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 declare void @has_i32_arg(i32)
9 declare void @has_struct_arg({i32})
10 declare i32 @has_i32_ret()
11 declare void @vararg(...)
12 declare void @plain(i32)
20 ; CHECK: call .Lhas_i32_arg_bitcast.2{{$}}
21 ; CHECK-NEXT: call .Lhas_i32_arg_bitcast.2{{$}}
22 ; CHECK-NEXT: call .Lhas_i32_ret_bitcast{{$}}
23 ; CHECK-NEXT: i32.call $drop=, has_i32_ret
24 ; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 0
25 ; CHECK-NEXT: call .Lfoo0_bitcast, $pop[[L0]]{{$}}
26 ; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, 0
27 ; CHECK-NEXT: call .Lfoo0_bitcast, $pop[[L1]]{{$}}
28 ; CHECK-NEXT: i32.const $push[[L2:[0-9]+]]=, 0
29 ; CHECK-NEXT: call .Lfoo0_bitcast, $pop[[L2]]{{$}}
30 ; CHECK-NEXT: call foo0
31 ; CHECK-NEXT: i32.call $drop=, .Lfoo1_bitcast{{$}}
32 ; CHECK-NEXT: call foo2{{$}}
33 ; CHECK-NEXT: call foo1{{$}}
34 ; CHECK-NEXT: call foo3{{$}}
35 ; CHECK-NEXT: end_function
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)*
45 %q = bitcast void ()* @foo0 to void (i32)*
47 %r = bitcast void (i32)* %q to void ()*
49 %t = call i32 bitcast (void ()* @foo1 to i32 ()*)()
50 call void bitcast (void ()* @foo2 to void ()*)()
57 ; CHECK-LABEL: test_structs:
58 ; CHECK: call .Lhas_i32_arg_bitcast.1, $pop{{[0-9]+}}, $pop{{[0-9]+$}}
59 ; CHECK: call .Lhas_i32_arg_bitcast, $0, $pop2
60 ; CHECK: call .Lhas_struct_arg_bitcast{{$}}
61 define void @test_structs() {
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 ()*)()
69 ; CHECK-LABEL: test_structs_unhandled:
70 ; CHECK: call has_struct_arg, $pop{{[0-9]+$}}
71 ; CHECK: call has_struct_arg, $pop{{[0-9]+$}}
72 ; CHECK: call has_i32_ret, $pop{{[0-9]+$}}
73 define void @test_structs_unhandled() {
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} ()*)()
81 ; CHECK-LABEL: test_varargs:
83 ; CHECK: i32.const $push[[L3:[0-9]+]]=, 0{{$}}
84 ; CHECK-NEXT: call .Lvararg_bitcast, $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, $[[L5]]{{$}}
88 define 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)
94 ; Don't use wrappers when the value is stored in memory
96 @global_func = hidden local_unnamed_addr global void ()* null
98 ; CHECK-LABEL: test_store:
99 ; CHECK: i32.const $push[[L0:[0-9]+]]=, 0{{$}}
100 ; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, has_i32_ret{{$}}
101 ; CHECK-NEXT: i32.store global_func($pop[[L0]]), $pop[[L1]]{{$}}
102 define void @test_store() {
103 %1 = bitcast i32 ()* @has_i32_ret to void ()*
104 store void ()* %1, void ()** @global_func
108 ; CHECK-LABEL: test_load:
109 ; CHECK-NEXT: .functype test_load () -> (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]]{{$}}
113 define i32 @test_load() {
114 %1 = load i32 ()*, i32 ()** bitcast (void ()** @global_func to i32 ()**)
119 ; Don't use wrappers when the value is passed to a function call
121 declare void @call_func(i32 ()*)
123 ; CHECK-LABEL: test_argument:
124 ; CHECK: i32.const $push[[L0:[0-9]+]]=, has_i32_ret{{$}}
125 ; CHECK-NEXT: call call_func, $pop[[L0]]{{$}}
126 ; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, has_i32_arg{{$}}
127 ; CHECK-NEXT: call call_func, $pop[[L1]]{{$}}
128 define 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 ()*))
134 ; Invokes should be treated like calls
136 ; CHECK-LABEL: test_invoke:
137 ; CHECK: i32.const $push[[L1:[0-9]+]]=, call_func{{$}}
138 ; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, has_i32_ret{{$}}
139 ; CHECK-NEXT: call "__invoke_void_i32()*", $pop[[L1]], $pop[[L0]]{{$}}
140 ; CHECK: i32.const $push[[L3:[0-9]+]]=, call_func{{$}}
141 ; CHECK-NEXT: i32.const $push[[L2:[0-9]+]]=, has_i32_arg{{$}}
142 ; CHECK-NEXT: call "__invoke_void_i32()*", $pop[[L3]], $pop[[L2]]{{$}}
143 ; CHECK: i32.const $push[[L4:[0-9]+]]=, .Lhas_i32_arg_bitcast.2{{$}}
144 ; CHECK-NEXT: call __invoke_void, $pop[[L4]]{{$}}
145 declare i32 @personality(...)
146 define void @test_invoke() personality i32 (...)* @personality {
148 invoke void @call_func(i32 ()* @has_i32_ret)
149 to label %cont unwind label %lpad
152 invoke void @call_func(i32 ()* bitcast (void (i32)* @has_i32_arg to i32 ()*))
153 to label %cont2 unwind label %lpad
156 invoke void bitcast (void (i32)* @has_i32_arg to void ()*)()
157 to label %end unwind label %lpad
160 %0 = landingpad { i8*, i32 }
168 ; CHECK-LABEL: .Lhas_i32_arg_bitcast:
169 ; CHECK-NEXT: .functype .Lhas_i32_arg_bitcast (i32, i32) -> ()
170 ; CHECK-NEXT: call has_i32_arg, $1{{$}}
171 ; CHECK-NEXT: end_function
173 ; CHECK-LABEL: .Lhas_i32_arg_bitcast.1:
174 ; CHECK-NEXT: .functype .Lhas_i32_arg_bitcast.1 (i32, i32) -> ()
175 ; CHECK-NEXT: call has_i32_arg, $0{{$}}
176 ; CHECK-NEXT: end_function
178 ; CHECK-LABEL: .Lhas_i32_arg_bitcast.2:
179 ; CHECK: call has_i32_arg, $0{{$}}
180 ; CHECK-NEXT: end_function
182 ; CHECK-LABEL: .Lhas_i32_ret_bitcast:
183 ; CHECK: call $drop=, has_i32_ret{{$}}
184 ; CHECK-NEXT: end_function
186 ; CHECK-LABEL: .Lvararg_bitcast:
187 ; CHECK: call vararg, $1{{$}}
188 ; CHECK: end_function
190 ; CHECK-LABEL: .Lplain_bitcast:
191 ; CHECK: call plain, $1{{$}}
192 ; CHECK: end_function
194 ; CHECK-LABEL: .Lfoo0_bitcast:
195 ; CHECK-NEXT: .functype .Lfoo0_bitcast (i32) -> ()
196 ; CHECK-NEXT: call foo0{{$}}
197 ; CHECK-NEXT: end_function
199 ; CHECK-LABEL: .Lfoo1_bitcast:
200 ; CHECK-NEXT: .functype .Lfoo1_bitcast () -> (i32)
201 ; CHECK-NEXT: call foo1{{$}}
202 ; CHECK-NEXT: local.copy $push0=, $0
203 ; CHECK-NEXT: end_function