Chris Lattner | 8d5a16c | 2002-03-06 18:00:49 +0000 | [diff] [blame] | 1 | //===- CallGraph.cpp - Build a Module's call graph ------------------------===// |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 3 | // This interface is used to build and manipulate a call graph, which is a very |
| 4 | // useful tool for interprocedural optimization. |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 5 | // |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 6 | // 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 Lattner | 9f9e2be | 2001-10-13 06:33:19 +0000 | [diff] [blame] | 38 | // |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | |
| 41 | #include "llvm/Analysis/CallGraph.h" |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 42 | #include "llvm/Module.h" |
Chris Lattner | e590ff2 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 43 | #include "llvm/Function.h" |
Chris Lattner | 42a4127 | 2002-04-09 18:37:46 +0000 | [diff] [blame] | 44 | #include "llvm/BasicBlock.h" |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 45 | #include "llvm/iOther.h" |
Chris Lattner | 9f9e2be | 2001-10-13 06:33:19 +0000 | [diff] [blame] | 46 | #include "llvm/iTerminators.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 47 | #include "Support/STLExtras.h" |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 48 | #include <algorithm> |
Chris Lattner | 8d5a16c | 2002-03-06 18:00:49 +0000 | [diff] [blame] | 49 | #include <iostream> |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 4ce0f8a | 2002-03-06 17:16:43 +0000 | [diff] [blame] | 51 | AnalysisID CallGraph::ID(AnalysisID::create<CallGraph>()); |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 53 | // getNodeFor - Return the node for the specified method or create one if it |
| 54 | // does not already exist. |
| 55 | // |
Chris Lattner | e590ff2 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 56 | CallGraphNode *CallGraph::getNodeFor(Function *F) { |
| 57 | CallGraphNode *&CGN = MethodMap[F]; |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 58 | if (CGN) return CGN; |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 59 | |
Chris Lattner | e590ff2 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 60 | assert((!F || F->getParent() == Mod) && "Function not in current module!"); |
| 61 | return CGN = new CallGraphNode(F); |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // addToCallGraph - Add a method to the call graph, and link the node to all of |
| 65 | // the methods that it calls. |
| 66 | // |
Chris Lattner | e590ff2 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 67 | void CallGraph::addToCallGraph(Function *M) { |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 68 | CallGraphNode *Node = getNodeFor(M); |
| 69 | |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 70 | // If this method has external linkage, |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 71 | if (!M->hasInternalLinkage()) { |
| 72 | ExternalNode->addCalledMethod(Node); |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 73 | |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 74 | // Found the entry point? |
| 75 | if (M->getName() == "main") { |
| 76 | if (Root) |
| 77 | Root = ExternalNode; // Found multiple external mains? Don't pick one. |
| 78 | else |
| 79 | Root = Node; // Found a main, keep track of it! |
| 80 | } |
| 81 | } else if (M->isExternal()) { // Not defined in this xlation unit? |
| 82 | Node->addCalledMethod(ExternalNode); // It could call anything... |
| 83 | } |
| 84 | |
| 85 | // Loop over all of the users of the method... looking for callers... |
| 86 | // |
| 87 | for (Value::use_iterator I = M->use_begin(), E = M->use_end(); I != E; ++I) { |
| 88 | User *U = *I; |
| 89 | if (CallInst *CI = dyn_cast<CallInst>(U)) |
| 90 | getNodeFor(CI->getParent()->getParent())->addCalledMethod(Node); |
| 91 | else if (InvokeInst *II = dyn_cast<InvokeInst>(U)) |
| 92 | getNodeFor(II->getParent()->getParent())->addCalledMethod(Node); |
| 93 | else // Can't classify the user! |
| 94 | ExternalNode->addCalledMethod(Node); |
| 95 | } |
| 96 | |
| 97 | // Look for an indirect method call... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 98 | for (Function::iterator BB = M->begin(), BBE = M->end(); BB != BBE; ++BB) |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 99 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){ |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 100 | Instruction &I = *II; |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 102 | if (CallInst *CI = dyn_cast<CallInst>(&I)) { |
Chris Lattner | dc89f87 | 2002-03-29 17:08:29 +0000 | [diff] [blame] | 103 | if (CI->getCalledFunction() == 0) |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 104 | Node->addCalledMethod(ExternalNode); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 105 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) { |
Chris Lattner | dc89f87 | 2002-03-29 17:08:29 +0000 | [diff] [blame] | 106 | if (II->getCalledFunction() == 0) |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 107 | Node->addCalledMethod(ExternalNode); |
| 108 | } |
| 109 | } |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 112 | bool CallGraph::run(Module &M) { |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 113 | destroy(); |
| 114 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 115 | Mod = &M; |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 116 | ExternalNode = getNodeFor(0); |
| 117 | Root = 0; |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 119 | // Add every method to the call graph... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 120 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 121 | addToCallGraph(I); |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 122 | |
| 123 | // If we didn't find a main method, use the external call graph node |
| 124 | if (Root == 0) Root = ExternalNode; |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 125 | |
| 126 | return false; |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Chris Lattner | 4ce0f8a | 2002-03-06 17:16:43 +0000 | [diff] [blame] | 129 | void CallGraph::destroy() { |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 130 | for (MethodMapTy::iterator I = MethodMap.begin(), E = MethodMap.end(); |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 131 | I != E; ++I) |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 132 | delete I->second; |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 133 | MethodMap.clear(); |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 4ce0f8a | 2002-03-06 17:16:43 +0000 | [diff] [blame] | 137 | void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) { |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 138 | 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 Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 143 | for (unsigned i = 0; i < CGN->size(); ++i) |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 144 | if ((*CGN)[i]->getMethod()) |
| 145 | o << " Calls method '" << (*CGN)[i]->getMethod()->getName() << "'\n"; |
| 146 | else |
| 147 | o << " Calls external node\n"; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 148 | o << "\n"; |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Chris Lattner | 4ce0f8a | 2002-03-06 17:16:43 +0000 | [diff] [blame] | 151 | void WriteToOutput(const CallGraph &CG, std::ostream &o) { |
Chris Lattner | 5648b58 | 2002-04-10 20:31:44 +0000 | [diff] [blame] | 152 | o << "CallGraph Root is:\n" << CG.getRoot(); |
| 153 | |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 154 | for (CallGraph::const_iterator I = CG.begin(), E = CG.end(); I != E; ++I) |
| 155 | o << I->second; |
| 156 | } |
Vikram S. Adve | a7edb18 | 2001-10-22 13:55:46 +0000 | [diff] [blame] | 157 | |
| 158 | |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 159 | //===----------------------------------------------------------------------===// |
| 160 | // Implementations of public modification methods |
| 161 | // |
| 162 | |
| 163 | // Methods to keep a call graph up to date with a method that has been |
| 164 | // modified |
| 165 | // |
Chris Lattner | e590ff2 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 166 | void CallGraph::addMethodToModule(Function *Meth) { |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 167 | assert(0 && "not implemented"); |
| 168 | abort(); |
| 169 | } |
| 170 | |
| 171 | // removeMethodFromModule - Unlink the method from this module, returning it. |
| 172 | // Because this removes the method from the module, the call graph node is |
| 173 | // destroyed. This is only valid if the method does not call any other |
| 174 | // methods (ie, there are no edges in it's CGN). The easiest way to do this |
| 175 | // is to dropAllReferences before calling this. |
| 176 | // |
Chris Lattner | e590ff2 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 177 | Function *CallGraph::removeMethodFromModule(CallGraphNode *CGN) { |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 178 | assert(CGN->CalledMethods.empty() && "Cannot remove method from call graph" |
| 179 | " if it references other methods!"); |
Chris Lattner | e590ff2 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 180 | Function *M = CGN->getMethod(); // Get the function for the call graph node |
| 181 | delete CGN; // Delete the call graph node for this func |
| 182 | MethodMap.erase(M); // Remove the call graph node from the map |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 183 | |
Chris Lattner | e590ff2 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 184 | Mod->getFunctionList().remove(M); |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 185 | return M; |
| 186 | } |
| 187 | |