Skip to content

Commit 1722aea

Browse files
committed
Addressing suggestions
* Fixing comments * Adding more tests * Remove cmp latch presence requirements
1 parent d2272da commit 1722aea

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,29 +2652,21 @@ 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(
2655+
/// Knowing that loop \p L executes a single vector iteration, add instructions
2656+
/// that will get simplified and thus should not have any cost to \p
2657+
/// InstsToIgnore.
2658+
static void addFullyUnrolledInstructionsToIgnore(
26592659
Loop *L, const LoopVectorizationLegality::InductionList &IL,
26602660
SmallPtrSetImpl<Instruction *> &InstsToIgnore) {
26612661
auto *Cmp = L->getLatchCmpInst();
2662-
if (!Cmp)
2663-
return;
2664-
InstsToIgnore.insert(Cmp);
2662+
if (Cmp)
2663+
InstsToIgnore.insert(Cmp);
26652664
for (const auto &[IV, IndDesc] : IL) {
2666-
// Get next iteration value of the induction variable
2665+
// Get next iteration value of the induction variable.
26672666
Instruction *IVInst =
26682667
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)
2668+
if (all_of(IVInst->users(),
2669+
[&](const User *U) { return U == IV || U == Cmp; }))
26782670
InstsToIgnore.insert(IVInst);
26792671
}
26802672
}
@@ -5586,12 +5578,13 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
55865578
InstructionCost LoopVectorizationCostModel::expectedCost(ElementCount VF) {
55875579
InstructionCost Cost;
55885580

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
5581+
// If the vector loop gets executed exactly once with the given VF, ignore the
5582+
// costs of comparison and induction instructions, as they'll get simplified
5583+
// away.
55915584
SmallPtrSet<Instruction *, 2> ValuesToIgnoreForVF;
55925585
auto TC = PSE.getSE()->getSmallConstantTripCount(TheLoop);
55935586
if (VF.isFixed() && TC == VF.getFixedValue())
5594-
AddFullyUnrolledInstructionsToIgnore(TheLoop, Legal->getInductionVars(),
5587+
addFullyUnrolledInstructionsToIgnore(TheLoop, Legal->getInductionVars(),
55955588
ValuesToIgnoreForVF);
55965589

55975590
// For each block.
@@ -7284,11 +7277,14 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
72847277
IVInsts.push_back(CI);
72857278
}
72867279

7287-
// If with the given VF loop gets fully unrolled, ignore the costs of
7288-
// comparison and induction instructions, as they'll get simplified away
7280+
// If the vector loop gets executed exactly once with the given VF, ignore
7281+
// the costs of comparison and induction instructions, as they'll get
7282+
// simplified away.
7283+
// TODO: Remove this code after stepping away from the legacy cost model and
7284+
// adding code to simplify VPlans before calculating their costs.
72897285
auto TC = PSE.getSE()->getSmallConstantTripCount(OrigLoop);
72907286
if (VF.isFixed() && TC == VF.getFixedValue())
7291-
AddFullyUnrolledInstructionsToIgnore(OrigLoop, Legal->getInductionVars(),
7287+
addFullyUnrolledInstructionsToIgnore(OrigLoop, Legal->getInductionVars(),
72927288
CostCtx.SkipCostComputation);
72937289

72947290
for (Instruction *IVInst : IVInsts) {

0 commit comments

Comments
 (0)