#ifndef LLVM_ANALYSIS_INLINEADVISOR_H
#define LLVM_ANALYSIS_INLINEADVISOR_H
#include "llvm/Analysis/CGSCCPassManager.h"
#include "llvm/Analysis/InlineCost.h"
#include "llvm/Analysis/LazyCallGraph.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/PassManager.h"
#include <memory>
namespace llvm {
class BasicBlock;
class CallBase;
class Function;
class Module;
class OptimizationRemark;
class ImportedFunctionsInliningStatistics;
class OptimizationRemarkEmitter;
struct ReplayInlinerSettings;
enum class InliningAdvisorMode : int { Default, Release, Development };
enum class InlinePass : int {
AlwaysInliner,
CGSCCInliner,
EarlyInliner,
ModuleInliner,
MLInliner,
ReplayCGSCCInliner,
ReplaySampleProfileInliner,
SampleProfileInliner,
};
struct InlineContext {
ThinOrFullLTOPhase LTOPhase;
InlinePass Pass;
};
std::string AnnotateInlinePassName(InlineContext IC);
class InlineAdvisor;
class InlineAdvice {
public:
InlineAdvice(InlineAdvisor *Advisor, CallBase &CB,
OptimizationRemarkEmitter &ORE, bool IsInliningRecommended);
InlineAdvice(InlineAdvice &&) = delete;
InlineAdvice(const InlineAdvice &) = delete;
virtual ~InlineAdvice() {
assert(Recorded && "InlineAdvice should have been informed of the "
"inliner's decision in all cases");
}
void recordInlining();
void recordInliningWithCalleeDeleted();
void recordUnsuccessfulInlining(const InlineResult &Result) {
markRecorded();
recordUnsuccessfulInliningImpl(Result);
}
void recordUnattemptedInlining() {
markRecorded();
recordUnattemptedInliningImpl();
}
bool isInliningRecommended() const { return IsInliningRecommended; }
const DebugLoc &getOriginalCallSiteDebugLoc() const { return DLoc; }
const BasicBlock *getOriginalCallSiteBasicBlock() const { return Block; }
protected:
virtual void recordInliningImpl() {}
virtual void recordInliningWithCalleeDeletedImpl() {}
virtual void recordUnsuccessfulInliningImpl(const InlineResult &Result) {}
virtual void recordUnattemptedInliningImpl() {}
InlineAdvisor *const Advisor;
Function *const Caller;
Function *const Callee;
const DebugLoc DLoc;
const BasicBlock *const Block;
OptimizationRemarkEmitter &ORE;
const bool IsInliningRecommended;
private:
void markRecorded() {
assert(!Recorded && "Recording should happen exactly once");
Recorded = true;
}
void recordInlineStatsIfNeeded();
bool Recorded = false;
};
class DefaultInlineAdvice : public InlineAdvice {
public:
DefaultInlineAdvice(InlineAdvisor *Advisor, CallBase &CB,
Optional<InlineCost> OIC, OptimizationRemarkEmitter &ORE,
bool EmitRemarks = true)
: InlineAdvice(Advisor, CB, ORE, OIC.has_value()), OriginalCB(&CB),
OIC(OIC), EmitRemarks(EmitRemarks) {}
private:
void recordUnsuccessfulInliningImpl(const InlineResult &Result) override;
void recordInliningWithCalleeDeletedImpl() override;
void recordInliningImpl() override;
private:
CallBase *const OriginalCB;
Optional<InlineCost> OIC;
bool EmitRemarks;
};
class InlineAdvisor {
public:
InlineAdvisor(InlineAdvisor &&) = delete;
virtual ~InlineAdvisor();
std::unique_ptr<InlineAdvice> getAdvice(CallBase &CB,
bool MandatoryOnly = false);
virtual void onPassEntry(LazyCallGraph::SCC *SCC = nullptr) {}
virtual void onPassExit(LazyCallGraph::SCC *SCC = nullptr) {}
virtual void print(raw_ostream &OS) const {
OS << "Unimplemented InlineAdvisor print\n";
}
const char *getAnnotatedInlinePassName() const {
return AnnotatedInlinePassName.c_str();
}
protected:
InlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
Optional<InlineContext> IC = NoneType::None);
virtual std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) = 0;
virtual std::unique_ptr<InlineAdvice> getMandatoryAdvice(CallBase &CB,
bool Advice);
Module &M;
FunctionAnalysisManager &FAM;
const Optional<InlineContext> IC;
const std::string AnnotatedInlinePassName;
std::unique_ptr<ImportedFunctionsInliningStatistics> ImportedFunctionsStats;
enum class MandatoryInliningKind { NotMandatory, Always, Never };
static MandatoryInliningKind getMandatoryKind(CallBase &CB,
FunctionAnalysisManager &FAM,
OptimizationRemarkEmitter &ORE);
OptimizationRemarkEmitter &getCallerORE(CallBase &CB);
private:
friend class InlineAdvice;
};
class DefaultInlineAdvisor : public InlineAdvisor {
public:
DefaultInlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
InlineParams Params, InlineContext IC)
: InlineAdvisor(M, FAM, IC), Params(Params) {}
private:
std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;
InlineParams Params;
};
class InlineAdvisorAnalysis : public AnalysisInfoMixin<InlineAdvisorAnalysis> {
public:
static AnalysisKey Key;
InlineAdvisorAnalysis() = default;
struct Result {
Result(Module &M, ModuleAnalysisManager &MAM) : M(M), MAM(MAM) {}
bool invalidate(Module &, const PreservedAnalyses &PA,
ModuleAnalysisManager::Invalidator &) {
auto PAC = PA.getChecker<InlineAdvisorAnalysis>();
return !PAC.preservedWhenStateless();
}
bool tryCreate(InlineParams Params, InliningAdvisorMode Mode,
const ReplayInlinerSettings &ReplaySettings,
InlineContext IC);
InlineAdvisor *getAdvisor() const { return Advisor.get(); }
private:
Module &M;
ModuleAnalysisManager &MAM;
std::unique_ptr<InlineAdvisor> Advisor;
};
Result run(Module &M, ModuleAnalysisManager &MAM) { return Result(M, MAM); }
};
class InlineAdvisorAnalysisPrinterPass
: public PassInfoMixin<InlineAdvisorAnalysisPrinterPass> {
raw_ostream &OS;
public:
explicit InlineAdvisorAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
PreservedAnalyses run(LazyCallGraph::SCC &InitialC, CGSCCAnalysisManager &AM,
LazyCallGraph &CG, CGSCCUpdateResult &UR);
};
std::unique_ptr<InlineAdvisor>
getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM);
std::unique_ptr<InlineAdvisor>
getDevelopmentModeAdvisor(Module &M, ModuleAnalysisManager &MAM,
std::function<bool(CallBase &)> GetDefaultAdvice);
Optional<InlineCost>
shouldInline(CallBase &CB, function_ref<InlineCost(CallBase &CB)> GetInlineCost,
OptimizationRemarkEmitter &ORE, bool EnableDeferral = true);
void emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
const BasicBlock *Block, const Function &Callee,
const Function &Caller, bool IsMandatory,
function_ref<void(OptimizationRemark &)> ExtraContext = {},
const char *PassName = nullptr);
void emitInlinedIntoBasedOnCost(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
const BasicBlock *Block, const Function &Callee,
const Function &Caller, const InlineCost &IC,
bool ForProfileContext = false,
const char *PassName = nullptr);
void addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc);
void setInlineRemark(CallBase &CB, StringRef Message);
std::string inlineCostStr(const InlineCost &IC);
} #endif