InstCombine: Don't assume that m_ZExt matches an Instruction
[oota-llvm.git] / test / Transforms / InstCombine / bitcast-alias-function.ll
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"
3
4
5
6 ; Cases that should be bitcast
7
8 ; Test cast between scalars with same bit sizes
9 @alias_i32_to_f32 = alias bitcast (i32 (i32)* @func_i32 to float (float)*)
10
11 ; Test cast between vectors with same number of elements and bit sizes
12 @alias_v2i32_to_v2f32 = alias bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <2 x float> (<2 x float>)*)
13
14 ; Test cast from vector to scalar with same number of bits
15 @alias_v2f32_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <2 x float> (<2 x float>)*)
16
17 ; Test cast from scalar to vector with same number of bits
18 @alias_i64_to_v2f32 = alias bitcast (<2 x float> (<2 x float>)* @func_v2f32 to i64 (i64)*)
19
20 ; Test cast between vectors of pointers
21 @alias_v2i32p_to_v2i64p = alias bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to <2 x i64*> (<2 x i64*>)*)
22
23
24 ; Cases that should be invalid and unchanged
25
26 ; Test cast between scalars with different bit sizes
27 @alias_i64_to_f32 = alias bitcast (i64 (i64)* @func_i64 to float (float)*)
28
29 ; Test cast between vectors with different bit sizes but the
30 ; same number of elements
31 @alias_v2i64_to_v2f32 = alias bitcast (<2 x i64> (<2 x i64>)* @func_v2i64 to <2 x float> (<2 x float>)*)
32
33 ; Test cast between vectors with same number of bits and different
34 ; numbers of elements
35 @alias_v2i32_to_v4f32 = alias bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <4 x float> (<4 x float>)*)
36
37 ; Test cast between scalar and vector with different number of bits
38 @alias_i64_to_v4f32 = alias bitcast (<4 x float> (<4 x float>)* @func_v4f32 to i64 (i64)*)
39
40 ; Test cast between vector and scalar with different number of bits
41 @alias_v4f32_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <4 x float> (<4 x float>)*)
42
43 ; Test cast from scalar to vector of pointers with same number of bits
44 ; We don't know the pointer size at this point, so this can't be done
45 @alias_i64_to_v2i32p = alias bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to i64 (i64)*)
46
47 ; Test cast between vector of pointers and scalar with different number of bits
48 @alias_v4i32p_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <4 x i32*> (<4 x i32*>)*)
49
50
51
52 define internal <2 x i32> @func_v2i32(<2 x i32> %v) noinline nounwind {
53 entry:
54   ret <2 x i32> %v
55 }
56
57 define internal <2 x float> @func_v2f32(<2 x float> %v) noinline nounwind {
58 entry:
59   ret <2 x float> %v
60 }
61
62 define internal <4 x float> @func_v4f32(<4 x float> %v) noinline nounwind {
63 entry:
64   ret <4 x float> %v
65 }
66
67 define internal i32 @func_i32(i32 %v) noinline nounwind {
68 entry:
69   ret i32 %v
70 }
71
72 define internal i64 @func_i64(i64 %v) noinline nounwind {
73 entry:
74   ret i64 %v
75 }
76
77 define internal <2 x i64> @func_v2i64(<2 x i64> %v) noinline nounwind {
78 entry:
79   ret <2 x i64> %v
80 }
81
82 define internal <2 x i32*> @func_v2i32p(<2 x i32*> %v) noinline nounwind {
83 entry:
84   ret <2 x i32*> %v
85 }
86
87 ; Valid cases, only bitcast for argument / return type and call underlying function
88
89 ; Sizes match, should only bitcast
90 define void @bitcast_alias_scalar(float* noalias %source, float* noalias %dest) nounwind {
91 entry:
92 ; CHECK-LABEL: @bitcast_alias_scalar
93 ; CHECK: bitcast float* %source to i32*
94 ; CHECK: load i32*
95 ; CHECK-NOT: fptoui
96 ; CHECK-NOT: uitofp
97 ; CHECK: bitcast i32 %call to float
98   %tmp = load float* %source, align 8
99   %call = call float @alias_i32_to_f32(float %tmp) nounwind
100   store float %call, float* %dest, align 8
101   ret void
102 }
103
104 ; Sizes match, should only bitcast
105 define void @bitcast_alias_vector(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
106 entry:
107 ; CHECK-LABEL: @bitcast_alias_vector
108 ; CHECK: bitcast <2 x float>* %source to <2 x i32>*
109 ; CHECK: load <2 x i32>*
110 ; CHECK-NOT: fptoui
111 ; CHECK-NOT: uitofp
112 ; CHECK: bitcast <2 x i32> %call to <2 x float>
113   %tmp = load <2 x float>* %source, align 8
114   %call = call <2 x float> @alias_v2i32_to_v2f32(<2 x float> %tmp) nounwind
115   store <2 x float> %call, <2 x float>* %dest, align 8
116   ret void
117 }
118
119 ; Sizes match, should only bitcast
120 define void @bitcast_alias_vector_scalar_same_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
121 entry:
122 ; CHECK-LABEL: @bitcast_alias_vector_scalar_same_size
123 ; CHECK: bitcast <2 x float>* %source to i64*
124 ; CHECK: load i64*
125 ; CHECK: %call = call i64 @func_i64
126 ; CHECK: bitcast i64 %call to <2 x float>
127   %tmp = load <2 x float>* %source, align 8
128   %call = call <2 x float> @alias_v2f32_to_i64(<2 x float> %tmp) nounwind
129   store <2 x float> %call, <2 x float>* %dest, align 8
130   ret void
131 }
132
133 define void @bitcast_alias_scalar_vector_same_size(i64* noalias %source, i64* noalias %dest) nounwind {
134 entry:
135 ; CHECK-LABEL: @bitcast_alias_scalar_vector_same_size
136 ; CHECK: bitcast i64* %source to <2 x float>*
137 ; CHECK: load <2 x float>*
138 ; CHECK: call <2 x float> @func_v2f32
139 ; CHECK: bitcast <2 x float> %call to i64
140   %tmp = load i64* %source, align 8
141   %call = call i64 @alias_i64_to_v2f32(i64 %tmp) nounwind
142   store i64 %call, i64* %dest, align 8
143   ret void
144 }
145
146 define void @bitcast_alias_vector_ptrs_same_size(<2 x i64*>* noalias %source, <2 x i64*>* noalias %dest) nounwind {
147 entry:
148 ; CHECK-LABEL: @bitcast_alias_vector_ptrs_same_size
149 ; CHECK: bitcast <2 x i64*>* %source to <2 x i32*>*
150 ; CHECK: load <2 x i32*>*
151 ; CHECK: call <2 x i32*> @func_v2i32p
152 ; CHECK: bitcast <2 x i32*> %call to <2 x i64*>
153   %tmp = load <2 x i64*>* %source, align 8
154   %call = call <2 x i64*> @alias_v2i32p_to_v2i64p(<2 x i64*> %tmp) nounwind
155   store <2 x i64*> %call, <2 x i64*>* %dest, align 8
156   ret void
157 }
158
159 ; Invalid cases:
160
161 define void @bitcast_alias_mismatch_scalar_size(float* noalias %source, float* noalias %dest) nounwind {
162 entry:
163 ; CHECK-LABEL: @bitcast_alias_mismatch_scalar_size
164 ; CHECK-NOT: fptoui
165 ; CHECK: @alias_i64_to_f32
166 ; CHECK-NOT: uitofp
167   %tmp = load float* %source, align 8
168   %call = call float @alias_i64_to_f32(float %tmp) nounwind
169   store float %call, float* %dest, align 8
170   ret void
171 }
172
173 define void @bitcast_alias_mismatch_vector_element_and_bit_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind {
174 entry:
175 ; CHECK-LABEL: @bitcast_alias_mismatch_vector_element_and_bit_size
176 ; CHECK-NOT: fptoui <2 x float> %tmp to <2 x i64>
177 ; CHECK: @alias_v2i64_to_v2f32
178 ; CHECK-NOT: uitofp <2 x i64> %call to <2 x float>
179   %tmp = load <2 x float>* %source, align 8
180   %call = call <2 x float> @alias_v2i64_to_v2f32(<2 x float> %tmp) nounwind
181   store <2 x float> %call, <2 x float>* %dest, align 8
182   ret void
183 }
184
185 define void @bitcast_alias_vector_mismatched_number_elements(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind {
186 entry:
187 ; CHECK-LABEL: @bitcast_alias_vector_mismatched_number_elements
188 ; CHECK:  %call = call <4 x float> @alias_v2i32_to_v4f32
189   %tmp = load <4 x float>* %source, align 8
190   %call = call <4 x float> @alias_v2i32_to_v4f32(<4 x float> %tmp) nounwind
191   store <4 x float> %call, <4 x float>* %dest, align 8
192   ret void
193 }
194
195 define void @bitcast_alias_vector_scalar_mismatched_bit_size(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind {
196 entry:
197 ; CHECK-LABEL: @bitcast_alias_vector_scalar_mismatched_bit_size
198 ; CHECK:  %call = call <4 x float> @alias_v4f32_to_i64
199   %tmp = load <4 x float>* %source, align 8
200   %call = call <4 x float> @alias_v4f32_to_i64(<4 x float> %tmp) nounwind
201   store <4 x float> %call, <4 x float>* %dest, align 8
202   ret void
203 }
204
205 define void @bitcast_alias_vector_ptrs_scalar_mismatched_bit_size(<4 x i32*>* noalias %source, <4 x i32*>* noalias %dest) nounwind {
206 entry:
207 ; CHECK-LABEL: @bitcast_alias_vector_ptrs_scalar_mismatched_bit_size
208 ; CHECK: @alias_v4i32p_to_i64
209   %tmp = load <4 x i32*>* %source, align 8
210   %call = call <4 x i32*> @alias_v4i32p_to_i64(<4 x i32*> %tmp) nounwind
211   store <4 x i32*> %call, <4 x i32*>* %dest, align 8
212   ret void
213 }
214
215 define void @bitcast_alias_scalar_vector_ptrs_same_size(i64* noalias %source, i64* noalias %dest) nounwind {
216 entry:
217 ; CHECK-LABEL: @bitcast_alias_scalar_vector_ptrs_same_size
218 ; CHECK: @alias_i64_to_v2i32p
219   %tmp = load i64* %source, align 8
220   %call = call i64 @alias_i64_to_v2i32p(i64 %tmp) nounwind
221   store i64 %call, i64* %dest, align 8
222   ret void
223 }
224
225 define void @bitcast_alias_scalar_vector_mismatched_bit_size(i64* noalias %source, i64* noalias %dest) nounwind {
226 entry:
227 ; CHECK-LABEL: @bitcast_alias_scalar_vector_mismatched_bit_size
228 ; CHECK: call i64 @alias_i64_to_v4f32
229   %tmp = load i64* %source, align 8
230   %call = call i64 @alias_i64_to_v4f32(i64 %tmp) nounwind
231   store i64 %call, i64* %dest, align 8
232   ret void
233 }
234