1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3
4target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n32:64"
5declare void @use(i32)
6
7; Should be eliminated
8define i32 @test12(i32 %A) {
9; CHECK-LABEL: @test12(
10; CHECK-NEXT:    [[C:%.*]] = and i32 [[A:%.*]], 8
11; CHECK-NEXT:    ret i32 [[C]]
12;
13  %B = or i32 %A, 4
14  %C = and i32 %B, 8
15  ret i32 %C
16}
17
18define i32 @test13(i32 %A) {
19; CHECK-LABEL: @test13(
20; CHECK-NEXT:    ret i32 8
21;
22  %B = or i32 %A, 12
23  ; Always equal to 8
24  %C = and i32 %B, 8
25  ret i32 %C
26}
27
28define i1 @test14(i32 %A, i32 %B) {
29; CHECK-LABEL: @test14(
30; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i32 [[A:%.*]], [[B:%.*]]
31; CHECK-NEXT:    ret i1 [[TMP1]]
32;
33  %C1 = icmp ult i32 %A, %B
34  %C2 = icmp ugt i32 %A, %B
35  ; (A < B) | (A > B) === A != B
36  %D = or i1 %C1, %C2
37  ret i1 %D
38}
39
40define i1 @test14_commuted(i32 %A, i32 %B) {
41; CHECK-LABEL: @test14_commuted(
42; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i32 [[B:%.*]], [[A:%.*]]
43; CHECK-NEXT:    ret i1 [[TMP1]]
44;
45  %C1 = icmp ult i32 %A, %B
46  %C2 = icmp ult i32 %B, %A
47  ; (A < B) | (A > B) === A != B
48  %D = or i1 %C1, %C2
49  ret i1 %D
50}
51
52define i1 @test14_logical(i32 %A, i32 %B) {
53; CHECK-LABEL: @test14_logical(
54; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i32 [[A:%.*]], [[B:%.*]]
55; CHECK-NEXT:    ret i1 [[TMP1]]
56;
57  %C1 = icmp ult i32 %A, %B
58  %C2 = icmp ugt i32 %A, %B
59  ; (A < B) | (A > B) === A != B
60  %D = select i1 %C1, i1 true, i1 %C2
61  ret i1 %D
62}
63
64define i1 @test15(i32 %A, i32 %B) {
65; CHECK-LABEL: @test15(
66; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule i32 [[A:%.*]], [[B:%.*]]
67; CHECK-NEXT:    ret i1 [[TMP1]]
68;
69  %C1 = icmp ult i32 %A, %B
70  %C2 = icmp eq i32 %A, %B
71  ; (A < B) | (A == B) === A <= B
72  %D = or i1 %C1, %C2
73  ret i1 %D
74}
75
76define i1 @test15_logical(i32 %A, i32 %B) {
77; CHECK-LABEL: @test15_logical(
78; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule i32 [[A:%.*]], [[B:%.*]]
79; CHECK-NEXT:    ret i1 [[TMP1]]
80;
81  %C1 = icmp ult i32 %A, %B
82  %C2 = icmp eq i32 %A, %B
83  ; (A < B) | (A == B) === A <= B
84  %D = select i1 %C1, i1 true, i1 %C2
85  ret i1 %D
86}
87
88define i32 @test16(i32 %A) {
89; CHECK-LABEL: @test16(
90; CHECK-NEXT:    ret i32 [[A:%.*]]
91;
92  %B = and i32 %A, 1
93  ; -2 = ~1
94  %C = and i32 %A, -2
95  ; %D = and int %B, -1 == %B
96  %D = or i32 %B, %C
97  ret i32 %D
98}
99
100define i32 @test17(i32 %A) {
101; CHECK-LABEL: @test17(
102; CHECK-NEXT:    [[D:%.*]] = and i32 [[A:%.*]], 5
103; CHECK-NEXT:    ret i32 [[D]]
104;
105  %B = and i32 %A, 1
106  %C = and i32 %A, 4
107  ; %D = and int %B, 5
108  %D = or i32 %B, %C
109  ret i32 %D
110}
111
112define i1 @test18(i32 %A) {
113; CHECK-LABEL: @test18(
114; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[A:%.*]], -100
115; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i32 [[TMP1]], -50
116; CHECK-NEXT:    ret i1 [[TMP2]]
117;
118  %B = icmp sge i32 %A, 100
119  %C = icmp slt i32 %A, 50
120  %D = or i1 %B, %C
121  ret i1 %D
122}
123
124define i1 @test18_logical(i32 %A) {
125; CHECK-LABEL: @test18_logical(
126; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[A:%.*]], -100
127; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i32 [[TMP1]], -50
128; CHECK-NEXT:    ret i1 [[TMP2]]
129;
130  %B = icmp sge i32 %A, 100
131  %C = icmp slt i32 %A, 50
132  %D = select i1 %B, i1 true, i1 %C
133  ret i1 %D
134}
135
136define <2 x i1> @test18vec(<2 x i32> %A) {
137; CHECK-LABEL: @test18vec(
138; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i32> [[A:%.*]], <i32 -100, i32 -100>
139; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult <2 x i32> [[TMP1]], <i32 -50, i32 -50>
140; CHECK-NEXT:    ret <2 x i1> [[TMP2]]
141;
142  %B = icmp sge <2 x i32> %A, <i32 100, i32 100>
143  %C = icmp slt <2 x i32> %A, <i32 50, i32 50>
144  %D = or <2 x i1> %B, %C
145  ret <2 x i1> %D
146}
147
148define i32 @test20(i32 %x) {
149; CHECK-LABEL: @test20(
150; CHECK-NEXT:    ret i32 [[X:%.*]]
151;
152  %y = and i32 %x, 123
153  %z = or i32 %y, %x
154  ret i32 %z
155}
156
157define i32 @test21(i32 %t1) {
158; CHECK-LABEL: @test21(
159; CHECK-NEXT:    [[T1_MASK1:%.*]] = add i32 [[T1:%.*]], 2
160; CHECK-NEXT:    ret i32 [[T1_MASK1]]
161;
162  %t1.mask1 = add i32 %t1, 2
163  %t3 = and i32 %t1.mask1, -2
164  %t5 = and i32 %t1, 1
165  ;; add tmp.1, 2
166  %t6 = or i32 %t5, %t3
167  ret i32 %t6
168}
169
170define i32 @test22(i32 %B) {
171; CHECK-LABEL: @test22(
172; CHECK-NEXT:    ret i32 [[B:%.*]]
173;
174  %ELIM41 = and i32 %B, 1
175  %ELIM7 = and i32 %B, -2
176  %ELIM5 = or i32 %ELIM41, %ELIM7
177  ret i32 %ELIM5
178}
179
180define i16 @test23(i16 %A) {
181; CHECK-LABEL: @test23(
182; CHECK-NEXT:    [[B:%.*]] = lshr i16 [[A:%.*]], 1
183; CHECK-NEXT:    [[D:%.*]] = xor i16 [[B]], -24575
184; CHECK-NEXT:    ret i16 [[D]]
185;
186  %B = lshr i16 %A, 1
187  ;; fold or into xor
188  %C = or i16 %B, -32768
189  %D = xor i16 %C, 8193
190  ret i16 %D
191}
192
193define <2 x i16> @test23vec(<2 x i16> %A) {
194; CHECK-LABEL: @test23vec(
195; CHECK-NEXT:    [[B:%.*]] = lshr <2 x i16> [[A:%.*]], <i16 1, i16 1>
196; CHECK-NEXT:    [[D:%.*]] = xor <2 x i16> [[B]], <i16 -24575, i16 -24575>
197; CHECK-NEXT:    ret <2 x i16> [[D]]
198;
199  %B = lshr <2 x i16> %A, <i16 1, i16 1>
200  ;; fold or into xor
201  %C = or <2 x i16> %B, <i16 -32768, i16 -32768>
202  %D = xor <2 x i16> %C, <i16 8193, i16 8193>
203  ret <2 x i16> %D
204}
205
206; PR3266 & PR5276
207define i1 @test25(i32 %A, i32 %B) {
208; CHECK-LABEL: @test25(
209; CHECK-NEXT:    [[C:%.*]] = icmp ne i32 [[A:%.*]], 0
210; CHECK-NEXT:    [[D:%.*]] = icmp ne i32 [[B:%.*]], 57
211; CHECK-NEXT:    [[F:%.*]] = and i1 [[C]], [[D]]
212; CHECK-NEXT:    ret i1 [[F]]
213;
214  %C = icmp eq i32 %A, 0
215  %D = icmp eq i32 %B, 57
216  %E = or i1 %C, %D
217  %F = xor i1 %E, -1
218  ret i1 %F
219}
220
221define i1 @test25_logical(i32 %A, i32 %B) {
222; CHECK-LABEL: @test25_logical(
223; CHECK-NEXT:    [[C:%.*]] = icmp ne i32 [[A:%.*]], 0
224; CHECK-NEXT:    [[D:%.*]] = icmp ne i32 [[B:%.*]], 57
225; CHECK-NEXT:    [[E:%.*]] = select i1 [[C]], i1 [[D]], i1 false
226; CHECK-NEXT:    ret i1 [[E]]
227;
228  %C = icmp eq i32 %A, 0
229  %D = icmp eq i32 %B, 57
230  %E = select i1 %C, i1 true, i1 %D
231  %F = xor i1 %E, -1
232  ret i1 %F
233}
234
235; PR5634
236define i1 @and_icmp_eq_0(i32 %A, i32 %B) {
237; CHECK-LABEL: @and_icmp_eq_0(
238; CHECK-NEXT:    [[TMP1:%.*]] = or i32 [[A:%.*]], [[B:%.*]]
239; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32 [[TMP1]], 0
240; CHECK-NEXT:    ret i1 [[TMP2]]
241;
242  %C1 = icmp eq i32 %A, 0
243  %C2 = icmp eq i32 %B, 0
244  ; (A == 0) & (A == 0)   -->   (A|B) == 0
245  %D = and i1 %C1, %C2
246  ret i1 %D
247}
248
249define <2 x i1> @and_icmp_eq_0_vector(<2 x i32> %A, <2 x i32> %B) {
250; CHECK-LABEL: @and_icmp_eq_0_vector(
251; CHECK-NEXT:    [[TMP1:%.*]] = or <2 x i32> [[A:%.*]], [[B:%.*]]
252; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq <2 x i32> [[TMP1]], zeroinitializer
253; CHECK-NEXT:    ret <2 x i1> [[TMP2]]
254;
255  %C1 = icmp eq <2 x i32> %A, zeroinitializer
256  %C2 = icmp eq <2 x i32> %B, zeroinitializer
257  %D = and <2 x i1> %C1, %C2
258  ret <2 x i1> %D
259}
260
261define <2 x i1> @and_icmp_eq_0_vector_undef1(<2 x i32> %A, <2 x i32> %B) {
262; CHECK-LABEL: @and_icmp_eq_0_vector_undef1(
263; CHECK-NEXT:    [[TMP1:%.*]] = or <2 x i32> [[A:%.*]], [[B:%.*]]
264; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq <2 x i32> [[TMP1]], zeroinitializer
265; CHECK-NEXT:    ret <2 x i1> [[TMP2]]
266;
267  %C1 = icmp eq <2 x i32> %A, <i32 0, i32 undef>
268  %C2 = icmp eq <2 x i32> %B, <i32 0, i32 undef>
269  %D = and <2 x i1> %C1, %C2
270  ret <2 x i1> %D
271}
272
273define <2 x i1> @and_icmp_eq_0_vector_undef2(<2 x i32> %A, <2 x i32> %B) {
274; CHECK-LABEL: @and_icmp_eq_0_vector_undef2(
275; CHECK-NEXT:    [[TMP1:%.*]] = or <2 x i32> [[A:%.*]], [[B:%.*]]
276; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq <2 x i32> [[TMP1]], zeroinitializer
277; CHECK-NEXT:    ret <2 x i1> [[TMP2]]
278;
279  %C1 = icmp eq <2 x i32> %A, <i32 0, i32 undef>
280  %C2 = icmp eq <2 x i32> %B, <i32 undef, i32 0>
281  %D = and <2 x i1> %C1, %C2
282  ret <2 x i1> %D
283}
284
285define i1 @and_icmp_eq_0_logical(i32 %A, i32 %B) {
286; CHECK-LABEL: @and_icmp_eq_0_logical(
287; CHECK-NEXT:    [[C1:%.*]] = icmp eq i32 [[A:%.*]], 0
288; CHECK-NEXT:    [[C2:%.*]] = icmp eq i32 [[B:%.*]], 0
289; CHECK-NEXT:    [[D:%.*]] = select i1 [[C1]], i1 [[C2]], i1 false
290; CHECK-NEXT:    ret i1 [[D]]
291;
292  %C1 = icmp eq i32 %A, 0
293  %C2 = icmp eq i32 %B, 0
294  ; (A == 0) & (A == 0)   -->   (A|B) == 0
295  %D = select i1 %C1, i1 %C2, i1 false
296  ret i1 %D
297}
298
299define i1 @test27(i32* %A, i32* %B) {
300; CHECK-LABEL: @test27(
301; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i32* [[A:%.*]], null
302; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq i32* [[B:%.*]], null
303; CHECK-NEXT:    [[E:%.*]] = and i1 [[TMP1]], [[TMP2]]
304; CHECK-NEXT:    ret i1 [[E]]
305;
306  %C1 = ptrtoint i32* %A to i32
307  %C2 = ptrtoint i32* %B to i32
308  %D = or i32 %C1, %C2
309  %E = icmp eq i32 %D, 0
310  ret i1 %E
311}
312
313define <2 x i1> @test27vec(<2 x i32*> %A, <2 x i32*> %B) {
314; CHECK-LABEL: @test27vec(
315; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq <2 x i32*> [[A:%.*]], zeroinitializer
316; CHECK-NEXT:    [[TMP2:%.*]] = icmp eq <2 x i32*> [[B:%.*]], zeroinitializer
317; CHECK-NEXT:    [[E:%.*]] = and <2 x i1> [[TMP1]], [[TMP2]]
318; CHECK-NEXT:    ret <2 x i1> [[E]]
319;
320  %C1 = ptrtoint <2 x i32*> %A to <2 x i32>
321  %C2 = ptrtoint <2 x i32*> %B to <2 x i32>
322  %D = or <2 x i32> %C1, %C2
323  %E = icmp eq <2 x i32> %D, zeroinitializer
324  ret <2 x i1> %E
325}
326
327; PR5634
328define i1 @test28(i32 %A, i32 %B) {
329; CHECK-LABEL: @test28(
330; CHECK-NEXT:    [[TMP1:%.*]] = or i32 [[A:%.*]], [[B:%.*]]
331; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i32 [[TMP1]], 0
332; CHECK-NEXT:    ret i1 [[TMP2]]
333;
334  %C1 = icmp ne i32 %A, 0
335  %C2 = icmp ne i32 %B, 0
336  ; (A != 0) | (A != 0)   -->   (A|B) != 0
337  %D = or i1 %C1, %C2
338  ret i1 %D
339}
340
341define i1 @test28_logical(i32 %A, i32 %B) {
342; CHECK-LABEL: @test28_logical(
343; CHECK-NEXT:    [[C1:%.*]] = icmp ne i32 [[A:%.*]], 0
344; CHECK-NEXT:    [[C2:%.*]] = icmp ne i32 [[B:%.*]], 0
345; CHECK-NEXT:    [[D:%.*]] = select i1 [[C1]], i1 true, i1 [[C2]]
346; CHECK-NEXT:    ret i1 [[D]]
347;
348  %C1 = icmp ne i32 %A, 0
349  %C2 = icmp ne i32 %B, 0
350  ; (A != 0) | (A != 0)   -->   (A|B) != 0
351  %D = select i1 %C1, i1 true, i1 %C2
352  ret i1 %D
353}
354
355define i1 @test29(i32* %A, i32* %B) {
356; CHECK-LABEL: @test29(
357; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i32* [[A:%.*]], null
358; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i32* [[B:%.*]], null
359; CHECK-NEXT:    [[E:%.*]] = or i1 [[TMP1]], [[TMP2]]
360; CHECK-NEXT:    ret i1 [[E]]
361;
362  %C1 = ptrtoint i32* %A to i32
363  %C2 = ptrtoint i32* %B to i32
364  %D = or i32 %C1, %C2
365  %E = icmp ne i32 %D, 0
366  ret i1 %E
367}
368
369define <2 x i1> @test29vec(<2 x i32*> %A, <2 x i32*> %B) {
370; CHECK-LABEL: @test29vec(
371; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne <2 x i32*> [[A:%.*]], zeroinitializer
372; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne <2 x i32*> [[B:%.*]], zeroinitializer
373; CHECK-NEXT:    [[E:%.*]] = or <2 x i1> [[TMP1]], [[TMP2]]
374; CHECK-NEXT:    ret <2 x i1> [[E]]
375;
376  %C1 = ptrtoint <2 x i32*> %A to <2 x i32>
377  %C2 = ptrtoint <2 x i32*> %B to <2 x i32>
378  %D = or <2 x i32> %C1, %C2
379  %E = icmp ne <2 x i32> %D, zeroinitializer
380  ret <2 x i1> %E
381}
382
383; PR4216
384define i32 @test30(i32 %A) {
385; CHECK-LABEL: @test30(
386; CHECK-NEXT:    [[D:%.*]] = and i32 [[A:%.*]], -58312
387; CHECK-NEXT:    [[E:%.*]] = or i32 [[D]], 32962
388; CHECK-NEXT:    ret i32 [[E]]
389;
390  %B = or i32 %A, 32962   ; 0b1000_0000_1100_0010
391  %C = and i32 %A, -65536 ; 0xffff0000
392  %D = and i32 %B, 40186  ; 0b1001_1100_1111_1010
393  %E = or i32 %D, %C
394  ret i32 %E
395}
396
397define <2 x i32> @test30vec(<2 x i32> %A) {
398; CHECK-LABEL: @test30vec(
399; CHECK-NEXT:    [[C:%.*]] = and <2 x i32> [[A:%.*]], <i32 -65536, i32 -65536>
400; CHECK-NEXT:    [[B:%.*]] = and <2 x i32> [[A]], <i32 7224, i32 7224>
401; CHECK-NEXT:    [[D:%.*]] = or <2 x i32> [[B]], <i32 32962, i32 32962>
402; CHECK-NEXT:    [[E:%.*]] = or <2 x i32> [[D]], [[C]]
403; CHECK-NEXT:    ret <2 x i32> [[E]]
404;
405  %B = or <2 x i32> %A, <i32 32962, i32 32962>
406  %C = and <2 x i32> %A, <i32 -65536, i32 -65536>
407  %D = and <2 x i32> %B, <i32 40186, i32 40186>
408  %E = or <2 x i32> %D, %C
409  ret <2 x i32> %E
410}
411
412; PR4216
413define i64 @test31(i64 %A) {
414; CHECK-LABEL: @test31(
415; CHECK-NEXT:    [[E:%.*]] = and i64 [[A:%.*]], 4294908984
416; CHECK-NEXT:    [[F:%.*]] = or i64 [[E]], 32962
417; CHECK-NEXT:    ret i64 [[F]]
418;
419  %B = or i64 %A, 194
420  %D = and i64 %B, 250
421
422  %C = or i64 %A, 32768
423  %E = and i64 %C, 4294941696
424
425  %F = or i64 %D, %E
426  ret i64 %F
427}
428
429define <2 x i64> @test31vec(<2 x i64> %A) {
430; CHECK-LABEL: @test31vec(
431; CHECK-NEXT:    [[E:%.*]] = and <2 x i64> [[A:%.*]], <i64 4294908984, i64 4294908984>
432; CHECK-NEXT:    [[F:%.*]] = or <2 x i64> [[E]], <i64 32962, i64 32962>
433; CHECK-NEXT:    ret <2 x i64> [[F]]
434;
435  %B = or <2 x i64> %A, <i64 194, i64 194>
436  %D = and <2 x i64> %B, <i64 250, i64 250>
437
438  %C = or <2 x i64> %A, <i64 32768, i64 32768>
439  %E = and <2 x i64> %C, <i64 4294941696, i64 4294941696>
440
441  %F = or <2 x i64> %D, %E
442  ret <2 x i64> %F
443}
444
445; codegen is mature enough to handle vector selects.
446define <4 x i32> @test32(<4 x i1> %and.i1352, <4 x i32> %vecinit6.i176, <4 x i32> %vecinit6.i191) {
447; CHECK-LABEL: @test32(
448; CHECK-NEXT:    [[TMP1:%.*]] = select <4 x i1> [[AND_I1352:%.*]], <4 x i32> [[VECINIT6_I176:%.*]], <4 x i32> [[VECINIT6_I191:%.*]]
449; CHECK-NEXT:    ret <4 x i32> [[TMP1]]
450;
451  %and.i135 = sext <4 x i1> %and.i1352 to <4 x i32>
452  %and.i129 = and <4 x i32> %vecinit6.i176, %and.i135
453  %neg.i = xor <4 x i32> %and.i135, <i32 -1, i32 -1, i32 -1, i32 -1>
454  %and.i = and <4 x i32> %vecinit6.i191, %neg.i
455  %or.i = or <4 x i32> %and.i, %and.i129
456  ret <4 x i32> %or.i
457}
458
459define i1 @test33(i1 %X, i1 %Y) {
460; CHECK-LABEL: @test33(
461; CHECK-NEXT:    [[A:%.*]] = or i1 [[X:%.*]], [[Y:%.*]]
462; CHECK-NEXT:    ret i1 [[A]]
463;
464  %a = or i1 %X, %Y
465  %b = or i1 %a, %X
466  ret i1 %b
467}
468
469define i1 @test33_logical(i1 %X, i1 %Y) {
470; CHECK-LABEL: @test33_logical(
471; CHECK-NEXT:    [[A:%.*]] = select i1 [[X:%.*]], i1 true, i1 [[Y:%.*]]
472; CHECK-NEXT:    ret i1 [[A]]
473;
474  %a = select i1 %X, i1 true, i1 %Y
475  %b = select i1 %a, i1 true, i1 %X
476  ret i1 %b
477}
478
479define i32 @test34(i32 %X, i32 %Y) {
480; CHECK-LABEL: @test34(
481; CHECK-NEXT:    [[A:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
482; CHECK-NEXT:    ret i32 [[A]]
483;
484  %a = or i32 %X, %Y
485  %b = or i32 %Y, %a
486  ret i32 %b
487}
488
489define i32 @test35(i32 %a, i32 %b) {
490; CHECK-LABEL: @test35(
491; CHECK-NEXT:    [[TMP1:%.*]] = or i32 [[A:%.*]], [[B:%.*]]
492; CHECK-NEXT:    [[TMP2:%.*]] = or i32 [[TMP1]], 1135
493; CHECK-NEXT:    ret i32 [[TMP2]]
494;
495  %1 = or i32 %a, 1135
496  %2 = or i32 %1, %b
497  ret i32 %2
498}
499
500define i1 @test36(i32 %x) {
501; CHECK-LABEL: @test36(
502; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[X:%.*]], -23
503; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 3
504; CHECK-NEXT:    ret i1 [[TMP2]]
505;
506  %cmp1 = icmp eq i32 %x, 23
507  %cmp2 = icmp eq i32 %x, 24
508  %ret1 = or i1 %cmp1, %cmp2
509  %cmp3 = icmp eq i32 %x, 25
510  %ret2 = or i1 %ret1, %cmp3
511  ret i1 %ret2
512}
513
514define i1 @test36_logical(i32 %x) {
515; CHECK-LABEL: @test36_logical(
516; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[X:%.*]], -23
517; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 3
518; CHECK-NEXT:    ret i1 [[TMP2]]
519;
520  %cmp1 = icmp eq i32 %x, 23
521  %cmp2 = icmp eq i32 %x, 24
522  %ret1 = select i1 %cmp1, i1 true, i1 %cmp2
523  %cmp3 = icmp eq i32 %x, 25
524  %ret2 = select i1 %ret1, i1 true, i1 %cmp3
525  ret i1 %ret2
526}
527
528define i1 @test37(i32 %x) {
529; CHECK-LABEL: @test37(
530; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[X:%.*]], 7
531; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 31
532; CHECK-NEXT:    ret i1 [[TMP2]]
533;
534  %add1 = add i32 %x, 7
535  %cmp1 = icmp ult i32 %add1, 30
536  %cmp2 = icmp eq i32 %x, 23
537  %ret1 = or i1 %cmp1, %cmp2
538  ret i1 %ret1
539}
540
541define i1 @test37_logical(i32 %x) {
542; CHECK-LABEL: @test37_logical(
543; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[X:%.*]], 7
544; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 31
545; CHECK-NEXT:    ret i1 [[TMP2]]
546;
547  %add1 = add i32 %x, 7
548  %cmp1 = icmp ult i32 %add1, 30
549  %cmp2 = icmp eq i32 %x, 23
550  %ret1 = select i1 %cmp1, i1 true, i1 %cmp2
551  ret i1 %ret1
552}
553
554define <2 x i1> @test37_uniform(<2 x i32> %x) {
555; CHECK-LABEL: @test37_uniform(
556; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i32> [[X:%.*]], <i32 7, i32 7>
557; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult <2 x i32> [[TMP1]], <i32 31, i32 31>
558; CHECK-NEXT:    ret <2 x i1> [[TMP2]]
559;
560  %add1 = add <2 x i32> %x, <i32 7, i32 7>
561  %cmp1 = icmp ult <2 x i32> %add1, <i32 30, i32 30>
562  %cmp2 = icmp eq <2 x i32> %x, <i32 23, i32 23>
563  %ret1 = or <2 x i1> %cmp1, %cmp2
564  ret <2 x i1> %ret1
565}
566
567define <2 x i1> @test37_undef(<2 x i32> %x) {
568; CHECK-LABEL: @test37_undef(
569; CHECK-NEXT:    [[ADD1:%.*]] = add <2 x i32> [[X:%.*]], <i32 7, i32 undef>
570; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult <2 x i32> [[ADD1]], <i32 30, i32 undef>
571; CHECK-NEXT:    [[CMP2:%.*]] = icmp eq <2 x i32> [[X]], <i32 23, i32 undef>
572; CHECK-NEXT:    [[RET1:%.*]] = or <2 x i1> [[CMP1]], [[CMP2]]
573; CHECK-NEXT:    ret <2 x i1> [[RET1]]
574;
575  %add1 = add <2 x i32> %x, <i32 7, i32 undef>
576  %cmp1 = icmp ult <2 x i32> %add1, <i32 30, i32 undef>
577  %cmp2 = icmp eq <2 x i32> %x, <i32 23, i32 undef>
578  %ret1 = or <2 x i1> %cmp1, %cmp2
579  ret <2 x i1> %ret1
580}
581
582define i1 @test38(i32 %x) {
583; CHECK-LABEL: @test38(
584; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[X:%.*]], 7
585; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 31
586; CHECK-NEXT:    ret i1 [[TMP2]]
587;
588  %add1 = add i32 %x, 7
589  %cmp1 = icmp eq i32 %x, 23
590  %cmp2 = icmp ult i32 %add1, 30
591  %ret1 = or i1 %cmp1, %cmp2
592  ret i1 %ret1
593}
594
595define i1 @test38_logical(i32 %x) {
596; CHECK-LABEL: @test38_logical(
597; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[X:%.*]], 7
598; CHECK-NEXT:    [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 31
599; CHECK-NEXT:    ret i1 [[TMP2]]
600;
601  %add1 = add i32 %x, 7
602  %cmp1 = icmp eq i32 %x, 23
603  %cmp2 = icmp ult i32 %add1, 30
604  %ret1 = select i1 %cmp1, i1 true, i1 %cmp2
605  ret i1 %ret1
606}
607
608define <2 x i1> @test38_nonuniform(<2 x i32> %x) {
609; CHECK-LABEL: @test38_nonuniform(
610; CHECK-NEXT:    [[ADD1:%.*]] = add <2 x i32> [[X:%.*]], <i32 7, i32 24>
611; CHECK-NEXT:    [[CMP1:%.*]] = icmp eq <2 x i32> [[X]], <i32 23, i32 8>
612; CHECK-NEXT:    [[CMP2:%.*]] = icmp ult <2 x i32> [[ADD1]], <i32 30, i32 32>
613; CHECK-NEXT:    [[RET1:%.*]] = or <2 x i1> [[CMP1]], [[CMP2]]
614; CHECK-NEXT:    ret <2 x i1> [[RET1]]
615;
616  %add1 = add <2 x i32> %x, <i32 7, i32 24>
617  %cmp1 = icmp eq <2 x i32> %x, <i32 23, i32 8>
618  %cmp2 = icmp ult <2 x i32> %add1, <i32 30, i32 32>
619  %ret1 = or <2 x i1> %cmp1, %cmp2
620  ret <2 x i1> %ret1
621}
622
623; (~A & B) | A --> A | B
624
625define i32 @test39a(i32 %a, float %b) {
626; CHECK-LABEL: @test39a(
627; CHECK-NEXT:    [[A1:%.*]] = mul i32 [[A:%.*]], 42
628; CHECK-NEXT:    [[B1:%.*]] = bitcast float [[B:%.*]] to i32
629; CHECK-NEXT:    [[OR:%.*]] = or i32 [[A1]], [[B1]]
630; CHECK-NEXT:    ret i32 [[OR]]
631;
632  %a1 = mul i32 %a, 42          ; thwart complexity-based ordering
633  %b1 = bitcast float %b to i32 ; thwart complexity-based ordering
634  %nota = xor i32 %a1, -1
635  %and = and i32 %nota, %b1
636  %or = or i32 %and, %a1
637  ret i32 %or
638}
639
640; Commute 'and' operands:
641; (B & ~A) | A --> A | B
642
643define i32 @test39b(i32 %a, float %b) {
644; CHECK-LABEL: @test39b(
645; CHECK-NEXT:    [[A1:%.*]] = mul i32 [[A:%.*]], 42
646; CHECK-NEXT:    [[B1:%.*]] = bitcast float [[B:%.*]] to i32
647; CHECK-NEXT:    [[OR:%.*]] = or i32 [[A1]], [[B1]]
648; CHECK-NEXT:    ret i32 [[OR]]
649;
650  %a1 = mul i32 %a, 42          ; thwart complexity-based ordering
651  %b1 = bitcast float %b to i32 ; thwart complexity-based ordering
652  %nota = xor i32 %a1, -1
653  %and = and i32 %b1, %nota
654  %or = or i32 %and, %a1
655  ret i32 %or
656}
657
658; Commute 'or' operands:
659; A | (~A & B) --> A | B
660
661define i32 @test39c(i32 %a, float %b) {
662; CHECK-LABEL: @test39c(
663; CHECK-NEXT:    [[A1:%.*]] = mul i32 [[A:%.*]], 42
664; CHECK-NEXT:    [[B1:%.*]] = bitcast float [[B:%.*]] to i32
665; CHECK-NEXT:    [[OR:%.*]] = or i32 [[A1]], [[B1]]
666; CHECK-NEXT:    ret i32 [[OR]]
667;
668  %a1 = mul i32 %a, 42          ; thwart complexity-based ordering
669  %b1 = bitcast float %b to i32 ; thwart complexity-based ordering
670  %nota = xor i32 %a1, -1
671  %and = and i32 %nota, %b1
672  %or = or i32 %a1, %and
673  ret i32 %or
674}
675
676; Commute 'and' operands:
677; A | (B & ~A) --> A | B
678
679define i32 @test39d(i32 %a, float %b) {
680; CHECK-LABEL: @test39d(
681; CHECK-NEXT:    [[A1:%.*]] = mul i32 [[A:%.*]], 42
682; CHECK-NEXT:    [[B1:%.*]] = bitcast float [[B:%.*]] to i32
683; CHECK-NEXT:    [[OR:%.*]] = or i32 [[A1]], [[B1]]
684; CHECK-NEXT:    ret i32 [[OR]]
685;
686  %a1 = mul i32 %a, 42          ; thwart complexity-based ordering
687  %b1 = bitcast float %b to i32 ; thwart complexity-based ordering
688  %nota = xor i32 %a1, -1
689  %and = and i32 %b1, %nota
690  %or = or i32 %a1, %and
691  ret i32 %or
692}
693
694define i32 @test40(i32 %a, i32 %b) {
695; CHECK-LABEL: @test40(
696; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], -1
697; CHECK-NEXT:    [[OR:%.*]] = or i32 [[XOR]], [[B:%.*]]
698; CHECK-NEXT:    ret i32 [[OR]]
699;
700  %and = and i32 %a, %b
701  %xor = xor i32 %a, -1
702  %or = or i32 %and, %xor
703  ret i32 %or
704}
705
706define i32 @test40b(i32 %a, i32 %b) {
707; CHECK-LABEL: @test40b(
708; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], -1
709; CHECK-NEXT:    [[OR:%.*]] = or i32 [[XOR]], [[B:%.*]]
710; CHECK-NEXT:    ret i32 [[OR]]
711;
712  %and = and i32 %b, %a
713  %xor = xor i32 %a, -1
714  %or = or i32 %and, %xor
715  ret i32 %or
716}
717
718define i32 @test40c(i32 %a, i32 %b) {
719; CHECK-LABEL: @test40c(
720; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], -1
721; CHECK-NEXT:    [[OR:%.*]] = or i32 [[XOR]], [[B:%.*]]
722; CHECK-NEXT:    ret i32 [[OR]]
723;
724  %and = and i32 %b, %a
725  %xor = xor i32 %a, -1
726  %or = or i32 %xor, %and
727  ret i32 %or
728}
729
730define i32 @test40d(i32 %a, i32 %b) {
731; CHECK-LABEL: @test40d(
732; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[A:%.*]], -1
733; CHECK-NEXT:    [[OR:%.*]] = or i32 [[XOR]], [[B:%.*]]
734; CHECK-NEXT:    ret i32 [[OR]]
735;
736  %and = and i32 %a, %b
737  %xor = xor i32 %a, -1
738  %or = or i32 %xor, %and
739  ret i32 %or
740}
741
742define i32 @test45(i32 %x, i32 %y, i32 %z) {
743; CHECK-LABEL: @test45(
744; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], [[Z:%.*]]
745; CHECK-NEXT:    [[OR1:%.*]] = or i32 [[TMP1]], [[Y:%.*]]
746; CHECK-NEXT:    ret i32 [[OR1]]
747;
748  %or = or i32 %y, %z
749  %and = and i32 %x, %or
750  %or1 = or i32 %and, %y
751  ret i32 %or1
752}
753
754define i1 @test46(i8 signext %c)  {
755; CHECK-LABEL: @test46(
756; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[C:%.*]], -33
757; CHECK-NEXT:    [[TMP2:%.*]] = add i8 [[TMP1]], -65
758; CHECK-NEXT:    [[TMP3:%.*]] = icmp ult i8 [[TMP2]], 26
759; CHECK-NEXT:    ret i1 [[TMP3]]
760;
761  %c.off = add i8 %c, -97
762  %cmp1 = icmp ult i8 %c.off, 26
763  %c.off17 = add i8 %c, -65
764  %cmp2 = icmp ult i8 %c.off17, 26
765  %or = or i1 %cmp1, %cmp2
766  ret i1 %or
767}
768
769define i1 @test46_logical(i8 signext %c)  {
770; CHECK-LABEL: @test46_logical(
771; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[C:%.*]], -33
772; CHECK-NEXT:    [[TMP2:%.*]] = add i8 [[TMP1]], -65
773; CHECK-NEXT:    [[TMP3:%.*]] = icmp ult i8 [[TMP2]], 26
774; CHECK-NEXT:    ret i1 [[TMP3]]
775;
776  %c.off = add i8 %c, -97
777  %cmp1 = icmp ult i8 %c.off, 26
778  %c.off17 = add i8 %c, -65
779  %cmp2 = icmp ult i8 %c.off17, 26
780  %or = select i1 %cmp1, i1 true, i1 %cmp2
781  ret i1 %or
782}
783
784define <2 x i1> @test46_uniform(<2 x i8> %c)  {
785; CHECK-LABEL: @test46_uniform(
786; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i8> [[C:%.*]], <i8 -33, i8 -33>
787; CHECK-NEXT:    [[TMP2:%.*]] = add <2 x i8> [[TMP1]], <i8 -65, i8 -65>
788; CHECK-NEXT:    [[TMP3:%.*]] = icmp ult <2 x i8> [[TMP2]], <i8 26, i8 26>
789; CHECK-NEXT:    ret <2 x i1> [[TMP3]]
790;
791  %c.off = add <2 x i8> %c, <i8 -97, i8 -97>
792  %cmp1 = icmp ult <2 x i8> %c.off, <i8 26, i8 26>
793  %c.off17 = add <2 x i8> %c, <i8 -65, i8 -65>
794  %cmp2 = icmp ult <2 x i8> %c.off17, <i8 26, i8 26>
795  %or = or <2 x i1> %cmp1, %cmp2
796  ret <2 x i1> %or
797}
798
799define <2 x i1> @test46_undef(<2 x i8> %c)  {
800; CHECK-LABEL: @test46_undef(
801; CHECK-NEXT:    [[C_OFF:%.*]] = add <2 x i8> [[C:%.*]], <i8 -97, i8 undef>
802; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult <2 x i8> [[C_OFF]], <i8 26, i8 undef>
803; CHECK-NEXT:    [[C_OFF17:%.*]] = add <2 x i8> [[C]], <i8 -65, i8 undef>
804; CHECK-NEXT:    [[CMP2:%.*]] = icmp ult <2 x i8> [[C_OFF17]], <i8 26, i8 undef>
805; CHECK-NEXT:    [[OR:%.*]] = or <2 x i1> [[CMP1]], [[CMP2]]
806; CHECK-NEXT:    ret <2 x i1> [[OR]]
807;
808  %c.off = add <2 x i8> %c, <i8 -97, i8 undef>
809  %cmp1 = icmp ult <2 x i8> %c.off, <i8 26, i8 undef>
810  %c.off17 = add <2 x i8> %c, <i8 -65, i8 undef>
811  %cmp2 = icmp ult <2 x i8> %c.off17, <i8 26, i8 undef>
812  %or = or <2 x i1> %cmp1, %cmp2
813  ret <2 x i1> %or
814}
815
816define i1 @test47(i8 signext %c)  {
817; CHECK-LABEL: @test47(
818; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[C:%.*]], -33
819; CHECK-NEXT:    [[TMP2:%.*]] = add i8 [[TMP1]], -65
820; CHECK-NEXT:    [[TMP3:%.*]] = icmp ult i8 [[TMP2]], 27
821; CHECK-NEXT:    ret i1 [[TMP3]]
822;
823  %c.off = add i8 %c, -65
824  %cmp1 = icmp ule i8 %c.off, 26
825  %c.off17 = add i8 %c, -97
826  %cmp2 = icmp ule i8 %c.off17, 26
827  %or = or i1 %cmp1, %cmp2
828  ret i1 %or
829}
830
831define i1 @test47_logical(i8 signext %c)  {
832; CHECK-LABEL: @test47_logical(
833; CHECK-NEXT:    [[TMP1:%.*]] = and i8 [[C:%.*]], -33
834; CHECK-NEXT:    [[TMP2:%.*]] = add i8 [[TMP1]], -65
835; CHECK-NEXT:    [[TMP3:%.*]] = icmp ult i8 [[TMP2]], 27
836; CHECK-NEXT:    ret i1 [[TMP3]]
837;
838  %c.off = add i8 %c, -65
839  %cmp1 = icmp ule i8 %c.off, 26
840  %c.off17 = add i8 %c, -97
841  %cmp2 = icmp ule i8 %c.off17, 26
842  %or = select i1 %cmp1, i1 true, i1 %cmp2
843  ret i1 %or
844}
845
846define <2 x i1> @test47_nonuniform(<2 x i8> %c)  {
847; CHECK-LABEL: @test47_nonuniform(
848; CHECK-NEXT:    [[C_OFF:%.*]] = add <2 x i8> [[C:%.*]], <i8 -65, i8 -97>
849; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult <2 x i8> [[C_OFF]], <i8 27, i8 27>
850; CHECK-NEXT:    [[C_OFF17:%.*]] = add <2 x i8> [[C]], <i8 -97, i8 -65>
851; CHECK-NEXT:    [[CMP2:%.*]] = icmp ult <2 x i8> [[C_OFF17]], <i8 27, i8 27>
852; CHECK-NEXT:    [[OR:%.*]] = or <2 x i1> [[CMP1]], [[CMP2]]
853; CHECK-NEXT:    ret <2 x i1> [[OR]]
854;
855  %c.off = add <2 x i8> %c, <i8 -65, i8 -97>
856  %cmp1 = icmp ule <2 x i8> %c.off, <i8 26, i8 26>
857  %c.off17 = add <2 x i8> %c, <i8 -97, i8 -65>
858  %cmp2 = icmp ule <2 x i8> %c.off17, <i8 26, i8 26>
859  %or = or <2 x i1> %cmp1, %cmp2
860  ret <2 x i1> %or
861}
862
863define i32 @test49(i1 %C) {
864; CHECK-LABEL: @test49(
865; CHECK-NEXT:    [[V:%.*]] = select i1 [[C:%.*]], i32 1019, i32 123
866; CHECK-NEXT:    ret i32 [[V]]
867;
868  %A = select i1 %C, i32 1000, i32 10
869  %V = or i32 %A, 123
870  ret i32 %V
871}
872
873define <2 x i32> @test49vec(i1 %C) {
874; CHECK-LABEL: @test49vec(
875; CHECK-NEXT:    [[V:%.*]] = select i1 [[C:%.*]], <2 x i32> <i32 1019, i32 1019>, <2 x i32> <i32 123, i32 123>
876; CHECK-NEXT:    ret <2 x i32> [[V]]
877;
878  %A = select i1 %C, <2 x i32> <i32 1000, i32 1000>, <2 x i32> <i32 10, i32 10>
879  %V = or <2 x i32> %A, <i32 123, i32 123>
880  ret <2 x i32> %V
881}
882
883define <2 x i32> @test49vec2(i1 %C) {
884; CHECK-LABEL: @test49vec2(
885; CHECK-NEXT:    [[V:%.*]] = select i1 [[C:%.*]], <2 x i32> <i32 1019, i32 2509>, <2 x i32> <i32 123, i32 351>
886; CHECK-NEXT:    ret <2 x i32> [[V]]
887;
888  %A = select i1 %C, <2 x i32> <i32 1000, i32 2500>, <2 x i32> <i32 10, i32 30>
889  %V = or <2 x i32> %A, <i32 123, i32 333>
890  ret <2 x i32> %V
891}
892
893define i32 @test50(i1 %which) {
894; CHECK-LABEL: @test50(
895; CHECK-NEXT:  entry:
896; CHECK-NEXT:    br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
897; CHECK:       delay:
898; CHECK-NEXT:    br label [[FINAL]]
899; CHECK:       final:
900; CHECK-NEXT:    [[A:%.*]] = phi i32 [ 1019, [[ENTRY:%.*]] ], [ 123, [[DELAY]] ]
901; CHECK-NEXT:    ret i32 [[A]]
902;
903entry:
904  br i1 %which, label %final, label %delay
905
906delay:
907  br label %final
908
909final:
910  %A = phi i32 [ 1000, %entry ], [ 10, %delay ]
911  %value = or i32 %A, 123
912  ret i32 %value
913}
914
915define <2 x i32> @test50vec(i1 %which) {
916; CHECK-LABEL: @test50vec(
917; CHECK-NEXT:  entry:
918; CHECK-NEXT:    br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
919; CHECK:       delay:
920; CHECK-NEXT:    br label [[FINAL]]
921; CHECK:       final:
922; CHECK-NEXT:    [[A:%.*]] = phi <2 x i32> [ <i32 1019, i32 1019>, [[ENTRY:%.*]] ], [ <i32 123, i32 123>, [[DELAY]] ]
923; CHECK-NEXT:    ret <2 x i32> [[A]]
924;
925entry:
926  br i1 %which, label %final, label %delay
927
928delay:
929  br label %final
930
931final:
932  %A = phi <2 x i32> [ <i32 1000, i32 1000>, %entry ], [ <i32 10, i32 10>, %delay ]
933  %value = or <2 x i32> %A, <i32 123, i32 123>
934  ret <2 x i32> %value
935}
936
937define <2 x i32> @test50vec2(i1 %which) {
938; CHECK-LABEL: @test50vec2(
939; CHECK-NEXT:  entry:
940; CHECK-NEXT:    br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
941; CHECK:       delay:
942; CHECK-NEXT:    br label [[FINAL]]
943; CHECK:       final:
944; CHECK-NEXT:    [[A:%.*]] = phi <2 x i32> [ <i32 1019, i32 2509>, [[ENTRY:%.*]] ], [ <i32 123, i32 351>, [[DELAY]] ]
945; CHECK-NEXT:    ret <2 x i32> [[A]]
946;
947entry:
948  br i1 %which, label %final, label %delay
949
950delay:
951  br label %final
952
953final:
954  %A = phi <2 x i32> [ <i32 1000, i32 2500>, %entry ], [ <i32 10, i32 30>, %delay ]
955  %value = or <2 x i32> %A, <i32 123, i32 333>
956  ret <2 x i32> %value
957}
958
959; In the next 4 tests, vary the types and predicates for extra coverage.
960; (X | (Y & ~X)) -> (X | Y), where 'not' is an inverted cmp
961
962define i1 @or_andn_cmp_1(i32 %a, i32 %b, i32 %c) {
963; CHECK-LABEL: @or_andn_cmp_1(
964; CHECK-NEXT:    [[X:%.*]] = icmp sgt i32 [[A:%.*]], [[B:%.*]]
965; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i32 [[C:%.*]], 42
966; CHECK-NEXT:    [[OR:%.*]] = or i1 [[X]], [[Y]]
967; CHECK-NEXT:    ret i1 [[OR]]
968;
969  %x = icmp sgt i32 %a, %b
970  %x_inv = icmp sle i32 %a, %b
971  %y = icmp ugt i32 %c, 42      ; thwart complexity-based ordering
972  %and = and i1 %y, %x_inv
973  %or = or i1 %x, %and
974  ret i1 %or
975}
976
977define i1 @or_andn_cmp_1_logical(i32 %a, i32 %b, i32 %c) {
978; CHECK-LABEL: @or_andn_cmp_1_logical(
979; CHECK-NEXT:    [[X:%.*]] = icmp sgt i32 [[A:%.*]], [[B:%.*]]
980; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i32 [[C:%.*]], 42
981; CHECK-NEXT:    [[OR:%.*]] = select i1 [[X]], i1 true, i1 [[Y]]
982; CHECK-NEXT:    ret i1 [[OR]]
983;
984  %x = icmp sgt i32 %a, %b
985  %x_inv = icmp sle i32 %a, %b
986  %y = icmp ugt i32 %c, 42      ; thwart complexity-based ordering
987  %and = select i1 %y, i1 %x_inv, i1 false
988  %or = select i1 %x, i1 true, i1 %and
989  ret i1 %or
990}
991
992; Commute the 'or':
993; ((Y & ~X) | X) -> (X | Y), where 'not' is an inverted cmp
994
995define <2 x i1> @or_andn_cmp_2(<2 x i32> %a, <2 x i32> %b, <2 x i32> %c) {
996; CHECK-LABEL: @or_andn_cmp_2(
997; CHECK-NEXT:    [[X:%.*]] = icmp sge <2 x i32> [[A:%.*]], [[B:%.*]]
998; CHECK-NEXT:    [[Y:%.*]] = icmp ugt <2 x i32> [[C:%.*]], <i32 42, i32 47>
999; CHECK-NEXT:    [[OR:%.*]] = or <2 x i1> [[Y]], [[X]]
1000; CHECK-NEXT:    ret <2 x i1> [[OR]]
1001;
1002  %x = icmp sge <2 x i32> %a, %b
1003  %x_inv = icmp slt <2 x i32> %a, %b
1004  %y = icmp ugt <2 x i32> %c, <i32 42, i32 47>      ; thwart complexity-based ordering
1005  %and = and <2 x i1> %y, %x_inv
1006  %or = or <2 x i1> %and, %x
1007  ret <2 x i1> %or
1008}
1009
1010; Commute the 'and':
1011; (X | (~X & Y)) -> (X | Y), where 'not' is an inverted cmp
1012
1013define i1 @or_andn_cmp_3(i72 %a, i72 %b, i72 %c) {
1014; CHECK-LABEL: @or_andn_cmp_3(
1015; CHECK-NEXT:    [[X:%.*]] = icmp ugt i72 [[A:%.*]], [[B:%.*]]
1016; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i72 [[C:%.*]], 42
1017; CHECK-NEXT:    [[OR:%.*]] = or i1 [[X]], [[Y]]
1018; CHECK-NEXT:    ret i1 [[OR]]
1019;
1020  %x = icmp ugt i72 %a, %b
1021  %x_inv = icmp ule i72 %a, %b
1022  %y = icmp ugt i72 %c, 42      ; thwart complexity-based ordering
1023  %and = and i1 %x_inv, %y
1024  %or = or i1 %x, %and
1025  ret i1 %or
1026}
1027
1028define i1 @or_andn_cmp_3_logical(i72 %a, i72 %b, i72 %c) {
1029; CHECK-LABEL: @or_andn_cmp_3_logical(
1030; CHECK-NEXT:    [[X:%.*]] = icmp ugt i72 [[A:%.*]], [[B:%.*]]
1031; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i72 [[C:%.*]], 42
1032; CHECK-NEXT:    [[OR:%.*]] = select i1 [[X]], i1 true, i1 [[Y]]
1033; CHECK-NEXT:    ret i1 [[OR]]
1034;
1035  %x = icmp ugt i72 %a, %b
1036  %x_inv = icmp ule i72 %a, %b
1037  %y = icmp ugt i72 %c, 42      ; thwart complexity-based ordering
1038  %and = select i1 %x_inv, i1 %y, i1 false
1039  %or = select i1 %x, i1 true, i1 %and
1040  ret i1 %or
1041}
1042
1043; Commute the 'or':
1044; ((~X & Y) | X) -> (X | Y), where 'not' is an inverted cmp
1045
1046define <3 x i1> @or_andn_cmp_4(<3 x i32> %a, <3 x i32> %b, <3 x i32> %c) {
1047; CHECK-LABEL: @or_andn_cmp_4(
1048; CHECK-NEXT:    [[X:%.*]] = icmp eq <3 x i32> [[A:%.*]], [[B:%.*]]
1049; CHECK-NEXT:    [[Y:%.*]] = icmp ugt <3 x i32> [[C:%.*]], <i32 42, i32 43, i32 -1>
1050; CHECK-NEXT:    [[OR:%.*]] = or <3 x i1> [[Y]], [[X]]
1051; CHECK-NEXT:    ret <3 x i1> [[OR]]
1052;
1053  %x = icmp eq <3 x i32> %a, %b
1054  %x_inv = icmp ne <3 x i32> %a, %b
1055  %y = icmp ugt <3 x i32> %c, <i32 42, i32 43, i32 -1>      ; thwart complexity-based ordering
1056  %and = and <3 x i1> %x_inv, %y
1057  %or = or <3 x i1> %and, %x
1058  ret <3 x i1> %or
1059}
1060
1061; In the next 4 tests, vary the types and predicates for extra coverage.
1062; (~X | (Y & X)) -> (~X | Y), where 'not' is an inverted cmp
1063
1064define i1 @orn_and_cmp_1(i37 %a, i37 %b, i37 %c) {
1065; CHECK-LABEL: @orn_and_cmp_1(
1066; CHECK-NEXT:    [[X_INV:%.*]] = icmp sle i37 [[A:%.*]], [[B:%.*]]
1067; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i37 [[C:%.*]], 42
1068; CHECK-NEXT:    [[OR:%.*]] = or i1 [[X_INV]], [[Y]]
1069; CHECK-NEXT:    ret i1 [[OR]]
1070;
1071  %x = icmp sgt i37 %a, %b
1072  %x_inv = icmp sle i37 %a, %b
1073  %y = icmp ugt i37 %c, 42      ; thwart complexity-based ordering
1074  %and = and i1 %y, %x
1075  %or = or i1 %x_inv, %and
1076  ret i1 %or
1077}
1078
1079define i1 @orn_and_cmp_1_logical(i37 %a, i37 %b, i37 %c) {
1080; CHECK-LABEL: @orn_and_cmp_1_logical(
1081; CHECK-NEXT:    [[X_INV:%.*]] = icmp sle i37 [[A:%.*]], [[B:%.*]]
1082; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i37 [[C:%.*]], 42
1083; CHECK-NEXT:    [[OR:%.*]] = select i1 [[X_INV]], i1 true, i1 [[Y]]
1084; CHECK-NEXT:    ret i1 [[OR]]
1085;
1086  %x = icmp sgt i37 %a, %b
1087  %x_inv = icmp sle i37 %a, %b
1088  %y = icmp ugt i37 %c, 42      ; thwart complexity-based ordering
1089  %and = select i1 %y, i1 %x, i1 false
1090  %or = select i1 %x_inv, i1 true, i1 %and
1091  ret i1 %or
1092}
1093
1094; Commute the 'or':
1095; ((Y & X) | ~X) -> (~X | Y), where 'not' is an inverted cmp
1096
1097define i1 @orn_and_cmp_2(i16 %a, i16 %b, i16 %c) {
1098; CHECK-LABEL: @orn_and_cmp_2(
1099; CHECK-NEXT:    [[X_INV:%.*]] = icmp slt i16 [[A:%.*]], [[B:%.*]]
1100; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i16 [[C:%.*]], 42
1101; CHECK-NEXT:    [[OR:%.*]] = or i1 [[Y]], [[X_INV]]
1102; CHECK-NEXT:    ret i1 [[OR]]
1103;
1104  %x = icmp sge i16 %a, %b
1105  %x_inv = icmp slt i16 %a, %b
1106  %y = icmp ugt i16 %c, 42      ; thwart complexity-based ordering
1107  %and = and i1 %y, %x
1108  %or = or i1 %and, %x_inv
1109  ret i1 %or
1110}
1111
1112define i1 @orn_and_cmp_2_logical(i16 %a, i16 %b, i16 %c) {
1113; CHECK-LABEL: @orn_and_cmp_2_logical(
1114; CHECK-NEXT:    [[X_INV:%.*]] = icmp slt i16 [[A:%.*]], [[B:%.*]]
1115; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i16 [[C:%.*]], 42
1116; CHECK-NEXT:    [[OR:%.*]] = select i1 [[Y]], i1 true, i1 [[X_INV]]
1117; CHECK-NEXT:    ret i1 [[OR]]
1118;
1119  %x = icmp sge i16 %a, %b
1120  %x_inv = icmp slt i16 %a, %b
1121  %y = icmp ugt i16 %c, 42      ; thwart complexity-based ordering
1122  %and = select i1 %y, i1 %x, i1 false
1123  %or = select i1 %and, i1 true, i1 %x_inv
1124  ret i1 %or
1125}
1126
1127; Commute the 'and':
1128; (~X | (X & Y)) -> (~X | Y), where 'not' is an inverted cmp
1129
1130define <4 x i1> @orn_and_cmp_3(<4 x i32> %a, <4 x i32> %b, <4 x i32> %c) {
1131; CHECK-LABEL: @orn_and_cmp_3(
1132; CHECK-NEXT:    [[X_INV:%.*]] = icmp ule <4 x i32> [[A:%.*]], [[B:%.*]]
1133; CHECK-NEXT:    [[Y:%.*]] = icmp ugt <4 x i32> [[C:%.*]], <i32 42, i32 0, i32 1, i32 -1>
1134; CHECK-NEXT:    [[OR:%.*]] = or <4 x i1> [[X_INV]], [[Y]]
1135; CHECK-NEXT:    ret <4 x i1> [[OR]]
1136;
1137  %x = icmp ugt <4 x i32> %a, %b
1138  %x_inv = icmp ule <4 x i32> %a, %b
1139  %y = icmp ugt <4 x i32> %c, <i32 42, i32 0, i32 1, i32 -1>      ; thwart complexity-based ordering
1140  %and = and <4 x i1> %x, %y
1141  %or = or <4 x i1> %x_inv, %and
1142  ret <4 x i1> %or
1143}
1144
1145; Commute the 'or':
1146; ((X & Y) | ~X) -> (~X | Y), where 'not' is an inverted cmp
1147
1148define i1 @orn_and_cmp_4(i32 %a, i32 %b, i32 %c) {
1149; CHECK-LABEL: @orn_and_cmp_4(
1150; CHECK-NEXT:    [[X_INV:%.*]] = icmp ne i32 [[A:%.*]], [[B:%.*]]
1151; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i32 [[C:%.*]], 42
1152; CHECK-NEXT:    [[OR:%.*]] = or i1 [[Y]], [[X_INV]]
1153; CHECK-NEXT:    ret i1 [[OR]]
1154;
1155  %x = icmp eq i32 %a, %b
1156  %x_inv = icmp ne i32 %a, %b
1157  %y = icmp ugt i32 %c, 42      ; thwart complexity-based ordering
1158  %and = and i1 %x, %y
1159  %or = or i1 %and, %x_inv
1160  ret i1 %or
1161}
1162
1163define i1 @orn_and_cmp_4_logical(i32 %a, i32 %b, i32 %c) {
1164; CHECK-LABEL: @orn_and_cmp_4_logical(
1165; CHECK-NEXT:    [[X_INV:%.*]] = icmp ne i32 [[A:%.*]], [[B:%.*]]
1166; CHECK-NEXT:    [[Y:%.*]] = icmp ugt i32 [[C:%.*]], 42
1167; CHECK-NEXT:    [[OR:%.*]] = select i1 [[X_INV]], i1 true, i1 [[Y]]
1168; CHECK-NEXT:    ret i1 [[OR]]
1169;
1170  %x = icmp eq i32 %a, %b
1171  %x_inv = icmp ne i32 %a, %b
1172  %y = icmp ugt i32 %c, 42      ; thwart complexity-based ordering
1173  %and = select i1 %x, i1 %y, i1 false
1174  %or = select i1 %and, i1 true, i1 %x_inv
1175  ret i1 %or
1176}
1177
1178; The constant vectors are inverses. Make sure we can turn this into a select without crashing trying to truncate the constant to 16xi1.
1179define <16 x i1> @test51(<16 x i1> %arg, <16 x i1> %arg1) {
1180; CHECK-LABEL: @test51(
1181; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <16 x i1> [[ARG:%.*]], <16 x i1> [[ARG1:%.*]], <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 20, i32 5, i32 6, i32 23, i32 24, i32 9, i32 10, i32 27, i32 28, i32 29, i32 30, i32 31>
1182; CHECK-NEXT:    ret <16 x i1> [[TMP1]]
1183;
1184  %tmp = and <16 x i1> %arg, <i1 true, i1 true, i1 true, i1 true, i1 false, i1 true, i1 true, i1 false, i1 false, i1 true, i1 true, i1 false, i1 false, i1 false, i1 false, i1 false>
1185  %tmp2 = and <16 x i1> %arg1, <i1 false, i1 false, i1 false, i1 false, i1 true, i1 false, i1 false, i1 true, i1 true, i1 false, i1 false, i1 true, i1 true, i1 true, i1 true, i1 true>
1186  %tmp3 = or <16 x i1> %tmp, %tmp2
1187  ret <16 x i1> %tmp3
1188}
1189
1190; This would infinite loop because it reaches a transform
1191; that was not expecting a constant-foldable value.
1192
1193define i32 @PR46712(i1 %x, i1 %y, i1 %b, i64 %z) {
1194; CHECK-LABEL: @PR46712(
1195; CHECK-NEXT:  entry:
1196; CHECK-NEXT:    br i1 [[B:%.*]], label [[TRUE:%.*]], label [[END:%.*]]
1197; CHECK:       true:
1198; CHECK-NEXT:    [[BOOL5:%.*]] = icmp eq i64 [[Z:%.*]], 0
1199; CHECK-NEXT:    [[SEL:%.*]] = zext i1 [[BOOL5]] to i32
1200; CHECK-NEXT:    br label [[END]]
1201; CHECK:       end:
1202; CHECK-NEXT:    [[T5:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[SEL]], [[TRUE]] ]
1203; CHECK-NEXT:    ret i32 [[T5]]
1204;
1205entry:
1206  %t2 = or i1 %x, %y
1207  %conv = sext i1 %t2 to i32
1208  %cmp = icmp sge i32 %conv, 1
1209  %conv2 = zext i1 %cmp to i64
1210  br i1 %b, label %true, label %end
1211
1212true:
1213  %bool4 = icmp eq i64 %conv2, 0
1214  %bool5 = icmp ne i64 %z, 0
1215  %and = and i1 %bool4, %bool5
1216  %sel = select i1 %and, i1 false, i1 true
1217  br label %end
1218
1219end:
1220  %t5 = phi i1 [ 0, %entry ], [ %sel, %true ]
1221  %conv8 = zext i1 %t5 to i32
1222  ret i32 %conv8
1223}
1224
1225define i32 @PR46712_logical(i1 %x, i1 %y, i1 %b, i64 %z) {
1226; CHECK-LABEL: @PR46712_logical(
1227; CHECK-NEXT:  entry:
1228; CHECK-NEXT:    br i1 [[B:%.*]], label [[TRUE:%.*]], label [[END:%.*]]
1229; CHECK:       true:
1230; CHECK-NEXT:    [[BOOL5:%.*]] = icmp eq i64 [[Z:%.*]], 0
1231; CHECK-NEXT:    [[SEL:%.*]] = zext i1 [[BOOL5]] to i32
1232; CHECK-NEXT:    br label [[END]]
1233; CHECK:       end:
1234; CHECK-NEXT:    [[T5:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[SEL]], [[TRUE]] ]
1235; CHECK-NEXT:    ret i32 [[T5]]
1236;
1237entry:
1238  %t2 = select i1 %x, i1 true, i1 %y
1239  %conv = sext i1 %t2 to i32
1240  %cmp = icmp sge i32 %conv, 1
1241  %conv2 = zext i1 %cmp to i64
1242  br i1 %b, label %true, label %end
1243
1244true:
1245  %bool4 = icmp eq i64 %conv2, 0
1246  %bool5 = icmp ne i64 %z, 0
1247  %and = select i1 %bool4, i1 %bool5, i1 false
1248  %sel = select i1 %and, i1 false, i1 true
1249  br label %end
1250
1251end:
1252  %t5 = phi i1 [ 0, %entry ], [ %sel, %true ]
1253  %conv8 = zext i1 %t5 to i32
1254  ret i32 %conv8
1255}
1256
1257; (~x & y) | ~(x | y) --> ~x
1258define i32 @PR38929(i32 %0, i32 %1) {
1259; CHECK-LABEL: @PR38929(
1260; CHECK-NEXT:    [[TMP3:%.*]] = xor i32 [[TMP0:%.*]], -1
1261; CHECK-NEXT:    ret i32 [[TMP3]]
1262;
1263  %3 = xor i32 %0, -1
1264  %4 = and i32 %3, %1
1265  %5 = or i32 %1, %0
1266  %6 = xor i32 %5, -1
1267  %7 = or i32 %4, %6
1268  ret i32 %7
1269}
1270
1271define i32 @test1(i32 %x, i32 %y) {
1272; CHECK-LABEL: @test1(
1273; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[Y:%.*]], [[X:%.*]]
1274; CHECK-NEXT:    [[OR1:%.*]] = xor i32 [[TMP1]], -1
1275; CHECK-NEXT:    ret i32 [[OR1]]
1276;
1277  %xor = xor i32 %y, %x
1278  %or = or i32 %y, %x
1279  %neg = xor i32 %or, -1
1280  %or1 = or i32 %xor, %neg
1281  ret i32 %or1
1282}
1283
1284define i32 @test2(i32 %x, i32 %y) {
1285; CHECK-LABEL: @test2(
1286; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[Y:%.*]], [[X:%.*]]
1287; CHECK-NEXT:    [[OR1:%.*]] = xor i32 [[TMP1]], -1
1288; CHECK-NEXT:    ret i32 [[OR1]]
1289;
1290  %or = or i32 %x, %y
1291  %neg = xor i32 %or, -1
1292  %xor = xor i32 %y, %x
1293  %or1 = or i32 %xor, %neg
1294  ret i32 %or1
1295}
1296
1297define i32 @test3(i32 %x, i32 %y) {
1298; CHECK-LABEL: @test3(
1299; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
1300; CHECK-NEXT:    [[OR1:%.*]] = xor i32 [[TMP1]], -1
1301; CHECK-NEXT:    ret i32 [[OR1]]
1302;
1303  %or = or i32 %y, %x
1304  %neg = xor i32 %or, -1
1305  %xor = xor i32 %x, %y
1306  %or1 = or i32 %xor, %neg
1307  ret i32 %or1
1308}
1309
1310define <2 x i32> @test4_vec(<2 x i32> %x, <2 x i32> %y) {
1311; CHECK-LABEL: @test4_vec(
1312; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i32> [[Y:%.*]], [[X:%.*]]
1313; CHECK-NEXT:    [[OR1:%.*]] = xor <2 x i32> [[TMP1]], <i32 -1, i32 -1>
1314; CHECK-NEXT:    ret <2 x i32> [[OR1]]
1315;
1316  %or = or <2 x i32> %y, %x
1317  %neg = xor <2 x i32> %or, <i32 -1, i32 -1>
1318  %xor = xor <2 x i32> %y, %x
1319  %or1 = or <2 x i32> %xor, %neg
1320  ret <2 x i32> %or1
1321}
1322
1323define i32 @test5_use(i32 %x, i32 %y) {
1324; CHECK-LABEL: @test5_use(
1325; CHECK-NEXT:    [[OR:%.*]] = or i32 [[Y:%.*]], [[X:%.*]]
1326; CHECK-NEXT:    [[NEG:%.*]] = xor i32 [[OR]], -1
1327; CHECK-NEXT:    call void @use(i32 [[NEG]])
1328; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[Y]], [[X]]
1329; CHECK-NEXT:    [[OR1:%.*]] = xor i32 [[TMP1]], -1
1330; CHECK-NEXT:    ret i32 [[OR1]]
1331;
1332  %or = or i32 %y, %x
1333  %neg = xor i32 %or, -1
1334  %xor = xor i32 %y, %x
1335  call void @use(i32 %neg)
1336  %or1 = or i32 %xor, %neg
1337  ret i32 %or1
1338}
1339
1340define i32 @test5_use2(i32 %x, i32 %y) {
1341; CHECK-LABEL: @test5_use2(
1342; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[Y:%.*]], [[X:%.*]]
1343; CHECK-NEXT:    call void @use(i32 [[XOR]])
1344; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[Y]], [[X]]
1345; CHECK-NEXT:    [[OR1:%.*]] = xor i32 [[TMP1]], -1
1346; CHECK-NEXT:    ret i32 [[OR1]]
1347;
1348  %or = or i32 %y, %x
1349  %neg = xor i32 %or, -1
1350  %xor = xor i32 %y, %x
1351  call void @use(i32 %xor)
1352  %or1 = or i32 %xor, %neg
1353  ret i32 %or1
1354}
1355define i32 @test5_use3(i32 %x, i32 %y) {
1356; CHECK-LABEL: @test5_use3(
1357; CHECK-NEXT:    [[OR:%.*]] = or i32 [[Y:%.*]], [[X:%.*]]
1358; CHECK-NEXT:    [[NEG:%.*]] = xor i32 [[OR]], -1
1359; CHECK-NEXT:    call void @use(i32 [[NEG]])
1360; CHECK-NEXT:    [[XOR:%.*]] = xor i32 [[Y]], [[X]]
1361; CHECK-NEXT:    call void @use(i32 [[XOR]])
1362; CHECK-NEXT:    [[OR1:%.*]] = or i32 [[XOR]], [[NEG]]
1363; CHECK-NEXT:    ret i32 [[OR1]]
1364;
1365  %or = or i32 %y, %x
1366  %neg = xor i32 %or, -1
1367  call void @use(i32 %neg)
1368  %xor = xor i32 %y, %x
1369  call void @use(i32 %xor)
1370  %or1 = or i32 %xor, %neg
1371  ret i32 %or1
1372}
1373
1374define i8 @ashr_bitwidth_mask(i8 %x, i8 %y) {
1375; CHECK-LABEL: @ashr_bitwidth_mask(
1376; CHECK-NEXT:    [[SIGN:%.*]] = ashr i8 [[X:%.*]], 7
1377; CHECK-NEXT:    [[R:%.*]] = or i8 [[SIGN]], [[Y:%.*]]
1378; CHECK-NEXT:    ret i8 [[R]]
1379;
1380  %sign = ashr i8 %x, 7
1381  %r = or i8 %sign, %y
1382  ret i8 %r
1383}
1384
1385define <2 x i8> @ashr_bitwidth_mask_vec_commute(<2 x i8> %x, <2 x i8> %py) {
1386; CHECK-LABEL: @ashr_bitwidth_mask_vec_commute(
1387; CHECK-NEXT:    [[Y:%.*]] = mul <2 x i8> [[PY:%.*]], <i8 42, i8 2>
1388; CHECK-NEXT:    [[SIGN:%.*]] = ashr <2 x i8> [[X:%.*]], <i8 7, i8 7>
1389; CHECK-NEXT:    [[R:%.*]] = or <2 x i8> [[Y]], [[SIGN]]
1390; CHECK-NEXT:    ret <2 x i8> [[R]]
1391;
1392  %y = mul <2 x i8> %py, <i8 42, i8 2>      ; thwart complexity-based ordering
1393  %sign = ashr <2 x i8> %x, <i8 7, i8 7>
1394  %r = or <2 x i8> %y, %sign
1395  ret <2 x i8> %r
1396}
1397
1398define i32 @ashr_bitwidth_mask_use(i32 %x, i32 %y) {
1399; CHECK-LABEL: @ashr_bitwidth_mask_use(
1400; CHECK-NEXT:    [[SIGN:%.*]] = ashr i32 [[X:%.*]], 7
1401; CHECK-NEXT:    call void @use(i32 [[SIGN]])
1402; CHECK-NEXT:    [[R:%.*]] = or i32 [[SIGN]], [[Y:%.*]]
1403; CHECK-NEXT:    ret i32 [[R]]
1404;
1405  %sign = ashr i32 %x, 7
1406  call void @use(i32 %sign)
1407  %r = or i32 %sign, %y
1408  ret i32 %r
1409}
1410
1411define i8 @ashr_not_bitwidth_mask(i8 %x, i8 %y) {
1412; CHECK-LABEL: @ashr_not_bitwidth_mask(
1413; CHECK-NEXT:    [[SIGN:%.*]] = ashr i8 [[X:%.*]], 6
1414; CHECK-NEXT:    [[R:%.*]] = or i8 [[SIGN]], [[Y:%.*]]
1415; CHECK-NEXT:    ret i8 [[R]]
1416;
1417  %sign = ashr i8 %x, 6
1418  %r = or i8 %sign, %y
1419  ret i8 %r
1420}
1421
1422define i8 @lshr_bitwidth_mask(i8 %x, i8 %y) {
1423; CHECK-LABEL: @lshr_bitwidth_mask(
1424; CHECK-NEXT:    [[SIGN:%.*]] = lshr i8 [[X:%.*]], 7
1425; CHECK-NEXT:    [[R:%.*]] = or i8 [[SIGN]], [[Y:%.*]]
1426; CHECK-NEXT:    ret i8 [[R]]
1427;
1428  %sign = lshr i8 %x, 7
1429  %r = or i8 %sign, %y
1430  ret i8 %r
1431}
1432
1433define i1 @cmp_overlap(i32 %x) {
1434; CHECK-LABEL: @cmp_overlap(
1435; CHECK-NEXT:    [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 1
1436; CHECK-NEXT:    ret i1 [[TMP1]]
1437;
1438  %isneg = icmp slt i32 %x, 0
1439  %negx = sub i32 0, %x
1440  %isnotneg = icmp sgt i32 %negx, -1
1441  %r = or i1 %isneg, %isnotneg
1442  ret i1 %r
1443}
1444
1445define <2 x i1> @cmp_overlap_splat(<2 x i5> %x) {
1446; CHECK-LABEL: @cmp_overlap_splat(
1447; CHECK-NEXT:    [[TMP1:%.*]] = icmp slt <2 x i5> [[X:%.*]], <i5 1, i5 1>
1448; CHECK-NEXT:    ret <2 x i1> [[TMP1]]
1449;
1450  %isneg = icmp slt <2 x i5> %x, zeroinitializer
1451  %negx = sub <2 x i5> zeroinitializer, %x
1452  %isnotneg = icmp sgt <2 x i5> %negx, <i5 -1, i5 -1>
1453  %r = or <2 x i1> %isneg, %isnotneg
1454  ret <2 x i1> %r
1455}
1456
1457define i32 @mul_no_common_bits(i32 %p1, i32 %p2) {
1458; CHECK-LABEL: @mul_no_common_bits(
1459; CHECK-NEXT:    [[X:%.*]] = and i32 [[P1:%.*]], 7
1460; CHECK-NEXT:    [[Y:%.*]] = shl i32 [[P2:%.*]], 3
1461; CHECK-NEXT:    [[TMP1:%.*]] = or i32 [[Y]], 1
1462; CHECK-NEXT:    [[R:%.*]] = mul i32 [[X]], [[TMP1]]
1463; CHECK-NEXT:    ret i32 [[R]]
1464;
1465  %x = and i32 %p1, 7
1466  %y = shl i32 %p2, 3
1467  %m = mul i32 %x, %y
1468  %r = or i32 %m, %x
1469  ret i32 %r
1470}
1471
1472define i32 @mul_no_common_bits_const_op(i32 %p) {
1473; CHECK-LABEL: @mul_no_common_bits_const_op(
1474; CHECK-NEXT:    [[X:%.*]] = and i32 [[P:%.*]], 7
1475; CHECK-NEXT:    [[R:%.*]] = mul nuw nsw i32 [[X]], 25
1476; CHECK-NEXT:    ret i32 [[R]]
1477;
1478  %x = and i32 %p, 7
1479  %m = mul i32 %x, 24
1480  %r = or i32 %m, %x
1481  ret i32 %r
1482}
1483
1484define <2 x i12> @mul_no_common_bits_commute(<2 x i12> %p) {
1485; CHECK-LABEL: @mul_no_common_bits_commute(
1486; CHECK-NEXT:    [[X:%.*]] = and <2 x i12> [[P:%.*]], <i12 1, i12 1>
1487; CHECK-NEXT:    [[R:%.*]] = mul nuw nsw <2 x i12> [[X]], <i12 15, i12 17>
1488; CHECK-NEXT:    ret <2 x i12> [[R]]
1489;
1490  %x = and <2 x i12> %p, <i12 1, i12 1>
1491  %m = mul <2 x i12> %x, <i12 14, i12 16>
1492  %r = or <2 x i12> %x, %m
1493  ret <2 x i12> %r
1494}
1495
1496; negative test - extra use requires extra instructions
1497
1498define i32 @mul_no_common_bits_uses(i32 %p1, i32 %p2) {
1499; CHECK-LABEL: @mul_no_common_bits_uses(
1500; CHECK-NEXT:    [[X:%.*]] = and i32 [[P1:%.*]], 7
1501; CHECK-NEXT:    [[Y:%.*]] = shl i32 [[P2:%.*]], 3
1502; CHECK-NEXT:    [[M:%.*]] = mul i32 [[X]], [[Y]]
1503; CHECK-NEXT:    call void @use(i32 [[M]])
1504; CHECK-NEXT:    [[R:%.*]] = or i32 [[M]], [[X]]
1505; CHECK-NEXT:    ret i32 [[R]]
1506;
1507  %x = and i32 %p1, 7
1508  %y = shl i32 %p2, 3
1509  %m = mul i32 %x, %y
1510  call void @use(i32 %m)
1511  %r = or i32 %m, %x
1512  ret i32 %r
1513}
1514
1515; negative test - probably not good to create an extra mul
1516
1517define i32 @mul_no_common_bits_const_op_uses(i32 %p) {
1518; CHECK-LABEL: @mul_no_common_bits_const_op_uses(
1519; CHECK-NEXT:    [[X:%.*]] = and i32 [[P:%.*]], 7
1520; CHECK-NEXT:    [[M:%.*]] = mul nuw nsw i32 [[X]], 24
1521; CHECK-NEXT:    call void @use(i32 [[M]])
1522; CHECK-NEXT:    [[R:%.*]] = or i32 [[M]], [[X]]
1523; CHECK-NEXT:    ret i32 [[R]]
1524;
1525  %x = and i32 %p, 7
1526  %m = mul i32 %x, 24
1527  call void @use(i32 %m)
1528  %r = or i32 %m, %x
1529  ret i32 %r
1530}
1531
1532; negative test - %x and %m may have set 3rd bit
1533
1534define i32 @mul_common_bits(i32 %p) {
1535; CHECK-LABEL: @mul_common_bits(
1536; CHECK-NEXT:    [[X:%.*]] = and i32 [[P:%.*]], 7
1537; CHECK-NEXT:    [[M:%.*]] = mul nuw nsw i32 [[X]], 12
1538; CHECK-NEXT:    [[R:%.*]] = or i32 [[M]], [[X]]
1539; CHECK-NEXT:    ret i32 [[R]]
1540;
1541  %x = and i32 %p, 7
1542  %m = mul i32 %x, 12
1543  %r = or i32 %m, %x
1544  ret i32 %r
1545}
1546