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