Path: blob/main/contrib/llvm-project/llvm/lib/Analysis/CallGraphSCCPass.cpp
35234 views
//===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the CallGraphSCCPass class, which is used for passes9// which are implemented as bottom-up traversals on the call graph. Because10// there may be cycles in the call graph, passes of this type operate on the11// call-graph in SCC order: that is, they process function bottom-up, except for12// recursive functions, which they process all at once.13//14//===----------------------------------------------------------------------===//1516#include "llvm/Analysis/CallGraphSCCPass.h"17#include "llvm/ADT/DenseMap.h"18#include "llvm/ADT/SCCIterator.h"19#include "llvm/ADT/Statistic.h"20#include "llvm/ADT/StringExtras.h"21#include "llvm/Analysis/CallGraph.h"22#include "llvm/IR/AbstractCallSite.h"23#include "llvm/IR/Function.h"24#include "llvm/IR/Intrinsics.h"25#include "llvm/IR/LLVMContext.h"26#include "llvm/IR/LegacyPassManagers.h"27#include "llvm/IR/Module.h"28#include "llvm/IR/OptBisect.h"29#include "llvm/IR/PassTimingInfo.h"30#include "llvm/IR/PrintPasses.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>4041using namespace llvm;4243#define DEBUG_TYPE "cgscc-passmgr"4445namespace llvm {46cl::opt<unsigned> MaxDevirtIterations("max-devirt-iterations", cl::ReallyHidden,47cl::init(4));48} // namespace llvm4950STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");5152//===----------------------------------------------------------------------===//53// CGPassManager54//55/// CGPassManager manages FPPassManagers and CallGraphSCCPasses.5657namespace {5859class CGPassManager : public ModulePass, public PMDataManager {60public:61static char ID;6263explicit CGPassManager() : ModulePass(ID) {}6465/// Execute all of the passes scheduled for execution. Keep track of66/// whether any of the passes modifies the module, and if so, return true.67bool runOnModule(Module &M) override;6869using ModulePass::doInitialization;70using ModulePass::doFinalization;7172bool doInitialization(CallGraph &CG);73bool doFinalization(CallGraph &CG);7475/// Pass Manager itself does not invalidate any analysis info.76void getAnalysisUsage(AnalysisUsage &Info) const override {77// CGPassManager walks SCC and it needs CallGraph.78Info.addRequired<CallGraphWrapperPass>();79Info.setPreservesAll();80}8182StringRef getPassName() const override { return "CallGraph Pass Manager"; }8384PMDataManager *getAsPMDataManager() override { return this; }85Pass *getAsPass() override { return this; }8687// Print passes managed by this manager88void dumpPassStructure(unsigned Offset) override {89errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";90for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {91Pass *P = getContainedPass(Index);92P->dumpPassStructure(Offset + 1);93dumpLastUses(P, Offset+1);94}95}9697Pass *getContainedPass(unsigned N) {98assert(N < PassVector.size() && "Pass number out of range!");99return static_cast<Pass *>(PassVector[N]);100}101102PassManagerType getPassManagerType() const override {103return PMT_CallGraphPassManager;104}105106private:107bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,108bool &DevirtualizedCall);109110bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,111CallGraph &CG, bool &CallGraphUpToDate,112bool &DevirtualizedCall);113bool RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,114bool IsCheckingMode);115};116117} // end anonymous namespace.118119char CGPassManager::ID = 0;120121bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,122CallGraph &CG, bool &CallGraphUpToDate,123bool &DevirtualizedCall) {124bool Changed = false;125PMDataManager *PM = P->getAsPMDataManager();126Module &M = CG.getModule();127128if (!PM) {129CallGraphSCCPass *CGSP = (CallGraphSCCPass *)P;130if (!CallGraphUpToDate) {131DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);132CallGraphUpToDate = true;133}134135{136unsigned InstrCount, SCCCount = 0;137StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;138bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();139TimeRegion PassTimer(getPassTimer(CGSP));140if (EmitICRemark)141InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);142Changed = CGSP->runOnSCC(CurSCC);143144if (EmitICRemark) {145// FIXME: Add getInstructionCount to CallGraphSCC.146SCCCount = M.getInstructionCount();147// Is there a difference in the number of instructions in the module?148if (SCCCount != InstrCount) {149// Yep. Emit a remark and update InstrCount.150int64_t Delta =151static_cast<int64_t>(SCCCount) - static_cast<int64_t>(InstrCount);152emitInstrCountChangedRemark(P, M, Delta, InstrCount,153FunctionToInstrCount);154InstrCount = SCCCount;155}156}157}158159// After the CGSCCPass is done, when assertions are enabled, use160// RefreshCallGraph to verify that the callgraph was correctly updated.161#ifndef NDEBUG162if (Changed)163RefreshCallGraph(CurSCC, CG, true);164#endif165166return Changed;167}168169assert(PM->getPassManagerType() == PMT_FunctionPassManager &&170"Invalid CGPassManager member");171FPPassManager *FPP = (FPPassManager*)P;172173// Run pass P on all functions in the current SCC.174for (CallGraphNode *CGN : CurSCC) {175if (Function *F = CGN->getFunction()) {176dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());177{178TimeRegion PassTimer(getPassTimer(FPP));179Changed |= FPP->runOnFunction(*F);180}181F->getContext().yield();182}183}184185// The function pass(es) modified the IR, they may have clobbered the186// callgraph.187if (Changed && CallGraphUpToDate) {188LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: " << P->getPassName()189<< '\n');190CallGraphUpToDate = false;191}192return Changed;193}194195/// Scan the functions in the specified CFG and resync the196/// callgraph with the call sites found in it. This is used after197/// FunctionPasses have potentially munged the callgraph, and can be used after198/// CallGraphSCC passes to verify that they correctly updated the callgraph.199///200/// This function returns true if it devirtualized an existing function call,201/// meaning it turned an indirect call into a direct call. This happens when202/// a function pass like GVN optimizes away stuff feeding the indirect call.203/// This never happens in checking mode.204bool CGPassManager::RefreshCallGraph(const CallGraphSCC &CurSCC, CallGraph &CG,205bool CheckingMode) {206DenseMap<Value *, CallGraphNode *> Calls;207208LLVM_DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()209<< " nodes:\n";210for (CallGraphNode *CGN211: CurSCC) CGN->dump(););212213bool MadeChange = false;214bool DevirtualizedCall = false;215216// Scan all functions in the SCC.217unsigned FunctionNo = 0;218for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();219SCCIdx != E; ++SCCIdx, ++FunctionNo) {220CallGraphNode *CGN = *SCCIdx;221Function *F = CGN->getFunction();222if (!F || F->isDeclaration()) continue;223224// Walk the function body looking for call sites. Sync up the call sites in225// CGN with those actually in the function.226227// Keep track of the number of direct and indirect calls that were228// invalidated and removed.229unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;230231CallGraphNode::iterator CGNEnd = CGN->end();232233auto RemoveAndCheckForDone = [&](CallGraphNode::iterator I) {234// Just remove the edge from the set of callees, keep track of whether235// I points to the last element of the vector.236bool WasLast = I + 1 == CGNEnd;237CGN->removeCallEdge(I);238239// If I pointed to the last element of the vector, we have to bail out:240// iterator checking rejects comparisons of the resultant pointer with241// end.242if (WasLast)243return true;244245CGNEnd = CGN->end();246return false;247};248249// Get the set of call sites currently in the function.250for (CallGraphNode::iterator I = CGN->begin(); I != CGNEnd;) {251// Delete "reference" call records that do not have call instruction. We252// reinsert them as needed later. However, keep them in checking mode.253if (!I->first) {254if (CheckingMode) {255++I;256continue;257}258if (RemoveAndCheckForDone(I))259break;260continue;261}262263// If this call site is null, then the function pass deleted the call264// entirely and the WeakTrackingVH nulled it out.265auto *Call = dyn_cast_or_null<CallBase>(*I->first);266if (!Call ||267// If we've already seen this call site, then the FunctionPass RAUW'd268// one call with another, which resulted in two "uses" in the edge269// list of the same call.270Calls.count(Call)) {271assert(!CheckingMode &&272"CallGraphSCCPass did not update the CallGraph correctly!");273274// If this was an indirect call site, count it.275if (!I->second->getFunction())276++NumIndirectRemoved;277else278++NumDirectRemoved;279280if (RemoveAndCheckForDone(I))281break;282continue;283}284285assert(!Calls.count(Call) && "Call site occurs in node multiple times");286287if (Call) {288Function *Callee = Call->getCalledFunction();289// Ignore intrinsics because they're not really function calls.290if (!Callee || !(Callee->isIntrinsic()))291Calls.insert(std::make_pair(Call, I->second));292}293++I;294}295296// Loop over all of the instructions in the function, getting the callsites.297// Keep track of the number of direct/indirect calls added.298unsigned NumDirectAdded = 0, NumIndirectAdded = 0;299300for (BasicBlock &BB : *F)301for (Instruction &I : BB) {302auto *Call = dyn_cast<CallBase>(&I);303if (!Call)304continue;305Function *Callee = Call->getCalledFunction();306if (Callee && Callee->isIntrinsic())307continue;308309// If we are not in checking mode, insert potential callback calls as310// references. This is not a requirement but helps to iterate over the311// functions in the right order.312if (!CheckingMode) {313forEachCallbackFunction(*Call, [&](Function *CB) {314CGN->addCalledFunction(nullptr, CG.getOrInsertFunction(CB));315});316}317318// If this call site already existed in the callgraph, just verify it319// matches up to expectations and remove it from Calls.320DenseMap<Value *, CallGraphNode *>::iterator ExistingIt =321Calls.find(Call);322if (ExistingIt != Calls.end()) {323CallGraphNode *ExistingNode = ExistingIt->second;324325// Remove from Calls since we have now seen it.326Calls.erase(ExistingIt);327328// Verify that the callee is right.329if (ExistingNode->getFunction() == Call->getCalledFunction())330continue;331332// If we are in checking mode, we are not allowed to actually mutate333// the callgraph. If this is a case where we can infer that the334// callgraph is less precise than it could be (e.g. an indirect call335// site could be turned direct), don't reject it in checking mode, and336// don't tweak it to be more precise.337if (CheckingMode && Call->getCalledFunction() &&338ExistingNode->getFunction() == nullptr)339continue;340341assert(!CheckingMode &&342"CallGraphSCCPass did not update the CallGraph correctly!");343344// If not, we either went from a direct call to indirect, indirect to345// direct, or direct to different direct.346CallGraphNode *CalleeNode;347if (Function *Callee = Call->getCalledFunction()) {348CalleeNode = CG.getOrInsertFunction(Callee);349// Keep track of whether we turned an indirect call into a direct350// one.351if (!ExistingNode->getFunction()) {352DevirtualizedCall = true;353LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Devirtualized call to '"354<< Callee->getName() << "'\n");355}356} else {357CalleeNode = CG.getCallsExternalNode();358}359360// Update the edge target in CGN.361CGN->replaceCallEdge(*Call, *Call, CalleeNode);362MadeChange = true;363continue;364}365366assert(!CheckingMode &&367"CallGraphSCCPass did not update the CallGraph correctly!");368369// If the call site didn't exist in the CGN yet, add it.370CallGraphNode *CalleeNode;371if (Function *Callee = Call->getCalledFunction()) {372CalleeNode = CG.getOrInsertFunction(Callee);373++NumDirectAdded;374} else {375CalleeNode = CG.getCallsExternalNode();376++NumIndirectAdded;377}378379CGN->addCalledFunction(Call, CalleeNode);380MadeChange = true;381}382383// We scanned the old callgraph node, removing invalidated call sites and384// then added back newly found call sites. One thing that can happen is385// that an old indirect call site was deleted and replaced with a new direct386// call. In this case, we have devirtualized a call, and CGSCCPM would like387// to iteratively optimize the new code. Unfortunately, we don't really388// have a great way to detect when this happens. As an approximation, we389// just look at whether the number of indirect calls is reduced and the390// number of direct calls is increased. There are tons of ways to fool this391// (e.g. DCE'ing an indirect call and duplicating an unrelated block with a392// direct call) but this is close enough.393if (NumIndirectRemoved > NumIndirectAdded &&394NumDirectRemoved < NumDirectAdded)395DevirtualizedCall = true;396397// After scanning this function, if we still have entries in callsites, then398// they are dangling pointers. WeakTrackingVH should save us for this, so399// abort if400// this happens.401assert(Calls.empty() && "Dangling pointers found in call sites map");402403// Periodically do an explicit clear to remove tombstones when processing404// large scc's.405if ((FunctionNo & 15) == 15)406Calls.clear();407}408409LLVM_DEBUG(if (MadeChange) {410dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";411for (CallGraphNode *CGN : CurSCC)412CGN->dump();413if (DevirtualizedCall)414dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";415} else {416dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";417});418(void)MadeChange;419420return DevirtualizedCall;421}422423/// Execute the body of the entire pass manager on the specified SCC.424/// This keeps track of whether a function pass devirtualizes425/// any calls and returns it in DevirtualizedCall.426bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,427bool &DevirtualizedCall) {428bool Changed = false;429430// Keep track of whether the callgraph is known to be up-to-date or not.431// The CGSSC pass manager runs two types of passes:432// CallGraphSCC Passes and other random function passes. Because other433// random function passes are not CallGraph aware, they may clobber the434// call graph by introducing new calls or deleting other ones. This flag435// is set to false when we run a function pass so that we know to clean up436// the callgraph when we need to run a CGSCCPass again.437bool CallGraphUpToDate = true;438439// Run all passes on current SCC.440for (unsigned PassNo = 0, e = getNumContainedPasses();441PassNo != e; ++PassNo) {442Pass *P = getContainedPass(PassNo);443444// If we're in -debug-pass=Executions mode, construct the SCC node list,445// otherwise avoid constructing this string as it is expensive.446if (isPassDebuggingExecutionsOrMore()) {447std::string Functions;448#ifndef NDEBUG449raw_string_ostream OS(Functions);450ListSeparator LS;451for (const CallGraphNode *CGN : CurSCC) {452OS << LS;453CGN->print(OS);454}455OS.flush();456#endif457dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);458}459dumpRequiredSet(P);460461initializeAnalysisImpl(P);462463#ifdef EXPENSIVE_CHECKS464uint64_t RefHash = P->structuralHash(CG.getModule());465#endif466467// Actually run this pass on the current SCC.468bool LocalChanged =469RunPassOnSCC(P, CurSCC, CG, CallGraphUpToDate, DevirtualizedCall);470471Changed |= LocalChanged;472473#ifdef EXPENSIVE_CHECKS474if (!LocalChanged && (RefHash != P->structuralHash(CG.getModule()))) {475llvm::errs() << "Pass modifies its input and doesn't report it: "476<< P->getPassName() << "\n";477llvm_unreachable("Pass modifies its input and doesn't report it");478}479#endif480if (LocalChanged)481dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");482dumpPreservedSet(P);483484verifyPreservedAnalysis(P);485if (LocalChanged)486removeNotPreservedAnalysis(P);487recordAvailableAnalysis(P);488removeDeadPasses(P, "", ON_CG_MSG);489}490491// If the callgraph was left out of date (because the last pass run was a492// functionpass), refresh it before we move on to the next SCC.493if (!CallGraphUpToDate)494DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);495return Changed;496}497498/// Execute all of the passes scheduled for execution. Keep track of499/// whether any of the passes modifies the module, and if so, return true.500bool CGPassManager::runOnModule(Module &M) {501CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();502bool Changed = doInitialization(CG);503504// Walk the callgraph in bottom-up SCC order.505scc_iterator<CallGraph*> CGI = scc_begin(&CG);506507CallGraphSCC CurSCC(CG, &CGI);508while (!CGI.isAtEnd()) {509// Copy the current SCC and increment past it so that the pass can hack510// on the SCC if it wants to without invalidating our iterator.511const std::vector<CallGraphNode *> &NodeVec = *CGI;512CurSCC.initialize(NodeVec);513++CGI;514515// At the top level, we run all the passes in this pass manager on the516// functions in this SCC. However, we support iterative compilation in the517// case where a function pass devirtualizes a call to a function. For518// example, it is very common for a function pass (often GVN or instcombine)519// to eliminate the addressing that feeds into a call. With that improved520// information, we would like the call to be an inline candidate, infer521// mod-ref information etc.522//523// Because of this, we allow iteration up to a specified iteration count.524// This only happens in the case of a devirtualized call, so we only burn525// compile time in the case that we're making progress. We also have a hard526// iteration count limit in case there is crazy code.527unsigned Iteration = 0;528bool DevirtualizedCall = false;529do {530LLVM_DEBUG(if (Iteration) dbgs()531<< " SCCPASSMGR: Re-visiting SCC, iteration #" << Iteration532<< '\n');533DevirtualizedCall = false;534Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);535} while (Iteration++ < MaxDevirtIterations && DevirtualizedCall);536537if (DevirtualizedCall)538LLVM_DEBUG(dbgs() << " CGSCCPASSMGR: Stopped iteration after "539<< Iteration540<< " times, due to -max-devirt-iterations\n");541542MaxSCCIterations.updateMax(Iteration);543}544Changed |= doFinalization(CG);545return Changed;546}547548/// Initialize CG549bool CGPassManager::doInitialization(CallGraph &CG) {550bool Changed = false;551for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {552if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {553assert(PM->getPassManagerType() == PMT_FunctionPassManager &&554"Invalid CGPassManager member");555Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());556} else {557Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);558}559}560return Changed;561}562563/// Finalize CG564bool CGPassManager::doFinalization(CallGraph &CG) {565bool Changed = false;566for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {567if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {568assert(PM->getPassManagerType() == PMT_FunctionPassManager &&569"Invalid CGPassManager member");570Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());571} else {572Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);573}574}575return Changed;576}577578//===----------------------------------------------------------------------===//579// CallGraphSCC Implementation580//===----------------------------------------------------------------------===//581582/// This informs the SCC and the pass manager that the specified583/// Old node has been deleted, and New is to be used in its place.584void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {585assert(Old != New && "Should not replace node with self");586for (unsigned i = 0; ; ++i) {587assert(i != Nodes.size() && "Node not in SCC");588if (Nodes[i] != Old) continue;589if (New)590Nodes[i] = New;591else592Nodes.erase(Nodes.begin() + i);593break;594}595596// Update the active scc_iterator so that it doesn't contain dangling597// pointers to the old CallGraphNode.598scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;599CGI->ReplaceNode(Old, New);600}601602void CallGraphSCC::DeleteNode(CallGraphNode *Old) {603ReplaceNode(Old, /*New=*/nullptr);604}605606//===----------------------------------------------------------------------===//607// CallGraphSCCPass Implementation608//===----------------------------------------------------------------------===//609610/// Assign pass manager to manage this pass.611void CallGraphSCCPass::assignPassManager(PMStack &PMS,612PassManagerType PreferredType) {613// Find CGPassManager614while (!PMS.empty() &&615PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)616PMS.pop();617618assert(!PMS.empty() && "Unable to handle Call Graph Pass");619CGPassManager *CGP;620621if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)622CGP = (CGPassManager*)PMS.top();623else {624// Create new Call Graph SCC Pass Manager if it does not exist.625assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");626PMDataManager *PMD = PMS.top();627628// [1] Create new Call Graph Pass Manager629CGP = new CGPassManager();630631// [2] Set up new manager's top level manager632PMTopLevelManager *TPM = PMD->getTopLevelManager();633TPM->addIndirectPassManager(CGP);634635// [3] Assign manager to manage this new manager. This may create636// and push new managers into PMS637Pass *P = CGP;638TPM->schedulePass(P);639640// [4] Push new manager into PMS641PMS.push(CGP);642}643644CGP->add(this);645}646647/// For this class, we declare that we require and preserve the call graph.648/// If the derived class implements this method, it should649/// always explicitly call the implementation here.650void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {651AU.addRequired<CallGraphWrapperPass>();652AU.addPreserved<CallGraphWrapperPass>();653}654655//===----------------------------------------------------------------------===//656// PrintCallGraphPass Implementation657//===----------------------------------------------------------------------===//658659namespace {660661/// PrintCallGraphPass - Print a Module corresponding to a call graph.662///663class PrintCallGraphPass : public CallGraphSCCPass {664std::string Banner;665raw_ostream &OS; // raw_ostream to print on.666667public:668static char ID;669670PrintCallGraphPass(const std::string &B, raw_ostream &OS)671: CallGraphSCCPass(ID), Banner(B), OS(OS) {}672673void getAnalysisUsage(AnalysisUsage &AU) const override {674AU.setPreservesAll();675}676677bool runOnSCC(CallGraphSCC &SCC) override {678bool BannerPrinted = false;679auto PrintBannerOnce = [&]() {680if (BannerPrinted)681return;682OS << Banner;683BannerPrinted = true;684};685686bool NeedModule = llvm::forcePrintModuleIR();687if (isFunctionInPrintList("*") && NeedModule) {688PrintBannerOnce();689OS << "\n";690SCC.getCallGraph().getModule().print(OS, nullptr);691return false;692}693bool FoundFunction = false;694for (CallGraphNode *CGN : SCC) {695if (Function *F = CGN->getFunction()) {696if (!F->isDeclaration() && isFunctionInPrintList(F->getName())) {697FoundFunction = true;698if (!NeedModule) {699PrintBannerOnce();700F->print(OS);701}702}703} else if (isFunctionInPrintList("*")) {704PrintBannerOnce();705OS << "\nPrinting <null> Function\n";706}707}708if (NeedModule && FoundFunction) {709PrintBannerOnce();710OS << "\n";711SCC.getCallGraph().getModule().print(OS, nullptr);712}713return false;714}715716StringRef getPassName() const override { return "Print CallGraph IR"; }717};718719} // end anonymous namespace.720721char PrintCallGraphPass::ID = 0;722723Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &OS,724const std::string &Banner) const {725return new PrintCallGraphPass(Banner, OS);726}727728static std::string getDescription(const CallGraphSCC &SCC) {729std::string Desc = "SCC (";730ListSeparator LS;731for (CallGraphNode *CGN : SCC) {732Desc += LS;733Function *F = CGN->getFunction();734if (F)735Desc += F->getName();736else737Desc += "<<null function>>";738}739Desc += ")";740return Desc;741}742743bool CallGraphSCCPass::skipSCC(CallGraphSCC &SCC) const {744OptPassGate &Gate =745SCC.getCallGraph().getModule().getContext().getOptPassGate();746return Gate.isEnabled() &&747!Gate.shouldRunPass(this->getPassName(), getDescription(SCC));748}749750char DummyCGSCCPass::ID = 0;751752INITIALIZE_PASS(DummyCGSCCPass, "DummyCGSCCPass", "DummyCGSCCPass", false,753false)754755756