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 for (k
= 1; k
< m
->NbColumns
; k
++)
64 mpz_neg (m
->p
[row
][k
], m
->p
[row
][k
]);
67 /* Inserts constraint CSTR at row ROW of matrix M. */
70 insert_constraint_into_matrix (CloogMatrix
*m
, int row
,
71 ppl_const_Constraint_t cstr
)
74 ppl_dimension_type i
, dim
, nb_cols
= m
->NbColumns
;
76 ppl_Constraint_space_dimension (cstr
, &dim
);
77 ppl_new_Coefficient (&c
);
79 for (i
= 0; i
< dim
; i
++)
81 ppl_Constraint_coefficient (cstr
, i
, c
);
82 ppl_Coefficient_to_mpz_t (c
, m
->p
[row
][i
+ 1]);
85 for (i
= dim
; i
< nb_cols
- 1; i
++)
86 mpz_set_si (m
->p
[row
][i
+ 1], 0);
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);
92 switch (ppl_Constraint_type (cstr
))
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);
101 case PPL_CONSTRAINT_TYPE_LESS_OR_EQUAL
:
102 oppose_constraint (m
, row
);
103 case PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
:
106 case PPL_CONSTRAINT_TYPE_EQUAL
:
107 mpz_set_si (m
->p
[row
][0], 0);
111 /* Not yet implemented. */
115 ppl_delete_Coefficient (c
);
118 /* Creates a CloogMatrix from constraint system PCS. */
121 new_Cloog_Matrix_from_ppl_Constraint_System (ppl_const_Constraint_System_t pcs
)
124 ppl_Constraint_System_const_iterator_t cit
, end
;
125 ppl_dimension_type dim
;
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
);
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
))
140 ppl_const_Constraint_t c
;
141 ppl_Constraint_System_const_iterator_dereference (cit
, &c
);
142 insert_constraint_into_matrix (matrix
, row
, c
);
146 ppl_delete_Constraint_System_const_iterator (cit
);
147 ppl_delete_Constraint_System_const_iterator (end
);
152 /* Creates a CloogMatrix from polyhedron PH. */
155 new_Cloog_Matrix_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph
)
157 ppl_const_Constraint_System_t pcs
;
160 ppl_Polyhedron_get_constraints (ph
, &pcs
);
161 res
= new_Cloog_Matrix_from_ppl_Constraint_System (pcs
);
166 /* Translates row ROW of the CloogMatrix MATRIX to a PPL Constraint. */
168 static ppl_Constraint_t
169 cloog_matrix_to_ppl_constraint (CloogMatrix
*matrix
, int row
)
172 ppl_Constraint_t cstr
;
173 ppl_Coefficient_t coef
;
174 ppl_Linear_Expression_t expr
;
175 ppl_dimension_type dim
= matrix
->NbColumns
- 2;
177 ppl_new_Coefficient (&coef
);
178 ppl_new_Linear_Expression_with_dimension (&expr
, dim
);
180 for (j
= 1; j
< matrix
->NbColumns
- 1; j
++)
182 ppl_assign_Coefficient_from_mpz_t (coef
, matrix
->p
[row
][j
]);
183 ppl_Linear_Expression_add_to_coefficient (expr
, j
- 1, coef
);
186 ppl_assign_Coefficient_from_mpz_t (coef
,
187 matrix
->p
[row
][matrix
->NbColumns
- 1]);
188 ppl_Linear_Expression_add_to_inhomogeneous (expr
, coef
);
189 ppl_delete_Coefficient (coef
);
191 if (mpz_sgn (matrix
->p
[row
][0]) == 0)
192 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_EQUAL
);
194 ppl_new_Constraint (&cstr
, expr
, PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
196 ppl_delete_Linear_Expression (expr
);
200 /* Creates a PPL constraint system from MATRIX. */
203 new_Constraint_System_from_Cloog_Matrix (ppl_Constraint_System_t
*pcs
,
208 ppl_new_Constraint_System (pcs
);
210 for (i
= 0; i
< matrix
->NbRows
; i
++)
212 ppl_Constraint_t c
= cloog_matrix_to_ppl_constraint (matrix
, i
);
213 ppl_Constraint_System_insert_Constraint (*pcs
, c
);
214 ppl_delete_Constraint (c
);
218 /* Creates a PPL Polyhedron from MATRIX. */
221 new_C_Polyhedron_from_Cloog_Matrix (ppl_Polyhedron_t
*ph
,
224 ppl_Constraint_System_t cs
;
225 new_Constraint_System_from_Cloog_Matrix (&cs
, matrix
);
226 ppl_new_C_Polyhedron_recycle_Constraint_System (ph
, cs
);
229 /* Creates a CloogDomain from polyhedron PH. */
232 new_Cloog_Domain_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph
, int nb_params
,
233 CloogState
*state ATTRIBUTE_UNUSED
)
235 CloogMatrix
*mat
= new_Cloog_Matrix_from_ppl_Polyhedron (ph
);
236 CloogDomain
*res
= cloog_domain_from_cloog_matrix (state
, mat
, nb_params
);
237 cloog_matrix_free (mat
);
241 /* Create a CloogScattering from polyhedron PH. */
244 new_Cloog_Scattering_from_ppl_Polyhedron (ppl_const_Polyhedron_t ph
,
245 int nb_params ATTRIBUTE_UNUSED
,
246 int nb_scatt ATTRIBUTE_UNUSED
,
247 CloogState
*state ATTRIBUTE_UNUSED
)
250 CloogMatrix
*mat
= new_Cloog_Matrix_from_ppl_Polyhedron (ph
);
251 CloogScattering
*res
= cloog_scattering_from_cloog_matrix (state
, mat
,
255 cloog_matrix_free (mat
);
258 return new_Cloog_Domain_from_ppl_Polyhedron (ph
, nb_params
, state
);
262 /* Creates a CloogDomain from a pointset powerset PS. */
265 new_Cloog_Domain_from_ppl_Pointset_Powerset
266 (ppl_Pointset_Powerset_C_Polyhedron_t ps
, int nb_params
,
267 CloogState
*state ATTRIBUTE_UNUSED
)
269 CloogDomain
*res
= NULL
;
270 ppl_Pointset_Powerset_C_Polyhedron_iterator_t it
, end
;
272 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&it
);
273 ppl_new_Pointset_Powerset_C_Polyhedron_iterator (&end
);
275 for (ppl_Pointset_Powerset_C_Polyhedron_iterator_begin (ps
, it
),
276 ppl_Pointset_Powerset_C_Polyhedron_iterator_end (ps
, end
);
277 !ppl_Pointset_Powerset_C_Polyhedron_iterator_equal_test (it
, end
);
278 ppl_Pointset_Powerset_C_Polyhedron_iterator_increment (it
))
280 ppl_const_Polyhedron_t ph
;
283 ppl_Pointset_Powerset_C_Polyhedron_iterator_dereference (it
, &ph
);
284 tmp
= new_Cloog_Domain_from_ppl_Polyhedron (ph
, nb_params
, state
);
289 res
= cloog_domain_union (res
, tmp
);
292 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (it
);
293 ppl_delete_Pointset_Powerset_C_Polyhedron_iterator (end
);
295 gcc_assert (res
!= NULL
);