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)
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/>. */
24 #include "coretypes.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
)
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
);
61 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
63 ppl_delete_Linear_Expression (expr
);
67 /* Creates a PPL constraint system from MATRIX. */
70 new_Constraint_System_from_Cloog_Matrix (ppl_Constraint_System_t
*pcs
,
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. */
88 new_C_Polyhedron_from_Cloog_Matrix (ppl_Polyhedron_t
*ph
,
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. */
99 ppl_Constrain_System_number_of_constraints (ppl_const_Constraint_System_t pcs
)
101 ppl_Constraint_System_const_iterator_t cit
, end
;
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
))
113 ppl_delete_Constraint_System_const_iterator (cit
);
114 ppl_delete_Constraint_System_const_iterator (end
);
119 oppose_constraint (CloogMatrix
*m
, int row
)
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. */
131 insert_constraint_into_matrix (CloogMatrix
*m
, int row
,
132 ppl_const_Constraint_t cstr
)
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);
162 case PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
:
163 oppose_constraint (m
, row
);
164 case PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
:
167 case PPL_CONSTRAINT_TYPE_EQUAL
:
168 value_set_si (m
->p
[row
][0], 0);
172 /* Not yet implemented. */
176 ppl_delete_Coefficient (c
);
179 /* Creates a CloogMatrix from constraint system PCS. */
182 new_Cloog_Matrix_from_ppl_Constraint_System (ppl_const_Constraint_System_t pcs
)
185 ppl_Constraint_System_const_iterator_t cit
, end
;
186 ppl_dimension_type dim
;
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
);
207 ppl_delete_Constraint_System_const_iterator (cit
);
208 ppl_delete_Constraint_System_const_iterator (end
);
213 /* Creates a CloogMatrix from polyhedron PH. */
216 new_Cloog_Matrix_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph
)
218 ppl_const_Constraint_System_t pcs
;
221 ppl_Polyhedron_get_constraints (ph
, &pcs
);
222 res
= new_Cloog_Matrix_from_ppl_Constraint_System (pcs
);
227 /* Creates a CloogDomain from polyhedron PH. */
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
);
238 /* Creates a CloogDomain from a pointset powerset PS. */
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
;
258 ppl_Pointset_Powerset_C_Polyhedron_iterator_dereference (it
, &ph
);
259 tmp
= new_Cloog_Domain_from_ppl_Polyhedron (ph
);
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
);
275 /* Set the inhomogeneous term of E to X. */
278 ppl_set_inhomogeneous_gmp (ppl_Linear_Expression_t e
, Value x
)
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
);
297 ppl_delete_Coefficient (c
);
303 ppl_set_coef_gmp (ppl_Linear_Expression_t e
, ppl_dimension_type i
, Value x
)
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
);
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
333 | d0 d1 d2 x0 x1 x2 x3 d3 d4
335 | map = {0, 1, 2, 7, 8, 3, 4, 5, 6}
339 ppl_insert_dimensions_pointset (ppl_Pointset_Powerset_C_Polyhedron_t ph
, int x
,
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
++)
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
);
367 /* Insert after X NB_NEW_DIMS empty dimensions into PH.
369 With x = 3 and nb_new_dims = 4
375 | d0 d1 d2 x0 x1 x2 x3 d3 d4
377 | map = {0, 1, 2, 7, 8, 3, 4, 5, 6}
381 ppl_insert_dimensions (ppl_Polyhedron_t ph
, int x
,
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
++)
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
);
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. */
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
;
421 ppl_dimension_type dim
;
422 ppl_Polyhedron_t res
;
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
);
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
);
503 ppl_delete_Coefficient (c
);
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
;
520 ppl_Linear_Expression_space_dimension (a
, &length1
);
521 ppl_Linear_Expression_space_dimension (b
, &length2
);
522 ppl_new_Coefficient (&c
);
526 if (length1
< length2
)
527 min_length
= length1
;
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
);
544 ppl_delete_Coefficient (c
);
550 ppl_delete_Coefficient (c
);
551 return length1
- length2
;
554 /* Print to FILE the polyhedron PH under its PolyLib matrix form. */
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. */
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. */
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. */
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. */
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. */
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
;
630 ppl_new_Coefficient (&num
);
631 ppl_new_Coefficient (&denom
);
632 err
= ppl_Pointset_Powerset_C_Polyhedron_maximize (ps
, le
, num
, denom
, &maximum
);
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
);
644 ppl_delete_Coefficient (num
);
645 ppl_delete_Coefficient (denom
);