1 ; RUN: llc -mtriple arm64-windows -o - %s | FileCheck %s
5 ; void foo(struct S o);
10 ; __finally { foo(o.x); }
12 ; void stack_realign() {
13 ; struct S __declspec(align(32)) o;
16 ; __finally { foo(o.x); }
18 ; void vla_present(int n) {
22 ; __finally { foo(n); }
24 ; void vla_and_realign(int n) {
25 ; struct S __declspec(align(32)) o;
29 ; __finally { foo(o.x); }
32 %struct.S = type { i32 }
34 ; Test simple SEH (__try/__finally).
35 define void @simple_seh() #0 personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*) {
37 ; CHECK-LABEL: simple_seh
38 ; CHECK: add x29, sp, #16
40 ; CHECK: stur x0, [x29, #-16]
41 ; CHECK: .set .Lsimple_seh$frame_escape_0, -8
42 ; CHECK: ldur w0, [x29, #-8]
45 %o = alloca %struct.S, align 4
46 call void (...) @llvm.localescape(%struct.S* %o)
47 %x = getelementptr inbounds %struct.S, %struct.S* %o, i32 0, i32 0
48 %0 = load i32, i32* %x, align 4
49 invoke void @foo(i32 %0) #5
50 to label %invoke.cont unwind label %ehcleanup
52 invoke.cont: ; preds = %entry
53 %1 = call i8* @llvm.localaddress()
54 call void @fin_simple_seh(i8 0, i8* %1)
57 ehcleanup: ; preds = %entry
58 %2 = cleanuppad within none []
59 %3 = call i8* @llvm.localaddress()
60 call void @fin_simple_seh(i8 1, i8* %3) [ "funclet"(token %2) ]
61 cleanupret from %2 unwind to caller
64 define void @fin_simple_seh(i8 %abnormal_termination, i8* %frame_pointer) {
66 ; CHECK-LABEL: fin_simple_seh
67 ; CHECK: movz x8, #:abs_g1_s:.Lsimple_seh$frame_escape_0
68 ; CHECK: movk x8, #:abs_g0_nc:.Lsimple_seh$frame_escape_0
69 ; CHECK: strb w0, [sp, #15]
70 ; CHECK: ldr w0, [x1, x8]
73 %frame_pointer.addr = alloca i8*, align 8
74 %abnormal_termination.addr = alloca i8, align 1
75 %0 = call i8* @llvm.localrecover(i8* bitcast (void ()* @simple_seh to i8*), i8* %frame_pointer, i32 0)
76 %o = bitcast i8* %0 to %struct.S*
77 store i8* %frame_pointer, i8** %frame_pointer.addr, align 8
78 store i8 %abnormal_termination, i8* %abnormal_termination.addr, align 1
79 %x = getelementptr inbounds %struct.S, %struct.S* %o, i32 0, i32 0
80 %1 = load i32, i32* %x, align 4
81 call void @foo(i32 %1)
85 ; Test SEH when stack realignment is needed in case highly aligned stack objects are present.
86 define void @stack_realign() #0 personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*) {
88 ; CHECK-LABEL: stack_realign
90 ; CHECK: sub x9, sp, #64
91 ; CHECK: and sp, x9, #0xffffffffffffffe0
94 ; CHECK: stur x0, [x19, #16]
95 ; CHECK: .set .Lstack_realign$frame_escape_0, 32
96 ; CHECK: ldr w0, [x19, #32]
99 %o = alloca %struct.S, align 32
100 call void (...) @llvm.localescape(%struct.S* %o)
101 %x = getelementptr inbounds %struct.S, %struct.S* %o, i32 0, i32 0
102 %0 = load i32, i32* %x, align 32
103 invoke void @foo(i32 %0) #5
104 to label %invoke.cont unwind label %ehcleanup
106 invoke.cont: ; preds = %entry
107 %1 = call i8* @llvm.localaddress()
108 call void @fin_stack_realign(i8 0, i8* %1)
111 ehcleanup: ; preds = %entry
112 %2 = cleanuppad within none []
113 %3 = call i8* @llvm.localaddress()
114 call void @fin_stack_realign(i8 1, i8* %3) [ "funclet"(token %2) ]
115 cleanupret from %2 unwind to caller
118 define void @fin_stack_realign(i8 %abnormal_termination, i8* %frame_pointer) {
120 ; CHECK-LABEL: fin_stack_realign
121 ; CHECK: movz x8, #:abs_g1_s:.Lstack_realign$frame_escape_0
122 ; CHECK: movk x8, #:abs_g0_nc:.Lstack_realign$frame_escape_0
123 ; CHECK: strb w0, [sp, #15]
124 ; CHECK: ldr w0, [x1, x8]
127 %frame_pointer.addr = alloca i8*, align 8
128 %abnormal_termination.addr = alloca i8, align 1
129 %0 = call i8* @llvm.localrecover(i8* bitcast (void ()* @stack_realign to i8*), i8* %frame_pointer, i32 0)
130 %o = bitcast i8* %0 to %struct.S*
131 store i8* %frame_pointer, i8** %frame_pointer.addr, align 8
132 store i8 %abnormal_termination, i8* %abnormal_termination.addr, align 1
133 %x = getelementptr inbounds %struct.S, %struct.S* %o, i32 0, i32 0
134 %1 = load i32, i32* %x, align 32
135 call void @foo(i32 %1)
139 ; Test SEH when variable size objects are present on the stack. Note: Escaped vla's are current not supported by SEH.
140 define void @vla_present(i32 %n) #0 personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*) {
142 ; CHECK-LABEL: vla_present
143 ; CHECK: add x29, sp, #32
145 ; CHECK: stur x1, [x29, #-32]
146 ; CHECK: .set .Lvla_present$frame_escape_0, -4
147 ; CHECK: stur w0, [x29, #-4]
148 ; CHECK: ldur w8, [x29, #-4]
150 ; CHECK: stur x9, [x29, #-16]
151 ; CHECK: stur x8, [x29, #-24]
152 ; CHECK: ldur w0, [x29, #-4]
155 %n.addr = alloca i32, align 4
156 %saved_stack = alloca i8*, align 8
157 %__vla_expr0 = alloca i64, align 8
158 call void (...) @llvm.localescape(i32* %n.addr)
159 store i32 %n, i32* %n.addr, align 4
160 %0 = load i32, i32* %n.addr, align 4
161 %1 = zext i32 %0 to i64
162 %2 = call i8* @llvm.stacksave()
163 store i8* %2, i8** %saved_stack, align 8
164 %vla = alloca i32, i64 %1, align 4
165 store i64 %1, i64* %__vla_expr0, align 8
166 %3 = load i32, i32* %n.addr, align 4
167 invoke void @foo(i32 %3) #5
168 to label %invoke.cont unwind label %ehcleanup
170 invoke.cont: ; preds = %entry
171 %4 = call i8* @llvm.localaddress()
172 call void @fin_vla_present(i8 0, i8* %4)
173 %5 = load i8*, i8** %saved_stack, align 8
174 call void @llvm.stackrestore(i8* %5)
177 ehcleanup: ; preds = %entry
178 %6 = cleanuppad within none []
179 %7 = call i8* @llvm.localaddress()
180 call void @fin_vla_present(i8 1, i8* %7) [ "funclet"(token %6) ]
181 cleanupret from %6 unwind to caller
184 define void @fin_vla_present(i8 %abnormal_termination, i8* %frame_pointer) {
186 ; CHECK-LABEL: fin_vla_present
187 ; CHECK: movz x8, #:abs_g1_s:.Lvla_present$frame_escape_0
188 ; CHECK: movk x8, #:abs_g0_nc:.Lvla_present$frame_escape_0
189 ; CHECK: strb w0, [sp, #15]
190 ; CHECK: ldr w0, [x1, x8]
193 %frame_pointer.addr = alloca i8*, align 8
194 %abnormal_termination.addr = alloca i8, align 1
195 %0 = call i8* @llvm.localrecover(i8* bitcast (void (i32)* @vla_present to i8*), i8* %frame_pointer, i32 0)
196 %n.addr = bitcast i8* %0 to i32*
197 store i8* %frame_pointer, i8** %frame_pointer.addr, align 8
198 store i8 %abnormal_termination, i8* %abnormal_termination.addr, align 1
199 %1 = load i32, i32* %n.addr, align 4
200 call void @foo(i32 %1)
204 ; Test when both vla's and highly aligned objects are present on stack.
205 define void @vla_and_realign(i32 %n) #0 personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*) {
207 ; CHECK-LABEL: vla_and_realign
209 ; CHECK: sub x9, sp, #64
210 ; CHECK: and sp, x9, #0xffffffffffffffe0
213 ; CHECK: stur x1, [x19]
214 ; CHECK: .set .Lvla_and_realign$frame_escape_0, 32
215 ; CHECK: str w0, [x29, #28]
216 ; CHECK: ldr w8, [x29, #28]
218 ; CHECK: str x9, [x19, #24]
219 ; CHECK: str x8, [x19, #16]
220 ; CHECK: ldr w0, [x19, #32]
223 %n.addr = alloca i32, align 4
224 %o = alloca %struct.S, align 32
225 %saved_stack = alloca i8*, align 8
226 %__vla_expr0 = alloca i64, align 8
227 call void (...) @llvm.localescape(%struct.S* %o)
228 store i32 %n, i32* %n.addr, align 4
229 %0 = load i32, i32* %n.addr, align 4
230 %1 = zext i32 %0 to i64
231 %2 = call i8* @llvm.stacksave()
232 store i8* %2, i8** %saved_stack, align 8
233 %vla = alloca i32, i64 %1, align 4
234 store i64 %1, i64* %__vla_expr0, align 8
235 %x = getelementptr inbounds %struct.S, %struct.S* %o, i32 0, i32 0
236 %3 = load i32, i32* %x, align 32
237 invoke void @foo(i32 %3) #5
238 to label %invoke.cont unwind label %ehcleanup
240 invoke.cont: ; preds = %entry
241 %4 = call i8* @llvm.localaddress()
242 call void @fin_vla_and_realign(i8 0, i8* %4)
243 %5 = load i8*, i8** %saved_stack, align 8
244 call void @llvm.stackrestore(i8* %5)
247 ehcleanup: ; preds = %entry
248 %6 = cleanuppad within none []
249 %7 = call i8* @llvm.localaddress()
250 call void @fin_vla_and_realign(i8 1, i8* %7) [ "funclet"(token %6) ]
251 cleanupret from %6 unwind to caller
254 define void @fin_vla_and_realign(i8 %abnormal_termination, i8* %frame_pointer) {
256 ; CHECK-LABEL: fin_vla_and_realign
257 ; CHECK: movz x8, #:abs_g1_s:.Lvla_and_realign$frame_escape_0
258 ; CHECK: movk x8, #:abs_g0_nc:.Lvla_and_realign$frame_escape_0
259 ; CHECK: strb w0, [sp, #15]
260 ; CHECK: ldr w0, [x1, x8]
263 %frame_pointer.addr = alloca i8*, align 8
264 %abnormal_termination.addr = alloca i8, align 1
265 %0 = call i8* @llvm.localrecover(i8* bitcast (void (i32)* @vla_and_realign to i8*), i8* %frame_pointer, i32 0)
266 %o = bitcast i8* %0 to %struct.S*
267 store i8* %frame_pointer, i8** %frame_pointer.addr, align 8
268 store i8 %abnormal_termination, i8* %abnormal_termination.addr, align 1
269 %x = getelementptr inbounds %struct.S, %struct.S* %o, i32 0, i32 0
270 %1 = load i32, i32* %x, align 32
271 call void @foo(i32 %1)
275 declare void @foo(i32)
276 declare void @llvm.stackrestore(i8*)
277 declare i8* @llvm.stacksave()
278 declare i8* @llvm.localrecover(i8*, i8*, i32)
279 declare i8* @llvm.localaddress()
280 declare void @llvm.localescape(...)
281 declare i32 @__C_specific_handler(...)
283 attributes #0 = { noinline optnone }