8#include <boostGraphCFG.h>
13#include <boost/foreach.hpp>
14#include <boost/graph/adjacency_list.hpp>
15#include <boost/graph/topological_sort.hpp>
20 using namespace boost;
24 template<
class CfgNodeT>
25 set<CfgNodeT> calculateIteratedDominanceFrontier(
const map<CfgNodeT, set<CfgNodeT> >& dominanceFrontiers,
26 const vector<CfgNodeT>& startNodes)
29 set<CfgNodeT> visitedNodes;
30 vector<CfgNodeT> worklist;
32 worklist.insert(worklist.end(), startNodes.begin(), startNodes.end());
34 while (!worklist.empty())
36 CfgNodeT currentNode = worklist.back();
38 visitedNodes.insert(currentNode);
41 ROSE_ASSERT(dominanceFrontiers.count(currentNode) != 0);
42 const set<CfgNodeT>& dominanceFrontier = dominanceFrontiers.find(currentNode)->second;
46 BOOST_FOREACH(CfgNodeT dfNode, dominanceFrontier)
48 if (visitedNodes.count(dfNode) > 0)
51 result.insert(dfNode);
52 worklist.push_back(dfNode);
62 template<
class CfgNodeT,
class CfgEdgeT>
63 map<CfgNodeT, set<CfgNodeT> > calculateDominanceFrontiers(
SgFunctionDefinition* func, map<CfgNodeT, CfgNodeT>* iDominatorMap,
64 map<CfgNodeT, CfgNodeT>* iPostDominatorMap)
72 typename ControlFlowGraph::VertexVertexMap dominatorTreeMap = functionCfg.getDominatorTree();
75 typedef adjacency_list<vecS, vecS, bidirectionalS, CfgNodeT> TreeType;
77 typedef typename graph_traits<TreeType>::vertex_descriptor TreeVertex;
79 set<CfgNodeT> addedNodes;
80 map<CfgNodeT, TreeVertex> cfgNodeToVertex;
82 BOOST_FOREACH(
typename ControlFlowGraph::VertexVertexMap::value_type& nodeDominatorPair, dominatorTreeMap)
84 CfgNodeT node = *functionCfg[nodeDominatorPair.first];
85 CfgNodeT dominator = *functionCfg[nodeDominatorPair.second];
87 if (addedNodes.count(dominator) == 0)
89 TreeVertex newVertex = add_vertex(domTree);
90 cfgNodeToVertex[dominator] = newVertex;
91 domTree[newVertex] = dominator;
92 addedNodes.insert(dominator);
95 if (addedNodes.count(node) == 0)
97 TreeVertex newVertex = add_vertex(domTree);
98 cfgNodeToVertex[node] = newVertex;
99 domTree[newVertex] = node;
100 addedNodes.insert(node);
104 add_edge(cfgNodeToVertex[dominator], cfgNodeToVertex[node], domTree);
106 if (iDominatorMap != NULL)
108 ROSE_ASSERT(iDominatorMap->count(node) == 0);
109 iDominatorMap->insert(make_pair(node, dominator));
114 vector<TreeVertex> reverseTopological;
115 topological_sort(domTree, back_inserter(reverseTopological));
118 map<CfgNodeT, set<CfgNodeT> > dominanceFrontiers;
120 BOOST_FOREACH(TreeVertex v, reverseTopological)
122 CfgNodeT currentNode = domTree[v];
123 set<CfgNodeT>& currentDominanceFrontier = dominanceFrontiers[currentNode];
127 BOOST_FOREACH(CfgEdgeT outEdge, currentNode.outEdges())
129 CfgNodeT successor = outEdge.target();
132 typename ControlFlowGraph::Vertex successorVertex = functionCfg.getVertexForNode(successor);
137 ROSE_ASSERT(successorVertex != ControlFlowGraph::GraphTraits::null_vertex());
139 ROSE_ASSERT(dominatorTreeMap.count(successorVertex) == 1);
140 typename ControlFlowGraph::Vertex iDominatorVertex = dominatorTreeMap[successorVertex];
141 CfgNodeT iDominator = *functionCfg[iDominatorVertex];
144 if (iDominator != currentNode)
146 currentDominanceFrontier.insert(successor);
151 typename graph_traits<TreeType>::adjacency_iterator currentIter, lastIter;
152 for (tie(currentIter, lastIter) = adjacent_vertices(v, domTree); currentIter != lastIter; currentIter++)
154 CfgNodeT childNode = domTree[*currentIter];
155 const set<CfgNodeT>& childDominanceFrontier = dominanceFrontiers[childNode];
157 BOOST_FOREACH(CfgNodeT childDFNode, childDominanceFrontier)
160 typename ControlFlowGraph::Vertex childDFVertex = functionCfg.getVertexForNode(childDFNode);
165 ROSE_ASSERT(childDFVertex != ControlFlowGraph::GraphTraits::null_vertex());
167 ROSE_ASSERT(dominatorTreeMap.count(childDFVertex) == 1);
168 typename ControlFlowGraph::Vertex iDominatorVertex = dominatorTreeMap[childDFVertex];
169 CfgNodeT iDominator = *functionCfg[iDominatorVertex];
171 if (iDominator != currentNode)
173 currentDominanceFrontier.insert(childDFNode);
180 if (iPostDominatorMap != NULL)
182 typename ControlFlowGraph::VertexVertexMap postDominatorTreeMap = functionCfg.getPostdominatorTree();
184 BOOST_FOREACH(
typename ControlFlowGraph::VertexVertexMap::value_type& nodePostDominatorPair, postDominatorTreeMap)
186 CfgNodeT node = *functionCfg[nodePostDominatorPair.first];
187 CfgNodeT postDominator = *functionCfg[nodePostDominatorPair.second];
189 ROSE_ASSERT(iPostDominatorMap->count(node) == 0);
190 iPostDominatorMap->insert(make_pair(node, postDominator));
194 return dominanceFrontiers;
This class represents the concept of a scope in C++ (e.g. global scope, fuction scope,...
Sawyer::Container::Graph< CfgVertex, CfgEdge > ControlFlowGraph
Control flow graph.