Skip to content

[IR] Add helper for comparing KnownBits with IR predicate (NFC) #115878

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

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class APInt;
class BasicBlock;
class ConstantInt;
class DataLayout;
struct KnownBits;
class StringRef;
class Type;
class Value;
Expand Down Expand Up @@ -1305,6 +1306,11 @@ class ICmpInst: public CmpInst {
static bool compare(const APInt &LHS, const APInt &RHS,
ICmpInst::Predicate Pred);

/// Return result of `LHS Pred RHS`, if it can be determined from the
/// KnownBits. Otherwise return nullopt.
static std::optional<bool> compare(const KnownBits &LHS, const KnownBits &RHS,
ICmpInst::Predicate Pred);

// Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Instruction *I) {
return I->getOpcode() == Instruction::ICmp;
Expand Down
35 changes: 1 addition & 34 deletions llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4442,40 +4442,7 @@ bool CombinerHelper::matchICmpToTrueFalseKnownBits(MachineInstr &MI,

if (!KnownVal) {
auto KnownLHS = KB->getKnownBits(MI.getOperand(2).getReg());
switch (Pred) {
default:
llvm_unreachable("Unexpected G_ICMP predicate?");
case CmpInst::ICMP_EQ:
KnownVal = KnownBits::eq(KnownLHS, KnownRHS);
break;
case CmpInst::ICMP_NE:
KnownVal = KnownBits::ne(KnownLHS, KnownRHS);
break;
case CmpInst::ICMP_SGE:
KnownVal = KnownBits::sge(KnownLHS, KnownRHS);
break;
case CmpInst::ICMP_SGT:
KnownVal = KnownBits::sgt(KnownLHS, KnownRHS);
break;
case CmpInst::ICMP_SLE:
KnownVal = KnownBits::sle(KnownLHS, KnownRHS);
break;
case CmpInst::ICMP_SLT:
KnownVal = KnownBits::slt(KnownLHS, KnownRHS);
break;
case CmpInst::ICMP_UGE:
KnownVal = KnownBits::uge(KnownLHS, KnownRHS);
break;
case CmpInst::ICMP_UGT:
KnownVal = KnownBits::ugt(KnownLHS, KnownRHS);
break;
case CmpInst::ICMP_ULE:
KnownVal = KnownBits::ule(KnownLHS, KnownRHS);
break;
case CmpInst::ICMP_ULT:
KnownVal = KnownBits::ult(KnownLHS, KnownRHS);
break;
}
KnownVal = ICmpInst::compare(KnownLHS, KnownRHS, Pred);
}

if (!KnownVal)
Expand Down
30 changes: 30 additions & 0 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/CheckedArithmetic.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/KnownBits.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/ModRef.h"
#include "llvm/Support/TypeSize.h"
Expand Down Expand Up @@ -3837,6 +3838,35 @@ bool FCmpInst::compare(const APFloat &LHS, const APFloat &RHS,
}
}

std::optional<bool> ICmpInst::compare(const KnownBits &LHS,
const KnownBits &RHS,
ICmpInst::Predicate Pred) {
switch (Pred) {
case ICmpInst::ICMP_EQ:
return KnownBits::eq(LHS, RHS);
case ICmpInst::ICMP_NE:
return KnownBits::ne(LHS, RHS);
case ICmpInst::ICMP_UGE:
return KnownBits::uge(LHS, RHS);
case ICmpInst::ICMP_UGT:
return KnownBits::ugt(LHS, RHS);
case ICmpInst::ICMP_ULE:
return KnownBits::ule(LHS, RHS);
case ICmpInst::ICMP_ULT:
return KnownBits::ult(LHS, RHS);
case ICmpInst::ICMP_SGE:
return KnownBits::sge(LHS, RHS);
case ICmpInst::ICMP_SGT:
return KnownBits::sgt(LHS, RHS);
case ICmpInst::ICMP_SLE:
return KnownBits::sle(LHS, RHS);
case ICmpInst::ICMP_SLT:
return KnownBits::slt(LHS, RHS);
default:
llvm_unreachable("Unexpected non-integer predicate.");
}
}

CmpInst::Predicate CmpInst::getFlippedSignednessPredicate(Predicate pred) {
assert(CmpInst::isRelational(pred) &&
"Call only with non-equality predicates!");
Expand Down
Loading