1; RUN: opt -S -instcombine -o - %s | FileCheck %s
2target datalayout = "e-p:32:32:32-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v64:64:64-v128:128:128-a0:0:64"
3
4define internal <2 x i32> @func_v2i32(<2 x i32> %v) noinline nounwind {
5entry:
6  ret <2 x i32> %v
7}
8
9define internal <2 x float> @func_v2f32(<2 x float> %v) noinline nounwind {
10entry:
11  ret <2 x float> %v
12}
13
14define internal <4 x float> @func_v4f32(<4 x float> %v) noinline nounwind {
15entry:
16  ret <4 x float> %v
17}
18
19define internal i32 @func_i32(i32 %v) noinline nounwind {
20entry:
21  ret i32 %v
22}
23
24define internal i64 @func_i64(i64 %v) noinline nounwind {
25entry:
26  ret i64 %v
27}
28
29define internal <2 x i64> @func_v2i64(<2 x i64> %v) noinline nounwind {
30entry:
31  ret <2 x i64> %v
32}
33
34define internal <2 x i32*> @func_v2i32p(<2 x i32*> %v) noinline nounwind {
35entry:
36  ret <2 x i32*> %v
37}
38
39; Valid cases, only bitcast for argument / return type and call underlying function
40
41; Test cast between scalars with same bit sizes
42; Sizes match, should only bitcast
43define void @bitcast_scalar(float* noalias %source, float* noalias %dest) nounwind {
44entry:
45; CHECK-LABEL: @bitcast_scalar
46; CHECK: bitcast float* %source to i32*
47; CHECK: load i32, i32*
48; CHECK-NOT: fptoui
49; CHECK-NOT: uitofp
50; CHECK: bitcast float* %dest to i32*
51; CHECK: store i32
52  %tmp = load float, float* %source, align 8
53  %call = call float bitcast (i32 (i32)* @func_i32 to float (float)*)(float %tmp) nounwind
54  store float %call, float* %dest, align 8
55  ret void
56}
57
58; Test cast between vectors with same number of elements and bit sizes
59; Sizes match, should only bitcast
60define void @bitcast_vector(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
61entry:
62; CHECK-LABEL: @bitcast_vector
63; CHECK: bitcast <2 x float>* %source to <2 x i32>*
64; CHECK: load <2 x i32>, <2 x i32>*
65; CHECK-NOT: fptoui
66; CHECK-NOT: uitofp
67; CHECK: bitcast <2 x float>* %dest to <2 x i32>*
68; CHECK: store <2 x i32>
69  %tmp = load <2 x float>, <2 x float>* %source, align 8
70  %call = call <2 x float> bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <2 x float> (<2 x float>)*)(<2 x float> %tmp) nounwind
71  store <2 x float> %call, <2 x float>* %dest, align 8
72  ret void
73}
74
75; Test cast from vector to scalar with same number of bits
76; Sizes match, should only bitcast
77define void @bitcast_vector_scalar_same_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
78entry:
79; CHECK-LABEL: @bitcast_vector_scalar_same_size
80; CHECK: bitcast <2 x float>* %source to i64*
81; CHECK: load i64, i64*
82; CHECK: %call = call i64 @func_i64
83; CHECK: bitcast <2 x float>* %dest to i64*
84; CHECK: store i64
85  %tmp = load <2 x float>, <2 x float>* %source, align 8
86  %call = call <2 x float> bitcast (i64 (i64)* @func_i64 to <2 x float> (<2 x float>)*)(<2 x float> %tmp) nounwind
87  store <2 x float> %call, <2 x float>* %dest, align 8
88  ret void
89}
90
91; Test cast from scalar to vector with same number of bits
92define void @bitcast_scalar_vector_same_size(i64* noalias %source, i64* noalias %dest) nounwind {
93entry:
94; CHECK-LABEL: @bitcast_scalar_vector_same_size
95; CHECK: bitcast i64* %source to <2 x float>*
96; CHECK: load <2 x float>, <2 x float>*
97; CHECK: call <2 x float> @func_v2f32
98; CHECK: bitcast i64* %dest to <2 x float>*
99; CHECK: store <2 x float>
100  %tmp = load i64, i64* %source, align 8
101  %call = call i64 bitcast (<2 x float> (<2 x float>)* @func_v2f32 to i64 (i64)*)(i64 %tmp) nounwind
102  store i64 %call, i64* %dest, align 8
103  ret void
104}
105
106; Test cast between vectors of pointers
107define void @bitcast_vector_ptrs_same_size(<2 x i64*>* noalias %source, <2 x i64*>* noalias %dest) nounwind {
108entry:
109; CHECK-LABEL: @bitcast_vector_ptrs_same_size
110; CHECK: bitcast <2 x i64*>* %source to <2 x i32*>*
111; CHECK: load <2 x i32*>, <2 x i32*>*
112; CHECK: call <2 x i32*> @func_v2i32p
113; CHECK: bitcast <2 x i64*>* %dest to <2 x i32*>*
114; CHECK: store <2 x i32*>
115  %tmp = load <2 x i64*>, <2 x i64*>* %source, align 8
116  %call = call <2 x i64*> bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to <2 x i64*> (<2 x i64*>)*)(<2 x i64*> %tmp) nounwind
117  store <2 x i64*> %call, <2 x i64*>* %dest, align 8
118  ret void
119}
120
121; Invalid cases:
122
123; Test cast between scalars with different bit sizes
124define void @bitcast_mismatch_scalar_size(float* noalias %source, float* noalias %dest) nounwind {
125entry:
126; CHECK-LABEL: @bitcast_mismatch_scalar_size
127; CHECK-NOT: fptoui
128; CHECK: call float bitcast
129; CHECK-NOT: uitofp
130  %tmp = load float, float* %source, align 8
131  %call = call float bitcast (i64 (i64)* @func_i64 to float (float)*)(float %tmp) nounwind
132  store float %call, float* %dest, align 8
133  ret void
134}
135
136; Test cast between vectors with different bit sizes but the
137; same number of elements
138define void @bitcast_mismatch_vector_element_and_bit_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
139entry:
140; CHECK-LABEL: @bitcast_mismatch_vector_element_and_bit_size
141; CHECK-NOT: fptoui <2 x float> %tmp to <2 x i64>
142; CHECK: call <2 x float> bitcast
143; CHECK-NOT: uitofp <2 x i64> %call to <2 x float>
144  %tmp = load <2 x float>, <2 x float>* %source, align 8
145  %call = call <2 x float> bitcast (<2 x i64> (<2 x i64>)* @func_v2i64 to <2 x float> (<2 x float>)*)(<2 x float> %tmp) nounwind
146  store <2 x float> %call, <2 x float>* %dest, align 8
147  ret void
148}
149
150; Test cast between vectors with same number of bits and different
151; numbers of elements
152define void @bitcast_vector_mismatched_number_elements(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind {
153entry:
154; CHECK-LABEL: @bitcast_vector_mismatched_number_elements
155; CHECK:  %call = call <4 x float> bitcast
156  %tmp = load <4 x float>, <4 x float>* %source, align 8
157  %call = call <4 x float> bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <4 x float> (<4 x float>)*)(<4 x float> %tmp) nounwind
158  store <4 x float> %call, <4 x float>* %dest, align 8
159  ret void
160}
161
162; Test cast between vector and scalar with different number of bits
163define void @bitcast_vector_scalar_mismatched_bit_size(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind {
164entry:
165; CHECK-LABEL: @bitcast_vector_scalar_mismatched_bit_size
166; CHECK:  %call = call <4 x float> bitcast
167  %tmp = load <4 x float>, <4 x float>* %source, align 8
168  %call = call <4 x float> bitcast (i64 (i64)* @func_i64 to <4 x float> (<4 x float>)*)(<4 x float> %tmp) nounwind
169  store <4 x float> %call, <4 x float>* %dest, align 8
170  ret void
171}
172
173; Test cast between vector of pointers and scalar with different number of bits
174define void @bitcast_vector_ptrs_scalar_mismatched_bit_size(<4 x i32*>* noalias %source, <4 x i32*>* noalias %dest) nounwind {
175entry:
176; CHECK-LABEL: @bitcast_vector_ptrs_scalar_mismatched_bit_size
177; CHECK: call <4 x i32*> bitcast
178  %tmp = load <4 x i32*>, <4 x i32*>* %source, align 8
179  %call = call <4 x i32*> bitcast (i64 (i64)* @func_i64 to <4 x i32*> (<4 x i32*>)*)(<4 x i32*> %tmp) nounwind
180  store <4 x i32*> %call, <4 x i32*>* %dest, align 8
181  ret void
182}
183
184; Test cast from scalar to vector of pointers with same number of bits
185; We don't know the pointer size at this point, so this can't be done
186define void @bitcast_scalar_vector_ptrs_same_size(i64* noalias %source, i64* noalias %dest) nounwind {
187entry:
188; CHECK-LABEL: @bitcast_scalar_vector_ptrs_same_size
189; CHECK: call i64 bitcast
190  %tmp = load i64, i64* %source, align 8
191  %call = call i64 bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to i64 (i64)*)(i64 %tmp) nounwind
192  store i64 %call, i64* %dest, align 8
193  ret void
194}
195
196; Test cast between scalar and vector with different number of bits
197define void @bitcast_scalar_vector_mismatched_bit_size(i64* noalias %source, i64* noalias %dest) nounwind {
198entry:
199; CHECK-LABEL: @bitcast_scalar_vector_mismatched_bit_size
200; CHECK: call i64 bitcast
201  %tmp = load i64, i64* %source, align 8
202  %call = call i64 bitcast (<4 x float> (<4 x float>)* @func_v4f32 to i64 (i64)*)(i64 %tmp) nounwind
203  store i64 %call, i64* %dest, align 8
204  ret void
205}
206
207