1 ; RUN: opt < %s -instsimplify -S | FileCheck %s
2 target datalayout = "p:32:32-p1:64:64"
4 define i1 @ptrtoint() {
5 ; CHECK-LABEL: @ptrtoint(
7 %tmp = ptrtoint i8* %a to i32
8 %r = icmp eq i32 %tmp, 0
13 define i1 @bitcast() {
14 ; CHECK-LABEL: @bitcast(
17 %x = bitcast i32* %a to i8*
18 %y = bitcast i64* %b to i8*
19 %cmp = icmp eq i8* %x, %y
21 ; CHECK-NEXT: ret i1 false
26 %a = alloca [3 x i8], align 8
27 %x = getelementptr inbounds [3 x i8], [3 x i8]* %a, i32 0, i32 0
28 %cmp = icmp eq i8* %x, null
30 ; CHECK-NEXT: ret i1 false
35 %a = alloca [3 x i8], align 8
36 %x = getelementptr inbounds [3 x i8], [3 x i8]* %a, i32 0, i32 0
37 %y = getelementptr inbounds [3 x i8], [3 x i8]* %a, i32 0, i32 0
38 %cmp = icmp eq i8* %x, %y
40 ; CHECK-NEXT: ret i1 true
44 %gept = type { i32, i32 }
45 @gepy = global %gept zeroinitializer, align 8
46 @gepz = extern_weak global %gept
50 %x = alloca %gept, align 8
51 %a = getelementptr %gept, %gept* %x, i64 0, i32 0
52 %b = getelementptr %gept, %gept* %x, i64 0, i32 1
53 %equal = icmp eq i32* %a, %b
55 ; CHECK-NEXT: ret i1 false
60 %x = alloca %gept, align 8
61 %a = getelementptr %gept, %gept* @gepy, i64 0, i32 0
62 %b = getelementptr %gept, %gept* @gepy, i64 0, i32 1
63 %equal = icmp eq i32* %a, %b
65 ; CHECK-NEXT: ret i1 false
68 @a = common global [1 x i32] zeroinitializer, align 4
70 define i1 @PR31262() {
71 ; CHECK-LABEL: @PR31262(
72 ; CHECK-NEXT: ret i1 icmp uge (i32* getelementptr ([1 x i32], [1 x i32]* @a, i32 0, i32 undef), i32* getelementptr inbounds ([1 x i32], [1 x i32]* @a, i32 0, i32 0))
74 %idx = getelementptr inbounds [1 x i32], [1 x i32]* @a, i64 0, i64 undef
75 %cmp = icmp uge i32* %idx, getelementptr inbounds ([1 x i32], [1 x i32]* @a, i32 0, i32 0)
81 %x = alloca %gept, align 8
82 %a = getelementptr inbounds %gept, %gept* %x, i64 0, i32 1
83 %b = getelementptr %gept, %gept* @gepy, i64 0, i32 0
84 %equal = icmp eq i32* %a, %b
86 ; CHECK-NEXT: ret i1 false
89 define i1 @gep6(%gept* %x) {
90 ; Same as @gep3 but potentially null.
92 %a = getelementptr %gept, %gept* %x, i64 0, i32 0
93 %b = getelementptr %gept, %gept* %x, i64 0, i32 1
94 %equal = icmp eq i32* %a, %b
96 ; CHECK-NEXT: ret i1 false
99 define i1 @gep7(%gept* %x) {
100 ; CHECK-LABEL: @gep7(
101 %a = getelementptr %gept, %gept* %x, i64 0, i32 0
102 %b = getelementptr %gept, %gept* @gepz, i64 0, i32 0
103 %equal = icmp eq i32* %a, %b
105 ; CHECK: ret i1 %equal
108 define i1 @gep8(%gept* %x) {
109 ; CHECK-LABEL: @gep8(
110 %a = getelementptr %gept, %gept* %x, i32 1
111 %b = getelementptr %gept, %gept* %x, i32 -1
112 %equal = icmp ugt %gept* %a, %b
114 ; CHECK: ret i1 %equal
117 define i1 @gep9(i8* %ptr) {
118 ; CHECK-LABEL: @gep9(
123 %first1 = getelementptr inbounds i8, i8* %ptr, i32 0
124 %first2 = getelementptr inbounds i8, i8* %first1, i32 1
125 %first3 = getelementptr inbounds i8, i8* %first2, i32 2
126 %first4 = getelementptr inbounds i8, i8* %first3, i32 4
127 %last1 = getelementptr inbounds i8, i8* %first2, i32 48
128 %last2 = getelementptr inbounds i8, i8* %last1, i32 8
129 %last3 = getelementptr inbounds i8, i8* %last2, i32 -4
130 %last4 = getelementptr inbounds i8, i8* %last3, i32 -4
131 %first.int = ptrtoint i8* %first4 to i32
132 %last.int = ptrtoint i8* %last4 to i32
133 %cmp = icmp ne i32 %last.int, %first.int
137 define i1 @gep10(i8* %ptr) {
138 ; CHECK-LABEL: @gep10(
143 %first1 = getelementptr inbounds i8, i8* %ptr, i32 -2
144 %first2 = getelementptr inbounds i8, i8* %first1, i32 44
145 %last1 = getelementptr inbounds i8, i8* %ptr, i32 48
146 %last2 = getelementptr inbounds i8, i8* %last1, i32 -6
147 %first.int = ptrtoint i8* %first2 to i32
148 %last.int = ptrtoint i8* %last2 to i32
149 %cmp = icmp eq i32 %last.int, %first.int
153 define i1 @gep11(i8* %ptr) {
154 ; CHECK-LABEL: @gep11(
159 %first1 = getelementptr inbounds i8, i8* %ptr, i32 -2
160 %last1 = getelementptr inbounds i8, i8* %ptr, i32 48
161 %last2 = getelementptr inbounds i8, i8* %last1, i32 -6
162 %cmp = icmp ult i8* %first1, %last2
166 define i1 @gep12(i8* %ptr) {
167 ; CHECK-LABEL: @gep12(
172 %first1 = getelementptr inbounds i8, i8* %ptr, i32 -2
173 %last1 = getelementptr inbounds i8, i8* %ptr, i32 48
174 %last2 = getelementptr inbounds i8, i8* %last1, i32 -6
175 %cmp = icmp slt i8* %first1, %last2
179 define i1 @gep13(i8* %ptr) {
180 ; CHECK-LABEL: @gep13(
181 ; We can prove this GEP is non-null because it is inbounds.
182 %x = getelementptr inbounds i8, i8* %ptr, i32 1
183 %cmp = icmp eq i8* %x, null
185 ; CHECK-NEXT: ret i1 false
188 define i1 @gep13_no_null_opt(i8* %ptr) #0 {
189 ; We can't prove this GEP is non-null.
190 ; CHECK-LABEL: @gep13_no_null_opt(
191 ; CHECK: getelementptr
194 %x = getelementptr inbounds i8, i8* %ptr, i32 1
195 %cmp = icmp eq i8* %x, null
199 define i1 @gep14({ {}, i8 }* %ptr) {
200 ; CHECK-LABEL: @gep14(
201 ; We can't simplify this because the offset of one in the GEP actually doesn't
203 %x = getelementptr inbounds { {}, i8 }, { {}, i8 }* %ptr, i32 0, i32 1
204 %cmp = icmp eq i8* %x, null
206 ; CHECK-NOT: ret i1 false
209 define i1 @gep15({ {}, [4 x {i8, i8}]}* %ptr, i32 %y) {
210 ; CHECK-LABEL: @gep15(
211 ; We can prove this GEP is non-null even though there is a user value, as we
212 ; would necessarily violate inbounds on one side or the other.
213 %x = getelementptr inbounds { {}, [4 x {i8, i8}]}, { {}, [4 x {i8, i8}]}* %ptr, i32 0, i32 1, i32 %y, i32 1
214 %cmp = icmp eq i8* %x, null
216 ; CHECK-NEXT: ret i1 false
219 define i1 @gep15_no_null_opt({ {}, [4 x {i8, i8}]}* %ptr, i32 %y) #0 {
220 ; We can't prove this GEP is non-null.
221 ; CHECK-LABEL: @gep15_no_null_opt(
222 ; CHECK: getelementptr
225 %x = getelementptr inbounds { {}, [4 x {i8, i8}]}, { {}, [4 x {i8, i8}]}* %ptr, i32 0, i32 1, i32 %y, i32 1
226 %cmp = icmp eq i8* %x, null
230 define i1 @gep16(i8* %ptr, i32 %a) {
231 ; CHECK-LABEL: @gep16(
232 ; We can prove this GEP is non-null because it is inbounds and because we know
233 ; %b is non-zero even though we don't know its value.
235 %x = getelementptr inbounds i8, i8* %ptr, i32 %b
236 %cmp = icmp eq i8* %x, null
238 ; CHECK-NEXT: ret i1 false
241 define i1 @gep16_no_null_opt(i8* %ptr, i32 %a) #0 {
242 ; We can't prove this GEP is non-null.
243 ; CHECK-LABEL: @gep16_no_null_opt(
244 ; CHECK getelementptr inbounds i8, i8* %ptr, i32 %b
245 ; CHECK: %cmp = icmp eq i8* %x, null
246 ; CHECK-NEXT: ret i1 %cmp
248 %x = getelementptr inbounds i8, i8* %ptr, i32 %b
249 %cmp = icmp eq i8* %x, null
254 ; CHECK-LABEL: @gep17(
255 %alloca = alloca i32, align 4
256 %bc = bitcast i32* %alloca to [4 x i8]*
257 %gep1 = getelementptr inbounds i32, i32* %alloca, i32 1
258 %pti1 = ptrtoint i32* %gep1 to i32
259 %gep2 = getelementptr inbounds [4 x i8], [4 x i8]* %bc, i32 0, i32 1
260 %pti2 = ptrtoint i8* %gep2 to i32
261 %cmp = icmp ugt i32 %pti1, %pti2
263 ; CHECK-NEXT: ret i1 true
266 define i1 @zext(i32 %x) {
267 ; CHECK-LABEL: @zext(
268 %e1 = zext i32 %x to i64
269 %e2 = zext i32 %x to i64
270 %r = icmp eq i64 %e1, %e2
275 define i1 @zext2(i1 %x) {
276 ; CHECK-LABEL: @zext2(
277 %e = zext i1 %x to i32
278 %c = icmp ne i32 %e, 0
284 ; CHECK-LABEL: @zext3(
285 %e = zext i1 1 to i32
286 %c = icmp ne i32 %e, 0
291 define i1 @sext(i32 %x) {
292 ; CHECK-LABEL: @sext(
293 %e1 = sext i32 %x to i64
294 %e2 = sext i32 %x to i64
295 %r = icmp eq i64 %e1, %e2
300 define i1 @sext2(i1 %x) {
301 ; CHECK-LABEL: @sext2(
302 %e = sext i1 %x to i32
303 %c = icmp ne i32 %e, 0
309 ; CHECK-LABEL: @sext3(
310 %e = sext i1 1 to i32
311 %c = icmp ne i32 %e, 0
316 define i1 @add(i32 %x, i32 %y) {
322 %c = icmp eq i32 %s, 0
324 ; CHECK: ret i1 false
327 define i1 @add2(i8 %x, i8 %y) {
328 ; CHECK-LABEL: @add2(
332 %c = icmp eq i8 %s, 0
334 ; CHECK: ret i1 false
337 define i1 @add3(i8 %x, i8 %y) {
338 ; CHECK-LABEL: @add3(
339 %l = zext i8 %x to i32
340 %r = zext i8 %y to i32
342 %c = icmp eq i32 %s, 0
347 define i1 @add4(i32 %x, i32 %y) {
348 ; CHECK-LABEL: @add4(
349 %z = add nsw i32 %y, 1
350 %s1 = add nsw i32 %x, %y
351 %s2 = add nsw i32 %x, %z
352 %c = icmp slt i32 %s1, %s2
357 define i1 @add5(i32 %x, i32 %y) {
358 ; CHECK-LABEL: @add5(
359 %z = add nuw i32 %y, 1
360 %s1 = add nuw i32 %x, %z
361 %s2 = add nuw i32 %x, %y
362 %c = icmp ugt i32 %s1, %s2
367 define i1 @add6(i64 %A, i64 %B) {
368 ; CHECK-LABEL: @add6(
371 %cmp = icmp eq i64 %s1, %s2
376 define i1 @addpowtwo(i32 %x, i32 %y) {
377 ; CHECK-LABEL: @addpowtwo(
381 %c = icmp eq i32 %s, 0
383 ; CHECK: ret i1 false
386 define i1 @or(i32 %x) {
389 %c = icmp eq i32 %o, 0
391 ; CHECK: ret i1 false
394 ; Do not simplify if we cannot guarantee that the ConstantExpr is a non-zero
396 @GV = common global i32* null
397 define i1 @or_constexp(i32 %x) {
398 ; CHECK-LABEL: @or_constexp(
400 %0 = and i32 ptrtoint (i32** @GV to i32), 32
402 %c = icmp eq i32 %o, 0
405 ; CHECK-NEXT: icmp eq
406 ; CHECK-NOT: ret i1 false
409 define i1 @shl1(i32 %x) {
410 ; CHECK-LABEL: @shl1(
412 %c = icmp eq i32 %s, 0
414 ; CHECK: ret i1 false
417 define i1 @shl3(i32 %X) {
419 %sub = shl nuw i32 4, %X
420 %cmp = icmp eq i32 %sub, 31
422 ; CHECK-NEXT: ret i1 false
425 define i1 @lshr1(i32 %x) {
426 ; CHECK-LABEL: @lshr1(
428 %c = icmp eq i32 %s, 0
430 ; CHECK: ret i1 false
433 define i1 @lshr3(i32 %x) {
434 ; CHECK-LABEL: @lshr3(
436 %c = icmp eq i32 %s, 0
441 define i1 @lshr4(i32 %X, i32 %Y) {
442 ; CHECK-LABEL: @lshr4(
444 %C = icmp ule i32 %A, %X
449 define i1 @lshr5(i32 %X, i32 %Y) {
450 ; CHECK-LABEL: @lshr5(
452 %C = icmp ugt i32 %A, %X
454 ; CHECK: ret i1 false
457 define i1 @lshr6(i32 %X, i32 %Y) {
458 ; CHECK-LABEL: @lshr6(
460 %C = icmp ult i32 %X, %A
462 ; CHECK: ret i1 false
465 define i1 @lshr7(i32 %X, i32 %Y) {
466 ; CHECK-LABEL: @lshr7(
468 %C = icmp uge i32 %X, %A
473 define i1 @ashr1(i32 %x) {
474 ; CHECK-LABEL: @ashr1(
476 %c = icmp eq i32 %s, 0
478 ; CHECK: ret i1 false
481 define i1 @ashr3(i32 %x) {
482 ; CHECK-LABEL: @ashr3(
484 %c = icmp eq i32 %s, 0
489 define i1 @select1(i1 %cond) {
490 ; CHECK-LABEL: @select1(
491 %s = select i1 %cond, i32 1, i32 0
492 %c = icmp eq i32 %s, 1
494 ; CHECK: ret i1 %cond
497 define i1 @select2(i1 %cond) {
498 ; CHECK-LABEL: @select2(
499 %x = zext i1 %cond to i32
500 %s = select i1 %cond, i32 %x, i32 0
501 %c = icmp ne i32 %s, 0
503 ; CHECK: ret i1 %cond
506 define i1 @select3(i1 %cond) {
507 ; CHECK-LABEL: @select3(
508 %x = zext i1 %cond to i32
509 %s = select i1 %cond, i32 1, i32 %x
510 %c = icmp ne i32 %s, 0
512 ; CHECK: ret i1 %cond
515 define i1 @select4(i1 %cond) {
516 ; CHECK-LABEL: @select4(
517 %invert = xor i1 %cond, 1
518 %s = select i1 %invert, i32 0, i32 1
519 %c = icmp ne i32 %s, 0
521 ; CHECK: ret i1 %cond
524 define i1 @select5(i32 %x) {
525 ; CHECK-LABEL: @select5(
526 %c = icmp eq i32 %x, 0
527 %s = select i1 %c, i32 1, i32 %x
528 %c2 = icmp eq i32 %s, 0
530 ; CHECK: ret i1 false
533 define i1 @select6(i32 %x) {
534 ; CHECK-LABEL: @select6(
535 %c = icmp sgt i32 %x, 0
536 %s = select i1 %c, i32 %x, i32 4
537 %c2 = icmp eq i32 %s, 0
542 define i1 @urem1(i32 %X, i32 %Y) {
543 ; CHECK-LABEL: @urem1(
545 %B = icmp ult i32 %A, %Y
550 define i1 @urem2(i32 %X, i32 %Y) {
551 ; CHECK-LABEL: @urem2(
553 %B = icmp eq i32 %A, %Y
555 ; CHECK: ret i1 false
558 define i1 @urem4(i32 %X) {
559 ; CHECK-LABEL: @urem4(
561 %B = icmp ult i32 %A, 10
566 define i1 @urem5(i16 %X, i32 %Y) {
567 ; CHECK-LABEL: @urem5(
568 %A = zext i16 %X to i32
570 %C = icmp slt i32 %B, %Y
572 ; CHECK-NOT: ret i1 true
575 define i1 @urem6(i32 %X, i32 %Y) {
576 ; CHECK-LABEL: @urem6(
578 %B = icmp ugt i32 %Y, %A
583 define i1 @urem7(i32 %X) {
584 ; CHECK-LABEL: @urem7(
586 %B = icmp sgt i32 %A, %X
588 ; CHECK-NOT: ret i1 false
592 ; CHECK-LABEL: @srem2(
593 ; CHECK: ret i1 false
594 define i1 @srem2(i16 %X, i32 %Y) {
595 %A = zext i16 %X to i32
596 %B = add nsw i32 %A, 1
598 %D = icmp slt i32 %C, 0
602 ; CHECK-LABEL: @srem3(
603 ; CHECK-NEXT: ret i1 false
604 define i1 @srem3(i16 %X, i32 %Y) {
605 %A = zext i16 %X to i32
606 %B = or i32 2147483648, %A
607 %C = sub nsw i32 1, %B
609 %E = icmp slt i32 %D, 0
613 define i1 @udiv2(i32 %Z) {
614 ; CHECK-LABEL: @udiv2(
615 ; CHECK-NEXT: ret i1 true
617 %A = udiv exact i32 10, %Z
618 %B = udiv exact i32 20, %Z
619 %C = icmp ult i32 %A, %B
623 ; Exact sdiv and equality preds can simplify.
625 define i1 @sdiv_exact_equality(i32 %Z) {
626 ; CHECK-LABEL: @sdiv_exact_equality(
627 ; CHECK-NEXT: ret i1 false
629 %A = sdiv exact i32 10, %Z
630 %B = sdiv exact i32 20, %Z
631 %C = icmp eq i32 %A, %B
635 ; But not other preds: PR32949 - https://bugs.llvm.org/show_bug.cgi?id=32949
637 define i1 @sdiv_exact_not_equality(i32 %Z) {
638 ; CHECK-LABEL: @sdiv_exact_not_equality(
639 ; CHECK-NEXT: [[A:%.*]] = sdiv exact i32 10, %Z
640 ; CHECK-NEXT: [[B:%.*]] = sdiv exact i32 20, %Z
641 ; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[A]], [[B]]
642 ; CHECK-NEXT: ret i1 [[C]]
644 %A = sdiv exact i32 10, %Z
645 %B = sdiv exact i32 20, %Z
646 %C = icmp ult i32 %A, %B
650 define i1 @udiv3(i32 %X, i32 %Y) {
651 ; CHECK-LABEL: @udiv3(
653 %C = icmp ugt i32 %A, %X
655 ; CHECK: ret i1 false
658 define i1 @udiv4(i32 %X, i32 %Y) {
659 ; CHECK-LABEL: @udiv4(
661 %C = icmp ule i32 %A, %X
667 define i1 @udiv6(i32 %X) nounwind {
668 ; CHECK-LABEL: @udiv6(
670 %C = icmp eq i32 %A, 0
675 define i1 @udiv7(i32 %X, i32 %Y) {
676 ; CHECK-LABEL: @udiv7(
678 %C = icmp ult i32 %X, %A
680 ; CHECK: ret i1 false
683 define i1 @udiv8(i32 %X, i32 %Y) {
684 ; CHECK-LABEL: @udiv8(
686 %C = icmp uge i32 %X, %A
691 define i1 @mul1(i32 %X) {
692 ; CHECK-LABEL: @mul1(
693 ; Square of a non-zero number is non-zero if there is no overflow.
695 %M = mul nuw i32 %Y, %Y
696 %C = icmp eq i32 %M, 0
698 ; CHECK: ret i1 false
701 define i1 @mul2(i32 %X) {
702 ; CHECK-LABEL: @mul2(
703 ; Square of a non-zero number is positive if there is no signed overflow.
705 %M = mul nsw i32 %Y, %Y
706 %C = icmp sgt i32 %M, 0
711 define i1 @mul3(i32 %X, i32 %Y) {
712 ; CHECK-LABEL: @mul3(
713 ; Product of non-negative numbers is non-negative if there is no signed overflow.
714 %XX = mul nsw i32 %X, %X
715 %YY = mul nsw i32 %Y, %Y
716 %M = mul nsw i32 %XX, %YY
717 %C = icmp sge i32 %M, 0
722 define <2 x i1> @vectorselect1(<2 x i1> %cond) {
723 ; CHECK-LABEL: @vectorselect1(
724 %invert = xor <2 x i1> %cond, <i1 1, i1 1>
725 %s = select <2 x i1> %invert, <2 x i32> <i32 0, i32 0>, <2 x i32> <i32 1, i32 1>
726 %c = icmp ne <2 x i32> %s, <i32 0, i32 0>
728 ; CHECK: ret <2 x i1> %cond
732 define <2 x i1> @vectorselectcrash(i32 %arg1) {
733 %tobool40 = icmp ne i32 %arg1, 0
734 %cond43 = select i1 %tobool40, <2 x i16> <i16 -5, i16 66>, <2 x i16> <i16 46, i16 1>
735 %cmp45 = icmp ugt <2 x i16> %cond43, <i16 73, i16 21>
740 define i1 @alloca_compare(i64 %idx) {
741 %sv = alloca { i32, i32, [124 x i32] }
742 %1 = getelementptr inbounds { i32, i32, [124 x i32] }, { i32, i32, [124 x i32] }* %sv, i32 0, i32 2, i64 %idx
743 %2 = icmp eq i32* %1, null
745 ; CHECK: alloca_compare
746 ; CHECK: ret i1 false
749 define i1 @alloca_compare_no_null_opt(i64 %idx) #0 {
750 ; CHECK-LABEL: alloca_compare_no_null_opt(
751 ; CHECK: %sv = alloca { i32, i32, [124 x i32] }
752 ; CHECK: %cmp = getelementptr inbounds { i32, i32, [124 x i32] }, { i32, i32, [124 x i32] }* %sv, i32 0, i32 2, i64 %idx
753 ; CHECK: %X = icmp eq i32* %cmp, null
755 %sv = alloca { i32, i32, [124 x i32] }
756 %cmp = getelementptr inbounds { i32, i32, [124 x i32] }, { i32, i32, [124 x i32] }* %sv, i32 0, i32 2, i64 %idx
757 %X = icmp eq i32* %cmp, null
761 define i1 @infinite_gep() {
765 %X = getelementptr i32, i32 *%X, i32 1
766 %Y = icmp eq i32* %X, null
770 ; It's not valid to fold a comparison of an argument with an alloca, even though
771 ; that's tempting. An argument can't *alias* an alloca, however the aliasing rule
772 ; relies on restrictions against guessing an object's address and dereferencing.
773 ; There are no restrictions against guessing an object's address and comparing.
775 define i1 @alloca_argument_compare(i64* %arg) {
777 %cmp = icmp eq i64* %arg, %alloc
779 ; CHECK: alloca_argument_compare
783 ; As above, but with the operands reversed.
785 define i1 @alloca_argument_compare_swapped(i64* %arg) {
787 %cmp = icmp eq i64* %alloc, %arg
789 ; CHECK: alloca_argument_compare_swapped
793 ; Don't assume that a noalias argument isn't equal to a global variable's
794 ; address. This is an example where AliasAnalysis' NoAlias concept is
795 ; different from actual pointer inequality.
797 @y = external global i32
798 define zeroext i1 @external_compare(i32* noalias %x) {
799 %cmp = icmp eq i32* %x, @y
801 ; CHECK: external_compare
805 define i1 @alloca_gep(i64 %a, i64 %b) {
806 ; CHECK-LABEL: @alloca_gep(
807 ; We can prove this GEP is non-null because it is inbounds and the pointer
809 %strs = alloca [1000 x [1001 x i8]], align 16
810 %x = getelementptr inbounds [1000 x [1001 x i8]], [1000 x [1001 x i8]]* %strs, i64 0, i64 %a, i64 %b
811 %cmp = icmp eq i8* %x, null
813 ; CHECK-NEXT: ret i1 false
816 define i1 @alloca_gep_no_null_opt(i64 %a, i64 %b) #0 {
817 ; CHECK-LABEL: @alloca_gep_no_null_opt(
818 ; We can't prove this GEP is non-null.
820 ; CHECK: getelementptr
823 %strs = alloca [1000 x [1001 x i8]], align 16
824 %x = getelementptr inbounds [1000 x [1001 x i8]], [1000 x [1001 x i8]]* %strs, i64 0, i64 %a, i64 %b
825 %cmp = icmp eq i8* %x, null
829 define i1 @non_inbounds_gep_compare(i64* %a) {
830 ; CHECK-LABEL: @non_inbounds_gep_compare(
831 ; Equality compares with non-inbounds GEPs can be folded.
832 %x = getelementptr i64, i64* %a, i64 42
833 %y = getelementptr inbounds i64, i64* %x, i64 -42
834 %z = getelementptr i64, i64* %a, i64 -42
835 %w = getelementptr inbounds i64, i64* %z, i64 42
836 %cmp = icmp eq i64* %y, %w
838 ; CHECK-NEXT: ret i1 true
841 define i1 @non_inbounds_gep_compare2(i64* %a) {
842 ; CHECK-LABEL: @non_inbounds_gep_compare2(
843 ; Equality compares with non-inbounds GEPs can be folded.
844 %x = getelementptr i64, i64* %a, i64 4294967297
845 %y = getelementptr i64, i64* %a, i64 1
846 %cmp = icmp eq i64* %y, %y
848 ; CHECK-NEXT: ret i1 true
851 define i1 @compare_always_true_slt(i16 %a) {
852 %1 = zext i16 %a to i32
853 %2 = sub nsw i32 0, %1
854 %3 = icmp slt i32 %2, 1
857 ; CHECK-LABEL: @compare_always_true_slt
858 ; CHECK-NEXT: ret i1 true
861 define i1 @compare_always_true_sle(i16 %a) {
862 %1 = zext i16 %a to i32
863 %2 = sub nsw i32 0, %1
864 %3 = icmp sle i32 %2, 0
867 ; CHECK-LABEL: @compare_always_true_sle
868 ; CHECK-NEXT: ret i1 true
871 define i1 @compare_always_false_sgt(i16 %a) {
872 %1 = zext i16 %a to i32
873 %2 = sub nsw i32 0, %1
874 %3 = icmp sgt i32 %2, 0
877 ; CHECK-LABEL: @compare_always_false_sgt
878 ; CHECK-NEXT: ret i1 false
881 define i1 @compare_always_false_sge(i16 %a) {
882 %1 = zext i16 %a to i32
883 %2 = sub nsw i32 0, %1
884 %3 = icmp sge i32 %2, 1
887 ; CHECK-LABEL: @compare_always_false_sge
888 ; CHECK-NEXT: ret i1 false
891 define i1 @compare_always_false_eq(i16 %a) {
892 %1 = zext i16 %a to i32
893 %2 = sub nsw i32 0, %1
894 %3 = icmp eq i32 %2, 1
897 ; CHECK-LABEL: @compare_always_false_eq
898 ; CHECK-NEXT: ret i1 false
901 define i1 @compare_always_false_ne(i16 %a) {
902 %1 = zext i16 %a to i32
903 %2 = sub nsw i32 0, %1
904 %3 = icmp ne i32 %2, 1
907 ; CHECK-LABEL: @compare_always_false_ne
908 ; CHECK-NEXT: ret i1 true
911 define i1 @lshr_ugt_false(i32 %a) {
912 %shr = lshr i32 1, %a
913 %cmp = icmp ugt i32 %shr, 1
915 ; CHECK-LABEL: @lshr_ugt_false
916 ; CHECK-NEXT: ret i1 false
919 define i1 @nonnull_arg(i32* nonnull %i) {
920 %cmp = icmp eq i32* %i, null
922 ; CHECK-LABEL: @nonnull_arg
923 ; CHECK: ret i1 false
926 define i1 @nonnull_arg_no_null_opt(i32* nonnull %i) #0 {
927 %cmp = icmp eq i32* %i, null
929 ; CHECK-LABEL: @nonnull_arg_no_null_opt
930 ; CHECK: ret i1 false
933 define i1 @nonnull_deref_arg(i32* dereferenceable(4) %i) {
934 %cmp = icmp eq i32* %i, null
936 ; CHECK-LABEL: @nonnull_deref_arg
937 ; CHECK: ret i1 false
940 define i1 @nonnull_deref_arg_no_null_opt(i32* dereferenceable(4) %i) #0 {
941 %cmp = icmp eq i32* %i, null
943 ; CHECK-LABEL: @nonnull_deref_arg_no_null_opt
947 define i1 @nonnull_deref_as_arg(i32 addrspace(1)* dereferenceable(4) %i) {
948 %cmp = icmp eq i32 addrspace(1)* %i, null
950 ; CHECK-LABEL: @nonnull_deref_as_arg
955 declare nonnull i32* @returns_nonnull_helper()
956 define i1 @returns_nonnull() {
957 %call = call nonnull i32* @returns_nonnull_helper()
958 %cmp = icmp eq i32* %call, null
960 ; CHECK-LABEL: @returns_nonnull
961 ; CHECK: ret i1 false
964 declare dereferenceable(4) i32* @returns_nonnull_deref_helper()
965 define i1 @returns_nonnull_deref() {
966 %call = call dereferenceable(4) i32* @returns_nonnull_deref_helper()
967 %cmp = icmp eq i32* %call, null
969 ; CHECK-LABEL: @returns_nonnull_deref
970 ; CHECK: ret i1 false
973 define i1 @returns_nonnull_deref_no_null_opt () #0 {
974 %call = call dereferenceable(4) i32* @returns_nonnull_deref_helper()
975 %cmp = icmp eq i32* %call, null
977 ; CHECK-LABEL: @returns_nonnull_deref_no_null_opt
982 declare dereferenceable(4) i32 addrspace(1)* @returns_nonnull_deref_as_helper()
983 define i1 @returns_nonnull_as_deref() {
984 %call = call dereferenceable(4) i32 addrspace(1)* @returns_nonnull_deref_as_helper()
985 %cmp = icmp eq i32 addrspace(1)* %call, null
987 ; CHECK-LABEL: @returns_nonnull_as_deref
992 define i1 @nonnull_load(i32** %addr) {
993 %ptr = load i32*, i32** %addr, !nonnull !{}
994 %cmp = icmp eq i32* %ptr, null
996 ; CHECK-LABEL: @nonnull_load
997 ; CHECK: ret i1 false
1000 define i1 @nonnull_load_as_outer(i32* addrspace(1)* %addr) {
1001 %ptr = load i32*, i32* addrspace(1)* %addr, !nonnull !{}
1002 %cmp = icmp eq i32* %ptr, null
1004 ; CHECK-LABEL: @nonnull_load_as_outer
1005 ; CHECK: ret i1 false
1007 define i1 @nonnull_load_as_inner(i32 addrspace(1)** %addr) {
1008 %ptr = load i32 addrspace(1)*, i32 addrspace(1)** %addr, !nonnull !{}
1009 %cmp = icmp eq i32 addrspace(1)* %ptr, null
1011 ; CHECK-LABEL: @nonnull_load_as_inner
1012 ; CHECK: ret i1 false
1015 ; If a bit is known to be zero for A and known to be one for B,
1016 ; then A and B cannot be equal.
1017 define i1 @icmp_eq_const(i32 %a) {
1018 ; CHECK-LABEL: @icmp_eq_const(
1019 ; CHECK-NEXT: ret i1 false
1021 %b = mul nsw i32 %a, -2
1022 %c = icmp eq i32 %b, 1
1026 define <2 x i1> @icmp_eq_const_vec(<2 x i32> %a) {
1027 ; CHECK-LABEL: @icmp_eq_const_vec(
1028 ; CHECK-NEXT: ret <2 x i1> zeroinitializer
1030 %b = mul nsw <2 x i32> %a, <i32 -2, i32 -2>
1031 %c = icmp eq <2 x i32> %b, <i32 1, i32 1>
1035 define i1 @icmp_ne_const(i32 %a) {
1036 ; CHECK-LABEL: @icmp_ne_const(
1037 ; CHECK-NEXT: ret i1 true
1039 %b = mul nsw i32 %a, -2
1040 %c = icmp ne i32 %b, 1
1044 define <2 x i1> @icmp_ne_const_vec(<2 x i32> %a) {
1045 ; CHECK-LABEL: @icmp_ne_const_vec(
1046 ; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
1048 %b = mul nsw <2 x i32> %a, <i32 -2, i32 -2>
1049 %c = icmp ne <2 x i32> %b, <i32 1, i32 1>
1053 define i1 @icmp_sdiv_int_min(i32 %a) {
1054 %div = sdiv i32 -2147483648, %a
1055 %cmp = icmp ne i32 %div, -1073741824
1058 ; CHECK-LABEL: @icmp_sdiv_int_min
1059 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 -2147483648, %a
1060 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[DIV]], -1073741824
1061 ; CHECK-NEXT: ret i1 [[CMP]]
1064 define i1 @icmp_sdiv_pr20288(i64 %a) {
1065 %div = sdiv i64 %a, -8589934592
1066 %cmp = icmp ne i64 %div, 1073741824
1069 ; CHECK-LABEL: @icmp_sdiv_pr20288
1070 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i64 %a, -8589934592
1071 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i64 [[DIV]], 1073741824
1072 ; CHECK-NEXT: ret i1 [[CMP]]
1075 define i1 @icmp_sdiv_neg1(i64 %a) {
1076 %div = sdiv i64 %a, -1
1077 %cmp = icmp ne i64 %div, 1073741824
1080 ; CHECK-LABEL: @icmp_sdiv_neg1
1081 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i64 %a, -1
1082 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i64 [[DIV]], 1073741824
1083 ; CHECK-NEXT: ret i1 [[CMP]]
1086 define i1 @icmp_known_bits(i4 %x, i4 %y) {
1087 %and1 = and i4 %y, -7
1088 %and2 = and i4 %x, -7
1089 %or1 = or i4 %and1, 2
1090 %or2 = or i4 %and2, 2
1091 %add = add i4 %or1, %or2
1092 %cmp = icmp eq i4 %add, 0
1095 ; CHECK-LABEL: @icmp_known_bits
1096 ; CHECK-NEXT: ret i1 false
1099 define i1 @icmp_shl_nuw_1(i64 %a) {
1100 %shl = shl nuw i64 1, %a
1101 %cmp = icmp ne i64 %shl, 0
1104 ; CHECK-LABEL: @icmp_shl_nuw_1
1105 ; CHECK-NEXT: ret i1 true
1108 define i1 @icmp_shl_1_V_ugt_2147483648(i32 %V) {
1109 %shl = shl i32 1, %V
1110 %cmp = icmp ugt i32 %shl, 2147483648
1113 ; CHECK-LABEL: @icmp_shl_1_V_ugt_2147483648(
1114 ; CHECK-NEXT: ret i1 false
1117 define i1 @icmp_shl_1_V_ule_2147483648(i32 %V) {
1118 %shl = shl i32 1, %V
1119 %cmp = icmp ule i32 %shl, 2147483648
1122 ; CHECK-LABEL: @icmp_shl_1_V_ule_2147483648(
1123 ; CHECK-NEXT: ret i1 true
1126 define i1 @icmp_shl_1_V_eq_31(i32 %V) {
1127 %shl = shl i32 1, %V
1128 %cmp = icmp eq i32 %shl, 31
1131 ; CHECK-LABEL: @icmp_shl_1_V_eq_31(
1132 ; CHECK-NEXT: ret i1 false
1135 define i1 @icmp_shl_1_V_ne_31(i32 %V) {
1136 %shl = shl i32 1, %V
1137 %cmp = icmp ne i32 %shl, 31
1140 ; CHECK-LABEL: @icmp_shl_1_V_ne_31(
1141 ; CHECK-NEXT: ret i1 true
1144 define i1 @tautological1(i32 %A, i32 %B) {
1146 %D = icmp ugt i32 %C, %A
1148 ; CHECK-LABEL: @tautological1(
1149 ; CHECK: ret i1 false
1152 define i1 @tautological2(i32 %A, i32 %B) {
1154 %D = icmp ule i32 %C, %A
1156 ; CHECK-LABEL: @tautological2(
1157 ; CHECK: ret i1 true
1160 define i1 @tautological3(i32 %A, i32 %B) {
1162 %D = icmp ule i32 %A, %C
1164 ; CHECK-LABEL: @tautological3(
1165 ; CHECK: ret i1 true
1168 define i1 @tautological4(i32 %A, i32 %B) {
1170 %D = icmp ugt i32 %A, %C
1172 ; CHECK-LABEL: @tautological4(
1173 ; CHECK: ret i1 false
1176 define i1 @tautological5(i32 %A, i32 %B) {
1178 %D = icmp ult i32 %C, %A
1180 ; CHECK-LABEL: @tautological5(
1181 ; CHECK: ret i1 false
1184 define i1 @tautological6(i32 %A, i32 %B) {
1186 %D = icmp uge i32 %C, %A
1188 ; CHECK-LABEL: @tautological6(
1189 ; CHECK: ret i1 true
1192 define i1 @tautological7(i32 %A, i32 %B) {
1194 %D = icmp uge i32 %A, %C
1196 ; CHECK-LABEL: @tautological7(
1197 ; CHECK: ret i1 true
1200 define i1 @tautological8(i32 %A, i32 %B) {
1202 %D = icmp ult i32 %A, %C
1204 ; CHECK-LABEL: @tautological8(
1205 ; CHECK: ret i1 false
1208 declare void @helper_i1(i1)
1209 ; Series of tests for icmp s[lt|ge] (or A, B), A and icmp s[gt|le] A, (or A, B)
1210 define void @icmp_slt_sge_or(i32 %Ax, i32 %Bx) {
1211 ; 'p' for positive, 'n' for negative, 'x' for potentially either.
1212 ; %D is 'icmp slt (or A, B), A'
1213 ; %E is 'icmp sge (or A, B), A' making it the not of %D
1214 ; %F is 'icmp sgt A, (or A, B)' making it the same as %D
1215 ; %G is 'icmp sle A, (or A, B)' making it the not of %D
1216 %Aneg = or i32 %Ax, 2147483648
1217 %Apos = and i32 %Ax, 2147483647
1218 %Bneg = or i32 %Bx, 2147483648
1219 %Bpos = and i32 %Bx, 2147483647
1221 %Cpp = or i32 %Apos, %Bpos
1222 %Dpp = icmp slt i32 %Cpp, %Apos
1223 %Epp = icmp sge i32 %Cpp, %Apos
1224 %Fpp = icmp sgt i32 %Apos, %Cpp
1225 %Gpp = icmp sle i32 %Apos, %Cpp
1226 %Cpx = or i32 %Apos, %Bx
1227 %Dpx = icmp slt i32 %Cpx, %Apos
1228 %Epx = icmp sge i32 %Cpx, %Apos
1229 %Fpx = icmp sgt i32 %Apos, %Cpx
1230 %Gpx = icmp sle i32 %Apos, %Cpx
1231 %Cpn = or i32 %Apos, %Bneg
1232 %Dpn = icmp slt i32 %Cpn, %Apos
1233 %Epn = icmp sge i32 %Cpn, %Apos
1234 %Fpn = icmp sgt i32 %Apos, %Cpn
1235 %Gpn = icmp sle i32 %Apos, %Cpn
1237 %Cxp = or i32 %Ax, %Bpos
1238 %Dxp = icmp slt i32 %Cxp, %Ax
1239 %Exp = icmp sge i32 %Cxp, %Ax
1240 %Fxp = icmp sgt i32 %Ax, %Cxp
1241 %Gxp = icmp sle i32 %Ax, %Cxp
1242 %Cxx = or i32 %Ax, %Bx
1243 %Dxx = icmp slt i32 %Cxx, %Ax
1244 %Exx = icmp sge i32 %Cxx, %Ax
1245 %Fxx = icmp sgt i32 %Ax, %Cxx
1246 %Gxx = icmp sle i32 %Ax, %Cxx
1247 %Cxn = or i32 %Ax, %Bneg
1248 %Dxn = icmp slt i32 %Cxn, %Ax
1249 %Exn = icmp sge i32 %Cxn, %Ax
1250 %Fxn = icmp sgt i32 %Ax, %Cxn
1251 %Gxn = icmp sle i32 %Ax, %Cxn
1253 %Cnp = or i32 %Aneg, %Bpos
1254 %Dnp = icmp slt i32 %Cnp, %Aneg
1255 %Enp = icmp sge i32 %Cnp, %Aneg
1256 %Fnp = icmp sgt i32 %Aneg, %Cnp
1257 %Gnp = icmp sle i32 %Aneg, %Cnp
1258 %Cnx = or i32 %Aneg, %Bx
1259 %Dnx = icmp slt i32 %Cnx, %Aneg
1260 %Enx = icmp sge i32 %Cnx, %Aneg
1261 %Fnx = icmp sgt i32 %Aneg, %Cnx
1262 %Gnx = icmp sle i32 %Aneg, %Cnx
1263 %Cnn = or i32 %Aneg, %Bneg
1264 %Dnn = icmp slt i32 %Cnn, %Aneg
1265 %Enn = icmp sge i32 %Cnn, %Aneg
1266 %Fnn = icmp sgt i32 %Aneg, %Cnn
1267 %Gnn = icmp sle i32 %Aneg, %Cnn
1269 call void @helper_i1(i1 %Dpp)
1270 call void @helper_i1(i1 %Epp)
1271 call void @helper_i1(i1 %Fpp)
1272 call void @helper_i1(i1 %Gpp)
1273 call void @helper_i1(i1 %Dpx)
1274 call void @helper_i1(i1 %Epx)
1275 call void @helper_i1(i1 %Fpx)
1276 call void @helper_i1(i1 %Gpx)
1277 call void @helper_i1(i1 %Dpn)
1278 call void @helper_i1(i1 %Epn)
1279 call void @helper_i1(i1 %Fpn)
1280 call void @helper_i1(i1 %Gpn)
1281 call void @helper_i1(i1 %Dxp)
1282 call void @helper_i1(i1 %Exp)
1283 call void @helper_i1(i1 %Fxp)
1284 call void @helper_i1(i1 %Gxp)
1285 call void @helper_i1(i1 %Dxx)
1286 call void @helper_i1(i1 %Exx)
1287 call void @helper_i1(i1 %Fxx)
1288 call void @helper_i1(i1 %Gxx)
1289 call void @helper_i1(i1 %Dxn)
1290 call void @helper_i1(i1 %Exn)
1291 call void @helper_i1(i1 %Fxn)
1292 call void @helper_i1(i1 %Gxn)
1293 call void @helper_i1(i1 %Dnp)
1294 call void @helper_i1(i1 %Enp)
1295 call void @helper_i1(i1 %Fnp)
1296 call void @helper_i1(i1 %Gnp)
1297 call void @helper_i1(i1 %Dnx)
1298 call void @helper_i1(i1 %Enx)
1299 call void @helper_i1(i1 %Fnx)
1300 call void @helper_i1(i1 %Gnx)
1301 call void @helper_i1(i1 %Dnn)
1302 call void @helper_i1(i1 %Enn)
1303 call void @helper_i1(i1 %Fnn)
1304 call void @helper_i1(i1 %Gnn)
1305 ; CHECK-LABEL: @icmp_slt_sge_or
1306 ; CHECK: call void @helper_i1(i1 false)
1307 ; CHECK: call void @helper_i1(i1 true)
1308 ; CHECK: call void @helper_i1(i1 false)
1309 ; CHECK: call void @helper_i1(i1 true)
1310 ; CHECK: call void @helper_i1(i1 %Dpx)
1311 ; CHECK: call void @helper_i1(i1 %Epx)
1312 ; CHECK: call void @helper_i1(i1 %Fpx)
1313 ; CHECK: call void @helper_i1(i1 %Gpx)
1314 ; CHECK: call void @helper_i1(i1 true)
1315 ; CHECK: call void @helper_i1(i1 false)
1316 ; CHECK: call void @helper_i1(i1 true)
1317 ; CHECK: call void @helper_i1(i1 false)
1318 ; CHECK: call void @helper_i1(i1 false)
1319 ; CHECK: call void @helper_i1(i1 true)
1320 ; CHECK: call void @helper_i1(i1 false)
1321 ; CHECK: call void @helper_i1(i1 true)
1322 ; CHECK: call void @helper_i1(i1 %Dxx)
1323 ; CHECK: call void @helper_i1(i1 %Exx)
1324 ; CHECK: call void @helper_i1(i1 %Fxx)
1325 ; CHECK: call void @helper_i1(i1 %Gxx)
1326 ; CHECK: call void @helper_i1(i1 %Dxn)
1327 ; CHECK: call void @helper_i1(i1 %Exn)
1328 ; CHECK: call void @helper_i1(i1 %Fxn)
1329 ; CHECK: call void @helper_i1(i1 %Gxn)
1330 ; CHECK: call void @helper_i1(i1 false)
1331 ; CHECK: call void @helper_i1(i1 true)
1332 ; CHECK: call void @helper_i1(i1 false)
1333 ; CHECK: call void @helper_i1(i1 true)
1334 ; CHECK: call void @helper_i1(i1 false)
1335 ; CHECK: call void @helper_i1(i1 true)
1336 ; CHECK: call void @helper_i1(i1 false)
1337 ; CHECK: call void @helper_i1(i1 true)
1338 ; CHECK: call void @helper_i1(i1 false)
1339 ; CHECK: call void @helper_i1(i1 true)
1340 ; CHECK: call void @helper_i1(i1 false)
1341 ; CHECK: call void @helper_i1(i1 true)
1345 define i1 @constant_fold_inttoptr_null() {
1346 ; CHECK-LABEL: @constant_fold_inttoptr_null(
1347 ; CHECK-NEXT: ret i1 false
1349 %x = icmp eq i32* inttoptr (i64 32 to i32*), null
1353 define i1 @constant_fold_null_inttoptr() {
1354 ; CHECK-LABEL: @constant_fold_null_inttoptr(
1355 ; CHECK-NEXT: ret i1 false
1357 %x = icmp eq i32* null, inttoptr (i64 32 to i32*)
1361 ; CHECK-LABEL: @cmp_through_addrspacecast(
1362 ; CHECK-NEXT: ret i1 true
1363 define i1 @cmp_through_addrspacecast(i32 addrspace(1)* %p1) {
1364 %p0 = addrspacecast i32 addrspace(1)* %p1 to i32*
1365 %p0.1 = getelementptr inbounds i32, i32* %p0, i64 1
1366 %cmp = icmp ne i32* %p0, %p0.1
1370 attributes #0 = { "null-pointer-is-valid"="true" }