Inliner.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Inliner.h - Inliner pass and infrastructure --------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TRANSFORMS_IPO_INLINER_H
  14. #define LLVM_TRANSFORMS_IPO_INLINER_H
  15. #include "llvm/Analysis/CGSCCPassManager.h"
  16. #include "llvm/Analysis/CallGraphSCCPass.h"
  17. #include "llvm/Analysis/InlineAdvisor.h"
  18. #include "llvm/Analysis/InlineCost.h"
  19. #include "llvm/Analysis/LazyCallGraph.h"
  20. #include "llvm/Analysis/Utils/ImportedFunctionsInliningStatistics.h"
  21. #include "llvm/IR/PassManager.h"
  22. #include <utility>
  23. namespace llvm {
  24. class AssumptionCacheTracker;
  25. class CallGraph;
  26. class ProfileSummaryInfo;
  27. /// This class contains all of the helper code which is used to perform the
  28. /// inlining operations that do not depend on the policy. It contains the core
  29. /// bottom-up inlining infrastructure that specific inliner passes use.
  30. struct LegacyInlinerBase : public CallGraphSCCPass {
  31. explicit LegacyInlinerBase(char &ID);
  32. explicit LegacyInlinerBase(char &ID, bool InsertLifetime);
  33. /// For this class, we declare that we require and preserve the call graph.
  34. /// If the derived class implements this method, it should always explicitly
  35. /// call the implementation here.
  36. void getAnalysisUsage(AnalysisUsage &Info) const override;
  37. using llvm::Pass::doInitialization;
  38. bool doInitialization(CallGraph &CG) override;
  39. /// Main run interface method, this implements the interface required by the
  40. /// Pass class.
  41. bool runOnSCC(CallGraphSCC &SCC) override;
  42. using llvm::Pass::doFinalization;
  43. /// Remove now-dead linkonce functions at the end of processing to avoid
  44. /// breaking the SCC traversal.
  45. bool doFinalization(CallGraph &CG) override;
  46. /// This method must be implemented by the subclass to determine the cost of
  47. /// inlining the specified call site. If the cost returned is greater than
  48. /// the current inline threshold, the call site is not inlined.
  49. virtual InlineCost getInlineCost(CallBase &CB) = 0;
  50. /// Remove dead functions.
  51. ///
  52. /// This also includes a hack in the form of the 'AlwaysInlineOnly' flag
  53. /// which restricts it to deleting functions with an 'AlwaysInline'
  54. /// attribute. This is useful for the InlineAlways pass that only wants to
  55. /// deal with that subset of the functions.
  56. bool removeDeadFunctions(CallGraph &CG, bool AlwaysInlineOnly = false);
  57. /// This function performs the main work of the pass. The default of
  58. /// Inlinter::runOnSCC() calls skipSCC() before calling this method, but
  59. /// derived classes which cannot be skipped can override that method and call
  60. /// this function unconditionally.
  61. bool inlineCalls(CallGraphSCC &SCC);
  62. private:
  63. // Insert @llvm.lifetime intrinsics.
  64. bool InsertLifetime = true;
  65. protected:
  66. AssumptionCacheTracker *ACT;
  67. ProfileSummaryInfo *PSI;
  68. std::function<const TargetLibraryInfo &(Function &)> GetTLI;
  69. ImportedFunctionsInliningStatistics ImportedFunctionsStats;
  70. };
  71. /// The inliner pass for the new pass manager.
  72. ///
  73. /// This pass wires together the inlining utilities and the inline cost
  74. /// analysis into a CGSCC pass. It considers every call in every function in
  75. /// the SCC and tries to inline if profitable. It can be tuned with a number of
  76. /// parameters to control what cost model is used and what tradeoffs are made
  77. /// when making the decision.
  78. ///
  79. /// It should be noted that the legacy inliners do considerably more than this
  80. /// inliner pass does. They provide logic for manually merging allocas, and
  81. /// doing considerable DCE including the DCE of dead functions. This pass makes
  82. /// every attempt to be simpler. DCE of functions requires complex reasoning
  83. /// about comdat groups, etc. Instead, it is expected that other more focused
  84. /// passes be composed to achieve the same end result.
  85. class InlinerPass : public PassInfoMixin<InlinerPass> {
  86. public:
  87. InlinerPass(bool OnlyMandatory = false) : OnlyMandatory(OnlyMandatory) {}
  88. InlinerPass(InlinerPass &&Arg) = default;
  89. PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM,
  90. LazyCallGraph &CG, CGSCCUpdateResult &UR);
  91. void printPipeline(raw_ostream &OS,
  92. function_ref<StringRef(StringRef)> MapClassName2PassName);
  93. private:
  94. InlineAdvisor &getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
  95. FunctionAnalysisManager &FAM, Module &M);
  96. std::unique_ptr<InlineAdvisor> OwnedAdvisor;
  97. const bool OnlyMandatory;
  98. };
  99. /// Module pass, wrapping the inliner pass. This works in conjunction with the
  100. /// InlineAdvisorAnalysis to facilitate inlining decisions taking into account
  101. /// module-wide state, that need to keep track of inter-inliner pass runs, for
  102. /// a given module. An InlineAdvisor is configured and kept alive for the
  103. /// duration of the ModuleInlinerWrapperPass::run.
  104. class ModuleInlinerWrapperPass
  105. : public PassInfoMixin<ModuleInlinerWrapperPass> {
  106. public:
  107. ModuleInlinerWrapperPass(
  108. InlineParams Params = getInlineParams(), bool MandatoryFirst = true,
  109. InliningAdvisorMode Mode = InliningAdvisorMode::Default,
  110. unsigned MaxDevirtIterations = 0);
  111. ModuleInlinerWrapperPass(ModuleInlinerWrapperPass &&Arg) = default;
  112. PreservedAnalyses run(Module &, ModuleAnalysisManager &);
  113. /// Allow adding more CGSCC passes, besides inlining. This should be called
  114. /// before run is called, as part of pass pipeline building.
  115. CGSCCPassManager &getPM() { return PM; }
  116. /// Add a module pass that runs before the CGSCC passes.
  117. template <class T> void addModulePass(T Pass) {
  118. MPM.addPass(std::move(Pass));
  119. }
  120. /// Add a module pass that runs after the CGSCC passes.
  121. template <class T> void addLateModulePass(T Pass) {
  122. AfterCGMPM.addPass(std::move(Pass));
  123. }
  124. void printPipeline(raw_ostream &OS,
  125. function_ref<StringRef(StringRef)> MapClassName2PassName);
  126. private:
  127. const InlineParams Params;
  128. const InliningAdvisorMode Mode;
  129. const unsigned MaxDevirtIterations;
  130. // TODO: Clean this up so we only have one ModulePassManager.
  131. CGSCCPassManager PM;
  132. ModulePassManager MPM;
  133. ModulePassManager AfterCGMPM;
  134. };
  135. } // end namespace llvm
  136. #endif // LLVM_TRANSFORMS_IPO_INLINER_H
  137. #ifdef __GNUC__
  138. #pragma GCC diagnostic pop
  139. #endif