[InstCombine] Signed saturation patterns
[llvm-core.git] / test / Transforms / InstCombine / bitcast-function.ll
blobca82165a49c9f08b5ba2a61770c825328f06369a
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 {
5 entry:
6   ret <2 x i32> %v
9 define internal <2 x float> @func_v2f32(<2 x float> %v) noinline nounwind {
10 entry:
11   ret <2 x float> %v
14 define internal <4 x float> @func_v4f32(<4 x float> %v) noinline nounwind {
15 entry:
16   ret <4 x float> %v
19 define internal i32 @func_i32(i32 %v) noinline nounwind {
20 entry:
21   ret i32 %v
24 define internal i64 @func_i64(i64 %v) noinline nounwind {
25 entry:
26   ret i64 %v
29 define internal <2 x i64> @func_v2i64(<2 x i64> %v) noinline nounwind {
30 entry:
31   ret <2 x i64> %v
34 define internal <2 x i32*> @func_v2i32p(<2 x i32*> %v) noinline nounwind {
35 entry:
36   ret <2 x i32*> %v
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 {
44 entry:
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
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 {
61 entry:
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
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 {
78 entry:
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
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 {
93 entry:
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
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 {
108 entry:
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
121 ; Invalid cases:
123 ; Test cast between scalars with different bit sizes
124 define void @bitcast_mismatch_scalar_size(float* noalias %source, float* noalias %dest) nounwind {
125 entry:
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
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 {
139 entry:
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
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 {
153 entry:
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
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 {
164 entry:
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
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 {
175 entry:
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
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 {
187 entry:
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
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 {
198 entry:
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