Skip to content

Commit 4ac43b5

Browse files
authored
[LV] Restrict widest induction type to be IntegerType (NFC) (#128173)
As the name of the function suggests, convertPointerToIntegerType should return an IntegerType instead of a Type, and should only ever be called with integer or ptr type. Fix the callers getWiderType, and addInductionPhi to narrow the type of WidestIndTy to IntegerType, stripping unclear casts. While at it, rename convertPointerToIntegerType and getWiderType for clarity.
1 parent ab9cd53 commit 4ac43b5

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class LoopVectorizationLegality {
308308
RecurrenceSet &getFixedOrderRecurrences() { return FixedOrderRecurrences; }
309309

310310
/// Returns the widest induction type.
311-
Type *getWidestInductionType() { return WidestIndTy; }
311+
IntegerType *getWidestInductionType() { return WidestIndTy; }
312312

313313
/// Returns True if given store is a final invariant store of one of the
314314
/// reductions found in the loop.
@@ -595,7 +595,7 @@ class LoopVectorizationLegality {
595595
RecurrenceSet FixedOrderRecurrences;
596596

597597
/// Holds the widest induction type encountered.
598-
Type *WidestIndTy = nullptr;
598+
IntegerType *WidestIndTy = nullptr;
599599

600600
/// Allowed outside users. This holds the variables that can be accessed from
601601
/// outside the loop.

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -395,24 +395,25 @@ static bool isUniformLoopNest(Loop *Lp, Loop *OuterLp) {
395395
return true;
396396
}
397397

398-
static Type *convertPointerToIntegerType(const DataLayout &DL, Type *Ty) {
398+
static IntegerType *getInductionIntegerTy(const DataLayout &DL, Type *Ty) {
399+
assert(Ty->isIntOrPtrTy() && "Expected integer or pointer type");
400+
399401
if (Ty->isPointerTy())
400-
return DL.getIntPtrType(Ty);
402+
return DL.getIntPtrType(Ty->getContext(), Ty->getPointerAddressSpace());
401403

402404
// It is possible that char's or short's overflow when we ask for the loop's
403405
// trip count, work around this by changing the type size.
404406
if (Ty->getScalarSizeInBits() < 32)
405407
return Type::getInt32Ty(Ty->getContext());
406408

407-
return Ty;
409+
return cast<IntegerType>(Ty);
408410
}
409411

410-
static Type *getWiderType(const DataLayout &DL, Type *Ty0, Type *Ty1) {
411-
Ty0 = convertPointerToIntegerType(DL, Ty0);
412-
Ty1 = convertPointerToIntegerType(DL, Ty1);
413-
if (Ty0->getScalarSizeInBits() > Ty1->getScalarSizeInBits())
414-
return Ty0;
415-
return Ty1;
412+
static IntegerType *getWiderInductionTy(const DataLayout &DL, Type *Ty0,
413+
Type *Ty1) {
414+
IntegerType *TyA = getInductionIntegerTy(DL, Ty0);
415+
IntegerType *TyB = getInductionIntegerTy(DL, Ty1);
416+
return TyA->getScalarSizeInBits() > TyB->getScalarSizeInBits() ? TyA : TyB;
416417
}
417418

418419
/// Check that the instruction has outside loop users and is not an
@@ -692,12 +693,15 @@ void LoopVectorizationLegality::addInductionPhi(
692693
Type *PhiTy = Phi->getType();
693694
const DataLayout &DL = Phi->getDataLayout();
694695

696+
assert((PhiTy->isIntOrPtrTy() || PhiTy->isFloatingPointTy()) &&
697+
"Expected int, ptr, or FP induction phi type");
698+
695699
// Get the widest type.
696-
if (!PhiTy->isFloatingPointTy()) {
700+
if (PhiTy->isIntOrPtrTy()) {
697701
if (!WidestIndTy)
698-
WidestIndTy = convertPointerToIntegerType(DL, PhiTy);
702+
WidestIndTy = getInductionIntegerTy(DL, PhiTy);
699703
else
700-
WidestIndTy = getWiderType(DL, PhiTy, WidestIndTy);
704+
WidestIndTy = getWiderInductionTy(DL, PhiTy, WidestIndTy);
701705
}
702706

703707
// Int inductions are special because we only allow one IV.

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,8 +2353,8 @@ static bool isIndvarOverflowCheckKnownFalse(
23532353
// Always be conservative if we don't know the exact unroll factor.
23542354
unsigned MaxUF = UF ? *UF : Cost->TTI.getMaxInterleaveFactor(VF);
23552355

2356-
Type *IdxTy = Cost->Legal->getWidestInductionType();
2357-
APInt MaxUIntTripCount = cast<IntegerType>(IdxTy)->getMask();
2356+
IntegerType *IdxTy = Cost->Legal->getWidestInductionType();
2357+
APInt MaxUIntTripCount = IdxTy->getMask();
23582358

23592359
// We know the runtime overflow check is known false iff the (max) trip-count
23602360
// is known and (max) trip-count + (VF * UF) does not overflow in the type of

0 commit comments

Comments
 (0)