Skip to content

Commit f38c40b

Browse files
authored
VT: teach isImpliedCondMatchingOperands about samesign (#122474)
Move isImplied{True,False}ByMatchingCmp from CmpInst to ICmpInst, so that it can operate on CmpPredicate instead of CmpInst::Predicate, and teach it about samesign. There are two callers of this function, and we choose to migrate the one in ValueTracking, namely isImpliedCondMatchingOperands to CmpPredicate, hence teaching it about samesign, with visible test impact.
1 parent 212cba0 commit f38c40b

File tree

7 files changed

+39
-32
lines changed

7 files changed

+39
-32
lines changed

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -967,14 +967,6 @@ class CmpInst : public Instruction {
967967
/// Determine if the predicate is false when comparing a value with itself.
968968
static bool isFalseWhenEqual(Predicate predicate);
969969

970-
/// Determine if Pred1 implies Pred2 is true when two compares have matching
971-
/// operands.
972-
static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2);
973-
974-
/// Determine if Pred1 implies Pred2 is false when two compares have matching
975-
/// operands.
976-
static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2);
977-
978970
/// Methods for support type inquiry through isa, cast, and dyn_cast:
979971
static bool classof(const Instruction *I) {
980972
return I->getOpcode() == Instruction::ICmp ||

llvm/include/llvm/IR/Instructions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,16 @@ class ICmpInst: public CmpInst {
12661266
return getFlippedSignednessPredicate(getPredicate());
12671267
}
12681268

1269+
/// Determine if Pred1 implies Pred2 is true when two compares have matching
1270+
/// operands.
1271+
static bool isImpliedTrueByMatchingCmp(CmpPredicate Pred1,
1272+
CmpPredicate Pred2);
1273+
1274+
/// Determine if Pred1 implies Pred2 is false when two compares have matching
1275+
/// operands.
1276+
static bool isImpliedFalseByMatchingCmp(CmpPredicate Pred1,
1277+
CmpPredicate Pred2);
1278+
12691279
void setSameSign(bool B = true) {
12701280
SubclassOptionalData = (SubclassOptionalData & ~SameSign) | (B * SameSign);
12711281
}

llvm/include/llvm/SandboxIR/Instruction.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,13 +2511,6 @@ class CmpInst : public SingleLLVMInstructionImpl<llvm::CmpInst> {
25112511
WRAP_STATIC_PREDICATE(isOrdered);
25122512
WRAP_STATIC_PREDICATE(isUnordered);
25132513

2514-
static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
2515-
return llvm::CmpInst::isImpliedTrueByMatchingCmp(Pred1, Pred2);
2516-
}
2517-
static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
2518-
return llvm::CmpInst::isImpliedFalseByMatchingCmp(Pred1, Pred2);
2519-
}
2520-
25212514
/// Method for support type inquiry through isa, cast, and dyn_cast:
25222515
static bool classof(const Value *From) {
25232516
return From->getSubclassID() == ClassID::ICmp ||
@@ -2554,6 +2547,15 @@ class ICmpInst : public CmpInst {
25542547
WRAP_STATIC_PREDICATE(isGE);
25552548
WRAP_STATIC_PREDICATE(isLE);
25562549

2550+
static bool isImpliedTrueByMatchingCmp(CmpPredicate Pred1,
2551+
CmpPredicate Pred2) {
2552+
return llvm::ICmpInst::isImpliedTrueByMatchingCmp(Pred1, Pred2);
2553+
}
2554+
static bool isImpliedFalseByMatchingCmp(CmpPredicate Pred1,
2555+
CmpPredicate Pred2) {
2556+
return llvm::ICmpInst::isImpliedFalseByMatchingCmp(Pred1, Pred2);
2557+
}
2558+
25572559
static auto predicates() { return llvm::ICmpInst::predicates(); }
25582560
static bool compare(const APInt &LHS, const APInt &RHS,
25592561
ICmpInst::Predicate Pred) {

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9387,12 +9387,11 @@ isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
93879387
/// Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true.
93889388
/// Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false.
93899389
/// Otherwise, return std::nullopt if we can't infer anything.
9390-
static std::optional<bool>
9391-
isImpliedCondMatchingOperands(CmpInst::Predicate LPred,
9392-
CmpInst::Predicate RPred) {
9393-
if (CmpInst::isImpliedTrueByMatchingCmp(LPred, RPred))
9390+
static std::optional<bool> isImpliedCondMatchingOperands(CmpPredicate LPred,
9391+
CmpPredicate RPred) {
9392+
if (ICmpInst::isImpliedTrueByMatchingCmp(LPred, RPred))
93949393
return true;
9395-
if (CmpInst::isImpliedFalseByMatchingCmp(LPred, RPred))
9394+
if (ICmpInst::isImpliedFalseByMatchingCmp(LPred, RPred))
93969395
return false;
93979396

93989397
return std::nullopt;

llvm/lib/IR/Instructions.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3886,12 +3886,18 @@ bool CmpInst::isFalseWhenEqual(Predicate predicate) {
38863886
}
38873887
}
38883888

3889-
bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
3889+
bool ICmpInst::isImpliedTrueByMatchingCmp(CmpPredicate Pred1,
3890+
CmpPredicate Pred2) {
38903891
// If the predicates match, then we know the first condition implies the
38913892
// second is true.
3892-
if (Pred1 == Pred2)
3893+
if (CmpPredicate::getMatching(Pred1, Pred2))
38933894
return true;
38943895

3896+
if (Pred1.hasSameSign() && CmpInst::isSigned(Pred2))
3897+
Pred1 = ICmpInst::getFlippedSignednessPredicate(Pred1);
3898+
else if (Pred2.hasSameSign() && CmpInst::isSigned(Pred1))
3899+
Pred2 = ICmpInst::getFlippedSignednessPredicate(Pred2);
3900+
38953901
switch (Pred1) {
38963902
default:
38973903
break;
@@ -3911,7 +3917,8 @@ bool CmpInst::isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2) {
39113917
return false;
39123918
}
39133919

3914-
bool CmpInst::isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2) {
3920+
bool ICmpInst::isImpliedFalseByMatchingCmp(CmpPredicate Pred1,
3921+
CmpPredicate Pred2) {
39153922
return isImpliedTrueByMatchingCmp(Pred1, getInversePredicate(Pred2));
39163923
}
39173924

llvm/lib/Transforms/Scalar/NewGVN.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,15 +1964,15 @@ NewGVN::ExprResult NewGVN::performSymbolicCmpEvaluation(Instruction *I) const {
19641964
if (PBranch->TrueEdge) {
19651965
// If we know the previous predicate is true and we are in the true
19661966
// edge then we may be implied true or false.
1967-
if (CmpInst::isImpliedTrueByMatchingCmp(BranchPredicate,
1968-
OurPredicate)) {
1967+
if (ICmpInst::isImpliedTrueByMatchingCmp(BranchPredicate,
1968+
OurPredicate)) {
19691969
return ExprResult::some(
19701970
createConstantExpression(ConstantInt::getTrue(CI->getType())),
19711971
PI);
19721972
}
19731973

1974-
if (CmpInst::isImpliedFalseByMatchingCmp(BranchPredicate,
1975-
OurPredicate)) {
1974+
if (ICmpInst::isImpliedFalseByMatchingCmp(BranchPredicate,
1975+
OurPredicate)) {
19761976
return ExprResult::some(
19771977
createConstantExpression(ConstantInt::getFalse(CI->getType())),
19781978
PI);

llvm/test/Analysis/ValueTracking/implied-condition-samesign.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ define i1 @sgt_implies_ge_via_assume(i32 %i, i32 %j) {
118118
; CHECK-SAME: i32 [[I:%.*]], i32 [[J:%.*]]) {
119119
; CHECK-NEXT: [[I_SGT_J:%.*]] = icmp sgt i32 [[I]], [[J]]
120120
; CHECK-NEXT: call void @llvm.assume(i1 [[I_SGT_J]])
121-
; CHECK-NEXT: [[I_GE_J:%.*]] = icmp samesign uge i32 [[I]], [[J]]
122-
; CHECK-NEXT: ret i1 [[I_GE_J]]
121+
; CHECK-NEXT: ret i1 true
123122
;
124123
%i.sgt.j = icmp sgt i32 %i, %j
125124
call void @llvm.assume(i1 %i.sgt.j)
@@ -134,9 +133,7 @@ define i32 @gt_implies_sge_dominating(i32 %a, i32 %len) {
134133
; CHECK-NEXT: [[A_GT_LEN:%.*]] = icmp samesign ugt i32 [[A]], [[LEN]]
135134
; CHECK-NEXT: br i1 [[A_GT_LEN]], label %[[TAKEN:.*]], label %[[END:.*]]
136135
; CHECK: [[TAKEN]]:
137-
; CHECK-NEXT: [[A_SGE_LEN:%.*]] = icmp sge i32 [[A]], [[LEN]]
138-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A_SGE_LEN]], i32 30, i32 0
139-
; CHECK-NEXT: ret i32 [[RES]]
136+
; CHECK-NEXT: ret i32 30
140137
; CHECK: [[END]]:
141138
; CHECK-NEXT: ret i32 -1
142139
;

0 commit comments

Comments
 (0)