[llvm] [DAG] foldAddToAvg - split nested m_Reassociatable matchers (PR #171681)

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 11 09:51:52 PST 2025


================
@@ -3164,26 +3164,28 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {
 SDValue DAGCombiner::foldAddToAvg(SDNode *N, const SDLoc &DL) {
   SDValue N0 = N->getOperand(0);
   EVT VT = N0.getValueType();
-  SDValue A, B;
+  SDValue A, B, C;
 
   if ((!LegalOperations || hasOperation(ISD::AVGFLOORU, VT)) &&
       (sd_match(N,
                 m_Add(m_And(m_Value(A), m_Value(B)),
                       m_Srl(m_Xor(m_Deferred(A), m_Deferred(B)), m_One()))) ||
-       sd_match(N, m_ReassociatableAdd(
-                       m_Srl(m_Value(A), m_One()), m_Srl(m_Value(B), m_One()),
-                       m_ReassociatableAnd(m_Deferred(A), m_Deferred(B),
-                                           m_One()))))) {
+       (sd_match(N,
+                 m_ReassociatableAdd(m_Srl(m_Value(A), m_One()),
+                                     m_Srl(m_Value(B), m_One()), m_Value(C))) &&
----------------
mshockwave wrote:

just a comment: since `m_ReassociatableAnd` in the nest line will always collect the leaves first, we can avoid such collection on obviously-wrong cases (e.g. `C` is not even an ADD) by checking whether `C` is ADD here preemptively. Though that means we have to do some ugly conditional binding like `m_AllOf(m_Opc(ISD::ADD), m_Value(C))` to bind this term to `C` only if it's an ADD, which probably will just complicate the code further.

Side note: instead of `m_AllOf(m_Opc(ISD::ADD), m_Value(C))`, we should create something like `m_Value(C, <sub pattern>)` to bind to a SDValue only if the sub-pattern matches.

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


More information about the llvm-commits mailing list