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 | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 23 | static void addEdge(SmallVectorImpl<LazyCallGraph::Edge> &Edges, |
| 24 | DenseMap<Function *, size_t> &EdgeIndexMap, Function &F, |
| 25 | LazyCallGraph::Edge::Kind EK) { |
| 26 | // Note that we consider *any* function with a definition to be a viable |
| 27 | // edge. Even if the function's definition is subject to replacement by |
| 28 | // some other module (say, a weak definition) there may still be |
| 29 | // optimizations which essentially speculate based on the definition and |
| 30 | // a way to check that the specific definition is in fact the one being |
| 31 | // used. For example, this could be done by moving the weak definition to |
| 32 | // a strong (internal) definition and making the weak definition be an |
| 33 | // alias. Then a test of the address of the weak function against the new |
| 34 | // strong definition's address would be an effective way to determine the |
| 35 | // safety of optimizing a direct call edge. |
| 36 | if (!F.isDeclaration() && |
| 37 | EdgeIndexMap.insert(std::make_pair(&F, Edges.size())).second) { |
| 38 | DEBUG(dbgs() << " Added callable function: " << F.getName() << "\n"); |
| 39 | Edges.emplace_back(LazyCallGraph::Edge(F, EK)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static void findReferences( |
| 44 | SmallVectorImpl<Constant *> &Worklist, |
| 45 | SmallPtrSetImpl<Constant *> &Visited, |
| 46 | SmallVectorImpl<LazyCallGraph::Edge> &Edges, |
| 47 | DenseMap<Function *, size_t> &EdgeIndexMap) { |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 48 | while (!Worklist.empty()) { |
| 49 | Constant *C = Worklist.pop_back_val(); |
| 50 | |
| 51 | if (Function *F = dyn_cast<Function>(C)) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 52 | addEdge(Edges, EdgeIndexMap, *F, LazyCallGraph::Edge::Ref); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 53 | continue; |
| 54 | } |
| 55 | |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 56 | for (Value *Op : C->operand_values()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 57 | if (Visited.insert(cast<Constant>(Op)).second) |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 58 | Worklist.push_back(cast<Constant>(Op)); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 62 | LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F) |
| 63 | : G(&G), F(F), DFSNumber(0), LowLink(0) { |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 64 | DEBUG(dbgs() << " Adding functions called by '" << F.getName() |
| 65 | << "' to the graph.\n"); |
| 66 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 67 | SmallVector<Constant *, 16> Worklist; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 68 | SmallPtrSet<Function *, 4> Callees; |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 69 | SmallPtrSet<Constant *, 16> Visited; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 70 | |
| 71 | // Find all the potential call graph edges in this function. We track both |
| 72 | // actual call edges and indirect references to functions. The direct calls |
| 73 | // are trivially added, but to accumulate the latter we walk the instructions |
| 74 | // and add every operand which is a constant to the worklist to process |
| 75 | // afterward. |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 76 | for (BasicBlock &BB : F) |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 77 | for (Instruction &I : BB) { |
| 78 | if (auto CS = CallSite(&I)) |
| 79 | if (Function *Callee = CS.getCalledFunction()) |
| 80 | if (Callees.insert(Callee).second) { |
| 81 | Visited.insert(Callee); |
| 82 | addEdge(Edges, EdgeIndexMap, *Callee, LazyCallGraph::Edge::Call); |
| 83 | } |
| 84 | |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 85 | for (Value *Op : I.operand_values()) |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 86 | if (Constant *C = dyn_cast<Constant>(Op)) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 87 | if (Visited.insert(C).second) |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 88 | Worklist.push_back(C); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 89 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 90 | |
| 91 | // We've collected all the constant (and thus potentially function or |
| 92 | // function containing) operands to all of the instructions in the function. |
| 93 | // Process them (recursively) collecting every function found. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 94 | findReferences(Worklist, Visited, Edges, EdgeIndexMap); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 97 | void LazyCallGraph::Node::insertEdgeInternal(Function &Child, Edge::Kind EK) { |
| 98 | if (Node *N = G->lookup(Child)) |
| 99 | return insertEdgeInternal(*N, EK); |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 100 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 101 | EdgeIndexMap.insert(std::make_pair(&Child, Edges.size())); |
| 102 | Edges.emplace_back(Child, EK); |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 105 | void LazyCallGraph::Node::insertEdgeInternal(Node &ChildN, Edge::Kind EK) { |
| 106 | EdgeIndexMap.insert(std::make_pair(&ChildN.getFunction(), Edges.size())); |
| 107 | Edges.emplace_back(ChildN, EK); |
Chandler Carruth | c00a7ff | 2014-04-28 11:10:23 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 110 | void LazyCallGraph::Node::removeEdgeInternal(Function &Child) { |
| 111 | auto IndexMapI = EdgeIndexMap.find(&Child); |
| 112 | assert(IndexMapI != EdgeIndexMap.end() && |
| 113 | "Child not in the edge set for this caller?"); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 114 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 115 | Edges[IndexMapI->second] = Edge(); |
| 116 | EdgeIndexMap.erase(IndexMapI); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 119 | LazyCallGraph::LazyCallGraph(Module &M) : NextDFSNumber(0) { |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 120 | DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier() |
| 121 | << "\n"); |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 122 | for (Function &F : M) |
| 123 | if (!F.isDeclaration() && !F.hasLocalLinkage()) |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 124 | if (EntryIndexMap.insert(std::make_pair(&F, EntryEdges.size())).second) { |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 125 | DEBUG(dbgs() << " Adding '" << F.getName() |
| 126 | << "' to entry set of the graph.\n"); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 127 | EntryEdges.emplace_back(F, Edge::Ref); |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 128 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 129 | |
| 130 | // Now add entry nodes for functions reachable via initializers to globals. |
| 131 | SmallVector<Constant *, 16> Worklist; |
| 132 | SmallPtrSet<Constant *, 16> Visited; |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 133 | for (GlobalVariable &GV : M.globals()) |
| 134 | if (GV.hasInitializer()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 135 | if (Visited.insert(GV.getInitializer()).second) |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 136 | Worklist.push_back(GV.getInitializer()); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 137 | |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 138 | DEBUG(dbgs() << " Adding functions referenced by global initializers to the " |
| 139 | "entry set.\n"); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 140 | findReferences(Worklist, Visited, EntryEdges, EntryIndexMap); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 141 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 142 | for (const Edge &E : EntryEdges) |
| 143 | SCCEntryNodes.push_back(&E.getFunction()); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 146 | LazyCallGraph::LazyCallGraph(LazyCallGraph &&G) |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 147 | : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)), |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 148 | EntryEdges(std::move(G.EntryEdges)), |
Chandler Carruth | 0b623ba | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 149 | EntryIndexMap(std::move(G.EntryIndexMap)), SCCBPA(std::move(G.SCCBPA)), |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 150 | SCCMap(std::move(G.SCCMap)), LeafSCCs(std::move(G.LeafSCCs)), |
| 151 | DFSStack(std::move(G.DFSStack)), |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 152 | SCCEntryNodes(std::move(G.SCCEntryNodes)), |
| 153 | NextDFSNumber(G.NextDFSNumber) { |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 154 | updateGraphPtrs(); |
| 155 | } |
| 156 | |
| 157 | LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) { |
| 158 | BPA = std::move(G.BPA); |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 159 | NodeMap = std::move(G.NodeMap); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 160 | EntryEdges = std::move(G.EntryEdges); |
Chandler Carruth | 0b623ba | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 161 | EntryIndexMap = std::move(G.EntryIndexMap); |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 162 | SCCBPA = std::move(G.SCCBPA); |
| 163 | SCCMap = std::move(G.SCCMap); |
| 164 | LeafSCCs = std::move(G.LeafSCCs); |
| 165 | DFSStack = std::move(G.DFSStack); |
| 166 | SCCEntryNodes = std::move(G.SCCEntryNodes); |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 167 | NextDFSNumber = G.NextDFSNumber; |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 168 | updateGraphPtrs(); |
| 169 | return *this; |
| 170 | } |
| 171 | |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 172 | void LazyCallGraph::SCC::insert(Node &N) { |
Chandler Carruth | 8f92d6d | 2014-04-26 01:03:46 +0000 | [diff] [blame] | 173 | N.DFSNumber = N.LowLink = -1; |
| 174 | Nodes.push_back(&N); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 175 | G->SCCMap[&N] = this; |
Chandler Carruth | 8f92d6d | 2014-04-26 01:03:46 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Chandler Carruth | 4b09674 | 2014-05-01 12:12:42 +0000 | [diff] [blame] | 178 | bool LazyCallGraph::SCC::isDescendantOf(const SCC &C) const { |
| 179 | // Walk up the parents of this SCC and verify that we eventually find C. |
| 180 | SmallVector<const SCC *, 4> AncestorWorklist; |
| 181 | AncestorWorklist.push_back(this); |
| 182 | do { |
| 183 | const SCC *AncestorC = AncestorWorklist.pop_back_val(); |
| 184 | if (AncestorC->isChildOf(C)) |
| 185 | return true; |
| 186 | for (const SCC *ParentC : AncestorC->ParentSCCs) |
| 187 | AncestorWorklist.push_back(ParentC); |
| 188 | } while (!AncestorWorklist.empty()); |
| 189 | |
| 190 | return false; |
| 191 | } |
| 192 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 193 | void LazyCallGraph::SCC::insertIntraSCCEdge(Node &ParentN, Node &ChildN, |
| 194 | Edge::Kind EK) { |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 195 | // First insert it into the caller. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 196 | ParentN.insertEdgeInternal(ChildN, EK); |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 197 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 198 | assert(G->SCCMap.lookup(&ParentN) == this && "Parent must be in this SCC."); |
| 199 | assert(G->SCCMap.lookup(&ChildN) == this && "Child must be in this SCC."); |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 200 | |
| 201 | // Nothing changes about this SCC or any other. |
| 202 | } |
| 203 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 204 | void LazyCallGraph::SCC::insertOutgoingEdge(Node &ParentN, Node &ChildN, |
| 205 | Edge::Kind EK) { |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 206 | // First insert it into the caller. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 207 | ParentN.insertEdgeInternal(ChildN, EK); |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 208 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 209 | assert(G->SCCMap.lookup(&ParentN) == this && "Parent must be in this SCC."); |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 210 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 211 | SCC &ChildC = *G->SCCMap.lookup(&ChildN); |
| 212 | assert(&ChildC != this && "Child must not be in this SCC."); |
| 213 | assert(ChildC.isDescendantOf(*this) && |
| 214 | "Child must be a descendant of the Parent."); |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 215 | |
Chandler Carruth | 9153911 | 2015-12-28 01:54:20 +0000 | [diff] [blame] | 216 | // The only change required is to add this SCC to the parent set of the |
| 217 | // callee. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 218 | ChildC.ParentSCCs.insert(this); |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 221 | SmallVector<LazyCallGraph::SCC *, 1> |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 222 | LazyCallGraph::SCC::insertIncomingEdge(Node &ParentN, Node &ChildN, |
| 223 | Edge::Kind EK) { |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 224 | // First insert it into the caller. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 225 | ParentN.insertEdgeInternal(ChildN, EK); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 226 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 227 | assert(G->SCCMap.lookup(&ChildN) == this && "Child must be in this SCC."); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 228 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 229 | SCC &ParentC = *G->SCCMap.lookup(&ParentN); |
| 230 | assert(&ParentC != this && "Parent must not be in this SCC."); |
| 231 | assert(ParentC.isDescendantOf(*this) && |
| 232 | "Parent must be a descendant of the Child."); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 233 | |
| 234 | // The algorithm we use for merging SCCs based on the cycle introduced here |
| 235 | // is to walk the SCC inverted DAG formed by the parent SCC sets. The inverse |
| 236 | // graph has the same cycle properties as the actual DAG of the SCCs, and |
| 237 | // when forming SCCs lazily by a DFS, the bottom of the graph won't exist in |
| 238 | // many cases which should prune the search space. |
| 239 | // |
| 240 | // FIXME: We can get this pruning behavior even after the incremental SCC |
| 241 | // formation by leaving behind (conservative) DFS numberings in the nodes, |
| 242 | // and pruning the search with them. These would need to be cleverly updated |
| 243 | // during the removal of intra-SCC edges, but could be preserved |
| 244 | // conservatively. |
| 245 | |
| 246 | // The set of SCCs that are connected to the caller, and thus will |
| 247 | // participate in the merged connected component. |
| 248 | SmallPtrSet<SCC *, 8> ConnectedSCCs; |
| 249 | ConnectedSCCs.insert(this); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 250 | ConnectedSCCs.insert(&ParentC); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 251 | |
| 252 | // We build up a DFS stack of the parents chains. |
| 253 | SmallVector<std::pair<SCC *, SCC::parent_iterator>, 8> DFSSCCs; |
| 254 | SmallPtrSet<SCC *, 8> VisitedSCCs; |
| 255 | int ConnectedDepth = -1; |
| 256 | SCC *C = this; |
| 257 | parent_iterator I = parent_begin(), E = parent_end(); |
| 258 | for (;;) { |
| 259 | while (I != E) { |
| 260 | SCC &ParentSCC = *I++; |
| 261 | |
| 262 | // If we have already processed this parent SCC, skip it, and remember |
| 263 | // whether it was connected so we don't have to check the rest of the |
| 264 | // stack. This also handles when we reach a child of the 'this' SCC (the |
| 265 | // callee) which terminates the search. |
| 266 | if (ConnectedSCCs.count(&ParentSCC)) { |
| 267 | ConnectedDepth = std::max<int>(ConnectedDepth, DFSSCCs.size()); |
| 268 | continue; |
| 269 | } |
| 270 | if (VisitedSCCs.count(&ParentSCC)) |
| 271 | continue; |
| 272 | |
| 273 | // We fully explore the depth-first space, adding nodes to the connected |
| 274 | // set only as we pop them off, so "recurse" by rotating to the parent. |
| 275 | DFSSCCs.push_back(std::make_pair(C, I)); |
| 276 | C = &ParentSCC; |
| 277 | I = ParentSCC.parent_begin(); |
| 278 | E = ParentSCC.parent_end(); |
| 279 | } |
| 280 | |
| 281 | // If we've found a connection anywhere below this point on the stack (and |
| 282 | // thus up the parent graph from the caller), the current node needs to be |
| 283 | // added to the connected set now that we've processed all of its parents. |
| 284 | if ((int)DFSSCCs.size() == ConnectedDepth) { |
| 285 | --ConnectedDepth; // We're finished with this connection. |
| 286 | ConnectedSCCs.insert(C); |
| 287 | } else { |
| 288 | // Otherwise remember that its parents don't ever connect. |
| 289 | assert(ConnectedDepth < (int)DFSSCCs.size() && |
| 290 | "Cannot have a connected depth greater than the DFS depth!"); |
| 291 | VisitedSCCs.insert(C); |
| 292 | } |
| 293 | |
| 294 | if (DFSSCCs.empty()) |
| 295 | break; // We've walked all the parents of the caller transitively. |
| 296 | |
| 297 | // Pop off the prior node and position to unwind the depth first recursion. |
| 298 | std::tie(C, I) = DFSSCCs.pop_back_val(); |
| 299 | E = C->parent_end(); |
| 300 | } |
| 301 | |
| 302 | // Now that we have identified all of the SCCs which need to be merged into |
| 303 | // a connected set with the inserted edge, merge all of them into this SCC. |
| 304 | // FIXME: This operation currently creates ordering stability problems |
| 305 | // because we don't use stably ordered containers for the parent SCCs or the |
| 306 | // connected SCCs. |
| 307 | unsigned NewNodeBeginIdx = Nodes.size(); |
| 308 | for (SCC *C : ConnectedSCCs) { |
| 309 | if (C == this) |
| 310 | continue; |
| 311 | for (SCC *ParentC : C->ParentSCCs) |
| 312 | if (!ConnectedSCCs.count(ParentC)) |
| 313 | ParentSCCs.insert(ParentC); |
| 314 | C->ParentSCCs.clear(); |
| 315 | |
| 316 | for (Node *N : *C) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 317 | for (Edge &E : *N) { |
| 318 | assert(E.getNode() && "Cannot have a null node within a visited SCC!"); |
| 319 | SCC &ChildC = *G->SCCMap.lookup(E.getNode()); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 320 | if (&ChildC != C) |
| 321 | ChildC.ParentSCCs.erase(C); |
| 322 | } |
| 323 | G->SCCMap[N] = this; |
| 324 | Nodes.push_back(N); |
| 325 | } |
| 326 | C->Nodes.clear(); |
| 327 | } |
| 328 | for (auto I = Nodes.begin() + NewNodeBeginIdx, E = Nodes.end(); I != E; ++I) |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 329 | for (Edge &E : **I) { |
| 330 | assert(E.getNode() && "Cannot have a null node within a visited SCC!"); |
| 331 | SCC &ChildC = *G->SCCMap.lookup(E.getNode()); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 332 | if (&ChildC != this) |
| 333 | ChildC.ParentSCCs.insert(this); |
| 334 | } |
| 335 | |
| 336 | // We return the list of SCCs which were merged so that callers can |
| 337 | // invalidate any data they have associated with those SCCs. Note that these |
| 338 | // SCCs are no longer in an interesting state (they are totally empty) but |
| 339 | // the pointers will remain stable for the life of the graph itself. |
| 340 | return SmallVector<SCC *, 1>(ConnectedSCCs.begin(), ConnectedSCCs.end()); |
| 341 | } |
| 342 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 343 | void LazyCallGraph::SCC::removeInterSCCEdge(Node &ParentN, Node &ChildN) { |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 344 | // First remove it from the node. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 345 | ParentN.removeEdgeInternal(ChildN.getFunction()); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 346 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 347 | assert(G->SCCMap.lookup(&ParentN) == this && |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 348 | "The caller must be a member of this SCC."); |
| 349 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 350 | SCC &ChildC = *G->SCCMap.lookup(&ChildN); |
| 351 | assert(&ChildC != this && |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 352 | "This API only supports the rmoval of inter-SCC edges."); |
| 353 | |
| 354 | assert(std::find(G->LeafSCCs.begin(), G->LeafSCCs.end(), this) == |
| 355 | G->LeafSCCs.end() && |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 356 | "Cannot have a leaf SCC caller with a different SCC callee."); |
| 357 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 358 | bool HasOtherEdgeToChildC = false; |
| 359 | bool HasOtherChildC = false; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 360 | for (Node *N : *this) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 361 | for (Edge &E : *N) { |
| 362 | assert(E.getNode() && "Cannot have a missing node in a visited SCC!"); |
| 363 | SCC &OtherChildC = *G->SCCMap.lookup(E.getNode()); |
| 364 | if (&OtherChildC == &ChildC) { |
| 365 | HasOtherEdgeToChildC = true; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 366 | break; |
| 367 | } |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 368 | if (&OtherChildC != this) |
| 369 | HasOtherChildC = true; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 370 | } |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 371 | if (HasOtherEdgeToChildC) |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 372 | break; |
| 373 | } |
| 374 | // Because the SCCs form a DAG, deleting such an edge cannot change the set |
| 375 | // of SCCs in the graph. However, it may cut an edge of the SCC DAG, making |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 376 | // the parent SCC no longer connected to the child SCC. If so, we need to |
| 377 | // update the child SCC's map of its parents. |
| 378 | if (!HasOtherEdgeToChildC) { |
| 379 | bool Removed = ChildC.ParentSCCs.erase(this); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 380 | (void)Removed; |
| 381 | assert(Removed && |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 382 | "Did not find the parent SCC in the child SCC's parent list!"); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 383 | |
| 384 | // It may orphan an SCC if it is the last edge reaching it, but that does |
| 385 | // not violate any invariants of the graph. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 386 | if (ChildC.ParentSCCs.empty()) |
| 387 | DEBUG(dbgs() << "LCG: Update removing " << ParentN.getFunction().getName() |
| 388 | << " -> " << ChildN.getFunction().getName() |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 389 | << " edge orphaned the callee's SCC!\n"); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 392 | // It may make the Parent SCC a leaf SCC. |
| 393 | if (!HasOtherChildC) |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 394 | G->LeafSCCs.push_back(this); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 397 | void LazyCallGraph::SCC::internalDFS( |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 398 | SmallVectorImpl<std::pair<Node *, Node::edge_iterator>> &DFSStack, |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 399 | SmallVectorImpl<Node *> &PendingSCCStack, Node *N, |
| 400 | SmallVectorImpl<SCC *> &ResultSCCs) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 401 | auto I = N->begin(); |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 402 | N->LowLink = N->DFSNumber = 1; |
| 403 | int NextDFSNumber = 2; |
| 404 | for (;;) { |
| 405 | assert(N->DFSNumber != 0 && "We should always assign a DFS number " |
| 406 | "before processing a node."); |
| 407 | |
| 408 | // We simulate recursion by popping out of the nested loop and continuing. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 409 | auto E = N->end(); |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 410 | while (I != E) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 411 | Node &ChildN = I->getNode(*G); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 412 | if (SCC *ChildSCC = G->SCCMap.lookup(&ChildN)) { |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 413 | // Check if we have reached a node in the new (known connected) set of |
| 414 | // this SCC. If so, the entire stack is necessarily in that set and we |
| 415 | // can re-start. |
| 416 | if (ChildSCC == this) { |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 417 | insert(*N); |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 418 | while (!PendingSCCStack.empty()) |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 419 | insert(*PendingSCCStack.pop_back_val()); |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 420 | while (!DFSStack.empty()) |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 421 | insert(*DFSStack.pop_back_val().first); |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 422 | return; |
| 423 | } |
| 424 | |
| 425 | // If this child isn't currently in this SCC, no need to process it. |
| 426 | // However, we do need to remove this SCC from its SCC's parent set. |
| 427 | ChildSCC->ParentSCCs.erase(this); |
| 428 | ++I; |
| 429 | continue; |
| 430 | } |
| 431 | |
| 432 | if (ChildN.DFSNumber == 0) { |
| 433 | // Mark that we should start at this child when next this node is the |
| 434 | // top of the stack. We don't start at the next child to ensure this |
| 435 | // child's lowlink is reflected. |
| 436 | DFSStack.push_back(std::make_pair(N, I)); |
| 437 | |
| 438 | // Continue, resetting to the child node. |
| 439 | ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++; |
| 440 | N = &ChildN; |
| 441 | I = ChildN.begin(); |
| 442 | E = ChildN.end(); |
| 443 | continue; |
| 444 | } |
| 445 | |
Alp Toker | beaca19 | 2014-05-15 01:52:21 +0000 | [diff] [blame] | 446 | // Track the lowest link of the children, if any are still in the stack. |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 447 | // Any child not on the stack will have a LowLink of -1. |
| 448 | assert(ChildN.LowLink != 0 && |
| 449 | "Low-link must not be zero with a non-zero DFS number."); |
| 450 | if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink) |
| 451 | N->LowLink = ChildN.LowLink; |
| 452 | ++I; |
| 453 | } |
| 454 | |
| 455 | if (N->LowLink == N->DFSNumber) { |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 456 | ResultSCCs.push_back(G->formSCC(N, PendingSCCStack)); |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 457 | if (DFSStack.empty()) |
| 458 | return; |
| 459 | } else { |
| 460 | // At this point we know that N cannot ever be an SCC root. Its low-link |
| 461 | // is not its dfs-number, and we've processed all of its children. It is |
| 462 | // just sitting here waiting until some node further down the stack gets |
| 463 | // low-link == dfs-number and pops it off as well. Move it to the pending |
| 464 | // stack which is pulled into the next SCC to be formed. |
| 465 | PendingSCCStack.push_back(N); |
| 466 | |
| 467 | assert(!DFSStack.empty() && "We shouldn't have an empty stack!"); |
| 468 | } |
| 469 | |
| 470 | N = DFSStack.back().first; |
| 471 | I = DFSStack.back().second; |
| 472 | DFSStack.pop_back(); |
| 473 | } |
| 474 | } |
| 475 | |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 476 | SmallVector<LazyCallGraph::SCC *, 1> |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 477 | LazyCallGraph::SCC::removeIntraSCCEdge(Node &ParentN, Node &ChildN) { |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 478 | // First remove it from the node. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 479 | ParentN.removeEdgeInternal(ChildN.getFunction()); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 480 | |
Chandler Carruth | 3f5f5fe | 2014-04-28 10:49:06 +0000 | [diff] [blame] | 481 | // We return a list of the resulting *new* SCCs in postorder. |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 482 | SmallVector<SCC *, 1> ResultSCCs; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 483 | |
Chandler Carruth | a7205b6 | 2014-04-26 03:36:37 +0000 | [diff] [blame] | 484 | // Direct recursion doesn't impact the SCC graph at all. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 485 | if (&ParentN == &ChildN) |
Chandler Carruth | a7205b6 | 2014-04-26 03:36:37 +0000 | [diff] [blame] | 486 | return ResultSCCs; |
| 487 | |
Chandler Carruth | 770060d | 2014-04-25 09:08:05 +0000 | [diff] [blame] | 488 | // The worklist is every node in the original SCC. |
| 489 | SmallVector<Node *, 1> Worklist; |
| 490 | Worklist.swap(Nodes); |
| 491 | for (Node *N : Worklist) { |
Chandler Carruth | 2e6ef0e | 2014-04-25 09:08:10 +0000 | [diff] [blame] | 492 | // The nodes formerly in this SCC are no longer in any SCC. |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 493 | N->DFSNumber = 0; |
| 494 | N->LowLink = 0; |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 495 | G->SCCMap.erase(N); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 496 | } |
Chandler Carruth | a7205b6 | 2014-04-26 03:36:37 +0000 | [diff] [blame] | 497 | assert(Worklist.size() > 1 && "We have to have at least two nodes to have an " |
| 498 | "edge between them that is within the SCC."); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 499 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 500 | // The child can already reach every node in this SCC (by definition). It is |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 501 | // the only node we know will stay inside this SCC. Everything which |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 502 | // transitively reaches Child will also remain in the SCC. To model this we |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 503 | // incrementally add any chain of nodes which reaches something in the new |
| 504 | // node set to the new node set. This short circuits one side of the Tarjan's |
| 505 | // walk. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 506 | insert(ChildN); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 507 | |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 508 | // We're going to do a full mini-Tarjan's walk using a local stack here. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 509 | SmallVector<std::pair<Node *, Node::edge_iterator>, 4> DFSStack; |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 510 | SmallVector<Node *, 4> PendingSCCStack; |
| 511 | do { |
| 512 | Node *N = Worklist.pop_back_val(); |
| 513 | if (N->DFSNumber == 0) |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 514 | internalDFS(DFSStack, PendingSCCStack, N, ResultSCCs); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 515 | |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 516 | assert(DFSStack.empty() && "Didn't flush the entire DFS stack!"); |
| 517 | assert(PendingSCCStack.empty() && "Didn't flush all pending SCC nodes!"); |
| 518 | } while (!Worklist.empty()); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 519 | |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 520 | // Now we need to reconnect the current SCC to the graph. |
| 521 | bool IsLeafSCC = true; |
Chandler Carruth | 9ba7762 | 2014-04-25 09:52:44 +0000 | [diff] [blame] | 522 | for (Node *N : Nodes) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 523 | for (Edge &E : *N) { |
| 524 | assert(E.getNode() && "Cannot have a missing node in a visited SCC!"); |
| 525 | SCC &ChildSCC = *G->SCCMap.lookup(E.getNode()); |
Chandler Carruth | 9ba7762 | 2014-04-25 09:52:44 +0000 | [diff] [blame] | 526 | if (&ChildSCC == this) |
| 527 | continue; |
Chandler Carruth | bd5d308 | 2014-04-23 23:34:48 +0000 | [diff] [blame] | 528 | ChildSCC.ParentSCCs.insert(this); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 529 | IsLeafSCC = false; |
| 530 | } |
| 531 | } |
| 532 | #ifndef NDEBUG |
Chandler Carruth | 3f5f5fe | 2014-04-28 10:49:06 +0000 | [diff] [blame] | 533 | if (!ResultSCCs.empty()) |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 534 | assert(!IsLeafSCC && "This SCC cannot be a leaf as we have split out new " |
| 535 | "SCCs by removing this edge."); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 536 | if (!std::any_of(G->LeafSCCs.begin(), G->LeafSCCs.end(), |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 537 | [&](SCC *C) { return C == this; })) |
| 538 | assert(!IsLeafSCC && "This SCC cannot be a leaf as it already had child " |
| 539 | "SCCs before we removed this edge."); |
| 540 | #endif |
| 541 | // If this SCC stopped being a leaf through this edge removal, remove it from |
| 542 | // the leaf SCC list. |
Chandler Carruth | 3f5f5fe | 2014-04-28 10:49:06 +0000 | [diff] [blame] | 543 | if (!IsLeafSCC && !ResultSCCs.empty()) |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 544 | G->LeafSCCs.erase(std::remove(G->LeafSCCs.begin(), G->LeafSCCs.end(), this), |
Chandler Carruth | 9153911 | 2015-12-28 01:54:20 +0000 | [diff] [blame] | 545 | G->LeafSCCs.end()); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 546 | |
| 547 | // Return the new list of SCCs. |
| 548 | return ResultSCCs; |
| 549 | } |
| 550 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 551 | void LazyCallGraph::insertEdge(Node &ParentN, Function &Child, Edge::Kind EK) { |
Chandler Carruth | c00a7ff | 2014-04-28 11:10:23 +0000 | [diff] [blame] | 552 | assert(SCCMap.empty() && DFSStack.empty() && |
| 553 | "This method cannot be called after SCCs have been formed!"); |
| 554 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 555 | return ParentN.insertEdgeInternal(Child, EK); |
Chandler Carruth | c00a7ff | 2014-04-28 11:10:23 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 558 | void LazyCallGraph::removeEdge(Node &ParentN, Function &Child) { |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 559 | assert(SCCMap.empty() && DFSStack.empty() && |
| 560 | "This method cannot be called after SCCs have been formed!"); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 561 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 562 | return ParentN.removeEdgeInternal(Child); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Chandler Carruth | 2a898e0 | 2014-04-23 23:20:36 +0000 | [diff] [blame] | 565 | LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) { |
| 566 | return *new (MappedN = BPA.Allocate()) Node(*this, F); |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | void LazyCallGraph::updateGraphPtrs() { |
Chandler Carruth | b60cb31 | 2014-04-17 07:25:59 +0000 | [diff] [blame] | 570 | // Process all nodes updating the graph pointers. |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 571 | { |
| 572 | SmallVector<Node *, 16> Worklist; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 573 | for (Edge &E : EntryEdges) |
| 574 | if (Node *EntryN = E.getNode()) |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 575 | Worklist.push_back(EntryN); |
Chandler Carruth | b60cb31 | 2014-04-17 07:25:59 +0000 | [diff] [blame] | 576 | |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 577 | while (!Worklist.empty()) { |
| 578 | Node *N = Worklist.pop_back_val(); |
| 579 | N->G = this; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 580 | for (Edge &E : N->Edges) |
| 581 | if (Node *ChildN = E.getNode()) |
| 582 | Worklist.push_back(ChildN); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
| 586 | // Process all SCCs updating the graph pointers. |
| 587 | { |
| 588 | SmallVector<SCC *, 16> Worklist(LeafSCCs.begin(), LeafSCCs.end()); |
| 589 | |
| 590 | while (!Worklist.empty()) { |
| 591 | SCC *C = Worklist.pop_back_val(); |
| 592 | C->G = this; |
| 593 | Worklist.insert(Worklist.end(), C->ParentSCCs.begin(), |
| 594 | C->ParentSCCs.end()); |
| 595 | } |
Chandler Carruth | b60cb31 | 2014-04-17 07:25:59 +0000 | [diff] [blame] | 596 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 597 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 598 | |
Chandler Carruth | 2455393 | 2014-04-24 11:05:20 +0000 | [diff] [blame] | 599 | LazyCallGraph::SCC *LazyCallGraph::formSCC(Node *RootN, |
| 600 | SmallVectorImpl<Node *> &NodeStack) { |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 601 | // The tail of the stack is the new SCC. Allocate the SCC and pop the stack |
| 602 | // into it. |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 603 | SCC *NewSCC = new (SCCBPA.Allocate()) SCC(*this); |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 604 | |
Chandler Carruth | 2455393 | 2014-04-24 11:05:20 +0000 | [diff] [blame] | 605 | while (!NodeStack.empty() && NodeStack.back()->DFSNumber > RootN->DFSNumber) { |
Chandler Carruth | 8f92d6d | 2014-04-26 01:03:46 +0000 | [diff] [blame] | 606 | assert(NodeStack.back()->LowLink >= RootN->LowLink && |
Chandler Carruth | cace662 | 2014-04-23 10:31:17 +0000 | [diff] [blame] | 607 | "We cannot have a low link in an SCC lower than its root on the " |
| 608 | "stack!"); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 609 | NewSCC->insert(*NodeStack.pop_back_val()); |
Chandler Carruth | cace662 | 2014-04-23 10:31:17 +0000 | [diff] [blame] | 610 | } |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 611 | NewSCC->insert(*RootN); |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 612 | |
| 613 | // A final pass over all edges in the SCC (this remains linear as we only |
| 614 | // do this once when we build the SCC) to connect it to the parent sets of |
| 615 | // its children. |
| 616 | bool IsLeafSCC = true; |
| 617 | for (Node *SCCN : NewSCC->Nodes) |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 618 | for (Edge &E : *SCCN) { |
| 619 | assert(E.getNode() && "Cannot have a missing node in a visited SCC!"); |
| 620 | SCC &ChildSCC = *SCCMap.lookup(E.getNode()); |
Chandler Carruth | 034d0d6 | 2014-05-01 12:16:31 +0000 | [diff] [blame] | 621 | if (&ChildSCC == NewSCC) |
| 622 | continue; |
Chandler Carruth | bd5d308 | 2014-04-23 23:34:48 +0000 | [diff] [blame] | 623 | ChildSCC.ParentSCCs.insert(NewSCC); |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 624 | IsLeafSCC = false; |
| 625 | } |
| 626 | |
| 627 | // For the SCCs where we fine no child SCCs, add them to the leaf list. |
| 628 | if (IsLeafSCC) |
| 629 | LeafSCCs.push_back(NewSCC); |
| 630 | |
| 631 | return NewSCC; |
| 632 | } |
| 633 | |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 634 | LazyCallGraph::SCC *LazyCallGraph::getNextSCCInPostOrder() { |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 635 | Node *N; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 636 | Node::edge_iterator I; |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 637 | if (!DFSStack.empty()) { |
| 638 | N = DFSStack.back().first; |
| 639 | I = DFSStack.back().second; |
| 640 | DFSStack.pop_back(); |
| 641 | } else { |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 642 | // If we've handled all candidate entry nodes to the SCC forest, we're done. |
Chandler Carruth | 90821c2 | 2014-04-26 09:45:55 +0000 | [diff] [blame] | 643 | do { |
| 644 | if (SCCEntryNodes.empty()) |
| 645 | return nullptr; |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 646 | |
Chandler Carruth | 90821c2 | 2014-04-26 09:45:55 +0000 | [diff] [blame] | 647 | N = &get(*SCCEntryNodes.pop_back_val()); |
| 648 | } while (N->DFSNumber != 0); |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 649 | I = N->begin(); |
| 650 | N->LowLink = N->DFSNumber = 1; |
Chandler Carruth | 09751bf | 2014-04-24 09:59:59 +0000 | [diff] [blame] | 651 | NextDFSNumber = 2; |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Chandler Carruth | 91dcf0f | 2014-04-24 21:19:30 +0000 | [diff] [blame] | 654 | for (;;) { |
Chandler Carruth | 2455393 | 2014-04-24 11:05:20 +0000 | [diff] [blame] | 655 | assert(N->DFSNumber != 0 && "We should always assign a DFS number " |
| 656 | "before placing a node onto the stack."); |
| 657 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 658 | auto E = N->end(); |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 659 | while (I != E) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 660 | Node &ChildN = I->getNode(*this); |
Chandler Carruth | bd5d308 | 2014-04-23 23:34:48 +0000 | [diff] [blame] | 661 | if (ChildN.DFSNumber == 0) { |
Chandler Carruth | cace662 | 2014-04-23 10:31:17 +0000 | [diff] [blame] | 662 | // Mark that we should start at this child when next this node is the |
| 663 | // top of the stack. We don't start at the next child to ensure this |
| 664 | // child's lowlink is reflected. |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 665 | DFSStack.push_back(std::make_pair(N, N->begin())); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 666 | |
Chandler Carruth | cace662 | 2014-04-23 10:31:17 +0000 | [diff] [blame] | 667 | // Recurse onto this node via a tail call. |
Chandler Carruth | 09751bf | 2014-04-24 09:59:59 +0000 | [diff] [blame] | 668 | assert(!SCCMap.count(&ChildN) && |
| 669 | "Found a node with 0 DFS number but already in an SCC!"); |
| 670 | ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++; |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 671 | N = &ChildN; |
| 672 | I = ChildN.begin(); |
| 673 | E = ChildN.end(); |
| 674 | continue; |
Chandler Carruth | cace662 | 2014-04-23 10:31:17 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Alp Toker | beaca19 | 2014-05-15 01:52:21 +0000 | [diff] [blame] | 677 | // Track the lowest link of the children, if any are still in the stack. |
Chandler Carruth | bd5d308 | 2014-04-23 23:34:48 +0000 | [diff] [blame] | 678 | assert(ChildN.LowLink != 0 && |
Chandler Carruth | b4a04da | 2014-04-23 22:28:13 +0000 | [diff] [blame] | 679 | "Low-link must not be zero with a non-zero DFS number."); |
Chandler Carruth | bd5d308 | 2014-04-23 23:34:48 +0000 | [diff] [blame] | 680 | if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink) |
| 681 | N->LowLink = ChildN.LowLink; |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 682 | ++I; |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Chandler Carruth | cace662 | 2014-04-23 10:31:17 +0000 | [diff] [blame] | 685 | if (N->LowLink == N->DFSNumber) |
| 686 | // Form the new SCC out of the top of the DFS stack. |
Chandler Carruth | 2455393 | 2014-04-24 11:05:20 +0000 | [diff] [blame] | 687 | return formSCC(N, PendingSCCStack); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 688 | |
Chandler Carruth | 2455393 | 2014-04-24 11:05:20 +0000 | [diff] [blame] | 689 | // At this point we know that N cannot ever be an SCC root. Its low-link |
| 690 | // is not its dfs-number, and we've processed all of its children. It is |
| 691 | // just sitting here waiting until some node further down the stack gets |
| 692 | // low-link == dfs-number and pops it off as well. Move it to the pending |
| 693 | // stack which is pulled into the next SCC to be formed. |
| 694 | PendingSCCStack.push_back(N); |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 695 | |
| 696 | assert(!DFSStack.empty() && "We never found a viable root!"); |
| 697 | N = DFSStack.back().first; |
| 698 | I = DFSStack.back().second; |
| 699 | DFSStack.pop_back(); |
Chandler Carruth | 91dcf0f | 2014-04-24 21:19:30 +0000 | [diff] [blame] | 700 | } |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 703 | char LazyCallGraphAnalysis::PassID; |
| 704 | |
| 705 | LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {} |
| 706 | |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 707 | static void printNodes(raw_ostream &OS, LazyCallGraph::Node &N, |
| 708 | SmallPtrSetImpl<LazyCallGraph::Node *> &Printed) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 709 | LazyCallGraph &G = N.getGraph(); |
| 710 | |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 711 | // Recurse depth first through the nodes. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 712 | for (LazyCallGraph::Edge &E : N) { |
| 713 | LazyCallGraph::Node &ChildN = E.getNode(G); |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 714 | if (Printed.insert(&ChildN).second) |
| 715 | printNodes(OS, ChildN, Printed); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 716 | } |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 717 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 718 | OS << " Edges in function: " << N.getFunction().getName() << "\n"; |
| 719 | for (const LazyCallGraph::Edge &E : N) |
| 720 | OS << " " << (E.isCall() ? "call" : "ref ") << " -> " |
| 721 | << E.getFunction().getName() << "\n"; |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 722 | |
| 723 | OS << "\n"; |
| 724 | } |
| 725 | |
| 726 | static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &SCC) { |
| 727 | ptrdiff_t SCCSize = std::distance(SCC.begin(), SCC.end()); |
| 728 | OS << " SCC with " << SCCSize << " functions:\n"; |
| 729 | |
| 730 | for (LazyCallGraph::Node *N : SCC) |
| 731 | OS << " " << N->getFunction().getName() << "\n"; |
| 732 | |
| 733 | OS << "\n"; |
| 734 | } |
| 735 | |
Chandler Carruth | d174ce4 | 2015-01-05 02:47:05 +0000 | [diff] [blame] | 736 | PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M, |
Chandler Carruth | e9b5061 | 2014-03-10 02:14:14 +0000 | [diff] [blame] | 737 | ModuleAnalysisManager *AM) { |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 738 | LazyCallGraph &G = AM->getResult<LazyCallGraphAnalysis>(M); |
| 739 | |
| 740 | OS << "Printing the call graph for module: " << M.getModuleIdentifier() |
| 741 | << "\n\n"; |
| 742 | |
| 743 | SmallPtrSet<LazyCallGraph::Node *, 16> Printed; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 744 | for (LazyCallGraph::Edge &E : G) { |
| 745 | LazyCallGraph::Node &N = E.getNode(G); |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 746 | if (Printed.insert(&N).second) |
| 747 | printNodes(OS, N, Printed); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 748 | } |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 749 | |
| 750 | for (LazyCallGraph::SCC &SCC : G.postorder_sccs()) |
| 751 | printSCC(OS, SCC); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 752 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 753 | return PreservedAnalyses::all(); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 754 | } |