1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -instsimplify -S | FileCheck %s
3
4define <4 x i32> @const_folding(<4 x i32> %x) {
5; CHECK-LABEL: @const_folding(
6; CHECK-NEXT:    ret <4 x i32> zeroinitializer
7;
8  %shuf = shufflevector <4 x i32> %x, <4 x i32> zeroinitializer, <4 x i32> <i32 5, i32 4, i32 5, i32 4>
9  ret <4 x i32> %shuf
10}
11
12define <4 x i32> @const_folding1(<4 x i32> %x) {
13; CHECK-LABEL: @const_folding1(
14; CHECK-NEXT:    ret <4 x i32> <i32 5, i32 5, i32 5, i32 5>
15;
16  %shuf = shufflevector <4 x i32> <i32 5, i32 4, i32 5, i32 4>, <4 x i32> %x, <4 x i32> zeroinitializer
17  ret <4 x i32> %shuf
18}
19
20define <4 x i32> @const_folding_negative(<3 x i32> %x) {
21; CHECK-LABEL: @const_folding_negative(
22; CHECK-NEXT:    [[SHUF:%.*]] = shufflevector <3 x i32> [[X:%.*]], <3 x i32> zeroinitializer, <4 x i32> <i32 2, i32 4, i32 5, i32 4>
23; CHECK-NEXT:    ret <4 x i32> [[SHUF]]
24;
25  %shuf = shufflevector <3 x i32> %x, <3 x i32> zeroinitializer, <4 x i32> <i32 2, i32 4, i32 5, i32 4>
26  ret <4 x i32> %shuf
27}
28
29define <4 x i32> @splat_operand(<4 x i32> %x) {
30; CHECK-LABEL: @splat_operand(
31; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
32; CHECK-NEXT:    ret <4 x i32> [[SPLAT]]
33;
34  %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
35  %shuf = shufflevector <4 x i32> %splat, <4 x i32> undef, <4 x i32> <i32 0, i32 3, i32 2, i32 1>
36  ret <4 x i32> %shuf
37}
38
39define <4 x i32> @splat_operand1(<4 x i32> %x, <4 x i32> %y) {
40; CHECK-LABEL: @splat_operand1(
41; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> [[Y:%.*]], <4 x i32> zeroinitializer
42; CHECK-NEXT:    ret <4 x i32> [[SPLAT]]
43;
44  %splat = shufflevector <4 x i32> %x, <4 x i32> %y, <4 x i32> zeroinitializer
45  %shuf = shufflevector <4 x i32> %splat, <4 x i32> undef, <4 x i32> <i32 0, i32 3, i32 2, i32 1>
46  ret <4 x i32> %shuf
47}
48
49define <4 x i32> @splat_operand2(<4 x i32> %x, <4 x i32> %y) {
50; CHECK-LABEL: @splat_operand2(
51; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
52; CHECK-NEXT:    ret <4 x i32> [[SPLAT]]
53;
54  %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
55  %shuf = shufflevector <4 x i32> %splat, <4 x i32> %y, <4 x i32> <i32 0, i32 3, i32 2, i32 1>
56  ret <4 x i32> %shuf
57}
58
59define <4 x i32> @splat_operand3(<4 x i32> %x) {
60; CHECK-LABEL: @splat_operand3(
61; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
62; CHECK-NEXT:    ret <4 x i32> [[SPLAT]]
63;
64  %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
65  %shuf = shufflevector <4 x i32> zeroinitializer, <4 x i32> %splat, <4 x i32> <i32 7, i32 6, i32 5, i32 5>
66  ret <4 x i32> %shuf
67}
68
69define <8 x i32> @splat_operand_negative(<4 x i32> %x) {
70; CHECK-LABEL: @splat_operand_negative(
71; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
72; CHECK-NEXT:    [[SHUF:%.*]] = shufflevector <4 x i32> [[SPLAT]], <4 x i32> undef, <8 x i32> <i32 0, i32 3, i32 2, i32 1, i32 undef, i32 undef, i32 undef, i32 undef>
73; CHECK-NEXT:    ret <8 x i32> [[SHUF]]
74;
75  %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
76  %shuf = shufflevector <4 x i32> %splat, <4 x i32> undef, <8 x i32> <i32 0, i32 3, i32 2, i32 1, i32 undef, i32 undef, i32 undef, i32 undef>
77  ret <8 x i32> %shuf
78}
79
80define <4 x i32> @splat_operand_negative2(<4 x i32> %x, <4 x i32> %y) {
81; CHECK-LABEL: @splat_operand_negative2(
82; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
83; CHECK-NEXT:    [[SHUF:%.*]] = shufflevector <4 x i32> [[SPLAT]], <4 x i32> [[Y:%.*]], <4 x i32> <i32 0, i32 3, i32 4, i32 1>
84; CHECK-NEXT:    ret <4 x i32> [[SHUF]]
85;
86  %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
87  %shuf = shufflevector <4 x i32> %splat, <4 x i32> %y, <4 x i32> <i32 0, i32 3, i32 4, i32 1>
88  ret <4 x i32> %shuf
89}
90
91define <4 x i32> @splat_operand_negative3(<4 x i32> %x, <4 x i32> %y) {
92; CHECK-LABEL: @splat_operand_negative3(
93; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
94; CHECK-NEXT:    [[SHUF:%.*]] = shufflevector <4 x i32> [[Y:%.*]], <4 x i32> [[SPLAT]], <4 x i32> <i32 0, i32 3, i32 4, i32 1>
95; CHECK-NEXT:    ret <4 x i32> [[SHUF]]
96;
97  %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
98  %shuf = shufflevector <4 x i32> %y, <4 x i32> %splat, <4 x i32> <i32 0, i32 3, i32 4, i32 1>
99  ret <4 x i32> %shuf
100}
101
102define <4 x i32> @splat_operand_negative4(<4 x i32> %x) {
103; CHECK-LABEL: @splat_operand_negative4(
104; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> <i32 2, i32 undef, i32 2, i32 undef>
105; CHECK-NEXT:    [[SHUF:%.*]] = shufflevector <4 x i32> [[SPLAT]], <4 x i32> undef, <4 x i32> <i32 0, i32 2, i32 undef, i32 undef>
106; CHECK-NEXT:    ret <4 x i32> [[SHUF]]
107;
108  %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 2, i32 undef, i32 2, i32 undef>
109  %shuf = shufflevector <4 x i32> %splat, <4 x i32> undef, <4 x i32> <i32 0, i32 2, i32 undef, i32 undef>
110  ret <4 x i32> %shuf
111}
112
113define <4 x i32> @undef_mask(<4 x i32> %x) {
114; CHECK-LABEL: @undef_mask(
115; CHECK-NEXT:    ret <4 x i32> undef
116;
117  %shuf = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> undef
118  ret <4 x i32> %shuf
119}
120
121define <4 x i32> @undef_mask_1(<4 x i32> %x, <4 x i32> %y) {
122; CHECK-LABEL: @undef_mask_1(
123; CHECK-NEXT:    ret <4 x i32> undef
124;
125  %shuf = shufflevector <4 x i32> %x, <4 x i32> %y, <4 x i32> undef
126  ret <4 x i32> %shuf
127}
128
129define <4 x i32> @identity_mask_0(<4 x i32> %x) {
130; CHECK-LABEL: @identity_mask_0(
131; CHECK-NEXT:    ret <4 x i32> [[X:%.*]]
132;
133  %shuf = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
134  ret <4 x i32> %shuf
135}
136
137define <4 x i32> @identity_mask_1(<4 x i32> %x) {
138; CHECK-LABEL: @identity_mask_1(
139; CHECK-NEXT:    ret <4 x i32> [[X:%.*]]
140;
141  %shuf = shufflevector <4 x i32> undef, <4 x i32> %x, <4 x i32> <i32 4, i32 5, i32 6, i32 7>
142  ret <4 x i32> %shuf
143}
144
145define <4 x i32> @pseudo_identity_mask(<4 x i32> %x) {
146; CHECK-LABEL: @pseudo_identity_mask(
147; CHECK-NEXT:    ret <4 x i32> [[X:%.*]]
148;
149  %shuf = shufflevector <4 x i32> %x, <4 x i32> %x, <4 x i32> <i32 0, i32 1, i32 2, i32 7>
150  ret <4 x i32> %shuf
151}
152
153define <4 x i32> @not_identity_mask(<4 x i32> %x) {
154; CHECK-LABEL: @not_identity_mask(
155; CHECK-NEXT:    [[SHUF:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 6>
156; CHECK-NEXT:    ret <4 x i32> [[SHUF]]
157;
158  %shuf = shufflevector <4 x i32> %x, <4 x i32> %x, <4 x i32> <i32 0, i32 1, i32 2, i32 6>
159  ret <4 x i32> %shuf
160}
161
162; TODO: Should we simplify if the mask has an undef element?
163
164define <4 x i32> @possible_identity_mask(<4 x i32> %x) {
165; CHECK-LABEL: @possible_identity_mask(
166; CHECK-NEXT:    [[SHUF:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
167; CHECK-NEXT:    ret <4 x i32> [[SHUF]]
168;
169  %shuf = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
170  ret <4 x i32> %shuf
171}
172
173define <4 x i32> @const_operand(<4 x i32> %x) {
174; CHECK-LABEL: @const_operand(
175; CHECK-NEXT:    ret <4 x i32> <i32 42, i32 45, i32 44, i32 43>
176;
177  %shuf = shufflevector <4 x i32> <i32 42, i32 43, i32 44, i32 45>, <4 x i32> %x, <4 x i32> <i32 0, i32 3, i32 2, i32 1>
178  ret <4 x i32> %shuf
179}
180
181define <4 x i32> @merge(<4 x i32> %x) {
182; CHECK-LABEL: @merge(
183; CHECK-NEXT:    ret <4 x i32> [[X:%.*]]
184;
185  %lower = shufflevector <4 x i32> %x, <4 x i32> undef, <2 x i32> <i32 1, i32 0>
186  %upper = shufflevector <4 x i32> %x, <4 x i32> undef, <2 x i32> <i32 2, i32 3>
187  %merged = shufflevector <2 x i32> %upper, <2 x i32> %lower, <4 x i32> <i32 3, i32 2, i32 0, i32 1>
188  ret <4 x i32> %merged
189}
190
191; This crosses lanes from the source op.
192
193define <4 x i32> @not_merge(<4 x i32> %x) {
194; CHECK-LABEL: @not_merge(
195; CHECK-NEXT:    [[L:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <2 x i32> <i32 0, i32 1>
196; CHECK-NEXT:    [[U:%.*]] = shufflevector <4 x i32> [[X]], <4 x i32> undef, <2 x i32> <i32 2, i32 3>
197; CHECK-NEXT:    [[MERGED:%.*]] = shufflevector <2 x i32> [[U]], <2 x i32> [[L]], <4 x i32> <i32 3, i32 2, i32 0, i32 1>
198; CHECK-NEXT:    ret <4 x i32> [[MERGED]]
199;
200  %l = shufflevector <4 x i32> %x, <4 x i32> undef, <2 x i32> <i32 0, i32 1>
201  %u = shufflevector <4 x i32> %x, <4 x i32> undef, <2 x i32> <i32 2, i32 3>
202  %merged = shufflevector <2 x i32> %u, <2 x i32> %l, <4 x i32> <i32 3, i32 2, i32 0, i32 1>
203  ret <4 x i32> %merged
204}
205
206define <8 x double> @extract_and_concat(<8 x double> %x) {
207; CHECK-LABEL: @extract_and_concat(
208; CHECK-NEXT:    ret <8 x double> [[X:%.*]]
209;
210  %s1 = shufflevector <8 x double> %x, <8 x double> undef, <2 x i32> <i32 0, i32 1>
211  %s2 = shufflevector <8 x double> %x, <8 x double> undef, <2 x i32> <i32 2, i32 3>
212  %s3 = shufflevector <8 x double> %x, <8 x double> undef, <2 x i32> <i32 4, i32 5>
213  %s4 = shufflevector <8 x double> %x, <8 x double> undef, <2 x i32> <i32 6, i32 7>
214  %s5 = shufflevector <2 x double> %s1, <2 x double> %s2, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
215  %s6 = shufflevector <2 x double> %s3, <2 x double> %s4, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
216  %s7 = shufflevector <4 x double> %s5, <4 x double> %s6, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
217  ret <8 x double> %s7
218}
219
220; This case has intermediate lane crossings.
221
222define <8 x i64> @PR30630(<8 x i64> %x) {
223; CHECK-LABEL: @PR30630(
224; CHECK-NEXT:    ret <8 x i64> [[X:%.*]]
225;
226  %s1 = shufflevector <8 x i64> %x, <8 x i64> undef, <2 x i32> <i32 0, i32 4>
227  %s2 = shufflevector <8 x i64> %x, <8 x i64> undef, <2 x i32> <i32 1, i32 5>
228  %s3 = shufflevector <8 x i64> %x, <8 x i64> undef, <2 x i32> <i32 2, i32 6>
229  %s4 = shufflevector <8 x i64> %x, <8 x i64> undef, <2 x i32> <i32 3, i32 7>
230  %s5 = shufflevector <2 x i64> %s1, <2 x i64> %s2, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
231  %s6 = shufflevector <2 x i64> %s3, <2 x i64> %s4, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
232  %s7 = shufflevector <4 x i64> %s5, <4 x i64> %s6, <8 x i32> <i32 0, i32 2, i32 4, i32 6, i32 1, i32 3, i32 5, i32 7>
233  ret <8 x i64> %s7
234}
235
236; This case covers internal canonicalization of shuffles with one constant input vector.
237
238;FIXME: Another issue exposed here, this whole function could be simplified to:
239;         ret <2 x float> zeroinitializer
240define <2 x float> @PR32872(<2 x float> %x) {
241; CHECK-LABEL: @PR32872(
242; CHECK-NEXT:    [[TMP1:%.*]] = shufflevector <2 x float> [[X:%.*]], <2 x float> zeroinitializer, <4 x i32> <i32 2, i32 2, i32 0, i32 1>
243; CHECK-NEXT:    [[TMP4:%.*]] = shufflevector <4 x float> zeroinitializer, <4 x float> [[TMP1]], <2 x i32> <i32 4, i32 5>
244; CHECK-NEXT:    ret <2 x float> [[TMP4]]
245;
246  %tmp1 = shufflevector <2 x float> %x, <2 x float> zeroinitializer, <4 x i32> <i32 2, i32 2, i32 0, i32 1>
247  %tmp4 = shufflevector <4 x float> zeroinitializer, <4 x float> %tmp1, <2 x i32> <i32 4, i32 5>
248  ret <2 x float> %tmp4
249}
250