1 ; RUN: opt < %s -instsimplify -S | FileCheck %s
2 target datalayout = "p:32:32"
4 define i1 @ptrtoint() {
7 %tmp = ptrtoint i8* %a to i32
8 %r = icmp eq i32 %tmp, 0
13 define i1 @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) {
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) {
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) {
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) {
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) {
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) {
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) {
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) {
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) {
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) {
219 %e = zext i1 %x to i32
220 %c = icmp ne i32 %e, 0
227 %e = zext i1 1 to i32
228 %c = icmp ne i32 %e, 0
233 define i1 @sext(i32 %x) {
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) {
244 %e = sext i1 %x to i32
245 %c = icmp ne i32 %e, 0
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) {
274 %c = icmp eq i8 %s, 0
276 ; CHECK: ret i1 false
279 define i1 @add3(i8 %x, i8 %y) {
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) {
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) {
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) {
313 %cmp = icmp eq i64 %s1, %s2
318 define i1 @addpowtwo(i32 %x, i32 %y) {
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) {
347 %c = icmp eq i32 %s, 0
349 ; CHECK: ret i1 false
352 define i1 @lshr2(i32 %x) {
355 %c = icmp ugt i32 %s, 8
357 ; CHECK: ret i1 false
360 define i1 @ashr1(i32 %x) {
363 %c = icmp eq i32 %s, 0
365 ; CHECK: ret i1 false
368 define i1 @ashr2(i32 %x) {
371 %c = icmp slt i32 %s, -5
373 ; CHECK: ret i1 false
376 define i1 @select1(i1 %cond) {
378 %s = select i1 %cond, i32 1, i32 0
379 %c = icmp eq i32 %s, 1
381 ; CHECK: ret i1 %cond
384 define i1 @select2(i1 %cond) {
386 %x = zext i1 %cond to i32
387 %s = select i1 %cond, i32 %x, i32 0
388 %c = icmp ne i32 %s, 0
390 ; CHECK: ret i1 %cond
393 define i1 @select3(i1 %cond) {
395 %x = zext i1 %cond to i32
396 %s = select i1 %cond, i32 1, i32 %x
397 %c = icmp ne i32 %s, 0
399 ; CHECK: ret i1 %cond
402 define i1 @select4(i1 %cond) {
404 %invert = xor i1 %cond, 1
405 %s = select i1 %invert, i32 0, i32 1
406 %c = icmp ne i32 %s, 0
408 ; CHECK: ret i1 %cond
411 define i1 @select5(i32 %x) {
413 %c = icmp eq i32 %x, 0
414 %s = select i1 %c, i32 1, i32 %x
415 %c2 = icmp eq i32 %s, 0
417 ; CHECK: ret i1 false
420 define i1 @select6(i32 %x) {
422 %c = icmp sgt i32 %x, 0
423 %s = select i1 %c, i32 %x, i32 4
424 %c2 = icmp eq i32 %s, 0
429 define i1 @urem1(i32 %X, i32 %Y) {
432 %B = icmp ult i32 %A, %Y
437 define i1 @urem2(i32 %X, i32 %Y) {
440 %B = icmp eq i32 %A, %Y
442 ; CHECK: ret i1 false
445 define i1 @urem3(i32 %X) {
448 %B = icmp ult i32 %A, 15
453 define i1 @urem4(i32 %X) {
456 %B = icmp ult i32 %A, 10
461 define i1 @urem5(i16 %X, i32 %Y) {
463 %A = zext i16 %X to i32
465 %C = icmp slt i32 %B, %Y
470 define i1 @urem6(i32 %X, i32 %Y) {
473 %B = icmp ugt i32 %Y, %A
478 define i1 @srem1(i32 %X) {
481 %B = icmp sgt i32 %A, 5
483 ; CHECK: ret i1 false
488 ; CHECK: ret i1 false
489 define i1 @srem2(i16 %X, i32 %Y) {
490 %A = zext i16 %X to i32
491 %B = add nsw i32 %A, 1
493 %D = icmp slt i32 %C, 0
498 ; CHECK-NEXT: ret i1 false
499 define i1 @srem3(i16 %X, i32 %Y) {
500 %A = zext i16 %X to i32
501 %B = or i32 2147483648, %A
502 %C = sub nsw i32 1, %B
504 %E = icmp slt i32 %D, 0
508 define i1 @udiv1(i32 %X) {
510 %A = udiv i32 %X, 1000000
511 %B = icmp ult i32 %A, 5000
516 define i1 @udiv2(i32 %X, i32 %Y, i32 %Z) {
518 %A = udiv exact i32 10, %Z
519 %B = udiv exact i32 20, %Z
520 %C = icmp ult i32 %A, %B
525 define i1 @udiv3(i32 %X, i32 %Y) {
528 %C = icmp ugt i32 %A, %X
530 ; CHECK: ret i1 false
533 define i1 @udiv4(i32 %X, i32 %Y) {
536 %C = icmp ule i32 %A, %X
541 define i1 @udiv5(i32 %X) {
543 %A = udiv i32 123, %X
544 %C = icmp ugt i32 %A, 124
546 ; CHECK: ret i1 false
550 define i1 @udiv6(i32 %X) nounwind {
553 %C = icmp eq i32 %A, 0
559 define i1 @sdiv1(i32 %X) {
561 %A = sdiv i32 %X, 1000000
562 %B = icmp slt i32 %A, 3000
567 define i1 @or1(i32 %X) {
570 %B = icmp ult i32 %A, 50
572 ; CHECK: ret i1 false
575 define i1 @and1(i32 %X) {
578 %B = icmp ugt i32 %A, 70
580 ; CHECK: ret i1 false
583 define i1 @mul1(i32 %X) {
585 ; Square of a non-zero number is non-zero if there is no overflow.
587 %M = mul nuw i32 %Y, %Y
588 %C = icmp eq i32 %M, 0
590 ; CHECK: ret i1 false
593 define i1 @mul2(i32 %X) {
595 ; Square of a non-zero number is positive if there is no signed overflow.
597 %M = mul nsw i32 %Y, %Y
598 %C = icmp sgt i32 %M, 0
603 define i1 @mul3(i32 %X, i32 %Y) {
605 ; Product of non-negative numbers is non-negative if there is no signed overflow.
606 %XX = mul nsw i32 %X, %X
607 %YY = mul nsw i32 %Y, %Y
608 %M = mul nsw i32 %XX, %YY
609 %C = icmp sge i32 %M, 0
614 define <2 x i1> @vectorselect1(<2 x i1> %cond) {
615 ; CHECK: @vectorselect1
616 %invert = xor <2 x i1> %cond, <i1 1, i1 1>
617 %s = select <2 x i1> %invert, <2 x i32> <i32 0, i32 0>, <2 x i32> <i32 1, i32 1>
618 %c = icmp ne <2 x i32> %s, <i32 0, i32 0>
620 ; CHECK: ret <2 x i1> %cond
624 define <2 x i1> @vectorselectcrash(i32 %arg1) {
625 %tobool40 = icmp ne i32 %arg1, 0
626 %cond43 = select i1 %tobool40, <2 x i16> <i16 -5, i16 66>, <2 x i16> <i16 46, i16 1>
627 %cmp45 = icmp ugt <2 x i16> %cond43, <i16 73, i16 21>
632 define i1 @alloca_compare(i64 %idx) {
633 %sv = alloca { i32, i32, [124 x i32] }
634 %1 = getelementptr inbounds { i32, i32, [124 x i32] }* %sv, i32 0, i32 2, i64 %idx
635 %2 = icmp eq i32* %1, null
637 ; CHECK: alloca_compare
638 ; CHECK: ret i1 false
642 define i1 @infinite_gep() {
646 %X = getelementptr i32 *%X, i32 1
647 %Y = icmp eq i32* %X, null
651 ; It's not valid to fold a comparison of an argument with an alloca, even though
652 ; that's tempting. An argument can't *alias* an alloca, however the aliasing rule
653 ; relies on restrictions against guessing an object's address and dereferencing.
654 ; There are no restrictions against guessing an object's address and comparing.
656 define i1 @alloca_argument_compare(i64* %arg) {
658 %cmp = icmp eq i64* %arg, %alloc
660 ; CHECK: alloca_argument_compare
664 ; As above, but with the operands reversed.
666 define i1 @alloca_argument_compare_swapped(i64* %arg) {
668 %cmp = icmp eq i64* %alloc, %arg
670 ; CHECK: alloca_argument_compare_swapped
674 ; Don't assume that a noalias argument isn't equal to a global variable's
675 ; address. This is an example where AliasAnalysis' NoAlias concept is
676 ; different from actual pointer inequality.
678 @y = external global i32
679 define zeroext i1 @external_compare(i32* noalias %x) {
680 %cmp = icmp eq i32* %x, @y
682 ; CHECK: external_compare
686 define i1 @alloca_gep(i64 %a, i64 %b) {
688 ; We can prove this GEP is non-null because it is inbounds and the pointer
690 %strs = alloca [1000 x [1001 x i8]], align 16
691 %x = getelementptr inbounds [1000 x [1001 x i8]]* %strs, i64 0, i64 %a, i64 %b
692 %cmp = icmp eq i8* %x, null
694 ; CHECK-NEXT: ret i1 false