From c8719e935397ee86cd8555771f0a40cc0496e8f2 Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Thu, 7 Sep 2006 23:22:37 +0000 Subject: [PATCH] Use iterative do-while loop instead of recursive DFSPass calls to reduce amount of stack space used at runtime. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30167 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/PostDominators.cpp | 45 +++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/lib/Analysis/PostDominators.cpp b/lib/Analysis/PostDominators.cpp index ec7f7c75466..b0b2374317d 100644 --- a/lib/Analysis/PostDominators.cpp +++ b/lib/Analysis/PostDominators.cpp @@ -28,23 +28,36 @@ D("postidom", "Immediate Post-Dominators Construction", true); unsigned ImmediatePostDominators::DFSPass(BasicBlock *V, InfoRec &VInfo, unsigned N) { - VInfo.Semi = ++N; - VInfo.Label = V; - - Vertex.push_back(V); // Vertex[n] = V; - //Info[V].Ancestor = 0; // Ancestor[n] = 0 - //Child[V] = 0; // Child[v] = 0 - VInfo.Size = 1; // Size[v] = 1 - - // For PostDominators, we want to walk predecessors rather than successors - // as we do in forward Dominators. - for (pred_iterator PI = pred_begin(V), PE = pred_end(V); PI != PE; ++PI) { - InfoRec &SuccVInfo = Info[*PI]; - if (SuccVInfo.Semi == 0) { - SuccVInfo.Parent = V; - N = DFSPass(*PI, SuccVInfo, N); + + std::vector > workStack; + workStack.push_back(std::make_pair(V, &VInfo)); + + do { + BasicBlock *currentBB = workStack.back().first; + InfoRec *currentVInfo = workStack.back().second; + workStack.pop_back(); + + currentVInfo->Semi = ++N; + currentVInfo->Label = currentBB; + + Vertex.push_back(currentBB); // Vertex[n] = current; + // Info[currentBB].Ancestor = 0; + // Ancestor[n] = 0 + // Child[currentBB] = 0; + currentVInfo->Size = 1; // Size[currentBB] = 1 + + // For PostDominators, we want to walk predecessors rather than successors + // as we do in forward Dominators. + for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB); + PI != PE; ++PI) { + InfoRec &SuccVInfo = Info[*PI]; + if (SuccVInfo.Semi == 0) { + SuccVInfo.Parent = currentBB; + + workStack.push_back(std::make_pair(*PI, &SuccVInfo)); + } } - } + } while (!workStack.empty()); return N; }