[llvm] 99ef0dd - [RISCV] Fold (sub constant, (setcc x, y, eq/neq)) -> (add constant - 1, (setcc x, y, neq/eq))
via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 13 05:38:05 PDT 2022
Author: LiaoChunyu
Date: 2022-08-13T20:37:57+08:00
New Revision: 99ef0ddea3c39aca07c6309b7a5b3cfc4ce1ab19
URL: https://github.com/llvm/llvm-project/commit/99ef0ddea3c39aca07c6309b7a5b3cfc4ce1ab19
DIFF: https://github.com/llvm/llvm-project/commit/99ef0ddea3c39aca07c6309b7a5b3cfc4ce1ab19.diff
LOG: [RISCV] Fold (sub constant, (setcc x, y, eq/neq)) -> (add constant - 1, (setcc x, y, neq/eq))
(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.
Reviewed By: craig.topper
Differential Revision: https://reviews.llvm.org/D131471
Added:
Modified:
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
llvm/test/CodeGen/RISCV/setcc-logic.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 8ea09fe666e02..74e054fc0910e 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -8276,10 +8276,32 @@ static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
}
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 (setcc x, y, neq/eq), constant - 1)
+ auto *Nnz0 = dyn_cast<ConstantSDNode>(N0);
+ if (Nnz0 && N1.getOpcode() == ISD::SETCC && N1.hasOneUse()) {
+ const auto *CC = cast<CondCodeSDNode>(N1->getOperand(2));
+ ISD::CondCode CCVal = CC->get();
+ if (!Nnz0->isZero() && isIntEqualitySetCC(CCVal)) {
+ EVT VT = N->getValueType(0);
+ const APInt &ImmVal = Nnz0->getAPIntValue();
+ SDValue CCInverse =
+ DAG.getCondCode(ISD::getSetCCInverse(CCVal, N0.getValueType()));
+ SDValue NewN0 = DAG.getNode(ISD::SETCC, SDLoc(N), VT, N1->getOperand(0),
+ N1->getOperand(1), CCInverse);
+ SDValue NewN1 = DAG.getConstant(ImmVal - 1, SDLoc(N), VT);
+ 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);
}
diff --git a/llvm/test/CodeGen/RISCV/setcc-logic.ll b/llvm/test/CodeGen/RISCV/setcc-logic.ll
index 2107c17d1ec07..3209619b7d0eb 100644
--- a/llvm/test/CodeGen/RISCV/setcc-logic.ll
+++ b/llvm/test/CodeGen/RISCV/setcc-logic.ll
@@ -118,3 +118,24 @@ define i1 @and_icmps_const_not1bit_
diff (i32 %x) nounwind {
%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
+}
More information about the llvm-commits
mailing list