Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion bolt/lib/Core/BinaryFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3591,6 +3591,18 @@ void BinaryFunction::fixBranches() {
auto &MIB = BC.MIB;
MCContext *Ctx = BC.Ctx.get();

// Caches `FunctionLayout::nextBasicBlock(IgnoreSplits = false)`.
// nextBasicBlock uses linear search to find the next block, so the loop
// below becomes O(n^2). This avoids that.
DenseMap<BinaryBasicBlock *, BinaryBasicBlock *> nextBasicBlock(
Layout.block_size());
for (size_t i = 0; i + 1 < Layout.block_size(); i++) {
auto current = Layout.block_begin() + i;
auto next = Layout.block_begin() + i + 1;
if (next != Layout.getFragment((*current)->getFragmentNum()).end())
nextBasicBlock.insert(std::pair(*current, *next));
}

for (BinaryBasicBlock *BB : BasicBlocks) {
const MCSymbol *TBB = nullptr;
const MCSymbol *FBB = nullptr;
Expand All @@ -3605,7 +3617,7 @@ void BinaryFunction::fixBranches() {

// Basic block that follows the current one in the final layout.
const BinaryBasicBlock *const NextBB =
Layout.getBasicBlockAfter(BB, /*IgnoreSplits=*/false);
nextBasicBlock.lookup_or(BB, nullptr);

if (BB->succ_size() == 1) {
// __builtin_unreachable() could create a conditional branch that
Expand Down
Loading