1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
4 define <4 x i32> @const_folding(<4 x i32> %x) {
5 ; CHECK-LABEL: @const_folding(
6 ; CHECK-NEXT: ret <4 x i32> zeroinitializer
8 %shuf = shufflevector <4 x i32> %x, <4 x i32> zeroinitializer, <4 x i32> <i32 5, i32 4, i32 5, i32 4>
12 define <4 x i32> @const_folding1(<4 x i32> %x) {
13 ; CHECK-LABEL: @const_folding1(
14 ; CHECK-NEXT: ret <4 x i32> splat (i32 5)
16 %shuf = shufflevector <4 x i32> <i32 5, i32 4, i32 5, i32 4>, <4 x i32> %x, <4 x i32> zeroinitializer
20 define <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]]
25 %shuf = shufflevector <3 x i32> %x, <3 x i32> zeroinitializer, <4 x i32> <i32 2, i32 4, i32 5, i32 4>
29 define <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]]
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>
39 define <4 x i32> @splat_operand_poison(<4 x i32> %x) {
40 ; CHECK-LABEL: @splat_operand_poison(
41 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
42 ; CHECK-NEXT: ret <4 x i32> [[SPLAT]]
44 %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
45 %shuf = shufflevector <4 x i32> %splat, <4 x i32> poison, <4 x i32> <i32 0, i32 3, i32 2, i32 1>
49 define <4 x i32> @splat_operand1(<4 x i32> %x, <4 x i32> %y) {
50 ; CHECK-LABEL: @splat_operand1(
51 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> [[Y:%.*]], <4 x i32> zeroinitializer
52 ; CHECK-NEXT: ret <4 x i32> [[SPLAT]]
54 %splat = shufflevector <4 x i32> %x, <4 x i32> %y, <4 x i32> zeroinitializer
55 %shuf = shufflevector <4 x i32> %splat, <4 x i32> undef, <4 x i32> <i32 0, i32 3, i32 2, i32 1>
59 define <4 x i32> @splat_operand2(<4 x i32> %x, <4 x i32> %y) {
60 ; CHECK-LABEL: @splat_operand2(
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]]
64 %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
65 %shuf = shufflevector <4 x i32> %splat, <4 x i32> %y, <4 x i32> <i32 0, i32 3, i32 2, i32 1>
69 define <4 x i32> @splat_operand3(<4 x i32> %x) {
70 ; CHECK-LABEL: @splat_operand3(
71 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
72 ; CHECK-NEXT: ret <4 x i32> [[SPLAT]]
74 %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
75 %shuf = shufflevector <4 x i32> zeroinitializer, <4 x i32> %splat, <4 x i32> <i32 7, i32 6, i32 5, i32 5>
79 define <8 x i32> @splat_operand_negative(<4 x i32> %x) {
80 ; CHECK-LABEL: @splat_operand_negative(
81 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
82 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[SPLAT]], <4 x i32> undef, <8 x i32> <i32 0, i32 3, i32 2, i32 1, i32 poison, i32 poison, i32 poison, i32 poison>
83 ; CHECK-NEXT: ret <8 x i32> [[SHUF]]
85 %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
86 %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>
90 define <4 x i32> @splat_operand_negative2(<4 x i32> %x, <4 x i32> %y) {
91 ; CHECK-LABEL: @splat_operand_negative2(
92 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
93 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[SPLAT]], <4 x i32> [[Y:%.*]], <4 x i32> <i32 0, i32 3, i32 4, i32 1>
94 ; CHECK-NEXT: ret <4 x i32> [[SHUF]]
96 %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
97 %shuf = shufflevector <4 x i32> %splat, <4 x i32> %y, <4 x i32> <i32 0, i32 3, i32 4, i32 1>
101 define <4 x i32> @splat_operand_negative3(<4 x i32> %x, <4 x i32> %y) {
102 ; CHECK-LABEL: @splat_operand_negative3(
103 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> zeroinitializer
104 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[Y:%.*]], <4 x i32> [[SPLAT]], <4 x i32> <i32 0, i32 3, i32 4, i32 1>
105 ; CHECK-NEXT: ret <4 x i32> [[SHUF]]
107 %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> zeroinitializer
108 %shuf = shufflevector <4 x i32> %y, <4 x i32> %splat, <4 x i32> <i32 0, i32 3, i32 4, i32 1>
112 define <4 x i32> @splat_operand_negative4(<4 x i32> %x) {
113 ; CHECK-LABEL: @splat_operand_negative4(
114 ; CHECK-NEXT: [[SPLAT:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> <i32 2, i32 poison, i32 2, i32 poison>
115 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[SPLAT]], <4 x i32> undef, <4 x i32> <i32 0, i32 2, i32 poison, i32 poison>
116 ; CHECK-NEXT: ret <4 x i32> [[SHUF]]
118 %splat = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 2, i32 undef, i32 2, i32 undef>
119 %shuf = shufflevector <4 x i32> %splat, <4 x i32> undef, <4 x i32> <i32 0, i32 2, i32 undef, i32 undef>
123 define <4 x i32> @undef_mask(<4 x i32> %x) {
124 ; CHECK-LABEL: @undef_mask(
125 ; CHECK-NEXT: ret <4 x i32> poison
127 %shuf = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> undef
131 define <4 x i32> @undef_mask_1(<4 x i32> %x, <4 x i32> %y) {
132 ; CHECK-LABEL: @undef_mask_1(
133 ; CHECK-NEXT: ret <4 x i32> poison
135 %shuf = shufflevector <4 x i32> %x, <4 x i32> %y, <4 x i32> undef
139 define <4 x i32> @identity_mask_0(<4 x i32> %x) {
140 ; CHECK-LABEL: @identity_mask_0(
141 ; CHECK-NEXT: ret <4 x i32> [[X:%.*]]
143 %shuf = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
147 define <4 x i32> @identity_mask_1(<4 x i32> %x) {
148 ; CHECK-LABEL: @identity_mask_1(
149 ; CHECK-NEXT: ret <4 x i32> [[X:%.*]]
151 %shuf = shufflevector <4 x i32> undef, <4 x i32> %x, <4 x i32> <i32 4, i32 5, i32 6, i32 7>
155 define <4 x i32> @pseudo_identity_mask(<4 x i32> %x) {
156 ; CHECK-LABEL: @pseudo_identity_mask(
157 ; CHECK-NEXT: ret <4 x i32> [[X:%.*]]
159 %shuf = shufflevector <4 x i32> %x, <4 x i32> %x, <4 x i32> <i32 0, i32 1, i32 2, i32 7>
163 define <4 x i32> @not_identity_mask(<4 x i32> %x) {
164 ; CHECK-LABEL: @not_identity_mask(
165 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> [[X]], <4 x i32> <i32 0, i32 1, i32 2, i32 6>
166 ; CHECK-NEXT: ret <4 x i32> [[SHUF]]
168 %shuf = shufflevector <4 x i32> %x, <4 x i32> %x, <4 x i32> <i32 0, i32 1, i32 2, i32 6>
172 ; TODO: Should we simplify if the mask has an undef element?
174 define <4 x i32> @possible_identity_mask(<4 x i32> %x) {
175 ; CHECK-LABEL: @possible_identity_mask(
176 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 poison>
177 ; CHECK-NEXT: ret <4 x i32> [[SHUF]]
179 %shuf = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 undef>
183 define <4 x i32> @const_operand(<4 x i32> %x) {
184 ; CHECK-LABEL: @const_operand(
185 ; CHECK-NEXT: ret <4 x i32> <i32 42, i32 45, i32 44, i32 43>
187 %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>
191 define <4 x i32> @merge(<4 x i32> %x) {
192 ; CHECK-LABEL: @merge(
193 ; CHECK-NEXT: ret <4 x i32> [[X:%.*]]
195 %lower = shufflevector <4 x i32> %x, <4 x i32> undef, <2 x i32> <i32 1, i32 0>
196 %upper = shufflevector <4 x i32> %x, <4 x i32> undef, <2 x i32> <i32 2, i32 3>
197 %merged = shufflevector <2 x i32> %upper, <2 x i32> %lower, <4 x i32> <i32 3, i32 2, i32 0, i32 1>
198 ret <4 x i32> %merged
201 ; This crosses lanes from the source op.
203 define <4 x i32> @not_merge(<4 x i32> %x) {
204 ; CHECK-LABEL: @not_merge(
205 ; CHECK-NEXT: [[L:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> undef, <2 x i32> <i32 0, i32 1>
206 ; CHECK-NEXT: [[U:%.*]] = shufflevector <4 x i32> [[X]], <4 x i32> undef, <2 x i32> <i32 2, i32 3>
207 ; CHECK-NEXT: [[MERGED:%.*]] = shufflevector <2 x i32> [[U]], <2 x i32> [[L]], <4 x i32> <i32 3, i32 2, i32 0, i32 1>
208 ; CHECK-NEXT: ret <4 x i32> [[MERGED]]
210 %l = shufflevector <4 x i32> %x, <4 x i32> undef, <2 x i32> <i32 0, i32 1>
211 %u = shufflevector <4 x i32> %x, <4 x i32> undef, <2 x i32> <i32 2, i32 3>
212 %merged = shufflevector <2 x i32> %u, <2 x i32> %l, <4 x i32> <i32 3, i32 2, i32 0, i32 1>
213 ret <4 x i32> %merged
216 define <8 x double> @extract_and_concat(<8 x double> %x) {
217 ; CHECK-LABEL: @extract_and_concat(
218 ; CHECK-NEXT: ret <8 x double> [[X:%.*]]
220 %s1 = shufflevector <8 x double> %x, <8 x double> undef, <2 x i32> <i32 0, i32 1>
221 %s2 = shufflevector <8 x double> %x, <8 x double> undef, <2 x i32> <i32 2, i32 3>
222 %s3 = shufflevector <8 x double> %x, <8 x double> undef, <2 x i32> <i32 4, i32 5>
223 %s4 = shufflevector <8 x double> %x, <8 x double> undef, <2 x i32> <i32 6, i32 7>
224 %s5 = shufflevector <2 x double> %s1, <2 x double> %s2, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
225 %s6 = shufflevector <2 x double> %s3, <2 x double> %s4, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
226 %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>
230 ; This case has intermediate lane crossings.
232 define <8 x i64> @PR30630(<8 x i64> %x) {
233 ; CHECK-LABEL: @PR30630(
234 ; CHECK-NEXT: ret <8 x i64> [[X:%.*]]
236 %s1 = shufflevector <8 x i64> %x, <8 x i64> undef, <2 x i32> <i32 0, i32 4>
237 %s2 = shufflevector <8 x i64> %x, <8 x i64> undef, <2 x i32> <i32 1, i32 5>
238 %s3 = shufflevector <8 x i64> %x, <8 x i64> undef, <2 x i32> <i32 2, i32 6>
239 %s4 = shufflevector <8 x i64> %x, <8 x i64> undef, <2 x i32> <i32 3, i32 7>
240 %s5 = shufflevector <2 x i64> %s1, <2 x i64> %s2, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
241 %s6 = shufflevector <2 x i64> %s3, <2 x i64> %s4, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
242 %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>
246 ; This case covers internal canonicalization of shuffles with one constant input vector.
248 ;FIXME: Another issue exposed here, this whole function could be simplified to:
249 ; ret <2 x float> zeroinitializer
250 define <2 x float> @PR32872(<2 x float> %x) {
251 ; CHECK-LABEL: @PR32872(
252 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <2 x float> [[X:%.*]], <2 x float> zeroinitializer, <4 x i32> <i32 2, i32 2, i32 0, i32 1>
253 ; CHECK-NEXT: [[SHUF2:%.*]] = shufflevector <4 x float> zeroinitializer, <4 x float> [[SHUF]], <2 x i32> <i32 4, i32 5>
254 ; CHECK-NEXT: ret <2 x float> [[SHUF2]]
256 %shuf = shufflevector <2 x float> %x, <2 x float> zeroinitializer, <4 x i32> <i32 2, i32 2, i32 0, i32 1>
257 %shuf2 = shufflevector <4 x float> zeroinitializer, <4 x float> %shuf, <2 x i32> <i32 4, i32 5>
258 ret <2 x float> %shuf2
261 define <5 x i8> @splat_inserted_constant(<4 x i8> %x) {
262 ; CHECK-LABEL: @splat_inserted_constant(
263 ; CHECK-NEXT: ret <5 x i8> splat (i8 42)
265 %ins3 = insertelement <4 x i8> %x, i8 42, i64 3
266 %splat5 = shufflevector <4 x i8> %ins3, <4 x i8> undef, <5 x i32> <i32 3, i32 3, i32 3, i32 3, i32 3>
270 define <4 x float> @splat_inserted_constant_undef_elt(<4 x float> %x) {
271 ; CHECK-LABEL: @splat_inserted_constant_undef_elt(
272 ; CHECK-NEXT: ret <4 x float> <float 1.200000e+01, float 1.200000e+01, float poison, float 1.200000e+01>
274 %ins1 = insertelement <4 x float> %x, float 12.0, i32 1
275 %splat1 = shufflevector <4 x float> %ins1, <4 x float> undef, <4 x i32> <i32 1, i32 1, i32 undef, i32 1>
276 ret <4 x float> %splat1
279 define <2 x i8> @splat_inserted_constant_not_canonical(<3 x i8> %x, <3 x i8> %y) {
280 ; CHECK-LABEL: @splat_inserted_constant_not_canonical(
281 ; CHECK-NEXT: ret <2 x i8> <i8 poison, i8 23>
283 %ins2 = insertelement <3 x i8> %x, i8 23, i7 2
284 %splat2 = shufflevector <3 x i8> %y, <3 x i8> %ins2, <2 x i32> <i32 undef, i32 5>
288 define <4 x i32> @fold_identity(<4 x i32> %x) {
289 ; CHECK-LABEL: @fold_identity(
290 ; CHECK-NEXT: ret <4 x i32> [[X:%.*]]
292 %shuf = shufflevector <4 x i32> %x, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
293 %revshuf = shufflevector <4 x i32> %shuf, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
294 ret <4 x i32> %revshuf
297 define <4 x i32> @fold_identity2(<4 x i32> %x) {
298 ; CHECK-LABEL: @fold_identity2(
299 ; CHECK-NEXT: [[SHL:%.*]] = shl <4 x i32> [[X:%.*]], splat (i32 1)
300 ; CHECK-NEXT: ret <4 x i32> [[SHL]]
302 %shl = shl <4 x i32> %x, <i32 1, i32 1, i32 1, i32 1>
303 %shuf = shufflevector <4 x i32> %shl, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
304 %revshuf = shufflevector <4 x i32> %shuf, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
305 ret <4 x i32> %revshuf
308 define <4 x i32> @fold_identity3(<4 x i32> %x) {
309 ; CHECK-LABEL: @fold_identity3(
310 ; CHECK-NEXT: [[SHL:%.*]] = shl <4 x i32> [[X:%.*]], [[X]]
311 ; CHECK-NEXT: ret <4 x i32> [[SHL]]
313 %shl = shl <4 x i32> %x, %x
314 %shuf = shufflevector <4 x i32> %shl, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
315 %revshuf = shufflevector <4 x i32> %shuf, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
316 ret <4 x i32> %revshuf
319 define <4 x i32> @not_fold_identity(<4 x i32> %x) {
320 ; CHECK-LABEL: @not_fold_identity(
321 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
322 ; CHECK-NEXT: [[REVSHUF:%.*]] = shufflevector <4 x i32> [[SHUF]], <4 x i32> poison, <4 x i32> <i32 1, i32 0, i32 3, i32 2>
323 ; CHECK-NEXT: ret <4 x i32> [[REVSHUF]]
325 %shuf = shufflevector <4 x i32> %x, <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 0, i32 1>
326 %revshuf = shufflevector <4 x i32> %shuf, <4 x i32> poison, <4 x i32> <i32 1, i32 0, i32 3, i32 2>
327 ret <4 x i32> %revshuf
330 define <4 x i32> @not_fold_identity2(<4 x i32> %x) {
331 ; CHECK-LABEL: @not_fold_identity2(
332 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 1, i32 0>
333 ; CHECK-NEXT: [[REVSHUF:%.*]] = shufflevector <4 x i32> [[SHUF]], <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
334 ; CHECK-NEXT: ret <4 x i32> [[REVSHUF]]
336 %shuf = shufflevector <4 x i32> %x, <4 x i32> poison, <4 x i32> <i32 2, i32 3, i32 1, i32 0>
337 %revshuf = shufflevector <4 x i32> %shuf, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
338 ret <4 x i32> %revshuf