1 ; RUN: opt -S -instcombine -o - %s | FileCheck %s
2 target 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"
4 define internal <2 x i32> @func_v2i32(<2 x i32> %v) noinline nounwind {
9 define internal <2 x float> @func_v2f32(<2 x float> %v) noinline nounwind {
14 define internal <4 x float> @func_v4f32(<4 x float> %v) noinline nounwind {
19 define internal i32 @func_i32(i32 %v) noinline nounwind {
24 define internal i64 @func_i64(i64 %v) noinline nounwind {
29 define internal <2 x i64> @func_v2i64(<2 x i64> %v) noinline nounwind {
34 define internal <2 x i32*> @func_v2i32p(<2 x i32*> %v) noinline nounwind {
39 ; Valid cases, only bitcast for argument / return type and call underlying function
41 ; Test cast between scalars with same bit sizes
42 ; Sizes match, should only bitcast
43 define void @bitcast_scalar(float* noalias %source, float* noalias %dest) nounwind {
45 ; CHECK-LABEL: @bitcast_scalar
46 ; CHECK: bitcast float* %source to i32*
47 ; CHECK: load i32, i32*
50 ; CHECK: bitcast float* %dest to 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
58 ; Test cast between vectors with same number of elements and bit sizes
59 ; Sizes match, should only bitcast
60 define void @bitcast_vector(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
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>*
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
75 ; Test cast from vector to scalar with same number of bits
76 ; Sizes match, should only bitcast
77 define void @bitcast_vector_scalar_same_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
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*
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
91 ; Test cast from scalar to vector with same number of bits
92 define void @bitcast_scalar_vector_same_size(i64* noalias %source, i64* noalias %dest) nounwind {
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
106 ; Test cast between vectors of pointers
107 define void @bitcast_vector_ptrs_same_size(<2 x i64*>* noalias %source, <2 x i64*>* noalias %dest) nounwind {
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
123 ; Test cast between scalars with different bit sizes
124 define void @bitcast_mismatch_scalar_size(float* noalias %source, float* noalias %dest) nounwind {
126 ; CHECK-LABEL: @bitcast_mismatch_scalar_size
128 ; CHECK: call float bitcast
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
136 ; Test cast between vectors with different bit sizes but the
137 ; same number of elements
138 define void @bitcast_mismatch_vector_element_and_bit_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
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
150 ; Test cast between vectors with same number of bits and different
151 ; numbers of elements
152 define void @bitcast_vector_mismatched_number_elements(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind {
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
162 ; Test cast between vector and scalar with different number of bits
163 define void @bitcast_vector_scalar_mismatched_bit_size(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind {
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
173 ; Test cast between vector of pointers and scalar with different number of bits
174 define void @bitcast_vector_ptrs_scalar_mismatched_bit_size(<4 x i32*>* noalias %source, <4 x i32*>* noalias %dest) nounwind {
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
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
186 define void @bitcast_scalar_vector_ptrs_same_size(i64* noalias %source, i64* noalias %dest) nounwind {
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
196 ; Test cast between scalar and vector with different number of bits
197 define void @bitcast_scalar_vector_mismatched_bit_size(i64* noalias %source, i64* noalias %dest) nounwind {
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