Skip to content

Commit d017cfc

Browse files
committed
CmpPredicate: address review
1 parent 1a31075 commit d017cfc

File tree

7 files changed

+59
-90
lines changed

7 files changed

+59
-90
lines changed

llvm/include/llvm/IR/CmpPredicate.h

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
namespace llvm {
1919
/// An abstraction over a floating-point predicate, and a pack of an integer
20-
/// predicate with samesign information. Functions in ICmpInst construct and
21-
/// return this type in place of a Predicate. It is also implictly constructed
22-
/// with a Predicate, dropping samesign information.
20+
/// predicate with samesign information. Some functions in ICmpInst construct
21+
/// and return this type in place of a Predicate. It is also implictly
22+
/// constructed with a Predicate, dropping samesign information.
2323
class CmpPredicate {
2424
CmpInst::Predicate Pred;
2525
bool HasSameSign;
@@ -30,26 +30,16 @@ class CmpPredicate {
3030
assert(!HasSameSign || CmpInst::isIntPredicate(Pred));
3131
}
3232

33-
inline operator CmpInst::Predicate() const { return Pred; }
33+
operator CmpInst::Predicate() const { return Pred; }
3434

35-
inline bool hasSameSign() const { return HasSameSign; }
35+
bool hasSameSign() const { return HasSameSign; }
3636

3737
static std::optional<CmpPredicate> getMatching(CmpPredicate A,
38-
CmpPredicate B) {
39-
if (A.Pred == B.Pred)
40-
return A.HasSameSign == B.HasSameSign ? A : CmpPredicate(A.Pred);
41-
if (A.HasSameSign &&
42-
A.Pred == CmpInst::getFlippedSignednessPredicate(B.Pred))
43-
return B.Pred;
44-
if (B.HasSameSign &&
45-
B.Pred == CmpInst::getFlippedSignednessPredicate(A.Pred))
46-
return A.Pred;
47-
return {};
48-
}
38+
CmpPredicate B);
4939

50-
inline bool operator==(CmpInst::Predicate P) const { return Pred == P; }
40+
bool operator==(CmpInst::Predicate P) const { return Pred == P; }
5141

52-
inline bool operator==(CmpPredicate) const = delete;
42+
bool operator==(CmpPredicate) const = delete;
5343
};
5444
} // namespace llvm
5545

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -728,24 +728,6 @@ class CmpInst : public Instruction {
728728
InsertPosition InsertBefore = nullptr,
729729
Instruction *FlagsSource = nullptr);
730730

731-
/// Return the signed version of the predicate: variant that operates on
732-
/// Predicate; used by the corresponding function in ICmpInst, to operate with
733-
/// CmpPredicate.
734-
static Predicate getSignedPredicate(Predicate Pred);
735-
736-
/// Return the unsigned version of the predicate: variant that operates on
737-
/// Predicate; used by the corresponding function in ICmpInst, to operate with
738-
/// CmpPredicate.
739-
static Predicate getUnsignedPredicate(Predicate Pred);
740-
741-
/// Return the unsigned version of the signed predicate pred or the signed
742-
/// version of the signed predicate pred: variant that operates on Predicate;
743-
/// used by the corresponding function in ICmpInst, to operate with
744-
/// CmpPredicate.
745-
static Predicate getFlippedSignednessPredicate(Predicate Pred);
746-
747-
friend class CmpPredicate;
748-
749731
public:
750732
// allocate space for exactly two operands
751733
void *operator new(size_t S) { return User::operator new(S, AllocMarker); }

llvm/include/llvm/IR/Instructions.h

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,56 +1226,44 @@ class ICmpInst: public CmpInst {
12261226
return {getSwappedPredicate(Pred), Pred.hasSameSign()};
12271227
}
12281228

