ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DefUseAnalysis.h
Go to the documentation of this file.
1 /******************************************
2  * Category: DFA
3  * DefUse Analysis Declaration
4  * created by tps in Feb 2007
5  *****************************************/
6 
7 #ifndef __DefUseAnalysis_HXX_LOADED__
8 #define __DefUseAnalysis_HXX_LOADED__
9 #include <string>
10 
11 #include "filteredCFG.h"
12 #include "DFAnalysis.h"
13 #include "support.h"
14 #include "DFAFilter.h"
15 
16 #include <iostream>
17 
18 // CH (4/9/2010): Use boost::unordered instead
19 #include <boost/unordered_map.hpp>
20 #include <boost/unordered_set.hpp>
21 
22 #if 0
23 #ifdef _MSC_VER
24 #include <hash_map>
25 #include <hash_set>
26 #if 0
27 template <> struct hash <SgNode*> {
28  size_t operator()(SgNode* const & n) const {
29  return (size_t) n;
30  }
31  size_t operator()(SgNode* const & n1, SgNode* const & n2) const {
32  ROSE_ASSERT(n1);
33  ROSE_ASSERT(n2);
34  return ( n1==n2);
35  }
36  };
37 #endif
38 #else
39 #include <ext/hash_map>
40 #include <ext/hash_set>
41 
42 namespace rose_hash {
43  template <> struct hash <SgNode*> {
44  size_t operator()(SgNode* const & n) const {
45  return (size_t) n;
46  }
47  size_t operator()(SgNode* const & n1, SgNode* const & n2) const {
48  ROSE_ASSERT(n1);
49  ROSE_ASSERT(n2);
50  return ( n1==n2);
51  }
52  };
53 }
54 #endif
55 
56 #endif
57 
58 
60  private:
62  bool DEBUG_MODE;
64  std::vector<SgInitializedName*> globalVarList;
65 
67 
68  // def-use-specific --------------------
69  typedef std::vector < std::pair<SgInitializedName* , SgNode*> > multitype;
70  // typedef std::multimap < SgInitializedName* , SgNode* > multitype;
71 
72  typedef std::map< SgNode* , multitype > tabletype;
73  // typedef std::map< SgNode* , int > convtype;
74 // CH (4/9/2010): Use boost::unordered instead
75 //#ifdef _MSC_VER
76 #if 0
77  typedef hash_map< SgNode* , int > convtype;
78 #else
79  //typedef __gnu_cxx::hash_map< SgNode* , int > convtype;
80  typedef rose_hash::unordered_map< SgNode* , int > convtype;
81 #endif
82 
83  // local functions ---------------------
84  void find_all_global_variables();
85  bool start_traversal_of_functions();
86  bool searchMap(const tabletype* ltable, SgNode* node);
87  bool searchVizzMap(SgNode* node);
88  std::string getInitName(SgNode* sgNode);
89 
90  // the main table of all entries
93  // table for indirect definitions
94  //ideftype idefTable;
95  // the helper table for visualization
97  static int sgNodeCounter ;
99 
100  // functions to be printed in DFAtoDOT
101  std::vector <FilteredCFGNode < IsDFAFilter > > dfaFunctions;
102 
103  void addAnyElement(tabletype* tabl, SgNode* sgNode, SgInitializedName* initName, SgNode* defNode);
104  void mapAnyUnion(tabletype* tabl, SgNode* before, SgNode* other, SgNode* current);
105  void printAnyMap(tabletype* tabl);
106 
107 
108  public:
110  DEBUG_MODE(false), DEBUG_MODE_EXTRA(false){
111  //visualizationEnabled=true;
112  //table.clear();
113  //usetable.clear();
114  //globalVarList.clear();
115  //vizzhelp.clear();
116  //sgNodeCounter=0;
117  };
118  virtual ~DefUseAnalysis() {}
119 
120  std::map< SgNode* , multitype > getDefMap() { return table;}
121  std::map< SgNode* , multitype > getUseMap() { return usetable;}
122  void setMaps(std::map< SgNode* , multitype > def,
123  std::map< SgNode* , multitype > use) {
124  table = def;
125  usetable = use;
126  }
127 
128  // def-use-public-functions -----------
129  int run();
130  int run(bool debug);
131  multitype getDefMultiMapFor(SgNode* node);
132  multitype getUseMultiMapFor(SgNode* node);
133  std::vector < SgNode* > getAnyFor(const multitype* mul, SgInitializedName* initName);
134  std::vector < SgNode* > getDefFor(SgNode* node, SgInitializedName* initName);
135  std::vector < SgNode* > getUseFor(SgNode* node, SgInitializedName* initName);
137  std::vector <SgInitializedName*> getGlobalVariables();
138  // the following one is used for parallel traversal
139  int start_traversal_of_one_function(SgFunctionDefinition* proc);
140 
141  // helpers -----------------------------
142  bool searchMap(SgNode* node);
143  int getDefSize();
144  int getUseSize();
145  void printMultiMap(const multitype* type);
146  void printDefMap();
147  void printUseMap();
148 
149  bool addID(SgNode* sgNode);
150  void addDefElement(SgNode* sgNode, SgInitializedName* initName, SgNode* defNode);
151  void addUseElement(SgNode* sgNode, SgInitializedName* initName, SgNode* defNode);
152  void replaceElement(SgNode* sgNode, SgInitializedName* initName);
153  void mapDefUnion(SgNode* before, SgNode* other, SgNode* current);
154  void mapUseUnion(SgNode* before, SgNode* other, SgNode* current);
155 
156  void clearUseOfElement(SgNode* sgNode, SgInitializedName* initName);
157 
158  int getIntForSgNode(SgNode* node);
159  void dfaToDOT();
160  //void addIDefElement(SgNode* sgNode, SgInitializedName* initName);
161 
162  // clear the tables if necessary
163  void flush() {
164  table.clear();
165  usetable.clear();
166  globalVarList.clear();
167  vizzhelp.clear();
168  sgNodeCounter=1;
169  // nrOfNodesVisited=0;
170  }
171 
172  void flushHelp() {
173  vizzhelp.clear();
174  sgNodeCounter=1;
175  }
176 
178  visualizationEnabled=false;
179  }
180 
181  void flushDefuse() {
182  table.clear();
183  usetable.clear();
184  // vizzhelp.clear();
185  //sgNodeCounter=1;
186  }
187 };
188 
189 #endif