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