blob: ab03716ef74d98964afadd771ec9e37825f9a19b [file] [log] [blame]
Dan Gohman3a762bf2017-12-08 21:27:00 +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"
Dan Gohman7d7409e2017-02-28 23:37:04 +00006target triple = "wasm32-unknown-unknown-wasm"
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:
19; CHECK-NEXT: call .Lbitcast@FUNCTION{{$}}
Derek Schuff7acb42a2017-01-10 21:59:53 +000020; CHECK-NEXT: call .Lbitcast@FUNCTION{{$}}
Dan Gohman1b637452017-01-07 00:34:54 +000021; CHECK-NEXT: call .Lbitcast.1@FUNCTION{{$}}
Dan Gohman0e2ceb82017-01-07 01:50:01 +000022; CHECK-NEXT: i32.const $push[[L0:[0-9]+]]=, 0
Dan Gohman3a762bf2017-12-08 21:27:00 +000023; CHECK-NEXT: call .Lbitcast.4@FUNCTION, $pop[[L0]]{{$}}
Derek Schuff7acb42a2017-01-10 21:59:53 +000024; CHECK-NEXT: i32.const $push[[L1:[0-9]+]]=, 0
Dan Gohman3a762bf2017-12-08 21:27:00 +000025; CHECK-NEXT: call .Lbitcast.4@FUNCTION, $pop[[L1]]{{$}}
Derek Schuff7acb42a2017-01-10 21:59:53 +000026; CHECK-NEXT: i32.const $push[[L2:[0-9]+]]=, 0
Dan Gohman3a762bf2017-12-08 21:27:00 +000027; CHECK-NEXT: call .Lbitcast.4@FUNCTION, $pop[[L2]]{{$}}
Derek Schuff7acb42a2017-01-10 21:59:53 +000028; CHECK-NEXT: call foo0@FUNCTION
Dan Gohman3a762bf2017-12-08 21:27:00 +000029; CHECK-NEXT: i32.call $drop=, .Lbitcast.5@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{{$}}
Dan Gohman3a762bf2017-12-08 21:27:00 +000057; CHECK-NEXT: call .Lbitcast.2@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]]{{$}}
Dan Gohman3a762bf2017-12-08 21:27:00 +000060; CHECK-NEXT: call .Lbitcast.3@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]]{{$}}
116; CHECK: i32.const $push[[L4:[0-9]+]]=, .Lbitcast@FUNCTION{{$}}
117; 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
141; CHECK-LABEL: .Lbitcast:
142; CHECK-NEXT: call has_i32_arg@FUNCTION, $0{{$}}
143; CHECK-NEXT: end_function
144
145; CHECK-LABEL: .Lbitcast.1:
146; CHECK-NEXT: call $drop=, has_i32_ret@FUNCTION{{$}}
147; CHECK-NEXT: end_function
148
149; CHECK-LABEL: .Lbitcast.2:
Dan Gohman3a762bf2017-12-08 21:27:00 +0000150; CHECK: call vararg@FUNCTION, $1{{$}}
151; CHECK: end_function
152
153; CHECK-LABEL: .Lbitcast.3:
154; CHECK: call plain@FUNCTION, $1{{$}}
155; CHECK: end_function
156
157; CHECK-LABEL: .Lbitcast.4:
Jacob Gravelle37af00e2017-10-10 16:20:18 +0000158; CHECK-NEXT: .param i32
159; CHECK-NEXT: call foo0@FUNCTION{{$}}
160; CHECK-NEXT: end_function
161
Dan Gohman3a762bf2017-12-08 21:27:00 +0000162; CHECK-LABEL: .Lbitcast.5:
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