R600: Don't promote allocas when one of the users is a ptrtoint instruction
[oota-llvm.git] / test / CodeGen / R600 / 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=r600 -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=r600 -mcpu=SI < %s | FileCheck %s -check-prefix=SI-ALLOCA -check-prefix=SI -check-prefix=FUNC
4
5 declare i32 @llvm.r600.read.tidig.x() nounwind readnone
6
7 ; FUNC-LABEL: {{^}}mova_same_clause:
8
9 ; R600: LDS_WRITE
10 ; R600: LDS_WRITE
11 ; R600: LDS_READ
12 ; R600: LDS_READ
13
14 ; SI-PROMOTE: DS_WRITE_B32
15 ; SI-PROMOTE: DS_WRITE_B32
16 ; SI-PROMOTE: DS_READ_B32
17 ; SI-PROMOTE: DS_READ_B32
18
19 ; 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
20 ; 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
21 define void @mova_same_clause(i32 addrspace(1)* nocapture %out, i32 addrspace(1)* nocapture %in) {
22 entry:
23   %stack = alloca [5 x i32], align 4
24   %0 = load i32 addrspace(1)* %in, align 4
25   %arrayidx1 = getelementptr inbounds [5 x i32]* %stack, i32 0, i32 %0
26   store i32 4, i32* %arrayidx1, align 4
27   %arrayidx2 = getelementptr inbounds i32 addrspace(1)* %in, i32 1
28   %1 = load i32 addrspace(1)* %arrayidx2, align 4
29   %arrayidx3 = getelementptr inbounds [5 x i32]* %stack, i32 0, i32 %1
30   store i32 5, i32* %arrayidx3, align 4
31   %arrayidx10 = getelementptr inbounds [5 x i32]* %stack, i32 0, i32 0
32   %2 = load i32* %arrayidx10, align 4
33   store i32 %2, i32 addrspace(1)* %out, align 4
34   %arrayidx12 = getelementptr inbounds [5 x i32]* %stack, i32 0, i32 1
35   %3 = load i32* %arrayidx12
36   %arrayidx13 = getelementptr inbounds i32 addrspace(1)* %out, i32 1
37   store i32 %3, i32 addrspace(1)* %arrayidx13
38   ret void
39 }
40
41 ; This test checks that the stack offset is calculated correctly for structs.
42 ; All register loads/stores should be optimized away, so there shouldn't be
43 ; any MOVA instructions.
44 ;
45 ; XXX: This generated code has unnecessary MOVs, we should be able to optimize
46 ; this.
47
48 ; FUNC-LABEL: {{^}}multiple_structs:
49 ; R600-NOT: MOVA_INT
50 ; SI-NOT: V_MOVREL
51 ; SI-NOT: V_MOVREL
52 %struct.point = type { i32, i32 }
53
54 define void @multiple_structs(i32 addrspace(1)* %out) {
55 entry:
56   %a = alloca %struct.point
57   %b = alloca %struct.point
58   %a.x.ptr = getelementptr %struct.point* %a, i32 0, i32 0
59   %a.y.ptr = getelementptr %struct.point* %a, i32 0, i32 1
60   %b.x.ptr = getelementptr %struct.point* %b, i32 0, i32 0
61   %b.y.ptr = getelementptr %struct.point* %b, i32 0, i32 1
62   store i32 0, i32* %a.x.ptr
63   store i32 1, i32* %a.y.ptr
64   store i32 2, i32* %b.x.ptr
65   store i32 3, i32* %b.y.ptr
66   %a.indirect.ptr = getelementptr %struct.point* %a, i32 0, i32 0
67   %b.indirect.ptr = getelementptr %struct.point* %b, i32 0, i32 0
68   %a.indirect = load i32* %a.indirect.ptr
69   %b.indirect = load i32* %b.indirect.ptr
70   %0 = add i32 %a.indirect, %b.indirect
71   store i32 %0, i32 addrspace(1)* %out
72   ret void
73 }
74
75 ; Test direct access of a private array inside a loop.  The private array
76 ; loads and stores should be lowered to copies, so there shouldn't be any
77 ; MOVA instructions.
78
79 ; FUNC-LABEL: {{^}}direct_loop:
80 ; R600-NOT: MOVA_INT
81 ; SI-NOT: V_MOVREL
82
83 define void @direct_loop(i32 addrspace(1)* %out, i32 addrspace(1)* %in) {
84 entry:
85   %prv_array_const = alloca [2 x i32]
86   %prv_array = alloca [2 x i32]
87   %a = load i32 addrspace(1)* %in
88   %b_src_ptr = getelementptr i32 addrspace(1)* %in, i32 1
89   %b = load i32 addrspace(1)* %b_src_ptr
90   %a_dst_ptr = getelementptr [2 x i32]* %prv_array_const, i32 0, i32 0
91   store i32 %a, i32* %a_dst_ptr
92   %b_dst_ptr = getelementptr [2 x i32]* %prv_array_const, i32 0, i32 1
93   store i32 %b, i32* %b_dst_ptr
94   br label %for.body
95
96 for.body:
97   %inc = phi i32 [0, %entry], [%count, %for.body]
98   %x_ptr = getelementptr [2 x i32]* %prv_array_const, i32 0, i32 0
99   %x = load i32* %x_ptr
100   %y_ptr = getelementptr [2 x i32]* %prv_array, i32 0, i32 0
101   %y = load i32* %y_ptr
102   %xy = add i32 %x, %y
103   store i32 %xy, i32* %y_ptr
104   %count = add i32 %inc, 1
105   %done = icmp eq i32 %count, 4095
106   br i1 %done, label %for.end, label %for.body
107
108 for.end:
109   %value_ptr = getelementptr [2 x i32]* %prv_array, i32 0, i32 0
110   %value = load i32* %value_ptr
111   store i32 %value, i32 addrspace(1)* %out
112   ret void
113 }
114
115 ; FUNC-LABEL: {{^}}short_array:
116
117 ; R600: MOVA_INT
118
119 ; 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
120 ; SI-PROMOTE-DAG: BUFFER_STORE_SHORT v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen offset:0x2 ; encoding: [0x02,0x10,0x68,0xe0
121 ; SI-PROMOTE: BUFFER_LOAD_SSHORT v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}}
122 define void @short_array(i32 addrspace(1)* %out, i32 %index) {
123 entry:
124   %0 = alloca [2 x i16]
125   %1 = getelementptr [2 x i16]* %0, i32 0, i32 0
126   %2 = getelementptr [2 x i16]* %0, i32 0, i32 1
127   store i16 0, i16* %1
128   store i16 1, i16* %2
129   %3 = getelementptr [2 x i16]* %0, i32 0, i32 %index
130   %4 = load i16* %3
131   %5 = sext i16 %4 to i32
132   store i32 %5, i32 addrspace(1)* %out
133   ret void
134 }
135
136 ; FUNC-LABEL: {{^}}char_array:
137
138 ; R600: MOVA_INT
139
140 ; 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
141 ; SI-DAG: BUFFER_STORE_BYTE v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen offset:0x1 ; encoding: [0x01,0x10,0x60,0xe0
142 define void @char_array(i32 addrspace(1)* %out, i32 %index) {
143 entry:
144   %0 = alloca [2 x i8]
145   %1 = getelementptr [2 x i8]* %0, i32 0, i32 0
146   %2 = getelementptr [2 x i8]* %0, i32 0, i32 1
147   store i8 0, i8* %1
148   store i8 1, i8* %2
149   %3 = getelementptr [2 x i8]* %0, i32 0, i32 %index
150   %4 = load i8* %3
151   %5 = sext i8 %4 to i32
152   store i32 %5, i32 addrspace(1)* %out
153   ret void
154
155 }
156
157 ; Make sure we don't overwrite workitem information with private memory
158
159 ; FUNC-LABEL: {{^}}work_item_info:
160 ; R600-NOT: MOV T0.X
161 ; Additional check in case the move ends up in the last slot
162 ; R600-NOT: MOV * TO.X
163
164 ; SI-NOT: V_MOV_B32_e{{(32|64)}} v0
165 define void @work_item_info(i32 addrspace(1)* %out, i32 %in) {
166 entry:
167   %0 = alloca [2 x i32]
168   %1 = getelementptr [2 x i32]* %0, i32 0, i32 0
169   %2 = getelementptr [2 x i32]* %0, i32 0, i32 1
170   store i32 0, i32* %1
171   store i32 1, i32* %2
172   %3 = getelementptr [2 x i32]* %0, i32 0, i32 %in
173   %4 = load i32* %3
174   %5 = call i32 @llvm.r600.read.tidig.x()
175   %6 = add i32 %4, %5
176   store i32 %6, i32 addrspace(1)* %out
177   ret void
178 }
179
180 ; Test that two stack objects are not stored in the same register
181 ; The second stack object should be in T3.X
182 ; FUNC-LABEL: {{^}}no_overlap:
183 ; R600_CHECK: MOV
184 ; R600_CHECK: [[CHAN:[XYZW]]]+
185 ; R600-NOT: [[CHAN]]+
186 ; SI: V_MOV_B32_e32 v3
187 define void @no_overlap(i32 addrspace(1)* %out, i32 %in) {
188 entry:
189   %0 = alloca [3 x i8], align 1
190   %1 = alloca [2 x i8], align 1
191   %2 = getelementptr [3 x i8]* %0, i32 0, i32 0
192   %3 = getelementptr [3 x i8]* %0, i32 0, i32 1
193   %4 = getelementptr [3 x i8]* %0, i32 0, i32 2
194   %5 = getelementptr [2 x i8]* %1, i32 0, i32 0
195   %6 = getelementptr [2 x i8]* %1, i32 0, i32 1
196   store i8 0, i8* %2
197   store i8 1, i8* %3
198   store i8 2, i8* %4
199   store i8 1, i8* %5
200   store i8 0, i8* %6
201   %7 = getelementptr [3 x i8]* %0, i32 0, i32 %in
202   %8 = getelementptr [2 x i8]* %1, i32 0, i32 %in
203   %9 = load i8* %7
204   %10 = load i8* %8
205   %11 = add i8 %9, %10
206   %12 = sext i8 %11 to i32
207   store i32 %12, i32 addrspace(1)* %out
208   ret void
209 }
210
211 define void @char_array_array(i32 addrspace(1)* %out, i32 %index) {
212 entry:
213   %alloca = alloca [2 x [2 x i8]]
214   %gep0 = getelementptr [2 x [2 x i8]]* %alloca, i32 0, i32 0, i32 0
215   %gep1 = getelementptr [2 x [2 x i8]]* %alloca, i32 0, i32 0, i32 1
216   store i8 0, i8* %gep0
217   store i8 1, i8* %gep1
218   %gep2 = getelementptr [2 x [2 x i8]]* %alloca, i32 0, i32 0, i32 %index
219   %load = load i8* %gep2
220   %sext = sext i8 %load to i32
221   store i32 %sext, i32 addrspace(1)* %out
222   ret void
223 }
224
225 define void @i32_array_array(i32 addrspace(1)* %out, i32 %index) {
226 entry:
227   %alloca = alloca [2 x [2 x i32]]
228   %gep0 = getelementptr [2 x [2 x i32]]* %alloca, i32 0, i32 0, i32 0
229   %gep1 = getelementptr [2 x [2 x i32]]* %alloca, i32 0, i32 0, i32 1
230   store i32 0, i32* %gep0
231   store i32 1, i32* %gep1
232   %gep2 = getelementptr [2 x [2 x i32]]* %alloca, i32 0, i32 0, i32 %index
233   %load = load i32* %gep2
234   store i32 %load, i32 addrspace(1)* %out
235   ret void
236 }
237
238 define void @i64_array_array(i64 addrspace(1)* %out, i32 %index) {
239 entry:
240   %alloca = alloca [2 x [2 x i64]]
241   %gep0 = getelementptr [2 x [2 x i64]]* %alloca, i32 0, i32 0, i32 0
242   %gep1 = getelementptr [2 x [2 x i64]]* %alloca, i32 0, i32 0, i32 1
243   store i64 0, i64* %gep0
244   store i64 1, i64* %gep1
245   %gep2 = getelementptr [2 x [2 x i64]]* %alloca, i32 0, i32 0, i32 %index
246   %load = load i64* %gep2
247   store i64 %load, i64 addrspace(1)* %out
248   ret void
249 }
250
251 %struct.pair32 = type { i32, i32 }
252
253 define void @struct_array_array(i32 addrspace(1)* %out, i32 %index) {
254 entry:
255   %alloca = alloca [2 x [2 x %struct.pair32]]
256   %gep0 = getelementptr [2 x [2 x %struct.pair32]]* %alloca, i32 0, i32 0, i32 0, i32 1
257   %gep1 = getelementptr [2 x [2 x %struct.pair32]]* %alloca, i32 0, i32 0, i32 1, i32 1
258   store i32 0, i32* %gep0
259   store i32 1, i32* %gep1
260   %gep2 = getelementptr [2 x [2 x %struct.pair32]]* %alloca, i32 0, i32 0, i32 %index, i32 0
261   %load = load i32* %gep2
262   store i32 %load, i32 addrspace(1)* %out
263   ret void
264 }
265
266 define void @struct_pair32_array(i32 addrspace(1)* %out, i32 %index) {
267 entry:
268   %alloca = alloca [2 x %struct.pair32]
269   %gep0 = getelementptr [2 x %struct.pair32]* %alloca, i32 0, i32 0, i32 1
270   %gep1 = getelementptr [2 x %struct.pair32]* %alloca, i32 0, i32 1, i32 0
271   store i32 0, i32* %gep0
272   store i32 1, i32* %gep1
273   %gep2 = getelementptr [2 x %struct.pair32]* %alloca, i32 0, i32 %index, i32 0
274   %load = load i32* %gep2
275   store i32 %load, i32 addrspace(1)* %out
276   ret void
277 }
278
279 define void @select_private(i32 addrspace(1)* %out, i32 %in) nounwind {
280 entry:
281   %tmp = alloca [2 x i32]
282   %tmp1 = getelementptr [2 x i32]* %tmp, i32 0, i32 0
283   %tmp2 = getelementptr [2 x i32]* %tmp, i32 0, i32 1
284   store i32 0, i32* %tmp1
285   store i32 1, i32* %tmp2
286   %cmp = icmp eq i32 %in, 0
287   %sel = select i1 %cmp, i32* %tmp1, i32* %tmp2
288   %load = load i32* %sel
289   store i32 %load, i32 addrspace(1)* %out
290   ret void
291 }
292
293 ; AMDGPUPromoteAlloca does not know how to handle ptrtoint.  When it
294 ; finds one, it should stop trying to promote.
295
296 ; FUNC-LABEL: ptrtoint:
297 ; SI-NOT: DS_WRITE
298 ; SI: BUFFER_STORE_DWORD v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen
299 ; SI: BUFFER_LOAD_DWORD v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen offset:0x5
300 define void @ptrtoint(i32 addrspace(1)* %out, i32 %a, i32 %b) {
301   %alloca = alloca [16 x i32]
302   %tmp0 = getelementptr [16 x i32]* %alloca, i32 0, i32 %a
303   store i32 5, i32* %tmp0
304   %tmp1 = ptrtoint [16 x i32]* %alloca to i32
305   %tmp2 = add i32 %tmp1, 5
306   %tmp3 = inttoptr i32 %tmp2 to i32*
307   %tmp4 = getelementptr i32* %tmp3, i32 %b
308   %tmp5 = load i32* %tmp4
309   store i32 %tmp5, i32 addrspace(1)* %out
310   ret void
311 }