[llvm] [SCEVExp] Factor out findExistingExpansionAndDropPoisonFlags helper (NFC) (PR #218700)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 25 07:43:32 PDT 2026


https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/218700

Move out helper to find an existing expanded value to re-use.

Preparation for a follow-up patch

>From 8916b1e817ea76705d4ef8ac5daacffc6fd5f80a Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Tue, 25 Aug 2026 11:53:51 +0100
Subject: [PATCH] [SCEVExpander] Factor out findExistingExpansion helper (NFC)

expand() looks up an already-expanded Value for a SCEV and, on a hit, drops
the poison-generating flags reuse requires. Pull that pattern into a
findExistingExpansion helper so other callers needing the same lookup don't
have to duplicate the drop-flags loop.
---
 .../Utils/ScalarEvolutionExpander.h           |  5 ++++
 .../Utils/ScalarEvolutionExpander.cpp         | 25 ++++++++++++-------
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
index 9f8fd66c0c6f4..c98c0cb52fa9c 100644
--- a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
+++ b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
@@ -496,6 +496,11 @@ class SCEVExpander : public SCEVUseVisitor<SCEVExpander, Value *> {
       SCEVUse S, const Instruction *InsertPt,
       SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts);
 
+  /// Like FindValueInExprValueMap, but on a successful lookup also drops the
+  /// poison-generating flags that reusing the value requires.
+  Value *findExistingExpansionAndDropPoisonFlags(SCEVUse S,
+                                                 const Instruction *InsertPt);
+
   LLVM_ABI Value *expand(SCEVUse S);
   Value *expand(SCEVUse S, BasicBlock::iterator I) {
     setInsertPoint(I);
diff --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
index 45a9f7309ab6c..f01a674609325 100644
--- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -1666,6 +1666,19 @@ Value *SCEVExpander::FindValueInExprValueMap(
   return nullptr;
 }
 
+Value *SCEVExpander::findExistingExpansionAndDropPoisonFlags(
+    SCEVUse S, const Instruction *InsertPt) {
+  SmallVector<Instruction *> DropPoisonGeneratingInsts;
+  Value *V = FindValueInExprValueMap(S, InsertPt, DropPoisonGeneratingInsts);
+  if (!V)
+    return nullptr;
+  for (Instruction *I : DropPoisonGeneratingInsts) {
+    rememberFlags(I);
+    dropPoisonGeneratingAnnotationsAndReinfer(SE, I);
+  }
+  return V;
+}
+
 // The expansion of SCEV will either reuse a previous Value in ExprValueMap,
 // or expand the SCEV literally. Specifically, if the expansion is in LSRMode,
 // and the SCEV contains any sub scAddRecExpr type SCEV, it will be expanded
@@ -1733,23 +1746,17 @@ Value *SCEVExpander::expand(SCEVUse S) {
   Builder.SetInsertPoint(InsertPt->getParent(), InsertPt);
 
   // Expand the expression into instructions.
-  SmallVector<Instruction *> DropPoisonGeneratingInsts;
-  Value *V = FindValueInExprValueMap(S, &*InsertPt, DropPoisonGeneratingInsts);
+  Value *V = findExistingExpansionAndDropPoisonFlags(S, &*InsertPt);
   BasicBlock::iterator CacheAt = InsertPt;
   if (!V && InsertPt != OrigInsertPt && PostIncLoops.empty()) {
     // Hoisting the insertion point can move it above a value that already
     // computes S. Such a value is still usable: it only has to dominate the
     // point we were asked to expand at, which is where the result is used.
-    V = FindValueInExprValueMap(S, &*OrigInsertPt, DropPoisonGeneratingInsts);
+    V = findExistingExpansionAndDropPoisonFlags(S, &*OrigInsertPt);
     if (V)
       CacheAt = OrigInsertPt;
   }
-  if (V) {
-    for (Instruction *I : DropPoisonGeneratingInsts) {
-      rememberFlags(I);
-      dropPoisonGeneratingAnnotationsAndReinfer(SE, I);
-    }
-  } else {
+  if (!V) {
     V = visit(S);
     V = fixupLCSSAFormFor(V);
   }



More information about the llvm-commits mailing list