1 //===- Inliner.h - Inliner pass and infrastructure --------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TRANSFORMS_IPO_INLINER_H
10 #define LLVM_TRANSFORMS_IPO_INLINER_H
12 #include "llvm/Analysis/CGSCCPassManager.h"
13 #include "llvm/Analysis/CallGraphSCCPass.h"
14 #include "llvm/Analysis/InlineCost.h"
15 #include "llvm/Analysis/LazyCallGraph.h"
16 #include "llvm/IR/CallSite.h"
17 #include "llvm/IR/PassManager.h"
18 #include "llvm/Transforms/Utils/ImportedFunctionsInliningStatistics.h"
23 class AssumptionCacheTracker
;
25 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
);
34 /// For this class, we declare that we require and preserve the call graph.
35 /// If the derived class implements this method, it should always explicitly
36 /// call the implementation here.
37 void getAnalysisUsage(AnalysisUsage
&Info
) const override
;
39 bool doInitialization(CallGraph
&CG
) override
;
41 /// Main run interface method, this implements the interface required by the
43 bool runOnSCC(CallGraphSCC
&SCC
) override
;
45 using llvm::Pass::doFinalization
;
47 /// Remove now-dead linkonce functions at the end of processing to avoid
48 /// breaking the SCC traversal.
49 bool doFinalization(CallGraph
&CG
) override
;
51 /// This method must be implemented by the subclass to determine the cost of
52 /// inlining the specified call site. If the cost returned is greater than
53 /// the current inline threshold, the call site is not inlined.
54 virtual InlineCost
getInlineCost(CallSite CS
) = 0;
56 /// Remove dead functions.
58 /// This also includes a hack in the form of the 'AlwaysInlineOnly' flag
59 /// which restricts it to deleting functions with an 'AlwaysInline'
60 /// attribute. This is useful for the InlineAlways pass that only wants to
61 /// deal with that subset of the functions.
62 bool removeDeadFunctions(CallGraph
&CG
, bool AlwaysInlineOnly
= false);
64 /// This function performs the main work of the pass. The default of
65 /// Inlinter::runOnSCC() calls skipSCC() before calling this method, but
66 /// derived classes which cannot be skipped can override that method and call
67 /// this function unconditionally.
68 bool inlineCalls(CallGraphSCC
&SCC
);
71 // Insert @llvm.lifetime intrinsics.
72 bool InsertLifetime
= true;
75 AssumptionCacheTracker
*ACT
;
76 ProfileSummaryInfo
*PSI
;
77 ImportedFunctionsInliningStatistics ImportedFunctionsStats
;
80 /// The inliner pass for the new pass manager.
82 /// This pass wires together the inlining utilities and the inline cost
83 /// analysis into a CGSCC pass. It considers every call in every function in
84 /// the SCC and tries to inline if profitable. It can be tuned with a number of
85 /// parameters to control what cost model is used and what tradeoffs are made
86 /// when making the decision.
88 /// It should be noted that the legacy inliners do considerably more than this
89 /// inliner pass does. They provide logic for manually merging allocas, and
90 /// doing considerable DCE including the DCE of dead functions. This pass makes
91 /// every attempt to be simpler. DCE of functions requires complex reasoning
92 /// about comdat groups, etc. Instead, it is expected that other more focused
93 /// passes be composed to achieve the same end result.
94 class InlinerPass
: public PassInfoMixin
<InlinerPass
> {
96 InlinerPass(InlineParams Params
= getInlineParams())
97 : Params(std::move(Params
)) {}
99 InlinerPass(InlinerPass
&&Arg
)
100 : Params(std::move(Arg
.Params
)),
101 ImportedFunctionsStats(std::move(Arg
.ImportedFunctionsStats
)) {}
103 PreservedAnalyses
run(LazyCallGraph::SCC
&C
, CGSCCAnalysisManager
&AM
,
104 LazyCallGraph
&CG
, CGSCCUpdateResult
&UR
);
108 std::unique_ptr
<ImportedFunctionsInliningStatistics
> ImportedFunctionsStats
;
111 } // end namespace llvm
113 #endif // LLVM_TRANSFORMS_IPO_INLINER_H