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