Skip to content

Commit f9d4d54

Browse files
authored
[RISCV] Break the (czero_eqz x, (setne x, 0)) -> x combine into 2 combines. (#90428)
We can think of this as two separate combines (czero_eqz x, (setne y, 0)) -> (czero_eqz x, y) and (czero_eqz x, x) -> x Similary the (czero_nez x, (seteq x, 0)) -> x combine can be broken into (czero_nez x, (seteq y, 0)) -> (czero_eqz x, y) and (czero_eqz x, x) -> x isel already does the (czero_eqz x, (setne y, 0)) -> (czero_eqz x, y) and (czero_nez x, (seteq y, 0)) -> (czero_eqz x, y) combines, but doing them early could expose other opportunities.
1 parent 618adc7 commit f9d4d54

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16168,28 +16168,36 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
1616816168
return performSELECTCombine(N, DAG, Subtarget);
1616916169
case RISCVISD::CZERO_EQZ:
1617016170
case RISCVISD::CZERO_NEZ: {
16171-
SDValue LHS = N->getOperand(0);
16172-
SDValue RHS = N->getOperand(1);
16173-
// czero_eq X, (xor Y, 1) -> czero_ne X, Y if Y is 0 or 1.
16174-
// czero_ne X, (xor Y, 1) -> czero_eq X, Y if Y is 0 or 1.
16175-
if (RHS.getOpcode() == ISD::XOR && isOneConstant(RHS.getOperand(1))) {
16176-
SDValue Cond = RHS.getOperand(0);
16177-
APInt Mask = APInt::getBitsSetFrom(Cond.getValueSizeInBits(), 1);
16178-
if (DAG.MaskedValueIsZero(Cond, Mask)) {
16179-
unsigned NewOpc = N->getOpcode() == RISCVISD::CZERO_EQZ
16180-
? RISCVISD::CZERO_NEZ
16181-
: RISCVISD::CZERO_EQZ;
16182-
return DAG.getNode(NewOpc, SDLoc(N), N->getValueType(0), LHS, Cond);
16183-
}
16171+
SDValue Val = N->getOperand(0);
16172+
SDValue Cond = N->getOperand(1);
16173+
16174+
unsigned Opc = N->getOpcode();
16175+
16176+
// czero_eqz x, x -> x
16177+
if (Opc == RISCVISD::CZERO_EQZ && Val == Cond)
16178+
return Val;
16179+
16180+
unsigned InvOpc =
16181+
Opc == RISCVISD::CZERO_EQZ ? RISCVISD::CZERO_NEZ : RISCVISD::CZERO_EQZ;
16182+
16183+
// czero_eqz X, (xor Y, 1) -> czero_nez X, Y if Y is 0 or 1.
16184+
// czero_nez X, (xor Y, 1) -> czero_eqz X, Y if Y is 0 or 1.
16185+
if (Cond.getOpcode() == ISD::XOR && isOneConstant(Cond.getOperand(1))) {
16186+
SDValue NewCond = Cond.getOperand(0);
16187+
APInt Mask = APInt::getBitsSetFrom(NewCond.getValueSizeInBits(), 1);
16188+
if (DAG.MaskedValueIsZero(NewCond, Mask))
16189+
return DAG.getNode(InvOpc, SDLoc(N), N->getValueType(0), Val, NewCond);
16190+
}
16191+
// czero_eqz x, (setcc y, 0, ne) -> czero_eqz x, y
16192+
// czero_nez x, (setcc y, 0, ne) -> czero_nez x, y
16193+
// czero_eqz x, (setcc y, 0, eq) -> czero_nez x, y
16194+
// czero_nez x, (setcc y, 0, eq) -> czero_eqz x, y
16195+
if (Cond.getOpcode() == ISD::SETCC && isNullConstant(Cond.getOperand(1))) {
16196+
ISD::CondCode CCVal = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
16197+
if (ISD::isIntEqualitySetCC(CCVal))
16198+
return DAG.getNode(CCVal == ISD::SETNE ? Opc : InvOpc, SDLoc(N),
16199+
N->getValueType(0), Val, Cond.getOperand(0));
1618416200
}
16185-
// czero_eqz x, (setcc x, 0, ne) -> x
16186-
// czero_nez x, (setcc x, 0, eq) -> x
16187-
if (RHS.getOpcode() == ISD::SETCC && isNullConstant(RHS.getOperand(1)) &&
16188-
cast<CondCodeSDNode>(RHS.getOperand(2))->get() ==
16189-
(N->getOpcode() == RISCVISD::CZERO_EQZ ? ISD::CondCode::SETNE
16190-
: ISD::CondCode::SETEQ) &&
16191-
LHS == RHS.getOperand(0))
16192-
return LHS;
1619316201
return SDValue();
1619416202
}
1619516203
case RISCVISD::SELECT_CC: {

0 commit comments

Comments
 (0)