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