Skip to content

Commit

Permalink
[RISCV] Make selectSETCC return SDValue instead of bool. NFC
Browse files Browse the repository at this point in the history
We can use a null SDValue for the 'false' case. This avoids the
need for an output parameter. This is consistent with other
SelectionDAG code.

Reviewed By: wangpc

Differential Revision: https://reviews.llvm.org/D155388
  • Loading branch information
topperc committed Jul 16, 2023
1 parent cc4f865 commit 2a33f47
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5767,56 +5767,49 @@ static SDValue combineSelectToBinOp(SDNode *N, SelectionDAG &DAG,
/// seteq/setne into something that can be compared with 0.
/// Based on RISCVDAGToDAGISel::selectSETCC but modified to produce
/// target-independent SelectionDAG nodes rather than machine nodes.
static bool selectSETCC(SDValue N, ISD::CondCode ExpectedCCVal, SDValue &Val,
SelectionDAG &DAG) {
static SDValue selectSETCC(SDValue N, ISD::CondCode ExpectedCCVal,
SelectionDAG &DAG) {
assert(ISD::isIntEqualitySetCC(ExpectedCCVal) &&
"Unexpected condition code!");

// We're looking for a setcc.
if (N->getOpcode() != ISD::SETCC)
return false;
return SDValue();

// Must be an equality comparison.
ISD::CondCode CCVal = cast<CondCodeSDNode>(N->getOperand(2))->get();
if (CCVal != ExpectedCCVal)
return false;
return SDValue();

SDValue LHS = N->getOperand(0);
SDValue RHS = N->getOperand(1);

if (!LHS.getValueType().isScalarInteger())
return false;
return SDValue();

// If the RHS side is 0, we don't need any extra instructions, return the LHS.
if (isNullConstant(RHS)) {
Val = LHS;
return true;
}
if (isNullConstant(RHS))
return LHS;

SDLoc DL(N);

if (auto *C = dyn_cast<ConstantSDNode>(RHS)) {
int64_t CVal = C->getSExtValue();
// If the RHS is -2048, we can use xori to produce 0 if the LHS is -2048 and
// non-zero otherwise.
if (CVal == -2048) {
Val = DAG.getNode(ISD::XOR, DL, N->getValueType(0), LHS,
DAG.getConstant(CVal, DL, N->getValueType(0)));
return true;
}
if (CVal == -2048)
return DAG.getNode(ISD::XOR, DL, N->getValueType(0), LHS,
DAG.getConstant(CVal, DL, N->getValueType(0)));
// If the RHS is [-2047,2048], we can use addi with -RHS to produce 0 if the
// LHS is equal to the RHS and non-zero otherwise.
if (isInt<12>(CVal) || CVal == 2048) {
Val = DAG.getNode(ISD::ADD, DL, N->getValueType(0), LHS,
DAG.getConstant(-CVal, DL, N->getValueType(0)));
return true;
}
if (isInt<12>(CVal) || CVal == 2048)
return DAG.getNode(ISD::ADD, DL, N->getValueType(0), LHS,
DAG.getConstant(-CVal, DL, N->getValueType(0)));
}

// If nothing else we can XOR the LHS and RHS to produce zero if they are
// equal and a non-zero value if they aren't.
Val = DAG.getNode(ISD::XOR, DL, N->getValueType(0), LHS, RHS);
return true;
return DAG.getNode(ISD::XOR, DL, N->getValueType(0), LHS, RHS);
}

// Transform `binOp (select cond, x, c0), c1` where `c0` and `c1` are constants
Expand Down Expand Up @@ -5904,9 +5897,14 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
// the SELECT. Performing the lowering here allows for greater control over
// when CZERO_{EQZ/NEZ} are used vs another branchless sequence or
// RISCVISD::SELECT_CC node (branch-based select).
<<<<<<< HEAD
if (Subtarget.hasStdExtZicond() && VT.isScalarInteger()) {
SDValue NewCondV;
if (selectSETCC(CondV, ISD::SETNE, NewCondV, DAG)) {
=======
if (Subtarget.hasStdExtZicond() && VT.isInteger()) {
if (SDValue NewCondV = selectSETCC(CondV, ISD::SETNE, DAG)) {
>>>>>>> 8f5aee536b99 ([RISCV] Make selectSETCC return SDValue instead of bool. NFC)
if (isNullConstant(FalseV))
// (select (riscv_setne c), t, 0) -> (czero_eqz t, c)
return DAG.getNode(RISCVISD::CZERO_EQZ, DL, VT, TrueV, NewCondV);
Expand All @@ -5920,7 +5918,7 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
DAG.getNode(RISCVISD::CZERO_EQZ, DL, VT, TrueV, NewCondV),
DAG.getNode(RISCVISD::CZERO_NEZ, DL, VT, FalseV, NewCondV));
}
if (selectSETCC(CondV, ISD::SETEQ, NewCondV, DAG)) {
if (SDValue NewCondV = selectSETCC(CondV, ISD::SETEQ, DAG)) {
if (isNullConstant(FalseV))
// (select (riscv_seteq c), t, 0) -> (czero_nez t, c)
return DAG.getNode(RISCVISD::CZERO_NEZ, DL, VT, TrueV, NewCondV);
Expand Down

0 comments on commit 2a33f47

Please sign in to comment.