[PATCH] D132000: [RISCV] Fold (sub C, (xor (setcc), 1)) -> (add (setcc), C-1).

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 16 16:36:28 PDT 2022


craig.topper created this revision.
craig.topper added reviewers: reames, asb, luismarques.
Herald added subscribers: sunshaoce, VincentWu, luke957, StephenFan, vkmr, frasercrmck, evandro, apazos, sameer.abuasal, s.egerton, Jim, benna, psnobl, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, hiraditya, arichardson.
Herald added a project: All.
craig.topper requested review of this revision.
Herald added subscribers: pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.

Extracted from D131729 <https://reviews.llvm.org/D131729> where we handled C==0. It's now generalized
to more constants.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132000

Files:
  llvm/lib/Target/RISCV/RISCVISelLowering.cpp
  llvm/test/CodeGen/RISCV/double-select-fcmp.ll
  llvm/test/CodeGen/RISCV/float-select-fcmp.ll
  llvm/test/CodeGen/RISCV/half-select-fcmp.ll


Index: llvm/test/CodeGen/RISCV/half-select-fcmp.ll
===================================================================
--- llvm/test/CodeGen/RISCV/half-select-fcmp.ll
+++ llvm/test/CodeGen/RISCV/half-select-fcmp.ll
@@ -258,8 +258,7 @@
 ; CHECK-LABEL: select_fcmp_uge_negone_zero:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    fle.h a0, fa0, fa1
-; CHECK-NEXT:    xori a0, a0, 1
-; CHECK-NEXT:    neg a0, a0
+; CHECK-NEXT:    addi a0, a0, -1
 ; CHECK-NEXT:    ret
   %1 = fcmp ugt half %a, %b
   %2 = select i1 %1, i32 -1, i32 0
@@ -270,9 +269,7 @@
 ; CHECK-LABEL: select_fcmp_uge_1_2:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    fle.h a0, fa0, fa1
-; CHECK-NEXT:    xori a0, a0, 1
-; CHECK-NEXT:    li a1, 2
-; CHECK-NEXT:    sub a0, a1, a0
+; CHECK-NEXT:    addi a0, a0, 1
 ; CHECK-NEXT:    ret
   %1 = fcmp ugt half %a, %b
   %2 = select i1 %1, i32 1, i32 2
Index: llvm/test/CodeGen/RISCV/float-select-fcmp.ll
===================================================================
--- llvm/test/CodeGen/RISCV/float-select-fcmp.ll
+++ llvm/test/CodeGen/RISCV/float-select-fcmp.ll
@@ -258,8 +258,7 @@
 ; CHECK-LABEL: select_fcmp_uge_negone_zero:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    fle.s a0, fa0, fa1
-; CHECK-NEXT:    xori a0, a0, 1
-; CHECK-NEXT:    neg a0, a0
+; CHECK-NEXT:    addi a0, a0, -1
 ; CHECK-NEXT:    ret
   %1 = fcmp ugt float %a, %b
   %2 = select i1 %1, i32 -1, i32 0
@@ -270,9 +269,7 @@
 ; CHECK-LABEL: select_fcmp_uge_1_2:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    fle.s a0, fa0, fa1
-; CHECK-NEXT:    xori a0, a0, 1
-; CHECK-NEXT:    li a1, 2
-; CHECK-NEXT:    sub a0, a1, a0
+; CHECK-NEXT:    addi a0, a0, 1
 ; CHECK-NEXT:    ret
   %1 = fcmp ugt float %a, %b
   %2 = select i1 %1, i32 1, i32 2
Index: llvm/test/CodeGen/RISCV/double-select-fcmp.ll
===================================================================
--- llvm/test/CodeGen/RISCV/double-select-fcmp.ll
+++ llvm/test/CodeGen/RISCV/double-select-fcmp.ll
@@ -258,8 +258,7 @@
 ; CHECK-LABEL: select_fcmp_uge_negone_zero:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    fle.d a0, fa0, fa1
-; CHECK-NEXT:    xori a0, a0, 1
-; CHECK-NEXT:    neg a0, a0
+; CHECK-NEXT:    addi a0, a0, -1
 ; CHECK-NEXT:    ret
   %1 = fcmp ugt double %a, %b
   %2 = select i1 %1, i32 -1, i32 0
@@ -270,9 +269,7 @@
 ; CHECK-LABEL: select_fcmp_uge_1_2:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    fle.d a0, fa0, fa1
-; CHECK-NEXT:    xori a0, a0, 1
-; CHECK-NEXT:    li a1, 2
-; CHECK-NEXT:    sub a0, a1, a0
+; CHECK-NEXT:    addi a0, a0, 1
 ; CHECK-NEXT:    ret
   %1 = fcmp ugt double %a, %b
   %2 = select i1 %1, i32 1, i32 2
Index: llvm/lib/Target/RISCV/RISCVISelLowering.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -8308,6 +8308,11 @@
     CCVal = ISD::getSetCCInverse(CCVal, SetCCOpVT);
     NewLHS =
         DAG.getSetCC(SDLoc(N1), VT, N1.getOperand(0), N1.getOperand(1), CCVal);
+  } else if (N1.getOpcode() == ISD::XOR && isOneConstant(N1.getOperand(1)) &&
+             N1.getOperand(0).getOpcode() == ISD::SETCC) {
+    // (sub C, (xor (setcc), 1)) -> (add (setcc), C-1).
+    // Since setcc returns a bool the xor is equivalent to 1-setcc.
+    NewLHS = N1.getOperand(0);
   } else
     return SDValue();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132000.453162.patch
Type: text/x-patch
Size: 3301 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220816/a34ee1ad/attachment.bin>


More information about the llvm-commits mailing list