Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 1 | //===- LazyCallGraph.cpp - Analysis of a Module's call graph --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Analysis/LazyCallGraph.h" |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 12 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 13 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Instructions.h" |
| 15 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Debug.h" |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame^] | 21 | #define DEBUG_TYPE "lcg" |
| 22 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 23 | static void findCallees( |
| 24 | SmallVectorImpl<Constant *> &Worklist, SmallPtrSetImpl<Constant *> &Visited, |
Chandler Carruth | e9b5061 | 2014-03-10 02:14:14 +0000 | [diff] [blame] | 25 | SmallVectorImpl<PointerUnion<Function *, LazyCallGraph::Node *>> &Callees, |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 26 | SmallPtrSetImpl<Function *> &CalleeSet) { |
| 27 | while (!Worklist.empty()) { |
| 28 | Constant *C = Worklist.pop_back_val(); |
| 29 | |
| 30 | if (Function *F = dyn_cast<Function>(C)) { |
| 31 | // Note that we consider *any* function with a definition to be a viable |
| 32 | // edge. Even if the function's definition is subject to replacement by |
| 33 | // some other module (say, a weak definition) there may still be |
| 34 | // optimizations which essentially speculate based on the definition and |
| 35 | // a way to check that the specific definition is in fact the one being |
| 36 | // used. For example, this could be done by moving the weak definition to |
| 37 | // a strong (internal) definition and making the weak definition be an |
| 38 | // alias. Then a test of the address of the weak function against the new |
| 39 | // strong definition's address would be an effective way to determine the |
| 40 | // safety of optimizing a direct call edge. |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 41 | if (!F->isDeclaration() && CalleeSet.insert(F)) { |
| 42 | DEBUG(dbgs() << " Added callable function: " << F->getName() |
| 43 | << "\n"); |
Chandler Carruth | e9b5061 | 2014-03-10 02:14:14 +0000 | [diff] [blame] | 44 | Callees.push_back(F); |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 45 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 46 | continue; |
| 47 | } |
| 48 | |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 49 | for (Value *Op : C->operand_values()) |
| 50 | if (Visited.insert(cast<Constant>(Op))) |
| 51 | Worklist.push_back(cast<Constant>(Op)); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 55 | LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F) |
| 56 | : G(&G), F(F), DFSNumber(0), LowLink(0) { |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 57 | DEBUG(dbgs() << " Adding functions called by '" << F.getName() |
| 58 | << "' to the graph.\n"); |
| 59 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 60 | SmallVector<Constant *, 16> Worklist; |
| 61 | SmallPtrSet<Constant *, 16> Visited; |
| 62 | // Find all the potential callees in this function. First walk the |
| 63 | // instructions and add every operand which is a constant to the worklist. |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 64 | for (BasicBlock &BB : F) |
| 65 | for (Instruction &I : BB) |
| 66 | for (Value *Op : I.operand_values()) |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 67 | if (Constant *C = dyn_cast<Constant>(Op)) |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 68 | if (Visited.insert(C)) |
| 69 | Worklist.push_back(C); |
| 70 | |
| 71 | // We've collected all the constant (and thus potentially function or |
| 72 | // function containing) operands to all of the instructions in the function. |
| 73 | // Process them (recursively) collecting every function found. |
| 74 | findCallees(Worklist, Visited, Callees, CalleeSet); |
| 75 | } |
| 76 | |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 77 | LazyCallGraph::LazyCallGraph(Module &M) : NextDFSNumber(0) { |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 78 | DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier() |
| 79 | << "\n"); |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 80 | for (Function &F : M) |
| 81 | if (!F.isDeclaration() && !F.hasLocalLinkage()) |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 82 | if (EntryNodeSet.insert(&F)) { |
| 83 | DEBUG(dbgs() << " Adding '" << F.getName() |
| 84 | << "' to entry set of the graph.\n"); |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 85 | EntryNodes.push_back(&F); |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 86 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 87 | |
| 88 | // Now add entry nodes for functions reachable via initializers to globals. |
| 89 | SmallVector<Constant *, 16> Worklist; |
| 90 | SmallPtrSet<Constant *, 16> Visited; |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 91 | for (GlobalVariable &GV : M.globals()) |
| 92 | if (GV.hasInitializer()) |
| 93 | if (Visited.insert(GV.getInitializer())) |
| 94 | Worklist.push_back(GV.getInitializer()); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 95 | |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 96 | DEBUG(dbgs() << " Adding functions referenced by global initializers to the " |
| 97 | "entry set.\n"); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 98 | findCallees(Worklist, Visited, EntryNodes, EntryNodeSet); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 99 | |
| 100 | for (auto &Entry : EntryNodes) |
| 101 | if (Function *F = Entry.dyn_cast<Function *>()) |
| 102 | SCCEntryNodes.insert(F); |
| 103 | else |
| 104 | SCCEntryNodes.insert(&Entry.get<Node *>()->getFunction()); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 107 | LazyCallGraph::LazyCallGraph(LazyCallGraph &&G) |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 108 | : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)), |
| 109 | EntryNodes(std::move(G.EntryNodes)), |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 110 | EntryNodeSet(std::move(G.EntryNodeSet)), SCCBPA(std::move(G.SCCBPA)), |
| 111 | SCCMap(std::move(G.SCCMap)), LeafSCCs(std::move(G.LeafSCCs)), |
| 112 | DFSStack(std::move(G.DFSStack)), |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 113 | SCCEntryNodes(std::move(G.SCCEntryNodes)), |
| 114 | NextDFSNumber(G.NextDFSNumber) { |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 115 | updateGraphPtrs(); |
| 116 | } |
| 117 | |
| 118 | LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) { |
| 119 | BPA = std::move(G.BPA); |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 120 | NodeMap = std::move(G.NodeMap); |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 121 | EntryNodes = std::move(G.EntryNodes); |
| 122 | EntryNodeSet = std::move(G.EntryNodeSet); |
| 123 | SCCBPA = std::move(G.SCCBPA); |
| 124 | SCCMap = std::move(G.SCCMap); |
| 125 | LeafSCCs = std::move(G.LeafSCCs); |
| 126 | DFSStack = std::move(G.DFSStack); |
| 127 | SCCEntryNodes = std::move(G.SCCEntryNodes); |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 128 | NextDFSNumber = G.NextDFSNumber; |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 129 | updateGraphPtrs(); |
| 130 | return *this; |
| 131 | } |
| 132 | |
| 133 | LazyCallGraph::Node *LazyCallGraph::insertInto(Function &F, Node *&MappedN) { |
| 134 | return new (MappedN = BPA.Allocate()) Node(*this, F); |
| 135 | } |
| 136 | |
| 137 | void LazyCallGraph::updateGraphPtrs() { |
Chandler Carruth | b60cb31 | 2014-04-17 07:25:59 +0000 | [diff] [blame] | 138 | // Process all nodes updating the graph pointers. |
| 139 | SmallVector<Node *, 16> Worklist; |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 140 | for (auto &Entry : EntryNodes) |
| 141 | if (Node *EntryN = Entry.dyn_cast<Node *>()) |
Chandler Carruth | b60cb31 | 2014-04-17 07:25:59 +0000 | [diff] [blame] | 142 | Worklist.push_back(EntryN); |
| 143 | |
| 144 | while (!Worklist.empty()) { |
| 145 | Node *N = Worklist.pop_back_val(); |
| 146 | N->G = this; |
| 147 | for (auto &Callee : N->Callees) |
| 148 | if (Node *CalleeN = Callee.dyn_cast<Node *>()) |
| 149 | Worklist.push_back(CalleeN); |
| 150 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 151 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 152 | |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 153 | LazyCallGraph::SCC *LazyCallGraph::getNextSCCInPostOrder() { |
| 154 | // When the stack is empty, there are no more SCCs to walk in this graph. |
| 155 | if (DFSStack.empty()) { |
| 156 | // If we've handled all candidate entry nodes to the SCC forest, we're done. |
| 157 | if (SCCEntryNodes.empty()) |
| 158 | return nullptr; |
| 159 | |
| 160 | Node *N = get(*SCCEntryNodes.pop_back_val()); |
| 161 | DFSStack.push_back(std::make_pair(N, N->begin())); |
| 162 | } |
| 163 | |
| 164 | Node *N = DFSStack.back().first; |
| 165 | if (N->DFSNumber == 0) { |
| 166 | // This node hasn't been visited before, assign it a DFS number and remove |
| 167 | // it from the entry set. |
| 168 | N->LowLink = N->DFSNumber = NextDFSNumber++; |
| 169 | SCCEntryNodes.remove(&N->getFunction()); |
| 170 | } |
| 171 | |
| 172 | for (auto I = DFSStack.back().second, E = N->end(); I != E; ++I) { |
| 173 | Node *ChildN = *I; |
| 174 | if (ChildN->DFSNumber == 0) { |
| 175 | // Mark that we should start at this child when next this node is the |
| 176 | // top of the stack. We don't start at the next child to ensure this |
| 177 | // child's lowlink is reflected. |
| 178 | // FIXME: I don't actually think this is required, and we could start |
| 179 | // at the next child. |
| 180 | DFSStack.back().second = I; |
| 181 | |
| 182 | // Recurse onto this node via a tail call. |
| 183 | DFSStack.push_back(std::make_pair(ChildN, ChildN->begin())); |
| 184 | return LazyCallGraph::getNextSCCInPostOrder(); |
| 185 | } |
| 186 | |
| 187 | // Track the lowest link of the childen, if any are still in the stack. |
| 188 | if (ChildN->LowLink < N->LowLink && !SCCMap.count(&ChildN->getFunction())) |
| 189 | N->LowLink = ChildN->LowLink; |
| 190 | } |
| 191 | |
| 192 | // The tail of the stack is the new SCC. Allocate the SCC and pop the stack |
| 193 | // into it. |
| 194 | SCC *NewSCC = new (SCCBPA.Allocate()) SCC(); |
| 195 | |
| 196 | // Because we don't follow the strict Tarjan recursive formulation, walk |
| 197 | // from the top of the stack down, propagating the lowest link and stopping |
| 198 | // when the DFS number is the lowest link. |
| 199 | int LowestLink = N->LowLink; |
| 200 | do { |
| 201 | Node *SCCN = DFSStack.pop_back_val().first; |
| 202 | SCCMap.insert(std::make_pair(&SCCN->getFunction(), NewSCC)); |
| 203 | NewSCC->Nodes.push_back(SCCN); |
| 204 | LowestLink = std::min(LowestLink, SCCN->LowLink); |
| 205 | bool Inserted = |
| 206 | NewSCC->NodeSet.insert(&SCCN->getFunction()); |
| 207 | (void)Inserted; |
| 208 | assert(Inserted && "Cannot have duplicates in the DFSStack!"); |
| 209 | } while (!DFSStack.empty() && LowestLink <= DFSStack.back().first->DFSNumber); |
| 210 | assert(LowestLink == NewSCC->Nodes.back()->DFSNumber && |
| 211 | "Cannot stop with a DFS number greater than the lowest link!"); |
| 212 | |
| 213 | // A final pass over all edges in the SCC (this remains linear as we only |
| 214 | // do this once when we build the SCC) to connect it to the parent sets of |
| 215 | // its children. |
| 216 | bool IsLeafSCC = true; |
| 217 | for (Node *SCCN : NewSCC->Nodes) |
| 218 | for (Node *SCCChildN : *SCCN) { |
| 219 | if (NewSCC->NodeSet.count(&SCCChildN->getFunction())) |
| 220 | continue; |
| 221 | SCC *ChildSCC = SCCMap.lookup(&SCCChildN->getFunction()); |
| 222 | assert(ChildSCC && |
| 223 | "Must have all child SCCs processed when building a new SCC!"); |
| 224 | ChildSCC->ParentSCCs.insert(NewSCC); |
| 225 | IsLeafSCC = false; |
| 226 | } |
| 227 | |
| 228 | // For the SCCs where we fine no child SCCs, add them to the leaf list. |
| 229 | if (IsLeafSCC) |
| 230 | LeafSCCs.push_back(NewSCC); |
| 231 | |
| 232 | return NewSCC; |
| 233 | } |
| 234 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 235 | char LazyCallGraphAnalysis::PassID; |
| 236 | |
| 237 | LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {} |
| 238 | |
| 239 | static void printNodes(raw_ostream &OS, LazyCallGraph::Node &N, |
| 240 | SmallPtrSetImpl<LazyCallGraph::Node *> &Printed) { |
| 241 | // Recurse depth first through the nodes. |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 242 | for (LazyCallGraph::Node *ChildN : N) |
| 243 | if (Printed.insert(ChildN)) |
| 244 | printNodes(OS, *ChildN, Printed); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 245 | |
| 246 | OS << " Call edges in function: " << N.getFunction().getName() << "\n"; |
| 247 | for (LazyCallGraph::iterator I = N.begin(), E = N.end(); I != E; ++I) |
| 248 | OS << " -> " << I->getFunction().getName() << "\n"; |
| 249 | |
| 250 | OS << "\n"; |
| 251 | } |
| 252 | |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 253 | static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &SCC) { |
| 254 | ptrdiff_t SCCSize = std::distance(SCC.begin(), SCC.end()); |
| 255 | OS << " SCC with " << SCCSize << " functions:\n"; |
| 256 | |
| 257 | for (LazyCallGraph::Node *N : SCC) |
| 258 | OS << " " << N->getFunction().getName() << "\n"; |
| 259 | |
| 260 | OS << "\n"; |
| 261 | } |
| 262 | |
Chandler Carruth | e9b5061 | 2014-03-10 02:14:14 +0000 | [diff] [blame] | 263 | PreservedAnalyses LazyCallGraphPrinterPass::run(Module *M, |
| 264 | ModuleAnalysisManager *AM) { |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 265 | LazyCallGraph &G = AM->getResult<LazyCallGraphAnalysis>(M); |
| 266 | |
Chandler Carruth | e9b5061 | 2014-03-10 02:14:14 +0000 | [diff] [blame] | 267 | OS << "Printing the call graph for module: " << M->getModuleIdentifier() |
| 268 | << "\n\n"; |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 269 | |
| 270 | SmallPtrSet<LazyCallGraph::Node *, 16> Printed; |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 271 | for (LazyCallGraph::Node *N : G) |
| 272 | if (Printed.insert(N)) |
| 273 | printNodes(OS, *N, Printed); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 274 | |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 275 | for (LazyCallGraph::SCC *SCC : G.postorder_sccs()) |
| 276 | printSCC(OS, *SCC); |
| 277 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 278 | return PreservedAnalyses::all(); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 279 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 280 | } |