1 ; RUN: llc -march=r600 -mcpu=redwood < %s | FileCheck %s -check-prefix=R600 -check-prefix=FUNC
2 ; RUN: llc -show-mc-encoding -mattr=+promote-alloca -verify-machineinstrs -march=amdgcn -mcpu=SI < %s | FileCheck %s -check-prefix=SI-PROMOTE -check-prefix=SI -check-prefix=FUNC
3 ; RUN: llc -show-mc-encoding -mattr=-promote-alloca -verify-machineinstrs -march=amdgcn -mcpu=SI < %s | FileCheck %s -check-prefix=SI-ALLOCA -check-prefix=SI -check-prefix=FUNC
4 ; RUN: llc -show-mc-encoding -mattr=-promote-alloca -verify-machineinstrs -mtriple=amdgcn-amdhsa -mcpu=kaveri < %s | FileCheck %s -check-prefix=SI-ALLOCA -check-prefix=SI -check-prefix=FUNC -check-prefix=HSA-ALLOCA
5 ; RUN: llc -show-mc-encoding -mattr=+promote-alloca -verify-machineinstrs -march=amdgcn -mcpu=tonga < %s | FileCheck %s -check-prefix=SI-PROMOTE -check-prefix=SI -check-prefix=FUNC
6 ; RUN: llc -show-mc-encoding -mattr=-promote-alloca -verify-machineinstrs -march=amdgcn -mcpu=tonga < %s | FileCheck %s -check-prefix=SI-ALLOCA -check-prefix=SI -check-prefix=FUNC
8 declare i32 @llvm.r600.read.tidig.x() nounwind readnone
10 ; FUNC-LABEL: {{^}}mova_same_clause:
17 ; SI-PROMOTE: ds_write_b32
18 ; SI-PROMOTE: ds_write_b32
19 ; SI-PROMOTE: ds_read_b32
20 ; SI-PROMOTE: ds_read_b32
22 ; HSA-ALLOCA: .amd_kernel_code_t
23 ; FIXME: Creating the emergency stack slots causes us to over-estimate scratch
25 ; HSA-ALLOCA: workitem_private_segment_byte_size = 24
26 ; HSA-ALLOCA: .end_amd_kernel_code_t
28 ; SI-ALLOCA: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen ; encoding: [0x00,0x10,0x70,0xe0
29 ; SI-ALLOCA: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen ; encoding: [0x00,0x10,0x70,0xe0
30 define void @mova_same_clause(i32 addrspace(1)* nocapture %out, i32 addrspace(1)* nocapture %in) {
32 %stack = alloca [5 x i32], align 4
33 %0 = load i32, i32 addrspace(1)* %in, align 4
34 %arrayidx1 = getelementptr inbounds [5 x i32], [5 x i32]* %stack, i32 0, i32 %0
35 store i32 4, i32* %arrayidx1, align 4
36 %arrayidx2 = getelementptr inbounds i32, i32 addrspace(1)* %in, i32 1
37 %1 = load i32, i32 addrspace(1)* %arrayidx2, align 4
38 %arrayidx3 = getelementptr inbounds [5 x i32], [5 x i32]* %stack, i32 0, i32 %1
39 store i32 5, i32* %arrayidx3, align 4
40 %arrayidx10 = getelementptr inbounds [5 x i32], [5 x i32]* %stack, i32 0, i32 0
41 %2 = load i32, i32* %arrayidx10, align 4
42 store i32 %2, i32 addrspace(1)* %out, align 4
43 %arrayidx12 = getelementptr inbounds [5 x i32], [5 x i32]* %stack, i32 0, i32 1
44 %3 = load i32, i32* %arrayidx12
45 %arrayidx13 = getelementptr inbounds i32, i32 addrspace(1)* %out, i32 1
46 store i32 %3, i32 addrspace(1)* %arrayidx13
50 ; This test checks that the stack offset is calculated correctly for structs.
51 ; All register loads/stores should be optimized away, so there shouldn't be
52 ; any MOVA instructions.
54 ; XXX: This generated code has unnecessary MOVs, we should be able to optimize
57 ; FUNC-LABEL: {{^}}multiple_structs:
61 %struct.point = type { i32, i32 }
63 define void @multiple_structs(i32 addrspace(1)* %out) {
65 %a = alloca %struct.point
66 %b = alloca %struct.point
67 %a.x.ptr = getelementptr %struct.point, %struct.point* %a, i32 0, i32 0
68 %a.y.ptr = getelementptr %struct.point, %struct.point* %a, i32 0, i32 1
69 %b.x.ptr = getelementptr %struct.point, %struct.point* %b, i32 0, i32 0
70 %b.y.ptr = getelementptr %struct.point, %struct.point* %b, i32 0, i32 1
71 store i32 0, i32* %a.x.ptr
72 store i32 1, i32* %a.y.ptr
73 store i32 2, i32* %b.x.ptr
74 store i32 3, i32* %b.y.ptr
75 %a.indirect.ptr = getelementptr %struct.point, %struct.point* %a, i32 0, i32 0
76 %b.indirect.ptr = getelementptr %struct.point, %struct.point* %b, i32 0, i32 0
77 %a.indirect = load i32, i32* %a.indirect.ptr
78 %b.indirect = load i32, i32* %b.indirect.ptr
79 %0 = add i32 %a.indirect, %b.indirect
80 store i32 %0, i32 addrspace(1)* %out
84 ; Test direct access of a private array inside a loop. The private array
85 ; loads and stores should be lowered to copies, so there shouldn't be any
88 ; FUNC-LABEL: {{^}}direct_loop:
92 define void @direct_loop(i32 addrspace(1)* %out, i32 addrspace(1)* %in) {
94 %prv_array_const = alloca [2 x i32]
95 %prv_array = alloca [2 x i32]
96 %a = load i32, i32 addrspace(1)* %in
97 %b_src_ptr = getelementptr i32, i32 addrspace(1)* %in, i32 1
98 %b = load i32, i32 addrspace(1)* %b_src_ptr
99 %a_dst_ptr = getelementptr [2 x i32], [2 x i32]* %prv_array_const, i32 0, i32 0
100 store i32 %a, i32* %a_dst_ptr
101 %b_dst_ptr = getelementptr [2 x i32], [2 x i32]* %prv_array_const, i32 0, i32 1
102 store i32 %b, i32* %b_dst_ptr
106 %inc = phi i32 [0, %entry], [%count, %for.body]
107 %x_ptr = getelementptr [2 x i32], [2 x i32]* %prv_array_const, i32 0, i32 0
108 %x = load i32, i32* %x_ptr
109 %y_ptr = getelementptr [2 x i32], [2 x i32]* %prv_array, i32 0, i32 0
110 %y = load i32, i32* %y_ptr
112 store i32 %xy, i32* %y_ptr
113 %count = add i32 %inc, 1
114 %done = icmp eq i32 %count, 4095
115 br i1 %done, label %for.end, label %for.body
118 %value_ptr = getelementptr [2 x i32], [2 x i32]* %prv_array, i32 0, i32 0
119 %value = load i32, i32* %value_ptr
120 store i32 %value, i32 addrspace(1)* %out
124 ; FUNC-LABEL: {{^}}short_array:
128 ; SI-PROMOTE-DAG: buffer_store_short v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen ; encoding: [0x00,0x10,0x68,0xe0
129 ; SI-PROMOTE-DAG: buffer_store_short v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen offset:2 ; encoding: [0x02,0x10,0x68,0xe0
130 ; SI-PROMOTE: buffer_load_sshort v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}}
131 define void @short_array(i32 addrspace(1)* %out, i32 %index) {
133 %0 = alloca [2 x i16]
134 %1 = getelementptr [2 x i16], [2 x i16]* %0, i32 0, i32 0
135 %2 = getelementptr [2 x i16], [2 x i16]* %0, i32 0, i32 1
138 %3 = getelementptr [2 x i16], [2 x i16]* %0, i32 0, i32 %index
139 %4 = load i16, i16* %3
140 %5 = sext i16 %4 to i32
141 store i32 %5, i32 addrspace(1)* %out
145 ; FUNC-LABEL: {{^}}char_array:
149 ; SI-DAG: buffer_store_byte v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen ; encoding: [0x00,0x10,0x60,0xe0
150 ; SI-DAG: buffer_store_byte v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen offset:1 ; encoding: [0x01,0x10,0x60,0xe0
151 define void @char_array(i32 addrspace(1)* %out, i32 %index) {
154 %1 = getelementptr [2 x i8], [2 x i8]* %0, i32 0, i32 0
155 %2 = getelementptr [2 x i8], [2 x i8]* %0, i32 0, i32 1
158 %3 = getelementptr [2 x i8], [2 x i8]* %0, i32 0, i32 %index
160 %5 = sext i8 %4 to i32
161 store i32 %5, i32 addrspace(1)* %out
166 ; Make sure we don't overwrite workitem information with private memory
168 ; FUNC-LABEL: {{^}}work_item_info:
170 ; Additional check in case the move ends up in the last slot
171 ; R600-NOT: MOV * TO.X
173 ; SI-NOT: v_mov_b32_e{{(32|64)}} v0
174 define void @work_item_info(i32 addrspace(1)* %out, i32 %in) {
176 %0 = alloca [2 x i32]
177 %1 = getelementptr [2 x i32], [2 x i32]* %0, i32 0, i32 0
178 %2 = getelementptr [2 x i32], [2 x i32]* %0, i32 0, i32 1
181 %3 = getelementptr [2 x i32], [2 x i32]* %0, i32 0, i32 %in
182 %4 = load i32, i32* %3
183 %5 = call i32 @llvm.r600.read.tidig.x()
185 store i32 %6, i32 addrspace(1)* %out
189 ; Test that two stack objects are not stored in the same register
190 ; The second stack object should be in T3.X
191 ; FUNC-LABEL: {{^}}no_overlap:
193 ; R600_CHECK: [[CHAN:[XYZW]]]+
194 ; R600-NOT: [[CHAN]]+
195 ; SI: v_mov_b32_e32 v3
196 define void @no_overlap(i32 addrspace(1)* %out, i32 %in) {
198 %0 = alloca [3 x i8], align 1
199 %1 = alloca [2 x i8], align 1
200 %2 = getelementptr [3 x i8], [3 x i8]* %0, i32 0, i32 0
201 %3 = getelementptr [3 x i8], [3 x i8]* %0, i32 0, i32 1
202 %4 = getelementptr [3 x i8], [3 x i8]* %0, i32 0, i32 2
203 %5 = getelementptr [2 x i8], [2 x i8]* %1, i32 0, i32 0
204 %6 = getelementptr [2 x i8], [2 x i8]* %1, i32 0, i32 1
210 %7 = getelementptr [3 x i8], [3 x i8]* %0, i32 0, i32 %in
211 %8 = getelementptr [2 x i8], [2 x i8]* %1, i32 0, i32 %in
213 %10 = load i8, i8* %8
215 %12 = sext i8 %11 to i32
216 store i32 %12, i32 addrspace(1)* %out
220 define void @char_array_array(i32 addrspace(1)* %out, i32 %index) {
222 %alloca = alloca [2 x [2 x i8]]
223 %gep0 = getelementptr [2 x [2 x i8]], [2 x [2 x i8]]* %alloca, i32 0, i32 0, i32 0
224 %gep1 = getelementptr [2 x [2 x i8]], [2 x [2 x i8]]* %alloca, i32 0, i32 0, i32 1
225 store i8 0, i8* %gep0
226 store i8 1, i8* %gep1
227 %gep2 = getelementptr [2 x [2 x i8]], [2 x [2 x i8]]* %alloca, i32 0, i32 0, i32 %index
228 %load = load i8, i8* %gep2
229 %sext = sext i8 %load to i32
230 store i32 %sext, i32 addrspace(1)* %out
234 define void @i32_array_array(i32 addrspace(1)* %out, i32 %index) {
236 %alloca = alloca [2 x [2 x i32]]
237 %gep0 = getelementptr [2 x [2 x i32]], [2 x [2 x i32]]* %alloca, i32 0, i32 0, i32 0
238 %gep1 = getelementptr [2 x [2 x i32]], [2 x [2 x i32]]* %alloca, i32 0, i32 0, i32 1
239 store i32 0, i32* %gep0
240 store i32 1, i32* %gep1
241 %gep2 = getelementptr [2 x [2 x i32]], [2 x [2 x i32]]* %alloca, i32 0, i32 0, i32 %index
242 %load = load i32, i32* %gep2
243 store i32 %load, i32 addrspace(1)* %out
247 define void @i64_array_array(i64 addrspace(1)* %out, i32 %index) {
249 %alloca = alloca [2 x [2 x i64]]
250 %gep0 = getelementptr [2 x [2 x i64]], [2 x [2 x i64]]* %alloca, i32 0, i32 0, i32 0
251 %gep1 = getelementptr [2 x [2 x i64]], [2 x [2 x i64]]* %alloca, i32 0, i32 0, i32 1
252 store i64 0, i64* %gep0
253 store i64 1, i64* %gep1
254 %gep2 = getelementptr [2 x [2 x i64]], [2 x [2 x i64]]* %alloca, i32 0, i32 0, i32 %index
255 %load = load i64, i64* %gep2
256 store i64 %load, i64 addrspace(1)* %out
260 %struct.pair32 = type { i32, i32 }
262 define void @struct_array_array(i32 addrspace(1)* %out, i32 %index) {
264 %alloca = alloca [2 x [2 x %struct.pair32]]
265 %gep0 = getelementptr [2 x [2 x %struct.pair32]], [2 x [2 x %struct.pair32]]* %alloca, i32 0, i32 0, i32 0, i32 1
266 %gep1 = getelementptr [2 x [2 x %struct.pair32]], [2 x [2 x %struct.pair32]]* %alloca, i32 0, i32 0, i32 1, i32 1
267 store i32 0, i32* %gep0
268 store i32 1, i32* %gep1
269 %gep2 = getelementptr [2 x [2 x %struct.pair32]], [2 x [2 x %struct.pair32]]* %alloca, i32 0, i32 0, i32 %index, i32 0
270 %load = load i32, i32* %gep2
271 store i32 %load, i32 addrspace(1)* %out
275 define void @struct_pair32_array(i32 addrspace(1)* %out, i32 %index) {
277 %alloca = alloca [2 x %struct.pair32]
278 %gep0 = getelementptr [2 x %struct.pair32], [2 x %struct.pair32]* %alloca, i32 0, i32 0, i32 1
279 %gep1 = getelementptr [2 x %struct.pair32], [2 x %struct.pair32]* %alloca, i32 0, i32 1, i32 0
280 store i32 0, i32* %gep0
281 store i32 1, i32* %gep1
282 %gep2 = getelementptr [2 x %struct.pair32], [2 x %struct.pair32]* %alloca, i32 0, i32 %index, i32 0
283 %load = load i32, i32* %gep2
284 store i32 %load, i32 addrspace(1)* %out
288 define void @select_private(i32 addrspace(1)* %out, i32 %in) nounwind {
290 %tmp = alloca [2 x i32]
291 %tmp1 = getelementptr [2 x i32], [2 x i32]* %tmp, i32 0, i32 0
292 %tmp2 = getelementptr [2 x i32], [2 x i32]* %tmp, i32 0, i32 1
293 store i32 0, i32* %tmp1
294 store i32 1, i32* %tmp2
295 %cmp = icmp eq i32 %in, 0
296 %sel = select i1 %cmp, i32* %tmp1, i32* %tmp2
297 %load = load i32, i32* %sel
298 store i32 %load, i32 addrspace(1)* %out
302 ; AMDGPUPromoteAlloca does not know how to handle ptrtoint. When it
303 ; finds one, it should stop trying to promote.
305 ; FUNC-LABEL: ptrtoint:
307 ; SI: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen
308 ; SI: buffer_load_dword v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen ;
309 define void @ptrtoint(i32 addrspace(1)* %out, i32 %a, i32 %b) {
310 %alloca = alloca [16 x i32]
311 %tmp0 = getelementptr [16 x i32], [16 x i32]* %alloca, i32 0, i32 %a
312 store i32 5, i32* %tmp0
313 %tmp1 = ptrtoint [16 x i32]* %alloca to i32
314 %tmp2 = add i32 %tmp1, 5
315 %tmp3 = inttoptr i32 %tmp2 to i32*
316 %tmp4 = getelementptr i32, i32* %tmp3, i32 %b
317 %tmp5 = load i32, i32* %tmp4
318 store i32 %tmp5, i32 addrspace(1)* %out