@@ -9683,15 +9683,16 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
96839683 // bc.merge.rdx phi nodes, hence it needs to be created unconditionally here
96849684 // even for in-loop reductions, until the reduction resume value handling is
96859685 // also modeled in VPlan.
9686- auto *FinalReductionResult = new VPInstruction (
9686+ VPBuilder::InsertPointGuard Guard (Builder);
9687+ Builder.setInsertPoint (MiddleVPBB, IP);
9688+ auto *FinalReductionResult = Builder.createNaryOp (
96879689 VPInstruction::ComputeReductionResult, {PhiR, NewExitingVPV}, ExitDL);
96889690 // Update all users outside the vector region.
96899691 OrigExitingVPV->replaceUsesWithIf (
9690- FinalReductionResult, [](VPUser &User, unsigned ) {
9692+ FinalReductionResult, [FinalReductionResult ](VPUser &User, unsigned ) {
96919693 auto *Parent = cast<VPRecipeBase>(&User)->getParent ();
9692- return Parent && !Parent->getParent ();
9694+ return FinalReductionResult != &User && !Parent->getParent ();
96939695 });
9694- FinalReductionResult->insertBefore (*MiddleVPBB, IP);
96959696
96969697 // Adjust AnyOf reductions; replace the reduction phi for the selected value
96979698 // with a boolean reduction phi node to check if the condition is true in
@@ -9713,7 +9714,6 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
97139714 if (CmpR->getOperand (I) == PhiR)
97149715 CmpR->setOperand (I, PhiR->getStartValue ());
97159716 }
9716- VPBuilder::InsertPointGuard Guard (Builder);
97179717 Builder.setInsertPoint (Select);
97189718
97199719 // If the true value of the select is the reduction phi, the new value is
0 commit comments