1009b1c42SEd Schouten //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten
9009b1c42SEd Schouten #include "llvm/Analysis/CallGraph.h"
10e3b55780SDimitry Andric #include "llvm/ADT/SCCIterator.h"
11044eb2f6SDimitry Andric #include "llvm/ADT/STLExtras.h"
12044eb2f6SDimitry Andric #include "llvm/ADT/SmallVector.h"
13eb11fae6SDimitry Andric #include "llvm/Config/llvm-config.h"
14cfca06d7SDimitry Andric #include "llvm/IR/AbstractCallSite.h"
15044eb2f6SDimitry Andric #include "llvm/IR/Function.h"
16cfca06d7SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
17706b4fc4SDimitry Andric #include "llvm/IR/Module.h"
18044eb2f6SDimitry Andric #include "llvm/IR/PassManager.h"
19706b4fc4SDimitry Andric #include "llvm/InitializePasses.h"
20044eb2f6SDimitry Andric #include "llvm/Pass.h"
21044eb2f6SDimitry Andric #include "llvm/Support/Compiler.h"
221e7804dbSRoman Divacky #include "llvm/Support/Debug.h"
2359850d08SRoman Divacky #include "llvm/Support/raw_ostream.h"
24044eb2f6SDimitry Andric #include <cassert>
25044eb2f6SDimitry Andric
26009b1c42SEd Schouten using namespace llvm;
27009b1c42SEd Schouten
285ca98fd9SDimitry Andric //===----------------------------------------------------------------------===//
295ca98fd9SDimitry Andric // Implementations of the CallGraph class methods.
305ca98fd9SDimitry Andric //
315ca98fd9SDimitry Andric
CallGraph(Module & M)325ca98fd9SDimitry Andric CallGraph::CallGraph(Module &M)
336b3f41edSDimitry Andric : M(M), ExternalCallingNode(getOrInsertFunction(nullptr)),
34cfca06d7SDimitry Andric CallsExternalNode(std::make_unique<CallGraphNode>(this, nullptr)) {
35cfca06d7SDimitry Andric // Add every interesting function to the call graph.
363a0822f0SDimitry Andric for (Function &F : M)
37cfca06d7SDimitry Andric if (!isDbgInfoIntrinsic(F.getIntrinsicID()))
383a0822f0SDimitry Andric addToCallGraph(&F);
395ca98fd9SDimitry Andric }
405ca98fd9SDimitry Andric
CallGraph(CallGraph && Arg)41dd58ef01SDimitry Andric CallGraph::CallGraph(CallGraph &&Arg)
426b3f41edSDimitry Andric : M(Arg.M), FunctionMap(std::move(Arg.FunctionMap)),
43dd58ef01SDimitry Andric ExternalCallingNode(Arg.ExternalCallingNode),
44dd58ef01SDimitry Andric CallsExternalNode(std::move(Arg.CallsExternalNode)) {
45dd58ef01SDimitry Andric Arg.FunctionMap.clear();
46dd58ef01SDimitry Andric Arg.ExternalCallingNode = nullptr;
47cfca06d7SDimitry Andric
48cfca06d7SDimitry Andric // Update parent CG for all call graph's nodes.
49cfca06d7SDimitry Andric CallsExternalNode->CG = this;
50cfca06d7SDimitry Andric for (auto &P : FunctionMap)
51cfca06d7SDimitry Andric P.second->CG = this;
52dd58ef01SDimitry Andric }
53dd58ef01SDimitry Andric
~CallGraph()545ca98fd9SDimitry Andric CallGraph::~CallGraph() {
555ca98fd9SDimitry Andric // CallsExternalNode is not in the function map, delete it explicitly.
56dd58ef01SDimitry Andric if (CallsExternalNode)
575ca98fd9SDimitry Andric CallsExternalNode->allReferencesDropped();
585ca98fd9SDimitry Andric
595ca98fd9SDimitry Andric // Reset all node's use counts to zero before deleting them to prevent an
605ca98fd9SDimitry Andric // assertion from firing.
615ca98fd9SDimitry Andric #ifndef NDEBUG
623a0822f0SDimitry Andric for (auto &I : FunctionMap)
633a0822f0SDimitry Andric I.second->allReferencesDropped();
645ca98fd9SDimitry Andric #endif
65cf099d11SDimitry Andric }
66009b1c42SEd Schouten
invalidate(Module &,const PreservedAnalyses & PA,ModuleAnalysisManager::Invalidator &)67cfca06d7SDimitry Andric bool CallGraph::invalidate(Module &, const PreservedAnalyses &PA,
68cfca06d7SDimitry Andric ModuleAnalysisManager::Invalidator &) {
69cfca06d7SDimitry Andric // Check whether the analysis, all analyses on functions, or the function's
70cfca06d7SDimitry Andric // CFG have been preserved.
71cfca06d7SDimitry Andric auto PAC = PA.getChecker<CallGraphAnalysis>();
72145449b1SDimitry Andric return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Module>>());
73cfca06d7SDimitry Andric }
74cfca06d7SDimitry Andric
addToCallGraph(Function * F)75f8af5cf6SDimitry Andric void CallGraph::addToCallGraph(Function *F) {
76009b1c42SEd Schouten CallGraphNode *Node = getOrInsertFunction(F);
77009b1c42SEd Schouten
78cfca06d7SDimitry Andric // If this function has external linkage or has its address taken and
79cfca06d7SDimitry Andric // it is not a callback, then anything could call it.
80cfca06d7SDimitry Andric if (!F->hasLocalLinkage() ||
81344a3780SDimitry Andric F->hasAddressTaken(nullptr, /*IgnoreCallbackUses=*/true,
82344a3780SDimitry Andric /* IgnoreAssumeLikeCalls */ true,
83344a3780SDimitry Andric /* IgnoreLLVMUsed */ false))
84e6d15924SDimitry Andric ExternalCallingNode->addCalledFunction(nullptr, Node);
85009b1c42SEd Schouten
86cfca06d7SDimitry Andric populateCallGraphNode(Node);
87cfca06d7SDimitry Andric }
88cfca06d7SDimitry Andric
populateCallGraphNode(CallGraphNode * Node)89cfca06d7SDimitry Andric void CallGraph::populateCallGraphNode(CallGraphNode *Node) {
90cfca06d7SDimitry Andric Function *F = Node->getFunction();
91cfca06d7SDimitry Andric
92009b1c42SEd Schouten // If this function is not defined in this translation unit, it could call
93009b1c42SEd Schouten // anything.
94e3b55780SDimitry Andric if (F->isDeclaration() && !F->hasFnAttribute(Attribute::NoCallback))
95e6d15924SDimitry Andric Node->addCalledFunction(nullptr, CallsExternalNode.get());
96009b1c42SEd Schouten
97009b1c42SEd Schouten // Look for calls by this function.
9801095a5dSDimitry Andric for (BasicBlock &BB : *F)
9901095a5dSDimitry Andric for (Instruction &I : BB) {
100e6d15924SDimitry Andric if (auto *Call = dyn_cast<CallBase>(&I)) {
101e6d15924SDimitry Andric const Function *Callee = Call->getCalledFunction();
102e3b55780SDimitry Andric if (!Callee)
103e6d15924SDimitry Andric Node->addCalledFunction(Call, CallsExternalNode.get());
104e3b55780SDimitry Andric else if (!isDbgInfoIntrinsic(Callee->getIntrinsicID()))
105e6d15924SDimitry Andric Node->addCalledFunction(Call, getOrInsertFunction(Callee));
106cfca06d7SDimitry Andric
107cfca06d7SDimitry Andric // Add reference to callback functions.
108cfca06d7SDimitry Andric forEachCallbackFunction(*Call, [=](Function *CB) {
109cfca06d7SDimitry Andric Node->addCalledFunction(nullptr, getOrInsertFunction(CB));
110cfca06d7SDimitry Andric });
111009b1c42SEd Schouten }
112009b1c42SEd Schouten }
113009b1c42SEd Schouten }
114009b1c42SEd Schouten
print(raw_ostream & OS) const1155ca98fd9SDimitry Andric void CallGraph::print(raw_ostream &OS) const {
1163a0822f0SDimitry Andric // Print in a deterministic order by sorting CallGraphNodes by name. We do
1173a0822f0SDimitry Andric // this here to avoid slowing down the non-printing fast path.
1183a0822f0SDimitry Andric
1193a0822f0SDimitry Andric SmallVector<CallGraphNode *, 16> Nodes;
1203a0822f0SDimitry Andric Nodes.reserve(FunctionMap.size());
1213a0822f0SDimitry Andric
12201095a5dSDimitry Andric for (const auto &I : *this)
12301095a5dSDimitry Andric Nodes.push_back(I.second.get());
1243a0822f0SDimitry Andric
125d8e91e46SDimitry Andric llvm::sort(Nodes, [](CallGraphNode *LHS, CallGraphNode *RHS) {
1263a0822f0SDimitry Andric if (Function *LF = LHS->getFunction())
1273a0822f0SDimitry Andric if (Function *RF = RHS->getFunction())
1283a0822f0SDimitry Andric return LF->getName() < RF->getName();
1293a0822f0SDimitry Andric
1303a0822f0SDimitry Andric return RHS->getFunction() != nullptr;
1313a0822f0SDimitry Andric });
1323a0822f0SDimitry Andric
1333a0822f0SDimitry Andric for (CallGraphNode *CN : Nodes)
1343a0822f0SDimitry Andric CN->print(OS);
135009b1c42SEd Schouten }
136009b1c42SEd Schouten
13771d5a254SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const13871d5a254SDimitry Andric LLVM_DUMP_METHOD void CallGraph::dump() const { print(dbgs()); }
13971d5a254SDimitry Andric #endif
140009b1c42SEd Schouten
ReplaceExternalCallEdge(CallGraphNode * Old,CallGraphNode * New)141cfca06d7SDimitry Andric void CallGraph::ReplaceExternalCallEdge(CallGraphNode *Old,
142cfca06d7SDimitry Andric CallGraphNode *New) {
143cfca06d7SDimitry Andric for (auto &CR : ExternalCallingNode->CalledFunctions)
144cfca06d7SDimitry Andric if (CR.second == Old) {
145cfca06d7SDimitry Andric CR.second->DropRef();
146cfca06d7SDimitry Andric CR.second = New;
147cfca06d7SDimitry Andric CR.second->AddRef();
148cfca06d7SDimitry Andric }
149cfca06d7SDimitry Andric }
150cfca06d7SDimitry Andric
151009b1c42SEd Schouten // removeFunctionFromModule - Unlink the function from this module, returning
152009b1c42SEd Schouten // it. Because this removes the function from the module, the call graph node
153009b1c42SEd Schouten // is destroyed. This is only valid if the function does not call any other
154009b1c42SEd Schouten // functions (ie, there are no edges in it's CGN). The easiest way to do this
155009b1c42SEd Schouten // is to dropAllReferences before calling this.
156009b1c42SEd Schouten //
removeFunctionFromModule(CallGraphNode * CGN)157009b1c42SEd Schouten Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
15859850d08SRoman Divacky assert(CGN->empty() && "Cannot remove function from call "
159009b1c42SEd Schouten "graph if it references other functions!");
160009b1c42SEd Schouten Function *F = CGN->getFunction(); // Get the function for the call graph node
161009b1c42SEd Schouten FunctionMap.erase(F); // Remove the call graph node from the map
162009b1c42SEd Schouten
1635ca98fd9SDimitry Andric M.getFunctionList().remove(F);
164009b1c42SEd Schouten return F;
165009b1c42SEd Schouten }
166009b1c42SEd Schouten
167009b1c42SEd Schouten // getOrInsertFunction - This method is identical to calling operator[], but
168009b1c42SEd Schouten // it will insert a new CallGraphNode for the specified function if one does
169009b1c42SEd Schouten // not already exist.
getOrInsertFunction(const Function * F)170009b1c42SEd Schouten CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
171dd58ef01SDimitry Andric auto &CGN = FunctionMap[F];
1725ca98fd9SDimitry Andric if (CGN)
173dd58ef01SDimitry Andric return CGN.get();
174009b1c42SEd Schouten
1755ca98fd9SDimitry Andric assert((!F || F->getParent() == &M) && "Function not in current module!");
176cfca06d7SDimitry Andric CGN = std::make_unique<CallGraphNode>(this, const_cast<Function *>(F));
177dd58ef01SDimitry Andric return CGN.get();
178009b1c42SEd Schouten }
179009b1c42SEd Schouten
1805ca98fd9SDimitry Andric //===----------------------------------------------------------------------===//
1815ca98fd9SDimitry Andric // Implementations of the CallGraphNode class methods.
1825ca98fd9SDimitry Andric //
1835ca98fd9SDimitry Andric
print(raw_ostream & OS) const18459850d08SRoman Divacky void CallGraphNode::print(raw_ostream &OS) const {
185009b1c42SEd Schouten if (Function *F = getFunction())
18659850d08SRoman Divacky OS << "Call graph node for function: '" << F->getName() << "'";
187009b1c42SEd Schouten else
18859850d08SRoman Divacky OS << "Call graph node <<null function>>";
18959850d08SRoman Divacky
190d7f7719eSRoman Divacky OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n';
191009b1c42SEd Schouten
19201095a5dSDimitry Andric for (const auto &I : *this) {
19301095a5dSDimitry Andric OS << " CS<" << I.first << "> calls ";
19401095a5dSDimitry Andric if (Function *FI = I.second->getFunction())
195d7f7719eSRoman Divacky OS << "function '" << FI->getName() <<"'\n";
196009b1c42SEd Schouten else
197d7f7719eSRoman Divacky OS << "external node\n";
198d7f7719eSRoman Divacky }
199d7f7719eSRoman Divacky OS << '\n';
200009b1c42SEd Schouten }
201009b1c42SEd Schouten
20271d5a254SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const20371d5a254SDimitry Andric LLVM_DUMP_METHOD void CallGraphNode::dump() const { print(dbgs()); }
20471d5a254SDimitry Andric #endif
205009b1c42SEd Schouten
206009b1c42SEd Schouten /// removeCallEdgeFor - This method removes the edge in the node for the
207009b1c42SEd Schouten /// specified call site. Note that this method takes linear time, so it
208009b1c42SEd Schouten /// should be used sparingly.
removeCallEdgeFor(CallBase & Call)209e6d15924SDimitry Andric void CallGraphNode::removeCallEdgeFor(CallBase &Call) {
210009b1c42SEd Schouten for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
211009b1c42SEd Schouten assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
212cfca06d7SDimitry Andric if (I->first && *I->first == &Call) {
21359850d08SRoman Divacky I->second->DropRef();
21459850d08SRoman Divacky *I = CalledFunctions.back();
21559850d08SRoman Divacky CalledFunctions.pop_back();
216cfca06d7SDimitry Andric
217cfca06d7SDimitry Andric // Remove all references to callback functions if there are any.
218cfca06d7SDimitry Andric forEachCallbackFunction(Call, [=](Function *CB) {
219cfca06d7SDimitry Andric removeOneAbstractEdgeTo(CG->getOrInsertFunction(CB));
220cfca06d7SDimitry Andric });
221009b1c42SEd Schouten return;
222009b1c42SEd Schouten }
223009b1c42SEd Schouten }
224009b1c42SEd Schouten }
225009b1c42SEd Schouten
226009b1c42SEd Schouten // removeAnyCallEdgeTo - This method removes any call edges from this node to
227009b1c42SEd Schouten // the specified callee function. This takes more time to execute than
228009b1c42SEd Schouten // removeCallEdgeTo, so it should not be used unless necessary.
removeAnyCallEdgeTo(CallGraphNode * Callee)229009b1c42SEd Schouten void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
230009b1c42SEd Schouten for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
231009b1c42SEd Schouten if (CalledFunctions[i].second == Callee) {
23259850d08SRoman Divacky Callee->DropRef();
233009b1c42SEd Schouten CalledFunctions[i] = CalledFunctions.back();
234009b1c42SEd Schouten CalledFunctions.pop_back();
235009b1c42SEd Schouten --i; --e;
236009b1c42SEd Schouten }
237009b1c42SEd Schouten }
238009b1c42SEd Schouten
239009b1c42SEd Schouten /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
240009b1c42SEd Schouten /// from this node to the specified callee function.
removeOneAbstractEdgeTo(CallGraphNode * Callee)241009b1c42SEd Schouten void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
242009b1c42SEd Schouten for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
243009b1c42SEd Schouten assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
244009b1c42SEd Schouten CallRecord &CR = *I;
245cfca06d7SDimitry Andric if (CR.second == Callee && !CR.first) {
24659850d08SRoman Divacky Callee->DropRef();
24759850d08SRoman Divacky *I = CalledFunctions.back();
24859850d08SRoman Divacky CalledFunctions.pop_back();
249009b1c42SEd Schouten return;
250009b1c42SEd Schouten }
251009b1c42SEd Schouten }
252009b1c42SEd Schouten }
253009b1c42SEd Schouten
25459850d08SRoman Divacky /// replaceCallEdge - This method replaces the edge in the node for the
25559850d08SRoman Divacky /// specified call site with a new one. Note that this method takes linear
25659850d08SRoman Divacky /// time, so it should be used sparingly.
replaceCallEdge(CallBase & Call,CallBase & NewCall,CallGraphNode * NewNode)257e6d15924SDimitry Andric void CallGraphNode::replaceCallEdge(CallBase &Call, CallBase &NewCall,
258e6d15924SDimitry Andric CallGraphNode *NewNode) {
259009b1c42SEd Schouten for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
26059850d08SRoman Divacky assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
261cfca06d7SDimitry Andric if (I->first && *I->first == &Call) {
26259850d08SRoman Divacky I->second->DropRef();
263e6d15924SDimitry Andric I->first = &NewCall;
26459850d08SRoman Divacky I->second = NewNode;
26559850d08SRoman Divacky NewNode->AddRef();
266cfca06d7SDimitry Andric
267b60736ecSDimitry Andric // Refresh callback references. Do not resize CalledFunctions if the
268b60736ecSDimitry Andric // number of callbacks is the same for new and old call sites.
269b60736ecSDimitry Andric SmallVector<CallGraphNode *, 4u> OldCBs;
270b60736ecSDimitry Andric SmallVector<CallGraphNode *, 4u> NewCBs;
271b60736ecSDimitry Andric forEachCallbackFunction(Call, [this, &OldCBs](Function *CB) {
272b60736ecSDimitry Andric OldCBs.push_back(CG->getOrInsertFunction(CB));
273cfca06d7SDimitry Andric });
274b60736ecSDimitry Andric forEachCallbackFunction(NewCall, [this, &NewCBs](Function *CB) {
275b60736ecSDimitry Andric NewCBs.push_back(CG->getOrInsertFunction(CB));
276cfca06d7SDimitry Andric });
277b60736ecSDimitry Andric if (OldCBs.size() == NewCBs.size()) {
278b60736ecSDimitry Andric for (unsigned N = 0; N < OldCBs.size(); ++N) {
279b60736ecSDimitry Andric CallGraphNode *OldNode = OldCBs[N];
280b60736ecSDimitry Andric CallGraphNode *NewNode = NewCBs[N];
281b60736ecSDimitry Andric for (auto J = CalledFunctions.begin();; ++J) {
282b60736ecSDimitry Andric assert(J != CalledFunctions.end() &&
283b60736ecSDimitry Andric "Cannot find callsite to update!");
284b60736ecSDimitry Andric if (!J->first && J->second == OldNode) {
285b60736ecSDimitry Andric J->second = NewNode;
286b60736ecSDimitry Andric OldNode->DropRef();
287b60736ecSDimitry Andric NewNode->AddRef();
288b60736ecSDimitry Andric break;
289b60736ecSDimitry Andric }
290b60736ecSDimitry Andric }
291b60736ecSDimitry Andric }
292b60736ecSDimitry Andric } else {
293b60736ecSDimitry Andric for (auto *CGN : OldCBs)
294b60736ecSDimitry Andric removeOneAbstractEdgeTo(CGN);
295b60736ecSDimitry Andric for (auto *CGN : NewCBs)
296b60736ecSDimitry Andric addCalledFunction(nullptr, CGN);
297b60736ecSDimitry Andric }
298009b1c42SEd Schouten return;
299009b1c42SEd Schouten }
300009b1c42SEd Schouten }
301009b1c42SEd Schouten }
302009b1c42SEd Schouten
30301095a5dSDimitry Andric // Provide an explicit template instantiation for the static ID.
304b915e9e0SDimitry Andric AnalysisKey CallGraphAnalysis::Key;
30501095a5dSDimitry Andric
run(Module & M,ModuleAnalysisManager & AM)30601095a5dSDimitry Andric PreservedAnalyses CallGraphPrinterPass::run(Module &M,
307b915e9e0SDimitry Andric ModuleAnalysisManager &AM) {
30801095a5dSDimitry Andric AM.getResult<CallGraphAnalysis>(M).print(OS);
30901095a5dSDimitry Andric return PreservedAnalyses::all();
31001095a5dSDimitry Andric }
31101095a5dSDimitry Andric
run(Module & M,ModuleAnalysisManager & AM)312e3b55780SDimitry Andric PreservedAnalyses CallGraphSCCsPrinterPass::run(Module &M,
313e3b55780SDimitry Andric ModuleAnalysisManager &AM) {
314e3b55780SDimitry Andric auto &CG = AM.getResult<CallGraphAnalysis>(M);
315e3b55780SDimitry Andric unsigned sccNum = 0;
316e3b55780SDimitry Andric OS << "SCCs for the program in PostOrder:";
317e3b55780SDimitry Andric for (scc_iterator<CallGraph *> SCCI = scc_begin(&CG); !SCCI.isAtEnd();
318e3b55780SDimitry Andric ++SCCI) {
319e3b55780SDimitry Andric const std::vector<CallGraphNode *> &nextSCC = *SCCI;
320e3b55780SDimitry Andric OS << "\nSCC #" << ++sccNum << ": ";
321e3b55780SDimitry Andric bool First = true;
322ac9a064cSDimitry Andric for (CallGraphNode *CGN : nextSCC) {
323e3b55780SDimitry Andric if (First)
324e3b55780SDimitry Andric First = false;
325e3b55780SDimitry Andric else
326e3b55780SDimitry Andric OS << ", ";
327ac9a064cSDimitry Andric OS << (CGN->getFunction() ? CGN->getFunction()->getName()
328e3b55780SDimitry Andric : "external node");
329e3b55780SDimitry Andric }
330e3b55780SDimitry Andric
331e3b55780SDimitry Andric if (nextSCC.size() == 1 && SCCI.hasCycle())
332e3b55780SDimitry Andric OS << " (Has self-loop).";
333e3b55780SDimitry Andric }
334e3b55780SDimitry Andric OS << "\n";
335e3b55780SDimitry Andric return PreservedAnalyses::all();
336e3b55780SDimitry Andric }
337e3b55780SDimitry Andric
3385ca98fd9SDimitry Andric //===----------------------------------------------------------------------===//
3395ca98fd9SDimitry Andric // Out-of-line definitions of CallGraphAnalysis class members.
3405ca98fd9SDimitry Andric //
3415ca98fd9SDimitry Andric
3425ca98fd9SDimitry Andric //===----------------------------------------------------------------------===//
3435ca98fd9SDimitry Andric // Implementations of the CallGraphWrapperPass class methods.
3445ca98fd9SDimitry Andric //
3455ca98fd9SDimitry Andric
CallGraphWrapperPass()3465ca98fd9SDimitry Andric CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) {
3475ca98fd9SDimitry Andric initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry());
3485ca98fd9SDimitry Andric }
3495ca98fd9SDimitry Andric
350044eb2f6SDimitry Andric CallGraphWrapperPass::~CallGraphWrapperPass() = default;
3515ca98fd9SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const3525ca98fd9SDimitry Andric void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
3535ca98fd9SDimitry Andric AU.setPreservesAll();
3545ca98fd9SDimitry Andric }
3555ca98fd9SDimitry Andric
runOnModule(Module & M)3565ca98fd9SDimitry Andric bool CallGraphWrapperPass::runOnModule(Module &M) {
3575ca98fd9SDimitry Andric // All the real work is done in the constructor for the CallGraph.
3585ca98fd9SDimitry Andric G.reset(new CallGraph(M));
3595ca98fd9SDimitry Andric return false;
3605ca98fd9SDimitry Andric }
3615ca98fd9SDimitry Andric
3625ca98fd9SDimitry Andric INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction",
3635ca98fd9SDimitry Andric false, true)
3645ca98fd9SDimitry Andric
3655ca98fd9SDimitry Andric char CallGraphWrapperPass::ID = 0;
3665ca98fd9SDimitry Andric
releaseMemory()3675ca98fd9SDimitry Andric void CallGraphWrapperPass::releaseMemory() { G.reset(); }
3685ca98fd9SDimitry Andric
print(raw_ostream & OS,const Module *) const3695ca98fd9SDimitry Andric void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const {
3705ca98fd9SDimitry Andric if (!G) {
3715ca98fd9SDimitry Andric OS << "No call graph has been built!\n";
3725ca98fd9SDimitry Andric return;
3735ca98fd9SDimitry Andric }
3745ca98fd9SDimitry Andric
3755ca98fd9SDimitry Andric // Just delegate.
3765ca98fd9SDimitry Andric G->print(OS);
3775ca98fd9SDimitry Andric }
3785ca98fd9SDimitry Andric
37971d5a254SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
380dd58ef01SDimitry Andric LLVM_DUMP_METHOD
dump() const3815ca98fd9SDimitry Andric void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); }
38271d5a254SDimitry Andric #endif
383