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)), |
David Blaikie | a5d7de9 | 2015-08-05 20:55:50 +0000 | [diff] [blame] | 25 | CallsExternalNode(llvm::make_unique<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 | |
Chandler Carruth | 5efd530 | 2015-08-16 06:35:19 +0000 | [diff] [blame] | 35 | CallGraph::CallGraph(CallGraph &&Arg) |
| 36 | : M(Arg.M), FunctionMap(std::move(Arg.FunctionMap)), Root(Arg.Root), |
| 37 | ExternalCallingNode(Arg.ExternalCallingNode), |
| 38 | CallsExternalNode(std::move(Arg.CallsExternalNode)) { |
| 39 | Arg.FunctionMap.clear(); |
| 40 | Arg.Root = nullptr; |
| 41 | Arg.ExternalCallingNode = nullptr; |
| 42 | } |
| 43 | |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 44 | CallGraph::~CallGraph() { |
| 45 | // CallsExternalNode is not in the function map, delete it explicitly. |
David Blaikie | a5d7de9 | 2015-08-05 20:55:50 +0000 | [diff] [blame] | 46 | if (CallsExternalNode) |
| 47 | CallsExternalNode->allReferencesDropped(); |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 48 | |
| 49 | // Reset all node's use counts to zero before deleting them to prevent an |
| 50 | // assertion from firing. |
| 51 | #ifndef NDEBUG |
Yaron Keren | 26ceb08 | 2015-06-12 05:15:27 +0000 | [diff] [blame] | 52 | for (auto &I : FunctionMap) |
| 53 | I.second->allReferencesDropped(); |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 54 | #endif |
Chris Lattner | bbf3ae8 | 2001-09-28 00:08:15 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Rafael Espindola | 6554e5a | 2013-10-31 03:03:55 +0000 | [diff] [blame] | 57 | void CallGraph::addToCallGraph(Function *F) { |
| 58 | CallGraphNode *Node = getOrInsertFunction(F); |
| 59 | |
| 60 | // If this function has external linkage, anything could call it. |
| 61 | if (!F->hasLocalLinkage()) { |
| 62 | ExternalCallingNode->addCalledFunction(CallSite(), Node); |
| 63 | |
| 64 | // Found the entry point? |
| 65 | if (F->getName() == "main") { |
| 66 | if (Root) // Found multiple external mains? Don't pick one. |
| 67 | Root = ExternalCallingNode; |
| 68 | else |
| 69 | Root = Node; // Found a main, keep track of it! |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // If this function has its address taken, anything could call it. |
| 74 | if (F->hasAddressTaken()) |
| 75 | ExternalCallingNode->addCalledFunction(CallSite(), Node); |
| 76 | |
| 77 | // If this function is not defined in this translation unit, it could call |
| 78 | // anything. |
| 79 | if (F->isDeclaration() && !F->isIntrinsic()) |
David Blaikie | a5d7de9 | 2015-08-05 20:55:50 +0000 | [diff] [blame] | 80 | Node->addCalledFunction(CallSite(), CallsExternalNode.get()); |
Rafael Espindola | 6554e5a | 2013-10-31 03:03:55 +0000 | [diff] [blame] | 81 | |
| 82 | // Look for calls by this function. |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 83 | for (BasicBlock &BB : *F) |
| 84 | for (Instruction &I : BB) { |
| 85 | if (auto CS = CallSite(&I)) { |
Rafael Espindola | 6554e5a | 2013-10-31 03:03:55 +0000 | [diff] [blame] | 86 | const Function *Callee = CS.getCalledFunction(); |
Sanjoy Das | c65d43e | 2015-06-18 19:28:26 +0000 | [diff] [blame] | 87 | if (!Callee || !Intrinsic::isLeaf(Callee->getIntrinsicID())) |
Rafael Espindola | 6554e5a | 2013-10-31 03:03:55 +0000 | [diff] [blame] | 88 | // Indirect calls of intrinsics are not allowed so no need to check. |
Sanjoy Das | c65d43e | 2015-06-18 19:28:26 +0000 | [diff] [blame] | 89 | // We can be more precise here by using TargetArg returned by |
| 90 | // Intrinsic::isLeaf. |
David Blaikie | a5d7de9 | 2015-08-05 20:55:50 +0000 | [diff] [blame] | 91 | Node->addCalledFunction(CS, CallsExternalNode.get()); |
Rafael Espindola | 6554e5a | 2013-10-31 03:03:55 +0000 | [diff] [blame] | 92 | else if (!Callee->isIntrinsic()) |
| 93 | Node->addCalledFunction(CS, getOrInsertFunction(Callee)); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 98 | void CallGraph::print(raw_ostream &OS) const { |
Rafael Espindola | 6554e5a | 2013-10-31 03:03:55 +0000 | [diff] [blame] | 99 | OS << "CallGraph Root is: "; |
| 100 | if (Function *F = Root->getFunction()) |
| 101 | OS << F->getName() << "\n"; |
| 102 | else { |
| 103 | OS << "<<null function: 0x" << Root << ">>\n"; |
| 104 | } |
| 105 | |
Sanjoy Das | 18c9dd3 | 2015-06-19 23:20:31 +0000 | [diff] [blame] | 106 | // Print in a deterministic order by sorting CallGraphNodes by name. We do |
| 107 | // this here to avoid slowing down the non-printing fast path. |
| 108 | |
| 109 | SmallVector<CallGraphNode *, 16> Nodes; |
| 110 | Nodes.reserve(FunctionMap.size()); |
| 111 | |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 112 | for (const auto &I : *this) |
| 113 | Nodes.push_back(I.second.get()); |
Sanjoy Das | 18c9dd3 | 2015-06-19 23:20:31 +0000 | [diff] [blame] | 114 | |
| 115 | std::sort(Nodes.begin(), Nodes.end(), |
| 116 | [](CallGraphNode *LHS, CallGraphNode *RHS) { |
| 117 | if (Function *LF = LHS->getFunction()) |
| 118 | if (Function *RF = RHS->getFunction()) |
| 119 | return LF->getName() < RF->getName(); |
| 120 | |
| 121 | return RHS->getFunction() != nullptr; |
| 122 | }); |
| 123 | |
| 124 | for (CallGraphNode *CN : Nodes) |
| 125 | CN->print(OS); |
Chris Lattner | 6b541102 | 2004-08-08 03:27:49 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Davide Italiano | 6f93df8 | 2015-11-23 02:58:42 +0000 | [diff] [blame] | 128 | LLVM_DUMP_METHOD |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 129 | void CallGraph::dump() const { print(dbgs()); } |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 131 | // removeFunctionFromModule - Unlink the function from this module, returning |
| 132 | // it. Because this removes the function from the module, the call graph node |
| 133 | // is destroyed. This is only valid if the function does not call any other |
| 134 | // 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] | 135 | // is to dropAllReferences before calling this. |
| 136 | // |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 137 | Function *CallGraph::removeFunctionFromModule(CallGraphNode *CGN) { |
Chris Lattner | 4275cf2 | 2009-08-31 02:24:20 +0000 | [diff] [blame] | 138 | assert(CGN->empty() && "Cannot remove function from call " |
Chris Lattner | 79b0c7d | 2002-07-18 04:43:16 +0000 | [diff] [blame] | 139 | "graph if it references other functions!"); |
Chris Lattner | 6f5e18d | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 140 | Function *F = CGN->getFunction(); // Get the function for the call graph node |
Chris Lattner | 6f5e18d | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 141 | FunctionMap.erase(F); // Remove the call graph node from the map |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 142 | |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 143 | M.getFunctionList().remove(F); |
Chris Lattner | 6f5e18d | 2003-08-31 20:36:52 +0000 | [diff] [blame] | 144 | return F; |
Chris Lattner | 03946cd | 2001-11-26 18:51:25 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Nick Lewycky | 0f87ca7 | 2011-01-03 03:19:35 +0000 | [diff] [blame] | 147 | /// spliceFunction - Replace the function represented by this node by another. |
| 148 | /// This does not rescan the body of the function, so it is suitable when |
| 149 | /// splicing the body of the old function to the new while also updating all |
| 150 | /// callers from old to new. |
| 151 | /// |
| 152 | void CallGraph::spliceFunction(const Function *From, const Function *To) { |
| 153 | assert(FunctionMap.count(From) && "No CallGraphNode for function!"); |
| 154 | assert(!FunctionMap.count(To) && |
| 155 | "Pointing CallGraphNode at a function that already exists"); |
| 156 | FunctionMapTy::iterator I = FunctionMap.find(From); |
| 157 | I->second->F = const_cast<Function*>(To); |
David Blaikie | a5d7de9 | 2015-08-05 20:55:50 +0000 | [diff] [blame] | 158 | FunctionMap[To] = std::move(I->second); |
Nick Lewycky | 0f87ca7 | 2011-01-03 03:19:35 +0000 | [diff] [blame] | 159 | FunctionMap.erase(I); |
| 160 | } |
| 161 | |
Chris Lattner | 00ca8d2 | 2006-01-14 20:03:00 +0000 | [diff] [blame] | 162 | // getOrInsertFunction - This method is identical to calling operator[], but |
| 163 | // it will insert a new CallGraphNode for the specified function if one does |
| 164 | // not already exist. |
| 165 | CallGraphNode *CallGraph::getOrInsertFunction(const Function *F) { |
David Blaikie | a5d7de9 | 2015-08-05 20:55:50 +0000 | [diff] [blame] | 166 | auto &CGN = FunctionMap[F]; |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 167 | if (CGN) |
David Blaikie | a5d7de9 | 2015-08-05 20:55:50 +0000 | [diff] [blame] | 168 | return CGN.get(); |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 169 | |
| 170 | assert((!F || F->getParent() == &M) && "Function not in current module!"); |
David Blaikie | a5d7de9 | 2015-08-05 20:55:50 +0000 | [diff] [blame] | 171 | CGN = llvm::make_unique<CallGraphNode>(const_cast<Function *>(F)); |
| 172 | return CGN.get(); |
Chris Lattner | 00ca8d2 | 2006-01-14 20:03:00 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 175 | //===----------------------------------------------------------------------===// |
| 176 | // Implementations of the CallGraphNode class methods. |
| 177 | // |
| 178 | |
Chris Lattner | 1362602 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 179 | void CallGraphNode::print(raw_ostream &OS) const { |
Chris Lattner | be198777 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 180 | if (Function *F = getFunction()) |
Chris Lattner | 081375b | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 181 | OS << "Call graph node for function: '" << F->getName() << "'"; |
Chris Lattner | be198777 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 182 | else |
Chris Lattner | 081375b | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 183 | OS << "Call graph node <<null function>>"; |
| 184 | |
Chris Lattner | 8c56254 | 2010-04-23 18:23:40 +0000 | [diff] [blame] | 185 | OS << "<<" << this << ">> #uses=" << getNumReferences() << '\n'; |
Chris Lattner | be198777 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 186 | |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 187 | for (const auto &I : *this) { |
| 188 | OS << " CS<" << I.first << "> calls "; |
| 189 | if (Function *FI = I.second->getFunction()) |
Chris Lattner | 8c56254 | 2010-04-23 18:23:40 +0000 | [diff] [blame] | 190 | OS << "function '" << FI->getName() <<"'\n"; |
| 191 | else |
| 192 | OS << "external node\n"; |
| 193 | } |
| 194 | OS << '\n'; |
Chris Lattner | be198777 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Davide Italiano | 6f93df8 | 2015-11-23 02:58:42 +0000 | [diff] [blame] | 197 | LLVM_DUMP_METHOD |
David Greene | ba44b3e | 2009-12-23 20:03:58 +0000 | [diff] [blame] | 198 | void CallGraphNode::dump() const { print(dbgs()); } |
Chris Lattner | be198777 | 2005-12-22 06:07:52 +0000 | [diff] [blame] | 199 | |
Chris Lattner | cc9709c | 2008-04-13 19:41:25 +0000 | [diff] [blame] | 200 | /// removeCallEdgeFor - This method removes the edge in the node for the |
| 201 | /// specified call site. Note that this method takes linear time, so it |
| 202 | /// should be used sparingly. |
| 203 | void CallGraphNode::removeCallEdgeFor(CallSite CS) { |
Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 204 | for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { |
| 205 | assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); |
Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 206 | if (I->first == CS.getInstruction()) { |
Chris Lattner | eedcd84 | 2009-08-31 07:23:46 +0000 | [diff] [blame] | 207 | I->second->DropRef(); |
| 208 | *I = CalledFunctions.back(); |
| 209 | CalledFunctions.pop_back(); |
| 210 | return; |
| 211 | } |
Chris Lattner | cc9709c | 2008-04-13 19:41:25 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Chris Lattner | 824a218 | 2004-09-18 21:34:34 +0000 | [diff] [blame] | 215 | // removeAnyCallEdgeTo - This method removes any call edges from this node to |
| 216 | // the specified callee function. This takes more time to execute than |
| 217 | // removeCallEdgeTo, so it should not be used unless necessary. |
| 218 | void CallGraphNode::removeAnyCallEdgeTo(CallGraphNode *Callee) { |
Chris Lattner | d6d99df | 2004-09-19 19:01:06 +0000 | [diff] [blame] | 219 | for (unsigned i = 0, e = CalledFunctions.size(); i != e; ++i) |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 220 | if (CalledFunctions[i].second == Callee) { |
Chris Lattner | 081375b | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 221 | Callee->DropRef(); |
Chris Lattner | d6d99df | 2004-09-19 19:01:06 +0000 | [diff] [blame] | 222 | CalledFunctions[i] = CalledFunctions.back(); |
| 223 | CalledFunctions.pop_back(); |
| 224 | --i; --e; |
Chris Lattner | 824a218 | 2004-09-18 21:34:34 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
Reid Spencer | be53566 | 2006-06-07 22:00:26 +0000 | [diff] [blame] | 227 | |
Duncan Sands | 3a813a5 | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 228 | /// removeOneAbstractEdgeTo - Remove one edge associated with a null callsite |
| 229 | /// from this node to the specified callee function. |
| 230 | void CallGraphNode::removeOneAbstractEdgeTo(CallGraphNode *Callee) { |
Gabor Greif | 20b722f | 2009-01-17 19:46:01 +0000 | [diff] [blame] | 231 | for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { |
| 232 | assert(I != CalledFunctions.end() && "Cannot find callee to remove!"); |
| 233 | CallRecord &CR = *I; |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 234 | if (CR.second == Callee && CR.first == nullptr) { |
Chris Lattner | 081375b | 2009-08-31 03:15:49 +0000 | [diff] [blame] | 235 | Callee->DropRef(); |
| 236 | *I = CalledFunctions.back(); |
| 237 | CalledFunctions.pop_back(); |
Duncan Sands | 3a813a5 | 2008-10-03 07:36:09 +0000 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
Chris Lattner | e098721 | 2009-09-15 05:40:35 +0000 | [diff] [blame] | 243 | /// replaceCallEdge - This method replaces the edge in the node for the |
| 244 | /// specified call site with a new one. Note that this method takes linear |
| 245 | /// time, so it should be used sparingly. |
| 246 | void CallGraphNode::replaceCallEdge(CallSite CS, |
| 247 | CallSite NewCS, CallGraphNode *NewNode){ |
| 248 | for (CalledFunctionsVector::iterator I = CalledFunctions.begin(); ; ++I) { |
| 249 | assert(I != CalledFunctions.end() && "Cannot find callsite to remove!"); |
| 250 | if (I->first == CS.getInstruction()) { |
| 251 | I->second->DropRef(); |
| 252 | I->first = NewCS.getInstruction(); |
| 253 | I->second = NewNode; |
| 254 | NewNode->AddRef(); |
| 255 | return; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Chandler Carruth | cf3f4f2 | 2016-03-10 14:33:10 +0000 | [diff] [blame] | 260 | // Provide an explicit template instantiation for the static ID. |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 261 | AnalysisKey CallGraphAnalysis::Key; |
Chandler Carruth | cf3f4f2 | 2016-03-10 14:33:10 +0000 | [diff] [blame] | 262 | |
Chandler Carruth | 4c660f7 | 2016-03-10 11:24:11 +0000 | [diff] [blame] | 263 | PreservedAnalyses CallGraphPrinterPass::run(Module &M, |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 264 | ModuleAnalysisManager &AM) { |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 265 | AM.getResult<CallGraphAnalysis>(M).print(OS); |
Chandler Carruth | 4c660f7 | 2016-03-10 11:24:11 +0000 | [diff] [blame] | 266 | return PreservedAnalyses::all(); |
| 267 | } |
| 268 | |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 269 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 5efd530 | 2015-08-16 06:35:19 +0000 | [diff] [blame] | 270 | // Out-of-line definitions of CallGraphAnalysis class members. |
| 271 | // |
| 272 | |
Chandler Carruth | 5efd530 | 2015-08-16 06:35:19 +0000 | [diff] [blame] | 273 | //===----------------------------------------------------------------------===// |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 274 | // Implementations of the CallGraphWrapperPass class methods. |
| 275 | // |
| 276 | |
| 277 | CallGraphWrapperPass::CallGraphWrapperPass() : ModulePass(ID) { |
| 278 | initializeCallGraphWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 279 | } |
| 280 | |
| 281 | CallGraphWrapperPass::~CallGraphWrapperPass() {} |
| 282 | |
| 283 | void CallGraphWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 284 | AU.setPreservesAll(); |
| 285 | } |
| 286 | |
| 287 | bool CallGraphWrapperPass::runOnModule(Module &M) { |
| 288 | // All the real work is done in the constructor for the CallGraph. |
| 289 | G.reset(new CallGraph(M)); |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | INITIALIZE_PASS(CallGraphWrapperPass, "basiccg", "CallGraph Construction", |
| 294 | false, true) |
| 295 | |
| 296 | char CallGraphWrapperPass::ID = 0; |
| 297 | |
David Blaikie | b61064e | 2014-07-19 01:05:11 +0000 | [diff] [blame] | 298 | void CallGraphWrapperPass::releaseMemory() { G.reset(); } |
Chandler Carruth | 6378cf5 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 299 | |
| 300 | void CallGraphWrapperPass::print(raw_ostream &OS, const Module *) const { |
| 301 | if (!G) { |
| 302 | OS << "No call graph has been built!\n"; |
| 303 | return; |
| 304 | } |
| 305 | |
| 306 | // Just delegate. |
| 307 | G->print(OS); |
| 308 | } |
| 309 | |
Davide Italiano | 6f93df8 | 2015-11-23 02:58:42 +0000 | [diff] [blame] | 310 | LLVM_DUMP_METHOD |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 311 | void CallGraphWrapperPass::dump() const { print(dbgs(), nullptr); } |
Chandler Carruth | 1ecd740 | 2016-03-10 11:08:44 +0000 | [diff] [blame] | 312 | |
| 313 | namespace { |
| 314 | struct CallGraphPrinterLegacyPass : public ModulePass { |
| 315 | static char ID; // Pass ID, replacement for typeid |
| 316 | CallGraphPrinterLegacyPass() : ModulePass(ID) { |
| 317 | initializeCallGraphPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 318 | } |
| 319 | |
| 320 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 321 | AU.setPreservesAll(); |
| 322 | AU.addRequiredTransitive<CallGraphWrapperPass>(); |
| 323 | } |
| 324 | bool runOnModule(Module &M) override { |
| 325 | getAnalysis<CallGraphWrapperPass>().print(errs(), &M); |
| 326 | return false; |
| 327 | } |
| 328 | }; |
| 329 | } |
| 330 | |
| 331 | char CallGraphPrinterLegacyPass::ID = 0; |
| 332 | |
| 333 | INITIALIZE_PASS_BEGIN(CallGraphPrinterLegacyPass, "print-callgraph", |
| 334 | "Print a call graph", true, true) |
| 335 | INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass) |
| 336 | INITIALIZE_PASS_END(CallGraphPrinterLegacyPass, "print-callgraph", |
| 337 | "Print a call graph", true, true) |