blob: 2580771eb2cffdda42ad7680b6de255aead3591e [file] [log] [blame]
Dan Gohmanb7c24002016-05-21 00:21:56 +00001; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt | FileCheck %s
Derek Schuff9769deb2015-12-11 23:49:46 +00002
Dan Gohman0c6f5ac2016-01-07 03:19:23 +00003target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
Dan Gohman7d7409e2017-02-28 23:37:04 +00004target triple = "wasm32-unknown-unknown-wasm"
Derek Schuff9769deb2015-12-11 23:49:46 +00005
Derek Schuff4b3bb212016-02-23 18:13:07 +00006declare void @ext_func(i64* %ptr)
7declare void @ext_func_i32(i32* %ptr)
8
Derek Schuff9769deb2015-12-11 23:49:46 +00009; CHECK-LABEL: alloca32:
Derek Schuff45cd5a72015-12-16 20:43:06 +000010; Check that there is an extra local for the stack pointer.
Derek Schuffdc5f6aa2016-02-20 21:46:50 +000011; CHECK: .local i32{{$}}
Derek Schuff4b3bb212016-02-23 18:13:07 +000012define void @alloca32() noredzone {
Sam Clegg9d24fb72017-06-16 23:59:10 +000013 ; CHECK-NEXT: get_global $push[[L2:.+]]=, __stack_pointer{{$}}
Derek Schuffdc5f6aa2016-02-20 21:46:50 +000014 ; CHECK-NEXT: i32.const $push[[L3:.+]]=, 16
Dan Gohmanc9623db2016-08-18 17:51:27 +000015 ; CHECK-NEXT: i32.sub $push[[L9:.+]]=, $pop[[L2]], $pop[[L3]]
Dan Gohman7d7409e2017-02-28 23:37:04 +000016 ; CHECK-NEXT: tee_local $push[[L8:.+]]=, [[SP:.+]], $pop[[L9]]{{$}}
Sam Clegg9d24fb72017-06-16 23:59:10 +000017 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L8]]{{$}}
Derek Schuff9769deb2015-12-11 23:49:46 +000018 %retval = alloca i32
Dan Gohman7d7409e2017-02-28 23:37:04 +000019 ; CHECK: get_local $push[[L4:.+]]=, [[SP]]{{$}}
Derek Schuffdc5f6aa2016-02-20 21:46:50 +000020 ; CHECK: i32.const $push[[L0:.+]]=, 0
Dan Gohman7d7409e2017-02-28 23:37:04 +000021 ; CHECK: i32.store 12($pop[[L4]]), $pop[[L0]]
Derek Schuff9769deb2015-12-11 23:49:46 +000022 store i32 0, i32* %retval
Dan Gohman7d7409e2017-02-28 23:37:04 +000023 ; CHECK: get_local $push[[L6:.+]]=, [[SP]]{{$}}
Derek Schuffd4207ba2016-03-17 17:00:29 +000024 ; CHECK-NEXT: i32.const $push[[L5:.+]]=, 16
Dan Gohman7d7409e2017-02-28 23:37:04 +000025 ; CHECK-NEXT: i32.add $push[[L7:.+]]=, $pop[[L6]], $pop[[L5]]
Sam Clegg9d24fb72017-06-16 23:59:10 +000026 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L7]]
Derek Schuff9769deb2015-12-11 23:49:46 +000027 ret void
28}
29
30; CHECK-LABEL: alloca3264:
Derek Schuffdc5f6aa2016-02-20 21:46:50 +000031; CHECK: .local i32{{$}}
Derek Schuff9769deb2015-12-11 23:49:46 +000032define void @alloca3264() {
Sam Clegg9d24fb72017-06-16 23:59:10 +000033 ; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}}
Dan Gohman0cfb5f82016-05-10 04:24:02 +000034 ; CHECK-NEXT: i32.const $push[[L4:.+]]=, 16
35 ; CHECK-NEXT: i32.sub $push[[L6:.+]]=, $pop[[L3]], $pop[[L4]]
Dan Gohman7d7409e2017-02-28 23:37:04 +000036 ; CHECK-NEXT: tee_local $push[[L5:.+]]=, [[SP:.+]], $pop[[L6]]
Derek Schuff9769deb2015-12-11 23:49:46 +000037 %r1 = alloca i32
38 %r2 = alloca double
Chandler Carruth3a996812017-07-18 08:20:50 +000039 store i32 0, i32* %r1
Chandler Carruth3a996812017-07-18 08:20:50 +000040 store double 0.0, double* %r2
Chandler Carruthbb835582017-07-19 04:15:30 +000041 ; CHECK-NEXT: i64.const $push[[L1:.+]]=, 0
42 ; CHECK-NEXT: i64.store 0($pop[[L5]]), $pop[[L1]]
43 ; CHECK-NEXT: get_local $push[[L2:.+]]=, [[SP]]{{$}}
44 ; CHECK-NEXT: i32.const $push[[L0:.+]]=, 0
45 ; CHECK-NEXT: i32.store 12($pop[[L2]]), $pop[[L0]]
Derek Schuff4b3bb212016-02-23 18:13:07 +000046 ; CHECK-NEXT: return
Derek Schuff9769deb2015-12-11 23:49:46 +000047 ret void
48}
49
Derek Schuff45cd5a72015-12-16 20:43:06 +000050; CHECK-LABEL: allocarray:
Derek Schuffd4207ba2016-03-17 17:00:29 +000051; CHECK: .local i32{{$}}
Derek Schuff9769deb2015-12-11 23:49:46 +000052define void @allocarray() {
Sam Clegg9d24fb72017-06-16 23:59:10 +000053 ; CHECK-NEXT: get_global $push[[L4:.+]]=, __stack_pointer{{$}}
Dan Gohmanc9623db2016-08-18 17:51:27 +000054 ; CHECK-NEXT: i32.const $push[[L5:.+]]=, 144{{$}}
55 ; CHECK-NEXT: i32.sub $push[[L12:.+]]=, $pop[[L4]], $pop[[L5]]
Dan Gohman7d7409e2017-02-28 23:37:04 +000056 ; CHECK-NEXT: tee_local $push[[L11:.+]]=, 0, $pop[[L12]]
Sam Clegg9d24fb72017-06-16 23:59:10 +000057 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L11]]
Derek Schuff4b3bb212016-02-23 18:13:07 +000058 %r = alloca [33 x i32]
Dan Gohman7e649172016-01-20 04:21:16 +000059
Dan Gohman10545702016-05-17 22:24:18 +000060 ; CHECK: i32.const $push{{.+}}=, 24
Dan Gohman7d7409e2017-02-28 23:37:04 +000061 ; CHECK-NEXT: i32.add $push[[L3:.+]]=, $pop{{.+}}, $pop{{.+}}
Dan Gohman0cfb5f82016-05-10 04:24:02 +000062 ; CHECK-NEXT: i32.const $push[[L1:.+]]=, 1{{$}}
Dan Gohman7f1bdb22016-10-06 22:08:28 +000063 ; CHECK-NEXT: i32.store 0($pop[[L3]]), $pop[[L1]]{{$}}
Dan Gohman7d7409e2017-02-28 23:37:04 +000064 ; CHECK-NEXT: get_local $push[[L4:.+]]=, 0{{$}}
Dan Gohmanc9623db2016-08-18 17:51:27 +000065 ; CHECK-NEXT: i32.const $push[[L10:.+]]=, 1{{$}}
Dan Gohman7d7409e2017-02-28 23:37:04 +000066 ; CHECK-NEXT: i32.store 12($pop[[L4]]), $pop[[L10]]{{$}}
Derek Schuff4b3bb212016-02-23 18:13:07 +000067 %p = getelementptr [33 x i32], [33 x i32]* %r, i32 0, i32 0
Derek Schuff9769deb2015-12-11 23:49:46 +000068 store i32 1, i32* %p
Derek Schuff4b3bb212016-02-23 18:13:07 +000069 %p2 = getelementptr [33 x i32], [33 x i32]* %r, i32 0, i32 3
Derek Schuff9769deb2015-12-11 23:49:46 +000070 store i32 1, i32* %p2
Dan Gohman7e649172016-01-20 04:21:16 +000071
Dan Gohman7d7409e2017-02-28 23:37:04 +000072 ; CHECK-NEXT: get_local $push[[L2:.+]]=, [[SP]]{{$}}
Dan Gohmanc9623db2016-08-18 17:51:27 +000073 ; CHECK-NEXT: i32.const $push[[L7:.+]]=, 144
Dan Gohman7d7409e2017-02-28 23:37:04 +000074 ; CHECK-NEXT: i32.add $push[[L8:.+]]=, $pop[[L2]], $pop[[L7]]
Sam Clegg9d24fb72017-06-16 23:59:10 +000075 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L8]]
Derek Schuff9769deb2015-12-11 23:49:46 +000076 ret void
77}
78
Derek Schuff90d9e8d2016-01-26 22:47:43 +000079; CHECK-LABEL: non_mem_use
Derek Schuffc97ba932016-01-30 21:43:08 +000080define void @non_mem_use(i8** %addr) {
Dan Gohmanc9623db2016-08-18 17:51:27 +000081 ; CHECK: i32.const $push[[L2:.+]]=, 48
82 ; CHECK-NEXT: i32.sub $push[[L12:.+]]=, {{.+}}, $pop[[L2]]
Dan Gohman7d7409e2017-02-28 23:37:04 +000083 ; CHECK-NEXT: tee_local $push[[L11:.+]]=, [[SP:.+]], $pop[[L12]]
Sam Clegg9d24fb72017-06-16 23:59:10 +000084 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L11]]
Derek Schuffc97ba932016-01-30 21:43:08 +000085 %buf = alloca [27 x i8], align 16
Derek Schuff90d9e8d2016-01-26 22:47:43 +000086 %r = alloca i64
87 %r2 = alloca i64
88 ; %r is at SP+8
Dan Gohman7d7409e2017-02-28 23:37:04 +000089 ; CHECK: get_local $push[[L3:.+]]=, [[SP]]
Derek Schuffd4207ba2016-03-17 17:00:29 +000090 ; CHECK: i32.const $push[[OFF:.+]]=, 8
Dan Gohman7d7409e2017-02-28 23:37:04 +000091 ; CHECK-NEXT: i32.add $push[[ARG1:.+]]=, $pop[[L3]], $pop[[OFF]]
Derek Schuffd4207ba2016-03-17 17:00:29 +000092 ; CHECK-NEXT: call ext_func@FUNCTION, $pop[[ARG1]]
Derek Schuff90d9e8d2016-01-26 22:47:43 +000093 call void @ext_func(i64* %r)
94 ; %r2 is at SP+0, no add needed
Dan Gohman7d7409e2017-02-28 23:37:04 +000095 ; CHECK: get_local $push[[L4:.+]]=, [[SP]]
96 ; CHECK-NEXT: call ext_func@FUNCTION, $pop[[L4]]
Derek Schuff90d9e8d2016-01-26 22:47:43 +000097 call void @ext_func(i64* %r2)
Derek Schuffc97ba932016-01-30 21:43:08 +000098 ; Use as a value, but in a store
99 ; %buf is at SP+16
Dan Gohman7d7409e2017-02-28 23:37:04 +0000100 ; CHECK: get_local $push[[L5:.+]]=, [[SP]]
Derek Schuffd4207ba2016-03-17 17:00:29 +0000101 ; CHECK: i32.const $push[[OFF:.+]]=, 16
Dan Gohman7d7409e2017-02-28 23:37:04 +0000102 ; CHECK-NEXT: i32.add $push[[VAL:.+]]=, $pop[[L5]], $pop[[OFF]]
103 ; CHECK-NEXT: i32.store 0($pop{{.+}}), $pop[[VAL]]
Derek Schuffc97ba932016-01-30 21:43:08 +0000104 %gep = getelementptr inbounds [27 x i8], [27 x i8]* %buf, i32 0, i32 0
105 store i8* %gep, i8** %addr
Derek Schuff90d9e8d2016-01-26 22:47:43 +0000106 ret void
107}
108
Dan Gohman7e649172016-01-20 04:21:16 +0000109; CHECK-LABEL: allocarray_inbounds:
Derek Schuffdc5f6aa2016-02-20 21:46:50 +0000110; CHECK: .local i32{{$}}
Derek Schuff9bfea272016-01-07 18:55:52 +0000111define void @allocarray_inbounds() {
Sam Clegg9d24fb72017-06-16 23:59:10 +0000112 ; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}}
Dan Gohmanc9623db2016-08-18 17:51:27 +0000113 ; CHECK-NEXT: i32.const $push[[L4:.+]]=, 32{{$}}
114 ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, $pop[[L3]], $pop[[L4]]
Dan Gohman7d7409e2017-02-28 23:37:04 +0000115 ; CHECK-NEXT: tee_local $push[[L10:.+]]=, [[SP:.+]], $pop[[L11]]
Sam Clegg9d24fb72017-06-16 23:59:10 +0000116 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}}
Derek Schuff9bfea272016-01-07 18:55:52 +0000117 %r = alloca [5 x i32]
118 ; CHECK: i32.const $push[[L3:.+]]=, 1
Dan Gohman7f1bdb22016-10-06 22:08:28 +0000119 ; CHECK-DAG: i32.store 24(${{.+}}), $pop[[L3]]
Derek Schuff9bfea272016-01-07 18:55:52 +0000120 %p = getelementptr inbounds [5 x i32], [5 x i32]* %r, i32 0, i32 0
121 store i32 1, i32* %p
122 ; This store should have both the GEP and the FI folded into it.
Dan Gohman7f1bdb22016-10-06 22:08:28 +0000123 ; CHECK-DAG: i32.store 12(${{.+}}), $pop
Dan Gohmanbb372242016-01-26 03:39:31 +0000124 %p2 = getelementptr inbounds [5 x i32], [5 x i32]* %r, i32 0, i32 3
Derek Schuff9bfea272016-01-07 18:55:52 +0000125 store i32 1, i32* %p2
Derek Schuff4b3bb212016-02-23 18:13:07 +0000126 call void @ext_func(i64* null);
Dan Gohmand530f682016-05-24 23:47:41 +0000127 ; CHECK: call ext_func
Dan Gohman7d7409e2017-02-28 23:37:04 +0000128 ; CHECK: i32.const $push[[L5:.+]]=, 32{{$}}
Dan Gohman10545702016-05-17 22:24:18 +0000129 ; CHECK-NEXT: i32.add $push[[L7:.+]]=, ${{.+}}, $pop[[L5]]
Sam Clegg9d24fb72017-06-16 23:59:10 +0000130 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L7]]
Derek Schuff9bfea272016-01-07 18:55:52 +0000131 ret void
132}
133
Dan Gohman7e649172016-01-20 04:21:16 +0000134; CHECK-LABEL: dynamic_alloca:
Derek Schuff8bb5f292015-12-16 23:21:30 +0000135define void @dynamic_alloca(i32 %alloc) {
Sam Clegg9d24fb72017-06-16 23:59:10 +0000136 ; CHECK: get_global $push[[L13:.+]]=, __stack_pointer{{$}}
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000137 ; CHECK-NEXT: tee_local $push[[L12:.+]]=, [[SP:.+]], $pop[[L13]]{{$}}
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000138 ; Target independent codegen bumps the stack pointer.
139 ; CHECK: i32.sub
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000140 ; Check that SP is written back to memory after decrement
Sam Clegg9d24fb72017-06-16 23:59:10 +0000141 ; CHECK: set_global __stack_pointer,
Derek Schuff6ea637a2016-01-29 18:37:49 +0000142 %r = alloca i32, i32 %alloc
143 ; Target-independent codegen also calculates the store addr
Derek Schuff4b3bb212016-02-23 18:13:07 +0000144 ; CHECK: call ext_func_i32@FUNCTION
145 call void @ext_func_i32(i32* %r)
Sam Clegg9d24fb72017-06-16 23:59:10 +0000146 ; CHECK: set_global __stack_pointer, $pop{{.+}}
Derek Schuff6ea637a2016-01-29 18:37:49 +0000147 ret void
148}
149
Derek Schuff4b3bb212016-02-23 18:13:07 +0000150; CHECK-LABEL: dynamic_alloca_redzone:
151define void @dynamic_alloca_redzone(i32 %alloc) {
Sam Clegg9d24fb72017-06-16 23:59:10 +0000152 ; CHECK: get_global $push[[L13:.+]]=, __stack_pointer{{$}}
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000153 ; CHECK-NEXT: tee_local $push[[L12:.+]]=, [[SP:.+]], $pop[[L13]]{{$}}
Derek Schuff4b3bb212016-02-23 18:13:07 +0000154 ; Target independent codegen bumps the stack pointer
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000155 ; CHECK: i32.sub
Derek Schuff4b3bb212016-02-23 18:13:07 +0000156 %r = alloca i32, i32 %alloc
Dan Gohman7d7409e2017-02-28 23:37:04 +0000157 ; CHECK-NEXT: tee_local $push[[L8:.+]]=, {{.+}}, $pop
158 ; CHECK: get_local $push[[L7:.+]]=, 0{{$}}
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000159 ; CHECK-NEXT: i32.const $push[[L6:.+]]=, 0{{$}}
Dan Gohman7d7409e2017-02-28 23:37:04 +0000160 ; CHECK-NEXT: i32.store 0($pop[[L7]]), $pop[[L6]]{{$}}
Derek Schuff4b3bb212016-02-23 18:13:07 +0000161 store i32 0, i32* %r
162 ; CHECK-NEXT: return
163 ret void
164}
165
Derek Schuff6ea637a2016-01-29 18:37:49 +0000166; CHECK-LABEL: dynamic_static_alloca:
Derek Schuff4b3bb212016-02-23 18:13:07 +0000167define void @dynamic_static_alloca(i32 %alloc) noredzone {
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000168 ; Decrement SP in the prolog by the static amount and writeback to memory.
Sam Clegg9d24fb72017-06-16 23:59:10 +0000169 ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}}
Derek Schuff92d300e2016-09-26 21:18:03 +0000170 ; CHECK-NEXT: i32.const $push[[L12:.+]]=, 16
171 ; CHECK-NEXT: i32.sub $push[[L23:.+]]=, $pop[[L11]], $pop[[L12]]
Dan Gohman7d7409e2017-02-28 23:37:04 +0000172 ; CHECK-NEXT: tee_local $push[[L22:.+]]=, [[SP:.+]], $pop[[L23]]
Sam Clegg9d24fb72017-06-16 23:59:10 +0000173 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L22]]
Derek Schuff92d300e2016-09-26 21:18:03 +0000174
175 ; Alloc and write to a static alloca
Dan Gohman7d7409e2017-02-28 23:37:04 +0000176 ; CHECK: get_local $push[[L21:.+]]=, [[SP:.+]]
177 ; CHECK-NEXT: tee_local $push[[pushedFP:.+]]=, [[FP:.+]], $pop[[L21]]
Derek Schuff92d300e2016-09-26 21:18:03 +0000178 ; CHECK-NEXT: i32.const $push[[L0:.+]]=, 101
Dan Gohman7f1bdb22016-10-06 22:08:28 +0000179 ; CHECK-NEXT: i32.store [[static_offset:.+]]($pop[[pushedFP]]), $pop[[L0]]
Derek Schuff92d300e2016-09-26 21:18:03 +0000180 %static = alloca i32
181 store volatile i32 101, i32* %static
182
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000183 ; Decrement SP in the body by the dynamic amount.
184 ; CHECK: i32.sub
Dan Gohman7d7409e2017-02-28 23:37:04 +0000185 ; CHECK: tee_local $push[[L16:.+]]=, [[dynamic_local:.+]], $pop{{.+}}
186 ; CHECK: tee_local $push[[L15:.+]]=, [[other:.+]], $pop[[L16]]{{$}}
Sam Clegg9d24fb72017-06-16 23:59:10 +0000187 ; CHECK: set_global __stack_pointer, $pop[[L15]]{{$}}
Derek Schuff92d300e2016-09-26 21:18:03 +0000188 %dynamic = alloca i32, i32 %alloc
189
190 ; Ensure we don't modify the frame pointer after assigning it.
191 ; CHECK-NOT: $[[FP]]=
192
193 ; Ensure the static address doesn't change after modifying the stack pointer.
Dan Gohman7d7409e2017-02-28 23:37:04 +0000194 ; CHECK: get_local $push[[L17:.+]]=, [[FP]]
Derek Schuff92d300e2016-09-26 21:18:03 +0000195 ; CHECK: i32.const $push[[L7:.+]]=, 102
Dan Gohman7d7409e2017-02-28 23:37:04 +0000196 ; CHECK-NEXT: i32.store [[static_offset]]($pop[[L17]]), $pop[[L7]]
197 ; CHECK-NEXT: get_local $push[[L9:.+]]=, [[dynamic_local]]{{$}}
Derek Schuff92d300e2016-09-26 21:18:03 +0000198 ; CHECK-NEXT: i32.const $push[[L8:.+]]=, 103
Dan Gohman7d7409e2017-02-28 23:37:04 +0000199 ; CHECK-NEXT: i32.store 0($pop[[L9]]), $pop[[L8]]
Derek Schuff92d300e2016-09-26 21:18:03 +0000200 store volatile i32 102, i32* %static
201 store volatile i32 103, i32* %dynamic
202
203 ; Decrement SP in the body by the dynamic amount.
204 ; CHECK: i32.sub
Dan Gohman7d7409e2017-02-28 23:37:04 +0000205 ; CHECK: tee_local $push{{.+}}=, [[dynamic2_local:.+]], $pop{{.+}}
Derek Schuff92d300e2016-09-26 21:18:03 +0000206 %dynamic.2 = alloca i32, i32 %alloc
207
208 ; CHECK-NOT: $[[FP]]=
209
210 ; Ensure neither the static nor dynamic address changes after the second
211 ; modification of the stack pointer.
Dan Gohman7d7409e2017-02-28 23:37:04 +0000212 ; CHECK: get_local $push[[L22:.+]]=, [[FP]]
Derek Schuff92d300e2016-09-26 21:18:03 +0000213 ; CHECK: i32.const $push[[L9:.+]]=, 104
Dan Gohman7d7409e2017-02-28 23:37:04 +0000214 ; CHECK-NEXT: i32.store [[static_offset]]($pop[[L22]]), $pop[[L9]]
215 ; CHECK-NEXT: get_local $push[[L23:.+]]=, [[dynamic_local]]
Derek Schuff92d300e2016-09-26 21:18:03 +0000216 ; CHECK-NEXT: i32.const $push[[L10:.+]]=, 105
Dan Gohman7d7409e2017-02-28 23:37:04 +0000217 ; CHECK-NEXT: i32.store 0($pop[[L23]]), $pop[[L10]]
218 ; CHECK-NEXT: get_local $push[[L23:.+]]=, [[dynamic2_local]]
Derek Schuff92d300e2016-09-26 21:18:03 +0000219 ; CHECK-NEXT: i32.const $push[[L11:.+]]=, 106
Dan Gohman7d7409e2017-02-28 23:37:04 +0000220 ; CHECK-NEXT: i32.store 0($pop[[L23]]), $pop[[L11]]
Derek Schuff92d300e2016-09-26 21:18:03 +0000221 store volatile i32 104, i32* %static
222 store volatile i32 105, i32* %dynamic
223 store volatile i32 106, i32* %dynamic.2
224
Derek Schuff27e3b8a2016-02-22 21:57:17 +0000225 ; Writeback to memory.
Dan Gohman7d7409e2017-02-28 23:37:04 +0000226 ; CHECK: get_local $push[[L24:.+]]=, [[FP]]{{$}}
227 ; CHECK: i32.const $push[[L18:.+]]=, 16
228 ; CHECK-NEXT: i32.add $push[[L19:.+]]=, $pop[[L24]], $pop[[L18]]
Sam Clegg9d24fb72017-06-16 23:59:10 +0000229 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L19]]
Derek Schuff8bb5f292015-12-16 23:21:30 +0000230 ret void
231}
Derek Schuffc97ba932016-01-30 21:43:08 +0000232
Derek Schuffe9e68912016-09-30 18:02:54 +0000233declare i8* @llvm.stacksave()
234declare void @llvm.stackrestore(i8*)
235
236; CHECK-LABEL: llvm_stack_builtins:
237define void @llvm_stack_builtins(i32 %alloc) noredzone {
Sam Clegg9d24fb72017-06-16 23:59:10 +0000238 ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}}
Dan Gohman7d7409e2017-02-28 23:37:04 +0000239 ; CHECK-NEXT: tee_local $push[[L10:.+]]=, {{.+}}, $pop[[L11]]
240 ; CHECK-NEXT: set_local [[STACK:.+]], $pop[[L10]]
Derek Schuffe9e68912016-09-30 18:02:54 +0000241 %stack = call i8* @llvm.stacksave()
242
243 ; Ensure we don't reassign the stacksave local
Dan Gohman7d7409e2017-02-28 23:37:04 +0000244 ; CHECK-NOT: set_local [[STACK]],
Derek Schuffe9e68912016-09-30 18:02:54 +0000245 %dynamic = alloca i32, i32 %alloc
246
Dan Gohman7d7409e2017-02-28 23:37:04 +0000247 ; CHECK: get_local $push[[L12:.+]]=, [[STACK]]
Sam Clegg9d24fb72017-06-16 23:59:10 +0000248 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L12]]
Derek Schuffe9e68912016-09-30 18:02:54 +0000249 call void @llvm.stackrestore(i8* %stack)
250
251 ret void
252}
253
254; Not actually using the alloca'd variables exposed an issue with register
255; stackification, where copying the stack pointer into the frame pointer was
256; moved after the stack pointer was updated for the dynamic alloca.
257; CHECK-LABEL: dynamic_alloca_nouse:
258define void @dynamic_alloca_nouse(i32 %alloc) noredzone {
Sam Clegg9d24fb72017-06-16 23:59:10 +0000259 ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}}
Dan Gohman7d7409e2017-02-28 23:37:04 +0000260 ; CHECK-NEXT: tee_local $push[[L10:.+]]=, {{.+}}, $pop[[L11]]
261 ; CHECK-NEXT: set_local [[FP:.+]], $pop[[L10]]
Derek Schuffe9e68912016-09-30 18:02:54 +0000262 %dynamic = alloca i32, i32 %alloc
263
Dan Gohman7d7409e2017-02-28 23:37:04 +0000264 ; CHECK-NOT: set_local [[FP]],
Derek Schuffe9e68912016-09-30 18:02:54 +0000265
Dan Gohman7d7409e2017-02-28 23:37:04 +0000266 ; CHECK: get_local $push[[L12:.+]]=, [[FP]]
Sam Clegg9d24fb72017-06-16 23:59:10 +0000267 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L12]]
Derek Schuffe9e68912016-09-30 18:02:54 +0000268 ret void
269}
270
Derek Schuffaadc89c2016-02-16 18:18:36 +0000271; The use of the alloca in a phi causes a CopyToReg DAG node to be generated,
272; which has to have special handling because CopyToReg can't have a FI operand
273; CHECK-LABEL: copytoreg_fi:
274define void @copytoreg_fi(i1 %cond, i32* %b) {
275entry:
Derek Schuffdc5f6aa2016-02-20 21:46:50 +0000276 ; CHECK: i32.const $push[[L1:.+]]=, 16
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000277 ; CHECK-NEXT: i32.sub $push[[L3:.+]]=, {{.+}}, $pop[[L1]]
Derek Schuffaadc89c2016-02-16 18:18:36 +0000278 %addr = alloca i32
Derek Schuffd4207ba2016-03-17 17:00:29 +0000279 ; CHECK: i32.const $push[[OFF:.+]]=, 12
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000280 ; CHECK-NEXT: i32.add $push[[ADDR:.+]]=, $pop[[L3]], $pop[[OFF]]
Dan Gohman7d7409e2017-02-28 23:37:04 +0000281 ; CHECK-NEXT: set_local [[COPY:.+]], $pop[[ADDR]]
Derek Schuffaadc89c2016-02-16 18:18:36 +0000282 br label %body
283body:
284 %a = phi i32* [%addr, %entry], [%b, %body]
285 store i32 1, i32* %a
Dan Gohman7d7409e2017-02-28 23:37:04 +0000286 ; CHECK: get_local $push[[L12:.+]]=, [[COPY]]
287 ; CHECK: i32.store 0($pop[[L12]]),
Derek Schuffaadc89c2016-02-16 18:18:36 +0000288 br i1 %cond, label %body, label %exit
289exit:
290 ret void
291}
292
Dan Gohman94c65662016-02-16 23:48:04 +0000293declare void @use_i8_star(i8*)
294declare i8* @llvm.frameaddress(i32)
295
296; Test __builtin_frame_address(0).
Dan Gohman94c65662016-02-16 23:48:04 +0000297; CHECK-LABEL: frameaddress_0:
Sam Clegg9d24fb72017-06-16 23:59:10 +0000298; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}}
Dan Gohman7d7409e2017-02-28 23:37:04 +0000299; CHECK-NEXT: tee_local $push[[L2:.+]]=, [[FP:.+]], $pop[[L3]]{{$}}
Dan Gohman0cfb5f82016-05-10 04:24:02 +0000300; CHECK-NEXT: call use_i8_star@FUNCTION, $pop[[L2]]
Dan Gohman7d7409e2017-02-28 23:37:04 +0000301; CHECK-NEXT: get_local $push[[L5:.+]]=, [[FP]]
Sam Clegg9d24fb72017-06-16 23:59:10 +0000302; CHECK-NEXT: set_global __stack_pointer, $pop[[L5]]
Dan Gohman94c65662016-02-16 23:48:04 +0000303define void @frameaddress_0() {
304 %t = call i8* @llvm.frameaddress(i32 0)
305 call void @use_i8_star(i8* %t)
306 ret void
307}
308
309; Test __builtin_frame_address(1).
310
311; CHECK-LABEL: frameaddress_1:
312; CHECK-NEXT: i32.const $push0=, 0{{$}}
313; CHECK-NEXT: call use_i8_star@FUNCTION, $pop0{{$}}
314; CHECK-NEXT: return{{$}}
315define void @frameaddress_1() {
316 %t = call i8* @llvm.frameaddress(i32 1)
317 call void @use_i8_star(i8* %t)
318 ret void
319}
320
Dan Gohman02c08712016-02-20 23:09:44 +0000321; Test a stack address passed to an inline asm.
322; CHECK-LABEL: inline_asm:
Sam Clegg9d24fb72017-06-16 23:59:10 +0000323; CHECK: get_global {{.+}}, __stack_pointer{{$}}
Dan Gohman02c08712016-02-20 23:09:44 +0000324; CHECK: #APP
325; CHECK-NEXT: # %{{[0-9]+}}{{$}}
326; CHECK-NEXT: #NO_APP
327define void @inline_asm() {
328 %tmp = alloca i8
329 call void asm sideeffect "# %0", "r"(i8* %tmp)
330 ret void
331}
332
Derek Schuffc97ba932016-01-30 21:43:08 +0000333; TODO: test over-aligned alloca