Skip to content

Commit 886d2c2

Browse files
committed
[BranchRelaxation] Simplify offset computation and fix a bug in adjustBlockOffsets()
If Start!=0, adjustBlockOffsets() may unnecessarily adjust the offset of Start. There is no correctness issue, but it can create more block splits.
1 parent a7818e6 commit 886d2c2

File tree

3 files changed

+8
-20
lines changed

3 files changed

+8
-20
lines changed

llvm/lib/CodeGen/BranchRelaxation.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,13 @@ class BranchRelaxation : public MachineFunctionPass {
6767
unsigned postOffset(const MachineBasicBlock &MBB) const {
6868
const unsigned PO = Offset + Size;
6969
const Align Alignment = MBB.getAlignment();
70-
if (Alignment == 1)
71-
return PO;
72-
7370
const Align ParentAlign = MBB.getParent()->getAlignment();
7471
if (Alignment <= ParentAlign)
75-
return PO + offsetToAlignment(PO, Alignment);
72+
return alignTo(PO, Alignment);
7673

7774
// The alignment of this MBB is larger than the function's alignment, so we
7875
// can't tell whether or not it will insert nops. Assume that it will.
79-
return PO + Alignment.value() + offsetToAlignment(PO, Alignment);
76+
return alignTo(PO, Alignment) + Alignment.value() - ParentAlign.value();
8077
}
8178
};
8279

@@ -129,7 +126,6 @@ void BranchRelaxation::verify() {
129126
unsigned PrevNum = MF->begin()->getNumber();
130127
for (MachineBasicBlock &MBB : *MF) {
131128
const unsigned Num = MBB.getNumber();
132-
assert(isAligned(MBB.getAlignment(), BlockInfo[Num].Offset));
133129
assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].Offset);
134130
assert(BlockInfo[Num].Size == computeBlockSize(MBB));
135131
PrevNum = Num;
@@ -195,10 +191,9 @@ unsigned BranchRelaxation::getInstrOffset(const MachineInstr &MI) const {
195191

196192
void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) {
197193
unsigned PrevNum = Start.getNumber();
198-
for (auto &MBB : make_range(MachineFunction::iterator(Start), MF->end())) {
194+
for (auto &MBB :
195+
make_range(std::next(MachineFunction::iterator(Start)), MF->end())) {
199196
unsigned Num = MBB.getNumber();
200-
if (!Num) // block zero is never changed from offset zero.
201-
continue;
202197
// Get the offset and known bits at the end of the layout predecessor.
203198
// Include the alignment of the current block.
204199
BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB);

llvm/test/CodeGen/AArch64/branch-relax-bcc.ll

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
; CHECK-LABEL: invert_bcc:
44
; CHECK: fcmp s0, s1
5-
; CHECK-NEXT: b.eq [[JUMP_BB1:LBB[0-9]+_[0-9]+]]
6-
; CHECK-NEXT: b [[JUMP_BB2:LBB[0-9]+_[0-9]+]]
7-
8-
; CHECK-NEXT: [[JUMP_BB1]]:
5+
; CHECK-NEXT: b.ne [[JUMP_BB1:LBB[0-9]+_[0-9]+]]
96
; CHECK-NEXT: b [[BB1:LBB[0-9]+_[0-9]+]]
107

11-
; CHECK-NEXT: [[JUMP_BB2]]:
8+
; CHECK-NEXT: [[JUMP_BB1]]:
129
; CHECK-NEXT: b.vc [[BB2:LBB[0-9]+_[0-9]+]]
1310
; CHECK-NEXT: b [[BB1]]
1411

@@ -41,10 +38,7 @@ declare i32 @foo() #0
4138

4239
; CHECK-LABEL: _block_split:
4340
; CHECK: cmp w0, #5
44-
; CHECK-NEXT: b.eq [[LONG_BR_BB:LBB[0-9]+_[0-9]+]]
45-
; CHECK-NEXT: b [[LOR_LHS_FALSE_BB:LBB[0-9]+_[0-9]+]]
46-
47-
; CHECK: [[LONG_BR_BB]]:
41+
; CHECK-NEXT: b.ne [[LOR_LHS_FALSE_BB:LBB[0-9]+_[0-9]+]]
4842
; CHECK-NEXT: b [[IF_THEN_BB:LBB[0-9]+_[0-9]+]]
4943

5044
; CHECK: [[LOR_LHS_FALSE_BB]]:

llvm/test/CodeGen/AMDGPU/branch-relaxation.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,7 @@ ret:
491491

492492
; GCN-LABEL: {{^}}long_branch_hang:
493493
; GCN: s_cmp_lt_i32 s{{[0-9]+}}, 6
494-
; GCN: s_cbranch_scc1 {{BB[0-9]+_[0-9]+}}
495-
; GCN-NEXT: s_branch [[LONG_BR_0:BB[0-9]+_[0-9]+]]
494+
; GCN: s_cbranch_scc0 [[LONG_BR_0:BB[0-9]+_[0-9]+]]
496495
; GCN-NEXT: BB{{[0-9]+_[0-9]+}}:
497496

498497
; GCN: s_add_u32 s{{[0-9]+}}, s{{[0-9]+}}, [[LONG_BR_DEST0:BB[0-9]+_[0-9]+]]-(

0 commit comments

Comments
 (0)