ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dominanceAnalysis/DominatorTree.h
Go to the documentation of this file.
1 #ifndef _DOMINATORTREE_H_
2 #define _DOMINATORTREE_H_
3 
4 #include <GraphDotOutput.h>
5 #include <map>
6 #include "filteredCFG.h"
7 // #include "rose.h"
8 namespace DominatorTreesAndDominanceFrontiers
9 {
10  /* ! \class TemplatedDominatorTree
11  This class constructs either a dominator or a post-dominator tree for a control-flow-graph. */
12 
13  typedef enum Dir_ection
14  {
15  PRE_DOMINATOR, /* !< This indicates that we are building a
16  dominator tree */
17  POST_DOMINATOR /* !< This indicates that we are building a
18  post-dominator tree */
19  } Direction;
21  template < typename CFGFilterFunction > class DominatorForwardBackwardWrapperClass
22  {
23  public:
24  // ! Constructor for the DominatorForwardBackwardWrapperClass
26  {
27  };
28 
29  // ! returns whether this is a dominator tree (PRE) or a
30  // post-dominator tree (POST)
32  {
33  return treeDirection;
34  }
35 
36  protected:
38  std::vector < VirtualCFG::FilteredCFGEdge < CFGFilterFunction > > getDirectionModifiedOutEdges(VirtualCFG::FilteredCFGNode < CFGFilterFunction >
39  current)
40  {
42  return current.outEdges();
43  else
44  return current.inEdges();
45  }
46  std::vector < VirtualCFG::FilteredCFGEdge < CFGFilterFunction > > getDirectionModifiedInEdges(VirtualCFG::FilteredCFGNode < CFGFilterFunction >
47  current)
48  {
50  return current.inEdges();
51  else
52  return current.outEdges();
53  }
54 
56  CFGFilterFunction > outedge)
57  {
59  return outedge.target();
60  else
61  return outedge.source();
62  }
63 
65  CFGFilterFunction > outedge)
66  {
68  return outedge.source();
69  else
70  return outedge.target();
71  }
72 
73  // ! treeDirection stores the traversal direction and indicates construction of a dominator or post-dominator tree
75  };
76 
77  // CI (01/23/2007): Implemented the DT for the VirtualCFG interface with
78  // the Lingauer-Tarjan algorithm
80  template < typename CFGFilterFunction > class TemplatedDominatorTree:public DominatorForwardBackwardWrapperClass <CFGFilterFunction >
81  {
82  public:
84  void writeDot(char *filename);
85 
87  //Direction determines Pre/Post-Dominator construction
88 #ifdef _MSC_VER
90 #else
92 #endif
93  // TemplatedDominatorTree(VirtualCFG::FilteredCFGNode<CFGFilterFunction>
94  // cfg , Direction d = PRE);
95 
96  // ! get the CFG the dominator tree is built from
97  // ControlFlowGraph * getCFG() {return _cfg;}
98  // ! returns the corresponding direction for the numbering of the CFG.
99  // ControlFlowGraph::ID_dir getCFGDirection() {return _iddir;}
100 
101  // ! returns the number of nodes in the tree
102  int getSize()
103  {
104  return idom.size();
105  }
106 
108  std::set<int> getDirectDominatedSet(int nodeID)
109  {
110  std::set<int> dds;
111  for (unsigned int i=0;i<idom.size();i++)
112  {
113  if (idom[i]==nodeID) dds.insert(i);
114  }
115  return dds;
116  }
117 /*
119  int isDomintated(int a, int b)
120  {
121  // a node dominates itself
122  if (a == b)
123  return true;
124  int tmp = b;
125 
126  while (tmp > 0 && tmp < a)
127  {
128  tmp = getImDomID(tmp);
129  if (tmp == a)
130  return true;
131  }
132  return false;
133  }
134 */
135  // ! for a given nodeID, return the id of its immediate dominator
136  int getImDomID(int i)
137  {
138  return idom[i];
139  }
140 
143  {
144  int id = getID(node);
145 
146  if (id < 0)
147  return -1;
148  else
149  return getImDomID(id);
150  }
151 
153  bool dominates(int a,int b)
154  {
155  if (a==0) return true;
156  int i=b;
157  while(i>0)
158  {
159  i=getImDomID(i);
160  if (i==a) return true;
161  }
162  return false;
163  }
164 
167  {
168  return dominates(getID(a),getID(b));
169  }
170 
171  // !for an ID return the corresponing CFGNode
173  {
174  if ( /* id >= 0 && */ id < idToNode.size())
175  return idToNode[id];
176  ROSE_ASSERT(false);
177  return cfgRoot;
178  }
179  // ! for an CFG Node, return the corresponding id
181  {
182  if (nodeToIdMap.count(node) == 0)
183  {
184  return -1;
185  }
186  else
187  {
188  return nodeToIdMap[node];
189  }
190  }
191  private:
192  // ! this is the cfg-root for dominator construction, for pre this is
193  // the source, for post this is the sink of the cfg
195 
196  // use a vector for mapping id's to nodes, since all ids are consecutive
197  std::vector < VirtualCFG::FilteredCFGNode < CFGFilterFunction > >idToNode;
198  // a map for mapping nodes to id's,since this might be a sparse set
199  std::map < VirtualCFG::FilteredCFGNode < CFGFilterFunction >, int >nodeToIdMap;
200 
202  void init();
204  void depthFirstSearch();
208  int eval(int);
210  void link(int source, int target);
212  std::vector < int >semi, idom, ancestor, dfsParent;
213  std::vector < std::set < int > > buckets;
214 
216  void printInfo()
217  {
218  std::cout <<"dfs#\tanc \tsemi\tidom\tbucket"<<std::endl;
219  for(unsigned int i=0;i<idToNode.size();i++)
220  {
221  std::cout <<i<<"\t"<<ancestor[i]<<"\t"<<semi[i]<<"\t"<<idom[i]<<"\t"<<buckets[i]<<std::endl;
222  }
223  }
224  };
225 
227  {
229  {
230  // get rid of all beginning nodes
231  if (!cfgn.isInteresting())
232  return false;
233  SgNode *n = cfgn.getNode();
234  if (isSgStatement(n))
235  return true;
236  return false;
237  }
238  };
239 
242 
243  // end of namespace: DominatorTreesAndDominanceFrontiers
244 };
245 
246 #include "DominatorTreeImpl.h"
247 #endif