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 triple = "wasm32-unknown-unknown" 6 7define void @has_i32_arg(i32) { 8entry: 9 ret void 10} 11 12declare void @has_struct_arg({i32}) 13declare i32 @has_i32_ret() 14declare void @vararg(...) 15declare void @plain(i32) 16 17declare void @foo0() 18declare void @foo1() 19declare void @foo2() 20declare void @foo3() 21 22; CHECK-LABEL: test: 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 39define void @test() { 40entry: 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)* 47 call void %p(i32 0) 48 %q = bitcast void ()* @foo0 to void (i32)* 49 call void %q(i32 0) 50 %r = bitcast void (i32)* %q to void ()* 51 call void %r() 52 %t = call i32 bitcast (void ()* @foo1 to i32 ()*)() 53 call void bitcast (void ()* @foo2 to void ()*)() 54 call void @foo1() 55 call void @foo3() 56 57 ret void 58} 59 60; Calling aliases should also generate a wrapper 61 62@alias_i32_arg = weak hidden alias void (i32), void (i32)* @has_i32_arg 63 64; CHECK-LABEL: test_alias: 65; CHECK: call .Lhas_i32_arg_bitcast.2 66define void @test_alias() { 67entry: 68 call void bitcast (void (i32)* @alias_i32_arg to void ()*)() 69 ret void 70} 71 72 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{{$}} 77define void @test_structs() { 78entry: 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 ()*)() 82 ret void 83} 84 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]+$}} 89define void @test_structs_unhandled() { 90entry: 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} ()*)() 94 ret void 95} 96 97; CHECK-LABEL: test_varargs: 98; CHECK: global.set 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]]{{$}} 104define 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) 107 ret void 108} 109 110; Don't use wrappers when the value is stored in memory 111 112@global_func = hidden local_unnamed_addr global void ()* null 113 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]]{{$}} 118define void @test_store() { 119 %1 = bitcast i32 ()* @has_i32_ret to void ()* 120 store void ()* %1, void ()** @global_func 121 ret void 122} 123 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]]{{$}} 129define i32 @test_load() { 130 %1 = load i32 ()*, i32 ()** bitcast (void ()** @global_func to i32 ()**) 131 %2 = call i32 %1() 132 ret i32 %2 133} 134 135; Don't use wrappers when the value is passed to a function call 136 137declare void @call_func(i32 ()*) 138 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]]{{$}} 144define 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 ()*)) 147 ret void 148} 149 150; Invokes should be treated like calls 151 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]]{{$}} 161declare i32 @personality(...) 162define void @test_invoke() personality i32 (...)* @personality { 163entry: 164 invoke void @call_func(i32 ()* @has_i32_ret) 165 to label %cont unwind label %lpad 166 167cont: 168 invoke void @call_func(i32 ()* bitcast (void (i32)* @has_i32_arg to i32 ()*)) 169 to label %cont2 unwind label %lpad 170 171cont2: 172 invoke void bitcast (void (i32)* @has_i32_arg to void ()*)() 173 to label %end unwind label %lpad 174 175lpad: 176 %0 = landingpad { i8*, i32 } 177 catch i8* null 178 br label %end 179 180end: 181 ret void 182} 183 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 188 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 193 194; CHECK-LABEL: .Lhas_i32_arg_bitcast.2: 195; CHECK: call has_i32_arg, $0{{$}} 196; CHECK-NEXT: end_function 197 198; CHECK-LABEL: .Lhas_i32_ret_bitcast: 199; CHECK: call $drop=, has_i32_ret{{$}} 200; CHECK-NEXT: end_function 201 202; CHECK-LABEL: .Lvararg_bitcast: 203; CHECK: call vararg, $1{{$}} 204; CHECK: end_function 205 206; CHECK-LABEL: .Lplain_bitcast: 207; CHECK: call plain, $1{{$}} 208; CHECK: end_function 209 210; CHECK-LABEL: .Lfoo0_bitcast: 211; CHECK-NEXT: .functype .Lfoo0_bitcast (i32) -> () 212; CHECK-NEXT: call foo0{{$}} 213; CHECK-NEXT: end_function 214 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 220