mirror of
https://github.com/intel/llvm.git
synced 2026-01-16 05:32:28 +08:00
BOLT: Remove double jumps peephole.
Summary:
Replace jumps to other unconditional jumps with the final
destination, e.g.
B0: ...
jmp B1 (or jcc B1)
B1: jmp B2
->
B0: ...
jmp B2 (or jcc B1)
This peephole removes 8928 double jumps from a test binary.
Note: after filtering out double jumps found in EH code and infinite
loops, the number of double jumps patched is 49 (24 for a clang
compiled test). The 24 in the clang build are all from external
libraries which have probably been compiled with gcc. This peephole
is still useful for cleaning up after ICP though.
(cherry picked from FBD3815420)
This commit is contained in:
committed by
Maksim Panchenko
parent
617c6a13b7
commit
861d5a1586
@@ -39,6 +39,15 @@ MCInst *BinaryBasicBlock::findFirstNonPseudoInstruction() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
MCInst *BinaryBasicBlock::findLastNonPseudoInstruction() {
|
||||
auto &BC = Function->getBinaryContext();
|
||||
for (auto Itr = Instructions.rbegin(); Itr != Instructions.rend(); ++Itr) {
|
||||
if (!BC.MII->get(Itr->getOpcode()).isPseudo())
|
||||
return &*Itr;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
BinaryBasicBlock *BinaryBasicBlock::getSuccessor(const MCSymbol *Label) const {
|
||||
if (!Label && succ_size() == 1)
|
||||
return *succ_begin();
|
||||
@@ -68,6 +77,24 @@ void BinaryBasicBlock::addSuccessor(BinaryBasicBlock *Succ,
|
||||
Succ->Predecessors.push_back(this);
|
||||
}
|
||||
|
||||
void BinaryBasicBlock::replaceSuccessor(BinaryBasicBlock *Succ,
|
||||
BinaryBasicBlock *NewSucc,
|
||||
uint64_t Count,
|
||||
uint64_t MispredictedCount) {
|
||||
auto I = succ_begin();
|
||||
auto BI = BranchInfo.begin();
|
||||
for (; I != succ_end(); ++I) {
|
||||
assert(BI != BranchInfo.end() && "missing BranchInfo entry");
|
||||
if (*I == Succ)
|
||||
break;
|
||||
++BI;
|
||||
}
|
||||
assert(I != succ_end() && "no such successor!");
|
||||
|
||||
*I = NewSucc;
|
||||
*BI = BinaryBranchInfo{Count, MispredictedCount};
|
||||
}
|
||||
|
||||
void BinaryBasicBlock::removeSuccessor(BinaryBasicBlock *Succ) {
|
||||
Succ->removePredecessor(this);
|
||||
auto I = succ_begin();
|
||||
@@ -117,12 +144,20 @@ bool BinaryBasicBlock::swapConditionalSuccessors() {
|
||||
}
|
||||
|
||||
void BinaryBasicBlock::addBranchInstruction(const BinaryBasicBlock *Successor) {
|
||||
assert(isSuccessor(Successor));
|
||||
auto &BC = Function->getBinaryContext();
|
||||
MCInst NewInst;
|
||||
BC.MIA->createUncondBranch(NewInst, Successor->getLabel(), BC.Ctx.get());
|
||||
Instructions.emplace_back(std::move(NewInst));
|
||||
}
|
||||
|
||||
void BinaryBasicBlock::addTailCallInstruction(const MCSymbol *Target) {
|
||||
auto &BC = Function->getBinaryContext();
|
||||
MCInst NewInst;
|
||||
BC.MIA->createTailCall(NewInst, Target, BC.Ctx.get());
|
||||
Instructions.emplace_back(std::move(NewInst));
|
||||
}
|
||||
|
||||
uint32_t BinaryBasicBlock::getNumPseudos() const {
|
||||
#ifndef NDEBUG
|
||||
auto &BC = Function->getBinaryContext();
|
||||
|
||||
Reference in New Issue
Block a user