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 triple = "wasm32-unknown-unknown"
7 define void @has_i32_arg(i32) {
12 declare void @has_struct_arg({i32})
13 declare i32 @has_i32_ret()
14 declare void @vararg(...)
15 declare void @plain(i32)
23 ; CHECK: call .Lhas_i32_arg_bitcast.2{{$}}
24 ; CHECK-NEXT: call .Lhas_i32_arg_bitcast.2{{$}}
25 ; CHECK-NEXT: call .Lhas_i32_ret_bitcast{{$}}
26 ; CHECK-NEXT: call $drop=, has_i32_ret
27 ; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 0
28 ; CHECK-NEXT: call .Lfoo0_bitcast, $pop[[L0]]{{$}}
29 ; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, 0
30 ; CHECK-NEXT: call .Lfoo0_bitcast, $pop[[L1]]{{$}}
31 ; CHECK-NEXT: i32.const $push[[L2:[0-9]+]]=, 0
32 ; CHECK-NEXT: call .Lfoo0_bitcast, $pop[[L2]]{{$}}
33 ; CHECK-NEXT: call foo0
34 ; CHECK-NEXT: call $drop=, .Lfoo1_bitcast{{$}}
35 ; CHECK-NEXT: call foo2{{$}}
36 ; CHECK-NEXT: call foo1{{$}}
37 ; CHECK-NEXT: call foo3{{$}}
38 ; CHECK-NEXT: end_function
41 call void bitcast (void (i32)* @has_i32_arg to void ()*)()
42 call void bitcast (void (i32)* @has_i32_arg to void ()*)()
43 call void bitcast (i32 ()* @has_i32_ret to void ()*)()
44 call i32 bitcast (i32 ()* @has_i32_ret to i32 ()*)()
45 call void bitcast (void ()* @foo0 to void (i32)*)(i32 0)
46 %p = bitcast void ()* @foo0 to void (i32)*
48 %q = bitcast void ()* @foo0 to void (i32)*
50 %r = bitcast void (i32)* %q to void ()*
52 %t = call i32 bitcast (void ()* @foo1 to i32 ()*)()
53 call void bitcast (void ()* @foo2 to void ()*)()
60 ; Calling aliases should also generate a wrapper
62 @alias_i32_arg = weak hidden alias void (i32), void (i32)* @has_i32_arg
64 ; CHECK-LABEL: test_alias:
65 ; CHECK: call .Lhas_i32_arg_bitcast.2
66 define void @test_alias() {
68 call void bitcast (void (i32)* @alias_i32_arg to void ()*)()
73 ; CHECK-LABEL: test_structs:
74 ; CHECK: call .Lhas_i32_arg_bitcast.1, $pop{{[0-9]+}}, $pop{{[0-9]+$}}
75 ; CHECK: call .Lhas_i32_arg_bitcast, $0, $pop2
76 ; CHECK: call .Lhas_struct_arg_bitcast{{$}}
77 define void @test_structs() {
79 call void bitcast (void (i32)* @has_i32_arg to void (i32, {i32})*)(i32 5, {i32} {i32 6})
80 call {i32, i64} bitcast (void (i32)* @has_i32_arg to {i32, i64} (i32)*)(i32 7)
81 call void bitcast (void ({i32})* @has_struct_arg to void ()*)()
85 ; CHECK-LABEL: test_structs_unhandled:
86 ; CHECK: call has_struct_arg, $pop{{[0-9]+$}}
87 ; CHECK: call has_struct_arg, $pop{{[0-9]+$}}
88 ; CHECK: call has_i32_ret, $pop{{[0-9]+$}}
89 define void @test_structs_unhandled() {
91 call void @has_struct_arg({i32} {i32 3})
92 call void bitcast (void ({i32})* @has_struct_arg to void ({i64})*)({i64} {i64 4})
93 call {i32, i32} bitcast (i32 ()* @has_i32_ret to {i32, i32} ()*)()
97 ; CHECK-LABEL: test_varargs:
99 ; CHECK: i32.const $push[[L3:[0-9]+]]=, 0{{$}}
100 ; CHECK-NEXT: call .Lvararg_bitcast, $pop[[L3]]{{$}}
101 ; CHECK-NEXT: i32.const $push[[L4:[0-9]+]]=, 0{{$}}
102 ; CHECK-NEXT: i32.store 0($[[L5:[0-9]+]]), $pop[[L4]]{{$}}
103 ; CHECK-NEXT: call .Lplain_bitcast, $[[L5]]{{$}}
104 define void @test_varargs() {
105 call void bitcast (void (...)* @vararg to void (i32)*)(i32 0)
106 call void (...) bitcast (void (i32)* @plain to void (...)*)(i32 0)
110 ; Don't use wrappers when the value is stored in memory
112 @global_func = hidden local_unnamed_addr global void ()* null
114 ; CHECK-LABEL: test_store:
115 ; CHECK: i32.const $push[[L0:[0-9]+]]=, 0{{$}}
116 ; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, has_i32_ret{{$}}
117 ; CHECK-NEXT: i32.store global_func($pop[[L0]]), $pop[[L1]]{{$}}
118 define void @test_store() {
119 %1 = bitcast i32 ()* @has_i32_ret to void ()*
120 store void ()* %1, void ()** @global_func
124 ; CHECK-LABEL: test_load:
125 ; CHECK-NEXT: .functype test_load () -> (i32){{$}}
126 ; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 0{{$}}
127 ; CHECK-NEXT: i32.load $push[[L1:[0-9]+]]=, global_func($pop[[L0]]){{$}}
128 ; CHECK-NEXT: call_indirect $push{{[0-9]+}}=, $pop[[L1]]{{$}}
129 define i32 @test_load() {
130 %1 = load i32 ()*, i32 ()** bitcast (void ()** @global_func to i32 ()**)
135 ; Don't use wrappers when the value is passed to a function call
137 declare void @call_func(i32 ()*)
139 ; CHECK-LABEL: test_argument:
140 ; CHECK: i32.const $push[[L0:[0-9]+]]=, has_i32_ret{{$}}
141 ; CHECK-NEXT: call call_func, $pop[[L0]]{{$}}
142 ; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, has_i32_arg{{$}}
143 ; CHECK-NEXT: call call_func, $pop[[L1]]{{$}}
144 define void @test_argument() {
145 call void @call_func(i32 ()* @has_i32_ret)
146 call void @call_func(i32 ()* bitcast (void (i32)* @has_i32_arg to i32 ()*))
150 ; Invokes should be treated like calls
152 ; CHECK-LABEL: test_invoke:
153 ; CHECK: i32.const $push[[L1:[0-9]+]]=, call_func{{$}}
154 ; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, has_i32_ret{{$}}
155 ; CHECK-NEXT: call invoke_vi, $pop[[L1]], $pop[[L0]]{{$}}
156 ; CHECK: i32.const $push[[L3:[0-9]+]]=, call_func{{$}}
157 ; CHECK-NEXT: i32.const $push[[L2:[0-9]+]]=, has_i32_arg{{$}}
158 ; CHECK-NEXT: call invoke_vi, $pop[[L3]], $pop[[L2]]{{$}}
159 ; CHECK: i32.const $push[[L4:[0-9]+]]=, .Lhas_i32_arg_bitcast.2{{$}}
160 ; CHECK-NEXT: call invoke_v, $pop[[L4]]{{$}}
161 declare i32 @personality(...)
162 define void @test_invoke() personality i32 (...)* @personality {
164 invoke void @call_func(i32 ()* @has_i32_ret)
165 to label %cont unwind label %lpad
168 invoke void @call_func(i32 ()* bitcast (void (i32)* @has_i32_arg to i32 ()*))
169 to label %cont2 unwind label %lpad
172 invoke void bitcast (void (i32)* @has_i32_arg to void ()*)()
173 to label %end unwind label %lpad
176 %0 = landingpad { i8*, i32 }
184 ; CHECK-LABEL: .Lhas_i32_arg_bitcast:
185 ; CHECK-NEXT: .functype .Lhas_i32_arg_bitcast (i32, i32) -> ()
186 ; CHECK-NEXT: call has_i32_arg, $1{{$}}
187 ; CHECK-NEXT: end_function
189 ; CHECK-LABEL: .Lhas_i32_arg_bitcast.1:
190 ; CHECK-NEXT: .functype .Lhas_i32_arg_bitcast.1 (i32, i32) -> ()
191 ; CHECK-NEXT: call has_i32_arg, $0{{$}}
192 ; CHECK-NEXT: end_function
194 ; CHECK-LABEL: .Lhas_i32_arg_bitcast.2:
195 ; CHECK: call has_i32_arg, $0{{$}}
196 ; CHECK-NEXT: end_function
198 ; CHECK-LABEL: .Lhas_i32_ret_bitcast:
199 ; CHECK: call $drop=, has_i32_ret{{$}}
200 ; CHECK-NEXT: end_function
202 ; CHECK-LABEL: .Lvararg_bitcast:
203 ; CHECK: call vararg, $1{{$}}
204 ; CHECK: end_function
206 ; CHECK-LABEL: .Lplain_bitcast:
207 ; CHECK: call plain, $1{{$}}
208 ; CHECK: end_function
210 ; CHECK-LABEL: .Lfoo0_bitcast:
211 ; CHECK-NEXT: .functype .Lfoo0_bitcast (i32) -> ()
212 ; CHECK-NEXT: call foo0{{$}}
213 ; CHECK-NEXT: end_function
215 ; CHECK-LABEL: .Lfoo1_bitcast:
216 ; CHECK-NEXT: .functype .Lfoo1_bitcast () -> (i32)
217 ; CHECK-NEXT: call foo1{{$}}
218 ; CHECK-NEXT: local.copy $push0=, $0
219 ; CHECK-NEXT: end_function