Optimize icmp involving addition better
[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-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: @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: @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: @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: @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: @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: @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: @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: @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: @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   
105 ; CHECK: @test10
106 ; CHECK: %b = icmp ne i32 %x, -2147483648
107 ; CHECK: ret i1 %b
108 }
109
110 define i1 @test11(i32 %x) {
111   %a = add nsw i32 %x, 8
112   %b = icmp slt i32 %x, %a
113   ret i1 %b
114 ; CHECK: @test11  
115 ; CHECK: ret i1 true
116 }
117
118 ; PR6195
119 define i1 @test12(i1 %A) {
120   %S = select i1 %A, i64 -4294967295, i64 8589934591
121   %B = icmp ne i64 bitcast (<2 x i32> <i32 1, i32 -1> to i64), %S
122   ret i1 %B
123 ; CHECK: @test12
124 ; CHECK-NEXT: = xor i1 %A, true
125 ; CHECK-NEXT: ret i1
126 }
127
128 ; PR6481
129 define i1 @test13(i8 %X) nounwind readnone {
130 entry:
131         %cmp = icmp slt i8 undef, %X
132         ret i1 %cmp
133 ; CHECK: @test13
134 ; CHECK: ret i1 false
135 }
136
137 define i1 @test14(i8 %X) nounwind readnone {
138 entry:
139         %cmp = icmp slt i8 undef, -128
140         ret i1 %cmp
141 ; CHECK: @test14
142 ; CHECK: ret i1 false
143 }
144
145 define i1 @test15() nounwind readnone {
146 entry:
147         %cmp = icmp eq i8 undef, -128
148         ret i1 %cmp
149 ; CHECK: @test15
150 ; CHECK: ret i1 undef
151 }
152
153 define i1 @test16() nounwind readnone {
154 entry:
155         %cmp = icmp ne i8 undef, -128
156         ret i1 %cmp
157 ; CHECK: @test16
158 ; CHECK: ret i1 undef
159 }
160
161 define i1 @test17(i32 %x) nounwind {
162   %shl = shl i32 1, %x
163   %and = and i32 %shl, 8
164   %cmp = icmp eq i32 %and, 0
165   ret i1 %cmp
166 ; CHECK: @test17
167 ; CHECK-NEXT: %cmp = icmp ne i32 %x, 3
168 }
169
170
171 define i1 @test18(i32 %x) nounwind {
172   %sh = lshr i32 8, %x
173   %and = and i32 %sh, 1
174   %cmp = icmp eq i32 %and, 0
175   ret i1 %cmp
176 ; CHECK: @test18
177 ; CHECK-NEXT: %cmp = icmp ne i32 %x, 3
178 }
179
180 define i1 @test19(i32 %x) nounwind {
181   %shl = shl i32 1, %x
182   %and = and i32 %shl, 8
183   %cmp = icmp eq i32 %and, 8
184   ret i1 %cmp
185 ; CHECK: @test19
186 ; CHECK-NEXT: %cmp = icmp eq i32 %x, 3
187 }
188
189 define i1 @test20(i32 %x) nounwind {
190   %shl = shl i32 1, %x
191   %and = and i32 %shl, 8
192   %cmp = icmp ne i32 %and, 0
193   ret i1 %cmp
194 ; CHECK: @test20
195 ; CHECK-NEXT: %cmp = icmp eq i32 %x, 3
196 }
197
198 define i1 @test21(i8 %x, i8 %y) {
199 ; CHECK: @test21
200 ; CHECK-NOT: or i8
201 ; CHECK: icmp ugt
202   %A = or i8 %x, 1
203   %B = icmp ugt i8 %A, 3
204   ret i1 %B
205 }
206
207 define i1 @test22(i8 %x, i8 %y) {
208 ; CHECK: @test22
209 ; CHECK-NOT: or i8
210 ; CHECK: icmp ult
211   %A = or i8 %x, 1
212   %B = icmp ult i8 %A, 4
213   ret i1 %B
214 }
215
216 ; PR2740
217 ; CHECK: @test23
218 ; CHECK: icmp sgt i32 %x, 1328634634
219 define i1 @test23(i32 %x) nounwind {
220         %i3 = sdiv i32 %x, -1328634635
221         %i4 = icmp eq i32 %i3, -1
222         ret i1 %i4
223 }
224
225 @X = global [1000 x i32] zeroinitializer
226
227 ; PR8882
228 ; CHECK: @test24
229 ; CHECK:    %cmp = icmp eq i64 %i, 1000
230 ; CHECK:   ret i1 %cmp
231 define i1 @test24(i64 %i) {
232   %p1 = getelementptr inbounds i32* getelementptr inbounds ([1000 x i32]* @X, i64 0, i64 0), i64 %i
233   %cmp = icmp eq i32* %p1, getelementptr inbounds ([1000 x i32]* @X, i64 1, i64 0)
234   ret i1 %cmp
235 }
236
237 ; CHECK: @test25
238 ; X + Z > Y + Z -> X > Y if there is no overflow.
239 ; CHECK: %c = icmp sgt i32 %x, %y
240 ; CHECK: ret i1 %c
241 define i1 @test25(i32 %x, i32 %y, i32 %z) {
242   %lhs = add nsw i32 %x, %z
243   %rhs = add nsw i32 %y, %z
244   %c = icmp sgt i32 %lhs, %rhs
245   ret i1 %c
246 }
247
248 ; CHECK: @test26
249 ; X + Z > Y + Z -> X > Y if there is no overflow.
250 ; CHECK: %c = icmp ugt i32 %x, %y
251 ; CHECK: ret i1 %c
252 define i1 @test26(i32 %x, i32 %y, i32 %z) {
253   %lhs = add nuw i32 %x, %z
254   %rhs = add nuw i32 %y, %z
255   %c = icmp ugt i32 %lhs, %rhs
256   ret i1 %c
257 }
258
259 ; CHECK: @test27
260 ; X - Z > Y - Z -> X > Y if there is no overflow.
261 ; CHECK: %c = icmp sgt i32 %x, %y
262 ; CHECK: ret i1 %c
263 define i1 @test27(i32 %x, i32 %y, i32 %z) {
264   %lhs = sub nsw i32 %x, %z
265   %rhs = sub nsw i32 %y, %z
266   %c = icmp sgt i32 %lhs, %rhs
267   ret i1 %c
268 }
269
270 ; CHECK: @test28
271 ; X - Z > Y - Z -> X > Y if there is no overflow.
272 ; CHECK: %c = icmp ugt i32 %x, %y
273 ; CHECK: ret i1 %c
274 define i1 @test28(i32 %x, i32 %y, i32 %z) {
275   %lhs = sub nuw i32 %x, %z
276   %rhs = sub nuw i32 %y, %z
277   %c = icmp ugt i32 %lhs, %rhs
278   ret i1 %c
279 }
280
281 ; CHECK: @test29
282 ; X + Y > X -> Y > 0 if there is no overflow.
283 ; CHECK: %c = icmp sgt i32 %y, 0
284 ; CHECK: ret i1 %c
285 define i1 @test29(i32 %x, i32 %y) {
286   %lhs = add nsw i32 %x, %y
287   %c = icmp sgt i32 %lhs, %x
288   ret i1 %c
289 }
290
291 ; CHECK: @test30
292 ; X + Y > X -> Y > 0 if there is no overflow.
293 ; CHECK: %c = icmp ne i32 %y, 0
294 ; CHECK: ret i1 %c
295 define i1 @test30(i32 %x, i32 %y) {
296   %lhs = add nuw i32 %x, %y
297   %c = icmp ugt i32 %lhs, %x
298   ret i1 %c
299 }
300
301 ; CHECK: @test31
302 ; X > X + Y -> 0 > Y if there is no overflow.
303 ; CHECK: %c = icmp slt i32 %y, 0
304 ; CHECK: ret i1 %c
305 define i1 @test31(i32 %x, i32 %y) {
306   %rhs = add nsw i32 %x, %y
307   %c = icmp sgt i32 %x, %rhs
308   ret i1 %c
309 }
310
311 ; CHECK: @test32
312 ; X > X + Y -> 0 > Y if there is no overflow.
313 ; CHECK: ret i1 false
314 define i1 @test32(i32 %x, i32 %y) {
315   %rhs = add nuw i32 %x, %y
316   %c = icmp ugt i32 %x, %rhs
317   ret i1 %c
318 }
319
320 ; CHECK: @test33
321 ; X - Y > X -> 0 > Y if there is no overflow.
322 ; CHECK: %c = icmp slt i32 %y, 0
323 ; CHECK: ret i1 %c
324 define i1 @test33(i32 %x, i32 %y) {
325   %lhs = sub nsw i32 %x, %y
326   %c = icmp sgt i32 %lhs, %x
327   ret i1 %c
328 }
329
330 ; CHECK: @test34
331 ; X - Y > X -> 0 > Y if there is no overflow.
332 ; CHECK: ret i1 false
333 define i1 @test34(i32 %x, i32 %y) {
334   %lhs = sub nuw i32 %x, %y
335   %c = icmp ugt i32 %lhs, %x
336   ret i1 %c
337 }
338
339 ; CHECK: @test35
340 ; X > X - Y -> Y > 0 if there is no overflow.
341 ; CHECK: %c = icmp sgt i32 %y, 0
342 ; CHECK: ret i1 %c
343 define i1 @test35(i32 %x, i32 %y) {
344   %rhs = sub nsw i32 %x, %y
345   %c = icmp sgt i32 %x, %rhs
346   ret i1 %c
347 }
348
349 ; CHECK: @test36
350 ; X > X - Y -> Y > 0 if there is no overflow.
351 ; CHECK: %c = icmp ne i32 %y, 0
352 ; CHECK: ret i1 %c
353 define i1 @test36(i32 %x, i32 %y) {
354   %rhs = sub nuw i32 %x, %y
355   %c = icmp ugt i32 %x, %rhs
356   ret i1 %c
357 }
358
359 ; CHECK: @test37
360 ; X - Y > X - Z -> Z > Y if there is no overflow.
361 ; CHECK: %c = icmp sgt i32 %z, %y
362 ; CHECK: ret i1 %c
363 define i1 @test37(i32 %x, i32 %y, i32 %z) {
364   %lhs = sub nsw i32 %x, %y
365   %rhs = sub nsw i32 %x, %z
366   %c = icmp sgt i32 %lhs, %rhs
367   ret i1 %c
368 }
369
370 ; CHECK: @test38
371 ; X - Y > X - Z -> Z > Y if there is no overflow.
372 ; CHECK: %c = icmp ugt i32 %z, %y
373 ; CHECK: ret i1 %c
374 define i1 @test38(i32 %x, i32 %y, i32 %z) {
375   %lhs = sub nuw i32 %x, %y
376   %rhs = sub nuw i32 %x, %z
377   %c = icmp ugt i32 %lhs, %rhs
378   ret i1 %c
379 }
380
381 ; PR9343 #1
382 ; CHECK: @test39
383 ; CHECK: %B = icmp eq i32 %X, 0
384 define i1 @test39(i32 %X, i32 %Y) {
385   %A = ashr exact i32 %X, %Y
386   %B = icmp eq i32 %A, 0
387   ret i1 %B
388 }
389
390 ; CHECK: @test40
391 ; CHECK: %B = icmp ne i32 %X, 0
392 define i1 @test40(i32 %X, i32 %Y) {
393   %A = lshr exact i32 %X, %Y
394   %B = icmp ne i32 %A, 0
395   ret i1 %B
396 }
397
398 ; PR9343 #3
399 ; CHECK: @test41
400 ; CHECK: ret i1 true
401 define i1 @test41(i32 %X, i32 %Y) {
402   %A = urem i32 %X, %Y
403   %B = icmp ugt i32 %Y, %A
404   ret i1 %B
405 }
406
407 ; CHECK: @test42
408 ; CHECK: %B = icmp sgt i32 %Y, -1
409 define i1 @test42(i32 %X, i32 %Y) {
410   %A = srem i32 %X, %Y
411   %B = icmp slt i32 %A, %Y
412   ret i1 %B
413 }
414
415 ; CHECK: @test43
416 ; CHECK: %B = icmp slt i32 %Y, 0
417 define i1 @test43(i32 %X, i32 %Y) {
418   %A = srem i32 %X, %Y
419   %B = icmp slt i32 %Y, %A
420   ret i1 %B
421 }
422
423 ; CHECK: @test44
424 ; CHECK: %B = icmp sgt i32 %Y, -1
425 define i1 @test44(i32 %X, i32 %Y) {
426   %A = srem i32 %X, %Y
427   %B = icmp slt i32 %A, %Y
428   ret i1 %B
429 }
430
431 ; CHECK: @test45
432 ; CHECK: %B = icmp slt i32 %Y, 0
433 define i1 @test45(i32 %X, i32 %Y) {
434   %A = srem i32 %X, %Y
435   %B = icmp slt i32 %Y, %A
436   ret i1 %B
437 }
438
439 ; PR9343 #4
440 ; CHECK: @test46
441 ; CHECK: %C = icmp ult i32 %X, %Y
442 define i1 @test46(i32 %X, i32 %Y, i32 %Z) {
443   %A = ashr exact i32 %X, %Z
444   %B = ashr exact i32 %Y, %Z
445   %C = icmp ult i32 %A, %B
446   ret i1 %C
447 }
448
449 ; PR9343 #5
450 ; CHECK: @test47
451 ; CHECK: %C = icmp ugt i32 %X, %Y
452 define i1 @test47(i32 %X, i32 %Y, i32 %Z) {
453   %A = ashr exact i32 %X, %Z
454   %B = ashr exact i32 %Y, %Z
455   %C = icmp ugt i32 %A, %B
456   ret i1 %C
457 }
458
459 ; PR9343 #8
460 ; CHECK: @test48
461 ; CHECK: %C = icmp eq i32 %X, %Y
462 define i1 @test48(i32 %X, i32 %Y, i32 %Z) {
463   %A = sdiv exact i32 %X, %Z
464   %B = sdiv exact i32 %Y, %Z
465   %C = icmp eq i32 %A, %B
466   ret i1 %C
467 }
468
469 ; PR8469
470 ; CHECK: @test49
471 ; CHECK: ret <2 x i1> <i1 true, i1 true>
472 define <2 x i1> @test49(<2 x i32> %tmp3) {
473 entry:
474   %tmp11 = and <2 x i32> %tmp3, <i32 3, i32 3>
475   %cmp = icmp ult <2 x i32> %tmp11, <i32 4, i32 4>
476   ret <2 x i1> %cmp  
477 }
478
479 ; PR9343 #7
480 ; CHECK: @test50
481 ; CHECK: ret i1 true
482 define i1 @test50(i16 %X, i32 %Y) {
483   %A = zext i16 %X to i32
484   %B = srem i32 %A, %Y
485   %C = icmp sgt i32 %B, -1
486   ret i1 %C
487 }
488
489 ; CHECK: @test51
490 ; CHECK: ret i1 %C
491 define i1 @test51(i32 %X, i32 %Y) {
492   %A = and i32 %X, 2147483648
493   %B = srem i32 %A, %Y
494   %C = icmp sgt i32 %B, -1
495   ret i1 %C
496 }
497
498 ; CHECK: @test52
499 ; CHECK-NEXT: and i32 %x1, 16711935
500 ; CHECK-NEXT: icmp eq i32 {{.*}}, 4980863
501 ; CHECK-NEXT: ret i1
502 define i1 @test52(i32 %x1) nounwind {
503   %conv = and i32 %x1, 255
504   %cmp = icmp eq i32 %conv, 127
505   %tmp2 = lshr i32 %x1, 16
506   %tmp3 = trunc i32 %tmp2 to i8
507   %cmp15 = icmp eq i8 %tmp3, 76
508
509   %A = and i1 %cmp, %cmp15
510   ret i1 %A
511 }
512
513 ; PR9838
514 ; CHECK: @test53
515 ; CHECK-NEXT: ashr exact
516 ; CHECK-NEXT: ashr
517 ; CHECK-NEXT: icmp
518 define i1 @test53(i32 %a, i32 %b) nounwind {
519  %x = ashr exact i32 %a, 30
520  %y = ashr i32 %b, 30
521  %z = icmp eq i32 %x, %y
522  ret i1 %z
523 }
524
525 ; CHECK: @test54
526 ; CHECK-NEXT: %and = and i8 %a, -64
527 ; CHECK-NEXT: icmp eq i8 %and, -128
528 define i1 @test54(i8 %a) nounwind {
529   %ext = zext i8 %a to i32
530   %and = and i32 %ext, 192
531   %ret = icmp eq i32 %and, 128
532   ret i1 %ret
533 }
534
535 ; CHECK: @test55
536 ; CHECK-NEXT: icmp eq i32 %a, -123
537 define i1 @test55(i32 %a) {
538   %sub = sub i32 0, %a
539   %cmp = icmp eq i32 %sub, 123
540   ret i1 %cmp
541 }
542
543 ; CHECK: @test56
544 ; CHECK-NEXT: icmp eq i32 %a, -113
545 define i1 @test56(i32 %a) {
546   %sub = sub i32 10, %a
547   %cmp = icmp eq i32 %sub, 123
548   ret i1 %cmp
549 }
550
551 ; PR10267 Don't make icmps more expensive when no other inst is subsumed.
552 declare void @foo(i32)
553 ; CHECK: @test57
554 ; CHECK: %and = and i32 %a, -2
555 ; CHECK: %cmp = icmp ne i32 %and, 0
556 define i1 @test57(i32 %a) {
557   %and = and i32 %a, -2
558   %cmp = icmp ne i32 %and, 0
559   call void @foo(i32 %and)
560   ret i1 %cmp
561 }
562
563 ; rdar://problem/10482509
564 ; CHECK: @cmpabs1
565 ; CHECK-NEXT: icmp ne
566 define zeroext i1 @cmpabs1(i64 %val) {
567   %sub = sub nsw i64 0, %val
568   %cmp = icmp slt i64 %val, 0
569   %sub.val = select i1 %cmp, i64 %sub, i64 %val
570   %tobool = icmp ne i64 %sub.val, 0
571   ret i1 %tobool
572 }
573
574 ; CHECK: @cmpabs2
575 ; CHECK-NEXT: icmp ne
576 define zeroext i1 @cmpabs2(i64 %val) {
577   %sub = sub nsw i64 0, %val
578   %cmp = icmp slt i64 %val, 0
579   %sub.val = select i1 %cmp, i64 %val, i64 %sub
580   %tobool = icmp ne i64 %sub.val, 0
581   ret i1 %tobool
582 }
583
584 ; CHECK: @test58
585 ; CHECK-NEXT: call i32 @test58_d(i64 36029346783166592)
586 define void @test58() nounwind {
587   %cast = bitcast <1 x i64> <i64 36029346783166592> to i64
588   %call = call i32 @test58_d( i64 %cast) nounwind
589   ret void
590 }
591 declare i32 @test58_d(i64)
592
593 define i1 @test59(i8* %foo) {
594   %bit = bitcast i8* %foo to i32*
595   %gep1 = getelementptr inbounds i32* %bit, i64 2
596   %gep2 = getelementptr inbounds i8* %foo, i64 10
597   %cast1 = bitcast i32* %gep1 to i8*
598   %cmp = icmp ult i8* %cast1, %gep2
599   %use = ptrtoint i8* %cast1 to i64
600   %call = call i32 @test58_d(i64 %use) nounwind
601   ret i1 %cmp
602 ; CHECK: @test59
603 ; CHECK: ret i1 true
604 }
605
606 define i1 @test60(i8* %foo, i64 %i, i64 %j) {
607   %bit = bitcast i8* %foo to i32*
608   %gep1 = getelementptr inbounds i32* %bit, i64 %i
609   %gep2 = getelementptr inbounds i8* %foo, i64 %j
610   %cast1 = bitcast i32* %gep1 to i8*
611   %cmp = icmp ult i8* %cast1, %gep2
612   ret i1 %cmp
613 ; CHECK: @test60
614 ; CHECK-NEXT: %gep1.idx = shl nuw i64 %i, 2
615 ; CHECK-NEXT: icmp slt i64 %gep1.idx, %j
616 ; CHECK-NEXT: ret i1
617 }
618
619 define i1 @test61(i8* %foo, i64 %i, i64 %j) {
620   %bit = bitcast i8* %foo to i32*
621   %gep1 = getelementptr i32* %bit, i64 %i
622   %gep2 = getelementptr  i8* %foo, i64 %j
623   %cast1 = bitcast i32* %gep1 to i8*
624   %cmp = icmp ult i8* %cast1, %gep2
625   ret i1 %cmp
626 ; Don't transform non-inbounds GEPs.
627 ; CHECK: @test61
628 ; CHECK: icmp ult i8* %cast1, %gep2
629 ; CHECK-NEXT: ret i1
630 }
631
632 define i1 @test62(i8* %a) {
633   %arrayidx1 = getelementptr inbounds i8* %a, i64 1
634   %arrayidx2 = getelementptr inbounds i8* %a, i64 10
635   %cmp = icmp slt i8* %arrayidx1, %arrayidx2
636   ret i1 %cmp
637 ; CHECK: @test62
638 ; CHECK-NEXT: ret i1 true
639 }
640
641 define i1 @test63(i8 %a, i32 %b) nounwind {
642   %z = zext i8 %a to i32
643   %t = and i32 %b, 255
644   %c = icmp eq i32 %z, %t
645   ret i1 %c
646 ; CHECK: @test63
647 ; CHECK-NEXT: %1 = trunc i32 %b to i8
648 ; CHECK-NEXT: %c = icmp eq i8 %1, %a
649 ; CHECK-NEXT: ret i1 %c
650 }
651
652 define i1 @test64(i8 %a, i32 %b) nounwind {
653   %t = and i32 %b, 255
654   %z = zext i8 %a to i32
655   %c = icmp eq i32 %t, %z
656   ret i1 %c
657 ; CHECK: @test64
658 ; CHECK-NEXT: %1 = trunc i32 %b to i8
659 ; CHECK-NEXT: %c = icmp eq i8 %1, %a
660 ; CHECK-NEXT: ret i1 %c
661 }
662
663 define i1 @test65(i64 %A, i64 %B) {
664   %s1 = add i64 %A, %B
665   %s2 = add i64 %A, %B
666   %cmp = icmp eq i64 %s1, %s2
667 ; CHECK: @test65
668 ; CHECK-NEXT: ret i1 true
669   ret i1 %cmp
670 }
671
672 define i1 @test66(i64 %A, i64 %B) {
673   %s1 = add i64 %A, %B
674   %s2 = add i64 %B, %A
675   %cmp = icmp eq i64 %s1, %s2
676 ; CHECK: @test66
677 ; CHECK-NEXT: ret i1 true
678   ret i1 %cmp
679 }
680
681 ; CHECK: @test67
682 ; CHECK: %and = and i32 %x, 96
683 ; CHECK: %cmp = icmp ne i32 %and, 0
684 define i1 @test67(i32 %x) nounwind uwtable {
685   %and = and i32 %x, 127
686   %cmp = icmp sgt i32 %and, 31
687   ret i1 %cmp
688 }
689
690 ; CHECK: @test68
691 ; CHECK: %cmp = icmp ugt i32 %and, 30
692 define i1 @test68(i32 %x) nounwind uwtable {
693   %and = and i32 %x, 127
694   %cmp = icmp sgt i32 %and, 30
695   ret i1 %cmp
696 }
697
698 ; PR14708
699 ; CHECK: @test69
700 ; CHECK: %1 = and i32 %c, -33
701 ; CHECK: %2 = icmp eq i32 %1, 65
702 ; CHECK: ret i1 %2
703 define i1 @test69(i32 %c) nounwind uwtable {
704   %1 = icmp eq i32 %c, 97
705   %2 = icmp eq i32 %c, 65
706   %3 = or i1 %1, %2
707   ret i1 %3
708 }
709
710 ; CHECK: @icmp_sext16trunc
711 ; CHECK-NEXT: %1 = trunc i32 %x to i16
712 ; CHECK-NEXT: %cmp = icmp slt i16 %1, 36
713 define i1 @icmp_sext16trunc(i32 %x) {
714   %trunc = trunc i32 %x to i16
715   %sext = sext i16 %trunc to i32
716   %cmp = icmp slt i32 %sext, 36
717   ret i1 %cmp
718 }
719
720 ; CHECK: @icmp_sext8trunc
721 ; CHECK-NEXT: %1 = trunc i32 %x to i8
722 ; CHECK-NEXT: %cmp = icmp slt i8 %1, 36
723 define i1 @icmp_sext8trunc(i32 %x) {
724   %trunc = trunc i32 %x to i8
725   %sext = sext i8 %trunc to i32
726   %cmp = icmp slt i32 %sext, 36
727   ret i1 %cmp
728 }
729
730 ; CHECK: @icmp_shl16
731 ; CHECK-NEXT: %1 = trunc i32 %x to i16
732 ; CHECK-NEXT: %cmp = icmp slt i16 %1, 36
733 define i1 @icmp_shl16(i32 %x) {
734   %shl = shl i32 %x, 16
735   %cmp = icmp slt i32 %shl, 2359296
736   ret i1 %cmp
737 }
738
739 ; CHECK: @icmp_shl24
740 ; CHECK-NEXT: %1 = trunc i32 %x to i8
741 ; CHECK-NEXT: %cmp = icmp slt i8 %1, 36
742 define i1 @icmp_shl24(i32 %x) {
743   %shl = shl i32 %x, 24
744   %cmp = icmp slt i32 %shl, 603979776
745   ret i1 %cmp
746 }
747
748 ; If the (shl x, C) preserved the sign and this is a sign test,
749 ; compare the LHS operand instead
750 ; CHECK: @icmp_shl_nsw_sgt
751 ; CHECK-NEXT: icmp sgt i32 %x, 0
752 define i1 @icmp_shl_nsw_sgt(i32 %x) {
753   %shl = shl nsw i32 %x, 21
754   %cmp = icmp sgt i32 %shl, 0
755   ret i1 %cmp
756 }
757
758 ; CHECK: @icmp_shl_nsw_sge0
759 ; CHECK-NEXT: icmp sgt i32 %x, -1
760 define i1 @icmp_shl_nsw_sge0(i32 %x) {
761   %shl = shl nsw i32 %x, 21
762   %cmp = icmp sge i32 %shl, 0
763   ret i1 %cmp
764 }
765
766 ; CHECK: @icmp_shl_nsw_sge1
767 ; CHECK-NEXT: icmp sgt i32 %x, 0
768 define i1 @icmp_shl_nsw_sge1(i32 %x) {
769   %shl = shl nsw i32 %x, 21
770   %cmp = icmp sge i32 %shl, 1
771   ret i1 %cmp
772 }
773
774 ; Checks for icmp (eq|ne) (shl x, C), 0
775 ; CHECK: @icmp_shl_nsw_eq
776 ; CHECK-NEXT: icmp eq i32 %x, 0
777 define i1 @icmp_shl_nsw_eq(i32 %x) {
778   %mul = shl nsw i32 %x, 5
779   %cmp = icmp eq i32 %mul, 0
780   ret i1 %cmp
781 }
782
783 ; CHECK: @icmp_shl_eq
784 ; CHECK-NOT: icmp eq i32 %mul, 0
785 define i1 @icmp_shl_eq(i32 %x) {
786   %mul = shl i32 %x, 5
787   %cmp = icmp eq i32 %mul, 0
788   ret i1 %cmp
789 }
790
791 ; CHECK: @icmp_shl_nsw_ne
792 ; CHECK-NEXT: icmp ne i32 %x, 0
793 define i1 @icmp_shl_nsw_ne(i32 %x) {
794   %mul = shl nsw i32 %x, 7
795   %cmp = icmp ne i32 %mul, 0
796   ret i1 %cmp
797 }
798
799 ; CHECK: @icmp_shl_ne
800 ; CHECK-NOT: icmp ne i32 %x, 0
801 define i1 @icmp_shl_ne(i32 %x) {
802   %mul = shl i32 %x, 7
803   %cmp = icmp ne i32 %mul, 0
804   ret i1 %cmp
805 }
806
807 ; If the (mul x, C) preserved the sign and this is sign test,
808 ; compare the LHS operand instead
809 ; CHECK: @icmp_mul_nsw
810 ; CHECK-NEXT: icmp sgt i32 %x, 0
811 define i1 @icmp_mul_nsw(i32 %x) {
812   %mul = mul nsw i32 %x, 12
813   %cmp = icmp sgt i32 %mul, 0
814   ret i1 %cmp
815 }
816
817 ; CHECK: @icmp_mul_nsw1
818 ; CHECK-NEXT: icmp slt i32 %x, 0
819 define i1 @icmp_mul_nsw1(i32 %x) {
820   %mul = mul nsw i32 %x, 12
821   %cmp = icmp sle i32 %mul, -1
822   ret i1 %cmp
823 }
824
825 ; CHECK: @icmp_mul_nsw_neg
826 ; CHECK-NEXT: icmp slt i32 %x, 1
827 define i1 @icmp_mul_nsw_neg(i32 %x) {
828   %mul = mul nsw i32 %x, -12
829   %cmp = icmp sge i32 %mul, 0
830   ret i1 %cmp
831 }
832
833 ; CHECK: @icmp_mul_nsw_neg1
834 ; CHECK-NEXT: icmp slt i32 %x, 0
835 define i1 @icmp_mul_nsw_neg1(i32 %x) {
836   %mul = mul nsw i32 %x, -12
837   %cmp = icmp sge i32 %mul, 1
838   ret i1 %cmp
839 }
840
841 ; CHECK: @icmp_mul_nsw_0
842 ; CHECK-NOT: icmp sgt i32 %x, 0
843 define i1 @icmp_mul_nsw_0(i32 %x) {
844   %mul = mul nsw i32 %x, 0
845   %cmp = icmp sgt i32 %mul, 0
846   ret i1 %cmp
847 }
848
849 ; CHECK: @icmp_mul
850 ; CHECK-NEXT: %mul = mul i32 %x, -12
851 define i1 @icmp_mul(i32 %x) {
852   %mul = mul i32 %x, -12
853   %cmp = icmp sge i32 %mul, 0
854   ret i1 %cmp
855 }
856
857 ; Checks for icmp (eq|ne) (mul x, C), 0
858 ; CHECK: @icmp_mul_neq0
859 ; CHECK-NEXT: icmp ne i32 %x, 0
860 define i1 @icmp_mul_neq0(i32 %x) {
861   %mul = mul nsw i32 %x, -12
862   %cmp = icmp ne i32 %mul, 0
863   ret i1 %cmp
864 }
865
866 ; CHECK: @icmp_mul_eq0
867 ; CHECK-NEXT: icmp eq i32 %x, 0
868 define i1 @icmp_mul_eq0(i32 %x) {
869   %mul = mul nsw i32 %x, 12
870   %cmp = icmp eq i32 %mul, 0
871   ret i1 %cmp
872 }
873
874 ; CHECK: @icmp_mul0_eq0
875 ; CHECK-NEXT: ret i1 true
876 define i1 @icmp_mul0_eq0(i32 %x) {
877   %mul = mul i32 %x, 0
878   %cmp = icmp eq i32 %mul, 0
879   ret i1 %cmp
880 }
881
882 ; CHECK: @icmp_mul0_ne0
883 ; CHECK-NEXT: ret i1 false
884 define i1 @icmp_mul0_ne0(i32 %x) {
885   %mul = mul i32 %x, 0
886   %cmp = icmp ne i32 %mul, 0
887   ret i1 %cmp
888 }
889
890 ; CHECK: @icmp_sub1_sge
891 ; CHECK-NEXT: icmp sgt i32 %x, %y
892 define i1 @icmp_sub1_sge(i32 %x, i32 %y) {
893   %sub = add nsw i32 %x, -1
894   %cmp = icmp sge i32 %sub, %y
895   ret i1 %cmp
896 }
897
898 ; CHECK: @icmp_add1_sgt
899 ; CHECK-NEXT: icmp sge i32 %x, %y
900 define i1 @icmp_add1_sgt(i32 %x, i32 %y) {
901   %add = add nsw i32 %x, 1
902   %cmp = icmp sgt i32 %add, %y
903   ret i1 %cmp
904 }
905
906 ; CHECK: @icmp_sub1_slt
907 ; CHECK-NEXT: icmp sle i32 %x, %y
908 define i1 @icmp_sub1_slt(i32 %x, i32 %y) {
909   %sub = add nsw i32 %x, -1
910   %cmp = icmp slt i32 %sub, %y
911   ret i1 %cmp
912 }
913
914 ; CHECK: @icmp_add1_sle
915 ; CHECK-NEXT: icmp slt i32 %x, %y
916 define i1 @icmp_add1_sle(i32 %x, i32 %y) {
917   %add = add nsw i32 %x, 1
918   %cmp = icmp sle i32 %add, %y
919   ret i1 %cmp
920 }
921
922 ; CHECK: @icmp_add20_sge_add57
923 ; CHECK-NEXT: [[ADD:%[a-z0-9]+]] = add nsw i32 %y, 37
924 ; CHECK-NEXT: icmp sle i32 [[ADD]], %x
925 define i1 @icmp_add20_sge_add57(i32 %x, i32 %y) {
926   %1 = add nsw i32 %x, 20
927   %2 = add nsw i32 %y, 57
928   %cmp = icmp sge i32 %1, %2
929   ret i1 %cmp
930 }
931
932 ; CHECK: @icmp_sub57_sge_sub20
933 ; CHECK-NEXT: [[SUB:%[a-z0-9]+]] = add nsw i32 %x, -37
934 ; CHECK-NEXT: icmp sge i32 [[SUB]], %y
935 define i1 @icmp_sub57_sge_sub20(i32 %x, i32 %y) {
936   %1 = add nsw i32 %x, -57
937   %2 = add nsw i32 %y, -20
938   %cmp = icmp sge i32 %1, %2
939   ret i1 %cmp
940 }