Skip to content

Commit ce60a03

Browse files
authored
[BranchRelaxation] Fix invalid branch generation in branch-relaxation (#162065)
If we have MBB with only one successor which is accessable through both conditional and unconditional branches (TBB == FBB), in `fixupConditionalBranch` we will first replace FBB with NewMBB in successors list - `MBB->replaceSuccessor(FBB, NewBB);`, and then create branch to TBB - `insertBranch(MBB, &NextBB, TBB, Cond);`, ending up with two branches to different blocks, but only one successor. Fixes: llvm/llvm-project#162063
1 parent 9deba01 commit ce60a03

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

llvm/lib/CodeGen/BranchRelaxation.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,20 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
491491
return true;
492492
}
493493
if (FBB) {
494+
// If we get here with a MBB which ends like this:
495+
//
496+
// bb.1:
497+
// successors: %bb.2;
498+
// ...
499+
// BNE $x1, $x0, %bb.2
500+
// PseudoBR %bb.2
501+
//
502+
// Just remove conditional branch.
503+
if (TBB == FBB) {
504+
removeBranch(MBB);
505+
insertUncondBranch(MBB, TBB);
506+
return true;
507+
}
494508
// We need to split the basic block here to obtain two long-range
495509
// unconditional branches.
496510
NewBB = createNewBlockAfter(*MBB);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc %s -mtriple=riscv64 -run-pass=branch-relaxation -o - -verify-machineinstrs | FileCheck %s
3+
4+
--- |
5+
define void @foo() {
6+
ret void
7+
}
8+
...
9+
---
10+
name: foo
11+
tracksRegLiveness: true
12+
body: |
13+
; CHECK-LABEL: name: foo
14+
; CHECK: bb.0:
15+
; CHECK-NEXT: successors: %bb.2(0x80000000)
16+
; CHECK-NEXT: liveins: $x1
17+
; CHECK-NEXT: {{ $}}
18+
; CHECK-NEXT: PseudoBR %bb.2
19+
; CHECK-NEXT: {{ $}}
20+
; CHECK-NEXT: bb.1:
21+
; CHECK-NEXT: successors: %bb.2(0x80000000)
22+
; CHECK-NEXT: liveins: $x1
23+
; CHECK-NEXT: {{ $}}
24+
; CHECK-NEXT: INLINEASM &".space 4096", 1 /* sideeffect attdialect */
25+
; CHECK-NEXT: BGE $x1, $x0, %bb.2
26+
; CHECK-NEXT: {{ $}}
27+
; CHECK-NEXT: bb.2:
28+
; CHECK-NEXT: PseudoRET
29+
bb.0:
30+
liveins: $x1
31+
BNE $x1, $x0, %bb.3
32+
PseudoBR %bb.3
33+
bb.1:
34+
liveins: $x1
35+
INLINEASM &".space 4096", 1
36+
BGE $x1, $x0, %bb.3
37+
bb.3:
38+
PseudoRET
39+
## NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:

0 commit comments

Comments
 (0)