blob: 8e190b5c0ba893606c018a0e831ae7b872d442ef [file] [log] [blame]
Wouter van Oortmerssena90d24d2018-07-27 23:19:51 +00001; RUN: llc < %s -asm-verbose=false -disable-wasm-explicit-locals -enable-emscripten-cxx-exceptions -wasm-temporary-workarounds=false | FileCheck %s
Dan Gohman1b637452017-01-07 00:34:54 +00002
3; Test that function pointer casts are replaced with wrappers.
4
5target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
Sam Clegga5908002018-05-10 17:49:11 +00006target triple = "wasm32-unknown-unknown"
Dan Gohman1b637452017-01-07 00:34:54 +00007
Jacob Gravelle37af00e2017-10-10 16:20:18 +00008declare void @has_i32_arg(i32)
9declare i32 @has_i32_ret()
10declare void @vararg(...)
11declare void @plain(i32)
12
13declare void @foo0()
14declare void @foo1()
15declare void @foo2()
16declare void @foo3()
17
Dan Gohman1b637452017-01-07 00:34:54 +000018; CHECK-LABEL: test:
Sam Clegg41d70472018-08-02 17:38:06 +000019; CHECK-NEXT: call .Lhas_i32_arg_bitcast@FUNCTION{{$}}
20; CHECK-NEXT: call .Lhas_i32_arg_bitcast@FUNCTION{{$}}
21; CHECK-NEXT: call .Lhas_i32_ret_bitcast@FUNCTION{{$}}
Dan Gohman0e2ceb82017-01-07 01:50:01 +000022; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 0
Sam Clegg41d70472018-08-02 17:38:06 +000023; CHECK-NEXT: call .Lfoo0_bitcast@FUNCTION, $pop[[L0]]{{$}}
Derek Schuff7acb42a2017-01-10 21:59:53 +000024; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, 0
Sam Clegg41d70472018-08-02 17:38:06 +000025; CHECK-NEXT: call .Lfoo0_bitcast@FUNCTION, $pop[[L1]]{{$}}
Derek Schuff7acb42a2017-01-10 21:59:53 +000026; CHECK-NEXT: i32.const $push[[L2:[0-9]+]]=, 0
Sam Clegg41d70472018-08-02 17:38:06 +000027; CHECK-NEXT: call .Lfoo0_bitcast@FUNCTION, $pop[[L2]]{{$}}
Derek Schuff7acb42a2017-01-10 21:59:53 +000028; CHECK-NEXT: call foo0@FUNCTION
Sam Clegg41d70472018-08-02 17:38:06 +000029; CHECK-NEXT: i32.call $drop=, .Lfoo1_bitcast@FUNCTION{{$}}
Dan Gohman1b637452017-01-07 00:34:54 +000030; CHECK-NEXT: call foo2@FUNCTION{{$}}
Derek Schuff7acb42a2017-01-10 21:59:53 +000031; CHECK-NEXT: call foo1@FUNCTION{{$}}
Dan Gohman1b637452017-01-07 00:34:54 +000032; CHECK-NEXT: call foo3@FUNCTION{{$}}
Dan Gohman7d7409e2017-02-28 23:37:04 +000033; CHECK-NEXT: end_function
Dan Gohman1b637452017-01-07 00:34:54 +000034define void @test() {
35entry:
36 call void bitcast (void (i32)* @has_i32_arg to void ()*)()
Derek Schuff7acb42a2017-01-10 21:59:53 +000037 call void bitcast (void (i32)* @has_i32_arg to void ()*)()
Dan Gohman1b637452017-01-07 00:34:54 +000038 call void bitcast (i32 ()* @has_i32_ret to void ()*)()
39 call void bitcast (void ()* @foo0 to void (i32)*)(i32 0)
Derek Schuff7acb42a2017-01-10 21:59:53 +000040 %p = bitcast void ()* @foo0 to void (i32)*
41 call void %p(i32 0)
42 %q = bitcast void ()* @foo0 to void (i32)*
43 call void %q(i32 0)
44 %r = bitcast void (i32)* %q to void ()*
45 call void %r()
Dan Gohman1b637452017-01-07 00:34:54 +000046 %t = call i32 bitcast (void ()* @foo1 to i32 ()*)()
47 call void bitcast (void ()* @foo2 to void ()*)()
Derek Schuff7acb42a2017-01-10 21:59:53 +000048 call void @foo1()
Dan Gohman1b637452017-01-07 00:34:54 +000049 call void @foo3()
Derek Schuff7acb42a2017-01-10 21:59:53 +000050
Dan Gohman1b637452017-01-07 00:34:54 +000051 ret void
52}
Dan Gohmana99b7172017-01-20 20:50:29 +000053
Jacob Gravelle37af00e2017-10-10 16:20:18 +000054; CHECK-LABEL: test_varargs:
55; CHECK: set_global
56; CHECK: i32.const $push[[L3:[0-9]+]]=, 0{{$}}
Sam Clegg41d70472018-08-02 17:38:06 +000057; CHECK-NEXT: call .Lvararg_bitcast@FUNCTION, $pop[[L3]]{{$}}
Jacob Gravelle37af00e2017-10-10 16:20:18 +000058; CHECK-NEXT: i32.const $push[[L4:[0-9]+]]=, 0{{$}}
59; CHECK-NEXT: i32.store 0($[[L5:[0-9]+]]), $pop[[L4]]{{$}}
Sam Clegg41d70472018-08-02 17:38:06 +000060; CHECK-NEXT: call .Lplain_bitcast@FUNCTION, $[[L5]]{{$}}
Dan Gohmana99b7172017-01-20 20:50:29 +000061define void @test_varargs() {
62 call void bitcast (void (...)* @vararg to void (i32)*)(i32 0)
63 call void (...) bitcast (void (i32)* @plain to void (...)*)(i32 0)
64 ret void
65}
Jacob Gravelle37af00e2017-10-10 16:20:18 +000066
67; Don't use wrappers when the value is stored in memory
68
69@global_func = hidden local_unnamed_addr global void ()* null
70
71; CHECK-LABEL: test_store:
72; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 0{{$}}
73; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, has_i32_ret@FUNCTION{{$}}
74; CHECK-NEXT: i32.store global_func($pop[[L0]]), $pop[[L1]]{{$}}
75define void @test_store() {
76 %1 = bitcast i32 ()* @has_i32_ret to void ()*
77 store void ()* %1, void ()** @global_func
78 ret void
79}
80
81; CHECK-LABEL: test_load:
82; CHECK-NEXT: result i32{{$}}
83; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 0{{$}}
84; CHECK-NEXT: i32.load $push[[L1:[0-9]+]]=, global_func($pop[[L0]]){{$}}
85; CHECK-NEXT: i32.call_indirect $push{{[0-9]+}}=, $pop[[L1]]{{$}}
86define i32 @test_load() {
87 %1 = load i32 ()*, i32 ()** bitcast (void ()** @global_func to i32 ()**)
88 %2 = call i32 %1()
89 ret i32 %2
90}
91
92; Don't use wrappers when the value is passed to a function call
93
94declare void @call_func(i32 ()*)
95
96; CHECK-LABEL: test_argument:
97; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, has_i32_ret@FUNCTION{{$}}
98; CHECK-NEXT: call call_func@FUNCTION, $pop[[L0]]{{$}}
99; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, has_i32_arg@FUNCTION{{$}}
100; CHECK-NEXT: call call_func@FUNCTION, $pop[[L1]]{{$}}
101define void @test_argument() {
102 call void @call_func(i32 ()* @has_i32_ret)
103 call void @call_func(i32 ()* bitcast (void (i32)* @has_i32_arg to i32 ()*))
104 ret void
105}
106
107; Invokes should be treated like calls
108
109; CHECK-LABEL: test_invoke:
110; CHECK: i32.const $push[[L1:[0-9]+]]=, call_func@FUNCTION{{$}}
111; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, has_i32_ret@FUNCTION{{$}}
112; CHECK-NEXT: call "__invoke_void_i32()*"@FUNCTION, $pop[[L1]], $pop[[L0]]{{$}}
113; CHECK: i32.const $push[[L3:[0-9]+]]=, call_func@FUNCTION{{$}}
114; CHECK-NEXT: i32.const $push[[L2:[0-9]+]]=, has_i32_arg@FUNCTION{{$}}
115; CHECK-NEXT: call "__invoke_void_i32()*"@FUNCTION, $pop[[L3]], $pop[[L2]]{{$}}
Sam Clegg41d70472018-08-02 17:38:06 +0000116; CHECK: i32.const $push[[L4:[0-9]+]]=, .Lhas_i32_arg_bitcast@FUNCTION{{$}}
Jacob Gravelle37af00e2017-10-10 16:20:18 +0000117; CHECK-NEXT: call __invoke_void@FUNCTION, $pop[[L4]]{{$}}
118declare i32 @personality(...)
119define void @test_invoke() personality i32 (...)* @personality {
120entry:
121 invoke void @call_func(i32 ()* @has_i32_ret)
122 to label %cont unwind label %lpad
123
124cont:
125 invoke void @call_func(i32 ()* bitcast (void (i32)* @has_i32_arg to i32 ()*))
126 to label %cont2 unwind label %lpad
127
128cont2:
129 invoke void bitcast (void (i32)* @has_i32_arg to void ()*)()
130 to label %end unwind label %lpad
131
132lpad:
133 %0 = landingpad { i8*, i32 }
134 catch i8* null
135 br label %end
136
137end:
138 ret void
139}
140
Sam Clegg41d70472018-08-02 17:38:06 +0000141; CHECK-LABEL: .Lhas_i32_arg_bitcast:
Jacob Gravelle37af00e2017-10-10 16:20:18 +0000142; CHECK-NEXT: call has_i32_arg@FUNCTION, $0{{$}}
143; CHECK-NEXT: end_function
144
Sam Clegg41d70472018-08-02 17:38:06 +0000145; CHECK-LABEL: .Lhas_i32_ret_bitcast:
Jacob Gravelle37af00e2017-10-10 16:20:18 +0000146; CHECK-NEXT: call $drop=, has_i32_ret@FUNCTION{{$}}
147; CHECK-NEXT: end_function
148
Sam Clegg41d70472018-08-02 17:38:06 +0000149; CHECK-LABEL: .Lvararg_bitcast:
Dan Gohman3a762bf2017-12-08 21:27:00 +0000150; CHECK: call vararg@FUNCTION, $1{{$}}
151; CHECK: end_function
152
Sam Clegg41d70472018-08-02 17:38:06 +0000153; CHECK-LABEL: .Lplain_bitcast:
Dan Gohman3a762bf2017-12-08 21:27:00 +0000154; CHECK: call plain@FUNCTION, $1{{$}}
155; CHECK: end_function
156
Sam Clegg41d70472018-08-02 17:38:06 +0000157; CHECK-LABEL: .Lfoo0_bitcast:
Jacob Gravelle37af00e2017-10-10 16:20:18 +0000158; CHECK-NEXT: .param i32
159; CHECK-NEXT: call foo0@FUNCTION{{$}}
160; CHECK-NEXT: end_function
161
Sam Clegg41d70472018-08-02 17:38:06 +0000162; CHECK-LABEL: .Lfoo1_bitcast:
Jacob Gravelle37af00e2017-10-10 16:20:18 +0000163; CHECK-NEXT: .result i32
164; CHECK-NEXT: call foo1@FUNCTION{{$}}
165; CHECK-NEXT: copy_local $push0=, $0
166; CHECK-NEXT: end_function