1 //===- Inliner.cpp - Code common to all inliners --------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // 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.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/Analysis/AssumptionCache.h"
20 #include "llvm/Analysis/BasicAliasAnalysis.h"
21 #include "llvm/Analysis/CallGraph.h"
22 #include "llvm/Analysis/InlineCost.h"
23 #include "llvm/Analysis/ProfileSummaryInfo.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
25 #include "llvm/IR/CallSite.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DiagnosticInfo.h"
28 #include "llvm/IR/Instructions.h"
29 #include "llvm/IR/IntrinsicInst.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Transforms/IPO/InlinerPass.h"
34 #include "llvm/Transforms/Utils/Cloning.h"
35 #include "llvm/Transforms/Utils/Local.h"
36 using namespace llvm;
37 
38 #define DEBUG_TYPE "inline"
39 
40 STATISTIC(NumInlined, "Number of functions inlined");
41 STATISTIC(NumCallsDeleted, "Number of call sites deleted, not inlined");
42 STATISTIC(NumDeleted, "Number of functions deleted because all callers found");
43 STATISTIC(NumMergedAllocas, "Number of allocas merged together");
44 
45 // This weirdly named statistic tracks the number of times that, when attempting
46 // to inline a function A into B, we analyze the callers of B in order to see
47 // if those would be more profitable and blocked inline steps.
48 STATISTIC(NumCallerCallersAnalyzed, "Number of caller-callers analyzed");
49 
50 Inliner::Inliner(char &ID) : CallGraphSCCPass(ID), InsertLifetime(true) {}
51 
52 Inliner::Inliner(char &ID, bool InsertLifetime)
53     : CallGraphSCCPass(ID), InsertLifetime(InsertLifetime) {}
54 
55 /// For this class, we declare that we require and preserve the call graph.
56 /// If the derived class implements this method, it should
57 /// always explicitly call the implementation here.
58 void Inliner::getAnalysisUsage(AnalysisUsage &AU) const {
59   AU.addRequired<AssumptionCacheTracker>();
60   AU.addRequired<ProfileSummaryInfoWrapperPass>();
61   AU.addRequired<TargetLibraryInfoWrapperPass>();
62   getAAResultsAnalysisUsage(AU);
63   CallGraphSCCPass::getAnalysisUsage(AU);
64 }
65 
66 
67 typedef DenseMap<ArrayType*, std::vector<AllocaInst*> >
68 InlinedArrayAllocasTy;
69 
70 /// If it is possible to inline the specified call site,
71 /// do so and update the CallGraph for this operation.
72 ///
73 /// This function also does some basic book-keeping to update the IR.  The
74 /// InlinedArrayAllocas map keeps track of any allocas that are already
75 /// available from other functions inlined into the caller.  If we are able to
76 /// inline this call site we attempt to reuse already available allocas or add
77 /// any new allocas to the set if not possible.
78 static bool
79 InlineCallIfPossible(CallSite CS, InlineFunctionInfo &IFI,
80                      InlinedArrayAllocasTy &InlinedArrayAllocas,
81                      int InlineHistory, bool InsertLifetime,
82                      std::function<AAResults &(Function &)> &AARGetter) {
83   Function *Callee = CS.getCalledFunction();
84   Function *Caller = CS.getCaller();
85 
86   AAResults &AAR = AARGetter(*Callee);
87 
88   // Try to inline the function.  Get the list of static allocas that were
89   // inlined.
90   if (!InlineFunction(CS, IFI, &AAR, InsertLifetime))
91     return false;
92 
93   AttributeFuncs::mergeAttributesForInlining(*Caller, *Callee);
94 
95   // Look at all of the allocas that we inlined through this call site.  If we
96   // have already inlined other allocas through other calls into this function,
97   // then we know that they have disjoint lifetimes and that we can merge them.
98   //
99   // There are many heuristics possible for merging these allocas, and the
100   // different options have different tradeoffs.  One thing that we *really*
101   // don't want to hurt is SRoA: once inlining happens, often allocas are no
102   // longer address taken and so they can be promoted.
103   //
104   // Our "solution" for that is to only merge allocas whose outermost type is an
105   // array type.  These are usually not promoted because someone is using a
106   // variable index into them.  These are also often the most important ones to
107   // merge.
108   //
109   // A better solution would be to have real memory lifetime markers in the IR
110   // and not have the inliner do any merging of allocas at all.  This would
111   // allow the backend to do proper stack slot coloring of all allocas that
112   // *actually make it to the backend*, which is really what we want.
113   //
114   // Because we don't have this information, we do this simple and useful hack.
115   //
116   SmallPtrSet<AllocaInst*, 16> UsedAllocas;
117 
118   // When processing our SCC, check to see if CS was inlined from some other
119   // call site.  For example, if we're processing "A" in this code:
120   //   A() { B() }
121   //   B() { x = alloca ... C() }
122   //   C() { y = alloca ... }
123   // Assume that C was not inlined into B initially, and so we're processing A
124   // and decide to inline B into A.  Doing this makes an alloca available for
125   // reuse and makes a callsite (C) available for inlining.  When we process
126   // the C call site we don't want to do any alloca merging between X and Y
127   // because their scopes are not disjoint.  We could make this smarter by
128   // keeping track of the inline history for each alloca in the
129   // InlinedArrayAllocas but this isn't likely to be a significant win.
130   if (InlineHistory != -1)  // Only do merging for top-level call sites in SCC.
131     return true;
132 
133   // Loop over all the allocas we have so far and see if they can be merged with
134   // a previously inlined alloca.  If not, remember that we had it.
135   for (unsigned AllocaNo = 0, e = IFI.StaticAllocas.size();
136        AllocaNo != e; ++AllocaNo) {
137     AllocaInst *AI = IFI.StaticAllocas[AllocaNo];
138 
139     // Don't bother trying to merge array allocations (they will usually be
140     // canonicalized to be an allocation *of* an array), or allocations whose
141     // type is not itself an array (because we're afraid of pessimizing SRoA).
142     ArrayType *ATy = dyn_cast<ArrayType>(AI->getAllocatedType());
143     if (!ATy || AI->isArrayAllocation())
144       continue;
145 
146     // Get the list of all available allocas for this array type.
147     std::vector<AllocaInst*> &AllocasForType = InlinedArrayAllocas[ATy];
148 
149     // Loop over the allocas in AllocasForType to see if we can reuse one.  Note
150     // that we have to be careful not to reuse the same "available" alloca for
151     // multiple different allocas that we just inlined, we use the 'UsedAllocas'
152     // set to keep track of which "available" allocas are being used by this
153     // function.  Also, AllocasForType can be empty of course!
154     bool MergedAwayAlloca = false;
155     for (AllocaInst *AvailableAlloca : AllocasForType) {
156 
157       unsigned Align1 = AI->getAlignment(),
158                Align2 = AvailableAlloca->getAlignment();
159 
160       // The available alloca has to be in the right function, not in some other
161       // function in this SCC.
162       if (AvailableAlloca->getParent() != AI->getParent())
163         continue;
164 
165       // If the inlined function already uses this alloca then we can't reuse
166       // it.
167       if (!UsedAllocas.insert(AvailableAlloca).second)
168         continue;
169 
170       // Otherwise, we *can* reuse it, RAUW AI into AvailableAlloca and declare
171       // success!
172       DEBUG(dbgs() << "    ***MERGED ALLOCA: " << *AI << "\n\t\tINTO: "
173                    << *AvailableAlloca << '\n');
174 
175       // Move affected dbg.declare calls immediately after the new alloca to
176       // avoid the situation when a dbg.declare preceeds its alloca.
177       if (auto *L = LocalAsMetadata::getIfExists(AI))
178         if (auto *MDV = MetadataAsValue::getIfExists(AI->getContext(), L))
179           for (User *U : MDV->users())
180             if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
181               DDI->moveBefore(AvailableAlloca->getNextNode());
182 
183       AI->replaceAllUsesWith(AvailableAlloca);
184 
185       if (Align1 != Align2) {
186         if (!Align1 || !Align2) {
187           const DataLayout &DL = Caller->getParent()->getDataLayout();
188           unsigned TypeAlign = DL.getABITypeAlignment(AI->getAllocatedType());
189 
190           Align1 = Align1 ? Align1 : TypeAlign;
191           Align2 = Align2 ? Align2 : TypeAlign;
192         }
193 
194         if (Align1 > Align2)
195           AvailableAlloca->setAlignment(AI->getAlignment());
196       }
197 
198       AI->eraseFromParent();
199       MergedAwayAlloca = true;
200       ++NumMergedAllocas;
201       IFI.StaticAllocas[AllocaNo] = nullptr;
202       break;
203     }
204 
205     // If we already nuked the alloca, we're done with it.
206     if (MergedAwayAlloca)
207       continue;
208 
209     // If we were unable to merge away the alloca either because there are no
210     // allocas of the right type available or because we reused them all
211     // already, remember that this alloca came from an inlined function and mark
212     // it used so we don't reuse it for other allocas from this inline
213     // operation.
214     AllocasForType.push_back(AI);
215     UsedAllocas.insert(AI);
216   }
217 
218   return true;
219 }
220 
221 static void emitAnalysis(CallSite CS, const Twine &Msg) {
222   Function *Caller = CS.getCaller();
223   LLVMContext &Ctx = Caller->getContext();
224   DebugLoc DLoc = CS.getInstruction()->getDebugLoc();
225   emitOptimizationRemarkAnalysis(Ctx, DEBUG_TYPE, *Caller, DLoc, Msg);
226 }
227 
228 /// Return true if inlining of CS can block the caller from being
229 /// inlined which is proved to be more beneficial. \p IC is the
230 /// estimated inline cost associated with callsite \p CS.
231 /// \p TotalAltCost will be set to the estimated cost of inlining the caller
232 /// if \p CS is suppressed for inlining.
233 static bool
234 shouldBeDeferred(Function *Caller, CallSite CS, InlineCost IC,
235                  int &TotalSecondaryCost,
236                  std::function<InlineCost(CallSite CS)> &GetInlineCost) {
237 
238   // For now we only handle local or inline functions.
239   if (!Caller->hasLocalLinkage() && !Caller->hasLinkOnceODRLinkage())
240     return false;
241   // Try to detect the case where the current inlining candidate caller (call
242   // it B) is a static or linkonce-ODR function and is an inlining candidate
243   // elsewhere, and the current candidate callee (call it C) is large enough
244   // that inlining it into B would make B too big to inline later. In these
245   // circumstances it may be best not to inline C into B, but to inline B into
246   // its callers.
247   //
248   // This only applies to static and linkonce-ODR functions because those are
249   // expected to be available for inlining in the translation units where they
250   // are used. Thus we will always have the opportunity to make local inlining
251   // decisions. Importantly the linkonce-ODR linkage covers inline functions
252   // and templates in C++.
253   //
254   // FIXME: All of this logic should be sunk into getInlineCost. It relies on
255   // the internal implementation of the inline cost metrics rather than
256   // treating them as truly abstract units etc.
257   TotalSecondaryCost = 0;
258   // The candidate cost to be imposed upon the current function.
259   int CandidateCost = IC.getCost() - (InlineConstants::CallPenalty + 1);
260   // This bool tracks what happens if we do NOT inline C into B.
261   bool callerWillBeRemoved = Caller->hasLocalLinkage();
262   // This bool tracks what happens if we DO inline C into B.
263   bool inliningPreventsSomeOuterInline = false;
264   for (User *U : Caller->users()) {
265     CallSite CS2(U);
266 
267     // If this isn't a call to Caller (it could be some other sort
268     // of reference) skip it.  Such references will prevent the caller
269     // from being removed.
270     if (!CS2 || CS2.getCalledFunction() != Caller) {
271       callerWillBeRemoved = false;
272       continue;
273     }
274 
275     InlineCost IC2 = GetInlineCost(CS2);
276     ++NumCallerCallersAnalyzed;
277     if (!IC2) {
278       callerWillBeRemoved = false;
279       continue;
280     }
281     if (IC2.isAlways())
282       continue;
283 
284     // See if inlining or original callsite would erase the cost delta of
285     // this callsite. We subtract off the penalty for the call instruction,
286     // which we would be deleting.
287     if (IC2.getCostDelta() <= CandidateCost) {
288       inliningPreventsSomeOuterInline = true;
289       TotalSecondaryCost += IC2.getCost();
290     }
291   }
292   // If all outer calls to Caller would get inlined, the cost for the last
293   // one is set very low by getInlineCost, in anticipation that Caller will
294   // be removed entirely.  We did not account for this above unless there
295   // is only one caller of Caller.
296   if (callerWillBeRemoved && !Caller->use_empty())
297     TotalSecondaryCost += InlineConstants::LastCallToStaticBonus;
298 
299   if (inliningPreventsSomeOuterInline && TotalSecondaryCost < IC.getCost())
300     return true;
301 
302   return false;
303 }
304 
305 /// Return true if the inliner should attempt to inline at the given CallSite.
306 static bool shouldInline(CallSite CS,
307                          std::function<InlineCost(CallSite CS)> GetInlineCost) {
308   InlineCost IC = GetInlineCost(CS);
309 
310   if (IC.isAlways()) {
311     DEBUG(dbgs() << "    Inlining: cost=always"
312           << ", Call: " << *CS.getInstruction() << "\n");
313     emitAnalysis(CS, Twine(CS.getCalledFunction()->getName()) +
314                          " should always be inlined (cost=always)");
315     return true;
316   }
317 
318   if (IC.isNever()) {
319     DEBUG(dbgs() << "    NOT Inlining: cost=never"
320           << ", Call: " << *CS.getInstruction() << "\n");
321     emitAnalysis(CS, Twine(CS.getCalledFunction()->getName() +
322                            " should never be inlined (cost=never)"));
323     return false;
324   }
325 
326   Function *Caller = CS.getCaller();
327   if (!IC) {
328     DEBUG(dbgs() << "    NOT Inlining: cost=" << IC.getCost()
329           << ", thres=" << (IC.getCostDelta() + IC.getCost())
330           << ", Call: " << *CS.getInstruction() << "\n");
331     emitAnalysis(CS, Twine(CS.getCalledFunction()->getName() +
332                            " too costly to inline (cost=") +
333                          Twine(IC.getCost()) + ", threshold=" +
334                          Twine(IC.getCostDelta() + IC.getCost()) + ")");
335     return false;
336   }
337 
338   int TotalSecondaryCost = 0;
339   if (shouldBeDeferred(Caller, CS, IC, TotalSecondaryCost, GetInlineCost)) {
340     DEBUG(dbgs() << "    NOT Inlining: " << *CS.getInstruction()
341           << " Cost = " << IC.getCost()
342           << ", outer Cost = " << TotalSecondaryCost << '\n');
343     emitAnalysis(CS, Twine("Not inlining. Cost of inlining " +
344                            CS.getCalledFunction()->getName() +
345                            " increases the cost of inlining " +
346                            CS.getCaller()->getName() + " in other contexts"));
347     return false;
348   }
349 
350   DEBUG(dbgs() << "    Inlining: cost=" << IC.getCost()
351         << ", thres=" << (IC.getCostDelta() + IC.getCost())
352         << ", Call: " << *CS.getInstruction() << '\n');
353   emitAnalysis(
354       CS, CS.getCalledFunction()->getName() + Twine(" can be inlined into ") +
355               CS.getCaller()->getName() + " with cost=" + Twine(IC.getCost()) +
356               " (threshold=" + Twine(IC.getCostDelta() + IC.getCost()) + ")");
357   return true;
358 }
359 
360 /// Return true if the specified inline history ID
361 /// indicates an inline history that includes the specified function.
362 static bool InlineHistoryIncludes(Function *F, int InlineHistoryID,
363             const SmallVectorImpl<std::pair<Function*, int> > &InlineHistory) {
364   while (InlineHistoryID != -1) {
365     assert(unsigned(InlineHistoryID) < InlineHistory.size() &&
366            "Invalid inline history ID");
367     if (InlineHistory[InlineHistoryID].first == F)
368       return true;
369     InlineHistoryID = InlineHistory[InlineHistoryID].second;
370   }
371   return false;
372 }
373 
374 bool Inliner::runOnSCC(CallGraphSCC &SCC) {
375   if (skipSCC(SCC))
376     return false;
377 
378   return inlineCalls(SCC);
379 }
380 
381 static bool
382 inlineCallsImpl(CallGraphSCC &SCC, CallGraph &CG,
383                 std::function<AssumptionCache &(Function &)> GetAssumptionCache,
384                 ProfileSummaryInfo *PSI, TargetLibraryInfo &TLI,
385                 bool InsertLifetime,
386                 std::function<InlineCost(CallSite CS)> GetInlineCost,
387                 std::function<AAResults &(Function &)> AARGetter) {
388   SmallPtrSet<Function*, 8> SCCFunctions;
389   DEBUG(dbgs() << "Inliner visiting SCC:");
390   for (CallGraphNode *Node : SCC) {
391     Function *F = Node->getFunction();
392     if (F) SCCFunctions.insert(F);
393     DEBUG(dbgs() << " " << (F ? F->getName() : "INDIRECTNODE"));
394   }
395 
396   // Scan through and identify all call sites ahead of time so that we only
397   // inline call sites in the original functions, not call sites that result
398   // from inlining other functions.
399   SmallVector<std::pair<CallSite, int>, 16> CallSites;
400 
401   // When inlining a callee produces new call sites, we want to keep track of
402   // the fact that they were inlined from the callee.  This allows us to avoid
403   // infinite inlining in some obscure cases.  To represent this, we use an
404   // index into the InlineHistory vector.
405   SmallVector<std::pair<Function*, int>, 8> InlineHistory;
406 
407   for (CallGraphNode *Node : SCC) {
408     Function *F = Node->getFunction();
409     if (!F) continue;
410 
411     for (BasicBlock &BB : *F)
412       for (Instruction &I : BB) {
413         CallSite CS(cast<Value>(&I));
414         // If this isn't a call, or it is a call to an intrinsic, it can
415         // never be inlined.
416         if (!CS || isa<IntrinsicInst>(I))
417           continue;
418 
419         // If this is a direct call to an external function, we can never inline
420         // it.  If it is an indirect call, inlining may resolve it to be a
421         // direct call, so we keep it.
422         if (Function *Callee = CS.getCalledFunction())
423           if (Callee->isDeclaration())
424             continue;
425 
426         CallSites.push_back(std::make_pair(CS, -1));
427       }
428   }
429 
430   DEBUG(dbgs() << ": " << CallSites.size() << " call sites.\n");
431 
432   // If there are no calls in this function, exit early.
433   if (CallSites.empty())
434     return false;
435 
436   // Now that we have all of the call sites, move the ones to functions in the
437   // current SCC to the end of the list.
438   unsigned FirstCallInSCC = CallSites.size();
439   for (unsigned i = 0; i < FirstCallInSCC; ++i)
440     if (Function *F = CallSites[i].first.getCalledFunction())
441       if (SCCFunctions.count(F))
442         std::swap(CallSites[i--], CallSites[--FirstCallInSCC]);
443 
444 
445   InlinedArrayAllocasTy InlinedArrayAllocas;
446   InlineFunctionInfo InlineInfo(&CG, &GetAssumptionCache);
447 
448   // Now that we have all of the call sites, loop over them and inline them if
449   // it looks profitable to do so.
450   bool Changed = false;
451   bool LocalChange;
452   do {
453     LocalChange = false;
454     // Iterate over the outer loop because inlining functions can cause indirect
455     // calls to become direct calls.
456     // CallSites may be modified inside so ranged for loop can not be used.
457     for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) {
458       CallSite CS = CallSites[CSi].first;
459 
460       Function *Caller = CS.getCaller();
461       Function *Callee = CS.getCalledFunction();
462 
463       // If this call site is dead and it is to a readonly function, we should
464       // just delete the call instead of trying to inline it, regardless of
465       // size.  This happens because IPSCCP propagates the result out of the
466       // call and then we're left with the dead call.
467       if (isInstructionTriviallyDead(CS.getInstruction(), &TLI)) {
468         DEBUG(dbgs() << "    -> Deleting dead call: "
469                      << *CS.getInstruction() << "\n");
470         // Update the call graph by deleting the edge from Callee to Caller.
471         CG[Caller]->removeCallEdgeFor(CS);
472         CS.getInstruction()->eraseFromParent();
473         ++NumCallsDeleted;
474       } else {
475         // We can only inline direct calls to non-declarations.
476         if (!Callee || Callee->isDeclaration()) continue;
477 
478         // If this call site was obtained by inlining another function, verify
479         // that the include path for the function did not include the callee
480         // itself.  If so, we'd be recursively inlining the same function,
481         // which would provide the same callsites, which would cause us to
482         // infinitely inline.
483         int InlineHistoryID = CallSites[CSi].second;
484         if (InlineHistoryID != -1 &&
485             InlineHistoryIncludes(Callee, InlineHistoryID, InlineHistory))
486           continue;
487 
488         LLVMContext &CallerCtx = Caller->getContext();
489 
490         // Get DebugLoc to report. CS will be invalid after Inliner.
491         DebugLoc DLoc = CS.getInstruction()->getDebugLoc();
492 
493         // If the policy determines that we should inline this function,
494         // try to do so.
495         if (!shouldInline(CS, GetInlineCost)) {
496           emitOptimizationRemarkMissed(CallerCtx, DEBUG_TYPE, *Caller, DLoc,
497                                        Twine(Callee->getName() +
498                                              " will not be inlined into " +
499                                              Caller->getName()));
500           continue;
501         }
502 
503         // Attempt to inline the function.
504         if (!InlineCallIfPossible(CS, InlineInfo, InlinedArrayAllocas,
505                                   InlineHistoryID, InsertLifetime, AARGetter)) {
506           emitOptimizationRemarkMissed(CallerCtx, DEBUG_TYPE, *Caller, DLoc,
507                                        Twine(Callee->getName() +
508                                              " will not be inlined into " +
509                                              Caller->getName()));
510           continue;
511         }
512         ++NumInlined;
513 
514         // Report the inline decision.
515         emitOptimizationRemark(
516             CallerCtx, DEBUG_TYPE, *Caller, DLoc,
517             Twine(Callee->getName() + " inlined into " + Caller->getName()));
518 
519         // If inlining this function gave us any new call sites, throw them
520         // onto our worklist to process.  They are useful inline candidates.
521         if (!InlineInfo.InlinedCalls.empty()) {
522           // Create a new inline history entry for this, so that we remember
523           // that these new callsites came about due to inlining Callee.
524           int NewHistoryID = InlineHistory.size();
525           InlineHistory.push_back(std::make_pair(Callee, InlineHistoryID));
526 
527           for (Value *Ptr : InlineInfo.InlinedCalls)
528             CallSites.push_back(std::make_pair(CallSite(Ptr), NewHistoryID));
529         }
530       }
531 
532       // If we inlined or deleted the last possible call site to the function,
533       // delete the function body now.
534       if (Callee && Callee->use_empty() && Callee->hasLocalLinkage() &&
535           // TODO: Can remove if in SCC now.
536           !SCCFunctions.count(Callee) &&
537 
538           // The function may be apparently dead, but if there are indirect
539           // callgraph references to the node, we cannot delete it yet, this
540           // could invalidate the CGSCC iterator.
541           CG[Callee]->getNumReferences() == 0) {
542         DEBUG(dbgs() << "    -> Deleting dead function: "
543               << Callee->getName() << "\n");
544         CallGraphNode *CalleeNode = CG[Callee];
545 
546         // Remove any call graph edges from the callee to its callees.
547         CalleeNode->removeAllCalledFunctions();
548 
549         // Removing the node for callee from the call graph and delete it.
550         delete CG.removeFunctionFromModule(CalleeNode);
551         ++NumDeleted;
552       }
553 
554       // Remove this call site from the list.  If possible, use
555       // swap/pop_back for efficiency, but do not use it if doing so would
556       // move a call site to a function in this SCC before the
557       // 'FirstCallInSCC' barrier.
558       if (SCC.isSingular()) {
559         CallSites[CSi] = CallSites.back();
560         CallSites.pop_back();
561       } else {
562         CallSites.erase(CallSites.begin()+CSi);
563       }
564       --CSi;
565 
566       Changed = true;
567       LocalChange = true;
568     }
569   } while (LocalChange);
570 
571   return Changed;
572 }
573 
574 bool Inliner::inlineCalls(CallGraphSCC &SCC) {
575   CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
576   ACT = &getAnalysis<AssumptionCacheTracker>();
577   PSI = getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(CG.getModule());
578   auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
579   // We compute dedicated AA results for each function in the SCC as needed. We
580   // use a lambda referencing external objects so that they live long enough to
581   // be queried, but we re-use them each time.
582   Optional<BasicAAResult> BAR;
583   Optional<AAResults> AAR;
584   auto AARGetter = [&](Function &F) -> AAResults & {
585     BAR.emplace(createLegacyPMBasicAAResult(*this, F));
586     AAR.emplace(createLegacyPMAAResults(*this, F, *BAR));
587     return *AAR;
588   };
589   auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
590     return ACT->getAssumptionCache(F);
591   };
592   return inlineCallsImpl(SCC, CG, GetAssumptionCache, PSI, TLI, InsertLifetime,
593                          [this](CallSite CS) { return getInlineCost(CS); },
594                          AARGetter);
595 }
596 
597 /// Remove now-dead linkonce functions at the end of
598 /// processing to avoid breaking the SCC traversal.
599 bool Inliner::doFinalization(CallGraph &CG) {
600   return removeDeadFunctions(CG);
601 }
602 
603 /// Remove dead functions that are not included in DNR (Do Not Remove) list.
604 bool Inliner::removeDeadFunctions(CallGraph &CG, bool AlwaysInlineOnly) {
605   SmallVector<CallGraphNode*, 16> FunctionsToRemove;
606   SmallVector<CallGraphNode *, 16> DeadFunctionsInComdats;
607   SmallDenseMap<const Comdat *, int, 16> ComdatEntriesAlive;
608 
609   auto RemoveCGN = [&](CallGraphNode *CGN) {
610     // Remove any call graph edges from the function to its callees.
611     CGN->removeAllCalledFunctions();
612 
613     // Remove any edges from the external node to the function's call graph
614     // node.  These edges might have been made irrelegant due to
615     // optimization of the program.
616     CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
617 
618     // Removing the node for callee from the call graph and delete it.
619     FunctionsToRemove.push_back(CGN);
620   };
621 
622   // Scan for all of the functions, looking for ones that should now be removed
623   // from the program.  Insert the dead ones in the FunctionsToRemove set.
624   for (const auto &I : CG) {
625     CallGraphNode *CGN = I.second.get();
626     Function *F = CGN->getFunction();
627     if (!F || F->isDeclaration())
628       continue;
629 
630     // Handle the case when this function is called and we only want to care
631     // about always-inline functions. This is a bit of a hack to share code
632     // between here and the InlineAlways pass.
633     if (AlwaysInlineOnly && !F->hasFnAttribute(Attribute::AlwaysInline))
634       continue;
635 
636     // If the only remaining users of the function are dead constants, remove
637     // them.
638     F->removeDeadConstantUsers();
639 
640     if (!F->isDefTriviallyDead())
641       continue;
642 
643     // It is unsafe to drop a function with discardable linkage from a COMDAT
644     // without also dropping the other members of the COMDAT.
645     // The inliner doesn't visit non-function entities which are in COMDAT
646     // groups so it is unsafe to do so *unless* the linkage is local.
647     if (!F->hasLocalLinkage()) {
648       if (const Comdat *C = F->getComdat()) {
649         --ComdatEntriesAlive[C];
650         DeadFunctionsInComdats.push_back(CGN);
651         continue;
652       }
653     }
654 
655     RemoveCGN(CGN);
656   }
657   if (!DeadFunctionsInComdats.empty()) {
658     // Count up all the entities in COMDAT groups
659     auto ComdatGroupReferenced = [&](const Comdat *C) {
660       auto I = ComdatEntriesAlive.find(C);
661       if (I != ComdatEntriesAlive.end())
662         ++(I->getSecond());
663     };
664     for (const Function &F : CG.getModule())
665       if (const Comdat *C = F.getComdat())
666         ComdatGroupReferenced(C);
667     for (const GlobalVariable &GV : CG.getModule().globals())
668       if (const Comdat *C = GV.getComdat())
669         ComdatGroupReferenced(C);
670     for (const GlobalAlias &GA : CG.getModule().aliases())
671       if (const Comdat *C = GA.getComdat())
672         ComdatGroupReferenced(C);
673     for (CallGraphNode *CGN : DeadFunctionsInComdats) {
674       Function *F = CGN->getFunction();
675       const Comdat *C = F->getComdat();
676       int NumAlive = ComdatEntriesAlive[C];
677       // We can remove functions in a COMDAT group if the entire group is dead.
678       assert(NumAlive >= 0);
679       if (NumAlive > 0)
680         continue;
681 
682       RemoveCGN(CGN);
683     }
684   }
685 
686   if (FunctionsToRemove.empty())
687     return false;
688 
689   // Now that we know which functions to delete, do so.  We didn't want to do
690   // this inline, because that would invalidate our CallGraph::iterator
691   // objects. :(
692   //
693   // Note that it doesn't matter that we are iterating over a non-stable order
694   // here to do this, it doesn't matter which order the functions are deleted
695   // in.
696   array_pod_sort(FunctionsToRemove.begin(), FunctionsToRemove.end());
697   FunctionsToRemove.erase(std::unique(FunctionsToRemove.begin(),
698                                       FunctionsToRemove.end()),
699                           FunctionsToRemove.end());
700   for (CallGraphNode *CGN : FunctionsToRemove) {
701     delete CG.removeFunctionFromModule(CGN);
702     ++NumDeleted;
703   }
704   return true;
705 }
706