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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 17 03:29:43 PDT 2026


================
@@ -935,6 +937,26 @@ LinearExpression llvm::decomposeLinearExpression(const DataLayout &DL,
       VarIndex = Index;
     }
 
+    APInt IndexScale(BitWidth, 1);
+    while (auto *BO = dyn_cast_or_null<BinaryOperator>(VarIndex)) {
+      Value *UnscaledIndex = nullptr;
+      APInt Factor(BitWidth, 0);
+      ConstantInt *Constant;
+      if (match(BO, m_c_Mul(m_Value(UnscaledIndex), m_ConstantInt(Constant)))) {
+        Factor = Constant->getValue().zextOrTrunc(BitWidth);
+      } else if (match(BO, m_Shl(m_Value(UnscaledIndex),
+                                 m_ConstantInt(Constant)))) {
+        if (Constant->getValue().uge(BitWidth))
+          break;
+        Factor.setBit(Constant->getZExtValue());
+      } else {
+        break;
+      }
+
+      IndexScale *= Factor;
+      VarIndex = UnscaledIndex;
+    }
+
     // Don't return non-canonical indexes.
     if (VarIndex && !VarIndex->getType()->isIntegerTy(BitWidth))
----------------
nikic wrote:

Should do this check before your new code. In that case you can drop the BitWidth adjustments, because you know the bit width must be the same.

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


More information about the llvm-commits mailing list