[llvm] [RISCV] Optimize (slli (srli (slli X, C1), C1), C2) -> (srli (slli X, C1), C1-C2) (PR #119567)

Piotr Fusik via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 11 10:36:59 PST 2024


================
@@ -1041,6 +1041,21 @@ void RISCVDAGToDAGISel::Select(SDNode *Node) {
             CurDAG->getTargetConstant(TrailingZeros + ShAmt, DL, VT));
         ReplaceNode(Node, SLLI);
         return;
+      } else if (TrailingZeros == 0 && LeadingZeros > ShAmt &&
+                 XLen - LeadingZeros > 11 && LeadingZeros != 32) {
+        // Optimize (shl (and X, C2), C) -> (srli (slli X, C4), C4-C)
+        // where C2 has C4 leading zeros and no trailing zeros.
+        // This is profitable if the "and" was to be lowered to
+        // (srli (slli X, C4), C4) and not (andi X, C2).
+        // For "LeadingZeros == 32" we prefer Zba (slli.uw X, C).
----------------
pfusik wrote:

It is profitable without Zba. I added the `and_0xffffffff_shl_2` test.
It turns out that for `LeadingZeros == 32` this transform is applied elsewhere. What's the easiest way to check where this happens?
Perhaps it could be implemented for all cases in one place.

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


More information about the llvm-commits mailing list