aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/LazyCallGraph.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2014-04-26 09:28:00 +0000
committerChandler Carruth <chandlerc@gmail.com>2014-04-26 09:28:00 +0000
commit5e2d70b9a3ac93069115a0e30749750ae1764c6c (patch)
tree5d6f4cb35724e74e852991e80dc599ce9a381dc4 /llvm/lib/Analysis/LazyCallGraph.cpp
parentaca48d0443ce43580876779305bef04f535d89bd (diff)
downloadllvm-5e2d70b9a3ac93069115a0e30749750ae1764c6c.zip
llvm-5e2d70b9a3ac93069115a0e30749750ae1764c6c.tar.gz
llvm-5e2d70b9a3ac93069115a0e30749750ae1764c6c.tar.bz2
[LCG] Rotate the full SCC finding algorithm to avoid round-trips through
the DFS stack for leaves in the call graph. As mentioned in my previous commit, this is particularly interesting for graphs which have high fan out but low connectivity resulting in many leaves. For such graphs, this can remove a large % of the DFS stack traffic even though it doesn't make the stack much smaller. It's a bit easier to formulate this for the full algorithm because that one stops completely for each SCC. For example, I was able to directly eliminate the "Recurse" boolean used to continue an outer loop from the inner loop. llvm-svn: 207311
Diffstat (limited to 'llvm/lib/Analysis/LazyCallGraph.cpp')
-rw-r--r--llvm/lib/Analysis/LazyCallGraph.cpp44
1 files changed, 23 insertions, 21 deletions
diff --git a/llvm/lib/Analysis/LazyCallGraph.cpp b/llvm/lib/Analysis/LazyCallGraph.cpp
index 1e7f796..50accb3 100644
--- a/llvm/lib/Analysis/LazyCallGraph.cpp
+++ b/llvm/lib/Analysis/LazyCallGraph.cpp
@@ -429,40 +429,45 @@ LazyCallGraph::SCC *LazyCallGraph::formSCC(Node *RootN,
}
LazyCallGraph::SCC *LazyCallGraph::getNextSCCInPostOrder() {
- // When the stack is empty, there are no more SCCs to walk in this graph.
- if (DFSStack.empty()) {
+ Node *N;
+ Node::iterator I;
+ if (!DFSStack.empty()) {
+ N = DFSStack.back().first;
+ I = DFSStack.back().second;
+ DFSStack.pop_back();
+ } else {
// If we've handled all candidate entry nodes to the SCC forest, we're done.
if (SCCEntryNodes.empty())
return nullptr;
- Node &N = get(*SCCEntryNodes.pop_back_val());
- N.LowLink = N.DFSNumber = 1;
+ N = &get(*SCCEntryNodes.pop_back_val());
+ I = N->begin();
+ N->LowLink = N->DFSNumber = 1;
NextDFSNumber = 2;
- DFSStack.push_back(std::make_pair(&N, N.begin()));
}
for (;;) {
- Node *N = DFSStack.back().first;
assert(N->DFSNumber != 0 && "We should always assign a DFS number "
"before placing a node onto the stack.");
- bool Recurse = false; // Used to simulate recursing onto a child.
- for (auto I = DFSStack.back().second, E = N->end(); I != E; ++I) {
+ Node::iterator E = N->end();
+ while (I != E) {
Node &ChildN = *I;
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.back().second = I;
+ DFSStack.push_back(std::make_pair(N, N->begin()));
// Recurse onto this node via a tail call.
assert(!SCCMap.count(&ChildN) &&
"Found a node with 0 DFS number but already in an SCC!");
ChildN.LowLink = ChildN.DFSNumber = NextDFSNumber++;
SCCEntryNodes.remove(&ChildN.getFunction());
- DFSStack.push_back(std::make_pair(&ChildN, ChildN.begin()));
- Recurse = true;
- break;
+ N = &ChildN;
+ I = ChildN.begin();
+ E = ChildN.end();
+ continue;
}
// Track the lowest link of the childen, if any are still in the stack.
@@ -470,27 +475,24 @@ LazyCallGraph::SCC *LazyCallGraph::getNextSCCInPostOrder() {
"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 (Recurse)
- // Continue the outer loop when we exit the inner loop in order to
- // recurse onto a child.
- continue;
-
- // No more children to process here, pop the node off the stack.
- DFSStack.pop_back();
if (N->LowLink == N->DFSNumber)
// Form the new SCC out of the top of the DFS stack.
return formSCC(N, PendingSCCStack);
- assert(!DFSStack.empty() && "We never found a viable root!");
-
// 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 never found a viable root!");
+ N = DFSStack.back().first;
+ I = DFSStack.back().second;
+ DFSStack.pop_back();
}
}