1 //===- Inliner.h - Inliner pass and infrastructure --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_TRANSFORMS_IPO_INLINER_H
10 #define LLVM_TRANSFORMS_IPO_INLINER_H
11 
12 #include "llvm/Analysis/CGSCCPassManager.h"
13 #include "llvm/Analysis/InlineAdvisor.h"
14 #include "llvm/Analysis/InlineCost.h"
15 #include "llvm/Analysis/LazyCallGraph.h"
16 #include "llvm/Analysis/Utils/ImportedFunctionsInliningStatistics.h"
17 #include "llvm/IR/PassManager.h"
18 
19 namespace llvm {
20 
21 /// The inliner pass for the new pass manager.
22 ///
23 /// This pass wires together the inlining utilities and the inline cost
24 /// analysis into a CGSCC pass. It considers every call in every function in
25 /// the SCC and tries to inline if profitable. It can be tuned with a number of
26 /// parameters to control what cost model is used and what tradeoffs are made
27 /// when making the decision.
28 ///
29 /// It should be noted that the legacy inliners do considerably more than this
30 /// inliner pass does. They provide logic for manually merging allocas, and
31 /// doing considerable DCE including the DCE of dead functions. This pass makes
32 /// every attempt to be simpler. DCE of functions requires complex reasoning
33 /// about comdat groups, etc. Instead, it is expected that other more focused
34 /// passes be composed to achieve the same end result.
35 class InlinerPass : public PassInfoMixin<InlinerPass> {
36 public:
37   InlinerPass(bool OnlyMandatory = false,
38               ThinOrFullLTOPhase LTOPhase = ThinOrFullLTOPhase::None)
39       : OnlyMandatory(OnlyMandatory), LTOPhase(LTOPhase) {}
40   InlinerPass(InlinerPass &&Arg) = default;
41 
42   PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM,
43                         LazyCallGraph &CG, CGSCCUpdateResult &UR);
44 
45   void printPipeline(raw_ostream &OS,
46                      function_ref<StringRef(StringRef)> MapClassName2PassName);
47 
48 private:
49   InlineAdvisor &getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
50                             FunctionAnalysisManager &FAM, Module &M);
51   std::unique_ptr<InlineAdvisor> OwnedAdvisor;
52   const bool OnlyMandatory;
53   const ThinOrFullLTOPhase LTOPhase;
54 };
55 
56 /// Module pass, wrapping the inliner pass. This works in conjunction with the
57 /// InlineAdvisorAnalysis to facilitate inlining decisions taking into account
58 /// module-wide state, that need to keep track of inter-inliner pass runs, for
59 /// a given module. An InlineAdvisor is configured and kept alive for the
60 /// duration of the ModuleInlinerWrapperPass::run.
61 class ModuleInlinerWrapperPass
62     : public PassInfoMixin<ModuleInlinerWrapperPass> {
63 public:
64   ModuleInlinerWrapperPass(
65       InlineParams Params = getInlineParams(), bool MandatoryFirst = true,
66       InlineContext IC = {},
67       InliningAdvisorMode Mode = InliningAdvisorMode::Default,
68       unsigned MaxDevirtIterations = 0);
69   ModuleInlinerWrapperPass(ModuleInlinerWrapperPass &&Arg) = default;
70 
71   PreservedAnalyses run(Module &, ModuleAnalysisManager &);
72 
73   /// Allow adding more CGSCC passes, besides inlining. This should be called
74   /// before run is called, as part of pass pipeline building.
75   CGSCCPassManager &getPM() { return PM; }
76 
77   /// Add a module pass that runs before the CGSCC passes.
78   template <class T> void addModulePass(T Pass) {
79     MPM.addPass(std::move(Pass));
80   }
81 
82   /// Add a module pass that runs after the CGSCC passes.
83   template <class T> void addLateModulePass(T Pass) {
84     AfterCGMPM.addPass(std::move(Pass));
85   }
86 
87   void printPipeline(raw_ostream &OS,
88                      function_ref<StringRef(StringRef)> MapClassName2PassName);
89 
90 private:
91   const InlineParams Params;
92   const InlineContext IC;
93   const InliningAdvisorMode Mode;
94   const unsigned MaxDevirtIterations;
95   // TODO: Clean this up so we only have one ModulePassManager.
96   CGSCCPassManager PM;
97   ModulePassManager MPM;
98   ModulePassManager AfterCGMPM;
99 };
100 } // end namespace llvm
101 
102 #endif // LLVM_TRANSFORMS_IPO_INLINER_H
103