[PATCH] D131471: [RISCV] Fold (sub constant, (setcc x, y, eq/neq)) -> (add constant - 1, (setcc x, y, neq/eq))
Liao Chunyu via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 9 01:41:58 PDT 2022
liaolucy created this revision.
liaolucy added reviewers: craig.topper, asb.
Herald added subscribers: sunshaoce, VincentWu, luke957, StephenFan, vkmr, frasercrmck, evandro, luismarques, 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.
liaolucy requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, eopXD, MaskRay.
Herald added a project: LLVM.
(setcc x, y, eq/neq) are seqz, snez that set rd = 0/1.
addi is used to process immediate, which can save instructions for load immediate.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D131471
Files:
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
llvm/test/CodeGen/RISCV/setcc-logic.ll
Index: llvm/test/CodeGen/RISCV/setcc-logic.ll
===================================================================
--- llvm/test/CodeGen/RISCV/setcc-logic.ll
+++ llvm/test/CodeGen/RISCV/setcc-logic.ll
@@ -118,3 +118,24 @@
%r = and i1 %a, %b
ret i1 %r
}
+
+define i32 @bar(i32 %n) {
+; RV32I-LABEL: bar:
+; RV32I: # %bb.0: # %entry
+; RV32I-NEXT: addi a0, a0, -9
+; RV32I-NEXT: snez a0, a0
+; RV32I-NEXT: addi a0, a0, 1
+; RV32I-NEXT: ret
+;
+; RV64I-LABEL: bar:
+; RV64I: # %bb.0: # %entry
+; RV64I-NEXT: sext.w a0, a0
+; RV64I-NEXT: addi a0, a0, -9
+; RV64I-NEXT: snez a0, a0
+; RV64I-NEXT: addi a0, a0, 1
+; RV64I-NEXT: ret
+entry:
+ %cmp = icmp eq i32 %n, 9
+ %a = select i1 %cmp, i32 1, i32 2
+ ret i32 %a
+}
Index: llvm/lib/Target/RISCV/RISCVISelLowering.cpp
===================================================================
--- llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -8264,10 +8264,32 @@
}
static SDValue performSUBCombine(SDNode *N, SelectionDAG &DAG) {
- // fold (sub x, (select lhs, rhs, cc, 0, y)) ->
- // (select lhs, rhs, cc, x, (sub x, y))
SDValue N0 = N->getOperand(0);
SDValue N1 = N->getOperand(1);
+
+ // Prefer to make this 'add 0/1' rather than 'sub 0/1'
+ // sub constant(!0), 0/1 -> add constant - 1, 1/0
+ // NODE: constant == 0, No redundant instructions are generated.
+ // (sub constant, (setcc x, y, eq/neq)) ->
+ // (add constant - 1, (setcc x, y, neq/eq))
+ if (isa<ConstantSDNode>(N0) && N1.getOpcode() == ISD::SETCC) {
+ auto *Nnz0 = cast<ConstantSDNode>(N0);
+ const auto *CC = cast<CondCodeSDNode>(N1->getOperand(2));
+ ISD::CondCode CCVal = CC->get();
+ if (!Nnz0->isZero() && (CCVal == ISD::SETEQ || CCVal == ISD::SETNE)) {
+ EVT VT = N->getValueType(0);
+ int64_t ImmVal = cast<ConstantSDNode>(N0)->getSExtValue() - 1;
+ SDValue NewN0 = DAG.getConstant(ImmVal, SDLoc(N), VT);
+ SDValue CCInverse =
+ DAG.getCondCode(ISD::getSetCCInverse(CCVal, N0.getValueType()));
+ SDValue NewN1 = DAG.getNode(ISD::SETCC, SDLoc(N), VT, N1->getOperand(0),
+ N1->getOperand(1), CCInverse);
+ return DAG.getNode(ISD::ADD, SDLoc(N), VT, NewN0, NewN1);
+ }
+ }
+
+ // fold (sub x, (select lhs, rhs, cc, 0, y)) ->
+ // (select lhs, rhs, cc, x, (sub x, y))
return combineSelectAndUse(N, N1, N0, DAG, /*AllOnes*/ false);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131471.451065.patch
Type: text/x-patch
Size: 2465 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220809/96090fde/attachment.bin>
More information about the llvm-commits
mailing list