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

ShCtrlGraph.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #ifndef SHCTRLGRAPH_HPP
00025 #define SHCTRLGRAPH_HPP
00026 
00027 #include <set>
00028 #include <vector>
00029 #include <list>
00030 #include <iosfwd>
00031 #include "ShDllExport.hpp"
00032 #include "ShRefCount.hpp"
00033 #include "ShBasicBlock.hpp"
00034 #include "ShVariable.hpp"
00035 #include "ShBlock.hpp"
00036 
00037 namespace SH {
00038 
00039 class ShCtrlGraphNode;
00040 
00041 struct
00042 SH_DLLEXPORT ShCtrlGraphBranch {
00043   ShCtrlGraphBranch(const ShPointer<ShCtrlGraphNode>& node,
00044                     ShVariable cond);
00045   
00046   ShPointer<ShCtrlGraphNode> node; 
00047   ShVariable cond; 
00048 };
00049 
00060 class
00061 SH_DLLEXPORT ShCtrlGraphNode : public ShRefCountable, public ShInfoHolder {
00062 public:
00063   ShCtrlGraphNode();
00064   ~ShCtrlGraphNode();
00065 
00066   ShBasicBlockPtr block;
00067   typedef std::vector<ShCtrlGraphBranch> SuccessorList;
00068   SuccessorList successors; 
00069   ShPointer<ShCtrlGraphNode> follower; 
00070 
00071   typedef std::list<ShCtrlGraphNode*> ShPredList;
00072   ShPredList predecessors;
00073 
00076   std::ostream& print(std::ostream& out, int indent) const;
00077 
00080   std::ostream& graphvizDump(std::ostream& out) const;
00081 
00083   void append(const ShPointer<ShCtrlGraphNode>& node);
00084 
00086   void append(const ShPointer<ShCtrlGraphNode>& node,
00087               ShVariable cond);
00088 
00102   ShPointer<ShCtrlGraphNode> 
00103   split(ShBasicBlock::ShStmtList::iterator stmt);
00104 
00105   typedef std::set<ShVariableNodePtr> DeclSet;
00106   typedef DeclSet::const_iterator DeclIt;
00107 
00109   void addDecl(ShVariableNodePtr node);
00110 
00112   bool hasDecl(ShVariableNodePtr node) const;
00113 
00115   void insert_decls(DeclIt f, DeclIt l);
00116 
00118   DeclIt decl_begin() const;
00119   DeclIt decl_end() const;
00120 
00121 
00124   bool marked() const;
00125   
00126   // Technically these should not be const. But they're useful in
00127   // functions which are const, so we just make the "marked" flag
00128   // mutable.
00129   void mark() const; 
00130   void clearMarked() const; 
00131 
00132   
00133   template<typename F>
00134   void dfs(F& functor);
00135 
00136   template<typename F>
00137   void dfs(F& functor) const;
00138 
00139 private:
00140   template<typename F>
00141   void real_dfs(F& functor);
00142 
00143   template<typename F>
00144   void real_dfs(F& functor) const;
00145 
00146   mutable bool m_marked;
00147 
00148   DeclSet m_decls; 
00149 };
00150 
00151 typedef ShPointer<ShCtrlGraphNode> ShCtrlGraphNodePtr;
00152 typedef ShPointer<const ShCtrlGraphNode> ShCtrlGraphNodeCPtr;
00153 
00157 class
00158 SH_DLLEXPORT ShCtrlGraph : public ShRefCountable {
00159 public:
00160   ShCtrlGraph(ShCtrlGraphNodePtr head, ShCtrlGraphNodePtr tail);
00161   ShCtrlGraph(ShBlockListPtr blocks);
00162   ~ShCtrlGraph();
00163 
00164   std::ostream& print(std::ostream& out, int indent) const;
00165   
00166   std::ostream& graphvizDump(std::ostream& out) const;
00167 
00168   ShCtrlGraphNodePtr entry() const;
00169   ShCtrlGraphNodePtr exit() const;
00170 
00172   // New entry is marked (so it does not prevent clearMarking on future DFSes)
00173   ShCtrlGraphNodePtr prependEntry();
00174 
00176   ShCtrlGraphNodePtr appendExit();
00177 
00179   void prepend(ShPointer<ShCtrlGraph> cfg); 
00180 
00182   void append(ShPointer<ShCtrlGraph> cfg); 
00183 
00184 
00185   template<typename F>
00186   void dfs(F& functor);
00187   
00188   template<typename F>
00189   void dfs(F& functor) const;
00190   
00192   void computePredecessors();
00193 
00194   // Make a copy of this control graph placing the result into head and tail.
00195   void copy(ShCtrlGraphNodePtr& head, ShCtrlGraphNodePtr& tail) const;
00196   
00197 private:
00198   ShCtrlGraphNodePtr m_entry;
00199   ShCtrlGraphNodePtr m_exit;
00200 
00201   //  void parse(ShCtrlGraphNodePtr& tail, ShBlockListPtr blocks);
00202 };
00203 
00204 typedef ShPointer<ShCtrlGraph> ShCtrlGraphPtr;
00205 typedef ShPointer<const ShCtrlGraph> ShCtrlGraphCPtr;
00206 
00207 template<typename F>
00208 void ShCtrlGraphNode::real_dfs(F& functor)
00209 {
00210   if (this == 0) return;
00211   if (m_marked) return;
00212   mark();
00213   functor(this);
00214   for (std::vector<ShCtrlGraphBranch>::iterator I = successors.begin(); I != successors.end(); ++I) {
00215     I->node->real_dfs(functor);
00216   }
00217   if (follower) follower->real_dfs(functor);
00218 }
00219 
00220 template<typename F>
00221 void ShCtrlGraphNode::real_dfs(F& functor) const
00222 {
00223   if (this == 0) return;
00224   if (m_marked) return;
00225   mark();
00226   functor(this);
00227   for (std::vector<ShCtrlGraphBranch>::const_iterator I = successors.begin();
00228        I != successors.end(); ++I) {
00229     I->node->real_dfs(functor);
00230   }
00231   if (follower) follower->real_dfs(functor);
00232 }
00233 
00234 template<typename F>
00235 void ShCtrlGraphNode::dfs(F& functor)
00236 {
00237   clearMarked();
00238   real_dfs(functor);
00239   clearMarked();
00240 }
00241 
00242 template<typename F>
00243 void ShCtrlGraphNode::dfs(F& functor) const
00244 {
00245   clearMarked();
00246   real_dfs(functor);
00247   clearMarked();
00248 }
00249 
00250 template<typename F>
00251 void ShCtrlGraph::dfs(F& functor)
00252 {
00253   m_entry->dfs(functor);
00254 }
00255 
00256 template<typename F>
00257 void ShCtrlGraph::dfs(F& functor) const
00258 {
00259   m_entry->dfs(functor);
00260 }
00261 
00262 }
00263 
00264 #endif

Generated on Wed Jun 15 18:12:39 2005 for Sh by  doxygen 1.4.3-20050530