1 // Copyright (C) 2009 Free Software Foundation, Inc.
2 // Contributed by Jan Sjodin <jan.sjodin@amd.com>.
4 // This file is part of the Polyhedral Compilation Package Library (libpcp).
6 // Libpcp is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation; either version 2.1 of the License, or
9 // (at your option) any later version.
11 // Libpcp is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with libpcp; see the file COPYING.LIB. If not, write to the
18 // Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 // MA 02110-1301, USA.
21 // As a special exception, if you link this library with other files, some
22 // of which are compiled with GCC, to produce an executable, this library
23 // does not by itself cause the resulting executable to be covered by the
24 // GNU General Public License. This exception does not however invalidate
25 // any other reasons why the executable file might be covered by the GNU
26 // General Public License.
29 #include "pcp_poly_spi.h"
30 #include "pcp_dynamic_array.h"
31 #include "pcp_string_buffer.h"
32 #include "pcp_poly_spi_c.h"
34 class PcpPolySpiC
: public PcpPolySpi
37 struct pcp_poly_spi
* interface
;
41 // Coefficient primitives
43 coefficientGetValue(Coefficient
* coeff
)
45 return this->interface
->coefficientGetValue(coeff
);
48 PcpPolySpi::Coefficient
*
49 coefficientCreate(int value
)
51 return (PcpPolySpi::Coefficient
*)this->interface
->coefficientCreate(value
);
55 coefficientToString(Coefficient
* coeff
)
57 return this->interface
->coefficientToString(coeff
);
60 void coefficientDestroy(Coefficient
* coeff
)
62 this->interface
->coefficientDestroy(coeff
);
66 // Linear Expr Primitives
67 PcpPolySpi::LinearExpr
* linearExprCopy(LinearExpr
* linearExpr
)
69 return (PcpPolySpi::LinearExpr
*)this->interface
->linearExprCopy(linearExpr
);
72 void linearExprAddToCoefficient(LinearExpr
* linearExpr
, int dimension
, Coefficient
* value
)
74 this->interface
->linearExprAddToCoefficient(linearExpr
, dimension
, value
);
77 void linearExprAddToInhomogenous(LinearExpr
* linearExpr
, Coefficient
* value
)
79 this->interface
->linearExprAddToInhomogenous(linearExpr
, value
);
82 void linearExprSetCoefficient(LinearExpr
* linearExpr
, int dimension
, Coefficient
* value
)
84 this->interface
->linearExprSetCoefficient(linearExpr
, dimension
, value
);
88 int linearExprGetNumDimensions(LinearExpr
* linearExpr
)
90 return this->interface
->linearExprGetNumDimensions(linearExpr
);
93 void linearExprSubtract(LinearExpr
* linearExpr
, LinearExpr
* subtract
)
95 this->interface
->linearExprSubtract(linearExpr
, subtract
);
99 linearExprToString(LinearExpr
* linearExpr
)
101 return this->interface
->linearExprToString(linearExpr
);
104 PcpPolySpi::LinearExpr
* linearExprCreate()
106 return (PcpPolySpi::LinearExpr
*)this->interface
->linearExprCreate();
109 PcpPolySpi::LinearExpr
* linearExprCreate(int numDimensions
)
111 return (PcpPolySpi::LinearExpr
*) this->interface
->linearExprCreateDim(numDimensions
);
114 void linearExprDestroy(LinearExpr
* linearExpr
)
116 this->interface
->linearExprDestroy(linearExpr
);
119 // Constraint primitives
120 PcpPolySpi::Constraint
* constraintCreateEqualZero(LinearExpr
* linearExpr
)
122 return (PcpPolySpi::Constraint
*) this->interface
->constraintCreateEqualZero(linearExpr
);
125 PcpPolySpi::Constraint
* constraintCreateGreaterEqualZero(LinearExpr
* linearExpr
)
127 return (PcpPolySpi::Constraint
*) this->interface
->constraintCreateGreaterEqualZero(linearExpr
);
130 void constraintDestroy(Constraint
* constraint
)
132 this->interface
->constraintDestroy(constraint
);
136 constraintToString(Constraint
* constraint
)
138 return this->interface
->constraintToString(constraint
);
141 // Polyhedron primitives
142 PcpPolySpi::ConstraintSystem
* polyhedronGetConstraintSystem(Polyhedron
* polyhedron
)
144 return (PcpPolySpi::ConstraintSystem
*) this->interface
->polyhedronGetConstraintSystem(polyhedron
);
148 void polyhedronAddConstraint(Polyhedron
* polyhedron
, Constraint
* constraint
)
150 this->interface
->polyhedronAddConstraint(polyhedron
, constraint
);
153 void polyhedronAddConstraints(Polyhedron
* polyhedron
, ConstraintSystem
* constraintSystem
)
155 this->interface
->polyhedronAddConstraints(polyhedron
, constraintSystem
);
158 void polyhedronRemap(Polyhedron
* polyhedron
, PcpArray
<int>* map
)
160 // FIXME: implement conversion from array to int*
161 int size
= map
->getSize();
162 int* array
= (int*)malloc(sizeof(int)*size
);
163 for(int i
= 0; i
< size
; i
++)
164 array
[i
] = map
->get(i
);
165 this->interface
->polyhedronRemap(polyhedron
, array
, size
);
168 PcpPolySpi::Polyhedron
* polyhedronCopy(Polyhedron
* polyhedron
)
170 return (PcpPolySpi::Polyhedron
*) this->interface
->polyhedronCopy(polyhedron
);
173 PcpPolySpi::Polyhedron
* polyhedronCreate(int numDimensions
)
175 return (PcpPolySpi::Polyhedron
*) this->interface
->polyhedronCreate(numDimensions
);
178 void polyhedronDestroy(Polyhedron
* polyhedron
)
180 this->interface
->polyhedronDestroy(polyhedron
);
183 void pointsetPowersetDestroy(PointsetPowerset
* poinsetPowerset
)
185 this->interface
->pointsetPowersetDestroy(poinsetPowerset
);
189 PcpPolySpi::PointsetPowerset
* pointsetPowersetCreate(Polyhedron
* polyhedron
)
191 return (PcpPolySpi::PointsetPowerset
*) this->interface
->pointsetPowersetCreate(polyhedron
);
196 this->interface
= NULL
;
199 PcpPolySpiC(struct pcp_poly_spi
* spi
)
201 this->interface
= spi
;
206 #include "cloog/cloog.h"
208 #include "pcp_poly_spi_c.h"
210 #include "pcp_string_buffer.h"
212 static void ppl_coefficient_print(ppl_Coefficient_t
* coeff
)
214 printf("Coefficient: ");
215 ppl_io_print_Coefficient(*coeff
);
219 static int ppl_coefficient_to_int(void* coeff
)
223 ppl_Coefficient_to_mpz_t(*((ppl_Coefficient_t
*) coeff
), v
);
224 return value_get_si(v
);
227 static const char* ppl_coefficient_to_string(void* coeff
)
229 PcpStringBuffer stringBuffer
;
230 stringBuffer
.appendInt(ppl_coefficient_to_int(coeff
));
231 return stringBuffer
.toString();
234 static void* ppl_coefficient_create(int value
)
238 value_set_si(v
, value
);
240 ppl_Coefficient_t
* coeff
= (ppl_Coefficient_t
*) malloc(sizeof(ppl_Coefficient_t
));
241 ppl_new_Coefficient(coeff
);
242 ppl_assign_Coefficient_from_mpz_t(*coeff
, v
);
246 static void ppl_coefficient_destroy(void* coeff
)
248 ppl_delete_Coefficient(*((ppl_Coefficient_t
*)coeff
));
249 free((ppl_Coefficient_t
*)coeff
);
253 static void ppl_linear_expr_print(ppl_Linear_Expression_t
* linearExpr
)
255 printf("Linear Expression: ");
256 ppl_io_print_Linear_Expression(*linearExpr
);
260 static void ppl_linear_expr_add_to_coefficient(void* linearExpr
, int dimension
, void* value
)
262 ppl_Linear_Expression_add_to_coefficient(*((ppl_Linear_Expression_t
*)linearExpr
),
264 *((ppl_Coefficient_t
*)value
));
267 static void ppl_linear_expr_add_to_inhomogenous(void* linearExpr
, void* value
)
269 ppl_Linear_Expression_add_to_inhomogeneous(*((ppl_Linear_Expression_t
*)linearExpr
),
270 *((ppl_Coefficient_t
*)value
));
274 set_coef (ppl_Linear_Expression_t e
, ppl_dimension_type i
, int x
)
281 ppl_new_Coefficient (&c
);
283 ppl_Linear_Expression_coefficient (e
, i
, c
);
284 ppl_Coefficient_to_mpz_t (c
, v1
);
285 value_oppose (v1
, v1
);
286 value_set_si (v0
, x
);
287 value_addto (v0
, v0
, v1
);
288 ppl_assign_Coefficient_from_mpz_t (c
, v0
);
289 ppl_Linear_Expression_add_to_coefficient (e
, i
, c
);
293 ppl_delete_Coefficient (c
);
296 static void ppl_linear_expr_set_coefficient(void* linearExpr
, int dimension
, void* value
)
298 ppl_Coefficient_t coeff
;
299 ppl_new_Coefficient(&coeff
);
300 ppl_Linear_Expression_coefficient(*((ppl_Linear_Expression_t
*)linearExpr
), dimension
, coeff
);
301 set_coef(*((ppl_Linear_Expression_t
*)linearExpr
),
303 ppl_coefficient_to_int(value
));
305 ppl_Linear_Expression_coefficient(*((ppl_Linear_Expression_t
*)linearExpr
), dimension
, coeff
);
308 static int ppl_linear_expr_get_num_dimensions(void* linearExpr
)
310 ppl_dimension_type numDimensions
;
311 ppl_Linear_Expression_space_dimension (*((ppl_Linear_Expression_t
*)linearExpr
), &numDimensions
);
312 return (int)numDimensions
;
315 static void ppl_linear_expr_subtract(void* linearExpr0
, void* linearExpr1
)
317 ppl_subtract_Linear_Expression_from_Linear_Expression(*((ppl_Linear_Expression_t
*)linearExpr0
),
318 *((ppl_Linear_Expression_t
*)linearExpr1
));
321 static const char* ppl_linear_expr_to_string(void* linearExpr
)
323 int numDimensions
= ppl_linear_expr_get_num_dimensions(linearExpr
);
325 PcpStringBuffer buffer
;
326 ppl_Coefficient_t coeff
;
328 ppl_new_Coefficient(&coeff
);
330 for(i
= 0; i
< numDimensions
; i
++)
333 ppl_Linear_Expression_coefficient(*((ppl_Linear_Expression_t
*)linearExpr
),
336 buffer
.appendInt(ppl_coefficient_to_int(coeff
));
340 ppl_Linear_Expression_inhomogeneous_term(*((ppl_Linear_Expression_t
*)linearExpr
),
342 buffer
.appendInt(ppl_coefficient_to_int(&coeff
));
343 ppl_delete_Coefficient(coeff
);
344 return buffer
.toString();
347 static void* ppl_linear_expr_create_dim(int numDimensions
)
349 ppl_Linear_Expression_t
* linearExpr
= (ppl_Linear_Expression_t
*) malloc(sizeof(ppl_Linear_Expression_t
));
350 ppl_new_Linear_Expression_with_dimension(linearExpr
, numDimensions
);
354 static void ppl_linear_expr_destroy(void* linearExpr
)
356 ppl_delete_Linear_Expression(*((ppl_Linear_Expression_t
*)linearExpr
));
360 static void* ppl_constraint_create_equal_zero(void* linearExpr
)
362 ppl_Constraint_t
* constraint
= (ppl_Constraint_t
*) malloc(sizeof(ppl_Constraint_t
));
363 ppl_new_Constraint(constraint
, *((ppl_Linear_Expression_t
*)linearExpr
), PPL_CONSTRAINT_TYPE_EQUAL
);
367 static void* ppl_constraint_create_greater_equal_zero(void* linearExpr
)
369 ppl_Constraint_t
* constraint
= (ppl_Constraint_t
*) malloc(sizeof(ppl_Constraint_t
));
370 ppl_new_Constraint(constraint
, *((ppl_Linear_Expression_t
*)linearExpr
), PPL_CONSTRAINT_TYPE_GREATER_OR_EQUAL
);
374 static void ppl_constraint_destory(void* constraint
)
376 ppl_delete_Constraint(*((ppl_Constraint_t
*)constraint
));
380 static const char* ppl_constraint_to_string(void* constr
)
382 ppl_Constraint_t
* constraint
= (ppl_Constraint_t
*) constr
;
383 bool isEqual
= ppl_Constraint_type(*constraint
) == PPL_CONSTRAINT_TYPE_EQUAL
;
384 ppl_dimension_type numDimensions
;
385 PcpStringBuffer buffer
;
386 ppl_Coefficient_t coeff
;
389 ppl_new_Coefficient(&coeff
);
390 ppl_Constraint_space_dimension(*constraint
, &numDimensions
);
391 for(i
= 0; i
< numDimensions
; i
++)
393 ppl_Constraint_coefficient(*constraint
, i
, coeff
);
394 buffer
.append(ppl_coefficient_to_string(&coeff
));
397 ppl_Constraint_inhomogeneous_term(*constraint
, coeff
);
398 buffer
.appendInt(ppl_coefficient_to_int(&coeff
));
400 buffer
.append( isEqual
? " == 0" : " >= 0" );
401 ppl_delete_Coefficient(coeff
);
402 return buffer
.toString();
405 static void* ppl_polyhedron_get_constraint_system(void* polyhedron
)
407 ppl_const_Constraint_System_t
* constraintSystem
=
408 (ppl_const_Constraint_System_t
*) malloc(sizeof(ppl_const_Constraint_System_t
));
409 ppl_Polyhedron_get_constraints(*((ppl_Polyhedron_t
*)polyhedron
), constraintSystem
);
410 return constraintSystem
;
413 static void ppl_polyhedron_add_constraint(void* polyhedron
, void* constraint
)
415 ppl_Polyhedron_add_constraint(*((ppl_Polyhedron_t
*)polyhedron
), *((ppl_Constraint_t
*)constraint
));
418 static void ppl_polyhedron_add_constraints(void* polyhedron
, void* constraintsystem
)
420 ppl_Polyhedron_add_constraints(*((ppl_Polyhedron_t
*)polyhedron
), *((ppl_Constraint_System_t
*)constraintsystem
));
423 static void ppl_polyhedron_remap(void* polyhedron
, int* map
, int numElements
)
425 ppl_Polyhedron_map_space_dimensions(*((ppl_Polyhedron_t
*)polyhedron
), (ppl_dimension_type
*)map
, numElements
);
428 static void* ppl_polyhedron_copy(void* polyhedron
)
430 ppl_Polyhedron_t
* copy
= (ppl_Polyhedron_t
*)malloc(sizeof(ppl_Polyhedron_t
*));
431 ppl_new_NNC_Polyhedron_from_NNC_Polyhedron(copy
, *((ppl_Polyhedron_t
*)polyhedron
));
435 static void* ppl_polyhedron_create(int numDimensions
)
437 ppl_Polyhedron_t
* polyhedron
= (ppl_Polyhedron_t
*)malloc(sizeof(ppl_Polyhedron_t
*));
438 ppl_new_NNC_Polyhedron_from_space_dimension(polyhedron
, numDimensions
, 0);
442 static void ppl_polyhedron_destroy(void* polyhedron
)
444 ppl_delete_Polyhedron(*((ppl_Polyhedron_t
*)polyhedron
));
447 void* ppl_pointset_powerset_create(void* polyhedron
)
449 ppl_Pointset_Powerset_NNC_Polyhedron_t
* pointsetPowerset
= (ppl_Pointset_Powerset_NNC_Polyhedron_t
*)
450 malloc(sizeof(ppl_Pointset_Powerset_NNC_Polyhedron_t
));
451 ppl_new_Pointset_Powerset_NNC_Polyhedron_from_NNC_Polyhedron(pointsetPowerset
, *((ppl_Polyhedron_t
*)polyhedron
));
452 return pointsetPowerset
;
455 void ppl_pointset_powerset_destroy(void* pointsetPowerset
)
457 ppl_delete_Pointset_Powerset_NNC_Polyhedron(*((ppl_Pointset_Powerset_NNC_Polyhedron_t
*)pointsetPowerset
));
460 static pcp_poly_spi
* create_ppl_c_interface()
462 struct pcp_poly_spi
* pcp_poly_spi_impl
= (struct pcp_poly_spi
*)malloc(sizeof(pcp_poly_spi
));
464 pcp_poly_spi_impl
->coefficientGetValue
= &ppl_coefficient_to_int
;
465 pcp_poly_spi_impl
->coefficientToString
= &ppl_coefficient_to_string
;
466 pcp_poly_spi_impl
->coefficientCreate
= &ppl_coefficient_create
;
467 pcp_poly_spi_impl
->coefficientDestroy
= &ppl_coefficient_destroy
;
468 pcp_poly_spi_impl
->linearExprCopy
= NULL
; // Probably not needed
469 pcp_poly_spi_impl
->linearExprAddToCoefficient
= ppl_linear_expr_add_to_coefficient
;
470 pcp_poly_spi_impl
->linearExprAddToInhomogenous
= ppl_linear_expr_add_to_inhomogenous
;
471 pcp_poly_spi_impl
->linearExprSetCoefficient
= ppl_linear_expr_set_coefficient
;
472 pcp_poly_spi_impl
->linearExprGetNumDimensions
= ppl_linear_expr_get_num_dimensions
;
473 pcp_poly_spi_impl
->linearExprSubtract
= ppl_linear_expr_subtract
;
474 pcp_poly_spi_impl
->linearExprToString
= ppl_linear_expr_to_string
;
475 pcp_poly_spi_impl
->linearExprCreate
= NULL
; // Not needed
476 pcp_poly_spi_impl
->linearExprCreateDim
= ppl_linear_expr_create_dim
;
477 pcp_poly_spi_impl
->linearExprDestroy
= ppl_linear_expr_destroy
;
478 pcp_poly_spi_impl
->constraintCreateGreaterEqualZero
= ppl_constraint_create_greater_equal_zero
;
479 pcp_poly_spi_impl
->constraintCreateEqualZero
= ppl_constraint_create_equal_zero
;
480 pcp_poly_spi_impl
->constraintDestroy
= ppl_constraint_destory
;
481 pcp_poly_spi_impl
->constraintToString
= ppl_constraint_to_string
;
482 pcp_poly_spi_impl
->polyhedronGetConstraintSystem
= ppl_polyhedron_get_constraint_system
;
483 pcp_poly_spi_impl
->polyhedronAddConstraint
= ppl_polyhedron_add_constraint
;
484 pcp_poly_spi_impl
->polyhedronAddConstraints
= ppl_polyhedron_add_constraints
;
485 pcp_poly_spi_impl
->polyhedronRemap
= ppl_polyhedron_remap
;
486 pcp_poly_spi_impl
->polyhedronCopy
= ppl_polyhedron_copy
;
487 pcp_poly_spi_impl
->polyhedronCreate
= ppl_polyhedron_create
;
488 pcp_poly_spi_impl
->polyhedronDestroy
= ppl_polyhedron_destroy
;
489 pcp_poly_spi_impl
->pointsetPowersetCreate
= ppl_pointset_powerset_create
;
490 pcp_poly_spi_impl
->pointsetPowersetDestroy
= ppl_pointset_powerset_destroy
;
493 PcpPolySpi
* createPPLPolySpi()
495 return new PcpPolySpiC(create_ppl_c_interface());