[llvm] [VPlan] Don't use the legacy cost model for loop conditions (PR #156864)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 14 10:33:20 PDT 2025


================
@@ -1151,6 +1151,32 @@ InstructionCost VPInstruction::computeCost(ElementCount VF,
     return Ctx.TTI.getIndexedVectorInstrCostFromEnd(Instruction::ExtractElement,
                                                     VecTy, Ctx.CostKind, 0);
   }
+  case VPInstruction::BranchOnCount: {
+    // If TC <= VF then this is just a branch.
+    // FIXME: Removing the branch happens in simplifyBranchConditionForVFAndUF
----------------
david-arm wrote:

So you're saying the cost of this branch is based on a prediction about what `simplifyBranchConditionForVFAndUF` is going to do later on? I guess that's fine so long as they're both using the same logic. Ideally both `simplifyBranchConditionForVFAndUF` and this code would call the same common function checking if the branch will be simplified or not. I'm just a bit worried that over time the two will diverge. Although I appreciate here in the code you'd have to assume UF=1.

For example, if you pulled this code out of `simplifyBranchConditionForVFAndUF` into a common function you could reuse it in both places:

```
    // Try to simplify the branch condition if TC <= VF * UF when the latch
    // terminator is   BranchOnCount or BranchOnCond where the input is
    // Not(ActiveLaneMask).
    const SCEV *TripCount =
        vputils::getSCEVExprForVPValue(Plan.getTripCount(), SE);
    assert(!isa<SCEVCouldNotCompute>(TripCount) &&
           "Trip count SCEV must be computable");
    ElementCount NumElements = BestVF.multiplyCoefficientBy(BestUF);
    const SCEV *C = SE.getElementCount(TripCount->getType(), NumElements);
    if (TripCount->isZero() ||
        !SE.isKnownPredicate(CmpInst::ICMP_ULE, TripCount, C))
      return false;
```

You'd also now be able to remove the `// FIXME: The compare could also be removed if TC = M * vscale,` comment below.

https://github.com/llvm/llvm-project/pull/156864


More information about the llvm-commits mailing list