Skip to content

VT: teach isImpliedCondMatchingOperands about samesign #122474

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
Jan 11, 2025
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
8 changes: 0 additions & 8 deletions llvm/include/llvm/IR/InstrTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -967,14 +967,6 @@ class CmpInst : public Instruction {
/// Determine if the predicate is false when comparing a value with itself.
static bool isFalseWhenEqual(Predicate predicate);

/// Determine if Pred1 implies Pred2 is true when two compares have matching
/// operands.
static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2);

/// Determine if Pred1 implies Pred2 is false when two compares have matching
/// operands.
static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2);

/// Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Instruction *I) {
return I->getOpcode() == Instruction::ICmp ||
Expand Down
10 changes: 10 additions & 0 deletions llvm/include/llvm/IR/Instructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,16 @@ class ICmpInst: public CmpInst {
return getFlippedSignednessPredicate(getPredicate());
}

/// Determine if Pred1 implies Pred2 is true when two compares have matching
/// operands.
static bool isImpliedTrueByMatchingCmp(CmpPredicate Pred1,
CmpPredicate Pred2);

/// Determine if Pred1 implies Pred2 is false when two compares have matching
/// operands.
static bool isImpliedFalseByMatchingCmp(CmpPredicate Pred1,
CmpPredicate Pred2);

void setSameSign(bool B = true) {
SubclassOptionalData = (SubclassOptionalData & ~SameSign) | (B * SameSign);
}
Expand Down
16 changes: 9 additions & 7 deletions llvm/include/llvm/SandboxIR/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2511,13 +2511,6 @@ class CmpInst : public SingleLLVMInstructionImpl<llvm::CmpInst> {
WRAP_STATIC_PREDICATE(isOrdered);
WRAP_STATIC_PREDICATE(isUnordered);

static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
return llvm::CmpInst::isImpliedTrueByMatchingCmp(Pred1, Pred2);
}
static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
return llvm::CmpInst::isImpliedFalseByMatchingCmp(Pred1, Pred2);
}

/// Method for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Value *From) {
return From->getSubclassID() == ClassID::ICmp ||
Expand Down Expand Up @@ -2554,6 +2547,15 @@ class ICmpInst : public CmpInst {
WRAP_STATIC_PREDICATE(isGE);
WRAP_STATIC_PREDICATE(isLE);

static bool isImpliedTrueByMatchingCmp(CmpPredicate Pred1,
CmpPredicate Pred2) {
return llvm::ICmpInst::isImpliedTrueByMatchingCmp(Pred1, Pred2);
}
static bool isImpliedFalseByMatchingCmp(CmpPredicate Pred1,
CmpPredicate Pred2) {
return llvm::ICmpInst::isImpliedFalseByMatchingCmp(Pred1, Pred2);
}

static auto predicates() { return llvm::ICmpInst::predicates(); }
static bool compare(const APInt &LHS, const APInt &RHS,
ICmpInst::Predicate Pred) {
Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9374,12 +9374,11 @@ isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
/// Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true.
/// Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false.
/// Otherwise, return std::nullopt if we can't infer anything.
static std::optional<bool>
isImpliedCondMatchingOperands(CmpInst::Predicate LPred,
CmpInst::Predicate RPred) {
if (CmpInst::isImpliedTrueByMatchingCmp(LPred, RPred))
static std::optional<bool> isImpliedCondMatchingOperands(CmpPredicate LPred,
CmpPredicate RPred) {
if (ICmpInst::isImpliedTrueByMatchingCmp(LPred, RPred))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like isImpliedTrueByMatchingCmp and isImpliedFalseByMatchingCmp are always called together. We can replace them with a helper std::optional<bool> isImpliedByMatchingCmp in the future.

return true;
if (CmpInst::isImpliedFalseByMatchingCmp(LPred, RPred))
if (ICmpInst::isImpliedFalseByMatchingCmp(LPred, RPred))
return false;

return std::nullopt;
Expand Down
13 changes: 10 additions & 3 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3886,12 +3886,18 @@ bool CmpInst::isFalseWhenEqual(Predicate predicate) {
}
}

bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
bool ICmpInst::isImpliedTrueByMatchingCmp(CmpPredicate Pred1,
CmpPredicate Pred2) {
// If the predicates match, then we know the first condition implies the
// second is true.
if (Pred1 == Pred2)
if (CmpPredicate::getMatching(Pred1, Pred2))
return true;

if (Pred1.hasSameSign() && CmpInst::isSigned(Pred2))
Pred1 = ICmpInst::getFlippedSignednessPredicate(Pred1);
else if (Pred2.hasSameSign() && CmpInst::isSigned(Pred1))
Pred2 = ICmpInst::getFlippedSignednessPredicate(Pred2);

switch (Pred1) {
default:
break;
Expand All @@ -3911,7 +3917,8 @@ bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
return false;
}

bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
bool ICmpInst::isImpliedFalseByMatchingCmp(CmpPredicate Pred1,
CmpPredicate Pred2) {
return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
}

Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Transforms/Scalar/NewGVN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1964,15 +1964,15 @@ NewGVN::ExprResult NewGVN::performSymbolicCmpEvaluation(Instruction *I) const {
if (PBranch->TrueEdge) {
// If we know the previous predicate is true and we are in the true
// edge then we may be implied true or false.
if (CmpInst::isImpliedTrueByMatchingCmp(BranchPredicate,
OurPredicate)) {
if (ICmpInst::isImpliedTrueByMatchingCmp(BranchPredicate,
OurPredicate)) {
return ExprResult::some(
createConstantExpression(ConstantInt::getTrue(CI->getType())),
PI);
}

if (CmpInst::isImpliedFalseByMatchingCmp(BranchPredicate,
OurPredicate)) {
if (ICmpInst::isImpliedFalseByMatchingCmp(BranchPredicate,
OurPredicate)) {
return ExprResult::some(
createConstantExpression(ConstantInt::getFalse(CI->getType())),
PI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ define i1 @sgt_implies_ge_via_assume(i32 %i, i32 %j) {
; CHECK-SAME: i32 [[I:%.*]], i32 [[J:%.*]]) {
; CHECK-NEXT: [[I_SGT_J:%.*]] = icmp sgt i32 [[I]], [[J]]
; CHECK-NEXT: call void @llvm.assume(i1 [[I_SGT_J]])
; CHECK-NEXT: [[I_GE_J:%.*]] = icmp samesign uge i32 [[I]], [[J]]
; CHECK-NEXT: ret i1 [[I_GE_J]]
; CHECK-NEXT: ret i1 true
;
%i.sgt.j = icmp sgt i32 %i, %j
call void @llvm.assume(i1 %i.sgt.j)
Expand All @@ -134,9 +133,7 @@ define i32 @gt_implies_sge_dominating(i32 %a, i32 %len) {
; CHECK-NEXT: [[A_GT_LEN:%.*]] = icmp samesign ugt i32 [[A]], [[LEN]]
; CHECK-NEXT: br i1 [[A_GT_LEN]], label %[[TAKEN:.*]], label %[[END:.*]]
; CHECK: [[TAKEN]]:
; CHECK-NEXT: [[A_SGE_LEN:%.*]] = icmp sge i32 [[A]], [[LEN]]
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A_SGE_LEN]], i32 30, i32 0
; CHECK-NEXT: ret i32 [[RES]]
; CHECK-NEXT: ret i32 30
; CHECK: [[END]]:
; CHECK-NEXT: ret i32 -1
;
Expand Down
Loading