Skip to content

Commit 53ea522

Browse files
authored
[LV] Introduce and use VPBuilder::createScalarZExtOrTrunc [nfc] (#144946)
Reduce redundant code, make the flow slightly easier to read.
1 parent 2f3a8fd commit 53ea522

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ class VPBuilder {
278278
new VPInstructionWithType(Opcode, Op, ResultTy, {}, DL));
279279
}
280280

281+
VPValue *createScalarZExtOrTrunc(VPValue *Op, Type *ResultTy, Type *SrcTy,
282+
DebugLoc DL) {
283+
if (ResultTy == SrcTy)
284+
return Op;
285+
Instruction::CastOps CastOp =
286+
ResultTy->getScalarSizeInBits() < SrcTy->getScalarSizeInBits()
287+
? Instruction::Trunc
288+
: Instruction::ZExt;
289+
return createScalarCast(CastOp, Op, ResultTy, DL);
290+
}
291+
281292
VPWidenCastRecipe *createWidenCast(Instruction::CastOps Opcode, VPValue *Op,
282293
Type *ResultTy) {
283294
return tryInsertInstruction(new VPWidenCastRecipe(Opcode, Op, ResultTy));

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -797,15 +797,8 @@ static VPValue *optimizeEarlyExitInductionUser(VPlan &Plan,
797797
VPValue *FirstActiveLane =
798798
B.createNaryOp(VPInstruction::FirstActiveLane, Mask, DL);
799799
Type *FirstActiveLaneType = TypeInfo.inferScalarType(FirstActiveLane);
800-
if (CanonicalIVType != FirstActiveLaneType) {
801-
Instruction::CastOps CastOp =
802-
CanonicalIVType->getScalarSizeInBits() <
803-
FirstActiveLaneType->getScalarSizeInBits()
804-
? Instruction::Trunc
805-
: Instruction::ZExt;
806-
FirstActiveLane =
807-
B.createScalarCast(CastOp, FirstActiveLane, CanonicalIVType, DL);
808-
}
800+
FirstActiveLane = B.createScalarZExtOrTrunc(FirstActiveLane, CanonicalIVType,
801+
FirstActiveLaneType, DL);
809802
EndValue = B.createNaryOp(Instruction::Add, {EndValue, FirstActiveLane}, DL);
810803

811804
// `getOptimizableIVOf()` always returns the pre-incremented IV, so if it
@@ -2182,13 +2175,10 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
21822175
VPValue *MaxEVL = &Plan.getVF();
21832176
// Emit VPScalarCastRecipe in preheader if VF is not a 32 bits integer.
21842177
VPBuilder Builder(LoopRegion->getPreheaderVPBB());
2185-
if (unsigned VFSize =
2186-
TypeInfo.inferScalarType(MaxEVL)->getScalarSizeInBits();
2187-
VFSize != 32) {
2188-
MaxEVL = Builder.createScalarCast(
2189-
VFSize > 32 ? Instruction::Trunc : Instruction::ZExt, MaxEVL,
2190-
Type::getInt32Ty(Ctx), DebugLoc());
2191-
}
2178+
MaxEVL = Builder.createScalarZExtOrTrunc(MaxEVL, Type::getInt32Ty(Ctx),
2179+
TypeInfo.inferScalarType(MaxEVL),
2180+
DebugLoc());
2181+
21922182
Builder.setInsertPoint(Header, Header->getFirstNonPhi());
21932183
PrevEVL = Builder.createScalarPhi({MaxEVL, &EVL}, DebugLoc(), "prev.evl");
21942184
}
@@ -2286,6 +2276,7 @@ bool VPlanTransforms::tryAddExplicitVectorLength(
22862276
return false;
22872277

22882278
auto *CanonicalIVPHI = Plan.getCanonicalIV();
2279+
auto *CanIVTy = CanonicalIVPHI->getScalarType();
22892280
VPValue *StartV = CanonicalIVPHI->getStartValue();
22902281

22912282
// Create the ExplicitVectorLengthPhi recipe in the main loop.
@@ -2297,8 +2288,8 @@ bool VPlanTransforms::tryAddExplicitVectorLength(
22972288
Instruction::Sub, {Plan.getTripCount(), EVLPhi}, DebugLoc(), "avl");
22982289
if (MaxSafeElements) {
22992290
// Support for MaxSafeDist for correct loop emission.
2300-
VPValue *AVLSafe = Plan.getOrAddLiveIn(
2301-
ConstantInt::get(CanonicalIVPHI->getScalarType(), *MaxSafeElements));
2291+
VPValue *AVLSafe =
2292+
Plan.getOrAddLiveIn(ConstantInt::get(CanIVTy, *MaxSafeElements));
23022293
VPValue *Cmp = Builder.createICmp(ICmpInst::ICMP_ULT, AVL, AVLSafe);
23032294
AVL = Builder.createSelect(Cmp, AVL, AVLSafe, DebugLoc(), "safe_avl");
23042295
}
@@ -2308,13 +2299,12 @@ bool VPlanTransforms::tryAddExplicitVectorLength(
23082299
auto *CanonicalIVIncrement =
23092300
cast<VPInstruction>(CanonicalIVPHI->getBackedgeValue());
23102301
Builder.setInsertPoint(CanonicalIVIncrement);
2311-
VPSingleDefRecipe *OpVPEVL = VPEVL;
2312-
if (unsigned IVSize = CanonicalIVPHI->getScalarType()->getScalarSizeInBits();
2313-
IVSize != 32) {
2314-
OpVPEVL = Builder.createScalarCast(
2315-
IVSize < 32 ? Instruction::Trunc : Instruction::ZExt, OpVPEVL,
2316-
CanonicalIVPHI->getScalarType(), CanonicalIVIncrement->getDebugLoc());
2317-
}
2302+
VPValue *OpVPEVL = VPEVL;
2303+
2304+
auto *I32Ty = Type::getInt32Ty(CanIVTy->getContext());
2305+
OpVPEVL = Builder.createScalarZExtOrTrunc(
2306+
OpVPEVL, CanIVTy, I32Ty, CanonicalIVIncrement->getDebugLoc());
2307+
23182308
auto *NextEVLIV = Builder.createOverflowingOp(
23192309
Instruction::Add, {OpVPEVL, EVLPhi},
23202310
{CanonicalIVIncrement->hasNoUnsignedWrap(),

0 commit comments

Comments
 (0)