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

Paul Walker via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 29 06:54:59 PDT 2026


================
@@ -599,6 +599,27 @@ Value *SCEVExpander::visitAddExpr(SCEVUseT<const SCEVAddExpr *> S) {
 Value *SCEVExpander::visitMulExpr(SCEVUseT<const SCEVMulExpr *> S) {
   Type *Ty = S->getType();
 
+  // Specializations for 2-operand cases.
+  if (S->getNumOperands() == 2) {
+    const SCEVConstant *MulC;
+    const SCEV *Val;
+    // mul(PowerOf2C, (udiv X, PowerOf2C)) == (X >> C) << C
+    //  -> X & (-1 << C)
+    if (match(S->getOperand(0), m_SCEVConstant(MulC)) &&
+        MulC->getAPInt().isPowerOf2() &&
+        match(S->getOperand(1),
+              m_scev_UDiv(m_SCEV(Val), m_scev_Specific(MulC)))) {
----------------
paulwalker-arm wrote:

Perhaps use `m_scev_Mul` to match the whole expression? Point against is that we already know S is a mul, but it would remove the `getNumOperands` check and do the two operand matches in one.

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


More information about the llvm-commits mailing list