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

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 11 09:35:24 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).
----------------
mshockwave wrote:

In the absent of Zba, is this pattern still profitable? If that's the case, could we predicate this pattern by the availability of Zba?
Also, could you add a test for this case?

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


More information about the llvm-commits mailing list