Skip to content

[ValueTracking] Compute knownbits from (icmp upred (add/sub nuw X, Y), C) #87180

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 2 commits into from
Closed
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
47 changes: 29 additions & 18 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,17 +706,22 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred,
LHSRange = LHSRange.sub(*Offset);
Known = Known.unionWith(LHSRange.toKnownBits());
}
// X & Y u> C -> X u> C && Y u> C
if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) &&
match(LHS, m_c_And(m_V, m_Value()))) {
Known.One.setHighBits(
(*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
// X & Y u> C -> X u> C && Y u> C
// X nuw- Y u> C -> X u> C
if (match(LHS, m_c_And(m_V, m_Value())) ||
match(LHS, m_NUWSub(m_V, m_Value())))
Known.One.setHighBits(
(*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
}
// X | Y u< C -> X u< C && Y u< C
if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) &&
match(LHS, m_c_Or(m_V, m_Value()))) {
Known.Zero.setHighBits(
(*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
// X | Y u< C -> X u< C && Y u< C
// X nuw+ Y u< C -> X u< C && Y u< C
if (match(LHS, m_c_Or(m_V, m_Value())) ||
match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
Known.Zero.setHighBits(
(*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
}
}
}
break;
Expand Down Expand Up @@ -9576,14 +9581,20 @@ void llvm::findValuesAffectedByCondition(
if (match(A, m_AddLike(m_Value(X), m_ConstantInt())))
AddAffected(X);

Value *Y;
// X & Y u> C -> X >u C && Y >u C
// X | Y u< C -> X u< C && Y u< C
if (ICmpInst::isUnsigned(Pred) &&
(match(A, m_And(m_Value(X), m_Value(Y))) ||
match(A, m_Or(m_Value(X), m_Value(Y))))) {
AddAffected(X);
AddAffected(Y);
if (ICmpInst::isUnsigned(Pred)) {
Value *Y;
// X & Y u> C -> X >u C && Y >u C
// X | Y u< C -> X u< C && Y u< C
// X nuw+ Y u< C -> X u< C && Y u< C
if (match(A, m_And(m_Value(X), m_Value(Y))) ||
match(A, m_Or(m_Value(X), m_Value(Y))) ||
match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
AddAffected(X);
AddAffected(Y);
}
// X nuw- Y u> C -> X u> C
if (match(A, m_NUWSub(m_Value(X), m_Value())))
AddAffected(X);
}
}

Expand Down
272 changes: 272 additions & 0 deletions llvm/test/Transforms/InstCombine/known-bits.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1698,5 +1698,277 @@ define i32 @test_none(float nofpclass(all) %x) {
ret i32 %and
}

define i8 @test_icmp_add(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_add(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[N_ADD:%.*]] = add nuw i8 [[N:%.*]], [[N2:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[N_ADD]], 32
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: ret i8 0
; CHECK: if.else:
; CHECK-NEXT: ret i8 [[OTHER:%.*]]
;
entry:
%n_add = add nuw i8 %n, %n2
%cmp = icmp ult i8 %n_add, 32
br i1 %cmp, label %if.then, label %if.else

if.then:
%r = and i8 %n, 32
ret i8 %r

if.else:
ret i8 %other
}

define i8 @test_icmp_add_fail_nsw(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_add_fail_nsw(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[N_ADD:%.*]] = add nsw i8 [[N:%.*]], [[N2:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[N_ADD]], 32
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[R:%.*]] = and i8 [[N]], 32
; CHECK-NEXT: ret i8 [[R]]
; CHECK: if.else:
; CHECK-NEXT: ret i8 [[OTHER:%.*]]
;
entry:
%n_add = add nsw i8 %n, %n2
%cmp = icmp ult i8 %n_add, 32
br i1 %cmp, label %if.then, label %if.else

if.then:
%r = and i8 %n, 32
ret i8 %r

if.else:
ret i8 %other
}

define i8 @test_icmp_add2(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_add2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[N_ADD:%.*]] = add nuw nsw i8 [[N:%.*]], [[N2:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[N_ADD]], 14
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: ret i8 [[OTHER:%.*]]
; CHECK: if.else:
; CHECK-NEXT: ret i8 0
;
entry:
%n_add = add nsw nuw i8 %n, %n2
%cmp = icmp uge i8 %n_add, 15
br i1 %cmp, label %if.then, label %if.else

if.then:
ret i8 %other
if.else:
%r = and i8 %n, 32
ret i8 %r

}

define i8 @test_icmp_add_fail_bad_range(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_add_fail_bad_range(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[N_ADD:%.*]] = add nuw i8 [[N:%.*]], [[N2:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[N_ADD]], 33
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[R:%.*]] = and i8 [[N]], 32
; CHECK-NEXT: ret i8 [[R]]
; CHECK: if.else:
; CHECK-NEXT: ret i8 [[OTHER:%.*]]
;
entry:
%n_add = add nuw i8 %n, %n2
%cmp = icmp ule i8 %n_add, 32
br i1 %cmp, label %if.then, label %if.else

if.then:
%r = and i8 %n, 32
ret i8 %r

if.else:
ret i8 %other
}

define i8 @test_icmp_add_fail_bad_pred(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_add_fail_bad_pred(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[N_ADD:%.*]] = add nuw i8 [[N:%.*]], [[N2:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[N_ADD]], 32
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[R:%.*]] = and i8 [[N]], 32
; CHECK-NEXT: ret i8 [[R]]
; CHECK: if.else:
; CHECK-NEXT: ret i8 [[OTHER:%.*]]
;
entry:
%n_add = add nuw i8 %n, %n2
%cmp = icmp ugt i8 %n_add, 32
br i1 %cmp, label %if.then, label %if.else

if.then:
%r = and i8 %n, 32
ret i8 %r

if.else:
ret i8 %other
}

define i8 @test_icmp_sub(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_sub(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[N_SUB:%.*]] = sub nuw i8 [[N:%.*]], [[N2:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[N_SUB]], -33
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: ret i8 32
; CHECK: if.else:
; CHECK-NEXT: ret i8 [[OTHER:%.*]]
;
entry:
%n_sub = sub nuw i8 %n, %n2
%cmp = icmp ugt i8 %n_sub, 223
br i1 %cmp, label %if.then, label %if.else

if.then:
%r = and i8 %n, 32
ret i8 %r

if.else:
ret i8 %other
}

define i8 @test_icmp_sub_fail_wrong_arg(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_sub_fail_wrong_arg(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[N_SUB:%.*]] = sub nuw i8 [[N:%.*]], [[N2:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[N_SUB]], -33
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[R:%.*]] = and i8 [[N2]], 32
; CHECK-NEXT: ret i8 [[R]]
; CHECK: if.else:
; CHECK-NEXT: ret i8 [[OTHER:%.*]]
;
entry:
%n_sub = sub nuw i8 %n, %n2
%cmp = icmp ugt i8 %n_sub, 223
br i1 %cmp, label %if.then, label %if.else

if.then:
%r = and i8 %n2, 32
ret i8 %r

if.else:
ret i8 %other
}

define i8 @test_icmp_sub2(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_sub2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[N_SUB:%.*]] = sub nuw i8 [[N:%.*]], [[N2:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[N_SUB]], -31
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: ret i8 [[OTHER:%.*]]
; CHECK: if.else:
; CHECK-NEXT: ret i8 32
;
entry:
%n_sub = sub nuw i8 %n, %n2
%cmp = icmp ule i8 %n_sub, 224
br i1 %cmp, label %if.then, label %if.else

if.then:
ret i8 %other
if.else:
%r = and i8 %n, 32
ret i8 %r

}

define i8 @test_icmp_sub2_fail_nsw(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_sub2_fail_nsw(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[N_SUB:%.*]] = sub nsw i8 [[N:%.*]], [[N2:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[N_SUB]], -31
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: ret i8 [[OTHER:%.*]]
; CHECK: if.else:
; CHECK-NEXT: [[R:%.*]] = and i8 [[N]], 32
; CHECK-NEXT: ret i8 [[R]]
;
entry:
%n_sub = sub nsw i8 %n, %n2
%cmp = icmp ule i8 %n_sub, 224
br i1 %cmp, label %if.then, label %if.else

if.then:
ret i8 %other
if.else:
%r = and i8 %n, 32
ret i8 %r

}


define i8 @test_icmp_sub_fail_bad_range(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_sub_fail_bad_range(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[N_SUB:%.*]] = sub nuw i8 [[N:%.*]], [[N2:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i8 [[N_SUB]], -34
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[R:%.*]] = and i8 [[N]], 32
; CHECK-NEXT: ret i8 [[R]]
; CHECK: if.else:
; CHECK-NEXT: ret i8 [[OTHER:%.*]]
;
entry:
%n_sub = sub nuw i8 %n, %n2
%cmp = icmp uge i8 %n_sub, 223
br i1 %cmp, label %if.then, label %if.else

if.then:
%r = and i8 %n, 32
ret i8 %r

if.else:
ret i8 %other
}

define i8 @test_icmp_sub_fail_bad_pred(i8 %n, i8 %n2, i8 %other) {
; CHECK-LABEL: @test_icmp_sub_fail_bad_pred(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[N_SUB:%.*]] = sub nuw i8 [[N:%.*]], [[N2:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i8 [[N_SUB]], 31
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[R:%.*]] = and i8 [[N]], 32
; CHECK-NEXT: ret i8 [[R]]
; CHECK: if.else:
; CHECK-NEXT: ret i8 [[OTHER:%.*]]
;
entry:
%n_sub = sub nuw i8 %n, %n2
%cmp = icmp sge i8 %n_sub, 32
br i1 %cmp, label %if.then, label %if.else

if.then:
%r = and i8 %n, 32
ret i8 %r

if.else:
ret i8 %other
}

declare void @use(i1)
declare void @sink(i8)
Loading