]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/graphite-cloog-util.c
[multiple changes]
[thirdparty/gcc.git] / gcc / graphite-cloog-util.c
CommitLineData
0647324a
AS
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>.
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
0647324a
AS
25
26#ifdef HAVE_cloog
27
28#include "ppl_c.h"
29#include "cloog/cloog.h"
30#include "graphite-cloog-util.h"
31
32/* Counts the number of constraints in PCS. */
33
34static int
35ppl_Constrain_System_number_of_constraints (ppl_const_Constraint_System_t pcs)
36{
37 ppl_Constraint_System_const_iterator_t cit, end;
38 int num = 0;
39
40 ppl_new_Constraint_System_const_iterator (&cit);
41 ppl_new_Constraint_System_const_iterator (&end);
42
43 for (ppl_Constraint_System_begin (pcs, cit),
44 ppl_Constraint_System_end (pcs, end);
45 !ppl_Constraint_System_const_iterator_equal_test (cit, end);
46 ppl_Constraint_System_const_iterator_increment (cit))
47 num++;
48
49 ppl_delete_Constraint_System_const_iterator (cit);
50 ppl_delete_Constraint_System_const_iterator (end);
51 return num;
52}
53
54static void
55oppose_constraint (CloogMatrix *m, int row)
56{
57 int k;
58
59 /* Do not oppose the first column: it is the eq/ineq one. */
731d8886
TG
60 /* Cast needed to remove warning that is generated as CLooG isl
61 is using an unsigned int for NbColumns and CLooG PPL is
62 using a signed int for NBColumns. */
63 for (k = 1; k < (int)m->NbColumns; k++)
0647324a
AS
64 mpz_neg (m->p[row][k], m->p[row][k]);
65}
66
67/* Inserts constraint CSTR at row ROW of matrix M. */
68
69static void
70insert_constraint_into_matrix (CloogMatrix *m, int row,
71 ppl_const_Constraint_t cstr)
72{
73 ppl_Coefficient_t c;
74 ppl_dimension_type i, dim, nb_cols = m->NbColumns;
75
76 ppl_Constraint_space_dimension (cstr, &dim);
77 ppl_new_Coefficient (&c);
78
79 for (i = 0; i < dim; i++)
80 {
81 ppl_Constraint_coefficient (cstr, i, c);
82 ppl_Coefficient_to_mpz_t (c, m->p[row][i + 1]);
83 }
84
85 for (i = dim; i < nb_cols - 1; i++)
86 mpz_set_si (m->p[row][i + 1], 0);
87
88 ppl_Constraint_inhomogeneous_term (cstr, c);
89 ppl_Coefficient_to_mpz_t (c, m->p[row][nb_cols - 1]);
90 mpz_set_si (m->p[row][0], 1);
91
92 switch (ppl_Constraint_type (cstr))
93 {
94 case PPL_CONSTRAINT_TYPE_LESS_THAN:
95 oppose_constraint (m, row);
96 case PPL_CONSTRAINT_TYPE_GREATER_THAN:
97 mpz_sub_ui (m->p[row][nb_cols - 1],
98 m->p[row][nb_cols - 1], 1);
99 break;
100
101 case PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL:
102 oppose_constraint (m, row);
103 case PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL:
104 break;
105
106 case PPL_CONSTRAINT_TYPE_EQUAL:
107 mpz_set_si (m->p[row][0], 0);
108 break;
109
110 default:
111 /* Not yet implemented. */
112 gcc_unreachable();
113 }
114
115 ppl_delete_Coefficient (c);
116}
117
118/* Creates a CloogMatrix from constraint system PCS. */
119
120static CloogMatrix *
121new_Cloog_Matrix_from_ppl_Constraint_System (ppl_const_Constraint_System_t pcs)
122{
123 CloogMatrix *matrix;
124 ppl_Constraint_System_const_iterator_t cit, end;
125 ppl_dimension_type dim;
126 int rows;
127 int row = 0;
128
129 rows = ppl_Constrain_System_number_of_constraints (pcs);
130 ppl_Constraint_System_space_dimension (pcs, &dim);
131 matrix = cloog_matrix_alloc (rows, dim + 2);
132 ppl_new_Constraint_System_const_iterator (&cit);
133 ppl_new_Constraint_System_const_iterator (&end);
134
135 for (ppl_Constraint_System_begin (pcs, cit),
136 ppl_Constraint_System_end (pcs, end);
137 !ppl_Constraint_System_const_iterator_equal_test (cit, end);
138 ppl_Constraint_System_const_iterator_increment (cit))
139 {
140 ppl_const_Constraint_t c;
141 ppl_Constraint_System_const_iterator_dereference (cit, &c);
142 insert_constraint_into_matrix (matrix, row, c);
143 row++;
144 }
145
146 ppl_delete_Constraint_System_const_iterator (cit);
147 ppl_delete_Constraint_System_const_iterator (end);
148
149 return matrix;
150}
151
152/* Creates a CloogMatrix from polyhedron PH. */
153
154CloogMatrix *
155new_Cloog_Matrix_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph)
156{
157 ppl_const_Constraint_System_t pcs;
158 CloogMatrix *res;
159
160 ppl_Polyhedron_get_constraints (ph, &pcs);
161 res = new_Cloog_Matrix_from_ppl_Constraint_System (pcs);
162
163 return res;
164}
165
166/* Translates row ROW of the CloogMatrix MATRIX to a PPL Constraint. */
167
168static ppl_Constraint_t
169cloog_matrix_to_ppl_constraint (CloogMatrix *matrix, int row)
170{
171 int j;
172 ppl_Constraint_t cstr;
173 ppl_Coefficient_t coef;
174 ppl_Linear_Expression_t expr;
175 ppl_dimension_type dim = matrix->NbColumns - 2;
176
177 ppl_new_Coefficient (&coef);
178 ppl_new_Linear_Expression_with_dimension (&expr, dim);
179
731d8886
TG
180 /* Cast needed to remove warning that is generated as CLooG isl
181 is using an unsigned int for NbColumns and CLooG PPL is
182 using a signed int for NBColumns. */
183 for (j = 1; j < (int)matrix->NbColumns - 1; j++)
0647324a
AS
184 {
185 ppl_assign_Coefficient_from_mpz_t (coef, matrix->p[row][j]);
186 ppl_Linear_Expression_add_to_coefficient (expr, j - 1, coef);
187 }
188
189 ppl_assign_Coefficient_from_mpz_t (coef,
190 matrix->p[row][matrix->NbColumns - 1]);
191 ppl_Linear_Expression_add_to_inhomogeneous (expr, coef);
192 ppl_delete_Coefficient (coef);
193
61298f69 194 if (mpz_sgn (matrix->p[row][0]) == 0)
0647324a
AS
195 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
196 else
197 ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL);
198
199 ppl_delete_Linear_Expression (expr);
200 return cstr;
201}
202
203/* Creates a PPL constraint system from MATRIX. */
204
205static void
206new_Constraint_System_from_Cloog_Matrix (ppl_Constraint_System_t *pcs,
207 CloogMatrix *matrix)
208{
209 int i;
210
211 ppl_new_Constraint_System (pcs);
212
731d8886
TG
213 /* Cast needed to remove warning that is generated as CLooG isl
214 is using an unsigned int for NbColumns and CLooG PPL is
215 using a signed int for NBColumns. */
216 for (i = 0; i < (int)matrix->NbRows; i++)
0647324a
AS
217 {
218 ppl_Constraint_t c = cloog_matrix_to_ppl_constraint (matrix, i);
219 ppl_Constraint_System_insert_Constraint (*pcs, c);
220 ppl_delete_Constraint (c);
221 }
222}
223
224/* Creates a PPL Polyhedron from MATRIX. */
225
226void
227new_C_Polyhedron_from_Cloog_Matrix (ppl_Polyhedron_t *ph,
228 CloogMatrix *matrix)
229{
230 ppl_Constraint_System_t cs;
231 new_Constraint_System_from_Cloog_Matrix (&cs, matrix);
232 ppl_new_C_Polyhedron_recycle_Constraint_System (ph, cs);
233}
234
235/* Creates a CloogDomain from polyhedron PH. */
236
237CloogDomain *
60f87855 238new_Cloog_Domain_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph, int nb_params,
6886e444 239 CloogState *state)
0647324a
AS
240{
241 CloogMatrix *mat = new_Cloog_Matrix_from_ppl_Polyhedron (ph);
60f87855 242 CloogDomain *res = cloog_domain_from_cloog_matrix (state, mat, nb_params);
0647324a
AS
243 cloog_matrix_free (mat);
244 return res;
245}
246
1461761b
AS
247/* Create a CloogScattering from polyhedron PH. */
248
249CloogScattering *
250new_Cloog_Scattering_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph,
6886e444
RG
251 int nb_params,
252 int nb_scatt,
253 CloogState *state)
1461761b 254{
1461761b
AS
255 CloogMatrix *mat = new_Cloog_Matrix_from_ppl_Polyhedron (ph);
256 CloogScattering *res = cloog_scattering_from_cloog_matrix (state, mat,
257 nb_scatt,
258 nb_params);
259
260 cloog_matrix_free (mat);
261 return res;
1461761b
AS
262}
263
0647324a
AS
264/* Creates a CloogDomain from a pointset powerset PS. */
265
266CloogDomain *
60f87855
AS
267new_Cloog_Domain_from_ppl_Pointset_Powerset
268 (ppl_Pointset_Powerset_C_Polyhedron_t ps, int nb_params,
6886e444 269 CloogState *state)
0647324a
AS
270{
271 CloogDomain *res = NULL;
272 ppl_Pointset_Powerset_C_Polyhedron_iterator_t it, end;
273
274 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&it);
275 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&end);
276
277 for (ppl_Pointset_Powerset_C_Polyhedron_iterator_begin (ps, it),
278 ppl_Pointset_Powerset_C_Polyhedron_iterator_end (ps, end);
279 !ppl_Pointset_Powerset_C_Polyhedron_iterator_equal_test (it, end);
280 ppl_Pointset_Powerset_C_Polyhedron_iterator_increment (it))
281 {
282 ppl_const_Polyhedron_t ph;
283 CloogDomain *tmp;
284
285 ppl_Pointset_Powerset_C_Polyhedron_iterator_dereference (it, &ph);
60f87855 286 tmp = new_Cloog_Domain_from_ppl_Polyhedron (ph, nb_params, state);
0647324a
AS
287
288 if (res == NULL)
289 res = tmp;
290 else
291 res = cloog_domain_union (res, tmp);
292 }
293
294 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (it);
295 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (end);
296
297 gcc_assert (res != NULL);
298
299 return res;
300}
ae403f5a
RB
301
302/* Print to FILE the matrix MAT in OpenScop format. OUTPUT is the number
303 of output dimensions, INPUT is the number of input dimensions, LOCALS
304 is the number of existentially quantified variables and PARAMS is the
305 number of parameters. */
306
307static void
308openscop_print_cloog_matrix (FILE *file, CloogMatrix *mat,
309 int output, int input, int locals,
310 int params)
311{
6886e444 312 unsigned i, j;
ae403f5a 313
6886e444
RG
314 fprintf (file, "%d %d %d %d %d %d \n", mat->NbRows,
315 mat->NbColumns, output, input, locals, params);
ae403f5a 316
6886e444 317 for (i = 0; i < mat->NbRows; i++)
ae403f5a 318 {
6886e444 319 for (j = 0; j < mat->NbColumns; j++)
7241f8c5
RB
320 if (j == 0)
321 fprintf (file, "%ld ", mpz_get_si (mat->p[i][j]));
322 else
323 fprintf (file, "%6ld ", mpz_get_si (mat->p[i][j]));
324
ae403f5a
RB
325 fprintf (file, "\n");
326 }
327}
328
329/* Print to FILE the polyhedron PH in OpenScop format. OUTPUT is the number
330 of output dimensions, INPUT is the number of input dimensions, LOCALS is
331 the number of existentially quantified variables and PARAMS is the number
332 of parameters. */
333
334void
335openscop_print_polyhedron_matrix (FILE *file, ppl_const_Polyhedron_t ph,
336 int output, int input, int locals,
337 int params)
338{
339 CloogMatrix *mat = new_Cloog_Matrix_from_ppl_Polyhedron (ph);
340 openscop_print_cloog_matrix (file, mat, output, input, locals, params);
341 cloog_matrix_free (mat);
342}
343
721c8b1e
RB
344/* Read from FILE a matrix in OpenScop format. OUTPUT is the number of
345 output dimensions, INPUT is the number of input dimensions, LOCALS
346 is the number of existentially quantified variables and PARAMS is the
347 number of parameters. */
348
349static CloogMatrix *
350openscop_read_cloog_matrix (FILE *file, int *output, int *input, int *locals,
351 int *params)
352{
353 int nb_rows, nb_cols, i, j;
354 CloogMatrix *mat;
355 int *openscop_matrix_header, *matrix_line;
356
357 openscop_matrix_header = openscop_read_N_int (file, 6);
358
359 nb_rows = openscop_matrix_header[0];
360 nb_cols = openscop_matrix_header[1];
361 *output = openscop_matrix_header[2];
362 *input = openscop_matrix_header[3];
363 *locals = openscop_matrix_header[4];
364 *params = openscop_matrix_header[5];
365
366 free (openscop_matrix_header);
367
368 if (nb_rows == 0 || nb_cols == 0)
369 return NULL;
370
371 mat = cloog_matrix_alloc (nb_rows, nb_cols);
372 mat->NbRows = nb_rows;
373 mat->NbColumns = nb_cols;
374
375 for (i = 0; i < nb_rows; i++)
376 {
377 matrix_line = openscop_read_N_int (file, nb_cols);
378
379 for (j = 0; j < nb_cols; j++)
380 mpz_set_si (mat->p[i][j], matrix_line[j]);
381 }
382
383 return mat;
384}
385
386/* Read from FILE the polyhedron PH in OpenScop format. OUTPUT is the number
387 of output dimensions, INPUT is the number of input dimensions, LOCALS is
388 the number of existentially quantified variables and PARAMS is the number
389 of parameters. */
390
391void
392openscop_read_polyhedron_matrix (FILE *file, ppl_Polyhedron_t *ph,
393 int *output, int *input, int *locals,
394 int *params)
395{
396 CloogMatrix *mat;
397
398 mat = openscop_read_cloog_matrix (file, output, input, locals, params);
399
400 if (!mat)
401 *ph = NULL;
402 else
403 {
404 new_C_Polyhedron_from_Cloog_Matrix (ph, mat);
405 cloog_matrix_free (mat);
406 }
407}
408
0647324a 409#endif