PIPS
control_counters.c
Go to the documentation of this file.
1 /*
2 
3  $Id: control_counters.c 23065 2016-03-02 09:05:50Z 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 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "genC.h"
34 #include "linear.h"
35 
36 #include "pipsdbm.h"
37 
38 #include "ri-util.h"
39 #include "control.h" // module_reorder
40 
41 /* Add Control Counter recursion context
42  */
43 typedef struct {
45 } acc_ctx;
46 
47 /* generate: var = var + 1 (Fortran) or var++ (C)
48  */
50 {
56  int_to_expression(1)));
57  else // this is probably C
58  return make_call_statement
62 }
63 
64 /* create a new integer local variable in module using name as a prefix
65  */
66 static entity create_counter(entity module, const string name)
67 {
68  // build name (could also insert: entity_local_name(module))
69  string full = strdup(concatenate("_", name, "_", NULL));
70  // create an integer counter
71  entity var =
73  // cleanup
74  free(full), full=NULL;
75  // initialized to zero
78  // add as a local variable
80  return var;
81 }
82 
83 /* add a new counter at entry of statement s
84  */
85 static void add_counter(acc_ctx * c, string name, statement s)
86 {
87  entity counter = create_counter(c->module, name);
88  insert_statement(s, make_increment_statement(c->module, counter), true);
89 }
90 
91 /* add a counter to each branch of the test
92  */
93 static void test_rwt(test t, acc_ctx * c)
94 {
95  add_counter(c, "if_then", test_true(t));
96  add_counter(c, "if_else", test_false(t));
97 }
98 
99 /* add a counter to the loop body
100  */
101 static void loop_rwt(loop l, acc_ctx * c)
102 {
103  add_counter(c, "do", loop_body(l));
104 }
105 
106 /* add a counter to the loop body
107  */
108 static void whileloop_rwt(whileloop w, acc_ctx * c)
109 {
110  add_counter(c, "while", whileloop_body(w));
111 }
112 
113 /* add a counter to the loop body
114  */
115 static void forloop_rwt(forloop f, acc_ctx * c)
116 {
117  add_counter(c, "for", forloop_body(f));
118 }
119 
120 /* add control counter instrumentation
121  * assumes current module entity & statement are okay.
122  */
124 {
125  acc_ctx c = { module };
127  (root, &c,
132  NULL);
133 }
134 
135 /* instrument a module with control structure counters for test & loops
136  */
138 {
139  // get resources from database
141  statement stat =
142  (statement) db_get_memory_resource(DBR_CODE, module_name, true);
143 
146 
147  // do the job
148  add_counters(module, stat);
149 
150  // reset unique identifier for each statement
151  module_reorder(stat);
152 
153  // update resource
154  DB_PUT_MEMORY_RESOURCE(DBR_CODE, module_name, stat);
155 
156  // cleanup
159  return true;
160 }
value make_value_expression(expression _field_)
Definition: ri.c:2850
basic make_basic_int(intptr_t _field_)
Definition: ri.c:158
void free_value(value p)
Definition: ri.c:2787
struct _newgen_struct_statement_ * statement
Definition: cloning.h:21
static void forloop_rwt(forloop f, acc_ctx *c)
add a counter to the loop body
static void test_rwt(test t, acc_ctx *c)
add a counter to each branch of the test
static void whileloop_rwt(whileloop w, acc_ctx *c)
add a counter to the loop body
bool add_control_counters(const string module_name)
instrument a module with control structure counters for test & loops
static entity create_counter(entity module, const string name)
create a new integer local variable in module using name as a prefix
static void loop_rwt(loop l, acc_ctx *c)
add a counter to the loop body
static void add_counter(acc_ctx *c, string name, statement s)
add a new counter at entry of statement s
static void add_counters(entity module, statement root)
add control counter instrumentation assumes current module entity & statement are okay.
static statement make_increment_statement(entity module, entity var)
generate: var = var + 1 (Fortran) or var++ (C)
const char * module_name(const char *s)
Return the module part of an entity name.
Definition: entity_names.c:296
void free(void *)
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
void gen_context_multi_recurse(void *o, void *context,...)
Multi-recursion with context function visitor.
Definition: genClib.c:3373
bool gen_true(__attribute__((unused)) gen_chunk *unused)
Return true and ignore the argument.
Definition: genClib.c:2780
#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
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
statement make_assign_statement(expression, expression)
Definition: statement.c:583
statement make_call_statement(string, list, entity, string)
This function is limited to intrinsics calls...
Definition: statement.c:1274
void insert_statement(statement, statement, bool)
This is the normal entry point.
Definition: statement.c:2570
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
static char * module
Definition: pips.c:74
bool module_reorder(statement body)
Reorder a module and recompute order to statement if any.
Definition: reorder.c:244
#define PLUS_OPERATOR_NAME
#define POST_INCREMENT_OPERATOR_NAME
Definition: ri-util-local.h:97
#define empty_comments
Empty comments (i.e.
entity module_name_to_entity(const char *mn)
This is an alias for local_name_to_top_level_entity.
Definition: entity.c:1479
bool fortran_module_p(entity m)
Test if a module is in Fortran.
Definition: entity.c:2799
entity entity_intrinsic(const char *name)
FI: I do not understand this function name (see next one!).
Definition: entity.c:1292
expression entity_to_expression(entity e)
if v is a constant, returns a constant call.
Definition: expression.c:165
expression MakeBinaryCall(entity f, expression eg, expression ed)
Creates a call expression to a function with 2 arguments.
Definition: expression.c:354
expression int_to_expression(_int i)
transform an int into an expression and generate the corresponding entity if necessary; it is not cle...
Definition: expression.c:1188
void AddEntityToCurrentModule(entity)
Add a variable entity to the current module declarations.
Definition: variable.c:260
entity make_new_scalar_variable_with_prefix(const char *, entity, basic)
Create a new scalar variable of type b in the given module.
Definition: variable.c:592
#define forloop_domain
newgen_extensions_domain_defined
Definition: ri.h:178
#define loop_body(x)
Definition: ri.h:1644
#define test_domain
newgen_entity_domain_defined
Definition: ri.h:418
#define loop_domain
newgen_language_domain_defined
Definition: ri.h:218
#define test_false(x)
Definition: ri.h:2837
#define entity_undefined
Definition: ri.h:2761
#define test_true(x)
Definition: ri.h:2835
#define whileloop_body(x)
Definition: ri.h:3162
#define whileloop_domain
newgen_variable_domain_defined
Definition: ri.h:466
#define forloop_body(x)
Definition: ri.h:1372
#define entity_initial(x)
Definition: ri.h:2796
char * strdup()
Add Control Counter recursion context.
entity module
@ full
Definition: union-local.h:65