@@ -9861,15 +9861,16 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
9861
9861
// bc.merge.rdx phi nodes, hence it needs to be created unconditionally here
9862
9862
// even for in-loop reductions, until the reduction resume value handling is
9863
9863
// also modeled in VPlan.
9864
- auto *FinalReductionResult = new VPInstruction (
9864
+ VPBuilder::InsertPointGuard Guard (Builder);
9865
+ Builder.setInsertPoint (MiddleVPBB, IP);
9866
+ auto *FinalReductionResult = Builder.createNaryOp (
9865
9867
VPInstruction::ComputeReductionResult, {PhiR, NewExitingVPV}, ExitDL);
9866
9868
// Update all users outside the vector region.
9867
9869
OrigExitingVPV->replaceUsesWithIf (
9868
- FinalReductionResult, [](VPUser &User, unsigned ) {
9870
+ FinalReductionResult, [FinalReductionResult ](VPUser &User, unsigned ) {
9869
9871
auto *Parent = cast<VPRecipeBase>(&User)->getParent ();
9870
- return Parent && !Parent->getParent ();
9872
+ return FinalReductionResult != &User && !Parent->getParent ();
9871
9873
});
9872
- FinalReductionResult->insertBefore (*MiddleVPBB, IP);
9873
9874
9874
9875
// Adjust AnyOf reductions; replace the reduction phi for the selected value
9875
9876
// with a boolean reduction phi node to check if the condition is true in
@@ -9891,7 +9892,6 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
9891
9892
if (CmpR->getOperand (I) == PhiR)
9892
9893
CmpR->setOperand (I, PhiR->getStartValue ());
9893
9894
}
9894
- VPBuilder::InsertPointGuard Guard (Builder);
9895
9895
Builder.setInsertPoint (Select);
9896
9896
9897
9897
// If the true value of the select is the reduction phi, the new value is
0 commit comments