CallGraphSCCPass.cpp 26 KB

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