1*0b57cec5SDimitry Andric //===- CallGraph.cpp - Build a Module's call graph ------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #include "llvm/Analysis/CallGraph.h" 10*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 11*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 12*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 135ffd83dbSDimitry Andric #include "llvm/IR/AbstractCallSite.h" 14*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 155ffd83dbSDimitry Andric #include "llvm/IR/IntrinsicInst.h" 16*0b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h" 17480093f4SDimitry Andric #include "llvm/IR/Module.h" 18*0b57cec5SDimitry Andric #include "llvm/IR/PassManager.h" 19480093f4SDimitry Andric #include "llvm/InitializePasses.h" 20*0b57cec5SDimitry Andric #include "llvm/Pass.h" 21*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 22*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 23*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 24*0b57cec5SDimitry Andric #include <algorithm> 25*0b57cec5SDimitry Andric #include <cassert> 26*0b57cec5SDimitry Andric 27*0b57cec5SDimitry Andric using namespace llvm; 28*0b57cec5SDimitry Andric 29*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 30*0b57cec5SDimitry Andric // Implementations of the CallGraph class methods. 31*0b57cec5SDimitry Andric // 32*0b57cec5SDimitry Andric 33*0b57cec5SDimitry Andric CallGraph::CallGraph(Module &M) 34*0b57cec5SDimitry Andric : M(M), ExternalCallingNode(getOrInsertFunction(nullptr)), 355ffd83dbSDimitry Andric CallsExternalNode(std::make_unique<CallGraphNode>(this, nullptr)) { 365ffd83dbSDimitry Andric // Add every interesting function to the call graph. 37*0b57cec5SDimitry Andric for (Function &F : M) 385ffd83dbSDimitry Andric if (!isDbgInfoIntrinsic(F.getIntrinsicID())) 39*0b57cec5SDimitry Andric addToCallGraph(&F); 40*0b57cec5SDimitry Andric } 41*0b57cec5SDimitry Andric 42*0b57cec5SDimitry Andric CallGraph::CallGraph(CallGraph &&Arg) 43*0b57cec5SDimitry Andric : M(Arg.M), FunctionMap(std::move(Arg.FunctionMap)), 44*0b57cec5SDimitry Andric ExternalCallingNode(Arg.ExternalCallingNode), 45*0b57cec5SDimitry Andric CallsExternalNode(std::move(Arg.CallsExternalNode)) { 46*0b57cec5SDimitry Andric Arg.FunctionMap.clear(); 47*0b57cec5SDimitry Andric Arg.ExternalCallingNode = nullptr; 485ffd83dbSDimitry Andric 495ffd83dbSDimitry Andric // Update parent CG for all call graph's nodes. 505ffd83dbSDimitry Andric CallsExternalNode->CG = this; 515ffd83dbSDimitry Andric for (auto &P : FunctionMap) 525ffd83dbSDimitry Andric P.second->CG = this; 53*0b57cec5SDimitry Andric } 54*0b57cec5SDimitry Andric 55*0b57cec5SDimitry Andric CallGraph::~CallGraph() { 56*0b57cec5SDimitry Andric // CallsExternalNode is not in the function map, delete it explicitly. 57*0b57cec5SDimitry Andric if (CallsExternalNode) 58*0b57cec5SDimitry Andric CallsExternalNode->allReferencesDropped(); 59*0b57cec5SDimitry Andric 60*0b57cec5SDimitry Andric // Reset all node's use counts to zero before deleting them to prevent an 61*0b57cec5SDimitry Andric // assertion from firing. 62*0b57cec5SDimitry Andric #ifndef NDEBUG 63*0b57cec5SDimitry Andric for (auto &I : FunctionMap) 64*0b57cec5SDimitry Andric I.second->allReferencesDropped(); 65*0b57cec5SDimitry Andric #endif 66*0b57cec5SDimitry Andric } 67*0b57cec5SDimitry Andric 685ffd83dbSDimitry Andric bool CallGraph::invalidate(Module &, const PreservedAnalyses &PA, 695ffd83dbSDimitry Andric ModuleAnalysisManager::Invalidator &) { 705ffd83dbSDimitry Andric // Check whether the analysis, all analyses on functions, or the function's 715ffd83dbSDimitry Andric // CFG have been preserved. 725ffd83dbSDimitry Andric auto PAC = PA.getChecker<CallGraphAnalysis>(); 735ffd83dbSDimitry Andric return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Module>>() || 745ffd83dbSDimitry Andric PAC.preservedSet<CFGAnalyses>()); 755ffd83dbSDimitry Andric } 765ffd83dbSDimitry Andric 77*0b57cec5SDimitry Andric void CallGraph::addToCallGraph(Function *F) { 78*0b57cec5SDimitry Andric CallGraphNode *Node = getOrInsertFunction(F); 79*0b57cec5SDimitry Andric 805ffd83dbSDimitry Andric // If this function has external linkage or has its address taken and 815ffd83dbSDimitry Andric // it is not a callback, then anything could call it. 825ffd83dbSDimitry Andric if (!F->hasLocalLinkage() || 835ffd83dbSDimitry Andric F->hasAddressTaken(nullptr, /*IgnoreCallbackUses=*/true)) 84*0b57cec5SDimitry Andric ExternalCallingNode->addCalledFunction(nullptr, Node); 85*0b57cec5SDimitry Andric 865ffd83dbSDimitry Andric populateCallGraphNode(Node); 875ffd83dbSDimitry Andric } 885ffd83dbSDimitry Andric 895ffd83dbSDimitry Andric void CallGraph::populateCallGraphNode(CallGraphNode *Node) { 905ffd83dbSDimitry Andric Function *F = Node->getFunction(); 915ffd83dbSDimitry Andric 92*0b57cec5SDimitry Andric // If this function is not defined in this translation unit, it could call 93*0b57cec5SDimitry Andric // anything. 94*0b57cec5SDimitry Andric if (F->isDeclaration() && !F->isIntrinsic()) 95*0b57cec5SDimitry Andric Node->addCalledFunction(nullptr, CallsExternalNode.get()); 96*0b57cec5SDimitry Andric 97*0b57cec5SDimitry Andric // Look for calls by this function. 98*0b57cec5SDimitry Andric for (BasicBlock &BB : *F) 99*0b57cec5SDimitry Andric for (Instruction &I : BB) { 100*0b57cec5SDimitry Andric if (auto *Call = dyn_cast<CallBase>(&I)) { 101*0b57cec5SDimitry Andric const Function *Callee = Call->getCalledFunction(); 102*0b57cec5SDimitry Andric if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID())) 103*0b57cec5SDimitry Andric // Indirect calls of intrinsics are not allowed so no need to check. 104*0b57cec5SDimitry Andric // We can be more precise here by using TargetArg returned by 105*0b57cec5SDimitry Andric // Intrinsic::isLeaf. 106*0b57cec5SDimitry Andric Node->addCalledFunction(Call, CallsExternalNode.get()); 107*0b57cec5SDimitry Andric else if (!Callee->isIntrinsic()) 108*0b57cec5SDimitry Andric Node->addCalledFunction(Call, getOrInsertFunction(Callee)); 1095ffd83dbSDimitry Andric 1105ffd83dbSDimitry Andric // Add reference to callback functions. 1115ffd83dbSDimitry Andric forEachCallbackFunction(*Call, [=](Function *CB) { 1125ffd83dbSDimitry Andric Node->addCalledFunction(nullptr, getOrInsertFunction(CB)); 1135ffd83dbSDimitry Andric }); 114*0b57cec5SDimitry Andric } 115*0b57cec5SDimitry Andric } 116*0b57cec5SDimitry Andric } 117*0b57cec5SDimitry Andric 118*0b57cec5SDimitry Andric void CallGraph::print(raw_ostream &OS) const { 119*0b57cec5SDimitry Andric // Print in a deterministic order by sorting CallGraphNodes by name. We do 120*0b57cec5SDimitry Andric // this here to avoid slowing down the non-printing fast path. 121*0b57cec5SDimitry Andric 122*0b57cec5SDimitry Andric SmallVector<CallGraphNode *, 16> Nodes; 123*0b57cec5SDimitry Andric Nodes.reserve(FunctionMap.size()); 124*0b57cec5SDimitry Andric 125*0b57cec5SDimitry Andric for (const auto &I : *this) 126*0b57cec5SDimitry Andric Nodes.push_back(I.second.get()); 127*0b57cec5SDimitry Andric 128*0b57cec5SDimitry Andric llvm::sort(Nodes, [](CallGraphNode *LHS, CallGraphNode *RHS) { 129*0b57cec5SDimitry Andric if (Function *LF = LHS->getFunction()) 130*0b57cec5SDimitry Andric if (Function *RF = RHS->getFunction()) 131*0b57cec5SDimitry Andric return LF->getName() < RF->getName(); 132*0b57cec5SDimitry Andric 133*0b57cec5SDimitry Andric return RHS->getFunction() != nullptr; 134*0b57cec5SDimitry Andric }); 135*0b57cec5SDimitry Andric 136*0b57cec5SDimitry Andric for (CallGraphNode *CN : Nodes) 137*0b57cec5SDimitry Andric CN->print(OS); 138*0b57cec5SDimitry Andric } 139*0b57cec5SDimitry Andric 140*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 141*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void CallGraph::dump() const { print(dbgs()); } 142*0b57cec5SDimitry Andric #endif 143*0b57cec5SDimitry Andric 1445ffd83dbSDimitry Andric void CallGraph::ReplaceExternalCallEdge(CallGraphNode *Old, 1455ffd83dbSDimitry Andric CallGraphNode *New) { 1465ffd83dbSDimitry Andric for (auto &CR : ExternalCallingNode->CalledFunctions) 1475ffd83dbSDimitry Andric if (CR.second == Old) { 1485ffd83dbSDimitry Andric CR.second->DropRef(); 1495ffd83dbSDimitry Andric CR.second = New; 1505ffd83dbSDimitry Andric CR.second->AddRef(); 1515ffd83dbSDimitry Andric } 1525ffd83dbSDimitry Andric } 1535ffd83dbSDimitry Andric 154*0b57cec5SDimitry Andric // removeFunctionFromModule - Unlink the function from this module, returning 155*0b57cec5SDimitry Andric // it. Because this removes the function from the module, the call graph node 156*0b57cec5SDimitry Andric // is destroyed. This is only valid if the function does not call any other 157*0b57cec5SDimitry Andric // functions (ie, there are no edges in it's CGN). The easiest way to do this 158*0b57cec5SDimitry Andric // is to dropAllReferences before calling this. 159*0b57cec5SDimitry Andric // 160*0b57cec5SDimitry Andric Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) { 161*0b57cec5SDimitry Andric assert(CGN->empty() && "Cannot remove function from call " 162*0b57cec5SDimitry Andric "graph if it references other functions!"); 163*0b57cec5SDimitry Andric Function *F = CGN->getFunction(); // Get the function for the call graph node 164*0b57cec5SDimitry Andric FunctionMap.erase(F); // Remove the call graph node from the map 165*0b57cec5SDimitry Andric 166*0b57cec5SDimitry Andric M.getFunctionList().remove(F); 167*0b57cec5SDimitry Andric return F; 168*0b57cec5SDimitry Andric } 169*0b57cec5SDimitry Andric 170*0b57cec5SDimitry Andric /// spliceFunction - Replace the function represented by this node by another. 171*0b57cec5SDimitry Andric /// This does not rescan the body of the function, so it is suitable when 172*0b57cec5SDimitry Andric /// splicing the body of the old function to the new while also updating all 173*0b57cec5SDimitry Andric /// callers from old to new. 174*0b57cec5SDimitry Andric void CallGraph::spliceFunction(const Function *From, const Function *To) { 175*0b57cec5SDimitry Andric assert(FunctionMap.count(From) && "No CallGraphNode for function!"); 176*0b57cec5SDimitry Andric assert(!FunctionMap.count(To) && 177*0b57cec5SDimitry Andric "Pointing CallGraphNode at a function that already exists"); 178*0b57cec5SDimitry Andric FunctionMapTy::iterator I = FunctionMap.find(From); 179*0b57cec5SDimitry Andric I->second->F = const_cast<Function*>(To); 180*0b57cec5SDimitry Andric FunctionMap[To] = std::move(I->second); 181*0b57cec5SDimitry Andric FunctionMap.erase(I); 182*0b57cec5SDimitry Andric } 183*0b57cec5SDimitry Andric 184*0b57cec5SDimitry Andric // getOrInsertFunction - This method is identical to calling operator[], but 185*0b57cec5SDimitry Andric // it will insert a new CallGraphNode for the specified function if one does 186*0b57cec5SDimitry Andric // not already exist. 187*0b57cec5SDimitry Andric CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) { 188*0b57cec5SDimitry Andric auto &CGN = FunctionMap[F]; 189*0b57cec5SDimitry Andric if (CGN) 190*0b57cec5SDimitry Andric return CGN.get(); 191*0b57cec5SDimitry Andric 192*0b57cec5SDimitry Andric assert((!F || F->getParent() == &M) && "Function not in current module!"); 1935ffd83dbSDimitry Andric CGN = std::make_unique<CallGraphNode>(this, const_cast<Function *>(F)); 194*0b57cec5SDimitry Andric return CGN.get(); 195*0b57cec5SDimitry Andric } 196*0b57cec5SDimitry Andric 197*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 198*0b57cec5SDimitry Andric // Implementations of the CallGraphNode class methods. 199*0b57cec5SDimitry Andric // 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric void CallGraphNode::print(raw_ostream &OS) const { 202*0b57cec5SDimitry Andric if (Function *F = getFunction()) 203*0b57cec5SDimitry Andric OS << "Call graph node for function: '" << F->getName() << "'"; 204*0b57cec5SDimitry Andric else 205*0b57cec5SDimitry Andric OS << "Call graph node <<null function>>"; 206*0b57cec5SDimitry Andric 207*0b57cec5SDimitry Andric OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n'; 208*0b57cec5SDimitry Andric 209*0b57cec5SDimitry Andric for (const auto &I : *this) { 210*0b57cec5SDimitry Andric OS << " CS<" << I.first << "> calls "; 211*0b57cec5SDimitry Andric if (Function *FI = I.second->getFunction()) 212*0b57cec5SDimitry Andric OS << "function '" << FI->getName() <<"'\n"; 213*0b57cec5SDimitry Andric else 214*0b57cec5SDimitry Andric OS << "external node\n"; 215*0b57cec5SDimitry Andric } 216*0b57cec5SDimitry Andric OS << '\n'; 217*0b57cec5SDimitry Andric } 218*0b57cec5SDimitry Andric 219*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 220*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void CallGraphNode::dump() const { print(dbgs()); } 221*0b57cec5SDimitry Andric #endif 222*0b57cec5SDimitry Andric 223*0b57cec5SDimitry Andric /// removeCallEdgeFor - This method removes the edge in the node for the 224*0b57cec5SDimitry Andric /// specified call site. Note that this method takes linear time, so it 225*0b57cec5SDimitry Andric /// should be used sparingly. 226*0b57cec5SDimitry Andric void CallGraphNode::removeCallEdgeFor(CallBase &Call) { 227*0b57cec5SDimitry Andric for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 228*0b57cec5SDimitry Andric assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); 2295ffd83dbSDimitry Andric if (I->first && *I->first == &Call) { 230*0b57cec5SDimitry Andric I->second->DropRef(); 231*0b57cec5SDimitry Andric *I = CalledFunctions.back(); 232*0b57cec5SDimitry Andric CalledFunctions.pop_back(); 2335ffd83dbSDimitry Andric 2345ffd83dbSDimitry Andric // Remove all references to callback functions if there are any. 2355ffd83dbSDimitry Andric forEachCallbackFunction(Call, [=](Function *CB) { 2365ffd83dbSDimitry Andric removeOneAbstractEdgeTo(CG->getOrInsertFunction(CB)); 2375ffd83dbSDimitry Andric }); 238*0b57cec5SDimitry Andric return; 239*0b57cec5SDimitry Andric } 240*0b57cec5SDimitry Andric } 241*0b57cec5SDimitry Andric } 242*0b57cec5SDimitry Andric 243*0b57cec5SDimitry Andric // removeAnyCallEdgeTo - This method removes any call edges from this node to 244*0b57cec5SDimitry Andric // the specified callee function. This takes more time to execute than 245*0b57cec5SDimitry Andric // removeCallEdgeTo, so it should not be used unless necessary. 246*0b57cec5SDimitry Andric void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) { 247*0b57cec5SDimitry Andric for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i) 248*0b57cec5SDimitry Andric if (CalledFunctions[i].second == Callee) { 249*0b57cec5SDimitry Andric Callee->DropRef(); 250*0b57cec5SDimitry Andric CalledFunctions[i] = CalledFunctions.back(); 251*0b57cec5SDimitry Andric CalledFunctions.pop_back(); 252*0b57cec5SDimitry Andric --i; --e; 253*0b57cec5SDimitry Andric } 254*0b57cec5SDimitry Andric } 255*0b57cec5SDimitry Andric 256*0b57cec5SDimitry Andric /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite 257*0b57cec5SDimitry Andric /// from this node to the specified callee function. 258*0b57cec5SDimitry Andric void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) { 259*0b57cec5SDimitry Andric for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 260*0b57cec5SDimitry Andric assert(I != CalledFunctions.end() && "Cannot find callee to remove!"); 261*0b57cec5SDimitry Andric CallRecord &CR = *I; 2625ffd83dbSDimitry Andric if (CR.second == Callee && !CR.first) { 263*0b57cec5SDimitry Andric Callee->DropRef(); 264*0b57cec5SDimitry Andric *I = CalledFunctions.back(); 265*0b57cec5SDimitry Andric CalledFunctions.pop_back(); 266*0b57cec5SDimitry Andric return; 267*0b57cec5SDimitry Andric } 268*0b57cec5SDimitry Andric } 269*0b57cec5SDimitry Andric } 270*0b57cec5SDimitry Andric 271*0b57cec5SDimitry Andric /// replaceCallEdge - This method replaces the edge in the node for the 272*0b57cec5SDimitry Andric /// specified call site with a new one. Note that this method takes linear 273*0b57cec5SDimitry Andric /// time, so it should be used sparingly. 274*0b57cec5SDimitry Andric void CallGraphNode::replaceCallEdge(CallBase &Call, CallBase &NewCall, 275*0b57cec5SDimitry Andric CallGraphNode *NewNode) { 276*0b57cec5SDimitry Andric for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { 277*0b57cec5SDimitry Andric assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); 2785ffd83dbSDimitry Andric if (I->first && *I->first == &Call) { 279*0b57cec5SDimitry Andric I->second->DropRef(); 280*0b57cec5SDimitry Andric I->first = &NewCall; 281*0b57cec5SDimitry Andric I->second = NewNode; 282*0b57cec5SDimitry Andric NewNode->AddRef(); 2835ffd83dbSDimitry Andric 2845ffd83dbSDimitry Andric // Refresh callback references. 2855ffd83dbSDimitry Andric forEachCallbackFunction(Call, [=](Function *CB) { 2865ffd83dbSDimitry Andric removeOneAbstractEdgeTo(CG->getOrInsertFunction(CB)); 2875ffd83dbSDimitry Andric }); 2885ffd83dbSDimitry Andric forEachCallbackFunction(NewCall, [=](Function *CB) { 2895ffd83dbSDimitry Andric addCalledFunction(nullptr, CG->getOrInsertFunction(CB)); 2905ffd83dbSDimitry Andric }); 291*0b57cec5SDimitry Andric return; 292*0b57cec5SDimitry Andric } 293*0b57cec5SDimitry Andric } 294*0b57cec5SDimitry Andric } 295*0b57cec5SDimitry Andric 296*0b57cec5SDimitry Andric // Provide an explicit template instantiation for the static ID. 297*0b57cec5SDimitry Andric AnalysisKey CallGraphAnalysis::Key; 298*0b57cec5SDimitry Andric 299*0b57cec5SDimitry Andric PreservedAnalyses CallGraphPrinterPass::run(Module &M, 300*0b57cec5SDimitry Andric ModuleAnalysisManager &AM) { 301*0b57cec5SDimitry Andric AM.getResult<CallGraphAnalysis>(M).print(OS); 302*0b57cec5SDimitry Andric return PreservedAnalyses::all(); 303*0b57cec5SDimitry Andric } 304*0b57cec5SDimitry Andric 305*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 306*0b57cec5SDimitry Andric // Out-of-line definitions of CallGraphAnalysis class members. 307*0b57cec5SDimitry Andric // 308*0b57cec5SDimitry Andric 309*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 310*0b57cec5SDimitry Andric // Implementations of the CallGraphWrapperPass class methods. 311*0b57cec5SDimitry Andric // 312*0b57cec5SDimitry Andric 313*0b57cec5SDimitry Andric CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) { 314*0b57cec5SDimitry Andric initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry()); 315*0b57cec5SDimitry Andric } 316*0b57cec5SDimitry Andric 317*0b57cec5SDimitry Andric CallGraphWrapperPass::~CallGraphWrapperPass() = default; 318*0b57cec5SDimitry Andric 319*0b57cec5SDimitry Andric void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 320*0b57cec5SDimitry Andric AU.setPreservesAll(); 321*0b57cec5SDimitry Andric } 322*0b57cec5SDimitry Andric 323*0b57cec5SDimitry Andric bool CallGraphWrapperPass::runOnModule(Module &M) { 324*0b57cec5SDimitry Andric // All the real work is done in the constructor for the CallGraph. 325*0b57cec5SDimitry Andric G.reset(new CallGraph(M)); 326*0b57cec5SDimitry Andric return false; 327*0b57cec5SDimitry Andric } 328*0b57cec5SDimitry Andric 329*0b57cec5SDimitry Andric INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction", 330*0b57cec5SDimitry Andric false, true) 331*0b57cec5SDimitry Andric 332*0b57cec5SDimitry Andric char CallGraphWrapperPass::ID = 0; 333*0b57cec5SDimitry Andric 334*0b57cec5SDimitry Andric void CallGraphWrapperPass::releaseMemory() { G.reset(); } 335*0b57cec5SDimitry Andric 336*0b57cec5SDimitry Andric void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const { 337*0b57cec5SDimitry Andric if (!G) { 338*0b57cec5SDimitry Andric OS << "No call graph has been built!\n"; 339*0b57cec5SDimitry Andric return; 340*0b57cec5SDimitry Andric } 341*0b57cec5SDimitry Andric 342*0b57cec5SDimitry Andric // Just delegate. 343*0b57cec5SDimitry Andric G->print(OS); 344*0b57cec5SDimitry Andric } 345*0b57cec5SDimitry Andric 346*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 347*0b57cec5SDimitry Andric LLVM_DUMP_METHOD 348*0b57cec5SDimitry Andric void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); } 349*0b57cec5SDimitry Andric #endif 350*0b57cec5SDimitry Andric 351*0b57cec5SDimitry Andric namespace { 352*0b57cec5SDimitry Andric 353*0b57cec5SDimitry Andric struct CallGraphPrinterLegacyPass : public ModulePass { 354*0b57cec5SDimitry Andric static char ID; // Pass ID, replacement for typeid 355*0b57cec5SDimitry Andric 356*0b57cec5SDimitry Andric CallGraphPrinterLegacyPass() : ModulePass(ID) { 357*0b57cec5SDimitry Andric initializeCallGraphPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); 358*0b57cec5SDimitry Andric } 359*0b57cec5SDimitry Andric 360*0b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 361*0b57cec5SDimitry Andric AU.setPreservesAll(); 362*0b57cec5SDimitry Andric AU.addRequiredTransitive<CallGraphWrapperPass>(); 363*0b57cec5SDimitry Andric } 364*0b57cec5SDimitry Andric 365*0b57cec5SDimitry Andric bool runOnModule(Module &M) override { 366*0b57cec5SDimitry Andric getAnalysis<CallGraphWrapperPass>().print(errs(), &M); 367*0b57cec5SDimitry Andric return false; 368*0b57cec5SDimitry Andric } 369*0b57cec5SDimitry Andric }; 370*0b57cec5SDimitry Andric 371*0b57cec5SDimitry Andric } // end anonymous namespace 372*0b57cec5SDimitry Andric 373*0b57cec5SDimitry Andric char CallGraphPrinterLegacyPass::ID = 0; 374*0b57cec5SDimitry Andric 375*0b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(CallGraphPrinterLegacyPass, "print-callgraph", 376*0b57cec5SDimitry Andric "Print a call graph", true, true) 377*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) 378*0b57cec5SDimitry Andric INITIALIZE_PASS_END(CallGraphPrinterLegacyPass, "print-callgraph", 379*0b57cec5SDimitry Andric "Print a call graph", true, true) 380