[llvm] [LV] Initial support for stores in early exit loops (PR #137774)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Fri May 9 07:28:01 PDT 2025


================
@@ -1656,6 +1688,21 @@ bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
         return false;
       }
 
+      // For loops with stores.
+      // Record load for analysis by isDereferenceableAndAlignedInLoop
+      // and later by dependence analysis.
+      if (BranchInst *Br = dyn_cast<BranchInst>(BB->getTerminator())) {
+        // FIXME: Handle exit conditions with multiple users, more complex exit
+        //        conditions than br(icmp(load, loop_inv)).
+        ICmpInst *Cmp = dyn_cast<ICmpInst>(Br->getCondition());
+        if (Cmp && Cmp->hasOneUse() &&
+            TheLoop->isLoopInvariant(Cmp->getOperand(1))) {
+          LoadInst *Load = dyn_cast<LoadInst>(Cmp->getOperand(0));
+          if (Load && Load->hasOneUse() && TheLoop->contains(Load))
----------------
david-arm wrote:

Is `TheLoop->contains` enough here, because it doesn't guarantee `Load` varies in the loop. Perhaps the load hasn't been hoisted out because it might have a memory conflict with another store? You might still want to do:

```
  if (Load && Load->hasOneUse() && !TheLoop->isLoopInvariant(Cmp->getOperand(0)))
```

In general, do we still make correct decisions regarding dependences in the loop between loads and stores? I'm thinking of situations where you have

```
for.body:
  ... load from a[i] ...
  ... store to a[i + 4] ...
  ... early exit compare ...
  br i1 %cmp, label %early.exit, label %for.inc

for.inc:
  ...
```

or

```
for.body:
  ... load from a[i] ...
  ... early exit compare ...
  br i1 %cmp, label %early.exit, label %for.inc

for.inc:
  ... store to a[i + 4] ...
  ...
```

or similarly for stores to negative offsets, like a[i - 4]

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


More information about the llvm-commits mailing list