LLVM 23.0.0git
ModuleInliner.cpp
Go to the documentation of this file.
1//===- ModuleInliner.cpp - Code related to module inliner -----------------===//
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// This file implements the mechanics required to implement inlining without
10// missing any calls in the module level. It doesn't need any infromation about
11// SCC or call graph, which is different from the SCC inliner. The decisions of
12// which calls are profitable to inline are implemented elsewhere.
13//
14//===----------------------------------------------------------------------===//
15
17#include "llvm/ADT/ScopeExit.h"
19#include "llvm/ADT/Statistic.h"
32#include "llvm/IR/Function.h"
34#include "llvm/IR/Instruction.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/PassManager.h"
39#include "llvm/Support/Debug.h"
43#include <cassert>
44
45using namespace llvm;
46
47#define DEBUG_TYPE "module-inline"
48
49STATISTIC(NumInlined, "Number of functions inlined");
50STATISTIC(NumDeleted, "Number of functions deleted because all callers found");
51
53 "ctx-prof-promote-alwaysinline", cl::init(false), cl::Hidden,
54 cl::desc("If using a contextual profile in this module, and an indirect "
55 "call target is marked as alwaysinline, perform indirect call "
56 "promotion for that target. If multiple targets for an indirect "
57 "call site fit this description, they are all promoted."));
58
59InlineAdvisor &ModuleInlinerPass::getAdvisor(const ModuleAnalysisManager &MAM,
61 Module &M) {
62 if (OwnedAdvisor)
63 return *OwnedAdvisor;
64
65 auto *IAA = MAM.getCachedResult<InlineAdvisorAnalysis>(M);
66 if (!IAA) {
67 // It should still be possible to run the inliner as a stand-alone module
68 // pass, for test scenarios. In that case, we default to the
69 // DefaultInlineAdvisor, which doesn't need to keep state between module
70 // pass runs. It also uses just the default InlineParams. In this case, we
71 // need to use the provided FAM, which is valid for the duration of the
72 // inliner pass, and thus the lifetime of the owned advisor. The one we
73 // would get from the MAM can be invalidated as a result of the inliner's
74 // activity.
75 OwnedAdvisor = std::make_unique<DefaultInlineAdvisor>(
76 M, FAM, Params, InlineContext{LTOPhase, InlinePass::ModuleInliner});
77
78 return *OwnedAdvisor;
79 }
80 assert(IAA->getAdvisor() &&
81 "Expected a present InlineAdvisorAnalysis also have an "
82 "InlineAdvisor initialized");
83 return *IAA->getAdvisor();
84}
85
87 LibFunc LF;
88
89 // Either this is a normal library function or a "vectorizable"
90 // function. Not using the VFDatabase here because this query
91 // is related only to libraries handled via the TLI.
92 return TLI.getLibFunc(F, LF) ||
93 TLI.isKnownVectorFunctionInLibrary(F.getName());
94}
95
98 LLVM_DEBUG(dbgs() << "---- Module Inliner is Running ---- \n");
99
100 auto &IAA = MAM.getResult<InlineAdvisorAnalysis>(M);
101 if (!IAA.tryCreate(Params, Mode, {},
102 InlineContext{LTOPhase, InlinePass::ModuleInliner})) {
103 M.getContext().emitError(
104 "Could not setup Inlining Advisor for the requested "
105 "mode and/or options");
106 return PreservedAnalyses::all();
107 }
108
109 auto &CtxProf = MAM.getResult<CtxProfAnalysis>(M);
110
111 bool Changed = false;
112
113 ProfileSummaryInfo *PSI = MAM.getCachedResult<ProfileSummaryAnalysis>(M);
114
116 MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
117
118 auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
119 return FAM.getResult<TargetLibraryAnalysis>(F);
120 };
121
122 InlineAdvisor &Advisor = getAdvisor(MAM, FAM, M);
123 Advisor.onPassEntry();
124
125 llvm::scope_exit AdvisorOnExit([&] { Advisor.onPassExit(); });
126
127 // In the module inliner, a priority-based worklist is used for calls across
128 // the entire Module. With this module inliner, the inline order is not
129 // limited to bottom-up order. More globally scope inline order is enabled.
130 // Also, the inline deferral logic become unnecessary in this module inliner.
131 // It is possible to use other priority heuristics, e.g. profile-based
132 // heuristic.
133 //
134 // TODO: Here is a huge amount duplicate code between the module inliner and
135 // the SCC inliner, which need some refactoring.
136 auto Calls = getInlineOrder(FAM, Params, MAM, M);
137 assert(Calls != nullptr && "Expected an initialized InlineOrder");
138
139 // Populate the initial list of calls in this module.
141 for (Function &F : M) {
142 if (F.isDeclaration())
143 continue;
144 auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
145 for (Instruction &I : instructions(F)) {
146 if (auto *CB = dyn_cast<CallBase>(&I)) {
147 if (Function *Callee = CB->getCalledFunction()) {
148 if (!Callee->isDeclaration())
149 Calls->push(CB);
150 else if (!isa<IntrinsicInst>(I)) {
151 using namespace ore;
152 setInlineRemark(*CB, "unavailable definition");
153 ORE.emit([&]() {
154 return OptimizationRemarkMissed(DEBUG_TYPE, "NoDefinition", &I)
155 << NV("Callee", Callee) << " will not be inlined into "
156 << NV("Caller", CB->getCaller())
157 << " because its definition is unavailable"
158 << setIsVerbose();
159 });
160 }
161 } else if (CtxProfPromoteAlwaysInline &&
162 CtxProf.isInSpecializedModule() && CB->isIndirectCall()) {
164 ICPCandidates);
165 }
166 }
167 }
168 }
169 for (auto &[CB, Target] : ICPCandidates) {
170 if (auto *DirectCB = promoteCallWithIfThenElse(*CB, *Target, CtxProf))
171 Calls->push(DirectCB);
172 }
173 if (Calls->empty())
174 return PreservedAnalyses::all();
175
176 // Track the dead functions to delete once finished with inlining calls. We
177 // defer deleting these to make it easier to handle the call graph updates.
178 SmallVector<Function *, 4> DeadFunctions;
179
180 // Loop forward over all of the calls.
181 while (!Calls->empty()) {
182 CallBase *CB = Calls->pop();
183 Function &F = *CB->getCaller();
184 Function &Callee = *CB->getCalledFunction();
185
186 LLVM_DEBUG(dbgs() << "Inlining calls in: " << F.getName() << "\n"
187 << " Function size: " << F.getInstructionCount()
188 << "\n");
189 (void)F;
190
191 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
192 return FAM.getResult<AssumptionAnalysis>(F);
193 };
194
195 auto Advice = Advisor.getAdvice(*CB, /*OnlyMandatory*/ false);
196 // Check whether we want to inline this callsite.
197 if (!Advice->isInliningRecommended()) {
198 Advice->recordUnattemptedInlining();
199 continue;
200 }
201
202 // Setup the data structure used to plumb customization into the
203 // `InlineFunction` routine.
205 GetAssumptionCache, PSI,
206 &FAM.getResult<BlockFrequencyAnalysis>(*(CB->getCaller())),
207 &FAM.getResult<BlockFrequencyAnalysis>(Callee));
208
210 InlineFunction(*CB, IFI, CtxProf, /*MergeAttributes=*/true,
211 &FAM.getResult<AAManager>(*CB->getCaller()),
212 /*InsertLifetime=*/true,
213 /*TrackInlineHistory=*/true);
214 if (!IR.isSuccess()) {
215 Advice->recordUnsuccessfulInlining(IR);
216 continue;
217 }
218
219 Changed = true;
220 ++NumInlined;
221
222 LLVM_DEBUG(dbgs() << " Size after inlining: " << F.getInstructionCount()
223 << "\n");
224
225 // Add any new callsites to defined functions to the worklist.
226 if (!IFI.InlinedCallSites.empty()) {
227 for (CallBase *ICB : reverse(IFI.InlinedCallSites)) {
228 Function *NewCallee = ICB->getCalledFunction();
229 if (!NewCallee) {
230 // Try to promote an indirect (virtual) call without waiting for
231 // the post-inline cleanup and the next DevirtSCCRepeatedPass
232 // iteration because the next iteration may not happen and we may
233 // miss inlining it.
234 // FIXME: enable for ctxprof.
235 if (CtxProf.isInSpecializedModule())
236 if (tryPromoteCall(*ICB))
237 NewCallee = ICB->getCalledFunction();
238 }
239 if (NewCallee)
240 if (!NewCallee->isDeclaration())
241 Calls->push(ICB);
242 }
243 }
244
245 // For local functions, check whether this makes the callee trivially
246 // dead. In that case, we can drop the body of the function eagerly
247 // which may reduce the number of callers of other functions to one,
248 // changing inline cost thresholds.
249 bool CalleeWasDeleted = false;
250 if (Callee.hasLocalLinkage()) {
251 // To check this we also need to nuke any dead constant uses (perhaps
252 // made dead by this operation on other functions).
253 Callee.removeDeadConstantUsers();
254 // if (Callee.use_empty() && !CG.isLibFunction(Callee)) {
255 if (Callee.use_empty() && !isKnownLibFunction(Callee, GetTLI(Callee))) {
256 Calls->erase_if(
257 [&](const CallBase *CB) { return CB->getCaller() == &Callee; });
258
259 // Report inlining decision BEFORE deleting function contents, so we
260 // can still access e.g. the DebugLoc
261 Advice->recordInliningWithCalleeDeleted();
262 // Clear the body and queue the function itself for deletion when we
263 // finish inlining.
264 // Note that after this point, it is an error to do anything other
265 // than use the callee's address or delete it.
266 Callee.dropAllReferences();
267 assert(!is_contained(DeadFunctions, &Callee) &&
268 "Cannot put cause a function to become dead twice!");
269 DeadFunctions.push_back(&Callee);
270 CalleeWasDeleted = true;
271 }
272 }
273 if (!CalleeWasDeleted)
274 Advice->recordInlining();
275 }
276
277 // Now that we've finished inlining all of the calls across this module,
278 // delete all of the trivially dead functions.
279 //
280 // Note that this walks a pointer set which has non-deterministic order but
281 // that is OK as all we do is delete things and add pointers to unordered
282 // sets.
283 for (Function *DeadF : DeadFunctions) {
284 // Clear out any cached analyses.
285 FAM.clear(*DeadF, DeadF->getName());
286
287 // And delete the actual function from the module.
288 M.getFunctionList().erase(DeadF);
289
290 ++NumDeleted;
291 }
292
293 if (!Changed)
294 return PreservedAnalyses::all();
295
297}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Expand Atomic instructions
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
static bool isKnownLibFunction(Function &F, TargetLibraryInfo &TLI)
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:81
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static cl::opt< bool > CtxProfPromoteAlwaysInline("ctx-prof-promote-alwaysinline", cl::init(false), cl::Hidden, cl::desc("If using a contextual profile in this module, and an indirect " "call target is marked as alwaysinline, perform indirect call " "promotion for that target. If multiple targets for an indirect " "call site fit this description, they are all promoted."))
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
A manager for alias analyses.
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
Analysis pass which computes BlockFrequencyInfo.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI Function * getCaller()
Helper to get the caller (the parent function).
static LLVM_ABI void collectIndirectCallPromotionList(CallBase &IC, Result &Profile, SetVector< std::pair< CallBase *, Function * > > &Candidates)
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:337
The InlineAdvisorAnalysis is a module pass because the InlineAdvisor needs to capture state right bef...
Interface for deciding whether to inline a call site or not.
virtual void onPassEntry(LazyCallGraph::SCC *SCC=nullptr)
This must be called when the Inliner pass is entered, to allow the InlineAdvisor update internal stat...
virtual void onPassExit(LazyCallGraph::SCC *SCC=nullptr)
This must be called when the Inliner pass is exited, as function passes may be run subsequently.
std::unique_ptr< InlineAdvice > getAdvice(CallBase &CB, bool MandatoryOnly=false)
Get an InlineAdvice containing a recommendation on whether to inline or not.
This class captures the data input to the InlineFunction call, and records the auxiliary results prod...
Definition Cloning.h:259
SmallVector< CallBase *, 8 > InlinedCallSites
All of the new call sites inlined into the caller.
Definition Cloning.h:282
InlineResult is basically true or false.
Definition InlineCost.h:181
LLVM_ABI PreservedAnalyses run(Module &, ModuleAnalysisManager &)
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Diagnostic information for missed-optimization remarks.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
Analysis providing profile information.
A vector that has set insertion semantics.
Definition SetVector.h:57
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool isKnownVectorFunctionInLibrary(StringRef F) const
Check if the function "F" is listed in a library known to LLVM.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
Target - Wrapper for Target specific information.
Changed
initializer< Ty > init(const Ty &Val)
Add a small namespace to avoid name clashes with the classes used in the streaming interface.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI CallBase & promoteCallWithIfThenElse(CallBase &CB, Function *Callee, MDNode *BranchWeights=nullptr)
Promote the given indirect call site to conditionally call Callee.
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
LLVM_ABI void setInlineRemark(CallBase &CB, StringRef Message)
Set the inline-remark attribute.
LLVM_ABI InlineResult InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, bool MergeAttributes=false, AAResults *CalleeAAR=nullptr, bool InsertLifetime=true, bool TrackInlineHistory=false, Function *ForwardVarArgsTo=nullptr, OptimizationRemarkEmitter *ORE=nullptr)
This function inlines the called function into the basic block of the caller.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI std::unique_ptr< InlineOrder > getInlineOrder(FunctionAnalysisManager &FAM, const InlineParams &Params, ModuleAnalysisManager &MAM, Module &M)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
LLVM_ABI bool tryPromoteCall(CallBase &CB)
Try to promote (devirtualize) a virtual call on an Alloca.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
Provides context on when an inline advisor is constructed in the pipeline (e.g., link phase,...