#include "bolt/Passes/TailDuplication.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/MC/MCRegisterInfo.h"
#include <numeric>
#include <queue>
#define DEBUG_TYPE …
usingnamespacellvm;
namespace opts {
extern cl::OptionCategory BoltOptCategory;
extern cl::opt<bool> NoThreads;
cl::opt<bolt::TailDuplication::DuplicationMode> TailDuplicationMode(
"tail-duplication",
cl::desc("duplicate unconditional branches that cross a cache line"),
cl::init(bolt::TailDuplication::TD_NONE),
cl::values(clEnumValN(bolt::TailDuplication::TD_NONE, "none",
"do not apply"),
clEnumValN(bolt::TailDuplication::TD_AGGRESSIVE, "aggressive",
"aggressive strategy"),
clEnumValN(bolt::TailDuplication::TD_MODERATE, "moderate",
"moderate strategy"),
clEnumValN(bolt::TailDuplication::TD_CACHE, "cache",
"cache-aware duplication strategy")),
cl::ZeroOrMore, cl::Hidden, cl::cat(BoltOptCategory));
static cl::opt<unsigned>
TailDuplicationMinimumOffset("tail-duplication-minimum-offset",
cl::desc("minimum offset needed between block "
"and successor to allow duplication"),
cl::ReallyHidden, cl::init(64),
cl::cat(BoltOptCategory));
static cl::opt<unsigned> TailDuplicationMaximumDuplication(
"tail-duplication-maximum-duplication",
cl::desc("tail blocks whose size (in bytes) exceeds the value are never "
"duplicated"),
cl::ZeroOrMore, cl::ReallyHidden, cl::init(24), cl::cat(BoltOptCategory));
static cl::opt<unsigned> TailDuplicationMinimumDuplication(
"tail-duplication-minimum-duplication",
cl::desc("tail blocks with size (in bytes) not exceeding the value are "
"always duplicated"),
cl::ReallyHidden, cl::init(2), cl::cat(BoltOptCategory));
static cl::opt<bool> TailDuplicationConstCopyPropagation(
"tail-duplication-const-copy-propagation",
cl::desc("enable const and copy propagation after tail duplication"),
cl::ReallyHidden, cl::init(false), cl::cat(BoltOptCategory));
static cl::opt<unsigned> TailDuplicationMaxCacheDistance(
"tail-duplication-max-cache-distance",
cl::desc("The weight of backward jumps for ExtTSP value"), cl::init(256),
cl::ReallyHidden, cl::cat(BoltOptCategory));
static cl::opt<double> TailDuplicationCacheBackwardWeight(
"tail-duplication-cache-backward-weight",
cl::desc(
"The maximum distance (in bytes) of backward jumps for ExtTSP value"),
cl::init(0.5), cl::ReallyHidden, cl::cat(BoltOptCategory));
}
namespace llvm {
namespace bolt {
void TailDuplication::getCallerSavedRegs(const MCInst &Inst, BitVector &Regs,
BinaryContext &BC) const { … }
bool TailDuplication::regIsPossiblyOverwritten(const MCInst &Inst, unsigned Reg,
BinaryContext &BC) const { … }
bool TailDuplication::regIsDefinitelyOverwritten(const MCInst &Inst,
unsigned Reg,
BinaryContext &BC) const { … }
bool TailDuplication::regIsUsed(const MCInst &Inst, unsigned Reg,
BinaryContext &BC) const { … }
bool TailDuplication::isOverwrittenBeforeUsed(BinaryBasicBlock &StartBB,
unsigned Reg) const { … }
void TailDuplication::constantAndCopyPropagate(
BinaryBasicBlock &OriginalBB,
std::vector<BinaryBasicBlock *> &BlocksToPropagate) { … }
bool TailDuplication::isInCacheLine(const BinaryBasicBlock &BB,
const BinaryBasicBlock &Succ) const { … }
std::vector<BinaryBasicBlock *>
TailDuplication::moderateDuplicate(BinaryBasicBlock &BB,
BinaryBasicBlock &Tail) const { … }
std::vector<BinaryBasicBlock *>
TailDuplication::aggressiveDuplicate(BinaryBasicBlock &BB,
BinaryBasicBlock &Tail) const { … }
bool TailDuplication::shouldDuplicate(BinaryBasicBlock *Pred,
BinaryBasicBlock *Tail) const { … }
double TailDuplication::cacheScore(uint64_t SrcAddr, uint64_t SrcSize,
uint64_t DstAddr, uint64_t DstSize,
uint64_t Count) const { … }
bool TailDuplication::cacheScoreImproved(const MCCodeEmitter *Emitter,
BinaryFunction &BF,
BinaryBasicBlock *Pred,
BinaryBasicBlock *Tail) const { … }
std::vector<BinaryBasicBlock *>
TailDuplication::cacheDuplicate(const MCCodeEmitter *Emitter,
BinaryFunction &BF, BinaryBasicBlock *Pred,
BinaryBasicBlock *Tail) const { … }
std::vector<BinaryBasicBlock *> TailDuplication::duplicateBlocks(
BinaryBasicBlock &BB,
const std::vector<BinaryBasicBlock *> &BlocksToDuplicate) const { … }
void TailDuplication::runOnFunction(BinaryFunction &Function) { … }
Error TailDuplication::runOnFunctions(BinaryContext &BC) { … }
}
}