Skip to content

Commit a1c6b3c

Browse files
committed
[VPlan] Don't reset canonical IV start value.
Instead of re-setting the start value of the canonical IV when vectorizing the epilogue we can emit an Add VPInstruction to provide canonical IV value, adjusted by the resume value from the main loop. This is in preparation to make the canonical IV a VPValue defined by loop regions. It ensures that the canonical IV always starts at 0.
1 parent 1d01a84 commit a1c6b3c

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9590,7 +9590,10 @@ static SmallVector<Instruction *> preparePlanForEpilogueVectorLoop(
95909590
}) &&
95919591
"the canonical IV should only be used by its increment or "
95929592
"ScalarIVSteps when resetting the start value");
9593-
IV->setOperand(0, VPV);
9593+
VPBuilder Builder(Header, Header->getFirstNonPhi());
9594+
VPInstruction *Add = Builder.createNaryOp(Instruction::Add, {IV, VPV});
9595+
IV->replaceAllUsesWith(Add);
9596+
Add->setOperand(0, IV);
95949597

95959598
DenseMap<Value *, Value *> ToFrozen;
95969599
SmallVector<Instruction *> InstsToMove;

llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ m_c_Binary(const Op0_t &Op0, const Op1_t &Op1) {
431431
return AllRecipe_commutative_match<Opcode, Op0_t, Op1_t>(Op0, Op1);
432432
}
433433

434+
template <typename Op0_t, typename Op1_t>
435+
inline AllRecipe_match<Instruction::Add, Op0_t, Op1_t> m_Add(const Op0_t &Op0,
436+
const Op1_t &Op1) {
437+
return m_Binary<Instruction::Add, Op0_t, Op1_t>(Op0, Op1);
438+
}
439+
434440
template <typename Op0_t, typename Op1_t>
435441
inline AllRecipe_commutative_match<Instruction::Add, Op0_t, Op1_t>
436442
m_c_Add(const Op0_t &Op0, const Op1_t &Op1) {

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,17 @@ static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
12341234
if (!Plan->isUnrolled())
12351235
return;
12361236

1237+
if (match(Def, m_Add(m_VPValue(X), m_VPValue(Y))) && Y->isLiveIn() &&
1238+
isa<VPPhi>(X)) {
1239+
auto *Phi = cast<VPPhi>(X);
1240+
if (Phi->getOperand(1) != Def && match(Phi->getOperand(0), m_ZeroInt()) &&
1241+
Phi->getNumUsers() == 1 && (*Phi->user_begin() == &R)) {
1242+
Phi->setOperand(0, Y);
1243+
Def->replaceAllUsesWith(Phi);
1244+
return;
1245+
}
1246+
}
1247+
12371248
// VPVectorPointer for part 0 can be replaced by their start pointer.
12381249
if (auto *VecPtr = dyn_cast<VPVectorPointerRecipe>(&R)) {
12391250
if (VecPtr->isFirstPart()) {

0 commit comments

Comments
 (0)