Skip to content

Commit 044cc4e

Browse files
committed
[InstSimplify] Improve simplifyICmpWithConstant by using KnownBits info
1 parent 8d80473 commit 044cc4e

File tree

12 files changed

+184
-228
lines changed

12 files changed

+184
-228
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

+5
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,11 @@ ConstantRange computeConstantRange(const Value *V, bool ForSigned,
863863
const DominatorTree *DT = nullptr,
864864
unsigned Depth = 0);
865865

866+
/// Combine constant ranges from computeConstantRange() and computeKnownBits().
867+
ConstantRange
868+
computeConstantRangeIncludingKnownBits(const WithCache<const Value *> &V,
869+
bool ForSigned, const SimplifyQuery &SQ);
870+
866871
/// Return true if this function can prove that the instruction I will
867872
/// always transfer execution to one of its successors (including the next
868873
/// instruction that follows within a basic block). E.g. this is not

llvm/lib/Analysis/InstructionSimplify.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -3005,7 +3005,7 @@ static Value *simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS,
30053005
}
30063006

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

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

3034-
ConstantRange LHS_CR =
3035-
computeConstantRange(LHS, CmpInst::isSigned(Pred), IIQ.UseInstrInfo);
3034+
ConstantRange LHS_CR = llvm::computeConstantRangeIncludingKnownBits(
3035+
LHS, CmpInst::isSigned(Pred), SQ);
30363036
if (!LHS_CR.isFullSet()) {
30373037
if (RHS_CR.contains(LHS_CR))
30383038
return ConstantInt::getTrue(ITy);
@@ -3043,7 +3043,7 @@ static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
30433043
// (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
30443044
// (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
30453045
const APInt *MulC;
3046-
if (IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) &&
3046+
if (SQ.IIQ.UseInstrInfo && ICmpInst::isEquality(Pred) &&
30473047
((match(LHS, m_NUWMul(m_Value(), m_APIntAllowUndef(MulC))) &&
30483048
*MulC != 0 && C->urem(*MulC) != 0) ||
30493049
(match(LHS, m_NSWMul(m_Value(), m_APIntAllowUndef(MulC))) &&
@@ -3749,7 +3749,7 @@ static Value *simplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
37493749
if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
37503750
return V;
37513751

3752-
if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q.IIQ))
3752+
if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q))
37533753
return V;
37543754

37553755
// If both operands have range metadata, use the metadata

llvm/lib/Analysis/ValueTracking.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -6289,10 +6289,10 @@ static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) {
62896289
}
62906290

