628b8a21b819474d72dd340be38c3f0edfb33724
[oota-llvm.git] / test / CodeGen / AMDGPU / private-memory.ll
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
7
8 declare i32 @llvm.r600.read.tidig.x() nounwind readnone
9
10 ; FUNC-LABEL: {{^}}mova_same_clause:
11
12 ; R600: LDS_WRITE
13 ; R600: LDS_WRITE
14 ; R600: LDS_READ
15 ; R600: LDS_READ
16
17 ; SI-PROMOTE: ds_write_b32
18 ; SI-PROMOTE: ds_write_b32
19 ; SI-PROMOTE: ds_read_b32
20 ; SI-PROMOTE: ds_read_b32
21
22 ; HSA-ALLOCA: .amd_kernel_code_t
23 ; FIXME: Creating the emergency stack slots causes us to over-estimate scratch
24 ; by 4 bytes.
25 ; HSA-ALLOCA: workitem_private_segment_byte_size = 24
26 ; HSA-ALLOCA: .end_amd_kernel_code_t
27
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) {
31 entry:
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
47   ret void
48 }
49
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.
53 ;
54 ; XXX: This generated code has unnecessary MOVs, we should be able to optimize
55 ; this.
56
57 ; FUNC-LABEL: {{^}}multiple_structs:
58 ; R600-NOT: MOVA_INT
59 ; SI-NOT: v_movrel
60 ; SI-NOT: v_movrel
61 %struct.point = type { i32, i32 }
62
63 define void @multiple_structs(i32 addrspace(1)* %out) {
64 entry:
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
81   ret void
82 }
83
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
86 ; MOVA instructions.
87
88 ; FUNC-LABEL: {{^}}direct_loop:
89 ; R600-NOT: MOVA_INT
90 ; SI-NOT: v_movrel
91
92 define void @direct_loop(i32 addrspace(1)* %out, i32 addrspace(1)* %in) {
93 entry:
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
103   br label %for.body
104
105 for.body:
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
111   %xy = add i32 %x, %y
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
116
117 for.end:
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
121   ret void
122 }
123
124 ; FUNC-LABEL: {{^}}short_array:
125
126 ; R600: MOVA_INT
127
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) {
132 entry:
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
136   store i16 0, i16* %1
137   store i16 1, i16* %2
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
142   ret void
143 }
144
145 ; FUNC-LABEL: {{^}}char_array:
146
147 ; R600: MOVA_INT
148
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) {
152 entry:
153   %0 = alloca [2 x i8]
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
156   store i8 0, i8* %1
157   store i8 1, i8* %2
158   %3 = getelementptr [2 x i8], [2 x i8]* %0, i32 0, i32 %index
159   %4 = load i8, i8* %3
160   %5 = sext i8 %4 to i32
161   store i32 %5, i32 addrspace(1)* %out
162   ret void
163
164 }
165
166 ; Make sure we don't overwrite workitem information with private memory
167
168 ; FUNC-LABEL: {{^}}work_item_info:
169 ; R600-NOT: MOV T0.X
170 ; Additional check in case the move ends up in the last slot
171 ; R600-NOT: MOV * TO.X
172
173 ; SI-NOT: v_mov_b32_e{{(32|64)}} v0
174 define void @work_item_info(i32 addrspace(1)* %out, i32 %in) {
175 entry:
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
179   store i32 0, i32* %1
180   store i32 1, i32* %2
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()
184   %6 = add i32 %4, %5
185   store i32 %6, i32 addrspace(1)* %out
186   ret void
187 }
188
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:
192 ; R600_CHECK: MOV
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) {
197 entry:
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
205   store i8 0, i8* %2
206   store i8 1, i8* %3
207   store i8 2, i8* %4
208   store i8 1, i8* %5
209   store i8 0, i8* %6
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
212   %9 = load i8, i8* %7
213   %10 = load i8, i8* %8
214   %11 = add i8 %9, %10
215   %12 = sext i8 %11 to i32
216   store i32 %12, i32 addrspace(1)* %out
217   ret void
218 }
219
220 define void @char_array_array(i32 addrspace(1)* %out, i32 %index) {
221 entry:
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
231   ret void
232 }
233
234 define void @i32_array_array(i32 addrspace(1)* %out, i32 %index) {
235 entry:
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
244   ret void
245 }
246
247 define void @i64_array_array(i64 addrspace(1)* %out, i32 %index) {
248 entry:
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
257   ret void
258 }
259
260 %struct.pair32 = type { i32, i32 }
261
262 define void @struct_array_array(i32 addrspace(1)* %out, i32 %index) {
263 entry:
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
272   ret void
273 }
274
275 define void @struct_pair32_array(i32 addrspace(1)* %out, i32 %index) {
276 entry:
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
285   ret void
286 }
287
288 define void @select_private(i32 addrspace(1)* %out, i32 %in) nounwind {
289 entry:
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
299   ret void
300 }
301
302 ; AMDGPUPromoteAlloca does not know how to handle ptrtoint.  When it
303 ; finds one, it should stop trying to promote.
304
305 ; FUNC-LABEL: ptrtoint:
306 ; SI-NOT: ds_write
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
319   ret void
320 }