Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ucmp and scmp support to SelectionDAG #85822

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions llvm/include/llvm/CodeGen/ISDOpcodes.h
Expand Up @@ -676,6 +676,11 @@ enum NodeType {
UMIN,
UMAX,

/// [US]CMP - Three way integer comparison - returns -1, 0, or 1 if
/// Op1 < Op2, Op1 == Op2, Op1 > Op2, respectively.
SCMP,
UCMP,

/// Bitwise operators - logical and, logical or, logical xor.
AND,
OR,
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/CodeGen/TargetLowering.h
Expand Up @@ -5314,6 +5314,10 @@ class TargetLowering : public TargetLoweringBase {
/// method accepts integers as its arguments.
SDValue expandIntMINMAX(SDNode *Node, SelectionDAG &DAG) const;

/// Method for building the DAG expansion of ISD::[US]CMP. This
/// method accepts integers as its arguments.
SDValue expandCMP(SDNode *Node, SelectionDAG &DAG) const;

/// Method for building the DAG expansion of ISD::[US][ADD|SUB]SAT. This
/// method accepts integers as its arguments.
SDValue expandAddSubSat(SDNode *Node, SelectionDAG &DAG) const;
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
Expand Up @@ -3582,6 +3582,10 @@ bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
Results.push_back(Tmp1);
break;
}
case ISD::UCMP:
case ISD::SCMP:
// FIX: add logic here
break;
case ISD::FMINNUM:
case ISD::FMAXNUM: {
if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG))
Expand Down Expand Up @@ -5134,6 +5138,10 @@ void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
break;
}
case ISD::UCMP:
case ISD::SCMP:
// FIX: add logic here
break;
case ISD::UMUL_LOHI:
case ISD::SMUL_LOHI: {
// Promote to a multiply in a wider integer type.
Expand Down
19 changes: 19 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Expand Up @@ -6704,6 +6704,25 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
VT.getVectorElementType() == MVT::i1)
return getNode(ISD::XOR, DL, VT, N1, N2);
break;
case ISD::UCMP:
case ISD::SCMP:
// FIX: This cast is clearly wrong
assert(cast<SignedInt>(N1.getValueType) && "This operator should have signed types");
assert(VT.isInteger() && "This operator does not apply to FP types!");
assert(N1.getValueType() == N2.getValueType() && N1.getValueType() == VT &&
"Binary operator types must match");
// FIX: This logic should probably go in a separate function to deduplicate it from ucmp. Suggestions?
if (N1C > N2C) {
// FIX: All of these casts are horrible, I couldn't find the proper way to fold the constants in
return cast<ConstantInt>(1);
}
if (N1C == N2C) {
return cast<ConstantInt>(0);
}
if (N1C < N2C) {
return cast<ConstantInt>(-1);
}
break;
case ISD::MUL:
assert(VT.isInteger() && "This operator does not apply to FP types!");
assert(N1.getValueType() == N2.getValueType() &&
Expand Down
2 changes: 2 additions & 0 deletions llvm/unittests/Analysis/ValueTrackingTest.cpp
Expand Up @@ -891,6 +891,8 @@ TEST(ValueTracking, propagatesPoison) {
{true, "call i32 @llvm.smin.i32(i32 %x, i32 %y)", 0},
{true, "call i32 @llvm.umax.i32(i32 %x, i32 %y)", 0},
{true, "call i32 @llvm.umin.i32(i32 %x, i32 %y)", 0},
{true, "call i32 @llvm.scmp.i32.i32(i32 %x, i32 %y)", 0},
{true, "call i32 @llvm.ucmp.i32.i32(i32 %x, i32 %y)", 0},
{true, "call i32 @llvm.bitreverse.i32(i32 %x)", 0},
{true, "call i32 @llvm.bswap.i32(i32 %x)", 0},
{false, "call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %shamt)", 0},
Expand Down