PandA-2024.02
bb_cdg_computation.cpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
40 #include "bb_cdg_computation.hpp"
42 
44 #include "Parameter.hpp"
45 
47 #include "Dominance.hpp"
48 
50 #include "basic_block.hpp"
52 #include "function_behavior.hpp"
53 #include "hash_helper.hpp"
54 #include "op_graph.hpp"
55 
57  unsigned int _function_id, const DesignFlowManagerConstRef _design_flow_manager)
58  : FunctionFrontendFlowStep(_AppM, _function_id, BB_CONTROL_DEPENDENCE_COMPUTATION, _design_flow_manager, _Param)
59 {
60 }
61 
63 
66 {
68  switch(relationship_type)
69  {
71  {
72  relationships.insert(std::make_pair(BB_FEEDBACK_EDGES_IDENTIFICATION, SAME_FUNCTION));
73  relationships.insert(std::make_pair(DOM_POST_DOM_COMPUTATION, SAME_FUNCTION));
74  relationships.insert(std::make_pair(BB_ORDER_COMPUTATION, SAME_FUNCTION));
75  break;
76  }
79  {
80  break;
81  }
82  default:
83  {
85  }
86  }
87  return relationships;
88 }
89 
91 {
92  if(bb_version != 0 and bb_version != function_behavior->GetBBVersion())
93  {
94  const BBGraphConstRef bb_cdg = function_behavior->CGetBBGraph(FunctionBehavior::CDG_BB);
95  if(boost::num_vertices(*bb_cdg) != 0)
96  {
97  EdgeIterator edge, edge_end;
98  for(boost::tie(edge, edge_end) = boost::edges(*bb_cdg); edge != edge_end; edge++)
99  {
100  function_behavior->bbgc->RemoveEdge(*edge, CDG_SELECTOR);
101  }
102  }
103  }
104 }
105 
107 {
108  const BBGraphRef bb = function_behavior->GetBBGraph(FunctionBehavior::BB);
109  const dominance<BBGraph>* post_dominators = function_behavior->post_dominators;
110  const BehavioralHelperConstRef helper = function_behavior->CGetBehavioralHelper();
111 
112  EdgeIterator ei, ei_end;
113  std::list<vertex> bb_levels;
114  boost::topological_sort(*bb, std::front_inserter(bb_levels));
115  std::map<vertex, unsigned int> bb_sorted;
116  unsigned int counter = 0;
117  for(auto& bb_level : bb_levels)
118  {
119  bb_sorted[bb_level] = ++counter;
120  }
121  // iterate over outgoing edges of the basic block CFG.
122  for(boost::tie(ei, ei_end) = boost::edges(*bb); ei != ei_end; ++ei)
123  {
124  vertex A = boost::source(*ei, *bb);
125  vertex B = boost::target(*ei, *bb);
126  InEdgeIterator pd_ei, pd_ei_end;
127  vertex current_node = B;
128  while(current_node && current_node != A && current_node != post_dominators->get_immediate_dominator(A))
129  {
130  if(bb_sorted[current_node] > bb_sorted[A])
131  {
132  function_behavior->bbgc->AddEdge(A, current_node, CDG_SELECTOR);
133  const auto& labels = bb->CGetBBEdgeInfo(*ei)->get_labels(CFG_SELECTOR);
134  if(labels.size())
135  {
136  auto it_end = labels.end();
137  for(auto it = labels.begin(); it != it_end; ++it)
138  {
139  function_behavior->bbgc->add_bb_edge_info(A, current_node, CDG_SELECTOR, *it);
140  }
141  }
142  }
143  else
144  {
145  break;
146  }
147  current_node = post_dominators->get_immediate_dominator(current_node);
148  }
149  }
150 
151  BBGraphRef cdg_bb = function_behavior->cdg_bb;
152 
153  // Counter used to enumerate different
154  unsigned int cer_counter = 0;
155  // Map control equivalent region codification to control equivalent index;
156  // The codification is the set of pair predecessor-edge label in the cdg_computation
157  std::map<CustomOrderedSet<std::pair<vertex, CustomOrderedSet<unsigned int>>>, unsigned int> cdg_to_index;
158 
159  const std::deque<vertex>& topological_sorted_nodes = function_behavior->get_bb_levels();
160  std::deque<vertex>::const_iterator it, it_end;
161  it_end = topological_sorted_nodes.end();
162  for(it = topological_sorted_nodes.begin(); it != it_end; ++it)
163  {
164  unsigned int cer_index = cer_counter;
165  const BBNodeInfoRef bb_node_info = cdg_bb->GetBBNodeInfo(*it);
166  if(boost::in_degree(*it, *cdg_bb) > 0)
167  {
168  // codification of this basic block
170  InEdgeIterator eii, eii_end;
171  for(boost::tie(eii, eii_end) = boost::in_edges(*it, *cdg_bb); eii != eii_end; eii++)
172  {
173  this_cod.emplace(std::pair<vertex, CustomOrderedSet<unsigned int>>(
174  boost::source(*eii, *cdg_bb), cdg_bb->CGetBBEdgeInfo(*eii)->get_labels(CDG_SELECTOR)));
175  }
176  if(cdg_to_index.find(this_cod) == cdg_to_index.end())
177  {
178  cdg_to_index[this_cod] = cer_counter;
179  cer_counter++;
180  }
181  else
182  {
183  cer_index = cdg_to_index[this_cod];
184  }
185  bb_node_info->cer = cer_index;
186  }
187  }
188 
189  if(parameters->getOption<bool>(OPT_print_dot))
190  {
191  function_behavior->CGetBBGraph(FunctionBehavior::CDG_BB)->WriteDot("BB_CDG.dot");
192  }
194 }
~BBCdgComputation() override
Destructor.
Vertex get_immediate_dominator(Vertex v) const
Return the immediate dominator of a Vertex.
Definition: Dominance.hpp:757
Basic block control flow graph.
BBCdgComputation(const ParameterConstRef Param, const application_managerRef AppM, unsigned int function_id, const DesignFlowManagerConstRef design_flow_manager)
Constructor.
string target
Definition: lenet_tvm.py:16
RelationshipType
The relationship type.
Source must be executed to satisfy target.
boost::graph_traits< graph >::in_edge_iterator InEdgeIterator
in_edge_iterator definition.
Definition: graph.hpp:1310
This class provides methods to build a basic blocks graph.
unsigned int bb_version
The version of the basic block intermediate representation on which this step has been applied...
#define A
Definition: generate.c:13
void Initialize() override
Initialize the step (i.e., like a constructor, but executed just before exec.
DesignFlowStep_Status InternalExec() override
Performs the computation of the CDG representation.
boost::graph_traits< graph >::edge_iterator EdgeIterator
edge_iterator definition.
Definition: graph.hpp:1314
#define THROW_UNREACHABLE(str_expr)
helper function used to specify that some points should never be reached
Definition: exceptions.hpp:292
const BBEdgeInfoConstRef CGetBBEdgeInfo(const EdgeDescriptor e) const
Returns the info associated with an edge.
unsigned edges[4545]
Definition: graph.h:25
Analysis step computing basic block control dependencies.
#define CDG_SELECTOR
Control dependence edge selector.
boost::graph_traits< graph >::vertex_descriptor vertex
vertex definition.
Definition: graph.hpp:1303
const ParameterConstRef parameters
Set of input parameters.
DesignFlowStep_Status
The status of a step.
Basic block control dependence graph.
BBNodeInfoRef GetBBNodeInfo(const vertex node)
Return the info associated with a basic block.
Class specification of the basic_block structure.
This file collects some hash functors.
Data structures used in operations graph.
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
this class is used to manage the command-line or XML options.
unsigned counter[N_THREADS]
Definition: data.c:3
#define CFG_SELECTOR
Control flow graph edge selector.
const CustomUnorderedSet< std::pair< FrontendFlowStepType, FunctionRelationship > > ComputeFrontendRelationships(const DesignFlowStep::RelationshipType relationship_type) const override
Return the set of analyses in relationship with this design step.
#define B
Definition: generate.c:14
A brief description of the C++ Header File.
const FunctionBehaviorRef function_behavior
The function behavior of the function to be analyzed.

Generated on Mon Feb 12 2024 13:02:51 for PandA-2024.02 by doxygen 1.8.13