@@ -7357,16 +7357,30 @@ InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan,
7357
7357
// / be a single condition to control the vector loop.
7358
7358
SmallVector<BasicBlock *> Exiting;
7359
7359
CM.TheLoop ->getExitingBlocks (Exiting);
7360
- // Add the cost of all exit conditions.
7360
+ SetVector<Instruction *> ExitInstrs;
7361
+ // Collect all exit conditions.
7361
7362
for (BasicBlock *EB : Exiting) {
7362
7363
auto *Term = dyn_cast<BranchInst>(EB->getTerminator ());
7363
7364
if (!Term)
7364
7365
continue ;
7365
7366
if (auto *CondI = dyn_cast<Instruction>(Term->getOperand (0 ))) {
7366
- assert (!CostCtx.SkipCostComputation .contains (CondI) &&
7367
- " Condition already skipped?" );
7368
- CostCtx.SkipCostComputation .insert (CondI);
7369
- Cost += CostCtx.getLegacyCost (CondI, VF);
7367
+ ExitInstrs.insert (CondI);
7368
+ }
7369
+ }
7370
+ // Compute the cost of all instructions only feeding the exit conditions.
7371
+ for (unsigned I = 0 ; I != ExitInstrs.size (); ++I) {
7372
+ Instruction *CondI = ExitInstrs[I];
7373
+ if (!OrigLoop->contains (CondI) ||
7374
+ !CostCtx.SkipCostComputation .insert (CondI).second )
7375
+ continue ;
7376
+ Cost += CostCtx.getLegacyCost (CondI, VF);
7377
+ for (Value *Op : CondI->operands ()) {
7378
+ auto *OpI = dyn_cast<Instruction>(Op);
7379
+ if (!OpI || any_of (OpI->users (), [&ExitInstrs](User *U) {
7380
+ return !ExitInstrs.contains (cast<Instruction>(U));
7381
+ }))
7382
+ continue ;
7383
+ ExitInstrs.insert (OpI);
7370
7384
}
7371
7385
}
7372
7386
0 commit comments