62916291
/// Combine constant ranges from computeConstantRange() and computeKnownBits().
6292-
static ConstantRange
6293-
computeConstantRangeIncludingKnownBits(const WithCache<const Value *> &V,
6294-
bool ForSigned,
6295-
const SimplifyQuery &SQ) {
6292+
ConstantRange
6293+
llvm::computeConstantRangeIncludingKnownBits(const WithCache<const Value *> &V,
6294+
bool ForSigned,
6295+
const SimplifyQuery &SQ) {
62966296
ConstantRange CR1 =
62976297
ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
62986298
ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);

llvm/test/Analysis/ValueTracking/knownbits-bmi-pattern.ll

+2-10
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,7 @@ define i1 @blsi_ne_is_true(i32 %x) {
335335

336336
define <2 x i1> @blsi_ge_is_false_vec(<2 x i32> %x) {
337337
; CHECK-LABEL: @blsi_ge_is_false_vec(
338-
; CHECK-NEXT: [[X1:%.*]] = or <2 x i32> [[X:%.*]], <i32 10, i32 10>
339-
; CHECK-NEXT: [[X2:%.*]] = sub nsw <2 x i32> zeroinitializer, [[X1]]
340-
; CHECK-NEXT: [[X3:%.*]] = and <2 x i32> [[X2]], [[X]]
341-
; CHECK-NEXT: [[Z:%.*]] = icmp ugt <2 x i32> [[X3]], <i32 7, i32 7>
342-
; CHECK-NEXT: ret <2 x i1> [[Z]]
338+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
343339
;
344340
%x1 = or <2 x i32> %x, <i32 10, i32 10>
345341
%x2 = sub <2 x i32> <i32 0, i32 0>, %x1
@@ -350,11 +346,7 @@ define <2 x i1> @blsi_ge_is_false_vec(<2 x i32> %x) {
350346

351347
define <2 x i1> @blsi_ge_is_false_diff_vec(<2 x i32> %x) {
352348
; CHECK-LABEL: @blsi_ge_is_false_diff_vec(
353-
; CHECK-NEXT: [[X1:%.*]] = or <2 x i32> [[X:%.*]], <i32 10, i32 11>
354-
; CHECK-NEXT: [[X2:%.*]] = sub nsw <2 x i32> zeroinitializer, [[X1]]
355-
; CHECK-NEXT: [[X3:%.*]] = and <2 x i32> [[X2]], [[X]]
356-
; CHECK-NEXT: [[Z:%.*]] = icmp ugt <2 x i32> [[X3]], <i32 7, i32 7>
357-
; CHECK-NEXT: ret <2 x i1> [[Z]]
349+
; CHECK-NEXT: ret <2 x i1> zeroinitializer
358350
;
359351
%x1 = or <2 x i32> %x, <i32 10, i32 11>
360352
%x2 = sub <2 x i32> <i32 0, i32 0>, %x1

llvm/test/Analysis/ValueTracking/monotonic-phi.ll

+3-6
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,7 @@ define i1 @test_shl_zero_start(i8 %n) {
484484
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[A]], [[N:%.*]]
485485
; CHECK-NEXT: br i1 [[CMP1]], label [[EXIT:%.*]], label [[LOOP]]
486486
; CHECK: exit:
487-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[A]], 0
488-
; CHECK-NEXT: ret i1 [[CMP]]
487+
; CHECK-NEXT: ret i1 true
489488
;
490489
entry:
491490
br label %loop
@@ -559,8 +558,7 @@ define i1 @test_lshr_zero_start(i8 %n) {
559558
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[A]], [[N:%.*]]
560559
; CHECK-NEXT: br i1 [[CMP1]], label [[EXIT:%.*]], label [[LOOP]]
561560
; CHECK: exit:
562-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[A]], 0
563-
; CHECK-NEXT: ret i1 [[CMP]]
561+
; CHECK-NEXT: ret i1 true
564562
;
565563
entry:
566564
br label %loop
@@ -633,8 +631,7 @@ define i1 @test_ashr_zero_start(i8 %n) {
633631
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[A]], [[N:%.*]]
634632
; CHECK-NEXT: br i1 [[CMP1]], label [[EXIT:%.*]], label [[LOOP]]
635633
; CHECK: exit:
636-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[A]], 0
637-
; CHECK-NEXT: ret i1 [[CMP]]
634+
; CHECK-NEXT: ret i1 true
638635
;
639636
entry:
640637
br label %loop

llvm/test/Transforms/Attributor/lvi-after-jumpthreading.ll

+1-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ define i32 @test4(i32 %i, i1 %f, i32 %n) {
148148
; CHECK-NEXT: call void @dummy(i1 [[F]]) #[[ATTR2]]
149149
; CHECK-NEXT: [[CONSUME:%.*]] = call i32 @exit()
150150
; CHECK-NEXT: call void @llvm.assume(i1 noundef [[F]])
151-
; CHECK-NEXT: [[COND:%.*]] = icmp eq i1 [[F]], false
152-
; CHECK-NEXT: br i1 [[COND]], label [[EXIT]], label [[CONT:%.*]]
151+
; CHECK-NEXT: br label [[CONT:%.*]]
153152
; CHECK: exit2:
154153
; CHECK-NEXT: ret i32 30
155154
;

llvm/test/Transforms/InstSimplify/call.ll

+2-6
Original file line numberDiff line numberDiff line change
@@ -1582,9 +1582,7 @@ define i1 @ctlz_i1_non_poison_eq_false(i1 %x) {
15821582

15831583
define i1 @ctlz_i1_poison_eq_false(i1 %x) {
15841584
; CHECK-LABEL: @ctlz_i1_poison_eq_false(
1585-
; CHECK-NEXT: [[CT:%.*]] = call i1 @llvm.ctlz.i1(i1 [[X:%.*]], i1 true)
1586-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i1 [[CT]], false
1587-
; CHECK-NEXT: ret i1 [[CMP]]
1585+
; CHECK-NEXT: ret i1 true
15881586
;
15891587
%ct = call i1 @llvm.ctlz.i1(i1 %x, i1 true)
15901588
%cmp = icmp eq i1 %ct, false
@@ -1604,9 +1602,7 @@ define i1 @cttz_i1_non_poison_eq_false(i1 %x) {
16041602

16051603
define i1 @cttz_i1_poison_eq_false(i1 %x) {
16061604
; CHECK-LABEL: @cttz_i1_poison_eq_false(
1607-
; CHECK-NEXT: [[CT:%.*]] = call i1 @llvm.cttz.i1(i1 [[X:%.*]], i1 true)
1608-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i1 [[CT]], false
1609-
; CHECK-NEXT: ret i1 [[CMP]]
1605+
; CHECK-NEXT: ret i1 true
16101606
;
16111607
%ct = call i1 @llvm.cttz.i1(i1 %x, i1 true)
16121608
%cmp = icmp eq i1 %ct, false

llvm/test/Transforms/InstSimplify/icmp-constant.ll

+4-11
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,7 @@ define <2 x i1> @or1_vec_partial_undef(<2 x i32> %X) {
433433
; Single bit OR.
434434
define i1 @or2_true(i8 %x) {
435435
; CHECK-LABEL: @or2_true(
436-
; CHECK-NEXT: [[Y:%.*]] = or i8 [[X:%.*]], 64
437-
; CHECK-NEXT: [[Z:%.*]] = icmp sge i8 [[Y]], -64
438-
; CHECK-NEXT: ret i1 [[Z]]
436+
; CHECK-NEXT: ret i1 true
439437
;
440438
%y = or i8 %x, 64
441439
%z = icmp sge i8 %y, -64
@@ -457,9 +455,7 @@ define i1 @or2_unknown(i8 %x) {
457455
; 78 = 0b01001110; -50 = 0b11001110
458456
define i1 @or3_true(i8 %x) {
459457
; CHECK-LABEL: @or3_true(
460-
; CHECK-NEXT: [[Y:%.*]] = or i8 [[X:%.*]], 78
461-
; CHECK-NEXT: [[Z:%.*]] = icmp sge i8 [[Y]], -50
462-
; CHECK-NEXT: ret i1 [[Z]]
458+
; CHECK-NEXT: ret i1 true
463459
;
464460
%y = or i8 %x, 78
465461
%z = icmp sge i8 %y, -50
@@ -573,9 +569,7 @@ define i1 @and3_unknown1(i8 %x) {
573569

574570
define i1 @and3_true2(i8 %x) {
575571
; CHECK-LABEL: @and3_true2(
576-
; CHECK-NEXT: [[Y:%.*]] = and i8 [[X:%.*]], -75
577-
; CHECK-NEXT: [[Z:%.*]] = icmp sle i8 [[Y]], 53
578-
; CHECK-NEXT: ret i1 [[Z]]
572+
; CHECK-NEXT: ret i1 true
579573
;
580574
%y = and i8 %x, -75
581575
%z = icmp sle i8 %y, 53
@@ -1146,8 +1140,7 @@ define i8 @infer_sub_with_knownbits_info(i8 %a, i8 %b) {
11461140
; CHECK-NEXT: [[A1:%.*]] = or i8 [[A:%.*]], 1
11471141
; CHECK-NEXT: [[A2:%.*]] = shl i8 [[B:%.*]], 1
11481142
; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[A1]], [[A2]]
1149-
; CHECK-NEXT: [[UMAX:%.*]] = tail call i8 @llvm.umax.i8(i8 [[SUB]], i8 1)
1150-
; CHECK-NEXT: ret i8 [[UMAX]]
1143+
; CHECK-NEXT: ret i8 [[SUB]]
11511144
;
11521145
%a1 = or i8 %a, 1
11531146
%a2 = shl i8 %b, 1

llvm/test/Transforms/JumpThreading/pr62908.ll

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55

66
define i32 @test() {
77
; CHECK-LABEL: define i32 @test() {
8-
; CHECK-NEXT: end:
8+
; CHECK-NEXT: entry:
9+
; CHECK-NEXT: br label [[END:%.*]]
10+
; CHECK: unreachable:
11+
; CHECK-NEXT: [[SH_PROM:%.*]] = zext i32 -1 to i64
12+
; CHECK-NEXT: [[SHL:%.*]] = shl nsw i64 -1, [[SH_PROM]]
13+
; CHECK-NEXT: [[CONV:%.*]] = trunc i64 [[SHL]] to i32
14+
; CHECK-NEXT: br label [[END]]
15+
; CHECK: end:
916
; CHECK-NEXT: ret i32 0
1017
;
1118
entry:

llvm/test/Transforms/LoopUnroll/runtime-loop-multiple-exits.ll

+2-6
Original file line numberDiff line numberDiff line change
@@ -4578,10 +4578,8 @@ define void @test8() {
45784578
; EPILOG-BLOCK: outerloop:
45794579
; EPILOG-BLOCK-NEXT: %i = phi i64 [ 3, %bb ], [ 0, %outerloop.loopexit.1 ]
45804580
; EPILOG-BLOCK-NEXT: %0 = sub i64 100, %i
4581-
; EPILOG-BLOCK-NEXT: %1 = sub i64 99, %i
45824581
; EPILOG-BLOCK-NEXT: %xtraiter = and i64 %0, 1
4583-
; EPILOG-BLOCK-NEXT: %2 = icmp ult i64 %1, 1
4584-
; EPILOG-BLOCK-NEXT: br i1 %2, label %exit.unr-lcssa, label %outerloop.new
4582+
; EPILOG-BLOCK-NEXT: br i1 false, label %exit.unr-lcssa, label %outerloop.new
45854583
; EPILOG-BLOCK: outerloop.new:
45864584
; EPILOG-BLOCK-NEXT: %unroll_iter = sub i64 %0, %xtraiter
45874585
; EPILOG-BLOCK-NEXT: br label %innerH
@@ -4711,7 +4709,6 @@ define void @test8() {
47114709
; PROLOG-BLOCK: outerloop:
47124710
; PROLOG-BLOCK-NEXT: %i = phi i64 [ 3, %bb ], [ 0, %outerloop.loopexit.1 ]
47134711
; PROLOG-BLOCK-NEXT: %0 = sub i64 100, %i
4714-
; PROLOG-BLOCK-NEXT: %1 = sub i64 99, %i
47154712
; PROLOG-BLOCK-NEXT: %xtraiter = and i64 %0, 1
47164713
; PROLOG-BLOCK-NEXT: %lcmp.mod = icmp ne i64 %xtraiter, 0
47174714
; PROLOG-BLOCK-NEXT: br i1 %lcmp.mod, label %innerH.prol.preheader, label %innerH.prol.loopexit
@@ -4724,8 +4721,7 @@ define void @test8() {
47244721
; PROLOG-BLOCK-NEXT: br label %innerH.prol.loopexit
47254722
; PROLOG-BLOCK: innerH.prol.loopexit:
47264723
; PROLOG-BLOCK-NEXT: %i3.unr = phi i64 [ %i, %outerloop ], [ %i4.prol, %latch.prol ]
4727-
; PROLOG-BLOCK-NEXT: %2 = icmp ult i64 %1, 1
4728-
; PROLOG-BLOCK-NEXT: br i1 %2, label %exit.loopexit, label %outerloop.new
4724+
; PROLOG-BLOCK-NEXT: br i1 false, label %exit.loopexit, label %outerloop.new
47294725
; PROLOG-BLOCK: outerloop.new:
47304726
; PROLOG-BLOCK-NEXT: br label %innerH
47314727
; PROLOG-BLOCK: innerH:

0 commit comments

Comments
 (0)