Skip to content

Commit d757cc5

Browse files
committed
[RISCV] Reorganize select lowering to pull binop expansion early
This is purely stylistic, but I think makes the code easier to follow. It isn't quite NFC because it undoes the airthmetic lowering for the select c, simm12, 0 cases for a processor with both conditional move forwarding and zicond.
1 parent 91e85cc commit d757cc5

File tree

2 files changed

+19
-34
lines changed

2 files changed

+19
-34
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9106,8 +9106,12 @@ static std::optional<bool> matchSetCC(SDValue LHS, SDValue RHS,
91069106
return std::nullopt;
91079107
}
91089108

9109-
static SDValue combineSelectToBinOp(SDNode *N, SelectionDAG &DAG,
9110-
const RISCVSubtarget &Subtarget) {
9109+
static bool isSimm12Constant(SDValue V) {
9110+
return isa<ConstantSDNode>(V) && V->getAsAPIntVal().isSignedIntN(12);
9111+
}
9112+
9113+
static SDValue lowerSelectToBinOp(SDNode *N, SelectionDAG &DAG,
9114+
const RISCVSubtarget &Subtarget) {
91119115
SDValue CondV = N->getOperand(0);
91129116
SDValue TrueV = N->getOperand(1);
91139117
SDValue FalseV = N->getOperand(2);
@@ -9127,14 +9131,17 @@ static SDValue combineSelectToBinOp(SDNode *N, SelectionDAG &DAG,
91279131
return DAG.getNode(ISD::OR, DL, VT, Neg, DAG.getFreeze(TrueV));
91289132
}
91299133

9134+
const bool HasCZero = VT.isScalarInteger() &&
9135+
(Subtarget.hasStdExtZicond() || Subtarget.hasVendorXVentanaCondOps());
9136+
91309137
// (select c, 0, y) -> (c-1) & y
9131-
if (isNullConstant(TrueV)) {
9138+
if (isNullConstant(TrueV) && (!HasCZero || isSimm12Constant(FalseV))) {
91329139
SDValue Neg = DAG.getNode(ISD::ADD, DL, VT, CondV,
91339140
DAG.getAllOnesConstant(DL, VT));
91349141
return DAG.getNode(ISD::AND, DL, VT, Neg, DAG.getFreeze(FalseV));
91359142
}
91369143
// (select c, y, 0) -> -c & y
9137-
if (isNullConstant(FalseV)) {
9144+
if (isNullConstant(FalseV) && (!HasCZero || isSimm12Constant(TrueV))) {
91389145
SDValue Neg = DAG.getNegative(CondV, DL, VT);
91399146
return DAG.getNode(ISD::AND, DL, VT, Neg, DAG.getFreeze(TrueV));
91409147
}
@@ -9240,10 +9247,6 @@ foldBinOpIntoSelectIfProfitable(SDNode *BO, SelectionDAG &DAG,
92409247
return DAG.getSelect(DL, VT, Sel.getOperand(0), NewT, NewF);
92419248
}
92429249

9243-
static bool isSimm12Constant(SDValue V) {
9244-
return isa<ConstantSDNode>(V) && V->getAsAPIntVal().isSignedIntN(12);
9245-
}
9246-
92479250
SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
92489251
SDValue CondV = Op.getOperand(0);
92499252
SDValue TrueV = Op.getOperand(1);
@@ -9259,26 +9262,17 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
92599262
return DAG.getNode(ISD::VSELECT, DL, VT, CondSplat, TrueV, FalseV);
92609263
}
92619264

9265+
// Try some other optimizations before falling back to generic lowering.
9266+
if (SDValue V = lowerSelectToBinOp(Op.getNode(), DAG, Subtarget))
9267+
return V;
9268+
92629269
// When Zicond or XVentanaCondOps is present, emit CZERO_EQZ and CZERO_NEZ
92639270
// nodes to implement the SELECT. Performing the lowering here allows for
92649271
// greater control over when CZERO_{EQZ/NEZ} are used vs another branchless
92659272
// sequence or RISCVISD::SELECT_CC node (branch-based select).
92669273
if ((Subtarget.hasStdExtZicond() || Subtarget.hasVendorXVentanaCondOps()) &&
92679274
VT.isScalarInteger()) {
92689275

9269-
// select c, simm12, 0 -> andi (sub x0, c), simm12
9270-
if (isSimm12Constant(TrueV) && isNullConstant(FalseV)) {
9271-
SDValue Mask = DAG.getNegative(CondV, DL, VT);
9272-
return DAG.getNode(ISD::AND, DL, VT, TrueV, Mask);
9273-
}
9274-
9275-
// select c, 0, simm12 -> andi (addi c, -1), simm12
9276-
if (isNullConstant(TrueV) && isSimm12Constant(FalseV)) {
9277-
SDValue Mask = DAG.getNode(ISD::ADD, DL, VT, CondV,
9278-
DAG.getSignedConstant(-1, DL, XLenVT));
9279-
return DAG.getNode(ISD::AND, DL, VT, FalseV, Mask);
9280-
}
9281-
92829276
// (select c, t, 0) -> (czero_eqz t, c)
92839277
if (isNullConstant(FalseV))
92849278
return DAG.getNode(RISCVISD::CZERO_EQZ, DL, VT, TrueV, CondV);
@@ -9332,10 +9326,6 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
93329326
DAG.getNode(RISCVISD::CZERO_EQZ, DL, VT, TrueV, CondV));
93339327
}
93349328

9335-
// Try some other optimizations before falling back to generic lowering.
9336-
if (SDValue V = combineSelectToBinOp(Op.getNode(), DAG, Subtarget))
9337-
return V;
9338-
93399329
// (select c, c1, c2) -> (add (czero_nez c2 - c1, c), c1)
93409330
// (select c, c1, c2) -> (add (czero_eqz c1 - c2, c), c2)
93419331
if (isa<ConstantSDNode>(TrueV) && isa<ConstantSDNode>(FalseV)) {
@@ -9438,9 +9428,6 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
94389428
SDNodeFlags::Disjoint);
94399429
}
94409430

9441-
if (SDValue V = combineSelectToBinOp(Op.getNode(), DAG, Subtarget))
9442-
return V;
9443-
94449431
if (Op.hasOneUse()) {
94459432
unsigned UseOpc = Op->user_begin()->getOpcode();
94469433
if (isBinOp(UseOpc) && DAG.isSafeToSpeculativelyExecute(UseOpc)) {

llvm/test/CodeGen/RISCV/cmov-branch-opt.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ define signext i32 @test4(i32 signext %x, i32 signext %y, i32 signext %z) {
149149
;
150150
; CMOV-ZICOND-LABEL: test4:
151151
; CMOV-ZICOND: # %bb.0:
152-
; CMOV-ZICOND-NEXT: snez a0, a2
153-
; CMOV-ZICOND-NEXT: addi a0, a0, -1
154-
; CMOV-ZICOND-NEXT: andi a0, a0, 3
152+
; CMOV-ZICOND-NEXT: li a0, 3
153+
; CMOV-ZICOND-NEXT: czero.nez a0, a0, a2
155154
; CMOV-ZICOND-NEXT: ret
156155
;
157156
; SFB-NOZICOND-LABEL: test4:
@@ -165,9 +164,8 @@ define signext i32 @test4(i32 signext %x, i32 signext %y, i32 signext %z) {
165164
;
166165
; SFB-ZICOND-LABEL: test4:
167166
; SFB-ZICOND: # %bb.0:
168-
; SFB-ZICOND-NEXT: snez a0, a2
169-
; SFB-ZICOND-NEXT: addi a0, a0, -1
170-
; SFB-ZICOND-NEXT: andi a0, a0, 3
167+
; SFB-ZICOND-NEXT: li a0, 3
168+
; SFB-ZICOND-NEXT: czero.nez a0, a0, a2
171169
; SFB-ZICOND-NEXT: ret
172170
%c = icmp eq i32 %z, 0
173171
%a = select i1 %c, i32 3, i32 0

0 commit comments

Comments
 (0)