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 3 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. 8 9 10; IR-LABEL: @reduced_nested_loop_conditions( 11 12; IR: bb5: 13; IR-NEXT: %phi.broken = phi i64 [ %loop.phi, %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(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 20 21; IR: bb8: 22; IR-NEXT: %3 = call i64 @llvm.amdgcn.break(i64 %phi.broken) 23; IR-NEXT: br label %bb13 24 25; IR: bb10: 26; IR-NEXT: %loop.phi = phi i64 [ %6, %Flow ] 27; IR-NEXT: %tmp11 = phi i32 [ %5, %Flow ] 28; IR-NEXT: %4 = call i1 @llvm.amdgcn.loop(i64 %loop.phi) 29; IR-NEXT: br i1 %4, label %bb23, label %bb5 30 31; IR: Flow: 32; IR-NEXT: %loop.phi1 = phi i64 [ %loop.phi2, %bb4 ], [ %phi.broken, %bb5 ] 33; IR-NEXT: %5 = phi i32 [ %tmp21, %bb4 ], [ undef, %bb5 ] 34; IR-NEXT: %6 = call i64 @llvm.amdgcn.else.break(i64 %2, i64 %loop.phi1) 35; IR-NEXT: call void @llvm.amdgcn.end.cf(i64 %2) 36; IR-NEXT: br label %bb10 37 38; IR: bb13: 39; IR-NEXT: %loop.phi3 = phi i64 [ %loop.phi4, %bb3 ], [ %3, %bb8 ] 40; IR-NEXT: %tmp14 = phi i1 [ false, %bb3 ], [ true, %bb8 ] 41; IR-NEXT: %tmp15 = bitcast i64 %tmp2 to <2 x i32> 42; IR-NEXT: br i1 %tmp14, label %bb16, label %bb20 43 44; IR: bb16: 45; IR-NEXT: %tmp17 = extractelement <2 x i32> %tmp15, i64 1 46; IR-NEXT: %tmp18 = getelementptr inbounds i32, i32 addrspace(3)* undef, i32 %tmp17 47; IR-NEXT: %tmp19 = load volatile i32, i32 addrspace(3)* %tmp18 48; IR-NEXT: br label %bb20 49 50; IR: bb20: 51; IR-NEXT: %loop.phi4 = phi i64 [ %phi.broken, %bb16 ], [ %phi.broken, %bb13 ] 52; IR-NEXT: %loop.phi2 = phi i64 [ %phi.broken, %bb16 ], [ %loop.phi3, %bb13 ] 53; IR-NEXT: %tmp21 = phi i32 [ %tmp19, %bb16 ], [ 0, %bb13 ] 54; IR-NEXT: br label %bb9 55 56; IR: bb23: 57; IR-NEXT: call void @llvm.amdgcn.end.cf(i64 %loop.phi) 58; IR-NEXT: ret void 59 60; GCN-LABEL: {{^}}reduced_nested_loop_conditions: 61 62; GCN: s_cmp_lg_u32 s{{[0-9]+}}, 1 63; GCN-NEXT: s_cbranch_scc0 64 65; FIXME: Should fold to unconditional branch? 66; GCN: ; implicit-def 67; GCN: s_cbranch_vccnz 68 69; GCN: ds_read_b32 70 71; GCN: [[BB9:BB[0-9]+_[0-9]+]]: ; %bb9 72; GCN-NEXT: ; =>This Inner Loop Header: Depth=1 73; GCN-NEXT: s_cbranch_vccnz [[BB9]] 74define amdgpu_kernel void @reduced_nested_loop_conditions(i64 addrspace(3)* nocapture %arg) #0 { 75bb: 76 %tmp = tail call i32 @llvm.amdgcn.workitem.id.x() #1 77 %tmp1 = getelementptr inbounds i64, i64 addrspace(3)* %arg, i32 %tmp 78 %tmp2 = load volatile i64, i64 addrspace(3)* %tmp1 79 br label %bb5 80 81bb3: ; preds = %bb9 82 br i1 true, label %bb4, label %bb13 83 84bb4: ; preds = %bb3 85 br label %bb10 86 87bb5: ; preds = %bb10, %bb 88 %tmp6 = phi i32 [ 0, %bb ], [ %tmp11, %bb10 ] 89 %tmp7 = icmp eq i32 %tmp6, 1 90 br i1 %tmp7, label %bb8, label %bb10 91 92bb8: ; preds = %bb5 93 br label %bb13 94 95bb9: ; preds = %bb20, %bb9 96 br i1 false, label %bb3, label %bb9 97 98bb10: ; preds = %bb5, %bb4 99 %tmp11 = phi i32 [ %tmp21, %bb4 ], [ undef, %bb5 ] 100 %tmp12 = phi i1 [ %tmp22, %bb4 ], [ true, %bb5 ] 101 br i1 %tmp12, label %bb23, label %bb5 102 103bb13: ; preds = %bb8, %bb3 104 %tmp14 = phi i1 [ %tmp22, %bb3 ], [ true, %bb8 ] 105 %tmp15 = bitcast i64 %tmp2 to <2 x i32> 106 br i1 %tmp14, label %bb16, label %bb20 107 108bb16: ; preds = %bb13 109 %tmp17 = extractelement <2 x i32> %tmp15, i64 1 110 %tmp18 = getelementptr inbounds i32, i32 addrspace(3)* undef, i32 %tmp17 111 %tmp19 = load volatile i32, i32 addrspace(3)* %tmp18 112 br label %bb20 113 114bb20: ; preds = %bb16, %bb13 115 %tmp21 = phi i32 [ %tmp19, %bb16 ], [ 0, %bb13 ] 116 %tmp22 = phi i1 [ false, %bb16 ], [ %tmp14, %bb13 ] 117 br label %bb9 118 119bb23: ; preds = %bb10 120 ret void 121} 122 123; Earlier version of above, before a run of the structurizer. 124; IR-LABEL: @nested_loop_conditions( 125 126; IR: Flow3: 127; IR-NEXT: call void @llvm.amdgcn.end.cf(i64 %21) 128; IR-NEXT: %0 = call { i1, i64 } @llvm.amdgcn.if(i1 %13) 129; IR-NEXT: %1 = extractvalue { i1, i64 } %0, 0 130; IR-NEXT: %2 = extractvalue { i1, i64 } %0, 1 131; IR-NEXT: br i1 %1, label %bb4.bb13_crit_edge, label %Flow4 132 133; IR: Flow4: 134; IR-NEXT: %3 = phi i1 [ true, %bb4.bb13_crit_edge ], [ false, %Flow3 ] 135; IR-NEXT: call void @llvm.amdgcn.end.cf(i64 %2) 136; IR-NEXT: br label %Flow 137 138; IR: Flow: 139; IR-NEXT: %4 = phi i1 [ %3, %Flow4 ], [ true, %bb ] 140; IR-NEXT: %5 = call { i1, i64 } @llvm.amdgcn.if(i1 %4) 141; IR-NEXT: %6 = extractvalue { i1, i64 } %5, 0 142; IR-NEXT: %7 = extractvalue { i1, i64 } %5, 1 143; IR-NEXT: br i1 %6, label %bb13, label %bb31 144 145; IR: bb14: 146; IR: %tmp15 = icmp eq i32 %tmp1037, 1 147; IR-NEXT: %8 = call { i1, i64 } @llvm.amdgcn.if(i1 %tmp15) 148 149; IR: Flow1: 150; IR-NEXT: %loop.phi = phi i64 [ %18, %bb21 ], [ %phi.broken, %bb14 ] 151; IR-NEXT: %11 = phi <4 x i32> [ %tmp9, %bb21 ], [ undef, %bb14 ] 152; IR-NEXT: %12 = phi i32 [ %tmp10, %bb21 ], [ undef, %bb14 ] 153; IR-NEXT: %13 = phi i1 [ %17, %bb21 ], [ false, %bb14 ] 154; IR-NEXT: %14 = phi i1 [ false, %bb21 ], [ true, %bb14 ] 155; IR-NEXT: %15 = call i64 @llvm.amdgcn.else.break(i64 %10, i64 %loop.phi) 156; IR-NEXT: call void @llvm.amdgcn.end.cf(i64 %10) 157; IR-NEXT: %16 = call i1 @llvm.amdgcn.loop(i64 %15) 158; IR-NEXT: br i1 %16, label %Flow2, label %bb14 159 160; IR: bb21: 161; IR: %tmp12 = icmp slt i32 %tmp11, 9 162; IR-NEXT: %17 = xor i1 %tmp12, true 163; IR-NEXT: %18 = call i64 @llvm.amdgcn.if.break(i1 %17, i64 %phi.broken) 164; IR-NEXT: br label %Flow1 165 166; IR: Flow2: 167; IR-NEXT: call void @llvm.amdgcn.end.cf(i64 %15) 168; IR-NEXT: %19 = call { i1, i64 } @llvm.amdgcn.if(i1 %14) 169; IR-NEXT: %20 = extractvalue { i1, i64 } %19, 0 170; IR-NEXT: %21 = extractvalue { i1, i64 } %19, 1 171; IR-NEXT: br i1 %20, label %bb31.loopexit, label %Flow3 172 173; IR: bb31: 174; IR-NEXT: call void @llvm.amdgcn.end.cf(i64 %7) 175; IR-NEXT: store volatile i32 0, i32 addrspace(1)* undef 176; IR-NEXT: ret void 177 178 179; GCN-LABEL: {{^}}nested_loop_conditions: 180 181; GCN: v_cmp_lt_i32_e32 vcc, 8, v 182; GCN: s_and_b64 vcc, exec, vcc 183; GCN: s_cbranch_vccnz [[BB31:BB[0-9]+_[0-9]+]] 184 185; GCN: [[BB14:BB[0-9]+_[0-9]+]]: ; %bb14 186; GCN: v_cmp_ne_u32_e32 vcc, 1, v 187; GCN-NEXT: s_and_b64 vcc, exec, vcc 188; GCN-NEXT: s_cbranch_vccnz [[BB31]] 189 190; GCN: [[BB18:BB[0-9]+_[0-9]+]]: ; %bb18 191; GCN: buffer_load_dword 192; GCN: v_cmp_lt_i32_e32 vcc, 8, v 193; GCN-NEXT: s_and_b64 vcc, exec, vcc 194; GCN-NEXT: s_cbranch_vccnz [[BB18]] 195 196; GCN: buffer_load_dword 197; GCN: buffer_load_dword 198; GCN: v_cmp_gt_i32_e32 vcc, 9 199; GCN-NEXT: s_and_b64 vcc, exec, vcc 200; GCN-NEXT: s_cbranch_vccnz [[BB14]] 201 202; GCN: [[BB31]]: 203; GCN: buffer_store_dword 204; GCN: s_endpgm 205define amdgpu_kernel void @nested_loop_conditions(i64 addrspace(1)* nocapture %arg) #0 { 206bb: 207 %tmp = tail call i32 @llvm.amdgcn.workitem.id.x() #1 208 %tmp1 = zext i32 %tmp to i64 209 %tmp2 = getelementptr inbounds i64, i64 addrspace(1)* %arg, i64 %tmp1 210 %tmp3 = load i64, i64 addrspace(1)* %tmp2, align 16 211 %tmp932 = load <4 x i32>, <4 x i32> addrspace(1)* undef, align 16 212 %tmp1033 = extractelement <4 x i32> %tmp932, i64 0 213 %tmp1134 = load volatile i32, i32 addrspace(1)* undef 214 %tmp1235 = icmp slt i32 %tmp1134, 9 215 br i1 %tmp1235, label %bb14.lr.ph, label %bb13 216 217bb14.lr.ph: ; preds = %bb 218 br label %bb14 219 220bb4.bb13_crit_edge: ; preds = %bb21 221 br label %bb13 222 223bb13: ; preds = %bb4.bb13_crit_edge, %bb 224 br label %bb31 225 226bb14: ; preds = %bb21, %bb14.lr.ph 227 %tmp1037 = phi i32 [ %tmp1033, %bb14.lr.ph ], [ %tmp10, %bb21 ] 228 %tmp936 = phi <4 x i32> [ %tmp932, %bb14.lr.ph ], [ %tmp9, %bb21 ] 229 %tmp15 = icmp eq i32 %tmp1037, 1 230 br i1 %tmp15, label %bb16, label %bb31.loopexit 231 232bb16: ; preds = %bb14 233 %tmp17 = bitcast i64 %tmp3 to <2 x i32> 234 br label %bb18 235 236bb18: ; preds = %bb18, %bb16 237 %tmp19 = load volatile i32, i32 addrspace(1)* undef 238 %tmp20 = icmp slt i32 %tmp19, 9 239 br i1 %tmp20, label %bb21, label %bb18 240 241bb21: ; preds = %bb18 242 %tmp22 = extractelement <2 x i32> %tmp17, i64 1 243 %tmp23 = lshr i32 %tmp22, 16 244 %tmp24 = select i1 undef, i32 undef, i32 %tmp23 245 %tmp25 = uitofp i32 %tmp24 to float 246 %tmp26 = fmul float %tmp25, 0x3EF0001000000000 247 %tmp27 = fsub float %tmp26, undef 248 %tmp28 = fcmp olt float %tmp27, 5.000000e-01 249 %tmp29 = select i1 %tmp28, i64 1, i64 2 250 %tmp30 = extractelement <4 x i32> %tmp936, i64 %tmp29 251 %tmp7 = zext i32 %tmp30 to i64 252 %tmp8 = getelementptr inbounds <4 x i32>, <4 x i32> addrspace(1)* undef, i64 %tmp7 253 %tmp9 = load <4 x i32>, <4 x i32> addrspace(1)* %tmp8, align 16 254 %tmp10 = extractelement <4 x i32> %tmp9, i64 0 255 %tmp11 = load volatile i32, i32 addrspace(1)* undef 256 %tmp12 = icmp slt i32 %tmp11, 9 257 br i1 %tmp12, label %bb14, label %bb4.bb13_crit_edge 258 259bb31.loopexit: ; preds = %bb14 260 br label %bb31 261 262bb31: ; preds = %bb31.loopexit, %bb13 263 store volatile i32 0, i32 addrspace(1)* undef 264 ret void 265} 266 267declare i32 @llvm.amdgcn.workitem.id.x() #1 268 269attributes #0 = { nounwind } 270attributes #1 = { nounwind readnone } 271