Skip to content

Commit 9e6c426

Browse files
committed
Check that induction variable has no unsimplifiable users
Add AArch64 test
1 parent 660c195 commit 9e6c426

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
@@ -2682,6 +2682,33 @@ static Value *getExpandedStep(const InductionDescriptor &ID,
26822682
return I->second;
26832683
}
26842684

2685+
/// Knowing that loop \p L would be fully unrolled after vectorisation, add
2686+
/// instructions that will get simplified and thus should not have any cost to
2687+
/// \p InstsToIgnore
2688+
static void AddFullyUnrolledInstructionsToIgnore(
2689+
Loop *L, const LoopVectorizationLegality::InductionList &IL,
2690+
SmallPtrSetImpl<Instruction *> &InstsToIgnore) {
2691+
auto *Cmp = L->getLatchCmpInst();
2692+
if (!Cmp)
2693+
return;
2694+
InstsToIgnore.insert(Cmp);
2695+
for (const auto &[IV, IndDesc] : IL) {
2696+
// Get next iteration value of the induction variable
2697+
Instruction *IVInst =
2698+
cast<Instruction>(IV->getIncomingValueForBlock(L->getLoopLatch()));
2699+
bool IsSimplifiedAway = true;
2700+
// Check that this value used only to exit the loop
2701+
for (auto *UIV : IVInst->users()) {
2702+
if (UIV != IV && UIV != Cmp) {
2703+
IsSimplifiedAway = false;
2704+
break;
2705+
}
2706+
}
2707+
if (IsSimplifiedAway)
2708+
InstsToIgnore.insert(IVInst);
2709+
}
2710+
}
2711+
26852712
void InnerLoopVectorizer::createInductionResumeVPValues(
26862713
const SCEV2ValueTy &ExpandedSCEVs, Value *MainVectorTripCount,
26872714
SmallPtrSetImpl<PHINode *> *IVSubset) {
@@ -5592,19 +5619,13 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
55925619
InstructionCost LoopVectorizationCostModel::expectedCost(ElementCount VF) {
55935620
InstructionCost Cost;
55945621

5595-
// If with the given VF loop gets fully unrolled, ignore the costs of
5596-
// comparison and induction instructions, as they'll get simplified away
5597-
SmallPtrSet<const Value *, 16> ValuesToIgnoreForVF;
5622+
// If with the given fixed width VF loop gets fully unrolled, ignore the costs
5623+
// of comparison and induction instructions, as they'll get simplified away
5624+
SmallPtrSet<Instruction *, 2> ValuesToIgnoreForVF;
55985625
auto TC = PSE.getSE()->getSmallConstantTripCount(TheLoop);
5599-
auto *Cmp = TheLoop->getLatchCmpInst();
5600-
if (Cmp && TC == VF.getKnownMinValue()) {
5601-
ValuesToIgnoreForVF.insert(Cmp);
5602-
for (const auto &[IV, IndDesc] : Legal->getInductionVars()) {
5603-
Instruction *IVInc = cast<Instruction>(
5604-
IV->getIncomingValueForBlock(TheLoop->getLoopLatch()));
5605-
ValuesToIgnoreForVF.insert(IVInc);
5606-
}
5607-
}
5626+
if (VF.isFixed() && TC == VF.getFixedValue())
5627+
AddFullyUnrolledInstructionsToIgnore(TheLoop, Legal->getInductionVars(),
5628+
ValuesToIgnoreForVF);
56085629

56095630
// For each block.
56105631
for (BasicBlock *BB : TheLoop->blocks()) {
@@ -7298,16 +7319,10 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
72987319

72997320
// If with the given VF loop gets fully unrolled, ignore the costs of
73007321
// comparison and induction instructions, as they'll get simplified away
7301-
auto TC = CM.PSE.getSE()->getSmallConstantTripCount(OrigLoop);
7302-
auto *Cmp = OrigLoop->getLatchCmpInst();
7303-
if (Cmp && TC == VF.getKnownMinValue()) {
7304-
CostCtx.SkipCostComputation.insert(Cmp);
7305-
for (const auto &[IV, IndDesc] : Legal->getInductionVars()) {
7306-
Instruction *IVInc = cast<Instruction>(
7307-
IV->getIncomingValueForBlock(OrigLoop->getLoopLatch()));
7308-
CostCtx.SkipCostComputation.insert(IVInc);
7309-
}
7310-
}
7322+
auto TC = PSE.getSE()->getSmallConstantTripCount(OrigLoop);
7323+
if (VF.isFixed() && TC == VF.getFixedValue())
7324+
AddFullyUnrolledInstructionsToIgnore(OrigLoop, Legal->getInductionVars(),
7325+
CostCtx.SkipCostComputation);
73117326

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

0 commit comments

Comments
 (0)