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

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 10 05:24:50 PST 2025


================
@@ -10987,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();
----------------
jayfoad wrote:

```suggestion
    unsigned ShiftSum = Sum.getLimitedValue(OpSizeInBits - 1);
```

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


More information about the llvm-commits mailing list