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