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 | 11b3f60 | 2016-09-04 08:34:31 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/ScopeExit.h" |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/Sequence.h" |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 86f0bdf | 2016-12-09 00:46:44 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/ScopeExit.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 15 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 16 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Instructions.h" |
| 18 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Sean Silva | 7cb3066 | 2016-06-18 09:17:32 +0000 | [diff] [blame] | 20 | #include "llvm/Support/GraphWriter.h" |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "lcg" |
| 25 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 26 | static void addEdge(SmallVectorImpl<LazyCallGraph::Edge> &Edges, |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 27 | DenseMap<Function *, int> &EdgeIndexMap, Function &F, |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 28 | LazyCallGraph::Edge::Kind EK) { |
Chandler Carruth | 86f0bdf | 2016-12-09 00:46:44 +0000 | [diff] [blame] | 29 | if (!EdgeIndexMap.insert({&F, Edges.size()}).second) |
| 30 | return; |
| 31 | |
| 32 | DEBUG(dbgs() << " Added callable function: " << F.getName() << "\n"); |
| 33 | Edges.emplace_back(LazyCallGraph::Edge(F, EK)); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 36 | LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F) |
| 37 | : G(&G), F(F), DFSNumber(0), LowLink(0) { |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 38 | DEBUG(dbgs() << " Adding functions called by '" << F.getName() |
| 39 | << "' to the graph.\n"); |
| 40 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 41 | SmallVector<Constant *, 16> Worklist; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 42 | SmallPtrSet<Function *, 4> Callees; |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 43 | SmallPtrSet<Constant *, 16> Visited; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 44 | |
| 45 | // Find all the potential call graph edges in this function. We track both |
| 46 | // actual call edges and indirect references to functions. The direct calls |
| 47 | // are trivially added, but to accumulate the latter we walk the instructions |
| 48 | // and add every operand which is a constant to the worklist to process |
| 49 | // afterward. |
Chandler Carruth | 86f0bdf | 2016-12-09 00:46:44 +0000 | [diff] [blame] | 50 | // |
| 51 | // Note that we consider *any* function with a definition to be a viable |
| 52 | // edge. Even if the function's definition is subject to replacement by |
| 53 | // some other module (say, a weak definition) there may still be |
| 54 | // optimizations which essentially speculate based on the definition and |
| 55 | // a way to check that the specific definition is in fact the one being |
| 56 | // used. For example, this could be done by moving the weak definition to |
| 57 | // a strong (internal) definition and making the weak definition be an |
| 58 | // alias. Then a test of the address of the weak function against the new |
| 59 | // strong definition's address would be an effective way to determine the |
| 60 | // safety of optimizing a direct call edge. |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 61 | for (BasicBlock &BB : F) |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 62 | for (Instruction &I : BB) { |
| 63 | if (auto CS = CallSite(&I)) |
| 64 | if (Function *Callee = CS.getCalledFunction()) |
Chandler Carruth | 86f0bdf | 2016-12-09 00:46:44 +0000 | [diff] [blame] | 65 | if (!Callee->isDeclaration()) |
| 66 | if (Callees.insert(Callee).second) { |
| 67 | Visited.insert(Callee); |
| 68 | addEdge(Edges, EdgeIndexMap, *Callee, LazyCallGraph::Edge::Call); |
| 69 | } |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 70 | |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 71 | for (Value *Op : I.operand_values()) |
Chandler Carruth | 1583e99 | 2014-03-03 10:42:58 +0000 | [diff] [blame] | 72 | if (Constant *C = dyn_cast<Constant>(Op)) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 73 | if (Visited.insert(C).second) |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 74 | Worklist.push_back(C); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 75 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 76 | |
| 77 | // We've collected all the constant (and thus potentially function or |
| 78 | // function containing) operands to all of the instructions in the function. |
| 79 | // Process them (recursively) collecting every function found. |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 80 | visitReferences(Worklist, Visited, [&](Function &F) { |
| 81 | addEdge(Edges, EdgeIndexMap, F, LazyCallGraph::Edge::Ref); |
| 82 | }); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 85 | void LazyCallGraph::Node::insertEdgeInternal(Function &Target, Edge::Kind EK) { |
| 86 | if (Node *N = G->lookup(Target)) |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 87 | return insertEdgeInternal(*N, EK); |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 88 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 89 | EdgeIndexMap.insert({&Target, Edges.size()}); |
| 90 | Edges.emplace_back(Target, EK); |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 93 | void LazyCallGraph::Node::insertEdgeInternal(Node &TargetN, Edge::Kind EK) { |
| 94 | EdgeIndexMap.insert({&TargetN.getFunction(), Edges.size()}); |
| 95 | Edges.emplace_back(TargetN, EK); |
Chandler Carruth | c00a7ff | 2014-04-28 11:10:23 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 98 | void LazyCallGraph::Node::setEdgeKind(Function &TargetF, Edge::Kind EK) { |
| 99 | Edges[EdgeIndexMap.find(&TargetF)->second].setKind(EK); |
| 100 | } |
| 101 | |
| 102 | void LazyCallGraph::Node::removeEdgeInternal(Function &Target) { |
| 103 | auto IndexMapI = EdgeIndexMap.find(&Target); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 104 | assert(IndexMapI != EdgeIndexMap.end() && |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 105 | "Target not in the edge set for this caller?"); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 106 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 107 | Edges[IndexMapI->second] = Edge(); |
| 108 | EdgeIndexMap.erase(IndexMapI); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Chandler Carruth | dca8340 | 2016-06-27 23:26:08 +0000 | [diff] [blame] | 111 | void LazyCallGraph::Node::dump() const { |
| 112 | dbgs() << *this << '\n'; |
| 113 | } |
| 114 | |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 115 | LazyCallGraph::LazyCallGraph(Module &M) : NextDFSNumber(0) { |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 116 | DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier() |
| 117 | << "\n"); |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 118 | for (Function &F : M) |
| 119 | if (!F.isDeclaration() && !F.hasLocalLinkage()) |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 120 | if (EntryIndexMap.insert({&F, EntryEdges.size()}).second) { |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 121 | DEBUG(dbgs() << " Adding '" << F.getName() |
| 122 | << "' to entry set of the graph.\n"); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 123 | EntryEdges.emplace_back(F, Edge::Ref); |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 124 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 125 | |
| 126 | // Now add entry nodes for functions reachable via initializers to globals. |
| 127 | SmallVector<Constant *, 16> Worklist; |
| 128 | SmallPtrSet<Constant *, 16> Visited; |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 129 | for (GlobalVariable &GV : M.globals()) |
| 130 | if (GV.hasInitializer()) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 131 | if (Visited.insert(GV.getInitializer()).second) |
Chandler Carruth | b9e2f8c | 2014-03-09 12:20:34 +0000 | [diff] [blame] | 132 | Worklist.push_back(GV.getInitializer()); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 133 | |
Chandler Carruth | 99b756d | 2014-04-21 05:04:24 +0000 | [diff] [blame] | 134 | DEBUG(dbgs() << " Adding functions referenced by global initializers to the " |
| 135 | "entry set.\n"); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 136 | visitReferences(Worklist, Visited, [&](Function &F) { |
| 137 | addEdge(EntryEdges, EntryIndexMap, F, LazyCallGraph::Edge::Ref); |
| 138 | }); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 139 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 140 | for (const Edge &E : EntryEdges) |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 141 | RefSCCEntryNodes.push_back(&E.getFunction()); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 144 | LazyCallGraph::LazyCallGraph(LazyCallGraph &&G) |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 145 | : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)), |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 146 | EntryEdges(std::move(G.EntryEdges)), |
Chandler Carruth | 0b623ba | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 147 | EntryIndexMap(std::move(G.EntryIndexMap)), SCCBPA(std::move(G.SCCBPA)), |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 148 | SCCMap(std::move(G.SCCMap)), LeafRefSCCs(std::move(G.LeafRefSCCs)), |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 149 | DFSStack(std::move(G.DFSStack)), |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 150 | RefSCCEntryNodes(std::move(G.RefSCCEntryNodes)), |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 151 | NextDFSNumber(G.NextDFSNumber) { |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 152 | updateGraphPtrs(); |
| 153 | } |
| 154 | |
| 155 | LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) { |
| 156 | BPA = std::move(G.BPA); |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 157 | NodeMap = std::move(G.NodeMap); |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 158 | EntryEdges = std::move(G.EntryEdges); |
Chandler Carruth | 0b623ba | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 159 | EntryIndexMap = std::move(G.EntryIndexMap); |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 160 | SCCBPA = std::move(G.SCCBPA); |
| 161 | SCCMap = std::move(G.SCCMap); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 162 | LeafRefSCCs = std::move(G.LeafRefSCCs); |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 163 | DFSStack = std::move(G.DFSStack); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 164 | RefSCCEntryNodes = std::move(G.RefSCCEntryNodes); |
Chandler Carruth | 2174f44 | 2014-04-18 20:44:16 +0000 | [diff] [blame] | 165 | NextDFSNumber = G.NextDFSNumber; |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 166 | updateGraphPtrs(); |
| 167 | return *this; |
| 168 | } |
| 169 | |
Chandler Carruth | dca8340 | 2016-06-27 23:26:08 +0000 | [diff] [blame] | 170 | void LazyCallGraph::SCC::dump() const { |
| 171 | dbgs() << *this << '\n'; |
| 172 | } |
| 173 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 174 | #ifndef NDEBUG |
| 175 | void LazyCallGraph::SCC::verify() { |
| 176 | assert(OuterRefSCC && "Can't have a null RefSCC!"); |
| 177 | assert(!Nodes.empty() && "Can't have an empty SCC!"); |
Chandler Carruth | 8f92d6d | 2014-04-26 01:03:46 +0000 | [diff] [blame] | 178 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 179 | for (Node *N : Nodes) { |
| 180 | assert(N && "Can't have a null node!"); |
| 181 | assert(OuterRefSCC->G->lookupSCC(*N) == this && |
| 182 | "Node does not map to this SCC!"); |
| 183 | assert(N->DFSNumber == -1 && |
| 184 | "Must set DFS numbers to -1 when adding a node to an SCC!"); |
| 185 | assert(N->LowLink == -1 && |
| 186 | "Must set low link to -1 when adding a node to an SCC!"); |
| 187 | for (Edge &E : *N) |
| 188 | assert(E.getNode() && "Can't have an edge to a raw function!"); |
| 189 | } |
| 190 | } |
| 191 | #endif |
| 192 | |
Chandler Carruth | bae595b | 2016-11-22 19:23:31 +0000 | [diff] [blame] | 193 | bool LazyCallGraph::SCC::isParentOf(const SCC &C) const { |
| 194 | if (this == &C) |
| 195 | return false; |
| 196 | |
| 197 | for (Node &N : *this) |
| 198 | for (Edge &E : N.calls()) |
| 199 | if (Node *CalleeN = E.getNode()) |
| 200 | if (OuterRefSCC->G->lookupSCC(*CalleeN) == &C) |
| 201 | return true; |
| 202 | |
| 203 | // No edges found. |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | bool LazyCallGraph::SCC::isAncestorOf(const SCC &TargetC) const { |
| 208 | if (this == &TargetC) |
| 209 | return false; |
| 210 | |
| 211 | LazyCallGraph &G = *OuterRefSCC->G; |
| 212 | |
| 213 | // Start with this SCC. |
| 214 | SmallPtrSet<const SCC *, 16> Visited = {this}; |
| 215 | SmallVector<const SCC *, 16> Worklist = {this}; |
| 216 | |
| 217 | // Walk down the graph until we run out of edges or find a path to TargetC. |
| 218 | do { |
| 219 | const SCC &C = *Worklist.pop_back_val(); |
| 220 | for (Node &N : C) |
| 221 | for (Edge &E : N.calls()) { |
| 222 | Node *CalleeN = E.getNode(); |
| 223 | if (!CalleeN) |
| 224 | continue; |
| 225 | SCC *CalleeC = G.lookupSCC(*CalleeN); |
| 226 | if (!CalleeC) |
| 227 | continue; |
| 228 | |
| 229 | // If the callee's SCC is the TargetC, we're done. |
| 230 | if (CalleeC == &TargetC) |
| 231 | return true; |
| 232 | |
| 233 | // If this is the first time we've reached this SCC, put it on the |
| 234 | // worklist to recurse through. |
| 235 | if (Visited.insert(CalleeC).second) |
| 236 | Worklist.push_back(CalleeC); |
| 237 | } |
| 238 | } while (!Worklist.empty()); |
| 239 | |
| 240 | // No paths found. |
| 241 | return false; |
| 242 | } |
| 243 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 244 | LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {} |
| 245 | |
Chandler Carruth | dca8340 | 2016-06-27 23:26:08 +0000 | [diff] [blame] | 246 | void LazyCallGraph::RefSCC::dump() const { |
| 247 | dbgs() << *this << '\n'; |
| 248 | } |
| 249 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 250 | #ifndef NDEBUG |
| 251 | void LazyCallGraph::RefSCC::verify() { |
| 252 | assert(G && "Can't have a null graph!"); |
| 253 | assert(!SCCs.empty() && "Can't have an empty SCC!"); |
| 254 | |
| 255 | // Verify basic properties of the SCCs. |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 256 | SmallPtrSet<SCC *, 4> SCCSet; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 257 | for (SCC *C : SCCs) { |
| 258 | assert(C && "Can't have a null SCC!"); |
| 259 | C->verify(); |
| 260 | assert(&C->getOuterRefSCC() == this && |
| 261 | "SCC doesn't think it is inside this RefSCC!"); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 262 | bool Inserted = SCCSet.insert(C).second; |
| 263 | assert(Inserted && "Found a duplicate SCC!"); |
Chandler Carruth | 23a6c3f | 2016-12-06 10:29:23 +0000 | [diff] [blame] | 264 | auto IndexIt = SCCIndices.find(C); |
| 265 | assert(IndexIt != SCCIndices.end() && |
| 266 | "Found an SCC that doesn't have an index!"); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Check that our indices map correctly. |
| 270 | for (auto &SCCIndexPair : SCCIndices) { |
| 271 | SCC *C = SCCIndexPair.first; |
| 272 | int i = SCCIndexPair.second; |
| 273 | assert(C && "Can't have a null SCC in the indices!"); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 274 | assert(SCCSet.count(C) && "Found an index for an SCC not in the RefSCC!"); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 275 | assert(SCCs[i] == C && "Index doesn't point to SCC!"); |
| 276 | } |
| 277 | |
| 278 | // Check that the SCCs are in fact in post-order. |
| 279 | for (int i = 0, Size = SCCs.size(); i < Size; ++i) { |
| 280 | SCC &SourceSCC = *SCCs[i]; |
| 281 | for (Node &N : SourceSCC) |
| 282 | for (Edge &E : N) { |
| 283 | if (!E.isCall()) |
| 284 | continue; |
| 285 | SCC &TargetSCC = *G->lookupSCC(*E.getNode()); |
| 286 | if (&TargetSCC.getOuterRefSCC() == this) { |
| 287 | assert(SCCIndices.find(&TargetSCC)->second <= i && |
| 288 | "Edge between SCCs violates post-order relationship."); |
| 289 | continue; |
| 290 | } |
| 291 | assert(TargetSCC.getOuterRefSCC().Parents.count(this) && |
| 292 | "Edge to a RefSCC missing us in its parent set."); |
| 293 | } |
| 294 | } |
Chandler Carruth | 5205c35 | 2016-12-07 01:42:40 +0000 | [diff] [blame] | 295 | |
| 296 | // Check that our parents are actually parents. |
| 297 | for (RefSCC *ParentRC : Parents) { |
| 298 | assert(ParentRC != this && "Cannot be our own parent!"); |
| 299 | auto HasConnectingEdge = [&] { |
| 300 | for (SCC &C : *ParentRC) |
| 301 | for (Node &N : C) |
| 302 | for (Edge &E : N) |
| 303 | if (G->lookupRefSCC(*E.getNode()) == this) |
| 304 | return true; |
| 305 | return false; |
| 306 | }; |
| 307 | assert(HasConnectingEdge() && "No edge connects the parent to us!"); |
| 308 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 309 | } |
| 310 | #endif |
| 311 | |
| 312 | bool LazyCallGraph::RefSCC::isDescendantOf(const RefSCC &C) const { |
Chandler Carruth | 4b09674 | 2014-05-01 12:12:42 +0000 | [diff] [blame] | 313 | // 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] | 314 | SmallVector<const RefSCC *, 4> AncestorWorklist; |
Chandler Carruth | 4b09674 | 2014-05-01 12:12:42 +0000 | [diff] [blame] | 315 | AncestorWorklist.push_back(this); |
| 316 | do { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 317 | const RefSCC *AncestorC = AncestorWorklist.pop_back_val(); |
Chandler Carruth | 4b09674 | 2014-05-01 12:12:42 +0000 | [diff] [blame] | 318 | if (AncestorC->isChildOf(C)) |
| 319 | return true; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 320 | for (const RefSCC *ParentC : AncestorC->Parents) |
Chandler Carruth | 4b09674 | 2014-05-01 12:12:42 +0000 | [diff] [blame] | 321 | AncestorWorklist.push_back(ParentC); |
| 322 | } while (!AncestorWorklist.empty()); |
| 323 | |
| 324 | return false; |
| 325 | } |
| 326 | |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 327 | /// Generic helper that updates a postorder sequence of SCCs for a potentially |
| 328 | /// cycle-introducing edge insertion. |
| 329 | /// |
| 330 | /// A postorder sequence of SCCs of a directed graph has one fundamental |
| 331 | /// property: all deges in the DAG of SCCs point "up" the sequence. That is, |
| 332 | /// all edges in the SCC DAG point to prior SCCs in the sequence. |
| 333 | /// |
| 334 | /// This routine both updates a postorder sequence and uses that sequence to |
| 335 | /// compute the set of SCCs connected into a cycle. It should only be called to |
| 336 | /// insert a "downward" edge which will require changing the sequence to |
| 337 | /// restore it to a postorder. |
| 338 | /// |
| 339 | /// When inserting an edge from an earlier SCC to a later SCC in some postorder |
| 340 | /// sequence, all of the SCCs which may be impacted are in the closed range of |
| 341 | /// those two within the postorder sequence. The algorithm used here to restore |
| 342 | /// the state is as follows: |
| 343 | /// |
| 344 | /// 1) Starting from the source SCC, construct a set of SCCs which reach the |
| 345 | /// source SCC consisting of just the source SCC. Then scan toward the |
| 346 | /// target SCC in postorder and for each SCC, if it has an edge to an SCC |
| 347 | /// in the set, add it to the set. Otherwise, the source SCC is not |
| 348 | /// a successor, move it in the postorder sequence to immediately before |
| 349 | /// the source SCC, shifting the source SCC and all SCCs in the set one |
| 350 | /// position toward the target SCC. Stop scanning after processing the |
| 351 | /// target SCC. |
| 352 | /// 2) If the source SCC is now past the target SCC in the postorder sequence, |
| 353 | /// and thus the new edge will flow toward the start, we are done. |
| 354 | /// 3) Otherwise, starting from the target SCC, walk all edges which reach an |
| 355 | /// SCC between the source and the target, and add them to the set of |
| 356 | /// connected SCCs, then recurse through them. Once a complete set of the |
| 357 | /// SCCs the target connects to is known, hoist the remaining SCCs between |
| 358 | /// the source and the target to be above the target. Note that there is no |
| 359 | /// need to process the source SCC, it is already known to connect. |
| 360 | /// 4) At this point, all of the SCCs in the closed range between the source |
| 361 | /// SCC and the target SCC in the postorder sequence are connected, |
| 362 | /// including the target SCC and the source SCC. Inserting the edge from |
| 363 | /// the source SCC to the target SCC will form a cycle out of precisely |
| 364 | /// these SCCs. Thus we can merge all of the SCCs in this closed range into |
| 365 | /// a single SCC. |
| 366 | /// |
| 367 | /// This process has various important properties: |
| 368 | /// - Only mutates the SCCs when adding the edge actually changes the SCC |
| 369 | /// structure. |
| 370 | /// - Never mutates SCCs which are unaffected by the change. |
| 371 | /// - Updates the postorder sequence to correctly satisfy the postorder |
| 372 | /// constraint after the edge is inserted. |
| 373 | /// - Only reorders SCCs in the closed postorder sequence from the source to |
| 374 | /// the target, so easy to bound how much has changed even in the ordering. |
| 375 | /// - Big-O is the number of edges in the closed postorder range of SCCs from |
| 376 | /// source to target. |
| 377 | /// |
| 378 | /// This helper routine, in addition to updating the postorder sequence itself |
| 379 | /// will also update a map from SCCs to indices within that sequecne. |
| 380 | /// |
| 381 | /// The sequence and the map must operate on pointers to the SCC type. |
| 382 | /// |
| 383 | /// Two callbacks must be provided. The first computes the subset of SCCs in |
| 384 | /// the postorder closed range from the source to the target which connect to |
| 385 | /// the source SCC via some (transitive) set of edges. The second computes the |
| 386 | /// subset of the same range which the target SCC connects to via some |
| 387 | /// (transitive) set of edges. Both callbacks should populate the set argument |
| 388 | /// provided. |
| 389 | template <typename SCCT, typename PostorderSequenceT, typename SCCIndexMapT, |
| 390 | typename ComputeSourceConnectedSetCallableT, |
| 391 | typename ComputeTargetConnectedSetCallableT> |
| 392 | static iterator_range<typename PostorderSequenceT::iterator> |
| 393 | updatePostorderSequenceForEdgeInsertion( |
| 394 | SCCT &SourceSCC, SCCT &TargetSCC, PostorderSequenceT &SCCs, |
| 395 | SCCIndexMapT &SCCIndices, |
| 396 | ComputeSourceConnectedSetCallableT ComputeSourceConnectedSet, |
| 397 | ComputeTargetConnectedSetCallableT ComputeTargetConnectedSet) { |
| 398 | int SourceIdx = SCCIndices[&SourceSCC]; |
| 399 | int TargetIdx = SCCIndices[&TargetSCC]; |
| 400 | assert(SourceIdx < TargetIdx && "Cannot have equal indices here!"); |
| 401 | |
| 402 | SmallPtrSet<SCCT *, 4> ConnectedSet; |
| 403 | |
| 404 | // Compute the SCCs which (transitively) reach the source. |
| 405 | ComputeSourceConnectedSet(ConnectedSet); |
| 406 | |
| 407 | // Partition the SCCs in this part of the port-order sequence so only SCCs |
| 408 | // connecting to the source remain between it and the target. This is |
| 409 | // a benign partition as it preserves postorder. |
| 410 | auto SourceI = std::stable_partition( |
| 411 | SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx + 1, |
| 412 | [&ConnectedSet](SCCT *C) { return !ConnectedSet.count(C); }); |
| 413 | for (int i = SourceIdx, e = TargetIdx + 1; i < e; ++i) |
| 414 | SCCIndices.find(SCCs[i])->second = i; |
| 415 | |
| 416 | // If the target doesn't connect to the source, then we've corrected the |
| 417 | // post-order and there are no cycles formed. |
| 418 | if (!ConnectedSet.count(&TargetSCC)) { |
| 419 | assert(SourceI > (SCCs.begin() + SourceIdx) && |
| 420 | "Must have moved the source to fix the post-order."); |
| 421 | assert(*std::prev(SourceI) == &TargetSCC && |
| 422 | "Last SCC to move should have bene the target."); |
| 423 | |
| 424 | // Return an empty range at the target SCC indicating there is nothing to |
| 425 | // merge. |
| 426 | return make_range(std::prev(SourceI), std::prev(SourceI)); |
| 427 | } |
| 428 | |
| 429 | assert(SCCs[TargetIdx] == &TargetSCC && |
| 430 | "Should not have moved target if connected!"); |
| 431 | SourceIdx = SourceI - SCCs.begin(); |
| 432 | assert(SCCs[SourceIdx] == &SourceSCC && |
| 433 | "Bad updated index computation for the source SCC!"); |
| 434 | |
| 435 | |
| 436 | // See whether there are any remaining intervening SCCs between the source |
| 437 | // and target. If so we need to make sure they all are reachable form the |
| 438 | // target. |
| 439 | if (SourceIdx + 1 < TargetIdx) { |
| 440 | ConnectedSet.clear(); |
| 441 | ComputeTargetConnectedSet(ConnectedSet); |
| 442 | |
| 443 | // Partition SCCs so that only SCCs reached from the target remain between |
| 444 | // the source and the target. This preserves postorder. |
| 445 | auto TargetI = std::stable_partition( |
| 446 | SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1, |
| 447 | [&ConnectedSet](SCCT *C) { return ConnectedSet.count(C); }); |
| 448 | for (int i = SourceIdx + 1, e = TargetIdx + 1; i < e; ++i) |
| 449 | SCCIndices.find(SCCs[i])->second = i; |
| 450 | TargetIdx = std::prev(TargetI) - SCCs.begin(); |
| 451 | assert(SCCs[TargetIdx] == &TargetSCC && |
| 452 | "Should always end with the target!"); |
| 453 | } |
| 454 | |
| 455 | // At this point, we know that connecting source to target forms a cycle |
| 456 | // because target connects back to source, and we know that all of the SCCs |
| 457 | // between the source and target in the postorder sequence participate in that |
| 458 | // cycle. |
| 459 | return make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx); |
| 460 | } |
| 461 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 462 | SmallVector<LazyCallGraph::SCC *, 1> |
| 463 | LazyCallGraph::RefSCC::switchInternalEdgeToCall(Node &SourceN, Node &TargetN) { |
| 464 | assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!"); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 465 | SmallVector<SCC *, 1> DeletedSCCs; |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 466 | |
Chandler Carruth | 11b3f60 | 2016-09-04 08:34:31 +0000 | [diff] [blame] | 467 | #ifndef NDEBUG |
| 468 | // In a debug build, verify the RefSCC is valid to start with and when this |
| 469 | // routine finishes. |
| 470 | verify(); |
| 471 | auto VerifyOnExit = make_scope_exit([&]() { verify(); }); |
| 472 | #endif |
| 473 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 474 | SCC &SourceSCC = *G->lookupSCC(SourceN); |
| 475 | SCC &TargetSCC = *G->lookupSCC(TargetN); |
| 476 | |
| 477 | // If the two nodes are already part of the same SCC, we're also done as |
| 478 | // we've just added more connectivity. |
| 479 | if (&SourceSCC == &TargetSCC) { |
| 480 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 481 | return DeletedSCCs; |
| 482 | } |
| 483 | |
| 484 | // At this point we leverage the postorder list of SCCs to detect when the |
| 485 | // insertion of an edge changes the SCC structure in any way. |
| 486 | // |
| 487 | // First and foremost, we can eliminate the need for any changes when the |
| 488 | // edge is toward the beginning of the postorder sequence because all edges |
| 489 | // flow in that direction already. Thus adding a new one cannot form a cycle. |
| 490 | int SourceIdx = SCCIndices[&SourceSCC]; |
| 491 | int TargetIdx = SCCIndices[&TargetSCC]; |
| 492 | if (TargetIdx < SourceIdx) { |
| 493 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 494 | return DeletedSCCs; |
| 495 | } |
| 496 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 497 | // Compute the SCCs which (transitively) reach the source. |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 498 | auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 499 | #ifndef NDEBUG |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 500 | // Check that the RefSCC is still valid before computing this as the |
| 501 | // results will be nonsensical of we've broken its invariants. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 502 | verify(); |
| 503 | #endif |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 504 | ConnectedSet.insert(&SourceSCC); |
| 505 | auto IsConnected = [&](SCC &C) { |
| 506 | for (Node &N : C) |
| 507 | for (Edge &E : N.calls()) { |
| 508 | assert(E.getNode() && "Must have formed a node within an SCC!"); |
| 509 | if (ConnectedSet.count(G->lookupSCC(*E.getNode()))) |
| 510 | return true; |
| 511 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 512 | |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 513 | return false; |
| 514 | }; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 515 | |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 516 | for (SCC *C : |
| 517 | make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1)) |
| 518 | if (IsConnected(*C)) |
| 519 | ConnectedSet.insert(C); |
| 520 | }; |
| 521 | |
| 522 | // Use a normal worklist to find which SCCs the target connects to. We still |
| 523 | // bound the search based on the range in the postorder list we care about, |
| 524 | // but because this is forward connectivity we just "recurse" through the |
| 525 | // edges. |
| 526 | auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 527 | #ifndef NDEBUG |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 528 | // Check that the RefSCC is still valid before computing this as the |
| 529 | // results will be nonsensical of we've broken its invariants. |
| 530 | verify(); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 531 | #endif |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 532 | ConnectedSet.insert(&TargetSCC); |
| 533 | SmallVector<SCC *, 4> Worklist; |
| 534 | Worklist.push_back(&TargetSCC); |
| 535 | do { |
| 536 | SCC &C = *Worklist.pop_back_val(); |
| 537 | for (Node &N : C) |
| 538 | for (Edge &E : N) { |
| 539 | assert(E.getNode() && "Must have formed a node within an SCC!"); |
| 540 | if (!E.isCall()) |
| 541 | continue; |
| 542 | SCC &EdgeC = *G->lookupSCC(*E.getNode()); |
| 543 | if (&EdgeC.getOuterRefSCC() != this) |
| 544 | // Not in this RefSCC... |
| 545 | continue; |
| 546 | if (SCCIndices.find(&EdgeC)->second <= SourceIdx) |
| 547 | // Not in the postorder sequence between source and target. |
| 548 | continue; |
| 549 | |
| 550 | if (ConnectedSet.insert(&EdgeC).second) |
| 551 | Worklist.push_back(&EdgeC); |
| 552 | } |
| 553 | } while (!Worklist.empty()); |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 554 | }; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 555 | |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 556 | // Use a generic helper to update the postorder sequence of SCCs and return |
| 557 | // a range of any SCCs connected into a cycle by inserting this edge. This |
| 558 | // routine will also take care of updating the indices into the postorder |
| 559 | // sequence. |
| 560 | auto MergeRange = updatePostorderSequenceForEdgeInsertion( |
| 561 | SourceSCC, TargetSCC, SCCs, SCCIndices, ComputeSourceConnectedSet, |
| 562 | ComputeTargetConnectedSet); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 563 | |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 564 | // If the merge range is empty, then adding the edge didn't actually form any |
| 565 | // new cycles. We're done. |
| 566 | if (MergeRange.begin() == MergeRange.end()) { |
| 567 | // Now that the SCC structure is finalized, flip the kind to call. |
| 568 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call); |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 569 | return DeletedSCCs; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Chandler Carruth | 1f621f0 | 2016-09-04 08:34:24 +0000 | [diff] [blame] | 572 | #ifndef NDEBUG |
| 573 | // Before merging, check that the RefSCC remains valid after all the |
| 574 | // postorder updates. |
| 575 | verify(); |
| 576 | #endif |
| 577 | |
| 578 | // Otherwise we need to merge all of the SCCs in the cycle into a single |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 579 | // result SCC. |
| 580 | // |
| 581 | // NB: We merge into the target because all of these functions were already |
| 582 | // reachable from the target, meaning any SCC-wide properties deduced about it |
| 583 | // other than the set of functions within it will not have changed. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 584 | for (SCC *C : MergeRange) { |
| 585 | assert(C != &TargetSCC && |
| 586 | "We merge *into* the target and shouldn't process it here!"); |
| 587 | SCCIndices.erase(C); |
| 588 | TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end()); |
| 589 | for (Node *N : C->Nodes) |
| 590 | G->SCCMap[N] = &TargetSCC; |
| 591 | C->clear(); |
| 592 | DeletedSCCs.push_back(C); |
| 593 | } |
| 594 | |
| 595 | // Erase the merged SCCs from the list and update the indices of the |
| 596 | // remaining SCCs. |
| 597 | int IndexOffset = MergeRange.end() - MergeRange.begin(); |
| 598 | auto EraseEnd = SCCs.erase(MergeRange.begin(), MergeRange.end()); |
| 599 | for (SCC *C : make_range(EraseEnd, SCCs.end())) |
| 600 | SCCIndices[C] -= IndexOffset; |
| 601 | |
| 602 | // Now that the SCC structure is finalized, flip the kind to call. |
| 603 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call); |
| 604 | |
Chandler Carruth | 11b3f60 | 2016-09-04 08:34:31 +0000 | [diff] [blame] | 605 | // And we're done! |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 606 | return DeletedSCCs; |
Chandler Carruth | 5217c94 | 2014-04-30 10:48:36 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 609 | iterator_range<LazyCallGraph::RefSCC::iterator> |
| 610 | LazyCallGraph::RefSCC::switchInternalEdgeToRef(Node &SourceN, Node &TargetN) { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 611 | assert(SourceN[TargetN].isCall() && "Must start with a call edge!"); |
| 612 | |
Chandler Carruth | 11b3f60 | 2016-09-04 08:34:31 +0000 | [diff] [blame] | 613 | #ifndef NDEBUG |
| 614 | // In a debug build, verify the RefSCC is valid to start with and when this |
| 615 | // routine finishes. |
| 616 | verify(); |
| 617 | auto VerifyOnExit = make_scope_exit([&]() { verify(); }); |
| 618 | #endif |
| 619 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 620 | SCC &SourceSCC = *G->lookupSCC(SourceN); |
| 621 | SCC &TargetSCC = *G->lookupSCC(TargetN); |
| 622 | |
| 623 | assert(&SourceSCC.getOuterRefSCC() == this && |
| 624 | "Source must be in this RefSCC."); |
| 625 | assert(&TargetSCC.getOuterRefSCC() == this && |
| 626 | "Target must be in this RefSCC."); |
| 627 | |
| 628 | // Set the edge kind. |
| 629 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref); |
| 630 | |
| 631 | // If this call edge is just connecting two separate SCCs within this RefSCC, |
| 632 | // there is nothing to do. |
Chandler Carruth | 11b3f60 | 2016-09-04 08:34:31 +0000 | [diff] [blame] | 633 | if (&SourceSCC != &TargetSCC) |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 634 | return make_range(SCCs.end(), SCCs.end()); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 635 | |
| 636 | // Otherwise we are removing a call edge from a single SCC. This may break |
| 637 | // the cycle. In order to compute the new set of SCCs, we need to do a small |
| 638 | // DFS over the nodes within the SCC to form any sub-cycles that remain as |
| 639 | // distinct SCCs and compute a postorder over the resulting SCCs. |
| 640 | // |
| 641 | // However, we specially handle the target node. The target node is known to |
| 642 | // reach all other nodes in the original SCC by definition. This means that |
| 643 | // we want the old SCC to be replaced with an SCC contaning that node as it |
| 644 | // will be the root of whatever SCC DAG results from the DFS. Assumptions |
| 645 | // about an SCC such as the set of functions called will continue to hold, |
| 646 | // etc. |
| 647 | |
| 648 | SCC &OldSCC = TargetSCC; |
| 649 | SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack; |
| 650 | SmallVector<Node *, 16> PendingSCCStack; |
| 651 | SmallVector<SCC *, 4> NewSCCs; |
| 652 | |
| 653 | // Prepare the nodes for a fresh DFS. |
| 654 | SmallVector<Node *, 16> Worklist; |
| 655 | Worklist.swap(OldSCC.Nodes); |
| 656 | for (Node *N : Worklist) { |
| 657 | N->DFSNumber = N->LowLink = 0; |
| 658 | G->SCCMap.erase(N); |
| 659 | } |
| 660 | |
| 661 | // Force the target node to be in the old SCC. This also enables us to take |
| 662 | // a very significant short-cut in the standard Tarjan walk to re-form SCCs |
| 663 | // below: whenever we build an edge that reaches the target node, we know |
| 664 | // that the target node eventually connects back to all other nodes in our |
| 665 | // walk. As a consequence, we can detect and handle participants in that |
| 666 | // cycle without walking all the edges that form this connection, and instead |
| 667 | // by relying on the fundamental guarantee coming into this operation (all |
| 668 | // nodes are reachable from the target due to previously forming an SCC). |
| 669 | TargetN.DFSNumber = TargetN.LowLink = -1; |
| 670 | OldSCC.Nodes.push_back(&TargetN); |
| 671 | G->SCCMap[&TargetN] = &OldSCC; |
| 672 | |
| 673 | // Scan down the stack and DFS across the call edges. |
| 674 | for (Node *RootN : Worklist) { |
| 675 | assert(DFSStack.empty() && |
| 676 | "Cannot begin a new root with a non-empty DFS stack!"); |
| 677 | assert(PendingSCCStack.empty() && |
| 678 | "Cannot begin a new root with pending nodes for an SCC!"); |
| 679 | |
| 680 | // Skip any nodes we've already reached in the DFS. |
| 681 | if (RootN->DFSNumber != 0) { |
| 682 | assert(RootN->DFSNumber == -1 && |
| 683 | "Shouldn't have any mid-DFS root nodes!"); |
| 684 | continue; |
| 685 | } |
| 686 | |
| 687 | RootN->DFSNumber = RootN->LowLink = 1; |
| 688 | int NextDFSNumber = 2; |
| 689 | |
| 690 | DFSStack.push_back({RootN, RootN->call_begin()}); |
| 691 | do { |
| 692 | Node *N; |
| 693 | call_edge_iterator I; |
| 694 | std::tie(N, I) = DFSStack.pop_back_val(); |
| 695 | auto E = N->call_end(); |
| 696 | while (I != E) { |
| 697 | Node &ChildN = *I->getNode(); |
| 698 | if (ChildN.DFSNumber == 0) { |
| 699 | // We haven't yet visited this child, so descend, pushing the current |
| 700 | // node onto the stack. |
| 701 | DFSStack.push_back({N, I}); |
| 702 | |
| 703 | assert(!G->SCCMap.count(&ChildN) && |
| 704 | "Found a node with 0 DFS number but already in an SCC!"); |
| 705 | ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++; |
| 706 | N = &ChildN; |
| 707 | I = N->call_begin(); |
| 708 | E = N->call_end(); |
| 709 | continue; |
| 710 | } |
| 711 | |
| 712 | // Check for the child already being part of some component. |
| 713 | if (ChildN.DFSNumber == -1) { |
| 714 | if (G->lookupSCC(ChildN) == &OldSCC) { |
| 715 | // If the child is part of the old SCC, we know that it can reach |
| 716 | // every other node, so we have formed a cycle. Pull the entire DFS |
| 717 | // and pending stacks into it. See the comment above about setting |
| 718 | // up the old SCC for why we do this. |
| 719 | int OldSize = OldSCC.size(); |
| 720 | OldSCC.Nodes.push_back(N); |
| 721 | OldSCC.Nodes.append(PendingSCCStack.begin(), PendingSCCStack.end()); |
| 722 | PendingSCCStack.clear(); |
| 723 | while (!DFSStack.empty()) |
| 724 | OldSCC.Nodes.push_back(DFSStack.pop_back_val().first); |
| 725 | for (Node &N : make_range(OldSCC.begin() + OldSize, OldSCC.end())) { |
| 726 | N.DFSNumber = N.LowLink = -1; |
| 727 | G->SCCMap[&N] = &OldSCC; |
| 728 | } |
| 729 | N = nullptr; |
| 730 | break; |
| 731 | } |
| 732 | |
| 733 | // If the child has already been added to some child component, it |
| 734 | // couldn't impact the low-link of this parent because it isn't |
| 735 | // connected, and thus its low-link isn't relevant so skip it. |
| 736 | ++I; |
| 737 | continue; |
| 738 | } |
| 739 | |
| 740 | // Track the lowest linked child as the lowest link for this node. |
| 741 | assert(ChildN.LowLink > 0 && "Must have a positive low-link number!"); |
| 742 | if (ChildN.LowLink < N->LowLink) |
| 743 | N->LowLink = ChildN.LowLink; |
| 744 | |
| 745 | // Move to the next edge. |
| 746 | ++I; |
| 747 | } |
| 748 | if (!N) |
| 749 | // Cleared the DFS early, start another round. |
| 750 | break; |
| 751 | |
| 752 | // We've finished processing N and its descendents, put it on our pending |
| 753 | // SCC stack to eventually get merged into an SCC of nodes. |
| 754 | PendingSCCStack.push_back(N); |
| 755 | |
| 756 | // If this node is linked to some lower entry, continue walking up the |
| 757 | // stack. |
| 758 | if (N->LowLink != N->DFSNumber) |
| 759 | continue; |
| 760 | |
| 761 | // Otherwise, we've completed an SCC. Append it to our post order list of |
| 762 | // SCCs. |
| 763 | int RootDFSNumber = N->DFSNumber; |
| 764 | // Find the range of the node stack by walking down until we pass the |
| 765 | // root DFS number. |
| 766 | auto SCCNodes = make_range( |
| 767 | PendingSCCStack.rbegin(), |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 768 | find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) { |
| 769 | return N->DFSNumber < RootDFSNumber; |
| 770 | })); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 771 | |
| 772 | // Form a new SCC out of these nodes and then clear them off our pending |
| 773 | // stack. |
| 774 | NewSCCs.push_back(G->createSCC(*this, SCCNodes)); |
| 775 | for (Node &N : *NewSCCs.back()) { |
| 776 | N.DFSNumber = N.LowLink = -1; |
| 777 | G->SCCMap[&N] = NewSCCs.back(); |
| 778 | } |
| 779 | PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end()); |
| 780 | } while (!DFSStack.empty()); |
| 781 | } |
| 782 | |
| 783 | // Insert the remaining SCCs before the old one. The old SCC can reach all |
| 784 | // other SCCs we form because it contains the target node of the removed edge |
| 785 | // of the old SCC. This means that we will have edges into all of the new |
| 786 | // SCCs, which means the old one must come last for postorder. |
| 787 | int OldIdx = SCCIndices[&OldSCC]; |
| 788 | SCCs.insert(SCCs.begin() + OldIdx, NewSCCs.begin(), NewSCCs.end()); |
| 789 | |
| 790 | // Update the mapping from SCC* to index to use the new SCC*s, and remove the |
| 791 | // old SCC from the mapping. |
| 792 | for (int Idx = OldIdx, Size = SCCs.size(); Idx < Size; ++Idx) |
| 793 | SCCIndices[SCCs[Idx]] = Idx; |
| 794 | |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 795 | return make_range(SCCs.begin() + OldIdx, |
| 796 | SCCs.begin() + OldIdx + NewSCCs.size()); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN, |
| 800 | Node &TargetN) { |
| 801 | assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!"); |
| 802 | |
| 803 | assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC."); |
| 804 | assert(G->lookupRefSCC(TargetN) != this && |
| 805 | "Target must not be in this RefSCC."); |
| 806 | assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) && |
| 807 | "Target must be a descendant of the Source."); |
| 808 | |
| 809 | // Edges between RefSCCs are the same regardless of call or ref, so we can |
| 810 | // just flip the edge here. |
| 811 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call); |
| 812 | |
| 813 | #ifndef NDEBUG |
| 814 | // Check that the RefSCC is still valid. |
| 815 | verify(); |
| 816 | #endif |
| 817 | } |
| 818 | |
| 819 | void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN, |
| 820 | Node &TargetN) { |
| 821 | assert(SourceN[TargetN].isCall() && "Must start with a call edge!"); |
| 822 | |
| 823 | assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC."); |
| 824 | assert(G->lookupRefSCC(TargetN) != this && |
| 825 | "Target must not be in this RefSCC."); |
| 826 | assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) && |
| 827 | "Target must be a descendant of the Source."); |
| 828 | |
| 829 | // Edges between RefSCCs are the same regardless of call or ref, so we can |
| 830 | // just flip the edge here. |
| 831 | SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref); |
| 832 | |
| 833 | #ifndef NDEBUG |
| 834 | // Check that the RefSCC is still valid. |
| 835 | verify(); |
| 836 | #endif |
| 837 | } |
| 838 | |
| 839 | void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN, |
| 840 | Node &TargetN) { |
| 841 | assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC."); |
| 842 | assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC."); |
| 843 | |
| 844 | SourceN.insertEdgeInternal(TargetN, Edge::Ref); |
| 845 | |
| 846 | #ifndef NDEBUG |
| 847 | // Check that the RefSCC is still valid. |
| 848 | verify(); |
| 849 | #endif |
| 850 | } |
| 851 | |
| 852 | void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN, |
| 853 | Edge::Kind EK) { |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 854 | // First insert it into the caller. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 855 | SourceN.insertEdgeInternal(TargetN, EK); |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 856 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 857 | assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC."); |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 858 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 859 | RefSCC &TargetC = *G->lookupRefSCC(TargetN); |
| 860 | assert(&TargetC != this && "Target must not be in this RefSCC."); |
| 861 | assert(TargetC.isDescendantOf(*this) && |
| 862 | "Target must be a descendant of the Source."); |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 863 | |
Chandler Carruth | 9153911 | 2015-12-28 01:54:20 +0000 | [diff] [blame] | 864 | // The only change required is to add this SCC to the parent set of the |
| 865 | // callee. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 866 | TargetC.Parents.insert(this); |
| 867 | |
| 868 | #ifndef NDEBUG |
| 869 | // Check that the RefSCC is still valid. |
| 870 | verify(); |
| 871 | #endif |
Chandler Carruth | 7cc4ed8 | 2014-05-01 12:18:20 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 874 | SmallVector<LazyCallGraph::RefSCC *, 1> |
| 875 | LazyCallGraph::RefSCC::insertIncomingRefEdge(Node &SourceN, Node &TargetN) { |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 876 | assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC."); |
| 877 | RefSCC &SourceC = *G->lookupRefSCC(SourceN); |
| 878 | assert(&SourceC != this && "Source must not be in this RefSCC."); |
| 879 | assert(SourceC.isDescendantOf(*this) && |
| 880 | "Source must be a descendant of the Target."); |
| 881 | |
| 882 | SmallVector<RefSCC *, 1> DeletedRefSCCs; |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 883 | |
Chandler Carruth | 11b3f60 | 2016-09-04 08:34:31 +0000 | [diff] [blame] | 884 | #ifndef NDEBUG |
| 885 | // In a debug build, verify the RefSCC is valid to start with and when this |
| 886 | // routine finishes. |
| 887 | verify(); |
| 888 | auto VerifyOnExit = make_scope_exit([&]() { verify(); }); |
| 889 | #endif |
| 890 | |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 891 | int SourceIdx = G->RefSCCIndices[&SourceC]; |
| 892 | int TargetIdx = G->RefSCCIndices[this]; |
| 893 | assert(SourceIdx < TargetIdx && |
| 894 | "Postorder list doesn't see edge as incoming!"); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 895 | |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 896 | // Compute the RefSCCs which (transitively) reach the source. We do this by |
| 897 | // working backwards from the source using the parent set in each RefSCC, |
| 898 | // skipping any RefSCCs that don't fall in the postorder range. This has the |
| 899 | // advantage of walking the sparser parent edge (in high fan-out graphs) but |
| 900 | // more importantly this removes examining all forward edges in all RefSCCs |
| 901 | // within the postorder range which aren't in fact connected. Only connected |
| 902 | // RefSCCs (and their edges) are visited here. |
| 903 | auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) { |
| 904 | Set.insert(&SourceC); |
| 905 | SmallVector<RefSCC *, 4> Worklist; |
| 906 | Worklist.push_back(&SourceC); |
| 907 | do { |
| 908 | RefSCC &RC = *Worklist.pop_back_val(); |
| 909 | for (RefSCC &ParentRC : RC.parents()) { |
| 910 | // Skip any RefSCCs outside the range of source to target in the |
| 911 | // postorder sequence. |
| 912 | int ParentIdx = G->getRefSCCIndex(ParentRC); |
| 913 | assert(ParentIdx > SourceIdx && "Parent cannot precede source in postorder!"); |
| 914 | if (ParentIdx > TargetIdx) |
| 915 | continue; |
| 916 | if (Set.insert(&ParentRC).second) |
| 917 | // First edge connecting to this parent, add it to our worklist. |
| 918 | Worklist.push_back(&ParentRC); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 919 | } |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 920 | } while (!Worklist.empty()); |
| 921 | }; |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 922 | |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 923 | // Use a normal worklist to find which SCCs the target connects to. We still |
| 924 | // bound the search based on the range in the postorder list we care about, |
| 925 | // but because this is forward connectivity we just "recurse" through the |
| 926 | // edges. |
| 927 | auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) { |
| 928 | Set.insert(this); |
| 929 | SmallVector<RefSCC *, 4> Worklist; |
| 930 | Worklist.push_back(this); |
| 931 | do { |
| 932 | RefSCC &RC = *Worklist.pop_back_val(); |
| 933 | for (SCC &C : RC) |
| 934 | for (Node &N : C) |
| 935 | for (Edge &E : N) { |
| 936 | assert(E.getNode() && "Must have formed a node!"); |
| 937 | RefSCC &EdgeRC = *G->lookupRefSCC(*E.getNode()); |
| 938 | if (G->getRefSCCIndex(EdgeRC) <= SourceIdx) |
| 939 | // Not in the postorder sequence between source and target. |
| 940 | continue; |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 941 | |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 942 | if (Set.insert(&EdgeRC).second) |
| 943 | Worklist.push_back(&EdgeRC); |
| 944 | } |
| 945 | } while (!Worklist.empty()); |
| 946 | }; |
| 947 | |
| 948 | // Use a generic helper to update the postorder sequence of RefSCCs and return |
| 949 | // a range of any RefSCCs connected into a cycle by inserting this edge. This |
| 950 | // routine will also take care of updating the indices into the postorder |
| 951 | // sequence. |
| 952 | iterator_range<SmallVectorImpl<RefSCC *>::iterator> MergeRange = |
| 953 | updatePostorderSequenceForEdgeInsertion( |
| 954 | SourceC, *this, G->PostOrderRefSCCs, G->RefSCCIndices, |
| 955 | ComputeSourceConnectedSet, ComputeTargetConnectedSet); |
| 956 | |
Chandler Carruth | 5205c35 | 2016-12-07 01:42:40 +0000 | [diff] [blame] | 957 | // Build a set so we can do fast tests for whether a RefSCC will end up as |
| 958 | // part of the merged RefSCC. |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 959 | SmallPtrSet<RefSCC *, 16> MergeSet(MergeRange.begin(), MergeRange.end()); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 960 | |
Chandler Carruth | 5205c35 | 2016-12-07 01:42:40 +0000 | [diff] [blame] | 961 | // This RefSCC will always be part of that set, so just insert it here. |
| 962 | MergeSet.insert(this); |
| 963 | |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 964 | // Now that we have identified all of the SCCs which need to be merged into |
| 965 | // 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] | 966 | SmallVector<SCC *, 16> MergedSCCs; |
| 967 | int SCCIndex = 0; |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 968 | for (RefSCC *RC : MergeRange) { |
| 969 | assert(RC != this && "We're merging into the target RefSCC, so it " |
| 970 | "shouldn't be in the range."); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 971 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 972 | // Merge the parents which aren't part of the merge into the our parents. |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 973 | for (RefSCC *ParentRC : RC->Parents) |
| 974 | if (!MergeSet.count(ParentRC)) |
| 975 | Parents.insert(ParentRC); |
| 976 | RC->Parents.clear(); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 977 | |
| 978 | // Walk the inner SCCs to update their up-pointer and walk all the edges to |
| 979 | // update any parent sets. |
| 980 | // FIXME: We should try to find a way to avoid this (rather expensive) edge |
| 981 | // walk by updating the parent sets in some other manner. |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 982 | for (SCC &InnerC : *RC) { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 983 | InnerC.OuterRefSCC = this; |
| 984 | SCCIndices[&InnerC] = SCCIndex++; |
| 985 | for (Node &N : InnerC) { |
| 986 | G->SCCMap[&N] = &InnerC; |
| 987 | for (Edge &E : N) { |
| 988 | assert(E.getNode() && |
| 989 | "Cannot have a null node within a visited SCC!"); |
| 990 | RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode()); |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 991 | if (MergeSet.count(&ChildRC)) |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 992 | continue; |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 993 | ChildRC.Parents.erase(RC); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 994 | ChildRC.Parents.insert(this); |
| 995 | } |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 996 | } |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 997 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 998 | |
| 999 | // Now merge in the SCCs. We can actually move here so try to reuse storage |
| 1000 | // the first time through. |
| 1001 | if (MergedSCCs.empty()) |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 1002 | MergedSCCs = std::move(RC->SCCs); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1003 | else |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 1004 | MergedSCCs.append(RC->SCCs.begin(), RC->SCCs.end()); |
| 1005 | RC->SCCs.clear(); |
| 1006 | DeletedRefSCCs.push_back(RC); |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 1007 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1008 | |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 1009 | // Append our original SCCs to the merged list and move it into place. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1010 | for (SCC &InnerC : *this) |
| 1011 | SCCIndices[&InnerC] = SCCIndex++; |
| 1012 | MergedSCCs.append(SCCs.begin(), SCCs.end()); |
| 1013 | SCCs = std::move(MergedSCCs); |
| 1014 | |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 1015 | // Remove the merged away RefSCCs from the post order sequence. |
| 1016 | for (RefSCC *RC : MergeRange) |
| 1017 | G->RefSCCIndices.erase(RC); |
| 1018 | int IndexOffset = MergeRange.end() - MergeRange.begin(); |
| 1019 | auto EraseEnd = |
| 1020 | G->PostOrderRefSCCs.erase(MergeRange.begin(), MergeRange.end()); |
| 1021 | for (RefSCC *RC : make_range(EraseEnd, G->PostOrderRefSCCs.end())) |
| 1022 | G->RefSCCIndices[RC] -= IndexOffset; |
| 1023 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1024 | // At this point we have a merged RefSCC with a post-order SCCs list, just |
| 1025 | // connect the nodes to form the new edge. |
| 1026 | SourceN.insertEdgeInternal(TargetN, Edge::Ref); |
| 1027 | |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 1028 | // We return the list of SCCs which were merged so that callers can |
| 1029 | // invalidate any data they have associated with those SCCs. Note that these |
| 1030 | // SCCs are no longer in an interesting state (they are totally empty) but |
| 1031 | // the pointers will remain stable for the life of the graph itself. |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 1032 | return DeletedRefSCCs; |
Chandler Carruth | 312dddf | 2014-05-04 09:38:32 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1035 | void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) { |
| 1036 | assert(G->lookupRefSCC(SourceN) == this && |
| 1037 | "The source must be a member of this RefSCC."); |
| 1038 | |
| 1039 | RefSCC &TargetRC = *G->lookupRefSCC(TargetN); |
| 1040 | assert(&TargetRC != this && "The target must not be a member of this RefSCC"); |
| 1041 | |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 1042 | assert(!is_contained(G->LeafRefSCCs, this) && |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1043 | "Cannot have a leaf RefSCC source."); |
| 1044 | |
Chandler Carruth | 11b3f60 | 2016-09-04 08:34:31 +0000 | [diff] [blame] | 1045 | #ifndef NDEBUG |
| 1046 | // In a debug build, verify the RefSCC is valid to start with and when this |
| 1047 | // routine finishes. |
| 1048 | verify(); |
| 1049 | auto VerifyOnExit = make_scope_exit([&]() { verify(); }); |
| 1050 | #endif |
| 1051 | |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1052 | // First remove it from the node. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1053 | SourceN.removeEdgeInternal(TargetN.getFunction()); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1054 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1055 | bool HasOtherEdgeToChildRC = false; |
| 1056 | bool HasOtherChildRC = false; |
| 1057 | for (SCC *InnerC : SCCs) { |
| 1058 | for (Node &N : *InnerC) { |
| 1059 | for (Edge &E : N) { |
| 1060 | assert(E.getNode() && "Cannot have a missing node in a visited SCC!"); |
| 1061 | RefSCC &OtherChildRC = *G->lookupRefSCC(*E.getNode()); |
| 1062 | if (&OtherChildRC == &TargetRC) { |
| 1063 | HasOtherEdgeToChildRC = true; |
| 1064 | break; |
| 1065 | } |
| 1066 | if (&OtherChildRC != this) |
| 1067 | HasOtherChildRC = true; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1068 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1069 | if (HasOtherEdgeToChildRC) |
| 1070 | break; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1071 | } |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1072 | if (HasOtherEdgeToChildRC) |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1073 | break; |
| 1074 | } |
| 1075 | // Because the SCCs form a DAG, deleting such an edge cannot change the set |
| 1076 | // 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] | 1077 | // the source SCC no longer connected to the target SCC. If so, we need to |
| 1078 | // update the target SCC's map of its parents. |
| 1079 | if (!HasOtherEdgeToChildRC) { |
| 1080 | bool Removed = TargetRC.Parents.erase(this); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1081 | (void)Removed; |
| 1082 | assert(Removed && |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1083 | "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] | 1084 | |
| 1085 | // It may orphan an SCC if it is the last edge reaching it, but that does |
| 1086 | // not violate any invariants of the graph. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1087 | if (TargetRC.Parents.empty()) |
| 1088 | DEBUG(dbgs() << "LCG: Update removing " << SourceN.getFunction().getName() |
| 1089 | << " -> " << TargetN.getFunction().getName() |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1090 | << " edge orphaned the callee's SCC!\n"); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1091 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1092 | // It may make the Source SCC a leaf SCC. |
| 1093 | if (!HasOtherChildRC) |
| 1094 | G->LeafRefSCCs.push_back(this); |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1098 | SmallVector<LazyCallGraph::RefSCC *, 1> |
| 1099 | LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN, Node &TargetN) { |
| 1100 | assert(!SourceN[TargetN].isCall() && |
| 1101 | "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] | 1102 | |
Chandler Carruth | 11b3f60 | 2016-09-04 08:34:31 +0000 | [diff] [blame] | 1103 | #ifndef NDEBUG |
| 1104 | // In a debug build, verify the RefSCC is valid to start with and when this |
| 1105 | // routine finishes. |
| 1106 | verify(); |
| 1107 | auto VerifyOnExit = make_scope_exit([&]() { verify(); }); |
| 1108 | #endif |
| 1109 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1110 | // First remove the actual edge. |
| 1111 | SourceN.removeEdgeInternal(TargetN.getFunction()); |
| 1112 | |
| 1113 | // We return a list of the resulting *new* RefSCCs in post-order. |
| 1114 | SmallVector<RefSCC *, 1> Result; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1115 | |
Chandler Carruth | a7205b6 | 2014-04-26 03:36:37 +0000 | [diff] [blame] | 1116 | // Direct recursion doesn't impact the SCC graph at all. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1117 | if (&SourceN == &TargetN) |
| 1118 | return Result; |
Chandler Carruth | a7205b6 | 2014-04-26 03:36:37 +0000 | [diff] [blame] | 1119 | |
Chandler Carruth | c633457 | 2016-12-28 02:24:58 +0000 | [diff] [blame^] | 1120 | // If this ref edge is within an SCC then there are sufficient other edges to |
| 1121 | // form a cycle without this edge so removing it is a no-op. |
| 1122 | SCC &SourceC = *G->lookupSCC(SourceN); |
| 1123 | SCC &TargetC = *G->lookupSCC(TargetN); |
| 1124 | if (&SourceC == &TargetC) |
| 1125 | return Result; |
| 1126 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1127 | // We build somewhat synthetic new RefSCCs by providing a postorder mapping |
| 1128 | // for each inner SCC. We also store these associated with *nodes* rather |
| 1129 | // than SCCs because this saves a round-trip through the node->SCC map and in |
| 1130 | // the common case, SCCs are small. We will verify that we always give the |
| 1131 | // same number to every node in the SCC such that these are equivalent. |
| 1132 | const int RootPostOrderNumber = 0; |
| 1133 | int PostOrderNumber = RootPostOrderNumber + 1; |
| 1134 | SmallDenseMap<Node *, int> PostOrderMapping; |
| 1135 | |
| 1136 | // Every node in the target SCC can already reach every node in this RefSCC |
| 1137 | // (by definition). It is the only node we know will stay inside this RefSCC. |
| 1138 | // Everything which transitively reaches Target will also remain in the |
| 1139 | // RefSCC. We handle this by pre-marking that the nodes in the target SCC map |
| 1140 | // back to the root post order number. |
| 1141 | // |
| 1142 | // This also enables us to take a very significant short-cut in the standard |
| 1143 | // Tarjan walk to re-form RefSCCs below: whenever we build an edge that |
| 1144 | // references the target node, we know that the target node eventually |
| 1145 | // references all other nodes in our walk. As a consequence, we can detect |
| 1146 | // and handle participants in that cycle without walking all the edges that |
| 1147 | // form the connections, and instead by relying on the fundamental guarantee |
| 1148 | // coming into this operation. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1149 | for (Node &N : TargetC) |
| 1150 | PostOrderMapping[&N] = RootPostOrderNumber; |
| 1151 | |
| 1152 | // Reset all the other nodes to prepare for a DFS over them, and add them to |
| 1153 | // our worklist. |
| 1154 | SmallVector<Node *, 8> Worklist; |
| 1155 | for (SCC *C : SCCs) { |
| 1156 | if (C == &TargetC) |
| 1157 | continue; |
| 1158 | |
| 1159 | for (Node &N : *C) |
| 1160 | N.DFSNumber = N.LowLink = 0; |
| 1161 | |
| 1162 | Worklist.append(C->Nodes.begin(), C->Nodes.end()); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1165 | auto MarkNodeForSCCNumber = [&PostOrderMapping](Node &N, int Number) { |
| 1166 | N.DFSNumber = N.LowLink = -1; |
| 1167 | PostOrderMapping[&N] = Number; |
| 1168 | }; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1169 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1170 | SmallVector<std::pair<Node *, edge_iterator>, 4> DFSStack; |
| 1171 | SmallVector<Node *, 4> PendingRefSCCStack; |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 1172 | do { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1173 | assert(DFSStack.empty() && |
| 1174 | "Cannot begin a new root with a non-empty DFS stack!"); |
| 1175 | assert(PendingRefSCCStack.empty() && |
| 1176 | "Cannot begin a new root with pending nodes for an SCC!"); |
| 1177 | |
| 1178 | Node *RootN = Worklist.pop_back_val(); |
| 1179 | // Skip any nodes we've already reached in the DFS. |
| 1180 | if (RootN->DFSNumber != 0) { |
| 1181 | assert(RootN->DFSNumber == -1 && |
| 1182 | "Shouldn't have any mid-DFS root nodes!"); |
| 1183 | continue; |
| 1184 | } |
| 1185 | |
| 1186 | RootN->DFSNumber = RootN->LowLink = 1; |
| 1187 | int NextDFSNumber = 2; |
| 1188 | |
| 1189 | DFSStack.push_back({RootN, RootN->begin()}); |
| 1190 | do { |
| 1191 | Node *N; |
| 1192 | edge_iterator I; |
| 1193 | std::tie(N, I) = DFSStack.pop_back_val(); |
| 1194 | auto E = N->end(); |
| 1195 | |
| 1196 | assert(N->DFSNumber != 0 && "We should always assign a DFS number " |
| 1197 | "before processing a node."); |
| 1198 | |
| 1199 | while (I != E) { |
| 1200 | Node &ChildN = I->getNode(*G); |
| 1201 | if (ChildN.DFSNumber == 0) { |
| 1202 | // Mark that we should start at this child when next this node is the |
| 1203 | // top of the stack. We don't start at the next child to ensure this |
| 1204 | // child's lowlink is reflected. |
| 1205 | DFSStack.push_back({N, I}); |
| 1206 | |
| 1207 | // Continue, resetting to the child node. |
| 1208 | ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++; |
| 1209 | N = &ChildN; |
| 1210 | I = ChildN.begin(); |
| 1211 | E = ChildN.end(); |
| 1212 | continue; |
| 1213 | } |
| 1214 | if (ChildN.DFSNumber == -1) { |
| 1215 | // Check if this edge's target node connects to the deleted edge's |
| 1216 | // target node. If so, we know that every node connected will end up |
| 1217 | // in this RefSCC, so collapse the entire current stack into the root |
| 1218 | // slot in our SCC numbering. See above for the motivation of |
| 1219 | // optimizing the target connected nodes in this way. |
| 1220 | auto PostOrderI = PostOrderMapping.find(&ChildN); |
| 1221 | if (PostOrderI != PostOrderMapping.end() && |
| 1222 | PostOrderI->second == RootPostOrderNumber) { |
| 1223 | MarkNodeForSCCNumber(*N, RootPostOrderNumber); |
| 1224 | while (!PendingRefSCCStack.empty()) |
| 1225 | MarkNodeForSCCNumber(*PendingRefSCCStack.pop_back_val(), |
| 1226 | RootPostOrderNumber); |
| 1227 | while (!DFSStack.empty()) |
| 1228 | MarkNodeForSCCNumber(*DFSStack.pop_back_val().first, |
| 1229 | RootPostOrderNumber); |
| 1230 | // Ensure we break all the way out of the enclosing loop. |
| 1231 | N = nullptr; |
| 1232 | break; |
| 1233 | } |
| 1234 | |
| 1235 | // If this child isn't currently in this RefSCC, no need to process |
Chandler Carruth | 23a6c3f | 2016-12-06 10:29:23 +0000 | [diff] [blame] | 1236 | // it. However, we do need to remove this RefSCC from its RefSCC's |
| 1237 | // parent set. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1238 | RefSCC &ChildRC = *G->lookupRefSCC(ChildN); |
| 1239 | ChildRC.Parents.erase(this); |
| 1240 | ++I; |
| 1241 | continue; |
| 1242 | } |
| 1243 | |
| 1244 | // Track the lowest link of the children, if any are still in the stack. |
| 1245 | // Any child not on the stack will have a LowLink of -1. |
| 1246 | assert(ChildN.LowLink != 0 && |
| 1247 | "Low-link must not be zero with a non-zero DFS number."); |
| 1248 | if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink) |
| 1249 | N->LowLink = ChildN.LowLink; |
| 1250 | ++I; |
| 1251 | } |
| 1252 | if (!N) |
| 1253 | // We short-circuited this node. |
| 1254 | break; |
| 1255 | |
| 1256 | // We've finished processing N and its descendents, put it on our pending |
| 1257 | // stack to eventually get merged into a RefSCC. |
| 1258 | PendingRefSCCStack.push_back(N); |
| 1259 | |
| 1260 | // If this node is linked to some lower entry, continue walking up the |
| 1261 | // stack. |
| 1262 | if (N->LowLink != N->DFSNumber) { |
| 1263 | assert(!DFSStack.empty() && |
| 1264 | "We never found a viable root for a RefSCC to pop off!"); |
| 1265 | continue; |
| 1266 | } |
| 1267 | |
| 1268 | // Otherwise, form a new RefSCC from the top of the pending node stack. |
| 1269 | int RootDFSNumber = N->DFSNumber; |
| 1270 | // Find the range of the node stack by walking down until we pass the |
| 1271 | // root DFS number. |
| 1272 | auto RefSCCNodes = make_range( |
| 1273 | PendingRefSCCStack.rbegin(), |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1274 | find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) { |
| 1275 | return N->DFSNumber < RootDFSNumber; |
| 1276 | })); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1277 | |
| 1278 | // Mark the postorder number for these nodes and clear them off the |
| 1279 | // stack. We'll use the postorder number to pull them into RefSCCs at the |
| 1280 | // end. FIXME: Fuse with the loop above. |
| 1281 | int RefSCCNumber = PostOrderNumber++; |
| 1282 | for (Node *N : RefSCCNodes) |
| 1283 | MarkNodeForSCCNumber(*N, RefSCCNumber); |
| 1284 | |
| 1285 | PendingRefSCCStack.erase(RefSCCNodes.end().base(), |
| 1286 | PendingRefSCCStack.end()); |
| 1287 | } while (!DFSStack.empty()); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1288 | |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 1289 | assert(DFSStack.empty() && "Didn't flush the entire DFS stack!"); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1290 | assert(PendingRefSCCStack.empty() && "Didn't flush all pending nodes!"); |
Chandler Carruth | aca48d0 | 2014-04-26 09:06:53 +0000 | [diff] [blame] | 1291 | } while (!Worklist.empty()); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1292 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1293 | // We now have a post-order numbering for RefSCCs and a mapping from each |
| 1294 | // node in this RefSCC to its final RefSCC. We create each new RefSCC node |
| 1295 | // (re-using this RefSCC node for the root) and build a radix-sort style map |
| 1296 | // from postorder number to the RefSCC. We then append SCCs to each of these |
| 1297 | // RefSCCs in the order they occured in the original SCCs container. |
| 1298 | for (int i = 1; i < PostOrderNumber; ++i) |
| 1299 | Result.push_back(G->createRefSCC(*G)); |
| 1300 | |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 1301 | // Insert the resulting postorder sequence into the global graph postorder |
| 1302 | // sequence before the current RefSCC in that sequence. The idea being that |
| 1303 | // this RefSCC is the target of the reference edge removed, and thus has |
| 1304 | // a direct or indirect edge to every other RefSCC formed and so must be at |
| 1305 | // the end of any postorder traversal. |
| 1306 | // |
| 1307 | // FIXME: It'd be nice to change the APIs so that we returned an iterator |
| 1308 | // range over the global postorder sequence and generally use that sequence |
| 1309 | // rather than building a separate result vector here. |
| 1310 | if (!Result.empty()) { |
| 1311 | int Idx = G->getRefSCCIndex(*this); |
| 1312 | G->PostOrderRefSCCs.insert(G->PostOrderRefSCCs.begin() + Idx, |
| 1313 | Result.begin(), Result.end()); |
| 1314 | for (int i : seq<int>(Idx, G->PostOrderRefSCCs.size())) |
| 1315 | G->RefSCCIndices[G->PostOrderRefSCCs[i]] = i; |
| 1316 | assert(G->PostOrderRefSCCs[G->getRefSCCIndex(*this)] == this && |
| 1317 | "Failed to update this RefSCC's index after insertion!"); |
| 1318 | } |
| 1319 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1320 | for (SCC *C : SCCs) { |
| 1321 | auto PostOrderI = PostOrderMapping.find(&*C->begin()); |
| 1322 | assert(PostOrderI != PostOrderMapping.end() && |
| 1323 | "Cannot have missing mappings for nodes!"); |
| 1324 | int SCCNumber = PostOrderI->second; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1325 | #ifndef NDEBUG |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1326 | for (Node &N : *C) |
| 1327 | assert(PostOrderMapping.find(&N)->second == SCCNumber && |
| 1328 | "Cannot have different numbers for nodes in the same SCC!"); |
| 1329 | #endif |
| 1330 | if (SCCNumber == 0) |
| 1331 | // The root node is handled separately by removing the SCCs. |
| 1332 | continue; |
| 1333 | |
| 1334 | RefSCC &RC = *Result[SCCNumber - 1]; |
| 1335 | int SCCIndex = RC.SCCs.size(); |
| 1336 | RC.SCCs.push_back(C); |
Chandler Carruth | 23a6c3f | 2016-12-06 10:29:23 +0000 | [diff] [blame] | 1337 | RC.SCCIndices[C] = SCCIndex; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1338 | C->OuterRefSCC = &RC; |
| 1339 | } |
| 1340 | |
| 1341 | // FIXME: We re-walk the edges in each RefSCC to establish whether it is |
| 1342 | // a leaf and connect it to the rest of the graph's parents lists. This is |
| 1343 | // really wasteful. We should instead do this during the DFS to avoid yet |
| 1344 | // another edge walk. |
| 1345 | for (RefSCC *RC : Result) |
| 1346 | G->connectRefSCC(*RC); |
| 1347 | |
| 1348 | // Now erase all but the root's SCCs. |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1349 | SCCs.erase(remove_if(SCCs, |
| 1350 | [&](SCC *C) { |
| 1351 | return PostOrderMapping.lookup(&*C->begin()) != |
| 1352 | RootPostOrderNumber; |
| 1353 | }), |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1354 | SCCs.end()); |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 1355 | SCCIndices.clear(); |
| 1356 | for (int i = 0, Size = SCCs.size(); i < Size; ++i) |
| 1357 | SCCIndices[SCCs[i]] = i; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1358 | |
| 1359 | #ifndef NDEBUG |
| 1360 | // Now we need to reconnect the current (root) SCC to the graph. We do this |
| 1361 | // manually because we can special case our leaf handling and detect errors. |
| 1362 | bool IsLeaf = true; |
| 1363 | #endif |
| 1364 | for (SCC *C : SCCs) |
| 1365 | for (Node &N : *C) { |
| 1366 | for (Edge &E : N) { |
| 1367 | assert(E.getNode() && "Cannot have a missing node in a visited SCC!"); |
| 1368 | RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode()); |
| 1369 | if (&ChildRC == this) |
| 1370 | continue; |
| 1371 | ChildRC.Parents.insert(this); |
| 1372 | #ifndef NDEBUG |
| 1373 | IsLeaf = false; |
| 1374 | #endif |
| 1375 | } |
| 1376 | } |
| 1377 | #ifndef NDEBUG |
| 1378 | if (!Result.empty()) |
| 1379 | assert(!IsLeaf && "This SCC cannot be a leaf as we have split out new " |
| 1380 | "SCCs by removing this edge."); |
David Majnemer | 0a16c22 | 2016-08-11 21:15:00 +0000 | [diff] [blame] | 1381 | if (none_of(G->LeafRefSCCs, [&](RefSCC *C) { return C == this; })) |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1382 | assert(!IsLeaf && "This SCC cannot be a leaf as it already had child " |
| 1383 | "SCCs before we removed this edge."); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1384 | #endif |
Chandler Carruth | 5dbc164 | 2016-10-12 07:59:56 +0000 | [diff] [blame] | 1385 | // And connect both this RefSCC and all the new ones to the correct parents. |
| 1386 | // The easiest way to do this is just to re-analyze the old parent set. |
| 1387 | SmallVector<RefSCC *, 4> OldParents(Parents.begin(), Parents.end()); |
| 1388 | Parents.clear(); |
| 1389 | for (RefSCC *ParentRC : OldParents) |
Chandler Carruth | 5205c35 | 2016-12-07 01:42:40 +0000 | [diff] [blame] | 1390 | for (SCC &ParentC : *ParentRC) |
| 1391 | for (Node &ParentN : ParentC) |
Chandler Carruth | 5dbc164 | 2016-10-12 07:59:56 +0000 | [diff] [blame] | 1392 | for (Edge &E : ParentN) { |
| 1393 | assert(E.getNode() && "Cannot have a missing node in a visited SCC!"); |
| 1394 | RefSCC &RC = *G->lookupRefSCC(*E.getNode()); |
Chandler Carruth | 5205c35 | 2016-12-07 01:42:40 +0000 | [diff] [blame] | 1395 | if (&RC != ParentRC) |
| 1396 | RC.Parents.insert(ParentRC); |
Chandler Carruth | 5dbc164 | 2016-10-12 07:59:56 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1399 | // 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] | 1400 | // the leaf SCC list. Note that this DTRT in the case where this was never |
| 1401 | // a leaf. |
| 1402 | // FIXME: As LeafRefSCCs could be very large, we might want to not walk the |
| 1403 | // entire list if this RefSCC wasn't a leaf before the edge removal. |
| 1404 | if (!Result.empty()) |
| 1405 | G->LeafRefSCCs.erase( |
| 1406 | std::remove(G->LeafRefSCCs.begin(), G->LeafRefSCCs.end(), this), |
| 1407 | G->LeafRefSCCs.end()); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1408 | |
Chandler Carruth | 23a6c3f | 2016-12-06 10:29:23 +0000 | [diff] [blame] | 1409 | #ifndef NDEBUG |
| 1410 | // Verify all of the new RefSCCs. |
| 1411 | for (RefSCC *RC : Result) |
| 1412 | RC->verify(); |
| 1413 | #endif |
| 1414 | |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1415 | // Return the new list of SCCs. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1416 | return Result; |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Chandler Carruth | 5dbc164 | 2016-10-12 07:59:56 +0000 | [diff] [blame] | 1419 | void LazyCallGraph::RefSCC::handleTrivialEdgeInsertion(Node &SourceN, |
| 1420 | Node &TargetN) { |
| 1421 | // The only trivial case that requires any graph updates is when we add new |
| 1422 | // ref edge and may connect different RefSCCs along that path. This is only |
| 1423 | // because of the parents set. Every other part of the graph remains constant |
| 1424 | // after this edge insertion. |
| 1425 | assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC."); |
| 1426 | RefSCC &TargetRC = *G->lookupRefSCC(TargetN); |
| 1427 | if (&TargetRC == this) { |
| 1428 | |
| 1429 | return; |
| 1430 | } |
| 1431 | |
| 1432 | assert(TargetRC.isDescendantOf(*this) && |
| 1433 | "Target must be a descendant of the Source."); |
| 1434 | // The only change required is to add this RefSCC to the parent set of the |
| 1435 | // target. This is a set and so idempotent if the edge already existed. |
| 1436 | TargetRC.Parents.insert(this); |
| 1437 | } |
| 1438 | |
| 1439 | void LazyCallGraph::RefSCC::insertTrivialCallEdge(Node &SourceN, |
| 1440 | Node &TargetN) { |
| 1441 | #ifndef NDEBUG |
| 1442 | // Check that the RefSCC is still valid when we finish. |
| 1443 | auto ExitVerifier = make_scope_exit([this] { verify(); }); |
Chandler Carruth | bae595b | 2016-11-22 19:23:31 +0000 | [diff] [blame] | 1444 | |
| 1445 | // Check that we aren't breaking some invariants of the SCC graph. |
| 1446 | SCC &SourceC = *G->lookupSCC(SourceN); |
| 1447 | SCC &TargetC = *G->lookupSCC(TargetN); |
| 1448 | if (&SourceC != &TargetC) |
| 1449 | assert(SourceC.isAncestorOf(TargetC) && |
| 1450 | "Call edge is not trivial in the SCC graph!"); |
Chandler Carruth | 5dbc164 | 2016-10-12 07:59:56 +0000 | [diff] [blame] | 1451 | #endif |
| 1452 | // First insert it into the source or find the existing edge. |
| 1453 | auto InsertResult = SourceN.EdgeIndexMap.insert( |
| 1454 | {&TargetN.getFunction(), SourceN.Edges.size()}); |
| 1455 | if (!InsertResult.second) { |
| 1456 | // Already an edge, just update it. |
| 1457 | Edge &E = SourceN.Edges[InsertResult.first->second]; |
| 1458 | if (E.isCall()) |
| 1459 | return; // Nothing to do! |
| 1460 | E.setKind(Edge::Call); |
| 1461 | } else { |
| 1462 | // Create the new edge. |
| 1463 | SourceN.Edges.emplace_back(TargetN, Edge::Call); |
| 1464 | } |
| 1465 | |
| 1466 | // Now that we have the edge, handle the graph fallout. |
| 1467 | handleTrivialEdgeInsertion(SourceN, TargetN); |
| 1468 | } |
| 1469 | |
| 1470 | void LazyCallGraph::RefSCC::insertTrivialRefEdge(Node &SourceN, Node &TargetN) { |
| 1471 | #ifndef NDEBUG |
| 1472 | // Check that the RefSCC is still valid when we finish. |
| 1473 | auto ExitVerifier = make_scope_exit([this] { verify(); }); |
Chandler Carruth | 9eb857c | 2016-11-22 21:40:10 +0000 | [diff] [blame] | 1474 | |
| 1475 | // Check that we aren't breaking some invariants of the RefSCC graph. |
| 1476 | RefSCC &SourceRC = *G->lookupRefSCC(SourceN); |
| 1477 | RefSCC &TargetRC = *G->lookupRefSCC(TargetN); |
| 1478 | if (&SourceRC != &TargetRC) |
| 1479 | assert(SourceRC.isAncestorOf(TargetRC) && |
| 1480 | "Ref edge is not trivial in the RefSCC graph!"); |
Chandler Carruth | 5dbc164 | 2016-10-12 07:59:56 +0000 | [diff] [blame] | 1481 | #endif |
| 1482 | // First insert it into the source or find the existing edge. |
| 1483 | auto InsertResult = SourceN.EdgeIndexMap.insert( |
| 1484 | {&TargetN.getFunction(), SourceN.Edges.size()}); |
| 1485 | if (!InsertResult.second) |
| 1486 | // Already an edge, we're done. |
| 1487 | return; |
| 1488 | |
| 1489 | // Create the new edge. |
| 1490 | SourceN.Edges.emplace_back(TargetN, Edge::Ref); |
| 1491 | |
| 1492 | // Now that we have the edge, handle the graph fallout. |
| 1493 | handleTrivialEdgeInsertion(SourceN, TargetN); |
| 1494 | } |
| 1495 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1496 | void LazyCallGraph::insertEdge(Node &SourceN, Function &Target, Edge::Kind EK) { |
Chandler Carruth | c00a7ff | 2014-04-28 11:10:23 +0000 | [diff] [blame] | 1497 | assert(SCCMap.empty() && DFSStack.empty() && |
| 1498 | "This method cannot be called after SCCs have been formed!"); |
| 1499 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1500 | return SourceN.insertEdgeInternal(Target, EK); |
Chandler Carruth | c00a7ff | 2014-04-28 11:10:23 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1503 | void LazyCallGraph::removeEdge(Node &SourceN, Function &Target) { |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1504 | assert(SCCMap.empty() && DFSStack.empty() && |
| 1505 | "This method cannot be called after SCCs have been formed!"); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1506 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1507 | return SourceN.removeEdgeInternal(Target); |
Chandler Carruth | 9302fbf | 2014-04-23 11:03:03 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Chandler Carruth | 5dbc164 | 2016-10-12 07:59:56 +0000 | [diff] [blame] | 1510 | void LazyCallGraph::removeDeadFunction(Function &F) { |
| 1511 | // FIXME: This is unnecessarily restrictive. We should be able to remove |
| 1512 | // functions which recursively call themselves. |
| 1513 | assert(F.use_empty() && |
| 1514 | "This routine should only be called on trivially dead functions!"); |
| 1515 | |
| 1516 | auto EII = EntryIndexMap.find(&F); |
| 1517 | if (EII != EntryIndexMap.end()) { |
| 1518 | EntryEdges[EII->second] = Edge(); |
| 1519 | EntryIndexMap.erase(EII); |
| 1520 | } |
| 1521 | |
| 1522 | // It's safe to just remove un-visited functions from the RefSCC entry list. |
| 1523 | // FIXME: This is a linear operation which could become hot and benefit from |
| 1524 | // an index map. |
| 1525 | auto RENI = find(RefSCCEntryNodes, &F); |
| 1526 | if (RENI != RefSCCEntryNodes.end()) |
| 1527 | RefSCCEntryNodes.erase(RENI); |
| 1528 | |
| 1529 | auto NI = NodeMap.find(&F); |
| 1530 | if (NI == NodeMap.end()) |
| 1531 | // Not in the graph at all! |
| 1532 | return; |
| 1533 | |
| 1534 | Node &N = *NI->second; |
| 1535 | NodeMap.erase(NI); |
| 1536 | |
| 1537 | if (SCCMap.empty() && DFSStack.empty()) { |
| 1538 | // No SCC walk has begun, so removing this is fine and there is nothing |
| 1539 | // else necessary at this point but clearing out the node. |
| 1540 | N.clear(); |
| 1541 | return; |
| 1542 | } |
| 1543 | |
| 1544 | // Check that we aren't going to break the DFS walk. |
| 1545 | assert(all_of(DFSStack, |
| 1546 | [&N](const std::pair<Node *, edge_iterator> &Element) { |
| 1547 | return Element.first != &N; |
| 1548 | }) && |
| 1549 | "Tried to remove a function currently in the DFS stack!"); |
| 1550 | assert(find(PendingRefSCCStack, &N) == PendingRefSCCStack.end() && |
| 1551 | "Tried to remove a function currently pending to add to a RefSCC!"); |
| 1552 | |
| 1553 | // Cannot remove a function which has yet to be visited in the DFS walk, so |
| 1554 | // if we have a node at all then we must have an SCC and RefSCC. |
| 1555 | auto CI = SCCMap.find(&N); |
| 1556 | assert(CI != SCCMap.end() && |
| 1557 | "Tried to remove a node without an SCC after DFS walk started!"); |
| 1558 | SCC &C = *CI->second; |
| 1559 | SCCMap.erase(CI); |
| 1560 | RefSCC &RC = C.getOuterRefSCC(); |
| 1561 | |
| 1562 | // This node must be the only member of its SCC as it has no callers, and |
| 1563 | // that SCC must be the only member of a RefSCC as it has no references. |
| 1564 | // Validate these properties first. |
| 1565 | assert(C.size() == 1 && "Dead functions must be in a singular SCC"); |
| 1566 | assert(RC.size() == 1 && "Dead functions must be in a singular RefSCC"); |
| 1567 | assert(RC.Parents.empty() && "Cannot have parents of a dead RefSCC!"); |
| 1568 | |
| 1569 | // Now remove this RefSCC from any parents sets and the leaf list. |
| 1570 | for (Edge &E : N) |
| 1571 | if (Node *TargetN = E.getNode()) |
| 1572 | if (RefSCC *TargetRC = lookupRefSCC(*TargetN)) |
| 1573 | TargetRC->Parents.erase(&RC); |
| 1574 | // FIXME: This is a linear operation which could become hot and benefit from |
| 1575 | // an index map. |
| 1576 | auto LRI = find(LeafRefSCCs, &RC); |
| 1577 | if (LRI != LeafRefSCCs.end()) |
| 1578 | LeafRefSCCs.erase(LRI); |
| 1579 | |
| 1580 | auto RCIndexI = RefSCCIndices.find(&RC); |
| 1581 | int RCIndex = RCIndexI->second; |
| 1582 | PostOrderRefSCCs.erase(PostOrderRefSCCs.begin() + RCIndex); |
| 1583 | RefSCCIndices.erase(RCIndexI); |
| 1584 | for (int i = RCIndex, Size = PostOrderRefSCCs.size(); i < Size; ++i) |
| 1585 | RefSCCIndices[PostOrderRefSCCs[i]] = i; |
| 1586 | |
| 1587 | // Finally clear out all the data structures from the node down through the |
| 1588 | // components. |
| 1589 | N.clear(); |
| 1590 | C.clear(); |
| 1591 | RC.clear(); |
| 1592 | |
| 1593 | // Nothing to delete as all the objects are allocated in stable bump pointer |
| 1594 | // allocators. |
| 1595 | } |
| 1596 | |
Chandler Carruth | 2a898e0 | 2014-04-23 23:20:36 +0000 | [diff] [blame] | 1597 | LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) { |
| 1598 | return *new (MappedN = BPA.Allocate()) Node(*this, F); |
Chandler Carruth | d8d865e | 2014-04-18 11:02:33 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | void LazyCallGraph::updateGraphPtrs() { |
Chandler Carruth | b60cb31 | 2014-04-17 07:25:59 +0000 | [diff] [blame] | 1602 | // Process all nodes updating the graph pointers. |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1603 | { |
| 1604 | SmallVector<Node *, 16> Worklist; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 1605 | for (Edge &E : EntryEdges) |
| 1606 | if (Node *EntryN = E.getNode()) |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1607 | Worklist.push_back(EntryN); |
Chandler Carruth | b60cb31 | 2014-04-17 07:25:59 +0000 | [diff] [blame] | 1608 | |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1609 | while (!Worklist.empty()) { |
| 1610 | Node *N = Worklist.pop_back_val(); |
| 1611 | N->G = this; |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 1612 | for (Edge &E : N->Edges) |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1613 | if (Node *TargetN = E.getNode()) |
| 1614 | Worklist.push_back(TargetN); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1615 | } |
| 1616 | } |
| 1617 | |
| 1618 | // Process all SCCs updating the graph pointers. |
| 1619 | { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1620 | SmallVector<RefSCC *, 16> Worklist(LeafRefSCCs.begin(), LeafRefSCCs.end()); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1621 | |
| 1622 | while (!Worklist.empty()) { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1623 | RefSCC &C = *Worklist.pop_back_val(); |
| 1624 | C.G = this; |
| 1625 | for (RefSCC &ParentC : C.parents()) |
| 1626 | Worklist.push_back(&ParentC); |
Chandler Carruth | aa839b2 | 2014-04-27 01:59:50 +0000 | [diff] [blame] | 1627 | } |
Chandler Carruth | b60cb31 | 2014-04-17 07:25:59 +0000 | [diff] [blame] | 1628 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 1629 | } |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 1630 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1631 | /// Build the internal SCCs for a RefSCC from a sequence of nodes. |
| 1632 | /// |
| 1633 | /// Appends the SCCs to the provided vector and updates the map with their |
| 1634 | /// indices. Both the vector and map must be empty when passed into this |
| 1635 | /// routine. |
| 1636 | void LazyCallGraph::buildSCCs(RefSCC &RC, node_stack_range Nodes) { |
| 1637 | assert(RC.SCCs.empty() && "Already built SCCs!"); |
| 1638 | assert(RC.SCCIndices.empty() && "Already mapped SCC indices!"); |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 1639 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1640 | for (Node *N : Nodes) { |
| 1641 | assert(N->LowLink >= (*Nodes.begin())->LowLink && |
Chandler Carruth | cace662 | 2014-04-23 10:31:17 +0000 | [diff] [blame] | 1642 | "We cannot have a low link in an SCC lower than its root on the " |
| 1643 | "stack!"); |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 1644 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1645 | // This node will go into the next RefSCC, clear out its DFS and low link |
| 1646 | // as we scan. |
| 1647 | N->DFSNumber = N->LowLink = 0; |
| 1648 | } |
| 1649 | |
| 1650 | // Each RefSCC contains a DAG of the call SCCs. To build these, we do |
| 1651 | // a direct walk of the call edges using Tarjan's algorithm. We reuse the |
| 1652 | // internal storage as we won't need it for the outer graph's DFS any longer. |
| 1653 | |
| 1654 | SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack; |
| 1655 | SmallVector<Node *, 16> PendingSCCStack; |
| 1656 | |
| 1657 | // Scan down the stack and DFS across the call edges. |
| 1658 | for (Node *RootN : Nodes) { |
| 1659 | assert(DFSStack.empty() && |
| 1660 | "Cannot begin a new root with a non-empty DFS stack!"); |
| 1661 | assert(PendingSCCStack.empty() && |
| 1662 | "Cannot begin a new root with pending nodes for an SCC!"); |
| 1663 | |
| 1664 | // Skip any nodes we've already reached in the DFS. |
| 1665 | if (RootN->DFSNumber != 0) { |
| 1666 | assert(RootN->DFSNumber == -1 && |
| 1667 | "Shouldn't have any mid-DFS root nodes!"); |
| 1668 | continue; |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1671 | RootN->DFSNumber = RootN->LowLink = 1; |
| 1672 | int NextDFSNumber = 2; |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 1673 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1674 | DFSStack.push_back({RootN, RootN->call_begin()}); |
| 1675 | do { |
| 1676 | Node *N; |
| 1677 | call_edge_iterator I; |
| 1678 | std::tie(N, I) = DFSStack.pop_back_val(); |
| 1679 | auto E = N->call_end(); |
| 1680 | while (I != E) { |
| 1681 | Node &ChildN = *I->getNode(); |
| 1682 | if (ChildN.DFSNumber == 0) { |
| 1683 | // We haven't yet visited this child, so descend, pushing the current |
| 1684 | // node onto the stack. |
| 1685 | DFSStack.push_back({N, I}); |
| 1686 | |
| 1687 | assert(!lookupSCC(ChildN) && |
| 1688 | "Found a node with 0 DFS number but already in an SCC!"); |
| 1689 | ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++; |
| 1690 | N = &ChildN; |
| 1691 | I = N->call_begin(); |
| 1692 | E = N->call_end(); |
| 1693 | continue; |
| 1694 | } |
| 1695 | |
| 1696 | // If the child has already been added to some child component, it |
| 1697 | // couldn't impact the low-link of this parent because it isn't |
| 1698 | // connected, and thus its low-link isn't relevant so skip it. |
| 1699 | if (ChildN.DFSNumber == -1) { |
| 1700 | ++I; |
| 1701 | continue; |
| 1702 | } |
| 1703 | |
| 1704 | // Track the lowest linked child as the lowest link for this node. |
| 1705 | assert(ChildN.LowLink > 0 && "Must have a positive low-link number!"); |
| 1706 | if (ChildN.LowLink < N->LowLink) |
| 1707 | N->LowLink = ChildN.LowLink; |
| 1708 | |
| 1709 | // Move to the next edge. |
| 1710 | ++I; |
| 1711 | } |
| 1712 | |
| 1713 | // We've finished processing N and its descendents, put it on our pending |
| 1714 | // SCC stack to eventually get merged into an SCC of nodes. |
| 1715 | PendingSCCStack.push_back(N); |
| 1716 | |
| 1717 | // If this node is linked to some lower entry, continue walking up the |
| 1718 | // stack. |
| 1719 | if (N->LowLink != N->DFSNumber) |
| 1720 | continue; |
| 1721 | |
| 1722 | // Otherwise, we've completed an SCC. Append it to our post order list of |
| 1723 | // SCCs. |
| 1724 | int RootDFSNumber = N->DFSNumber; |
| 1725 | // Find the range of the node stack by walking down until we pass the |
| 1726 | // root DFS number. |
| 1727 | auto SCCNodes = make_range( |
| 1728 | PendingSCCStack.rbegin(), |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1729 | find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) { |
| 1730 | return N->DFSNumber < RootDFSNumber; |
| 1731 | })); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1732 | // Form a new SCC out of these nodes and then clear them off our pending |
| 1733 | // stack. |
| 1734 | RC.SCCs.push_back(createSCC(RC, SCCNodes)); |
| 1735 | for (Node &N : *RC.SCCs.back()) { |
| 1736 | N.DFSNumber = N.LowLink = -1; |
| 1737 | SCCMap[&N] = RC.SCCs.back(); |
| 1738 | } |
| 1739 | PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end()); |
| 1740 | } while (!DFSStack.empty()); |
| 1741 | } |
| 1742 | |
| 1743 | // Wire up the SCC indices. |
| 1744 | for (int i = 0, Size = RC.SCCs.size(); i < Size; ++i) |
| 1745 | RC.SCCIndices[RC.SCCs[i]] = i; |
Chandler Carruth | 3f9869a | 2014-04-23 06:09:03 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1748 | // FIXME: We should move callers of this to embed the parent linking and leaf |
| 1749 | // tracking into their DFS in order to remove a full walk of all edges. |
| 1750 | void LazyCallGraph::connectRefSCC(RefSCC &RC) { |
| 1751 | // Walk all edges in the RefSCC (this remains linear as we only do this once |
| 1752 | // when we build the RefSCC) to connect it to the parent sets of its |
| 1753 | // children. |
| 1754 | bool IsLeaf = true; |
| 1755 | for (SCC &C : RC) |
| 1756 | for (Node &N : C) |
| 1757 | for (Edge &E : N) { |
| 1758 | assert(E.getNode() && |
| 1759 | "Cannot have a missing node in a visited part of the graph!"); |
| 1760 | RefSCC &ChildRC = *lookupRefSCC(*E.getNode()); |
| 1761 | if (&ChildRC == &RC) |
| 1762 | continue; |
| 1763 | ChildRC.Parents.insert(&RC); |
| 1764 | IsLeaf = false; |
| 1765 | } |
| 1766 | |
Chandler Carruth | 5dbc164 | 2016-10-12 07:59:56 +0000 | [diff] [blame] | 1767 | // For the SCCs where we find no child SCCs, add them to the leaf list. |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1768 | if (IsLeaf) |
| 1769 | LeafRefSCCs.push_back(&RC); |
| 1770 | } |
| 1771 | |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 1772 | bool LazyCallGraph::buildNextRefSCCInPostOrder() { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1773 | if (DFSStack.empty()) { |
| 1774 | Node *N; |
Chandler Carruth | 90821c2 | 2014-04-26 09:45:55 +0000 | [diff] [blame] | 1775 | do { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1776 | // If we've handled all candidate entry nodes to the SCC forest, we're |
| 1777 | // done. |
| 1778 | if (RefSCCEntryNodes.empty()) |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 1779 | return false; |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1780 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1781 | N = &get(*RefSCCEntryNodes.pop_back_val()); |
Chandler Carruth | 90821c2 | 2014-04-26 09:45:55 +0000 | [diff] [blame] | 1782 | } while (N->DFSNumber != 0); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1783 | |
| 1784 | // Found a new root, begin the DFS here. |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1785 | N->LowLink = N->DFSNumber = 1; |
Chandler Carruth | 09751bf | 2014-04-24 09:59:59 +0000 | [diff] [blame] | 1786 | NextDFSNumber = 2; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1787 | DFSStack.push_back({N, N->begin()}); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Chandler Carruth | 91dcf0f | 2014-04-24 21:19:30 +0000 | [diff] [blame] | 1790 | for (;;) { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1791 | Node *N; |
| 1792 | edge_iterator I; |
| 1793 | std::tie(N, I) = DFSStack.pop_back_val(); |
| 1794 | |
| 1795 | assert(N->DFSNumber > 0 && "We should always assign a DFS number " |
| 1796 | "before placing a node onto the stack."); |
Chandler Carruth | 2455393 | 2014-04-24 11:05:20 +0000 | [diff] [blame] | 1797 | |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 1798 | auto E = N->end(); |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1799 | while (I != E) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 1800 | Node &ChildN = I->getNode(*this); |
Chandler Carruth | bd5d308 | 2014-04-23 23:34:48 +0000 | [diff] [blame] | 1801 | if (ChildN.DFSNumber == 0) { |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1802 | // We haven't yet visited this child, so descend, pushing the current |
| 1803 | // node onto the stack. |
| 1804 | DFSStack.push_back({N, N->begin()}); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1805 | |
Chandler Carruth | 09751bf | 2014-04-24 09:59:59 +0000 | [diff] [blame] | 1806 | assert(!SCCMap.count(&ChildN) && |
| 1807 | "Found a node with 0 DFS number but already in an SCC!"); |
| 1808 | ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++; |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1809 | N = &ChildN; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1810 | I = N->begin(); |
| 1811 | E = N->end(); |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1812 | continue; |
Chandler Carruth | cace662 | 2014-04-23 10:31:17 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1815 | // If the child has already been added to some child component, it |
| 1816 | // couldn't impact the low-link of this parent because it isn't |
| 1817 | // connected, and thus its low-link isn't relevant so skip it. |
| 1818 | if (ChildN.DFSNumber == -1) { |
| 1819 | ++I; |
| 1820 | continue; |
| 1821 | } |
| 1822 | |
| 1823 | // Track the lowest linked child as the lowest link for this node. |
| 1824 | assert(ChildN.LowLink > 0 && "Must have a positive low-link number!"); |
| 1825 | if (ChildN.LowLink < N->LowLink) |
Chandler Carruth | bd5d308 | 2014-04-23 23:34:48 +0000 | [diff] [blame] | 1826 | N->LowLink = ChildN.LowLink; |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1827 | |
| 1828 | // Move to the next edge. |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1829 | ++I; |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1832 | // We've finished processing N and its descendents, put it on our pending |
| 1833 | // SCC stack to eventually get merged into an SCC of nodes. |
| 1834 | PendingRefSCCStack.push_back(N); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1835 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1836 | // If this node is linked to some lower entry, continue walking up the |
| 1837 | // stack. |
| 1838 | if (N->LowLink != N->DFSNumber) { |
| 1839 | assert(!DFSStack.empty() && |
| 1840 | "We never found a viable root for an SCC to pop off!"); |
| 1841 | continue; |
| 1842 | } |
Chandler Carruth | 5e2d70b | 2014-04-26 09:28:00 +0000 | [diff] [blame] | 1843 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1844 | // Otherwise, form a new RefSCC from the top of the pending node stack. |
| 1845 | int RootDFSNumber = N->DFSNumber; |
| 1846 | // Find the range of the node stack by walking down until we pass the |
| 1847 | // root DFS number. |
| 1848 | auto RefSCCNodes = node_stack_range( |
| 1849 | PendingRefSCCStack.rbegin(), |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1850 | find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) { |
| 1851 | return N->DFSNumber < RootDFSNumber; |
| 1852 | })); |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1853 | // Form a new RefSCC out of these nodes and then clear them off our pending |
| 1854 | // stack. |
| 1855 | RefSCC *NewRC = createRefSCC(*this); |
| 1856 | buildSCCs(*NewRC, RefSCCNodes); |
| 1857 | connectRefSCC(*NewRC); |
| 1858 | PendingRefSCCStack.erase(RefSCCNodes.end().base(), |
| 1859 | PendingRefSCCStack.end()); |
| 1860 | |
Chandler Carruth | 49d728a | 2016-09-16 10:20:17 +0000 | [diff] [blame] | 1861 | // Push the new node into the postorder list and return true indicating we |
| 1862 | // successfully grew the postorder sequence by one. |
| 1863 | bool Inserted = |
| 1864 | RefSCCIndices.insert({NewRC, PostOrderRefSCCs.size()}).second; |
| 1865 | (void)Inserted; |
| 1866 | assert(Inserted && "Cannot already have this RefSCC in the index map!"); |
| 1867 | PostOrderRefSCCs.push_back(NewRC); |
| 1868 | return true; |
Chandler Carruth | 91dcf0f | 2014-04-24 21:19:30 +0000 | [diff] [blame] | 1869 | } |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 1872 | AnalysisKey LazyCallGraphAnalysis::Key; |
NAKAMURA Takumi | df0cd72 | 2016-02-28 17:17:00 +0000 | [diff] [blame] | 1873 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 1874 | LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {} |
| 1875 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1876 | static void printNode(raw_ostream &OS, LazyCallGraph::Node &N) { |
Chandler Carruth | a4499e9 | 2016-02-02 03:57:13 +0000 | [diff] [blame] | 1877 | OS << " Edges in function: " << N.getFunction().getName() << "\n"; |
| 1878 | for (const LazyCallGraph::Edge &E : N) |
| 1879 | OS << " " << (E.isCall() ? "call" : "ref ") << " -> " |
| 1880 | << E.getFunction().getName() << "\n"; |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 1881 | |
| 1882 | OS << "\n"; |
| 1883 | } |
| 1884 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1885 | static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) { |
| 1886 | ptrdiff_t Size = std::distance(C.begin(), C.end()); |
| 1887 | OS << " SCC with " << Size << " functions:\n"; |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 1888 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1889 | for (LazyCallGraph::Node &N : C) |
| 1890 | OS << " " << N.getFunction().getName() << "\n"; |
| 1891 | } |
| 1892 | |
| 1893 | static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) { |
| 1894 | ptrdiff_t Size = std::distance(C.begin(), C.end()); |
| 1895 | OS << " RefSCC with " << Size << " call SCCs:\n"; |
| 1896 | |
| 1897 | for (LazyCallGraph::SCC &InnerC : C) |
| 1898 | printSCC(OS, InnerC); |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 1899 | |
| 1900 | OS << "\n"; |
| 1901 | } |
| 1902 | |
Chandler Carruth | d174ce4 | 2015-01-05 02:47:05 +0000 | [diff] [blame] | 1903 | PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M, |
Chandler Carruth | b47f801 | 2016-03-11 11:05:24 +0000 | [diff] [blame] | 1904 | ModuleAnalysisManager &AM) { |
| 1905 | LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M); |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 1906 | |
| 1907 | OS << "Printing the call graph for module: " << M.getModuleIdentifier() |
| 1908 | << "\n\n"; |
| 1909 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1910 | for (Function &F : M) |
| 1911 | printNode(OS, G.get(F)); |
Chandler Carruth | 11f5032 | 2015-01-14 00:27:45 +0000 | [diff] [blame] | 1912 | |
Chandler Carruth | e5944d9 | 2016-02-17 00:18:16 +0000 | [diff] [blame] | 1913 | for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs()) |
| 1914 | printRefSCC(OS, C); |
Chandler Carruth | 18eadd92 | 2014-04-18 10:50:32 +0000 | [diff] [blame] | 1915 | |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 1916 | return PreservedAnalyses::all(); |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 1917 | } |
Sean Silva | 7cb3066 | 2016-06-18 09:17:32 +0000 | [diff] [blame] | 1918 | |
| 1919 | LazyCallGraphDOTPrinterPass::LazyCallGraphDOTPrinterPass(raw_ostream &OS) |
| 1920 | : OS(OS) {} |
| 1921 | |
| 1922 | static void printNodeDOT(raw_ostream &OS, LazyCallGraph::Node &N) { |
| 1923 | std::string Name = "\"" + DOT::EscapeString(N.getFunction().getName()) + "\""; |
| 1924 | |
| 1925 | for (const LazyCallGraph::Edge &E : N) { |
| 1926 | OS << " " << Name << " -> \"" |
| 1927 | << DOT::EscapeString(E.getFunction().getName()) << "\""; |
| 1928 | if (!E.isCall()) // It is a ref edge. |
| 1929 | OS << " [style=dashed,label=\"ref\"]"; |
| 1930 | OS << ";\n"; |
| 1931 | } |
| 1932 | |
| 1933 | OS << "\n"; |
| 1934 | } |
| 1935 | |
| 1936 | PreservedAnalyses LazyCallGraphDOTPrinterPass::run(Module &M, |
| 1937 | ModuleAnalysisManager &AM) { |
| 1938 | LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M); |
| 1939 | |
| 1940 | OS << "digraph \"" << DOT::EscapeString(M.getModuleIdentifier()) << "\" {\n"; |
| 1941 | |
| 1942 | for (Function &F : M) |
| 1943 | printNodeDOT(OS, G.get(F)); |
| 1944 | |
| 1945 | OS << "}\n"; |
| 1946 | |
| 1947 | return PreservedAnalyses::all(); |
| 1948 | } |