Skip to content

Commit 73f4b35

Browse files
nikicakiramenai
authored andcommitted
[IR] Add helper for comparing KnownBits with IR predicate (NFC) (#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 106a9cd commit 73f4b35

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;
@@ -1279,6 +1280,11 @@ class ICmpInst: public CmpInst {
12791280
static bool compare(const APInt &LHS, const APInt &RHS,
12801281
ICmpInst::Predicate Pred);
12811282

1283+
/// Return result of `LHS Pred RHS`, if it can be determined from the
1284+
/// KnownBits. Otherwise return nullopt.
1285+
static std::optional<bool> compare(const KnownBits &LHS, const KnownBits &RHS,
1286+
ICmpInst::Predicate Pred);
1287+
12821288
// Methods for support type inquiry through isa, cast, and dyn_cast:
12831289
static bool classof(const Instruction *I) {
12841290
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
@@ -4369,40 +4369,7 @@ bool CombinerHelper::matchICmpToTrueFalseKnownBits(MachineInstr &MI,
43694369

43704370
if (!KnownVal) {
43714371
auto KnownLHS = KB->getKnownBits(MI.getOperand(2).getReg());
4372-
switch (Pred) {
4373-
default:
4374-
llvm_unreachable("Unexpected G_ICMP predicate?");
4375-
case CmpInst::ICMP_EQ:
4376-
KnownVal = KnownBits::eq(KnownLHS, KnownRHS);
4377-
break;
4378-
case CmpInst::ICMP_NE:
4379-
KnownVal = KnownBits::ne(KnownLHS, KnownRHS);
4380-
break;
4381-
case CmpInst::ICMP_SGE:
4382-
KnownVal = KnownBits::sge(KnownLHS, KnownRHS);
4383-
break;
4384-
case CmpInst::ICMP_SGT:
4385-
KnownVal = KnownBits::sgt(KnownLHS, KnownRHS);
4386-
break;
4387-
case CmpInst::ICMP_SLE:
4388-
KnownVal = KnownBits::sle(KnownLHS, KnownRHS);
4389-
break;
4390-
case CmpInst::ICMP_SLT:
4391-
KnownVal = KnownBits::slt(KnownLHS, KnownRHS);
4392-
break;
4393-
case CmpInst::ICMP_UGE:
4394-
KnownVal = KnownBits::uge(KnownLHS, KnownRHS);
4395-
break;
4396-
case CmpInst::ICMP_UGT:
4397-
KnownVal = KnownBits::ugt(KnownLHS, KnownRHS);
4398-
break;
4399-
case CmpInst::ICMP_ULE:
4400-
KnownVal = KnownBits::ule(KnownLHS, KnownRHS);
4401-
break;
4402-
case CmpInst::ICMP_ULT:
4403-
KnownVal = KnownBits::ult(KnownLHS, KnownRHS);
4404-
break;
4405-
}
4372+
KnownVal = ICmpInst::compare(KnownLHS, KnownRHS, Pred);
44064373
}
44074374

44084375
if (!KnownVal)

llvm/lib/IR/Instructions.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "llvm/Support/Casting.h"
4040
#include "llvm/Support/CheckedArithmetic.h"
4141
#include "llvm/Support/ErrorHandling.h"
42+
#include "llvm/Support/KnownBits.h"
4243
#include "llvm/Support/MathExtras.h"
4344
#include "llvm/Support/ModRef.h"
4445
#include "llvm/Support/TypeSize.h"
@@ -3826,6 +3827,35 @@ bool FCmpInst::compare(const APFloat &LHS, const APFloat &RHS,
38263827
}
38273828
}
38283829

3830+
std::optional<bool> ICmpInst::compare(const KnownBits &LHS,
3831+
const KnownBits &RHS,
3832+
ICmpInst::Predicate Pred) {
3833+
switch (Pred) {
3834+
case ICmpInst::ICMP_EQ:
3835+
return KnownBits::eq(LHS, RHS);
3836+
case ICmpInst::ICMP_NE:
3837+
return KnownBits::ne(LHS, RHS);
3838+
case ICmpInst::ICMP_UGE:
3839+
return KnownBits::uge(LHS, RHS);
3840+
case ICmpInst::ICMP_UGT:
3841+
return KnownBits::ugt(LHS, RHS);
3842+
case ICmpInst::ICMP_ULE:
3843+
return KnownBits::ule(LHS, RHS);
3844+
case ICmpInst::ICMP_ULT:
3845+
return KnownBits::ult(LHS, RHS);
3846+
case ICmpInst::ICMP_SGE:
3847+
return KnownBits::sge(LHS, RHS);
3848+
case ICmpInst::ICMP_SGT:
3849+
return KnownBits::sgt(LHS, RHS);
3850+
case ICmpInst::ICMP_SLE:
3851+
return KnownBits::sle(LHS, RHS);
3852+
case ICmpInst::ICMP_SLT:
3853+
return KnownBits::slt(LHS, RHS);
3854+
default:
3855+
llvm_unreachable("Unexpected non-integer predicate.");
3856+
}
3857+
}
3858+
38293859
CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) {
38303860
assert(CmpInst::isRelational(pred) &&
38313861
"Call only with non-equality predicates!");

0 commit comments

Comments
 (0)