blob: 718c86410cc6b861e076d7d46bd6886ce9c71130 [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"
Chandler Carruthbf71a342014-02-06 04:37:03 +000017#include "llvm/Support/raw_ostream.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 Carruthe5944d92016-02-17 00:18:16 +000043static void findReferences(SmallVectorImpl<Constant *> &Worklist,
44 SmallPtrSetImpl<Constant *> &Visited,
45 SmallVectorImpl<LazyCallGraph::Edge> &Edges,
46 DenseMap<Function *, int> &EdgeIndexMap) {
Chandler Carruthbf71a342014-02-06 04:37:03 +000047 while (!Worklist.empty()) {
48 Constant *C = Worklist.pop_back_val();
49
50 if (Function *F = dyn_cast<Function>(C)) {
Chandler Carrutha4499e92016-02-02 03:57:13 +000051 addEdge(Edges, EdgeIndexMap, *F, LazyCallGraph::Edge::Ref);
Chandler Carruthbf71a342014-02-06 04:37:03 +000052 continue;
53 }
54
Chandler Carruth1583e992014-03-03 10:42:58 +000055 for (Value *Op : C->operand_values())
David Blaikie70573dc2014-11-19 07:49:26 +000056 if (Visited.insert(cast<Constant>(Op)).second)
Chandler Carruth1583e992014-03-03 10:42:58 +000057 Worklist.push_back(cast<Constant>(Op));
Chandler Carruthbf71a342014-02-06 04:37:03 +000058 }
59}
60
Chandler Carruth18eadd922014-04-18 10:50:32 +000061LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F)
62 : G(&G), F(F), DFSNumber(0), LowLink(0) {
Chandler Carruth99b756d2014-04-21 05:04:24 +000063 DEBUG(dbgs() << " Adding functions called by '" << F.getName()
64 << "' to the graph.\n");
65
Chandler Carruthbf71a342014-02-06 04:37:03 +000066 SmallVector<Constant *, 16> Worklist;
Chandler Carrutha4499e92016-02-02 03:57:13 +000067 SmallPtrSet<Function *, 4> Callees;
Chandler Carruthbf71a342014-02-06 04:37:03 +000068 SmallPtrSet<Constant *, 16> Visited;
Chandler Carrutha4499e92016-02-02 03:57:13 +000069
70 // Find all the potential call graph edges in this function. We track both
71 // actual call edges and indirect references to functions. The direct calls
72 // are trivially added, but to accumulate the latter we walk the instructions
73 // and add every operand which is a constant to the worklist to process
74 // afterward.
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +000075 for (BasicBlock &BB : F)
Chandler Carrutha4499e92016-02-02 03:57:13 +000076 for (Instruction &I : BB) {
77 if (auto CS = CallSite(&I))
78 if (Function *Callee = CS.getCalledFunction())
79 if (Callees.insert(Callee).second) {
80 Visited.insert(Callee);
81 addEdge(Edges, EdgeIndexMap, *Callee, LazyCallGraph::Edge::Call);
82 }
83
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +000084 for (Value *Op : I.operand_values())
Chandler Carruth1583e992014-03-03 10:42:58 +000085 if (Constant *C = dyn_cast<Constant>(Op))
David Blaikie70573dc2014-11-19 07:49:26 +000086 if (Visited.insert(C).second)
Chandler Carruthbf71a342014-02-06 04:37:03 +000087 Worklist.push_back(C);
Chandler Carrutha4499e92016-02-02 03:57:13 +000088 }
Chandler Carruthbf71a342014-02-06 04:37:03 +000089
90 // We've collected all the constant (and thus potentially function or
91 // function containing) operands to all of the instructions in the function.
92 // Process them (recursively) collecting every function found.
Chandler Carrutha4499e92016-02-02 03:57:13 +000093 findReferences(Worklist, Visited, Edges, EdgeIndexMap);
Chandler Carruthbf71a342014-02-06 04:37:03 +000094}
95
Chandler Carruthe5944d92016-02-17 00:18:16 +000096void LazyCallGraph::Node::insertEdgeInternal(Function &Target, Edge::Kind EK) {
97 if (Node *N = G->lookup(Target))
Chandler Carrutha4499e92016-02-02 03:57:13 +000098 return insertEdgeInternal(*N, EK);
Chandler Carruth5217c942014-04-30 10:48:36 +000099
Chandler Carruthe5944d92016-02-17 00:18:16 +0000100 EdgeIndexMap.insert({&Target, Edges.size()});
101 Edges.emplace_back(Target, EK);
Chandler Carruth5217c942014-04-30 10:48:36 +0000102}
103
Chandler Carruthe5944d92016-02-17 00:18:16 +0000104void LazyCallGraph::Node::insertEdgeInternal(Node &TargetN, Edge::Kind EK) {
105 EdgeIndexMap.insert({&TargetN.getFunction(), Edges.size()});
106 Edges.emplace_back(TargetN, EK);
Chandler Carruthc00a7ff2014-04-28 11:10:23 +0000107}
108
Chandler Carruthe5944d92016-02-17 00:18:16 +0000109void LazyCallGraph::Node::setEdgeKind(Function &TargetF, Edge::Kind EK) {
110 Edges[EdgeIndexMap.find(&TargetF)->second].setKind(EK);
111}
112
113void LazyCallGraph::Node::removeEdgeInternal(Function &Target) {
114 auto IndexMapI = EdgeIndexMap.find(&Target);
Chandler Carrutha4499e92016-02-02 03:57:13 +0000115 assert(IndexMapI != EdgeIndexMap.end() &&
Chandler Carruthe5944d92016-02-17 00:18:16 +0000116 "Target not in the edge set for this caller?");
Chandler Carruthaa839b22014-04-27 01:59:50 +0000117
Chandler Carrutha4499e92016-02-02 03:57:13 +0000118 Edges[IndexMapI->second] = Edge();
119 EdgeIndexMap.erase(IndexMapI);
Chandler Carruthaa839b22014-04-27 01:59:50 +0000120}
121
Chandler Carruth2174f442014-04-18 20:44:16 +0000122LazyCallGraph::LazyCallGraph(Module &M) : NextDFSNumber(0) {
Chandler Carruth99b756d2014-04-21 05:04:24 +0000123 DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier()
124 << "\n");
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000125 for (Function &F : M)
126 if (!F.isDeclaration() && !F.hasLocalLinkage())
Chandler Carruthe5944d92016-02-17 00:18:16 +0000127 if (EntryIndexMap.insert({&F, EntryEdges.size()}).second) {
Chandler Carruth99b756d2014-04-21 05:04:24 +0000128 DEBUG(dbgs() << " Adding '" << F.getName()
129 << "' to entry set of the graph.\n");
Chandler Carrutha4499e92016-02-02 03:57:13 +0000130 EntryEdges.emplace_back(F, Edge::Ref);
Chandler Carruth99b756d2014-04-21 05:04:24 +0000131 }
Chandler Carruthbf71a342014-02-06 04:37:03 +0000132
133 // Now add entry nodes for functions reachable via initializers to globals.
134 SmallVector<Constant *, 16> Worklist;
135 SmallPtrSet<Constant *, 16> Visited;
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000136 for (GlobalVariable &GV : M.globals())
137 if (GV.hasInitializer())
David Blaikie70573dc2014-11-19 07:49:26 +0000138 if (Visited.insert(GV.getInitializer()).second)
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000139 Worklist.push_back(GV.getInitializer());
Chandler Carruthbf71a342014-02-06 04:37:03 +0000140
Chandler Carruth99b756d2014-04-21 05:04:24 +0000141 DEBUG(dbgs() << " Adding functions referenced by global initializers to the "
142 "entry set.\n");
Chandler Carrutha4499e92016-02-02 03:57:13 +0000143 findReferences(Worklist, Visited, EntryEdges, EntryIndexMap);
Chandler Carruth18eadd922014-04-18 10:50:32 +0000144
Chandler Carrutha4499e92016-02-02 03:57:13 +0000145 for (const Edge &E : EntryEdges)
Chandler Carruthe5944d92016-02-17 00:18:16 +0000146 RefSCCEntryNodes.push_back(&E.getFunction());
Chandler Carruthbf71a342014-02-06 04:37:03 +0000147}
148
Chandler Carruthbf71a342014-02-06 04:37:03 +0000149LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
Chandler Carruth2174f442014-04-18 20:44:16 +0000150 : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)),
Chandler Carrutha4499e92016-02-02 03:57:13 +0000151 EntryEdges(std::move(G.EntryEdges)),
Chandler Carruth0b623ba2014-04-23 04:00:17 +0000152 EntryIndexMap(std::move(G.EntryIndexMap)), SCCBPA(std::move(G.SCCBPA)),
Chandler Carruthe5944d92016-02-17 00:18:16 +0000153 SCCMap(std::move(G.SCCMap)), LeafRefSCCs(std::move(G.LeafRefSCCs)),
Chandler Carruth18eadd922014-04-18 10:50:32 +0000154 DFSStack(std::move(G.DFSStack)),
Chandler Carruthe5944d92016-02-17 00:18:16 +0000155 RefSCCEntryNodes(std::move(G.RefSCCEntryNodes)),
Chandler Carruth2174f442014-04-18 20:44:16 +0000156 NextDFSNumber(G.NextDFSNumber) {
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000157 updateGraphPtrs();
158}
159
160LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) {
161 BPA = std::move(G.BPA);
Chandler Carruth2174f442014-04-18 20:44:16 +0000162 NodeMap = std::move(G.NodeMap);
Chandler Carrutha4499e92016-02-02 03:57:13 +0000163 EntryEdges = std::move(G.EntryEdges);
Chandler Carruth0b623ba2014-04-23 04:00:17 +0000164 EntryIndexMap = std::move(G.EntryIndexMap);
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000165 SCCBPA = std::move(G.SCCBPA);
166 SCCMap = std::move(G.SCCMap);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000167 LeafRefSCCs = std::move(G.LeafRefSCCs);
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000168 DFSStack = std::move(G.DFSStack);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000169 RefSCCEntryNodes = std::move(G.RefSCCEntryNodes);
Chandler Carruth2174f442014-04-18 20:44:16 +0000170 NextDFSNumber = G.NextDFSNumber;
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000171 updateGraphPtrs();
172 return *this;
173}
174
Chandler Carruthe5944d92016-02-17 00:18:16 +0000175#ifndef NDEBUG
176void LazyCallGraph::SCC::verify() {
177 assert(OuterRefSCC && "Can't have a null RefSCC!");
178 assert(!Nodes.empty() && "Can't have an empty SCC!");
Chandler Carruth8f92d6d2014-04-26 01:03:46 +0000179
Chandler Carruthe5944d92016-02-17 00:18:16 +0000180 for (Node *N : Nodes) {
181 assert(N && "Can't have a null node!");
182 assert(OuterRefSCC->G->lookupSCC(*N) == this &&
183 "Node does not map to this SCC!");
184 assert(N->DFSNumber == -1 &&
185 "Must set DFS numbers to -1 when adding a node to an SCC!");
186 assert(N->LowLink == -1 &&
187 "Must set low link to -1 when adding a node to an SCC!");
188 for (Edge &E : *N)
189 assert(E.getNode() && "Can't have an edge to a raw function!");
190 }
191}
192#endif
193
194LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {}
195
196#ifndef NDEBUG
197void LazyCallGraph::RefSCC::verify() {
198 assert(G && "Can't have a null graph!");
199 assert(!SCCs.empty() && "Can't have an empty SCC!");
200
201 // Verify basic properties of the SCCs.
202 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!");
207 }
208
209 // Check that our indices map correctly.
210 for (auto &SCCIndexPair : SCCIndices) {
211 SCC *C = SCCIndexPair.first;
212 int i = SCCIndexPair.second;
213 assert(C && "Can't have a null SCC in the indices!");
214 assert(SCCs[i] == C && "Index doesn't point to SCC!");
215 }
216
217 // Check that the SCCs are in fact in post-order.
218 for (int i = 0, Size = SCCs.size(); i < Size; ++i) {
219 SCC &SourceSCC = *SCCs[i];
220 for (Node &N : SourceSCC)
221 for (Edge &E : N) {
222 if (!E.isCall())
223 continue;
224 SCC &TargetSCC = *G->lookupSCC(*E.getNode());
225 if (&TargetSCC.getOuterRefSCC() == this) {
226 assert(SCCIndices.find(&TargetSCC)->second <= i &&
227 "Edge between SCCs violates post-order relationship.");
228 continue;
229 }
230 assert(TargetSCC.getOuterRefSCC().Parents.count(this) &&
231 "Edge to a RefSCC missing us in its parent set.");
232 }
233 }
234}
235#endif
236
237bool LazyCallGraph::RefSCC::isDescendantOf(const RefSCC &C) const {
Chandler Carruth4b096742014-05-01 12:12:42 +0000238 // Walk up the parents of this SCC and verify that we eventually find C.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000239 SmallVector<const RefSCC *, 4> AncestorWorklist;
Chandler Carruth4b096742014-05-01 12:12:42 +0000240 AncestorWorklist.push_back(this);
241 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000242 const RefSCC *AncestorC = AncestorWorklist.pop_back_val();
Chandler Carruth4b096742014-05-01 12:12:42 +0000243 if (AncestorC->isChildOf(C))
244 return true;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000245 for (const RefSCC *ParentC : AncestorC->Parents)
Chandler Carruth4b096742014-05-01 12:12:42 +0000246 AncestorWorklist.push_back(ParentC);
247 } while (!AncestorWorklist.empty());
248
249 return false;
250}
251
Chandler Carruthe5944d92016-02-17 00:18:16 +0000252SmallVector<LazyCallGraph::SCC *, 1>
253LazyCallGraph::RefSCC::switchInternalEdgeToCall(Node &SourceN, Node &TargetN) {
254 assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
Chandler Carruth5217c942014-04-30 10:48:36 +0000255
Chandler Carruthe5944d92016-02-17 00:18:16 +0000256 SmallVector<SCC *, 1> DeletedSCCs;
Chandler Carruth5217c942014-04-30 10:48:36 +0000257
Chandler Carruthe5944d92016-02-17 00:18:16 +0000258 SCC &SourceSCC = *G->lookupSCC(SourceN);
259 SCC &TargetSCC = *G->lookupSCC(TargetN);
260
261 // If the two nodes are already part of the same SCC, we're also done as
262 // we've just added more connectivity.
263 if (&SourceSCC == &TargetSCC) {
264 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
265#ifndef NDEBUG
266 // Check that the RefSCC is still valid.
267 verify();
268#endif
269 return DeletedSCCs;
270 }
271
272 // At this point we leverage the postorder list of SCCs to detect when the
273 // insertion of an edge changes the SCC structure in any way.
274 //
275 // First and foremost, we can eliminate the need for any changes when the
276 // edge is toward the beginning of the postorder sequence because all edges
277 // flow in that direction already. Thus adding a new one cannot form a cycle.
278 int SourceIdx = SCCIndices[&SourceSCC];
279 int TargetIdx = SCCIndices[&TargetSCC];
280 if (TargetIdx < SourceIdx) {
281 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
282#ifndef NDEBUG
283 // Check that the RefSCC is still valid.
284 verify();
285#endif
286 return DeletedSCCs;
287 }
288
289 // When we do have an edge from an earlier SCC to a later SCC in the
290 // postorder sequence, all of the SCCs which may be impacted are in the
291 // closed range of those two within the postorder sequence. The algorithm to
292 // restore the state is as follows:
293 //
294 // 1) Starting from the source SCC, construct a set of SCCs which reach the
295 // source SCC consisting of just the source SCC. Then scan toward the
296 // target SCC in postorder and for each SCC, if it has an edge to an SCC
297 // in the set, add it to the set. Otherwise, the source SCC is not
298 // a successor, move it in the postorder sequence to immediately before
299 // the source SCC, shifting the source SCC and all SCCs in the set one
300 // position toward the target SCC. Stop scanning after processing the
301 // target SCC.
302 // 2) If the source SCC is now past the target SCC in the postorder sequence,
303 // and thus the new edge will flow toward the start, we are done.
304 // 3) Otherwise, starting from the target SCC, walk all edges which reach an
305 // SCC between the source and the target, and add them to the set of
306 // connected SCCs, then recurse through them. Once a complete set of the
307 // SCCs the target connects to is known, hoist the remaining SCCs between
308 // the source and the target to be above the target. Note that there is no
309 // need to process the source SCC, it is already known to connect.
310 // 4) At this point, all of the SCCs in the closed range between the source
311 // SCC and the target SCC in the postorder sequence are connected,
312 // including the target SCC and the source SCC. Inserting the edge from
313 // the source SCC to the target SCC will form a cycle out of precisely
314 // these SCCs. Thus we can merge all of the SCCs in this closed range into
315 // a single SCC.
316 //
317 // This process has various important properties:
318 // - Only mutates the SCCs when adding the edge actually changes the SCC
319 // structure.
320 // - Never mutates SCCs which are unaffected by the change.
321 // - Updates the postorder sequence to correctly satisfy the postorder
322 // constraint after the edge is inserted.
323 // - Only reorders SCCs in the closed postorder sequence from the source to
324 // the target, so easy to bound how much has changed even in the ordering.
325 // - Big-O is the number of edges in the closed postorder range of SCCs from
326 // source to target.
327
328 assert(SourceIdx < TargetIdx && "Cannot have equal indices here!");
329 SmallPtrSet<SCC *, 4> ConnectedSet;
330
331 // Compute the SCCs which (transitively) reach the source.
332 ConnectedSet.insert(&SourceSCC);
333 auto IsConnected = [&](SCC &C) {
334 for (Node &N : C)
335 for (Edge &E : N.calls()) {
336 assert(E.getNode() && "Must have formed a node within an SCC!");
337 if (ConnectedSet.count(G->lookupSCC(*E.getNode())))
338 return true;
339 }
340
341 return false;
342 };
343
344 for (SCC *C :
345 make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1))
346 if (IsConnected(*C))
347 ConnectedSet.insert(C);
348
349 // Partition the SCCs in this part of the port-order sequence so only SCCs
350 // connecting to the source remain between it and the target. This is
351 // a benign partition as it preserves postorder.
352 auto SourceI = std::stable_partition(
353 SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx + 1,
354 [&ConnectedSet](SCC *C) { return !ConnectedSet.count(C); });
355 for (int i = SourceIdx, e = TargetIdx + 1; i < e; ++i)
356 SCCIndices.find(SCCs[i])->second = i;
357
358 // If the target doesn't connect to the source, then we've corrected the
359 // post-order and there are no cycles formed.
360 if (!ConnectedSet.count(&TargetSCC)) {
361 assert(SourceI > (SCCs.begin() + SourceIdx) &&
362 "Must have moved the source to fix the post-order.");
363 assert(*std::prev(SourceI) == &TargetSCC &&
364 "Last SCC to move should have bene the target.");
365 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
366#ifndef NDEBUG
367 verify();
368#endif
369 return DeletedSCCs;
370 }
371
372 assert(SCCs[TargetIdx] == &TargetSCC &&
373 "Should not have moved target if connected!");
374 SourceIdx = SourceI - SCCs.begin();
375
376#ifndef NDEBUG
377 // Check that the RefSCC is still valid.
378 verify();
379#endif
380
381 // See whether there are any remaining intervening SCCs between the source
382 // and target. If so we need to make sure they all are reachable form the
383 // target.
384 if (SourceIdx + 1 < TargetIdx) {
385 // Use a normal worklist to find which SCCs the target connects to. We still
386 // bound the search based on the range in the postorder list we care about,
387 // but because this is forward connectivity we just "recurse" through the
388 // edges.
389 ConnectedSet.clear();
390 ConnectedSet.insert(&TargetSCC);
391 SmallVector<SCC *, 4> Worklist;
392 Worklist.push_back(&TargetSCC);
393 do {
394 SCC &C = *Worklist.pop_back_val();
395 for (Node &N : C)
396 for (Edge &E : N) {
397 assert(E.getNode() && "Must have formed a node within an SCC!");
398 if (!E.isCall())
399 continue;
400 SCC &EdgeC = *G->lookupSCC(*E.getNode());
401 if (&EdgeC.getOuterRefSCC() != this)
402 // Not in this RefSCC...
403 continue;
404 if (SCCIndices.find(&EdgeC)->second <= SourceIdx)
405 // Not in the postorder sequence between source and target.
406 continue;
407
408 if (ConnectedSet.insert(&EdgeC).second)
409 Worklist.push_back(&EdgeC);
410 }
411 } while (!Worklist.empty());
412
413 // Partition SCCs so that only SCCs reached from the target remain between
414 // the source and the target. This preserves postorder.
415 auto TargetI = std::stable_partition(
416 SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1,
417 [&ConnectedSet](SCC *C) { return ConnectedSet.count(C); });
418 for (int i = SourceIdx + 1, e = TargetIdx + 1; i < e; ++i)
419 SCCIndices.find(SCCs[i])->second = i;
420 TargetIdx = std::prev(TargetI) - SCCs.begin();
421 assert(SCCs[TargetIdx] == &TargetSCC &&
422 "Should always end with the target!");
423
424#ifndef NDEBUG
425 // Check that the RefSCC is still valid.
426 verify();
427#endif
428 }
429
430 // At this point, we know that connecting source to target forms a cycle
431 // because target connects back to source, and we know that all of the SCCs
432 // between the source and target in the postorder sequence participate in that
433 // cycle. This means that we need to merge all of these SCCs into a single
434 // result SCC.
435 //
436 // NB: We merge into the target because all of these functions were already
437 // reachable from the target, meaning any SCC-wide properties deduced about it
438 // other than the set of functions within it will not have changed.
439 auto MergeRange =
440 make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx);
441 for (SCC *C : MergeRange) {
442 assert(C != &TargetSCC &&
443 "We merge *into* the target and shouldn't process it here!");
444 SCCIndices.erase(C);
445 TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end());
446 for (Node *N : C->Nodes)
447 G->SCCMap[N] = &TargetSCC;
448 C->clear();
449 DeletedSCCs.push_back(C);
450 }
451
452 // Erase the merged SCCs from the list and update the indices of the
453 // remaining SCCs.
454 int IndexOffset = MergeRange.end() - MergeRange.begin();
455 auto EraseEnd = SCCs.erase(MergeRange.begin(), MergeRange.end());
456 for (SCC *C : make_range(EraseEnd, SCCs.end()))
457 SCCIndices[C] -= IndexOffset;
458
459 // Now that the SCC structure is finalized, flip the kind to call.
460 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
461
462#ifndef NDEBUG
463 // And we're done! Verify in debug builds that the RefSCC is coherent.
464 verify();
465#endif
466 return DeletedSCCs;
Chandler Carruth5217c942014-04-30 10:48:36 +0000467}
468
Chandler Carruthe5944d92016-02-17 00:18:16 +0000469void LazyCallGraph::RefSCC::switchInternalEdgeToRef(Node &SourceN,
470 Node &TargetN) {
471 assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
472
473 SCC &SourceSCC = *G->lookupSCC(SourceN);
474 SCC &TargetSCC = *G->lookupSCC(TargetN);
475
476 assert(&SourceSCC.getOuterRefSCC() == this &&
477 "Source must be in this RefSCC.");
478 assert(&TargetSCC.getOuterRefSCC() == this &&
479 "Target must be in this RefSCC.");
480
481 // Set the edge kind.
482 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
483
484 // If this call edge is just connecting two separate SCCs within this RefSCC,
485 // there is nothing to do.
486 if (&SourceSCC != &TargetSCC) {
487#ifndef NDEBUG
488 // Check that the RefSCC is still valid.
489 verify();
490#endif
491 return;
492 }
493
494 // Otherwise we are removing a call edge from a single SCC. This may break
495 // the cycle. In order to compute the new set of SCCs, we need to do a small
496 // DFS over the nodes within the SCC to form any sub-cycles that remain as
497 // distinct SCCs and compute a postorder over the resulting SCCs.
498 //
499 // However, we specially handle the target node. The target node is known to
500 // reach all other nodes in the original SCC by definition. This means that
501 // we want the old SCC to be replaced with an SCC contaning that node as it
502 // will be the root of whatever SCC DAG results from the DFS. Assumptions
503 // about an SCC such as the set of functions called will continue to hold,
504 // etc.
505
506 SCC &OldSCC = TargetSCC;
507 SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
508 SmallVector<Node *, 16> PendingSCCStack;
509 SmallVector<SCC *, 4> NewSCCs;
510
511 // Prepare the nodes for a fresh DFS.
512 SmallVector<Node *, 16> Worklist;
513 Worklist.swap(OldSCC.Nodes);
514 for (Node *N : Worklist) {
515 N->DFSNumber = N->LowLink = 0;
516 G->SCCMap.erase(N);
517 }
518
519 // Force the target node to be in the old SCC. This also enables us to take
520 // a very significant short-cut in the standard Tarjan walk to re-form SCCs
521 // below: whenever we build an edge that reaches the target node, we know
522 // that the target node eventually connects back to all other nodes in our
523 // walk. As a consequence, we can detect and handle participants in that
524 // cycle without walking all the edges that form this connection, and instead
525 // by relying on the fundamental guarantee coming into this operation (all
526 // nodes are reachable from the target due to previously forming an SCC).
527 TargetN.DFSNumber = TargetN.LowLink = -1;
528 OldSCC.Nodes.push_back(&TargetN);
529 G->SCCMap[&TargetN] = &OldSCC;
530
531 // Scan down the stack and DFS across the call edges.
532 for (Node *RootN : Worklist) {
533 assert(DFSStack.empty() &&
534 "Cannot begin a new root with a non-empty DFS stack!");
535 assert(PendingSCCStack.empty() &&
536 "Cannot begin a new root with pending nodes for an SCC!");
537
538 // Skip any nodes we've already reached in the DFS.
539 if (RootN->DFSNumber != 0) {
540 assert(RootN->DFSNumber == -1 &&
541 "Shouldn't have any mid-DFS root nodes!");
542 continue;
543 }
544
545 RootN->DFSNumber = RootN->LowLink = 1;
546 int NextDFSNumber = 2;
547
548 DFSStack.push_back({RootN, RootN->call_begin()});
549 do {
550 Node *N;
551 call_edge_iterator I;
552 std::tie(N, I) = DFSStack.pop_back_val();
553 auto E = N->call_end();
554 while (I != E) {
555 Node &ChildN = *I->getNode();
556 if (ChildN.DFSNumber == 0) {
557 // We haven't yet visited this child, so descend, pushing the current
558 // node onto the stack.
559 DFSStack.push_back({N, I});
560
561 assert(!G->SCCMap.count(&ChildN) &&
562 "Found a node with 0 DFS number but already in an SCC!");
563 ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
564 N = &ChildN;
565 I = N->call_begin();
566 E = N->call_end();
567 continue;
568 }
569
570 // Check for the child already being part of some component.
571 if (ChildN.DFSNumber == -1) {
572 if (G->lookupSCC(ChildN) == &OldSCC) {
573 // If the child is part of the old SCC, we know that it can reach
574 // every other node, so we have formed a cycle. Pull the entire DFS
575 // and pending stacks into it. See the comment above about setting
576 // up the old SCC for why we do this.
577 int OldSize = OldSCC.size();
578 OldSCC.Nodes.push_back(N);
579 OldSCC.Nodes.append(PendingSCCStack.begin(), PendingSCCStack.end());
580 PendingSCCStack.clear();
581 while (!DFSStack.empty())
582 OldSCC.Nodes.push_back(DFSStack.pop_back_val().first);
583 for (Node &N : make_range(OldSCC.begin() + OldSize, OldSCC.end())) {
584 N.DFSNumber = N.LowLink = -1;
585 G->SCCMap[&N] = &OldSCC;
586 }
587 N = nullptr;
588 break;
589 }
590
591 // If the child has already been added to some child component, it
592 // couldn't impact the low-link of this parent because it isn't
593 // connected, and thus its low-link isn't relevant so skip it.
594 ++I;
595 continue;
596 }
597
598 // Track the lowest linked child as the lowest link for this node.
599 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
600 if (ChildN.LowLink < N->LowLink)
601 N->LowLink = ChildN.LowLink;
602
603 // Move to the next edge.
604 ++I;
605 }
606 if (!N)
607 // Cleared the DFS early, start another round.
608 break;
609
610 // We've finished processing N and its descendents, put it on our pending
611 // SCC stack to eventually get merged into an SCC of nodes.
612 PendingSCCStack.push_back(N);
613
614 // If this node is linked to some lower entry, continue walking up the
615 // stack.
616 if (N->LowLink != N->DFSNumber)
617 continue;
618
619 // Otherwise, we've completed an SCC. Append it to our post order list of
620 // SCCs.
621 int RootDFSNumber = N->DFSNumber;
622 // Find the range of the node stack by walking down until we pass the
623 // root DFS number.
624 auto SCCNodes = make_range(
625 PendingSCCStack.rbegin(),
626 std::find_if(PendingSCCStack.rbegin(), PendingSCCStack.rend(),
627 [RootDFSNumber](Node *N) {
628 return N->DFSNumber < RootDFSNumber;
629 }));
630
631 // Form a new SCC out of these nodes and then clear them off our pending
632 // stack.
633 NewSCCs.push_back(G->createSCC(*this, SCCNodes));
634 for (Node &N : *NewSCCs.back()) {
635 N.DFSNumber = N.LowLink = -1;
636 G->SCCMap[&N] = NewSCCs.back();
637 }
638 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
639 } while (!DFSStack.empty());
640 }
641
642 // Insert the remaining SCCs before the old one. The old SCC can reach all
643 // other SCCs we form because it contains the target node of the removed edge
644 // of the old SCC. This means that we will have edges into all of the new
645 // SCCs, which means the old one must come last for postorder.
646 int OldIdx = SCCIndices[&OldSCC];
647 SCCs.insert(SCCs.begin() + OldIdx, NewSCCs.begin(), NewSCCs.end());
648
649 // Update the mapping from SCC* to index to use the new SCC*s, and remove the
650 // old SCC from the mapping.
651 for (int Idx = OldIdx, Size = SCCs.size(); Idx < Size; ++Idx)
652 SCCIndices[SCCs[Idx]] = Idx;
653
654#ifndef NDEBUG
655 // We're done. Check the validity on our way out.
656 verify();
657#endif
658}
659
660void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN,
661 Node &TargetN) {
662 assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
663
664 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
665 assert(G->lookupRefSCC(TargetN) != this &&
666 "Target must not be in this RefSCC.");
667 assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
668 "Target must be a descendant of the Source.");
669
670 // Edges between RefSCCs are the same regardless of call or ref, so we can
671 // just flip the edge here.
672 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
673
674#ifndef NDEBUG
675 // Check that the RefSCC is still valid.
676 verify();
677#endif
678}
679
680void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN,
681 Node &TargetN) {
682 assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
683
684 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
685 assert(G->lookupRefSCC(TargetN) != this &&
686 "Target must not be in this RefSCC.");
687 assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
688 "Target must be a descendant of the Source.");
689
690 // Edges between RefSCCs are the same regardless of call or ref, so we can
691 // just flip the edge here.
692 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
693
694#ifndef NDEBUG
695 // Check that the RefSCC is still valid.
696 verify();
697#endif
698}
699
700void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN,
701 Node &TargetN) {
702 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
703 assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
704
705 SourceN.insertEdgeInternal(TargetN, Edge::Ref);
706
707#ifndef NDEBUG
708 // Check that the RefSCC is still valid.
709 verify();
710#endif
711}
712
713void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN,
714 Edge::Kind EK) {
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000715 // First insert it into the caller.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000716 SourceN.insertEdgeInternal(TargetN, EK);
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000717
Chandler Carruthe5944d92016-02-17 00:18:16 +0000718 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000719
Chandler Carruthe5944d92016-02-17 00:18:16 +0000720 RefSCC &TargetC = *G->lookupRefSCC(TargetN);
721 assert(&TargetC != this && "Target must not be in this RefSCC.");
722 assert(TargetC.isDescendantOf(*this) &&
723 "Target must be a descendant of the Source.");
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000724
Chandler Carruth91539112015-12-28 01:54:20 +0000725 // The only change required is to add this SCC to the parent set of the
726 // callee.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000727 TargetC.Parents.insert(this);
728
729#ifndef NDEBUG
730 // Check that the RefSCC is still valid.
731 verify();
732#endif
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000733}
734
Chandler Carruthe5944d92016-02-17 00:18:16 +0000735SmallVector<LazyCallGraph::RefSCC *, 1>
736LazyCallGraph::RefSCC::insertIncomingRefEdge(Node &SourceN, Node &TargetN) {
737 assert(G->lookupRefSCC(TargetN) == this && "Target must be in this SCC.");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000738
Chandler Carruthe5944d92016-02-17 00:18:16 +0000739 // We store the RefSCCs found to be connected in postorder so that we can use
740 // that when merging. We also return this to the caller to allow them to
741 // invalidate information pertaining to these RefSCCs.
742 SmallVector<RefSCC *, 1> Connected;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000743
Chandler Carruthe5944d92016-02-17 00:18:16 +0000744 RefSCC &SourceC = *G->lookupRefSCC(SourceN);
745 assert(&SourceC != this && "Source must not be in this SCC.");
746 assert(SourceC.isDescendantOf(*this) &&
747 "Source must be a descendant of the Target.");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000748
749 // The algorithm we use for merging SCCs based on the cycle introduced here
Chandler Carruthe5944d92016-02-17 00:18:16 +0000750 // is to walk the RefSCC inverted DAG formed by the parent sets. The inverse
751 // graph has the same cycle properties as the actual DAG of the RefSCCs, and
752 // when forming RefSCCs lazily by a DFS, the bottom of the graph won't exist
753 // in many cases which should prune the search space.
Chandler Carruth312dddf2014-05-04 09:38:32 +0000754 //
Chandler Carruthe5944d92016-02-17 00:18:16 +0000755 // FIXME: We can get this pruning behavior even after the incremental RefSCC
Chandler Carruth312dddf2014-05-04 09:38:32 +0000756 // formation by leaving behind (conservative) DFS numberings in the nodes,
757 // and pruning the search with them. These would need to be cleverly updated
758 // during the removal of intra-SCC edges, but could be preserved
759 // conservatively.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000760 //
761 // FIXME: This operation currently creates ordering stability problems
762 // because we don't use stably ordered containers for the parent SCCs.
Chandler Carruth312dddf2014-05-04 09:38:32 +0000763
Chandler Carruthe5944d92016-02-17 00:18:16 +0000764 // The set of RefSCCs that are connected to the parent, and thus will
Chandler Carruth312dddf2014-05-04 09:38:32 +0000765 // participate in the merged connected component.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000766 SmallPtrSet<RefSCC *, 8> ConnectedSet;
767 ConnectedSet.insert(this);
Chandler Carruth312dddf2014-05-04 09:38:32 +0000768
769 // We build up a DFS stack of the parents chains.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000770 SmallVector<std::pair<RefSCC *, parent_iterator>, 8> DFSStack;
771 SmallPtrSet<RefSCC *, 8> Visited;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000772 int ConnectedDepth = -1;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000773 DFSStack.push_back({&SourceC, SourceC.parent_begin()});
774 do {
775 auto DFSPair = DFSStack.pop_back_val();
776 RefSCC *C = DFSPair.first;
777 parent_iterator I = DFSPair.second;
778 auto E = C->parent_end();
779
Chandler Carruth312dddf2014-05-04 09:38:32 +0000780 while (I != E) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000781 RefSCC &Parent = *I++;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000782
783 // If we have already processed this parent SCC, skip it, and remember
784 // whether it was connected so we don't have to check the rest of the
785 // stack. This also handles when we reach a child of the 'this' SCC (the
786 // callee) which terminates the search.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000787 if (ConnectedSet.count(&Parent)) {
788 assert(ConnectedDepth < (int)DFSStack.size() &&
789 "Cannot have a connected depth greater than the DFS depth!");
790 ConnectedDepth = DFSStack.size();
Chandler Carruth312dddf2014-05-04 09:38:32 +0000791 continue;
792 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000793 if (Visited.count(&Parent))
Chandler Carruth312dddf2014-05-04 09:38:32 +0000794 continue;
795
796 // We fully explore the depth-first space, adding nodes to the connected
797 // set only as we pop them off, so "recurse" by rotating to the parent.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000798 DFSStack.push_back({C, I});
799 C = &Parent;
800 I = C->parent_begin();
801 E = C->parent_end();
Chandler Carruth312dddf2014-05-04 09:38:32 +0000802 }
803
804 // If we've found a connection anywhere below this point on the stack (and
805 // thus up the parent graph from the caller), the current node needs to be
806 // added to the connected set now that we've processed all of its parents.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000807 if ((int)DFSStack.size() == ConnectedDepth) {
Chandler Carruth312dddf2014-05-04 09:38:32 +0000808 --ConnectedDepth; // We're finished with this connection.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000809 bool Inserted = ConnectedSet.insert(C).second;
810 (void)Inserted;
811 assert(Inserted && "Cannot insert a refSCC multiple times!");
812 Connected.push_back(C);
Chandler Carruth312dddf2014-05-04 09:38:32 +0000813 } else {
814 // Otherwise remember that its parents don't ever connect.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000815 assert(ConnectedDepth < (int)DFSStack.size() &&
Chandler Carruth312dddf2014-05-04 09:38:32 +0000816 "Cannot have a connected depth greater than the DFS depth!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000817 Visited.insert(C);
Chandler Carruth312dddf2014-05-04 09:38:32 +0000818 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000819 } while (!DFSStack.empty());
Chandler Carruth312dddf2014-05-04 09:38:32 +0000820
821 // Now that we have identified all of the SCCs which need to be merged into
822 // a connected set with the inserted edge, merge all of them into this SCC.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000823 // We walk the newly connected RefSCCs in the reverse postorder of the parent
824 // DAG walk above and merge in each of their SCC postorder lists. This
825 // ensures a merged postorder SCC list.
826 SmallVector<SCC *, 16> MergedSCCs;
827 int SCCIndex = 0;
828 for (RefSCC *C : reverse(Connected)) {
829 assert(C != this &&
830 "This RefSCC should terminate the DFS without being reached.");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000831
Chandler Carruthe5944d92016-02-17 00:18:16 +0000832 // Merge the parents which aren't part of the merge into the our parents.
833 for (RefSCC *ParentC : C->Parents)
834 if (!ConnectedSet.count(ParentC))
835 Parents.insert(ParentC);
836 C->Parents.clear();
837
838 // Walk the inner SCCs to update their up-pointer and walk all the edges to
839 // update any parent sets.
840 // FIXME: We should try to find a way to avoid this (rather expensive) edge
841 // walk by updating the parent sets in some other manner.
842 for (SCC &InnerC : *C) {
843 InnerC.OuterRefSCC = this;
844 SCCIndices[&InnerC] = SCCIndex++;
845 for (Node &N : InnerC) {
846 G->SCCMap[&N] = &InnerC;
847 for (Edge &E : N) {
848 assert(E.getNode() &&
849 "Cannot have a null node within a visited SCC!");
850 RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
851 if (ConnectedSet.count(&ChildRC))
852 continue;
853 ChildRC.Parents.erase(C);
854 ChildRC.Parents.insert(this);
855 }
Chandler Carruth312dddf2014-05-04 09:38:32 +0000856 }
Chandler Carruth312dddf2014-05-04 09:38:32 +0000857 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000858
859 // Now merge in the SCCs. We can actually move here so try to reuse storage
860 // the first time through.
861 if (MergedSCCs.empty())
862 MergedSCCs = std::move(C->SCCs);
863 else
864 MergedSCCs.append(C->SCCs.begin(), C->SCCs.end());
865 C->SCCs.clear();
Chandler Carruth312dddf2014-05-04 09:38:32 +0000866 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000867
868 // Finally append our original SCCs to the merged list and move it into
869 // place.
870 for (SCC &InnerC : *this)
871 SCCIndices[&InnerC] = SCCIndex++;
872 MergedSCCs.append(SCCs.begin(), SCCs.end());
873 SCCs = std::move(MergedSCCs);
874
875 // At this point we have a merged RefSCC with a post-order SCCs list, just
876 // connect the nodes to form the new edge.
877 SourceN.insertEdgeInternal(TargetN, Edge::Ref);
878
879#ifndef NDEBUG
880 // Check that the RefSCC is still valid.
881 verify();
882#endif
Chandler Carruth312dddf2014-05-04 09:38:32 +0000883
884 // We return the list of SCCs which were merged so that callers can
885 // invalidate any data they have associated with those SCCs. Note that these
886 // SCCs are no longer in an interesting state (they are totally empty) but
887 // the pointers will remain stable for the life of the graph itself.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000888 return Connected;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000889}
890
Chandler Carruthe5944d92016-02-17 00:18:16 +0000891void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) {
892 assert(G->lookupRefSCC(SourceN) == this &&
893 "The source must be a member of this RefSCC.");
894
895 RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
896 assert(&TargetRC != this && "The target must not be a member of this RefSCC");
897
898 assert(std::find(G->LeafRefSCCs.begin(), G->LeafRefSCCs.end(), this) ==
899 G->LeafRefSCCs.end() &&
900 "Cannot have a leaf RefSCC source.");
901
Chandler Carruthaa839b22014-04-27 01:59:50 +0000902 // First remove it from the node.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000903 SourceN.removeEdgeInternal(TargetN.getFunction());
Chandler Carruthaa839b22014-04-27 01:59:50 +0000904
Chandler Carruthe5944d92016-02-17 00:18:16 +0000905 bool HasOtherEdgeToChildRC = false;
906 bool HasOtherChildRC = false;
907 for (SCC *InnerC : SCCs) {
908 for (Node &N : *InnerC) {
909 for (Edge &E : N) {
910 assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
911 RefSCC &OtherChildRC = *G->lookupRefSCC(*E.getNode());
912 if (&OtherChildRC == &TargetRC) {
913 HasOtherEdgeToChildRC = true;
914 break;
915 }
916 if (&OtherChildRC != this)
917 HasOtherChildRC = true;
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000918 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000919 if (HasOtherEdgeToChildRC)
920 break;
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000921 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000922 if (HasOtherEdgeToChildRC)
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000923 break;
924 }
925 // Because the SCCs form a DAG, deleting such an edge cannot change the set
926 // of SCCs in the graph. However, it may cut an edge of the SCC DAG, making
Chandler Carruthe5944d92016-02-17 00:18:16 +0000927 // the source SCC no longer connected to the target SCC. If so, we need to
928 // update the target SCC's map of its parents.
929 if (!HasOtherEdgeToChildRC) {
930 bool Removed = TargetRC.Parents.erase(this);
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000931 (void)Removed;
932 assert(Removed &&
Chandler Carruthe5944d92016-02-17 00:18:16 +0000933 "Did not find the source SCC in the target SCC's parent list!");
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000934
935 // It may orphan an SCC if it is the last edge reaching it, but that does
936 // not violate any invariants of the graph.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000937 if (TargetRC.Parents.empty())
938 DEBUG(dbgs() << "LCG: Update removing " << SourceN.getFunction().getName()
939 << " -> " << TargetN.getFunction().getName()
Chandler Carruthaa839b22014-04-27 01:59:50 +0000940 << " edge orphaned the callee's SCC!\n");
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000941
Chandler Carruthe5944d92016-02-17 00:18:16 +0000942 // It may make the Source SCC a leaf SCC.
943 if (!HasOtherChildRC)
944 G->LeafRefSCCs.push_back(this);
Chandler Carruthaca48d02014-04-26 09:06:53 +0000945 }
946}
947
Chandler Carruthe5944d92016-02-17 00:18:16 +0000948SmallVector<LazyCallGraph::RefSCC *, 1>
949LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN, Node &TargetN) {
950 assert(!SourceN[TargetN].isCall() &&
951 "Cannot remove a call edge, it must first be made a ref edge");
Chandler Carruthaa839b22014-04-27 01:59:50 +0000952
Chandler Carruthe5944d92016-02-17 00:18:16 +0000953 // First remove the actual edge.
954 SourceN.removeEdgeInternal(TargetN.getFunction());
955
956 // We return a list of the resulting *new* RefSCCs in post-order.
957 SmallVector<RefSCC *, 1> Result;
Chandler Carruth9302fbf2014-04-23 11:03:03 +0000958
Chandler Carrutha7205b62014-04-26 03:36:37 +0000959 // Direct recursion doesn't impact the SCC graph at all.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000960 if (&SourceN == &TargetN)
961 return Result;
Chandler Carrutha7205b62014-04-26 03:36:37 +0000962
Chandler Carruthe5944d92016-02-17 00:18:16 +0000963 // We build somewhat synthetic new RefSCCs by providing a postorder mapping
964 // for each inner SCC. We also store these associated with *nodes* rather
965 // than SCCs because this saves a round-trip through the node->SCC map and in
966 // the common case, SCCs are small. We will verify that we always give the
967 // same number to every node in the SCC such that these are equivalent.
968 const int RootPostOrderNumber = 0;
969 int PostOrderNumber = RootPostOrderNumber + 1;
970 SmallDenseMap<Node *, int> PostOrderMapping;
971
972 // Every node in the target SCC can already reach every node in this RefSCC
973 // (by definition). It is the only node we know will stay inside this RefSCC.
974 // Everything which transitively reaches Target will also remain in the
975 // RefSCC. We handle this by pre-marking that the nodes in the target SCC map
976 // back to the root post order number.
977 //
978 // This also enables us to take a very significant short-cut in the standard
979 // Tarjan walk to re-form RefSCCs below: whenever we build an edge that
980 // references the target node, we know that the target node eventually
981 // references all other nodes in our walk. As a consequence, we can detect
982 // and handle participants in that cycle without walking all the edges that
983 // form the connections, and instead by relying on the fundamental guarantee
984 // coming into this operation.
985 SCC &TargetC = *G->lookupSCC(TargetN);
986 for (Node &N : TargetC)
987 PostOrderMapping[&N] = RootPostOrderNumber;
988
989 // Reset all the other nodes to prepare for a DFS over them, and add them to
990 // our worklist.
991 SmallVector<Node *, 8> Worklist;
992 for (SCC *C : SCCs) {
993 if (C == &TargetC)
994 continue;
995
996 for (Node &N : *C)
997 N.DFSNumber = N.LowLink = 0;
998
999 Worklist.append(C->Nodes.begin(), C->Nodes.end());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001000 }
1001
Chandler Carruthe5944d92016-02-17 00:18:16 +00001002 auto MarkNodeForSCCNumber = [&PostOrderMapping](Node &N, int Number) {
1003 N.DFSNumber = N.LowLink = -1;
1004 PostOrderMapping[&N] = Number;
1005 };
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001006
Chandler Carruthe5944d92016-02-17 00:18:16 +00001007 SmallVector<std::pair<Node *, edge_iterator>, 4> DFSStack;
1008 SmallVector<Node *, 4> PendingRefSCCStack;
Chandler Carruthaca48d02014-04-26 09:06:53 +00001009 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001010 assert(DFSStack.empty() &&
1011 "Cannot begin a new root with a non-empty DFS stack!");
1012 assert(PendingRefSCCStack.empty() &&
1013 "Cannot begin a new root with pending nodes for an SCC!");
1014
1015 Node *RootN = Worklist.pop_back_val();
1016 // Skip any nodes we've already reached in the DFS.
1017 if (RootN->DFSNumber != 0) {
1018 assert(RootN->DFSNumber == -1 &&
1019 "Shouldn't have any mid-DFS root nodes!");
1020 continue;
1021 }
1022
1023 RootN->DFSNumber = RootN->LowLink = 1;
1024 int NextDFSNumber = 2;
1025
1026 DFSStack.push_back({RootN, RootN->begin()});
1027 do {
1028 Node *N;
1029 edge_iterator I;
1030 std::tie(N, I) = DFSStack.pop_back_val();
1031 auto E = N->end();
1032
1033 assert(N->DFSNumber != 0 && "We should always assign a DFS number "
1034 "before processing a node.");
1035
1036 while (I != E) {
1037 Node &ChildN = I->getNode(*G);
1038 if (ChildN.DFSNumber == 0) {
1039 // Mark that we should start at this child when next this node is the
1040 // top of the stack. We don't start at the next child to ensure this
1041 // child's lowlink is reflected.
1042 DFSStack.push_back({N, I});
1043
1044 // Continue, resetting to the child node.
1045 ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
1046 N = &ChildN;
1047 I = ChildN.begin();
1048 E = ChildN.end();
1049 continue;
1050 }
1051 if (ChildN.DFSNumber == -1) {
1052 // Check if this edge's target node connects to the deleted edge's
1053 // target node. If so, we know that every node connected will end up
1054 // in this RefSCC, so collapse the entire current stack into the root
1055 // slot in our SCC numbering. See above for the motivation of
1056 // optimizing the target connected nodes in this way.
1057 auto PostOrderI = PostOrderMapping.find(&ChildN);
1058 if (PostOrderI != PostOrderMapping.end() &&
1059 PostOrderI->second == RootPostOrderNumber) {
1060 MarkNodeForSCCNumber(*N, RootPostOrderNumber);
1061 while (!PendingRefSCCStack.empty())
1062 MarkNodeForSCCNumber(*PendingRefSCCStack.pop_back_val(),
1063 RootPostOrderNumber);
1064 while (!DFSStack.empty())
1065 MarkNodeForSCCNumber(*DFSStack.pop_back_val().first,
1066 RootPostOrderNumber);
1067 // Ensure we break all the way out of the enclosing loop.
1068 N = nullptr;
1069 break;
1070 }
1071
1072 // If this child isn't currently in this RefSCC, no need to process
1073 // it.
1074 // However, we do need to remove this RefSCC from its RefSCC's parent
1075 // set.
1076 RefSCC &ChildRC = *G->lookupRefSCC(ChildN);
1077 ChildRC.Parents.erase(this);
1078 ++I;
1079 continue;
1080 }
1081
1082 // Track the lowest link of the children, if any are still in the stack.
1083 // Any child not on the stack will have a LowLink of -1.
1084 assert(ChildN.LowLink != 0 &&
1085 "Low-link must not be zero with a non-zero DFS number.");
1086 if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
1087 N->LowLink = ChildN.LowLink;
1088 ++I;
1089 }
1090 if (!N)
1091 // We short-circuited this node.
1092 break;
1093
1094 // We've finished processing N and its descendents, put it on our pending
1095 // stack to eventually get merged into a RefSCC.
1096 PendingRefSCCStack.push_back(N);
1097
1098 // If this node is linked to some lower entry, continue walking up the
1099 // stack.
1100 if (N->LowLink != N->DFSNumber) {
1101 assert(!DFSStack.empty() &&
1102 "We never found a viable root for a RefSCC to pop off!");
1103 continue;
1104 }
1105
1106 // Otherwise, form a new RefSCC from the top of the pending node stack.
1107 int RootDFSNumber = N->DFSNumber;
1108 // Find the range of the node stack by walking down until we pass the
1109 // root DFS number.
1110 auto RefSCCNodes = make_range(
1111 PendingRefSCCStack.rbegin(),
1112 std::find_if(PendingRefSCCStack.rbegin(), PendingRefSCCStack.rend(),
1113 [RootDFSNumber](Node *N) {
1114 return N->DFSNumber < RootDFSNumber;
1115 }));
1116
1117 // Mark the postorder number for these nodes and clear them off the
1118 // stack. We'll use the postorder number to pull them into RefSCCs at the
1119 // end. FIXME: Fuse with the loop above.
1120 int RefSCCNumber = PostOrderNumber++;
1121 for (Node *N : RefSCCNodes)
1122 MarkNodeForSCCNumber(*N, RefSCCNumber);
1123
1124 PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1125 PendingRefSCCStack.end());
1126 } while (!DFSStack.empty());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001127
Chandler Carruthaca48d02014-04-26 09:06:53 +00001128 assert(DFSStack.empty() && "Didn't flush the entire DFS stack!");
Chandler Carruthe5944d92016-02-17 00:18:16 +00001129 assert(PendingRefSCCStack.empty() && "Didn't flush all pending nodes!");
Chandler Carruthaca48d02014-04-26 09:06:53 +00001130 } while (!Worklist.empty());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001131
Chandler Carruthe5944d92016-02-17 00:18:16 +00001132 // We now have a post-order numbering for RefSCCs and a mapping from each
1133 // node in this RefSCC to its final RefSCC. We create each new RefSCC node
1134 // (re-using this RefSCC node for the root) and build a radix-sort style map
1135 // from postorder number to the RefSCC. We then append SCCs to each of these
1136 // RefSCCs in the order they occured in the original SCCs container.
1137 for (int i = 1; i < PostOrderNumber; ++i)
1138 Result.push_back(G->createRefSCC(*G));
1139
1140 for (SCC *C : SCCs) {
1141 auto PostOrderI = PostOrderMapping.find(&*C->begin());
1142 assert(PostOrderI != PostOrderMapping.end() &&
1143 "Cannot have missing mappings for nodes!");
1144 int SCCNumber = PostOrderI->second;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001145#ifndef NDEBUG
Chandler Carruthe5944d92016-02-17 00:18:16 +00001146 for (Node &N : *C)
1147 assert(PostOrderMapping.find(&N)->second == SCCNumber &&
1148 "Cannot have different numbers for nodes in the same SCC!");
1149#endif
1150 if (SCCNumber == 0)
1151 // The root node is handled separately by removing the SCCs.
1152 continue;
1153
1154 RefSCC &RC = *Result[SCCNumber - 1];
1155 int SCCIndex = RC.SCCs.size();
1156 RC.SCCs.push_back(C);
1157 SCCIndices[C] = SCCIndex;
1158 C->OuterRefSCC = &RC;
1159 }
1160
1161 // FIXME: We re-walk the edges in each RefSCC to establish whether it is
1162 // a leaf and connect it to the rest of the graph's parents lists. This is
1163 // really wasteful. We should instead do this during the DFS to avoid yet
1164 // another edge walk.
1165 for (RefSCC *RC : Result)
1166 G->connectRefSCC(*RC);
1167
1168 // Now erase all but the root's SCCs.
1169 SCCs.erase(std::remove_if(SCCs.begin(), SCCs.end(),
1170 [&](SCC *C) {
1171 return PostOrderMapping.lookup(&*C->begin()) !=
1172 RootPostOrderNumber;
1173 }),
1174 SCCs.end());
1175
1176#ifndef NDEBUG
1177 // Now we need to reconnect the current (root) SCC to the graph. We do this
1178 // manually because we can special case our leaf handling and detect errors.
1179 bool IsLeaf = true;
1180#endif
1181 for (SCC *C : SCCs)
1182 for (Node &N : *C) {
1183 for (Edge &E : N) {
1184 assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
1185 RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
1186 if (&ChildRC == this)
1187 continue;
1188 ChildRC.Parents.insert(this);
1189#ifndef NDEBUG
1190 IsLeaf = false;
1191#endif
1192 }
1193 }
1194#ifndef NDEBUG
1195 if (!Result.empty())
1196 assert(!IsLeaf && "This SCC cannot be a leaf as we have split out new "
1197 "SCCs by removing this edge.");
1198 if (!std::any_of(G->LeafRefSCCs.begin(), G->LeafRefSCCs.end(),
1199 [&](RefSCC *C) { return C == this; }))
1200 assert(!IsLeaf && "This SCC cannot be a leaf as it already had child "
1201 "SCCs before we removed this edge.");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001202#endif
1203 // If this SCC stopped being a leaf through this edge removal, remove it from
Chandler Carruthe5944d92016-02-17 00:18:16 +00001204 // the leaf SCC list. Note that this DTRT in the case where this was never
1205 // a leaf.
1206 // FIXME: As LeafRefSCCs could be very large, we might want to not walk the
1207 // entire list if this RefSCC wasn't a leaf before the edge removal.
1208 if (!Result.empty())
1209 G->LeafRefSCCs.erase(
1210 std::remove(G->LeafRefSCCs.begin(), G->LeafRefSCCs.end(), this),
1211 G->LeafRefSCCs.end());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001212
1213 // Return the new list of SCCs.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001214 return Result;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001215}
1216
Chandler Carruthe5944d92016-02-17 00:18:16 +00001217void LazyCallGraph::insertEdge(Node &SourceN, Function &Target, Edge::Kind EK) {
Chandler Carruthc00a7ff2014-04-28 11:10:23 +00001218 assert(SCCMap.empty() && DFSStack.empty() &&
1219 "This method cannot be called after SCCs have been formed!");
1220
Chandler Carruthe5944d92016-02-17 00:18:16 +00001221 return SourceN.insertEdgeInternal(Target, EK);
Chandler Carruthc00a7ff2014-04-28 11:10:23 +00001222}
1223
Chandler Carruthe5944d92016-02-17 00:18:16 +00001224void LazyCallGraph::removeEdge(Node &SourceN, Function &Target) {
Chandler Carruthaa839b22014-04-27 01:59:50 +00001225 assert(SCCMap.empty() && DFSStack.empty() &&
1226 "This method cannot be called after SCCs have been formed!");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001227
Chandler Carruthe5944d92016-02-17 00:18:16 +00001228 return SourceN.removeEdgeInternal(Target);
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001229}
1230
Chandler Carruth2a898e02014-04-23 23:20:36 +00001231LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
1232 return *new (MappedN = BPA.Allocate()) Node(*this, F);
Chandler Carruthd8d865e2014-04-18 11:02:33 +00001233}
1234
1235void LazyCallGraph::updateGraphPtrs() {
Chandler Carruthb60cb312014-04-17 07:25:59 +00001236 // Process all nodes updating the graph pointers.
Chandler Carruthaa839b22014-04-27 01:59:50 +00001237 {
1238 SmallVector<Node *, 16> Worklist;
Chandler Carrutha4499e92016-02-02 03:57:13 +00001239 for (Edge &E : EntryEdges)
1240 if (Node *EntryN = E.getNode())
Chandler Carruthaa839b22014-04-27 01:59:50 +00001241 Worklist.push_back(EntryN);
Chandler Carruthb60cb312014-04-17 07:25:59 +00001242
Chandler Carruthaa839b22014-04-27 01:59:50 +00001243 while (!Worklist.empty()) {
1244 Node *N = Worklist.pop_back_val();
1245 N->G = this;
Chandler Carrutha4499e92016-02-02 03:57:13 +00001246 for (Edge &E : N->Edges)
Chandler Carruthe5944d92016-02-17 00:18:16 +00001247 if (Node *TargetN = E.getNode())
1248 Worklist.push_back(TargetN);
Chandler Carruthaa839b22014-04-27 01:59:50 +00001249 }
1250 }
1251
1252 // Process all SCCs updating the graph pointers.
1253 {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001254 SmallVector<RefSCC *, 16> Worklist(LeafRefSCCs.begin(), LeafRefSCCs.end());
Chandler Carruthaa839b22014-04-27 01:59:50 +00001255
1256 while (!Worklist.empty()) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001257 RefSCC &C = *Worklist.pop_back_val();
1258 C.G = this;
1259 for (RefSCC &ParentC : C.parents())
1260 Worklist.push_back(&ParentC);
Chandler Carruthaa839b22014-04-27 01:59:50 +00001261 }
Chandler Carruthb60cb312014-04-17 07:25:59 +00001262 }
Chandler Carruthbf71a342014-02-06 04:37:03 +00001263}
Chandler Carruthbf71a342014-02-06 04:37:03 +00001264
Chandler Carruthe5944d92016-02-17 00:18:16 +00001265/// Build the internal SCCs for a RefSCC from a sequence of nodes.
1266///
1267/// Appends the SCCs to the provided vector and updates the map with their
1268/// indices. Both the vector and map must be empty when passed into this
1269/// routine.
1270void LazyCallGraph::buildSCCs(RefSCC &RC, node_stack_range Nodes) {
1271 assert(RC.SCCs.empty() && "Already built SCCs!");
1272 assert(RC.SCCIndices.empty() && "Already mapped SCC indices!");
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001273
Chandler Carruthe5944d92016-02-17 00:18:16 +00001274 for (Node *N : Nodes) {
1275 assert(N->LowLink >= (*Nodes.begin())->LowLink &&
Chandler Carruthcace6622014-04-23 10:31:17 +00001276 "We cannot have a low link in an SCC lower than its root on the "
1277 "stack!");
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001278
Chandler Carruthe5944d92016-02-17 00:18:16 +00001279 // This node will go into the next RefSCC, clear out its DFS and low link
1280 // as we scan.
1281 N->DFSNumber = N->LowLink = 0;
1282 }
1283
1284 // Each RefSCC contains a DAG of the call SCCs. To build these, we do
1285 // a direct walk of the call edges using Tarjan's algorithm. We reuse the
1286 // internal storage as we won't need it for the outer graph's DFS any longer.
1287
1288 SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
1289 SmallVector<Node *, 16> PendingSCCStack;
1290
1291 // Scan down the stack and DFS across the call edges.
1292 for (Node *RootN : Nodes) {
1293 assert(DFSStack.empty() &&
1294 "Cannot begin a new root with a non-empty DFS stack!");
1295 assert(PendingSCCStack.empty() &&
1296 "Cannot begin a new root with pending nodes for an SCC!");
1297
1298 // Skip any nodes we've already reached in the DFS.
1299 if (RootN->DFSNumber != 0) {
1300 assert(RootN->DFSNumber == -1 &&
1301 "Shouldn't have any mid-DFS root nodes!");
1302 continue;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001303 }
1304
Chandler Carruthe5944d92016-02-17 00:18:16 +00001305 RootN->DFSNumber = RootN->LowLink = 1;
1306 int NextDFSNumber = 2;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001307
Chandler Carruthe5944d92016-02-17 00:18:16 +00001308 DFSStack.push_back({RootN, RootN->call_begin()});
1309 do {
1310 Node *N;
1311 call_edge_iterator I;
1312 std::tie(N, I) = DFSStack.pop_back_val();
1313 auto E = N->call_end();
1314 while (I != E) {
1315 Node &ChildN = *I->getNode();
1316 if (ChildN.DFSNumber == 0) {
1317 // We haven't yet visited this child, so descend, pushing the current
1318 // node onto the stack.
1319 DFSStack.push_back({N, I});
1320
1321 assert(!lookupSCC(ChildN) &&
1322 "Found a node with 0 DFS number but already in an SCC!");
1323 ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
1324 N = &ChildN;
1325 I = N->call_begin();
1326 E = N->call_end();
1327 continue;
1328 }
1329
1330 // If the child has already been added to some child component, it
1331 // couldn't impact the low-link of this parent because it isn't
1332 // connected, and thus its low-link isn't relevant so skip it.
1333 if (ChildN.DFSNumber == -1) {
1334 ++I;
1335 continue;
1336 }
1337
1338 // Track the lowest linked child as the lowest link for this node.
1339 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1340 if (ChildN.LowLink < N->LowLink)
1341 N->LowLink = ChildN.LowLink;
1342
1343 // Move to the next edge.
1344 ++I;
1345 }
1346
1347 // We've finished processing N and its descendents, put it on our pending
1348 // SCC stack to eventually get merged into an SCC of nodes.
1349 PendingSCCStack.push_back(N);
1350
1351 // If this node is linked to some lower entry, continue walking up the
1352 // stack.
1353 if (N->LowLink != N->DFSNumber)
1354 continue;
1355
1356 // Otherwise, we've completed an SCC. Append it to our post order list of
1357 // SCCs.
1358 int RootDFSNumber = N->DFSNumber;
1359 // Find the range of the node stack by walking down until we pass the
1360 // root DFS number.
1361 auto SCCNodes = make_range(
1362 PendingSCCStack.rbegin(),
1363 std::find_if(PendingSCCStack.rbegin(), PendingSCCStack.rend(),
1364 [RootDFSNumber](Node *N) {
1365 return N->DFSNumber < RootDFSNumber;
1366 }));
1367 // Form a new SCC out of these nodes and then clear them off our pending
1368 // stack.
1369 RC.SCCs.push_back(createSCC(RC, SCCNodes));
1370 for (Node &N : *RC.SCCs.back()) {
1371 N.DFSNumber = N.LowLink = -1;
1372 SCCMap[&N] = RC.SCCs.back();
1373 }
1374 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
1375 } while (!DFSStack.empty());
1376 }
1377
1378 // Wire up the SCC indices.
1379 for (int i = 0, Size = RC.SCCs.size(); i < Size; ++i)
1380 RC.SCCIndices[RC.SCCs[i]] = i;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001381}
1382
Chandler Carruthe5944d92016-02-17 00:18:16 +00001383// FIXME: We should move callers of this to embed the parent linking and leaf
1384// tracking into their DFS in order to remove a full walk of all edges.
1385void LazyCallGraph::connectRefSCC(RefSCC &RC) {
1386 // Walk all edges in the RefSCC (this remains linear as we only do this once
1387 // when we build the RefSCC) to connect it to the parent sets of its
1388 // children.
1389 bool IsLeaf = true;
1390 for (SCC &C : RC)
1391 for (Node &N : C)
1392 for (Edge &E : N) {
1393 assert(E.getNode() &&
1394 "Cannot have a missing node in a visited part of the graph!");
1395 RefSCC &ChildRC = *lookupRefSCC(*E.getNode());
1396 if (&ChildRC == &RC)
1397 continue;
1398 ChildRC.Parents.insert(&RC);
1399 IsLeaf = false;
1400 }
1401
1402 // For the SCCs where we fine no child SCCs, add them to the leaf list.
1403 if (IsLeaf)
1404 LeafRefSCCs.push_back(&RC);
1405}
1406
1407LazyCallGraph::RefSCC *LazyCallGraph::getNextRefSCCInPostOrder() {
1408 if (DFSStack.empty()) {
1409 Node *N;
Chandler Carruth90821c22014-04-26 09:45:55 +00001410 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001411 // If we've handled all candidate entry nodes to the SCC forest, we're
1412 // done.
1413 if (RefSCCEntryNodes.empty())
Chandler Carruth90821c22014-04-26 09:45:55 +00001414 return nullptr;
Chandler Carruth18eadd922014-04-18 10:50:32 +00001415
Chandler Carruthe5944d92016-02-17 00:18:16 +00001416 N = &get(*RefSCCEntryNodes.pop_back_val());
Chandler Carruth90821c22014-04-26 09:45:55 +00001417 } while (N->DFSNumber != 0);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001418
1419 // Found a new root, begin the DFS here.
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001420 N->LowLink = N->DFSNumber = 1;
Chandler Carruth09751bf2014-04-24 09:59:59 +00001421 NextDFSNumber = 2;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001422 DFSStack.push_back({N, N->begin()});
Chandler Carruth18eadd922014-04-18 10:50:32 +00001423 }
1424
Chandler Carruth91dcf0f2014-04-24 21:19:30 +00001425 for (;;) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001426 Node *N;
1427 edge_iterator I;
1428 std::tie(N, I) = DFSStack.pop_back_val();
1429
1430 assert(N->DFSNumber > 0 && "We should always assign a DFS number "
1431 "before placing a node onto the stack.");
Chandler Carruth24553932014-04-24 11:05:20 +00001432
Chandler Carrutha4499e92016-02-02 03:57:13 +00001433 auto E = N->end();
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001434 while (I != E) {
Chandler Carrutha4499e92016-02-02 03:57:13 +00001435 Node &ChildN = I->getNode(*this);
Chandler Carruthbd5d3082014-04-23 23:34:48 +00001436 if (ChildN.DFSNumber == 0) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001437 // We haven't yet visited this child, so descend, pushing the current
1438 // node onto the stack.
1439 DFSStack.push_back({N, N->begin()});
Chandler Carruth18eadd922014-04-18 10:50:32 +00001440
Chandler Carruth09751bf2014-04-24 09:59:59 +00001441 assert(!SCCMap.count(&ChildN) &&
1442 "Found a node with 0 DFS number but already in an SCC!");
1443 ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001444 N = &ChildN;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001445 I = N->begin();
1446 E = N->end();
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001447 continue;
Chandler Carruthcace6622014-04-23 10:31:17 +00001448 }
1449
Chandler Carruthe5944d92016-02-17 00:18:16 +00001450 // If the child has already been added to some child component, it
1451 // couldn't impact the low-link of this parent because it isn't
1452 // connected, and thus its low-link isn't relevant so skip it.
1453 if (ChildN.DFSNumber == -1) {
1454 ++I;
1455 continue;
1456 }
1457
1458 // Track the lowest linked child as the lowest link for this node.
1459 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1460 if (ChildN.LowLink < N->LowLink)
Chandler Carruthbd5d3082014-04-23 23:34:48 +00001461 N->LowLink = ChildN.LowLink;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001462
1463 // Move to the next edge.
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001464 ++I;
Chandler Carruth18eadd922014-04-18 10:50:32 +00001465 }
1466
Chandler Carruthe5944d92016-02-17 00:18:16 +00001467 // We've finished processing N and its descendents, put it on our pending
1468 // SCC stack to eventually get merged into an SCC of nodes.
1469 PendingRefSCCStack.push_back(N);
Chandler Carruth18eadd922014-04-18 10:50:32 +00001470
Chandler Carruthe5944d92016-02-17 00:18:16 +00001471 // If this node is linked to some lower entry, continue walking up the
1472 // stack.
1473 if (N->LowLink != N->DFSNumber) {
1474 assert(!DFSStack.empty() &&
1475 "We never found a viable root for an SCC to pop off!");
1476 continue;
1477 }
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001478
Chandler Carruthe5944d92016-02-17 00:18:16 +00001479 // Otherwise, form a new RefSCC from the top of the pending node stack.
1480 int RootDFSNumber = N->DFSNumber;
1481 // Find the range of the node stack by walking down until we pass the
1482 // root DFS number.
1483 auto RefSCCNodes = node_stack_range(
1484 PendingRefSCCStack.rbegin(),
1485 std::find_if(
1486 PendingRefSCCStack.rbegin(), PendingRefSCCStack.rend(),
1487 [RootDFSNumber](Node *N) { return N->DFSNumber < RootDFSNumber; }));
1488 // Form a new RefSCC out of these nodes and then clear them off our pending
1489 // stack.
1490 RefSCC *NewRC = createRefSCC(*this);
1491 buildSCCs(*NewRC, RefSCCNodes);
1492 connectRefSCC(*NewRC);
1493 PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1494 PendingRefSCCStack.end());
1495
1496 // We return the new node here. This essentially suspends the DFS walk
1497 // until another RefSCC is requested.
1498 return NewRC;
Chandler Carruth91dcf0f2014-04-24 21:19:30 +00001499 }
Chandler Carruth18eadd922014-04-18 10:50:32 +00001500}
1501
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001502template class llvm::AnalysisBase<LazyCallGraphAnalysis>;
1503
Chandler Carruthbf71a342014-02-06 04:37:03 +00001504LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
1505
Chandler Carruthe5944d92016-02-17 00:18:16 +00001506static void printNode(raw_ostream &OS, LazyCallGraph::Node &N) {
Chandler Carrutha4499e92016-02-02 03:57:13 +00001507 OS << " Edges in function: " << N.getFunction().getName() << "\n";
1508 for (const LazyCallGraph::Edge &E : N)
1509 OS << " " << (E.isCall() ? "call" : "ref ") << " -> "
1510 << E.getFunction().getName() << "\n";
Chandler Carruth11f50322015-01-14 00:27:45 +00001511
1512 OS << "\n";
1513}
1514
Chandler Carruthe5944d92016-02-17 00:18:16 +00001515static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) {
1516 ptrdiff_t Size = std::distance(C.begin(), C.end());
1517 OS << " SCC with " << Size << " functions:\n";
Chandler Carruth11f50322015-01-14 00:27:45 +00001518
Chandler Carruthe5944d92016-02-17 00:18:16 +00001519 for (LazyCallGraph::Node &N : C)
1520 OS << " " << N.getFunction().getName() << "\n";
1521}
1522
1523static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) {
1524 ptrdiff_t Size = std::distance(C.begin(), C.end());
1525 OS << " RefSCC with " << Size << " call SCCs:\n";
1526
1527 for (LazyCallGraph::SCC &InnerC : C)
1528 printSCC(OS, InnerC);
Chandler Carruth11f50322015-01-14 00:27:45 +00001529
1530 OS << "\n";
1531}
1532
Chandler Carruthd174ce42015-01-05 02:47:05 +00001533PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M,
Chandler Carruthe9b50612014-03-10 02:14:14 +00001534 ModuleAnalysisManager *AM) {
Chandler Carruth11f50322015-01-14 00:27:45 +00001535 LazyCallGraph &G = AM->getResult<LazyCallGraphAnalysis>(M);
1536
1537 OS << "Printing the call graph for module: " << M.getModuleIdentifier()
1538 << "\n\n";
1539
Chandler Carruthe5944d92016-02-17 00:18:16 +00001540 for (Function &F : M)
1541 printNode(OS, G.get(F));
Chandler Carruth11f50322015-01-14 00:27:45 +00001542
Chandler Carruthe5944d92016-02-17 00:18:16 +00001543 for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs())
1544 printRefSCC(OS, C);
Chandler Carruth18eadd922014-04-18 10:50:32 +00001545
Chandler Carruthbf71a342014-02-06 04:37:03 +00001546 return PreservedAnalyses::all();
Chandler Carruthbf71a342014-02-06 04:37:03 +00001547}