Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

ShDomTree.cpp

00001 // Sh: A GPU metaprogramming language. 00002 // 00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory 00004 // Project administrator: Michael D. McCool 00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa, 00006 // Michael D. McCool 00007 // 00008 // This software is provided 'as-is', without any express or implied 00009 // warranty. In no event will the authors be held liable for any damages 00010 // arising from the use of this software. 00011 // 00012 // Permission is granted to anyone to use this software for any purpose, 00013 // including commercial applications, and to alter it and redistribute it 00014 // freely, subject to the following restrictions: 00015 // 00016 // 1. The origin of this software must not be misrepresented; you must 00017 // not claim that you wrote the original software. If you use this 00018 // software in a product, an acknowledgment in the product documentation 00019 // would be appreciated but is not required. 00020 // 00021 // 2. Altered source versions must be plainly marked as such, and must 00022 // not be misrepresented as being the original software. 00023 // 00024 // 3. This notice may not be removed or altered from any source 00025 // distribution. 00027 00028 #include "ShDomTree.hpp" 00029 #include <iostream> 00030 #include "ShDebug.hpp" 00031 #include "ShUtility.hpp" 00032 00033 00034 namespace SH { 00035 00036 ShDomTree::ShDomTree(ShCtrlGraphPtr graph) 00037 : m_graph(graph), 00038 m_n(0) 00039 { 00040 m_vertex.push_back(0); // Hack to have indexing start at 1. 00041 // Step 1 00042 dfs(m_graph->entry()); 00043 00044 // Steps 2 and 3 00045 for (int i = m_n; i >= 2; i--) { 00046 ShCtrlGraphNodePtr w = m_vertex[i]; 00047 // Step 2 00048 PredSet& pred = m_pred[w]; 00049 for (PredSet::iterator I = pred.begin(); I != pred.end(); ++I) { 00050 ShCtrlGraphNodePtr v = *I; 00051 ShCtrlGraphNodePtr u = eval(v); 00052 if (m_semi[u] < m_semi[v]) m_semi[w] = m_semi[u]; 00053 } 00054 m_bucket[m_vertex[m_semi[w]]].insert(w); 00055 link(m_parent[w], w); 00056 00057 // Step 3 00058 BucketSet& bucket = m_bucket[m_parent[w]]; 00059 while (!bucket.empty()) { 00060 BucketSet::iterator I = bucket.begin(); 00061 ShCtrlGraphNodePtr v = *I; 00062 bucket.erase(I); 00063 ShCtrlGraphNodePtr u = eval(v); 00064 m_dom[v] = (m_semi[u] < m_semi[v] ? u : m_parent[w]); 00065 } 00066 } 00067 00068 // Step 4 00069 for (int i = 2; i <= m_n; i++) { 00070 ShCtrlGraphNodePtr w = m_vertex[i]; 00071 if (m_dom[w] != m_vertex[m_semi[w]]) m_dom[w] = m_dom[m_dom[w]]; 00072 } 00073 m_dom[m_graph->entry()] = 0; 00074 } 00075 00076 struct DebugDumper { 00077 DebugDumper(const ShDomTree& tree) 00078 : tree(tree) 00079 { 00080 } 00081 void operator()(ShCtrlGraphNodePtr node, int level) { 00082 // node->print(std::cerr, 0); 00083 shPrintIndent(std::cerr, level); 00084 SH_DEBUG_PRINT("Node with numbering " << tree.numbering(node)); 00085 } 00086 const ShDomTree& tree; 00087 }; 00088 00089 void ShDomTree::debugDump() 00090 { 00091 #ifdef SH_DEBUG 00092 SH_DEBUG_PRINT("Debug Dump"); 00093 DebugDumper d(*this); 00094 preorder(d); 00095 SH_DEBUG_PRINT("Debug Dump End"); 00096 #endif 00097 } 00098 00099 void ShDomTree::dfs(ShCtrlGraphNodePtr v) 00100 { 00101 if (!v) return; 00102 m_n++; 00103 m_semi[v] = m_n; 00104 m_vertex.push_back(v); 00105 m_ancestor[v] = 0; 00106 m_label[v] = v; 00107 00108 for (std::vector<ShCtrlGraphBranch>::const_iterator I = v->successors.begin(); 00109 I != v->successors.end(); ++I) { 00110 ShCtrlGraphNodePtr w = I->node; 00111 if (m_semi[w] == 0) { 00112 m_parent[w] = v; 00113 dfs(w); 00114 } 00115 m_pred[w].insert(v); 00116 } 00117 ShCtrlGraphNodePtr w = v->follower; 00118 if (w) { 00119 if (m_semi[w] == 0) { 00120 m_parent[w] = v; 00121 dfs(w); 00122 } 00123 m_pred[w].insert(v); 00124 } 00125 } 00126 00127 ShCtrlGraphNodePtr ShDomTree::eval(ShCtrlGraphNodePtr v) 00128 { 00129 if (!m_ancestor[v]) { 00130 return v; 00131 } else { 00132 compress(v); 00133 return m_label[v]; 00134 } 00135 } 00136 00137 void ShDomTree::compress(ShCtrlGraphNodePtr v) 00138 { 00139 // This procedure assumes ancestor[v] != 0 00140 if (m_ancestor[m_ancestor[v]]) { 00141 compress(m_ancestor[v]); 00142 if (m_semi[m_label[m_ancestor[v]]] < m_semi[m_label[v]]) { 00143 m_label[v] = m_label[m_ancestor[v]]; 00144 } 00145 m_ancestor[v] = m_ancestor[m_ancestor[v]]; 00146 } 00147 } 00148 00149 void ShDomTree::link(ShCtrlGraphNodePtr v, ShCtrlGraphNodePtr w) 00150 { 00151 m_ancestor[w] = v; 00152 m_children[v].insert(w); 00153 } 00154 00155 }

Generated on Mon Oct 18 14:17:39 2004 for Sh by doxygen 1.3.7