+ // It may make the Caller SCC a leaf SCC.
+ if (!HasOtherCallOutsideSCC)
+ G->LeafSCCs.push_back(this);
+}
+
+void LazyCallGraph::SCC::internalDFS(
+ SmallVectorImpl<std::pair<Node *, Node::iterator>> &DFSStack,
+ SmallVectorImpl<Node *> &PendingSCCStack, Node *N,
+ SmallVectorImpl<SCC *> &ResultSCCs) {
+ Node::iterator I = N->begin();
+ N->LowLink = N->DFSNumber = 1;
+ int NextDFSNumber = 2;
+ for (;;) {
+ assert(N->DFSNumber != 0 && "We should always assign a DFS number "
+ "before processing a node.");
+
+ // We simulate recursion by popping out of the nested loop and continuing.
+ Node::iterator E = N->end();
+ while (I != E) {
+ Node &ChildN = *I;
+ if (SCC *ChildSCC = G->SCCMap.lookup(&ChildN)) {
+ // Check if we have reached a node in the new (known connected) set of
+ // this SCC. If so, the entire stack is necessarily in that set and we
+ // can re-start.
+ if (ChildSCC == this) {
+ insert(*N);
+ while (!PendingSCCStack.empty())
+ insert(*PendingSCCStack.pop_back_val());
+ while (!DFSStack.empty())
+ insert(*DFSStack.pop_back_val().first);
+ return;
+ }
+
+ // If this child isn't currently in this SCC, no need to process it.
+ // However, we do need to remove this SCC from its SCC's parent set.
+ ChildSCC->ParentSCCs.erase(this);
+ ++I;
+ continue;
+ }
+
+ if (ChildN.DFSNumber == 0) {
+ // Mark that we should start at this child when next this node is the
+ // top of the stack. We don't start at the next child to ensure this
+ // child's lowlink is reflected.
+ DFSStack.push_back(std::make_pair(N, I));
+
+ // Continue, resetting to the child node.
+ ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
+ N = &ChildN;
+ I = ChildN.begin();
+ E = ChildN.end();
+ continue;
+ }
+
+ // Track the lowest link of the children, if any are still in the stack.
+ // Any child not on the stack will have a LowLink of -1.
+ assert(ChildN.LowLink != 0 &&
+ "Low-link must not be zero with a non-zero DFS number.");
+ if (ChildN.LowLink >= 0 && ChildN.LowLink < N->LowLink)
+ N->LowLink = ChildN.LowLink;
+ ++I;
+ }
+
+ if (N->LowLink == N->DFSNumber) {
+ ResultSCCs.push_back(G->formSCC(N, PendingSCCStack));
+ if (DFSStack.empty())
+ return;
+ } else {
+ // At this point we know that N cannot ever be an SCC root. Its low-link
+ // is not its dfs-number, and we've processed all of its children. It is
+ // just sitting here waiting until some node further down the stack gets
+ // low-link == dfs-number and pops it off as well. Move it to the pending
+ // stack which is pulled into the next SCC to be formed.
+ PendingSCCStack.push_back(N);
+
+ assert(!DFSStack.empty() && "We shouldn't have an empty stack!");
+ }
+
+ N = DFSStack.back().first;
+ I = DFSStack.back().second;
+ DFSStack.pop_back();
+ }
+}
+
+SmallVector<LazyCallGraph::SCC *, 1>
+LazyCallGraph::SCC::removeIntraSCCEdge(Node &CallerN,
+ Node &CalleeN) {
+ // First remove it from the node.
+ CallerN.removeEdgeInternal(CalleeN.getFunction());
+
+ // We return a list of the resulting *new* SCCs in postorder.
+ SmallVector<SCC *, 1> ResultSCCs;
+
+ // Direct recursion doesn't impact the SCC graph at all.
+ if (&CallerN == &CalleeN)
+ return ResultSCCs;
+
+ // The worklist is every node in the original SCC.
+ SmallVector<Node *, 1> Worklist;
+ Worklist.swap(Nodes);
+ for (Node *N : Worklist) {
+ // The nodes formerly in this SCC are no longer in any SCC.
+ N->DFSNumber = 0;
+ N->LowLink = 0;
+ G->SCCMap.erase(N);
+ }
+ assert(Worklist.size() > 1 && "We have to have at least two nodes to have an "
+ "edge between them that is within the SCC.");
+
+ // The callee can already reach every node in this SCC (by definition). It is
+ // the only node we know will stay inside this SCC. Everything which
+ // transitively reaches Callee will also remain in the SCC. To model this we
+ // incrementally add any chain of nodes which reaches something in the new
+ // node set to the new node set. This short circuits one side of the Tarjan's
+ // walk.
+ insert(CalleeN);
+
+ // We're going to do a full mini-Tarjan's walk using a local stack here.
+ SmallVector<std::pair<Node *, Node::iterator>, 4> DFSStack;
+ SmallVector<Node *, 4> PendingSCCStack;
+ do {
+ Node *N = Worklist.pop_back_val();
+ if (N->DFSNumber == 0)
+ internalDFS(DFSStack, PendingSCCStack, N, ResultSCCs);
+
+ assert(DFSStack.empty() && "Didn't flush the entire DFS stack!");
+ assert(PendingSCCStack.empty() && "Didn't flush all pending SCC nodes!");
+ } while (!Worklist.empty());
+
+ // Now we need to reconnect the current SCC to the graph.
+ bool IsLeafSCC = true;
+ for (Node *N : Nodes) {
+ for (Node &ChildN : *N) {
+ SCC &ChildSCC = *G->SCCMap.lookup(&ChildN);
+ if (&ChildSCC == this)
+ continue;
+ ChildSCC.ParentSCCs.insert(this);
+ IsLeafSCC = false;
+ }
+ }
+#ifndef NDEBUG
+ if (!ResultSCCs.empty())
+ assert(!IsLeafSCC && "This SCC cannot be a leaf as we have split out new "
+ "SCCs by removing this edge.");
+ if (!std::any_of(G->LeafSCCs.begin(), G->LeafSCCs.end(),
+ [&](SCC *C) { return C == this; }))
+ assert(!IsLeafSCC && "This SCC cannot be a leaf as it already had child "
+ "SCCs before we removed this edge.");
+#endif
+ // If this SCC stopped being a leaf through this edge removal, remove it from
+ // the leaf SCC list.
+ if (!IsLeafSCC && !ResultSCCs.empty())
+ G->LeafSCCs.erase(std::remove(G->LeafSCCs.begin(), G->LeafSCCs.end(), this),
+ G->LeafSCCs.end());
+
+ // Return the new list of SCCs.
+ return ResultSCCs;
+}
+
+void LazyCallGraph::insertEdge(Node &CallerN, Function &Callee) {
+ assert(SCCMap.empty() && DFSStack.empty() &&
+ "This method cannot be called after SCCs have been formed!");
+
+ return CallerN.insertEdgeInternal(Callee);
+}
+
+void LazyCallGraph::removeEdge(Node &CallerN, Function &Callee) {
+ assert(SCCMap.empty() && DFSStack.empty() &&
+ "This method cannot be called after SCCs have been formed!");
+
+ return CallerN.removeEdgeInternal(Callee);
+}
+
+LazyCallGraph::Node &LazyCallGraph::insertInto(Function &F, Node *&MappedN) {
+ return *new (MappedN = BPA.Allocate()) Node(*this, F);
+}
+
+void LazyCallGraph::updateGraphPtrs() {
+ // Process all nodes updating the graph pointers.
+ {
+ SmallVector<Node *, 16> Worklist;
+ for (auto &Entry : EntryNodes)
+ if (Node *EntryN = Entry.dyn_cast<Node *>())
+ Worklist.push_back(EntryN);
+
+ while (!Worklist.empty()) {
+ Node *N = Worklist.pop_back_val();
+ N->G = this;
+ for (auto &Callee : N->Callees)
+ if (!Callee.isNull())
+ if (Node *CalleeN = Callee.dyn_cast<Node *>())
+ Worklist.push_back(CalleeN);
+ }