In libobjc/: 2011-05-24 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / gcc / graphite-ppl.c
blob9762ca46770bbc8b39f7135999d23d64ac89d50d
1 /* Gimple Represented as Polyhedra.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@amd.com>
4 and Tobias Grosser <grosser@fim.uni-passau.de>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
26 #ifdef HAVE_cloog
28 #include "ppl_c.h"
29 #include "graphite-cloog-util.h"
30 #include "graphite-ppl.h"
32 /* Set the inhomogeneous term of E to X. */
34 void
35 ppl_set_inhomogeneous_gmp (ppl_Linear_Expression_t e, mpz_t x)
37 mpz_t v0, v1;
38 ppl_Coefficient_t c;
40 mpz_init (v0);
41 mpz_init (v1);
42 ppl_new_Coefficient (&c);
44 ppl_Linear_Expression_inhomogeneous_term (e, c);
45 ppl_Coefficient_to_mpz_t (c, v1);
46 mpz_neg (v1, v1);
47 mpz_set (v0, x);
48 mpz_add (v0, v0, v1);
49 ppl_assign_Coefficient_from_mpz_t (c, v0);
50 ppl_Linear_Expression_add_to_inhomogeneous (e, c);
52 mpz_clear (v0);
53 mpz_clear (v1);
54 ppl_delete_Coefficient (c);
57 /* Set E[I] to X. */
59 void
60 ppl_set_coef_gmp (ppl_Linear_Expression_t e, ppl_dimension_type i, mpz_t x)
62 mpz_t v0, v1;
63 ppl_Coefficient_t c;
65 mpz_init (v0);
66 mpz_init (v1);
67 ppl_new_Coefficient (&c);
69 ppl_Linear_Expression_coefficient (e, i, c);
70 ppl_Coefficient_to_mpz_t (c, v1);
71 mpz_neg (v1, v1);
72 mpz_set (v0, x);
73 mpz_add (v0, v0, v1);
74 ppl_assign_Coefficient_from_mpz_t (c, v0);
75 ppl_Linear_Expression_add_to_coefficient (e, i, c);
77 mpz_clear (v0);
78 mpz_clear (v1);
79 ppl_delete_Coefficient (c);
82 /* Insert after X NB_NEW_DIMS empty dimensions into PH.
84 With x = 3 and nb_new_dims = 4
86 | d0 d1 d2 d3 d4
88 is transformed to
90 | d0 d1 d2 x0 x1 x2 x3 d3 d4
92 | map = {0, 1, 2, 7, 8, 3, 4, 5, 6}
95 void
96 ppl_insert_dimensions_pointset (ppl_Pointset_Powerset_C_Polyhedron_t ph, int x,
97 int nb_new_dims)
99 ppl_dimension_type i, dim;
100 ppl_dimension_type *map;
101 ppl_dimension_type x_ppl, nb_new_dims_ppl;
103 x_ppl = (ppl_dimension_type) x;
104 nb_new_dims_ppl = (ppl_dimension_type) nb_new_dims;
106 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (ph, &dim);
107 ppl_Pointset_Powerset_C_Polyhedron_add_space_dimensions_and_embed (ph, nb_new_dims);
109 map = (ppl_dimension_type *) XNEWVEC (ppl_dimension_type, dim + nb_new_dims);
111 for (i = 0; i < x_ppl; i++)
112 map[i] = i;
114 for (i = x_ppl; i < x_ppl + nb_new_dims_ppl; i++)
115 map[dim + i - x_ppl] = i;
117 for (i = x_ppl + nb_new_dims_ppl; i < dim + nb_new_dims_ppl; i++)
118 map[i - nb_new_dims_ppl] = i;
120 ppl_Pointset_Powerset_C_Polyhedron_map_space_dimensions (ph, map, dim + nb_new_dims);
121 free (map);
124 /* Insert after X NB_NEW_DIMS empty dimensions into PH.
126 With x = 3 and nb_new_dims = 4
128 | d0 d1 d2 d3 d4
130 is transformed to
132 | d0 d1 d2 x0 x1 x2 x3 d3 d4
134 | map = {0, 1, 2, 7, 8, 3, 4, 5, 6}
137 void
138 ppl_insert_dimensions (ppl_Polyhedron_t ph, int x,
139 int nb_new_dims)
141 ppl_dimension_type i, dim;
142 ppl_dimension_type *map;
143 ppl_dimension_type x_ppl, nb_new_dims_ppl;
145 x_ppl = (ppl_dimension_type) x;
146 nb_new_dims_ppl = (ppl_dimension_type) nb_new_dims;
148 ppl_Polyhedron_space_dimension (ph, &dim);
149 ppl_Polyhedron_add_space_dimensions_and_embed (ph, nb_new_dims);
151 map = (ppl_dimension_type *) XNEWVEC (ppl_dimension_type, dim + nb_new_dims);
153 for (i = 0; i < x_ppl; i++)
154 map[i] = i;
156 for (i = x_ppl; i < x_ppl + nb_new_dims_ppl; i++)
157 map[dim + i - x_ppl] = i;
159 for (i = x_ppl + nb_new_dims_ppl; i < dim + nb_new_dims_ppl; i++)
160 map[i - nb_new_dims_ppl] = i;
162 ppl_Polyhedron_map_space_dimensions (ph, map, dim + nb_new_dims);
163 free (map);
166 /* Based on the original polyhedron PH, returns a new polyhedron with
167 an extra dimension placed at position LOOP + 1 that slices the
168 dimension LOOP into strips of size STRIDE. */
170 ppl_Polyhedron_t
171 ppl_strip_loop (ppl_Polyhedron_t ph, ppl_dimension_type loop, int stride)
173 ppl_const_Constraint_System_t pcs;
174 ppl_Constraint_System_const_iterator_t cit, end;
175 ppl_const_Constraint_t cstr;
176 ppl_Linear_Expression_t expr;
177 int v;
178 ppl_dimension_type dim;
179 ppl_Polyhedron_t res;
180 ppl_Coefficient_t c;
181 mpz_t val;
183 mpz_init (val);
184 ppl_new_Coefficient (&c);
186 ppl_Polyhedron_space_dimension (ph, &dim);
187 ppl_Polyhedron_get_constraints (ph, &pcs);
189 /* Start from a copy of the constraints. */
190 ppl_new_C_Polyhedron_from_space_dimension (&res, dim + 1, 0);
191 ppl_Polyhedron_add_constraints (res, pcs);
193 /* Add an empty dimension for the strip loop. */
194 ppl_insert_dimensions (res, loop, 1);
196 /* Identify the constraints that define the lower and upper bounds
197 of the strip-mined loop, and add them to the strip loop. */
199 ppl_Polyhedron_t tmp;
201 ppl_new_C_Polyhedron_from_space_dimension (&tmp, dim + 1, 0);
202 ppl_new_Constraint_System_const_iterator (&cit);
203 ppl_new_Constraint_System_const_iterator (&end);
205 for (ppl_Constraint_System_begin (pcs, cit),
206 ppl_Constraint_System_end (pcs, end);
207 !ppl_Constraint_System_const_iterator_equal_test (cit, end);
208 ppl_Constraint_System_const_iterator_increment (cit))
210 ppl_Constraint_System_const_iterator_dereference (cit, &cstr);
211 ppl_new_Linear_Expression_from_Constraint (&expr, cstr);
212 ppl_Linear_Expression_coefficient (expr, loop, c);
213 ppl_delete_Linear_Expression (expr);
214 ppl_Coefficient_to_mpz_t (c, val);
215 v = mpz_get_si (val);
217 if (0 < v || v < 0)
218 ppl_Polyhedron_add_constraint (tmp, cstr);
220 ppl_delete_Constraint_System_const_iterator (cit);
221 ppl_delete_Constraint_System_const_iterator (end);
223 ppl_insert_dimensions (tmp, loop + 1, 1);
224 ppl_Polyhedron_get_constraints (tmp, &pcs);
225 ppl_Polyhedron_add_constraints (res, pcs);
226 ppl_delete_Polyhedron (tmp);
229 /* Lower bound of a tile starts at "stride * outer_iv". */
231 ppl_Constraint_t new_cstr;
232 ppl_new_Linear_Expression_with_dimension (&expr, dim + 1);
234 ppl_set_coef (expr, loop + 1, 1);
235 ppl_set_coef (expr, loop, -1 * stride);
237 ppl_new_Constraint (&new_cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
238 ppl_delete_Linear_Expression (expr);
239 ppl_Polyhedron_add_constraint (res, new_cstr);
240 ppl_delete_Constraint (new_cstr);
243 /* Upper bound of a tile stops at "stride * outer_iv + stride - 1",
244 or at the old upper bound that is not modified. */
246 ppl_Constraint_t new_cstr;
247 ppl_new_Linear_Expression_with_dimension (&expr, dim + 1);
249 ppl_set_coef (expr, loop + 1, -1);
250 ppl_set_coef (expr, loop, stride);
251 ppl_set_inhomogeneous (expr, stride - 1);
253 ppl_new_Constraint (&new_cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
254 ppl_delete_Linear_Expression (expr);
255 ppl_Polyhedron_add_constraint (res, new_cstr);
256 ppl_delete_Constraint (new_cstr);
259 mpz_clear (val);
260 ppl_delete_Coefficient (c);
261 return res;
264 /* Lexicographically compares two linear expressions A and B and
265 returns negative when A < B, 0 when A == B and positive when A > B. */
268 ppl_lexico_compare_linear_expressions (ppl_Linear_Expression_t a,
269 ppl_Linear_Expression_t b)
271 ppl_dimension_type min_length, length1, length2;
272 ppl_dimension_type i;
273 ppl_Coefficient_t c;
274 int res;
275 mpz_t va, vb;
277 ppl_Linear_Expression_space_dimension (a, &length1);
278 ppl_Linear_Expression_space_dimension (b, &length2);
279 ppl_new_Coefficient (&c);
280 mpz_init (va);
281 mpz_init (vb);
283 if (length1 < length2)
284 min_length = length1;
285 else
286 min_length = length2;
288 for (i = 0; i < min_length; i++)
290 ppl_Linear_Expression_coefficient (a, i, c);
291 ppl_Coefficient_to_mpz_t (c, va);
292 ppl_Linear_Expression_coefficient (b, i, c);
293 ppl_Coefficient_to_mpz_t (c, vb);
294 res = mpz_cmp (va, vb);
296 if (res == 0)
297 continue;
299 mpz_clear (va);
300 mpz_clear (vb);
301 ppl_delete_Coefficient (c);
302 return res;
305 mpz_clear (va);
306 mpz_clear (vb);
307 ppl_delete_Coefficient (c);
308 return length1 - length2;
311 /* Print to FILE the polyhedron PH under its PolyLib matrix form. */
313 void
314 ppl_print_polyhedron_matrix (FILE *file, ppl_const_Polyhedron_t ph)
316 CloogMatrix *mat = new_Cloog_Matrix_from_ppl_Polyhedron (ph);
317 cloog_matrix_print (file, mat);
318 cloog_matrix_free (mat);
321 /* Print to FILE the linear expression LE. */
323 void
324 ppl_print_linear_expr (FILE *file, ppl_Linear_Expression_t le)
326 ppl_Constraint_t c;
327 ppl_Polyhedron_t pol;
328 ppl_dimension_type dim;
330 ppl_Linear_Expression_space_dimension (le, &dim);
331 ppl_new_C_Polyhedron_from_space_dimension (&pol, dim, 0);
332 ppl_new_Constraint (&c, le, PPL_CONSTRAINT_TYPE_EQUAL);
333 ppl_Polyhedron_add_constraint (pol, c);
334 ppl_print_polyhedron_matrix (file, pol);
337 /* Print to STDERR the linear expression LE. */
339 DEBUG_FUNCTION void
340 debug_ppl_linear_expr (ppl_Linear_Expression_t le)
342 ppl_print_linear_expr (stderr, le);
345 /* Print to FILE the powerset PS in its PolyLib matrix form. */
347 void
348 ppl_print_powerset_matrix (FILE *file,
349 ppl_Pointset_Powerset_C_Polyhedron_t ps)
351 size_t nb_disjuncts;
352 ppl_Pointset_Powerset_C_Polyhedron_iterator_t it, end;
354 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&it);
355 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&end);
357 ppl_Pointset_Powerset_C_Polyhedron_size (ps, &nb_disjuncts);
358 fprintf (file, "%d\n", (int) nb_disjuncts);
360 for (ppl_Pointset_Powerset_C_Polyhedron_iterator_begin (ps, it),
361 ppl_Pointset_Powerset_C_Polyhedron_iterator_end (ps, end);
362 !ppl_Pointset_Powerset_C_Polyhedron_iterator_equal_test (it, end);
363 ppl_Pointset_Powerset_C_Polyhedron_iterator_increment (it))
365 ppl_const_Polyhedron_t ph;
367 ppl_Pointset_Powerset_C_Polyhedron_iterator_dereference (it, &ph);
368 ppl_print_polyhedron_matrix (file, ph);
371 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (it);
372 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (end);
375 /* Print to STDERR the polyhedron PH under its PolyLib matrix form. */
377 DEBUG_FUNCTION void
378 debug_ppl_polyhedron_matrix (ppl_Polyhedron_t ph)
380 ppl_print_polyhedron_matrix (stderr, ph);
383 /* Print to STDERR the powerset PS in its PolyLib matrix form. */
385 DEBUG_FUNCTION void
386 debug_ppl_powerset_matrix (ppl_Pointset_Powerset_C_Polyhedron_t ps)
388 ppl_print_powerset_matrix (stderr, ps);
391 /* Read from FILE a polyhedron under PolyLib matrix form and return a
392 PPL polyhedron object. */
394 void
395 ppl_read_polyhedron_matrix (ppl_Polyhedron_t *ph, FILE *file)
397 CloogMatrix *mat = cloog_matrix_read (file);
398 new_C_Polyhedron_from_Cloog_Matrix (ph, mat);
399 cloog_matrix_free (mat);
402 /* Return in RES the maximum of the linear expression LE on the
403 pointset powerset of polyhedra PS. */
405 void
406 ppl_max_for_le_pointset (ppl_Pointset_Powerset_C_Polyhedron_t ps,
407 ppl_Linear_Expression_t le, mpz_t res)
409 ppl_Coefficient_t num, denom;
410 mpz_t dv, nv;
411 int maximum, err;
413 mpz_init (nv);
414 mpz_init (dv);
415 ppl_new_Coefficient (&num);
416 ppl_new_Coefficient (&denom);
417 err = ppl_Pointset_Powerset_C_Polyhedron_maximize (ps, le, num, denom, &maximum);
419 if (err > 0)
421 ppl_Coefficient_to_mpz_t (num, nv);
422 ppl_Coefficient_to_mpz_t (denom, dv);
423 gcc_assert (mpz_sgn (dv) != 0);
424 mpz_tdiv_q (res, nv, dv);
427 mpz_clear (nv);
428 mpz_clear (dv);
429 ppl_delete_Coefficient (num);
430 ppl_delete_Coefficient (denom);
433 /* Return in RES the maximum of the linear expression LE on the
434 polyhedron POL. */
436 void
437 ppl_min_for_le_pointset (ppl_Pointset_Powerset_C_Polyhedron_t ps,
438 ppl_Linear_Expression_t le, mpz_t res)
440 ppl_Coefficient_t num, denom;
441 mpz_t dv, nv;
442 int minimum, err;
444 mpz_init (nv);
445 mpz_init (dv);
446 ppl_new_Coefficient (&num);
447 ppl_new_Coefficient (&denom);
448 err = ppl_Pointset_Powerset_C_Polyhedron_minimize (ps, le, num, denom, &minimum);
450 if (err > 0)
452 ppl_Coefficient_to_mpz_t (num, nv);
453 ppl_Coefficient_to_mpz_t (denom, dv);
454 gcc_assert (mpz_sgn (dv) != 0);
455 mpz_tdiv_q (res, nv, dv);
458 mpz_clear (nv);
459 mpz_clear (dv);
460 ppl_delete_Coefficient (num);
461 ppl_delete_Coefficient (denom);
464 /* Builds a constraint in dimension DIM relating dimensions POS1 to
465 POS2 as "POS1 - POS2 + C CSTR_TYPE 0" */
467 ppl_Constraint_t
468 ppl_build_relation (int dim, int pos1, int pos2, int c,
469 enum ppl_enum_Constraint_Type cstr_type)
471 ppl_Linear_Expression_t expr;
472 ppl_Constraint_t cstr;
473 ppl_Coefficient_t coef;
474 mpz_t v, v_op, v_c;
476 mpz_init (v);
477 mpz_init (v_op);
478 mpz_init (v_c);
480 mpz_set_si (v, 1);
481 mpz_set_si (v_op, -1);
482 mpz_set_si (v_c, c);
484 ppl_new_Coefficient (&coef);
485 ppl_new_Linear_Expression_with_dimension (&expr, dim);
487 ppl_assign_Coefficient_from_mpz_t (coef, v);
488 ppl_Linear_Expression_add_to_coefficient (expr, pos1, coef);
489 ppl_assign_Coefficient_from_mpz_t (coef, v_op);
490 ppl_Linear_Expression_add_to_coefficient (expr, pos2, coef);
491 ppl_assign_Coefficient_from_mpz_t (coef, v_c);
492 ppl_Linear_Expression_add_to_inhomogeneous (expr, coef);
494 ppl_new_Constraint (&cstr, expr, cstr_type);
496 ppl_delete_Linear_Expression (expr);
497 ppl_delete_Coefficient (coef);
498 mpz_clear (v);
499 mpz_clear (v_op);
500 mpz_clear (v_c);
502 return cstr;
505 /* Print to STDERR the GMP value VAL. */
507 DEBUG_FUNCTION void
508 debug_gmp_value (mpz_t val)
510 char *str = mpz_get_str (0, 10, val);
511 void (*gmp_free) (void *, size_t);
513 fprintf (stderr, "%s", str);
514 mp_get_memory_functions (NULL, NULL, &gmp_free);
515 (*gmp_free) (str, strlen (str) + 1);
518 /* Checks for integer feasibility: returns true when the powerset
519 polyhedron PS has no integer solutions. */
521 bool
522 ppl_powerset_is_empty (ppl_Pointset_Powerset_C_Polyhedron_t ps)
524 ppl_PIP_Problem_t pip;
525 ppl_dimension_type d;
526 ppl_const_Constraint_System_t pcs;
527 ppl_Constraint_System_const_iterator_t first, last;
528 ppl_Pointset_Powerset_C_Polyhedron_iterator_t it, end;
529 bool has_integer_solutions = false;
531 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (ps))
532 return true;
534 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (ps, &d);
535 ppl_new_Constraint_System_const_iterator (&first);
536 ppl_new_Constraint_System_const_iterator (&last);
537 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&it);
538 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&end);
540 for (ppl_Pointset_Powerset_C_Polyhedron_iterator_begin (ps, it),
541 ppl_Pointset_Powerset_C_Polyhedron_iterator_end (ps, end);
542 !ppl_Pointset_Powerset_C_Polyhedron_iterator_equal_test (it, end);
543 ppl_Pointset_Powerset_C_Polyhedron_iterator_increment (it))
545 ppl_const_Polyhedron_t ph;
546 ppl_Pointset_Powerset_C_Polyhedron_iterator_dereference (it, &ph);
548 ppl_Polyhedron_get_constraints (ph, &pcs);
549 ppl_Constraint_System_begin (pcs, first);
550 ppl_Constraint_System_end (pcs, last);
552 ppl_new_PIP_Problem_from_constraints (&pip, d, first, last, 0, NULL);
553 has_integer_solutions |= ppl_PIP_Problem_is_satisfiable (pip);
555 ppl_delete_PIP_Problem (pip);
558 ppl_delete_Constraint_System_const_iterator (first);
559 ppl_delete_Constraint_System_const_iterator (last);
560 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (it);
561 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (end);
563 return !has_integer_solutions;
566 #endif