iteratedDominanceFrontier.h

Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include <boostGraphCFG.h>
00004 #include "rose.h"
00005 #include <vector>
00006 #include <set>
00007 #include <map>
00008 #include <iterator>
00009 #include <boost/foreach.hpp>
00010 #include <boost/graph/adjacency_list.hpp>
00011 #include <boost/graph/topological_sort.hpp>
00012 
00013 namespace ssa_private
00014 {
00015     using namespace std;
00016     using namespace boost;
00017 
00020     template<class CfgNodeT>
00021     set<CfgNodeT> calculateIteratedDominanceFrontier(const map<CfgNodeT, set<CfgNodeT> >& dominanceFrontiers,
00022     const vector<CfgNodeT>& startNodes)
00023     {
00024         set<CfgNodeT> result;
00025         set<CfgNodeT> visitedNodes;
00026         vector<CfgNodeT> worklist;
00027 
00028         worklist.insert(worklist.end(), startNodes.begin(), startNodes.end());
00029 
00030         while (!worklist.empty())
00031         {
00032             CfgNodeT currentNode = worklist.back();
00033             worklist.pop_back();
00034             visitedNodes.insert(currentNode);
00035 
00036             //Get the dominance frontier of the node and add it to the results
00037             ROSE_ASSERT(dominanceFrontiers.count(currentNode) != 0);
00038             const set<CfgNodeT>& dominanceFrontier = dominanceFrontiers.find(currentNode)->second;
00039 
00040             //Add all the children to the result and to the worklist
00041 
00042             BOOST_FOREACH(CfgNodeT dfNode, dominanceFrontier)
00043             {
00044                 if (visitedNodes.count(dfNode) > 0)
00045                     continue;
00046 
00047                 result.insert(dfNode);
00048                 worklist.push_back(dfNode);
00049             }
00050         }
00051 
00052         return result;
00053     }
00054 
00058     template<class CfgNodeT, class CfgEdgeT>
00059     map<CfgNodeT, set<CfgNodeT> > calculateDominanceFrontiers(SgFunctionDefinition* func, map<CfgNodeT, CfgNodeT>* iDominatorMap,
00060     map<CfgNodeT, CfgNodeT>* iPostDominatorMap)
00061     {
00062         typedef CFG<CfgNodeT, CfgEdgeT> ControlFlowGraph;
00063 
00064         //Build a CFG first
00065         ControlFlowGraph functionCfg(func);
00066 
00067         //Build the dominator tree
00068         typename ControlFlowGraph::VertexVertexMap dominatorTreeMap = functionCfg.getDominatorTree();
00069 
00070         //TODO: This code converts a VertexVertex Map to a  boost graph. Should be factored out
00071         typedef adjacency_list<vecS, vecS, bidirectionalS, CfgNodeT> TreeType;
00072         TreeType domTree;
00073         typedef typename graph_traits<TreeType>::vertex_descriptor TreeVertex;
00074 
00075         set<CfgNodeT> addedNodes;
00076         map<CfgNodeT, TreeVertex> cfgNodeToVertex;
00077 
00078         BOOST_FOREACH(typename ControlFlowGraph::VertexVertexMap::value_type& nodeDominatorPair, dominatorTreeMap)
00079         {
00080             CfgNodeT node = *functionCfg[nodeDominatorPair.first];
00081             CfgNodeT dominator = *functionCfg[nodeDominatorPair.second];
00082 
00083             if (addedNodes.count(dominator) == 0)
00084             {
00085                 TreeVertex newVertex = add_vertex(domTree);
00086                 cfgNodeToVertex[dominator] = newVertex;
00087                 domTree[newVertex] = dominator;
00088                 addedNodes.insert(dominator);
00089             }
00090 
00091             if (addedNodes.count(node) == 0)
00092             {
00093                 TreeVertex newVertex = add_vertex(domTree);
00094                 cfgNodeToVertex[node] = newVertex;
00095                 domTree[newVertex] = node;
00096                 addedNodes.insert(node);
00097             }
00098 
00099             //Add the edge from dominator to node
00100             add_edge(cfgNodeToVertex[dominator], cfgNodeToVertex[node], domTree);
00101 
00102             if (iDominatorMap != NULL)
00103             {
00104                 ROSE_ASSERT(iDominatorMap->count(node) == 0);
00105                 iDominatorMap->insert(make_pair(node, dominator));
00106             }
00107         }
00108 
00109         //Get a topological ordering of the vertices
00110         vector<TreeVertex> reverseTopological;
00111         topological_sort(domTree, back_inserter(reverseTopological));
00112 
00113         //Calculate all the dominance frontiers. This algorithm is from figure 10, Cytron et. al 1991
00114         map<CfgNodeT, set<CfgNodeT> > dominanceFrontiers;
00115 
00116         BOOST_FOREACH(TreeVertex v, reverseTopological)
00117         {
00118             CfgNodeT currentNode = domTree[v];
00119             set<CfgNodeT>& currentDominanceFrontier = dominanceFrontiers[currentNode];
00120 
00121             //Local contribution: Iterate over all the successors of v in the control flow graph
00122 
00123             BOOST_FOREACH(CfgEdgeT outEdge, currentNode.outEdges())
00124             {
00125                 CfgNodeT successor = outEdge.target();
00126 
00127                 //Get the immediate dominator of the successor
00128                 typename ControlFlowGraph::Vertex successorVertex = functionCfg.getVertexForNode(successor);
00129 #if !USE_ROSE
00130              // DQ (11/3/2011): EDG compilains about this (but GNU allowed it, I think that EDG might be correct.
00131              // since it might be a private variable.  But since we are only trying to compile ROSE with ROSE (using the
00132              // new EDG 4.3 front-end as a tests) we can just skip this case for now.
00133                 ROSE_ASSERT(successorVertex != ControlFlowGraph::GraphTraits::null_vertex());
00134 #endif
00135                 ROSE_ASSERT(dominatorTreeMap.count(successorVertex) == 1);
00136                 typename ControlFlowGraph::Vertex iDominatorVertex = dominatorTreeMap[successorVertex];
00137                 CfgNodeT iDominator = *functionCfg[iDominatorVertex];
00138 
00139                 //If we have a successor that we don't dominate, that successor is in our dominance frontier
00140                 if (iDominator != currentNode)
00141                 {
00142                     currentDominanceFrontier.insert(successor);
00143                 }
00144             }
00145 
00146             //"Up" contribution. Iterate over all children in the dominator tree
00147             typename graph_traits<TreeType>::adjacency_iterator currentIter, lastIter;
00148             for (tie(currentIter, lastIter) = adjacent_vertices(v, domTree); currentIter != lastIter; currentIter++)
00149             {
00150                 CfgNodeT childNode = domTree[*currentIter];
00151                 const set<CfgNodeT>& childDominanceFrontier = dominanceFrontiers[childNode];
00152 
00153                 BOOST_FOREACH(CfgNodeT childDFNode, childDominanceFrontier)
00154                 {
00155                     //Get the immediate dominator of the child DF node
00156                     typename ControlFlowGraph::Vertex childDFVertex = functionCfg.getVertexForNode(childDFNode);
00157 #if !USE_ROSE
00158                  // DQ (11/3/2011): EDG compilains about this (but GNU allowed it, I think that EDG might be correct.
00159                  // since it might be a private variable.  But since we are only trying to compile ROSE with ROSE (using the
00160                  // new EDG 4.3 front-end as a tests) we can just skip this case for now.
00161                     ROSE_ASSERT(childDFVertex != ControlFlowGraph::GraphTraits::null_vertex());
00162 #endif
00163                     ROSE_ASSERT(dominatorTreeMap.count(childDFVertex) == 1);
00164                     typename ControlFlowGraph::Vertex iDominatorVertex = dominatorTreeMap[childDFVertex];
00165                     CfgNodeT iDominator = *functionCfg[iDominatorVertex];
00166 
00167                     if (iDominator != currentNode)
00168                     {
00169                         currentDominanceFrontier.insert(childDFNode);
00170                     }
00171                 }
00172             }
00173         }
00174 
00175         //While we're at it, calculate the postdominator tree
00176         if (iPostDominatorMap != NULL)
00177         {
00178             typename ControlFlowGraph::VertexVertexMap postDominatorTreeMap = functionCfg.getPostdominatorTree();
00179 
00180             BOOST_FOREACH(typename ControlFlowGraph::VertexVertexMap::value_type& nodePostDominatorPair, postDominatorTreeMap)
00181             {
00182                 CfgNodeT node = *functionCfg[nodePostDominatorPair.first];
00183                 CfgNodeT postDominator = *functionCfg[nodePostDominatorPair.second];
00184 
00185                 ROSE_ASSERT(iPostDominatorMap->count(node) == 0);
00186                 iPostDominatorMap->insert(make_pair(node, postDominator));
00187             }
00188         }
00189 
00190         return dominanceFrontiers;
00191     }
00192 
00193 }

Generated on Sat May 19 00:53:06 2012 for ROSE by  doxygen 1.4.7