Correctly print version number
[cloog.git] / source / loop.c
blob50d1d2b0ecc5fcc206165fae22956589476ee30d
2 /**-------------------------------------------------------------------**
3 ** CLooG **
4 **-------------------------------------------------------------------**
5 ** loop.c **
6 **-------------------------------------------------------------------**
7 ** First version: october 26th 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 !
39 # include <stdlib.h>
40 # include <stdio.h>
41 # include "../include/cloog/cloog.h"
44 /******************************************************************************
45 * Memory leaks hunting *
46 ******************************************************************************/
49 /**
50 * These functions and global variables are devoted to memory leaks hunting: we
51 * want to know at each moment how many CloogLoop structures had been allocated
52 * (cloog_loop_allocated) and how many had been freed (cloog_loop_freed).
53 * Each time a CloogLoog structure is allocated, a call to the function
54 * cloog_loop_leak_up() must be carried out, and respectively
55 * cloog_loop_leak_down() when a CloogLoop structure is freed. The special
56 * variable cloog_loop_max gives the maximal number of CloogLoop structures
57 * simultaneously alive (i.e. allocated and non-freed) in memory.
58 * - July 3rd->11th 2003: first version (memory leaks hunt and correction).
62 extern int cloog_value_allocated ;
63 extern int cloog_value_freed ;
64 extern int cloog_value_max ;
67 int cloog_loop_allocated = 0 ;
68 int cloog_loop_freed = 0 ;
69 int cloog_loop_max = 0 ;
72 static void cloog_loop_leak_up()
73 { cloog_loop_allocated ++ ;
74 if ((cloog_loop_allocated-cloog_loop_freed) > cloog_loop_max)
75 cloog_loop_max = cloog_loop_allocated - cloog_loop_freed ;
79 static void cloog_loop_leak_down()
80 { cloog_loop_freed ++ ;
84 /******************************************************************************
85 * Structure display function *
86 ******************************************************************************/
89 /**
90 * cloog_loop_print_structure function:
91 * Displays a loop structure in a way that trends to be understandable without
92 * falling in a deep depression or, for the lucky ones, getting a headache...
93 * Written by Olivier Chorier, Luc Marchaud, Pierre Martin and Romain Tartiere.
94 * - April 24th 2005: Initial version.
95 * - May 21rd 2005: - New parameter `F' for destination file (ie stdout),
96 * - Minor tweaks.
97 * - May 26th 2005: Memory leak hunt.
98 * - June 2nd 2005: (Ced) Integration and minor fixes.
99 * -June 22nd 2005: (Ced) Adaptation for GMP.
101 void cloog_loop_print_structure(FILE * file, CloogLoop * loop, int level)
102 { int i, j, first=1 ;
104 if (loop)
105 { /* Go to the right level. */
106 for (i=0; i<level; i++)
107 fprintf(file,"|\t") ;
109 fprintf(file,"+-- CloogLoop\n") ;
112 /* For each loop. */
113 while (loop)
114 { if (!first)
115 { /* Go to the right level. */
116 for (i=0; i<level; i++)
117 fprintf(file,"|\t") ;
119 fprintf(file,"| CloogLoop\n") ;
121 else
122 first = 0 ;
124 /* A blank line. */
125 for(j=0; j<=level+1; j++)
126 fprintf(file,"|\t") ;
127 fprintf(file,"\n") ;
129 /* Print the domain. */
130 cloog_domain_print_structure(file,loop->domain,level+1) ;
132 /* Print the stride. */
133 for(j=0; j<=level; j++)
134 fprintf(file,"|\t") ;
135 fprintf(file, "Stride: ") ;
136 value_print(file,VALUE_FMT,loop->stride) ;
137 fprintf(file, "\n") ;
139 /* A blank line. */
140 for(j=0; j<=level+1; j++)
141 fprintf(file,"|\t") ;
142 fprintf(file,"\n") ;
144 /* Print the block. */
145 cloog_block_print_structure(file,loop->block,level+1) ;
147 /* A blank line. */
148 for (i=0; i<=level+1; i++)
149 fprintf(file,"|\t") ;
150 fprintf(file,"\n") ;
152 /* Print inner if any. */
153 if (loop->inner)
154 cloog_loop_print_structure(file,loop->inner,level+1) ;
156 /* And let's go for the next one. */
157 loop = loop->next ;
159 /* One more time something that is here only for a better look. */
160 if (!loop)
161 { /* Two blank lines if this is the end of the linked list. */
162 for (j=0; j<2; j++)
163 { for (i=0; i<=level; i++)
164 fprintf(file,"|\t") ;
166 fprintf(file,"\n") ;
169 else
170 { /* A special blank line if the is a next loop. */
171 for (i=0; i<=level; i++)
172 fprintf(file,"|\t") ;
173 fprintf(file,"V\n") ;
180 * cloog_loop_print function:
181 * This function prints the content of a CloogLoop structure (start) into a
182 * file (file, possibly stdout).
183 * - June 2nd 2005: Now this very old function (probably as old as CLooG) is
184 * only a frontend to cloog_loop_print_structure, with a quite
185 * better human-readable representation.
187 void cloog_loop_print(FILE * file, CloogLoop * loop)
188 { cloog_loop_print_structure(file,loop,0) ;
192 /******************************************************************************
193 * Memory deallocation function *
194 ******************************************************************************/
198 * cloog_loop_free function:
199 * This function frees the allocated memory for a CloogLoop structure (loop),
200 * and frees its inner loops and its next loops.
201 * - June 22nd 2005: Adaptation for GMP.
203 void cloog_loop_free(CloogLoop * loop)
204 { CloogLoop * next ;
206 while (loop != NULL)
207 { cloog_loop_leak_down() ;
209 next = loop->next ;
210 cloog_domain_free(loop->domain) ;
211 cloog_block_free(loop->block) ;
212 if (loop->inner != NULL)
213 cloog_loop_free(loop->inner) ;
215 value_clear_c(loop->stride) ;
216 free(loop) ;
217 loop = next ;
223 * cloog_loop_free_parts function:
224 * This function frees the allocated memory for some parts of a CloogLoop
225 * structure (loop), each other argument is a boolean having to be set to 1 if
226 * we want to free the corresponding part, 0 otherwise. This function applies
227 * the same freeing policy to its inner ans next loops recursively.
228 * - July 3rd 2003: first version.
229 * - June 22nd 2005: Adaptation for GMP.
231 void cloog_loop_free_parts(loop, domain, block, inner, next)
232 CloogLoop * loop ;
233 int domain, block, inner, next ;
234 { CloogLoop * follow ;
236 while (loop != NULL)
237 { cloog_loop_leak_down() ;
238 follow = loop->next ;
240 if (domain)
241 cloog_domain_free(loop->domain) ;
243 if (block)
244 cloog_block_free(loop->block) ;
246 if ((inner) && (loop->inner != NULL))
247 cloog_loop_free_parts(loop->inner,domain,block,inner,1) ;
249 value_clear_c(loop->stride) ;
250 free(loop) ;
251 if (next)
252 loop = follow ;
253 else
254 loop = NULL ;
259 /******************************************************************************
260 * Reading functions *
261 ******************************************************************************/
265 * cloog_loop_read function:
266 * This function reads loop data into a file (foo, possibly stdin) and
267 * returns a pointer to a CloogLoop structure containing the read information.
268 * This function can be used only for input file reading, when one loop is
269 * associated with one statement.
270 * - number is the statement block number carried by the loop (-1 if none).
271 * - nb_parameters is the number of parameters.
273 * - September 9th 2002: first version.
274 * - April 16th 2005: adaptation to new CloogStatement struct (with number).
275 * - June 11th 2005: adaptation to new CloogBlock structure.
276 * - June 22nd 2005: Adaptation for GMP.
278 CloogLoop * cloog_loop_read(FILE * foo, int number, int nb_parameters)
279 { int nb_iterators, op1, op2, op3 ;
280 char s[MAX_STRING] ;
281 CloogLoop * loop ;
282 CloogStatement * statement ;
284 cloog_loop_leak_up() ;
286 /* Memory allocation and information reading for the first domain: */
287 loop = (CloogLoop *)malloc(sizeof(CloogLoop)) ;
288 if (loop == NULL)
289 { fprintf(stderr, "Memory Overflow.\n") ;
290 exit(1) ;
292 /* domain. */
293 loop->domain = cloog_domain_union_read(foo) ;
294 if (loop->domain != NULL)
295 nb_iterators = cloog_domain_dimension(loop->domain) - nb_parameters ;
296 else
297 nb_iterators = 0 ;
298 /* stride is initialized to 1. */
299 value_init_c(loop->stride) ;
300 value_set_si(loop->stride,1) ;
301 /* included statement block. */
302 statement = cloog_statement_alloc(number+1);
303 loop->block = cloog_block_alloc(statement,NULL,0,NULL,nb_iterators) ;
304 /* inner is NULL at beginning. */
305 loop->inner = NULL ;
306 /* next element. */
307 loop->next = NULL ;
309 /* To read that stupid "0 0 0" line. */
310 while (fgets(s,MAX_STRING,foo) == 0) ;
311 while ((*s=='#' || *s=='\n') || (sscanf(s," %d %d %d",&op1,&op2,&op3)<3))
312 fgets(s,MAX_STRING,foo) ;
314 return loop ;
318 /******************************************************************************
319 * Processing functions *
320 ******************************************************************************/
324 * cloog_loop_malloc function:
325 * This function allocates the memory space for a CloogLoop structure and
326 * sets its fields with default values. Then it returns a pointer to the
327 * allocated space.
328 * - November 21th 2005: first version.
330 CloogLoop * cloog_loop_malloc()
331 { CloogLoop * loop ;
333 /* Memory allocation for the CloogLoop structure. */
334 loop = (CloogLoop *)malloc(sizeof(CloogLoop)) ;
335 if (loop == NULL)
336 { fprintf(stderr, "[CLooG]ERROR: memory overflow.\n") ;
337 exit(1) ;
339 cloog_loop_leak_up() ;
342 /* We set the various fields with default values. */
343 loop->domain = NULL ;
344 loop->block = NULL ;
345 loop->inner = NULL ;
346 loop->next = NULL ;
347 value_init_c(loop->stride) ;
348 value_set_si(loop->stride,1) ;
350 return loop ;
355 * cloog_loop_alloc function:
356 * This function allocates the memory space for a CloogLoop structure and
357 * sets its fields with those given as input. Then it returns a pointer to the
358 * allocated space.
359 * - October 27th 2001: first version.
360 * - June 22nd 2005: Adaptation for GMP.
361 * - November 21th 2005: use of cloog_loop_malloc.
363 CloogLoop * cloog_loop_alloc(domain, stride, block, inner, next)
364 CloogDomain * domain ;
365 Value stride ;
366 CloogBlock * block ;
367 CloogLoop * inner, * next ;
368 { CloogLoop * loop ;
370 loop = cloog_loop_malloc() ;
372 loop->domain = domain ;
373 loop->block = block ;
374 loop->inner = inner ;
375 loop->next = next ;
376 value_assign(loop->stride,stride) ;
378 return(loop) ;
383 * cloog_loop_add function:
384 * This function adds a CloogLoop structure (loop) at a given place (now) of a
385 * NULL terminated list of CloogLoop structures. The beginning of this list
386 * is (start). This function updates (now) to (loop), and updates (start) if the
387 * added element is the first one -that is when (start) is NULL-.
388 * - October 28th 2001: first version.
390 void cloog_loop_add(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
391 { if (*start == NULL)
392 { *start = loop ;
393 *now = *start ;
395 else
396 { (*now)->next = loop ;
397 *now = (*now)->next ;
403 * cloog_loop_add function:
404 * This function adds a CloogLoop structure (loop) at a given place (now) of a
405 * NULL terminated list of CloogLoop structures. The beginning of this list
406 * is (start). This function updates (now) to the end of the loop list (loop),
407 * and updates (start) if the added element is the first one -that is when
408 * (start) is NULL-.
409 * - September 9th 2005: first version.
411 void cloog_loop_add_list(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
412 { if (*start == NULL)
413 { *start = loop ;
414 *now = *start ;
416 else
417 { (*now)->next = loop ;
418 *now = (*now)->next ;
421 while ((*now)->next != NULL)
422 *now = (*now)->next ;
427 * cloog_loop_copy function:
428 * This function returns a copy of the CloogLoop structure given as input. In
429 * fact, there is just new allocations for the CloogLoop structures, but their
430 * contents are the same.
431 * - October 28th 2001: first version.
432 * - July 3rd->11th 2003: memory leaks hunt and correction.
434 CloogLoop * cloog_loop_copy(CloogLoop * source)
435 { CloogLoop * loop ;
436 CloogBlock * block ;
437 CloogDomain * domain ;
439 loop = NULL ;
440 if (source != NULL)
441 { domain = cloog_domain_copy(source->domain) ;
442 block = cloog_block_copy(source->block) ;
443 loop = cloog_loop_alloc(domain,source->stride,block,NULL,NULL) ;
444 loop->inner = cloog_loop_copy(source->inner) ;
445 loop->next = cloog_loop_copy(source->next) ;
447 return(loop) ;
452 * cloog_loop_add_disjoint function:
453 * This function adds some CloogLoop structures at a given place (now) of a
454 * NULL terminated list of CloogLoop structures. The beginning of this list
455 * is (start). (loop) can be an union of polyhedra, this function separates the
456 * union into a list of *disjoint* polyhedra then adds the list. This function
457 * updates (now) to the end of the list and updates (start) if first added
458 * element is the first of the principal list -that is when (start) is NULL-.
459 * (loop) can be freed by this function, basically when its domain is actually
460 * a union of polyhedra, but don't worry, all the useful data are now stored
461 * inside the list (start). We do not use PolyLib's Domain_Disjoint function,
462 * since the number of union components is often higher (thus code size too).
463 * - October 28th 2001: first version.
464 * - November 14th 2001: bug correction (this one was hard to find !).
465 * - July 3rd->11th 2003: memory leaks hunt and correction.
466 * - June 22nd 2005: Adaptation for GMP.
467 * - October 27th 2005: (debug) included blocks were not copied for new loops.
469 void cloog_loop_add_disjoint(start, now, loop)
470 CloogLoop ** start, ** now, * loop ;
471 { Value one ;
472 CloogLoop * sep, * inner ;
473 CloogDomain * domain, * convex, * seen, * seen_before, * temp, * rest ;
474 CloogBlock * block ;
476 value_init_c(one) ;
477 value_set_si(one,1) ;
479 if (cloog_domain_isconvex(loop->domain))
480 cloog_loop_add(start,now,loop) ;
481 else
482 { /* Seems useless but may simplify the union expression (PolyLib pb). */
483 convex = cloog_domain_convex(loop->domain) ;
484 temp = cloog_domain_difference(convex,loop->domain) ;
485 cloog_domain_free(loop->domain) ;
486 loop->domain = NULL ;
487 domain = cloog_domain_difference(convex,temp) ;
488 cloog_domain_free(convex) ;
489 cloog_domain_free(temp) ;
491 /* We separate the first element of the rest of the union. */
492 rest = cloog_domain_cut_first(domain) ;
494 /* This first element is the first of the list of disjoint polyhedra. */
495 sep = cloog_loop_alloc(domain,one,loop->block,loop->inner,NULL) ;
496 cloog_loop_add(start,now,sep) ;
498 /* If there are other elements, add a loop for each of them. */
499 if (rest != NULL)
500 { /* domain is used by the first element, and we will free 'seen', so... */
501 seen = cloog_domain_copy(domain) ;
503 while ((domain = rest) != NULL)
504 { rest = cloog_domain_cut_first(domain) ;
505 temp = cloog_domain_difference(domain,seen) ;
507 /* Each new loop will have its own life, for instance we can free its
508 * inner loop and included block. Then each one must have its own copy
509 * of both 'inner' and 'block'.
511 inner = cloog_loop_copy(loop->inner) ;
512 block = cloog_block_copy(loop->block) ;
514 sep = cloog_loop_alloc(temp,one,block,inner,NULL) ;
515 /* temp can be an union too. If so: recursion. */
516 if (cloog_domain_isconvex(temp))
517 cloog_loop_add(start,now,sep) ;
518 else
519 cloog_loop_add_disjoint(start,now,sep) ;
521 seen_before = seen ;
522 if (rest != NULL)
523 seen = cloog_domain_union(seen_before,domain) ;
525 cloog_domain_free(seen_before) ;
526 cloog_domain_free(domain) ;
529 cloog_loop_free_parts(loop,0,0,0,0) ;
531 value_clear_c(one) ;
536 * cloog_loop_disjoint function:
537 * This function returns a list of loops such that each loop with non-convex
538 * domain in the input list (loop) is separated into several loops where the
539 * domains are the components of the union of *disjoint* polyhedra equivalent
540 * to the original non-convex domain. See cloog_loop_add_disjoint comments
541 * for more details.
542 * - September 16th 2005: first version.
544 CloogLoop * cloog_loop_disjoint(CloogLoop * loop)
545 { CloogLoop *res=NULL, * now=NULL, * next ;
547 /* Because this is often the case, don't waste time ! */
548 if ((loop != NULL) && cloog_domain_isconvex(loop->domain))
549 return loop ;
551 while (loop != NULL)
552 { next = loop->next ;
553 loop->next = NULL ;
554 cloog_loop_add_disjoint(&res,&now,loop) ;
555 loop = next ;
558 return res ;
563 * cloog_loop_restrict function:
564 * This function returns the (loop) in the context of (context): it makes the
565 * intersection between the (loop) domain and the (context), then it returns
566 * a pointer to a new loop, with this intersection as domain.
567 * - nb_par is the number of parameters.
569 * - October 27th 2001: first version.
570 * - June 15th 2005: a memory leak fixed (domain was not freed when empty).
571 * - June 22nd 2005: Adaptation for GMP.
573 CloogLoop * cloog_loop_restrict(loop, context, nb_par)
574 CloogLoop * loop ;
575 CloogDomain * context ;
576 int nb_par ;
577 { int new_dimension ;
578 Value one ;
579 CloogDomain * domain, * extended_context, * new_domain ;
580 CloogLoop * new_loop ;
582 domain = loop->domain ;
583 if (cloog_domain_dimension(domain) > cloog_domain_dimension(context))
584 { new_dimension = cloog_domain_dimension(domain) - nb_par ;
585 extended_context = cloog_domain_extend(context,new_dimension,nb_par) ;
586 new_domain = cloog_domain_intersection(extended_context,loop->domain) ;
587 cloog_domain_free(extended_context) ;
589 else
590 new_domain = cloog_domain_intersection(context,loop->domain) ;
592 if (cloog_domain_isempty(new_domain))
593 { cloog_domain_free(new_domain) ;
594 return(NULL) ;
596 else
597 { value_init_c(one) ;
598 value_set_si(one,1) ;
599 new_loop = cloog_loop_alloc(new_domain,one,loop->block,loop->inner,NULL) ;
600 value_clear_c(one) ;
601 return(new_loop) ;
607 * cloog_loop_project function:
608 * This function returns the projection of (loop) on the (level) first
609 * dimensions (outer loops). It makes the projection of the (loop) domain,
610 * then it returns a pointer to a new loop, with this projection as domain.
611 * - nb_par is the number of parameters.
613 * - October 27th 2001: first version.
614 * - July 3rd->11th 2003: memory leaks hunt and correction.
615 * - June 22nd 2005: Adaptation for GMP.
617 CloogLoop * cloog_loop_project(CloogLoop * loop, int level, int nb_par)
618 { Value one ;
619 CloogDomain * new_domain ;
620 CloogLoop * new_loop, * copy ;
622 value_init_c(one) ;
623 value_set_si(one,1) ;
625 copy = cloog_loop_alloc(loop->domain,loop->stride,loop->block,
626 loop->inner,NULL) ;
628 if ((cloog_domain_dimension(loop->domain)-nb_par) == level)
629 new_domain = cloog_domain_copy(loop->domain) ;
630 else
631 new_domain = cloog_domain_project(loop->domain,level,nb_par) ;
633 new_loop = cloog_loop_alloc(new_domain,one,NULL,copy,NULL) ;
634 value_clear_c(one) ;
636 return(new_loop) ;
641 * cloog_loop_concat function:
642 * This function returns a pointer to the concatenation of the
643 * CloogLoop lists given as input.
644 * - October 28th 2001: first version.
646 CloogLoop * cloog_loop_concat(CloogLoop * a, CloogLoop * b)
647 { CloogLoop * loop, * temp ;
649 loop = a ;
650 temp = loop ;
651 if (loop != NULL)
652 { while (temp->next != NULL)
653 temp = temp->next ;
654 temp->next = b ;
656 else
657 loop = b ;
659 return(loop) ;
664 * cloog_loop_separate function:
665 * This function implements the Quillere algorithm for separation of multiple
666 * loops: for a given set of polyhedra (loop), it computes a set of disjoint
667 * polyhedra such that the unions of these sets are equal, and returns this set.
668 * - October 28th 2001: first version.
669 * - November 14th 2001: elimination of some unused blocks.
670 * - August 13th 2002: (debug) in the case of union of polyhedra for one
671 * loop, redundant constraints are fired.
672 * - July 3rd->11th 2003: memory leaks hunt and correction.
673 * - June 22nd 2005: Adaptation for GMP.
674 * - October 16th 2005: Removal of the non-shared constraint elimination when
675 * there is only one loop in the list (seems to work
676 * without now, DomainSimplify may have been improved).
677 * The problem was visible with test/iftest2.cloog.
679 CloogLoop * cloog_loop_separate(CloogLoop * loop)
680 { int first, lazy_equal=0, lazy_disjoint=0 ;
681 Value one ;
682 CloogLoop * new_loop, * new_inner, * res, * now, * temp, * Q,
683 * inner, * old /*, * previous, * next*/ ;
684 CloogDomain * UQ, * old_UQ, * domain ;
686 if (loop == NULL)
687 return NULL ;
689 if (loop->next == NULL)
690 return cloog_loop_disjoint(loop) ;
692 value_init_c(one) ;
693 value_set_si(one,1) ;
695 UQ = cloog_domain_copy(loop->domain) ;
696 domain = cloog_domain_copy(loop->domain) ;
697 res = cloog_loop_alloc(domain,one,loop->block,loop->inner,NULL) ;
699 old = loop ;
700 while((loop = loop->next) != NULL)
701 { temp = NULL ;
702 first = 1 ;
704 /* For all Q, add Q-loop associated with the blocks of Q alone,
705 * and Q inter loop associated with the blocks of Q and loop.
707 Q = res ;
708 while (Q != NULL)
709 { if (Q->block == NULL)
710 { /* Add (Q inter loop). */
711 if((lazy_disjoint=cloog_domain_lazy_disjoint(Q->domain,loop->domain)))
712 domain = NULL ;
713 else
714 { if ((lazy_equal = cloog_domain_lazy_equal(Q->domain,loop->domain)))
715 domain = cloog_domain_copy(Q->domain) ;
716 else
717 domain = cloog_domain_intersection(Q->domain,loop->domain) ;
719 if (!cloog_domain_isempty(domain))
720 { new_inner = cloog_loop_concat(cloog_loop_copy(Q->inner),
721 cloog_loop_copy(loop->inner)) ;
722 new_loop = cloog_loop_alloc(domain,one,NULL,new_inner,NULL) ;
723 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
725 else
726 cloog_domain_free(domain) ;
729 /* Add (Q - loop). */
730 if (lazy_disjoint)
731 domain = cloog_domain_copy(Q->domain) ;
732 else
733 { if (lazy_equal)
734 domain = cloog_domain_empty(cloog_domain_dimension(Q->domain)) ;
735 else
736 domain = cloog_domain_difference(Q->domain,loop->domain) ;
739 if (!cloog_domain_isempty(domain))
740 { new_loop = cloog_loop_alloc(domain,one,NULL,Q->inner,NULL) ;
741 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
743 else
744 { cloog_domain_free(domain) ;
745 /* If Q->inner is no more useful, we can free it. */
746 inner = Q->inner ;
747 Q->inner = NULL ;
748 if (first) /* For the first Q, inner is also old->inner. */
749 old->inner = NULL ;
750 cloog_loop_free(inner) ;
753 Q = Q->next ;
756 /* Add loop-UQ associated with the blocks of loop alone.*/
757 if (cloog_domain_lazy_disjoint(loop->domain,UQ))
758 domain = cloog_domain_copy(loop->domain) ;
759 else
760 { if (cloog_domain_lazy_equal(loop->domain,UQ))
761 domain = cloog_domain_empty(cloog_domain_dimension(UQ)) ;
762 else
763 domain = cloog_domain_difference(loop->domain,UQ) ;
766 if (!cloog_domain_isempty(domain))
767 { new_loop = cloog_loop_alloc(domain,one,NULL,loop->inner,NULL) ;
768 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
770 else
771 { cloog_domain_free(domain) ;
772 /* If loop->inner is no more useful, we can free it. */
773 cloog_loop_free(loop->inner) ;
776 loop->inner = NULL ;
778 old_UQ = UQ ;
779 if (loop->next != NULL)
780 UQ = cloog_domain_union(UQ,loop->domain) ;
782 cloog_domain_free(old_UQ) ;
783 cloog_loop_free_parts(res,1,0,0,1) ;
785 first = 0 ;
786 res = temp ;
788 cloog_loop_free_parts(old,1,0,0,1) ;
789 value_clear_c(one) ;
791 return(res) ;
796 * cloog_loop_merge_list
797 * Merge two lists of CloogLoops. The new list contains the
798 * elements of the two lists in the same order, but they may
799 * be interleaved.
800 * In particular, if the elements of a and b are ordered
801 * according to the inner loops of the order list, then so are the elements
802 * in the new list.
804 static CloogLoop *cloog_loop_merge_inner_list(CloogLoop *a, CloogLoop *b,
805 CloogLoop *order)
807 CloogLoop *loop, **next;
808 next = &loop;
810 for ( ; order && (a||b); order = order->next) {
811 if (a && order->inner->block == a->block) {
812 *next = a;
813 a = a->next;
814 next = &(*next)->next;
815 continue;
817 if (b && order->inner->block == b->block) {
818 *next = b;
819 b = b->next;
820 next = &(*next)->next;
823 return loop;
827 * cloog_loop_merge function:
828 * This function is the 'soft' version of loop_separate if we are looking for
829 * a code much simpler (and less efficicient). Here we merge loops if they have
830 * common parts in the iteration space (if the intersection of their domains is
831 * not empty), and let them isolated otherwise. This function returns the new
832 * CloogLoop list.
833 * - October 29th 2001: first version.
834 * - July 3rd->11th 2003: memory leaks hunt and correction.
835 * - June 22nd 2005: Adaptation for GMP.
837 CloogLoop * cloog_loop_merge(CloogLoop * loop, int nb_par, CloogOptions * options)
838 { Value one ;
839 CloogLoop * res, * merge, * now, * Q, * P, * new_inner, * next, * old ;
840 CloogDomain * new_domain, * temp ;
842 if ((loop == NULL) || (loop->next == NULL))
843 return loop ;
845 value_init_c(one) ;
846 value_set_si(one,1) ;
848 /* First loop is added to the target list. */
849 res = cloog_loop_alloc(loop->domain,one,loop->block,loop->inner,NULL) ;
850 old = loop ;
851 /* Now the domain is in 'res' and it will be freed. */
852 loop->domain = NULL ;
854 /* And one by one, we see if we have to merge or to add the other loops. */
855 while((loop = loop->next) != NULL)
856 { merge = NULL ;
857 P = cloog_loop_alloc(loop->domain,one,loop->block,loop->inner,NULL) ;
858 Q = res ;
859 /* Now the domain is in 'P' and it will be freed. */
860 loop->domain = NULL ;
862 /* For each loop in the target list, if the intersection with the new loop
863 * is empty, we can add the new loop directly, otherwise, we can merge then
864 * add the fusion.
866 while (Q != NULL)
867 { temp = cloog_domain_intersection(Q->domain,P->domain) ;
868 next = Q->next ;
869 if (cloog_domain_isempty(temp))
870 { cloog_domain_free(temp) ;
871 cloog_loop_add_disjoint(&merge,&now,Q) ;
873 else
874 { cloog_domain_free(temp) ;
875 new_inner = cloog_loop_merge_inner_list(Q->inner, P->inner, old);
876 temp = cloog_domain_union(P->domain,Q->domain) ;
877 if (options->sh)
878 new_domain = cloog_domain_simple_convex(temp, nb_par);
879 else
880 new_domain = cloog_domain_convex(temp);
881 cloog_domain_free(temp) ;
882 /* Q and P are no more used (but their content yes !).*/
883 cloog_loop_free_parts(P,1,0,0,0) ;
884 cloog_loop_free_parts(Q,1,0,0,0) ;
885 P = cloog_loop_alloc(new_domain,one,NULL,new_inner,NULL) ;
887 Q = next ;
890 /* If there was merging, add it, otherwise add the loop lonely.
891 * DEBUG : ici pas besoin de s'assurer que P->next est NULL (possible que
892 * non si pas de fusion) car le dernier loop etudie a loop->next = NULL.
894 cloog_loop_add_disjoint(&merge,&now,P) ;
895 res = merge ;
897 cloog_loop_free_parts(old,0,0,0,1) ;
898 value_clear_c(one) ;
900 return (res);
905 * cloog_loop_sort function:
906 * Adaptation from LoopGen 0.4 by F. Quillere. This function sorts a list of
907 * parameterized disjoint polyhedra, in order to not have lexicographic order
908 * violation (see Quillere paper).
909 * - September 16th 2005: inclusion of cloog_loop_number (October 29th 2001).
911 CloogLoop * cloog_loop_sort(CloogLoop * loop, int level, int nb_par)
912 { CloogLoop * res, * now, * temp, ** loop_array ;
913 Polyhedron ** pols ;
914 int i, nb_loops=0, * permut ;
916 /* We will need to know how many loops are in the list. */
917 temp = loop ;
918 while (temp != NULL)
919 { nb_loops ++ ;
920 temp = temp->next ;
923 /* If there is only one loop, it's the end. */
924 if (nb_loops == 1)
925 return(loop) ;
927 /* We have to allocate memory for some useful components:
928 * - loop_array: the loop array,
929 * - pols: the array of domains to sort,
930 * - permut: will give us a possible sort (maybe not the only one).
932 loop_array = (CloogLoop **)malloc(nb_loops*sizeof(CloogLoop *)) ;
933 pols = (Polyhedron **)malloc(nb_loops*sizeof(Polyhedron *)) ;
934 permut = (int *)malloc(nb_loops*sizeof(int)) ;
936 /* We fill up the loop and domain arrays. */
937 for (i=0;i<nb_loops;i++,loop=loop->next)
938 { loop_array[i] = loop ;
939 pols[i] = cloog_domain_polyhedron(loop_array[i]->domain) ;
942 /* cloog_domain_sort will fill up permut. */
943 cloog_domain_sort(pols,nb_loops,level,nb_par,permut) ;
945 /* With permut and loop_array we build the sorted list. */
946 res = NULL ;
947 for (i=0;i<nb_loops;i++)
948 { /* To avoid pointer looping... loop_add will rebuild the list. */
949 loop_array[permut[i]-1]->next = NULL ;
950 cloog_loop_add(&res,&now,loop_array[permut[i]-1]) ;
953 free(permut) ;
954 free(pols) ;
955 free(loop_array) ;
957 return res;
962 * cloog_loop_nest function:
963 * This function changes the loop list in such a way that we have no more than
964 * one dimension added by level. It returns an equivalent loop list with
965 * this property.
966 * - October 29th 2001: first version.
967 * - July 3rd->11th 2003: memory leaks hunt and correction.
968 * - June 22nd 2005: Adaptation for GMP.
969 * - November 21th 2005: (debug) now OK when cloog_loop_restrict returns NULL.
971 CloogLoop * cloog_loop_nest(loop, context, level, nb_par)
972 CloogLoop * loop ;
973 CloogDomain * context ;
974 int level, nb_par ;
975 { int l ;
976 Value one ;
977 CloogLoop * p, * temp, * res, * now, * next ;
978 CloogDomain * new_domain ;
980 value_init_c(one) ;
981 value_set_si(one,1) ;
983 res = NULL ;
984 /* Each domain is changed by its intersection with the context. */
985 while (loop != NULL)
986 { p = cloog_loop_restrict(loop,context,nb_par) ;
987 next = loop->next ;
989 if (p != NULL)
990 { cloog_loop_free_parts(loop,1,0,0,0) ;
992 temp = cloog_loop_alloc(p->domain,one,p->block,p->inner,NULL) ;
994 /* If the intersection dimension is too big, we make projections smaller
995 * and smaller, and each projection includes the preceding projection
996 * (thus, in the target list, dimensions are added one by one).
998 if ((cloog_domain_dimension(p->domain)-nb_par) > level)
999 for (l=cloog_domain_dimension(p->domain)-nb_par-1;l>=level;l--)
1000 { new_domain = cloog_domain_project(p->domain,l,nb_par) ;
1001 temp = cloog_loop_alloc(new_domain,one,NULL,temp,NULL) ;
1004 /* p is no more useful (but its content yes !). */
1005 cloog_loop_free_parts(p,0,0,0,0) ;
1007 cloog_loop_add(&res,&now,temp) ;
1009 else
1010 cloog_loop_free_parts(loop,1,1,1,0) ;
1012 loop = next ;
1014 value_clear_c(one) ;
1016 return(res) ;
1021 * cloog_loop_stride function:
1022 * This function will find the stride of a loop for the iterator at the column
1023 * number 'level' in the constraint matrix. It will update the lower bound of
1024 * the iterator accordingly. Basically, the function will try to find in the
1025 * inner loops a common condition on this iterator for the inner loop iterators
1026 * to be integral. For instance, let us consider a loop with the iterator i,
1027 * the iteration domain -4<=i<=n, and its two inner loops with the iterator j.
1028 * The first inner loop has the constraint 3j=i, and the second one has the
1029 * constraint 6j=i. Then the common constraint on i for j to be integral is
1030 * i%3=0, the stride for i is 3. Lastly, we have to find the new lower bound
1031 * for i: the first value satisfying the common constraint: -3. At the end, the
1032 * iteration domain for i is -3<=i<=n and the stride for i is 3.
1033 * - loop is the loop including the iteration domain of the considered iterator,
1034 * - level is the column number of the iterator in the matrix of contraints.
1036 * - June 29th 2003: first version (work in progress since June 26th 2003).
1037 * - July 14th 2003: simpler version.
1038 * - June 22nd 2005: Adaptation for GMP (from S. Verdoolaege's 0.12.1 version).
1040 void cloog_loop_stride(CloogLoop * loop, int level, int nb_par)
1041 { int first_search ;
1042 Value stride, ref_offset, offset, potential, lower ;
1043 CloogLoop * inner ;
1045 value_init_c(stride) ;
1046 value_init_c(ref_offset) ;
1047 value_init_c(offset) ;
1048 value_init_c(potential) ;
1049 value_init_c(lower) ;
1051 value_set_si(ref_offset,0) ;
1052 value_set_si(offset,0) ;
1053 value_set_si(lower,0) ;
1055 /* Default stride. */
1056 value_set_si(stride,1) ;
1057 first_search = 1 ;
1058 inner = loop->inner ;
1060 if (cloog_domain_integral_lowerbound(loop->domain,level,&lower))
1061 while (inner != NULL)
1062 { /* If the minimun stride has not been found yet, find the stride. */
1063 if ((first_search) || (value_notone_p(stride)))
1064 { cloog_domain_stride(inner->domain,level,nb_par,&potential,&offset) ;
1065 if (value_notone_p(potential) && (!first_search))
1066 { /* Offsets must be the same for common stride. */
1067 Gcd(potential,stride,&stride) ;
1068 value_modulus(offset, offset, stride);
1069 value_modulus(ref_offset, ref_offset, stride);
1070 if (value_ne(offset,ref_offset))
1071 value_set_si(stride, 1);
1073 else
1074 { value_assign(stride,potential) ;
1075 value_assign(ref_offset,offset) ;
1078 first_search = 0 ;
1081 inner = inner->next ;
1084 /* Update the values if necessary. */
1085 if (value_notone_p(stride))
1086 { /* Update the stride value. */
1087 value_assign(loop->stride,stride) ;
1088 /* The new lower bound l' is such that
1089 * (l' + offset) % s = 0 and l <= l' <= l+(s-1)
1090 * Let l' = k s - offset, then
1091 * k s - offset <= l + (s-1) <= k s - offset + (s-1)
1092 * Or l' = floor((l+offset+(s-1))/s) * s - offset
1093 * = (floor((l+offset-1)/s) + 1) * s - offset
1095 value_addto(lower, lower, offset);
1096 value_decrement(lower, lower);
1097 value_pdivision(lower, lower, stride);
1098 value_increment(lower, lower);
1099 value_multiply(lower, lower, stride);
1100 value_subtract(lower, lower, offset);
1101 cloog_domain_lowerbound_update(loop->domain,level,lower) ;
1104 value_clear_c(stride) ;
1105 value_clear_c(ref_offset) ;
1106 value_clear_c(offset) ;
1107 value_clear_c(potential) ;
1108 value_clear_c(lower) ;
1113 * cloog_loop_stop function:
1114 * This function implements the 'stop' option : each domain of each loop
1115 * in the list 'loop' is replaced by 'context'. 'context' should be the
1116 * domain of the outer loop. By using this method, there are no more dimensions
1117 * to scan and the simplification step will automaticaly remove the domains
1118 * since they are the same as the corresponding contexts. The effect of this
1119 * function is to stop the code generation at the level this function is called,
1120 * the resulting code do not consider the next dimensions.
1121 * - January 11th 2005: first version.
1123 CloogLoop * cloog_loop_stop(CloogLoop * loop, CloogDomain * context)
1124 { if (loop == NULL)
1125 return NULL ;
1126 else
1127 { cloog_domain_free(loop->domain) ;
1128 loop->domain = cloog_domain_copy(context) ;
1129 loop->next = cloog_loop_stop(loop->next, context) ;
1132 return loop ;
1137 * cloog_loop_scalar_ge function:
1138 * This function returns 1 if loop 'l1' is greater or equal to loop 'l2' for the
1139 * scalar dimension vector that begins at dimension 'scalar', 0 otherwise. What
1140 * we want to know is whether a loop is scheduled before another one or not.
1141 * This function solves the problem when the considered dimension for scheduling
1142 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1143 * this function will reason about the vector of scalar dimension that begins
1144 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1145 * - l1 and l2 are the loops to compare,
1146 * - level is the current non-scalar dimension,
1147 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1148 * - nb_scattdims is the size of the scaldims array,
1149 * - scalar is the current scalar dimension.
1151 * - September 9th 2005 : first version.
1153 int cloog_loop_scalar_ge(l1, l2, level, scaldims, nb_scattdims, scalar)
1154 CloogLoop * l1, * l2 ;
1155 int level, * scaldims, nb_scattdims, scalar ;
1156 { while ((scalar < l1->inner->block->nb_scaldims) && scaldims[level+scalar-1])
1157 { if (value_ge(l1->inner->block->scaldims[scalar],
1158 l2->inner->block->scaldims[scalar]))
1159 scalar ++ ;
1160 else
1161 return 0 ;
1163 return 1 ;
1168 * cloog_loop_scalar_eq function:
1169 * This function returns 1 if loop 'l1' is equal to loop 'l2' for the scalar
1170 * dimension vector that begins at dimension 'scalar', 0 otherwise. What we want
1171 * to know is whether two loops are scheduled for the same time or not.
1172 * This function solves the problem when the considered dimension for scheduling
1173 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1174 * this function will reason about the vector of scalar dimension that begins
1175 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1176 * - l1 and l2 are the loops to compare,
1177 * - level is the current non-scalar dimension,
1178 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1179 * - nb_scattdims is the size of the scaldims array,
1180 * - scalar is the current scalar dimension.
1182 * - September 9th 2005 : first version.
1184 int cloog_loop_scalar_eq(l1, l2, level, scaldims, nb_scattdims, scalar)
1185 CloogLoop * l1, * l2 ;
1186 int level, * scaldims, nb_scattdims, scalar ;
1187 { while ((scalar < l1->inner->block->nb_scaldims) && scaldims[level+scalar-1])
1188 { if (value_eq(l1->inner->block->scaldims[scalar],
1189 l2->inner->block->scaldims[scalar]))
1190 scalar ++ ;
1191 else
1192 return 0 ;
1194 return 1 ;
1199 * cloog_loop_scalar_sort function:
1200 * This function sorts a linked list of loops (loop) with respect to the
1201 * scalar dimension vector that begins at dimension 'scalar'. Since there may
1202 * be a succession of scalar dimensions, this function will reason about the
1203 * vector of scalar dimension that begins at dimension 'level+scalar' and
1204 * finish to the first non-scalar dimension.
1205 * - loop is the loop list to sort,
1206 * - level is the current non-scalar dimension,
1207 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1208 * - nb_scattdims is the size of the scaldims array,
1209 * - scalar is the current scalar dimension.
1211 * - July 2nd 2005: first developments.
1212 * - September 2nd 2005: first version.
1214 CloogLoop * cloog_loop_scalar_sort(loop, level, scaldims, nb_scattdims, scalar)
1215 CloogLoop * loop ;
1216 int level, * scaldims, nb_scattdims, scalar ;
1217 { CloogLoop * unsorted, * previous_unsorted, * current, * previous_current ;
1219 if (loop == NULL)
1220 return NULL ;
1222 /* We start from the second element.*/
1223 unsorted = loop->next ;
1224 previous_unsorted = loop ;
1226 while (unsorted != NULL)
1227 { /* We look for the unsorted element place in the list. */
1228 current = loop ;
1229 previous_current = NULL ;
1231 while((current != unsorted) &&
1232 (cloog_loop_scalar_ge(unsorted,current,level,
1233 scaldims,nb_scattdims,scalar)))
1234 { previous_current = current ;
1235 current = current->next ;
1238 if (current == unsorted)
1239 { /* If the unsorted element is yet at the right place, restart with the
1240 * next unsorted element.
1242 previous_unsorted = unsorted ;
1243 unsorted = unsorted->next ;
1245 else
1246 { /* We have to insert the unsorted element after previous_current. */
1247 previous_unsorted->next = unsorted->next ;
1248 if (previous_current == NULL)
1249 { /* Insertion at the very first place. */
1250 unsorted->next = loop ;
1251 loop = unsorted ;
1253 else
1254 { /* Insertion after previous_current. */
1255 unsorted->next = previous_current->next ;
1256 previous_current->next = unsorted ;
1258 /* Restart with the next unsorted element. */
1259 unsorted = previous_unsorted->next ;
1263 return loop ;
1268 * cloog_loop_generate_backtrack function:
1269 * adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1270 * backtrack of the Quillere et al. algorithm (see the Quillere paper).
1271 * It eliminates unused iterations of the current level for the new one. See the
1272 * example called linearity-1-1 example with and without this part for an idea.
1273 * - October 26th 2001: first version in cloog_loop_generate_general.
1274 * - July 31th 2002: (debug) no more parasite loops (REALLY hard !).
1275 * - October 30th 2005: extraction from cloog_loop_generate_general.
1277 CloogLoop * cloog_loop_generate_backtrack(loop, context, level, nb_par, options)
1278 CloogLoop * loop ;
1279 CloogDomain * context ;
1280 int level, nb_par ;
1281 CloogOptions * options ;
1282 { Value one ;
1283 CloogDomain * domain ;
1284 CloogLoop * now, * now2, * next, * next2, * end, * temp, * l, * inner,
1285 * new_loop ;
1287 value_init_c(one) ;
1288 value_set_si(one,1) ;
1290 temp = loop ;
1291 loop = NULL ;
1293 while (temp != NULL)
1294 { l = NULL ;
1295 inner = temp->inner ;
1297 while (inner != NULL)
1298 { next = inner->next ;
1299 /* This 'if' and its first part is the debug of july 31th 2002. */
1300 if (inner->block != NULL)
1301 { end = cloog_loop_alloc(inner->domain,one,inner->block,NULL,NULL) ;
1302 domain = cloog_domain_copy(temp->domain) ;
1303 new_loop = cloog_loop_alloc(domain,one,NULL,end,NULL) ;
1305 else
1306 new_loop = cloog_loop_project(inner,level,nb_par) ;
1308 cloog_loop_free_parts(inner,0,0,0,0) ;
1309 cloog_loop_add(&l,&now2,new_loop) ;
1310 inner = next ;
1313 temp->inner = NULL ;
1315 if (l != NULL)
1316 { l = cloog_loop_separate(l) ;
1317 l = cloog_loop_sort(l,level,nb_par) ;
1318 while (l != NULL)
1319 { value_assign(l->stride,temp->stride) ;
1320 cloog_loop_add(&loop,&now,l) ;
1321 l = l->next ;
1324 next2 = temp->next ;
1325 cloog_loop_free_parts(temp,1,0,0,0) ;
1326 temp = next2 ;
1329 value_clear_c(one) ;
1331 return loop ;
1336 * cloog_loop_generate_general function:
1337 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1338 * Quillere algorithm for polyhedron scanning from step 3 to 5.
1339 * (see the Quillere paper).
1340 * - loop is the loop for which we have to generate a scanning code,
1341 * - context is the context of the current loop (constraints on parameter and/or
1342 * on outer loop counters),
1343 * - level is the current non-scalar dimension,
1344 * - scalar is the current scalar dimension,
1345 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1346 * - nb_scattdims is the size of the scaldims array,
1347 * - nb_par is the number of parameters,
1348 * - options are the general code generation options.
1350 * - October 26th 2001: first version.
1351 * - July 3rd->11th 2003: memory leaks hunt and correction.
1352 * - June 22nd 2005: Adaptation for GMP.
1353 * - September 2nd 2005: The function have been cutted out in two pieces:
1354 * cloog_loop_generate and this one, in order to handle
1355 * the scalar dimension case more efficiently with
1356 * cloog_loop_generate_scalar.
1357 * - November 15th 2005: (debug) the result of the cloog_loop_generate call may
1358 * be a list of polyhedra (especially if stop option is
1359 * used): cloog_loop_add_list instead of cloog_loop_add.
1361 CloogLoop * cloog_loop_generate_general(loop, context, level, scalar,
1362 scaldims, nb_scattdims, nb_par, options)
1363 CloogLoop * loop ;
1364 CloogDomain * context ;
1365 int level, scalar, * scaldims, nb_scattdims, nb_par ;
1366 CloogOptions * options ;
1367 { Value one ;
1368 CloogLoop * res, * now, * temp, * l, * new_loop, * inner, * now2, * end,
1369 * next, * into ;
1370 CloogDomain * domain ;
1372 /* 3. Separate all projections into disjoint polyhedra. */
1373 res = ((options->f > level+scalar) || (options->f < 0)) ?
1374 cloog_loop_merge(loop, nb_par, options) : cloog_loop_separate(loop);
1376 /* 3b. -correction- sort the loops to determine their textual order. */
1377 res = cloog_loop_sort(res,level,nb_par) ;
1379 value_init_c(one) ;
1380 value_set_si(one,1) ;
1382 /* 4. Recurse for each loop with the current domain as context. */
1383 temp = res ;
1384 res = NULL ;
1385 if ((level+scalar < options->l) || (options->l < 0))
1386 while(temp != NULL)
1387 { if (options->strides)
1388 cloog_loop_stride(temp,level,nb_par) ;
1389 inner = temp->inner ;
1390 domain = temp->domain ;
1391 into = NULL ;
1392 while (inner != NULL)
1393 { /* 4b. -ced- recurse for each sub-list of non terminal loops. */
1394 if (cloog_domain_dimension(inner->domain) > (level + nb_par))
1395 { end = inner ;
1396 while ((end->next != NULL) &&
1397 (cloog_domain_dimension(end->next->domain) > (level + nb_par)))
1398 end = end->next ;
1400 next = end->next ;
1401 end->next = NULL ;
1403 l = cloog_loop_generate(inner,domain,level+1,scalar,
1404 scaldims,nb_scattdims,nb_par,options) ;
1406 if (l != NULL)
1407 cloog_loop_add_list(&into,&now,l) ;
1409 inner = next ;
1411 else
1412 { cloog_loop_add(&into,&now,inner) ;
1413 inner = inner->next ;
1416 next = temp->next ;
1417 temp->next = NULL ;
1418 temp->inner = into ;
1419 cloog_loop_add(&res,&now2,temp) ;
1420 temp = next ;
1422 else
1423 while (temp != NULL)
1424 { next = temp->next ;
1425 l = cloog_loop_nest(temp->inner,temp->domain,level+1,nb_par) ;
1426 new_loop = cloog_loop_alloc(temp->domain,one,NULL,l,NULL) ;
1427 temp->inner = NULL ;
1428 temp->next = NULL ;
1429 cloog_loop_free_parts(temp,0,0,0,0) ;
1430 cloog_loop_add(&res,&now,new_loop) ;
1431 temp = next ;
1434 /* 5. eliminate unused iterations of the current level for the new one. See
1435 * the example called linearity-1-1 example with and without this part
1436 * for an idea.
1438 if ((!options->nobacktrack) &&
1439 ((level+scalar < options->l) || (options->l < 0)) &&
1440 ((options->f <= level+scalar) && !(options->f < 0)))
1441 res = cloog_loop_generate_backtrack(res,context,level,nb_par,options) ;
1443 /* Pray for my new paper to be accepted somewhere since the following stuff
1444 * is really amazing :-) !
1445 * Far long later: The paper has been accepted to PACT 2004 :-))). But there
1446 * are still some bugs and I have no time to fix them. Thus now you have to
1447 * pray for me to get an academic position for that really amazing stuff :-) !
1448 * Later again: OK, I get my academic position, but still I have not enough
1449 * time to fix and clean this part... Pray again :-) !!!
1451 /* res = cloog_loop_unisolate(res,context,level,nb_par) ;*/
1453 value_clear_c(one) ;
1454 return(res) ;
1459 * cloog_loop_generate_scalar function:
1460 * This function applies the simplified code generation scheme in the trivial
1461 * case of scalar dimensions. When dealing with scalar dimensions, there is
1462 * no need of costly polyhedral operations for separation or sorting: sorting
1463 * is a question of comparing scalar vectors and separation amounts to consider
1464 * only loops with the same scalar vector for the next step of the code
1465 * generation process. This function achieves the separation/sorting process
1466 * for the vector of scalar dimension that begins at dimension 'level+scalar'
1467 * and finish to the first non-scalar dimension.
1468 * - loop is the loop for which we have to generate a scanning code,
1469 * - context is the context of the current loop (constraints on parameter and/or
1470 * on outer loop counters),
1471 * - level is the current non-scalar dimension,
1472 * - scalar is the current scalar dimension,
1473 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1474 * - nb_scattdims is the size of the scaldims array,
1475 * - nb_par is the number of parameters,
1476 * - options are the general code generation options.
1478 * - September 2nd 2005: First version.
1480 CloogLoop * cloog_loop_generate_scalar(loop, context, level, scalar,
1481 scaldims, nb_scattdims, nb_par, options)
1482 CloogLoop * loop ;
1483 CloogDomain * context ;
1484 int level, scalar, * scaldims, nb_scattdims, nb_par ;
1485 CloogOptions * options ;
1486 { CloogLoop * res, * now, * temp, * l, * end, * next, * ref ;
1488 /* We sort the loop list with respect to the current scalar vector. */
1489 res = cloog_loop_scalar_sort(loop,level,scaldims,nb_scattdims,scalar) ;
1491 temp = res ;
1492 res = NULL ;
1493 while (temp != NULL)
1494 { /* Then we will appy the general code generation process to each sub-list
1495 * of loops with the same scalar vector.
1497 end = temp ;
1498 ref = temp ;
1500 while((end->next != NULL) &&
1501 cloog_loop_scalar_eq(ref,end->next,level,scaldims,nb_scattdims,scalar))
1502 end = end->next ;
1504 next = end->next ;
1505 end->next = NULL ;
1507 /* For the next dimension, scalar value is updated by adding the scalar
1508 * vector size, which is stored at scaldims[level+scalar-1].
1510 l = cloog_loop_generate_general(temp,context,level,
1511 scalar+scaldims[level+scalar-1],
1512 scaldims,nb_scattdims,nb_par,options) ;
1514 if (l != NULL)
1515 cloog_loop_add_list(&res,&now,l) ;
1517 temp = next ;
1520 return res ;
1525 * cloog_loop_generate function:
1526 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1527 * Quillere algorithm for polyhedron scanning from step 1 to 2.
1528 * (see the Quillere paper).
1529 * - loop is the loop for which we have to generate a scanning code,
1530 * - context is the context of the current loop (constraints on parameter and/or
1531 * on outer loop counters),
1532 * - level is the current non-scalar dimension,
1533 * - scalar is the current scalar dimension,
1534 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1535 * - nb_scattdims is the size of the scaldims array,
1536 * - nb_par is the number of parameters,
1537 * - options are the general code generation options.
1539 * - October 26th 2001: first version.
1540 * - July 3rd->11th 2003: memory leaks hunt and correction.
1541 * - June 15th 2005: a memory leak fixed (loop was not entirely freed when
1542 * the result of cloog_loop_restrict was NULL).
1543 * - June 22nd 2005: Adaptation for GMP.
1544 * - September 2nd 2005: The function have been cutted out in two pieces:
1545 * cloog_loop_generate and this one, in order to handle
1546 * the scalar dimension case more efficiently with
1547 * cloog_loop_generate_scalar.
1548 * - November 15th 2005: (debug) Condition for stop option no more take care of
1549 * further scalar dimensions.
1551 CloogLoop * cloog_loop_generate(loop, context, level, scalar,
1552 scaldims, nb_scattdims, nb_par, options)
1553 CloogLoop * loop ;
1554 CloogDomain * context ;
1555 int level, scalar, * scaldims, nb_scattdims, nb_par ;
1556 CloogOptions * options ;
1557 { CloogLoop * res, * now, * temp, * next, * old ;
1559 /* If the user asked to stop code generation at this level, let's stop. */
1560 if ((options->stop >= 0) && (level+scalar >= options->stop+1))
1561 return cloog_loop_stop(loop,context) ;
1563 res = NULL ;
1565 /* 1. Replace each polyhedron by its intersection with the context.
1566 * 2. Compute the projection of each polyhedron onto the outermost
1567 * loop variable and the parameters.
1569 while (loop != NULL)
1570 { next = loop->next ;
1571 temp = cloog_loop_restrict(loop,context,nb_par) ;
1573 if (temp != NULL)
1574 { old = temp ;
1575 temp = cloog_loop_project(temp,level,nb_par) ;
1576 cloog_loop_free_parts(old,0,0,0,0) ;
1577 cloog_loop_add(&res,&now,temp) ;
1578 cloog_loop_free_parts(loop,1,0,0,0) ;
1580 else
1581 { loop->next = NULL ;
1582 cloog_loop_free(loop) ;
1585 loop = next ;
1587 if (res == NULL)
1588 return NULL ;
1590 /* To save both time and memory, we switch here depending on whether the
1591 * current dimension is scalar (simplified processing) or not (general
1592 * processing).
1594 if ((level+scalar <= nb_scattdims) && (scaldims[level+scalar-1]))
1595 res = cloog_loop_generate_scalar(res,context,level,scalar,
1596 scaldims,nb_scattdims,nb_par,options) ;
1597 else
1598 res = cloog_loop_generate_general(res,context,level,scalar,
1599 scaldims,nb_scattdims,nb_par,options) ;
1601 return res ;
1606 * cloog_loop_simplify function:
1607 * This function implements the part 6. of the Quillere algorithm, it
1608 * recursively simplifies each loop in the context of the preceding loop domain.
1609 * It returns a pointer to the simplified loop list.
1610 * The cloog_domain_simplify (DomainSimplify) behaviour is really bad with
1611 * polyhedra union and some really awful sidesteppings were written, I plan
1612 * to solve that...
1613 * - October 31th 2001: first version.
1614 * - July 3rd->11th 2003: memory leaks hunt and correction.
1615 * - April 16th 2005: a memory leak fixed (extended_context was not freed).
1616 * - June 15th 2005: a memory leak fixed (loop was not conveniently freed
1617 * when the constraint system is never true).
1618 * - October 27th 2005: - this function called before cloog_loop_fast_simplify
1619 * is now the official cloog_loop_simplify function in
1620 * replacement of a slower and more complex one (after
1621 * deep changes in the pretty printer).
1622 * - we use cloog_loop_disjoint to fix the problem when
1623 * simplifying gives a union of polyhedra (before, it
1624 * was under the responsibility of the pretty printer).
1626 CloogLoop * cloog_loop_simplify(loop, context, level, nb_par)
1627 CloogLoop * loop ;
1628 CloogDomain * context ;
1629 int level, nb_par ;
1630 { int domain_dim ;
1631 CloogBlock * new_block ;
1632 CloogLoop * simplified, * inner, * next ;
1633 CloogDomain * domain, * simp, * inter, * extended_context ;
1635 if (loop == NULL)
1636 return(NULL) ;
1638 domain = loop->domain ;
1640 next = cloog_loop_simplify(loop->next,context,level,nb_par) ;
1642 domain_dim = cloog_domain_dimension(domain) - nb_par ;
1643 extended_context=cloog_domain_extend(context,domain_dim,nb_par);
1644 inter = cloog_domain_intersection(domain,extended_context) ;
1645 simp = cloog_domain_simplify(inter,extended_context) ;
1646 cloog_domain_free(extended_context) ;
1648 /* If the constraint system is never true, go to the next one. */
1649 if (cloog_domain_never_integral(simp)) {
1650 loop->next = NULL;
1651 cloog_loop_free(loop);
1652 cloog_domain_free(inter);
1653 cloog_domain_free(simp);
1654 return next;
1657 inner = cloog_loop_simplify(loop->inner,inter,level+1,nb_par) ;
1658 cloog_domain_free(inter) ;
1660 if ((inner == NULL) && (loop->block == NULL))
1661 { loop->inner = NULL ; /* For loop integrity. */
1662 loop->next = NULL ; /* For loop integrity. */
1663 cloog_loop_free_parts(loop,1,1,1,0) ;
1664 cloog_domain_free(simp);
1665 return(next) ;
1668 new_block = cloog_block_copy(loop->block) ;
1670 simplified = cloog_loop_alloc(simp,loop->stride,new_block,inner,next) ;
1672 /* Examples like test/iftest2.cloog give unions of polyhedra after
1673 * simplifying, thus we we have to disjoint them. Another good reason to
1674 * put the simplifying step in the Quillere backtrack.
1676 simplified = cloog_loop_disjoint(simplified) ;
1678 loop->inner = NULL ; /* For loop integrity. */
1679 loop->next = NULL ; /* For loop integrity. */
1680 cloog_loop_free_parts(loop,1,1,0,0) ;
1682 return(simplified) ;
1687 * cloog_loop_scatter function:
1688 * This function add the scattering (scheduling) informations in a loop.
1689 * - November 4th 2001: first version.
1691 void cloog_loop_scatter(CloogLoop * loop, CloogDomain * scatt)
1692 { int scatt_dim ;
1693 CloogDomain * domain, * ext, * newdom, * newpart, * temp ;
1695 domain = loop->domain ;
1696 newdom = NULL ;
1697 scatt_dim = cloog_domain_dimension(scatt) - cloog_domain_dimension(domain) ;
1699 /* For each polyhedron of domain (it can be an union of polyhedra). */
1700 while (domain != NULL)
1701 { /* Extend the domain by adding the scattering dimensions as the new
1702 * first domain dimensions.
1704 ext = cloog_domain_extend(domain,scatt_dim,cloog_domain_dimension(domain)) ;
1705 /* Then add the scattering constraints. */
1706 newpart = cloog_domain_addconstraints(scatt,ext) ;
1707 cloog_domain_free(ext) ;
1709 if (newdom != NULL)
1710 { temp = newdom ;
1711 newdom = cloog_domain_union(newdom,newpart) ;
1712 cloog_domain_free(temp) ;
1713 cloog_domain_free(newpart) ;
1715 else
1716 newdom = newpart ;
1718 /* We don't want to free the rest of the list. */
1719 temp = domain ;
1720 domain = cloog_domain_cut_first(temp) ;
1721 cloog_domain_free(temp) ;
1724 loop->domain = newdom ;