ShDomTree.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2006 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 
00021 #ifndef SHDOMTREE_HPP
00022 #define SHDOMTREE_HPP
00023 
00024 #include <set>
00025 #include <map>
00026 #include <vector>
00027 #include <algorithm>
00028 #include "ShDllExport.hpp"
00029 #include "ShCtrlGraph.hpp"
00030 #include "ShRefCount.hpp"
00031 
00032 namespace SH {
00033 
00036   class
00037   SH_DLLEXPORT ShDomTree {
00038   public:
00039     ShDomTree(ShCtrlGraphPtr ctrlGraph);
00040 
00042     template<typename T> void postorder(T& f);
00043 
00045     template<typename T> void preorder(T& f);
00046   
00047     void debugDump();
00048 
00049     int numbering(ShCtrlGraphNodePtr node) const {
00050       return (int)(std::find( m_vertex.begin(), m_vertex.end(), node) - m_vertex.begin());
00051     }
00052   
00053   private:
00054     void dfs(ShCtrlGraphNodePtr v);
00055     ShCtrlGraphNodePtr eval(ShCtrlGraphNodePtr v);
00056     void compress(ShCtrlGraphNodePtr v);
00057     void link(ShCtrlGraphNodePtr v, ShCtrlGraphNodePtr w);
00058   
00059     ShCtrlGraphPtr m_graph;
00060 
00061     // See [TODO Ref Langauer & Tarjan pg 127] for more information on these
00062 
00063     typedef std::map<ShCtrlGraphNodePtr, ShCtrlGraphNodePtr> ParentMap;
00064     ParentMap m_parent;
00065     typedef std::set<ShCtrlGraphNodePtr> PredSet;
00066     typedef std::map<ShCtrlGraphNodePtr, PredSet> PredMap;
00067     PredMap m_pred;
00068     typedef std::map<ShCtrlGraphNodePtr, int> SemiMap;
00069     SemiMap m_semi;
00070     std::vector<ShCtrlGraphNodePtr> m_vertex;
00071     typedef std::set<ShCtrlGraphNodePtr> BucketSet;
00072     typedef std::map<ShCtrlGraphNodePtr, BucketSet> BucketMap;
00073     BucketMap m_bucket;
00074     typedef std::map<ShCtrlGraphNodePtr, ShCtrlGraphNodePtr> DomMap;
00075     DomMap m_dom;
00076 
00077     typedef std::map<ShCtrlGraphNodePtr, ShCtrlGraphNodePtr> AncestorMap;
00078     AncestorMap m_ancestor;
00079     typedef std::map<ShCtrlGraphNodePtr, ShCtrlGraphNodePtr> LabelMap;
00080     LabelMap m_label;
00081 
00082     typedef std::set<ShCtrlGraphNodePtr> ChildrenSet;
00083     typedef std::map<ShCtrlGraphNodePtr, ChildrenSet> ChildrenMap;
00084     ChildrenMap m_children;
00085   
00086     int m_n;
00087 
00088     template<typename T> void postorderNode(T& f, ShCtrlGraphNodePtr root, int level = 0);
00089     template<typename T> void preorderNode(T& f, ShCtrlGraphNodePtr root, int level = 0);
00090   };
00091 
00092   template<typename T>
00093   void ShDomTree::postorder(T& f)
00094   {
00095     postorderNode(f, m_graph->entry());
00096   }
00097 
00098   template<typename T>
00099   void ShDomTree::postorderNode(T& f, ShCtrlGraphNodePtr root, int level)
00100   {
00101     ChildrenSet& children = m_children[root];
00102     for (ChildrenSet::iterator I = children.begin(); I != children.end(); ++I) {
00103       postorderNode(f, *I, level + 1);
00104     }
00105     f(root, level);
00106   }
00107 
00108   template<typename T>
00109   void ShDomTree::preorder(T& f)
00110   {
00111     preorderNode(f, m_graph->entry());
00112   }
00113 
00114   template<typename T>
00115   void ShDomTree::preorderNode(T& f, ShCtrlGraphNodePtr root, int level)
00116   {
00117     f(root, level);
00118     ChildrenSet& children = m_children[root];
00119     for (ChildrenSet::iterator I = children.begin(); I != children.end(); ++I) {
00120       preorderNode(f, *I, level + 1);
00121     }
00122   }
00123 
00124 }
00125 
00126 #endif

Generated on Thu Feb 16 14:51:32 2006 for Sh by  doxygen 1.4.6