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