[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
Thu May 2 09:52:40 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 think using constants in the proof is making this more permissive looking than it should be

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


More information about the llvm-commits mailing list