OpenASIP  2.0
ProgramDependenceGraph.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file ProgramDependenceGraph.hh
26  *
27  * Declaration of prototype of graph-based program representation:
28  * declaration of the program dependence graph.
29  *
30  * @author Vladimir Guzma 2006 (vladimir.guzma-no.spam-tut.fi)
31  * @note rating: red
32  */
33 
34 #ifndef TTA_PROGRAM_DEPENDENCE_GRAPH_HH
35 #define TTA_PROGRAM_DEPENDENCE_GRAPH_HH
36 
37 #include <boost/graph/filtered_graph.hpp>
38 
39 #include "BoostGraph.hh"
40 #include "ProgramDependenceEdge.hh"
41 #include "ProgramDependenceNode.hh"
43 #include "AssocTools.hh"
44 #include "MoveNode.hh"
45 
46 namespace TTAProgram {
47  class Terminal;
48 }
50 
52  public BoostGraph<ProgramDependenceNode, ProgramDependenceEdge> {
53 
54 public:
56  A_BEFORE_B, // Subgraph A must be scheduled before subgraph B
57  B_BEFORE_A, // vice verse
58  ANY_ORDER, // Any order is acceptable
59  UNORDERABLE, // Can not be ordered, needs additional predicate
60  ERROR // Relation can not be determined! This is error
61  };
62 
65  DataDependenceGraph& ddg);
66  virtual ~ProgramDependenceGraph();
68  bool serializePDG();
69  void disassemble() const;
70 
71 private:
72  typedef std::map<const ControlDependenceNode*, ProgramDependenceNode*,
75  typedef std::map<const BasicBlockNode*, ControlDependenceNode*,
77  typedef std::map<const MoveNode*, ProgramDependenceNode*,
79  typedef std::map<const ControlDependenceNode*, std::vector<Node*> >
81 
82  /// Filter control dependence edges only
83  template <typename GraphType>
84  struct CDGFilter {
85  CDGFilter() { }
86  CDGFilter(GraphType graph) : graph_(graph) { }
87  template <typename Edge>
88  bool operator()(const Edge& e) const {
89  return graph_[e]->isControlDependence()
90  || graph_[e]->isArtificialControlDependence();
91  }
92  GraphType graph_;
93  };
94 
95  /// Type of filtered graph with CD edges only
96  typedef boost::filtered_graph<Graph, CDGFilter<Graph> > FilteredCDG;
97  /// Few types to work with filtered graph
98  typedef boost::graph_traits<FilteredCDG>::in_edge_iterator
100  typedef boost::graph_traits<FilteredCDG>::out_edge_iterator
102  typedef boost::graph_traits<FilteredCDG>::vertex_descriptor
104  typedef std::pair<FilteredInEdgeIter, FilteredInEdgeIter>
106  typedef std::pair<FilteredOutEdgeIter, FilteredOutEdgeIter>
108  /// Stores data to compute post order relation on CDG and strong components
109  typedef std::map<NodeDescriptor, int> PDGOrderMap;
110  typedef boost::associative_property_map<PDGOrderMap> PDGOrder;
111  /// Storage for relations between nodes
112  typedef std::map<NodeDescriptor, NodeDescriptor> DescriptorMap;
113  typedef boost::associative_property_map<DescriptorMap> Descriptors;
114  /// Storage for color property used by dfs
115  typedef std::map <NodeDescriptor, boost::default_color_type > ColorMap;
116  typedef boost::associative_property_map<ColorMap> Color;
117 
118  /// Filter nodes of subgraph only
119  template <typename NodeSetType, typename GraphType>
122  SubgraphTypeTest(Node* root, NodeSetType nodes, GraphType graph) :
123  root_(root), subgraphNodes_(nodes), graph_(graph){ }
124  template <typename Node>
125  bool operator()(const Node& n) const {
126  return (graph_[n] != root_) &&
128  }
130  NodeSetType subgraphNodes_;
131  GraphType graph_;
132  };
133 
134  /// Filter away back edges
135  template <typename GraphType>
136  struct BackFilter {
138  BackFilter(GraphType graph) : graph_(graph) { }
139  template <typename Edge>
140  bool operator()(const Edge& e) const {
141  return !(graph_[e]->isLoopCloseEdge() ||
142  (graph_[e]->isDataDependence() &&
143  graph_[e]->dataDependenceEdge().isBackEdge()));
144  }
145  GraphType graph_;
146  };
147  typedef boost::filtered_graph<
150 
151  template <typename GraphType>
152  struct BackCFGFilter {
154  BackCFGFilter(GraphType graph) : graph_(graph) { }
155  template <typename Edge>
156  bool operator()(const Edge& e) const {
157  return !(graph_[e]->isBackEdge());
158  }
159  GraphType graph_;
160  };
161  typedef boost::filtered_graph<Graph,BackCFGFilter<Graph> >
163 
164 
165  void removeGuardedJump(
169 
172  BBToCD&,
174  MovesInCD&);
175 
177  PDGOrderMap& components,
178  DescriptorMap& roots,
179  FilteredCDG& filteredCDG);
180  void computeRegionInfo(
181  const PDGOrderMap& orderMap,
182  FilteredCDG& filteredCDG);
183  void computeEECInfo(
184  const PDGOrderMap& orderMap,
185  FilteredCDG& filteredCDG);
186 
187  void computeRelations(
188  const PDGOrderMap& orderMap,
189  FilteredCDG& filteredCDG);
191 
192  void regionHelper(
193  Node* node,
194  FilteredCDG& filteredCDG,
195  Node::NodesInfo& finalNodesInfo);
196  void processRegion(
197  Node* region,
198  FilteredCDG& filteredCDG);
199  void processPredicate(
200  Node* predicate,
201  FilteredCDG& filteredCDG);
202  void processEntry(BasicBlockNode* firstBB);
203  void processLoopClose(Node* node);
205 
206  void moveDDGedges(
207  Node& root,
208  NodeSet& subgraphNodes,
209  FilteredCDG& filteredCDG);
210 
211  void createJump(
212  BasicBlockNode* from,
213  BasicBlockNode* to,
214  TTAProgram::Terminal* guardReg = NULL,
217  void addLeafEdges(std::vector<BasicBlockNode*> leafs, BasicBlockNode* bb);
218 
219  /// Stores original control dependence graph
221  /// Stores original data dependence graph
223  /// Stores pointer to entry node of the PDG graph
225  /// Stored reference to PDG equivalent of DDG ENTRYNODE
227  /// stores nodes present in each of the found components
228  std::vector<std::set<Node*> > strongComponents_;
229  /// Newly created control flow graph
231  /// Counts new instructions when creating basic blocks
233  /// Original Program object, to get instruction reference manager
235 
236 
237  template <class Name>
238  class label_writer {
239  public:
240  label_writer(Name _name) : name(_name) {}
241  template <class VertexOrEdge>
242  void operator()(std::ostream& out, const
243  VertexOrEdge& v) const {
244  out << "[" << name[v]->dotString() << "]";
245  //"[label=\"" << name[v]->toString() << "\"]";
246  }
247  private:
248  Name name;
249  };
251 };
252 
253 #endif
ProgramDependenceGraph::ANY_ORDER
@ ANY_ORDER
Definition: ProgramDependenceGraph.hh:58
TTAProgram
Definition: Estimator.hh:65
ProgramDependenceGraph::compareSiblings
CompareResult compareSiblings(Node *a, Node *b)
Definition: ProgramDependenceGraph.cc:919
TTAProgram::Program
Definition: Program.hh:63
ProgramDependenceGraph::regionHelper
void regionHelper(Node *node, FilteredCDG &filteredCDG, Node::NodesInfo &finalNodesInfo)
Definition: ProgramDependenceGraph.cc:1110
ProgramDependenceGraph::computeEECInfo
void computeEECInfo(const PDGOrderMap &orderMap, FilteredCDG &filteredCDG)
Definition: ProgramDependenceGraph.cc:806
ProgramDependenceGraph::FilteredOutEdgePair
std::pair< FilteredOutEdgeIter, FilteredOutEdgeIter > FilteredOutEdgePair
Definition: ProgramDependenceGraph.hh:107
BoostGraph< ProgramDependenceNode, ProgramDependenceEdge >::node
Node & node(const int index) const
ProgramDependenceGraph::PDGOrder
boost::associative_property_map< PDGOrderMap > PDGOrder
Definition: ProgramDependenceGraph.hh:110
ProgramDependenceGraph::SubgraphTypeTest::graph_
GraphType graph_
Definition: ProgramDependenceGraph.hh:131
ProgramDependenceGraph::FilteredInEdgePair
std::pair< FilteredInEdgeIter, FilteredInEdgeIter > FilteredInEdgePair
Definition: ProgramDependenceGraph.hh:105
ControlDependenceGraph.hh
ProgramDependenceGraph::BBToCD
std::map< const BasicBlockNode *, ControlDependenceNode *, BasicBlockNode::Comparator > BBToCD
Definition: ProgramDependenceGraph.hh:76
ProgramDependenceGraph::detectStrongComponents
int detectStrongComponents(PDGOrderMap &components, DescriptorMap &roots, FilteredCDG &filteredCDG)
Definition: ProgramDependenceGraph.cc:524
AssocTools::containsKey
static bool containsKey(const ContainerType &aContainer, const KeyType &aKey)
BoostGraph< ProgramDependenceNode, ProgramDependenceEdge >::NodeSet
std::set< ProgramDependenceNode *, typename ProgramDependenceNode ::Comparator > NodeSet
Definition: BoostGraph.hh:86
BoostGraph.hh
ProgramDependenceGraph::BackCFGFilter::graph_
GraphType graph_
Definition: ProgramDependenceGraph.hh:159
ProgramDependenceGraph::BackFilter::BackFilter
BackFilter(GraphType graph)
Definition: ProgramDependenceGraph.hh:138
ProgramDependenceGraph::BackCFGFilter::operator()
bool operator()(const Edge &e) const
Definition: ProgramDependenceGraph.hh:156
ProgramDependenceGraph::insCount_
int insCount_
Counts new instructions when creating basic blocks.
Definition: ProgramDependenceGraph.hh:232
ProgramDependenceGraph::serializePDG
bool serializePDG()
Definition: ProgramDependenceGraph.cc:401
ProgramDependenceGraph::SubgraphTypeTest::root_
Node * root_
Definition: ProgramDependenceGraph.hh:129
ProgramDependenceGraph::processLoopEntry
void processLoopEntry(Node *node, BasicBlockNode *bb)
Definition: ProgramDependenceGraph.cc:1916
ControlFlowEdge::CFGEdgePredicate
CFGEdgePredicate
Definition: ControlFlowEdge.hh:52
MoveNode
Definition: MoveNode.hh:65
ProgramDependenceGraph::SubgraphTypeTest::operator()
bool operator()(const Node &n) const
Definition: ProgramDependenceGraph.hh:125
ProgramDependenceGraph::processRegion
void processRegion(Node *region, FilteredCDG &filteredCDG)
Definition: ProgramDependenceGraph.cc:1190
ProgramDependenceGraph::BackFilter::operator()
bool operator()(const Edge &e) const
Definition: ProgramDependenceGraph.hh:140
ProgramDependenceGraph::addLeafEdges
void addLeafEdges(std::vector< BasicBlockNode * > leafs, BasicBlockNode *bb)
Definition: ProgramDependenceGraph.cc:1858
ProgramDependenceGraph::BackFilter::graph_
GraphType graph_
Definition: ProgramDependenceGraph.hh:145
ProgramDependenceGraph::ddgEntryNode_
Node * ddgEntryNode_
Stored reference to PDG equivalent of DDG ENTRYNODE.
Definition: ProgramDependenceGraph.hh:226
ProgramDependenceGraph::CDGFilter::operator()
bool operator()(const Edge &e) const
Definition: ProgramDependenceGraph.hh:88
ProgramDependenceGraph::wrongCounter_
int wrongCounter_
Definition: ProgramDependenceGraph.hh:250
ProgramDependenceGraph::Subgraph
boost::filtered_graph< Graph, BackFilter< Graph >, SubgraphTypeTest< NodeSet, Graph > > Subgraph
Definition: ProgramDependenceGraph.hh:149
ProgramDependenceGraph::cdg_
const ControlDependenceGraph * cdg_
Stores original control dependence graph.
Definition: ProgramDependenceGraph.hh:220
ProgramDependenceEdge
Definition: ProgramDependenceEdge.hh:41
ProgramDependenceGraph::ColorMap
std::map< NodeDescriptor, boost::default_color_type > ColorMap
Storage for color property used by dfs.
Definition: ProgramDependenceGraph.hh:115
ProgramDependenceGraph::computeRegionInfo
void computeRegionInfo(const PDGOrderMap &orderMap, FilteredCDG &filteredCDG)
Definition: ProgramDependenceGraph.cc:734
ProgramDependenceGraph::processLoopClose
void processLoopClose(Node *node)
Definition: ProgramDependenceGraph.cc:1895
ProgramDependenceNode::NodesInfo
std::set< ProgramDependenceNode * > NodesInfo
Definition: ProgramDependenceNode.hh:64
ProgramDependenceGraph::disassemble
void disassemble() const
Definition: ProgramDependenceGraph.cc:1940
ControlFlowEdge::CFLOW_EDGE_NORMAL
@ CFLOW_EDGE_NORMAL
Definition: ControlFlowEdge.hh:53
ProgramDependenceGraph::FilteredOutEdgeIter
boost::graph_traits< FilteredCDG >::out_edge_iterator FilteredOutEdgeIter
Definition: ProgramDependenceGraph.hh:101
ProgramDependenceGraph::SubgraphTypeTest
Filter nodes of subgraph only.
Definition: ProgramDependenceGraph.hh:120
ProgramDependenceGraph::processEntry
void processEntry(BasicBlockNode *firstBB)
Definition: ProgramDependenceGraph.cc:1736
ProgramDependenceGraph::CDGFilter
Filter control dependence edges only.
Definition: ProgramDependenceGraph.hh:84
ProgramDependenceGraph::entryNode
ProgramDependenceNode & entryNode() const
Definition: ProgramDependenceGraph.cc:1091
ProgramDependenceGraph::label_writer
Definition: ProgramDependenceGraph.hh:238
ProgramDependenceGraph::BackFilter::BackFilter
BackFilter()
Definition: ProgramDependenceGraph.hh:137
ProgramDependenceGraph::~ProgramDependenceGraph
virtual ~ProgramDependenceGraph()
Definition: ProgramDependenceGraph.cc:75
ProgramDependenceGraph::BackCFGFilter::BackCFGFilter
BackCFGFilter()
Definition: ProgramDependenceGraph.hh:153
ProgramDependenceGraph::UNORDERABLE
@ UNORDERABLE
Definition: ProgramDependenceGraph.hh:59
ProgramDependenceGraph::CDGFilter::graph_
GraphType graph_
Definition: ProgramDependenceGraph.hh:92
ProgramDependenceGraph::ddg_
const DataDependenceGraph * ddg_
Stores original data dependence graph.
Definition: ProgramDependenceGraph.hh:222
ProgramDependenceGraph::MovesInCD
std::map< const ControlDependenceNode *, std::vector< Node * > > MovesInCD
Definition: ProgramDependenceGraph.hh:80
ProgramDependenceGraph::FilteredCDG
boost::filtered_graph< Graph, CDGFilter< Graph > > FilteredCDG
Type of filtered graph with CD edges only.
Definition: ProgramDependenceGraph.hh:96
ProgramDependenceNode.hh
ProgramDependenceGraph::Descriptors
boost::associative_property_map< DescriptorMap > Descriptors
Definition: ProgramDependenceGraph.hh:113
BasicBlockNode
Definition: BasicBlockNode.hh:64
GraphNode::Comparator
Definition: GraphNode.hh:56
ProgramDependenceGraph::ProgramDependenceGraph
ProgramDependenceGraph(ControlDependenceGraph &cdg, DataDependenceGraph &ddg)
Definition: ProgramDependenceGraph.cc:93
ProgramDependenceGraph::BackCFGFilter
Definition: ProgramDependenceGraph.hh:152
ProgramDependenceGraph
Definition: ProgramDependenceGraph.hh:51
ProgramDependenceGraph::strongComponents_
std::vector< std::set< Node * > > strongComponents_
stores nodes present in each of the found components
Definition: ProgramDependenceGraph.hh:228
ProgramDependenceGraph::computeRelations
void computeRelations(const PDGOrderMap &orderMap, FilteredCDG &filteredCDG)
Definition: ProgramDependenceGraph.cc:1444
ProgramDependenceGraph::BackFilter
Filter away back edges.
Definition: ProgramDependenceGraph.hh:136
ProgramDependenceGraph::label_writer::label_writer
label_writer(Name _name)
Definition: ProgramDependenceGraph.hh:240
BoostGraph< ProgramDependenceNode, ProgramDependenceEdge >::Graph
boost::adjacency_list< boost::listS, boost::vecS, boost::bidirectionalS, Node *, Edge * > Graph
Internal graph type, providing actual graph-like operations. This type definition relies on bundled p...
Definition: BoostGraph.hh:247
ProgramDependenceGraph::SubgraphTypeTest::SubgraphTypeTest
SubgraphTypeTest(Node *root, NodeSetType nodes, GraphType graph)
Definition: ProgramDependenceGraph.hh:122
ProgramDependenceGraph::newCFG_
ControlFlowGraph * newCFG_
Newly created control flow graph.
Definition: ProgramDependenceGraph.hh:230
ProgramDependenceGraph::label_writer::name
Name name
Definition: ProgramDependenceGraph.hh:248
ProgramDependenceGraph::CompareResult
CompareResult
Definition: ProgramDependenceGraph.hh:55
ProgramDependenceGraph::MoveNodeToPDGNode
std::map< const MoveNode *, ProgramDependenceNode *, MoveNode::Comparator > MoveNodeToPDGNode
Definition: ProgramDependenceGraph.hh:78
ProgramDependenceGraph::FilteredVertexDescriptor
boost::graph_traits< FilteredCDG >::vertex_descriptor FilteredVertexDescriptor
Definition: ProgramDependenceGraph.hh:103
ProgramDependenceGraph::ERROR
@ ERROR
Definition: ProgramDependenceGraph.hh:60
ProgramDependenceGraph::SubgraphTypeTest::subgraphNodes_
NodeSetType subgraphNodes_
Definition: ProgramDependenceGraph.hh:130
ProgramDependenceGraph::A_BEFORE_B
@ A_BEFORE_B
Definition: ProgramDependenceGraph.hh:56
ProgramDependenceGraph::entryNode_
Node * entryNode_
Stores pointer to entry node of the PDG graph.
Definition: ProgramDependenceGraph.hh:224
ProgramDependenceGraph::CFGSubgraph
boost::filtered_graph< Graph, BackCFGFilter< Graph > > CFGSubgraph
Definition: ProgramDependenceGraph.hh:162
ProgramDependenceGraph::PDGOrderMap
std::map< NodeDescriptor, int > PDGOrderMap
Stores data to compute post order relation on CDG and strong components.
Definition: ProgramDependenceGraph.hh:109
ProgramDependenceEdge.hh
ProgramDependenceGraph::CDGFilter::CDGFilter
CDGFilter(GraphType graph)
Definition: ProgramDependenceGraph.hh:86
ProgramDependenceGraph::createJump
void createJump(BasicBlockNode *from, BasicBlockNode *to, TTAProgram::Terminal *guardReg=NULL, ControlFlowEdge::CFGEdgePredicate predicate=ControlFlowEdge::CFLOW_EDGE_NORMAL)
Definition: ProgramDependenceGraph.cc:1995
AssocTools.hh
ProgramDependenceGraph::B_BEFORE_A
@ B_BEFORE_A
Definition: ProgramDependenceGraph.hh:57
ProgramDependenceGraph::removeGuardedJump
void removeGuardedJump(ControlToProgram &, ProgramDependenceNode &, ControlDependenceNode &)
Definition: ProgramDependenceGraph.cc:327
DataDependenceGraph
Definition: DataDependenceGraph.hh:67
ProgramDependenceGraph::ControlToProgram
std::map< const ControlDependenceNode *, ProgramDependenceNode *, ControlDependenceNode::Comparator > ControlToProgram
Definition: ProgramDependenceGraph.hh:74
ProgramDependenceNode
Definition: ProgramDependenceNode.hh:43
ControlDependenceGraph
Definition: ControlDependenceGraph.hh:65
TTAProgram::Terminal
Definition: Terminal.hh:60
BoostGraph
Definition: BoostGraph.hh:83
ProgramDependenceGraph::label_writer::operator()
void operator()(std::ostream &out, const VertexOrEdge &v) const
Definition: ProgramDependenceGraph.hh:242
ProgramDependenceGraph::Color
boost::associative_property_map< ColorMap > Color
Definition: ProgramDependenceGraph.hh:116
ProgramDependenceGraph::SubgraphTypeTest::SubgraphTypeTest
SubgraphTypeTest()
Definition: ProgramDependenceGraph.hh:121
ProgramDependenceGraph::CDGFilter::CDGFilter
CDGFilter()
Definition: ProgramDependenceGraph.hh:85
ProgramDependenceGraph::copyRegionEECComponent
void copyRegionEECComponent(ControlToProgram &, BBToCD &, MoveNodeToPDGNode &, MovesInCD &)
Definition: ProgramDependenceGraph.cc:1623
ProgramDependenceGraph::FilteredInEdgeIter
boost::graph_traits< FilteredCDG >::in_edge_iterator FilteredInEdgeIter
Few types to work with filtered graph.
Definition: ProgramDependenceGraph.hh:99
ControlDependenceNode
Definition: ControlDependenceNode.hh:61
MoveNode.hh
ProgramDependenceGraph::processPredicate
void processPredicate(Node *predicate, FilteredCDG &filteredCDG)
Definition: ProgramDependenceGraph.cc:1760
ProgramDependenceGraph::program_
TTAProgram::Program * program_
Original Program object, to get instruction reference manager.
Definition: ProgramDependenceGraph.hh:234
ProgramDependenceGraph::DescriptorMap
std::map< NodeDescriptor, NodeDescriptor > DescriptorMap
Storage for relations between nodes.
Definition: ProgramDependenceGraph.hh:112
ProgramDependenceGraph::BackCFGFilter::BackCFGFilter
BackCFGFilter(GraphType graph)
Definition: ProgramDependenceGraph.hh:154
ProgramDependenceGraph::moveDDGedges
void moveDDGedges(Node &root, NodeSet &subgraphNodes, FilteredCDG &filteredCDG)
Definition: ProgramDependenceGraph.cc:1490
ControlFlowGraph
Definition: ControlFlowGraph.hh:100