CallGraphSCCPass.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
  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 CallGraphSCCPass class, which is used for passes
  10. // which are implemented as bottom-up traversals on the call graph. Because
  11. // there may be cycles in the call graph, passes of this type operate on the
  12. // call-graph in SCC order: that is, they process function bottom-up, except for
  13. // recursive functions, which they process all at once.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Analysis/CallGraphSCCPass.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/ADT/SCCIterator.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/Analysis/CallGraph.h"
  21. #include "llvm/IR/AbstractCallSite.h"
  22. #include "llvm/IR/Function.h"
  23. #include "llvm/IR/Intrinsics.h"
  24. #include "llvm/IR/LLVMContext.h"
  25. #include "llvm/IR/LegacyPassManagers.h"
  26. #include "llvm/IR/Module.h"
  27. #include "llvm/IR/OptBisect.h"
  28. #include "llvm/IR/PassTimingInfo.h"
  29. #include "llvm/IR/PrintPasses.h"
  30. #include "llvm/IR/StructuralHash.h"
  31. #include "llvm/Pass.h"
  32. #include "llvm/Support/CommandLine.h"
  33. #include "llvm/Support/Debug.h"
  34. #include "llvm/Support/Timer.h"
  35. #include "llvm/Support/raw_ostream.h"
  36. #include <cassert>
  37. #include <string>
  38. #include <utility>
  39. #include <vector>
  40. using namespace llvm;
  41. #define DEBUG_TYPE "cgscc-passmgr"
  42. namespace llvm {
  43. cl::opt<unsigned> MaxDevirtIterations("max-devirt-iterations", cl::ReallyHidden,
  44. cl::init(4));
  45. }
  46. STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
  47. //===----------------------------------------------------------------------===//
  48. // CGPassManager
  49. //
  50. /// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
  51. namespace {
  52. class CGPassManager : public ModulePass, public PMDataManager {
  53. public:
  54. static char ID;
  55. explicit CGPassManager() : ModulePass(ID) {}
  56. /// Execute all of the passes scheduled for execution. Keep track of
  57. /// whether any of the passes modifies the module, and if so, return true.
  58. bool runOnModule(Module &M) override;
  59. using ModulePass::doInitialization;
  60. using ModulePass::doFinalization;
  61. bool doInitialization(CallGraph &CG);
  62. bool doFinalization(CallGraph &CG);
  63. /// Pass Manager itself does not invalidate any analysis info.
  64. void getAnalysisUsage(AnalysisUsage &Info) const override {
  65. // CGPassManager walks SCC and it needs CallGraph.
  66. Info.addRequired<CallGraphWrapperPass>();
  67. Info.setPreservesAll();
  68. }
  69. StringRef getPassName() const override { return "CallGraph Pass Manager"; }
  70. PMDataManager *getAsPMDataManager() override { return this; }
  71. Pass *getAsPass() override { return this; }
  72. // Print passes managed by this manager
  73. void dumpPassStructure(unsigned Offset) override {
  74. errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
  75. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  76. Pass *P = getContainedPass(Index);
  77. P->dumpPassStructure(Offset + 1);
  78. dumpLastUses(P, Offset+1);
  79. }
  80. }
  81. Pass *getContainedPass(unsigned N) {
  82. assert(N < PassVector.size() && "Pass number out of range!");
  83. return static_cast<Pass *>(PassVector[N]);
  84. }
  85. PassManagerType getPassManagerType() const override {
  86. return PMT_CallGraphPassManager;
  87. }
  88. private:
  89. bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
  90. bool &DevirtualizedCall);
  91. bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
  92. CallGraph &CG, bool &CallGraphUpToDate,
  93. bool &DevirtualizedCall);
  94. bool RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,
  95. bool IsCheckingMode);
  96. };
  97. } // end anonymous namespace.
  98. char CGPassManager::ID = 0;
  99. bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
  100. CallGraph &CG, bool &CallGraphUpToDate,
  101. bool &DevirtualizedCall) {
  102. bool Changed = false;
  103. PMDataManager *PM = P->getAsPMDataManager();
  104. Module &M = CG.getModule();
  105. if (!PM) {
  106. CallGraphSCCPass *CGSP = (CallGraphSCCPass *)P;
  107. if (!CallGraphUpToDate) {
  108. DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
  109. CallGraphUpToDate = true;
  110. }
  111. {
  112. unsigned InstrCount, SCCCount = 0;
  113. StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
  114. bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
  115. TimeRegion PassTimer(getPassTimer(CGSP));
  116. if (EmitICRemark)
  117. InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
  118. Changed = CGSP->runOnSCC(CurSCC);
  119. if (EmitICRemark) {
  120. // FIXME: Add getInstructionCount to CallGraphSCC.
  121. SCCCount = M.getInstructionCount();
  122. // Is there a difference in the number of instructions in the module?
  123. if (SCCCount != InstrCount) {
  124. // Yep. Emit a remark and update InstrCount.
  125. int64_t Delta =
  126. static_cast<int64_t>(SCCCount) - static_cast<int64_t>(InstrCount);
  127. emitInstrCountChangedRemark(P, M, Delta, InstrCount,
  128. FunctionToInstrCount);
  129. InstrCount = SCCCount;
  130. }
  131. }
  132. }
  133. // After the CGSCCPass is done, when assertions are enabled, use
  134. // RefreshCallGraph to verify that the callgraph was correctly updated.
  135. #ifndef NDEBUG
  136. if (Changed)
  137. RefreshCallGraph(CurSCC, CG, true);
  138. #endif
  139. return Changed;
  140. }
  141. assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
  142. "Invalid CGPassManager member");
  143. FPPassManager *FPP = (FPPassManager*)P;
  144. // Run pass P on all functions in the current SCC.
  145. for (CallGraphNode *CGN : CurSCC) {
  146. if (Function *F = CGN->getFunction()) {
  147. dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
  148. {
  149. TimeRegion PassTimer(getPassTimer(FPP));
  150. Changed |= FPP->runOnFunction(*F);
  151. }
  152. F->getContext().yield();
  153. }
  154. }
  155. // The function pass(es) modified the IR, they may have clobbered the
  156. // callgraph.
  157. if (Changed && CallGraphUpToDate) {
  158. LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: " << P->getPassName()
  159. << '\n');
  160. CallGraphUpToDate = false;
  161. }
  162. return Changed;
  163. }
  164. /// Scan the functions in the specified CFG and resync the
  165. /// callgraph with the call sites found in it. This is used after
  166. /// FunctionPasses have potentially munged the callgraph, and can be used after
  167. /// CallGraphSCC passes to verify that they correctly updated the callgraph.
  168. ///
  169. /// This function returns true if it devirtualized an existing function call,
  170. /// meaning it turned an indirect call into a direct call. This happens when
  171. /// a function pass like GVN optimizes away stuff feeding the indirect call.
  172. /// This never happens in checking mode.
  173. bool CGPassManager::RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,
  174. bool CheckingMode) {
  175. DenseMap<Value *, CallGraphNode *> Calls;
  176. LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
  177. << " nodes:\n";
  178. for (CallGraphNode *CGN
  179. : CurSCC) CGN->dump(););
  180. bool MadeChange = false;
  181. bool DevirtualizedCall = false;
  182. // Scan all functions in the SCC.
  183. unsigned FunctionNo = 0;
  184. for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
  185. SCCIdx != E; ++SCCIdx, ++FunctionNo) {
  186. CallGraphNode *CGN = *SCCIdx;
  187. Function *F = CGN->getFunction();
  188. if (!F || F->isDeclaration()) continue;
  189. // Walk the function body looking for call sites. Sync up the call sites in
  190. // CGN with those actually in the function.
  191. // Keep track of the number of direct and indirect calls that were
  192. // invalidated and removed.
  193. unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
  194. CallGraphNode::iterator CGNEnd = CGN->end();
  195. auto RemoveAndCheckForDone = [&](CallGraphNode::iterator I) {
  196. // Just remove the edge from the set of callees, keep track of whether
  197. // I points to the last element of the vector.
  198. bool WasLast = I + 1 == CGNEnd;
  199. CGN->removeCallEdge(I);
  200. // If I pointed to the last element of the vector, we have to bail out:
  201. // iterator checking rejects comparisons of the resultant pointer with
  202. // end.
  203. if (WasLast)
  204. return true;
  205. CGNEnd = CGN->end();
  206. return false;
  207. };
  208. // Get the set of call sites currently in the function.
  209. for (CallGraphNode::iterator I = CGN->begin(); I != CGNEnd;) {
  210. // Delete "reference" call records that do not have call instruction. We
  211. // reinsert them as needed later. However, keep them in checking mode.
  212. if (!I->first) {
  213. if (CheckingMode) {
  214. ++I;
  215. continue;
  216. }
  217. if (RemoveAndCheckForDone(I))
  218. break;
  219. continue;
  220. }
  221. // If this call site is null, then the function pass deleted the call
  222. // entirely and the WeakTrackingVH nulled it out.
  223. auto *Call = dyn_cast_or_null<CallBase>(*I->first);
  224. if (!Call ||
  225. // If we've already seen this call site, then the FunctionPass RAUW'd
  226. // one call with another, which resulted in two "uses" in the edge
  227. // list of the same call.
  228. Calls.count(Call) ||
  229. // If the call edge is not from a call or invoke, or it is a
  230. // instrinsic call, then the function pass RAUW'd a call with
  231. // another value. This can happen when constant folding happens
  232. // of well known functions etc.
  233. (Call->getCalledFunction() &&
  234. Call->getCalledFunction()->isIntrinsic() &&
  235. Intrinsic::isLeaf(Call->getCalledFunction()->getIntrinsicID()))) {
  236. assert(!CheckingMode &&
  237. "CallGraphSCCPass did not update the CallGraph correctly!");
  238. // If this was an indirect call site, count it.
  239. if (!I->second->getFunction())
  240. ++NumIndirectRemoved;
  241. else
  242. ++NumDirectRemoved;
  243. if (RemoveAndCheckForDone(I))
  244. break;
  245. continue;
  246. }
  247. assert(!Calls.count(Call) && "Call site occurs in node multiple times");
  248. if (Call) {
  249. Function *Callee = Call->getCalledFunction();
  250. // Ignore intrinsics because they're not really function calls.
  251. if (!Callee || !(Callee->isIntrinsic()))
  252. Calls.insert(std::make_pair(Call, I->second));
  253. }
  254. ++I;
  255. }
  256. // Loop over all of the instructions in the function, getting the callsites.
  257. // Keep track of the number of direct/indirect calls added.
  258. unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
  259. for (BasicBlock &BB : *F)
  260. for (Instruction &I : BB) {
  261. auto *Call = dyn_cast<CallBase>(&I);
  262. if (!Call)
  263. continue;
  264. Function *Callee = Call->getCalledFunction();
  265. if (Callee && Callee->isIntrinsic())
  266. continue;
  267. // If we are not in checking mode, insert potential callback calls as
  268. // references. This is not a requirement but helps to iterate over the
  269. // functions in the right order.
  270. if (!CheckingMode) {
  271. forEachCallbackFunction(*Call, [&](Function *CB) {
  272. CGN->addCalledFunction(nullptr, CG.getOrInsertFunction(CB));
  273. });
  274. }
  275. // If this call site already existed in the callgraph, just verify it
  276. // matches up to expectations and remove it from Calls.
  277. DenseMap<Value *, CallGraphNode *>::iterator ExistingIt =
  278. Calls.find(Call);
  279. if (ExistingIt != Calls.end()) {
  280. CallGraphNode *ExistingNode = ExistingIt->second;
  281. // Remove from Calls since we have now seen it.
  282. Calls.erase(ExistingIt);
  283. // Verify that the callee is right.
  284. if (ExistingNode->getFunction() == Call->getCalledFunction())
  285. continue;
  286. // If we are in checking mode, we are not allowed to actually mutate
  287. // the callgraph. If this is a case where we can infer that the
  288. // callgraph is less precise than it could be (e.g. an indirect call
  289. // site could be turned direct), don't reject it in checking mode, and
  290. // don't tweak it to be more precise.
  291. if (CheckingMode && Call->getCalledFunction() &&
  292. ExistingNode->getFunction() == nullptr)
  293. continue;
  294. assert(!CheckingMode &&
  295. "CallGraphSCCPass did not update the CallGraph correctly!");
  296. // If not, we either went from a direct call to indirect, indirect to
  297. // direct, or direct to different direct.
  298. CallGraphNode *CalleeNode;
  299. if (Function *Callee = Call->getCalledFunction()) {
  300. CalleeNode = CG.getOrInsertFunction(Callee);
  301. // Keep track of whether we turned an indirect call into a direct
  302. // one.
  303. if (!ExistingNode->getFunction()) {
  304. DevirtualizedCall = true;
  305. LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '"
  306. << Callee->getName() << "'\n");
  307. }
  308. } else {
  309. CalleeNode = CG.getCallsExternalNode();
  310. }
  311. // Update the edge target in CGN.
  312. CGN->replaceCallEdge(*Call, *Call, CalleeNode);
  313. MadeChange = true;
  314. continue;
  315. }
  316. assert(!CheckingMode &&
  317. "CallGraphSCCPass did not update the CallGraph correctly!");
  318. // If the call site didn't exist in the CGN yet, add it.
  319. CallGraphNode *CalleeNode;
  320. if (Function *Callee = Call->getCalledFunction()) {
  321. CalleeNode = CG.getOrInsertFunction(Callee);
  322. ++NumDirectAdded;
  323. } else {
  324. CalleeNode = CG.getCallsExternalNode();
  325. ++NumIndirectAdded;
  326. }
  327. CGN->addCalledFunction(Call, CalleeNode);
  328. MadeChange = true;
  329. }
  330. // We scanned the old callgraph node, removing invalidated call sites and
  331. // then added back newly found call sites. One thing that can happen is
  332. // that an old indirect call site was deleted and replaced with a new direct
  333. // call. In this case, we have devirtualized a call, and CGSCCPM would like
  334. // to iteratively optimize the new code. Unfortunately, we don't really
  335. // have a great way to detect when this happens. As an approximation, we
  336. // just look at whether the number of indirect calls is reduced and the
  337. // number of direct calls is increased. There are tons of ways to fool this
  338. // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
  339. // direct call) but this is close enough.
  340. if (NumIndirectRemoved > NumIndirectAdded &&
  341. NumDirectRemoved < NumDirectAdded)
  342. DevirtualizedCall = true;
  343. // After scanning this function, if we still have entries in callsites, then
  344. // they are dangling pointers. WeakTrackingVH should save us for this, so
  345. // abort if
  346. // this happens.
  347. assert(Calls.empty() && "Dangling pointers found in call sites map");
  348. // Periodically do an explicit clear to remove tombstones when processing
  349. // large scc's.
  350. if ((FunctionNo & 15) == 15)
  351. Calls.clear();
  352. }
  353. LLVM_DEBUG(if (MadeChange) {
  354. dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
  355. for (CallGraphNode *CGN : CurSCC)
  356. CGN->dump();
  357. if (DevirtualizedCall)
  358. dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
  359. } else {
  360. dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
  361. });
  362. (void)MadeChange;
  363. return DevirtualizedCall;
  364. }
  365. /// Execute the body of the entire pass manager on the specified SCC.
  366. /// This keeps track of whether a function pass devirtualizes
  367. /// any calls and returns it in DevirtualizedCall.
  368. bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
  369. bool &DevirtualizedCall) {
  370. bool Changed = false;
  371. // Keep track of whether the callgraph is known to be up-to-date or not.
  372. // The CGSSC pass manager runs two types of passes:
  373. // CallGraphSCC Passes and other random function passes. Because other
  374. // random function passes are not CallGraph aware, they may clobber the
  375. // call graph by introducing new calls or deleting other ones. This flag
  376. // is set to false when we run a function pass so that we know to clean up
  377. // the callgraph when we need to run a CGSCCPass again.
  378. bool CallGraphUpToDate = true;
  379. // Run all passes on current SCC.
  380. for (unsigned PassNo = 0, e = getNumContainedPasses();
  381. PassNo != e; ++PassNo) {
  382. Pass *P = getContainedPass(PassNo);
  383. // If we're in -debug-pass=Executions mode, construct the SCC node list,
  384. // otherwise avoid constructing this string as it is expensive.
  385. if (isPassDebuggingExecutionsOrMore()) {
  386. std::string Functions;
  387. #ifndef NDEBUG
  388. raw_string_ostream OS(Functions);
  389. ListSeparator LS;
  390. for (const CallGraphNode *CGN : CurSCC) {
  391. OS << LS;
  392. CGN->print(OS);
  393. }
  394. OS.flush();
  395. #endif
  396. dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
  397. }
  398. dumpRequiredSet(P);
  399. initializeAnalysisImpl(P);
  400. #ifdef EXPENSIVE_CHECKS
  401. uint64_t RefHash = StructuralHash(CG.getModule());
  402. #endif
  403. // Actually run this pass on the current SCC.
  404. bool LocalChanged =
  405. RunPassOnSCC(P, CurSCC, CG, CallGraphUpToDate, DevirtualizedCall);
  406. Changed |= LocalChanged;
  407. #ifdef EXPENSIVE_CHECKS
  408. if (!LocalChanged && (RefHash != StructuralHash(CG.getModule()))) {
  409. llvm::errs() << "Pass modifies its input and doesn't report it: "
  410. << P->getPassName() << "\n";
  411. llvm_unreachable("Pass modifies its input and doesn't report it");
  412. }
  413. #endif
  414. if (LocalChanged)
  415. dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
  416. dumpPreservedSet(P);
  417. verifyPreservedAnalysis(P);
  418. if (LocalChanged)
  419. removeNotPreservedAnalysis(P);
  420. recordAvailableAnalysis(P);
  421. removeDeadPasses(P, "", ON_CG_MSG);
  422. }
  423. // If the callgraph was left out of date (because the last pass run was a
  424. // functionpass), refresh it before we move on to the next SCC.
  425. if (!CallGraphUpToDate)
  426. DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
  427. return Changed;
  428. }
  429. /// Execute all of the passes scheduled for execution. Keep track of
  430. /// whether any of the passes modifies the module, and if so, return true.
  431. bool CGPassManager::runOnModule(Module &M) {
  432. CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
  433. bool Changed = doInitialization(CG);
  434. // Walk the callgraph in bottom-up SCC order.
  435. scc_iterator<CallGraph*> CGI = scc_begin(&CG);
  436. CallGraphSCC CurSCC(CG, &CGI);
  437. while (!CGI.isAtEnd()) {
  438. // Copy the current SCC and increment past it so that the pass can hack
  439. // on the SCC if it wants to without invalidating our iterator.
  440. const std::vector<CallGraphNode *> &NodeVec = *CGI;
  441. CurSCC.initialize(NodeVec);
  442. ++CGI;
  443. // At the top level, we run all the passes in this pass manager on the
  444. // functions in this SCC. However, we support iterative compilation in the
  445. // case where a function pass devirtualizes a call to a function. For
  446. // example, it is very common for a function pass (often GVN or instcombine)
  447. // to eliminate the addressing that feeds into a call. With that improved
  448. // information, we would like the call to be an inline candidate, infer
  449. // mod-ref information etc.
  450. //
  451. // Because of this, we allow iteration up to a specified iteration count.
  452. // This only happens in the case of a devirtualized call, so we only burn
  453. // compile time in the case that we're making progress. We also have a hard
  454. // iteration count limit in case there is crazy code.
  455. unsigned Iteration = 0;
  456. bool DevirtualizedCall = false;
  457. do {
  458. LLVM_DEBUG(if (Iteration) dbgs()
  459. << " SCCPASSMGR: Re-visiting SCC, iteration #" << Iteration
  460. << '\n');
  461. DevirtualizedCall = false;
  462. Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
  463. } while (Iteration++ < MaxDevirtIterations && DevirtualizedCall);
  464. if (DevirtualizedCall)
  465. LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after "
  466. << Iteration
  467. << " times, due to -max-devirt-iterations\n");
  468. MaxSCCIterations.updateMax(Iteration);
  469. }
  470. Changed |= doFinalization(CG);
  471. return Changed;
  472. }
  473. /// Initialize CG
  474. bool CGPassManager::doInitialization(CallGraph &CG) {
  475. bool Changed = false;
  476. for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
  477. if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
  478. assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
  479. "Invalid CGPassManager member");
  480. Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
  481. } else {
  482. Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
  483. }
  484. }
  485. return Changed;
  486. }
  487. /// Finalize CG
  488. bool CGPassManager::doFinalization(CallGraph &CG) {
  489. bool Changed = false;
  490. for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
  491. if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
  492. assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
  493. "Invalid CGPassManager member");
  494. Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
  495. } else {
  496. Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
  497. }
  498. }
  499. return Changed;
  500. }
  501. //===----------------------------------------------------------------------===//
  502. // CallGraphSCC Implementation
  503. //===----------------------------------------------------------------------===//
  504. /// This informs the SCC and the pass manager that the specified
  505. /// Old node has been deleted, and New is to be used in its place.
  506. void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
  507. assert(Old != New && "Should not replace node with self");
  508. for (unsigned i = 0; ; ++i) {
  509. assert(i != Nodes.size() && "Node not in SCC");
  510. if (Nodes[i] != Old) continue;
  511. if (New)
  512. Nodes[i] = New;
  513. else
  514. Nodes.erase(Nodes.begin() + i);
  515. break;
  516. }
  517. // Update the active scc_iterator so that it doesn't contain dangling
  518. // pointers to the old CallGraphNode.
  519. scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;
  520. CGI->ReplaceNode(Old, New);
  521. }
  522. void CallGraphSCC::DeleteNode(CallGraphNode *Old) {
  523. ReplaceNode(Old, /*New=*/nullptr);
  524. }
  525. //===----------------------------------------------------------------------===//
  526. // CallGraphSCCPass Implementation
  527. //===----------------------------------------------------------------------===//
  528. /// Assign pass manager to manage this pass.
  529. void CallGraphSCCPass::assignPassManager(PMStack &PMS,
  530. PassManagerType PreferredType) {
  531. // Find CGPassManager
  532. while (!PMS.empty() &&
  533. PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
  534. PMS.pop();
  535. assert(!PMS.empty() && "Unable to handle Call Graph Pass");
  536. CGPassManager *CGP;
  537. if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
  538. CGP = (CGPassManager*)PMS.top();
  539. else {
  540. // Create new Call Graph SCC Pass Manager if it does not exist.
  541. assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
  542. PMDataManager *PMD = PMS.top();
  543. // [1] Create new Call Graph Pass Manager
  544. CGP = new CGPassManager();
  545. // [2] Set up new manager's top level manager
  546. PMTopLevelManager *TPM = PMD->getTopLevelManager();
  547. TPM->addIndirectPassManager(CGP);
  548. // [3] Assign manager to manage this new manager. This may create
  549. // and push new managers into PMS
  550. Pass *P = CGP;
  551. TPM->schedulePass(P);
  552. // [4] Push new manager into PMS
  553. PMS.push(CGP);
  554. }
  555. CGP->add(this);
  556. }
  557. /// For this class, we declare that we require and preserve the call graph.
  558. /// If the derived class implements this method, it should
  559. /// always explicitly call the implementation here.
  560. void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
  561. AU.addRequired<CallGraphWrapperPass>();
  562. AU.addPreserved<CallGraphWrapperPass>();
  563. }
  564. //===----------------------------------------------------------------------===//
  565. // PrintCallGraphPass Implementation
  566. //===----------------------------------------------------------------------===//
  567. namespace {
  568. /// PrintCallGraphPass - Print a Module corresponding to a call graph.
  569. ///
  570. class PrintCallGraphPass : public CallGraphSCCPass {
  571. std::string Banner;
  572. raw_ostream &OS; // raw_ostream to print on.
  573. public:
  574. static char ID;
  575. PrintCallGraphPass(const std::string &B, raw_ostream &OS)
  576. : CallGraphSCCPass(ID), Banner(B), OS(OS) {}
  577. void getAnalysisUsage(AnalysisUsage &AU) const override {
  578. AU.setPreservesAll();
  579. }
  580. bool runOnSCC(CallGraphSCC &SCC) override {
  581. bool BannerPrinted = false;
  582. auto PrintBannerOnce = [&]() {
  583. if (BannerPrinted)
  584. return;
  585. OS << Banner;
  586. BannerPrinted = true;
  587. };
  588. bool NeedModule = llvm::forcePrintModuleIR();
  589. if (isFunctionInPrintList("*") && NeedModule) {
  590. PrintBannerOnce();
  591. OS << "\n";
  592. SCC.getCallGraph().getModule().print(OS, nullptr);
  593. return false;
  594. }
  595. bool FoundFunction = false;
  596. for (CallGraphNode *CGN : SCC) {
  597. if (Function *F = CGN->getFunction()) {
  598. if (!F->isDeclaration() && isFunctionInPrintList(F->getName())) {
  599. FoundFunction = true;
  600. if (!NeedModule) {
  601. PrintBannerOnce();
  602. F->print(OS);
  603. }
  604. }
  605. } else if (isFunctionInPrintList("*")) {
  606. PrintBannerOnce();
  607. OS << "\nPrinting <null> Function\n";
  608. }
  609. }
  610. if (NeedModule && FoundFunction) {
  611. PrintBannerOnce();
  612. OS << "\n";
  613. SCC.getCallGraph().getModule().print(OS, nullptr);
  614. }
  615. return false;
  616. }
  617. StringRef getPassName() const override { return "Print CallGraph IR"; }
  618. };
  619. } // end anonymous namespace.
  620. char PrintCallGraphPass::ID = 0;
  621. Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &OS,
  622. const std::string &Banner) const {
  623. return new PrintCallGraphPass(Banner, OS);
  624. }
  625. static std::string getDescription(const CallGraphSCC &SCC) {
  626. std::string Desc = "SCC (";
  627. ListSeparator LS;
  628. for (CallGraphNode *CGN : SCC) {
  629. Desc += LS;
  630. Function *F = CGN->getFunction();
  631. if (F)
  632. Desc += F->getName();
  633. else
  634. Desc += "<<null function>>";
  635. }
  636. Desc += ")";
  637. return Desc;
  638. }
  639. bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const {
  640. OptPassGate &Gate =
  641. SCC.getCallGraph().getModule().getContext().getOptPassGate();
  642. return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(SCC));
  643. }
  644. char DummyCGSCCPass::ID = 0;
  645. INITIALIZE_PASS(DummyCGSCCPass, "DummyCGSCCPass", "DummyCGSCCPass", false,
  646. false)