PandA-2024.02
block_fix.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  */
45 #include "block_fix.hpp"
47 
49 #include "Parameter.hpp"
50 
52 #include "application_manager.hpp"
53 #include "function_behavior.hpp"
54 
56 #include <fstream>
57 
59 #include "tree_basic_block.hpp"
60 #include "tree_helper.hpp"
61 #include "tree_manager.hpp"
62 #include "tree_node.hpp"
63 #include "tree_reindex.hpp"
64 
66 #include "dbgPrintHelper.hpp"
67 #include "string_manipulation.hpp" // for GET_CLASS
68 
69 BlockFix::BlockFix(const application_managerRef _AppM, unsigned int _function_id,
70  const DesignFlowManagerConstRef _design_flow_manager, const ParameterConstRef _parameters)
71  : FunctionFrontendFlowStep(_AppM, _function_id, BLOCK_FIX, _design_flow_manager, _parameters)
72 {
73  debug_level = _parameters->get_class_debug_level(GET_CLASS(*this), DEBUG_LEVEL_NONE);
74 }
75 
76 BlockFix::~BlockFix() = default;
77 
80 {
82  switch(relationship_type)
83  {
85  {
86  relationships.insert(std::make_pair(CALL_EXPR_FIX, SAME_FUNCTION));
87  break;
88  }
90  {
91  break;
92  }
94  {
95  break;
96  }
97  default:
98  {
100  }
101  }
102  return relationships;
103 }
104 
106 {
107  const tree_managerRef TM = AppM->get_tree_manager();
109  auto* fd = GetPointer<function_decl>(temp);
110  auto* sl = GetPointer<statement_list>(GET_NODE(fd->body));
111 
112  std::map<unsigned int, blocRef>& list_of_bloc = sl->list_of_bloc;
113  std::map<unsigned int, blocRef>::iterator it3, it3_end = list_of_bloc.end();
114 
115  // Adding entry block
116  blocRef entry_bloc = blocRef(new bloc(BB_ENTRY));
117  // Set of successor of entry
118  std::vector<unsigned int>& succ_entry = entry_bloc->list_of_succ;
119  for(it3 = list_of_bloc.begin(); it3 != it3_end; ++it3)
120  {
121  std::vector<unsigned int>::iterator it2, it2_end;
122  if(it3->second)
123  {
124  it2_end = it3->second->list_of_pred.end();
125  for(it2 = it3->second->list_of_pred.begin(); it2 != it2_end; ++it2)
126  {
127  if(*it2 == BB_ENTRY)
128  {
129  succ_entry.push_back(it3->second->number);
130  }
131  }
132  }
133  }
135  // Adding exit
136  blocRef exit_bloc = blocRef(new bloc(BB_EXIT));
137  sl->list_of_bloc[BB_ENTRY] = entry_bloc;
138  sl->list_of_bloc[BB_EXIT] = exit_bloc;
139 
142  CustomSet<unsigned int> reachable_labels;
143  for(const auto& block : sl->list_of_bloc)
144  {
145  for(const auto& statement : block.second->CGetStmtList())
146  {
147  const auto* gg = GetPointer<const gimple_goto>(GET_NODE(statement));
148  if(gg)
149  {
150  THROW_ASSERT(gg->op and GetPointer<const label_decl>(GET_NODE(gg->op)),
151  "Unexpexted condition :" + gg->ToString());
152  INDENT_DBG_MEX(DEBUG_LEVEL_VERY_PEDANTIC, debug_level, "---Found a reachable label " + gg->op->ToString());
153  reachable_labels.insert(GET_INDEX_NODE(gg->op));
154  }
155  const auto gs = GetPointer<const gimple_switch>(GET_NODE(statement));
156  if(gs)
157  {
158  for(const auto& vec_op : GetPointer<const tree_vec>(GET_NODE(gs->op1))->list_of_op)
159  {
160  const auto cle = GetPointer<const case_label_expr>(GET_NODE(vec_op));
161  if(cle->got and GetPointer<const label_decl>(GET_NODE(cle->got)))
162  {
163  reachable_labels.insert(cle->got->index);
165  "---Found a reachable label " + cle->got->ToString());
166  }
167  }
168  }
169  }
170  }
171  std::list<std::pair<tree_nodeRef, unsigned int>> to_be_removed;
172  for(const auto& block : sl->list_of_bloc)
173  {
174  for(const auto& statement : block.second->CGetStmtList())
175  {
176  const auto* gl = GetPointer<const gimple_label>(GET_NODE(statement));
177  if(gl)
178  {
179  const auto* ld = GetPointer<const label_decl>(GET_NODE(gl->op));
180  if(ld and reachable_labels.find(ld->index) == reachable_labels.end())
181  {
183  "---Found a removable label " + statement->ToString());
184  to_be_removed.push_back(std::pair<tree_nodeRef, unsigned int>(statement, block.first));
185  }
186  }
187  }
188  }
189 
190  for(const auto& removing : to_be_removed)
191  {
192  sl->list_of_bloc[removing.second]->RemoveStmt(removing.first, AppM);
193  }
194 
195  if(to_be_removed.empty())
196  {
198  }
199  else
200  {
201  function_behavior->UpdateBBVersion();
203  }
204 }
#define GET_NODE(t)
Macro used to hide implementation details when accessing a tree_node from another tree_node...
Definition: tree_node.hpp:343
#define DEBUG_LEVEL_VERY_PEDANTIC
extremely verbose debugging print is performed.
This struct specifies the field bloc (basic block).
#define INDENT_DBG_MEX(dbgLevel, curDbgLevel, mex)
We are producing a debug version of the program, so the message is printed;.
File containing functions and utilities to support the printing of debug messagges.
Step successfully executed.
#define GET_CLASS(obj)
Macro returning the actual type of an object.
#define BB_EXIT
constant identifying the basic block node of type exit
Definition of the class representing a generic C application.
RelationshipType
The relationship type.
Source must be executed to satisfy target.
BlockFix(const application_managerRef AppM, unsigned int function_id, const DesignFlowManagerConstRef design_flow_manager, const ParameterConstRef parameters)
Constructor.
Definition: block_fix.cpp:69
#define GET_INDEX_NODE(t)
Macro used to hide implementation details when accessing a tree_node from another tree_node...
Definition: tree_node.hpp:361
Data structure describing a basic block at tree level.
Auxiliary methods for manipulating string.
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.
Definition: block_fix.cpp:79
const tree_nodeRef get_tree_node_const(unsigned int i) const
Return the reference to the i-th tree_node Constant version of get_tree_node.
#define THROW_UNREACHABLE(str_expr)
helper function used to specify that some points should never be reached
Definition: exceptions.hpp:292
~BlockFix() override
Destructor.
#define BB_ENTRY
constant identifying the basic block node of type entry
Classes specification of the tree_node data structures.
DesignFlowStep_Status
The status of a step.
#define DEBUG_LEVEL_NONE
no debugging print is performed.
This struct specifies the block node.
Definition: tree_node.hpp:1820
This file collects some utility functions.
const unsigned int function_id
The index of the function to be analyzed.
const application_managerRef AppM
The application manager.
Class specification of the tree_reindex support class.
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.
DesignFlowStep_Status InternalExec() override
Updates the tree to have a more compliant CFG.
Definition: block_fix.cpp:105
int debug_level
The debug level.
Analysis step that modifies the control flow graph of the tree to make it more compliant.
Class specification of the manager of the tree structures extracted from the raw file.
A brief description of the C++ Header File.
const FunctionBehaviorRef function_behavior
The function behavior of the function to be analyzed.
int sl
Definition: adpcm.c:105
#define THROW_ASSERT(cond, str_expr)
helper function used to check an assert and if needed to throw an error in a standard way ...
Definition: exceptions.hpp:289

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