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" |
Sean Silva | 7cb3066 | 2016-06-18 09:17:32 +0000 | [diff] [blame] | 17 | #include "llvm/Support/GraphWriter.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, |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 24 | DenseMap<Function *, int> &EdgeIndexMap, Function &F, |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 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() && |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 37 | EdgeIndexMap.insert({&F, Edges.size()}).second) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 38 | DEBUG(dbgs() << " Added callable function: " << F.getName() << "\n"); |
| 39 | Edges.emplace_back(LazyCallGraph::Edge(F, EK)); |
| 40 | } |
| 41 | } |
| 42 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 43 | static void findReferences(SmallVectorImpl<Constant *> &Worklist, |
| 44 | SmallPtrSetImpl<Constant *> &Visited, |
| 45 | SmallVectorImpl<LazyCallGraph::Edge> &Edges, |
| 46 | DenseMap<Function *, int> &EdgeIndexMap) { |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 47 | while (!Worklist.empty()) { |
| 48 | Constant *C = Worklist.pop_back_val(); |
| 49 | |
| 50 | if (Function *F = dyn_cast<Function>(C)) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 51 | addEdge(Edges, EdgeIndexMap, *F, LazyCallGraph::Edge::Ref); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 52 | continue; |
| 53 | } |
| 54 | |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 55 | for (Value *Op : C->operand_values()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 56 | if (Visited.insert(cast<Constant>(Op)).second) |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 57 | Worklist.push_back(cast<Constant>(Op)); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 61 | LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F) |
| 62 | : G(&G), F(F), DFSNumber(0), LowLink(0) { |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 63 | DEBUG(dbgs() << " Adding functions called by '" << F.getName() |
| 64 | << "' to the graph.\n"); |
| 65 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 66 | SmallVector<Constant *, 16> Worklist; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 67 | SmallPtrSet<Function *, 4> Callees; |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 68 | SmallPtrSet<Constant *, 16> Visited; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 69 | |
| 70 | // Find all the potential call graph edges in this function. We track both |
| 71 | // actual call edges and indirect references to functions. The direct calls |
| 72 | // are trivially added, but to accumulate the latter we walk the instructions |
| 73 | // and add every operand which is a constant to the worklist to process |
| 74 | // afterward. |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 75 | for (BasicBlock &BB : F) |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 76 | for (Instruction &I : BB) { |
| 77 | if (auto CS = CallSite(&I)) |
| 78 | if (Function *Callee = CS.getCalledFunction()) |
| 79 | if (Callees.insert(Callee).second) { |
| 80 | Visited.insert(Callee); |
| 81 | addEdge(Edges, EdgeIndexMap, *Callee, LazyCallGraph::Edge::Call); |
| 82 | } |
| 83 | |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 84 | for (Value *Op : I.operand_values()) |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 85 | if (Constant *C = dyn_cast<Constant>(Op)) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 86 | if (Visited.insert(C).second) |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 87 | Worklist.push_back(C); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 88 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 89 | |
| 90 | // We've collected all the constant (and thus potentially function or |
| 91 | // function containing) operands to all of the instructions in the function. |
| 92 | // Process them (recursively) collecting every function found. |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 93 | findReferences(Worklist, Visited, Edges, EdgeIndexMap); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 96 | void LazyCallGraph::Node::insertEdgeInternal(Function &Target, Edge::Kind EK) { |
| 97 | if (Node *N = G->lookup(Target)) |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 98 | return insertEdgeInternal(*N, EK); |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 99 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 100 | EdgeIndexMap.insert({&Target, Edges.size()}); |
| 101 | Edges.emplace_back(Target, EK); |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 104 | void LazyCallGraph::Node::insertEdgeInternal(Node &TargetN, Edge::Kind EK) { |
| 105 | EdgeIndexMap.insert({&TargetN.getFunction(), Edges.size()}); |
| 106 | Edges.emplace_back(TargetN, EK); |
Chandler Carruth | c00a7ff | 2014-04-28 11:10:23 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 109 | void LazyCallGraph::Node::setEdgeKind(Function &TargetF, Edge::Kind EK) { |
| 110 | Edges[EdgeIndexMap.find(&TargetF)->second].setKind(EK); |
| 111 | } |
| 112 | |
| 113 | void LazyCallGraph::Node::removeEdgeInternal(Function &Target) { |
| 114 | auto IndexMapI = EdgeIndexMap.find(&Target); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 115 | assert(IndexMapI != EdgeIndexMap.end() && |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 116 | "Target not in the edge set for this caller?"); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 117 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 118 | Edges[IndexMapI->second] = Edge(); |
| 119 | EdgeIndexMap.erase(IndexMapI); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Chandler Carruth | dca8340 | 2016-06-27 23:26:08 +0000 | [diff] [blame] | 122 | void LazyCallGraph::Node::dump() const { |
| 123 | dbgs() << *this << '\n'; |
| 124 | } |
| 125 | |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 126 | LazyCallGraph::LazyCallGraph(Module &M) : NextDFSNumber(0) { |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 127 | DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier() |
| 128 | << "\n"); |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 129 | for (Function &F : M) |
| 130 | if (!F.isDeclaration() && !F.hasLocalLinkage()) |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 131 | if (EntryIndexMap.insert({&F, EntryEdges.size()}).second) { |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 132 | DEBUG(dbgs() << " Adding '" << F.getName() |
| 133 | << "' to entry set of the graph.\n"); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 134 | EntryEdges.emplace_back(F, Edge::Ref); |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 135 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 136 | |
| 137 | // Now add entry nodes for functions reachable via initializers to globals. |
| 138 | SmallVector<Constant *, 16> Worklist; |
| 139 | SmallPtrSet<Constant *, 16> Visited; |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 140 | for (GlobalVariable &GV : M.globals()) |
| 141 | if (GV.hasInitializer()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 142 | if (Visited.insert(GV.getInitializer()).second) |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 143 | Worklist.push_back(GV.getInitializer()); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 144 | |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 145 | DEBUG(dbgs() << " Adding functions referenced by global initializers to the " |
| 146 | "entry set.\n"); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 147 | findReferences(Worklist, Visited, EntryEdges, EntryIndexMap); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 148 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 149 | for (const Edge &E : EntryEdges) |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 150 | RefSCCEntryNodes.push_back(&E.getFunction()); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 153 | LazyCallGraph::LazyCallGraph(LazyCallGraph &&G) |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 154 | : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)), |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 155 | EntryEdges(std::move(G.EntryEdges)), |
Chandler Carruth | 0b623ba | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 156 | EntryIndexMap(std::move(G.EntryIndexMap)), SCCBPA(std::move(G.SCCBPA)), |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 157 | SCCMap(std::move(G.SCCMap)), LeafRefSCCs(std::move(G.LeafRefSCCs)), |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 158 | DFSStack(std::move(G.DFSStack)), |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 159 | RefSCCEntryNodes(std::move(G.RefSCCEntryNodes)), |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 160 | NextDFSNumber(G.NextDFSNumber) { |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 161 | updateGraphPtrs(); |
| 162 | } |
| 163 | |
| 164 | LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) { |
| 165 | BPA = std::move(G.BPA); |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 166 | NodeMap = std::move(G.NodeMap); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 167 | EntryEdges = std::move(G.EntryEdges); |
Chandler Carruth | 0b623ba | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 168 | EntryIndexMap = std::move(G.EntryIndexMap); |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 169 | SCCBPA = std::move(G.SCCBPA); |
| 170 | SCCMap = std::move(G.SCCMap); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 171 | LeafRefSCCs = std::move(G.LeafRefSCCs); |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 172 | DFSStack = std::move(G.DFSStack); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 173 | RefSCCEntryNodes = std::move(G.RefSCCEntryNodes); |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 174 | NextDFSNumber = G.NextDFSNumber; |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 175 | updateGraphPtrs(); |
| 176 | return *this; |
| 177 | } |
| 178 | |
Chandler Carruth | dca8340 | 2016-06-27 23:26:08 +0000 | [diff] [blame] | 179 | void LazyCallGraph::SCC::dump() const { |
| 180 | dbgs() << *this << '\n'; |
| 181 | } |
| 182 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 183 | #ifndef NDEBUG |
| 184 | void LazyCallGraph::SCC::verify() { |
| 185 | assert(OuterRefSCC && "Can't have a null RefSCC!"); |
| 186 | assert(!Nodes.empty() && "Can't have an empty SCC!"); |
Chandler Carruth | 8f92d6d | 2014-04-26 01:03:46 +0000 | [diff] [blame] | 187 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 188 | for (Node *N : Nodes) { |
| 189 | assert(N && "Can't have a null node!"); |
| 190 | assert(OuterRefSCC->G->lookupSCC(*N) == this && |
| 191 | "Node does not map to this SCC!"); |
| 192 | assert(N->DFSNumber == -1 && |
| 193 | "Must set DFS numbers to -1 when adding a node to an SCC!"); |
| 194 | assert(N->LowLink == -1 && |
| 195 | "Must set low link to -1 when adding a node to an SCC!"); |
| 196 | for (Edge &E : *N) |
| 197 | assert(E.getNode() && "Can't have an edge to a raw function!"); |
| 198 | } |
| 199 | } |
| 200 | #endif |
| 201 | |
| 202 | LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {} |
| 203 | |
Chandler Carruth | dca8340 | 2016-06-27 23:26:08 +0000 | [diff] [blame] | 204 | void LazyCallGraph::RefSCC::dump() const { |
| 205 | dbgs() << *this << '\n'; |
| 206 | } |
| 207 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 208 | #ifndef NDEBUG |
| 209 | void LazyCallGraph::RefSCC::verify() { |
| 210 | assert(G && "Can't have a null graph!"); |
| 211 | assert(!SCCs.empty() && "Can't have an empty SCC!"); |
| 212 | |
| 213 | // Verify basic properties of the SCCs. |
| 214 | for (SCC *C : SCCs) { |
| 215 | assert(C && "Can't have a null SCC!"); |
| 216 | C->verify(); |
| 217 | assert(&C->getOuterRefSCC() == this && |
| 218 | "SCC doesn't think it is inside this RefSCC!"); |
| 219 | } |
| 220 | |
| 221 | // Check that our indices map correctly. |
| 222 | for (auto &SCCIndexPair : SCCIndices) { |
| 223 | SCC *C = SCCIndexPair.first; |
| 224 | int i = SCCIndexPair.second; |
| 225 | assert(C && "Can't have a null SCC in the indices!"); |
| 226 | assert(SCCs[i] == C && "Index doesn't point to SCC!"); |
| 227 | } |
| 228 | |
| 229 | // Check that the SCCs are in fact in post-order. |
| 230 | for (int i = 0, Size = SCCs.size(); i < Size; ++i) { |
| 231 | SCC &SourceSCC = *SCCs[i]; |
| 232 | for (Node &N : SourceSCC) |
| 233 | for (Edge &E : N) { |
| 234 | if (!E.isCall()) |
| 235 | continue; |
| 236 | SCC &TargetSCC = *G->lookupSCC(*E.getNode()); |
| 237 | if (&TargetSCC.getOuterRefSCC() == this) { |
| 238 | assert(SCCIndices.find(&TargetSCC)->second <= i && |
| 239 | "Edge between SCCs violates post-order relationship."); |
| 240 | continue; |
| 241 | } |
| 242 | assert(TargetSCC.getOuterRefSCC().Parents.count(this) && |
| 243 | "Edge to a RefSCC missing us in its parent set."); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | #endif |
| 248 | |
| 249 | bool LazyCallGraph::RefSCC::isDescendantOf(const RefSCC &C) const { |
Chandler Carruth | 4b09674 | 2014-05-01 12:12:42 +0000 | [diff] [blame] | 250 | // Walk up the parents of this SCC and verify that we eventually find C. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 251 | SmallVector<const RefSCC *, 4> AncestorWorklist; |
Chandler Carruth | 4b09674 | 2014-05-01 12:12:42 +0000 | [diff] [blame] | 252 | AncestorWorklist.push_back(this); |
| 253 | do { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 254 | const RefSCC *AncestorC = AncestorWorklist.pop_back_val(); |
Chandler Carruth | 4b09674 | 2014-05-01 12:12:42 +0000 | [diff] [blame] | 255 | if (AncestorC->isChildOf(C)) |
| 256 | return true; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 257 | for (const RefSCC *ParentC : AncestorC->Parents) |
Chandler Carruth | 4b09674 | 2014-05-01 12:12:42 +0000 | [diff] [blame] | 258 | AncestorWorklist.push_back(ParentC); |
| 259 | } while (!AncestorWorklist.empty()); |
| 260 | |
| 261 | return false; |
| 262 | } |
| 263 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 264 | SmallVector<LazyCallGraph::SCC *, 1> |
| 265 | LazyCallGraph::RefSCC::switchInternalEdgeToCall(Node &SourceN, Node &TargetN) { |
| 266 | assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!"); |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 267 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 268 | SmallVector<SCC *, 1> DeletedSCCs; |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 269 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 270 | SCC &SourceSCC = *G->lookupSCC(SourceN); |
| 271 | SCC &TargetSCC = *G->lookupSCC(TargetN); |
| 272 | |
| 273 | // If the two nodes are already part of the same SCC, we're also done as |
| 274 | // we've just added more connectivity. |
| 275 | if (&SourceSCC == &TargetSCC) { |
| 276 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call); |
| 277 | #ifndef NDEBUG |
| 278 | // Check that the RefSCC is still valid. |
| 279 | verify(); |
| 280 | #endif |
| 281 | return DeletedSCCs; |
| 282 | } |
| 283 | |
| 284 | // At this point we leverage the postorder list of SCCs to detect when the |
| 285 | // insertion of an edge changes the SCC structure in any way. |
| 286 | // |
| 287 | // First and foremost, we can eliminate the need for any changes when the |
| 288 | // edge is toward the beginning of the postorder sequence because all edges |
| 289 | // flow in that direction already. Thus adding a new one cannot form a cycle. |
| 290 | int SourceIdx = SCCIndices[&SourceSCC]; |
| 291 | int TargetIdx = SCCIndices[&TargetSCC]; |
| 292 | if (TargetIdx < SourceIdx) { |
| 293 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call); |
| 294 | #ifndef NDEBUG |
| 295 | // Check that the RefSCC is still valid. |
| 296 | verify(); |
| 297 | #endif |
| 298 | return DeletedSCCs; |
| 299 | } |
| 300 | |
| 301 | // When we do have an edge from an earlier SCC to a later SCC in the |
| 302 | // postorder sequence, all of the SCCs which may be impacted are in the |
| 303 | // closed range of those two within the postorder sequence. The algorithm to |
| 304 | // restore the state is as follows: |
| 305 | // |
| 306 | // 1) Starting from the source SCC, construct a set of SCCs which reach the |
| 307 | // source SCC consisting of just the source SCC. Then scan toward the |
| 308 | // target SCC in postorder and for each SCC, if it has an edge to an SCC |
| 309 | // in the set, add it to the set. Otherwise, the source SCC is not |
| 310 | // a successor, move it in the postorder sequence to immediately before |
| 311 | // the source SCC, shifting the source SCC and all SCCs in the set one |
| 312 | // position toward the target SCC. Stop scanning after processing the |
| 313 | // target SCC. |
| 314 | // 2) If the source SCC is now past the target SCC in the postorder sequence, |
| 315 | // and thus the new edge will flow toward the start, we are done. |
| 316 | // 3) Otherwise, starting from the target SCC, walk all edges which reach an |
| 317 | // SCC between the source and the target, and add them to the set of |
| 318 | // connected SCCs, then recurse through them. Once a complete set of the |
| 319 | // SCCs the target connects to is known, hoist the remaining SCCs between |
| 320 | // the source and the target to be above the target. Note that there is no |
| 321 | // need to process the source SCC, it is already known to connect. |
| 322 | // 4) At this point, all of the SCCs in the closed range between the source |
| 323 | // SCC and the target SCC in the postorder sequence are connected, |
| 324 | // including the target SCC and the source SCC. Inserting the edge from |
| 325 | // the source SCC to the target SCC will form a cycle out of precisely |
| 326 | // these SCCs. Thus we can merge all of the SCCs in this closed range into |
| 327 | // a single SCC. |
| 328 | // |
| 329 | // This process has various important properties: |
| 330 | // - Only mutates the SCCs when adding the edge actually changes the SCC |
| 331 | // structure. |
| 332 | // - Never mutates SCCs which are unaffected by the change. |
| 333 | // - Updates the postorder sequence to correctly satisfy the postorder |
| 334 | // constraint after the edge is inserted. |
| 335 | // - Only reorders SCCs in the closed postorder sequence from the source to |
| 336 | // the target, so easy to bound how much has changed even in the ordering. |
| 337 | // - Big-O is the number of edges in the closed postorder range of SCCs from |
| 338 | // source to target. |
| 339 | |
| 340 | assert(SourceIdx < TargetIdx && "Cannot have equal indices here!"); |
| 341 | SmallPtrSet<SCC *, 4> ConnectedSet; |
| 342 | |
| 343 | // Compute the SCCs which (transitively) reach the source. |
| 344 | ConnectedSet.insert(&SourceSCC); |
| 345 | auto IsConnected = [&](SCC &C) { |
| 346 | for (Node &N : C) |
| 347 | for (Edge &E : N.calls()) { |
| 348 | assert(E.getNode() && "Must have formed a node within an SCC!"); |
| 349 | if (ConnectedSet.count(G->lookupSCC(*E.getNode()))) |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | return false; |
| 354 | }; |
| 355 | |
| 356 | for (SCC *C : |
| 357 | make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1)) |
| 358 | if (IsConnected(*C)) |
| 359 | ConnectedSet.insert(C); |
| 360 | |
| 361 | // Partition the SCCs in this part of the port-order sequence so only SCCs |
| 362 | // connecting to the source remain between it and the target. This is |
| 363 | // a benign partition as it preserves postorder. |
| 364 | auto SourceI = std::stable_partition( |
| 365 | SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx + 1, |
| 366 | [&ConnectedSet](SCC *C) { return !ConnectedSet.count(C); }); |
| 367 | for (int i = SourceIdx, e = TargetIdx + 1; i < e; ++i) |
| 368 | SCCIndices.find(SCCs[i])->second = i; |
| 369 | |
| 370 | // If the target doesn't connect to the source, then we've corrected the |
| 371 | // post-order and there are no cycles formed. |
| 372 | if (!ConnectedSet.count(&TargetSCC)) { |
| 373 | assert(SourceI > (SCCs.begin() + SourceIdx) && |
| 374 | "Must have moved the source to fix the post-order."); |
| 375 | assert(*std::prev(SourceI) == &TargetSCC && |
| 376 | "Last SCC to move should have bene the target."); |
| 377 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call); |
| 378 | #ifndef NDEBUG |
| 379 | verify(); |
| 380 | #endif |
| 381 | return DeletedSCCs; |
| 382 | } |
| 383 | |
| 384 | assert(SCCs[TargetIdx] == &TargetSCC && |
| 385 | "Should not have moved target if connected!"); |
| 386 | SourceIdx = SourceI - SCCs.begin(); |
| 387 | |
| 388 | #ifndef NDEBUG |
| 389 | // Check that the RefSCC is still valid. |
| 390 | verify(); |
| 391 | #endif |
| 392 | |
| 393 | // See whether there are any remaining intervening SCCs between the source |
| 394 | // and target. If so we need to make sure they all are reachable form the |
| 395 | // target. |
| 396 | if (SourceIdx + 1 < TargetIdx) { |
| 397 | // Use a normal worklist to find which SCCs the target connects to. We still |
| 398 | // bound the search based on the range in the postorder list we care about, |
| 399 | // but because this is forward connectivity we just "recurse" through the |
| 400 | // edges. |
| 401 | ConnectedSet.clear(); |
| 402 | ConnectedSet.insert(&TargetSCC); |
| 403 | SmallVector<SCC *, 4> Worklist; |
| 404 | Worklist.push_back(&TargetSCC); |
| 405 | do { |
| 406 | SCC &C = *Worklist.pop_back_val(); |
| 407 | for (Node &N : C) |
| 408 | for (Edge &E : N) { |
| 409 | assert(E.getNode() && "Must have formed a node within an SCC!"); |
| 410 | if (!E.isCall()) |
| 411 | continue; |
| 412 | SCC &EdgeC = *G->lookupSCC(*E.getNode()); |
| 413 | if (&EdgeC.getOuterRefSCC() != this) |
| 414 | // Not in this RefSCC... |
| 415 | continue; |
| 416 | if (SCCIndices.find(&EdgeC)->second <= SourceIdx) |
| 417 | // Not in the postorder sequence between source and target. |
| 418 | continue; |
| 419 | |
| 420 | if (ConnectedSet.insert(&EdgeC).second) |
| 421 | Worklist.push_back(&EdgeC); |
| 422 | } |
| 423 | } while (!Worklist.empty()); |
| 424 | |
| 425 | // Partition SCCs so that only SCCs reached from the target remain between |
| 426 | // the source and the target. This preserves postorder. |
| 427 | auto TargetI = std::stable_partition( |
| 428 | SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1, |
| 429 | [&ConnectedSet](SCC *C) { return ConnectedSet.count(C); }); |
| 430 | for (int i = SourceIdx + 1, e = TargetIdx + 1; i < e; ++i) |
| 431 | SCCIndices.find(SCCs[i])->second = i; |
| 432 | TargetIdx = std::prev(TargetI) - SCCs.begin(); |
| 433 | assert(SCCs[TargetIdx] == &TargetSCC && |
| 434 | "Should always end with the target!"); |
| 435 | |
| 436 | #ifndef NDEBUG |
| 437 | // Check that the RefSCC is still valid. |
| 438 | verify(); |
| 439 | #endif |
| 440 | } |
| 441 | |
| 442 | // At this point, we know that connecting source to target forms a cycle |
| 443 | // because target connects back to source, and we know that all of the SCCs |
| 444 | // between the source and target in the postorder sequence participate in that |
| 445 | // cycle. This means that we need to merge all of these SCCs into a single |
| 446 | // result SCC. |
| 447 | // |
| 448 | // NB: We merge into the target because all of these functions were already |
| 449 | // reachable from the target, meaning any SCC-wide properties deduced about it |
| 450 | // other than the set of functions within it will not have changed. |
| 451 | auto MergeRange = |
| 452 | make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx); |
| 453 | for (SCC *C : MergeRange) { |
| 454 | assert(C != &TargetSCC && |
| 455 | "We merge *into* the target and shouldn't process it here!"); |
| 456 | SCCIndices.erase(C); |
| 457 | TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end()); |
| 458 | for (Node *N : C->Nodes) |
| 459 | G->SCCMap[N] = &TargetSCC; |
| 460 | C->clear(); |
| 461 | DeletedSCCs.push_back(C); |
| 462 | } |
| 463 | |
| 464 | // Erase the merged SCCs from the list and update the indices of the |
| 465 | // remaining SCCs. |
| 466 | int IndexOffset = MergeRange.end() - MergeRange.begin(); |
| 467 | auto EraseEnd = SCCs.erase(MergeRange.begin(), MergeRange.end()); |
| 468 | for (SCC *C : make_range(EraseEnd, SCCs.end())) |
| 469 | SCCIndices[C] -= IndexOffset; |
| 470 | |
| 471 | // Now that the SCC structure is finalized, flip the kind to call. |
| 472 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call); |
| 473 | |
| 474 | #ifndef NDEBUG |
| 475 | // And we're done! Verify in debug builds that the RefSCC is coherent. |
| 476 | verify(); |
| 477 | #endif |
| 478 | return DeletedSCCs; |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 481 | void LazyCallGraph::RefSCC::switchInternalEdgeToRef(Node &SourceN, |
| 482 | Node &TargetN) { |
| 483 | assert(SourceN[TargetN].isCall() && "Must start with a call edge!"); |
| 484 | |
| 485 | SCC &SourceSCC = *G->lookupSCC(SourceN); |
| 486 | SCC &TargetSCC = *G->lookupSCC(TargetN); |
| 487 | |
| 488 | assert(&SourceSCC.getOuterRefSCC() == this && |
| 489 | "Source must be in this RefSCC."); |
| 490 | assert(&TargetSCC.getOuterRefSCC() == this && |
| 491 | "Target must be in this RefSCC."); |
| 492 | |
| 493 | // Set the edge kind. |
| 494 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref); |
| 495 | |
| 496 | // If this call edge is just connecting two separate SCCs within this RefSCC, |
| 497 | // there is nothing to do. |
| 498 | if (&SourceSCC != &TargetSCC) { |
| 499 | #ifndef NDEBUG |
| 500 | // Check that the RefSCC is still valid. |
| 501 | verify(); |
| 502 | #endif |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | // Otherwise we are removing a call edge from a single SCC. This may break |
| 507 | // the cycle. In order to compute the new set of SCCs, we need to do a small |
| 508 | // DFS over the nodes within the SCC to form any sub-cycles that remain as |
| 509 | // distinct SCCs and compute a postorder over the resulting SCCs. |
| 510 | // |
| 511 | // However, we specially handle the target node. The target node is known to |
| 512 | // reach all other nodes in the original SCC by definition. This means that |
| 513 | // we want the old SCC to be replaced with an SCC contaning that node as it |
| 514 | // will be the root of whatever SCC DAG results from the DFS. Assumptions |
| 515 | // about an SCC such as the set of functions called will continue to hold, |
| 516 | // etc. |
| 517 | |
| 518 | SCC &OldSCC = TargetSCC; |
| 519 | SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack; |
| 520 | SmallVector<Node *, 16> PendingSCCStack; |
| 521 | SmallVector<SCC *, 4> NewSCCs; |
| 522 | |
| 523 | // Prepare the nodes for a fresh DFS. |
| 524 | SmallVector<Node *, 16> Worklist; |
| 525 | Worklist.swap(OldSCC.Nodes); |
| 526 | for (Node *N : Worklist) { |
| 527 | N->DFSNumber = N->LowLink = 0; |
| 528 | G->SCCMap.erase(N); |
| 529 | } |
| 530 | |
| 531 | // Force the target node to be in the old SCC. This also enables us to take |
| 532 | // a very significant short-cut in the standard Tarjan walk to re-form SCCs |
| 533 | // below: whenever we build an edge that reaches the target node, we know |
| 534 | // that the target node eventually connects back to all other nodes in our |
| 535 | // walk. As a consequence, we can detect and handle participants in that |
| 536 | // cycle without walking all the edges that form this connection, and instead |
| 537 | // by relying on the fundamental guarantee coming into this operation (all |
| 538 | // nodes are reachable from the target due to previously forming an SCC). |
| 539 | TargetN.DFSNumber = TargetN.LowLink = -1; |
| 540 | OldSCC.Nodes.push_back(&TargetN); |
| 541 | G->SCCMap[&TargetN] = &OldSCC; |
| 542 | |
| 543 | // Scan down the stack and DFS across the call edges. |
| 544 | for (Node *RootN : Worklist) { |
| 545 | assert(DFSStack.empty() && |
| 546 | "Cannot begin a new root with a non-empty DFS stack!"); |
| 547 | assert(PendingSCCStack.empty() && |
| 548 | "Cannot begin a new root with pending nodes for an SCC!"); |
| 549 | |
| 550 | // Skip any nodes we've already reached in the DFS. |
| 551 | if (RootN->DFSNumber != 0) { |
| 552 | assert(RootN->DFSNumber == -1 && |
| 553 | "Shouldn't have any mid-DFS root nodes!"); |
| 554 | continue; |
| 555 | } |
| 556 | |
| 557 | RootN->DFSNumber = RootN->LowLink = 1; |
| 558 | int NextDFSNumber = 2; |
| 559 | |
| 560 | DFSStack.push_back({RootN, RootN->call_begin()}); |
| 561 | do { |
| 562 | Node *N; |
| 563 | call_edge_iterator I; |
| 564 | std::tie(N, I) = DFSStack.pop_back_val(); |
| 565 | auto E = N->call_end(); |
| 566 | while (I != E) { |
| 567 | Node &ChildN = *I->getNode(); |
| 568 | if (ChildN.DFSNumber == 0) { |
| 569 | // We haven't yet visited this child, so descend, pushing the current |
| 570 | // node onto the stack. |
| 571 | DFSStack.push_back({N, I}); |
| 572 | |
| 573 | assert(!G->SCCMap.count(&ChildN) && |
| 574 | "Found a node with 0 DFS number but already in an SCC!"); |
| 575 | ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++; |
| 576 | N = &ChildN; |
| 577 | I = N->call_begin(); |
| 578 | E = N->call_end(); |
| 579 | continue; |
| 580 | } |
| 581 | |
| 582 | // Check for the child already being part of some component. |
| 583 | if (ChildN.DFSNumber == -1) { |
| 584 | if (G->lookupSCC(ChildN) == &OldSCC) { |
| 585 | // If the child is part of the old SCC, we know that it can reach |
| 586 | // every other node, so we have formed a cycle. Pull the entire DFS |
| 587 | // and pending stacks into it. See the comment above about setting |
| 588 | // up the old SCC for why we do this. |
| 589 | int OldSize = OldSCC.size(); |
| 590 | OldSCC.Nodes.push_back(N); |
| 591 | OldSCC.Nodes.append(PendingSCCStack.begin(), PendingSCCStack.end()); |
| 592 | PendingSCCStack.clear(); |
| 593 | while (!DFSStack.empty()) |
| 594 | OldSCC.Nodes.push_back(DFSStack.pop_back_val().first); |
| 595 | for (Node &N : make_range(OldSCC.begin() + OldSize, OldSCC.end())) { |
| 596 | N.DFSNumber = N.LowLink = -1; |
| 597 | G->SCCMap[&N] = &OldSCC; |
| 598 | } |
| 599 | N = nullptr; |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | // If the child has already been added to some child component, it |
| 604 | // couldn't impact the low-link of this parent because it isn't |
| 605 | // connected, and thus its low-link isn't relevant so skip it. |
| 606 | ++I; |
| 607 | continue; |
| 608 | } |
| 609 | |
| 610 | // Track the lowest linked child as the lowest link for this node. |
| 611 | assert(ChildN.LowLink > 0 && "Must have a positive low-link number!"); |
| 612 | if (ChildN.LowLink < N->LowLink) |
| 613 | N->LowLink = ChildN.LowLink; |
| 614 | |
| 615 | // Move to the next edge. |
| 616 | ++I; |
| 617 | } |
| 618 | if (!N) |
| 619 | // Cleared the DFS early, start another round. |
| 620 | break; |
| 621 | |
| 622 | // We've finished processing N and its descendents, put it on our pending |
| 623 | // SCC stack to eventually get merged into an SCC of nodes. |
| 624 | PendingSCCStack.push_back(N); |
| 625 | |
| 626 | // If this node is linked to some lower entry, continue walking up the |
| 627 | // stack. |
| 628 | if (N->LowLink != N->DFSNumber) |
| 629 | continue; |
| 630 | |
| 631 | // Otherwise, we've completed an SCC. Append it to our post order list of |
| 632 | // SCCs. |
| 633 | int RootDFSNumber = N->DFSNumber; |
| 634 | // Find the range of the node stack by walking down until we pass the |
| 635 | // root DFS number. |
| 636 | auto SCCNodes = make_range( |
| 637 | PendingSCCStack.rbegin(), |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 638 | find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) { |
| 639 | return N->DFSNumber < RootDFSNumber; |
| 640 | })); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 641 | |
| 642 | // Form a new SCC out of these nodes and then clear them off our pending |
| 643 | // stack. |
| 644 | NewSCCs.push_back(G->createSCC(*this, SCCNodes)); |
| 645 | for (Node &N : *NewSCCs.back()) { |
| 646 | N.DFSNumber = N.LowLink = -1; |
| 647 | G->SCCMap[&N] = NewSCCs.back(); |
| 648 | } |
| 649 | PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end()); |
| 650 | } while (!DFSStack.empty()); |
| 651 | } |
| 652 | |
| 653 | // Insert the remaining SCCs before the old one. The old SCC can reach all |
| 654 | // other SCCs we form because it contains the target node of the removed edge |
| 655 | // of the old SCC. This means that we will have edges into all of the new |
| 656 | // SCCs, which means the old one must come last for postorder. |
| 657 | int OldIdx = SCCIndices[&OldSCC]; |
| 658 | SCCs.insert(SCCs.begin() + OldIdx, NewSCCs.begin(), NewSCCs.end()); |
| 659 | |
| 660 | // Update the mapping from SCC* to index to use the new SCC*s, and remove the |
| 661 | // old SCC from the mapping. |
| 662 | for (int Idx = OldIdx, Size = SCCs.size(); Idx < Size; ++Idx) |
| 663 | SCCIndices[SCCs[Idx]] = Idx; |
| 664 | |
| 665 | #ifndef NDEBUG |
| 666 | // We're done. Check the validity on our way out. |
| 667 | verify(); |
| 668 | #endif |
| 669 | } |
| 670 | |
| 671 | void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN, |
| 672 | Node &TargetN) { |
| 673 | assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!"); |
| 674 | |
| 675 | assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC."); |
| 676 | assert(G->lookupRefSCC(TargetN) != this && |
| 677 | "Target must not be in this RefSCC."); |
| 678 | assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) && |
| 679 | "Target must be a descendant of the Source."); |
| 680 | |
| 681 | // Edges between RefSCCs are the same regardless of call or ref, so we can |
| 682 | // just flip the edge here. |
| 683 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call); |
| 684 | |
| 685 | #ifndef NDEBUG |
| 686 | // Check that the RefSCC is still valid. |
| 687 | verify(); |
| 688 | #endif |
| 689 | } |
| 690 | |
| 691 | void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN, |
| 692 | Node &TargetN) { |
| 693 | assert(SourceN[TargetN].isCall() && "Must start with a call edge!"); |
| 694 | |
| 695 | assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC."); |
| 696 | assert(G->lookupRefSCC(TargetN) != this && |
| 697 | "Target must not be in this RefSCC."); |
| 698 | assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) && |
| 699 | "Target must be a descendant of the Source."); |
| 700 | |
| 701 | // Edges between RefSCCs are the same regardless of call or ref, so we can |
| 702 | // just flip the edge here. |
| 703 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref); |
| 704 | |
| 705 | #ifndef NDEBUG |
| 706 | // Check that the RefSCC is still valid. |
| 707 | verify(); |
| 708 | #endif |
| 709 | } |
| 710 | |
| 711 | void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN, |
| 712 | Node &TargetN) { |
| 713 | assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC."); |
| 714 | assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC."); |
| 715 | |
| 716 | SourceN.insertEdgeInternal(TargetN, Edge::Ref); |
| 717 | |
| 718 | #ifndef NDEBUG |
| 719 | // Check that the RefSCC is still valid. |
| 720 | verify(); |
| 721 | #endif |
| 722 | } |
| 723 | |
| 724 | void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN, |
| 725 | Edge::Kind EK) { |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 726 | // First insert it into the caller. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 727 | SourceN.insertEdgeInternal(TargetN, EK); |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 728 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 729 | assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC."); |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 730 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 731 | RefSCC &TargetC = *G->lookupRefSCC(TargetN); |
| 732 | assert(&TargetC != this && "Target must not be in this RefSCC."); |
| 733 | assert(TargetC.isDescendantOf(*this) && |
| 734 | "Target must be a descendant of the Source."); |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 735 | |
Chandler Carruth | 9153911 | 2015-12-28 01:54:20 +0000 | [diff] [blame] | 736 | // The only change required is to add this SCC to the parent set of the |
| 737 | // callee. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 738 | TargetC.Parents.insert(this); |
| 739 | |
| 740 | #ifndef NDEBUG |
| 741 | // Check that the RefSCC is still valid. |
| 742 | verify(); |
| 743 | #endif |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 746 | SmallVector<LazyCallGraph::RefSCC *, 1> |
| 747 | LazyCallGraph::RefSCC::insertIncomingRefEdge(Node &SourceN, Node &TargetN) { |
| 748 | assert(G->lookupRefSCC(TargetN) == this && "Target must be in this SCC."); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 749 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 750 | // We store the RefSCCs found to be connected in postorder so that we can use |
| 751 | // that when merging. We also return this to the caller to allow them to |
| 752 | // invalidate information pertaining to these RefSCCs. |
| 753 | SmallVector<RefSCC *, 1> Connected; |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 754 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 755 | RefSCC &SourceC = *G->lookupRefSCC(SourceN); |
| 756 | assert(&SourceC != this && "Source must not be in this SCC."); |
| 757 | assert(SourceC.isDescendantOf(*this) && |
| 758 | "Source must be a descendant of the Target."); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 759 | |
| 760 | // The algorithm we use for merging SCCs based on the cycle introduced here |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 761 | // is to walk the RefSCC inverted DAG formed by the parent sets. The inverse |
| 762 | // graph has the same cycle properties as the actual DAG of the RefSCCs, and |
| 763 | // when forming RefSCCs lazily by a DFS, the bottom of the graph won't exist |
| 764 | // in many cases which should prune the search space. |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 765 | // |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 766 | // FIXME: We can get this pruning behavior even after the incremental RefSCC |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 767 | // formation by leaving behind (conservative) DFS numberings in the nodes, |
| 768 | // and pruning the search with them. These would need to be cleverly updated |
| 769 | // during the removal of intra-SCC edges, but could be preserved |
| 770 | // conservatively. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 771 | // |
| 772 | // FIXME: This operation currently creates ordering stability problems |
| 773 | // because we don't use stably ordered containers for the parent SCCs. |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 774 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 775 | // The set of RefSCCs that are connected to the parent, and thus will |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 776 | // participate in the merged connected component. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 777 | SmallPtrSet<RefSCC *, 8> ConnectedSet; |
| 778 | ConnectedSet.insert(this); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 779 | |
| 780 | // We build up a DFS stack of the parents chains. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 781 | SmallVector<std::pair<RefSCC *, parent_iterator>, 8> DFSStack; |
| 782 | SmallPtrSet<RefSCC *, 8> Visited; |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 783 | int ConnectedDepth = -1; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 784 | DFSStack.push_back({&SourceC, SourceC.parent_begin()}); |
| 785 | do { |
| 786 | auto DFSPair = DFSStack.pop_back_val(); |
| 787 | RefSCC *C = DFSPair.first; |
| 788 | parent_iterator I = DFSPair.second; |
| 789 | auto E = C->parent_end(); |
| 790 | |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 791 | while (I != E) { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 792 | RefSCC &Parent = *I++; |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 793 | |
| 794 | // If we have already processed this parent SCC, skip it, and remember |
| 795 | // whether it was connected so we don't have to check the rest of the |
| 796 | // stack. This also handles when we reach a child of the 'this' SCC (the |
| 797 | // callee) which terminates the search. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 798 | if (ConnectedSet.count(&Parent)) { |
| 799 | assert(ConnectedDepth < (int)DFSStack.size() && |
| 800 | "Cannot have a connected depth greater than the DFS depth!"); |
| 801 | ConnectedDepth = DFSStack.size(); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 802 | continue; |
| 803 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 804 | if (Visited.count(&Parent)) |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 805 | continue; |
| 806 | |
| 807 | // We fully explore the depth-first space, adding nodes to the connected |
| 808 | // set only as we pop them off, so "recurse" by rotating to the parent. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 809 | DFSStack.push_back({C, I}); |
| 810 | C = &Parent; |
| 811 | I = C->parent_begin(); |
| 812 | E = C->parent_end(); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | // If we've found a connection anywhere below this point on the stack (and |
| 816 | // thus up the parent graph from the caller), the current node needs to be |
| 817 | // added to the connected set now that we've processed all of its parents. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 818 | if ((int)DFSStack.size() == ConnectedDepth) { |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 819 | --ConnectedDepth; // We're finished with this connection. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 820 | bool Inserted = ConnectedSet.insert(C).second; |
| 821 | (void)Inserted; |
| 822 | assert(Inserted && "Cannot insert a refSCC multiple times!"); |
| 823 | Connected.push_back(C); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 824 | } else { |
| 825 | // Otherwise remember that its parents don't ever connect. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 826 | assert(ConnectedDepth < (int)DFSStack.size() && |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 827 | "Cannot have a connected depth greater than the DFS depth!"); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 828 | Visited.insert(C); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 829 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 830 | } while (!DFSStack.empty()); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 831 | |
| 832 | // Now that we have identified all of the SCCs which need to be merged into |
| 833 | // a connected set with the inserted edge, merge all of them into this SCC. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 834 | // We walk the newly connected RefSCCs in the reverse postorder of the parent |
| 835 | // DAG walk above and merge in each of their SCC postorder lists. This |
| 836 | // ensures a merged postorder SCC list. |
| 837 | SmallVector<SCC *, 16> MergedSCCs; |
| 838 | int SCCIndex = 0; |
| 839 | for (RefSCC *C : reverse(Connected)) { |
| 840 | assert(C != this && |
| 841 | "This RefSCC should terminate the DFS without being reached."); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 842 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 843 | // Merge the parents which aren't part of the merge into the our parents. |
| 844 | for (RefSCC *ParentC : C->Parents) |
| 845 | if (!ConnectedSet.count(ParentC)) |
| 846 | Parents.insert(ParentC); |
| 847 | C->Parents.clear(); |
| 848 | |
| 849 | // Walk the inner SCCs to update their up-pointer and walk all the edges to |
| 850 | // update any parent sets. |
| 851 | // FIXME: We should try to find a way to avoid this (rather expensive) edge |
| 852 | // walk by updating the parent sets in some other manner. |
| 853 | for (SCC &InnerC : *C) { |
| 854 | InnerC.OuterRefSCC = this; |
| 855 | SCCIndices[&InnerC] = SCCIndex++; |
| 856 | for (Node &N : InnerC) { |
| 857 | G->SCCMap[&N] = &InnerC; |
| 858 | for (Edge &E : N) { |
| 859 | assert(E.getNode() && |
| 860 | "Cannot have a null node within a visited SCC!"); |
| 861 | RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode()); |
| 862 | if (ConnectedSet.count(&ChildRC)) |
| 863 | continue; |
| 864 | ChildRC.Parents.erase(C); |
| 865 | ChildRC.Parents.insert(this); |
| 866 | } |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 867 | } |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 868 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 869 | |
| 870 | // Now merge in the SCCs. We can actually move here so try to reuse storage |
| 871 | // the first time through. |
| 872 | if (MergedSCCs.empty()) |
| 873 | MergedSCCs = std::move(C->SCCs); |
| 874 | else |
| 875 | MergedSCCs.append(C->SCCs.begin(), C->SCCs.end()); |
| 876 | C->SCCs.clear(); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 877 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 878 | |
| 879 | // Finally append our original SCCs to the merged list and move it into |
| 880 | // place. |
| 881 | for (SCC &InnerC : *this) |
| 882 | SCCIndices[&InnerC] = SCCIndex++; |
| 883 | MergedSCCs.append(SCCs.begin(), SCCs.end()); |
| 884 | SCCs = std::move(MergedSCCs); |
| 885 | |
| 886 | // At this point we have a merged RefSCC with a post-order SCCs list, just |
| 887 | // connect the nodes to form the new edge. |
| 888 | SourceN.insertEdgeInternal(TargetN, Edge::Ref); |
| 889 | |
| 890 | #ifndef NDEBUG |
| 891 | // Check that the RefSCC is still valid. |
| 892 | verify(); |
| 893 | #endif |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 894 | |
| 895 | // We return the list of SCCs which were merged so that callers can |
| 896 | // invalidate any data they have associated with those SCCs. Note that these |
| 897 | // SCCs are no longer in an interesting state (they are totally empty) but |
| 898 | // the pointers will remain stable for the life of the graph itself. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 899 | return Connected; |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 902 | void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) { |
| 903 | assert(G->lookupRefSCC(SourceN) == this && |
| 904 | "The source must be a member of this RefSCC."); |
| 905 | |
| 906 | RefSCC &TargetRC = *G->lookupRefSCC(TargetN); |
| 907 | assert(&TargetRC != this && "The target must not be a member of this RefSCC"); |
| 908 | |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 909 | assert(!is_contained(G->LeafRefSCCs, this) && |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 910 | "Cannot have a leaf RefSCC source."); |
| 911 | |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 912 | // First remove it from the node. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 913 | SourceN.removeEdgeInternal(TargetN.getFunction()); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 914 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 915 | bool HasOtherEdgeToChildRC = false; |
| 916 | bool HasOtherChildRC = false; |
| 917 | for (SCC *InnerC : SCCs) { |
| 918 | for (Node &N : *InnerC) { |
| 919 | for (Edge &E : N) { |
| 920 | assert(E.getNode() && "Cannot have a missing node in a visited SCC!"); |
| 921 | RefSCC &OtherChildRC = *G->lookupRefSCC(*E.getNode()); |
| 922 | if (&OtherChildRC == &TargetRC) { |
| 923 | HasOtherEdgeToChildRC = true; |
| 924 | break; |
| 925 | } |
| 926 | if (&OtherChildRC != this) |
| 927 | HasOtherChildRC = true; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 928 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 929 | if (HasOtherEdgeToChildRC) |
| 930 | break; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 931 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 932 | if (HasOtherEdgeToChildRC) |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 933 | break; |
| 934 | } |
| 935 | // Because the SCCs form a DAG, deleting such an edge cannot change the set |
| 936 | // of SCCs in the graph. However, it may cut an edge of the SCC DAG, making |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 937 | // the source SCC no longer connected to the target SCC. If so, we need to |
| 938 | // update the target SCC's map of its parents. |
| 939 | if (!HasOtherEdgeToChildRC) { |
| 940 | bool Removed = TargetRC.Parents.erase(this); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 941 | (void)Removed; |
| 942 | assert(Removed && |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 943 | "Did not find the source SCC in the target SCC's parent list!"); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 944 | |
| 945 | // It may orphan an SCC if it is the last edge reaching it, but that does |
| 946 | // not violate any invariants of the graph. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 947 | if (TargetRC.Parents.empty()) |
| 948 | DEBUG(dbgs() << "LCG: Update removing " << SourceN.getFunction().getName() |
| 949 | << " -> " << TargetN.getFunction().getName() |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 950 | << " edge orphaned the callee's SCC!\n"); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 951 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 952 | // It may make the Source SCC a leaf SCC. |
| 953 | if (!HasOtherChildRC) |
| 954 | G->LeafRefSCCs.push_back(this); |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 958 | SmallVector<LazyCallGraph::RefSCC *, 1> |
| 959 | LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN, Node &TargetN) { |
| 960 | assert(!SourceN[TargetN].isCall() && |
| 961 | "Cannot remove a call edge, it must first be made a ref edge"); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 962 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 963 | // First remove the actual edge. |
| 964 | SourceN.removeEdgeInternal(TargetN.getFunction()); |
| 965 | |
| 966 | // We return a list of the resulting *new* RefSCCs in post-order. |
| 967 | SmallVector<RefSCC *, 1> Result; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 968 | |
Chandler Carruth | a7205b6 | 2014-04-26 03:36:37 +0000 | [diff] [blame] | 969 | // Direct recursion doesn't impact the SCC graph at all. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 970 | if (&SourceN == &TargetN) |
| 971 | return Result; |
Chandler Carruth | a7205b6 | 2014-04-26 03:36:37 +0000 | [diff] [blame] | 972 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 973 | // We build somewhat synthetic new RefSCCs by providing a postorder mapping |
| 974 | // for each inner SCC. We also store these associated with *nodes* rather |
| 975 | // than SCCs because this saves a round-trip through the node->SCC map and in |
| 976 | // the common case, SCCs are small. We will verify that we always give the |
| 977 | // same number to every node in the SCC such that these are equivalent. |
| 978 | const int RootPostOrderNumber = 0; |
| 979 | int PostOrderNumber = RootPostOrderNumber + 1; |
| 980 | SmallDenseMap<Node *, int> PostOrderMapping; |
| 981 | |
| 982 | // Every node in the target SCC can already reach every node in this RefSCC |
| 983 | // (by definition). It is the only node we know will stay inside this RefSCC. |
| 984 | // Everything which transitively reaches Target will also remain in the |
| 985 | // RefSCC. We handle this by pre-marking that the nodes in the target SCC map |
| 986 | // back to the root post order number. |
| 987 | // |
| 988 | // This also enables us to take a very significant short-cut in the standard |
| 989 | // Tarjan walk to re-form RefSCCs below: whenever we build an edge that |
| 990 | // references the target node, we know that the target node eventually |
| 991 | // references all other nodes in our walk. As a consequence, we can detect |
| 992 | // and handle participants in that cycle without walking all the edges that |
| 993 | // form the connections, and instead by relying on the fundamental guarantee |
| 994 | // coming into this operation. |
| 995 | SCC &TargetC = *G->lookupSCC(TargetN); |
| 996 | for (Node &N : TargetC) |
| 997 | PostOrderMapping[&N] = RootPostOrderNumber; |
| 998 | |
| 999 | // Reset all the other nodes to prepare for a DFS over them, and add them to |
| 1000 | // our worklist. |
| 1001 | SmallVector<Node *, 8> Worklist; |
| 1002 | for (SCC *C : SCCs) { |
| 1003 | if (C == &TargetC) |
| 1004 | continue; |
| 1005 | |
| 1006 | for (Node &N : *C) |
| 1007 | N.DFSNumber = N.LowLink = 0; |
| 1008 | |
| 1009 | Worklist.append(C->Nodes.begin(), C->Nodes.end()); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1012 | auto MarkNodeForSCCNumber = [&PostOrderMapping](Node &N, int Number) { |
| 1013 | N.DFSNumber = N.LowLink = -1; |
| 1014 | PostOrderMapping[&N] = Number; |
| 1015 | }; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1016 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1017 | SmallVector<std::pair<Node *, edge_iterator>, 4> DFSStack; |
| 1018 | SmallVector<Node *, 4> PendingRefSCCStack; |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 1019 | do { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1020 | assert(DFSStack.empty() && |
| 1021 | "Cannot begin a new root with a non-empty DFS stack!"); |
| 1022 | assert(PendingRefSCCStack.empty() && |
| 1023 | "Cannot begin a new root with pending nodes for an SCC!"); |
| 1024 | |
| 1025 | Node *RootN = Worklist.pop_back_val(); |
| 1026 | // Skip any nodes we've already reached in the DFS. |
| 1027 | if (RootN->DFSNumber != 0) { |
| 1028 | assert(RootN->DFSNumber == -1 && |
| 1029 | "Shouldn't have any mid-DFS root nodes!"); |
| 1030 | continue; |
| 1031 | } |
| 1032 | |
| 1033 | RootN->DFSNumber = RootN->LowLink = 1; |
| 1034 | int NextDFSNumber = 2; |
| 1035 | |
| 1036 | DFSStack.push_back({RootN, RootN->begin()}); |
| 1037 | do { |
| 1038 | Node *N; |
| 1039 | edge_iterator I; |
| 1040 | std::tie(N, I) = DFSStack.pop_back_val(); |
| 1041 | auto E = N->end(); |
| 1042 | |
| 1043 | assert(N->DFSNumber != 0 && "We should always assign a DFS number " |
| 1044 | "before processing a node."); |
| 1045 | |
| 1046 | while (I != E) { |
| 1047 | Node &ChildN = I->getNode(*G); |
| 1048 | if (ChildN.DFSNumber == 0) { |
| 1049 | // Mark that we should start at this child when next this node is the |
| 1050 | // top of the stack. We don't start at the next child to ensure this |
| 1051 | // child's lowlink is reflected. |
| 1052 | DFSStack.push_back({N, I}); |
| 1053 | |
| 1054 | // Continue, resetting to the child node. |
| 1055 | ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++; |
| 1056 | N = &ChildN; |
| 1057 | I = ChildN.begin(); |
| 1058 | E = ChildN.end(); |
| 1059 | continue; |
| 1060 | } |
| 1061 | if (ChildN.DFSNumber == -1) { |
| 1062 | // Check if this edge's target node connects to the deleted edge's |
| 1063 | // target node. If so, we know that every node connected will end up |
| 1064 | // in this RefSCC, so collapse the entire current stack into the root |
| 1065 | // slot in our SCC numbering. See above for the motivation of |
| 1066 | // optimizing the target connected nodes in this way. |
| 1067 | auto PostOrderI = PostOrderMapping.find(&ChildN); |
| 1068 | if (PostOrderI != PostOrderMapping.end() && |
| 1069 | PostOrderI->second == RootPostOrderNumber) { |
| 1070 | MarkNodeForSCCNumber(*N, RootPostOrderNumber); |
| 1071 | while (!PendingRefSCCStack.empty()) |
| 1072 | MarkNodeForSCCNumber(*PendingRefSCCStack.pop_back_val(), |
| 1073 | RootPostOrderNumber); |
| 1074 | while (!DFSStack.empty()) |
| 1075 | MarkNodeForSCCNumber(*DFSStack.pop_back_val().first, |
| 1076 | RootPostOrderNumber); |
| 1077 | // Ensure we break all the way out of the enclosing loop. |
| 1078 | N = nullptr; |
| 1079 | break; |
| 1080 | } |
| 1081 | |
| 1082 | // If this child isn't currently in this RefSCC, no need to process |
| 1083 | // it. |
| 1084 | // However, we do need to remove this RefSCC from its RefSCC's parent |
| 1085 | // set. |
| 1086 | RefSCC &ChildRC = *G->lookupRefSCC(ChildN); |
| 1087 | ChildRC.Parents.erase(this); |
| 1088 | ++I; |
| 1089 | continue; |
| 1090 | } |
| 1091 | |
| 1092 | // Track the lowest link of the children, if any are still in the stack. |
| 1093 | // Any child not on the stack will have a LowLink of -1. |
| 1094 | assert(ChildN.LowLink != 0 && |
| 1095 | "Low-link must not be zero with a non-zero DFS number."); |
| 1096 | if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink) |
| 1097 | N->LowLink = ChildN.LowLink; |
| 1098 | ++I; |
| 1099 | } |
| 1100 | if (!N) |
| 1101 | // We short-circuited this node. |
| 1102 | break; |
| 1103 | |
| 1104 | // We've finished processing N and its descendents, put it on our pending |
| 1105 | // stack to eventually get merged into a RefSCC. |
| 1106 | PendingRefSCCStack.push_back(N); |
| 1107 | |
| 1108 | // If this node is linked to some lower entry, continue walking up the |
| 1109 | // stack. |
| 1110 | if (N->LowLink != N->DFSNumber) { |
| 1111 | assert(!DFSStack.empty() && |
| 1112 | "We never found a viable root for a RefSCC to pop off!"); |
| 1113 | continue; |
| 1114 | } |
| 1115 | |
| 1116 | // Otherwise, form a new RefSCC from the top of the pending node stack. |
| 1117 | int RootDFSNumber = N->DFSNumber; |
| 1118 | // Find the range of the node stack by walking down until we pass the |
| 1119 | // root DFS number. |
| 1120 | auto RefSCCNodes = make_range( |
| 1121 | PendingRefSCCStack.rbegin(), |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1122 | find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) { |
| 1123 | return N->DFSNumber < RootDFSNumber; |
| 1124 | })); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1125 | |
| 1126 | // Mark the postorder number for these nodes and clear them off the |
| 1127 | // stack. We'll use the postorder number to pull them into RefSCCs at the |
| 1128 | // end. FIXME: Fuse with the loop above. |
| 1129 | int RefSCCNumber = PostOrderNumber++; |
| 1130 | for (Node *N : RefSCCNodes) |
| 1131 | MarkNodeForSCCNumber(*N, RefSCCNumber); |
| 1132 | |
| 1133 | PendingRefSCCStack.erase(RefSCCNodes.end().base(), |
| 1134 | PendingRefSCCStack.end()); |
| 1135 | } while (!DFSStack.empty()); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1136 | |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 1137 | assert(DFSStack.empty() && "Didn't flush the entire DFS stack!"); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1138 | assert(PendingRefSCCStack.empty() && "Didn't flush all pending nodes!"); |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 1139 | } while (!Worklist.empty()); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1140 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1141 | // We now have a post-order numbering for RefSCCs and a mapping from each |
| 1142 | // node in this RefSCC to its final RefSCC. We create each new RefSCC node |
| 1143 | // (re-using this RefSCC node for the root) and build a radix-sort style map |
| 1144 | // from postorder number to the RefSCC. We then append SCCs to each of these |
| 1145 | // RefSCCs in the order they occured in the original SCCs container. |
| 1146 | for (int i = 1; i < PostOrderNumber; ++i) |
| 1147 | Result.push_back(G->createRefSCC(*G)); |
| 1148 | |
| 1149 | for (SCC *C : SCCs) { |
| 1150 | auto PostOrderI = PostOrderMapping.find(&*C->begin()); |
| 1151 | assert(PostOrderI != PostOrderMapping.end() && |
| 1152 | "Cannot have missing mappings for nodes!"); |
| 1153 | int SCCNumber = PostOrderI->second; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1154 | #ifndef NDEBUG |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1155 | for (Node &N : *C) |
| 1156 | assert(PostOrderMapping.find(&N)->second == SCCNumber && |
| 1157 | "Cannot have different numbers for nodes in the same SCC!"); |
| 1158 | #endif |
| 1159 | if (SCCNumber == 0) |
| 1160 | // The root node is handled separately by removing the SCCs. |
| 1161 | continue; |
| 1162 | |
| 1163 | RefSCC &RC = *Result[SCCNumber - 1]; |
| 1164 | int SCCIndex = RC.SCCs.size(); |
| 1165 | RC.SCCs.push_back(C); |
| 1166 | SCCIndices[C] = SCCIndex; |
| 1167 | C->OuterRefSCC = &RC; |
| 1168 | } |
| 1169 | |
| 1170 | // FIXME: We re-walk the edges in each RefSCC to establish whether it is |
| 1171 | // a leaf and connect it to the rest of the graph's parents lists. This is |
| 1172 | // really wasteful. We should instead do this during the DFS to avoid yet |
| 1173 | // another edge walk. |
| 1174 | for (RefSCC *RC : Result) |
| 1175 | G->connectRefSCC(*RC); |
| 1176 | |
| 1177 | // Now erase all but the root's SCCs. |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1178 | SCCs.erase(remove_if(SCCs, |
| 1179 | [&](SCC *C) { |
| 1180 | return PostOrderMapping.lookup(&*C->begin()) != |
| 1181 | RootPostOrderNumber; |
| 1182 | }), |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1183 | SCCs.end()); |
| 1184 | |
| 1185 | #ifndef NDEBUG |
| 1186 | // Now we need to reconnect the current (root) SCC to the graph. We do this |
| 1187 | // manually because we can special case our leaf handling and detect errors. |
| 1188 | bool IsLeaf = true; |
| 1189 | #endif |
| 1190 | for (SCC *C : SCCs) |
| 1191 | for (Node &N : *C) { |
| 1192 | for (Edge &E : N) { |
| 1193 | assert(E.getNode() && "Cannot have a missing node in a visited SCC!"); |
| 1194 | RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode()); |
| 1195 | if (&ChildRC == this) |
| 1196 | continue; |
| 1197 | ChildRC.Parents.insert(this); |
| 1198 | #ifndef NDEBUG |
| 1199 | IsLeaf = false; |
| 1200 | #endif |
| 1201 | } |
| 1202 | } |
| 1203 | #ifndef NDEBUG |
| 1204 | if (!Result.empty()) |
| 1205 | assert(!IsLeaf && "This SCC cannot be a leaf as we have split out new " |
| 1206 | "SCCs by removing this edge."); |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 1207 | if (none_of(G->LeafRefSCCs, [&](RefSCC *C) { return C == this; })) |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1208 | assert(!IsLeaf && "This SCC cannot be a leaf as it already had child " |
| 1209 | "SCCs before we removed this edge."); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1210 | #endif |
| 1211 | // If this SCC stopped being a leaf through this edge removal, remove it from |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1212 | // the leaf SCC list. Note that this DTRT in the case where this was never |
| 1213 | // a leaf. |
| 1214 | // FIXME: As LeafRefSCCs could be very large, we might want to not walk the |
| 1215 | // entire list if this RefSCC wasn't a leaf before the edge removal. |
| 1216 | if (!Result.empty()) |
| 1217 | G->LeafRefSCCs.erase( |
| 1218 | std::remove(G->LeafRefSCCs.begin(), G->LeafRefSCCs.end(), this), |
| 1219 | G->LeafRefSCCs.end()); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1220 | |
| 1221 | // Return the new list of SCCs. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1222 | return Result; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1225 | void LazyCallGraph::insertEdge(Node &SourceN, Function &Target, Edge::Kind EK) { |
Chandler Carruth | c00a7ff | 2014-04-28 11:10:23 +0000 | [diff] [blame] | 1226 | assert(SCCMap.empty() && DFSStack.empty() && |
| 1227 | "This method cannot be called after SCCs have been formed!"); |
| 1228 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1229 | return SourceN.insertEdgeInternal(Target, EK); |
Chandler Carruth | c00a7ff | 2014-04-28 11:10:23 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1232 | void LazyCallGraph::removeEdge(Node &SourceN, Function &Target) { |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1233 | assert(SCCMap.empty() && DFSStack.empty() && |
| 1234 | "This method cannot be called after SCCs have been formed!"); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1235 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1236 | return SourceN.removeEdgeInternal(Target); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Chandler Carruth | 2a898e0 | 2014-04-23 23:20:36 +0000 | [diff] [blame] | 1239 | LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) { |
| 1240 | return *new (MappedN = BPA.Allocate()) Node(*this, F); |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | void LazyCallGraph::updateGraphPtrs() { |
Chandler Carruth | b60cb31 | 2014-04-17 07:25:59 +0000 | [diff] [blame] | 1244 | // Process all nodes updating the graph pointers. |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1245 | { |
| 1246 | SmallVector<Node *, 16> Worklist; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 1247 | for (Edge &E : EntryEdges) |
| 1248 | if (Node *EntryN = E.getNode()) |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1249 | Worklist.push_back(EntryN); |
Chandler Carruth | b60cb31 | 2014-04-17 07:25:59 +0000 | [diff] [blame] | 1250 | |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1251 | while (!Worklist.empty()) { |
| 1252 | Node *N = Worklist.pop_back_val(); |
| 1253 | N->G = this; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 1254 | for (Edge &E : N->Edges) |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1255 | if (Node *TargetN = E.getNode()) |
| 1256 | Worklist.push_back(TargetN); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | // Process all SCCs updating the graph pointers. |
| 1261 | { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1262 | SmallVector<RefSCC *, 16> Worklist(LeafRefSCCs.begin(), LeafRefSCCs.end()); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1263 | |
| 1264 | while (!Worklist.empty()) { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1265 | RefSCC &C = *Worklist.pop_back_val(); |
| 1266 | C.G = this; |
| 1267 | for (RefSCC &ParentC : C.parents()) |
| 1268 | Worklist.push_back(&ParentC); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1269 | } |
Chandler Carruth | b60cb31 | 2014-04-17 07:25:59 +0000 | [diff] [blame] | 1270 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 1271 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 1272 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1273 | /// Build the internal SCCs for a RefSCC from a sequence of nodes. |
| 1274 | /// |
| 1275 | /// Appends the SCCs to the provided vector and updates the map with their |
| 1276 | /// indices. Both the vector and map must be empty when passed into this |
| 1277 | /// routine. |
| 1278 | void LazyCallGraph::buildSCCs(RefSCC &RC, node_stack_range Nodes) { |
| 1279 | assert(RC.SCCs.empty() && "Already built SCCs!"); |
| 1280 | assert(RC.SCCIndices.empty() && "Already mapped SCC indices!"); |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 1281 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1282 | for (Node *N : Nodes) { |
| 1283 | assert(N->LowLink >= (*Nodes.begin())->LowLink && |
Chandler Carruth | cace662 | 2014-04-23 10:31:17 +0000 | [diff] [blame] | 1284 | "We cannot have a low link in an SCC lower than its root on the " |
| 1285 | "stack!"); |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 1286 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1287 | // This node will go into the next RefSCC, clear out its DFS and low link |
| 1288 | // as we scan. |
| 1289 | N->DFSNumber = N->LowLink = 0; |
| 1290 | } |
| 1291 | |
| 1292 | // Each RefSCC contains a DAG of the call SCCs. To build these, we do |
| 1293 | // a direct walk of the call edges using Tarjan's algorithm. We reuse the |
| 1294 | // internal storage as we won't need it for the outer graph's DFS any longer. |
| 1295 | |
| 1296 | SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack; |
| 1297 | SmallVector<Node *, 16> PendingSCCStack; |
| 1298 | |
| 1299 | // Scan down the stack and DFS across the call edges. |
| 1300 | for (Node *RootN : Nodes) { |
| 1301 | assert(DFSStack.empty() && |
| 1302 | "Cannot begin a new root with a non-empty DFS stack!"); |
| 1303 | assert(PendingSCCStack.empty() && |
| 1304 | "Cannot begin a new root with pending nodes for an SCC!"); |
| 1305 | |
| 1306 | // Skip any nodes we've already reached in the DFS. |
| 1307 | if (RootN->DFSNumber != 0) { |
| 1308 | assert(RootN->DFSNumber == -1 && |
| 1309 | "Shouldn't have any mid-DFS root nodes!"); |
| 1310 | continue; |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1313 | RootN->DFSNumber = RootN->LowLink = 1; |
| 1314 | int NextDFSNumber = 2; |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 1315 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1316 | DFSStack.push_back({RootN, RootN->call_begin()}); |
| 1317 | do { |
| 1318 | Node *N; |
| 1319 | call_edge_iterator I; |
| 1320 | std::tie(N, I) = DFSStack.pop_back_val(); |
| 1321 | auto E = N->call_end(); |
| 1322 | while (I != E) { |
| 1323 | Node &ChildN = *I->getNode(); |
| 1324 | if (ChildN.DFSNumber == 0) { |
| 1325 | // We haven't yet visited this child, so descend, pushing the current |
| 1326 | // node onto the stack. |
| 1327 | DFSStack.push_back({N, I}); |
| 1328 | |
| 1329 | assert(!lookupSCC(ChildN) && |
| 1330 | "Found a node with 0 DFS number but already in an SCC!"); |
| 1331 | ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++; |
| 1332 | N = &ChildN; |
| 1333 | I = N->call_begin(); |
| 1334 | E = N->call_end(); |
| 1335 | continue; |
| 1336 | } |
| 1337 | |
| 1338 | // If the child has already been added to some child component, it |
| 1339 | // couldn't impact the low-link of this parent because it isn't |
| 1340 | // connected, and thus its low-link isn't relevant so skip it. |
| 1341 | if (ChildN.DFSNumber == -1) { |
| 1342 | ++I; |
| 1343 | continue; |
| 1344 | } |
| 1345 | |
| 1346 | // Track the lowest linked child as the lowest link for this node. |
| 1347 | assert(ChildN.LowLink > 0 && "Must have a positive low-link number!"); |
| 1348 | if (ChildN.LowLink < N->LowLink) |
| 1349 | N->LowLink = ChildN.LowLink; |
| 1350 | |
| 1351 | // Move to the next edge. |
| 1352 | ++I; |
| 1353 | } |
| 1354 | |
| 1355 | // We've finished processing N and its descendents, put it on our pending |
| 1356 | // SCC stack to eventually get merged into an SCC of nodes. |
| 1357 | PendingSCCStack.push_back(N); |
| 1358 | |
| 1359 | // If this node is linked to some lower entry, continue walking up the |
| 1360 | // stack. |
| 1361 | if (N->LowLink != N->DFSNumber) |
| 1362 | continue; |
| 1363 | |
| 1364 | // Otherwise, we've completed an SCC. Append it to our post order list of |
| 1365 | // SCCs. |
| 1366 | int RootDFSNumber = N->DFSNumber; |
| 1367 | // Find the range of the node stack by walking down until we pass the |
| 1368 | // root DFS number. |
| 1369 | auto SCCNodes = make_range( |
| 1370 | PendingSCCStack.rbegin(), |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1371 | find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) { |
| 1372 | return N->DFSNumber < RootDFSNumber; |
| 1373 | })); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1374 | // Form a new SCC out of these nodes and then clear them off our pending |
| 1375 | // stack. |
| 1376 | RC.SCCs.push_back(createSCC(RC, SCCNodes)); |
| 1377 | for (Node &N : *RC.SCCs.back()) { |
| 1378 | N.DFSNumber = N.LowLink = -1; |
| 1379 | SCCMap[&N] = RC.SCCs.back(); |
| 1380 | } |
| 1381 | PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end()); |
| 1382 | } while (!DFSStack.empty()); |
| 1383 | } |
| 1384 | |
| 1385 | // Wire up the SCC indices. |
| 1386 | for (int i = 0, Size = RC.SCCs.size(); i < Size; ++i) |
| 1387 | RC.SCCIndices[RC.SCCs[i]] = i; |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1390 | // FIXME: We should move callers of this to embed the parent linking and leaf |
| 1391 | // tracking into their DFS in order to remove a full walk of all edges. |
| 1392 | void LazyCallGraph::connectRefSCC(RefSCC &RC) { |
| 1393 | // Walk all edges in the RefSCC (this remains linear as we only do this once |
| 1394 | // when we build the RefSCC) to connect it to the parent sets of its |
| 1395 | // children. |
| 1396 | bool IsLeaf = true; |
| 1397 | for (SCC &C : RC) |
| 1398 | for (Node &N : C) |
| 1399 | for (Edge &E : N) { |
| 1400 | assert(E.getNode() && |
| 1401 | "Cannot have a missing node in a visited part of the graph!"); |
| 1402 | RefSCC &ChildRC = *lookupRefSCC(*E.getNode()); |
| 1403 | if (&ChildRC == &RC) |
| 1404 | continue; |
| 1405 | ChildRC.Parents.insert(&RC); |
| 1406 | IsLeaf = false; |
| 1407 | } |
| 1408 | |
| 1409 | // For the SCCs where we fine no child SCCs, add them to the leaf list. |
| 1410 | if (IsLeaf) |
| 1411 | LeafRefSCCs.push_back(&RC); |
| 1412 | } |
| 1413 | |
| 1414 | LazyCallGraph::RefSCC *LazyCallGraph::getNextRefSCCInPostOrder() { |
| 1415 | if (DFSStack.empty()) { |
| 1416 | Node *N; |
Chandler Carruth | 90821c2 | 2014-04-26 09:45:55 +0000 | [diff] [blame] | 1417 | do { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1418 | // If we've handled all candidate entry nodes to the SCC forest, we're |
| 1419 | // done. |
| 1420 | if (RefSCCEntryNodes.empty()) |
Chandler Carruth | 90821c2 | 2014-04-26 09:45:55 +0000 | [diff] [blame] | 1421 | return nullptr; |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1422 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1423 | N = &get(*RefSCCEntryNodes.pop_back_val()); |
Chandler Carruth | 90821c2 | 2014-04-26 09:45:55 +0000 | [diff] [blame] | 1424 | } while (N->DFSNumber != 0); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1425 | |
| 1426 | // Found a new root, begin the DFS here. |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1427 | N->LowLink = N->DFSNumber = 1; |
Chandler Carruth | 09751bf | 2014-04-24 09:59:59 +0000 | [diff] [blame] | 1428 | NextDFSNumber = 2; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1429 | DFSStack.push_back({N, N->begin()}); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Chandler Carruth | 91dcf0f | 2014-04-24 21:19:30 +0000 | [diff] [blame] | 1432 | for (;;) { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1433 | Node *N; |
| 1434 | edge_iterator I; |
| 1435 | std::tie(N, I) = DFSStack.pop_back_val(); |
| 1436 | |
| 1437 | assert(N->DFSNumber > 0 && "We should always assign a DFS number " |
| 1438 | "before placing a node onto the stack."); |
Chandler Carruth | 2455393 | 2014-04-24 11:05:20 +0000 | [diff] [blame] | 1439 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 1440 | auto E = N->end(); |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1441 | while (I != E) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 1442 | Node &ChildN = I->getNode(*this); |
Chandler Carruth | bd5d308 | 2014-04-23 23:34:48 +0000 | [diff] [blame] | 1443 | if (ChildN.DFSNumber == 0) { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1444 | // We haven't yet visited this child, so descend, pushing the current |
| 1445 | // node onto the stack. |
| 1446 | DFSStack.push_back({N, N->begin()}); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1447 | |
Chandler Carruth | 09751bf | 2014-04-24 09:59:59 +0000 | [diff] [blame] | 1448 | assert(!SCCMap.count(&ChildN) && |
| 1449 | "Found a node with 0 DFS number but already in an SCC!"); |
| 1450 | ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++; |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1451 | N = &ChildN; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1452 | I = N->begin(); |
| 1453 | E = N->end(); |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1454 | continue; |
Chandler Carruth | cace662 | 2014-04-23 10:31:17 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1457 | // If the child has already been added to some child component, it |
| 1458 | // couldn't impact the low-link of this parent because it isn't |
| 1459 | // connected, and thus its low-link isn't relevant so skip it. |
| 1460 | if (ChildN.DFSNumber == -1) { |
| 1461 | ++I; |
| 1462 | continue; |
| 1463 | } |
| 1464 | |
| 1465 | // Track the lowest linked child as the lowest link for this node. |
| 1466 | assert(ChildN.LowLink > 0 && "Must have a positive low-link number!"); |
| 1467 | if (ChildN.LowLink < N->LowLink) |
Chandler Carruth | bd5d308 | 2014-04-23 23:34:48 +0000 | [diff] [blame] | 1468 | N->LowLink = ChildN.LowLink; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1469 | |
| 1470 | // Move to the next edge. |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1471 | ++I; |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1474 | // We've finished processing N and its descendents, put it on our pending |
| 1475 | // SCC stack to eventually get merged into an SCC of nodes. |
| 1476 | PendingRefSCCStack.push_back(N); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1477 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1478 | // If this node is linked to some lower entry, continue walking up the |
| 1479 | // stack. |
| 1480 | if (N->LowLink != N->DFSNumber) { |
| 1481 | assert(!DFSStack.empty() && |
| 1482 | "We never found a viable root for an SCC to pop off!"); |
| 1483 | continue; |
| 1484 | } |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1485 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1486 | // Otherwise, form a new RefSCC from the top of the pending node stack. |
| 1487 | int RootDFSNumber = N->DFSNumber; |
| 1488 | // Find the range of the node stack by walking down until we pass the |
| 1489 | // root DFS number. |
| 1490 | auto RefSCCNodes = node_stack_range( |
| 1491 | PendingRefSCCStack.rbegin(), |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1492 | find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) { |
| 1493 | return N->DFSNumber < RootDFSNumber; |
| 1494 | })); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1495 | // Form a new RefSCC out of these nodes and then clear them off our pending |
| 1496 | // stack. |
| 1497 | RefSCC *NewRC = createRefSCC(*this); |
| 1498 | buildSCCs(*NewRC, RefSCCNodes); |
| 1499 | connectRefSCC(*NewRC); |
| 1500 | PendingRefSCCStack.erase(RefSCCNodes.end().base(), |
| 1501 | PendingRefSCCStack.end()); |
| 1502 | |
| 1503 | // We return the new node here. This essentially suspends the DFS walk |
| 1504 | // until another RefSCC is requested. |
| 1505 | return NewRC; |
Chandler Carruth | 91dcf0f | 2014-04-24 21:19:30 +0000 | [diff] [blame] | 1506 | } |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 1509 | char LazyCallGraphAnalysis::PassID; |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 1510 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 1511 | LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {} |
| 1512 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1513 | static void printNode(raw_ostream &OS, LazyCallGraph::Node &N) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 1514 | OS << " Edges in function: " << N.getFunction().getName() << "\n"; |
| 1515 | for (const LazyCallGraph::Edge &E : N) |
| 1516 | OS << " " << (E.isCall() ? "call" : "ref ") << " -> " |
| 1517 | << E.getFunction().getName() << "\n"; |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 1518 | |
| 1519 | OS << "\n"; |
| 1520 | } |
| 1521 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1522 | static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) { |
| 1523 | ptrdiff_t Size = std::distance(C.begin(), C.end()); |
| 1524 | OS << " SCC with " << Size << " functions:\n"; |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 1525 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1526 | for (LazyCallGraph::Node &N : C) |
| 1527 | OS << " " << N.getFunction().getName() << "\n"; |
| 1528 | } |
| 1529 | |
| 1530 | static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) { |
| 1531 | ptrdiff_t Size = std::distance(C.begin(), C.end()); |
| 1532 | OS << " RefSCC with " << Size << " call SCCs:\n"; |
| 1533 | |
| 1534 | for (LazyCallGraph::SCC &InnerC : C) |
| 1535 | printSCC(OS, InnerC); |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 1536 | |
| 1537 | OS << "\n"; |
| 1538 | } |
| 1539 | |
Chandler Carruth | d174ce4 | 2015-01-05 02:47:05 +0000 | [diff] [blame] | 1540 | PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M, |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 1541 | ModuleAnalysisManager &AM) { |
| 1542 | LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M); |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 1543 | |
| 1544 | OS << "Printing the call graph for module: " << M.getModuleIdentifier() |
| 1545 | << "\n\n"; |
| 1546 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1547 | for (Function &F : M) |
| 1548 | printNode(OS, G.get(F)); |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 1549 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1550 | for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs()) |
| 1551 | printRefSCC(OS, C); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1552 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 1553 | return PreservedAnalyses::all(); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 1554 | } |
Sean Silva | 7cb3066 | 2016-06-18 09:17:32 +0000 | [diff] [blame] | 1555 | |
| 1556 | LazyCallGraphDOTPrinterPass::LazyCallGraphDOTPrinterPass(raw_ostream &OS) |
| 1557 | : OS(OS) {} |
| 1558 | |
| 1559 | static void printNodeDOT(raw_ostream &OS, LazyCallGraph::Node &N) { |
| 1560 | std::string Name = "\"" + DOT::EscapeString(N.getFunction().getName()) + "\""; |
| 1561 | |
| 1562 | for (const LazyCallGraph::Edge &E : N) { |
| 1563 | OS << " " << Name << " -> \"" |
| 1564 | << DOT::EscapeString(E.getFunction().getName()) << "\""; |
| 1565 | if (!E.isCall()) // It is a ref edge. |
| 1566 | OS << " [style=dashed,label=\"ref\"]"; |
| 1567 | OS << ";\n"; |
| 1568 | } |
| 1569 | |
| 1570 | OS << "\n"; |
| 1571 | } |
| 1572 | |
| 1573 | PreservedAnalyses LazyCallGraphDOTPrinterPass::run(Module &M, |
| 1574 | ModuleAnalysisManager &AM) { |
| 1575 | LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M); |
| 1576 | |
| 1577 | OS << "digraph \"" << DOT::EscapeString(M.getModuleIdentifier()) << "\" {\n"; |
| 1578 | |
| 1579 | for (Function &F : M) |
| 1580 | printNodeDOT(OS, G.get(F)); |
| 1581 | |
| 1582 | OS << "}\n"; |
| 1583 | |
| 1584 | return PreservedAnalyses::all(); |
| 1585 | } |