PIPS
expression_substitution.c
Go to the documentation of this file.
1 /*
2 
3  $Id: expression_substitution.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 #include <stdlib.h>
29 #include <stdio.h>
30 
31 #include "genC.h"
32 
33 #include "linear.h"
34 #include "ri.h"
35 #include "effects.h"
36 #include "database.h"
37 
38 #include "ri-util.h"
39 #include "effects-util.h"
40 #include "pipsdbm.h"
41 #include "control.h"
42 #include "callgraph.h"
43 
44 #include "misc.h"
45 
46 #include "resources.h"
47 #include "properties.h"
48 #include "expressions.h"
49 
52 
53 /* retrieves the expression used as a pattern based on
54  * the one - statement function given in a property
55  * and sets static variables accordingly
56  */
57 static
59 {
60  pattern_module_name = get_string_property("EXPRESSION_SUBSTITUTION_PATTERN");
62  {
63 
66  if( instruction_block_p(i))
67  {
68  list l = instruction_block(i);
69  if(!ENDP(l)) i = statement_instruction(STATEMENT(CAR(l)));
70  }
71  if( return_instruction_p(i) )
73  else {
74  pips_user_warning("%s used as pattern for expression substitution, but is not a module with a single return !\n", pattern_module_name);
75  return false;
76  }
78 
79  }
80  else
81  {
82  pips_user_warning("EXPRESSION_SUBSTITUTION_PATTERN undefined, but needed to perform expression substitution\n");
83  return false;
84  }
85  return true;
86 }
87 
89 {
90  if(same_expression_p(e,pi[0]))
92 }
93 
95 {
96  expression arge [] = { pattern, into };
98 }
99 
100 static
102 {
103  hash_table symbols; // contains the symbols gathered during the matching
104  // match e against pattern and stocks symbols in hash_table
106  {
109 
110  /* recover pattern's arguments */
111  list iter = NIL;
113  if(entity_formal_p(e))
114  iter=CONS(ENTITY,e,iter);
115  iter=gen_nreverse(iter);
116 
117  /* fill the arguments */
118  list args = NIL;
119  FOREACH(ENTITY, arg, iter)
120  {
121  expression arge = hash_get(symbols,entity_name(arg));
122  pips_assert("created map consistant with dynamic decl",arge != HASH_UNDEFINED_VALUE);
123  args=CONS(EXPRESSION,copy_expression(arge),args);
124 
125  }
126 
128 
129  /* fix the expression field*/
131  make_call(pattern_entity,gen_nreverse(args))
132  );
133  hash_table_free(symbols);
134  return false;
135  }
136  return true;
137 }
138 static
140 {
141  if(instruction_call_p(i))
142  {
144  if( !replace_expression_similar_to_pattern(exp) ) /* replacement successfull */
145  {
149  return false;
150  }
151  }
152  return true;
153 }
154 
155 /* simple pass that performs substitution of expression by module call
156  */
158 {
159  /* prelude */
162 
163  /* search pattern*/
164  bool pattern_set_p = set_pattern();
165  if( pattern_set_p )
166  {
174  0
175  );
176  }
177 
178  /* validate */
182 
183  /*postlude*/
186  return pattern_set_p;
187 }
call make_call(entity a1, list a2)
Definition: ri.c:269
syntax make_syntax_call(call _field_)
Definition: ri.c:2500
expression copy_expression(expression p)
EXPRESSION.
Definition: ri.c:850
syntax copy_syntax(syntax p)
SYNTAX.
Definition: ri.c:2442
void free_expression(expression p)
Definition: ri.c:853
void free_syntax(syntax p)
Definition: ri.c:2445
callees compute_callees(const statement stat)
Recompute the callees of a module statement.
Definition: callgraph.c:355
struct _newgen_struct_statement_ * statement
Definition: cloning.h:21
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
static void substitute_expression_walker(expression e, expression *pi)
static bool replace_instruction_similar_to_pattern(instruction i)
static bool set_pattern()
retrieves the expression used as a pattern based on the one - statement function given in a property ...
bool expression_substitution(const char *module_name)
simple pass that performs substitution of expression by module call
static bool replace_expression_similar_to_pattern(expression e)
static const char * pattern_module_name
static expression pattern
void substitute_expression(void *in, expression pattern, expression into)
expression_substitution.c
void convert_to_standard_operators(void *)
Definition: optimize.c:1364
char * get_string_property(const char *)
#define gen_context_recurse(start, ctxt, domain_number, flt, rwt)
Definition: genC.h:285
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
void gen_multi_recurse(void *o,...)
Multi recursion visitor function.
Definition: genClib.c:3428
bool gen_false(__attribute__((unused)) gen_chunk *unused)
Return false and ignore the argument.
Definition: genClib.c:2796
bool gen_true2(__attribute__((unused)) gen_chunk *u1, __attribute__((unused)) void *u2)
Definition: genClib.c:2785
void gen_null(__attribute__((unused)) void *unused)
Ignore the argument.
Definition: genClib.c:2752
bool return_instruction_p(instruction i)
Test if an instruction is a C or Fortran "return".
Definition: instruction.c:185
#define ENDP(l)
Test if a list is empty.
Definition: newgen_list.h:66
list gen_nreverse(list cp)
reverse a list in place
Definition: list.c:304
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
#define CONS(_t_, _i_, _l_)
List element cell constructor (insert an element at the beginning of a list)
Definition: newgen_list.h:150
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
#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
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
void * hash_get(const hash_table htp, const void *key)
this function retrieves in the hash table pointed to by htp the couple whose key is equal to key.
Definition: hash.c:449
void hash_table_free(hash_table htp)
this function deletes a hash table that is no longer useful.
Definition: hash.c:327
#define pips_user_warning
Definition: misc-local.h:146
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define TOP_LEVEL_MODULE_NAME
Module containing the global variables in Fortran and C.
Definition: naming-local.h:101
#define HASH_UNDEFINED_VALUE
value returned by hash_get() when the key is not found; could also be called HASH_KEY_NOT_FOUND,...
Definition: newgen_hash.h:56
#define string_undefined
Definition: newgen_types.h:40
#define string_undefined_p(s)
Definition: newgen_types.h:41
bool module_reorder(statement body)
Reorder a module and recompute order to statement if any.
Definition: reorder.c:244
#define instruction_block_p(i)
#define instruction_block(i)
entity FindEntity(const char *package, const char *name)
Retrieve an entity from its package/module name and its local name.
Definition: entity.c:1503
bool entity_formal_p(entity p)
is p a formal parameter?
Definition: entity.c:1935
entity module_name_to_entity(const char *mn)
This is an alias for local_name_to_top_level_entity.
Definition: entity.c:1479
bool expression_similar_get_context_p(expression target, expression pattern, hash_table *symbol_table)
similar to expression_similar_p but the hash_map containing the crossref value is retured for further...
Definition: expression.c:3339
call expression_call(expression e)
Definition: expression.c:445
void update_expression_syntax(expression e, syntax s)
frees expression syntax of e and replace it by the new syntax s
Definition: expression.c:3564
bool same_expression_p(expression e1, expression e2)
this is slightly different from expression_equal_p, as it will return true for a+b vs b+a
Definition: expression.c:1426
expression call_to_expression(call c)
Build an expression that call a function or procedure.
Definition: expression.c:309
#define normalized_undefined
Definition: ri.h:1745
#define expression_domain
newgen_execution_domain_defined
Definition: ri.h:154
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define code_declarations(x)
Definition: ri.h:784
#define instruction_domain
newgen_functional_domain_defined
Definition: ri.h:202
#define EXPRESSION(x)
EXPRESSION.
Definition: ri.h:1217
#define reference_domain
newgen_range_domain_defined
Definition: ri.h:338
#define expression_undefined
Definition: ri.h:1223
#define entity_name(x)
Definition: ri.h:2790
#define expression_normalized(x)
Definition: ri.h:1249
#define value_code(x)
Definition: ri.h:3067
#define instruction_call_p(x)
Definition: ri.h:1527
#define statement_instruction(x)
Definition: ri.h:2458
#define syntax_undefined
Definition: ri.h:2676
#define instruction_call(x)
Definition: ri.h:1529
#define call_arguments(x)
Definition: ri.h:711
#define expression_syntax(x)
Definition: ri.h:1247
#define range_domain
newgen_ram_domain_defined
Definition: ri.h:330
#define STATEMENT(x)
STATEMENT.
Definition: ri.h:2413
#define entity_initial(x)
Definition: ri.h:2796
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
#define exp
Avoid some warnings from "gcc -Wshadow".
Definition: vasnprintf.c:207