2009-09-03 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / graphite-ppl.c
blob3e84dcf9039d29aa0f2ce20ab25807db32b9787f
1 /* Gimple Represented as Polyhedra.
2 Copyright (C) 2009 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"
25 #include "tm.h"
26 #include "ggc.h"
28 #ifdef HAVE_cloog
29 #include "ppl_c.h"
30 #include "cloog/cloog.h"
31 #include "graphite-ppl.h"
33 /* Translates row ROW of the CloogMatrix MATRIX to a PPL Constraint. */
35 static ppl_Constraint_t
36 cloog_matrix_to_ppl_constraint (CloogMatrix *matrix, int row)
38 int j;
39 ppl_Constraint_t cstr;
40 ppl_Coefficient_t coef;
41 ppl_Linear_Expression_t expr;
42 ppl_dimension_type dim = matrix->NbColumns - 2;
44 ppl_new_Coefficient (&coef);
45 ppl_new_Linear_Expression_with_dimension (&expr, dim);
47 for (j = 1; j < matrix->NbColumns - 1; j++)
49 ppl_assign_Coefficient_from_mpz_t (coef, matrix->p[row][j]);
50 ppl_Linear_Expression_add_to_coefficient (expr, j - 1, coef);
53 ppl_assign_Coefficient_from_mpz_t (coef,
54 matrix->p[row][matrix->NbColumns - 1]);
55 ppl_Linear_Expression_add_to_inhomogeneous (expr, coef);
56 ppl_delete_Coefficient (coef);
58 if (value_zero_p (matrix->p[row][0]))
59 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
60 else
61 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
63 ppl_delete_Linear_Expression (expr);
64 return cstr;
67 /* Creates a PPL constraint system from MATRIX. */
69 static void
70 new_Constraint_System_from_Cloog_Matrix (ppl_Constraint_System_t *pcs,
71 CloogMatrix *matrix)
73 int i;
75 ppl_new_Constraint_System (pcs);
77 for (i = 0; i < matrix->NbRows; i++)
79 ppl_Constraint_t c = cloog_matrix_to_ppl_constraint (matrix, i);
80 ppl_Constraint_System_insert_Constraint (*pcs, c);
81 ppl_delete_Constraint (c);
85 /* Creates a PPL Polyhedron from MATRIX. */
87 void
88 new_C_Polyhedron_from_Cloog_Matrix (ppl_Polyhedron_t *ph,
89 CloogMatrix *matrix)
91 ppl_Constraint_System_t cs;
92 new_Constraint_System_from_Cloog_Matrix (&cs, matrix);
93 ppl_new_C_Polyhedron_recycle_Constraint_System (ph, cs);
96 /* Counts the number of constraints in PCS. */
98 static int
99 ppl_Constrain_System_number_of_constraints (ppl_const_Constraint_System_t pcs)
101 ppl_Constraint_System_const_iterator_t cit, end;
102 int num = 0;
104 ppl_new_Constraint_System_const_iterator (&cit);
105 ppl_new_Constraint_System_const_iterator (&end);
107 for (ppl_Constraint_System_begin (pcs, cit),
108 ppl_Constraint_System_end (pcs, end);
109 !ppl_Constraint_System_const_iterator_equal_test (cit, end);
110 ppl_Constraint_System_const_iterator_increment (cit))
111 num++;
113 ppl_delete_Constraint_System_const_iterator (cit);
114 ppl_delete_Constraint_System_const_iterator (end);
115 return num;
118 static void
119 oppose_constraint (CloogMatrix *m, int row)
121 int k;
123 /* Do not oppose the first column: it is the eq/ineq one. */
124 for (k = 1; k < m->NbColumns; k++)
125 value_oppose (m->p[row][k], m->p[row][k]);
128 /* Inserts constraint CSTR at row ROW of matrix M. */
130 void
131 insert_constraint_into_matrix (CloogMatrix *m, int row,
132 ppl_const_Constraint_t cstr)
134 ppl_Coefficient_t c;
135 ppl_dimension_type i, dim, nb_cols = m->NbColumns;
137 ppl_Constraint_space_dimension (cstr, &dim);
138 ppl_new_Coefficient (&c);
140 for (i = 0; i < dim; i++)
142 ppl_Constraint_coefficient (cstr, i, c);
143 ppl_Coefficient_to_mpz_t (c, m->p[row][i + 1]);
146 for (i = dim; i < nb_cols - 1; i++)
147 value_set_si (m->p[row][i + 1], 0);
149 ppl_Constraint_inhomogeneous_term (cstr, c);
150 ppl_Coefficient_to_mpz_t (c, m->p[row][nb_cols - 1]);
151 value_set_si (m->p[row][0], 1);
153 switch (ppl_Constraint_type (cstr))
155 case PPL_CONSTRAINT_TYPE_LESS_THAN:
156 oppose_constraint (m, row);
157 case PPL_CONSTRAINT_TYPE_GREATER_THAN:
158 value_sub_int (m->p[row][nb_cols - 1],
159 m->p[row][nb_cols - 1], 1);
160 break;
162 case PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL:
163 oppose_constraint (m, row);
164 case PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL:
165 break;
167 case PPL_CONSTRAINT_TYPE_EQUAL:
168 value_set_si (m->p[row][0], 0);
169 break;
171 default:
172 /* Not yet implemented. */
173 gcc_unreachable();
176 ppl_delete_Coefficient (c);
179 /* Creates a CloogMatrix from constraint system PCS. */
181 static CloogMatrix *
182 new_Cloog_Matrix_from_ppl_Constraint_System (ppl_const_Constraint_System_t pcs)
184 CloogMatrix *matrix;
185 ppl_Constraint_System_const_iterator_t cit, end;
186 ppl_dimension_type dim;
187 int rows;
188 int row = 0;
190 rows = ppl_Constrain_System_number_of_constraints (pcs);
191 ppl_Constraint_System_space_dimension (pcs, &dim);
192 matrix = cloog_matrix_alloc (rows, dim + 2);
193 ppl_new_Constraint_System_const_iterator (&cit);
194 ppl_new_Constraint_System_const_iterator (&end);
196 for (ppl_Constraint_System_begin (pcs, cit),
197 ppl_Constraint_System_end (pcs, end);
198 !ppl_Constraint_System_const_iterator_equal_test (cit, end);
199 ppl_Constraint_System_const_iterator_increment (cit))
201 ppl_const_Constraint_t c;
202 ppl_Constraint_System_const_iterator_dereference (cit, &c);
203 insert_constraint_into_matrix (matrix, row, c);
204 row++;
207 ppl_delete_Constraint_System_const_iterator (cit);
208 ppl_delete_Constraint_System_const_iterator (end);
210 return matrix;
213 /* Creates a CloogMatrix from polyhedron PH. */
215 CloogMatrix *
216 new_Cloog_Matrix_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph)
218 ppl_const_Constraint_System_t pcs;
219 CloogMatrix *res;
221 ppl_Polyhedron_get_constraints (ph, &pcs);
222 res = new_Cloog_Matrix_from_ppl_Constraint_System (pcs);
224 return res;
227 /* Creates a CloogDomain from polyhedron PH. */
229 CloogDomain *
230 new_Cloog_Domain_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph)
232 CloogMatrix *mat = new_Cloog_Matrix_from_ppl_Polyhedron (ph);
233 CloogDomain *res = cloog_domain_matrix2domain (mat);
234 cloog_matrix_free (mat);
235 return res;
238 /* Creates a CloogDomain from a pointset powerset PS. */
240 CloogDomain *
241 new_Cloog_Domain_from_ppl_Pointset_Powerset (
242 ppl_Pointset_Powerset_C_Polyhedron_t ps)
244 CloogDomain *res = NULL;
245 ppl_Pointset_Powerset_C_Polyhedron_iterator_t it, end;
247 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&it);
248 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&end);
250 for (ppl_Pointset_Powerset_C_Polyhedron_iterator_begin (ps, it),
251 ppl_Pointset_Powerset_C_Polyhedron_iterator_end (ps, end);
252 !ppl_Pointset_Powerset_C_Polyhedron_iterator_equal_test (it, end);
253 ppl_Pointset_Powerset_C_Polyhedron_iterator_increment (it))
255 ppl_const_Polyhedron_t ph;
256 CloogDomain *tmp;
258 ppl_Pointset_Powerset_C_Polyhedron_iterator_dereference (it, &ph);
259 tmp = new_Cloog_Domain_from_ppl_Polyhedron (ph);
261 if (res == NULL)
262 res = tmp;
263 else
264 res = cloog_domain_union (res, tmp);
267 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (it);
268 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (end);
270 gcc_assert (res != NULL);
272 return res;
275 /* Set the inhomogeneous term of E to X. */
277 void
278 ppl_set_inhomogeneous_gmp (ppl_Linear_Expression_t e, Value x)
280 Value v0, v1;
281 ppl_Coefficient_t c;
283 value_init (v0);
284 value_init (v1);
285 ppl_new_Coefficient (&c);
287 ppl_Linear_Expression_inhomogeneous_term (e, c);
288 ppl_Coefficient_to_mpz_t (c, v1);
289 value_oppose (v1, v1);
290 value_assign (v0, x);
291 value_addto (v0, v0, v1);
292 ppl_assign_Coefficient_from_mpz_t (c, v0);
293 ppl_Linear_Expression_add_to_inhomogeneous (e, c);
295 value_clear (v0);
296 value_clear (v1);
297 ppl_delete_Coefficient (c);
300 /* Set E[I] to X. */
302 void
303 ppl_set_coef_gmp (ppl_Linear_Expression_t e, ppl_dimension_type i, Value x)
305 Value v0, v1;
306 ppl_Coefficient_t c;
308 value_init (v0);
309 value_init (v1);
310 ppl_new_Coefficient (&c);
312 ppl_Linear_Expression_coefficient (e, i, c);
313 ppl_Coefficient_to_mpz_t (c, v1);
314 value_oppose (v1, v1);
315 value_assign (v0, x);
316 value_addto (v0, v0, v1);
317 ppl_assign_Coefficient_from_mpz_t (c, v0);
318 ppl_Linear_Expression_add_to_coefficient (e, i, c);
320 value_clear (v0);
321 value_clear (v1);
322 ppl_delete_Coefficient (c);
325 /* Insert after X NB_NEW_DIMS empty dimensions into PH.
327 With x = 3 and nb_new_dims = 4
329 | d0 d1 d2 d3 d4
331 is transformed to
333 | d0 d1 d2 x0 x1 x2 x3 d3 d4
335 | map = {0, 1, 2, 7, 8, 3, 4, 5, 6}
338 void
339 ppl_insert_dimensions_pointset (ppl_Pointset_Powerset_C_Polyhedron_t ph, int x,
340 int nb_new_dims)
342 ppl_dimension_type i, dim;
343 ppl_dimension_type *map;
344 ppl_dimension_type x_ppl, nb_new_dims_ppl;
346 x_ppl = (ppl_dimension_type) x;
347 nb_new_dims_ppl = (ppl_dimension_type) nb_new_dims;
349 ppl_Pointset_Powerset_C_Polyhedron_space_dimension (ph, &dim);
350 ppl_Pointset_Powerset_C_Polyhedron_add_space_dimensions_and_embed (ph, nb_new_dims);
352 map = (ppl_dimension_type *) XNEWVEC (ppl_dimension_type, dim + nb_new_dims);
354 for (i = 0; i < x_ppl; i++)
355 map[i] = i;
357 for (i = x_ppl; i < x_ppl + nb_new_dims_ppl; i++)
358 map[dim + i - x_ppl] = i;
360 for (i = x_ppl + nb_new_dims_ppl; i < dim + nb_new_dims_ppl; i++)
361 map[i - nb_new_dims_ppl] = i;
363 ppl_Pointset_Powerset_C_Polyhedron_map_space_dimensions (ph, map, dim + nb_new_dims);
364 free (map);
367 /* Insert after X NB_NEW_DIMS empty dimensions into PH.
369 With x = 3 and nb_new_dims = 4
371 | d0 d1 d2 d3 d4
373 is transformed to
375 | d0 d1 d2 x0 x1 x2 x3 d3 d4
377 | map = {0, 1, 2, 7, 8, 3, 4, 5, 6}
380 void
381 ppl_insert_dimensions (ppl_Polyhedron_t ph, int x,
382 int nb_new_dims)
384 ppl_dimension_type i, dim;
385 ppl_dimension_type *map;
386 ppl_dimension_type x_ppl, nb_new_dims_ppl;
388 x_ppl = (ppl_dimension_type) x;
389 nb_new_dims_ppl = (ppl_dimension_type) nb_new_dims;
391 ppl_Polyhedron_space_dimension (ph, &dim);
392 ppl_Polyhedron_add_space_dimensions_and_embed (ph, nb_new_dims);
394 map = (ppl_dimension_type *) XNEWVEC (ppl_dimension_type, dim + nb_new_dims);
396 for (i = 0; i < x_ppl; i++)
397 map[i] = i;
399 for (i = x_ppl; i < x_ppl + nb_new_dims_ppl; i++)
400 map[dim + i - x_ppl] = i;
402 for (i = x_ppl + nb_new_dims_ppl; i < dim + nb_new_dims_ppl; i++)
403 map[i - nb_new_dims_ppl] = i;
405 ppl_Polyhedron_map_space_dimensions (ph, map, dim + nb_new_dims);
406 free (map);
409 /* Based on the original polyhedron PH, returns a new polyhedron with
410 an extra dimension placed at position LOOP + 1 that slices the
411 dimension LOOP into strips of size STRIDE. */
413 ppl_Polyhedron_t
414 ppl_strip_loop (ppl_Polyhedron_t ph, ppl_dimension_type loop, int stride)
416 ppl_const_Constraint_System_t pcs;
417 ppl_Constraint_System_const_iterator_t cit, end;
418 ppl_const_Constraint_t cstr;
419 ppl_Linear_Expression_t expr;
420 int v;
421 ppl_dimension_type dim;
422 ppl_Polyhedron_t res;
423 ppl_Coefficient_t c;
424 Value val;
426 value_init (val);
427 ppl_new_Coefficient (&c);
429 ppl_Polyhedron_space_dimension (ph, &dim);
430 ppl_Polyhedron_get_constraints (ph, &pcs);
432 /* Start from a copy of the constraints. */
433 ppl_new_C_Polyhedron_from_space_dimension (&res, dim + 1, 0);
434 ppl_Polyhedron_add_constraints (res, pcs);
436 /* Add an empty dimension for the strip loop. */
437 ppl_insert_dimensions (res, loop, 1);
439 /* Identify the constraints that define the lower and upper bounds
440 of the strip-mined loop, and add them to the strip loop. */
442 ppl_Polyhedron_t tmp;
444 ppl_new_C_Polyhedron_from_space_dimension (&tmp, dim + 1, 0);
445 ppl_new_Constraint_System_const_iterator (&cit);
446 ppl_new_Constraint_System_const_iterator (&end);
448 for (ppl_Constraint_System_begin (pcs, cit),
449 ppl_Constraint_System_end (pcs, end);
450 !ppl_Constraint_System_const_iterator_equal_test (cit, end);
451 ppl_Constraint_System_const_iterator_increment (cit))
453 ppl_Constraint_System_const_iterator_dereference (cit, &cstr);
454 ppl_new_Linear_Expression_from_Constraint (&expr, cstr);
455 ppl_Linear_Expression_coefficient (expr, loop, c);
456 ppl_delete_Linear_Expression (expr);
457 ppl_Coefficient_to_mpz_t (c, val);
458 v = value_get_si (val);
460 if (0 < v || v < 0)
461 ppl_Polyhedron_add_constraint (tmp, cstr);
463 ppl_delete_Constraint_System_const_iterator (cit);
464 ppl_delete_Constraint_System_const_iterator (end);
466 ppl_insert_dimensions (tmp, loop + 1, 1);
467 ppl_Polyhedron_get_constraints (tmp, &pcs);
468 ppl_Polyhedron_add_constraints (res, pcs);
469 ppl_delete_Polyhedron (tmp);
472 /* Lower bound of a tile starts at "stride * outer_iv". */
474 ppl_Constraint_t new_cstr;
475 ppl_new_Linear_Expression_with_dimension (&expr, dim + 1);
477 ppl_set_coef (expr, loop + 1, 1);
478 ppl_set_coef (expr, loop, -1 * stride);
480 ppl_new_Constraint (&new_cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
481 ppl_delete_Linear_Expression (expr);
482 ppl_Polyhedron_add_constraint (res, new_cstr);
483 ppl_delete_Constraint (new_cstr);
486 /* Upper bound of a tile stops at "stride * outer_iv + stride - 1",
487 or at the old upper bound that is not modified. */
489 ppl_Constraint_t new_cstr;
490 ppl_new_Linear_Expression_with_dimension (&expr, dim + 1);
492 ppl_set_coef (expr, loop + 1, -1);
493 ppl_set_coef (expr, loop, stride);
494 ppl_set_inhomogeneous (expr, stride - 1);
496 ppl_new_Constraint (&new_cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
497 ppl_delete_Linear_Expression (expr);
498 ppl_Polyhedron_add_constraint (res, new_cstr);
499 ppl_delete_Constraint (new_cstr);
502 value_clear (val);
503 ppl_delete_Coefficient (c);
504 return res;
507 /* Lexicographically compares two linear expressions A and B and
508 returns negative when A < B, 0 when A == B and positive when A > B. */
511 ppl_lexico_compare_linear_expressions (ppl_Linear_Expression_t a,
512 ppl_Linear_Expression_t b)
514 ppl_dimension_type min_length, length1, length2;
515 ppl_dimension_type i;
516 ppl_Coefficient_t c;
517 int res;
518 Value va, vb;
520 ppl_Linear_Expression_space_dimension (a, &length1);
521 ppl_Linear_Expression_space_dimension (b, &length2);
522 ppl_new_Coefficient (&c);
523 value_init (va);
524 value_init (vb);
526 if (length1 < length2)
527 min_length = length1;
528 else
529 min_length = length2;
531 for (i = 0; i < min_length; i++)
533 ppl_Linear_Expression_coefficient (a, i, c);
534 ppl_Coefficient_to_mpz_t (c, va);
535 ppl_Linear_Expression_coefficient (b, i, c);
536 ppl_Coefficient_to_mpz_t (c, vb);
537 res = value_compare (va, vb);
539 if (res == 0)
540 continue;
542 value_clear (va);
543 value_clear (vb);
544 ppl_delete_Coefficient (c);
545 return res;
548 value_clear (va);
549 value_clear (vb);
550 ppl_delete_Coefficient (c);
551 return length1 - length2;
554 /* Print to FILE the polyhedron PH under its PolyLib matrix form. */
556 void
557 ppl_print_polyhedron_matrix (FILE *file, ppl_const_Polyhedron_t ph)
559 CloogMatrix *mat = new_Cloog_Matrix_from_ppl_Polyhedron (ph);
560 cloog_matrix_print (file, mat);
561 cloog_matrix_free (mat);
564 /* Print to FILE the powerset PS in its PolyLib matrix form. */
566 void
567 ppl_print_powerset_matrix (FILE *file,
568 ppl_Pointset_Powerset_C_Polyhedron_t ps)
570 ppl_Pointset_Powerset_C_Polyhedron_iterator_t it, end;
572 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&it);
573 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&end);
575 for (ppl_Pointset_Powerset_C_Polyhedron_iterator_begin (ps, it),
576 ppl_Pointset_Powerset_C_Polyhedron_iterator_end (ps, end);
577 !ppl_Pointset_Powerset_C_Polyhedron_iterator_equal_test (it, end);
578 ppl_Pointset_Powerset_C_Polyhedron_iterator_increment (it))
580 ppl_const_Polyhedron_t ph;
582 ppl_Pointset_Powerset_C_Polyhedron_iterator_dereference (it, &ph);
583 ppl_print_polyhedron_matrix (file, ph);
586 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (it);
587 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (end);
590 /* Print to STDERR the polyhedron PH under its PolyLib matrix form. */
592 void
593 debug_ppl_polyhedron_matrix (ppl_Polyhedron_t ph)
595 ppl_print_polyhedron_matrix (stderr, ph);
598 /* Print to STDERR the powerset PS in its PolyLib matrix form. */
600 void
601 debug_ppl_powerset_matrix (ppl_Pointset_Powerset_C_Polyhedron_t ps)
603 ppl_print_powerset_matrix (stderr, ps);
606 /* Read from FILE a polyhedron under PolyLib matrix form and return a
607 PPL polyhedron object. */
609 void
610 ppl_read_polyhedron_matrix (ppl_Polyhedron_t *ph, FILE *file)
612 CloogMatrix *mat = cloog_matrix_read (file);
613 new_C_Polyhedron_from_Cloog_Matrix (ph, mat);
614 cloog_matrix_free (mat);
617 /* Return in RES the maximum of the linear expression LE on the
618 pointset powerset of polyhedra PS. */
620 void
621 ppl_max_for_le_pointset (ppl_Pointset_Powerset_C_Polyhedron_t ps,
622 ppl_Linear_Expression_t le, Value res)
624 ppl_Coefficient_t num, denom;
625 Value dv, nv;
626 int maximum, err;
628 value_init (nv);
629 value_init (dv);
630 ppl_new_Coefficient (&num);
631 ppl_new_Coefficient (&denom);
632 err = ppl_Pointset_Powerset_C_Polyhedron_maximize (ps, le, num, denom, &maximum);
634 if (err > 0)
636 ppl_Coefficient_to_mpz_t (num, nv);
637 ppl_Coefficient_to_mpz_t (denom, dv);
638 gcc_assert (value_notzero_p (dv));
639 value_division (res, nv, dv);
642 value_clear (nv);
643 value_clear (dv);
644 ppl_delete_Coefficient (num);
645 ppl_delete_Coefficient (denom);
648 #endif