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

ShCtrlGraph.hpp

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 #ifndef SHCTRLGRAPH_HPP 00028 #define SHCTRLGRAPH_HPP 00029 00030 #include <vector> 00031 #include <list> 00032 #include <iosfwd> 00033 #include "ShDllExport.hpp" 00034 #include "ShRefCount.hpp" 00035 #include "ShBasicBlock.hpp" 00036 #include "ShVariable.hpp" 00037 #include "ShBlock.hpp" 00038 00039 namespace SH { 00040 00041 class ShCtrlGraphNode; 00042 00043 struct 00044 SH_DLLEXPORT ShCtrlGraphBranch { 00045 ShCtrlGraphBranch(const ShPointer<ShCtrlGraphNode>& node, 00046 ShVariable cond); 00047 00048 ShPointer<ShCtrlGraphNode> node; 00049 ShVariable cond; 00050 }; 00051 00062 class 00063 SH_DLLEXPORT ShCtrlGraphNode : public ShRefCountable { 00064 public: 00065 ShCtrlGraphNode(); 00066 ~ShCtrlGraphNode(); 00067 00068 ShBasicBlockPtr block; 00069 typedef std::vector<ShCtrlGraphBranch> SuccessorList; 00070 SuccessorList successors; 00071 ShPointer<ShCtrlGraphNode> follower; 00072 00073 typedef std::list<ShCtrlGraphNode*> ShPredList; 00074 ShPredList predecessors; 00075 00078 std::ostream& print(std::ostream& out, int indent) const; 00079 00082 std::ostream& graphvizDump(std::ostream& out) const; 00083 00085 void append(const ShPointer<ShCtrlGraphNode>& node); 00086 00088 void append(const ShPointer<ShCtrlGraphNode>& node, 00089 ShVariable cond); 00090 00093 bool marked() const; 00094 00095 // Technically these should not be const. But they're useful in 00096 // functions which are const, so we just make the "marked" flag 00097 // mutable. 00098 void mark() const; 00099 void clearMarked() const; 00100 00101 00102 template<typename F> 00103 void dfs(F& functor); 00104 00105 template<typename F> 00106 void dfs(F& functor) const; 00107 00108 private: 00109 template<typename F> 00110 void real_dfs(F& functor); 00111 00112 template<typename F> 00113 void real_dfs(F& functor) const; 00114 00115 mutable bool m_marked; 00116 }; 00117 00118 typedef ShPointer<ShCtrlGraphNode> ShCtrlGraphNodePtr; 00119 typedef ShPointer<const ShCtrlGraphNode> ShCtrlGraphNodeCPtr; 00120 00124 class 00125 SH_DLLEXPORT ShCtrlGraph : public ShRefCountable { 00126 public: 00127 ShCtrlGraph(ShCtrlGraphNodePtr head, ShCtrlGraphNodePtr tail); 00128 ShCtrlGraph(ShBlockListPtr blocks); 00129 ~ShCtrlGraph(); 00130 00131 std::ostream& print(std::ostream& out, int indent) const; 00132 00133 std::ostream& graphvizDump(std::ostream& out) const; 00134 00135 ShCtrlGraphNodePtr entry() const; 00136 ShCtrlGraphNodePtr exit() const; 00137 00139 // New entry is marked (so it does not prevent clearMarking on future DFSes) 00140 ShCtrlGraphNodePtr prependEntry(); 00141 00143 ShCtrlGraphNodePtr appendExit(); 00144 00145 template<typename F> 00146 void dfs(F& functor); 00147 00148 template<typename F> 00149 void dfs(F& functor) const; 00150 00152 void computePredecessors(); 00153 00154 // Make a copy of this control graph placing the result into head and tail. 00155 void copy(ShCtrlGraphNodePtr& head, ShCtrlGraphNodePtr& tail) const; 00156 00157 private: 00158 ShCtrlGraphNodePtr m_entry; 00159 ShCtrlGraphNodePtr m_exit; 00160 00161 // void parse(ShCtrlGraphNodePtr& tail, ShBlockListPtr blocks); 00162 }; 00163 00164 typedef ShPointer<ShCtrlGraph> ShCtrlGraphPtr; 00165 typedef ShPointer<const ShCtrlGraph> ShCtrlGraphCPtr; 00166 00167 template<typename F> 00168 void ShCtrlGraphNode::real_dfs(F& functor) 00169 { 00170 if (this == 0) return; 00171 if (m_marked) return; 00172 mark(); 00173 functor(this); 00174 for (std::vector<ShCtrlGraphBranch>::iterator I = successors.begin(); I != successors.end(); ++I) { 00175 I->node->real_dfs(functor); 00176 } 00177 if (follower) follower->real_dfs(functor); 00178 } 00179 00180 template<typename F> 00181 void ShCtrlGraphNode::real_dfs(F& functor) const 00182 { 00183 if (this == 0) return; 00184 if (m_marked) return; 00185 mark(); 00186 functor(this); 00187 for (std::vector<ShCtrlGraphBranch>::const_iterator I = successors.begin(); 00188 I != successors.end(); ++I) { 00189 I->node->real_dfs(functor); 00190 } 00191 if (follower) follower->real_dfs(functor); 00192 } 00193 00194 template<typename F> 00195 void ShCtrlGraphNode::dfs(F& functor) 00196 { 00197 clearMarked(); 00198 real_dfs(functor); 00199 clearMarked(); 00200 } 00201 00202 template<typename F> 00203 void ShCtrlGraphNode::dfs(F& functor) const 00204 { 00205 clearMarked(); 00206 real_dfs(functor); 00207 clearMarked(); 00208 } 00209 00210 template<typename F> 00211 void ShCtrlGraph::dfs(F& functor) 00212 { 00213 m_entry->dfs(functor); 00214 } 00215 00216 template<typename F> 00217 void ShCtrlGraph::dfs(F& functor) const 00218 { 00219 m_entry->dfs(functor); 00220 } 00221 00222 } 00223 00224 #endif

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