blob: 43f47ccc73737a1b7fb110a4ea30106946df41c9 [file] [log] [blame]
Chandler Carruthbf71a342014-02-06 04:37:03 +00001//===- LazyCallGraph.cpp - Analysis of a Module's call graph --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruth11b3f602016-09-04 08:34:31 +000011#include "llvm/ADT/ScopeExit.h"
Chandler Carruth49d728a2016-09-16 10:20:17 +000012#include "llvm/ADT/Sequence.h"
Chandler Carruth18eadd922014-04-18 10:50:32 +000013#include "llvm/ADT/STLExtras.h"
Chandler Carruth86f0bdf2016-12-09 00:46:44 +000014#include "llvm/ADT/ScopeExit.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000015#include "llvm/IR/CallSite.h"
Chandler Carruth7da14f12014-03-06 03:23:41 +000016#include "llvm/IR/InstVisitor.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000017#include "llvm/IR/Instructions.h"
18#include "llvm/IR/PassManager.h"
Chandler Carruth99b756d2014-04-21 05:04:24 +000019#include "llvm/Support/Debug.h"
Sean Silva7cb30662016-06-18 09:17:32 +000020#include "llvm/Support/GraphWriter.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000021
22using namespace llvm;
23
Chandler Carruthf1221bd2014-04-22 02:48:03 +000024#define DEBUG_TYPE "lcg"
25
Chandler Carrutha4499e92016-02-02 03:57:13 +000026static void addEdge(SmallVectorImpl<LazyCallGraph::Edge> &Edges,
Chandler Carruthe5944d92016-02-17 00:18:16 +000027 DenseMap<Function *, int> &EdgeIndexMap, Function &F,
Chandler Carrutha4499e92016-02-02 03:57:13 +000028 LazyCallGraph::Edge::Kind EK) {
Chandler Carruth86f0bdf2016-12-09 00:46:44 +000029 if (!EdgeIndexMap.insert({&F, Edges.size()}).second)
30 return;
31
32 DEBUG(dbgs() << " Added callable function: " << F.getName() << "\n");
33 Edges.emplace_back(LazyCallGraph::Edge(F, EK));
Chandler Carrutha4499e92016-02-02 03:57:13 +000034}
35
Chandler Carruth18eadd922014-04-18 10:50:32 +000036LazyCallGraph::Node::Node(LazyCallGraph &G, Function &F)
37 : G(&G), F(F), DFSNumber(0), LowLink(0) {
Chandler Carruth99b756d2014-04-21 05:04:24 +000038 DEBUG(dbgs() << " Adding functions called by '" << F.getName()
39 << "' to the graph.\n");
40
Chandler Carruthbf71a342014-02-06 04:37:03 +000041 SmallVector<Constant *, 16> Worklist;
Chandler Carrutha4499e92016-02-02 03:57:13 +000042 SmallPtrSet<Function *, 4> Callees;
Chandler Carruthbf71a342014-02-06 04:37:03 +000043 SmallPtrSet<Constant *, 16> Visited;
Chandler Carrutha4499e92016-02-02 03:57:13 +000044
45 // Find all the potential call graph edges in this function. We track both
46 // actual call edges and indirect references to functions. The direct calls
47 // are trivially added, but to accumulate the latter we walk the instructions
48 // and add every operand which is a constant to the worklist to process
49 // afterward.
Chandler Carruth86f0bdf2016-12-09 00:46:44 +000050 //
51 // Note that we consider *any* function with a definition to be a viable
52 // edge. Even if the function's definition is subject to replacement by
53 // some other module (say, a weak definition) there may still be
54 // optimizations which essentially speculate based on the definition and
55 // a way to check that the specific definition is in fact the one being
56 // used. For example, this could be done by moving the weak definition to
57 // a strong (internal) definition and making the weak definition be an
58 // alias. Then a test of the address of the weak function against the new
59 // strong definition's address would be an effective way to determine the
60 // safety of optimizing a direct call edge.
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +000061 for (BasicBlock &BB : F)
Chandler Carrutha4499e92016-02-02 03:57:13 +000062 for (Instruction &I : BB) {
63 if (auto CS = CallSite(&I))
64 if (Function *Callee = CS.getCalledFunction())
Chandler Carruth86f0bdf2016-12-09 00:46:44 +000065 if (!Callee->isDeclaration())
66 if (Callees.insert(Callee).second) {
67 Visited.insert(Callee);
68 addEdge(Edges, EdgeIndexMap, *Callee, LazyCallGraph::Edge::Call);
69 }
Chandler Carrutha4499e92016-02-02 03:57:13 +000070
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +000071 for (Value *Op : I.operand_values())
Chandler Carruth1583e992014-03-03 10:42:58 +000072 if (Constant *C = dyn_cast<Constant>(Op))
David Blaikie70573dc2014-11-19 07:49:26 +000073 if (Visited.insert(C).second)
Chandler Carruthbf71a342014-02-06 04:37:03 +000074 Worklist.push_back(C);
Chandler Carrutha4499e92016-02-02 03:57:13 +000075 }
Chandler Carruthbf71a342014-02-06 04:37:03 +000076
77 // We've collected all the constant (and thus potentially function or
78 // function containing) operands to all of the instructions in the function.
79 // Process them (recursively) collecting every function found.
Chandler Carruth88823462016-08-24 09:37:14 +000080 visitReferences(Worklist, Visited, [&](Function &F) {
81 addEdge(Edges, EdgeIndexMap, F, LazyCallGraph::Edge::Ref);
82 });
Chandler Carruthbf71a342014-02-06 04:37:03 +000083}
84
Chandler Carruthe5944d92016-02-17 00:18:16 +000085void LazyCallGraph::Node::insertEdgeInternal(Function &Target, Edge::Kind EK) {
86 if (Node *N = G->lookup(Target))
Chandler Carrutha4499e92016-02-02 03:57:13 +000087 return insertEdgeInternal(*N, EK);
Chandler Carruth5217c942014-04-30 10:48:36 +000088
Chandler Carruthe5944d92016-02-17 00:18:16 +000089 EdgeIndexMap.insert({&Target, Edges.size()});
90 Edges.emplace_back(Target, EK);
Chandler Carruth5217c942014-04-30 10:48:36 +000091}
92
Chandler Carruthe5944d92016-02-17 00:18:16 +000093void LazyCallGraph::Node::insertEdgeInternal(Node &TargetN, Edge::Kind EK) {
94 EdgeIndexMap.insert({&TargetN.getFunction(), Edges.size()});
95 Edges.emplace_back(TargetN, EK);
Chandler Carruthc00a7ff2014-04-28 11:10:23 +000096}
97
Chandler Carruthe5944d92016-02-17 00:18:16 +000098void LazyCallGraph::Node::setEdgeKind(Function &TargetF, Edge::Kind EK) {
99 Edges[EdgeIndexMap.find(&TargetF)->second].setKind(EK);
100}
101
102void LazyCallGraph::Node::removeEdgeInternal(Function &Target) {
103 auto IndexMapI = EdgeIndexMap.find(&Target);
Chandler Carrutha4499e92016-02-02 03:57:13 +0000104 assert(IndexMapI != EdgeIndexMap.end() &&
Chandler Carruthe5944d92016-02-17 00:18:16 +0000105 "Target not in the edge set for this caller?");
Chandler Carruthaa839b22014-04-27 01:59:50 +0000106
Chandler Carrutha4499e92016-02-02 03:57:13 +0000107 Edges[IndexMapI->second] = Edge();
108 EdgeIndexMap.erase(IndexMapI);
Chandler Carruthaa839b22014-04-27 01:59:50 +0000109}
110
Matthias Braun8c209aa2017-01-28 02:02:38 +0000111#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
112LLVM_DUMP_METHOD void LazyCallGraph::Node::dump() const {
Chandler Carruthdca83402016-06-27 23:26:08 +0000113 dbgs() << *this << '\n';
114}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000115#endif
Chandler Carruthdca83402016-06-27 23:26:08 +0000116
Chandler Carruth2174f442014-04-18 20:44:16 +0000117LazyCallGraph::LazyCallGraph(Module &M) : NextDFSNumber(0) {
Chandler Carruth99b756d2014-04-21 05:04:24 +0000118 DEBUG(dbgs() << "Building CG for module: " << M.getModuleIdentifier()
119 << "\n");
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000120 for (Function &F : M)
121 if (!F.isDeclaration() && !F.hasLocalLinkage())
Chandler Carruthe5944d92016-02-17 00:18:16 +0000122 if (EntryIndexMap.insert({&F, EntryEdges.size()}).second) {
Chandler Carruth99b756d2014-04-21 05:04:24 +0000123 DEBUG(dbgs() << " Adding '" << F.getName()
124 << "' to entry set of the graph.\n");
Chandler Carrutha4499e92016-02-02 03:57:13 +0000125 EntryEdges.emplace_back(F, Edge::Ref);
Chandler Carruth99b756d2014-04-21 05:04:24 +0000126 }
Chandler Carruthbf71a342014-02-06 04:37:03 +0000127
128 // Now add entry nodes for functions reachable via initializers to globals.
129 SmallVector<Constant *, 16> Worklist;
130 SmallPtrSet<Constant *, 16> Visited;
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000131 for (GlobalVariable &GV : M.globals())
132 if (GV.hasInitializer())
David Blaikie70573dc2014-11-19 07:49:26 +0000133 if (Visited.insert(GV.getInitializer()).second)
Chandler Carruthb9e2f8c2014-03-09 12:20:34 +0000134 Worklist.push_back(GV.getInitializer());
Chandler Carruthbf71a342014-02-06 04:37:03 +0000135
Chandler Carruth99b756d2014-04-21 05:04:24 +0000136 DEBUG(dbgs() << " Adding functions referenced by global initializers to the "
137 "entry set.\n");
Chandler Carruth88823462016-08-24 09:37:14 +0000138 visitReferences(Worklist, Visited, [&](Function &F) {
139 addEdge(EntryEdges, EntryIndexMap, F, LazyCallGraph::Edge::Ref);
140 });
Chandler Carruth18eadd922014-04-18 10:50:32 +0000141
Chandler Carrutha4499e92016-02-02 03:57:13 +0000142 for (const Edge &E : EntryEdges)
Chandler Carruthe5944d92016-02-17 00:18:16 +0000143 RefSCCEntryNodes.push_back(&E.getFunction());
Chandler Carruthbf71a342014-02-06 04:37:03 +0000144}
145
Chandler Carruthbf71a342014-02-06 04:37:03 +0000146LazyCallGraph::LazyCallGraph(LazyCallGraph &&G)
Chandler Carruth2174f442014-04-18 20:44:16 +0000147 : BPA(std::move(G.BPA)), NodeMap(std::move(G.NodeMap)),
Chandler Carrutha4499e92016-02-02 03:57:13 +0000148 EntryEdges(std::move(G.EntryEdges)),
Chandler Carruth0b623ba2014-04-23 04:00:17 +0000149 EntryIndexMap(std::move(G.EntryIndexMap)), SCCBPA(std::move(G.SCCBPA)),
Chandler Carruthe5944d92016-02-17 00:18:16 +0000150 SCCMap(std::move(G.SCCMap)), LeafRefSCCs(std::move(G.LeafRefSCCs)),
Chandler Carruth18eadd922014-04-18 10:50:32 +0000151 DFSStack(std::move(G.DFSStack)),
Chandler Carruthe5944d92016-02-17 00:18:16 +0000152 RefSCCEntryNodes(std::move(G.RefSCCEntryNodes)),
Chandler Carruth2174f442014-04-18 20:44:16 +0000153 NextDFSNumber(G.NextDFSNumber) {
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000154 updateGraphPtrs();
155}
156
157LazyCallGraph &LazyCallGraph::operator=(LazyCallGraph &&G) {
158 BPA = std::move(G.BPA);
Chandler Carruth2174f442014-04-18 20:44:16 +0000159 NodeMap = std::move(G.NodeMap);
Chandler Carrutha4499e92016-02-02 03:57:13 +0000160 EntryEdges = std::move(G.EntryEdges);
Chandler Carruth0b623ba2014-04-23 04:00:17 +0000161 EntryIndexMap = std::move(G.EntryIndexMap);
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000162 SCCBPA = std::move(G.SCCBPA);
163 SCCMap = std::move(G.SCCMap);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000164 LeafRefSCCs = std::move(G.LeafRefSCCs);
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000165 DFSStack = std::move(G.DFSStack);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000166 RefSCCEntryNodes = std::move(G.RefSCCEntryNodes);
Chandler Carruth2174f442014-04-18 20:44:16 +0000167 NextDFSNumber = G.NextDFSNumber;
Chandler Carruthd8d865e2014-04-18 11:02:33 +0000168 updateGraphPtrs();
169 return *this;
170}
171
Matthias Braun8c209aa2017-01-28 02:02:38 +0000172#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
173LLVM_DUMP_METHOD void LazyCallGraph::SCC::dump() const {
Chandler Carruthdca83402016-06-27 23:26:08 +0000174 dbgs() << *this << '\n';
175}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000176#endif
Chandler Carruthdca83402016-06-27 23:26:08 +0000177
Chandler Carruthe5944d92016-02-17 00:18:16 +0000178#ifndef NDEBUG
179void LazyCallGraph::SCC::verify() {
180 assert(OuterRefSCC && "Can't have a null RefSCC!");
181 assert(!Nodes.empty() && "Can't have an empty SCC!");
Chandler Carruth8f92d6d2014-04-26 01:03:46 +0000182
Chandler Carruthe5944d92016-02-17 00:18:16 +0000183 for (Node *N : Nodes) {
184 assert(N && "Can't have a null node!");
185 assert(OuterRefSCC->G->lookupSCC(*N) == this &&
186 "Node does not map to this SCC!");
187 assert(N->DFSNumber == -1 &&
188 "Must set DFS numbers to -1 when adding a node to an SCC!");
189 assert(N->LowLink == -1 &&
190 "Must set low link to -1 when adding a node to an SCC!");
191 for (Edge &E : *N)
192 assert(E.getNode() && "Can't have an edge to a raw function!");
193 }
194}
195#endif
196
Chandler Carruthbae595b2016-11-22 19:23:31 +0000197bool LazyCallGraph::SCC::isParentOf(const SCC &C) const {
198 if (this == &C)
199 return false;
200
201 for (Node &N : *this)
202 for (Edge &E : N.calls())
203 if (Node *CalleeN = E.getNode())
204 if (OuterRefSCC->G->lookupSCC(*CalleeN) == &C)
205 return true;
206
207 // No edges found.
208 return false;
209}
210
211bool LazyCallGraph::SCC::isAncestorOf(const SCC &TargetC) const {
212 if (this == &TargetC)
213 return false;
214
215 LazyCallGraph &G = *OuterRefSCC->G;
216
217 // Start with this SCC.
218 SmallPtrSet<const SCC *, 16> Visited = {this};
219 SmallVector<const SCC *, 16> Worklist = {this};
220
221 // Walk down the graph until we run out of edges or find a path to TargetC.
222 do {
223 const SCC &C = *Worklist.pop_back_val();
224 for (Node &N : C)
225 for (Edge &E : N.calls()) {
226 Node *CalleeN = E.getNode();
227 if (!CalleeN)
228 continue;
229 SCC *CalleeC = G.lookupSCC(*CalleeN);
230 if (!CalleeC)
231 continue;
232
233 // If the callee's SCC is the TargetC, we're done.
234 if (CalleeC == &TargetC)
235 return true;
236
237 // If this is the first time we've reached this SCC, put it on the
238 // worklist to recurse through.
239 if (Visited.insert(CalleeC).second)
240 Worklist.push_back(CalleeC);
241 }
242 } while (!Worklist.empty());
243
244 // No paths found.
245 return false;
246}
247
Chandler Carruthe5944d92016-02-17 00:18:16 +0000248LazyCallGraph::RefSCC::RefSCC(LazyCallGraph &G) : G(&G) {}
249
Matthias Braun8c209aa2017-01-28 02:02:38 +0000250#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
251LLVM_DUMP_METHOD void LazyCallGraph::RefSCC::dump() const {
Chandler Carruthdca83402016-06-27 23:26:08 +0000252 dbgs() << *this << '\n';
253}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000254#endif
Chandler Carruthdca83402016-06-27 23:26:08 +0000255
Chandler Carruthe5944d92016-02-17 00:18:16 +0000256#ifndef NDEBUG
257void LazyCallGraph::RefSCC::verify() {
258 assert(G && "Can't have a null graph!");
259 assert(!SCCs.empty() && "Can't have an empty SCC!");
260
261 // Verify basic properties of the SCCs.
Chandler Carruth88823462016-08-24 09:37:14 +0000262 SmallPtrSet<SCC *, 4> SCCSet;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000263 for (SCC *C : SCCs) {
264 assert(C && "Can't have a null SCC!");
265 C->verify();
266 assert(&C->getOuterRefSCC() == this &&
267 "SCC doesn't think it is inside this RefSCC!");
Chandler Carruth88823462016-08-24 09:37:14 +0000268 bool Inserted = SCCSet.insert(C).second;
269 assert(Inserted && "Found a duplicate SCC!");
Chandler Carruth23a6c3f2016-12-06 10:29:23 +0000270 auto IndexIt = SCCIndices.find(C);
271 assert(IndexIt != SCCIndices.end() &&
272 "Found an SCC that doesn't have an index!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000273 }
274
275 // Check that our indices map correctly.
276 for (auto &SCCIndexPair : SCCIndices) {
277 SCC *C = SCCIndexPair.first;
278 int i = SCCIndexPair.second;
279 assert(C && "Can't have a null SCC in the indices!");
Chandler Carruth88823462016-08-24 09:37:14 +0000280 assert(SCCSet.count(C) && "Found an index for an SCC not in the RefSCC!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000281 assert(SCCs[i] == C && "Index doesn't point to SCC!");
282 }
283
284 // Check that the SCCs are in fact in post-order.
285 for (int i = 0, Size = SCCs.size(); i < Size; ++i) {
286 SCC &SourceSCC = *SCCs[i];
287 for (Node &N : SourceSCC)
288 for (Edge &E : N) {
289 if (!E.isCall())
290 continue;
291 SCC &TargetSCC = *G->lookupSCC(*E.getNode());
292 if (&TargetSCC.getOuterRefSCC() == this) {
293 assert(SCCIndices.find(&TargetSCC)->second <= i &&
294 "Edge between SCCs violates post-order relationship.");
295 continue;
296 }
297 assert(TargetSCC.getOuterRefSCC().Parents.count(this) &&
298 "Edge to a RefSCC missing us in its parent set.");
299 }
300 }
Chandler Carruth5205c352016-12-07 01:42:40 +0000301
302 // Check that our parents are actually parents.
303 for (RefSCC *ParentRC : Parents) {
304 assert(ParentRC != this && "Cannot be our own parent!");
305 auto HasConnectingEdge = [&] {
306 for (SCC &C : *ParentRC)
307 for (Node &N : C)
308 for (Edge &E : N)
309 if (G->lookupRefSCC(*E.getNode()) == this)
310 return true;
311 return false;
312 };
313 assert(HasConnectingEdge() && "No edge connects the parent to us!");
314 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000315}
316#endif
317
318bool LazyCallGraph::RefSCC::isDescendantOf(const RefSCC &C) const {
Chandler Carruth4b096742014-05-01 12:12:42 +0000319 // Walk up the parents of this SCC and verify that we eventually find C.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000320 SmallVector<const RefSCC *, 4> AncestorWorklist;
Chandler Carruth4b096742014-05-01 12:12:42 +0000321 AncestorWorklist.push_back(this);
322 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000323 const RefSCC *AncestorC = AncestorWorklist.pop_back_val();
Chandler Carruth4b096742014-05-01 12:12:42 +0000324 if (AncestorC->isChildOf(C))
325 return true;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000326 for (const RefSCC *ParentC : AncestorC->Parents)
Chandler Carruth4b096742014-05-01 12:12:42 +0000327 AncestorWorklist.push_back(ParentC);
328 } while (!AncestorWorklist.empty());
329
330 return false;
331}
332
Chandler Carruth1f621f02016-09-04 08:34:24 +0000333/// Generic helper that updates a postorder sequence of SCCs for a potentially
334/// cycle-introducing edge insertion.
335///
336/// A postorder sequence of SCCs of a directed graph has one fundamental
337/// property: all deges in the DAG of SCCs point "up" the sequence. That is,
338/// all edges in the SCC DAG point to prior SCCs in the sequence.
339///
340/// This routine both updates a postorder sequence and uses that sequence to
341/// compute the set of SCCs connected into a cycle. It should only be called to
342/// insert a "downward" edge which will require changing the sequence to
343/// restore it to a postorder.
344///
345/// When inserting an edge from an earlier SCC to a later SCC in some postorder
346/// sequence, all of the SCCs which may be impacted are in the closed range of
347/// those two within the postorder sequence. The algorithm used here to restore
348/// the state is as follows:
349///
350/// 1) Starting from the source SCC, construct a set of SCCs which reach the
351/// source SCC consisting of just the source SCC. Then scan toward the
352/// target SCC in postorder and for each SCC, if it has an edge to an SCC
353/// in the set, add it to the set. Otherwise, the source SCC is not
354/// a successor, move it in the postorder sequence to immediately before
355/// the source SCC, shifting the source SCC and all SCCs in the set one
356/// position toward the target SCC. Stop scanning after processing the
357/// target SCC.
358/// 2) If the source SCC is now past the target SCC in the postorder sequence,
359/// and thus the new edge will flow toward the start, we are done.
360/// 3) Otherwise, starting from the target SCC, walk all edges which reach an
361/// SCC between the source and the target, and add them to the set of
362/// connected SCCs, then recurse through them. Once a complete set of the
363/// SCCs the target connects to is known, hoist the remaining SCCs between
364/// the source and the target to be above the target. Note that there is no
365/// need to process the source SCC, it is already known to connect.
366/// 4) At this point, all of the SCCs in the closed range between the source
367/// SCC and the target SCC in the postorder sequence are connected,
368/// including the target SCC and the source SCC. Inserting the edge from
369/// the source SCC to the target SCC will form a cycle out of precisely
370/// these SCCs. Thus we can merge all of the SCCs in this closed range into
371/// a single SCC.
372///
373/// This process has various important properties:
374/// - Only mutates the SCCs when adding the edge actually changes the SCC
375/// structure.
376/// - Never mutates SCCs which are unaffected by the change.
377/// - Updates the postorder sequence to correctly satisfy the postorder
378/// constraint after the edge is inserted.
379/// - Only reorders SCCs in the closed postorder sequence from the source to
380/// the target, so easy to bound how much has changed even in the ordering.
381/// - Big-O is the number of edges in the closed postorder range of SCCs from
382/// source to target.
383///
384/// This helper routine, in addition to updating the postorder sequence itself
385/// will also update a map from SCCs to indices within that sequecne.
386///
387/// The sequence and the map must operate on pointers to the SCC type.
388///
389/// Two callbacks must be provided. The first computes the subset of SCCs in
390/// the postorder closed range from the source to the target which connect to
391/// the source SCC via some (transitive) set of edges. The second computes the
392/// subset of the same range which the target SCC connects to via some
393/// (transitive) set of edges. Both callbacks should populate the set argument
394/// provided.
395template <typename SCCT, typename PostorderSequenceT, typename SCCIndexMapT,
396 typename ComputeSourceConnectedSetCallableT,
397 typename ComputeTargetConnectedSetCallableT>
398static iterator_range<typename PostorderSequenceT::iterator>
399updatePostorderSequenceForEdgeInsertion(
400 SCCT &SourceSCC, SCCT &TargetSCC, PostorderSequenceT &SCCs,
401 SCCIndexMapT &SCCIndices,
402 ComputeSourceConnectedSetCallableT ComputeSourceConnectedSet,
403 ComputeTargetConnectedSetCallableT ComputeTargetConnectedSet) {
404 int SourceIdx = SCCIndices[&SourceSCC];
405 int TargetIdx = SCCIndices[&TargetSCC];
406 assert(SourceIdx < TargetIdx && "Cannot have equal indices here!");
407
408 SmallPtrSet<SCCT *, 4> ConnectedSet;
409
410 // Compute the SCCs which (transitively) reach the source.
411 ComputeSourceConnectedSet(ConnectedSet);
412
413 // Partition the SCCs in this part of the port-order sequence so only SCCs
414 // connecting to the source remain between it and the target. This is
415 // a benign partition as it preserves postorder.
416 auto SourceI = std::stable_partition(
417 SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx + 1,
418 [&ConnectedSet](SCCT *C) { return !ConnectedSet.count(C); });
419 for (int i = SourceIdx, e = TargetIdx + 1; i < e; ++i)
420 SCCIndices.find(SCCs[i])->second = i;
421
422 // If the target doesn't connect to the source, then we've corrected the
423 // post-order and there are no cycles formed.
424 if (!ConnectedSet.count(&TargetSCC)) {
425 assert(SourceI > (SCCs.begin() + SourceIdx) &&
426 "Must have moved the source to fix the post-order.");
427 assert(*std::prev(SourceI) == &TargetSCC &&
428 "Last SCC to move should have bene the target.");
429
430 // Return an empty range at the target SCC indicating there is nothing to
431 // merge.
432 return make_range(std::prev(SourceI), std::prev(SourceI));
433 }
434
435 assert(SCCs[TargetIdx] == &TargetSCC &&
436 "Should not have moved target if connected!");
437 SourceIdx = SourceI - SCCs.begin();
438 assert(SCCs[SourceIdx] == &SourceSCC &&
439 "Bad updated index computation for the source SCC!");
440
441
442 // See whether there are any remaining intervening SCCs between the source
443 // and target. If so we need to make sure they all are reachable form the
444 // target.
445 if (SourceIdx + 1 < TargetIdx) {
446 ConnectedSet.clear();
447 ComputeTargetConnectedSet(ConnectedSet);
448
449 // Partition SCCs so that only SCCs reached from the target remain between
450 // the source and the target. This preserves postorder.
451 auto TargetI = std::stable_partition(
452 SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1,
453 [&ConnectedSet](SCCT *C) { return ConnectedSet.count(C); });
454 for (int i = SourceIdx + 1, e = TargetIdx + 1; i < e; ++i)
455 SCCIndices.find(SCCs[i])->second = i;
456 TargetIdx = std::prev(TargetI) - SCCs.begin();
457 assert(SCCs[TargetIdx] == &TargetSCC &&
458 "Should always end with the target!");
459 }
460
461 // At this point, we know that connecting source to target forms a cycle
462 // because target connects back to source, and we know that all of the SCCs
463 // between the source and target in the postorder sequence participate in that
464 // cycle.
465 return make_range(SCCs.begin() + SourceIdx, SCCs.begin() + TargetIdx);
466}
467
Chandler Carruthe5944d92016-02-17 00:18:16 +0000468SmallVector<LazyCallGraph::SCC *, 1>
469LazyCallGraph::RefSCC::switchInternalEdgeToCall(Node &SourceN, Node &TargetN) {
470 assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
Chandler Carruthe5944d92016-02-17 00:18:16 +0000471 SmallVector<SCC *, 1> DeletedSCCs;
Chandler Carruth5217c942014-04-30 10:48:36 +0000472
Chandler Carruth11b3f602016-09-04 08:34:31 +0000473#ifndef NDEBUG
474 // In a debug build, verify the RefSCC is valid to start with and when this
475 // routine finishes.
476 verify();
477 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
478#endif
479
Chandler Carruthe5944d92016-02-17 00:18:16 +0000480 SCC &SourceSCC = *G->lookupSCC(SourceN);
481 SCC &TargetSCC = *G->lookupSCC(TargetN);
482
483 // If the two nodes are already part of the same SCC, we're also done as
484 // we've just added more connectivity.
485 if (&SourceSCC == &TargetSCC) {
486 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000487 return DeletedSCCs;
488 }
489
490 // At this point we leverage the postorder list of SCCs to detect when the
491 // insertion of an edge changes the SCC structure in any way.
492 //
493 // First and foremost, we can eliminate the need for any changes when the
494 // edge is toward the beginning of the postorder sequence because all edges
495 // flow in that direction already. Thus adding a new one cannot form a cycle.
496 int SourceIdx = SCCIndices[&SourceSCC];
497 int TargetIdx = SCCIndices[&TargetSCC];
498 if (TargetIdx < SourceIdx) {
499 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000500 return DeletedSCCs;
501 }
502
Chandler Carruthe5944d92016-02-17 00:18:16 +0000503 // Compute the SCCs which (transitively) reach the source.
Chandler Carruth1f621f02016-09-04 08:34:24 +0000504 auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000505#ifndef NDEBUG
Chandler Carruth1f621f02016-09-04 08:34:24 +0000506 // Check that the RefSCC is still valid before computing this as the
507 // results will be nonsensical of we've broken its invariants.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000508 verify();
509#endif
Chandler Carruth1f621f02016-09-04 08:34:24 +0000510 ConnectedSet.insert(&SourceSCC);
511 auto IsConnected = [&](SCC &C) {
512 for (Node &N : C)
513 for (Edge &E : N.calls()) {
514 assert(E.getNode() && "Must have formed a node within an SCC!");
515 if (ConnectedSet.count(G->lookupSCC(*E.getNode())))
516 return true;
517 }
Chandler Carruthe5944d92016-02-17 00:18:16 +0000518
Chandler Carruth1f621f02016-09-04 08:34:24 +0000519 return false;
520 };
Chandler Carruthe5944d92016-02-17 00:18:16 +0000521
Chandler Carruth1f621f02016-09-04 08:34:24 +0000522 for (SCC *C :
523 make_range(SCCs.begin() + SourceIdx + 1, SCCs.begin() + TargetIdx + 1))
524 if (IsConnected(*C))
525 ConnectedSet.insert(C);
526 };
527
528 // Use a normal worklist to find which SCCs the target connects to. We still
529 // bound the search based on the range in the postorder list we care about,
530 // but because this is forward connectivity we just "recurse" through the
531 // edges.
532 auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<SCC *> &ConnectedSet) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000533#ifndef NDEBUG
Chandler Carruth1f621f02016-09-04 08:34:24 +0000534 // Check that the RefSCC is still valid before computing this as the
535 // results will be nonsensical of we've broken its invariants.
536 verify();
Chandler Carruthe5944d92016-02-17 00:18:16 +0000537#endif
Chandler Carruthe5944d92016-02-17 00:18:16 +0000538 ConnectedSet.insert(&TargetSCC);
539 SmallVector<SCC *, 4> Worklist;
540 Worklist.push_back(&TargetSCC);
541 do {
542 SCC &C = *Worklist.pop_back_val();
543 for (Node &N : C)
544 for (Edge &E : N) {
545 assert(E.getNode() && "Must have formed a node within an SCC!");
546 if (!E.isCall())
547 continue;
548 SCC &EdgeC = *G->lookupSCC(*E.getNode());
549 if (&EdgeC.getOuterRefSCC() != this)
550 // Not in this RefSCC...
551 continue;
552 if (SCCIndices.find(&EdgeC)->second <= SourceIdx)
553 // Not in the postorder sequence between source and target.
554 continue;
555
556 if (ConnectedSet.insert(&EdgeC).second)
557 Worklist.push_back(&EdgeC);
558 }
559 } while (!Worklist.empty());
Chandler Carruth1f621f02016-09-04 08:34:24 +0000560 };
Chandler Carruthe5944d92016-02-17 00:18:16 +0000561
Chandler Carruth1f621f02016-09-04 08:34:24 +0000562 // Use a generic helper to update the postorder sequence of SCCs and return
563 // a range of any SCCs connected into a cycle by inserting this edge. This
564 // routine will also take care of updating the indices into the postorder
565 // sequence.
566 auto MergeRange = updatePostorderSequenceForEdgeInsertion(
567 SourceSCC, TargetSCC, SCCs, SCCIndices, ComputeSourceConnectedSet,
568 ComputeTargetConnectedSet);
Chandler Carruthe5944d92016-02-17 00:18:16 +0000569
Chandler Carruth1f621f02016-09-04 08:34:24 +0000570 // If the merge range is empty, then adding the edge didn't actually form any
571 // new cycles. We're done.
572 if (MergeRange.begin() == MergeRange.end()) {
573 // Now that the SCC structure is finalized, flip the kind to call.
574 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
Chandler Carruth1f621f02016-09-04 08:34:24 +0000575 return DeletedSCCs;
Chandler Carruthe5944d92016-02-17 00:18:16 +0000576 }
577
Chandler Carruth1f621f02016-09-04 08:34:24 +0000578#ifndef NDEBUG
579 // Before merging, check that the RefSCC remains valid after all the
580 // postorder updates.
581 verify();
582#endif
583
584 // Otherwise we need to merge all of the SCCs in the cycle into a single
Chandler Carruthe5944d92016-02-17 00:18:16 +0000585 // result SCC.
586 //
587 // NB: We merge into the target because all of these functions were already
588 // reachable from the target, meaning any SCC-wide properties deduced about it
589 // other than the set of functions within it will not have changed.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000590 for (SCC *C : MergeRange) {
591 assert(C != &TargetSCC &&
592 "We merge *into* the target and shouldn't process it here!");
593 SCCIndices.erase(C);
594 TargetSCC.Nodes.append(C->Nodes.begin(), C->Nodes.end());
595 for (Node *N : C->Nodes)
596 G->SCCMap[N] = &TargetSCC;
597 C->clear();
598 DeletedSCCs.push_back(C);
599 }
600
601 // Erase the merged SCCs from the list and update the indices of the
602 // remaining SCCs.
603 int IndexOffset = MergeRange.end() - MergeRange.begin();
604 auto EraseEnd = SCCs.erase(MergeRange.begin(), MergeRange.end());
605 for (SCC *C : make_range(EraseEnd, SCCs.end()))
606 SCCIndices[C] -= IndexOffset;
607
608 // Now that the SCC structure is finalized, flip the kind to call.
609 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
610
Chandler Carruth11b3f602016-09-04 08:34:31 +0000611 // And we're done!
Chandler Carruthe5944d92016-02-17 00:18:16 +0000612 return DeletedSCCs;
Chandler Carruth5217c942014-04-30 10:48:36 +0000613}
614
Chandler Carruth443e57e2016-12-28 10:34:50 +0000615void LazyCallGraph::RefSCC::switchTrivialInternalEdgeToRef(Node &SourceN,
616 Node &TargetN) {
617 assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
618
619#ifndef NDEBUG
620 // In a debug build, verify the RefSCC is valid to start with and when this
621 // routine finishes.
622 verify();
623 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
624#endif
625
626 assert(G->lookupRefSCC(SourceN) == this &&
627 "Source must be in this RefSCC.");
628 assert(G->lookupRefSCC(TargetN) == this &&
629 "Target must be in this RefSCC.");
630 assert(G->lookupSCC(SourceN) != G->lookupSCC(TargetN) &&
631 "Source and Target must be in separate SCCs for this to be trivial!");
632
633 // Set the edge kind.
634 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
635}
636
Chandler Carruth88823462016-08-24 09:37:14 +0000637iterator_range<LazyCallGraph::RefSCC::iterator>
638LazyCallGraph::RefSCC::switchInternalEdgeToRef(Node &SourceN, Node &TargetN) {
Chandler Carruthe5944d92016-02-17 00:18:16 +0000639 assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
640
Chandler Carruth11b3f602016-09-04 08:34:31 +0000641#ifndef NDEBUG
642 // In a debug build, verify the RefSCC is valid to start with and when this
643 // routine finishes.
644 verify();
645 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
646#endif
647
Chandler Carruth443e57e2016-12-28 10:34:50 +0000648 assert(G->lookupRefSCC(SourceN) == this &&
Chandler Carruthe5944d92016-02-17 00:18:16 +0000649 "Source must be in this RefSCC.");
Chandler Carruth443e57e2016-12-28 10:34:50 +0000650 assert(G->lookupRefSCC(TargetN) == this &&
Chandler Carruthe5944d92016-02-17 00:18:16 +0000651 "Target must be in this RefSCC.");
652
Chandler Carruth443e57e2016-12-28 10:34:50 +0000653 SCC &TargetSCC = *G->lookupSCC(TargetN);
654 assert(G->lookupSCC(SourceN) == &TargetSCC && "Source and Target must be in "
655 "the same SCC to require the "
656 "full CG update.");
657
Chandler Carruthe5944d92016-02-17 00:18:16 +0000658 // Set the edge kind.
659 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
660
Chandler Carruthe5944d92016-02-17 00:18:16 +0000661 // Otherwise we are removing a call edge from a single SCC. This may break
662 // the cycle. In order to compute the new set of SCCs, we need to do a small
663 // DFS over the nodes within the SCC to form any sub-cycles that remain as
664 // distinct SCCs and compute a postorder over the resulting SCCs.
665 //
666 // However, we specially handle the target node. The target node is known to
667 // reach all other nodes in the original SCC by definition. This means that
668 // we want the old SCC to be replaced with an SCC contaning that node as it
669 // will be the root of whatever SCC DAG results from the DFS. Assumptions
670 // about an SCC such as the set of functions called will continue to hold,
671 // etc.
672
673 SCC &OldSCC = TargetSCC;
674 SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
675 SmallVector<Node *, 16> PendingSCCStack;
676 SmallVector<SCC *, 4> NewSCCs;
677
678 // Prepare the nodes for a fresh DFS.
679 SmallVector<Node *, 16> Worklist;
680 Worklist.swap(OldSCC.Nodes);
681 for (Node *N : Worklist) {
682 N->DFSNumber = N->LowLink = 0;
683 G->SCCMap.erase(N);
684 }
685
686 // Force the target node to be in the old SCC. This also enables us to take
687 // a very significant short-cut in the standard Tarjan walk to re-form SCCs
688 // below: whenever we build an edge that reaches the target node, we know
689 // that the target node eventually connects back to all other nodes in our
690 // walk. As a consequence, we can detect and handle participants in that
691 // cycle without walking all the edges that form this connection, and instead
692 // by relying on the fundamental guarantee coming into this operation (all
693 // nodes are reachable from the target due to previously forming an SCC).
694 TargetN.DFSNumber = TargetN.LowLink = -1;
695 OldSCC.Nodes.push_back(&TargetN);
696 G->SCCMap[&TargetN] = &OldSCC;
697
698 // Scan down the stack and DFS across the call edges.
699 for (Node *RootN : Worklist) {
700 assert(DFSStack.empty() &&
701 "Cannot begin a new root with a non-empty DFS stack!");
702 assert(PendingSCCStack.empty() &&
703 "Cannot begin a new root with pending nodes for an SCC!");
704
705 // Skip any nodes we've already reached in the DFS.
706 if (RootN->DFSNumber != 0) {
707 assert(RootN->DFSNumber == -1 &&
708 "Shouldn't have any mid-DFS root nodes!");
709 continue;
710 }
711
712 RootN->DFSNumber = RootN->LowLink = 1;
713 int NextDFSNumber = 2;
714
715 DFSStack.push_back({RootN, RootN->call_begin()});
716 do {
717 Node *N;
718 call_edge_iterator I;
719 std::tie(N, I) = DFSStack.pop_back_val();
720 auto E = N->call_end();
721 while (I != E) {
722 Node &ChildN = *I->getNode();
723 if (ChildN.DFSNumber == 0) {
724 // We haven't yet visited this child, so descend, pushing the current
725 // node onto the stack.
726 DFSStack.push_back({N, I});
727
728 assert(!G->SCCMap.count(&ChildN) &&
729 "Found a node with 0 DFS number but already in an SCC!");
730 ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
731 N = &ChildN;
732 I = N->call_begin();
733 E = N->call_end();
734 continue;
735 }
736
737 // Check for the child already being part of some component.
738 if (ChildN.DFSNumber == -1) {
739 if (G->lookupSCC(ChildN) == &OldSCC) {
740 // If the child is part of the old SCC, we know that it can reach
741 // every other node, so we have formed a cycle. Pull the entire DFS
742 // and pending stacks into it. See the comment above about setting
743 // up the old SCC for why we do this.
744 int OldSize = OldSCC.size();
745 OldSCC.Nodes.push_back(N);
746 OldSCC.Nodes.append(PendingSCCStack.begin(), PendingSCCStack.end());
747 PendingSCCStack.clear();
748 while (!DFSStack.empty())
749 OldSCC.Nodes.push_back(DFSStack.pop_back_val().first);
750 for (Node &N : make_range(OldSCC.begin() + OldSize, OldSCC.end())) {
751 N.DFSNumber = N.LowLink = -1;
752 G->SCCMap[&N] = &OldSCC;
753 }
754 N = nullptr;
755 break;
756 }
757
758 // If the child has already been added to some child component, it
759 // couldn't impact the low-link of this parent because it isn't
760 // connected, and thus its low-link isn't relevant so skip it.
761 ++I;
762 continue;
763 }
764
765 // Track the lowest linked child as the lowest link for this node.
766 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
767 if (ChildN.LowLink < N->LowLink)
768 N->LowLink = ChildN.LowLink;
769
770 // Move to the next edge.
771 ++I;
772 }
773 if (!N)
774 // Cleared the DFS early, start another round.
775 break;
776
777 // We've finished processing N and its descendents, put it on our pending
778 // SCC stack to eventually get merged into an SCC of nodes.
779 PendingSCCStack.push_back(N);
780
781 // If this node is linked to some lower entry, continue walking up the
782 // stack.
783 if (N->LowLink != N->DFSNumber)
784 continue;
785
786 // Otherwise, we've completed an SCC. Append it to our post order list of
787 // SCCs.
788 int RootDFSNumber = N->DFSNumber;
789 // Find the range of the node stack by walking down until we pass the
790 // root DFS number.
791 auto SCCNodes = make_range(
792 PendingSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +0000793 find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
794 return N->DFSNumber < RootDFSNumber;
795 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +0000796
797 // Form a new SCC out of these nodes and then clear them off our pending
798 // stack.
799 NewSCCs.push_back(G->createSCC(*this, SCCNodes));
800 for (Node &N : *NewSCCs.back()) {
801 N.DFSNumber = N.LowLink = -1;
802 G->SCCMap[&N] = NewSCCs.back();
803 }
804 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
805 } while (!DFSStack.empty());
806 }
807
808 // Insert the remaining SCCs before the old one. The old SCC can reach all
809 // other SCCs we form because it contains the target node of the removed edge
810 // of the old SCC. This means that we will have edges into all of the new
811 // SCCs, which means the old one must come last for postorder.
812 int OldIdx = SCCIndices[&OldSCC];
813 SCCs.insert(SCCs.begin() + OldIdx, NewSCCs.begin(), NewSCCs.end());
814
815 // Update the mapping from SCC* to index to use the new SCC*s, and remove the
816 // old SCC from the mapping.
817 for (int Idx = OldIdx, Size = SCCs.size(); Idx < Size; ++Idx)
818 SCCIndices[SCCs[Idx]] = Idx;
819
Chandler Carruth88823462016-08-24 09:37:14 +0000820 return make_range(SCCs.begin() + OldIdx,
821 SCCs.begin() + OldIdx + NewSCCs.size());
Chandler Carruthe5944d92016-02-17 00:18:16 +0000822}
823
824void LazyCallGraph::RefSCC::switchOutgoingEdgeToCall(Node &SourceN,
825 Node &TargetN) {
826 assert(!SourceN[TargetN].isCall() && "Must start with a ref edge!");
827
828 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
829 assert(G->lookupRefSCC(TargetN) != this &&
830 "Target must not be in this RefSCC.");
831 assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
832 "Target must be a descendant of the Source.");
833
834 // Edges between RefSCCs are the same regardless of call or ref, so we can
835 // just flip the edge here.
836 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Call);
837
838#ifndef NDEBUG
839 // Check that the RefSCC is still valid.
840 verify();
841#endif
842}
843
844void LazyCallGraph::RefSCC::switchOutgoingEdgeToRef(Node &SourceN,
845 Node &TargetN) {
846 assert(SourceN[TargetN].isCall() && "Must start with a call edge!");
847
848 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
849 assert(G->lookupRefSCC(TargetN) != this &&
850 "Target must not be in this RefSCC.");
851 assert(G->lookupRefSCC(TargetN)->isDescendantOf(*this) &&
852 "Target must be a descendant of the Source.");
853
854 // Edges between RefSCCs are the same regardless of call or ref, so we can
855 // just flip the edge here.
856 SourceN.setEdgeKind(TargetN.getFunction(), Edge::Ref);
857
858#ifndef NDEBUG
859 // Check that the RefSCC is still valid.
860 verify();
861#endif
862}
863
864void LazyCallGraph::RefSCC::insertInternalRefEdge(Node &SourceN,
865 Node &TargetN) {
866 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
867 assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
868
869 SourceN.insertEdgeInternal(TargetN, Edge::Ref);
870
871#ifndef NDEBUG
872 // Check that the RefSCC is still valid.
873 verify();
874#endif
875}
876
877void LazyCallGraph::RefSCC::insertOutgoingEdge(Node &SourceN, Node &TargetN,
878 Edge::Kind EK) {
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000879 // First insert it into the caller.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000880 SourceN.insertEdgeInternal(TargetN, EK);
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000881
Chandler Carruthe5944d92016-02-17 00:18:16 +0000882 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000883
Chandler Carruthe5944d92016-02-17 00:18:16 +0000884 RefSCC &TargetC = *G->lookupRefSCC(TargetN);
885 assert(&TargetC != this && "Target must not be in this RefSCC.");
886 assert(TargetC.isDescendantOf(*this) &&
887 "Target must be a descendant of the Source.");
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000888
Chandler Carruth91539112015-12-28 01:54:20 +0000889 // The only change required is to add this SCC to the parent set of the
890 // callee.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000891 TargetC.Parents.insert(this);
892
893#ifndef NDEBUG
894 // Check that the RefSCC is still valid.
895 verify();
896#endif
Chandler Carruth7cc4ed82014-05-01 12:18:20 +0000897}
898
Chandler Carruthe5944d92016-02-17 00:18:16 +0000899SmallVector<LazyCallGraph::RefSCC *, 1>
900LazyCallGraph::RefSCC::insertIncomingRefEdge(Node &SourceN, Node &TargetN) {
Chandler Carruth49d728a2016-09-16 10:20:17 +0000901 assert(G->lookupRefSCC(TargetN) == this && "Target must be in this RefSCC.");
902 RefSCC &SourceC = *G->lookupRefSCC(SourceN);
903 assert(&SourceC != this && "Source must not be in this RefSCC.");
904 assert(SourceC.isDescendantOf(*this) &&
905 "Source must be a descendant of the Target.");
906
907 SmallVector<RefSCC *, 1> DeletedRefSCCs;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000908
Chandler Carruth11b3f602016-09-04 08:34:31 +0000909#ifndef NDEBUG
910 // In a debug build, verify the RefSCC is valid to start with and when this
911 // routine finishes.
912 verify();
913 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
914#endif
915
Chandler Carruth49d728a2016-09-16 10:20:17 +0000916 int SourceIdx = G->RefSCCIndices[&SourceC];
917 int TargetIdx = G->RefSCCIndices[this];
918 assert(SourceIdx < TargetIdx &&
919 "Postorder list doesn't see edge as incoming!");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000920
Chandler Carruth49d728a2016-09-16 10:20:17 +0000921 // Compute the RefSCCs which (transitively) reach the source. We do this by
922 // working backwards from the source using the parent set in each RefSCC,
923 // skipping any RefSCCs that don't fall in the postorder range. This has the
924 // advantage of walking the sparser parent edge (in high fan-out graphs) but
925 // more importantly this removes examining all forward edges in all RefSCCs
926 // within the postorder range which aren't in fact connected. Only connected
927 // RefSCCs (and their edges) are visited here.
928 auto ComputeSourceConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) {
929 Set.insert(&SourceC);
930 SmallVector<RefSCC *, 4> Worklist;
931 Worklist.push_back(&SourceC);
932 do {
933 RefSCC &RC = *Worklist.pop_back_val();
934 for (RefSCC &ParentRC : RC.parents()) {
935 // Skip any RefSCCs outside the range of source to target in the
936 // postorder sequence.
937 int ParentIdx = G->getRefSCCIndex(ParentRC);
938 assert(ParentIdx > SourceIdx && "Parent cannot precede source in postorder!");
939 if (ParentIdx > TargetIdx)
940 continue;
941 if (Set.insert(&ParentRC).second)
942 // First edge connecting to this parent, add it to our worklist.
943 Worklist.push_back(&ParentRC);
Chandler Carruth312dddf2014-05-04 09:38:32 +0000944 }
Chandler Carruth49d728a2016-09-16 10:20:17 +0000945 } while (!Worklist.empty());
946 };
Chandler Carruth312dddf2014-05-04 09:38:32 +0000947
Chandler Carruth49d728a2016-09-16 10:20:17 +0000948 // Use a normal worklist to find which SCCs the target connects to. We still
949 // bound the search based on the range in the postorder list we care about,
950 // but because this is forward connectivity we just "recurse" through the
951 // edges.
952 auto ComputeTargetConnectedSet = [&](SmallPtrSetImpl<RefSCC *> &Set) {
953 Set.insert(this);
954 SmallVector<RefSCC *, 4> Worklist;
955 Worklist.push_back(this);
956 do {
957 RefSCC &RC = *Worklist.pop_back_val();
958 for (SCC &C : RC)
959 for (Node &N : C)
960 for (Edge &E : N) {
961 assert(E.getNode() && "Must have formed a node!");
962 RefSCC &EdgeRC = *G->lookupRefSCC(*E.getNode());
963 if (G->getRefSCCIndex(EdgeRC) <= SourceIdx)
964 // Not in the postorder sequence between source and target.
965 continue;
Chandler Carruth312dddf2014-05-04 09:38:32 +0000966
Chandler Carruth49d728a2016-09-16 10:20:17 +0000967 if (Set.insert(&EdgeRC).second)
968 Worklist.push_back(&EdgeRC);
969 }
970 } while (!Worklist.empty());
971 };
972
973 // Use a generic helper to update the postorder sequence of RefSCCs and return
974 // a range of any RefSCCs connected into a cycle by inserting this edge. This
975 // routine will also take care of updating the indices into the postorder
976 // sequence.
977 iterator_range<SmallVectorImpl<RefSCC *>::iterator> MergeRange =
978 updatePostorderSequenceForEdgeInsertion(
979 SourceC, *this, G->PostOrderRefSCCs, G->RefSCCIndices,
980 ComputeSourceConnectedSet, ComputeTargetConnectedSet);
981
Chandler Carruth5205c352016-12-07 01:42:40 +0000982 // Build a set so we can do fast tests for whether a RefSCC will end up as
983 // part of the merged RefSCC.
Chandler Carruth49d728a2016-09-16 10:20:17 +0000984 SmallPtrSet<RefSCC *, 16> MergeSet(MergeRange.begin(), MergeRange.end());
Chandler Carruth312dddf2014-05-04 09:38:32 +0000985
Chandler Carruth5205c352016-12-07 01:42:40 +0000986 // This RefSCC will always be part of that set, so just insert it here.
987 MergeSet.insert(this);
988
Chandler Carruth312dddf2014-05-04 09:38:32 +0000989 // Now that we have identified all of the SCCs which need to be merged into
990 // a connected set with the inserted edge, merge all of them into this SCC.
Chandler Carruthe5944d92016-02-17 00:18:16 +0000991 SmallVector<SCC *, 16> MergedSCCs;
992 int SCCIndex = 0;
Chandler Carruth49d728a2016-09-16 10:20:17 +0000993 for (RefSCC *RC : MergeRange) {
994 assert(RC != this && "We're merging into the target RefSCC, so it "
995 "shouldn't be in the range.");
Chandler Carruth312dddf2014-05-04 09:38:32 +0000996
Chandler Carruthe5944d92016-02-17 00:18:16 +0000997 // Merge the parents which aren't part of the merge into the our parents.
Chandler Carruth49d728a2016-09-16 10:20:17 +0000998 for (RefSCC *ParentRC : RC->Parents)
999 if (!MergeSet.count(ParentRC))
1000 Parents.insert(ParentRC);
1001 RC->Parents.clear();
Chandler Carruthe5944d92016-02-17 00:18:16 +00001002
1003 // Walk the inner SCCs to update their up-pointer and walk all the edges to
1004 // update any parent sets.
1005 // FIXME: We should try to find a way to avoid this (rather expensive) edge
1006 // walk by updating the parent sets in some other manner.
Chandler Carruth49d728a2016-09-16 10:20:17 +00001007 for (SCC &InnerC : *RC) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001008 InnerC.OuterRefSCC = this;
1009 SCCIndices[&InnerC] = SCCIndex++;
1010 for (Node &N : InnerC) {
1011 G->SCCMap[&N] = &InnerC;
1012 for (Edge &E : N) {
1013 assert(E.getNode() &&
1014 "Cannot have a null node within a visited SCC!");
1015 RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
Chandler Carruth49d728a2016-09-16 10:20:17 +00001016 if (MergeSet.count(&ChildRC))
Chandler Carruthe5944d92016-02-17 00:18:16 +00001017 continue;
Chandler Carruth49d728a2016-09-16 10:20:17 +00001018 ChildRC.Parents.erase(RC);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001019 ChildRC.Parents.insert(this);
1020 }
Chandler Carruth312dddf2014-05-04 09:38:32 +00001021 }
Chandler Carruth312dddf2014-05-04 09:38:32 +00001022 }
Chandler Carruthe5944d92016-02-17 00:18:16 +00001023
1024 // Now merge in the SCCs. We can actually move here so try to reuse storage
1025 // the first time through.
1026 if (MergedSCCs.empty())
Chandler Carruth49d728a2016-09-16 10:20:17 +00001027 MergedSCCs = std::move(RC->SCCs);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001028 else
Chandler Carruth49d728a2016-09-16 10:20:17 +00001029 MergedSCCs.append(RC->SCCs.begin(), RC->SCCs.end());
1030 RC->SCCs.clear();
1031 DeletedRefSCCs.push_back(RC);
Chandler Carruth312dddf2014-05-04 09:38:32 +00001032 }
Chandler Carruthe5944d92016-02-17 00:18:16 +00001033
Chandler Carruth49d728a2016-09-16 10:20:17 +00001034 // Append our original SCCs to the merged list and move it into place.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001035 for (SCC &InnerC : *this)
1036 SCCIndices[&InnerC] = SCCIndex++;
1037 MergedSCCs.append(SCCs.begin(), SCCs.end());
1038 SCCs = std::move(MergedSCCs);
1039
Chandler Carruth49d728a2016-09-16 10:20:17 +00001040 // Remove the merged away RefSCCs from the post order sequence.
1041 for (RefSCC *RC : MergeRange)
1042 G->RefSCCIndices.erase(RC);
1043 int IndexOffset = MergeRange.end() - MergeRange.begin();
1044 auto EraseEnd =
1045 G->PostOrderRefSCCs.erase(MergeRange.begin(), MergeRange.end());
1046 for (RefSCC *RC : make_range(EraseEnd, G->PostOrderRefSCCs.end()))
1047 G->RefSCCIndices[RC] -= IndexOffset;
1048
Chandler Carruthe5944d92016-02-17 00:18:16 +00001049 // At this point we have a merged RefSCC with a post-order SCCs list, just
1050 // connect the nodes to form the new edge.
1051 SourceN.insertEdgeInternal(TargetN, Edge::Ref);
1052
Chandler Carruth312dddf2014-05-04 09:38:32 +00001053 // We return the list of SCCs which were merged so that callers can
1054 // invalidate any data they have associated with those SCCs. Note that these
1055 // SCCs are no longer in an interesting state (they are totally empty) but
1056 // the pointers will remain stable for the life of the graph itself.
Chandler Carruth49d728a2016-09-16 10:20:17 +00001057 return DeletedRefSCCs;
Chandler Carruth312dddf2014-05-04 09:38:32 +00001058}
1059
Chandler Carruthe5944d92016-02-17 00:18:16 +00001060void LazyCallGraph::RefSCC::removeOutgoingEdge(Node &SourceN, Node &TargetN) {
1061 assert(G->lookupRefSCC(SourceN) == this &&
1062 "The source must be a member of this RefSCC.");
1063
1064 RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
1065 assert(&TargetRC != this && "The target must not be a member of this RefSCC");
1066
David Majnemer0d955d02016-08-11 22:21:41 +00001067 assert(!is_contained(G->LeafRefSCCs, this) &&
Chandler Carruthe5944d92016-02-17 00:18:16 +00001068 "Cannot have a leaf RefSCC source.");
1069
Chandler Carruth11b3f602016-09-04 08:34:31 +00001070#ifndef NDEBUG
1071 // In a debug build, verify the RefSCC is valid to start with and when this
1072 // routine finishes.
1073 verify();
1074 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
1075#endif
1076
Chandler Carruthaa839b22014-04-27 01:59:50 +00001077 // First remove it from the node.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001078 SourceN.removeEdgeInternal(TargetN.getFunction());
Chandler Carruthaa839b22014-04-27 01:59:50 +00001079
Chandler Carruthe5944d92016-02-17 00:18:16 +00001080 bool HasOtherEdgeToChildRC = false;
1081 bool HasOtherChildRC = false;
1082 for (SCC *InnerC : SCCs) {
1083 for (Node &N : *InnerC) {
1084 for (Edge &E : N) {
1085 assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
1086 RefSCC &OtherChildRC = *G->lookupRefSCC(*E.getNode());
1087 if (&OtherChildRC == &TargetRC) {
1088 HasOtherEdgeToChildRC = true;
1089 break;
1090 }
1091 if (&OtherChildRC != this)
1092 HasOtherChildRC = true;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001093 }
Chandler Carruthe5944d92016-02-17 00:18:16 +00001094 if (HasOtherEdgeToChildRC)
1095 break;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001096 }
Chandler Carruthe5944d92016-02-17 00:18:16 +00001097 if (HasOtherEdgeToChildRC)
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001098 break;
1099 }
1100 // Because the SCCs form a DAG, deleting such an edge cannot change the set
1101 // of SCCs in the graph. However, it may cut an edge of the SCC DAG, making
Chandler Carruthe5944d92016-02-17 00:18:16 +00001102 // the source SCC no longer connected to the target SCC. If so, we need to
1103 // update the target SCC's map of its parents.
1104 if (!HasOtherEdgeToChildRC) {
1105 bool Removed = TargetRC.Parents.erase(this);
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001106 (void)Removed;
1107 assert(Removed &&
Chandler Carruthe5944d92016-02-17 00:18:16 +00001108 "Did not find the source SCC in the target SCC's parent list!");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001109
1110 // It may orphan an SCC if it is the last edge reaching it, but that does
1111 // not violate any invariants of the graph.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001112 if (TargetRC.Parents.empty())
1113 DEBUG(dbgs() << "LCG: Update removing " << SourceN.getFunction().getName()
1114 << " -> " << TargetN.getFunction().getName()
Chandler Carruthaa839b22014-04-27 01:59:50 +00001115 << " edge orphaned the callee's SCC!\n");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001116
Chandler Carruthe5944d92016-02-17 00:18:16 +00001117 // It may make the Source SCC a leaf SCC.
1118 if (!HasOtherChildRC)
1119 G->LeafRefSCCs.push_back(this);
Chandler Carruthaca48d02014-04-26 09:06:53 +00001120 }
1121}
1122
Chandler Carruthe5944d92016-02-17 00:18:16 +00001123SmallVector<LazyCallGraph::RefSCC *, 1>
1124LazyCallGraph::RefSCC::removeInternalRefEdge(Node &SourceN, Node &TargetN) {
1125 assert(!SourceN[TargetN].isCall() &&
1126 "Cannot remove a call edge, it must first be made a ref edge");
Chandler Carruthaa839b22014-04-27 01:59:50 +00001127
Chandler Carruth11b3f602016-09-04 08:34:31 +00001128#ifndef NDEBUG
1129 // In a debug build, verify the RefSCC is valid to start with and when this
1130 // routine finishes.
1131 verify();
1132 auto VerifyOnExit = make_scope_exit([&]() { verify(); });
1133#endif
1134
Chandler Carruthe5944d92016-02-17 00:18:16 +00001135 // First remove the actual edge.
1136 SourceN.removeEdgeInternal(TargetN.getFunction());
1137
1138 // We return a list of the resulting *new* RefSCCs in post-order.
1139 SmallVector<RefSCC *, 1> Result;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001140
Chandler Carrutha7205b62014-04-26 03:36:37 +00001141 // Direct recursion doesn't impact the SCC graph at all.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001142 if (&SourceN == &TargetN)
1143 return Result;
Chandler Carrutha7205b62014-04-26 03:36:37 +00001144
Chandler Carruthc6334572016-12-28 02:24:58 +00001145 // If this ref edge is within an SCC then there are sufficient other edges to
1146 // form a cycle without this edge so removing it is a no-op.
1147 SCC &SourceC = *G->lookupSCC(SourceN);
1148 SCC &TargetC = *G->lookupSCC(TargetN);
1149 if (&SourceC == &TargetC)
1150 return Result;
1151
Chandler Carruthe5944d92016-02-17 00:18:16 +00001152 // We build somewhat synthetic new RefSCCs by providing a postorder mapping
1153 // for each inner SCC. We also store these associated with *nodes* rather
1154 // than SCCs because this saves a round-trip through the node->SCC map and in
1155 // the common case, SCCs are small. We will verify that we always give the
1156 // same number to every node in the SCC such that these are equivalent.
1157 const int RootPostOrderNumber = 0;
1158 int PostOrderNumber = RootPostOrderNumber + 1;
1159 SmallDenseMap<Node *, int> PostOrderMapping;
1160
1161 // Every node in the target SCC can already reach every node in this RefSCC
1162 // (by definition). It is the only node we know will stay inside this RefSCC.
1163 // Everything which transitively reaches Target will also remain in the
1164 // RefSCC. We handle this by pre-marking that the nodes in the target SCC map
1165 // back to the root post order number.
1166 //
1167 // This also enables us to take a very significant short-cut in the standard
1168 // Tarjan walk to re-form RefSCCs below: whenever we build an edge that
1169 // references the target node, we know that the target node eventually
1170 // references all other nodes in our walk. As a consequence, we can detect
1171 // and handle participants in that cycle without walking all the edges that
1172 // form the connections, and instead by relying on the fundamental guarantee
1173 // coming into this operation.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001174 for (Node &N : TargetC)
1175 PostOrderMapping[&N] = RootPostOrderNumber;
1176
1177 // Reset all the other nodes to prepare for a DFS over them, and add them to
1178 // our worklist.
1179 SmallVector<Node *, 8> Worklist;
1180 for (SCC *C : SCCs) {
1181 if (C == &TargetC)
1182 continue;
1183
1184 for (Node &N : *C)
1185 N.DFSNumber = N.LowLink = 0;
1186
1187 Worklist.append(C->Nodes.begin(), C->Nodes.end());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001188 }
1189
Chandler Carruthe5944d92016-02-17 00:18:16 +00001190 auto MarkNodeForSCCNumber = [&PostOrderMapping](Node &N, int Number) {
1191 N.DFSNumber = N.LowLink = -1;
1192 PostOrderMapping[&N] = Number;
1193 };
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001194
Chandler Carruthe5944d92016-02-17 00:18:16 +00001195 SmallVector<std::pair<Node *, edge_iterator>, 4> DFSStack;
1196 SmallVector<Node *, 4> PendingRefSCCStack;
Chandler Carruthaca48d02014-04-26 09:06:53 +00001197 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001198 assert(DFSStack.empty() &&
1199 "Cannot begin a new root with a non-empty DFS stack!");
1200 assert(PendingRefSCCStack.empty() &&
1201 "Cannot begin a new root with pending nodes for an SCC!");
1202
1203 Node *RootN = Worklist.pop_back_val();
1204 // Skip any nodes we've already reached in the DFS.
1205 if (RootN->DFSNumber != 0) {
1206 assert(RootN->DFSNumber == -1 &&
1207 "Shouldn't have any mid-DFS root nodes!");
1208 continue;
1209 }
1210
1211 RootN->DFSNumber = RootN->LowLink = 1;
1212 int NextDFSNumber = 2;
1213
1214 DFSStack.push_back({RootN, RootN->begin()});
1215 do {
1216 Node *N;
1217 edge_iterator I;
1218 std::tie(N, I) = DFSStack.pop_back_val();
1219 auto E = N->end();
1220
1221 assert(N->DFSNumber != 0 && "We should always assign a DFS number "
1222 "before processing a node.");
1223
1224 while (I != E) {
1225 Node &ChildN = I->getNode(*G);
1226 if (ChildN.DFSNumber == 0) {
1227 // Mark that we should start at this child when next this node is the
1228 // top of the stack. We don't start at the next child to ensure this
1229 // child's lowlink is reflected.
1230 DFSStack.push_back({N, I});
1231
1232 // Continue, resetting to the child node.
1233 ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
1234 N = &ChildN;
1235 I = ChildN.begin();
1236 E = ChildN.end();
1237 continue;
1238 }
1239 if (ChildN.DFSNumber == -1) {
1240 // Check if this edge's target node connects to the deleted edge's
1241 // target node. If so, we know that every node connected will end up
1242 // in this RefSCC, so collapse the entire current stack into the root
1243 // slot in our SCC numbering. See above for the motivation of
1244 // optimizing the target connected nodes in this way.
1245 auto PostOrderI = PostOrderMapping.find(&ChildN);
1246 if (PostOrderI != PostOrderMapping.end() &&
1247 PostOrderI->second == RootPostOrderNumber) {
1248 MarkNodeForSCCNumber(*N, RootPostOrderNumber);
1249 while (!PendingRefSCCStack.empty())
1250 MarkNodeForSCCNumber(*PendingRefSCCStack.pop_back_val(),
1251 RootPostOrderNumber);
1252 while (!DFSStack.empty())
1253 MarkNodeForSCCNumber(*DFSStack.pop_back_val().first,
1254 RootPostOrderNumber);
1255 // Ensure we break all the way out of the enclosing loop.
1256 N = nullptr;
1257 break;
1258 }
1259
1260 // If this child isn't currently in this RefSCC, no need to process
Chandler Carruth23a6c3f2016-12-06 10:29:23 +00001261 // it. However, we do need to remove this RefSCC from its RefSCC's
1262 // parent set.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001263 RefSCC &ChildRC = *G->lookupRefSCC(ChildN);
1264 ChildRC.Parents.erase(this);
1265 ++I;
1266 continue;
1267 }
1268
1269 // Track the lowest link of the children, if any are still in the stack.
1270 // Any child not on the stack will have a LowLink of -1.
1271 assert(ChildN.LowLink != 0 &&
1272 "Low-link must not be zero with a non-zero DFS number.");
1273 if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
1274 N->LowLink = ChildN.LowLink;
1275 ++I;
1276 }
1277 if (!N)
1278 // We short-circuited this node.
1279 break;
1280
1281 // We've finished processing N and its descendents, put it on our pending
1282 // stack to eventually get merged into a RefSCC.
1283 PendingRefSCCStack.push_back(N);
1284
1285 // If this node is linked to some lower entry, continue walking up the
1286 // stack.
1287 if (N->LowLink != N->DFSNumber) {
1288 assert(!DFSStack.empty() &&
1289 "We never found a viable root for a RefSCC to pop off!");
1290 continue;
1291 }
1292
1293 // Otherwise, form a new RefSCC from the top of the pending node stack.
1294 int RootDFSNumber = N->DFSNumber;
1295 // Find the range of the node stack by walking down until we pass the
1296 // root DFS number.
1297 auto RefSCCNodes = make_range(
1298 PendingRefSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +00001299 find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1300 return N->DFSNumber < RootDFSNumber;
1301 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +00001302
1303 // Mark the postorder number for these nodes and clear them off the
1304 // stack. We'll use the postorder number to pull them into RefSCCs at the
1305 // end. FIXME: Fuse with the loop above.
1306 int RefSCCNumber = PostOrderNumber++;
1307 for (Node *N : RefSCCNodes)
1308 MarkNodeForSCCNumber(*N, RefSCCNumber);
1309
1310 PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1311 PendingRefSCCStack.end());
1312 } while (!DFSStack.empty());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001313
Chandler Carruthaca48d02014-04-26 09:06:53 +00001314 assert(DFSStack.empty() && "Didn't flush the entire DFS stack!");
Chandler Carruthe5944d92016-02-17 00:18:16 +00001315 assert(PendingRefSCCStack.empty() && "Didn't flush all pending nodes!");
Chandler Carruthaca48d02014-04-26 09:06:53 +00001316 } while (!Worklist.empty());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001317
Chandler Carruthe5944d92016-02-17 00:18:16 +00001318 // We now have a post-order numbering for RefSCCs and a mapping from each
1319 // node in this RefSCC to its final RefSCC. We create each new RefSCC node
1320 // (re-using this RefSCC node for the root) and build a radix-sort style map
1321 // from postorder number to the RefSCC. We then append SCCs to each of these
1322 // RefSCCs in the order they occured in the original SCCs container.
1323 for (int i = 1; i < PostOrderNumber; ++i)
1324 Result.push_back(G->createRefSCC(*G));
1325
Chandler Carruth49d728a2016-09-16 10:20:17 +00001326 // Insert the resulting postorder sequence into the global graph postorder
1327 // sequence before the current RefSCC in that sequence. The idea being that
1328 // this RefSCC is the target of the reference edge removed, and thus has
1329 // a direct or indirect edge to every other RefSCC formed and so must be at
1330 // the end of any postorder traversal.
1331 //
1332 // FIXME: It'd be nice to change the APIs so that we returned an iterator
1333 // range over the global postorder sequence and generally use that sequence
1334 // rather than building a separate result vector here.
1335 if (!Result.empty()) {
1336 int Idx = G->getRefSCCIndex(*this);
1337 G->PostOrderRefSCCs.insert(G->PostOrderRefSCCs.begin() + Idx,
1338 Result.begin(), Result.end());
1339 for (int i : seq<int>(Idx, G->PostOrderRefSCCs.size()))
1340 G->RefSCCIndices[G->PostOrderRefSCCs[i]] = i;
1341 assert(G->PostOrderRefSCCs[G->getRefSCCIndex(*this)] == this &&
1342 "Failed to update this RefSCC's index after insertion!");
1343 }
1344
Chandler Carruthe5944d92016-02-17 00:18:16 +00001345 for (SCC *C : SCCs) {
1346 auto PostOrderI = PostOrderMapping.find(&*C->begin());
1347 assert(PostOrderI != PostOrderMapping.end() &&
1348 "Cannot have missing mappings for nodes!");
1349 int SCCNumber = PostOrderI->second;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001350#ifndef NDEBUG
Chandler Carruthe5944d92016-02-17 00:18:16 +00001351 for (Node &N : *C)
1352 assert(PostOrderMapping.find(&N)->second == SCCNumber &&
1353 "Cannot have different numbers for nodes in the same SCC!");
1354#endif
1355 if (SCCNumber == 0)
1356 // The root node is handled separately by removing the SCCs.
1357 continue;
1358
1359 RefSCC &RC = *Result[SCCNumber - 1];
1360 int SCCIndex = RC.SCCs.size();
1361 RC.SCCs.push_back(C);
Chandler Carruth23a6c3f2016-12-06 10:29:23 +00001362 RC.SCCIndices[C] = SCCIndex;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001363 C->OuterRefSCC = &RC;
1364 }
1365
1366 // FIXME: We re-walk the edges in each RefSCC to establish whether it is
1367 // a leaf and connect it to the rest of the graph's parents lists. This is
1368 // really wasteful. We should instead do this during the DFS to avoid yet
1369 // another edge walk.
1370 for (RefSCC *RC : Result)
1371 G->connectRefSCC(*RC);
1372
1373 // Now erase all but the root's SCCs.
David Majnemer42531262016-08-12 03:55:06 +00001374 SCCs.erase(remove_if(SCCs,
1375 [&](SCC *C) {
1376 return PostOrderMapping.lookup(&*C->begin()) !=
1377 RootPostOrderNumber;
1378 }),
Chandler Carruthe5944d92016-02-17 00:18:16 +00001379 SCCs.end());
Chandler Carruth88823462016-08-24 09:37:14 +00001380 SCCIndices.clear();
1381 for (int i = 0, Size = SCCs.size(); i < Size; ++i)
1382 SCCIndices[SCCs[i]] = i;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001383
1384#ifndef NDEBUG
1385 // Now we need to reconnect the current (root) SCC to the graph. We do this
1386 // manually because we can special case our leaf handling and detect errors.
1387 bool IsLeaf = true;
1388#endif
1389 for (SCC *C : SCCs)
1390 for (Node &N : *C) {
1391 for (Edge &E : N) {
1392 assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
1393 RefSCC &ChildRC = *G->lookupRefSCC(*E.getNode());
1394 if (&ChildRC == this)
1395 continue;
1396 ChildRC.Parents.insert(this);
1397#ifndef NDEBUG
1398 IsLeaf = false;
1399#endif
1400 }
1401 }
1402#ifndef NDEBUG
1403 if (!Result.empty())
1404 assert(!IsLeaf && "This SCC cannot be a leaf as we have split out new "
1405 "SCCs by removing this edge.");
David Majnemer0a16c222016-08-11 21:15:00 +00001406 if (none_of(G->LeafRefSCCs, [&](RefSCC *C) { return C == this; }))
Chandler Carruthe5944d92016-02-17 00:18:16 +00001407 assert(!IsLeaf && "This SCC cannot be a leaf as it already had child "
1408 "SCCs before we removed this edge.");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001409#endif
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001410 // And connect both this RefSCC and all the new ones to the correct parents.
1411 // The easiest way to do this is just to re-analyze the old parent set.
1412 SmallVector<RefSCC *, 4> OldParents(Parents.begin(), Parents.end());
1413 Parents.clear();
1414 for (RefSCC *ParentRC : OldParents)
Chandler Carruth5205c352016-12-07 01:42:40 +00001415 for (SCC &ParentC : *ParentRC)
1416 for (Node &ParentN : ParentC)
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001417 for (Edge &E : ParentN) {
1418 assert(E.getNode() && "Cannot have a missing node in a visited SCC!");
1419 RefSCC &RC = *G->lookupRefSCC(*E.getNode());
Chandler Carruth5205c352016-12-07 01:42:40 +00001420 if (&RC != ParentRC)
1421 RC.Parents.insert(ParentRC);
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001422 }
1423
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001424 // If this SCC stopped being a leaf through this edge removal, remove it from
Chandler Carruthe5944d92016-02-17 00:18:16 +00001425 // the leaf SCC list. Note that this DTRT in the case where this was never
1426 // a leaf.
1427 // FIXME: As LeafRefSCCs could be very large, we might want to not walk the
1428 // entire list if this RefSCC wasn't a leaf before the edge removal.
1429 if (!Result.empty())
1430 G->LeafRefSCCs.erase(
1431 std::remove(G->LeafRefSCCs.begin(), G->LeafRefSCCs.end(), this),
1432 G->LeafRefSCCs.end());
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001433
Chandler Carruth23a6c3f2016-12-06 10:29:23 +00001434#ifndef NDEBUG
1435 // Verify all of the new RefSCCs.
1436 for (RefSCC *RC : Result)
1437 RC->verify();
1438#endif
1439
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001440 // Return the new list of SCCs.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001441 return Result;
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001442}
1443
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001444void LazyCallGraph::RefSCC::handleTrivialEdgeInsertion(Node &SourceN,
1445 Node &TargetN) {
1446 // The only trivial case that requires any graph updates is when we add new
1447 // ref edge and may connect different RefSCCs along that path. This is only
1448 // because of the parents set. Every other part of the graph remains constant
1449 // after this edge insertion.
1450 assert(G->lookupRefSCC(SourceN) == this && "Source must be in this RefSCC.");
1451 RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
1452 if (&TargetRC == this) {
1453
1454 return;
1455 }
1456
1457 assert(TargetRC.isDescendantOf(*this) &&
1458 "Target must be a descendant of the Source.");
1459 // The only change required is to add this RefSCC to the parent set of the
1460 // target. This is a set and so idempotent if the edge already existed.
1461 TargetRC.Parents.insert(this);
1462}
1463
1464void LazyCallGraph::RefSCC::insertTrivialCallEdge(Node &SourceN,
1465 Node &TargetN) {
1466#ifndef NDEBUG
1467 // Check that the RefSCC is still valid when we finish.
1468 auto ExitVerifier = make_scope_exit([this] { verify(); });
Chandler Carruthbae595b2016-11-22 19:23:31 +00001469
1470 // Check that we aren't breaking some invariants of the SCC graph.
1471 SCC &SourceC = *G->lookupSCC(SourceN);
1472 SCC &TargetC = *G->lookupSCC(TargetN);
1473 if (&SourceC != &TargetC)
1474 assert(SourceC.isAncestorOf(TargetC) &&
1475 "Call edge is not trivial in the SCC graph!");
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001476#endif
1477 // First insert it into the source or find the existing edge.
1478 auto InsertResult = SourceN.EdgeIndexMap.insert(
1479 {&TargetN.getFunction(), SourceN.Edges.size()});
1480 if (!InsertResult.second) {
1481 // Already an edge, just update it.
1482 Edge &E = SourceN.Edges[InsertResult.first->second];
1483 if (E.isCall())
1484 return; // Nothing to do!
1485 E.setKind(Edge::Call);
1486 } else {
1487 // Create the new edge.
1488 SourceN.Edges.emplace_back(TargetN, Edge::Call);
1489 }
1490
1491 // Now that we have the edge, handle the graph fallout.
1492 handleTrivialEdgeInsertion(SourceN, TargetN);
1493}
1494
1495void LazyCallGraph::RefSCC::insertTrivialRefEdge(Node &SourceN, Node &TargetN) {
1496#ifndef NDEBUG
1497 // Check that the RefSCC is still valid when we finish.
1498 auto ExitVerifier = make_scope_exit([this] { verify(); });
Chandler Carruth9eb857c2016-11-22 21:40:10 +00001499
1500 // Check that we aren't breaking some invariants of the RefSCC graph.
1501 RefSCC &SourceRC = *G->lookupRefSCC(SourceN);
1502 RefSCC &TargetRC = *G->lookupRefSCC(TargetN);
1503 if (&SourceRC != &TargetRC)
1504 assert(SourceRC.isAncestorOf(TargetRC) &&
1505 "Ref edge is not trivial in the RefSCC graph!");
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001506#endif
1507 // First insert it into the source or find the existing edge.
1508 auto InsertResult = SourceN.EdgeIndexMap.insert(
1509 {&TargetN.getFunction(), SourceN.Edges.size()});
1510 if (!InsertResult.second)
1511 // Already an edge, we're done.
1512 return;
1513
1514 // Create the new edge.
1515 SourceN.Edges.emplace_back(TargetN, Edge::Ref);
1516
1517 // Now that we have the edge, handle the graph fallout.
1518 handleTrivialEdgeInsertion(SourceN, TargetN);
1519}
1520
Chandler Carruthe5944d92016-02-17 00:18:16 +00001521void LazyCallGraph::insertEdge(Node &SourceN, Function &Target, Edge::Kind EK) {
Chandler Carruthc00a7ff2014-04-28 11:10:23 +00001522 assert(SCCMap.empty() && DFSStack.empty() &&
1523 "This method cannot be called after SCCs have been formed!");
1524
Chandler Carruthe5944d92016-02-17 00:18:16 +00001525 return SourceN.insertEdgeInternal(Target, EK);
Chandler Carruthc00a7ff2014-04-28 11:10:23 +00001526}
1527
Chandler Carruthe5944d92016-02-17 00:18:16 +00001528void LazyCallGraph::removeEdge(Node &SourceN, Function &Target) {
Chandler Carruthaa839b22014-04-27 01:59:50 +00001529 assert(SCCMap.empty() && DFSStack.empty() &&
1530 "This method cannot be called after SCCs have been formed!");
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001531
Chandler Carruthe5944d92016-02-17 00:18:16 +00001532 return SourceN.removeEdgeInternal(Target);
Chandler Carruth9302fbf2014-04-23 11:03:03 +00001533}
1534
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001535void LazyCallGraph::removeDeadFunction(Function &F) {
1536 // FIXME: This is unnecessarily restrictive. We should be able to remove
1537 // functions which recursively call themselves.
1538 assert(F.use_empty() &&
1539 "This routine should only be called on trivially dead functions!");
1540
1541 auto EII = EntryIndexMap.find(&F);
1542 if (EII != EntryIndexMap.end()) {
1543 EntryEdges[EII->second] = Edge();
1544 EntryIndexMap.erase(EII);
1545 }
1546
1547 // It's safe to just remove un-visited functions from the RefSCC entry list.
1548 // FIXME: This is a linear operation which could become hot and benefit from
1549 // an index map.
1550 auto RENI = find(RefSCCEntryNodes, &F);
1551 if (RENI != RefSCCEntryNodes.end())
1552 RefSCCEntryNodes.erase(RENI);
1553
1554 auto NI = NodeMap.find(&F);
1555 if (NI == NodeMap.end())
1556 // Not in the graph at all!
1557 return;
1558
1559 Node &N = *NI->second;
1560 NodeMap.erase(NI);
1561
1562 if (SCCMap.empty() && DFSStack.empty()) {
1563 // No SCC walk has begun, so removing this is fine and there is nothing
1564 // else necessary at this point but clearing out the node.
1565 N.clear();
1566 return;
1567 }
1568
1569 // Check that we aren't going to break the DFS walk.
1570 assert(all_of(DFSStack,
1571 [&N](const std::pair<Node *, edge_iterator> &Element) {
1572 return Element.first != &N;
1573 }) &&
1574 "Tried to remove a function currently in the DFS stack!");
1575 assert(find(PendingRefSCCStack, &N) == PendingRefSCCStack.end() &&
1576 "Tried to remove a function currently pending to add to a RefSCC!");
1577
1578 // Cannot remove a function which has yet to be visited in the DFS walk, so
1579 // if we have a node at all then we must have an SCC and RefSCC.
1580 auto CI = SCCMap.find(&N);
1581 assert(CI != SCCMap.end() &&
1582 "Tried to remove a node without an SCC after DFS walk started!");
1583 SCC &C = *CI->second;
1584 SCCMap.erase(CI);
1585 RefSCC &RC = C.getOuterRefSCC();
1586
1587 // This node must be the only member of its SCC as it has no callers, and
1588 // that SCC must be the only member of a RefSCC as it has no references.
1589 // Validate these properties first.
1590 assert(C.size() == 1 && "Dead functions must be in a singular SCC");
1591 assert(RC.size() == 1 && "Dead functions must be in a singular RefSCC");
1592 assert(RC.Parents.empty() && "Cannot have parents of a dead RefSCC!");
1593
1594 // Now remove this RefSCC from any parents sets and the leaf list.
1595 for (Edge &E : N)
1596 if (Node *TargetN = E.getNode())
1597 if (RefSCC *TargetRC = lookupRefSCC(*TargetN))
1598 TargetRC->Parents.erase(&RC);
1599 // FIXME: This is a linear operation which could become hot and benefit from
1600 // an index map.
1601 auto LRI = find(LeafRefSCCs, &RC);
1602 if (LRI != LeafRefSCCs.end())
1603 LeafRefSCCs.erase(LRI);
1604
1605 auto RCIndexI = RefSCCIndices.find(&RC);
1606 int RCIndex = RCIndexI->second;
1607 PostOrderRefSCCs.erase(PostOrderRefSCCs.begin() + RCIndex);
1608 RefSCCIndices.erase(RCIndexI);
1609 for (int i = RCIndex, Size = PostOrderRefSCCs.size(); i < Size; ++i)
1610 RefSCCIndices[PostOrderRefSCCs[i]] = i;
1611
1612 // Finally clear out all the data structures from the node down through the
1613 // components.
1614 N.clear();
1615 C.clear();
1616 RC.clear();
1617
1618 // Nothing to delete as all the objects are allocated in stable bump pointer
1619 // allocators.
1620}
1621
Chandler Carruth2a898e02014-04-23 23:20:36 +00001622LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
1623 return *new (MappedN = BPA.Allocate()) Node(*this, F);
Chandler Carruthd8d865e2014-04-18 11:02:33 +00001624}
1625
1626void LazyCallGraph::updateGraphPtrs() {
Chandler Carruthb60cb312014-04-17 07:25:59 +00001627 // Process all nodes updating the graph pointers.
Chandler Carruthaa839b22014-04-27 01:59:50 +00001628 {
1629 SmallVector<Node *, 16> Worklist;
Chandler Carrutha4499e92016-02-02 03:57:13 +00001630 for (Edge &E : EntryEdges)
1631 if (Node *EntryN = E.getNode())
Chandler Carruthaa839b22014-04-27 01:59:50 +00001632 Worklist.push_back(EntryN);
Chandler Carruthb60cb312014-04-17 07:25:59 +00001633
Chandler Carruthaa839b22014-04-27 01:59:50 +00001634 while (!Worklist.empty()) {
1635 Node *N = Worklist.pop_back_val();
1636 N->G = this;
Chandler Carrutha4499e92016-02-02 03:57:13 +00001637 for (Edge &E : N->Edges)
Chandler Carruthe5944d92016-02-17 00:18:16 +00001638 if (Node *TargetN = E.getNode())
1639 Worklist.push_back(TargetN);
Chandler Carruthaa839b22014-04-27 01:59:50 +00001640 }
1641 }
1642
1643 // Process all SCCs updating the graph pointers.
1644 {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001645 SmallVector<RefSCC *, 16> Worklist(LeafRefSCCs.begin(), LeafRefSCCs.end());
Chandler Carruthaa839b22014-04-27 01:59:50 +00001646
1647 while (!Worklist.empty()) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001648 RefSCC &C = *Worklist.pop_back_val();
1649 C.G = this;
1650 for (RefSCC &ParentC : C.parents())
1651 Worklist.push_back(&ParentC);
Chandler Carruthaa839b22014-04-27 01:59:50 +00001652 }
Chandler Carruthb60cb312014-04-17 07:25:59 +00001653 }
Chandler Carruthbf71a342014-02-06 04:37:03 +00001654}
Chandler Carruthbf71a342014-02-06 04:37:03 +00001655
Chandler Carruthe5944d92016-02-17 00:18:16 +00001656/// Build the internal SCCs for a RefSCC from a sequence of nodes.
1657///
1658/// Appends the SCCs to the provided vector and updates the map with their
1659/// indices. Both the vector and map must be empty when passed into this
1660/// routine.
1661void LazyCallGraph::buildSCCs(RefSCC &RC, node_stack_range Nodes) {
1662 assert(RC.SCCs.empty() && "Already built SCCs!");
1663 assert(RC.SCCIndices.empty() && "Already mapped SCC indices!");
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001664
Chandler Carruthe5944d92016-02-17 00:18:16 +00001665 for (Node *N : Nodes) {
1666 assert(N->LowLink >= (*Nodes.begin())->LowLink &&
Chandler Carruthcace6622014-04-23 10:31:17 +00001667 "We cannot have a low link in an SCC lower than its root on the "
1668 "stack!");
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001669
Chandler Carruthe5944d92016-02-17 00:18:16 +00001670 // This node will go into the next RefSCC, clear out its DFS and low link
1671 // as we scan.
1672 N->DFSNumber = N->LowLink = 0;
1673 }
1674
1675 // Each RefSCC contains a DAG of the call SCCs. To build these, we do
1676 // a direct walk of the call edges using Tarjan's algorithm. We reuse the
1677 // internal storage as we won't need it for the outer graph's DFS any longer.
1678
1679 SmallVector<std::pair<Node *, call_edge_iterator>, 16> DFSStack;
1680 SmallVector<Node *, 16> PendingSCCStack;
1681
1682 // Scan down the stack and DFS across the call edges.
1683 for (Node *RootN : Nodes) {
1684 assert(DFSStack.empty() &&
1685 "Cannot begin a new root with a non-empty DFS stack!");
1686 assert(PendingSCCStack.empty() &&
1687 "Cannot begin a new root with pending nodes for an SCC!");
1688
1689 // Skip any nodes we've already reached in the DFS.
1690 if (RootN->DFSNumber != 0) {
1691 assert(RootN->DFSNumber == -1 &&
1692 "Shouldn't have any mid-DFS root nodes!");
1693 continue;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001694 }
1695
Chandler Carruthe5944d92016-02-17 00:18:16 +00001696 RootN->DFSNumber = RootN->LowLink = 1;
1697 int NextDFSNumber = 2;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001698
Chandler Carruthe5944d92016-02-17 00:18:16 +00001699 DFSStack.push_back({RootN, RootN->call_begin()});
1700 do {
1701 Node *N;
1702 call_edge_iterator I;
1703 std::tie(N, I) = DFSStack.pop_back_val();
1704 auto E = N->call_end();
1705 while (I != E) {
1706 Node &ChildN = *I->getNode();
1707 if (ChildN.DFSNumber == 0) {
1708 // We haven't yet visited this child, so descend, pushing the current
1709 // node onto the stack.
1710 DFSStack.push_back({N, I});
1711
1712 assert(!lookupSCC(ChildN) &&
1713 "Found a node with 0 DFS number but already in an SCC!");
1714 ChildN.DFSNumber = ChildN.LowLink = NextDFSNumber++;
1715 N = &ChildN;
1716 I = N->call_begin();
1717 E = N->call_end();
1718 continue;
1719 }
1720
1721 // If the child has already been added to some child component, it
1722 // couldn't impact the low-link of this parent because it isn't
1723 // connected, and thus its low-link isn't relevant so skip it.
1724 if (ChildN.DFSNumber == -1) {
1725 ++I;
1726 continue;
1727 }
1728
1729 // Track the lowest linked child as the lowest link for this node.
1730 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1731 if (ChildN.LowLink < N->LowLink)
1732 N->LowLink = ChildN.LowLink;
1733
1734 // Move to the next edge.
1735 ++I;
1736 }
1737
1738 // We've finished processing N and its descendents, put it on our pending
1739 // SCC stack to eventually get merged into an SCC of nodes.
1740 PendingSCCStack.push_back(N);
1741
1742 // If this node is linked to some lower entry, continue walking up the
1743 // stack.
1744 if (N->LowLink != N->DFSNumber)
1745 continue;
1746
1747 // Otherwise, we've completed an SCC. Append it to our post order list of
1748 // SCCs.
1749 int RootDFSNumber = N->DFSNumber;
1750 // Find the range of the node stack by walking down until we pass the
1751 // root DFS number.
1752 auto SCCNodes = make_range(
1753 PendingSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +00001754 find_if(reverse(PendingSCCStack), [RootDFSNumber](const Node *N) {
1755 return N->DFSNumber < RootDFSNumber;
1756 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +00001757 // Form a new SCC out of these nodes and then clear them off our pending
1758 // stack.
1759 RC.SCCs.push_back(createSCC(RC, SCCNodes));
1760 for (Node &N : *RC.SCCs.back()) {
1761 N.DFSNumber = N.LowLink = -1;
1762 SCCMap[&N] = RC.SCCs.back();
1763 }
1764 PendingSCCStack.erase(SCCNodes.end().base(), PendingSCCStack.end());
1765 } while (!DFSStack.empty());
1766 }
1767
1768 // Wire up the SCC indices.
1769 for (int i = 0, Size = RC.SCCs.size(); i < Size; ++i)
1770 RC.SCCIndices[RC.SCCs[i]] = i;
Chandler Carruth3f9869a2014-04-23 06:09:03 +00001771}
1772
Chandler Carruthe5944d92016-02-17 00:18:16 +00001773// FIXME: We should move callers of this to embed the parent linking and leaf
1774// tracking into their DFS in order to remove a full walk of all edges.
1775void LazyCallGraph::connectRefSCC(RefSCC &RC) {
1776 // Walk all edges in the RefSCC (this remains linear as we only do this once
1777 // when we build the RefSCC) to connect it to the parent sets of its
1778 // children.
1779 bool IsLeaf = true;
1780 for (SCC &C : RC)
1781 for (Node &N : C)
1782 for (Edge &E : N) {
1783 assert(E.getNode() &&
1784 "Cannot have a missing node in a visited part of the graph!");
1785 RefSCC &ChildRC = *lookupRefSCC(*E.getNode());
1786 if (&ChildRC == &RC)
1787 continue;
1788 ChildRC.Parents.insert(&RC);
1789 IsLeaf = false;
1790 }
1791
Chandler Carruth5dbc1642016-10-12 07:59:56 +00001792 // For the SCCs where we find no child SCCs, add them to the leaf list.
Chandler Carruthe5944d92016-02-17 00:18:16 +00001793 if (IsLeaf)
1794 LeafRefSCCs.push_back(&RC);
1795}
1796
Chandler Carruth49d728a2016-09-16 10:20:17 +00001797bool LazyCallGraph::buildNextRefSCCInPostOrder() {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001798 if (DFSStack.empty()) {
1799 Node *N;
Chandler Carruth90821c22014-04-26 09:45:55 +00001800 do {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001801 // If we've handled all candidate entry nodes to the SCC forest, we're
1802 // done.
1803 if (RefSCCEntryNodes.empty())
Chandler Carruth49d728a2016-09-16 10:20:17 +00001804 return false;
Chandler Carruth18eadd922014-04-18 10:50:32 +00001805
Chandler Carruthe5944d92016-02-17 00:18:16 +00001806 N = &get(*RefSCCEntryNodes.pop_back_val());
Chandler Carruth90821c22014-04-26 09:45:55 +00001807 } while (N->DFSNumber != 0);
Chandler Carruthe5944d92016-02-17 00:18:16 +00001808
1809 // Found a new root, begin the DFS here.
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001810 N->LowLink = N->DFSNumber = 1;
Chandler Carruth09751bf2014-04-24 09:59:59 +00001811 NextDFSNumber = 2;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001812 DFSStack.push_back({N, N->begin()});
Chandler Carruth18eadd922014-04-18 10:50:32 +00001813 }
1814
Chandler Carruth91dcf0f2014-04-24 21:19:30 +00001815 for (;;) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001816 Node *N;
1817 edge_iterator I;
1818 std::tie(N, I) = DFSStack.pop_back_val();
1819
1820 assert(N->DFSNumber > 0 && "We should always assign a DFS number "
1821 "before placing a node onto the stack.");
Chandler Carruth24553932014-04-24 11:05:20 +00001822
Chandler Carrutha4499e92016-02-02 03:57:13 +00001823 auto E = N->end();
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001824 while (I != E) {
Chandler Carrutha4499e92016-02-02 03:57:13 +00001825 Node &ChildN = I->getNode(*this);
Chandler Carruthbd5d3082014-04-23 23:34:48 +00001826 if (ChildN.DFSNumber == 0) {
Chandler Carruthe5944d92016-02-17 00:18:16 +00001827 // We haven't yet visited this child, so descend, pushing the current
1828 // node onto the stack.
1829 DFSStack.push_back({N, N->begin()});
Chandler Carruth18eadd922014-04-18 10:50:32 +00001830
Chandler Carruth09751bf2014-04-24 09:59:59 +00001831 assert(!SCCMap.count(&ChildN) &&
1832 "Found a node with 0 DFS number but already in an SCC!");
1833 ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001834 N = &ChildN;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001835 I = N->begin();
1836 E = N->end();
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001837 continue;
Chandler Carruthcace6622014-04-23 10:31:17 +00001838 }
1839
Chandler Carruthe5944d92016-02-17 00:18:16 +00001840 // If the child has already been added to some child component, it
1841 // couldn't impact the low-link of this parent because it isn't
1842 // connected, and thus its low-link isn't relevant so skip it.
1843 if (ChildN.DFSNumber == -1) {
1844 ++I;
1845 continue;
1846 }
1847
1848 // Track the lowest linked child as the lowest link for this node.
1849 assert(ChildN.LowLink > 0 && "Must have a positive low-link number!");
1850 if (ChildN.LowLink < N->LowLink)
Chandler Carruthbd5d3082014-04-23 23:34:48 +00001851 N->LowLink = ChildN.LowLink;
Chandler Carruthe5944d92016-02-17 00:18:16 +00001852
1853 // Move to the next edge.
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001854 ++I;
Chandler Carruth18eadd922014-04-18 10:50:32 +00001855 }
1856
Chandler Carruthe5944d92016-02-17 00:18:16 +00001857 // We've finished processing N and its descendents, put it on our pending
1858 // SCC stack to eventually get merged into an SCC of nodes.
1859 PendingRefSCCStack.push_back(N);
Chandler Carruth18eadd922014-04-18 10:50:32 +00001860
Chandler Carruthe5944d92016-02-17 00:18:16 +00001861 // If this node is linked to some lower entry, continue walking up the
1862 // stack.
1863 if (N->LowLink != N->DFSNumber) {
1864 assert(!DFSStack.empty() &&
1865 "We never found a viable root for an SCC to pop off!");
1866 continue;
1867 }
Chandler Carruth5e2d70b2014-04-26 09:28:00 +00001868
Chandler Carruthe5944d92016-02-17 00:18:16 +00001869 // Otherwise, form a new RefSCC from the top of the pending node stack.
1870 int RootDFSNumber = N->DFSNumber;
1871 // Find the range of the node stack by walking down until we pass the
1872 // root DFS number.
1873 auto RefSCCNodes = node_stack_range(
1874 PendingRefSCCStack.rbegin(),
David Majnemer42531262016-08-12 03:55:06 +00001875 find_if(reverse(PendingRefSCCStack), [RootDFSNumber](const Node *N) {
1876 return N->DFSNumber < RootDFSNumber;
1877 }));
Chandler Carruthe5944d92016-02-17 00:18:16 +00001878 // Form a new RefSCC out of these nodes and then clear them off our pending
1879 // stack.
1880 RefSCC *NewRC = createRefSCC(*this);
1881 buildSCCs(*NewRC, RefSCCNodes);
1882 connectRefSCC(*NewRC);
1883 PendingRefSCCStack.erase(RefSCCNodes.end().base(),
1884 PendingRefSCCStack.end());
1885
Chandler Carruth49d728a2016-09-16 10:20:17 +00001886 // Push the new node into the postorder list and return true indicating we
1887 // successfully grew the postorder sequence by one.
1888 bool Inserted =
1889 RefSCCIndices.insert({NewRC, PostOrderRefSCCs.size()}).second;
1890 (void)Inserted;
1891 assert(Inserted && "Cannot already have this RefSCC in the index map!");
1892 PostOrderRefSCCs.push_back(NewRC);
1893 return true;
Chandler Carruth91dcf0f2014-04-24 21:19:30 +00001894 }
Chandler Carruth18eadd922014-04-18 10:50:32 +00001895}
1896
Chandler Carruthdab4eae2016-11-23 17:53:26 +00001897AnalysisKey LazyCallGraphAnalysis::Key;
NAKAMURA Takumidf0cd722016-02-28 17:17:00 +00001898
Chandler Carruthbf71a342014-02-06 04:37:03 +00001899LazyCallGraphPrinterPass::LazyCallGraphPrinterPass(raw_ostream &OS) : OS(OS) {}
1900
Chandler Carruthe5944d92016-02-17 00:18:16 +00001901static void printNode(raw_ostream &OS, LazyCallGraph::Node &N) {
Chandler Carrutha4499e92016-02-02 03:57:13 +00001902 OS << " Edges in function: " << N.getFunction().getName() << "\n";
1903 for (const LazyCallGraph::Edge &E : N)
1904 OS << " " << (E.isCall() ? "call" : "ref ") << " -> "
1905 << E.getFunction().getName() << "\n";
Chandler Carruth11f50322015-01-14 00:27:45 +00001906
1907 OS << "\n";
1908}
1909
Chandler Carruthe5944d92016-02-17 00:18:16 +00001910static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) {
1911 ptrdiff_t Size = std::distance(C.begin(), C.end());
1912 OS << " SCC with " << Size << " functions:\n";
Chandler Carruth11f50322015-01-14 00:27:45 +00001913
Chandler Carruthe5944d92016-02-17 00:18:16 +00001914 for (LazyCallGraph::Node &N : C)
1915 OS << " " << N.getFunction().getName() << "\n";
1916}
1917
1918static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) {
1919 ptrdiff_t Size = std::distance(C.begin(), C.end());
1920 OS << " RefSCC with " << Size << " call SCCs:\n";
1921
1922 for (LazyCallGraph::SCC &InnerC : C)
1923 printSCC(OS, InnerC);
Chandler Carruth11f50322015-01-14 00:27:45 +00001924
1925 OS << "\n";
1926}
1927
Chandler Carruthd174ce42015-01-05 02:47:05 +00001928PreservedAnalyses LazyCallGraphPrinterPass::run(Module &M,
Chandler Carruthb47f8012016-03-11 11:05:24 +00001929 ModuleAnalysisManager &AM) {
1930 LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
Chandler Carruth11f50322015-01-14 00:27:45 +00001931
1932 OS << "Printing the call graph for module: " << M.getModuleIdentifier()
1933 << "\n\n";
1934
Chandler Carruthe5944d92016-02-17 00:18:16 +00001935 for (Function &F : M)
1936 printNode(OS, G.get(F));
Chandler Carruth11f50322015-01-14 00:27:45 +00001937
Chandler Carruthe5944d92016-02-17 00:18:16 +00001938 for (LazyCallGraph::RefSCC &C : G.postorder_ref_sccs())
1939 printRefSCC(OS, C);
Chandler Carruth18eadd922014-04-18 10:50:32 +00001940
Chandler Carruthbf71a342014-02-06 04:37:03 +00001941 return PreservedAnalyses::all();
Chandler Carruthbf71a342014-02-06 04:37:03 +00001942}
Sean Silva7cb30662016-06-18 09:17:32 +00001943
1944LazyCallGraphDOTPrinterPass::LazyCallGraphDOTPrinterPass(raw_ostream &OS)
1945 : OS(OS) {}
1946
1947static void printNodeDOT(raw_ostream &OS, LazyCallGraph::Node &N) {
1948 std::string Name = "\"" + DOT::EscapeString(N.getFunction().getName()) + "\"";
1949
1950 for (const LazyCallGraph::Edge &E : N) {
1951 OS << " " << Name << " -> \""
1952 << DOT::EscapeString(E.getFunction().getName()) << "\"";
1953 if (!E.isCall()) // It is a ref edge.
1954 OS << " [style=dashed,label=\"ref\"]";
1955 OS << ";\n";
1956 }
1957
1958 OS << "\n";
1959}
1960
1961PreservedAnalyses LazyCallGraphDOTPrinterPass::run(Module &M,
1962 ModuleAnalysisManager &AM) {
1963 LazyCallGraph &G = AM.getResult<LazyCallGraphAnalysis>(M);
1964
1965 OS << "digraph \"" << DOT::EscapeString(M.getModuleIdentifier()) << "\" {\n";
1966
1967 for (Function &F : M)
1968 printNodeDOT(OS, G.get(F));
1969
1970 OS << "}\n";
1971
1972 return PreservedAnalyses::all();
1973}