Index: include/llvm/Support/GenericDomTreeConstruction.h =================================================================== --- include/llvm/Support/GenericDomTreeConstruction.h +++ include/llvm/Support/GenericDomTreeConstruction.h @@ -76,118 +76,108 @@ llvm::make_unique>(BB, IDomNode))) .get(); } -}; -} // namespace detail -// External storage for depth first iterator that reuses the info lookup map -// domtree already has. We don't have a set, but a map instead, so we are -// converting the one argument insert calls. -template struct df_iterator_dom_storage { -public: - using BaseSet = DenseMap; - df_iterator_dom_storage(BaseSet &Storage) : Storage(Storage) {} - - using iterator = typename BaseSet::iterator; - std::pair insert(NodeRef N) { - return Storage.insert({N, InfoType()}); - } - void completed(NodeRef) {} + // External storage for depth first iterator that reuses the info lookup map + // SemiNCAInfo already has. We don't have a set, but a map instead, so we are + // converting the one argument insert calls. + struct df_iterator_dom_storage { + public: + using BaseSet = decltype(NodeToInfo); + df_iterator_dom_storage(BaseSet &Storage) : Storage(Storage) {} + + using iterator = typename BaseSet::iterator; + std::pair insert(NodePtr N) { + return Storage.insert({N, InfoRec()}); + } + void completed(NodePtr) {} -private: - BaseSet &Storage; -}; + private: + BaseSet &Storage; + }; -template ::type> -unsigned ReverseDFSPass(NodePtr V, detail::SemiNCAInfo &SNCA, - unsigned N) { - using SNCAInfoTy = detail::SemiNCAInfo; - df_iterator_dom_storage DFStorage( - SNCA.NodeToInfo); - - bool IsChildOfArtificialExit = (N != 0); - for (auto I = idf_ext_begin(V, DFStorage), E = idf_ext_end(V, DFStorage); - I != E; ++I) { - NodePtr BB = *I; - auto &BBInfo = SNCA.NodeToInfo[BB]; - BBInfo.DFSNum = BBInfo.Semi = ++N; - BBInfo.Label = BB; - // Set the parent to the top of the visited stack. The stack includes us, - // and is 1 based, so we subtract to account for both of these. - if (I.getPathLength() > 1) - BBInfo.Parent = SNCA.NodeToInfo[I.getPath(I.getPathLength() - 2)].DFSNum; - SNCA.NumToNode.push_back(BB); // NumToNode[n] = V; + df_iterator_dom_storage getStorage() { return {NodeToInfo}; } + + unsigned runReverseDFS(NodePtr V, unsigned N) { + auto DFStorage = getStorage(); + + bool IsChildOfArtificialExit = (N != 0); + for (auto I = idf_ext_begin(V, DFStorage), E = idf_ext_end(V, DFStorage); + I != E; ++I) { + NodePtr BB = *I; + auto &BBInfo = NodeToInfo[BB]; + BBInfo.DFSNum = BBInfo.Semi = ++N; + BBInfo.Label = BB; + // Set the parent to the top of the visited stack. The stack includes us, + // and is 1 based, so we subtract to account for both of these. + if (I.getPathLength() > 1) + BBInfo.Parent = NodeToInfo[I.getPath(I.getPathLength() - 2)].DFSNum; + NumToNode.push_back(BB); // NumToNode[n] = V; - if (IsChildOfArtificialExit) - BBInfo.Parent = 1; + if (IsChildOfArtificialExit) + BBInfo.Parent = 1; - IsChildOfArtificialExit = false; + IsChildOfArtificialExit = false; + } + return N; } - return N; -} -template ::type> -unsigned DFSPass(NodePtr V, detail::SemiNCAInfo &SNCA, unsigned N) { - using SNCAInfoTy = detail::SemiNCAInfo; - df_iterator_dom_storage DFStorage( - SNCA.NodeToInfo); - - for (auto I = df_ext_begin(V, DFStorage), E = df_ext_end(V, DFStorage); - I != E; ++I) { - NodePtr BB = *I; - auto &BBInfo = SNCA.NodeToInfo[BB]; - BBInfo.DFSNum = BBInfo.Semi = ++N; - BBInfo.Label = BB; - // Set the parent to the top of the visited stack. The stack includes us, - // and is 1 based, so we subtract to account for both of these. - if (I.getPathLength() > 1) - BBInfo.Parent = SNCA.NodeToInfo[I.getPath(I.getPathLength() - 2)].DFSNum; - SNCA.NumToNode.push_back(BB); // NumToNode[n] = V; + unsigned runDFS(NodePtr V, unsigned N) { + auto DFStorage = getStorage(); + + for (auto I = df_ext_begin(V, DFStorage), E = df_ext_end(V, DFStorage); + I != E; ++I) { + NodePtr BB = *I; + auto &BBInfo = NodeToInfo[BB]; + BBInfo.DFSNum = BBInfo.Semi = ++N; + BBInfo.Label = BB; + // Set the parent to the top of the visited stack. The stack includes us, + // and is 1 based, so we subtract to account for both of these. + if (I.getPathLength() > 1) + BBInfo.Parent = NodeToInfo[I.getPath(I.getPathLength() - 2)].DFSNum; + NumToNode.push_back(BB); // NumToNode[n] = V; + } + return N; } - return N; -} -template ::type> -NodePtr Eval(NodePtr VIn, detail::SemiNCAInfo &SNCA, - unsigned LastLinked) { - auto &VInInfo = SNCA.NodeToInfo[VIn]; - if (VInInfo.DFSNum < LastLinked) - return VIn; + NodePtr eval(NodePtr VIn, unsigned LastLinked) { + auto &VInInfo = NodeToInfo[VIn]; + if (VInInfo.DFSNum < LastLinked) + return VIn; - SmallVector Work; - SmallPtrSet Visited; + SmallVector Work; + SmallPtrSet Visited; - if (VInInfo.Parent >= LastLinked) - Work.push_back(VIn); + if (VInInfo.Parent >= LastLinked) + Work.push_back(VIn); - while (!Work.empty()) { - NodePtr V = Work.back(); - auto &VInfo = SNCA.NodeToInfo[V]; - NodePtr VAncestor = SNCA.NumToNode[VInfo.Parent]; + while (!Work.empty()) { + NodePtr V = Work.back(); + auto &VInfo = NodeToInfo[V]; + NodePtr VAncestor = NumToNode[VInfo.Parent]; - // Process Ancestor first - if (Visited.insert(VAncestor).second && VInfo.Parent >= LastLinked) { - Work.push_back(VAncestor); - continue; + // Process Ancestor first + if (Visited.insert(VAncestor).second && VInfo.Parent >= LastLinked) { + Work.push_back(VAncestor); + continue; + } + Work.pop_back(); + + // Update VInfo based on Ancestor info + if (VInfo.Parent < LastLinked) + continue; + + auto &VAInfo = NodeToInfo[VAncestor]; + NodePtr VAncestorLabel = VAInfo.Label; + NodePtr VLabel = VInfo.Label; + if (NodeToInfo[VAncestorLabel].Semi < NodeToInfo[VLabel].Semi) + VInfo.Label = VAncestorLabel; + VInfo.Parent = VAInfo.Parent; } - Work.pop_back(); - - // Update VInfo based on Ancestor info - if (VInfo.Parent < LastLinked) - continue; - - auto &VAInfo = SNCA.NodeToInfo[VAncestor]; - NodePtr VAncestorLabel = VAInfo.Label; - NodePtr VLabel = VInfo.Label; - if (SNCA.NodeToInfo[VAncestorLabel].Semi < SNCA.NodeToInfo[VLabel].Semi) - VInfo.Label = VAncestorLabel; - VInfo.Parent = VAInfo.Parent; - } - return VInInfo.Label; -} + return VInInfo.Label; + } +}; +} // namespace detail template void Calculate(DominatorTreeBaseByGraphTraits> &DT, @@ -216,9 +206,9 @@ if (DT.isPostDominator()){ for (unsigned i = 0, e = static_cast(DT.Roots.size()); i != e; ++i) - N = ReverseDFSPass(DT.Roots[i], SNCA, N); + N = SNCA.runReverseDFS(DT.Roots[i], N); } else { - N = DFSPass(DT.Roots[0], SNCA, N); + N = SNCA.runDFS(DT.Roots[0], N); } // It might be that some blocks did not get a DFS number (e.g., blocks of @@ -241,7 +231,7 @@ WInfo.Semi = WInfo.Parent; for (const auto &N : inverse_children(W)) if (SNCA.NodeToInfo.count(N)) { // Only if this predecessor is reachable! - unsigned SemiU = SNCA.NodeToInfo[Eval(N, SNCA, i + 1)].Semi; + unsigned SemiU = SNCA.NodeToInfo[SNCA.eval(N, i + 1)].Semi; if (SemiU < WInfo.Semi) WInfo.Semi = SemiU; }