Skip to content

Commit d2272da

Browse files
committed
Check that induction variable has no unsimplifiable users
Add AArch64 test
1 parent 2549d0a commit d2272da

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,33 @@ static Value *getExpandedStep(const InductionDescriptor &ID,
26522652
return I->second;
26532653
}
26542654

2655+
/// Knowing that loop \p L would be fully unrolled after vectorisation, add
2656+
/// instructions that will get simplified and thus should not have any cost to
2657+
/// \p InstsToIgnore
2658+
static void AddFullyUnrolledInstructionsToIgnore(
2659+
Loop *L, const LoopVectorizationLegality::InductionList &IL,
2660+
SmallPtrSetImpl<Instruction *> &InstsToIgnore) {
2661+
auto *Cmp = L->getLatchCmpInst();
2662+
if (!Cmp)
2663+
return;
2664+
InstsToIgnore.insert(Cmp);
2665+
for (const auto &[IV, IndDesc] : IL) {
2666+
// Get next iteration value of the induction variable
2667+
Instruction *IVInst =
2668+
cast<Instruction>(IV->getIncomingValueForBlock(L->getLoopLatch()));
2669+
bool IsSimplifiedAway = true;
2670+
// Check that this value used only to exit the loop
2671+
for (auto *UIV : IVInst->users()) {
2672+
if (UIV != IV && UIV != Cmp) {
2673+
IsSimplifiedAway = false;
2674+
break;
2675+
}
2676+
}
2677+
if (IsSimplifiedAway)
2678+
InstsToIgnore.insert(IVInst);
2679+
}
2680+
}
2681+
26552682
void InnerLoopVectorizer::createInductionResumeValues(
26562683
const SCEV2ValueTy &ExpandedSCEVs,
26572684
std::pair<BasicBlock *, Value *> AdditionalBypass) {
@@ -5559,19 +5586,13 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
55595586
InstructionCost LoopVectorizationCostModel::expectedCost(ElementCount VF) {
55605587
InstructionCost Cost;
55615588

5562-
// If with the given VF loop gets fully unrolled, ignore the costs of
5563-
// comparison and induction instructions, as they'll get simplified away
5564-
SmallPtrSet<const Value *, 16> ValuesToIgnoreForVF;
5589+
// If with the given fixed width VF loop gets fully unrolled, ignore the costs
5590+
// of comparison and induction instructions, as they'll get simplified away
5591+
SmallPtrSet<Instruction *, 2> ValuesToIgnoreForVF;
55655592
auto TC = PSE.getSE()->getSmallConstantTripCount(TheLoop);
5566-
auto *Cmp = TheLoop->getLatchCmpInst();
5567-
if (Cmp && TC == VF.getKnownMinValue()) {
5568-
ValuesToIgnoreForVF.insert(Cmp);
5569-
for (const auto &[IV, IndDesc] : Legal->getInductionVars()) {
5570-
Instruction *IVInc = cast<Instruction>(
5571-
IV->getIncomingValueForBlock(TheLoop->getLoopLatch()));
5572-
ValuesToIgnoreForVF.insert(IVInc);
5573-
}
5574-
}
5593+
if (VF.isFixed() && TC == VF.getFixedValue())
5594+
AddFullyUnrolledInstructionsToIgnore(TheLoop, Legal->getInductionVars(),
5595+
ValuesToIgnoreForVF);
55755596

55765597
// For each block.
55775598
for (BasicBlock *BB : TheLoop->blocks()) {
@@ -7265,16 +7286,10 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
72657286

72667287
// If with the given VF loop gets fully unrolled, ignore the costs of
72677288
// comparison and induction instructions, as they'll get simplified away
7268-
auto TC = CM.PSE.getSE()->getSmallConstantTripCount(OrigLoop);
7269-
auto *Cmp = OrigLoop->getLatchCmpInst();
7270-
if (Cmp && TC == VF.getKnownMinValue()) {
7271-
CostCtx.SkipCostComputation.insert(Cmp);
7272-
for (const auto &[IV, IndDesc] : Legal->getInductionVars()) {
7273-
Instruction *IVInc = cast<Instruction>(
7274-
IV->getIncomingValueForBlock(OrigLoop->getLoopLatch()));
7275-
CostCtx.SkipCostComputation.insert(IVInc);
7276-
}
7277-
}
7289+
auto TC = PSE.getSE()->getSmallConstantTripCount(OrigLoop);
7290+
if (VF.isFixed() && TC == VF.getFixedValue())
7291+
AddFullyUnrolledInstructionsToIgnore(OrigLoop, Legal->getInductionVars(),
7292+
CostCtx.SkipCostComputation);
72787293

72797294
for (Instruction *IVInst : IVInsts) {
72807295
if (CostCtx.skipCostComputation(IVInst, VF.isVector()))

0 commit comments

Comments
 (0)