1 ; RUN: opt -mtriple=amdgcn-- -S -structurizecfg -si-annotate-control-flow %s | FileCheck -check-prefix=IR %s
2 ; RUN: llc -march=amdgcn -mcpu=hawaii -verify-machineinstrs < %s | FileCheck -check-prefix=GCN %s
4 ; After structurizing, there are 3 levels of loops. The i1 phi
5 ; conditions mutually depend on each other, so it isn't safe to delete
6 ; the condition that appears to have no uses until the loop is
7 ; completely processed.
10 ; IR-LABEL: @reduced_nested_loop_conditions(
13 ; IR-NEXT: %phi.broken = phi i64 [ %3, %bb10 ], [ 0, %bb ]
14 ; IR-NEXT: %tmp6 = phi i32 [ 0, %bb ], [ %tmp11, %bb10 ]
15 ; IR-NEXT: %tmp7 = icmp eq i32 %tmp6, 1
16 ; IR-NEXT: %0 = call { i1, i64 } @llvm.amdgcn.if.i64(i1 %tmp7)
17 ; IR-NEXT: %1 = extractvalue { i1, i64 } %0, 0
18 ; IR-NEXT: %2 = extractvalue { i1, i64 } %0, 1
19 ; IR-NEXT: br i1 %1, label %bb8, label %Flow
22 ; IR-NEXT: br label %bb13
25 ; IR-NEXT: %tmp11 = phi i32 [ %6, %Flow ]
26 ; IR-NEXT: %tmp12 = phi i1 [ %5, %Flow ]
27 ; IR-NEXT: %3 = call i64 @llvm.amdgcn.if.break.i64.i64(i1 %tmp12, i64 %phi.broken)
28 ; IR-NEXT: %4 = call i1 @llvm.amdgcn.loop.i64(i64 %3)
29 ; IR-NEXT: br i1 %4, label %bb23, label %bb5
32 ; IR-NEXT: %5 = phi i1 [ %tmp22, %bb4 ], [ true, %bb5 ]
33 ; IR-NEXT: %6 = phi i32 [ %tmp21, %bb4 ], [ undef, %bb5 ]
34 ; IR-NEXT: call void @llvm.amdgcn.end.cf.i64(i64 %2)
35 ; IR-NEXT: br label %bb10
38 ; IR-NEXT: %tmp14 = phi i1 [ %tmp22, %bb3 ], [ true, %bb8 ]
39 ; IR-NEXT: %tmp15 = bitcast i64 %tmp2 to <2 x i32>
40 ; IR-NEXT: br i1 %tmp14, label %bb16, label %bb20
43 ; IR-NEXT: %tmp17 = extractelement <2 x i32> %tmp15, i64 1
44 ; IR-NEXT: %tmp18 = getelementptr inbounds i32, i32 addrspace(3)* undef, i32 %tmp17
45 ; IR-NEXT: %tmp19 = load volatile i32, i32 addrspace(3)* %tmp18
46 ; IR-NEXT: br label %bb20
49 ; IR-NEXT: %tmp21 = phi i32 [ %tmp19, %bb16 ], [ 0, %bb13 ]
50 ; IR-NEXT: %tmp22 = phi i1 [ false, %bb16 ], [ %tmp14, %bb13 ]
51 ; IR-NEXT: br label %bb9
54 ; IR-NEXT: call void @llvm.amdgcn.end.cf.i64(i64 %3)
57 ; GCN-LABEL: {{^}}reduced_nested_loop_conditions:
59 ; GCN: s_cmp_lg_u32 s{{[0-9]+}}, 1
60 ; GCN-NEXT: s_cbranch_scc0
62 ; FIXME: Should fold to unconditional branch?
64 ; GCN: s_cbranch_vccnz
68 ; GCN: [[BB9:BB[0-9]+_[0-9]+]]: ; %bb9
69 ; GCN-NEXT: ; =>This Inner Loop Header: Depth=1
70 ; GCN-NEXT: s_cbranch_vccnz [[BB9]]
71 define amdgpu_kernel void @reduced_nested_loop_conditions(i64 addrspace(3)* nocapture %arg) #0 {
73 %tmp = tail call i32 @llvm.amdgcn.workitem.id.x() #1
74 %tmp1 = getelementptr inbounds i64, i64 addrspace(3)* %arg, i32 %tmp
75 %tmp2 = load volatile i64, i64 addrspace(3)* %tmp1
79 br i1 true, label %bb4, label %bb13
84 bb5: ; preds = %bb10, %bb
85 %tmp6 = phi i32 [ 0, %bb ], [ %tmp11, %bb10 ]
86 %tmp7 = icmp eq i32 %tmp6, 1
87 br i1 %tmp7, label %bb8, label %bb10
92 bb9: ; preds = %bb20, %bb9
93 br i1 false, label %bb3, label %bb9
95 bb10: ; preds = %bb5, %bb4
96 %tmp11 = phi i32 [ %tmp21, %bb4 ], [ undef, %bb5 ]
97 %tmp12 = phi i1 [ %tmp22, %bb4 ], [ true, %bb5 ]
98 br i1 %tmp12, label %bb23, label %bb5
100 bb13: ; preds = %bb8, %bb3
101 %tmp14 = phi i1 [ %tmp22, %bb3 ], [ true, %bb8 ]
102 %tmp15 = bitcast i64 %tmp2 to <2 x i32>
103 br i1 %tmp14, label %bb16, label %bb20
105 bb16: ; preds = %bb13
106 %tmp17 = extractelement <2 x i32> %tmp15, i64 1
107 %tmp18 = getelementptr inbounds i32, i32 addrspace(3)* undef, i32 %tmp17
108 %tmp19 = load volatile i32, i32 addrspace(3)* %tmp18
111 bb20: ; preds = %bb16, %bb13
112 %tmp21 = phi i32 [ %tmp19, %bb16 ], [ 0, %bb13 ]
113 %tmp22 = phi i1 [ false, %bb16 ], [ %tmp14, %bb13 ]
116 bb23: ; preds = %bb10
120 ; Earlier version of above, before a run of the structurizer.
121 ; IR-LABEL: @nested_loop_conditions(
124 ; IR-NEXT: call void @llvm.amdgcn.end.cf.i64(i64 %21)
125 ; IR-NEXT: %0 = call { i1, i64 } @llvm.amdgcn.if.i64(i1 %14)
126 ; IR-NEXT: %1 = extractvalue { i1, i64 } %0, 0
127 ; IR-NEXT: %2 = extractvalue { i1, i64 } %0, 1
128 ; IR-NEXT: br i1 %1, label %bb4.bb13_crit_edge, label %Flow4
131 ; IR-NEXT: %3 = phi i1 [ true, %bb4.bb13_crit_edge ], [ false, %Flow3 ]
132 ; IR-NEXT: call void @llvm.amdgcn.end.cf.i64(i64 %2)
133 ; IR-NEXT: br label %Flow
136 ; IR-NEXT: %4 = phi i1 [ %3, %Flow4 ], [ true, %bb ]
137 ; IR-NEXT: %5 = call { i1, i64 } @llvm.amdgcn.if.i64(i1 %4)
138 ; IR-NEXT: %6 = extractvalue { i1, i64 } %5, 0
139 ; IR-NEXT: %7 = extractvalue { i1, i64 } %5, 1
140 ; IR-NEXT: br i1 %6, label %bb13, label %bb31
143 ; IR: %tmp15 = icmp eq i32 %tmp1037, 1
144 ; IR-NEXT: %8 = call { i1, i64 } @llvm.amdgcn.if.i64(i1 %tmp15)
147 ; IR-NEXT: %11 = phi <4 x i32> [ %tmp9, %bb21 ], [ undef, %bb14 ]
148 ; IR-NEXT: %12 = phi i32 [ %tmp10, %bb21 ], [ undef, %bb14 ]
149 ; IR-NEXT: %13 = phi i1 [ %18, %bb21 ], [ true, %bb14 ]
150 ; IR-NEXT: %14 = phi i1 [ %18, %bb21 ], [ false, %bb14 ]
151 ; IR-NEXT: %15 = phi i1 [ false, %bb21 ], [ true, %bb14 ]
152 ; IR-NEXT: call void @llvm.amdgcn.end.cf.i64(i64 %10)
153 ; IR-NEXT: %16 = call i64 @llvm.amdgcn.if.break.i64.i64(i1 %13, i64 %phi.broken)
154 ; IR-NEXT: %17 = call i1 @llvm.amdgcn.loop.i64(i64 %16)
155 ; IR-NEXT: br i1 %17, label %Flow2, label %bb14
158 ; IR: %tmp12 = icmp slt i32 %tmp11, 9
159 ; IR-NEXT: %18 = xor i1 %tmp12, true
160 ; IR-NEXT: br label %Flow1
163 ; IR-NEXT: call void @llvm.amdgcn.end.cf.i64(i64 %16)
164 ; IR-NEXT: %19 = call { i1, i64 } @llvm.amdgcn.if.i64(i1 %15)
165 ; IR-NEXT: %20 = extractvalue { i1, i64 } %19, 0
166 ; IR-NEXT: %21 = extractvalue { i1, i64 } %19, 1
167 ; IR-NEXT: br i1 %20, label %bb31.loopexit, label %Flow3
170 ; IR-NEXT: call void @llvm.amdgcn.end.cf.i64(i64 %7)
171 ; IR-NEXT: store volatile i32 0, i32 addrspace(1)* undef
175 ; GCN-LABEL: {{^}}nested_loop_conditions:
177 ; GCN: v_cmp_lt_i32_e32 vcc, 8, v
178 ; GCN: s_and_b64 vcc, exec, vcc
179 ; GCN: s_cbranch_vccnz [[BB31:BB[0-9]+_[0-9]+]]
181 ; GCN: [[BB14:BB[0-9]+_[0-9]+]]: ; %bb14
182 ; GCN: v_cmp_ne_u32_e32 vcc, 1, v
183 ; GCN-NEXT: s_and_b64 vcc, exec, vcc
184 ; GCN-NEXT: s_cbranch_vccnz [[BB31]]
186 ; GCN: [[BB18:BB[0-9]+_[0-9]+]]: ; %bb18
187 ; GCN: buffer_load_dword
188 ; GCN: v_cmp_lt_i32_e32 vcc, 8, v
189 ; GCN-NEXT: s_and_b64 vcc, exec, vcc
190 ; GCN-NEXT: s_cbranch_vccnz [[BB18]]
192 ; GCN: buffer_load_dword
193 ; GCN: buffer_load_dword
194 ; GCN: v_cmp_gt_i32_e32 vcc, 9
195 ; GCN-NEXT: s_and_b64 vcc, exec, vcc
196 ; GCN-NEXT: s_cbranch_vccnz [[BB14]]
199 ; GCN: buffer_store_dword
201 define amdgpu_kernel void @nested_loop_conditions(i64 addrspace(1)* nocapture %arg) #0 {
203 %tmp = tail call i32 @llvm.amdgcn.workitem.id.x() #1
204 %tmp1 = zext i32 %tmp to i64
205 %tmp2 = getelementptr inbounds i64, i64 addrspace(1)* %arg, i64 %tmp1
206 %tmp3 = load i64, i64 addrspace(1)* %tmp2, align 16
207 %tmp932 = load <4 x i32>, <4 x i32> addrspace(1)* undef, align 16
208 %tmp1033 = extractelement <4 x i32> %tmp932, i64 0
209 %tmp1134 = load volatile i32, i32 addrspace(1)* undef
210 %tmp1235 = icmp slt i32 %tmp1134, 9
211 br i1 %tmp1235, label %bb14.lr.ph, label %bb13
213 bb14.lr.ph: ; preds = %bb
216 bb4.bb13_crit_edge: ; preds = %bb21
219 bb13: ; preds = %bb4.bb13_crit_edge, %bb
222 bb14: ; preds = %bb21, %bb14.lr.ph
223 %tmp1037 = phi i32 [ %tmp1033, %bb14.lr.ph ], [ %tmp10, %bb21 ]
224 %tmp936 = phi <4 x i32> [ %tmp932, %bb14.lr.ph ], [ %tmp9, %bb21 ]
225 %tmp15 = icmp eq i32 %tmp1037, 1
226 br i1 %tmp15, label %bb16, label %bb31.loopexit
228 bb16: ; preds = %bb14
229 %tmp17 = bitcast i64 %tmp3 to <2 x i32>
232 bb18: ; preds = %bb18, %bb16
233 %tmp19 = load volatile i32, i32 addrspace(1)* undef
234 %tmp20 = icmp slt i32 %tmp19, 9
235 br i1 %tmp20, label %bb21, label %bb18
237 bb21: ; preds = %bb18
238 %tmp22 = extractelement <2 x i32> %tmp17, i64 1
239 %tmp23 = lshr i32 %tmp22, 16
240 %tmp24 = select i1 undef, i32 undef, i32 %tmp23
241 %tmp25 = uitofp i32 %tmp24 to float
242 %tmp26 = fmul float %tmp25, 0x3EF0001000000000
243 %tmp27 = fsub float %tmp26, undef
244 %tmp28 = fcmp olt float %tmp27, 5.000000e-01
245 %tmp29 = select i1 %tmp28, i64 1, i64 2
246 %tmp30 = extractelement <4 x i32> %tmp936, i64 %tmp29
247 %tmp7 = zext i32 %tmp30 to i64
248 %tmp8 = getelementptr inbounds <4 x i32>, <4 x i32> addrspace(1)* undef, i64 %tmp7
249 %tmp9 = load <4 x i32>, <4 x i32> addrspace(1)* %tmp8, align 16
250 %tmp10 = extractelement <4 x i32> %tmp9, i64 0
251 %tmp11 = load volatile i32, i32 addrspace(1)* undef
252 %tmp12 = icmp slt i32 %tmp11, 9
253 br i1 %tmp12, label %bb14, label %bb4.bb13_crit_edge
255 bb31.loopexit: ; preds = %bb14
258 bb31: ; preds = %bb31.loopexit, %bb13
259 store volatile i32 0, i32 addrspace(1)* undef
263 declare i32 @llvm.amdgcn.workitem.id.x() #1
265 attributes #0 = { nounwind }
266 attributes #1 = { nounwind readnone }