Skip to content

Commit a21d8c2

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 89d8449 commit a21d8c2

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
@@ -7221,6 +7221,26 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
72217221
continue;
72227222
IVInsts.push_back(CI);
72237223
}
7224+
7225+
// If the given VF loop gets fully unrolled, ignore the costs of comparison
7226+
// and increment instruction, as they'll get simplified away
7227+
auto TC = CM.PSE.getSE()->getSmallConstantTripCount(OrigLoop);
7228+
auto *Cmp = OrigLoop->getLatchCmpInst();
7229+
if (Cmp && VF.isFixed() && VF.getFixedValue() == TC) {
7230+
CostCtx.SkipCostComputation.insert(Cmp);
7231+
for (Instruction *IVInst : IVInsts) {
7232+
bool IsSimplifiedAway = true;
7233+
for (auto *UIV : IVInst->users()) {
7234+
if (!Legal->isInductionVariable(UIV) && UIV != Cmp) {
7235+
IsSimplifiedAway = false;
7236+
break;
7237+
}
7238+
}
7239+
if (IsSimplifiedAway)
7240+
CostCtx.SkipCostComputation.insert(IVInst);
7241+
}
7242+
}
7243+
72247244
for (Instruction *IVInst : IVInsts) {
72257245
if (CostCtx.skipCostComputation(IVInst, VF.isVector()))
72267246
continue;

0 commit comments

Comments
 (0)