@@ -4295,7 +4295,7 @@ void InnerLoopVectorizer::fixReduction(VPReductionPHIRecipe *PhiR,
4295
4295
Instruction *LoopExitInst = RdxDesc.getLoopExitInstr ();
4296
4296
setDebugLocFromInst (ReductionStartValue);
4297
4297
4298
- VPValue *LoopExitInstDef = State. Plan -> getVPValue (LoopExitInst );
4298
+ VPValue *LoopExitInstDef = PhiR-> getBackedgeValue ( );
4299
4299
// This is the vector-clone of the value that leaves the loop.
4300
4300
Type *VecTy = State.get (LoopExitInstDef, 0 )->getType ();
4301
4301
@@ -9438,21 +9438,7 @@ VPlanPtr LoopVectorizationPlanner::buildVPlanWithVPRecipes(
9438
9438
}
9439
9439
9440
9440
// Adjust the recipes for any inloop reductions.
9441
- adjustRecipesForInLoopReductions (Plan, RecipeBuilder, Range.Start );
9442
-
9443
- // Finally, if tail is folded by masking, introduce selects between the phi
9444
- // and the live-out instruction of each reduction, at the end of the latch.
9445
- if (CM.foldTailByMasking () && !Legal->getReductionVars ().empty ()) {
9446
- Builder.setInsertPoint (VPBB);
9447
- auto *Cond = RecipeBuilder.createBlockInMask (OrigLoop->getHeader (), Plan);
9448
- for (auto &Reduction : Legal->getReductionVars ()) {
9449
- if (CM.isInLoopReduction (Reduction.first ))
9450
- continue ;
9451
- VPValue *Phi = Plan->getOrAddVPValue (Reduction.first );
9452
- VPValue *Red = Plan->getOrAddVPValue (Reduction.second .getLoopExitInstr ());
9453
- Builder.createNaryOp (Instruction::Select, {Cond, Red, Phi});
9454
- }
9455
- }
9441
+ adjustRecipesForReductions (VPBB, Plan, RecipeBuilder, Range.Start );
9456
9442
9457
9443
VPlanTransforms::sinkScalarOperands (*Plan);
9458
9444
VPlanTransforms::mergeReplicateRegions (*Plan);
@@ -9508,12 +9494,14 @@ VPlanPtr LoopVectorizationPlanner::buildVPlan(VFRange &Range) {
9508
9494
return Plan;
9509
9495
}
9510
9496
9511
- // Adjust the recipes for any inloop reductions. The chain of instructions
9512
- // leading from the loop exit instr to the phi need to be converted to
9513
- // reductions, with one operand being vector and the other being the scalar
9514
- // reduction chain.
9515
- void LoopVectorizationPlanner::adjustRecipesForInLoopReductions (
9516
- VPlanPtr &Plan, VPRecipeBuilder &RecipeBuilder, ElementCount MinVF) {
9497
+ // Adjust the recipes for reductions. For in-loop reductions the chain of
9498
+ // instructions leading from the loop exit instr to the phi need to be converted
9499
+ // to reductions, with one operand being vector and the other being the scalar
9500
+ // reduction chain. For other reductions, a select is introduced between the phi
9501
+ // and live-out recipes when folding the tail.
9502
+ void LoopVectorizationPlanner::adjustRecipesForReductions (
9503
+ VPBasicBlock *LatchVPBB, VPlanPtr &Plan, VPRecipeBuilder &RecipeBuilder,
9504
+ ElementCount MinVF) {
9517
9505
for (auto &Reduction : CM.getInLoopReductionChains ()) {
9518
9506
PHINode *Phi = Reduction.first ;
9519
9507
RecurrenceDescriptor &RdxDesc = Legal->getReductionVars ()[Phi];
@@ -9570,6 +9558,21 @@ void LoopVectorizationPlanner::adjustRecipesForInLoopReductions(
9570
9558
Chain = R;
9571
9559
}
9572
9560
}
9561
+
9562
+ // If tail is folded by masking, introduce selects between the phi
9563
+ // and the live-out instruction of each reduction, at the end of the latch.
9564
+ if (CM.foldTailByMasking ()) {
9565
+ for (VPRecipeBase &R : Plan->getEntry ()->getEntryBasicBlock ()->phis ()) {
9566
+ VPReductionPHIRecipe *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
9567
+ if (!PhiR || PhiR->isInLoop ())
9568
+ continue ;
9569
+ Builder.setInsertPoint (LatchVPBB);
9570
+ VPValue *Cond =
9571
+ RecipeBuilder.createBlockInMask (OrigLoop->getHeader (), Plan);
9572
+ VPValue *Red = PhiR->getBackedgeValue ();
9573
+ Builder.createNaryOp (Instruction::Select, {Cond, Red, PhiR});
9574
+ }
9575
+ }
9573
9576
}
9574
9577
9575
9578
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
0 commit comments