Skip to content

Commit

Permalink
[BOLT] Introduce MCPlus layer
Browse files Browse the repository at this point in the history
Summary:
Refactor architecture-specific code out of llvm into llvm-bolt.

Introduce MCPlusBuilder, a class that is taking over MCInstrAnalysis
responsibilities, i.e. creating, analyzing, and modifying instructions.
To access the builder use BC->MIB, i.e. substitute MIA with MIB.
MIB is an acronym for MCInstBuilder, that's what MCPlusBuilder used
to be. The name stuck, and I find it better than MPB.

Instructions are still MCInst, and a bunch of BOLT-specific code still
lives in LLVM, but the staff under Target/* is significantly reduced.

(cherry picked from FBD7300101)
  • Loading branch information
maksfb committed Mar 9, 2018
1 parent 8c16594 commit 48ae32a
Show file tree
Hide file tree
Showing 43 changed files with 6,254 additions and 574 deletions.
22 changes: 11 additions & 11 deletions bolt/BinaryBasicBlock.cpp
Expand Up @@ -66,7 +66,7 @@ bool BinaryBasicBlock::validateSuccessorInvariants() {
// Note: for now we assume that successors do not reference labels from
// any overlapping jump tables. We only look at the entries for the jump
// table that is referenced at the last instruction.
const auto Range = JT->getEntriesForAddress(BC.MIA->getJumpTable(*Inst));
const auto Range = JT->getEntriesForAddress(BC.MIB->getJumpTable(*Inst));
const std::vector<const MCSymbol *> Entries(&JT->Entries[Range.first],
&JT->Entries[Range.second]);
std::set<const MCSymbol *> UniqueSyms(Entries.begin(), Entries.end());
Expand Down Expand Up @@ -108,7 +108,7 @@ bool BinaryBasicBlock::validateSuccessorInvariants() {
break;
case 1: {
const bool HasCondBlock = CondBranch &&
Function->getBasicBlockForLabel(BC.MIA->getTargetSymbol(*CondBranch));
Function->getBasicBlockForLabel(BC.MIB->getTargetSymbol(*CondBranch));
Valid = !CondBranch || !HasCondBlock;
break;
}
Expand All @@ -128,7 +128,7 @@ bool BinaryBasicBlock::validateSuccessorInvariants() {
<< getName() << "\n";
if (JT) {
errs() << "Jump Table instruction addr = 0x"
<< Twine::utohexstr(BC.MIA->getJumpTable(*Inst)) << "\n";
<< Twine::utohexstr(BC.MIB->getJumpTable(*Inst)) << "\n";
JT->print(errs());
}
getFunction()->dump();
Expand Down Expand Up @@ -188,7 +188,7 @@ int32_t BinaryBasicBlock::getCFIStateAtInstr(const MCInst *Instr) const {
InstrSeen = (&*RII == Instr);
continue;
}
if (Function->getBinaryContext().MIA->isCFI(*RII)) {
if (Function->getBinaryContext().MIB->isCFI(*RII)) {
LastCFI = &*RII;
break;
}
Expand Down Expand Up @@ -322,8 +322,8 @@ bool BinaryBasicBlock::analyzeBranch(const MCSymbol *&TBB,
const MCSymbol *&FBB,
MCInst *&CondBranch,
MCInst *&UncondBranch) {
auto &MIA = Function->getBinaryContext().MIA;
return MIA->analyzeBranch(Instructions.begin(),
auto &MIB = Function->getBinaryContext().MIB;
return MIB->analyzeBranch(Instructions.begin(),
Instructions.end(),
TBB,
FBB,
Expand All @@ -343,7 +343,7 @@ MCInst *BinaryBasicBlock::getTerminatorBefore(MCInst *Pos) {
++Itr;
continue;
}
if (BC.MIA->isTerminator(*Itr))
if (BC.MIB->isTerminator(*Itr))
FirstTerminator = &*Itr;
++Itr;
}
Expand All @@ -356,7 +356,7 @@ bool BinaryBasicBlock::hasTerminatorAfter(MCInst *Pos) {
while (Itr != rend()) {
if (&*Itr == Pos)
return false;
if (BC.MIA->isTerminator(*Itr))
if (BC.MIB->isTerminator(*Itr))
return true;
++Itr;
}
Expand All @@ -376,22 +376,22 @@ void BinaryBasicBlock::addBranchInstruction(const BinaryBasicBlock *Successor) {
assert(isSuccessor(Successor));
auto &BC = Function->getBinaryContext();
MCInst NewInst;
BC.MIA->createUncondBranch(NewInst, Successor->getLabel(), BC.Ctx.get());
BC.MIB->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());
BC.MIB->createTailCall(NewInst, Target, BC.Ctx.get());
Instructions.emplace_back(std::move(NewInst));
}

uint32_t BinaryBasicBlock::getNumCalls() const {
uint32_t N{0};
auto &BC = Function->getBinaryContext();
for (auto &Instr : Instructions) {
if (BC.MIA->isCall(Instr))
if (BC.MIB->isCall(Instr))
++N;
}
return N;
Expand Down
24 changes: 12 additions & 12 deletions bolt/BinaryContext.cpp
Expand Up @@ -658,12 +658,12 @@ void BinaryContext::printInstruction(raw_ostream &OS,
bool PrintMCInst,
bool PrintMemData,
bool PrintRelocations) const {
if (MIA->isEHLabel(Instruction)) {
OS << " EH_LABEL: " << *MIA->getTargetSymbol(Instruction) << '\n';
if (MIB->isEHLabel(Instruction)) {
OS << " EH_LABEL: " << *MIB->getTargetSymbol(Instruction) << '\n';
return;
}
OS << format(" %08" PRIx64 ": ", Offset);
if (MIA->isCFI(Instruction)) {
if (MIB->isCFI(Instruction)) {
uint32_t Offset = Instruction.getOperand(0).getImm();
OS << "\t!CFI\t$" << Offset << "\t; ";
if (Function)
Expand All @@ -672,31 +672,31 @@ void BinaryContext::printInstruction(raw_ostream &OS,
return;
}
InstPrinter->printInst(&Instruction, OS, "", *STI);
if (MIA->isCall(Instruction)) {
if (MIA->isTailCall(Instruction))
if (MIB->isCall(Instruction)) {
if (MIB->isTailCall(Instruction))
OS << " # TAILCALL ";
if (MIA->isInvoke(Instruction)) {
if (MIB->isInvoke(Instruction)) {
const MCSymbol *LP;
uint64_t Action;
std::tie(LP, Action) = MIA->getEHInfo(Instruction);
std::tie(LP, Action) = MIB->getEHInfo(Instruction);
OS << " # handler: ";
if (LP)
OS << *LP;
else
OS << '0';
OS << "; action: " << Action;
auto GnuArgsSize = MIA->getGnuArgsSize(Instruction);
auto GnuArgsSize = MIB->getGnuArgsSize(Instruction);
if (GnuArgsSize >= 0)
OS << "; GNU_args_size = " << GnuArgsSize;
}
}
if (MIA->isIndirectBranch(Instruction)) {
if (auto JTAddress = MIA->getJumpTable(Instruction)) {
if (MIB->isIndirectBranch(Instruction)) {
if (auto JTAddress = MIB->getJumpTable(Instruction)) {
OS << " # JUMPTABLE @0x" << Twine::utohexstr(JTAddress);
}
}

MIA->forEachAnnotation(
MIB->forEachAnnotation(
Instruction,
[&OS](const MCAnnotation *Annotation) {
OS << " # " << Annotation->getName() << ": ";
Expand Down Expand Up @@ -726,7 +726,7 @@ void BinaryContext::printInstruction(raw_ostream &OS,
if ((opts::PrintMemData || PrintMemData) && Function) {
const auto *MD = Function->getMemData();
const auto MemDataOffset =
MIA->tryGetAnnotationAs<uint64_t>(Instruction, "MemDataOffset");
MIB->tryGetAnnotationAs<uint64_t>(Instruction, "MemDataOffset");
if (MD && MemDataOffset) {
bool DidPrint = false;
for (auto &MI : MD->getMemInfoRange(MemDataOffset.get())) {
Expand Down
11 changes: 8 additions & 3 deletions bolt/BinaryContext.h
Expand Up @@ -17,6 +17,7 @@
#include "BinaryData.h"
#include "BinarySection.h"
#include "DebugData.h"
#include "MCPlusBuilder.h"
#include "llvm/ADT/iterator.h"
#include "llvm/ADT/Triple.h"
#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
Expand Down Expand Up @@ -222,6 +223,8 @@ class BinaryContext {

std::unique_ptr<const MCInstrAnalysis> MIA;

std::unique_ptr<const MCPlusBuilder> MIB;

std::unique_ptr<const MCRegisterInfo> MRI;

std::unique_ptr<MCDisassembler> DisAsm;
Expand Down Expand Up @@ -270,6 +273,7 @@ class BinaryContext {
std::unique_ptr<const MCSubtargetInfo> STI,
std::unique_ptr<MCInstPrinter> InstPrinter,
std::unique_ptr<const MCInstrAnalysis> MIA,
std::unique_ptr<const MCPlusBuilder> MIB,
std::unique_ptr<const MCRegisterInfo> MRI,
std::unique_ptr<MCDisassembler> DisAsm,
DataReader &DR) :
Expand All @@ -285,6 +289,7 @@ class BinaryContext {
STI(std::move(STI)),
InstPrinter(std::move(InstPrinter)),
MIA(std::move(MIA)),
MIB(std::move(MIB)),
MRI(std::move(MRI)),
DisAsm(std::move(DisAsm)),
DR(DR) {
Expand Down Expand Up @@ -612,7 +617,7 @@ class BinaryContext {
SmallString<256> Code;
SmallVector<MCFixup, 4> Fixups;
raw_svector_ostream VecOS(Code);
if (MIA->isCFI(*Beg) || MIA->isEHLabel(*Beg)) {
if (MIB->isCFI(*Beg) || MIB->isEHLabel(*Beg)) {
++Beg;
continue;
}
Expand All @@ -637,8 +642,8 @@ class BinaryContext {
/// Return true if instruction \p Inst requires an offset for further
/// processing (e.g. assigning a profile).
bool keepOffsetForInstruction(const MCInst &Inst) const {
if (MIA->isCall(Inst) || MIA->isBranch(Inst) || MIA->isReturn(Inst) ||
MIA->isPrefix(Inst) || MIA->isIndirectBranch(Inst)) {
if (MIB->isCall(Inst) || MIB->isBranch(Inst) || MIB->isReturn(Inst) ||
MIB->isPrefix(Inst) || MIB->isIndirectBranch(Inst)) {
return true;
}
return false;
Expand Down

0 comments on commit 48ae32a

Please sign in to comment.