1; RUN: llc -march=amdgcn -verify-machineinstrs -amdgpu-s-branch-bits=4 < %s | FileCheck -check-prefix=GCN %s 2; Restrict maximum branch to between +7 and -8 dwords 3 4; Used to emit an always 4 byte instruction. Inline asm always assumes 5; each instruction is the maximum size. 6declare void @llvm.amdgcn.s.sleep(i32) #0 7 8declare i32 @llvm.amdgcn.workitem.id.x() #1 9 10 11; GCN-LABEL: {{^}}uniform_conditional_max_short_forward_branch: 12; GCN: s_load_dword [[CND:s[0-9]+]] 13; GCN: s_cmp_eq_u32 [[CND]], 0 14; GCN-NEXT: s_cbranch_scc1 [[BB3:BB[0-9]+_[0-9]+]] 15 16 17; GCN-NEXT: ; BB#1: ; %bb2 18; GCN-NEXT: ;;#ASMSTART 19; GCN-NEXT: v_nop_e64 20; GCN-NEXT: v_nop_e64 21; GCN-NEXT: v_nop_e64 22; GCN-NEXT: ;;#ASMEND 23; GCN-NEXT: s_sleep 0 24 25; GCN-NEXT: [[BB3]]: ; %bb3 26; GCN: v_mov_b32_e32 [[V_CND:v[0-9]+]], [[CND]] 27; GCN: buffer_store_dword [[V_CND]] 28; GCN: s_endpgm 29define void @uniform_conditional_max_short_forward_branch(i32 addrspace(1)* %arg, i32 %cnd) #0 { 30bb: 31 %cmp = icmp eq i32 %cnd, 0 32 br i1 %cmp, label %bb3, label %bb2 ; +8 dword branch 33 34bb2: 35; 24 bytes 36 call void asm sideeffect 37 "v_nop_e64 38 v_nop_e64 39 v_nop_e64", ""() #0 40 call void @llvm.amdgcn.s.sleep(i32 0) 41 br label %bb3 42 43bb3: 44 store volatile i32 %cnd, i32 addrspace(1)* %arg 45 ret void 46} 47 48; GCN-LABEL: {{^}}uniform_conditional_min_long_forward_branch: 49; GCN: s_load_dword [[CND:s[0-9]+]] 50; GCN: s_cmp_eq_u32 [[CND]], 0 51; GCN-NEXT: s_cbranch_scc0 [[LONGBB:BB[0-9]+_[0-9]+]] 52 53; GCN-NEXT: [[LONG_JUMP:BB[0-9]+_[0-9]+]]: ; %bb0 54; GCN-NEXT: s_getpc_b64 vcc 55; GCN-NEXT: s_add_u32 vcc_lo, vcc_lo, [[ENDBB:BB[0-9]+_[0-9]+]]-([[LONG_JUMP]]+4) 56; GCN-NEXT: s_addc_u32 vcc_hi, vcc_hi, 0 57; GCN-NEXT: s_setpc_b64 vcc 58 59; GCN-NEXT: [[LONGBB]]: 60; GCN-NEXT: ;;#ASMSTART 61; GCN: v_nop_e64 62; GCN: v_nop_e64 63; GCN: v_nop_e64 64; GCN: v_nop_e64 65; GCN-NEXT: ;;#ASMEND 66 67; GCN-NEXT: [[ENDBB]]: 68; GCN: v_mov_b32_e32 [[V_CND:v[0-9]+]], [[CND]] 69; GCN: buffer_store_dword [[V_CND]] 70; GCN: s_endpgm 71define void @uniform_conditional_min_long_forward_branch(i32 addrspace(1)* %arg, i32 %cnd) #0 { 72bb0: 73 %cmp = icmp eq i32 %cnd, 0 74 br i1 %cmp, label %bb3, label %bb2 ; +9 dword branch 75 76bb2: 77; 32 bytes 78 call void asm sideeffect 79 "v_nop_e64 80 v_nop_e64 81 v_nop_e64 82 v_nop_e64", ""() #0 83 br label %bb3 84 85bb3: 86 store volatile i32 %cnd, i32 addrspace(1)* %arg 87 ret void 88} 89 90; GCN-LABEL: {{^}}uniform_conditional_min_long_forward_vcnd_branch: 91; GCN: s_load_dword [[CND:s[0-9]+]] 92; GCN-DAG: v_cmp_eq_f32_e64 [[CMP:s\[[0-9]+:[0-9]+\]]], [[CND]], 0 93; GCN-DAG: v_mov_b32_e32 [[V_CND:v[0-9]+]], [[CND]] 94; GCN: s_and_b64 vcc, exec, [[CMP]] 95; GCN-NEXT: s_cbranch_vccz [[LONGBB:BB[0-9]+_[0-9]+]] 96 97; GCN-NEXT: [[LONG_JUMP:BB[0-9]+_[0-9]+]]: ; %bb0 98; GCN-NEXT: s_getpc_b64 vcc 99; GCN-NEXT: s_add_u32 vcc_lo, vcc_lo, [[ENDBB:BB[0-9]+_[0-9]+]]-([[LONG_JUMP]]+4) 100; GCN-NEXT: s_addc_u32 vcc_hi, vcc_hi, 0 101; GCN-NEXT: s_setpc_b64 vcc 102 103; GCN-NEXT: [[LONGBB]]: 104; GCN: v_nop_e64 105; GCN: v_nop_e64 106; GCN: v_nop_e64 107; GCN: v_nop_e64 108 109; GCN: [[ENDBB]]: 110; GCN: buffer_store_dword [[V_CND]] 111; GCN: s_endpgm 112define void @uniform_conditional_min_long_forward_vcnd_branch(float addrspace(1)* %arg, float %cnd) #0 { 113bb0: 114 %cmp = fcmp oeq float %cnd, 0.0 115 br i1 %cmp, label %bb3, label %bb2 ; + 8 dword branch 116 117bb2: 118 call void asm sideeffect " ; 32 bytes 119 v_nop_e64 120 v_nop_e64 121 v_nop_e64 122 v_nop_e64", ""() #0 123 br label %bb3 124 125bb3: 126 store volatile float %cnd, float addrspace(1)* %arg 127 ret void 128} 129 130; GCN-LABEL: {{^}}min_long_forward_vbranch: 131 132; GCN: buffer_load_dword 133; GCN: v_cmp_ne_u32_e32 vcc, 0, v{{[0-9]+}} 134; GCN: s_and_saveexec_b64 [[SAVE:s\[[0-9]+:[0-9]+\]]], vcc 135; GCN: s_xor_b64 [[SAVE]], exec, [[SAVE]] 136 137; GCN: v_nop_e64 138; GCN: v_nop_e64 139; GCN: v_nop_e64 140; GCN: v_nop_e64 141 142; GCN: s_or_b64 exec, exec, [[SAVE]] 143; GCN: buffer_store_dword 144; GCN: s_endpgm 145define void @min_long_forward_vbranch(i32 addrspace(1)* %arg) #0 { 146bb: 147 %tid = call i32 @llvm.amdgcn.workitem.id.x() 148 %tid.ext = zext i32 %tid to i64 149 %gep = getelementptr inbounds i32, i32 addrspace(1)* %arg, i64 %tid.ext 150 %load = load volatile i32, i32 addrspace(1)* %gep 151 %cmp = icmp eq i32 %load, 0 152 br i1 %cmp, label %bb3, label %bb2 ; + 8 dword branch 153 154bb2: 155 call void asm sideeffect " ; 32 bytes 156 v_nop_e64 157 v_nop_e64 158 v_nop_e64 159 v_nop_e64", ""() #0 160 br label %bb3 161 162bb3: 163 store volatile i32 %load, i32 addrspace(1)* %gep 164 ret void 165} 166 167; FIXME: Should be able to use s_cbranch_scc0 168; GCN-LABEL: {{^}}long_backward_sbranch: 169; GCN: v_mov_b32_e32 [[LOOPIDX:v[0-9]+]], 0{{$}} 170 171; GCN: [[LOOPBB:BB[0-9]+_[0-9]+]]: ; %bb2 172; GCN-NEXT: ; =>This Inner Loop Header: Depth=1 173; GCN-NEXT: v_add_i32_e32 [[INC:v[0-9]+]], vcc, 1, [[LOOPIDX]] 174; GCN-NEXT: v_cmp_gt_i32_e32 vcc, 10, [[INC]] 175 176; GCN-NEXT: ;;#ASMSTART 177; GCN-NEXT: v_nop_e64 178; GCN-NEXT: v_nop_e64 179; GCN-NEXT: v_nop_e64 180; GCN-NEXT: ;;#ASMEND 181 182; GCN-NEXT: s_and_b64 vcc, exec, vcc 183; GCN-NEXT: s_cbranch_vccz [[ENDBB:BB[0-9]+_[0-9]+]] 184 185; GCN-NEXT: [[LONG_JUMP:BB[0-9]+_[0-9]+]]: ; %bb2 186; GCN-NEXT: ; in Loop: Header=[[LOOPBB]] Depth=1 187; GCN-NEXT: s_getpc_b64 vcc 188; GCN-NEXT: s_sub_u32 vcc_lo, vcc_lo, ([[LONG_JUMP]]+4)-[[LOOPBB]] 189; GCN-NEXT: s_subb_u32 vcc_hi, vcc_hi, 0 190; GCN-NEXT: s_setpc_b64 vcc 191 192; GCN-NEXT: [[ENDBB]]: 193; GCN-NEXT: s_endpgm 194define void @long_backward_sbranch(i32 addrspace(1)* %arg) #0 { 195bb: 196 br label %bb2 197 198bb2: 199 %loop.idx = phi i32 [ 0, %bb ], [ %inc, %bb2 ] 200 ; 24 bytes 201 call void asm sideeffect 202 "v_nop_e64 203 v_nop_e64 204 v_nop_e64", ""() #0 205 %inc = add nsw i32 %loop.idx, 1 ; add cost 4 206 %cmp = icmp slt i32 %inc, 10 ; condition cost = 8 207 br i1 %cmp, label %bb2, label %bb3 ; - 208 209bb3: 210 ret void 211} 212 213; Requires expansion of unconditional branch from %bb2 to %bb4 (and 214; expansion of conditional branch from %bb to %bb3. 215 216; GCN-LABEL: {{^}}uniform_unconditional_min_long_forward_branch: 217; GCN: s_cmp_eq_u32 218; GCN-NEXT: s_cbranch_scc0 [[BB2:BB[0-9]+_[0-9]+]] 219 220; GCN-NEXT: [[LONG_JUMP0:BB[0-9]+_[0-9]+]]: ; %bb0 221; GCN-NEXT: s_getpc_b64 vcc 222; GCN-NEXT: s_add_u32 vcc_lo, vcc_lo, [[BB3:BB[0-9]_[0-9]+]]-([[LONG_JUMP0]]+4) 223; GCN-NEXT: s_addc_u32 vcc_hi, vcc_hi, 0{{$}} 224; GCN-NEXT: s_setpc_b64 vcc 225 226; GCN-NEXT: [[BB2]]: ; %bb2 227; GCN: v_mov_b32_e32 [[BB2_K:v[0-9]+]], 17 228; GCN: buffer_store_dword [[BB2_K]] 229; GCN: s_waitcnt vmcnt(0) 230 231; GCN-NEXT: [[LONG_JUMP1:BB[0-9]+_[0-9]+]]: ; %bb2 232; GCN-NEXT: s_getpc_b64 vcc 233; GCN-NEXT: s_add_u32 vcc_lo, vcc_lo, [[BB4:BB[0-9]_[0-9]+]]-([[LONG_JUMP1]]+4) 234; GCN-NEXT: s_addc_u32 vcc_hi, vcc_hi, 0{{$}} 235; GCN-NEXT: s_setpc_b64 vcc 236 237; GCN: [[BB3]]: ; %bb3 238; GCN: v_nop_e64 239; GCN: v_nop_e64 240; GCN: v_nop_e64 241; GCN: v_nop_e64 242; GCN: ;;#ASMEND 243 244; GCN-NEXT: [[BB4]]: ; %bb4 245; GCN: v_mov_b32_e32 [[BB4_K:v[0-9]+]], 63 246; GCN: buffer_store_dword [[BB4_K]] 247; GCN-NEXT: s_endpgm 248; GCN-NEXT: .Lfunc_end{{[0-9]+}}: 249define void @uniform_unconditional_min_long_forward_branch(i32 addrspace(1)* %arg, i32 %arg1) { 250bb0: 251 %tmp = icmp ne i32 %arg1, 0 252 br i1 %tmp, label %bb2, label %bb3 253 254bb2: 255 store volatile i32 17, i32 addrspace(1)* undef 256 br label %bb4 257 258bb3: 259 ; 32 byte asm 260 call void asm sideeffect 261 "v_nop_e64 262 v_nop_e64 263 v_nop_e64 264 v_nop_e64", ""() #0 265 br label %bb4 266 267bb4: 268 store volatile i32 63, i32 addrspace(1)* %arg 269 ret void 270} 271 272; GCN-LABEL: {{^}}uniform_unconditional_min_long_backward_branch: 273; GCN-NEXT: ; BB#0: ; %entry 274 275; GCN-NEXT: [[LOOP:BB[0-9]_[0-9]+]]: ; %loop 276; GCN-NEXT: ; =>This Inner Loop Header: Depth=1 277; GCN-NEXT: ;;#ASMSTART 278; GCN-NEXT: v_nop_e64 279; GCN-NEXT: v_nop_e64 280; GCN-NEXT: v_nop_e64 281; GCN-NEXT: v_nop_e64 282; GCN-NEXT: ;;#ASMEND 283 284; GCN-NEXT: [[LONGBB:BB[0-9]+_[0-9]+]]: ; %loop 285; GCN-NEXT: ; in Loop: Header=[[LOOP]] Depth=1 286; GCN-NEXT: s_getpc_b64 vcc 287; GCN-NEXT: s_sub_u32 vcc_lo, vcc_lo, ([[LONGBB]]+4)-[[LOOP]] 288; GCN-NEXT: s_subb_u32 vcc_hi, vcc_hi, 0{{$}} 289; GCN-NEXT: s_setpc_b64 vcc 290; GCN-NEXT .Lfunc_end{{[0-9]+}}: 291define void @uniform_unconditional_min_long_backward_branch(i32 addrspace(1)* %arg, i32 %arg1) { 292entry: 293 br label %loop 294 295loop: 296 ; 32 byte asm 297 call void asm sideeffect 298 "v_nop_e64 299 v_nop_e64 300 v_nop_e64 301 v_nop_e64", ""() #0 302 br label %loop 303} 304 305; Expansion of branch from %bb1 to %bb3 introduces need to expand 306; branch from %bb0 to %bb2 307 308; GCN-LABEL: {{^}}expand_requires_expand: 309; GCN-NEXT: ; BB#0: ; %bb0 310; GCN: s_load_dword 311; GCN: s_cmp_lt_i32 s{{[0-9]+}}, 0{{$}} 312; GCN-NEXT: s_cbranch_scc0 [[BB1:BB[0-9]+_[0-9]+]] 313 314; GCN-NEXT: [[LONGBB0:BB[0-9]+_[0-9]+]]: ; %bb0 315; GCN-NEXT: s_getpc_b64 vcc 316; GCN-NEXT: s_add_u32 vcc_lo, vcc_lo, [[BB2:BB[0-9]_[0-9]+]]-([[LONGBB0]]+4) 317; GCN-NEXT: s_addc_u32 vcc_hi, vcc_hi, 0{{$}} 318; GCN-NEXT: s_setpc_b64 vcc 319 320; GCN-NEXT: [[BB1]]: ; %bb1 321; GCN-NEXT: s_load_dword 322; GCN-NEXT: s_waitcnt lgkmcnt(0) 323; GCN-NEXT: s_cmp_eq_u32 s{{[0-9]+}}, 3{{$}} 324; GCN-NEXT: s_cbranch_scc0 [[BB2:BB[0-9]_[0-9]+]] 325 326; GCN-NEXT: [[LONGBB1:BB[0-9]+_[0-9]+]]: ; %bb1 327; GCN-NEXT: s_getpc_b64 vcc 328; GCN-NEXT: s_add_u32 vcc_lo, vcc_lo, [[BB3:BB[0-9]+_[0-9]+]]-([[LONGBB1]]+4) 329; GCN-NEXT: s_addc_u32 vcc_hi, vcc_hi, 0{{$}} 330; GCN-NEXT: s_setpc_b64 vcc 331 332; GCN-NEXT: [[BB2]]: ; %bb2 333; GCN-NEXT: ;;#ASMSTART 334; GCN-NEXT: v_nop_e64 335; GCN-NEXT: v_nop_e64 336; GCN-NEXT: v_nop_e64 337; GCN-NEXT: v_nop_e64 338; GCN-NEXT: ;;#ASMEND 339 340; GCN-NEXT: [[BB3]]: ; %bb3 341; GCN-NEXT: s_endpgm 342define void @expand_requires_expand(i32 %cond0) #0 { 343bb0: 344 %tmp = tail call i32 @llvm.amdgcn.workitem.id.x() #0 345 %cmp0 = icmp slt i32 %cond0, 0 346 br i1 %cmp0, label %bb2, label %bb1 347 348bb1: 349 %val = load volatile i32, i32 addrspace(2)* undef 350 %cmp1 = icmp eq i32 %val, 3 351 br i1 %cmp1, label %bb3, label %bb2 352 353bb2: 354 call void asm sideeffect 355 "v_nop_e64 356 v_nop_e64 357 v_nop_e64 358 v_nop_e64", ""() #0 359 br label %bb3 360 361bb3: 362 ret void 363} 364 365; Requires expanding of required skip branch. 366 367; GCN-LABEL: {{^}}uniform_inside_divergent: 368; GCN: v_cmp_gt_u32_e32 vcc, 16, v{{[0-9]+}} 369; GCN-NEXT: s_and_saveexec_b64 [[MASK:s\[[0-9]+:[0-9]+\]]], vcc 370; GCN-NEXT: s_xor_b64 [[MASK1:s\[[0-9]+:[0-9]+\]]], exec, [[MASK]] 371; GCN-NEXT: ; mask branch [[ENDIF:BB[0-9]+_[0-9]+]] 372; GCN-NEXT: s_cbranch_execnz [[IF:BB[0-9]+_[0-9]+]] 373 374; GCN-NEXT: [[LONGBB:BB[0-9]+_[0-9]+]]: ; %entry 375; GCN-NEXT: s_getpc_b64 vcc 376; GCN-NEXT: s_add_u32 vcc_lo, vcc_lo, [[BB2:BB[0-9]_[0-9]+]]-([[LONGBB]]+4) 377; GCN-NEXT: s_addc_u32 vcc_hi, vcc_hi, 0{{$}} 378; GCN-NEXT: s_setpc_b64 vcc 379 380; GCN-NEXT: [[IF]]: ; %if 381; GCN: buffer_store_dword 382; GCN: s_cmp_lg_u32 383; GCN: s_cbranch_scc1 [[ENDIF]] 384 385; GCN-NEXT: ; BB#2: ; %if_uniform 386; GCN: buffer_store_dword 387; GCN: s_waitcnt vmcnt(0) 388 389; GCN-NEXT: [[ENDIF]]: ; %endif 390; GCN-NEXT: s_or_b64 exec, exec, [[MASK]] 391; GCN-NEXT: s_endpgm 392define void @uniform_inside_divergent(i32 addrspace(1)* %out, i32 %cond) #0 { 393entry: 394 %tid = call i32 @llvm.amdgcn.workitem.id.x() 395 %d_cmp = icmp ult i32 %tid, 16 396 br i1 %d_cmp, label %if, label %endif 397 398if: 399 store i32 0, i32 addrspace(1)* %out 400 %u_cmp = icmp eq i32 %cond, 0 401 br i1 %u_cmp, label %if_uniform, label %endif 402 403if_uniform: 404 store i32 1, i32 addrspace(1)* %out 405 br label %endif 406 407endif: 408 ret void 409} 410 411; si_mask_branch 412; s_cbranch_execz 413; s_branch 414 415; GCN-LABEL: {{^}}analyze_mask_branch: 416; GCN: v_cmp_lt_f32_e32 vcc 417; GCN-NEXT: s_and_saveexec_b64 [[MASK:s\[[0-9]+:[0-9]+\]]], vcc 418; GCN-NEXT: s_xor_b64 [[MASK]], exec, [[MASK]] 419; GCN-NEXT: ; mask branch [[RET:BB[0-9]+_[0-9]+]] 420; GCN-NEXT: s_cbranch_execz [[BRANCH_SKIP:BB[0-9]+_[0-9]+]] 421; GCN-NEXT: s_branch [[LOOP_BODY:BB[0-9]+_[0-9]+]] 422 423; GCN-NEXT: [[BRANCH_SKIP]]: ; %entry 424; GCN-NEXT: s_getpc_b64 vcc 425; GCN-NEXT: s_add_u32 vcc_lo, vcc_lo, [[RET]]-([[BRANCH_SKIP]]+4) 426; GCN-NEXT: s_addc_u32 vcc_hi, vcc_hi, 0 427; GCN-NEXT: s_setpc_b64 vcc 428 429; GCN-NEXT: [[LOOP_BODY]]: ; %loop_body 430; GCN: v_nop_e64 431; GCN: v_nop_e64 432; GCN: v_nop_e64 433; GCN: v_nop_e64 434; GCN: v_nop_e64 435; GCN: v_nop_e64 436; GCN: ;;#ASMEND 437; GCN-NEXT: s_and_b64 vcc, exec, -1{{$}} 438; GCN-NEXT: s_cbranch_vccz [[RET]] 439 440; GCN-NEXT: [[LONGBB:BB[0-9]+_[0-9]+]]: ; %loop_body 441; GCN-NEXT: ; in Loop: Header=[[LOOP_BODY]] Depth=1 442; GCN-NEXT: s_getpc_b64 vcc 443; GCN-NEXT: s_sub_u32 vcc_lo, vcc_lo, ([[LONGBB]]+4)-[[LOOP_BODY]] 444; GCN-NEXT: s_subb_u32 vcc_hi, vcc_hi, 0 445; GCN-NEXT: s_setpc_b64 vcc 446 447; GCN-NEXT: [[RET]]: ; %Flow 448; GCN-NEXT: s_or_b64 exec, exec, [[MASK]] 449; GCN: buffer_store_dword 450; GCN-NEXT: s_endpgm 451define void @analyze_mask_branch() #0 { 452entry: 453 %reg = call float asm sideeffect "v_mov_b32_e64 $0, 0", "=v"() 454 %cmp0 = fcmp ogt float %reg, 0.000000e+00 455 br i1 %cmp0, label %loop, label %ret 456 457loop: 458 %phi = phi float [ 0.000000e+00, %loop_body ], [ 1.000000e+00, %entry ] 459 call void asm sideeffect 460 "v_nop_e64 461 v_nop_e64", ""() #0 462 %cmp1 = fcmp olt float %phi, 8.0 463 br i1 %cmp1, label %loop_body, label %ret 464 465loop_body: 466 call void asm sideeffect 467 "v_nop_e64 468 v_nop_e64 469 v_nop_e64 470 v_nop_e64", ""() #0 471 br label %loop 472 473ret: 474 store volatile i32 7, i32 addrspace(1)* undef 475 ret void 476} 477 478attributes #0 = { nounwind } 479attributes #1 = { nounwind readnone } 480