Skip to content

Commit 63fb980

Browse files
authored
[IR] Add helper for comparing KnownBits with IR predicate (NFC) (llvm#115878)
Add `ICmpInst::compare()` overload accepting `KnownBits`, similar to the existing one accepting `APInt`. This is not directly part of KnownBits (or APInt) for layering reasons.
1 parent 8a1ca6c commit 63fb980

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

llvm/include/llvm/IR/Instructions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class APInt;
4848
class BasicBlock;
4949
class ConstantInt;
5050
class DataLayout;
51+
struct KnownBits;
5152
class StringRef;
5253
class Type;
5354
class Value;
@@ -1305,6 +1306,11 @@ class ICmpInst: public CmpInst {
13051306
static bool compare(const APInt &LHS, const APInt &RHS,
13061307
ICmpInst::Predicate Pred);
13071308

1309+
/// Return result of `LHS Pred RHS`, if it can be determined from the
1310+
/// KnownBits. Otherwise return nullopt.
1311+
static std::optional<bool> compare(const KnownBits &LHS, const KnownBits &RHS,
1312+
ICmpInst::Predicate Pred);
1313+
13081314
// Methods for support type inquiry through isa, cast, and dyn_cast:
13091315
static bool classof(const Instruction *I) {
13101316
return I->getOpcode() == Instruction::ICmp;

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4442,40 +4442,7 @@ bool CombinerHelper::matchICmpToTrueFalseKnownBits(MachineInstr &MI,
44424442

44434443
if (!KnownVal) {
44444444
auto KnownLHS = KB->getKnownBits(MI.getOperand(2).getReg());
4445-
switch (Pred) {
4446-
default:
4447-
llvm_unreachable("Unexpected G_ICMP predicate?");
4448-
case CmpInst::ICMP_EQ:
4449-
KnownVal = KnownBits::eq(KnownLHS, KnownRHS);
4450-
break;
4451-
case CmpInst::ICMP_NE:
4452-
KnownVal = KnownBits::ne(KnownLHS, KnownRHS);
4453-
break;
4454-
case CmpInst::ICMP_SGE:
4455-
KnownVal = KnownBits::sge(KnownLHS, KnownRHS);
4456-
break;
4457-
case CmpInst::ICMP_SGT:
4458-
KnownVal = KnownBits::sgt(KnownLHS, KnownRHS);
4459-
break;
4460-
case CmpInst::ICMP_SLE:
4461-
KnownVal = KnownBits::sle(KnownLHS, KnownRHS);
4462-
break;
4463-
case CmpInst::ICMP_SLT:
4464-
KnownVal = KnownBits::slt(KnownLHS, KnownRHS);
4465-
break;
4466-
case CmpInst::ICMP_UGE:
4467-
KnownVal = KnownBits::uge(KnownLHS, KnownRHS);
4468-
break;
4469-
case CmpInst::ICMP_UGT:
4470-
KnownVal = KnownBits::ugt(KnownLHS, KnownRHS);
4471-
break;
4472-
case CmpInst::ICMP_ULE:
4473-
KnownVal = KnownBits::ule(KnownLHS, KnownRHS);
4474-
break;
4475-
case CmpInst::ICMP_ULT:
4476-
KnownVal = KnownBits::ult(KnownLHS, KnownRHS);
4477-
break;
4478-
}
4445+
KnownVal = ICmpInst::compare(KnownLHS, KnownRHS, Pred);
44794446
}
44804447

44814448
if (!KnownVal)

llvm/lib/IR/Instructions.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/Support/Casting.h"
4141
#include "llvm/Support/CheckedArithmetic.h"
4242
#include "llvm/Support/ErrorHandling.h"
43+
#include "llvm/Support/KnownBits.h"
4344
#include "llvm/Support/MathExtras.h"
4445
#include "llvm/Support/ModRef.h"
4546
#include "llvm/Support/TypeSize.h"
@@ -3837,6 +3838,35 @@ bool FCmpInst::compare(const APFloat &LHS, const APFloat &RHS,
38373838
}
38383839
}
38393840

3841+
std::optional<bool> ICmpInst::compare(const KnownBits &LHS,
3842+
const KnownBits &RHS,
3843+
ICmpInst::Predicate Pred) {
3844+
switch (Pred) {
3845+
case ICmpInst::ICMP_EQ:
3846+
return KnownBits::eq(LHS, RHS);
3847+
case ICmpInst::ICMP_NE:
3848+
return KnownBits::ne(LHS, RHS);
3849+
case ICmpInst::ICMP_UGE:
3850+
return KnownBits::uge(LHS, RHS);
3851+
case ICmpInst::ICMP_UGT:
3852+
return KnownBits::ugt(LHS, RHS);
3853+
case ICmpInst::ICMP_ULE:
3854+
return KnownBits::ule(LHS, RHS);
3855+
case ICmpInst::ICMP_ULT:
3856+
return KnownBits::ult(LHS, RHS);
3857+
case ICmpInst::ICMP_SGE:
3858+
return KnownBits::sge(LHS, RHS);
3859+
case ICmpInst::ICMP_SGT:
3860+
return KnownBits::sgt(LHS, RHS);
3861+
case ICmpInst::ICMP_SLE:
3862+
return KnownBits::sle(LHS, RHS);
3863+
case ICmpInst::ICMP_SLT:
3864+
return KnownBits::slt(LHS, RHS);
3865+
default:
3866+
llvm_unreachable("Unexpected non-integer predicate.");
3867+
}
3868+
}
3869+
38403870
CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) {
38413871
assert(CmpInst::isRelational(pred) &&
38423872
"Call only with non-equality predicates!");

0 commit comments

Comments
 (0)