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