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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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" |
Dale Johannesen | 1f67ce4 | 2009-03-19 18:03:56 +0000 | [diff] [blame] | 18 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 07a38e7 | 2003-10-31 21:05:12 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CallSite.h" |
David Greene | 265c026 | 2009-12-23 20:03:58 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | b81c021 | 2004-04-12 05:36:32 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // BasicCallGraph class definition |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 28 | // |
Chris Lattner | cdac3a3 | 2010-01-20 19:51:46 +0000 | [diff] [blame] | 29 | class BasicCallGraph : public ModulePass, public CallGraph { |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 30 | // Root is root of the call graph, or the external node if a 'main' function |
| 31 | // couldn't be found. |
| 32 | // |
| 33 | CallGraphNode *Root; |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 35 | // ExternalCallingNode - This node has edges to all external functions and |
| 36 | // those internal functions that have their address taken. |
| 37 | CallGraphNode *ExternalCallingNode; |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 39 | // CallsExternalNode - This node has edges to it from all functions making |
| 40 | // indirect calls or calling an external function. |
| 41 | CallGraphNode *CallsExternalNode; |
| 42 | |
| 43 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 44 | static char ID; // Class identification, replacement for typeinfo |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 45 | BasicCallGraph() : ModulePass(ID), Root(0), |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 46 | ExternalCallingNode(0), CallsExternalNode(0) { |
| 47 | initializeBasicCallGraphPass(*PassRegistry::getPassRegistry()); |
| 48 | } |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 49 | |
| 50 | // runOnModule - Compute the call graph for the specified module. |
| 51 | virtual bool runOnModule(Module &M) { |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 52 | CallGraph::initialize(M); |
| 53 | |
Chris Lattner | c54b1c1 | 2006-01-14 20:03:00 +0000 | [diff] [blame] | 54 | ExternalCallingNode = getOrInsertFunction(0); |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 55 | CallsExternalNode = new CallGraphNode(0); |
| 56 | Root = 0; |
| 57 | |
Chris Lattner | b374b90 | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 58 | // Add every function to the call graph. |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 59 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 60 | addToCallGraph(I); |
| 61 | |
| 62 | // If we didn't find a main function, use the external call graph node |
| 63 | if (Root == 0) Root = ExternalCallingNode; |
| 64 | |
| 65 | return false; |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 66 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 68 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 69 | AU.setPreservesAll(); |
| 70 | } |
Chris Lattner | d4d427b | 2002-03-06 20:19:35 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 72 | virtual void print(raw_ostream &OS, const Module *) const { |
| 73 | OS << "CallGraph Root is: "; |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 74 | if (Function *F = getRoot()->getFunction()) |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 75 | OS << F->getName() << "\n"; |
| 76 | else { |
| 77 | OS << "<<null function: 0x" << getRoot() << ">>\n"; |
| 78 | } |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 80 | CallGraph::print(OS, 0); |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | virtual void releaseMemory() { |
| 84 | destroy(); |
| 85 | } |
| 86 | |
Chris Lattner | cdac3a3 | 2010-01-20 19:51:46 +0000 | [diff] [blame] | 87 | /// getAdjustedAnalysisPointer - This method is used when a pass implements |
| 88 | /// an analysis interface through multiple inheritance. If needed, it should |
| 89 | /// override this to adjust the this pointer as needed for the specified pass |
| 90 | /// info. |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 91 | virtual void *getAdjustedAnalysisPointer(AnalysisID PI) { |
| 92 | if (PI == &CallGraph::ID) |
Chris Lattner | cdac3a3 | 2010-01-20 19:51:46 +0000 | [diff] [blame] | 93 | return (CallGraph*)this; |
| 94 | return this; |
| 95 | } |
| 96 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 111 | // addToCallGraph - Add a function to the call graph, and link the node to all |
| 112 | // of the functions that it calls. |
| 113 | // |
| 114 | void addToCallGraph(Function *F) { |
Chris Lattner | c54b1c1 | 2006-01-14 20:03:00 +0000 | [diff] [blame] | 115 | CallGraphNode *Node = getOrInsertFunction(F); |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 116 | |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 117 | // If this function has external linkage, anything could call it. |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 118 | if (!F->hasLocalLinkage()) { |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 119 | ExternalCallingNode->addCalledFunction(CallSite(), Node); |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 120 | |
| 121 | // Found the entry point? |
| 122 | if (F->getName() == "main") { |
| 123 | if (Root) // Found multiple external mains? Don't pick one. |
| 124 | Root = ExternalCallingNode; |
| 125 | else |
| 126 | Root = Node; // Found a main, keep track of it! |
| 127 | } |
| 128 | } |
| 129 | |
Eli Friedman | 400ea5b | 2011-10-20 03:23:14 +0000 | [diff] [blame] | 130 | // If this function has its address taken, anything could call it. |
| 131 | if (F->hasAddressTaken()) |
| 132 | ExternalCallingNode->addCalledFunction(CallSite(), Node); |
Duncan Sands | 7ca9d81 | 2008-09-09 19:56:34 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 134 | // If this function is not defined in this translation unit, it could call |
| 135 | // anything. |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 136 | if (F->isDeclaration() && !F->isIntrinsic()) |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 137 | Node->addCalledFunction(CallSite(), CallsExternalNode); |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 138 | |
Duncan Sands | 99c1a7c | 2008-09-09 12:40:47 +0000 | [diff] [blame] | 139 | // Look for calls by this function. |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 140 | for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB) |
| 141 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); |
| 142 | II != IE; ++II) { |
Gabor Greif | 3737984 | 2010-07-28 12:19:46 +0000 | [diff] [blame] | 143 | CallSite CS(cast<Value>(II)); |
John McCall | e669d83 | 2011-06-09 19:46:27 +0000 | [diff] [blame] | 144 | if (CS && !isa<IntrinsicInst>(II)) { |
Duncan Sands | 99c1a7c | 2008-09-09 12:40:47 +0000 | [diff] [blame] | 145 | const Function *Callee = CS.getCalledFunction(); |
| 146 | if (Callee) |
| 147 | Node->addCalledFunction(CS, getOrInsertFunction(Callee)); |
| 148 | else |
| 149 | Node->addCalledFunction(CS, CallsExternalNode); |
| 150 | } |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | // |
| 155 | // destroy - Release memory for the call graph |
| 156 | virtual void destroy() { |
Chris Lattner | f2d9ceb | 2006-12-05 19:46:12 +0000 | [diff] [blame] | 157 | /// CallsExternalNode is not in the function map, delete it explicitly. |
Benjamin Kramer | eb1f4b1 | 2010-04-20 12:16:50 +0000 | [diff] [blame] | 158 | if (CallsExternalNode) { |
| 159 | CallsExternalNode->allReferencesDropped(); |
| 160 | delete CallsExternalNode; |
| 161 | CallsExternalNode = 0; |
| 162 | } |
Chris Lattner | f2d9ceb | 2006-12-05 19:46:12 +0000 | [diff] [blame] | 163 | CallGraph::destroy(); |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 164 | } |
| 165 | }; |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 167 | } //End anonymous namespace |
| 168 | |
Owen Anderson | 325e264 | 2010-10-13 21:49:58 +0000 | [diff] [blame] | 169 | INITIALIZE_ANALYSIS_GROUP(CallGraph, "Call Graph", BasicCallGraph) |
Owen Anderson | d8cc7be | 2010-07-21 23:07:00 +0000 | [diff] [blame] | 170 | INITIALIZE_AG_PASS(BasicCallGraph, CallGraph, "basiccg", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 171 | "Basic CallGraph Construction", false, true, true) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 172 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 173 | char CallGraph::ID = 0; |
| 174 | char BasicCallGraph::ID = 0; |
Lauro Ramos Venancio | c718288 | 2007-05-02 20:37:47 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 176 | void CallGraph::initialize(Module &M) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 177 | Mod = &M; |
Chris Lattner | 41fbf30 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Chris Lattner | 4ce0f8a | 2002-03-06 17:16:43 +0000 | [diff] [blame] | 180 | void CallGraph::destroy() { |
Chris Lattner | b374b90 | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 181 | if (FunctionMap.empty()) return; |
| 182 | |
Chris Lattner | 8a39ed7 | 2010-04-20 00:47:34 +0000 | [diff] [blame] | 183 | // Reset all node's use counts to zero before deleting them to prevent an |
| 184 | // assertion from firing. |
| 185 | #ifndef NDEBUG |
| 186 | for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end(); |
| 187 | I != E; ++I) |
| 188 | I->second->allReferencesDropped(); |
| 189 | #endif |
| 190 | |
Chris Lattner | b374b90 | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 191 | for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end(); |
| 192 | I != E; ++I) |
| 193 | delete I->second; |
| 194 | FunctionMap.clear(); |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 197 | void CallGraph::print(raw_ostream &OS, Module*) const { |
Chris Lattner | 1650015 | 2002-11-04 00:21:19 +0000 | [diff] [blame] | 198 | for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I) |
Chris Lattner | af8a424 | 2004-08-08 03:27:49 +0000 | [diff] [blame] | 199 | I->second->print(OS); |
| 200 | } |
Manman Ren | cc77eec | 2012-09-06 19:55:56 +0000 | [diff] [blame^] | 201 | #ifndef NDEBUG |
Chris Lattner | 23603a6 | 2009-08-30 22:24:32 +0000 | [diff] [blame] | 202 | void CallGraph::dump() const { |
David Greene | 265c026 | 2009-12-23 20:03:58 +0000 | [diff] [blame] | 203 | print(dbgs(), 0); |
Chris Lattner | 23603a6 | 2009-08-30 22:24:32 +0000 | [diff] [blame] | 204 | } |
Manman Ren | cc77eec | 2012-09-06 19:55:56 +0000 | [diff] [blame^] | 205 | #endif |
Chris Lattner | af8a424 | 2004-08-08 03:27:49 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 207 | //===----------------------------------------------------------------------===// |
| 208 | // Implementations of public modification methods |
| 209 | // |
| 210 | |
Chris Lattner | d99d4d7 | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 211 | // removeFunctionFromModule - Unlink the function from this module, returning |
| 212 | // it. Because this removes the function from the module, the call graph node |
| 213 | // is destroyed. This is only valid if the function does not call any other |
| 214 | // 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] | 215 | // is to dropAllReferences before calling this. |
| 216 | // |
Chris Lattner | d99d4d7 | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 217 | Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) { |
Chris Lattner | 2adb830 | 2009-08-31 02:24:20 +0000 | [diff] [blame] | 218 | assert(CGN->empty() && "Cannot remove function from call " |
Chris Lattner | d99d4d7 | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 219 | "graph if it references other functions!"); |
Chris Lattner | 5714c97 | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 220 | Function *F = CGN->getFunction(); // Get the function for the call graph node |
Chris Lattner | d99d4d7 | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 221 | delete CGN; // Delete the call graph node for this func |
Chris Lattner | 5714c97 | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 222 | FunctionMap.erase(F); // Remove the call graph node from the map |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 223 | |
Chris Lattner | 5714c97 | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 224 | Mod->getFunctionList().remove(F); |
| 225 | return F; |
Chris Lattner | 25e9cad | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Nick Lewycky | 9ad1cb5 | 2011-01-03 03:19:35 +0000 | [diff] [blame] | 228 | /// spliceFunction - Replace the function represented by this node by another. |
| 229 | /// This does not rescan the body of the function, so it is suitable when |
| 230 | /// splicing the body of the old function to the new while also updating all |
| 231 | /// callers from old to new. |
| 232 | /// |
| 233 | void CallGraph::spliceFunction(const Function *From, const Function *To) { |
| 234 | assert(FunctionMap.count(From) && "No CallGraphNode for function!"); |
| 235 | assert(!FunctionMap.count(To) && |
| 236 | "Pointing CallGraphNode at a function that already exists"); |
| 237 | FunctionMapTy::iterator I = FunctionMap.find(From); |
| 238 | I->second->F = const_cast<Function*>(To); |
| 239 | FunctionMap[To] = I->second; |
| 240 | FunctionMap.erase(I); |
| 241 | } |
| 242 | |
Chris Lattner | c54b1c1 | 2006-01-14 20:03:00 +0000 | [diff] [blame] | 243 | // getOrInsertFunction - This method is identical to calling operator[], but |
| 244 | // it will insert a new CallGraphNode for the specified function if one does |
| 245 | // not already exist. |
| 246 | CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) { |
| 247 | CallGraphNode *&CGN = FunctionMap[F]; |
| 248 | if (CGN) return CGN; |
| 249 | |
| 250 | assert((!F || F->getParent() == Mod) && "Function not in current module!"); |
| 251 | return CGN = new CallGraphNode(const_cast<Function*>(F)); |
| 252 | } |
| 253 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 254 | void CallGraphNode::print(raw_ostream &OS) const { |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 255 | if (Function *F = getFunction()) |
Chris Lattner | b374b90 | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 256 | OS << "Call graph node for function: '" << F->getName() << "'"; |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 257 | else |
Chris Lattner | b374b90 | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 258 | OS << "Call graph node <<null function>>"; |
| 259 | |
Chris Lattner | 6275413 | 2010-04-23 18:23:40 +0000 | [diff] [blame] | 260 | OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n'; |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 6275413 | 2010-04-23 18:23:40 +0000 | [diff] [blame] | 262 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
| 263 | OS << " CS<" << I->first << "> calls "; |
Gabor Greif | 6104626 | 2009-01-14 17:09:04 +0000 | [diff] [blame] | 264 | if (Function *FI = I->second->getFunction()) |
Chris Lattner | 6275413 | 2010-04-23 18:23:40 +0000 | [diff] [blame] | 265 | OS << "function '" << FI->getName() <<"'\n"; |
| 266 | else |
| 267 | OS << "external node\n"; |
| 268 | } |
| 269 | OS << '\n'; |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Manman Ren | cc77eec | 2012-09-06 19:55:56 +0000 | [diff] [blame^] | 272 | #ifndef NDEBUG |
David Greene | 265c026 | 2009-12-23 20:03:58 +0000 | [diff] [blame] | 273 | void CallGraphNode::dump() const { print(dbgs()); } |
Manman Ren | cc77eec | 2012-09-06 19:55:56 +0000 | [diff] [blame^] | 274 | #endif |
Chris Lattner | 0383995 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 75caee2 | 2008-04-13 19:41:25 +0000 | [diff] [blame] | 276 | /// removeCallEdgeFor - This method removes the edge in the node for the |
| 277 | /// specified call site. Note that this method takes linear time, so it |
| 278 | /// should be used sparingly. |
| 279 | void CallGraphNode::removeCallEdgeFor(CallSite CS) { |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 280 | for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { |
| 281 | assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 282 | if (I->first == CS.getInstruction()) { |
Chris Lattner | be57765 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 283 | I->second->DropRef(); |
| 284 | *I = CalledFunctions.back(); |
| 285 | CalledFunctions.pop_back(); |
| 286 | return; |
| 287 | } |
Chris Lattner | 75caee2 | 2008-04-13 19:41:25 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Chris Lattner | cd382a3 | 2004-09-18 21:34:34 +0000 | [diff] [blame] | 291 | // removeAnyCallEdgeTo - This method removes any call edges from this node to |
| 292 | // the specified callee function. This takes more time to execute than |
| 293 | // removeCallEdgeTo, so it should not be used unless necessary. |
| 294 | void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) { |
Chris Lattner | fff03c9 | 2004-09-19 19:01:06 +0000 | [diff] [blame] | 295 | for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i) |
Chris Lattner | d85340f | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 296 | if (CalledFunctions[i].second == Callee) { |
Chris Lattner | b374b90 | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 297 | Callee->DropRef(); |
Chris Lattner | fff03c9 | 2004-09-19 19:01:06 +0000 | [diff] [blame] | 298 | CalledFunctions[i] = CalledFunctions.back(); |
| 299 | CalledFunctions.pop_back(); |
| 300 | --i; --e; |
Chris Lattner | cd382a3 | 2004-09-18 21:34:34 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
Reid Spencer | 4f1bd9e | 2006-06-07 22:00:26 +0000 | [diff] [blame] | 303 | |
Duncan Sands | a2582da | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 304 | /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite |
| 305 | /// from this node to the specified callee function. |
| 306 | void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) { |
Gabor Greif | 7f2e381 | 2009-01-17 19:46:01 +0000 | [diff] [blame] | 307 | for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { |
| 308 | assert(I != CalledFunctions.end() && "Cannot find callee to remove!"); |
| 309 | CallRecord &CR = *I; |
Chris Lattner | a541b0f | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 310 | if (CR.second == Callee && CR.first == 0) { |
Chris Lattner | b374b90 | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 311 | Callee->DropRef(); |
| 312 | *I = CalledFunctions.back(); |
| 313 | CalledFunctions.pop_back(); |
Duncan Sands | a2582da | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 314 | return; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
Chris Lattner | a51c39c | 2009-09-15 05:40:35 +0000 | [diff] [blame] | 319 | /// replaceCallEdge - This method replaces the edge in the node for the |
| 320 | /// specified call site with a new one. Note that this method takes linear |
| 321 | /// time, so it should be used sparingly. |
| 322 | void CallGraphNode::replaceCallEdge(CallSite CS, |
| 323 | CallSite NewCS, CallGraphNode *NewNode){ |
| 324 | for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { |
| 325 | assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); |
| 326 | if (I->first == CS.getInstruction()) { |
| 327 | I->second->DropRef(); |
| 328 | I->first = NewCS.getInstruction(); |
| 329 | I->second = NewNode; |
| 330 | NewNode->AddRef(); |
| 331 | return; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
Reid Spencer | 4f1bd9e | 2006-06-07 22:00:26 +0000 | [diff] [blame] | 336 | // Enuse that users of CallGraph.h also link with this file |
| 337 | DEFINING_FILE_FOR(CallGraph) |