[llvm] de6fd16 - [RISCV] Don't fold (sub C, (setcc x, y, eq/neq)) -> (add C-1, (setcc x, y, neq/eq)) if C-1 isn't simm12.

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 16 14:14:55 PDT 2022


Author: Craig Topper
Date: 2022-08-16T14:11:31-07:00
New Revision: de6fd169715764f0401d8580b64c11fda45101e1

URL: https://github.com/llvm/llvm-project/commit/de6fd169715764f0401d8580b64c11fda45101e1
DIFF: https://github.com/llvm/llvm-project/commit/de6fd169715764f0401d8580b64c11fda45101e1.diff

LOG: [RISCV] Don't fold (sub C, (setcc x, y, eq/neq)) -> (add C-1, (setcc x, y, neq/eq)) if C-1 isn't simm12.

We still need to materialize the constant in a register and we
may not be removing all uses of the original constant so it may
increase code size.

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/RISCVISelLowering.cpp
    llvm/test/CodeGen/RISCV/select-const.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index ea38077b4ab1..1311d7bd58a8 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -8294,12 +8294,16 @@ static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG) {
     EVT SetCCOpVT = N1.getOperand(0).getValueType();
     if (!N0C->isZero() && SetCCOpVT.isInteger() && isIntEqualitySetCC(CCVal)) {
       EVT VT = N->getValueType(0);
-      const APInt &ImmVal = N0C->getAPIntValue();
-      CCVal = ISD::getSetCCInverse(CCVal, SetCCOpVT);
-      SDValue NewN0 =
-          DAG.getSetCC(SDLoc(N), VT, N1.getOperand(0), N1.getOperand(1), CCVal);
-      SDValue NewN1 = DAG.getConstant(ImmVal - 1, SDLoc(N), VT);
-      return DAG.getNode(ISD::ADD, SDLoc(N), VT, NewN0, NewN1);
+      APInt ImmValMinus1 = N0C->getAPIntValue() - 1;
+      // If this doesn't form ADDI, the transform won't save any instructions
+      // and may increase the number of constants we need.
+      if (ImmValMinus1.isSignedIntN(12)) {
+        CCVal = ISD::getSetCCInverse(CCVal, SetCCOpVT);
+        SDValue NewN0 =
+            DAG.getSetCC(SDLoc(N), VT, N1.getOperand(0), N1.getOperand(1), CCVal);
+        SDValue NewN1 = DAG.getConstant(ImmValMinus1, SDLoc(N), VT);
+        return DAG.getNode(ISD::ADD, SDLoc(N), VT, NewN0, NewN1);
+      }
     }
   }
 

diff  --git a/llvm/test/CodeGen/RISCV/select-const.ll b/llvm/test/CodeGen/RISCV/select-const.ll
index 25980ab87b71..071626e9c63f 100644
--- a/llvm/test/CodeGen/RISCV/select-const.ll
+++ b/llvm/test/CodeGen/RISCV/select-const.ll
@@ -448,19 +448,19 @@ define i32 @select_eq_10000_10001(i32 signext %a, i32 signext %b) {
 ; RV32-LABEL: select_eq_10000_10001:
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    xor a0, a0, a1
-; RV32-NEXT:    snez a0, a0
+; RV32-NEXT:    seqz a0, a0
 ; RV32-NEXT:    lui a1, 2
-; RV32-NEXT:    addi a1, a1, 1809
-; RV32-NEXT:    add a0, a0, a1
+; RV32-NEXT:    addi a1, a1, 1810
+; RV32-NEXT:    sub a0, a1, a0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: select_eq_10000_10001:
 ; RV64:       # %bb.0:
 ; RV64-NEXT:    xor a0, a0, a1
-; RV64-NEXT:    snez a0, a0
+; RV64-NEXT:    seqz a0, a0
 ; RV64-NEXT:    lui a1, 2
-; RV64-NEXT:    addiw a1, a1, 1809
-; RV64-NEXT:    add a0, a0, a1
+; RV64-NEXT:    addiw a1, a1, 1810
+; RV64-NEXT:    sub a0, a1, a0
 ; RV64-NEXT:    ret
   %1 = icmp eq i32 %a, %b
   %2 = select i1 %1, i32 10001, i32 10002
@@ -471,19 +471,19 @@ define i32 @select_ne_10001_10002(i32 signext %a, i32 signext %b) {
 ; RV32-LABEL: select_ne_10001_10002:
 ; RV32:       # %bb.0:
 ; RV32-NEXT:    xor a0, a0, a1
-; RV32-NEXT:    seqz a0, a0
+; RV32-NEXT:    snez a0, a0
 ; RV32-NEXT:    lui a1, 2
-; RV32-NEXT:    addi a1, a1, 1809
-; RV32-NEXT:    add a0, a0, a1
+; RV32-NEXT:    addi a1, a1, 1810
+; RV32-NEXT:    sub a0, a1, a0
 ; RV32-NEXT:    ret
 ;
 ; RV64-LABEL: select_ne_10001_10002:
 ; RV64:       # %bb.0:
 ; RV64-NEXT:    xor a0, a0, a1
-; RV64-NEXT:    seqz a0, a0
+; RV64-NEXT:    snez a0, a0
 ; RV64-NEXT:    lui a1, 2
-; RV64-NEXT:    addiw a1, a1, 1809
-; RV64-NEXT:    add a0, a0, a1
+; RV64-NEXT:    addiw a1, a1, 1810
+; RV64-NEXT:    sub a0, a1, a0
 ; RV64-NEXT:    ret
   %1 = icmp ne i32 %a, %b
   %2 = select i1 %1, i32 10001, i32 10002


        


More information about the llvm-commits mailing list