Skip to content

Commit 0b23b3d

Browse files
committed
Addressing suggestions
* Fixing comments * Adding more tests * Remove cmp latch presence requirements
1 parent 9e6c426 commit 0b23b3d

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
@@ -2682,29 +2682,21 @@ 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(
2685+
/// Knowing that loop \p L executes a single vector iteration, add instructions
2686+
/// that will get simplified and thus should not have any cost to \p
2687+
/// InstsToIgnore.
2688+
static void addFullyUnrolledInstructionsToIgnore(
26892689
Loop *L, const LoopVectorizationLegality::InductionList &IL,
26902690
SmallPtrSetImpl<Instruction *> &InstsToIgnore) {
26912691
auto *Cmp = L->getLatchCmpInst();
2692-
if (!Cmp)
2693-
return;
2694-
InstsToIgnore.insert(Cmp);
2692+
if (Cmp)
2693+
InstsToIgnore.insert(Cmp);
26952694
for (const auto &[IV, IndDesc] : IL) {
2696-
// Get next iteration value of the induction variable
2695+
// Get next iteration value of the induction variable.
26972696
Instruction *IVInst =
26982697
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)
2698+
if (all_of(IVInst->users(),
2699+
[&](const User *U) { return U == IV || U == Cmp; }))
27082700
InstsToIgnore.insert(IVInst);
27092701
}
27102702
}
@@ -5619,12 +5611,13 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
56195611
InstructionCost LoopVectorizationCostModel::expectedCost(ElementCount VF) {
56205612
InstructionCost Cost;
56215613

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
5614+
// If the vector loop gets executed exactly once with the given VF, ignore the
5615+
// costs of comparison and induction instructions, as they'll get simplified
5616+
// away.
56245617
SmallPtrSet<Instruction *, 2> ValuesToIgnoreForVF;
56255618
auto TC = PSE.getSE()->getSmallConstantTripCount(TheLoop);
56265619
if (VF.isFixed() && TC == VF.getFixedValue())
5627-
AddFullyUnrolledInstructionsToIgnore(TheLoop, Legal->getInductionVars(),
5620+
addFullyUnrolledInstructionsToIgnore(TheLoop, Legal->getInductionVars(),
56285621
ValuesToIgnoreForVF);
56295622

56305623
// For each block.
@@ -7317,11 +7310,14 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
73177310
IVInsts.push_back(CI);
73187311
}
73197312

7320-
// If with the given VF loop gets fully unrolled, ignore the costs of
7321-
// comparison and induction instructions, as they'll get simplified away
7313+
// If the vector loop gets executed exactly once with the given VF, ignore
7314+
// the costs of comparison and induction instructions, as they'll get
7315+
// simplified away.
7316+
// TODO: Remove this code after stepping away from the legacy cost model and
7317+
// adding code to simplify VPlans before calculating their costs.
73227318
auto TC = PSE.getSE()->getSmallConstantTripCount(OrigLoop);
73237319
if (VF.isFixed() && TC == VF.getFixedValue())
7324-
AddFullyUnrolledInstructionsToIgnore(OrigLoop, Legal->getInductionVars(),
7320+
addFullyUnrolledInstructionsToIgnore(OrigLoop, Legal->getInductionVars(),
73257321
CostCtx.SkipCostComputation);
73267322

73277323
for (Instruction *IVInst : IVInsts) {

0 commit comments

Comments
 (0)