frame-access.ll
13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
; RUN: llc -mtriple=thumbv6m-eabi -frame-pointer=none %s -o - | FileCheck %s
; struct S { int x[128]; } s;
; int f(int *, int, int, int, struct S);
; int g(int *, int, int, int, int, int);
; int h(int *, int *, int *);
; int u(int *, int *, int *, struct S, struct S);
%struct.S = type { [128 x i32] }
%struct.__va_list = type { i8* }
@s = common dso_local global %struct.S zeroinitializer, align 4
declare void @llvm.va_start(i8*)
declare dso_local i32 @g(i32*, i32, i32, i32, i32, i32) local_unnamed_addr
declare dso_local i32 @f(i32*, i32, i32, i32, %struct.S* byval align 4) local_unnamed_addr
declare dso_local i32 @h(i32*, i32*, i32*) local_unnamed_addr
declare dso_local i32 @u(i32*, i32*, i32*, %struct.S* byval align 4, %struct.S* byval align 4) local_unnamed_addr
;
; Test access to arguments, passed on stack (including varargs)
;
; Usual case, access via SP
; int test_args_sp(int a, int b, int c, int d, int e) {
; int v[4];
; return g(v, a, b, c, d, e);
; }
define dso_local i32 @test_args_sp(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) local_unnamed_addr {
entry:
%v = alloca [4 x i32], align 4
%0 = bitcast [4 x i32]* %v to i8*
%arraydecay = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 0
%call = call i32 @g(i32* nonnull %arraydecay, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e)
ret i32 %call
}
; CHECK-LABEL: test_args_sp
; Load `e`
; CHECK: ldr r0, [sp, #32]
; CHECK-NEXT: str r3, [sp]
; Pass `e` on stack
; CHECK-NEXT: str r0, [sp, #4]
; CHECK: bl g
; int test_varargs_sp(int a, ...) {
; int v[4];
; __builtin_va_list ap;
; __builtin_va_start(ap, a);
; return g(v, a, 0, 0, 0, 0);
; }
define dso_local i32 @test_varargs_sp(i32 %a, ...) local_unnamed_addr {
entry:
%v = alloca [4 x i32], align 4
%ap = alloca %struct.__va_list, align 4
%0 = bitcast [4 x i32]* %v to i8*
%1 = bitcast %struct.__va_list* %ap to i8*
call void @llvm.va_start(i8* nonnull %1)
%arraydecay = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 0
%call = call i32 @g(i32* nonnull %arraydecay, i32 %a, i32 0, i32 0, i32 0, i32 0)
ret i32 %call
}
; CHECK-LABEL: test_varargs_sp
; Three incoming varargs in registers
; CHECK: sub sp, #12
; CHECK: sub sp, #28
; Incoming arguments area is accessed via SP
; CHECK: add r0, sp, #36
; CHECK: stm r0!, {r1, r2, r3}
; Re-aligned stack, access via FP
; int test_args_realign(int a, int b, int c, int d, int e) {
; __attribute__((aligned(16))) int v[4];
; return g(v, a, b, c, d, e);
; }
; Function Attrs: nounwind
define dso_local i32 @test_args_realign(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) local_unnamed_addr {
entry:
%v = alloca [4 x i32], align 16
%0 = bitcast [4 x i32]* %v to i8*
%arraydecay = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 0
%call = call i32 @g(i32* nonnull %arraydecay, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e)
ret i32 %call
}
; CHECK-LABEL: test_args_realign
; Setup frame pointer
; CHECK: add r7, sp, #8
; Align stack
; CHECK: mov r4, sp
; CHECK-NEXT: lsrs r4, r4, #4
; CHECK-NEXT: lsls r4, r4, #4
; CHECK-NEXT: mov sp, r4
; Load `e` via FP
; CHECK: ldr r0, [r7, #8]
; CHECK-NEXT: str r3, [sp]
; Pass `e` as argument
; CHECK-NEXT: str r0, [sp, #4]
; CHECK: bl g
; int test_varargs_realign(int a, ...) {
; __attribute__((aligned(16))) int v[4];
; __builtin_va_list ap;
; __builtin_va_start(ap, a);
; return g(v, a, 0, 0, 0, 0);
; }
define dso_local i32 @test_varargs_realign(i32 %a, ...) local_unnamed_addr {
entry:
%v = alloca [4 x i32], align 16
%ap = alloca %struct.__va_list, align 4
%0 = bitcast [4 x i32]* %v to i8*
%1 = bitcast %struct.__va_list* %ap to i8*
call void @llvm.va_start(i8* nonnull %1)
%arraydecay = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 0
%call = call i32 @g(i32* nonnull %arraydecay, i32 %a, i32 0, i32 0, i32 0, i32 0)
ret i32 %call
}
; CHECK-LABEL: test_varargs_realign
; Three incoming register varargs
; CHECK: sub sp, #12
; Setup frame pointer
; CHECK: add r7, sp, #8
; Align stack
; CHECK: mov r4, sp
; CHECK-NEXT: lsrs r4, r4, #4
; CHECK-NEXT: lsls r4, r4, #4
; CHECK-NEXT: mov sp, r4
; Incoming register varargs stored via FP
; CHECK: mov r0, r7
; CHECK-NEXT: adds r0, #8
; CHECK-NEXT: stm r0!, {r1, r2, r3}
; VLAs present, access via FP
; int test_args_vla(int a, int b, int c, int d, int e) {
; int v[a];
; return g(v, a, b, c, d, e);
; }
define dso_local i32 @test_args_vla(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) local_unnamed_addr {
entry:
%vla = alloca i32, i32 %a, align 4
%call = call i32 @g(i32* nonnull %vla, i32 %a, i32 %b, i32 %c, i32 %d, i32 %e)
ret i32 %call
}
; CHECK-LABEL: test_args_vla
; Setup frame pointer
; CHECK: add r7, sp, #12
; Allocate outgoing stack arguments space
; CHECK: sub sp, #4
; Load `e` via FP
; CHECK: ldr r5, [r7, #8]
; Pass `d` and `e` as arguments
; CHECK-NEXT: str r3, [sp]
; CHECK-NEXT: str r5, [sp, #4]
; CHECK: bl g
; int test_varargs_vla(int a, ...) {
; int v[a];
; __builtin_va_list ap;
; __builtin_va_start(ap, a);
; return g(v, a, 0, 0, 0, 0);
; }
define dso_local i32 @test_varargs_vla(i32 %a, ...) local_unnamed_addr {
entry:
%ap = alloca %struct.__va_list, align 4
%vla = alloca i32, i32 %a, align 4
%0 = bitcast %struct.__va_list* %ap to i8*
call void @llvm.va_start(i8* nonnull %0)
%call = call i32 @g(i32* nonnull %vla, i32 %a, i32 0, i32 0, i32 0, i32 0)
ret i32 %call
}
; CHECK-LABEL: test_varargs_vla
; Three incoming register varargs
; CHECK: sub sp, #12
; Setup frame pointer
; CHECK: add r7, sp, #8
; Register varargs stored via FP
; CHECK-DAG: str r3, [r7, #16]
; CHECK-DAG: str r2, [r7, #12]
; CHECK-DAG: str r1, [r7, #8]
; Moving SP, access via SP
; int test_args_moving_sp(int a, int b, int c, int d, int e) {
; int v[4];
; return f(v, a, b + c + d, e, s) + h(v, v+1, v+2);
; }
define dso_local i32 @test_args_moving_sp(i32 %a, i32 %b, i32 %c, i32 %d, i32 %e) local_unnamed_addr {
entry:
%v = alloca [4 x i32], align 4
%0 = bitcast [4 x i32]* %v to i8*
%arraydecay = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 0
%add = add nsw i32 %c, %b
%add1 = add nsw i32 %add, %d
%call = call i32 @f(i32* nonnull %arraydecay, i32 %a, i32 %add1, i32 %e, %struct.S* byval nonnull align 4 @s)
%add.ptr = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 1
%add.ptr5 = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 2
%call6 = call i32 @h(i32* nonnull %arraydecay, i32* nonnull %add.ptr, i32* nonnull %add.ptr5)
%add7 = add nsw i32 %call6, %call
ret i32 %add7
}
; CHECK-LABEL: test_args_moving_sp
; 20 bytes callee-saved area
; CHECK: push {r4, r5, r6, r7, lr}
; 20 bytes locals
; CHECK: sub sp, #20
; Setup base pointer
; CHECK: mov r6, sp
; Allocate outgoing arguments space
; CHECK: sub sp, #508
; CHECK: sub sp, #4
; Load `e` via BP, 40 = 20 + 20
; CHECK: ldr r3, [r6, #40]
; CHECK: bl f
; Stack restored before next call
; CHECK-NEXT: add sp, #508
; CHECK-NEXT: add sp, #4
; CHECK: bl h
; int test_varargs_moving_sp(int a, ...) {
; int v[4];
; __builtin_va_list ap;
; __builtin_va_start(ap, a);
; return f(v, a, 0, 0, s) + h(v, v+1, v+2);
; }
define dso_local i32 @test_varargs_moving_sp(i32 %a, ...) local_unnamed_addr {
entry:
%v = alloca [4 x i32], align 4
%ap = alloca %struct.__va_list, align 4
%0 = bitcast [4 x i32]* %v to i8*
%1 = bitcast %struct.__va_list* %ap to i8*
call void @llvm.va_start(i8* nonnull %1)
%arraydecay = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 0
%call = call i32 @f(i32* nonnull %arraydecay, i32 %a, i32 0, i32 0, %struct.S* byval nonnull align 4 @s)
%add.ptr = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 1
%add.ptr5 = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 2
%call6 = call i32 @h(i32* nonnull %arraydecay, i32* nonnull %add.ptr, i32* nonnull %add.ptr5)
%add = add nsw i32 %call6, %call
ret i32 %add
}
; CHECK-LABEL: test_varargs_moving_sp
; Three incoming register varargs
; CHECK: sub sp, #12
; 16 bytes callee-saves
; CHECK: push {r4, r5, r6, lr}
; 20 bytes locals
; CHECK: sub sp, #20
; Incoming varargs stored via BP, 36 = 20 + 16
; CHECK: mov r0, r6
; CHECK-NEXT: adds r0, #36
; CHECK-NEXT: stm r0!, {r1, r2, r3}
;
; Access to locals
;
; Usual case, access via SP.
; int test_local(int n) {
; int v[4];
; int x, y, z;
; h(&x, &y, &z);
; return g(v, x, y, z, 0, 0);
; }
define dso_local i32 @test_local(i32 %n) local_unnamed_addr {
entry:
%v = alloca [4 x i32], align 4
%x = alloca i32, align 4
%y = alloca i32, align 4
%z = alloca i32, align 4
%0 = bitcast [4 x i32]* %v to i8*
%1 = bitcast i32* %x to i8*
%2 = bitcast i32* %y to i8*
%3 = bitcast i32* %z to i8*
%call = call i32 @h(i32* nonnull %x, i32* nonnull %y, i32* nonnull %z)
%arraydecay = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 0
%4 = load i32, i32* %x, align 4
%5 = load i32, i32* %y, align 4
%6 = load i32, i32* %z, align 4
%call1 = call i32 @g(i32* nonnull %arraydecay, i32 %4, i32 %5, i32 %6, i32 0, i32 0)
ret i32 %call1
}
; CHECK-LABEL: test_local
; Arguments to `h` relative to SP
; CHECK: add r0, sp, #20
; CHECK-NEXT: add r1, sp, #16
; CHECK-NEXT: add r2, sp, #12
; CHECK-NEXT: bl h
; Load `x`, `y`, and `z` via SP
; CHECK: ldr r1, [sp, #20]
; CHECK-NEXT: ldr r2, [sp, #16]
; CHECK-NEXT: ldr r3, [sp, #12]
; CHECK: bl g
; Re-aligned stack, access via SP.
; int test_local_realign(int n) {
; __attribute__((aligned(16))) int v[4];
; int x, y, z;
; h(&x, &y, &z);
; return g(v, x, y, z, 0, 0);
; }
define dso_local i32 @test_local_realign(i32 %n) local_unnamed_addr {
entry:
%v = alloca [4 x i32], align 16
%x = alloca i32, align 4
%y = alloca i32, align 4
%z = alloca i32, align 4
%0 = bitcast [4 x i32]* %v to i8*
%1 = bitcast i32* %x to i8*
%2 = bitcast i32* %y to i8*
%3 = bitcast i32* %z to i8*
%call = call i32 @h(i32* nonnull %x, i32* nonnull %y, i32* nonnull %z)
%arraydecay = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 0
%4 = load i32, i32* %x, align 4
%5 = load i32, i32* %y, align 4
%6 = load i32, i32* %z, align 4
%call1 = call i32 @g(i32* nonnull %arraydecay, i32 %4, i32 %5, i32 %6, i32 0, i32 0)
ret i32 %call1
}
; CHECK-LABEL: test_local_realign
; Setup frame pointer
; CHECK: add r7, sp, #8
; Re-align stack
; CHECK: mov r4, sp
; CHECK-NEXT: lsrs r4, r4, #4
; CHECK-NEXT: lsls r4, r4, #4
; CHECK-NEXT: mov sp, r4
; Arguments to `h` computed relative to SP
; CHECK: add r0, sp, #28
; CHECK-NEXT: add r1, sp, #24
; CHECK-NEXT: add r2, sp, #20
; CHECK-NEXT: bl h
; Load `x`, `y`, and `z` via SP for passing to `g`
; CHECK: ldr r1, [sp, #28]
; CHECK-NEXT: ldr r2, [sp, #24]
; CHECK-NEXT: ldr r3, [sp, #20]
; CHECK: bl g
; VLAs, access via BP.
; int test_local_vla(int n) {
; int v[n];
; int x, y, z;
; h(&x, &y, &z);
; return g(v, x, y, z, 0, 0);
; }
define dso_local i32 @test_local_vla(i32 %n) local_unnamed_addr {
entry:
%x = alloca i32, align 4
%y = alloca i32, align 4
%z = alloca i32, align 4
%vla = alloca i32, i32 %n, align 4
%0 = bitcast i32* %x to i8*
%1 = bitcast i32* %y to i8*
%2 = bitcast i32* %z to i8*
%call = call i32 @h(i32* nonnull %x, i32* nonnull %y, i32* nonnull %z)
%3 = load i32, i32* %x, align 4
%4 = load i32, i32* %y, align 4
%5 = load i32, i32* %z, align 4
%call1 = call i32 @g(i32* nonnull %vla, i32 %3, i32 %4, i32 %5, i32 0, i32 0)
ret i32 %call1
}
; CHECK-LABEL: test_local_vla
; Setup frame pointer
; CHECK: add r7, sp, #12
; Setup base pointer
; CHECK: mov r6, sp
; CHECK: mov r5, r6
; Arguments to `h` compute relative to BP
; CHECK: adds r0, r6, #7
; CHECK-NEXT: adds r0, #1
; CHECK-NEXT: adds r1, r6, #4
; CHECK-NEXT: mov r2, r6
; CHECK-NEXT: bl h
; Load `x`, `y`, `z` via BP (r5 should still have the value of r6 from the move
; above)
; CHECK: ldr r3, [r5]
; CHECK-NEXT: ldr r2, [r5, #4]
; CHECK-NEXT: ldr r1, [r5, #8]
; CHECK: bl g
; Moving SP, access via SP.
; int test_local_moving_sp(int n) {
; int v[4];
; int x, y, z;
; return u(v, &x, &y, s, s) + u(v, &y, &z, s, s);
; }
define dso_local i32 @test_local_moving_sp(i32 %n) local_unnamed_addr {
entry:
%v = alloca [4 x i32], align 4
%x = alloca i32, align 4
%y = alloca i32, align 4
%z = alloca i32, align 4
%0 = bitcast [4 x i32]* %v to i8*
%1 = bitcast i32* %x to i8*
%2 = bitcast i32* %y to i8*
%3 = bitcast i32* %z to i8*
%arraydecay = getelementptr inbounds [4 x i32], [4 x i32]* %v, i32 0, i32 0
%call = call i32 @u(i32* nonnull %arraydecay, i32* nonnull %x, i32* nonnull %y, %struct.S* byval nonnull align 4 @s, %struct.S* byval nonnull align 4 @s)
%call2 = call i32 @u(i32* nonnull %arraydecay, i32* nonnull %y, i32* nonnull %z, %struct.S* byval nonnull align 4 @s, %struct.S* byval nonnull align 4 @s)
%add = add nsw i32 %call2, %call
ret i32 %add
}
; CHECK-LABEL: test_local_moving_sp
; Locals area
; CHECK: sub sp, #36
; Setup BP
; CHECK: mov r6, sp
; Outoging arguments
; CHECK: sub sp, #508
; CHECK-NEXT: sub sp, #508
; CHECK-NEXT: sub sp, #8
; Argument addresses computed relative to BP
; CHECK: adds r0, r6, #7
; CHECK-NEXT: adds r0, #13
; CHECK: adds r1, r6, #7
; CHECK-NEXT: adds r1, #9
; CHECK: adds r5, r6, #7
; CHECK-NEXT: adds r5, #5
; CHECK: bl u
; Stack restored before next call
; CHECK: add sp, #508
; CHECK-NEXT: add sp, #508
; CHECK-NEXT: add sp, #8
; CHECK: bl u