Skip to content

Commit 6018820

Browse files
authored
[NVPTX] Fix lowering of i1 SETCC (llvm#115035)
Add DAG legalization support for expanding i1 SETCC nodes using appropriate logical operations to simulate integer comparisons. Use these expansions to handle i1 SETCC in NVPTX. fixes llvm#58428 and llvm#57405
1 parent e84f79e commit 6018820

File tree

5 files changed

+370
-14
lines changed

5 files changed

+370
-14
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18763,9 +18763,13 @@ SDValue DAGCombiner::rebuildSetCC(SDValue N) {
1876318763
EVT SetCCVT = N.getValueType();
1876418764
if (LegalTypes)
1876518765
SetCCVT = getSetCCResultType(SetCCVT);
18766-
// Replace the uses of XOR with SETCC
18767-
return DAG.getSetCC(SDLoc(N), SetCCVT, Op0, Op1,
18768-
Equal ? ISD::SETEQ : ISD::SETNE);
18766+
// Replace the uses of XOR with SETCC. Note, avoid this transformation if
18767+
// it would introduce illegal operations post-legalization as this can
18768+
// result in infinite looping between converting xor->setcc here, and
18769+
// expanding setcc->xor in LegalizeSetCCCondCode if requested.
18770+
const ISD::CondCode CC = Equal ? ISD::SETEQ : ISD::SETNE;
18771+
if (!LegalOperations || TLI.isCondCodeLegal(CC, Op0.getSimpleValueType()))
18772+
return DAG.getSetCC(SDLoc(N), SetCCVT, Op0, Op1, CC);
1876918773
}
1877018774
}
1877118775

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11889,6 +11889,47 @@ bool TargetLowering::LegalizeSetCCCondCode(SelectionDAG &DAG, EVT VT,
1188911889
return true;
1189011890
}
1189111891

11892+
// Special case: expand i1 comparisons using logical operations.
11893+
if (OpVT == MVT::i1) {
11894+
SDValue Ret;
11895+
switch (CCCode) {
11896+
default:
11897+
llvm_unreachable("Unknown integer setcc!");
11898+
case ISD::SETEQ: // X == Y --> ~(X ^ Y)
11899+
Ret = DAG.getNOT(dl, DAG.getNode(ISD::XOR, dl, MVT::i1, LHS, RHS),
11900+
MVT::i1);
11901+
break;
11902+
case ISD::SETNE: // X != Y --> (X ^ Y)
11903+
Ret = DAG.getNode(ISD::XOR, dl, MVT::i1, LHS, RHS);
11904+
break;
11905+
case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> ~X & Y
11906+
case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> ~X & Y
11907+
Ret = DAG.getNode(ISD::AND, dl, MVT::i1, RHS,
11908+
DAG.getNOT(dl, LHS, MVT::i1));
11909+
break;
11910+
case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> ~Y & X
11911+
case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> ~Y & X
11912+
Ret = DAG.getNode(ISD::AND, dl, MVT::i1, LHS,
11913+
DAG.getNOT(dl, RHS, MVT::i1));
11914+
break;
11915+
case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> ~X | Y
11916+
case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> ~X | Y
11917+
Ret = DAG.getNode(ISD::OR, dl, MVT::i1, RHS,
11918+
DAG.getNOT(dl, LHS, MVT::i1));
11919+
break;
11920+
case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> ~Y | X
11921+
case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> ~Y | X
11922+
Ret = DAG.getNode(ISD::OR, dl, MVT::i1, LHS,
11923+
DAG.getNOT(dl, RHS, MVT::i1));
11924+
break;
11925+
}
11926+
11927+
LHS = DAG.getZExtOrTrunc(Ret, dl, VT);
11928+
RHS = SDValue();
11929+
CC = SDValue();
11930+
return true;
11931+
}
11932+
1189211933
ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
1189311934
unsigned Opc = 0;
1189411935
switch (CCCode) {

llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,11 @@ NVPTXTargetLowering::NVPTXTargetLowering(const NVPTXTargetMachine &TM,
667667
setTruncStoreAction(VT, MVT::i1, Expand);
668668
}
669669

670+
setCondCodeAction({ISD::SETNE, ISD::SETEQ, ISD::SETUGE, ISD::SETULE,
671+
ISD::SETUGT, ISD::SETULT, ISD::SETGT, ISD::SETLT,
672+
ISD::SETGE, ISD::SETLE},
673+
MVT::i1, Expand);
674+
670675
// expand extload of vector of integers.
671676
setLoadExtAction({ISD::EXTLOAD, ISD::SEXTLOAD, ISD::ZEXTLOAD}, MVT::v2i16,
672677
MVT::v2i8, Expand);

llvm/lib/Target/NVPTX/NVPTXInstrInfo.td

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,17 +2120,6 @@ defm : ISET_FORMAT_UNSIGNED<setule, CmpLE>;
21202120
defm : ISET_FORMAT_UNSIGNED<setueq, CmpEQ>;
21212121
defm : ISET_FORMAT_UNSIGNED<setune, CmpNE>;
21222122

2123-
// i1 compares
2124-
def : Pat<(setne Int1Regs:$a, Int1Regs:$b),
2125-
(XORb1rr Int1Regs:$a, Int1Regs:$b)>;
2126-
def : Pat<(setune Int1Regs:$a, Int1Regs:$b),
2127-
(XORb1rr Int1Regs:$a, Int1Regs:$b)>;
2128-
2129-
def : Pat<(seteq Int1Regs:$a, Int1Regs:$b),
2130-
(NOT1 (XORb1rr Int1Regs:$a, Int1Regs:$b))>;
2131-
def : Pat<(setueq Int1Regs:$a, Int1Regs:$b),
2132-
(NOT1 (XORb1rr Int1Regs:$a, Int1Regs:$b))>;
2133-
21342123
// comparisons of i8 extracted with BFE as i32
21352124
// It's faster to do comparison directly on i32 extracted by BFE,
21362125
// instead of the long conversion and sign extending.

0 commit comments

Comments
 (0)