PIPS
declarations.c
Go to the documentation of this file.
1 /*
2 
3  $Id: declarations.c 23495 2018-10-24 09:19:47Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 /*
28  * clean the declarations of a module.
29  * to be called from pipsmake.
30  */
31 
32 #include <stdio.h>
33 
34 #include "genC.h"
35 #include "linear.h"
36 
37 #include "pipsdbm.h"
38 #include "misc.h"
39 #include "properties.h"
40 
41 #include "ri.h"
42 #include "effects.h"
43 #include "ri-util.h"
44 #include "effects-util.h"
45 
46 #include "effects-generic.h" // used
47 #include "control.h" // for clean_up_sequences, module_reorder
48 #include "transformations.h" // clean_declarations
49 
51 {
53  {
55  bool entity_written = false;
58  { entity_written = true; break; }
59  if(entity_written) /* we only manage written entity in the form of e binary_op something else */
60  {
61  call c = statement_call(s);
62  expression lhs = binary_call_lhs(c);
64  {
65  list next = CDR(call_arguments(c));
66  if(ENDP(next))
68  else if( ENDP(CDR(next)) ) {
69  expression rhs = binary_call_rhs(c);
70  if(expression_call_p(rhs))
71  {
72  call new_c = expression_call(rhs);
74  free_call(c);
76  }
77  else {
81  }
82  }
83  else {
84  pips_internal_error("case unhandled yet\nfell free to contribute :-)");
85  }
86  }
87  }
88 
89  }
90 }
92 {
94  if(formal_parameter_p(d) && entities_may_conflict_p(e,d)) return true;
95  return false;
96 }
97 
98 typedef struct {
100  bool result;
102 
104 {
107  { p->result = true; gen_recurse_stop(0); }
108 }
109 
111 {
112  entity_used_somewhere_param p = { e, false };
114  return p.result;
115 }
117 {
118  if(statement_block_p(s))
119  {
121  {
122  if(entity_variable_p(e)) {
123  if(!entity_may_conflict_with_a_formal_parameter_p(e,get_current_module_entity()) && /* it is useless to try to remove formal parameters */
124  entity_scalar_p(e) ) /* and we cannot afford removing something that may implies aliasing */
125  {
126  bool effects_read_variable = entity_read_somewhere_p(e,s);
127  if(!effects_read_variable) {
128  /* the entity is never read, it is disposable */
129  pips_debug(4,"Entity %s is never read, we can remove "
130  "statements that only produce it.\n",
131  entity_name(e));
133  }
134  }
135  }
136  }
137  }
138 }
139 
142 
144 }
145 
146 /* A phase to remove the declaration of useless variables
147 
148  It recursively calls statement_remove_unused_declarations on all module
149  statement
150 
151  @param[in] module_name is the name of the module to process
152 
153  @return true because always successful
154 */
155 bool clean_declarations(const string module_name)
156 {
157  /* prelude */
161 
162  debug_on("CLEAN_DECLARATIONS_DEBUG_LEVEL");
163 
164  /* first remove any statement that writes only variable that are never read */
166 
167  /* body*/
169 
171 
172  debug_off();
173 
174  /*postlude */
178  return true;
179 }
180 
instruction make_instruction_expression(expression _field_)
Definition: ri.c:1196
expression copy_expression(expression p)
EXPRESSION.
Definition: ri.c:850
void free_instruction(instruction p)
Definition: ri.c:1118
void free_call(call p)
Definition: ri.c:236
static statement module_statement
Definition: alias_check.c:125
void set_cumulated_rw_effects(statement_effects)
list load_cumulated_rw_effects_list(statement)
void reset_cumulated_rw_effects(void)
#define effect_any_reference(e)
FI: cannot be used as a left hand side.
#define effect_write_p(eff)
bool effects_read_variable_p(list, entity)
Definition: effects.c:1123
#define EFFECT(x)
EFFECT.
Definition: effects.h:608
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
#define gen_context_recurse(start, ctxt, domain_number, flt, rwt)
Definition: genC.h:285
#define gen_recurse(start, domain_number, flt, rwt)
Definition: genC.h:283
bool entities_must_conflict_p(entity e1, entity e2)
Check if two entities must conflict.
Definition: conflicts.c:992
bool entities_may_conflict_p(entity e1, entity e2)
Check if two entities may conflict.
Definition: conflicts.c:984
void reset_current_module_entity(void)
Reset the current module entity.
Definition: static.c:97
void reset_current_module_statement(void)
Reset the current module statement.
Definition: static.c:221
statement set_current_module_statement(statement)
Set the current module statement.
Definition: static.c:165
statement get_current_module_statement(void)
Get the current module statement.
Definition: static.c:208
entity set_current_module_entity(entity)
static.c
Definition: static.c:66
entity get_current_module_entity(void)
Get the entity of the current module.
Definition: static.c:85
void gen_recurse_stop(void *obj)
Tells the recursion not to go in this object.
Definition: genClib.c:3251
bool gen_true2(__attribute__((unused)) gen_chunk *u1, __attribute__((unused)) void *u2)
Definition: genClib.c:2785
bool gen_true(__attribute__((unused)) gen_chunk *unused)
Return true and ignore the argument.
Definition: genClib.c:2780
#define ENDP(l)
Test if a list is empty.
Definition: newgen_list.h:66
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
#define CDR(pcons)
Get the list less its first element.
Definition: newgen_list.h:111
string db_get_memory_resource(const char *rname, const char *oname, bool pure)
Return the pointer to the resource, whatever it is.
Definition: database.c:755
#define DB_PUT_MEMORY_RESOURCE(res_name, own_name, res_val)
conform to old interface.
Definition: pipsdbm-local.h:66
call statement_call(statement)
Get the call of a statement.
Definition: statement.c:1406
bool statement_call_p(statement)
Definition: statement.c:364
bool declaration_statement_p(statement)
Had to be optimized according to Beatrice Creusillet.
Definition: statement.c:224
#define debug_on(env)
Definition: misc-local.h:157
#define pips_debug
these macros use the GNU extensions that allow variadic macros, including with an empty list.
Definition: misc-local.h:145
#define pips_internal_error
Definition: misc-local.h:149
#define debug_off()
Definition: misc-local.h:160
static char * module
Definition: pips.c:74
#define binary_call_rhs(c)
#define statement_block_p(stat)
#define entity_declarations(e)
MISC: newgen shorthands.
#define CONTINUE_FUNCTION_NAME
#define entity_variable_p(e)
An entity_variable_p(e) may hide a typedef and hence a functional type.
#define binary_call_lhs(c)
entity module_name_to_entity(const char *mn)
This is an alias for local_name_to_top_level_entity.
Definition: entity.c:1479
entity entity_intrinsic(const char *name)
FI: I do not understand this function name (see next one!).
Definition: entity.c:1292
bool expression_call_p(expression e)
Definition: expression.c:415
call expression_call(expression e)
Definition: expression.c:445
bool expression_reference_p(expression e)
Test if an expression is a reference.
Definition: expression.c:528
reference expression_reference(expression e)
Short cut, meaningful only if expression_reference_p(e) holds.
Definition: expression.c:1832
bool entity_scalar_p(entity)
The concrete type of e is a scalar type.
Definition: variable.c:1113
bool formal_parameter_p(entity)
Definition: variable.c:1489
#define call_function(x)
Definition: ri.h:709
#define reference_variable(x)
Definition: ri.h:2326
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define statement_domain
newgen_sizeofexpression_domain_defined
Definition: ri.h:362
#define entity_name(x)
Definition: ri.h:2790
#define syntax_call(x)
Definition: ri.h:2736
#define statement_declarations(x)
Definition: ri.h:2460
#define statement_instruction(x)
Definition: ri.h:2458
#define instruction_call(x)
Definition: ri.h:1529
#define call_arguments(x)
Definition: ri.h:711
#define call_undefined
Definition: ri.h:685
#define expression_syntax(x)
Definition: ri.h:1247
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
static void remove_unread_variable(statement s, entity e)
Definition: declarations.c:50
static bool entity_read_somewhere_p(entity e, statement in)
Definition: declarations.c:110
static bool entity_may_conflict_with_a_formal_parameter_p(entity e, entity module)
Definition: declarations.c:91
static void remove_unread_variables(statement s)
Definition: declarations.c:116
static void entity_used_somewhere_walker(statement s, entity_used_somewhere_param *p)
Definition: declarations.c:103
bool clean_declarations(const string module_name)
A phase to remove the declaration of useless variables.
Definition: declarations.c:155
void module_clean_declarations(entity module, statement module_statement)
declarations.c
Definition: declarations.c:140
void statement_clean_declarations(statement)
cproto-generated files
Definition: util.c:164
void entity_clean_declarations(entity, statement)
remove all entities declared in module but never used in s
Definition: util.c:177
#define exp
Avoid some warnings from "gcc -Wshadow".
Definition: vasnprintf.c:207