blob: fbf39a237797fbc19c927862e36d69ed6cac3a2d [file] [log] [blame]
Chandler Carruthbf71a342014-02-06 04:37:03 +00001//===- 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 Carruth18eadd922014-04-18 10:50:32 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruth86f0bdf2016-12-09 00:46:44 +000012#include "llvm/ADT/ScopeExit.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000013#include "llvm/ADT/Sequence.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000014#include "llvm/IR/CallSite.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000015#include "llvm/IR/InstVisitor.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000016#include "llvm/IR/Instructions.h"
17#include "llvm/IR/PassManager.h"
Chandler Carruth99b756d2014-04-21 05:04:24 +000018#include "llvm/Support/Debug.h"
Sean Silva7cb30662016-06-18 09:17:32 +000019#include "llvm/Support/GraphWriter.h"
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +000020#include <utility>
Chandler Carruthbf71a342014-02-06 04:37:03 +000021
22using namespace llvm;
23
Chandler Carruthf1221bd2014-04-22 02:48:03 +000024#define DEBUG_TYPE "lcg"
25
Chandler Carruthaaad9f82017-02-09 23:24:13 +000026void LazyCallGraph::EdgeSequence::insertEdgeInternal(Node &TargetN,
27 Edge::Kind EK) {
28 EdgeIndexMap.insert({&TargetN, Edges.size()});
29 Edges.emplace_back(TargetN, EK);
Chandler Carrutha4499e92016-02-02 03:57:13 +000030}
31
Chandler Carruthaaad9f82017-02-09 23:24:13 +000032void LazyCallGraph::EdgeSequence::setEdgeKind(Node &TargetN, Edge::Kind EK) {
33 Edges[EdgeIndexMap.find(&TargetN)->second].setKind(EK);
34}
35
36bool 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
46static 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
56LazyCallGraph::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 Carruth99b756d2014-04-21 05:04:24 +000060 << "' to the graph.\n");
61
Chandler Carruthaaad9f82017-02-09 23:24:13 +000062 Edges = EdgeSequence();
63
Chandler Carruthbf71a342014-02-06 04:37:03 +000064 SmallVector<Constant *, 16> Worklist;
Chandler Carrutha4499e92016-02-02 03:57:13 +000065 SmallPtrSet<Function *, 4> Callees;
Chandler Carruthbf71a342014-02-06 04:37:03 +000066 SmallPtrSet<Constant *, 16> Visited;
Chandler Carrutha4499e92016-02-02 03:57:13 +000067
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 Carruth86f0bdf2016-12-09 00:46:44 +000073 //
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 Carruthaaad9f82017-02-09 23:24:13 +000084 for (BasicBlock &BB : *F)
Chandler Carrutha4499e92016-02-02 03:57:13 +000085 for (Instruction &I : BB) {
86 if (auto CS = CallSite(&I))
87 if (Function *Callee = CS.getCalledFunction())
Chandler Carruth86f0bdf2016-12-09 00:46:44 +000088 if (!Callee->isDeclaration())
89 if (Callees.insert(Callee).second) {
90 Visited.insert(Callee);
Chandler Carruthaaad9f82017-02-09 23:24:13 +000091 addEdge(Edges->Edges, Edges->EdgeIndexMap, G->get(*Callee),
92 LazyCallGraph::Edge::Call);
Chandler Carruth86f0bdf2016-12-09 00:46:44 +000093 }
Chandler Carrutha4499e92016-02-02 03:57:13 +000094
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +000095 for (Value *Op : I.operand_values())
Chandler Carruth1583e992014-03-03 10:42:58 +000096 if (Constant *C = dyn_cast<Constant>(Op))
David Blaikie70573dc2014-11-19 07:49:26 +000097 if (Visited.insert(C).second)
Chandler Carruthbf71a342014-02-06 04:37:03 +000098 Worklist.push_back(C);
Chandler Carrutha4499e92016-02-02 03:57:13 +000099 }
Chandler Carruthbf71a342014-02-06 04:37:03 +0000100
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 Carruth88823462016-08-24 09:37:14 +0000104 visitReferences(Worklist, Visited, [&](Function &F) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000105 addEdge(Edges->Edges, Edges->EdgeIndexMap, G->get(F),
106 LazyCallGraph::Edge::Ref);
Chandler Carruth88823462016-08-24 09:37:14 +0000107 });
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000108
Chandler Carruthf59a8382017-07-15 08:08:19 +0000109 // Add implicit reference edges to any defined libcall functions (if we
110 // haven't found an explicit edge).
111 for (auto *F : G->LibFunctions)
112 if (!Visited.count(F))
113 addEdge(Edges->Edges, Edges->EdgeIndexMap, G->get(*F),
114 LazyCallGraph::Edge::Ref);
115
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000116 return *Edges;
Chandler Carruthbf71a342014-02-06 04:37:03 +0000117}
118
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000119void LazyCallGraph::Node::replaceFunction(Function &NewF) {
120 assert(F != &NewF && "Must not replace a function with itself!");
121 F = &NewF;
Chandler Carruthaa839b22014-04-27 01:59:50 +0000122}
123
Matthias Braun8c209aa2017-01-28 02:02:38 +0000124#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
125LLVM_DUMP_METHOD void LazyCallGraph::Node::dump() const {
Chandler Carruthdca83402016-06-27 23:26:08 +0000126 dbgs() << *this << '\n';
127}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000128#endif
Chandler Carruthdca83402016-06-27 23:26:08 +0000129
Chandler Carruthf59a8382017-07-15 08:08:19 +0000130static bool isKnownLibFunction(Function &F, TargetLibraryInfo &TLI) {
131 LibFunc LF;
132
133 // Either this is a normal library function or a "vectorizable" function.
134 return TLI.getLibFunc(F, LF) || TLI.isFunctionVectorizable(F.getName());
135}
136
137LazyCallGraph::LazyCallGraph(Module &M, TargetLibraryInfo &TLI) {
Chandler Carruth99b756d2014-04-21 05:04:24 +0000138 DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier()
139 << "\n");
Chandler Carruthf59a8382017-07-15 08:08:19 +0000140 for (Function &F : M) {
141 if (F.isDeclaration())
142 continue;
143 // If this function is a known lib function to LLVM then we want to
144 // synthesize reference edges to it to model the fact that LLVM can turn
145 // arbitrary code into a library function call.
146 if (isKnownLibFunction(F, TLI))
Chandler Carruth06a86302017-07-19 04:12:25 +0000147 LibFunctions.insert(&F);
Chandler Carruthf59a8382017-07-15 08:08:19 +0000148
149 if (F.hasLocalLinkage())
150 continue;
151
152 // External linkage defined functions have edges to them from other
153 // modules.
154 DEBUG(dbgs() << " Adding '" << F.getName()
155 << "' to entry set of the graph.\n");
156 addEdge(EntryEdges.Edges, EntryEdges.EdgeIndexMap, get(F), Edge::Ref);
157 }
Chandler Carruthbf71a342014-02-06 04:37:03 +0000158
159 // Now add entry nodes for functions reachable via initializers to globals.
160 SmallVector<Constant *, 16> Worklist;
161 SmallPtrSet<Constant *, 16> Visited;
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000162 for (GlobalVariable &GV : M.globals())
163 if (GV.hasInitializer())
David Blaikie70573dc2014-11-19 07:49:26 +0000164 if (Visited.insert(GV.getInitializer()).second)
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000165 Worklist.push_back(GV.getInitializer());
Chandler Carruthbf71a342014-02-06 04:37:03 +0000166
Chandler Carruth99b756d2014-04-21 05:04:24 +0000167 DEBUG(dbgs() << " Adding functions referenced by global initializers to the "
168 "entry set.\n");
Chandler Carruth88823462016-08-24 09:37:14 +0000169 visitReferences(Worklist, Visited, [&](Function &F) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000170 addEdge(EntryEdges.Edges, EntryEdges.EdgeIndexMap, get(F),
171 LazyCallGraph::Edge::Ref);
Chandler Carruth88823462016-08-24 09:37:14 +0000172 });
Chandler Carruthbf71a342014-02-06 04:37:03 +0000173}
174
Chandler Carruthbf71a342014-02-06 04:37:03 +0000175LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
Chandler Carruth2174f442014-04-18 20:44:16 +0000176 : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)),
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000177 EntryEdges(std::move(G.EntryEdges)), SCCBPA(std::move(G.SCCBPA)),
Chandler Carruthf59a8382017-07-15 08:08:19 +0000178 SCCMap(std::move(G.SCCMap)), LeafRefSCCs(std::move(G.LeafRefSCCs)),
179 LibFunctions(std::move(G.LibFunctions)) {
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000180 updateGraphPtrs();
181}
182
183LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) {
184 BPA = std::move(G.BPA);
Chandler Carruth2174f442014-04-18 20:44:16 +0000185 NodeMap = std::move(G.NodeMap);
Chandler Carrutha4499e92016-02-02 03:57:13 +0000186 EntryEdges = std::move(G.EntryEdges);
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000187 SCCBPA = std::move(G.SCCBPA);
188 SCCMap = std::move(G.SCCMap);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000189 LeafRefSCCs = std::move(G.LeafRefSCCs);
Chandler Carruthf59a8382017-07-15 08:08:19 +0000190 LibFunctions = std::move(G.LibFunctions);
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000191 updateGraphPtrs();
192 return *this;
193}
194
Matthias Braun8c209aa2017-01-28 02:02:38 +0000195#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
196LLVM_DUMP_METHOD void LazyCallGraph::SCC::dump() const {
Chandler Carruthdca83402016-06-27 23:26:08 +0000197 dbgs() << *this << '\n';
198}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000199#endif
Chandler Carruthdca83402016-06-27 23:26:08 +0000200
Chandler Carruthe5944d92016-02-17 00:18:16 +0000201#ifndef NDEBUG
202void LazyCallGraph::SCC::verify() {
203 assert(OuterRefSCC && "Can't have a null RefSCC!");
204 assert(!Nodes.empty() && "Can't have an empty SCC!");
Chandler Carruth8f92d6d2014-04-26 01:03:46 +0000205
Chandler Carruthe5944d92016-02-17 00:18:16 +0000206 for (Node *N : Nodes) {
207 assert(N && "Can't have a null node!");
208 assert(OuterRefSCC->G->lookupSCC(*N) == this &&
209 "Node does not map to this SCC!");
210 assert(N->DFSNumber == -1 &&
211 "Must set DFS numbers to -1 when adding a node to an SCC!");
212 assert(N->LowLink == -1 &&
213 "Must set low link to -1 when adding a node to an SCC!");
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000214 for (Edge &E : **N)
215 assert(E.getNode() && "Can't have an unpopulated node!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000216 }
217}
218#endif
219
Chandler Carruthbae595b2016-11-22 19:23:31 +0000220bool LazyCallGraph::SCC::isParentOf(const SCC &C) const {
221 if (this == &C)
222 return false;
223
224 for (Node &N : *this)
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000225 for (Edge &E : N->calls())
226 if (OuterRefSCC->G->lookupSCC(E.getNode()) == &C)
227 return true;
Chandler Carruthbae595b2016-11-22 19:23:31 +0000228
229 // No edges found.
230 return false;
231}
232
233bool LazyCallGraph::SCC::isAncestorOf(const SCC &TargetC) const {
234 if (this == &TargetC)
235 return false;
236
237 LazyCallGraph &G = *OuterRefSCC->G;
238
239 // Start with this SCC.
240 SmallPtrSet<const SCC *, 16> Visited = {this};
241 SmallVector<const SCC *, 16> Worklist = {this};
242
243 // Walk down the graph until we run out of edges or find a path to TargetC.
244 do {
245 const SCC &C = *Worklist.pop_back_val();
246 for (Node &N : C)
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000247 for (Edge &E : N->calls()) {
248 SCC *CalleeC = G.lookupSCC(E.getNode());
Chandler Carruthbae595b2016-11-22 19:23:31 +0000249 if (!CalleeC)
250 continue;
251
252 // If the callee's SCC is the TargetC, we're done.
253 if (CalleeC == &TargetC)
254 return true;
255
256 // If this is the first time we've reached this SCC, put it on the
257 // worklist to recurse through.
258 if (Visited.insert(CalleeC).second)
259 Worklist.push_back(CalleeC);
260 }
261 } while (!Worklist.empty());
262
263 // No paths found.
264 return false;
265}
266
Chandler Carruthe5944d92016-02-17 00:18:16 +0000267LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {}
268
Matthias Braun8c209aa2017-01-28 02:02:38 +0000269#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
270LLVM_DUMP_METHOD void LazyCallGraph::RefSCC::dump() const {
Chandler Carruthdca83402016-06-27 23:26:08 +0000271 dbgs() << *this << '\n';
272}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000273#endif
Chandler Carruthdca83402016-06-27 23:26:08 +0000274
Chandler Carruthe5944d92016-02-17 00:18:16 +0000275#ifndef NDEBUG
276void LazyCallGraph::RefSCC::verify() {
277 assert(G && "Can't have a null graph!");
278 assert(!SCCs.empty() && "Can't have an empty SCC!");
279
280 // Verify basic properties of the SCCs.
Chandler Carruth88823462016-08-24 09:37:14 +0000281 SmallPtrSet<SCC *, 4> SCCSet;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000282 for (SCC *C : SCCs) {
283 assert(C && "Can't have a null SCC!");
284 C->verify();
285 assert(&C->getOuterRefSCC() == this &&
286 "SCC doesn't think it is inside this RefSCC!");
Chandler Carruth88823462016-08-24 09:37:14 +0000287 bool Inserted = SCCSet.insert(C).second;
288 assert(Inserted && "Found a duplicate SCC!");
Chandler Carruth23a6c3f2016-12-06 10:29:23 +0000289 auto IndexIt = SCCIndices.find(C);
290 assert(IndexIt != SCCIndices.end() &&
291 "Found an SCC that doesn't have an index!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000292 }
293
294 // Check that our indices map correctly.
295 for (auto &SCCIndexPair : SCCIndices) {
296 SCC *C = SCCIndexPair.first;
297 int i = SCCIndexPair.second;
298 assert(C && "Can't have a null SCC in the indices!");
Chandler Carruth88823462016-08-24 09:37:14 +0000299 assert(SCCSet.count(C) && "Found an index for an SCC not in the RefSCC!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000300 assert(SCCs[i] == C && "Index doesn't point to SCC!");
301 }
302
303 // Check that the SCCs are in fact in post-order.
304 for (int i = 0, Size = SCCs.size(); i < Size; ++i) {
305 SCC &SourceSCC = *SCCs[i];
306 for (Node &N : SourceSCC)
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000307 for (Edge &E : *N) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000308 if (!E.isCall())
309 continue;
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000310 SCC &TargetSCC = *G->lookupSCC(E.getNode());
Chandler Carruthe5944d92016-02-17 00:18:16 +0000311 if (&TargetSCC.getOuterRefSCC() == this) {
312 assert(SCCIndices.find(&TargetSCC)->second <= i &&
313 "Edge between SCCs violates post-order relationship.");
314 continue;
315 }
316 assert(TargetSCC.getOuterRefSCC().Parents.count(this) &&
317 "Edge to a RefSCC missing us in its parent set.");
318 }
319 }
Chandler Carruth5205c352016-12-07 01:42:40 +0000320
321 // Check that our parents are actually parents.
322 for (RefSCC *ParentRC : Parents) {
323 assert(ParentRC != this && "Cannot be our own parent!");
324 auto HasConnectingEdge = [&] {
325 for (SCC &C : *ParentRC)
326 for (Node &N : C)
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000327 for (Edge &E : *N)
328 if (G->lookupRefSCC(E.getNode()) == this)
Chandler Carruth5205c352016-12-07 01:42:40 +0000329 return true;
330 return false;
331 };
332 assert(HasConnectingEdge() && "No edge connects the parent to us!");
333 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000334}
335#endif
336
337bool LazyCallGraph::RefSCC::isDescendantOf(const RefSCC &C) const {
Chandler Carruth4b096742014-05-01 12:12:42 +0000338 // Walk up the parents of this SCC and verify that we eventually find C.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000339 SmallVector<const RefSCC *, 4> AncestorWorklist;
Chandler Carruth4b096742014-05-01 12:12:42 +0000340 AncestorWorklist.push_back(this);
341 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000342 const RefSCC *AncestorC = AncestorWorklist.pop_back_val();
Chandler Carruth4b096742014-05-01 12:12:42 +0000343 if (AncestorC->isChildOf(C))
344 return true;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000345 for (const RefSCC *ParentC : AncestorC->Parents)
Chandler Carruth4b096742014-05-01 12:12:42 +0000346 AncestorWorklist.push_back(ParentC);
347 } while (!AncestorWorklist.empty());
348
349 return false;
350}
351
Chandler Carruth1f621f02016-09-04 08:34:24 +0000352/// Generic helper that updates a postorder sequence of SCCs for a potentially
353/// cycle-introducing edge insertion.
354///
355/// A postorder sequence of SCCs of a directed graph has one fundamental
356/// property: all deges in the DAG of SCCs point "up" the sequence. That is,
357/// all edges in the SCC DAG point to prior SCCs in the sequence.
358///
359/// This routine both updates a postorder sequence and uses that sequence to
360/// compute the set of SCCs connected into a cycle. It should only be called to
361/// insert a "downward" edge which will require changing the sequence to
362/// restore it to a postorder.
363///
364/// When inserting an edge from an earlier SCC to a later SCC in some postorder
365/// sequence, all of the SCCs which may be impacted are in the closed range of
366/// those two within the postorder sequence. The algorithm used here to restore
367/// the state is as follows:
368///
369/// 1) Starting from the source SCC, construct a set of SCCs which reach the
370/// source SCC consisting of just the source SCC. Then scan toward the
371/// target SCC in postorder and for each SCC, if it has an edge to an SCC
372/// in the set, add it to the set. Otherwise, the source SCC is not
373/// a successor, move it in the postorder sequence to immediately before
374/// the source SCC, shifting the source SCC and all SCCs in the set one
375/// position toward the target SCC. Stop scanning after processing the
376/// target SCC.
377/// 2) If the source SCC is now past the target SCC in the postorder sequence,
378/// and thus the new edge will flow toward the start, we are done.
379/// 3) Otherwise, starting from the target SCC, walk all edges which reach an
380/// SCC between the source and the target, and add them to the set of
381/// connected SCCs, then recurse through them. Once a complete set of the
382/// SCCs the target connects to is known, hoist the remaining SCCs between
383/// the source and the target to be above the target. Note that there is no
384/// need to process the source SCC, it is already known to connect.
385/// 4) At this point, all of the SCCs in the closed range between the source
386/// SCC and the target SCC in the postorder sequence are connected,
387/// including the target SCC and the source SCC. Inserting the edge from
388/// the source SCC to the target SCC will form a cycle out of precisely
389/// these SCCs. Thus we can merge all of the SCCs in this closed range into
390/// a single SCC.
391///
392/// This process has various important properties:
393/// - Only mutates the SCCs when adding the edge actually changes the SCC
394/// structure.
395/// - Never mutates SCCs which are unaffected by the change.
396/// - Updates the postorder sequence to correctly satisfy the postorder
397/// constraint after the edge is inserted.
398/// - Only reorders SCCs in the closed postorder sequence from the source to
399/// the target, so easy to bound how much has changed even in the ordering.
400/// - Big-O is the number of edges in the closed postorder range of SCCs from
401/// source to target.
402///
403/// This helper routine, in addition to updating the postorder sequence itself
404/// will also update a map from SCCs to indices within that sequecne.
405///
406/// The sequence and the map must operate on pointers to the SCC type.
407///
408/// Two callbacks must be provided. The first computes the subset of SCCs in
409/// the postorder closed range from the source to the target which connect to
410/// the source SCC via some (transitive) set of edges. The second computes the
411/// subset of the same range which the target SCC connects to via some
412/// (transitive) set of edges. Both callbacks should populate the set argument
413/// provided.
414template <typename SCCT, typename PostorderSequenceT, typename SCCIndexMapT,
415 typename ComputeSourceConnectedSetCallableT,
416 typename ComputeTargetConnectedSetCallableT>
417static iterator_range<typename PostorderSequenceT::iterator>
418updatePostorderSequenceForEdgeInsertion(
419 SCCT &SourceSCC, SCCT &TargetSCC, PostorderSequenceT &SCCs,
420 SCCIndexMapT &SCCIndices,
421 ComputeSourceConnectedSetCallableT ComputeSourceConnectedSet,
422 ComputeTargetConnectedSetCallableT ComputeTargetConnectedSet) {
423 int SourceIdx = SCCIndices[&SourceSCC];
424 int TargetIdx = SCCIndices[&TargetSCC];
425 assert(SourceIdx < TargetIdx && "Cannot have equal indices here!");
426
427 SmallPtrSet<SCCT *, 4> ConnectedSet;
428
429 // Compute the SCCs which (transitively) reach the source.
430 ComputeSourceConnectedSet(ConnectedSet);
431
432 // Partition the SCCs in this part of the port-order sequence so only SCCs
433 // connecting to the source remain between it and the target. This is
434 // a benign partition as it preserves postorder.
435 auto SourceI = std::stable_partition(
436 SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx + 1,
437 [&ConnectedSet](SCCT *C) { return !ConnectedSet.count(C); });
438 for (int i = SourceIdx, e = TargetIdx + 1; i < e; ++i)
439 SCCIndices.find(SCCs[i])->second = i;
440
441 // If the target doesn't connect to the source, then we've corrected the
442 // post-order and there are no cycles formed.
443 if (!ConnectedSet.count(&TargetSCC)) {
444 assert(SourceI > (SCCs.begin() + SourceIdx) &&
445 "Must have moved the source to fix the post-order.");
446 assert(*std::prev(SourceI) == &TargetSCC &&
447 "Last SCC to move should have bene the target.");
448
449 // Return an empty range at the target SCC indicating there is nothing to
450 // merge.
451 return make_range(std::prev(SourceI), std::prev(SourceI));
452 }
453
454 assert(SCCs[TargetIdx] == &TargetSCC &&
455 "Should not have moved target if connected!");
456 SourceIdx = SourceI - SCCs.begin();
457 assert(SCCs[SourceIdx] == &SourceSCC &&
458 "Bad updated index computation for the source SCC!");
459
460
461 // See whether there are any remaining intervening SCCs between the source
462 // and target. If so we need to make sure they all are reachable form the
463 // target.
464 if (SourceIdx + 1 < TargetIdx) {
465 ConnectedSet.clear();
466 ComputeTargetConnectedSet(ConnectedSet);
467
468 // Partition SCCs so that only SCCs reached from the target remain between
469 // the source and the target. This preserves postorder.
470 auto TargetI = std::stable_partition(
471 SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1,
472 [&ConnectedSet](SCCT *C) { return ConnectedSet.count(C); });
473 for (int i = SourceIdx + 1, e = TargetIdx + 1; i < e; ++i)
474 SCCIndices.find(SCCs[i])->second = i;
475 TargetIdx = std::prev(TargetI) - SCCs.begin();
476 assert(SCCs[TargetIdx] == &TargetSCC &&
477 "Should always end with the target!");
478 }
479
480 // At this point, we know that connecting source to target forms a cycle
481 // because target connects back to source, and we know that all of the SCCs
482 // between the source and target in the postorder sequence participate in that
483 // cycle.
484 return make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx);
485}
486
Chandler Carruthc213c672017-07-09 13:45:11 +0000487bool
488LazyCallGraph::RefSCC::switchInternalEdgeToCall(
489 Node &SourceN, Node &TargetN,
490 function_ref<void(ArrayRef<SCC *> MergeSCCs)> MergeCB) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000491 assert(!(*SourceN)[TargetN].isCall() && "Must start with a ref edge!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000492 SmallVector<SCC *, 1> DeletedSCCs;
Chandler Carruth5217c942014-04-30 10:48:36 +0000493
Chandler Carruth11b3f602016-09-04 08:34:31 +0000494#ifndef NDEBUG
495 // In a debug build, verify the RefSCC is valid to start with and when this
496 // routine finishes.
497 verify();
498 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
499#endif
500
Chandler Carruthe5944d92016-02-17 00:18:16 +0000501 SCC &SourceSCC = *G->lookupSCC(SourceN);
502 SCC &TargetSCC = *G->lookupSCC(TargetN);
503
504 // If the two nodes are already part of the same SCC, we're also done as
505 // we've just added more connectivity.
506 if (&SourceSCC == &TargetSCC) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000507 SourceN->setEdgeKind(TargetN, Edge::Call);
Chandler Carruthc213c672017-07-09 13:45:11 +0000508 return false; // No new cycle.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000509 }
510
511 // At this point we leverage the postorder list of SCCs to detect when the
512 // insertion of an edge changes the SCC structure in any way.
513 //
514 // First and foremost, we can eliminate the need for any changes when the
515 // edge is toward the beginning of the postorder sequence because all edges
516 // flow in that direction already. Thus adding a new one cannot form a cycle.
517 int SourceIdx = SCCIndices[&SourceSCC];
518 int TargetIdx = SCCIndices[&TargetSCC];
519 if (TargetIdx < SourceIdx) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000520 SourceN->setEdgeKind(TargetN, Edge::Call);
Chandler Carruthc213c672017-07-09 13:45:11 +0000521 return false; // No new cycle.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000522 }
523
Chandler Carruthe5944d92016-02-17 00:18:16 +0000524 // Compute the SCCs which (transitively) reach the source.
Chandler Carruth1f621f02016-09-04 08:34:24 +0000525 auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000526#ifndef NDEBUG
Chandler Carruth1f621f02016-09-04 08:34:24 +0000527 // Check that the RefSCC is still valid before computing this as the
528 // results will be nonsensical of we've broken its invariants.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000529 verify();
530#endif
Chandler Carruth1f621f02016-09-04 08:34:24 +0000531 ConnectedSet.insert(&SourceSCC);
532 auto IsConnected = [&](SCC &C) {
533 for (Node &N : C)
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000534 for (Edge &E : N->calls())
535 if (ConnectedSet.count(G->lookupSCC(E.getNode())))
Chandler Carruth1f621f02016-09-04 08:34:24 +0000536 return true;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000537
Chandler Carruth1f621f02016-09-04 08:34:24 +0000538 return false;
539 };
Chandler Carruthe5944d92016-02-17 00:18:16 +0000540
Chandler Carruth1f621f02016-09-04 08:34:24 +0000541 for (SCC *C :
542 make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1))
543 if (IsConnected(*C))
544 ConnectedSet.insert(C);
545 };
546
547 // Use a normal worklist to find which SCCs the target connects to. We still
548 // bound the search based on the range in the postorder list we care about,
549 // but because this is forward connectivity we just "recurse" through the
550 // edges.
551 auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000552#ifndef NDEBUG
Chandler Carruth1f621f02016-09-04 08:34:24 +0000553 // Check that the RefSCC is still valid before computing this as the
554 // results will be nonsensical of we've broken its invariants.
555 verify();
Chandler Carruthe5944d92016-02-17 00:18:16 +0000556#endif
Chandler Carruthe5944d92016-02-17 00:18:16 +0000557 ConnectedSet.insert(&TargetSCC);
558 SmallVector<SCC *, 4> Worklist;
559 Worklist.push_back(&TargetSCC);
560 do {
561 SCC &C = *Worklist.pop_back_val();
562 for (Node &N : C)
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000563 for (Edge &E : *N) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000564 if (!E.isCall())
565 continue;
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000566 SCC &EdgeC = *G->lookupSCC(E.getNode());
Chandler Carruthe5944d92016-02-17 00:18:16 +0000567 if (&EdgeC.getOuterRefSCC() != this)
568 // Not in this RefSCC...
569 continue;
570 if (SCCIndices.find(&EdgeC)->second <= SourceIdx)
571 // Not in the postorder sequence between source and target.
572 continue;
573
574 if (ConnectedSet.insert(&EdgeC).second)
575 Worklist.push_back(&EdgeC);
576 }
577 } while (!Worklist.empty());
Chandler Carruth1f621f02016-09-04 08:34:24 +0000578 };
Chandler Carruthe5944d92016-02-17 00:18:16 +0000579
Chandler Carruth1f621f02016-09-04 08:34:24 +0000580 // Use a generic helper to update the postorder sequence of SCCs and return
581 // a range of any SCCs connected into a cycle by inserting this edge. This
582 // routine will also take care of updating the indices into the postorder
583 // sequence.
584 auto MergeRange = updatePostorderSequenceForEdgeInsertion(
585 SourceSCC, TargetSCC, SCCs, SCCIndices, ComputeSourceConnectedSet,
586 ComputeTargetConnectedSet);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000587
Chandler Carruthc213c672017-07-09 13:45:11 +0000588 // Run the user's callback on the merged SCCs before we actually merge them.
589 if (MergeCB)
590 MergeCB(makeArrayRef(MergeRange.begin(), MergeRange.end()));
591
Chandler Carruth1f621f02016-09-04 08:34:24 +0000592 // If the merge range is empty, then adding the edge didn't actually form any
593 // new cycles. We're done.
594 if (MergeRange.begin() == MergeRange.end()) {
595 // Now that the SCC structure is finalized, flip the kind to call.
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000596 SourceN->setEdgeKind(TargetN, Edge::Call);
Chandler Carruthc213c672017-07-09 13:45:11 +0000597 return false; // No new cycle.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000598 }
599
Chandler Carruth1f621f02016-09-04 08:34:24 +0000600#ifndef NDEBUG
601 // Before merging, check that the RefSCC remains valid after all the
602 // postorder updates.
603 verify();
604#endif
605
606 // Otherwise we need to merge all of the SCCs in the cycle into a single
Chandler Carruthe5944d92016-02-17 00:18:16 +0000607 // result SCC.
608 //
609 // NB: We merge into the target because all of these functions were already
610 // reachable from the target, meaning any SCC-wide properties deduced about it
611 // other than the set of functions within it will not have changed.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000612 for (SCC *C : MergeRange) {
613 assert(C != &TargetSCC &&
614 "We merge *into* the target and shouldn't process it here!");
615 SCCIndices.erase(C);
616 TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end());
617 for (Node *N : C->Nodes)
618 G->SCCMap[N] = &TargetSCC;
619 C->clear();
620 DeletedSCCs.push_back(C);
621 }
622
623 // Erase the merged SCCs from the list and update the indices of the
624 // remaining SCCs.
625 int IndexOffset = MergeRange.end() - MergeRange.begin();
626 auto EraseEnd = SCCs.erase(MergeRange.begin(), MergeRange.end());
627 for (SCC *C : make_range(EraseEnd, SCCs.end()))
628 SCCIndices[C] -= IndexOffset;
629
630 // Now that the SCC structure is finalized, flip the kind to call.
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000631 SourceN->setEdgeKind(TargetN, Edge::Call);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000632
Chandler Carruthc213c672017-07-09 13:45:11 +0000633 // And we're done, but we did form a new cycle.
634 return true;
Chandler Carruth5217c942014-04-30 10:48:36 +0000635}
636
Chandler Carruth443e57e2016-12-28 10:34:50 +0000637void LazyCallGraph::RefSCC::switchTrivialInternalEdgeToRef(Node &SourceN,
638 Node &TargetN) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000639 assert((*SourceN)[TargetN].isCall() && "Must start with a call edge!");
Chandler Carruth443e57e2016-12-28 10:34:50 +0000640
641#ifndef NDEBUG
642 // In a debug build, verify the RefSCC is valid to start with and when this
643 // routine finishes.
644 verify();
645 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
646#endif
647
648 assert(G->lookupRefSCC(SourceN) == this &&
649 "Source must be in this RefSCC.");
650 assert(G->lookupRefSCC(TargetN) == this &&
651 "Target must be in this RefSCC.");
652 assert(G->lookupSCC(SourceN) != G->lookupSCC(TargetN) &&
653 "Source and Target must be in separate SCCs for this to be trivial!");
654
655 // Set the edge kind.
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000656 SourceN->setEdgeKind(TargetN, Edge::Ref);
Chandler Carruth443e57e2016-12-28 10:34:50 +0000657}
658
Chandler Carruth88823462016-08-24 09:37:14 +0000659iterator_range<LazyCallGraph::RefSCC::iterator>
660LazyCallGraph::RefSCC::switchInternalEdgeToRef(Node &SourceN, Node &TargetN) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000661 assert((*SourceN)[TargetN].isCall() && "Must start with a call edge!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000662
Chandler Carruth11b3f602016-09-04 08:34:31 +0000663#ifndef NDEBUG
664 // In a debug build, verify the RefSCC is valid to start with and when this
665 // routine finishes.
666 verify();
667 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
668#endif
669
Chandler Carruth443e57e2016-12-28 10:34:50 +0000670 assert(G->lookupRefSCC(SourceN) == this &&
Chandler Carruthe5944d92016-02-17 00:18:16 +0000671 "Source must be in this RefSCC.");
Chandler Carruth443e57e2016-12-28 10:34:50 +0000672 assert(G->lookupRefSCC(TargetN) == this &&
Chandler Carruthe5944d92016-02-17 00:18:16 +0000673 "Target must be in this RefSCC.");
674
Chandler Carruth443e57e2016-12-28 10:34:50 +0000675 SCC &TargetSCC = *G->lookupSCC(TargetN);
676 assert(G->lookupSCC(SourceN) == &TargetSCC && "Source and Target must be in "
677 "the same SCC to require the "
678 "full CG update.");
679
Chandler Carruthe5944d92016-02-17 00:18:16 +0000680 // Set the edge kind.
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000681 SourceN->setEdgeKind(TargetN, Edge::Ref);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000682
Chandler Carruthe5944d92016-02-17 00:18:16 +0000683 // Otherwise we are removing a call edge from a single SCC. This may break
684 // the cycle. In order to compute the new set of SCCs, we need to do a small
685 // DFS over the nodes within the SCC to form any sub-cycles that remain as
686 // distinct SCCs and compute a postorder over the resulting SCCs.
687 //
688 // However, we specially handle the target node. The target node is known to
689 // reach all other nodes in the original SCC by definition. This means that
690 // we want the old SCC to be replaced with an SCC contaning that node as it
691 // will be the root of whatever SCC DAG results from the DFS. Assumptions
692 // about an SCC such as the set of functions called will continue to hold,
693 // etc.
694
695 SCC &OldSCC = TargetSCC;
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000696 SmallVector<std::pair<Node *, EdgeSequence::call_iterator>, 16> DFSStack;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000697 SmallVector<Node *, 16> PendingSCCStack;
698 SmallVector<SCC *, 4> NewSCCs;
699
700 // Prepare the nodes for a fresh DFS.
701 SmallVector<Node *, 16> Worklist;
702 Worklist.swap(OldSCC.Nodes);
703 for (Node *N : Worklist) {
704 N->DFSNumber = N->LowLink = 0;
705 G->SCCMap.erase(N);
706 }
707
708 // Force the target node to be in the old SCC. This also enables us to take
709 // a very significant short-cut in the standard Tarjan walk to re-form SCCs
710 // below: whenever we build an edge that reaches the target node, we know
711 // that the target node eventually connects back to all other nodes in our
712 // walk. As a consequence, we can detect and handle participants in that
713 // cycle without walking all the edges that form this connection, and instead
714 // by relying on the fundamental guarantee coming into this operation (all
715 // nodes are reachable from the target due to previously forming an SCC).
716 TargetN.DFSNumber = TargetN.LowLink = -1;
717 OldSCC.Nodes.push_back(&TargetN);
718 G->SCCMap[&TargetN] = &OldSCC;
719
720 // Scan down the stack and DFS across the call edges.
721 for (Node *RootN : Worklist) {
722 assert(DFSStack.empty() &&
723 "Cannot begin a new root with a non-empty DFS stack!");
724 assert(PendingSCCStack.empty() &&
725 "Cannot begin a new root with pending nodes for an SCC!");
726
727 // Skip any nodes we've already reached in the DFS.
728 if (RootN->DFSNumber != 0) {
729 assert(RootN->DFSNumber == -1 &&
730 "Shouldn't have any mid-DFS root nodes!");
731 continue;
732 }
733
734 RootN->DFSNumber = RootN->LowLink = 1;
735 int NextDFSNumber = 2;
736
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000737 DFSStack.push_back({RootN, (*RootN)->call_begin()});
Chandler Carruthe5944d92016-02-17 00:18:16 +0000738 do {
739 Node *N;
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000740 EdgeSequence::call_iterator I;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000741 std::tie(N, I) = DFSStack.pop_back_val();
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000742 auto E = (*N)->call_end();
Chandler Carruthe5944d92016-02-17 00:18:16 +0000743 while (I != E) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000744 Node &ChildN = I->getNode();
Chandler Carruthe5944d92016-02-17 00:18:16 +0000745 if (ChildN.DFSNumber == 0) {
746 // We haven't yet visited this child, so descend, pushing the current
747 // node onto the stack.
748 DFSStack.push_back({N, I});
749
750 assert(!G->SCCMap.count(&ChildN) &&
751 "Found a node with 0 DFS number but already in an SCC!");
752 ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
753 N = &ChildN;
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000754 I = (*N)->call_begin();
755 E = (*N)->call_end();
Chandler Carruthe5944d92016-02-17 00:18:16 +0000756 continue;
757 }
758
759 // Check for the child already being part of some component.
760 if (ChildN.DFSNumber == -1) {
761 if (G->lookupSCC(ChildN) == &OldSCC) {
762 // If the child is part of the old SCC, we know that it can reach
763 // every other node, so we have formed a cycle. Pull the entire DFS
764 // and pending stacks into it. See the comment above about setting
765 // up the old SCC for why we do this.
766 int OldSize = OldSCC.size();
767 OldSCC.Nodes.push_back(N);
768 OldSCC.Nodes.append(PendingSCCStack.begin(), PendingSCCStack.end());
769 PendingSCCStack.clear();
770 while (!DFSStack.empty())
771 OldSCC.Nodes.push_back(DFSStack.pop_back_val().first);
772 for (Node &N : make_range(OldSCC.begin() + OldSize, OldSCC.end())) {
773 N.DFSNumber = N.LowLink = -1;
774 G->SCCMap[&N] = &OldSCC;
775 }
776 N = nullptr;
777 break;
778 }
779
780 // If the child has already been added to some child component, it
781 // couldn't impact the low-link of this parent because it isn't
782 // connected, and thus its low-link isn't relevant so skip it.
783 ++I;
784 continue;
785 }
786
787 // Track the lowest linked child as the lowest link for this node.
788 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
789 if (ChildN.LowLink < N->LowLink)
790 N->LowLink = ChildN.LowLink;
791
792 // Move to the next edge.
793 ++I;
794 }
795 if (!N)
796 // Cleared the DFS early, start another round.
797 break;
798
799 // We've finished processing N and its descendents, put it on our pending
800 // SCC stack to eventually get merged into an SCC of nodes.
801 PendingSCCStack.push_back(N);
802
803 // If this node is linked to some lower entry, continue walking up the
804 // stack.
805 if (N->LowLink != N->DFSNumber)
806 continue;
807
808 // Otherwise, we've completed an SCC. Append it to our post order list of
809 // SCCs.
810 int RootDFSNumber = N->DFSNumber;
811 // Find the range of the node stack by walking down until we pass the
812 // root DFS number.
813 auto SCCNodes = make_range(
814 PendingSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +0000815 find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
816 return N->DFSNumber < RootDFSNumber;
817 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +0000818
819 // Form a new SCC out of these nodes and then clear them off our pending
820 // stack.
821 NewSCCs.push_back(G->createSCC(*this, SCCNodes));
822 for (Node &N : *NewSCCs.back()) {
823 N.DFSNumber = N.LowLink = -1;
824 G->SCCMap[&N] = NewSCCs.back();
825 }
826 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
827 } while (!DFSStack.empty());
828 }
829
830 // Insert the remaining SCCs before the old one. The old SCC can reach all
831 // other SCCs we form because it contains the target node of the removed edge
832 // of the old SCC. This means that we will have edges into all of the new
833 // SCCs, which means the old one must come last for postorder.
834 int OldIdx = SCCIndices[&OldSCC];
835 SCCs.insert(SCCs.begin() + OldIdx, NewSCCs.begin(), NewSCCs.end());
836
837 // Update the mapping from SCC* to index to use the new SCC*s, and remove the
838 // old SCC from the mapping.
839 for (int Idx = OldIdx, Size = SCCs.size(); Idx < Size; ++Idx)
840 SCCIndices[SCCs[Idx]] = Idx;
841
Chandler Carruth88823462016-08-24 09:37:14 +0000842 return make_range(SCCs.begin() + OldIdx,
843 SCCs.begin() + OldIdx + NewSCCs.size());
Chandler Carruthe5944d92016-02-17 00:18:16 +0000844}
845
846void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN,
847 Node &TargetN) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000848 assert(!(*SourceN)[TargetN].isCall() && "Must start with a ref edge!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000849
850 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
851 assert(G->lookupRefSCC(TargetN) != this &&
852 "Target must not be in this RefSCC.");
Francis Visoiu Mistrih262ad162017-02-28 18:34:55 +0000853#ifdef EXPENSIVE_CHECKS
Chandler Carruthe5944d92016-02-17 00:18:16 +0000854 assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
855 "Target must be a descendant of the Source.");
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +0000856#endif
Chandler Carruthe5944d92016-02-17 00:18:16 +0000857
858 // Edges between RefSCCs are the same regardless of call or ref, so we can
859 // just flip the edge here.
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000860 SourceN->setEdgeKind(TargetN, Edge::Call);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000861
862#ifndef NDEBUG
863 // Check that the RefSCC is still valid.
864 verify();
865#endif
866}
867
868void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN,
869 Node &TargetN) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000870 assert((*SourceN)[TargetN].isCall() && "Must start with a call edge!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000871
872 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
873 assert(G->lookupRefSCC(TargetN) != this &&
874 "Target must not be in this RefSCC.");
Francis Visoiu Mistrih262ad162017-02-28 18:34:55 +0000875#ifdef EXPENSIVE_CHECKS
Chandler Carruthe5944d92016-02-17 00:18:16 +0000876 assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
877 "Target must be a descendant of the Source.");
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +0000878#endif
Chandler Carruthe5944d92016-02-17 00:18:16 +0000879
880 // Edges between RefSCCs are the same regardless of call or ref, so we can
881 // just flip the edge here.
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000882 SourceN->setEdgeKind(TargetN, Edge::Ref);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000883
884#ifndef NDEBUG
885 // Check that the RefSCC is still valid.
886 verify();
887#endif
888}
889
890void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN,
891 Node &TargetN) {
892 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
893 assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
894
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000895 SourceN->insertEdgeInternal(TargetN, Edge::Ref);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000896
897#ifndef NDEBUG
898 // Check that the RefSCC is still valid.
899 verify();
900#endif
901}
902
903void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN,
904 Edge::Kind EK) {
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000905 // First insert it into the caller.
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000906 SourceN->insertEdgeInternal(TargetN, EK);
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000907
Chandler Carruthe5944d92016-02-17 00:18:16 +0000908 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000909
Chandler Carruthe5944d92016-02-17 00:18:16 +0000910 RefSCC &TargetC = *G->lookupRefSCC(TargetN);
911 assert(&TargetC != this && "Target must not be in this RefSCC.");
Francis Visoiu Mistrih262ad162017-02-28 18:34:55 +0000912#ifdef EXPENSIVE_CHECKS
Chandler Carruthe5944d92016-02-17 00:18:16 +0000913 assert(TargetC.isDescendantOf(*this) &&
914 "Target must be a descendant of the Source.");
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +0000915#endif
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000916
Chandler Carruth91539112015-12-28 01:54:20 +0000917 // The only change required is to add this SCC to the parent set of the
918 // callee.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000919 TargetC.Parents.insert(this);
920
921#ifndef NDEBUG
922 // Check that the RefSCC is still valid.
923 verify();
924#endif
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000925}
926
Chandler Carruthe5944d92016-02-17 00:18:16 +0000927SmallVector<LazyCallGraph::RefSCC *, 1>
928LazyCallGraph::RefSCC::insertIncomingRefEdge(Node &SourceN, Node &TargetN) {
Chandler Carruth49d728a2016-09-16 10:20:17 +0000929 assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
930 RefSCC &SourceC = *G->lookupRefSCC(SourceN);
931 assert(&SourceC != this && "Source must not be in this RefSCC.");
Francis Visoiu Mistrih262ad162017-02-28 18:34:55 +0000932#ifdef EXPENSIVE_CHECKS
Chandler Carruth49d728a2016-09-16 10:20:17 +0000933 assert(SourceC.isDescendantOf(*this) &&
934 "Source must be a descendant of the Target.");
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +0000935#endif
Chandler Carruth49d728a2016-09-16 10:20:17 +0000936
937 SmallVector<RefSCC *, 1> DeletedRefSCCs;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000938
Chandler Carruth11b3f602016-09-04 08:34:31 +0000939#ifndef NDEBUG
940 // In a debug build, verify the RefSCC is valid to start with and when this
941 // routine finishes.
942 verify();
943 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
944#endif
945
Chandler Carruth49d728a2016-09-16 10:20:17 +0000946 int SourceIdx = G->RefSCCIndices[&SourceC];
947 int TargetIdx = G->RefSCCIndices[this];
948 assert(SourceIdx < TargetIdx &&
949 "Postorder list doesn't see edge as incoming!");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000950
Chandler Carruth49d728a2016-09-16 10:20:17 +0000951 // Compute the RefSCCs which (transitively) reach the source. We do this by
952 // working backwards from the source using the parent set in each RefSCC,
953 // skipping any RefSCCs that don't fall in the postorder range. This has the
954 // advantage of walking the sparser parent edge (in high fan-out graphs) but
955 // more importantly this removes examining all forward edges in all RefSCCs
956 // within the postorder range which aren't in fact connected. Only connected
957 // RefSCCs (and their edges) are visited here.
958 auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) {
959 Set.insert(&SourceC);
Chandler Carruth13ffd112017-08-05 03:37:37 +0000960 auto IsConnected = [&](RefSCC &RC) {
961 for (SCC &C : RC)
962 for (Node &N : C)
963 for (Edge &E : *N)
964 if (Set.count(G->lookupRefSCC(E.getNode())))
965 return true;
966
967 return false;
968 };
969
970 for (RefSCC *C : make_range(G->PostOrderRefSCCs.begin() + SourceIdx + 1,
971 G->PostOrderRefSCCs.begin() + TargetIdx + 1))
972 if (IsConnected(*C))
973 Set.insert(C);
Chandler Carruth49d728a2016-09-16 10:20:17 +0000974 };
Chandler Carruth312dddf2014-05-04 09:38:32 +0000975
Chandler Carruth49d728a2016-09-16 10:20:17 +0000976 // Use a normal worklist to find which SCCs the target connects to. We still
977 // bound the search based on the range in the postorder list we care about,
978 // but because this is forward connectivity we just "recurse" through the
979 // edges.
980 auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) {
981 Set.insert(this);
982 SmallVector<RefSCC *, 4> Worklist;
983 Worklist.push_back(this);
984 do {
985 RefSCC &RC = *Worklist.pop_back_val();
986 for (SCC &C : RC)
987 for (Node &N : C)
Chandler Carruthaaad9f82017-02-09 23:24:13 +0000988 for (Edge &E : *N) {
989 RefSCC &EdgeRC = *G->lookupRefSCC(E.getNode());
Chandler Carruth49d728a2016-09-16 10:20:17 +0000990 if (G->getRefSCCIndex(EdgeRC) <= SourceIdx)
991 // Not in the postorder sequence between source and target.
992 continue;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000993
Chandler Carruth49d728a2016-09-16 10:20:17 +0000994 if (Set.insert(&EdgeRC).second)
995 Worklist.push_back(&EdgeRC);
996 }
997 } while (!Worklist.empty());
998 };
999
1000 // Use a generic helper to update the postorder sequence of RefSCCs and return
1001 // a range of any RefSCCs connected into a cycle by inserting this edge. This
1002 // routine will also take care of updating the indices into the postorder
1003 // sequence.
1004 iterator_range<SmallVectorImpl<RefSCC *>::iterator> MergeRange =
1005 updatePostorderSequenceForEdgeInsertion(
1006 SourceC, *this, G->PostOrderRefSCCs, G->RefSCCIndices,
1007 ComputeSourceConnectedSet, ComputeTargetConnectedSet);
1008
Chandler Carruth5205c352016-12-07 01:42:40 +00001009 // Build a set so we can do fast tests for whether a RefSCC will end up as
1010 // part of the merged RefSCC.
Chandler Carruth49d728a2016-09-16 10:20:17 +00001011 SmallPtrSet<RefSCC *, 16> MergeSet(MergeRange.begin(), MergeRange.end());
Chandler Carruth312dddf2014-05-04 09:38:32 +00001012
Chandler Carruth5205c352016-12-07 01:42:40 +00001013 // This RefSCC will always be part of that set, so just insert it here.
1014 MergeSet.insert(this);
1015
Chandler Carruth312dddf2014-05-04 09:38:32 +00001016 // Now that we have identified all of the SCCs which need to be merged into
1017 // a connected set with the inserted edge, merge all of them into this SCC.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001018 SmallVector<SCC *, 16> MergedSCCs;
1019 int SCCIndex = 0;
Chandler Carruth49d728a2016-09-16 10:20:17 +00001020 for (RefSCC *RC : MergeRange) {
1021 assert(RC != this && "We're merging into the target RefSCC, so it "
1022 "shouldn't be in the range.");
Chandler Carruth312dddf2014-05-04 09:38:32 +00001023
Chandler Carruthe5944d92016-02-17 00:18:16 +00001024 // Merge the parents which aren't part of the merge into the our parents.
Chandler Carruth49d728a2016-09-16 10:20:17 +00001025 for (RefSCC *ParentRC : RC->Parents)
1026 if (!MergeSet.count(ParentRC))
1027 Parents.insert(ParentRC);
1028 RC->Parents.clear();
Chandler Carruthe5944d92016-02-17 00:18:16 +00001029
1030 // Walk the inner SCCs to update their up-pointer and walk all the edges to
1031 // update any parent sets.
1032 // FIXME: We should try to find a way to avoid this (rather expensive) edge
1033 // walk by updating the parent sets in some other manner.
Chandler Carruth49d728a2016-09-16 10:20:17 +00001034 for (SCC &InnerC : *RC) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001035 InnerC.OuterRefSCC = this;
1036 SCCIndices[&InnerC] = SCCIndex++;
1037 for (Node &N : InnerC) {
1038 G->SCCMap[&N] = &InnerC;
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001039 for (Edge &E : *N) {
1040 RefSCC &ChildRC = *G->lookupRefSCC(E.getNode());
Chandler Carruth49d728a2016-09-16 10:20:17 +00001041 if (MergeSet.count(&ChildRC))
Chandler Carruthe5944d92016-02-17 00:18:16 +00001042 continue;
Chandler Carruth49d728a2016-09-16 10:20:17 +00001043 ChildRC.Parents.erase(RC);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001044 ChildRC.Parents.insert(this);
1045 }
Chandler Carruth312dddf2014-05-04 09:38:32 +00001046 }
Chandler Carruth312dddf2014-05-04 09:38:32 +00001047 }
Chandler Carruthe5944d92016-02-17 00:18:16 +00001048
1049 // Now merge in the SCCs. We can actually move here so try to reuse storage
1050 // the first time through.
1051 if (MergedSCCs.empty())
Chandler Carruth49d728a2016-09-16 10:20:17 +00001052 MergedSCCs = std::move(RC->SCCs);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001053 else
Chandler Carruth49d728a2016-09-16 10:20:17 +00001054 MergedSCCs.append(RC->SCCs.begin(), RC->SCCs.end());
1055 RC->SCCs.clear();
1056 DeletedRefSCCs.push_back(RC);
Chandler Carruth312dddf2014-05-04 09:38:32 +00001057 }
Chandler Carruthe5944d92016-02-17 00:18:16 +00001058
Chandler Carruth49d728a2016-09-16 10:20:17 +00001059 // Append our original SCCs to the merged list and move it into place.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001060 for (SCC &InnerC : *this)
1061 SCCIndices[&InnerC] = SCCIndex++;
1062 MergedSCCs.append(SCCs.begin(), SCCs.end());
1063 SCCs = std::move(MergedSCCs);
1064
Chandler Carruth49d728a2016-09-16 10:20:17 +00001065 // Remove the merged away RefSCCs from the post order sequence.
1066 for (RefSCC *RC : MergeRange)
1067 G->RefSCCIndices.erase(RC);
1068 int IndexOffset = MergeRange.end() - MergeRange.begin();
1069 auto EraseEnd =
1070 G->PostOrderRefSCCs.erase(MergeRange.begin(), MergeRange.end());
1071 for (RefSCC *RC : make_range(EraseEnd, G->PostOrderRefSCCs.end()))
1072 G->RefSCCIndices[RC] -= IndexOffset;
1073
Chandler Carruthe5944d92016-02-17 00:18:16 +00001074 // At this point we have a merged RefSCC with a post-order SCCs list, just
1075 // connect the nodes to form the new edge.
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001076 SourceN->insertEdgeInternal(TargetN, Edge::Ref);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001077
Chandler Carruth312dddf2014-05-04 09:38:32 +00001078 // We return the list of SCCs which were merged so that callers can
1079 // invalidate any data they have associated with those SCCs. Note that these
1080 // SCCs are no longer in an interesting state (they are totally empty) but
1081 // the pointers will remain stable for the life of the graph itself.
Chandler Carruth49d728a2016-09-16 10:20:17 +00001082 return DeletedRefSCCs;
Chandler Carruth312dddf2014-05-04 09:38:32 +00001083}
1084
Chandler Carruthe5944d92016-02-17 00:18:16 +00001085void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) {
1086 assert(G->lookupRefSCC(SourceN) == this &&
1087 "The source must be a member of this RefSCC.");
1088
1089 RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
1090 assert(&TargetRC != this && "The target must not be a member of this RefSCC");
1091
David Majnemer0d955d02016-08-11 22:21:41 +00001092 assert(!is_contained(G->LeafRefSCCs, this) &&
Chandler Carruthe5944d92016-02-17 00:18:16 +00001093 "Cannot have a leaf RefSCC source.");
1094
Chandler Carruth11b3f602016-09-04 08:34:31 +00001095#ifndef NDEBUG
1096 // In a debug build, verify the RefSCC is valid to start with and when this
1097 // routine finishes.
1098 verify();
1099 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
1100#endif
1101
Chandler Carruthaa839b22014-04-27 01:59:50 +00001102 // First remove it from the node.
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001103 bool Removed = SourceN->removeEdgeInternal(TargetN);
1104 (void)Removed;
1105 assert(Removed && "Target not in the edge set for this caller?");
Chandler Carruthaa839b22014-04-27 01:59:50 +00001106
Chandler Carruthe5944d92016-02-17 00:18:16 +00001107 bool HasOtherEdgeToChildRC = false;
1108 bool HasOtherChildRC = false;
1109 for (SCC *InnerC : SCCs) {
1110 for (Node &N : *InnerC) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001111 for (Edge &E : *N) {
1112 RefSCC &OtherChildRC = *G->lookupRefSCC(E.getNode());
Chandler Carruthe5944d92016-02-17 00:18:16 +00001113 if (&OtherChildRC == &TargetRC) {
1114 HasOtherEdgeToChildRC = true;
1115 break;
1116 }
1117 if (&OtherChildRC != this)
1118 HasOtherChildRC = true;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001119 }
Chandler Carruthe5944d92016-02-17 00:18:16 +00001120 if (HasOtherEdgeToChildRC)
1121 break;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001122 }
Chandler Carruthe5944d92016-02-17 00:18:16 +00001123 if (HasOtherEdgeToChildRC)
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001124 break;
1125 }
1126 // Because the SCCs form a DAG, deleting such an edge cannot change the set
1127 // of SCCs in the graph. However, it may cut an edge of the SCC DAG, making
Chandler Carruthe5944d92016-02-17 00:18:16 +00001128 // the source SCC no longer connected to the target SCC. If so, we need to
1129 // update the target SCC's map of its parents.
1130 if (!HasOtherEdgeToChildRC) {
1131 bool Removed = TargetRC.Parents.erase(this);
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001132 (void)Removed;
1133 assert(Removed &&
Chandler Carruthe5944d92016-02-17 00:18:16 +00001134 "Did not find the source SCC in the target SCC's parent list!");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001135
1136 // It may orphan an SCC if it is the last edge reaching it, but that does
1137 // not violate any invariants of the graph.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001138 if (TargetRC.Parents.empty())
1139 DEBUG(dbgs() << "LCG: Update removing " << SourceN.getFunction().getName()
1140 << " -> " << TargetN.getFunction().getName()
Chandler Carruthaa839b22014-04-27 01:59:50 +00001141 << " edge orphaned the callee's SCC!\n");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001142
Chandler Carruthe5944d92016-02-17 00:18:16 +00001143 // It may make the Source SCC a leaf SCC.
1144 if (!HasOtherChildRC)
1145 G->LeafRefSCCs.push_back(this);
Chandler Carruthaca48d02014-04-26 09:06:53 +00001146 }
1147}
1148
Chandler Carruthe5944d92016-02-17 00:18:16 +00001149SmallVector<LazyCallGraph::RefSCC *, 1>
1150LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN, Node &TargetN) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001151 assert(!(*SourceN)[TargetN].isCall() &&
Chandler Carruthe5944d92016-02-17 00:18:16 +00001152 "Cannot remove a call edge, it must first be made a ref edge");
Chandler Carruthaa839b22014-04-27 01:59:50 +00001153
Chandler Carruth11b3f602016-09-04 08:34:31 +00001154#ifndef NDEBUG
1155 // In a debug build, verify the RefSCC is valid to start with and when this
1156 // routine finishes.
1157 verify();
1158 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
1159#endif
1160
Chandler Carruthe5944d92016-02-17 00:18:16 +00001161 // First remove the actual edge.
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001162 bool Removed = SourceN->removeEdgeInternal(TargetN);
1163 (void)Removed;
1164 assert(Removed && "Target not in the edge set for this caller?");
Chandler Carruthe5944d92016-02-17 00:18:16 +00001165
1166 // We return a list of the resulting *new* RefSCCs in post-order.
1167 SmallVector<RefSCC *, 1> Result;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001168
Chandler Carrutha7205b62014-04-26 03:36:37 +00001169 // Direct recursion doesn't impact the SCC graph at all.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001170 if (&SourceN == &TargetN)
1171 return Result;
Chandler Carrutha7205b62014-04-26 03:36:37 +00001172
Chandler Carruthc6334572016-12-28 02:24:58 +00001173 // If this ref edge is within an SCC then there are sufficient other edges to
1174 // form a cycle without this edge so removing it is a no-op.
1175 SCC &SourceC = *G->lookupSCC(SourceN);
1176 SCC &TargetC = *G->lookupSCC(TargetN);
1177 if (&SourceC == &TargetC)
1178 return Result;
1179
Chandler Carruthe5944d92016-02-17 00:18:16 +00001180 // We build somewhat synthetic new RefSCCs by providing a postorder mapping
1181 // for each inner SCC. We also store these associated with *nodes* rather
1182 // than SCCs because this saves a round-trip through the node->SCC map and in
1183 // the common case, SCCs are small. We will verify that we always give the
1184 // same number to every node in the SCC such that these are equivalent.
1185 const int RootPostOrderNumber = 0;
1186 int PostOrderNumber = RootPostOrderNumber + 1;
1187 SmallDenseMap<Node *, int> PostOrderMapping;
1188
1189 // Every node in the target SCC can already reach every node in this RefSCC
1190 // (by definition). It is the only node we know will stay inside this RefSCC.
1191 // Everything which transitively reaches Target will also remain in the
1192 // RefSCC. We handle this by pre-marking that the nodes in the target SCC map
1193 // back to the root post order number.
1194 //
1195 // This also enables us to take a very significant short-cut in the standard
1196 // Tarjan walk to re-form RefSCCs below: whenever we build an edge that
1197 // references the target node, we know that the target node eventually
1198 // references all other nodes in our walk. As a consequence, we can detect
1199 // and handle participants in that cycle without walking all the edges that
1200 // form the connections, and instead by relying on the fundamental guarantee
1201 // coming into this operation.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001202 for (Node &N : TargetC)
1203 PostOrderMapping[&N] = RootPostOrderNumber;
1204
1205 // Reset all the other nodes to prepare for a DFS over them, and add them to
1206 // our worklist.
1207 SmallVector<Node *, 8> Worklist;
1208 for (SCC *C : SCCs) {
1209 if (C == &TargetC)
1210 continue;
1211
1212 for (Node &N : *C)
1213 N.DFSNumber = N.LowLink = 0;
1214
1215 Worklist.append(C->Nodes.begin(), C->Nodes.end());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001216 }
1217
Chandler Carruthe5944d92016-02-17 00:18:16 +00001218 auto MarkNodeForSCCNumber = [&PostOrderMapping](Node &N, int Number) {
1219 N.DFSNumber = N.LowLink = -1;
1220 PostOrderMapping[&N] = Number;
1221 };
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001222
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001223 SmallVector<std::pair<Node *, EdgeSequence::iterator>, 4> DFSStack;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001224 SmallVector<Node *, 4> PendingRefSCCStack;
Chandler Carruthaca48d02014-04-26 09:06:53 +00001225 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001226 assert(DFSStack.empty() &&
1227 "Cannot begin a new root with a non-empty DFS stack!");
1228 assert(PendingRefSCCStack.empty() &&
1229 "Cannot begin a new root with pending nodes for an SCC!");
1230
1231 Node *RootN = Worklist.pop_back_val();
1232 // Skip any nodes we've already reached in the DFS.
1233 if (RootN->DFSNumber != 0) {
1234 assert(RootN->DFSNumber == -1 &&
1235 "Shouldn't have any mid-DFS root nodes!");
1236 continue;
1237 }
1238
1239 RootN->DFSNumber = RootN->LowLink = 1;
1240 int NextDFSNumber = 2;
1241
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001242 DFSStack.push_back({RootN, (*RootN)->begin()});
Chandler Carruthe5944d92016-02-17 00:18:16 +00001243 do {
1244 Node *N;
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001245 EdgeSequence::iterator I;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001246 std::tie(N, I) = DFSStack.pop_back_val();
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001247 auto E = (*N)->end();
Chandler Carruthe5944d92016-02-17 00:18:16 +00001248
1249 assert(N->DFSNumber != 0 && "We should always assign a DFS number "
1250 "before processing a node.");
1251
1252 while (I != E) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001253 Node &ChildN = I->getNode();
Chandler Carruthe5944d92016-02-17 00:18:16 +00001254 if (ChildN.DFSNumber == 0) {
1255 // Mark that we should start at this child when next this node is the
1256 // top of the stack. We don't start at the next child to ensure this
1257 // child's lowlink is reflected.
1258 DFSStack.push_back({N, I});
1259
1260 // Continue, resetting to the child node.
1261 ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
1262 N = &ChildN;
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001263 I = ChildN->begin();
1264 E = ChildN->end();
Chandler Carruthe5944d92016-02-17 00:18:16 +00001265 continue;
1266 }
1267 if (ChildN.DFSNumber == -1) {
1268 // Check if this edge's target node connects to the deleted edge's
1269 // target node. If so, we know that every node connected will end up
1270 // in this RefSCC, so collapse the entire current stack into the root
1271 // slot in our SCC numbering. See above for the motivation of
1272 // optimizing the target connected nodes in this way.
1273 auto PostOrderI = PostOrderMapping.find(&ChildN);
1274 if (PostOrderI != PostOrderMapping.end() &&
1275 PostOrderI->second == RootPostOrderNumber) {
1276 MarkNodeForSCCNumber(*N, RootPostOrderNumber);
1277 while (!PendingRefSCCStack.empty())
1278 MarkNodeForSCCNumber(*PendingRefSCCStack.pop_back_val(),
1279 RootPostOrderNumber);
1280 while (!DFSStack.empty())
1281 MarkNodeForSCCNumber(*DFSStack.pop_back_val().first,
1282 RootPostOrderNumber);
1283 // Ensure we break all the way out of the enclosing loop.
1284 N = nullptr;
1285 break;
1286 }
1287
1288 // If this child isn't currently in this RefSCC, no need to process
Chandler Carruth23a6c3f2016-12-06 10:29:23 +00001289 // it. However, we do need to remove this RefSCC from its RefSCC's
1290 // parent set.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001291 RefSCC &ChildRC = *G->lookupRefSCC(ChildN);
1292 ChildRC.Parents.erase(this);
1293 ++I;
1294 continue;
1295 }
1296
1297 // Track the lowest link of the children, if any are still in the stack.
1298 // Any child not on the stack will have a LowLink of -1.
1299 assert(ChildN.LowLink != 0 &&
1300 "Low-link must not be zero with a non-zero DFS number.");
1301 if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
1302 N->LowLink = ChildN.LowLink;
1303 ++I;
1304 }
1305 if (!N)
1306 // We short-circuited this node.
1307 break;
1308
1309 // We've finished processing N and its descendents, put it on our pending
1310 // stack to eventually get merged into a RefSCC.
1311 PendingRefSCCStack.push_back(N);
1312
1313 // If this node is linked to some lower entry, continue walking up the
1314 // stack.
1315 if (N->LowLink != N->DFSNumber) {
1316 assert(!DFSStack.empty() &&
1317 "We never found a viable root for a RefSCC to pop off!");
1318 continue;
1319 }
1320
1321 // Otherwise, form a new RefSCC from the top of the pending node stack.
1322 int RootDFSNumber = N->DFSNumber;
1323 // Find the range of the node stack by walking down until we pass the
1324 // root DFS number.
1325 auto RefSCCNodes = make_range(
1326 PendingRefSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +00001327 find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1328 return N->DFSNumber < RootDFSNumber;
1329 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +00001330
1331 // Mark the postorder number for these nodes and clear them off the
1332 // stack. We'll use the postorder number to pull them into RefSCCs at the
1333 // end. FIXME: Fuse with the loop above.
1334 int RefSCCNumber = PostOrderNumber++;
1335 for (Node *N : RefSCCNodes)
1336 MarkNodeForSCCNumber(*N, RefSCCNumber);
1337
1338 PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1339 PendingRefSCCStack.end());
1340 } while (!DFSStack.empty());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001341
Chandler Carruthaca48d02014-04-26 09:06:53 +00001342 assert(DFSStack.empty() && "Didn't flush the entire DFS stack!");
Chandler Carruthe5944d92016-02-17 00:18:16 +00001343 assert(PendingRefSCCStack.empty() && "Didn't flush all pending nodes!");
Chandler Carruthaca48d02014-04-26 09:06:53 +00001344 } while (!Worklist.empty());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001345
Chandler Carruthe5944d92016-02-17 00:18:16 +00001346 // We now have a post-order numbering for RefSCCs and a mapping from each
1347 // node in this RefSCC to its final RefSCC. We create each new RefSCC node
1348 // (re-using this RefSCC node for the root) and build a radix-sort style map
1349 // from postorder number to the RefSCC. We then append SCCs to each of these
1350 // RefSCCs in the order they occured in the original SCCs container.
1351 for (int i = 1; i < PostOrderNumber; ++i)
1352 Result.push_back(G->createRefSCC(*G));
1353
Chandler Carruth49d728a2016-09-16 10:20:17 +00001354 // Insert the resulting postorder sequence into the global graph postorder
1355 // sequence before the current RefSCC in that sequence. The idea being that
1356 // this RefSCC is the target of the reference edge removed, and thus has
1357 // a direct or indirect edge to every other RefSCC formed and so must be at
1358 // the end of any postorder traversal.
1359 //
1360 // FIXME: It'd be nice to change the APIs so that we returned an iterator
1361 // range over the global postorder sequence and generally use that sequence
1362 // rather than building a separate result vector here.
1363 if (!Result.empty()) {
1364 int Idx = G->getRefSCCIndex(*this);
1365 G->PostOrderRefSCCs.insert(G->PostOrderRefSCCs.begin() + Idx,
1366 Result.begin(), Result.end());
1367 for (int i : seq<int>(Idx, G->PostOrderRefSCCs.size()))
1368 G->RefSCCIndices[G->PostOrderRefSCCs[i]] = i;
1369 assert(G->PostOrderRefSCCs[G->getRefSCCIndex(*this)] == this &&
1370 "Failed to update this RefSCC's index after insertion!");
1371 }
1372
Chandler Carruthe5944d92016-02-17 00:18:16 +00001373 for (SCC *C : SCCs) {
1374 auto PostOrderI = PostOrderMapping.find(&*C->begin());
1375 assert(PostOrderI != PostOrderMapping.end() &&
1376 "Cannot have missing mappings for nodes!");
1377 int SCCNumber = PostOrderI->second;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001378#ifndef NDEBUG
Chandler Carruthe5944d92016-02-17 00:18:16 +00001379 for (Node &N : *C)
1380 assert(PostOrderMapping.find(&N)->second == SCCNumber &&
1381 "Cannot have different numbers for nodes in the same SCC!");
1382#endif
1383 if (SCCNumber == 0)
1384 // The root node is handled separately by removing the SCCs.
1385 continue;
1386
1387 RefSCC &RC = *Result[SCCNumber - 1];
1388 int SCCIndex = RC.SCCs.size();
1389 RC.SCCs.push_back(C);
Chandler Carruth23a6c3f2016-12-06 10:29:23 +00001390 RC.SCCIndices[C] = SCCIndex;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001391 C->OuterRefSCC = &RC;
1392 }
1393
1394 // FIXME: We re-walk the edges in each RefSCC to establish whether it is
1395 // a leaf and connect it to the rest of the graph's parents lists. This is
1396 // really wasteful. We should instead do this during the DFS to avoid yet
1397 // another edge walk.
1398 for (RefSCC *RC : Result)
1399 G->connectRefSCC(*RC);
1400
1401 // Now erase all but the root's SCCs.
David Majnemer42531262016-08-12 03:55:06 +00001402 SCCs.erase(remove_if(SCCs,
1403 [&](SCC *C) {
1404 return PostOrderMapping.lookup(&*C->begin()) !=
1405 RootPostOrderNumber;
1406 }),
Chandler Carruthe5944d92016-02-17 00:18:16 +00001407 SCCs.end());
Chandler Carruth88823462016-08-24 09:37:14 +00001408 SCCIndices.clear();
1409 for (int i = 0, Size = SCCs.size(); i < Size; ++i)
1410 SCCIndices[SCCs[i]] = i;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001411
1412#ifndef NDEBUG
1413 // Now we need to reconnect the current (root) SCC to the graph. We do this
1414 // manually because we can special case our leaf handling and detect errors.
1415 bool IsLeaf = true;
1416#endif
1417 for (SCC *C : SCCs)
1418 for (Node &N : *C) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001419 for (Edge &E : *N) {
1420 RefSCC &ChildRC = *G->lookupRefSCC(E.getNode());
Chandler Carruthe5944d92016-02-17 00:18:16 +00001421 if (&ChildRC == this)
1422 continue;
1423 ChildRC.Parents.insert(this);
1424#ifndef NDEBUG
1425 IsLeaf = false;
1426#endif
1427 }
1428 }
1429#ifndef NDEBUG
1430 if (!Result.empty())
1431 assert(!IsLeaf && "This SCC cannot be a leaf as we have split out new "
1432 "SCCs by removing this edge.");
David Majnemer0a16c222016-08-11 21:15:00 +00001433 if (none_of(G->LeafRefSCCs, [&](RefSCC *C) { return C == this; }))
Chandler Carruthe5944d92016-02-17 00:18:16 +00001434 assert(!IsLeaf && "This SCC cannot be a leaf as it already had child "
1435 "SCCs before we removed this edge.");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001436#endif
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001437 // And connect both this RefSCC and all the new ones to the correct parents.
1438 // The easiest way to do this is just to re-analyze the old parent set.
1439 SmallVector<RefSCC *, 4> OldParents(Parents.begin(), Parents.end());
1440 Parents.clear();
1441 for (RefSCC *ParentRC : OldParents)
Chandler Carruth5205c352016-12-07 01:42:40 +00001442 for (SCC &ParentC : *ParentRC)
1443 for (Node &ParentN : ParentC)
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001444 for (Edge &E : *ParentN) {
1445 RefSCC &RC = *G->lookupRefSCC(E.getNode());
Chandler Carruth5205c352016-12-07 01:42:40 +00001446 if (&RC != ParentRC)
1447 RC.Parents.insert(ParentRC);
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001448 }
1449
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001450 // If this SCC stopped being a leaf through this edge removal, remove it from
Chandler Carruthe5944d92016-02-17 00:18:16 +00001451 // the leaf SCC list. Note that this DTRT in the case where this was never
1452 // a leaf.
1453 // FIXME: As LeafRefSCCs could be very large, we might want to not walk the
1454 // entire list if this RefSCC wasn't a leaf before the edge removal.
1455 if (!Result.empty())
1456 G->LeafRefSCCs.erase(
1457 std::remove(G->LeafRefSCCs.begin(), G->LeafRefSCCs.end(), this),
1458 G->LeafRefSCCs.end());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001459
Chandler Carruth23a6c3f2016-12-06 10:29:23 +00001460#ifndef NDEBUG
1461 // Verify all of the new RefSCCs.
1462 for (RefSCC *RC : Result)
1463 RC->verify();
1464#endif
1465
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001466 // Return the new list of SCCs.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001467 return Result;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001468}
1469
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001470void LazyCallGraph::RefSCC::handleTrivialEdgeInsertion(Node &SourceN,
1471 Node &TargetN) {
1472 // The only trivial case that requires any graph updates is when we add new
1473 // ref edge and may connect different RefSCCs along that path. This is only
1474 // because of the parents set. Every other part of the graph remains constant
1475 // after this edge insertion.
1476 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
1477 RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
1478 if (&TargetRC == this) {
1479
1480 return;
1481 }
1482
Francis Visoiu Mistrih262ad162017-02-28 18:34:55 +00001483#ifdef EXPENSIVE_CHECKS
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001484 assert(TargetRC.isDescendantOf(*this) &&
1485 "Target must be a descendant of the Source.");
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001486#endif
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001487 // The only change required is to add this RefSCC to the parent set of the
1488 // target. This is a set and so idempotent if the edge already existed.
1489 TargetRC.Parents.insert(this);
1490}
1491
1492void LazyCallGraph::RefSCC::insertTrivialCallEdge(Node &SourceN,
1493 Node &TargetN) {
1494#ifndef NDEBUG
1495 // Check that the RefSCC is still valid when we finish.
1496 auto ExitVerifier = make_scope_exit([this] { verify(); });
Chandler Carruthbae595b2016-11-22 19:23:31 +00001497
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001498#ifdef EXPENSIVE_CHECKS
1499 // Check that we aren't breaking some invariants of the SCC graph. Note that
1500 // this is quadratic in the number of edges in the call graph!
Chandler Carruthbae595b2016-11-22 19:23:31 +00001501 SCC &SourceC = *G->lookupSCC(SourceN);
1502 SCC &TargetC = *G->lookupSCC(TargetN);
1503 if (&SourceC != &TargetC)
1504 assert(SourceC.isAncestorOf(TargetC) &&
1505 "Call edge is not trivial in the SCC graph!");
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001506#endif // EXPENSIVE_CHECKS
1507#endif // NDEBUG
1508
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001509 // First insert it into the source or find the existing edge.
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001510 auto InsertResult =
1511 SourceN->EdgeIndexMap.insert({&TargetN, SourceN->Edges.size()});
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001512 if (!InsertResult.second) {
1513 // Already an edge, just update it.
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001514 Edge &E = SourceN->Edges[InsertResult.first->second];
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001515 if (E.isCall())
1516 return; // Nothing to do!
1517 E.setKind(Edge::Call);
1518 } else {
1519 // Create the new edge.
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001520 SourceN->Edges.emplace_back(TargetN, Edge::Call);
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001521 }
1522
1523 // Now that we have the edge, handle the graph fallout.
1524 handleTrivialEdgeInsertion(SourceN, TargetN);
1525}
1526
1527void LazyCallGraph::RefSCC::insertTrivialRefEdge(Node &SourceN, Node &TargetN) {
1528#ifndef NDEBUG
1529 // Check that the RefSCC is still valid when we finish.
1530 auto ExitVerifier = make_scope_exit([this] { verify(); });
Chandler Carruth9eb857c2016-11-22 21:40:10 +00001531
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001532#ifdef EXPENSIVE_CHECKS
Chandler Carruth9eb857c2016-11-22 21:40:10 +00001533 // Check that we aren't breaking some invariants of the RefSCC graph.
1534 RefSCC &SourceRC = *G->lookupRefSCC(SourceN);
1535 RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
1536 if (&SourceRC != &TargetRC)
1537 assert(SourceRC.isAncestorOf(TargetRC) &&
1538 "Ref edge is not trivial in the RefSCC graph!");
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001539#endif // EXPENSIVE_CHECKS
1540#endif // NDEBUG
1541
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001542 // First insert it into the source or find the existing edge.
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001543 auto InsertResult =
1544 SourceN->EdgeIndexMap.insert({&TargetN, SourceN->Edges.size()});
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001545 if (!InsertResult.second)
1546 // Already an edge, we're done.
1547 return;
1548
1549 // Create the new edge.
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001550 SourceN->Edges.emplace_back(TargetN, Edge::Ref);
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001551
1552 // Now that we have the edge, handle the graph fallout.
1553 handleTrivialEdgeInsertion(SourceN, TargetN);
1554}
1555
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001556void LazyCallGraph::RefSCC::replaceNodeFunction(Node &N, Function &NewF) {
1557 Function &OldF = N.getFunction();
Chandler Carruthc00a7ff2014-04-28 11:10:23 +00001558
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001559#ifndef NDEBUG
1560 // Check that the RefSCC is still valid when we finish.
1561 auto ExitVerifier = make_scope_exit([this] { verify(); });
1562
1563 assert(G->lookupRefSCC(N) == this &&
1564 "Cannot replace the function of a node outside this RefSCC.");
1565
1566 assert(G->NodeMap.find(&NewF) == G->NodeMap.end() &&
1567 "Must not have already walked the new function!'");
1568
1569 // It is important that this replacement not introduce graph changes so we
1570 // insist that the caller has already removed every use of the original
1571 // function and that all uses of the new function correspond to existing
1572 // edges in the graph. The common and expected way to use this is when
1573 // replacing the function itself in the IR without changing the call graph
1574 // shape and just updating the analysis based on that.
1575 assert(&OldF != &NewF && "Cannot replace a function with itself!");
1576 assert(OldF.use_empty() &&
1577 "Must have moved all uses from the old function to the new!");
1578#endif
1579
1580 N.replaceFunction(NewF);
1581
1582 // Update various call graph maps.
1583 G->NodeMap.erase(&OldF);
1584 G->NodeMap[&NewF] = &N;
Chandler Carruthc00a7ff2014-04-28 11:10:23 +00001585}
1586
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001587void LazyCallGraph::insertEdge(Node &SourceN, Node &TargetN, Edge::Kind EK) {
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001588 assert(SCCMap.empty() &&
Chandler Carruthaa839b22014-04-27 01:59:50 +00001589 "This method cannot be called after SCCs have been formed!");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001590
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001591 return SourceN->insertEdgeInternal(TargetN, EK);
1592}
1593
1594void LazyCallGraph::removeEdge(Node &SourceN, Node &TargetN) {
1595 assert(SCCMap.empty() &&
1596 "This method cannot be called after SCCs have been formed!");
1597
1598 bool Removed = SourceN->removeEdgeInternal(TargetN);
1599 (void)Removed;
1600 assert(Removed && "Target not in the edge set for this caller?");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001601}
1602
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001603void LazyCallGraph::removeDeadFunction(Function &F) {
1604 // FIXME: This is unnecessarily restrictive. We should be able to remove
1605 // functions which recursively call themselves.
1606 assert(F.use_empty() &&
1607 "This routine should only be called on trivially dead functions!");
1608
Chandler Carruth06a86302017-07-19 04:12:25 +00001609 // We shouldn't remove library functions as they are never really dead while
1610 // the call graph is in use -- every function definition refers to them.
1611 assert(!isLibFunction(F) &&
1612 "Must not remove lib functions from the call graph!");
1613
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001614 auto NI = NodeMap.find(&F);
1615 if (NI == NodeMap.end())
1616 // Not in the graph at all!
1617 return;
1618
1619 Node &N = *NI->second;
1620 NodeMap.erase(NI);
1621
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001622 // Remove this from the entry edges if present.
1623 EntryEdges.removeEdgeInternal(N);
1624
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001625 if (SCCMap.empty()) {
1626 // No SCCs have been formed, so removing this is fine and there is nothing
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001627 // else necessary at this point but clearing out the node.
1628 N.clear();
1629 return;
1630 }
1631
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001632 // Cannot remove a function which has yet to be visited in the DFS walk, so
1633 // if we have a node at all then we must have an SCC and RefSCC.
1634 auto CI = SCCMap.find(&N);
1635 assert(CI != SCCMap.end() &&
1636 "Tried to remove a node without an SCC after DFS walk started!");
1637 SCC &C = *CI->second;
1638 SCCMap.erase(CI);
1639 RefSCC &RC = C.getOuterRefSCC();
1640
1641 // This node must be the only member of its SCC as it has no callers, and
1642 // that SCC must be the only member of a RefSCC as it has no references.
1643 // Validate these properties first.
1644 assert(C.size() == 1 && "Dead functions must be in a singular SCC");
1645 assert(RC.size() == 1 && "Dead functions must be in a singular RefSCC");
Chandler Carruth1f8fcfe2017-02-09 23:30:14 +00001646
1647 // Clean up any remaining reference edges. Note that we walk an unordered set
1648 // here but are just removing and so the order doesn't matter.
1649 for (RefSCC &ParentRC : RC.parents())
1650 for (SCC &ParentC : ParentRC)
1651 for (Node &ParentN : ParentC)
1652 if (ParentN)
1653 ParentN->removeEdgeInternal(N);
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001654
1655 // Now remove this RefSCC from any parents sets and the leaf list.
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001656 for (Edge &E : *N)
1657 if (RefSCC *TargetRC = lookupRefSCC(E.getNode()))
1658 TargetRC->Parents.erase(&RC);
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001659 // FIXME: This is a linear operation which could become hot and benefit from
1660 // an index map.
1661 auto LRI = find(LeafRefSCCs, &RC);
1662 if (LRI != LeafRefSCCs.end())
1663 LeafRefSCCs.erase(LRI);
1664
1665 auto RCIndexI = RefSCCIndices.find(&RC);
1666 int RCIndex = RCIndexI->second;
1667 PostOrderRefSCCs.erase(PostOrderRefSCCs.begin() + RCIndex);
1668 RefSCCIndices.erase(RCIndexI);
1669 for (int i = RCIndex, Size = PostOrderRefSCCs.size(); i < Size; ++i)
1670 RefSCCIndices[PostOrderRefSCCs[i]] = i;
1671
1672 // Finally clear out all the data structures from the node down through the
1673 // components.
1674 N.clear();
1675 C.clear();
1676 RC.clear();
1677
1678 // Nothing to delete as all the objects are allocated in stable bump pointer
1679 // allocators.
1680}
1681
Chandler Carruth2a898e02014-04-23 23:20:36 +00001682LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
1683 return *new (MappedN = BPA.Allocate()) Node(*this, F);
Chandler Carruthd8d865e2014-04-18 11:02:33 +00001684}
1685
1686void LazyCallGraph::updateGraphPtrs() {
Chandler Carruthb60cb312014-04-17 07:25:59 +00001687 // Process all nodes updating the graph pointers.
Chandler Carruthaa839b22014-04-27 01:59:50 +00001688 {
1689 SmallVector<Node *, 16> Worklist;
Chandler Carrutha4499e92016-02-02 03:57:13 +00001690 for (Edge &E : EntryEdges)
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001691 Worklist.push_back(&E.getNode());
Chandler Carruthb60cb312014-04-17 07:25:59 +00001692
Chandler Carruthaa839b22014-04-27 01:59:50 +00001693 while (!Worklist.empty()) {
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001694 Node &N = *Worklist.pop_back_val();
1695 N.G = this;
1696 if (N)
1697 for (Edge &E : *N)
1698 Worklist.push_back(&E.getNode());
Chandler Carruthaa839b22014-04-27 01:59:50 +00001699 }
1700 }
1701
1702 // Process all SCCs updating the graph pointers.
1703 {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001704 SmallVector<RefSCC *, 16> Worklist(LeafRefSCCs.begin(), LeafRefSCCs.end());
Chandler Carruthaa839b22014-04-27 01:59:50 +00001705
1706 while (!Worklist.empty()) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001707 RefSCC &C = *Worklist.pop_back_val();
1708 C.G = this;
1709 for (RefSCC &ParentC : C.parents())
1710 Worklist.push_back(&ParentC);
Chandler Carruthaa839b22014-04-27 01:59:50 +00001711 }
Chandler Carruthb60cb312014-04-17 07:25:59 +00001712 }
Chandler Carruthbf71a342014-02-06 04:37:03 +00001713}
Chandler Carruthbf71a342014-02-06 04:37:03 +00001714
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001715template <typename RootsT, typename GetBeginT, typename GetEndT,
1716 typename GetNodeT, typename FormSCCCallbackT>
1717void LazyCallGraph::buildGenericSCCs(RootsT &&Roots, GetBeginT &&GetBegin,
1718 GetEndT &&GetEnd, GetNodeT &&GetNode,
1719 FormSCCCallbackT &&FormSCC) {
1720 typedef decltype(GetBegin(std::declval<Node &>())) EdgeItT;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001721
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001722 SmallVector<std::pair<Node *, EdgeItT>, 16> DFSStack;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001723 SmallVector<Node *, 16> PendingSCCStack;
1724
1725 // Scan down the stack and DFS across the call edges.
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001726 for (Node *RootN : Roots) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001727 assert(DFSStack.empty() &&
1728 "Cannot begin a new root with a non-empty DFS stack!");
1729 assert(PendingSCCStack.empty() &&
1730 "Cannot begin a new root with pending nodes for an SCC!");
1731
1732 // Skip any nodes we've already reached in the DFS.
1733 if (RootN->DFSNumber != 0) {
1734 assert(RootN->DFSNumber == -1 &&
1735 "Shouldn't have any mid-DFS root nodes!");
1736 continue;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001737 }
1738
Chandler Carruthe5944d92016-02-17 00:18:16 +00001739 RootN->DFSNumber = RootN->LowLink = 1;
1740 int NextDFSNumber = 2;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001741
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001742 DFSStack.push_back({RootN, GetBegin(*RootN)});
Chandler Carruthe5944d92016-02-17 00:18:16 +00001743 do {
1744 Node *N;
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001745 EdgeItT I;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001746 std::tie(N, I) = DFSStack.pop_back_val();
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001747 auto E = GetEnd(*N);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001748 while (I != E) {
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001749 Node &ChildN = GetNode(I);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001750 if (ChildN.DFSNumber == 0) {
1751 // We haven't yet visited this child, so descend, pushing the current
1752 // node onto the stack.
1753 DFSStack.push_back({N, I});
1754
Chandler Carruthe5944d92016-02-17 00:18:16 +00001755 ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
1756 N = &ChildN;
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001757 I = GetBegin(*N);
1758 E = GetEnd(*N);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001759 continue;
1760 }
1761
1762 // If the child has already been added to some child component, it
1763 // couldn't impact the low-link of this parent because it isn't
1764 // connected, and thus its low-link isn't relevant so skip it.
1765 if (ChildN.DFSNumber == -1) {
1766 ++I;
1767 continue;
1768 }
1769
1770 // Track the lowest linked child as the lowest link for this node.
1771 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1772 if (ChildN.LowLink < N->LowLink)
1773 N->LowLink = ChildN.LowLink;
1774
1775 // Move to the next edge.
1776 ++I;
1777 }
1778
1779 // We've finished processing N and its descendents, put it on our pending
1780 // SCC stack to eventually get merged into an SCC of nodes.
1781 PendingSCCStack.push_back(N);
1782
1783 // If this node is linked to some lower entry, continue walking up the
1784 // stack.
1785 if (N->LowLink != N->DFSNumber)
1786 continue;
1787
1788 // Otherwise, we've completed an SCC. Append it to our post order list of
1789 // SCCs.
1790 int RootDFSNumber = N->DFSNumber;
1791 // Find the range of the node stack by walking down until we pass the
1792 // root DFS number.
1793 auto SCCNodes = make_range(
1794 PendingSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +00001795 find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
1796 return N->DFSNumber < RootDFSNumber;
1797 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +00001798 // Form a new SCC out of these nodes and then clear them off our pending
1799 // stack.
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001800 FormSCC(SCCNodes);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001801 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
1802 } while (!DFSStack.empty());
1803 }
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001804}
1805
1806/// Build the internal SCCs for a RefSCC from a sequence of nodes.
1807///
1808/// Appends the SCCs to the provided vector and updates the map with their
1809/// indices. Both the vector and map must be empty when passed into this
1810/// routine.
1811void LazyCallGraph::buildSCCs(RefSCC &RC, node_stack_range Nodes) {
1812 assert(RC.SCCs.empty() && "Already built SCCs!");
1813 assert(RC.SCCIndices.empty() && "Already mapped SCC indices!");
1814
1815 for (Node *N : Nodes) {
1816 assert(N->LowLink >= (*Nodes.begin())->LowLink &&
1817 "We cannot have a low link in an SCC lower than its root on the "
1818 "stack!");
1819
1820 // This node will go into the next RefSCC, clear out its DFS and low link
1821 // as we scan.
1822 N->DFSNumber = N->LowLink = 0;
1823 }
1824
1825 // Each RefSCC contains a DAG of the call SCCs. To build these, we do
1826 // a direct walk of the call edges using Tarjan's algorithm. We reuse the
1827 // internal storage as we won't need it for the outer graph's DFS any longer.
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001828 buildGenericSCCs(
1829 Nodes, [](Node &N) { return N->call_begin(); },
1830 [](Node &N) { return N->call_end(); },
1831 [](EdgeSequence::call_iterator I) -> Node & { return I->getNode(); },
1832 [this, &RC](node_stack_range Nodes) {
1833 RC.SCCs.push_back(createSCC(RC, Nodes));
1834 for (Node &N : *RC.SCCs.back()) {
1835 N.DFSNumber = N.LowLink = -1;
1836 SCCMap[&N] = RC.SCCs.back();
1837 }
1838 });
Chandler Carruthe5944d92016-02-17 00:18:16 +00001839
1840 // Wire up the SCC indices.
1841 for (int i = 0, Size = RC.SCCs.size(); i < Size; ++i)
1842 RC.SCCIndices[RC.SCCs[i]] = i;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001843}
1844
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001845void LazyCallGraph::buildRefSCCs() {
1846 if (EntryEdges.empty() || !PostOrderRefSCCs.empty())
1847 // RefSCCs are either non-existent or already built!
1848 return;
1849
1850 assert(RefSCCIndices.empty() && "Already mapped RefSCC indices!");
1851
1852 SmallVector<Node *, 16> Roots;
1853 for (Edge &E : *this)
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001854 Roots.push_back(&E.getNode());
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001855
1856 // The roots will be popped of a stack, so use reverse to get a less
1857 // surprising order. This doesn't change any of the semantics anywhere.
1858 std::reverse(Roots.begin(), Roots.end());
1859
1860 buildGenericSCCs(
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001861 Roots,
1862 [](Node &N) {
1863 // We need to populate each node as we begin to walk its edges.
1864 N.populate();
1865 return N->begin();
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001866 },
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001867 [](Node &N) { return N->end(); },
1868 [](EdgeSequence::iterator I) -> Node & { return I->getNode(); },
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001869 [this](node_stack_range Nodes) {
1870 RefSCC *NewRC = createRefSCC(*this);
1871 buildSCCs(*NewRC, Nodes);
1872 connectRefSCC(*NewRC);
1873
1874 // Push the new node into the postorder list and remember its position
1875 // in the index map.
1876 bool Inserted =
1877 RefSCCIndices.insert({NewRC, PostOrderRefSCCs.size()}).second;
1878 (void)Inserted;
1879 assert(Inserted && "Cannot already have this RefSCC in the index map!");
1880 PostOrderRefSCCs.push_back(NewRC);
Chandler Carrutha80cfb32017-02-06 20:59:07 +00001881#ifndef NDEBUG
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001882 NewRC->verify();
Chandler Carrutha80cfb32017-02-06 20:59:07 +00001883#endif
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001884 });
1885}
1886
Chandler Carruthe5944d92016-02-17 00:18:16 +00001887// FIXME: We should move callers of this to embed the parent linking and leaf
1888// tracking into their DFS in order to remove a full walk of all edges.
1889void LazyCallGraph::connectRefSCC(RefSCC &RC) {
1890 // Walk all edges in the RefSCC (this remains linear as we only do this once
1891 // when we build the RefSCC) to connect it to the parent sets of its
1892 // children.
1893 bool IsLeaf = true;
1894 for (SCC &C : RC)
1895 for (Node &N : C)
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001896 for (Edge &E : *N) {
1897 RefSCC &ChildRC = *lookupRefSCC(E.getNode());
Chandler Carruthe5944d92016-02-17 00:18:16 +00001898 if (&ChildRC == &RC)
1899 continue;
1900 ChildRC.Parents.insert(&RC);
1901 IsLeaf = false;
1902 }
1903
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001904 // For the SCCs where we find no child SCCs, add them to the leaf list.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001905 if (IsLeaf)
1906 LeafRefSCCs.push_back(&RC);
1907}
1908
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001909AnalysisKey LazyCallGraphAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001910
Chandler Carruthbf71a342014-02-06 04:37:03 +00001911LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
1912
Chandler Carruthe5944d92016-02-17 00:18:16 +00001913static void printNode(raw_ostream &OS, LazyCallGraph::Node &N) {
Chandler Carrutha4499e92016-02-02 03:57:13 +00001914 OS << " Edges in function: " << N.getFunction().getName() << "\n";
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001915 for (LazyCallGraph::Edge &E : N.populate())
Chandler Carrutha4499e92016-02-02 03:57:13 +00001916 OS << " " << (E.isCall() ? "call" : "ref ") << " -> "
1917 << E.getFunction().getName() << "\n";
Chandler Carruth11f50322015-01-14 00:27:45 +00001918
1919 OS << "\n";
1920}
1921
Chandler Carruthe5944d92016-02-17 00:18:16 +00001922static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) {
1923 ptrdiff_t Size = std::distance(C.begin(), C.end());
1924 OS << " SCC with " << Size << " functions:\n";
Chandler Carruth11f50322015-01-14 00:27:45 +00001925
Chandler Carruthe5944d92016-02-17 00:18:16 +00001926 for (LazyCallGraph::Node &N : C)
1927 OS << " " << N.getFunction().getName() << "\n";
1928}
1929
1930static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) {
1931 ptrdiff_t Size = std::distance(C.begin(), C.end());
1932 OS << " RefSCC with " << Size << " call SCCs:\n";
1933
1934 for (LazyCallGraph::SCC &InnerC : C)
1935 printSCC(OS, InnerC);
Chandler Carruth11f50322015-01-14 00:27:45 +00001936
1937 OS << "\n";
1938}
1939
Chandler Carruthd174ce42015-01-05 02:47:05 +00001940PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M,
Chandler Carruthb47f8012016-03-11 11:05:24 +00001941 ModuleAnalysisManager &AM) {
1942 LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
Chandler Carruth11f50322015-01-14 00:27:45 +00001943
1944 OS << "Printing the call graph for module: " << M.getModuleIdentifier()
1945 << "\n\n";
1946
Chandler Carruthe5944d92016-02-17 00:18:16 +00001947 for (Function &F : M)
1948 printNode(OS, G.get(F));
Chandler Carruth11f50322015-01-14 00:27:45 +00001949
Chandler Carruth2e0fe3e2017-02-06 19:38:06 +00001950 G.buildRefSCCs();
Chandler Carruthe5944d92016-02-17 00:18:16 +00001951 for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs())
1952 printRefSCC(OS, C);
Chandler Carruth18eadd922014-04-18 10:50:32 +00001953
Chandler Carruthbf71a342014-02-06 04:37:03 +00001954 return PreservedAnalyses::all();
Chandler Carruthbf71a342014-02-06 04:37:03 +00001955}
Sean Silva7cb30662016-06-18 09:17:32 +00001956
1957LazyCallGraphDOTPrinterPass::LazyCallGraphDOTPrinterPass(raw_ostream &OS)
1958 : OS(OS) {}
1959
1960static void printNodeDOT(raw_ostream &OS, LazyCallGraph::Node &N) {
1961 std::string Name = "\"" + DOT::EscapeString(N.getFunction().getName()) + "\"";
1962
Chandler Carruthaaad9f82017-02-09 23:24:13 +00001963 for (LazyCallGraph::Edge &E : N.populate()) {
Sean Silva7cb30662016-06-18 09:17:32 +00001964 OS << " " << Name << " -> \""
1965 << DOT::EscapeString(E.getFunction().getName()) << "\"";
1966 if (!E.isCall()) // It is a ref edge.
1967 OS << " [style=dashed,label=\"ref\"]";
1968 OS << ";\n";
1969 }
1970
1971 OS << "\n";
1972}
1973
1974PreservedAnalyses LazyCallGraphDOTPrinterPass::run(Module &M,
1975 ModuleAnalysisManager &AM) {
1976 LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
1977
1978 OS << "digraph \"" << DOT::EscapeString(M.getModuleIdentifier()) << "\" {\n";
1979
1980 for (Function &F : M)
1981 printNodeDOT(OS, G.get(F));
1982
1983 OS << "}\n";
1984
1985 return PreservedAnalyses::all();
1986}