Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 16 additions & 5 deletions bolt/include/bolt/Core/FunctionLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,31 @@ class FunctionLayout {
return Blocks[Index];
}

/// Return the basic block after the given basic block iterator in the layout
/// or nullptr if the last basic block iterator is given.
const BinaryBasicBlock *getBasicBlockAfter(block_const_iterator BlockIt,
bool IgnoreSplits = true) const;

/// Returns the basic block after the given basic block in the layout or
/// nullptr if the last basic block is given.
///
/// Note: prefer the version that takes the iterator as this function uses
/// linear basic block lookup.
const BinaryBasicBlock *getBasicBlockAfter(const BinaryBasicBlock *BB,
bool IgnoreSplits = true) const;

/// Returns the basic block after the given basic block in the layout or
/// nullptr if the last basic block is given.
///
/// Note: prefer the version that takes the iterator as this function uses
/// linear basic block lookup.
BinaryBasicBlock *getBasicBlockAfter(const BinaryBasicBlock *const BB,
const bool IgnoreSplits = true) {
return const_cast<BinaryBasicBlock *>(
static_cast<const FunctionLayout &>(*this).getBasicBlockAfter(
BB, IgnoreSplits));
}

/// Returns the basic block after the given basic block in the layout or
/// nullptr if the last basic block is given.
const BinaryBasicBlock *getBasicBlockAfter(const BinaryBasicBlock *BB,
bool IgnoreSplits = true) const;

/// True if the layout contains at least two non-empty fragments.
bool isSplit() const;

Expand Down
6 changes: 4 additions & 2 deletions bolt/lib/Core/BinaryFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3598,7 +3598,9 @@ void BinaryFunction::fixBranches() {
auto &MIB = BC.MIB;
MCContext *Ctx = BC.Ctx.get();

for (BinaryBasicBlock *BB : BasicBlocks) {
for (auto BBI = Layout.block_begin(), BBE = Layout.block_end(); BBI != BBE;
++BBI) {
BinaryBasicBlock *BB = *BBI;
const MCSymbol *TBB = nullptr;
const MCSymbol *FBB = nullptr;
MCInst *CondBranch = nullptr;
Expand All @@ -3612,7 +3614,7 @@ void BinaryFunction::fixBranches() {

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

if (BB->succ_size() == 1) {
// __builtin_unreachable() could create a conditional branch that
Expand Down
20 changes: 13 additions & 7 deletions bolt/lib/Core/FunctionLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,29 @@ void FunctionLayout::clear() {
}

const BinaryBasicBlock *
FunctionLayout::getBasicBlockAfter(const BinaryBasicBlock *BB,
FunctionLayout::getBasicBlockAfter(block_const_iterator BBIter,
bool IgnoreSplits) const {
const block_const_iterator BBPos = find(blocks(), BB);
if (BBPos == block_end())
return nullptr;

const block_const_iterator BlockAfter = std::next(BBPos);
const block_const_iterator BlockAfter = std::next(BBIter);
if (BlockAfter == block_end())
return nullptr;

if (!IgnoreSplits)
if (BlockAfter == getFragment(BB->getFragmentNum()).end())
if (BlockAfter == getFragment((*BBIter)->getFragmentNum()).end())
return nullptr;

return *BlockAfter;
}

const BinaryBasicBlock *
FunctionLayout::getBasicBlockAfter(const BinaryBasicBlock *BB,
bool IgnoreSplits) const {
const block_const_iterator BBPos = find(blocks(), BB);
if (BBPos == block_end())
return nullptr;

return getBasicBlockAfter(BBPos, IgnoreSplits);
}

bool FunctionLayout::isSplit() const {
const unsigned NonEmptyFragCount = llvm::count_if(
fragments(), [](const FunctionFragment &FF) { return !FF.empty(); });
Expand Down
Loading