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