1; RUN: llc -show-mc-encoding -mattr=+promote-alloca -amdgpu-load-store-vectorizer=0 -verify-machineinstrs -march=amdgcn < %s | FileCheck %s -check-prefix=SI-PROMOTE -check-prefix=SI -check-prefix=FUNC
2; RUN: llc -show-mc-encoding -mattr=+promote-alloca -amdgpu-load-store-vectorizer=0 -verify-machineinstrs -mtriple=amdgcn--amdhsa -mcpu=kaveri -mattr=-unaligned-buffer-access < %s | FileCheck %s -check-prefix=SI-PROMOTE -check-prefix=SI -check-prefix=FUNC -check-prefix=HSA-PROMOTE
3; RUN: llc -show-mc-encoding -mattr=-promote-alloca -amdgpu-load-store-vectorizer=0 -verify-machineinstrs -march=amdgcn < %s | FileCheck %s -check-prefix=SI-ALLOCA -check-prefix=SI -check-prefix=FUNC
4; RUN: llc -show-mc-encoding -mattr=-promote-alloca -amdgpu-load-store-vectorizer=0 -verify-machineinstrs -mtriple=amdgcn-amdhsa -mcpu=kaveri -mattr=-unaligned-buffer-access < %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 -amdgpu-load-store-vectorizer=0 -verify-machineinstrs -mtriple=amdgcn-amdhsa -march=amdgcn -mcpu=tonga -mattr=-unaligned-buffer-access < %s | FileCheck %s -check-prefix=SI-PROMOTE -check-prefix=SI -check-prefix=FUNC
6; RUN: llc -show-mc-encoding -mattr=-promote-alloca -amdgpu-load-store-vectorizer=0 -verify-machineinstrs -mtriple=amdgcn-amdhsa -march=amdgcn -mcpu=tonga -mattr=-unaligned-buffer-access < %s | FileCheck %s -check-prefix=SI-ALLOCA -check-prefix=SI -check-prefix=FUNC
7
8; RUN: opt -S -mtriple=amdgcn-unknown-amdhsa -mcpu=kaveri -amdgpu-promote-alloca < %s | FileCheck -check-prefix=HSAOPT -check-prefix=OPT %s
9; RUN: opt -S -mtriple=amdgcn-unknown-unknown -mcpu=kaveri -amdgpu-promote-alloca < %s | FileCheck -check-prefix=NOHSAOPT -check-prefix=OPT %s
10
11; RUN: llc -march=r600 -mcpu=cypress < %s | FileCheck %s -check-prefix=R600 -check-prefix=FUNC
12
13
14; HSAOPT: @mova_same_clause.stack = internal unnamed_addr addrspace(3) global [256 x [5 x i32]] undef, align 4
15; HSAOPT: @high_alignment.stack = internal unnamed_addr addrspace(3) global [256 x [8 x i32]] undef, align 16
16
17
18; FUNC-LABEL: {{^}}mova_same_clause:
19; OPT-LABEL: @mova_same_clause(
20
21; R600: LDS_WRITE
22; R600: LDS_WRITE
23; R600: LDS_READ
24; R600: LDS_READ
25
26; HSA-PROMOTE: .amd_kernel_code_t
27; HSA-PROMOTE: workgroup_group_segment_byte_size = 5120
28; HSA-PROMOTE: .end_amd_kernel_code_t
29
30; FIXME: These should be merged
31; HSA-PROMOTE: s_load_dword s{{[0-9]+}}, s[4:5], 0x1
32; HSA-PROMOTE: s_load_dword s{{[0-9]+}}, s[4:5], 0x2
33
34; SI-PROMOTE: ds_write_b32
35; SI-PROMOTE: ds_write_b32
36; SI-PROMOTE: ds_read_b32
37; SI-PROMOTE: ds_read_b32
38
39; HSA-ALLOCA: .amd_kernel_code_t
40; FIXME: Creating the emergency stack slots causes us to over-estimate scratch
41; by 4 bytes.
42; HSA-ALLOCA: workitem_private_segment_byte_size = 24
43; HSA-ALLOCA: .end_amd_kernel_code_t
44
45; HSA-ALLOCA: s_mov_b32 flat_scratch_lo, s7
46; HSA-ALLOCA: s_add_u32 s6, s6, s9
47; HSA-ALLOCA: s_lshr_b32 flat_scratch_hi, s6, 8
48
49; 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
50; 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
51
52
53; HSAOPT: [[DISPATCH_PTR:%[0-9]+]] = call noalias nonnull dereferenceable(64) i8 addrspace(2)* @llvm.amdgcn.dispatch.ptr()
54; HSAOPT: [[CAST_DISPATCH_PTR:%[0-9]+]] = bitcast i8 addrspace(2)* [[DISPATCH_PTR]] to i32 addrspace(2)*
55; HSAOPT: [[GEP0:%[0-9]+]] = getelementptr inbounds i32, i32 addrspace(2)* [[CAST_DISPATCH_PTR]], i64 1
56; HSAOPT: [[LDXY:%[0-9]+]] = load i32, i32 addrspace(2)* [[GEP0]], align 4, !invariant.load !0
57; HSAOPT: [[GEP1:%[0-9]+]] = getelementptr inbounds i32, i32 addrspace(2)* [[CAST_DISPATCH_PTR]], i64 2
58; HSAOPT: [[LDZU:%[0-9]+]] = load i32, i32 addrspace(2)* [[GEP1]], align 4, !range !1, !invariant.load !0
59; HSAOPT: [[EXTRACTY:%[0-9]+]] = lshr i32 [[LDXY]], 16
60
61; HSAOPT: [[WORKITEM_ID_X:%[0-9]+]] = call i32 @llvm.amdgcn.workitem.id.x(), !range !1
62; HSAOPT: [[WORKITEM_ID_Y:%[0-9]+]] = call i32 @llvm.amdgcn.workitem.id.y(), !range !1
63; HSAOPT: [[WORKITEM_ID_Z:%[0-9]+]] = call i32 @llvm.amdgcn.workitem.id.z(), !range !1
64
65; HSAOPT: [[Y_SIZE_X_Z_SIZE:%[0-9]+]] = mul nuw nsw i32 [[EXTRACTY]], [[LDZU]]
66; HSAOPT: [[YZ_X_XID:%[0-9]+]] = mul i32 [[Y_SIZE_X_Z_SIZE]], [[WORKITEM_ID_X]]
67; HSAOPT: [[Y_X_Z_SIZE:%[0-9]+]] = mul nuw nsw i32 [[WORKITEM_ID_Y]], [[LDZU]]
68; HSAOPT: [[ADD_YZ_X_X_YZ_SIZE:%[0-9]+]] = add i32 [[YZ_X_XID]], [[Y_X_Z_SIZE]]
69; HSAOPT: [[ADD_ZID:%[0-9]+]] = add i32 [[ADD_YZ_X_X_YZ_SIZE]], [[WORKITEM_ID_Z]]
70
71; HSAOPT: [[LOCAL_GEP:%[0-9]+]] = getelementptr inbounds [256 x [5 x i32]], [256 x [5 x i32]] addrspace(3)* @mova_same_clause.stack, i32 0, i32 [[ADD_ZID]]
72; HSAOPT: %arrayidx1 = getelementptr inbounds [5 x i32], [5 x i32] addrspace(3)* [[LOCAL_GEP]], i32 0, i32 {{%[0-9]+}}
73; HSAOPT: %arrayidx3 = getelementptr inbounds [5 x i32], [5 x i32] addrspace(3)* [[LOCAL_GEP]], i32 0, i32 {{%[0-9]+}}
74; HSAOPT: %arrayidx10 = getelementptr inbounds [5 x i32], [5 x i32] addrspace(3)* [[LOCAL_GEP]], i32 0, i32 0
75; HSAOPT: %arrayidx12 = getelementptr inbounds [5 x i32], [5 x i32] addrspace(3)* [[LOCAL_GEP]], i32 0, i32 1
76
77
78; NOHSAOPT: call i32 @llvm.r600.read.local.size.y(), !range !0
79; NOHSAOPT: call i32 @llvm.r600.read.local.size.z(), !range !0
80; NOHSAOPT: call i32 @llvm.amdgcn.workitem.id.x(), !range !0
81; NOHSAOPT: call i32 @llvm.amdgcn.workitem.id.y(), !range !0
82; NOHSAOPT: call i32 @llvm.amdgcn.workitem.id.z(), !range !0
83define void @mova_same_clause(i32 addrspace(1)* nocapture %out, i32 addrspace(1)* nocapture %in) #0 {
84entry:
85  %stack = alloca [5 x i32], align 4
86  %0 = load i32, i32 addrspace(1)* %in, align 4
87  %arrayidx1 = getelementptr inbounds [5 x i32], [5 x i32]* %stack, i32 0, i32 %0
88  store i32 4, i32* %arrayidx1, align 4
89  %arrayidx2 = getelementptr inbounds i32, i32 addrspace(1)* %in, i32 1
90  %1 = load i32, i32 addrspace(1)* %arrayidx2, align 4
91  %arrayidx3 = getelementptr inbounds [5 x i32], [5 x i32]* %stack, i32 0, i32 %1
92  store i32 5, i32* %arrayidx3, align 4
93  %arrayidx10 = getelementptr inbounds [5 x i32], [5 x i32]* %stack, i32 0, i32 0
94  %2 = load i32, i32* %arrayidx10, align 4
95  store i32 %2, i32 addrspace(1)* %out, align 4
96  %arrayidx12 = getelementptr inbounds [5 x i32], [5 x i32]* %stack, i32 0, i32 1
97  %3 = load i32, i32* %arrayidx12
98  %arrayidx13 = getelementptr inbounds i32, i32 addrspace(1)* %out, i32 1
99  store i32 %3, i32 addrspace(1)* %arrayidx13
100  ret void
101}
102
103; OPT-LABEL: @high_alignment(
104; OPT: getelementptr inbounds [256 x [8 x i32]], [256 x [8 x i32]] addrspace(3)* @high_alignment.stack, i32 0, i32 %{{[0-9]+}}
105define void @high_alignment(i32 addrspace(1)* nocapture %out, i32 addrspace(1)* nocapture %in) #0 {
106entry:
107  %stack = alloca [8 x i32], align 16
108  %0 = load i32, i32 addrspace(1)* %in, align 4
109  %arrayidx1 = getelementptr inbounds [8 x i32], [8 x i32]* %stack, i32 0, i32 %0
110  store i32 4, i32* %arrayidx1, align 4
111  %arrayidx2 = getelementptr inbounds i32, i32 addrspace(1)* %in, i32 1
112  %1 = load i32, i32 addrspace(1)* %arrayidx2, align 4
113  %arrayidx3 = getelementptr inbounds [8 x i32], [8 x i32]* %stack, i32 0, i32 %1
114  store i32 5, i32* %arrayidx3, align 4
115  %arrayidx10 = getelementptr inbounds [8 x i32], [8 x i32]* %stack, i32 0, i32 0
116  %2 = load i32, i32* %arrayidx10, align 4
117  store i32 %2, i32 addrspace(1)* %out, align 4
118  %arrayidx12 = getelementptr inbounds [8 x i32], [8 x i32]* %stack, i32 0, i32 1
119  %3 = load i32, i32* %arrayidx12
120  %arrayidx13 = getelementptr inbounds i32, i32 addrspace(1)* %out, i32 1
121  store i32 %3, i32 addrspace(1)* %arrayidx13
122  ret void
123}
124
125; FUNC-LABEL: {{^}}no_replace_inbounds_gep:
126; OPT-LABEL: @no_replace_inbounds_gep(
127; OPT: alloca [5 x i32]
128
129; SI-NOT: ds_write
130define void @no_replace_inbounds_gep(i32 addrspace(1)* nocapture %out, i32 addrspace(1)* nocapture %in) #0 {
131entry:
132  %stack = alloca [5 x i32], align 4
133  %0 = load i32, i32 addrspace(1)* %in, align 4
134  %arrayidx1 = getelementptr [5 x i32], [5 x i32]* %stack, i32 0, i32 %0
135  store i32 4, i32* %arrayidx1, align 4
136  %arrayidx2 = getelementptr inbounds i32, i32 addrspace(1)* %in, i32 1
137  %1 = load i32, i32 addrspace(1)* %arrayidx2, align 4
138  %arrayidx3 = getelementptr inbounds [5 x i32], [5 x i32]* %stack, i32 0, i32 %1
139  store i32 5, i32* %arrayidx3, align 4
140  %arrayidx10 = getelementptr inbounds [5 x i32], [5 x i32]* %stack, i32 0, i32 0
141  %2 = load i32, i32* %arrayidx10, align 4
142  store i32 %2, i32 addrspace(1)* %out, align 4
143  %arrayidx12 = getelementptr inbounds [5 x i32], [5 x i32]* %stack, i32 0, i32 1
144  %3 = load i32, i32* %arrayidx12
145  %arrayidx13 = getelementptr inbounds i32, i32 addrspace(1)* %out, i32 1
146  store i32 %3, i32 addrspace(1)* %arrayidx13
147  ret void
148}
149
150; This test checks that the stack offset is calculated correctly for structs.
151; All register loads/stores should be optimized away, so there shouldn't be
152; any MOVA instructions.
153;
154; XXX: This generated code has unnecessary MOVs, we should be able to optimize
155; this.
156
157; FUNC-LABEL: {{^}}multiple_structs:
158; OPT-LABEL: @multiple_structs(
159
160; R600-NOT: MOVA_INT
161; SI-NOT: v_movrel
162; SI-NOT: v_movrel
163%struct.point = type { i32, i32 }
164
165define void @multiple_structs(i32 addrspace(1)* %out) #0 {
166entry:
167  %a = alloca %struct.point
168  %b = alloca %struct.point
169  %a.x.ptr = getelementptr %struct.point, %struct.point* %a, i32 0, i32 0
170  %a.y.ptr = getelementptr %struct.point, %struct.point* %a, i32 0, i32 1
171  %b.x.ptr = getelementptr %struct.point, %struct.point* %b, i32 0, i32 0
172  %b.y.ptr = getelementptr %struct.point, %struct.point* %b, i32 0, i32 1
173  store i32 0, i32* %a.x.ptr
174  store i32 1, i32* %a.y.ptr
175  store i32 2, i32* %b.x.ptr
176  store i32 3, i32* %b.y.ptr
177  %a.indirect.ptr = getelementptr %struct.point, %struct.point* %a, i32 0, i32 0
178  %b.indirect.ptr = getelementptr %struct.point, %struct.point* %b, i32 0, i32 0
179  %a.indirect = load i32, i32* %a.indirect.ptr
180  %b.indirect = load i32, i32* %b.indirect.ptr
181  %0 = add i32 %a.indirect, %b.indirect
182  store i32 %0, i32 addrspace(1)* %out
183  ret void
184}
185
186; Test direct access of a private array inside a loop.  The private array
187; loads and stores should be lowered to copies, so there shouldn't be any
188; MOVA instructions.
189
190; FUNC-LABEL: {{^}}direct_loop:
191; R600-NOT: MOVA_INT
192; SI-NOT: v_movrel
193
194define void @direct_loop(i32 addrspace(1)* %out, i32 addrspace(1)* %in) #0 {
195entry:
196  %prv_array_const = alloca [2 x i32]
197  %prv_array = alloca [2 x i32]
198  %a = load i32, i32 addrspace(1)* %in
199  %b_src_ptr = getelementptr inbounds i32, i32 addrspace(1)* %in, i32 1
200  %b = load i32, i32 addrspace(1)* %b_src_ptr
201  %a_dst_ptr = getelementptr inbounds [2 x i32], [2 x i32]* %prv_array_const, i32 0, i32 0
202  store i32 %a, i32* %a_dst_ptr
203  %b_dst_ptr = getelementptr inbounds [2 x i32], [2 x i32]* %prv_array_const, i32 0, i32 1
204  store i32 %b, i32* %b_dst_ptr
205  br label %for.body
206
207for.body:
208  %inc = phi i32 [0, %entry], [%count, %for.body]
209  %x_ptr = getelementptr inbounds [2 x i32], [2 x i32]* %prv_array_const, i32 0, i32 0
210  %x = load i32, i32* %x_ptr
211  %y_ptr = getelementptr inbounds [2 x i32], [2 x i32]* %prv_array, i32 0, i32 0
212  %y = load i32, i32* %y_ptr
213  %xy = add i32 %x, %y
214  store i32 %xy, i32* %y_ptr
215  %count = add i32 %inc, 1
216  %done = icmp eq i32 %count, 4095
217  br i1 %done, label %for.end, label %for.body
218
219for.end:
220  %value_ptr = getelementptr inbounds [2 x i32], [2 x i32]* %prv_array, i32 0, i32 0
221  %value = load i32, i32* %value_ptr
222  store i32 %value, i32 addrspace(1)* %out
223  ret void
224}
225
226; FUNC-LABEL: {{^}}short_array:
227
228; R600: MOVA_INT
229
230; 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
231; 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
232; Loaded value is 0 or 1, so sext will become zext, so we get buffer_load_ushort instead of buffer_load_sshort.
233; SI-PROMOTE: buffer_load_ushort v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}}
234define void @short_array(i32 addrspace(1)* %out, i32 %index) #0 {
235entry:
236  %0 = alloca [2 x i16]
237  %1 = getelementptr inbounds [2 x i16], [2 x i16]* %0, i32 0, i32 0
238  %2 = getelementptr inbounds [2 x i16], [2 x i16]* %0, i32 0, i32 1
239  store i16 0, i16* %1
240  store i16 1, i16* %2
241  %3 = getelementptr inbounds [2 x i16], [2 x i16]* %0, i32 0, i32 %index
242  %4 = load i16, i16* %3
243  %5 = sext i16 %4 to i32
244  store i32 %5, i32 addrspace(1)* %out
245  ret void
246}
247
248; FUNC-LABEL: {{^}}char_array:
249
250; R600: MOVA_INT
251
252; 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
253; 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
254define void @char_array(i32 addrspace(1)* %out, i32 %index) #0 {
255entry:
256  %0 = alloca [2 x i8]
257  %1 = getelementptr inbounds [2 x i8], [2 x i8]* %0, i32 0, i32 0
258  %2 = getelementptr inbounds [2 x i8], [2 x i8]* %0, i32 0, i32 1
259  store i8 0, i8* %1
260  store i8 1, i8* %2
261  %3 = getelementptr inbounds [2 x i8], [2 x i8]* %0, i32 0, i32 %index
262  %4 = load i8, i8* %3
263  %5 = sext i8 %4 to i32
264  store i32 %5, i32 addrspace(1)* %out
265  ret void
266
267}
268
269; Test that two stack objects are not stored in the same register
270; The second stack object should be in T3.X
271; FUNC-LABEL: {{^}}no_overlap:
272; R600_CHECK: MOV
273; R600_CHECK: [[CHAN:[XYZW]]]+
274; R600-NOT: [[CHAN]]+
275; SI: v_mov_b32_e32 v3
276define void @no_overlap(i32 addrspace(1)* %out, i32 %in) #0 {
277entry:
278  %0 = alloca [3 x i8], align 1
279  %1 = alloca [2 x i8], align 1
280  %2 = getelementptr [3 x i8], [3 x i8]* %0, i32 0, i32 0
281  %3 = getelementptr [3 x i8], [3 x i8]* %0, i32 0, i32 1
282  %4 = getelementptr [3 x i8], [3 x i8]* %0, i32 0, i32 2
283  %5 = getelementptr [2 x i8], [2 x i8]* %1, i32 0, i32 0
284  %6 = getelementptr [2 x i8], [2 x i8]* %1, i32 0, i32 1
285  store i8 0, i8* %2
286  store i8 1, i8* %3
287  store i8 2, i8* %4
288  store i8 1, i8* %5
289  store i8 0, i8* %6
290  %7 = getelementptr [3 x i8], [3 x i8]* %0, i32 0, i32 %in
291  %8 = getelementptr [2 x i8], [2 x i8]* %1, i32 0, i32 %in
292  %9 = load i8, i8* %7
293  %10 = load i8, i8* %8
294  %11 = add i8 %9, %10
295  %12 = sext i8 %11 to i32
296  store i32 %12, i32 addrspace(1)* %out
297  ret void
298}
299
300define void @char_array_array(i32 addrspace(1)* %out, i32 %index) #0 {
301entry:
302  %alloca = alloca [2 x [2 x i8]]
303  %gep0 = getelementptr [2 x [2 x i8]], [2 x [2 x i8]]* %alloca, i32 0, i32 0, i32 0
304  %gep1 = getelementptr [2 x [2 x i8]], [2 x [2 x i8]]* %alloca, i32 0, i32 0, i32 1
305  store i8 0, i8* %gep0
306  store i8 1, i8* %gep1
307  %gep2 = getelementptr [2 x [2 x i8]], [2 x [2 x i8]]* %alloca, i32 0, i32 0, i32 %index
308  %load = load i8, i8* %gep2
309  %sext = sext i8 %load to i32
310  store i32 %sext, i32 addrspace(1)* %out
311  ret void
312}
313
314define void @i32_array_array(i32 addrspace(1)* %out, i32 %index) #0 {
315entry:
316  %alloca = alloca [2 x [2 x i32]]
317  %gep0 = getelementptr [2 x [2 x i32]], [2 x [2 x i32]]* %alloca, i32 0, i32 0, i32 0
318  %gep1 = getelementptr [2 x [2 x i32]], [2 x [2 x i32]]* %alloca, i32 0, i32 0, i32 1
319  store i32 0, i32* %gep0
320  store i32 1, i32* %gep1
321  %gep2 = getelementptr [2 x [2 x i32]], [2 x [2 x i32]]* %alloca, i32 0, i32 0, i32 %index
322  %load = load i32, i32* %gep2
323  store i32 %load, i32 addrspace(1)* %out
324  ret void
325}
326
327define void @i64_array_array(i64 addrspace(1)* %out, i32 %index) #0 {
328entry:
329  %alloca = alloca [2 x [2 x i64]]
330  %gep0 = getelementptr [2 x [2 x i64]], [2 x [2 x i64]]* %alloca, i32 0, i32 0, i32 0
331  %gep1 = getelementptr [2 x [2 x i64]], [2 x [2 x i64]]* %alloca, i32 0, i32 0, i32 1
332  store i64 0, i64* %gep0
333  store i64 1, i64* %gep1
334  %gep2 = getelementptr [2 x [2 x i64]], [2 x [2 x i64]]* %alloca, i32 0, i32 0, i32 %index
335  %load = load i64, i64* %gep2
336  store i64 %load, i64 addrspace(1)* %out
337  ret void
338}
339
340%struct.pair32 = type { i32, i32 }
341
342define void @struct_array_array(i32 addrspace(1)* %out, i32 %index) #0 {
343entry:
344  %alloca = alloca [2 x [2 x %struct.pair32]]
345  %gep0 = getelementptr [2 x [2 x %struct.pair32]], [2 x [2 x %struct.pair32]]* %alloca, i32 0, i32 0, i32 0, i32 1
346  %gep1 = getelementptr [2 x [2 x %struct.pair32]], [2 x [2 x %struct.pair32]]* %alloca, i32 0, i32 0, i32 1, i32 1
347  store i32 0, i32* %gep0
348  store i32 1, i32* %gep1
349  %gep2 = getelementptr [2 x [2 x %struct.pair32]], [2 x [2 x %struct.pair32]]* %alloca, i32 0, i32 0, i32 %index, i32 0
350  %load = load i32, i32* %gep2
351  store i32 %load, i32 addrspace(1)* %out
352  ret void
353}
354
355define void @struct_pair32_array(i32 addrspace(1)* %out, i32 %index) #0 {
356entry:
357  %alloca = alloca [2 x %struct.pair32]
358  %gep0 = getelementptr [2 x %struct.pair32], [2 x %struct.pair32]* %alloca, i32 0, i32 0, i32 1
359  %gep1 = getelementptr [2 x %struct.pair32], [2 x %struct.pair32]* %alloca, i32 0, i32 1, i32 0
360  store i32 0, i32* %gep0
361  store i32 1, i32* %gep1
362  %gep2 = getelementptr [2 x %struct.pair32], [2 x %struct.pair32]* %alloca, i32 0, i32 %index, i32 0
363  %load = load i32, i32* %gep2
364  store i32 %load, i32 addrspace(1)* %out
365  ret void
366}
367
368define void @select_private(i32 addrspace(1)* %out, i32 %in) nounwind {
369entry:
370  %tmp = alloca [2 x i32]
371  %tmp1 = getelementptr [2 x i32], [2 x i32]* %tmp, i32 0, i32 0
372  %tmp2 = getelementptr [2 x i32], [2 x i32]* %tmp, i32 0, i32 1
373  store i32 0, i32* %tmp1
374  store i32 1, i32* %tmp2
375  %cmp = icmp eq i32 %in, 0
376  %sel = select i1 %cmp, i32* %tmp1, i32* %tmp2
377  %load = load i32, i32* %sel
378  store i32 %load, i32 addrspace(1)* %out
379  ret void
380}
381
382; AMDGPUPromoteAlloca does not know how to handle ptrtoint.  When it
383; finds one, it should stop trying to promote.
384
385; FUNC-LABEL: ptrtoint:
386; SI-NOT: ds_write
387; SI: buffer_store_dword v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen
388; SI: buffer_load_dword v{{[0-9]+}}, v{{[0-9]+}}, s[{{[0-9]+:[0-9]+}}], s{{[0-9]+}} offen offset:5 ;
389define void @ptrtoint(i32 addrspace(1)* %out, i32 %a, i32 %b) #0 {
390  %alloca = alloca [16 x i32]
391  %tmp0 = getelementptr [16 x i32], [16 x i32]* %alloca, i32 0, i32 %a
392  store i32 5, i32* %tmp0
393  %tmp1 = ptrtoint [16 x i32]* %alloca to i32
394  %tmp2 = add i32 %tmp1, 5
395  %tmp3 = inttoptr i32 %tmp2 to i32*
396  %tmp4 = getelementptr i32, i32* %tmp3, i32 %b
397  %tmp5 = load i32, i32* %tmp4
398  store i32 %tmp5, i32 addrspace(1)* %out
399  ret void
400}
401
402; OPT-LABEL: @pointer_typed_alloca(
403; OPT:  getelementptr inbounds [256 x i32 addrspace(1)*], [256 x i32 addrspace(1)*] addrspace(3)* @pointer_typed_alloca.A.addr, i32 0, i32 %{{[0-9]+}}
404; OPT: load i32 addrspace(1)*, i32 addrspace(1)* addrspace(3)* %{{[0-9]+}}, align 4
405define void @pointer_typed_alloca(i32 addrspace(1)* %A) {
406entry:
407  %A.addr = alloca i32 addrspace(1)*, align 4
408  store i32 addrspace(1)* %A, i32 addrspace(1)** %A.addr, align 4
409  %ld0 = load i32 addrspace(1)*, i32 addrspace(1)** %A.addr, align 4
410  %arrayidx = getelementptr inbounds i32, i32 addrspace(1)* %ld0, i32 0
411  store i32 1, i32 addrspace(1)* %arrayidx, align 4
412  %ld1 = load i32 addrspace(1)*, i32 addrspace(1)** %A.addr, align 4
413  %arrayidx1 = getelementptr inbounds i32, i32 addrspace(1)* %ld1, i32 1
414  store i32 2, i32 addrspace(1)* %arrayidx1, align 4
415  %ld2 = load i32 addrspace(1)*, i32 addrspace(1)** %A.addr, align 4
416  %arrayidx2 = getelementptr inbounds i32, i32 addrspace(1)* %ld2, i32 2
417  store i32 3, i32 addrspace(1)* %arrayidx2, align 4
418  ret void
419}
420
421; FUNC-LABEL: v16i32_stack:
422
423; R600: MOVA_INT
424; R600: MOVA_INT
425; R600: MOVA_INT
426; R600: MOVA_INT
427; R600: MOVA_INT
428; R600: MOVA_INT
429; R600: MOVA_INT
430; R600: MOVA_INT
431; R600: MOVA_INT
432; R600: MOVA_INT
433; R600: MOVA_INT
434; R600: MOVA_INT
435; R600: MOVA_INT
436; R600: MOVA_INT
437; R600: MOVA_INT
438; R600: MOVA_INT
439
440; SI: buffer_load_dword
441; SI: buffer_load_dword
442; SI: buffer_load_dword
443; SI: buffer_load_dword
444; SI: buffer_load_dword
445; SI: buffer_load_dword
446; SI: buffer_load_dword
447; SI: buffer_load_dword
448; SI: buffer_load_dword
449; SI: buffer_load_dword
450; SI: buffer_load_dword
451; SI: buffer_load_dword
452; SI: buffer_load_dword
453; SI: buffer_load_dword
454; SI: buffer_load_dword
455; SI: buffer_load_dword
456
457define void @v16i32_stack(<16 x i32> addrspace(1)* %out, i32 %a) {
458  %alloca = alloca [2 x <16 x i32>]
459  %tmp0 = getelementptr [2 x <16 x i32>], [2 x <16 x i32>]* %alloca, i32 0, i32 %a
460  %tmp5 = load <16 x i32>, <16 x i32>* %tmp0
461  store <16 x i32> %tmp5, <16 x i32> addrspace(1)* %out
462  ret void
463}
464
465; FUNC-LABEL: v16float_stack:
466
467; R600: MOVA_INT
468; R600: MOVA_INT
469; R600: MOVA_INT
470; R600: MOVA_INT
471; R600: MOVA_INT
472; R600: MOVA_INT
473; R600: MOVA_INT
474; R600: MOVA_INT
475; R600: MOVA_INT
476; R600: MOVA_INT
477; R600: MOVA_INT
478; R600: MOVA_INT
479; R600: MOVA_INT
480; R600: MOVA_INT
481; R600: MOVA_INT
482; R600: MOVA_INT
483
484; SI: buffer_load_dword
485; SI: buffer_load_dword
486; SI: buffer_load_dword
487; SI: buffer_load_dword
488; SI: buffer_load_dword
489; SI: buffer_load_dword
490; SI: buffer_load_dword
491; SI: buffer_load_dword
492; SI: buffer_load_dword
493; SI: buffer_load_dword
494; SI: buffer_load_dword
495; SI: buffer_load_dword
496; SI: buffer_load_dword
497; SI: buffer_load_dword
498; SI: buffer_load_dword
499; SI: buffer_load_dword
500
501define void @v16float_stack(<16 x float> addrspace(1)* %out, i32 %a) {
502  %alloca = alloca [2 x <16 x float>]
503  %tmp0 = getelementptr [2 x <16 x float>], [2 x <16 x float>]* %alloca, i32 0, i32 %a
504  %tmp5 = load <16 x float>, <16 x float>* %tmp0
505  store <16 x float> %tmp5, <16 x float> addrspace(1)* %out
506  ret void
507}
508
509; FUNC-LABEL: v2float_stack:
510
511; R600: MOVA_INT
512; R600: MOVA_INT
513
514; SI: buffer_load_dword
515; SI: buffer_load_dword
516
517define void @v2float_stack(<2 x float> addrspace(1)* %out, i32 %a) {
518  %alloca = alloca [16 x <2 x float>]
519  %tmp0 = getelementptr [16 x <2 x float>], [16 x <2 x float>]* %alloca, i32 0, i32 %a
520  %tmp5 = load <2 x float>, <2 x float>* %tmp0
521  store <2 x float> %tmp5, <2 x float> addrspace(1)* %out
522  ret void
523}
524
525; OPT-LABEL: @direct_alloca_read_0xi32(
526; OPT: store [0 x i32] undef, [0 x i32] addrspace(3)*
527; OPT: load [0 x i32], [0 x i32] addrspace(3)*
528define void @direct_alloca_read_0xi32([0 x i32] addrspace(1)* %out, i32 %index) {
529entry:
530  %tmp = alloca [0 x i32]
531  store [0 x i32] [], [0 x i32]* %tmp
532  %load = load [0 x i32], [0 x i32]* %tmp
533  store [0 x i32] %load, [0 x i32] addrspace(1)* %out
534  ret void
535}
536
537; OPT-LABEL: @direct_alloca_read_1xi32(
538; OPT: store [1 x i32] zeroinitializer, [1 x i32] addrspace(3)*
539; OPT: load [1 x i32], [1 x i32] addrspace(3)*
540define void @direct_alloca_read_1xi32([1 x i32] addrspace(1)* %out, i32 %index) {
541entry:
542  %tmp = alloca [1 x i32]
543  store [1 x i32] [i32 0], [1 x i32]* %tmp
544  %load = load [1 x i32], [1 x i32]* %tmp
545  store [1 x i32] %load, [1 x i32] addrspace(1)* %out
546  ret void
547}
548
549attributes #0 = { nounwind "amdgpu-waves-per-eu"="1,2" }
550
551; HSAOPT: !0 = !{}
552; HSAOPT: !1 = !{i32 0, i32 2048}
553
554; NOHSAOPT: !0 = !{i32 0, i32 2048}
555