Chris Lattner | 8d5a16c | 2002-03-06 18:00:49 +0000 | [diff] [blame] | 1 | //===- CallGraph.cpp - Build a Module's call graph ------------------------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 10 | // This file implements the CallGraph class and provides the BasicCallGraph |
| 11 | // default implementation. |
Chris Lattner | 9f9e2be | 2001-10-13 06:33:19 +0000 | [diff] [blame] | 12 | // |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/CallGraph.h" |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Chris Lattner | 07a38e7 | 2003-10-31 21:05:12 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CallSite.h" |
Chris Lattner | af8a424 | 2004-08-08 03:27:49 +0000 | [diff] [blame] | 19 | #include <iostream> |
Chris Lattner | b81c021 | 2004-04-12 05:36:32 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 22 | void llvm::BasicCallGraphStub() {} |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 07a38e7 | 2003-10-31 21:05:12 +0000 | [diff] [blame] | 24 | static bool isOnlyADirectCall(Function *F, CallSite CS) { |
| 25 | if (!CS.getInstruction()) return false; |
| 26 | for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); I != E; ++I) |
| 27 | if (*I == F) return false; |
| 28 | return true; |
| 29 | } |
| 30 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 31 | namespace { |
| 32 | |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | // BasicCallGraph class definition |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 35 | // |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 36 | class BasicCallGraph : public CallGraph, public ModulePass { |
| 37 | // Root is root of the call graph, or the external node if a 'main' function |
| 38 | // couldn't be found. |
| 39 | // |
| 40 | CallGraphNode *Root; |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 42 | // ExternalCallingNode - This node has edges to all external functions and |
| 43 | // those internal functions that have their address taken. |
| 44 | CallGraphNode *ExternalCallingNode; |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 46 | // CallsExternalNode - This node has edges to it from all functions making |
| 47 | // indirect calls or calling an external function. |
| 48 | CallGraphNode *CallsExternalNode; |
| 49 | |
| 50 | public: |
| 51 | BasicCallGraph() : Root(0), ExternalCallingNode(0), CallsExternalNode(0) {} |
| 52 | ~BasicCallGraph() { destroy(); } |
| 53 | |
| 54 | // runOnModule - Compute the call graph for the specified module. |
| 55 | virtual bool runOnModule(Module &M) { |
| 56 | destroy(); |
| 57 | CallGraph::initialize(M); |
| 58 | |
| 59 | ExternalCallingNode = getNodeFor(0); |
| 60 | CallsExternalNode = new CallGraphNode(0); |
| 61 | Root = 0; |
| 62 | |
| 63 | // Add every function to the call graph... |
| 64 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 65 | addToCallGraph(I); |
| 66 | |
| 67 | // If we didn't find a main function, use the external call graph node |
| 68 | if (Root == 0) Root = ExternalCallingNode; |
| 69 | |
| 70 | return false; |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 71 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 73 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 74 | AU.setPreservesAll(); |
| 75 | } |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 77 | virtual void print(std::ostream &o, const Module *M) const { |
| 78 | o << "CallGraph Root is: "; |
| 79 | if (Function *F = getRoot()->getFunction()) |
| 80 | o << F->getName() << "\n"; |
| 81 | else |
| 82 | o << "<<null function: 0x" << getRoot() << ">>\n"; |
| 83 | |
| 84 | CallGraph::print(o, M); |
| 85 | } |
| 86 | |
| 87 | virtual void releaseMemory() { |
| 88 | destroy(); |
| 89 | } |
| 90 | |
| 91 | /// dump - Print out this call graph. |
| 92 | /// |
| 93 | inline void dump() const { |
| 94 | print(std::cerr, Mod); |
| 95 | } |
| 96 | |
| 97 | CallGraphNode* getExternalCallingNode() const { return ExternalCallingNode; } |
| 98 | CallGraphNode* getCallsExternalNode() const { return CallsExternalNode; } |
| 99 | |
| 100 | // getRoot - Return the root of the call graph, which is either main, or if |
| 101 | // main cannot be found, the external node. |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 102 | // |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 103 | CallGraphNode *getRoot() { return Root; } |
| 104 | const CallGraphNode *getRoot() const { return Root; } |
| 105 | |
| 106 | private: |
| 107 | //===--------------------------------------------------------------------- |
| 108 | // Implementation of CallGraph construction |
| 109 | // |
| 110 | // getNodeFor - Return the node for the specified function or create one if it |
| 111 | // does not already exist. |
| 112 | // |
| 113 | |
| 114 | CallGraphNode *getNodeFor(Function *F) { |
| 115 | CallGraphNode *&CGN = FunctionMap[F]; |
| 116 | if (CGN) return CGN; |
| 117 | |
| 118 | assert((!F || F->getParent() == Mod) && "Function not in current module!"); |
| 119 | return CGN = new CallGraphNode(F); |
| 120 | } |
| 121 | |
| 122 | // |
| 123 | // addToCallGraph - Add a function to the call graph, and link the node to all |
| 124 | // of the functions that it calls. |
| 125 | // |
| 126 | void addToCallGraph(Function *F) { |
| 127 | CallGraphNode *Node = getNodeFor(F); |
| 128 | |
| 129 | // If this function has external linkage, anything could call it... |
| 130 | if (!F->hasInternalLinkage()) { |
| 131 | ExternalCallingNode->addCalledFunction(Node); |
| 132 | |
| 133 | // Found the entry point? |
| 134 | if (F->getName() == "main") { |
| 135 | if (Root) // Found multiple external mains? Don't pick one. |
| 136 | Root = ExternalCallingNode; |
| 137 | else |
| 138 | Root = Node; // Found a main, keep track of it! |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // If this function is not defined in this translation unit, it could call |
| 143 | // anything. |
| 144 | if (F->isExternal() && !F->getIntrinsicID()) |
| 145 | Node->addCalledFunction(CallsExternalNode); |
| 146 | |
| 147 | // Loop over all of the users of the function... looking for callers... |
| 148 | // |
| 149 | bool isUsedExternally = false; |
| 150 | for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; ++I){ |
| 151 | if (Instruction *Inst = dyn_cast<Instruction>(*I)) { |
| 152 | if (isOnlyADirectCall(F, CallSite::get(Inst))) |
| 153 | getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node); |
| 154 | else |
| 155 | isUsedExternally = true; |
| 156 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(*I)) { |
| 157 | for (Value::use_iterator I = GV->use_begin(), E = GV->use_end(); |
| 158 | I != E; ++I) |
| 159 | if (Instruction *Inst = dyn_cast<Instruction>(*I)) { |
Chris Lattner | 07a38e7 | 2003-10-31 21:05:12 +0000 | [diff] [blame] | 160 | if (isOnlyADirectCall(F, CallSite::get(Inst))) |
| 161 | getNodeFor(Inst->getParent()->getParent())->addCalledFunction(Node); |
| 162 | else |
| 163 | isUsedExternally = true; |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 164 | } else { |
| 165 | isUsedExternally = true; |
| 166 | } |
| 167 | } else { // Can't classify the user! |
| 168 | isUsedExternally = true; |
| 169 | } |
Chris Lattner | 07a38e7 | 2003-10-31 21:05:12 +0000 | [diff] [blame] | 170 | } |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 171 | if (isUsedExternally) |
| 172 | ExternalCallingNode->addCalledFunction(Node); |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 173 | |
Chris Lattner | d99d4d7 | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 174 | // Look for an indirect function call... |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 175 | for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB) |
| 176 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); |
| 177 | II != IE; ++II) { |
Chris Lattner | 07a38e7 | 2003-10-31 21:05:12 +0000 | [diff] [blame] | 178 | CallSite CS = CallSite::get(II); |
| 179 | if (CS.getInstruction() && !CS.getCalledFunction()) |
Chris Lattner | b81c021 | 2004-04-12 05:36:32 +0000 | [diff] [blame] | 180 | Node->addCalledFunction(CallsExternalNode); |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 181 | } |
| 182 | } |
| 183 | |
| 184 | // |
| 185 | // destroy - Release memory for the call graph |
| 186 | virtual void destroy() { |
| 187 | if (!CallsExternalNode) { |
| 188 | delete CallsExternalNode; |
| 189 | CallsExternalNode = 0; |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 190 | } |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 191 | } |
| 192 | }; |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 194 | RegisterAnalysisGroup<CallGraph> X("Call Graph"); |
| 195 | RegisterOpt<BasicCallGraph> Y("basiccg", "Basic CallGraph Construction"); |
| 196 | RegisterAnalysisGroup<CallGraph, BasicCallGraph, true> Z; |
| 197 | |
| 198 | } //End anonymous namespace |
| 199 | |
| 200 | void CallGraph::initialize(Module &M) { |
Chris Lattner | 93193f8 | 2002-01-31 00:42:27 +0000 | [diff] [blame] | 201 | destroy(); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 202 | Mod = &M; |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Chris Lattner | 4ce0f8a | 2002-03-06 17:16:43 +0000 | [diff] [blame] | 205 | void CallGraph::destroy() { |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 206 | if(!FunctionMap.size()) { |
| 207 | for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end(); |
| 208 | I != E; ++I) |
| 209 | delete I->second; |
| 210 | FunctionMap.clear(); |
| 211 | } |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Chris Lattner | af8a424 | 2004-08-08 03:27:49 +0000 | [diff] [blame] | 214 | void CallGraph::print(std::ostream &OS, const Module *M) const { |
Chris Lattner | 1650015 | 2002-11-04 00:21:19 +0000 | [diff] [blame] | 215 | for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I) |
Chris Lattner | af8a424 | 2004-08-08 03:27:49 +0000 | [diff] [blame] | 216 | I->second->print(OS); |
| 217 | } |
| 218 | |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 219 | //===----------------------------------------------------------------------===// |
| 220 | // Implementations of public modification methods |
| 221 | // |
| 222 | |
Chris Lattner | d99d4d7 | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 223 | // removeFunctionFromModule - Unlink the function from this module, returning |
| 224 | // it. Because this removes the function from the module, the call graph node |
| 225 | // is destroyed. This is only valid if the function does not call any other |
| 226 | // functions (ie, there are no edges in it's CGN). The easiest way to do this |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 227 | // is to dropAllReferences before calling this. |
| 228 | // |
Chris Lattner | d99d4d7 | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 229 | Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) { |
| 230 | assert(CGN->CalledFunctions.empty() && "Cannot remove function from call " |
| 231 | "graph if it references other functions!"); |
Chris Lattner | 5714c97 | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 232 | Function *F = CGN->getFunction(); // Get the function for the call graph node |
Chris Lattner | d99d4d7 | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 233 | delete CGN; // Delete the call graph node for this func |
Chris Lattner | 5714c97 | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 234 | FunctionMap.erase(F); // Remove the call graph node from the map |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 5714c97 | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 236 | Mod->getFunctionList().remove(F); |
| 237 | return F; |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Chris Lattner | 6f7e5eb | 2004-09-18 00:22:13 +0000 | [diff] [blame] | 240 | // changeFunction - This method changes the function associated with this |
| 241 | // CallGraphNode, for use by transformations that need to change the prototype |
| 242 | // of a Function (thus they must create a new Function and move the old code |
| 243 | // over). |
| 244 | void CallGraph::changeFunction(Function *OldF, Function *NewF) { |
| 245 | iterator I = FunctionMap.find(OldF); |
| 246 | CallGraphNode *&New = FunctionMap[NewF]; |
| 247 | assert(I != FunctionMap.end() && I->second && !New && |
| 248 | "OldF didn't exist in CG or NewF already does!"); |
| 249 | New = I->second; |
Chris Lattner | 3795bc9 | 2004-09-18 00:27:20 +0000 | [diff] [blame] | 250 | New->F = NewF; |
Chris Lattner | 6f7e5eb | 2004-09-18 00:22:13 +0000 | [diff] [blame] | 251 | FunctionMap.erase(I); |
| 252 | } |
| 253 | |
Chris Lattner | 14fffaf | 2003-10-30 05:17:30 +0000 | [diff] [blame] | 254 | void CallGraph::stub() {} |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame^] | 256 | void CallGraphNode::print(std::ostream &OS) const { |
| 257 | if (Function *F = getFunction()) |
| 258 | OS << "Call graph node for function: '" << F->getName() <<"'\n"; |
| 259 | else |
| 260 | OS << "Call graph node <<null function: 0x" << this << ">>:\n"; |
| 261 | |
| 262 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
| 263 | if ((*I)->getFunction()) |
| 264 | OS << " Calls function '" << (*I)->getFunction()->getName() << "'\n"; |
| 265 | else |
| 266 | OS << " Calls external node\n"; |
| 267 | OS << "\n"; |
| 268 | } |
| 269 | |
| 270 | void CallGraphNode::dump() const { print(std::cerr); } |
| 271 | |
Chris Lattner | b81c021 | 2004-04-12 05:36:32 +0000 | [diff] [blame] | 272 | void CallGraphNode::removeCallEdgeTo(CallGraphNode *Callee) { |
| 273 | for (unsigned i = CalledFunctions.size(); ; --i) { |
| 274 | assert(i && "Cannot find callee to remove!"); |
| 275 | if (CalledFunctions[i-1] == Callee) { |
| 276 | CalledFunctions.erase(CalledFunctions.begin()+i-1); |
| 277 | return; |
| 278 | } |
| 279 | } |
| 280 | } |
Chris Lattner | cd382a3 | 2004-09-18 21:34:34 +0000 | [diff] [blame] | 281 | |
| 282 | // removeAnyCallEdgeTo - This method removes any call edges from this node to |
| 283 | // the specified callee function. This takes more time to execute than |
| 284 | // removeCallEdgeTo, so it should not be used unless necessary. |
| 285 | void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) { |
Chris Lattner | fff03c9 | 2004-09-19 19:01:06 +0000 | [diff] [blame] | 286 | for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i) |
| 287 | if (CalledFunctions[i] == Callee) { |
| 288 | CalledFunctions[i] = CalledFunctions.back(); |
| 289 | CalledFunctions.pop_back(); |
| 290 | --i; --e; |
Chris Lattner | cd382a3 | 2004-09-18 21:34:34 +0000 | [diff] [blame] | 291 | } |
| 292 | } |