Added inst combine tarnsform for (1 << X) & C pattrens where C is (some PowerOf2...
[oota-llvm.git] / test / Transforms / InstCombine / icmp.ll
1 ; RUN: opt < %s -instcombine -S | FileCheck %s
2
3 target datalayout =
4 "e-p:64:64:64-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
5
6 define i32 @test1(i32 %X) {
7 entry:
8         icmp slt i32 %X, 0              ; <i1>:0 [#uses=1]
9         zext i1 %0 to i32               ; <i32>:1 [#uses=1]
10         ret i32 %1
11 ; CHECK-LABEL: @test1(
12 ; CHECK: lshr i32 %X, 31
13 ; CHECK-NEXT: ret i32
14 }
15
16 define i32 @test2(i32 %X) {
17 entry:
18         icmp ult i32 %X, -2147483648            ; <i1>:0 [#uses=1]
19         zext i1 %0 to i32               ; <i32>:1 [#uses=1]
20         ret i32 %1
21 ; CHECK-LABEL: @test2(
22 ; CHECK: lshr i32 %X, 31
23 ; CHECK-NEXT: xor i32
24 ; CHECK-NEXT: ret i32
25 }
26
27 define i32 @test3(i32 %X) {
28 entry:
29         icmp slt i32 %X, 0              ; <i1>:0 [#uses=1]
30         sext i1 %0 to i32               ; <i32>:1 [#uses=1]
31         ret i32 %1
32 ; CHECK-LABEL: @test3(
33 ; CHECK: ashr i32 %X, 31
34 ; CHECK-NEXT: ret i32
35 }
36
37 define i32 @test4(i32 %X) {
38 entry:
39         icmp ult i32 %X, -2147483648            ; <i1>:0 [#uses=1]
40         sext i1 %0 to i32               ; <i32>:1 [#uses=1]
41         ret i32 %1
42 ; CHECK-LABEL: @test4(
43 ; CHECK: ashr i32 %X, 31
44 ; CHECK-NEXT: xor i32
45 ; CHECK-NEXT: ret i32
46 }
47
48 ; PR4837
49 define <2 x i1> @test5(<2 x i64> %x) {
50 entry:
51   %V = icmp eq <2 x i64> %x, undef
52   ret <2 x i1> %V
53 ; CHECK-LABEL: @test5(
54 ; CHECK: ret <2 x i1> <i1 true, i1 true>
55 }
56
57 define i32 @test6(i32 %a, i32 %b) {
58         %c = icmp sle i32 %a, -1
59         %d = zext i1 %c to i32
60         %e = sub i32 0, %d
61         %f = and i32 %e, %b
62         ret i32 %f
63 ; CHECK-LABEL: @test6(
64 ; CHECK-NEXT: ashr i32 %a, 31
65 ; CHECK-NEXT: %f = and i32 %e, %b
66 ; CHECK-NEXT: ret i32 %f
67 }
68
69
70 define i1 @test7(i32 %x) {
71 entry:
72   %a = add i32 %x, -1
73   %b = icmp ult i32 %a, %x
74   ret i1 %b
75 ; CHECK-LABEL: @test7(
76 ; CHECK: %b = icmp ne i32 %x, 0
77 ; CHECK: ret i1 %b
78 }
79
80 define i1 @test8(i32 %x){
81 entry:
82   %a = add i32 %x, -1
83   %b = icmp eq i32 %a, %x
84   ret i1 %b
85 ; CHECK-LABEL: @test8(
86 ; CHECK: ret i1 false
87 }
88
89 define i1 @test9(i32 %x)  {
90 entry:
91   %a = add i32 %x, -2
92   %b = icmp ugt i32 %x, %a
93   ret i1 %b
94 ; CHECK-LABEL: @test9(
95 ; CHECK: icmp ugt i32 %x, 1
96 ; CHECK: ret i1 %b
97 }
98
99 define i1 @test10(i32 %x){
100 entry:
101   %a = add i32 %x, -1
102   %b = icmp slt i32 %a, %x
103   ret i1 %b
104 ; CHECK-LABEL: @test10(
105 ; CHECK: %b = icmp ne i32 %x, -2147483648
106 ; CHECK: ret i1 %b
107 }
108
109 define i1 @test11(i32 %x) {
110   %a = add nsw i32 %x, 8
111   %b = icmp slt i32 %x, %a
112   ret i1 %b
113 ; CHECK-LABEL: @test11(
114 ; CHECK: ret i1 true
115 }
116
117 ; PR6195
118 define i1 @test12(i1 %A) {
119   %S = select i1 %A, i64 -4294967295, i64 8589934591
120   %B = icmp ne i64 bitcast (<2 x i32> <i32 1, i32 -1> to i64), %S
121   ret i1 %B
122 ; CHECK-LABEL: @test12(
123 ; CHECK-NEXT: = xor i1 %A, true
124 ; CHECK-NEXT: ret i1
125 }
126
127 ; PR6481
128 define i1 @test13(i8 %X) nounwind readnone {
129 entry:
130         %cmp = icmp slt i8 undef, %X
131         ret i1 %cmp
132 ; CHECK-LABEL: @test13(
133 ; CHECK: ret i1 false
134 }
135
136 define i1 @test14(i8 %X) nounwind readnone {
137 entry:
138         %cmp = icmp slt i8 undef, -128
139         ret i1 %cmp
140 ; CHECK-LABEL: @test14(
141 ; CHECK: ret i1 false
142 }
143
144 define i1 @test15() nounwind readnone {
145 entry:
146         %cmp = icmp eq i8 undef, -128
147         ret i1 %cmp
148 ; CHECK-LABEL: @test15(
149 ; CHECK: ret i1 undef
150 }
151
152 define i1 @test16() nounwind readnone {
153 entry:
154         %cmp = icmp ne i8 undef, -128
155         ret i1 %cmp
156 ; CHECK-LABEL: @test16(
157 ; CHECK: ret i1 undef
158 }
159
160 define i1 @test17(i32 %x) nounwind {
161   %shl = shl i32 1, %x
162   %and = and i32 %shl, 8
163   %cmp = icmp eq i32 %and, 0
164   ret i1 %cmp
165 ; CHECK-LABEL: @test17(
166 ; CHECK-NEXT: %cmp = icmp ne i32 %x, 3
167 }
168
169 define i1 @test17a(i32 %x) nounwind {
170   %shl = shl i32 1, %x
171   %and = and i32 %shl, 7
172   %cmp = icmp eq i32 %and, 0
173   ret i1 %cmp
174 ; CHECK-LABEL: @test17a(
175 ; CHECK-NEXT: %cmp = icmp ugt i32 %x, 2
176 }
177
178 define i1 @test18(i32 %x) nounwind {
179   %sh = lshr i32 8, %x
180   %and = and i32 %sh, 1
181   %cmp = icmp eq i32 %and, 0
182   ret i1 %cmp
183 ; CHECK-LABEL: @test18(
184 ; CHECK-NEXT: %cmp = icmp ne i32 %x, 3
185 }
186
187 define i1 @test19(i32 %x) nounwind {
188   %shl = shl i32 1, %x
189   %and = and i32 %shl, 8
190   %cmp = icmp eq i32 %and, 8
191   ret i1 %cmp
192 ; CHECK-LABEL: @test19(
193 ; CHECK-NEXT: %cmp = icmp eq i32 %x, 3
194 }
195
196 define i1 @test20(i32 %x) nounwind {
197   %shl = shl i32 1, %x
198   %and = and i32 %shl, 8
199   %cmp = icmp ne i32 %and, 0
200   ret i1 %cmp
201 ; CHECK-LABEL: @test20(
202 ; CHECK-NEXT: %cmp = icmp eq i32 %x, 3
203 }
204
205 define i1 @test20a(i32 %x) nounwind {
206   %shl = shl i32 1, %x
207   %and = and i32 %shl, 7
208   %cmp = icmp ne i32 %and, 0
209   ret i1 %cmp
210 ; CHECK-LABEL: @test20a(
211 ; CHECK-NEXT: %cmp = icmp ult i32 %x, 3
212 }
213
214 define i1 @test21(i8 %x, i8 %y) {
215 ; CHECK-LABEL: @test21(
216 ; CHECK-NOT: or i8
217 ; CHECK: icmp ugt
218   %A = or i8 %x, 1
219   %B = icmp ugt i8 %A, 3
220   ret i1 %B
221 }
222
223 define i1 @test22(i8 %x, i8 %y) {
224 ; CHECK-LABEL: @test22(
225 ; CHECK-NOT: or i8
226 ; CHECK: icmp ult
227   %A = or i8 %x, 1
228   %B = icmp ult i8 %A, 4
229   ret i1 %B
230 }
231
232 ; PR2740
233 ; CHECK-LABEL: @test23(
234 ; CHECK: icmp sgt i32 %x, 1328634634
235 define i1 @test23(i32 %x) nounwind {
236         %i3 = sdiv i32 %x, -1328634635
237         %i4 = icmp eq i32 %i3, -1
238         ret i1 %i4
239 }
240
241 @X = global [1000 x i32] zeroinitializer
242
243 ; PR8882
244 ; CHECK-LABEL: @test24(
245 ; CHECK:    %cmp = icmp eq i64 %i, 1000
246 ; CHECK:   ret i1 %cmp
247 define i1 @test24(i64 %i) {
248   %p1 = getelementptr inbounds i32* getelementptr inbounds ([1000 x i32]* @X, i64 0, i64 0), i64 %i
249   %cmp = icmp eq i32* %p1, getelementptr inbounds ([1000 x i32]* @X, i64 1, i64 0)
250   ret i1 %cmp
251 }
252
253 @X_as1 = addrspace(1) global [1000 x i32] zeroinitializer
254
255 ; CHECK: @test24_as1
256 ; CHECK: trunc i64 %i to i16
257 ; CHECK: %cmp = icmp eq i16 %1, 1000
258 ; CHECK: ret i1 %cmp
259 define i1 @test24_as1(i64 %i) {
260   %p1 = getelementptr inbounds i32 addrspace(1)* getelementptr inbounds ([1000 x i32] addrspace(1)* @X_as1, i64 0, i64 0), i64 %i
261   %cmp = icmp eq i32 addrspace(1)* %p1, getelementptr inbounds ([1000 x i32] addrspace(1)* @X_as1, i64 1, i64 0)
262   ret i1 %cmp
263 }
264
265 ; CHECK-LABEL: @test25(
266 ; X + Z > Y + Z -> X > Y if there is no overflow.
267 ; CHECK: %c = icmp sgt i32 %x, %y
268 ; CHECK: ret i1 %c
269 define i1 @test25(i32 %x, i32 %y, i32 %z) {
270   %lhs = add nsw i32 %x, %z
271   %rhs = add nsw i32 %y, %z
272   %c = icmp sgt i32 %lhs, %rhs
273   ret i1 %c
274 }
275
276 ; CHECK-LABEL: @test26(
277 ; X + Z > Y + Z -> X > Y if there is no overflow.
278 ; CHECK: %c = icmp ugt i32 %x, %y
279 ; CHECK: ret i1 %c
280 define i1 @test26(i32 %x, i32 %y, i32 %z) {
281   %lhs = add nuw i32 %x, %z
282   %rhs = add nuw i32 %y, %z
283   %c = icmp ugt i32 %lhs, %rhs
284   ret i1 %c
285 }
286
287 ; CHECK-LABEL: @test27(
288 ; X - Z > Y - Z -> X > Y if there is no overflow.
289 ; CHECK: %c = icmp sgt i32 %x, %y
290 ; CHECK: ret i1 %c
291 define i1 @test27(i32 %x, i32 %y, i32 %z) {
292   %lhs = sub nsw i32 %x, %z
293   %rhs = sub nsw i32 %y, %z
294   %c = icmp sgt i32 %lhs, %rhs
295   ret i1 %c
296 }
297
298 ; CHECK-LABEL: @test28(
299 ; X - Z > Y - Z -> X > Y if there is no overflow.
300 ; CHECK: %c = icmp ugt i32 %x, %y
301 ; CHECK: ret i1 %c
302 define i1 @test28(i32 %x, i32 %y, i32 %z) {
303   %lhs = sub nuw i32 %x, %z
304   %rhs = sub nuw i32 %y, %z
305   %c = icmp ugt i32 %lhs, %rhs
306   ret i1 %c
307 }
308
309 ; CHECK-LABEL: @test29(
310 ; X + Y > X -> Y > 0 if there is no overflow.
311 ; CHECK: %c = icmp sgt i32 %y, 0
312 ; CHECK: ret i1 %c
313 define i1 @test29(i32 %x, i32 %y) {
314   %lhs = add nsw i32 %x, %y
315   %c = icmp sgt i32 %lhs, %x
316   ret i1 %c
317 }
318
319 ; CHECK-LABEL: @test30(
320 ; X + Y > X -> Y > 0 if there is no overflow.
321 ; CHECK: %c = icmp ne i32 %y, 0
322 ; CHECK: ret i1 %c
323 define i1 @test30(i32 %x, i32 %y) {
324   %lhs = add nuw i32 %x, %y
325   %c = icmp ugt i32 %lhs, %x
326   ret i1 %c
327 }
328
329 ; CHECK-LABEL: @test31(
330 ; X > X + Y -> 0 > Y if there is no overflow.
331 ; CHECK: %c = icmp slt i32 %y, 0
332 ; CHECK: ret i1 %c
333 define i1 @test31(i32 %x, i32 %y) {
334   %rhs = add nsw i32 %x, %y
335   %c = icmp sgt i32 %x, %rhs
336   ret i1 %c
337 }
338
339 ; CHECK-LABEL: @test32(
340 ; X > X + Y -> 0 > Y if there is no overflow.
341 ; CHECK: ret i1 false
342 define i1 @test32(i32 %x, i32 %y) {
343   %rhs = add nuw i32 %x, %y
344   %c = icmp ugt i32 %x, %rhs
345   ret i1 %c
346 }
347
348 ; CHECK-LABEL: @test33(
349 ; X - Y > X -> 0 > Y if there is no overflow.
350 ; CHECK: %c = icmp slt i32 %y, 0
351 ; CHECK: ret i1 %c
352 define i1 @test33(i32 %x, i32 %y) {
353   %lhs = sub nsw i32 %x, %y
354   %c = icmp sgt i32 %lhs, %x
355   ret i1 %c
356 }
357
358 ; CHECK-LABEL: @test34(
359 ; X - Y > X -> 0 > Y if there is no overflow.
360 ; CHECK: ret i1 false
361 define i1 @test34(i32 %x, i32 %y) {
362   %lhs = sub nuw i32 %x, %y
363   %c = icmp ugt i32 %lhs, %x
364   ret i1 %c
365 }
366
367 ; CHECK-LABEL: @test35(
368 ; X > X - Y -> Y > 0 if there is no overflow.
369 ; CHECK: %c = icmp sgt i32 %y, 0
370 ; CHECK: ret i1 %c
371 define i1 @test35(i32 %x, i32 %y) {
372   %rhs = sub nsw i32 %x, %y
373   %c = icmp sgt i32 %x, %rhs
374   ret i1 %c
375 }
376
377 ; CHECK-LABEL: @test36(
378 ; X > X - Y -> Y > 0 if there is no overflow.
379 ; CHECK: %c = icmp ne i32 %y, 0
380 ; CHECK: ret i1 %c
381 define i1 @test36(i32 %x, i32 %y) {
382   %rhs = sub nuw i32 %x, %y
383   %c = icmp ugt i32 %x, %rhs
384   ret i1 %c
385 }
386
387 ; CHECK-LABEL: @test37(
388 ; X - Y > X - Z -> Z > Y if there is no overflow.
389 ; CHECK: %c = icmp sgt i32 %z, %y
390 ; CHECK: ret i1 %c
391 define i1 @test37(i32 %x, i32 %y, i32 %z) {
392   %lhs = sub nsw i32 %x, %y
393   %rhs = sub nsw i32 %x, %z
394   %c = icmp sgt i32 %lhs, %rhs
395   ret i1 %c
396 }
397
398 ; CHECK-LABEL: @test38(
399 ; X - Y > X - Z -> Z > Y if there is no overflow.
400 ; CHECK: %c = icmp ugt i32 %z, %y
401 ; CHECK: ret i1 %c
402 define i1 @test38(i32 %x, i32 %y, i32 %z) {
403   %lhs = sub nuw i32 %x, %y
404   %rhs = sub nuw i32 %x, %z
405   %c = icmp ugt i32 %lhs, %rhs
406   ret i1 %c
407 }
408
409 ; PR9343 #1
410 ; CHECK-LABEL: @test39(
411 ; CHECK: %B = icmp eq i32 %X, 0
412 define i1 @test39(i32 %X, i32 %Y) {
413   %A = ashr exact i32 %X, %Y
414   %B = icmp eq i32 %A, 0
415   ret i1 %B
416 }
417
418 ; CHECK-LABEL: @test40(
419 ; CHECK: %B = icmp ne i32 %X, 0
420 define i1 @test40(i32 %X, i32 %Y) {
421   %A = lshr exact i32 %X, %Y
422   %B = icmp ne i32 %A, 0
423   ret i1 %B
424 }
425
426 ; PR9343 #3
427 ; CHECK-LABEL: @test41(
428 ; CHECK: ret i1 true
429 define i1 @test41(i32 %X, i32 %Y) {
430   %A = urem i32 %X, %Y
431   %B = icmp ugt i32 %Y, %A
432   ret i1 %B
433 }
434
435 ; CHECK-LABEL: @test42(
436 ; CHECK: %B = icmp sgt i32 %Y, -1
437 define i1 @test42(i32 %X, i32 %Y) {
438   %A = srem i32 %X, %Y
439   %B = icmp slt i32 %A, %Y
440   ret i1 %B
441 }
442
443 ; CHECK-LABEL: @test43(
444 ; CHECK: %B = icmp slt i32 %Y, 0
445 define i1 @test43(i32 %X, i32 %Y) {
446   %A = srem i32 %X, %Y
447   %B = icmp slt i32 %Y, %A
448   ret i1 %B
449 }
450
451 ; CHECK-LABEL: @test44(
452 ; CHECK: %B = icmp sgt i32 %Y, -1
453 define i1 @test44(i32 %X, i32 %Y) {
454   %A = srem i32 %X, %Y
455   %B = icmp slt i32 %A, %Y
456   ret i1 %B
457 }
458
459 ; CHECK-LABEL: @test45(
460 ; CHECK: %B = icmp slt i32 %Y, 0
461 define i1 @test45(i32 %X, i32 %Y) {
462   %A = srem i32 %X, %Y
463   %B = icmp slt i32 %Y, %A
464   ret i1 %B
465 }
466
467 ; PR9343 #4
468 ; CHECK-LABEL: @test46(
469 ; CHECK: %C = icmp ult i32 %X, %Y
470 define i1 @test46(i32 %X, i32 %Y, i32 %Z) {
471   %A = ashr exact i32 %X, %Z
472   %B = ashr exact i32 %Y, %Z
473   %C = icmp ult i32 %A, %B
474   ret i1 %C
475 }
476
477 ; PR9343 #5
478 ; CHECK-LABEL: @test47(
479 ; CHECK: %C = icmp ugt i32 %X, %Y
480 define i1 @test47(i32 %X, i32 %Y, i32 %Z) {
481   %A = ashr exact i32 %X, %Z
482   %B = ashr exact i32 %Y, %Z
483   %C = icmp ugt i32 %A, %B
484   ret i1 %C
485 }
486
487 ; PR9343 #8
488 ; CHECK-LABEL: @test48(
489 ; CHECK: %C = icmp eq i32 %X, %Y
490 define i1 @test48(i32 %X, i32 %Y, i32 %Z) {
491   %A = sdiv exact i32 %X, %Z
492   %B = sdiv exact i32 %Y, %Z
493   %C = icmp eq i32 %A, %B
494   ret i1 %C
495 }
496
497 ; PR8469
498 ; CHECK-LABEL: @test49(
499 ; CHECK: ret <2 x i1> <i1 true, i1 true>
500 define <2 x i1> @test49(<2 x i32> %tmp3) {
501 entry:
502   %tmp11 = and <2 x i32> %tmp3, <i32 3, i32 3>
503   %cmp = icmp ult <2 x i32> %tmp11, <i32 4, i32 4>
504   ret <2 x i1> %cmp
505 }
506
507 ; PR9343 #7
508 ; CHECK-LABEL: @test50(
509 ; CHECK: ret i1 true
510 define i1 @test50(i16 %X, i32 %Y) {
511   %A = zext i16 %X to i32
512   %B = srem i32 %A, %Y
513   %C = icmp sgt i32 %B, -1
514   ret i1 %C
515 }
516
517 ; CHECK-LABEL: @test51(
518 ; CHECK: ret i1 %C
519 define i1 @test51(i32 %X, i32 %Y) {
520   %A = and i32 %X, 2147483648
521   %B = srem i32 %A, %Y
522   %C = icmp sgt i32 %B, -1
523   ret i1 %C
524 }
525
526 ; CHECK-LABEL: @test52(
527 ; CHECK-NEXT: and i32 %x1, 16711935
528 ; CHECK-NEXT: icmp eq i32 {{.*}}, 4980863
529 ; CHECK-NEXT: ret i1
530 define i1 @test52(i32 %x1) nounwind {
531   %conv = and i32 %x1, 255
532   %cmp = icmp eq i32 %conv, 127
533   %tmp2 = lshr i32 %x1, 16
534   %tmp3 = trunc i32 %tmp2 to i8
535   %cmp15 = icmp eq i8 %tmp3, 76
536
537   %A = and i1 %cmp, %cmp15
538   ret i1 %A
539 }
540
541 ; PR9838
542 ; CHECK-LABEL: @test53(
543 ; CHECK-NEXT: sdiv exact
544 ; CHECK-NEXT: sdiv
545 ; CHECK-NEXT: icmp
546 define i1 @test53(i32 %a, i32 %b) nounwind {
547  %x = sdiv exact i32 %a, 30
548  %y = sdiv i32 %b, 30
549  %z = icmp eq i32 %x, %y
550  ret i1 %z
551 }
552
553 ; CHECK-LABEL: @test54(
554 ; CHECK-NEXT: %and = and i8 %a, -64
555 ; CHECK-NEXT: icmp eq i8 %and, -128
556 define i1 @test54(i8 %a) nounwind {
557   %ext = zext i8 %a to i32
558   %and = and i32 %ext, 192
559   %ret = icmp eq i32 %and, 128
560   ret i1 %ret
561 }
562
563 ; CHECK-LABEL: @test55(
564 ; CHECK-NEXT: icmp eq i32 %a, -123
565 define i1 @test55(i32 %a) {
566   %sub = sub i32 0, %a
567   %cmp = icmp eq i32 %sub, 123
568   ret i1 %cmp
569 }
570
571 ; CHECK-LABEL: @test56(
572 ; CHECK-NEXT: icmp eq i32 %a, -113
573 define i1 @test56(i32 %a) {
574   %sub = sub i32 10, %a
575   %cmp = icmp eq i32 %sub, 123
576   ret i1 %cmp
577 }
578
579 ; PR10267 Don't make icmps more expensive when no other inst is subsumed.
580 declare void @foo(i32)
581 ; CHECK-LABEL: @test57(
582 ; CHECK: %and = and i32 %a, -2
583 ; CHECK: %cmp = icmp ne i32 %and, 0
584 define i1 @test57(i32 %a) {
585   %and = and i32 %a, -2
586   %cmp = icmp ne i32 %and, 0
587   call void @foo(i32 %and)
588   ret i1 %cmp
589 }
590
591 ; rdar://problem/10482509
592 ; CHECK-LABEL: @cmpabs1(
593 ; CHECK-NEXT: icmp ne
594 define zeroext i1 @cmpabs1(i64 %val) {
595   %sub = sub nsw i64 0, %val
596   %cmp = icmp slt i64 %val, 0
597   %sub.val = select i1 %cmp, i64 %sub, i64 %val
598   %tobool = icmp ne i64 %sub.val, 0
599   ret i1 %tobool
600 }
601
602 ; CHECK-LABEL: @cmpabs2(
603 ; CHECK-NEXT: icmp ne
604 define zeroext i1 @cmpabs2(i64 %val) {
605   %sub = sub nsw i64 0, %val
606   %cmp = icmp slt i64 %val, 0
607   %sub.val = select i1 %cmp, i64 %val, i64 %sub
608   %tobool = icmp ne i64 %sub.val, 0
609   ret i1 %tobool
610 }
611
612 ; CHECK-LABEL: @test58(
613 ; CHECK-NEXT: call i32 @test58_d(i64 36029346783166592)
614 define void @test58() nounwind {
615   %cast = bitcast <1 x i64> <i64 36029346783166592> to i64
616   %call = call i32 @test58_d( i64 %cast) nounwind
617   ret void
618 }
619 declare i32 @test58_d(i64)
620
621 define i1 @test59(i8* %foo) {
622   %bit = bitcast i8* %foo to i32*
623   %gep1 = getelementptr inbounds i32* %bit, i64 2
624   %gep2 = getelementptr inbounds i8* %foo, i64 10
625   %cast1 = bitcast i32* %gep1 to i8*
626   %cmp = icmp ult i8* %cast1, %gep2
627   %use = ptrtoint i8* %cast1 to i64
628   %call = call i32 @test58_d(i64 %use) nounwind
629   ret i1 %cmp
630 ; CHECK-LABEL: @test59(
631 ; CHECK: ret i1 true
632 }
633
634 define i1 @test59_as1(i8 addrspace(1)* %foo) {
635   %bit = bitcast i8 addrspace(1)* %foo to i32 addrspace(1)*
636   %gep1 = getelementptr inbounds i32 addrspace(1)* %bit, i64 2
637   %gep2 = getelementptr inbounds i8 addrspace(1)* %foo, i64 10
638   %cast1 = bitcast i32 addrspace(1)* %gep1 to i8 addrspace(1)*
639   %cmp = icmp ult i8 addrspace(1)* %cast1, %gep2
640   %use = ptrtoint i8 addrspace(1)* %cast1 to i64
641   %call = call i32 @test58_d(i64 %use) nounwind
642   ret i1 %cmp
643 ; CHECK: @test59_as1
644 ; CHECK: %[[GEP:.+]] = getelementptr inbounds i8 addrspace(1)* %foo, i16 8
645 ; CHECK: ptrtoint i8 addrspace(1)* %[[GEP]] to i16
646 ; CHECK: ret i1 true
647 }
648
649 define i1 @test60(i8* %foo, i64 %i, i64 %j) {
650   %bit = bitcast i8* %foo to i32*
651   %gep1 = getelementptr inbounds i32* %bit, i64 %i
652   %gep2 = getelementptr inbounds i8* %foo, i64 %j
653   %cast1 = bitcast i32* %gep1 to i8*
654   %cmp = icmp ult i8* %cast1, %gep2
655   ret i1 %cmp
656 ; CHECK-LABEL: @test60(
657 ; CHECK-NEXT: %gep1.idx = shl nuw i64 %i, 2
658 ; CHECK-NEXT: icmp slt i64 %gep1.idx, %j
659 ; CHECK-NEXT: ret i1
660 }
661
662 define i1 @test60_as1(i8 addrspace(1)* %foo, i64 %i, i64 %j) {
663   %bit = bitcast i8 addrspace(1)* %foo to i32 addrspace(1)*
664   %gep1 = getelementptr inbounds i32 addrspace(1)* %bit, i64 %i
665   %gep2 = getelementptr inbounds i8 addrspace(1)* %foo, i64 %j
666   %cast1 = bitcast i32 addrspace(1)* %gep1 to i8 addrspace(1)*
667   %cmp = icmp ult i8 addrspace(1)* %cast1, %gep2
668   ret i1 %cmp
669 ; CHECK: @test60_as1
670 ; CHECK: trunc i64 %i to i16
671 ; CHECK: trunc i64 %j to i16
672 ; CHECK: %gep1.idx = shl nuw i16 %{{.+}}, 2
673 ; CHECK-NEXT: icmp sgt i16 %{{.+}}, %gep1.idx
674 ; CHECK-NEXT: ret i1
675 }
676
677 define i1 @test61(i8* %foo, i64 %i, i64 %j) {
678   %bit = bitcast i8* %foo to i32*
679   %gep1 = getelementptr i32* %bit, i64 %i
680   %gep2 = getelementptr  i8* %foo, i64 %j
681   %cast1 = bitcast i32* %gep1 to i8*
682   %cmp = icmp ult i8* %cast1, %gep2
683   ret i1 %cmp
684 ; Don't transform non-inbounds GEPs.
685 ; CHECK-LABEL: @test61(
686 ; CHECK: icmp ult i8* %cast1, %gep2
687 ; CHECK-NEXT: ret i1
688 }
689
690 define i1 @test61_as1(i8 addrspace(1)* %foo, i16 %i, i16 %j) {
691   %bit = bitcast i8 addrspace(1)* %foo to i32 addrspace(1)*
692   %gep1 = getelementptr i32 addrspace(1)* %bit, i16 %i
693   %gep2 = getelementptr i8 addrspace(1)* %foo, i16 %j
694   %cast1 = bitcast i32 addrspace(1)* %gep1 to i8 addrspace(1)*
695   %cmp = icmp ult i8 addrspace(1)* %cast1, %gep2
696   ret i1 %cmp
697 ; Don't transform non-inbounds GEPs.
698 ; CHECK: @test61_as1
699 ; CHECK: icmp ult i8 addrspace(1)* %cast1, %gep2
700 ; CHECK-NEXT: ret i1
701 }
702
703 define i1 @test62(i8* %a) {
704   %arrayidx1 = getelementptr inbounds i8* %a, i64 1
705   %arrayidx2 = getelementptr inbounds i8* %a, i64 10
706   %cmp = icmp slt i8* %arrayidx1, %arrayidx2
707   ret i1 %cmp
708 ; CHECK-LABEL: @test62(
709 ; CHECK-NEXT: ret i1 true
710 }
711
712 define i1 @test62_as1(i8 addrspace(1)* %a) {
713 ; CHECK-LABEL: @test62_as1(
714 ; CHECK-NEXT: ret i1 true
715   %arrayidx1 = getelementptr inbounds i8 addrspace(1)* %a, i64 1
716   %arrayidx2 = getelementptr inbounds i8 addrspace(1)* %a, i64 10
717   %cmp = icmp slt i8 addrspace(1)* %arrayidx1, %arrayidx2
718   ret i1 %cmp
719 }
720
721 define i1 @test63(i8 %a, i32 %b) nounwind {
722   %z = zext i8 %a to i32
723   %t = and i32 %b, 255
724   %c = icmp eq i32 %z, %t
725   ret i1 %c
726 ; CHECK-LABEL: @test63(
727 ; CHECK-NEXT: %1 = trunc i32 %b to i8
728 ; CHECK-NEXT: %c = icmp eq i8 %1, %a
729 ; CHECK-NEXT: ret i1 %c
730 }
731
732 define i1 @test64(i8 %a, i32 %b) nounwind {
733   %t = and i32 %b, 255
734   %z = zext i8 %a to i32
735   %c = icmp eq i32 %t, %z
736   ret i1 %c
737 ; CHECK-LABEL: @test64(
738 ; CHECK-NEXT: %1 = trunc i32 %b to i8
739 ; CHECK-NEXT: %c = icmp eq i8 %1, %a
740 ; CHECK-NEXT: ret i1 %c
741 }
742
743 define i1 @test65(i64 %A, i64 %B) {
744   %s1 = add i64 %A, %B
745   %s2 = add i64 %A, %B
746   %cmp = icmp eq i64 %s1, %s2
747 ; CHECK-LABEL: @test65(
748 ; CHECK-NEXT: ret i1 true
749   ret i1 %cmp
750 }
751
752 define i1 @test66(i64 %A, i64 %B) {
753   %s1 = add i64 %A, %B
754   %s2 = add i64 %B, %A
755   %cmp = icmp eq i64 %s1, %s2
756 ; CHECK-LABEL: @test66(
757 ; CHECK-NEXT: ret i1 true
758   ret i1 %cmp
759 }
760
761 ; CHECK-LABEL: @test67(
762 ; CHECK: %and = and i32 %x, 96
763 ; CHECK: %cmp = icmp ne i32 %and, 0
764 define i1 @test67(i32 %x) nounwind uwtable {
765   %and = and i32 %x, 127
766   %cmp = icmp sgt i32 %and, 31
767   ret i1 %cmp
768 }
769
770 ; CHECK-LABEL: @test68(
771 ; CHECK: %cmp = icmp ugt i32 %and, 30
772 define i1 @test68(i32 %x) nounwind uwtable {
773   %and = and i32 %x, 127
774   %cmp = icmp sgt i32 %and, 30
775   ret i1 %cmp
776 }
777
778 ; PR14708
779 ; CHECK-LABEL: @test69(
780 ; CHECK: %1 = and i32 %c, -33
781 ; CHECK: %2 = icmp eq i32 %1, 65
782 ; CHECK: ret i1 %2
783 define i1 @test69(i32 %c) nounwind uwtable {
784   %1 = icmp eq i32 %c, 97
785   %2 = icmp eq i32 %c, 65
786   %3 = or i1 %1, %2
787   ret i1 %3
788 }
789
790 ; PR15940
791 ; CHECK-LABEL: @test70(
792 ; CHECK-NEXT: %A = srem i32 5, %X
793 ; CHECK-NEXT: %C = icmp ne i32 %A, 2
794 ; CHECK-NEXT: ret i1 %C
795 define i1 @test70(i32 %X) {
796   %A = srem i32 5, %X
797   %B = add i32 %A, 2
798   %C = icmp ne i32 %B, 4
799   ret i1 %C
800 }
801
802 ; CHECK-LABEL: @icmp_sext16trunc(
803 ; CHECK-NEXT: %1 = trunc i32 %x to i16
804 ; CHECK-NEXT: %cmp = icmp slt i16 %1, 36
805 define i1 @icmp_sext16trunc(i32 %x) {
806   %trunc = trunc i32 %x to i16
807   %sext = sext i16 %trunc to i32
808   %cmp = icmp slt i32 %sext, 36
809   ret i1 %cmp
810 }
811
812 ; CHECK-LABEL: @icmp_sext8trunc(
813 ; CHECK-NEXT: %1 = trunc i32 %x to i8
814 ; CHECK-NEXT: %cmp = icmp slt i8 %1, 36
815 define i1 @icmp_sext8trunc(i32 %x) {
816   %trunc = trunc i32 %x to i8
817   %sext = sext i8 %trunc to i32
818   %cmp = icmp slt i32 %sext, 36
819   ret i1 %cmp
820 }
821
822 ; CHECK-LABEL: @icmp_shl16(
823 ; CHECK-NEXT: %1 = trunc i32 %x to i16
824 ; CHECK-NEXT: %cmp = icmp slt i16 %1, 36
825 define i1 @icmp_shl16(i32 %x) {
826   %shl = shl i32 %x, 16
827   %cmp = icmp slt i32 %shl, 2359296
828   ret i1 %cmp
829 }
830
831 ; CHECK-LABEL: @icmp_shl24(
832 ; CHECK-NEXT: %1 = trunc i32 %x to i8
833 ; CHECK-NEXT: %cmp = icmp slt i8 %1, 36
834 define i1 @icmp_shl24(i32 %x) {
835   %shl = shl i32 %x, 24
836   %cmp = icmp slt i32 %shl, 603979776
837   ret i1 %cmp
838 }
839
840 ; If the (shl x, C) preserved the sign and this is a sign test,
841 ; compare the LHS operand instead
842 ; CHECK-LABEL: @icmp_shl_nsw_sgt(
843 ; CHECK-NEXT: icmp sgt i32 %x, 0
844 define i1 @icmp_shl_nsw_sgt(i32 %x) {
845   %shl = shl nsw i32 %x, 21
846   %cmp = icmp sgt i32 %shl, 0
847   ret i1 %cmp
848 }
849
850 ; CHECK-LABEL: @icmp_shl_nsw_sge0(
851 ; CHECK-NEXT: icmp sgt i32 %x, -1
852 define i1 @icmp_shl_nsw_sge0(i32 %x) {
853   %shl = shl nsw i32 %x, 21
854   %cmp = icmp sge i32 %shl, 0
855   ret i1 %cmp
856 }
857
858 ; CHECK-LABEL: @icmp_shl_nsw_sge1(
859 ; CHECK-NEXT: icmp sgt i32 %x, 0
860 define i1 @icmp_shl_nsw_sge1(i32 %x) {
861   %shl = shl nsw i32 %x, 21
862   %cmp = icmp sge i32 %shl, 1
863   ret i1 %cmp
864 }
865
866 ; Checks for icmp (eq|ne) (shl x, C), 0
867 ; CHECK-LABEL: @icmp_shl_nsw_eq(
868 ; CHECK-NEXT: icmp eq i32 %x, 0
869 define i1 @icmp_shl_nsw_eq(i32 %x) {
870   %mul = shl nsw i32 %x, 5
871   %cmp = icmp eq i32 %mul, 0
872   ret i1 %cmp
873 }
874
875 ; CHECK-LABEL: @icmp_shl_eq(
876 ; CHECK-NOT: icmp eq i32 %mul, 0
877 define i1 @icmp_shl_eq(i32 %x) {
878   %mul = shl i32 %x, 5
879   %cmp = icmp eq i32 %mul, 0
880   ret i1 %cmp
881 }
882
883 ; CHECK-LABEL: @icmp_shl_nsw_ne(
884 ; CHECK-NEXT: icmp ne i32 %x, 0
885 define i1 @icmp_shl_nsw_ne(i32 %x) {
886   %mul = shl nsw i32 %x, 7
887   %cmp = icmp ne i32 %mul, 0
888   ret i1 %cmp
889 }
890
891 ; CHECK-LABEL: @icmp_shl_ne(
892 ; CHECK-NOT: icmp ne i32 %x, 0
893 define i1 @icmp_shl_ne(i32 %x) {
894   %mul = shl i32 %x, 7
895   %cmp = icmp ne i32 %mul, 0
896   ret i1 %cmp
897 }
898
899 ; If the (mul x, C) preserved the sign and this is sign test,
900 ; compare the LHS operand instead
901 ; CHECK-LABEL: @icmp_mul_nsw(
902 ; CHECK-NEXT: icmp sgt i32 %x, 0
903 define i1 @icmp_mul_nsw(i32 %x) {
904   %mul = mul nsw i32 %x, 12
905   %cmp = icmp sgt i32 %mul, 0
906   ret i1 %cmp
907 }
908
909 ; CHECK-LABEL: @icmp_mul_nsw1(
910 ; CHECK-NEXT: icmp slt i32 %x, 0
911 define i1 @icmp_mul_nsw1(i32 %x) {
912   %mul = mul nsw i32 %x, 12
913   %cmp = icmp sle i32 %mul, -1
914   ret i1 %cmp
915 }
916
917 ; CHECK-LABEL: @icmp_mul_nsw_neg(
918 ; CHECK-NEXT: icmp slt i32 %x, 1
919 define i1 @icmp_mul_nsw_neg(i32 %x) {
920   %mul = mul nsw i32 %x, -12
921   %cmp = icmp sge i32 %mul, 0
922   ret i1 %cmp
923 }
924
925 ; CHECK-LABEL: @icmp_mul_nsw_neg1(
926 ; CHECK-NEXT: icmp slt i32 %x, 0
927 define i1 @icmp_mul_nsw_neg1(i32 %x) {
928   %mul = mul nsw i32 %x, -12
929   %cmp = icmp sge i32 %mul, 1
930   ret i1 %cmp
931 }
932
933 ; CHECK-LABEL: @icmp_mul_nsw_0(
934 ; CHECK-NOT: icmp sgt i32 %x, 0
935 define i1 @icmp_mul_nsw_0(i32 %x) {
936   %mul = mul nsw i32 %x, 0
937   %cmp = icmp sgt i32 %mul, 0
938   ret i1 %cmp
939 }
940
941 ; CHECK-LABEL: @icmp_mul(
942 ; CHECK-NEXT: %mul = mul i32 %x, -12
943 define i1 @icmp_mul(i32 %x) {
944   %mul = mul i32 %x, -12
945   %cmp = icmp sge i32 %mul, 0
946   ret i1 %cmp
947 }
948
949 ; Checks for icmp (eq|ne) (mul x, C), 0
950 ; CHECK-LABEL: @icmp_mul_neq0(
951 ; CHECK-NEXT: icmp ne i32 %x, 0
952 define i1 @icmp_mul_neq0(i32 %x) {
953   %mul = mul nsw i32 %x, -12
954   %cmp = icmp ne i32 %mul, 0
955   ret i1 %cmp
956 }
957
958 ; CHECK-LABEL: @icmp_mul_eq0(
959 ; CHECK-NEXT: icmp eq i32 %x, 0
960 define i1 @icmp_mul_eq0(i32 %x) {
961   %mul = mul nsw i32 %x, 12
962   %cmp = icmp eq i32 %mul, 0
963   ret i1 %cmp
964 }
965
966 ; CHECK-LABEL: @icmp_mul0_eq0(
967 ; CHECK-NEXT: ret i1 true
968 define i1 @icmp_mul0_eq0(i32 %x) {
969   %mul = mul i32 %x, 0
970   %cmp = icmp eq i32 %mul, 0
971   ret i1 %cmp
972 }
973
974 ; CHECK-LABEL: @icmp_mul0_ne0(
975 ; CHECK-NEXT: ret i1 false
976 define i1 @icmp_mul0_ne0(i32 %x) {
977   %mul = mul i32 %x, 0
978   %cmp = icmp ne i32 %mul, 0
979   ret i1 %cmp
980 }
981
982 ; CHECK-LABEL: @icmp_sub1_sge(
983 ; CHECK-NEXT: icmp sgt i32 %x, %y
984 define i1 @icmp_sub1_sge(i32 %x, i32 %y) {
985   %sub = add nsw i32 %x, -1
986   %cmp = icmp sge i32 %sub, %y
987   ret i1 %cmp
988 }
989
990 ; CHECK-LABEL: @icmp_add1_sgt(
991 ; CHECK-NEXT: icmp sge i32 %x, %y
992 define i1 @icmp_add1_sgt(i32 %x, i32 %y) {
993   %add = add nsw i32 %x, 1
994   %cmp = icmp sgt i32 %add, %y
995   ret i1 %cmp
996 }
997
998 ; CHECK-LABEL: @icmp_sub1_slt(
999 ; CHECK-NEXT: icmp sle i32 %x, %y
1000 define i1 @icmp_sub1_slt(i32 %x, i32 %y) {
1001   %sub = add nsw i32 %x, -1
1002   %cmp = icmp slt i32 %sub, %y
1003   ret i1 %cmp
1004 }
1005
1006 ; CHECK-LABEL: @icmp_add1_sle(
1007 ; CHECK-NEXT: icmp slt i32 %x, %y
1008 define i1 @icmp_add1_sle(i32 %x, i32 %y) {
1009   %add = add nsw i32 %x, 1
1010   %cmp = icmp sle i32 %add, %y
1011   ret i1 %cmp
1012 }
1013
1014 ; CHECK-LABEL: @icmp_add20_sge_add57(
1015 ; CHECK-NEXT: [[ADD:%[a-z0-9]+]] = add nsw i32 %y, 37
1016 ; CHECK-NEXT: icmp sle i32 [[ADD]], %x
1017 define i1 @icmp_add20_sge_add57(i32 %x, i32 %y) {
1018   %1 = add nsw i32 %x, 20
1019   %2 = add nsw i32 %y, 57
1020   %cmp = icmp sge i32 %1, %2
1021   ret i1 %cmp
1022 }
1023
1024 ; CHECK-LABEL: @icmp_sub57_sge_sub20(
1025 ; CHECK-NEXT: [[SUB:%[a-z0-9]+]] = add nsw i32 %x, -37
1026 ; CHECK-NEXT: icmp sge i32 [[SUB]], %y
1027 define i1 @icmp_sub57_sge_sub20(i32 %x, i32 %y) {
1028   %1 = add nsw i32 %x, -57
1029   %2 = add nsw i32 %y, -20
1030   %cmp = icmp sge i32 %1, %2
1031   ret i1 %cmp
1032 }
1033
1034 ; CHECK-LABEL: @icmp_and_shl_neg_ne_0(
1035 ; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl i32 1, %B
1036 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHL]], %A
1037 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp eq i32 [[AND]], 0
1038 ; CHECK-NEXT: ret i1 [[CMP]]
1039 define i1 @icmp_and_shl_neg_ne_0(i32 %A, i32 %B) {
1040   %neg = xor i32 %A, -1
1041   %shl = shl i32 1, %B
1042   %and = and i32 %shl, %neg
1043   %cmp = icmp ne i32 %and, 0
1044   ret i1 %cmp
1045 }
1046
1047 ; CHECK-LABEL: @icmp_and_shl_neg_eq_0(
1048 ; CHECK-NEXT: [[SHL:%[a-z0-9]+]] = shl i32 1, %B
1049 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 [[SHL]], %A
1050 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ne i32 [[AND]], 0
1051 ; CHECK-NEXT: ret i1 [[CMP]]
1052 define i1 @icmp_and_shl_neg_eq_0(i32 %A, i32 %B) {
1053   %neg = xor i32 %A, -1
1054   %shl = shl i32 1, %B
1055   %and = and i32 %shl, %neg
1056   %cmp = icmp eq i32 %and, 0
1057   ret i1 %cmp
1058 }
1059
1060 ; CHECK-LABEL: @icmp_add_and_shr_ne_0(
1061 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %X, 240
1062 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ne i32 [[AND]], 224
1063 ; CHECK-NEXT: ret i1 [[CMP]]
1064 define i1 @icmp_add_and_shr_ne_0(i32 %X) {
1065   %shr = lshr i32 %X, 4
1066   %and = and i32 %shr, 15
1067   %add = add i32 %and, -14
1068   %tobool = icmp ne i32 %add, 0
1069   ret i1 %tobool
1070 }
1071
1072 ; PR16244
1073 ; CHECK-LABEL: define i1 @test71(
1074 ; CHECK-NEXT: ret i1 false
1075 define i1 @test71(i8* %x) {
1076   %a = getelementptr i8* %x, i64 8
1077   %b = getelementptr inbounds i8* %x, i64 8
1078   %c = icmp ugt i8* %a, %b
1079   ret i1 %c
1080 }
1081
1082 define i1 @test71_as1(i8 addrspace(1)* %x) {
1083 ; CHECK-LABEL: @test71_as1(
1084 ; CHECK-NEXT: ret i1 false
1085   %a = getelementptr i8 addrspace(1)* %x, i64 8
1086   %b = getelementptr inbounds i8 addrspace(1)* %x, i64 8
1087   %c = icmp ugt i8 addrspace(1)* %a, %b
1088   ret i1 %c
1089 }
1090
1091 ; CHECK-LABEL: @icmp_shl_1_V_ult_32(
1092 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ult i32 %V, 5
1093 ; CHECK-NEXT: ret i1 [[CMP]]
1094 define i1 @icmp_shl_1_V_ult_32(i32 %V) {
1095   %shl = shl i32 1, %V
1096   %cmp = icmp ult i32 %shl, 32
1097   ret i1 %cmp
1098 }
1099
1100 ; CHECK-LABEL: @icmp_shl_1_V_eq_32(
1101 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp eq i32 %V, 5
1102 ; CHECK-NEXT: ret i1 [[CMP]]
1103 define i1 @icmp_shl_1_V_eq_32(i32 %V) {
1104   %shl = shl i32 1, %V
1105   %cmp = icmp eq i32 %shl, 32
1106   ret i1 %cmp
1107 }
1108
1109 ; CHECK-LABEL: @icmp_shl_1_V_eq_31(
1110 ; CHECK-NEXT: ret i1 false
1111 define i1 @icmp_shl_1_V_eq_31(i32 %V) {
1112   %shl = shl i32 1, %V
1113   %cmp = icmp eq i32 %shl, 31
1114   ret i1 %cmp
1115 }
1116
1117 ; CHECK-LABEL: @icmp_shl_1_V_ne_31(
1118 ; CHECK-NEXT: ret i1 true
1119 define i1 @icmp_shl_1_V_ne_31(i32 %V) {
1120   %shl = shl i32 1, %V
1121   %cmp = icmp ne i32 %shl, 31
1122   ret i1 %cmp
1123 }
1124
1125 ; CHECK-LABEL: @icmp_shl_1_V_ult_30(
1126 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ult i32 %V, 5
1127 ; CHECK-NEXT: ret i1 [[CMP]]
1128 define i1 @icmp_shl_1_V_ult_30(i32 %V) {
1129   %shl = shl i32 1, %V
1130   %cmp = icmp ult i32 %shl, 30
1131   ret i1 %cmp
1132 }
1133
1134 ; CHECK-LABEL: @icmp_shl_1_V_ugt_30(
1135 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ugt i32 %V, 4
1136 ; CHECK-NEXT: ret i1 [[CMP]]
1137 define i1 @icmp_shl_1_V_ugt_30(i32 %V) {
1138   %shl = shl i32 1, %V
1139   %cmp = icmp ugt i32 %shl, 30
1140   ret i1 %cmp
1141 }
1142
1143 ; CHECK-LABEL: @icmp_shl_1_V_ule_30(
1144 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ult i32 %V, 5
1145 ; CHECK-NEXT: ret i1 [[CMP]]
1146 define i1 @icmp_shl_1_V_ule_30(i32 %V) {
1147   %shl = shl i32 1, %V
1148   %cmp = icmp ule i32 %shl, 30
1149   ret i1 %cmp
1150 }
1151
1152 ; CHECK-LABEL: @icmp_shl_1_V_uge_30(
1153 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ugt i32 %V, 4
1154 ; CHECK-NEXT: ret i1 [[CMP]]
1155 define i1 @icmp_shl_1_V_uge_30(i32 %V) {
1156   %shl = shl i32 1, %V
1157   %cmp = icmp uge i32 %shl, 30
1158   ret i1 %cmp
1159 }
1160
1161 ; CHECK-LABEL: @icmp_shl_1_V_uge_2147483648(
1162 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp eq i32 %V, 31
1163 ; CHECK-NEXT: ret i1 [[CMP]]
1164 define i1 @icmp_shl_1_V_uge_2147483648(i32 %V) {
1165   %shl = shl i32 1, %V
1166   %cmp = icmp uge i32 %shl, 2147483648
1167   ret i1 %cmp
1168 }
1169
1170 ; CHECK-LABEL: @icmp_shl_1_V_ugt_2147483648(
1171 ; CHECK-NEXT: ret i1 false
1172 define i1 @icmp_shl_1_V_ugt_2147483648(i32 %V) {
1173   %shl = shl i32 1, %V
1174   %cmp = icmp ugt i32 %shl, 2147483648
1175   ret i1 %cmp
1176 }
1177
1178 ; CHECK-LABEL: @icmp_shl_1_V_ule_2147483648(
1179 ; CHECK-NEXT: ret i1 true
1180 define i1 @icmp_shl_1_V_ule_2147483648(i32 %V) {
1181   %shl = shl i32 1, %V
1182   %cmp = icmp ule i32 %shl, 2147483648
1183   ret i1 %cmp
1184 }
1185
1186 ; CHECK-LABEL: @icmp_shl_1_V_ult_2147483648(
1187 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ne i32 %V, 31
1188 ; CHECK-NEXT: ret i1 [[CMP]]
1189 define i1 @icmp_shl_1_V_ult_2147483648(i32 %V) {
1190   %shl = shl i32 1, %V
1191   %cmp = icmp ult i32 %shl, 2147483648
1192   ret i1 %cmp
1193 }
1194
1195 ; CHECK-LABEL: @or_icmp_eq_B_0_icmp_ult_A_B(
1196 ; CHECK-NEXT: [[SUB:%[a-z0-9]+]] = add i64 %b, -1
1197 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp uge i64 [[SUB]], %a
1198 ; CHECK-NEXT: ret i1 [[CMP]]
1199 define i1 @or_icmp_eq_B_0_icmp_ult_A_B(i64 %a, i64 %b) {
1200   %1 = icmp eq i64 %b, 0
1201   %2 = icmp ult i64 %a, %b
1202   %3 = or i1 %1, %2
1203   ret i1 %3
1204 }
1205
1206 ; CHECK-LABEL: @icmp_add_ult_2(
1207 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %X, -2
1208 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp eq i32 [[AND]], 14
1209 ; CHECK-NEXT: ret i1 [[CMP]]
1210 define i1 @icmp_add_ult_2(i32 %X) {
1211   %add = add i32 %X, -14
1212   %cmp = icmp ult i32 %add, 2
1213   ret i1 %cmp
1214 }
1215
1216 ; CHECK: @icmp_add_X_-14_ult_2
1217 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %X, -2
1218 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp eq i32 [[AND]], 14
1219 ; CHECK-NEXT: ret i1 [[CMP]]
1220 define i1 @icmp_add_X_-14_ult_2(i32 %X) {
1221   %add = add i32 %X, -14
1222   %cmp = icmp ult i32 %add, 2
1223   ret i1 %cmp
1224 }
1225
1226 ; CHECK-LABEL: @icmp_sub_3_X_ult_2(
1227 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %X, 1
1228 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp eq i32 [[OR]], 3
1229 ; CHECK-NEXT: ret i1 [[CMP]]
1230 define i1 @icmp_sub_3_X_ult_2(i32 %X) {
1231   %add = sub i32 3, %X
1232   %cmp = icmp ult i32 %add, 2
1233   ret i1 %cmp
1234 }
1235
1236 ; CHECK: @icmp_add_X_-14_uge_2
1237 ; CHECK-NEXT: [[AND:%[a-z0-9]+]] = and i32 %X, -2
1238 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ne i32 [[AND]], 14
1239 ; CHECK-NEXT: ret i1 [[CMP]]
1240 define i1 @icmp_add_X_-14_uge_2(i32 %X) {
1241   %add = add i32 %X, -14
1242   %cmp = icmp uge i32 %add, 2
1243   ret i1 %cmp
1244 }
1245
1246 ; CHECK-LABEL: @icmp_sub_3_X_uge_2(
1247 ; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %X, 1
1248 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ne i32 [[OR]], 3
1249 ; CHECK-NEXT: ret i1 [[CMP]]
1250 define i1 @icmp_sub_3_X_uge_2(i32 %X) {
1251   %add = sub i32 3, %X
1252   %cmp = icmp uge i32 %add, 2
1253   ret i1 %cmp
1254 }
1255
1256 ; CHECK: @icmp_and_X_-16_eq-16
1257 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ugt i32 %X, -17
1258 ; CHECK-NEXT: ret i1 [[CMP]]
1259 define i1 @icmp_and_X_-16_eq-16(i32 %X) {
1260   %and = and i32 %X, -16
1261   %cmp = icmp eq i32 %and, -16
1262   ret i1 %cmp
1263 }
1264
1265 ; CHECK: @icmp_and_X_-16_ne-16
1266 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ult i32 %X, -16
1267 ; CHECK-NEXT: ret i1 [[CMP]]
1268 define i1 @icmp_and_X_-16_ne-16(i32 %X) {
1269   %and = and i32 %X, -16
1270   %cmp = icmp ne i32 %and, -16
1271   ret i1 %cmp
1272 }
1273
1274 ; CHECK: @icmp_sub_-1_X_ult_4
1275 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ugt i32 %X, -5
1276 ; CHECK-NEXT: ret i1 [[CMP]]
1277 define i1 @icmp_sub_-1_X_ult_4(i32 %X) {
1278   %sub = sub i32 -1, %X
1279   %cmp = icmp ult i32 %sub, 4
1280   ret i1 %cmp
1281 }
1282
1283 ; CHECK: @icmp_sub_-1_X_uge_4
1284 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp ult i32 %X, -4
1285 ; CHECK-NEXT: ret i1 [[CMP]]
1286 define i1 @icmp_sub_-1_X_uge_4(i32 %X) {
1287   %sub = sub i32 -1, %X
1288   %cmp = icmp uge i32 %sub, 4
1289   ret i1 %cmp
1290 }
1291
1292 ; CHECK-LABEL: @icmp_swap_operands_for_cse
1293 ; CHECK: [[CMP:%[a-z0-9]+]] = icmp ult i32 %X, %Y
1294 ; CHECK-NEXT: br i1 [[CMP]], label %true, label %false
1295 ; CHECK: ret i1
1296 define i1 @icmp_swap_operands_for_cse(i32 %X, i32 %Y) {
1297 entry:
1298   %sub = sub i32 %X, %Y
1299   %cmp = icmp ugt i32 %Y, %X
1300   br i1 %cmp, label %true, label %false
1301 true:
1302   %restrue = trunc i32 %sub to i1
1303   br label %end
1304 false:
1305   %shift = lshr i32 %sub, 4
1306   %resfalse = trunc i32 %shift to i1
1307   br label %end
1308 end:
1309   %res = phi i1 [%restrue, %true], [%resfalse, %false]
1310   ret i1 %res
1311 }
1312
1313 ; CHECK-LABEL: @icmp_swap_operands_for_cse2
1314 ; CHECK: [[CMP:%[a-z0-9]+]] = icmp ult i32 %X, %Y
1315 ; CHECK-NEXT: br i1 [[CMP]], label %true, label %false
1316 ; CHECK: ret i1
1317 define i1 @icmp_swap_operands_for_cse2(i32 %X, i32 %Y) {
1318 entry:
1319   %cmp = icmp ugt i32 %Y, %X
1320   br i1 %cmp, label %true, label %false
1321 true:
1322   %sub = sub i32 %X, %Y
1323   %sub1 = sub i32 %X, %Y
1324   %add = add i32 %sub, %sub1
1325   %restrue = trunc i32 %add to i1
1326   br label %end
1327 false:
1328   %sub2 = sub i32 %Y, %X
1329   %resfalse = trunc i32 %sub2 to i1
1330   br label %end
1331 end:
1332   %res = phi i1 [%restrue, %true], [%resfalse, %false]
1333   ret i1 %res
1334 }
1335
1336 ; CHECK-LABEL: @icmp_do_not_swap_operands_for_cse
1337 ; CHECK: [[CMP:%[a-z0-9]+]] = icmp ugt i32 %Y, %X
1338 ; CHECK-NEXT: br i1 [[CMP]], label %true, label %false
1339 ; CHECK: ret i1
1340 define i1 @icmp_do_not_swap_operands_for_cse(i32 %X, i32 %Y) {
1341 entry:
1342   %cmp = icmp ugt i32 %Y, %X
1343   br i1 %cmp, label %true, label %false
1344 true:
1345   %sub = sub i32 %X, %Y
1346   %restrue = trunc i32 %sub to i1
1347   br label %end
1348 false:
1349   %sub2 = sub i32 %Y, %X
1350   %resfalse = trunc i32 %sub2 to i1
1351   br label %end
1352 end:
1353   %res = phi i1 [%restrue, %true], [%resfalse, %false]
1354   ret i1 %res
1355 }
1356
1357 ; CHECK-LABEL: @icmp_lshr_lshr_eq
1358 ; CHECK: %z.unshifted = xor i32 %a, %b
1359 ; CHECK: %z = icmp ult i32 %z.unshifted, 1073741824
1360 define i1 @icmp_lshr_lshr_eq(i32 %a, i32 %b) nounwind {
1361  %x = lshr i32 %a, 30
1362  %y = lshr i32 %b, 30
1363  %z = icmp eq i32 %x, %y
1364  ret i1 %z
1365 }
1366
1367 ; CHECK-LABEL: @icmp_ashr_ashr_ne
1368 ; CHECK: %z.unshifted = xor i32 %a, %b
1369 ; CHECK: %z = icmp ugt i32 %z.unshifted, 255
1370 define i1 @icmp_ashr_ashr_ne(i32 %a, i32 %b) nounwind {
1371  %x = ashr i32 %a, 8
1372  %y = ashr i32 %b, 8
1373  %z = icmp ne i32 %x, %y
1374  ret i1 %z
1375 }
1376
1377 ; CHECK-LABEL: @icmp_neg_cst_slt
1378 ; CHECK-NEXT: [[CMP:%[a-z0-9]+]] = icmp sgt i32 %a, 10
1379 ; CHECK-NEXT: ret i1 [[CMP]]
1380 define i1 @icmp_neg_cst_slt(i32 %a) {
1381   %1 = sub nsw i32 0, %a
1382   %2 = icmp slt i32 %1, -10
1383   ret i1 %2
1384 }
1385
1386 ; CHECK-LABEL: @exact_ashr_eq_false
1387 ; CHECK-NEXT: icmp eq i32 %a, 1
1388 define i1 @exact_ashr_eq_false(i32 %a) {
1389   %shr = ashr exact i32 -30, %a
1390   %cmp = icmp eq i32 %shr, -15
1391   ret i1 %cmp
1392 }