Skip to content
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
35 changes: 25 additions & 10 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
;
Expand All @@ -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
;
Expand Down
Loading