1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3
4declare void @use8(i8)
5declare void @use32(i32)
6
7; There should be no 'and' instructions left in any test.
8
9define i32 @test1(i32 %A) {
10; CHECK-LABEL: @test1(
11; CHECK-NEXT:    ret i32 0
12;
13  %B = and i32 %A, 0
14  ret i32 %B
15}
16
17define i32 @test2(i32 %A) {
18; CHECK-LABEL: @test2(
19; CHECK-NEXT:    ret i32 [[A:%.*]]
20;
21  %B = and i32 %A, -1
22  ret i32 %B
23}
24
25define i1 @test3(i1 %A) {
26; CHECK-LABEL: @test3(
27; CHECK-NEXT:    ret i1 false
28;
29  %B = and i1 %A, false
30  ret i1 %B
31}
32
33define i1 @test3_logical(i1 %A) {
34; CHECK-LABEL: @test3_logical(
35; CHECK-NEXT:    ret i1 false
36;
37  %B = select i1 %A, i1 false, i1 false
38  ret i1 %B
39}
40
41define i1 @test4(i1 %A) {
42; CHECK-LABEL: @test4(
43; CHECK-NEXT:    ret i1 [[A:%.*]]
44;
45  %B = and i1 %A, true
46  ret i1 %B
47}
48
49define i1 @test4_logical(i1 %A) {
50; CHECK-LABEL: @test4_logical(
51; CHECK-NEXT:    ret i1 [[A:%.*]]
52;
53  %B = select i1 %A, i1 true, i1 false
54  ret i1 %B
55}
56
57define i32 @test5(i32 %A) {
58; CHECK-LABEL: @test5(
59; CHECK-NEXT:    ret i32 [[A:%.*]]
60;
61  %B = and i32 %A, %A
62  ret i32 %B
63}
64
65define i1 @test6(i1 %A) {
66; CHECK-LABEL: @test6(
67; CHECK-NEXT:    ret i1 [[A:%.*]]
68;
69  %B = and i1 %A, %A
70  ret i1 %B
71}
72
73define i1 @test6_logical(i1 %A) {
74; CHECK-LABEL: @test6_logical(
75; CHECK-NEXT:    ret i1 [[A:%.*]]
76;
77  %B = select i1 %A, i1 %A, i1 false
78  ret i1 %B
79}
80
81; A & ~A == 0
82define i32 @test7(i32 %A) {
83; CHECK-LABEL: @test7(
84; CHECK-NEXT:    ret i32 0
85;
86  %NotA = xor i32 %A, -1
87  %B = and i32 %A, %NotA
88  ret i32 %B
89}
90
91; AND associates
92define i8 @test8(i8 %A) {
93; CHECK-LABEL: @test8(
94; CHECK-NEXT:    ret i8 0
95;
96  %B = and i8 %A, 3
97  %C = and i8 %B, 4
98  ret i8 %C
99}
100
101; Test of sign bit, convert to setle %A, 0
102define i1 @test9(i32 %A) {
103; CHECK-LABEL: @test9(
104; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[A:%.*]], 0
105; CHECK-NEXT:    ret i1 [[C]]
106;
107  %B = and i32 %A, -2147483648
108  %C = icmp ne i32 %B, 0
109  ret i1 %C
110}
111
112; Test of sign bit, convert to setle %A, 0
113define i1 @test9a(i32 %A) {
114; CHECK-LABEL: @test9a(
115; CHECK-NEXT:    [[C:%.*]] = icmp slt i32 [[A:%.*]], 0
116; CHECK-NEXT:    ret i1 [[C]]
117;
118  %B = and i32 %A, -2147483648
119  %C = icmp ne i32 %B, 0
120  ret i1 %C
121}
122
123define i32 @test10(i32 %A) {
124; CHECK-LABEL: @test10(
125; CHECK-NEXT:    ret i32 1
126;
127  %B = and i32 %A, 12
128  %C = xor i32 %B, 15
129  ; (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
130  %D = and i32 %C, 1
131  ret i32 %D
132}
133
134define i32 @test11(i32 %A, i32* %P) {
135; CHECK-LABEL: @test11(
136; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[A:%.*]], -4
137; CHECK-NEXT:    [[C:%.*]] = xor i32 [[TMP1]], 15
138; CHECK-NEXT:    store i32 [[C]], i32* [[P:%.*]], align 4
139; CHECK-NEXT:    ret i32 3
140;
141  %B = or i32 %A, 3
142  %C = xor i32 %B, 12
143  ; additional use of C
144  store i32 %C, i32* %P
145  ; %C = and uint %B, 3 --> 3
146  %D = and i32 %C, 3
147  ret i32 %D
148}
149
150define i1 @test12(i32 %A, i32 %B) {
151; CHECK-LABEL: @test12(
152; CHECK-NEXT:    [[C1:%.*]] = icmp ult i32 [[A:%.*]], [[B:%.*]]
153; CHECK-NEXT:    ret i1 [[C1]]
154;
155  %C1 = icmp ult i32 %A, %B
156  %C2 = icmp ule i32 %A, %B
157  ; (A < B) & (A <= B) === (A < B)
158  %D = and i1 %C1, %C2
159  ret i1 %D
160}
161
162define i1 @test12_logical(i32 %A, i32 %B) {
163; CHECK-LABEL: @test12_logical(
164; CHECK-NEXT:    [[C1:%.*]] = icmp ult i32 [[A:%.*]], [[B:%.*]]
165; CHECK-NEXT:    ret i1 [[C1]]
166;
167  %C1 = icmp ult i32 %A, %B
168  %C2 = icmp ule i32 %A, %B
169  ; (A < B) & (A <= B) === (A < B)
170  %D = select i1 %C1, i1 %C2, i1 false
171  ret i1 %D
172}
173
174define i1 @test13(i32 %A, i32 %B) {
175; CHECK-LABEL: @test13(
176; CHECK-NEXT:    ret i1 false
177;
178  %C1 = icmp ult i32 %A, %B
179  %C2 = icmp ugt i32 %A, %B
180  ; (A < B) & (A > B) === false
181  %D = and i1 %C1, %C2
182  ret i1 %D
183}
184
185define i1 @test13_logical(i32 %A, i32 %B) {
186; CHECK-LABEL: @test13_logical(
187; CHECK-NEXT:    ret i1 false
188;
189  %C1 = icmp ult i32 %A, %B
190  %C2 = icmp ugt i32 %A, %B
191  ; (A < B) & (A > B) === false
192  %D = select i1 %C1, i1 %C2, i1 false
193  ret i1 %D
194}
195
196define i1 @test14(i8 %A) {
197; CHECK-LABEL: @test14(
198; CHECK-NEXT:    [[C:%.*]] = icmp slt i8 [[A:%.*]], 0
199; CHECK-NEXT:    ret i1 [[C]]
200;
201  %B = and i8 %A, -128
202  %C = icmp ne i8 %B, 0
203  ret i1 %C
204}
205
206define i8 @test15(i8 %A) {
207; CHECK-LABEL: @test15(
208; CHECK-NEXT:    ret i8 0
209;
210  %B = lshr i8 %A, 7
211  ; Always equals zero
212  %C = and i8 %B, 2
213  ret i8 %C
214}
215
216define i8 @test16(i8 %A) {
217; CHECK-LABEL: @test16(
218; CHECK-NEXT:    ret i8 0
219;
220  %B = shl i8 %A, 2
221  %C = and i8 %B, 3
222  ret i8 %C
223}
224
225define i1 @test18(i32 %A) {
226; CHECK-LABEL: @test18(
227; CHECK-NEXT:    [[C:%.*]] = icmp ugt i32 [[A:%.*]], 127
228; CHECK-NEXT:    ret i1 [[C]]
229;
230  %B = and i32 %A, -128
231  ;; C >= 128
232  %C = icmp ne i32 %B, 0
233  ret i1 %C
234}
235
236define <2 x i1> @test18_vec(<2 x i32> %A) {
237; CHECK-LABEL: @test18_vec(
238; CHECK-NEXT:    [[C:%.*]] = icmp ugt <2 x i32> [[A:%.*]], <i32 127, i32 127>
239; CHECK-NEXT:    ret <2 x i1> [[C]]
240;
241  %B = and <2 x i32> %A, <i32 -128, i32 -128>
242  %C = icmp ne <2 x i32> %B, zeroinitializer
243  ret <2 x i1> %C
244}
245
246define i1 @test18a(i8 %A) {
247; CHECK-LABEL: @test18a(
248; CHECK-NEXT:    [[C:%.*]] = icmp ult i8 [[A:%.*]], 2
249; CHECK-NEXT:    ret i1 [[C]]
250;
251  %B = and i8 %A, -2
252  %C = icmp eq i8 %B, 0
253  ret i1 %C
254}
255
256define <2 x i1> @test18a_vec(<2 x i8> %A) {
257; CHECK-LABEL: @test18a_vec(
258; CHECK-NEXT:    [[C:%.*]] = icmp ult <2 x i8> [[A:%.*]], <i8 2, i8 2>
259; CHECK-NEXT:    ret <2 x i1> [[C]]
260;
261  %B = and <2 x i8> %A, <i8 -2, i8 -2>
262  %C = icmp eq <2 x i8> %B, zeroinitializer
263  ret <2 x i1> %C
264}
265
266define i32 @test19(i32 %A) {
267; CHECK-LABEL: @test19(
268; CHECK-NEXT:    [[B:%.*]] = shl i32 [[A:%.*]], 3
269; CHECK-NEXT:    ret i32 [[B]]
270;
271  %B = shl i32 %A, 3
272  ;; Clearing a zero bit
273  %C = and i32 %B, -2
274  ret i32 %C
275}
276
277define i8 @test20(i8 %A) {
278; CHECK-LABEL: @test20(
279; CHECK-NEXT:    [[C:%.*]] = lshr i8 [[A:%.*]], 7
280; CHECK-NEXT:    ret i8 [[C]]
281;
282  %C = lshr i8 %A, 7
283  ;; Unneeded
284  %D = and i8 %C, 1
285  ret i8 %D
286}
287
288define i1 @test23(i32 %A) {
289; CHECK-LABEL: @test23(
290; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[A:%.*]], 2
291; CHECK-NEXT:    ret i1 [[TMP1]]
292;
293  %B = icmp sgt i32 %A, 1
294  %C = icmp sle i32 %A, 2
295  %D = and i1 %B, %C
296  ret i1 %D
297}
298
299define i1 @test23_logical(i32 %A) {
300; CHECK-LABEL: @test23_logical(
301; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32 [[A:%.*]], 2
302; CHECK-NEXT:    ret i1 [[TMP1]]
303;
304  %B = icmp sgt i32 %A, 1
305  %C = icmp sle i32 %A, 2
306  %D = select i1 %B, i1 %C, i1 false
307  ret i1 %D
308}
309
310define <2 x i1> @test23vec(<2 x i32> %A) {
311; CHECK-LABEL: @test23vec(
312; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq <2 x i32> [[A:%.*]], <i32 2, i32 2>
313; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
314;
315  %B = icmp sgt <2 x i32> %A, <i32 1, i32 1>
316  %C = icmp sle <2 x i32> %A, <i32 2, i32 2>
317  %D = and <2 x i1> %B, %C
318  ret <2 x i1> %D
319}
320
321define i1 @test24(i32 %A) {
322; CHECK-LABEL: @test24(
323; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[A:%.*]], 2
324; CHECK-NEXT:    ret i1 [[TMP1]]
325;
326  %B = icmp sgt i32 %A, 1
327  %C = icmp ne i32 %A, 2
328  ;; A > 2
329  %D = and i1 %B, %C
330  ret i1 %D
331}
332
333define i1 @test24_logical(i32 %A) {
334; CHECK-LABEL: @test24_logical(
335; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[A:%.*]], 2
336; CHECK-NEXT:    ret i1 [[TMP1]]
337;
338  %B = icmp sgt i32 %A, 1
339  %C = icmp ne i32 %A, 2
340  ;; A > 2
341  %D = select i1 %B, i1 %C, i1 false
342  ret i1 %D
343}
344
345define i1 @test25(i32 %A) {
346; CHECK-LABEL: @test25(
347; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[A:%.*]], -50
348; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 50
349; CHECK-NEXT:    ret i1 [[TMP2]]
350;
351  %B = icmp sge i32 %A, 50
352  %C = icmp slt i32 %A, 100
353  %D = and i1 %B, %C
354  ret i1 %D
355}
356
357define i1 @test25_logical(i32 %A) {
358; CHECK-LABEL: @test25_logical(
359; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[A:%.*]], -50
360; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 50
361; CHECK-NEXT:    ret i1 [[TMP2]]
362;
363  %B = icmp sge i32 %A, 50
364  %C = icmp slt i32 %A, 100
365  %D = select i1 %B, i1 %C, i1 false
366  ret i1 %D
367}
368
369define <2 x i1> @test25vec(<2 x i32> %A) {
370; CHECK-LABEL: @test25vec(
371; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i32> [[A:%.*]], <i32 -50, i32 -50>
372; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult <2 x i32> [[TMP1]], <i32 50, i32 50>
373; CHECK-NEXT:    ret <2 x i1> [[TMP2]]
374;
375  %B = icmp sge <2 x i32> %A, <i32 50, i32 50>
376  %C = icmp slt <2 x i32> %A, <i32 100, i32 100>
377  %D = and <2 x i1> %B, %C
378  ret <2 x i1> %D
379}
380
381define i8 @test27(i8 %A) {
382; CHECK-LABEL: @test27(
383; CHECK-NEXT:    ret i8 0
384;
385  %B = and i8 %A, 4
386  %C = sub i8 %B, 16
387  ;; 0xF0
388  %D = and i8 %C, -16
389  %E = add i8 %D, 16
390  ret i8 %E
391}
392
393;; No demand for extra sign bits.
394
395define i32 @ashr_lowmask(i32 %x) {
396; CHECK-LABEL: @ashr_lowmask(
397; CHECK-NEXT:    [[TMP1:%.*]] = lshr i32 [[X:%.*]], 24
398; CHECK-NEXT:    ret i32 [[TMP1]]
399;
400  %a = ashr i32 %x, 24
401  %r = and i32 %a, 255
402  ret i32 %r
403}
404
405define i32 @ashr_lowmask_use(i32 %x) {
406; CHECK-LABEL: @ashr_lowmask_use(
407; CHECK-NEXT:    [[A:%.*]] = ashr i32 [[X:%.*]], 1
408; CHECK-NEXT:    call void @use32(i32 [[A]])
409; CHECK-NEXT:    [[R:%.*]] = lshr i32 [[X]], 1
410; CHECK-NEXT:    ret i32 [[R]]
411;
412  %a = ashr i32 %x, 1
413  call void @use32(i32 %a)
414  %r = and i32 %a, 2147483647
415  ret i32 %r
416}
417
418define <2 x i8> @ashr_lowmask_use_splat(<2 x i8> %x, <2 x i8>* %p) {
419; CHECK-LABEL: @ashr_lowmask_use_splat(
420; CHECK-NEXT:    [[A:%.*]] = ashr <2 x i8> [[X:%.*]], <i8 7, i8 7>
421; CHECK-NEXT:    store <2 x i8> [[A]], <2 x i8>* [[P:%.*]], align 2
422; CHECK-NEXT:    [[R:%.*]] = lshr <2 x i8> [[X]], <i8 7, i8 7>
423; CHECK-NEXT:    ret <2 x i8> [[R]]
424;
425  %a = ashr <2 x i8> %x, <i8 7, i8 7>
426  store <2 x i8> %a, <2 x i8>* %p
427  %r = and <2 x i8> %a, <i8 1, i8 1>
428  ret <2 x i8> %r
429}
430
431; negative test - must keep all low bits
432
433define i32 @ashr_not_lowmask1_use(i32 %x) {
434; CHECK-LABEL: @ashr_not_lowmask1_use(
435; CHECK-NEXT:    [[A:%.*]] = ashr i32 [[X:%.*]], 24
436; CHECK-NEXT:    call void @use32(i32 [[A]])
437; CHECK-NEXT:    [[R:%.*]] = and i32 [[A]], 254
438; CHECK-NEXT:    ret i32 [[R]]
439;
440  %a = ashr i32 %x, 24
441  call void @use32(i32 %a)
442  %r = and i32 %a, 254
443  ret i32 %r
444}
445
446; negative test - must keep all low bits
447
448define i32 @ashr_not_lowmask2_use(i32 %x) {
449; CHECK-LABEL: @ashr_not_lowmask2_use(
450; CHECK-NEXT:    [[A:%.*]] = ashr i32 [[X:%.*]], 24
451; CHECK-NEXT:    call void @use32(i32 [[A]])
452; CHECK-NEXT:    [[R:%.*]] = and i32 [[A]], 127
453; CHECK-NEXT:    ret i32 [[R]]
454;
455  %a = ashr i32 %x, 24
456  call void @use32(i32 %a)
457  %r = and i32 %a, 127
458  ret i32 %r
459}
460
461; negative test - must keep only low bits
462
463define i32 @ashr_not_lowmask3_use(i32 %x) {
464; CHECK-LABEL: @ashr_not_lowmask3_use(
465; CHECK-NEXT:    [[A:%.*]] = ashr i32 [[X:%.*]], 24
466; CHECK-NEXT:    call void @use32(i32 [[A]])
467; CHECK-NEXT:    [[R:%.*]] = and i32 [[A]], 511
468; CHECK-NEXT:    ret i32 [[R]]
469;
470  %a = ashr i32 %x, 24
471  call void @use32(i32 %a)
472  %r = and i32 %a, 511
473  ret i32 %r
474}
475
476define i32 @test29(i8 %X) {
477; CHECK-LABEL: @test29(
478; CHECK-NEXT:    [[Y:%.*]] = zext i8 [[X:%.*]] to i32
479; CHECK-NEXT:    ret i32 [[Y]]
480;
481  %Y = zext i8 %X to i32
482  ;; Zero extend makes this unneeded.
483  %Z = and i32 %Y, 255
484  ret i32 %Z
485}
486
487define i32 @test30(i1 %X) {
488; CHECK-LABEL: @test30(
489; CHECK-NEXT:    [[Y:%.*]] = zext i1 [[X:%.*]] to i32
490; CHECK-NEXT:    ret i32 [[Y]]
491;
492  %Y = zext i1 %X to i32
493  %Z = and i32 %Y, 1
494  ret i32 %Z
495}
496
497define i32 @test31(i1 %X) {
498; CHECK-LABEL: @test31(
499; CHECK-NEXT:    [[Z:%.*]] = select i1 [[X:%.*]], i32 16, i32 0
500; CHECK-NEXT:    ret i32 [[Z]]
501;
502  %Y = zext i1 %X to i32
503  %Z = shl i32 %Y, 4
504  %A = and i32 %Z, 16
505  ret i32 %A
506}
507
508; Demanded bit analysis allows us to eliminate the add.
509
510define <2 x i32> @and_demanded_bits_splat_vec(<2 x i32> %x) {
511; CHECK-LABEL: @and_demanded_bits_splat_vec(
512; CHECK-NEXT:    [[Z:%.*]] = and <2 x i32> [[X:%.*]], <i32 7, i32 7>
513; CHECK-NEXT:    ret <2 x i32> [[Z]]
514;
515  %y = add <2 x i32> %x, <i32 8, i32 8>
516  %z = and <2 x i32> %y, <i32 7, i32 7>
517  ret <2 x i32> %z
518}
519
520; zext (x >> 8) has all zeros in the high 24-bits:  0x000000xx
521; (y | 255) has all ones in the low 8-bits: 0xyyyyyyff
522; 'and' of those is all known bits - it's just 'z'.
523
524define i32 @and_zext_demanded(i16 %x, i32 %y) {
525; CHECK-LABEL: @and_zext_demanded(
526; CHECK-NEXT:    [[S:%.*]] = lshr i16 [[X:%.*]], 8
527; CHECK-NEXT:    [[Z:%.*]] = zext i16 [[S]] to i32
528; CHECK-NEXT:    ret i32 [[Z]]
529;
530  %s = lshr i16 %x, 8
531  %z = zext i16 %s to i32
532  %o = or i32 %y, 255
533  %a = and i32 %o, %z
534  ret i32 %a
535}
536
537define i32 @test32(i32 %In) {
538; CHECK-LABEL: @test32(
539; CHECK-NEXT:    ret i32 0
540;
541  %Y = and i32 %In, 16
542  %Z = lshr i32 %Y, 2
543  %A = and i32 %Z, 1
544  ret i32 %A
545}
546
547;; Code corresponding to one-bit bitfield ^1.
548define i32 @test33(i32 %b) {
549; CHECK-LABEL: @test33(
550; CHECK-NEXT:    [[T13:%.*]] = xor i32 [[B:%.*]], 1
551; CHECK-NEXT:    ret i32 [[T13]]
552;
553  %t4.mask = and i32 %b, 1
554  %t10 = xor i32 %t4.mask, 1
555  %t12 = and i32 %b, -2
556  %t13 = or i32 %t12, %t10
557  ret i32 %t13
558}
559
560define i32 @test33b(i32 %b) {
561; CHECK-LABEL: @test33b(
562; CHECK-NEXT:    [[T13:%.*]] = xor i32 [[B:%.*]], 1
563; CHECK-NEXT:    ret i32 [[T13]]
564;
565  %t4.mask = and i32 %b, 1
566  %t10 = xor i32 %t4.mask, 1
567  %t12 = and i32 %b, -2
568  %t13 = or i32 %t10, %t12
569  ret i32 %t13
570}
571
572define <2 x i32> @test33vec(<2 x i32> %b) {
573; CHECK-LABEL: @test33vec(
574; CHECK-NEXT:    [[T13:%.*]] = xor <2 x i32> [[B:%.*]], <i32 1, i32 1>
575; CHECK-NEXT:    ret <2 x i32> [[T13]]
576;
577  %t4.mask = and <2 x i32> %b, <i32 1, i32 1>
578  %t10 = xor <2 x i32> %t4.mask, <i32 1, i32 1>
579  %t12 = and <2 x i32> %b, <i32 -2, i32 -2>
580  %t13 = or <2 x i32> %t12, %t10
581  ret <2 x i32> %t13
582}
583
584define <2 x i32> @test33vecb(<2 x i32> %b) {
585; CHECK-LABEL: @test33vecb(
586; CHECK-NEXT:    [[T13:%.*]] = xor <2 x i32> [[B:%.*]], <i32 1, i32 1>
587; CHECK-NEXT:    ret <2 x i32> [[T13]]
588;
589  %t4.mask = and <2 x i32> %b, <i32 1, i32 1>
590  %t10 = xor <2 x i32> %t4.mask, <i32 1, i32 1>
591  %t12 = and <2 x i32> %b, <i32 -2, i32 -2>
592  %t13 = or <2 x i32> %t10, %t12
593  ret <2 x i32> %t13
594}
595
596define i32 @test34(i32 %A, i32 %B) {
597; CHECK-LABEL: @test34(
598; CHECK-NEXT:    ret i32 [[B:%.*]]
599;
600  %t2 = or i32 %B, %A
601  %t4 = and i32 %t2, %B
602  ret i32 %t4
603}
604
605; FIXME: This test should only need -instsimplify (ValueTracking / computeKnownBits), not -passes=instcombine.
606
607define <2 x i32> @PR24942(<2 x i32> %x) {
608; CHECK-LABEL: @PR24942(
609; CHECK-NEXT:    ret <2 x i32> zeroinitializer
610;
611  %lshr = lshr <2 x i32> %x, <i32 31, i32 31>
612  %and = and <2 x i32> %lshr, <i32 2, i32 2>
613  ret <2 x i32> %and
614}
615
616define i64 @test35(i32 %X) {
617; CHECK-LABEL: @test35(
618; CHECK-NEXT:    [[TMP1:%.*]] = sub i32 0, [[X:%.*]]
619; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], 240
620; CHECK-NEXT:    [[RES:%.*]] = zext i32 [[TMP2]] to i64
621; CHECK-NEXT:    ret i64 [[RES]]
622;
623  %zext = zext i32 %X to i64
624  %zsub = sub i64 0, %zext
625  %res = and i64 %zsub, 240
626  ret i64 %res
627}
628
629define <2 x i64> @test35_uniform(<2 x i32> %X) {
630; CHECK-LABEL: @test35_uniform(
631; CHECK-NEXT:    [[TMP1:%.*]] = sub <2 x i32> zeroinitializer, [[X:%.*]]
632; CHECK-NEXT:    [[TMP2:%.*]] = and <2 x i32> [[TMP1]], <i32 240, i32 240>
633; CHECK-NEXT:    [[RES:%.*]] = zext <2 x i32> [[TMP2]] to <2 x i64>
634; CHECK-NEXT:    ret <2 x i64> [[RES]]
635;
636  %zext = zext <2 x i32> %X to <2 x i64>
637  %zsub = sub <2 x i64> zeroinitializer, %zext
638  %res = and <2 x i64> %zsub, <i64 240, i64 240>
639  ret <2 x i64> %res
640}
641
642define i64 @test36(i32 %X) {
643; CHECK-LABEL: @test36(
644; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[X:%.*]], 7
645; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], 240
646; CHECK-NEXT:    [[RES:%.*]] = zext i32 [[TMP2]] to i64
647; CHECK-NEXT:    ret i64 [[RES]]
648;
649  %zext = zext i32 %X to i64
650  %zsub = add i64 %zext, 7
651  %res = and i64 %zsub, 240
652  ret i64 %res
653}
654
655define <2 x i64> @test36_uniform(<2 x i32> %X) {
656; CHECK-LABEL: @test36_uniform(
657; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i32> [[X:%.*]], <i32 7, i32 7>
658; CHECK-NEXT:    [[TMP2:%.*]] = and <2 x i32> [[TMP1]], <i32 240, i32 240>
659; CHECK-NEXT:    [[RES:%.*]] = zext <2 x i32> [[TMP2]] to <2 x i64>
660; CHECK-NEXT:    ret <2 x i64> [[RES]]
661;
662  %zext = zext <2 x i32> %X to <2 x i64>
663  %zsub = add <2 x i64> %zext, <i64 7, i64 7>
664  %res = and <2 x i64> %zsub, <i64 240, i64 240>
665  ret <2 x i64> %res
666}
667
668define <2 x i64> @test36_undef(<2 x i32> %X) {
669; CHECK-LABEL: @test36_undef(
670; CHECK-NEXT:    [[ZEXT:%.*]] = zext <2 x i32> [[X:%.*]] to <2 x i64>
671; CHECK-NEXT:    [[ZSUB:%.*]] = add <2 x i64> [[ZEXT]], <i64 7, i64 undef>
672; CHECK-NEXT:    [[RES:%.*]] = and <2 x i64> [[ZSUB]], <i64 240, i64 undef>
673; CHECK-NEXT:    ret <2 x i64> [[RES]]
674;
675  %zext = zext <2 x i32> %X to <2 x i64>
676  %zsub = add <2 x i64> %zext, <i64 7, i64 undef>
677  %res = and <2 x i64> %zsub, <i64 240, i64 undef>
678  ret <2 x i64> %res
679}
680
681define i64 @test37(i32 %X) {
682; CHECK-LABEL: @test37(
683; CHECK-NEXT:    [[TMP1:%.*]] = mul i32 [[X:%.*]], 7
684; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], 240
685; CHECK-NEXT:    [[RES:%.*]] = zext i32 [[TMP2]] to i64
686; CHECK-NEXT:    ret i64 [[RES]]
687;
688  %zext = zext i32 %X to i64
689  %zsub = mul i64 %zext, 7
690  %res = and i64 %zsub, 240
691  ret i64 %res
692}
693
694define <2 x i64> @test37_uniform(<2 x i32> %X) {
695; CHECK-LABEL: @test37_uniform(
696; CHECK-NEXT:    [[TMP1:%.*]] = mul <2 x i32> [[X:%.*]], <i32 7, i32 7>
697; CHECK-NEXT:    [[TMP2:%.*]] = and <2 x i32> [[TMP1]], <i32 240, i32 240>
698; CHECK-NEXT:    [[RES:%.*]] = zext <2 x i32> [[TMP2]] to <2 x i64>
699; CHECK-NEXT:    ret <2 x i64> [[RES]]
700;
701  %zext = zext <2 x i32> %X to <2 x i64>
702  %zsub = mul <2 x i64> %zext, <i64 7, i64 7>
703  %res = and <2 x i64> %zsub, <i64 240, i64 240>
704  ret <2 x i64> %res
705}
706
707define <2 x i64> @test37_nonuniform(<2 x i32> %X) {
708; CHECK-LABEL: @test37_nonuniform(
709; CHECK-NEXT:    [[ZEXT:%.*]] = zext <2 x i32> [[X:%.*]] to <2 x i64>
710; CHECK-NEXT:    [[ZSUB:%.*]] = mul nuw nsw <2 x i64> [[ZEXT]], <i64 7, i64 9>
711; CHECK-NEXT:    [[RES:%.*]] = and <2 x i64> [[ZSUB]], <i64 240, i64 110>
712; CHECK-NEXT:    ret <2 x i64> [[RES]]
713;
714  %zext = zext <2 x i32> %X to <2 x i64>
715  %zsub = mul <2 x i64> %zext, <i64 7, i64 9>
716  %res = and <2 x i64> %zsub, <i64 240, i64 110>
717  ret <2 x i64> %res
718}
719
720define i64 @test38(i32 %X) {
721; CHECK-LABEL: @test38(
722; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 240
723; CHECK-NEXT:    [[RES:%.*]] = zext i32 [[TMP1]] to i64
724; CHECK-NEXT:    ret i64 [[RES]]
725;
726  %zext = zext i32 %X to i64
727  %zsub = xor i64 %zext, 7
728  %res = and i64 %zsub, 240
729  ret i64 %res
730}
731
732define i64 @test39(i32 %X) {
733; CHECK-LABEL: @test39(
734; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 240
735; CHECK-NEXT:    [[RES:%.*]] = zext i32 [[TMP1]] to i64
736; CHECK-NEXT:    ret i64 [[RES]]
737;
738  %zext = zext i32 %X to i64
739  %zsub = or i64 %zext, 7
740  %res = and i64 %zsub, 240
741  ret i64 %res
742}
743
744define i32 @test40(i1 %C) {
745; CHECK-LABEL: @test40(
746; CHECK-NEXT:    [[A:%.*]] = select i1 [[C:%.*]], i32 104, i32 10
747; CHECK-NEXT:    ret i32 [[A]]
748;
749  %A = select i1 %C, i32 1000, i32 10
750  %V = and i32 %A, 123
751  ret i32 %V
752}
753
754define <2 x i32> @test40vec(i1 %C) {
755; CHECK-LABEL: @test40vec(
756; CHECK-NEXT:    [[A:%.*]] = select i1 [[C:%.*]], <2 x i32> <i32 104, i32 104>, <2 x i32> <i32 10, i32 10>
757; CHECK-NEXT:    ret <2 x i32> [[A]]
758;
759  %A = select i1 %C, <2 x i32> <i32 1000, i32 1000>, <2 x i32> <i32 10, i32 10>
760  %V = and <2 x i32> %A, <i32 123, i32 123>
761  ret <2 x i32> %V
762}
763
764define <2 x i32> @test40vec2(i1 %C) {
765; CHECK-LABEL: @test40vec2(
766; CHECK-NEXT:    [[V:%.*]] = select i1 [[C:%.*]], <2 x i32> <i32 104, i32 324>, <2 x i32> <i32 10, i32 12>
767; CHECK-NEXT:    ret <2 x i32> [[V]]
768;
769  %A = select i1 %C, <2 x i32> <i32 1000, i32 2500>, <2 x i32> <i32 10, i32 30>
770  %V = and <2 x i32> %A, <i32 123, i32 333>
771  ret <2 x i32> %V
772}
773
774define i32 @test41(i1 %which) {
775; CHECK-LABEL: @test41(
776; CHECK-NEXT:  entry:
777; CHECK-NEXT:    br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
778; CHECK:       delay:
779; CHECK-NEXT:    br label [[FINAL]]
780; CHECK:       final:
781; CHECK-NEXT:    [[A:%.*]] = phi i32 [ 104, [[ENTRY:%.*]] ], [ 10, [[DELAY]] ]
782; CHECK-NEXT:    ret i32 [[A]]
783;
784entry:
785  br i1 %which, label %final, label %delay
786
787delay:
788  br label %final
789
790final:
791  %A = phi i32 [ 1000, %entry ], [ 10, %delay ]
792  %value = and i32 %A, 123
793  ret i32 %value
794}
795
796define <2 x i32> @test41vec(i1 %which) {
797; CHECK-LABEL: @test41vec(
798; CHECK-NEXT:  entry:
799; CHECK-NEXT:    br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
800; CHECK:       delay:
801; CHECK-NEXT:    br label [[FINAL]]
802; CHECK:       final:
803; CHECK-NEXT:    [[A:%.*]] = phi <2 x i32> [ <i32 104, i32 104>, [[ENTRY:%.*]] ], [ <i32 10, i32 10>, [[DELAY]] ]
804; CHECK-NEXT:    ret <2 x i32> [[A]]
805;
806entry:
807  br i1 %which, label %final, label %delay
808
809delay:
810  br label %final
811
812final:
813  %A = phi <2 x i32> [ <i32 1000, i32 1000>, %entry ], [ <i32 10, i32 10>, %delay ]
814  %value = and <2 x i32> %A, <i32 123, i32 123>
815  ret <2 x i32> %value
816}
817
818define <2 x i32> @test41vec2(i1 %which) {
819; CHECK-LABEL: @test41vec2(
820; CHECK-NEXT:  entry:
821; CHECK-NEXT:    br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
822; CHECK:       delay:
823; CHECK-NEXT:    br label [[FINAL]]
824; CHECK:       final:
825; CHECK-NEXT:    [[A:%.*]] = phi <2 x i32> [ <i32 104, i32 324>, [[ENTRY:%.*]] ], [ <i32 10, i32 12>, [[DELAY]] ]
826; CHECK-NEXT:    ret <2 x i32> [[A]]
827;
828entry:
829  br i1 %which, label %final, label %delay
830
831delay:
832  br label %final
833
834final:
835  %A = phi <2 x i32> [ <i32 1000, i32 2500>, %entry ], [ <i32 10, i32 30>, %delay ]
836  %value = and <2 x i32> %A, <i32 123, i32 333>
837  ret <2 x i32> %value
838}
839
840define i32 @test42(i32 %a, i32 %c, i32 %d) {
841; CHECK-LABEL: @test42(
842; CHECK-NEXT:    [[FORCE:%.*]] = mul i32 [[C:%.*]], [[D:%.*]]
843; CHECK-NEXT:    [[AND:%.*]] = and i32 [[FORCE]], [[A:%.*]]
844; CHECK-NEXT:    ret i32 [[AND]]
845;
846  %force = mul i32 %c, %d ; forces the complexity sorting
847  %or = or i32 %a, %force
848  %nota = xor i32 %a, -1
849  %xor = xor i32 %nota, %force
850  %and = and i32 %xor, %or
851  ret i32 %and
852}
853
854define i32 @test43(i32 %a, i32 %c, i32 %d) {
855; CHECK-LABEL: @test43(
856; CHECK-NEXT:    [[FORCE:%.*]] = mul i32 [[C:%.*]], [[D:%.*]]
857; CHECK-NEXT:    [[AND:%.*]] = and i32 [[FORCE]], [[A:%.*]]
858; CHECK-NEXT:    ret i32 [[AND]]
859;
860  %force = mul i32 %c, %d ; forces the complexity sorting
861  %or = or i32 %a, %force
862  %nota = xor i32 %a, -1
863  %xor = xor i32 %nota, %force
864  %and = and i32 %or, %xor
865  ret i32 %and
866}
867
868; (~y | x) & y -> x & y
869define i32 @test44(i32 %x, i32 %y) nounwind {
870; CHECK-LABEL: @test44(
871; CHECK-NEXT:    [[A:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
872; CHECK-NEXT:    ret i32 [[A]]
873;
874  %n = xor i32 %y, -1
875  %o = or i32 %n, %x
876  %a = and i32 %o, %y
877  ret i32 %a
878}
879
880; (x | ~y) & y -> x & y
881define i32 @test45(i32 %x, i32 %y) nounwind {
882; CHECK-LABEL: @test45(
883; CHECK-NEXT:    [[A:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
884; CHECK-NEXT:    ret i32 [[A]]
885;
886  %n = xor i32 %y, -1
887  %o = or i32 %x, %n
888  %a = and i32 %o, %y
889  ret i32 %a
890}
891
892; y & (~y | x) -> y | x
893define i32 @test46(i32 %x, i32 %y) nounwind {
894; CHECK-LABEL: @test46(
895; CHECK-NEXT:    [[A:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
896; CHECK-NEXT:    ret i32 [[A]]
897;
898  %n = xor i32 %y, -1
899  %o = or i32 %n, %x
900  %a = and i32 %y, %o
901  ret i32 %a
902}
903
904; y & (x | ~y) -> y | x
905define i32 @test47(i32 %x, i32 %y) nounwind {
906; CHECK-LABEL: @test47(
907; CHECK-NEXT:    [[A:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
908; CHECK-NEXT:    ret i32 [[A]]
909;
910  %n = xor i32 %y, -1
911  %o = or i32 %x, %n
912  %a = and i32 %y, %o
913  ret i32 %a
914}
915
916; In the next 4 tests, vary the types and predicates for extra coverage.
917; (X & (Y | ~X)) -> (X & Y), where 'not' is an inverted cmp
918
919define i1 @and_orn_cmp_1(i32 %a, i32 %b, i32 %c) {
920; CHECK-LABEL: @and_orn_cmp_1(
921; CHECK-NEXT:    [[X:%.*]] = icmp sgt i32 [[A:%.*]], [[B:%.*]]
922; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i32 [[C:%.*]], 42
923; CHECK-NEXT:    [[AND:%.*]] = and i1 [[X]], [[Y]]
924; CHECK-NEXT:    ret i1 [[AND]]
925;
926  %x = icmp sgt i32 %a, %b
927  %x_inv = icmp sle i32 %a, %b
928  %y = icmp ugt i32 %c, 42      ; thwart complexity-based ordering
929  %or = or i1 %y, %x_inv
930  %and = and i1 %x, %or
931  ret i1 %and
932}
933
934define i1 @and_orn_cmp_1_logical(i32 %a, i32 %b, i32 %c) {
935; CHECK-LABEL: @and_orn_cmp_1_logical(
936; CHECK-NEXT:    [[X:%.*]] = icmp sgt i32 [[A:%.*]], [[B:%.*]]
937; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i32 [[C:%.*]], 42
938; CHECK-NEXT:    [[AND:%.*]] = select i1 [[X]], i1 [[Y]], i1 false
939; CHECK-NEXT:    ret i1 [[AND]]
940;
941  %x = icmp sgt i32 %a, %b
942  %x_inv = icmp sle i32 %a, %b
943  %y = icmp ugt i32 %c, 42      ; thwart complexity-based ordering
944  %or = select i1 %y, i1 true, i1 %x_inv
945  %and = select i1 %x, i1 %or, i1 false
946  ret i1 %and
947}
948
949; Commute the 'and':
950; ((Y | ~X) & X) -> (X & Y), where 'not' is an inverted cmp
951
952define <2 x i1> @and_orn_cmp_2(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c) {
953; CHECK-LABEL: @and_orn_cmp_2(
954; CHECK-NEXT:    [[X:%.*]] = icmp sge <2 x i32> [[A:%.*]], [[B:%.*]]
955; CHECK-NEXT:    [[Y:%.*]] = icmp ugt <2 x i32> [[C:%.*]], <i32 42, i32 47>
956; CHECK-NEXT:    [[AND:%.*]] = and <2 x i1> [[Y]], [[X]]
957; CHECK-NEXT:    ret <2 x i1> [[AND]]
958;
959  %x = icmp sge <2 x i32> %a, %b
960  %x_inv = icmp slt <2 x i32> %a, %b
961  %y = icmp ugt <2 x i32> %c, <i32 42, i32 47>      ; thwart complexity-based ordering
962  %or = or <2 x i1> %y, %x_inv
963  %and = and <2 x i1> %or, %x
964  ret <2 x i1> %and
965}
966
967; Commute the 'or':
968; (X & (~X | Y)) -> (X & Y), where 'not' is an inverted cmp
969
970define i1 @and_orn_cmp_3(i72 %a, i72 %b, i72 %c) {
971; CHECK-LABEL: @and_orn_cmp_3(
972; CHECK-NEXT:    [[X:%.*]] = icmp ugt i72 [[A:%.*]], [[B:%.*]]
973; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i72 [[C:%.*]], 42
974; CHECK-NEXT:    [[AND:%.*]] = and i1 [[X]], [[Y]]
975; CHECK-NEXT:    ret i1 [[AND]]
976;
977  %x = icmp ugt i72 %a, %b
978  %x_inv = icmp ule i72 %a, %b
979  %y = icmp ugt i72 %c, 42      ; thwart complexity-based ordering
980  %or = or i1 %x_inv, %y
981  %and = and i1 %x, %or
982  ret i1 %and
983}
984
985define i1 @and_orn_cmp_3_logical(i72 %a, i72 %b, i72 %c) {
986; CHECK-LABEL: @and_orn_cmp_3_logical(
987; CHECK-NEXT:    [[X:%.*]] = icmp ugt i72 [[A:%.*]], [[B:%.*]]
988; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i72 [[C:%.*]], 42
989; CHECK-NEXT:    [[AND:%.*]] = select i1 [[X]], i1 [[Y]], i1 false
990; CHECK-NEXT:    ret i1 [[AND]]
991;
992  %x = icmp ugt i72 %a, %b
993  %x_inv = icmp ule i72 %a, %b
994  %y = icmp ugt i72 %c, 42      ; thwart complexity-based ordering
995  %or = select i1 %x_inv, i1 true, i1 %y
996  %and = select i1 %x, i1 %or, i1 false
997  ret i1 %and
998}
999
1000; Commute the 'and':
1001; ((~X | Y) & X) -> (X & Y), where 'not' is an inverted cmp
1002
1003define <3 x i1> @or_andn_cmp_4(<3 x i32> %a, <3 x i32> %b, <3 x i32> %c) {
1004; CHECK-LABEL: @or_andn_cmp_4(
1005; CHECK-NEXT:    [[X:%.*]] = icmp eq <3 x i32> [[A:%.*]], [[B:%.*]]
1006; CHECK-NEXT:    [[Y:%.*]] = icmp ugt <3 x i32> [[C:%.*]], <i32 42, i32 43, i32 -1>
1007; CHECK-NEXT:    [[AND:%.*]] = and <3 x i1> [[Y]], [[X]]
1008; CHECK-NEXT:    ret <3 x i1> [[AND]]
1009;
1010  %x = icmp eq <3 x i32> %a, %b
1011  %x_inv = icmp ne <3 x i32> %a, %b
1012  %y = icmp ugt <3 x i32> %c, <i32 42, i32 43, i32 -1>      ; thwart complexity-based ordering
1013  %or = or <3 x i1> %x_inv, %y
1014  %and = and <3 x i1> %or, %x
1015  ret <3 x i1> %and
1016}
1017
1018; In the next 4 tests, vary the types and predicates for extra coverage.
1019; (~X & (Y | X)) -> (~X & Y), where 'not' is an inverted cmp
1020
1021define i1 @andn_or_cmp_1(i37 %a, i37 %b, i37 %c) {
1022; CHECK-LABEL: @andn_or_cmp_1(
1023; CHECK-NEXT:    [[X_INV:%.*]] = icmp sle i37 [[A:%.*]], [[B:%.*]]
1024; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i37 [[C:%.*]], 42
1025; CHECK-NEXT:    [[AND:%.*]] = and i1 [[X_INV]], [[Y]]
1026; CHECK-NEXT:    ret i1 [[AND]]
1027;
1028  %x = icmp sgt i37 %a, %b
1029  %x_inv = icmp sle i37 %a, %b
1030  %y = icmp ugt i37 %c, 42      ; thwart complexity-based ordering
1031  %or = or i1 %y, %x
1032  %and = and i1 %x_inv, %or
1033  ret i1 %and
1034}
1035
1036define i1 @andn_or_cmp_1_logical(i37 %a, i37 %b, i37 %c) {
1037; CHECK-LABEL: @andn_or_cmp_1_logical(
1038; CHECK-NEXT:    [[X_INV:%.*]] = icmp sle i37 [[A:%.*]], [[B:%.*]]
1039; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i37 [[C:%.*]], 42
1040; CHECK-NEXT:    [[AND:%.*]] = select i1 [[X_INV]], i1 [[Y]], i1 false
1041; CHECK-NEXT:    ret i1 [[AND]]
1042;
1043  %x = icmp sgt i37 %a, %b
1044  %x_inv = icmp sle i37 %a, %b
1045  %y = icmp ugt i37 %c, 42      ; thwart complexity-based ordering
1046  %or = select i1 %y, i1 true, i1 %x
1047  %and = select i1 %x_inv, i1 %or, i1 false
1048  ret i1 %and
1049}
1050
1051; Commute the 'and':
1052; ((Y | X) & ~X) -> (~X & Y), where 'not' is an inverted cmp
1053
1054define i1 @andn_or_cmp_2(i16 %a, i16 %b, i16 %c) {
1055; CHECK-LABEL: @andn_or_cmp_2(
1056; CHECK-NEXT:    [[X_INV:%.*]] = icmp slt i16 [[A:%.*]], [[B:%.*]]
1057; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i16 [[C:%.*]], 42
1058; CHECK-NEXT:    [[AND:%.*]] = and i1 [[Y]], [[X_INV]]
1059; CHECK-NEXT:    ret i1 [[AND]]
1060;
1061  %x = icmp sge i16 %a, %b
1062  %x_inv = icmp slt i16 %a, %b
1063  %y = icmp ugt i16 %c, 42      ; thwart complexity-based ordering
1064  %or = or i1 %y, %x
1065  %and = and i1 %or, %x_inv
1066  ret i1 %and
1067}
1068
1069define i1 @andn_or_cmp_2_logical(i16 %a, i16 %b, i16 %c) {
1070; CHECK-LABEL: @andn_or_cmp_2_logical(
1071; CHECK-NEXT:    [[X_INV:%.*]] = icmp slt i16 [[A:%.*]], [[B:%.*]]
1072; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i16 [[C:%.*]], 42
1073; CHECK-NEXT:    [[AND:%.*]] = select i1 [[Y]], i1 [[X_INV]], i1 false
1074; CHECK-NEXT:    ret i1 [[AND]]
1075;
1076  %x = icmp sge i16 %a, %b
1077  %x_inv = icmp slt i16 %a, %b
1078  %y = icmp ugt i16 %c, 42      ; thwart complexity-based ordering
1079  %or = select i1 %y, i1 true, i1 %x
1080  %and = select i1 %or, i1 %x_inv, i1 false
1081  ret i1 %and
1082}
1083
1084; Commute the 'or':
1085; (~X & (X | Y)) -> (~X & Y), where 'not' is an inverted cmp
1086
1087define <4 x i1> @andn_or_cmp_3(<4 x i32> %a, <4 x i32> %b, <4 x i32> %c) {
1088; CHECK-LABEL: @andn_or_cmp_3(
1089; CHECK-NEXT:    [[X_INV:%.*]] = icmp ule <4 x i32> [[A:%.*]], [[B:%.*]]
1090; CHECK-NEXT:    [[Y:%.*]] = icmp ugt <4 x i32> [[C:%.*]], <i32 42, i32 0, i32 1, i32 -1>
1091; CHECK-NEXT:    [[AND:%.*]] = and <4 x i1> [[X_INV]], [[Y]]
1092; CHECK-NEXT:    ret <4 x i1> [[AND]]
1093;
1094  %x = icmp ugt <4 x i32> %a, %b
1095  %x_inv = icmp ule <4 x i32> %a, %b
1096  %y = icmp ugt <4 x i32> %c, <i32 42, i32 0, i32 1, i32 -1>      ; thwart complexity-based ordering
1097  %or = or <4 x i1> %x, %y
1098  %and = and <4 x i1> %x_inv, %or
1099  ret <4 x i1> %and
1100}
1101
1102; Commute the 'and':
1103; ((X | Y) & ~X) -> (~X & Y), where 'not' is an inverted cmp
1104
1105define i1 @andn_or_cmp_4(i32 %a, i32 %b, i32 %c) {
1106; CHECK-LABEL: @andn_or_cmp_4(
1107; CHECK-NEXT:    [[X_INV:%.*]] = icmp ne i32 [[A:%.*]], [[B:%.*]]
1108; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i32 [[C:%.*]], 42
1109; CHECK-NEXT:    [[AND:%.*]] = and i1 [[Y]], [[X_INV]]
1110; CHECK-NEXT:    ret i1 [[AND]]
1111;
1112  %x = icmp eq i32 %a, %b
1113  %x_inv = icmp ne i32 %a, %b
1114  %y = icmp ugt i32 %c, 42      ; thwart complexity-based ordering
1115  %or = or i1 %x, %y
1116  %and = and i1 %or, %x_inv
1117  ret i1 %and
1118}
1119
1120define i1 @andn_or_cmp_4_logical(i32 %a, i32 %b, i32 %c) {
1121; CHECK-LABEL: @andn_or_cmp_4_logical(
1122; CHECK-NEXT:    [[X_INV:%.*]] = icmp ne i32 [[A:%.*]], [[B:%.*]]
1123; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i32 [[C:%.*]], 42
1124; CHECK-NEXT:    [[AND:%.*]] = select i1 [[X_INV]], i1 [[Y]], i1 false
1125; CHECK-NEXT:    ret i1 [[AND]]
1126;
1127  %x = icmp eq i32 %a, %b
1128  %x_inv = icmp ne i32 %a, %b
1129  %y = icmp ugt i32 %c, 42      ; thwart complexity-based ordering
1130  %or = select i1 %x, i1 true, i1 %y
1131  %and = select i1 %or, i1 %x_inv, i1 false
1132  ret i1 %and
1133}
1134
1135define i32 @lowbitmask_casted_shift(i8 %x) {
1136; CHECK-LABEL: @lowbitmask_casted_shift(
1137; CHECK-NEXT:    [[TMP1:%.*]] = sext i8 [[X:%.*]] to i32
1138; CHECK-NEXT:    [[R:%.*]] = lshr i32 [[TMP1]], 1
1139; CHECK-NEXT:    ret i32 [[R]]
1140;
1141  %a = ashr i8 %x, 1
1142  %s = sext i8 %a to i32
1143  %r = and i32 %s, 2147483647
1144  ret i32 %r
1145}
1146
1147; Negative test - mask constant is too big.
1148
1149define i32 @lowbitmask_casted_shift_wrong_mask1(i8 %x) {
1150; CHECK-LABEL: @lowbitmask_casted_shift_wrong_mask1(
1151; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[X:%.*]], 2
1152; CHECK-NEXT:    [[S:%.*]] = sext i8 [[A]] to i32
1153; CHECK-NEXT:    [[R:%.*]] = and i32 [[S]], 2147483647
1154; CHECK-NEXT:    ret i32 [[R]]
1155;
1156  %a = ashr i8 %x, 2
1157  %s = sext i8 %a to i32
1158  %r = and i32 %s, 2147483647 ; 0x7fffffff
1159  ret i32 %r
1160}
1161
1162; Negative test - mask constant is too small.
1163
1164define i32 @lowbitmask_casted_shift_wrong_mask2(i8 %x) {
1165; CHECK-LABEL: @lowbitmask_casted_shift_wrong_mask2(
1166; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[X:%.*]], 2
1167; CHECK-NEXT:    [[S:%.*]] = sext i8 [[A]] to i32
1168; CHECK-NEXT:    [[R:%.*]] = and i32 [[S]], 536870911
1169; CHECK-NEXT:    ret i32 [[R]]
1170;
1171  %a = ashr i8 %x, 2
1172  %s = sext i8 %a to i32
1173  %r = and i32 %s, 536870911  ; 0x1fffffff
1174  ret i32 %r
1175}
1176
1177; Extra use of shift is ok.
1178
1179define i32 @lowbitmask_casted_shift_use1(i8 %x) {
1180; CHECK-LABEL: @lowbitmask_casted_shift_use1(
1181; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[X:%.*]], 3
1182; CHECK-NEXT:    call void @use8(i8 [[A]])
1183; CHECK-NEXT:    [[TMP1:%.*]] = sext i8 [[X]] to i32
1184; CHECK-NEXT:    [[R:%.*]] = lshr i32 [[TMP1]], 3
1185; CHECK-NEXT:    ret i32 [[R]]
1186;
1187  %a = ashr i8 %x, 3
1188  call void @use8(i8 %a)
1189  %s = sext i8 %a to i32
1190  %r = and i32 %s, 536870911
1191  ret i32 %r
1192}
1193
1194; Negative test - extra use of sext requires more instructions.
1195
1196define i32 @lowbitmask_casted_shift_use2(i8 %x) {
1197; CHECK-LABEL: @lowbitmask_casted_shift_use2(
1198; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[X:%.*]], 3
1199; CHECK-NEXT:    [[S:%.*]] = sext i8 [[A]] to i32
1200; CHECK-NEXT:    call void @use32(i32 [[S]])
1201; CHECK-NEXT:    [[R:%.*]] = and i32 [[S]], 536870911
1202; CHECK-NEXT:    ret i32 [[R]]
1203;
1204  %a = ashr i8 %x, 3
1205  %s = sext i8 %a to i32
1206  call void @use32(i32 %s)
1207  %r = and i32 %s, 536870911
1208  ret i32 %r
1209}
1210
1211; Vectors/weird types are ok.
1212
1213define <2 x i59> @lowbitmask_casted_shift_vec_splat(<2 x i47> %x) {
1214; CHECK-LABEL: @lowbitmask_casted_shift_vec_splat(
1215; CHECK-NEXT:    [[TMP1:%.*]] = sext <2 x i47> [[X:%.*]] to <2 x i59>
1216; CHECK-NEXT:    [[R:%.*]] = lshr <2 x i59> [[TMP1]], <i59 5, i59 5>
1217; CHECK-NEXT:    ret <2 x i59> [[R]]
1218;
1219  %a = ashr <2 x i47> %x, <i47 5, i47 5>
1220  %s = sext <2 x i47> %a to <2 x i59>
1221  %r = and <2 x i59> %s, <i59 18014398509481983, i59 18014398509481983>  ;  -1 u>> 5 == 0x3f_ffff_ffff_ffff
1222  ret <2 x i59> %r
1223}
1224
1225define i32 @lowmask_sext_in_reg(i32 %x) {
1226; CHECK-LABEL: @lowmask_sext_in_reg(
1227; CHECK-NEXT:    [[L:%.*]] = shl i32 [[X:%.*]], 20
1228; CHECK-NEXT:    [[R:%.*]] = ashr exact i32 [[L]], 20
1229; CHECK-NEXT:    call void @use32(i32 [[R]])
1230; CHECK-NEXT:    [[AND:%.*]] = and i32 [[X]], 4095
1231; CHECK-NEXT:    ret i32 [[AND]]
1232;
1233  %l = shl i32 %x, 20
1234  %r = ashr i32 %l, 20
1235  call void @use32(i32 %r)
1236  %and = and i32 %r, 4095
1237  ret i32 %and
1238}
1239
1240; Mismatched shift amounts, but the mask op can be replaced by a shift.
1241
1242define i32 @lowmask_not_sext_in_reg(i32 %x) {
1243; CHECK-LABEL: @lowmask_not_sext_in_reg(
1244; CHECK-NEXT:    [[L:%.*]] = shl i32 [[X:%.*]], 19
1245; CHECK-NEXT:    [[R:%.*]] = ashr i32 [[L]], 20
1246; CHECK-NEXT:    call void @use32(i32 [[R]])
1247; CHECK-NEXT:    [[AND:%.*]] = lshr i32 [[L]], 20
1248; CHECK-NEXT:    ret i32 [[AND]]
1249;
1250  %l = shl i32 %x, 19
1251  %r = ashr i32 %l, 20
1252  call void @use32(i32 %r)
1253  %and = and i32 %r, 4095
1254  ret i32 %and
1255}
1256
1257; Negative test - too much shift for mask
1258
1259define i32 @not_lowmask_sext_in_reg(i32 %x) {
1260; CHECK-LABEL: @not_lowmask_sext_in_reg(
1261; CHECK-NEXT:    [[L:%.*]] = shl i32 [[X:%.*]], 20
1262; CHECK-NEXT:    [[R:%.*]] = ashr exact i32 [[L]], 20
1263; CHECK-NEXT:    call void @use32(i32 [[R]])
1264; CHECK-NEXT:    [[AND:%.*]] = and i32 [[R]], 4096
1265; CHECK-NEXT:    ret i32 [[AND]]
1266;
1267  %l = shl i32 %x, 20
1268  %r = ashr i32 %l, 20
1269  call void @use32(i32 %r)
1270  %and = and i32 %r, 4096
1271  ret i32 %and
1272}
1273
1274; Negative test - too much shift for mask
1275
1276define i32 @not_lowmask_sext_in_reg2(i32 %x) {
1277; CHECK-LABEL: @not_lowmask_sext_in_reg2(
1278; CHECK-NEXT:    [[L:%.*]] = shl i32 [[X:%.*]], 21
1279; CHECK-NEXT:    [[R:%.*]] = ashr exact i32 [[L]], 21
1280; CHECK-NEXT:    call void @use32(i32 [[R]])
1281; CHECK-NEXT:    [[AND:%.*]] = and i32 [[R]], 4095
1282; CHECK-NEXT:    ret i32 [[AND]]
1283;
1284  %l = shl i32 %x, 21
1285  %r = ashr i32 %l, 21
1286  call void @use32(i32 %r)
1287  %and = and i32 %r, 4095
1288  ret i32 %and
1289}
1290
1291define <2 x i32> @lowmask_sext_in_reg_splat(<2 x i32> %x, <2 x i32>* %p) {
1292; CHECK-LABEL: @lowmask_sext_in_reg_splat(
1293; CHECK-NEXT:    [[L:%.*]] = shl <2 x i32> [[X:%.*]], <i32 20, i32 20>
1294; CHECK-NEXT:    [[R:%.*]] = ashr exact <2 x i32> [[L]], <i32 20, i32 20>
1295; CHECK-NEXT:    store <2 x i32> [[R]], <2 x i32>* [[P:%.*]], align 8
1296; CHECK-NEXT:    [[AND:%.*]] = and <2 x i32> [[X]], <i32 4095, i32 4095>
1297; CHECK-NEXT:    ret <2 x i32> [[AND]]
1298;
1299  %l = shl <2 x i32> %x, <i32 20, i32 20>
1300  %r = ashr <2 x i32> %l, <i32 20, i32 20>
1301  store <2 x i32> %r, <2 x i32>* %p
1302  %and = and <2 x i32> %r, <i32 4095, i32 4095>
1303  ret <2 x i32> %and
1304}
1305
1306define i8 @lowmask_add(i8 %x) {
1307; CHECK-LABEL: @lowmask_add(
1308; CHECK-NEXT:    [[A:%.*]] = add i8 [[X:%.*]], -64
1309; CHECK-NEXT:    call void @use8(i8 [[A]])
1310; CHECK-NEXT:    [[R:%.*]] = and i8 [[X]], 32
1311; CHECK-NEXT:    ret i8 [[R]]
1312;
1313  %a = add i8 %x, -64 ; 0xc0
1314  call void @use8(i8 %a)
1315  %r = and i8 %a, 32 ; 0x20
1316  ret i8 %r
1317}
1318
1319define i8 @lowmask_add_2(i8 %x) {
1320; CHECK-LABEL: @lowmask_add_2(
1321; CHECK-NEXT:    [[R:%.*]] = and i8 [[X:%.*]], 63
1322; CHECK-NEXT:    ret i8 [[R]]
1323;
1324  %a = add i8 %x, -64 ; 0xc0
1325  %r = and i8 %a, 63 ; 0x3f
1326  ret i8 %r
1327}
1328
1329define i8 @lowmask_add_2_uses(i8 %x) {
1330; CHECK-LABEL: @lowmask_add_2_uses(
1331; CHECK-NEXT:    [[A:%.*]] = add i8 [[X:%.*]], -64
1332; CHECK-NEXT:    call void @use8(i8 [[A]])
1333; CHECK-NEXT:    [[R:%.*]] = and i8 [[X]], 63
1334; CHECK-NEXT:    ret i8 [[R]]
1335;
1336  %a = add i8 %x, -64 ; 0xc0
1337  call void @use8(i8 %a)
1338  %r = and i8 %a, 63 ; 0x3f
1339  ret i8 %r
1340}
1341
1342define <2 x i8> @lowmask_add_2_splat(<2 x i8> %x, <2 x i8>* %p) {
1343; CHECK-LABEL: @lowmask_add_2_splat(
1344; CHECK-NEXT:    [[A:%.*]] = add <2 x i8> [[X:%.*]], <i8 -64, i8 -64>
1345; CHECK-NEXT:    store <2 x i8> [[A]], <2 x i8>* [[P:%.*]], align 2
1346; CHECK-NEXT:    [[R:%.*]] = and <2 x i8> [[X]], <i8 63, i8 63>
1347; CHECK-NEXT:    ret <2 x i8> [[R]]
1348;
1349  %a = add <2 x i8> %x, <i8 -64, i8 -64> ; 0xc0
1350  store <2 x i8> %a, <2 x i8>* %p
1351  %r = and <2 x i8> %a, <i8 63, i8 63> ; 0x3f
1352  ret <2 x i8> %r
1353}
1354
1355; Negative test - mask overlaps low bit of add
1356
1357define i8 @not_lowmask_add(i8 %x) {
1358; CHECK-LABEL: @not_lowmask_add(
1359; CHECK-NEXT:    [[A:%.*]] = add i8 [[X:%.*]], -64
1360; CHECK-NEXT:    call void @use8(i8 [[A]])
1361; CHECK-NEXT:    [[R:%.*]] = and i8 [[A]], 64
1362; CHECK-NEXT:    ret i8 [[R]]
1363;
1364  %a = add i8 %x, -64 ; 0xc0
1365  call void @use8(i8 %a)
1366  %r = and i8 %a, 64 ; 0x40
1367  ret i8 %r
1368}
1369
1370; Negative test - mask overlaps low bit of add
1371
1372define i8 @not_lowmask_add2(i8 %x) {
1373; CHECK-LABEL: @not_lowmask_add2(
1374; CHECK-NEXT:    [[A:%.*]] = add i8 [[X:%.*]], -96
1375; CHECK-NEXT:    call void @use8(i8 [[A]])
1376; CHECK-NEXT:    [[R:%.*]] = and i8 [[A]], 63
1377; CHECK-NEXT:    ret i8 [[R]]
1378;
1379  %a = add i8 %x, -96 ; 0xe0
1380  call void @use8(i8 %a)
1381  %r = and i8 %a, 63 ; 0x3f
1382  ret i8 %r
1383}
1384
1385define <2 x i8> @lowmask_add_splat(<2 x i8> %x, <2 x i8>* %p) {
1386; CHECK-LABEL: @lowmask_add_splat(
1387; CHECK-NEXT:    [[A:%.*]] = add <2 x i8> [[X:%.*]], <i8 -64, i8 -64>
1388; CHECK-NEXT:    store <2 x i8> [[A]], <2 x i8>* [[P:%.*]], align 2
1389; CHECK-NEXT:    [[R:%.*]] = and <2 x i8> [[X]], <i8 32, i8 32>
1390; CHECK-NEXT:    ret <2 x i8> [[R]]
1391;
1392  %a = add <2 x i8> %x, <i8 -64, i8 -64> ; 0xc0
1393  store <2 x i8> %a, <2 x i8>* %p
1394  %r = and <2 x i8> %a, <i8 32, i8 32> ; 0x20
1395  ret <2 x i8> %r
1396}
1397
1398define <2 x i8> @lowmask_add_splat_undef(<2 x i8> %x, <2 x i8>* %p) {
1399; CHECK-LABEL: @lowmask_add_splat_undef(
1400; CHECK-NEXT:    [[A:%.*]] = add <2 x i8> [[X:%.*]], <i8 -64, i8 undef>
1401; CHECK-NEXT:    store <2 x i8> [[A]], <2 x i8>* [[P:%.*]], align 2
1402; CHECK-NEXT:    [[R:%.*]] = and <2 x i8> [[A]], <i8 undef, i8 32>
1403; CHECK-NEXT:    ret <2 x i8> [[R]]
1404;
1405  %a = add <2 x i8> %x, <i8 -64, i8 undef> ; 0xc0
1406  store <2 x i8> %a, <2 x i8>* %p
1407  %r = and <2 x i8> %a, <i8 undef, i8 32> ; 0x20
1408  ret <2 x i8> %r
1409}
1410
1411define <2 x i8> @lowmask_add_vec(<2 x i8> %x, <2 x i8>* %p) {
1412; CHECK-LABEL: @lowmask_add_vec(
1413; CHECK-NEXT:    [[A:%.*]] = add <2 x i8> [[X:%.*]], <i8 -96, i8 -64>
1414; CHECK-NEXT:    store <2 x i8> [[A]], <2 x i8>* [[P:%.*]], align 2
1415; CHECK-NEXT:    [[R:%.*]] = and <2 x i8> [[A]], <i8 16, i8 32>
1416; CHECK-NEXT:    ret <2 x i8> [[R]]
1417;
1418  %a = add <2 x i8> %x, <i8 -96, i8 -64> ; 0xe0, 0xc0
1419  store <2 x i8> %a, <2 x i8>* %p
1420  %r = and <2 x i8> %a, <i8 16, i8 32> ; 0x10, 0x20
1421  ret <2 x i8> %r
1422}
1423
1424; Only one bit set
1425define i8 @flip_masked_bit(i8 %A) {
1426; CHECK-LABEL: @flip_masked_bit(
1427; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[A:%.*]], 16
1428; CHECK-NEXT:    [[C:%.*]] = xor i8 [[TMP1]], 16
1429; CHECK-NEXT:    ret i8 [[C]]
1430;
1431  %B = add i8 %A, 16
1432  %C = and i8 %B, 16
1433  ret i8 %C
1434}
1435
1436define <2 x i8> @flip_masked_bit_uniform(<2 x i8> %A) {
1437; CHECK-LABEL: @flip_masked_bit_uniform(
1438; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i8> [[A:%.*]], <i8 16, i8 16>
1439; CHECK-NEXT:    [[C:%.*]] = xor <2 x i8> [[TMP1]], <i8 16, i8 16>
1440; CHECK-NEXT:    ret <2 x i8> [[C]]
1441;
1442  %B = add <2 x i8> %A, <i8 16, i8 16>
1443  %C = and <2 x i8> %B, <i8 16, i8 16>
1444  ret <2 x i8> %C
1445}
1446
1447define <2 x i8> @flip_masked_bit_undef(<2 x i8> %A) {
1448; CHECK-LABEL: @flip_masked_bit_undef(
1449; CHECK-NEXT:    [[B:%.*]] = add <2 x i8> [[A:%.*]], <i8 16, i8 undef>
1450; CHECK-NEXT:    [[C:%.*]] = and <2 x i8> [[B]], <i8 16, i8 undef>
1451; CHECK-NEXT:    ret <2 x i8> [[C]]
1452;
1453  %B = add <2 x i8> %A, <i8 16, i8 undef>
1454  %C = and <2 x i8> %B, <i8 16, i8 undef>
1455  ret <2 x i8> %C
1456}
1457
1458define <2 x i8> @flip_masked_bit_nonuniform(<2 x i8> %A) {
1459; CHECK-LABEL: @flip_masked_bit_nonuniform(
1460; CHECK-NEXT:    [[B:%.*]] = add <2 x i8> [[A:%.*]], <i8 16, i8 4>
1461; CHECK-NEXT:    [[C:%.*]] = and <2 x i8> [[B]], <i8 16, i8 4>
1462; CHECK-NEXT:    ret <2 x i8> [[C]]
1463;
1464  %B = add <2 x i8> %A, <i8 16, i8 4>
1465  %C = and <2 x i8> %B, <i8 16, i8 4>
1466  ret <2 x i8> %C
1467}
1468
1469define i8 @ashr_bitwidth_mask(i8 %x, i8 %y) {
1470; CHECK-LABEL: @ashr_bitwidth_mask(
1471; CHECK-NEXT:    [[ISNEG:%.*]] = icmp slt i8 [[X:%.*]], 0
1472; CHECK-NEXT:    [[NEG_OR_ZERO:%.*]] = select i1 [[ISNEG]], i8 [[Y:%.*]], i8 0
1473; CHECK-NEXT:    ret i8 [[NEG_OR_ZERO]]
1474;
1475  %sign = ashr i8 %x, 7
1476  %neg_or_zero = and i8 %sign, %y
1477  ret i8 %neg_or_zero
1478}
1479
1480define <2 x i8> @ashr_bitwidth_mask_vec_commute(<2 x i8> %x, <2 x i8> %py) {
1481; CHECK-LABEL: @ashr_bitwidth_mask_vec_commute(
1482; CHECK-NEXT:    [[Y:%.*]] = mul <2 x i8> [[PY:%.*]], <i8 42, i8 2>
1483; CHECK-NEXT:    [[ISNEG:%.*]] = icmp slt <2 x i8> [[X:%.*]], zeroinitializer
1484; CHECK-NEXT:    [[NEG_OR_ZERO:%.*]] = select <2 x i1> [[ISNEG]], <2 x i8> [[Y]], <2 x i8> zeroinitializer
1485; CHECK-NEXT:    ret <2 x i8> [[NEG_OR_ZERO]]
1486;
1487  %y = mul <2 x i8> %py, <i8 42, i8 2>      ; thwart complexity-based ordering
1488  %sign = ashr <2 x i8> %x, <i8 7, i8 7>
1489  %neg_or_zero = and <2 x i8> %y, %sign
1490  ret <2 x i8> %neg_or_zero
1491}
1492
1493; negative test - extra use
1494
1495define i8 @ashr_bitwidth_mask_use(i8 %x, i8 %y) {
1496; CHECK-LABEL: @ashr_bitwidth_mask_use(
1497; CHECK-NEXT:    [[SIGN:%.*]] = ashr i8 [[X:%.*]], 7
1498; CHECK-NEXT:    call void @use8(i8 [[SIGN]])
1499; CHECK-NEXT:    [[R:%.*]] = and i8 [[SIGN]], [[Y:%.*]]
1500; CHECK-NEXT:    ret i8 [[R]]
1501;
1502  %sign = ashr i8 %x, 7
1503  call void @use8(i8 %sign)
1504  %r = and i8 %sign, %y
1505  ret i8 %r
1506}
1507
1508; negative test - wrong shift amount
1509
1510define i8 @ashr_not_bitwidth_mask(i8 %x, i8 %y) {
1511; CHECK-LABEL: @ashr_not_bitwidth_mask(
1512; CHECK-NEXT:    [[SIGN:%.*]] = ashr i8 [[X:%.*]], 6
1513; CHECK-NEXT:    [[R:%.*]] = and i8 [[SIGN]], [[Y:%.*]]
1514; CHECK-NEXT:    ret i8 [[R]]
1515;
1516  %sign = ashr i8 %x, 6
1517  %r = and i8 %sign, %y
1518  ret i8 %r
1519}
1520
1521; negative test - wrong shift opcode
1522
1523define i8 @lshr_bitwidth_mask(i8 %x, i8 %y) {
1524; CHECK-LABEL: @lshr_bitwidth_mask(
1525; CHECK-NEXT:    [[SIGN:%.*]] = lshr i8 [[X:%.*]], 7
1526; CHECK-NEXT:    [[R:%.*]] = and i8 [[SIGN]], [[Y:%.*]]
1527; CHECK-NEXT:    ret i8 [[R]]
1528;
1529  %sign = lshr i8 %x, 7
1530  %r = and i8 %sign, %y
1531  ret i8 %r
1532}
1533
1534define i8 @not_ashr_bitwidth_mask(i8 %x, i8 %y) {
1535; CHECK-LABEL: @not_ashr_bitwidth_mask(
1536; CHECK-NEXT:    [[ISNEG:%.*]] = icmp slt i8 [[X:%.*]], 0
1537; CHECK-NEXT:    [[POS_OR_ZERO:%.*]] = select i1 [[ISNEG]], i8 0, i8 [[Y:%.*]]
1538; CHECK-NEXT:    ret i8 [[POS_OR_ZERO]]
1539;
1540  %sign = ashr i8 %x, 7
1541  %not = xor i8 %sign, -1
1542  %pos_or_zero = and i8 %not, %y
1543  ret i8 %pos_or_zero
1544}
1545
1546define <2 x i8> @not_ashr_bitwidth_mask_vec_commute(<2 x i8> %x, <2 x i8> %py) {
1547; CHECK-LABEL: @not_ashr_bitwidth_mask_vec_commute(
1548; CHECK-NEXT:    [[Y:%.*]] = mul <2 x i8> [[PY:%.*]], <i8 42, i8 2>
1549; CHECK-NEXT:    [[ISNEG:%.*]] = icmp slt <2 x i8> [[X:%.*]], zeroinitializer
1550; CHECK-NEXT:    [[POS_OR_ZERO:%.*]] = select <2 x i1> [[ISNEG]], <2 x i8> zeroinitializer, <2 x i8> [[Y]]
1551; CHECK-NEXT:    ret <2 x i8> [[POS_OR_ZERO]]
1552;
1553  %y = mul <2 x i8> %py, <i8 42, i8 2>      ; thwart complexity-based ordering
1554  %sign = ashr <2 x i8> %x, <i8 7, i8 7>
1555  %not = xor <2 x i8> %sign, <i8 -1, i8 -1>
1556  %pos_or_zero = and <2 x i8> %y, %not
1557  ret <2 x i8> %pos_or_zero
1558}
1559
1560; extra use of shift is ok
1561
1562define i8 @not_ashr_bitwidth_mask_use1(i8 %x, i8 %y) {
1563; CHECK-LABEL: @not_ashr_bitwidth_mask_use1(
1564; CHECK-NEXT:    [[SIGN:%.*]] = ashr i8 [[X:%.*]], 7
1565; CHECK-NEXT:    call void @use8(i8 [[SIGN]])
1566; CHECK-NEXT:    [[ISNEG:%.*]] = icmp slt i8 [[X]], 0
1567; CHECK-NEXT:    [[R:%.*]] = select i1 [[ISNEG]], i8 0, i8 [[Y:%.*]]
1568; CHECK-NEXT:    ret i8 [[R]]
1569;
1570  %sign = ashr i8 %x, 7
1571  call void @use8(i8 %sign)
1572  %not = xor i8 %sign, -1
1573  %r = and i8 %not, %y
1574  ret i8 %r
1575}
1576
1577; negative test - extra use
1578
1579define i8 @not_ashr_bitwidth_mask_use2(i8 %x, i8 %y) {
1580; CHECK-LABEL: @not_ashr_bitwidth_mask_use2(
1581; CHECK-NEXT:    [[SIGN:%.*]] = ashr i8 [[X:%.*]], 7
1582; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[SIGN]], -1
1583; CHECK-NEXT:    call void @use8(i8 [[NOT]])
1584; CHECK-NEXT:    [[R:%.*]] = and i8 [[NOT]], [[Y:%.*]]
1585; CHECK-NEXT:    ret i8 [[R]]
1586;
1587  %sign = ashr i8 %x, 7
1588  %not = xor i8 %sign, -1
1589  call void @use8(i8 %not)
1590  %r = and i8 %not, %y
1591  ret i8 %r
1592}
1593
1594; negative test - wrong shift amount
1595
1596define i8 @not_ashr_not_bitwidth_mask(i8 %x, i8 %y) {
1597; CHECK-LABEL: @not_ashr_not_bitwidth_mask(
1598; CHECK-NEXT:    [[SIGN:%.*]] = ashr i8 [[X:%.*]], 6
1599; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[SIGN]], -1
1600; CHECK-NEXT:    [[R:%.*]] = and i8 [[NOT]], [[Y:%.*]]
1601; CHECK-NEXT:    ret i8 [[R]]
1602;
1603  %sign = ashr i8 %x, 6
1604  %not = xor i8 %sign, -1
1605  %r = and i8 %not, %y
1606  ret i8 %r
1607}
1608
1609; negative test - wrong shift opcode
1610
1611define i8 @not_lshr_bitwidth_mask(i8 %x, i8 %y) {
1612; CHECK-LABEL: @not_lshr_bitwidth_mask(
1613; CHECK-NEXT:    [[SIGN:%.*]] = lshr i8 [[X:%.*]], 7
1614; CHECK-NEXT:    [[NOT:%.*]] = xor i8 [[SIGN]], -1
1615; CHECK-NEXT:    [[R:%.*]] = and i8 [[NOT]], [[Y:%.*]]
1616; CHECK-NEXT:    ret i8 [[R]]
1617;
1618  %sign = lshr i8 %x, 7
1619  %not = xor i8 %sign, -1
1620  %r = and i8 %not, %y
1621  ret i8 %r
1622}
1623