blob: 2e78fca3989199cecb19a890ac59c5ae9d9a65da [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 Carruth219b89b2014-03-04 11:01:28 +000012#include "llvm/IR/CallSite.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000013#include "llvm/IR/InstVisitor.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000014#include "llvm/IR/Instructions.h"
15#include "llvm/IR/PassManager.h"
Chandler Carruth99b756d2014-04-21 05:04:24 +000016#include "llvm/Support/Debug.h"
Sean Silva7cb30662016-06-18 09:17:32 +000017#include "llvm/Support/GraphWriter.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000018
19using namespace llvm;
20
Chandler Carruthf1221bd2014-04-22 02:48:03 +000021#define DEBUG_TYPE "lcg"
22
Chandler Carrutha4499e92016-02-02 03:57:13 +000023static void addEdge(SmallVectorImpl<LazyCallGraph::Edge> &Edges,
Chandler Carruthe5944d92016-02-17 00:18:16 +000024 DenseMap<Function *, int> &EdgeIndexMap, Function &F,
Chandler Carrutha4499e92016-02-02 03:57:13 +000025 LazyCallGraph::Edge::Kind EK) {
26 // Note that we consider *any* function with a definition to be a viable
27 // edge. Even if the function's definition is subject to replacement by
28 // some other module (say, a weak definition) there may still be
29 // optimizations which essentially speculate based on the definition and
30 // a way to check that the specific definition is in fact the one being
31 // used. For example, this could be done by moving the weak definition to
32 // a strong (internal) definition and making the weak definition be an
33 // alias. Then a test of the address of the weak function against the new
34 // strong definition's address would be an effective way to determine the
35 // safety of optimizing a direct call edge.
36 if (!F.isDeclaration() &&
Chandler Carruthe5944d92016-02-17 00:18:16 +000037 EdgeIndexMap.insert({&F, Edges.size()}).second) {
Chandler Carrutha4499e92016-02-02 03:57:13 +000038 DEBUG(dbgs() << " Added callable function: " << F.getName() << "\n");
39 Edges.emplace_back(LazyCallGraph::Edge(F, EK));
40 }
41}
42
Chandler Carruth18eadd922014-04-18 10:50:32 +000043LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F)
44 : G(&G), F(F), DFSNumber(0), LowLink(0) {
Chandler Carruth99b756d2014-04-21 05:04:24 +000045 DEBUG(dbgs() << " Adding functions called by '" << F.getName()
46 << "' to the graph.\n");
47
Chandler Carruthbf71a342014-02-06 04:37:03 +000048 SmallVector<Constant *, 16> Worklist;
Chandler Carrutha4499e92016-02-02 03:57:13 +000049 SmallPtrSet<Function *, 4> Callees;
Chandler Carruthbf71a342014-02-06 04:37:03 +000050 SmallPtrSet<Constant *, 16> Visited;
Chandler Carrutha4499e92016-02-02 03:57:13 +000051
52 // Find all the potential call graph edges in this function. We track both
53 // actual call edges and indirect references to functions. The direct calls
54 // are trivially added, but to accumulate the latter we walk the instructions
55 // and add every operand which is a constant to the worklist to process
56 // afterward.
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +000057 for (BasicBlock &BB : F)
Chandler Carrutha4499e92016-02-02 03:57:13 +000058 for (Instruction &I : BB) {
59 if (auto CS = CallSite(&I))
60 if (Function *Callee = CS.getCalledFunction())
61 if (Callees.insert(Callee).second) {
62 Visited.insert(Callee);
63 addEdge(Edges, EdgeIndexMap, *Callee, LazyCallGraph::Edge::Call);
64 }
65
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +000066 for (Value *Op : I.operand_values())
Chandler Carruth1583e992014-03-03 10:42:58 +000067 if (Constant *C = dyn_cast<Constant>(Op))
David Blaikie70573dc2014-11-19 07:49:26 +000068 if (Visited.insert(C).second)
Chandler Carruthbf71a342014-02-06 04:37:03 +000069 Worklist.push_back(C);
Chandler Carrutha4499e92016-02-02 03:57:13 +000070 }
Chandler Carruthbf71a342014-02-06 04:37:03 +000071
72 // We've collected all the constant (and thus potentially function or
73 // function containing) operands to all of the instructions in the function.
74 // Process them (recursively) collecting every function found.
Chandler Carruth88823462016-08-24 09:37:14 +000075 visitReferences(Worklist, Visited, [&](Function &F) {
76 addEdge(Edges, EdgeIndexMap, F, LazyCallGraph::Edge::Ref);
77 });
Chandler Carruthbf71a342014-02-06 04:37:03 +000078}
79
Chandler Carruthe5944d92016-02-17 00:18:16 +000080void LazyCallGraph::Node::insertEdgeInternal(Function &Target, Edge::Kind EK) {
81 if (Node *N = G->lookup(Target))
Chandler Carrutha4499e92016-02-02 03:57:13 +000082 return insertEdgeInternal(*N, EK);
Chandler Carruth5217c942014-04-30 10:48:36 +000083
Chandler Carruthe5944d92016-02-17 00:18:16 +000084 EdgeIndexMap.insert({&Target, Edges.size()});
85 Edges.emplace_back(Target, EK);
Chandler Carruth5217c942014-04-30 10:48:36 +000086}
87
Chandler Carruthe5944d92016-02-17 00:18:16 +000088void LazyCallGraph::Node::insertEdgeInternal(Node &TargetN, Edge::Kind EK) {
89 EdgeIndexMap.insert({&TargetN.getFunction(), Edges.size()});
90 Edges.emplace_back(TargetN, EK);
Chandler Carruthc00a7ff2014-04-28 11:10:23 +000091}
92
Chandler Carruthe5944d92016-02-17 00:18:16 +000093void LazyCallGraph::Node::setEdgeKind(Function &TargetF, Edge::Kind EK) {
94 Edges[EdgeIndexMap.find(&TargetF)->second].setKind(EK);
95}
96
97void LazyCallGraph::Node::removeEdgeInternal(Function &Target) {
98 auto IndexMapI = EdgeIndexMap.find(&Target);
Chandler Carrutha4499e92016-02-02 03:57:13 +000099 assert(IndexMapI != EdgeIndexMap.end() &&
Chandler Carruthe5944d92016-02-17 00:18:16 +0000100 "Target not in the edge set for this caller?");
Chandler Carruthaa839b22014-04-27 01:59:50 +0000101
Chandler Carrutha4499e92016-02-02 03:57:13 +0000102 Edges[IndexMapI->second] = Edge();
103 EdgeIndexMap.erase(IndexMapI);
Chandler Carruthaa839b22014-04-27 01:59:50 +0000104}
105
Chandler Carruthdca83402016-06-27 23:26:08 +0000106void LazyCallGraph::Node::dump() const {
107 dbgs() << *this << '\n';
108}
109
Chandler Carruth2174f442014-04-18 20:44:16 +0000110LazyCallGraph::LazyCallGraph(Module &M) : NextDFSNumber(0) {
Chandler Carruth99b756d2014-04-21 05:04:24 +0000111 DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier()
112 << "\n");
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000113 for (Function &F : M)
114 if (!F.isDeclaration() && !F.hasLocalLinkage())
Chandler Carruthe5944d92016-02-17 00:18:16 +0000115 if (EntryIndexMap.insert({&F, EntryEdges.size()}).second) {
Chandler Carruth99b756d2014-04-21 05:04:24 +0000116 DEBUG(dbgs() << " Adding '" << F.getName()
117 << "' to entry set of the graph.\n");
Chandler Carrutha4499e92016-02-02 03:57:13 +0000118 EntryEdges.emplace_back(F, Edge::Ref);
Chandler Carruth99b756d2014-04-21 05:04:24 +0000119 }
Chandler Carruthbf71a342014-02-06 04:37:03 +0000120
121 // Now add entry nodes for functions reachable via initializers to globals.
122 SmallVector<Constant *, 16> Worklist;
123 SmallPtrSet<Constant *, 16> Visited;
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000124 for (GlobalVariable &GV : M.globals())
125 if (GV.hasInitializer())
David Blaikie70573dc2014-11-19 07:49:26 +0000126 if (Visited.insert(GV.getInitializer()).second)
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000127 Worklist.push_back(GV.getInitializer());
Chandler Carruthbf71a342014-02-06 04:37:03 +0000128
Chandler Carruth99b756d2014-04-21 05:04:24 +0000129 DEBUG(dbgs() << " Adding functions referenced by global initializers to the "
130 "entry set.\n");
Chandler Carruth88823462016-08-24 09:37:14 +0000131 visitReferences(Worklist, Visited, [&](Function &F) {
132 addEdge(EntryEdges, EntryIndexMap, F, LazyCallGraph::Edge::Ref);
133 });
Chandler Carruth18eadd922014-04-18 10:50:32 +0000134
Chandler Carrutha4499e92016-02-02 03:57:13 +0000135 for (const Edge &E : EntryEdges)
Chandler Carruthe5944d92016-02-17 00:18:16 +0000136 RefSCCEntryNodes.push_back(&E.getFunction());
Chandler Carruthbf71a342014-02-06 04:37:03 +0000137}
138
Chandler Carruthbf71a342014-02-06 04:37:03 +0000139LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
Chandler Carruth2174f442014-04-18 20:44:16 +0000140 : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)),
Chandler Carrutha4499e92016-02-02 03:57:13 +0000141 EntryEdges(std::move(G.EntryEdges)),
Chandler Carruth0b623ba2014-04-23 04:00:17 +0000142 EntryIndexMap(std::move(G.EntryIndexMap)), SCCBPA(std::move(G.SCCBPA)),
Chandler Carruthe5944d92016-02-17 00:18:16 +0000143 SCCMap(std::move(G.SCCMap)), LeafRefSCCs(std::move(G.LeafRefSCCs)),
Chandler Carruth18eadd922014-04-18 10:50:32 +0000144 DFSStack(std::move(G.DFSStack)),
Chandler Carruthe5944d92016-02-17 00:18:16 +0000145 RefSCCEntryNodes(std::move(G.RefSCCEntryNodes)),
Chandler Carruth2174f442014-04-18 20:44:16 +0000146 NextDFSNumber(G.NextDFSNumber) {
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000147 updateGraphPtrs();
148}
149
150LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) {
151 BPA = std::move(G.BPA);
Chandler Carruth2174f442014-04-18 20:44:16 +0000152 NodeMap = std::move(G.NodeMap);
Chandler Carrutha4499e92016-02-02 03:57:13 +0000153 EntryEdges = std::move(G.EntryEdges);
Chandler Carruth0b623ba2014-04-23 04:00:17 +0000154 EntryIndexMap = std::move(G.EntryIndexMap);
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000155 SCCBPA = std::move(G.SCCBPA);
156 SCCMap = std::move(G.SCCMap);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000157 LeafRefSCCs = std::move(G.LeafRefSCCs);
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000158 DFSStack = std::move(G.DFSStack);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000159 RefSCCEntryNodes = std::move(G.RefSCCEntryNodes);
Chandler Carruth2174f442014-04-18 20:44:16 +0000160 NextDFSNumber = G.NextDFSNumber;
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000161 updateGraphPtrs();
162 return *this;
163}
164
Chandler Carruthdca83402016-06-27 23:26:08 +0000165void LazyCallGraph::SCC::dump() const {
166 dbgs() << *this << '\n';
167}
168
Chandler Carruthe5944d92016-02-17 00:18:16 +0000169#ifndef NDEBUG
170void LazyCallGraph::SCC::verify() {
171 assert(OuterRefSCC && "Can't have a null RefSCC!");
172 assert(!Nodes.empty() && "Can't have an empty SCC!");
Chandler Carruth8f92d6d2014-04-26 01:03:46 +0000173
Chandler Carruthe5944d92016-02-17 00:18:16 +0000174 for (Node *N : Nodes) {
175 assert(N && "Can't have a null node!");
176 assert(OuterRefSCC->G->lookupSCC(*N) == this &&
177 "Node does not map to this SCC!");
178 assert(N->DFSNumber == -1 &&
179 "Must set DFS numbers to -1 when adding a node to an SCC!");
180 assert(N->LowLink == -1 &&
181 "Must set low link to -1 when adding a node to an SCC!");
182 for (Edge &E : *N)
183 assert(E.getNode() && "Can't have an edge to a raw function!");
184 }
185}
186#endif
187
188LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {}
189
Chandler Carruthdca83402016-06-27 23:26:08 +0000190void LazyCallGraph::RefSCC::dump() const {
191 dbgs() << *this << '\n';
192}
193
Chandler Carruthe5944d92016-02-17 00:18:16 +0000194#ifndef NDEBUG
195void LazyCallGraph::RefSCC::verify() {
196 assert(G && "Can't have a null graph!");
197 assert(!SCCs.empty() && "Can't have an empty SCC!");
198
199 // Verify basic properties of the SCCs.
Chandler Carruth88823462016-08-24 09:37:14 +0000200 SmallPtrSet<SCC *, 4> SCCSet;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000201 for (SCC *C : SCCs) {
202 assert(C && "Can't have a null SCC!");
203 C->verify();
204 assert(&C->getOuterRefSCC() == this &&
205 "SCC doesn't think it is inside this RefSCC!");
Chandler Carruth88823462016-08-24 09:37:14 +0000206 bool Inserted = SCCSet.insert(C).second;
207 assert(Inserted && "Found a duplicate SCC!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000208 }
209
210 // Check that our indices map correctly.
211 for (auto &SCCIndexPair : SCCIndices) {
212 SCC *C = SCCIndexPair.first;
213 int i = SCCIndexPair.second;
214 assert(C && "Can't have a null SCC in the indices!");
Chandler Carruth88823462016-08-24 09:37:14 +0000215 assert(SCCSet.count(C) && "Found an index for an SCC not in the RefSCC!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000216 assert(SCCs[i] == C && "Index doesn't point to SCC!");
217 }
218
219 // Check that the SCCs are in fact in post-order.
220 for (int i = 0, Size = SCCs.size(); i < Size; ++i) {
221 SCC &SourceSCC = *SCCs[i];
222 for (Node &N : SourceSCC)
223 for (Edge &E : N) {
224 if (!E.isCall())
225 continue;
226 SCC &TargetSCC = *G->lookupSCC(*E.getNode());
227 if (&TargetSCC.getOuterRefSCC() == this) {
228 assert(SCCIndices.find(&TargetSCC)->second <= i &&
229 "Edge between SCCs violates post-order relationship.");
230 continue;
231 }
232 assert(TargetSCC.getOuterRefSCC().Parents.count(this) &&
233 "Edge to a RefSCC missing us in its parent set.");
234 }
235 }
236}
237#endif
238
239bool LazyCallGraph::RefSCC::isDescendantOf(const RefSCC &C) const {
Chandler Carruth4b096742014-05-01 12:12:42 +0000240 // Walk up the parents of this SCC and verify that we eventually find C.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000241 SmallVector<const RefSCC *, 4> AncestorWorklist;
Chandler Carruth4b096742014-05-01 12:12:42 +0000242 AncestorWorklist.push_back(this);
243 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000244 const RefSCC *AncestorC = AncestorWorklist.pop_back_val();
Chandler Carruth4b096742014-05-01 12:12:42 +0000245 if (AncestorC->isChildOf(C))
246 return true;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000247 for (const RefSCC *ParentC : AncestorC->Parents)
Chandler Carruth4b096742014-05-01 12:12:42 +0000248 AncestorWorklist.push_back(ParentC);
249 } while (!AncestorWorklist.empty());
250
251 return false;
252}
253
Chandler Carruth1f621f02016-09-04 08:34:24 +0000254/// Generic helper that updates a postorder sequence of SCCs for a potentially
255/// cycle-introducing edge insertion.
256///
257/// A postorder sequence of SCCs of a directed graph has one fundamental
258/// property: all deges in the DAG of SCCs point "up" the sequence. That is,
259/// all edges in the SCC DAG point to prior SCCs in the sequence.
260///
261/// This routine both updates a postorder sequence and uses that sequence to
262/// compute the set of SCCs connected into a cycle. It should only be called to
263/// insert a "downward" edge which will require changing the sequence to
264/// restore it to a postorder.
265///
266/// When inserting an edge from an earlier SCC to a later SCC in some postorder
267/// sequence, all of the SCCs which may be impacted are in the closed range of
268/// those two within the postorder sequence. The algorithm used here to restore
269/// the state is as follows:
270///
271/// 1) Starting from the source SCC, construct a set of SCCs which reach the
272/// source SCC consisting of just the source SCC. Then scan toward the
273/// target SCC in postorder and for each SCC, if it has an edge to an SCC
274/// in the set, add it to the set. Otherwise, the source SCC is not
275/// a successor, move it in the postorder sequence to immediately before
276/// the source SCC, shifting the source SCC and all SCCs in the set one
277/// position toward the target SCC. Stop scanning after processing the
278/// target SCC.
279/// 2) If the source SCC is now past the target SCC in the postorder sequence,
280/// and thus the new edge will flow toward the start, we are done.
281/// 3) Otherwise, starting from the target SCC, walk all edges which reach an
282/// SCC between the source and the target, and add them to the set of
283/// connected SCCs, then recurse through them. Once a complete set of the
284/// SCCs the target connects to is known, hoist the remaining SCCs between
285/// the source and the target to be above the target. Note that there is no
286/// need to process the source SCC, it is already known to connect.
287/// 4) At this point, all of the SCCs in the closed range between the source
288/// SCC and the target SCC in the postorder sequence are connected,
289/// including the target SCC and the source SCC. Inserting the edge from
290/// the source SCC to the target SCC will form a cycle out of precisely
291/// these SCCs. Thus we can merge all of the SCCs in this closed range into
292/// a single SCC.
293///
294/// This process has various important properties:
295/// - Only mutates the SCCs when adding the edge actually changes the SCC
296/// structure.
297/// - Never mutates SCCs which are unaffected by the change.
298/// - Updates the postorder sequence to correctly satisfy the postorder
299/// constraint after the edge is inserted.
300/// - Only reorders SCCs in the closed postorder sequence from the source to
301/// the target, so easy to bound how much has changed even in the ordering.
302/// - Big-O is the number of edges in the closed postorder range of SCCs from
303/// source to target.
304///
305/// This helper routine, in addition to updating the postorder sequence itself
306/// will also update a map from SCCs to indices within that sequecne.
307///
308/// The sequence and the map must operate on pointers to the SCC type.
309///
310/// Two callbacks must be provided. The first computes the subset of SCCs in
311/// the postorder closed range from the source to the target which connect to
312/// the source SCC via some (transitive) set of edges. The second computes the
313/// subset of the same range which the target SCC connects to via some
314/// (transitive) set of edges. Both callbacks should populate the set argument
315/// provided.
316template <typename SCCT, typename PostorderSequenceT, typename SCCIndexMapT,
317 typename ComputeSourceConnectedSetCallableT,
318 typename ComputeTargetConnectedSetCallableT>
319static iterator_range<typename PostorderSequenceT::iterator>
320updatePostorderSequenceForEdgeInsertion(
321 SCCT &SourceSCC, SCCT &TargetSCC, PostorderSequenceT &SCCs,
322 SCCIndexMapT &SCCIndices,
323 ComputeSourceConnectedSetCallableT ComputeSourceConnectedSet,
324 ComputeTargetConnectedSetCallableT ComputeTargetConnectedSet) {
325 int SourceIdx = SCCIndices[&SourceSCC];
326 int TargetIdx = SCCIndices[&TargetSCC];
327 assert(SourceIdx < TargetIdx && "Cannot have equal indices here!");
328
329 SmallPtrSet<SCCT *, 4> ConnectedSet;
330
331 // Compute the SCCs which (transitively) reach the source.
332 ComputeSourceConnectedSet(ConnectedSet);
333
334 // Partition the SCCs in this part of the port-order sequence so only SCCs
335 // connecting to the source remain between it and the target. This is
336 // a benign partition as it preserves postorder.
337 auto SourceI = std::stable_partition(
338 SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx + 1,
339 [&ConnectedSet](SCCT *C) { return !ConnectedSet.count(C); });
340 for (int i = SourceIdx, e = TargetIdx + 1; i < e; ++i)
341 SCCIndices.find(SCCs[i])->second = i;
342
343 // If the target doesn't connect to the source, then we've corrected the
344 // post-order and there are no cycles formed.
345 if (!ConnectedSet.count(&TargetSCC)) {
346 assert(SourceI > (SCCs.begin() + SourceIdx) &&
347 "Must have moved the source to fix the post-order.");
348 assert(*std::prev(SourceI) == &TargetSCC &&
349 "Last SCC to move should have bene the target.");
350
351 // Return an empty range at the target SCC indicating there is nothing to
352 // merge.
353 return make_range(std::prev(SourceI), std::prev(SourceI));
354 }
355
356 assert(SCCs[TargetIdx] == &TargetSCC &&
357 "Should not have moved target if connected!");
358 SourceIdx = SourceI - SCCs.begin();
359 assert(SCCs[SourceIdx] == &SourceSCC &&
360 "Bad updated index computation for the source SCC!");
361
362
363 // See whether there are any remaining intervening SCCs between the source
364 // and target. If so we need to make sure they all are reachable form the
365 // target.
366 if (SourceIdx + 1 < TargetIdx) {
367 ConnectedSet.clear();
368 ComputeTargetConnectedSet(ConnectedSet);
369
370 // Partition SCCs so that only SCCs reached from the target remain between
371 // the source and the target. This preserves postorder.
372 auto TargetI = std::stable_partition(
373 SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1,
374 [&ConnectedSet](SCCT *C) { return ConnectedSet.count(C); });
375 for (int i = SourceIdx + 1, e = TargetIdx + 1; i < e; ++i)
376 SCCIndices.find(SCCs[i])->second = i;
377 TargetIdx = std::prev(TargetI) - SCCs.begin();
378 assert(SCCs[TargetIdx] == &TargetSCC &&
379 "Should always end with the target!");
380 }
381
382 // At this point, we know that connecting source to target forms a cycle
383 // because target connects back to source, and we know that all of the SCCs
384 // between the source and target in the postorder sequence participate in that
385 // cycle.
386 return make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx);
387}
388
Chandler Carruthe5944d92016-02-17 00:18:16 +0000389SmallVector<LazyCallGraph::SCC *, 1>
390LazyCallGraph::RefSCC::switchInternalEdgeToCall(Node &SourceN, Node &TargetN) {
391 assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
Chandler Carruth5217c942014-04-30 10:48:36 +0000392
Chandler Carruthe5944d92016-02-17 00:18:16 +0000393 SmallVector<SCC *, 1> DeletedSCCs;
Chandler Carruth5217c942014-04-30 10:48:36 +0000394
Chandler Carruthe5944d92016-02-17 00:18:16 +0000395 SCC &SourceSCC = *G->lookupSCC(SourceN);
396 SCC &TargetSCC = *G->lookupSCC(TargetN);
397
398 // If the two nodes are already part of the same SCC, we're also done as
399 // we've just added more connectivity.
400 if (&SourceSCC == &TargetSCC) {
401 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
402#ifndef NDEBUG
403 // Check that the RefSCC is still valid.
404 verify();
405#endif
406 return DeletedSCCs;
407 }
408
409 // At this point we leverage the postorder list of SCCs to detect when the
410 // insertion of an edge changes the SCC structure in any way.
411 //
412 // First and foremost, we can eliminate the need for any changes when the
413 // edge is toward the beginning of the postorder sequence because all edges
414 // flow in that direction already. Thus adding a new one cannot form a cycle.
415 int SourceIdx = SCCIndices[&SourceSCC];
416 int TargetIdx = SCCIndices[&TargetSCC];
417 if (TargetIdx < SourceIdx) {
418 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
419#ifndef NDEBUG
420 // Check that the RefSCC is still valid.
421 verify();
422#endif
423 return DeletedSCCs;
424 }
425
Chandler Carruthe5944d92016-02-17 00:18:16 +0000426 // Compute the SCCs which (transitively) reach the source.
Chandler Carruth1f621f02016-09-04 08:34:24 +0000427 auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000428#ifndef NDEBUG
Chandler Carruth1f621f02016-09-04 08:34:24 +0000429 // Check that the RefSCC is still valid before computing this as the
430 // results will be nonsensical of we've broken its invariants.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000431 verify();
432#endif
Chandler Carruth1f621f02016-09-04 08:34:24 +0000433 ConnectedSet.insert(&SourceSCC);
434 auto IsConnected = [&](SCC &C) {
435 for (Node &N : C)
436 for (Edge &E : N.calls()) {
437 assert(E.getNode() && "Must have formed a node within an SCC!");
438 if (ConnectedSet.count(G->lookupSCC(*E.getNode())))
439 return true;
440 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000441
Chandler Carruth1f621f02016-09-04 08:34:24 +0000442 return false;
443 };
Chandler Carruthe5944d92016-02-17 00:18:16 +0000444
Chandler Carruth1f621f02016-09-04 08:34:24 +0000445 for (SCC *C :
446 make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1))
447 if (IsConnected(*C))
448 ConnectedSet.insert(C);
449 };
450
451 // Use a normal worklist to find which SCCs the target connects to. We still
452 // bound the search based on the range in the postorder list we care about,
453 // but because this is forward connectivity we just "recurse" through the
454 // edges.
455 auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000456#ifndef NDEBUG
Chandler Carruth1f621f02016-09-04 08:34:24 +0000457 // Check that the RefSCC is still valid before computing this as the
458 // results will be nonsensical of we've broken its invariants.
459 verify();
Chandler Carruthe5944d92016-02-17 00:18:16 +0000460#endif
Chandler Carruthe5944d92016-02-17 00:18:16 +0000461 ConnectedSet.insert(&TargetSCC);
462 SmallVector<SCC *, 4> Worklist;
463 Worklist.push_back(&TargetSCC);
464 do {
465 SCC &C = *Worklist.pop_back_val();
466 for (Node &N : C)
467 for (Edge &E : N) {
468 assert(E.getNode() && "Must have formed a node within an SCC!");
469 if (!E.isCall())
470 continue;
471 SCC &EdgeC = *G->lookupSCC(*E.getNode());
472 if (&EdgeC.getOuterRefSCC() != this)
473 // Not in this RefSCC...
474 continue;
475 if (SCCIndices.find(&EdgeC)->second <= SourceIdx)
476 // Not in the postorder sequence between source and target.
477 continue;
478
479 if (ConnectedSet.insert(&EdgeC).second)
480 Worklist.push_back(&EdgeC);
481 }
482 } while (!Worklist.empty());
Chandler Carruth1f621f02016-09-04 08:34:24 +0000483 };
Chandler Carruthe5944d92016-02-17 00:18:16 +0000484
Chandler Carruth1f621f02016-09-04 08:34:24 +0000485 // Use a generic helper to update the postorder sequence of SCCs and return
486 // a range of any SCCs connected into a cycle by inserting this edge. This
487 // routine will also take care of updating the indices into the postorder
488 // sequence.
489 auto MergeRange = updatePostorderSequenceForEdgeInsertion(
490 SourceSCC, TargetSCC, SCCs, SCCIndices, ComputeSourceConnectedSet,
491 ComputeTargetConnectedSet);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000492
Chandler Carruth1f621f02016-09-04 08:34:24 +0000493 // If the merge range is empty, then adding the edge didn't actually form any
494 // new cycles. We're done.
495 if (MergeRange.begin() == MergeRange.end()) {
496 // Now that the SCC structure is finalized, flip the kind to call.
497 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000498#ifndef NDEBUG
Chandler Carruthe5944d92016-02-17 00:18:16 +0000499 verify();
500#endif
Chandler Carruth1f621f02016-09-04 08:34:24 +0000501 return DeletedSCCs;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000502 }
503
Chandler Carruth1f621f02016-09-04 08:34:24 +0000504#ifndef NDEBUG
505 // Before merging, check that the RefSCC remains valid after all the
506 // postorder updates.
507 verify();
508#endif
509
510 // Otherwise we need to merge all of the SCCs in the cycle into a single
Chandler Carruthe5944d92016-02-17 00:18:16 +0000511 // result SCC.
512 //
513 // NB: We merge into the target because all of these functions were already
514 // reachable from the target, meaning any SCC-wide properties deduced about it
515 // other than the set of functions within it will not have changed.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000516 for (SCC *C : MergeRange) {
517 assert(C != &TargetSCC &&
518 "We merge *into* the target and shouldn't process it here!");
519 SCCIndices.erase(C);
520 TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end());
521 for (Node *N : C->Nodes)
522 G->SCCMap[N] = &TargetSCC;
523 C->clear();
524 DeletedSCCs.push_back(C);
525 }
526
527 // Erase the merged SCCs from the list and update the indices of the
528 // remaining SCCs.
529 int IndexOffset = MergeRange.end() - MergeRange.begin();
530 auto EraseEnd = SCCs.erase(MergeRange.begin(), MergeRange.end());
531 for (SCC *C : make_range(EraseEnd, SCCs.end()))
532 SCCIndices[C] -= IndexOffset;
533
534 // Now that the SCC structure is finalized, flip the kind to call.
535 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
536
537#ifndef NDEBUG
538 // And we're done! Verify in debug builds that the RefSCC is coherent.
539 verify();
540#endif
541 return DeletedSCCs;
Chandler Carruth5217c942014-04-30 10:48:36 +0000542}
543
Chandler Carruth88823462016-08-24 09:37:14 +0000544iterator_range<LazyCallGraph::RefSCC::iterator>
545LazyCallGraph::RefSCC::switchInternalEdgeToRef(Node &SourceN, Node &TargetN) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000546 assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
547
548 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.
561 if (&SourceSCC != &TargetSCC) {
562#ifndef NDEBUG
563 // Check that the RefSCC is still valid.
564 verify();
565#endif
Chandler Carruth88823462016-08-24 09:37:14 +0000566 return make_range(SCCs.end(), SCCs.end());
Chandler Carruthe5944d92016-02-17 00:18:16 +0000567 }
568
569 // Otherwise we are removing a call edge from a single SCC. This may break
570 // the cycle. In order to compute the new set of SCCs, we need to do a small
571 // DFS over the nodes within the SCC to form any sub-cycles that remain as
572 // distinct SCCs and compute a postorder over the resulting SCCs.
573 //
574 // However, we specially handle the target node. The target node is known to
575 // reach all other nodes in the original SCC by definition. This means that
576 // we want the old SCC to be replaced with an SCC contaning that node as it
577 // will be the root of whatever SCC DAG results from the DFS. Assumptions
578 // about an SCC such as the set of functions called will continue to hold,
579 // etc.
580
581 SCC &OldSCC = TargetSCC;
582 SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
583 SmallVector<Node *, 16> PendingSCCStack;
584 SmallVector<SCC *, 4> NewSCCs;
585
586 // Prepare the nodes for a fresh DFS.
587 SmallVector<Node *, 16> Worklist;
588 Worklist.swap(OldSCC.Nodes);
589 for (Node *N : Worklist) {
590 N->DFSNumber = N->LowLink = 0;
591 G->SCCMap.erase(N);
592 }
593
594 // Force the target node to be in the old SCC. This also enables us to take
595 // a very significant short-cut in the standard Tarjan walk to re-form SCCs
596 // below: whenever we build an edge that reaches the target node, we know
597 // that the target node eventually connects back to all other nodes in our
598 // walk. As a consequence, we can detect and handle participants in that
599 // cycle without walking all the edges that form this connection, and instead
600 // by relying on the fundamental guarantee coming into this operation (all
601 // nodes are reachable from the target due to previously forming an SCC).
602 TargetN.DFSNumber = TargetN.LowLink = -1;
603 OldSCC.Nodes.push_back(&TargetN);
604 G->SCCMap[&TargetN] = &OldSCC;
605
606 // Scan down the stack and DFS across the call edges.
607 for (Node *RootN : Worklist) {
608 assert(DFSStack.empty() &&
609 "Cannot begin a new root with a non-empty DFS stack!");
610 assert(PendingSCCStack.empty() &&
611 "Cannot begin a new root with pending nodes for an SCC!");
612
613 // Skip any nodes we've already reached in the DFS.
614 if (RootN->DFSNumber != 0) {
615 assert(RootN->DFSNumber == -1 &&
616 "Shouldn't have any mid-DFS root nodes!");
617 continue;
618 }
619
620 RootN->DFSNumber = RootN->LowLink = 1;
621 int NextDFSNumber = 2;
622
623 DFSStack.push_back({RootN, RootN->call_begin()});
624 do {
625 Node *N;
626 call_edge_iterator I;
627 std::tie(N, I) = DFSStack.pop_back_val();
628 auto E = N->call_end();
629 while (I != E) {
630 Node &ChildN = *I->getNode();
631 if (ChildN.DFSNumber == 0) {
632 // We haven't yet visited this child, so descend, pushing the current
633 // node onto the stack.
634 DFSStack.push_back({N, I});
635
636 assert(!G->SCCMap.count(&ChildN) &&
637 "Found a node with 0 DFS number but already in an SCC!");
638 ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
639 N = &ChildN;
640 I = N->call_begin();
641 E = N->call_end();
642 continue;
643 }
644
645 // Check for the child already being part of some component.
646 if (ChildN.DFSNumber == -1) {
647 if (G->lookupSCC(ChildN) == &OldSCC) {
648 // If the child is part of the old SCC, we know that it can reach
649 // every other node, so we have formed a cycle. Pull the entire DFS
650 // and pending stacks into it. See the comment above about setting
651 // up the old SCC for why we do this.
652 int OldSize = OldSCC.size();
653 OldSCC.Nodes.push_back(N);
654 OldSCC.Nodes.append(PendingSCCStack.begin(), PendingSCCStack.end());
655 PendingSCCStack.clear();
656 while (!DFSStack.empty())
657 OldSCC.Nodes.push_back(DFSStack.pop_back_val().first);
658 for (Node &N : make_range(OldSCC.begin() + OldSize, OldSCC.end())) {
659 N.DFSNumber = N.LowLink = -1;
660 G->SCCMap[&N] = &OldSCC;
661 }
662 N = nullptr;
663 break;
664 }
665
666 // If the child has already been added to some child component, it
667 // couldn't impact the low-link of this parent because it isn't
668 // connected, and thus its low-link isn't relevant so skip it.
669 ++I;
670 continue;
671 }
672
673 // Track the lowest linked child as the lowest link for this node.
674 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
675 if (ChildN.LowLink < N->LowLink)
676 N->LowLink = ChildN.LowLink;
677
678 // Move to the next edge.
679 ++I;
680 }
681 if (!N)
682 // Cleared the DFS early, start another round.
683 break;
684
685 // We've finished processing N and its descendents, put it on our pending
686 // SCC stack to eventually get merged into an SCC of nodes.
687 PendingSCCStack.push_back(N);
688
689 // If this node is linked to some lower entry, continue walking up the
690 // stack.
691 if (N->LowLink != N->DFSNumber)
692 continue;
693
694 // Otherwise, we've completed an SCC. Append it to our post order list of
695 // SCCs.
696 int RootDFSNumber = N->DFSNumber;
697 // Find the range of the node stack by walking down until we pass the
698 // root DFS number.
699 auto SCCNodes = make_range(
700 PendingSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +0000701 find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
702 return N->DFSNumber < RootDFSNumber;
703 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +0000704
705 // Form a new SCC out of these nodes and then clear them off our pending
706 // stack.
707 NewSCCs.push_back(G->createSCC(*this, SCCNodes));
708 for (Node &N : *NewSCCs.back()) {
709 N.DFSNumber = N.LowLink = -1;
710 G->SCCMap[&N] = NewSCCs.back();
711 }
712 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
713 } while (!DFSStack.empty());
714 }
715
716 // Insert the remaining SCCs before the old one. The old SCC can reach all
717 // other SCCs we form because it contains the target node of the removed edge
718 // of the old SCC. This means that we will have edges into all of the new
719 // SCCs, which means the old one must come last for postorder.
720 int OldIdx = SCCIndices[&OldSCC];
721 SCCs.insert(SCCs.begin() + OldIdx, NewSCCs.begin(), NewSCCs.end());
722
723 // Update the mapping from SCC* to index to use the new SCC*s, and remove the
724 // old SCC from the mapping.
725 for (int Idx = OldIdx, Size = SCCs.size(); Idx < Size; ++Idx)
726 SCCIndices[SCCs[Idx]] = Idx;
727
728#ifndef NDEBUG
729 // We're done. Check the validity on our way out.
730 verify();
731#endif
Chandler Carruth88823462016-08-24 09:37:14 +0000732
733 return make_range(SCCs.begin() + OldIdx,
734 SCCs.begin() + OldIdx + NewSCCs.size());
Chandler Carruthe5944d92016-02-17 00:18:16 +0000735}
736
737void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN,
738 Node &TargetN) {
739 assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
740
741 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
742 assert(G->lookupRefSCC(TargetN) != this &&
743 "Target must not be in this RefSCC.");
744 assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
745 "Target must be a descendant of the Source.");
746
747 // Edges between RefSCCs are the same regardless of call or ref, so we can
748 // just flip the edge here.
749 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
750
751#ifndef NDEBUG
752 // Check that the RefSCC is still valid.
753 verify();
754#endif
755}
756
757void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN,
758 Node &TargetN) {
759 assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
760
761 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
762 assert(G->lookupRefSCC(TargetN) != this &&
763 "Target must not be in this RefSCC.");
764 assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
765 "Target must be a descendant of the Source.");
766
767 // Edges between RefSCCs are the same regardless of call or ref, so we can
768 // just flip the edge here.
769 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
770
771#ifndef NDEBUG
772 // Check that the RefSCC is still valid.
773 verify();
774#endif
775}
776
777void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN,
778 Node &TargetN) {
779 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
780 assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
781
782 SourceN.insertEdgeInternal(TargetN, Edge::Ref);
783
784#ifndef NDEBUG
785 // Check that the RefSCC is still valid.
786 verify();
787#endif
788}
789
790void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN,
791 Edge::Kind EK) {
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000792 // First insert it into the caller.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000793 SourceN.insertEdgeInternal(TargetN, EK);
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000794
Chandler Carruthe5944d92016-02-17 00:18:16 +0000795 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000796
Chandler Carruthe5944d92016-02-17 00:18:16 +0000797 RefSCC &TargetC = *G->lookupRefSCC(TargetN);
798 assert(&TargetC != this && "Target must not be in this RefSCC.");
799 assert(TargetC.isDescendantOf(*this) &&
800 "Target must be a descendant of the Source.");
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000801
Chandler Carruth91539112015-12-28 01:54:20 +0000802 // The only change required is to add this SCC to the parent set of the
803 // callee.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000804 TargetC.Parents.insert(this);
805
806#ifndef NDEBUG
807 // Check that the RefSCC is still valid.
808 verify();
809#endif
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000810}
811
Chandler Carruthe5944d92016-02-17 00:18:16 +0000812SmallVector<LazyCallGraph::RefSCC *, 1>
813LazyCallGraph::RefSCC::insertIncomingRefEdge(Node &SourceN, Node &TargetN) {
814 assert(G->lookupRefSCC(TargetN) == this && "Target must be in this SCC.");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000815
Chandler Carruthe5944d92016-02-17 00:18:16 +0000816 // We store the RefSCCs found to be connected in postorder so that we can use
817 // that when merging. We also return this to the caller to allow them to
818 // invalidate information pertaining to these RefSCCs.
819 SmallVector<RefSCC *, 1> Connected;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000820
Chandler Carruthe5944d92016-02-17 00:18:16 +0000821 RefSCC &SourceC = *G->lookupRefSCC(SourceN);
822 assert(&SourceC != this && "Source must not be in this SCC.");
823 assert(SourceC.isDescendantOf(*this) &&
824 "Source must be a descendant of the Target.");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000825
826 // The algorithm we use for merging SCCs based on the cycle introduced here
Chandler Carruthe5944d92016-02-17 00:18:16 +0000827 // is to walk the RefSCC inverted DAG formed by the parent sets. The inverse
828 // graph has the same cycle properties as the actual DAG of the RefSCCs, and
829 // when forming RefSCCs lazily by a DFS, the bottom of the graph won't exist
830 // in many cases which should prune the search space.
Chandler Carruth312dddf2014-05-04 09:38:32 +0000831 //
Chandler Carruthe5944d92016-02-17 00:18:16 +0000832 // FIXME: We can get this pruning behavior even after the incremental RefSCC
Chandler Carruth312dddf2014-05-04 09:38:32 +0000833 // formation by leaving behind (conservative) DFS numberings in the nodes,
834 // and pruning the search with them. These would need to be cleverly updated
835 // during the removal of intra-SCC edges, but could be preserved
836 // conservatively.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000837 //
838 // FIXME: This operation currently creates ordering stability problems
839 // because we don't use stably ordered containers for the parent SCCs.
Chandler Carruth312dddf2014-05-04 09:38:32 +0000840
Chandler Carruthe5944d92016-02-17 00:18:16 +0000841 // The set of RefSCCs that are connected to the parent, and thus will
Chandler Carruth312dddf2014-05-04 09:38:32 +0000842 // participate in the merged connected component.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000843 SmallPtrSet<RefSCC *, 8> ConnectedSet;
844 ConnectedSet.insert(this);
Chandler Carruth312dddf2014-05-04 09:38:32 +0000845
846 // We build up a DFS stack of the parents chains.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000847 SmallVector<std::pair<RefSCC *, parent_iterator>, 8> DFSStack;
848 SmallPtrSet<RefSCC *, 8> Visited;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000849 int ConnectedDepth = -1;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000850 DFSStack.push_back({&SourceC, SourceC.parent_begin()});
851 do {
852 auto DFSPair = DFSStack.pop_back_val();
853 RefSCC *C = DFSPair.first;
854 parent_iterator I = DFSPair.second;
855 auto E = C->parent_end();
856
Chandler Carruth312dddf2014-05-04 09:38:32 +0000857 while (I != E) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000858 RefSCC &Parent = *I++;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000859
860 // If we have already processed this parent SCC, skip it, and remember
861 // whether it was connected so we don't have to check the rest of the
862 // stack. This also handles when we reach a child of the 'this' SCC (the
863 // callee) which terminates the search.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000864 if (ConnectedSet.count(&Parent)) {
865 assert(ConnectedDepth < (int)DFSStack.size() &&
866 "Cannot have a connected depth greater than the DFS depth!");
867 ConnectedDepth = DFSStack.size();
Chandler Carruth312dddf2014-05-04 09:38:32 +0000868 continue;
869 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000870 if (Visited.count(&Parent))
Chandler Carruth312dddf2014-05-04 09:38:32 +0000871 continue;
872
873 // We fully explore the depth-first space, adding nodes to the connected
874 // set only as we pop them off, so "recurse" by rotating to the parent.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000875 DFSStack.push_back({C, I});
876 C = &Parent;
877 I = C->parent_begin();
878 E = C->parent_end();
Chandler Carruth312dddf2014-05-04 09:38:32 +0000879 }
880
881 // If we've found a connection anywhere below this point on the stack (and
882 // thus up the parent graph from the caller), the current node needs to be
883 // added to the connected set now that we've processed all of its parents.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000884 if ((int)DFSStack.size() == ConnectedDepth) {
Chandler Carruth312dddf2014-05-04 09:38:32 +0000885 --ConnectedDepth; // We're finished with this connection.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000886 bool Inserted = ConnectedSet.insert(C).second;
887 (void)Inserted;
888 assert(Inserted && "Cannot insert a refSCC multiple times!");
889 Connected.push_back(C);
Chandler Carruth312dddf2014-05-04 09:38:32 +0000890 } else {
891 // Otherwise remember that its parents don't ever connect.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000892 assert(ConnectedDepth < (int)DFSStack.size() &&
Chandler Carruth312dddf2014-05-04 09:38:32 +0000893 "Cannot have a connected depth greater than the DFS depth!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000894 Visited.insert(C);
Chandler Carruth312dddf2014-05-04 09:38:32 +0000895 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000896 } while (!DFSStack.empty());
Chandler Carruth312dddf2014-05-04 09:38:32 +0000897
898 // Now that we have identified all of the SCCs which need to be merged into
899 // a connected set with the inserted edge, merge all of them into this SCC.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000900 // We walk the newly connected RefSCCs in the reverse postorder of the parent
901 // DAG walk above and merge in each of their SCC postorder lists. This
902 // ensures a merged postorder SCC list.
903 SmallVector<SCC *, 16> MergedSCCs;
904 int SCCIndex = 0;
905 for (RefSCC *C : reverse(Connected)) {
906 assert(C != this &&
907 "This RefSCC should terminate the DFS without being reached.");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000908
Chandler Carruthe5944d92016-02-17 00:18:16 +0000909 // Merge the parents which aren't part of the merge into the our parents.
910 for (RefSCC *ParentC : C->Parents)
911 if (!ConnectedSet.count(ParentC))
912 Parents.insert(ParentC);
913 C->Parents.clear();
914
915 // Walk the inner SCCs to update their up-pointer and walk all the edges to
916 // update any parent sets.
917 // FIXME: We should try to find a way to avoid this (rather expensive) edge
918 // walk by updating the parent sets in some other manner.
919 for (SCC &InnerC : *C) {
920 InnerC.OuterRefSCC = this;
921 SCCIndices[&InnerC] = SCCIndex++;
922 for (Node &N : InnerC) {
923 G->SCCMap[&N] = &InnerC;
924 for (Edge &E : N) {
925 assert(E.getNode() &&
926 "Cannot have a null node within a visited SCC!");
927 RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
928 if (ConnectedSet.count(&ChildRC))
929 continue;
930 ChildRC.Parents.erase(C);
931 ChildRC.Parents.insert(this);
932 }
Chandler Carruth312dddf2014-05-04 09:38:32 +0000933 }
Chandler Carruth312dddf2014-05-04 09:38:32 +0000934 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000935
936 // Now merge in the SCCs. We can actually move here so try to reuse storage
937 // the first time through.
938 if (MergedSCCs.empty())
939 MergedSCCs = std::move(C->SCCs);
940 else
941 MergedSCCs.append(C->SCCs.begin(), C->SCCs.end());
942 C->SCCs.clear();
Chandler Carruth312dddf2014-05-04 09:38:32 +0000943 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000944
945 // Finally append our original SCCs to the merged list and move it into
946 // place.
947 for (SCC &InnerC : *this)
948 SCCIndices[&InnerC] = SCCIndex++;
949 MergedSCCs.append(SCCs.begin(), SCCs.end());
950 SCCs = std::move(MergedSCCs);
951
952 // At this point we have a merged RefSCC with a post-order SCCs list, just
953 // connect the nodes to form the new edge.
954 SourceN.insertEdgeInternal(TargetN, Edge::Ref);
955
956#ifndef NDEBUG
957 // Check that the RefSCC is still valid.
958 verify();
959#endif
Chandler Carruth312dddf2014-05-04 09:38:32 +0000960
961 // We return the list of SCCs which were merged so that callers can
962 // invalidate any data they have associated with those SCCs. Note that these
963 // SCCs are no longer in an interesting state (they are totally empty) but
964 // the pointers will remain stable for the life of the graph itself.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000965 return Connected;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000966}
967
Chandler Carruthe5944d92016-02-17 00:18:16 +0000968void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) {
969 assert(G->lookupRefSCC(SourceN) == this &&
970 "The source must be a member of this RefSCC.");
971
972 RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
973 assert(&TargetRC != this && "The target must not be a member of this RefSCC");
974
David Majnemer0d955d02016-08-11 22:21:41 +0000975 assert(!is_contained(G->LeafRefSCCs, this) &&
Chandler Carruthe5944d92016-02-17 00:18:16 +0000976 "Cannot have a leaf RefSCC source.");
977
Chandler Carruthaa839b22014-04-27 01:59:50 +0000978 // First remove it from the node.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000979 SourceN.removeEdgeInternal(TargetN.getFunction());
Chandler Carruthaa839b22014-04-27 01:59:50 +0000980
Chandler Carruthe5944d92016-02-17 00:18:16 +0000981 bool HasOtherEdgeToChildRC = false;
982 bool HasOtherChildRC = false;
983 for (SCC *InnerC : SCCs) {
984 for (Node &N : *InnerC) {
985 for (Edge &E : N) {
986 assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
987 RefSCC &OtherChildRC = *G->lookupRefSCC(*E.getNode());
988 if (&OtherChildRC == &TargetRC) {
989 HasOtherEdgeToChildRC = true;
990 break;
991 }
992 if (&OtherChildRC != this)
993 HasOtherChildRC = true;
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000994 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000995 if (HasOtherEdgeToChildRC)
996 break;
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000997 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000998 if (HasOtherEdgeToChildRC)
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000999 break;
1000 }
1001 // Because the SCCs form a DAG, deleting such an edge cannot change the set
1002 // of SCCs in the graph. However, it may cut an edge of the SCC DAG, making
Chandler Carruthe5944d92016-02-17 00:18:16 +00001003 // the source SCC no longer connected to the target SCC. If so, we need to
1004 // update the target SCC's map of its parents.
1005 if (!HasOtherEdgeToChildRC) {
1006 bool Removed = TargetRC.Parents.erase(this);
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001007 (void)Removed;
1008 assert(Removed &&
Chandler Carruthe5944d92016-02-17 00:18:16 +00001009 "Did not find the source SCC in the target SCC's parent list!");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001010
1011 // It may orphan an SCC if it is the last edge reaching it, but that does
1012 // not violate any invariants of the graph.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001013 if (TargetRC.Parents.empty())
1014 DEBUG(dbgs() << "LCG: Update removing " << SourceN.getFunction().getName()
1015 << " -> " << TargetN.getFunction().getName()
Chandler Carruthaa839b22014-04-27 01:59:50 +00001016 << " edge orphaned the callee's SCC!\n");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001017
Chandler Carruthe5944d92016-02-17 00:18:16 +00001018 // It may make the Source SCC a leaf SCC.
1019 if (!HasOtherChildRC)
1020 G->LeafRefSCCs.push_back(this);
Chandler Carruthaca48d02014-04-26 09:06:53 +00001021 }
1022}
1023
Chandler Carruthe5944d92016-02-17 00:18:16 +00001024SmallVector<LazyCallGraph::RefSCC *, 1>
1025LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN, Node &TargetN) {
1026 assert(!SourceN[TargetN].isCall() &&
1027 "Cannot remove a call edge, it must first be made a ref edge");
Chandler Carruthaa839b22014-04-27 01:59:50 +00001028
Chandler Carruthe5944d92016-02-17 00:18:16 +00001029 // First remove the actual edge.
1030 SourceN.removeEdgeInternal(TargetN.getFunction());
1031
1032 // We return a list of the resulting *new* RefSCCs in post-order.
1033 SmallVector<RefSCC *, 1> Result;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001034
Chandler Carrutha7205b62014-04-26 03:36:37 +00001035 // Direct recursion doesn't impact the SCC graph at all.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001036 if (&SourceN == &TargetN)
1037 return Result;
Chandler Carrutha7205b62014-04-26 03:36:37 +00001038
Chandler Carruthe5944d92016-02-17 00:18:16 +00001039 // We build somewhat synthetic new RefSCCs by providing a postorder mapping
1040 // for each inner SCC. We also store these associated with *nodes* rather
1041 // than SCCs because this saves a round-trip through the node->SCC map and in
1042 // the common case, SCCs are small. We will verify that we always give the
1043 // same number to every node in the SCC such that these are equivalent.
1044 const int RootPostOrderNumber = 0;
1045 int PostOrderNumber = RootPostOrderNumber + 1;
1046 SmallDenseMap<Node *, int> PostOrderMapping;
1047
1048 // Every node in the target SCC can already reach every node in this RefSCC
1049 // (by definition). It is the only node we know will stay inside this RefSCC.
1050 // Everything which transitively reaches Target will also remain in the
1051 // RefSCC. We handle this by pre-marking that the nodes in the target SCC map
1052 // back to the root post order number.
1053 //
1054 // This also enables us to take a very significant short-cut in the standard
1055 // Tarjan walk to re-form RefSCCs below: whenever we build an edge that
1056 // references the target node, we know that the target node eventually
1057 // references all other nodes in our walk. As a consequence, we can detect
1058 // and handle participants in that cycle without walking all the edges that
1059 // form the connections, and instead by relying on the fundamental guarantee
1060 // coming into this operation.
1061 SCC &TargetC = *G->lookupSCC(TargetN);
1062 for (Node &N : TargetC)
1063 PostOrderMapping[&N] = RootPostOrderNumber;
1064
1065 // Reset all the other nodes to prepare for a DFS over them, and add them to
1066 // our worklist.
1067 SmallVector<Node *, 8> Worklist;
1068 for (SCC *C : SCCs) {
1069 if (C == &TargetC)
1070 continue;
1071
1072 for (Node &N : *C)
1073 N.DFSNumber = N.LowLink = 0;
1074
1075 Worklist.append(C->Nodes.begin(), C->Nodes.end());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001076 }
1077
Chandler Carruthe5944d92016-02-17 00:18:16 +00001078 auto MarkNodeForSCCNumber = [&PostOrderMapping](Node &N, int Number) {
1079 N.DFSNumber = N.LowLink = -1;
1080 PostOrderMapping[&N] = Number;
1081 };
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001082
Chandler Carruthe5944d92016-02-17 00:18:16 +00001083 SmallVector<std::pair<Node *, edge_iterator>, 4> DFSStack;
1084 SmallVector<Node *, 4> PendingRefSCCStack;
Chandler Carruthaca48d02014-04-26 09:06:53 +00001085 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001086 assert(DFSStack.empty() &&
1087 "Cannot begin a new root with a non-empty DFS stack!");
1088 assert(PendingRefSCCStack.empty() &&
1089 "Cannot begin a new root with pending nodes for an SCC!");
1090
1091 Node *RootN = Worklist.pop_back_val();
1092 // Skip any nodes we've already reached in the DFS.
1093 if (RootN->DFSNumber != 0) {
1094 assert(RootN->DFSNumber == -1 &&
1095 "Shouldn't have any mid-DFS root nodes!");
1096 continue;
1097 }
1098
1099 RootN->DFSNumber = RootN->LowLink = 1;
1100 int NextDFSNumber = 2;
1101
1102 DFSStack.push_back({RootN, RootN->begin()});
1103 do {
1104 Node *N;
1105 edge_iterator I;
1106 std::tie(N, I) = DFSStack.pop_back_val();
1107 auto E = N->end();
1108
1109 assert(N->DFSNumber != 0 && "We should always assign a DFS number "
1110 "before processing a node.");
1111
1112 while (I != E) {
1113 Node &ChildN = I->getNode(*G);
1114 if (ChildN.DFSNumber == 0) {
1115 // Mark that we should start at this child when next this node is the
1116 // top of the stack. We don't start at the next child to ensure this
1117 // child's lowlink is reflected.
1118 DFSStack.push_back({N, I});
1119
1120 // Continue, resetting to the child node.
1121 ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
1122 N = &ChildN;
1123 I = ChildN.begin();
1124 E = ChildN.end();
1125 continue;
1126 }
1127 if (ChildN.DFSNumber == -1) {
1128 // Check if this edge's target node connects to the deleted edge's
1129 // target node. If so, we know that every node connected will end up
1130 // in this RefSCC, so collapse the entire current stack into the root
1131 // slot in our SCC numbering. See above for the motivation of
1132 // optimizing the target connected nodes in this way.
1133 auto PostOrderI = PostOrderMapping.find(&ChildN);
1134 if (PostOrderI != PostOrderMapping.end() &&
1135 PostOrderI->second == RootPostOrderNumber) {
1136 MarkNodeForSCCNumber(*N, RootPostOrderNumber);
1137 while (!PendingRefSCCStack.empty())
1138 MarkNodeForSCCNumber(*PendingRefSCCStack.pop_back_val(),
1139 RootPostOrderNumber);
1140 while (!DFSStack.empty())
1141 MarkNodeForSCCNumber(*DFSStack.pop_back_val().first,
1142 RootPostOrderNumber);
1143 // Ensure we break all the way out of the enclosing loop.
1144 N = nullptr;
1145 break;
1146 }
1147
1148 // If this child isn't currently in this RefSCC, no need to process
1149 // it.
1150 // However, we do need to remove this RefSCC from its RefSCC's parent
1151 // set.
1152 RefSCC &ChildRC = *G->lookupRefSCC(ChildN);
1153 ChildRC.Parents.erase(this);
1154 ++I;
1155 continue;
1156 }
1157
1158 // Track the lowest link of the children, if any are still in the stack.
1159 // Any child not on the stack will have a LowLink of -1.
1160 assert(ChildN.LowLink != 0 &&
1161 "Low-link must not be zero with a non-zero DFS number.");
1162 if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
1163 N->LowLink = ChildN.LowLink;
1164 ++I;
1165 }
1166 if (!N)
1167 // We short-circuited this node.
1168 break;
1169
1170 // We've finished processing N and its descendents, put it on our pending
1171 // stack to eventually get merged into a RefSCC.
1172 PendingRefSCCStack.push_back(N);
1173
1174 // If this node is linked to some lower entry, continue walking up the
1175 // stack.
1176 if (N->LowLink != N->DFSNumber) {
1177 assert(!DFSStack.empty() &&
1178 "We never found a viable root for a RefSCC to pop off!");
1179 continue;
1180 }
1181
1182 // Otherwise, form a new RefSCC from the top of the pending node stack.
1183 int RootDFSNumber = N->DFSNumber;
1184 // Find the range of the node stack by walking down until we pass the
1185 // root DFS number.
1186 auto RefSCCNodes = make_range(
1187 PendingRefSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +00001188 find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1189 return N->DFSNumber < RootDFSNumber;
1190 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +00001191
1192 // Mark the postorder number for these nodes and clear them off the
1193 // stack. We'll use the postorder number to pull them into RefSCCs at the
1194 // end. FIXME: Fuse with the loop above.
1195 int RefSCCNumber = PostOrderNumber++;
1196 for (Node *N : RefSCCNodes)
1197 MarkNodeForSCCNumber(*N, RefSCCNumber);
1198
1199 PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1200 PendingRefSCCStack.end());
1201 } while (!DFSStack.empty());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001202
Chandler Carruthaca48d02014-04-26 09:06:53 +00001203 assert(DFSStack.empty() && "Didn't flush the entire DFS stack!");
Chandler Carruthe5944d92016-02-17 00:18:16 +00001204 assert(PendingRefSCCStack.empty() && "Didn't flush all pending nodes!");
Chandler Carruthaca48d02014-04-26 09:06:53 +00001205 } while (!Worklist.empty());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001206
Chandler Carruthe5944d92016-02-17 00:18:16 +00001207 // We now have a post-order numbering for RefSCCs and a mapping from each
1208 // node in this RefSCC to its final RefSCC. We create each new RefSCC node
1209 // (re-using this RefSCC node for the root) and build a radix-sort style map
1210 // from postorder number to the RefSCC. We then append SCCs to each of these
1211 // RefSCCs in the order they occured in the original SCCs container.
1212 for (int i = 1; i < PostOrderNumber; ++i)
1213 Result.push_back(G->createRefSCC(*G));
1214
1215 for (SCC *C : SCCs) {
1216 auto PostOrderI = PostOrderMapping.find(&*C->begin());
1217 assert(PostOrderI != PostOrderMapping.end() &&
1218 "Cannot have missing mappings for nodes!");
1219 int SCCNumber = PostOrderI->second;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001220#ifndef NDEBUG
Chandler Carruthe5944d92016-02-17 00:18:16 +00001221 for (Node &N : *C)
1222 assert(PostOrderMapping.find(&N)->second == SCCNumber &&
1223 "Cannot have different numbers for nodes in the same SCC!");
1224#endif
1225 if (SCCNumber == 0)
1226 // The root node is handled separately by removing the SCCs.
1227 continue;
1228
1229 RefSCC &RC = *Result[SCCNumber - 1];
1230 int SCCIndex = RC.SCCs.size();
1231 RC.SCCs.push_back(C);
1232 SCCIndices[C] = SCCIndex;
1233 C->OuterRefSCC = &RC;
1234 }
1235
1236 // FIXME: We re-walk the edges in each RefSCC to establish whether it is
1237 // a leaf and connect it to the rest of the graph's parents lists. This is
1238 // really wasteful. We should instead do this during the DFS to avoid yet
1239 // another edge walk.
1240 for (RefSCC *RC : Result)
1241 G->connectRefSCC(*RC);
1242
1243 // Now erase all but the root's SCCs.
David Majnemer42531262016-08-12 03:55:06 +00001244 SCCs.erase(remove_if(SCCs,
1245 [&](SCC *C) {
1246 return PostOrderMapping.lookup(&*C->begin()) !=
1247 RootPostOrderNumber;
1248 }),
Chandler Carruthe5944d92016-02-17 00:18:16 +00001249 SCCs.end());
Chandler Carruth88823462016-08-24 09:37:14 +00001250 SCCIndices.clear();
1251 for (int i = 0, Size = SCCs.size(); i < Size; ++i)
1252 SCCIndices[SCCs[i]] = i;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001253
1254#ifndef NDEBUG
1255 // Now we need to reconnect the current (root) SCC to the graph. We do this
1256 // manually because we can special case our leaf handling and detect errors.
1257 bool IsLeaf = true;
1258#endif
1259 for (SCC *C : SCCs)
1260 for (Node &N : *C) {
1261 for (Edge &E : N) {
1262 assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
1263 RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
1264 if (&ChildRC == this)
1265 continue;
1266 ChildRC.Parents.insert(this);
1267#ifndef NDEBUG
1268 IsLeaf = false;
1269#endif
1270 }
1271 }
1272#ifndef NDEBUG
1273 if (!Result.empty())
1274 assert(!IsLeaf && "This SCC cannot be a leaf as we have split out new "
1275 "SCCs by removing this edge.");
David Majnemer0a16c222016-08-11 21:15:00 +00001276 if (none_of(G->LeafRefSCCs, [&](RefSCC *C) { return C == this; }))
Chandler Carruthe5944d92016-02-17 00:18:16 +00001277 assert(!IsLeaf && "This SCC cannot be a leaf as it already had child "
1278 "SCCs before we removed this edge.");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001279#endif
1280 // If this SCC stopped being a leaf through this edge removal, remove it from
Chandler Carruthe5944d92016-02-17 00:18:16 +00001281 // the leaf SCC list. Note that this DTRT in the case where this was never
1282 // a leaf.
1283 // FIXME: As LeafRefSCCs could be very large, we might want to not walk the
1284 // entire list if this RefSCC wasn't a leaf before the edge removal.
1285 if (!Result.empty())
1286 G->LeafRefSCCs.erase(
1287 std::remove(G->LeafRefSCCs.begin(), G->LeafRefSCCs.end(), this),
1288 G->LeafRefSCCs.end());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001289
1290 // Return the new list of SCCs.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001291 return Result;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001292}
1293
Chandler Carruthe5944d92016-02-17 00:18:16 +00001294void LazyCallGraph::insertEdge(Node &SourceN, Function &Target, Edge::Kind EK) {
Chandler Carruthc00a7ff2014-04-28 11:10:23 +00001295 assert(SCCMap.empty() && DFSStack.empty() &&
1296 "This method cannot be called after SCCs have been formed!");
1297
Chandler Carruthe5944d92016-02-17 00:18:16 +00001298 return SourceN.insertEdgeInternal(Target, EK);
Chandler Carruthc00a7ff2014-04-28 11:10:23 +00001299}
1300
Chandler Carruthe5944d92016-02-17 00:18:16 +00001301void LazyCallGraph::removeEdge(Node &SourceN, Function &Target) {
Chandler Carruthaa839b22014-04-27 01:59:50 +00001302 assert(SCCMap.empty() && DFSStack.empty() &&
1303 "This method cannot be called after SCCs have been formed!");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001304
Chandler Carruthe5944d92016-02-17 00:18:16 +00001305 return SourceN.removeEdgeInternal(Target);
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001306}
1307
Chandler Carruth2a898e02014-04-23 23:20:36 +00001308LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
1309 return *new (MappedN = BPA.Allocate()) Node(*this, F);
Chandler Carruthd8d865e2014-04-18 11:02:33 +00001310}
1311
1312void LazyCallGraph::updateGraphPtrs() {
Chandler Carruthb60cb312014-04-17 07:25:59 +00001313 // Process all nodes updating the graph pointers.
Chandler Carruthaa839b22014-04-27 01:59:50 +00001314 {
1315 SmallVector<Node *, 16> Worklist;
Chandler Carrutha4499e92016-02-02 03:57:13 +00001316 for (Edge &E : EntryEdges)
1317 if (Node *EntryN = E.getNode())
Chandler Carruthaa839b22014-04-27 01:59:50 +00001318 Worklist.push_back(EntryN);
Chandler Carruthb60cb312014-04-17 07:25:59 +00001319
Chandler Carruthaa839b22014-04-27 01:59:50 +00001320 while (!Worklist.empty()) {
1321 Node *N = Worklist.pop_back_val();
1322 N->G = this;
Chandler Carrutha4499e92016-02-02 03:57:13 +00001323 for (Edge &E : N->Edges)
Chandler Carruthe5944d92016-02-17 00:18:16 +00001324 if (Node *TargetN = E.getNode())
1325 Worklist.push_back(TargetN);
Chandler Carruthaa839b22014-04-27 01:59:50 +00001326 }
1327 }
1328
1329 // Process all SCCs updating the graph pointers.
1330 {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001331 SmallVector<RefSCC *, 16> Worklist(LeafRefSCCs.begin(), LeafRefSCCs.end());
Chandler Carruthaa839b22014-04-27 01:59:50 +00001332
1333 while (!Worklist.empty()) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001334 RefSCC &C = *Worklist.pop_back_val();
1335 C.G = this;
1336 for (RefSCC &ParentC : C.parents())
1337 Worklist.push_back(&ParentC);
Chandler Carruthaa839b22014-04-27 01:59:50 +00001338 }
Chandler Carruthb60cb312014-04-17 07:25:59 +00001339 }
Chandler Carruthbf71a342014-02-06 04:37:03 +00001340}
Chandler Carruthbf71a342014-02-06 04:37:03 +00001341
Chandler Carruthe5944d92016-02-17 00:18:16 +00001342/// Build the internal SCCs for a RefSCC from a sequence of nodes.
1343///
1344/// Appends the SCCs to the provided vector and updates the map with their
1345/// indices. Both the vector and map must be empty when passed into this
1346/// routine.
1347void LazyCallGraph::buildSCCs(RefSCC &RC, node_stack_range Nodes) {
1348 assert(RC.SCCs.empty() && "Already built SCCs!");
1349 assert(RC.SCCIndices.empty() && "Already mapped SCC indices!");
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001350
Chandler Carruthe5944d92016-02-17 00:18:16 +00001351 for (Node *N : Nodes) {
1352 assert(N->LowLink >= (*Nodes.begin())->LowLink &&
Chandler Carruthcace6622014-04-23 10:31:17 +00001353 "We cannot have a low link in an SCC lower than its root on the "
1354 "stack!");
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001355
Chandler Carruthe5944d92016-02-17 00:18:16 +00001356 // This node will go into the next RefSCC, clear out its DFS and low link
1357 // as we scan.
1358 N->DFSNumber = N->LowLink = 0;
1359 }
1360
1361 // Each RefSCC contains a DAG of the call SCCs. To build these, we do
1362 // a direct walk of the call edges using Tarjan's algorithm. We reuse the
1363 // internal storage as we won't need it for the outer graph's DFS any longer.
1364
1365 SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
1366 SmallVector<Node *, 16> PendingSCCStack;
1367
1368 // Scan down the stack and DFS across the call edges.
1369 for (Node *RootN : Nodes) {
1370 assert(DFSStack.empty() &&
1371 "Cannot begin a new root with a non-empty DFS stack!");
1372 assert(PendingSCCStack.empty() &&
1373 "Cannot begin a new root with pending nodes for an SCC!");
1374
1375 // Skip any nodes we've already reached in the DFS.
1376 if (RootN->DFSNumber != 0) {
1377 assert(RootN->DFSNumber == -1 &&
1378 "Shouldn't have any mid-DFS root nodes!");
1379 continue;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001380 }
1381
Chandler Carruthe5944d92016-02-17 00:18:16 +00001382 RootN->DFSNumber = RootN->LowLink = 1;
1383 int NextDFSNumber = 2;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001384
Chandler Carruthe5944d92016-02-17 00:18:16 +00001385 DFSStack.push_back({RootN, RootN->call_begin()});
1386 do {
1387 Node *N;
1388 call_edge_iterator I;
1389 std::tie(N, I) = DFSStack.pop_back_val();
1390 auto E = N->call_end();
1391 while (I != E) {
1392 Node &ChildN = *I->getNode();
1393 if (ChildN.DFSNumber == 0) {
1394 // We haven't yet visited this child, so descend, pushing the current
1395 // node onto the stack.
1396 DFSStack.push_back({N, I});
1397
1398 assert(!lookupSCC(ChildN) &&
1399 "Found a node with 0 DFS number but already in an SCC!");
1400 ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
1401 N = &ChildN;
1402 I = N->call_begin();
1403 E = N->call_end();
1404 continue;
1405 }
1406
1407 // If the child has already been added to some child component, it
1408 // couldn't impact the low-link of this parent because it isn't
1409 // connected, and thus its low-link isn't relevant so skip it.
1410 if (ChildN.DFSNumber == -1) {
1411 ++I;
1412 continue;
1413 }
1414
1415 // Track the lowest linked child as the lowest link for this node.
1416 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1417 if (ChildN.LowLink < N->LowLink)
1418 N->LowLink = ChildN.LowLink;
1419
1420 // Move to the next edge.
1421 ++I;
1422 }
1423
1424 // We've finished processing N and its descendents, put it on our pending
1425 // SCC stack to eventually get merged into an SCC of nodes.
1426 PendingSCCStack.push_back(N);
1427
1428 // If this node is linked to some lower entry, continue walking up the
1429 // stack.
1430 if (N->LowLink != N->DFSNumber)
1431 continue;
1432
1433 // Otherwise, we've completed an SCC. Append it to our post order list of
1434 // SCCs.
1435 int RootDFSNumber = N->DFSNumber;
1436 // Find the range of the node stack by walking down until we pass the
1437 // root DFS number.
1438 auto SCCNodes = make_range(
1439 PendingSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +00001440 find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
1441 return N->DFSNumber < RootDFSNumber;
1442 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +00001443 // Form a new SCC out of these nodes and then clear them off our pending
1444 // stack.
1445 RC.SCCs.push_back(createSCC(RC, SCCNodes));
1446 for (Node &N : *RC.SCCs.back()) {
1447 N.DFSNumber = N.LowLink = -1;
1448 SCCMap[&N] = RC.SCCs.back();
1449 }
1450 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
1451 } while (!DFSStack.empty());
1452 }
1453
1454 // Wire up the SCC indices.
1455 for (int i = 0, Size = RC.SCCs.size(); i < Size; ++i)
1456 RC.SCCIndices[RC.SCCs[i]] = i;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001457}
1458
Chandler Carruthe5944d92016-02-17 00:18:16 +00001459// FIXME: We should move callers of this to embed the parent linking and leaf
1460// tracking into their DFS in order to remove a full walk of all edges.
1461void LazyCallGraph::connectRefSCC(RefSCC &RC) {
1462 // Walk all edges in the RefSCC (this remains linear as we only do this once
1463 // when we build the RefSCC) to connect it to the parent sets of its
1464 // children.
1465 bool IsLeaf = true;
1466 for (SCC &C : RC)
1467 for (Node &N : C)
1468 for (Edge &E : N) {
1469 assert(E.getNode() &&
1470 "Cannot have a missing node in a visited part of the graph!");
1471 RefSCC &ChildRC = *lookupRefSCC(*E.getNode());
1472 if (&ChildRC == &RC)
1473 continue;
1474 ChildRC.Parents.insert(&RC);
1475 IsLeaf = false;
1476 }
1477
1478 // For the SCCs where we fine no child SCCs, add them to the leaf list.
1479 if (IsLeaf)
1480 LeafRefSCCs.push_back(&RC);
1481}
1482
1483LazyCallGraph::RefSCC *LazyCallGraph::getNextRefSCCInPostOrder() {
1484 if (DFSStack.empty()) {
1485 Node *N;
Chandler Carruth90821c22014-04-26 09:45:55 +00001486 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001487 // If we've handled all candidate entry nodes to the SCC forest, we're
1488 // done.
1489 if (RefSCCEntryNodes.empty())
Chandler Carruth90821c22014-04-26 09:45:55 +00001490 return nullptr;
Chandler Carruth18eadd922014-04-18 10:50:32 +00001491
Chandler Carruthe5944d92016-02-17 00:18:16 +00001492 N = &get(*RefSCCEntryNodes.pop_back_val());
Chandler Carruth90821c22014-04-26 09:45:55 +00001493 } while (N->DFSNumber != 0);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001494
1495 // Found a new root, begin the DFS here.
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001496 N->LowLink = N->DFSNumber = 1;
Chandler Carruth09751bf2014-04-24 09:59:59 +00001497 NextDFSNumber = 2;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001498 DFSStack.push_back({N, N->begin()});
Chandler Carruth18eadd922014-04-18 10:50:32 +00001499 }
1500
Chandler Carruth91dcf0f2014-04-24 21:19:30 +00001501 for (;;) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001502 Node *N;
1503 edge_iterator I;
1504 std::tie(N, I) = DFSStack.pop_back_val();
1505
1506 assert(N->DFSNumber > 0 && "We should always assign a DFS number "
1507 "before placing a node onto the stack.");
Chandler Carruth24553932014-04-24 11:05:20 +00001508
Chandler Carrutha4499e92016-02-02 03:57:13 +00001509 auto E = N->end();
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001510 while (I != E) {
Chandler Carrutha4499e92016-02-02 03:57:13 +00001511 Node &ChildN = I->getNode(*this);
Chandler Carruthbd5d3082014-04-23 23:34:48 +00001512 if (ChildN.DFSNumber == 0) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001513 // We haven't yet visited this child, so descend, pushing the current
1514 // node onto the stack.
1515 DFSStack.push_back({N, N->begin()});
Chandler Carruth18eadd922014-04-18 10:50:32 +00001516
Chandler Carruth09751bf2014-04-24 09:59:59 +00001517 assert(!SCCMap.count(&ChildN) &&
1518 "Found a node with 0 DFS number but already in an SCC!");
1519 ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001520 N = &ChildN;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001521 I = N->begin();
1522 E = N->end();
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001523 continue;
Chandler Carruthcace6622014-04-23 10:31:17 +00001524 }
1525
Chandler Carruthe5944d92016-02-17 00:18:16 +00001526 // If the child has already been added to some child component, it
1527 // couldn't impact the low-link of this parent because it isn't
1528 // connected, and thus its low-link isn't relevant so skip it.
1529 if (ChildN.DFSNumber == -1) {
1530 ++I;
1531 continue;
1532 }
1533
1534 // Track the lowest linked child as the lowest link for this node.
1535 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1536 if (ChildN.LowLink < N->LowLink)
Chandler Carruthbd5d3082014-04-23 23:34:48 +00001537 N->LowLink = ChildN.LowLink;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001538
1539 // Move to the next edge.
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001540 ++I;
Chandler Carruth18eadd922014-04-18 10:50:32 +00001541 }
1542
Chandler Carruthe5944d92016-02-17 00:18:16 +00001543 // We've finished processing N and its descendents, put it on our pending
1544 // SCC stack to eventually get merged into an SCC of nodes.
1545 PendingRefSCCStack.push_back(N);
Chandler Carruth18eadd922014-04-18 10:50:32 +00001546
Chandler Carruthe5944d92016-02-17 00:18:16 +00001547 // If this node is linked to some lower entry, continue walking up the
1548 // stack.
1549 if (N->LowLink != N->DFSNumber) {
1550 assert(!DFSStack.empty() &&
1551 "We never found a viable root for an SCC to pop off!");
1552 continue;
1553 }
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001554
Chandler Carruthe5944d92016-02-17 00:18:16 +00001555 // Otherwise, form a new RefSCC from the top of the pending node stack.
1556 int RootDFSNumber = N->DFSNumber;
1557 // Find the range of the node stack by walking down until we pass the
1558 // root DFS number.
1559 auto RefSCCNodes = node_stack_range(
1560 PendingRefSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +00001561 find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1562 return N->DFSNumber < RootDFSNumber;
1563 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +00001564 // Form a new RefSCC out of these nodes and then clear them off our pending
1565 // stack.
1566 RefSCC *NewRC = createRefSCC(*this);
1567 buildSCCs(*NewRC, RefSCCNodes);
1568 connectRefSCC(*NewRC);
1569 PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1570 PendingRefSCCStack.end());
1571
1572 // We return the new node here. This essentially suspends the DFS walk
1573 // until another RefSCC is requested.
1574 return NewRC;
Chandler Carruth91dcf0f2014-04-24 21:19:30 +00001575 }
Chandler Carruth18eadd922014-04-18 10:50:32 +00001576}
1577
Chandler Carruthb4faf132016-03-11 10:22:49 +00001578char LazyCallGraphAnalysis::PassID;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001579
Chandler Carruthbf71a342014-02-06 04:37:03 +00001580LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
1581
Chandler Carruthe5944d92016-02-17 00:18:16 +00001582static void printNode(raw_ostream &OS, LazyCallGraph::Node &N) {
Chandler Carrutha4499e92016-02-02 03:57:13 +00001583 OS << " Edges in function: " << N.getFunction().getName() << "\n";
1584 for (const LazyCallGraph::Edge &E : N)
1585 OS << " " << (E.isCall() ? "call" : "ref ") << " -> "
1586 << E.getFunction().getName() << "\n";
Chandler Carruth11f50322015-01-14 00:27:45 +00001587
1588 OS << "\n";
1589}
1590
Chandler Carruthe5944d92016-02-17 00:18:16 +00001591static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) {
1592 ptrdiff_t Size = std::distance(C.begin(), C.end());
1593 OS << " SCC with " << Size << " functions:\n";
Chandler Carruth11f50322015-01-14 00:27:45 +00001594
Chandler Carruthe5944d92016-02-17 00:18:16 +00001595 for (LazyCallGraph::Node &N : C)
1596 OS << " " << N.getFunction().getName() << "\n";
1597}
1598
1599static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) {
1600 ptrdiff_t Size = std::distance(C.begin(), C.end());
1601 OS << " RefSCC with " << Size << " call SCCs:\n";
1602
1603 for (LazyCallGraph::SCC &InnerC : C)
1604 printSCC(OS, InnerC);
Chandler Carruth11f50322015-01-14 00:27:45 +00001605
1606 OS << "\n";
1607}
1608
Chandler Carruthd174ce42015-01-05 02:47:05 +00001609PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M,
Chandler Carruthb47f8012016-03-11 11:05:24 +00001610 ModuleAnalysisManager &AM) {
1611 LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
Chandler Carruth11f50322015-01-14 00:27:45 +00001612
1613 OS << "Printing the call graph for module: " << M.getModuleIdentifier()
1614 << "\n\n";
1615
Chandler Carruthe5944d92016-02-17 00:18:16 +00001616 for (Function &F : M)
1617 printNode(OS, G.get(F));
Chandler Carruth11f50322015-01-14 00:27:45 +00001618
Chandler Carruthe5944d92016-02-17 00:18:16 +00001619 for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs())
1620 printRefSCC(OS, C);
Chandler Carruth18eadd922014-04-18 10:50:32 +00001621
Chandler Carruthbf71a342014-02-06 04:37:03 +00001622 return PreservedAnalyses::all();
Chandler Carruthbf71a342014-02-06 04:37:03 +00001623}
Sean Silva7cb30662016-06-18 09:17:32 +00001624
1625LazyCallGraphDOTPrinterPass::LazyCallGraphDOTPrinterPass(raw_ostream &OS)
1626 : OS(OS) {}
1627
1628static void printNodeDOT(raw_ostream &OS, LazyCallGraph::Node &N) {
1629 std::string Name = "\"" + DOT::EscapeString(N.getFunction().getName()) + "\"";
1630
1631 for (const LazyCallGraph::Edge &E : N) {
1632 OS << " " << Name << " -> \""
1633 << DOT::EscapeString(E.getFunction().getName()) << "\"";
1634 if (!E.isCall()) // It is a ref edge.
1635 OS << " [style=dashed,label=\"ref\"]";
1636 OS << ";\n";
1637 }
1638
1639 OS << "\n";
1640}
1641
1642PreservedAnalyses LazyCallGraphDOTPrinterPass::run(Module &M,
1643 ModuleAnalysisManager &AM) {
1644 LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
1645
1646 OS << "digraph \"" << DOT::EscapeString(M.getModuleIdentifier()) << "\" {\n";
1647
1648 for (Function &F : M)
1649 printNodeDOT(OS, G.get(F));
1650
1651 OS << "}\n";
1652
1653 return PreservedAnalyses::all();
1654}