aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Analysis/ValueTracking.cpp54
-rw-r--r--llvm/lib/IR/Instructions.cpp52
2 files changed, 54 insertions, 52 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 7053195..0f6b0ec 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3935,61 +3935,11 @@ static Optional<bool> isImpliedCondMatchingOperands(CmpInst::Predicate APred,
std::swap(BLHS, BRHS);
BPred = ICmpInst::getSwappedPredicate(BPred);
}
-
- // If the predicates match, then we know the first condition implies the
- // second is true.
- if (APred == BPred)
+ if (CmpInst::isTrueWhenOperandsMatch(APred, BPred))
return true;
-
- // If an inverted APred matches BPred, we can infer the second condition is
- // false.
- if (CmpInst::getInversePredicate(APred) == BPred)
+ if (CmpInst::isFalseWhenOperandsMatch(APred, BPred))
return false;
- // If a swapped APred matches BPred, we can infer the second condition is
- // false in many cases.
- if (CmpInst::getSwappedPredicate(APred) == BPred) {
- switch (APred) {
- default:
- break;
- case CmpInst::ICMP_UGT: // A >u B implies A <u B is false.
- case CmpInst::ICMP_ULT: // A <u B implies A >u B is false.
- case CmpInst::ICMP_SGT: // A >s B implies A <s B is false.
- case CmpInst::ICMP_SLT: // A <s B implies A >s B is false.
- return false;
- }
- }
-
- // The predicates must match sign or at least one of them must be an equality
- // comparison (which is signless).
- if (ICmpInst::isSigned(APred) != ICmpInst::isSigned(BPred) &&
- !ICmpInst::isEquality(APred) && !ICmpInst::isEquality(BPred))
- return None;
-
- switch (APred) {
- default:
- break;
- case CmpInst::ICMP_EQ:
- // A == B implies A > B and A < B are false.
- if (CmpInst::isFalseWhenEqual(BPred))
- return false;
-
- break;
- case CmpInst::ICMP_UGT:
- case CmpInst::ICMP_ULT:
- case CmpInst::ICMP_SGT:
- case CmpInst::ICMP_SLT:
- // A > B implies A == B is false.
- // A < B implies A == B is false.
- if (BPred == CmpInst::ICMP_EQ)
- return false;
-
- // A > B implies A != B is true.
- // A < B implies A != B is true.
- if (BPred == CmpInst::ICMP_NE)
- return true;
- break;
- }
return None;
}
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index ac43eef..02005cf 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -3597,6 +3597,58 @@ bool CmpInst::isFalseWhenEqual(Predicate predicate) {
}
}
+bool CmpInst::isTrueWhenOperandsMatch(Predicate Pred1, Predicate Pred2) {
+ // If the predicates match, then we know the first condition implies the
+ // second is true.
+ if (Pred1 == Pred2)
+ return true;
+
+ switch (Pred1) {
+ default:
+ break;
+ case ICMP_UGT: // A >u B implies A != B is true.
+ case ICMP_ULT: // A <u B implies A != B is true.
+ case ICMP_SGT: // A >s B implies A != B is true.
+ case ICMP_SLT: // A <s B implies A != B is true.
+ return Pred2 == ICMP_NE;
+ }
+ return false;
+}
+
+bool CmpInst::isFalseWhenOperandsMatch(Predicate Pred1, Predicate Pred2) {
+ // If an inverted Pred1 matches Pred2, we can infer the second condition is
+ // false.
+ if (getInversePredicate(Pred1) == Pred2)
+ return true;
+
+ // If a swapped Pred1 matches Pred2, we can infer the second condition is
+ // false in many cases.
+ if (getSwappedPredicate(Pred1) == Pred2) {
+ switch (Pred1) {
+ default:
+ break;
+ case ICMP_UGT: // A >u B implies A <u B is false.
+ case ICMP_ULT: // A <u B implies A >u B is false.
+ case ICMP_SGT: // A >s B implies A <s B is false.
+ case ICMP_SLT: // A <s B implies A >s B is false.
+ return true;
+ }
+ }
+ // A == B implies A > B and A < B are false.
+ if (Pred1 == ICMP_EQ && isFalseWhenEqual(Pred2))
+ return true;
+
+ switch (Pred1) {
+ default:
+ break;
+ case ICMP_UGT: // A >u B implies A == B is false.
+ case ICMP_ULT: // A <u B implies A == B is false.
+ case ICMP_SGT: // A >s B implies A == B is false.
+ case ICMP_SLT: // A <s B implies A == B is false.
+ return Pred2 == ICMP_EQ;
+ }
+ return false;
+}
//===----------------------------------------------------------------------===//
// SwitchInst Implementation