1*9880d681SAndroid Build Coastguard Worker //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CallGraph.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallSite.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
17*9880d681SAndroid Build Coastguard Worker using namespace llvm;
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
20*9880d681SAndroid Build Coastguard Worker // Implementations of the CallGraph class methods.
21*9880d681SAndroid Build Coastguard Worker //
22*9880d681SAndroid Build Coastguard Worker
CallGraph(Module & M)23*9880d681SAndroid Build Coastguard Worker CallGraph::CallGraph(Module &M)
24*9880d681SAndroid Build Coastguard Worker : M(M), Root(nullptr), ExternalCallingNode(getOrInsertFunction(nullptr)),
25*9880d681SAndroid Build Coastguard Worker CallsExternalNode(llvm::make_unique<CallGraphNode>(nullptr)) {
26*9880d681SAndroid Build Coastguard Worker // Add every function to the call graph.
27*9880d681SAndroid Build Coastguard Worker for (Function &F : M)
28*9880d681SAndroid Build Coastguard Worker addToCallGraph(&F);
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker // If we didn't find a main function, use the external call graph node
31*9880d681SAndroid Build Coastguard Worker if (!Root)
32*9880d681SAndroid Build Coastguard Worker Root = ExternalCallingNode;
33*9880d681SAndroid Build Coastguard Worker }
34*9880d681SAndroid Build Coastguard Worker
CallGraph(CallGraph && Arg)35*9880d681SAndroid Build Coastguard Worker CallGraph::CallGraph(CallGraph &&Arg)
36*9880d681SAndroid Build Coastguard Worker : M(Arg.M), FunctionMap(std::move(Arg.FunctionMap)), Root(Arg.Root),
37*9880d681SAndroid Build Coastguard Worker ExternalCallingNode(Arg.ExternalCallingNode),
38*9880d681SAndroid Build Coastguard Worker CallsExternalNode(std::move(Arg.CallsExternalNode)) {
39*9880d681SAndroid Build Coastguard Worker Arg.FunctionMap.clear();
40*9880d681SAndroid Build Coastguard Worker Arg.Root = nullptr;
41*9880d681SAndroid Build Coastguard Worker Arg.ExternalCallingNode = nullptr;
42*9880d681SAndroid Build Coastguard Worker }
43*9880d681SAndroid Build Coastguard Worker
~CallGraph()44*9880d681SAndroid Build Coastguard Worker CallGraph::~CallGraph() {
45*9880d681SAndroid Build Coastguard Worker // CallsExternalNode is not in the function map, delete it explicitly.
46*9880d681SAndroid Build Coastguard Worker if (CallsExternalNode)
47*9880d681SAndroid Build Coastguard Worker CallsExternalNode->allReferencesDropped();
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker // Reset all node's use counts to zero before deleting them to prevent an
50*9880d681SAndroid Build Coastguard Worker // assertion from firing.
51*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
52*9880d681SAndroid Build Coastguard Worker for (auto &I : FunctionMap)
53*9880d681SAndroid Build Coastguard Worker I.second->allReferencesDropped();
54*9880d681SAndroid Build Coastguard Worker #endif
55*9880d681SAndroid Build Coastguard Worker }
56*9880d681SAndroid Build Coastguard Worker
addToCallGraph(Function * F)57*9880d681SAndroid Build Coastguard Worker void CallGraph::addToCallGraph(Function *F) {
58*9880d681SAndroid Build Coastguard Worker CallGraphNode *Node = getOrInsertFunction(F);
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard Worker // If this function has external linkage, anything could call it.
61*9880d681SAndroid Build Coastguard Worker if (!F->hasLocalLinkage()) {
62*9880d681SAndroid Build Coastguard Worker ExternalCallingNode->addCalledFunction(CallSite(), Node);
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker // Found the entry point?
65*9880d681SAndroid Build Coastguard Worker if (F->getName() == "main") {
66*9880d681SAndroid Build Coastguard Worker if (Root) // Found multiple external mains? Don't pick one.
67*9880d681SAndroid Build Coastguard Worker Root = ExternalCallingNode;
68*9880d681SAndroid Build Coastguard Worker else
69*9880d681SAndroid Build Coastguard Worker Root = Node; // Found a main, keep track of it!
70*9880d681SAndroid Build Coastguard Worker }
71*9880d681SAndroid Build Coastguard Worker }
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker // If this function has its address taken, anything could call it.
74*9880d681SAndroid Build Coastguard Worker if (F->hasAddressTaken())
75*9880d681SAndroid Build Coastguard Worker ExternalCallingNode->addCalledFunction(CallSite(), Node);
76*9880d681SAndroid Build Coastguard Worker
77*9880d681SAndroid Build Coastguard Worker // If this function is not defined in this translation unit, it could call
78*9880d681SAndroid Build Coastguard Worker // anything.
79*9880d681SAndroid Build Coastguard Worker if (F->isDeclaration() && !F->isIntrinsic())
80*9880d681SAndroid Build Coastguard Worker Node->addCalledFunction(CallSite(), CallsExternalNode.get());
81*9880d681SAndroid Build Coastguard Worker
82*9880d681SAndroid Build Coastguard Worker // Look for calls by this function.
83*9880d681SAndroid Build Coastguard Worker for (BasicBlock &BB : *F)
84*9880d681SAndroid Build Coastguard Worker for (Instruction &I : BB) {
85*9880d681SAndroid Build Coastguard Worker if (auto CS = CallSite(&I)) {
86*9880d681SAndroid Build Coastguard Worker const Function *Callee = CS.getCalledFunction();
87*9880d681SAndroid Build Coastguard Worker if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID()))
88*9880d681SAndroid Build Coastguard Worker // Indirect calls of intrinsics are not allowed so no need to check.
89*9880d681SAndroid Build Coastguard Worker // We can be more precise here by using TargetArg returned by
90*9880d681SAndroid Build Coastguard Worker // Intrinsic::isLeaf.
91*9880d681SAndroid Build Coastguard Worker Node->addCalledFunction(CS, CallsExternalNode.get());
92*9880d681SAndroid Build Coastguard Worker else if (!Callee->isIntrinsic())
93*9880d681SAndroid Build Coastguard Worker Node->addCalledFunction(CS, getOrInsertFunction(Callee));
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker }
96*9880d681SAndroid Build Coastguard Worker }
97*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS) const98*9880d681SAndroid Build Coastguard Worker void CallGraph::print(raw_ostream &OS) const {
99*9880d681SAndroid Build Coastguard Worker OS << "CallGraph Root is: ";
100*9880d681SAndroid Build Coastguard Worker if (Function *F = Root->getFunction())
101*9880d681SAndroid Build Coastguard Worker OS << F->getName() << "\n";
102*9880d681SAndroid Build Coastguard Worker else {
103*9880d681SAndroid Build Coastguard Worker OS << "<<null function: 0x" << Root << ">>\n";
104*9880d681SAndroid Build Coastguard Worker }
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker // Print in a deterministic order by sorting CallGraphNodes by name. We do
107*9880d681SAndroid Build Coastguard Worker // this here to avoid slowing down the non-printing fast path.
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker SmallVector<CallGraphNode *, 16> Nodes;
110*9880d681SAndroid Build Coastguard Worker Nodes.reserve(FunctionMap.size());
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker for (const auto &I : *this)
113*9880d681SAndroid Build Coastguard Worker Nodes.push_back(I.second.get());
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker std::sort(Nodes.begin(), Nodes.end(),
116*9880d681SAndroid Build Coastguard Worker [](CallGraphNode *LHS, CallGraphNode *RHS) {
117*9880d681SAndroid Build Coastguard Worker if (Function *LF = LHS->getFunction())
118*9880d681SAndroid Build Coastguard Worker if (Function *RF = RHS->getFunction())
119*9880d681SAndroid Build Coastguard Worker return LF->getName() < RF->getName();
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard Worker return RHS->getFunction() != nullptr;
122*9880d681SAndroid Build Coastguard Worker });
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker for (CallGraphNode *CN : Nodes)
125*9880d681SAndroid Build Coastguard Worker CN->print(OS);
126*9880d681SAndroid Build Coastguard Worker }
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD
dump() const129*9880d681SAndroid Build Coastguard Worker void CallGraph::dump() const { print(dbgs()); }
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker // removeFunctionFromModule - Unlink the function from this module, returning
132*9880d681SAndroid Build Coastguard Worker // it. Because this removes the function from the module, the call graph node
133*9880d681SAndroid Build Coastguard Worker // is destroyed. This is only valid if the function does not call any other
134*9880d681SAndroid Build Coastguard Worker // functions (ie, there are no edges in it's CGN). The easiest way to do this
135*9880d681SAndroid Build Coastguard Worker // is to dropAllReferences before calling this.
136*9880d681SAndroid Build Coastguard Worker //
removeFunctionFromModule(CallGraphNode * CGN)137*9880d681SAndroid Build Coastguard Worker Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
138*9880d681SAndroid Build Coastguard Worker assert(CGN->empty() && "Cannot remove function from call "
139*9880d681SAndroid Build Coastguard Worker "graph if it references other functions!");
140*9880d681SAndroid Build Coastguard Worker Function *F = CGN->getFunction(); // Get the function for the call graph node
141*9880d681SAndroid Build Coastguard Worker FunctionMap.erase(F); // Remove the call graph node from the map
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker M.getFunctionList().remove(F);
144*9880d681SAndroid Build Coastguard Worker return F;
145*9880d681SAndroid Build Coastguard Worker }
146*9880d681SAndroid Build Coastguard Worker
147*9880d681SAndroid Build Coastguard Worker /// spliceFunction - Replace the function represented by this node by another.
148*9880d681SAndroid Build Coastguard Worker /// This does not rescan the body of the function, so it is suitable when
149*9880d681SAndroid Build Coastguard Worker /// splicing the body of the old function to the new while also updating all
150*9880d681SAndroid Build Coastguard Worker /// callers from old to new.
151*9880d681SAndroid Build Coastguard Worker ///
spliceFunction(const Function * From,const Function * To)152*9880d681SAndroid Build Coastguard Worker void CallGraph::spliceFunction(const Function *From, const Function *To) {
153*9880d681SAndroid Build Coastguard Worker assert(FunctionMap.count(From) && "No CallGraphNode for function!");
154*9880d681SAndroid Build Coastguard Worker assert(!FunctionMap.count(To) &&
155*9880d681SAndroid Build Coastguard Worker "Pointing CallGraphNode at a function that already exists");
156*9880d681SAndroid Build Coastguard Worker FunctionMapTy::iterator I = FunctionMap.find(From);
157*9880d681SAndroid Build Coastguard Worker I->second->F = const_cast<Function*>(To);
158*9880d681SAndroid Build Coastguard Worker FunctionMap[To] = std::move(I->second);
159*9880d681SAndroid Build Coastguard Worker FunctionMap.erase(I);
160*9880d681SAndroid Build Coastguard Worker }
161*9880d681SAndroid Build Coastguard Worker
162*9880d681SAndroid Build Coastguard Worker // getOrInsertFunction - This method is identical to calling operator[], but
163*9880d681SAndroid Build Coastguard Worker // it will insert a new CallGraphNode for the specified function if one does
164*9880d681SAndroid Build Coastguard Worker // not already exist.
getOrInsertFunction(const Function * F)165*9880d681SAndroid Build Coastguard Worker CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
166*9880d681SAndroid Build Coastguard Worker auto &CGN = FunctionMap[F];
167*9880d681SAndroid Build Coastguard Worker if (CGN)
168*9880d681SAndroid Build Coastguard Worker return CGN.get();
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker assert((!F || F->getParent() == &M) && "Function not in current module!");
171*9880d681SAndroid Build Coastguard Worker CGN = llvm::make_unique<CallGraphNode>(const_cast<Function *>(F));
172*9880d681SAndroid Build Coastguard Worker return CGN.get();
173*9880d681SAndroid Build Coastguard Worker }
174*9880d681SAndroid Build Coastguard Worker
175*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
176*9880d681SAndroid Build Coastguard Worker // Implementations of the CallGraphNode class methods.
177*9880d681SAndroid Build Coastguard Worker //
178*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS) const179*9880d681SAndroid Build Coastguard Worker void CallGraphNode::print(raw_ostream &OS) const {
180*9880d681SAndroid Build Coastguard Worker if (Function *F = getFunction())
181*9880d681SAndroid Build Coastguard Worker OS << "Call graph node for function: '" << F->getName() << "'";
182*9880d681SAndroid Build Coastguard Worker else
183*9880d681SAndroid Build Coastguard Worker OS << "Call graph node <<null function>>";
184*9880d681SAndroid Build Coastguard Worker
185*9880d681SAndroid Build Coastguard Worker OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard Worker for (const auto &I : *this) {
188*9880d681SAndroid Build Coastguard Worker OS << " CS<" << I.first << "> calls ";
189*9880d681SAndroid Build Coastguard Worker if (Function *FI = I.second->getFunction())
190*9880d681SAndroid Build Coastguard Worker OS << "function '" << FI->getName() <<"'\n";
191*9880d681SAndroid Build Coastguard Worker else
192*9880d681SAndroid Build Coastguard Worker OS << "external node\n";
193*9880d681SAndroid Build Coastguard Worker }
194*9880d681SAndroid Build Coastguard Worker OS << '\n';
195*9880d681SAndroid Build Coastguard Worker }
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD
dump() const198*9880d681SAndroid Build Coastguard Worker void CallGraphNode::dump() const { print(dbgs()); }
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard Worker /// removeCallEdgeFor - This method removes the edge in the node for the
201*9880d681SAndroid Build Coastguard Worker /// specified call site. Note that this method takes linear time, so it
202*9880d681SAndroid Build Coastguard Worker /// should be used sparingly.
removeCallEdgeFor(CallSite CS)203*9880d681SAndroid Build Coastguard Worker void CallGraphNode::removeCallEdgeFor(CallSite CS) {
204*9880d681SAndroid Build Coastguard Worker for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
205*9880d681SAndroid Build Coastguard Worker assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
206*9880d681SAndroid Build Coastguard Worker if (I->first == CS.getInstruction()) {
207*9880d681SAndroid Build Coastguard Worker I->second->DropRef();
208*9880d681SAndroid Build Coastguard Worker *I = CalledFunctions.back();
209*9880d681SAndroid Build Coastguard Worker CalledFunctions.pop_back();
210*9880d681SAndroid Build Coastguard Worker return;
211*9880d681SAndroid Build Coastguard Worker }
212*9880d681SAndroid Build Coastguard Worker }
213*9880d681SAndroid Build Coastguard Worker }
214*9880d681SAndroid Build Coastguard Worker
215*9880d681SAndroid Build Coastguard Worker // removeAnyCallEdgeTo - This method removes any call edges from this node to
216*9880d681SAndroid Build Coastguard Worker // the specified callee function. This takes more time to execute than
217*9880d681SAndroid Build Coastguard Worker // removeCallEdgeTo, so it should not be used unless necessary.
removeAnyCallEdgeTo(CallGraphNode * Callee)218*9880d681SAndroid Build Coastguard Worker void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
219*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
220*9880d681SAndroid Build Coastguard Worker if (CalledFunctions[i].second == Callee) {
221*9880d681SAndroid Build Coastguard Worker Callee->DropRef();
222*9880d681SAndroid Build Coastguard Worker CalledFunctions[i] = CalledFunctions.back();
223*9880d681SAndroid Build Coastguard Worker CalledFunctions.pop_back();
224*9880d681SAndroid Build Coastguard Worker --i; --e;
225*9880d681SAndroid Build Coastguard Worker }
226*9880d681SAndroid Build Coastguard Worker }
227*9880d681SAndroid Build Coastguard Worker
228*9880d681SAndroid Build Coastguard Worker /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
229*9880d681SAndroid Build Coastguard Worker /// from this node to the specified callee function.
removeOneAbstractEdgeTo(CallGraphNode * Callee)230*9880d681SAndroid Build Coastguard Worker void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
231*9880d681SAndroid Build Coastguard Worker for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
232*9880d681SAndroid Build Coastguard Worker assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
233*9880d681SAndroid Build Coastguard Worker CallRecord &CR = *I;
234*9880d681SAndroid Build Coastguard Worker if (CR.second == Callee && CR.first == nullptr) {
235*9880d681SAndroid Build Coastguard Worker Callee->DropRef();
236*9880d681SAndroid Build Coastguard Worker *I = CalledFunctions.back();
237*9880d681SAndroid Build Coastguard Worker CalledFunctions.pop_back();
238*9880d681SAndroid Build Coastguard Worker return;
239*9880d681SAndroid Build Coastguard Worker }
240*9880d681SAndroid Build Coastguard Worker }
241*9880d681SAndroid Build Coastguard Worker }
242*9880d681SAndroid Build Coastguard Worker
243*9880d681SAndroid Build Coastguard Worker /// replaceCallEdge - This method replaces the edge in the node for the
244*9880d681SAndroid Build Coastguard Worker /// specified call site with a new one. Note that this method takes linear
245*9880d681SAndroid Build Coastguard Worker /// time, so it should be used sparingly.
replaceCallEdge(CallSite CS,CallSite NewCS,CallGraphNode * NewNode)246*9880d681SAndroid Build Coastguard Worker void CallGraphNode::replaceCallEdge(CallSite CS,
247*9880d681SAndroid Build Coastguard Worker CallSite NewCS, CallGraphNode *NewNode){
248*9880d681SAndroid Build Coastguard Worker for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
249*9880d681SAndroid Build Coastguard Worker assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
250*9880d681SAndroid Build Coastguard Worker if (I->first == CS.getInstruction()) {
251*9880d681SAndroid Build Coastguard Worker I->second->DropRef();
252*9880d681SAndroid Build Coastguard Worker I->first = NewCS.getInstruction();
253*9880d681SAndroid Build Coastguard Worker I->second = NewNode;
254*9880d681SAndroid Build Coastguard Worker NewNode->AddRef();
255*9880d681SAndroid Build Coastguard Worker return;
256*9880d681SAndroid Build Coastguard Worker }
257*9880d681SAndroid Build Coastguard Worker }
258*9880d681SAndroid Build Coastguard Worker }
259*9880d681SAndroid Build Coastguard Worker
260*9880d681SAndroid Build Coastguard Worker // Provide an explicit template instantiation for the static ID.
261*9880d681SAndroid Build Coastguard Worker char CallGraphAnalysis::PassID;
262*9880d681SAndroid Build Coastguard Worker
run(Module & M,AnalysisManager<Module> & AM)263*9880d681SAndroid Build Coastguard Worker PreservedAnalyses CallGraphPrinterPass::run(Module &M,
264*9880d681SAndroid Build Coastguard Worker AnalysisManager<Module> &AM) {
265*9880d681SAndroid Build Coastguard Worker AM.getResult<CallGraphAnalysis>(M).print(OS);
266*9880d681SAndroid Build Coastguard Worker return PreservedAnalyses::all();
267*9880d681SAndroid Build Coastguard Worker }
268*9880d681SAndroid Build Coastguard Worker
269*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
270*9880d681SAndroid Build Coastguard Worker // Out-of-line definitions of CallGraphAnalysis class members.
271*9880d681SAndroid Build Coastguard Worker //
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
274*9880d681SAndroid Build Coastguard Worker // Implementations of the CallGraphWrapperPass class methods.
275*9880d681SAndroid Build Coastguard Worker //
276*9880d681SAndroid Build Coastguard Worker
CallGraphWrapperPass()277*9880d681SAndroid Build Coastguard Worker CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) {
278*9880d681SAndroid Build Coastguard Worker initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry());
279*9880d681SAndroid Build Coastguard Worker }
280*9880d681SAndroid Build Coastguard Worker
~CallGraphWrapperPass()281*9880d681SAndroid Build Coastguard Worker CallGraphWrapperPass::~CallGraphWrapperPass() {}
282*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const283*9880d681SAndroid Build Coastguard Worker void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
284*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll();
285*9880d681SAndroid Build Coastguard Worker }
286*9880d681SAndroid Build Coastguard Worker
runOnModule(Module & M)287*9880d681SAndroid Build Coastguard Worker bool CallGraphWrapperPass::runOnModule(Module &M) {
288*9880d681SAndroid Build Coastguard Worker // All the real work is done in the constructor for the CallGraph.
289*9880d681SAndroid Build Coastguard Worker G.reset(new CallGraph(M));
290*9880d681SAndroid Build Coastguard Worker return false;
291*9880d681SAndroid Build Coastguard Worker }
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction",
294*9880d681SAndroid Build Coastguard Worker false, true)
295*9880d681SAndroid Build Coastguard Worker
296*9880d681SAndroid Build Coastguard Worker char CallGraphWrapperPass::ID = 0;
297*9880d681SAndroid Build Coastguard Worker
releaseMemory()298*9880d681SAndroid Build Coastguard Worker void CallGraphWrapperPass::releaseMemory() { G.reset(); }
299*9880d681SAndroid Build Coastguard Worker
print(raw_ostream & OS,const Module *) const300*9880d681SAndroid Build Coastguard Worker void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const {
301*9880d681SAndroid Build Coastguard Worker if (!G) {
302*9880d681SAndroid Build Coastguard Worker OS << "No call graph has been built!\n";
303*9880d681SAndroid Build Coastguard Worker return;
304*9880d681SAndroid Build Coastguard Worker }
305*9880d681SAndroid Build Coastguard Worker
306*9880d681SAndroid Build Coastguard Worker // Just delegate.
307*9880d681SAndroid Build Coastguard Worker G->print(OS);
308*9880d681SAndroid Build Coastguard Worker }
309*9880d681SAndroid Build Coastguard Worker
310*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD
dump() const311*9880d681SAndroid Build Coastguard Worker void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); }
312*9880d681SAndroid Build Coastguard Worker
313*9880d681SAndroid Build Coastguard Worker namespace {
314*9880d681SAndroid Build Coastguard Worker struct CallGraphPrinterLegacyPass : public ModulePass {
315*9880d681SAndroid Build Coastguard Worker static char ID; // Pass ID, replacement for typeid
CallGraphPrinterLegacyPass__anonf6e9e1a90211::CallGraphPrinterLegacyPass316*9880d681SAndroid Build Coastguard Worker CallGraphPrinterLegacyPass() : ModulePass(ID) {
317*9880d681SAndroid Build Coastguard Worker initializeCallGraphPrinterLegacyPassPass(*PassRegistry::getPassRegistry());
318*9880d681SAndroid Build Coastguard Worker }
319*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage__anonf6e9e1a90211::CallGraphPrinterLegacyPass320*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
321*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll();
322*9880d681SAndroid Build Coastguard Worker AU.addRequiredTransitive<CallGraphWrapperPass>();
323*9880d681SAndroid Build Coastguard Worker }
runOnModule__anonf6e9e1a90211::CallGraphPrinterLegacyPass324*9880d681SAndroid Build Coastguard Worker bool runOnModule(Module &M) override {
325*9880d681SAndroid Build Coastguard Worker getAnalysis<CallGraphWrapperPass>().print(errs(), &M);
326*9880d681SAndroid Build Coastguard Worker return false;
327*9880d681SAndroid Build Coastguard Worker }
328*9880d681SAndroid Build Coastguard Worker };
329*9880d681SAndroid Build Coastguard Worker }
330*9880d681SAndroid Build Coastguard Worker
331*9880d681SAndroid Build Coastguard Worker char CallGraphPrinterLegacyPass::ID = 0;
332*9880d681SAndroid Build Coastguard Worker
333*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(CallGraphPrinterLegacyPass, "print-callgraph",
334*9880d681SAndroid Build Coastguard Worker "Print a call graph", true, true)
335*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
336*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(CallGraphPrinterLegacyPass, "print-callgraph",
337*9880d681SAndroid Build Coastguard Worker "Print a call graph", true, true)
338