[llvm] [Loads] Add MUL/SHL handling in decomposeLinearExpression (PR #222574)

Arne Stenkrona via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 29 01:40:49 PDT 2026


================
@@ -943,6 +945,30 @@ LinearExpression llvm::decomposeLinearExpression(const DataLayout &DL,
     // members past this point.
     Expr.BasePtr = GEP->getPointerOperand();
     Expr.Flags = Expr.Flags.intersectForOffsetAdd(GEP->getNoWrapFlags());
+
+    // Try to look through mul and shl
+    APInt IndexScale(BitWidth, 1);
+    if (auto *BO = dyn_cast_or_null<BinaryOperator>(VarIndex)) {
+      Value *UnscaledIndex = nullptr;
+      const APInt *Constant;
+      if (match(BO, m_Mul(m_Value(UnscaledIndex), m_APInt(Constant)))) {
+        IndexScale *= *Constant;
+      } else if (match(BO, m_Shl(m_Value(UnscaledIndex), m_APInt(Constant))) &&
+                 Constant->ult(BitWidth)) {
+        IndexScale <<= *Constant;
+      } else {
+        break;
+      }
+
+      // If we do not have nuw or nsw on the mul/shl we need to update the flags
+      if (!BO->hasNoSignedWrap())
+        Expr.Flags = Expr.Flags.withoutNoUnsignedSignedWrap();
+      if (!BO->hasNoSignedWrap())
----------------
ArneStenkrona2 wrote:

Ah, that was my intention but I made a typo here. Terribly sorry, updated now to hasNoUnsignedWrap.

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


More information about the llvm-commits mailing list