[llvm] [SCEVExpander] Cache expansions for canonical SCEV. (PR #228890)

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 4 06:14:18 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Florian Hahn (fhahn)

<details>
<summary>Changes</summary>

Update SCEVExpander to allow re-use of expansion results with different
use flags.

Add an additional bool to the cached entries, to indicate that the
expression can be re-used but flags need to be dropped.

This allows re-using expressions differing in their use flags and fixes
expansion regressions after
https://github.com/llvm/llvm-project/pull/224974, similar to
https://github.com/llvm/llvm-project/pull/228224.

---
Full diff: https://github.com/llvm/llvm-project/pull/228890.diff


3 Files Affected:

- (modified) llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h (+5-2) 
- (modified) llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp (+30-5) 
- (added) llvm/test/Transforms/IndVarSimplify/exit-value-use-flags-reuse.ll (+127) 


``````````diff
diff --git a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
index e72235fbf6d42..d33caff177c48 100644
--- a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
+++ b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
@@ -74,8 +74,11 @@ class SCEVExpander : public SCEVUseVisitor<SCEVExpander, Value *> {
   /// Indicates whether LCSSA phis should be created for inserted values.
   bool PreserveLCSSA;
 
-  // InsertedExpressions caches Values for reuse, so must track RAUW.
-  DenseMap<std::pair<SCEVUse, Instruction *>, TrackingVH<Value>>
+  // InsertedExpressions caches Values for reuse, so must track RAUW. The
+  // boolean indicates whether poison-generating annotations must be dropped
+  // before reuse.
+  DenseMap<std::pair<SCEVUse, Instruction *>,
+           std::pair<TrackingVH<Value>, bool>>
       InsertedExpressions;
 
   // InsertedOverflowChecks caches Values for reuse, so must track RAUW.
diff --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
index d56958be37b94..540a45d12e99f 100644
--- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -1738,10 +1738,32 @@ Value *SCEVExpander::expand(SCEVUse S) {
     }
   }
 
-  // Check to see if we already expanded this here.
-  auto I = InsertedExpressions.find(std::make_pair(S, &*InsertPt));
-  if (I != InsertedExpressions.end())
-    return I->second;
+  // Check to see if we already expanded this or its canonical form here.
+  // Canonical entries populated by a flagged expansion drop poison-generating
+  // flags before re-use.
+  auto LookupCached = [&](SCEVUse Key) -> Value * {
+    auto It = InsertedExpressions.find({Key, &*InsertPt});
+    if (It == InsertedExpressions.end())
+      return nullptr;
+    auto &[CachedV, NeedsDropPoisonFlags] = It->second;
+    auto *CachedI = dyn_cast<Instruction>(&*CachedV);
+    if (!NeedsDropPoisonFlags || !CachedI)
+      return CachedV;
+    SmallVector<Instruction *> DropPoisonGeneratingInsts;
+    if (!SE.canReuseInstruction(Key, CachedI, DropPoisonGeneratingInsts))
+      return nullptr;
+    for (Instruction *I : DropPoisonGeneratingInsts) {
+      rememberFlags(I);
+      dropPoisonGeneratingAnnotationsAndReinfer(SE, I);
+    }
+    NeedsDropPoisonFlags = false;
+    return CachedV;
+  };
+  if (Value *V = LookupCached(S))
+    return V;
+  if (!S.isCanonical())
+    if (Value *V = LookupCached(S.getCanonical()))
+      return V;
 
   SCEVInsertPointGuard Guard(Builder, this);
   Builder.SetInsertPoint(InsertPt);
@@ -1767,7 +1789,10 @@ Value *SCEVExpander::expand(SCEVUse S) {
   // the expression at this insertion point. If the mapped value happened to be
   // a postinc expansion, it could be reused by a non-postinc user, but only if
   // its insertion point was already at the head of the loop.
-  InsertedExpressions[std::make_pair(S, &*CacheAt)] = V;
+  InsertedExpressions[{S, &*CacheAt}] = {V, false};
+  // Also make this expansion available for the canonical SCEV.
+  if (!S.isCanonical())
+    InsertedExpressions.try_emplace({S.getCanonical(), &*CacheAt}, V, true);
   return V;
 }
 
diff --git a/llvm/test/Transforms/IndVarSimplify/exit-value-use-flags-reuse.ll b/llvm/test/Transforms/IndVarSimplify/exit-value-use-flags-reuse.ll
new file mode 100644
index 0000000000000..6c1f030e4cc81
--- /dev/null
+++ b/llvm/test/Transforms/IndVarSimplify/exit-value-use-flags-reuse.ll
@@ -0,0 +1,127 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=indvars -S %s | FileCheck %s
+
+declare void @use(i64)
+
+; The exit values of %x and %y share (-1 + %n) * %b. Only %x is a nuw
+; recurrence, so only its copy has the use flag <u nuw>. The multiply should
+; be expanded once.
+define void @flagged_first(i64 %n, i64 %a, i64 %c, i64 %b) {
+; CHECK-LABEL: define void @flagged_first(
+; CHECK-SAME: i64 [[N:%.*]], i64 [[A:%.*]], i64 [[C:%.*]], i64 [[B:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    br i1 true, label %[[EXIT:.*]], label %[[LOOP]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = add i64 [[N]], -1
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[B]], [[TMP0]]
+; CHECK-NEXT:    [[TMP2:%.*]] = add nuw i64 [[A]], [[TMP1]]
+; CHECK-NEXT:    [[TMP3:%.*]] = add i64 [[C]], [[TMP1]]
+; CHECK-NEXT:    call void @use(i64 [[TMP2]])
+; CHECK-NEXT:    call void @use(i64 [[TMP3]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %x = phi i64 [ %a, %entry ], [ %x.next, %loop ]
+  %y = phi i64 [ %c, %entry ], [ %y.next, %loop ]
+  %x.next = add nuw i64 %x, %b
+  %y.next = add i64 %y, %b
+  %iv.next = add nuw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  %x.lcssa = phi i64 [ %x, %loop ]
+  %y.lcssa = phi i64 [ %y, %loop ]
+  call void @use(i64 %x.lcssa)
+  call void @use(i64 %y.lcssa)
+  ret void
+}
+
+; Same as above, but the exit value without use flags is expanded first.
+define void @flagged_second(i64 %n, i64 %a, i64 %c, i64 %b) {
+; CHECK-LABEL: define void @flagged_second(
+; CHECK-SAME: i64 [[N:%.*]], i64 [[A:%.*]], i64 [[C:%.*]], i64 [[B:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    br i1 true, label %[[EXIT:.*]], label %[[LOOP]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = add i64 [[N]], -1
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[B]], [[TMP0]]
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[C]], [[TMP1]]
+; CHECK-NEXT:    [[TMP3:%.*]] = add nuw i64 [[A]], [[TMP1]]
+; CHECK-NEXT:    call void @use(i64 [[TMP2]])
+; CHECK-NEXT:    call void @use(i64 [[TMP3]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %x = phi i64 [ %a, %entry ], [ %x.next, %loop ]
+  %y = phi i64 [ %c, %entry ], [ %y.next, %loop ]
+  %x.next = add nuw i64 %x, %b
+  %y.next = add i64 %y, %b
+  %iv.next = add nuw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  %y.lcssa = phi i64 [ %y, %loop ]
+  %x.lcssa = phi i64 [ %x, %loop ]
+  call void @use(i64 %y.lcssa)
+  call void @use(i64 %x.lcssa)
+  ret void
+}
+
+; Expand a canonical use, a flagged use, then another canonical use of the
+; shared product.
+define void @canonical_entry_survives_flagged_expansion(i64 %n, i64 %a, i64 %c, i64 %d, i64 %b) {
+; CHECK-LABEL: define void @canonical_entry_survives_flagged_expansion(
+; CHECK-SAME: i64 [[N:%.*]], i64 [[A:%.*]], i64 [[C:%.*]], i64 [[D:%.*]], i64 [[B:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    br i1 true, label %[[EXIT:.*]], label %[[LOOP]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = add i64 [[N]], -1
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[B]], [[TMP0]]
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[C]], [[TMP1]]
+; CHECK-NEXT:    [[TMP3:%.*]] = add nuw i64 [[A]], [[TMP1]]
+; CHECK-NEXT:    [[TMP4:%.*]] = add i64 [[D]], [[TMP1]]
+; CHECK-NEXT:    call void @use(i64 [[TMP2]])
+; CHECK-NEXT:    call void @use(i64 [[TMP3]])
+; CHECK-NEXT:    call void @use(i64 [[TMP4]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %x = phi i64 [ %a, %entry ], [ %x.next, %loop ]
+  %y = phi i64 [ %c, %entry ], [ %y.next, %loop ]
+  %z = phi i64 [ %d, %entry ], [ %z.next, %loop ]
+  %x.next = add nuw i64 %x, %b
+  %y.next = add i64 %y, %b
+  %z.next = add i64 %z, %b
+  %iv.next = add nuw i64 %iv, 1
+  %ec = icmp eq i64 %iv.next, %n
+  br i1 %ec, label %exit, label %loop
+
+exit:
+  %y.lcssa = phi i64 [ %y, %loop ]
+  %x.lcssa = phi i64 [ %x, %loop ]
+  %z.lcssa = phi i64 [ %z, %loop ]
+  call void @use(i64 %y.lcssa)
+  call void @use(i64 %x.lcssa)
+  call void @use(i64 %z.lcssa)
+  ret void
+}

``````````

</details>


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


More information about the llvm-commits mailing list