diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -4757,7 +4757,7 @@ assert(PN->getParent() == OrigLoop->getHeader() && "Non-header phis should have been handled elsewhere"); - VPValue *StartVPV = PhiR->getStartValue(); + VPValue *StartVPV = RdxDesc ? PhiR->getStartValue() : nullptr; Value *StartV = StartVPV ? StartVPV->getLiveInIRValue() : nullptr; // In order to support recurrences we need to be able to vectorize Phi nodes. // Phi nodes have cycles, so we need to vectorize them in two stages. This is @@ -8958,22 +8958,32 @@ if ((Recipe = tryToOptimizeInductionPHI(Phi, Operands))) return toVPRecipeResult(Recipe); + VPWidenPHIRecipe *PhiRecipe = nullptr; + VPValue *StartV = Operands[0]; if (Legal->isReductionVariable(Phi)) { RecurrenceDescriptor &RdxDesc = Legal->getReductionVars()[Phi]; assert(RdxDesc.getRecurrenceStartValue() == Phi->getIncomingValueForBlock(OrigLoop->getLoopPreheader())); - VPValue *StartV = Operands[0]; - auto *PhiRecipe = new VPWidenPHIRecipe(Phi, RdxDesc, *StartV); - PhisToFix.push_back(PhiRecipe); + PhiRecipe = new VPWidenPHIRecipe(Phi, RdxDesc, *StartV); // Record the incoming value from the backedge, so we can add the incoming // value from the backedge after all recipes have been created. recordRecipeOf(cast( Phi->getIncomingValueForBlock(OrigLoop->getLoopLatch()))); - return toVPRecipeResult(PhiRecipe); + PhisToFix.push_back(PhiRecipe); + } else { + PhiRecipe = new VPWidenPHIRecipe(Phi); + PhiRecipe->addOperand(StartV); + if (Legal->isFirstOrderRecurrence(Phi)) { + Value *Incoming = + Phi->getIncomingValueForBlock(OrigLoop->getLoopLatch()); + Instruction *IncI = dyn_cast(Incoming); + recordRecipeOf(IncI); + PhisToFix.push_back(PhiRecipe); + } } - return toVPRecipeResult(new VPWidenPHIRecipe(Phi)); + return toVPRecipeResult(PhiRecipe); } if (isa(Instr) && diff --git a/llvm/test/Transforms/LoopVectorize/first-order-recurrence-sink-replicate-region.ll b/llvm/test/Transforms/LoopVectorize/first-order-recurrence-sink-replicate-region.ll --- a/llvm/test/Transforms/LoopVectorize/first-order-recurrence-sink-replicate-region.ll +++ b/llvm/test/Transforms/LoopVectorize/first-order-recurrence-sink-replicate-region.ll @@ -10,7 +10,7 @@ ; CHECK-LABEL: sink_replicate_region_1 ; CHECK: VPlan 'Initial VPlan for VF={2},UF>=1' { ; CHECK-NEXT: loop: -; CHECK-NEXT: WIDEN-PHI %0 = phi 0, %conv +; CHECK-NEXT: WIDEN-PHI ir<%0> = phi ir<0>, ir<%conv> ; CHECK-NEXT: WIDEN-INDUCTION %iv = phi 0, %iv.next ; CHECK-NEXT: EMIT vp<%3> = icmp ule ir<%iv> vp<%0> ; CHECK-NEXT: Successor(s): loop.0 @@ -83,7 +83,7 @@ ; CHECK-LABEL: sink_replicate_region_2 ; CHECK: VPlan 'Initial VPlan for VF={2},UF>=1' { ; CHECK-NEXT: loop: -; CHECK-NEXT: WIDEN-PHI %recur = phi 0, %recur.next +; CHECK-NEXT: WIDEN-PHI ir<%recur> = phi ir<0>, ir<%recur.next> ; CHECK-NEXT: WIDEN-INDUCTION %iv = phi 0, %iv.next ; CHECK-NEXT: EMIT vp<%3> = icmp ule ir<%iv> vp<%0> ; CHECK-NEXT: Successor(s): loop.0 @@ -155,7 +155,7 @@ ; CHECK-LABEL: sink_replicate_region_3_reduction ; CHECK: VPlan 'Initial VPlan for VF={2},UF>=1' { ; CHECK-NEXT: loop: -; CHECK-NEXT: WIDEN-PHI %recur = phi 0, %recur.next +; CHECK-NEXT: WIDEN-PHI ir<%recur> = phi ir<0>, ir<%recur.next> ; CHECK-NEXT: WIDEN-INDUCTION %iv = phi 0, %iv.next ; CHECK-NEXT: WIDEN-PHI ir<%and.red> = phi ir<1234>, ir<%and.red.next> ; CHECK-NEXT: EMIT vp<%4> = icmp ule ir<%iv> vp<%0> @@ -214,7 +214,7 @@ ; CHECK-LABEL: sink_replicate_region_4_requires_split_at_end_of_block ; CHECK: VPlan 'Initial VPlan for VF={2},UF>=1' { ; CHECK-NEXT: loop: -; CHECK-NEXT: WIDEN-PHI %0 = phi 0, %conv +; CHECK-NEXT: WIDEN-PHI ir<%0> = phi ir<0>, ir<%conv> ; CHECK-NEXT: WIDEN-INDUCTION %iv = phi 0, %iv.next ; CHECK-NEXT: EMIT vp<%3> = icmp ule ir<%iv> vp<%0> ; CHECK-NEXT: REPLICATE ir<%gep> = getelementptr ir<%ptr>, ir<%iv>