Skip to content

Commit 07e7159

Browse files
committed
[VPlan] Check users of LoopExitInstDef in VPlan directly. (NFCI)
Instead of walking the IR def use chains of the generated code, adjust the generated VPInstruction if needed and check its users in VPlan.
1 parent 9a7a6dd commit 07e7159

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,34 +3818,16 @@ void InnerLoopVectorizer::fixReduction(VPReductionPHIRecipe *PhiR,
38183818
// instead of the former. For an inloop reduction the reduction will already
38193819
// be predicated, and does not need to be handled here.
38203820
if (Cost->foldTailByMasking() && !PhiR->isInLoop()) {
3821-
for (unsigned Part = 0; Part < UF; ++Part) {
3822-
Value *VecLoopExitInst = State.get(LoopExitInstDef, Part);
3823-
SelectInst *Sel = nullptr;
3824-
for (User *U : VecLoopExitInst->users()) {
3825-
if (isa<SelectInst>(U)) {
3826-
assert((!Sel || U == Sel) &&
3827-
"Reduction exit feeding two different selects");
3828-
Sel = cast<SelectInst>(U);
3829-
} else
3830-
assert(isa<PHINode>(U) && "Reduction exit must feed Phi's or select");
3831-
}
3832-
assert(Sel && "Reduction exit feeds no select");
3833-
State.reset(LoopExitInstDef, Sel, Part);
3834-
3835-
// If the target can create a predicated operator for the reduction at no
3836-
// extra cost in the loop (for example a predicated vadd), it can be
3837-
// cheaper for the select to remain in the loop than be sunk out of it,
3838-
// and so use the select value for the phi instead of the old
3839-
// LoopExitValue.
3840-
if (PreferPredicatedReductionSelect ||
3841-
TTI->preferPredicatedReductionSelect(
3842-
RdxDesc.getOpcode(), PhiTy,
3843-
TargetTransformInfo::ReductionFlags())) {
3844-
auto *VecRdxPhi =
3845-
cast<PHINode>(State.get(PhiR, Part));
3846-
VecRdxPhi->setIncomingValueForBlock(VectorLoopLatch, Sel);
3821+
VPValue *Def = nullptr;
3822+
for (VPUser *U : LoopExitInstDef->users()) {
3823+
auto *S = dyn_cast<VPInstruction>(U);
3824+
if (S && S->getOpcode() == Instruction::Select) {
3825+
Def = S;
3826+
break;
38473827
}
38483828
}
3829+
if (Def)
3830+
LoopExitInstDef = Def;
38493831
}
38503832

38513833
// If the vector reduction can be performed in a smaller type, we truncate
@@ -9099,6 +9081,11 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
90999081
? new VPInstruction(Instruction::Select, {Cond, Red, PhiR}, FMFs)
91009082
: new VPInstruction(Instruction::Select, {Cond, Red, PhiR});
91019083
Select->insertBefore(&*Builder.getInsertPoint());
9084+
if (PreferPredicatedReductionSelect ||
9085+
TTI.preferPredicatedReductionSelect(
9086+
PhiR->getRecurrenceDescriptor().getOpcode(), PhiTy,
9087+
TargetTransformInfo::ReductionFlags()))
9088+
PhiR->setOperand(1, Select);
91029089
}
91039090
}
91049091

0 commit comments

Comments
 (0)