1 /* Gimple Represented as Polyhedra.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@inria.fr>
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"
31 #include "cloog/cloog.h"
32 #include "graphite-cloog-util.h"
33 #include "graphite-cloog-compat.h"
35 /* Counts the number of constraints in PCS. */
38 ppl_Constrain_System_number_of_constraints (ppl_const_Constraint_System_t pcs
)
40 ppl_Constraint_System_const_iterator_t cit
, end
;
43 ppl_new_Constraint_System_const_iterator (&cit
);
44 ppl_new_Constraint_System_const_iterator (&end
);
46 for (ppl_Constraint_System_begin (pcs
, cit
),
47 ppl_Constraint_System_end (pcs
, end
);
48 !ppl_Constraint_System_const_iterator_equal_test (cit
, end
);
49 ppl_Constraint_System_const_iterator_increment (cit
))
52 ppl_delete_Constraint_System_const_iterator (cit
);
53 ppl_delete_Constraint_System_const_iterator (end
);
58 oppose_constraint (CloogMatrix
*m
, int row
)
62 /* Do not oppose the first column: it is the eq/ineq one. */
63 /* Cast needed to remove warning that is generated as CLooG isl
64 is using an unsigned int for NbColumns and CLooG PPL is
65 using a signed int for NBColumns. */
66 for (k
= 1; k
< (int)m
->NbColumns
; k
++)
67 mpz_neg (m
->p
[row
][k
], m
->p
[row
][k
]);
70 /* Inserts constraint CSTR at row ROW of matrix M. */
73 insert_constraint_into_matrix (CloogMatrix
*m
, int row
,
74 ppl_const_Constraint_t cstr
)
77 ppl_dimension_type i
, dim
, nb_cols
= m
->NbColumns
;
79 ppl_Constraint_space_dimension (cstr
, &dim
);
80 ppl_new_Coefficient (&c
);
82 for (i
= 0; i
< dim
; i
++)
84 ppl_Constraint_coefficient (cstr
, i
, c
);
85 ppl_Coefficient_to_mpz_t (c
, m
->p
[row
][i
+ 1]);
88 for (i
= dim
; i
< nb_cols
- 1; i
++)
89 mpz_set_si (m
->p
[row
][i
+ 1], 0);
91 ppl_Constraint_inhomogeneous_term (cstr
, c
);
92 ppl_Coefficient_to_mpz_t (c
, m
->p
[row
][nb_cols
- 1]);
93 mpz_set_si (m
->p
[row
][0], 1);
95 switch (ppl_Constraint_type (cstr
))
97 case PPL_CONSTRAINT_TYPE_LESS_THAN
:
98 oppose_constraint (m
, row
);
99 case PPL_CONSTRAINT_TYPE_GREATER_THAN
:
100 mpz_sub_ui (m
->p
[row
][nb_cols
- 1],
101 m
->p
[row
][nb_cols
- 1], 1);
104 case PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
:
105 oppose_constraint (m
, row
);
106 case PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
:
109 case PPL_CONSTRAINT_TYPE_EQUAL
:
110 mpz_set_si (m
->p
[row
][0], 0);
114 /* Not yet implemented. */
118 ppl_delete_Coefficient (c
);
121 /* Creates a CloogMatrix from constraint system PCS. */
124 new_Cloog_Matrix_from_ppl_Constraint_System (ppl_const_Constraint_System_t pcs
)
127 ppl_Constraint_System_const_iterator_t cit
, end
;
128 ppl_dimension_type dim
;
132 rows
= ppl_Constrain_System_number_of_constraints (pcs
);
133 ppl_Constraint_System_space_dimension (pcs
, &dim
);
134 matrix
= cloog_matrix_alloc (rows
, dim
+ 2);
135 ppl_new_Constraint_System_const_iterator (&cit
);
136 ppl_new_Constraint_System_const_iterator (&end
);
138 for (ppl_Constraint_System_begin (pcs
, cit
),
139 ppl_Constraint_System_end (pcs
, end
);
140 !ppl_Constraint_System_const_iterator_equal_test (cit
, end
);
141 ppl_Constraint_System_const_iterator_increment (cit
))
143 ppl_const_Constraint_t c
;
144 ppl_Constraint_System_const_iterator_dereference (cit
, &c
);
145 insert_constraint_into_matrix (matrix
, row
, c
);
149 ppl_delete_Constraint_System_const_iterator (cit
);
150 ppl_delete_Constraint_System_const_iterator (end
);
155 /* Creates a CloogMatrix from polyhedron PH. */
158 new_Cloog_Matrix_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph
)
160 ppl_const_Constraint_System_t pcs
;
163 ppl_Polyhedron_get_constraints (ph
, &pcs
);
164 res
= new_Cloog_Matrix_from_ppl_Constraint_System (pcs
);
169 /* Translates row ROW of the CloogMatrix MATRIX to a PPL Constraint. */
171 static ppl_Constraint_t
172 cloog_matrix_to_ppl_constraint (CloogMatrix
*matrix
, int row
)
175 ppl_Constraint_t cstr
;
176 ppl_Coefficient_t coef
;
177 ppl_Linear_Expression_t expr
;
178 ppl_dimension_type dim
= matrix
->NbColumns
- 2;
180 ppl_new_Coefficient (&coef
);
181 ppl_new_Linear_Expression_with_dimension (&expr
, dim
);
183 /* Cast needed to remove warning that is generated as CLooG isl
184 is using an unsigned int for NbColumns and CLooG PPL is
185 using a signed int for NBColumns. */
186 for (j
= 1; j
< (int)matrix
->NbColumns
- 1; j
++)
188 ppl_assign_Coefficient_from_mpz_t (coef
, matrix
->p
[row
][j
]);
189 ppl_Linear_Expression_add_to_coefficient (expr
, j
- 1, coef
);
192 ppl_assign_Coefficient_from_mpz_t (coef
,
193 matrix
->p
[row
][matrix
->NbColumns
- 1]);
194 ppl_Linear_Expression_add_to_inhomogeneous (expr
, coef
);
195 ppl_delete_Coefficient (coef
);
197 if (mpz_sgn (matrix
->p
[row
][0]) == 0)
198 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_EQUAL
);
200 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
202 ppl_delete_Linear_Expression (expr
);
206 /* Creates a PPL constraint system from MATRIX. */
209 new_Constraint_System_from_Cloog_Matrix (ppl_Constraint_System_t
*pcs
,
214 ppl_new_Constraint_System (pcs
);
216 /* Cast needed to remove warning that is generated as CLooG isl
217 is using an unsigned int for NbColumns and CLooG PPL is
218 using a signed int for NBColumns. */
219 for (i
= 0; i
< (int)matrix
->NbRows
; i
++)
221 ppl_Constraint_t c
= cloog_matrix_to_ppl_constraint (matrix
, i
);
222 ppl_Constraint_System_insert_Constraint (*pcs
, c
);
223 ppl_delete_Constraint (c
);
227 /* Creates a PPL Polyhedron from MATRIX. */
230 new_C_Polyhedron_from_Cloog_Matrix (ppl_Polyhedron_t
*ph
,
233 ppl_Constraint_System_t cs
;
234 new_Constraint_System_from_Cloog_Matrix (&cs
, matrix
);
235 ppl_new_C_Polyhedron_recycle_Constraint_System (ph
, cs
);
238 /* Creates a CloogDomain from polyhedron PH. */
241 new_Cloog_Domain_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph
, int nb_params
,
242 CloogState
*state ATTRIBUTE_UNUSED
)
244 CloogMatrix
*mat
= new_Cloog_Matrix_from_ppl_Polyhedron (ph
);
245 CloogDomain
*res
= cloog_domain_from_cloog_matrix (state
, mat
, nb_params
);
246 cloog_matrix_free (mat
);
250 /* Create a CloogScattering from polyhedron PH. */
253 new_Cloog_Scattering_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph
,
254 int nb_params ATTRIBUTE_UNUSED
,
255 int nb_scatt ATTRIBUTE_UNUSED
,
256 CloogState
*state ATTRIBUTE_UNUSED
)
259 CloogMatrix
*mat
= new_Cloog_Matrix_from_ppl_Polyhedron (ph
);
260 CloogScattering
*res
= cloog_scattering_from_cloog_matrix (state
, mat
,
264 cloog_matrix_free (mat
);
267 return new_Cloog_Domain_from_ppl_Polyhedron (ph
, nb_params
, state
);
271 /* Creates a CloogDomain from a pointset powerset PS. */
274 new_Cloog_Domain_from_ppl_Pointset_Powerset
275 (ppl_Pointset_Powerset_C_Polyhedron_t ps
, int nb_params
,
276 CloogState
*state ATTRIBUTE_UNUSED
)
278 CloogDomain
*res
= NULL
;
279 ppl_Pointset_Powerset_C_Polyhedron_iterator_t it
, end
;
281 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&it
);
282 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&end
);
284 for (ppl_Pointset_Powerset_C_Polyhedron_iterator_begin (ps
, it
),
285 ppl_Pointset_Powerset_C_Polyhedron_iterator_end (ps
, end
);
286 !ppl_Pointset_Powerset_C_Polyhedron_iterator_equal_test (it
, end
);
287 ppl_Pointset_Powerset_C_Polyhedron_iterator_increment (it
))
289 ppl_const_Polyhedron_t ph
;
292 ppl_Pointset_Powerset_C_Polyhedron_iterator_dereference (it
, &ph
);
293 tmp
= new_Cloog_Domain_from_ppl_Polyhedron (ph
, nb_params
, state
);
298 res
= cloog_domain_union (res
, tmp
);
301 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (it
);
302 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (end
);
304 gcc_assert (res
!= NULL
);
309 /* Print to FILE the matrix MAT in OpenScop format. OUTPUT is the number
310 of output dimensions, INPUT is the number of input dimensions, LOCALS
311 is the number of existentially quantified variables and PARAMS is the
312 number of parameters. */
315 openscop_print_cloog_matrix (FILE *file
, CloogMatrix
*mat
,
316 int output
, int input
, int locals
,
321 fprintf (file
, "%d %d %d %d %d %d \n", cloog_matrix_nrows (mat
),
322 cloog_matrix_ncolumns (mat
), output
, input
, locals
, params
);
324 for (i
= 0; i
< cloog_matrix_nrows (mat
); i
++)
326 for (j
= 0; j
< cloog_matrix_ncolumns (mat
); j
++)
328 fprintf (file
, "%ld ", mpz_get_si (mat
->p
[i
][j
]));
330 fprintf (file
, "%6ld ", mpz_get_si (mat
->p
[i
][j
]));
332 fprintf (file
, "\n");
336 /* Print to FILE the polyhedron PH in OpenScop format. OUTPUT is the number
337 of output dimensions, INPUT is the number of input dimensions, LOCALS is
338 the number of existentially quantified variables and PARAMS is the number
342 openscop_print_polyhedron_matrix (FILE *file
, ppl_const_Polyhedron_t ph
,
343 int output
, int input
, int locals
,
346 CloogMatrix
*mat
= new_Cloog_Matrix_from_ppl_Polyhedron (ph
);
347 openscop_print_cloog_matrix (file
, mat
, output
, input
, locals
, params
);
348 cloog_matrix_free (mat
);
351 /* Read from FILE a matrix in OpenScop format. OUTPUT is the number of
352 output dimensions, INPUT is the number of input dimensions, LOCALS
353 is the number of existentially quantified variables and PARAMS is the
354 number of parameters. */
357 openscop_read_cloog_matrix (FILE *file
, int *output
, int *input
, int *locals
,
360 int nb_rows
, nb_cols
, i
, j
;
362 int *openscop_matrix_header
, *matrix_line
;
364 openscop_matrix_header
= openscop_read_N_int (file
, 6);
366 nb_rows
= openscop_matrix_header
[0];
367 nb_cols
= openscop_matrix_header
[1];
368 *output
= openscop_matrix_header
[2];
369 *input
= openscop_matrix_header
[3];
370 *locals
= openscop_matrix_header
[4];
371 *params
= openscop_matrix_header
[5];
373 free (openscop_matrix_header
);
375 if (nb_rows
== 0 || nb_cols
== 0)
378 mat
= cloog_matrix_alloc (nb_rows
, nb_cols
);
379 mat
->NbRows
= nb_rows
;
380 mat
->NbColumns
= nb_cols
;
382 for (i
= 0; i
< nb_rows
; i
++)
384 matrix_line
= openscop_read_N_int (file
, nb_cols
);
386 for (j
= 0; j
< nb_cols
; j
++)
387 mpz_set_si (mat
->p
[i
][j
], matrix_line
[j
]);
393 /* Read from FILE the polyhedron PH in OpenScop format. OUTPUT is the number
394 of output dimensions, INPUT is the number of input dimensions, LOCALS is
395 the number of existentially quantified variables and PARAMS is the number
399 openscop_read_polyhedron_matrix (FILE *file
, ppl_Polyhedron_t
*ph
,
400 int *output
, int *input
, int *locals
,
405 mat
= openscop_read_cloog_matrix (file
, output
, input
, locals
, params
);
411 new_C_Polyhedron_from_Cloog_Matrix (ph
, mat
);
412 cloog_matrix_free (mat
);