OSDN Git Service

9447f0671e45fb4ab804cdd89a18a9495dc875ab
[android-x86/external-llvm.git] / test / Transforms / InstSimplify / AndOrXor.ll
1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instsimplify -S | FileCheck %s
3
4 define i8 @and0(i8 %x) {
5 ; CHECK-LABEL: @and0(
6 ; CHECK-NEXT:    ret i8 0
7 ;
8   %r = and i8 %x, 0
9   ret i8 %r
10 }
11
12 define <2 x i8> @and0_vec_undef_elt(<2 x i8> %x) {
13 ; CHECK-LABEL: @and0_vec_undef_elt(
14 ; CHECK-NEXT:    ret <2 x i8> zeroinitializer
15 ;
16   %r = and <2 x i8> %x, <i8 undef, i8 0>
17   ret <2 x i8> %r
18 }
19
20 ; add nsw (xor X, signbit), signbit --> X
21
22 define <2 x i32> @add_nsw_signbit(<2 x i32> %x) {
23 ; CHECK-LABEL: @add_nsw_signbit(
24 ; CHECK-NEXT:    ret <2 x i32> [[X:%.*]]
25 ;
26   %y = xor <2 x i32> %x, <i32 -2147483648, i32 -2147483648>
27   %z = add nsw <2 x i32> %y, <i32 -2147483648, i32 -2147483648>
28   ret <2 x i32> %z
29 }
30
31 ; Undef elements in either constant vector are ok.
32
33 define <2 x i32> @add_nsw_signbit_undef(<2 x i32> %x) {
34 ; CHECK-LABEL: @add_nsw_signbit_undef(
35 ; CHECK-NEXT:    ret <2 x i32> [[X:%.*]]
36 ;
37   %y = xor <2 x i32> %x, <i32 undef, i32 -2147483648>
38   %z = add nsw <2 x i32> %y, <i32 -2147483648, i32 undef>
39   ret <2 x i32> %z
40 }
41
42 ; add nuw (xor X, signbit), signbit --> X
43
44 define <2 x i5> @add_nuw_signbit(<2 x i5> %x) {
45 ; CHECK-LABEL: @add_nuw_signbit(
46 ; CHECK-NEXT:    ret <2 x i5> [[X:%.*]]
47 ;
48   %y = xor <2 x i5> %x, <i5 -16, i5 -16>
49   %z = add nuw <2 x i5> %y, <i5 -16, i5 -16>
50   ret <2 x i5> %z
51 }
52
53 ; Undef elements in either constant vector are ok.
54
55 define <2 x i5> @add_nuw_signbit_undef(<2 x i5> %x) {
56 ; CHECK-LABEL: @add_nuw_signbit_undef(
57 ; CHECK-NEXT:    ret <2 x i5> [[X:%.*]]
58 ;
59   %y = xor <2 x i5> %x, <i5 -16, i5 undef>
60   %z = add nuw <2 x i5> %y, <i5 undef, i5 -16>
61   ret <2 x i5> %z
62 }
63
64 define i64 @pow2(i32 %x) {
65 ; CHECK-LABEL: @pow2(
66 ; CHECK-NEXT:    [[NEGX:%.*]] = sub i32 0, [[X:%.*]]
67 ; CHECK-NEXT:    [[X2:%.*]] = and i32 [[X]], [[NEGX]]
68 ; CHECK-NEXT:    [[E:%.*]] = zext i32 [[X2]] to i64
69 ; CHECK-NEXT:    ret i64 [[E]]
70 ;
71   %negx = sub i32 0, %x
72   %x2 = and i32 %x, %negx
73   %e = zext i32 %x2 to i64
74   %nege = sub i64 0, %e
75   %e2 = and i64 %e, %nege
76   ret i64 %e2
77 }
78
79 define i64 @pow2b(i32 %x) {
80 ; CHECK-LABEL: @pow2b(
81 ; CHECK-NEXT:    [[SH:%.*]] = shl i32 2, [[X:%.*]]
82 ; CHECK-NEXT:    [[E:%.*]] = zext i32 [[SH]] to i64
83 ; CHECK-NEXT:    ret i64 [[E]]
84 ;
85   %sh = shl i32 2, %x
86   %e = zext i32 %sh to i64
87   %nege = sub i64 0, %e
88   %e2 = and i64 %e, %nege
89   ret i64 %e2
90 }
91
92 define i1 @and_of_icmps0(i32 %b) {
93 ; CHECK-LABEL: @and_of_icmps0(
94 ; CHECK-NEXT:    ret i1 false
95 ;
96   %1 = add i32 %b, 2
97   %2 = icmp ult i32 %1, 4
98   %cmp3 = icmp sgt i32 %b, 2
99   %cmp = and i1 %2, %cmp3
100   ret i1 %cmp
101 }
102
103 define <2 x i1> @and_of_icmps0_vec(<2 x i32> %b) {
104 ; CHECK-LABEL: @and_of_icmps0_vec(
105 ; CHECK-NEXT:    ret <2 x i1> zeroinitializer
106 ;
107   %1 = add <2 x i32> %b, <i32 2, i32 2>
108   %2 = icmp ult <2 x i32> %1, <i32 4, i32 4>
109   %cmp3 = icmp sgt <2 x i32> %b, <i32 2, i32 2>
110   %cmp = and <2 x i1> %2, %cmp3
111   ret <2 x i1> %cmp
112 }
113
114 define i1 @and_of_icmps1(i32 %b) {
115 ; CHECK-LABEL: @and_of_icmps1(
116 ; CHECK-NEXT:    ret i1 false
117 ;
118   %1 = add nsw i32 %b, 2
119   %2 = icmp slt i32 %1, 4
120   %cmp3 = icmp sgt i32 %b, 2
121   %cmp = and i1 %2, %cmp3
122   ret i1 %cmp
123 }
124
125 define <2 x i1> @and_of_icmps1_vec(<2 x i32> %b) {
126 ; CHECK-LABEL: @and_of_icmps1_vec(
127 ; CHECK-NEXT:    ret <2 x i1> zeroinitializer
128 ;
129   %1 = add nsw <2 x i32> %b, <i32 2, i32 2>
130   %2 = icmp slt <2 x i32> %1, <i32 4, i32 4>
131   %cmp3 = icmp sgt <2 x i32> %b, <i32 2, i32 2>
132   %cmp = and <2 x i1> %2, %cmp3
133   ret <2 x i1> %cmp
134 }
135
136 define i1 @and_of_icmps2(i32 %b) {
137 ; CHECK-LABEL: @and_of_icmps2(
138 ; CHECK-NEXT:    ret i1 false
139 ;
140   %1 = add i32 %b, 2
141   %2 = icmp ule i32 %1, 3
142   %cmp3 = icmp sgt i32 %b, 2
143   %cmp = and i1 %2, %cmp3
144   ret i1 %cmp
145 }
146
147 define <2 x i1> @and_of_icmps2_vec(<2 x i32> %b) {
148 ; CHECK-LABEL: @and_of_icmps2_vec(
149 ; CHECK-NEXT:    ret <2 x i1> zeroinitializer
150 ;
151   %1 = add <2 x i32> %b, <i32 2, i32 2>
152   %2 = icmp ule <2 x i32> %1, <i32 3, i32 3>
153   %cmp3 = icmp sgt <2 x i32> %b, <i32 2, i32 2>
154   %cmp = and <2 x i1> %2, %cmp3
155   ret <2 x i1> %cmp
156 }
157
158 define i1 @and_of_icmps3(i32 %b) {
159 ; CHECK-LABEL: @and_of_icmps3(
160 ; CHECK-NEXT:    ret i1 false
161 ;
162   %1 = add nsw i32 %b, 2
163   %2 = icmp sle i32 %1, 3
164   %cmp3 = icmp sgt i32 %b, 2
165   %cmp = and i1 %2, %cmp3
166   ret i1 %cmp
167 }
168
169 define <2 x i1> @and_of_icmps3_vec(<2 x i32> %b) {
170 ; CHECK-LABEL: @and_of_icmps3_vec(
171 ; CHECK-NEXT:    ret <2 x i1> zeroinitializer
172 ;
173   %1 = add nsw <2 x i32> %b, <i32 2, i32 2>
174   %2 = icmp sle <2 x i32> %1, <i32 3, i32 3>
175   %cmp3 = icmp sgt <2 x i32> %b, <i32 2, i32 2>
176   %cmp = and <2 x i1> %2, %cmp3
177   ret <2 x i1> %cmp
178 }
179
180 define i1 @and_of_icmps4(i32 %b) {
181 ; CHECK-LABEL: @and_of_icmps4(
182 ; CHECK-NEXT:    ret i1 false
183 ;
184   %1 = add nuw i32 %b, 2
185   %2 = icmp ult i32 %1, 4
186   %cmp3 = icmp ugt i32 %b, 2
187   %cmp = and i1 %2, %cmp3
188   ret i1 %cmp
189 }
190
191 define <2 x i1> @and_of_icmps4_vec(<2 x i32> %b) {
192 ; CHECK-LABEL: @and_of_icmps4_vec(
193 ; CHECK-NEXT:    ret <2 x i1> zeroinitializer
194 ;
195   %1 = add nuw <2 x i32> %b, <i32 2, i32 2>
196   %2 = icmp ult <2 x i32> %1, <i32 4, i32 4>
197   %cmp3 = icmp ugt <2 x i32> %b, <i32 2, i32 2>
198   %cmp = and <2 x i1> %2, %cmp3
199   ret <2 x i1> %cmp
200 }
201
202 define i1 @and_of_icmps5(i32 %b) {
203 ; CHECK-LABEL: @and_of_icmps5(
204 ; CHECK-NEXT:    ret i1 false
205 ;
206   %1 = add nuw i32 %b, 2
207   %2 = icmp ule i32 %1, 3
208   %cmp3 = icmp ugt i32 %b, 2
209   %cmp = and i1 %2, %cmp3
210   ret i1 %cmp
211 }
212
213 define <2 x i1> @and_of_icmps5_vec(<2 x i32> %b) {
214 ; CHECK-LABEL: @and_of_icmps5_vec(
215 ; CHECK-NEXT:    ret <2 x i1> zeroinitializer
216 ;
217   %1 = add nuw <2 x i32> %b, <i32 2, i32 2>
218   %2 = icmp ule <2 x i32> %1, <i32 3, i32 3>
219   %cmp3 = icmp ugt <2 x i32> %b, <i32 2, i32 2>
220   %cmp = and <2 x i1> %2, %cmp3
221   ret <2 x i1> %cmp
222 }
223
224 define i1 @or_of_icmps0(i32 %b) {
225 ; CHECK-LABEL: @or_of_icmps0(
226 ; CHECK-NEXT:    ret i1 true
227 ;
228   %1 = add i32 %b, 2
229   %2 = icmp uge i32 %1, 4
230   %cmp3 = icmp sle i32 %b, 2
231   %cmp = or i1 %2, %cmp3
232   ret i1 %cmp
233 }
234
235 define <2 x i1> @or_of_icmps0_vec(<2 x i32> %b) {
236 ; CHECK-LABEL: @or_of_icmps0_vec(
237 ; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
238 ;
239   %1 = add <2 x i32> %b, <i32 2, i32 2>
240   %2 = icmp uge <2 x i32> %1, <i32 4, i32 4>
241   %cmp3 = icmp sle <2 x i32> %b, <i32 2, i32 2>
242   %cmp = or <2 x i1> %2, %cmp3
243   ret <2 x i1> %cmp
244 }
245
246 define i1 @or_of_icmps1(i32 %b) {
247 ; CHECK-LABEL: @or_of_icmps1(
248 ; CHECK-NEXT:    ret i1 true
249 ;
250   %1 = add nsw i32 %b, 2
251   %2 = icmp sge i32 %1, 4
252   %cmp3 = icmp sle i32 %b, 2
253   %cmp = or i1 %2, %cmp3
254   ret i1 %cmp
255 }
256
257 define <2 x i1> @or_of_icmps1_vec(<2 x i32> %b) {
258 ; CHECK-LABEL: @or_of_icmps1_vec(
259 ; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
260 ;
261   %1 = add nsw <2 x i32> %b, <i32 2, i32 2>
262   %2 = icmp sge <2 x i32> %1, <i32 4, i32 4>
263   %cmp3 = icmp sle <2 x i32> %b, <i32 2, i32 2>
264   %cmp = or <2 x i1> %2, %cmp3
265   ret <2 x i1> %cmp
266 }
267
268 define i1 @or_of_icmps2(i32 %b) {
269 ; CHECK-LABEL: @or_of_icmps2(
270 ; CHECK-NEXT:    ret i1 true
271 ;
272   %1 = add i32 %b, 2
273   %2 = icmp ugt i32 %1, 3
274   %cmp3 = icmp sle i32 %b, 2
275   %cmp = or i1 %2, %cmp3
276   ret i1 %cmp
277 }
278
279 define <2 x i1> @or_of_icmps2_vec(<2 x i32> %b) {
280 ; CHECK-LABEL: @or_of_icmps2_vec(
281 ; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
282 ;
283   %1 = add <2 x i32> %b, <i32 2, i32 2>
284   %2 = icmp ugt <2 x i32> %1, <i32 3, i32 3>
285   %cmp3 = icmp sle <2 x i32> %b, <i32 2, i32 2>
286   %cmp = or <2 x i1> %2, %cmp3
287   ret <2 x i1> %cmp
288 }
289
290 define i1 @or_of_icmps3(i32 %b) {
291 ; CHECK-LABEL: @or_of_icmps3(
292 ; CHECK-NEXT:    ret i1 true
293 ;
294   %1 = add nsw i32 %b, 2
295   %2 = icmp sgt i32 %1, 3
296   %cmp3 = icmp sle i32 %b, 2
297   %cmp = or i1 %2, %cmp3
298   ret i1 %cmp
299 }
300
301 define <2 x i1> @or_of_icmps3_vec(<2 x i32> %b) {
302 ; CHECK-LABEL: @or_of_icmps3_vec(
303 ; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
304 ;
305   %1 = add nsw <2 x i32> %b, <i32 2, i32 2>
306   %2 = icmp sgt <2 x i32> %1, <i32 3, i32 3>
307   %cmp3 = icmp sle <2 x i32> %b, <i32 2, i32 2>
308   %cmp = or <2 x i1> %2, %cmp3
309   ret <2 x i1> %cmp
310 }
311
312 define i1 @or_of_icmps4(i32 %b) {
313 ; CHECK-LABEL: @or_of_icmps4(
314 ; CHECK-NEXT:    ret i1 true
315 ;
316   %1 = add nuw i32 %b, 2
317   %2 = icmp uge i32 %1, 4
318   %cmp3 = icmp ule i32 %b, 2
319   %cmp = or i1 %2, %cmp3
320   ret i1 %cmp
321 }
322
323 define <2 x i1> @or_of_icmps4_vec(<2 x i32> %b) {
324 ; CHECK-LABEL: @or_of_icmps4_vec(
325 ; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
326 ;
327   %1 = add nuw <2 x i32> %b, <i32 2, i32 2>
328   %2 = icmp uge <2 x i32> %1, <i32 4, i32 4>
329   %cmp3 = icmp ule <2 x i32> %b, <i32 2, i32 2>
330   %cmp = or <2 x i1> %2, %cmp3
331   ret <2 x i1> %cmp
332 }
333
334 define i1 @or_of_icmps5(i32 %b) {
335 ; CHECK-LABEL: @or_of_icmps5(
336 ; CHECK-NEXT:    ret i1 true
337 ;
338   %1 = add nuw i32 %b, 2
339   %2 = icmp ugt i32 %1, 3
340   %cmp3 = icmp ule i32 %b, 2
341   %cmp = or i1 %2, %cmp3
342   ret i1 %cmp
343 }
344
345 define <2 x i1> @or_of_icmps5_vec(<2 x i32> %b) {
346 ; CHECK-LABEL: @or_of_icmps5_vec(
347 ; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
348 ;
349   %1 = add nuw <2 x i32> %b, <i32 2, i32 2>
350   %2 = icmp ugt <2 x i32> %1, <i32 3, i32 3>
351   %cmp3 = icmp ule <2 x i32> %b, <i32 2, i32 2>
352   %cmp = or <2 x i1> %2, %cmp3
353   ret <2 x i1> %cmp
354 }
355
356 define i32 @neg_nuw(i32 %x) {
357 ; CHECK-LABEL: @neg_nuw(
358 ; CHECK-NEXT:    ret i32 0
359 ;
360   %neg = sub nuw i32 0, %x
361   ret i32 %neg
362 }
363
364 define i1 @and_icmp1(i32 %x, i32 %y) {
365 ; CHECK-LABEL: @and_icmp1(
366 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ult i32 [[X:%.*]], [[Y:%.*]]
367 ; CHECK-NEXT:    ret i1 [[TMP1]]
368 ;
369   %1 = icmp ult i32 %x, %y
370   %2 = icmp ne i32 %y, 0
371   %3 = and i1 %1, %2
372   ret i1 %3
373 }
374
375 define i1 @and_icmp2(i32 %x, i32 %y) {
376 ; CHECK-LABEL: @and_icmp2(
377 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], [[Y:%.*]]
378 ; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i32 [[X]], 0
379 ; CHECK-NEXT:    [[TMP3:%.*]] = and i1 [[TMP1]], [[TMP2]]
380 ; CHECK-NEXT:    ret i1 [[TMP3]]
381 ;
382   %1 = icmp ugt i32 %x, %y
383   %2 = icmp ne i32 %x, 0
384   %3 = and i1 %1, %2
385   ret i1 %3
386 }
387
388 define i1 @and_icmp3(i32 %x, i32 %y) {
389 ; CHECK-LABEL: @and_icmp3(
390 ; CHECK-NEXT:    ret i1 false
391 ;
392   %1 = icmp ult i32 %x, %y
393   %2 = icmp eq i32 %y, 0
394   %3 = and i1 %1, %2
395   ret i1 %3
396 }
397
398 define i1 @and_icmp4(i32 %x, i32 %y) {
399 ; CHECK-LABEL: @and_icmp4(
400 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], [[Y:%.*]]
401 ; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[X]], 0
402 ; CHECK-NEXT:    [[TMP3:%.*]] = and i1 [[TMP1]], [[TMP2]]
403 ; CHECK-NEXT:    ret i1 [[TMP3]]
404 ;
405   %1 = icmp ugt i32 %x, %y
406   %2 = icmp eq i32 %x, 0
407   %3 = and i1 %1, %2
408   ret i1 %3
409 }
410
411 define i1 @or_icmp1(i32 %x, i32 %y) {
412 ; CHECK-LABEL: @or_icmp1(
413 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i32 [[Y:%.*]], 0
414 ; CHECK-NEXT:    ret i1 [[TMP1]]
415 ;
416   %1 = icmp ult i32 %x, %y
417   %2 = icmp ne i32 %y, 0
418   %3 = or i1 %1, %2
419   ret i1 %3
420 }
421
422 define i1 @or_icmp2(i32 %x, i32 %y) {
423 ; CHECK-LABEL: @or_icmp2(
424 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], [[Y:%.*]]
425 ; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i32 [[X]], 0
426 ; CHECK-NEXT:    [[TMP3:%.*]] = or i1 [[TMP1]], [[TMP2]]
427 ; CHECK-NEXT:    ret i1 [[TMP3]]
428 ;
429   %1 = icmp ugt i32 %x, %y
430   %2 = icmp ne i32 %x, 0
431   %3 = or i1 %1, %2
432   ret i1 %3
433 }
434
435 define i1 @or_icmp3(i32 %x, i32 %y) {
436 ; CHECK-LABEL: @or_icmp3(
437 ; CHECK-NEXT:    ret i1 true
438 ;
439   %1 = icmp uge i32 %x, %y
440   %2 = icmp ne i32 %y, 0
441   %3 = or i1 %1, %2
442   ret i1 %3
443 }
444
445 define i1 @or_icmp4(i32 %x, i32 %y) {
446 ; CHECK-LABEL: @or_icmp4(
447 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule i32 [[X:%.*]], [[Y:%.*]]
448 ; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i32 [[X]], 0
449 ; CHECK-NEXT:    [[TMP3:%.*]] = or i1 [[TMP1]], [[TMP2]]
450 ; CHECK-NEXT:    ret i1 [[TMP3]]
451 ;
452   %1 = icmp ule i32 %x, %y
453   %2 = icmp ne i32 %x, 0
454   %3 = or i1 %1, %2
455   ret i1 %3
456 }
457
458 define i1 @or_icmp5(i32 %x, i32 %y) {
459 ; CHECK-LABEL: @or_icmp5(
460 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp uge i32 [[X:%.*]], [[Y:%.*]]
461 ; CHECK-NEXT:    ret i1 [[TMP1]]
462 ;
463   %1 = icmp uge i32 %x, %y
464   %2 = icmp eq i32 %y, 0
465   %3 = or i1 %1, %2
466   ret i1 %3
467 }
468
469 define i1 @or_icmp6(i32 %x, i32 %y) {
470 ; CHECK-LABEL: @or_icmp6(
471 ; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule i32 [[X:%.*]], [[Y:%.*]]
472 ; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[X]], 0
473 ; CHECK-NEXT:    [[TMP3:%.*]] = or i1 [[TMP1]], [[TMP2]]
474 ; CHECK-NEXT:    ret i1 [[TMP3]]
475 ;
476   %1 = icmp ule i32 %x, %y
477   %2 = icmp eq i32 %x, 0
478   %3 = or i1 %1, %2
479   ret i1 %3
480 }
481
482 ; PR27869 - Look through casts to eliminate cmps and bitwise logic.
483
484 define i32 @and_of_zexted_icmps(i32 %i) {
485 ; CHECK-LABEL: @and_of_zexted_icmps(
486 ; CHECK-NEXT:    ret i32 0
487 ;
488   %cmp0 = icmp eq i32 %i, 0
489   %conv0 = zext i1 %cmp0 to i32
490   %cmp1 = icmp ugt i32 %i, 4
491   %conv1 = zext i1 %cmp1 to i32
492   %and = and i32 %conv0, %conv1
493   ret i32 %and
494 }
495
496 ; Make sure vectors work too.
497
498 define <4 x i32> @and_of_zexted_icmps_vec(<4 x i32> %i) {
499 ; CHECK-LABEL: @and_of_zexted_icmps_vec(
500 ; CHECK-NEXT:    ret <4 x i32> zeroinitializer
501 ;
502   %cmp0 = icmp eq <4 x i32> %i, zeroinitializer
503   %conv0 = zext <4 x i1> %cmp0 to <4 x i32>
504   %cmp1 = icmp slt <4 x i32> %i, zeroinitializer
505   %conv1 = zext <4 x i1> %cmp1 to <4 x i32>
506   %and = and <4 x i32> %conv0, %conv1
507   ret <4 x i32> %and
508 }
509
510 ; Try a different cast and weird types.
511
512 define i5 @and_of_sexted_icmps(i3 %i) {
513 ; CHECK-LABEL: @and_of_sexted_icmps(
514 ; CHECK-NEXT:    ret i5 0
515 ;
516   %cmp0 = icmp eq i3 %i, 0
517   %conv0 = sext i1 %cmp0 to i5
518   %cmp1 = icmp ugt i3 %i, 1
519   %conv1 = sext i1 %cmp1 to i5
520   %and = and i5 %conv0, %conv1
521   ret i5 %and
522 }
523
524 ; Try a different cast and weird vector types.
525
526 define i3 @and_of_bitcast_icmps_vec(<3 x i65> %i) {
527 ; CHECK-LABEL: @and_of_bitcast_icmps_vec(
528 ; CHECK-NEXT:    ret i3 0
529 ;
530   %cmp0 = icmp sgt <3 x i65> %i, zeroinitializer
531   %conv0 = bitcast <3 x i1> %cmp0 to i3
532   %cmp1 = icmp slt <3 x i65> %i, zeroinitializer
533   %conv1 = bitcast <3 x i1> %cmp1 to i3
534   %and = and i3 %conv0, %conv1
535   ret i3 %and
536 }
537
538 ; We can't do this if the casts are different.
539
540 define i16 @and_of_different_cast_icmps(i8 %i) {
541 ; CHECK-LABEL: @and_of_different_cast_icmps(
542 ; CHECK-NEXT:    [[CMP0:%.*]] = icmp eq i8 [[I:%.*]], 0
543 ; CHECK-NEXT:    [[CONV0:%.*]] = zext i1 [[CMP0]] to i16
544 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq i8 [[I]], 1
545 ; CHECK-NEXT:    [[CONV1:%.*]] = sext i1 [[CMP1]] to i16
546 ; CHECK-NEXT:    [[AND:%.*]] = and i16 [[CONV0]], [[CONV1]]
547 ; CHECK-NEXT:    ret i16 [[AND]]
548 ;
549   %cmp0 = icmp eq i8 %i, 0
550   %conv0 = zext i1 %cmp0 to i16
551   %cmp1 = icmp eq i8 %i, 1
552   %conv1 = sext i1 %cmp1 to i16
553   %and = and i16 %conv0, %conv1
554   ret i16 %and
555 }
556
557 define <2 x i3> @and_of_different_cast_icmps_vec(<2 x i8> %i, <2 x i16> %j) {
558 ; CHECK-LABEL: @and_of_different_cast_icmps_vec(
559 ; CHECK-NEXT:    [[CMP0:%.*]] = icmp eq <2 x i8> [[I:%.*]], zeroinitializer
560 ; CHECK-NEXT:    [[CONV0:%.*]] = zext <2 x i1> [[CMP0]] to <2 x i3>
561 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp ugt <2 x i16> [[J:%.*]], <i16 1, i16 1>
562 ; CHECK-NEXT:    [[CONV1:%.*]] = zext <2 x i1> [[CMP1]] to <2 x i3>
563 ; CHECK-NEXT:    [[AND:%.*]] = and <2 x i3> [[CONV0]], [[CONV1]]
564 ; CHECK-NEXT:    ret <2 x i3> [[AND]]
565 ;
566   %cmp0 = icmp eq <2 x i8> %i, zeroinitializer
567   %conv0 = zext <2 x i1> %cmp0 to <2 x i3>
568   %cmp1 = icmp ugt <2 x i16> %j, <i16 1, i16 1>
569   %conv1 = zext <2 x i1> %cmp1 to <2 x i3>
570   %and = and <2 x i3> %conv0, %conv1
571   ret <2 x i3> %and
572 }
573
574 define i32 @or_of_zexted_icmps(i32 %i) {
575 ; CHECK-LABEL: @or_of_zexted_icmps(
576 ; CHECK-NEXT:    ret i32 1
577 ;
578   %cmp0 = icmp ne i32 %i, 0
579   %conv0 = zext i1 %cmp0 to i32
580   %cmp1 = icmp uge i32 4, %i
581   %conv1 = zext i1 %cmp1 to i32
582   %or = or i32 %conv0, %conv1
583   ret i32 %or
584 }
585
586 ; Try a different cast and weird vector types.
587
588 define i3 @or_of_bitcast_icmps_vec(<3 x i65> %i) {
589 ; CHECK-LABEL: @or_of_bitcast_icmps_vec(
590 ; CHECK-NEXT:    ret i3 bitcast (<3 x i1> <i1 true, i1 true, i1 true> to i3)
591 ;
592   %cmp0 = icmp sge <3 x i65> %i, zeroinitializer
593   %conv0 = bitcast <3 x i1> %cmp0 to i3
594   %cmp1 = icmp slt <3 x i65> %i, zeroinitializer
595   %conv1 = bitcast <3 x i1> %cmp1 to i3
596   %or = or i3 %conv0, %conv1
597   ret i3 %or
598 }
599
600 ; We can't simplify if the casts are different.
601
602 define i16 @or_of_different_cast_icmps(i8 %i) {
603 ; CHECK-LABEL: @or_of_different_cast_icmps(
604 ; CHECK-NEXT:    [[CMP0:%.*]] = icmp ne i8 [[I:%.*]], 0
605 ; CHECK-NEXT:    [[CONV0:%.*]] = zext i1 [[CMP0]] to i16
606 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp ne i8 [[I]], 1
607 ; CHECK-NEXT:    [[CONV1:%.*]] = sext i1 [[CMP1]] to i16
608 ; CHECK-NEXT:    [[OR:%.*]] = or i16 [[CONV0]], [[CONV1]]
609 ; CHECK-NEXT:    ret i16 [[OR]]
610 ;
611   %cmp0 = icmp ne i8 %i, 0
612   %conv0 = zext i1 %cmp0 to i16
613   %cmp1 = icmp ne i8 %i, 1
614   %conv1 = sext i1 %cmp1 to i16
615   %or = or i16 %conv0, %conv1
616   ret i16 %or
617 }
618
619 ; (A & ~B) | (A ^ B) -> A ^ B
620
621 define i32 @test43(i32 %a, i32 %b) {
622 ; CHECK-LABEL: @test43(
623 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
624 ; CHECK-NEXT:    ret i32 [[XOR]]
625 ;
626   %neg = xor i32 %b, -1
627   %and = and i32 %a, %neg
628   %xor = xor i32 %a, %b
629   %or = or i32 %and, %xor
630   ret i32 %or
631 }
632
633 define i32 @test43_commuted_and(i32 %a, i32 %b) {
634 ; CHECK-LABEL: @test43_commuted_and(
635 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
636 ; CHECK-NEXT:    ret i32 [[XOR]]
637 ;
638   %neg = xor i32 %b, -1
639   %and = and i32 %neg, %a
640   %xor = xor i32 %a, %b
641   %or = or i32 %and, %xor
642   ret i32 %or
643 }
644
645 ; Commute operands of the 'or'.
646 ; (A ^ B) | (A & ~B) -> A ^ B
647
648 define i32 @test44(i32 %a, i32 %b) {
649 ; CHECK-LABEL: @test44(
650 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
651 ; CHECK-NEXT:    ret i32 [[XOR]]
652 ;
653   %xor = xor i32 %a, %b
654   %neg = xor i32 %b, -1
655   %and = and i32 %a, %neg
656   %or = or i32 %xor, %and
657   ret i32 %or
658 }
659
660 define i32 @test44_commuted_and(i32 %a, i32 %b) {
661 ; CHECK-LABEL: @test44_commuted_and(
662 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
663 ; CHECK-NEXT:    ret i32 [[XOR]]
664 ;
665   %xor = xor i32 %a, %b
666   %neg = xor i32 %b, -1
667   %and = and i32 %neg, %a
668   %or = or i32 %xor, %and
669   ret i32 %or
670 }
671
672 ; (~A & ~B) | (~A ^ B) -> ~A ^ B
673
674 define i32 @test45(i32 %a, i32 %b) {
675 ; CHECK-LABEL: @test45(
676 ; CHECK-NEXT:    [[NEGB:%.*]] = xor i32 [[B:%.*]], -1
677 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], [[NEGB]]
678 ; CHECK-NEXT:    ret i32 [[XOR]]
679 ;
680   %nega = xor i32 %a, -1
681   %negb = xor i32 %b, -1
682   %and = and i32 %nega, %negb
683   %xor = xor i32 %a, %negb
684   %or = or i32 %and, %xor
685   ret i32 %or
686 }
687
688 define i32 @test45_commuted_and(i32 %a, i32 %b) {
689 ; CHECK-LABEL: @test45_commuted_and(
690 ; CHECK-NEXT:    [[NEGB:%.*]] = xor i32 [[B:%.*]], -1
691 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], [[NEGB]]
692 ; CHECK-NEXT:    ret i32 [[XOR]]
693 ;
694   %nega = xor i32 %a, -1
695   %negb = xor i32 %b, -1
696   %and = and i32 %negb, %nega
697   %xor = xor i32 %a, %negb
698   %or = or i32 %and, %xor
699   ret i32 %or
700 }
701
702 ; Commute operands of the 'or'.
703 ; (~A ^ B) | (~A & ~B) -> ~A ^ B
704
705 define i32 @test46(i32 %a, i32 %b) {
706 ; CHECK-LABEL: @test46(
707 ; CHECK-NEXT:    [[NEGB:%.*]] = xor i32 [[B:%.*]], -1
708 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], [[NEGB]]
709 ; CHECK-NEXT:    ret i32 [[XOR]]
710 ;
711   %nega = xor i32 %a, -1
712   %negb = xor i32 %b, -1
713   %and = and i32 %nega, %negb
714   %xor = xor i32 %a, %negb
715   %or = or i32 %xor, %and
716   ret i32 %or
717 }
718
719 ; (~A & ~B) | (~A ^ B) -> ~A ^ B
720
721 define i32 @test46_commuted_and(i32 %a, i32 %b) {
722 ; CHECK-LABEL: @test46_commuted_and(
723 ; CHECK-NEXT:    [[NEGB:%.*]] = xor i32 [[B:%.*]], -1
724 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], [[NEGB]]
725 ; CHECK-NEXT:    ret i32 [[XOR]]
726 ;
727   %nega = xor i32 %a, -1
728   %negb = xor i32 %b, -1
729   %and = and i32 %negb, %nega
730   %xor = xor i32 %a, %negb
731   %or = or i32 %xor, %and
732   ret i32 %or
733 }
734
735 ; (~A ^ B) | (A & B) -> ~A ^ B
736
737 define i32 @test47(i32 %a, i32 %b) {
738 ; CHECK-LABEL: @test47(
739 ; CHECK-NEXT:    [[NEGA:%.*]] = xor i32 [[A:%.*]], -1
740 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[NEGA]], [[B:%.*]]
741 ; CHECK-NEXT:    ret i32 [[XOR]]
742 ;
743   %nega = xor i32 %a, -1
744   %and = and i32 %a, %b
745   %xor = xor i32 %nega, %b
746   %or = or i32 %xor, %and
747   ret i32 %or
748 }
749
750 define i32 @test48(i32 %a, i32 %b) {
751 ; CHECK-LABEL: @test48(
752 ; CHECK-NEXT:    [[NEGA:%.*]] = xor i32 [[A:%.*]], -1
753 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[B:%.*]], [[NEGA]]
754 ; CHECK-NEXT:    ret i32 [[XOR]]
755 ;
756   %nega = xor i32 %a, -1
757   %and = and i32 %a, %b
758   %xor = xor i32 %b, %nega
759   %or = or i32 %xor, %and
760   ret i32 %or
761 }
762
763 define i32 @test49(i32 %a, i32 %b) {
764 ; CHECK-LABEL: @test49(
765 ; CHECK-NEXT:    [[NEGA:%.*]] = xor i32 [[A:%.*]], -1
766 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[B:%.*]], [[NEGA]]
767 ; CHECK-NEXT:    ret i32 [[XOR]]
768 ;
769   %nega = xor i32 %a, -1
770   %and = and i32 %b, %a
771   %xor = xor i32 %b, %nega
772   %or = or i32 %xor, %and
773   ret i32 %or
774 }
775
776 define i32 @test50(i32 %a, i32 %b) {
777 ; CHECK-LABEL: @test50(
778 ; CHECK-NEXT:    [[NEGA:%.*]] = xor i32 [[A:%.*]], -1
779 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[NEGA]], [[B:%.*]]
780 ; CHECK-NEXT:    ret i32 [[XOR]]
781 ;
782   %nega = xor i32 %a, -1
783   %and = and i32 %b, %a
784   %xor = xor i32 %nega, %b
785   %or = or i32 %xor, %and
786   ret i32 %or
787 }
788
789 define i32 @test51(i32 %a, i32 %b) {
790 ; CHECK-LABEL: @test51(
791 ; CHECK-NEXT:    [[NEGA:%.*]] = xor i32 [[A:%.*]], -1
792 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[NEGA]], [[B:%.*]]
793 ; CHECK-NEXT:    ret i32 [[XOR]]
794 ;
795   %nega = xor i32 %a, -1
796   %and = and i32 %a, %b
797   %xor = xor i32 %nega, %b
798   %or = or i32 %and, %xor
799   ret i32 %or
800 }
801
802 define i32 @test52(i32 %a, i32 %b) {
803 ; CHECK-LABEL: @test52(
804 ; CHECK-NEXT:    [[NEGA:%.*]] = xor i32 [[A:%.*]], -1
805 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[B:%.*]], [[NEGA]]
806 ; CHECK-NEXT:    ret i32 [[XOR]]
807 ;
808   %nega = xor i32 %a, -1
809   %and = and i32 %a, %b
810   %xor = xor i32 %b, %nega
811   %or = or i32 %and, %xor
812   ret i32 %or
813 }
814
815 define i32 @test53(i32 %a, i32 %b) {
816 ; CHECK-LABEL: @test53(
817 ; CHECK-NEXT:    [[NEGA:%.*]] = xor i32 [[A:%.*]], -1
818 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[B:%.*]], [[NEGA]]
819 ; CHECK-NEXT:    ret i32 [[XOR]]
820 ;
821   %nega = xor i32 %a, -1
822   %and = and i32 %b, %a
823   %xor = xor i32 %b, %nega
824   %or = or i32 %and, %xor
825   ret i32 %or
826 }
827
828 define i32 @test54(i32 %a, i32 %b) {
829 ; CHECK-LABEL: @test54(
830 ; CHECK-NEXT:    [[NEGA:%.*]] = xor i32 [[A:%.*]], -1
831 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[NEGA]], [[B:%.*]]
832 ; CHECK-NEXT:    ret i32 [[XOR]]
833 ;
834   %nega = xor i32 %a, -1
835   %and = and i32 %b, %a
836   %xor = xor i32 %nega, %b
837   %or = or i32 %and, %xor
838   ret i32 %or
839 }
840
841 ; (A & B) | ~(A ^ B) -> ~(A ^ B)
842
843 define i32 @test55(i32 %a, i32 %b) {
844 ; CHECK-LABEL: @test55(
845 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[A:%.*]], [[B:%.*]]
846 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A]], [[B]]
847 ; CHECK-NEXT:    [[XNOR:%.*]] = xor i32 [[XOR]], -1
848 ; CHECK-NEXT:    [[OR:%.*]] = or i32 [[AND]], [[XNOR]]
849 ; CHECK-NEXT:    ret i32 [[OR]]
850 ;
851   %and = and i32 %a, %b
852   %xor = xor i32 %a, %b
853   %xnor = xor i32 %xor, -1
854   %or = or i32 %and, %xnor
855   ret i32 %or
856 }
857
858 ; ~(A ^ B) | (A & B) -> ~(A ^ B)
859
860 define i32 @test56(i32 %a, i32 %b) {
861 ; CHECK-LABEL: @test56(
862 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[A:%.*]], [[B:%.*]]
863 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A]], [[B]]
864 ; CHECK-NEXT:    [[XNOR:%.*]] = xor i32 [[XOR]], -1
865 ; CHECK-NEXT:    [[OR:%.*]] = or i32 [[XNOR]], [[AND]]
866 ; CHECK-NEXT:    ret i32 [[OR]]
867 ;
868   %and = and i32 %a, %b
869   %xor = xor i32 %a, %b
870   %xnor = xor i32 %xor, -1
871   %or = or i32 %xnor, %and
872   ret i32 %or
873 }
874
875 ; (B & A) | ~(A ^ B) -> ~(A ^ B)
876
877 define i32 @test57(i32 %a, i32 %b) {
878 ; CHECK-LABEL: @test57(
879 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
880 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A]], [[B]]
881 ; CHECK-NEXT:    [[XNOR:%.*]] = xor i32 [[XOR]], -1
882 ; CHECK-NEXT:    [[OR:%.*]] = or i32 [[AND]], [[XNOR]]
883 ; CHECK-NEXT:    ret i32 [[OR]]
884 ;
885   %and = and i32 %b, %a
886   %xor = xor i32 %a, %b
887   %xnor = xor i32 %xor, -1
888   %or = or i32 %and, %xnor
889   ret i32 %or
890 }
891
892 ; ~(A ^ B) | (A & B) -> ~(A ^ B)
893
894 define i32 @test58(i32 %a, i32 %b) {
895 ; CHECK-LABEL: @test58(
896 ; CHECK-NEXT:    [[AND:%.*]] = and i32 [[B:%.*]], [[A:%.*]]
897 ; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A]], [[B]]
898 ; CHECK-NEXT:    [[XNOR:%.*]] = xor i32 [[XOR]], -1
899 ; CHECK-NEXT:    [[OR:%.*]] = or i32 [[XNOR]], [[AND]]
900 ; CHECK-NEXT:    ret i32 [[OR]]
901 ;
902   %and = and i32 %b, %a
903   %xor = xor i32 %a, %b
904   %xnor = xor i32 %xor, -1
905   %or = or i32 %xnor, %and
906   ret i32 %or
907 }
908
909 define i8 @lshr_perfect_mask(i8 %x) {
910 ; CHECK-LABEL: @lshr_perfect_mask(
911 ; CHECK-NEXT:    [[SH:%.*]] = lshr i8 [[X:%.*]], 5
912 ; CHECK-NEXT:    ret i8 [[SH]]
913 ;
914   %sh = lshr i8 %x, 5
915   %mask = and i8 %sh, 7  ; 0x07
916   ret i8 %mask
917 }
918
919 define <2 x i8> @lshr_oversized_mask_splat(<2 x i8> %x) {
920 ; CHECK-LABEL: @lshr_oversized_mask_splat(
921 ; CHECK-NEXT:    [[SH:%.*]] = lshr <2 x i8> [[X:%.*]], <i8 5, i8 5>
922 ; CHECK-NEXT:    ret <2 x i8> [[SH]]
923 ;
924   %sh = lshr <2 x i8> %x, <i8 5, i8 5>
925   %mask = and <2 x i8> %sh, <i8 135, i8 135>  ; 0x87
926   ret <2 x i8> %mask
927 }
928
929 define i8 @lshr_undersized_mask(i8 %x) {
930 ; CHECK-LABEL: @lshr_undersized_mask(
931 ; CHECK-NEXT:    [[SH:%.*]] = lshr i8 [[X:%.*]], 5
932 ; CHECK-NEXT:    [[MASK:%.*]] = and i8 [[SH]], -2
933 ; CHECK-NEXT:    ret i8 [[MASK]]
934 ;
935   %sh = lshr i8 %x, 5
936   %mask = and i8 %sh, -2  ; 0xFE
937   ret i8 %mask
938 }
939
940 define <2 x i8> @shl_perfect_mask_splat(<2 x i8> %x) {
941 ; CHECK-LABEL: @shl_perfect_mask_splat(
942 ; CHECK-NEXT:    [[SH:%.*]] = shl <2 x i8> [[X:%.*]], <i8 6, i8 6>
943 ; CHECK-NEXT:    ret <2 x i8> [[SH]]
944 ;
945   %sh = shl <2 x i8> %x, <i8 6, i8 6>
946   %mask = and <2 x i8> %sh, <i8 192, i8 192>  ; 0xC0
947   ret <2 x i8> %mask
948 }
949
950 define i8 @shl_oversized_mask(i8 %x) {
951 ; CHECK-LABEL: @shl_oversized_mask(
952 ; CHECK-NEXT:    [[SH:%.*]] = shl i8 [[X:%.*]], 6
953 ; CHECK-NEXT:    ret i8 [[SH]]
954 ;
955   %sh = shl i8 %x, 6
956   %mask = and i8 %sh, 195  ; 0xC3
957   ret i8 %mask
958 }
959
960 define <2 x i8> @shl_undersized_mask_splat(<2 x i8> %x) {
961 ; CHECK-LABEL: @shl_undersized_mask_splat(
962 ; CHECK-NEXT:    [[SH:%.*]] = shl <2 x i8> [[X:%.*]], <i8 6, i8 6>
963 ; CHECK-NEXT:    [[MASK:%.*]] = and <2 x i8> [[SH]], <i8 -120, i8 -120>
964 ; CHECK-NEXT:    ret <2 x i8> [[MASK]]
965 ;
966   %sh = shl <2 x i8> %x, <i8 6, i8 6>
967   %mask = and <2 x i8> %sh, <i8 136, i8 136>  ; 0x88
968   ret <2 x i8> %mask
969 }
970
971 define i32 @reversed_not(i32 %a) {
972 ; CHECK-LABEL: @reversed_not(
973 ; CHECK-NEXT:    ret i32 -1
974 ;
975   %nega = xor i32 -1, %a
976   %or = or i32 %a, %nega
977   ret i32 %or
978 }