1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
5 ; Instcombine should be able to eliminate all of these ext casts.
9 define i64 @test1(i64 %a) {
10 ; CHECK-LABEL: @test1(
11 ; CHECK-NEXT: [[B:%.*]] = trunc i64 [[A:%.*]] to i32
12 ; CHECK-NEXT: [[C:%.*]] = and i64 [[A]], 15
13 ; CHECK-NEXT: call void @use(i32 [[B]])
14 ; CHECK-NEXT: ret i64 [[C]]
16 %b = trunc i64 %a to i32
18 %d = zext i32 %c to i64
19 call void @use(i32 %b)
23 define i64 @test2(i64 %a) {
24 ; CHECK-LABEL: @test2(
25 ; CHECK-NEXT: [[B:%.*]] = trunc i64 [[A:%.*]] to i32
26 ; CHECK-NEXT: [[D1:%.*]] = shl i64 [[A]], 36
27 ; CHECK-NEXT: [[D:%.*]] = ashr exact i64 [[D1]], 36
28 ; CHECK-NEXT: call void @use(i32 [[B]])
29 ; CHECK-NEXT: ret i64 [[D]]
31 %b = trunc i64 %a to i32
34 %d = sext i32 %q to i64
35 call void @use(i32 %b)
39 define i64 @test3(i64 %a) {
40 ; CHECK-LABEL: @test3(
41 ; CHECK-NEXT: [[B:%.*]] = trunc i64 [[A:%.*]] to i32
42 ; CHECK-NEXT: [[C:%.*]] = and i64 [[A]], 8
43 ; CHECK-NEXT: call void @use(i32 [[B]])
44 ; CHECK-NEXT: ret i64 [[C]]
46 %b = trunc i64 %a to i32
48 %d = zext i32 %c to i64
49 call void @use(i32 %b)
53 define i64 @test4(i64 %a) {
54 ; CHECK-LABEL: @test4(
55 ; CHECK-NEXT: [[B:%.*]] = trunc i64 [[A:%.*]] to i32
56 ; CHECK-NEXT: [[C:%.*]] = and i64 [[A]], 8
57 ; CHECK-NEXT: [[X:%.*]] = xor i64 [[C]], 8
58 ; CHECK-NEXT: call void @use(i32 [[B]])
59 ; CHECK-NEXT: ret i64 [[X]]
61 %b = trunc i64 %a to i32
64 %d = zext i32 %x to i64
65 call void @use(i32 %b)
69 define i32 @test5(i32 %A) {
70 ; CHECK-LABEL: @test5(
71 ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[A:%.*]], 16
72 ; CHECK-NEXT: ret i32 [[TMP1]]
74 %B = zext i32 %A to i128
76 %D = trunc i128 %C to i32
80 define i32 @test6(i64 %A) {
81 ; CHECK-LABEL: @test6(
82 ; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[A:%.*]], 32
83 ; CHECK-NEXT: [[D:%.*]] = trunc i64 [[TMP1]] to i32
84 ; CHECK-NEXT: ret i32 [[D]]
86 %B = zext i64 %A to i128
88 %D = trunc i128 %C to i32
92 ; Test case where 'ashr' demanded bits does not contain any of the high bits,
93 ; but does contain sign bits, where the sign bit is not known to be zero.
94 define i16 @ashr_mul_sign_bits(i8 %X, i8 %Y) {
95 ; CHECK-LABEL: @ashr_mul_sign_bits(
96 ; CHECK-NEXT: [[A:%.*]] = sext i8 [[X:%.*]] to i16
97 ; CHECK-NEXT: [[B:%.*]] = sext i8 [[Y:%.*]] to i16
98 ; CHECK-NEXT: [[C:%.*]] = mul nsw i16 [[A]], [[B]]
99 ; CHECK-NEXT: [[D:%.*]] = ashr i16 [[C]], 3
100 ; CHECK-NEXT: ret i16 [[D]]
102 %A = sext i8 %X to i32
103 %B = sext i8 %Y to i32
106 %E = trunc i32 %D to i16
110 define i16 @ashr_mul(i8 %X, i8 %Y) {
111 ; CHECK-LABEL: @ashr_mul(
112 ; CHECK-NEXT: [[A:%.*]] = sext i8 [[X:%.*]] to i16
113 ; CHECK-NEXT: [[B:%.*]] = sext i8 [[Y:%.*]] to i16
114 ; CHECK-NEXT: [[C:%.*]] = mul nsw i16 [[A]], [[B]]
115 ; CHECK-NEXT: [[D:%.*]] = ashr i16 [[C]], 8
116 ; CHECK-NEXT: ret i16 [[D]]
118 %A = sext i8 %X to i20
119 %B = sext i8 %Y to i20
122 %E = trunc i20 %D to i16
126 define i32 @trunc_ashr(i32 %X) {
127 ; CHECK-LABEL: @trunc_ashr(
128 ; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 8
129 ; CHECK-NEXT: [[C:%.*]] = or i32 [[TMP1]], -8388608
130 ; CHECK-NEXT: ret i32 [[C]]
132 %A = zext i32 %X to i36
133 %B = or i36 %A, -2147483648 ; 0xF80000000
135 %T = trunc i36 %C to i32
139 define <2 x i32> @trunc_ashr_vec(<2 x i32> %X) {
140 ; CHECK-LABEL: @trunc_ashr_vec(
141 ; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 8, i32 8>
142 ; CHECK-NEXT: [[C:%.*]] = or <2 x i32> [[TMP1]], <i32 -8388608, i32 -8388608>
143 ; CHECK-NEXT: ret <2 x i32> [[C]]
145 %A = zext <2 x i32> %X to <2 x i36>
146 %B = or <2 x i36> %A, <i36 -2147483648, i36 -2147483648> ; 0xF80000000
147 %C = ashr <2 x i36> %B, <i36 8, i36 8>
148 %T = trunc <2 x i36> %C to <2 x i32>
152 define i92 @test7(i64 %A) {
153 ; CHECK-LABEL: @test7(
154 ; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[A:%.*]], 32
155 ; CHECK-NEXT: [[D:%.*]] = zext i64 [[TMP1]] to i92
156 ; CHECK-NEXT: ret i92 [[D]]
158 %B = zext i64 %A to i128
159 %C = lshr i128 %B, 32
160 %D = trunc i128 %C to i92
164 define i64 @test8(i32 %A, i32 %B) {
165 ; CHECK-LABEL: @test8(
166 ; CHECK-NEXT: [[TMP38:%.*]] = zext i32 [[A:%.*]] to i64
167 ; CHECK-NEXT: [[TMP32:%.*]] = zext i32 [[B:%.*]] to i64
168 ; CHECK-NEXT: [[TMP33:%.*]] = shl nuw i64 [[TMP32]], 32
169 ; CHECK-NEXT: [[INS35:%.*]] = or i64 [[TMP33]], [[TMP38]]
170 ; CHECK-NEXT: ret i64 [[INS35]]
172 %tmp38 = zext i32 %A to i128
173 %tmp32 = zext i32 %B to i128
174 %tmp33 = shl i128 %tmp32, 32
175 %ins35 = or i128 %tmp33, %tmp38
176 %tmp42 = trunc i128 %ins35 to i64
180 define i8 @test9(i32 %X) {
181 ; CHECK-LABEL: @test9(
182 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[X:%.*]] to i8
183 ; CHECK-NEXT: [[Z:%.*]] = and i8 [[TMP1]], 42
184 ; CHECK-NEXT: ret i8 [[Z]]
187 %Z = trunc i32 %Y to i8
192 define i8 @test10(i32 %X) {
193 ; CHECK-LABEL: @test10(
194 ; CHECK-NEXT: [[Y:%.*]] = trunc i32 [[X:%.*]] to i8
195 ; CHECK-NEXT: [[Z:%.*]] = and i8 [[Y]], 42
196 ; CHECK-NEXT: ret i8 [[Z]]
198 %Y = trunc i32 %X to i8
204 ; https://llvm.org/bugs/show_bug.cgi?id=25543
205 ; This is an extractelement.
207 define i32 @trunc_bitcast1(<4 x i32> %v) {
208 ; CHECK-LABEL: @trunc_bitcast1(
209 ; CHECK-NEXT: [[EXT:%.*]] = extractelement <4 x i32> [[V:%.*]], i32 1
210 ; CHECK-NEXT: ret i32 [[EXT]]
212 %bc = bitcast <4 x i32> %v to i128
213 %shr = lshr i128 %bc, 32
214 %ext = trunc i128 %shr to i32
218 ; A bitcast may still be required.
220 define i32 @trunc_bitcast2(<2 x i64> %v) {
221 ; CHECK-LABEL: @trunc_bitcast2(
222 ; CHECK-NEXT: [[BC1:%.*]] = bitcast <2 x i64> [[V:%.*]] to <4 x i32>
223 ; CHECK-NEXT: [[EXT:%.*]] = extractelement <4 x i32> [[BC1]], i32 2
224 ; CHECK-NEXT: ret i32 [[EXT]]
226 %bc = bitcast <2 x i64> %v to i128
227 %shr = lshr i128 %bc, 64
228 %ext = trunc i128 %shr to i32
232 ; The right shift is optional.
234 define i32 @trunc_bitcast3(<4 x i32> %v) {
235 ; CHECK-LABEL: @trunc_bitcast3(
236 ; CHECK-NEXT: [[EXT:%.*]] = extractelement <4 x i32> [[V:%.*]], i32 0
237 ; CHECK-NEXT: ret i32 [[EXT]]
239 %bc = bitcast <4 x i32> %v to i128
240 %ext = trunc i128 %bc to i32
244 define i32 @trunc_shl_31_i32_i64(i64 %val) {
245 ; CHECK-LABEL: @trunc_shl_31_i32_i64(
246 ; CHECK-NEXT: [[VAL_TR:%.*]] = trunc i64 [[VAL:%.*]] to i32
247 ; CHECK-NEXT: [[TRUNC:%.*]] = shl i32 [[VAL_TR]], 31
248 ; CHECK-NEXT: ret i32 [[TRUNC]]
250 %shl = shl i64 %val, 31
251 %trunc = trunc i64 %shl to i32
255 define i32 @trunc_shl_nsw_31_i32_i64(i64 %val) {
256 ; CHECK-LABEL: @trunc_shl_nsw_31_i32_i64(
257 ; CHECK-NEXT: [[VAL_TR:%.*]] = trunc i64 [[VAL:%.*]] to i32
258 ; CHECK-NEXT: [[TRUNC:%.*]] = shl i32 [[VAL_TR]], 31
259 ; CHECK-NEXT: ret i32 [[TRUNC]]
261 %shl = shl nsw i64 %val, 31
262 %trunc = trunc i64 %shl to i32
266 define i32 @trunc_shl_nuw_31_i32_i64(i64 %val) {
267 ; CHECK-LABEL: @trunc_shl_nuw_31_i32_i64(
268 ; CHECK-NEXT: [[VAL_TR:%.*]] = trunc i64 [[VAL:%.*]] to i32
269 ; CHECK-NEXT: [[TRUNC:%.*]] = shl i32 [[VAL_TR]], 31
270 ; CHECK-NEXT: ret i32 [[TRUNC]]
272 %shl = shl nuw i64 %val, 31
273 %trunc = trunc i64 %shl to i32
277 define i32 @trunc_shl_nsw_nuw_31_i32_i64(i64 %val) {
278 ; CHECK-LABEL: @trunc_shl_nsw_nuw_31_i32_i64(
279 ; CHECK-NEXT: [[VAL_TR:%.*]] = trunc i64 [[VAL:%.*]] to i32
280 ; CHECK-NEXT: [[TRUNC:%.*]] = shl i32 [[VAL_TR]], 31
281 ; CHECK-NEXT: ret i32 [[TRUNC]]
283 %shl = shl nsw nuw i64 %val, 31
284 %trunc = trunc i64 %shl to i32
288 define i16 @trunc_shl_15_i16_i64(i64 %val) {
289 ; CHECK-LABEL: @trunc_shl_15_i16_i64(
290 ; CHECK-NEXT: [[VAL_TR:%.*]] = trunc i64 [[VAL:%.*]] to i16
291 ; CHECK-NEXT: [[TRUNC:%.*]] = shl i16 [[VAL_TR]], 15
292 ; CHECK-NEXT: ret i16 [[TRUNC]]
294 %shl = shl i64 %val, 15
295 %trunc = trunc i64 %shl to i16
299 define i16 @trunc_shl_15_i16_i32(i32 %val) {
300 ; CHECK-LABEL: @trunc_shl_15_i16_i32(
301 ; CHECK-NEXT: [[VAL_TR:%.*]] = trunc i32 [[VAL:%.*]] to i16
302 ; CHECK-NEXT: [[TRUNC:%.*]] = shl i16 [[VAL_TR]], 15
303 ; CHECK-NEXT: ret i16 [[TRUNC]]
305 %shl = shl i32 %val, 15
306 %trunc = trunc i32 %shl to i16
310 define i8 @trunc_shl_7_i8_i64(i64 %val) {
311 ; CHECK-LABEL: @trunc_shl_7_i8_i64(
312 ; CHECK-NEXT: [[VAL_TR:%.*]] = trunc i64 [[VAL:%.*]] to i8
313 ; CHECK-NEXT: [[TRUNC:%.*]] = shl i8 [[VAL_TR]], 7
314 ; CHECK-NEXT: ret i8 [[TRUNC]]
316 %shl = shl i64 %val, 7
317 %trunc = trunc i64 %shl to i8
321 define i2 @trunc_shl_1_i2_i64(i64 %val) {
322 ; CHECK-LABEL: @trunc_shl_1_i2_i64(
323 ; CHECK-NEXT: [[SHL:%.*]] = shl i64 [[VAL:%.*]], 1
324 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i64 [[SHL]] to i2
325 ; CHECK-NEXT: ret i2 [[TRUNC]]
327 %shl = shl i64 %val, 1
328 %trunc = trunc i64 %shl to i2
332 define i32 @trunc_shl_1_i32_i64(i64 %val) {
333 ; CHECK-LABEL: @trunc_shl_1_i32_i64(
334 ; CHECK-NEXT: [[VAL_TR:%.*]] = trunc i64 [[VAL:%.*]] to i32
335 ; CHECK-NEXT: [[TRUNC:%.*]] = shl i32 [[VAL_TR]], 1
336 ; CHECK-NEXT: ret i32 [[TRUNC]]
338 %shl = shl i64 %val, 1
339 %trunc = trunc i64 %shl to i32
343 define i32 @trunc_shl_16_i32_i64(i64 %val) {
344 ; CHECK-LABEL: @trunc_shl_16_i32_i64(
345 ; CHECK-NEXT: [[VAL_TR:%.*]] = trunc i64 [[VAL:%.*]] to i32
346 ; CHECK-NEXT: [[TRUNC:%.*]] = shl i32 [[VAL_TR]], 16
347 ; CHECK-NEXT: ret i32 [[TRUNC]]
349 %shl = shl i64 %val, 16
350 %trunc = trunc i64 %shl to i32
354 define i32 @trunc_shl_33_i32_i64(i64 %val) {
355 ; CHECK-LABEL: @trunc_shl_33_i32_i64(
356 ; CHECK-NEXT: ret i32 0
358 %shl = shl i64 %val, 33
359 %trunc = trunc i64 %shl to i32
363 define i32 @trunc_shl_32_i32_i64(i64 %val) {
364 ; CHECK-LABEL: @trunc_shl_32_i32_i64(
365 ; CHECK-NEXT: ret i32 0
367 %shl = shl i64 %val, 32
368 %trunc = trunc i64 %shl to i32
372 ; TODO: Should be able to handle vectors
373 define <2 x i32> @trunc_shl_16_v2i32_v2i64(<2 x i64> %val) {
374 ; CHECK-LABEL: @trunc_shl_16_v2i32_v2i64(
375 ; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i64> [[VAL:%.*]], <i64 16, i64 16>
376 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc <2 x i64> [[SHL]] to <2 x i32>
377 ; CHECK-NEXT: ret <2 x i32> [[TRUNC]]
379 %shl = shl <2 x i64> %val, <i64 16, i64 16>
380 %trunc = trunc <2 x i64> %shl to <2 x i32>
384 define <2 x i32> @trunc_shl_nosplat_v2i32_v2i64(<2 x i64> %val) {
385 ; CHECK-LABEL: @trunc_shl_nosplat_v2i32_v2i64(
386 ; CHECK-NEXT: [[SHL:%.*]] = shl <2 x i64> [[VAL:%.*]], <i64 15, i64 16>
387 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc <2 x i64> [[SHL]] to <2 x i32>
388 ; CHECK-NEXT: ret <2 x i32> [[TRUNC]]
390 %shl = shl <2 x i64> %val, <i64 15, i64 16>
391 %trunc = trunc <2 x i64> %shl to <2 x i32>
395 define void @trunc_shl_31_i32_i64_multi_use(i64 %val, i32 addrspace(1)* %ptr0, i64 addrspace(1)* %ptr1) {
396 ; CHECK-LABEL: @trunc_shl_31_i32_i64_multi_use(
397 ; CHECK-NEXT: [[SHL:%.*]] = shl i64 [[VAL:%.*]], 31
398 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc i64 [[SHL]] to i32
399 ; CHECK-NEXT: store volatile i32 [[TRUNC]], i32 addrspace(1)* [[PTR0:%.*]], align 4
400 ; CHECK-NEXT: store volatile i64 [[SHL]], i64 addrspace(1)* [[PTR1:%.*]], align 8
401 ; CHECK-NEXT: ret void
403 %shl = shl i64 %val, 31
404 %trunc = trunc i64 %shl to i32
405 store volatile i32 %trunc, i32 addrspace(1)* %ptr0
406 store volatile i64 %shl, i64 addrspace(1)* %ptr1
410 define i32 @trunc_shl_lshr_infloop(i64 %arg) {
411 ; CHECK-LABEL: @trunc_shl_lshr_infloop(
412 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i64 [[ARG:%.*]], 1
413 ; CHECK-NEXT: [[TMP1:%.*]] = shl i64 [[TMP0]], 2
414 ; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32
415 ; CHECK-NEXT: ret i32 [[TMP2]]
417 %tmp0 = lshr i64 %arg, 1
418 %tmp1 = shl i64 %tmp0, 2
419 %tmp2 = trunc i64 %tmp1 to i32
423 define i32 @trunc_shl_ashr_infloop(i64 %arg) {
424 ; CHECK-LABEL: @trunc_shl_ashr_infloop(
425 ; CHECK-NEXT: [[TMP0:%.*]] = ashr i64 [[ARG:%.*]], 3
426 ; CHECK-NEXT: [[TMP1:%.*]] = shl nsw i64 [[TMP0]], 2
427 ; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32
428 ; CHECK-NEXT: ret i32 [[TMP2]]
430 %tmp0 = ashr i64 %arg, 3
431 %tmp1 = shl i64 %tmp0, 2
432 %tmp2 = trunc i64 %tmp1 to i32
436 define i32 @trunc_shl_shl_infloop(i64 %arg) {
437 ; CHECK-LABEL: @trunc_shl_shl_infloop(
438 ; CHECK-NEXT: [[ARG_TR:%.*]] = trunc i64 [[ARG:%.*]] to i32
439 ; CHECK-NEXT: [[TMP2:%.*]] = shl i32 [[ARG_TR]], 3
440 ; CHECK-NEXT: ret i32 [[TMP2]]
442 %tmp0 = shl i64 %arg, 1
443 %tmp1 = shl i64 %tmp0, 2
444 %tmp2 = trunc i64 %tmp1 to i32
448 define i32 @trunc_shl_lshr_var(i64 %arg, i64 %val) {
449 ; CHECK-LABEL: @trunc_shl_lshr_var(
450 ; CHECK-NEXT: [[TMP0:%.*]] = lshr i64 [[ARG:%.*]], [[VAL:%.*]]
451 ; CHECK-NEXT: [[TMP0_TR:%.*]] = trunc i64 [[TMP0]] to i32
452 ; CHECK-NEXT: [[TMP2:%.*]] = shl i32 [[TMP0_TR]], 2
453 ; CHECK-NEXT: ret i32 [[TMP2]]
455 %tmp0 = lshr i64 %arg, %val
456 %tmp1 = shl i64 %tmp0, 2
457 %tmp2 = trunc i64 %tmp1 to i32
461 define i32 @trunc_shl_ashr_var(i64 %arg, i64 %val) {
462 ; CHECK-LABEL: @trunc_shl_ashr_var(
463 ; CHECK-NEXT: [[TMP0:%.*]] = ashr i64 [[ARG:%.*]], [[VAL:%.*]]
464 ; CHECK-NEXT: [[TMP0_TR:%.*]] = trunc i64 [[TMP0]] to i32
465 ; CHECK-NEXT: [[TMP2:%.*]] = shl i32 [[TMP0_TR]], 2
466 ; CHECK-NEXT: ret i32 [[TMP2]]
468 %tmp0 = ashr i64 %arg, %val
469 %tmp1 = shl i64 %tmp0, 2
470 %tmp2 = trunc i64 %tmp1 to i32
474 define i32 @trunc_shl_shl_var(i64 %arg, i64 %val) {
475 ; CHECK-LABEL: @trunc_shl_shl_var(
476 ; CHECK-NEXT: [[TMP0:%.*]] = shl i64 [[ARG:%.*]], [[VAL:%.*]]
477 ; CHECK-NEXT: [[TMP0_TR:%.*]] = trunc i64 [[TMP0]] to i32
478 ; CHECK-NEXT: [[TMP2:%.*]] = shl i32 [[TMP0_TR]], 2
479 ; CHECK-NEXT: ret i32 [[TMP2]]
481 %tmp0 = shl i64 %arg, %val
482 %tmp1 = shl i64 %tmp0, 2
483 %tmp2 = trunc i64 %tmp1 to i32
487 define <8 x i16> @trunc_shl_v8i15_v8i32_15(<8 x i32> %a) {
488 ; CHECK-LABEL: @trunc_shl_v8i15_v8i32_15(
489 ; CHECK-NEXT: [[SHL:%.*]] = shl <8 x i32> [[A:%.*]], <i32 15, i32 15, i32 15, i32 15, i32 15, i32 15, i32 15, i32 15>
490 ; CHECK-NEXT: [[CONV:%.*]] = trunc <8 x i32> [[SHL]] to <8 x i16>
491 ; CHECK-NEXT: ret <8 x i16> [[CONV]]
493 %shl = shl <8 x i32> %a, <i32 15, i32 15, i32 15, i32 15, i32 15, i32 15, i32 15, i32 15>
494 %conv = trunc <8 x i32> %shl to <8 x i16>
498 define <8 x i16> @trunc_shl_v8i16_v8i32_16(<8 x i32> %a) {
499 ; CHECK-LABEL: @trunc_shl_v8i16_v8i32_16(
500 ; CHECK-NEXT: ret <8 x i16> zeroinitializer
502 %shl = shl <8 x i32> %a, <i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16, i32 16>
503 %conv = trunc <8 x i32> %shl to <8 x i16>
507 define <8 x i16> @trunc_shl_v8i16_v8i32_17(<8 x i32> %a) {
508 ; CHECK-LABEL: @trunc_shl_v8i16_v8i32_17(
509 ; CHECK-NEXT: ret <8 x i16> zeroinitializer
511 %shl = shl <8 x i32> %a, <i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17>
512 %conv = trunc <8 x i32> %shl to <8 x i16>
516 define <8 x i16> @trunc_shl_v8i16_v8i32_4(<8 x i32> %a) {
517 ; CHECK-LABEL: @trunc_shl_v8i16_v8i32_4(
518 ; CHECK-NEXT: [[SHL:%.*]] = shl <8 x i32> [[A:%.*]], <i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4>
519 ; CHECK-NEXT: [[CONV:%.*]] = trunc <8 x i32> [[SHL]] to <8 x i16>
520 ; CHECK-NEXT: ret <8 x i16> [[CONV]]
522 %shl = shl <8 x i32> %a, <i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4, i32 4>
523 %conv = trunc <8 x i32> %shl to <8 x i16>
527 ; Although the mask is the same value, we don't create a shuffle for types that the backend may not be able to handle:
528 ; trunc (shuffle X, C, Mask) --> shuffle (trunc X), C', Mask
530 define <4 x i8> @wide_shuf(<4 x i32> %x) {
531 ; CHECK-LABEL: @wide_shuf(
532 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[X:%.*]], <4 x i32> <i32 undef, i32 3634, i32 90, i32 undef>, <4 x i32> <i32 1, i32 5, i32 6, i32 2>
533 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc <4 x i32> [[SHUF]] to <4 x i8>
534 ; CHECK-NEXT: ret <4 x i8> [[TRUNC]]
536 %shuf = shufflevector <4 x i32> %x, <4 x i32> <i32 35, i32 3634, i32 90, i32 -1>, <4 x i32> <i32 1, i32 5, i32 6, i32 2>
537 %trunc = trunc <4 x i32> %shuf to <4 x i8>
541 ; trunc (shuffle X, undef, SplatMask) --> shuffle (trunc X), undef, SplatMask
543 define <4 x i8> @wide_splat1(<4 x i32> %x) {
544 ; CHECK-LABEL: @wide_splat1(
545 ; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i32> [[X:%.*]] to <4 x i8>
546 ; CHECK-NEXT: [[TRUNC:%.*]] = shufflevector <4 x i8> [[TMP1]], <4 x i8> undef, <4 x i32> <i32 2, i32 2, i32 2, i32 2>
547 ; CHECK-NEXT: ret <4 x i8> [[TRUNC]]
549 %shuf = shufflevector <4 x i32> %x, <4 x i32> undef, <4 x i32> <i32 2, i32 2, i32 2, i32 2>
550 %trunc = trunc <4 x i32> %shuf to <4 x i8>
555 ; trunc (shuffle X, undef, SplatMask) --> shuffle (trunc X), undef, SplatMask
557 define <3 x i31> @wide_splat2(<3 x i33> %x) {
558 ; CHECK-LABEL: @wide_splat2(
559 ; CHECK-NEXT: [[TMP1:%.*]] = trunc <3 x i33> [[X:%.*]] to <3 x i31>
560 ; CHECK-NEXT: [[TRUNC:%.*]] = shufflevector <3 x i31> [[TMP1]], <3 x i31> undef, <3 x i32> <i32 1, i32 1, i32 1>
561 ; CHECK-NEXT: ret <3 x i31> [[TRUNC]]
563 %shuf = shufflevector <3 x i33> %x, <3 x i33> undef, <3 x i32> <i32 1, i32 1, i32 1>
564 %trunc = trunc <3 x i33> %shuf to <3 x i31>
569 ; trunc (shuffle X, undef, SplatMask) --> shuffle (trunc X), undef, SplatMask
570 ; A mask with undef elements should still be considered a splat mask.
572 define <3 x i31> @wide_splat3(<3 x i33> %x) {
573 ; CHECK-LABEL: @wide_splat3(
574 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <3 x i33> [[X:%.*]], <3 x i33> undef, <3 x i32> <i32 undef, i32 1, i32 1>
575 ; CHECK-NEXT: [[TRUNC:%.*]] = trunc <3 x i33> [[SHUF]] to <3 x i31>
576 ; CHECK-NEXT: ret <3 x i31> [[TRUNC]]
578 %shuf = shufflevector <3 x i33> %x, <3 x i33> undef, <3 x i32> <i32 undef, i32 1, i32 1>
579 %trunc = trunc <3 x i33> %shuf to <3 x i31>
583 ; TODO: The shuffle extends the length of the input vector. Should we shrink this?
585 define <8 x i8> @wide_lengthening_splat(<4 x i16> %v) {
586 ; CHECK-LABEL: @wide_lengthening_splat(
587 ; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i16> [[V:%.*]], <4 x i16> undef, <8 x i32> zeroinitializer
588 ; CHECK-NEXT: [[TR:%.*]] = trunc <8 x i16> [[SHUF]] to <8 x i8>
589 ; CHECK-NEXT: ret <8 x i8> [[TR]]
591 %shuf = shufflevector <4 x i16> %v, <4 x i16> %v, <8 x i32> zeroinitializer
592 %tr = trunc <8 x i16> %shuf to <8 x i8>
596 define <2 x i8> @narrow_add_vec_constant(<2 x i32> %x) {
597 ; CHECK-LABEL: @narrow_add_vec_constant(
598 ; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i32> [[X:%.*]] to <2 x i8>
599 ; CHECK-NEXT: [[TR:%.*]] = add <2 x i8> [[TMP1]], <i8 0, i8 127>
600 ; CHECK-NEXT: ret <2 x i8> [[TR]]
602 %add = add <2 x i32> %x, <i32 256, i32 -129>
603 %tr = trunc <2 x i32> %add to <2 x i8>
607 define <2 x i8> @narrow_mul_vec_constant(<2 x i32> %x) {
608 ; CHECK-LABEL: @narrow_mul_vec_constant(
609 ; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i32> [[X:%.*]] to <2 x i8>
610 ; CHECK-NEXT: [[TR:%.*]] = mul <2 x i8> [[TMP1]], <i8 0, i8 127>
611 ; CHECK-NEXT: ret <2 x i8> [[TR]]
613 %add = mul <2 x i32> %x, <i32 256, i32 -129>
614 %tr = trunc <2 x i32> %add to <2 x i8>
618 define <2 x i8> @narrow_sub_vec_constant(<2 x i32> %x) {
619 ; CHECK-LABEL: @narrow_sub_vec_constant(
620 ; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i32> [[X:%.*]] to <2 x i8>
621 ; CHECK-NEXT: [[TR:%.*]] = sub <2 x i8> <i8 0, i8 127>, [[TMP1]]
622 ; CHECK-NEXT: ret <2 x i8> [[TR]]
624 %sub = sub <2 x i32> <i32 256, i32 -129>, %x
625 %tr = trunc <2 x i32> %sub to <2 x i8>