1; RUN: llc < %s -asm-verbose=false -wasm-disable-explicit-locals -wasm-keep-registers -enable-emscripten-cxx-exceptions | FileCheck %s
2
3; Test that function pointer casts are replaced with wrappers.
4
5target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6target triple = "wasm32-unknown-unknown"
7
8define void @has_i32_arg(i32) {
9entry:
10  ret void
11}
12
13declare void @has_struct_arg({i32})
14declare i32 @has_i32_ret()
15declare void @vararg(...)
16declare void @plain(i32)
17
18declare void @foo0()
19declare void @foo1()
20declare void @foo2()
21declare void @foo3()
22
23; CHECK-LABEL: test:
24; CHECK:      call        .Lhas_i32_arg_bitcast.2{{$}}
25; CHECK-NEXT: call        .Lhas_i32_arg_bitcast.2{{$}}
26; CHECK-NEXT: call        .Lhas_i32_ret_bitcast{{$}}
27; CHECK-NEXT: call        $drop=, has_i32_ret
28; CHECK-NEXT: i32.const   $push[[L0:[0-9]+]]=, 0
29; CHECK-NEXT: call        .Lfoo0_bitcast, $pop[[L0]]{{$}}
30; CHECK-NEXT: i32.const   $push[[L1:[0-9]+]]=, 0
31; CHECK-NEXT: call        .Lfoo0_bitcast, $pop[[L1]]{{$}}
32; CHECK-NEXT: i32.const   $push[[L2:[0-9]+]]=, 0
33; CHECK-NEXT: call        .Lfoo0_bitcast, $pop[[L2]]{{$}}
34; CHECK-NEXT: call        foo0
35; CHECK-NEXT: call        $drop=, .Lfoo1_bitcast{{$}}
36; CHECK-NEXT: call        foo2{{$}}
37; CHECK-NEXT: call        foo1{{$}}
38; CHECK-NEXT: call        foo3{{$}}
39; CHECK-NEXT: end_function
40define void @test() {
41entry:
42  call void bitcast (void (i32)* @has_i32_arg to void ()*)()
43  call void bitcast (void (i32)* @has_i32_arg to void ()*)()
44  call void bitcast (i32 ()* @has_i32_ret to void ()*)()
45  call i32 bitcast (i32 ()* @has_i32_ret to i32 ()*)()
46  call void bitcast (void ()* @foo0 to void (i32)*)(i32 0)
47  %p = bitcast void ()* @foo0 to void (i32)*
48  call void %p(i32 0)
49  %q = bitcast void ()* @foo0 to void (i32)*
50  call void %q(i32 0)
51  %r = bitcast void (i32)* %q to void ()*
52  call void %r()
53  %t = call i32 bitcast (void ()* @foo1 to i32 ()*)()
54  call void bitcast (void ()* @foo2 to void ()*)()
55  call void @foo1()
56  call void @foo3()
57
58  ret void
59}
60
61; Calling aliases should also generate a wrapper
62
63@alias_i32_arg = weak hidden alias void (i32), void (i32)* @has_i32_arg
64
65; CHECK-LABEL: test_alias:
66; CHECK: call    .Lhas_i32_arg_bitcast.2
67define void @test_alias() {
68entry:
69  call void bitcast (void (i32)* @alias_i32_arg to void ()*)()
70  ret void
71}
72
73
74; CHECK-LABEL: test_structs:
75; CHECK: call     .Lhas_i32_arg_bitcast.1, $pop{{[0-9]+}}, $pop{{[0-9]+$}}
76; CHECK: call     .Lhas_i32_arg_bitcast, $0, $pop2
77; CHECK: call     .Lhas_struct_arg_bitcast{{$}}
78define void @test_structs() {
79entry:
80  call void bitcast (void (i32)* @has_i32_arg to void (i32, {i32})*)(i32 5, {i32} {i32 6})
81  call {i32, i64} bitcast (void (i32)* @has_i32_arg to {i32, i64} (i32)*)(i32 7)
82  call void bitcast (void ({i32})* @has_struct_arg to void ()*)()
83  ret void
84}
85
86; CHECK-LABEL: test_structs_unhandled:
87; CHECK: call    has_struct_arg, $pop{{[0-9]+$}}
88; CHECK: call    has_struct_arg, $pop{{[0-9]+$}}
89; CHECK: call    has_i32_ret, $pop{{[0-9]+$}}
90define void @test_structs_unhandled() {
91entry:
92  call void @has_struct_arg({i32} {i32 3})
93  call void bitcast (void ({i32})* @has_struct_arg to void ({i64})*)({i64} {i64 4})
94  call {i32, i32} bitcast (i32 ()* @has_i32_ret to {i32, i32} ()*)()
95  ret void
96}
97
98; CHECK-LABEL: test_varargs:
99; CHECK:      global.set
100; CHECK:      i32.const   $push[[L3:[0-9]+]]=, 0{{$}}
101; CHECK-NEXT: call        .Lvararg_bitcast, $pop[[L3]]{{$}}
102; CHECK-NEXT: i32.const   $push[[L4:[0-9]+]]=, 0{{$}}
103; CHECK-NEXT: i32.store   0($[[L5:[0-9]+]]), $pop[[L4]]{{$}}
104; CHECK-NEXT: call        .Lplain_bitcast, $[[L5]]{{$}}
105define void @test_varargs() {
106  call void bitcast (void (...)* @vararg to void (i32)*)(i32 0)
107  call void (...) bitcast (void (i32)* @plain to void (...)*)(i32 0)
108  ret void
109}
110
111; Don't use wrappers when the value is stored in memory
112
113@global_func = hidden local_unnamed_addr global void ()* null
114
115; CHECK-LABEL: test_store:
116; CHECK:      i32.const   $push[[L0:[0-9]+]]=, 0{{$}}
117; CHECK-NEXT: i32.const   $push[[L1:[0-9]+]]=, has_i32_ret{{$}}
118; CHECK-NEXT: i32.store   global_func($pop[[L0]]), $pop[[L1]]{{$}}
119define void @test_store() {
120  %1 = bitcast i32 ()* @has_i32_ret to void ()*
121  store void ()* %1, void ()** @global_func
122  ret void
123}
124
125; CHECK-LABEL: test_load:
126; CHECK-NEXT: .functype test_load () -> (i32){{$}}
127; CHECK-NEXT: i32.const   $push[[L0:[0-9]+]]=, 0{{$}}
128; CHECK-NEXT: i32.load    $push[[L1:[0-9]+]]=, global_func($pop[[L0]]){{$}}
129; CHECK-NEXT: call_indirect $push{{[0-9]+}}=, $pop[[L1]]{{$}}
130define i32 @test_load() {
131  %1 = load i32 ()*, i32 ()** bitcast (void ()** @global_func to i32 ()**)
132  %2 = call i32 %1()
133  ret i32 %2
134}
135
136; Don't use wrappers when the value is passed to a function call
137
138declare void @call_func(i32 ()*)
139
140; CHECK-LABEL: test_argument:
141; CHECK:      i32.const   $push[[L0:[0-9]+]]=, has_i32_ret{{$}}
142; CHECK-NEXT: call        call_func, $pop[[L0]]{{$}}
143; CHECK-NEXT: i32.const   $push[[L1:[0-9]+]]=, has_i32_arg{{$}}
144; CHECK-NEXT: call        call_func, $pop[[L1]]{{$}}
145define void @test_argument() {
146  call void @call_func(i32 ()* @has_i32_ret)
147  call void @call_func(i32 ()* bitcast (void (i32)* @has_i32_arg to i32 ()*))
148  ret void
149}
150
151; Invokes should be treated like calls
152
153; CHECK-LABEL: test_invoke:
154; CHECK:      i32.const   $push[[L1:[0-9]+]]=, call_func{{$}}
155; CHECK-NEXT: i32.const   $push[[L0:[0-9]+]]=, has_i32_ret{{$}}
156; CHECK-NEXT: call        "__invoke_void_i32()*", $pop[[L1]], $pop[[L0]]{{$}}
157; CHECK:      i32.const   $push[[L3:[0-9]+]]=, call_func{{$}}
158; CHECK-NEXT: i32.const   $push[[L2:[0-9]+]]=, has_i32_arg{{$}}
159; CHECK-NEXT: call        "__invoke_void_i32()*", $pop[[L3]], $pop[[L2]]{{$}}
160; CHECK:      i32.const   $push[[L4:[0-9]+]]=, .Lhas_i32_arg_bitcast.2{{$}}
161; CHECK-NEXT: call        __invoke_void, $pop[[L4]]{{$}}
162declare i32 @personality(...)
163define void @test_invoke() personality i32 (...)* @personality {
164entry:
165  invoke void @call_func(i32 ()* @has_i32_ret)
166          to label %cont unwind label %lpad
167
168cont:
169  invoke void @call_func(i32 ()* bitcast (void (i32)* @has_i32_arg to i32 ()*))
170          to label %cont2 unwind label %lpad
171
172cont2:
173  invoke void bitcast (void (i32)* @has_i32_arg to void ()*)()
174          to label %end unwind label %lpad
175
176lpad:
177  %0 = landingpad { i8*, i32 }
178          catch i8* null
179  br label %end
180
181end:
182  ret void
183}
184
185; CHECK-LABEL: .Lhas_i32_arg_bitcast:
186; CHECK-NEXT: .functype .Lhas_i32_arg_bitcast (i32, i32) -> ()
187; CHECK-NEXT: call        has_i32_arg, $1{{$}}
188; CHECK-NEXT: end_function
189
190; CHECK-LABEL: .Lhas_i32_arg_bitcast.1:
191; CHECK-NEXT: .functype .Lhas_i32_arg_bitcast.1 (i32, i32) -> ()
192; CHECK-NEXT: call        has_i32_arg, $0{{$}}
193; CHECK-NEXT: end_function
194
195; CHECK-LABEL: .Lhas_i32_arg_bitcast.2:
196; CHECK:      call        has_i32_arg, $0{{$}}
197; CHECK-NEXT: end_function
198
199; CHECK-LABEL: .Lhas_i32_ret_bitcast:
200; CHECK:      call        $drop=, has_i32_ret{{$}}
201; CHECK-NEXT: end_function
202
203; CHECK-LABEL: .Lvararg_bitcast:
204; CHECK: call        vararg, $1{{$}}
205; CHECK: end_function
206
207; CHECK-LABEL: .Lplain_bitcast:
208; CHECK: call        plain, $1{{$}}
209; CHECK: end_function
210
211; CHECK-LABEL: .Lfoo0_bitcast:
212; CHECK-NEXT: .functype .Lfoo0_bitcast (i32) -> ()
213; CHECK-NEXT: call        foo0{{$}}
214; CHECK-NEXT: end_function
215
216; CHECK-LABEL: .Lfoo1_bitcast:
217; CHECK-NEXT: .functype .Lfoo1_bitcast () -> (i32)
218; CHECK-NEXT: call        foo1{{$}}
219; CHECK-NEXT: local.copy  $push0=, $0
220; CHECK-NEXT: end_function
221