PIPS
util.c
Go to the documentation of this file.
1 /*
2 
3  $Id: util.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 
25 #ifdef HAVE_CONFIG_H
26 #include "pips_config.h"
27 #endif
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "genC.h"
34 #include "linear.h"
35 
36 #include "misc.h"
37 #include "pipsdbm.h"
38 #include "properties.h"
39 
40 #include "ri.h"
41 #include "ri-util.h"
42 
43 #include "effects.h"
44 #include "effects-simple.h" // used
45 
46 /******************************************************** CLEAN DECLARATIONS */
47 
48 static bool filterout_statement(void *obj) {
49  bool return_val = !INSTANCE_OF(statement,(gen_chunkp)obj);
50  return return_val;
51 }
52 
53 /* entities that match the following conditions are not
54  * cleaned from declarations:
55  */
56 static bool wipeout_entity(entity e) {
57  pips_debug(6,"Wipeout entity evaluating %s\n",entity_name(e));
58  bool return_val = entity_not_constant_or_intrinsic_p(e) && // filters out constants and intrinsics
59  !formal_parameter_p(e) &&
61  !entity_area_p(e)&&
62  !entity_struct_p(e)&&
63  !entity_union_p(e) &&
64  !typedef_entity_p(e);
65 
66  pips_debug(6,"return %d\n",return_val);
67 
68  return return_val;
69 }
70 
71 /**
72  * remove useless entities from declarations
73  * an entity is flagged useless when no reference is found in stmt
74  * and when it is not used by an entity found in stmt
75  *
76  * @param declarations list of entity to purge
77  * @param stmt statement where entities are used
78  *
79  */
80 static void statement_clean_declarations_helper(list declarations,
81  statement stmt) {
82  set referenced_entities =
86 
87  /* look for entity that are used in the statement
88  * SG: we need to work on a copy of the declarations because of
89  * the RemoveLocalEntityFromDeclarations
90  */
91  list decl_cpy = gen_copy_seq(declarations);
92  FOREACH(ENTITY,e,decl_cpy) {
93  /* filtered referenced entities are always used,
94  * some entity types listed in keep_entity cannot be wiped out*/
95  if(wipeout_entity(e) && !set_belong_p(referenced_entities, e)) {
96  /* entities whose declaration have a side effect are always used too */
97  bool has_side_effects_p = false;
98  value v = entity_initial(e);
99  list effects = NIL;
100  if(value_expression_p(v))
103  /* one should check if dimensions do not have side effects either */
104  if(entity_variable_p(e)) {
106  {
107  expression upper = dimension_upper(dim), lower = dimension_lower(dim);
110  }
111  }
112  pips_debug(5,"Evaluating effects on %s\n",entity_name(e));
113  FOREACH(EFFECT, eff, effects) {
114  ifdebug(6) {
115  print_effect(eff);
116  }
117  if(action_write_p(effect_action(eff)))
118  has_side_effects_p = true;
119  }
121 
122  /* do not keep the declaration, and remove it from any declaration_statement */
123  if(!has_side_effects_p) {
124  pips_debug(4,"Remove declaration for %s\n",entity_name(e));
126  }
127  }
128  }
129  gen_free_list(decl_cpy);
130 
131  set_free(referenced_entities);
132 }
133 
134 /**
135  * check if all entities used in s and module are declared in module
136  * does not work as well as expected on c module because it does not fill the statement declaration
137  * @param module module to check
138  * @param s statement where reference can be found
139  */
141 {
142  /* gather referenced entities */
143  set referenced_entities = get_referenced_entities(s);
144 
145  /* fill the declarations with missing entities (ohhhhh a nice 0(n²) algorithm*/
146  list new = NIL;
147  SET_FOREACH(entity,e1,referenced_entities) {
149  new=CONS(ENTITY,e1,new);
150  }
151 
152  set_free(referenced_entities);
155 }
156 
157 
158 /**
159  * remove all the entity declared in s but never referenced
160  * it's a lower version of use-def-elim !
161  *
162  * @param s statement to check
163  */
165 {
166  if(statement_block_p(s)) {
168  }
169 }
170 
171 /**
172  * remove all entities declared in module but never used in s
173  *
174  * @param module module to check
175  * @param s statement where entites may be used
176  */
178 {
180  if( ! same_entity_p(curr,module)) {
183  }
184  else
185  curr=entity_undefined;
186 
188  if(fortran_module_p(module)) /* to keep backward compatibility with hpfc*/
190 
191  if(!entity_undefined_p(curr)){
194  }
195 }
196 
197 /****************************************************** FIND LOOP FROM LABEL */
198 
199 struct flfl {
202 };
203 
204 static bool find_loop_from_label_walker(loop l, struct flfl *p)
205 {
207  entity
208  do_lab_ent = loop_label(l),
209  stmt_lab_ent = statement_label(s);
210  if (gen_eq(p->label, do_lab_ent) || gen_eq(p->label, stmt_lab_ent) )
211  {
212  p->found = s;
213  gen_recurse_stop(NULL);
214  }
215  return true;
216 }
217 
219 {
220  struct flfl ctx = { label, statement_undefined };
221  gen_context_recurse(s, &ctx,
223  return ctx.found;
224 }
225 
227 {
228  entity e = statement_label(st);
229  const char* label_pattern = get_string_property("LABEL_EXCEPTION");
230  bool exception = (*label_pattern!=0) && (strstr(entity_user_name(e),label_pattern)!=NULL);
231  if(!entity_empty_label_p(e) && !exception) {
234  }
235  return true;
236 }
237 
238 bool clean_labels(const string mod_name)
239 {
240  statement mod_stmt = (statement) db_get_memory_resource(DBR_CODE, mod_name, true);
241 
243  set_current_module_statement( mod_stmt);
244 
246 
247  DB_PUT_MEMORY_RESOURCE(DBR_CODE,
248  strdup(mod_name),
249  (char*) mod_stmt);
250 
253 
254  return true;
255 }
256 
257 /******************************************************************** TARGET */
258 
259 // where is this used?
260 
262 {
263  return 16;
264 }
265 
267 {
268  return 64;
269 }
270 
272 {
273  return 8;
274 }
275 
276 #if 0
277 static int get_cache_line_size(void)
278 {
279  return 1;
280 }
281 static int get_minimal_task_size(void)
282 {
283  /* the unit is supposed to be consistent with the complexity cost tables used
284  * that should be expressed in machine cycles
285  */
286  return 10000;
287 }
288 #endif
struct _newgen_struct_statement_ * statement
Definition: cloning.h:21
list expression_to_proper_effects(expression)
#define effect_action(x)
Definition: effects.h:642
#define action_write_p(x)
Definition: effects.h:314
#define EFFECT(x)
EFFECT.
Definition: effects.h:608
char * get_string_property(const char *)
#define gen_chunk_undefined_p(c)
Definition: genC.h:75
#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
void gen_full_free_list(list l)
Definition: genClib.c:1023
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
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
gen_chunk * gen_get_ancestor(int, const void *)
return the first ancestor object found of the given type.
Definition: genClib.c:3560
void gen_null2(__attribute__((unused)) void *u1, __attribute__((unused)) void *u2)
idem with 2 args, to please overpeaky compiler checks
Definition: genClib.c:2758
void gen_null(__attribute__((unused)) void *unused)
Ignore the argument.
Definition: genClib.c:2752
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
list gen_copy_seq(list l)
Copy a list structure.
Definition: list.c:501
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
list gen_nconc(list cp1, list cp2)
physically concatenates CP1 and CP2 but do not duplicates the elements
Definition: list.c:344
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
#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
void * gen_find_eq(const void *item, const list seq)
Definition: list.c:422
bool gen_eq(const void *obj1, const void *obj2)
Definition: list.c: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
#define pips_debug
these macros use the GNU extensions that allow variadic macros, including with an empty list.
Definition: misc-local.h:145
#define SET_FOREACH(type_name, the_item, the_set)
enumerate set elements in their internal order.
Definition: newgen_set.h:78
void set_free(set)
Definition: set.c:332
bool set_belong_p(const set, const void *)
Definition: set.c:194
static char * module
Definition: pips.c:74
#define print_effect(e)
Definition: print.c:336
#define statement_block_p(stat)
#define INSTANCE_OF(type, value)
polymorhism thanks to newgen !
#define entity_declarations(e)
MISC: newgen shorthands.
#define entity_variable_p(e)
An entity_variable_p(e) may hide a typedef and hence a functional type.
bool entity_area_p(entity e)
Definition: area.c:149
bool entity_not_constant_or_intrinsic_p(entity e)
Default entity filter for get_referenced_entities()
Definition: entity.c:3050
const char * entity_user_name(entity e)
Since entity_local_name may contain PIPS special characters such as prefixes (label,...
Definition: entity.c:487
bool entity_struct_p(entity e)
Is entity e the entity corresponding to a struct declaration?
Definition: entity.c:1002
bool same_entity_p(entity e1, entity e2)
predicates on entities
Definition: entity.c:1321
set get_referenced_entities_filtered(void *elem, bool(*chunk_filter)(void *), bool(*entity_filter)(entity))
Same as get_referenced_entities, but will only consider entities that fulfills entity_filter and will...
Definition: entity.c:2982
entity module_name_to_entity(const char *mn)
This is an alias for local_name_to_top_level_entity.
Definition: entity.c:1479
bool typedef_entity_p(entity e)
Definition: entity.c:1902
entity entity_empty_label(void)
Definition: entity.c:1105
void sort_list_of_entities(list l)
sorted in place.
Definition: entity.c:1358
bool entity_empty_label_p(entity e)
Definition: entity.c:666
bool fortran_module_p(entity m)
Test if a module is in Fortran.
Definition: entity.c:2799
set get_referenced_entities(void *elem)
retrieves the set of entities used in elem beware that this entities may be formal parameters,...
Definition: entity.c:3063
bool entity_union_p(entity e)
Is entity e an entity representing the union declaration?
Definition: entity.c:1038
void RemoveLocalEntityFromDeclarations(entity, entity, statement)
Definition: variable.c:120
bool formal_parameter_p(entity)
Definition: variable.c:1489
#define loop_domain
newgen_language_domain_defined
Definition: ri.h:218
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define dimension_lower(x)
Definition: ri.h:980
#define type_variable(x)
Definition: ri.h:2949
#define entity_storage(x)
Definition: ri.h:2794
#define statement_domain
newgen_sizeofexpression_domain_defined
Definition: ri.h:362
#define statement_label(x)
Definition: ri.h:2450
#define entity_undefined_p(x)
Definition: ri.h:2762
#define entity_undefined
Definition: ri.h:2761
#define entity_name(x)
Definition: ri.h:2790
#define dimension_upper(x)
Definition: ri.h:982
#define loop_label(x)
Definition: ri.h:1646
#define variable_dimensions(x)
Definition: ri.h:3122
#define statement_declarations(x)
Definition: ri.h:2460
#define entity_type(x)
Definition: ri.h:2792
#define value_expression_p(x)
Definition: ri.h:3080
#define storage_return_p(x)
Definition: ri.h:2516
#define value_expression(x)
Definition: ri.h:3082
#define statement_undefined
Definition: ri.h:2419
#define entity_initial(x)
Definition: ri.h:2796
char * strdup()
#define ifdebug(n)
Definition: sg.c:47
FI: I do not understand why the type is duplicated at the set level.
Definition: set.c:59
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
Definition: util.c:199
entity label
Definition: util.c:200
statement found
Definition: util.c:201
Definition: statement.c:54
void gen_clear_tabulated_element(gen_chunk *obj)
GEN_CLEAR_TABULATED_ELEMENT only clears the entry for object OBJ in the gen_tabulated_ and gen_tabula...
Definition: tabulated.c:251
static bool find_loop_from_label_walker(loop l, struct flfl *p)
Definition: util.c:204
bool clean_labels(const string mod_name)
Definition: util.c:238
static void entity_generate_missing_declarations(entity module, statement s)
check if all entities used in s and module are declared in module does not work as well as expected o...
Definition: util.c:140
statement find_loop_from_label(statement s, entity label)
hmmm...
Definition: util.c:218
static bool wipeout_entity(entity e)
entities that match the following conditions are not cleaned from declarations:
Definition: util.c:56
static void statement_clean_declarations_helper(list declarations, statement stmt)
remove useless entities from declarations an entity is flagged useless when no reference is found in ...
Definition: util.c:80
void entity_clean_declarations(entity module, statement s)
remove all entities declared in module but never used in s
Definition: util.c:177
void statement_clean_declarations(statement s)
remove all the entity declared in s but never referenced it's a lower version of use-def-elim !
Definition: util.c:164
static bool clean_statement_label(statement st)
Definition: util.c:226
int get_vector_register_length(void)
Definition: util.c:266
int get_vector_register_number(void)
Definition: util.c:271
int get_processor_number(void)
Definition: util.c:261
static bool filterout_statement(void *obj)
Definition: util.c:48
A gen_chunk is used to store every object.
Definition: genC.h:58