File indexing completed on 2026-05-10 08:43:13
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_ANALYSIS_INLINEADVISOR_H
0010 #define LLVM_ANALYSIS_INLINEADVISOR_H
0011
0012 #include "llvm/Analysis/CGSCCPassManager.h"
0013 #include "llvm/Analysis/InlineCost.h"
0014 #include "llvm/Analysis/LazyCallGraph.h"
0015 #include "llvm/IR/PassManager.h"
0016 #include <memory>
0017
0018 namespace llvm {
0019 class BasicBlock;
0020 class CallBase;
0021 class Function;
0022 class Module;
0023 class OptimizationRemark;
0024 class ImportedFunctionsInliningStatistics;
0025 class OptimizationRemarkEmitter;
0026 struct ReplayInlinerSettings;
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 enum class InliningAdvisorMode : int { Default, Release, Development };
0043
0044
0045 enum class InlinePass : int {
0046 AlwaysInliner,
0047 CGSCCInliner,
0048 EarlyInliner,
0049 ModuleInliner,
0050 MLInliner,
0051 ReplayCGSCCInliner,
0052 ReplaySampleProfileInliner,
0053 SampleProfileInliner,
0054 };
0055
0056
0057
0058 struct InlineContext {
0059 ThinOrFullLTOPhase LTOPhase;
0060
0061 InlinePass Pass;
0062 };
0063
0064 std::string AnnotateInlinePassName(InlineContext IC);
0065
0066 class InlineAdvisor;
0067
0068
0069
0070
0071
0072
0073
0074 class InlineAdvice {
0075 public:
0076 InlineAdvice(InlineAdvisor *Advisor, CallBase &CB,
0077 OptimizationRemarkEmitter &ORE, bool IsInliningRecommended);
0078
0079 InlineAdvice(InlineAdvice &&) = delete;
0080 InlineAdvice(const InlineAdvice &) = delete;
0081 virtual ~InlineAdvice() {
0082 assert(Recorded && "InlineAdvice should have been informed of the "
0083 "inliner's decision in all cases");
0084 }
0085
0086
0087
0088
0089
0090 void recordInlining();
0091
0092
0093
0094
0095 void recordInliningWithCalleeDeleted();
0096
0097
0098 void recordUnsuccessfulInlining(const InlineResult &Result) {
0099 markRecorded();
0100 recordUnsuccessfulInliningImpl(Result);
0101 }
0102
0103
0104 void recordUnattemptedInlining() {
0105 markRecorded();
0106 recordUnattemptedInliningImpl();
0107 }
0108
0109
0110 bool isInliningRecommended() const { return IsInliningRecommended; }
0111 const DebugLoc &getOriginalCallSiteDebugLoc() const { return DLoc; }
0112 const BasicBlock *getOriginalCallSiteBasicBlock() const { return Block; }
0113
0114 protected:
0115 virtual void recordInliningImpl() {}
0116 virtual void recordInliningWithCalleeDeletedImpl() {}
0117 virtual void recordUnsuccessfulInliningImpl(const InlineResult &Result) {}
0118 virtual void recordUnattemptedInliningImpl() {}
0119
0120 InlineAdvisor *const Advisor;
0121
0122 Function *const Caller;
0123 Function *const Callee;
0124
0125
0126
0127
0128 const DebugLoc DLoc;
0129 const BasicBlock *const Block;
0130 OptimizationRemarkEmitter &ORE;
0131 const bool IsInliningRecommended;
0132
0133 private:
0134 void markRecorded() {
0135 assert(!Recorded && "Recording should happen exactly once");
0136 Recorded = true;
0137 }
0138 void recordInlineStatsIfNeeded();
0139
0140 bool Recorded = false;
0141 };
0142
0143 class DefaultInlineAdvice : public InlineAdvice {
0144 public:
0145 DefaultInlineAdvice(InlineAdvisor *Advisor, CallBase &CB,
0146 std::optional<InlineCost> OIC,
0147 OptimizationRemarkEmitter &ORE, bool EmitRemarks = true)
0148 : InlineAdvice(Advisor, CB, ORE, OIC.has_value()), OriginalCB(&CB),
0149 OIC(OIC), EmitRemarks(EmitRemarks) {}
0150
0151 private:
0152 void recordUnsuccessfulInliningImpl(const InlineResult &Result) override;
0153 void recordInliningWithCalleeDeletedImpl() override;
0154 void recordInliningImpl() override;
0155
0156 private:
0157 CallBase *const OriginalCB;
0158 std::optional<InlineCost> OIC;
0159 bool EmitRemarks;
0160 };
0161
0162
0163 class InlineAdvisor {
0164 public:
0165 InlineAdvisor(InlineAdvisor &&) = delete;
0166 virtual ~InlineAdvisor();
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 std::unique_ptr<InlineAdvice> getAdvice(CallBase &CB,
0179 bool MandatoryOnly = false);
0180
0181
0182
0183
0184 virtual void onPassEntry(LazyCallGraph::SCC *SCC = nullptr) {}
0185
0186
0187
0188
0189 virtual void onPassExit(LazyCallGraph::SCC *SCC = nullptr) {}
0190
0191
0192 virtual void print(raw_ostream &OS) const {
0193 OS << "Unimplemented InlineAdvisor print\n";
0194 }
0195
0196
0197 const char *getAnnotatedInlinePassName() const {
0198 return AnnotatedInlinePassName.c_str();
0199 }
0200
0201 protected:
0202 InlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
0203 std::optional<InlineContext> IC = std::nullopt);
0204 virtual std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) = 0;
0205 virtual std::unique_ptr<InlineAdvice> getMandatoryAdvice(CallBase &CB,
0206 bool Advice);
0207
0208 Module &M;
0209 FunctionAnalysisManager &FAM;
0210 const std::optional<InlineContext> IC;
0211 const std::string AnnotatedInlinePassName;
0212 std::unique_ptr<ImportedFunctionsInliningStatistics> ImportedFunctionsStats;
0213
0214 enum class MandatoryInliningKind { NotMandatory, Always, Never };
0215
0216 static MandatoryInliningKind getMandatoryKind(CallBase &CB,
0217 FunctionAnalysisManager &FAM,
0218 OptimizationRemarkEmitter &ORE);
0219
0220 OptimizationRemarkEmitter &getCallerORE(CallBase &CB);
0221
0222 private:
0223 friend class InlineAdvice;
0224 };
0225
0226
0227
0228
0229 class DefaultInlineAdvisor : public InlineAdvisor {
0230 public:
0231 DefaultInlineAdvisor(Module &M, FunctionAnalysisManager &FAM,
0232 InlineParams Params, InlineContext IC)
0233 : InlineAdvisor(M, FAM, IC), Params(Params) {}
0234
0235 private:
0236 std::unique_ptr<InlineAdvice> getAdviceImpl(CallBase &CB) override;
0237
0238 InlineParams Params;
0239 };
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279 class PluginInlineAdvisorAnalysis
0280 : public AnalysisInfoMixin<PluginInlineAdvisorAnalysis> {
0281 public:
0282 static AnalysisKey Key;
0283
0284 typedef InlineAdvisor *(*AdvisorFactory)(Module &M,
0285 FunctionAnalysisManager &FAM,
0286 InlineParams Params,
0287 InlineContext IC);
0288
0289 PluginInlineAdvisorAnalysis(AdvisorFactory Factory) : Factory(Factory) {
0290 assert(Factory != nullptr &&
0291 "The plugin advisor factory should not be a null pointer.");
0292 }
0293
0294 struct Result {
0295 AdvisorFactory Factory;
0296 };
0297
0298 Result run(Module &M, ModuleAnalysisManager &MAM) { return {Factory}; }
0299 Result getResult() { return {Factory}; }
0300
0301 private:
0302 AdvisorFactory Factory;
0303 };
0304
0305
0306
0307 class InlineAdvisorAnalysis : public AnalysisInfoMixin<InlineAdvisorAnalysis> {
0308 public:
0309 static AnalysisKey Key;
0310 InlineAdvisorAnalysis() = default;
0311 struct Result {
0312 Result(Module &M, ModuleAnalysisManager &MAM) : M(M), MAM(MAM) {}
0313 bool invalidate(Module &, const PreservedAnalyses &PA,
0314 ModuleAnalysisManager::Invalidator &) {
0315
0316
0317 auto PAC = PA.getChecker<InlineAdvisorAnalysis>();
0318 return !PAC.preservedWhenStateless();
0319 }
0320 bool tryCreate(InlineParams Params, InliningAdvisorMode Mode,
0321 const ReplayInlinerSettings &ReplaySettings,
0322 InlineContext IC);
0323 InlineAdvisor *getAdvisor() const { return Advisor.get(); }
0324
0325 private:
0326 Module &M;
0327 ModuleAnalysisManager &MAM;
0328 std::unique_ptr<InlineAdvisor> Advisor;
0329 };
0330
0331 Result run(Module &M, ModuleAnalysisManager &MAM) { return Result(M, MAM); }
0332 };
0333
0334
0335 class InlineAdvisorAnalysisPrinterPass
0336 : public PassInfoMixin<InlineAdvisorAnalysisPrinterPass> {
0337 raw_ostream &OS;
0338
0339 public:
0340 explicit InlineAdvisorAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
0341
0342 PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
0343
0344 PreservedAnalyses run(LazyCallGraph::SCC &InitialC, CGSCCAnalysisManager &AM,
0345 LazyCallGraph &CG, CGSCCUpdateResult &UR);
0346 static bool isRequired() { return true; }
0347 };
0348
0349 std::unique_ptr<InlineAdvisor>
0350 getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM,
0351 std::function<bool(CallBase &)> GetDefaultAdvice);
0352
0353 std::unique_ptr<InlineAdvisor>
0354 getDevelopmentModeAdvisor(Module &M, ModuleAnalysisManager &MAM,
0355 std::function<bool(CallBase &)> GetDefaultAdvice);
0356
0357
0358
0359
0360
0361
0362
0363
0364 std::optional<InlineCost>
0365 shouldInline(CallBase &CB, TargetTransformInfo &CalleeTTI,
0366 function_ref<InlineCost(CallBase &CB)> GetInlineCost,
0367 OptimizationRemarkEmitter &ORE, bool EnableDeferral = true);
0368
0369
0370 void emitInlinedInto(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
0371 const BasicBlock *Block, const Function &Callee,
0372 const Function &Caller, bool IsMandatory,
0373 function_ref<void(OptimizationRemark &)> ExtraContext = {},
0374 const char *PassName = nullptr);
0375
0376
0377 void emitInlinedIntoBasedOnCost(OptimizationRemarkEmitter &ORE, DebugLoc DLoc,
0378 const BasicBlock *Block, const Function &Callee,
0379 const Function &Caller, const InlineCost &IC,
0380 bool ForProfileContext = false,
0381 const char *PassName = nullptr);
0382
0383
0384 void addLocationToRemarks(OptimizationRemark &Remark, DebugLoc DLoc);
0385
0386
0387 void setInlineRemark(CallBase &CB, StringRef Message);
0388
0389
0390 std::string inlineCostStr(const InlineCost &IC);
0391 }
0392 #endif