1 //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/CallGraph.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/IR/AbstractCallSite.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/IntrinsicInst.h"
16 #include "llvm/IR/Intrinsics.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/InitializePasses.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
29 //===----------------------------------------------------------------------===//
30 // Implementations of the CallGraph class methods.
33 CallGraph::CallGraph(Module
&M
)
34 : M(M
), ExternalCallingNode(getOrInsertFunction(nullptr)),
35 CallsExternalNode(std::make_unique
<CallGraphNode
>(this, nullptr)) {
36 // Add every interesting function to the call graph.
38 if (!isDbgInfoIntrinsic(F
.getIntrinsicID()))
42 CallGraph::CallGraph(CallGraph
&&Arg
)
43 : M(Arg
.M
), FunctionMap(std::move(Arg
.FunctionMap
)),
44 ExternalCallingNode(Arg
.ExternalCallingNode
),
45 CallsExternalNode(std::move(Arg
.CallsExternalNode
)) {
46 Arg
.FunctionMap
.clear();
47 Arg
.ExternalCallingNode
= nullptr;
49 // Update parent CG for all call graph's nodes.
50 CallsExternalNode
->CG
= this;
51 for (auto &P
: FunctionMap
)
55 CallGraph::~CallGraph() {
56 // CallsExternalNode is not in the function map, delete it explicitly.
57 if (CallsExternalNode
)
58 CallsExternalNode
->allReferencesDropped();
60 // Reset all node's use counts to zero before deleting them to prevent an
61 // assertion from firing.
63 for (auto &I
: FunctionMap
)
64 I
.second
->allReferencesDropped();
68 bool CallGraph::invalidate(Module
&, const PreservedAnalyses
&PA
,
69 ModuleAnalysisManager::Invalidator
&) {
70 // Check whether the analysis, all analyses on functions, or the function's
71 // CFG have been preserved.
72 auto PAC
= PA
.getChecker
<CallGraphAnalysis
>();
73 return !(PAC
.preserved() || PAC
.preservedSet
<AllAnalysesOn
<Module
>>() ||
74 PAC
.preservedSet
<CFGAnalyses
>());
77 void CallGraph::addToCallGraph(Function
*F
) {
78 CallGraphNode
*Node
= getOrInsertFunction(F
);
80 // If this function has external linkage or has its address taken and
81 // it is not a callback, then anything could call it.
82 if (!F
->hasLocalLinkage() ||
83 F
->hasAddressTaken(nullptr, /*IgnoreCallbackUses=*/true,
84 /* IgnoreAssumeLikeCalls */ true,
85 /* IgnoreLLVMUsed */ false))
86 ExternalCallingNode
->addCalledFunction(nullptr, Node
);
88 populateCallGraphNode(Node
);
91 void CallGraph::populateCallGraphNode(CallGraphNode
*Node
) {
92 Function
*F
= Node
->getFunction();
94 // If this function is not defined in this translation unit, it could call
96 if (F
->isDeclaration() && !F
->isIntrinsic())
97 Node
->addCalledFunction(nullptr, CallsExternalNode
.get());
99 // Look for calls by this function.
100 for (BasicBlock
&BB
: *F
)
101 for (Instruction
&I
: BB
) {
102 if (auto *Call
= dyn_cast
<CallBase
>(&I
)) {
103 const Function
*Callee
= Call
->getCalledFunction();
104 if (!Callee
|| !Intrinsic::isLeaf(Callee
->getIntrinsicID()))
105 // Indirect calls of intrinsics are not allowed so no need to check.
106 // We can be more precise here by using TargetArg returned by
107 // Intrinsic::isLeaf.
108 Node
->addCalledFunction(Call
, CallsExternalNode
.get());
109 else if (!Callee
->isIntrinsic())
110 Node
->addCalledFunction(Call
, getOrInsertFunction(Callee
));
112 // Add reference to callback functions.
113 forEachCallbackFunction(*Call
, [=](Function
*CB
) {
114 Node
->addCalledFunction(nullptr, getOrInsertFunction(CB
));
120 void CallGraph::print(raw_ostream
&OS
) const {
121 // Print in a deterministic order by sorting CallGraphNodes by name. We do
122 // this here to avoid slowing down the non-printing fast path.
124 SmallVector
<CallGraphNode
*, 16> Nodes
;
125 Nodes
.reserve(FunctionMap
.size());
127 for (const auto &I
: *this)
128 Nodes
.push_back(I
.second
.get());
130 llvm::sort(Nodes
, [](CallGraphNode
*LHS
, CallGraphNode
*RHS
) {
131 if (Function
*LF
= LHS
->getFunction())
132 if (Function
*RF
= RHS
->getFunction())
133 return LF
->getName() < RF
->getName();
135 return RHS
->getFunction() != nullptr;
138 for (CallGraphNode
*CN
: Nodes
)
142 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
143 LLVM_DUMP_METHOD
void CallGraph::dump() const { print(dbgs()); }
146 void CallGraph::ReplaceExternalCallEdge(CallGraphNode
*Old
,
147 CallGraphNode
*New
) {
148 for (auto &CR
: ExternalCallingNode
->CalledFunctions
)
149 if (CR
.second
== Old
) {
150 CR
.second
->DropRef();
156 // removeFunctionFromModule - Unlink the function from this module, returning
157 // it. Because this removes the function from the module, the call graph node
158 // is destroyed. This is only valid if the function does not call any other
159 // functions (ie, there are no edges in it's CGN). The easiest way to do this
160 // is to dropAllReferences before calling this.
162 Function
*CallGraph::removeFunctionFromModule(CallGraphNode
*CGN
) {
163 assert(CGN
->empty() && "Cannot remove function from call "
164 "graph if it references other functions!");
165 Function
*F
= CGN
->getFunction(); // Get the function for the call graph node
166 FunctionMap
.erase(F
); // Remove the call graph node from the map
168 M
.getFunctionList().remove(F
);
172 // getOrInsertFunction - This method is identical to calling operator[], but
173 // it will insert a new CallGraphNode for the specified function if one does
174 // not already exist.
175 CallGraphNode
*CallGraph::getOrInsertFunction(const Function
*F
) {
176 auto &CGN
= FunctionMap
[F
];
180 assert((!F
|| F
->getParent() == &M
) && "Function not in current module!");
181 CGN
= std::make_unique
<CallGraphNode
>(this, const_cast<Function
*>(F
));
185 //===----------------------------------------------------------------------===//
186 // Implementations of the CallGraphNode class methods.
189 void CallGraphNode::print(raw_ostream
&OS
) const {
190 if (Function
*F
= getFunction())
191 OS
<< "Call graph node for function: '" << F
->getName() << "'";
193 OS
<< "Call graph node <<null function>>";
195 OS
<< "<<" << this << ">> #uses=" << getNumReferences() << '\n';
197 for (const auto &I
: *this) {
198 OS
<< " CS<" << I
.first
<< "> calls ";
199 if (Function
*FI
= I
.second
->getFunction())
200 OS
<< "function '" << FI
->getName() <<"'\n";
202 OS
<< "external node\n";
207 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
208 LLVM_DUMP_METHOD
void CallGraphNode::dump() const { print(dbgs()); }
211 /// removeCallEdgeFor - This method removes the edge in the node for the
212 /// specified call site. Note that this method takes linear time, so it
213 /// should be used sparingly.
214 void CallGraphNode::removeCallEdgeFor(CallBase
&Call
) {
215 for (CalledFunctionsVector::iterator I
= CalledFunctions
.begin(); ; ++I
) {
216 assert(I
!= CalledFunctions
.end() && "Cannot find callsite to remove!");
217 if (I
->first
&& *I
->first
== &Call
) {
218 I
->second
->DropRef();
219 *I
= CalledFunctions
.back();
220 CalledFunctions
.pop_back();
222 // Remove all references to callback functions if there are any.
223 forEachCallbackFunction(Call
, [=](Function
*CB
) {
224 removeOneAbstractEdgeTo(CG
->getOrInsertFunction(CB
));
231 // removeAnyCallEdgeTo - This method removes any call edges from this node to
232 // the specified callee function. This takes more time to execute than
233 // removeCallEdgeTo, so it should not be used unless necessary.
234 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode
*Callee
) {
235 for (unsigned i
= 0, e
= CalledFunctions
.size(); i
!= e
; ++i
)
236 if (CalledFunctions
[i
].second
== Callee
) {
238 CalledFunctions
[i
] = CalledFunctions
.back();
239 CalledFunctions
.pop_back();
244 /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
245 /// from this node to the specified callee function.
246 void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode
*Callee
) {
247 for (CalledFunctionsVector::iterator I
= CalledFunctions
.begin(); ; ++I
) {
248 assert(I
!= CalledFunctions
.end() && "Cannot find callee to remove!");
250 if (CR
.second
== Callee
&& !CR
.first
) {
252 *I
= CalledFunctions
.back();
253 CalledFunctions
.pop_back();
259 /// replaceCallEdge - This method replaces the edge in the node for the
260 /// specified call site with a new one. Note that this method takes linear
261 /// time, so it should be used sparingly.
262 void CallGraphNode::replaceCallEdge(CallBase
&Call
, CallBase
&NewCall
,
263 CallGraphNode
*NewNode
) {
264 for (CalledFunctionsVector::iterator I
= CalledFunctions
.begin(); ; ++I
) {
265 assert(I
!= CalledFunctions
.end() && "Cannot find callsite to remove!");
266 if (I
->first
&& *I
->first
== &Call
) {
267 I
->second
->DropRef();
272 // Refresh callback references. Do not resize CalledFunctions if the
273 // number of callbacks is the same for new and old call sites.
274 SmallVector
<CallGraphNode
*, 4u> OldCBs
;
275 SmallVector
<CallGraphNode
*, 4u> NewCBs
;
276 forEachCallbackFunction(Call
, [this, &OldCBs
](Function
*CB
) {
277 OldCBs
.push_back(CG
->getOrInsertFunction(CB
));
279 forEachCallbackFunction(NewCall
, [this, &NewCBs
](Function
*CB
) {
280 NewCBs
.push_back(CG
->getOrInsertFunction(CB
));
282 if (OldCBs
.size() == NewCBs
.size()) {
283 for (unsigned N
= 0; N
< OldCBs
.size(); ++N
) {
284 CallGraphNode
*OldNode
= OldCBs
[N
];
285 CallGraphNode
*NewNode
= NewCBs
[N
];
286 for (auto J
= CalledFunctions
.begin();; ++J
) {
287 assert(J
!= CalledFunctions
.end() &&
288 "Cannot find callsite to update!");
289 if (!J
->first
&& J
->second
== OldNode
) {
298 for (auto *CGN
: OldCBs
)
299 removeOneAbstractEdgeTo(CGN
);
300 for (auto *CGN
: NewCBs
)
301 addCalledFunction(nullptr, CGN
);
308 // Provide an explicit template instantiation for the static ID.
309 AnalysisKey
CallGraphAnalysis::Key
;
311 PreservedAnalyses
CallGraphPrinterPass::run(Module
&M
,
312 ModuleAnalysisManager
&AM
) {
313 AM
.getResult
<CallGraphAnalysis
>(M
).print(OS
);
314 return PreservedAnalyses::all();
317 //===----------------------------------------------------------------------===//
318 // Out-of-line definitions of CallGraphAnalysis class members.
321 //===----------------------------------------------------------------------===//
322 // Implementations of the CallGraphWrapperPass class methods.
325 CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID
) {
326 initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry());
329 CallGraphWrapperPass::~CallGraphWrapperPass() = default;
331 void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
332 AU
.setPreservesAll();
335 bool CallGraphWrapperPass::runOnModule(Module
&M
) {
336 // All the real work is done in the constructor for the CallGraph.
337 G
.reset(new CallGraph(M
));
341 INITIALIZE_PASS(CallGraphWrapperPass
, "basiccg", "CallGraph Construction",
344 char CallGraphWrapperPass::ID
= 0;
346 void CallGraphWrapperPass::releaseMemory() { G
.reset(); }
348 void CallGraphWrapperPass::print(raw_ostream
&OS
, const Module
*) const {
350 OS
<< "No call graph has been built!\n";
358 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
360 void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); }
365 struct CallGraphPrinterLegacyPass
: public ModulePass
{
366 static char ID
; // Pass ID, replacement for typeid
368 CallGraphPrinterLegacyPass() : ModulePass(ID
) {
369 initializeCallGraphPrinterLegacyPassPass(*PassRegistry::getPassRegistry());
372 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
373 AU
.setPreservesAll();
374 AU
.addRequiredTransitive
<CallGraphWrapperPass
>();
377 bool runOnModule(Module
&M
) override
{
378 getAnalysis
<CallGraphWrapperPass
>().print(errs(), &M
);
383 } // end anonymous namespace
385 char CallGraphPrinterLegacyPass::ID
= 0;
387 INITIALIZE_PASS_BEGIN(CallGraphPrinterLegacyPass
, "print-callgraph",
388 "Print a call graph", true, true)
389 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass
)
390 INITIALIZE_PASS_END(CallGraphPrinterLegacyPass
, "print-callgraph",
391 "Print a call graph", true, true)