@@ -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-
92479250SDValue 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)) {
0 commit comments