Skip to content

Commit e0d2e68

Browse files
committed
[LV] Ignore some costs when loop gets fully unrolled
When VF equals the number of iterations, comparison instruction and induction operation will be DCEed later. Ignoring the costs of these instructions improves the cost model.
1 parent 3eb1bc5 commit e0d2e68

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7223,6 +7223,26 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
72237223
continue;
72247224
IVInsts.push_back(CI);
72257225
}
7226+
7227+
// If the given VF loop gets fully unrolled, ignore the costs of comparison
7228+
// and increment instruction, as they'll get simplified away
7229+
auto TC = CM.PSE.getSE()->getSmallConstantTripCount(OrigLoop);
7230+
auto *Cmp = OrigLoop->getLatchCmpInst();
7231+
if (Cmp && VF.isFixed() && VF.getFixedValue() == TC) {
7232+
CostCtx.SkipCostComputation.insert(Cmp);
7233+
for (Instruction *IVInst : IVInsts) {
7234+
bool IsSimplifiedAway = true;
7235+
for (auto *UIV : IVInst->users()) {
7236+
if (!Legal->isInductionVariable(UIV) && UIV != Cmp) {
7237+
IsSimplifiedAway = false;
7238+
break;
7239+
}
7240+
}
7241+
if (IsSimplifiedAway)
7242+
CostCtx.SkipCostComputation.insert(IVInst);
7243+
}
7244+
}
7245+
72267246
for (Instruction *IVInst : IVInsts) {
72277247
if (CostCtx.skipCostComputation(IVInst, VF.isVector()))
72287248
continue;

0 commit comments

Comments
 (0)