update docs to introduction of CloogScattering and CloogScatteringList
[cloog.git] / source / polylib / domain.c
blobbf118af93c533dbb63e5623abdb4479d52c0acbc
2 /**-------------------------------------------------------------------**
3 ** CLooG **
4 **-------------------------------------------------------------------**
5 ** domain.c **
6 **-------------------------------------------------------------------**
7 ** First version: october 28th 2001 **
8 **-------------------------------------------------------------------**/
11 /******************************************************************************
12 * CLooG : the Chunky Loop Generator (experimental) *
13 ******************************************************************************
14 * *
15 * Copyright (C) 2001-2005 Cedric Bastoul *
16 * *
17 * This is free software; you can redistribute it and/or modify it under the *
18 * terms of the GNU General Public License as published by the Free Software *
19 * Foundation; either version 2 of the License, or (at your option) any later *
20 * version. *
21 * *
22 * This software is distributed in the hope that it will be useful, but *
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
25 * for more details. *
26 * *
27 * You should have received a copy of the GNU General Public License along *
28 * with software; if not, write to the Free Software Foundation, Inc., *
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
30 * *
31 * CLooG, the Chunky Loop Generator *
32 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
33 * *
34 ******************************************************************************/
35 /* CAUTION: the english used for comments is probably the worst you ever read,
36 * please feel free to correct and improve it !
40 # include <stdlib.h>
41 # include <stdio.h>
42 # include <ctype.h>
43 #include <cloog/polylib/cloog.h>
46 /**
47 * The maximal number of rays allowed to be allocated by PolyLib. In fact since
48 * version 5.20, PolyLib automatically tune the number of rays by multiplying
49 * by 2 this number each time the maximum is reached. For unknown reasons
50 * PolyLib makes a segmentation fault if this number is too small. If this
51 * number is too small, performances will be reduced, if it is too high, memory
52 * will be saturated. Note that the option "-rays X" set this number to X.
54 int MAX_RAYS = 50 ;
57 /******************************************************************************
58 * Memory leaks hunting *
59 ******************************************************************************/
62 /**
63 * These functions and global variables are devoted to memory leaks hunting: we
64 * want to know at each moment how many Polyhedron structures had been allocated
65 * (cloog_domain_allocated) and how many had been freed (cloog_domain_freed).
66 * Each time a Polyhedron structure is allocated, a call to the function
67 * cloog_domain_leak_up() must be carried out, and respectively
68 * cloog_domain_leak_down() when a Polyhedron structure is freed. The special
69 * variable cloog_domain_max gives the maximal number of Polyhedron structures
70 * simultaneously alive (i.e. allocated and non-freed) in memory.
71 * - July 3rd->11th 2003: first version (memory leaks hunt and correction).
75 int cloog_domain_allocated = 0 ;
76 int cloog_domain_freed = 0 ;
77 int cloog_domain_max = 0 ;
80 static void cloog_domain_leak_up()
81 { cloog_domain_allocated ++ ;
82 if ((cloog_domain_allocated-cloog_domain_freed) > cloog_domain_max)
83 cloog_domain_max = cloog_domain_allocated - cloog_domain_freed ;
87 static void cloog_domain_leak_down()
88 { cloog_domain_freed ++ ;
92 /******************************************************************************
93 * PolyLib interface *
94 ******************************************************************************/
97 /* CLooG makes an intensive use of polyhedral operations and the PolyLib do
98 * the job. Here are the interfaces to all the PolyLib calls (CLooG uses 19
99 * PolyLib functions), with or without some adaptations. If another polyhedral
100 * library can be used, only these functions have to be changed.
101 * - April 16th 2005: Since PolyLib 5.20, compacting is no more useful and have
102 * been removed. The direct use of the PolyLib's Polyhedron
103 * data structure is also replaced with the CloogDomain data
104 * structure that includes the Polyhedron and an additional
105 * counter on how many pointers point on this structure.
106 * This allows to save memory (cloog_domain_copy now only
107 * increment the counter) while memory leaks are avoided (the
108 * function cloog_domain_free decrements the counter and
109 * actually frees the data structure only when its value
110 * is 0).
114 * Returns true if each scattering dimension is defined in terms
115 * of the original iterators.
117 int cloog_scattering_fully_specified(CloogScattering *scattering,
118 CloogDomain *domain)
120 int scattering_dim = cloog_domain_dimension(scattering) -
121 cloog_domain_dimension(domain);
122 return scattering->polyhedron->NbEq >= scattering_dim;
126 * cloog_domain_matrix2domain function:
127 * Given a matrix of constraints (matrix), this function constructs and returns
128 * the corresponding domain (i.e. the CloogDomain structure including the
129 * polyhedron with its double representation: constraint matrix and the set of
130 * rays).
132 CloogDomain * cloog_domain_matrix2domain(CloogMatrix * matrix)
133 { return (cloog_domain_alloc(Constraints2Polyhedron(matrix,MAX_RAYS))) ;
138 * cloog_domain_domain2matrix function:
139 * Given a polyhedron (in domain), this function returns its corresponding
140 * matrix of constraints.
142 CloogMatrix * cloog_domain_domain2matrix(CloogDomain * domain)
144 return cloog_matrix_matrix(Polyhedron2Constraints(domain->polyhedron));
147 CloogConstraintSet *cloog_domain_constraints(CloogDomain *domain)
149 return cloog_domain_domain2matrix(domain);
154 * cloog_domain_print function:
155 * This function prints the content of a CloogDomain structure (domain) into
156 * a file (foo, possibly stdout).
158 void cloog_domain_print(FILE * foo, CloogDomain * domain)
159 { Polyhedron_Print(foo,P_VALUE_FMT,domain->polyhedron) ;
160 fprintf(foo,"Number of active references: %d\n",domain->references) ;
163 void cloog_domain_print_constraints(FILE *foo, CloogDomain *domain,
164 int print_number)
166 Polyhedron *polyhedron;
167 CloogMatrix *matrix;
169 if (print_number) {
170 int j = 0;
171 /* Number of polyhedron inside the union of disjoint polyhedra. */
172 for (polyhedron = cloog_domain_polyhedron(domain); polyhedron;
173 polyhedron = polyhedron->next)
174 ++j;
175 fprintf(foo, "%d\n", j);
178 /* The polyhedra themselves. */
179 for (polyhedron = cloog_domain_polyhedron(domain); polyhedron;
180 polyhedron = polyhedron->next) {
181 matrix = cloog_matrix_matrix(Polyhedron2Constraints(polyhedron));
182 cloog_matrix_print(foo,matrix);
183 cloog_matrix_free(matrix);
188 * cloog_polyhedron_print function:
189 * This function prints the content of a Polyhedron structure (polyhedron) into
190 * a file (foo, possibly stdout). Just there as a development facility.
192 void cloog_polyhedron_print(FILE * foo, Polyhedron * polyhedron)
193 { Polyhedron_Print(foo,P_VALUE_FMT,polyhedron) ;
198 * cloog_domain_free function:
199 * This function frees the allocated memory for a CloogDomain structure
200 * (domain). It decrements the number of active references to this structure,
201 * if there are no more references on the structure, it frees it (with the
202 * included list of polyhedra).
204 void cloog_domain_free(CloogDomain * domain)
205 { if (domain != NULL)
206 { domain->references -- ;
208 if (domain->references == 0)
209 { if (domain->polyhedron != NULL)
210 { cloog_domain_leak_down() ;
211 Domain_Free(domain->polyhedron) ;
213 free(domain) ;
218 void cloog_scattering_free(CloogDomain * domain)
220 cloog_domain_free(domain);
225 * cloog_domain_copy function:
226 * This function returns a copy of a CloogDomain structure (domain). To save
227 * memory this is not a memory copy but we increment a counter of active
228 * references inside the structure, then return a pointer to that structure.
230 CloogDomain * cloog_domain_copy(CloogDomain * domain)
231 { domain->references ++ ;
232 return domain ;
237 * cloog_domain_image function:
238 * This function returns a CloogDomain structure such that the included
239 * polyhedral domain is computed from the former one into another
240 * domain according to a given affine mapping function (mapping).
242 CloogDomain * cloog_domain_image(CloogDomain * domain, CloogMatrix * mapping)
243 { return (cloog_domain_alloc(DomainImage(domain->polyhedron,mapping,MAX_RAYS)));
248 * cloog_domain_preimage function:
249 * Given a polyhedral domain (polyhedron) inside a CloogDomain structure and a
250 * mapping function (mapping), this function returns a new CloogDomain structure
251 * with a polyhedral domain which when transformed by mapping function (mapping)
252 * gives (polyhedron).
254 CloogDomain * cloog_domain_preimage(CloogDomain * domain, CloogMatrix * mapping)
255 { return (cloog_domain_alloc(DomainPreimage(domain->polyhedron,
256 mapping,MAX_RAYS))) ;
261 * cloog_domain_convex function:
262 * Given a polyhedral domain (polyhedron), this function concatenates the lists
263 * of rays and lines of the two (or more) polyhedra in the domain into one
264 * combined list, and find the set of constraints which tightly bound all of
265 * those objects. It returns the corresponding polyhedron.
267 CloogDomain * cloog_domain_convex(CloogDomain * domain)
268 { return (cloog_domain_alloc(DomainConvex(domain->polyhedron,MAX_RAYS)));
273 * cloog_domain_simplified_hull:
274 * Given a list (union) of polyhedra, this function returns a single
275 * polyhedron that contains this union and uses only contraints that
276 * appear in one or more of the polyhedra in the list.
278 * We simply iterate over all constraints of all polyhedra and test
279 * whether all rays of the other polyhedra satisfy/saturate the constraint.
281 static CloogDomain *cloog_domain_simplified_hull(CloogDomain * domain)
283 int dim = cloog_domain_dimension(domain);
284 int i, j, k, l;
285 int nb_pol = 0, nb_constraints = 0;
286 Polyhedron *P;
287 CloogMatrix **rays, *matrix;
288 Value tmp;
289 CloogDomain *bounds;
291 value_init(tmp);
292 for (P = domain->polyhedron; P; P = P->next) {
293 ++nb_pol;
294 nb_constraints += P->NbConstraints;
296 matrix = cloog_matrix_alloc(nb_constraints, 1 + dim + 1);
297 nb_constraints = 0;
298 rays = (CloogMatrix **)malloc(nb_pol * sizeof(CloogMatrix*));
299 for (P = domain->polyhedron, i = 0; P; P = P->next, ++i)
300 rays[i] = Polyhedron2Rays(P);
302 for (P = domain->polyhedron, i = 0; P; P = P->next, ++i) {
303 CloogMatrix *constraints = Polyhedron2Constraints(P);
304 for (j = 0; j < constraints->NbRows; ++j) {
305 for (k = 0; k < nb_pol; ++k) {
306 if (i == k)
307 continue;
308 for (l = 0; l < rays[k]->NbRows; ++l) {
309 Inner_Product(constraints->p[j]+1, rays[k]->p[l]+1, dim+1, &tmp);
310 if (value_neg_p(tmp))
311 break;
312 if ((value_zero_p(constraints->p[j][0]) ||
313 value_zero_p(rays[k]->p[l][0])) && value_pos_p(tmp))
314 break;
316 if (l < rays[k]->NbRows)
317 break;
319 if (k == nb_pol)
320 Vector_Copy(constraints->p[j], matrix->p[nb_constraints++], 1+dim+1);
322 Matrix_Free(constraints);
325 for (P = domain->polyhedron, i = 0; P; P = P->next, ++i)
326 Matrix_Free(rays[i]);
327 free(rays);
328 value_clear(tmp);
330 matrix->NbRows = nb_constraints;
331 bounds = cloog_domain_matrix2domain(matrix);
332 cloog_matrix_free(matrix);
334 return bounds;
339 * cloog_domain_simple_convex:
340 * Given a list (union) of polyhedra, this function returns a "simple"
341 * convex hull of this union. In particular, the constraints of the
342 * the returned polyhedron consist of (parametric) lower and upper
343 * bounds on individual variables and constraints that appear in the
344 * original polyhedra.
346 * nb_par is the number of parameters of the domain.
348 CloogDomain * cloog_domain_simple_convex(CloogDomain * domain, int nb_par)
350 int i;
351 int dim = cloog_domain_dimension(domain) - nb_par;
352 CloogDomain *convex = NULL;
354 if (cloog_domain_isconvex(domain))
355 return cloog_domain_copy(domain);
357 for (i = 0; i < dim; ++i) {
358 CloogDomain *bounds = cloog_domain_bounds(domain, i, nb_par);
360 if (!convex)
361 convex = bounds;
362 else {
363 CloogDomain *temp = cloog_domain_intersection(convex, bounds);
364 cloog_domain_free(bounds);
365 cloog_domain_free(convex);
366 convex = temp;
369 if (dim > 1) {
370 CloogDomain *temp, *bounds;
372 bounds = cloog_domain_simplified_hull(domain);
373 temp = cloog_domain_intersection(convex, bounds);
374 cloog_domain_free(bounds);
375 cloog_domain_free(convex);
376 convex = temp;
379 return convex;
384 * cloog_domain_simplify function:
385 * Given two polyhedral domains (pol1) and (pol2) inside two CloogDomain
386 * structures, this function finds the largest domain set (or the smallest list
387 * of non-redundant constraints), that when intersected with polyhedral
388 * domain (pol2) equals (Pol1)intersect(Pol2). The output is a new CloogDomain
389 * structure with a polyhedral domain with the "redundant" constraints removed.
390 * NB: this function do not work as expected with unions of polyhedra...
392 CloogDomain * cloog_domain_simplify(CloogDomain * dom1, CloogDomain * dom2)
394 CloogMatrix *M, *M2;
395 CloogDomain *dom;
396 Polyhedron *P = dom1->polyhedron;
398 /* DomainSimplify doesn't remove all redundant equalities,
399 * so we remove them here first in case both dom1 and dom2
400 * are single polyhedra (i.e., not unions of polyhedra).
402 if (!dom1->polyhedron->next && !dom2->polyhedron->next &&
403 P->NbEq && dom2->polyhedron->NbEq) {
404 int i, row;
405 int rows = P->NbEq + dom2->polyhedron->NbEq;
406 int cols = P->Dimension+2;
407 int rank;
408 M = cloog_matrix_alloc(rows, cols);
409 M2 = cloog_matrix_alloc(P->NbConstraints, cols);
410 Vector_Copy(dom2->polyhedron->Constraint[0], M->p[0],
411 dom2->polyhedron->NbEq * cols);
412 rank = dom2->polyhedron->NbEq;
413 row = 0;
414 for (i = 0; i < P->NbEq; ++i) {
415 Vector_Copy(P->Constraint[i], M->p[rank], cols);
416 if (Gauss(M, rank+1, cols-1) > rank) {
417 Vector_Copy(P->Constraint[i], M2->p[row++], cols);
418 rank++;
421 if (row < P->NbEq) {
422 if (P->NbConstraints > P->NbEq)
423 Vector_Copy(P->Constraint[P->NbEq], M2->p[row],
424 (P->NbConstraints - P->NbEq) * cols);
425 P = Constraints2Polyhedron(M2, MAX_RAYS);
427 cloog_matrix_free(M2);
428 cloog_matrix_free(M);
430 dom = cloog_domain_alloc(DomainSimplify(P, dom2->polyhedron,MAX_RAYS));
431 if (P != dom1->polyhedron)
432 Polyhedron_Free(P);
433 return dom;
438 * cloog_domain_union function:
439 * This function returns a new CloogDomain structure including a polyhedral
440 * domain which is the union of two polyhedral domains (pol1) U (pol2) inside
441 * two CloogDomain structures.
443 CloogDomain * cloog_domain_union(CloogDomain * dom1, CloogDomain * dom2)
444 { return (cloog_domain_alloc(DomainUnion(dom1->polyhedron,
445 dom2->polyhedron,MAX_RAYS))) ;
450 * cloog_domain_intersection function:
451 * This function returns a new CloogDomain structure including a polyhedral
452 * domain which is the intersection of two polyhedral domains (pol1)inter(pol2)
453 * inside two CloogDomain structures.
455 CloogDomain * cloog_domain_intersection(CloogDomain * dom1, CloogDomain * dom2)
456 { return (cloog_domain_alloc(DomainIntersection(dom1->polyhedron,
457 dom2->polyhedron,MAX_RAYS))) ;
462 * cloog_domain_difference function:
463 * This function returns a new CloogDomain structure including a polyhedral
464 * domain which is the difference of two polyhedral domains domain \ minus
465 * inside two CloogDomain structures.
466 * - November 8th 2001: first version.
468 CloogDomain * cloog_domain_difference(CloogDomain * domain, CloogDomain * minus)
469 { if (cloog_domain_isempty(minus))
470 return(cloog_domain_copy(domain)) ;
471 else
472 return (cloog_domain_alloc(DomainDifference(domain->polyhedron,
473 minus->polyhedron,MAX_RAYS))) ;
478 * cloog_domain_addconstraints function :
479 * This function adds source's polyhedron constraints to target polyhedron: for
480 * each element of the polyhedron inside 'target' (i.e. element of the union
481 * of polyhedra) it adds the constraints of the corresponding element in
482 * 'source'.
483 * - August 10th 2002: first version.
484 * Nota bene for future : it is possible that source and target don't have the
485 * same number of elements (try iftest2 without non-shared constraint
486 * elimination in cloog_loop_separate !). This function is yet another part
487 * of the DomainSimplify patching problem...
489 CloogDomain * cloog_domain_addconstraints(domain_source, domain_target)
490 CloogDomain * domain_source, * domain_target ;
491 { unsigned nb_constraint ;
492 Value * constraints ;
493 Polyhedron * source, * target, * new, * next, * last ;
495 source = domain_source->polyhedron ;
496 target = domain_target->polyhedron ;
498 constraints = source->p_Init ;
499 nb_constraint = source->NbConstraints ;
500 source = source->next ;
501 new = AddConstraints(constraints,nb_constraint,target,MAX_RAYS) ;
502 last = new ;
503 next = target->next ;
505 while (next != NULL)
506 { /* BUG !!! This is actually a bug. I don't know yet how to cleanly avoid
507 * the situation where source and target do not have the same number of
508 * elements. So this 'if' is an awful trick, waiting for better.
510 if (source != NULL)
511 { constraints = source->p_Init ;
512 nb_constraint = source->NbConstraints ;
513 source = source->next ;
515 last->next = AddConstraints(constraints,nb_constraint,next,MAX_RAYS) ;
516 last = last->next ;
517 next = next->next ;
520 return (cloog_domain_alloc(new)) ;
525 * cloog_domain_sort function:
526 * This function topologically sorts (nb_pols) polyhedra. Here (pols) is a an
527 * array of pointers to polyhedra, (nb_pols) is the number of polyhedra,
528 * (level) is the level to consider for partial ordering (nb_par) is the
529 * parameter space dimension, (permut) if not NULL, is an array of (nb_pols)
530 * integers that contains a permutation specification after call in order to
531 * apply the topological sorting.
533 void cloog_domain_sort(CloogDomain **doms, unsigned nb_doms, unsigned level,
534 unsigned nb_par, int *permut)
536 int i, *time;
537 Polyhedron **pols = (Polyhedron **) malloc(nb_doms * sizeof(Polyhedron *));
539 for (i = 0; i < nb_doms; i++)
540 pols[i] = cloog_domain_polyhedron(doms[i]);
542 /* time is an array of (nb_doms) integers to store logical time values. We
543 * do not use it, but it is compulsory for PolyhedronTSort.
545 time = (int *)malloc(nb_doms * sizeof(int));
547 /* PolyhedronTSort will fill up permut (and time). */
548 PolyhedronTSort(pols, nb_doms, level, nb_par, time, permut, MAX_RAYS);
550 free(pols);
551 free(time) ;
556 * cloog_domain_empty function:
557 * This function allocates the memory space for a CloogDomain structure and
558 * sets its polyhedron to an empty polyhedron with the same dimensions
559 * as template
560 * Then it returns a pointer to the allocated space.
561 * - June 10th 2005: first version.
563 CloogDomain * cloog_domain_empty(CloogDomain *template)
564 { return cloog_domain_alloc(Empty_Polyhedron(cloog_domain_dimension(template))) ;
568 /******************************************************************************
569 * Structure display function *
570 ******************************************************************************/
573 static void print_structure_prefix(FILE *file, int level)
575 int i;
577 for(i = 0; i < level; i++)
578 fprintf(file, "|\t");
583 * cloog_domain_print_structure :
584 * this function is a more human-friendly way to display the CloogDomain data
585 * structure, it only shows the constraint system and includes an indentation
586 * level (level) in order to work with others print_structure functions.
587 * Written by Olivier Chorier, Luc Marchaud, Pierre Martin and Romain Tartiere.
588 * - April 24th 2005: Initial version.
589 * - May 26th 2005: Memory leak hunt.
590 * - June 16th 2005: (Ced) Integration in domain.c.
592 void cloog_domain_print_structure(FILE *file, CloogDomain *domain, int level,
593 const char *name)
595 Polyhedron *P;
596 CloogMatrix * matrix ;
598 print_structure_prefix(file, level);
600 if (domain != NULL)
601 { fprintf(file,"+-- %s\n", name);
603 /* Print the matrix. */
604 for (P = domain->polyhedron; P; P = P->next) {
605 matrix = Polyhedron2Constraints(P);
606 cloog_matrix_print_structure(file, matrix, level);
607 cloog_matrix_free(matrix);
609 /* A blank line. */
610 print_structure_prefix(file, level+1);
611 fprintf(file,"\n");
614 else
615 fprintf(file,"+-- Null CloogDomain\n") ;
621 * cloog_scattering_list_print function:
622 * This function prints the content of a CloogScatteringList structure into a
623 * file (foo, possibly stdout).
624 * - November 6th 2001: first version.
626 void cloog_scattering_list_print(FILE * foo, CloogScatteringList * list)
627 { while (list != NULL)
628 { cloog_domain_print(foo, list->scatt);
629 list = list->next ;
634 /******************************************************************************
635 * Memory deallocation function *
636 ******************************************************************************/
640 * cloog_scattering_list_free function:
641 * This function frees the allocated memory for a CloogScatteringList structure.
642 * - November 6th 2001: first version.
644 void cloog_scattering_list_free(CloogScatteringList * list)
645 { CloogScatteringList * temp ;
647 while (list != NULL)
648 { temp = list->next ;
649 cloog_scattering_free(list->scatt);
650 free(list) ;
651 list = temp ;
656 /******************************************************************************
657 * Reading function *
658 ******************************************************************************/
662 * cloog_domain_read function:
663 * Adaptation from the PolyLib. This function reads a matrix into a file (foo,
664 * posibly stdin) and returns a pointer to a polyhedron containing the read
665 * information.
666 * - October 18th 2001: first version.
668 CloogDomain * cloog_domain_read(FILE * foo, int nb_parameters,
669 CloogOptions *options)
670 { CloogMatrix * matrix ;
671 CloogDomain * domain ;
673 matrix = cloog_matrix_read(foo) ;
674 domain = cloog_domain_matrix2domain(matrix) ;
675 cloog_matrix_free(matrix) ;
677 return(domain) ;
682 * cloog_domain_read_context:
683 * Read parameter domain. For the PolyLib backend, a parameter domain
684 * is indistinguishable from a parametric domain.
686 CloogDomain *cloog_domain_read_context(FILE * foo, CloogOptions *options)
688 return cloog_domain_read(foo, 0, options);
693 * cloog_domain_from_context
694 * Reinterpret context by turning parameters into variables.
695 * For the PolyLib backend, this has no effect.
697 CloogDomain *cloog_domain_from_context(CloogDomain *context)
699 return context;
704 * cloog_domain_union_read function:
705 * This function reads a union of polyhedra into a file (foo, posibly stdin) and
706 * returns a pointer to a Polyhedron containing the read information.
707 * - September 9th 2002: first version.
708 * - October 29th 2005: (debug) removal of a leak counting "correction" that
709 * was just false since ages.
711 CloogDomain * cloog_domain_union_read(FILE * foo, int nb_parameters,
712 CloogOptions *options)
713 { int i, nb_components ;
714 char s[MAX_STRING] ;
715 CloogDomain * domain, * temp, * old ;
717 /* domain reading: nb_components (constraint matrices). */
718 while (fgets(s,MAX_STRING,foo) == 0) ;
719 while ((*s=='#' || *s=='\n') || (sscanf(s," %d",&nb_components)<1))
720 fgets(s,MAX_STRING,foo) ;
722 if (nb_components > 0)
723 { /* 1. first part of the polyhedra union, */
724 domain = cloog_domain_read(foo, nb_parameters, options);
725 /* 2. and the nexts. */
726 for (i=1;i<nb_components;i++)
727 { /* Leak counting is OK since next allocated domain is freed here. */
728 temp = cloog_domain_read(foo, nb_parameters, options);
729 old = domain ;
730 domain = cloog_domain_union(temp,old) ;
731 cloog_domain_free(temp) ;
732 cloog_domain_free(old) ;
734 return domain ;
736 else
737 return NULL ;
742 * cloog_scattering_read function:
743 * This function reads in a scattering function fro the file foo.
745 CloogScattering *cloog_scattering_read(FILE *foo,
746 CloogDomain *domain, CloogOptions *options)
748 /* The PolyLib backend doesn't need to know the number of parameters. */
749 return cloog_domain_read(foo, -1, options);
753 /******************************************************************************
754 * Processing functions *
755 ******************************************************************************/
759 * cloog_domain_malloc function:
760 * This function allocates the memory space for a CloogDomain structure and
761 * sets its fields with default values. Then it returns a pointer to the
762 * allocated space.
763 * - November 21th 2005: first version.
765 CloogDomain * cloog_domain_malloc()
766 { CloogDomain * domain ;
768 domain = (CloogDomain *)malloc(sizeof(CloogDomain)) ;
769 if (domain == NULL)
770 cloog_die("memory overflow.\n");
771 cloog_domain_leak_up() ;
773 /* We set the various fields with default values. */
774 domain->polyhedron = NULL ;
775 domain->references = 1 ;
777 return domain ;
782 * cloog_domain_alloc function:
783 * This function allocates the memory space for a CloogDomain structure and
784 * sets its fields with those given as input. Then it returns a pointer to the
785 * allocated space.
786 * - April 19th 2005: first version.
787 * - November 21th 2005: cloog_domain_malloc use.
789 CloogDomain * cloog_domain_alloc(Polyhedron * polyhedron)
790 { CloogDomain * domain ;
792 if (polyhedron == NULL)
793 return NULL ;
794 else
795 { domain = cloog_domain_malloc() ;
796 domain->polyhedron = polyhedron ;
798 return domain ;
804 * cloog_domain_isempty function:
805 * This function returns 1 if the polyhedron given as input is empty, 0
806 * otherwise.
807 * - October 28th 2001: first version.
809 int cloog_domain_isempty(CloogDomain * domain)
810 { if (!domain || domain->polyhedron == NULL)
811 return 1 ;
813 if (domain->polyhedron->next)
814 return(0) ;
815 return((domain->polyhedron->Dimension < domain->polyhedron->NbEq) ? 1 : 0) ;
820 * cloog_domain_universe function:
821 * This function returns the complete dim-dimensional space.
823 CloogDomain *cloog_domain_universe(unsigned dim, CloogOptions *options)
825 return cloog_domain_alloc(Universe_Polyhedron(dim));
830 * cloog_domain_project function:
831 * From Quillere's LoopGen 0.4. This function returns the projection of
832 * (domain) on the (level) first dimensions (i.e. outer loops). It returns a
833 * pointer to the projected Polyhedron.
834 * - nb_par is the number of parameters.
836 * - October 27th 2001: first version.
837 * - June 21rd 2005: Adaptation for GMP (based on S. Verdoolaege's version of
838 * CLooG 0.12.1).
840 CloogDomain * cloog_domain_project(CloogDomain * domain, int level, int nb_par)
841 { int row, column, nb_rows, nb_columns, difference ;
842 CloogDomain * projected_domain ;
843 CloogMatrix * matrix ;
845 nb_rows = level + nb_par + 1 ;
846 nb_columns = domain->polyhedron->Dimension + 1 ;
847 difference = nb_columns - nb_rows ;
849 if (difference == 0)
850 return(cloog_domain_copy(domain)) ;
852 matrix = cloog_matrix_alloc(nb_rows,nb_columns) ;
854 for (row=0;row<level;row++)
855 for (column=0;column<nb_columns; column++)
856 value_set_si(matrix->p[row][column],(row == column ? 1 : 0)) ;
858 for (;row<nb_rows;row++)
859 for (column=0;column<nb_columns;column++)
860 value_set_si(matrix->p[row][column],(row+difference == column ? 1 : 0)) ;
862 projected_domain = cloog_domain_image(domain,matrix) ;
863 cloog_matrix_free(matrix) ;
865 return(projected_domain) ;
870 * cloog_domain_bounds:
871 * Given a list (union) of polyhedra "domain", this function returns a single
872 * polyhedron with constraints that reflect the (parametric) lower and
873 * upper bound on dimension "dim".
875 * nb_par is the number of parameters of the domain.
877 CloogDomain * cloog_domain_bounds(CloogDomain * domain, int dim, int nb_par)
879 int row, nb_rows, nb_columns, difference;
880 CloogDomain * projected_domain, *extended_domain, *bounds;
881 CloogMatrix * matrix ;
883 nb_rows = 1 + nb_par + 1;
884 nb_columns = domain->polyhedron->Dimension + 1 ;
885 difference = nb_columns - nb_rows ;
887 if (difference == 0)
888 return(cloog_domain_convex(domain));
890 matrix = cloog_matrix_alloc(nb_rows, nb_columns);
892 value_set_si(matrix->p[0][dim], 1);
893 for (row = 1; row < nb_rows; row++)
894 value_set_si(matrix->p[row][row+difference], 1);
896 projected_domain = cloog_domain_image(domain,matrix) ;
897 extended_domain = cloog_domain_preimage(projected_domain, matrix);
898 cloog_domain_free(projected_domain);
899 cloog_matrix_free(matrix) ;
900 bounds = cloog_domain_convex(extended_domain);
901 cloog_domain_free(extended_domain);
903 return bounds;
908 * cloog_domain_extend function:
909 * From Quillere's LoopGen 0.4. This function returns the (domain) given as
910 * input with (dim)+(nb_par) dimensions. The new dimensions are added before
911 * the (nb_par) parameters. This function does not free (domain), and returns
912 * a new polyhedron.
913 * - nb_par is the number of parameters.
915 * - October 27th 2001: first version.
916 * - June 21rd 2005: Adaptation for GMP (based on S. Verdoolaege's version of
917 * CLooG 0.12.1).
919 CloogDomain * cloog_domain_extend(CloogDomain * domain, int dim, int nb_par)
920 { int row, column, nb_rows, nb_columns, difference ;
921 CloogDomain * extended_domain ;
922 CloogMatrix * matrix ;
924 nb_rows = 1 + domain->polyhedron->Dimension ;
925 nb_columns = dim + nb_par + 1 ;
926 difference = nb_columns - nb_rows ;
928 if (difference == 0)
929 return(cloog_domain_copy(domain)) ;
931 matrix = cloog_matrix_alloc(nb_rows,nb_columns) ;
933 for (row=0;row<domain->polyhedron->Dimension-nb_par;row++)
934 for (column=0;column<nb_columns;column++)
935 value_set_si(matrix->p[row][column],(row == column ? 1 : 0)) ;
937 for (;row<=domain->polyhedron->Dimension;row++)
938 for (column=0;column<nb_columns;column++)
939 value_set_si(matrix->p[row][column],(row+difference == column ? 1 : 0)) ;
941 extended_domain = cloog_domain_preimage(domain,matrix) ;
942 cloog_matrix_free(matrix) ;
944 return(extended_domain) ;
949 * cloog_domain_never_integral function:
950 * For us, an equality like 3*i -4 = 0 is always false since 4%3 != 0. This
951 * function returns a boolean set to 1 if there is this kind of 'never true'
952 * constraint inside a polyhedron, 0 otherwise.
953 * - domain is the polyhedron to check,
955 * - November 28th 2001: first version.
956 * - June 26th 2003: for iterators, more 'never true' constraints are found
957 * (compare cholesky2 and vivien with a previous version),
958 * checking for the parameters created (compare using vivien).
959 * - June 28th 2003: Previously in loop.c and called
960 * cloog_loop_simplify_nevertrue, now here !
961 * - June 21rd 2005: Adaptation for GMP (based on S. Verdoolaege's version of
962 * CLooG 0.12.1).
963 * - October 14th 2005: Complete rewriting, not faster but code quite shorter.
965 int cloog_domain_never_integral(CloogDomain * domain)
966 { int i, dimension ;
967 Value gcd, modulo ;
968 Polyhedron * polyhedron ;
970 if ((domain == NULL) || (domain->polyhedron == NULL))
971 return 1 ;
973 value_init_c(gcd) ;
974 value_init_c(modulo) ;
975 polyhedron = domain->polyhedron ;
976 dimension = polyhedron->Dimension + 2 ;
978 /* For each constraint... */
979 for (i=0; i<polyhedron->NbConstraints; i++)
980 { /* If we have an equality and the scalar part is not zero... */
981 if (value_zero_p(polyhedron->Constraint[i][0]) &&
982 value_notzero_p(polyhedron->Constraint[i][dimension-1]))
983 { /* Then we check whether the scalar can be divided by the gcd of the
984 * unknown vector (including iterators and parameters) or not. If not,
985 * there is no integer point in the polyhedron and we return 1.
987 Vector_Gcd(&(polyhedron->Constraint[i][1]),dimension-2,&gcd) ;
988 value_modulus(modulo,polyhedron->Constraint[i][dimension-1],gcd) ;
990 if (value_notzero_p(modulo))
991 { value_clear_c(gcd) ;
992 value_clear_c(modulo) ;
993 return 1 ;
998 value_clear_c(gcd) ;
999 value_clear_c(modulo) ;
1000 return(0) ;
1005 * cloog_domain_stride function:
1006 * This function finds the stride imposed to unknown with the column number
1007 * 'strided_level' in order to be integral. For instance, if we have a
1008 * constraint like -i - 2j + 2k = 0, and we consider k, then k can be integral
1009 * only if (i + 2j)%2 = 0. Then only if i%2 = 0. Then k imposes a stride 2 to
1010 * the unknown i. The function returns the imposed stride in a parameter field.
1011 * - domain is the set of constraint we have to consider,
1012 * - strided_level is the column number of the unknown for which a stride have
1013 * to be found,
1014 * - looking_level is the column number of the unknown that impose a stride to
1015 * the first unknown.
1016 * - stride is the stride that is returned back as a function parameter.
1017 * - offset is the value of the constant c if the condition is of the shape
1018 * (i + c)%s = 0, s being the stride.
1020 * - June 28th 2003: first version.
1021 * - July 14th 2003: can now look for multiple striding constraints and returns
1022 * the GCD of the strides and the common offset.
1023 * - June 21rd 2005: Adaptation for GMP (based on S. Verdoolaege's version of
1024 * CLooG 0.12.1).
1026 void cloog_domain_stride(domain, strided_level, nb_par, stride, offset)
1027 CloogDomain * domain ;
1028 int strided_level, nb_par ;
1029 Value * stride, * offset;
1030 { int i, dimension;
1031 Polyhedron * polyhedron ;
1032 int n_col, n_row, rank;
1033 CloogMatrix *M;
1034 Matrix *U;
1035 Vector *V;
1037 polyhedron = domain->polyhedron ;
1038 dimension = polyhedron->Dimension ;
1040 /* Look at all equalities involving strided_level and the inner
1041 * iterators. We can ignore the outer iterators and the parameters
1042 * here because the lower bound on strided_level is assumed to
1043 * be a constant.
1045 n_col = (1+dimension-nb_par) - strided_level;
1046 for (i=0, n_row=0; i < polyhedron->NbEq; i++)
1047 if (First_Non_Zero(polyhedron->Constraint[i]+strided_level, n_col) != -1)
1048 ++n_row;
1050 M = cloog_matrix_alloc(n_row+1, n_col+1);
1051 for (i=0, n_row = 0; i < polyhedron->NbEq; i++) {
1052 if (First_Non_Zero(polyhedron->Constraint[i]+strided_level, n_col) == -1)
1053 continue;
1054 Vector_Copy(polyhedron->Constraint[i]+strided_level, M->p[n_row], n_col);
1055 value_assign(M->p[n_row][n_col], polyhedron->Constraint[i][1+dimension]);
1056 ++n_row;
1058 value_set_si(M->p[n_row][n_col], 1);
1060 /* Then look at the general solution to the above equalities. */
1061 rank = SolveDiophantine(M, &U, &V);
1062 cloog_matrix_free(M);
1064 if (rank == -1) {
1065 /* There is no solution, so the body of this loop will
1066 * never execute. We just leave the constraints alone here so
1067 * that they will ensure the body will not be executed.
1068 * We should probably propagate this information up so that
1069 * the loop can be removed entirely.
1071 value_set_si(*offset, 0);
1072 value_set_si(*stride, 1);
1073 } else {
1074 /* Compute the gcd of the coefficients defining strided_level. */
1075 Vector_Gcd(U->p[0], U->NbColumns, stride);
1076 value_oppose(*offset, V->p[0]);
1077 value_pmodulus(*offset, *offset, *stride);
1079 Matrix_Free(U);
1080 Vector_Free(V);
1082 return ;
1087 * cloog_domain_integral_lowerbound function:
1088 * This function returns 1 if the lower bound of an iterator (such as its
1089 * column rank in the constraint set 'domain' is 'level') is integral,
1090 * 0 otherwise. If the lower bound is actually integral, the function fills
1091 * the 'lower' field with the lower bound value.
1092 * - June 29th 2003: first version.
1093 * - June 21rd 2005: Adaptation for GMP (based on S. Verdoolaege's version of
1094 * CLooG 0.12.1).
1096 int cloog_domain_integral_lowerbound(domain, level, lower)
1097 CloogDomain * domain ;
1098 int level ;
1099 Value * lower ;
1100 { int i, first_lower=1, dimension, lower_constraint=-1 ;
1101 Value iterator, constant, tmp;
1102 Polyhedron * polyhedron ;
1104 polyhedron = domain->polyhedron ;
1105 dimension = polyhedron->Dimension ;
1107 /* We want one and only one lower bound (e.g. no equality, no maximum
1108 * calculation...).
1110 for (i=0; i<polyhedron->NbConstraints; i++)
1111 if (value_zero_p(polyhedron->Constraint[i][0]) &&
1112 value_notzero_p(polyhedron->Constraint[i][level]))
1113 return 0 ;
1115 for (i=0; i<polyhedron->NbConstraints; i++)
1116 if (value_pos_p(polyhedron->Constraint[i][level]))
1117 { if (first_lower)
1118 { first_lower = 0 ;
1119 lower_constraint = i ;
1121 else
1122 return 0 ;
1124 if (first_lower)
1125 return 0 ;
1127 /* We want an integral lower bound: no other non-zero entry except the
1128 * iterator coefficient and the constant.
1130 for (i=1; i<level; i++)
1131 if (value_notzero_p(polyhedron->Constraint[lower_constraint][i]))
1132 return 0 ;
1133 for (i=level+1; i<=polyhedron->Dimension; i++)
1134 if (value_notzero_p(polyhedron->Constraint[lower_constraint][i]))
1135 return 0 ;
1137 value_init_c(iterator) ;
1138 value_init_c(constant) ;
1139 value_init_c(tmp) ;
1141 /* If all is passed, then find the lower bound and return 1. */
1142 value_assign(iterator, polyhedron->Constraint[lower_constraint][level]) ;
1143 value_oppose(constant, polyhedron->Constraint[lower_constraint][dimension+1]);
1145 value_modulus(tmp, constant, iterator) ;
1146 value_division(*lower, constant, iterator) ;
1148 if (!(value_zero_p(tmp) || value_neg_p(constant)))
1149 value_increment(*lower, *lower) ;
1151 value_clear_c(iterator) ;
1152 value_clear_c(constant) ;
1153 value_clear_c(tmp) ;
1155 return 1 ;
1160 * cloog_domain_lowerbound_update function:
1161 * This function updates the integral lower bound of an iterator (such as its
1162 * column rank in the constraint set 'domain' is 'level') into 'lower'
1163 * and returns the updated domain.
1164 * - Jun 29th 2003: first version.
1165 * - June 21rd 2005: Adaptation for GMP (based on S. Verdoolaege's version of
1166 * CLooG 0.12.1).
1168 CloogDomain *cloog_domain_lowerbound_update(CloogDomain *domain, int level,
1169 cloog_int_t lower)
1170 { int i ;
1171 Polyhedron * polyhedron ;
1173 polyhedron = domain->polyhedron ;
1175 /* There is only one lower bound, the first one is the good one. */
1176 for (i=0; i<polyhedron->NbConstraints; i++)
1177 if (value_pos_p(polyhedron->Constraint[i][level]))
1178 { value_set_si(polyhedron->Constraint[i][level], 1) ;
1179 value_oppose(polyhedron->Constraint[i][polyhedron->Dimension+1], lower) ;
1180 break ;
1182 return domain;
1187 * cloog_domain_lazy_equal function:
1188 * This function returns 1 if the domains given as input are the same, 0 if it
1189 * is unable to decide. This function makes an entry-to-entry comparison between
1190 * the constraint systems, if all the entries are the same, the domains are
1191 * obviously the same and it returns 1, at the first difference, it returns 0.
1192 * This is a very fast way to verify this property. It has been shown (with the
1193 * CLooG benchmarks) that operations on equal domains are 17% of all the
1194 * polyhedral computations. For 75% of the actually identical domains, this
1195 * function answer that they are the same and allow to give immediately the
1196 * trivial solution instead of calling the heavy general functions of PolyLib.
1197 * - August 22th 2003: first version.
1198 * - June 21rd 2005: Adaptation for GMP (based on S. Verdoolaege's version of
1199 * CLooG 0.12.1).
1201 int cloog_domain_lazy_equal(CloogDomain * d1, CloogDomain * d2)
1202 { int i, nb_elements ;
1203 Polyhedron * p1, * p2 ;
1205 p1 = d1->polyhedron ;
1206 p2 = d2->polyhedron ;
1208 while ((p1 != NULL) && (p2 != NULL))
1209 { if ((p1->NbConstraints != p2->NbConstraints) ||
1210 (p1->Dimension != p2->Dimension))
1211 return 0 ;
1213 nb_elements = p1->NbConstraints * (p1->Dimension + 2) ;
1215 for (i=0;i<nb_elements;i++)
1216 if (value_ne(p1->p_Init[i], p2->p_Init[i]))
1217 return 0 ;
1219 p1 = p1->next ;
1220 p2 = p2->next ;
1223 if ((p1 != NULL) || (p2 != NULL))
1224 return 0 ;
1226 return 1 ;
1231 * cloog_scattering_lazy_block function:
1232 * This function returns 1 if the two domains d1 and d2 given as input are the
1233 * same (possibly except for a dimension equal to a constant where we accept
1234 * a difference of 1) AND if we are sure that there are no other domain in
1235 * the code generation problem that may put integral points between those of
1236 * d1 and d2 (0 otherwise). In fact this function answers the question "can I
1237 * safely consider the two domains as only one with two statements (a block) ?".
1238 * The original implementation had a problem and has therefore been
1239 * (temporarily) replaced by the safest possible implementation: always
1240 * assume that we cannot block the two statements.
1241 * - d1 and d2 are the two domains to check for blocking,
1242 * - scattering is the linked list of all domains,
1243 * - scattdims is the total number of scattering dimentions.
1245 int cloog_scattering_lazy_block(CloogScattering *d1, CloogScattering *d2,
1246 CloogScatteringList *scattering, int scattdims)
1248 return 0;
1253 * cloog_domain_lazy_disjoint function:
1254 * This function returns 1 if the domains given as input are disjoint, 0 if it
1255 * is unable to decide. This function finds the unknown with fixed values in
1256 * both domains (on a given constraint, their column entry is not zero and
1257 * only the constant coefficient can be different from zero) and verify that
1258 * their values are the same. If not, the domains are obviously disjoint and
1259 * it returns 1, if there is not such case it returns 0. This is a very fast
1260 * way to verify this property. It has been shown (with the CLooG benchmarks)
1261 * that operations on disjoint domains are 36% of all the polyhedral
1262 * computations. For 94% of the actually identical domains, this
1263 * function answer that they are disjoint and allow to give immediately the
1264 * trivial solution instead of calling the heavy general functions of PolyLib.
1265 * - August 22th 2003: first version.
1266 * - June 21rd 2005: Adaptation for GMP (based on S. Verdoolaege's version of
1267 * CLooG 0.12.1).
1269 int cloog_domain_lazy_disjoint(CloogDomain * d1, CloogDomain * d2)
1270 { int i1, j1, i2, j2, scat_dim ;
1271 Value scat_val ;
1272 Polyhedron * p1, * p2 ;
1274 p1 = d1->polyhedron ;
1275 p2 = d2->polyhedron ;
1277 if ((p1->next != NULL) || (p2->next != NULL))
1278 return 0 ;
1280 value_init_c(scat_val) ;
1282 for (i1=0; i1<p1->NbConstraints; i1++)
1283 { if (value_notzero_p(p1->Constraint[i1][0]))
1284 continue ;
1286 scat_dim = 1 ;
1287 while (value_zero_p(p1->Constraint[i1][scat_dim]) &&
1288 (scat_dim < p1->Dimension))
1289 scat_dim ++ ;
1291 if (value_notone_p(p1->Constraint[i1][scat_dim]))
1292 continue ;
1293 else
1294 { for (j1=scat_dim+1; j1<=p1->Dimension; j1++)
1295 if (value_notzero_p(p1->Constraint[i1][j1]))
1296 break ;
1298 if (j1 != p1->Dimension+1)
1299 continue ;
1301 value_assign(scat_val,p1->Constraint[i1][p1->Dimension+1]) ;
1303 for (i2=0; i2<p2->NbConstraints; i2++)
1304 { for (j2=0;j2<scat_dim;j2++)
1305 if (value_notzero_p(p2->Constraint[i2][j2]))
1306 break ;
1308 if ((j2 != scat_dim) || value_notone_p(p2->Constraint[i2][scat_dim]))
1309 continue ;
1311 for (j2=scat_dim+1; j2<=p2->Dimension; j2++)
1312 if (value_notzero_p(p2->Constraint[i2][j2]))
1313 break ;
1315 if (j2 != p2->Dimension+1)
1316 continue ;
1318 if (value_ne(p2->Constraint[i2][p2->Dimension+1],scat_val))
1319 { value_clear_c(scat_val) ;
1320 return 1 ;
1326 value_clear_c(scat_val) ;
1327 return 0 ;
1332 * cloog_scattering_list_lazy_same function:
1333 * This function returns 1 if two domains in the list are the same, 0 if it
1334 * is unable to decide.
1335 * - February 9th 2004: first version.
1337 int cloog_scattering_list_lazy_same(CloogScatteringList * list)
1338 { /*int i=1, j=1 ;*/
1339 CloogScatteringList * current, * next ;
1341 current = list ;
1342 while (current != NULL)
1343 { next = current->next ;
1344 /*j=i+1;*/
1345 while (next != NULL)
1346 { if (cloog_domain_lazy_equal(current->scatt, next->scatt))
1347 { /*printf("Same domains: %d and %d\n",i,j) ;*/
1348 return 1 ;
1350 /*j++ ;*/
1351 next = next->next ;
1353 /*i++ ;*/
1354 current = current->next ;
1357 return 0 ;
1362 * Those functions are provided for "object encapsulation", to separate as much
1363 * as possible the inside of the CloogDomain structure from the rest of the
1364 * program, in order to ease the change of polyhedral library. For efficiency
1365 * reasons, they are defined and used as macros in domain.h.
1366 * - April 20th 2005: setting.
1368 Polyhedron * cloog_domain_polyhedron(CloogDomain * domain)
1369 { return domain->polyhedron ;
1372 int cloog_domain_nbconstraints(CloogDomain * domain)
1373 { return domain->polyhedron->NbConstraints ;
1377 int cloog_domain_dimension(CloogDomain * domain)
1378 { return domain->polyhedron->Dimension ;
1381 int cloog_scattering_dimension(CloogScattering *scatt, CloogDomain *domain)
1383 return cloog_domain_dimension(scatt) - cloog_domain_dimension(domain);
1386 int cloog_domain_isconvex(CloogDomain * domain)
1387 { return (domain->polyhedron->next == NULL)? 1 : 0 ;
1392 * cloog_domain_cut_first function:
1393 * This function splits off and returns the first convex set in the
1394 * union "domain". The remainder of the union is returned in rest.
1395 * The original "domain" itself is destroyed and may not be used
1396 * after a call to this function.
1398 CloogDomain *cloog_domain_cut_first(CloogDomain *domain, CloogDomain **rest)
1400 if (!domain || !domain->polyhedron || cloog_domain_isconvex(domain)) {
1401 *rest = NULL;
1402 return domain;
1405 if (domain->references == 1) {
1406 *rest = cloog_domain_alloc(domain->polyhedron->next);
1407 domain->polyhedron->next = NULL ;
1408 return domain;
1411 cloog_domain_free(domain);
1412 *rest = cloog_domain_alloc(Domain_Copy(domain->polyhedron->next));
1413 return cloog_domain_alloc(Polyhedron_Copy(domain->polyhedron));
1418 * Given a union domain, try to find a simpler representation
1419 * using fewer sets in the union.
1420 * Since PolyLib does not have a proper implementation for this
1421 * functionality, we compute
1422 * convex(domain) \ (convex(domain) \ domain)
1423 * which usually approximates what we want.
1424 * The original "domain" itself is destroyed and may not be used
1425 * after a call to this function.
1427 CloogDomain *cloog_domain_simplify_union(CloogDomain *domain)
1429 CloogDomain *convex, *temp;
1431 convex = cloog_domain_convex(domain);
1432 temp = cloog_domain_difference(convex, domain);
1433 cloog_domain_free(domain);
1434 domain = cloog_domain_difference(convex, temp);
1435 cloog_domain_free(convex);
1436 cloog_domain_free(temp);
1438 return domain;
1442 static int polyhedron_lazy_isconstant(Polyhedron *polyhedron, int dimension,
1443 cloog_int_t *value)
1445 int i, j;
1447 /* For each constraint... */
1448 for (i=0;i<polyhedron->NbConstraints;i++)
1449 { /* ...if it is concerned by the potentially scalar dimension... */
1450 if (value_notzero_p(polyhedron->Constraint[i][dimension+1]))
1451 { /* ...check that the constraint has the shape "dimension + scalar = 0". */
1452 for (j=0;j<=dimension;j++)
1453 if (value_notzero_p(polyhedron->Constraint[i][j]))
1454 return 0 ;
1456 if (value_notone_p(polyhedron->Constraint[i][dimension+1]))
1457 return 0 ;
1459 for (j=dimension+2;j<(polyhedron->Dimension + 1);j++)
1460 if (value_notzero_p(polyhedron->Constraint[i][j]))
1461 return 0 ;
1463 if (value) {
1464 value_assign(*value,polyhedron->Constraint[i][polyhedron->Dimension+1]);
1465 value_oppose(*value,*value);
1467 return 1;
1471 return 0;
1476 * cloog_scattering_lazy_isscalar function:
1477 * this function returns 1 if the dimension 'dimension' in the domain 'domain'
1478 * is scalar, this means that the only constraint on this dimension must have
1479 * the shape "x.dimension + scalar = 0" with x an integral variable. This
1480 * function is lazy since we only accept x=1 (further calculations are easier
1481 * in this way).
1482 * If value is not NULL, then it is set to the constant value of dimension.
1483 * - June 14th 2005: first version.
1484 * - June 21rd 2005: Adaptation for GMP.
1486 int cloog_scattering_lazy_isscalar(CloogScattering *domain, int dimension,
1487 cloog_int_t *value)
1489 return polyhedron_lazy_isconstant(domain->polyhedron, dimension, value);
1494 * cloog_domain_lazy_isconstant function:
1495 * this function returns 1 if the dimension 'dimension' in the
1496 * domain 'domain' is constant.
1497 * If value is not NULL, then it is set to the constant value of dimension.
1499 int cloog_domain_lazy_isconstant(CloogDomain *domain, int dimension)
1501 return polyhedron_lazy_isconstant(domain->polyhedron, dimension, NULL);
1506 * cloog_scattering_erase_dimension function:
1507 * this function returns a CloogDomain structure builds from 'domain' where
1508 * we removed the dimension 'dimension' and every constraint considering this
1509 * dimension. This is not a projection ! Every data concerning the
1510 * considered dimension is simply erased.
1511 * - June 14th 2005: first version.
1512 * - June 21rd 2005: Adaptation for GMP.
1514 CloogDomain *cloog_scattering_erase_dimension(CloogScattering *domain,
1515 int dimension)
1516 { int i, j, mi, nb_dim ;
1517 CloogMatrix * matrix ;
1518 CloogDomain * erased ;
1519 Polyhedron * polyhedron ;
1521 polyhedron = domain->polyhedron ;
1522 nb_dim = polyhedron->Dimension ;
1524 /* The matrix is one column less and at least one constraint less. */
1525 matrix = cloog_matrix_alloc(polyhedron->NbConstraints-1,nb_dim+1) ;
1527 /* mi is the constraint counter for the matrix. */
1528 mi = 0 ;
1529 for (i=0;i<polyhedron->NbConstraints;i++)
1530 if (value_zero_p(polyhedron->Constraint[i][dimension+1]))
1531 { for (j=0;j<=dimension;j++)
1532 value_assign(matrix->p[mi][j],polyhedron->Constraint[i][j]) ;
1534 for (j=dimension+2;j<nb_dim+2;j++)
1535 value_assign(matrix->p[mi][j-1],polyhedron->Constraint[i][j]) ;
1537 mi ++ ;
1540 erased = cloog_domain_matrix2domain(matrix) ;
1541 cloog_matrix_free(matrix) ;
1543 return erased ;
1548 * cloog_domain_cube:
1549 * Construct and return a dim-dimensional cube, with values ranging
1550 * between min and max in each dimension.
1552 CloogDomain *cloog_domain_cube(int dim, cloog_int_t min, cloog_int_t max,
1553 CloogOptions *options)
1555 int i;
1556 Matrix *M;
1557 Polyhedron *P;
1559 M = Matrix_Alloc(2*dim, 2+dim);
1560 for (i = 0; i < dim; ++i) {
1561 value_set_si(M->p[2*i][0], 1);
1562 value_set_si(M->p[2*i][1+i], 1);
1563 value_oppose(M->p[2*i][1+dim], min);
1564 value_set_si(M->p[2*i+1][0], 1);
1565 value_set_si(M->p[2*i+1][1+i], -1);
1566 value_assign(M->p[2*i+1][1+dim], max);
1568 P = Constraints2Polyhedron(M, MAX_RAYS);
1569 Matrix_Free(M);
1570 return cloog_domain_alloc(P);
1574 * cloog_domain_scatter function:
1575 * This function add the scattering (scheduling) informations in a domain.
1577 CloogDomain *cloog_domain_scatter(CloogDomain *domain, CloogScattering *scatt)
1578 { int scatt_dim ;
1579 CloogDomain *ext, *newdom, *newpart, *temp;
1581 newdom = NULL ;
1582 scatt_dim = cloog_scattering_dimension(scatt, domain);
1584 /* For each polyhedron of domain (it can be an union of polyhedra). */
1585 while (domain != NULL)
1586 { /* Extend the domain by adding the scattering dimensions as the new
1587 * first domain dimensions.
1589 ext = cloog_domain_extend(domain,scatt_dim,cloog_domain_dimension(domain)) ;
1590 /* Then add the scattering constraints. */
1591 newpart = cloog_domain_addconstraints(scatt,ext) ;
1592 cloog_domain_free(ext) ;
1594 if (newdom != NULL)
1595 { temp = newdom ;
1596 newdom = cloog_domain_union(newdom,newpart) ;
1597 cloog_domain_free(temp) ;
1598 cloog_domain_free(newpart) ;
1600 else
1601 newdom = newpart ;
1603 /* We don't want to free the rest of the list. */
1604 temp = cloog_domain_cut_first(domain, &domain);
1605 cloog_domain_free(temp) ;
1608 return newdom;