[llvm] [RISCV] Lower SELECT's with one constant more efficiently using Zicond (PR #143581)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 10 15:39:55 PDT 2025


================
@@ -9088,6 +9088,22 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
       return DAG.getNode(ISD::ADD, DL, VT, CMOV, RHSVal);
     }
 
+    // (select c, c1, t) -> (add (czero_nez t - c1, c), c1)
+    // (select c, t, c1) -> (add (czero_eqz t - c1, c), c1)
+    if (isa<ConstantSDNode>(TrueV) ^ isa<ConstantSDNode>(FalseV)) {
+      bool IsCZERO_NEZ = isa<ConstantSDNode>(TrueV);
+      SDValue ConstVal = IsCZERO_NEZ ? TrueV : FalseV;
+      // Efficient only if the constant and its negation fit into `ADDI`
+      if (std::abs(cast<ConstantSDNode>(ConstVal)->getSExtValue()) < 0x800) {
----------------
topperc wrote:

Mathematically, yes, but ADDI is more compressible.

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


More information about the llvm-commits mailing list