LLVM: lib/Transforms/IPO/ModuleInliner.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
43#include
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
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
59
60
63 const SmallVectorImpl<std::pair<Function *, int>> &InlineHistory) {
64 while (InlineHistoryID != -1) {
65 assert(unsigned(InlineHistoryID) < InlineHistory.size() &&
66 "Invalid inline history ID");
67 if (InlineHistory[InlineHistoryID].first == F)
68 return true;
69 InlineHistoryID = InlineHistory[InlineHistoryID].second;
70 }
71 return false;
72}
73
77 if (OwnedAdvisor)
78 return *OwnedAdvisor;
79
81 if (!IAA) {
82
83
84
85
86
87
88
89
90 OwnedAdvisor = std::make_unique(
92
93 return *OwnedAdvisor;
94 }
95 assert(IAA->getAdvisor() &&
96 "Expected a present InlineAdvisorAnalysis also have an "
97 "InlineAdvisor initialized");
98 return *IAA->getAdvisor();
99}
100
102 LibFunc LF;
103
104
105
106
109}
110
113 LLVM_DEBUG(dbgs() << "---- Module Inliner is Running ---- \n");
114
116 if (!IAA.tryCreate(Params, Mode, {},
117 InlineContext{LTOPhase, InlinePass::ModuleInliner})) {
118 M.getContext().emitError(
119 "Could not setup Inlining Advisor for the requested "
120 "mode and/or options");
122 }
123
125
127
129
132
135 };
136
139
141
142
143
144
145
146
147
148
149
150
152 assert(Calls != nullptr && "Expected an initialized InlineOrder");
153
154
160 if (Function *Callee = CB->getCalledFunction()) {
161 if (!Callee->isDeclaration())
162 Calls->push({CB, -1});
164 using namespace ore;
166 ORE.emit([&]() {
168 << NV("Callee", Callee) << " will not be inlined into "
169 << NV("Caller", CB->getCaller())
170 << " because its definition is unavailable"
171 << setIsVerbose();
172 });
173 }
175 CtxProf.isInSpecializedModule() && CB->isIndirectCall()) {
177 ICPCandidates);
178 }
179 }
180 }
181 }
182 for (auto &[CB, Target] : ICPCandidates) {
184 Calls->push({DirectCB, -1});
185 }
186 if (Calls->empty())
188
189
190
191
192
194
195
196
198
199
200 while (!Calls->empty()) {
201 auto P = Calls->pop();
203 const int InlineHistoryID = P.second;
206
207 LLVM_DEBUG(dbgs() << "Inlining calls in: " << F.getName() << "\n"
208 << " Function size: " << F.getInstructionCount()
209 << "\n");
210 (void)F;
211
214 };
215
216 if (InlineHistoryID != -1 &&
219 continue;
220 }
221
222 auto Advice = Advisor.getAdvice(*CB, false);
223
224 if (!Advice->isInliningRecommended()) {
225 Advice->recordUnattemptedInlining();
226 continue;
227 }
228
229
230
232 GetAssumptionCache, PSI,
235
237 InlineFunction(*CB, IFI, CtxProf, true,
239 if (.isSuccess()) {
240 Advice->recordUnsuccessfulInlining(IR);
241 continue;
242 }
243
245 ++NumInlined;
246
247 LLVM_DEBUG(dbgs() << " Size after inlining: " << F.getInstructionCount()
248 << "\n");
249
250
252 int NewHistoryID = InlineHistory.size();
253 InlineHistory.push_back({&Callee, InlineHistoryID});
254
256 Function *NewCallee = ICB->getCalledFunction();
257 if (!NewCallee) {
258
259
260
261
262
263 if (CtxProf.isInSpecializedModule())
265 NewCallee = ICB->getCalledFunction();
266 }
267 if (NewCallee)
269 Calls->push({ICB, NewHistoryID});
270 }
271 }
272
273
274
275
276
277 bool CalleeWasDeleted = false;
278 if (Callee.hasLocalLinkage()) {
279
280
281 Callee.removeDeadConstantUsers();
282
283 if (Callee.use_empty() && (Callee, GetTLI(Callee))) {
284 Calls->erase_if([&](const std::pair<CallBase *, int> &Call) {
285 return Call.first->getCaller() == &Callee;
286 });
287
288
289
290 Advice->recordInliningWithCalleeDeleted();
291
292
293
294
295 Callee.dropAllReferences();
297 "Cannot put cause a function to become dead twice!");
298 DeadFunctions.push_back(&Callee);
299 CalleeWasDeleted = true;
300 }
301 }
302 if (!CalleeWasDeleted)
303 Advice->recordInlining();
304 }
305
306
307
308
309
310
311
312 for (Function *DeadF : DeadFunctions) {
313
314 FAM.clear(*DeadF, DeadF->getName());
315
316
317 M.getFunctionList().erase(DeadF);
318
319 ++NumDeleted;
320 }
321
324
326}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Expand Atomic instructions
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
static bool inlineHistoryIncludes(Function *F, int InlineHistoryID, const SmallVectorImpl< std::pair< Function *, int > > &InlineHistory)
Return true if the specified inline history ID indicates an inline history that includes the specifie...
static bool isKnownLibFunction(Function &F, TargetLibraryInfo &TLI)
Legalize the Machine IR a function s Machine IR
static bool inlineHistoryIncludes(Function *F, int InlineHistoryID, const SmallVectorImpl< std::pair< Function *, int > > &InlineHistory)
Return true if the specified inline history ID indicates an inline history that includes the specifie...
Definition ModuleInliner.cpp:61
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)
A manager for alias analyses.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
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...
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...
SmallVector< CallBase *, 8 > InlinedCallSites
All of the new call sites inlined into the caller.
InlineResult is basically true or false.
LLVM_ABI PreservedAnalyses run(Module &, ModuleAnalysisManager &)
Definition ModuleInliner.cpp:111
A Module instance is used to store all the information related to an LLVM module.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
Analysis providing profile information.
A vector that has set insertion semantics.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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.
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.
LLVM_ABI InlineResult InlineFunction(CallBase &CB, InlineFunctionInfo &IFI, bool MergeAttributes=false, AAResults *CalleeAAR=nullptr, bool InsertLifetime=true, Function *ForwardVarArgsTo=nullptr, OptimizationRemarkEmitter *ORE=nullptr)
This function inlines the called function into the basic block of the caller.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
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.
auto reverse(ContainerTy &&C)
LLVM_ABI std::unique_ptr< InlineOrder< std::pair< CallBase *, int > > > getInlineOrder(FunctionAnalysisManager &FAM, const InlineParams &Params, ModuleAnalysisManager &MAM, Module &M)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isa(const From &Val)
isa - Return true if the parameter to the template is an instance of one of the template type argu...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
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.
Provides context on when an inline advisor is constructed in the pipeline (e.g., link phase,...