1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instcombine -S | FileCheck %s
3
4define <4 x i32> @lshr_non_splat_vector(<4 x i32> %A) {
5; CHECK-LABEL: @lshr_non_splat_vector(
6; CHECK-NEXT:    [[B:%.*]] = lshr <4 x i32> [[A:%.*]], <i32 32, i32 1, i32 2, i32 3>
7; CHECK-NEXT:    ret <4 x i32> [[B]]
8;
9  %B = lshr <4 x i32> %A, <i32 32, i32 1, i32 2, i32 3>
10  ret <4 x i32> %B
11}
12
13define <4 x i32> @shl_non_splat_vector(<4 x i32> %A) {
14; CHECK-LABEL: @shl_non_splat_vector(
15; CHECK-NEXT:    [[B:%.*]] = shl <4 x i32> [[A:%.*]], <i32 32, i32 1, i32 2, i32 3>
16; CHECK-NEXT:    ret <4 x i32> [[B]]
17;
18  %B = shl <4 x i32> %A, <i32 32, i32 1, i32 2, i32 3>
19  ret <4 x i32> %B
20}
21
22define i32 @test6(i32 %A) {
23; CHECK-LABEL: @test6(
24; CHECK-NEXT:    [[C:%.*]] = mul i32 [[A:%.*]], 6
25; CHECK-NEXT:    ret i32 [[C]]
26;
27  %B = shl i32 %A, 1      ;; convert to an mul instruction
28  %C = mul i32 %B, 3
29  ret i32 %C
30}
31
32define i32 @test6a(i32 %A) {
33; CHECK-LABEL: @test6a(
34; CHECK-NEXT:    [[C:%.*]] = mul i32 [[A:%.*]], 6
35; CHECK-NEXT:    ret i32 [[C]]
36;
37  %B = mul i32 %A, 3
38  %C = shl i32 %B, 1      ;; convert to an mul instruction
39  ret i32 %C
40}
41
42;; (A << 5) << 3 === A << 8 == 0
43define i8 @test8(i8 %A) {
44; CHECK-LABEL: @test8(
45; CHECK-NEXT:    ret i8 0
46;
47  %B = shl i8 %A, 5
48  %C = shl i8 %B, 3
49  ret i8 %C
50}
51
52;; (A << 7) >> 7 === A & 1
53define i8 @test9(i8 %A) {
54; CHECK-LABEL: @test9(
55; CHECK-NEXT:    [[B:%.*]] = and i8 [[A:%.*]], 1
56; CHECK-NEXT:    ret i8 [[B]]
57;
58  %B = shl i8 %A, 7
59  %C = lshr i8 %B, 7
60  ret i8 %C
61}
62
63;; (A >> 7) << 7 === A & 128
64
65define i8 @test10(i8 %A) {
66; CHECK-LABEL: @test10(
67; CHECK-NEXT:    [[B:%.*]] = and i8 [[A:%.*]], -128
68; CHECK-NEXT:    ret i8 [[B]]
69;
70  %B = lshr i8 %A, 7
71  %C = shl i8 %B, 7
72  ret i8 %C
73}
74
75;; Allow the simplification when the lshr shift is exact.
76define i8 @test10a(i8 %A) {
77; CHECK-LABEL: @test10a(
78; CHECK-NEXT:    ret i8 [[A:%.*]]
79;
80  %B = lshr exact i8 %A, 7
81  %C = shl i8 %B, 7
82  ret i8 %C
83}
84
85;; (A >> 3) << 4 === (A & 0x1F) << 1
86define i8 @test11(i8 %x) {
87; CHECK-LABEL: @test11(
88; CHECK-NEXT:    [[TMP1:%.*]] = mul i8 [[X:%.*]], 6
89; CHECK-NEXT:    [[C:%.*]] = and i8 [[TMP1]], -16
90; CHECK-NEXT:    ret i8 [[C]]
91;
92  %a = mul i8 %x, 3
93  %B = lshr i8 %a, 3
94  %C = shl i8 %B, 4
95  ret i8 %C
96}
97
98;; Allow the simplification in InstCombine when the lshr shift is exact.
99define i8 @test11a(i8 %A) {
100; CHECK-LABEL: @test11a(
101; CHECK-NEXT:    [[C:%.*]] = mul i8 [[A:%.*]], 6
102; CHECK-NEXT:    ret i8 [[C]]
103;
104  %a = mul i8 %A, 3
105  %B = lshr exact i8 %a, 3
106  %C = shl i8 %B, 4
107  ret i8 %C
108}
109
110;; (A >> 8) << 8 === A & -256
111define i32 @test12(i32 %A) {
112; CHECK-LABEL: @test12(
113; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[A:%.*]], -256
114; CHECK-NEXT:    ret i32 [[TMP1]]
115;
116  %B = ashr i32 %A, 8
117  %C = shl i32 %B, 8
118  ret i32 %C
119}
120
121;; ((A >>s 6) << 6 === (A & FFFFFFC0)
122define i8 @shishi(i8 %x) {
123; CHECK-LABEL: @shishi(
124; CHECK-NEXT:    [[A:%.*]] = ashr i8 [[X:%.*]], 6
125; CHECK-NEXT:    [[B:%.*]] = and i8 [[X]], -64
126; CHECK-NEXT:    [[EXTRA_USE_OF_A:%.*]] = mul nsw i8 [[A]], 5
127; CHECK-NEXT:    [[R:%.*]] = sdiv i8 [[EXTRA_USE_OF_A]], [[B]]
128; CHECK-NEXT:    ret i8 [[R]]
129;
130  %a = ashr i8 %x, 6
131  %b = shl i8 %a, 6
132  %extra_use_of_a = mul i8 %a, 5
133  %r = sdiv i8 %extra_use_of_a, %b
134  ret i8 %r
135}
136
137;; (A >> 3) << 4 === (A & -8) * 2
138define i8 @test13(i8 %x) {
139; CHECK-LABEL: @test13(
140; CHECK-NEXT:    [[TMP1:%.*]] = mul i8 [[X:%.*]], 6
141; CHECK-NEXT:    [[C:%.*]] = and i8 [[TMP1]], -16
142; CHECK-NEXT:    ret i8 [[C]]
143;
144  %a = mul i8 %x, 3
145  %B = ashr i8 %a, 3
146  %C = shl i8 %B, 4
147  ret i8 %C
148}
149
150define i8 @test13a(i8 %A) {
151; CHECK-LABEL: @test13a(
152; CHECK-NEXT:    [[C:%.*]] = mul i8 [[A:%.*]], 6
153; CHECK-NEXT:    ret i8 [[C]]
154;
155  %a = mul i8 %A, 3
156  %B = ashr exact i8 %a, 3
157  %C = shl i8 %B, 4
158  ret i8 %C
159}
160
161;; D = ((B | 1234) << 4) === ((B << 4)|(1234 << 4)
162define i32 @test14(i32 %A) {
163; CHECK-LABEL: @test14(
164; CHECK-NEXT:    [[B:%.*]] = and i32 [[A:%.*]], -19760
165; CHECK-NEXT:    [[C:%.*]] = or i32 [[B]], 19744
166; CHECK-NEXT:    ret i32 [[C]]
167;
168  %B = lshr i32 %A, 4
169  %C = or i32 %B, 1234
170  %D = shl i32 %C, 4
171  ret i32 %D
172}
173
174;; D = ((B | 1234) << 4) === ((B << 4)|(1234 << 4)
175define i32 @test14a(i32 %A) {
176; CHECK-LABEL: @test14a(
177; CHECK-NEXT:    [[C:%.*]] = and i32 [[A:%.*]], 77
178; CHECK-NEXT:    ret i32 [[C]]
179;
180  %B = shl i32 %A, 4
181  %C = and i32 %B, 1234
182  %D = lshr i32 %C, 4
183  ret i32 %D
184}
185
186define i32 @test15(i1 %C) {
187; CHECK-LABEL: @test15(
188; CHECK-NEXT:    [[A:%.*]] = select i1 [[C:%.*]], i32 12, i32 4
189; CHECK-NEXT:    ret i32 [[A]]
190;
191  %A = select i1 %C, i32 3, i32 1
192  %V = shl i32 %A, 2
193  ret i32 %V
194}
195
196define i32 @test15a(i1 %C) {
197; CHECK-LABEL: @test15a(
198; CHECK-NEXT:    [[V:%.*]] = select i1 [[C:%.*]], i32 512, i32 128
199; CHECK-NEXT:    ret i32 [[V]]
200;
201  %A = select i1 %C, i8 3, i8 1
202  %shift.upgrd.4 = zext i8 %A to i32
203  %V = shl i32 64, %shift.upgrd.4
204  ret i32 %V
205}
206
207define i1 @test16(i32 %X) {
208; CHECK-LABEL: @test16(
209; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 16
210; CHECK-NEXT:    [[I_7:%.*]] = icmp ne i32 [[TMP1]], 0
211; CHECK-NEXT:    ret i1 [[I_7]]
212;
213  %i.3 = ashr i32 %X, 4
214  %i.6 = and i32 %i.3, 1
215  %i.7 = icmp ne i32 %i.6, 0
216  ret i1 %i.7
217}
218
219define i1 @test17(i32 %A) {
220; CHECK-LABEL: @test17(
221; CHECK-NEXT:    [[B_MASK:%.*]] = and i32 [[A:%.*]], -8
222; CHECK-NEXT:    [[C:%.*]] = icmp eq i32 [[B_MASK]], 9872
223; CHECK-NEXT:    ret i1 [[C]]
224;
225  %B = lshr i32 %A, 3
226  %C = icmp eq i32 %B, 1234
227  ret i1 %C
228}
229
230define <2 x i1> @test17vec(<2 x i32> %A) {
231; CHECK-LABEL: @test17vec(
232; CHECK-NEXT:    [[B_MASK:%.*]] = and <2 x i32> [[A:%.*]], <i32 -8, i32 -8>
233; CHECK-NEXT:    [[C:%.*]] = icmp eq <2 x i32> [[B_MASK]], <i32 9872, i32 9872>
234; CHECK-NEXT:    ret <2 x i1> [[C]]
235;
236  %B = lshr <2 x i32> %A, <i32 3, i32 3>
237  %C = icmp eq <2 x i32> %B, <i32 1234, i32 1234>
238  ret <2 x i1> %C
239}
240
241define i1 @test18(i8 %A) {
242; CHECK-LABEL: @test18(
243; CHECK-NEXT:    ret i1 false
244;
245  %B = lshr i8 %A, 7
246  ;; false
247  %C = icmp eq i8 %B, 123
248  ret i1 %C
249}
250
251define i1 @test19(i32 %A) {
252; CHECK-LABEL: @test19(
253; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[A:%.*]], 4
254; CHECK-NEXT:    ret i1 [[C]]
255;
256  %B = ashr i32 %A, 2
257  ;; (X & -4) == 0
258  %C = icmp eq i32 %B, 0
259  ret i1 %C
260}
261
262define <2 x i1> @test19vec(<2 x i32> %A) {
263; CHECK-LABEL: @test19vec(
264; CHECK-NEXT:    [[C:%.*]] = icmp ult <2 x i32> [[A:%.*]], <i32 4, i32 4>
265; CHECK-NEXT:    ret <2 x i1> [[C]]
266;
267  %B = ashr <2 x i32> %A, <i32 2, i32 2>
268  %C = icmp eq <2 x i32> %B, zeroinitializer
269  ret <2 x i1> %C
270}
271
272;; X >u ~4
273define i1 @test19a(i32 %A) {
274; CHECK-LABEL: @test19a(
275; CHECK-NEXT:    [[C:%.*]] = icmp ugt i32 [[A:%.*]], -5
276; CHECK-NEXT:    ret i1 [[C]]
277;
278  %B = ashr i32 %A, 2
279  %C = icmp eq i32 %B, -1
280  ret i1 %C
281}
282
283define <2 x i1> @test19a_vec(<2 x i32> %A) {
284; CHECK-LABEL: @test19a_vec(
285; CHECK-NEXT:    [[C:%.*]] = icmp ugt <2 x i32> [[A:%.*]], <i32 -5, i32 -5>
286; CHECK-NEXT:    ret <2 x i1> [[C]]
287;
288  %B = ashr <2 x i32> %A, <i32 2, i32 2>
289  %C = icmp eq <2 x i32> %B, <i32 -1, i32 -1>
290  ret <2 x i1> %C
291}
292
293define i1 @test20(i8 %A) {
294; CHECK-LABEL: @test20(
295; CHECK-NEXT:    ret i1 false
296;
297  %B = ashr i8 %A, 7
298  ;; false
299  %C = icmp eq i8 %B, 123
300  ret i1 %C
301}
302
303define i1 @test21(i8 %A) {
304; CHECK-LABEL: @test21(
305; CHECK-NEXT:    [[B_MASK:%.*]] = and i8 [[A:%.*]], 15
306; CHECK-NEXT:    [[C:%.*]] = icmp eq i8 [[B_MASK]], 8
307; CHECK-NEXT:    ret i1 [[C]]
308;
309  %B = shl i8 %A, 4
310  %C = icmp eq i8 %B, -128
311  ret i1 %C
312}
313
314define i1 @test22(i8 %A) {
315; CHECK-LABEL: @test22(
316; CHECK-NEXT:    [[B_MASK:%.*]] = and i8 [[A:%.*]], 15
317; CHECK-NEXT:    [[C:%.*]] = icmp eq i8 [[B_MASK]], 0
318; CHECK-NEXT:    ret i1 [[C]]
319;
320  %B = shl i8 %A, 4
321  %C = icmp eq i8 %B, 0
322  ret i1 %C
323}
324
325define i8 @test23(i32 %A) {
326; CHECK-LABEL: @test23(
327; CHECK-NEXT:    [[D:%.*]] = trunc i32 [[A:%.*]] to i8
328; CHECK-NEXT:    ret i8 [[D]]
329;
330  ;; casts not needed
331  %B = shl i32 %A, 24
332  %C = ashr i32 %B, 24
333  %D = trunc i32 %C to i8
334  ret i8 %D
335}
336
337define i8 @test24(i8 %X) {
338; CHECK-LABEL: @test24(
339; CHECK-NEXT:    [[Z:%.*]] = and i8 [[X:%.*]], 3
340; CHECK-NEXT:    ret i8 [[Z]]
341;
342  %Y = and i8 %X, -5
343  %Z = shl i8 %Y, 5
344  %Q = ashr i8 %Z, 5
345  ret i8 %Q
346}
347
348define i32 @test25(i32 %i.2, i32 %AA) {
349; CHECK-LABEL: @test25(
350; CHECK-NEXT:    [[I_3:%.*]] = and i32 [[I_2:%.*]], -131072
351; CHECK-NEXT:    [[X2:%.*]] = add i32 [[I_3]], [[AA:%.*]]
352; CHECK-NEXT:    [[I_6:%.*]] = and i32 [[X2]], -131072
353; CHECK-NEXT:    ret i32 [[I_6]]
354;
355  %x = lshr i32 %AA, 17
356  %i.3 = lshr i32 %i.2, 17
357  %i.5 = add i32 %i.3, %x
358  %i.6 = shl i32 %i.5, 17
359  ret i32 %i.6
360}
361
362define <2 x i32> @test25_vector(<2 x i32> %i.2, <2 x i32> %AA) {
363; CHECK-LABEL: @test25_vector(
364; CHECK-NEXT:    [[I_3:%.*]] = and <2 x i32> [[I_2:%.*]], <i32 -131072, i32 -131072>
365; CHECK-NEXT:    [[X2:%.*]] = add <2 x i32> [[I_3]], [[AA:%.*]]
366; CHECK-NEXT:    [[I_6:%.*]] = and <2 x i32> [[X2]], <i32 -131072, i32 -131072>
367; CHECK-NEXT:    ret <2 x i32> [[I_6]]
368;
369  %x = lshr <2 x i32> %AA, <i32 17, i32 17>
370  %i.3 = lshr <2 x i32> %i.2, <i32 17, i32 17>
371  %i.5 = add <2 x i32> %i.3, %x
372  %i.6 = shl <2 x i32> %i.5, <i32 17, i32 17>
373  ret <2 x i32> %i.6
374}
375
376;; handle casts between shifts.
377define i32 @test26(i32 %A) {
378; CHECK-LABEL: @test26(
379; CHECK-NEXT:    [[B:%.*]] = and i32 [[A:%.*]], -2
380; CHECK-NEXT:    ret i32 [[B]]
381;
382  %B = lshr i32 %A, 1
383  %C = bitcast i32 %B to i32
384  %D = shl i32 %C, 1
385  ret i32 %D
386}
387
388
389define i1 @test27(i32 %x) nounwind {
390; CHECK-LABEL: @test27(
391; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 8
392; CHECK-NEXT:    [[Z:%.*]] = icmp ne i32 [[TMP1]], 0
393; CHECK-NEXT:    ret i1 [[Z]]
394;
395  %y = lshr i32 %x, 3
396  %z = trunc i32 %y to i1
397  ret i1 %z
398}
399
400define i1 @test28(i8 %x) {
401; CHECK-LABEL: @test28(
402; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
403; CHECK-NEXT:    ret i1 [[CMP]]
404;
405  %shr = lshr i8 %x, 7
406  %cmp = icmp ne i8 %shr, 0
407  ret i1 %cmp
408}
409
410define <2 x i1> @test28vec(<2 x i8> %x) {
411; CHECK-LABEL: @test28vec(
412; CHECK-NEXT:    [[CMP:%.*]] = icmp slt <2 x i8> [[X:%.*]], zeroinitializer
413; CHECK-NEXT:    ret <2 x i1> [[CMP]]
414;
415  %shr = lshr <2 x i8> %x, <i8 7, i8 7>
416  %cmp = icmp ne <2 x i8> %shr, zeroinitializer
417  ret <2 x i1> %cmp
418}
419
420define i8 @test28a(i8 %x, i8 %y) {
421; CHECK-LABEL: @test28a(
422; CHECK-NEXT:  entry:
423; CHECK-NEXT:    [[I1:%.*]] = lshr i8 [[X:%.*]], 7
424; CHECK-NEXT:    [[COND1_NOT:%.*]] = icmp sgt i8 [[X]], -1
425; CHECK-NEXT:    br i1 [[COND1_NOT]], label [[BB2:%.*]], label [[BB1:%.*]]
426; CHECK:       bb1:
427; CHECK-NEXT:    ret i8 [[I1]]
428; CHECK:       bb2:
429; CHECK-NEXT:    [[I2:%.*]] = add i8 [[I1]], [[Y:%.*]]
430; CHECK-NEXT:    ret i8 [[I2]]
431;
432entry:
433; This shouldn't be transformed.
434  %i1 = lshr i8 %x, 7
435  %cond1 = icmp ne i8 %i1, 0
436  br i1 %cond1, label %bb1, label %bb2
437bb1:
438  ret i8 %i1
439bb2:
440  %i2 = add i8 %i1, %y
441  ret i8 %i2
442}
443
444define i32 @test29(i64 %d18) {
445; CHECK-LABEL: @test29(
446; CHECK-NEXT:  entry:
447; CHECK-NEXT:    [[I916:%.*]] = lshr i64 [[D18:%.*]], 63
448; CHECK-NEXT:    [[I10:%.*]] = trunc i64 [[I916]] to i32
449; CHECK-NEXT:    ret i32 [[I10]]
450;
451entry:
452  %i916 = lshr i64 %d18, 32
453  %i917 = trunc i64 %i916 to i32
454  %i10 = lshr i32 %i917, 31
455  ret i32 %i10
456}
457
458define <2 x i32> @test29_uniform(<2 x i64> %d18) {
459; CHECK-LABEL: @test29_uniform(
460; CHECK-NEXT:  entry:
461; CHECK-NEXT:    [[I916:%.*]] = lshr <2 x i64> [[D18:%.*]], <i64 63, i64 63>
462; CHECK-NEXT:    [[I10:%.*]] = trunc <2 x i64> [[I916]] to <2 x i32>
463; CHECK-NEXT:    ret <2 x i32> [[I10]]
464;
465entry:
466  %i916 = lshr <2 x i64> %d18, <i64 32, i64 32>
467  %i917 = trunc <2 x i64> %i916 to <2 x i32>
468  %i10 = lshr <2 x i32> %i917, <i32 31, i32 31>
469  ret <2 x i32> %i10
470}
471
472define <2 x i32> @test29_nonuniform(<2 x i64> %d18) {
473; CHECK-LABEL: @test29_nonuniform(
474; CHECK-NEXT:  entry:
475; CHECK-NEXT:    [[I916:%.*]] = lshr <2 x i64> [[D18:%.*]], <i64 32, i64 15>
476; CHECK-NEXT:    [[I917:%.*]] = trunc <2 x i64> [[I916]] to <2 x i32>
477; CHECK-NEXT:    [[I10:%.*]] = lshr <2 x i32> [[I917]], <i32 31, i32 22>
478; CHECK-NEXT:    ret <2 x i32> [[I10]]
479;
480entry:
481  %i916 = lshr <2 x i64> %d18, <i64 32, i64 15>
482  %i917 = trunc <2 x i64> %i916 to <2 x i32>
483  %i10 = lshr <2 x i32> %i917, <i32 31, i32 22>
484  ret <2 x i32> %i10
485}
486
487define <2 x i32> @test29_undef(<2 x i64> %d18) {
488; CHECK-LABEL: @test29_undef(
489; CHECK-NEXT:  entry:
490; CHECK-NEXT:    [[I916:%.*]] = lshr <2 x i64> [[D18:%.*]], <i64 32, i64 undef>
491; CHECK-NEXT:    [[I917:%.*]] = trunc <2 x i64> [[I916]] to <2 x i32>
492; CHECK-NEXT:    [[I10:%.*]] = lshr <2 x i32> [[I917]], <i32 31, i32 undef>
493; CHECK-NEXT:    ret <2 x i32> [[I10]]
494;
495entry:
496  %i916 = lshr <2 x i64> %d18, <i64 32, i64 undef>
497  %i917 = trunc <2 x i64> %i916 to <2 x i32>
498  %i10 = lshr <2 x i32> %i917, <i32 31, i32 undef>
499  ret <2 x i32> %i10
500}
501
502define i32 @test30(i32 %A, i32 %B, i32 %C) {
503; CHECK-LABEL: @test30(
504; CHECK-NEXT:    [[X1:%.*]] = and i32 [[A:%.*]], [[B:%.*]]
505; CHECK-NEXT:    [[Z:%.*]] = shl i32 [[X1]], [[C:%.*]]
506; CHECK-NEXT:    ret i32 [[Z]]
507;
508  %X = shl i32 %A, %C
509  %Y = shl i32 %B, %C
510  %Z = and i32 %X, %Y
511  ret i32 %Z
512}
513
514define i32 @test31(i32 %A, i32 %B, i32 %C) {
515; CHECK-LABEL: @test31(
516; CHECK-NEXT:    [[X1:%.*]] = or i32 [[A:%.*]], [[B:%.*]]
517; CHECK-NEXT:    [[Z:%.*]] = lshr i32 [[X1]], [[C:%.*]]
518; CHECK-NEXT:    ret i32 [[Z]]
519;
520  %X = lshr i32 %A, %C
521  %Y = lshr i32 %B, %C
522  %Z = or i32 %X, %Y
523  ret i32 %Z
524}
525
526define i32 @test32(i32 %A, i32 %B, i32 %C) {
527; CHECK-LABEL: @test32(
528; CHECK-NEXT:    [[X1:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
529; CHECK-NEXT:    [[Z:%.*]] = ashr i32 [[X1]], [[C:%.*]]
530; CHECK-NEXT:    ret i32 [[Z]]
531;
532  %X = ashr i32 %A, %C
533  %Y = ashr i32 %B, %C
534  %Z = xor i32 %X, %Y
535  ret i32 %Z
536}
537
538define i1 @test33(i32 %X) {
539; CHECK-LABEL: @test33(
540; CHECK-NEXT:    [[I1_MASK:%.*]] = and i32 [[X:%.*]], 16777216
541; CHECK-NEXT:    [[I2:%.*]] = icmp ne i32 [[I1_MASK]], 0
542; CHECK-NEXT:    ret i1 [[I2]]
543;
544  %i1 = shl i32 %X, 7
545  %i2 = icmp slt i32 %i1, 0
546  ret i1 %i2
547}
548
549define <2 x i1> @test33vec(<2 x i32> %X) {
550; CHECK-LABEL: @test33vec(
551; CHECK-NEXT:    [[I1_MASK:%.*]] = and <2 x i32> [[X:%.*]], <i32 16777216, i32 16777216>
552; CHECK-NEXT:    [[I2:%.*]] = icmp ne <2 x i32> [[I1_MASK]], zeroinitializer
553; CHECK-NEXT:    ret <2 x i1> [[I2]]
554;
555  %i1 = shl <2 x i32> %X, <i32 7, i32 7>
556  %i2 = icmp slt <2 x i32> %i1, zeroinitializer
557  ret <2 x i1> %i2
558}
559
560define i1 @test34(i32 %X) {
561; CHECK-LABEL: @test34(
562; CHECK-NEXT:    ret i1 false
563;
564  %i1 = lshr i32 %X, 7
565  %i2 = icmp slt i32 %i1, 0
566  ret i1 %i2
567}
568
569define i1 @test35(i32 %X) {
570; CHECK-LABEL: @test35(
571; CHECK-NEXT:    [[I2:%.*]] = icmp slt i32 [[X:%.*]], 0
572; CHECK-NEXT:    ret i1 [[I2]]
573;
574  %i1 = ashr i32 %X, 7
575  %i2 = icmp slt i32 %i1, 0
576  ret i1 %i2
577}
578
579define <2 x i1> @test35vec(<2 x i32> %X) {
580; CHECK-LABEL: @test35vec(
581; CHECK-NEXT:    [[I2:%.*]] = icmp slt <2 x i32> [[X:%.*]], zeroinitializer
582; CHECK-NEXT:    ret <2 x i1> [[I2]]
583;
584  %i1 = ashr <2 x i32> %X, <i32 7, i32 7>
585  %i2 = icmp slt <2 x i32> %i1, zeroinitializer
586  ret <2 x i1> %i2
587}
588
589define i128 @test36(i128 %A, i128 %B) {
590; CHECK-LABEL: @test36(
591; CHECK-NEXT:    [[I231:%.*]] = or i128 [[B:%.*]], [[A:%.*]]
592; CHECK-NEXT:    [[INS:%.*]] = and i128 [[I231]], 18446744073709551615
593; CHECK-NEXT:    ret i128 [[INS]]
594;
595  %i27 = shl i128 %A, 64
596  %i23 = shl i128 %B, 64
597  %ins = or i128 %i23, %i27
598  %i45 = lshr i128 %ins, 64
599  ret i128 %i45
600}
601
602define i64 @test37(i128 %A, i32 %B) {
603; CHECK-LABEL: @test37(
604; CHECK-NEXT:    [[I22:%.*]] = zext i32 [[B:%.*]] to i128
605; CHECK-NEXT:    [[I23:%.*]] = shl nuw nsw i128 [[I22]], 32
606; CHECK-NEXT:    [[INS:%.*]] = or i128 [[I23]], [[A:%.*]]
607; CHECK-NEXT:    [[I46:%.*]] = trunc i128 [[INS]] to i64
608; CHECK-NEXT:    ret i64 [[I46]]
609;
610  %i27 = shl i128 %A, 64
611  %i22 = zext i32 %B to i128
612  %i23 = shl i128 %i22, 96
613  %ins = or i128 %i23, %i27
614  %i45 = lshr i128 %ins, 64
615  %i46 = trunc i128 %i45 to i64
616  ret i64 %i46
617}
618
619define <2 x i32> @shl_nuw_nsw_splat_vec(<2 x i8> %x) {
620; CHECK-LABEL: @shl_nuw_nsw_splat_vec(
621; CHECK-NEXT:    [[T2:%.*]] = zext <2 x i8> [[X:%.*]] to <2 x i32>
622; CHECK-NEXT:    [[T3:%.*]] = shl nuw nsw <2 x i32> [[T2]], <i32 17, i32 17>
623; CHECK-NEXT:    ret <2 x i32> [[T3]]
624;
625  %t2 = zext <2 x i8> %x to <2 x i32>
626  %t3 = shl <2 x i32> %t2, <i32 17, i32 17>
627  ret <2 x i32> %t3
628}
629
630define i32 @test38(i32 %x) nounwind readnone {
631; CHECK-LABEL: @test38(
632; CHECK-NEXT:    [[REM1:%.*]] = and i32 [[X:%.*]], 31
633; CHECK-NEXT:    [[SHL:%.*]] = shl i32 1, [[REM1]]
634; CHECK-NEXT:    ret i32 [[SHL]]
635;
636  %rem = srem i32 %x, 32
637  %shl = shl i32 1, %rem
638  ret i32 %shl
639}
640
641define <2 x i32> @test38_uniform(<2 x i32> %x) nounwind readnone {
642; CHECK-LABEL: @test38_uniform(
643; CHECK-NEXT:    [[REM1:%.*]] = and <2 x i32> [[X:%.*]], <i32 31, i32 31>
644; CHECK-NEXT:    [[SHL:%.*]] = shl <2 x i32> <i32 1, i32 1>, [[REM1]]
645; CHECK-NEXT:    ret <2 x i32> [[SHL]]
646;
647  %rem = srem <2 x i32> %x, <i32 32, i32 32>
648  %shl = shl <2 x i32> <i32 1, i32 1>, %rem
649  ret <2 x i32> %shl
650}
651
652define <3 x i32> @test38_nonuniform(<3 x i32> %x) nounwind readnone {
653; CHECK-LABEL: @test38_nonuniform(
654; CHECK-NEXT:    [[REM1:%.*]] = and <3 x i32> [[X:%.*]], <i32 31, i32 15, i32 0>
655; CHECK-NEXT:    [[SHL:%.*]] = shl <3 x i32> <i32 1, i32 1, i32 1>, [[REM1]]
656; CHECK-NEXT:    ret <3 x i32> [[SHL]]
657;
658  %rem = srem <3 x i32> %x, <i32 32, i32 16, i32 1>
659  %shl = shl <3 x i32> <i32 1, i32 1, i32 1>, %rem
660  ret <3 x i32> %shl
661}
662
663define <2 x i32> @test38_undef(<2 x i32> %x) nounwind readnone {
664; CHECK-LABEL: @test38_undef(
665; CHECK-NEXT:    ret <2 x i32> undef
666;
667  %rem = srem <2 x i32> %x, <i32 32, i32 undef>
668  %shl = shl <2 x i32> <i32 1, i32 1>, %rem
669  ret <2 x i32> %shl
670}
671
672; <rdar://problem/8756731>
673define i8 @test39(i32 %a0) {
674; CHECK-LABEL: @test39(
675; CHECK-NEXT:  entry:
676; CHECK-NEXT:    [[I4:%.*]] = trunc i32 [[A0:%.*]] to i8
677; CHECK-NEXT:    [[I5:%.*]] = shl i8 [[I4]], 5
678; CHECK-NEXT:    [[I49:%.*]] = shl i8 [[I4]], 6
679; CHECK-NEXT:    [[I50:%.*]] = and i8 [[I49]], 64
680; CHECK-NEXT:    [[I51:%.*]] = xor i8 [[I50]], [[I5]]
681; CHECK-NEXT:    [[TMP0:%.*]] = shl i8 [[I4]], 2
682; CHECK-NEXT:    [[I54:%.*]] = and i8 [[TMP0]], 16
683; CHECK-NEXT:    [[I551:%.*]] = or i8 [[I54]], [[I51]]
684; CHECK-NEXT:    ret i8 [[I551]]
685;
686entry:
687  %i4 = trunc i32 %a0 to i8
688  %i5 = shl i8 %i4, 5
689  %i48 = and i8 %i5, 32
690  %i49 = lshr i8 %i48, 5
691  %i50 = mul i8 %i49, 64
692  %i51 = xor i8 %i50, %i5
693  %i52 = and i8 %i51, -128
694  %i53 = lshr i8 %i52, 7
695  %i54 = mul i8 %i53, 16
696  %i55 = xor i8 %i54, %i51
697  ret i8 %i55
698}
699
700; PR9809
701define i32 @test40(i32 %a, i32 %b) nounwind {
702; CHECK-LABEL: @test40(
703; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[B:%.*]], 2
704; CHECK-NEXT:    [[DIV:%.*]] = lshr i32 [[A:%.*]], [[TMP1]]
705; CHECK-NEXT:    ret i32 [[DIV]]
706;
707  %shl1 = shl i32 1, %b
708  %shl2 = shl i32 %shl1, 2
709  %div = udiv i32 %a, %shl2
710  ret i32 %div
711}
712
713define i32 @test41(i32 %a, i32 %b) nounwind {
714; CHECK-LABEL: @test41(
715; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 8, [[B:%.*]]
716; CHECK-NEXT:    ret i32 [[TMP1]]
717;
718  %1 = shl i32 1, %b
719  %2 = shl i32 %1, 3
720  ret i32 %2
721}
722
723define i32 @test42(i32 %a, i32 %b) nounwind {
724; CHECK-LABEL: @test42(
725; CHECK-NEXT:    [[DIV:%.*]] = lshr exact i32 4096, [[B:%.*]]
726; CHECK-NEXT:    [[DIV2:%.*]] = udiv i32 [[A:%.*]], [[DIV]]
727; CHECK-NEXT:    ret i32 [[DIV2]]
728;
729  %div = lshr i32 4096, %b    ; must be exact otherwise we'd divide by zero
730  %div2 = udiv i32 %a, %div
731  ret i32 %div2
732}
733
734define <2 x i32> @test42vec(<2 x i32> %a, <2 x i32> %b) {
735; CHECK-LABEL: @test42vec(
736; CHECK-NEXT:    [[DIV:%.*]] = lshr exact <2 x i32> <i32 4096, i32 4096>, [[B:%.*]]
737; CHECK-NEXT:    [[DIV2:%.*]] = udiv <2 x i32> [[A:%.*]], [[DIV]]
738; CHECK-NEXT:    ret <2 x i32> [[DIV2]]
739;
740  %div = lshr <2 x i32> <i32 4096, i32 4096>, %b    ; must be exact otherwise we'd divide by zero
741  %div2 = udiv <2 x i32> %a, %div
742  ret <2 x i32> %div2
743}
744
745define i32 @test43(i32 %a, i32 %b) nounwind {
746; CHECK-LABEL: @test43(
747; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[B:%.*]], 12
748; CHECK-NEXT:    [[DIV2:%.*]] = lshr i32 [[A:%.*]], [[TMP1]]
749; CHECK-NEXT:    ret i32 [[DIV2]]
750;
751  %div = shl i32 4096, %b    ; must be exact otherwise we'd divide by zero
752  %div2 = udiv i32 %a, %div
753  ret i32 %div2
754}
755
756define i32 @test44(i32 %a) nounwind {
757; CHECK-LABEL: @test44(
758; CHECK-NEXT:    [[Y:%.*]] = shl i32 [[A:%.*]], 5
759; CHECK-NEXT:    ret i32 [[Y]]
760;
761  %y = shl nuw i32 %a, 1
762  %z = shl i32 %y, 4
763  ret i32 %z
764}
765
766define i32 @test45(i32 %a) nounwind {
767; CHECK-LABEL: @test45(
768; CHECK-NEXT:    [[Y:%.*]] = lshr i32 [[A:%.*]], 5
769; CHECK-NEXT:    ret i32 [[Y]]
770;
771  %y = lshr exact i32 %a, 1
772  %z = lshr i32 %y, 4
773  ret i32 %z
774}
775
776; (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
777
778define i32 @test46(i32 %a) {
779; CHECK-LABEL: @test46(
780; CHECK-NEXT:    [[Z:%.*]] = ashr exact i32 [[A:%.*]], 2
781; CHECK-NEXT:    ret i32 [[Z]]
782;
783  %y = ashr exact i32 %a, 3
784  %z = shl i32 %y, 1
785  ret i32 %z
786}
787
788; (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
789
790define <2 x i32> @test46_splat_vec(<2 x i32> %a) {
791; CHECK-LABEL: @test46_splat_vec(
792; CHECK-NEXT:    [[Z:%.*]] = ashr exact <2 x i32> [[A:%.*]], <i32 2, i32 2>
793; CHECK-NEXT:    ret <2 x i32> [[Z]]
794;
795  %y = ashr exact <2 x i32> %a, <i32 3, i32 3>
796  %z = shl <2 x i32> %y, <i32 1, i32 1>
797  ret <2 x i32> %z
798}
799
800; (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
801
802define i8 @test47(i8 %a) {
803; CHECK-LABEL: @test47(
804; CHECK-NEXT:    [[Z:%.*]] = lshr exact i8 [[A:%.*]], 2
805; CHECK-NEXT:    ret i8 [[Z]]
806;
807  %y = lshr exact i8 %a, 3
808  %z = shl i8 %y, 1
809  ret i8 %z
810}
811
812; (X >>?exact C1) << C2 --> X >>?exact (C1-C2)
813
814define <2 x i8> @test47_splat_vec(<2 x i8> %a) {
815; CHECK-LABEL: @test47_splat_vec(
816; CHECK-NEXT:    [[Z:%.*]] = lshr exact <2 x i8> [[A:%.*]], <i8 2, i8 2>
817; CHECK-NEXT:    ret <2 x i8> [[Z]]
818;
819  %y = lshr exact <2 x i8> %a, <i8 3, i8 3>
820  %z = shl <2 x i8> %y, <i8 1, i8 1>
821  ret <2 x i8> %z
822}
823
824; (X >>u,exact C1) << C2 --> X << (C2-C1) when C2 > C1
825
826define i32 @test48(i32 %x) {
827; CHECK-LABEL: @test48(
828; CHECK-NEXT:    [[B:%.*]] = shl i32 [[X:%.*]], 2
829; CHECK-NEXT:    ret i32 [[B]]
830;
831  %A = lshr exact i32 %x, 1
832  %B = shl i32 %A, 3
833  ret i32 %B
834}
835
836; Verify that wrap flags are preserved from the original 'shl'.
837
838define i32 @test48_nuw_nsw(i32 %x) {
839; CHECK-LABEL: @test48_nuw_nsw(
840; CHECK-NEXT:    [[B:%.*]] = shl nuw nsw i32 [[X:%.*]], 2
841; CHECK-NEXT:    ret i32 [[B]]
842;
843  %A = lshr exact i32 %x, 1
844  %B = shl nuw nsw i32 %A, 3
845  ret i32 %B
846}
847
848; (X >>u,exact C1) << C2 --> X << (C2-C1) when splatted C2 > C1
849
850define <2 x i32> @test48_splat_vec(<2 x i32> %x) {
851; CHECK-LABEL: @test48_splat_vec(
852; CHECK-NEXT:    [[B:%.*]] = shl nuw nsw <2 x i32> [[X:%.*]], <i32 2, i32 2>
853; CHECK-NEXT:    ret <2 x i32> [[B]]
854;
855  %A = lshr exact <2 x i32> %x, <i32 1, i32 1>
856  %B = shl nsw nuw <2 x i32> %A, <i32 3, i32 3>
857  ret <2 x i32> %B
858}
859
860; (X >>s,exact C1) << C2 --> X << (C2-C1) when C2 > C1
861
862define i32 @test49(i32 %x) {
863; CHECK-LABEL: @test49(
864; CHECK-NEXT:    [[B:%.*]] = shl i32 [[X:%.*]], 2
865; CHECK-NEXT:    ret i32 [[B]]
866;
867  %A = ashr exact i32 %x, 1
868  %B = shl i32 %A, 3
869  ret i32 %B
870}
871
872; Verify that wrap flags are preserved from the original 'shl'.
873
874define i32 @test49_nuw_nsw(i32 %x) {
875; CHECK-LABEL: @test49_nuw_nsw(
876; CHECK-NEXT:    [[B:%.*]] = shl nuw nsw i32 [[X:%.*]], 2
877; CHECK-NEXT:    ret i32 [[B]]
878;
879  %A = ashr exact i32 %x, 1
880  %B = shl nuw nsw i32 %A, 3
881  ret i32 %B
882}
883
884; (X >>s,exact C1) << C2 --> X << (C2-C1) when splatted C2 > C1
885
886define <2 x i32> @test49_splat_vec(<2 x i32> %x) {
887; CHECK-LABEL: @test49_splat_vec(
888; CHECK-NEXT:    [[B:%.*]] = shl nuw nsw <2 x i32> [[X:%.*]], <i32 2, i32 2>
889; CHECK-NEXT:    ret <2 x i32> [[B]]
890;
891  %A = ashr exact <2 x i32> %x, <i32 1, i32 1>
892  %B = shl nsw nuw <2 x i32> %A, <i32 3, i32 3>
893  ret <2 x i32> %B
894}
895
896; (X <<nsw C1) >>s C2 --> X >>s (C2-C1)
897
898define i32 @test50(i32 %x) {
899; CHECK-LABEL: @test50(
900; CHECK-NEXT:    [[B:%.*]] = ashr i32 [[X:%.*]], 2
901; CHECK-NEXT:    ret i32 [[B]]
902;
903  %A = shl nsw i32 %x, 1
904  %B = ashr i32 %A, 3
905  ret i32 %B
906}
907
908; (X <<nsw C1) >>s C2 --> X >>s (C2-C1)
909; Also, check that exact is propagated.
910
911define <2 x i32> @test50_splat_vec(<2 x i32> %x) {
912; CHECK-LABEL: @test50_splat_vec(
913; CHECK-NEXT:    [[B:%.*]] = ashr exact <2 x i32> [[X:%.*]], <i32 2, i32 2>
914; CHECK-NEXT:    ret <2 x i32> [[B]]
915;
916  %A = shl nsw <2 x i32> %x, <i32 1, i32 1>
917  %B = ashr exact <2 x i32> %A, <i32 3, i32 3>
918  ret <2 x i32> %B
919}
920
921; (X <<nuw C1) >>u C2 --> X >>u (C2-C1)
922
923define i32 @test51(i32 %x) {
924; CHECK-LABEL: @test51(
925; CHECK-NEXT:    [[B:%.*]] = lshr i32 [[X:%.*]], 2
926; CHECK-NEXT:    ret i32 [[B]]
927;
928  %A = shl nuw i32 %x, 1
929  %B = lshr i32 %A, 3
930  ret i32 %B
931}
932
933; (X <<nuw C1) >>u C2 --> X >>u (C2-C1) with splats
934; Also, check that exact is propagated.
935
936define <2 x i32> @test51_splat_vec(<2 x i32> %x) {
937; CHECK-LABEL: @test51_splat_vec(
938; CHECK-NEXT:    [[B:%.*]] = lshr exact <2 x i32> [[X:%.*]], <i32 2, i32 2>
939; CHECK-NEXT:    ret <2 x i32> [[B]]
940;
941  %A = shl nuw <2 x i32> %x, <i32 1, i32 1>
942  %B = lshr exact <2 x i32> %A, <i32 3, i32 3>
943  ret <2 x i32> %B
944}
945
946; (X << C1) >>u C2  --> X >>u (C2-C1) & (-1 >> C2)
947; Also, check that exact is propagated.
948
949define i32 @test51_no_nuw(i32 %x) {
950; CHECK-LABEL: @test51_no_nuw(
951; CHECK-NEXT:    [[TMP1:%.*]] = lshr exact i32 [[X:%.*]], 2
952; CHECK-NEXT:    [[B:%.*]] = and i32 [[TMP1]], 536870911
953; CHECK-NEXT:    ret i32 [[B]]
954;
955  %A = shl i32 %x, 1
956  %B = lshr exact i32 %A, 3
957  ret i32 %B
958}
959
960; (X << C1) >>u C2  --> X >>u (C2-C1) & (-1 >> C2)
961
962define <2 x i32> @test51_no_nuw_splat_vec(<2 x i32> %x) {
963; CHECK-LABEL: @test51_no_nuw_splat_vec(
964; CHECK-NEXT:    [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 2, i32 2>
965; CHECK-NEXT:    [[B:%.*]] = and <2 x i32> [[TMP1]], <i32 536870911, i32 536870911>
966; CHECK-NEXT:    ret <2 x i32> [[B]]
967;
968  %A = shl <2 x i32> %x, <i32 1, i32 1>
969  %B = lshr <2 x i32> %A, <i32 3, i32 3>
970  ret <2 x i32> %B
971}
972
973; (X <<nsw C1) >>s C2 --> X <<nsw (C1 - C2)
974
975define i32 @test52(i32 %x) {
976; CHECK-LABEL: @test52(
977; CHECK-NEXT:    [[B:%.*]] = shl nsw i32 [[X:%.*]], 2
978; CHECK-NEXT:    ret i32 [[B]]
979;
980  %A = shl nsw i32 %x, 3
981  %B = ashr i32 %A, 1
982  ret i32 %B
983}
984
985; (X <<nsw C1) >>s C2 --> X <<nsw (C1 - C2)
986
987define <2 x i32> @test52_splat_vec(<2 x i32> %x) {
988; CHECK-LABEL: @test52_splat_vec(
989; CHECK-NEXT:    [[B:%.*]] = shl nsw <2 x i32> [[X:%.*]], <i32 2, i32 2>
990; CHECK-NEXT:    ret <2 x i32> [[B]]
991;
992  %A = shl nsw <2 x i32> %x, <i32 3, i32 3>
993  %B = ashr <2 x i32> %A, <i32 1, i32 1>
994  ret <2 x i32> %B
995}
996
997; (X <<nuw C1) >>u C2 --> X <<nuw (C1 - C2)
998
999define i32 @test53(i32 %x) {
1000; CHECK-LABEL: @test53(
1001; CHECK-NEXT:    [[B:%.*]] = shl nuw i32 [[X:%.*]], 2
1002; CHECK-NEXT:    ret i32 [[B]]
1003;
1004  %A = shl nuw i32 %x, 3
1005  %B = lshr i32 %A, 1
1006  ret i32 %B
1007}
1008
1009; (X <<nuw C1) >>u C2 --> X <<nuw (C1 - C2)
1010
1011define <2 x i32> @test53_splat_vec(<2 x i32> %x) {
1012; CHECK-LABEL: @test53_splat_vec(
1013; CHECK-NEXT:    [[B:%.*]] = shl nuw <2 x i32> [[X:%.*]], <i32 2, i32 2>
1014; CHECK-NEXT:    ret <2 x i32> [[B]]
1015;
1016  %A = shl nuw <2 x i32> %x, <i32 3, i32 3>
1017  %B = lshr <2 x i32> %A, <i32 1, i32 1>
1018  ret <2 x i32> %B
1019}
1020
1021; (X << C1) >>u C2  --> X << (C1 - C2) & (-1 >> C2)
1022
1023define i8 @test53_no_nuw(i8 %x) {
1024; CHECK-LABEL: @test53_no_nuw(
1025; CHECK-NEXT:    [[TMP1:%.*]] = shl i8 [[X:%.*]], 2
1026; CHECK-NEXT:    [[B:%.*]] = and i8 [[TMP1]], 124
1027; CHECK-NEXT:    ret i8 [[B]]
1028;
1029  %A = shl i8 %x, 3
1030  %B = lshr i8 %A, 1
1031  ret i8 %B
1032}
1033
1034; (X << C1) >>u C2  --> X << (C1 - C2) & (-1 >> C2)
1035
1036define <2 x i8> @test53_no_nuw_splat_vec(<2 x i8> %x) {
1037; CHECK-LABEL: @test53_no_nuw_splat_vec(
1038; CHECK-NEXT:    [[TMP1:%.*]] = shl <2 x i8> [[X:%.*]], <i8 2, i8 2>
1039; CHECK-NEXT:    [[B:%.*]] = and <2 x i8> [[TMP1]], <i8 124, i8 124>
1040; CHECK-NEXT:    ret <2 x i8> [[B]]
1041;
1042  %A = shl <2 x i8> %x, <i8 3, i8 3>
1043  %B = lshr <2 x i8> %A, <i8 1, i8 1>
1044  ret <2 x i8> %B
1045}
1046
1047define i32 @test54(i32 %x) {
1048; CHECK-LABEL: @test54(
1049; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 3
1050; CHECK-NEXT:    [[AND:%.*]] = and i32 [[TMP1]], 16
1051; CHECK-NEXT:    ret i32 [[AND]]
1052;
1053  %shr2 = lshr i32 %x, 1
1054  %shl = shl i32 %shr2, 4
1055  %and = and i32 %shl, 16
1056  ret i32 %and
1057}
1058
1059define <2 x i32> @test54_splat_vec(<2 x i32> %x) {
1060; CHECK-LABEL: @test54_splat_vec(
1061; CHECK-NEXT:    [[TMP1:%.*]] = shl <2 x i32> [[X:%.*]], <i32 3, i32 3>
1062; CHECK-NEXT:    [[AND:%.*]] = and <2 x i32> [[TMP1]], <i32 16, i32 16>
1063; CHECK-NEXT:    ret <2 x i32> [[AND]]
1064;
1065  %shr2 = lshr <2 x i32> %x, <i32 1, i32 1>
1066  %shl = shl <2 x i32> %shr2, <i32 4, i32 4>
1067  %and = and <2 x i32> %shl, <i32 16, i32 16>
1068  ret <2 x i32> %and
1069}
1070
1071define i32 @test55(i32 %x) {
1072; CHECK-LABEL: @test55(
1073; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 3
1074; CHECK-NEXT:    [[OR:%.*]] = or i32 [[TMP1]], 8
1075; CHECK-NEXT:    ret i32 [[OR]]
1076;
1077  %shr2 = lshr i32 %x, 1
1078  %shl = shl i32 %shr2, 4
1079  %or = or i32 %shl, 8
1080  ret i32 %or
1081}
1082
1083define i32 @test56(i32 %x) {
1084; CHECK-LABEL: @test56(
1085; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 3
1086; CHECK-NEXT:    [[SHL:%.*]] = and i32 [[TMP1]], -16
1087; CHECK-NEXT:    [[OR:%.*]] = or i32 [[SHL]], 7
1088; CHECK-NEXT:    ret i32 [[OR]]
1089;
1090  %shr2 = lshr i32 %x, 1
1091  %shl = shl i32 %shr2, 4
1092  %or = or i32 %shl, 7
1093  ret i32 %or
1094}
1095
1096define i32 @test57(i32 %x) {
1097; CHECK-LABEL: @test57(
1098; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 3
1099; CHECK-NEXT:    [[SHL:%.*]] = and i32 [[TMP1]], -16
1100; CHECK-NEXT:    [[OR:%.*]] = or i32 [[SHL]], 7
1101; CHECK-NEXT:    ret i32 [[OR]]
1102;
1103  %shr = ashr i32 %x, 1
1104  %shl = shl i32 %shr, 4
1105  %or = or i32 %shl, 7
1106  ret i32 %or
1107}
1108
1109define i32 @test58(i32 %x) {
1110; CHECK-LABEL: @test58(
1111; CHECK-NEXT:    [[TMP1:%.*]] = ashr i32 [[X:%.*]], 3
1112; CHECK-NEXT:    [[OR:%.*]] = or i32 [[TMP1]], 1
1113; CHECK-NEXT:    ret i32 [[OR]]
1114;
1115  %shr = ashr i32 %x, 4
1116  %shl = shl i32 %shr, 1
1117  %or = or i32 %shl, 1
1118  ret i32 %or
1119}
1120
1121define <2 x i32> @test58_splat_vec(<2 x i32> %x) {
1122; CHECK-LABEL: @test58_splat_vec(
1123; CHECK-NEXT:    [[TMP1:%.*]] = ashr <2 x i32> [[X:%.*]], <i32 3, i32 3>
1124; CHECK-NEXT:    [[OR:%.*]] = or <2 x i32> [[TMP1]], <i32 1, i32 1>
1125; CHECK-NEXT:    ret <2 x i32> [[OR]]
1126;
1127  %shr = ashr <2 x i32> %x, <i32 4, i32 4>
1128  %shl = shl <2 x i32> %shr, <i32 1, i32 1>
1129  %or = or <2 x i32> %shl, <i32 1, i32 1>
1130  ret <2 x i32> %or
1131}
1132
1133define i32 @test59(i32 %x) {
1134; CHECK-LABEL: @test59(
1135; CHECK-NEXT:    [[TMP1:%.*]] = ashr i32 [[X:%.*]], 3
1136; CHECK-NEXT:    [[SHL:%.*]] = and i32 [[TMP1]], -4
1137; CHECK-NEXT:    [[OR:%.*]] = or i32 [[SHL]], 2
1138; CHECK-NEXT:    ret i32 [[OR]]
1139;
1140  %shr = ashr i32 %x, 4
1141  %shl = shl i32 %shr, 1
1142  %or = or i32 %shl, 2
1143  ret i32 %or
1144}
1145
1146; propagate "exact" trait
1147define i32 @test60(i32 %x) {
1148; CHECK-LABEL: @test60(
1149; CHECK-NEXT:    [[SHL:%.*]] = ashr exact i32 [[X:%.*]], 3
1150; CHECK-NEXT:    [[OR:%.*]] = or i32 [[SHL]], 1
1151; CHECK-NEXT:    ret i32 [[OR]]
1152;
1153  %shr = ashr exact i32 %x, 4
1154  %shl = shl i32 %shr, 1
1155  %or = or i32 %shl, 1
1156  ret i32 %or
1157}
1158
1159; PR17026
1160define void @test61(i128 %arg) {
1161; CHECK-LABEL: @test61(
1162; CHECK-NEXT:  bb:
1163; CHECK-NEXT:    br i1 undef, label [[BB1:%.*]], label [[BB12:%.*]]
1164; CHECK:       bb1:
1165; CHECK-NEXT:    br label [[BB2:%.*]]
1166; CHECK:       bb2:
1167; CHECK-NEXT:    br i1 undef, label [[BB3:%.*]], label [[BB7:%.*]]
1168; CHECK:       bb3:
1169; CHECK-NEXT:    br label [[BB8:%.*]]
1170; CHECK:       bb7:
1171; CHECK-NEXT:    br i1 undef, label [[BB8]], label [[BB2]]
1172; CHECK:       bb8:
1173; CHECK-NEXT:    br i1 undef, label [[BB11:%.*]], label [[BB12]]
1174; CHECK:       bb11:
1175; CHECK-NEXT:    br i1 undef, label [[BB1]], label [[BB12]]
1176; CHECK:       bb12:
1177; CHECK-NEXT:    ret void
1178;
1179bb:
1180  br i1 undef, label %bb1, label %bb12
1181
1182bb1:                                              ; preds = %bb11, %bb
1183  br label %bb2
1184
1185bb2:                                              ; preds = %bb7, %bb1
1186  br i1 undef, label %bb3, label %bb7
1187
1188bb3:                                              ; preds = %bb2
1189  %i = lshr i128 %arg, 36893488147419103232
1190  %i4 = shl i128 %i, 0
1191  %i5 = or i128 %i4, undef
1192  %i6 = trunc i128 %i5 to i16
1193  br label %bb8
1194
1195bb7:                                              ; preds = %bb2
1196  br i1 undef, label %bb8, label %bb2
1197
1198bb8:                                              ; preds = %bb7, %bb3
1199  %i9 = phi i16 [ %i6, %bb3 ], [ undef, %bb7 ]
1200  %i10 = icmp eq i16 %i9, 0
1201  br i1 %i10, label %bb11, label %bb12
1202
1203bb11:                                             ; preds = %bb8
1204  br i1 undef, label %bb1, label %bb12
1205
1206bb12:                                             ; preds = %bb11, %bb8, %bb
1207  ret void
1208}
1209
1210define i32 @test62(i32 %a) {
1211; CHECK-LABEL: @test62(
1212; CHECK-NEXT:    ret i32 undef
1213;
1214  %b = ashr i32 %a, 32  ; shift all bits out
1215  ret i32 %b
1216}
1217
1218define <4 x i32> @test62_splat_vector(<4 x i32> %a) {
1219; CHECK-LABEL: @test62_splat_vector(
1220; CHECK-NEXT:    ret <4 x i32> undef
1221;
1222  %b = ashr <4 x i32> %a, <i32 32, i32 32, i32 32, i32 32>  ; shift all bits out
1223  ret <4 x i32> %b
1224}
1225
1226define <4 x i32> @test62_non_splat_vector(<4 x i32> %a) {
1227; CHECK-LABEL: @test62_non_splat_vector(
1228; CHECK-NEXT:    [[B:%.*]] = ashr <4 x i32> [[A:%.*]], <i32 32, i32 0, i32 1, i32 2>
1229; CHECK-NEXT:    ret <4 x i32> [[B]]
1230;
1231  %b = ashr <4 x i32> %a, <i32 32, i32 0, i32 1, i32 2>  ; shift all bits out
1232  ret <4 x i32> %b
1233}
1234
1235define <2 x i65> @test_63(<2 x i64> %t) {
1236; CHECK-LABEL: @test_63(
1237; CHECK-NEXT:    [[A:%.*]] = zext <2 x i64> [[T:%.*]] to <2 x i65>
1238; CHECK-NEXT:    [[SEXT:%.*]] = shl <2 x i65> [[A]], <i65 33, i65 33>
1239; CHECK-NEXT:    [[B:%.*]] = ashr exact <2 x i65> [[SEXT]], <i65 33, i65 33>
1240; CHECK-NEXT:    ret <2 x i65> [[B]]
1241;
1242  %a = zext <2 x i64> %t to <2 x i65>
1243  %sext = shl <2 x i65> %a, <i65 33, i65 33>
1244  %b = ashr <2 x i65> %sext, <i65 33, i65 33>
1245  ret <2 x i65> %b
1246}
1247
1248define i32 @test_shl_zext_bool(i1 %t) {
1249; CHECK-LABEL: @test_shl_zext_bool(
1250; CHECK-NEXT:    [[SHL:%.*]] = select i1 [[T:%.*]], i32 4, i32 0
1251; CHECK-NEXT:    ret i32 [[SHL]]
1252;
1253  %ext = zext i1 %t to i32
1254  %shl = shl i32 %ext, 2
1255  ret i32 %shl
1256}
1257
1258define <2 x i32> @test_shl_zext_bool_splat(<2 x i1> %t) {
1259; CHECK-LABEL: @test_shl_zext_bool_splat(
1260; CHECK-NEXT:    [[SHL:%.*]] = select <2 x i1> [[T:%.*]], <2 x i32> <i32 8, i32 8>, <2 x i32> zeroinitializer
1261; CHECK-NEXT:    ret <2 x i32> [[SHL]]
1262;
1263  %ext = zext <2 x i1> %t to <2 x i32>
1264  %shl = shl <2 x i32> %ext, <i32 3, i32 3>
1265  ret <2 x i32> %shl
1266}
1267
1268define <2 x i32> @test_shl_zext_bool_vec(<2 x i1> %t) {
1269; CHECK-LABEL: @test_shl_zext_bool_vec(
1270; CHECK-NEXT:    [[SHL:%.*]] = select <2 x i1> [[T:%.*]], <2 x i32> <i32 4, i32 8>, <2 x i32> zeroinitializer
1271; CHECK-NEXT:    ret <2 x i32> [[SHL]]
1272;
1273  %ext = zext <2 x i1> %t to <2 x i32>
1274  %shl = shl <2 x i32> %ext, <i32 2, i32 3>
1275  ret <2 x i32> %shl
1276}
1277
1278define i32 @test_shl_zext_bool_not_constant(i1 %cmp, i32 %shamt) {
1279; CHECK-LABEL: @test_shl_zext_bool_not_constant(
1280; CHECK-NEXT:    [[CONV3:%.*]] = zext i1 [[CMP:%.*]] to i32
1281; CHECK-NEXT:    [[SHL:%.*]] = shl i32 [[CONV3]], [[SHAMT:%.*]]
1282; CHECK-NEXT:    ret i32 [[SHL]]
1283;
1284  %conv3 = zext i1 %cmp to i32
1285  %shl = shl i32 %conv3, %shamt
1286  ret i32 %shl
1287}
1288
1289define i64 @shl_zext(i32 %t) {
1290; CHECK-LABEL: @shl_zext(
1291; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[T:%.*]], 8
1292; CHECK-NEXT:    [[SHL:%.*]] = zext i32 [[TMP1]] to i64
1293; CHECK-NEXT:    ret i64 [[SHL]]
1294;
1295  %and = and i32 %t, 16777215
1296  %ext = zext i32 %and to i64
1297  %shl = shl i64 %ext, 8
1298  ret i64 %shl
1299}
1300
1301declare void @use(i64)
1302
1303define i64 @shl_zext_extra_use(i32 %t) {
1304; CHECK-LABEL: @shl_zext_extra_use(
1305; CHECK-NEXT:    [[AND:%.*]] = and i32 [[T:%.*]], 16777215
1306; CHECK-NEXT:    [[EXT:%.*]] = zext i32 [[AND]] to i64
1307; CHECK-NEXT:    call void @use(i64 [[EXT]])
1308; CHECK-NEXT:    [[SHL:%.*]] = shl nuw nsw i64 [[EXT]], 8
1309; CHECK-NEXT:    ret i64 [[SHL]]
1310;
1311  %and = and i32 %t, 16777215
1312  %ext = zext i32 %and to i64
1313  call void @use(i64 %ext)
1314  %shl = shl i64 %ext, 8
1315  ret i64 %shl
1316}
1317
1318
1319define <2 x i64> @shl_zext_splat_vec(<2 x i32> %t) {
1320; CHECK-LABEL: @shl_zext_splat_vec(
1321; CHECK-NEXT:    [[TMP1:%.*]] = shl <2 x i32> [[T:%.*]], <i32 8, i32 8>
1322; CHECK-NEXT:    [[SHL:%.*]] = zext <2 x i32> [[TMP1]] to <2 x i64>
1323; CHECK-NEXT:    ret <2 x i64> [[SHL]]
1324;
1325  %and = and <2 x i32> %t, <i32 16777215, i32 16777215>
1326  %ext = zext <2 x i32> %and to <2 x i64>
1327  %shl = shl <2 x i64> %ext, <i64 8, i64 8>
1328  ret <2 x i64> %shl
1329}
1330
1331define i64 @shl_zext_mul(i32 %t) {
1332; CHECK-LABEL: @shl_zext_mul(
1333; CHECK-NEXT:    [[MUL:%.*]] = mul i32 [[T:%.*]], 16777215
1334; CHECK-NEXT:    [[EXT:%.*]] = zext i32 [[MUL]] to i64
1335; CHECK-NEXT:    [[SHL:%.*]] = shl nuw i64 [[EXT]], 32
1336; CHECK-NEXT:    ret i64 [[SHL]]
1337;
1338  %mul = mul i32 %t, 16777215
1339  %ext = zext i32 %mul to i64
1340  %shl = shl i64 %ext, 32
1341  ret i64 %shl
1342}
1343
1344define <3 x i17> @shl_zext_mul_splat(<3 x i5> %t) {
1345; CHECK-LABEL: @shl_zext_mul_splat(
1346; CHECK-NEXT:    [[MUL:%.*]] = mul <3 x i5> [[T:%.*]], <i5 13, i5 13, i5 13>
1347; CHECK-NEXT:    [[EXT:%.*]] = zext <3 x i5> [[MUL]] to <3 x i17>
1348; CHECK-NEXT:    [[SHL:%.*]] = shl nuw <3 x i17> [[EXT]], <i17 12, i17 12, i17 12>
1349; CHECK-NEXT:    ret <3 x i17> [[SHL]]
1350;
1351  %mul = mul <3 x i5> %t, <i5 13, i5 13, i5 13>
1352  %ext = zext <3 x i5> %mul to <3 x i17>
1353  %shl = shl <3 x i17> %ext, <i17 12, i17 12, i17 12>
1354  ret <3 x i17> %shl
1355}
1356
1357define i64 @shl_zext_mul_low_shift_amount(i32 %t) {
1358; CHECK-LABEL: @shl_zext_mul_low_shift_amount(
1359; CHECK-NEXT:    [[MUL:%.*]] = mul i32 [[T:%.*]], 16777215
1360; CHECK-NEXT:    [[EXT:%.*]] = zext i32 [[MUL]] to i64
1361; CHECK-NEXT:    [[SHL:%.*]] = shl nuw nsw i64 [[EXT]], 31
1362; CHECK-NEXT:    ret i64 [[SHL]]
1363;
1364  %mul = mul i32 %t, 16777215
1365  %ext = zext i32 %mul to i64
1366  %shl = shl i64 %ext, 31
1367  ret i64 %shl
1368}
1369
1370define i64 @shl_zext_mul_extra_use1(i32 %t) {
1371; CHECK-LABEL: @shl_zext_mul_extra_use1(
1372; CHECK-NEXT:    [[MUL:%.*]] = mul i32 [[T:%.*]], 16777215
1373; CHECK-NEXT:    [[EXT:%.*]] = zext i32 [[MUL]] to i64
1374; CHECK-NEXT:    call void @use(i64 [[EXT]])
1375; CHECK-NEXT:    [[SHL:%.*]] = shl nuw i64 [[EXT]], 32
1376; CHECK-NEXT:    ret i64 [[SHL]]
1377;
1378  %mul = mul i32 %t, 16777215
1379  %ext = zext i32 %mul to i64
1380  call void @use(i64 %ext)
1381  %shl = shl i64 %ext, 32
1382  ret i64 %shl
1383}
1384
1385declare void @use_i32(i32)
1386
1387define i64 @shl_zext_mul_extra_use2(i32 %t) {
1388; CHECK-LABEL: @shl_zext_mul_extra_use2(
1389; CHECK-NEXT:    [[MUL:%.*]] = mul i32 [[T:%.*]], 16777215
1390; CHECK-NEXT:    call void @use_i32(i32 [[MUL]])
1391; CHECK-NEXT:    [[EXT:%.*]] = zext i32 [[MUL]] to i64
1392; CHECK-NEXT:    [[SHL:%.*]] = shl nuw i64 [[EXT]], 32
1393; CHECK-NEXT:    ret i64 [[SHL]]
1394;
1395  %mul = mul i32 %t, 16777215
1396  call void @use_i32(i32 %mul)
1397  %ext = zext i32 %mul to i64
1398  %shl = shl i64 %ext, 32
1399  ret i64 %shl
1400}
1401
1402define <2 x i8> @ashr_demanded_bits_splat(<2 x i8> %x) {
1403; CHECK-LABEL: @ashr_demanded_bits_splat(
1404; CHECK-NEXT:    [[SHR:%.*]] = ashr <2 x i8> [[X:%.*]], <i8 7, i8 7>
1405; CHECK-NEXT:    ret <2 x i8> [[SHR]]
1406;
1407  %and = and <2 x i8> %x, <i8 128, i8 128>
1408  %shr = ashr <2 x i8> %and, <i8 7, i8 7>
1409  ret <2 x i8> %shr
1410}
1411
1412define <2 x i8> @lshr_demanded_bits_splat(<2 x i8> %x) {
1413; CHECK-LABEL: @lshr_demanded_bits_splat(
1414; CHECK-NEXT:    [[SHR:%.*]] = lshr <2 x i8> [[X:%.*]], <i8 7, i8 7>
1415; CHECK-NEXT:    ret <2 x i8> [[SHR]]
1416;
1417  %and = and <2 x i8> %x, <i8 128, i8 128>
1418  %shr = lshr <2 x i8> %and, <i8 7, i8 7>
1419  ret <2 x i8> %shr
1420}
1421
1422; Make sure known bits works correctly with non power of 2 bit widths.
1423define i7 @test65(i7 %a, i7 %b) {
1424; CHECK-LABEL: @test65(
1425; CHECK-NEXT:    ret i7 0
1426;
1427  %shiftamt = and i7 %b, 6 ; this ensures the shift amount is even and less than the bit width.
1428  %x = lshr i7 42, %shiftamt ; 42 has a zero in every even numbered bit and a one in every odd bit.
1429  %y = and i7 %x, 1 ; this extracts the lsb which should be 0 because we shifted an even number of bits and all even bits of the shift input are 0.
1430  ret i7 %y
1431}
1432
1433define i32 @shl_select_add_true(i32 %x, i1 %cond) {
1434; CHECK-LABEL: @shl_select_add_true(
1435; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 1
1436; CHECK-NEXT:    [[TMP2:%.*]] = add i32 [[TMP1]], 14
1437; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP2]], i32 [[TMP1]]
1438; CHECK-NEXT:    ret i32 [[TMP3]]
1439;
1440  %1 = add i32 %x, 7
1441  %2 = select i1 %cond, i32 %1, i32 %x
1442  %3 = shl i32 %2, 1
1443  ret i32 %3
1444}
1445
1446define i32 @shl_select_add_false(i32 %x, i1 %cond) {
1447; CHECK-LABEL: @shl_select_add_false(
1448; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 1
1449; CHECK-NEXT:    [[TMP2:%.*]] = add i32 [[TMP1]], 14
1450; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP1]], i32 [[TMP2]]
1451; CHECK-NEXT:    ret i32 [[TMP3]]
1452;
1453  %1 = add i32 %x, 7
1454  %2 = select i1 %cond, i32 %x, i32 %1
1455  %3 = shl i32 %2, 1
1456  ret i32 %3
1457}
1458
1459define i32 @shl_select_and_true(i32 %x, i1 %cond) {
1460; CHECK-LABEL: @shl_select_and_true(
1461; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 1
1462; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], 14
1463; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP2]], i32 [[TMP1]]
1464; CHECK-NEXT:    ret i32 [[TMP3]]
1465;
1466  %1 = and i32 %x, 7
1467  %2 = select i1 %cond, i32 %1, i32 %x
1468  %3 = shl i32 %2, 1
1469  ret i32 %3
1470}
1471
1472define i32 @shl_select_and_false(i32 %x, i1 %cond) {
1473; CHECK-LABEL: @shl_select_and_false(
1474; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 1
1475; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], 14
1476; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP1]], i32 [[TMP2]]
1477; CHECK-NEXT:    ret i32 [[TMP3]]
1478;
1479  %1 = and i32 %x, 7
1480  %2 = select i1 %cond, i32 %x, i32 %1
1481  %3 = shl i32 %2, 1
1482  ret i32 %3
1483}
1484
1485define i32 @lshr_select_and_true(i32 %x, i1 %cond) {
1486; CHECK-LABEL: @lshr_select_and_true(
1487; CHECK-NEXT:    [[TMP1:%.*]] = lshr i32 [[X:%.*]], 1
1488; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], 3
1489; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP2]], i32 [[TMP1]]
1490; CHECK-NEXT:    ret i32 [[TMP3]]
1491;
1492  %1 = and i32 %x, 7
1493  %2 = select i1 %cond, i32 %1, i32 %x
1494  %3 = lshr i32 %2, 1
1495  ret i32 %3
1496}
1497
1498define i32 @lshr_select_and_false(i32 %x, i1 %cond) {
1499; CHECK-LABEL: @lshr_select_and_false(
1500; CHECK-NEXT:    [[TMP1:%.*]] = lshr i32 [[X:%.*]], 1
1501; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], 3
1502; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP1]], i32 [[TMP2]]
1503; CHECK-NEXT:    ret i32 [[TMP3]]
1504;
1505  %1 = and i32 %x, 7
1506  %2 = select i1 %cond, i32 %x, i32 %1
1507  %3 = lshr i32 %2, 1
1508  ret i32 %3
1509}
1510
1511define i32 @ashr_select_and_true(i32 %x, i1 %cond) {
1512; CHECK-LABEL: @ashr_select_and_true(
1513; CHECK-NEXT:    [[TMP1:%.*]] = ashr i32 [[X:%.*]], 1
1514; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], -1073741821
1515; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP2]], i32 [[TMP1]]
1516; CHECK-NEXT:    ret i32 [[TMP3]]
1517;
1518  %1 = and i32 %x, 2147483655
1519  %2 = select i1 %cond, i32 %1, i32 %x
1520  %3 = ashr i32 %2, 1
1521  ret i32 %3
1522}
1523
1524define i32 @ashr_select_and_false(i32 %x, i1 %cond) {
1525; CHECK-LABEL: @ashr_select_and_false(
1526; CHECK-NEXT:    [[TMP1:%.*]] = ashr i32 [[X:%.*]], 1
1527; CHECK-NEXT:    [[TMP2:%.*]] = and i32 [[TMP1]], -1073741821
1528; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP1]], i32 [[TMP2]]
1529; CHECK-NEXT:    ret i32 [[TMP3]]
1530;
1531  %1 = and i32 %x, 2147483655
1532  %2 = select i1 %cond, i32 %x, i32 %1
1533  %3 = ashr i32 %2, 1
1534  ret i32 %3
1535}
1536
1537define i32 @shl_select_or_true(i32 %x, i1 %cond) {
1538; CHECK-LABEL: @shl_select_or_true(
1539; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 1
1540; CHECK-NEXT:    [[TMP2:%.*]] = or i32 [[TMP1]], 14
1541; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP2]], i32 [[TMP1]]
1542; CHECK-NEXT:    ret i32 [[TMP3]]
1543;
1544  %1 = or i32 %x, 7
1545  %2 = select i1 %cond, i32 %1, i32 %x
1546  %3 = shl i32 %2, 1
1547  ret i32 %3
1548}
1549
1550define i32 @shl_select_or_false(i32 %x, i1 %cond) {
1551; CHECK-LABEL: @shl_select_or_false(
1552; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 1
1553; CHECK-NEXT:    [[TMP2:%.*]] = or i32 [[TMP1]], 14
1554; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP1]], i32 [[TMP2]]
1555; CHECK-NEXT:    ret i32 [[TMP3]]
1556;
1557  %1 = or i32 %x, 7
1558  %2 = select i1 %cond, i32 %x, i32 %1
1559  %3 = shl i32 %2, 1
1560  ret i32 %3
1561}
1562
1563define i32 @lshr_select_or_true(i32 %x, i1 %cond) {
1564; CHECK-LABEL: @lshr_select_or_true(
1565; CHECK-NEXT:    [[TMP1:%.*]] = lshr i32 [[X:%.*]], 1
1566; CHECK-NEXT:    [[TMP2:%.*]] = or i32 [[TMP1]], 3
1567; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP2]], i32 [[TMP1]]
1568; CHECK-NEXT:    ret i32 [[TMP3]]
1569;
1570  %1 = or i32 %x, 7
1571  %2 = select i1 %cond, i32 %1, i32 %x
1572  %3 = lshr i32 %2, 1
1573  ret i32 %3
1574}
1575
1576define i32 @lshr_select_or_false(i32 %x, i1 %cond) {
1577; CHECK-LABEL: @lshr_select_or_false(
1578; CHECK-NEXT:    [[TMP1:%.*]] = lshr i32 [[X:%.*]], 1
1579; CHECK-NEXT:    [[TMP2:%.*]] = or i32 [[TMP1]], 3
1580; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP1]], i32 [[TMP2]]
1581; CHECK-NEXT:    ret i32 [[TMP3]]
1582;
1583  %1 = or i32 %x, 7
1584  %2 = select i1 %cond, i32 %x, i32 %1
1585  %3 = lshr i32 %2, 1
1586  ret i32 %3
1587}
1588
1589define i32 @ashr_select_or_true(i32 %x, i1 %cond) {
1590; CHECK-LABEL: @ashr_select_or_true(
1591; CHECK-NEXT:    [[TMP1:%.*]] = ashr i32 [[X:%.*]], 1
1592; CHECK-NEXT:    [[TMP2:%.*]] = or i32 [[TMP1]], 3
1593; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP2]], i32 [[TMP1]]
1594; CHECK-NEXT:    ret i32 [[TMP3]]
1595;
1596  %1 = or i32 %x, 7
1597  %2 = select i1 %cond, i32 %1, i32 %x
1598  %3 = ashr i32 %2, 1
1599  ret i32 %3
1600}
1601
1602define i32 @ashr_select_or_false(i32 %x, i1 %cond) {
1603; CHECK-LABEL: @ashr_select_or_false(
1604; CHECK-NEXT:    [[TMP1:%.*]] = ashr i32 [[X:%.*]], 1
1605; CHECK-NEXT:    [[TMP2:%.*]] = or i32 [[TMP1]], 3
1606; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP1]], i32 [[TMP2]]
1607; CHECK-NEXT:    ret i32 [[TMP3]]
1608;
1609  %1 = or i32 %x, 7
1610  %2 = select i1 %cond, i32 %x, i32 %1
1611  %3 = ashr i32 %2, 1
1612  ret i32 %3
1613}
1614
1615define i32 @shl_select_xor_true(i32 %x, i1 %cond) {
1616; CHECK-LABEL: @shl_select_xor_true(
1617; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 1
1618; CHECK-NEXT:    [[TMP2:%.*]] = xor i32 [[TMP1]], 14
1619; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP2]], i32 [[TMP1]]
1620; CHECK-NEXT:    ret i32 [[TMP3]]
1621;
1622  %1 = xor i32 %x, 7
1623  %2 = select i1 %cond, i32 %1, i32 %x
1624  %3 = shl i32 %2, 1
1625  ret i32 %3
1626}
1627
1628define i32 @shl_select_xor_false(i32 %x, i1 %cond) {
1629; CHECK-LABEL: @shl_select_xor_false(
1630; CHECK-NEXT:    [[TMP1:%.*]] = shl i32 [[X:%.*]], 1
1631; CHECK-NEXT:    [[TMP2:%.*]] = xor i32 [[TMP1]], 14
1632; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP1]], i32 [[TMP2]]
1633; CHECK-NEXT:    ret i32 [[TMP3]]
1634;
1635  %1 = xor i32 %x, 7
1636  %2 = select i1 %cond, i32 %x, i32 %1
1637  %3 = shl i32 %2, 1
1638  ret i32 %3
1639}
1640
1641define i32 @lshr_select_xor_true(i32 %x, i1 %cond) {
1642; CHECK-LABEL: @lshr_select_xor_true(
1643; CHECK-NEXT:    [[TMP1:%.*]] = lshr i32 [[X:%.*]], 1
1644; CHECK-NEXT:    [[TMP2:%.*]] = xor i32 [[TMP1]], 3
1645; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP2]], i32 [[TMP1]]
1646; CHECK-NEXT:    ret i32 [[TMP3]]
1647;
1648  %1 = xor i32 %x, 7
1649  %2 = select i1 %cond, i32 %1, i32 %x
1650  %3 = lshr i32 %2, 1
1651  ret i32 %3
1652}
1653
1654define i32 @lshr_select_xor_false(i32 %x, i1 %cond) {
1655; CHECK-LABEL: @lshr_select_xor_false(
1656; CHECK-NEXT:    [[TMP1:%.*]] = lshr i32 [[X:%.*]], 1
1657; CHECK-NEXT:    [[TMP2:%.*]] = xor i32 [[TMP1]], 3
1658; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP1]], i32 [[TMP2]]
1659; CHECK-NEXT:    ret i32 [[TMP3]]
1660;
1661  %1 = xor i32 %x, 7
1662  %2 = select i1 %cond, i32 %x, i32 %1
1663  %3 = lshr i32 %2, 1
1664  ret i32 %3
1665}
1666
1667define i32 @ashr_select_xor_true(i32 %x, i1 %cond) {
1668; CHECK-LABEL: @ashr_select_xor_true(
1669; CHECK-NEXT:    [[TMP1:%.*]] = ashr i32 [[X:%.*]], 1
1670; CHECK-NEXT:    [[TMP2:%.*]] = xor i32 [[TMP1]], 3
1671; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP2]], i32 [[TMP1]]
1672; CHECK-NEXT:    ret i32 [[TMP3]]
1673;
1674  %1 = xor i32 %x, 7
1675  %2 = select i1 %cond, i32 %1, i32 %x
1676  %3 = ashr i32 %2, 1
1677  ret i32 %3
1678}
1679
1680define i32 @ashr_select_xor_false(i32 %x, i1 %cond) {
1681; CHECK-LABEL: @ashr_select_xor_false(
1682; CHECK-NEXT:    [[TMP1:%.*]] = ashr i32 [[X:%.*]], 1
1683; CHECK-NEXT:    [[TMP2:%.*]] = xor i32 [[TMP1]], 3
1684; CHECK-NEXT:    [[TMP3:%.*]] = select i1 [[COND:%.*]], i32 [[TMP1]], i32 [[TMP2]]
1685; CHECK-NEXT:    ret i32 [[TMP3]]
1686;
1687  %1 = xor i32 %x, 7
1688  %2 = select i1 %cond, i32 %x, i32 %1
1689  %3 = ashr i32 %2, 1
1690  ret i32 %3
1691}
1692
1693; OSS Fuzz #4871
1694; https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=4871
1695define i177 @lshr_out_of_range(i177 %Y, i177** %A2) {
1696; CHECK-LABEL: @lshr_out_of_range(
1697; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i177 [[Y:%.*]], -1
1698; CHECK-NEXT:    [[B4:%.*]] = sext i1 [[TMP1]] to i177
1699; CHECK-NEXT:    [[C8:%.*]] = icmp ult i177 [[B4]], [[Y]]
1700; CHECK-NEXT:    [[TMP2:%.*]] = sext i1 [[C8]] to i64
1701; CHECK-NEXT:    [[G18:%.*]] = getelementptr i177*, i177** [[A2:%.*]], i64 [[TMP2]]
1702; CHECK-NEXT:    store i177** [[G18]], i177*** undef, align 8
1703; CHECK-NEXT:    ret i177 0
1704;
1705  %B5 = udiv i177 %Y, -1
1706  %B4 = add i177 %B5, -1
1707  %B2 = add i177 %B4, -1
1708  %B6 = mul i177 %B5, %B2
1709  %B3 = add i177 %B2, %B2
1710  %B10 = sub i177 %B5, %B3
1711  %B12 = lshr i177 %Y, %B6
1712  %C8 = icmp ugt i177 %B12, %B4
1713  %G18 = getelementptr i177*, i177** %A2, i1 %C8
1714  store i177** %G18, i177*** undef
1715  %B1 = udiv i177 %B10, %B6
1716  ret i177 %B1
1717}
1718
1719; OSS Fuzz #26716
1720; https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=26716
1721define i177 @lshr_out_of_range2(i177 %Y, i177** %A2) {
1722; CHECK-LABEL: @lshr_out_of_range2(
1723; CHECK-NEXT:    store i177** [[A2:%.*]], i177*** undef, align 8
1724; CHECK-NEXT:    ret i177 0
1725;
1726  %B5 = udiv i177 %Y, -1
1727  %B = sdiv i177 %B5, -1
1728  %B4 = add i177 %B5, %B
1729  %B2 = add i177 %B4, -1
1730  %B6 = mul i177 %B5, %B2
1731  %B12 = lshr i177 %Y, %B6
1732  %C8 = icmp ugt i177 %B12, %B4
1733  %G18 = getelementptr i177*, i177** %A2, i1 %C8
1734  store i177** %G18, i177*** undef, align 8
1735  %B1 = udiv i177 %B5, %B6
1736  ret i177 %B1
1737}
1738
1739; OSS Fuzz #5032
1740; https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=5032
1741define void @ashr_out_of_range(i177* %A) {
1742; CHECK-LABEL: @ashr_out_of_range(
1743; CHECK-NEXT:    [[L:%.*]] = load i177, i177* [[A:%.*]], align 4
1744; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i177 [[L]], -1
1745; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i64 -1, i64 -2
1746; CHECK-NEXT:    [[G11:%.*]] = getelementptr i177, i177* [[A]], i64 [[TMP2]]
1747; CHECK-NEXT:    [[L7:%.*]] = load i177, i177* [[G11]], align 4
1748; CHECK-NEXT:    [[B36:%.*]] = select i1 [[TMP1]], i177 0, i177 [[L7]]
1749; CHECK-NEXT:    [[C17:%.*]] = icmp sgt i177 [[B36]], [[L7]]
1750; CHECK-NEXT:    [[TMP3:%.*]] = sext i1 [[C17]] to i64
1751; CHECK-NEXT:    [[G62:%.*]] = getelementptr i177, i177* [[G11]], i64 [[TMP3]]
1752; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i177 [[L7]], -1
1753; CHECK-NEXT:    [[B28:%.*]] = select i1 [[TMP4]], i177 0, i177 [[L7]]
1754; CHECK-NEXT:    store i177 [[B28]], i177* [[G62]], align 4
1755; CHECK-NEXT:    ret void
1756;
1757  %L = load i177, i177* %A
1758  %B5 = udiv i177 %L, -1
1759  %B4 = add i177 %B5, -1
1760  %B2 = add i177 %B4, -1
1761  %G11 = getelementptr i177, i177* %A, i177 %B2
1762  %L7 = load i177, i177* %G11
1763  %B6 = mul i177 %B5, %B2
1764  %B24 = ashr i177 %L7, %B6
1765  %B36 = and i177 %L7, %B4
1766  %C17 = icmp sgt i177 %B36, %B24
1767  %G62 = getelementptr i177, i177* %G11, i1 %C17
1768  %B28 = urem i177 %B24, %B6
1769  store i177 %B28, i177* %G62
1770  ret void
1771}
1772
1773; OSS Fuzz #26135
1774; https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=26135
1775define void @ashr_out_of_range_1(i177* %A) {
1776; CHECK-LABEL: @ashr_out_of_range_1(
1777; CHECK-NEXT:    [[L:%.*]] = load i177, i177* [[A:%.*]], align 4
1778; CHECK-NEXT:    [[G11:%.*]] = getelementptr i177, i177* [[A]], i64 -1
1779; CHECK-NEXT:    [[B24_LOBIT:%.*]] = ashr i177 [[L]], 175
1780; CHECK-NEXT:    [[TMP1:%.*]] = trunc i177 [[B24_LOBIT]] to i64
1781; CHECK-NEXT:    [[G62:%.*]] = getelementptr i177, i177* [[G11]], i64 [[TMP1]]
1782; CHECK-NEXT:    store i177 0, i177* [[G62]], align 4
1783; CHECK-NEXT:    ret void
1784;
1785  %L = load i177, i177* %A, align 4
1786  %B5 = udiv i177 %L, -1
1787  %B4 = add i177 %B5, -1
1788  %B = and i177 %B4, %L
1789  %B2 = add i177 %B, -1
1790  %G11 = getelementptr i177, i177* %A, i177 %B2
1791  %B6 = mul i177 %B5, %B2
1792  %B24 = ashr i177 %L, %B6
1793  %C17 = icmp sgt i177 %B, %B24
1794  %G62 = getelementptr i177, i177* %G11, i1 %C17
1795  %B28 = urem i177 %B24, %B6
1796  store i177 %B28, i177* %G62, align 4
1797  ret void
1798}
1799