ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IntraProcAliasAnalysis.h
Go to the documentation of this file.
1 #ifndef INTRA_PROC_ALIAS_ANALYSIS_H
2 #define INTRA_PROC_ALIAS_ANALYSIS_H
4 #include "customFilteredCFG.h"
5 #include "ClassHierarchyGraph.h"
6 #include "CallGraph.h"
7 #include <boost/foreach.hpp>
8 #include <boost/shared_ptr.hpp>
9 #include <boost/unordered_map.hpp>
10 #include <algorithm>
11 
12 #define foreach BOOST_FOREACH
13 #define reverse_foreach BOOST_REVERSE_FOREACH
14 
15 
16 using namespace boost;
17 using namespace std;
18 
29 
30  bool operator==(const AliasRelationNode &that) const {
31  assert(this != NULL);
32  return (this->var == that.var && this->derefLevel == that.derefLevel);
33  }
34 
35 };
36 
37 
39 {
45  bool operator() (CFGNode cfgn) const
46  {
47  SgNode *node = cfgn.getNode();
48  switch (node->variantT())
49  {
50  case V_SgBasicBlock:
51  return cfgn == node->cfgForBeginning() || cfgn == node->cfgForBeginning();
53  case V_SgAssignOp:
55  //return (cfgn == node->cfgForBeginning());
56  return (cfgn == node->cfgForEnd());
57  case V_SgConditionalExp:
58  return (cfgn.getIndex() == 1);
61  case V_SgReturnStmt:
62  return (cfgn == node->cfgForBeginning());
63  //return (cfgn == node->cfgForEnd());
64  default:
65  return false;
66 
67  }
68  }
69 };
70 // Base class for CompactRepresentation
72 
73 
74 public:
76  }
78  virtual void computeAliases (SgVariableSymbol *var, int derefLevel, vector<SgGraphNode *> &) = 0;
79 
81  virtual void computeAliases (SgGraphNode *node, int derefLevel, vector<SgGraphNode *> &) = 0;
82 
84  virtual void addMustAliasRelation(const AliasRelationNode &left, const AliasRelationNode &right) = 0;
85 
87  virtual void addMayAliasRelation(const AliasRelationNode &left, const AliasRelationNode &right) = 0;
88 
90  virtual unsigned long getHash() const = 0;
92  virtual SgIncidenceDirectedGraph * getGraph() const = 0;
93 
95  virtual void merge(const CompReprBase &) = 0;
96 
98  virtual void toDot(const std::string& file_name) = 0;
99 
100 protected:
102  unsigned long hash;
107  delete graph;
108  graph = NULL;
109  }
110 };
111 
112 // The actual Compact Representation. It's implemented
113 // as a graph(SgIncidenceDirectedGraph).
115 
117  unordered_map<SgNode *,SgGraphNode *> all_nodes;
118 
121 
123  void merge(SgIncidenceDirectedGraph *thatGraph);
124 
126  void updateHash(SgNode *from, SgNode *to);
127 
129  void init();
130 
132  void addEdge(SgGraphNode *g_from, SgGraphNode *g_to);
133 
135  void processNodes(std::ostream & o, SgGraphNode* n, std::set<SgGraphNode*>& explored);
136 
138  void printNodePlusEdges(std::ostream & o, SgGraphNode* node);
139 
141  void printNode(std::ostream & o, SgGraphNode* node);
142 
144  void printEdge(std::ostream & o, SgDirectedGraphEdge* edge, bool isInEdge);
145 
146 
147 public:
149  unordered_map<SgNode *,SgGraphNode *> getNodesMapping(){ return all_nodes;}
151 
153  SgIncidenceDirectedGraph * getGraph() const{ return graph; }
154 
156  unsigned long getHash() const { return hash; }
157 
160 
162  CompactRepresentation& operator=(const CompactRepresentation& p);
163 
165  void computeAliases (SgVariableSymbol *var, int derefLevel, vector<SgGraphNode *> &nodes);
166 
168  void computeAliases (SgGraphNode *node, int derefLevel, vector<SgGraphNode *> &);
169 
171  void addMustAliasRelation(const AliasRelationNode &left, const AliasRelationNode &right);
172 
174  void addMayAliasRelation(const AliasRelationNode &left, const AliasRelationNode &right);
175 
177  void merge(const CompReprBase &that);
178 
180  bool operator==(const CompactRepresentation &that) const;
181 
183  bool operator !=(const CompactRepresentation &that) const;
184 
186  void toDot(const std::string& file_name);
187 };
188 
190 class CompReprPtr {
192  boost::shared_ptr<CompReprBase> ptr;
193 public:
195  CompReprPtr() { ptr = boost::shared_ptr<CompReprBase>(); }
196 
198  CompReprPtr(CompactRepresentation *repr) { ptr = boost::shared_ptr<CompReprBase>(repr); }
199 
201  CompReprBase * get() const{ return ptr.get(); }
202 
203  // Equality operator overload. Compares two CompactRepresentations
204  bool operator==(const CompReprPtr &that) const;
205 
206  // Not Equality operator overload. Compares two CompactRepresentations
207  bool operator!=(const CompReprPtr &that) const { return !(*this == that); }
208 
209  // += operator overload
210  void operator+=(const CompReprPtr &that) const;
211 };
212 
216  unordered_map<SgGraphNode *, CompReprPtr> ins;
217 
219  unordered_map<SgGraphNode *, CompReprPtr> outs;
220 
222  std::vector <AliasRelationNode > returnStmts;
223 
225  unordered_map<SgGraphNode *, std::vector <std::pair<AliasRelationNode, AliasRelationNode> > >aliasRelations;
226 
227 public :
229 
231  void init(SgGraphNode *n);
232 
234  CompReprPtr getEntryData(SgGraphNode *node);
235 
237  void setEntryData(SgGraphNode *node, CompReprPtr en) { ins[node] = en; }
238 
240  CompReprPtr getExitData(SgGraphNode *node);
241 
243  void setExitData(SgGraphNode *node, CompReprPtr en) { outs[node] = en; }
244 
246  std::vector <std::pair<AliasRelationNode, AliasRelationNode> > getAliasRelations(SgGraphNode *node);
247 
249  void addNewAliasRelation(SgGraphNode *node, std::pair<AliasRelationNode, AliasRelationNode> a_relation) ;
250 
252  void addReturnStmt(AliasRelationNode node) { returnStmts.push_back(node); }
253 
255  std::vector<AliasRelationNode> getReturnStmts(){ return returnStmts; }
256 
257 };
258 
259 namespace ProcessExpression {
261  void processLHS(SgNode *node, struct AliasRelationNode &arNode) ;
262 
264  void processRHS(SgNode *node, struct AliasRelationNode &arNode) ;
265 }
266 
271 
274 
276  void processNode(SgGraphNode *);
277  public:
278  enum COLOR {WHITE=0, GREY, BLACK};
279  enum TRAVERSAL_TYPE {TOPOLOGICAL=0, NON_TOPOLOGICAL};
280 
282  void run();
283  private:
285  void recursiveCollect(SgGraphNode *, unordered_map<SgGraphNode*, CollectAliasRelations::COLOR> &);
286 };
287 
290  public IntraProcDataFlowAnalysis <SgGraphNode, CompReprPtr> {
291 
294 
297 
298 protected:
301 
304 
306  vector<SgGraphNode *> cfgNodes;
307 
308  //typedef FilteredCFGNode<AliasCfgFilter> FilteredCfgNode;
309  //typedef FilteredCFGEdge<AliasCfgFilter> FilteredCfgEdge;
310 
313 
316 
317  unsigned long checkPointHash;
318 
320  virtual void buildCFG() ;
321 
323  boost::unordered_map<SgFunctionDeclaration *, IntraProcAliasAnalysis *> &mapping;
324 
326  boost::unordered_map<SgExpression*, std::vector<SgFunctionDeclaration*> > &resolver;
327 
330  void getFunctionParametersAliasRelations(SgFunctionCallExp *f_exp, SgFunctionDeclaration *funcDecl,
331  std::vector <std::pair<AliasRelationNode, AliasRelationNode> > &arg_relations,
332  std::vector <std::pair<AliasRelationNode, AliasRelationNode> > &return_relations);
333 
335  void getConstructorParametersAliasRelations(SgConstructorInitializer *f_exp, SgFunctionDeclaration *funcDecl,
336  std::vector <std::pair<AliasRelationNode, AliasRelationNode> > &arg_relations );
337 
339  bool addVirtualFunction(SgType *type, SgFunctionCallExp *funcExp);
340 
342  bool updateVirtualFunctionInCallGraph (SgFunctionCallExp *funcCall, CompReprPtr &callSiteIN);
343 
344  // Get all the aliases
345  void getAliases(CompReprPtr &ptr, AliasRelationNode &node, std::vector<SgVariableSymbol*>& aliases);
346 
347 public:
348  //ReachingDefinitionAnalysis(SgNode *head) : DataFlowAnalysis<ReachingDefNode, ReachingDefinitions>(head), g(0) {
350  boost::unordered_map<SgFunctionDeclaration *, IntraProcAliasAnalysis *> &mapping,
351  boost::unordered_map<SgExpression*, std::vector<SgFunctionDeclaration*> > &resolver);
352 
354  CompReprPtr getFunctionEntry() {return entry;}
355 
357  CompReprPtr getFunctionExit() {return exit;}
358 
360  std::vector<AliasRelationNode> getReturnStmts () { return gen->getReturnStmts(); }
361 
363  void setFunctionEntry(CompReprPtr &n);
364 
366  void setFunctionExit(CompReprPtr &n);
367 
369  bool runCheck();
370 
372  virtual void run() ;
373 
375  void init() { }
376 
379 
382  CompReprPtr meet_data( const CompReprPtr& d1, const CompReprPtr& d2);
383 
385  virtual std::vector<SgGraphNode *> getAllNodes() { return cfgNodes; }
386 
388  virtual std::vector<SgGraphNode *> getPredecessors(SgGraphNode *n);
389 
391  CompReprPtr getCFGInData(SgGraphNode *a) { return gen->getEntryData(a); }
392 
394  CompReprPtr getCFGOutData(SgGraphNode *a) { return gen->getExitData(a); }
395 
397  void setCFGInData(SgGraphNode*a, CompReprPtr &b) { gen->setEntryData(a, b); }
398 
403 
404  void applyCFGTransferFunction(SgGraphNode* s);
405 
406 };
407 
408 #endif