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