@@ -5592,14 +5592,28 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
5592
5592
InstructionCost LoopVectorizationCostModel::expectedCost (ElementCount VF) {
5593
5593
InstructionCost Cost;
5594
5594
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;
5598
+ 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
+ }
5608
+
5595
5609
// For each block.
5596
5610
for (BasicBlock *BB : TheLoop->blocks ()) {
5597
5611
InstructionCost BlockCost;
5598
5612
5599
5613
// For each instruction in the old loop.
5600
5614
for (Instruction &I : BB->instructionsWithoutDebug ()) {
5601
5615
// Skip ignored values.
5602
- if (ValuesToIgnore.count (&I) ||
5616
+ if (ValuesToIgnore.count (&I) || ValuesToIgnoreForVF. count (&I) ||
5603
5617
(VF.isVector () && VecValuesToIgnore.count (&I)))
5604
5618
continue ;
5605
5619
@@ -7282,22 +7296,16 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
7282
7296
IVInsts.push_back (CI);
7283
7297
}
7284
7298
7285
- // If the given VF loop gets fully unrolled, ignore the costs of comparison
7286
- // and increment instruction , as they'll get simplified away
7299
+ // If with the given VF loop gets fully unrolled, ignore the costs of
7300
+ // comparison and induction instructions , as they'll get simplified away
7287
7301
auto TC = CM.PSE .getSE ()->getSmallConstantTripCount (OrigLoop);
7288
7302
auto *Cmp = OrigLoop->getLatchCmpInst ();
7289
- if (Cmp && VF. isFixed () && VF.getFixedValue () == TC ) {
7303
+ if (Cmp && TC == VF.getKnownMinValue () ) {
7290
7304
CostCtx.SkipCostComputation .insert (Cmp);
7291
- for (Instruction *IVInst : IVInsts) {
7292
- bool IsSimplifiedAway = true ;
7293
- for (auto *UIV : IVInst->users ()) {
7294
- if (!Legal->isInductionVariable (UIV) && UIV != Cmp) {
7295
- IsSimplifiedAway = false ;
7296
- break ;
7297
- }
7298
- }
7299
- if (IsSimplifiedAway)
7300
- CostCtx.SkipCostComputation .insert (IVInst);
7305
+ for (const auto &[IV, IndDesc] : Legal->getInductionVars ()) {
7306
+ Instruction *IVInc = cast<Instruction>(
7307
+ IV->getIncomingValueForBlock (OrigLoop->getLoopLatch ()));
7308
+ CostCtx.SkipCostComputation .insert (IVInc);
7301
7309
}
7302
7310
}
7303
7311
0 commit comments