1 ; RUN: llc -verify-machineinstrs -frame-pointer=all -global-isel < %s -mtriple=aarch64-apple-ios -disable-post-ra | FileCheck %s
3 declare i8* @malloc(i64)
4 declare void @free(i8*)
5 %swift_error = type {i64, i8}
7 ; This tests the basic usage of a swifterror parameter. "foo" is the function
8 ; that takes a swifterror parameter and "caller" is the caller of "foo".
9 define float @foo(%swift_error** swifterror %error_ptr_ref) {
11 ; CHECK: mov [[ID:w[0-9]+]], #1
14 ; CHECK: strb [[ID]], [x0, #8]
19 %call = call i8* @malloc(i64 16)
20 %call.0 = bitcast i8* %call to %swift_error*
21 store %swift_error* %call.0, %swift_error** %error_ptr_ref
22 %tmp = getelementptr inbounds i8, i8* %call, i64 8
27 ; "caller" calls "foo" that takes a swifterror parameter.
28 define float @caller(i8* %error_ref) {
29 ; CHECK-LABEL: caller:
30 ; CHECK: mov [[ID:x[0-9]+]], x0
34 ; Access part of the error object and save it to error_ref
35 ; CHECK: ldrb [[CODE:w[0-9]+]], [x0, #8]
36 ; CHECK: strb [[CODE]], [{{.*}}[[ID]]]
37 ; CHECK: bl {{.*}}free
40 %error_ptr_ref = alloca swifterror %swift_error*
41 store %swift_error* null, %swift_error** %error_ptr_ref
42 %call = call float @foo(%swift_error** swifterror %error_ptr_ref)
43 %error_from_foo = load %swift_error*, %swift_error** %error_ptr_ref
44 %had_error_from_foo = icmp ne %swift_error* %error_from_foo, null
45 %tmp = bitcast %swift_error* %error_from_foo to i8*
46 br i1 %had_error_from_foo, label %handler, label %cont
48 %v1 = getelementptr inbounds %swift_error, %swift_error* %error_from_foo, i64 0, i32 1
50 store i8 %t, i8* %error_ref
53 call void @free(i8* %tmp)
57 ; "caller2" is the caller of "foo", it calls "foo" inside a loop.
58 define float @caller2(i8* %error_ref) {
59 ; CHECK-LABEL: caller2:
60 ; CHECK: mov [[ID:x[0-9]+]], x0
61 ; CHECK: fmov [[CMP:s[0-9]+]], #1.0
65 ; CHECK: fcmp s0, [[CMP]]
67 ; Access part of the error object and save it to error_ref
68 ; CHECK: ldrb [[CODE:w[0-9]+]], [x21, #8]
69 ; CHECK: strb [[CODE]], [{{.*}}[[ID]]]
71 ; CHECK: bl {{.*}}free
74 %error_ptr_ref = alloca swifterror %swift_error*
77 store %swift_error* null, %swift_error** %error_ptr_ref
78 %call = call float @foo(%swift_error** swifterror %error_ptr_ref)
79 %error_from_foo = load %swift_error*, %swift_error** %error_ptr_ref
80 %had_error_from_foo = icmp ne %swift_error* %error_from_foo, null
81 %tmp = bitcast %swift_error* %error_from_foo to i8*
82 br i1 %had_error_from_foo, label %handler, label %cont
84 %cmp = fcmp ogt float %call, 1.000000e+00
85 br i1 %cmp, label %bb_end, label %bb_loop
87 %v1 = getelementptr inbounds %swift_error, %swift_error* %error_from_foo, i64 0, i32 1
89 store i8 %t, i8* %error_ref
92 call void @free(i8* %tmp)
96 ; "foo_if" is a function that takes a swifterror parameter, it sets swifterror
97 ; under a certain condition.
98 define float @foo_if(%swift_error** swifterror %error_ptr_ref, i32 %cc) {
99 ; CHECK-LABEL: foo_if:
103 ; CHECK: mov [[ID:w[0-9]+]], #1
104 ; CHECK: strb [[ID]], [x0, #8]
110 %cond = icmp ne i32 %cc, 0
111 br i1 %cond, label %gen_error, label %normal
114 %call = call i8* @malloc(i64 16)
115 %call.0 = bitcast i8* %call to %swift_error*
116 store %swift_error* %call.0, %swift_error** %error_ptr_ref
117 %tmp = getelementptr inbounds i8, i8* %call, i64 8
125 ; "foo_loop" is a function that takes a swifterror parameter, it sets swifterror
126 ; under a certain condition inside a loop.
127 define float @foo_loop(%swift_error** swifterror %error_ptr_ref, i32 %cc, float %cc2) {
128 ; CHECK-LABEL: foo_loop:
133 ; CHECK: strb w{{.*}}, [x0, #8]
142 %cond = icmp ne i32 %cc, 0
143 br i1 %cond, label %gen_error, label %bb_cont
146 %call = call i8* @malloc(i64 16)
147 %call.0 = bitcast i8* %call to %swift_error*
148 store %swift_error* %call.0, %swift_error** %error_ptr_ref
149 %tmp = getelementptr inbounds i8, i8* %call, i64 8
154 %cmp = fcmp ogt float %cc2, 1.000000e+00
155 br i1 %cmp, label %bb_end, label %bb_loop
160 %struct.S = type { i32, i32, i32, i32, i32, i32 }
162 ; "foo_sret" is a function that takes a swifterror parameter, it also has a sret
164 define void @foo_sret(%struct.S* sret %agg.result, i32 %val1, %swift_error** swifterror %error_ptr_ref) {
165 ; CHECK-LABEL: foo_sret:
166 ; CHECK: mov [[SRET:x[0-9]+]], x8
167 ; CHECK: mov [[ID:w[0-9]+]], #1
170 ; CHECK: strb [[ID]], [x0, #8]
171 ; CHECK: str w{{.*}}, [{{.*}}[[SRET]], #4]
176 %call = call i8* @malloc(i64 16)
177 %call.0 = bitcast i8* %call to %swift_error*
178 store %swift_error* %call.0, %swift_error** %error_ptr_ref
179 %tmp = getelementptr inbounds i8, i8* %call, i64 8
181 %v2 = getelementptr inbounds %struct.S, %struct.S* %agg.result, i32 0, i32 1
182 store i32 %val1, i32* %v2
186 ; "caller3" calls "foo_sret" that takes a swifterror parameter.
187 define float @caller3(i8* %error_ref) {
188 ; CHECK-LABEL: caller3:
189 ; CHECK: mov [[ID:x[0-9]+]], x0
190 ; CHECK: mov [[ZERO:x[0-9]+]], xzr
191 ; CHECK: bl {{.*}}foo_sret
194 ; Access part of the error object and save it to error_ref
195 ; CHECK: ldrb [[CODE:w[0-9]+]], [x0, #8]
196 ; CHECK: strb [[CODE]], [{{.*}}[[ID]]]
197 ; CHECK: bl {{.*}}free
200 %s = alloca %struct.S, align 8
201 %error_ptr_ref = alloca swifterror %swift_error*
202 store %swift_error* null, %swift_error** %error_ptr_ref
203 call void @foo_sret(%struct.S* sret %s, i32 1, %swift_error** swifterror %error_ptr_ref)
204 %error_from_foo = load %swift_error*, %swift_error** %error_ptr_ref
205 %had_error_from_foo = icmp ne %swift_error* %error_from_foo, null
206 %tmp = bitcast %swift_error* %error_from_foo to i8*
207 br i1 %had_error_from_foo, label %handler, label %cont
209 %v1 = getelementptr inbounds %swift_error, %swift_error* %error_from_foo, i64 0, i32 1
210 %t = load i8, i8* %v1
211 store i8 %t, i8* %error_ref
214 call void @free(i8* %tmp)
218 ; "foo_vararg" is a function that takes a swifterror parameter, it also has
219 ; variable number of arguments.
220 declare void @llvm.va_start(i8*) nounwind
221 define float @foo_vararg(%swift_error** swifterror %error_ptr_ref, ...) {
222 ; CHECK-LABEL: foo_vararg:
223 ; CHECK-DAG: mov [[ID:w[0-9]+]], #1
226 ; CHECK-DAG: strb [[ID]], [x0, #8]
229 ; CHECK: ldr {{w[0-9]+}}, [x[[ARG1:[0-9]+]]], #8
231 ; CHECK: ldr {{w[0-9]+}}, [x[[ARG1]]], #8
233 ; CHECK: ldr {{w[0-9]+}}, [x[[ARG1]]], #8
238 %call = call i8* @malloc(i64 16)
239 %call.0 = bitcast i8* %call to %swift_error*
240 store %swift_error* %call.0, %swift_error** %error_ptr_ref
241 %tmp = getelementptr inbounds i8, i8* %call, i64 8
244 %args = alloca i8*, align 8
245 %a10 = alloca i32, align 4
246 %a11 = alloca i32, align 4
247 %a12 = alloca i32, align 4
248 %v10 = bitcast i8** %args to i8*
249 call void @llvm.va_start(i8* %v10)
250 %v11 = va_arg i8** %args, i32
251 store i32 %v11, i32* %a10, align 4
252 %v12 = va_arg i8** %args, i32
253 store i32 %v12, i32* %a11, align 4
254 %v13 = va_arg i8** %args, i32
255 store i32 %v13, i32* %a12, align 4
260 ; "caller4" calls "foo_vararg" that takes a swifterror parameter.
261 define float @caller4(i8* %error_ref) {
262 ; CHECK-LABEL: caller4:
264 ; CHECK: mov [[ID:x[0-9]+]], x0
265 ; CHECK: stp {{x[0-9]+}}, {{x[0-9]+}}, [sp]
266 ; CHECK: str {{x[0-9]+}}, [sp, #16]
267 ; CHECK: mov x21, xzr
269 ; CHECK: bl {{.*}}foo_vararg
272 ; Access part of the error object and save it to error_ref
273 ; CHECK: ldrb [[CODE:w[0-9]+]], [x0, #8]
274 ; CHECK: strb [[CODE]], [{{.*}}[[ID]]]
275 ; CHECK: bl {{.*}}free
277 %error_ptr_ref = alloca swifterror %swift_error*
278 store %swift_error* null, %swift_error** %error_ptr_ref
280 %a10 = alloca i32, align 4
281 %a11 = alloca i32, align 4
282 %a12 = alloca i32, align 4
283 store i32 10, i32* %a10, align 4
284 store i32 11, i32* %a11, align 4
285 store i32 12, i32* %a12, align 4
286 %v10 = load i32, i32* %a10, align 4
287 %v11 = load i32, i32* %a11, align 4
288 %v12 = load i32, i32* %a12, align 4
290 %call = call float (%swift_error**, ...) @foo_vararg(%swift_error** swifterror %error_ptr_ref, i32 %v10, i32 %v11, i32 %v12)
291 %error_from_foo = load %swift_error*, %swift_error** %error_ptr_ref
292 %had_error_from_foo = icmp ne %swift_error* %error_from_foo, null
293 %tmp = bitcast %swift_error* %error_from_foo to i8*
294 br i1 %had_error_from_foo, label %handler, label %cont
297 %v1 = getelementptr inbounds %swift_error, %swift_error* %error_from_foo, i64 0, i32 1
298 %t = load i8, i8* %v1
299 store i8 %t, i8* %error_ref
302 call void @free(i8* %tmp)
306 ; Check that we don't blow up on tail calling swifterror argument functions.
307 define float @tailcallswifterror(%swift_error** swifterror %error_ptr_ref) {
309 %0 = tail call float @tailcallswifterror(%swift_error** swifterror %error_ptr_ref)
312 define swiftcc float @tailcallswifterror_swiftcc(%swift_error** swifterror %error_ptr_ref) {
314 %0 = tail call swiftcc float @tailcallswifterror_swiftcc(%swift_error** swifterror %error_ptr_ref)
318 ; CHECK-LABEL: params_in_reg
319 ; Save callee saved registers and swifterror since it will be clobbered by the first call to params_in_reg2.
320 ; CHECK: stp x28, x0, [sp
321 ; CHECK: stp x27, x26, [sp
322 ; CHECK: stp x25, x24, [sp
323 ; CHECK: stp x23, x22, [sp
324 ; CHECK: stp x20, x19, [sp
325 ; CHECK: stp x29, x30, [sp
326 ; Store argument registers.
334 ; CHECK: mov x28, x21
344 ; CHECK: str xzr, [sp]
345 ; CHECK: mov x21, xzr
346 ; CHECK: bl _params_in_reg2
347 ; Restore original arguments for next call.
356 ; Restore original swiftself argument and swifterror %err.
357 ; CHECK: mov x21, x28
358 ; CHECK: bl _params_in_reg2
359 ; Restore calle save registers but don't clober swifterror x21.
361 ; CHECK: ldp x29, x30, [sp
363 ; CHECK: ldp x20, x19, [sp
365 ; CHECK: ldp x23, x22, [sp
367 ; CHECK: ldp x25, x24, [sp
369 ; CHECK: ldp x27, x26, [sp
371 ; CHECK: ldr x28, [sp
374 define swiftcc void @params_in_reg(i64, i64, i64, i64, i64, i64, i64, i64, i8*, %swift_error** nocapture swifterror %err) {
375 %error_ptr_ref = alloca swifterror %swift_error*, align 8
376 store %swift_error* null, %swift_error** %error_ptr_ref
377 call swiftcc void @params_in_reg2(i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i8* null, %swift_error** nocapture swifterror %error_ptr_ref)
378 call swiftcc void @params_in_reg2(i64 %0, i64 %1, i64 %2, i64 %3, i64 %4, i64 %5, i64 %6, i64 %7, i8* %8, %swift_error** nocapture swifterror %err)
381 declare swiftcc void @params_in_reg2(i64, i64, i64, i64, i64, i64, i64, i64, i8* , %swift_error** nocapture swifterror %err)
383 ; CHECK-LABEL: params_and_return_in_reg
384 ; Store callee saved registers.
385 ; CHECK: stp x28, x0, [sp, #16
386 ; CHECK: stp x27, x26, [sp
387 ; CHECK: stp x25, x24, [sp
388 ; CHECK: stp x23, x22, [sp
389 ; CHECK: stp x20, x19, [sp
390 ; CHECK: stp x29, x30, [sp
391 ; Save original arguments.
399 ; CHECK: mov x28, x21
400 ; Setup call arguments.
409 ; CHECK: mov x21, xzr
410 ; CHECK: bl _params_in_reg2
411 ; Store swifterror %error_ptr_ref.
412 ; CHECK: stp {{x[0-9]+}}, x21, [sp]
413 ; Setup call arguments from original arguments.
414 ; CHECK: ldr x0, [sp, #24
422 ; CHECK: mov x21, x28
423 ; CHECK: bl _params_and_return_in_reg2
424 ; Store return values.
433 ; Save swifterror %err.
434 ; CHECK: mov x19, x21
444 ; ... setup call with swiferror %error_ptr_ref.
445 ; CHECK: ldr x21, [sp, #8]
446 ; CHECK: bl _params_in_reg2
447 ; Restore return values for return from this function.
456 ; CHECK: mov x21, x19
457 ; Restore callee save registers.
458 ; CHECK: ldp x29, x30, [sp
459 ; CHECK: ldp x20, x19, [sp
460 ; CHECK: ldp x23, x22, [sp
461 ; CHECK: ldp x25, x24, [sp
462 ; CHECK: ldp x27, x26, [sp
463 ; CHECK: ldr x28, [sp
465 define swiftcc { i64, i64, i64, i64, i64, i64, i64, i64 } @params_and_return_in_reg(i64, i64, i64, i64, i64, i64, i64, i64, i8* , %swift_error** nocapture swifterror %err) {
466 %error_ptr_ref = alloca swifterror %swift_error*, align 8
467 store %swift_error* null, %swift_error** %error_ptr_ref
468 call swiftcc void @params_in_reg2(i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i8* null, %swift_error** nocapture swifterror %error_ptr_ref)
469 %val = call swiftcc { i64, i64, i64, i64, i64, i64, i64, i64 } @params_and_return_in_reg2(i64 %0, i64 %1, i64 %2, i64 %3, i64 %4, i64 %5, i64 %6, i64 %7, i8* %8, %swift_error** nocapture swifterror %err)
470 call swiftcc void @params_in_reg2(i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i8* null, %swift_error** nocapture swifterror %error_ptr_ref)
471 ret { i64, i64, i64, i64, i64, i64, i64, i64 } %val
474 declare swiftcc { i64, i64, i64, i64, i64, i64, i64, i64 } @params_and_return_in_reg2(i64, i64, i64, i64, i64, i64, i64, i64, i8* , %swift_error** nocapture swifterror %err)
476 declare void @acallee(i8*)
478 ; Make sure we don't tail call if the caller returns a swifterror value. We
479 ; would have to move into the swifterror register before the tail call.
480 ; CHECK: tailcall_from_swifterror:
481 ; CHECK-NOT: b _acallee
484 define swiftcc void @tailcall_from_swifterror(%swift_error** swifterror %error_ptr_ref) {
486 tail call void @acallee(i8* null)
490 ; CHECK: tailcall_from_swifterror2
491 ; CHECK-NOT: b _simple_fn
492 ; CHECK: bl _simple_fn
493 declare void @simple_fn()
494 define swiftcc void @tailcall_from_swifterror2(%swift_error** swifterror %error_ptr_ref) {
495 tail call void @simple_fn()
499 declare swiftcc void @foo2(%swift_error** swifterror)
500 ; CHECK-LABEL: testAssign
501 ; CHECK: mov x21, xzr
505 define swiftcc %swift_error* @testAssign(i8* %error_ref) {
507 %error_ptr = alloca swifterror %swift_error*
508 store %swift_error* null, %swift_error** %error_ptr
509 call swiftcc void @foo2(%swift_error** swifterror %error_ptr)
513 %error = load %swift_error*, %swift_error** %error_ptr
514 ret %swift_error* %error