@@ -311,7 +311,7 @@ void Float2IntPass::walkForwards() {
311
311
}
312
312
313
313
// If there is a valid transform to be done, do it.
314
- bool Float2IntPass::validateAndTransform () {
314
+ bool Float2IntPass::validateAndTransform (const DataLayout &DL ) {
315
315
bool MadeChange = false ;
316
316
317
317
// Iterate over every disjoint partition of the def-use graph.
@@ -321,8 +321,8 @@ bool Float2IntPass::validateAndTransform() {
321
321
Type *ConvertedToTy = nullptr ;
322
322
323
323
// For every member of the partition, union all the ranges together.
324
- for (auto MI = ECs.member_begin (It), ME = ECs.member_end ();
325
- MI != ME; ++MI) {
324
+ for (auto MI = ECs.member_begin (It), ME = ECs.member_end (); MI != ME;
325
+ ++MI) {
326
326
Instruction *I = *MI;
327
327
auto SeenI = SeenInsts.find (I);
328
328
if (SeenI == SeenInsts.end ())
@@ -352,8 +352,8 @@ bool Float2IntPass::validateAndTransform() {
352
352
353
353
// If the set was empty, or we failed, or the range is poisonous,
354
354
// bail out.
355
- if (ECs.member_begin (It) == ECs.member_end () || Fail ||
356
- R.isFullSet () || R. isSignWrappedSet ())
355
+ if (ECs.member_begin (It) == ECs.member_end () || Fail || R. isFullSet () ||
356
+ R.isSignWrappedSet ())
357
357
continue ;
358
358
assert (ConvertedToTy && " Must have set the convertedtoty by this point!" );
359
359
@@ -370,24 +370,29 @@ bool Float2IntPass::validateAndTransform() {
370
370
// Do we need more bits than are in the mantissa of the type we converted
371
371
// to? semanticsPrecision returns the number of mantissa bits plus one
372
372
// for the sign bit.
373
- unsigned MaxRepresentableBits
374
- = APFloat::semanticsPrecision (ConvertedToTy->getFltSemantics ()) - 1 ;
373
+ unsigned MaxRepresentableBits =
374
+ APFloat::semanticsPrecision (ConvertedToTy->getFltSemantics ()) - 1 ;
375
375
if (MinBW > MaxRepresentableBits) {
376
376
LLVM_DEBUG (dbgs () << " F2I: Value not guaranteed to be representable!\n " );
377
377
continue ;
378
378
}
379
- if (MinBW > 64 ) {
380
- LLVM_DEBUG (
381
- dbgs () << " F2I: Value requires more than 64 bits to represent!\n " );
382
- continue ;
383
- }
384
379
385
- // OK, R is known to be representable. Now pick a type for it.
386
- // FIXME: Pick the smallest legal type that will fit.
387
- Type *Ty = (MinBW > 32 ) ? Type::getInt64Ty (*Ctx) : Type::getInt32Ty (*Ctx);
380
+ // OK, R is known to be representable.
381
+ // Pick the smallest legal type that will fit.
382
+ Type *Ty = DL.getSmallestLegalIntType (*Ctx, MinBW);
383
+ if (!Ty) {
384
+ if (MinBW > 64 ) {
385
+ LLVM_DEBUG (dbgs () << " F2I: Value requires more than bits to represent "
386
+ " than the target supports!\n " );
387
+ continue ;
388
+ }
389
+
390
+ // Every supported target supports 64 and 32-bit
391
+ // integers.
392
+ Ty = (MinBW > 32 ) ? Type::getInt64Ty (*Ctx) : Type::getInt32Ty (*Ctx);
393
+ }
388
394
389
- for (auto MI = ECs.member_begin (It), ME = ECs.member_end ();
390
- MI != ME; ++MI)
395
+ for (auto MI = ECs.member_begin (It), ME = ECs.member_end (); MI != ME; ++MI)
391
396
convert (*MI, Ty);
392
397
MadeChange = true ;
393
398
}
@@ -491,7 +496,8 @@ bool Float2IntPass::runImpl(Function &F, const DominatorTree &DT) {
491
496
walkBackwards ();
492
497
walkForwards ();
493
498
494
- bool Modified = validateAndTransform ();
499
+ const DataLayout &DL = F.getParent ()->getDataLayout ();
500
+ bool Modified = validateAndTransform (DL);
495
501
if (Modified)
496
502
cleanup ();
497
503
return Modified;
0 commit comments