[llvm] [DAG] Fold add(mul(add(A, CA), CM), CB) -> add(mul(A, CM), CM*CA+CB) (PR #90860)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue May 7 03:50:37 PDT 2024


================
@@ -2838,6 +2838,36 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {
     return DAG.getNode(ISD::ADD, DL, VT, Not, N0.getOperand(0));
   }
 
+  // Fold add(mul(add(A, CA), CM), CB) -> add(mul(A, CM), CM*CA+CB).
+  // This can help if the inner add has multiple uses.
+  APInt CM, CA;
+  if (ConstantSDNode *CB = dyn_cast<ConstantSDNode>(N1)) {
+    if (sd_match(N0, m_OneUse(m_Mul(m_Add(m_Value(A), m_ConstInt(CA)),
+                                    m_ConstInt(CM)))) &&
+        TLI.isLegalAddImmediate(
+            (CA * CM + CB->getAPIntValue()).getSExtValue())) {
+      SDValue Mul =
+          DAG.getNode(ISD::MUL, SDLoc(N1), VT, A, DAG.getConstant(CM, DL, VT));
+      return DAG.getNode(
+          ISD::ADD, DL, VT, Mul,
----------------
arsenm wrote:

I mean the proof you wrote here isn't useful with small constants. You would ideally rewrite it with variables, and then assume calls if you need to restrict the ranges in some way 

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


More information about the llvm-commits mailing list