PIPS
dowhile_to_while.c
Go to the documentation of this file.
1 /*
2 
3  $Id: dowhile_to_while.c 23470 2018-06-02 13:39: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 #include "genC.h"
28 #include "linear.h"
29 #include "ri.h"
30 #include "ri-util.h"
31 #include "misc.h"
32 #include "control.h"
33 #include "pipsdbm.h"
34 #include "resources.h"
35 
36 /************************************************** SWITCH DO-WHILE TO WHILE */
37 
38 static void dowhile_to_while_walker(statement stmt, bool * changed)
39 {
41  if ( instruction_whileloop_p(instr) )
42  {
43  whileloop wl = instruction_whileloop(instr);
44 
45  // is it a do-while loop?
47  {
48  // we did something!
49  *changed = true;
50 
51  // do-while -> while-do
54 
55  // push while-do instruction
56  statement duplicated_stat = make_empty_statement();
57 
58  // duplicate while-do body statements and push it
59  // BUG: it seems that clone does not handle "unstructured"
63  NIL,
64  duplicated_stat
65  );
68 
69  insert_statement(duplicated_stat, dup, false);
70  insert_statement(duplicated_stat, instruction_to_statement(instr), false);
71 
72  // see how elegant is the patching ?
75  (stmt,
76  make_instruction_block(CONS(statement, duplicated_stat, NIL))
77  );
78  }
79  }
80 }
81 
82 bool dowhile_to_while(const char* module_name)
83 {
84  // prelude
85  debug_on("CONTROL_DEBUG_LEVEL");
89  );
90 
91  // whether anything was changed
92  bool changed = false;
93 
94  // actual transformation
97 
98  // postlude
99  if (changed)
100  {
104  }
105 
108  debug_off();
109  return true;
110 }
111 
112 /************************************************* LOOP TRANSFORMATION UTILS */
113 
114 /* converts a doloop to a while loop, in place */
116 {
117  pips_assert("statement is a loop",statement_loop_p(sl));
118  loop l =statement_loop(sl);
119  range r = loop_range(l);
120 
121  /* convert the loop to a while loop :
122  * fst the body
123  */
124  list statements = make_statement_list(
131  range_increment(r)
132  )
133  )
134  );
135  /* then the whileloop */
140  range_upper(r)
141  ),
142  make_block_statement(statements),
145 
146  /* and the prelude */
147  sequence seq = make_sequence(
150  range_lower(r)),
152  )
153  );
157 
159 }
160 
161 /* converts a doloop to a for loop, in place */
163 {
164  pips_assert("statement is a loop",statement_loop_p(sl));
165  loop l =statement_loop(sl);
166  range r = loop_range(l);
167 
168  forloop fl = make_forloop(
179 }
180 
clone_context make_clone_context(entity a1, entity a2, list a3, statement a4)
Definition: cloning.c:52
void free_clone_context(clone_context p)
Definition: cloning.c:19
evaluation make_evaluation_before(void)
Definition: ri.c:786
instruction make_instruction_forloop(forloop _field_)
Definition: ri.c:1193
whileloop make_whileloop(expression a1, statement a2, entity a3, evaluation a4)
Definition: ri.c:2937
expression copy_expression(expression p)
EXPRESSION.
Definition: ri.c:850
statement copy_statement(statement p)
STATEMENT.
Definition: ri.c:2186
instruction make_instruction_sequence(sequence _field_)
Definition: ri.c:1169
void free_evaluation(evaluation p)
Definition: ri.c:743
sequence make_sequence(list a)
Definition: ri.c:2125
instruction make_instruction_whileloop(whileloop _field_)
Definition: ri.c:1178
forloop make_forloop(expression a1, expression a2, expression a3, statement a4)
Definition: ri.c:1025
statement clone_statement(statement s, clone_context cc)
clone_statement.c
void do_loop_to_for_loop(statement sl)
converts a doloop to a for loop, in place
bool dowhile_to_while(const char *module_name)
dowhile_to_while.c
void do_loop_to_while_loop(statement sl)
converts a doloop to a while loop, in place
static void dowhile_to_while_walker(statement stmt, bool *changed)
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
statement make_block_statement(list)
Make a block statement from a list of statement.
Definition: statement.c:616
statement instruction_to_statement(instruction)
Build a statement from a give instruction.
Definition: statement.c:597
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
bool gen_true2(__attribute__((unused)) gen_chunk *u1, __attribute__((unused)) void *u2)
Definition: genClib.c:2785
instruction make_instruction_block(list statements)
Build an instruction block from a list of statements.
Definition: instruction.c:106
#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
loop statement_loop(statement)
Get the loop of a statement.
Definition: statement.c:1374
bool statement_loop_p(statement)
Definition: statement.c:349
statement make_assign_statement(expression, expression)
Definition: statement.c:583
statement update_statement_instruction(statement, instruction)
Replace the instruction in statement s by instruction i.
Definition: statement.c:3039
void insert_statement(statement, statement, bool)
This is the normal entry point.
Definition: statement.c:2570
#define debug_on(env)
Definition: misc-local.h:157
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define debug_off()
Definition: misc-local.h:160
bool module_reorder(statement body)
Reorder a module and recompute order to statement if any.
Definition: reorder.c:244
#define PLUS_OPERATOR_NAME
#define PLUS_UPDATE_OPERATOR_NAME
#define make_statement_list(stats...)
easy list constructor
#define LESS_OR_EQUAL_OPERATOR_NAME
#define make_empty_statement
An alias for make_empty_block_statement.
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_empty_label(void)
Definition: entity.c:1105
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 make_assign_expression(expression lhs, expression rhs)
Make an assign expression, since in C the assignment is a side effect operator.
Definition: expression.c:390
#define loop_body(x)
Definition: ri.h:1644
#define range_upper(x)
Definition: ri.h:2290
#define whileloop_evaluation(x)
Definition: ri.h:3166
#define statement_domain
newgen_sizeofexpression_domain_defined
Definition: ri.h:362
#define range_increment(x)
Definition: ri.h:2292
#define instruction_undefined
Definition: ri.h:1454
#define expression_undefined
Definition: ri.h:1223
#define instruction_whileloop(x)
Definition: ri.h:1523
#define range_lower(x)
Definition: ri.h:2288
#define whileloop_body(x)
Definition: ri.h:3162
#define statement_instruction(x)
Definition: ri.h:2458
#define instruction_whileloop_p(x)
Definition: ri.h:1521
#define loop_range(x)
Definition: ri.h:1642
#define evaluation_after_p(x)
Definition: ri.h:1162
#define loop_index(x)
Definition: ri.h:1640
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
Definition: statement.c:54