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