#include "llvm/CodeGen/MachineOutliner.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/CGData/CodeGenDataReader.h"
#include "llvm/CodeGen/LivePhysRegs.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/SuffixTree.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <functional>
#include <tuple>
#include <vector>
#define DEBUG_TYPE …
usingnamespacellvm;
usingnamespaceore;
usingnamespaceoutliner;
STATISTIC(NumOutlined, "Number of candidates outlined");
STATISTIC(FunctionsCreated, "Number of functions created");
STATISTIC(NumLegalInUnsignedVec, "Outlinable instructions mapped");
STATISTIC(NumIllegalInUnsignedVec,
"Unoutlinable instructions mapped + number of sentinel values");
STATISTIC(NumSentinels, "Sentinel values inserted during mapping");
STATISTIC(NumInvisible,
"Invisible instructions skipped during mapping");
STATISTIC(UnsignedVecSize,
"Total number of instructions mapped and saved to mapping vector");
STATISTIC(StableHashAttempts,
"Count of hashing attempts made for outlined functions");
STATISTIC(StableHashDropped,
"Count of unsuccessful hashing attempts for outlined functions");
static cl::opt<bool> EnableLinkOnceODROutlining(
"enable-linkonceodr-outlining", cl::Hidden,
cl::desc("Enable the machine outliner on linkonceodr functions"),
cl::init(false));
static cl::opt<unsigned> OutlinerReruns(
"machine-outliner-reruns", cl::init(0), cl::Hidden,
cl::desc(
"Number of times to rerun the outliner after the initial outline"));
static cl::opt<unsigned> OutlinerBenefitThreshold(
"outliner-benefit-threshold", cl::init(1), cl::Hidden,
cl::desc(
"The minimum size in bytes before an outlining candidate is accepted"));
static cl::opt<bool> OutlinerLeafDescendants(
"outliner-leaf-descendants", cl::init(true), cl::Hidden,
cl::desc("Consider all leaf descendants of internal nodes of the suffix "
"tree as candidates for outlining (if false, only leaf children "
"are considered)"));
static cl::opt<bool>
DisableGlobalOutlining("disable-global-outlining", cl::Hidden,
cl::desc("Disable global outlining only by ignoring "
"the codegen data generation or use"),
cl::init(false));
static cl::opt<bool> AppendContentHashToOutlinedName(
"append-content-hash-outlined-name", cl::Hidden,
cl::desc("This appends the content hash to the globally outlined function "
"name. It's beneficial for enhancing the precision of the stable "
"hash and for ordering the outlined functions."),
cl::init(true));
namespace {
struct InstructionMapper { … };
struct MachineOutliner : public ModulePass { … };
}
char MachineOutliner::ID = …;
namespace llvm {
ModulePass *createMachineOutlinerPass(bool RunOnAllFunctions) { … }
}
INITIALIZE_PASS(…)
void MachineOutliner::emitNotOutliningCheaperRemark(
unsigned StringLen, std::vector<Candidate> &CandidatesForRepeatedSeq,
OutlinedFunction &OF) { … }
void MachineOutliner::emitOutlinedFunctionRemark(OutlinedFunction &OF) { … }
struct MatchedEntry { … };
static SmallVector<MatchedEntry> getMatchedEntries(InstructionMapper &Mapper) { … }
void MachineOutliner::findGlobalCandidates(
InstructionMapper &Mapper,
std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList) { … }
void MachineOutliner::findCandidates(
InstructionMapper &Mapper,
std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList) { … }
void MachineOutliner::computeAndPublishHashSequence(MachineFunction &MF,
unsigned CandSize) { … }
MachineFunction *MachineOutliner::createOutlinedFunction(
Module &M, OutlinedFunction &OF, InstructionMapper &Mapper, unsigned Name) { … }
bool MachineOutliner::outline(
Module &M, std::vector<std::unique_ptr<OutlinedFunction>> &FunctionList,
InstructionMapper &Mapper, unsigned &OutlinedFunctionNum) { … }
void MachineOutliner::populateMapper(InstructionMapper &Mapper, Module &M) { … }
void MachineOutliner::initSizeRemarkInfo(
const Module &M, StringMap<unsigned> &FunctionToInstrCount) { … }
void MachineOutliner::emitInstrCountChangedRemark(
const Module &M, const StringMap<unsigned> &FunctionToInstrCount) { … }
void MachineOutliner::initializeOutlinerMode(const Module &M) { … }
void MachineOutliner::emitOutlinedHashTree(Module &M) { … }
bool MachineOutliner::runOnModule(Module &M) { … }
bool MachineOutliner::doOutline(Module &M, unsigned &OutlinedFunctionNum) { … }