@@ -5559,14 +5559,28 @@ InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
5559
5559
InstructionCost LoopVectorizationCostModel::expectedCost (ElementCount VF) {
5560
5560
InstructionCost Cost;
5561
5561
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;
5565
+ 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
+ }
5575
+
5562
5576
// For each block.
5563
5577
for (BasicBlock *BB : TheLoop->blocks ()) {
5564
5578
InstructionCost BlockCost;
5565
5579
5566
5580
// For each instruction in the old loop.
5567
5581
for (Instruction &I : BB->instructionsWithoutDebug ()) {
5568
5582
// Skip ignored values.
5569
- if (ValuesToIgnore.count (&I) ||
5583
+ if (ValuesToIgnore.count (&I) || ValuesToIgnoreForVF. count (&I) ||
5570
5584
(VF.isVector () && VecValuesToIgnore.count (&I)))
5571
5585
continue ;
5572
5586
@@ -7249,22 +7263,16 @@ LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
7249
7263
IVInsts.push_back (CI);
7250
7264
}
7251
7265
7252
- // If the given VF loop gets fully unrolled, ignore the costs of comparison
7253
- // and increment instruction , as they'll get simplified away
7266
+ // If with the given VF loop gets fully unrolled, ignore the costs of
7267
+ // comparison and induction instructions , as they'll get simplified away
7254
7268
auto TC = CM.PSE .getSE ()->getSmallConstantTripCount (OrigLoop);
7255
7269
auto *Cmp = OrigLoop->getLatchCmpInst ();
7256
- if (Cmp && VF. isFixed () && VF.getFixedValue () == TC ) {
7270
+ if (Cmp && TC == VF.getKnownMinValue () ) {
7257
7271
CostCtx.SkipCostComputation .insert (Cmp);
7258
- for (Instruction *IVInst : IVInsts) {
7259
- bool IsSimplifiedAway = true ;
7260
- for (auto *UIV : IVInst->users ()) {
7261
- if (!Legal->isInductionVariable (UIV) && UIV != Cmp) {
7262
- IsSimplifiedAway = false ;
7263
- break ;
7264
- }
7265
- }
7266
- if (IsSimplifiedAway)
7267
- CostCtx.SkipCostComputation .insert (IVInst);
7272
+ for (const auto &[IV, IndDesc] : Legal->getInductionVars ()) {
7273
+ Instruction *IVInc = cast<Instruction>(
7274
+ IV->getIncomingValueForBlock (OrigLoop->getLoopLatch ()));
7275
+ CostCtx.SkipCostComputation .insert (IVInc);
7268
7276
}
7269
7277
}
7270
7278
0 commit comments