Chris Lattner | d852cc3 | 2002-03-06 18:00:49 +0000 | [diff] [blame] | 1 | //===- CallGraph.cpp - Build a Module's call graph ------------------------===// |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 9 | |
| 10 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 11 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 12 | #include "llvm/IR/Instructions.h" |
| 13 | #include "llvm/IR/IntrinsicInst.h" |
| 14 | #include "llvm/IR/Module.h" |
David Greene | ba44b3e | 2009-12-23 20:03:58 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Debug.h" |
Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 8b6db18 | 2004-04-12 05:36:32 +0000 | [diff] [blame] | 17 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 18 | |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | // Implementations of the CallGraph class methods. |
| 21 | // |
| 22 | |
| 23 | CallGraph::CallGraph(Module &M) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 24 | : M(M), Root(nullptr), ExternalCallingNode(getOrInsertFunction(nullptr)), |
| 25 | CallsExternalNode(new CallGraphNode(nullptr)) { |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 26 | // Add every function to the call graph. |
Yaron Keren | 26ceb08 | 2015-06-12 05:15:27 +0000 | [diff] [blame^] | 27 | for (Function &F : M) |
| 28 | addToCallGraph(&F); |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 29 | |
| 30 | // If we didn't find a main function, use the external call graph node |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 31 | if (!Root) |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 32 | Root = ExternalCallingNode; |
| 33 | } |
| 34 | |
| 35 | CallGraph::~CallGraph() { |
| 36 | // CallsExternalNode is not in the function map, delete it explicitly. |
| 37 | CallsExternalNode->allReferencesDropped(); |
| 38 | delete CallsExternalNode; |
| 39 | |
| 40 | // Reset all node's use counts to zero before deleting them to prevent an |
| 41 | // assertion from firing. |
| 42 | #ifndef NDEBUG |
Yaron Keren | 26ceb08 | 2015-06-12 05:15:27 +0000 | [diff] [blame^] | 43 | for (auto &I : FunctionMap) |
| 44 | I.second->allReferencesDropped(); |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 45 | #endif |
Yaron Keren | 26ceb08 | 2015-06-12 05:15:27 +0000 | [diff] [blame^] | 46 | for (auto &I : FunctionMap) |
| 47 | delete I.second; |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Rafael Espindola | 6554e5a | 2013-10-31 03:03:55 +0000 | [diff] [blame] | 50 | void CallGraph::addToCallGraph(Function *F) { |
| 51 | CallGraphNode *Node = getOrInsertFunction(F); |
| 52 | |
| 53 | // If this function has external linkage, anything could call it. |
| 54 | if (!F->hasLocalLinkage()) { |
| 55 | ExternalCallingNode->addCalledFunction(CallSite(), Node); |
| 56 | |
| 57 | // Found the entry point? |
| 58 | if (F->getName() == "main") { |
| 59 | if (Root) // Found multiple external mains? Don't pick one. |
| 60 | Root = ExternalCallingNode; |
| 61 | else |
| 62 | Root = Node; // Found a main, keep track of it! |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // If this function has its address taken, anything could call it. |
| 67 | if (F->hasAddressTaken()) |
| 68 | ExternalCallingNode->addCalledFunction(CallSite(), Node); |
| 69 | |
| 70 | // If this function is not defined in this translation unit, it could call |
| 71 | // anything. |
| 72 | if (F->isDeclaration() && !F->isIntrinsic()) |
| 73 | Node->addCalledFunction(CallSite(), CallsExternalNode); |
| 74 | |
| 75 | // Look for calls by this function. |
| 76 | for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB) |
| 77 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; |
| 78 | ++II) { |
| 79 | CallSite CS(cast<Value>(II)); |
| 80 | if (CS) { |
| 81 | const Function *Callee = CS.getCalledFunction(); |
| 82 | if (!Callee) |
| 83 | // Indirect calls of intrinsics are not allowed so no need to check. |
| 84 | Node->addCalledFunction(CS, CallsExternalNode); |
| 85 | else if (!Callee->isIntrinsic()) |
| 86 | Node->addCalledFunction(CS, getOrInsertFunction(Callee)); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 91 | void CallGraph::print(raw_ostream &OS) const { |
Rafael Espindola | 6554e5a | 2013-10-31 03:03:55 +0000 | [diff] [blame] | 92 | OS << "CallGraph Root is: "; |
| 93 | if (Function *F = Root->getFunction()) |
| 94 | OS << F->getName() << "\n"; |
| 95 | else { |
| 96 | OS << "<<null function: 0x" << Root << ">>\n"; |
| 97 | } |
| 98 | |
Chris Lattner | b9d5547 | 2002-11-04 00:21:19 +0000 | [diff] [blame] | 99 | for (CallGraph::const_iterator I = begin(), E = end(); I != E; ++I) |
Chris Lattner | 6b541102 | 2004-08-08 03:27:49 +0000 | [diff] [blame] | 100 | I->second->print(OS); |
| 101 | } |
| 102 | |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 103 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 104 | void CallGraph::dump() const { print(dbgs()); } |
| 105 | #endif |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 107 | // removeFunctionFromModule - Unlink the function from this module, returning |
| 108 | // it. Because this removes the function from the module, the call graph node |
| 109 | // is destroyed. This is only valid if the function does not call any other |
| 110 | // 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] | 111 | // is to dropAllReferences before calling this. |
| 112 | // |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 113 | Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) { |
Chris Lattner | 4275cf2 | 2009-08-31 02:24:20 +0000 | [diff] [blame] | 114 | assert(CGN->empty() && "Cannot remove function from call " |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 115 | "graph if it references other functions!"); |
Chris Lattner | 6f5e18d | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 116 | Function *F = CGN->getFunction(); // Get the function for the call graph node |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 117 | delete CGN; // Delete the call graph node for this func |
Chris Lattner | 6f5e18d | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 118 | FunctionMap.erase(F); // Remove the call graph node from the map |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 119 | |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 120 | M.getFunctionList().remove(F); |
Chris Lattner | 6f5e18d | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 121 | return F; |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Nick Lewycky | 0f87ca7 | 2011-01-03 03:19:35 +0000 | [diff] [blame] | 124 | /// spliceFunction - Replace the function represented by this node by another. |
| 125 | /// This does not rescan the body of the function, so it is suitable when |
| 126 | /// splicing the body of the old function to the new while also updating all |
| 127 | /// callers from old to new. |
| 128 | /// |
| 129 | void CallGraph::spliceFunction(const Function *From, const Function *To) { |
| 130 | assert(FunctionMap.count(From) && "No CallGraphNode for function!"); |
| 131 | assert(!FunctionMap.count(To) && |
| 132 | "Pointing CallGraphNode at a function that already exists"); |
| 133 | FunctionMapTy::iterator I = FunctionMap.find(From); |
| 134 | I->second->F = const_cast<Function*>(To); |
| 135 | FunctionMap[To] = I->second; |
| 136 | FunctionMap.erase(I); |
| 137 | } |
| 138 | |
Chris Lattner | 00ca8d2 | 2006-01-14 20:03:00 +0000 | [diff] [blame] | 139 | // getOrInsertFunction - This method is identical to calling operator[], but |
| 140 | // it will insert a new CallGraphNode for the specified function if one does |
| 141 | // not already exist. |
| 142 | CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) { |
| 143 | CallGraphNode *&CGN = FunctionMap[F]; |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 144 | if (CGN) |
| 145 | return CGN; |
| 146 | |
| 147 | assert((!F || F->getParent() == &M) && "Function not in current module!"); |
Chris Lattner | 00ca8d2 | 2006-01-14 20:03:00 +0000 | [diff] [blame] | 148 | return CGN = new CallGraphNode(const_cast<Function*>(F)); |
| 149 | } |
| 150 | |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 151 | //===----------------------------------------------------------------------===// |
| 152 | // Implementations of the CallGraphNode class methods. |
| 153 | // |
| 154 | |
Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 155 | void CallGraphNode::print(raw_ostream &OS) const { |
Chris Lattner | be198777 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 156 | if (Function *F = getFunction()) |
Chris Lattner | 081375b | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 157 | OS << "Call graph node for function: '" << F->getName() << "'"; |
Chris Lattner | be198777 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 158 | else |
Chris Lattner | 081375b | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 159 | OS << "Call graph node <<null function>>"; |
| 160 | |
Chris Lattner | 8c56254 | 2010-04-23 18:23:40 +0000 | [diff] [blame] | 161 | OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n'; |
Chris Lattner | be198777 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 8c56254 | 2010-04-23 18:23:40 +0000 | [diff] [blame] | 163 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
| 164 | OS << " CS<" << I->first << "> calls "; |
Gabor Greif | 191812f | 2009-01-14 17:09:04 +0000 | [diff] [blame] | 165 | if (Function *FI = I->second->getFunction()) |
Chris Lattner | 8c56254 | 2010-04-23 18:23:40 +0000 | [diff] [blame] | 166 | OS << "function '" << FI->getName() <<"'\n"; |
| 167 | else |
| 168 | OS << "external node\n"; |
| 169 | } |
| 170 | OS << '\n'; |
Chris Lattner | be198777 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Manman Ren | 49d684e | 2012-09-12 05:06:18 +0000 | [diff] [blame] | 173 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
David Greene | ba44b3e | 2009-12-23 20:03:58 +0000 | [diff] [blame] | 174 | void CallGraphNode::dump() const { print(dbgs()); } |
Manman Ren | c3366cc | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 175 | #endif |
Chris Lattner | be198777 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 176 | |
Chris Lattner | cc9709c | 2008-04-13 19:41:25 +0000 | [diff] [blame] | 177 | /// removeCallEdgeFor - This method removes the edge in the node for the |
| 178 | /// specified call site. Note that this method takes linear time, so it |
| 179 | /// should be used sparingly. |
| 180 | void CallGraphNode::removeCallEdgeFor(CallSite CS) { |
Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 181 | for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { |
| 182 | assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); |
Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 183 | if (I->first == CS.getInstruction()) { |
Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 184 | I->second->DropRef(); |
| 185 | *I = CalledFunctions.back(); |
| 186 | CalledFunctions.pop_back(); |
| 187 | return; |
| 188 | } |
Chris Lattner | cc9709c | 2008-04-13 19:41:25 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Chris Lattner | 824a218 | 2004-09-18 21:34:34 +0000 | [diff] [blame] | 192 | // removeAnyCallEdgeTo - This method removes any call edges from this node to |
| 193 | // the specified callee function. This takes more time to execute than |
| 194 | // removeCallEdgeTo, so it should not be used unless necessary. |
| 195 | void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) { |
Chris Lattner | d6d99df | 2004-09-19 19:01:06 +0000 | [diff] [blame] | 196 | for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i) |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 197 | if (CalledFunctions[i].second == Callee) { |
Chris Lattner | 081375b | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 198 | Callee->DropRef(); |
Chris Lattner | d6d99df | 2004-09-19 19:01:06 +0000 | [diff] [blame] | 199 | CalledFunctions[i] = CalledFunctions.back(); |
| 200 | CalledFunctions.pop_back(); |
| 201 | --i; --e; |
Chris Lattner | 824a218 | 2004-09-18 21:34:34 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
Reid Spencer | be53566 | 2006-06-07 22:00:26 +0000 | [diff] [blame] | 204 | |
Duncan Sands | 3a813a5 | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 205 | /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite |
| 206 | /// from this node to the specified callee function. |
| 207 | void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) { |
Gabor Greif | 20b722f | 2009-01-17 19:46:01 +0000 | [diff] [blame] | 208 | for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { |
| 209 | assert(I != CalledFunctions.end() && "Cannot find callee to remove!"); |
| 210 | CallRecord &CR = *I; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 211 | if (CR.second == Callee && CR.first == nullptr) { |
Chris Lattner | 081375b | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 212 | Callee->DropRef(); |
| 213 | *I = CalledFunctions.back(); |
| 214 | CalledFunctions.pop_back(); |
Duncan Sands | 3a813a5 | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
Chris Lattner | e098721 | 2009-09-15 05:40:35 +0000 | [diff] [blame] | 220 | /// replaceCallEdge - This method replaces the edge in the node for the |
| 221 | /// specified call site with a new one. Note that this method takes linear |
| 222 | /// time, so it should be used sparingly. |
| 223 | void CallGraphNode::replaceCallEdge(CallSite CS, |
| 224 | CallSite NewCS, CallGraphNode *NewNode){ |
| 225 | for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { |
| 226 | assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); |
| 227 | if (I->first == CS.getInstruction()) { |
| 228 | I->second->DropRef(); |
| 229 | I->first = NewCS.getInstruction(); |
| 230 | I->second = NewNode; |
| 231 | NewNode->AddRef(); |
| 232 | return; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 237 | //===----------------------------------------------------------------------===// |
Chandler Carruth | c4ddab6 | 2014-01-05 10:38:52 +0000 | [diff] [blame] | 238 | // Out-of-line definitions of CallGraphAnalysis class members. |
| 239 | // |
| 240 | |
| 241 | char CallGraphAnalysis::PassID; |
| 242 | |
| 243 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 244 | // Implementations of the CallGraphWrapperPass class methods. |
| 245 | // |
| 246 | |
| 247 | CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) { |
| 248 | initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 249 | } |
| 250 | |
| 251 | CallGraphWrapperPass::~CallGraphWrapperPass() {} |
| 252 | |
| 253 | void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 254 | AU.setPreservesAll(); |
| 255 | } |
| 256 | |
| 257 | bool CallGraphWrapperPass::runOnModule(Module &M) { |
| 258 | // All the real work is done in the constructor for the CallGraph. |
| 259 | G.reset(new CallGraph(M)); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction", |
| 264 | false, true) |
| 265 | |
| 266 | char CallGraphWrapperPass::ID = 0; |
| 267 | |
David Blaikie | b61064e | 2014-07-19 01:05:11 +0000 | [diff] [blame] | 268 | void CallGraphWrapperPass::releaseMemory() { G.reset(); } |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 269 | |
| 270 | void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const { |
| 271 | if (!G) { |
| 272 | OS << "No call graph has been built!\n"; |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | // Just delegate. |
| 277 | G->print(OS); |
| 278 | } |
| 279 | |
| 280 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 281 | void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); } |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 282 | #endif |