1 ; RUN: llc < %s -asm-verbose=false -wasm-disable-explicit-locals -wasm-keep-registers -enable-emscripten-cxx-exceptions | FileCheck %s
3 ; Test that function pointer casts are replaced with wrappers.
5 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6 target triple = "wasm32-unknown-unknown"
8 define void @has_i32_arg(i32) {
13 declare void @has_struct_arg({i32})
14 declare i32 @has_i32_ret()
15 declare void @vararg(...)
16 declare void @plain(i32)
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: i32.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: i32.call $drop=, .Lfoo1_bitcast{{$}}
36 ; CHECK-NEXT: call foo2{{$}}
37 ; CHECK-NEXT: call foo1{{$}}
38 ; CHECK-NEXT: call foo3{{$}}
39 ; CHECK-NEXT: end_function
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)*
49 %q = bitcast void ()* @foo0 to void (i32)*
51 %r = bitcast void (i32)* %q to void ()*
53 %t = call i32 bitcast (void ()* @foo1 to i32 ()*)()
54 call void bitcast (void ()* @foo2 to void ()*)()
61 ; Calling aliases should also generate a wrapper
63 @alias_i32_arg = weak hidden alias void (i32), void (i32)* @has_i32_arg
65 ; CHECK-LABEL: test_alias:
66 ; CHECK: call .Lhas_i32_arg_bitcast.2
67 define void @test_alias() {
69 call void bitcast (void (i32)* @alias_i32_arg to void ()*)()
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{{$}}
78 define void @test_structs() {
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 ()*)()
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]+$}}
90 define void @test_structs_unhandled() {
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} ()*)()
98 ; CHECK-LABEL: test_varargs:
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]]{{$}}
105 define 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)
111 ; Don't use wrappers when the value is stored in memory
113 @global_func = hidden local_unnamed_addr global void ()* null
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]]{{$}}
119 define void @test_store() {
120 %1 = bitcast i32 ()* @has_i32_ret to void ()*
121 store void ()* %1, void ()** @global_func
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: i32.call_indirect $push{{[0-9]+}}=, $pop[[L1]]{{$}}
130 define i32 @test_load() {
131 %1 = load i32 ()*, i32 ()** bitcast (void ()** @global_func to i32 ()**)
136 ; Don't use wrappers when the value is passed to a function call
138 declare void @call_func(i32 ()*)
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]]{{$}}
145 define 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 ()*))
151 ; Invokes should be treated like calls
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]]{{$}}
162 declare i32 @personality(...)
163 define void @test_invoke() personality i32 (...)* @personality {
165 invoke void @call_func(i32 ()* @has_i32_ret)
166 to label %cont unwind label %lpad
169 invoke void @call_func(i32 ()* bitcast (void (i32)* @has_i32_arg to i32 ()*))
170 to label %cont2 unwind label %lpad
173 invoke void bitcast (void (i32)* @has_i32_arg to void ()*)()
174 to label %end unwind label %lpad
177 %0 = landingpad { i8*, i32 }
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
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
195 ; CHECK-LABEL: .Lhas_i32_arg_bitcast.2:
196 ; CHECK: call has_i32_arg, $0{{$}}
197 ; CHECK-NEXT: end_function
199 ; CHECK-LABEL: .Lhas_i32_ret_bitcast:
200 ; CHECK: call $drop=, has_i32_ret{{$}}
201 ; CHECK-NEXT: end_function
203 ; CHECK-LABEL: .Lvararg_bitcast:
204 ; CHECK: call vararg, $1{{$}}
205 ; CHECK: end_function
207 ; CHECK-LABEL: .Lplain_bitcast:
208 ; CHECK: call plain, $1{{$}}
209 ; CHECK: end_function
211 ; CHECK-LABEL: .Lfoo0_bitcast:
212 ; CHECK-NEXT: .functype .Lfoo0_bitcast (i32) -> ()
213 ; CHECK-NEXT: call foo0{{$}}
214 ; CHECK-NEXT: end_function
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