Skip to content

[ValueTracking] Improve isImpliedCondCommonOperandWithConstants to handle truncated LHS #69829

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
41 changes: 34 additions & 7 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8231,14 +8231,30 @@ isImpliedCondMatchingOperands(CmpInst::Predicate LPred,
return std::nullopt;
}

/// Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true.
/// Return false if "icmp LPred X, LC" implies "icmp RPred X, RC" is false.
/// Otherwise, return std::nullopt if we can't infer anything.
/// Return true if "icmp LPred X, LC" implies "icmp RPred cast(X), RC" is true.
/// Return false if "icmp LPred X, LC" implies "icmp RPred cast(X), RC" is
/// false. Otherwise, return std::nullopt if we can't infer anything.
static std::optional<bool> isImpliedCondCommonOperandWithConstants(
CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred,
const APInt &RC) {
const Value *L0, CmpInst::Predicate LPred, const APInt &LC, const Value *R0,
CmpInst::Predicate RPred, const APInt &RC) {
ConstantRange DomCR = ConstantRange::makeExactICmpRegion(LPred, LC);
ConstantRange CR = ConstantRange::makeExactICmpRegion(RPred, RC);

if (L0 == R0)
; // noop
// Example: icmp eq X, 3 --> icmp sgt trunc(X), 2
else if (match(R0, m_Trunc(m_Specific(L0))))
DomCR = DomCR.truncate(RC.getBitWidth());
// Example: icmp slt trunc(X), 3 --> icmp ne X, 3
else if (match(L0, m_Trunc(m_Specific(R0)))) {
// Try to prove by negation
DomCR = DomCR.inverse();
CR = CR.inverse();
std::swap(DomCR, CR);
DomCR = DomCR.truncate(LC.getBitWidth());
} else
return std::nullopt;

ConstantRange Intersection = DomCR.intersectWith(CR);
ConstantRange Difference = DomCR.difference(CR);
if (Intersection.isEmptySet())
Expand Down Expand Up @@ -8272,8 +8288,19 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
// Can we infer anything when the 0-operands match and the 1-operands are
// constants (not necessarily matching)?
const APInt *LC, *RC;
if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
if (match(L1, m_APInt(LC)) && match(R1, m_APInt(RC))) {
if (auto Res = isImpliedCondCommonOperandWithConstants(L0, LPred, *LC, R0,
RPred, *RC))
return Res;

if (match(L0, m_Trunc(m_Specific(R0)))) {
// When L0 == trunc(R0), we use the law of excluded middle to cover some
// missing cases.
if (auto Res = isImpliedCondCommonOperandWithConstants(
L0, LPred, *LC, R0, ICmpInst::getInversePredicate(RPred), *RC))
return !*Res;
}
}

// L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
if (ICmpInst::isUnsigned(LPred) && ICmpInst::isUnsigned(RPred)) {
Expand Down
70 changes: 70 additions & 0 deletions llvm/test/Analysis/ValueTracking/pr68514.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

; Tests from PR68514
define i1 @f(i32 %a) {
; CHECK-LABEL: define i1 @f(
; CHECK-SAME: i32 [[A:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i1 false
;
entry:
%cmp = icmp eq i32 %a, 0
%b = trunc i32 %a to i16
%cmp2 = icmp sgt i16 %b, 2
%and9 = and i1 %cmp, %cmp2
ret i1 %and9
}

define i1 @g(i32 %a) {
; CHECK-LABEL: define i1 @g(
; CHECK-SAME: i32 [[A:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[B:%.*]] = trunc i32 [[A]] to i16
; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i16 [[B]], 2
; CHECK-NEXT: ret i1 [[CMP2]]
;
entry:
%cmp = icmp eq i32 %a, 3
%b = trunc i32 %a to i16
%cmp2 = icmp sgt i16 %b, 2
%or9 = or i1 %cmp, %cmp2
ret i1 %or9
}

; b = trunc(a)

define i1 @fold_trunc(i32 %a) {
; CHECK-LABEL: define i1 @fold_trunc(
; CHECK-SAME: i32 [[A:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[A]], 2
; CHECK-NEXT: ret i1 [[CMP]]
;
entry:
%cmp = icmp ugt i32 %a, 2
%b = trunc i32 %a to i16
%cmp2 = icmp ugt i16 %b, 2
%and9 = or i1 %cmp, %cmp2
ret i1 %and9
}

; Negative tests

define i1 @nofold_trunc(i32 %a) {
; CHECK-LABEL: define i1 @nofold_trunc(
; CHECK-SAME: i32 [[A:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[A]], 3
; CHECK-NEXT: [[B:%.*]] = trunc i32 [[A]] to i16
; CHECK-NEXT: [[CMP2:%.*]] = icmp ugt i16 [[B]], 2
; CHECK-NEXT: [[AND9:%.*]] = or i1 [[CMP]], [[CMP2]]
; CHECK-NEXT: ret i1 [[AND9]]
;
entry:
%cmp = icmp ugt i32 %a, 3
%b = trunc i32 %a to i16
%cmp2 = icmp ugt i16 %b, 2
%and9 = or i1 %cmp, %cmp2
ret i1 %and9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test for lhs = trunc(rhs)

}