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 scmp and ucmp to SelectionDAG.cpp #84681

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
1361e6c
add sthreecmp and uthreecmp intrinsics to LangRef
miguelraz Mar 1, 2024
4fa4ebe
add tablegen for sthreecmp and usthreecmp intrinsics
miguelraz Mar 1, 2024
0c82ff2
typo fix unsigned integer for uthreecmp
miguelraz Mar 1, 2024
d6c2c1f
add sthreecmp and uthreecmp intrinsics modeled after llvm.smax
miguelraz Mar 2, 2024
b23515e
add int_sthreecmp and int_uthreecmp based on int_smin
miguelraz Mar 2, 2024
1136f14
add [u]sthreecmp to ValueTrackingTest.cpp
miguelraz Mar 2, 2024
e3e2ebf
call three way cmp intrinsic [u]scmp in LangRef
miguelraz Mar 10, 2024
d3fbc96
only add minimum stuff to LangRef and Intrinsics.td for s/ucmp
miguelraz Mar 10, 2024
31341f2
add u/scmp result type overload over a fixed type in Intrinsics.td
miguelraz Mar 10, 2024
3e503f1
add VisitIntrinsicCall cases for Intrinsic::ucmp/scmp
miguelraz Mar 11, 2024
f1e2586
add proper type overloads for llvm.ucmp.i2.i32 and llvm.scmp.i2.i32 i…
miguelraz Mar 11, 2024
ec576e2
Update llvm/lib/IR/Verifier.cpp
miguelraz Mar 11, 2024
ba58a4e
move isVector check above else to catch dest as scalar but src as vector
miguelraz Mar 11, 2024
475c2ae
omit checks done by Intrinsics.td on u/scmp operating on int-likes
miguelraz Mar 11, 2024
0c558f1
Update llvm/docs/LangRef.rst
miguelraz Mar 11, 2024
b673ca1
Update llvm/docs/LangRef.rst
miguelraz Mar 11, 2024
5d0707d
Update llvm/docs/LangRef.rst
miguelraz Mar 11, 2024
a7bb3a7
Update llvm/lib/IR/Verifier.cpp
miguelraz Mar 11, 2024
d2ed755
Update llvm/lib/IR/Verifier.cpp
miguelraz Mar 11, 2024
f4a57b0
delete stray newline
miguelraz Mar 12, 2024
18b12ba
cast to vectors (not arrays!) in scmp Check
miguelraz Mar 12, 2024
44308f6
add Filecheck tests for Verifier.cpp u/scmp intrinsics
miguelraz Mar 12, 2024
b219c40
delete last unneded CHECK
miguelraz Mar 12, 2024
09bf470
make return value length more simple on scmp and ucmp
miguelraz Mar 12, 2024
ca67268
make return value length more simple on scmp and ucmp
miguelraz Mar 12, 2024
ca1d412
Update llvm/docs/LangRef.rst
miguelraz Mar 12, 2024
7ddf56b
Update llvm/docs/LangRef.rst
miguelraz Mar 12, 2024
54b32cc
Update llvm/docs/LangRef.rst
miguelraz Mar 14, 2024
331cae3
Update llvm/docs/LangRef.rst
miguelraz Mar 14, 2024
6210d0c
Update llvm/lib/IR/Verifier.cpp
miguelraz Mar 14, 2024
497567d
fix CodingStandards for var casenames and 4 spaces not tabs in FileCh…
miguelraz Mar 14, 2024
ffbdeb0
Update llvm/test/Verifier/intrinsic-cmp.ll
miguelraz Mar 14, 2024
c795763
align to 2 spaces
miguelraz Mar 14, 2024
08835b5
fixup unit tests for scmp intrinsic
miguelraz Mar 14, 2024
96b8bab
appease the formatting gods
miguelraz Mar 14, 2024
40dba14
appease the formatting gods, again
miguelraz Mar 14, 2024
0cc31dc
appease the formatting gods, again, again
miguelraz Mar 14, 2024
41d39f3
appease the formatting gods, again, again, again
miguelraz Mar 14, 2024
b5015dc
appease the FileCheck gods \o/
miguelraz Mar 15, 2024
1b0d354
phrasing nits for error messages
miguelraz Mar 15, 2024
979b2cf
update FileCheck unit tests with most recent errors
miguelraz Mar 15, 2024
dd61f63
freakin' formatting gods
miguelraz Mar 15, 2024
f17f652
[clang] Remove unused lambda capture. (#83550)
labrinea Mar 1, 2024
6274092
[Hexagon] Use LiveRegUnits (#84112)
AtariDreams Mar 8, 2024
416d5bb
Unrevert "[Clang][C++23] Implement P2448R2: Relaxing some constexpr r…
amykhuang Mar 13, 2024
f0fe7b1
first changes for ValueTrackingTest.cpp and SelectionDAG for s/ucmp
miguelraz Mar 10, 2024
346cc9b
add first attempt at SelectionDAG.cpp for u/scmp
miguelraz Mar 10, 2024
813a2d1
fix formatting on irrelevant lines
miguelraz Mar 10, 2024
cf90dba
fix typo in assert
miguelraz Mar 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14574,6 +14574,63 @@ The arguments (``%a`` and ``%b``) may be of any integer type or a vector with
integer element type. The argument types must match each other, and the return
type must match the argument type.

.. _int_scmp:

'``llvm.scmp.*``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""

This is an overloaded intrinsic. You can use ``@llvm.scmp`` on any
integer bit width or any vector of integer elements.

::

declare i2 @llvm.scmp.i2.i32(i32 %a, i32 %b)
declare <4 x i32> @llvm.scmp.v4i32.v4i32(<4 x i32> %a, <4 x i32> %b)

Overview:
"""""""""

Return ``-1`` if ``%a`` is signed less than ``%b``, ``0`` if they are equal, and
``1`` if ``%a`` is signed greater than ``%b``. Vector intrinsics operate on a per-element basis.

Arguments:
""""""""""

The arguments (``%a`` and ``%b``) may be of any integer type or a vector with
integer element type. The argument types must match each other, and the return
type must be at least as wide as ``i2``, to hold the three possible return values.

.. _int_ucmp:

'``llvm.ucmp.*``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Syntax:
"""""""

This is an overloaded intrinsic. You can use ``@llvm.ucmp`` on any
integer bit width or any vector of integer elements.

::

declare i2 @llvm.ucmp.i2.i32(i32 %a, i32 %b)
declare <4 x i32> @llvm.ucmp.v4i32.v4i32(<4 x i32> %a, <4 x i32> %b)

Overview:
"""""""""

Return ``-1`` if ``%a`` is unsigned less than ``%b``, ``0`` if they are equal, and
``1`` if ``%a`` is unsigned greater than ``%b``. Vector intrinsics operate on a per-element basis.

Arguments:
""""""""""

The arguments (``%a`` and ``%b``) may be of any integer type or a vector with
integer element type. The argument types must match each other, and the return
type must be at least as wide as ``i2``, to hold the three possible return values.

.. _int_memcpy:

Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/CodeGen/ISDOpcodes.h
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/Intrinsics.td
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,12 @@ def int_umax : DefaultAttrsIntrinsic<
def int_umin : DefaultAttrsIntrinsic<
[llvm_anyint_ty], [LLVMMatchType<0>, LLVMMatchType<0>],
[IntrNoMem, IntrSpeculatable, IntrWillReturn]>;
def int_scmp : DefaultAttrsIntrinsic<
[llvm_anyint_ty], [llvm_anyint_ty, LLVMMatchType<1>],
[IntrNoMem, IntrSpeculatable, IntrWillReturn]>;
def int_ucmp : DefaultAttrsIntrinsic<
[llvm_anyint_ty], [llvm_anyint_ty, LLVMMatchType<1>],
[IntrNoMem, IntrSpeculatable, IntrWillReturn]>;

//===------------------------- Memory Use Markers -------------------------===//
//
Expand Down
6 changes: 6 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6750,6 +6750,12 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
return getNode(ISD::AND, DL, VT, N1, N2);
break;
case ISD::UCMP:
case ISD::SCMP:
assert(VT.isInteger() && "This operator does not apply to FP types!");
assert(N1.getValueType() == N2.getValueType() &&
N1.getValueType() == VT && "Binary operator types must match");
break;
case ISD::FADD:
case ISD::FSUB:
case ISD::FMUL:
Expand Down
23 changes: 23 additions & 0 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5265,6 +5265,29 @@ void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
}
break;
}
case Intrinsic::ucmp:
case Intrinsic::scmp: {
Type *SrcTy = Call.getOperand(0)->getType();
Type *DestTy = Call.getType();

Check(DestTy->getScalarSizeInBits() >= 2,
"result type must be at least 2 bits wide", Call);

bool IsDestTypeVector = DestTy->isVectorTy();
Check(SrcTy->isVectorTy() == IsDestTypeVector,
"ucmp/scmp argument and result types must both be either vector or "
"scalar types",
Call);
if (IsDestTypeVector) {
auto SrcVecLen = cast<VectorType>(SrcTy)->getElementCount();
auto DestVecLen = cast<VectorType>(DestTy)->getElementCount();
Check(SrcVecLen == DestVecLen,
"return type and arguments must have the same number of "
"elements",
Call);
}
break;
}
case Intrinsic::coro_id: {
auto *InfoArg = Call.getArgOperand(3)->stripPointerCasts();
if (isa<ConstantPointerNull>(InfoArg))
Expand Down
22 changes: 22 additions & 0 deletions llvm/test/Verifier/intrinsic-cmp.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; RUN: not opt -S -passes=verify 2>&1 < %s | FileCheck %s

define void @matching_vector_lens(<4 x i32> %arg1, <4 x i32> %arg2) {
; CHECK: return type and arguments must have the same number of elements
%res = call <8 x i32> @llvm.scmp.v8i32.v4i32(<4 x i32> %arg1, <4 x i32> %arg2)
ret void
}

define void @result_len_is_at_least_2bits_wide(i32 %arg1, i32 %arg2) {
; CHECK: result type must be at least 2 bits wide
%res2 = call i1 @llvm.scmp.i1.i32(i32 %arg1, i32 %arg2)
ret void
}

define void @both_args_are_vecs_or_neither(<4 x i32> %arg1, i32 %arg2) {
; CHECK: ucmp/scmp argument and result types must both be either vector or scalar types
%res3 = call i2 @llvm.scmp.i2.v4i32(<4 x i32> %arg1, <4 x i32> %arg1)
; CHECK: ucmp/scmp argument and result types must both be either vector or scalar types
%res4 = call <4 x i32> @llvm.scmp.v4i32.i32(i32 %arg2, i32 %arg2)
ret void
}

2 changes: 2 additions & 0 deletions llvm/unittests/Analysis/ValueTrackingTest.cpp
Original file line number Diff line number Diff line change
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 %x, i32 %y)", 0},
{true, "call i32 @llvm.ucmp.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
Loading