Home | History | Annotate | Download | only in AArch64
      1 ; RUN: llc -mtriple=aarch64-none-linux-gnu -verify-machineinstrs < %s | FileCheck %s
      2 
      3 declare void @use_addr(i8*)
      4 
      5 define void @test_simple_alloca(i64 %n) {
      6 ; CHECK-LABEL: test_simple_alloca:
      7 
      8   %buf = alloca i8, i64 %n
      9   ; Make sure we align the stack change to 16 bytes:
     10 ; CHECK: add [[SPDELTA:x[0-9]+]], x0, #15
     11 ; CHECK: and x0, [[SPDELTA]], #0xfffffffffffffff0
     12 
     13   ; Make sure we change SP. It would be surprising if anything but x0 were used
     14   ; for the final sp, but it could be if it was then moved into x0.
     15 ; CHECK: mov [[TMP:x[0-9]+]], sp
     16 ; CHECK: sub x0, [[TMP]], [[SPDELTA]]
     17 ; CHECK: mov sp, x0
     18 
     19   call void @use_addr(i8* %buf)
     20 ; CHECK: bl use_addr
     21 
     22   ret void
     23   ; Make sure epilogue restores sp from fp
     24 ; CHECK: sub sp, x29, #16
     25 ; CHECK: ldp x29, x30, [sp, #16]
     26 ; CHECK: add sp, sp, #32
     27 ; CHECK: ret
     28 }
     29 
     30 declare void @use_addr_loc(i8*, i64*)
     31 
     32 define i64 @test_alloca_with_local(i64 %n) {
     33 ; CHECK-LABEL: test_alloca_with_local:
     34 ; CHECK: sub sp, sp, #32
     35 ; CHECK: stp x29, x30, [sp, #16]
     36 
     37   %loc = alloca i64
     38   %buf = alloca i8, i64 %n
     39   ; Make sure we align the stack change to 16 bytes:
     40 ; CHECK: add [[SPDELTA:x[0-9]+]], x0, #15
     41 ; CHECK: and x0, [[SPDELTA]], #0xfffffffffffffff0
     42 
     43   ; Make sure we change SP. It would be surprising if anything but x0 were used
     44   ; for the final sp, but it could be if it was then moved into x0.
     45 ; CHECK: mov [[TMP:x[0-9]+]], sp
     46 ; CHECK: sub x0, [[TMP]], [[SPDELTA]]
     47 ; CHECK: mov sp, x0
     48 
     49   ; Obviously suboptimal code here, but it to get &local in x1
     50 ; CHECK: sub [[TMP:x[0-9]+]], x29, [[LOC_FROM_FP:#[0-9]+]]
     51 ; CHECK: add x1, [[TMP]], #0
     52 
     53   call void @use_addr_loc(i8* %buf, i64* %loc)
     54 ; CHECK: bl use_addr
     55 
     56   %val = load i64* %loc
     57 ; CHECK: sub x[[TMP:[0-9]+]], x29, [[LOC_FROM_FP]]
     58 ; CHECK: ldr x0, [x[[TMP]]]
     59 
     60   ret i64 %val
     61   ; Make sure epilogue restores sp from fp
     62 ; CHECK: sub sp, x29, #16
     63 ; CHECK: ldp x29, x30, [sp, #16]
     64 ; CHECK: add sp, sp, #32
     65 ; CHECK: ret
     66 }
     67 
     68 define void @test_variadic_alloca(i64 %n, ...) {
     69 ; CHECK-LABEL: test_variadic_alloca:
     70 
     71 ; CHECK: sub     sp, sp, #208
     72 ; CHECK: stp     x29, x30, [sp, #192]
     73 ; CHECK: add     x29, sp, #192
     74 ; CHECK: sub     [[TMP:x[0-9]+]], x29, #192
     75 ; CHECK: add     x8, [[TMP]], #0
     76 ; CHECK: str     q7, [x8, #112]
     77 ; [...]
     78 ; CHECK: str     q1, [x8, #16]
     79 
     80   %addr = alloca i8, i64 %n
     81 
     82   call void @use_addr(i8* %addr)
     83 ; CHECK: bl use_addr
     84 
     85   ret void
     86 ; CHECK: sub sp, x29, #192
     87 ; CHECK: ldp x29, x30, [sp, #192]
     88 ; CHECK: add sp, sp, #208
     89 }
     90 
     91 define void @test_alloca_large_frame(i64 %n) {
     92 ; CHECK-LABEL: test_alloca_large_frame:
     93 
     94 ; CHECK: sub sp, sp, #496
     95 ; CHECK: stp x29, x30, [sp, #480]
     96 ; CHECK: add x29, sp, #480
     97 ; CHECK: sub sp, sp, #48
     98 ; CHECK: sub sp, sp, #1953, lsl #12
     99 
    100   %addr1 = alloca i8, i64 %n
    101   %addr2 = alloca i64, i64 1000000
    102 
    103   call void @use_addr_loc(i8* %addr1, i64* %addr2)
    104 
    105   ret void
    106 ; CHECK: sub sp, x29, #480
    107 ; CHECK: ldp x29, x30, [sp, #480]
    108 ; CHECK: add sp, sp, #496
    109 }
    110 
    111 declare i8* @llvm.stacksave()
    112 declare void @llvm.stackrestore(i8*)
    113 
    114 define void @test_scoped_alloca(i64 %n) {
    115 ; CHECK: test_scoped_alloca
    116 ; CHECK: sub sp, sp, #32
    117 
    118   %sp = call i8* @llvm.stacksave()
    119 ; CHECK: mov [[SAVED_SP:x[0-9]+]], sp
    120 
    121   %addr = alloca i8, i64 %n
    122 ; CHECK: and [[SPDELTA:x[0-9]+]], {{x[0-9]+}}, #0xfffffffffffffff0
    123 ; CHECK: mov [[OLDSP:x[0-9]+]], sp
    124 ; CHECK: sub [[NEWSP:x[0-9]+]], [[OLDSP]], [[SPDELTA]]
    125 ; CHECK: mov sp, [[NEWSP]]
    126 
    127   call void @use_addr(i8* %addr)
    128 ; CHECK: bl use_addr
    129 
    130   call void @llvm.stackrestore(i8* %sp)
    131 ; CHECK: mov sp, [[SAVED_SP]]
    132 
    133   ret void
    134 }
    135