PIPS
bound_generation.c
Go to the documentation of this file.
1 /*
2 
3  $Id: bound_generation.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 /* Generation of bound expressions from constraint systems of the
25  Linear library */
26 #ifdef HAVE_CONFIG_H
27  #include "pips_config.h"
28 #endif
29 
30 #include <stdio.h>
31 
32 #include "linear.h"
33 
34 #include "genC.h"
35 #include "misc.h"
36 #include "ri.h"
37 #include "ri-util.h"
38 
39 /* Used for sorting each constraint and in between constraints, hopefully */
41 {
42  entity e1 = (entity) vecteur_var(*pv1);
43  entity e2 = (entity) vecteur_var(*pv2);
44  // FI: is "less" a well-chosen name?
45  // int less = strcmp(entity_local_name(e1), entity_local_name(e2));
46  int less;
47 
48  if(e1==NULL) {
49  if(e2==NULL)
50  less = 0;
51  else
52  less = -1;
53  }
54  else if(e2==NULL)
55  less = 1;
56  else
57  less = strcmp(entity_user_name(e1), entity_user_name(e2));
58 
59 #if 0
60  if(less==0) {
61  Value v1 = vecteur_val(*pv1);
62  Value v2 = vecteur_val(*pv2);
63  if(value_gt(v1,v2))
64  less = 1;
65  else if(value_lt(v1,v2))
66  less = -1;
67  else
68  /* To satisfy a later assert not so well designed */
69  less = pv1<pv2? 1 : -1;
70  }
71 #endif
72 
73  return less;
74 }
75 
76 /* void make_bound_expression(variable index, Pbase base, Psysteme sc,
77  * expression *lower, expression *upper)
78  *
79  * build the expressions "lower" and "upper" for the lower and upper
80  * bounds of variable "index". Variable "index" must appear in "base"
81  * and have lower and upper bounds in "sc"
82  *
83  * Beware of degenerated cases where constraints are reduced to
84  * equations because the upper and lower bounds are identical.
85  *
86  * The constraints are sorted according to the lexicographic order
87  * using bound_generation_compare_vector_component().
88  *
89  * see also constraints_to_loop_bound (in conversion system_to_code.c)
90  */
92  Pbase base,
93  Psysteme sc,
94  expression *lower,
95  expression *upper)
96 {
97  Pcontrainte pc;
98  cons *ll = NIL;
99  cons *lu = NIL;
100 
101  expression ex;
102  entity min, max;
103 
104  int i;
105  int rank_index ;
106 
107  /* compute the rank d of the index in the basis */
108  rank_index =
111 
112  pips_debug(7, "index :%s\n", entity_name_or_TCST(index));
113  pips_debug(8, "rank_index = %d\n", rank_index);
114 
115  /* The constraints should be lexicographically sorted to avoid
116  secondary variations in linear */
117  // contrainte_vect_sort(ll, bound_generation_compare_vector_component);
118  ifdebug(7) {
119  fprintf(stderr, "Constraints before sorting:\n");
120  sc_dump(sc);
121  }
123  ifdebug(7) {
124  fprintf(stderr, "Constraints after sorting:\n");
125  sc_dump(sc);
126  }
127 
128  /* search constraints referencing "index" among inequalities and
129  create the list of expressions for lower and upper bounds */
130  for (pc=sc->inegalites; pc!=NULL; pc=pc->succ) {
131  i = level_contrainte(pc, base);
132  pips_debug(8,"level: %d\n",i);
133  if (ABS(i)==rank_index){ /* found */
134  ifdebug(7) {
135  (void) fprintf(stderr, "\n constraint before :");
136  contrainte_fprint(stderr, pc, true,
138  }
139  ex = make_constraint_expression(pc->vecteur, (Variable) index);
140  // FI: to avoid cycles betwen librairies ri-util and prettyprint
141  /* ifdebug(7) { */
142  /* fprintf(stderr, "\n expression after :"); */
143  /* print_expression(ex); */
144  /* } */
145  /* add the expression to the list of lower bounds
146  or to the list of upper bounds*/
147  if (i>0)
148  lu = CONS(EXPRESSION, ex, lu);
149  else
150  ll = CONS(EXPRESSION, ex, ll);
151  }
152  }
153 
154  /* search equation constraints referencing "index" and create the
155  list of expressions for lower and upper bounds. We may have to
156  generate useless loops with only one iteration. */
157  for (pc=sc->egalites; pc!=NULL; pc=pc->succ) {
158  i = level_contrainte(pc, base);
159  pips_debug(8,"level: %d\n",i);
160  if (ABS(i)==rank_index){ /* found */
161  ifdebug(7) {
162  (void) fprintf(stderr, "\n constraint before :");
163  contrainte_fprint(stderr, pc, true,
165  }
166  if(i>0) {
167  Pvecteur mv = vect_copy(pc->vecteur);
168  mv = vect_multiply(mv, VALUE_MONE);
169  ex = make_constraint_expression(mv, (Variable) index);
170  vect_rm(mv);
171  }
172  else
173  ex = make_constraint_expression(pc->vecteur, (Variable) index);
174  // FI: to avoid cycles betwen librairies ri-util and prettyprint
175  /* ifdebug(7) { */
176  /* fprintf(stderr, "\n expression after :"); */
177  /* print_expression(ex); */
178  /* } */
179 
180  /* add the expression to the list of lower bounds
181  and to the list of upper bounds*/
182  lu = CONS(EXPRESSION, ex, lu);
183  ll = CONS(EXPRESSION, ex, ll);
184  }
185  }
186 
187  /* Reverse the expression order */
188  ll = gen_nreverse(ll);
189  lu = gen_nreverse(lu);
190 
191  /* build expressions for the lower and upper bounds */
193  /* To avoid clash with Fortran intrinsics */
194  /* pips_min and pips_max are supposed to be part of PIPS
195  run-time. They are varargs and their first argument is the
196  count of arguments */
199  }
200  else { // Fortran case
203  }
204 
205  pips_assert("entities for min and max are found",
207 
208  if (gen_length(ll) > 1) {
210  int c = gen_length(ll);
212  ll = CONS(EXPRESSION, ce, ll);
213  }
215  make_call(max,ll)),
217  }
218  else {
219  *lower = EXPRESSION(CAR(ll)); /* and memory leak... (cons lost) */
220  gen_free_list(ll);
221  }
222 
223  if (gen_length(lu) > 1 ) {
225  int c = gen_length(lu);
227  lu = CONS(EXPRESSION, ce, lu);
228  }
230  make_call(min,lu)),
232  }
233  else {
234  *upper = EXPRESSION(CAR(lu)); /* idem... */
235  gen_free_list(lu);
236  }
237 
238  ifdebug(7) {
239  pips_debug(9, "returning: \n");
240  // FI: to avoid cycles betwen librairies ri-util and prettyprint
241  /* print_expression(*lower); */
242  /* print_expression(*upper); */
243  }
244 }
call make_call(entity a1, list a2)
Definition: ri.c:269
expression make_expression(syntax a1, normalized a2)
Definition: ri.c:886
syntax make_syntax(enum syntax_utype tag, void *val)
Definition: ri.c:2491
struct _newgen_struct_entity_ * entity
Definition: abc_private.h:14
#define value_gt(v1, v2)
#define VALUE_MONE
int Value
#define ABS(x)
was: #define value_mult(v,w) value_direct_multiply(v,w) #define value_product(v,w) value_direct_produ...
#define value_lt(v1, v2)
int base_find_variable_rank(Pbase b, Variable v, char *(*variable_name)(Variable))
int base_find_variable_rank(Pbase b, Variable v, char * (*variable_name)()): returns variable v's ran...
Definition: base.c:194
bdt base
Current expression.
Definition: bdt_read_paf.c:100
int level_contrainte(Pcontrainte, Pbase)
int level_contrainte(Pcontrainte pc, Pbase base_index) compute the level (rank) of the constraint pc ...
Definition: unaires.c:292
void contrainte_fprint(FILE *, Pcontrainte, bool, char *(*)(Variable))
io.c
#define min(a, b)
#define max(a, b)
entity get_current_module_entity(void)
Get the entity of the current module.
Definition: static.c:85
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
size_t gen_length(const list l)
Definition: list.c:150
#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
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
#define pips_debug
these macros use the GNU extensions that allow variadic macros, including with an empty list.
Definition: misc-local.h:145
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define MAX_OPERATOR_NAME
#define PIPS_C_MAX_OPERATOR_NAME
#define PIPS_C_MIN_OPERATOR_NAME
PIPS run-time support for C code generation.
#define MIN_OPERATOR_NAME
int bound_generation_compare_vector_component(Pvecteur *pv1, Pvecteur *pv2)
Generation of bound expressions from constraint systems of the Linear library.
void make_bound_expression(Variable index, Pbase base, Psysteme sc, expression *lower, expression *upper)
void make_bound_expression(variable index, Pbase base, Psysteme sc, expression *lower,...
const char * entity_user_name(entity e)
Since entity_local_name may contain PIPS special characters such as prefixes (label,...
Definition: entity.c:487
const char * entity_name_or_TCST(entity e)
Return a name valid for sorting variables in vectors and constraint systems.
Definition: entity.c:627
entity entity_intrinsic(const char *name)
FI: I do not understand this function name (see next one!).
Definition: entity.c:1292
expression make_constraint_expression(Pvecteur v, Variable index)
Make an expression from a constraint v for a given index.
Definition: expression.c:1748
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
bool c_language_module_p(entity m)
Definition: module.c:447
#define normalized_undefined
Definition: ri.h:1745
@ is_syntax_call
Definition: ri.h:2693
#define EXPRESSION(x)
EXPRESSION.
Definition: ri.h:1217
#define entity_undefined
Definition: ri.h:2761
void sc_dump(Psysteme sc)
void sc_dump(Psysteme sc): dump to stderr
Definition: sc_io.c:142
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
void sc_lexicographic_sort(Psysteme sc, int(*compare)(Pvecteur *, Pvecteur *))
Minimize first the lexico-graphic weight of each constraint according to the comparison function "com...
Definition: sc_unaires.c:206
Pvecteur vect_multiply(Pvecteur v, Value x)
Pvecteur vect_multiply(Pvecteur v, Value x): multiplication du vecteur v par le scalaire x,...
Definition: scalaires.c:123
#define ifdebug(n)
Definition: sg.c:47
Pvecteur vecteur
struct Scontrainte * succ
Pcontrainte inegalites
Definition: sc-local.h:71
Pcontrainte egalites
Definition: sc-local.h:70
le type des coefficients dans les vecteurs: Value est defini dans le package arithmetique
Definition: vecteur-local.h:89
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
#define vecteur_val(v)
#define vecteur_var(v)
char *(* get_variable_name_t)(Variable)
Definition: vecteur-local.h:62
void * Variable
arithmetique is a requirement for vecteur, but I do not want to inforce it in all pips files....
Definition: vecteur-local.h:60
Pbase vect_copy(Pvecteur b)
direct duplication.
Definition: alloc.c:240
void vect_rm(Pvecteur v)
void vect_rm(Pvecteur v): desallocation des couples de v;
Definition: alloc.c:78