Extracted sequence insertion function into helper function

Summary: Factored out common code from multiple places into a helper function

(cherry picked from FBD22606101)
This commit is contained in:
Amir Ayupov 2020-07-17 23:16:52 -07:00 committed by Maksim Panchenko
parent 937244b4f2
commit f7d4bed9d1
1 changed files with 21 additions and 22 deletions

View File

@ -173,6 +173,22 @@ Instrumentation::createInstrumentationSnippet(BinaryContext &BC, bool IsLeaf) {
return CounterInstrs; 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, void Instrumentation::instrumentLeafNode(BinaryContext &BC,
BinaryBasicBlock &BB, BinaryBasicBlock &BB,
BinaryBasicBlock::iterator Iter, BinaryBasicBlock::iterator Iter,
@ -181,11 +197,7 @@ void Instrumentation::instrumentLeafNode(BinaryContext &BC,
uint32_t Node) { uint32_t Node) {
createLeafNodeDescription(FuncDesc, Node); createLeafNodeDescription(FuncDesc, Node);
std::vector<MCInst> CounterInstrs = createInstrumentationSnippet(BC, IsLeaf); std::vector<MCInst> CounterInstrs = createInstrumentationSnippet(BC, IsLeaf);
insertInstructions(CounterInstrs, BB, Iter);
for (auto &NewInst : CounterInstrs) {
Iter = BB.insertInstruction(Iter, NewInst);
++Iter;
}
} }
void Instrumentation::instrumentIndirectTarget(BinaryBasicBlock &BB, void Instrumentation::instrumentIndirectTarget(BinaryBasicBlock &BB,
@ -205,10 +217,7 @@ void Instrumentation::instrumentIndirectTarget(BinaryBasicBlock &BB,
IndCallSiteID, &*BC.Ctx); IndCallSiteID, &*BC.Ctx);
Iter = BB.eraseInstruction(Iter); Iter = BB.eraseInstruction(Iter);
for (auto &NewInst : CounterInstrs) { Iter = insertInstructions(CounterInstrs, BB, Iter);
Iter = BB.insertInstruction(Iter, NewInst);
++Iter;
}
--Iter; --Iter;
} }
@ -238,10 +247,7 @@ bool Instrumentation::instrumentOneTarget(
BinaryContext &BC = FromFunction.getBinaryContext(); BinaryContext &BC = FromFunction.getBinaryContext();
const MCInst &Inst = *Iter; const MCInst &Inst = *Iter;
if (BC.MIB->isCall(Inst) && !TargetBB) { if (BC.MIB->isCall(Inst) && !TargetBB) {
for (auto &NewInst : CounterInstrs) { Iter = insertInstructions(CounterInstrs, FromBB, Iter);
Iter = FromBB.insertInstruction(Iter, NewInst);
++Iter;
}
return true; return true;
} }
@ -252,18 +258,11 @@ bool Instrumentation::instrumentOneTarget(
// Regular cond branch, put counter at start of target block // Regular cond branch, put counter at start of target block
if (TargetBB->pred_size() == 1 && &FromBB != TargetBB && if (TargetBB->pred_size() == 1 && &FromBB != TargetBB &&
!TargetBB->isEntryPoint()) { !TargetBB->isEntryPoint()) {
auto RemoteIter = TargetBB->begin(); insertInstructions(CounterInstrs, *TargetBB, TargetBB->begin());
for (auto &NewInst : CounterInstrs) {
RemoteIter = TargetBB->insertInstruction(RemoteIter, NewInst);
++RemoteIter;
}
return true; return true;
} }
if (FromBB.succ_size() == 1 && &FromBB != TargetBB) { if (FromBB.succ_size() == 1 && &FromBB != TargetBB) {
for (auto &NewInst : CounterInstrs) { Iter = insertInstructions(CounterInstrs, FromBB, Iter);
Iter = FromBB.insertInstruction(Iter, NewInst);
++Iter;
}
return true; return true;
} }
// Critical edge, create BB and put counter there // Critical edge, create BB and put counter there