blob: 6a5cad4244a3dabc2bf3bc07c0813be5473a073f [file] [log] [blame]
Chris Lattner8d5a16c2002-03-06 18:00:49 +00001//===- CallGraph.cpp - Build a Module's call graph ------------------------===//
Chris Lattner41fbf302001-09-28 00:08:15 +00002//
Chris Lattnerd4d427b2002-03-06 20:19:35 +00003// This interface is used to build and manipulate a call graph, which is a very
4// useful tool for interprocedural optimization.
Chris Lattner41fbf302001-09-28 00:08:15 +00005//
Chris Lattnerd4d427b2002-03-06 20:19:35 +00006// Every method in a module is represented as a node in the call graph. The
7// callgraph node keeps track of which methods the are called by the method
8// corresponding to the node.
9//
10// A call graph will contain nodes where the method that they correspond to is
11// null. This 'external' node is used to represent control flow that is not
12// represented (or analyzable) in the module. As such, the external node will
13// have edges to methods with the following properties:
14// 1. All methods in the module without internal linkage, since they could
15// be called by methods outside of the our analysis capability.
16// 2. All methods whose address is used for something more than a direct call,
17// for example being stored into a memory location. Since they may be
18// called by an unknown caller later, they must be tracked as such.
19//
20// Similarly, methods have a call edge to the external node iff:
21// 1. The method is external, reflecting the fact that they could call
22// anything without internal linkage or that has its address taken.
23// 2. The method contains an indirect method call.
24//
25// As an extension in the future, there may be multiple nodes with a null
26// method. These will be used when we can prove (through pointer analysis) that
27// an indirect call site can call only a specific set of methods.
28//
29// Because of these properties, the CallGraph captures a conservative superset
30// of all of the caller-callee relationships, which is useful for
31// transformations.
32//
33// The CallGraph class also attempts to figure out what the root of the
34// CallGraph is, which is currently does by looking for a method named 'main'.
35// If no method named 'main' is found, the external node is used as the entry
36// node, reflecting the fact that any method without internal linkage could
37// be called into (which is common for libraries).
Chris Lattner9f9e2be2001-10-13 06:33:19 +000038//
Chris Lattner41fbf302001-09-28 00:08:15 +000039//===----------------------------------------------------------------------===//
40
41#include "llvm/Analysis/CallGraph.h"
Chris Lattner41fbf302001-09-28 00:08:15 +000042#include "llvm/Module.h"
43#include "llvm/Method.h"
44#include "llvm/iOther.h"
Chris Lattner9f9e2be2001-10-13 06:33:19 +000045#include "llvm/iTerminators.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000046#include "Support/STLExtras.h"
Chris Lattner41fbf302001-09-28 00:08:15 +000047#include <algorithm>
Chris Lattner8d5a16c2002-03-06 18:00:49 +000048#include <iostream>
Chris Lattner41fbf302001-09-28 00:08:15 +000049
Chris Lattner4ce0f8a2002-03-06 17:16:43 +000050AnalysisID CallGraph::ID(AnalysisID::create<CallGraph>());
Chris Lattner93193f82002-01-31 00:42:27 +000051
Chris Lattner41fbf302001-09-28 00:08:15 +000052// getNodeFor - Return the node for the specified method or create one if it
53// does not already exist.
54//
Chris Lattner4ce0f8a2002-03-06 17:16:43 +000055CallGraphNode *CallGraph::getNodeFor(Method *M) {
Chris Lattnerd4d427b2002-03-06 20:19:35 +000056 CallGraphNode *&CGN = MethodMap[M];
57 if (CGN) return CGN;
Chris Lattner41fbf302001-09-28 00:08:15 +000058
Chris Lattnerd4d427b2002-03-06 20:19:35 +000059 assert((!M || M->getParent() == Mod) && "Method not in current module!");
60 return CGN = new CallGraphNode(M);
Chris Lattner41fbf302001-09-28 00:08:15 +000061}
62
63// addToCallGraph - Add a method to the call graph, and link the node to all of
64// the methods that it calls.
65//
Chris Lattner4ce0f8a2002-03-06 17:16:43 +000066void CallGraph::addToCallGraph(Method *M) {
Chris Lattner41fbf302001-09-28 00:08:15 +000067 CallGraphNode *Node = getNodeFor(M);
68
Chris Lattner25e9cad2001-11-26 18:51:25 +000069 // If this method has external linkage,
Chris Lattnerd4d427b2002-03-06 20:19:35 +000070 if (!M->hasInternalLinkage()) {
71 ExternalNode->addCalledMethod(Node);
Chris Lattner25e9cad2001-11-26 18:51:25 +000072
Chris Lattnerd4d427b2002-03-06 20:19:35 +000073 // Found the entry point?
74 if (M->getName() == "main") {
75 if (Root)
76 Root = ExternalNode; // Found multiple external mains? Don't pick one.
77 else
78 Root = Node; // Found a main, keep track of it!
79 }
80 } else if (M->isExternal()) { // Not defined in this xlation unit?
81 Node->addCalledMethod(ExternalNode); // It could call anything...
82 }
83
84 // Loop over all of the users of the method... looking for callers...
85 //
86 for (Value::use_iterator I = M->use_begin(), E = M->use_end(); I != E; ++I) {
87 User *U = *I;
88 if (CallInst *CI = dyn_cast<CallInst>(U))
89 getNodeFor(CI->getParent()->getParent())->addCalledMethod(Node);
90 else if (InvokeInst *II = dyn_cast<InvokeInst>(U))
91 getNodeFor(II->getParent()->getParent())->addCalledMethod(Node);
92 else // Can't classify the user!
93 ExternalNode->addCalledMethod(Node);
94 }
95
96 // Look for an indirect method call...
97 for (Method::iterator BBI = M->begin(), BBE = M->end(); BBI != BBE; ++BBI) {
98 BasicBlock *BB = *BBI;
99 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){
100 Instruction *I = *II;
101
102 if (CallInst *CI = dyn_cast<CallInst>(I)) {
103 if (CI->getCalledMethod() == 0)
104 Node->addCalledMethod(ExternalNode);
105 } else if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
106 if (II->getCalledMethod() == 0)
107 Node->addCalledMethod(ExternalNode);
108 }
109 }
Chris Lattner41fbf302001-09-28 00:08:15 +0000110 }
111}
112
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000113bool CallGraph::run(Module *TheModule) {
Chris Lattner93193f82002-01-31 00:42:27 +0000114 destroy();
115
Chris Lattner41fbf302001-09-28 00:08:15 +0000116 Mod = TheModule;
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000117 ExternalNode = getNodeFor(0);
118 Root = 0;
Chris Lattner25e9cad2001-11-26 18:51:25 +0000119
Chris Lattner41fbf302001-09-28 00:08:15 +0000120 // Add every method to the call graph...
121 for_each(Mod->begin(), Mod->end(), bind_obj(this,&CallGraph::addToCallGraph));
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000122
123 // If we didn't find a main method, use the external call graph node
124 if (Root == 0) Root = ExternalNode;
Chris Lattner93193f82002-01-31 00:42:27 +0000125
126 return false;
Chris Lattner41fbf302001-09-28 00:08:15 +0000127}
128
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000129void CallGraph::destroy() {
Chris Lattner25e9cad2001-11-26 18:51:25 +0000130 for (MethodMapTy::iterator I = MethodMap.begin(), E = MethodMap.end();
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000131 I != E; ++I)
Chris Lattner25e9cad2001-11-26 18:51:25 +0000132 delete I->second;
Chris Lattner93193f82002-01-31 00:42:27 +0000133 MethodMap.clear();
Chris Lattner25e9cad2001-11-26 18:51:25 +0000134}
135
Chris Lattner41fbf302001-09-28 00:08:15 +0000136
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000137void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) {
Chris Lattner25e9cad2001-11-26 18:51:25 +0000138 if (CGN->getMethod())
139 o << "Call graph node for method: '" << CGN->getMethod()->getName() <<"'\n";
140 else
141 o << "Call graph node null method:\n";
142
Chris Lattner41fbf302001-09-28 00:08:15 +0000143 for (unsigned i = 0; i < CGN->size(); ++i)
Chris Lattnerd4d427b2002-03-06 20:19:35 +0000144 if ((*CGN)[i]->getMethod())
145 o << " Calls method '" << (*CGN)[i]->getMethod()->getName() << "'\n";
146 else
147 o << " Calls external node\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000148 o << "\n";
Chris Lattner41fbf302001-09-28 00:08:15 +0000149}
150
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000151void WriteToOutput(const CallGraph &CG, std::ostream &o) {
Chris Lattner41fbf302001-09-28 00:08:15 +0000152 for (CallGraph::const_iterator I = CG.begin(), E = CG.end(); I != E; ++I)
153 o << I->second;
154}
Vikram S. Advea7edb182001-10-22 13:55:46 +0000155
156
Chris Lattner25e9cad2001-11-26 18:51:25 +0000157//===----------------------------------------------------------------------===//
158// Implementations of public modification methods
159//
160
161// Methods to keep a call graph up to date with a method that has been
162// modified
163//
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000164void CallGraph::addMethodToModule(Method *Meth) {
Chris Lattner25e9cad2001-11-26 18:51:25 +0000165 assert(0 && "not implemented");
166 abort();
167}
168
169// removeMethodFromModule - Unlink the method from this module, returning it.
170// Because this removes the method from the module, the call graph node is
171// destroyed. This is only valid if the method does not call any other
172// methods (ie, there are no edges in it's CGN). The easiest way to do this
173// is to dropAllReferences before calling this.
174//
Chris Lattner4ce0f8a2002-03-06 17:16:43 +0000175Method *CallGraph::removeMethodFromModule(CallGraphNode *CGN) {
Chris Lattner25e9cad2001-11-26 18:51:25 +0000176 assert(CGN->CalledMethods.empty() && "Cannot remove method from call graph"
177 " if it references other methods!");
178 Method *M = CGN->getMethod(); // Get the method for the call graph node
179 delete CGN; // Delete the call graph node for this method
180 MethodMap.erase(M); // Remove the call graph node from the map
181
182 Mod->getMethodList().remove(M);
183 return M;
184}
185