blob: 31589ac99eea13c67cfc83b9126ce7867a8a2db7 [file] [log] [blame]
Chris Lattner7d30a6c2004-06-20 04:11:48 +00001//===- Inliner.cpp - Code common to all inliners --------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd075cc22003-08-31 19:10:30 +00009//
Chris Lattner6754b822004-05-23 21:22:17 +000010// This file implements the mechanics required to implement inlining without
11// missing any calls and updating the call graph. The decisions of which calls
12// are profitable to inline are implemented elsewhere.
Chris Lattnerd075cc22003-08-31 19:10:30 +000013//
14//===----------------------------------------------------------------------===//
15
Chandler Carruth1d963112016-12-20 03:15:32 +000016#include "llvm/Transforms/IPO/Inliner.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/Statistic.h"
Hal Finkel0c083022014-09-01 09:01:39 +000019#include "llvm/Analysis/AliasAnalysis.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000020#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000021#include "llvm/Analysis/BasicAliasAnalysis.h"
Easwaran Raman12585b02017-01-20 22:44:04 +000022#include "llvm/Analysis/BlockFrequencyInfo.h"
Chris Lattnerd075cc22003-08-31 19:10:30 +000023#include "llvm/Analysis/CallGraph.h"
Dan Gohman4552e3c2009-10-13 18:30:07 +000024#include "llvm/Analysis/InlineCost.h"
Adam Nemet896c09b2016-08-10 00:44:44 +000025#include "llvm/Analysis/OptimizationDiagnosticInfo.h"
Easwaran Raman71069cf2016-06-09 22:23:21 +000026#include "llvm/Analysis/ProfileSummaryInfo.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000027#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000028#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/DataLayout.h"
Diego Novilloa9298b22014-04-08 16:42:34 +000030#include "llvm/IR/DiagnosticInfo.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000031#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Instructions.h"
33#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000035#include "llvm/Support/Debug.h"
Daniel Dunbar0dd5e1e2009-07-25 00:23:56 +000036#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/Transforms/Utils/Cloning.h"
38#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth6e9bb7e2016-12-26 23:43:27 +000039#include "llvm/Transforms/Utils/ModuleUtils.h"
Chris Lattnera82f1312003-11-21 21:45:31 +000040using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000041
Chandler Carruth964daaa2014-04-22 02:55:47 +000042#define DEBUG_TYPE "inline"
43
Chris Lattner1631bcb2006-12-19 22:09:18 +000044STATISTIC(NumInlined, "Number of functions inlined");
Chris Lattnerb49a6222010-05-01 17:19:38 +000045STATISTIC(NumCallsDeleted, "Number of call sites deleted, not inlined");
Chris Lattner1631bcb2006-12-19 22:09:18 +000046STATISTIC(NumDeleted, "Number of functions deleted because all callers found");
Chris Lattnerd3374e82009-08-27 06:29:33 +000047STATISTIC(NumMergedAllocas, "Number of allocas merged together");
Chris Lattner1631bcb2006-12-19 22:09:18 +000048
Benjamin Kramerbde91762012-06-02 10:20:22 +000049// This weirdly named statistic tracks the number of times that, when attempting
Chandler Carruth7ae90d42012-04-11 10:15:10 +000050// to inline a function A into B, we analyze the callers of B in order to see
51// if those would be more profitable and blocked inline steps.
52STATISTIC(NumCallerCallersAnalyzed, "Number of caller-callers analyzed");
53
Chandler Carruthf702d8e2016-08-17 02:40:23 +000054/// Flag to disable manual alloca merging.
55///
56/// Merging of allocas was originally done as a stack-size saving technique
57/// prior to LLVM's code generator having support for stack coloring based on
58/// lifetime markers. It is now in the process of being removed. To experiment
59/// with disabling it and relying fully on lifetime marker based stack
60/// coloring, you can pass this flag to LLVM.
61static cl::opt<bool>
62 DisableInlinedAllocaMerging("disable-inlined-alloca-merging",
63 cl::init(false), cl::Hidden);
64
Piotr Padlewski84abc742016-07-29 00:27:16 +000065namespace {
66enum class InlinerFunctionImportStatsOpts {
67 No = 0,
68 Basic = 1,
69 Verbose = 2,
70};
71
72cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats(
73 "inliner-function-import-stats",
74 cl::init(InlinerFunctionImportStatsOpts::No),
75 cl::values(clEnumValN(InlinerFunctionImportStatsOpts::Basic, "basic",
76 "basic statistics"),
77 clEnumValN(InlinerFunctionImportStatsOpts::Verbose, "verbose",
Mehdi Amini732afdd2016-10-08 19:41:06 +000078 "printing of statistics for each inlined function")),
Piotr Padlewski84abc742016-07-29 00:27:16 +000079 cl::Hidden, cl::desc("Enable inliner stats for imported functions"));
80} // namespace
81
Chandler Carruth1d963112016-12-20 03:15:32 +000082LegacyInlinerBase::LegacyInlinerBase(char &ID)
83 : CallGraphSCCPass(ID), InsertLifetime(true) {}
Chris Lattnerd075cc22003-08-31 19:10:30 +000084
Chandler Carruth1d963112016-12-20 03:15:32 +000085LegacyInlinerBase::LegacyInlinerBase(char &ID, bool InsertLifetime)
Easwaran Ramanb1bd3982016-03-08 00:36:35 +000086 : CallGraphSCCPass(ID), InsertLifetime(InsertLifetime) {}
Chris Lattner22ad7ab2008-01-12 06:49:13 +000087
Sanjay Patelf1b0db12015-03-10 16:42:24 +000088/// For this class, we declare that we require and preserve the call graph.
89/// If the derived class implements this method, it should
Chris Lattnerf94bed32007-01-30 23:28:39 +000090/// always explicitly call the implementation here.
Chandler Carruth1d963112016-12-20 03:15:32 +000091void LegacyInlinerBase::getAnalysisUsage(AnalysisUsage &AU) const {
Daniel Jasperaec2fa32016-12-19 08:22:17 +000092 AU.addRequired<AssumptionCacheTracker>();
Easwaran Raman71069cf2016-06-09 22:23:21 +000093 AU.addRequired<ProfileSummaryInfoWrapperPass>();
Chandler Carruth7b560d42015-09-09 17:55:00 +000094 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth12884f72016-03-02 15:56:53 +000095 getAAResultsAnalysisUsage(AU);
Chandler Carruthe40e60e2012-12-27 11:17:15 +000096 CallGraphSCCPass::getAnalysisUsage(AU);
Chris Lattnerf94bed32007-01-30 23:28:39 +000097}
98
Chandler Carruth8562d3a2016-08-03 01:02:31 +000099typedef DenseMap<ArrayType *, std::vector<AllocaInst *>> InlinedArrayAllocasTy;
Chris Lattnerd3374e82009-08-27 06:29:33 +0000100
Chandler Carruthf702d8e2016-08-17 02:40:23 +0000101/// Look at all of the allocas that we inlined through this call site. If we
102/// have already inlined other allocas through other calls into this function,
103/// then we know that they have disjoint lifetimes and that we can merge them.
Chris Lattnerd3374e82009-08-27 06:29:33 +0000104///
Chandler Carruthf702d8e2016-08-17 02:40:23 +0000105/// There are many heuristics possible for merging these allocas, and the
106/// different options have different tradeoffs. One thing that we *really*
107/// don't want to hurt is SRoA: once inlining happens, often allocas are no
108/// longer address taken and so they can be promoted.
109///
110/// Our "solution" for that is to only merge allocas whose outermost type is an
111/// array type. These are usually not promoted because someone is using a
112/// variable index into them. These are also often the most important ones to
113/// merge.
114///
115/// A better solution would be to have real memory lifetime markers in the IR
116/// and not have the inliner do any merging of allocas at all. This would
117/// allow the backend to do proper stack slot coloring of all allocas that
118/// *actually make it to the backend*, which is really what we want.
119///
120/// Because we don't have this information, we do this simple and useful hack.
121static void mergeInlinedArrayAllocas(
122 Function *Caller, InlineFunctionInfo &IFI,
123 InlinedArrayAllocasTy &InlinedArrayAllocas, int InlineHistory) {
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000124 SmallPtrSet<AllocaInst *, 16> UsedAllocas;
125
Chris Lattnerfb212de2010-12-06 07:52:42 +0000126 // When processing our SCC, check to see if CS was inlined from some other
127 // call site. For example, if we're processing "A" in this code:
128 // A() { B() }
129 // B() { x = alloca ... C() }
130 // C() { y = alloca ... }
131 // Assume that C was not inlined into B initially, and so we're processing A
132 // and decide to inline B into A. Doing this makes an alloca available for
133 // reuse and makes a callsite (C) available for inlining. When we process
134 // the C call site we don't want to do any alloca merging between X and Y
135 // because their scopes are not disjoint. We could make this smarter by
136 // keeping track of the inline history for each alloca in the
137 // InlinedArrayAllocas but this isn't likely to be a significant win.
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000138 if (InlineHistory != -1) // Only do merging for top-level call sites in SCC.
Chandler Carruthf702d8e2016-08-17 02:40:23 +0000139 return;
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000140
Chris Lattnerd3374e82009-08-27 06:29:33 +0000141 // Loop over all the allocas we have so far and see if they can be merged with
142 // a previously inlined alloca. If not, remember that we had it.
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000143 for (unsigned AllocaNo = 0, e = IFI.StaticAllocas.size(); AllocaNo != e;
144 ++AllocaNo) {
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000145 AllocaInst *AI = IFI.StaticAllocas[AllocaNo];
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000146
Chris Lattnerd3374e82009-08-27 06:29:33 +0000147 // Don't bother trying to merge array allocations (they will usually be
148 // canonicalized to be an allocation *of* an array), or allocations whose
149 // type is not itself an array (because we're afraid of pessimizing SRoA).
Chris Lattner229907c2011-07-18 04:54:35 +0000150 ArrayType *ATy = dyn_cast<ArrayType>(AI->getAllocatedType());
Craig Topperf40110f2014-04-25 05:29:35 +0000151 if (!ATy || AI->isArrayAllocation())
Chris Lattnerd3374e82009-08-27 06:29:33 +0000152 continue;
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000153
Chris Lattnerd3374e82009-08-27 06:29:33 +0000154 // Get the list of all available allocas for this array type.
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000155 std::vector<AllocaInst *> &AllocasForType = InlinedArrayAllocas[ATy];
156
Chris Lattnerd3374e82009-08-27 06:29:33 +0000157 // Loop over the allocas in AllocasForType to see if we can reuse one. Note
158 // that we have to be careful not to reuse the same "available" alloca for
159 // multiple different allocas that we just inlined, we use the 'UsedAllocas'
160 // set to keep track of which "available" allocas are being used by this
161 // function. Also, AllocasForType can be empty of course!
162 bool MergedAwayAlloca = false;
Yaron Keren62064d62015-06-25 19:28:24 +0000163 for (AllocaInst *AvailableAlloca : AllocasForType) {
Hal Finkel9caa8f72013-07-16 17:10:55 +0000164
165 unsigned Align1 = AI->getAlignment(),
166 Align2 = AvailableAlloca->getAlignment();
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000167
Chris Lattnerd3374e82009-08-27 06:29:33 +0000168 // The available alloca has to be in the right function, not in some other
169 // function in this SCC.
170 if (AvailableAlloca->getParent() != AI->getParent())
171 continue;
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000172
Chris Lattnerd3374e82009-08-27 06:29:33 +0000173 // If the inlined function already uses this alloca then we can't reuse
174 // it.
David Blaikie70573dc2014-11-19 07:49:26 +0000175 if (!UsedAllocas.insert(AvailableAlloca).second)
Chris Lattnerd3374e82009-08-27 06:29:33 +0000176 continue;
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000177
Chris Lattnerd3374e82009-08-27 06:29:33 +0000178 // Otherwise, we *can* reuse it, RAUW AI into AvailableAlloca and declare
179 // success!
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000180 DEBUG(dbgs() << " ***MERGED ALLOCA: " << *AI
181 << "\n\t\tINTO: " << *AvailableAlloca << '\n');
182
Evgeniy Stepanovd8b86f72015-09-29 00:30:19 +0000183 // Move affected dbg.declare calls immediately after the new alloca to
Simon Pilgrim7d18a702016-11-20 13:19:49 +0000184 // avoid the situation when a dbg.declare precedes its alloca.
Evgeniy Stepanovd8b86f72015-09-29 00:30:19 +0000185 if (auto *L = LocalAsMetadata::getIfExists(AI))
186 if (auto *MDV = MetadataAsValue::getIfExists(AI->getContext(), L))
187 for (User *U : MDV->users())
188 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
189 DDI->moveBefore(AvailableAlloca->getNextNode());
190
Chris Lattnerd3374e82009-08-27 06:29:33 +0000191 AI->replaceAllUsesWith(AvailableAlloca);
Hal Finkel9caa8f72013-07-16 17:10:55 +0000192
Hal Finkelec7cd262013-07-17 14:32:41 +0000193 if (Align1 != Align2) {
194 if (!Align1 || !Align2) {
Mehdi Amini46a43552015-03-04 18:43:29 +0000195 const DataLayout &DL = Caller->getParent()->getDataLayout();
196 unsigned TypeAlign = DL.getABITypeAlignment(AI->getAllocatedType());
Hal Finkelec7cd262013-07-17 14:32:41 +0000197
198 Align1 = Align1 ? Align1 : TypeAlign;
199 Align2 = Align2 ? Align2 : TypeAlign;
200 }
201
202 if (Align1 > Align2)
203 AvailableAlloca->setAlignment(AI->getAlignment());
204 }
Hal Finkel9caa8f72013-07-16 17:10:55 +0000205
Chris Lattnerd3374e82009-08-27 06:29:33 +0000206 AI->eraseFromParent();
207 MergedAwayAlloca = true;
208 ++NumMergedAllocas;
Craig Topperf40110f2014-04-25 05:29:35 +0000209 IFI.StaticAllocas[AllocaNo] = nullptr;
Chris Lattnerd3374e82009-08-27 06:29:33 +0000210 break;
211 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000212
Chris Lattnerd3374e82009-08-27 06:29:33 +0000213 // If we already nuked the alloca, we're done with it.
214 if (MergedAwayAlloca)
215 continue;
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000216
Chris Lattnerd3374e82009-08-27 06:29:33 +0000217 // If we were unable to merge away the alloca either because there are no
218 // allocas of the right type available or because we reused them all
219 // already, remember that this alloca came from an inlined function and mark
220 // it used so we don't reuse it for other allocas from this inline
221 // operation.
222 AllocasForType.push_back(AI);
223 UsedAllocas.insert(AI);
Chris Lattner6754b822004-05-23 21:22:17 +0000224 }
Chandler Carruthf702d8e2016-08-17 02:40:23 +0000225}
226
227/// If it is possible to inline the specified call site,
228/// do so and update the CallGraph for this operation.
229///
230/// This function also does some basic book-keeping to update the IR. The
231/// InlinedArrayAllocas map keeps track of any allocas that are already
232/// available from other functions inlined into the caller. If we are able to
233/// inline this call site we attempt to reuse already available allocas or add
234/// any new allocas to the set if not possible.
235static bool InlineCallIfPossible(
236 CallSite CS, InlineFunctionInfo &IFI,
237 InlinedArrayAllocasTy &InlinedArrayAllocas, int InlineHistory,
238 bool InsertLifetime, function_ref<AAResults &(Function &)> &AARGetter,
239 ImportedFunctionsInliningStatistics &ImportedFunctionsStats) {
240 Function *Callee = CS.getCalledFunction();
241 Function *Caller = CS.getCaller();
242
243 AAResults &AAR = AARGetter(*Callee);
244
245 // Try to inline the function. Get the list of static allocas that were
246 // inlined.
247 if (!InlineFunction(CS, IFI, &AAR, InsertLifetime))
248 return false;
249
250 if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No)
251 ImportedFunctionsStats.recordInline(*Caller, *Callee);
252
253 AttributeFuncs::mergeAttributesForInlining(*Caller, *Callee);
254
255 if (!DisableInlinedAllocaMerging)
256 mergeInlinedArrayAllocas(Caller, IFI, InlinedArrayAllocas, InlineHistory);
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000257
Chris Lattner6754b822004-05-23 21:22:17 +0000258 return true;
Chris Lattnerd075cc22003-08-31 19:10:30 +0000259}
Jakob Stoklund Olesen8a19d3c2010-01-20 17:51:28 +0000260
Sean Silvaab6a6832016-07-23 04:22:50 +0000261/// Return true if inlining of CS can block the caller from being
262/// inlined which is proved to be more beneficial. \p IC is the
263/// estimated inline cost associated with callsite \p CS.
Haicheng Wuf8dc2d82017-01-30 16:15:14 +0000264/// \p TotalSecondaryCost will be set to the estimated cost of inlining the
265/// caller if \p CS is suppressed for inlining.
Sean Silvaab6a6832016-07-23 04:22:50 +0000266static bool
267shouldBeDeferred(Function *Caller, CallSite CS, InlineCost IC,
268 int &TotalSecondaryCost,
Benjamin Kramer41e66da2016-08-06 12:33:46 +0000269 function_ref<InlineCost(CallSite CS)> GetInlineCost) {
Xinliang David Li4b2fdcc2016-04-29 22:59:36 +0000270
271 // For now we only handle local or inline functions.
272 if (!Caller->hasLocalLinkage() && !Caller->hasLinkOnceODRLinkage())
273 return false;
274 // Try to detect the case where the current inlining candidate caller (call
275 // it B) is a static or linkonce-ODR function and is an inlining candidate
276 // elsewhere, and the current candidate callee (call it C) is large enough
277 // that inlining it into B would make B too big to inline later. In these
278 // circumstances it may be best not to inline C into B, but to inline B into
279 // its callers.
280 //
281 // This only applies to static and linkonce-ODR functions because those are
282 // expected to be available for inlining in the translation units where they
283 // are used. Thus we will always have the opportunity to make local inlining
284 // decisions. Importantly the linkonce-ODR linkage covers inline functions
285 // and templates in C++.
286 //
287 // FIXME: All of this logic should be sunk into getInlineCost. It relies on
288 // the internal implementation of the inline cost metrics rather than
289 // treating them as truly abstract units etc.
290 TotalSecondaryCost = 0;
291 // The candidate cost to be imposed upon the current function.
292 int CandidateCost = IC.getCost() - (InlineConstants::CallPenalty + 1);
293 // This bool tracks what happens if we do NOT inline C into B.
294 bool callerWillBeRemoved = Caller->hasLocalLinkage();
295 // This bool tracks what happens if we DO inline C into B.
296 bool inliningPreventsSomeOuterInline = false;
297 for (User *U : Caller->users()) {
298 CallSite CS2(U);
299
300 // If this isn't a call to Caller (it could be some other sort
301 // of reference) skip it. Such references will prevent the caller
302 // from being removed.
303 if (!CS2 || CS2.getCalledFunction() != Caller) {
304 callerWillBeRemoved = false;
305 continue;
306 }
307
Sean Silvaab6a6832016-07-23 04:22:50 +0000308 InlineCost IC2 = GetInlineCost(CS2);
Xinliang David Li4b2fdcc2016-04-29 22:59:36 +0000309 ++NumCallerCallersAnalyzed;
310 if (!IC2) {
311 callerWillBeRemoved = false;
312 continue;
313 }
314 if (IC2.isAlways())
315 continue;
316
Xinliang David Lif450b882016-11-04 03:00:52 +0000317 // See if inlining of the original callsite would erase the cost delta of
Xinliang David Li4b2fdcc2016-04-29 22:59:36 +0000318 // this callsite. We subtract off the penalty for the call instruction,
319 // which we would be deleting.
320 if (IC2.getCostDelta() <= CandidateCost) {
321 inliningPreventsSomeOuterInline = true;
322 TotalSecondaryCost += IC2.getCost();
323 }
324 }
325 // If all outer calls to Caller would get inlined, the cost for the last
326 // one is set very low by getInlineCost, in anticipation that Caller will
327 // be removed entirely. We did not account for this above unless there
328 // is only one caller of Caller.
Taewook Ohf22fa722017-02-14 17:30:05 +0000329 if (callerWillBeRemoved && !Caller->hasOneUse())
Piotr Padlewskid89875c2016-08-10 21:15:22 +0000330 TotalSecondaryCost -= InlineConstants::LastCallToStaticBonus;
Xinliang David Li4b2fdcc2016-04-29 22:59:36 +0000331
332 if (inliningPreventsSomeOuterInline && TotalSecondaryCost < IC.getCost())
333 return true;
334
335 return false;
336}
337
Sanjay Patelf1b0db12015-03-10 16:42:24 +0000338/// Return true if the inliner should attempt to inline at the given CallSite.
Sean Silvaab6a6832016-07-23 04:22:50 +0000339static bool shouldInline(CallSite CS,
Adam Nemet896c09b2016-08-10 00:44:44 +0000340 function_ref<InlineCost(CallSite CS)> GetInlineCost,
341 OptimizationRemarkEmitter &ORE) {
Adam Nemetc507ac92016-09-27 23:47:03 +0000342 using namespace ore;
Sean Silvaab6a6832016-07-23 04:22:50 +0000343 InlineCost IC = GetInlineCost(CS);
Adam Nemetc507ac92016-09-27 23:47:03 +0000344 Instruction *Call = CS.getInstruction();
345 Function *Callee = CS.getCalledFunction();
Adam Nemete7bdf222017-01-30 16:22:45 +0000346 Function *Caller = CS.getCaller();
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000347
Daniel Dunbar3933e662008-10-30 19:26:59 +0000348 if (IC.isAlways()) {
David Greene0122fc42010-01-05 01:27:51 +0000349 DEBUG(dbgs() << " Inlining: cost=always"
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000350 << ", Call: " << *CS.getInstruction() << "\n");
Adam Nemetc507ac92016-09-27 23:47:03 +0000351 ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AlwaysInline", Call)
352 << NV("Callee", Callee)
353 << " should always be inlined (cost=always)");
Daniel Dunbar3933e662008-10-30 19:26:59 +0000354 return true;
355 }
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000356
Daniel Dunbar3933e662008-10-30 19:26:59 +0000357 if (IC.isNever()) {
David Greene0122fc42010-01-05 01:27:51 +0000358 DEBUG(dbgs() << " NOT Inlining: cost=never"
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000359 << ", Call: " << *CS.getInstruction() << "\n");
Adam Nemete7bdf222017-01-30 16:22:45 +0000360 ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline", Call)
361 << NV("Callee", Callee) << " not inlined into "
362 << NV("Caller", Caller)
363 << " because it should never be inlined (cost=never)");
Daniel Dunbar3933e662008-10-30 19:26:59 +0000364 return false;
365 }
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000366
Chandler Carruth0539c072012-03-31 12:42:41 +0000367 if (!IC) {
368 DEBUG(dbgs() << " NOT Inlining: cost=" << IC.getCost()
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000369 << ", thres=" << (IC.getCostDelta() + IC.getCost())
370 << ", Call: " << *CS.getInstruction() << "\n");
Adam Nemete7bdf222017-01-30 16:22:45 +0000371 ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "TooCostly", Call)
372 << NV("Callee", Callee) << " not inlined into "
373 << NV("Caller", Caller) << " because too costly to inline (cost="
Adam Nemetc507ac92016-09-27 23:47:03 +0000374 << NV("Cost", IC.getCost()) << ", threshold="
375 << NV("Threshold", IC.getCostDelta() + IC.getCost()) << ")");
Daniel Dunbare7fbf9f42008-10-29 01:02:02 +0000376 return false;
Daniel Dunbare7fbf9f42008-10-29 01:02:02 +0000377 }
Dale Johannesen30599242009-10-09 00:11:32 +0000378
Xinliang David Li4b2fdcc2016-04-29 22:59:36 +0000379 int TotalSecondaryCost = 0;
Sean Silvaab6a6832016-07-23 04:22:50 +0000380 if (shouldBeDeferred(Caller, CS, IC, TotalSecondaryCost, GetInlineCost)) {
Xinliang David Li4b2fdcc2016-04-29 22:59:36 +0000381 DEBUG(dbgs() << " NOT Inlining: " << *CS.getInstruction()
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000382 << " Cost = " << IC.getCost()
383 << ", outer Cost = " << TotalSecondaryCost << '\n');
Adam Nemete7bdf222017-01-30 16:22:45 +0000384 ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "IncreaseCostInOtherContexts",
385 Call)
Adam Nemetc507ac92016-09-27 23:47:03 +0000386 << "Not inlining. Cost of inlining " << NV("Callee", Callee)
387 << " increases the cost of inlining " << NV("Caller", Caller)
388 << " in other contexts");
Xinliang David Li4b2fdcc2016-04-29 22:59:36 +0000389 return false;
Dale Johannesen30599242009-10-09 00:11:32 +0000390 }
391
Chandler Carruth0539c072012-03-31 12:42:41 +0000392 DEBUG(dbgs() << " Inlining: cost=" << IC.getCost()
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000393 << ", thres=" << (IC.getCostDelta() + IC.getCost())
394 << ", Call: " << *CS.getInstruction() << '\n');
Adam Nemetc507ac92016-09-27 23:47:03 +0000395 ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "CanBeInlined", Call)
396 << NV("Callee", Callee) << " can be inlined into "
397 << NV("Caller", Caller) << " with cost=" << NV("Cost", IC.getCost())
398 << " (threshold="
399 << NV("Threshold", IC.getCostDelta() + IC.getCost()) << ")");
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000400 return true;
Daniel Dunbare7fbf9f42008-10-29 01:02:02 +0000401}
Chris Lattnerd075cc22003-08-31 19:10:30 +0000402
Sanjay Patelf1b0db12015-03-10 16:42:24 +0000403/// Return true if the specified inline history ID
Chris Lattnere8262672010-05-01 01:05:10 +0000404/// indicates an inline history that includes the specified function.
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000405static bool InlineHistoryIncludes(
406 Function *F, int InlineHistoryID,
407 const SmallVectorImpl<std::pair<Function *, int>> &InlineHistory) {
Chris Lattnere8262672010-05-01 01:05:10 +0000408 while (InlineHistoryID != -1) {
409 assert(unsigned(InlineHistoryID) < InlineHistory.size() &&
410 "Invalid inline history ID");
411 if (InlineHistory[InlineHistoryID].first == F)
412 return true;
413 InlineHistoryID = InlineHistory[InlineHistoryID].second;
414 }
415 return false;
416}
417
Chandler Carruth1d963112016-12-20 03:15:32 +0000418bool LegacyInlinerBase::doInitialization(CallGraph &CG) {
Piotr Padlewski84abc742016-07-29 00:27:16 +0000419 if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No)
420 ImportedFunctionsStats.setModuleInfo(CG.getModule());
421 return false; // No changes to CallGraph.
422}
423
Chandler Carruth1d963112016-12-20 03:15:32 +0000424bool LegacyInlinerBase::runOnSCC(CallGraphSCC &SCC) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000425 if (skipSCC(SCC))
426 return false;
Andrew Kaylor9c81d0f2016-05-23 21:57:54 +0000427 return inlineCalls(SCC);
428}
Andrew Kayloraa641a52016-04-22 22:06:11 +0000429
Sean Silvaab6a6832016-07-23 04:22:50 +0000430static bool
431inlineCallsImpl(CallGraphSCC &SCC, CallGraph &CG,
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000432 std::function<AssumptionCache &(Function &)> GetAssumptionCache,
Sean Silvaab6a6832016-07-23 04:22:50 +0000433 ProfileSummaryInfo *PSI, TargetLibraryInfo &TLI,
434 bool InsertLifetime,
Benjamin Kramer41e66da2016-08-06 12:33:46 +0000435 function_ref<InlineCost(CallSite CS)> GetInlineCost,
436 function_ref<AAResults &(Function &)> AARGetter,
Piotr Padlewski84abc742016-07-29 00:27:16 +0000437 ImportedFunctionsInliningStatistics &ImportedFunctionsStats) {
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000438 SmallPtrSet<Function *, 8> SCCFunctions;
David Greene0122fc42010-01-05 01:27:51 +0000439 DEBUG(dbgs() << "Inliner visiting SCC:");
Yaron Keren4c548f22015-06-20 07:12:33 +0000440 for (CallGraphNode *Node : SCC) {
441 Function *F = Node->getFunction();
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000442 if (F)
443 SCCFunctions.insert(F);
David Greene0122fc42010-01-05 01:27:51 +0000444 DEBUG(dbgs() << " " << (F ? F->getName() : "INDIRECTNODE"));
Chris Lattnerd075cc22003-08-31 19:10:30 +0000445 }
Chris Lattnerd075cc22003-08-31 19:10:30 +0000446
Chris Lattner6754b822004-05-23 21:22:17 +0000447 // Scan through and identify all call sites ahead of time so that we only
448 // inline call sites in the original functions, not call sites that result
449 // from inlining other functions.
Chris Lattnere8262672010-05-01 01:05:10 +0000450 SmallVector<std::pair<CallSite, int>, 16> CallSites;
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000451
Chris Lattnere8262672010-05-01 01:05:10 +0000452 // When inlining a callee produces new call sites, we want to keep track of
453 // the fact that they were inlined from the callee. This allows us to avoid
454 // infinite inlining in some obscure cases. To represent this, we use an
455 // index into the InlineHistory vector.
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000456 SmallVector<std::pair<Function *, int>, 8> InlineHistory;
Chris Lattner6754b822004-05-23 21:22:17 +0000457
Yaron Keren4c548f22015-06-20 07:12:33 +0000458 for (CallGraphNode *Node : SCC) {
459 Function *F = Node->getFunction();
Adam Nemetcef33142016-08-26 20:21:05 +0000460 if (!F || F->isDeclaration())
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000461 continue;
462
Adam Nemetcef33142016-08-26 20:21:05 +0000463 OptimizationRemarkEmitter ORE(F);
Yaron Keren4c548f22015-06-20 07:12:33 +0000464 for (BasicBlock &BB : *F)
465 for (Instruction &I : BB) {
466 CallSite CS(cast<Value>(&I));
Dale Johannesen30599242009-10-09 00:11:32 +0000467 // If this isn't a call, or it is a call to an intrinsic, it can
Chris Lattner9e507472009-08-31 05:34:32 +0000468 // never be inlined.
Gabor Greif62f0aac2010-07-28 22:50:26 +0000469 if (!CS || isa<IntrinsicInst>(I))
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000470 continue;
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000471
Chris Lattner9e507472009-08-31 05:34:32 +0000472 // If this is a direct call to an external function, we can never inline
473 // it. If it is an indirect call, inlining may resolve it to be a
474 // direct call, so we keep it.
Yaron Kerenc66c06b2015-07-19 15:48:07 +0000475 if (Function *Callee = CS.getCalledFunction())
Adam Nemetcef33142016-08-26 20:21:05 +0000476 if (Callee->isDeclaration()) {
Adam Nemeta62b7e12016-09-27 20:55:07 +0000477 using namespace ore;
Adam Nemet04758ba2016-09-27 22:19:23 +0000478 ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "NoDefinition", &I)
Adam Nemeta62b7e12016-09-27 20:55:07 +0000479 << NV("Callee", Callee) << " will not be inlined into "
Adam Nemet11421472016-09-27 21:58:17 +0000480 << NV("Caller", CS.getCaller())
481 << " because its definition is unavailable"
482 << setIsVerbose());
Yaron Kerenc66c06b2015-07-19 15:48:07 +0000483 continue;
Adam Nemetcef33142016-08-26 20:21:05 +0000484 }
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000485
Chris Lattnere8262672010-05-01 01:05:10 +0000486 CallSites.push_back(std::make_pair(CS, -1));
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000487 }
488 }
Chris Lattnerd075cc22003-08-31 19:10:30 +0000489
David Greene0122fc42010-01-05 01:27:51 +0000490 DEBUG(dbgs() << ": " << CallSites.size() << " call sites.\n");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000491
Chris Lattnera5cdd5e2010-04-20 00:47:08 +0000492 // If there are no calls in this function, exit early.
493 if (CallSites.empty())
494 return false;
Yaron Keren6967cbb2015-07-02 14:25:09 +0000495
Chris Lattner6754b822004-05-23 21:22:17 +0000496 // Now that we have all of the call sites, move the ones to functions in the
497 // current SCC to the end of the list.
498 unsigned FirstCallInSCC = CallSites.size();
499 for (unsigned i = 0; i < FirstCallInSCC; ++i)
Chris Lattnere8262672010-05-01 01:05:10 +0000500 if (Function *F = CallSites[i].first.getCalledFunction())
Chris Lattner6754b822004-05-23 21:22:17 +0000501 if (SCCFunctions.count(F))
502 std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000503
Chris Lattnerd3374e82009-08-27 06:29:33 +0000504 InlinedArrayAllocasTy InlinedArrayAllocas;
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000505 InlineFunctionInfo InlineInfo(&CG, &GetAssumptionCache);
Chandler Carruth66b31302015-01-04 12:03:27 +0000506
Chris Lattner6754b822004-05-23 21:22:17 +0000507 // Now that we have all of the call sites, loop over them and inline them if
508 // it looks profitable to do so.
509 bool Changed = false;
510 bool LocalChange;
511 do {
512 LocalChange = false;
513 // Iterate over the outer loop because inlining functions can cause indirect
514 // calls to become direct calls.
Yaron Keren4c548f22015-06-20 07:12:33 +0000515 // CallSites may be modified inside so ranged for loop can not be used.
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000516 for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) {
Chris Lattnere8262672010-05-01 01:05:10 +0000517 CallSite CS = CallSites[CSi].first;
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000518
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000519 Function *Caller = CS.getCaller();
Chris Lattnereb9acbf2009-11-12 07:56:08 +0000520 Function *Callee = CS.getCalledFunction();
521
522 // If this call site is dead and it is to a readonly function, we should
523 // just delete the call instead of trying to inline it, regardless of
524 // size. This happens because IPSCCP propagates the result out of the
525 // call and then we're left with the dead call.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000526 if (isInstructionTriviallyDead(CS.getInstruction(), &TLI)) {
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000527 DEBUG(dbgs() << " -> Deleting dead call: " << *CS.getInstruction()
528 << "\n");
Chris Lattnereb9acbf2009-11-12 07:56:08 +0000529 // Update the call graph by deleting the edge from Callee to Caller.
530 CG[Caller]->removeCallEdgeFor(CS);
531 CS.getInstruction()->eraseFromParent();
532 ++NumCallsDeleted;
533 } else {
534 // We can only inline direct calls to non-declarations.
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000535 if (!Callee || Callee->isDeclaration())
536 continue;
537
Eric Christopherea2820342010-07-13 18:27:13 +0000538 // If this call site was obtained by inlining another function, verify
Chris Lattnere8262672010-05-01 01:05:10 +0000539 // that the include path for the function did not include the callee
Chris Lattner5b6a8652010-12-06 07:38:40 +0000540 // itself. If so, we'd be recursively inlining the same function,
Chris Lattnere8262672010-05-01 01:05:10 +0000541 // which would provide the same callsites, which would cause us to
542 // infinitely inline.
543 int InlineHistoryID = CallSites[CSi].second;
544 if (InlineHistoryID != -1 &&
545 InlineHistoryIncludes(Callee, InlineHistoryID, InlineHistory))
546 continue;
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000547
NAKAMURA Takumicd1fc4b2014-04-17 12:22:14 +0000548 // Get DebugLoc to report. CS will be invalid after Inliner.
549 DebugLoc DLoc = CS.getInstruction()->getDebugLoc();
Adam Nemet896c09b2016-08-10 00:44:44 +0000550 BasicBlock *Block = CS.getParent();
551 // FIXME for new PM: because of the old PM we currently generate ORE and
552 // in turn BFI on demand. With the new PM, the ORE dependency should
553 // just become a regular analysis dependency.
554 OptimizationRemarkEmitter ORE(Caller);
NAKAMURA Takumicd1fc4b2014-04-17 12:22:14 +0000555
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000556 // If the policy determines that we should inline this function,
557 // try to do so.
Adam Nemete7bdf222017-01-30 16:22:45 +0000558 if (!shouldInline(CS, GetInlineCost, ORE))
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000559 continue;
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000560
Chris Lattner2eee5d32010-04-22 23:37:35 +0000561 // Attempt to inline the function.
Adam Nemete7bdf222017-01-30 16:22:45 +0000562 using namespace ore;
Sean Silvaab6a6832016-07-23 04:22:50 +0000563 if (!InlineCallIfPossible(CS, InlineInfo, InlinedArrayAllocas,
Piotr Padlewski84abc742016-07-29 00:27:16 +0000564 InlineHistoryID, InsertLifetime, AARGetter,
565 ImportedFunctionsStats)) {
Adam Nemetc507ac92016-09-27 23:47:03 +0000566 ORE.emit(
567 OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc, Block)
568 << NV("Callee", Callee) << " will not be inlined into "
569 << NV("Caller", Caller));
Chris Lattnereb9acbf2009-11-12 07:56:08 +0000570 continue;
Diego Novillo7f8af8b2014-05-22 14:19:46 +0000571 }
Chris Lattnereb9acbf2009-11-12 07:56:08 +0000572 ++NumInlined;
Diego Novilloa9298b22014-04-08 16:42:34 +0000573
574 // Report the inline decision.
Adam Nemetc507ac92016-09-27 23:47:03 +0000575 ORE.emit(OptimizationRemark(DEBUG_TYPE, "Inlined", DLoc, Block)
576 << NV("Callee", Callee) << " inlined into "
577 << NV("Caller", Caller));
Diego Novilloa9298b22014-04-08 16:42:34 +0000578
Chris Lattnerc2432b92010-05-01 01:26:13 +0000579 // If inlining this function gave us any new call sites, throw them
Chris Lattner2eee5d32010-04-22 23:37:35 +0000580 // onto our worklist to process. They are useful inline candidates.
Chris Lattnerc2432b92010-05-01 01:26:13 +0000581 if (!InlineInfo.InlinedCalls.empty()) {
Chris Lattnere8262672010-05-01 01:05:10 +0000582 // Create a new inline history entry for this, so that we remember
583 // that these new callsites came about due to inlining Callee.
584 int NewHistoryID = InlineHistory.size();
585 InlineHistory.push_back(std::make_pair(Callee, InlineHistoryID));
586
Yaron Keren4c548f22015-06-20 07:12:33 +0000587 for (Value *Ptr : InlineInfo.InlinedCalls)
Chandler Carruth21211992012-03-25 04:03:40 +0000588 CallSites.push_back(std::make_pair(CallSite(Ptr), NewHistoryID));
Chris Lattnerc691de32010-04-23 18:37:01 +0000589 }
Chris Lattnereb9acbf2009-11-12 07:56:08 +0000590 }
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000591
Chris Lattnereb9acbf2009-11-12 07:56:08 +0000592 // If we inlined or deleted the last possible call site to the function,
593 // delete the function body now.
594 if (Callee && Callee->use_empty() && Callee->hasLocalLinkage() &&
Chris Lattner9e507472009-08-31 05:34:32 +0000595 // TODO: Can remove if in SCC now.
Chris Lattner081375b2009-08-31 03:15:49 +0000596 !SCCFunctions.count(Callee) &&
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000597
Chris Lattner081375b2009-08-31 03:15:49 +0000598 // The function may be apparently dead, but if there are indirect
599 // callgraph references to the node, we cannot delete it yet, this
600 // could invalidate the CGSCC iterator.
601 CG[Callee]->getNumReferences() == 0) {
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000602 DEBUG(dbgs() << " -> Deleting dead function: " << Callee->getName()
603 << "\n");
Chris Lattnerd3374e82009-08-27 06:29:33 +0000604 CallGraphNode *CalleeNode = CG[Callee];
Yaron Keren6967cbb2015-07-02 14:25:09 +0000605
Chris Lattnerd3374e82009-08-27 06:29:33 +0000606 // Remove any call graph edges from the callee to its callees.
607 CalleeNode->removeAllCalledFunctions();
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000608
Chris Lattnerd3374e82009-08-27 06:29:33 +0000609 // Removing the node for callee from the call graph and delete it.
Easwaran Ramanb1bd3982016-03-08 00:36:35 +0000610 delete CG.removeFunctionFromModule(CalleeNode);
Chris Lattnerd3374e82009-08-27 06:29:33 +0000611 ++NumDeleted;
612 }
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000613
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000614 // Remove this call site from the list. If possible, use
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000615 // swap/pop_back for efficiency, but do not use it if doing so would
616 // move a call site to a function in this SCC before the
617 // 'FirstCallInSCC' barrier.
Chris Lattner4422d312010-04-16 22:42:17 +0000618 if (SCC.isSingular()) {
Benjamin Kramer5ac57e32010-05-31 12:50:41 +0000619 CallSites[CSi] = CallSites.back();
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000620 CallSites.pop_back();
621 } else {
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000622 CallSites.erase(CallSites.begin() + CSi);
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000623 }
624 --CSi;
625
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000626 Changed = true;
627 LocalChange = true;
628 }
Chris Lattner6754b822004-05-23 21:22:17 +0000629 } while (LocalChange);
Chris Lattner4d25c862004-04-08 06:34:31 +0000630
Chris Lattnerd075cc22003-08-31 19:10:30 +0000631 return Changed;
632}
633
Chandler Carruth1d963112016-12-20 03:15:32 +0000634bool LegacyInlinerBase::inlineCalls(CallGraphSCC &SCC) {
Sean Silvaab6a6832016-07-23 04:22:50 +0000635 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000636 ACT = &getAnalysis<AssumptionCacheTracker>();
Dehao Chen5461d8b2016-09-28 21:00:58 +0000637 PSI = getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
Sean Silvaab6a6832016-07-23 04:22:50 +0000638 auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000639 auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
640 return ACT->getAssumptionCache(F);
641 };
642 return inlineCallsImpl(SCC, CG, GetAssumptionCache, PSI, TLI, InsertLifetime,
Sean Silvaab6a6832016-07-23 04:22:50 +0000643 [this](CallSite CS) { return getInlineCost(CS); },
Peter Collingbournecea1e4e2017-02-09 23:11:52 +0000644 LegacyAARGetter(*this), ImportedFunctionsStats);
Sean Silvaab6a6832016-07-23 04:22:50 +0000645}
646
Sanjay Patelf1b0db12015-03-10 16:42:24 +0000647/// Remove now-dead linkonce functions at the end of
648/// processing to avoid breaking the SCC traversal.
Chandler Carruth1d963112016-12-20 03:15:32 +0000649bool LegacyInlinerBase::doFinalization(CallGraph &CG) {
Piotr Padlewski84abc742016-07-29 00:27:16 +0000650 if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No)
651 ImportedFunctionsStats.dump(InlinerFunctionImportStats ==
652 InlinerFunctionImportStatsOpts::Verbose);
Devang Patelf0ef3572008-11-05 01:39:16 +0000653 return removeDeadFunctions(CG);
654}
655
Sanjay Patelf1b0db12015-03-10 16:42:24 +0000656/// Remove dead functions that are not included in DNR (Do Not Remove) list.
Chandler Carruth1d963112016-12-20 03:15:32 +0000657bool LegacyInlinerBase::removeDeadFunctions(CallGraph &CG,
658 bool AlwaysInlineOnly) {
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000659 SmallVector<CallGraphNode *, 16> FunctionsToRemove;
Chandler Carruth6e9bb7e2016-12-26 23:43:27 +0000660 SmallVector<Function *, 16> DeadFunctionsInComdats;
David Majnemerac256cf2015-05-05 20:14:22 +0000661
662 auto RemoveCGN = [&](CallGraphNode *CGN) {
663 // Remove any call graph edges from the function to its callees.
664 CGN->removeAllCalledFunctions();
665
666 // Remove any edges from the external node to the function's call graph
667 // node. These edges might have been made irrelegant due to
668 // optimization of the program.
669 CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
670
671 // Removing the node for callee from the call graph and delete it.
672 FunctionsToRemove.push_back(CGN);
673 };
Chris Lattnerbe8bb802004-04-21 20:44:33 +0000674
675 // Scan for all of the functions, looking for ones that should now be removed
676 // from the program. Insert the dead ones in the FunctionsToRemove set.
David Blaikiea5d7de92015-08-05 20:55:50 +0000677 for (const auto &I : CG) {
678 CallGraphNode *CGN = I.second.get();
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000679 Function *F = CGN->getFunction();
Chandler Carruthd7a5f2a2012-03-16 06:10:13 +0000680 if (!F || F->isDeclaration())
681 continue;
682
683 // Handle the case when this function is called and we only want to care
684 // about always-inline functions. This is a bit of a hack to share code
685 // between here and the InlineAlways pass.
Duncan P. N. Exon Smith2c79ad92015-02-14 01:11:29 +0000686 if (AlwaysInlineOnly && !F->hasFnAttribute(Attribute::AlwaysInline))
Chandler Carruthd7a5f2a2012-03-16 06:10:13 +0000687 continue;
688
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000689 // If the only remaining users of the function are dead constants, remove
690 // them.
691 F->removeDeadConstantUsers();
Chris Lattnerbe8bb802004-04-21 20:44:33 +0000692
Eli Friedman1923a332011-10-20 05:23:42 +0000693 if (!F->isDefTriviallyDead())
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000694 continue;
David Majnemerac077032014-10-08 19:32:32 +0000695
696 // It is unsafe to drop a function with discardable linkage from a COMDAT
697 // without also dropping the other members of the COMDAT.
698 // The inliner doesn't visit non-function entities which are in COMDAT
699 // groups so it is unsafe to do so *unless* the linkage is local.
David Majnemerac256cf2015-05-05 20:14:22 +0000700 if (!F->hasLocalLinkage()) {
Chandler Carruth6e9bb7e2016-12-26 23:43:27 +0000701 if (F->hasComdat()) {
702 DeadFunctionsInComdats.push_back(F);
David Majnemerac256cf2015-05-05 20:14:22 +0000703 continue;
704 }
705 }
Devang Patelf0ef3572008-11-05 01:39:16 +0000706
David Majnemerac256cf2015-05-05 20:14:22 +0000707 RemoveCGN(CGN);
Chris Lattnerc87784f2004-04-20 22:06:53 +0000708 }
David Majnemerac256cf2015-05-05 20:14:22 +0000709 if (!DeadFunctionsInComdats.empty()) {
Chandler Carruth6e9bb7e2016-12-26 23:43:27 +0000710 // Filter out the functions whose comdats remain alive.
711 filterDeadComdatFunctions(CG.getModule(), DeadFunctionsInComdats);
712 // Remove the rest.
713 for (Function *F : DeadFunctionsInComdats)
714 RemoveCGN(CG[F]);
David Majnemerac256cf2015-05-05 20:14:22 +0000715 }
716
Chandler Carruthd7a5f2a2012-03-16 06:10:13 +0000717 if (FunctionsToRemove.empty())
718 return false;
Chris Lattnerbe8bb802004-04-21 20:44:33 +0000719
720 // Now that we know which functions to delete, do so. We didn't want to do
721 // this inline, because that would invalidate our CallGraph::iterator
722 // objects. :(
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000723 //
Chandler Carruthd7a5f2a2012-03-16 06:10:13 +0000724 // Note that it doesn't matter that we are iterating over a non-stable order
Chris Lattner5eef6ad2009-08-27 03:51:50 +0000725 // here to do this, it doesn't matter which order the functions are deleted
726 // in.
Chandler Carruth45ae88f2012-04-01 10:41:24 +0000727 array_pod_sort(FunctionsToRemove.begin(), FunctionsToRemove.end());
Chandler Carruth8562d3a2016-08-03 01:02:31 +0000728 FunctionsToRemove.erase(
729 std::unique(FunctionsToRemove.begin(), FunctionsToRemove.end()),
730 FunctionsToRemove.end());
Yaron Keren62064d62015-06-25 19:28:24 +0000731 for (CallGraphNode *CGN : FunctionsToRemove) {
Easwaran Ramanb1bd3982016-03-08 00:36:35 +0000732 delete CG.removeFunctionFromModule(CGN);
Chris Lattnerbe8bb802004-04-21 20:44:33 +0000733 ++NumDeleted;
Chris Lattnerbe8bb802004-04-21 20:44:33 +0000734 }
Chandler Carruthd7a5f2a2012-03-16 06:10:13 +0000735 return true;
Chris Lattnerc87784f2004-04-20 22:06:53 +0000736}
Chandler Carruth1d963112016-12-20 03:15:32 +0000737
738PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
739 CGSCCAnalysisManager &AM, LazyCallGraph &CG,
740 CGSCCUpdateResult &UR) {
Chandler Carruth1d963112016-12-20 03:15:32 +0000741 const ModuleAnalysisManager &MAM =
742 AM.getResult<ModuleAnalysisManagerCGSCCProxy>(InitialC, CG).getManager();
743 bool Changed = false;
744
745 assert(InitialC.size() > 0 && "Cannot handle an empty SCC!");
746 Module &M = *InitialC.begin()->getFunction().getParent();
747 ProfileSummaryInfo *PSI = MAM.getCachedResult<ProfileSummaryAnalysis>(M);
748
Chandler Carruth20e588e2017-03-09 11:35:40 +0000749 // We use a single common worklist for calls across the entire SCC. We
750 // process these in-order and append new calls introduced during inlining to
751 // the end.
752 //
753 // Note that this particular order of processing is actually critical to
754 // avoid very bad behaviors. Consider *highly connected* call graphs where
755 // each function contains a small amonut of code and a couple of calls to
756 // other functions. Because the LLVM inliner is fundamentally a bottom-up
757 // inliner, it can handle gracefully the fact that these all appear to be
758 // reasonable inlining candidates as it will flatten things until they become
759 // too big to inline, and then move on and flatten another batch.
760 //
761 // However, when processing call edges *within* an SCC we cannot rely on this
762 // bottom-up behavior. As a consequence, with heavily connected *SCCs* of
763 // functions we can end up incrementally inlining N calls into each of
764 // N functions because each incremental inlining decision looks good and we
765 // don't have a topological ordering to prevent explosions.
766 //
767 // To compensate for this, we don't process transitive edges made immediate
768 // by inlining until we've done one pass of inlining across the entire SCC.
769 // Large, highly connected SCCs still lead to some amount of code bloat in
770 // this model, but it is uniformly spread across all the functions in the SCC
771 // and eventually they all become too large to inline, rather than
772 // incrementally maknig a single function grow in a super linear fashion.
773 SmallVector<std::pair<CallSite, int>, 16> Calls;
774
775 // Populate the initial list of calls in this SCC.
776 for (auto &N : InitialC) {
777 // We want to generally process call sites top-down in order for
778 // simplifications stemming from replacing the call with the returned value
779 // after inlining to be visible to subsequent inlining decisions.
780 // FIXME: Using instructions sequence is a really bad way to do this.
781 // Instead we should do an actual RPO walk of the function body.
782 for (Instruction &I : instructions(N.getFunction()))
783 if (auto CS = CallSite(&I))
784 if (Function *Callee = CS.getCalledFunction())
785 if (!Callee->isDeclaration())
786 Calls.push_back({CS, -1});
787 }
788 if (Calls.empty())
789 return PreservedAnalyses::all();
790
791 // Capture updatable variables for the current SCC and RefSCC.
Chandler Carruth1d963112016-12-20 03:15:32 +0000792 auto *C = &InitialC;
793 auto *RC = &C->getOuterRefSCC();
794
Chandler Carruth141bf5d2016-12-27 06:46:20 +0000795 // When inlining a callee produces new call sites, we want to keep track of
796 // the fact that they were inlined from the callee. This allows us to avoid
797 // infinite inlining in some obscure cases. To represent this, we use an
798 // index into the InlineHistory vector.
799 SmallVector<std::pair<Function *, int>, 16> InlineHistory;
Chandler Carruth1d963112016-12-20 03:15:32 +0000800
801 // Track a set vector of inlined callees so that we can augment the caller
802 // with all of their edges in the call graph before pruning out the ones that
803 // got simplified away.
804 SmallSetVector<Function *, 4> InlinedCallees;
805
806 // Track the dead functions to delete once finished with inlining calls. We
807 // defer deleting these to make it easier to handle the call graph updates.
808 SmallVector<Function *, 4> DeadFunctions;
809
Chandler Carruth20e588e2017-03-09 11:35:40 +0000810 // Loop forward over all of the calls. Note that we cannot cache the size as
811 // inlining can introduce new calls that need to be processed.
812 for (int i = 0; i < (int)Calls.size(); ++i) {
813 // We expect the calls to typically be batched with sequences of calls that
814 // have the same caller, so we first set up some shared infrastructure for
815 // this caller. We also do any pruning we can at this layer on the caller
816 // alone.
817 Function &F = *Calls[i].first.getCaller();
818 LazyCallGraph::Node &N = *CG.lookup(F);
Chandler Carruth1d963112016-12-20 03:15:32 +0000819 if (CG.lookupSCC(N) != C)
820 continue;
Chandler Carruth1d963112016-12-20 03:15:32 +0000821 if (F.hasFnAttribute(Attribute::OptimizeNone))
822 continue;
823
Chandler Carruthd4be9f42017-01-22 10:33:58 +0000824 DEBUG(dbgs() << "Inlining calls in: " << F.getName() << "\n");
825
Chandler Carruthb698d592017-01-22 10:34:01 +0000826 // Get a FunctionAnalysisManager via a proxy for this particular node. We
827 // do this each time we visit a node as the SCC may have changed and as
828 // we're going to mutate this particular function we want to make sure the
829 // proxy is in place to forward any invalidation events. We can use the
830 // manager we get here for looking up results for functions other than this
831 // node however because those functions aren't going to be mutated by this
832 // pass.
833 FunctionAnalysisManager &FAM =
834 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(*C, CG)
835 .getManager();
836 std::function<AssumptionCache &(Function &)> GetAssumptionCache =
837 [&](Function &F) -> AssumptionCache & {
838 return FAM.getResult<AssumptionAnalysis>(F);
839 };
840 auto GetBFI = [&](Function &F) -> BlockFrequencyInfo & {
841 return FAM.getResult<BlockFrequencyAnalysis>(F);
842 };
843
844 auto GetInlineCost = [&](CallSite CS) {
845 Function &Callee = *CS.getCalledFunction();
846 auto &CalleeTTI = FAM.getResult<TargetIRAnalysis>(Callee);
847 return getInlineCost(CS, Params, CalleeTTI, GetAssumptionCache, {GetBFI},
848 PSI);
849 };
850
Chandler Carruth1d963112016-12-20 03:15:32 +0000851 // Get the remarks emission analysis for the caller.
852 auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
853
Chandler Carruth20e588e2017-03-09 11:35:40 +0000854 // Now process as many calls as we have within this caller in the sequnece.
855 // We bail out as soon as the caller has to change so we can update the
856 // call graph and prepare the context of that new caller.
Chandler Carruth1d963112016-12-20 03:15:32 +0000857 bool DidInline = false;
Chandler Carruth20e588e2017-03-09 11:35:40 +0000858 for (; i < (int)Calls.size() && Calls[i].first.getCaller() == &F; ++i) {
Chandler Carruth141bf5d2016-12-27 06:46:20 +0000859 int InlineHistoryID;
860 CallSite CS;
Chandler Carruth20e588e2017-03-09 11:35:40 +0000861 std::tie(CS, InlineHistoryID) = Calls[i];
Chandler Carruth1d963112016-12-20 03:15:32 +0000862 Function &Callee = *CS.getCalledFunction();
863
Chandler Carruth141bf5d2016-12-27 06:46:20 +0000864 if (InlineHistoryID != -1 &&
865 InlineHistoryIncludes(&Callee, InlineHistoryID, InlineHistory))
866 continue;
867
Chandler Carruth1d963112016-12-20 03:15:32 +0000868 // Check whether we want to inline this callsite.
869 if (!shouldInline(CS, GetInlineCost, ORE))
870 continue;
871
Easwaran Raman12585b02017-01-20 22:44:04 +0000872 // Setup the data structure used to plumb customization into the
873 // `InlineFunction` routine.
874 InlineFunctionInfo IFI(
875 /*cg=*/nullptr, &GetAssumptionCache,
876 &FAM.getResult<BlockFrequencyAnalysis>(*(CS.getCaller())),
877 &FAM.getResult<BlockFrequencyAnalysis>(Callee));
878
Chandler Carruth1d963112016-12-20 03:15:32 +0000879 if (!InlineFunction(CS, IFI))
880 continue;
881 DidInline = true;
882 InlinedCallees.insert(&Callee);
883
884 // Add any new callsites to defined functions to the worklist.
Chandler Carruth141bf5d2016-12-27 06:46:20 +0000885 if (!IFI.InlinedCallSites.empty()) {
886 int NewHistoryID = InlineHistory.size();
887 InlineHistory.push_back({&Callee, InlineHistoryID});
888 for (CallSite &CS : reverse(IFI.InlinedCallSites))
889 if (Function *NewCallee = CS.getCalledFunction())
890 if (!NewCallee->isDeclaration())
891 Calls.push_back({CS, NewHistoryID});
892 }
Chandler Carruth1d963112016-12-20 03:15:32 +0000893
Chandler Carruth03130d92016-12-27 03:39:54 +0000894 // Merge the attributes based on the inlining.
895 AttributeFuncs::mergeAttributesForInlining(F, Callee);
896
Chandler Carruth1d963112016-12-20 03:15:32 +0000897 // For local functions, check whether this makes the callee trivially
898 // dead. In that case, we can drop the body of the function eagerly
899 // which may reduce the number of callers of other functions to one,
900 // changing inline cost thresholds.
901 if (Callee.hasLocalLinkage()) {
902 // To check this we also need to nuke any dead constant uses (perhaps
903 // made dead by this operation on other functions).
904 Callee.removeDeadConstantUsers();
905 if (Callee.use_empty()) {
Chandler Carruth814e0df2017-03-16 10:45:42 +0000906 Calls.erase(
907 std::remove_if(Calls.begin() + i + 1, Calls.end(),
908 [&Callee](const std::pair<CallSite, int> &Call) {
909 return Call.first.getCaller() == &Callee;
910 }),
911 Calls.end());
Chandler Carruth6acdca72017-01-24 12:55:57 +0000912 // Clear the body and queue the function itself for deletion when we
913 // finish inlining and call graph updates.
Chandler Carruth1d963112016-12-20 03:15:32 +0000914 // Note that after this point, it is an error to do anything other
915 // than use the callee's address or delete it.
916 Callee.dropAllReferences();
917 assert(find(DeadFunctions, &Callee) == DeadFunctions.end() &&
918 "Cannot put cause a function to become dead twice!");
919 DeadFunctions.push_back(&Callee);
920 }
921 }
922 }
923
Chandler Carruth20e588e2017-03-09 11:35:40 +0000924 // Back the call index up by one to put us in a good position to go around
925 // the outer loop.
926 --i;
927
Chandler Carruth1d963112016-12-20 03:15:32 +0000928 if (!DidInline)
929 continue;
930 Changed = true;
931
Chandler Carruth9900d182016-12-28 03:13:12 +0000932 // Add all the inlined callees' edges as ref edges to the caller. These are
933 // by definition trivial edges as we always have *some* transitive ref edge
934 // chain. While in some cases these edges are direct calls inside the
935 // callee, they have to be modeled in the inliner as reference edges as
936 // there may be a reference edge anywhere along the chain from the current
937 // caller to the callee that causes the whole thing to appear like
938 // a (transitive) reference edge that will require promotion to a call edge
939 // below.
Chandler Carruth1d963112016-12-20 03:15:32 +0000940 for (Function *InlinedCallee : InlinedCallees) {
941 LazyCallGraph::Node &CalleeN = *CG.lookup(*InlinedCallee);
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000942 for (LazyCallGraph::Edge &E : *CalleeN)
943 RC->insertTrivialRefEdge(N, E.getNode());
Chandler Carruth1d963112016-12-20 03:15:32 +0000944 }
945 InlinedCallees.clear();
946
947 // At this point, since we have made changes we have at least removed
948 // a call instruction. However, in the process we do some incremental
949 // simplification of the surrounding code. This simplification can
950 // essentially do all of the same things as a function pass and we can
951 // re-use the exact same logic for updating the call graph to reflect the
952 // change..
953 C = &updateCGAndAnalysisManagerForFunctionPass(CG, *C, N, AM, UR);
Chandler Carruthd4be9f42017-01-22 10:33:58 +0000954 DEBUG(dbgs() << "Updated inlining SCC: " << *C << "\n");
Chandler Carruth1d963112016-12-20 03:15:32 +0000955 RC = &C->getOuterRefSCC();
Chandler Carruth20e588e2017-03-09 11:35:40 +0000956 }
Chandler Carruth1d963112016-12-20 03:15:32 +0000957
958 // Now that we've finished inlining all of the calls across this SCC, delete
959 // all of the trivially dead functions, updating the call graph and the CGSCC
960 // pass manager in the process.
961 //
962 // Note that this walks a pointer set which has non-deterministic order but
963 // that is OK as all we do is delete things and add pointers to unordered
964 // sets.
965 for (Function *DeadF : DeadFunctions) {
966 // Get the necessary information out of the call graph and nuke the
Chandler Carruth6acdca72017-01-24 12:55:57 +0000967 // function there. Also, cclear out any cached analyses.
Chandler Carruth1d963112016-12-20 03:15:32 +0000968 auto &DeadC = *CG.lookupSCC(*CG.lookup(*DeadF));
Chandler Carruth6acdca72017-01-24 12:55:57 +0000969 FunctionAnalysisManager &FAM =
970 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(DeadC, CG)
971 .getManager();
972 FAM.clear(*DeadF);
973 AM.clear(DeadC);
Chandler Carruth1d963112016-12-20 03:15:32 +0000974 auto &DeadRC = DeadC.getOuterRefSCC();
975 CG.removeDeadFunction(*DeadF);
976
977 // Mark the relevant parts of the call graph as invalid so we don't visit
978 // them.
979 UR.InvalidatedSCCs.insert(&DeadC);
980 UR.InvalidatedRefSCCs.insert(&DeadRC);
981
982 // And delete the actual function from the module.
983 M.getFunctionList().erase(DeadF);
984 }
985 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
986}