[llvm] [Analysis] Optimise mul(const, (udiv %n, const)) during SCEV expansion (PR #212769)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 30 08:25:22 PDT 2026


================
@@ -599,6 +599,24 @@ Value *SCEVExpander::visitAddExpr(SCEVUseT<const SCEVAddExpr *> S) {
 Value *SCEVExpander::visitMulExpr(SCEVUseT<const SCEVMulExpr *> S) {
   Type *Ty = S->getType();
 
+  // Specializations for 2-operand cases.
+
+  const SCEVConstant *C1, *C2;
+  const SCEV *Val;
+  // mul(PowerOf2C, (udiv X, PowerOf2C)) == (X >> C) << C
+  //  -> X & (-1 << C)
+  if (match(S, m_scev_Mul(m_SCEVConstant(C1),
+                          m_scev_UDiv(m_SCEV(Val), m_SCEVConstant(C2)))) &&
+      C1 == C2 && C1->getAPInt().isPowerOf2()) {
+    Value *LHS = expand(Val);
+    unsigned ShAmtC = C1->getAPInt().logBase2();
+    unsigned BitWidth = Ty->getScalarSizeInBits();
+    APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC));
----------------
david-arm wrote:

I actually copied the original code from the same thing in `lib/Transforms/InstCombine/InstCombineShifts.cpp`, but sure I can use your suggestion. :)

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


More information about the llvm-commits mailing list