Skip to content

Commit

Permalink
Extracted sequence insertion function into helper function
Browse files Browse the repository at this point in the history
Summary: Factored out common code from multiple places into a helper function

(cherry picked from FBD22606101)
  • Loading branch information
aaupov authored and maksfb committed Jul 18, 2020
1 parent 937244b commit f7d4bed
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions bolt/src/Passes/Instrumentation.cpp
Expand Up @@ -173,6 +173,22 @@ Instrumentation::createInstrumentationSnippet(BinaryContext &BC, bool IsLeaf) {
return CounterInstrs;
}

namespace {

// Helper instruction sequence insertion function
BinaryBasicBlock::iterator
insertInstructions(std::vector<MCInst>& Instrs,
BinaryBasicBlock &BB,
BinaryBasicBlock::iterator Iter) {
for (auto &NewInst : Instrs) {
Iter = BB.insertInstruction(Iter, NewInst);
++Iter;
}
return Iter;
}

}

void Instrumentation::instrumentLeafNode(BinaryContext &BC,
BinaryBasicBlock &BB,
BinaryBasicBlock::iterator Iter,
Expand All @@ -181,11 +197,7 @@ void Instrumentation::instrumentLeafNode(BinaryContext &BC,
uint32_t Node) {
createLeafNodeDescription(FuncDesc, Node);
std::vector<MCInst> CounterInstrs = createInstrumentationSnippet(BC, IsLeaf);

for (auto &NewInst : CounterInstrs) {
Iter = BB.insertInstruction(Iter, NewInst);
++Iter;
}
insertInstructions(CounterInstrs, BB, Iter);
}

void Instrumentation::instrumentIndirectTarget(BinaryBasicBlock &BB,
Expand All @@ -205,10 +217,7 @@ void Instrumentation::instrumentIndirectTarget(BinaryBasicBlock &BB,
IndCallSiteID, &*BC.Ctx);

Iter = BB.eraseInstruction(Iter);
for (auto &NewInst : CounterInstrs) {
Iter = BB.insertInstruction(Iter, NewInst);
++Iter;
}
Iter = insertInstructions(CounterInstrs, BB, Iter);
--Iter;
}

Expand Down Expand Up @@ -238,10 +247,7 @@ bool Instrumentation::instrumentOneTarget(
BinaryContext &BC = FromFunction.getBinaryContext();
const MCInst &Inst = *Iter;
if (BC.MIB->isCall(Inst) && !TargetBB) {
for (auto &NewInst : CounterInstrs) {
Iter = FromBB.insertInstruction(Iter, NewInst);
++Iter;
}
Iter = insertInstructions(CounterInstrs, FromBB, Iter);
return true;
}

Expand All @@ -252,18 +258,11 @@ bool Instrumentation::instrumentOneTarget(
// Regular cond branch, put counter at start of target block
if (TargetBB->pred_size() == 1 && &FromBB != TargetBB &&
!TargetBB->isEntryPoint()) {
auto RemoteIter = TargetBB->begin();
for (auto &NewInst : CounterInstrs) {
RemoteIter = TargetBB->insertInstruction(RemoteIter, NewInst);
++RemoteIter;
}
insertInstructions(CounterInstrs, *TargetBB, TargetBB->begin());
return true;
}
if (FromBB.succ_size() == 1 && &FromBB != TargetBB) {
for (auto &NewInst : CounterInstrs) {
Iter = FromBB.insertInstruction(Iter, NewInst);
++Iter;
}
Iter = insertInstructions(CounterInstrs, FromBB, Iter);
return true;
}
// Critical edge, create BB and put counter there
Expand Down

0 comments on commit f7d4bed

Please sign in to comment.