xref: /llvm-project/llvm/lib/Analysis/CallGraph.cpp (revision 60e4af7ab8e64cec3c55e927a72cb06de0bb0c79)
1 //===- CallGraph.cpp - Build a Module's call graph ------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Analysis/CallGraph.h"
10 #include "llvm/ADT/SCCIterator.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/Config/llvm-config.h"
14 #include "llvm/IR/AbstractCallSite.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/IntrinsicInst.h"
17 #include "llvm/IR/Intrinsics.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/PassManager.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include <cassert>
26 
27 using namespace llvm;
28 
29 //===----------------------------------------------------------------------===//
30 // Implementations of the CallGraph class methods.
31 //
32 
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.
37   for (Function &F : M)
38     if (!isDbgInfoIntrinsic(F.getIntrinsicID()))
39       addToCallGraph(&F);
40 }
41 
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;
48 
49   // Update parent CG for all call graph's nodes.
50   CallsExternalNode->CG = this;
51   for (auto &P : FunctionMap)
52     P.second->CG = this;
53 }
54 
55 CallGraph::~CallGraph() {
56   // CallsExternalNode is not in the function map, delete it explicitly.
57   if (CallsExternalNode)
58     CallsExternalNode->allReferencesDropped();
59 
60 // Reset all node's use counts to zero before deleting them to prevent an
61 // assertion from firing.
62 #ifndef NDEBUG
63   for (auto &I : FunctionMap)
64     I.second->allReferencesDropped();
65 #endif
66 }
67 
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 }
75 
76 void CallGraph::addToCallGraph(Function *F) {
77   CallGraphNode *Node = getOrInsertFunction(F);
78 
79   // If this function has external linkage or has its address taken and
80   // it is not a callback, then anything could call it.
81   if (!F->hasLocalLinkage() ||
82       F->hasAddressTaken(nullptr, /*IgnoreCallbackUses=*/true,
83                          /* IgnoreAssumeLikeCalls */ true,
84                          /* IgnoreLLVMUsed */ false))
85     ExternalCallingNode->addCalledFunction(nullptr, Node);
86 
87   populateCallGraphNode(Node);
88 }
89 
90 void CallGraph::populateCallGraphNode(CallGraphNode *Node) {
91   Function *F = Node->getFunction();
92 
93   // If this function is not defined in this translation unit, it could call
94   // anything.
95   if (F->isDeclaration() && !F->isIntrinsic())
96     Node->addCalledFunction(nullptr, CallsExternalNode.get());
97 
98   // Look for calls by this function.
99   for (BasicBlock &BB : *F)
100     for (Instruction &I : BB) {
101       if (auto *Call = dyn_cast<CallBase>(&I)) {
102         const Function *Callee = Call->getCalledFunction();
103         if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID()))
104           // Indirect calls of intrinsics are not allowed so no need to check.
105           // We can be more precise here by using TargetArg returned by
106           // Intrinsic::isLeaf.
107           Node->addCalledFunction(Call, CallsExternalNode.get());
108         else if (!Callee->isIntrinsic())
109           Node->addCalledFunction(Call, getOrInsertFunction(Callee));
110 
111         // Add reference to callback functions.
112         forEachCallbackFunction(*Call, [=](Function *CB) {
113           Node->addCalledFunction(nullptr, getOrInsertFunction(CB));
114         });
115       }
116     }
117 }
118 
119 void CallGraph::print(raw_ostream &OS) const {
120   // Print in a deterministic order by sorting CallGraphNodes by name.  We do
121   // this here to avoid slowing down the non-printing fast path.
122 
123   SmallVector<CallGraphNode *, 16> Nodes;
124   Nodes.reserve(FunctionMap.size());
125 
126   for (const auto &I : *this)
127     Nodes.push_back(I.second.get());
128 
129   llvm::sort(Nodes, [](CallGraphNode *LHS, CallGraphNode *RHS) {
130     if (Function *LF = LHS->getFunction())
131       if (Function *RF = RHS->getFunction())
132         return LF->getName() < RF->getName();
133 
134     return RHS->getFunction() != nullptr;
135   });
136 
137   for (CallGraphNode *CN : Nodes)
138     CN->print(OS);
139 }
140 
141 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
142 LLVM_DUMP_METHOD void CallGraph::dump() const { print(dbgs()); }
143 #endif
144 
145 void CallGraph::ReplaceExternalCallEdge(CallGraphNode *Old,
146                                         CallGraphNode *New) {
147   for (auto &CR : ExternalCallingNode->CalledFunctions)
148     if (CR.second == Old) {
149       CR.second->DropRef();
150       CR.second = New;
151       CR.second->AddRef();
152     }
153 }
154 
155 // removeFunctionFromModule - Unlink the function from this module, returning
156 // it.  Because this removes the function from the module, the call graph node
157 // is destroyed.  This is only valid if the function does not call any other
158 // functions (ie, there are no edges in it's CGN).  The easiest way to do this
159 // is to dropAllReferences before calling this.
160 //
161 Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) {
162   assert(CGN->empty() && "Cannot remove function from call "
163          "graph if it references other functions!");
164   Function *F = CGN->getFunction(); // Get the function for the call graph node
165   FunctionMap.erase(F);             // Remove the call graph node from the map
166 
167   M.getFunctionList().remove(F);
168   return F;
169 }
170 
171 // getOrInsertFunction - This method is identical to calling operator[], but
172 // it will insert a new CallGraphNode for the specified function if one does
173 // not already exist.
174 CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) {
175   auto &CGN = FunctionMap[F];
176   if (CGN)
177     return CGN.get();
178 
179   assert((!F || F->getParent() == &M) && "Function not in current module!");
180   CGN = std::make_unique<CallGraphNode>(this, const_cast<Function *>(F));
181   return CGN.get();
182 }
183 
184 //===----------------------------------------------------------------------===//
185 // Implementations of the CallGraphNode class methods.
186 //
187 
188 void CallGraphNode::print(raw_ostream &OS) const {
189   if (Function *F = getFunction())
190     OS << "Call graph node for function: '" << F->getName() << "'";
191   else
192     OS << "Call graph node <<null function>>";
193 
194   OS << "<<" << this << ">>  #uses=" << getNumReferences() << '\n';
195 
196   for (const auto &I : *this) {
197     OS << "  CS<" << I.first << "> calls ";
198     if (Function *FI = I.second->getFunction())
199       OS << "function '" << FI->getName() <<"'\n";
200     else
201       OS << "external node\n";
202   }
203   OS << '\n';
204 }
205 
206 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
207 LLVM_DUMP_METHOD void CallGraphNode::dump() const { print(dbgs()); }
208 #endif
209 
210 /// removeCallEdgeFor - This method removes the edge in the node for the
211 /// specified call site.  Note that this method takes linear time, so it
212 /// should be used sparingly.
213 void CallGraphNode::removeCallEdgeFor(CallBase &Call) {
214   for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
215     assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
216     if (I->first && *I->first == &Call) {
217       I->second->DropRef();
218       *I = CalledFunctions.back();
219       CalledFunctions.pop_back();
220 
221       // Remove all references to callback functions if there are any.
222       forEachCallbackFunction(Call, [=](Function *CB) {
223         removeOneAbstractEdgeTo(CG->getOrInsertFunction(CB));
224       });
225       return;
226     }
227   }
228 }
229 
230 // removeAnyCallEdgeTo - This method removes any call edges from this node to
231 // the specified callee function.  This takes more time to execute than
232 // removeCallEdgeTo, so it should not be used unless necessary.
233 void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) {
234   for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i)
235     if (CalledFunctions[i].second == Callee) {
236       Callee->DropRef();
237       CalledFunctions[i] = CalledFunctions.back();
238       CalledFunctions.pop_back();
239       --i; --e;
240     }
241 }
242 
243 /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite
244 /// from this node to the specified callee function.
245 void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) {
246   for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
247     assert(I != CalledFunctions.end() && "Cannot find callee to remove!");
248     CallRecord &CR = *I;
249     if (CR.second == Callee && !CR.first) {
250       Callee->DropRef();
251       *I = CalledFunctions.back();
252       CalledFunctions.pop_back();
253       return;
254     }
255   }
256 }
257 
258 /// replaceCallEdge - This method replaces the edge in the node for the
259 /// specified call site with a new one.  Note that this method takes linear
260 /// time, so it should be used sparingly.
261 void CallGraphNode::replaceCallEdge(CallBase &Call, CallBase &NewCall,
262                                     CallGraphNode *NewNode) {
263   for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) {
264     assert(I != CalledFunctions.end() && "Cannot find callsite to remove!");
265     if (I->first && *I->first == &Call) {
266       I->second->DropRef();
267       I->first = &NewCall;
268       I->second = NewNode;
269       NewNode->AddRef();
270 
271       // Refresh callback references. Do not resize CalledFunctions if the
272       // number of callbacks is the same for new and old call sites.
273       SmallVector<CallGraphNode *, 4u> OldCBs;
274       SmallVector<CallGraphNode *, 4u> NewCBs;
275       forEachCallbackFunction(Call, [this, &OldCBs](Function *CB) {
276         OldCBs.push_back(CG->getOrInsertFunction(CB));
277       });
278       forEachCallbackFunction(NewCall, [this, &NewCBs](Function *CB) {
279         NewCBs.push_back(CG->getOrInsertFunction(CB));
280       });
281       if (OldCBs.size() == NewCBs.size()) {
282         for (unsigned N = 0; N < OldCBs.size(); ++N) {
283           CallGraphNode *OldNode = OldCBs[N];
284           CallGraphNode *NewNode = NewCBs[N];
285           for (auto J = CalledFunctions.begin();; ++J) {
286             assert(J != CalledFunctions.end() &&
287                    "Cannot find callsite to update!");
288             if (!J->first && J->second == OldNode) {
289               J->second = NewNode;
290               OldNode->DropRef();
291               NewNode->AddRef();
292               break;
293             }
294           }
295         }
296       } else {
297         for (auto *CGN : OldCBs)
298           removeOneAbstractEdgeTo(CGN);
299         for (auto *CGN : NewCBs)
300           addCalledFunction(nullptr, CGN);
301       }
302       return;
303     }
304   }
305 }
306 
307 // Provide an explicit template instantiation for the static ID.
308 AnalysisKey CallGraphAnalysis::Key;
309 
310 PreservedAnalyses CallGraphPrinterPass::run(Module &M,
311                                             ModuleAnalysisManager &AM) {
312   AM.getResult<CallGraphAnalysis>(M).print(OS);
313   return PreservedAnalyses::all();
314 }
315 
316 PreservedAnalyses CallGraphSCCsPrinterPass::run(Module &M,
317                                                 ModuleAnalysisManager &AM) {
318   auto &CG = AM.getResult<CallGraphAnalysis>(M);
319   unsigned sccNum = 0;
320   OS << "SCCs for the program in PostOrder:";
321   for (scc_iterator<CallGraph *> SCCI = scc_begin(&CG); !SCCI.isAtEnd();
322        ++SCCI) {
323     const std::vector<CallGraphNode *> &nextSCC = *SCCI;
324     OS << "\nSCC #" << ++sccNum << ": ";
325     bool First = true;
326     for (std::vector<CallGraphNode *>::const_iterator I = nextSCC.begin(),
327                                                       E = nextSCC.end();
328          I != E; ++I) {
329       if (First)
330         First = false;
331       else
332         OS << ", ";
333       OS << ((*I)->getFunction() ? (*I)->getFunction()->getName()
334                                  : "external node");
335     }
336 
337     if (nextSCC.size() == 1 && SCCI.hasCycle())
338       OS << " (Has self-loop).";
339   }
340   OS << "\n";
341   return PreservedAnalyses::all();
342 }
343 
344 //===----------------------------------------------------------------------===//
345 // Out-of-line definitions of CallGraphAnalysis class members.
346 //
347 
348 //===----------------------------------------------------------------------===//
349 // Implementations of the CallGraphWrapperPass class methods.
350 //
351 
352 CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) {
353   initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry());
354 }
355 
356 CallGraphWrapperPass::~CallGraphWrapperPass() = default;
357 
358 void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
359   AU.setPreservesAll();
360 }
361 
362 bool CallGraphWrapperPass::runOnModule(Module &M) {
363   // All the real work is done in the constructor for the CallGraph.
364   G.reset(new CallGraph(M));
365   return false;
366 }
367 
368 INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction",
369                 false, true)
370 
371 char CallGraphWrapperPass::ID = 0;
372 
373 void CallGraphWrapperPass::releaseMemory() { G.reset(); }
374 
375 void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const {
376   if (!G) {
377     OS << "No call graph has been built!\n";
378     return;
379   }
380 
381   // Just delegate.
382   G->print(OS);
383 }
384 
385 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
386 LLVM_DUMP_METHOD
387 void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); }
388 #endif
389 
390 namespace {
391 
392 struct CallGraphPrinterLegacyPass : public ModulePass {
393   static char ID; // Pass ID, replacement for typeid
394 
395   CallGraphPrinterLegacyPass() : ModulePass(ID) {
396     initializeCallGraphPrinterLegacyPassPass(*PassRegistry::getPassRegistry());
397   }
398 
399   void getAnalysisUsage(AnalysisUsage &AU) const override {
400     AU.setPreservesAll();
401     AU.addRequiredTransitive<CallGraphWrapperPass>();
402   }
403 
404   bool runOnModule(Module &M) override {
405     getAnalysis<CallGraphWrapperPass>().print(errs(), &M);
406     return false;
407   }
408 };
409 
410 } // end anonymous namespace
411 
412 char CallGraphPrinterLegacyPass::ID = 0;
413 
414 INITIALIZE_PASS_BEGIN(CallGraphPrinterLegacyPass, "print-callgraph",
415                       "Print a call graph", true, true)
416 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
417 INITIALIZE_PASS_END(CallGraphPrinterLegacyPass, "print-callgraph",
418                     "Print a call graph", true, true)
419