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