xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/IPO/ModuleInliner.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
1*349cc55cSDimitry Andric //===- ModuleInliner.cpp - Code related to module inliner -----------------===//
2*349cc55cSDimitry Andric //
3*349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*349cc55cSDimitry Andric //
7*349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8*349cc55cSDimitry Andric //
9*349cc55cSDimitry Andric // This file implements the mechanics required to implement inlining without
10*349cc55cSDimitry Andric // missing any calls in the module level. It doesn't need any infromation about
11*349cc55cSDimitry Andric // SCC or call graph, which is different from the SCC inliner.  The decisions of
12*349cc55cSDimitry Andric // which calls are profitable to inline are implemented elsewhere.
13*349cc55cSDimitry Andric //
14*349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
15*349cc55cSDimitry Andric 
16*349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ModuleInliner.h"
17*349cc55cSDimitry Andric #include "llvm/ADT/DenseMap.h"
18*349cc55cSDimitry Andric #include "llvm/ADT/ScopeExit.h"
19*349cc55cSDimitry Andric #include "llvm/ADT/SetVector.h"
20*349cc55cSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
21*349cc55cSDimitry Andric #include "llvm/ADT/SmallVector.h"
22*349cc55cSDimitry Andric #include "llvm/ADT/Statistic.h"
23*349cc55cSDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
24*349cc55cSDimitry Andric #include "llvm/Analysis/BlockFrequencyInfo.h"
25*349cc55cSDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
26*349cc55cSDimitry Andric #include "llvm/Analysis/InlineAdvisor.h"
27*349cc55cSDimitry Andric #include "llvm/Analysis/InlineCost.h"
28*349cc55cSDimitry Andric #include "llvm/Analysis/InlineOrder.h"
29*349cc55cSDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h"
30*349cc55cSDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h"
31*349cc55cSDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
32*349cc55cSDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
33*349cc55cSDimitry Andric #include "llvm/IR/DebugLoc.h"
34*349cc55cSDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
35*349cc55cSDimitry Andric #include "llvm/IR/Function.h"
36*349cc55cSDimitry Andric #include "llvm/IR/InstIterator.h"
37*349cc55cSDimitry Andric #include "llvm/IR/Instruction.h"
38*349cc55cSDimitry Andric #include "llvm/IR/Instructions.h"
39*349cc55cSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
40*349cc55cSDimitry Andric #include "llvm/IR/Metadata.h"
41*349cc55cSDimitry Andric #include "llvm/IR/Module.h"
42*349cc55cSDimitry Andric #include "llvm/IR/PassManager.h"
43*349cc55cSDimitry Andric #include "llvm/IR/User.h"
44*349cc55cSDimitry Andric #include "llvm/IR/Value.h"
45*349cc55cSDimitry Andric #include "llvm/Support/CommandLine.h"
46*349cc55cSDimitry Andric #include "llvm/Support/Debug.h"
47*349cc55cSDimitry Andric #include "llvm/Support/raw_ostream.h"
48*349cc55cSDimitry Andric #include "llvm/Transforms/Utils/CallPromotionUtils.h"
49*349cc55cSDimitry Andric #include "llvm/Transforms/Utils/Cloning.h"
50*349cc55cSDimitry Andric #include "llvm/Transforms/Utils/Local.h"
51*349cc55cSDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
52*349cc55cSDimitry Andric #include <cassert>
53*349cc55cSDimitry Andric #include <functional>
54*349cc55cSDimitry Andric 
55*349cc55cSDimitry Andric using namespace llvm;
56*349cc55cSDimitry Andric 
57*349cc55cSDimitry Andric #define DEBUG_TYPE "module-inline"
58*349cc55cSDimitry Andric 
59*349cc55cSDimitry Andric STATISTIC(NumInlined, "Number of functions inlined");
60*349cc55cSDimitry Andric STATISTIC(NumDeleted, "Number of functions deleted because all callers found");
61*349cc55cSDimitry Andric 
62*349cc55cSDimitry Andric static cl::opt<bool> InlineEnablePriorityOrder(
63*349cc55cSDimitry Andric     "module-inline-enable-priority-order", cl::Hidden, cl::init(true),
64*349cc55cSDimitry Andric     cl::desc("Enable the priority inline order for the module inliner"));
65*349cc55cSDimitry Andric 
66*349cc55cSDimitry Andric /// Return true if the specified inline history ID
67*349cc55cSDimitry Andric /// indicates an inline history that includes the specified function.
68*349cc55cSDimitry Andric static bool inlineHistoryIncludes(
69*349cc55cSDimitry Andric     Function *F, int InlineHistoryID,
70*349cc55cSDimitry Andric     const SmallVectorImpl<std::pair<Function *, int>> &InlineHistory) {
71*349cc55cSDimitry Andric   while (InlineHistoryID != -1) {
72*349cc55cSDimitry Andric     assert(unsigned(InlineHistoryID) < InlineHistory.size() &&
73*349cc55cSDimitry Andric            "Invalid inline history ID");
74*349cc55cSDimitry Andric     if (InlineHistory[InlineHistoryID].first == F)
75*349cc55cSDimitry Andric       return true;
76*349cc55cSDimitry Andric     InlineHistoryID = InlineHistory[InlineHistoryID].second;
77*349cc55cSDimitry Andric   }
78*349cc55cSDimitry Andric   return false;
79*349cc55cSDimitry Andric }
80*349cc55cSDimitry Andric 
81*349cc55cSDimitry Andric InlineAdvisor &ModuleInlinerPass::getAdvisor(const ModuleAnalysisManager &MAM,
82*349cc55cSDimitry Andric                                              FunctionAnalysisManager &FAM,
83*349cc55cSDimitry Andric                                              Module &M) {
84*349cc55cSDimitry Andric   if (OwnedAdvisor)
85*349cc55cSDimitry Andric     return *OwnedAdvisor;
86*349cc55cSDimitry Andric 
87*349cc55cSDimitry Andric   auto *IAA = MAM.getCachedResult<InlineAdvisorAnalysis>(M);
88*349cc55cSDimitry Andric   if (!IAA) {
89*349cc55cSDimitry Andric     // It should still be possible to run the inliner as a stand-alone module
90*349cc55cSDimitry Andric     // pass, for test scenarios. In that case, we default to the
91*349cc55cSDimitry Andric     // DefaultInlineAdvisor, which doesn't need to keep state between module
92*349cc55cSDimitry Andric     // pass runs. It also uses just the default InlineParams. In this case, we
93*349cc55cSDimitry Andric     // need to use the provided FAM, which is valid for the duration of the
94*349cc55cSDimitry Andric     // inliner pass, and thus the lifetime of the owned advisor. The one we
95*349cc55cSDimitry Andric     // would get from the MAM can be invalidated as a result of the inliner's
96*349cc55cSDimitry Andric     // activity.
97*349cc55cSDimitry Andric     OwnedAdvisor = std::make_unique<DefaultInlineAdvisor>(M, FAM, Params);
98*349cc55cSDimitry Andric 
99*349cc55cSDimitry Andric     return *OwnedAdvisor;
100*349cc55cSDimitry Andric   }
101*349cc55cSDimitry Andric   assert(IAA->getAdvisor() &&
102*349cc55cSDimitry Andric          "Expected a present InlineAdvisorAnalysis also have an "
103*349cc55cSDimitry Andric          "InlineAdvisor initialized");
104*349cc55cSDimitry Andric   return *IAA->getAdvisor();
105*349cc55cSDimitry Andric }
106*349cc55cSDimitry Andric 
107*349cc55cSDimitry Andric static bool isKnownLibFunction(Function &F, TargetLibraryInfo &TLI) {
108*349cc55cSDimitry Andric   LibFunc LF;
109*349cc55cSDimitry Andric 
110*349cc55cSDimitry Andric   // Either this is a normal library function or a "vectorizable"
111*349cc55cSDimitry Andric   // function.  Not using the VFDatabase here because this query
112*349cc55cSDimitry Andric   // is related only to libraries handled via the TLI.
113*349cc55cSDimitry Andric   return TLI.getLibFunc(F, LF) ||
114*349cc55cSDimitry Andric          TLI.isKnownVectorFunctionInLibrary(F.getName());
115*349cc55cSDimitry Andric }
116*349cc55cSDimitry Andric 
117*349cc55cSDimitry Andric PreservedAnalyses ModuleInlinerPass::run(Module &M,
118*349cc55cSDimitry Andric                                          ModuleAnalysisManager &MAM) {
119*349cc55cSDimitry Andric   LLVM_DEBUG(dbgs() << "---- Module Inliner is Running ---- \n");
120*349cc55cSDimitry Andric 
121*349cc55cSDimitry Andric   auto &IAA = MAM.getResult<InlineAdvisorAnalysis>(M);
122*349cc55cSDimitry Andric   if (!IAA.tryCreate(Params, Mode, {})) {
123*349cc55cSDimitry Andric     M.getContext().emitError(
124*349cc55cSDimitry Andric         "Could not setup Inlining Advisor for the requested "
125*349cc55cSDimitry Andric         "mode and/or options");
126*349cc55cSDimitry Andric     return PreservedAnalyses::all();
127*349cc55cSDimitry Andric   }
128*349cc55cSDimitry Andric 
129*349cc55cSDimitry Andric   bool Changed = false;
130*349cc55cSDimitry Andric 
131*349cc55cSDimitry Andric   ProfileSummaryInfo *PSI = MAM.getCachedResult<ProfileSummaryAnalysis>(M);
132*349cc55cSDimitry Andric 
133*349cc55cSDimitry Andric   FunctionAnalysisManager &FAM =
134*349cc55cSDimitry Andric       MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
135*349cc55cSDimitry Andric 
136*349cc55cSDimitry Andric   auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
137*349cc55cSDimitry Andric     return FAM.getResult<TargetLibraryAnalysis>(F);
138*349cc55cSDimitry Andric   };
139*349cc55cSDimitry Andric 
140*349cc55cSDimitry Andric   InlineAdvisor &Advisor = getAdvisor(MAM, FAM, M);
141*349cc55cSDimitry Andric   Advisor.onPassEntry();
142*349cc55cSDimitry Andric 
143*349cc55cSDimitry Andric   auto AdvisorOnExit = make_scope_exit([&] { Advisor.onPassExit(); });
144*349cc55cSDimitry Andric 
145*349cc55cSDimitry Andric   // In the module inliner, a priority-based worklist is used for calls across
146*349cc55cSDimitry Andric   // the entire Module. With this module inliner, the inline order is not
147*349cc55cSDimitry Andric   // limited to bottom-up order. More globally scope inline order is enabled.
148*349cc55cSDimitry Andric   // Also, the inline deferral logic become unnecessary in this module inliner.
149*349cc55cSDimitry Andric   // It is possible to use other priority heuristics, e.g. profile-based
150*349cc55cSDimitry Andric   // heuristic.
151*349cc55cSDimitry Andric   //
152*349cc55cSDimitry Andric   // TODO: Here is a huge amount duplicate code between the module inliner and
153*349cc55cSDimitry Andric   // the SCC inliner, which need some refactoring.
154*349cc55cSDimitry Andric   std::unique_ptr<InlineOrder<std::pair<CallBase *, int>>> Calls;
155*349cc55cSDimitry Andric   if (InlineEnablePriorityOrder)
156*349cc55cSDimitry Andric     Calls = std::make_unique<PriorityInlineOrder<InlineSizePriority>>();
157*349cc55cSDimitry Andric   else
158*349cc55cSDimitry Andric     Calls = std::make_unique<DefaultInlineOrder<std::pair<CallBase *, int>>>();
159*349cc55cSDimitry Andric   assert(Calls != nullptr && "Expected an initialized InlineOrder");
160*349cc55cSDimitry Andric 
161*349cc55cSDimitry Andric   // Populate the initial list of calls in this module.
162*349cc55cSDimitry Andric   for (Function &F : M) {
163*349cc55cSDimitry Andric     auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
164*349cc55cSDimitry Andric     // We want to generally process call sites top-down in order for
165*349cc55cSDimitry Andric     // simplifications stemming from replacing the call with the returned value
166*349cc55cSDimitry Andric     // after inlining to be visible to subsequent inlining decisions.
167*349cc55cSDimitry Andric     // FIXME: Using instructions sequence is a really bad way to do this.
168*349cc55cSDimitry Andric     // Instead we should do an actual RPO walk of the function body.
169*349cc55cSDimitry Andric     for (Instruction &I : instructions(F))
170*349cc55cSDimitry Andric       if (auto *CB = dyn_cast<CallBase>(&I))
171*349cc55cSDimitry Andric         if (Function *Callee = CB->getCalledFunction()) {
172*349cc55cSDimitry Andric           if (!Callee->isDeclaration())
173*349cc55cSDimitry Andric             Calls->push({CB, -1});
174*349cc55cSDimitry Andric           else if (!isa<IntrinsicInst>(I)) {
175*349cc55cSDimitry Andric             using namespace ore;
176*349cc55cSDimitry Andric             setInlineRemark(*CB, "unavailable definition");
177*349cc55cSDimitry Andric             ORE.emit([&]() {
178*349cc55cSDimitry Andric               return OptimizationRemarkMissed(DEBUG_TYPE, "NoDefinition", &I)
179*349cc55cSDimitry Andric                      << NV("Callee", Callee) << " will not be inlined into "
180*349cc55cSDimitry Andric                      << NV("Caller", CB->getCaller())
181*349cc55cSDimitry Andric                      << " because its definition is unavailable"
182*349cc55cSDimitry Andric                      << setIsVerbose();
183*349cc55cSDimitry Andric             });
184*349cc55cSDimitry Andric           }
185*349cc55cSDimitry Andric         }
186*349cc55cSDimitry Andric   }
187*349cc55cSDimitry Andric   if (Calls->empty())
188*349cc55cSDimitry Andric     return PreservedAnalyses::all();
189*349cc55cSDimitry Andric 
190*349cc55cSDimitry Andric   // When inlining a callee produces new call sites, we want to keep track of
191*349cc55cSDimitry Andric   // the fact that they were inlined from the callee.  This allows us to avoid
192*349cc55cSDimitry Andric   // infinite inlining in some obscure cases.  To represent this, we use an
193*349cc55cSDimitry Andric   // index into the InlineHistory vector.
194*349cc55cSDimitry Andric   SmallVector<std::pair<Function *, int>, 16> InlineHistory;
195*349cc55cSDimitry Andric 
196*349cc55cSDimitry Andric   // Track a set vector of inlined callees so that we can augment the caller
197*349cc55cSDimitry Andric   // with all of their edges in the call graph before pruning out the ones that
198*349cc55cSDimitry Andric   // got simplified away.
199*349cc55cSDimitry Andric   SmallSetVector<Function *, 4> InlinedCallees;
200*349cc55cSDimitry Andric 
201*349cc55cSDimitry Andric   // Track the dead functions to delete once finished with inlining calls. We
202*349cc55cSDimitry Andric   // defer deleting these to make it easier to handle the call graph updates.
203*349cc55cSDimitry Andric   SmallVector<Function *, 4> DeadFunctions;
204*349cc55cSDimitry Andric 
205*349cc55cSDimitry Andric   // Loop forward over all of the calls.
206*349cc55cSDimitry Andric   while (!Calls->empty()) {
207*349cc55cSDimitry Andric     // We expect the calls to typically be batched with sequences of calls that
208*349cc55cSDimitry Andric     // have the same caller, so we first set up some shared infrastructure for
209*349cc55cSDimitry Andric     // this caller. We also do any pruning we can at this layer on the caller
210*349cc55cSDimitry Andric     // alone.
211*349cc55cSDimitry Andric     Function &F = *Calls->front().first->getCaller();
212*349cc55cSDimitry Andric 
213*349cc55cSDimitry Andric     LLVM_DEBUG(dbgs() << "Inlining calls in: " << F.getName() << "\n"
214*349cc55cSDimitry Andric                       << "    Function size: " << F.getInstructionCount()
215*349cc55cSDimitry Andric                       << "\n");
216*349cc55cSDimitry Andric 
217*349cc55cSDimitry Andric     auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
218*349cc55cSDimitry Andric       return FAM.getResult<AssumptionAnalysis>(F);
219*349cc55cSDimitry Andric     };
220*349cc55cSDimitry Andric 
221*349cc55cSDimitry Andric     // Now process as many calls as we have within this caller in the sequence.
222*349cc55cSDimitry Andric     // We bail out as soon as the caller has to change so we can
223*349cc55cSDimitry Andric     // prepare the context of that new caller.
224*349cc55cSDimitry Andric     bool DidInline = false;
225*349cc55cSDimitry Andric     while (!Calls->empty() && Calls->front().first->getCaller() == &F) {
226*349cc55cSDimitry Andric       auto P = Calls->pop();
227*349cc55cSDimitry Andric       CallBase *CB = P.first;
228*349cc55cSDimitry Andric       const int InlineHistoryID = P.second;
229*349cc55cSDimitry Andric       Function &Callee = *CB->getCalledFunction();
230*349cc55cSDimitry Andric 
231*349cc55cSDimitry Andric       if (InlineHistoryID != -1 &&
232*349cc55cSDimitry Andric           inlineHistoryIncludes(&Callee, InlineHistoryID, InlineHistory)) {
233*349cc55cSDimitry Andric         setInlineRemark(*CB, "recursive");
234*349cc55cSDimitry Andric         continue;
235*349cc55cSDimitry Andric       }
236*349cc55cSDimitry Andric 
237*349cc55cSDimitry Andric       auto Advice = Advisor.getAdvice(*CB, /*OnlyMandatory*/ false);
238*349cc55cSDimitry Andric       // Check whether we want to inline this callsite.
239*349cc55cSDimitry Andric       if (!Advice->isInliningRecommended()) {
240*349cc55cSDimitry Andric         Advice->recordUnattemptedInlining();
241*349cc55cSDimitry Andric         continue;
242*349cc55cSDimitry Andric       }
243*349cc55cSDimitry Andric 
244*349cc55cSDimitry Andric       // Setup the data structure used to plumb customization into the
245*349cc55cSDimitry Andric       // `InlineFunction` routine.
246*349cc55cSDimitry Andric       InlineFunctionInfo IFI(
247*349cc55cSDimitry Andric           /*cg=*/nullptr, GetAssumptionCache, PSI,
248*349cc55cSDimitry Andric           &FAM.getResult<BlockFrequencyAnalysis>(*(CB->getCaller())),
249*349cc55cSDimitry Andric           &FAM.getResult<BlockFrequencyAnalysis>(Callee));
250*349cc55cSDimitry Andric 
251*349cc55cSDimitry Andric       InlineResult IR =
252*349cc55cSDimitry Andric           InlineFunction(*CB, IFI, &FAM.getResult<AAManager>(*CB->getCaller()));
253*349cc55cSDimitry Andric       if (!IR.isSuccess()) {
254*349cc55cSDimitry Andric         Advice->recordUnsuccessfulInlining(IR);
255*349cc55cSDimitry Andric         continue;
256*349cc55cSDimitry Andric       }
257*349cc55cSDimitry Andric 
258*349cc55cSDimitry Andric       DidInline = true;
259*349cc55cSDimitry Andric       InlinedCallees.insert(&Callee);
260*349cc55cSDimitry Andric       ++NumInlined;
261*349cc55cSDimitry Andric 
262*349cc55cSDimitry Andric       LLVM_DEBUG(dbgs() << "    Size after inlining: "
263*349cc55cSDimitry Andric                         << F.getInstructionCount() << "\n");
264*349cc55cSDimitry Andric 
265*349cc55cSDimitry Andric       // Add any new callsites to defined functions to the worklist.
266*349cc55cSDimitry Andric       if (!IFI.InlinedCallSites.empty()) {
267*349cc55cSDimitry Andric         int NewHistoryID = InlineHistory.size();
268*349cc55cSDimitry Andric         InlineHistory.push_back({&Callee, InlineHistoryID});
269*349cc55cSDimitry Andric 
270*349cc55cSDimitry Andric         for (CallBase *ICB : reverse(IFI.InlinedCallSites)) {
271*349cc55cSDimitry Andric           Function *NewCallee = ICB->getCalledFunction();
272*349cc55cSDimitry Andric           if (!NewCallee) {
273*349cc55cSDimitry Andric             // Try to promote an indirect (virtual) call without waiting for
274*349cc55cSDimitry Andric             // the post-inline cleanup and the next DevirtSCCRepeatedPass
275*349cc55cSDimitry Andric             // iteration because the next iteration may not happen and we may
276*349cc55cSDimitry Andric             // miss inlining it.
277*349cc55cSDimitry Andric             if (tryPromoteCall(*ICB))
278*349cc55cSDimitry Andric               NewCallee = ICB->getCalledFunction();
279*349cc55cSDimitry Andric           }
280*349cc55cSDimitry Andric           if (NewCallee)
281*349cc55cSDimitry Andric             if (!NewCallee->isDeclaration())
282*349cc55cSDimitry Andric               Calls->push({ICB, NewHistoryID});
283*349cc55cSDimitry Andric         }
284*349cc55cSDimitry Andric       }
285*349cc55cSDimitry Andric 
286*349cc55cSDimitry Andric       // Merge the attributes based on the inlining.
287*349cc55cSDimitry Andric       AttributeFuncs::mergeAttributesForInlining(F, Callee);
288*349cc55cSDimitry Andric 
289*349cc55cSDimitry Andric       // For local functions, check whether this makes the callee trivially
290*349cc55cSDimitry Andric       // dead. In that case, we can drop the body of the function eagerly
291*349cc55cSDimitry Andric       // which may reduce the number of callers of other functions to one,
292*349cc55cSDimitry Andric       // changing inline cost thresholds.
293*349cc55cSDimitry Andric       bool CalleeWasDeleted = false;
294*349cc55cSDimitry Andric       if (Callee.hasLocalLinkage()) {
295*349cc55cSDimitry Andric         // To check this we also need to nuke any dead constant uses (perhaps
296*349cc55cSDimitry Andric         // made dead by this operation on other functions).
297*349cc55cSDimitry Andric         Callee.removeDeadConstantUsers();
298*349cc55cSDimitry Andric         // if (Callee.use_empty() && !CG.isLibFunction(Callee)) {
299*349cc55cSDimitry Andric         if (Callee.use_empty() && !isKnownLibFunction(Callee, GetTLI(Callee))) {
300*349cc55cSDimitry Andric           Calls->erase_if([&](const std::pair<CallBase *, int> &Call) {
301*349cc55cSDimitry Andric             return Call.first->getCaller() == &Callee;
302*349cc55cSDimitry Andric           });
303*349cc55cSDimitry Andric           // Clear the body and queue the function itself for deletion when we
304*349cc55cSDimitry Andric           // finish inlining.
305*349cc55cSDimitry Andric           // Note that after this point, it is an error to do anything other
306*349cc55cSDimitry Andric           // than use the callee's address or delete it.
307*349cc55cSDimitry Andric           Callee.dropAllReferences();
308*349cc55cSDimitry Andric           assert(!is_contained(DeadFunctions, &Callee) &&
309*349cc55cSDimitry Andric                  "Cannot put cause a function to become dead twice!");
310*349cc55cSDimitry Andric           DeadFunctions.push_back(&Callee);
311*349cc55cSDimitry Andric           CalleeWasDeleted = true;
312*349cc55cSDimitry Andric         }
313*349cc55cSDimitry Andric       }
314*349cc55cSDimitry Andric       if (CalleeWasDeleted)
315*349cc55cSDimitry Andric         Advice->recordInliningWithCalleeDeleted();
316*349cc55cSDimitry Andric       else
317*349cc55cSDimitry Andric         Advice->recordInlining();
318*349cc55cSDimitry Andric     }
319*349cc55cSDimitry Andric 
320*349cc55cSDimitry Andric     if (!DidInline)
321*349cc55cSDimitry Andric       continue;
322*349cc55cSDimitry Andric     Changed = true;
323*349cc55cSDimitry Andric 
324*349cc55cSDimitry Andric     InlinedCallees.clear();
325*349cc55cSDimitry Andric   }
326*349cc55cSDimitry Andric 
327*349cc55cSDimitry Andric   // Now that we've finished inlining all of the calls across this module,
328*349cc55cSDimitry Andric   // delete all of the trivially dead functions.
329*349cc55cSDimitry Andric   //
330*349cc55cSDimitry Andric   // Note that this walks a pointer set which has non-deterministic order but
331*349cc55cSDimitry Andric   // that is OK as all we do is delete things and add pointers to unordered
332*349cc55cSDimitry Andric   // sets.
333*349cc55cSDimitry Andric   for (Function *DeadF : DeadFunctions) {
334*349cc55cSDimitry Andric     // Clear out any cached analyses.
335*349cc55cSDimitry Andric     FAM.clear(*DeadF, DeadF->getName());
336*349cc55cSDimitry Andric 
337*349cc55cSDimitry Andric     // And delete the actual function from the module.
338*349cc55cSDimitry Andric     // The Advisor may use Function pointers to efficiently index various
339*349cc55cSDimitry Andric     // internal maps, e.g. for memoization. Function cleanup passes like
340*349cc55cSDimitry Andric     // argument promotion create new functions. It is possible for a new
341*349cc55cSDimitry Andric     // function to be allocated at the address of a deleted function. We could
342*349cc55cSDimitry Andric     // index using names, but that's inefficient. Alternatively, we let the
343*349cc55cSDimitry Andric     // Advisor free the functions when it sees fit.
344*349cc55cSDimitry Andric     DeadF->getBasicBlockList().clear();
345*349cc55cSDimitry Andric     M.getFunctionList().remove(DeadF);
346*349cc55cSDimitry Andric 
347*349cc55cSDimitry Andric     ++NumDeleted;
348*349cc55cSDimitry Andric   }
349*349cc55cSDimitry Andric 
350*349cc55cSDimitry Andric   if (!Changed)
351*349cc55cSDimitry Andric     return PreservedAnalyses::all();
352*349cc55cSDimitry Andric 
353*349cc55cSDimitry Andric   return PreservedAnalyses::none();
354*349cc55cSDimitry Andric }
355