Skip to content

[InstSimplify] Improve simplifyICmpWithConstant by using KnownBits info #76221

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
5 changes: 5 additions & 0 deletions llvm/include/llvm/Analysis/ValueTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,11 @@ ConstantRange computeConstantRange(const Value *V, bool ForSigned,
const DominatorTree *DT = nullptr,
unsigned Depth = 0);

/// Combine constant ranges from computeConstantRange() and computeKnownBits().
ConstantRange
computeConstantRangeIncludingKnownBits(const WithCache<const Value *> &V,
bool ForSigned, const SimplifyQuery &SQ);

/// Return true if this function can prove that the instruction I will
/// always transfer execution to one of its successors (including the next
/// instruction that follows within a basic block). E.g. this is not
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3005,7 +3005,7 @@ static Value *simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS,
}

static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
Value *RHS, const InstrInfoQuery &IIQ) {
Value *RHS, const SimplifyQuery &SQ) {
Type *ITy = getCompareTy(RHS); // The return type.

Value *X;
Expand All @@ -3031,8 +3031,8 @@ static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
if (RHS_CR.isFullSet())
return ConstantInt::getTrue(ITy);

ConstantRange LHS_CR =
computeConstantRange(LHS, CmpInst::isSigned(Pred), IIQ.UseInstrInfo);
ConstantRange LHS_CR = llvm::computeConstantRangeIncludingKnownBits(
LHS, CmpInst::isSigned(Pred), SQ);
if (!LHS_CR.isFullSet()) {
if (RHS_CR.contains(LHS_CR))
return ConstantInt::getTrue(ITy);
Expand All @@ -3043,7 +3043,7 @@ static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
// (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
// (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
const APInt *MulC;
if (IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) &&
if (SQ.IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) &&
((match(LHS, m_NUWMul(m_Value(), m_APIntAllowUndef(MulC))) &&
*MulC != 0 && C->urem(*MulC) != 0) ||
(match(LHS, m_NSWMul(m_Value(), m_APIntAllowUndef(MulC))) &&
Expand Down Expand Up @@ -3749,7 +3749,7 @@ static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
return V;

if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q.IIQ))
if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q))
return V;

// If both operands have range metadata, use the metadata
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6289,10 +6289,10 @@ static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) {
}

