1 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 ; Ensure that type punning using a union of vector and same-sized array
4 ; generates an extract instead of a shuffle with an uncommon vector size:
6 ; typedef uint32_t v4i32 __attribute__((vector_size(16)));
7 ; union { v4i32 v; uint32_t a[4]; };
9 ; This cleans up behind SROA, which inserts the uncommon vector size when
10 ; cleaning up the alloca/store/GEP/load.
13 ; Extracting the zeroth element in an i32 array.
14 define i32 @type_pun_zeroth(<16 x i8> %in) {
15 ; CHECK-LABEL: @type_pun_zeroth(
16 ; CHECK-NEXT: %[[BC:.*]] = bitcast <16 x i8> %in to <4 x i32>
17 ; CHECK-NEXT: %[[EXT:.*]] = extractelement <4 x i32> %[[BC]], i32 0
18 ; CHECK-NEXT: ret i32 %[[EXT]]
19 %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
20 %1 = bitcast <4 x i8> %sroa to i32
24 ; Extracting the first element in an i32 array.
25 define i32 @type_pun_first(<16 x i8> %in) {
26 ; CHECK-LABEL: @type_pun_first(
27 ; CHECK-NEXT: %[[BC:.*]] = bitcast <16 x i8> %in to <4 x i32>
28 ; CHECK-NEXT: %[[EXT:.*]] = extractelement <4 x i32> %[[BC]], i32 1
29 ; CHECK-NEXT: ret i32 %[[EXT]]
30 %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, <4 x i32> <i32 4, i32 5, i32 6, i32 7>
31 %1 = bitcast <4 x i8> %sroa to i32
35 ; Extracting an i32 that isn't aligned to any natural boundary.
36 define i32 @type_pun_misaligned(<16 x i8> %in) {
37 ; CHECK-LABEL: @type_pun_misaligned(
38 ; CHECK-NEXT: %[[SHUF:.*]] = shufflevector <16 x i8> %in, <16 x i8> undef, <16 x i32> <i32 6, i32 7, i32 8, i32 9, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
39 ; CHECK-NEXT: %[[BC:.*]] = bitcast <16 x i8> %[[SHUF]] to <4 x i32>
40 ; CHECK-NEXT: %[[EXT:.*]] = extractelement <4 x i32> %[[BC]], i32 0
41 ; CHECK-NEXT: ret i32 %[[EXT]]
42 %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, <4 x i32> <i32 6, i32 7, i32 8, i32 9>
43 %1 = bitcast <4 x i8> %sroa to i32
47 ; Type punning to an array of pointers.
48 define i32* @type_pun_pointer(<16 x i8> %in) {
49 ; CHECK-LABEL: @type_pun_pointer(
50 ; CHECK-NEXT: %[[BC:.*]] = bitcast <16 x i8> %in to <4 x i32>
51 ; CHECK-NEXT: %[[EXT:.*]] = extractelement <4 x i32> %[[BC]], i32 0
52 ; CHECK-NEXT: %[[I2P:.*]] = inttoptr i32 %[[EXT]] to i32*
53 ; CHECK-NEXT: ret i32* %[[I2P]]
54 %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
55 %1 = bitcast <4 x i8> %sroa to i32
56 %2 = inttoptr i32 %1 to i32*
60 ; Type punning to an array of 32-bit floating-point values.
61 define float @type_pun_float(<16 x i8> %in) {
62 ; CHECK-LABEL: @type_pun_float(
63 ; CHECK-NEXT: %[[BC:.*]] = bitcast <16 x i8> %in to <4 x float>
64 ; CHECK-NEXT: %[[EXT:.*]] = extractelement <4 x float> %[[BC]], i32 0
65 ; CHECK-NEXT: ret float %[[EXT]]
66 %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
67 %1 = bitcast <4 x i8> %sroa to float
71 ; Type punning to an array of 64-bit floating-point values.
72 define double @type_pun_double(<16 x i8> %in) {
73 ; CHECK-LABEL: @type_pun_double(
74 ; CHECK-NEXT: %[[BC:.*]] = bitcast <16 x i8> %in to <2 x double>
75 ; CHECK-NEXT: %[[EXT:.*]] = extractelement <2 x double> %[[BC]], i32 0
76 ; CHECK-NEXT: ret double %[[EXT]]
77 %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
78 %1 = bitcast <8 x i8> %sroa to double
82 ; Type punning to same-size floating-point and integer values.
83 ; Verify that multiple uses with different bitcast types are properly handled.
84 define { float, i32 } @type_pun_float_i32(<16 x i8> %in) {
85 ; CHECK-LABEL: @type_pun_float_i32(
86 ; CHECK-NEXT: %[[BCI:.*]] = bitcast <16 x i8> %in to <4 x i32>
87 ; CHECK-NEXT: %[[EXTI:.*]] = extractelement <4 x i32> %[[BCI]], i32 0
88 ; CHECK-NEXT: %[[BCF:.*]] = bitcast <16 x i8> %in to <4 x float>
89 ; CHECK-NEXT: %[[EXTF:.*]] = extractelement <4 x float> %[[BCF]], i32 0
90 ; CHECK-NEXT: %1 = insertvalue { float, i32 } undef, float %[[EXTF]], 0
91 ; CHECK-NEXT: %2 = insertvalue { float, i32 } %1, i32 %[[EXTI]], 1
92 ; CHECK-NEXT: ret { float, i32 } %2
93 %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
94 %f = bitcast <4 x i8> %sroa to float
95 %i = bitcast <4 x i8> %sroa to i32
96 %1 = insertvalue { float, i32 } undef, float %f, 0
97 %2 = insertvalue { float, i32 } %1, i32 %i, 1
101 ; Type punning two i32 values, with control flow.
102 ; Verify that the bitcast is shared and dominates usage.
103 define i32 @type_pun_i32_ctrl(<16 x i8> %in) {
104 ; CHECK-LABEL: @type_pun_i32_ctrl(
105 entry: ; CHECK-NEXT: entry:
106 ; CHECK-NEXT: %[[BC:.*]] = bitcast <16 x i8> %in to <4 x i32>
108 %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
109 br i1 undef, label %left, label %right
111 ; CHECK-NEXT: %[[EXTL:.*]] = extractelement <4 x i32> %[[BC]], i32 0
113 %lhs = bitcast <4 x i8> %sroa to i32
115 right: ; CHECK: right:
116 ; CHECK-NEXT: %[[EXTR:.*]] = extractelement <4 x i32> %[[BC]], i32 0
118 %rhs = bitcast <4 x i8> %sroa to i32
121 ; CHECK-NEXT: %i = phi i32 [ %[[EXTL]], %left ], [ %[[EXTR]], %right ]
122 ; CHECK-NEXT: ret i32 %i
123 %i = phi i32 [ %lhs, %left ], [ %rhs, %right ]
127 ; Extracting a type that won't fit in a vector isn't handled. The function
128 ; should stay the same.
129 define i40 @type_pun_unhandled(<16 x i8> %in) {
130 ; CHECK-LABEL: @type_pun_unhandled(
131 ; CHECK-NEXT: %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, <5 x i32> <i32 4, i32 5, i32 6, i32 7, i32 8>
132 ; CHECK-NEXT: %1 = bitcast <5 x i8> %sroa to i40
133 ; CHECK-NEXT: ret i40 %1
134 %sroa = shufflevector <16 x i8> %in, <16 x i8> undef, <5 x i32> <i32 4, i32 5, i32 6, i32 7, i32 8>
135 %1 = bitcast <5 x i8> %sroa to i40