00001
00002 #pragma once
00003
00004 #include <rose.h>
00005 #include <string>
00006 #include <iostream>
00007 #include <map>
00008 #include <set>
00009 #include <vector>
00010 #include <algorithm>
00011 #include <ostream>
00012 #include <fstream>
00013 #include <sstream>
00014 #include <boost/foreach.hpp>
00015 #include "filteredCFG.h"
00016 #include <boost/unordered_map.hpp>
00017 #include "reachingDefUnfilteredCfg.h"
00018 #include "CallGraph.h"
00019 #include <uniqueNameTraversal.h>
00020
00021 namespace ssa_unfiltered_cfg
00022 {
00023
00025 struct FunctionFilter
00026 {
00027
00028 bool operator()(SgFunctionDeclaration * funcDecl)
00029 {
00030 ROSE_ASSERT(funcDecl != NULL);
00031
00032
00033 if (funcDecl->get_file_info()->isCompilerGenerated() && !isSgTemplateInstantiationFunctionDecl(funcDecl)
00034 && !isSgTemplateInstantiationMemberFunctionDecl(funcDecl))
00035 return false;
00036
00037
00038 if (funcDecl->get_definingDeclaration() == NULL)
00039 return false;
00040
00041 return true;
00042 }
00043 };
00044
00059 class SSA_UnfilteredCfg
00060 {
00061 private:
00063 SgProject* project;
00064
00065 public:
00066
00068 typedef std::vector<SgInitializedName*> VarName;
00069
00070 typedef boost::shared_ptr<ReachingDef> ReachingDefPtr;
00071
00073 typedef std::map<VarName, ReachingDefPtr> NodeReachingDefTable;
00074
00075 typedef std::map<CFGNode, std::set<VarName> > CFGNodeToVarNamesMap;
00076
00077 typedef std::map<SgNode*, std::set<SgVarRefExp*> > ASTNodeToVarRefsMap;
00078
00079 typedef std::map<CFGNode, NodeReachingDefTable> CFGNodeToDefTableMap;
00080
00081 private:
00082
00083 ASTNodeToVarRefsMap astNodeToUses;
00084
00087 CFGNodeToDefTableMap localDefTable;
00088
00091 CFGNodeToDefTableMap reachingDefTable;
00092
00095 CFGNodeToDefTableMap outgoingDefTable;
00096
00097 public:
00098
00099 SSA_UnfilteredCfg(SgProject* proj) : project(proj) { }
00100
00101 ~SSA_UnfilteredCfg() { }
00102
00104 void run();
00105
00106 static bool getDebug()
00107 {
00108 return SgProject::get_verbose() > 0;
00109 }
00110
00111 static bool getDebugExtra()
00112 {
00113 return SgProject::get_verbose() > 1;
00114 }
00115
00116 private:
00119 void runDefUseDataFlow(SgFunctionDefinition* func);
00120
00122 static bool isBuiltinVar(const VarName& var);
00123
00137 void expandParentMemberDefinitions(const CFGNodeToVarNamesMap& defs);
00138
00143 void insertDefsForChildMemberUses(const CFGNodeToVarNamesMap& defs, const std::set<VarName>& usedNames);
00144
00146 void insertDefsForExternalVariables(SgFunctionDefinition* function, const std::set<VarName>& usedNames);
00147
00152 void insertPhiFunctions(SgFunctionDefinition* function, const std::vector<CFGNode>& cfgNodesInPostOrder);
00153
00158 void renumberAllDefinitions(SgFunctionDefinition* func, const std::vector<CFGNode>& cfgNodesInPostOrder);
00159
00162 void updateIncomingPropagatedDefs(const CFGNode& cfgNode);
00163
00166 bool propagateDefs(const CFGNode& cfgNode);
00167
00170 static std::vector<CFGNode> getCfgNodesInPostorder(SgFunctionDefinition* func);
00171
00172
00173
00174
00175 void printToDOT(SgNode* root, std::ostream &outFile);
00176 void printToFilteredDOT(SgSourceFile* file, std::ofstream &outFile);
00177
00178 public:
00179
00180
00181 static VarName emptyName;
00182
00183
00184
00185
00186
00191 void toDOT(const std::string fileName);
00192
00200 void toFilteredDOT(const std::string fileName);
00201
00202
00203
00204
00206 const NodeReachingDefTable& getReachingDefsBefore(const CFGNode& node) const;
00207
00211 const NodeReachingDefTable& getReachingDefsAfter(const CFGNode& node) const;
00212
00215 const NodeReachingDefTable& getDefsAtNode(const CFGNode& node) const;
00216
00219 const NodeReachingDefTable& getReachingDefsBefore(SgNode* astNode) const;
00220
00223 const NodeReachingDefTable& getReachingDefsAfter(SgNode* astNode) const;
00224
00226 const NodeReachingDefTable& getLastVersions(SgFunctionDefinition* astNode) const;
00227
00230 const std::set<SgVarRefExp*>& getUsesAtNode(SgNode* astNode) const;
00231
00234 const ReachingDefPtr getDefinitionForUse(SgVarRefExp* astNode) const;
00235
00238 const ASTNodeToVarRefsMap& getUseTable() const;
00239
00240
00241
00252 static bool isPrefixOfName(VarName name, VarName prefix);
00253
00259 static ssa_private::VarUniqueName* getUniqueName(SgNode* node);
00260
00266 static const VarName& getVarName(SgNode* node);
00267
00271 static const VarName& getVarForExpression(SgNode* node);
00272
00279 static SgExpression* buildVariableReference(const VarName& var, SgScopeStatement* scope = NULL);
00280
00286 static std::string varnameToString(const VarName& vec);
00287
00288 static void printNodeDefTable(const NodeReachingDefTable& table);
00289 static void printFullDefTable(const CFGNodeToDefTableMap& defTable);
00290 };
00291
00292 }