PIPS
comEngine_optimization_function.c
Go to the documentation of this file.
1 /*
2 
3  $Id: comEngine_optimization_function.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 #include <stdio.h>
28 #include <ctype.h>
29 
30 #include "genC.h"
31 #include "linear.h"
32 #include "ri.h"
33 #include "effects.h"
34 
35 #include "resources.h"
36 
37 #include "misc.h"
38 #include "ri-util.h"
39 #include "prettyprint.h"
40 #include "effects-util.h"
41 
42 #include "text-util.h"
43 
44 #include "ray_dte.h"
45 #include "sommet.h"
46 #include "sg.h"
47 #include "polyedre.h"
48 
49 #include "phrase_tools.h"
50 
51 #include "effects-generic.h"
52 #include "effects-simple.h"
53 #include "effects-convex.h"
54 
55 #include "phrase_distribution.h"
56 #include "comEngine.h"
57 #include "phrase.h"
58 
60 
61 static list glSeqs = NIL;
62 static list glStats = NIL;
63 
65 {
67 }
68 
70 {
71  void* bIsNewLoop = hash_get(gIsNewLoop, stat);
72 
73  if(bIsNewLoop != HASH_UNDEFINED_VALUE)
74  {
75  if(glStats != NIL)
76  {
77  printf("put glSeqs 1\n");
78  MAP(STATEMENT, curStat,
79  {
80  print_statement(curStat);
81  }, glStats);
82 
84 
85  glStats = NIL;
86  }
87 
88  printf("put glSeqs 2\n");
89  print_statement(stat);
90 
92 
93  return;
94  }
95 
96  instruction instr = statement_instruction(stat);
97 
98  switch(instruction_tag(instr))
99  {
101  {
102  MAP(STATEMENT, curStat,
103  {
105 
107  break;
108  }
109  /*case is_instruction_loop:
110  {
111  //opt_loop_interchange_fill_lists_loop(stat);
112  break;
113  }*/
114  case is_instruction_call:
115  case is_instruction_test:
116  {
118  break;
119  }
120  default:
121  {
122  pips_internal_error("impossible");
123  break;
124  }
125  }
126 }
127 
129 {
130  loop curLoop = statement_loop(innerStat);
131 
132  range newRange = copy_range(loop_range(curLoop));
133 
134  loop newLoop = make_loop(loop_index(curLoop),
135  newRange,
137  loop_label(curLoop),
139  NIL);
140 
145  make_instruction_loop(newLoop),NIL,NULL,
147 }
148 
150 
151 static void entity_in_stat_rwt(reference curRef, bool * entInStat)
152 {
153  if(reference_variable(curRef) == gIndEnt)
154  {
155  *entInStat = true;
156  }
157 }
158 
159 static bool entity_in_stat(entity ind, statement stat)
160 {
161  bool entInStat = false;
162  gIndEnt = ind;
163 
164  gen_context_recurse(stat, &entInStat,
166 
167  return entInStat;
168 }
169 
170 static void move_statements_stat(statement stat, entity ind,
171  list * lInBody, list * lOutBody)
172 {
173  if(entity_in_stat(ind, stat) ||
174  statement_loop_p(stat))
175  {
176  *lInBody = gen_nconc(*lInBody,
177  CONS(STATEMENT, copy_statement(stat), NIL));
178  }
179  else
180  {
181  *lOutBody = gen_nconc(*lOutBody,
182  CONS(STATEMENT, copy_statement(stat), NIL));
183  }
184 }
185 
186 static void move_statements(statement stat, entity ind,
187  list * lInBody, list * lOutBody)
188 {
189  instruction instr = statement_instruction(stat);
190 
191  switch(instruction_tag(instr))
192  {
194  {
195  MAP(STATEMENT, curStat,
196  {
197  move_statements(curStat, ind, lInBody, lOutBody);
198 
200  break;
201  }
202  case is_instruction_loop:
203  case is_instruction_call:
204  case is_instruction_test:
205  {
206  move_statements_stat(stat, ind, lInBody, lOutBody);
207  break;
208  }
209  default:
210  {
211  pips_internal_error("impossible");
212  break;
213  }
214  }
215 }
216 
218 static bool gEntFound = false;
219 
220 static void entity_in_ref_rwt(reference curRef)
221 {
222  printf("entity_in_ref_rwt\n");
223  print_reference(curRef);printf("\n");
224  if(reference_variable(curRef) == gSearchedEnt)
225  {
226  gEntFound = true;
227  }
228 }
229 
231 {
232  gSearchedEnt = ent;
233  gEntFound = false;
234 
235  printf("entity_in_ref\n");
237  print_reference(ref);printf("\n");
238 
240 
241  if(gEntFound)
242  {
243  printf("found\n");
244  }
245  else
246  {
247  printf("not found\n");
248  }
249 
250  return gEntFound;
251 }
252 
253 static void regenerate_toggles(statement stat, statement newStat,
254  list * lInBody)
255 {
256  //printf("regenerate_toggles\n");
257  //print_statement(stat);
258 
259  list lToggleEnt = hash_get(gLoopToToggleEnt, newStat);
260 
261  if(lToggleEnt == HASH_UNDEFINED_VALUE)
262  {
263  return;
264  }
265 
266  statement tempStat = make_block_statement(*lInBody);
267 
268  list lIncStats = NIL;
269  list lMmcdStats = NIL;
270 
271  FOREACH(ENTITY, oldTog,lToggleEnt)
272  {
273  printf("oldTog\n");
274  print_entity_variable(oldTog);
275 
276  entity newTog =
278  make_basic(is_basic_int, (void *)4));
279  AddEntityToCurrentModule(newTog);
280 
282  make_reference(oldTog, NIL),
283  entity_to_expression(newTog));
284 
285  intptr_t inc = (intptr_t)hash_get(gToggleToInc, oldTog);
286 
287  pips_assert("inc != HASH_UNDEFINED_VALUE",
289 
290  statement incStat = make_toggle_inc_statement(newTog, inc);
291 
292  lIncStats = gen_nconc(lIncStats, CONS(STATEMENT, incStat, NIL));
293 
294  list lRef = NIL;
295  HASH_MAP(ref1, tog1,
296  {
297  if((entity)tog1 == oldTog)
298  {
300  ref1))
301  {
302  lRef = CONS(REFERENCE, ref1, lRef);
303  }
304  }
305  }, gRefToToggle);
306 
307  MAP(REFERENCE, ref1,
308  {
309  hash_put(gRefToToggle, ref1, newTog);
310  }, lRef);
311  gen_free_list(lRef);
312 
313  statement mmcdStat = make_toggle_mmcd(newTog);
314 
315  lMmcdStats = gen_nconc(lMmcdStats, CONS(STATEMENT, mmcdStat, NIL));
316 
317  statement initStat = make_toggle_init_statement(newTog);
318 
320 
321  }
322 
324 
325  free_statement(tempStat);
326 
327  *lInBody = gen_nconc(lMmcdStats, *lInBody);
328  *lInBody = gen_nconc(*lInBody, lIncStats);
329 }
330 
331 static statement make_seqStat(statement stat, statement loopPattern,
332  list oldCurList, entity newInd)
333 {
334  pips_assert("true", newInd==newInd);
335  statement tempStat = STATEMENT(CAR(oldCurList));
336 
337  void* bIsNewLoop = hash_get(gIsNewLoop, tempStat);
338 
339  if(bIsNewLoop == HASH_UNDEFINED_VALUE)
340  {
341  statement seqStat = copy_statement(loopPattern);
342 
343  loop_body(statement_loop(seqStat)) = make_block_statement(oldCurList);
344 
345  return seqStat;
346  }
347 
348  statement saveStat = statement_undefined;
349 
350  MAP(STATEMENT, curStat,
351  {
352  if(statement_loop_p(curStat))
353  {
354  saveStat = curStat;
355  }
356  }, statement_block(tempStat));
357 
358  pips_assert("saveStat != statement_undefined",
359  saveStat != statement_undefined);
360 
361  list curList = gen_full_copy_list(oldCurList);
362 
363  tempStat = STATEMENT(CAR(curList));
364 
365  statement seqStat = statement_undefined;// = STATEMENT(CAR(curList));
366 
367  pips_assert("statement_block_p(tempStat)", statement_block_p(tempStat));
368 
369  MAP(STATEMENT, curStat,
370  {
371  if(statement_loop_p(curStat))
372  {
373  seqStat = curStat;
374  }
375  }, statement_block(tempStat));
376 
377  pips_assert("seqStat != statement_undefined",
378  seqStat != statement_undefined);
379 
380  gen_free_list(curList);
381 
382  statement inLoop = copy_statement(loopPattern);
383 
384  list lOutBody = NIL;
385  list lInBody = NIL;
386 
388  loop_index(statement_loop(loopPattern)),
389  &lInBody, &lOutBody);
390 
392 
393  /*statement stepStat =
394  make_loop_step_stat(inLoop,
395  loop_index(statement_loop(loopPattern)));
396 
397  lInBody = gen_nconc(CONS(STATEMENT, stepStat, NIL), lInBody);
398  */
399 
400  regenerate_toggles(stat, saveStat, &lInBody);
401 
402  loop_body(statement_loop(inLoop)) = make_block_statement(lInBody);
403 
404  lOutBody = gen_nconc(lOutBody, CONS(STATEMENT, inLoop, NIL));
405 
406  loop_body(statement_loop(seqStat)) = make_block_statement(lOutBody);
407 
408  return tempStat;
409 }
410 
412  entity newInd)
413 {
414  printf("comEngine_opt_loop_interchange\n");
415  print_statement(innerStat);
416 
417  pips_assert("innerStat is a loop statement", statement_loop_p(innerStat));
418 
419  statement body = loop_body(statement_loop(innerStat));
420 
421  if(!statement_block_p(body))
422  {
423  return innerStat;
424  }
425 
426  printf("stat\n");
427  print_statement(stat);
428 
429  bool allowedOpt = (intptr_t)hash_get(gLoopToOpt, stat);
430 
431  pips_assert("allowedOpt != HASH_UNDEFINED_VALUE",
432  allowedOpt != (intptr_t)HASH_UNDEFINED_VALUE);
433 
434  if(!allowedOpt)
435  {
436  printf("opt not all\n");
437  return innerStat;
438  }
439  else
440  {
441  printf("opt all\n");
442  }
443 
444  glSeqs = NIL;
445  glStats = NIL;
446 
448 
449  if(glStats != NIL)
450  {
451  printf("put glSeqs 3\n");
452  MAP(STATEMENT, curStat,
453  {
454  print_statement(curStat);
455  }, glStats);
456 
458 
459  glStats = NIL;
460  }
461 
462  list lStats = NIL;
463  statement loopPattern = make_loopPattern(innerStat);
464 
465  //printf("glSeqs\n");
466  MAP(LIST, curList,
467  {
468  /*
469  //printf("glSeqs it 1\n");
470  MAP(STATEMENT, curStat,
471  {
472  //printf("glSeqs it 2\n");
473  //print_statement(curStat);
474  //printf("glSeqs it 2 end\n");
475 
476  }, curList);
477  */
478 
479  statement seqStat = make_seqStat(stat, loopPattern, curList, newInd);
480 
481  lStats = gen_nconc(lStats, CONS(STATEMENT, seqStat, NIL));
482 
483  }, glSeqs);
484 
486 
487  free_statement(loopPattern);
488 
489  printf("bef 2\n");
491  printf("aft 2\n");
492 
493  free_statement(innerStat);
494 
495  statement newStat = make_block_statement(lStats);
496 
497  printf("comEngine_opt_loop_interchange newStat\n");
498  print_statement(newStat);
499 
500  return newStat;
501 }
instruction make_instruction_loop(loop _field_)
Definition: ri.c:1175
execution make_execution(enum execution_utype tag, void *val)
Definition: ri.c:838
range copy_range(range p)
RANGE.
Definition: ri.c:2005
loop make_loop(entity a1, range a2, statement a3, entity a4, execution a5, list a6)
Definition: ri.c:1301
basic make_basic(enum basic_utype tag, void *val)
Definition: ri.c:155
statement copy_statement(statement p)
STATEMENT.
Definition: ri.c:2186
reference make_reference(entity a1, list a2)
Definition: ri.c:2083
statement make_statement(entity a1, intptr_t a2, intptr_t a3, string a4, instruction a5, list a6, string a7, extensions a8, synchronization a9)
Definition: ri.c:2222
synchronization make_synchronization_none(void)
Definition: ri.c:2424
void free_statement(statement p)
Definition: ri.c:2189
static reference ref
Current stmt (an integer)
Definition: adg_read_paf.c:163
hash_table gIsNewLoop
hash_table gLoopToToggleEnt
hash_table gToggleToInc
hash_table gRefToToggle
entity comEngine_make_new_scalar_variable(const char *prefix, basic bas)
list glToggleInitStats
statement make_toggle_init_statement(entity toggleEnt)
statement make_toggle_inc_statement(entity toggleEnt, int val)
void comEngine_replace_reference_in_stat(statement stat, reference ref, expression new)
statement make_toggle_mmcd(entity ent)
static list glStats
static bool entity_in_stat(entity ind, statement stat)
static void entity_in_ref_rwt(reference curRef)
static entity gSearchedEnt
static statement make_loopPattern(statement innerStat)
static statement make_seqStat(statement stat, statement loopPattern, list oldCurList, entity newInd)
static void regenerate_toggles(statement stat, statement newStat, list *lInBody)
static void move_statements_stat(statement stat, entity ind, list *lInBody, list *lOutBody)
static void entity_in_stat_rwt(reference curRef, bool *entInStat)
static bool gEntFound
static void move_statements(statement stat, entity ind, list *lInBody, list *lOutBody)
static bool entity_in_ref(entity ent, reference ref)
static entity gIndEnt
static list glSeqs
static void opt_loop_interchange_fill_lists(statement stat)
static void opt_loop_interchange_fill_lists_stat(statement stat)
hash_table gLoopToOpt
cproto-generated files
statement comEngine_opt_loop_interchange(statement stat, statement innerStat, entity newInd)
#define LIST(x)
Definition: genC.h:93
#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
statement make_block_statement(list)
Make a block statement from a list of statement.
Definition: statement.c:616
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 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
list gen_nconc(list cp1, list cp2)
physically concatenates CP1 and CP2 but do not duplicates the elements
Definition: list.c:344
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
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
#define MAP(_map_CASTER, _map_item, _map_code, _map_list)
Apply/map an instruction block on all the elements of a list (old fashioned)
Definition: newgen_list.h:226
list gen_full_copy_list(list l)
Copy a list structure with element copy.
Definition: list.c:535
list statement_block(statement)
Get the list of block statements of a statement sequence.
Definition: statement.c:1338
loop statement_loop(statement)
Get the loop of a statement.
Definition: statement.c:1374
bool statement_loop_p(statement)
Definition: statement.c:349
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_put(hash_table htp, const void *key, const void *val)
This functions stores a couple (key,val) in the hash table pointed to by htp.
Definition: hash.c:364
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define pips_internal_error
Definition: misc-local.h:149
#define STATEMENT_ORDERING_UNDEFINED
mapping.h inclusion
Definition: newgen-local.h:35
#define HASH_MAP(k, v, code, ht)
Definition: newgen_hash.h:60
#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 UU
Definition: newgen_types.h:98
void print_entity_variable(entity e)
print_entity_variable(e)
Definition: entity.c:56
void print_reference(reference r)
Definition: expression.c:142
void print_statement(statement)
Print a statement on stderr.
Definition: statement.c:98
#define statement_block_p(stat)
#define STATEMENT_NUMBER_UNDEFINED
default values
#define instruction_block(i)
#define empty_comments
Empty comments (i.e.
entity entity_empty_label(void)
Definition: entity.c:1105
expression entity_to_expression(entity e)
if v is a constant, returns a constant call.
Definition: expression.c:165
extensions empty_extensions(void)
extension.c
Definition: extension.c:43
void AddEntityToCurrentModule(entity)
Add a variable entity to the current module declarations.
Definition: variable.c:260
#define loop_body(x)
Definition: ri.h:1644
@ is_basic_int
Definition: ri.h:571
#define REFERENCE(x)
REFERENCE.
Definition: ri.h:2296
#define reference_variable(x)
Definition: ri.h:2326
#define ENTITY(x)
ENTITY.
Definition: ri.h:2755
#define reference_domain
newgen_range_domain_defined
Definition: ri.h:338
#define entity_undefined
Definition: ri.h:2761
@ is_instruction_test
Definition: ri.h:1470
@ is_instruction_call
Definition: ri.h:1474
@ is_instruction_sequence
Definition: ri.h:1469
@ is_instruction_loop
Definition: ri.h:1471
#define instruction_tag(x)
Definition: ri.h:1511
#define sequence_statements(x)
Definition: ri.h:2360
#define instruction_sequence(x)
Definition: ri.h:1514
#define loop_label(x)
Definition: ri.h:1646
#define statement_instruction(x)
Definition: ri.h:2458
#define loop_range(x)
Definition: ri.h:1642
@ is_execution_sequential
Definition: ri.h:1189
#define loop_index(x)
Definition: ri.h:1640
#define statement_undefined
Definition: ri.h:2419
#define STATEMENT(x)
STATEMENT.
Definition: ri.h:2413
char * strdup()
int printf()
#define intptr_t
Definition: stdint.in.h:294
The structure used to build lists in NewGen.
Definition: newgen_list.h:41