ROSE  0.9.6a
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
controlDependence.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <utility>
5 #include "rose.h"
7 #include <boost/foreach.hpp>
8 
9 namespace ssa_private
10 {
11  using namespace boost;
12  using namespace std;
13 
17  template<class CfgNodeT, class CfgEdgeT>
18  multimap< CfgNodeT, pair<CfgNodeT, CfgEdgeT> >
19  calculateControlDependence(SgFunctionDefinition* function, const map<CfgNodeT, CfgNodeT>& iPostDominatorMap)
20  {
21  //Map from each node to the nodes it's control dependent on (and corresponding edges)
22  multimap< CfgNodeT, pair<CfgNodeT, CfgEdgeT> > controlDepdendences;
23 
24  //Let's iterate the control flow graph and stop every time we hit an edge with a condition
25  set<CfgNodeT> visited;
26  set<CfgNodeT> worklist;
27 
28  CfgNodeT sourceNode = function->cfgForBeginning();
29  worklist.insert(sourceNode);
30 
31  while (!worklist.empty())
32  {
33  //Get the node to work on
34  sourceNode = *worklist.begin();
35  worklist.erase(worklist.begin());
36  visited.insert(sourceNode);
37 
38  //For every edge, add it to the worklist
39 
40  BOOST_FOREACH(const CfgEdgeT& edge, sourceNode.outEdges())
41  {
42  CfgNodeT targetNode = edge.target();
43 
44  //Insert the child in the worklist if the it hasn't been visited yet
45  if (visited.count(targetNode) == 0)
46  {
47  worklist.insert(targetNode);
48  }
49 
50  //Check if we need to process this edge in control dependence calculation
51  if (edge.condition() == VirtualCFG::eckUnconditional)
52  continue;
53 
54  //We traverse from nextNode up in the postdominator tree until we reach the parent of currNode.
55  CfgNodeT parent;
56  typename map<CfgNodeT, CfgNodeT>::const_iterator parentIter = iPostDominatorMap.find(sourceNode);
57  ROSE_ASSERT(parentIter != iPostDominatorMap.end());
58  parent = parentIter->second;
59 
60  //This is the node that we'll be marking as control dependent
61  CfgNodeT currNode = targetNode;
62 
63  while (true)
64  {
65  //If we reach the parent of the source, stop
66  if (currNode == parent)
67  {
68  break;
69  }
70 
71  //Add a control dependence from the source to the new node
72  controlDepdendences.insert(make_pair(currNode, make_pair(sourceNode, edge)));
73 
75  {
76  printf("%s is control-dependent on %s - %s \n", currNode.toStringForDebugging().c_str(),
77  sourceNode.toStringForDebugging().c_str(), edge.condition() == VirtualCFG::eckTrue ? "true" : "false");
78  }
79 
80  //Move to the parent of the current node
81  parentIter = iPostDominatorMap.find(currNode);
82  ROSE_ASSERT(parentIter != iPostDominatorMap.end());
83  currNode = parentIter->second;
84  }
85  }
86  }
87 
88  return controlDepdendences;
89  }
90 }
91