LLVM  15.0.0git
Inliner.h
Go to the documentation of this file.
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 
18 #include "llvm/IR/PassManager.h"
19 
20 namespace llvm {
21 
22 class AssumptionCacheTracker;
23 class CallGraph;
24 class ProfileSummaryInfo;
25 
26 /// This class contains all of the helper code which is used to perform the
27 /// inlining operations that do not depend on the policy. It contains the core
28 /// bottom-up inlining infrastructure that specific inliner passes use.
30  explicit LegacyInlinerBase(char &ID);
31  explicit LegacyInlinerBase(char &ID, bool InsertLifetime);
32 
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 
39 
40  bool doInitialization(CallGraph &CG) override;
41 
42  /// Main run interface method, this implements the interface required by the
43  /// Pass class.
44  bool runOnSCC(CallGraphSCC &SCC) override;
45 
47 
48  /// Remove now-dead linkonce functions at the end of processing to avoid
49  /// breaking the SCC traversal.
50  bool doFinalization(CallGraph &CG) override;
51 
52  /// This method must be implemented by the subclass to determine the cost of
53  /// inlining the specified call site. If the cost returned is greater than
54  /// the current inline threshold, the call site is not inlined.
55  virtual InlineCost getInlineCost(CallBase &CB) = 0;
56 
57  /// Remove dead functions.
58  ///
59  /// This also includes a hack in the form of the 'AlwaysInlineOnly' flag
60  /// which restricts it to deleting functions with an 'AlwaysInline'
61  /// attribute. This is useful for the InlineAlways pass that only wants to
62  /// deal with that subset of the functions.
63  bool removeDeadFunctions(CallGraph &CG, bool AlwaysInlineOnly = false);
64 
65  /// This function performs the main work of the pass. The default of
66  /// Inlinter::runOnSCC() calls skipSCC() before calling this method, but
67  /// derived classes which cannot be skipped can override that method and call
68  /// this function unconditionally.
70 
71 private:
72  // Insert @llvm.lifetime intrinsics.
73  bool InsertLifetime = true;
74 
75 protected:
80 };
81 
82 /// The inliner pass for the new pass manager.
83 ///
84 /// This pass wires together the inlining utilities and the inline cost
85 /// analysis into a CGSCC pass. It considers every call in every function in
86 /// the SCC and tries to inline if profitable. It can be tuned with a number of
87 /// parameters to control what cost model is used and what tradeoffs are made
88 /// when making the decision.
89 ///
90 /// It should be noted that the legacy inliners do considerably more than this
91 /// inliner pass does. They provide logic for manually merging allocas, and
92 /// doing considerable DCE including the DCE of dead functions. This pass makes
93 /// every attempt to be simpler. DCE of functions requires complex reasoning
94 /// about comdat groups, etc. Instead, it is expected that other more focused
95 /// passes be composed to achieve the same end result.
96 class InlinerPass : public PassInfoMixin<InlinerPass> {
97 public:
98  InlinerPass(bool OnlyMandatory = false,
100  : OnlyMandatory(OnlyMandatory), LTOPhase(LTOPhase) {}
101  InlinerPass(InlinerPass &&Arg) = default;
102 
105 
106  void printPipeline(raw_ostream &OS,
107  function_ref<StringRef(StringRef)> MapClassName2PassName);
108 
109 private:
112  std::unique_ptr<InlineAdvisor> OwnedAdvisor;
113  const bool OnlyMandatory;
114  const ThinOrFullLTOPhase LTOPhase;
115 };
116 
117 /// Module pass, wrapping the inliner pass. This works in conjunction with the
118 /// InlineAdvisorAnalysis to facilitate inlining decisions taking into account
119 /// module-wide state, that need to keep track of inter-inliner pass runs, for
120 /// a given module. An InlineAdvisor is configured and kept alive for the
121 /// duration of the ModuleInlinerWrapperPass::run.
123  : public PassInfoMixin<ModuleInlinerWrapperPass> {
124 public:
126  InlineParams Params = getInlineParams(), bool MandatoryFirst = true,
127  InlineContext IC = {},
129  unsigned MaxDevirtIterations = 0);
131 
133 
134  /// Allow adding more CGSCC passes, besides inlining. This should be called
135  /// before run is called, as part of pass pipeline building.
136  CGSCCPassManager &getPM() { return PM; }
137 
138  /// Add a module pass that runs before the CGSCC passes.
139  template <class T> void addModulePass(T Pass) {
140  MPM.addPass(std::move(Pass));
141  }
142 
143  /// Add a module pass that runs after the CGSCC passes.
144  template <class T> void addLateModulePass(T Pass) {
145  AfterCGMPM.addPass(std::move(Pass));
146  }
147 
148  void printPipeline(raw_ostream &OS,
149  function_ref<StringRef(StringRef)> MapClassName2PassName);
150 
151 private:
152  const InlineParams Params;
153  const InlineContext IC;
154  const InliningAdvisorMode Mode;
155  const unsigned MaxDevirtIterations;
156  // TODO: Clean this up so we only have one ModulePassManager.
157  CGSCCPassManager PM;
158  ModulePassManager MPM;
159  ModulePassManager AfterCGMPM;
160 };
161 } // end namespace llvm
162 
163 #endif // LLVM_TRANSFORMS_IPO_INLINER_H
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::LegacyInlinerBase::getInlineCost
virtual InlineCost getInlineCost(CallBase &CB)=0
This method must be implemented by the subclass to determine the cost of inlining the specified call ...
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:371
llvm::Function
Definition: Function.h:60
llvm::PassManager::addPass
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same< PassT, PassManager >::value > addPass(PassT &&Pass)
Definition: PassManager.h:550
llvm::LegacyInlinerBase::inlineCalls
bool inlineCalls(CallGraphSCC &SCC)
This function performs the main work of the pass.
Definition: Inliner.cpp:597
llvm::LegacyInlinerBase::GetTLI
std::function< const TargetLibraryInfo &(Function &)> GetTLI
Definition: Inliner.h:78
llvm::ImportedFunctionsInliningStatistics
Calculate and dump ThinLTO specific inliner stats.
Definition: ImportedFunctionsInliningStatistics.h:44
llvm::CallGraph
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:72
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
llvm::InlineParams
Thresholds to tune inline cost analysis.
Definition: InlineCost.h:190
llvm::LegacyInlinerBase::LegacyInlinerBase
LegacyInlinerBase(char &ID)
Definition: Inliner.cpp:166
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:186
llvm::ThinOrFullLTOPhase
ThinOrFullLTOPhase
This enumerates the LLVM full LTO or ThinLTO optimization phases.
Definition: Pass.h:73
llvm::ModuleInlinerWrapperPass
Module pass, wrapping the inliner pass.
Definition: Inliner.h:122
ImportedFunctionsInliningStatistics.h
llvm::CallGraphSCC
CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
Definition: CallGraphSCCPass.h:87
llvm::LazyCallGraph::SCC
An SCC of the call graph.
Definition: LazyCallGraph.h:419
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::ModuleInlinerWrapperPass::printPipeline
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition: Inliner.cpp:1184
MAM
ModuleAnalysisManager MAM
Definition: PassBuilderBindings.cpp:61
llvm::ModuleInlinerWrapperPass::getPM
CGSCCPassManager & getPM()
Allow adding more CGSCC passes, besides inlining.
Definition: Inliner.h:136
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::InlineCost
Represents the cost of inlining a function.
Definition: InlineCost.h:87
InlineAdvisor.h
llvm::OuterAnalysisManagerProxy::Result
Result proxy object for OuterAnalysisManagerProxy.
Definition: PassManager.h:1067
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::getInlineParams
InlineParams getInlineParams()
Generate the parameters to tune the inline cost analysis based only on the commandline options.
Definition: InlineCost.cpp:3081
LazyCallGraph.h
llvm::InlineContext
Provides context on when an inline advisor is constructed in the pipeline (e.g., link phase,...
Definition: InlineAdvisor.h:58
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::LegacyInlinerBase::ACT
AssumptionCacheTracker * ACT
Definition: Inliner.h:76
llvm::LegacyInlinerBase::removeDeadFunctions
bool removeDeadFunctions(CallGraph &CG, bool AlwaysInlineOnly=false)
Remove dead functions.
Definition: Inliner.cpp:623
llvm::ProfileSummaryInfo
Analysis providing profile information.
Definition: ProfileSummaryInfo.h:39
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::InliningAdvisorMode
InliningAdvisorMode
There are 3 scenarios we can use the InlineAdvisor:
Definition: InlineAdvisor.h:42
llvm::LegacyInlinerBase::runOnSCC
bool runOnSCC(CallGraphSCC &SCC) override
Main run interface method, this implements the interface required by the Pass class.
Definition: Inliner.cpp:355
llvm::LegacyInlinerBase
This class contains all of the helper code which is used to perform the inlining operations that do n...
Definition: Inliner.h:29
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
CGSCCPassManager.h
llvm::LegacyInlinerBase::PSI
ProfileSummaryInfo * PSI
Definition: Inliner.h:77
InlineCost.h
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:82
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::AMDGPU::CPol::SCC
@ SCC
Definition: SIDefines.h:304
llvm::Pass::doFinalization
virtual bool doFinalization(Module &)
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: Pass.h:120
llvm::AssumptionCacheTracker
An immutable pass that tracks lazily created AssumptionCache objects.
Definition: AssumptionCache.h:202
llvm::Pass::doInitialization
virtual bool doInitialization(Module &)
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: Pass.h:116
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::InlineAdvisor
Interface for deciding whether to inline a call site or not.
Definition: InlineAdvisor.h:163
llvm::InlinerPass::printPipeline
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition: Inliner.cpp:1176
CallGraphSCCPass.h
llvm::ModuleInlinerWrapperPass::addModulePass
void addModulePass(T Pass)
Add a module pass that runs before the CGSCC passes.
Definition: Inliner.h:139
llvm::PassManager< LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, CGSCCUpdateResult & >
llvm::CGSCCUpdateResult
Support structure for SCC passes to communicate updates the call graph back to the CGSCC pass manager...
Definition: CGSCCPassManager.h:232
llvm::ModuleInlinerWrapperPass::ModuleInlinerWrapperPass
ModuleInlinerWrapperPass(InlineParams Params=getInlineParams(), bool MandatoryFirst=true, InlineContext IC={}, InliningAdvisorMode Mode=InliningAdvisorMode::Default, unsigned MaxDevirtIterations=0)
Definition: Inliner.cpp:1115
llvm::CallGraphSCCPass
Definition: CallGraphSCCPass.h:34
PassManager.h
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:222
llvm::ThinOrFullLTOPhase::None
@ None
No LTO/ThinLTO behavior needed.
llvm::ModuleInlinerWrapperPass::addLateModulePass
void addLateModulePass(T Pass)
Add a module pass that runs after the CGSCC passes.
Definition: Inliner.h:144
llvm::ModuleInlinerWrapperPass::run
PreservedAnalyses run(Module &, ModuleAnalysisManager &)
Definition: Inliner.cpp:1137
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
llvm::InliningAdvisorMode::Default
@ Default
llvm::InlinerPass::InlinerPass
InlinerPass(bool OnlyMandatory=false, ThinOrFullLTOPhase LTOPhase=ThinOrFullLTOPhase::None)
Definition: Inliner.h:98
llvm::InlinerPass
The inliner pass for the new pass manager.
Definition: Inliner.h:96
llvm::InlinerPass::run
PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, LazyCallGraph &CG, CGSCCUpdateResult &UR)
Definition: Inliner.cpp:743
llvm::LegacyInlinerBase::doFinalization
bool doFinalization(CallGraph &CG) override
Remove now-dead linkonce functions at the end of processing to avoid breaking the SCC traversal.
Definition: Inliner.cpp:615
llvm::LegacyInlinerBase::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &Info) const override
For this class, we declare that we require and preserve the call graph.
Definition: Inliner.cpp:174
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::LazyCallGraph
A lazily constructed view of the call graph of a module.
Definition: LazyCallGraph.h:110
llvm::MaxDevirtIterations
cl::opt< unsigned > MaxDevirtIterations("max-devirt-iterations", cl::ReallyHidden, cl::init(4))
Definition: PassBuilderPipelines.cpp:200
llvm::LegacyInlinerBase::doInitialization
bool doInitialization(CallGraph &CG) override
doInitialization - This method is called before the SCC's of the program has been processed,...
Definition: Inliner.cpp:349
llvm::LegacyInlinerBase::ImportedFunctionsStats
ImportedFunctionsInliningStatistics ImportedFunctionsStats
Definition: Inliner.h:79