[llvm] [DAG] foldShiftToAvg - recognize sub(x, xor(y, -1)) >> 1 as avgceil[su] (PR #182616)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 1 03:36:56 PDT 2026


================
@@ -17148,6 +17195,24 @@ SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
     }
   }
 
+  // fold (trunc (sra/srl (sub (x, xor y, -1)), 1))) ->
+  // 			(avgceil[su] (trunc y, trunc x))
+  // Matched before SimplifyDemandedBits which can convert sra -> srl.
+  SDValue X, Y, XorRHS;
+  unsigned ShiftOpc = N0.getOpcode();
+  if ((ShiftOpc == ISD::SRA || ShiftOpc == ISD::SRL) && N0.hasOneUse() &&
+      sd_match(N0,
+               m_BinOp(ShiftOpc,
+                       m_Sub(m_Value(X), m_Xor(m_Value(Y), m_Value(XorRHS))),
----------------
RKSimon wrote:

Isn't this?
```suggestion
                       m_Sub(m_Value(X), m_Not(m_Value(Y))),
```
and you can remove the xorrhs matching from getAvgCeilFromXor

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


More information about the llvm-commits mailing list