[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.
diff --git a/llvm/lib/CodeGen/BranchRelaxation.cpp b/llvm/lib/CodeGen/BranchRelaxation.cpp
index f05517d..4845d06 100644
--- a/llvm/lib/CodeGen/BranchRelaxation.cpp
+++ b/llvm/lib/CodeGen/BranchRelaxation.cpp
@@ -67,16 +67,13 @@
unsigned postOffset(const MachineBasicBlock &MBB) const {
const unsigned PO = Offset + Size;
const Align Alignment = MBB.getAlignment();
- if (Alignment == 1)
- return PO;
-
const Align ParentAlign = MBB.getParent()->getAlignment();
if (Alignment <= ParentAlign)
- return PO + offsetToAlignment(PO, Alignment);
+ return alignTo(PO, Alignment);
// The alignment of this MBB is larger than the function's alignment, so we
// can't tell whether or not it will insert nops. Assume that it will.
- return PO + Alignment.value() + offsetToAlignment(PO, Alignment);
+ return alignTo(PO, Alignment) + Alignment.value() - ParentAlign.value();
}
};
@@ -129,7 +126,6 @@
unsigned PrevNum = MF->begin()->getNumber();
for (MachineBasicBlock &MBB : *MF) {
const unsigned Num = MBB.getNumber();
- assert(isAligned(MBB.getAlignment(), BlockInfo[Num].Offset));
assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].Offset);
assert(BlockInfo[Num].Size == computeBlockSize(MBB));
PrevNum = Num;
@@ -195,10 +191,9 @@
void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) {
unsigned PrevNum = Start.getNumber();
- for (auto &MBB : make_range(MachineFunction::iterator(Start), MF->end())) {
+ for (auto &MBB :
+ make_range(std::next(MachineFunction::iterator(Start)), MF->end())) {
unsigned Num = MBB.getNumber();
- if (!Num) // block zero is never changed from offset zero.
- continue;
// Get the offset and known bits at the end of the layout predecessor.
// Include the alignment of the current block.
BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB);