blob: 8ad34dc080b73878ec96bddb78cabbbdd4f8c208 [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 Carruth11b3f602016-09-04 08:34:31 +000011#include "llvm/ADT/ScopeExit.h"
Chandler Carruth18eadd922014-04-18 10:50:32 +000012#include "llvm/ADT/STLExtras.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000013#include "llvm/IR/CallSite.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000014#include "llvm/IR/InstVisitor.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000015#include "llvm/IR/Instructions.h"
16#include "llvm/IR/PassManager.h"
Chandler Carruth99b756d2014-04-21 05:04:24 +000017#include "llvm/Support/Debug.h"
Sean Silva7cb30662016-06-18 09:17:32 +000018#include "llvm/Support/GraphWriter.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000019
20using namespace llvm;
21
Chandler Carruthf1221bd2014-04-22 02:48:03 +000022#define DEBUG_TYPE "lcg"
23
Chandler Carrutha4499e92016-02-02 03:57:13 +000024static void addEdge(SmallVectorImpl<LazyCallGraph::Edge> &Edges,
Chandler Carruthe5944d92016-02-17 00:18:16 +000025 DenseMap<Function *, int> &EdgeIndexMap, Function &F,
Chandler Carrutha4499e92016-02-02 03:57:13 +000026 LazyCallGraph::Edge::Kind EK) {
27 // Note that we consider *any* function with a definition to be a viable
28 // edge. Even if the function's definition is subject to replacement by
29 // some other module (say, a weak definition) there may still be
30 // optimizations which essentially speculate based on the definition and
31 // a way to check that the specific definition is in fact the one being
32 // used. For example, this could be done by moving the weak definition to
33 // a strong (internal) definition and making the weak definition be an
34 // alias. Then a test of the address of the weak function against the new
35 // strong definition's address would be an effective way to determine the
36 // safety of optimizing a direct call edge.
37 if (!F.isDeclaration() &&
Chandler Carruthe5944d92016-02-17 00:18:16 +000038 EdgeIndexMap.insert({&F, Edges.size()}).second) {
Chandler Carrutha4499e92016-02-02 03:57:13 +000039 DEBUG(dbgs() << " Added callable function: " << F.getName() << "\n");
40 Edges.emplace_back(LazyCallGraph::Edge(F, EK));
41 }
42}
43
Chandler Carruth18eadd922014-04-18 10:50:32 +000044LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F)
45 : G(&G), F(F), DFSNumber(0), LowLink(0) {
Chandler Carruth99b756d2014-04-21 05:04:24 +000046 DEBUG(dbgs() << " Adding functions called by '" << F.getName()
47 << "' to the graph.\n");
48
Chandler Carruthbf71a342014-02-06 04:37:03 +000049 SmallVector<Constant *, 16> Worklist;
Chandler Carrutha4499e92016-02-02 03:57:13 +000050 SmallPtrSet<Function *, 4> Callees;
Chandler Carruthbf71a342014-02-06 04:37:03 +000051 SmallPtrSet<Constant *, 16> Visited;
Chandler Carrutha4499e92016-02-02 03:57:13 +000052
53 // Find all the potential call graph edges in this function. We track both
54 // actual call edges and indirect references to functions. The direct calls
55 // are trivially added, but to accumulate the latter we walk the instructions
56 // and add every operand which is a constant to the worklist to process
57 // afterward.
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +000058 for (BasicBlock &BB : F)
Chandler Carrutha4499e92016-02-02 03:57:13 +000059 for (Instruction &I : BB) {
60 if (auto CS = CallSite(&I))
61 if (Function *Callee = CS.getCalledFunction())
62 if (Callees.insert(Callee).second) {
63 Visited.insert(Callee);
64 addEdge(Edges, EdgeIndexMap, *Callee, LazyCallGraph::Edge::Call);
65 }
66
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +000067 for (Value *Op : I.operand_values())
Chandler Carruth1583e992014-03-03 10:42:58 +000068 if (Constant *C = dyn_cast<Constant>(Op))
David Blaikie70573dc2014-11-19 07:49:26 +000069 if (Visited.insert(C).second)
Chandler Carruthbf71a342014-02-06 04:37:03 +000070 Worklist.push_back(C);
Chandler Carrutha4499e92016-02-02 03:57:13 +000071 }
Chandler Carruthbf71a342014-02-06 04:37:03 +000072
73 // We've collected all the constant (and thus potentially function or
74 // function containing) operands to all of the instructions in the function.
75 // Process them (recursively) collecting every function found.
Chandler Carruth88823462016-08-24 09:37:14 +000076 visitReferences(Worklist, Visited, [&](Function &F) {
77 addEdge(Edges, EdgeIndexMap, F, LazyCallGraph::Edge::Ref);
78 });
Chandler Carruthbf71a342014-02-06 04:37:03 +000079}
80
Chandler Carruthe5944d92016-02-17 00:18:16 +000081void LazyCallGraph::Node::insertEdgeInternal(Function &Target, Edge::Kind EK) {
82 if (Node *N = G->lookup(Target))
Chandler Carrutha4499e92016-02-02 03:57:13 +000083 return insertEdgeInternal(*N, EK);
Chandler Carruth5217c942014-04-30 10:48:36 +000084
Chandler Carruthe5944d92016-02-17 00:18:16 +000085 EdgeIndexMap.insert({&Target, Edges.size()});
86 Edges.emplace_back(Target, EK);
Chandler Carruth5217c942014-04-30 10:48:36 +000087}
88
Chandler Carruthe5944d92016-02-17 00:18:16 +000089void LazyCallGraph::Node::insertEdgeInternal(Node &TargetN, Edge::Kind EK) {
90 EdgeIndexMap.insert({&TargetN.getFunction(), Edges.size()});
91 Edges.emplace_back(TargetN, EK);
Chandler Carruthc00a7ff2014-04-28 11:10:23 +000092}
93
Chandler Carruthe5944d92016-02-17 00:18:16 +000094void LazyCallGraph::Node::setEdgeKind(Function &TargetF, Edge::Kind EK) {
95 Edges[EdgeIndexMap.find(&TargetF)->second].setKind(EK);
96}
97
98void LazyCallGraph::Node::removeEdgeInternal(Function &Target) {
99 auto IndexMapI = EdgeIndexMap.find(&Target);
Chandler Carrutha4499e92016-02-02 03:57:13 +0000100 assert(IndexMapI != EdgeIndexMap.end() &&
Chandler Carruthe5944d92016-02-17 00:18:16 +0000101 "Target not in the edge set for this caller?");
Chandler Carruthaa839b22014-04-27 01:59:50 +0000102
Chandler Carrutha4499e92016-02-02 03:57:13 +0000103 Edges[IndexMapI->second] = Edge();
104 EdgeIndexMap.erase(IndexMapI);
Chandler Carruthaa839b22014-04-27 01:59:50 +0000105}
106
Chandler Carruthdca83402016-06-27 23:26:08 +0000107void LazyCallGraph::Node::dump() const {
108 dbgs() << *this << '\n';
109}
110
Chandler Carruth2174f442014-04-18 20:44:16 +0000111LazyCallGraph::LazyCallGraph(Module &M) : NextDFSNumber(0) {
Chandler Carruth99b756d2014-04-21 05:04:24 +0000112 DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier()
113 << "\n");
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000114 for (Function &F : M)
115 if (!F.isDeclaration() && !F.hasLocalLinkage())
Chandler Carruthe5944d92016-02-17 00:18:16 +0000116 if (EntryIndexMap.insert({&F, EntryEdges.size()}).second) {
Chandler Carruth99b756d2014-04-21 05:04:24 +0000117 DEBUG(dbgs() << " Adding '" << F.getName()
118 << "' to entry set of the graph.\n");
Chandler Carrutha4499e92016-02-02 03:57:13 +0000119 EntryEdges.emplace_back(F, Edge::Ref);
Chandler Carruth99b756d2014-04-21 05:04:24 +0000120 }
Chandler Carruthbf71a342014-02-06 04:37:03 +0000121
122 // Now add entry nodes for functions reachable via initializers to globals.
123 SmallVector<Constant *, 16> Worklist;
124 SmallPtrSet<Constant *, 16> Visited;
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000125 for (GlobalVariable &GV : M.globals())
126 if (GV.hasInitializer())
David Blaikie70573dc2014-11-19 07:49:26 +0000127 if (Visited.insert(GV.getInitializer()).second)
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000128 Worklist.push_back(GV.getInitializer());
Chandler Carruthbf71a342014-02-06 04:37:03 +0000129
Chandler Carruth99b756d2014-04-21 05:04:24 +0000130 DEBUG(dbgs() << " Adding functions referenced by global initializers to the "
131 "entry set.\n");
Chandler Carruth88823462016-08-24 09:37:14 +0000132 visitReferences(Worklist, Visited, [&](Function &F) {
133 addEdge(EntryEdges, EntryIndexMap, F, LazyCallGraph::Edge::Ref);
134 });
Chandler Carruth18eadd922014-04-18 10:50:32 +0000135
Chandler Carrutha4499e92016-02-02 03:57:13 +0000136 for (const Edge &E : EntryEdges)
Chandler Carruthe5944d92016-02-17 00:18:16 +0000137 RefSCCEntryNodes.push_back(&E.getFunction());
Chandler Carruthbf71a342014-02-06 04:37:03 +0000138}
139
Chandler Carruthbf71a342014-02-06 04:37:03 +0000140LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
Chandler Carruth2174f442014-04-18 20:44:16 +0000141 : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)),
Chandler Carrutha4499e92016-02-02 03:57:13 +0000142 EntryEdges(std::move(G.EntryEdges)),
Chandler Carruth0b623ba2014-04-23 04:00:17 +0000143 EntryIndexMap(std::move(G.EntryIndexMap)), SCCBPA(std::move(G.SCCBPA)),
Chandler Carruthe5944d92016-02-17 00:18:16 +0000144 SCCMap(std::move(G.SCCMap)), LeafRefSCCs(std::move(G.LeafRefSCCs)),
Chandler Carruth18eadd922014-04-18 10:50:32 +0000145 DFSStack(std::move(G.DFSStack)),
Chandler Carruthe5944d92016-02-17 00:18:16 +0000146 RefSCCEntryNodes(std::move(G.RefSCCEntryNodes)),
Chandler Carruth2174f442014-04-18 20:44:16 +0000147 NextDFSNumber(G.NextDFSNumber) {
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000148 updateGraphPtrs();
149}
150
151LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) {
152 BPA = std::move(G.BPA);
Chandler Carruth2174f442014-04-18 20:44:16 +0000153 NodeMap = std::move(G.NodeMap);
Chandler Carrutha4499e92016-02-02 03:57:13 +0000154 EntryEdges = std::move(G.EntryEdges);
Chandler Carruth0b623ba2014-04-23 04:00:17 +0000155 EntryIndexMap = std::move(G.EntryIndexMap);
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000156 SCCBPA = std::move(G.SCCBPA);
157 SCCMap = std::move(G.SCCMap);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000158 LeafRefSCCs = std::move(G.LeafRefSCCs);
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000159 DFSStack = std::move(G.DFSStack);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000160 RefSCCEntryNodes = std::move(G.RefSCCEntryNodes);
Chandler Carruth2174f442014-04-18 20:44:16 +0000161 NextDFSNumber = G.NextDFSNumber;
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000162 updateGraphPtrs();
163 return *this;
164}
165
Chandler Carruthdca83402016-06-27 23:26:08 +0000166void LazyCallGraph::SCC::dump() const {
167 dbgs() << *this << '\n';
168}
169
Chandler Carruthe5944d92016-02-17 00:18:16 +0000170#ifndef NDEBUG
171void LazyCallGraph::SCC::verify() {
172 assert(OuterRefSCC && "Can't have a null RefSCC!");
173 assert(!Nodes.empty() && "Can't have an empty SCC!");
Chandler Carruth8f92d6d2014-04-26 01:03:46 +0000174
Chandler Carruthe5944d92016-02-17 00:18:16 +0000175 for (Node *N : Nodes) {
176 assert(N && "Can't have a null node!");
177 assert(OuterRefSCC->G->lookupSCC(*N) == this &&
178 "Node does not map to this SCC!");
179 assert(N->DFSNumber == -1 &&
180 "Must set DFS numbers to -1 when adding a node to an SCC!");
181 assert(N->LowLink == -1 &&
182 "Must set low link to -1 when adding a node to an SCC!");
183 for (Edge &E : *N)
184 assert(E.getNode() && "Can't have an edge to a raw function!");
185 }
186}
187#endif
188
189LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {}
190
Chandler Carruthdca83402016-06-27 23:26:08 +0000191void LazyCallGraph::RefSCC::dump() const {
192 dbgs() << *this << '\n';
193}
194
Chandler Carruthe5944d92016-02-17 00:18:16 +0000195#ifndef NDEBUG
196void LazyCallGraph::RefSCC::verify() {
197 assert(G && "Can't have a null graph!");
198 assert(!SCCs.empty() && "Can't have an empty SCC!");
199
200 // Verify basic properties of the SCCs.
Chandler Carruth88823462016-08-24 09:37:14 +0000201 SmallPtrSet<SCC *, 4> SCCSet;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000202 for (SCC *C : SCCs) {
203 assert(C && "Can't have a null SCC!");
204 C->verify();
205 assert(&C->getOuterRefSCC() == this &&
206 "SCC doesn't think it is inside this RefSCC!");
Chandler Carruth88823462016-08-24 09:37:14 +0000207 bool Inserted = SCCSet.insert(C).second;
208 assert(Inserted && "Found a duplicate SCC!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000209 }
210
211 // Check that our indices map correctly.
212 for (auto &SCCIndexPair : SCCIndices) {
213 SCC *C = SCCIndexPair.first;
214 int i = SCCIndexPair.second;
215 assert(C && "Can't have a null SCC in the indices!");
Chandler Carruth88823462016-08-24 09:37:14 +0000216 assert(SCCSet.count(C) && "Found an index for an SCC not in the RefSCC!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000217 assert(SCCs[i] == C && "Index doesn't point to SCC!");
218 }
219
220 // Check that the SCCs are in fact in post-order.
221 for (int i = 0, Size = SCCs.size(); i < Size; ++i) {
222 SCC &SourceSCC = *SCCs[i];
223 for (Node &N : SourceSCC)
224 for (Edge &E : N) {
225 if (!E.isCall())
226 continue;
227 SCC &TargetSCC = *G->lookupSCC(*E.getNode());
228 if (&TargetSCC.getOuterRefSCC() == this) {
229 assert(SCCIndices.find(&TargetSCC)->second <= i &&
230 "Edge between SCCs violates post-order relationship.");
231 continue;
232 }
233 assert(TargetSCC.getOuterRefSCC().Parents.count(this) &&
234 "Edge to a RefSCC missing us in its parent set.");
235 }
236 }
237}
238#endif
239
240bool LazyCallGraph::RefSCC::isDescendantOf(const RefSCC &C) const {
Chandler Carruth4b096742014-05-01 12:12:42 +0000241 // Walk up the parents of this SCC and verify that we eventually find C.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000242 SmallVector<const RefSCC *, 4> AncestorWorklist;
Chandler Carruth4b096742014-05-01 12:12:42 +0000243 AncestorWorklist.push_back(this);
244 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000245 const RefSCC *AncestorC = AncestorWorklist.pop_back_val();
Chandler Carruth4b096742014-05-01 12:12:42 +0000246 if (AncestorC->isChildOf(C))
247 return true;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000248 for (const RefSCC *ParentC : AncestorC->Parents)
Chandler Carruth4b096742014-05-01 12:12:42 +0000249 AncestorWorklist.push_back(ParentC);
250 } while (!AncestorWorklist.empty());
251
252 return false;
253}
254
Chandler Carruth1f621f02016-09-04 08:34:24 +0000255/// Generic helper that updates a postorder sequence of SCCs for a potentially
256/// cycle-introducing edge insertion.
257///
258/// A postorder sequence of SCCs of a directed graph has one fundamental
259/// property: all deges in the DAG of SCCs point "up" the sequence. That is,
260/// all edges in the SCC DAG point to prior SCCs in the sequence.
261///
262/// This routine both updates a postorder sequence and uses that sequence to
263/// compute the set of SCCs connected into a cycle. It should only be called to
264/// insert a "downward" edge which will require changing the sequence to
265/// restore it to a postorder.
266///
267/// When inserting an edge from an earlier SCC to a later SCC in some postorder
268/// sequence, all of the SCCs which may be impacted are in the closed range of
269/// those two within the postorder sequence. The algorithm used here to restore
270/// the state is as follows:
271///
272/// 1) Starting from the source SCC, construct a set of SCCs which reach the
273/// source SCC consisting of just the source SCC. Then scan toward the
274/// target SCC in postorder and for each SCC, if it has an edge to an SCC
275/// in the set, add it to the set. Otherwise, the source SCC is not
276/// a successor, move it in the postorder sequence to immediately before
277/// the source SCC, shifting the source SCC and all SCCs in the set one
278/// position toward the target SCC. Stop scanning after processing the
279/// target SCC.
280/// 2) If the source SCC is now past the target SCC in the postorder sequence,
281/// and thus the new edge will flow toward the start, we are done.
282/// 3) Otherwise, starting from the target SCC, walk all edges which reach an
283/// SCC between the source and the target, and add them to the set of
284/// connected SCCs, then recurse through them. Once a complete set of the
285/// SCCs the target connects to is known, hoist the remaining SCCs between
286/// the source and the target to be above the target. Note that there is no
287/// need to process the source SCC, it is already known to connect.
288/// 4) At this point, all of the SCCs in the closed range between the source
289/// SCC and the target SCC in the postorder sequence are connected,
290/// including the target SCC and the source SCC. Inserting the edge from
291/// the source SCC to the target SCC will form a cycle out of precisely
292/// these SCCs. Thus we can merge all of the SCCs in this closed range into
293/// a single SCC.
294///
295/// This process has various important properties:
296/// - Only mutates the SCCs when adding the edge actually changes the SCC
297/// structure.
298/// - Never mutates SCCs which are unaffected by the change.
299/// - Updates the postorder sequence to correctly satisfy the postorder
300/// constraint after the edge is inserted.
301/// - Only reorders SCCs in the closed postorder sequence from the source to
302/// the target, so easy to bound how much has changed even in the ordering.
303/// - Big-O is the number of edges in the closed postorder range of SCCs from
304/// source to target.
305///
306/// This helper routine, in addition to updating the postorder sequence itself
307/// will also update a map from SCCs to indices within that sequecne.
308///
309/// The sequence and the map must operate on pointers to the SCC type.
310///
311/// Two callbacks must be provided. The first computes the subset of SCCs in
312/// the postorder closed range from the source to the target which connect to
313/// the source SCC via some (transitive) set of edges. The second computes the
314/// subset of the same range which the target SCC connects to via some
315/// (transitive) set of edges. Both callbacks should populate the set argument
316/// provided.
317template <typename SCCT, typename PostorderSequenceT, typename SCCIndexMapT,
318 typename ComputeSourceConnectedSetCallableT,
319 typename ComputeTargetConnectedSetCallableT>
320static iterator_range<typename PostorderSequenceT::iterator>
321updatePostorderSequenceForEdgeInsertion(
322 SCCT &SourceSCC, SCCT &TargetSCC, PostorderSequenceT &SCCs,
323 SCCIndexMapT &SCCIndices,
324 ComputeSourceConnectedSetCallableT ComputeSourceConnectedSet,
325 ComputeTargetConnectedSetCallableT ComputeTargetConnectedSet) {
326 int SourceIdx = SCCIndices[&SourceSCC];
327 int TargetIdx = SCCIndices[&TargetSCC];
328 assert(SourceIdx < TargetIdx && "Cannot have equal indices here!");
329
330 SmallPtrSet<SCCT *, 4> ConnectedSet;
331
332 // Compute the SCCs which (transitively) reach the source.
333 ComputeSourceConnectedSet(ConnectedSet);
334
335 // Partition the SCCs in this part of the port-order sequence so only SCCs
336 // connecting to the source remain between it and the target. This is
337 // a benign partition as it preserves postorder.
338 auto SourceI = std::stable_partition(
339 SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx + 1,
340 [&ConnectedSet](SCCT *C) { return !ConnectedSet.count(C); });
341 for (int i = SourceIdx, e = TargetIdx + 1; i < e; ++i)
342 SCCIndices.find(SCCs[i])->second = i;
343
344 // If the target doesn't connect to the source, then we've corrected the
345 // post-order and there are no cycles formed.
346 if (!ConnectedSet.count(&TargetSCC)) {
347 assert(SourceI > (SCCs.begin() + SourceIdx) &&
348 "Must have moved the source to fix the post-order.");
349 assert(*std::prev(SourceI) == &TargetSCC &&
350 "Last SCC to move should have bene the target.");
351
352 // Return an empty range at the target SCC indicating there is nothing to
353 // merge.
354 return make_range(std::prev(SourceI), std::prev(SourceI));
355 }
356
357 assert(SCCs[TargetIdx] == &TargetSCC &&
358 "Should not have moved target if connected!");
359 SourceIdx = SourceI - SCCs.begin();
360 assert(SCCs[SourceIdx] == &SourceSCC &&
361 "Bad updated index computation for the source SCC!");
362
363
364 // See whether there are any remaining intervening SCCs between the source
365 // and target. If so we need to make sure they all are reachable form the
366 // target.
367 if (SourceIdx + 1 < TargetIdx) {
368 ConnectedSet.clear();
369 ComputeTargetConnectedSet(ConnectedSet);
370
371 // Partition SCCs so that only SCCs reached from the target remain between
372 // the source and the target. This preserves postorder.
373 auto TargetI = std::stable_partition(
374 SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1,
375 [&ConnectedSet](SCCT *C) { return ConnectedSet.count(C); });
376 for (int i = SourceIdx + 1, e = TargetIdx + 1; i < e; ++i)
377 SCCIndices.find(SCCs[i])->second = i;
378 TargetIdx = std::prev(TargetI) - SCCs.begin();
379 assert(SCCs[TargetIdx] == &TargetSCC &&
380 "Should always end with the target!");
381 }
382
383 // At this point, we know that connecting source to target forms a cycle
384 // because target connects back to source, and we know that all of the SCCs
385 // between the source and target in the postorder sequence participate in that
386 // cycle.
387 return make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx);
388}
389
Chandler Carruthe5944d92016-02-17 00:18:16 +0000390SmallVector<LazyCallGraph::SCC *, 1>
391LazyCallGraph::RefSCC::switchInternalEdgeToCall(Node &SourceN, Node &TargetN) {
392 assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000393 SmallVector<SCC *, 1> DeletedSCCs;
Chandler Carruth5217c942014-04-30 10:48:36 +0000394
Chandler Carruth11b3f602016-09-04 08:34:31 +0000395#ifndef NDEBUG
396 // In a debug build, verify the RefSCC is valid to start with and when this
397 // routine finishes.
398 verify();
399 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
400#endif
401
Chandler Carruthe5944d92016-02-17 00:18:16 +0000402 SCC &SourceSCC = *G->lookupSCC(SourceN);
403 SCC &TargetSCC = *G->lookupSCC(TargetN);
404
405 // If the two nodes are already part of the same SCC, we're also done as
406 // we've just added more connectivity.
407 if (&SourceSCC == &TargetSCC) {
408 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000409 return DeletedSCCs;
410 }
411
412 // At this point we leverage the postorder list of SCCs to detect when the
413 // insertion of an edge changes the SCC structure in any way.
414 //
415 // First and foremost, we can eliminate the need for any changes when the
416 // edge is toward the beginning of the postorder sequence because all edges
417 // flow in that direction already. Thus adding a new one cannot form a cycle.
418 int SourceIdx = SCCIndices[&SourceSCC];
419 int TargetIdx = SCCIndices[&TargetSCC];
420 if (TargetIdx < SourceIdx) {
421 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000422 return DeletedSCCs;
423 }
424
Chandler Carruthe5944d92016-02-17 00:18:16 +0000425 // Compute the SCCs which (transitively) reach the source.
Chandler Carruth1f621f02016-09-04 08:34:24 +0000426 auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000427#ifndef NDEBUG
Chandler Carruth1f621f02016-09-04 08:34:24 +0000428 // Check that the RefSCC is still valid before computing this as the
429 // results will be nonsensical of we've broken its invariants.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000430 verify();
431#endif
Chandler Carruth1f621f02016-09-04 08:34:24 +0000432 ConnectedSet.insert(&SourceSCC);
433 auto IsConnected = [&](SCC &C) {
434 for (Node &N : C)
435 for (Edge &E : N.calls()) {
436 assert(E.getNode() && "Must have formed a node within an SCC!");
437 if (ConnectedSet.count(G->lookupSCC(*E.getNode())))
438 return true;
439 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000440
Chandler Carruth1f621f02016-09-04 08:34:24 +0000441 return false;
442 };
Chandler Carruthe5944d92016-02-17 00:18:16 +0000443
Chandler Carruth1f621f02016-09-04 08:34:24 +0000444 for (SCC *C :
445 make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1))
446 if (IsConnected(*C))
447 ConnectedSet.insert(C);
448 };
449
450 // Use a normal worklist to find which SCCs the target connects to. We still
451 // bound the search based on the range in the postorder list we care about,
452 // but because this is forward connectivity we just "recurse" through the
453 // edges.
454 auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000455#ifndef NDEBUG
Chandler Carruth1f621f02016-09-04 08:34:24 +0000456 // Check that the RefSCC is still valid before computing this as the
457 // results will be nonsensical of we've broken its invariants.
458 verify();
Chandler Carruthe5944d92016-02-17 00:18:16 +0000459#endif
Chandler Carruthe5944d92016-02-17 00:18:16 +0000460 ConnectedSet.insert(&TargetSCC);
461 SmallVector<SCC *, 4> Worklist;
462 Worklist.push_back(&TargetSCC);
463 do {
464 SCC &C = *Worklist.pop_back_val();
465 for (Node &N : C)
466 for (Edge &E : N) {
467 assert(E.getNode() && "Must have formed a node within an SCC!");
468 if (!E.isCall())
469 continue;
470 SCC &EdgeC = *G->lookupSCC(*E.getNode());
471 if (&EdgeC.getOuterRefSCC() != this)
472 // Not in this RefSCC...
473 continue;
474 if (SCCIndices.find(&EdgeC)->second <= SourceIdx)
475 // Not in the postorder sequence between source and target.
476 continue;
477
478 if (ConnectedSet.insert(&EdgeC).second)
479 Worklist.push_back(&EdgeC);
480 }
481 } while (!Worklist.empty());
Chandler Carruth1f621f02016-09-04 08:34:24 +0000482 };
Chandler Carruthe5944d92016-02-17 00:18:16 +0000483
Chandler Carruth1f621f02016-09-04 08:34:24 +0000484 // Use a generic helper to update the postorder sequence of SCCs and return
485 // a range of any SCCs connected into a cycle by inserting this edge. This
486 // routine will also take care of updating the indices into the postorder
487 // sequence.
488 auto MergeRange = updatePostorderSequenceForEdgeInsertion(
489 SourceSCC, TargetSCC, SCCs, SCCIndices, ComputeSourceConnectedSet,
490 ComputeTargetConnectedSet);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000491
Chandler Carruth1f621f02016-09-04 08:34:24 +0000492 // If the merge range is empty, then adding the edge didn't actually form any
493 // new cycles. We're done.
494 if (MergeRange.begin() == MergeRange.end()) {
495 // Now that the SCC structure is finalized, flip the kind to call.
496 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
Chandler Carruth1f621f02016-09-04 08:34:24 +0000497 return DeletedSCCs;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000498 }
499
Chandler Carruth1f621f02016-09-04 08:34:24 +0000500#ifndef NDEBUG
501 // Before merging, check that the RefSCC remains valid after all the
502 // postorder updates.
503 verify();
504#endif
505
506 // Otherwise we need to merge all of the SCCs in the cycle into a single
Chandler Carruthe5944d92016-02-17 00:18:16 +0000507 // result SCC.
508 //
509 // NB: We merge into the target because all of these functions were already
510 // reachable from the target, meaning any SCC-wide properties deduced about it
511 // other than the set of functions within it will not have changed.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000512 for (SCC *C : MergeRange) {
513 assert(C != &TargetSCC &&
514 "We merge *into* the target and shouldn't process it here!");
515 SCCIndices.erase(C);
516 TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end());
517 for (Node *N : C->Nodes)
518 G->SCCMap[N] = &TargetSCC;
519 C->clear();
520 DeletedSCCs.push_back(C);
521 }
522
523 // Erase the merged SCCs from the list and update the indices of the
524 // remaining SCCs.
525 int IndexOffset = MergeRange.end() - MergeRange.begin();
526 auto EraseEnd = SCCs.erase(MergeRange.begin(), MergeRange.end());
527 for (SCC *C : make_range(EraseEnd, SCCs.end()))
528 SCCIndices[C] -= IndexOffset;
529
530 // Now that the SCC structure is finalized, flip the kind to call.
531 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
532
Chandler Carruth11b3f602016-09-04 08:34:31 +0000533 // And we're done!
Chandler Carruthe5944d92016-02-17 00:18:16 +0000534 return DeletedSCCs;
Chandler Carruth5217c942014-04-30 10:48:36 +0000535}
536
Chandler Carruth88823462016-08-24 09:37:14 +0000537iterator_range<LazyCallGraph::RefSCC::iterator>
538LazyCallGraph::RefSCC::switchInternalEdgeToRef(Node &SourceN, Node &TargetN) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000539 assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
540
Chandler Carruth11b3f602016-09-04 08:34:31 +0000541#ifndef NDEBUG
542 // In a debug build, verify the RefSCC is valid to start with and when this
543 // routine finishes.
544 verify();
545 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
546#endif
547
Chandler Carruthe5944d92016-02-17 00:18:16 +0000548 SCC &SourceSCC = *G->lookupSCC(SourceN);
549 SCC &TargetSCC = *G->lookupSCC(TargetN);
550
551 assert(&SourceSCC.getOuterRefSCC() == this &&
552 "Source must be in this RefSCC.");
553 assert(&TargetSCC.getOuterRefSCC() == this &&
554 "Target must be in this RefSCC.");
555
556 // Set the edge kind.
557 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
558
559 // If this call edge is just connecting two separate SCCs within this RefSCC,
560 // there is nothing to do.
Chandler Carruth11b3f602016-09-04 08:34:31 +0000561 if (&SourceSCC != &TargetSCC)
Chandler Carruth88823462016-08-24 09:37:14 +0000562 return make_range(SCCs.end(), SCCs.end());
Chandler Carruthe5944d92016-02-17 00:18:16 +0000563
564 // Otherwise we are removing a call edge from a single SCC. This may break
565 // the cycle. In order to compute the new set of SCCs, we need to do a small
566 // DFS over the nodes within the SCC to form any sub-cycles that remain as
567 // distinct SCCs and compute a postorder over the resulting SCCs.
568 //
569 // However, we specially handle the target node. The target node is known to
570 // reach all other nodes in the original SCC by definition. This means that
571 // we want the old SCC to be replaced with an SCC contaning that node as it
572 // will be the root of whatever SCC DAG results from the DFS. Assumptions
573 // about an SCC such as the set of functions called will continue to hold,
574 // etc.
575
576 SCC &OldSCC = TargetSCC;
577 SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
578 SmallVector<Node *, 16> PendingSCCStack;
579 SmallVector<SCC *, 4> NewSCCs;
580
581 // Prepare the nodes for a fresh DFS.
582 SmallVector<Node *, 16> Worklist;
583 Worklist.swap(OldSCC.Nodes);
584 for (Node *N : Worklist) {
585 N->DFSNumber = N->LowLink = 0;
586 G->SCCMap.erase(N);
587 }
588
589 // Force the target node to be in the old SCC. This also enables us to take
590 // a very significant short-cut in the standard Tarjan walk to re-form SCCs
591 // below: whenever we build an edge that reaches the target node, we know
592 // that the target node eventually connects back to all other nodes in our
593 // walk. As a consequence, we can detect and handle participants in that
594 // cycle without walking all the edges that form this connection, and instead
595 // by relying on the fundamental guarantee coming into this operation (all
596 // nodes are reachable from the target due to previously forming an SCC).
597 TargetN.DFSNumber = TargetN.LowLink = -1;
598 OldSCC.Nodes.push_back(&TargetN);
599 G->SCCMap[&TargetN] = &OldSCC;
600
601 // Scan down the stack and DFS across the call edges.
602 for (Node *RootN : Worklist) {
603 assert(DFSStack.empty() &&
604 "Cannot begin a new root with a non-empty DFS stack!");
605 assert(PendingSCCStack.empty() &&
606 "Cannot begin a new root with pending nodes for an SCC!");
607
608 // Skip any nodes we've already reached in the DFS.
609 if (RootN->DFSNumber != 0) {
610 assert(RootN->DFSNumber == -1 &&
611 "Shouldn't have any mid-DFS root nodes!");
612 continue;
613 }
614
615 RootN->DFSNumber = RootN->LowLink = 1;
616 int NextDFSNumber = 2;
617
618 DFSStack.push_back({RootN, RootN->call_begin()});
619 do {
620 Node *N;
621 call_edge_iterator I;
622 std::tie(N, I) = DFSStack.pop_back_val();
623 auto E = N->call_end();
624 while (I != E) {
625 Node &ChildN = *I->getNode();
626 if (ChildN.DFSNumber == 0) {
627 // We haven't yet visited this child, so descend, pushing the current
628 // node onto the stack.
629 DFSStack.push_back({N, I});
630
631 assert(!G->SCCMap.count(&ChildN) &&
632 "Found a node with 0 DFS number but already in an SCC!");
633 ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
634 N = &ChildN;
635 I = N->call_begin();
636 E = N->call_end();
637 continue;
638 }
639
640 // Check for the child already being part of some component.
641 if (ChildN.DFSNumber == -1) {
642 if (G->lookupSCC(ChildN) == &OldSCC) {
643 // If the child is part of the old SCC, we know that it can reach
644 // every other node, so we have formed a cycle. Pull the entire DFS
645 // and pending stacks into it. See the comment above about setting
646 // up the old SCC for why we do this.
647 int OldSize = OldSCC.size();
648 OldSCC.Nodes.push_back(N);
649 OldSCC.Nodes.append(PendingSCCStack.begin(), PendingSCCStack.end());
650 PendingSCCStack.clear();
651 while (!DFSStack.empty())
652 OldSCC.Nodes.push_back(DFSStack.pop_back_val().first);
653 for (Node &N : make_range(OldSCC.begin() + OldSize, OldSCC.end())) {
654 N.DFSNumber = N.LowLink = -1;
655 G->SCCMap[&N] = &OldSCC;
656 }
657 N = nullptr;
658 break;
659 }
660
661 // If the child has already been added to some child component, it
662 // couldn't impact the low-link of this parent because it isn't
663 // connected, and thus its low-link isn't relevant so skip it.
664 ++I;
665 continue;
666 }
667
668 // Track the lowest linked child as the lowest link for this node.
669 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
670 if (ChildN.LowLink < N->LowLink)
671 N->LowLink = ChildN.LowLink;
672
673 // Move to the next edge.
674 ++I;
675 }
676 if (!N)
677 // Cleared the DFS early, start another round.
678 break;
679
680 // We've finished processing N and its descendents, put it on our pending
681 // SCC stack to eventually get merged into an SCC of nodes.
682 PendingSCCStack.push_back(N);
683
684 // If this node is linked to some lower entry, continue walking up the
685 // stack.
686 if (N->LowLink != N->DFSNumber)
687 continue;
688
689 // Otherwise, we've completed an SCC. Append it to our post order list of
690 // SCCs.
691 int RootDFSNumber = N->DFSNumber;
692 // Find the range of the node stack by walking down until we pass the
693 // root DFS number.
694 auto SCCNodes = make_range(
695 PendingSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +0000696 find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
697 return N->DFSNumber < RootDFSNumber;
698 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +0000699
700 // Form a new SCC out of these nodes and then clear them off our pending
701 // stack.
702 NewSCCs.push_back(G->createSCC(*this, SCCNodes));
703 for (Node &N : *NewSCCs.back()) {
704 N.DFSNumber = N.LowLink = -1;
705 G->SCCMap[&N] = NewSCCs.back();
706 }
707 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
708 } while (!DFSStack.empty());
709 }
710
711 // Insert the remaining SCCs before the old one. The old SCC can reach all
712 // other SCCs we form because it contains the target node of the removed edge
713 // of the old SCC. This means that we will have edges into all of the new
714 // SCCs, which means the old one must come last for postorder.
715 int OldIdx = SCCIndices[&OldSCC];
716 SCCs.insert(SCCs.begin() + OldIdx, NewSCCs.begin(), NewSCCs.end());
717
718 // Update the mapping from SCC* to index to use the new SCC*s, and remove the
719 // old SCC from the mapping.
720 for (int Idx = OldIdx, Size = SCCs.size(); Idx < Size; ++Idx)
721 SCCIndices[SCCs[Idx]] = Idx;
722
Chandler Carruth88823462016-08-24 09:37:14 +0000723 return make_range(SCCs.begin() + OldIdx,
724 SCCs.begin() + OldIdx + NewSCCs.size());
Chandler Carruthe5944d92016-02-17 00:18:16 +0000725}
726
727void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN,
728 Node &TargetN) {
729 assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
730
731 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
732 assert(G->lookupRefSCC(TargetN) != this &&
733 "Target must not be in this RefSCC.");
734 assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
735 "Target must be a descendant of the Source.");
736
737 // Edges between RefSCCs are the same regardless of call or ref, so we can
738 // just flip the edge here.
739 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
740
741#ifndef NDEBUG
742 // Check that the RefSCC is still valid.
743 verify();
744#endif
745}
746
747void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN,
748 Node &TargetN) {
749 assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
750
751 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
752 assert(G->lookupRefSCC(TargetN) != this &&
753 "Target must not be in this RefSCC.");
754 assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
755 "Target must be a descendant of the Source.");
756
757 // Edges between RefSCCs are the same regardless of call or ref, so we can
758 // just flip the edge here.
759 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
760
761#ifndef NDEBUG
762 // Check that the RefSCC is still valid.
763 verify();
764#endif
765}
766
767void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN,
768 Node &TargetN) {
769 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
770 assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
771
772 SourceN.insertEdgeInternal(TargetN, Edge::Ref);
773
774#ifndef NDEBUG
775 // Check that the RefSCC is still valid.
776 verify();
777#endif
778}
779
780void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN,
781 Edge::Kind EK) {
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000782 // First insert it into the caller.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000783 SourceN.insertEdgeInternal(TargetN, EK);
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000784
Chandler Carruthe5944d92016-02-17 00:18:16 +0000785 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000786
Chandler Carruthe5944d92016-02-17 00:18:16 +0000787 RefSCC &TargetC = *G->lookupRefSCC(TargetN);
788 assert(&TargetC != this && "Target must not be in this RefSCC.");
789 assert(TargetC.isDescendantOf(*this) &&
790 "Target must be a descendant of the Source.");
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000791
Chandler Carruth91539112015-12-28 01:54:20 +0000792 // The only change required is to add this SCC to the parent set of the
793 // callee.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000794 TargetC.Parents.insert(this);
795
796#ifndef NDEBUG
797 // Check that the RefSCC is still valid.
798 verify();
799#endif
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000800}
801
Chandler Carruthe5944d92016-02-17 00:18:16 +0000802SmallVector<LazyCallGraph::RefSCC *, 1>
803LazyCallGraph::RefSCC::insertIncomingRefEdge(Node &SourceN, Node &TargetN) {
804 assert(G->lookupRefSCC(TargetN) == this && "Target must be in this SCC.");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000805
Chandler Carruth11b3f602016-09-04 08:34:31 +0000806#ifndef NDEBUG
807 // In a debug build, verify the RefSCC is valid to start with and when this
808 // routine finishes.
809 verify();
810 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
811#endif
812
Chandler Carruthe5944d92016-02-17 00:18:16 +0000813 // We store the RefSCCs found to be connected in postorder so that we can use
814 // that when merging. We also return this to the caller to allow them to
815 // invalidate information pertaining to these RefSCCs.
816 SmallVector<RefSCC *, 1> Connected;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000817
Chandler Carruthe5944d92016-02-17 00:18:16 +0000818 RefSCC &SourceC = *G->lookupRefSCC(SourceN);
819 assert(&SourceC != this && "Source must not be in this SCC.");
820 assert(SourceC.isDescendantOf(*this) &&
821 "Source must be a descendant of the Target.");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000822
823 // The algorithm we use for merging SCCs based on the cycle introduced here
Chandler Carruthe5944d92016-02-17 00:18:16 +0000824 // is to walk the RefSCC inverted DAG formed by the parent sets. The inverse
825 // graph has the same cycle properties as the actual DAG of the RefSCCs, and
826 // when forming RefSCCs lazily by a DFS, the bottom of the graph won't exist
827 // in many cases which should prune the search space.
Chandler Carruth312dddf2014-05-04 09:38:32 +0000828 //
Chandler Carruthe5944d92016-02-17 00:18:16 +0000829 // FIXME: We can get this pruning behavior even after the incremental RefSCC
Chandler Carruth312dddf2014-05-04 09:38:32 +0000830 // formation by leaving behind (conservative) DFS numberings in the nodes,
831 // and pruning the search with them. These would need to be cleverly updated
832 // during the removal of intra-SCC edges, but could be preserved
833 // conservatively.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000834 //
835 // FIXME: This operation currently creates ordering stability problems
836 // because we don't use stably ordered containers for the parent SCCs.
Chandler Carruth312dddf2014-05-04 09:38:32 +0000837
Chandler Carruthe5944d92016-02-17 00:18:16 +0000838 // The set of RefSCCs that are connected to the parent, and thus will
Chandler Carruth312dddf2014-05-04 09:38:32 +0000839 // participate in the merged connected component.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000840 SmallPtrSet<RefSCC *, 8> ConnectedSet;
841 ConnectedSet.insert(this);
Chandler Carruth312dddf2014-05-04 09:38:32 +0000842
843 // We build up a DFS stack of the parents chains.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000844 SmallVector<std::pair<RefSCC *, parent_iterator>, 8> DFSStack;
845 SmallPtrSet<RefSCC *, 8> Visited;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000846 int ConnectedDepth = -1;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000847 DFSStack.push_back({&SourceC, SourceC.parent_begin()});
848 do {
849 auto DFSPair = DFSStack.pop_back_val();
850 RefSCC *C = DFSPair.first;
851 parent_iterator I = DFSPair.second;
852 auto E = C->parent_end();
853
Chandler Carruth312dddf2014-05-04 09:38:32 +0000854 while (I != E) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000855 RefSCC &Parent = *I++;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000856
857 // If we have already processed this parent SCC, skip it, and remember
858 // whether it was connected so we don't have to check the rest of the
859 // stack. This also handles when we reach a child of the 'this' SCC (the
860 // callee) which terminates the search.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000861 if (ConnectedSet.count(&Parent)) {
862 assert(ConnectedDepth < (int)DFSStack.size() &&
863 "Cannot have a connected depth greater than the DFS depth!");
864 ConnectedDepth = DFSStack.size();
Chandler Carruth312dddf2014-05-04 09:38:32 +0000865 continue;
866 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000867 if (Visited.count(&Parent))
Chandler Carruth312dddf2014-05-04 09:38:32 +0000868 continue;
869
870 // We fully explore the depth-first space, adding nodes to the connected
871 // set only as we pop them off, so "recurse" by rotating to the parent.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000872 DFSStack.push_back({C, I});
873 C = &Parent;
874 I = C->parent_begin();
875 E = C->parent_end();
Chandler Carruth312dddf2014-05-04 09:38:32 +0000876 }
877
878 // If we've found a connection anywhere below this point on the stack (and
879 // thus up the parent graph from the caller), the current node needs to be
880 // added to the connected set now that we've processed all of its parents.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000881 if ((int)DFSStack.size() == ConnectedDepth) {
Chandler Carruth312dddf2014-05-04 09:38:32 +0000882 --ConnectedDepth; // We're finished with this connection.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000883 bool Inserted = ConnectedSet.insert(C).second;
884 (void)Inserted;
885 assert(Inserted && "Cannot insert a refSCC multiple times!");
886 Connected.push_back(C);
Chandler Carruth312dddf2014-05-04 09:38:32 +0000887 } else {
888 // Otherwise remember that its parents don't ever connect.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000889 assert(ConnectedDepth < (int)DFSStack.size() &&
Chandler Carruth312dddf2014-05-04 09:38:32 +0000890 "Cannot have a connected depth greater than the DFS depth!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000891 Visited.insert(C);
Chandler Carruth312dddf2014-05-04 09:38:32 +0000892 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000893 } while (!DFSStack.empty());
Chandler Carruth312dddf2014-05-04 09:38:32 +0000894
895 // Now that we have identified all of the SCCs which need to be merged into
896 // a connected set with the inserted edge, merge all of them into this SCC.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000897 // We walk the newly connected RefSCCs in the reverse postorder of the parent
898 // DAG walk above and merge in each of their SCC postorder lists. This
899 // ensures a merged postorder SCC list.
900 SmallVector<SCC *, 16> MergedSCCs;
901 int SCCIndex = 0;
902 for (RefSCC *C : reverse(Connected)) {
903 assert(C != this &&
904 "This RefSCC should terminate the DFS without being reached.");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000905
Chandler Carruthe5944d92016-02-17 00:18:16 +0000906 // Merge the parents which aren't part of the merge into the our parents.
907 for (RefSCC *ParentC : C->Parents)
908 if (!ConnectedSet.count(ParentC))
909 Parents.insert(ParentC);
910 C->Parents.clear();
911
912 // Walk the inner SCCs to update their up-pointer and walk all the edges to
913 // update any parent sets.
914 // FIXME: We should try to find a way to avoid this (rather expensive) edge
915 // walk by updating the parent sets in some other manner.
916 for (SCC &InnerC : *C) {
917 InnerC.OuterRefSCC = this;
918 SCCIndices[&InnerC] = SCCIndex++;
919 for (Node &N : InnerC) {
920 G->SCCMap[&N] = &InnerC;
921 for (Edge &E : N) {
922 assert(E.getNode() &&
923 "Cannot have a null node within a visited SCC!");
924 RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
925 if (ConnectedSet.count(&ChildRC))
926 continue;
927 ChildRC.Parents.erase(C);
928 ChildRC.Parents.insert(this);
929 }
Chandler Carruth312dddf2014-05-04 09:38:32 +0000930 }
Chandler Carruth312dddf2014-05-04 09:38:32 +0000931 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000932
933 // Now merge in the SCCs. We can actually move here so try to reuse storage
934 // the first time through.
935 if (MergedSCCs.empty())
936 MergedSCCs = std::move(C->SCCs);
937 else
938 MergedSCCs.append(C->SCCs.begin(), C->SCCs.end());
939 C->SCCs.clear();
Chandler Carruth312dddf2014-05-04 09:38:32 +0000940 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000941
942 // Finally append our original SCCs to the merged list and move it into
943 // place.
944 for (SCC &InnerC : *this)
945 SCCIndices[&InnerC] = SCCIndex++;
946 MergedSCCs.append(SCCs.begin(), SCCs.end());
947 SCCs = std::move(MergedSCCs);
948
949 // At this point we have a merged RefSCC with a post-order SCCs list, just
950 // connect the nodes to form the new edge.
951 SourceN.insertEdgeInternal(TargetN, Edge::Ref);
952
Chandler Carruth312dddf2014-05-04 09:38:32 +0000953 // We return the list of SCCs which were merged so that callers can
954 // invalidate any data they have associated with those SCCs. Note that these
955 // SCCs are no longer in an interesting state (they are totally empty) but
956 // the pointers will remain stable for the life of the graph itself.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000957 return Connected;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000958}
959
Chandler Carruthe5944d92016-02-17 00:18:16 +0000960void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) {
961 assert(G->lookupRefSCC(SourceN) == this &&
962 "The source must be a member of this RefSCC.");
963
964 RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
965 assert(&TargetRC != this && "The target must not be a member of this RefSCC");
966
David Majnemer0d955d02016-08-11 22:21:41 +0000967 assert(!is_contained(G->LeafRefSCCs, this) &&
Chandler Carruthe5944d92016-02-17 00:18:16 +0000968 "Cannot have a leaf RefSCC source.");
969
Chandler Carruth11b3f602016-09-04 08:34:31 +0000970#ifndef NDEBUG
971 // In a debug build, verify the RefSCC is valid to start with and when this
972 // routine finishes.
973 verify();
974 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
975#endif
976
Chandler Carruthaa839b22014-04-27 01:59:50 +0000977 // First remove it from the node.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000978 SourceN.removeEdgeInternal(TargetN.getFunction());
Chandler Carruthaa839b22014-04-27 01:59:50 +0000979
Chandler Carruthe5944d92016-02-17 00:18:16 +0000980 bool HasOtherEdgeToChildRC = false;
981 bool HasOtherChildRC = false;
982 for (SCC *InnerC : SCCs) {
983 for (Node &N : *InnerC) {
984 for (Edge &E : N) {
985 assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
986 RefSCC &OtherChildRC = *G->lookupRefSCC(*E.getNode());
987 if (&OtherChildRC == &TargetRC) {
988 HasOtherEdgeToChildRC = true;
989 break;
990 }
991 if (&OtherChildRC != this)
992 HasOtherChildRC = true;
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000993 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000994 if (HasOtherEdgeToChildRC)
995 break;
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000996 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000997 if (HasOtherEdgeToChildRC)
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000998 break;
999 }
1000 // Because the SCCs form a DAG, deleting such an edge cannot change the set
1001 // of SCCs in the graph. However, it may cut an edge of the SCC DAG, making
Chandler Carruthe5944d92016-02-17 00:18:16 +00001002 // the source SCC no longer connected to the target SCC. If so, we need to
1003 // update the target SCC's map of its parents.
1004 if (!HasOtherEdgeToChildRC) {
1005 bool Removed = TargetRC.Parents.erase(this);
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001006 (void)Removed;
1007 assert(Removed &&
Chandler Carruthe5944d92016-02-17 00:18:16 +00001008 "Did not find the source SCC in the target SCC's parent list!");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001009
1010 // It may orphan an SCC if it is the last edge reaching it, but that does
1011 // not violate any invariants of the graph.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001012 if (TargetRC.Parents.empty())
1013 DEBUG(dbgs() << "LCG: Update removing " << SourceN.getFunction().getName()
1014 << " -> " << TargetN.getFunction().getName()
Chandler Carruthaa839b22014-04-27 01:59:50 +00001015 << " edge orphaned the callee's SCC!\n");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001016
Chandler Carruthe5944d92016-02-17 00:18:16 +00001017 // It may make the Source SCC a leaf SCC.
1018 if (!HasOtherChildRC)
1019 G->LeafRefSCCs.push_back(this);
Chandler Carruthaca48d02014-04-26 09:06:53 +00001020 }
1021}
1022
Chandler Carruthe5944d92016-02-17 00:18:16 +00001023SmallVector<LazyCallGraph::RefSCC *, 1>
1024LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN, Node &TargetN) {
1025 assert(!SourceN[TargetN].isCall() &&
1026 "Cannot remove a call edge, it must first be made a ref edge");
Chandler Carruthaa839b22014-04-27 01:59:50 +00001027
Chandler Carruth11b3f602016-09-04 08:34:31 +00001028#ifndef NDEBUG
1029 // In a debug build, verify the RefSCC is valid to start with and when this
1030 // routine finishes.
1031 verify();
1032 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
1033#endif
1034
Chandler Carruthe5944d92016-02-17 00:18:16 +00001035 // First remove the actual edge.
1036 SourceN.removeEdgeInternal(TargetN.getFunction());
1037
1038 // We return a list of the resulting *new* RefSCCs in post-order.
1039 SmallVector<RefSCC *, 1> Result;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001040
Chandler Carrutha7205b62014-04-26 03:36:37 +00001041 // Direct recursion doesn't impact the SCC graph at all.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001042 if (&SourceN == &TargetN)
1043 return Result;
Chandler Carrutha7205b62014-04-26 03:36:37 +00001044
Chandler Carruthe5944d92016-02-17 00:18:16 +00001045 // We build somewhat synthetic new RefSCCs by providing a postorder mapping
1046 // for each inner SCC. We also store these associated with *nodes* rather
1047 // than SCCs because this saves a round-trip through the node->SCC map and in
1048 // the common case, SCCs are small. We will verify that we always give the
1049 // same number to every node in the SCC such that these are equivalent.
1050 const int RootPostOrderNumber = 0;
1051 int PostOrderNumber = RootPostOrderNumber + 1;
1052 SmallDenseMap<Node *, int> PostOrderMapping;
1053
1054 // Every node in the target SCC can already reach every node in this RefSCC
1055 // (by definition). It is the only node we know will stay inside this RefSCC.
1056 // Everything which transitively reaches Target will also remain in the
1057 // RefSCC. We handle this by pre-marking that the nodes in the target SCC map
1058 // back to the root post order number.
1059 //
1060 // This also enables us to take a very significant short-cut in the standard
1061 // Tarjan walk to re-form RefSCCs below: whenever we build an edge that
1062 // references the target node, we know that the target node eventually
1063 // references all other nodes in our walk. As a consequence, we can detect
1064 // and handle participants in that cycle without walking all the edges that
1065 // form the connections, and instead by relying on the fundamental guarantee
1066 // coming into this operation.
1067 SCC &TargetC = *G->lookupSCC(TargetN);
1068 for (Node &N : TargetC)
1069 PostOrderMapping[&N] = RootPostOrderNumber;
1070
1071 // Reset all the other nodes to prepare for a DFS over them, and add them to
1072 // our worklist.
1073 SmallVector<Node *, 8> Worklist;
1074 for (SCC *C : SCCs) {
1075 if (C == &TargetC)
1076 continue;
1077
1078 for (Node &N : *C)
1079 N.DFSNumber = N.LowLink = 0;
1080
1081 Worklist.append(C->Nodes.begin(), C->Nodes.end());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001082 }
1083
Chandler Carruthe5944d92016-02-17 00:18:16 +00001084 auto MarkNodeForSCCNumber = [&PostOrderMapping](Node &N, int Number) {
1085 N.DFSNumber = N.LowLink = -1;
1086 PostOrderMapping[&N] = Number;
1087 };
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001088
Chandler Carruthe5944d92016-02-17 00:18:16 +00001089 SmallVector<std::pair<Node *, edge_iterator>, 4> DFSStack;
1090 SmallVector<Node *, 4> PendingRefSCCStack;
Chandler Carruthaca48d02014-04-26 09:06:53 +00001091 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001092 assert(DFSStack.empty() &&
1093 "Cannot begin a new root with a non-empty DFS stack!");
1094 assert(PendingRefSCCStack.empty() &&
1095 "Cannot begin a new root with pending nodes for an SCC!");
1096
1097 Node *RootN = Worklist.pop_back_val();
1098 // Skip any nodes we've already reached in the DFS.
1099 if (RootN->DFSNumber != 0) {
1100 assert(RootN->DFSNumber == -1 &&
1101 "Shouldn't have any mid-DFS root nodes!");
1102 continue;
1103 }
1104
1105 RootN->DFSNumber = RootN->LowLink = 1;
1106 int NextDFSNumber = 2;
1107
1108 DFSStack.push_back({RootN, RootN->begin()});
1109 do {
1110 Node *N;
1111 edge_iterator I;
1112 std::tie(N, I) = DFSStack.pop_back_val();
1113 auto E = N->end();
1114
1115 assert(N->DFSNumber != 0 && "We should always assign a DFS number "
1116 "before processing a node.");
1117
1118 while (I != E) {
1119 Node &ChildN = I->getNode(*G);
1120 if (ChildN.DFSNumber == 0) {
1121 // Mark that we should start at this child when next this node is the
1122 // top of the stack. We don't start at the next child to ensure this
1123 // child's lowlink is reflected.
1124 DFSStack.push_back({N, I});
1125
1126 // Continue, resetting to the child node.
1127 ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
1128 N = &ChildN;
1129 I = ChildN.begin();
1130 E = ChildN.end();
1131 continue;
1132 }
1133 if (ChildN.DFSNumber == -1) {
1134 // Check if this edge's target node connects to the deleted edge's
1135 // target node. If so, we know that every node connected will end up
1136 // in this RefSCC, so collapse the entire current stack into the root
1137 // slot in our SCC numbering. See above for the motivation of
1138 // optimizing the target connected nodes in this way.
1139 auto PostOrderI = PostOrderMapping.find(&ChildN);
1140 if (PostOrderI != PostOrderMapping.end() &&
1141 PostOrderI->second == RootPostOrderNumber) {
1142 MarkNodeForSCCNumber(*N, RootPostOrderNumber);
1143 while (!PendingRefSCCStack.empty())
1144 MarkNodeForSCCNumber(*PendingRefSCCStack.pop_back_val(),
1145 RootPostOrderNumber);
1146 while (!DFSStack.empty())
1147 MarkNodeForSCCNumber(*DFSStack.pop_back_val().first,
1148 RootPostOrderNumber);
1149 // Ensure we break all the way out of the enclosing loop.
1150 N = nullptr;
1151 break;
1152 }
1153
1154 // If this child isn't currently in this RefSCC, no need to process
1155 // it.
1156 // However, we do need to remove this RefSCC from its RefSCC's parent
1157 // set.
1158 RefSCC &ChildRC = *G->lookupRefSCC(ChildN);
1159 ChildRC.Parents.erase(this);
1160 ++I;
1161 continue;
1162 }
1163
1164 // Track the lowest link of the children, if any are still in the stack.
1165 // Any child not on the stack will have a LowLink of -1.
1166 assert(ChildN.LowLink != 0 &&
1167 "Low-link must not be zero with a non-zero DFS number.");
1168 if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
1169 N->LowLink = ChildN.LowLink;
1170 ++I;
1171 }
1172 if (!N)
1173 // We short-circuited this node.
1174 break;
1175
1176 // We've finished processing N and its descendents, put it on our pending
1177 // stack to eventually get merged into a RefSCC.
1178 PendingRefSCCStack.push_back(N);
1179
1180 // If this node is linked to some lower entry, continue walking up the
1181 // stack.
1182 if (N->LowLink != N->DFSNumber) {
1183 assert(!DFSStack.empty() &&
1184 "We never found a viable root for a RefSCC to pop off!");
1185 continue;
1186 }
1187
1188 // Otherwise, form a new RefSCC from the top of the pending node stack.
1189 int RootDFSNumber = N->DFSNumber;
1190 // Find the range of the node stack by walking down until we pass the
1191 // root DFS number.
1192 auto RefSCCNodes = make_range(
1193 PendingRefSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +00001194 find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1195 return N->DFSNumber < RootDFSNumber;
1196 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +00001197
1198 // Mark the postorder number for these nodes and clear them off the
1199 // stack. We'll use the postorder number to pull them into RefSCCs at the
1200 // end. FIXME: Fuse with the loop above.
1201 int RefSCCNumber = PostOrderNumber++;
1202 for (Node *N : RefSCCNodes)
1203 MarkNodeForSCCNumber(*N, RefSCCNumber);
1204
1205 PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1206 PendingRefSCCStack.end());
1207 } while (!DFSStack.empty());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001208
Chandler Carruthaca48d02014-04-26 09:06:53 +00001209 assert(DFSStack.empty() && "Didn't flush the entire DFS stack!");
Chandler Carruthe5944d92016-02-17 00:18:16 +00001210 assert(PendingRefSCCStack.empty() && "Didn't flush all pending nodes!");
Chandler Carruthaca48d02014-04-26 09:06:53 +00001211 } while (!Worklist.empty());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001212
Chandler Carruthe5944d92016-02-17 00:18:16 +00001213 // We now have a post-order numbering for RefSCCs and a mapping from each
1214 // node in this RefSCC to its final RefSCC. We create each new RefSCC node
1215 // (re-using this RefSCC node for the root) and build a radix-sort style map
1216 // from postorder number to the RefSCC. We then append SCCs to each of these
1217 // RefSCCs in the order they occured in the original SCCs container.
1218 for (int i = 1; i < PostOrderNumber; ++i)
1219 Result.push_back(G->createRefSCC(*G));
1220
1221 for (SCC *C : SCCs) {
1222 auto PostOrderI = PostOrderMapping.find(&*C->begin());
1223 assert(PostOrderI != PostOrderMapping.end() &&
1224 "Cannot have missing mappings for nodes!");
1225 int SCCNumber = PostOrderI->second;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001226#ifndef NDEBUG
Chandler Carruthe5944d92016-02-17 00:18:16 +00001227 for (Node &N : *C)
1228 assert(PostOrderMapping.find(&N)->second == SCCNumber &&
1229 "Cannot have different numbers for nodes in the same SCC!");
1230#endif
1231 if (SCCNumber == 0)
1232 // The root node is handled separately by removing the SCCs.
1233 continue;
1234
1235 RefSCC &RC = *Result[SCCNumber - 1];
1236 int SCCIndex = RC.SCCs.size();
1237 RC.SCCs.push_back(C);
1238 SCCIndices[C] = SCCIndex;
1239 C->OuterRefSCC = &RC;
1240 }
1241
1242 // FIXME: We re-walk the edges in each RefSCC to establish whether it is
1243 // a leaf and connect it to the rest of the graph's parents lists. This is
1244 // really wasteful. We should instead do this during the DFS to avoid yet
1245 // another edge walk.
1246 for (RefSCC *RC : Result)
1247 G->connectRefSCC(*RC);
1248
1249 // Now erase all but the root's SCCs.
David Majnemer42531262016-08-12 03:55:06 +00001250 SCCs.erase(remove_if(SCCs,
1251 [&](SCC *C) {
1252 return PostOrderMapping.lookup(&*C->begin()) !=
1253 RootPostOrderNumber;
1254 }),
Chandler Carruthe5944d92016-02-17 00:18:16 +00001255 SCCs.end());
Chandler Carruth88823462016-08-24 09:37:14 +00001256 SCCIndices.clear();
1257 for (int i = 0, Size = SCCs.size(); i < Size; ++i)
1258 SCCIndices[SCCs[i]] = i;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001259
1260#ifndef NDEBUG
1261 // Now we need to reconnect the current (root) SCC to the graph. We do this
1262 // manually because we can special case our leaf handling and detect errors.
1263 bool IsLeaf = true;
1264#endif
1265 for (SCC *C : SCCs)
1266 for (Node &N : *C) {
1267 for (Edge &E : N) {
1268 assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
1269 RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
1270 if (&ChildRC == this)
1271 continue;
1272 ChildRC.Parents.insert(this);
1273#ifndef NDEBUG
1274 IsLeaf = false;
1275#endif
1276 }
1277 }
1278#ifndef NDEBUG
1279 if (!Result.empty())
1280 assert(!IsLeaf && "This SCC cannot be a leaf as we have split out new "
1281 "SCCs by removing this edge.");
David Majnemer0a16c222016-08-11 21:15:00 +00001282 if (none_of(G->LeafRefSCCs, [&](RefSCC *C) { return C == this; }))
Chandler Carruthe5944d92016-02-17 00:18:16 +00001283 assert(!IsLeaf && "This SCC cannot be a leaf as it already had child "
1284 "SCCs before we removed this edge.");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001285#endif
1286 // If this SCC stopped being a leaf through this edge removal, remove it from
Chandler Carruthe5944d92016-02-17 00:18:16 +00001287 // the leaf SCC list. Note that this DTRT in the case where this was never
1288 // a leaf.
1289 // FIXME: As LeafRefSCCs could be very large, we might want to not walk the
1290 // entire list if this RefSCC wasn't a leaf before the edge removal.
1291 if (!Result.empty())
1292 G->LeafRefSCCs.erase(
1293 std::remove(G->LeafRefSCCs.begin(), G->LeafRefSCCs.end(), this),
1294 G->LeafRefSCCs.end());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001295
1296 // Return the new list of SCCs.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001297 return Result;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001298}
1299
Chandler Carruthe5944d92016-02-17 00:18:16 +00001300void LazyCallGraph::insertEdge(Node &SourceN, Function &Target, Edge::Kind EK) {
Chandler Carruthc00a7ff2014-04-28 11:10:23 +00001301 assert(SCCMap.empty() && DFSStack.empty() &&
1302 "This method cannot be called after SCCs have been formed!");
1303
Chandler Carruthe5944d92016-02-17 00:18:16 +00001304 return SourceN.insertEdgeInternal(Target, EK);
Chandler Carruthc00a7ff2014-04-28 11:10:23 +00001305}
1306
Chandler Carruthe5944d92016-02-17 00:18:16 +00001307void LazyCallGraph::removeEdge(Node &SourceN, Function &Target) {
Chandler Carruthaa839b22014-04-27 01:59:50 +00001308 assert(SCCMap.empty() && DFSStack.empty() &&
1309 "This method cannot be called after SCCs have been formed!");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001310
Chandler Carruthe5944d92016-02-17 00:18:16 +00001311 return SourceN.removeEdgeInternal(Target);
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001312}
1313
Chandler Carruth2a898e02014-04-23 23:20:36 +00001314LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
1315 return *new (MappedN = BPA.Allocate()) Node(*this, F);
Chandler Carruthd8d865e2014-04-18 11:02:33 +00001316}
1317
1318void LazyCallGraph::updateGraphPtrs() {
Chandler Carruthb60cb312014-04-17 07:25:59 +00001319 // Process all nodes updating the graph pointers.
Chandler Carruthaa839b22014-04-27 01:59:50 +00001320 {
1321 SmallVector<Node *, 16> Worklist;
Chandler Carrutha4499e92016-02-02 03:57:13 +00001322 for (Edge &E : EntryEdges)
1323 if (Node *EntryN = E.getNode())
Chandler Carruthaa839b22014-04-27 01:59:50 +00001324 Worklist.push_back(EntryN);
Chandler Carruthb60cb312014-04-17 07:25:59 +00001325
Chandler Carruthaa839b22014-04-27 01:59:50 +00001326 while (!Worklist.empty()) {
1327 Node *N = Worklist.pop_back_val();
1328 N->G = this;
Chandler Carrutha4499e92016-02-02 03:57:13 +00001329 for (Edge &E : N->Edges)
Chandler Carruthe5944d92016-02-17 00:18:16 +00001330 if (Node *TargetN = E.getNode())
1331 Worklist.push_back(TargetN);
Chandler Carruthaa839b22014-04-27 01:59:50 +00001332 }
1333 }
1334
1335 // Process all SCCs updating the graph pointers.
1336 {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001337 SmallVector<RefSCC *, 16> Worklist(LeafRefSCCs.begin(), LeafRefSCCs.end());
Chandler Carruthaa839b22014-04-27 01:59:50 +00001338
1339 while (!Worklist.empty()) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001340 RefSCC &C = *Worklist.pop_back_val();
1341 C.G = this;
1342 for (RefSCC &ParentC : C.parents())
1343 Worklist.push_back(&ParentC);
Chandler Carruthaa839b22014-04-27 01:59:50 +00001344 }
Chandler Carruthb60cb312014-04-17 07:25:59 +00001345 }
Chandler Carruthbf71a342014-02-06 04:37:03 +00001346}
Chandler Carruthbf71a342014-02-06 04:37:03 +00001347
Chandler Carruthe5944d92016-02-17 00:18:16 +00001348/// Build the internal SCCs for a RefSCC from a sequence of nodes.
1349///
1350/// Appends the SCCs to the provided vector and updates the map with their
1351/// indices. Both the vector and map must be empty when passed into this
1352/// routine.
1353void LazyCallGraph::buildSCCs(RefSCC &RC, node_stack_range Nodes) {
1354 assert(RC.SCCs.empty() && "Already built SCCs!");
1355 assert(RC.SCCIndices.empty() && "Already mapped SCC indices!");
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001356
Chandler Carruthe5944d92016-02-17 00:18:16 +00001357 for (Node *N : Nodes) {
1358 assert(N->LowLink >= (*Nodes.begin())->LowLink &&
Chandler Carruthcace6622014-04-23 10:31:17 +00001359 "We cannot have a low link in an SCC lower than its root on the "
1360 "stack!");
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001361
Chandler Carruthe5944d92016-02-17 00:18:16 +00001362 // This node will go into the next RefSCC, clear out its DFS and low link
1363 // as we scan.
1364 N->DFSNumber = N->LowLink = 0;
1365 }
1366
1367 // Each RefSCC contains a DAG of the call SCCs. To build these, we do
1368 // a direct walk of the call edges using Tarjan's algorithm. We reuse the
1369 // internal storage as we won't need it for the outer graph's DFS any longer.
1370
1371 SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
1372 SmallVector<Node *, 16> PendingSCCStack;
1373
1374 // Scan down the stack and DFS across the call edges.
1375 for (Node *RootN : Nodes) {
1376 assert(DFSStack.empty() &&
1377 "Cannot begin a new root with a non-empty DFS stack!");
1378 assert(PendingSCCStack.empty() &&
1379 "Cannot begin a new root with pending nodes for an SCC!");
1380
1381 // Skip any nodes we've already reached in the DFS.
1382 if (RootN->DFSNumber != 0) {
1383 assert(RootN->DFSNumber == -1 &&
1384 "Shouldn't have any mid-DFS root nodes!");
1385 continue;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001386 }
1387
Chandler Carruthe5944d92016-02-17 00:18:16 +00001388 RootN->DFSNumber = RootN->LowLink = 1;
1389 int NextDFSNumber = 2;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001390
Chandler Carruthe5944d92016-02-17 00:18:16 +00001391 DFSStack.push_back({RootN, RootN->call_begin()});
1392 do {
1393 Node *N;
1394 call_edge_iterator I;
1395 std::tie(N, I) = DFSStack.pop_back_val();
1396 auto E = N->call_end();
1397 while (I != E) {
1398 Node &ChildN = *I->getNode();
1399 if (ChildN.DFSNumber == 0) {
1400 // We haven't yet visited this child, so descend, pushing the current
1401 // node onto the stack.
1402 DFSStack.push_back({N, I});
1403
1404 assert(!lookupSCC(ChildN) &&
1405 "Found a node with 0 DFS number but already in an SCC!");
1406 ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
1407 N = &ChildN;
1408 I = N->call_begin();
1409 E = N->call_end();
1410 continue;
1411 }
1412
1413 // If the child has already been added to some child component, it
1414 // couldn't impact the low-link of this parent because it isn't
1415 // connected, and thus its low-link isn't relevant so skip it.
1416 if (ChildN.DFSNumber == -1) {
1417 ++I;
1418 continue;
1419 }
1420
1421 // Track the lowest linked child as the lowest link for this node.
1422 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1423 if (ChildN.LowLink < N->LowLink)
1424 N->LowLink = ChildN.LowLink;
1425
1426 // Move to the next edge.
1427 ++I;
1428 }
1429
1430 // We've finished processing N and its descendents, put it on our pending
1431 // SCC stack to eventually get merged into an SCC of nodes.
1432 PendingSCCStack.push_back(N);
1433
1434 // If this node is linked to some lower entry, continue walking up the
1435 // stack.
1436 if (N->LowLink != N->DFSNumber)
1437 continue;
1438
1439 // Otherwise, we've completed an SCC. Append it to our post order list of
1440 // SCCs.
1441 int RootDFSNumber = N->DFSNumber;
1442 // Find the range of the node stack by walking down until we pass the
1443 // root DFS number.
1444 auto SCCNodes = make_range(
1445 PendingSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +00001446 find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
1447 return N->DFSNumber < RootDFSNumber;
1448 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +00001449 // Form a new SCC out of these nodes and then clear them off our pending
1450 // stack.
1451 RC.SCCs.push_back(createSCC(RC, SCCNodes));
1452 for (Node &N : *RC.SCCs.back()) {
1453 N.DFSNumber = N.LowLink = -1;
1454 SCCMap[&N] = RC.SCCs.back();
1455 }
1456 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
1457 } while (!DFSStack.empty());
1458 }
1459
1460 // Wire up the SCC indices.
1461 for (int i = 0, Size = RC.SCCs.size(); i < Size; ++i)
1462 RC.SCCIndices[RC.SCCs[i]] = i;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001463}
1464
Chandler Carruthe5944d92016-02-17 00:18:16 +00001465// FIXME: We should move callers of this to embed the parent linking and leaf
1466// tracking into their DFS in order to remove a full walk of all edges.
1467void LazyCallGraph::connectRefSCC(RefSCC &RC) {
1468 // Walk all edges in the RefSCC (this remains linear as we only do this once
1469 // when we build the RefSCC) to connect it to the parent sets of its
1470 // children.
1471 bool IsLeaf = true;
1472 for (SCC &C : RC)
1473 for (Node &N : C)
1474 for (Edge &E : N) {
1475 assert(E.getNode() &&
1476 "Cannot have a missing node in a visited part of the graph!");
1477 RefSCC &ChildRC = *lookupRefSCC(*E.getNode());
1478 if (&ChildRC == &RC)
1479 continue;
1480 ChildRC.Parents.insert(&RC);
1481 IsLeaf = false;
1482 }
1483
1484 // For the SCCs where we fine no child SCCs, add them to the leaf list.
1485 if (IsLeaf)
1486 LeafRefSCCs.push_back(&RC);
1487}
1488
1489LazyCallGraph::RefSCC *LazyCallGraph::getNextRefSCCInPostOrder() {
1490 if (DFSStack.empty()) {
1491 Node *N;
Chandler Carruth90821c22014-04-26 09:45:55 +00001492 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001493 // If we've handled all candidate entry nodes to the SCC forest, we're
1494 // done.
1495 if (RefSCCEntryNodes.empty())
Chandler Carruth90821c22014-04-26 09:45:55 +00001496 return nullptr;
Chandler Carruth18eadd922014-04-18 10:50:32 +00001497
Chandler Carruthe5944d92016-02-17 00:18:16 +00001498 N = &get(*RefSCCEntryNodes.pop_back_val());
Chandler Carruth90821c22014-04-26 09:45:55 +00001499 } while (N->DFSNumber != 0);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001500
1501 // Found a new root, begin the DFS here.
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001502 N->LowLink = N->DFSNumber = 1;
Chandler Carruth09751bf2014-04-24 09:59:59 +00001503 NextDFSNumber = 2;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001504 DFSStack.push_back({N, N->begin()});
Chandler Carruth18eadd922014-04-18 10:50:32 +00001505 }
1506
Chandler Carruth91dcf0f2014-04-24 21:19:30 +00001507 for (;;) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001508 Node *N;
1509 edge_iterator I;
1510 std::tie(N, I) = DFSStack.pop_back_val();
1511
1512 assert(N->DFSNumber > 0 && "We should always assign a DFS number "
1513 "before placing a node onto the stack.");
Chandler Carruth24553932014-04-24 11:05:20 +00001514
Chandler Carrutha4499e92016-02-02 03:57:13 +00001515 auto E = N->end();
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001516 while (I != E) {
Chandler Carrutha4499e92016-02-02 03:57:13 +00001517 Node &ChildN = I->getNode(*this);
Chandler Carruthbd5d3082014-04-23 23:34:48 +00001518 if (ChildN.DFSNumber == 0) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001519 // We haven't yet visited this child, so descend, pushing the current
1520 // node onto the stack.
1521 DFSStack.push_back({N, N->begin()});
Chandler Carruth18eadd922014-04-18 10:50:32 +00001522
Chandler Carruth09751bf2014-04-24 09:59:59 +00001523 assert(!SCCMap.count(&ChildN) &&
1524 "Found a node with 0 DFS number but already in an SCC!");
1525 ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001526 N = &ChildN;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001527 I = N->begin();
1528 E = N->end();
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001529 continue;
Chandler Carruthcace6622014-04-23 10:31:17 +00001530 }
1531
Chandler Carruthe5944d92016-02-17 00:18:16 +00001532 // If the child has already been added to some child component, it
1533 // couldn't impact the low-link of this parent because it isn't
1534 // connected, and thus its low-link isn't relevant so skip it.
1535 if (ChildN.DFSNumber == -1) {
1536 ++I;
1537 continue;
1538 }
1539
1540 // Track the lowest linked child as the lowest link for this node.
1541 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1542 if (ChildN.LowLink < N->LowLink)
Chandler Carruthbd5d3082014-04-23 23:34:48 +00001543 N->LowLink = ChildN.LowLink;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001544
1545 // Move to the next edge.
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001546 ++I;
Chandler Carruth18eadd922014-04-18 10:50:32 +00001547 }
1548
Chandler Carruthe5944d92016-02-17 00:18:16 +00001549 // We've finished processing N and its descendents, put it on our pending
1550 // SCC stack to eventually get merged into an SCC of nodes.
1551 PendingRefSCCStack.push_back(N);
Chandler Carruth18eadd922014-04-18 10:50:32 +00001552
Chandler Carruthe5944d92016-02-17 00:18:16 +00001553 // If this node is linked to some lower entry, continue walking up the
1554 // stack.
1555 if (N->LowLink != N->DFSNumber) {
1556 assert(!DFSStack.empty() &&
1557 "We never found a viable root for an SCC to pop off!");
1558 continue;
1559 }
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001560
Chandler Carruthe5944d92016-02-17 00:18:16 +00001561 // Otherwise, form a new RefSCC from the top of the pending node stack.
1562 int RootDFSNumber = N->DFSNumber;
1563 // Find the range of the node stack by walking down until we pass the
1564 // root DFS number.
1565 auto RefSCCNodes = node_stack_range(
1566 PendingRefSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +00001567 find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1568 return N->DFSNumber < RootDFSNumber;
1569 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +00001570 // Form a new RefSCC out of these nodes and then clear them off our pending
1571 // stack.
1572 RefSCC *NewRC = createRefSCC(*this);
1573 buildSCCs(*NewRC, RefSCCNodes);
1574 connectRefSCC(*NewRC);
1575 PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1576 PendingRefSCCStack.end());
1577
1578 // We return the new node here. This essentially suspends the DFS walk
1579 // until another RefSCC is requested.
1580 return NewRC;
Chandler Carruth91dcf0f2014-04-24 21:19:30 +00001581 }
Chandler Carruth18eadd922014-04-18 10:50:32 +00001582}
1583
Chandler Carruthb4faf132016-03-11 10:22:49 +00001584char LazyCallGraphAnalysis::PassID;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001585
Chandler Carruthbf71a342014-02-06 04:37:03 +00001586LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
1587
Chandler Carruthe5944d92016-02-17 00:18:16 +00001588static void printNode(raw_ostream &OS, LazyCallGraph::Node &N) {
Chandler Carrutha4499e92016-02-02 03:57:13 +00001589 OS << " Edges in function: " << N.getFunction().getName() << "\n";
1590 for (const LazyCallGraph::Edge &E : N)
1591 OS << " " << (E.isCall() ? "call" : "ref ") << " -> "
1592 << E.getFunction().getName() << "\n";
Chandler Carruth11f50322015-01-14 00:27:45 +00001593
1594 OS << "\n";
1595}
1596
Chandler Carruthe5944d92016-02-17 00:18:16 +00001597static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) {
1598 ptrdiff_t Size = std::distance(C.begin(), C.end());
1599 OS << " SCC with " << Size << " functions:\n";
Chandler Carruth11f50322015-01-14 00:27:45 +00001600
Chandler Carruthe5944d92016-02-17 00:18:16 +00001601 for (LazyCallGraph::Node &N : C)
1602 OS << " " << N.getFunction().getName() << "\n";
1603}
1604
1605static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) {
1606 ptrdiff_t Size = std::distance(C.begin(), C.end());
1607 OS << " RefSCC with " << Size << " call SCCs:\n";
1608
1609 for (LazyCallGraph::SCC &InnerC : C)
1610 printSCC(OS, InnerC);
Chandler Carruth11f50322015-01-14 00:27:45 +00001611
1612 OS << "\n";
1613}
1614
Chandler Carruthd174ce42015-01-05 02:47:05 +00001615PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M,
Chandler Carruthb47f8012016-03-11 11:05:24 +00001616 ModuleAnalysisManager &AM) {
1617 LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
Chandler Carruth11f50322015-01-14 00:27:45 +00001618
1619 OS << "Printing the call graph for module: " << M.getModuleIdentifier()
1620 << "\n\n";
1621
Chandler Carruthe5944d92016-02-17 00:18:16 +00001622 for (Function &F : M)
1623 printNode(OS, G.get(F));
Chandler Carruth11f50322015-01-14 00:27:45 +00001624
Chandler Carruthe5944d92016-02-17 00:18:16 +00001625 for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs())
1626 printRefSCC(OS, C);
Chandler Carruth18eadd922014-04-18 10:50:32 +00001627
Chandler Carruthbf71a342014-02-06 04:37:03 +00001628 return PreservedAnalyses::all();
Chandler Carruthbf71a342014-02-06 04:37:03 +00001629}
Sean Silva7cb30662016-06-18 09:17:32 +00001630
1631LazyCallGraphDOTPrinterPass::LazyCallGraphDOTPrinterPass(raw_ostream &OS)
1632 : OS(OS) {}
1633
1634static void printNodeDOT(raw_ostream &OS, LazyCallGraph::Node &N) {
1635 std::string Name = "\"" + DOT::EscapeString(N.getFunction().getName()) + "\"";
1636
1637 for (const LazyCallGraph::Edge &E : N) {
1638 OS << " " << Name << " -> \""
1639 << DOT::EscapeString(E.getFunction().getName()) << "\"";
1640 if (!E.isCall()) // It is a ref edge.
1641 OS << " [style=dashed,label=\"ref\"]";
1642 OS << ";\n";
1643 }
1644
1645 OS << "\n";
1646}
1647
1648PreservedAnalyses LazyCallGraphDOTPrinterPass::run(Module &M,
1649 ModuleAnalysisManager &AM) {
1650 LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
1651
1652 OS << "digraph \"" << DOT::EscapeString(M.getModuleIdentifier()) << "\" {\n";
1653
1654 for (Function &F : M)
1655 printNodeDOT(OS, G.get(F));
1656
1657 OS << "}\n";
1658
1659 return PreservedAnalyses::all();
1660}