ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
staticSingleAssignment.h
Go to the documentation of this file.
1 //Author: George Vulov <georgevulov@hotmail.com>
2 //Based on work by Justin Frye <jafrye@tamu.edu>
3 
4 #pragma once
5 
6 #include <rose.h>
7 #include <string>
8 #include <iostream>
9 #include <map>
10 #include <vector>
11 #include <algorithm>
12 #include <ostream>
13 #include <fstream>
14 #include <sstream>
15 #include <boost/foreach.hpp>
16 #include <filteredCFG.h>
17 #include <boost/unordered_map.hpp>
18 #include "reachingDef.h"
19 #include "dataflowCfgFilter.h"
20 #include "CallGraph.h"
21 #include "uniqueNameTraversal.h"
22 
23 namespace ssa_private
24 {
25 
28  {
29 
31  {
32  ROSE_ASSERT(funcDecl != NULL);
33 
34  //Don't process any built-in functions
35  std::string filename = funcDecl->get_file_info()->get_filename();
36  if (filename.find("include") != std::string::npos)
37  return false;
38 
39  //Exclude compiler generated functions, but keep template instantiations
42  return false;
43 
44  //We don't process functions that don't have definitions
45  if (funcDecl->get_definingDeclaration() == NULL)
46  return false;
47 
48  return true;
49  }
50  };
51 
52 } //namespace ssa_private
53 
63 {
64 private:
67 
68 public:
69 
71  typedef std::vector<SgInitializedName*> VarName;
72 
74  typedef boost::unordered_map<SgNode*, std::set<VarName> > LocalDefUseTable;
75 
77  typedef FilteredCFGNode<ssa_private::DataflowCfgFilter> FilteredCfgNode;
78 
80  typedef FilteredCFGEdge<ssa_private::DataflowCfgFilter> FilteredCfgEdge;
81 
82  typedef boost::shared_ptr<ReachingDef> ReachingDefPtr;
83 
85  typedef std::map<VarName, ReachingDefPtr> NodeReachingDefTable;
86 
88  typedef boost::unordered_map<SgNode*, std::pair<NodeReachingDefTable, NodeReachingDefTable> > GlobalReachingDefTable;
89 
91  typedef boost::unordered_map<SgNode*, NodeReachingDefTable> UseTable;
92 
93 private:
94  //Private member variables
95 
101 
107 
112 
120 
123 
127  boost::unordered_map<SgNode*, NodeReachingDefTable> ssaLocalDefTable;
128 
129 public:
130 
132  {
133  }
134 
136  {
137  }
138 
143  void run(bool interprocedural, bool treatPointersAsStructures);
144 
145  static bool getDebug()
146  {
147  return SgProject::get_verbose() > 0;
148  }
149 
150  static bool getDebugExtra()
151  {
152  return SgProject::get_verbose() > 1;
153  }
154 
155 private:
158  void runDefUseDataFlow(SgFunctionDefinition* func);
159 
161  static bool isBuiltinVar(const VarName& var);
162 
176  void expandParentMemberDefinitions(SgFunctionDeclaration* function);
177 
194  void expandParentMemberUses(SgFunctionDeclaration* function);
195 
200  void insertDefsForChildMemberUses(SgFunctionDeclaration* function);
201 
203  void insertDefsForExternalVariables(SgFunctionDeclaration* function);
204 
210  std::multimap< FilteredCfgNode, std::pair<FilteredCfgNode, FilteredCfgEdge> > insertPhiFunctions(SgFunctionDefinition* function,
211  const std::vector<FilteredCfgNode>& cfgNodesInPostOrder);
212 
214  void populateLocalDefsTable(SgFunctionDeclaration* function);
215 
220  void renumberAllDefinitions(SgFunctionDefinition* func, const std::vector<FilteredCfgNode>& cfgNodesInPostOrder);
221 
224  void updateIncomingPropagatedDefs(FilteredCfgNode cfgNode);
225 
228  bool propagateDefs(FilteredCfgNode cfgNode);
229 
233  void buildUseTable(const std::vector<FilteredCfgNode>& cfgNodes);
234 
237  static std::vector<FilteredCfgNode> getCfgNodesInPostorder(SgFunctionDefinition* func);
238 
239  //------------ INTERPROCEDURAL ANALYSIS FUNCTIONS ------------ //
240 
244  void interproceduralDefPropagation(const boost::unordered_set<SgFunctionDefinition*>& interestingFunctions);
245 
249  std::vector<SgFunctionDefinition*> calculateInterproceduralProcessingOrder(
250  const boost::unordered_set<SgFunctionDefinition*>& interestingFunctions);
251 
254  void processCalleesThenFunction(SgFunctionDefinition* targetFunction, SgIncidenceDirectedGraph* callGraph,
255  const boost::unordered_map<SgFunctionDefinition*, SgGraphNode*>& graphNodeToFunction,
256  std::vector<SgFunctionDefinition*> &processingOrder, std::set<SgFunctionDefinition*> visited);
257 
264  bool insertInterproceduralDefs(SgFunctionDefinition* funcDef, const boost::unordered_set<SgFunctionDefinition*>& processed,
265  ClassHierarchyWrapper* classHierarchy);
266 
271  void processOneCallSite(SgExpression* callSite, SgFunctionDeclaration* callee,
272  const boost::unordered_set<SgFunctionDefinition*>& processed, ClassHierarchyWrapper* classHierarchy);
273 
276  static bool isVarAccessibleFromCaller(const VarName& var, SgExpression* callSite, SgFunctionDeclaration* callee);
277 
280  static bool varRequiresThisPointer(const VarName& var);
281 
283  static bool isThisPointerSameInCallee(SgFunctionCallExp* callSite, SgMemberFunctionDeclaration* callee);
284 
288  static bool isThisPointer(SgExpression* expression);
289 
292  static bool isDeepConstPointer(SgType* type);
293 
296  static bool isPointerToDeepConst(SgType* type);
297 
300  static bool isArgumentNonConstReferenceOrPointer(SgInitializedName* formalArgument);
301 
302  //------------ GRAPH OUTPUT FUNCTIONS ------------ //
303 
304  void printToDOT(SgSourceFile* file, std::ofstream &outFile);
305  void printToFilteredDOT(SgSourceFile* file, std::ofstream &outFile);
306 
307 public:
308  //External static helper functions/variables
309 
311 
312  /*
313  * Printing functions.
314  */
315 
320  void toDOT(const std::string fileName);
321 
329  void toFilteredDOT(const std::string fileName);
330 
331  void printOriginalDefs(SgNode* node);
332  void printOriginalDefTable();
333 
334 
335  //------------ DEF/USE TABLE ACCESS FUNCTIONS ------------ //
336 
343  {
344  return originalDefTable;
345  }
346 
348  {
349  return localUsesTable;
350  }
351 
355  const NodeReachingDefTable& getOutgoingDefsAtNode(SgNode* node) const;
356 
359  const NodeReachingDefTable& getReachingDefsAtNode_(SgNode* node) const;
360 
363  const NodeReachingDefTable& getUsesAtNode(SgNode* node) const;
364 
367  const NodeReachingDefTable& getDefsAtNode(SgNode* node) const;
368 
370  std::set<VarName> getVarsUsedInSubtree(SgNode* root) const;
371 
374  std::set<VarName> getVarsDefinedInSubtree(SgNode* root) const;
375 
378  std::set<VarName> getOriginalVarsDefinedInSubtree(SgNode* root) const;
379 
382  NodeReachingDefTable getLastVersions(SgFunctionDeclaration* func) const;
383 
384  //------------ STATIC UTILITY FUNCTIONS FUNCTIONS ------------ //
385 
386 
397  static bool isPrefixOfName(VarName name, VarName prefix);
398 
404  static ssa_private::VarUniqueName* getUniqueName(SgNode* node);
405 
411  static const VarName& getVarName(SgNode* node);
412 
416  static const VarName& getVarForExpression(SgNode* node);
417 
424  static SgExpression* buildVariableReference(const VarName& var, SgScopeStatement* scope = NULL);
425 
428  static bool isVarInScope(const VarName& var, SgNode* scope);
429 
435  static std::string varnameToString(const VarName& vec);
436 
437  static void printLocalDefUseTable(const LocalDefUseTable& table);
438 };
439 
440