Skip to content

Commit cfee344

Browse files
authored
VT: teach implied-cond-cr about samesign (#122447)
Teach isImpliedCondCommonOperandWithCR about samesign, noting that the only case we need to handle is when exactly one of the icmps have samesign.
1 parent 7b05367 commit cfee344

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9388,17 +9388,32 @@ isImpliedCondMatchingOperands(CmpInst::Predicate LPred,
93889388
/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
93899389
/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
93909390
/// Otherwise, return std::nullopt if we can't infer anything.
9391-
static std::optional<bool> isImpliedCondCommonOperandWithCR(
9392-
CmpInst::Predicate LPred, const ConstantRange &LCR,
9393-
CmpInst::Predicate RPred, const ConstantRange &RCR) {
9394-
ConstantRange DomCR = ConstantRange::makeAllowedICmpRegion(LPred, LCR);
9395-
// If all true values for lhs and true for rhs, lhs implies rhs
9396-
if (DomCR.icmp(RPred, RCR))
9397-
return true;
9391+
static std::optional<bool>
9392+
isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR,
9393+
CmpPredicate RPred, const ConstantRange &RCR) {
9394+
auto CRImpliesPred = [&](ConstantRange CR,
9395+
CmpInst::Predicate Pred) -> std::optional<bool> {
9396+
// If all true values for lhs and true for rhs, lhs implies rhs
9397+
if (CR.icmp(Pred, RCR))
9398+
return true;
93989399

9399-
// If there is no overlap, lhs implies not rhs
9400-
if (DomCR.icmp(CmpInst::getInversePredicate(RPred), RCR))
9401-
return false;
9400+
// If there is no overlap, lhs implies not rhs
9401+
if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9402+
return false;
9403+
9404+
return std::nullopt;
9405+
};
9406+
if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9407+
RPred))
9408+
return Res;
9409+
if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9410+
LPred = LPred.hasSameSign() ? ICmpInst::getFlippedSignednessPredicate(LPred)
9411+
: static_cast<CmpInst::Predicate>(LPred);
9412+
RPred = RPred.hasSameSign() ? ICmpInst::getFlippedSignednessPredicate(RPred)
9413+
: static_cast<CmpInst::Predicate>(RPred);
9414+
return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9415+
RPred);
9416+
}
94029417
return std::nullopt;
94039418
}
94049419

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ define i32 @gt_implies_sge_dominating_cr(i32 %a, i32 %len) {
160160
; CHECK-NEXT: [[A_GT_20:%.*]] = icmp samesign ugt i32 [[A]], 20
161161
; CHECK-NEXT: br i1 [[A_GT_20]], label %[[TAKEN:.*]], label %[[END:.*]]
162162
; CHECK: [[TAKEN]]:
163-
; CHECK-NEXT: [[A_SGE_10:%.*]] = icmp sge i32 [[A]], 10
164-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A_SGE_10]], i32 30, i32 0
165-
; CHECK-NEXT: ret i32 [[RES]]
163+
; CHECK-NEXT: ret i32 30
166164
; CHECK: [[END]]:
167165
; CHECK-NEXT: ret i32 -1
168166
;
@@ -186,9 +184,7 @@ define i32 @sgt_implies_ge_dominating_cr(i32 %a, i32 %len) {
186184
; CHECK-NEXT: [[A_SGT_MINUS_10:%.*]] = icmp sgt i32 [[A]], -10
187185
; CHECK-NEXT: br i1 [[A_SGT_MINUS_10]], label %[[TAKEN:.*]], label %[[END:.*]]
188186
; CHECK: [[TAKEN]]:
189-
; CHECK-NEXT: [[A_GE_MINUS_20:%.*]] = icmp samesign uge i32 [[A]], -20
190-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[A_GE_MINUS_20]], i32 30, i32 0
191-
; CHECK-NEXT: ret i32 [[RES]]
187+
; CHECK-NEXT: ret i32 30
192188
; CHECK: [[END]]:
193189
; CHECK-NEXT: ret i32 -1
194190
;

0 commit comments

Comments
 (0)