[llvm] [SCEV] Add option to request use-specific SCEV for a GEP expr, (PR #190677)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 10 08:27:30 PDT 2026


================
@@ -3967,6 +3970,30 @@ const SCEV *ScalarEvolution::getGEPExpr(SCEVUse BaseExpr,
   auto *GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap);
   assert(BaseExpr->getType() == GEPExpr->getType() &&
          "GEP should not change type mid-flight.");
+  if (!NUW) {
+    if (UseSpecificNW.hasNoUnsignedWrap() ||
+        (UseSpecificNW.isInBounds() && isKnownNonNegative(Offset))) {
+      // Check if it is safe to annotate the expression with use-specific NUW.
+      // Don't apply it if Base or Offset contain potentially wrapping
+      // sub-expressions that could be flattened into a larger Add expression.
+      auto IsSafeForUseNUW = [](SCEVUse S) {
+        if (any(S.getUseNoWrapFlags() & SCEV::FlagNUW))
+          return true;
+        // Add and AddRec expressions could be flattened into a wider
+        // add, so they need NUW to be safe.
+        if (isa<SCEVAddExpr, SCEVAddRecExpr>(S))
+          return S.hasNoUnsignedWrap();
+        // Conservatively allow a small set of expression forms that are
+        // known safe: they won't be flattened into a wider add.
+        const SCEVUnknown *U;
+        return match(
+            S, m_CombineOr(m_scev_ZExt(m_SCEVUnknown(U)), m_SCEVUnknown(U)));
+        match(S, m_SCEVConstant());
+      };
+      if (IsSafeForUseNUW(BaseExpr) && IsSafeForUseNUW(Offset))
----------------
fhahn wrote:

yeah I wanted to limit the scope initially, but updated now to pass UseFlags also to getAddExpr.

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


More information about the llvm-commits mailing list