2015-10-09 17:21:14 -07:00
|
|
|
//===--- BinaryBasicBlock.cpp - Interface for assembly-level basic block --===//
|
|
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2015-10-14 15:35:14 -07:00
|
|
|
#include "BinaryBasicBlock.h"
|
2016-07-23 08:01:53 -07:00
|
|
|
#include "BinaryContext.h"
|
2016-08-29 21:11:22 -07:00
|
|
|
#include "BinaryFunction.h"
|
2015-10-09 17:21:14 -07:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
|
|
|
|
#include <limits>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
#undef DEBUG_TYPE
|
2016-02-05 14:42:04 -08:00
|
|
|
#define DEBUG_TYPE "bolt"
|
2015-10-09 17:21:14 -07:00
|
|
|
|
|
|
|
|
namespace llvm {
|
2016-02-05 14:42:04 -08:00
|
|
|
namespace bolt {
|
2015-10-09 17:21:14 -07:00
|
|
|
|
|
|
|
|
bool operator<(const BinaryBasicBlock &LHS, const BinaryBasicBlock &RHS) {
|
|
|
|
|
return LHS.Offset < RHS.Offset;
|
|
|
|
|
}
|
2016-06-09 11:36:55 -07:00
|
|
|
|
|
|
|
|
BinaryBasicBlock *BinaryBasicBlock::getSuccessor(const MCSymbol *Label) const {
|
2016-08-29 21:11:22 -07:00
|
|
|
if (!Label && succ_size() == 1)
|
|
|
|
|
return *succ_begin();
|
|
|
|
|
|
2016-06-09 11:36:55 -07:00
|
|
|
for (BinaryBasicBlock *BB : successors()) {
|
|
|
|
|
if (BB->getLabel() == Label)
|
|
|
|
|
return BB;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BinaryBasicBlock *BinaryBasicBlock::getLandingPad(const MCSymbol *Label) const {
|
|
|
|
|
for (BinaryBasicBlock *BB : landing_pads()) {
|
|
|
|
|
if (BB->getLabel() == Label)
|
|
|
|
|
return BB;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2015-10-09 17:21:14 -07:00
|
|
|
|
|
|
|
|
void BinaryBasicBlock::addSuccessor(BinaryBasicBlock *Succ,
|
|
|
|
|
uint64_t Count,
|
|
|
|
|
uint64_t MispredictedCount) {
|
|
|
|
|
Successors.push_back(Succ);
|
2015-10-12 12:30:47 -07:00
|
|
|
BranchInfo.push_back({Count, MispredictedCount});
|
2015-10-09 17:21:14 -07:00
|
|
|
Succ->Predecessors.push_back(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinaryBasicBlock::removeSuccessor(BinaryBasicBlock *Succ) {
|
|
|
|
|
Succ->removePredecessor(this);
|
2015-10-12 12:30:47 -07:00
|
|
|
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;
|
|
|
|
|
}
|
2015-10-09 17:21:14 -07:00
|
|
|
assert(I != succ_end() && "no such successor!");
|
|
|
|
|
|
|
|
|
|
Successors.erase(I);
|
2015-10-12 12:30:47 -07:00
|
|
|
BranchInfo.erase(BI);
|
2015-10-09 17:21:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinaryBasicBlock::addPredecessor(BinaryBasicBlock *Pred) {
|
|
|
|
|
Predecessors.push_back(Pred);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinaryBasicBlock::removePredecessor(BinaryBasicBlock *Pred) {
|
|
|
|
|
auto I = std::find(pred_begin(), pred_end(), Pred);
|
|
|
|
|
assert(I != pred_end() && "Pred is not a predecessor of this block!");
|
|
|
|
|
Predecessors.erase(I);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-26 15:10:09 -07:00
|
|
|
void BinaryBasicBlock::addLandingPad(BinaryBasicBlock *LPBlock) {
|
|
|
|
|
LandingPads.insert(LPBlock);
|
|
|
|
|
LPBlock->Throwers.insert(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-02 12:47:18 -07:00
|
|
|
bool BinaryBasicBlock::analyzeBranch(const MCInstrAnalysis &MIA,
|
|
|
|
|
const MCSymbol *&TBB,
|
|
|
|
|
const MCSymbol *&FBB,
|
|
|
|
|
MCInst *&CondBranch,
|
|
|
|
|
MCInst *&UncondBranch) {
|
|
|
|
|
return MIA.analyzeBranch(Instructions, TBB, FBB, CondBranch, UncondBranch);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 21:11:22 -07:00
|
|
|
bool BinaryBasicBlock::swapConditionalSuccessors() {
|
|
|
|
|
if (succ_size() != 2)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
std::swap(Successors[0], Successors[1]);
|
|
|
|
|
std::swap(BranchInfo[0], BranchInfo[1]);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BinaryBasicBlock::addBranchInstruction(const BinaryBasicBlock *Successor) {
|
|
|
|
|
auto &BC = Function->getBinaryContext();
|
|
|
|
|
MCInst NewInst;
|
|
|
|
|
BC.MIA->createUncondBranch(NewInst, Successor->getLabel(), BC.Ctx.get());
|
|
|
|
|
Instructions.emplace_back(std::move(NewInst));
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-23 08:01:53 -07:00
|
|
|
void BinaryBasicBlock::dump(BinaryContext& BC) const {
|
2016-09-02 14:15:29 -07:00
|
|
|
if (Label) outs() << Label->getName() << ":\n";
|
|
|
|
|
BC.printInstructions(outs(), Instructions.begin(), Instructions.end(), Offset);
|
|
|
|
|
outs() << "preds:";
|
2016-07-23 08:01:53 -07:00
|
|
|
for (auto itr = pred_begin(); itr != pred_end(); ++itr) {
|
2016-09-02 14:15:29 -07:00
|
|
|
outs() << " " << (*itr)->getName();
|
2016-07-23 08:01:53 -07:00
|
|
|
}
|
2016-09-02 14:15:29 -07:00
|
|
|
outs() << "\nsuccs:";
|
2016-07-23 08:01:53 -07:00
|
|
|
for (auto itr = succ_begin(); itr != succ_end(); ++itr) {
|
2016-09-02 14:15:29 -07:00
|
|
|
outs() << " " << (*itr)->getName();
|
2016-07-23 08:01:53 -07:00
|
|
|
}
|
2016-09-02 14:15:29 -07:00
|
|
|
outs() << "\n";
|
2016-07-23 08:01:53 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 14:42:04 -08:00
|
|
|
} // namespace bolt
|
2015-10-09 17:21:14 -07:00
|
|
|
} // namespace llvm
|