1229-
/// @returns the swapped predicate along with samesign information.
1230-
CmpPredicate getSwappedCmpPredicate() const {
1229+
/// @returns the swapped predicate.
1230+
Predicate getSwappedCmpPredicate() const {
12311231
return getSwappedPredicate(getCmpPredicate());
12321232
}
12331233

12341234
/// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
12351235
/// @returns the predicate that would be the result if the operand were
12361236
/// regarded as signed.
1237-
/// Return the signed version of the predicate along with samesign
1238-
/// information.
1239-
CmpPredicate getSignedPredicate() const {
1240-
return getSignedPredicate(getCmpPredicate());
1237+
/// Return the signed version of the predicate.
1238+
Predicate getSignedPredicate() const {
1239+
return getSignedPredicate(getPredicate());
12411240
}
12421241

1243-
/// Return the signed version of the predicate along with samesign
1244-
/// information: static variant.
1245-
static CmpPredicate getSignedPredicate(CmpPredicate Pred) {
1246-
return {CmpInst::getSignedPredicate(Pred), Pred.hasSameSign()};
1247-
}
1242+
/// Return the signed version of the predicate: static variant.
1243+
static Predicate getSignedPredicate(Predicate Pred);
12481244

12491245
/// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
12501246
/// @returns the predicate that would be the result if the operand were
12511247
/// regarded as unsigned.
1252-
/// Return the unsigned version of the predicate along with samesign
1253-
/// information.
1254-
CmpPredicate getUnsignedPredicate() const {
1255-
return getUnsignedPredicate(getCmpPredicate());
1248+
/// Return the unsigned version of the predicate.
1249+
Predicate getUnsignedPredicate() const {
1250+
return getUnsignedPredicate(getPredicate());
12561251
}
12571252

1258-
/// Return the unsigned version of the predicate along with samesign
1259-
/// information: static variant.
1260-
static CmpPredicate getUnsignedPredicate(CmpPredicate Pred) {
1261-
return {CmpInst::getUnsignedPredicate(Pred), Pred.hasSameSign()};
1262-
}
1253+
/// Return the unsigned version of the predicate: static variant.
1254+
static Predicate getUnsignedPredicate(Predicate Pred);
12631255

12641256
/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ
12651257
/// @returns the unsigned version of the signed predicate pred or
1266-
/// the signed version of the signed predicate pred, along with
1267-
/// samesign information.
1258+
/// the signed version of the signed predicate pred.
12681259
/// Static variant.
1269-
static CmpPredicate getFlippedSignednessPredicate(CmpPredicate Pred) {
1270-
return {CmpInst::getFlippedSignednessPredicate(Pred), Pred.hasSameSign()};
1271-
}
1260+
static Predicate getFlippedSignednessPredicate(Predicate Pred);
12721261

12731262
/// For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ
12741263
/// @returns the unsigned version of the signed predicate pred or
1275-
/// the signed version of the signed predicate pred, along with
1276-
/// samesign information.
1277-
CmpPredicate getFlippedSignednessPredicate() const {
1278-
return getFlippedSignednessPredicate(getCmpPredicate());
1264+
/// the signed version of the signed predicate pred.
1265+
Predicate getFlippedSignednessPredicate() const {
1266+
return getFlippedSignednessPredicate(getPredicate());
12791267
}
12801268

12811269
void setSameSign(bool B = true) {

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9104,7 +9104,7 @@ bool llvm::matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P,
91049104
}
91059105

91069106
/// Return true if "icmp Pred LHS RHS" is always true.
9107-
static bool isTruePredicate(CmpPredicate Pred, const Value *LHS,
9107+
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
91089108
const Value *RHS) {
91099109
if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
91109110
return true;
@@ -9186,8 +9186,8 @@ static bool isTruePredicate(CmpPredicate Pred, const Value *LHS,
91869186
/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
91879187
/// ALHS ARHS" is true. Otherwise, return std::nullopt.
91889188
static std::optional<bool>
9189-
isImpliedCondOperands(CmpPredicate Pred, const Value *ALHS, const Value *ARHS,
9190-
const Value *BLHS, const Value *BRHS) {
9189+
isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
9190+
const Value *ARHS, const Value *BLHS, const Value *BRHS) {
91919191
switch (Pred) {
91929192
default:
91939193
return std::nullopt;
@@ -9256,16 +9256,18 @@ static std::optional<bool> isImpliedCondCommonOperandWithCR(
92569256
/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
92579257
/// is true. Return false if LHS implies RHS is false. Otherwise, return
92589258
/// std::nullopt if we can't infer anything.
9259-
static std::optional<bool>
9260-
isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
9261-
const Value *R1, const DataLayout &DL, bool LHSIsTrue) {
9259+
static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
9260+
CmpInst::Predicate RPred,
9261+
const Value *R0, const Value *R1,
9262+
const DataLayout &DL,
9263+
bool LHSIsTrue) {
92629264
Value *L0 = LHS->getOperand(0);
92639265
Value *L1 = LHS->getOperand(1);
92649266

92659267
// The rest of the logic assumes the LHS condition is true. If that's not the
92669268
// case, invert the predicate to make it so.
9267-
CmpPredicate LPred =
9268-
LHSIsTrue ? LHS->getCmpPredicate() : LHS->getInverseCmpPredicate();
9269+
CmpInst::Predicate LPred =
9270+
LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
92699271

92709272
// We can have non-canonical operands, so try to normalize any common operand
92719273
// to L0/R0.
@@ -9342,8 +9344,8 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
93429344
match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
93439345
return CmpPredicate::getMatching(LPred, RPred).has_value();
93449346

9345-
if (auto P = CmpPredicate::getMatching(LPred, RPred))
9346-
return isImpliedCondOperands(*P, L0, L1, R0, R1);
9347+
if (LPred == RPred)
9348+
return isImpliedCondOperands(LPred, L0, L1, R0, R1);
93479349

93489350
return std::nullopt;
93499351
}

llvm/lib/IR/Instructions.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3581,7 +3581,7 @@ raw_ostream &llvm::operator<<(raw_ostream &OS, CmpInst::Predicate Pred) {
35813581
return OS;
35823582
}
35833583

3584-
ICmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3584+
ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
35853585
switch (pred) {
35863586
default: llvm_unreachable("Unknown icmp predicate!");
35873587
case ICMP_EQ: case ICMP_NE:
@@ -3594,7 +3594,7 @@ ICmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
35943594
}
35953595
}
35963596

3597-
ICmpInst::Predicate CmpInst::getUnsignedPredicate(Predicate pred) {
3597+
ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
35983598
switch (pred) {
35993599
default: llvm_unreachable("Unknown icmp predicate!");
36003600
case ICMP_EQ: case ICMP_NE:
@@ -3841,7 +3841,7 @@ std::optional<bool> ICmpInst::compare(const KnownBits &LHS,
38413841
}
38423842
}
38433843

3844-
CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) {
3844+
CmpInst::Predicate ICmpInst::getFlippedSignednessPredicate(Predicate pred) {
38453845
if (CmpInst::isEquality(pred))
38463846
return pred;
38473847
if (isSigned(pred))
@@ -3915,6 +3915,22 @@ bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
39153915
return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
39163916
}
39173917

3918+
//===----------------------------------------------------------------------===//
3919+
// CmpPredicate Implementation
3920+
//===----------------------------------------------------------------------===//
3921+
std::optional<CmpPredicate> CmpPredicate::getMatching(CmpPredicate A,
3922+
CmpPredicate B) {
3923+
if (A.Pred == B.Pred)
3924+
return A.HasSameSign == B.HasSameSign ? A : CmpPredicate(A.Pred);
3925+
if (A.HasSameSign &&
3926+
A.Pred == ICmpInst::getFlippedSignednessPredicate(B.Pred))
3927+
return B.Pred;
3928+
if (B.HasSameSign &&
3929+
B.Pred == ICmpInst::getFlippedSignednessPredicate(A.Pred))
3930+
return A.Pred;
3931+
return {};
3932+
}
3933+
39183934
//===----------------------------------------------------------------------===//
39193935
// SwitchInst Implementation
39203936
//===----------------------------------------------------------------------===//

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Value *InstCombinerImpl::insertRangeTest(Value *V, const APInt &Lo,
5959

6060
// V >= Min && V < Hi --> V < Hi
6161
// V < Min || V >= Hi --> V >= Hi
62-
CmpPredicate Pred = Inside ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
62+
ICmpInst::Predicate Pred = Inside ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_UGE;
6363
if (isSigned ? Lo.isMinSignedValue() : Lo.isMinValue()) {
6464
Pred = isSigned ? ICmpInst::getSignedPredicate(Pred) : Pred;
6565
return Builder.CreateICmp(Pred, V, ConstantInt::get(Ty, Hi));

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5838,17 +5838,8 @@ define void @foo(i32 %i0, i32 %i1) {
58385838
checkCommonPredicates(ICmp, LLVMICmp);
58395839
EXPECT_EQ(ICmp->isSigned(), LLVMICmp->isSigned());
58405840
EXPECT_EQ(ICmp->isUnsigned(), LLVMICmp->isUnsigned());
5841-
EXPECT_EQ(
5842-
static_cast<llvm::CmpInst::Predicate>(ICmp->getSignedPredicate()),
5843-
static_cast<llvm::CmpInst::Predicate>(LLVMICmp->getSignedPredicate()));
5844-
EXPECT_EQ(ICmp->getSignedPredicate().hasSameSign(),
5845-
LLVMICmp->getSignedPredicate().hasSameSign());
5846-
EXPECT_EQ(
5847-
static_cast<llvm::CmpInst::Predicate>(ICmp->getUnsignedPredicate()),
5848-
static_cast<llvm::CmpInst::Predicate>(
5849-
LLVMICmp->getUnsignedPredicate()));
5850-
EXPECT_EQ(ICmp->getUnsignedPredicate().hasSameSign(),
5851-
LLVMICmp->getUnsignedPredicate().hasSameSign());
5841+
EXPECT_EQ(ICmp->getSignedPredicate(), LLVMICmp->getSignedPredicate());
5842+
EXPECT_EQ(ICmp->getUnsignedPredicate(), LLVMICmp->getUnsignedPredicate());
58525843
}
58535844
auto *NewCmp =
58545845
sandboxir::CmpInst::create(llvm::CmpInst::ICMP_ULE, F.getArg(0),

0 commit comments

Comments
 (0)