1 ; RUN: opt < %s -instcombine -S | FileCheck %s
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"
6 define i32 @test1(i32 %X) {
8 icmp slt i32 %X, 0 ; <i1>:0 [#uses=1]
9 zext i1 %0 to i32 ; <i32>:1 [#uses=1]
12 ; CHECK: lshr i32 %X, 31
16 define i32 @test2(i32 %X) {
18 icmp ult i32 %X, -2147483648 ; <i1>:0 [#uses=1]
19 zext i1 %0 to i32 ; <i32>:1 [#uses=1]
22 ; CHECK: lshr i32 %X, 31
27 define i32 @test3(i32 %X) {
29 icmp slt i32 %X, 0 ; <i1>:0 [#uses=1]
30 sext i1 %0 to i32 ; <i32>:1 [#uses=1]
33 ; CHECK: ashr i32 %X, 31
37 define i32 @test4(i32 %X) {
39 icmp ult i32 %X, -2147483648 ; <i1>:0 [#uses=1]
40 sext i1 %0 to i32 ; <i32>:1 [#uses=1]
43 ; CHECK: ashr i32 %X, 31
49 define <2 x i1> @test5(<2 x i64> %x) {
51 %V = icmp eq <2 x i64> %x, undef
54 ; CHECK: ret <2 x i1> <i1 true, i1 true>
57 define i32 @test6(i32 %a, i32 %b) {
58 %c = icmp sle i32 %a, -1
59 %d = zext i1 %c to i32
64 ; CHECK-NEXT: ashr i32 %a, 31
65 ; CHECK-NEXT: %f = and i32 %e, %b
66 ; CHECK-NEXT: ret i32 %f
70 define i1 @test7(i32 %x) {
73 %b = icmp ult i32 %a, %x
76 ; CHECK: %b = icmp ne i32 %x, 0
80 define i1 @test8(i32 %x){
83 %b = icmp eq i32 %a, %x
89 define i1 @test9(i32 %x) {
92 %b = icmp ugt i32 %x, %a
95 ; CHECK: icmp ugt i32 %x, 1
99 define i1 @test10(i32 %x){
102 %b = icmp slt i32 %a, %x
106 ; CHECK: %b = icmp ne i32 %x, -2147483648
110 define i1 @test11(i32 %x) {
111 %a = add nsw i32 %x, 8
112 %b = icmp slt i32 %x, %a
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
124 ; CHECK-NEXT: %B = select i1
125 ; CHECK-NEXT: ret i1 %B
129 define i1 @test13(i8 %X) nounwind readnone {
131 %cmp = icmp slt i8 undef, %X
134 ; CHECK: ret i1 false
137 define i1 @test14(i8 %X) nounwind readnone {
139 %cmp = icmp slt i8 undef, -128
142 ; CHECK: ret i1 false
145 define i1 @test15() nounwind readnone {
147 %cmp = icmp eq i8 undef, -128
150 ; CHECK: ret i1 undef
153 define i1 @test16() nounwind readnone {
155 %cmp = icmp ne i8 undef, -128
158 ; CHECK: ret i1 undef
161 define i1 @test17(i32 %x) nounwind {
163 %and = and i32 %shl, 8
164 %cmp = icmp eq i32 %and, 0
167 ; CHECK-NEXT: %cmp = icmp ne i32 %x, 3
171 define i1 @test18(i32 %x) nounwind {
173 %and = and i32 %sh, 1
174 %cmp = icmp eq i32 %and, 0
177 ; CHECK-NEXT: %cmp = icmp ne i32 %x, 3
180 define i1 @test19(i32 %x) nounwind {
182 %and = and i32 %shl, 8
183 %cmp = icmp eq i32 %and, 8
186 ; CHECK-NEXT: %cmp = icmp eq i32 %x, 3
189 define i1 @test20(i32 %x) nounwind {
191 %and = and i32 %shl, 8
192 %cmp = icmp ne i32 %and, 0
195 ; CHECK-NEXT: %cmp = icmp eq i32 %x, 3
198 define i1 @test21(i8 %x, i8 %y) {
203 %B = icmp ugt i8 %A, 3
207 define i1 @test22(i8 %x, i8 %y) {
212 %B = icmp ult i8 %A, 4
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
225 @X = global [1000 x i32] zeroinitializer
229 ; CHECK: %cmp = icmp eq i64 %i, 1000
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)
238 ; X + Z > Y + Z -> X > Y if there is no overflow.
239 ; CHECK: %c = icmp sgt i32 %x, %y
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
249 ; X + Z > Y + Z -> X > Y if there is no overflow.
250 ; CHECK: %c = icmp ugt i32 %x, %y
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
260 ; X - Z > Y - Z -> X > Y if there is no overflow.
261 ; CHECK: %c = icmp sgt i32 %x, %y
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
271 ; X - Z > Y - Z -> X > Y if there is no overflow.
272 ; CHECK: %c = icmp ugt i32 %x, %y
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
282 ; X + Y > X -> Y > 0 if there is no overflow.
283 ; CHECK: %c = icmp sgt i32 %y, 0
285 define i1 @test29(i32 %x, i32 %y) {
286 %lhs = add nsw i32 %x, %y
287 %c = icmp sgt i32 %lhs, %x
292 ; X + Y > X -> Y > 0 if there is no overflow.
293 ; CHECK: %c = icmp ne i32 %y, 0
295 define i1 @test30(i32 %x, i32 %y) {
296 %lhs = add nuw i32 %x, %y
297 %c = icmp ugt i32 %lhs, %x
302 ; X > X + Y -> 0 > Y if there is no overflow.
303 ; CHECK: %c = icmp slt i32 %y, 0
305 define i1 @test31(i32 %x, i32 %y) {
306 %rhs = add nsw i32 %x, %y
307 %c = icmp sgt i32 %x, %rhs
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
321 ; X - Y > X -> 0 > Y if there is no overflow.
322 ; CHECK: %c = icmp slt i32 %y, 0
324 define i1 @test33(i32 %x, i32 %y) {
325 %lhs = sub nsw i32 %x, %y
326 %c = icmp sgt i32 %lhs, %x
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
340 ; X > X - Y -> Y > 0 if there is no overflow.
341 ; CHECK: %c = icmp sgt i32 %y, 0
343 define i1 @test35(i32 %x, i32 %y) {
344 %rhs = sub nsw i32 %x, %y
345 %c = icmp sgt i32 %x, %rhs
350 ; X > X - Y -> Y > 0 if there is no overflow.
351 ; CHECK: %c = icmp ne i32 %y, 0
353 define i1 @test36(i32 %x, i32 %y) {
354 %rhs = sub nuw i32 %x, %y
355 %c = icmp ugt i32 %x, %rhs
360 ; X - Y > X - Z -> Z > Y if there is no overflow.
361 ; CHECK: %c = icmp sgt i32 %z, %y
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
371 ; X - Y > X - Z -> Z > Y if there is no overflow.
372 ; CHECK: %c = icmp ugt i32 %z, %y
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
383 ; CHECK: ret i1 false
384 define i1 @test39(i31 %X, i32 %Y) {
385 %A = zext i31 %X to i32
387 %C = icmp slt i32 %B, 0
393 ; CHECK %B = icmp eq i32 %X, 0
394 define i1 @test40(i32 %X, i32 %Y) {
395 %A = ashr exact i32 %X, %Y
396 %B = icmp eq i32 %A, 0
401 ; CHECK %B = icmp ne i32 %X, 0
402 define i1 @test41(i32 %X, i32 %Y) {
403 %A = lshr exact i32 %X, %Y
404 %B = icmp ne i32 %A, 0