Add some transforms of the kind X-Y>X -> 0>Y which are valid when there is no
[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: %B = select i1
125 ; CHECK-NEXT: ret i1 %B
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 }