Inliner.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. //===- Inliner.cpp - Code common to all inliners --------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the mechanics required to implement inlining without
  10. // missing any calls and updating the call graph. The decisions of which calls
  11. // are profitable to inline are implemented elsewhere.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Transforms/IPO/Inliner.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/None.h"
  17. #include "llvm/ADT/Optional.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/ScopeExit.h"
  20. #include "llvm/ADT/SetVector.h"
  21. #include "llvm/ADT/SmallPtrSet.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/Statistic.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/Analysis/AssumptionCache.h"
  26. #include "llvm/Analysis/BasicAliasAnalysis.h"
  27. #include "llvm/Analysis/BlockFrequencyInfo.h"
  28. #include "llvm/Analysis/CGSCCPassManager.h"
  29. #include "llvm/Analysis/CallGraph.h"
  30. #include "llvm/Analysis/GlobalsModRef.h"
  31. #include "llvm/Analysis/InlineAdvisor.h"
  32. #include "llvm/Analysis/InlineCost.h"
  33. #include "llvm/Analysis/LazyCallGraph.h"
  34. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  35. #include "llvm/Analysis/ProfileSummaryInfo.h"
  36. #include "llvm/Analysis/TargetLibraryInfo.h"
  37. #include "llvm/Analysis/TargetTransformInfo.h"
  38. #include "llvm/Analysis/Utils/ImportedFunctionsInliningStatistics.h"
  39. #include "llvm/IR/Attributes.h"
  40. #include "llvm/IR/BasicBlock.h"
  41. #include "llvm/IR/DataLayout.h"
  42. #include "llvm/IR/DebugLoc.h"
  43. #include "llvm/IR/DerivedTypes.h"
  44. #include "llvm/IR/DiagnosticInfo.h"
  45. #include "llvm/IR/Function.h"
  46. #include "llvm/IR/InstIterator.h"
  47. #include "llvm/IR/Instruction.h"
  48. #include "llvm/IR/Instructions.h"
  49. #include "llvm/IR/IntrinsicInst.h"
  50. #include "llvm/IR/Metadata.h"
  51. #include "llvm/IR/Module.h"
  52. #include "llvm/IR/PassManager.h"
  53. #include "llvm/IR/User.h"
  54. #include "llvm/IR/Value.h"
  55. #include "llvm/Pass.h"
  56. #include "llvm/Support/Casting.h"
  57. #include "llvm/Support/CommandLine.h"
  58. #include "llvm/Support/Debug.h"
  59. #include "llvm/Support/raw_ostream.h"
  60. #include "llvm/Transforms/Utils/CallPromotionUtils.h"
  61. #include "llvm/Transforms/Utils/Cloning.h"
  62. #include "llvm/Transforms/Utils/Local.h"
  63. #include "llvm/Transforms/Utils/ModuleUtils.h"
  64. #include <algorithm>
  65. #include <cassert>
  66. #include <functional>
  67. #include <sstream>
  68. #include <tuple>
  69. #include <utility>
  70. #include <vector>
  71. using namespace llvm;
  72. #define DEBUG_TYPE "inline"
  73. STATISTIC(NumInlined, "Number of functions inlined");
  74. STATISTIC(NumCallsDeleted, "Number of call sites deleted, not inlined");
  75. STATISTIC(NumDeleted, "Number of functions deleted because all callers found");
  76. STATISTIC(NumMergedAllocas, "Number of allocas merged together");
  77. /// Flag to disable manual alloca merging.
  78. ///
  79. /// Merging of allocas was originally done as a stack-size saving technique
  80. /// prior to LLVM's code generator having support for stack coloring based on
  81. /// lifetime markers. It is now in the process of being removed. To experiment
  82. /// with disabling it and relying fully on lifetime marker based stack
  83. /// coloring, you can pass this flag to LLVM.
  84. static cl::opt<bool>
  85. DisableInlinedAllocaMerging("disable-inlined-alloca-merging",
  86. cl::init(false), cl::Hidden);
  87. extern cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats;
  88. static cl::opt<std::string> CGSCCInlineReplayFile(
  89. "cgscc-inline-replay", cl::init(""), cl::value_desc("filename"),
  90. cl::desc(
  91. "Optimization remarks file containing inline remarks to be replayed "
  92. "by inlining from cgscc inline remarks."),
  93. cl::Hidden);
  94. LegacyInlinerBase::LegacyInlinerBase(char &ID) : CallGraphSCCPass(ID) {}
  95. LegacyInlinerBase::LegacyInlinerBase(char &ID, bool InsertLifetime)
  96. : CallGraphSCCPass(ID), InsertLifetime(InsertLifetime) {}
  97. /// For this class, we declare that we require and preserve the call graph.
  98. /// If the derived class implements this method, it should
  99. /// always explicitly call the implementation here.
  100. void LegacyInlinerBase::getAnalysisUsage(AnalysisUsage &AU) const {
  101. AU.addRequired<AssumptionCacheTracker>();
  102. AU.addRequired<ProfileSummaryInfoWrapperPass>();
  103. AU.addRequired<TargetLibraryInfoWrapperPass>();
  104. getAAResultsAnalysisUsage(AU);
  105. CallGraphSCCPass::getAnalysisUsage(AU);
  106. }
  107. using InlinedArrayAllocasTy = DenseMap<ArrayType *, std::vector<AllocaInst *>>;
  108. /// Look at all of the allocas that we inlined through this call site. If we
  109. /// have already inlined other allocas through other calls into this function,
  110. /// then we know that they have disjoint lifetimes and that we can merge them.
  111. ///
  112. /// There are many heuristics possible for merging these allocas, and the
  113. /// different options have different tradeoffs. One thing that we *really*
  114. /// don't want to hurt is SRoA: once inlining happens, often allocas are no
  115. /// longer address taken and so they can be promoted.
  116. ///
  117. /// Our "solution" for that is to only merge allocas whose outermost type is an
  118. /// array type. These are usually not promoted because someone is using a
  119. /// variable index into them. These are also often the most important ones to
  120. /// merge.
  121. ///
  122. /// A better solution would be to have real memory lifetime markers in the IR
  123. /// and not have the inliner do any merging of allocas at all. This would
  124. /// allow the backend to do proper stack slot coloring of all allocas that
  125. /// *actually make it to the backend*, which is really what we want.
  126. ///
  127. /// Because we don't have this information, we do this simple and useful hack.
  128. static void mergeInlinedArrayAllocas(Function *Caller, InlineFunctionInfo &IFI,
  129. InlinedArrayAllocasTy &InlinedArrayAllocas,
  130. int InlineHistory) {
  131. SmallPtrSet<AllocaInst *, 16> UsedAllocas;
  132. // When processing our SCC, check to see if the call site was inlined from
  133. // some other call site. For example, if we're processing "A" in this code:
  134. // A() { B() }
  135. // B() { x = alloca ... C() }
  136. // C() { y = alloca ... }
  137. // Assume that C was not inlined into B initially, and so we're processing A
  138. // and decide to inline B into A. Doing this makes an alloca available for
  139. // reuse and makes a callsite (C) available for inlining. When we process
  140. // the C call site we don't want to do any alloca merging between X and Y
  141. // because their scopes are not disjoint. We could make this smarter by
  142. // keeping track of the inline history for each alloca in the
  143. // InlinedArrayAllocas but this isn't likely to be a significant win.
  144. if (InlineHistory != -1) // Only do merging for top-level call sites in SCC.
  145. return;
  146. // Loop over all the allocas we have so far and see if they can be merged with
  147. // a previously inlined alloca. If not, remember that we had it.
  148. for (unsigned AllocaNo = 0, E = IFI.StaticAllocas.size(); AllocaNo != E;
  149. ++AllocaNo) {
  150. AllocaInst *AI = IFI.StaticAllocas[AllocaNo];
  151. // Don't bother trying to merge array allocations (they will usually be
  152. // canonicalized to be an allocation *of* an array), or allocations whose
  153. // type is not itself an array (because we're afraid of pessimizing SRoA).
  154. ArrayType *ATy = dyn_cast<ArrayType>(AI->getAllocatedType());
  155. if (!ATy || AI->isArrayAllocation())
  156. continue;
  157. // Get the list of all available allocas for this array type.
  158. std::vector<AllocaInst *> &AllocasForType = InlinedArrayAllocas[ATy];
  159. // Loop over the allocas in AllocasForType to see if we can reuse one. Note
  160. // that we have to be careful not to reuse the same "available" alloca for
  161. // multiple different allocas that we just inlined, we use the 'UsedAllocas'
  162. // set to keep track of which "available" allocas are being used by this
  163. // function. Also, AllocasForType can be empty of course!
  164. bool MergedAwayAlloca = false;
  165. for (AllocaInst *AvailableAlloca : AllocasForType) {
  166. Align Align1 = AI->getAlign();
  167. Align Align2 = AvailableAlloca->getAlign();
  168. // The available alloca has to be in the right function, not in some other
  169. // function in this SCC.
  170. if (AvailableAlloca->getParent() != AI->getParent())
  171. continue;
  172. // If the inlined function already uses this alloca then we can't reuse
  173. // it.
  174. if (!UsedAllocas.insert(AvailableAlloca).second)
  175. continue;
  176. // Otherwise, we *can* reuse it, RAUW AI into AvailableAlloca and declare
  177. // success!
  178. LLVM_DEBUG(dbgs() << " ***MERGED ALLOCA: " << *AI
  179. << "\n\t\tINTO: " << *AvailableAlloca << '\n');
  180. // Move affected dbg.declare calls immediately after the new alloca to
  181. // avoid the situation when a dbg.declare precedes its alloca.
  182. if (auto *L = LocalAsMetadata::getIfExists(AI))
  183. if (auto *MDV = MetadataAsValue::getIfExists(AI->getContext(), L))
  184. for (User *U : MDV->users())
  185. if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
  186. DDI->moveBefore(AvailableAlloca->getNextNode());
  187. AI->replaceAllUsesWith(AvailableAlloca);
  188. if (Align1 > Align2)
  189. AvailableAlloca->setAlignment(AI->getAlign());
  190. AI->eraseFromParent();
  191. MergedAwayAlloca = true;
  192. ++NumMergedAllocas;
  193. IFI.StaticAllocas[AllocaNo] = nullptr;
  194. break;
  195. }
  196. // If we already nuked the alloca, we're done with it.
  197. if (MergedAwayAlloca)
  198. continue;
  199. // If we were unable to merge away the alloca either because there are no
  200. // allocas of the right type available or because we reused them all
  201. // already, remember that this alloca came from an inlined function and mark
  202. // it used so we don't reuse it for other allocas from this inline
  203. // operation.
  204. AllocasForType.push_back(AI);
  205. UsedAllocas.insert(AI);
  206. }
  207. }
  208. /// If it is possible to inline the specified call site,
  209. /// do so and update the CallGraph for this operation.
  210. ///
  211. /// This function also does some basic book-keeping to update the IR. The
  212. /// InlinedArrayAllocas map keeps track of any allocas that are already
  213. /// available from other functions inlined into the caller. If we are able to
  214. /// inline this call site we attempt to reuse already available allocas or add
  215. /// any new allocas to the set if not possible.
  216. static InlineResult inlineCallIfPossible(
  217. CallBase &CB, InlineFunctionInfo &IFI,
  218. InlinedArrayAllocasTy &InlinedArrayAllocas, int InlineHistory,
  219. bool InsertLifetime, function_ref<AAResults &(Function &)> &AARGetter,
  220. ImportedFunctionsInliningStatistics &ImportedFunctionsStats) {
  221. Function *Callee = CB.getCalledFunction();
  222. Function *Caller = CB.getCaller();
  223. AAResults &AAR = AARGetter(*Callee);
  224. // Try to inline the function. Get the list of static allocas that were
  225. // inlined.
  226. InlineResult IR = InlineFunction(CB, IFI, &AAR, InsertLifetime);
  227. if (!IR.isSuccess())
  228. return IR;
  229. if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No)
  230. ImportedFunctionsStats.recordInline(*Caller, *Callee);
  231. AttributeFuncs::mergeAttributesForInlining(*Caller, *Callee);
  232. if (!DisableInlinedAllocaMerging)
  233. mergeInlinedArrayAllocas(Caller, IFI, InlinedArrayAllocas, InlineHistory);
  234. return IR; // success
  235. }
  236. /// Return true if the specified inline history ID
  237. /// indicates an inline history that includes the specified function.
  238. static bool inlineHistoryIncludes(
  239. Function *F, int InlineHistoryID,
  240. const SmallVectorImpl<std::pair<Function *, int>> &InlineHistory) {
  241. while (InlineHistoryID != -1) {
  242. assert(unsigned(InlineHistoryID) < InlineHistory.size() &&
  243. "Invalid inline history ID");
  244. if (InlineHistory[InlineHistoryID].first == F)
  245. return true;
  246. InlineHistoryID = InlineHistory[InlineHistoryID].second;
  247. }
  248. return false;
  249. }
  250. bool LegacyInlinerBase::doInitialization(CallGraph &CG) {
  251. if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No)
  252. ImportedFunctionsStats.setModuleInfo(CG.getModule());
  253. return false; // No changes to CallGraph.
  254. }
  255. bool LegacyInlinerBase::runOnSCC(CallGraphSCC &SCC) {
  256. if (skipSCC(SCC))
  257. return false;
  258. return inlineCalls(SCC);
  259. }
  260. static bool
  261. inlineCallsImpl(CallGraphSCC &SCC, CallGraph &CG,
  262. std::function<AssumptionCache &(Function &)> GetAssumptionCache,
  263. ProfileSummaryInfo *PSI,
  264. std::function<const TargetLibraryInfo &(Function &)> GetTLI,
  265. bool InsertLifetime,
  266. function_ref<InlineCost(CallBase &CB)> GetInlineCost,
  267. function_ref<AAResults &(Function &)> AARGetter,
  268. ImportedFunctionsInliningStatistics &ImportedFunctionsStats) {
  269. SmallPtrSet<Function *, 8> SCCFunctions;
  270. LLVM_DEBUG(dbgs() << "Inliner visiting SCC:");
  271. for (CallGraphNode *Node : SCC) {
  272. Function *F = Node->getFunction();
  273. if (F)
  274. SCCFunctions.insert(F);
  275. LLVM_DEBUG(dbgs() << " " << (F ? F->getName() : "INDIRECTNODE"));
  276. }
  277. // Scan through and identify all call sites ahead of time so that we only
  278. // inline call sites in the original functions, not call sites that result
  279. // from inlining other functions.
  280. SmallVector<std::pair<CallBase *, int>, 16> CallSites;
  281. // When inlining a callee produces new call sites, we want to keep track of
  282. // the fact that they were inlined from the callee. This allows us to avoid
  283. // infinite inlining in some obscure cases. To represent this, we use an
  284. // index into the InlineHistory vector.
  285. SmallVector<std::pair<Function *, int>, 8> InlineHistory;
  286. for (CallGraphNode *Node : SCC) {
  287. Function *F = Node->getFunction();
  288. if (!F || F->isDeclaration())
  289. continue;
  290. OptimizationRemarkEmitter ORE(F);
  291. for (BasicBlock &BB : *F)
  292. for (Instruction &I : BB) {
  293. auto *CB = dyn_cast<CallBase>(&I);
  294. // If this isn't a call, or it is a call to an intrinsic, it can
  295. // never be inlined.
  296. if (!CB || isa<IntrinsicInst>(I))
  297. continue;
  298. // If this is a direct call to an external function, we can never inline
  299. // it. If it is an indirect call, inlining may resolve it to be a
  300. // direct call, so we keep it.
  301. if (Function *Callee = CB->getCalledFunction())
  302. if (Callee->isDeclaration()) {
  303. using namespace ore;
  304. setInlineRemark(*CB, "unavailable definition");
  305. ORE.emit([&]() {
  306. return OptimizationRemarkMissed(DEBUG_TYPE, "NoDefinition", &I)
  307. << NV("Callee", Callee) << " will not be inlined into "
  308. << NV("Caller", CB->getCaller())
  309. << " because its definition is unavailable"
  310. << setIsVerbose();
  311. });
  312. continue;
  313. }
  314. CallSites.push_back(std::make_pair(CB, -1));
  315. }
  316. }
  317. LLVM_DEBUG(dbgs() << ": " << CallSites.size() << " call sites.\n");
  318. // If there are no calls in this function, exit early.
  319. if (CallSites.empty())
  320. return false;
  321. // Now that we have all of the call sites, move the ones to functions in the
  322. // current SCC to the end of the list.
  323. unsigned FirstCallInSCC = CallSites.size();
  324. for (unsigned I = 0; I < FirstCallInSCC; ++I)
  325. if (Function *F = CallSites[I].first->getCalledFunction())
  326. if (SCCFunctions.count(F))
  327. std::swap(CallSites[I--], CallSites[--FirstCallInSCC]);
  328. InlinedArrayAllocasTy InlinedArrayAllocas;
  329. InlineFunctionInfo InlineInfo(&CG, GetAssumptionCache, PSI);
  330. // Now that we have all of the call sites, loop over them and inline them if
  331. // it looks profitable to do so.
  332. bool Changed = false;
  333. bool LocalChange;
  334. do {
  335. LocalChange = false;
  336. // Iterate over the outer loop because inlining functions can cause indirect
  337. // calls to become direct calls.
  338. // CallSites may be modified inside so ranged for loop can not be used.
  339. for (unsigned CSi = 0; CSi != CallSites.size(); ++CSi) {
  340. auto &P = CallSites[CSi];
  341. CallBase &CB = *P.first;
  342. const int InlineHistoryID = P.second;
  343. Function *Caller = CB.getCaller();
  344. Function *Callee = CB.getCalledFunction();
  345. // We can only inline direct calls to non-declarations.
  346. if (!Callee || Callee->isDeclaration())
  347. continue;
  348. bool IsTriviallyDead = isInstructionTriviallyDead(&CB, &GetTLI(*Caller));
  349. if (!IsTriviallyDead) {
  350. // If this call site was obtained by inlining another function, verify
  351. // that the include path for the function did not include the callee
  352. // itself. If so, we'd be recursively inlining the same function,
  353. // which would provide the same callsites, which would cause us to
  354. // infinitely inline.
  355. if (InlineHistoryID != -1 &&
  356. inlineHistoryIncludes(Callee, InlineHistoryID, InlineHistory)) {
  357. setInlineRemark(CB, "recursive");
  358. continue;
  359. }
  360. }
  361. // FIXME for new PM: because of the old PM we currently generate ORE and
  362. // in turn BFI on demand. With the new PM, the ORE dependency should
  363. // just become a regular analysis dependency.
  364. OptimizationRemarkEmitter ORE(Caller);
  365. auto OIC = shouldInline(CB, GetInlineCost, ORE);
  366. // If the policy determines that we should inline this function,
  367. // delete the call instead.
  368. if (!OIC)
  369. continue;
  370. // If this call site is dead and it is to a readonly function, we should
  371. // just delete the call instead of trying to inline it, regardless of
  372. // size. This happens because IPSCCP propagates the result out of the
  373. // call and then we're left with the dead call.
  374. if (IsTriviallyDead) {
  375. LLVM_DEBUG(dbgs() << " -> Deleting dead call: " << CB << "\n");
  376. // Update the call graph by deleting the edge from Callee to Caller.
  377. setInlineRemark(CB, "trivially dead");
  378. CG[Caller]->removeCallEdgeFor(CB);
  379. CB.eraseFromParent();
  380. ++NumCallsDeleted;
  381. } else {
  382. // Get DebugLoc to report. CB will be invalid after Inliner.
  383. DebugLoc DLoc = CB.getDebugLoc();
  384. BasicBlock *Block = CB.getParent();
  385. // Attempt to inline the function.
  386. using namespace ore;
  387. InlineResult IR = inlineCallIfPossible(
  388. CB, InlineInfo, InlinedArrayAllocas, InlineHistoryID,
  389. InsertLifetime, AARGetter, ImportedFunctionsStats);
  390. if (!IR.isSuccess()) {
  391. setInlineRemark(CB, std::string(IR.getFailureReason()) + "; " +
  392. inlineCostStr(*OIC));
  393. ORE.emit([&]() {
  394. return OptimizationRemarkMissed(DEBUG_TYPE, "NotInlined", DLoc,
  395. Block)
  396. << NV("Callee", Callee) << " will not be inlined into "
  397. << NV("Caller", Caller) << ": "
  398. << NV("Reason", IR.getFailureReason());
  399. });
  400. continue;
  401. }
  402. ++NumInlined;
  403. emitInlinedInto(ORE, DLoc, Block, *Callee, *Caller, *OIC);
  404. // If inlining this function gave us any new call sites, throw them
  405. // onto our worklist to process. They are useful inline candidates.
  406. if (!InlineInfo.InlinedCalls.empty()) {
  407. // Create a new inline history entry for this, so that we remember
  408. // that these new callsites came about due to inlining Callee.
  409. int NewHistoryID = InlineHistory.size();
  410. InlineHistory.push_back(std::make_pair(Callee, InlineHistoryID));
  411. #ifndef NDEBUG
  412. // Make sure no dupplicates in the inline candidates. This could
  413. // happen when a callsite is simpilfied to reusing the return value
  414. // of another callsite during function cloning, thus the other
  415. // callsite will be reconsidered here.
  416. DenseSet<CallBase *> DbgCallSites;
  417. for (auto &II : CallSites)
  418. DbgCallSites.insert(II.first);
  419. #endif
  420. for (Value *Ptr : InlineInfo.InlinedCalls) {
  421. #ifndef NDEBUG
  422. assert(DbgCallSites.count(dyn_cast<CallBase>(Ptr)) == 0);
  423. #endif
  424. CallSites.push_back(
  425. std::make_pair(dyn_cast<CallBase>(Ptr), NewHistoryID));
  426. }
  427. }
  428. }
  429. // If we inlined or deleted the last possible call site to the function,
  430. // delete the function body now.
  431. if (Callee && Callee->use_empty() && Callee->hasLocalLinkage() &&
  432. // TODO: Can remove if in SCC now.
  433. !SCCFunctions.count(Callee) &&
  434. // The function may be apparently dead, but if there are indirect
  435. // callgraph references to the node, we cannot delete it yet, this
  436. // could invalidate the CGSCC iterator.
  437. CG[Callee]->getNumReferences() == 0) {
  438. LLVM_DEBUG(dbgs() << " -> Deleting dead function: "
  439. << Callee->getName() << "\n");
  440. CallGraphNode *CalleeNode = CG[Callee];
  441. // Remove any call graph edges from the callee to its callees.
  442. CalleeNode->removeAllCalledFunctions();
  443. // Removing the node for callee from the call graph and delete it.
  444. delete CG.removeFunctionFromModule(CalleeNode);
  445. ++NumDeleted;
  446. }
  447. // Remove this call site from the list. If possible, use
  448. // swap/pop_back for efficiency, but do not use it if doing so would
  449. // move a call site to a function in this SCC before the
  450. // 'FirstCallInSCC' barrier.
  451. if (SCC.isSingular()) {
  452. CallSites[CSi] = CallSites.back();
  453. CallSites.pop_back();
  454. } else {
  455. CallSites.erase(CallSites.begin() + CSi);
  456. }
  457. --CSi;
  458. Changed = true;
  459. LocalChange = true;
  460. }
  461. } while (LocalChange);
  462. return Changed;
  463. }
  464. bool LegacyInlinerBase::inlineCalls(CallGraphSCC &SCC) {
  465. CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
  466. ACT = &getAnalysis<AssumptionCacheTracker>();
  467. PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
  468. GetTLI = [&](Function &F) -> const TargetLibraryInfo & {
  469. return getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
  470. };
  471. auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
  472. return ACT->getAssumptionCache(F);
  473. };
  474. return inlineCallsImpl(
  475. SCC, CG, GetAssumptionCache, PSI, GetTLI, InsertLifetime,
  476. [&](CallBase &CB) { return getInlineCost(CB); }, LegacyAARGetter(*this),
  477. ImportedFunctionsStats);
  478. }
  479. /// Remove now-dead linkonce functions at the end of
  480. /// processing to avoid breaking the SCC traversal.
  481. bool LegacyInlinerBase::doFinalization(CallGraph &CG) {
  482. if (InlinerFunctionImportStats != InlinerFunctionImportStatsOpts::No)
  483. ImportedFunctionsStats.dump(InlinerFunctionImportStats ==
  484. InlinerFunctionImportStatsOpts::Verbose);
  485. return removeDeadFunctions(CG);
  486. }
  487. /// Remove dead functions that are not included in DNR (Do Not Remove) list.
  488. bool LegacyInlinerBase::removeDeadFunctions(CallGraph &CG,
  489. bool AlwaysInlineOnly) {
  490. SmallVector<CallGraphNode *, 16> FunctionsToRemove;
  491. SmallVector<Function *, 16> DeadFunctionsInComdats;
  492. auto RemoveCGN = [&](CallGraphNode *CGN) {
  493. // Remove any call graph edges from the function to its callees.
  494. CGN->removeAllCalledFunctions();
  495. // Remove any edges from the external node to the function's call graph
  496. // node. These edges might have been made irrelegant due to
  497. // optimization of the program.
  498. CG.getExternalCallingNode()->removeAnyCallEdgeTo(CGN);
  499. // Removing the node for callee from the call graph and delete it.
  500. FunctionsToRemove.push_back(CGN);
  501. };
  502. // Scan for all of the functions, looking for ones that should now be removed
  503. // from the program. Insert the dead ones in the FunctionsToRemove set.
  504. for (const auto &I : CG) {
  505. CallGraphNode *CGN = I.second.get();
  506. Function *F = CGN->getFunction();
  507. if (!F || F->isDeclaration())
  508. continue;
  509. // Handle the case when this function is called and we only want to care
  510. // about always-inline functions. This is a bit of a hack to share code
  511. // between here and the InlineAlways pass.
  512. if (AlwaysInlineOnly && !F->hasFnAttribute(Attribute::AlwaysInline))
  513. continue;
  514. // If the only remaining users of the function are dead constants, remove
  515. // them.
  516. F->removeDeadConstantUsers();
  517. if (!F->isDefTriviallyDead())
  518. continue;
  519. // It is unsafe to drop a function with discardable linkage from a COMDAT
  520. // without also dropping the other members of the COMDAT.
  521. // The inliner doesn't visit non-function entities which are in COMDAT
  522. // groups so it is unsafe to do so *unless* the linkage is local.
  523. if (!F->hasLocalLinkage()) {
  524. if (F->hasComdat()) {
  525. DeadFunctionsInComdats.push_back(F);
  526. continue;
  527. }
  528. }
  529. RemoveCGN(CGN);
  530. }
  531. if (!DeadFunctionsInComdats.empty()) {
  532. // Filter out the functions whose comdats remain alive.
  533. filterDeadComdatFunctions(CG.getModule(), DeadFunctionsInComdats);
  534. // Remove the rest.
  535. for (Function *F : DeadFunctionsInComdats)
  536. RemoveCGN(CG[F]);
  537. }
  538. if (FunctionsToRemove.empty())
  539. return false;
  540. // Now that we know which functions to delete, do so. We didn't want to do
  541. // this inline, because that would invalidate our CallGraph::iterator
  542. // objects. :(
  543. //
  544. // Note that it doesn't matter that we are iterating over a non-stable order
  545. // here to do this, it doesn't matter which order the functions are deleted
  546. // in.
  547. array_pod_sort(FunctionsToRemove.begin(), FunctionsToRemove.end());
  548. FunctionsToRemove.erase(
  549. std::unique(FunctionsToRemove.begin(), FunctionsToRemove.end()),
  550. FunctionsToRemove.end());
  551. for (CallGraphNode *CGN : FunctionsToRemove) {
  552. delete CG.removeFunctionFromModule(CGN);
  553. ++NumDeleted;
  554. }
  555. return true;
  556. }
  557. InlineAdvisor &
  558. InlinerPass::getAdvisor(const ModuleAnalysisManagerCGSCCProxy::Result &MAM,
  559. FunctionAnalysisManager &FAM, Module &M) {
  560. if (OwnedAdvisor)
  561. return *OwnedAdvisor;
  562. auto *IAA = MAM.getCachedResult<InlineAdvisorAnalysis>(M);
  563. if (!IAA) {
  564. // It should still be possible to run the inliner as a stand-alone SCC pass,
  565. // for test scenarios. In that case, we default to the
  566. // DefaultInlineAdvisor, which doesn't need to keep state between SCC pass
  567. // runs. It also uses just the default InlineParams.
  568. // In this case, we need to use the provided FAM, which is valid for the
  569. // duration of the inliner pass, and thus the lifetime of the owned advisor.
  570. // The one we would get from the MAM can be invalidated as a result of the
  571. // inliner's activity.
  572. OwnedAdvisor =
  573. std::make_unique<DefaultInlineAdvisor>(M, FAM, getInlineParams());
  574. if (!CGSCCInlineReplayFile.empty())
  575. OwnedAdvisor = std::make_unique<ReplayInlineAdvisor>(
  576. M, FAM, M.getContext(), std::move(OwnedAdvisor),
  577. CGSCCInlineReplayFile,
  578. /*EmitRemarks=*/true);
  579. return *OwnedAdvisor;
  580. }
  581. assert(IAA->getAdvisor() &&
  582. "Expected a present InlineAdvisorAnalysis also have an "
  583. "InlineAdvisor initialized");
  584. return *IAA->getAdvisor();
  585. }
  586. PreservedAnalyses InlinerPass::run(LazyCallGraph::SCC &InitialC,
  587. CGSCCAnalysisManager &AM, LazyCallGraph &CG,
  588. CGSCCUpdateResult &UR) {
  589. const auto &MAMProxy =
  590. AM.getResult<ModuleAnalysisManagerCGSCCProxy>(InitialC, CG);
  591. bool Changed = false;
  592. assert(InitialC.size() > 0 && "Cannot handle an empty SCC!");
  593. Module &M = *InitialC.begin()->getFunction().getParent();
  594. ProfileSummaryInfo *PSI = MAMProxy.getCachedResult<ProfileSummaryAnalysis>(M);
  595. FunctionAnalysisManager &FAM =
  596. AM.getResult<FunctionAnalysisManagerCGSCCProxy>(InitialC, CG)
  597. .getManager();
  598. InlineAdvisor &Advisor = getAdvisor(MAMProxy, FAM, M);
  599. Advisor.onPassEntry();
  600. auto AdvisorOnExit = make_scope_exit([&] { Advisor.onPassExit(); });
  601. // We use a single common worklist for calls across the entire SCC. We
  602. // process these in-order and append new calls introduced during inlining to
  603. // the end.
  604. //
  605. // Note that this particular order of processing is actually critical to
  606. // avoid very bad behaviors. Consider *highly connected* call graphs where
  607. // each function contains a small amount of code and a couple of calls to
  608. // other functions. Because the LLVM inliner is fundamentally a bottom-up
  609. // inliner, it can handle gracefully the fact that these all appear to be
  610. // reasonable inlining candidates as it will flatten things until they become
  611. // too big to inline, and then move on and flatten another batch.
  612. //
  613. // However, when processing call edges *within* an SCC we cannot rely on this
  614. // bottom-up behavior. As a consequence, with heavily connected *SCCs* of
  615. // functions we can end up incrementally inlining N calls into each of
  616. // N functions because each incremental inlining decision looks good and we
  617. // don't have a topological ordering to prevent explosions.
  618. //
  619. // To compensate for this, we don't process transitive edges made immediate
  620. // by inlining until we've done one pass of inlining across the entire SCC.
  621. // Large, highly connected SCCs still lead to some amount of code bloat in
  622. // this model, but it is uniformly spread across all the functions in the SCC
  623. // and eventually they all become too large to inline, rather than
  624. // incrementally maknig a single function grow in a super linear fashion.
  625. SmallVector<std::pair<CallBase *, int>, 16> Calls;
  626. // Populate the initial list of calls in this SCC.
  627. for (auto &N : InitialC) {
  628. auto &ORE =
  629. FAM.getResult<OptimizationRemarkEmitterAnalysis>(N.getFunction());
  630. // We want to generally process call sites top-down in order for
  631. // simplifications stemming from replacing the call with the returned value
  632. // after inlining to be visible to subsequent inlining decisions.
  633. // FIXME: Using instructions sequence is a really bad way to do this.
  634. // Instead we should do an actual RPO walk of the function body.
  635. for (Instruction &I : instructions(N.getFunction()))
  636. if (auto *CB = dyn_cast<CallBase>(&I))
  637. if (Function *Callee = CB->getCalledFunction()) {
  638. if (!Callee->isDeclaration())
  639. Calls.push_back({CB, -1});
  640. else if (!isa<IntrinsicInst>(I)) {
  641. using namespace ore;
  642. setInlineRemark(*CB, "unavailable definition");
  643. ORE.emit([&]() {
  644. return OptimizationRemarkMissed(DEBUG_TYPE, "NoDefinition", &I)
  645. << NV("Callee", Callee) << " will not be inlined into "
  646. << NV("Caller", CB->getCaller())
  647. << " because its definition is unavailable"
  648. << setIsVerbose();
  649. });
  650. }
  651. }
  652. }
  653. if (Calls.empty())
  654. return PreservedAnalyses::all();
  655. // Capture updatable variable for the current SCC.
  656. auto *C = &InitialC;
  657. // When inlining a callee produces new call sites, we want to keep track of
  658. // the fact that they were inlined from the callee. This allows us to avoid
  659. // infinite inlining in some obscure cases. To represent this, we use an
  660. // index into the InlineHistory vector.
  661. SmallVector<std::pair<Function *, int>, 16> InlineHistory;
  662. // Track a set vector of inlined callees so that we can augment the caller
  663. // with all of their edges in the call graph before pruning out the ones that
  664. // got simplified away.
  665. SmallSetVector<Function *, 4> InlinedCallees;
  666. // Track the dead functions to delete once finished with inlining calls. We
  667. // defer deleting these to make it easier to handle the call graph updates.
  668. SmallVector<Function *, 4> DeadFunctions;
  669. // Loop forward over all of the calls. Note that we cannot cache the size as
  670. // inlining can introduce new calls that need to be processed.
  671. for (int I = 0; I < (int)Calls.size(); ++I) {
  672. // We expect the calls to typically be batched with sequences of calls that
  673. // have the same caller, so we first set up some shared infrastructure for
  674. // this caller. We also do any pruning we can at this layer on the caller
  675. // alone.
  676. Function &F = *Calls[I].first->getCaller();
  677. LazyCallGraph::Node &N = *CG.lookup(F);
  678. if (CG.lookupSCC(N) != C)
  679. continue;
  680. LLVM_DEBUG(dbgs() << "Inlining calls in: " << F.getName() << "\n");
  681. auto GetAssumptionCache = [&](Function &F) -> AssumptionCache & {
  682. return FAM.getResult<AssumptionAnalysis>(F);
  683. };
  684. // Now process as many calls as we have within this caller in the sequence.
  685. // We bail out as soon as the caller has to change so we can update the
  686. // call graph and prepare the context of that new caller.
  687. bool DidInline = false;
  688. for (; I < (int)Calls.size() && Calls[I].first->getCaller() == &F; ++I) {
  689. auto &P = Calls[I];
  690. CallBase *CB = P.first;
  691. const int InlineHistoryID = P.second;
  692. Function &Callee = *CB->getCalledFunction();
  693. if (InlineHistoryID != -1 &&
  694. inlineHistoryIncludes(&Callee, InlineHistoryID, InlineHistory)) {
  695. setInlineRemark(*CB, "recursive");
  696. continue;
  697. }
  698. // Check if this inlining may repeat breaking an SCC apart that has
  699. // already been split once before. In that case, inlining here may
  700. // trigger infinite inlining, much like is prevented within the inliner
  701. // itself by the InlineHistory above, but spread across CGSCC iterations
  702. // and thus hidden from the full inline history.
  703. if (CG.lookupSCC(*CG.lookup(Callee)) == C &&
  704. UR.InlinedInternalEdges.count({&N, C})) {
  705. LLVM_DEBUG(dbgs() << "Skipping inlining internal SCC edge from a node "
  706. "previously split out of this SCC by inlining: "
  707. << F.getName() << " -> " << Callee.getName() << "\n");
  708. setInlineRemark(*CB, "recursive SCC split");
  709. continue;
  710. }
  711. auto Advice = Advisor.getAdvice(*CB, OnlyMandatory);
  712. // Check whether we want to inline this callsite.
  713. if (!Advice->isInliningRecommended()) {
  714. Advice->recordUnattemptedInlining();
  715. continue;
  716. }
  717. // Setup the data structure used to plumb customization into the
  718. // `InlineFunction` routine.
  719. InlineFunctionInfo IFI(
  720. /*cg=*/nullptr, GetAssumptionCache, PSI,
  721. &FAM.getResult<BlockFrequencyAnalysis>(*(CB->getCaller())),
  722. &FAM.getResult<BlockFrequencyAnalysis>(Callee));
  723. InlineResult IR =
  724. InlineFunction(*CB, IFI, &FAM.getResult<AAManager>(*CB->getCaller()));
  725. if (!IR.isSuccess()) {
  726. Advice->recordUnsuccessfulInlining(IR);
  727. continue;
  728. }
  729. DidInline = true;
  730. InlinedCallees.insert(&Callee);
  731. ++NumInlined;
  732. // Add any new callsites to defined functions to the worklist.
  733. if (!IFI.InlinedCallSites.empty()) {
  734. int NewHistoryID = InlineHistory.size();
  735. InlineHistory.push_back({&Callee, InlineHistoryID});
  736. for (CallBase *ICB : reverse(IFI.InlinedCallSites)) {
  737. Function *NewCallee = ICB->getCalledFunction();
  738. if (!NewCallee) {
  739. // Try to promote an indirect (virtual) call without waiting for
  740. // the post-inline cleanup and the next DevirtSCCRepeatedPass
  741. // iteration because the next iteration may not happen and we may
  742. // miss inlining it.
  743. if (tryPromoteCall(*ICB))
  744. NewCallee = ICB->getCalledFunction();
  745. }
  746. if (NewCallee)
  747. if (!NewCallee->isDeclaration())
  748. Calls.push_back({ICB, NewHistoryID});
  749. }
  750. }
  751. // Merge the attributes based on the inlining.
  752. AttributeFuncs::mergeAttributesForInlining(F, Callee);
  753. // For local functions, check whether this makes the callee trivially
  754. // dead. In that case, we can drop the body of the function eagerly
  755. // which may reduce the number of callers of other functions to one,
  756. // changing inline cost thresholds.
  757. bool CalleeWasDeleted = false;
  758. if (Callee.hasLocalLinkage()) {
  759. // To check this we also need to nuke any dead constant uses (perhaps
  760. // made dead by this operation on other functions).
  761. Callee.removeDeadConstantUsers();
  762. if (Callee.use_empty() && !CG.isLibFunction(Callee)) {
  763. Calls.erase(
  764. std::remove_if(Calls.begin() + I + 1, Calls.end(),
  765. [&](const std::pair<CallBase *, int> &Call) {
  766. return Call.first->getCaller() == &Callee;
  767. }),
  768. Calls.end());
  769. // Clear the body and queue the function itself for deletion when we
  770. // finish inlining and call graph updates.
  771. // Note that after this point, it is an error to do anything other
  772. // than use the callee's address or delete it.
  773. Callee.dropAllReferences();
  774. assert(!is_contained(DeadFunctions, &Callee) &&
  775. "Cannot put cause a function to become dead twice!");
  776. DeadFunctions.push_back(&Callee);
  777. CalleeWasDeleted = true;
  778. }
  779. }
  780. if (CalleeWasDeleted)
  781. Advice->recordInliningWithCalleeDeleted();
  782. else
  783. Advice->recordInlining();
  784. }
  785. // Back the call index up by one to put us in a good position to go around
  786. // the outer loop.
  787. --I;
  788. if (!DidInline)
  789. continue;
  790. Changed = true;
  791. // At this point, since we have made changes we have at least removed
  792. // a call instruction. However, in the process we do some incremental
  793. // simplification of the surrounding code. This simplification can
  794. // essentially do all of the same things as a function pass and we can
  795. // re-use the exact same logic for updating the call graph to reflect the
  796. // change.
  797. // Inside the update, we also update the FunctionAnalysisManager in the
  798. // proxy for this particular SCC. We do this as the SCC may have changed and
  799. // as we're going to mutate this particular function we want to make sure
  800. // the proxy is in place to forward any invalidation events.
  801. LazyCallGraph::SCC *OldC = C;
  802. C = &updateCGAndAnalysisManagerForCGSCCPass(CG, *C, N, AM, UR, FAM);
  803. LLVM_DEBUG(dbgs() << "Updated inlining SCC: " << *C << "\n");
  804. // If this causes an SCC to split apart into multiple smaller SCCs, there
  805. // is a subtle risk we need to prepare for. Other transformations may
  806. // expose an "infinite inlining" opportunity later, and because of the SCC
  807. // mutation, we will revisit this function and potentially re-inline. If we
  808. // do, and that re-inlining also has the potentially to mutate the SCC
  809. // structure, the infinite inlining problem can manifest through infinite
  810. // SCC splits and merges. To avoid this, we capture the originating caller
  811. // node and the SCC containing the call edge. This is a slight over
  812. // approximation of the possible inlining decisions that must be avoided,
  813. // but is relatively efficient to store. We use C != OldC to know when
  814. // a new SCC is generated and the original SCC may be generated via merge
  815. // in later iterations.
  816. //
  817. // It is also possible that even if no new SCC is generated
  818. // (i.e., C == OldC), the original SCC could be split and then merged
  819. // into the same one as itself. and the original SCC will be added into
  820. // UR.CWorklist again, we want to catch such cases too.
  821. //
  822. // FIXME: This seems like a very heavyweight way of retaining the inline
  823. // history, we should look for a more efficient way of tracking it.
  824. if ((C != OldC || UR.CWorklist.count(OldC)) &&
  825. llvm::any_of(InlinedCallees, [&](Function *Callee) {
  826. return CG.lookupSCC(*CG.lookup(*Callee)) == OldC;
  827. })) {
  828. LLVM_DEBUG(dbgs() << "Inlined an internal call edge and split an SCC, "
  829. "retaining this to avoid infinite inlining.\n");
  830. UR.InlinedInternalEdges.insert({&N, OldC});
  831. }
  832. InlinedCallees.clear();
  833. }
  834. // Now that we've finished inlining all of the calls across this SCC, delete
  835. // all of the trivially dead functions, updating the call graph and the CGSCC
  836. // pass manager in the process.
  837. //
  838. // Note that this walks a pointer set which has non-deterministic order but
  839. // that is OK as all we do is delete things and add pointers to unordered
  840. // sets.
  841. for (Function *DeadF : DeadFunctions) {
  842. // Get the necessary information out of the call graph and nuke the
  843. // function there. Also, clear out any cached analyses.
  844. auto &DeadC = *CG.lookupSCC(*CG.lookup(*DeadF));
  845. FAM.clear(*DeadF, DeadF->getName());
  846. AM.clear(DeadC, DeadC.getName());
  847. auto &DeadRC = DeadC.getOuterRefSCC();
  848. CG.removeDeadFunction(*DeadF);
  849. // Mark the relevant parts of the call graph as invalid so we don't visit
  850. // them.
  851. UR.InvalidatedSCCs.insert(&DeadC);
  852. UR.InvalidatedRefSCCs.insert(&DeadRC);
  853. // And delete the actual function from the module.
  854. // The Advisor may use Function pointers to efficiently index various
  855. // internal maps, e.g. for memoization. Function cleanup passes like
  856. // argument promotion create new functions. It is possible for a new
  857. // function to be allocated at the address of a deleted function. We could
  858. // index using names, but that's inefficient. Alternatively, we let the
  859. // Advisor free the functions when it sees fit.
  860. DeadF->getBasicBlockList().clear();
  861. M.getFunctionList().remove(DeadF);
  862. ++NumDeleted;
  863. }
  864. if (!Changed)
  865. return PreservedAnalyses::all();
  866. // Even if we change the IR, we update the core CGSCC data structures and so
  867. // can preserve the proxy to the function analysis manager.
  868. PreservedAnalyses PA;
  869. PA.preserve<FunctionAnalysisManagerCGSCCProxy>();
  870. return PA;
  871. }
  872. ModuleInlinerWrapperPass::ModuleInlinerWrapperPass(InlineParams Params,
  873. bool Debugging,
  874. bool MandatoryFirst,
  875. InliningAdvisorMode Mode,
  876. unsigned MaxDevirtIterations)
  877. : Params(Params), Mode(Mode), MaxDevirtIterations(MaxDevirtIterations),
  878. PM(Debugging), MPM(Debugging) {
  879. // Run the inliner first. The theory is that we are walking bottom-up and so
  880. // the callees have already been fully optimized, and we want to inline them
  881. // into the callers so that our optimizations can reflect that.
  882. // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
  883. // because it makes profile annotation in the backend inaccurate.
  884. if (MandatoryFirst)
  885. PM.addPass(InlinerPass(/*OnlyMandatory*/ true));
  886. PM.addPass(InlinerPass());
  887. }
  888. PreservedAnalyses ModuleInlinerWrapperPass::run(Module &M,
  889. ModuleAnalysisManager &MAM) {
  890. auto &IAA = MAM.getResult<InlineAdvisorAnalysis>(M);
  891. if (!IAA.tryCreate(Params, Mode, CGSCCInlineReplayFile)) {
  892. M.getContext().emitError(
  893. "Could not setup Inlining Advisor for the requested "
  894. "mode and/or options");
  895. return PreservedAnalyses::all();
  896. }
  897. // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
  898. // to detect when we devirtualize indirect calls and iterate the SCC passes
  899. // in that case to try and catch knock-on inlining or function attrs
  900. // opportunities. Then we add it to the module pipeline by walking the SCCs
  901. // in postorder (or bottom-up).
  902. // If MaxDevirtIterations is 0, we just don't use the devirtualization
  903. // wrapper.
  904. if (MaxDevirtIterations == 0)
  905. MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(PM)));
  906. else
  907. MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
  908. createDevirtSCCRepeatedPass(std::move(PM), MaxDevirtIterations)));
  909. auto Ret = MPM.run(M, MAM);
  910. IAA.clear();
  911. return Ret;
  912. }