Skip to content

Commit c3fda44

Browse files
committed
[VPlan] Use VPBuilder to create scalar IV steps and derived IV (NFCI).
Extend VPBuilder to allow creating VPDerivedIVRecipe, VPScalarCastRecipe and VPScalarIVStepsRecipe. Use them to simplify the code to create scalar IV steps slightly.
1 parent 2222e27 commit c3fda44

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ class VPBuilder {
4747
VPBasicBlock::iterator InsertPt = VPBasicBlock::iterator();
4848

4949
/// Insert \p VPI in BB at InsertPt if BB is set.
50-
VPInstruction *tryInsertInstruction(VPInstruction *VPI) {
50+
template <typename T> T *tryInsertInstruction(T *R) {
5151
if (BB)
52-
BB->insert(VPI, InsertPt);
53-
return VPI;
52+
BB->insert(R, InsertPt);
53+
return R;
5454
}
5555

5656
VPInstruction *createInstruction(unsigned Opcode,
@@ -210,6 +210,27 @@ class VPBuilder {
210210
new VPInstruction(Instruction::ICmp, Pred, A, B, DL, Name));
211211
}
212212

213+
VPDerivedIVRecipe *createDerivedIV(InductionDescriptor::InductionKind Kind,
214+
FPMathOperator *FPBinOp, VPValue *Start,
215+
VPCanonicalIVPHIRecipe *CanonicalIV,
216+
VPValue *Step) {
217+
return tryInsertInstruction(
218+
new VPDerivedIVRecipe(Kind, FPBinOp, Start, CanonicalIV, Step));
219+
}
220+
221+
VPScalarCastRecipe *createScalarCast(Instruction::CastOps Opcode, VPValue *Op,
222+
Type *ResultTy) {
223+
return tryInsertInstruction(new VPScalarCastRecipe(Opcode, Op, ResultTy));
224+
}
225+
226+
VPScalarIVStepsRecipe *
227+
createScalarIVSteps(Instruction::BinaryOps InductionOpcode,
228+
FPMathOperator *FPBinOp, VPValue *IV, VPValue *Step) {
229+
return tryInsertInstruction(new VPScalarIVStepsRecipe(
230+
IV, Step, InductionOpcode,
231+
FPBinOp ? FPBinOp->getFastMathFlags() : FastMathFlags()));
232+
}
233+
213234
//===--------------------------------------------------------------------===//
214235
// RAII helpers.
215236
//===--------------------------------------------------------------------===//

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,12 @@ static VPScalarIVStepsRecipe *
527527
createScalarIVSteps(VPlan &Plan, InductionDescriptor::InductionKind Kind,
528528
Instruction::BinaryOps InductionOpcode,
529529
FPMathOperator *FPBinOp, Instruction *TruncI,
530-
VPValue *StartV, VPValue *Step, VPBasicBlock::iterator IP) {
530+
VPValue *StartV, VPValue *Step, VPBuilder &Builder) {
531531
VPBasicBlock *HeaderVPBB = Plan.getVectorLoopRegion()->getEntryBasicBlock();
532532
VPCanonicalIVPHIRecipe *CanonicalIV = Plan.getCanonicalIV();
533533
VPSingleDefRecipe *BaseIV = CanonicalIV;
534534
if (!CanonicalIV->isCanonical(Kind, StartV, Step)) {
535-
BaseIV = new VPDerivedIVRecipe(Kind, FPBinOp, StartV, CanonicalIV, Step);
536-
HeaderVPBB->insert(BaseIV, IP);
535+
BaseIV = Builder.createDerivedIV(Kind, FPBinOp, StartV, CanonicalIV, Step);
537536
}
538537

539538
// Truncate base induction if needed.
@@ -545,8 +544,7 @@ createScalarIVSteps(VPlan &Plan, InductionDescriptor::InductionKind Kind,
545544
assert(ResultTy->getScalarSizeInBits() > TruncTy->getScalarSizeInBits() &&
546545
"Not truncating.");
547546
assert(ResultTy->isIntegerTy() && "Truncation requires an integer type");
548-
BaseIV = new VPScalarCastRecipe(Instruction::Trunc, BaseIV, TruncTy);
549-
HeaderVPBB->insert(BaseIV, IP);
547+
BaseIV = Builder.createScalarCast(Instruction::Trunc, BaseIV, TruncTy);
550548
ResultTy = TruncTy;
551549
}
552550

@@ -556,17 +554,13 @@ createScalarIVSteps(VPlan &Plan, InductionDescriptor::InductionKind Kind,
556554
assert(StepTy->getScalarSizeInBits() > ResultTy->getScalarSizeInBits() &&
557555
"Not truncating.");
558556
assert(StepTy->isIntegerTy() && "Truncation requires an integer type");
559-
Step = new VPScalarCastRecipe(Instruction::Trunc, Step, ResultTy);
560557
auto *VecPreheader =
561558
cast<VPBasicBlock>(HeaderVPBB->getSingleHierarchicalPredecessor());
562-
VecPreheader->appendRecipe(Step->getDefiningRecipe());
559+
VPBuilder::InsertPointGuard Guard(Builder);
560+
Builder.setInsertPoint(VecPreheader);
561+
Step = Builder.createScalarCast(Instruction::Trunc, Step, ResultTy);
563562
}
564-
565-
VPScalarIVStepsRecipe *Steps = new VPScalarIVStepsRecipe(
566-
BaseIV, Step, InductionOpcode,
567-
FPBinOp ? FPBinOp->getFastMathFlags() : FastMathFlags());
568-
HeaderVPBB->insert(Steps, IP);
569-
return Steps;
563+
return Builder.createScalarIVSteps(InductionOpcode, FPBinOp, BaseIV, Step);
570564
}
571565

572566
/// Legalize VPWidenPointerInductionRecipe, by replacing it with a PtrAdd
@@ -582,7 +576,7 @@ static void legalizeAndOptimizeInductions(VPlan &Plan) {
582576
SmallVector<VPRecipeBase *> ToRemove;
583577
VPBasicBlock *HeaderVPBB = Plan.getVectorLoopRegion()->getEntryBasicBlock();
584578
bool HasOnlyVectorVFs = !Plan.hasVF(ElementCount::getFixed(1));
585-
VPBasicBlock::iterator InsertPt = HeaderVPBB->getFirstNonPhi();
579+
VPBuilder Builder(HeaderVPBB, HeaderVPBB->getFirstNonPhi());
586580
for (VPRecipeBase &Phi : HeaderVPBB->phis()) {
587581
// Replace wide pointer inductions which have only their scalars used by
588582
// PtrAdd(IndStart, ScalarIVSteps (0, Step)).
@@ -596,7 +590,7 @@ static void legalizeAndOptimizeInductions(VPlan &Plan) {
596590
VPValue *StepV = PtrIV->getOperand(1);
597591
VPScalarIVStepsRecipe *Steps = createScalarIVSteps(
598592
Plan, InductionDescriptor::IK_IntInduction, Instruction::Add, nullptr,
599-
nullptr, StartV, StepV, InsertPt);
593+
nullptr, StartV, StepV, Builder);
600594

601595
auto *Recipe = new VPInstruction(VPInstruction::PtrAdd,
602596
{PtrIV->getStartValue(), Steps},
@@ -622,7 +616,7 @@ static void legalizeAndOptimizeInductions(VPlan &Plan) {
622616
Plan, ID.getKind(), ID.getInductionOpcode(),
623617
dyn_cast_or_null<FPMathOperator>(ID.getInductionBinOp()),
624618
WideIV->getTruncInst(), WideIV->getStartValue(), WideIV->getStepValue(),
625-
InsertPt);
619+
Builder);
626620

627621
// Update scalar users of IV to use Step instead.
628622
if (!HasOnlyVectorVFs)

0 commit comments

Comments
 (0)