Skip to content

Commit 6e83937

Browse files
committed
[VPlan] Add getConstantInt helpers for constant int creation (NFC).
Add getConstantInt helper methods to VPlan to simplify the common pattern of creating constant integer live-ins. Suggested as follow-up in #164127.
1 parent 197de78 commit 6e83937

File tree

7 files changed

+61
-70
lines changed

7 files changed

+61
-70
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ class VPBuilder {
280280

281281
VPValue *createElementCount(Type *Ty, ElementCount EC) {
282282
VPlan &Plan = *getInsertBlock()->getPlan();
283-
VPValue *RuntimeEC =
284-
Plan.getOrAddLiveIn(ConstantInt::get(Ty, EC.getKnownMinValue()));
283+
VPValue *RuntimeEC = Plan.getConstantInt(Ty, EC.getKnownMinValue());
285284
if (EC.isScalable()) {
286285
VPValue *VScale = createNaryOp(VPInstruction::VScale, {}, Ty);
287286
RuntimeEC = EC.getKnownMinValue() == 1

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7752,8 +7752,7 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
77527752
if (CM.isPredicatedInst(I)) {
77537753
SmallVector<VPValue *> Ops(Operands);
77547754
VPValue *Mask = getBlockInMask(Builder.getInsertBlock());
7755-
VPValue *One =
7756-
Plan.getOrAddLiveIn(ConstantInt::get(I->getType(), 1u, false));
7755+
VPValue *One = Plan.getConstantInt(I->getType(), 1u);
77577756
auto *SafeRHS = Builder.createSelect(Mask, Ops[1], One, I->getDebugLoc());
77587757
Ops[1] = SafeRHS;
77597758
return new VPWidenRecipe(*I, Ops);
@@ -7806,11 +7805,10 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
78067805
}
78077806
case Instruction::ExtractValue: {
78087807
SmallVector<VPValue *> NewOps(Operands);
7809-
Type *I32Ty = IntegerType::getInt32Ty(I->getContext());
78107808
auto *EVI = cast<ExtractValueInst>(I);
78117809
assert(EVI->getNumIndices() == 1 && "Expected one extractvalue index");
78127810
unsigned Idx = EVI->getIndices()[0];
7813-
NewOps.push_back(Plan.getOrAddLiveIn(ConstantInt::get(I32Ty, Idx, false)));
7811+
NewOps.push_back(Plan.getConstantInt(32, Idx));
78147812
return new VPWidenRecipe(*I, NewOps);
78157813
}
78167814
};
@@ -8179,8 +8177,7 @@ VPRecipeBuilder::tryToCreatePartialReduction(Instruction *Reduction,
81798177
"Expected an ADD or SUB operation for predicated partial "
81808178
"reductions (because the neutral element in the mask is zero)!");
81818179
Cond = getBlockInMask(Builder.getInsertBlock());
8182-
VPValue *Zero =
8183-
Plan.getOrAddLiveIn(ConstantInt::get(Reduction->getType(), 0));
8180+
VPValue *Zero = Plan.getConstantInt(Reduction->getType(), 0);
81848181
BinOp = Builder.createSelect(Cond, BinOp, Zero, Reduction->getDebugLoc());
81858182
}
81868183
return new VPPartialReductionRecipe(ReductionOpcode, Accumulator, BinOp, Cond,
@@ -8643,7 +8640,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
86438640
} else if (PhiR->isInLoop() && Kind == RecurKind::AddChainWithSubs &&
86448641
CurrentLinkI->getOpcode() == Instruction::Sub) {
86458642
Type *PhiTy = PhiR->getUnderlyingValue()->getType();
8646-
auto *Zero = Plan->getOrAddLiveIn(ConstantInt::get(PhiTy, 0));
8643+
auto *Zero = Plan->getConstantInt(PhiTy, 0);
86478644
VPWidenRecipe *Sub = new VPWidenRecipe(
86488645
Instruction::Sub, {Zero, CurrentLink->getOperand(1)}, {},
86498646
VPIRMetadata(), CurrentLinkI->getDebugLoc());
@@ -8857,8 +8854,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
88578854
ToDelete.push_back(Select);
88588855

88598856
// Convert the reduction phi to operate on bools.
8860-
PhiR->setOperand(0, Plan->getOrAddLiveIn(ConstantInt::getFalse(
8861-
OrigLoop->getHeader()->getContext())));
8857+
PhiR->setOperand(0, Plan->getFalse());
88628858
continue;
88638859
}
88648860

@@ -8880,9 +8876,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
88808876
unsigned ScaleFactor =
88818877
RecipeBuilder.getScalingForReduction(RdxDesc.getLoopExitInstr())
88828878
.value_or(1);
8883-
Type *I32Ty = IntegerType::getInt32Ty(PhiTy->getContext());
8884-
auto *ScaleFactorVPV =
8885-
Plan->getOrAddLiveIn(ConstantInt::get(I32Ty, ScaleFactor));
8879+
auto *ScaleFactorVPV = Plan->getConstantInt(32, ScaleFactor);
88868880
VPValue *StartV = PHBuilder.createNaryOp(
88878881
VPInstruction::ReductionStartVector,
88888882
{PhiR->getStartValue(), Iden, ScaleFactorVPV},

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4393,15 +4393,25 @@ class VPlan {
43934393
}
43944394

43954395
/// Return a VPValue wrapping i1 true.
4396-
VPValue *getTrue() {
4397-
LLVMContext &Ctx = getContext();
4398-
return getOrAddLiveIn(ConstantInt::getTrue(Ctx));
4399-
}
4396+
VPValue *getTrue() { return getConstantInt(1, 1); }
44004397

44014398
/// Return a VPValue wrapping i1 false.
4402-
VPValue *getFalse() {
4403-
LLVMContext &Ctx = getContext();
4404-
return getOrAddLiveIn(ConstantInt::getFalse(Ctx));
4399+
VPValue *getFalse() { return getConstantInt(1, 0); }
4400+
4401+
/// Return a VPValue wrapping a ConstantInt with the given type and value.
4402+
VPValue *getConstantInt(Type *Ty, uint64_t Val, bool IsSigned = false) {
4403+
return getOrAddLiveIn(ConstantInt::get(Ty, Val, IsSigned));
4404+
}
4405+
4406+
/// Return a VPValue wrapping a ConstantInt with the given bitwidth and value.
4407+
VPValue *getConstantInt(unsigned BitWidth, uint64_t Val,
4408+
bool IsSigned = false) {
4409+
return getConstantInt(APInt(BitWidth, Val, IsSigned));
4410+
}
4411+
4412+
/// Return a VPValue wrapping a ConstantInt with the given APInt value.
4413+
VPValue *getConstantInt(const APInt &Val) {
4414+
return getOrAddLiveIn(ConstantInt::get(getContext(), Val));
44054415
}
44064416

44074417
/// Return the live-in VPValue for \p V, if there is one or nullptr otherwise.

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,7 @@ void VPlanTransforms::addMiddleCheck(VPlan &Plan,
612612
if (!RequiresScalarEpilogueCheck)
613613
Cmp = Plan.getFalse();
614614
else if (TailFolded)
615-
Cmp = Plan.getOrAddLiveIn(
616-
ConstantInt::getTrue(IntegerType::getInt1Ty(Plan.getContext())));
615+
Cmp = Plan.getTrue();
617616
else
618617
Cmp = Builder.createICmp(CmpInst::ICMP_EQ, Plan.getTripCount(),
619618
&Plan.getVectorTripCount(), LatchDL, "cmp.n");
@@ -712,8 +711,8 @@ void VPlanTransforms::addMinimumIterationCheck(
712711
// additional overflow check is required before entering the vector loop.
713712

714713
// Get the maximum unsigned value for the type.
715-
VPValue *MaxUIntTripCount = Plan.getOrAddLiveIn(ConstantInt::get(
716-
TripCountTy, cast<IntegerType>(TripCountTy)->getMask()));
714+
VPValue *MaxUIntTripCount =
715+
Plan.getConstantInt(cast<IntegerType>(TripCountTy)->getMask());
717716
VPValue *DistanceToMax = Builder.createNaryOp(
718717
Instruction::Sub, {MaxUIntTripCount, TripCountVPV},
719718
DebugLoc::getUnknown());

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,7 @@ static void legalizeAndOptimizeInductions(VPlan &Plan) {
699699
continue;
700700

701701
const InductionDescriptor &ID = PtrIV->getInductionDescriptor();
702-
VPValue *StartV =
703-
Plan.getOrAddLiveIn(ConstantInt::get(ID.getStep()->getType(), 0));
702+
VPValue *StartV = Plan.getConstantInt(ID.getStep()->getType(), 0);
704703
VPValue *StepV = PtrIV->getOperand(1);
705704
VPScalarIVStepsRecipe *Steps = createScalarIVSteps(
706705
Plan, InductionDescriptor::IK_IntInduction, Instruction::Add, nullptr,
@@ -836,7 +835,7 @@ static VPValue *optimizeEarlyExitInductionUser(VPlan &Plan,
836835
// changed it means the exit is using the incremented value, so we need to
837836
// add the step.
838837
if (Incoming != WideIV) {
839-
VPValue *One = Plan.getOrAddLiveIn(ConstantInt::get(CanonicalIVType, 1));
838+
VPValue *One = Plan.getConstantInt(CanonicalIVType, 1);
840839
EndValue = B.createNaryOp(Instruction::Add, {EndValue, One}, DL);
841840
}
842841

@@ -882,7 +881,7 @@ static VPValue *optimizeLatchExitInductionUser(
882881
return B.createNaryOp(Instruction::Sub, {EndValue, Step}, {}, "ind.escape");
883882
if (ScalarTy->isPointerTy()) {
884883
Type *StepTy = TypeInfo.inferScalarType(Step);
885-
auto *Zero = Plan.getOrAddLiveIn(ConstantInt::get(StepTy, 0));
884+
auto *Zero = Plan.getConstantInt(StepTy, 0);
886885
return B.createPtrAdd(EndValue,
887886
B.createNaryOp(Instruction::Sub, {Zero, Step}),
888887
DebugLoc::getUnknown(), "ind.escape");
@@ -1574,9 +1573,9 @@ static bool optimizeVectorInductionWidthForTCAndVFUF(VPlan &Plan,
15741573
continue;
15751574

15761575
// Update IV operands and comparison bound to use new narrower type.
1577-
auto *NewStart = Plan.getOrAddLiveIn(ConstantInt::get(NewIVTy, 0));
1576+
auto *NewStart = Plan.getConstantInt(NewIVTy, 0);
15781577
WideIV->setStartValue(NewStart);
1579-
auto *NewStep = Plan.getOrAddLiveIn(ConstantInt::get(NewIVTy, 1));
1578+
auto *NewStep = Plan.getConstantInt(NewIVTy, 1);
15801579
WideIV->setStepValue(NewStep);
15811580

15821581
auto *NewBTC = new VPWidenCastRecipe(
@@ -1695,8 +1694,7 @@ static bool tryToReplaceALMWithWideALM(VPlan &Plan, ElementCount VF,
16951694

16961695
// When using wide lane masks, the return type of the get.active.lane.mask
16971696
// intrinsic is VF x UF (last operand).
1698-
VPValue *ALMMultiplier =
1699-
Plan.getOrAddLiveIn(ConstantInt::get(IntegerType::getInt64Ty(Ctx), UF));
1697+
VPValue *ALMMultiplier = Plan.getConstantInt(64, UF);
17001698
EntryALM->setOperand(2, ALMMultiplier);
17011699
LoopALM->setOperand(2, ALMMultiplier);
17021700

@@ -2403,7 +2401,7 @@ static VPActiveLaneMaskPHIRecipe *addVPLaneMaskPhiAndUpdateExitBranch(
24032401

24042402
// Create the active lane mask instruction in the VPlan preheader.
24052403
VPValue *ALMMultiplier =
2406-
Plan.getOrAddLiveIn(ConstantInt::get(TopRegion->getCanonicalIVType(), 1));
2404+
Plan.getConstantInt(TopRegion->getCanonicalIVType(), 1);
24072405
auto *EntryALM = Builder.createNaryOp(VPInstruction::ActiveLaneMask,
24082406
{EntryIncrement, TC, ALMMultiplier}, DL,
24092407
"active.lane.mask.entry");
@@ -2790,8 +2788,7 @@ void VPlanTransforms::addExplicitVectorLength(
27902788

27912789
if (MaxSafeElements) {
27922790
// Support for MaxSafeDist for correct loop emission.
2793-
VPValue *AVLSafe =
2794-
Plan.getOrAddLiveIn(ConstantInt::get(CanIVTy, *MaxSafeElements));
2791+
VPValue *AVLSafe = Plan.getConstantInt(CanIVTy, *MaxSafeElements);
27952792
VPValue *Cmp = Builder.createICmp(ICmpInst::ICMP_ULT, AVL, AVLSafe);
27962793
AVL = Builder.createSelect(Cmp, AVL, AVLSafe, DebugLoc::getUnknown(),
27972794
"safe_avl");
@@ -2904,9 +2901,8 @@ void VPlanTransforms::canonicalizeEVLLoops(VPlan &Plan) {
29042901

29052902
Type *AVLTy = VPTypeAnalysis(Plan).inferScalarType(AVLNext);
29062903
VPBuilder Builder(LatchExitingBr);
2907-
VPValue *Cmp =
2908-
Builder.createICmp(CmpInst::ICMP_EQ, AVLNext,
2909-
Plan.getOrAddLiveIn(ConstantInt::getNullValue(AVLTy)));
2904+
VPValue *Cmp = Builder.createICmp(CmpInst::ICMP_EQ, AVLNext,
2905+
Plan.getConstantInt(AVLTy, 0));
29102906
Builder.createNaryOp(VPInstruction::BranchOnCond, Cmp);
29112907
LatchExitingBr->eraseFromParent();
29122908
}
@@ -2930,8 +2926,7 @@ void VPlanTransforms::replaceSymbolicStrides(
29302926
// Only handle constant strides for now.
29312927
continue;
29322928

2933-
auto *CI =
2934-
Plan.getOrAddLiveIn(ConstantInt::get(Stride->getType(), *StrideConst));
2929+
auto *CI = Plan.getConstantInt(*StrideConst);
29352930
if (VPValue *StrideVPV = Plan.getLiveIn(StrideV))
29362931
StrideVPV->replaceUsesWithIf(CI, CanUseVersionedStride);
29372932

@@ -2946,7 +2941,7 @@ void VPlanTransforms::replaceSymbolicStrides(
29462941
unsigned BW = U->getType()->getScalarSizeInBits();
29472942
APInt C =
29482943
isa<SExtInst>(U) ? StrideConst->sext(BW) : StrideConst->zext(BW);
2949-
VPValue *CI = Plan.getOrAddLiveIn(ConstantInt::get(U->getType(), C));
2944+
VPValue *CI = Plan.getConstantInt(C);
29502945
StrideVPV->replaceUsesWithIf(CI, CanUseVersionedStride);
29512946
}
29522947
RewriteMap[StrideV] = PSE.getSCEV(StrideV);
@@ -3125,8 +3120,7 @@ void VPlanTransforms::createInterleaveGroups(
31253120
DL.getTypeAllocSize(getLoadStoreType(IRInsertPos)) *
31263121
IG->getIndex(IRInsertPos),
31273122
/*IsSigned=*/true);
3128-
VPValue *OffsetVPV =
3129-
Plan.getOrAddLiveIn(ConstantInt::get(Plan.getContext(), -Offset));
3123+
VPValue *OffsetVPV = Plan.getConstantInt(-Offset);
31303124
VPBuilder B(InsertPos);
31313125
Addr = B.createNoWrapPtrAdd(InsertPos->getAddr(), OffsetVPV, NW);
31323126
}
@@ -3867,8 +3861,7 @@ void VPlanTransforms::materializeBackedgeTakenCount(VPlan &Plan,
38673861
VPBuilder Builder(VectorPH, VectorPH->begin());
38683862
auto *TCTy = VPTypeAnalysis(Plan).inferScalarType(Plan.getTripCount());
38693863
auto *TCMO = Builder.createNaryOp(
3870-
Instruction::Sub,
3871-
{Plan.getTripCount(), Plan.getOrAddLiveIn(ConstantInt::get(TCTy, 1))},
3864+
Instruction::Sub, {Plan.getTripCount(), Plan.getConstantInt(TCTy, 1)},
38723865
DebugLoc::getCompilerGenerated(), "trip.count.minus.1");
38733866
BTC->replaceAllUsesWith(TCMO);
38743867
}
@@ -3993,9 +3986,8 @@ void VPlanTransforms::materializeVectorTripCount(VPlan &Plan,
39933986
if (TailByMasking) {
39943987
TC = Builder.createNaryOp(
39953988
Instruction::Add,
3996-
{TC, Builder.createNaryOp(
3997-
Instruction::Sub,
3998-
{Step, Plan.getOrAddLiveIn(ConstantInt::get(TCTy, 1))})},
3989+
{TC, Builder.createNaryOp(Instruction::Sub,
3990+
{Step, Plan.getConstantInt(TCTy, 1)})},
39993991
DebugLoc::getCompilerGenerated(), "n.rnd.up");
40003992
}
40013993

@@ -4017,8 +4009,8 @@ void VPlanTransforms::materializeVectorTripCount(VPlan &Plan,
40174009
if (RequiresScalarEpilogue) {
40184010
assert(!TailByMasking &&
40194011
"requiring scalar epilogue is not supported with fail folding");
4020-
VPValue *IsZero = Builder.createICmp(
4021-
CmpInst::ICMP_EQ, R, Plan.getOrAddLiveIn(ConstantInt::get(TCTy, 0)));
4012+
VPValue *IsZero =
4013+
Builder.createICmp(CmpInst::ICMP_EQ, R, Plan.getConstantInt(TCTy, 0));
40224014
R = Builder.createSelect(IsZero, Step, R);
40234015
}
40244016

@@ -4056,7 +4048,7 @@ void VPlanTransforms::materializeVFAndVFxUF(VPlan &Plan, VPBasicBlock *VectorPH,
40564048
}
40574049
VF.replaceAllUsesWith(RuntimeVF);
40584050

4059-
VPValue *UF = Plan.getOrAddLiveIn(ConstantInt::get(TCTy, Plan.getUF()));
4051+
VPValue *UF = Plan.getConstantInt(TCTy, Plan.getUF());
40604052
VPValue *MulByUF = Builder.createNaryOp(Instruction::Mul, {RuntimeVF, UF});
40614053
VFxUF.replaceAllUsesWith(MulByUF);
40624054
}
@@ -4346,7 +4338,7 @@ void VPlanTransforms::narrowInterleaveGroups(VPlan &Plan, ElementCount VF,
43464338
} else {
43474339
Inc->setOperand(1, UF);
43484340
Plan.getVF().replaceAllUsesWith(
4349-
Plan.getOrAddLiveIn(ConstantInt::get(CanIV->getScalarType(), 1)));
4341+
Plan.getConstantInt(CanIV->getScalarType(), 1));
43504342
}
43514343
removeDeadRecipes(Plan);
43524344
}

llvm/lib/Transforms/Vectorize/VPlanUnroll.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ class UnrollState {
6868
void unrollWidenInductionByUF(VPWidenInductionRecipe *IV,
6969
VPBasicBlock::iterator InsertPtForPhi);
7070

71-
VPValue *getConstantVPV(unsigned Part) {
71+
VPValue *getConstantInt(unsigned Part) {
7272
Type *CanIVIntTy = Plan.getVectorLoopRegion()->getCanonicalIVType();
73-
return Plan.getOrAddLiveIn(ConstantInt::get(CanIVIntTy, Part));
73+
return Plan.getConstantInt(CanIVIntTy, Part);
7474
}
7575

7676
public:
@@ -137,7 +137,7 @@ void UnrollState::unrollReplicateRegionByUF(VPRegionBlock *VPR) {
137137
for (const auto &[PartIR, Part0R] : zip(*PartIVPBB, *Part0VPBB)) {
138138
remapOperands(&PartIR, Part);
139139
if (auto *ScalarIVSteps = dyn_cast<VPScalarIVStepsRecipe>(&PartIR)) {
140-
ScalarIVSteps->addOperand(getConstantVPV(Part));
140+
ScalarIVSteps->addOperand(getConstantInt(Part));
141141
}
142142

143143
addRecipeForPart(&Part0R, &PartIR, Part);
@@ -249,7 +249,7 @@ void UnrollState::unrollHeaderPHIByUF(VPHeaderPHIRecipe *R,
249249
for (unsigned Part = 1; Part != UF; ++Part)
250250
VPV2Parts[VPI][Part - 1] = StartV;
251251
}
252-
Copy->addOperand(getConstantVPV(Part));
252+
Copy->addOperand(getConstantInt(Part));
253253
} else {
254254
assert(isa<VPActiveLaneMaskPHIRecipe>(R) &&
255255
"unexpected header phi recipe not needing unrolled part");
@@ -318,7 +318,7 @@ void UnrollState::unrollRecipeByUF(VPRecipeBase &R) {
318318
VPVectorPointerRecipe, VPVectorEndPointerRecipe>(Copy) ||
319319
match(Copy,
320320
m_VPInstruction<VPInstruction::CanonicalIVIncrementForPart>()))
321-
Copy->addOperand(getConstantVPV(Part));
321+
Copy->addOperand(getConstantInt(Part));
322322

323323
if (isa<VPVectorPointerRecipe, VPVectorEndPointerRecipe>(R))
324324
Copy->setOperand(0, R.getOperand(0));
@@ -474,8 +474,7 @@ cloneForLane(VPlan &Plan, VPBuilder &Builder, Type *IdxTy,
474474
if (LaneDefs != Def2LaneDefs.end())
475475
return LaneDefs->second[Lane.getKnownLane()];
476476

477-
VPValue *Idx =
478-
Plan.getOrAddLiveIn(ConstantInt::get(IdxTy, Lane.getKnownLane()));
477+
VPValue *Idx = Plan.getConstantInt(IdxTy, Lane.getKnownLane());
479478
return Builder.createNaryOp(Instruction::ExtractElement, {Op, Idx});
480479
}
481480

@@ -509,8 +508,7 @@ cloneForLane(VPlan &Plan, VPBuilder &Builder, Type *IdxTy,
509508
cast<VPInstruction>(Op)->getOperand(Lane.getKnownLane()));
510509
continue;
511510
}
512-
VPValue *Idx =
513-
Plan.getOrAddLiveIn(ConstantInt::get(IdxTy, Lane.getKnownLane()));
511+
VPValue *Idx = Plan.getConstantInt(IdxTy, Lane.getKnownLane());
514512
VPValue *Ext = Builder.createNaryOp(Instruction::ExtractElement, {Op, Idx});
515513
NewOps.push_back(Ext);
516514
}

0 commit comments

Comments
 (0)