1; Ignore stderr, we expect warnings there
2; RUN: opt < %s -instcombine 2> /dev/null -S | FileCheck %s
3
4target datalayout = "E-p:64:64:64-p1:16:16:16-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128"
5
6; Simple case, argument translatable without changing the value
7declare void @test1a(i8*)
8
9define void @test1(i32* %A) {
10; CHECK-LABEL: @test1(
11; CHECK: %1 = bitcast i32* %A to i8*
12; CHECK: call void @test1a(i8* %1)
13; CHECK: ret void
14  call void bitcast (void (i8*)* @test1a to void (i32*)*)( i32* %A )
15  ret void
16}
17
18
19; Should not do because of change in address space of the parameter
20define void @test1_as1_illegal(i32 addrspace(1)* %A) {
21; CHECK-LABEL: @test1_as1_illegal(
22; CHECK: call void bitcast
23  call void bitcast (void (i8*)* @test1a to void (i32 addrspace(1)*)*)(i32 addrspace(1)* %A)
24  ret void
25}
26
27; Test1, but the argument has a different sized address-space
28declare void @test1a_as1(i8 addrspace(1)*)
29
30; This one is OK to perform
31define void @test1_as1(i32 addrspace(1)* %A) {
32; CHECK-LABEL: @test1_as1(
33; CHECK: %1 = bitcast i32 addrspace(1)* %A to i8 addrspace(1)*
34; CHECK: call void @test1a_as1(i8 addrspace(1)* %1)
35; CHECK: ret void
36  call void bitcast (void (i8 addrspace(1)*)* @test1a_as1 to void (i32 addrspace(1)*)*)(i32 addrspace(1)* %A )
37  ret void
38}
39
40; More complex case, translate argument because of resolution.  This is safe
41; because we have the body of the function
42define void @test2a(i8 %A) {
43; CHECK-LABEL: @test2a(
44; CHECK: ret void
45  ret void
46}
47
48define i32 @test2(i32 %A) {
49; CHECK-LABEL: @test2(
50; CHECK: call void bitcast
51; CHECK: ret i32 %A
52  call void bitcast (void (i8)* @test2a to void (i32)*)( i32 %A )
53  ret i32 %A
54}
55
56
57; Resolving this should insert a cast from sbyte to int, following the C
58; promotion rules.
59define void @test3a(i8, ...) {unreachable }
60
61define void @test3(i8 %A, i8 %B) {
62; CHECK-LABEL: @test3(
63; CHECK: %1 = zext i8 %B to i32
64; CHECK: call void (i8, ...) @test3a(i8 %A, i32 %1)
65; CHECK: ret void
66  call void bitcast (void (i8, ...)* @test3a to void (i8, i8)*)( i8 %A, i8 %B)
67  ret void
68}
69
70; test conversion of return value...
71define i8 @test4a() {
72; CHECK-LABEL: @test4a(
73; CHECK: ret i8 0
74  ret i8 0
75}
76
77define i32 @test4() {
78; CHECK-LABEL: @test4(
79; CHECK: call i32 bitcast
80  %X = call i32 bitcast (i8 ()* @test4a to i32 ()*)( )            ; <i32> [#uses=1]
81  ret i32 %X
82}
83
84; test conversion of return value... no value conversion occurs so we can do
85; this with just a prototype...
86declare i32 @test5a()
87
88define i32 @test5() {
89; CHECK-LABEL: @test5(
90; CHECK: %X = call i32 @test5a()
91; CHECK: ret i32 %X
92  %X = call i32 @test5a( )                ; <i32> [#uses=1]
93  ret i32 %X
94}
95
96; test addition of new arguments...
97declare i32 @test6a(i32)
98
99define i32 @test6() {
100; CHECK-LABEL: @test6(
101; CHECK: %X = call i32 @test6a(i32 0)
102; CHECK: ret i32 %X
103  %X = call i32 bitcast (i32 (i32)* @test6a to i32 ()*)( )
104  ret i32 %X
105}
106
107; test removal of arguments, only can happen with a function body
108define void @test7a() {
109; CHECK-LABEL: @test7a(
110; CHECK: ret void
111  ret void
112}
113
114define void @test7() {
115; CHECK-LABEL: @test7(
116; CHECK: call void @test7a()
117; CHECK: ret void
118  call void bitcast (void ()* @test7a to void (i32)*)( i32 5 )
119  ret void
120}
121
122
123; rdar://7590304
124declare void @test8a()
125
126define i8* @test8() {
127; CHECK-LABEL: @test8(
128; CHECK-NEXT: invoke void @test8a()
129; Don't turn this into "unreachable": the callee and caller don't agree in
130; calling conv, but the implementation of test8a may actually end up using the
131; right calling conv.
132  invoke void @test8a()
133          to label %invoke.cont unwind label %try.handler
134
135invoke.cont:                                      ; preds = %entry
136  unreachable
137
138try.handler:                                      ; preds = %entry
139  %exn = landingpad {i8*, i32} personality i32 (...)* @__gxx_personality_v0
140            cleanup
141  ret i8* null
142}
143
144declare i32 @__gxx_personality_v0(...)
145
146
147; Don't turn this into a direct call, because test9x is just a prototype and
148; doing so will make it varargs.
149; rdar://9038601
150declare i8* @test9x(i8*, i8*, ...) noredzone
151define i8* @test9(i8* %arg, i8* %tmp3) nounwind ssp noredzone {
152; CHECK-LABEL: @test9
153entry:
154  %call = call i8* bitcast (i8* (i8*, i8*, ...)* @test9x to i8* (i8*, i8*)*)(i8* %arg, i8* %tmp3) noredzone
155  ret i8* %call
156; CHECK-LABEL: @test9(
157; CHECK: call i8* bitcast
158}
159
160
161; Parameter that's a vector of pointers
162declare void @test10a(<2 x i8*>)
163
164define void @test10(<2 x i32*> %A) {
165; CHECK-LABEL: @test10(
166; CHECK: %1 = bitcast <2 x i32*> %A to <2 x i8*>
167; CHECK: call void @test10a(<2 x i8*> %1)
168; CHECK: ret void
169  call void bitcast (void (<2 x i8*>)* @test10a to void (<2 x i32*>)*)(<2 x i32*> %A)
170  ret void
171}
172
173; Don't transform because different address spaces
174declare void @test10a_mixed_as(<2 x i8 addrspace(1)*>)
175
176define void @test10_mixed_as(<2 x i8*> %A) {
177; CHECK-LABEL: @test10_mixed_as(
178; CHECK: call void bitcast
179  call void bitcast (void (<2 x i8 addrspace(1)*>)* @test10a_mixed_as to void (<2 x i8*>)*)(<2 x i8*> %A)
180  ret void
181}
182
183; Return type that's a pointer
184define i8* @test11a() {
185  ret i8* zeroinitializer
186}
187
188define i32* @test11() {
189; CHECK-LABEL: @test11(
190; CHECK: %X = call i8* @test11a()
191; CHECK: %1 = bitcast i8* %X to i32*
192  %X = call i32* bitcast (i8* ()* @test11a to i32* ()*)()
193  ret i32* %X
194}
195
196; Return type that's a pointer with a different address space
197define i8 addrspace(1)* @test11a_mixed_as() {
198  ret i8 addrspace(1)* zeroinitializer
199}
200
201define i8* @test11_mixed_as() {
202; CHECK-LABEL: @test11_mixed_as(
203; CHECK: call i8* bitcast
204  %X = call i8* bitcast (i8 addrspace(1)* ()* @test11a_mixed_as to i8* ()*)()
205  ret i8* %X
206}
207
208; Return type that's a vector of pointers
209define <2 x i8*> @test12a() {
210  ret <2 x i8*> zeroinitializer
211}
212
213define <2 x i32*> @test12() {
214; CHECK-LABEL: @test12(
215; CHECK: %X = call <2 x i8*> @test12a()
216; CHECK: %1 = bitcast <2 x i8*> %X to <2 x i32*>
217  %X = call <2 x i32*> bitcast (<2 x i8*> ()* @test12a to <2 x i32*> ()*)()
218  ret <2 x i32*> %X
219}
220
221define <2 x i8 addrspace(1)*> @test12a_mixed_as() {
222  ret <2 x i8 addrspace(1)*> zeroinitializer
223}
224
225define <2 x i8*> @test12_mixed_as() {
226; CHECK-LABEL: @test12_mixed_as(
227; CHECK: call <2 x i8*> bitcast
228  %X = call <2 x i8*> bitcast (<2 x i8 addrspace(1)*> ()* @test12a_mixed_as to <2 x i8*> ()*)()
229  ret <2 x i8*> %X
230}
231
232
233; Mix parameter that's a vector of integers and pointers of the same size
234declare void @test13a(<2 x i64>)
235
236define void @test13(<2 x i32*> %A) {
237; CHECK-LABEL: @test13(
238; CHECK: call void bitcast
239  call void bitcast (void (<2 x i64>)* @test13a to void (<2 x i32*>)*)(<2 x i32*> %A)
240  ret void
241}
242
243; Mix parameter that's a vector of integers and pointers of the same
244; size, but the other way around
245declare void @test14a(<2 x i8*>)
246
247define void @test14(<2 x i64> %A) {
248; CHECK-LABEL: @test14(
249; CHECK: call void bitcast
250  call void bitcast (void (<2 x i8*>)* @test14a to void (<2 x i64>)*)(<2 x i64> %A)
251  ret void
252}
253
254
255; Return type that's a vector
256define <2 x i16> @test15a() {
257  ret <2 x i16> zeroinitializer
258}
259
260define i32 @test15() {
261; CHECK-LABEL: @test15(
262; CHECK: %X = call <2 x i16> @test15a()
263; CHECK: %1 = bitcast <2 x i16> %X to i32
264  %X = call i32 bitcast (<2 x i16> ()* @test15a to i32 ()*)( )
265  ret i32 %X
266}
267
268define i32 @test16a() {
269  ret i32 0
270}
271
272define <2 x i16> @test16() {
273; CHECK-LABEL: @test16(
274; CHECK: %X = call i32 @test16a()
275; CHECK: %1 = bitcast i32 %X to <2 x i16>
276  %X = call <2 x i16> bitcast (i32 ()* @test16a to <2 x i16> ()*)( )
277  ret <2 x i16> %X
278}
279