-
Notifications
You must be signed in to change notification settings - Fork 15.3k
VT: teach implied-cond-cr about samesign #122447
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
Conversation
Teach isImpliedCondCommonOperandWithCR about samesign, noting that the only case we need to handle is when exactly one of the icmps have samesign.
|
@llvm/pr-subscribers-llvm-analysis Author: Ramkumar Ramachandra (artagnon) ChangesTeach isImpliedCondCommonOperandWithCR about samesign, noting that the only case we need to handle is when exactly one of the icmps have samesign. Full diff: https://github.com/llvm/llvm-project/pull/122447.diff 2 Files Affected:
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index db244148a3b1ee..9a61b36efa51da 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9388,17 +9388,32 @@ isImpliedCondMatchingOperands(CmpInst::Predicate LPred,
/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
/// Otherwise, return std::nullopt if we can't infer anything.
-static std::optional<bool> isImpliedCondCommonOperandWithCR(
- CmpInst::Predicate LPred, const ConstantRange &LCR,
- CmpInst::Predicate RPred, const ConstantRange &RCR) {
- ConstantRange DomCR = ConstantRange::makeAllowedICmpRegion(LPred, LCR);
- // If all true values for lhs and true for rhs, lhs implies rhs
- if (DomCR.icmp(RPred, RCR))
- return true;
+static std::optional<bool>
+isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR,
+ CmpPredicate RPred, const ConstantRange &RCR) {
+ auto CRImpliesPred = [&](ConstantRange CR,
+ CmpInst::Predicate Pred) -> std::optional<bool> {
+ // If all true values for lhs and true for rhs, lhs implies rhs
+ if (CR.icmp(Pred, RCR))
+ return true;
- // If there is no overlap, lhs implies not rhs
- if (DomCR.icmp(CmpInst::getInversePredicate(RPred), RCR))
- return false;
+ // If there is no overlap, lhs implies not rhs
+ if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
+ return false;
+
+ return std::nullopt;
+ };
+ if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
+ RPred))
+ return Res;
+ if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
+ LPred = LPred.hasSameSign() ? ICmpInst::getFlippedSignednessPredicate(LPred)
+ : static_cast<CmpInst::Predicate>(LPred);
+ RPred = RPred.hasSameSign() ? ICmpInst::getFlippedSignednessPredicate(RPred)
+ : static_cast<CmpInst::Predicate>(RPred);
+ return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
+ RPred);
+ }
return std::nullopt;
}
diff --git a/llvm/test/Analysis/ValueTracking/implied-condition-samesign.ll b/llvm/test/Analysis/ValueTracking/implied-condition-samesign.ll
index 3abe4c76aaab6e..042155ae2bb79b 100644
--- a/llvm/test/Analysis/ValueTracking/implied-condition-samesign.ll
+++ b/llvm/test/Analysis/ValueTracking/implied-condition-samesign.ll
@@ -160,9 +160,7 @@ define i32 @gt_implies_sge_dominating_cr(i32 %a, i32 %len) {
; CHECK-NEXT: [[A_GT_20:%.*]] = icmp samesign ugt i32 [[A]], 20
; CHECK-NEXT: br i1 [[A_GT_20]], label %[[TAKEN:.*]], label %[[END:.*]]
; CHECK: [[TAKEN]]:
-; CHECK-NEXT: [[A_SGE_10:%.*]] = icmp sge i32 [[A]], 10
-; CHECK-NEXT: [[RES:%.*]] = select i1 [[A_SGE_10]], i32 30, i32 0
-; CHECK-NEXT: ret i32 [[RES]]
+; CHECK-NEXT: ret i32 30
; CHECK: [[END]]:
; CHECK-NEXT: ret i32 -1
;
@@ -186,9 +184,7 @@ define i32 @sgt_implies_ge_dominating_cr(i32 %a, i32 %len) {
; CHECK-NEXT: [[A_SGT_MINUS_10:%.*]] = icmp sgt i32 [[A]], -10
; CHECK-NEXT: br i1 [[A_SGT_MINUS_10]], label %[[TAKEN:.*]], label %[[END:.*]]
; CHECK: [[TAKEN]]:
-; CHECK-NEXT: [[A_GE_MINUS_20:%.*]] = icmp samesign uge i32 [[A]], -20
-; CHECK-NEXT: [[RES:%.*]] = select i1 [[A_GE_MINUS_20]], i32 30, i32 0
-; CHECK-NEXT: ret i32 [[RES]]
+; CHECK-NEXT: ret i32 30
; CHECK: [[END]]:
; CHECK-NEXT: ret i32 -1
;
|
nikic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Teach isImpliedCondCommonOperandWithCR about samesign, noting that the only case we need to handle is when exactly one of the icmps have samesign.
Teach isImpliedCondCommonOperandWithCR about samesign, noting that the only case we need to handle is when exactly one of the icmps have samesign.