1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3
4define <4 x float> @test1(<4 x float> %v1) {
5; CHECK-LABEL: @test1(
6; CHECK-NEXT:    ret <4 x float> [[V1:%.*]]
7;
8  %v2 = shufflevector <4 x float> %v1, <4 x float> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
9  ret <4 x float> %v2
10}
11
12define <4 x float> @test2(<4 x float> %v1) {
13; CHECK-LABEL: @test2(
14; CHECK-NEXT:    ret <4 x float> [[V1:%.*]]
15;
16  %v2 = shufflevector <4 x float> %v1, <4 x float> %v1, <4 x i32> <i32 0, i32 5, i32 2, i32 7>
17  ret <4 x float> %v2
18}
19
20define float @test3(<4 x float> %A, <4 x float> %B, float %f) {
21; CHECK-LABEL: @test3(
22; CHECK-NEXT:    ret float [[F:%.*]]
23;
24  %C = insertelement <4 x float> %A, float %f, i32 0
25  %D = shufflevector <4 x float> %C, <4 x float> %B, <4 x i32> <i32 5, i32 0, i32 2, i32 7>
26  %E = extractelement <4 x float> %D, i32 1
27  ret float %E
28}
29
30define i32 @test4(<4 x i32> %X) {
31; CHECK-LABEL: @test4(
32; CHECK-NEXT:    [[R:%.*]] = extractelement <4 x i32> [[X:%.*]], i64 0
33; CHECK-NEXT:    ret i32 [[R]]
34;
35  %t = shufflevector <4 x i32> %X, <4 x i32> undef, <4 x i32> zeroinitializer
36  %r = extractelement <4 x i32> %t, i32 0
37  ret i32 %r
38}
39
40define i32 @test5(<4 x i32> %X) {
41; CHECK-LABEL: @test5(
42; CHECK-NEXT:    [[R:%.*]] = extractelement <4 x i32> [[X:%.*]], i64 3
43; CHECK-NEXT:    ret i32 [[R]]
44;
45  %t = shufflevector <4 x i32> %X, <4 x i32> undef, <4 x i32> <i32 3, i32 2, i32 undef, i32 undef>
46  %r = extractelement <4 x i32> %t, i32 0
47  ret i32 %r
48}
49
50define float @test6(<4 x float> %X) {
51; CHECK-LABEL: @test6(
52; CHECK-NEXT:    [[R:%.*]] = extractelement <4 x float> [[X:%.*]], i64 0
53; CHECK-NEXT:    ret float [[R]]
54;
55  %X1 = bitcast <4 x float> %X to <4 x i32>
56  %t = shufflevector <4 x i32> %X1, <4 x i32> undef, <4 x i32> zeroinitializer
57  %t2 = bitcast <4 x i32> %t to <4 x float>
58  %r = extractelement <4 x float> %t2, i32 0
59  ret float %r
60}
61
62define float @testvscale6(<vscale x 4 x float> %X) {
63; CHECK-LABEL: @testvscale6(
64; CHECK-NEXT:    [[T2:%.*]] = shufflevector <vscale x 4 x float> [[X:%.*]], <vscale x 4 x float> undef, <vscale x 4 x i32> zeroinitializer
65; CHECK-NEXT:    [[R:%.*]] = extractelement <vscale x 4 x float> [[T2]], i64 0
66; CHECK-NEXT:    ret float [[R]]
67;
68  %X1 = bitcast <vscale x 4 x float> %X to <vscale x 4 x i32>
69  %t = shufflevector <vscale x 4 x i32> %X1, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer
70  %t2 = bitcast <vscale x 4 x i32> %t to <vscale x 4 x float>
71  %r = extractelement <vscale x 4 x float> %t2, i32 0
72  ret float %r
73}
74
75
76define <4 x float> @test7(<4 x float> %x) {
77; CHECK-LABEL: @test7(
78; CHECK-NEXT:    [[R:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
79; CHECK-NEXT:    ret <4 x float> [[R]]
80;
81  %r = shufflevector <4 x float> %x, <4 x float> undef, <4 x i32> < i32 0, i32 1, i32 6, i32 7 >
82  ret <4 x float> %r
83}
84
85; This should turn into a single shuffle.
86define <4 x float> @test8(<4 x float> %x, <4 x float> %y) {
87; CHECK-LABEL: @test8(
88; CHECK-NEXT:    [[T134:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> [[Y:%.*]], <4 x i32> <i32 1, i32 undef, i32 3, i32 4>
89; CHECK-NEXT:    ret <4 x float> [[T134]]
90;
91  %t4 = extractelement <4 x float> %x, i32 1
92  %t2 = extractelement <4 x float> %x, i32 3
93  %t1 = extractelement <4 x float> %y, i32 0
94  %t128 = insertelement <4 x float> undef, float %t4, i32 0
95  %t130 = insertelement <4 x float> %t128, float undef, i32 1
96  %t132 = insertelement <4 x float> %t130, float %t2, i32 2
97  %t134 = insertelement <4 x float> %t132, float %t1, i32 3
98  ret <4 x float> %t134
99}
100
101; Test fold of two shuffles where the first shuffle vectors inputs are a
102; different length then the second.
103define <4 x i8> @test9(<16 x i8> %t6) {
104; CHECK-LABEL: @test9(
105; CHECK-NEXT:    [[T9:%.*]] = shufflevector <16 x i8> [[T6:%.*]], <16 x i8> undef, <4 x i32> <i32 13, i32 9, i32 4, i32 13>
106; CHECK-NEXT:    ret <4 x i8> [[T9]]
107;
108  %t7 = shufflevector <16 x i8> %t6, <16 x i8> undef, <4 x i32> < i32 13, i32 9, i32 4, i32 13 >
109  %t9 = shufflevector <4 x i8> %t7, <4 x i8> undef, <4 x i32> < i32 3, i32 1, i32 2, i32 0 >
110  ret <4 x i8> %t9
111}
112
113; Same as test9, but make sure that "undef" mask values are not confused with
114; mask values of 2*N, where N is the mask length.  These shuffles should not
115; be folded (because [8,9,4,8] may not be a mask supported by the target).
116
117define <4 x i8> @test9a(<16 x i8> %t6) {
118; CHECK-LABEL: @test9a(
119; CHECK-NEXT:    [[T7:%.*]] = shufflevector <16 x i8> [[T6:%.*]], <16 x i8> undef, <4 x i32> <i32 undef, i32 9, i32 4, i32 8>
120; CHECK-NEXT:    [[T9:%.*]] = shufflevector <4 x i8> [[T7]], <4 x i8> undef, <4 x i32> <i32 3, i32 1, i32 2, i32 undef>
121; CHECK-NEXT:    ret <4 x i8> [[T9]]
122;
123  %t7 = shufflevector <16 x i8> %t6, <16 x i8> undef, <4 x i32> < i32 undef, i32 9, i32 4, i32 8 >
124  %t9 = shufflevector <4 x i8> %t7, <4 x i8> undef, <4 x i32> < i32 3, i32 1, i32 2, i32 0 >
125  ret <4 x i8> %t9
126}
127
128; Test fold of two shuffles where the first shuffle vectors inputs are a
129; different length then the second.
130define <4 x i8> @test9b(<4 x i8> %t6, <4 x i8> %t7) {
131; CHECK-LABEL: @test9b(
132; CHECK-NEXT:    [[T9:%.*]] = shufflevector <4 x i8> [[T6:%.*]], <4 x i8> [[T7:%.*]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
133; CHECK-NEXT:    ret <4 x i8> [[T9]]
134;
135  %t1 = shufflevector <4 x i8> %t6, <4 x i8> %t7, <8 x i32> <i32 0, i32 1, i32 4, i32 5, i32 4, i32 5, i32 2, i32 3>
136  %t9 = shufflevector <8 x i8> %t1, <8 x i8> undef, <4 x i32> <i32 0, i32 1, i32 4, i32 5>
137  ret <4 x i8> %t9
138}
139
140; Redundant vector splats should be removed.  Radar 8597790.
141define <4 x i32> @test10(<4 x i32> %t5) {
142; CHECK-LABEL: @test10(
143; CHECK-NEXT:    [[T7:%.*]] = shufflevector <4 x i32> [[T5:%.*]], <4 x i32> undef, <4 x i32> <i32 1, i32 1, i32 1, i32 1>
144; CHECK-NEXT:    ret <4 x i32> [[T7]]
145;
146  %t6 = shufflevector <4 x i32> %t5, <4 x i32> undef, <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef>
147  %t7 = shufflevector <4 x i32> %t6, <4 x i32> undef, <4 x i32> zeroinitializer
148  ret <4 x i32> %t7
149}
150
151; Test fold of two shuffles where the two shufflevector inputs's op1 are the same.
152
153define <8 x i8> @test11(<16 x i8> %t6) {
154; CHECK-LABEL: @test11(
155; CHECK-NEXT:    [[T3:%.*]] = shufflevector <16 x i8> [[T6:%.*]], <16 x i8> undef, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
156; CHECK-NEXT:    ret <8 x i8> [[T3]]
157;
158  %t1 = shufflevector <16 x i8> %t6, <16 x i8> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
159  %t2 = shufflevector <16 x i8> %t6, <16 x i8> undef, <4 x i32> <i32 4, i32 5, i32 6, i32 7>
160  %t3 = shufflevector <4 x i8> %t1, <4 x i8> %t2, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
161  ret <8 x i8> %t3
162}
163
164; Test fold of two shuffles where the first shufflevector's inputs are the same as the second.
165
166define <8 x i8> @test12(<8 x i8> %t6, <8 x i8> %t2) {
167; CHECK-LABEL: @test12(
168; CHECK-NEXT:    [[T3:%.*]] = shufflevector <8 x i8> [[T6:%.*]], <8 x i8> [[T2:%.*]], <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 9, i32 8, i32 11, i32 12>
169; CHECK-NEXT:    ret <8 x i8> [[T3]]
170;
171  %t1 = shufflevector <8 x i8> %t6, <8 x i8> undef, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 5, i32 4, i32 undef, i32 7>
172  %t3 = shufflevector <8 x i8> %t1, <8 x i8> %t2, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 9, i32 8, i32 11, i32 12>
173  ret <8 x i8> %t3
174}
175
176; Test fold of two shuffles where the first shufflevector's inputs are the same as the second.
177
178define <8 x i8> @test12a(<8 x i8> %t6, <8 x i8> %t2) {
179; CHECK-LABEL: @test12a(
180; CHECK-NEXT:    [[T3:%.*]] = shufflevector <8 x i8> [[T2:%.*]], <8 x i8> [[T6:%.*]], <8 x i32> <i32 0, i32 3, i32 1, i32 4, i32 8, i32 9, i32 10, i32 11>
181; CHECK-NEXT:    ret <8 x i8> [[T3]]
182;
183  %t1 = shufflevector <8 x i8> %t6, <8 x i8> undef, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 5, i32 4, i32 undef, i32 7>
184  %t3 = shufflevector <8 x i8> %t2, <8 x i8> %t1, <8 x i32> <i32 0, i32 3, i32 1, i32 4, i32 8, i32 9, i32 10, i32 11>
185  ret <8 x i8> %t3
186}
187
188; The mask length of the 1st shuffle can be reduced to eliminate the 2nd shuffle.
189
190define <2 x i8> @extract_subvector_of_shuffle(<2 x i8> %x, <2 x i8> %y) {
191; CHECK-LABEL: @extract_subvector_of_shuffle(
192; CHECK-NEXT:    [[EXTRACT_SUBV:%.*]] = shufflevector <2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]], <2 x i32> <i32 0, i32 2>
193; CHECK-NEXT:    ret <2 x i8> [[EXTRACT_SUBV]]
194;
195  %shuf = shufflevector <2 x i8> %x, <2 x i8> %y, <3 x i32> <i32 0, i32 2, i32 0>
196  %extract_subv = shufflevector <3 x i8> %shuf, <3 x i8> undef, <2 x i32> <i32 0, i32 1>
197  ret <2 x i8> %extract_subv
198}
199
200; Undef elements in either mask are ok. Undefs from the 2nd shuffle mask should propagate to the new shuffle.
201; The type of the inputs does not have to match the output type.
202
203define <4 x i8> @extract_subvector_of_shuffle_undefs_types(<2 x i8> %x, <2 x i8> %y) {
204; CHECK-LABEL: @extract_subvector_of_shuffle_undefs_types(
205; CHECK-NEXT:    [[EXTRACT_SUBV:%.*]] = shufflevector <2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]], <4 x i32> <i32 undef, i32 2, i32 0, i32 undef>
206; CHECK-NEXT:    ret <4 x i8> [[EXTRACT_SUBV]]
207;
208  %shuf = shufflevector <2 x i8> %x, <2 x i8> %y, <5 x i32> <i32 undef, i32 2, i32 0, i32 1, i32 0>
209  %extract_subv = shufflevector <5 x i8> %shuf, <5 x i8> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
210  ret <4 x i8> %extract_subv
211}
212
213; Extra uses are not ok - we only do the transform when we can eliminate an instruction.
214
215declare void @use_v5i8(<5 x i8>)
216
217define <4 x i8> @extract_subvector_of_shuffle_extra_use(<2 x i8> %x, <2 x i8> %y) {
218; CHECK-LABEL: @extract_subvector_of_shuffle_extra_use(
219; CHECK-NEXT:    [[SHUF:%.*]] = shufflevector <2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]], <5 x i32> <i32 undef, i32 2, i32 0, i32 1, i32 0>
220; CHECK-NEXT:    call void @use_v5i8(<5 x i8> [[SHUF]])
221; CHECK-NEXT:    [[EXTRACT_SUBV:%.*]] = shufflevector <5 x i8> [[SHUF]], <5 x i8> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
222; CHECK-NEXT:    ret <4 x i8> [[EXTRACT_SUBV]]
223;
224  %shuf = shufflevector <2 x i8> %x, <2 x i8> %y, <5 x i32> <i32 undef, i32 2, i32 0, i32 1, i32 0>
225  call void @use_v5i8(<5 x i8> %shuf)
226  %extract_subv = shufflevector <5 x i8> %shuf, <5 x i8> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
227  ret <4 x i8> %extract_subv
228}
229
230define <2 x i8> @test13a(i8 %x1, i8 %x2) {
231; CHECK-LABEL: @test13a(
232; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <2 x i8> undef, i8 [[X1:%.*]], i64 1
233; CHECK-NEXT:    [[TMP2:%.*]] = insertelement <2 x i8> [[TMP1]], i8 [[X2:%.*]], i64 0
234; CHECK-NEXT:    [[TMP3:%.*]] = add <2 x i8> [[TMP2]], <i8 7, i8 5>
235; CHECK-NEXT:    ret <2 x i8> [[TMP3]]
236;
237  %A = insertelement <2 x i8> undef, i8 %x1, i32 0
238  %B = insertelement <2 x i8> %A, i8 %x2, i32 1
239  %C = add <2 x i8> %B, <i8 5, i8 7>
240  %D = shufflevector <2 x i8> %C, <2 x i8> undef, <2 x i32> <i32 1, i32 0>
241  ret <2 x i8> %D
242}
243
244; Increasing length of vector ops is not a good canonicalization.
245
246define <3 x i32> @add_wider(i32 %y, i32 %z) {
247; CHECK-LABEL: @add_wider(
248; CHECK-NEXT:    [[I0:%.*]] = insertelement <2 x i32> undef, i32 [[Y:%.*]], i64 0
249; CHECK-NEXT:    [[I1:%.*]] = insertelement <2 x i32> [[I0]], i32 [[Z:%.*]], i64 1
250; CHECK-NEXT:    [[A:%.*]] = add <2 x i32> [[I1]], <i32 255, i32 255>
251; CHECK-NEXT:    [[EXT:%.*]] = shufflevector <2 x i32> [[A]], <2 x i32> undef, <3 x i32> <i32 0, i32 1, i32 undef>
252; CHECK-NEXT:    ret <3 x i32> [[EXT]]
253;
254  %i0 = insertelement <2 x i32> undef, i32 %y, i32 0
255  %i1 = insertelement <2 x i32> %i0, i32 %z, i32 1
256  %a = add <2 x i32> %i1, <i32 255, i32 255>
257  %ext = shufflevector <2 x i32> %a, <2 x i32> undef, <3 x i32> <i32 0, i32 1, i32 undef>
258  ret <3 x i32> %ext
259}
260
261; Increasing length of vector ops must be safe from illegal undef propagation.
262
263define <3 x i32> @div_wider(i32 %y, i32 %z) {
264; CHECK-LABEL: @div_wider(
265; CHECK-NEXT:    [[I0:%.*]] = insertelement <2 x i32> undef, i32 [[Y:%.*]], i64 0
266; CHECK-NEXT:    [[I1:%.*]] = insertelement <2 x i32> [[I0]], i32 [[Z:%.*]], i64 1
267; CHECK-NEXT:    [[A:%.*]] = sdiv <2 x i32> [[I1]], <i32 255, i32 255>
268; CHECK-NEXT:    [[EXT:%.*]] = shufflevector <2 x i32> [[A]], <2 x i32> undef, <3 x i32> <i32 0, i32 1, i32 undef>
269; CHECK-NEXT:    ret <3 x i32> [[EXT]]
270;
271  %i0 = insertelement <2 x i32> undef, i32 %y, i32 0
272  %i1 = insertelement <2 x i32> %i0, i32 %z, i32 1
273  %a = sdiv <2 x i32> %i1, <i32 255, i32 255>
274  %ext = shufflevector <2 x i32> %a, <2 x i32> undef, <3 x i32> <i32 0, i32 1, i32 undef>
275  ret <3 x i32> %ext
276}
277
278; Increasing length of insertelements (no math ops) is a good canonicalization.
279
280define <3 x i8> @fold_inselts_with_widening_shuffle(i8 %x, i8 %y) {
281; CHECK-LABEL: @fold_inselts_with_widening_shuffle(
282; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <3 x i8> undef, i8 [[X:%.*]], i64 0
283; CHECK-NEXT:    [[TMP2:%.*]] = insertelement <3 x i8> [[TMP1]], i8 [[Y:%.*]], i64 1
284; CHECK-NEXT:    ret <3 x i8> [[TMP2]]
285;
286  %ins0 = insertelement <2 x i8> undef, i8 %x, i32 0
287  %ins1 = insertelement <2 x i8> %ins0, i8 %y, i32 1
288  %widen = shufflevector <2 x i8> %ins1, <2 x i8> undef, <3 x i32> <i32 0, i32 1, i32 undef>
289  ret <3 x i8> %widen
290}
291
292define <2 x i8> @test13b(i8 %x) {
293; CHECK-LABEL: @test13b(
294; CHECK-NEXT:    [[B:%.*]] = insertelement <2 x i8> undef, i8 [[X:%.*]], i64 1
295; CHECK-NEXT:    ret <2 x i8> [[B]]
296;
297  %A = insertelement <2 x i8> undef, i8 %x, i32 0
298  %B = shufflevector <2 x i8> %A, <2 x i8> undef, <2 x i32> <i32 undef, i32 0>
299  ret <2 x i8> %B
300}
301
302define <2 x i8> @test13c(i8 %x1, i8 %x2) {
303; CHECK-LABEL: @test13c(
304; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <2 x i8> undef, i8 [[X1:%.*]], i64 0
305; CHECK-NEXT:    [[TMP2:%.*]] = insertelement <2 x i8> [[TMP1]], i8 [[X2:%.*]], i64 1
306; CHECK-NEXT:    ret <2 x i8> [[TMP2]]
307;
308  %A = insertelement <4 x i8> undef, i8 %x1, i32 0
309  %B = insertelement <4 x i8> %A, i8 %x2, i32 2
310  %C = shufflevector <4 x i8> %B, <4 x i8> undef, <2 x i32> <i32 0, i32 2>
311  ret <2 x i8> %C
312}
313
314define void @test14(i16 %conv10) {
315; CHECK-LABEL: @test14(
316; CHECK-NEXT:    store <4 x i16> <i16 undef, i16 undef, i16 undef, i16 23>, <4 x i16>* undef, align 8
317; CHECK-NEXT:    ret void
318;
319  %t = alloca <4 x i16>, align 8
320  %vecinit6 = insertelement <4 x i16> undef, i16 23, i32 3
321  store <4 x i16> %vecinit6, <4 x i16>* undef
322  %t1 = load <4 x i16>, <4 x i16>* undef
323  %vecinit11 = insertelement <4 x i16> undef, i16 %conv10, i32 3
324  %div = udiv <4 x i16> %t1, %vecinit11
325  store <4 x i16> %div, <4 x i16>* %t
326  %t4 = load <4 x i16>, <4 x i16>* %t
327  %t5 = shufflevector <4 x i16> %t4, <4 x i16> undef, <2 x i32> <i32 2, i32 0>
328  %cmp = icmp ule <2 x i16> %t5, undef
329  %sext = sext <2 x i1> %cmp to <2 x i16>
330  ret void
331}
332
333; Check that sequences of insert/extract element are
334; collapsed into valid shuffle instruction with correct shuffle indexes.
335
336define <4 x float> @test15a(<4 x float> %LHS, <4 x float> %RHS) {
337; CHECK-LABEL: @test15a(
338; CHECK-NEXT:    [[T4:%.*]] = shufflevector <4 x float> [[LHS:%.*]], <4 x float> [[RHS:%.*]], <4 x i32> <i32 4, i32 0, i32 6, i32 6>
339; CHECK-NEXT:    ret <4 x float> [[T4]]
340;
341  %t1 = extractelement <4 x float> %LHS, i32 0
342  %t2 = insertelement <4 x float> %RHS, float %t1, i32 1
343  %t3 = extractelement <4 x float> %RHS, i32 2
344  %t4 = insertelement <4 x float> %t2, float %t3, i32 3
345  ret <4 x float> %t4
346}
347
348define <4 x float> @test15b(<4 x float> %LHS, <4 x float> %RHS) {
349; CHECK-LABEL: @test15b(
350; CHECK-NEXT:    [[T5:%.*]] = shufflevector <4 x float> [[LHS:%.*]], <4 x float> [[RHS:%.*]], <4 x i32> <i32 4, i32 3, i32 6, i32 6>
351; CHECK-NEXT:    ret <4 x float> [[T5]]
352;
353  %t0 = extractelement <4 x float> %LHS, i32 3
354  %t1 = insertelement <4 x float> %RHS, float %t0, i32 0
355  %t2 = extractelement <4 x float> %t1, i32 0
356  %t3 = insertelement <4 x float> %RHS, float %t2, i32 1
357  %t4 = extractelement <4 x float> %RHS, i32 2
358  %t5 = insertelement <4 x float> %t3, float %t4, i32 3
359  ret <4 x float> %t5
360}
361
362define <1 x i32> @test16a(i32 %ele) {
363; CHECK-LABEL: @test16a(
364; CHECK-NEXT:    ret <1 x i32> <i32 2>
365;
366  %t0 = insertelement <2 x i32> <i32 1, i32 undef>, i32 %ele, i32 1
367  %t1 = shl <2 x i32> %t0, <i32 1, i32 1>
368  %t2 = shufflevector <2 x i32> %t1, <2 x i32> undef, <1 x i32> <i32 0>
369  ret <1 x i32> %t2
370}
371
372define <4 x i8> @test16b(i8 %ele) {
373; CHECK-LABEL: @test16b(
374; CHECK-NEXT:    ret <4 x i8> <i8 2, i8 2, i8 2, i8 2>
375;
376  %t0 = insertelement <8 x i8> <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 undef, i8 1>, i8 %ele, i32 6
377  %t1 = shl <8 x i8> %t0, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
378  %t2 = shufflevector <8 x i8> %t1, <8 x i8> undef, <4 x i32> <i32 1, i32 2, i32 3, i32 4>
379  ret <4 x i8> %t2
380}
381
382; If composition of two shuffles is identity, shuffles can be removed.
383define <4 x i32> @shuffle_17ident(<4 x i32> %v) {
384; CHECK-LABEL: @shuffle_17ident(
385; CHECK-NEXT:    ret <4 x i32> [[V:%.*]]
386;
387  %shuffle = shufflevector <4 x i32> %v, <4 x i32> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
388  %shuffle2 = shufflevector <4 x i32> %shuffle, <4 x i32> zeroinitializer, <4 x i32> <i32 3, i32 0, i32 1, i32 2>
389  ret <4 x i32> %shuffle2
390}
391
392; swizzle can be put after operation
393define <4 x i32> @shuffle_17and(<4 x i32> %v1, <4 x i32> %v2) {
394; CHECK-LABEL: @shuffle_17and(
395; CHECK-NEXT:    [[TMP1:%.*]] = and <4 x i32> [[V1:%.*]], [[V2:%.*]]
396; CHECK-NEXT:    [[R:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
397; CHECK-NEXT:    ret <4 x i32> [[R]]
398;
399  %t1 = shufflevector <4 x i32> %v1, <4 x i32> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
400  %t2 = shufflevector <4 x i32> %v2, <4 x i32> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
401  %r = and <4 x i32> %t1, %t2
402  ret <4 x i32> %r
403}
404
405declare void @use(<2 x float>)
406declare void @use4(<4 x float>)
407
408; One extra use is ok to transform.
409
410define <2 x float> @shuffle_fadd_multiuse(<2 x float> %v1, <2 x float> %v2) {
411; CHECK-LABEL: @shuffle_fadd_multiuse(
412; CHECK-NEXT:    [[T1:%.*]] = shufflevector <2 x float> [[V1:%.*]], <2 x float> undef, <2 x i32> <i32 1, i32 0>
413; CHECK-NEXT:    [[TMP1:%.*]] = fadd <2 x float> [[V1]], [[V2:%.*]]
414; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> poison, <2 x i32> <i32 1, i32 0>
415; CHECK-NEXT:    call void @use(<2 x float> [[T1]])
416; CHECK-NEXT:    ret <2 x float> [[R]]
417;
418  %t1 = shufflevector <2 x float> %v1, <2 x float> undef, <2 x i32> <i32 1, i32 0>
419  %t2 = shufflevector <2 x float> %v2, <2 x float> undef, <2 x i32> <i32 1, i32 0>
420  %r = fadd <2 x float> %t1, %t2
421  call void @use(<2 x float> %t1)
422  ret <2 x float> %r
423}
424
425define <2 x float> @shuffle_fdiv_multiuse(<2 x float> %v1, <2 x float> %v2) {
426; CHECK-LABEL: @shuffle_fdiv_multiuse(
427; CHECK-NEXT:    [[T2:%.*]] = shufflevector <2 x float> [[V2:%.*]], <2 x float> undef, <2 x i32> <i32 1, i32 0>
428; CHECK-NEXT:    [[TMP1:%.*]] = fdiv <2 x float> [[V1:%.*]], [[V2]]
429; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> poison, <2 x i32> <i32 1, i32 0>
430; CHECK-NEXT:    call void @use(<2 x float> [[T2]])
431; CHECK-NEXT:    ret <2 x float> [[R]]
432;
433  %t1 = shufflevector <2 x float> %v1, <2 x float> undef, <2 x i32> <i32 1, i32 0>
434  %t2 = shufflevector <2 x float> %v2, <2 x float> undef, <2 x i32> <i32 1, i32 0>
435  %r = fdiv <2 x float> %t1, %t2
436  call void @use(<2 x float> %t2)
437  ret <2 x float> %r
438}
439
440; But 2 extra uses would require an extra instruction.
441
442define <2 x float> @shuffle_fsub_multiuse(<2 x float> %v1, <2 x float> %v2) {
443; CHECK-LABEL: @shuffle_fsub_multiuse(
444; CHECK-NEXT:    [[T1:%.*]] = shufflevector <2 x float> [[V1:%.*]], <2 x float> undef, <2 x i32> <i32 1, i32 0>
445; CHECK-NEXT:    [[T2:%.*]] = shufflevector <2 x float> [[V2:%.*]], <2 x float> undef, <2 x i32> <i32 1, i32 0>
446; CHECK-NEXT:    [[R:%.*]] = fsub <2 x float> [[T1]], [[T2]]
447; CHECK-NEXT:    call void @use(<2 x float> [[T1]])
448; CHECK-NEXT:    call void @use(<2 x float> [[T2]])
449; CHECK-NEXT:    ret <2 x float> [[R]]
450;
451  %t1 = shufflevector <2 x float> %v1, <2 x float> undef, <2 x i32> <i32 1, i32 0>
452  %t2 = shufflevector <2 x float> %v2, <2 x float> undef, <2 x i32> <i32 1, i32 0>
453  %r = fsub <2 x float> %t1, %t2
454  call void @use(<2 x float> %t1)
455  call void @use(<2 x float> %t2)
456  ret <2 x float> %r
457}
458
459define <4 x i32> @shuffle_17add(<4 x i32> %v1, <4 x i32> %v2) {
460; CHECK-LABEL: @shuffle_17add(
461; CHECK-NEXT:    [[TMP1:%.*]] = add <4 x i32> [[V1:%.*]], [[V2:%.*]]
462; CHECK-NEXT:    [[R:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
463; CHECK-NEXT:    ret <4 x i32> [[R]]
464;
465  %t1 = shufflevector <4 x i32> %v1, <4 x i32> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
466  %t2 = shufflevector <4 x i32> %v2, <4 x i32> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
467  %r = add <4 x i32> %t1, %t2
468  ret <4 x i32> %r
469}
470
471define <4 x i32> @shuffle_17addnsw(<4 x i32> %v1, <4 x i32> %v2) {
472; CHECK-LABEL: @shuffle_17addnsw(
473; CHECK-NEXT:    [[TMP1:%.*]] = add nsw <4 x i32> [[V1:%.*]], [[V2:%.*]]
474; CHECK-NEXT:    [[R:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
475; CHECK-NEXT:    ret <4 x i32> [[R]]
476;
477  %t1 = shufflevector <4 x i32> %v1, <4 x i32> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
478  %t2 = shufflevector <4 x i32> %v2, <4 x i32> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
479  %r = add nsw <4 x i32> %t1, %t2
480  ret <4 x i32> %r
481}
482
483define <4 x i32> @shuffle_17addnuw(<4 x i32> %v1, <4 x i32> %v2) {
484; CHECK-LABEL: @shuffle_17addnuw(
485; CHECK-NEXT:    [[TMP1:%.*]] = add nuw <4 x i32> [[V1:%.*]], [[V2:%.*]]
486; CHECK-NEXT:    [[R:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
487; CHECK-NEXT:    ret <4 x i32> [[R]]
488;
489  %t1 = shufflevector <4 x i32> %v1, <4 x i32> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
490  %t2 = shufflevector <4 x i32> %v2, <4 x i32> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
491  %r = add nuw <4 x i32> %t1, %t2
492  ret <4 x i32> %r
493}
494
495define <4 x float> @shuffle_17fsub_fast(<4 x float> %v1, <4 x float> %v2) {
496; CHECK-LABEL: @shuffle_17fsub_fast(
497; CHECK-NEXT:    [[TMP1:%.*]] = fsub fast <4 x float> [[V1:%.*]], [[V2:%.*]]
498; CHECK-NEXT:    [[R:%.*]] = shufflevector <4 x float> [[TMP1]], <4 x float> poison, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
499; CHECK-NEXT:    ret <4 x float> [[R]]
500;
501  %t1 = shufflevector <4 x float> %v1, <4 x float> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
502  %t2 = shufflevector <4 x float> %v2, <4 x float> zeroinitializer, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
503  %r = fsub fast <4 x float> %t1, %t2
504  ret <4 x float> %r
505}
506
507define <4 x i32> @add_const(<4 x i32> %v) {
508; CHECK-LABEL: @add_const(
509; CHECK-NEXT:    [[TMP1:%.*]] = add <4 x i32> [[V:%.*]], <i32 44, i32 41, i32 42, i32 43>
510; CHECK-NEXT:    [[R:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
511; CHECK-NEXT:    ret <4 x i32> [[R]]
512;
513  %t1 = shufflevector <4 x i32> %v, <4 x i32> undef, <4 x i32> <i32 1, i32 2, i32 3, i32 0>
514  %r = add <4 x i32> %t1, <i32 41, i32 42, i32 43, i32 44>
515  ret <4 x i32> %r
516}
517
518define <4 x i32> @sub_const(<4 x i32> %v) {
519; CHECK-LABEL: @sub_const(
520; CHECK-NEXT:    [[TMP1:%.*]] = sub <4 x i32> <i32 44, i32 43, i32 42, i32 41>, [[V:%.*]]
521; CHECK-NEXT:    [[R:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
522; CHECK-NEXT:    ret <4 x i32> [[R]]
523;
524  %t1 = shufflevector <4 x i32> %v, <4 x i32> undef, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
525  %r = sub <4 x i32> <i32 41, i32 42, i32 43, i32 44>, %t1
526  ret <4 x i32> %r
527}
528
529; Math before shuffle requires an extra shuffle.
530
531define <2 x float> @fadd_const_multiuse(<2 x float> %v) {
532; CHECK-LABEL: @fadd_const_multiuse(
533; CHECK-NEXT:    [[T1:%.*]] = shufflevector <2 x float> [[V:%.*]], <2 x float> undef, <2 x i32> <i32 1, i32 0>
534; CHECK-NEXT:    [[R:%.*]] = fadd <2 x float> [[T1]], <float 4.100000e+01, float 4.200000e+01>
535; CHECK-NEXT:    call void @use(<2 x float> [[T1]])
536; CHECK-NEXT:    ret <2 x float> [[R]]
537;
538  %t1 = shufflevector <2 x float> %v, <2 x float> undef, <2 x i32> <i32 1, i32 0>
539  %r = fadd <2 x float> %t1, <float 41.0, float 42.0>
540  call void @use(<2 x float> %t1)
541  ret <2 x float> %r
542}
543
544; Math before splat allows replacing constant elements with undef lanes.
545
546define <4 x i32> @mul_const_splat(<4 x i32> %v) {
547; CHECK-LABEL: @mul_const_splat(
548; CHECK-NEXT:    [[TMP1:%.*]] = mul <4 x i32> [[V:%.*]], <i32 poison, i32 42, i32 poison, i32 poison>
549; CHECK-NEXT:    [[R:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> <i32 1, i32 1, i32 1, i32 1>
550; CHECK-NEXT:    ret <4 x i32> [[R]]
551;
552  %t1 = shufflevector <4 x i32> %v, <4 x i32> undef, <4 x i32> <i32 1, i32 1, i32 1, i32 1>
553  %r = mul <4 x i32> <i32 42, i32 42, i32 42, i32 42>, %t1
554  ret <4 x i32> %r
555}
556
557; Take 2 elements of a vector and shift each of those by a different amount
558
559define <4 x i32> @lshr_const_half_splat(<4 x i32> %v) {
560; CHECK-LABEL: @lshr_const_half_splat(
561; CHECK-NEXT:    [[TMP1:%.*]] = lshr <4 x i32> <i32 undef, i32 8, i32 9, i32 undef>, [[V:%.*]]
562; CHECK-NEXT:    [[R:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> <i32 1, i32 1, i32 2, i32 2>
563; CHECK-NEXT:    ret <4 x i32> [[R]]
564;
565  %t1 = shufflevector <4 x i32> %v, <4 x i32> undef, <4 x i32> <i32 1, i32 1, i32 2, i32 2>
566  %r = lshr <4 x i32> <i32 8, i32 8, i32 9, i32 9>, %t1
567  ret <4 x i32> %r
568}
569
570; We can't change this because there's no pre-shuffle version of the fmul constant.
571
572define <2 x float> @fmul_const_invalid_constant(<2 x float> %v) {
573; CHECK-LABEL: @fmul_const_invalid_constant(
574; CHECK-NEXT:    [[T1:%.*]] = shufflevector <2 x float> [[V:%.*]], <2 x float> undef, <2 x i32> zeroinitializer
575; CHECK-NEXT:    [[R:%.*]] = fmul <2 x float> [[T1]], <float 4.100000e+01, float 4.200000e+01>
576; CHECK-NEXT:    ret <2 x float> [[R]]
577;
578  %t1 = shufflevector <2 x float> %v, <2 x float> undef, <2 x i32> <i32 0, i32 0>
579  %r = fmul <2 x float> %t1, <float 41.0, float 42.0>
580  ret <2 x float> %r
581}
582
583; Reduce the width of the binop by moving it ahead of a shuffle.
584
585define <4 x i8> @widening_shuffle_add_1(<2 x i8> %x) {
586; CHECK-LABEL: @widening_shuffle_add_1(
587; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i8> [[X:%.*]], <i8 42, i8 43>
588; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i8> [[TMP1]], <2 x i8> poison, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
589; CHECK-NEXT:    ret <4 x i8> [[R]]
590;
591  %widex = shufflevector <2 x i8> %x, <2 x i8> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
592  %r = add <4 x i8> %widex, <i8 42, i8 43, i8 44, i8 45>
593  ret <4 x i8> %r
594}
595
596; Reduce the width of the binop by moving it ahead of a shuffle.
597
598define <4 x i8> @widening_shuffle_add_2(<2 x i8> %x) {
599; CHECK-LABEL: @widening_shuffle_add_2(
600; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i8> [[X:%.*]], <i8 43, i8 42>
601; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i8> [[TMP1]], <2 x i8> poison, <4 x i32> <i32 1, i32 0, i32 undef, i32 undef>
602; CHECK-NEXT:    ret <4 x i8> [[R]]
603;
604  %widex = shufflevector <2 x i8> %x, <2 x i8> undef, <4 x i32> <i32 1, i32 0, i32 undef, i32 undef>
605  %r = add <4 x i8> %widex, <i8 42, i8 43, i8 44, i8 45>
606  ret <4 x i8> %r
607}
608
609; Negative test - widening shuffles have the same mask/constant constraint as non-size-changing shuffles.
610
611define <4 x i8> @widening_shuffle_add_invalid_constant(<2 x i8> %x) {
612; CHECK-LABEL: @widening_shuffle_add_invalid_constant(
613; CHECK-NEXT:    [[WIDEX:%.*]] = shufflevector <2 x i8> [[X:%.*]], <2 x i8> undef, <4 x i32> <i32 1, i32 1, i32 undef, i32 undef>
614; CHECK-NEXT:    [[R:%.*]] = add <4 x i8> [[WIDEX]], <i8 42, i8 43, i8 44, i8 45>
615; CHECK-NEXT:    ret <4 x i8> [[R]]
616;
617  %widex = shufflevector <2 x i8> %x, <2 x i8> undef, <4 x i32> <i32 1, i32 1, i32 undef, i32 undef>
618  %r = add <4 x i8> %widex, <i8 42, i8 43, i8 44, i8 45>
619  ret <4 x i8> %r
620}
621
622; Negative test - widening shuffles have an additional constraint: they must not extend with anything but undefs.
623
624define <4 x i8> @widening_shuffle_add_invalid_mask(<2 x i8> %x) {
625; CHECK-LABEL: @widening_shuffle_add_invalid_mask(
626; CHECK-NEXT:    [[WIDEX:%.*]] = shufflevector <2 x i8> [[X:%.*]], <2 x i8> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 0>
627; CHECK-NEXT:    [[R:%.*]] = add <4 x i8> [[WIDEX]], <i8 42, i8 43, i8 44, i8 45>
628; CHECK-NEXT:    ret <4 x i8> [[R]]
629;
630  %widex = shufflevector <2 x i8> %x, <2 x i8> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 0>
631  %r = add <4 x i8> %widex, <i8 42, i8 43, i8 44, i8 45>
632  ret <4 x i8> %r
633}
634
635; A binop that produces undef in the high lanes can be moved before the shuffle.
636; This is ok because 'shl C, undef --> undef'.
637
638define <4 x i16> @widening_shuffle_shl_constant_op0(<2 x i16> %v) {
639; CHECK-LABEL: @widening_shuffle_shl_constant_op0(
640; CHECK-NEXT:    [[TMP1:%.*]] = shl <2 x i16> <i16 42, i16 -42>, [[V:%.*]]
641; CHECK-NEXT:    [[BO:%.*]] = shufflevector <2 x i16> [[TMP1]], <2 x i16> poison, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
642; CHECK-NEXT:    ret <4 x i16> [[BO]]
643;
644  %shuf = shufflevector <2 x i16> %v, <2 x i16> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
645  %bo = shl <4 x i16> <i16 42, i16 -42, i16 -1, i16 -1>, %shuf
646  ret <4 x i16> %bo
647}
648
649; A binop that produces undef in the high lanes can be moved before the shuffle.
650; This is ok because 'shl undef, 0 --> undef'.
651
652define <4 x i16> @widening_shuffle_shl_constant_op1(<2 x i16> %v) {
653; CHECK-LABEL: @widening_shuffle_shl_constant_op1(
654; CHECK-NEXT:    [[TMP1:%.*]] = shl <2 x i16> [[V:%.*]], <i16 2, i16 4>
655; CHECK-NEXT:    [[BO:%.*]] = shufflevector <2 x i16> [[TMP1]], <2 x i16> poison, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
656; CHECK-NEXT:    ret <4 x i16> [[BO]]
657;
658  %shuf = shufflevector <2 x i16> %v, <2 x i16> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
659  %bo = shl <4 x i16> %shuf, <i16 2, i16 4, i16 0, i16 0>
660  ret <4 x i16> %bo
661}
662
663; A binop that does not produce undef in the high lanes can not be moved before the shuffle.
664; This is not ok because 'shl undef, 1 (or 2)' --> 0' but moving the shuffle results in undef instead.
665
666define <4 x i16> @widening_shuffle_shl_constant_op1_non0(<2 x i16> %v) {
667; CHECK-LABEL: @widening_shuffle_shl_constant_op1_non0(
668; CHECK-NEXT:    [[SHUF:%.*]] = shufflevector <2 x i16> [[V:%.*]], <2 x i16> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
669; CHECK-NEXT:    [[BO:%.*]] = shl <4 x i16> [[SHUF]], <i16 2, i16 4, i16 1, i16 2>
670; CHECK-NEXT:    ret <4 x i16> [[BO]]
671;
672  %shuf = shufflevector <2 x i16> %v, <2 x i16> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
673  %bo = shl <4 x i16> %shuf, <i16 2, i16 4, i16 1, i16 2>
674  ret <4 x i16> %bo
675}
676
677; A binop that does not produce undef in the high lanes can not be moved before the shuffle.
678; This is not ok because 'or -1, undef --> -1' but moving the shuffle results in undef instead.
679
680define <4 x i16> @widening_shuffle_or(<2 x i16> %v) {
681; CHECK-LABEL: @widening_shuffle_or(
682; CHECK-NEXT:    [[SHUF:%.*]] = shufflevector <2 x i16> [[V:%.*]], <2 x i16> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
683; CHECK-NEXT:    [[BO:%.*]] = or <4 x i16> [[SHUF]], <i16 42, i16 -42, i16 -1, i16 -1>
684; CHECK-NEXT:    ret <4 x i16> [[BO]]
685;
686  %shuf = shufflevector <2 x i16> %v, <2 x i16> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
687  %bo = or <4 x i16> %shuf, <i16 42, i16 -42, i16 -1, i16 -1>
688  ret <4 x i16> %bo
689}
690
691define <4 x i32> @shuffle_17add2(<4 x i32> %v) {
692; CHECK-LABEL: @shuffle_17add2(
693; CHECK-NEXT:    [[TMP1:%.*]] = shl <4 x i32> [[V:%.*]], <i32 1, i32 1, i32 1, i32 1>
694; CHECK-NEXT:    ret <4 x i32> [[TMP1]]
695;
696  %t1 = shufflevector <4 x i32> %v, <4 x i32> zeroinitializer, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
697  %t2 = add <4 x i32> %t1, %t1
698  %r = shufflevector <4 x i32> %t2, <4 x i32> zeroinitializer, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
699  ret <4 x i32> %r
700}
701
702define <4 x i32> @shuffle_17mulsplat(<4 x i32> %v) {
703; CHECK-LABEL: @shuffle_17mulsplat(
704; CHECK-NEXT:    [[TMP1:%.*]] = mul <4 x i32> [[V:%.*]], [[V]]
705; CHECK-NEXT:    [[M1:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> zeroinitializer
706; CHECK-NEXT:    ret <4 x i32> [[M1]]
707;
708  %s1 = shufflevector <4 x i32> %v, <4 x i32> zeroinitializer, <4 x i32> zeroinitializer
709  %m1 = mul <4 x i32> %s1, %s1
710  %s2 = shufflevector <4 x i32> %m1, <4 x i32> zeroinitializer, <4 x i32> <i32 1, i32 1, i32 1, i32 1>
711  ret <4 x i32> %s2
712}
713
714; Do not reorder shuffle and binop if LHS of shuffles are of different size
715define <2 x i32> @pr19717(<4 x i32> %in0, <2 x i32> %in1) {
716; CHECK-LABEL: @pr19717(
717; CHECK-NEXT:    [[SHUFFLE:%.*]] = shufflevector <4 x i32> [[IN0:%.*]], <4 x i32> poison, <2 x i32> zeroinitializer
718; CHECK-NEXT:    [[SHUFFLE4:%.*]] = shufflevector <2 x i32> [[IN1:%.*]], <2 x i32> poison, <2 x i32> zeroinitializer
719; CHECK-NEXT:    [[MUL:%.*]] = mul <2 x i32> [[SHUFFLE]], [[SHUFFLE4]]
720; CHECK-NEXT:    ret <2 x i32> [[MUL]]
721;
722  %shuffle = shufflevector <4 x i32> %in0, <4 x i32> %in0, <2 x i32> zeroinitializer
723  %shuffle4 = shufflevector <2 x i32> %in1, <2 x i32> %in1, <2 x i32> zeroinitializer
724  %mul = mul <2 x i32> %shuffle, %shuffle4
725  ret <2 x i32> %mul
726}
727
728define <4 x i16> @pr19717a(<8 x i16> %in0, <8 x i16> %in1) {
729; CHECK-LABEL: @pr19717a(
730; CHECK-NEXT:    [[TMP1:%.*]] = mul <8 x i16> [[IN0:%.*]], [[IN1:%.*]]
731; CHECK-NEXT:    [[MUL:%.*]] = shufflevector <8 x i16> [[TMP1]], <8 x i16> poison, <4 x i32> <i32 5, i32 5, i32 5, i32 5>
732; CHECK-NEXT:    ret <4 x i16> [[MUL]]
733;
734  %shuffle = shufflevector <8 x i16> %in0, <8 x i16> %in0, <4 x i32> <i32 5, i32 5, i32 5, i32 5>
735  %shuffle1 = shufflevector <8 x i16> %in1, <8 x i16> %in1, <4 x i32> <i32 5, i32 5, i32 5, i32 5>
736  %mul = mul <4 x i16> %shuffle, %shuffle1
737  ret <4 x i16> %mul
738}
739
740define <8 x i8> @pr19730(<16 x i8> %in0) {
741; CHECK-LABEL: @pr19730(
742; CHECK-NEXT:    [[SHUFFLE:%.*]] = shufflevector <16 x i8> [[IN0:%.*]], <16 x i8> undef, <8 x i32> <i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
743; CHECK-NEXT:    [[SHUFFLE1:%.*]] = shufflevector <8 x i8> [[SHUFFLE]], <8 x i8> undef, <8 x i32> <i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
744; CHECK-NEXT:    ret <8 x i8> [[SHUFFLE1]]
745;
746  %shuffle = shufflevector <16 x i8> %in0, <16 x i8> undef, <8 x i32> <i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
747  %shuffle1 = shufflevector <8 x i8> %shuffle, <8 x i8> undef, <8 x i32> <i32 7, i32 6, i32 5, i32 4, i32 3, i32 2, i32 1, i32 0>
748  ret <8 x i8> %shuffle1
749}
750
751define i32 @pr19737(<4 x i32> %in0) {
752; CHECK-LABEL: @pr19737(
753; CHECK-NEXT:    [[TMP1:%.*]] = extractelement <4 x i32> [[IN0:%.*]], i64 0
754; CHECK-NEXT:    ret i32 [[TMP1]]
755;
756  %shuffle.i = shufflevector <4 x i32> zeroinitializer, <4 x i32> %in0, <4 x i32> <i32 0, i32 4, i32 2, i32 6>
757  %neg.i = xor <4 x i32> %shuffle.i, <i32 -1, i32 -1, i32 -1, i32 -1>
758  %and.i = and <4 x i32> %in0, %neg.i
759  %rv = extractelement <4 x i32> %and.i, i32 0
760  ret i32 %rv
761}
762
763; In PR20059 ( http://llvm.org/pr20059 ), shufflevector operations are reordered/removed
764; for an srem operation. This is not a valid optimization because it may cause a trap
765; on div-by-zero.
766
767define <4 x i32> @pr20059(<4 x i32> %p1, <4 x i32> %p2) {
768; CHECK-LABEL: @pr20059(
769; CHECK-NEXT:    [[SPLAT1:%.*]] = shufflevector <4 x i32> [[P1:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
770; CHECK-NEXT:    [[SPLAT2:%.*]] = shufflevector <4 x i32> [[P2:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
771; CHECK-NEXT:    [[RETVAL:%.*]] = srem <4 x i32> [[SPLAT1]], [[SPLAT2]]
772; CHECK-NEXT:    ret <4 x i32> [[RETVAL]]
773;
774  %splat1 = shufflevector <4 x i32> %p1, <4 x i32> undef, <4 x i32> zeroinitializer
775  %splat2 = shufflevector <4 x i32> %p2, <4 x i32> undef, <4 x i32> zeroinitializer
776  %retval = srem <4 x i32> %splat1, %splat2
777  ret <4 x i32> %retval
778}
779
780define <4 x i32> @pr20114(<4 x i32> %__mask) {
781; CHECK-LABEL: @pr20114(
782; CHECK-NEXT:    [[MASK01_I:%.*]] = shufflevector <4 x i32> [[__MASK:%.*]], <4 x i32> undef, <4 x i32> <i32 0, i32 0, i32 1, i32 1>
783; CHECK-NEXT:    [[MASKED_NEW_I_I_I:%.*]] = and <4 x i32> [[MASK01_I]], bitcast (<2 x i64> <i64 ptrtoint (<4 x i32> (<4 x i32>)* @pr20114 to i64), i64 ptrtoint (<4 x i32> (<4 x i32>)* @pr20114 to i64)> to <4 x i32>)
784; CHECK-NEXT:    ret <4 x i32> [[MASKED_NEW_I_I_I]]
785;
786  %mask01.i = shufflevector <4 x i32> %__mask, <4 x i32> undef, <4 x i32> <i32 0, i32 0, i32 1, i32 1>
787  %masked_new.i.i.i = and <4 x i32> bitcast (<2 x i64> <i64 ptrtoint (<4 x i32> (<4 x i32>)* @pr20114 to i64), i64 ptrtoint (<4 x i32> (<4 x i32>)* @pr20114 to i64)> to <4 x i32>), %mask01.i
788  ret <4 x i32> %masked_new.i.i.i
789}
790
791define <2 x i32*> @pr23113(<4 x i32*> %A) {
792; CHECK-LABEL: @pr23113(
793; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <4 x i32*> [[A:%.*]], <4 x i32*> undef, <2 x i32> <i32 0, i32 1>
794; CHECK-NEXT:    ret <2 x i32*> [[TMP1]]
795;
796  %1 = shufflevector <4 x i32*> %A, <4 x i32*> undef, <2 x i32> <i32 0, i32 1>
797  ret <2 x i32*> %1
798}
799
800; Unused lanes in the new binop should not kill the entire op (although it may simplify anyway as shown here).
801
802define <2 x i32> @PR37648(<2 x i32> %x) {
803; CHECK-LABEL: @PR37648(
804; CHECK-NEXT:    ret <2 x i32> zeroinitializer
805;
806  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
807  %r = urem <2 x i32> %splat, <i32 1, i32 1>
808  ret <2 x i32> %r
809}
810
811; Test shuffle followed by binop with splat constant for all 18 binop opcodes.
812; Test with constant as operand 0 and operand 1 for non-commutative opcodes.
813
814define <2 x i32> @add_splat_constant(<2 x i32> %x) {
815; CHECK-LABEL: @add_splat_constant(
816; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i32> [[X:%.*]], <i32 42, i32 poison>
817; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
818; CHECK-NEXT:    ret <2 x i32> [[R]]
819;
820  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
821  %r = add <2 x i32> %splat, <i32 42, i32 42>
822  ret <2 x i32> %r
823}
824
825define <2 x i32> @sub_splat_constant0(<2 x i32> %x) {
826; CHECK-LABEL: @sub_splat_constant0(
827; CHECK-NEXT:    [[TMP1:%.*]] = sub <2 x i32> <i32 42, i32 poison>, [[X:%.*]]
828; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
829; CHECK-NEXT:    ret <2 x i32> [[R]]
830;
831  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
832  %r = sub <2 x i32> <i32 42, i32 42>, %splat
833  ret <2 x i32> %r
834}
835
836define <2 x i32> @sub_splat_constant1(<2 x i32> %x) {
837; CHECK-LABEL: @sub_splat_constant1(
838; CHECK-NEXT:    [[TMP1:%.*]] = add <2 x i32> [[X:%.*]], <i32 -42, i32 poison>
839; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
840; CHECK-NEXT:    ret <2 x i32> [[R]]
841;
842  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
843  %r = sub <2 x i32> %splat, <i32 42, i32 42>
844  ret <2 x i32> %r
845}
846
847define <2 x i32> @mul_splat_constant(<2 x i32> %x) {
848; CHECK-LABEL: @mul_splat_constant(
849; CHECK-NEXT:    [[TMP1:%.*]] = mul <2 x i32> [[X:%.*]], <i32 42, i32 poison>
850; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
851; CHECK-NEXT:    ret <2 x i32> [[R]]
852;
853  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
854  %r = mul <2 x i32> %splat, <i32 42, i32 42>
855  ret <2 x i32> %r
856}
857
858define <2 x i32> @shl_splat_constant0(<2 x i32> %x) {
859; CHECK-LABEL: @shl_splat_constant0(
860; CHECK-NEXT:    [[TMP1:%.*]] = shl <2 x i32> <i32 5, i32 undef>, [[X:%.*]]
861; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
862; CHECK-NEXT:    ret <2 x i32> [[R]]
863;
864  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
865  %r = shl <2 x i32> <i32 5, i32 5>, %splat
866  ret <2 x i32> %r
867}
868
869define <2 x i32> @shl_splat_constant1(<2 x i32> %x) {
870; CHECK-LABEL: @shl_splat_constant1(
871; CHECK-NEXT:    [[TMP1:%.*]] = shl <2 x i32> [[X:%.*]], <i32 5, i32 0>
872; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
873; CHECK-NEXT:    ret <2 x i32> [[R]]
874;
875  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
876  %r = shl <2 x i32> %splat, <i32 5, i32 5>
877  ret <2 x i32> %r
878}
879
880define <2 x i32> @ashr_splat_constant0(<2 x i32> %x) {
881; CHECK-LABEL: @ashr_splat_constant0(
882; CHECK-NEXT:    [[TMP1:%.*]] = ashr <2 x i32> <i32 5, i32 undef>, [[X:%.*]]
883; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
884; CHECK-NEXT:    ret <2 x i32> [[R]]
885;
886  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
887  %r = ashr <2 x i32> <i32 5, i32 5>, %splat
888  ret <2 x i32> %r
889}
890
891define <2 x i32> @ashr_splat_constant1(<2 x i32> %x) {
892; CHECK-LABEL: @ashr_splat_constant1(
893; CHECK-NEXT:    [[TMP1:%.*]] = ashr <2 x i32> [[X:%.*]], <i32 5, i32 0>
894; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
895; CHECK-NEXT:    ret <2 x i32> [[R]]
896;
897  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
898  %r = ashr <2 x i32> %splat, <i32 5, i32 5>
899  ret <2 x i32> %r
900}
901
902define <2 x i32> @lshr_splat_constant0(<2 x i32> %x) {
903; CHECK-LABEL: @lshr_splat_constant0(
904; CHECK-NEXT:    [[TMP1:%.*]] = lshr <2 x i32> <i32 5, i32 undef>, [[X:%.*]]
905; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
906; CHECK-NEXT:    ret <2 x i32> [[R]]
907;
908  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
909  %r = lshr <2 x i32> <i32 5, i32 5>, %splat
910  ret <2 x i32> %r
911}
912
913define <2 x i32> @lshr_splat_constant1(<2 x i32> %x) {
914; CHECK-LABEL: @lshr_splat_constant1(
915; CHECK-NEXT:    [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 5, i32 0>
916; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
917; CHECK-NEXT:    ret <2 x i32> [[R]]
918;
919  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
920  %r = lshr <2 x i32> %splat, <i32 5, i32 5>
921  ret <2 x i32> %r
922}
923
924define <2 x i32> @urem_splat_constant0(<2 x i32> %x) {
925; CHECK-LABEL: @urem_splat_constant0(
926; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <2 x i32> [[X:%.*]], <2 x i32> undef, <2 x i32> zeroinitializer
927; CHECK-NEXT:    [[R:%.*]] = urem <2 x i32> <i32 42, i32 42>, [[SPLAT]]
928; CHECK-NEXT:    ret <2 x i32> [[R]]
929;
930  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
931  %r = urem <2 x i32> <i32 42, i32 42>, %splat
932  ret <2 x i32> %r
933}
934
935define <2 x i32> @urem_splat_constant1(<2 x i32> %x) {
936; CHECK-LABEL: @urem_splat_constant1(
937; CHECK-NEXT:    [[TMP1:%.*]] = urem <2 x i32> [[X:%.*]], <i32 42, i32 1>
938; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
939; CHECK-NEXT:    ret <2 x i32> [[R]]
940;
941  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
942  %r = urem <2 x i32> %splat, <i32 42, i32 42>
943  ret <2 x i32> %r
944}
945
946define <2 x i32> @srem_splat_constant0(<2 x i32> %x) {
947; CHECK-LABEL: @srem_splat_constant0(
948; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <2 x i32> [[X:%.*]], <2 x i32> undef, <2 x i32> zeroinitializer
949; CHECK-NEXT:    [[R:%.*]] = srem <2 x i32> <i32 42, i32 42>, [[SPLAT]]
950; CHECK-NEXT:    ret <2 x i32> [[R]]
951;
952  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
953  %r = srem <2 x i32> <i32 42, i32 42>, %splat
954  ret <2 x i32> %r
955}
956
957define <2 x i32> @srem_splat_constant1(<2 x i32> %x) {
958; CHECK-LABEL: @srem_splat_constant1(
959; CHECK-NEXT:    [[TMP1:%.*]] = srem <2 x i32> [[X:%.*]], <i32 42, i32 1>
960; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
961; CHECK-NEXT:    ret <2 x i32> [[R]]
962;
963  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
964  %r = srem <2 x i32> %splat, <i32 42, i32 42>
965  ret <2 x i32> %r
966}
967
968define <2 x i32> @udiv_splat_constant0(<2 x i32> %x) {
969; CHECK-LABEL: @udiv_splat_constant0(
970; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <2 x i32> [[X:%.*]], <2 x i32> undef, <2 x i32> zeroinitializer
971; CHECK-NEXT:    [[R:%.*]] = udiv <2 x i32> <i32 42, i32 42>, [[SPLAT]]
972; CHECK-NEXT:    ret <2 x i32> [[R]]
973;
974  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
975  %r = udiv <2 x i32> <i32 42, i32 42>, %splat
976  ret <2 x i32> %r
977}
978
979define <2 x i32> @udiv_splat_constant1(<2 x i32> %x) {
980; CHECK-LABEL: @udiv_splat_constant1(
981; CHECK-NEXT:    [[TMP1:%.*]] = udiv <2 x i32> [[X:%.*]], <i32 42, i32 1>
982; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
983; CHECK-NEXT:    ret <2 x i32> [[R]]
984;
985  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
986  %r = udiv <2 x i32> %splat, <i32 42, i32 42>
987  ret <2 x i32> %r
988}
989
990define <2 x i32> @sdiv_splat_constant0(<2 x i32> %x) {
991; CHECK-LABEL: @sdiv_splat_constant0(
992; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <2 x i32> [[X:%.*]], <2 x i32> undef, <2 x i32> zeroinitializer
993; CHECK-NEXT:    [[R:%.*]] = sdiv <2 x i32> <i32 42, i32 42>, [[SPLAT]]
994; CHECK-NEXT:    ret <2 x i32> [[R]]
995;
996  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
997  %r = sdiv <2 x i32> <i32 42, i32 42>, %splat
998  ret <2 x i32> %r
999}
1000
1001define <2 x i32> @sdiv_splat_constant1(<2 x i32> %x) {
1002; CHECK-LABEL: @sdiv_splat_constant1(
1003; CHECK-NEXT:    [[TMP1:%.*]] = sdiv <2 x i32> [[X:%.*]], <i32 42, i32 1>
1004; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
1005; CHECK-NEXT:    ret <2 x i32> [[R]]
1006;
1007  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
1008  %r = sdiv <2 x i32> %splat, <i32 42, i32 42>
1009  ret <2 x i32> %r
1010}
1011
1012define <2 x i32> @and_splat_constant(<2 x i32> %x) {
1013; CHECK-LABEL: @and_splat_constant(
1014; CHECK-NEXT:    [[TMP1:%.*]] = and <2 x i32> [[X:%.*]], <i32 42, i32 poison>
1015; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
1016; CHECK-NEXT:    ret <2 x i32> [[R]]
1017;
1018  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
1019  %r = and <2 x i32> %splat, <i32 42, i32 42>
1020  ret <2 x i32> %r
1021}
1022
1023; AND does not fold to undef for undef operands, we cannot move it
1024; across a shuffle with undef masks.
1025define <4 x i16> @and_constant_mask_undef(<4 x i16> %add) {
1026; CHECK-LABEL: @and_constant_mask_undef(
1027; CHECK-NEXT:  entry:
1028; CHECK-NEXT:    [[SHUFFLE:%.*]] = shufflevector <4 x i16> [[ADD:%.*]], <4 x i16> undef, <4 x i32> <i32 undef, i32 undef, i32 1, i32 1>
1029; CHECK-NEXT:    [[AND:%.*]] = and <4 x i16> [[SHUFFLE]], <i16 0, i16 0, i16 -1, i16 -1>
1030; CHECK-NEXT:    ret <4 x i16> [[AND]]
1031;
1032entry:
1033  %shuffle = shufflevector <4 x i16> %add, <4 x i16> undef, <4 x i32> <i32 undef, i32 undef, i32 1, i32 1>
1034  %and = and <4 x i16> %shuffle, <i16 0, i16 0, i16 -1, i16 -1>
1035  ret <4 x i16> %and
1036}
1037
1038; AND does not fold to undef for undef operands, we cannot move it
1039; across a shuffle with undef masks.
1040define <4 x i16> @and_constant_mask_undef_2(<4 x i16> %add) {
1041; CHECK-LABEL: @and_constant_mask_undef_2(
1042; CHECK-NEXT:  entry:
1043; CHECK-NEXT:    [[SHUFFLE:%.*]] = shufflevector <4 x i16> [[ADD:%.*]], <4 x i16> undef, <4 x i32> <i32 1, i32 1, i32 1, i32 undef>
1044; CHECK-NEXT:    [[AND:%.*]] = and <4 x i16> [[SHUFFLE]], <i16 -1, i16 -1, i16 -1, i16 0>
1045; CHECK-NEXT:    ret <4 x i16> [[AND]]
1046;
1047entry:
1048  %shuffle = shufflevector <4 x i16> %add, <4 x i16> undef, <4 x i32> <i32 1, i32 1, i32 1, i32 undef>
1049  %and = and <4 x i16> %shuffle, <i16 -1, i16 -1, i16 -1, i16 -0>
1050  ret <4 x i16> %and
1051}
1052
1053; We can move the AND across the shuffle, as -1 (AND identity value) is used for undef lanes.
1054define <4 x i16> @and_constant_mask_undef_3(<4 x i16> %add) {
1055; CHECK-LABEL: @and_constant_mask_undef_3(
1056; CHECK-NEXT:  entry:
1057; CHECK-NEXT:    ret <4 x i16> <i16 0, i16 0, i16 0, i16 undef>
1058;
1059entry:
1060  %shuffle = shufflevector <4 x i16> %add, <4 x i16> undef, <4 x i32> <i32 0, i32 1, i32 1, i32 undef>
1061  %and = and <4 x i16> %shuffle, <i16 0, i16 0, i16 0, i16 -1>
1062  ret <4 x i16> %and
1063}
1064
1065; We can move the AND across the shuffle, as -1 (AND identity value) is used for undef lanes.
1066define <4 x i16> @and_constant_mask_undef_4(<4 x i16> %add) {
1067; CHECK-LABEL: @and_constant_mask_undef_4(
1068; CHECK-NEXT:  entry:
1069; CHECK-NEXT:    [[TMP0:%.*]] = and <4 x i16> [[ADD:%.*]], <i16 9, i16 20, i16 poison, i16 poison>
1070; CHECK-NEXT:    [[AND:%.*]] = shufflevector <4 x i16> [[TMP0]], <4 x i16> poison, <4 x i32> <i32 0, i32 1, i32 1, i32 undef>
1071; CHECK-NEXT:    ret <4 x i16> [[AND]]
1072;
1073entry:
1074  %shuffle = shufflevector <4 x i16> %add, <4 x i16> undef, <4 x i32> <i32 0, i32 1, i32 1, i32 undef>
1075  %and = and <4 x i16> %shuffle, <i16 9, i16 20, i16 20, i16 -1>
1076  ret <4 x i16> %and
1077}
1078
1079define <4 x i16> @and_constant_mask_not_undef(<4 x i16> %add) {
1080; CHECK-LABEL: @and_constant_mask_not_undef(
1081; CHECK-NEXT:  entry:
1082; CHECK-NEXT:    [[TMP0:%.*]] = and <4 x i16> [[ADD:%.*]], <i16 poison, i16 -1, i16 0, i16 0>
1083; CHECK-NEXT:    [[AND:%.*]] = shufflevector <4 x i16> [[TMP0]], <4 x i16> poison, <4 x i32> <i32 2, i32 3, i32 1, i32 1>
1084; CHECK-NEXT:    ret <4 x i16> [[AND]]
1085;
1086entry:
1087  %shuffle = shufflevector <4 x i16> %add, <4 x i16> undef, <4 x i32> <i32 2, i32 3, i32 1, i32 1>
1088  %and = and <4 x i16> %shuffle, <i16 0, i16 0, i16 -1, i16 -1>
1089  ret <4 x i16> %and
1090}
1091
1092; OR does not fold to undef for undef operands, we cannot move it
1093; across a shuffle with undef masks.
1094define <4 x i16> @or_constant_mask_undef(<4 x i16> %in) {
1095; CHECK-LABEL: @or_constant_mask_undef(
1096; CHECK-NEXT:  entry:
1097; CHECK-NEXT:    [[SHUFFLE:%.*]] = shufflevector <4 x i16> [[IN:%.*]], <4 x i16> undef, <4 x i32> <i32 undef, i32 undef, i32 1, i32 1>
1098; CHECK-NEXT:    [[OR:%.*]] = or <4 x i16> [[SHUFFLE]], <i16 -1, i16 -1, i16 0, i16 0>
1099; CHECK-NEXT:    ret <4 x i16> [[OR]]
1100;
1101entry:
1102  %shuffle = shufflevector <4 x i16> %in, <4 x i16> undef, <4 x i32> <i32 undef, i32 undef, i32 1, i32 1>
1103  %or = or <4 x i16> %shuffle, <i16 -1, i16 -1, i16 0, i16 0>
1104  ret <4 x i16> %or
1105}
1106
1107; OR does not fold to undef for undef operands, we cannot move it
1108; across a shuffle with undef masks.
1109define <4 x i16> @or_constant_mask_undef_2(<4 x i16> %in) {
1110; CHECK-LABEL: @or_constant_mask_undef_2(
1111; CHECK-NEXT:  entry:
1112; CHECK-NEXT:    [[SHUFFLE:%.*]] = shufflevector <4 x i16> [[IN:%.*]], <4 x i16> undef, <4 x i32> <i32 undef, i32 1, i32 1, i32 undef>
1113; CHECK-NEXT:    [[OR:%.*]] = or <4 x i16> [[SHUFFLE]], <i16 -1, i16 0, i16 0, i16 -1>
1114; CHECK-NEXT:    ret <4 x i16> [[OR]]
1115;
1116entry:
1117  %shuffle = shufflevector <4 x i16> %in, <4 x i16> undef, <4 x i32> <i32 undef, i32 1, i32 1, i32 undef>
1118  %or = or <4 x i16> %shuffle, <i16 -1, i16 0, i16 0, i16 -1>
1119  ret <4 x i16> %or
1120}
1121
1122; We can move the OR across the shuffle, as 0 (OR identity value) is used for undef lanes.
1123define <4 x i16> @or_constant_mask_undef_3(<4 x i16> %in) {
1124; CHECK-LABEL: @or_constant_mask_undef_3(
1125; CHECK-NEXT:  entry:
1126; CHECK-NEXT:    ret <4 x i16> <i16 undef, i16 -1, i16 -1, i16 undef>
1127;
1128entry:
1129  %shuffle = shufflevector <4 x i16> %in, <4 x i16> undef, <4 x i32> <i32 undef, i32 1, i32 1, i32 undef>
1130  %or = or <4 x i16> %shuffle, <i16 0, i16 -1, i16 -1, i16 0>
1131  ret <4 x i16> %or
1132}
1133
1134; We can move the OR across the shuffle, as 0 (OR identity value) is used for undef lanes.
1135define <4 x i16> @or_constant_mask_undef_4(<4 x i16> %in) {
1136; CHECK-LABEL: @or_constant_mask_undef_4(
1137; CHECK-NEXT:  entry:
1138; CHECK-NEXT:    [[TMP0:%.*]] = or <4 x i16> [[IN:%.*]], <i16 poison, i16 99, i16 poison, i16 poison>
1139; CHECK-NEXT:    [[OR:%.*]] = shufflevector <4 x i16> [[TMP0]], <4 x i16> poison, <4 x i32> <i32 undef, i32 1, i32 1, i32 undef>
1140; CHECK-NEXT:    ret <4 x i16> [[OR]]
1141;
1142entry:
1143  %shuffle = shufflevector <4 x i16> %in, <4 x i16> undef, <4 x i32> <i32 undef, i32 1, i32 1, i32 undef>
1144  %or = or <4 x i16> %shuffle, <i16 0, i16 99, i16 99, i16 0>
1145  ret <4 x i16> %or
1146}
1147
1148define <4 x i16> @or_constant_mask_not_undef(<4 x i16> %in) {
1149; CHECK-LABEL: @or_constant_mask_not_undef(
1150; CHECK-NEXT:  entry:
1151; CHECK-NEXT:    [[TMP0:%.*]] = or <4 x i16> [[IN:%.*]], <i16 poison, i16 -1, i16 0, i16 0>
1152; CHECK-NEXT:    [[AND:%.*]] = shufflevector <4 x i16> [[TMP0]], <4 x i16> poison, <4 x i32> <i32 2, i32 3, i32 1, i32 1>
1153; CHECK-NEXT:    ret <4 x i16> [[AND]]
1154;
1155entry:
1156  %shuffle = shufflevector <4 x i16> %in, <4 x i16> undef, <4 x i32> <i32 2, i32 3, i32 1, i32 1>
1157  %and = or <4 x i16> %shuffle, <i16 0, i16 0, i16 -1, i16 -1>
1158  ret <4 x i16> %and
1159}
1160
1161define <4 x i16> @shl_constant_mask_undef(<4 x i16> %in) {
1162; CHECK-LABEL: @shl_constant_mask_undef(
1163; CHECK-NEXT:  entry:
1164; CHECK-NEXT:    [[SHUFFLE:%.*]] = shufflevector <4 x i16> [[IN:%.*]], <4 x i16> undef, <4 x i32> <i32 0, i32 undef, i32 1, i32 1>
1165; CHECK-NEXT:    [[SHL:%.*]] = shl <4 x i16> [[SHUFFLE]], <i16 10, i16 3, i16 0, i16 0>
1166; CHECK-NEXT:    ret <4 x i16> [[SHL]]
1167;
1168entry:
1169  %shuffle = shufflevector <4 x i16> %in, <4 x i16> undef, <4 x i32> <i32 0, i32 undef, i32 1, i32 1>
1170  %shl = shl <4 x i16> %shuffle, <i16 10, i16 3, i16 0, i16 0>
1171  ret <4 x i16> %shl
1172}
1173
1174define <4 x i16> @add_constant_mask_undef(<4 x i16> %in) {
1175; CHECK-LABEL: @add_constant_mask_undef(
1176; CHECK-NEXT:  entry:
1177; CHECK-NEXT:    [[ADD:%.*]] = shufflevector <4 x i16> [[IN:%.*]], <4 x i16> poison, <4 x i32> <i32 undef, i32 undef, i32 1, i32 1>
1178; CHECK-NEXT:    ret <4 x i16> [[ADD]]
1179;
1180entry:
1181  %shuffle = shufflevector <4 x i16> %in, <4 x i16> undef, <4 x i32> <i32 undef, i32 undef, i32 1, i32 1>
1182  %add = add <4 x i16> %shuffle, <i16 10, i16 3, i16 0, i16 0>
1183  ret <4 x i16> %add
1184}
1185
1186define <4 x i16> @add_constant_mask_undef_2(<4 x i16> %in) {
1187; CHECK-LABEL: @add_constant_mask_undef_2(
1188; CHECK-NEXT:  entry:
1189; CHECK-NEXT:    [[TMP0:%.*]] = add <4 x i16> [[IN:%.*]], <i16 poison, i16 0, i16 3, i16 poison>
1190; CHECK-NEXT:    [[ADD:%.*]] = shufflevector <4 x i16> [[TMP0]], <4 x i16> poison, <4 x i32> <i32 undef, i32 2, i32 1, i32 1>
1191; CHECK-NEXT:    ret <4 x i16> [[ADD]]
1192;
1193entry:
1194  %shuffle = shufflevector <4 x i16> %in, <4 x i16> undef, <4 x i32> <i32 undef, i32 2, i32 1, i32 1>
1195  %add = add <4 x i16> %shuffle, <i16 10, i16 3, i16 0, i16 0>
1196  ret <4 x i16> %add
1197}
1198
1199define <4 x i16> @sub_constant_mask_undef(<4 x i16> %in) {
1200; CHECK-LABEL: @sub_constant_mask_undef(
1201; CHECK-NEXT:  entry:
1202; CHECK-NEXT:    [[SUB:%.*]] = shufflevector <4 x i16> [[IN:%.*]], <4 x i16> poison, <4 x i32> <i32 undef, i32 undef, i32 1, i32 1>
1203; CHECK-NEXT:    ret <4 x i16> [[SUB]]
1204;
1205entry:
1206  %shuffle = shufflevector <4 x i16> %in, <4 x i16> undef, <4 x i32> <i32 undef, i32 undef, i32 1, i32 1>
1207  %sub = sub <4 x i16> %shuffle, <i16 10, i16 3, i16 0, i16 0>
1208  ret <4 x i16> %sub
1209}
1210
1211define <4 x i16> @sub_constant_mask_undef_2(<4 x i16> %in) {
1212; CHECK-LABEL: @sub_constant_mask_undef_2(
1213; CHECK-NEXT:  entry:
1214; CHECK-NEXT:    [[TMP0:%.*]] = add <4 x i16> [[IN:%.*]], <i16 poison, i16 0, i16 -10, i16 poison>
1215; CHECK-NEXT:    [[SUB:%.*]] = shufflevector <4 x i16> [[TMP0]], <4 x i16> poison, <4 x i32> <i32 1, i32 1, i32 2, i32 undef>
1216; CHECK-NEXT:    ret <4 x i16> [[SUB]]
1217;
1218entry:
1219  %shuffle = shufflevector <4 x i16> %in, <4 x i16> undef, <4 x i32> <i32 1, i32 1, i32 2, i32 undef>
1220  %sub = sub <4 x i16> %shuffle, <i16 0, i16 0, i16 10, i16 99>
1221  ret <4 x i16> %sub
1222}
1223
1224define <2 x i32> @or_splat_constant(<2 x i32> %x) {
1225; CHECK-LABEL: @or_splat_constant(
1226; CHECK-NEXT:    [[TMP1:%.*]] = or <2 x i32> [[X:%.*]], <i32 42, i32 poison>
1227; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
1228; CHECK-NEXT:    ret <2 x i32> [[R]]
1229;
1230  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
1231  %r = or <2 x i32> %splat, <i32 42, i32 42>
1232  ret <2 x i32> %r
1233}
1234
1235define <2 x i32> @xor_splat_constant(<2 x i32> %x) {
1236; CHECK-LABEL: @xor_splat_constant(
1237; CHECK-NEXT:    [[TMP1:%.*]] = xor <2 x i32> [[X:%.*]], <i32 42, i32 poison>
1238; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x i32> [[TMP1]], <2 x i32> poison, <2 x i32> zeroinitializer
1239; CHECK-NEXT:    ret <2 x i32> [[R]]
1240;
1241  %splat = shufflevector <2 x i32> %x, <2 x i32> undef, <2 x i32> zeroinitializer
1242  %r = xor <2 x i32> %splat, <i32 42, i32 42>
1243  ret <2 x i32> %r
1244}
1245
1246define <2 x float> @fadd_splat_constant(<2 x float> %x) {
1247; CHECK-LABEL: @fadd_splat_constant(
1248; CHECK-NEXT:    [[TMP1:%.*]] = fadd <2 x float> [[X:%.*]], <float 4.200000e+01, float poison>
1249; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> poison, <2 x i32> zeroinitializer
1250; CHECK-NEXT:    ret <2 x float> [[R]]
1251;
1252  %splat = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> zeroinitializer
1253  %r = fadd <2 x float> %splat, <float 42.0, float 42.0>
1254  ret <2 x float> %r
1255}
1256
1257define <2 x float> @fsub_splat_constant0(<2 x float> %x) {
1258; CHECK-LABEL: @fsub_splat_constant0(
1259; CHECK-NEXT:    [[TMP1:%.*]] = fsub <2 x float> <float 4.200000e+01, float poison>, [[X:%.*]]
1260; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> poison, <2 x i32> zeroinitializer
1261; CHECK-NEXT:    ret <2 x float> [[R]]
1262;
1263  %splat = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> zeroinitializer
1264  %r = fsub <2 x float> <float 42.0, float 42.0>, %splat
1265  ret <2 x float> %r
1266}
1267
1268define <2 x float> @fsub_splat_constant1(<2 x float> %x) {
1269; CHECK-LABEL: @fsub_splat_constant1(
1270; CHECK-NEXT:    [[TMP1:%.*]] = fadd <2 x float> [[X:%.*]], <float -4.200000e+01, float poison>
1271; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> poison, <2 x i32> zeroinitializer
1272; CHECK-NEXT:    ret <2 x float> [[R]]
1273;
1274  %splat = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> zeroinitializer
1275  %r = fsub <2 x float> %splat, <float 42.0, float 42.0>
1276  ret <2 x float> %r
1277}
1278
1279define <2 x float> @fneg(<2 x float> %x) {
1280; CHECK-LABEL: @fneg(
1281; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <2 x float> [[X:%.*]], <2 x float> poison, <2 x i32> zeroinitializer
1282; CHECK-NEXT:    [[R:%.*]] = fneg <2 x float> [[TMP1]]
1283; CHECK-NEXT:    ret <2 x float> [[R]]
1284;
1285  %splat = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> zeroinitializer
1286  %r = fsub <2 x float> <float -0.0, float -0.0>, %splat
1287  ret <2 x float> %r
1288}
1289
1290define <2 x float> @fmul_splat_constant(<2 x float> %x) {
1291; CHECK-LABEL: @fmul_splat_constant(
1292; CHECK-NEXT:    [[TMP1:%.*]] = fmul <2 x float> [[X:%.*]], <float 4.200000e+01, float poison>
1293; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> poison, <2 x i32> zeroinitializer
1294; CHECK-NEXT:    ret <2 x float> [[R]]
1295;
1296  %splat = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> zeroinitializer
1297  %r = fmul <2 x float> %splat, <float 42.0, float 42.0>
1298  ret <2 x float> %r
1299}
1300
1301define <2 x float> @fdiv_splat_constant0(<2 x float> %x) {
1302; CHECK-LABEL: @fdiv_splat_constant0(
1303; CHECK-NEXT:    [[TMP1:%.*]] = fdiv <2 x float> <float 4.200000e+01, float poison>, [[X:%.*]]
1304; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> poison, <2 x i32> zeroinitializer
1305; CHECK-NEXT:    ret <2 x float> [[R]]
1306;
1307  %splat = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> zeroinitializer
1308  %r = fdiv <2 x float> <float 42.0, float 42.0>, %splat
1309  ret <2 x float> %r
1310}
1311
1312define <2 x float> @fdiv_splat_constant1(<2 x float> %x) {
1313; CHECK-LABEL: @fdiv_splat_constant1(
1314; CHECK-NEXT:    [[TMP1:%.*]] = fdiv <2 x float> [[X:%.*]], <float 4.200000e+01, float poison>
1315; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> poison, <2 x i32> zeroinitializer
1316; CHECK-NEXT:    ret <2 x float> [[R]]
1317;
1318  %splat = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> zeroinitializer
1319  %r = fdiv <2 x float> %splat, <float 42.0, float 42.0>
1320  ret <2 x float> %r
1321}
1322
1323define <2 x float> @frem_splat_constant0(<2 x float> %x) {
1324; CHECK-LABEL: @frem_splat_constant0(
1325; CHECK-NEXT:    [[TMP1:%.*]] = frem <2 x float> <float 4.200000e+01, float poison>, [[X:%.*]]
1326; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> poison, <2 x i32> zeroinitializer
1327; CHECK-NEXT:    ret <2 x float> [[R]]
1328;
1329  %splat = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> zeroinitializer
1330  %r = frem <2 x float> <float 42.0, float 42.0>, %splat
1331  ret <2 x float> %r
1332}
1333
1334define <2 x float> @frem_splat_constant1(<2 x float> %x) {
1335; CHECK-LABEL: @frem_splat_constant1(
1336; CHECK-NEXT:    [[TMP1:%.*]] = frem <2 x float> [[X:%.*]], <float 4.200000e+01, float poison>
1337; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> poison, <2 x i32> zeroinitializer
1338; CHECK-NEXT:    ret <2 x float> [[R]]
1339;
1340  %splat = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> zeroinitializer
1341  %r = frem <2 x float> %splat, <float 42.0, float 42.0>
1342  ret <2 x float> %r
1343}
1344
1345; Equivalent shuffle masks, but only one is a narrowing op.
1346
1347define <2 x i1> @PR40734(<1 x i1> %x, <4 x i1> %y) {
1348; CHECK-LABEL: @PR40734(
1349; CHECK-NEXT:    [[WIDEN:%.*]] = shufflevector <1 x i1> zeroinitializer, <1 x i1> [[X:%.*]], <2 x i32> <i32 0, i32 1>
1350; CHECK-NEXT:    [[NARROW:%.*]] = shufflevector <4 x i1> [[Y:%.*]], <4 x i1> undef, <2 x i32> <i32 0, i32 1>
1351; CHECK-NEXT:    [[R:%.*]] = and <2 x i1> [[WIDEN]], [[NARROW]]
1352; CHECK-NEXT:    ret <2 x i1> [[R]]
1353;
1354  %widen = shufflevector <1 x i1> zeroinitializer, <1 x i1> %x, <2 x i32> <i32 0, i32 1>
1355  %narrow = shufflevector <4 x i1> %y, <4 x i1> undef, <2 x i32> <i32 0, i32 1>
1356  %r = and <2 x i1> %widen, %narrow
1357  ret <2 x i1> %r
1358}
1359
1360; Negative test - do not transform non-power-of-2 unless we know the backend handles these sequences identically.
1361
1362define <7 x i8> @insert_subvector_shuffles(<3 x i8> %x, <3 x i8> %y) {
1363; CHECK-LABEL: @insert_subvector_shuffles(
1364; CHECK-NEXT:    [[S1:%.*]] = shufflevector <3 x i8> [[X:%.*]], <3 x i8> undef, <7 x i32> <i32 0, i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
1365; CHECK-NEXT:    [[S2:%.*]] = shufflevector <3 x i8> [[Y:%.*]], <3 x i8> undef, <7 x i32> <i32 undef, i32 1, i32 2, i32 undef, i32 undef, i32 undef, i32 undef>
1366; CHECK-NEXT:    [[S3:%.*]] = shufflevector <7 x i8> [[S1]], <7 x i8> [[S2]], <7 x i32> <i32 0, i32 8, i32 1, i32 undef, i32 8, i32 1, i32 9>
1367; CHECK-NEXT:    ret <7 x i8> [[S3]]
1368;
1369  %s1 = shufflevector <3 x i8> %x, <3 x i8> undef, <7 x i32> <i32 0, i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
1370  %s2 = shufflevector <3 x i8> %y, <3 x i8> undef, <7 x i32> <i32 undef, i32 1, i32 2, i32 undef, i32 undef, i32 undef, i32 undef>
1371  %s3 = shufflevector <7 x i8> %s1, <7 x i8> %s2, <7 x i32> <i32 0, i32 8, i32 1, i32 undef, i32 8, i32 1, i32 9>
1372  ret <7 x i8> %s3
1373}
1374
1375define <8 x i8> @insert_subvector_shuffles_pow2elts(<2 x i8> %x, <2 x i8> %y) {
1376; CHECK-LABEL: @insert_subvector_shuffles_pow2elts(
1377; CHECK-NEXT:    [[S3:%.*]] = shufflevector <2 x i8> [[X:%.*]], <2 x i8> [[Y:%.*]], <8 x i32> <i32 0, i32 2, i32 1, i32 undef, i32 2, i32 1, i32 3, i32 0>
1378; CHECK-NEXT:    ret <8 x i8> [[S3]]
1379;
1380  %s1 = shufflevector <2 x i8> %x, <2 x i8> undef, <8 x i32> <i32 0, i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
1381  %s2 = shufflevector <2 x i8> %y, <2 x i8> undef, <8 x i32> <i32 0, i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
1382  %s3 = shufflevector <8 x i8> %s1, <8 x i8> %s2, <8 x i32> <i32 0, i32 8, i32 1, i32 undef, i32 8, i32 1, i32 9, i32 0>
1383  ret <8 x i8> %s3
1384}
1385
1386; The last shuffle may change the vector type.
1387; Negative test - do not transform non-power-of-2 unless we know the backend handles these sequences identically.
1388
1389define <2 x i8> @insert_subvector_shuffles_narrowing(<3 x i8> %x, <3 x i8> %y) {
1390; CHECK-LABEL: @insert_subvector_shuffles_narrowing(
1391; CHECK-NEXT:    [[S1:%.*]] = shufflevector <3 x i8> [[X:%.*]], <3 x i8> undef, <7 x i32> <i32 0, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
1392; CHECK-NEXT:    [[S2:%.*]] = shufflevector <3 x i8> [[Y:%.*]], <3 x i8> undef, <7 x i32> <i32 undef, i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
1393; CHECK-NEXT:    [[S3:%.*]] = shufflevector <7 x i8> [[S1]], <7 x i8> [[S2]], <2 x i32> <i32 0, i32 8>
1394; CHECK-NEXT:    ret <2 x i8> [[S3]]
1395;
1396  %s1 = shufflevector <3 x i8> %x, <3 x i8> undef, <7 x i32> <i32 0, i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
1397  %s2 = shufflevector <3 x i8> %y, <3 x i8> undef, <7 x i32> <i32 undef, i32 1, i32 2, i32 undef, i32 undef, i32 undef, i32 undef>
1398  %s3 = shufflevector <7 x i8> %s1, <7 x i8> %s2, <2 x i32> <i32 0, i32 8>
1399  ret <2 x i8> %s3
1400}
1401
1402define <2 x i8> @insert_subvector_shuffles_narrowing_pow2elts(<4 x i8> %x, <4 x i8> %y) {
1403; CHECK-LABEL: @insert_subvector_shuffles_narrowing_pow2elts(
1404; CHECK-NEXT:    [[S3:%.*]] = shufflevector <4 x i8> [[X:%.*]], <4 x i8> [[Y:%.*]], <2 x i32> <i32 0, i32 4>
1405; CHECK-NEXT:    ret <2 x i8> [[S3]]
1406;
1407  %s1 = shufflevector <4 x i8> %x, <4 x i8> undef, <8 x i32> <i32 0, i32 1, i32 2, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
1408  %s2 = shufflevector <4 x i8> %y, <4 x i8> undef, <8 x i32> <i32 0, i32 1, i32 undef, i32 3, i32 undef, i32 undef, i32 undef, i32 undef>
1409  %s3 = shufflevector <8 x i8> %s1, <8 x i8> %s2, <2 x i32> <i32 0, i32 8>
1410  ret <2 x i8> %s3
1411}
1412
1413; Similar to above, but this reduces to a widen with undefs of 'x'.
1414
1415define <4 x double> @insert_subvector_shuffles_identity(<2 x double> %x) {
1416; CHECK-LABEL: @insert_subvector_shuffles_identity(
1417; CHECK-NEXT:    [[S3:%.*]] = shufflevector <2 x double> [[X:%.*]], <2 x double> poison, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
1418; CHECK-NEXT:    ret <4 x double> [[S3]]
1419;
1420  %s1 = shufflevector <2 x double> %x, <2 x double> undef, <4 x i32> <i32 undef, i32 1, i32 undef, i32 undef>
1421  %s2 = shufflevector <2 x double> %x, <2 x double> undef, <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
1422  %s3 = shufflevector <4 x double> %s2, <4 x double> %s1, <4 x i32> <i32 0, i32 5, i32 undef, i32 undef>
1423  ret <4 x double> %s3
1424}
1425
1426; Negative test - not identity with padding (although this could be folded with better analysis).
1427
1428define <4 x double> @not_insert_subvector_shuffle(<2 x double> %x) {
1429; CHECK-LABEL: @not_insert_subvector_shuffle(
1430; CHECK-NEXT:    [[S1:%.*]] = shufflevector <2 x double> [[X:%.*]], <2 x double> undef, <4 x i32> <i32 undef, i32 1, i32 undef, i32 1>
1431; CHECK-NEXT:    [[S2:%.*]] = shufflevector <2 x double> [[X]], <2 x double> undef, <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
1432; CHECK-NEXT:    [[S3:%.*]] = shufflevector <4 x double> [[S2]], <4 x double> [[S1]], <4 x i32> <i32 0, i32 5, i32 7, i32 undef>
1433; CHECK-NEXT:    ret <4 x double> [[S3]]
1434;
1435  %s1 = shufflevector <2 x double> %x, <2 x double> undef, <4 x i32> <i32 undef, i32 1, i32 undef, i32 1>
1436  %s2 = shufflevector <2 x double> %x, <2 x double> undef, <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
1437  %s3 = shufflevector <4 x double> %s2, <4 x double> %s1, <4 x i32> <i32 0, i32 5, i32 7, i32 undef>
1438  ret <4 x double> %s3
1439}
1440
1441; Negative test - operands are not the same size (although this could be partly folded with better analysis).
1442
1443define <4 x double> @not_insert_subvector_shuffles_with_same_size(<2 x double> %x, <3 x double> %y) {
1444; CHECK-LABEL: @not_insert_subvector_shuffles_with_same_size(
1445; CHECK-NEXT:    [[S1:%.*]] = shufflevector <2 x double> [[X:%.*]], <2 x double> undef, <4 x i32> <i32 undef, i32 1, i32 undef, i32 undef>
1446; CHECK-NEXT:    [[S2:%.*]] = shufflevector <3 x double> [[Y:%.*]], <3 x double> undef, <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
1447; CHECK-NEXT:    [[S3:%.*]] = shufflevector <4 x double> [[S2]], <4 x double> [[S1]], <4 x i32> <i32 0, i32 5, i32 undef, i32 undef>
1448; CHECK-NEXT:    ret <4 x double> [[S3]]
1449;
1450  %s1 = shufflevector <2 x double> %x, <2 x double> undef, <4 x i32> <i32 undef, i32 1, i32 undef, i32 undef>
1451  %s2 = shufflevector <3 x double> %y, <3 x double> undef, <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef>
1452  %s3 = shufflevector <4 x double> %s2, <4 x double> %s1, <4 x i32> <i32 0, i32 5, i32 undef, i32 undef>
1453  ret <4 x double> %s3
1454}
1455
1456; Demanded vector elements may not be able to simplify a shuffle mask
1457; before we try to narrow it. This used to crash.
1458
1459define <4 x float> @insert_subvector_crash_invalid_mask_elt(<2 x float> %x, <4 x float>* %p) {
1460; CHECK-LABEL: @insert_subvector_crash_invalid_mask_elt(
1461; CHECK-NEXT:    [[WIDEN:%.*]] = shufflevector <2 x float> [[X:%.*]], <2 x float> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
1462; CHECK-NEXT:    [[I:%.*]] = shufflevector <2 x float> [[X]], <2 x float> poison, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
1463; CHECK-NEXT:    store <4 x float> [[I]], <4 x float>* [[P:%.*]], align 16
1464; CHECK-NEXT:    ret <4 x float> [[WIDEN]]
1465;
1466  %widen = shufflevector <2 x float> %x, <2 x float> undef, <4 x i32> <i32 0, i32 1, i32 undef, i32 undef>
1467  %ext2 = extractelement <2 x float> %x, i32 0
1468  %I = insertelement <4 x float> %widen, float %ext2, i16 0
1469  store <4 x float> %I, <4 x float>* %p
1470  ret <4 x float> %widen
1471}
1472
1473define <4 x i32> @splat_assoc_add(<4 x i32> %x, <4 x i32> %y) {
1474; CHECK-LABEL: @splat_assoc_add(
1475; CHECK-NEXT:    [[TMP1:%.*]] = add <4 x i32> [[X:%.*]], <i32 317426, i32 poison, i32 poison, i32 poison>
1476; CHECK-NEXT:    [[TMP2:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> zeroinitializer
1477; CHECK-NEXT:    [[R:%.*]] = add <4 x i32> [[TMP2]], [[Y:%.*]]
1478; CHECK-NEXT:    ret <4 x i32> [[R]]
1479;
1480  %splatx = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
1481  %a = add <4 x i32> %y, <i32 317426, i32 317426, i32 317426, i32 317426>
1482  %r = add <4 x i32> %splatx, %a
1483  ret <4 x i32> %r
1484}
1485
1486define <vscale x 4 x i32> @vsplat_assoc_add(<vscale x 4 x i32> %x, <vscale x 4 x i32> %y) {
1487; CHECK-LABEL: @vsplat_assoc_add(
1488; CHECK-NEXT:    [[TMP1:%.*]] = add <vscale x 4 x i32> [[X:%.*]], shufflevector (<vscale x 4 x i32> insertelement (<vscale x 4 x i32> undef, i32 317426, i32 0), <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer)
1489; CHECK-NEXT:    [[TMP2:%.*]] = shufflevector <vscale x 4 x i32> [[TMP1]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
1490; CHECK-NEXT:    [[R:%.*]] = add <vscale x 4 x i32> [[TMP2]], [[Y:%.*]]
1491; CHECK-NEXT:    ret <vscale x 4 x i32> [[R]]
1492;
1493
1494  %splatx = shufflevector <vscale x 4 x i32> %x, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer
1495  %a = add <vscale x 4 x i32> %y, shufflevector (<vscale x 4 x i32> insertelement (<vscale x 4 x i32> undef, i32 317426, i32 0), <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer)
1496  %r = add <vscale x 4 x i32> %splatx, %a
1497  ret <vscale x 4 x i32> %r
1498}
1499
1500; Undefs in splat mask are replaced with defined splat index
1501
1502define <4 x i32> @splat_assoc_add_undef_mask_elts(<4 x i32> %x, <4 x i32> %y) {
1503; CHECK-LABEL: @splat_assoc_add_undef_mask_elts(
1504; CHECK-NEXT:    [[TMP1:%.*]] = add <4 x i32> [[X:%.*]], <i32 42, i32 poison, i32 poison, i32 poison>
1505; CHECK-NEXT:    [[TMP2:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> zeroinitializer
1506; CHECK-NEXT:    [[R:%.*]] = add <4 x i32> [[TMP2]], [[Y:%.*]]
1507; CHECK-NEXT:    ret <4 x i32> [[R]]
1508;
1509  %splatx = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 0, i32 0, i32 undef, i32 undef>
1510  %a = add <4 x i32> %y, <i32 42, i32 42, i32 42, i32 42>
1511  %r = add <4 x i32> %splatx, %a
1512  ret <4 x i32> %r
1513}
1514
1515; Undefs in splat mask are replaced with defined splat index
1516
1517define <4 x i32> @splat_assoc_add_undef_mask_elt_at_splat_index(<4 x i32> %x, <4 x i32> %y) {
1518; CHECK-LABEL: @splat_assoc_add_undef_mask_elt_at_splat_index(
1519; CHECK-NEXT:    [[TMP1:%.*]] = add <4 x i32> [[X:%.*]], <i32 42, i32 poison, i32 poison, i32 poison>
1520; CHECK-NEXT:    [[TMP2:%.*]] = shufflevector <4 x i32> [[TMP1]], <4 x i32> poison, <4 x i32> zeroinitializer
1521; CHECK-NEXT:    [[R:%.*]] = add <4 x i32> [[TMP2]], [[Y:%.*]]
1522; CHECK-NEXT:    ret <4 x i32> [[R]]
1523;
1524  %splatx = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 undef, i32 0, i32 0, i32 0>
1525  %a = add <4 x i32> %y, <i32 42, i32 42, i32 42, i32 42>
1526  %r = add <4 x i32> %splatx, %a
1527  ret <4 x i32> %r
1528}
1529
1530define <4 x i32> @splat_assoc_add_undef_constant_elts(<4 x i32> %x, <4 x i32> %y) {
1531; CHECK-LABEL: @splat_assoc_add_undef_constant_elts(
1532; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
1533; CHECK-NEXT:    [[A:%.*]] = add <4 x i32> [[Y:%.*]], <i32 42, i32 undef, i32 undef, i32 42>
1534; CHECK-NEXT:    [[R:%.*]] = add <4 x i32> [[SPLATX]], [[A]]
1535; CHECK-NEXT:    ret <4 x i32> [[R]]
1536;
1537  %splatx = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
1538  %a = add <4 x i32> %y, <i32 42, i32 undef, i32 undef, i32 42>
1539  %r = add <4 x i32> %splatx, %a
1540  ret <4 x i32> %r
1541}
1542
1543define <4 x i32> @splat_assoc_add_undef_constant_elt_at_splat_index(<4 x i32> %x, <4 x i32> %y) {
1544; CHECK-LABEL: @splat_assoc_add_undef_constant_elt_at_splat_index(
1545; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
1546; CHECK-NEXT:    [[A:%.*]] = add <4 x i32> [[Y:%.*]], <i32 undef, i32 42, i32 undef, i32 42>
1547; CHECK-NEXT:    [[R:%.*]] = add <4 x i32> [[SPLATX]], [[A]]
1548; CHECK-NEXT:    ret <4 x i32> [[R]]
1549;
1550  %splatx = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
1551  %a = add <4 x i32> %y, <i32 undef, i32 42, i32 undef, i32 42>
1552  %r = add <4 x i32> %splatx, %a
1553  ret <4 x i32> %r
1554}
1555
1556define <4 x i32> @splat_assoc_add_undef_mask_elts_undef_constant_elts(<4 x i32> %x, <4 x i32> %y) {
1557; CHECK-LABEL: @splat_assoc_add_undef_mask_elts_undef_constant_elts(
1558; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> <i32 0, i32 undef, i32 0, i32 undef>
1559; CHECK-NEXT:    [[A:%.*]] = add <4 x i32> [[Y:%.*]], <i32 42, i32 undef, i32 undef, i32 42>
1560; CHECK-NEXT:    [[R:%.*]] = add <4 x i32> [[SPLATX]], [[A]]
1561; CHECK-NEXT:    ret <4 x i32> [[R]]
1562;
1563  %splatx = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 0, i32 undef, i32 0, i32 undef>
1564  %a = add <4 x i32> %y, <i32 42, i32 undef, i32 undef, i32 42>
1565  %r = add <4 x i32> %splatx, %a
1566  ret <4 x i32> %r
1567}
1568
1569define <4 x i32> @splat_assoc_add_undef_mask_elt_at_splat_index_undef_constant_elts(<4 x i32> %x, <4 x i32> %y) {
1570; CHECK-LABEL: @splat_assoc_add_undef_mask_elt_at_splat_index_undef_constant_elts(
1571; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> <i32 undef, i32 0, i32 0, i32 0>
1572; CHECK-NEXT:    [[A:%.*]] = add <4 x i32> [[Y:%.*]], <i32 42, i32 undef, i32 undef, i32 42>
1573; CHECK-NEXT:    [[R:%.*]] = add <4 x i32> [[SPLATX]], [[A]]
1574; CHECK-NEXT:    ret <4 x i32> [[R]]
1575;
1576  %splatx = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 undef, i32 0, i32 0, i32 0>
1577  %a = add <4 x i32> %y, <i32 42, i32 undef, i32 undef, i32 42>
1578  %r = add <4 x i32> %splatx, %a
1579  ret <4 x i32> %r
1580}
1581
1582define <4 x i32> @splat_assoc_add_undef_mask_elt_at_splat_index_undef_constant_elt_at_splat_index(<4 x i32> %x, <4 x i32> %y) {
1583; CHECK-LABEL: @splat_assoc_add_undef_mask_elt_at_splat_index_undef_constant_elt_at_splat_index(
1584; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> <i32 undef, i32 0, i32 0, i32 0>
1585; CHECK-NEXT:    [[A:%.*]] = add <4 x i32> [[Y:%.*]], <i32 undef, i32 42, i32 undef, i32 42>
1586; CHECK-NEXT:    [[R:%.*]] = add <4 x i32> [[SPLATX]], [[A]]
1587; CHECK-NEXT:    ret <4 x i32> [[R]]
1588;
1589  %splatx = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 undef, i32 0, i32 0, i32 0>
1590  %a = add <4 x i32> %y, <i32 undef, i32 42, i32 undef, i32 42>
1591  %r = add <4 x i32> %splatx, %a
1592  ret <4 x i32> %r
1593}
1594
1595; Non-zero splat index; commute operands; FMF intersect
1596
1597define <2 x float> @splat_assoc_fmul(<2 x float> %x, <2 x float> %y) {
1598; CHECK-LABEL: @splat_assoc_fmul(
1599; CHECK-NEXT:    [[TMP1:%.*]] = fmul reassoc nsz <2 x float> [[X:%.*]], <float poison, float 3.000000e+00>
1600; CHECK-NEXT:    [[TMP2:%.*]] = shufflevector <2 x float> [[TMP1]], <2 x float> poison, <2 x i32> <i32 1, i32 1>
1601; CHECK-NEXT:    [[R:%.*]] = fmul reassoc nsz <2 x float> [[TMP2]], [[Y:%.*]]
1602; CHECK-NEXT:    ret <2 x float> [[R]]
1603;
1604  %splatx = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> <i32 1, i32 1>
1605  %a = fmul reassoc nsz <2 x float> %y, <float 3.0, float 3.0>
1606  %r = fmul reassoc nsz nnan <2 x float> %a, %splatx
1607  ret <2 x float> %r
1608}
1609
1610; Two splat shuffles; drop poison-generating flags
1611
1612define <3 x i8> @splat_assoc_mul(<3 x i8> %x, <3 x i8> %y, <3 x i8> %z) {
1613; CHECK-LABEL: @splat_assoc_mul(
1614; CHECK-NEXT:    [[TMP1:%.*]] = mul <3 x i8> [[Z:%.*]], [[X:%.*]]
1615; CHECK-NEXT:    [[TMP2:%.*]] = shufflevector <3 x i8> [[TMP1]], <3 x i8> poison, <3 x i32> <i32 2, i32 2, i32 2>
1616; CHECK-NEXT:    [[R:%.*]] = mul <3 x i8> [[TMP2]], [[Y:%.*]]
1617; CHECK-NEXT:    ret <3 x i8> [[R]]
1618;
1619  %splatx = shufflevector <3 x i8> %x, <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 2>
1620  %splatz = shufflevector <3 x i8> %z, <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 2>
1621  %a = mul nsw <3 x i8> %y, %splatz
1622  %r = mul <3 x i8> %a, %splatx
1623  ret <3 x i8> %r
1624}
1625
1626define <3 x i8> @splat_assoc_mul_undef_elt1(<3 x i8> %x, <3 x i8> %y, <3 x i8> %z) {
1627; CHECK-LABEL: @splat_assoc_mul_undef_elt1(
1628; CHECK-NEXT:    [[TMP1:%.*]] = mul <3 x i8> [[Z:%.*]], [[X:%.*]]
1629; CHECK-NEXT:    [[TMP2:%.*]] = shufflevector <3 x i8> [[TMP1]], <3 x i8> poison, <3 x i32> <i32 2, i32 2, i32 2>
1630; CHECK-NEXT:    [[R:%.*]] = mul <3 x i8> [[TMP2]], [[Y:%.*]]
1631; CHECK-NEXT:    ret <3 x i8> [[R]]
1632;
1633  %splatx = shufflevector <3 x i8> %x, <3 x i8> undef, <3 x i32> <i32 undef, i32 2, i32 2>
1634  %splatz = shufflevector <3 x i8> %z, <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 2>
1635  %a = mul nsw <3 x i8> %y, %splatz
1636  %r = mul nsw nuw <3 x i8> %a, %splatx
1637  ret <3 x i8> %r
1638}
1639
1640define <3 x i8> @splat_assoc_mul_undef_elt2(<3 x i8> %x, <3 x i8> %y, <3 x i8> %z) {
1641; CHECK-LABEL: @splat_assoc_mul_undef_elt2(
1642; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <3 x i8> [[X:%.*]], <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 2>
1643; CHECK-NEXT:    [[SPLATZ:%.*]] = shufflevector <3 x i8> [[Z:%.*]], <3 x i8> undef, <3 x i32> <i32 undef, i32 2, i32 2>
1644; CHECK-NEXT:    [[A:%.*]] = mul nsw <3 x i8> [[SPLATZ]], [[Y:%.*]]
1645; CHECK-NEXT:    [[R:%.*]] = mul nuw nsw <3 x i8> [[A]], [[SPLATX]]
1646; CHECK-NEXT:    ret <3 x i8> [[R]]
1647;
1648  %splatx = shufflevector <3 x i8> %x, <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 2>
1649  %splatz = shufflevector <3 x i8> %z, <3 x i8> undef, <3 x i32> <i32 undef, i32 2, i32 2>
1650  %a = mul nsw <3 x i8> %y, %splatz
1651  %r = mul nsw nuw <3 x i8> %a, %splatx
1652  ret <3 x i8> %r
1653}
1654
1655define <3 x i8> @splat_assoc_mul_undef_elt_at_splat_index1(<3 x i8> %x, <3 x i8> %y, <3 x i8> %z) {
1656; CHECK-LABEL: @splat_assoc_mul_undef_elt_at_splat_index1(
1657; CHECK-NEXT:    [[TMP1:%.*]] = mul <3 x i8> [[Z:%.*]], [[X:%.*]]
1658; CHECK-NEXT:    [[TMP2:%.*]] = shufflevector <3 x i8> [[TMP1]], <3 x i8> poison, <3 x i32> <i32 2, i32 2, i32 2>
1659; CHECK-NEXT:    [[R:%.*]] = mul <3 x i8> [[TMP2]], [[Y:%.*]]
1660; CHECK-NEXT:    ret <3 x i8> [[R]]
1661;
1662  %splatx = shufflevector <3 x i8> %x, <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 undef>
1663  %splatz = shufflevector <3 x i8> %z, <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 2>
1664  %a = mul nsw <3 x i8> %y, %splatz
1665  %r = mul nsw nuw <3 x i8> %a, %splatx
1666  ret <3 x i8> %r
1667}
1668
1669define <3 x i8> @splat_assoc_mul_undef_elt_at_splat_index2(<3 x i8> %x, <3 x i8> %y, <3 x i8> %z) {
1670; CHECK-LABEL: @splat_assoc_mul_undef_elt_at_splat_index2(
1671; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <3 x i8> [[X:%.*]], <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 2>
1672; CHECK-NEXT:    [[SPLATZ:%.*]] = shufflevector <3 x i8> [[Z:%.*]], <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 undef>
1673; CHECK-NEXT:    [[A:%.*]] = mul nsw <3 x i8> [[SPLATZ]], [[Y:%.*]]
1674; CHECK-NEXT:    [[R:%.*]] = mul nuw nsw <3 x i8> [[A]], [[SPLATX]]
1675; CHECK-NEXT:    ret <3 x i8> [[R]]
1676;
1677  %splatx = shufflevector <3 x i8> %x, <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 2>
1678  %splatz = shufflevector <3 x i8> %z, <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 undef>
1679  %a = mul nsw <3 x i8> %y, %splatz
1680  %r = mul nsw nuw <3 x i8> %a, %splatx
1681  ret <3 x i8> %r
1682}
1683
1684; Negative test - mismatched splat elements
1685
1686define <3 x i8> @splat_assoc_or(<3 x i8> %x, <3 x i8> %y, <3 x i8> %z) {
1687; CHECK-LABEL: @splat_assoc_or(
1688; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <3 x i8> [[X:%.*]], <3 x i8> undef, <3 x i32> <i32 1, i32 1, i32 1>
1689; CHECK-NEXT:    [[SPLATZ:%.*]] = shufflevector <3 x i8> [[Z:%.*]], <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 2>
1690; CHECK-NEXT:    [[A:%.*]] = or <3 x i8> [[SPLATZ]], [[Y:%.*]]
1691; CHECK-NEXT:    [[R:%.*]] = or <3 x i8> [[A]], [[SPLATX]]
1692; CHECK-NEXT:    ret <3 x i8> [[R]]
1693;
1694  %splatx = shufflevector <3 x i8> %x, <3 x i8> undef, <3 x i32> <i32 1, i32 1, i32 1>
1695  %splatz = shufflevector <3 x i8> %z, <3 x i8> undef, <3 x i32> <i32 2, i32 2, i32 2>
1696  %a = or <3 x i8> %y, %splatz
1697  %r = or <3 x i8> %a, %splatx
1698  ret <3 x i8> %r
1699}
1700
1701; Negative test - not associative
1702
1703define <2 x float> @splat_assoc_fdiv(<2 x float> %x, <2 x float> %y) {
1704; CHECK-LABEL: @splat_assoc_fdiv(
1705; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <2 x float> [[X:%.*]], <2 x float> undef, <2 x i32> zeroinitializer
1706; CHECK-NEXT:    [[A:%.*]] = fdiv reassoc nsz <2 x float> [[Y:%.*]], <float 3.000000e+00, float 3.000000e+00>
1707; CHECK-NEXT:    [[R:%.*]] = fdiv reassoc nsz <2 x float> [[A]], [[SPLATX]]
1708; CHECK-NEXT:    ret <2 x float> [[R]]
1709;
1710  %splatx = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> zeroinitializer
1711  %a = fdiv reassoc nsz <2 x float> %y, <float 3.0, float 3.0>
1712  %r = fdiv reassoc nsz <2 x float> %a, %splatx
1713  ret <2 x float> %r
1714}
1715
1716; Negative test - extra use
1717
1718define <2 x float> @splat_assoc_fadd(<2 x float> %x, <2 x float> %y) {
1719; CHECK-LABEL: @splat_assoc_fadd(
1720; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <2 x float> [[X:%.*]], <2 x float> undef, <2 x i32> <i32 1, i32 1>
1721; CHECK-NEXT:    [[A:%.*]] = fadd fast <2 x float> [[Y:%.*]], <float 3.000000e+00, float 3.000000e+00>
1722; CHECK-NEXT:    call void @use(<2 x float> [[A]])
1723; CHECK-NEXT:    [[R:%.*]] = fadd fast <2 x float> [[A]], [[SPLATX]]
1724; CHECK-NEXT:    ret <2 x float> [[R]]
1725;
1726  %splatx = shufflevector <2 x float> %x, <2 x float> undef, <2 x i32> <i32 1, i32 1>
1727  %a = fadd fast <2 x float> %y, <float 3.0, float 3.0>
1728  call void @use(<2 x float> %a)
1729  %r = fadd fast <2 x float> %a, %splatx
1730  ret <2 x float> %r
1731}
1732
1733; Negative test - narrowing splat
1734
1735define <3 x i32> @splat_assoc_and(<4 x i32> %x, <3 x i32> %y) {
1736; CHECK-LABEL: @splat_assoc_and(
1737; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <3 x i32> zeroinitializer
1738; CHECK-NEXT:    [[A:%.*]] = and <3 x i32> [[Y:%.*]], <i32 42, i32 42, i32 42>
1739; CHECK-NEXT:    [[R:%.*]] = and <3 x i32> [[SPLATX]], [[A]]
1740; CHECK-NEXT:    ret <3 x i32> [[R]]
1741;
1742  %splatx = shufflevector <4 x i32> %x, <4 x i32> undef, <3 x i32> zeroinitializer
1743  %a = and <3 x i32> %y, <i32 42, i32 42, i32 42>
1744  %r = and <3 x i32> %splatx, %a
1745  ret <3 x i32> %r
1746}
1747
1748; Negative test - widening splat
1749
1750define <5 x i32> @splat_assoc_xor(<4 x i32> %x, <5 x i32> %y) {
1751; CHECK-LABEL: @splat_assoc_xor(
1752; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <5 x i32> zeroinitializer
1753; CHECK-NEXT:    [[TMP1:%.*]] = xor <5 x i32> [[SPLATX]], [[Y:%.*]]
1754; CHECK-NEXT:    [[R:%.*]] = xor <5 x i32> [[TMP1]], <i32 42, i32 42, i32 42, i32 42, i32 42>
1755; CHECK-NEXT:    ret <5 x i32> [[R]]
1756;
1757  %splatx = shufflevector <4 x i32> %x, <4 x i32> undef, <5 x i32> zeroinitializer
1758  %a = xor <5 x i32> %y, <i32 42, i32 42, i32 42, i32 42, i32 42>
1759  %r = xor <5 x i32> %splatx, %a
1760  ret <5 x i32> %r
1761}
1762
1763; Negative test - opcode mismatch
1764
1765define <4 x i32> @splat_assoc_add_mul(<4 x i32> %x, <4 x i32> %y) {
1766; CHECK-LABEL: @splat_assoc_add_mul(
1767; CHECK-NEXT:    [[SPLATX:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
1768; CHECK-NEXT:    [[A:%.*]] = add <4 x i32> [[Y:%.*]], <i32 42, i32 42, i32 42, i32 42>
1769; CHECK-NEXT:    [[R:%.*]] = mul <4 x i32> [[SPLATX]], [[A]]
1770; CHECK-NEXT:    ret <4 x i32> [[R]]
1771;
1772  %splatx = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
1773  %a = add <4 x i32> %y, <i32 42, i32 42, i32 42, i32 42>
1774  %r = mul <4 x i32> %splatx, %a
1775  ret <4 x i32> %r
1776}
1777
1778
1779; Do not crash on constant expressions.
1780
1781define <4 x i32> @PR46872(<4 x i32> %x) {
1782; CHECK-LABEL: @PR46872(
1783; CHECK-NEXT:    [[S:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> <i32 undef, i32 0, i32 1, i32 1>
1784; CHECK-NEXT:    [[A:%.*]] = and <4 x i32> [[S]], bitcast (<2 x i64> <i64 ptrtoint (<4 x i32> (<4 x i32>)* @PR46872 to i64), i64 ptrtoint (<4 x i32> (<4 x i32>)* @PR46872 to i64)> to <4 x i32>)
1785; CHECK-NEXT:    ret <4 x i32> [[A]]
1786;
1787  %s = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 undef, i32 0, i32 1, i32 1>
1788  %a = and <4 x i32> %s, bitcast (<2 x i64> <i64 ptrtoint (<4 x i32> (<4 x i32>)* @PR46872 to i64), i64 ptrtoint (<4 x i32> (<4 x i32>)* @PR46872 to i64)> to <4 x i32>)
1789  ret <4 x i32> %a
1790}
1791
1792define <2 x float> @fneg_unary_shuf(<2 x float> %x) {
1793; CHECK-LABEL: @fneg_unary_shuf(
1794; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <2 x float> [[X:%.*]], <2 x float> poison, <2 x i32> <i32 1, i32 0>
1795; CHECK-NEXT:    [[R:%.*]] = fneg nnan nsz <2 x float> [[TMP1]]
1796; CHECK-NEXT:    ret <2 x float> [[R]]
1797;
1798  %nx = fneg nsz nnan <2 x float> %x
1799  %r = shufflevector <2 x float> %nx, <2 x float> poison, <2 x i32> <i32 1, i32 0>
1800  ret <2 x float> %r
1801}
1802
1803define <4 x half> @fneg_unary_shuf_widen(<2 x half> %x) {
1804; CHECK-LABEL: @fneg_unary_shuf_widen(
1805; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <2 x half> [[X:%.*]], <2 x half> poison, <4 x i32> <i32 1, i32 0, i32 0, i32 undef>
1806; CHECK-NEXT:    [[R:%.*]] = fneg ninf <4 x half> [[TMP1]]
1807; CHECK-NEXT:    ret <4 x half> [[R]]
1808;
1809  %nx = fneg ninf <2 x half> %x
1810  %r = shufflevector <2 x half> %nx, <2 x half> poison, <4 x i32> <i32 1, i32 0, i32 0, i32 poison>
1811  ret <4 x half> %r
1812}
1813
1814define <2 x double> @fneg_unary_shuf_narrow(<4 x double> %x) {
1815; CHECK-LABEL: @fneg_unary_shuf_narrow(
1816; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <4 x double> [[X:%.*]], <4 x double> poison, <2 x i32> <i32 3, i32 0>
1817; CHECK-NEXT:    [[R:%.*]] = fneg nsz <2 x double> [[TMP1]]
1818; CHECK-NEXT:    ret <2 x double> [[R]]
1819;
1820  %nx = fneg nsz <4 x double> %x
1821  %r = shufflevector <4 x double> %nx, <4 x double> poison, <2 x i32> <i32 3, i32 0>
1822  ret <2 x double> %r
1823}
1824
1825; negative test - extra use prevents canonicalization
1826
1827define <2 x float> @fneg_unary_shuf_use(<2 x float> %x) {
1828; CHECK-LABEL: @fneg_unary_shuf_use(
1829; CHECK-NEXT:    [[NX:%.*]] = fneg nsz <2 x float> [[X:%.*]]
1830; CHECK-NEXT:    call void @use(<2 x float> [[NX]])
1831; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[NX]], <2 x float> poison, <2 x i32> <i32 undef, i32 0>
1832; CHECK-NEXT:    ret <2 x float> [[R]]
1833;
1834  %nx = fneg nsz <2 x float> %x
1835  call void @use(<2 x float> %nx)
1836  %r = shufflevector <2 x float> %nx, <2 x float> poison, <2 x i32> <i32 3, i32 0>
1837  ret <2 x float> %r
1838}
1839
1840; intersect FMF
1841
1842define <4 x float> @fneg_shuf(<4 x float> %x, <4 x float> %y) {
1843; CHECK-LABEL: @fneg_shuf(
1844; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> [[Y:%.*]], <4 x i32> <i32 0, i32 1, i32 4, i32 5>
1845; CHECK-NEXT:    [[R:%.*]] = fneg ninf <4 x float> [[TMP1]]
1846; CHECK-NEXT:    ret <4 x float> [[R]]
1847;
1848  %nx = fneg nsz ninf <4 x float> %x
1849  %ny = fneg nnan ninf <4 x float> %y
1850  %r = shufflevector <4 x float> %nx, <4 x float> %ny, <4 x i32> <i32 0, i32 1, i32 4, i32 5>
1851  ret <4 x float> %r
1852}
1853
1854; length-changing shuffle and extra use are ok
1855
1856define <4 x float> @fneg_shuf_widen_use1(<2 x float> %x, <2 x float> %y) {
1857; CHECK-LABEL: @fneg_shuf_widen_use1(
1858; CHECK-NEXT:    [[NX:%.*]] = fneg nnan <2 x float> [[X:%.*]]
1859; CHECK-NEXT:    call void @use(<2 x float> [[NX]])
1860; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <2 x float> [[X]], <2 x float> [[Y:%.*]], <4 x i32> <i32 undef, i32 1, i32 2, i32 3>
1861; CHECK-NEXT:    [[R:%.*]] = fneg nnan <4 x float> [[TMP1]]
1862; CHECK-NEXT:    ret <4 x float> [[R]]
1863;
1864  %nx = fneg nnan <2 x float> %x
1865  call void @use(<2 x float> %nx)
1866  %ny = fneg nnan <2 x float> %y
1867  %r = shufflevector <2 x float> %nx, <2 x float> %ny, <4 x i32> <i32 poison, i32 1, i32 2, i32 3>
1868  ret <4 x float> %r
1869}
1870
1871; length-changing shuffle and extra use still ok
1872
1873define <2 x float> @fneg_shuf_narrow_use2(<4 x float> %x, <4 x float> %y) {
1874; CHECK-LABEL: @fneg_shuf_narrow_use2(
1875; CHECK-NEXT:    [[NY:%.*]] = fneg nnan nsz <4 x float> [[Y:%.*]]
1876; CHECK-NEXT:    call void @use4(<4 x float> [[NY]])
1877; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> [[Y]], <2 x i32> <i32 3, i32 5>
1878; CHECK-NEXT:    [[R:%.*]] = fneg nnan nsz <2 x float> [[TMP1]]
1879; CHECK-NEXT:    ret <2 x float> [[R]]
1880;
1881  %nx = fneg nsz nnan <4 x float> %x
1882  %ny = fneg nsz nnan <4 x float> %y
1883  call void @use4(<4 x float> %ny)
1884  %r = shufflevector <4 x float> %nx, <4 x float> %ny, <2 x i32> <i32 3, i32 5>
1885  ret <2 x float> %r
1886}
1887
1888; negative test - too many extra uses
1889
1890define <2 x float> @fneg_shuf_use3(<2 x float> %x, <2 x float> %y) {
1891; CHECK-LABEL: @fneg_shuf_use3(
1892; CHECK-NEXT:    [[NX:%.*]] = fneg nnan <2 x float> [[X:%.*]]
1893; CHECK-NEXT:    call void @use(<2 x float> [[NX]])
1894; CHECK-NEXT:    [[NY:%.*]] = fneg nnan <2 x float> [[Y:%.*]]
1895; CHECK-NEXT:    call void @use(<2 x float> [[NY]])
1896; CHECK-NEXT:    [[R:%.*]] = shufflevector <2 x float> [[NX]], <2 x float> [[NY]], <2 x i32> <i32 1, i32 2>
1897; CHECK-NEXT:    ret <2 x float> [[R]]
1898;
1899  %nx = fneg nnan <2 x float> %x
1900  call void @use(<2 x float> %nx)
1901  %ny = fneg nnan <2 x float> %y
1902  call void @use(<2 x float> %ny)
1903  %r = shufflevector <2 x float> %nx, <2 x float> %ny, <2 x i32> <i32 1, i32 2>
1904  ret <2 x float> %r
1905}
1906