[llvm] [DAGCombiner] Add sra-xor-sra pattern fold (PR #166777)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 7 01:11:26 PST 2025


================
@@ -10968,6 +10968,23 @@ SDValue DAGCombiner::visitSRA(SDNode *N) {
     }
   }
 
+  // fold (sra (xor (sra x, c1), -1), c2) -> (xor (sra x, c3), -1)
+  // This allows merging two arithmetic shifts even when there's a NOT in
+  // between.
+  SDValue X;
+  APInt C1;
+  if (N1C && sd_match(N0, m_OneUse(m_Not(
+                              m_OneUse(m_Sra(m_Value(X), m_ConstInt(C1))))))) {
+    APInt C2 = N1C->getAPIntValue();
+    zeroExtendToMatch(C1, C2, /*OverflowBit=*/1);
+    APInt Sum = C1 + C2;
+    unsigned ShiftSum =
+        Sum.uge(OpSizeInBits) ? (OpSizeInBits - 1) : Sum.getZExtValue();
+    SDValue NewShift = DAG.getNode(
+        ISD::SRA, DL, VT, X, DAG.getConstant(ShiftSum, DL, N1.getValueType()));
----------------
RKSimon wrote:

getConstant -> getShiftAmountConstant

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


More information about the llvm-commits mailing list