Chris Lattner | d852cc3 | 2002-03-06 18:00:49 +0000 | [diff] [blame] | 1 | //===- CallGraph.cpp - Build a Module's call graph ------------------------===// |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | beed742 | 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 | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 5 | // |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 6 | // Every function in a module is represented as a node in the call graph. The |
| 7 | // callgraph node keeps track of which functions the are called by the function |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 8 | // corresponding to the node. |
| 9 | // |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 10 | // A call graph will contain nodes where the function that they correspond to is |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 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 |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 13 | // have edges to functions with the following properties: |
| 14 | // 1. All functions in the module without internal linkage, since they could |
| 15 | // be called by functions outside of the our analysis capability. |
| 16 | // 2. All functions whose address is used for something more than a direct |
| 17 | // call, for example being stored into a memory location. Since they may |
| 18 | // be called by an unknown caller later, they must be tracked as such. |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 19 | // |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 20 | // Similarly, functions have a call edge to the external node iff: |
| 21 | // 1. The function is external, reflecting the fact that they could call |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 22 | // anything without internal linkage or that has its address taken. |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 23 | // 2. The function contains an indirect function call. |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 24 | // |
| 25 | // As an extension in the future, there may be multiple nodes with a null |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 26 | // function. These will be used when we can prove (through pointer analysis) |
| 27 | // that an indirect call site can call only a specific set of functions. |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 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 |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 34 | // CallGraph is, which is currently does by looking for a function named 'main'. |
| 35 | // If no function named 'main' is found, the external node is used as the entry |
| 36 | // node, reflecting the fact that any function without internal linkage could |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 37 | // be called into (which is common for libraries). |
Chris Lattner | 8cbbbef | 2001-10-13 06:33:19 +0000 | [diff] [blame] | 38 | // |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | |
| 41 | #include "llvm/Analysis/CallGraph.h" |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 42 | #include "llvm/Module.h" |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 43 | #include "llvm/iOther.h" |
Chris Lattner | 8cbbbef | 2001-10-13 06:33:19 +0000 | [diff] [blame] | 44 | #include "llvm/iTerminators.h" |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 45 | #include "Support/STLExtras.h" |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 46 | #include <algorithm> |
Chris Lattner | a2c0985 | 2002-07-26 21:12:44 +0000 | [diff] [blame^] | 47 | |
| 48 | static RegisterAnalysis<CallGraph> X("callgraph", "Call Graph Construction"); |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 8032732 | 2002-03-06 17:16:43 +0000 | [diff] [blame] | 50 | AnalysisID CallGraph::ID(AnalysisID::create<CallGraph>()); |
Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 52 | // getNodeFor - Return the node for the specified function or create one if it |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 53 | // does not already exist. |
| 54 | // |
Chris Lattner | 0df67e3 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 55 | CallGraphNode *CallGraph::getNodeFor(Function *F) { |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 56 | CallGraphNode *&CGN = FunctionMap[F]; |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 57 | if (CGN) return CGN; |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 0df67e3 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 59 | assert((!F || F->getParent() == Mod) && "Function not in current module!"); |
| 60 | return CGN = new CallGraphNode(F); |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 63 | // addToCallGraph - Add a function to the call graph, and link the node to all |
| 64 | // of the functions that it calls. |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 65 | // |
Chris Lattner | 0df67e3 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 66 | void CallGraph::addToCallGraph(Function *M) { |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 67 | CallGraphNode *Node = getNodeFor(M); |
| 68 | |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 69 | // If this function has external linkage, |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 70 | if (!M->hasInternalLinkage()) { |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 71 | ExternalNode->addCalledFunction(Node); |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 72 | |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 73 | // 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? |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 81 | Node->addCalledFunction(ExternalNode); // It could call anything... |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 84 | // Loop over all of the users of the function... looking for callers... |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 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)) |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 89 | getNodeFor(CI->getParent()->getParent())->addCalledFunction(Node); |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 90 | else if (InvokeInst *II = dyn_cast<InvokeInst>(U)) |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 91 | getNodeFor(II->getParent()->getParent())->addCalledFunction(Node); |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 92 | else // Can't classify the user! |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 93 | ExternalNode->addCalledFunction(Node); |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 96 | // Look for an indirect function call... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 97 | for (Function::iterator BB = M->begin(), BBE = M->end(); BB != BBE; ++BB) |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 98 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II){ |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 99 | Instruction &I = *II; |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 101 | if (CallInst *CI = dyn_cast<CallInst>(&I)) { |
Chris Lattner | 15deaa0 | 2002-03-29 17:08:29 +0000 | [diff] [blame] | 102 | if (CI->getCalledFunction() == 0) |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 103 | Node->addCalledFunction(ExternalNode); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 104 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) { |
Chris Lattner | 15deaa0 | 2002-03-29 17:08:29 +0000 | [diff] [blame] | 105 | if (II->getCalledFunction() == 0) |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 106 | Node->addCalledFunction(ExternalNode); |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 111 | bool CallGraph::run(Module &M) { |
Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 112 | destroy(); |
| 113 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 114 | Mod = &M; |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 115 | ExternalNode = getNodeFor(0); |
| 116 | Root = 0; |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 118 | // Add every function to the call graph... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 119 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 120 | addToCallGraph(I); |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 122 | // If we didn't find a main function, use the external call graph node |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 123 | if (Root == 0) Root = ExternalNode; |
Chris Lattner | ccf571a | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 124 | |
| 125 | return false; |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chris Lattner | 8032732 | 2002-03-06 17:16:43 +0000 | [diff] [blame] | 128 | void CallGraph::destroy() { |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 129 | for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end(); |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 130 | I != E; ++I) |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 131 | delete I->second; |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 132 | FunctionMap.clear(); |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 8032732 | 2002-03-06 17:16:43 +0000 | [diff] [blame] | 136 | void WriteToOutput(const CallGraphNode *CGN, std::ostream &o) { |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 137 | if (CGN->getFunction()) |
| 138 | o << "Call graph node for function: '" |
| 139 | << CGN->getFunction()->getName() <<"'\n"; |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 140 | else |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 141 | o << "Call graph node null function:\n"; |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 142 | |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 143 | for (unsigned i = 0; i < CGN->size(); ++i) |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 144 | if ((*CGN)[i]->getFunction()) |
| 145 | o << " Calls function '" << (*CGN)[i]->getFunction()->getName() << "'\n"; |
Chris Lattner | beed742 | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 146 | else |
| 147 | o << " Calls external node\n"; |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 148 | o << "\n"; |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Chris Lattner | 8032732 | 2002-03-06 17:16:43 +0000 | [diff] [blame] | 151 | void WriteToOutput(const CallGraph &CG, std::ostream &o) { |
Chris Lattner | ac7c298 | 2002-04-10 20:31:44 +0000 | [diff] [blame] | 152 | o << "CallGraph Root is:\n" << CG.getRoot(); |
| 153 | |
Chris Lattner | bbf3ae8 | 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 | 5dab57d | 2001-10-22 13:55:46 +0000 | [diff] [blame] | 157 | |
| 158 | |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 159 | //===----------------------------------------------------------------------===// |
| 160 | // Implementations of public modification methods |
| 161 | // |
| 162 | |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 163 | // Functions to keep a call graph up to date with a function that has been |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 164 | // modified |
| 165 | // |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 166 | void CallGraph::addFunctionToModule(Function *Meth) { |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 167 | assert(0 && "not implemented"); |
| 168 | abort(); |
| 169 | } |
| 170 | |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 171 | // removeFunctionFromModule - Unlink the function from this module, returning |
| 172 | // it. Because this removes the function from the module, the call graph node |
| 173 | // is destroyed. This is only valid if the function does not call any other |
| 174 | // functions (ie, there are no edges in it's CGN). The easiest way to do this |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 175 | // is to dropAllReferences before calling this. |
| 176 | // |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 177 | Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) { |
| 178 | assert(CGN->CalledFunctions.empty() && "Cannot remove function from call " |
| 179 | "graph if it references other functions!"); |
| 180 | Function *M = CGN->getFunction(); // Get the function for the call graph node |
| 181 | delete CGN; // Delete the call graph node for this func |
| 182 | FunctionMap.erase(M); // Remove the call graph node from the map |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 0df67e3 | 2002-03-26 17:55:33 +0000 | [diff] [blame] | 184 | Mod->getFunctionList().remove(M); |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 185 | return M; |
| 186 | } |
| 187 | |