/// Combine constant ranges from computeConstantRange() and computeKnownBits().
static ConstantRange
computeConstantRangeIncludingKnownBits(const WithCache<const Value *> &V,
bool ForSigned,
const SimplifyQuery &SQ) {
ConstantRange
llvm::computeConstantRangeIncludingKnownBits(const WithCache<const Value *> &V,
bool ForSigned,
const SimplifyQuery &SQ) {
ConstantRange CR1 =
ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
Expand Down
12 changes: 2 additions & 10 deletions llvm/test/Analysis/ValueTracking/knownbits-bmi-pattern.ll
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,7 @@ define i1 @blsi_ne_is_true(i32 %x) {

define <2 x i1> @blsi_ge_is_false_vec(<2 x i32> %x) {
; CHECK-LABEL: @blsi_ge_is_false_vec(
; CHECK-NEXT: [[X1:%.*]] = or <2 x i32> [[X:%.*]], <i32 10, i32 10>
; CHECK-NEXT: [[X2:%.*]] = sub nsw <2 x i32> zeroinitializer, [[X1]]
; CHECK-NEXT: [[X3:%.*]] = and <2 x i32> [[X2]], [[X]]
; CHECK-NEXT: [[Z:%.*]] = icmp ugt <2 x i32> [[X3]], <i32 7, i32 7>
; CHECK-NEXT: ret <2 x i1> [[Z]]
; CHECK-NEXT: ret <2 x i1> zeroinitializer
;
%x1 = or <2 x i32> %x, <i32 10, i32 10>
%x2 = sub <2 x i32> <i32 0, i32 0>, %x1
Expand All @@ -350,11 +346,7 @@ define <2 x i1> @blsi_ge_is_false_vec(<2 x i32> %x) {

define <2 x i1> @blsi_ge_is_false_diff_vec(<2 x i32> %x) {
; CHECK-LABEL: @blsi_ge_is_false_diff_vec(
; CHECK-NEXT: [[X1:%.*]] = or <2 x i32> [[X:%.*]], <i32 10, i32 11>
; CHECK-NEXT: [[X2:%.*]] = sub nsw <2 x i32> zeroinitializer, [[X1]]
; CHECK-NEXT: [[X3:%.*]] = and <2 x i32> [[X2]], [[X]]
; CHECK-NEXT: [[Z:%.*]] = icmp ugt <2 x i32> [[X3]], <i32 7, i32 7>
; CHECK-NEXT: ret <2 x i1> [[Z]]
; CHECK-NEXT: ret <2 x i1> zeroinitializer
;
%x1 = or <2 x i32> %x, <i32 10, i32 11>
%x2 = sub <2 x i32> <i32 0, i32 0>, %x1
Expand Down
9 changes: 3 additions & 6 deletions llvm/test/Analysis/ValueTracking/monotonic-phi.ll
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,7 @@ define i1 @test_shl_zero_start(i8 %n) {
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[A]], [[N:%.*]]
; CHECK-NEXT: br i1 [[CMP1]], label [[EXIT:%.*]], label [[LOOP]]
; CHECK: exit:
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[A]], 0
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
entry:
br label %loop
Expand Down Expand Up @@ -559,8 +558,7 @@ define i1 @test_lshr_zero_start(i8 %n) {
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[A]], [[N:%.*]]
; CHECK-NEXT: br i1 [[CMP1]], label [[EXIT:%.*]], label [[LOOP]]
; CHECK: exit:
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[A]], 0
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
entry:
br label %loop
Expand Down Expand Up @@ -633,8 +631,7 @@ define i1 @test_ashr_zero_start(i8 %n) {
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[A]], [[N:%.*]]
; CHECK-NEXT: br i1 [[CMP1]], label [[EXIT:%.*]], label [[LOOP]]
; CHECK: exit:
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[A]], 0
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
entry:
br label %loop
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/Attributor/lvi-after-jumpthreading.ll
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ define i32 @test4(i32 %i, i1 %f, i32 %n) {
; CHECK-NEXT: call void @dummy(i1 [[F]]) #[[ATTR2]]
; CHECK-NEXT: [[CONSUME:%.*]] = call i32 @exit()
; CHECK-NEXT: call void @llvm.assume(i1 noundef [[F]])
; CHECK-NEXT: [[COND:%.*]] = icmp eq i1 [[F]], false
; CHECK-NEXT: br i1 [[COND]], label [[EXIT]], label [[CONT:%.*]]
; CHECK-NEXT: br label [[CONT:%.*]]
; CHECK: exit2:
; CHECK-NEXT: ret i32 30
;
Expand Down
8 changes: 2 additions & 6 deletions llvm/test/Transforms/InstSimplify/call.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1582,9 +1582,7 @@ define i1 @ctlz_i1_non_poison_eq_false(i1 %x) {

define i1 @ctlz_i1_poison_eq_false(i1 %x) {
; CHECK-LABEL: @ctlz_i1_poison_eq_false(
; CHECK-NEXT: [[CT:%.*]] = call i1 @llvm.ctlz.i1(i1 [[X:%.*]], i1 true)
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i1 [[CT]], false
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
%ct = call i1 @llvm.ctlz.i1(i1 %x, i1 true)
%cmp = icmp eq i1 %ct, false
Expand All @@ -1604,9 +1602,7 @@ define i1 @cttz_i1_non_poison_eq_false(i1 %x) {

define i1 @cttz_i1_poison_eq_false(i1 %x) {
; CHECK-LABEL: @cttz_i1_poison_eq_false(
; CHECK-NEXT: [[CT:%.*]] = call i1 @llvm.cttz.i1(i1 [[X:%.*]], i1 true)
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i1 [[CT]], false
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
%ct = call i1 @llvm.cttz.i1(i1 %x, i1 true)
%cmp = icmp eq i1 %ct, false
Expand Down
28 changes: 19 additions & 9 deletions llvm/test/Transforms/InstSimplify/icmp-constant.ll
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,7 @@ define <2 x i1> @or1_vec_partial_undef(<2 x i32> %X) {
; Single bit OR.
define i1 @or2_true(i8 %x) {
; CHECK-LABEL: @or2_true(
; CHECK-NEXT: [[Y:%.*]] = or i8 [[X:%.*]], 64
; CHECK-NEXT: [[Z:%.*]] = icmp sge i8 [[Y]], -64
; CHECK-NEXT: ret i1 [[Z]]
; CHECK-NEXT: ret i1 true
;
%y = or i8 %x, 64
%z = icmp sge i8 %y, -64
Expand All @@ -457,9 +455,7 @@ define i1 @or2_unknown(i8 %x) {
; 78 = 0b01001110; -50 = 0b11001110
define i1 @or3_true(i8 %x) {
; CHECK-LABEL: @or3_true(
; CHECK-NEXT: [[Y:%.*]] = or i8 [[X:%.*]], 78
; CHECK-NEXT: [[Z:%.*]] = icmp sge i8 [[Y]], -50
; CHECK-NEXT: ret i1 [[Z]]
; CHECK-NEXT: ret i1 true
;
%y = or i8 %x, 78
%z = icmp sge i8 %y, -50
Expand Down Expand Up @@ -573,9 +569,7 @@ define i1 @and3_unknown1(i8 %x) {

define i1 @and3_true2(i8 %x) {
; CHECK-LABEL: @and3_true2(
; CHECK-NEXT: [[Y:%.*]] = and i8 [[X:%.*]], -75
; CHECK-NEXT: [[Z:%.*]] = icmp sle i8 [[Y]], 53
; CHECK-NEXT: ret i1 [[Z]]
; CHECK-NEXT: ret i1 true
;
%y = and i8 %x, -75
%z = icmp sle i8 %y, 53
Expand Down Expand Up @@ -1140,3 +1134,19 @@ define <2 x i1> @heterogeneous_constvector(<2 x i8> %x) {
%c = icmp ult <2 x i8> %x, <i8 undef, i8 poison>
ret <2 x i1> %c
}

define i8 @infer_sub_with_knownbits_info(i8 %a, i8 %b) {
; CHECK-LABEL: @infer_sub_with_knownbits_info(
; CHECK-NEXT: [[A1:%.*]] = or i8 [[A:%.*]], 1
; CHECK-NEXT: [[A2:%.*]] = shl i8 [[B:%.*]], 1
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[A1]], [[A2]]
; CHECK-NEXT: ret i8 [[SUB]]
;
%a1 = or i8 %a, 1
%a2 = shl i8 %b, 1
%sub = sub i8 %a1, %a2
%umax = tail call i8 @llvm.umax.i8(i8 %sub, i8 1)
ret i8 %umax
}

declare i8 @llvm.umax.i8(i8, i8)
9 changes: 8 additions & 1 deletion llvm/test/Transforms/JumpThreading/pr62908.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@

define i32 @test() {
; CHECK-LABEL: define i32 @test() {
; CHECK-NEXT: end:
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[END:%.*]]
; CHECK: unreachable:
; CHECK-NEXT: [[SH_PROM:%.*]] = zext i32 -1 to i64
; CHECK-NEXT: [[SHL:%.*]] = shl nsw i64 -1, [[SH_PROM]]
; CHECK-NEXT: [[CONV:%.*]] = trunc i64 [[SHL]] to i32
; CHECK-NEXT: br label [[END]]
; CHECK: end:
; CHECK-NEXT: ret i32 0
;
entry:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4578,10 +4578,8 @@ define void @test8() {
; EPILOG-BLOCK: outerloop:
; EPILOG-BLOCK-NEXT: %i = phi i64 [ 3, %bb ], [ 0, %outerloop.loopexit.1 ]
; EPILOG-BLOCK-NEXT: %0 = sub i64 100, %i
; EPILOG-BLOCK-NEXT: %1 = sub i64 99, %i
; EPILOG-BLOCK-NEXT: %xtraiter = and i64 %0, 1
; EPILOG-BLOCK-NEXT: %2 = icmp ult i64 %1, 1
; EPILOG-BLOCK-NEXT: br i1 %2, label %exit.unr-lcssa, label %outerloop.new
; EPILOG-BLOCK-NEXT: br i1 false, label %exit.unr-lcssa, label %outerloop.new
; EPILOG-BLOCK: outerloop.new:
; EPILOG-BLOCK-NEXT: %unroll_iter = sub i64 %0, %xtraiter
; EPILOG-BLOCK-NEXT: br label %innerH
Expand Down Expand Up @@ -4711,7 +4709,6 @@ define void @test8() {
; PROLOG-BLOCK: outerloop:
; PROLOG-BLOCK-NEXT: %i = phi i64 [ 3, %bb ], [ 0, %outerloop.loopexit.1 ]
; PROLOG-BLOCK-NEXT: %0 = sub i64 100, %i
; PROLOG-BLOCK-NEXT: %1 = sub i64 99, %i
; PROLOG-BLOCK-NEXT: %xtraiter = and i64 %0, 1
; PROLOG-BLOCK-NEXT: %lcmp.mod = icmp ne i64 %xtraiter, 0
; PROLOG-BLOCK-NEXT: br i1 %lcmp.mod, label %innerH.prol.preheader, label %innerH.prol.loopexit
Expand All @@ -4724,8 +4721,7 @@ define void @test8() {
; PROLOG-BLOCK-NEXT: br label %innerH.prol.loopexit
; PROLOG-BLOCK: innerH.prol.loopexit:
; PROLOG-BLOCK-NEXT: %i3.unr = phi i64 [ %i, %outerloop ], [ %i4.prol, %latch.prol ]
; PROLOG-BLOCK-NEXT: %2 = icmp ult i64 %1, 1
; PROLOG-BLOCK-NEXT: br i1 %2, label %exit.loopexit, label %outerloop.new
; PROLOG-BLOCK-NEXT: br i1 false, label %exit.loopexit, label %outerloop.new
; PROLOG-BLOCK: outerloop.new:
; PROLOG-BLOCK-NEXT: br label %innerH
; PROLOG-BLOCK: innerH:
Expand Down
Loading