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) {
13 ; 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:
104 ; CHECK: mov [[ID:w[0-9]+]], #1
105 ; 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]
140 %cond = icmp ne i32 %cc, 0
141 br i1 %cond, label %gen_error, label %bb_cont
144 %call = call i8* @malloc(i64 16)
145 %call.0 = bitcast i8* %call to %swift_error*
146 store %swift_error* %call.0, %swift_error** %error_ptr_ref
147 %tmp = getelementptr inbounds i8, i8* %call, i64 8
152 %cmp = fcmp ogt float %cc2, 1.000000e+00
153 br i1 %cmp, label %bb_end, label %bb_loop
158 %struct.S = type { i32, i32, i32, i32, i32, i32 }
160 ; "foo_sret" is a function that takes a swifterror parameter, it also has a sret
162 define void @foo_sret(%struct.S* sret(%struct.S) %agg.result, i32 %val1, %swift_error** swifterror %error_ptr_ref) {
163 ; CHECK-LABEL: foo_sret:
164 ; CHECK: mov [[SRET:x[0-9]+]], x8
167 ; CHECK: mov [[ID:w[0-9]+]], #1
168 ; CHECK: strb [[ID]], [x0, #8]
169 ; CHECK: str w{{.*}}, [{{.*}}[[SRET]], #4]
174 %call = call i8* @malloc(i64 16)
175 %call.0 = bitcast i8* %call to %swift_error*
176 store %swift_error* %call.0, %swift_error** %error_ptr_ref
177 %tmp = getelementptr inbounds i8, i8* %call, i64 8
179 %v2 = getelementptr inbounds %struct.S, %struct.S* %agg.result, i32 0, i32 1
180 store i32 %val1, i32* %v2
184 ; "caller3" calls "foo_sret" that takes a swifterror parameter.
185 define float @caller3(i8* %error_ref) {
186 ; CHECK-LABEL: caller3:
187 ; CHECK: mov [[ID:x[0-9]+]], x0
188 ; CHECK: mov [[ZERO:x[0-9]+]], xzr
189 ; CHECK: bl {{.*}}foo_sret
192 ; Access part of the error object and save it to error_ref
193 ; CHECK: ldrb [[CODE:w[0-9]+]], [x0, #8]
194 ; CHECK: strb [[CODE]], [{{.*}}[[ID]]]
195 ; CHECK: bl {{.*}}free
198 %s = alloca %struct.S, align 8
199 %error_ptr_ref = alloca swifterror %swift_error*
200 store %swift_error* null, %swift_error** %error_ptr_ref
201 call void @foo_sret(%struct.S* sret(%struct.S) %s, i32 1, %swift_error** swifterror %error_ptr_ref)
202 %error_from_foo = load %swift_error*, %swift_error** %error_ptr_ref
203 %had_error_from_foo = icmp ne %swift_error* %error_from_foo, null
204 %tmp = bitcast %swift_error* %error_from_foo to i8*
205 br i1 %had_error_from_foo, label %handler, label %cont
207 %v1 = getelementptr inbounds %swift_error, %swift_error* %error_from_foo, i64 0, i32 1
208 %t = load i8, i8* %v1
209 store i8 %t, i8* %error_ref
212 call void @free(i8* %tmp)
216 ; "foo_vararg" is a function that takes a swifterror parameter, it also has
217 ; variable number of arguments.
218 declare void @llvm.va_start(i8*) nounwind
219 define float @foo_vararg(%swift_error** swifterror %error_ptr_ref, ...) {
220 ; CHECK-LABEL: foo_vararg:
223 ; CHECK-DAG: mov [[ID:w[0-9]+]], #1
224 ; CHECK-DAG: strb [[ID]], [x0, #8]
227 ; CHECK: ldr {{w[0-9]+}}, [x[[ARG1:[0-9]+]]], #8
229 ; CHECK: ldr {{w[0-9]+}}, [x[[ARG1]]], #8
231 ; CHECK: ldr {{w[0-9]+}}, [x[[ARG1]]], #8
236 %call = call i8* @malloc(i64 16)
237 %call.0 = bitcast i8* %call to %swift_error*
238 store %swift_error* %call.0, %swift_error** %error_ptr_ref
239 %tmp = getelementptr inbounds i8, i8* %call, i64 8
242 %args = alloca i8*, align 8
243 %a10 = alloca i32, align 4
244 %a11 = alloca i32, align 4
245 %a12 = alloca i32, align 4
246 %v10 = bitcast i8** %args to i8*
247 call void @llvm.va_start(i8* %v10)
248 %v11 = va_arg i8** %args, i32
249 store i32 %v11, i32* %a10, align 4
250 %v12 = va_arg i8** %args, i32
251 store i32 %v12, i32* %a11, align 4
252 %v13 = va_arg i8** %args, i32
253 store i32 %v13, i32* %a12, align 4
258 ; "caller4" calls "foo_vararg" that takes a swifterror parameter.
259 define float @caller4(i8* %error_ref) {
260 ; CHECK-LABEL: caller4:
262 ; CHECK: mov [[ID:x[0-9]+]], x0
263 ; CHECK: stp {{x[0-9]+}}, {{x[0-9]+}}, [sp]
264 ; CHECK: str {{x[0-9]+}}, [sp, #16]
265 ; CHECK: mov x21, xzr
267 ; CHECK: bl {{.*}}foo_vararg
270 ; Access part of the error object and save it to error_ref
271 ; CHECK: ldrb [[CODE:w[0-9]+]], [x0, #8]
272 ; CHECK: strb [[CODE]], [{{.*}}[[ID]]]
273 ; CHECK: bl {{.*}}free
275 %error_ptr_ref = alloca swifterror %swift_error*
276 store %swift_error* null, %swift_error** %error_ptr_ref
278 %a10 = alloca i32, align 4
279 %a11 = alloca i32, align 4
280 %a12 = alloca i32, align 4
281 store i32 10, i32* %a10, align 4
282 store i32 11, i32* %a11, align 4
283 store i32 12, i32* %a12, align 4
284 %v10 = load i32, i32* %a10, align 4
285 %v11 = load i32, i32* %a11, align 4
286 %v12 = load i32, i32* %a12, align 4
288 %call = call float (%swift_error**, ...) @foo_vararg(%swift_error** swifterror %error_ptr_ref, i32 %v10, i32 %v11, i32 %v12)
289 %error_from_foo = load %swift_error*, %swift_error** %error_ptr_ref
290 %had_error_from_foo = icmp ne %swift_error* %error_from_foo, null
291 %tmp = bitcast %swift_error* %error_from_foo to i8*
292 br i1 %had_error_from_foo, label %handler, label %cont
295 %v1 = getelementptr inbounds %swift_error, %swift_error* %error_from_foo, i64 0, i32 1
296 %t = load i8, i8* %v1
297 store i8 %t, i8* %error_ref
300 call void @free(i8* %tmp)
304 ; Check that we don't blow up on tail calling swifterror argument functions.
305 define float @tailcallswifterror(%swift_error** swifterror %error_ptr_ref) {
307 %0 = tail call float @tailcallswifterror(%swift_error** swifterror %error_ptr_ref)
310 define swiftcc float @tailcallswifterror_swiftcc(%swift_error** swifterror %error_ptr_ref) {
312 %0 = tail call swiftcc float @tailcallswifterror_swiftcc(%swift_error** swifterror %error_ptr_ref)
316 ; CHECK-LABEL: params_in_reg
317 ; Save callee saved registers and swifterror since it will be clobbered by the first call to params_in_reg2.
318 ; CHECK: stp x28, x0, [sp
319 ; CHECK: stp x27, x26, [sp
320 ; CHECK: stp x25, x24, [sp
321 ; CHECK: stp x23, x22, [sp
322 ; CHECK: stp x20, x19, [sp
323 ; CHECK: stp x29, x30, [sp
324 ; Store argument registers.
332 ; CHECK: mov x28, x21
342 ; CHECK: str xzr, [sp]
343 ; CHECK: mov x21, xzr
344 ; CHECK: bl _params_in_reg2
345 ; Restore original arguments for next call.
354 ; Restore original swiftself argument and swifterror %err.
355 ; CHECK: mov x21, x28
356 ; CHECK: bl _params_in_reg2
357 ; Restore calle save registers but don't clober swifterror x21.
359 ; CHECK: ldp x29, x30, [sp
361 ; CHECK: ldp x20, x19, [sp
363 ; CHECK: ldp x23, x22, [sp
365 ; CHECK: ldp x25, x24, [sp
367 ; CHECK: ldp x27, x26, [sp
369 ; CHECK: ldr x28, [sp
372 define swiftcc void @params_in_reg(i64, i64, i64, i64, i64, i64, i64, i64, i8*, %swift_error** nocapture swifterror %err) {
373 %error_ptr_ref = alloca swifterror %swift_error*, align 8
374 store %swift_error* null, %swift_error** %error_ptr_ref
375 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)
376 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)
379 declare swiftcc void @params_in_reg2(i64, i64, i64, i64, i64, i64, i64, i64, i8* , %swift_error** nocapture swifterror %err)
381 ; CHECK-LABEL: params_and_return_in_reg
382 ; Store callee saved registers.
383 ; CHECK: stp x28, x0, [sp, #16
384 ; CHECK: stp x27, x26, [sp
385 ; CHECK: stp x25, x24, [sp
386 ; CHECK: stp x23, x22, [sp
387 ; CHECK: stp x20, x19, [sp
388 ; CHECK: stp x29, x30, [sp
389 ; Save original arguments.
397 ; CHECK: mov x28, x21
398 ; Setup call arguments.
407 ; CHECK: mov x21, xzr
408 ; CHECK: bl _params_in_reg2
409 ; Store swifterror %error_ptr_ref.
410 ; CHECK: stp {{x[0-9]+}}, x21, [sp]
411 ; Setup call arguments from original arguments.
412 ; CHECK: ldr x0, [sp, #24
420 ; CHECK: mov x21, x28
421 ; CHECK: bl _params_and_return_in_reg2
422 ; Store return values.
431 ; Save swifterror %err.
432 ; CHECK: mov x19, x21
442 ; ... setup call with swiferror %error_ptr_ref.
443 ; CHECK: ldr x21, [sp, #8]
444 ; CHECK: bl _params_in_reg2
445 ; Restore return values for return from this function.
454 ; CHECK: mov x21, x19
455 ; Restore callee save registers.
456 ; CHECK: ldp x29, x30, [sp
457 ; CHECK: ldp x20, x19, [sp
458 ; CHECK: ldp x23, x22, [sp
459 ; CHECK: ldp x25, x24, [sp
460 ; CHECK: ldp x27, x26, [sp
461 ; CHECK: ldr x28, [sp
463 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) {
464 %error_ptr_ref = alloca swifterror %swift_error*, align 8
465 store %swift_error* null, %swift_error** %error_ptr_ref
466 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)
467 %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)
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 ret { i64, i64, i64, i64, i64, i64, i64, i64 } %val
472 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)
474 declare void @acallee(i8*)
476 ; Make sure we don't tail call if the caller returns a swifterror value. We
477 ; would have to move into the swifterror register before the tail call.
478 ; CHECK: tailcall_from_swifterror:
479 ; CHECK-NOT: b _acallee
482 define swiftcc void @tailcall_from_swifterror(%swift_error** swifterror %error_ptr_ref) {
484 tail call void @acallee(i8* null)
488 ; CHECK: tailcall_from_swifterror2
489 ; CHECK-NOT: b _simple_fn
490 ; CHECK: bl _simple_fn
491 declare void @simple_fn()
492 define swiftcc void @tailcall_from_swifterror2(%swift_error** swifterror %error_ptr_ref) {
493 tail call void @simple_fn()
497 declare swiftcc void @foo2(%swift_error** swifterror)
498 ; CHECK-LABEL: testAssign
499 ; CHECK: mov x21, xzr
503 define swiftcc %swift_error* @testAssign(i8* %error_ref) {
505 %error_ptr = alloca swifterror %swift_error*
506 store %swift_error* null, %swift_error** %error_ptr
507 call swiftcc void @foo2(%swift_error** swifterror %error_ptr)
511 %error = load %swift_error*, %swift_error** %error_ptr
512 ret %swift_error* %error
515 ; foo takes a swifterror parameter. We should be able to see that even when
516 ; it isn't explicitly on the call.
517 define float @swifterror_param_not_on_call(i8* %error_ref) {
518 ; CHECK-LABEL: swifterror_param_not_on_call:
519 ; CHECK: mov [[ID:x[0-9]+]], x0
520 ; CHECK: bl {{.*}}foo
523 ; Access part of the error object and save it to error_ref
524 ; CHECK: ldrb [[CODE:w[0-9]+]], [x0, #8]
525 ; CHECK: strb [[CODE]], [{{.*}}[[ID]]]
526 ; CHECK: bl {{.*}}free
529 %error_ptr_ref = alloca swifterror %swift_error*
530 store %swift_error* null, %swift_error** %error_ptr_ref
531 %call = call float @foo(%swift_error** %error_ptr_ref)
532 %error_from_foo = load %swift_error*, %swift_error** %error_ptr_ref
533 %had_error_from_foo = icmp ne %swift_error* %error_from_foo, null
534 %tmp = bitcast %swift_error* %error_from_foo to i8*
535 br i1 %had_error_from_foo, label %handler, label %cont
537 %v1 = getelementptr inbounds %swift_error, %swift_error* %error_from_foo, i64 0, i32 1
538 %t = load i8, i8* %v1
539 store i8 %t, i8* %error_ref
542 call void @free(i8* %tmp)
546 ; foo_sret takes an sret parameter and a swifterror parameter. We should be
547 ; able to see that, even if it's not explicitly on the call.
548 define float @swifterror_param_not_on_call2(i8* %error_ref) {
549 ; CHECK-LABEL: swifterror_param_not_on_call2:
550 ; CHECK: mov [[ID:x[0-9]+]], x0
551 ; CHECK: mov [[ZERO:x[0-9]+]], xzr
552 ; CHECK: bl {{.*}}foo_sret
555 ; Access part of the error object and save it to error_ref
556 ; CHECK: ldrb [[CODE:w[0-9]+]], [x0, #8]
557 ; CHECK: strb [[CODE]], [{{.*}}[[ID]]]
558 ; CHECK: bl {{.*}}free
561 %s = alloca %struct.S, align 8
562 %error_ptr_ref = alloca swifterror %swift_error*
563 store %swift_error* null, %swift_error** %error_ptr_ref
564 call void @foo_sret(%struct.S* %s, i32 1, %swift_error** %error_ptr_ref)
565 %error_from_foo = load %swift_error*, %swift_error** %error_ptr_ref
566 %had_error_from_foo = icmp ne %swift_error* %error_from_foo, null
567 %tmp = bitcast %swift_error* %error_from_foo to i8*
568 br i1 %had_error_from_foo, label %handler, label %cont
570 %v1 = getelementptr inbounds %swift_error, %swift_error* %error_from_foo, i64 0, i32 1
571 %t = load i8, i8* %v1
572 store i8 %t, i8* %error_ref
575 call void @free(i8* %tmp)