[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
Thu Nov 20 07:37:16 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:

After a bit of investigating it looks like planContainsAdditionalSimplifications returns true on most loops due to #157387 as it looks like the check it added returns true for most memory accesses.

If I comment that check out then the assertion fails on the following (with the above increase to icmp cost) when using `opt -passes=loop-vectorize -mtriple=aarch64-none-elf -mcpu=cortex-a320 -prefer-predicate-over-epilogue=predicate-else-scalar-epilogue`:
```
define void @fn(ptr %p, i64 %n) {
entry:
  br label %for.body

for.body:                                         ; preds = %entry, %for.inc
  %i.06 = phi i64 [ 0, %entry ], [ %inc, %for.inc ]
  %arrayidx = getelementptr inbounds nuw i32, ptr %p, i64 %i.06
  %0 = load i32, ptr %arrayidx, align 4
  %inc = add nuw nsw i64 %i.06, 1
  %exitcond = icmp ne i64 %inc, %n
  br i1 %exitcond, label %if.then, label %for.inc

if.then:                                          ; preds = %for.body
  %add = add nsw i32 %0, 1
  store i32 %add, ptr %arrayidx, align 4
  br label %for.inc

for.inc:                                          ; preds = %for.body, %if.then
  br i1 %exitcond, label %for.body, label %for.cond.cleanup

for.cond.cleanup:                                 ; preds = %for.inc
  ret void
}
```

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


More information about the llvm-commits mailing list