xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/IPO/ModuleInliner.cpp (revision 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623)
1349cc55cSDimitry Andric //===- ModuleInliner.cpp - Code related to module inliner -----------------===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8349cc55cSDimitry Andric //
9349cc55cSDimitry Andric // This file implements the mechanics required to implement inlining without
10349cc55cSDimitry Andric // missing any calls in the module level. It doesn't need any infromation about
11349cc55cSDimitry Andric // SCC or call graph, which is different from the SCC inliner.  The decisions of
12349cc55cSDimitry Andric // which calls are profitable to inline are implemented elsewhere.
13349cc55cSDimitry Andric //
14349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
15349cc55cSDimitry Andric 
16349cc55cSDimitry Andric #include "llvm/Transforms/IPO/ModuleInliner.h"
17349cc55cSDimitry Andric #include "llvm/ADT/DenseMap.h"
18349cc55cSDimitry Andric #include "llvm/ADT/ScopeExit.h"
19349cc55cSDimitry Andric #include "llvm/ADT/SetVector.h"
20349cc55cSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
21349cc55cSDimitry Andric #include "llvm/ADT/SmallVector.h"
22349cc55cSDimitry Andric #include "llvm/ADT/Statistic.h"
23349cc55cSDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
24349cc55cSDimitry Andric #include "llvm/Analysis/BlockFrequencyInfo.h"
25349cc55cSDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
26349cc55cSDimitry Andric #include "llvm/Analysis/InlineAdvisor.h"
27349cc55cSDimitry Andric #include "llvm/Analysis/InlineCost.h"
28349cc55cSDimitry Andric #include "llvm/Analysis/InlineOrder.h"
29349cc55cSDimitry Andric #include "llvm/Analysis/OptimizationRemarkEmitter.h"
30349cc55cSDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h"
31349cc55cSDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
32349cc55cSDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
33349cc55cSDimitry Andric #include "llvm/IR/DebugLoc.h"
34349cc55cSDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
35349cc55cSDimitry Andric #include "llvm/IR/Function.h"
36349cc55cSDimitry Andric #include "llvm/IR/InstIterator.h"
37349cc55cSDimitry Andric #include "llvm/IR/Instruction.h"
38349cc55cSDimitry Andric #include "llvm/IR/Instructions.h"
39349cc55cSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
40349cc55cSDimitry Andric #include "llvm/IR/Metadata.h"
41349cc55cSDimitry Andric #include "llvm/IR/Module.h"
42349cc55cSDimitry Andric #include "llvm/IR/PassManager.h"
43349cc55cSDimitry Andric #include "llvm/IR/User.h"
44349cc55cSDimitry Andric #include "llvm/IR/Value.h"
45349cc55cSDimitry Andric #include "llvm/Support/CommandLine.h"
46349cc55cSDimitry Andric #include "llvm/Support/Debug.h"
47349cc55cSDimitry Andric #include "llvm/Support/raw_ostream.h"
48349cc55cSDimitry Andric #include "llvm/Transforms/Utils/CallPromotionUtils.h"
49349cc55cSDimitry Andric #include "llvm/Transforms/Utils/Cloning.h"
50349cc55cSDimitry Andric #include "llvm/Transforms/Utils/Local.h"
51349cc55cSDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
52349cc55cSDimitry Andric #include <cassert>
53349cc55cSDimitry Andric #include <functional>
54349cc55cSDimitry Andric 
55349cc55cSDimitry Andric using namespace llvm;
56349cc55cSDimitry Andric 
57349cc55cSDimitry Andric #define DEBUG_TYPE "module-inline"
58349cc55cSDimitry Andric 
59349cc55cSDimitry Andric STATISTIC(NumInlined, "Number of functions inlined");
60349cc55cSDimitry Andric STATISTIC(NumDeleted, "Number of functions deleted because all callers found");
61349cc55cSDimitry Andric 
62349cc55cSDimitry Andric static cl::opt<bool> InlineEnablePriorityOrder(
63349cc55cSDimitry Andric     "module-inline-enable-priority-order", cl::Hidden, cl::init(true),
64349cc55cSDimitry Andric     cl::desc("Enable the priority inline order for the module inliner"));
65349cc55cSDimitry Andric 
66349cc55cSDimitry Andric /// Return true if the specified inline history ID
67349cc55cSDimitry Andric /// indicates an inline history that includes the specified function.
68349cc55cSDimitry Andric static bool inlineHistoryIncludes(
69349cc55cSDimitry Andric     Function *F, int InlineHistoryID,
70349cc55cSDimitry Andric     const SmallVectorImpl<std::pair<Function *, int>> &InlineHistory) {
71349cc55cSDimitry Andric   while (InlineHistoryID != -1) {
72349cc55cSDimitry Andric     assert(unsigned(InlineHistoryID) < InlineHistory.size() &&
73349cc55cSDimitry Andric            "Invalid inline history ID");
74349cc55cSDimitry Andric     if (InlineHistory[InlineHistoryID].first == F)
75349cc55cSDimitry Andric       return true;
76349cc55cSDimitry Andric     InlineHistoryID = InlineHistory[InlineHistoryID].second;
77349cc55cSDimitry Andric   }
78349cc55cSDimitry Andric   return false;
79349cc55cSDimitry Andric }
80349cc55cSDimitry Andric 
81349cc55cSDimitry Andric InlineAdvisor &ModuleInlinerPass::getAdvisor(const ModuleAnalysisManager &MAM,
82349cc55cSDimitry Andric                                              FunctionAnalysisManager &FAM,
83349cc55cSDimitry Andric                                              Module &M) {
84349cc55cSDimitry Andric   if (OwnedAdvisor)
85349cc55cSDimitry Andric     return *OwnedAdvisor;
86349cc55cSDimitry Andric 
87349cc55cSDimitry Andric   auto *IAA = MAM.getCachedResult<InlineAdvisorAnalysis>(M);
88349cc55cSDimitry Andric   if (!IAA) {
89349cc55cSDimitry Andric     // It should still be possible to run the inliner as a stand-alone module
90349cc55cSDimitry Andric     // pass, for test scenarios. In that case, we default to the
91349cc55cSDimitry Andric     // DefaultInlineAdvisor, which doesn't need to keep state between module
92349cc55cSDimitry Andric     // pass runs. It also uses just the default InlineParams. In this case, we
93349cc55cSDimitry Andric     // need to use the provided FAM, which is valid for the duration of the
94349cc55cSDimitry Andric     // inliner pass, and thus the lifetime of the owned advisor. The one we
95349cc55cSDimitry Andric     // would get from the MAM can be invalidated as a result of the inliner's
96349cc55cSDimitry Andric     // activity.
97349cc55cSDimitry Andric     OwnedAdvisor = std::make_unique<DefaultInlineAdvisor>(M, FAM, Params);
98349cc55cSDimitry Andric 
99349cc55cSDimitry Andric     return *OwnedAdvisor;
100349cc55cSDimitry Andric   }
101349cc55cSDimitry Andric   assert(IAA->getAdvisor() &&
102349cc55cSDimitry Andric          "Expected a present InlineAdvisorAnalysis also have an "
103349cc55cSDimitry Andric          "InlineAdvisor initialized");
104349cc55cSDimitry Andric   return *IAA->getAdvisor();
105349cc55cSDimitry Andric }
106349cc55cSDimitry Andric 
107349cc55cSDimitry Andric static bool isKnownLibFunction(Function &F, TargetLibraryInfo &TLI) {
108349cc55cSDimitry Andric   LibFunc LF;
109349cc55cSDimitry Andric 
110349cc55cSDimitry Andric   // Either this is a normal library function or a "vectorizable"
111349cc55cSDimitry Andric   // function.  Not using the VFDatabase here because this query
112349cc55cSDimitry Andric   // is related only to libraries handled via the TLI.
113349cc55cSDimitry Andric   return TLI.getLibFunc(F, LF) ||
114349cc55cSDimitry Andric          TLI.isKnownVectorFunctionInLibrary(F.getName());
115349cc55cSDimitry Andric }
116349cc55cSDimitry Andric 
117349cc55cSDimitry Andric PreservedAnalyses ModuleInlinerPass::run(Module &M,
118349cc55cSDimitry Andric                                          ModuleAnalysisManager &MAM) {
119349cc55cSDimitry Andric   LLVM_DEBUG(dbgs() << "---- Module Inliner is Running ---- \n");
120349cc55cSDimitry Andric 
121349cc55cSDimitry Andric   auto &IAA = MAM.getResult<InlineAdvisorAnalysis>(M);
122349cc55cSDimitry Andric   if (!IAA.tryCreate(Params, Mode, {})) {
123349cc55cSDimitry Andric     M.getContext().emitError(
124349cc55cSDimitry Andric         "Could not setup Inlining Advisor for the requested "
125349cc55cSDimitry Andric         "mode and/or options");
126349cc55cSDimitry Andric     return PreservedAnalyses::all();
127349cc55cSDimitry Andric   }
128349cc55cSDimitry Andric 
129349cc55cSDimitry Andric   bool Changed = false;
130349cc55cSDimitry Andric 
131349cc55cSDimitry Andric   ProfileSummaryInfo *PSI = MAM.getCachedResult<ProfileSummaryAnalysis>(M);
132349cc55cSDimitry Andric 
133349cc55cSDimitry Andric   FunctionAnalysisManager &FAM =
134349cc55cSDimitry Andric       MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
135349cc55cSDimitry Andric 
136349cc55cSDimitry Andric   auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
137349cc55cSDimitry Andric     return FAM.getResult<TargetLibraryAnalysis>(F);
138349cc55cSDimitry Andric   };
139349cc55cSDimitry Andric 
140349cc55cSDimitry Andric   InlineAdvisor &Advisor = getAdvisor(MAM, FAM, M);
141349cc55cSDimitry Andric   Advisor.onPassEntry();
142349cc55cSDimitry Andric 
143349cc55cSDimitry Andric   auto AdvisorOnExit = make_scope_exit([&] { Advisor.onPassExit(); });
144349cc55cSDimitry Andric 
145349cc55cSDimitry Andric   // In the module inliner, a priority-based worklist is used for calls across
146349cc55cSDimitry Andric   // the entire Module. With this module inliner, the inline order is not
147349cc55cSDimitry Andric   // limited to bottom-up order. More globally scope inline order is enabled.
148349cc55cSDimitry Andric   // Also, the inline deferral logic become unnecessary in this module inliner.
149349cc55cSDimitry Andric   // It is possible to use other priority heuristics, e.g. profile-based
150349cc55cSDimitry Andric   // heuristic.
151349cc55cSDimitry Andric   //
152349cc55cSDimitry Andric   // TODO: Here is a huge amount duplicate code between the module inliner and
153349cc55cSDimitry Andric   // the SCC inliner, which need some refactoring.
154349cc55cSDimitry Andric   std::unique_ptr<InlineOrder<std::pair<CallBase *, int>>> Calls;
155349cc55cSDimitry Andric   if (InlineEnablePriorityOrder)
156349cc55cSDimitry Andric     Calls = std::make_unique<PriorityInlineOrder<InlineSizePriority>>();
157349cc55cSDimitry Andric   else
158349cc55cSDimitry Andric     Calls = std::make_unique<DefaultInlineOrder<std::pair<CallBase *, int>>>();
159349cc55cSDimitry Andric   assert(Calls != nullptr && "Expected an initialized InlineOrder");
160349cc55cSDimitry Andric 
161349cc55cSDimitry Andric   // Populate the initial list of calls in this module.
162349cc55cSDimitry Andric   for (Function &F : M) {
163349cc55cSDimitry Andric     auto &ORE = FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
164349cc55cSDimitry Andric     // We want to generally process call sites top-down in order for
165349cc55cSDimitry Andric     // simplifications stemming from replacing the call with the returned value
166349cc55cSDimitry Andric     // after inlining to be visible to subsequent inlining decisions.
167349cc55cSDimitry Andric     // FIXME: Using instructions sequence is a really bad way to do this.
168349cc55cSDimitry Andric     // Instead we should do an actual RPO walk of the function body.
169349cc55cSDimitry Andric     for (Instruction &I : instructions(F))
170349cc55cSDimitry Andric       if (auto *CB = dyn_cast<CallBase>(&I))
171349cc55cSDimitry Andric         if (Function *Callee = CB->getCalledFunction()) {
172349cc55cSDimitry Andric           if (!Callee->isDeclaration())
173349cc55cSDimitry Andric             Calls->push({CB, -1});
174349cc55cSDimitry Andric           else if (!isa<IntrinsicInst>(I)) {
175349cc55cSDimitry Andric             using namespace ore;
176349cc55cSDimitry Andric             setInlineRemark(*CB, "unavailable definition");
177349cc55cSDimitry Andric             ORE.emit([&]() {
178349cc55cSDimitry Andric               return OptimizationRemarkMissed(DEBUG_TYPE, "NoDefinition", &I)
179349cc55cSDimitry Andric                      << NV("Callee", Callee) << " will not be inlined into "
180349cc55cSDimitry Andric                      << NV("Caller", CB->getCaller())
181349cc55cSDimitry Andric                      << " because its definition is unavailable"
182349cc55cSDimitry Andric                      << setIsVerbose();
183349cc55cSDimitry Andric             });
184349cc55cSDimitry Andric           }
185349cc55cSDimitry Andric         }
186349cc55cSDimitry Andric   }
187349cc55cSDimitry Andric   if (Calls->empty())
188349cc55cSDimitry Andric     return PreservedAnalyses::all();
189349cc55cSDimitry Andric 
190349cc55cSDimitry Andric   // When inlining a callee produces new call sites, we want to keep track of
191349cc55cSDimitry Andric   // the fact that they were inlined from the callee.  This allows us to avoid
192349cc55cSDimitry Andric   // infinite inlining in some obscure cases.  To represent this, we use an
193349cc55cSDimitry Andric   // index into the InlineHistory vector.
194349cc55cSDimitry Andric   SmallVector<std::pair<Function *, int>, 16> InlineHistory;
195349cc55cSDimitry Andric 
196349cc55cSDimitry Andric   // Track a set vector of inlined callees so that we can augment the caller
197349cc55cSDimitry Andric   // with all of their edges in the call graph before pruning out the ones that
198349cc55cSDimitry Andric   // got simplified away.
199349cc55cSDimitry Andric   SmallSetVector<Function *, 4> InlinedCallees;
200349cc55cSDimitry Andric 
201349cc55cSDimitry Andric   // Track the dead functions to delete once finished with inlining calls. We
202349cc55cSDimitry Andric   // defer deleting these to make it easier to handle the call graph updates.
203349cc55cSDimitry Andric   SmallVector<Function *, 4> DeadFunctions;
204349cc55cSDimitry Andric 
205349cc55cSDimitry Andric   // Loop forward over all of the calls.
206349cc55cSDimitry Andric   while (!Calls->empty()) {
207349cc55cSDimitry Andric     // We expect the calls to typically be batched with sequences of calls that
208349cc55cSDimitry Andric     // have the same caller, so we first set up some shared infrastructure for
209349cc55cSDimitry Andric     // this caller. We also do any pruning we can at this layer on the caller
210349cc55cSDimitry Andric     // alone.
211349cc55cSDimitry Andric     Function &F = *Calls->front().first->getCaller();
212349cc55cSDimitry Andric 
213349cc55cSDimitry Andric     LLVM_DEBUG(dbgs() << "Inlining calls in: " << F.getName() << "\n"
214349cc55cSDimitry Andric                       << "    Function size: " << F.getInstructionCount()
215349cc55cSDimitry Andric                       << "\n");
216349cc55cSDimitry Andric 
217349cc55cSDimitry Andric     auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
218349cc55cSDimitry Andric       return FAM.getResult<AssumptionAnalysis>(F);
219349cc55cSDimitry Andric     };
220349cc55cSDimitry Andric 
221349cc55cSDimitry Andric     // Now process as many calls as we have within this caller in the sequence.
222349cc55cSDimitry Andric     // We bail out as soon as the caller has to change so we can
223349cc55cSDimitry Andric     // prepare the context of that new caller.
224349cc55cSDimitry Andric     bool DidInline = false;
225349cc55cSDimitry Andric     while (!Calls->empty() && Calls->front().first->getCaller() == &F) {
226349cc55cSDimitry Andric       auto P = Calls->pop();
227349cc55cSDimitry Andric       CallBase *CB = P.first;
228349cc55cSDimitry Andric       const int InlineHistoryID = P.second;
229349cc55cSDimitry Andric       Function &Callee = *CB->getCalledFunction();
230349cc55cSDimitry Andric 
231349cc55cSDimitry Andric       if (InlineHistoryID != -1 &&
232349cc55cSDimitry Andric           inlineHistoryIncludes(&Callee, InlineHistoryID, InlineHistory)) {
233349cc55cSDimitry Andric         setInlineRemark(*CB, "recursive");
234349cc55cSDimitry Andric         continue;
235349cc55cSDimitry Andric       }
236349cc55cSDimitry Andric 
237349cc55cSDimitry Andric       auto Advice = Advisor.getAdvice(*CB, /*OnlyMandatory*/ false);
238349cc55cSDimitry Andric       // Check whether we want to inline this callsite.
239349cc55cSDimitry Andric       if (!Advice->isInliningRecommended()) {
240349cc55cSDimitry Andric         Advice->recordUnattemptedInlining();
241349cc55cSDimitry Andric         continue;
242349cc55cSDimitry Andric       }
243349cc55cSDimitry Andric 
244349cc55cSDimitry Andric       // Setup the data structure used to plumb customization into the
245349cc55cSDimitry Andric       // `InlineFunction` routine.
246349cc55cSDimitry Andric       InlineFunctionInfo IFI(
247349cc55cSDimitry Andric           /*cg=*/nullptr, GetAssumptionCache, PSI,
248349cc55cSDimitry Andric           &FAM.getResult<BlockFrequencyAnalysis>(*(CB->getCaller())),
249349cc55cSDimitry Andric           &FAM.getResult<BlockFrequencyAnalysis>(Callee));
250349cc55cSDimitry Andric 
251349cc55cSDimitry Andric       InlineResult IR =
252349cc55cSDimitry Andric           InlineFunction(*CB, IFI, &FAM.getResult<AAManager>(*CB->getCaller()));
253349cc55cSDimitry Andric       if (!IR.isSuccess()) {
254349cc55cSDimitry Andric         Advice->recordUnsuccessfulInlining(IR);
255349cc55cSDimitry Andric         continue;
256349cc55cSDimitry Andric       }
257349cc55cSDimitry Andric 
258349cc55cSDimitry Andric       DidInline = true;
259349cc55cSDimitry Andric       InlinedCallees.insert(&Callee);
260349cc55cSDimitry Andric       ++NumInlined;
261349cc55cSDimitry Andric 
262349cc55cSDimitry Andric       LLVM_DEBUG(dbgs() << "    Size after inlining: "
263349cc55cSDimitry Andric                         << F.getInstructionCount() << "\n");
264349cc55cSDimitry Andric 
265349cc55cSDimitry Andric       // Add any new callsites to defined functions to the worklist.
266349cc55cSDimitry Andric       if (!IFI.InlinedCallSites.empty()) {
267349cc55cSDimitry Andric         int NewHistoryID = InlineHistory.size();
268349cc55cSDimitry Andric         InlineHistory.push_back({&Callee, InlineHistoryID});
269349cc55cSDimitry Andric 
270349cc55cSDimitry Andric         for (CallBase *ICB : reverse(IFI.InlinedCallSites)) {
271349cc55cSDimitry Andric           Function *NewCallee = ICB->getCalledFunction();
272349cc55cSDimitry Andric           if (!NewCallee) {
273349cc55cSDimitry Andric             // Try to promote an indirect (virtual) call without waiting for
274349cc55cSDimitry Andric             // the post-inline cleanup and the next DevirtSCCRepeatedPass
275349cc55cSDimitry Andric             // iteration because the next iteration may not happen and we may
276349cc55cSDimitry Andric             // miss inlining it.
277349cc55cSDimitry Andric             if (tryPromoteCall(*ICB))
278349cc55cSDimitry Andric               NewCallee = ICB->getCalledFunction();
279349cc55cSDimitry Andric           }
280349cc55cSDimitry Andric           if (NewCallee)
281349cc55cSDimitry Andric             if (!NewCallee->isDeclaration())
282349cc55cSDimitry Andric               Calls->push({ICB, NewHistoryID});
283349cc55cSDimitry Andric         }
284349cc55cSDimitry Andric       }
285349cc55cSDimitry Andric 
286349cc55cSDimitry Andric       // Merge the attributes based on the inlining.
287349cc55cSDimitry Andric       AttributeFuncs::mergeAttributesForInlining(F, Callee);
288349cc55cSDimitry Andric 
289349cc55cSDimitry Andric       // For local functions, check whether this makes the callee trivially
290349cc55cSDimitry Andric       // dead. In that case, we can drop the body of the function eagerly
291349cc55cSDimitry Andric       // which may reduce the number of callers of other functions to one,
292349cc55cSDimitry Andric       // changing inline cost thresholds.
293349cc55cSDimitry Andric       bool CalleeWasDeleted = false;
294349cc55cSDimitry Andric       if (Callee.hasLocalLinkage()) {
295349cc55cSDimitry Andric         // To check this we also need to nuke any dead constant uses (perhaps
296349cc55cSDimitry Andric         // made dead by this operation on other functions).
297349cc55cSDimitry Andric         Callee.removeDeadConstantUsers();
298349cc55cSDimitry Andric         // if (Callee.use_empty() && !CG.isLibFunction(Callee)) {
299349cc55cSDimitry Andric         if (Callee.use_empty() && !isKnownLibFunction(Callee, GetTLI(Callee))) {
300349cc55cSDimitry Andric           Calls->erase_if([&](const std::pair<CallBase *, int> &Call) {
301349cc55cSDimitry Andric             return Call.first->getCaller() == &Callee;
302349cc55cSDimitry Andric           });
303349cc55cSDimitry Andric           // Clear the body and queue the function itself for deletion when we
304349cc55cSDimitry Andric           // finish inlining.
305349cc55cSDimitry Andric           // Note that after this point, it is an error to do anything other
306349cc55cSDimitry Andric           // than use the callee's address or delete it.
307349cc55cSDimitry Andric           Callee.dropAllReferences();
308349cc55cSDimitry Andric           assert(!is_contained(DeadFunctions, &Callee) &&
309349cc55cSDimitry Andric                  "Cannot put cause a function to become dead twice!");
310349cc55cSDimitry Andric           DeadFunctions.push_back(&Callee);
311349cc55cSDimitry Andric           CalleeWasDeleted = true;
312349cc55cSDimitry Andric         }
313349cc55cSDimitry Andric       }
314349cc55cSDimitry Andric       if (CalleeWasDeleted)
315349cc55cSDimitry Andric         Advice->recordInliningWithCalleeDeleted();
316349cc55cSDimitry Andric       else
317349cc55cSDimitry Andric         Advice->recordInlining();
318349cc55cSDimitry Andric     }
319349cc55cSDimitry Andric 
320349cc55cSDimitry Andric     if (!DidInline)
321349cc55cSDimitry Andric       continue;
322349cc55cSDimitry Andric     Changed = true;
323349cc55cSDimitry Andric 
324349cc55cSDimitry Andric     InlinedCallees.clear();
325349cc55cSDimitry Andric   }
326349cc55cSDimitry Andric 
327349cc55cSDimitry Andric   // Now that we've finished inlining all of the calls across this module,
328349cc55cSDimitry Andric   // delete all of the trivially dead functions.
329349cc55cSDimitry Andric   //
330349cc55cSDimitry Andric   // Note that this walks a pointer set which has non-deterministic order but
331349cc55cSDimitry Andric   // that is OK as all we do is delete things and add pointers to unordered
332349cc55cSDimitry Andric   // sets.
333349cc55cSDimitry Andric   for (Function *DeadF : DeadFunctions) {
334349cc55cSDimitry Andric     // Clear out any cached analyses.
335349cc55cSDimitry Andric     FAM.clear(*DeadF, DeadF->getName());
336349cc55cSDimitry Andric 
337349cc55cSDimitry Andric     // And delete the actual function from the module.
338*04eeddc0SDimitry Andric     M.getFunctionList().erase(DeadF);
339349cc55cSDimitry Andric 
340349cc55cSDimitry Andric     ++NumDeleted;
341349cc55cSDimitry Andric   }
342349cc55cSDimitry Andric 
343349cc55cSDimitry Andric   if (!Changed)
344349cc55cSDimitry Andric     return PreservedAnalyses::all();
345349cc55cSDimitry Andric 
346349cc55cSDimitry Andric   return PreservedAnalyses::none();
347349cc55cSDimitry Andric }
348