Skip to content

Commit 2b5214b

Browse files
authored
IR: de-duplicate two CmpInst routines (NFC) (#116866)
De-duplicate the functions getSignedPredicate and getUnsignedPredicate, nearly identical versions of which were present in CmpInst and ICmpInst, creating less confusion.
1 parent 288f05f commit 2b5214b

File tree

8 files changed

+26
-88
lines changed

8 files changed

+26
-88
lines changed

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -935,43 +935,6 @@ class CmpInst : public Instruction {
935935
return isUnsigned(getPredicate());
936936
}
937937

938-
/// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert
939-
/// @returns the signed version of the unsigned predicate pred.
940-
/// return the signed version of a predicate
941-
static Predicate getSignedPredicate(Predicate pred);
942-
943-
/// For example, ULT->SLT, ULE->SLE, UGT->SGT, UGE->SGE, SLT->Failed assert
944-
/// @returns the signed version of the predicate for this instruction (which
945-
/// has to be an unsigned predicate).
946-
/// return the signed version of a predicate
947-
Predicate getSignedPredicate() {
948-
return getSignedPredicate(getPredicate());
949-
}
950-
951-
/// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert
952-
/// @returns the unsigned version of the signed predicate pred.
953-
static Predicate getUnsignedPredicate(Predicate pred);
954-
955-
/// For example, SLT->ULT, SLE->ULE, SGT->UGT, SGE->UGE, ULT->Failed assert
956-
/// @returns the unsigned version of the predicate for this instruction (which
957-
/// has to be an signed predicate).
958-
/// return the unsigned version of a predicate
959-
Predicate getUnsignedPredicate() {
960-
return getUnsignedPredicate(getPredicate());
961-
}
962-
963-
/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
964-
/// @returns the unsigned version of the signed predicate pred or
965-
/// the signed version of the signed predicate pred.
966-
static Predicate getFlippedSignednessPredicate(Predicate pred);
967-
968-
/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
969-
/// @returns the unsigned version of the signed predicate pred or
970-
/// the signed version of the signed predicate pred.
971-
Predicate getFlippedSignednessPredicate() {
972-
return getFlippedSignednessPredicate(getPredicate());
973-
}
974-
975938
/// This is just a convenience.
976939
/// Determine if this is true when both operands are the same.
977940
bool isTrueWhenEqual() const {

llvm/include/llvm/IR/Instructions.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,27 +1206,37 @@ class ICmpInst: public CmpInst {
12061206
/// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
12071207
/// @returns the predicate that would be the result if the operand were
12081208
/// regarded as signed.
1209-
/// Return the signed version of the predicate
1209+
/// Return the signed version of the predicate.
12101210
Predicate getSignedPredicate() const {
12111211
return getSignedPredicate(getPredicate());
12121212
}
12131213

1214-
/// This is a static version that you can use without an instruction.
1215-
/// Return the signed version of the predicate.
1214+
/// Return the signed version of the predicate: static variant.
12161215
static Predicate getSignedPredicate(Predicate pred);
12171216

12181217
/// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
12191218
/// @returns the predicate that would be the result if the operand were
12201219
/// regarded as unsigned.
1221-
/// Return the unsigned version of the predicate
1220+
/// Return the unsigned version of the predicate.
12221221
Predicate getUnsignedPredicate() const {
12231222
return getUnsignedPredicate(getPredicate());
12241223
}
12251224

1226-
/// This is a static version that you can use without an instruction.
1227-
/// Return the unsigned version of the predicate.
1225+
/// Return the unsigned version of the predicate: static variant.
12281226
static Predicate getUnsignedPredicate(Predicate pred);
12291227

1228+
/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
1229+
/// @returns the unsigned version of the signed predicate pred or
1230+
/// the signed version of the signed predicate pred.
1231+
static Predicate getFlippedSignednessPredicate(Predicate pred);
1232+
1233+
/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->Failed assert
1234+
/// @returns the unsigned version of the signed predicate pred or
1235+
/// the signed version of the signed predicate pred.
1236+
Predicate getFlippedSignednessPredicate() const {
1237+
return getFlippedSignednessPredicate(getPredicate());
1238+
}
1239+
12301240
void setSameSign(bool B = true) {
12311241
SubclassOptionalData = (SubclassOptionalData & ~SameSign) | (B * SameSign);
12321242
}

llvm/include/llvm/SandboxIR/Instruction.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,9 +2502,6 @@ class CmpInst : public SingleLLVMInstructionImpl<llvm::CmpInst> {
25022502
WRAP_BOTH(isEquality);
25032503
WRAP_BOTH(isRelational);
25042504
WRAP_BOTH(isSigned);
2505-
WRAP_BOTH(getSignedPredicate);
2506-
WRAP_BOTH(getUnsignedPredicate);
2507-
WRAP_BOTH(getFlippedSignednessPredicate);
25082505
WRAP_BOTH(isTrueWhenEqual);
25092506
WRAP_BOTH(isFalseWhenEqual);
25102507
WRAP_BOTH(isUnsigned);
@@ -2545,6 +2542,7 @@ class ICmpInst : public CmpInst {
25452542

25462543
WRAP_BOTH(getSignedPredicate);
25472544
WRAP_BOTH(getUnsignedPredicate);
2545+
WRAP_BOTH(getFlippedSignednessPredicate);
25482546
WRAP_BOTH(isEquality);
25492547
WRAP_MEMBER(isCommutative);
25502548
WRAP_MEMBER(isRelational);

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11874,7 +11874,7 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
1187411874
CmpInst::Predicate P2) {
1187511875
assert(P1 != P2 && "Handled earlier!");
1187611876
return CmpInst::isRelational(P2) &&
11877-
P1 == CmpInst::getFlippedSignednessPredicate(P2);
11877+
P1 == ICmpInst::getFlippedSignednessPredicate(P2);
1187811878
};
1187911879
if (IsSignFlippedPredicate(Pred, FoundPred)) {
1188011880
// Unsigned comparison is the same as signed comparison when both the

llvm/lib/CodeGen/ExpandMemCmp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ Value *MemCmpExpansion::getMemCmpOneBlock() {
686686
}
687687
// Generate new code and remove the original memcmp call and the user
688688
if (ICmpInst::isSigned(Pred)) {
689-
Value *Cmp = Builder.CreateICmp(CmpInst::getUnsignedPredicate(Pred),
689+
Value *Cmp = Builder.CreateICmp(ICmpInst::getUnsignedPredicate(Pred),
690690
Loads.Lhs, Loads.Rhs);
691691
auto *Result = NeedsZExt ? Builder.CreateZExt(Cmp, UI->getType()) : Cmp;
692692
UI->replaceAllUsesWith(Result);

llvm/lib/IR/ConstantRange.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
//
2121
//===----------------------------------------------------------------------===//
2222

23+
#include "llvm/IR/ConstantRange.h"
2324
#include "llvm/ADT/APInt.h"
2425
#include "llvm/Config/llvm-config.h"
25-
#include "llvm/IR/ConstantRange.h"
2626
#include "llvm/IR/Constants.h"
2727
#include "llvm/IR/InstrTypes.h"
2828
#include "llvm/IR/Instruction.h"
29+
#include "llvm/IR/Instructions.h"
2930
#include "llvm/IR/Intrinsics.h"
3031
#include "llvm/IR/Metadata.h"
3132
#include "llvm/IR/Operator.h"
@@ -191,7 +192,7 @@ CmpInst::Predicate ConstantRange::getEquivalentPredWithFlippedSignedness(
191192
"Only for relational integer predicates!");
192193

193194
CmpInst::Predicate FlippedSignednessPred =
194-
CmpInst::getFlippedSignednessPredicate(Pred);
195+
ICmpInst::getFlippedSignednessPredicate(Pred);
195196

196197
if (areInsensitiveToSignednessOfICmpPredicate(CR1, CR2))
197198
return FlippedSignednessPred;

llvm/lib/IR/Instructions.cpp

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3718,40 +3718,6 @@ CmpInst::Predicate CmpInst::getFlippedStrictnessPredicate(Predicate pred) {
37183718
llvm_unreachable("Unknown predicate!");
37193719
}
37203720

3721-
CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3722-
assert(CmpInst::isUnsigned(pred) && "Call only with unsigned predicates!");
3723-
3724-
switch (pred) {
3725-
default:
3726-
llvm_unreachable("Unknown predicate!");
3727-
case CmpInst::ICMP_ULT:
3728-
return CmpInst::ICMP_SLT;
3729-
case CmpInst::ICMP_ULE:
3730-
return CmpInst::ICMP_SLE;
3731-
case CmpInst::ICMP_UGT:
3732-
return CmpInst::ICMP_SGT;
3733-
case CmpInst::ICMP_UGE:
3734-
return CmpInst::ICMP_SGE;
3735-
}
3736-
}
3737-
3738-
CmpInst::Predicate CmpInst::getUnsignedPredicate(Predicate pred) {
3739-
assert(CmpInst::isSigned(pred) && "Call only with signed predicates!");
3740-
3741-
switch (pred) {
3742-
default:
3743-
llvm_unreachable("Unknown predicate!");
3744-
case CmpInst::ICMP_SLT:
3745-
return CmpInst::ICMP_ULT;
3746-
case CmpInst::ICMP_SLE:
3747-
return CmpInst::ICMP_ULE;
3748-
case CmpInst::ICMP_SGT:
3749-
return CmpInst::ICMP_UGT;
3750-
case CmpInst::ICMP_SGE:
3751-
return CmpInst::ICMP_UGE;
3752-
}
3753-
}
3754-
37553721
bool CmpInst::isUnsigned(Predicate predicate) {
37563722
switch (predicate) {
37573723
default: return false;
@@ -3867,7 +3833,7 @@ std::optional<bool> ICmpInst::compare(const KnownBits &LHS,
38673833
}
38683834
}
38693835

3870-
CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) {
3836+
CmpInst::Predicate ICmpInst::getFlippedSignednessPredicate(Predicate pred) {
38713837
assert(CmpInst::isRelational(pred) &&
38723838
"Call only with non-equality predicates!");
38733839

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ ConstraintTy ConstraintInfo::getConstraintForSolving(CmpInst::Predicate Pred,
767767
if (CmpInst::isSigned(Pred) &&
768768
isKnownNonNegative(Op0, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1) &&
769769
isKnownNonNegative(Op1, DL, /*Depth=*/MaxAnalysisRecursionDepth - 1))
770-
Pred = CmpInst::getUnsignedPredicate(Pred);
770+
Pred = ICmpInst::getUnsignedPredicate(Pred);
771771

772772
SmallVector<Value *> NewVariables;
773773
ConstraintTy R = getConstraint(Pred, Op0, Op1, NewVariables);
@@ -857,7 +857,7 @@ void ConstraintInfo::transferToOtherSystem(
857857
if (IsKnownNonNegative(B)) {
858858
addFact(CmpInst::ICMP_SGE, A, ConstantInt::get(B->getType(), 0), NumIn,
859859
NumOut, DFSInStack);
860-
addFact(CmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
860+
addFact(ICmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
861861
DFSInStack);
862862
}
863863
break;
@@ -867,7 +867,7 @@ void ConstraintInfo::transferToOtherSystem(
867867
if (IsKnownNonNegative(A)) {
868868
addFact(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), 0), NumIn,
869869
NumOut, DFSInStack);
870-
addFact(CmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
870+
addFact(ICmpInst::getSignedPredicate(Pred), A, B, NumIn, NumOut,
871871
DFSInStack);
872872
}
873873
break;

0 commit comments

Comments
 (0)