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 library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
00018 // MA  02110-1301, USA
00020 #ifndef SHCTRLGRAPH_HPP
00021 #define SHCTRLGRAPH_HPP
00022 
00023 #include <set>
00024 #include <vector>
00025 #include <list>
00026 #include <iosfwd>
00027 #include "ShDllExport.hpp"
00028 #include "ShRefCount.hpp"
00029 #include "ShBasicBlock.hpp"
00030 #include "ShVariable.hpp"
00031 #include "ShBlock.hpp"
00032 
00033 namespace SH {
00034 
00035 class ShCtrlGraphNode;
00036 
00037 struct
00038 SH_DLLEXPORT ShCtrlGraphBranch {
00039   ShCtrlGraphBranch(const ShPointer<ShCtrlGraphNode>& node,
00040                     ShVariable cond);
00041   
00042   ShPointer<ShCtrlGraphNode> node; 
00043   ShVariable cond; 
00044 };
00045 
00056 class
00057 SH_DLLEXPORT ShCtrlGraphNode : public ShRefCountable, public ShInfoHolder {
00058 public:
00059   ShCtrlGraphNode();
00060   ~ShCtrlGraphNode();
00061 
00062   ShBasicBlockPtr block;
00063   typedef std::vector<ShCtrlGraphBranch> SuccessorList;
00064   SuccessorList successors; 
00065   ShPointer<ShCtrlGraphNode> follower; 
00066 
00067   typedef std::list<ShCtrlGraphNode*> ShPredList;
00068   ShPredList predecessors;
00069 
00072   std::ostream& print(std::ostream& out, int indent) const;
00073 
00076   std::ostream& graphvizDump(std::ostream& out) const;
00077 
00079   void append(const ShPointer<ShCtrlGraphNode>& node);
00080 
00082   void append(const ShPointer<ShCtrlGraphNode>& node,
00083               ShVariable cond);
00084 
00098   ShPointer<ShCtrlGraphNode> 
00099   split(ShBasicBlock::ShStmtList::iterator stmt);
00100 
00101   typedef std::set<ShVariableNodePtr> DeclSet;
00102   typedef DeclSet::const_iterator DeclIt;
00103 
00105   void addDecl(ShVariableNodePtr node);
00106 
00108   bool hasDecl(ShVariableNodePtr node) const;
00109 
00111   void insert_decls(DeclIt f, DeclIt l);
00112 
00114   DeclIt decl_begin() const;
00115   DeclIt decl_end() const;
00116 
00117 
00120   bool marked() const;
00121   
00122   // Technically these should not be const. But they're useful in
00123   // functions which are const, so we just make the "marked" flag
00124   // mutable.
00125   void mark() const; 
00126   void clearMarked() const; 
00127 
00128   
00129   template<typename F>
00130   void dfs(F& functor);
00131 
00132   template<typename F>
00133   void dfs(F& functor) const;
00134 
00135 private:
00136   template<typename F>
00137   void real_dfs(F& functor);
00138 
00139   template<typename F>
00140   void real_dfs(F& functor) const;
00141 
00142   mutable bool m_marked;
00143 
00144   DeclSet m_decls; 
00145 };
00146 
00147 typedef ShPointer<ShCtrlGraphNode> ShCtrlGraphNodePtr;
00148 typedef ShPointer<const ShCtrlGraphNode> ShCtrlGraphNodeCPtr;
00149 
00153 class
00154 SH_DLLEXPORT ShCtrlGraph : public ShRefCountable {
00155 public:
00156   ShCtrlGraph(ShCtrlGraphNodePtr head, ShCtrlGraphNodePtr tail);
00157   ShCtrlGraph(ShBlockListPtr blocks);
00158   ~ShCtrlGraph();
00159 
00160   std::ostream& print(std::ostream& out, int indent) const;
00161   
00162   std::ostream& graphvizDump(std::ostream& out) const;
00163 
00164   ShCtrlGraphNodePtr entry() const;
00165   ShCtrlGraphNodePtr exit() const;
00166 
00168   // New entry is marked (so it does not prevent clearMarking on future DFSes)
00169   ShCtrlGraphNodePtr prependEntry();
00170 
00172   ShCtrlGraphNodePtr appendExit();
00173 
00175   void prepend(ShPointer<ShCtrlGraph> cfg); 
00176 
00178   void append(ShPointer<ShCtrlGraph> cfg); 
00179 
00180 
00181   template<typename F>
00182   void dfs(F& functor);
00183   
00184   template<typename F>
00185   void dfs(F& functor) const;
00186   
00188   void computePredecessors();
00189 
00190   // Make a copy of this control graph placing the result into head and tail.
00191   void copy(ShCtrlGraphNodePtr& head, ShCtrlGraphNodePtr& tail) const;
00192   
00193 private:
00194   ShCtrlGraphNodePtr m_entry;
00195   ShCtrlGraphNodePtr m_exit;
00196 
00197   //  void parse(ShCtrlGraphNodePtr& tail, ShBlockListPtr blocks);
00198 };
00199 
00200 typedef ShPointer<ShCtrlGraph> ShCtrlGraphPtr;
00201 typedef ShPointer<const ShCtrlGraph> ShCtrlGraphCPtr;
00202 
00203 template<typename F>
00204 void ShCtrlGraphNode::real_dfs(F& functor)
00205 {
00206   if (this == 0) return;
00207   if (m_marked) return;
00208   mark();
00209   functor(this);
00210   for (std::vector<ShCtrlGraphBranch>::iterator I = successors.begin(); I != successors.end(); ++I) {
00211     I->node->real_dfs(functor);
00212   }
00213   if (follower) follower->real_dfs(functor);
00214 }
00215 
00216 template<typename F>
00217 void ShCtrlGraphNode::real_dfs(F& functor) const
00218 {
00219   if (this == 0) return;
00220   if (m_marked) return;
00221   mark();
00222   functor(this);
00223   for (std::vector<ShCtrlGraphBranch>::const_iterator I = successors.begin();
00224        I != successors.end(); ++I) {
00225     I->node->real_dfs(functor);
00226   }
00227   if (follower) follower->real_dfs(functor);
00228 }
00229 
00230 template<typename F>
00231 void ShCtrlGraphNode::dfs(F& functor)
00232 {
00233   clearMarked();
00234   real_dfs(functor);
00235   clearMarked();
00236 }
00237 
00238 template<typename F>
00239 void ShCtrlGraphNode::dfs(F& functor) const
00240 {
00241   clearMarked();
00242   real_dfs(functor);
00243   clearMarked();
00244 }
00245 
00246 template<typename F>
00247 void ShCtrlGraph::dfs(F& functor)
00248 {
00249   m_entry->dfs(functor);
00250 }
00251 
00252 template<typename F>
00253 void ShCtrlGraph::dfs(F& functor) const
00254 {
00255   m_entry->dfs(functor);
00256 }
00257 
00258 }
00259 
00260 #endif

Generated on Thu Jul 28 17:33:02 2005 for Sh by  doxygen 1.4.3-20050530