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)
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"
29 #include "graphite-cloog-util.h"
30 #include "graphite-ppl.h"
32 /* Set the inhomogeneous term of E to X. */
35 ppl_set_inhomogeneous_gmp (ppl_Linear_Expression_t e
, mpz_t x
)
42 ppl_new_Coefficient (&c
);
44 ppl_Linear_Expression_inhomogeneous_term (e
, c
);
45 ppl_Coefficient_to_mpz_t (c
, v1
);
49 ppl_assign_Coefficient_from_mpz_t (c
, v0
);
50 ppl_Linear_Expression_add_to_inhomogeneous (e
, c
);
54 ppl_delete_Coefficient (c
);
60 ppl_set_coef_gmp (ppl_Linear_Expression_t e
, ppl_dimension_type i
, mpz_t x
)
67 ppl_new_Coefficient (&c
);
69 ppl_Linear_Expression_coefficient (e
, i
, c
);
70 ppl_Coefficient_to_mpz_t (c
, v1
);
74 ppl_assign_Coefficient_from_mpz_t (c
, v0
);
75 ppl_Linear_Expression_add_to_coefficient (e
, i
, c
);
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
90 | d0 d1 d2 x0 x1 x2 x3 d3 d4
92 | map = {0, 1, 2, 7, 8, 3, 4, 5, 6}
96 ppl_insert_dimensions_pointset (ppl_Pointset_Powerset_C_Polyhedron_t ph
, int x
,
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
++)
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
);
124 /* Insert after X NB_NEW_DIMS empty dimensions into PH.
126 With x = 3 and nb_new_dims = 4
132 | d0 d1 d2 x0 x1 x2 x3 d3 d4
134 | map = {0, 1, 2, 7, 8, 3, 4, 5, 6}
138 ppl_insert_dimensions (ppl_Polyhedron_t ph
, int x
,
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
++)
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
);
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. */
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
;
178 ppl_dimension_type dim
;
179 ppl_Polyhedron_t res
;
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
);
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
);
260 ppl_delete_Coefficient (c
);
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
;
277 ppl_Linear_Expression_space_dimension (a
, &length1
);
278 ppl_Linear_Expression_space_dimension (b
, &length2
);
279 ppl_new_Coefficient (&c
);
283 if (length1
< length2
)
284 min_length
= length1
;
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
);
301 ppl_delete_Coefficient (c
);
307 ppl_delete_Coefficient (c
);
308 return length1
- length2
;
311 /* Print to FILE the polyhedron PH under its PolyLib matrix form. */
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. */
324 ppl_print_linear_expr (FILE *file
, ppl_Linear_Expression_t le
)
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. */
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. */
348 ppl_print_powerset_matrix (FILE *file
,
349 ppl_Pointset_Powerset_C_Polyhedron_t ps
)
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. */
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. */
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. */
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. */
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
;
415 ppl_new_Coefficient (&num
);
416 ppl_new_Coefficient (&denom
);
417 err
= ppl_Pointset_Powerset_C_Polyhedron_maximize (ps
, le
, num
, denom
, &maximum
);
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
);
429 ppl_delete_Coefficient (num
);
430 ppl_delete_Coefficient (denom
);
433 /* Return in RES the maximum of the linear expression LE on the
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
;
446 ppl_new_Coefficient (&num
);
447 ppl_new_Coefficient (&denom
);
448 err
= ppl_Pointset_Powerset_C_Polyhedron_minimize (ps
, le
, num
, denom
, &minimum
);
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
);
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" */
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
;
481 mpz_set_si (v_op
, -1);
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
);