@@ -2648,6 +2648,33 @@ static Value *getExpandedStep(const InductionDescriptor &ID,
2648
2648
return I->second ;
2649
2649
}
2650
2650
2651
+ // / Knowing that loop \p L would be fully unrolled after vectorisation, add
2652
+ // / instructions that will get simplified and thus should not have any cost to
2653
+ // / \p InstsToIgnore
2654
+ static void AddFullyUnrolledInstructionsToIgnore (
2655
+ Loop *L, const LoopVectorizationLegality::InductionList &IL,
2656
+ SmallPtrSetImpl<Instruction *> &InstsToIgnore) {
2657
+ auto *Cmp = L->getLatchCmpInst ();
2658
+ if (!Cmp)
2659
+ return ;
2660
+ InstsToIgnore.insert (Cmp);
2661
+ for (const auto &[IV, IndDesc] : IL) {
2662
+ // Get next iteration value of the induction variable
2663
+ Instruction *IVInst =
2664
+ cast<Instruction>(IV->getIncomingValueForBlock (L->getLoopLatch ()));
2665
+ bool IsSimplifiedAway = true ;
2666
+ // Check that this value used only to exit the loop
2667
+ for (auto *UIV : IVInst->users ()) {
2668
+ if (UIV != IV && UIV != Cmp) {
2669
+ IsSimplifiedAway = false ;
2670
+ break ;
2671
+ }
2672
+ }
2673
+ if (IsSimplifiedAway)
2674
+ InstsToIgnore.insert (IVInst);
2675
+ }
2676
+ }
2677
+
2651
2678
void InnerLoopVectorizer::createInductionResumeValues (
2652
2679
const SCEV2ValueTy &ExpandedSCEVs,
2653
2680
std::pair<BasicBlock *, Value *> AdditionalBypass) {
@@ -5534,19 +5561,13 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
5534
5561
InstructionCost LoopVectorizationCostModel::expectedCost (ElementCount VF) {
5535
5562
InstructionCost Cost;
5536
5563
5537
- // If with the given VF loop gets fully unrolled, ignore the costs of
5538
- // comparison and induction instructions, as they'll get simplified away
5539
- SmallPtrSet<const Value *, 16 > ValuesToIgnoreForVF;
5564
+ // If with the given fixed width VF loop gets fully unrolled, ignore the costs
5565
+ // of comparison and induction instructions, as they'll get simplified away
5566
+ SmallPtrSet<Instruction *, 2 > ValuesToIgnoreForVF;
5540
5567
auto TC = PSE.getSE ()->getSmallConstantTripCount (TheLoop);
5541
- auto *Cmp = TheLoop->getLatchCmpInst ();
5542
- if (Cmp && TC == VF.getKnownMinValue ()) {
5543
- ValuesToIgnoreForVF.insert (Cmp);
5544
- for (const auto &[IV, IndDesc] : Legal->getInductionVars ()) {
5545
- Instruction *IVInc = cast<Instruction>(
5546
- IV->getIncomingValueForBlock (TheLoop->getLoopLatch ()));
5547
- ValuesToIgnoreForVF.insert (IVInc);
5548
- }
5549
- }
5568
+ if (VF.isFixed () && TC == VF.getFixedValue ())
5569
+ AddFullyUnrolledInstructionsToIgnore (TheLoop, Legal->getInductionVars (),
5570
+ ValuesToIgnoreForVF);
5550
5571
5551
5572
// For each block.
5552
5573
for (BasicBlock *BB : TheLoop->blocks ()) {
@@ -7240,16 +7261,10 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
7240
7261
7241
7262
// If with the given VF loop gets fully unrolled, ignore the costs of
7242
7263
// comparison and induction instructions, as they'll get simplified away
7243
- auto TC = CM.PSE .getSE ()->getSmallConstantTripCount (OrigLoop);
7244
- auto *Cmp = OrigLoop->getLatchCmpInst ();
7245
- if (Cmp && TC == VF.getKnownMinValue ()) {
7246
- CostCtx.SkipCostComputation .insert (Cmp);
7247
- for (const auto &[IV, IndDesc] : Legal->getInductionVars ()) {
7248
- Instruction *IVInc = cast<Instruction>(
7249
- IV->getIncomingValueForBlock (OrigLoop->getLoopLatch ()));
7250
- CostCtx.SkipCostComputation .insert (IVInc);
7251
- }
7252
- }
7264
+ auto TC = PSE.getSE ()->getSmallConstantTripCount (OrigLoop);
7265
+ if (VF.isFixed () && TC == VF.getFixedValue ())
7266
+ AddFullyUnrolledInstructionsToIgnore (OrigLoop, Legal->getInductionVars (),
7267
+ CostCtx.SkipCostComputation );
7253
7268
7254
7269
for (Instruction *IVInst : IVInsts) {
7255
7270
if (CostCtx.skipCostComputation (IVInst, VF.isVector ()))
0 commit comments