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

John Brawn via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 26 08:31:12 PST 2025


================
@@ -6988,6 +6948,35 @@ static bool planContainsAdditionalSimplifications(VPlan &Plan,
     });
   });
 }
+
+static bool planContainsDifferentCompares(VPlan &Plan, VPCostContext &CostCtx,
+                                          Loop *TheLoop, ElementCount VF) {
+  // Count how many compare instructions there are in the legacy cost model.
+  unsigned NumLegacyCompares = 0;
+  for (BasicBlock *BB : TheLoop->blocks()) {
+    for (auto &I : *BB) {
+      if (isa<CmpInst>(I)) {
+        NumLegacyCompares += 1;
+      }
+    }
+  }
+
+  // Count how many compare instructions there are in the VPlan.
+  unsigned NumVPlanCompares = 0;
+  auto Iter = vp_depth_first_deep(Plan.getVectorLoopRegion()->getEntry());
+  for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(Iter)) {
----------------
john-brawn-arm wrote:

In every function in ``llvm/test/Transforms/LoopVectorize/X86/float-induction-x86.ll`` planContainsAdditionalSimplifications is returning true: In the first function ``%lftr.wideiv = trunc i64 %iv.next to i32`` doesn't appear in the list of seen instructions, in the second ``%cond = icmp slt i64 %i.next, %n``, in the third ``%cmp.not = icmp eq i64 %iv.next, %0``. It looks like all of these are due to the loop termination condition being converted to branch-on-count.

Out of the tests in llvm/tests/Transforms/LoopVectorize, only in the following does planContainsAdditionalSimplifications return false for at least one of the test functions:
```
  LLVM :: Transforms/LoopVectorize/AArch64/call-costs.ll
  LLVM :: Transforms/LoopVectorize/AArch64/force-target-instruction-cost.ll
  LLVM :: Transforms/LoopVectorize/AArch64/fully-unrolled-cost.ll
  LLVM :: Transforms/LoopVectorize/AArch64/low_trip_count_predicates.ll
  LLVM :: Transforms/LoopVectorize/AArch64/optsize_minsize.ll
  LLVM :: Transforms/LoopVectorize/AArch64/replicating-load-store-costs.ll
  LLVM :: Transforms/LoopVectorize/AArch64/sve-vscale-based-trip-counts.ll
  LLVM :: Transforms/LoopVectorize/ARM/optsize_minsize.ll
  LLVM :: Transforms/LoopVectorize/SystemZ/scalar-steps-with-users-demanding-all-lanes-and-first-lane-only.ll
  LLVM :: Transforms/LoopVectorize/X86/cost-model.ll
  LLVM :: Transforms/LoopVectorize/X86/interleave-cost.ll
  LLVM :: Transforms/LoopVectorize/X86/pr34438.ll
  LLVM :: Transforms/LoopVectorize/X86/replicating-load-store-costs.ll
  LLVM :: Transforms/LoopVectorize/X86/vector_max_bandwidth.ll
  LLVM :: Transforms/LoopVectorize/vectorize-zero-estimated-trip-count.ll
```

Strangely, with the above change in these files planContainsAdditionalSimplifications changes to returning true. I think what's going on is that because of the increase in cost BestPlan is now the scalar plan, so planContainsAdditionalSimplifications is checking the scalar plan whereas before it was checking the vector plan, and there's something in the scalar plan that causes planContainsAdditionalSimplifications to return true.

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


More information about the llvm-commits mailing list