1 ; RUN: opt < %s -instsimplify -S | FileCheck %s
2 target datalayout = "p:32:32"
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]* %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]* %a, i32 0, i32 0
37 %y = getelementptr inbounds [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* %x, i64 0, i32 0
52 %b = getelementptr %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* @gepy, i64 0, i32 0
62 %b = getelementptr %gept* @gepy, i64 0, i32 1
63 %equal = icmp eq i32* %a, %b
65 ; CHECK-NEXT: ret i1 false
70 %x = alloca %gept, align 8
71 %a = getelementptr inbounds %gept* %x, i64 0, i32 1
72 %b = getelementptr %gept* @gepy, i64 0, i32 0
73 %equal = icmp eq i32* %a, %b
75 ; CHECK-NEXT: ret i1 false
78 define i1 @gep6(%gept* %x) {
79 ; Same as @gep3 but potentially null.
81 %a = getelementptr %gept* %x, i64 0, i32 0
82 %b = getelementptr %gept* %x, i64 0, i32 1
83 %equal = icmp eq i32* %a, %b
85 ; CHECK-NEXT: ret i1 false
88 define i1 @gep7(%gept* %x) {
90 %a = getelementptr %gept* %x, i64 0, i32 0
91 %b = getelementptr %gept* @gepz, i64 0, i32 0
92 %equal = icmp eq i32* %a, %b
94 ; CHECK: ret i1 %equal
97 define i1 @gep8(%gept* %x) {
99 %a = getelementptr %gept* %x, i32 1
100 %b = getelementptr %gept* %x, i32 -1
101 %equal = icmp ugt %gept* %a, %b
103 ; CHECK: ret i1 %equal
106 define i1 @gep9(i8* %ptr) {
107 ; CHECK-LABEL: @gep9(
112 %first1 = getelementptr inbounds i8* %ptr, i32 0
113 %first2 = getelementptr inbounds i8* %first1, i32 1
114 %first3 = getelementptr inbounds i8* %first2, i32 2
115 %first4 = getelementptr inbounds i8* %first3, i32 4
116 %last1 = getelementptr inbounds i8* %first2, i32 48
117 %last2 = getelementptr inbounds i8* %last1, i32 8
118 %last3 = getelementptr inbounds i8* %last2, i32 -4
119 %last4 = getelementptr inbounds i8* %last3, i32 -4
120 %first.int = ptrtoint i8* %first4 to i32
121 %last.int = ptrtoint i8* %last4 to i32
122 %cmp = icmp ne i32 %last.int, %first.int
126 define i1 @gep10(i8* %ptr) {
127 ; CHECK-LABEL: @gep10(
132 %first1 = getelementptr inbounds i8* %ptr, i32 -2
133 %first2 = getelementptr inbounds i8* %first1, i32 44
134 %last1 = getelementptr inbounds i8* %ptr, i32 48
135 %last2 = getelementptr inbounds i8* %last1, i32 -6
136 %first.int = ptrtoint i8* %first2 to i32
137 %last.int = ptrtoint i8* %last2 to i32
138 %cmp = icmp eq i32 %last.int, %first.int
142 define i1 @gep11(i8* %ptr) {
143 ; CHECK-LABEL: @gep11(
148 %first1 = getelementptr inbounds i8* %ptr, i32 -2
149 %last1 = getelementptr inbounds i8* %ptr, i32 48
150 %last2 = getelementptr inbounds i8* %last1, i32 -6
151 %cmp = icmp ult i8* %first1, %last2
155 define i1 @gep12(i8* %ptr) {
156 ; CHECK-LABEL: @gep12(
161 %first1 = getelementptr inbounds i8* %ptr, i32 -2
162 %last1 = getelementptr inbounds i8* %ptr, i32 48
163 %last2 = getelementptr inbounds i8* %last1, i32 -6
164 %cmp = icmp slt i8* %first1, %last2
168 define i1 @gep13(i8* %ptr) {
169 ; CHECK-LABEL: @gep13(
170 ; We can prove this GEP is non-null because it is inbounds.
171 %x = getelementptr inbounds i8* %ptr, i32 1
172 %cmp = icmp eq i8* %x, null
174 ; CHECK-NEXT: ret i1 false
177 define i1 @gep14({ {}, i8 }* %ptr) {
178 ; CHECK-LABEL: @gep14(
179 ; We can't simplify this because the offset of one in the GEP actually doesn't
181 %x = getelementptr inbounds { {}, i8 }* %ptr, i32 0, i32 1
182 %cmp = icmp eq i8* %x, null
184 ; CHECK-NOT: ret i1 false
187 define i1 @gep15({ {}, [4 x {i8, i8}]}* %ptr, i32 %y) {
188 ; CHECK-LABEL: @gep15(
189 ; We can prove this GEP is non-null even though there is a user value, as we
190 ; would necessarily violate inbounds on one side or the other.
191 %x = getelementptr inbounds { {}, [4 x {i8, i8}]}* %ptr, i32 0, i32 1, i32 %y, i32 1
192 %cmp = icmp eq i8* %x, null
194 ; CHECK-NEXT: ret i1 false
197 define i1 @gep16(i8* %ptr, i32 %a) {
198 ; CHECK-LABEL: @gep16(
199 ; We can prove this GEP is non-null because it is inbounds and because we know
200 ; %b is non-zero even though we don't know its value.
202 %x = getelementptr inbounds i8* %ptr, i32 %b
203 %cmp = icmp eq i8* %x, null
205 ; CHECK-NEXT: ret i1 false
208 define i1 @zext(i32 %x) {
209 ; CHECK-LABEL: @zext(
210 %e1 = zext i32 %x to i64
211 %e2 = zext i32 %x to i64
212 %r = icmp eq i64 %e1, %e2
217 define i1 @zext2(i1 %x) {
218 ; CHECK-LABEL: @zext2(
219 %e = zext i1 %x to i32
220 %c = icmp ne i32 %e, 0
226 ; CHECK-LABEL: @zext3(
227 %e = zext i1 1 to i32
228 %c = icmp ne i32 %e, 0
233 define i1 @sext(i32 %x) {
234 ; CHECK-LABEL: @sext(
235 %e1 = sext i32 %x to i64
236 %e2 = sext i32 %x to i64
237 %r = icmp eq i64 %e1, %e2
242 define i1 @sext2(i1 %x) {
243 ; CHECK-LABEL: @sext2(
244 %e = sext i1 %x to i32
245 %c = icmp ne i32 %e, 0
251 ; CHECK-LABEL: @sext3(
252 %e = sext i1 1 to i32
253 %c = icmp ne i32 %e, 0
258 define i1 @add(i32 %x, i32 %y) {
264 %c = icmp eq i32 %s, 0
266 ; CHECK: ret i1 false
269 define i1 @add2(i8 %x, i8 %y) {
270 ; CHECK-LABEL: @add2(
274 %c = icmp eq i8 %s, 0
276 ; CHECK: ret i1 false
279 define i1 @add3(i8 %x, i8 %y) {
280 ; CHECK-LABEL: @add3(
281 %l = zext i8 %x to i32
282 %r = zext i8 %y to i32
284 %c = icmp eq i32 %s, 0
289 define i1 @add4(i32 %x, i32 %y) {
290 ; CHECK-LABEL: @add4(
291 %z = add nsw i32 %y, 1
292 %s1 = add nsw i32 %x, %y
293 %s2 = add nsw i32 %x, %z
294 %c = icmp slt i32 %s1, %s2
299 define i1 @add5(i32 %x, i32 %y) {
300 ; CHECK-LABEL: @add5(
301 %z = add nuw i32 %y, 1
302 %s1 = add nuw i32 %x, %z
303 %s2 = add nuw i32 %x, %y
304 %c = icmp ugt i32 %s1, %s2
309 define i1 @add6(i64 %A, i64 %B) {
310 ; CHECK-LABEL: @add6(
313 %cmp = icmp eq i64 %s1, %s2
318 define i1 @addpowtwo(i32 %x, i32 %y) {
319 ; CHECK-LABEL: @addpowtwo(
323 %c = icmp eq i32 %s, 0
325 ; CHECK: ret i1 false
328 define i1 @or(i32 %x) {
331 %c = icmp eq i32 %o, 0
333 ; CHECK: ret i1 false
336 define i1 @shl(i32 %x) {
339 %c = icmp eq i32 %s, 0
341 ; CHECK: ret i1 false
344 define i1 @lshr1(i32 %x) {
345 ; CHECK-LABEL: @lshr1(
347 %c = icmp eq i32 %s, 0
349 ; CHECK: ret i1 false
352 define i1 @lshr2(i32 %x) {
353 ; CHECK-LABEL: @lshr2(
355 %c = icmp ugt i32 %s, 8
357 ; CHECK: ret i1 false
360 define i1 @lshr3(i32 %x) {
361 ; CHECK-LABEL: @lshr3(
363 %c = icmp eq i32 %s, 0
368 define i1 @ashr1(i32 %x) {
369 ; CHECK-LABEL: @ashr1(
371 %c = icmp eq i32 %s, 0
373 ; CHECK: ret i1 false
376 define i1 @ashr2(i32 %x) {
377 ; CHECK-LABEL: @ashr2(
379 %c = icmp slt i32 %s, -5
381 ; CHECK: ret i1 false
384 define i1 @ashr3(i32 %x) {
385 ; CHECK-LABEL: @ashr3(
387 %c = icmp eq i32 %s, 0
392 define i1 @select1(i1 %cond) {
393 ; CHECK-LABEL: @select1(
394 %s = select i1 %cond, i32 1, i32 0
395 %c = icmp eq i32 %s, 1
397 ; CHECK: ret i1 %cond
400 define i1 @select2(i1 %cond) {
401 ; CHECK-LABEL: @select2(
402 %x = zext i1 %cond to i32
403 %s = select i1 %cond, i32 %x, i32 0
404 %c = icmp ne i32 %s, 0
406 ; CHECK: ret i1 %cond
409 define i1 @select3(i1 %cond) {
410 ; CHECK-LABEL: @select3(
411 %x = zext i1 %cond to i32
412 %s = select i1 %cond, i32 1, i32 %x
413 %c = icmp ne i32 %s, 0
415 ; CHECK: ret i1 %cond
418 define i1 @select4(i1 %cond) {
419 ; CHECK-LABEL: @select4(
420 %invert = xor i1 %cond, 1
421 %s = select i1 %invert, i32 0, i32 1
422 %c = icmp ne i32 %s, 0
424 ; CHECK: ret i1 %cond
427 define i1 @select5(i32 %x) {
428 ; CHECK-LABEL: @select5(
429 %c = icmp eq i32 %x, 0
430 %s = select i1 %c, i32 1, i32 %x
431 %c2 = icmp eq i32 %s, 0
433 ; CHECK: ret i1 false
436 define i1 @select6(i32 %x) {
437 ; CHECK-LABEL: @select6(
438 %c = icmp sgt i32 %x, 0
439 %s = select i1 %c, i32 %x, i32 4
440 %c2 = icmp eq i32 %s, 0
445 define i1 @urem1(i32 %X, i32 %Y) {
446 ; CHECK-LABEL: @urem1(
448 %B = icmp ult i32 %A, %Y
453 define i1 @urem2(i32 %X, i32 %Y) {
454 ; CHECK-LABEL: @urem2(
456 %B = icmp eq i32 %A, %Y
458 ; CHECK: ret i1 false
461 define i1 @urem3(i32 %X) {
462 ; CHECK-LABEL: @urem3(
464 %B = icmp ult i32 %A, 15
469 define i1 @urem4(i32 %X) {
470 ; CHECK-LABEL: @urem4(
472 %B = icmp ult i32 %A, 10
477 define i1 @urem5(i16 %X, i32 %Y) {
478 ; CHECK-LABEL: @urem5(
479 %A = zext i16 %X to i32
481 %C = icmp slt i32 %B, %Y
483 ; CHECK-NOT: ret i1 true
486 define i1 @urem6(i32 %X, i32 %Y) {
487 ; CHECK-LABEL: @urem6(
489 %B = icmp ugt i32 %Y, %A
494 define i1 @urem7(i32 %X) {
495 ; CHECK-LABEL: @urem7(
497 %B = icmp sgt i32 %A, %X
499 ; CHECK-NOT: ret i1 false
502 define i1 @srem1(i32 %X) {
503 ; CHECK-LABEL: @srem1(
505 %B = icmp sgt i32 %A, 5
507 ; CHECK: ret i1 false
511 ; CHECK-LABEL: @srem2(
512 ; CHECK: ret i1 false
513 define i1 @srem2(i16 %X, i32 %Y) {
514 %A = zext i16 %X to i32
515 %B = add nsw i32 %A, 1
517 %D = icmp slt i32 %C, 0
521 ; CHECK-LABEL: @srem3(
522 ; CHECK-NEXT: ret i1 false
523 define i1 @srem3(i16 %X, i32 %Y) {
524 %A = zext i16 %X to i32
525 %B = or i32 2147483648, %A
526 %C = sub nsw i32 1, %B
528 %E = icmp slt i32 %D, 0
532 define i1 @udiv1(i32 %X) {
533 ; CHECK-LABEL: @udiv1(
534 %A = udiv i32 %X, 1000000
535 %B = icmp ult i32 %A, 5000
540 define i1 @udiv2(i32 %X, i32 %Y, i32 %Z) {
541 ; CHECK-LABEL: @udiv2(
542 %A = udiv exact i32 10, %Z
543 %B = udiv exact i32 20, %Z
544 %C = icmp ult i32 %A, %B
549 define i1 @udiv3(i32 %X, i32 %Y) {
550 ; CHECK-LABEL: @udiv3(
552 %C = icmp ugt i32 %A, %X
554 ; CHECK: ret i1 false
557 define i1 @udiv4(i32 %X, i32 %Y) {
558 ; CHECK-LABEL: @udiv4(
560 %C = icmp ule i32 %A, %X
565 define i1 @udiv5(i32 %X) {
566 ; CHECK-LABEL: @udiv5(
567 %A = udiv i32 123, %X
568 %C = icmp ugt i32 %A, 124
570 ; CHECK: ret i1 false
574 define i1 @udiv6(i32 %X) nounwind {
575 ; CHECK-LABEL: @udiv6(
577 %C = icmp eq i32 %A, 0
583 define i1 @sdiv1(i32 %X) {
584 ; CHECK-LABEL: @sdiv1(
585 %A = sdiv i32 %X, 1000000
586 %B = icmp slt i32 %A, 3000
591 define i1 @or1(i32 %X) {
594 %B = icmp ult i32 %A, 50
596 ; CHECK: ret i1 false
599 define i1 @and1(i32 %X) {
600 ; CHECK-LABEL: @and1(
602 %B = icmp ugt i32 %A, 70
604 ; CHECK: ret i1 false
607 define i1 @mul1(i32 %X) {
608 ; CHECK-LABEL: @mul1(
609 ; Square of a non-zero number is non-zero if there is no overflow.
611 %M = mul nuw i32 %Y, %Y
612 %C = icmp eq i32 %M, 0
614 ; CHECK: ret i1 false
617 define i1 @mul2(i32 %X) {
618 ; CHECK-LABEL: @mul2(
619 ; Square of a non-zero number is positive if there is no signed overflow.
621 %M = mul nsw i32 %Y, %Y
622 %C = icmp sgt i32 %M, 0
627 define i1 @mul3(i32 %X, i32 %Y) {
628 ; CHECK-LABEL: @mul3(
629 ; Product of non-negative numbers is non-negative if there is no signed overflow.
630 %XX = mul nsw i32 %X, %X
631 %YY = mul nsw i32 %Y, %Y
632 %M = mul nsw i32 %XX, %YY
633 %C = icmp sge i32 %M, 0
638 define <2 x i1> @vectorselect1(<2 x i1> %cond) {
639 ; CHECK-LABEL: @vectorselect1(
640 %invert = xor <2 x i1> %cond, <i1 1, i1 1>
641 %s = select <2 x i1> %invert, <2 x i32> <i32 0, i32 0>, <2 x i32> <i32 1, i32 1>
642 %c = icmp ne <2 x i32> %s, <i32 0, i32 0>
644 ; CHECK: ret <2 x i1> %cond
648 define <2 x i1> @vectorselectcrash(i32 %arg1) {
649 %tobool40 = icmp ne i32 %arg1, 0
650 %cond43 = select i1 %tobool40, <2 x i16> <i16 -5, i16 66>, <2 x i16> <i16 46, i16 1>
651 %cmp45 = icmp ugt <2 x i16> %cond43, <i16 73, i16 21>
656 define i1 @alloca_compare(i64 %idx) {
657 %sv = alloca { i32, i32, [124 x i32] }
658 %1 = getelementptr inbounds { i32, i32, [124 x i32] }* %sv, i32 0, i32 2, i64 %idx
659 %2 = icmp eq i32* %1, null
661 ; CHECK: alloca_compare
662 ; CHECK: ret i1 false
666 define i1 @infinite_gep() {
670 %X = getelementptr i32 *%X, i32 1
671 %Y = icmp eq i32* %X, null
675 ; It's not valid to fold a comparison of an argument with an alloca, even though
676 ; that's tempting. An argument can't *alias* an alloca, however the aliasing rule
677 ; relies on restrictions against guessing an object's address and dereferencing.
678 ; There are no restrictions against guessing an object's address and comparing.
680 define i1 @alloca_argument_compare(i64* %arg) {
682 %cmp = icmp eq i64* %arg, %alloc
684 ; CHECK: alloca_argument_compare
688 ; As above, but with the operands reversed.
690 define i1 @alloca_argument_compare_swapped(i64* %arg) {
692 %cmp = icmp eq i64* %alloc, %arg
694 ; CHECK: alloca_argument_compare_swapped
698 ; Don't assume that a noalias argument isn't equal to a global variable's
699 ; address. This is an example where AliasAnalysis' NoAlias concept is
700 ; different from actual pointer inequality.
702 @y = external global i32
703 define zeroext i1 @external_compare(i32* noalias %x) {
704 %cmp = icmp eq i32* %x, @y
706 ; CHECK: external_compare
710 define i1 @alloca_gep(i64 %a, i64 %b) {
711 ; CHECK-LABEL: @alloca_gep(
712 ; We can prove this GEP is non-null because it is inbounds and the pointer
714 %strs = alloca [1000 x [1001 x i8]], align 16
715 %x = getelementptr inbounds [1000 x [1001 x i8]]* %strs, i64 0, i64 %a, i64 %b
716 %cmp = icmp eq i8* %x, null
718 ; CHECK-NEXT: ret i1 false
721 define i1 @non_inbounds_gep_compare(i64* %a) {
722 ; CHECK-LABEL: @non_inbounds_gep_compare(
723 ; Equality compares with non-inbounds GEPs can be folded.
724 %x = getelementptr i64* %a, i64 42
725 %y = getelementptr inbounds i64* %x, i64 -42
726 %z = getelementptr i64* %a, i64 -42
727 %w = getelementptr inbounds i64* %z, i64 42
728 %cmp = icmp eq i64* %y, %w
730 ; CHECK-NEXT: ret i1 true
733 define i1 @non_inbounds_gep_compare2(i64* %a) {
734 ; CHECK-LABEL: @non_inbounds_gep_compare2(
735 ; Equality compares with non-inbounds GEPs can be folded.
736 %x = getelementptr i64* %a, i64 4294967297
737 %y = getelementptr i64* %a, i64 1
738 %cmp = icmp eq i64* %y, %y
740 ; CHECK-NEXT: ret i1 true