ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dominanceAnalysis/DominanceFrontier.h
Go to the documentation of this file.
1 #ifndef _DOMINANCEFRONTIER_H_
2 #define _DOMINANCEFRONTIER_H_
3 
4 #include "DominatorTree.h"
5 #include <algorithm>
6 using std::set_difference;
7 
8 namespace DominatorTreesAndDominanceFrontiers
9 {
10 
11  /* ! \class DominanceFrontier
12 
13  This class constructs the dominance (or post-dominance) frontiers for
14  all nodes in a ControlFlowGraph. A dominance (post-dominance) frontier
15  for node X is simply the set of nodes such that a given node Y from the
16  set is not dominated (post-dominated) by X, but there is an immediate
17  predecessor of Y that is dominated (post-dominated) by X.
18 
19  The type of frontier we construct is determined by the DominatorTree
20  that DominanceFrontier is initialized with.
21 
22  */
23 
24  template < typename CFGFilterFunction > class TemplatedDominanceFrontier:public DominatorForwardBackwardWrapperClass <
25  CFGFilterFunction
26  >
27  {
28  public:
30  std::set<int> getFrontier(int node)
31  {
32  if (validID(node))
33  return dominatorFrontier[node];
34  else
35  {
36  std::cerr<<"no such ID ("<<node<<") in dominator-tree\n";
37  ROSE_ASSERT(false);
38  }
39  return std::set<int>();
40  }
41 
45  (dt)
46  {
47  dominatorTree = dt;
48  init();
49  buildFrontier();
50  }
51 
54  {
55  std::cout << "x\tDF\n";
56  for (int i = 0; i < dominatorTree.getSize(); i++)
57  {
58  std::cout <<i<<"\t{";
59  for (std::set<int>::const_iterator j = dominatorFrontier[i].begin(); j != dominatorFrontier[i].end(); ++j) {
60  if (j != dominatorFrontier[i].begin()) std::cout << ", ";
61  std::cout << *j;
62  }
63  std::cout <<"}" << std::endl;
64  }
65  return ;
66 
67  }
68 
69 
70  private:
72  int validID(int id)
73  {
74  if (id>=0 && id<dominatorFrontier.size()) return true;
75  else return false;
76 
77  }
78 
79  // ! The dominator tree the dominance frontiers are based on
81 
83  std::vector < std::set < int > >dominatorFrontier;
84 
86  void init()
87  {
88  for (int i = 0; i < dominatorTree.getSize(); i++)
89  {
90  dominatorFrontier.push_back(std::set < int >());
91  }
92  }
93 
95  {
96  std::vector<std::set<int> > domSets;
97  std::vector<std::set<int> > dfLocal;
98  int nodeCount=dominatorTree.getSize();
99  int i;
100 #ifdef DEBUG
101  std::cout << "x\tDFl\tDS\tDF\n";
102 #endif
103  for (i=0;i<nodeCount;i++)
104  {
105  // make shure that the set acutally exists in the vector
106  domSets.push_back(dominatorTree.getDirectDominatedSet(i));
107  dfLocal.push_back(std::set<int>());
108  // compute the df local
110  // for each child
111  std::vector < VirtualCFG::FilteredCFGEdge < CFGFilterFunction > >outEdges =
112  getDirectionModifiedOutEdges(currentNode);
113  // for all those children
114  for (unsigned int edge = 0; edge < outEdges.size(); edge++)
115  {
117  target(outEdges[edge]);
118  int imDom=dominatorTree.getImDomID(child);
119  int childID=dominatorTree.getID(child);
120  // if the current ID is the immdom of this child, put it in the domSet
121  if (i!=imDom)
122  {
123  // child is not dominated -> frontier
124  dfLocal[i].insert(childID);
125  }
126  }
127  }
128 
129  // in the second pass, remove the dominated nodes from the merged dominance-frontier-set
130  for (i=0;i<nodeCount;i++)
131  {
132  std::set<int> tmp;
133  tmp.insert(dfLocal[i].begin(),dfLocal[i].end());
134  for (std::set<int>::iterator j=domSets[i].begin();j!=domSets[i].end();j++)
135  {
136  tmp.insert(dfLocal[(*j)].begin(),dfLocal[(*j)].end());
137  }
138  set_difference(tmp.begin(),tmp.end(),
139  domSets[i].begin(),domSets[i].end(),
140  inserter(dominatorFrontier[i],dominatorFrontier[i].begin()));
141 #ifdef DEBUG
142  std::cout <<i<<"\t"<<dfLocal[i]<<"\t"<<domSets[i]<<"\t"<<dominatorFrontier[i]<<std::endl;
143 #endif
144 
145  }
146  }
147  };
148 
149  // end of namespace: DominatorTreesAndDominanceFrontiers
150  }
151 
152 #endif