1; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=fiji -mattr=-flat-for-global -enable-ipra=0 -amdgpu-sroa=0 -verify-machineinstrs < %s | FileCheck -enable-var-scope -check-prefixes=GCN,VI,CIVI,MESA %s
2; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=hawaii -enable-ipra=0 -amdgpu-sroa=0 -verify-machineinstrs < %s | FileCheck -enable-var-scope -check-prefixes=GCN,CI,CIVI,MESA %s
3; RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -mattr=-flat-for-global -enable-ipra=0 -amdgpu-sroa=0 -verify-machineinstrs < %s | FileCheck -enable-var-scope -check-prefixes=GCN,GFX9,MESA %s
4target datalayout = "A5"
5
6; FIXME: Why is this commuted only sometimes?
7; GCN-LABEL: {{^}}i32_fastcc_i32_i32:
8; GCN: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
9; CIVI-NEXT: v_add_{{i|u}}32_e32 v0, vcc, v0, v1
10; GFX9-NEXT: v_add_u32_e32 v0, v0, v1
11; GCN-NEXT: s_setpc_b64
12define fastcc i32 @i32_fastcc_i32_i32(i32 %arg0, i32 %arg1) #1 {
13  %add0 = add i32 %arg0, %arg1
14  ret i32 %add0
15}
16
17; GCN-LABEL: {{^}}i32_fastcc_i32_i32_stack_object:
18; GCN: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
19; CIVI-NEXT: v_add_{{i|u}}32_e32 v0, vcc, v0, v1
20; GFX9-NEXT: v_add_u32_e32 v0, v0, v1
21; GCN: s_mov_b32 s5, s32
22; GCN: buffer_store_dword v{{[0-9]+}}, off, s[0:3], s5 offset:24
23; GCN: s_waitcnt vmcnt(0)
24; GCN: s_setpc_b64
25; GCN: ; ScratchSize: 68
26define fastcc i32 @i32_fastcc_i32_i32_stack_object(i32 %arg0, i32 %arg1) #1 {
27  %alloca = alloca [16 x i32], align 4, addrspace(5)
28  %gep = getelementptr inbounds [16 x i32], [16 x i32] addrspace(5)* %alloca, i32 0, i32 5
29  store volatile i32 9, i32 addrspace(5)* %gep
30  %add0 = add i32 %arg0, %arg1
31  ret i32 %add0
32}
33
34; GCN-LABEL: {{^}}sibling_call_i32_fastcc_i32_i32:
35define fastcc i32 @sibling_call_i32_fastcc_i32_i32(i32 %a, i32 %b, i32 %c) #1 {
36entry:
37  %ret = tail call fastcc i32 @i32_fastcc_i32_i32(i32 %a, i32 %b)
38  ret i32 %ret
39}
40
41; GCN-LABEL: {{^}}sibling_call_i32_fastcc_i32_i32_stack_object:
42; GCN: v_mov_b32_e32 [[NINE:v[0-9]+]], 9
43; GCN: buffer_store_dword [[NINE]], off, s[0:3], s5 offset:24
44; GCN: s_setpc_b64
45; GCN: ; ScratchSize: 68
46define fastcc i32 @sibling_call_i32_fastcc_i32_i32_stack_object(i32 %a, i32 %b, i32 %c) #1 {
47entry:
48  %alloca = alloca [16 x i32], align 4, addrspace(5)
49  %gep = getelementptr inbounds [16 x i32], [16 x i32] addrspace(5)* %alloca, i32 0, i32 5
50  store volatile i32 9, i32 addrspace(5)* %gep
51  %ret = tail call fastcc i32 @i32_fastcc_i32_i32(i32 %a, i32 %b)
52  ret i32 %ret
53}
54
55; GCN-LABEL: {{^}}sibling_call_i32_fastcc_i32_i32_callee_stack_object:
56; GCN: v_mov_b32_e32 [[NINE:v[0-9]+]], 9
57; GCN: buffer_store_dword [[NINE]], off, s[0:3], s5 offset:24
58; GCN: s_setpc_b64
59; GCN: ; ScratchSize: 136
60define fastcc i32 @sibling_call_i32_fastcc_i32_i32_callee_stack_object(i32 %a, i32 %b, i32 %c) #1 {
61entry:
62  %alloca = alloca [16 x i32], align 4, addrspace(5)
63  %gep = getelementptr inbounds [16 x i32], [16 x i32] addrspace(5)* %alloca, i32 0, i32 5
64  store volatile i32 9, i32 addrspace(5)* %gep
65  %ret = tail call fastcc i32 @i32_fastcc_i32_i32_stack_object(i32 %a, i32 %b)
66  ret i32 %ret
67}
68
69; GCN-LABEL: {{^}}sibling_call_i32_fastcc_i32_i32_unused_result:
70define fastcc void @sibling_call_i32_fastcc_i32_i32_unused_result(i32 %a, i32 %b, i32 %c) #1 {
71entry:
72  %ret = tail call fastcc i32 @i32_fastcc_i32_i32(i32 %a, i32 %b)
73  ret void
74}
75
76; It doesn't make sense to do a tail from a kernel
77; GCN-LABEL: {{^}}kernel_call_i32_fastcc_i32_i32_unused_result:
78;define amdgpu_kernel void @kernel_call_i32_fastcc_i32_i32_unused_result(i32 %a, i32 %b, i32 %c) #1 {
79define amdgpu_kernel void @kernel_call_i32_fastcc_i32_i32_unused_result(i32 %a, i32 %b, i32 %c) #1 {
80entry:
81  %ret = tail call fastcc i32 @i32_fastcc_i32_i32(i32 %a, i32 %b)
82  ret void
83}
84
85; GCN-LABEL: {{^}}i32_fastcc_i32_byval_i32:
86; GCN: s_waitcnt
87; GCN-NEXT: s_mov_b32 s5, s32
88; GCN-NEXT: buffer_load_dword v1, off, s[0:3], s5 offset:4
89; GCN-NEXT: s_waitcnt vmcnt(0)
90
91; CIVI-NEXT: v_add_{{i|u}}32_e32 v0, vcc, v0, v1
92; GFX9-NEXT: v_add_u32_e32 v0, v0, v1
93
94; GCN-NEXT: s_setpc_b64 s[30:31]
95define fastcc i32 @i32_fastcc_i32_byval_i32(i32 %arg0, i32 addrspace(5)* byval align 4 %arg1) #1 {
96  %arg1.load = load i32, i32 addrspace(5)* %arg1, align 4
97  %add0 = add i32 %arg0, %arg1.load
98  ret i32 %add0
99}
100
101; Tail call disallowed with byval in parent.
102; GCN-LABEL: {{^}}sibling_call_i32_fastcc_i32_byval_i32_byval_parent:
103; GCN-NOT: v_writelane_b32 v{{[0-9]+}}, s32
104; GCN: buffer_store_dword v{{[0-9]+}}, off, s[0:3], s32 offset:4
105; GCN: s_swappc_b64
106; GCN-NOT: v_readlane_b32 s32
107; GCN: s_setpc_b64
108define fastcc i32 @sibling_call_i32_fastcc_i32_byval_i32_byval_parent(i32 %a, i32 addrspace(5)* byval %b.byval, i32 %c) #1 {
109entry:
110  %ret = tail call fastcc i32 @i32_fastcc_i32_byval_i32(i32 %a, i32 addrspace(5)* %b.byval)
111  ret i32 %ret
112}
113
114; Tail call disallowed with byval in parent, not callee.
115; GCN-LABEL: {{^}}sibling_call_i32_fastcc_i32_byval_i32:
116; GCN-NOT: v0
117; GCN-NOT: s32
118; GCN: buffer_load_dword v1, off, s[0:3], s4 offset:16
119; GCN: s_mov_b32 s5, s32
120; GCN: buffer_store_dword v1, off, s[0:3], s5 offset:4
121; GCN-NEXT: s_setpc_b64
122define fastcc i32 @sibling_call_i32_fastcc_i32_byval_i32(i32 %a, [16 x i32] %large) #1 {
123entry:
124  %ret = tail call fastcc i32 @i32_fastcc_i32_byval_i32(i32 %a, i32 addrspace(5)* inttoptr (i32 16 to i32 addrspace(5)*))
125  ret i32 %ret
126}
127
128; GCN-LABEL: {{^}}i32_fastcc_i32_i32_a32i32:
129; GCN: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
130; GCN-DAG: buffer_load_dword [[LOAD_0:v[0-9]+]], off, s[0:3], s5 offset:4
131; GCN-DAG: buffer_load_dword [[LOAD_1:v[0-9]+]], off, s[0:3], s5 offset:8
132
133; CIVI-NEXT: v_add_{{i|u}}32_e32 v0, vcc, v0, v1
134; CIVI: v_add_{{i|u}}32_e32 v0, vcc, v0, [[LOAD_0]]
135; CIVI: v_add_{{i|u}}32_e32 v0, vcc, v0, [[LOAD_1]]
136
137
138; GFX9-NEXT: v_add_u32_e32 v0, v0, v1
139; GFX9: v_add3_u32 v0, v0, v3, v2
140
141; GCN-NEXT: s_setpc_b64
142define fastcc i32 @i32_fastcc_i32_i32_a32i32(i32 %arg0, i32 %arg1, [32 x i32] %large) #1 {
143  %val_firststack = extractvalue [32 x i32] %large, 30
144  %val_laststack = extractvalue [32 x i32] %large, 31
145  %add0 = add i32 %arg0, %arg1
146  %add1 = add i32 %add0, %val_firststack
147  %add2 = add i32 %add1, %val_laststack
148  ret i32 %add2
149}
150
151; FIXME: Why load and store same location for stack args?
152; GCN-LABEL: {{^}}sibling_call_i32_fastcc_i32_i32_a32i32:
153; GCN: s_mov_b32 s5, s32
154
155; GCN-DAG: buffer_store_dword v32, off, s[0:3], s5 offset:16 ; 4-byte Folded Spill
156; GCN-DAG: buffer_store_dword v33, off, s[0:3], s5 offset:12 ; 4-byte Folded Spill
157
158; GCN-DAG: buffer_load_dword [[LOAD_0:v[0-9]+]], off, s[0:3], s5 offset:4
159; GCN-DAG: buffer_load_dword [[LOAD_1:v[0-9]+]], off, s[0:3], s5 offset:8
160
161; GCN-NOT: s32
162
163; GCN-DAG: buffer_store_dword [[LOAD_0]], off, s[0:3], s5 offset:4
164; GCN-DAG: buffer_store_dword [[LOAD_1]], off, s[0:3], s5 offset:8
165
166; GCN-DAG: buffer_load_dword v32, off, s[0:3], s5 offset:16 ; 4-byte Folded Reload
167; GCN-DAG: buffer_load_dword v33, off, s[0:3], s5 offset:12 ; 4-byte Folded Reload
168
169; GCN-NOT: s32
170; GCN: s_setpc_b64
171define fastcc i32 @sibling_call_i32_fastcc_i32_i32_a32i32(i32 %a, i32 %b, [32 x i32] %c) #1 {
172entry:
173  %ret = tail call fastcc i32 @i32_fastcc_i32_i32_a32i32(i32 %a, i32 %b, [32 x i32] %c)
174  ret i32 %ret
175}
176
177; GCN-LABEL: {{^}}sibling_call_i32_fastcc_i32_i32_a32i32_stack_object:
178; GCN-DAG: s_mov_b32 s5, s32
179; GCN-NOT: s32
180; GCN-DAG: v_mov_b32_e32 [[NINE:v[0-9]+]], 9
181; GCN: buffer_store_dword [[NINE]], off, s[0:3], s5 offset:44
182
183; GCN-NOT: s32
184; GCN: s_setpc_b64
185define fastcc i32 @sibling_call_i32_fastcc_i32_i32_a32i32_stack_object(i32 %a, i32 %b, [32 x i32] %c) #1 {
186entry:
187  %alloca = alloca [16 x i32], align 4, addrspace(5)
188  %gep = getelementptr inbounds [16 x i32], [16 x i32] addrspace(5)* %alloca, i32 0, i32 5
189  store volatile i32 9, i32 addrspace(5)* %gep
190  %ret = tail call fastcc i32 @i32_fastcc_i32_i32_a32i32(i32 %a, i32 %b, [32 x i32] %c)
191  ret i32 %ret
192}
193
194; If the callee requires more stack argument space than the caller,
195; don't do a tail call.
196; TODO: Do we really need this restriction?
197
198; GCN-LABEL: {{^}}no_sibling_call_callee_more_stack_space:
199; GCN: s_swappc_b64
200; GCN: s_setpc_b64
201define fastcc i32 @no_sibling_call_callee_more_stack_space(i32 %a, i32 %b) #1 {
202entry:
203  %ret = tail call fastcc i32 @i32_fastcc_i32_i32_a32i32(i32 %a, i32 %b, [32 x i32] zeroinitializer)
204  ret i32 %ret
205}
206
207; Have another non-tail in the function
208; GCN-LABEL: {{^}}sibling_call_i32_fastcc_i32_i32_other_call:
209; GCN: s_mov_b32 s5, s32
210; GCN: buffer_store_dword v34, off, s[0:3], s5 offset:12
211; GCN: buffer_store_dword v32, off, s[0:3], s5 offset:8 ; 4-byte Folded Spill
212; GCN: buffer_store_dword v33, off, s[0:3], s5 offset:4 ; 4-byte Folded Spill
213; GCN-DAG: v_writelane_b32 v34, s33, 0
214; GCN-DAG: v_writelane_b32 v34, s34, 1
215; GCN-DAG: v_writelane_b32 v34, s35, 2
216; GCN-DAG: s_add_u32 s32, s32, 0x400
217
218; GCN-DAG: s_getpc_b64
219; GCN: s_swappc_b64
220
221; GCN: s_getpc_b64 s[6:7]
222; GCN: s_add_u32 s6, s6, sibling_call_i32_fastcc_i32_i32@rel32@lo+4
223; GCN: s_addc_u32 s7, s7, sibling_call_i32_fastcc_i32_i32@rel32@hi+4
224
225; GCN-DAG: v_readlane_b32 s33, v34, 0
226; GCN-DAG: v_readlane_b32 s34, v34, 1
227; GCN-DAG: v_readlane_b32 s35, v34, 2
228
229; GCN: buffer_load_dword v33, off, s[0:3], s5 offset:4
230; GCN: buffer_load_dword v32, off, s[0:3], s5 offset:8
231; GCN: buffer_load_dword v34, off, s[0:3], s5 offset:12
232; GCN: s_sub_u32 s32, s32, 0x400
233; GCN: s_setpc_b64 s[6:7]
234define fastcc i32 @sibling_call_i32_fastcc_i32_i32_other_call(i32 %a, i32 %b, i32 %c) #1 {
235entry:
236  %other.call = tail call fastcc i32 @i32_fastcc_i32_i32(i32 %a, i32 %b)
237  %ret = tail call fastcc i32 @sibling_call_i32_fastcc_i32_i32(i32 %a, i32 %b, i32 %other.call)
238  ret i32 %ret
239}
240
241; Have stack object in caller and stack passed arguments. SP should be
242; in same place at function exit.
243
244; GCN-LABEL: {{^}}sibling_call_stack_objecti32_fastcc_i32_i32_a32i32:
245; GCN: s_mov_b32 s5, s32
246; GCN-NOT: s32
247; GCN: s_setpc_b64 s[6:7]
248define fastcc i32 @sibling_call_stack_objecti32_fastcc_i32_i32_a32i32(i32 %a, i32 %b, [32 x i32] %c) #1 {
249entry:
250  %alloca = alloca [16 x i32], align 4, addrspace(5)
251  %gep = getelementptr inbounds [16 x i32], [16 x i32] addrspace(5)* %alloca, i32 0, i32 5
252  store volatile i32 9, i32 addrspace(5)* %gep
253  %ret = tail call fastcc i32 @i32_fastcc_i32_i32_a32i32(i32 %a, i32 %b, [32 x i32] %c)
254  ret i32 %ret
255}
256
257; GCN-LABEL: {{^}}sibling_call_stack_objecti32_fastcc_i32_i32_a32i32_larger_arg_area:
258; GCN: s_mov_b32 s5, s32
259; GCN-NOT: s32
260; GCN: s_setpc_b64 s[6:7]
261define fastcc i32 @sibling_call_stack_objecti32_fastcc_i32_i32_a32i32_larger_arg_area(i32 %a, i32 %b, [36 x i32] %c) #1 {
262entry:
263  %alloca = alloca [16 x i32], align 4, addrspace(5)
264  %gep = getelementptr inbounds [16 x i32], [16 x i32] addrspace(5)* %alloca, i32 0, i32 5
265  store volatile i32 9, i32 addrspace(5)* %gep
266  %ret = tail call fastcc i32 @i32_fastcc_i32_i32_a32i32(i32 %a, i32 %b, [32 x i32] zeroinitializer)
267  ret i32 %ret
268}
269
270attributes #0 = { nounwind }
271attributes #1 = { nounwind noinline }
272