@@ -2652,6 +2652,33 @@ static Value *getExpandedStep(const InductionDescriptor &ID,
2652
2652
return I->second ;
2653
2653
}
2654
2654
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
+
2655
2682
void InnerLoopVectorizer::createInductionResumeValues (
2656
2683
const SCEV2ValueTy &ExpandedSCEVs,
2657
2684
std::pair<BasicBlock *, Value *> AdditionalBypass) {
@@ -5559,19 +5586,13 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
5559
5586
InstructionCost LoopVectorizationCostModel::expectedCost (ElementCount VF) {
5560
5587
InstructionCost Cost;
5561
5588
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;
5565
5592
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);
5575
5596
5576
5597
// For each block.
5577
5598
for (BasicBlock *BB : TheLoop->blocks ()) {
@@ -7265,16 +7286,10 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
7265
7286
7266
7287
// If with the given VF loop gets fully unrolled, ignore the costs of
7267
7288
// 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 );
7278
7293
7279
7294
for (Instruction *IVInst : IVInsts) {
7280
7295
if (CostCtx.skipCostComputation (IVInst, VF.isVector ()))
0 commit comments