cloog_loop_constant: don't drop constraints on outer loop iterators
[cloog.git] / source / loop.c
blobe73904dc266e9e7e6f1dde249cbbf3e719c8cfda
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 library is free software; you can redistribute it and/or *
18 * modify it under the terms of the GNU Lesser General Public *
19 * License as published by the Free Software Foundation; either *
20 * version 2.1 of the License, or (at your option) any later version. *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
25 * Lesser General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU Lesser General Public *
28 * License along with this library; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
30 * Boston, MA 02110-1301 USA *
31 * *
32 * CLooG, the Chunky Loop Generator *
33 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
34 * *
35 ******************************************************************************/
36 /* CAUTION: the english used for comments is probably the worst you ever read,
37 * please feel free to correct and improve it !
40 # include <stdlib.h>
41 # include <stdio.h>
42 # include "../include/cloog/cloog.h"
44 #define ALLOC(type) (type*)malloc(sizeof(type))
47 /******************************************************************************
48 * Memory leaks hunting *
49 ******************************************************************************/
52 /**
53 * These functions and global variables are devoted to memory leaks hunting: we
54 * want to know at each moment how many CloogLoop structures had been allocated
55 * (cloog_loop_allocated) and how many had been freed (cloog_loop_freed).
56 * Each time a CloogLoog structure is allocated, a call to the function
57 * cloog_loop_leak_up() must be carried out, and respectively
58 * cloog_loop_leak_down() when a CloogLoop structure is freed. The special
59 * variable cloog_loop_max gives the maximal number of CloogLoop structures
60 * simultaneously alive (i.e. allocated and non-freed) in memory.
61 * - July 3rd->11th 2003: first version (memory leaks hunt and correction).
65 static void cloog_loop_leak_up(CloogState *state)
67 state->loop_allocated++;
68 if ((state->loop_allocated - state->loop_freed) > state->loop_max)
69 state->loop_max = state->loop_allocated - state->loop_freed;
73 static void cloog_loop_leak_down(CloogState *state)
75 state->loop_freed++;
79 /******************************************************************************
80 * Structure display function *
81 ******************************************************************************/
84 /**
85 * cloog_loop_print_structure function:
86 * Displays a loop structure in a way that trends to be understandable without
87 * falling in a deep depression or, for the lucky ones, getting a headache...
88 * Written by Olivier Chorier, Luc Marchaud, Pierre Martin and Romain Tartiere.
89 * - April 24th 2005: Initial version.
90 * - May 21rd 2005: - New parameter `F' for destination file (ie stdout),
91 * - Minor tweaks.
92 * - May 26th 2005: Memory leak hunt.
93 * - June 2nd 2005: (Ced) Integration and minor fixes.
94 * -June 22nd 2005: (Ced) Adaptation for GMP.
96 void cloog_loop_print_structure(FILE * file, CloogLoop * loop, int level)
97 { int i, j, first=1 ;
99 if (loop)
100 { /* Go to the right level. */
101 for (i=0; i<level; i++)
102 fprintf(file,"|\t") ;
104 fprintf(file,"+-- CloogLoop\n") ;
107 /* For each loop. */
108 while (loop)
109 { if (!first)
110 { /* Go to the right level. */
111 for (i=0; i<level; i++)
112 fprintf(file,"|\t") ;
114 fprintf(file,"| CloogLoop\n") ;
116 else
117 first = 0 ;
119 /* A blank line. */
120 for(j=0; j<=level+1; j++)
121 fprintf(file,"|\t") ;
122 fprintf(file,"\n") ;
124 /* Print the domain. */
125 cloog_domain_print_structure(file, loop->domain, level+1, "CloogDomain");
127 /* Print the stride. */
128 for(j=0; j<=level; j++)
129 fprintf(file,"|\t") ;
130 if (loop->stride) {
131 fprintf(file, "Stride: ");
132 cloog_int_print(file, loop->stride->stride);
133 fprintf(file, "\n");
134 fprintf(file, "Offset: ");
135 cloog_int_print(file, loop->stride->offset);
136 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(loop->state);
209 next = loop->next ;
210 cloog_domain_free(loop->domain) ;
211 cloog_domain_free(loop->unsimplified);
212 cloog_block_free(loop->block) ;
213 if (loop->inner != NULL)
214 cloog_loop_free(loop->inner) ;
216 cloog_stride_free(loop->stride);
217 free(loop) ;
218 loop = next ;
224 * cloog_loop_free_parts function:
225 * This function frees the allocated memory for some parts of a CloogLoop
226 * structure (loop), each other argument is a boolean having to be set to 1 if
227 * we want to free the corresponding part, 0 otherwise. This function applies
228 * the same freeing policy to its inner ans next loops recursively.
229 * - July 3rd 2003: first version.
230 * - June 22nd 2005: Adaptation for GMP.
232 void cloog_loop_free_parts(loop, domain, block, inner, next)
233 CloogLoop * loop ;
234 int domain, block, inner, next ;
235 { CloogLoop * follow ;
237 while (loop != NULL) {
238 cloog_loop_leak_down(loop->state);
239 follow = loop->next ;
241 if (domain)
242 cloog_domain_free(loop->domain) ;
244 if (block)
245 cloog_block_free(loop->block) ;
247 if ((inner) && (loop->inner != NULL))
248 cloog_loop_free_parts(loop->inner,domain,block,inner,1) ;
250 cloog_stride_free(loop->stride);
251 free(loop) ;
252 if (next)
253 loop = follow ;
254 else
255 loop = NULL ;
260 /******************************************************************************
261 * Reading functions *
262 ******************************************************************************/
266 * Construct a CloogLoop structure from a given iteration domain
267 * and statement number.
269 CloogLoop *cloog_loop_from_domain(CloogState *state, CloogDomain *domain,
270 int number)
272 int nb_iterators;
273 CloogLoop * loop ;
274 CloogStatement * statement ;
276 /* Memory allocation and information reading for the first domain: */
277 loop = cloog_loop_malloc(state);
278 /* domain. */
279 loop->domain = domain;
280 if (loop->domain != NULL)
281 nb_iterators = cloog_domain_dimension(loop->domain);
282 else
283 nb_iterators = 0 ;
284 /* included statement block. */
285 statement = cloog_statement_alloc(state, number + 1);
286 loop->block = cloog_block_alloc(statement, 0, NULL, nb_iterators);
288 return loop ;
293 * cloog_loop_read function:
294 * This function reads loop data from a file (foo, possibly stdin) and
295 * returns a pointer to a CloogLoop structure containing the read information.
296 * This function can be used only for input file reading, when one loop is
297 * associated with one statement.
298 * - number is the statement block number carried by the loop (-1 if none).
299 * - nb_parameters is the number of parameters.
301 * - September 9th 2002: first version.
302 * - April 16th 2005: adaptation to new CloogStatement struct (with number).
303 * - June 11th 2005: adaptation to new CloogBlock structure.
304 * - June 22nd 2005: Adaptation for GMP.
306 CloogLoop *cloog_loop_read(CloogState *state,
307 FILE *foo, int number, int nb_parameters)
309 int op1, op2, op3;
310 char s[MAX_STRING];
311 CloogDomain *domain;
313 domain = cloog_domain_union_read(state, foo, nb_parameters);
315 /* To read that stupid "0 0 0" line. */
316 while (fgets(s,MAX_STRING,foo) == 0) ;
317 while ((*s=='#' || *s=='\n') || (sscanf(s," %d %d %d",&op1,&op2,&op3)<3))
318 fgets(s,MAX_STRING,foo) ;
320 return cloog_loop_from_domain(state, domain, number);
324 /******************************************************************************
325 * Processing functions *
326 ******************************************************************************/
330 * cloog_loop_malloc function:
331 * This function allocates the memory space for a CloogLoop structure and
332 * sets its fields with default values. Then it returns a pointer to the
333 * allocated space.
334 * - November 21th 2005: first version.
336 CloogLoop *cloog_loop_malloc(CloogState *state)
337 { CloogLoop * loop ;
339 /* Memory allocation for the CloogLoop structure. */
340 loop = (CloogLoop *)malloc(sizeof(CloogLoop)) ;
341 if (loop == NULL)
342 cloog_die("memory overflow.\n");
343 cloog_loop_leak_up(state);
346 /* We set the various fields with default values. */
347 loop->state = state;
348 loop->domain = NULL ;
349 loop->unsimplified = NULL;
350 loop->block = NULL ;
351 loop->usr = NULL;
352 loop->inner = NULL ;
353 loop->next = NULL ;
354 loop->otl = 0;
355 loop->stride = NULL;
357 return loop ;
362 * cloog_loop_alloc function:
363 * This function allocates the memory space for a CloogLoop structure and
364 * sets its fields with those given as input. Then it returns a pointer to the
365 * allocated space.
366 * - October 27th 2001: first version.
367 * - June 22nd 2005: Adaptation for GMP.
368 * - November 21th 2005: use of cloog_loop_malloc.
370 CloogLoop *cloog_loop_alloc(CloogState *state,
371 CloogDomain *domain, int otl, CloogStride *stride,
372 CloogBlock *block, CloogLoop *inner, CloogLoop *next)
373 { CloogLoop * loop ;
375 loop = cloog_loop_malloc(state);
377 loop->domain = domain ;
378 loop->block = block ;
379 loop->inner = inner ;
380 loop->next = next ;
381 loop->otl = otl;
382 loop->stride = cloog_stride_copy(stride);
384 return(loop) ;
389 * cloog_loop_add function:
390 * This function adds a CloogLoop structure (loop) at a given place (now) of a
391 * NULL terminated list of CloogLoop structures. The beginning of this list
392 * is (start). This function updates (now) to (loop), and updates (start) if the
393 * added element is the first one -that is when (start) is NULL-.
394 * - October 28th 2001: first version.
396 void cloog_loop_add(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
397 { if (*start == NULL)
398 { *start = loop ;
399 *now = *start ;
401 else
402 { (*now)->next = loop ;
403 *now = (*now)->next ;
409 * cloog_loop_add function:
410 * This function adds a CloogLoop structure (loop) at a given place (now) of a
411 * NULL terminated list of CloogLoop structures. The beginning of this list
412 * is (start). This function updates (now) to the end of the loop list (loop),
413 * and updates (start) if the added element is the first one -that is when
414 * (start) is NULL-.
415 * - September 9th 2005: first version.
417 void cloog_loop_add_list(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
418 { if (*start == NULL)
419 { *start = loop ;
420 *now = *start ;
422 else
423 { (*now)->next = loop ;
424 *now = (*now)->next ;
427 while ((*now)->next != NULL)
428 *now = (*now)->next ;
433 * cloog_loop_copy function:
434 * This function returns a copy of the CloogLoop structure given as input. In
435 * fact, there is just new allocations for the CloogLoop structures, but their
436 * contents are the same.
437 * - October 28th 2001: first version.
438 * - July 3rd->11th 2003: memory leaks hunt and correction.
440 CloogLoop * cloog_loop_copy(CloogLoop * source)
441 { CloogLoop * loop ;
442 CloogBlock * block ;
443 CloogDomain * domain ;
445 loop = NULL ;
446 if (source != NULL)
447 { domain = cloog_domain_copy(source->domain) ;
448 block = cloog_block_copy(source->block) ;
449 loop = cloog_loop_alloc(source->state, domain, source->otl,
450 source->stride, block, NULL, NULL);
451 loop->usr = source->usr;
452 loop->inner = cloog_loop_copy(source->inner) ;
453 loop->next = cloog_loop_copy(source->next) ;
455 return(loop) ;
460 * cloog_loop_add_disjoint function:
461 * This function adds some CloogLoop structures at a given place (now) of a
462 * NULL terminated list of CloogLoop structures. The beginning of this list
463 * is (start). (loop) can be an union of polyhedra, this function separates the
464 * union into a list of *disjoint* polyhedra then adds the list. This function
465 * updates (now) to the end of the list and updates (start) if first added
466 * element is the first of the principal list -that is when (start) is NULL-.
467 * (loop) can be freed by this function, basically when its domain is actually
468 * a union of polyhedra, but don't worry, all the useful data are now stored
469 * inside the list (start). We do not use PolyLib's Domain_Disjoint function,
470 * since the number of union components is often higher (thus code size too).
471 * - October 28th 2001: first version.
472 * - November 14th 2001: bug correction (this one was hard to find !).
473 * - July 3rd->11th 2003: memory leaks hunt and correction.
474 * - June 22nd 2005: Adaptation for GMP.
475 * - October 27th 2005: (debug) included blocks were not copied for new loops.
477 void cloog_loop_add_disjoint(start, now, loop)
478 CloogLoop ** start, ** now, * loop ;
480 CloogLoop * sep, * inner ;
481 CloogDomain *domain, *seen, *temp, *rest;
482 CloogBlock * block ;
484 if (cloog_domain_isconvex(loop->domain))
485 cloog_loop_add(start,now,loop) ;
486 else {
487 domain = cloog_domain_simplify_union(loop->domain);
488 loop->domain = NULL ;
490 /* We separate the first element of the rest of the union. */
491 domain = cloog_domain_cut_first(domain, &rest);
493 /* This first element is the first of the list of disjoint polyhedra. */
494 sep = cloog_loop_alloc(loop->state, domain, 0, NULL,
495 loop->block, loop->inner, NULL);
496 cloog_loop_add(start,now,sep) ;
498 seen = cloog_domain_copy(domain);
499 while (!cloog_domain_isempty(domain = rest)) {
500 temp = cloog_domain_cut_first(domain, &rest);
501 domain = cloog_domain_difference(temp, seen);
502 cloog_domain_free(temp);
504 if (cloog_domain_isempty(domain)) {
505 cloog_domain_free(domain);
506 continue;
509 /* Each new loop will have its own life, for instance we can free its
510 * inner loop and included block. Then each one must have its own copy
511 * of both 'inner' and 'block'.
513 inner = cloog_loop_copy(loop->inner) ;
514 block = cloog_block_copy(loop->block) ;
516 sep = cloog_loop_alloc(loop->state, cloog_domain_copy(domain),
517 0, NULL, block, inner, NULL);
518 /* domain can be an union too. If so: recursion. */
519 if (cloog_domain_isconvex(domain))
520 cloog_loop_add(start,now,sep) ;
521 else
522 cloog_loop_add_disjoint(start,now,sep) ;
524 if (cloog_domain_isempty(rest)) {
525 cloog_domain_free(domain);
526 break;
529 seen = cloog_domain_union(seen, domain);
531 cloog_domain_free(rest);
532 cloog_domain_free(seen);
533 cloog_loop_free_parts(loop,0,0,0,0) ;
539 * cloog_loop_disjoint function:
540 * This function returns a list of loops such that each loop with non-convex
541 * domain in the input list (loop) is separated into several loops where the
542 * domains are the components of the union of *disjoint* polyhedra equivalent
543 * to the original non-convex domain. See cloog_loop_add_disjoint comments
544 * for more details.
545 * - September 16th 2005: first version.
547 CloogLoop * cloog_loop_disjoint(CloogLoop * loop)
548 { CloogLoop *res=NULL, * now=NULL, * next ;
550 /* Because this is often the case, don't waste time ! */
551 if (loop && !loop->next && cloog_domain_isconvex(loop->domain))
552 return loop ;
554 while (loop != NULL)
555 { next = loop->next ;
556 loop->next = NULL ;
557 cloog_loop_add_disjoint(&res,&now,loop) ;
558 loop = next ;
561 return res ;
566 * cloog_loop_restrict function:
567 * This function returns the (loop) in the context of (context): it makes the
568 * intersection between the (loop) domain and the (context), then it returns
569 * a pointer to a new loop, with this intersection as domain.
571 * - October 27th 2001: first version.
572 * - June 15th 2005: a memory leak fixed (domain was not freed when empty).
573 * - June 22nd 2005: Adaptation for GMP.
575 CloogLoop *cloog_loop_restrict(CloogLoop *loop, CloogDomain *context)
576 { int new_dimension ;
577 CloogDomain * domain, * extended_context, * new_domain ;
578 CloogLoop * new_loop ;
580 domain = loop->domain ;
581 if (cloog_domain_dimension(domain) > cloog_domain_dimension(context))
583 new_dimension = cloog_domain_dimension(domain);
584 extended_context = cloog_domain_extend(context, new_dimension);
585 new_domain = cloog_domain_intersection(extended_context,loop->domain) ;
586 cloog_domain_free(extended_context) ;
588 else
589 new_domain = cloog_domain_intersection(context,loop->domain) ;
591 if (cloog_domain_isempty(new_domain))
592 { cloog_domain_free(new_domain) ;
593 return(NULL) ;
595 else {
596 new_loop = cloog_loop_alloc(loop->state, new_domain,
597 0, NULL, loop->block, loop->inner, NULL);
598 return(new_loop) ;
604 * Call cloog_loop_restrict on each loop in the list "loop" and return
605 * the concatenated result.
607 CloogLoop *cloog_loop_restrict_all(CloogLoop *loop, CloogDomain *context)
609 CloogLoop *next;
610 CloogLoop *res = NULL;
611 CloogLoop **res_next = &res;
613 for (; loop; loop = next) {
614 next = loop->next;
616 *res_next = cloog_loop_restrict(loop, context);
617 if (*res_next) {
618 res_next = &(*res_next)->next;
619 cloog_loop_free_parts(loop, 1, 0, 0, 0);
620 } else {
621 loop->next = NULL;
622 cloog_loop_free(loop);
626 return res;
631 * Restrict the domains of the inner loops of each loop l in the given
632 * list of loops to the domain of the loop l. If the domains of all
633 * inner loops of a given loop l turn out to be empty, then remove l
634 * from the list.
636 CloogLoop *cloog_loop_restrict_inner(CloogLoop *loop)
638 CloogLoop *next;
639 CloogLoop *res;
640 CloogLoop **res_next = &res;
642 for (; loop; loop = next) {
643 next = loop->next;
645 loop->inner = cloog_loop_restrict_all(loop->inner, loop->domain);
646 if (loop->inner) {
647 *res_next = loop;
648 res_next = &(*res_next)->next;
649 } else {
650 loop->next = NULL;
651 cloog_loop_free(loop);
655 *res_next = NULL;
657 return res;
661 * cloog_loop_project function:
662 * This function returns the projection of (loop) on the (level) first
663 * dimensions (outer loops). It makes the projection of the (loop) domain,
664 * then it returns a pointer to a new loop, with this projection as domain.
666 * - October 27th 2001: first version.
667 * - July 3rd->11th 2003: memory leaks hunt and correction.
668 * - June 22nd 2005: Adaptation for GMP.
670 CloogLoop * cloog_loop_project(CloogLoop * loop, int level)
672 CloogDomain * new_domain ;
673 CloogLoop * new_loop, * copy ;
675 copy = cloog_loop_alloc(loop->state, loop->domain, loop->otl, loop->stride,
676 loop->block, loop->inner, NULL);
678 if (cloog_domain_dimension(loop->domain) == level)
679 new_domain = cloog_domain_copy(loop->domain) ;
680 else
681 new_domain = cloog_domain_project(loop->domain, level);
683 new_loop = cloog_loop_alloc(loop->state, new_domain, 0, NULL,
684 NULL, copy, NULL);
686 return(new_loop) ;
691 * Call cloog_loop_project on each loop in the list "loop" and return
692 * the concatenated result.
694 CloogLoop *cloog_loop_project_all(CloogLoop *loop, int level)
696 CloogLoop *next;
697 CloogLoop *res = NULL;
698 CloogLoop **res_next = &res;
700 for (; loop; loop = next) {
701 next = loop->next;
703 *res_next = cloog_loop_project(loop, level);
704 res_next = &(*res_next)->next;
705 cloog_loop_free_parts(loop, 0, 0, 0, 0);
708 return res;
713 * cloog_loop_concat function:
714 * This function returns a pointer to the concatenation of the
715 * CloogLoop lists given as input.
716 * - October 28th 2001: first version.
718 CloogLoop * cloog_loop_concat(CloogLoop * a, CloogLoop * b)
719 { CloogLoop * loop, * temp ;
721 loop = a ;
722 temp = loop ;
723 if (loop != NULL)
724 { while (temp->next != NULL)
725 temp = temp->next ;
726 temp->next = b ;
728 else
729 loop = b ;
731 return(loop) ;
736 * cloog_loop_combine:
737 * Combine consecutive loops with identical domains into
738 * a single loop with the concatenation of their inner loops
739 * as inner loop.
741 CloogLoop *cloog_loop_combine(CloogLoop *loop)
743 CloogLoop *first, *second;
745 for (first = loop; first; first = first->next) {
746 while (first->next) {
747 if (!cloog_domain_lazy_equal(first->domain, first->next->domain))
748 break;
749 second = first->next;
750 first->inner = cloog_loop_concat(first->inner, second->inner);
751 first->next = second->next;
752 cloog_loop_free_parts(second, 1, 0, 0, 0);
756 return loop;
760 * Remove loops from list that have an empty domain.
762 CloogLoop *cloog_loop_remove_empty_domain_loops(CloogLoop *loop)
764 CloogLoop *l, *res, *next, **res_next;
766 res = NULL;
767 res_next = &res;
768 for (l = loop; l; l = next) {
769 next = l->next;
770 if (cloog_domain_isempty(l->domain))
771 cloog_loop_free_parts(l, 1, 1, 1, 0);
772 else {
773 *res_next = l;
774 res_next = &(*res_next)->next;
777 *res_next = NULL;
779 return res;
782 CloogLoop *cloog_loop_decompose_inner(CloogLoop *loop,
783 int level, int scalar, int *scaldims, int nb_scattdims);
785 /* For each loop with only one inner loop, replace the domain
786 * of the loop with the projection of the domain of the inner
787 * loop. To increase the number of loops with a single inner
788 * we first decompose the inner loops into strongly connected
789 * components.
791 CloogLoop *cloog_loop_specialize(CloogLoop *loop,
792 int level, int scalar, int *scaldims, int nb_scattdims)
794 int dim;
795 CloogDomain *domain;
796 CloogLoop *l;
798 loop = cloog_loop_decompose_inner(loop, level, scalar,
799 scaldims, nb_scattdims);
801 for (l = loop; l; l = l->next) {
802 if (l->inner->next)
803 continue;
804 if (!cloog_domain_isconvex(l->inner->domain))
805 continue;
807 dim = cloog_domain_dimension(l->domain);
808 domain = cloog_domain_project(l->inner->domain, dim);
809 if (cloog_domain_isconvex(domain)) {
810 cloog_domain_free(l->domain);
811 l->domain = domain;
812 } else {
813 cloog_domain_free(domain);
817 return cloog_loop_remove_empty_domain_loops(loop);
820 /* For each loop with only one inner loop, propagate the bounds from
821 * the inner loop domain to the outer loop domain. This is especially
822 * useful if the inner loop domain has a non-trivial stride which
823 * results in an update of the lower bound.
825 CloogLoop *cloog_loop_propagate_lower_bound(CloogLoop *loop, int level)
827 int dim;
828 CloogDomain *domain, *t;
829 CloogLoop *l;
831 for (l = loop; l; l = l->next) {
832 if (l->inner->next)
833 continue;
834 if (!cloog_domain_isconvex(l->inner->domain))
835 continue;
837 dim = cloog_domain_dimension(l->domain);
838 domain = cloog_domain_project(l->inner->domain, dim);
839 if (cloog_domain_isconvex(domain)) {
840 t = cloog_domain_intersection(domain, l->domain);
841 cloog_domain_free(l->domain);
842 l->domain = t;
844 cloog_domain_free(domain);
847 return loop;
851 * cloog_loop_separate function:
852 * This function implements the Quillere algorithm for separation of multiple
853 * loops: for a given set of polyhedra (loop), it computes a set of disjoint
854 * polyhedra such that the unions of these sets are equal, and returns this set.
855 * - October 28th 2001: first version.
856 * - November 14th 2001: elimination of some unused blocks.
857 * - August 13th 2002: (debug) in the case of union of polyhedra for one
858 * loop, redundant constraints are fired.
859 * - July 3rd->11th 2003: memory leaks hunt and correction.
860 * - June 22nd 2005: Adaptation for GMP.
861 * - October 16th 2005: Removal of the non-shared constraint elimination when
862 * there is only one loop in the list (seems to work
863 * without now, DomainSimplify may have been improved).
864 * The problem was visible with test/iftest2.cloog.
866 CloogLoop * cloog_loop_separate(CloogLoop * loop)
867 { int lazy_equal=0, disjoint = 0;
868 CloogLoop * new_loop, * new_inner, * res, * now, * temp, * Q,
869 * inner, * old /*, * previous, * next*/ ;
870 CloogDomain *UQ, *domain;
872 if (loop == NULL)
873 return NULL ;
875 loop = cloog_loop_combine(loop);
877 if (loop->next == NULL)
878 return cloog_loop_disjoint(loop) ;
880 UQ = cloog_domain_copy(loop->domain) ;
881 domain = cloog_domain_copy(loop->domain) ;
882 res = cloog_loop_alloc(loop->state, domain, 0, NULL,
883 loop->block, loop->inner, NULL);
885 old = loop ;
886 while((loop = loop->next) != NULL)
887 { temp = NULL ;
889 /* For all Q, add Q-loop associated with the blocks of Q alone,
890 * and Q inter loop associated with the blocks of Q and loop.
892 for (Q = res; Q; Q = Q->next) {
893 /* Add (Q inter loop). */
894 if ((disjoint = cloog_domain_lazy_disjoint(Q->domain,loop->domain)))
895 domain = NULL ;
896 else
897 { if ((lazy_equal = cloog_domain_lazy_equal(Q->domain,loop->domain)))
898 domain = cloog_domain_copy(Q->domain) ;
899 else
900 domain = cloog_domain_intersection(Q->domain,loop->domain) ;
902 if (!cloog_domain_isempty(domain))
903 { new_inner = cloog_loop_concat(cloog_loop_copy(Q->inner),
904 cloog_loop_copy(loop->inner)) ;
905 new_loop = cloog_loop_alloc(loop->state, domain, 0, NULL,
906 NULL, new_inner, NULL);
907 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
909 else {
910 disjoint = 1;
911 cloog_domain_free(domain);
915 /* Add (Q - loop). */
916 if (disjoint)
917 domain = cloog_domain_copy(Q->domain) ;
918 else
919 { if (lazy_equal)
920 domain = cloog_domain_empty(Q->domain);
921 else
922 domain = cloog_domain_difference(Q->domain,loop->domain) ;
925 if (!cloog_domain_isempty(domain)) {
926 new_loop = cloog_loop_alloc(loop->state, domain, 0, NULL,
927 NULL, Q->inner, NULL);
928 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
930 else
931 { cloog_domain_free(domain) ;
932 /* If Q->inner is no more useful, we can free it. */
933 inner = Q->inner ;
934 Q->inner = NULL ;
935 cloog_loop_free(inner) ;
939 /* Add loop-UQ associated with the blocks of loop alone.*/
940 if (cloog_domain_lazy_disjoint(loop->domain,UQ))
941 domain = cloog_domain_copy(loop->domain) ;
942 else
943 { if (cloog_domain_lazy_equal(loop->domain,UQ))
944 domain = cloog_domain_empty(UQ);
945 else
946 domain = cloog_domain_difference(loop->domain,UQ) ;
949 if (!cloog_domain_isempty(domain)) {
950 new_loop = cloog_loop_alloc(loop->state, domain, 0, NULL,
951 NULL, loop->inner, NULL);
952 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
954 else
955 { cloog_domain_free(domain) ;
956 /* If loop->inner is no more useful, we can free it. */
957 cloog_loop_free(loop->inner) ;
960 loop->inner = NULL ;
962 if (loop->next != NULL)
963 UQ = cloog_domain_union(UQ, cloog_domain_copy(loop->domain));
964 else
965 cloog_domain_free(UQ);
967 cloog_loop_free_parts(res,1,0,0,1) ;
969 res = temp ;
971 cloog_loop_free_parts(old,1,0,0,1) ;
973 return(res) ;
977 static CloogDomain *bounding_domain(CloogDomain *dom, CloogOptions *options)
979 if (options->sh)
980 return cloog_domain_simple_convex(dom);
981 else
982 return cloog_domain_convex(dom);
987 * cloog_loop_merge function:
988 * This function is the 'soft' version of loop_separate if we are looking for
989 * a code much simpler (and less efficicient). This function returns the new
990 * CloogLoop list.
991 * - October 29th 2001: first version.
992 * - July 3rd->11th 2003: memory leaks hunt and correction.
993 * - June 22nd 2005: Adaptation for GMP.
995 CloogLoop *cloog_loop_merge(CloogLoop *loop, int level, CloogOptions *options)
997 CloogLoop *res, *new_inner, *old;
998 CloogDomain *new_domain, *temp;
1000 if (loop == NULL)
1001 return loop;
1003 if (loop->next == NULL && cloog_domain_isconvex(loop->domain))
1004 return loop;
1006 old = loop;
1007 temp = loop->domain;
1008 loop->domain = NULL;
1009 new_inner = loop->inner;
1011 for (loop = loop->next; loop; loop = loop->next) {
1012 temp = cloog_domain_union(temp, loop->domain);
1013 loop->domain = NULL;
1014 new_inner = cloog_loop_concat(new_inner, loop->inner);
1017 new_domain = bounding_domain(temp, options);
1019 if (level > 0 && !cloog_domain_is_bounded(new_domain, level) &&
1020 cloog_domain_is_bounded(temp, level)) {
1021 CloogDomain *splitter, *t2;
1023 cloog_domain_free(new_domain);
1024 splitter = cloog_domain_bound_splitter(temp, level);
1026 res = NULL;
1027 while (!cloog_domain_isconvex(splitter)) {
1028 CloogDomain *first, *rest;
1029 first = cloog_domain_cut_first(splitter, &rest);
1030 splitter = rest;
1031 t2 = cloog_domain_intersection(first, temp);
1032 cloog_domain_free(first);
1034 new_domain = bounding_domain(t2, options);
1035 cloog_domain_free(t2);
1037 if (cloog_domain_isempty(new_domain)) {
1038 cloog_domain_free(new_domain);
1039 continue;
1041 res = cloog_loop_alloc(old->state, new_domain, 0, NULL,
1042 NULL, cloog_loop_copy(new_inner), res);
1045 t2 = cloog_domain_intersection(splitter, temp);
1046 cloog_domain_free(splitter);
1048 new_domain = bounding_domain(t2, options);
1049 cloog_domain_free(t2);
1051 if (cloog_domain_isempty(new_domain)) {
1052 cloog_domain_free(new_domain);
1053 cloog_loop_free(new_inner);
1054 } else
1055 res = cloog_loop_alloc(old->state, new_domain, 0, NULL,
1056 NULL, new_inner, res);
1057 } else {
1058 res = cloog_loop_alloc(old->state, new_domain, 0, NULL,
1059 NULL, new_inner, NULL);
1061 cloog_domain_free(temp);
1063 cloog_loop_free_parts(old, 0, 0, 0, 1);
1065 return res;
1069 static int cloog_loop_count(CloogLoop *loop)
1071 int nb_loops;
1073 for (nb_loops = 0; loop; loop = loop->next)
1074 nb_loops++;
1076 return nb_loops;
1081 * cloog_loop_sort function:
1082 * Adaptation from LoopGen 0.4 by F. Quillere. This function sorts a list of
1083 * parameterized disjoint polyhedra, in order to not have lexicographic order
1084 * violation (see Quillere paper).
1085 * - September 16th 2005: inclusion of cloog_loop_number (October 29th 2001).
1087 CloogLoop *cloog_loop_sort(CloogLoop *loop, int level)
1089 CloogLoop *res, *now, **loop_array;
1090 CloogDomain **doms;
1091 int i, nb_loops=0, * permut ;
1093 /* There is no need to sort the parameter domains. */
1094 if (!level)
1095 return loop;
1097 /* We will need to know how many loops are in the list. */
1098 nb_loops = cloog_loop_count(loop);
1100 /* If there is only one loop, it's the end. */
1101 if (nb_loops == 1)
1102 return(loop) ;
1104 /* We have to allocate memory for some useful components:
1105 * - loop_array: the loop array,
1106 * - doms: the array of domains to sort,
1107 * - permut: will give us a possible sort (maybe not the only one).
1109 loop_array = (CloogLoop **)malloc(nb_loops*sizeof(CloogLoop *)) ;
1110 doms = (CloogDomain **)malloc(nb_loops*sizeof(CloogDomain *));
1111 permut = (int *)malloc(nb_loops*sizeof(int)) ;
1113 /* We fill up the loop and domain arrays. */
1114 for (i=0;i<nb_loops;i++,loop=loop->next)
1115 { loop_array[i] = loop ;
1116 doms[i] = loop_array[i]->domain;
1119 /* cloog_domain_sort will fill up permut. */
1120 cloog_domain_sort(doms, nb_loops, level, permut);
1122 /* With permut and loop_array we build the sorted list. */
1123 res = NULL ;
1124 for (i=0;i<nb_loops;i++)
1125 { /* To avoid pointer looping... loop_add will rebuild the list. */
1126 loop_array[permut[i]-1]->next = NULL ;
1127 cloog_loop_add(&res,&now,loop_array[permut[i]-1]) ;
1130 free(permut) ;
1131 free(doms);
1132 free(loop_array) ;
1134 return res;
1139 * cloog_loop_nest function:
1140 * This function changes the loop list in such a way that we have no more than
1141 * one dimension added by level. It returns an equivalent loop list with
1142 * this property.
1143 * - October 29th 2001: first version.
1144 * - July 3rd->11th 2003: memory leaks hunt and correction.
1145 * - June 22nd 2005: Adaptation for GMP.
1146 * - November 21th 2005: (debug) now OK when cloog_loop_restrict returns NULL.
1148 CloogLoop *cloog_loop_nest(CloogLoop *loop, CloogDomain *context, int level)
1149 { int l ;
1150 CloogLoop * p, * temp, * res, * now, * next ;
1151 CloogDomain * new_domain ;
1153 loop = cloog_loop_disjoint(loop);
1155 res = NULL ;
1156 /* Each domain is changed by its intersection with the context. */
1157 while (loop != NULL)
1158 { p = cloog_loop_restrict(loop, context);
1159 next = loop->next ;
1161 if (p != NULL)
1162 { cloog_loop_free_parts(loop,1,0,0,0) ;
1164 temp = cloog_loop_alloc(p->state, p->domain, 0, NULL,
1165 p->block, p->inner, NULL);
1167 /* If the intersection dimension is too big, we make projections smaller
1168 * and smaller, and each projection includes the preceding projection
1169 * (thus, in the target list, dimensions are added one by one).
1171 if (cloog_domain_dimension(p->domain) >= level)
1172 for (l = cloog_domain_dimension(p->domain); l >= level; l--) {
1173 new_domain = cloog_domain_project(p->domain, l);
1174 temp = cloog_loop_alloc(p->state, new_domain, 0, NULL,
1175 NULL, temp, NULL);
1178 /* p is no more useful (but its content yes !). */
1179 cloog_loop_free_parts(p,0,0,0,0) ;
1181 cloog_loop_add(&res,&now,temp) ;
1183 else
1184 cloog_loop_free_parts(loop,1,1,1,0) ;
1186 loop = next ;
1189 return(res) ;
1193 /* Check if the domains of the inner loops impose a stride constraint
1194 * on the given level.
1195 * The core of the search is implemented in cloog_domain_list_stride.
1196 * Here, we simply construct a list of domains to pass to this function
1197 * and if a stride is found, we adjust the lower bounds by calling
1198 * cloog_domain_stride_lower_bound.
1200 static int cloog_loop_variable_offset_stride(CloogLoop *loop, int level)
1202 CloogDomainList *list = NULL;
1203 CloogLoop *inner;
1204 CloogStride *stride;
1206 for (inner = loop->inner; inner; inner = inner->next) {
1207 CloogDomainList *entry = ALLOC(CloogDomainList);
1208 entry->domain = cloog_domain_copy(inner->domain);
1209 entry->next = list;
1210 list = entry;
1213 stride = cloog_domain_list_stride(list, level);
1215 cloog_domain_list_free(list);
1217 if (!stride)
1218 return 0;
1220 loop->stride = stride;
1221 loop->domain = cloog_domain_stride_lower_bound(loop->domain, level, stride);
1223 return 1;
1228 * cloog_loop_stride function:
1229 * This function will find the stride of a loop for the iterator at the column
1230 * number 'level' in the constraint matrix. It will update the lower bound of
1231 * the iterator accordingly. Basically, the function will try to find in the
1232 * inner loops a common condition on this iterator for the inner loop iterators
1233 * to be integral. For instance, let us consider a loop with the iterator i,
1234 * the iteration domain -4<=i<=n, and its two inner loops with the iterator j.
1235 * The first inner loop has the constraint 3j=i, and the second one has the
1236 * constraint 6j=i. Then the common constraint on i for j to be integral is
1237 * i%3=0, the stride for i is 3. Lastly, we have to find the new lower bound
1238 * for i: the first value satisfying the common constraint: -3. At the end, the
1239 * iteration domain for i is -3<=i<=n and the stride for i is 3.
1241 * The algorithm implemented in this function only allows for strides
1242 * on loops with a lower bound that has a constant remainder on division
1243 * by the stride. Before initiating this procedure, we first check
1244 * if we can find a stride with a lower bound with a variable offset in
1245 * cloog_loop_variable_offset_stride.
1247 * - loop is the loop including the iteration domain of the considered iterator,
1248 * - level is the column number of the iterator in the matrix of contraints.
1250 * - June 29th 2003: first version (work in progress since June 26th 2003).
1251 * - July 14th 2003: simpler version.
1252 * - June 22nd 2005: Adaptation for GMP (from S. Verdoolaege's 0.12.1 version).
1254 void cloog_loop_stride(CloogLoop * loop, int level)
1255 { int first_search ;
1256 cloog_int_t stride, ref_offset, offset, potential;
1257 CloogLoop * inner ;
1259 if (!cloog_domain_can_stride(loop->domain, level))
1260 return;
1262 if (cloog_loop_variable_offset_stride(loop, level))
1263 return;
1265 cloog_int_init(stride);
1266 cloog_int_init(ref_offset);
1267 cloog_int_init(offset);
1268 cloog_int_init(potential);
1270 cloog_int_set_si(ref_offset, 0);
1271 cloog_int_set_si(offset, 0);
1273 /* Default stride. */
1274 cloog_int_set_si(stride, 1);
1275 first_search = 1 ;
1276 inner = loop->inner ;
1278 while (inner != NULL)
1279 { /* If the minimun stride has not been found yet, find the stride. */
1280 if ((first_search) || (!cloog_int_is_one(stride)))
1282 cloog_domain_stride(inner->domain, level, &potential, &offset);
1283 if (!cloog_int_is_one(potential) && (!first_search))
1284 { /* Offsets must be the same for common stride. */
1285 cloog_int_gcd(stride, potential, stride);
1286 if (!cloog_int_is_zero(stride)) {
1287 cloog_int_fdiv_r(offset, offset, stride);
1288 cloog_int_fdiv_r(ref_offset, ref_offset, stride);
1290 if (cloog_int_ne(offset,ref_offset))
1291 cloog_int_set_si(stride, 1);
1293 else {
1294 cloog_int_set(stride, potential);
1295 cloog_int_set(ref_offset, offset);
1298 first_search = 0 ;
1301 inner = inner->next ;
1304 if (cloog_int_is_zero(stride))
1305 cloog_int_set_si(stride, 1);
1307 /* Update the values if necessary. */
1308 if (!cloog_int_is_one(stride))
1309 { /* Update the stride value. */
1310 if (!cloog_int_is_zero(offset))
1311 cloog_int_sub(offset, stride, offset);
1312 loop->stride = cloog_stride_alloc(stride, offset);
1313 loop->domain = cloog_domain_stride_lower_bound(loop->domain, level,
1314 loop->stride);
1317 cloog_int_clear(stride);
1318 cloog_int_clear(ref_offset);
1319 cloog_int_clear(offset);
1320 cloog_int_clear(potential);
1324 void cloog_loop_otl(CloogLoop *loop, int level)
1326 if (cloog_domain_is_otl(loop->domain, level))
1327 loop->otl = 1;
1332 * cloog_loop_stop function:
1333 * This function implements the 'stop' option : each domain of each loop
1334 * in the list 'loop' is replaced by 'context'. 'context' should be the
1335 * domain of the outer loop. By using this method, there are no more dimensions
1336 * to scan and the simplification step will automaticaly remove the domains
1337 * since they are the same as the corresponding contexts. The effect of this
1338 * function is to stop the code generation at the level this function is called,
1339 * the resulting code do not consider the next dimensions.
1340 * - January 11th 2005: first version.
1342 CloogLoop * cloog_loop_stop(CloogLoop * loop, CloogDomain * context)
1343 { if (loop == NULL)
1344 return NULL ;
1345 else
1346 { cloog_domain_free(loop->domain) ;
1347 loop->domain = cloog_domain_copy(context) ;
1348 loop->next = cloog_loop_stop(loop->next, context) ;
1351 return loop ;
1355 static int level_is_constant(int level, int scalar, int *scaldims, int nb_scattdims)
1357 return level && (level+scalar <= nb_scattdims) && (scaldims[level+scalar-1]);
1362 * Compare the constant dimensions of loops 'l1' and 'l2' starting at 'scalar'
1363 * and return -1 if the vector of constant dimensions of 'l1' is smaller
1364 * than that of 'l2', 0 if they are the same and +1 if that of 'l1' is
1365 * greater than that of 'l2'.
1366 * This function should be called on the innermost loop (the loop
1367 * containing a block).
1368 * \param l1 Loop to be compared with l2.
1369 * \param l2 Loop to be compared with l1.
1370 * \param level Current non-scalar dimension.
1371 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1372 * \param nb_scattdims Size of the scaldims array.
1373 * \param scalar Current scalar dimension.
1374 * \return -1 if (l1 < l2), 0 if (l1 == l2) and +1 if (l1 > l2)
1376 int cloog_loop_constant_cmp(CloogLoop *l1, CloogLoop *l2, int level,
1377 int *scaldims, int nb_scattdims, int scalar)
1379 CloogBlock *b1, *b2;
1380 b1 = l1->block;
1381 b2 = l2->block;
1382 while (level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1383 int cmp = cloog_int_cmp(b1->scaldims[scalar], b2->scaldims[scalar]);
1384 if (cmp)
1385 return cmp;
1386 scalar++;
1388 return 0;
1393 * cloog_loop_scalar_gt function:
1394 * This function returns 1 if loop 'l1' is greater than loop 'l2' for the
1395 * scalar dimension vector that begins at dimension 'scalar', 0 otherwise. What
1396 * we want to know is whether a loop is scheduled before another one or not.
1397 * This function solves the problem when the considered dimension for scheduling
1398 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1399 * this function will reason about the vector of scalar dimension that begins
1400 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1401 * \param l1 Loop to be compared with l2.
1402 * \param l2 Loop to be compared with l1.
1403 * \param level Current non-scalar dimension.
1404 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1405 * \param nb_scattdims Size of the scaldims array.
1406 * \param scalar Current scalar dimension.
1407 * \return 1 if (l1 > l2), 0 otherwise.
1409 * - September 9th 2005: first version.
1410 * - October 15nd 2007: now "greater than" instead of "greater or equal".
1412 int cloog_loop_scalar_gt(l1, l2, level, scaldims, nb_scattdims, scalar)
1413 CloogLoop * l1, * l2 ;
1414 int level, * scaldims, nb_scattdims, scalar ;
1416 return cloog_loop_constant_cmp(l1, l2, level, scaldims, nb_scattdims, scalar) > 0;
1421 * cloog_loop_scalar_eq function:
1422 * This function returns 1 if loop 'l1' is equal to loop 'l2' for the scalar
1423 * dimension vector that begins at dimension 'scalar', 0 otherwise. What we want
1424 * to know is whether two loops are scheduled for the same time or not.
1425 * This function solves the problem when the considered dimension for scheduling
1426 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1427 * this function will reason about the vector of scalar dimension that begins
1428 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1429 * - l1 and l2 are the loops to compare,
1430 * - level is the current non-scalar dimension,
1431 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1432 * - nb_scattdims is the size of the scaldims array,
1433 * - scalar is the current scalar dimension.
1435 * - September 9th 2005 : first version.
1437 int cloog_loop_scalar_eq(l1, l2, level, scaldims, nb_scattdims, scalar)
1438 CloogLoop * l1, * l2 ;
1439 int level, * scaldims, nb_scattdims, scalar ;
1441 return cloog_loop_constant_cmp(l1, l2, level, scaldims, nb_scattdims, scalar) == 0;
1446 * cloog_loop_scalar_sort function:
1447 * This function sorts a linked list of loops (loop) with respect to the
1448 * scalar dimension vector that begins at dimension 'scalar'. Since there may
1449 * be a succession of scalar dimensions, this function will reason about the
1450 * vector of scalar dimension that begins at dimension 'level+scalar' and
1451 * finish to the first non-scalar dimension.
1452 * \param loop Loop list to sort.
1453 * \param level Current non-scalar dimension.
1454 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1455 * \param nb_scattdims Size of the scaldims array.
1456 * \param scalar Current scalar dimension.
1457 * \return A pointer to the sorted list.
1459 * - July 2nd 2005: first developments.
1460 * - September 2nd 2005: first version.
1461 * - October 15nd 2007: complete rewrite to remove bugs, now a bubble sort.
1463 CloogLoop * cloog_loop_scalar_sort(loop, level, scaldims, nb_scattdims, scalar)
1464 CloogLoop * loop ;
1465 int level, * scaldims, nb_scattdims, scalar ;
1466 { int ok ;
1467 CloogLoop **current;
1469 do {
1470 ok = 1;
1471 for (current = &loop; (*current)->next; current = &(*current)->next) {
1472 CloogLoop *next = (*current)->next;
1473 if (cloog_loop_scalar_gt(*current,next,level,scaldims,nb_scattdims,scalar)) {
1474 ok = 0;
1475 (*current)->next = next->next;
1476 next->next = *current;
1477 *current = next;
1480 } while (!ok);
1482 return loop ;
1487 * cloog_loop_generate_backtrack function:
1488 * adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1489 * backtrack of the Quillere et al. algorithm (see the Quillere paper).
1490 * It eliminates unused iterations of the current level for the new one. See the
1491 * example called linearity-1-1 example with and without this part for an idea.
1492 * - October 26th 2001: first version in cloog_loop_generate_general.
1493 * - July 31th 2002: (debug) no more parasite loops (REALLY hard !).
1494 * - October 30th 2005: extraction from cloog_loop_generate_general.
1496 CloogLoop *cloog_loop_generate_backtrack(CloogLoop *loop,
1497 int level, CloogOptions *options)
1499 CloogDomain * domain ;
1500 CloogLoop * now, * now2, * next, * next2, * end, * temp, * l, * inner,
1501 * new_loop ;
1503 temp = loop ;
1504 loop = NULL ;
1506 while (temp != NULL)
1507 { l = NULL ;
1508 inner = temp->inner ;
1510 while (inner != NULL)
1511 { next = inner->next ;
1512 /* This 'if' and its first part is the debug of july 31th 2002. */
1513 if (inner->block != NULL) {
1514 end = cloog_loop_alloc(temp->state, inner->domain, 0, NULL,
1515 inner->block, NULL, NULL);
1516 domain = cloog_domain_copy(temp->domain) ;
1517 new_loop = cloog_loop_alloc(temp->state, domain, 0, NULL,
1518 NULL, end, NULL);
1520 else
1521 new_loop = cloog_loop_project(inner, level);
1523 cloog_loop_free_parts(inner,0,0,0,0) ;
1524 cloog_loop_add(&l,&now2,new_loop) ;
1525 inner = next ;
1528 temp->inner = NULL ;
1530 if (l != NULL)
1531 { l = cloog_loop_separate(l) ;
1532 l = cloog_loop_sort(l, level);
1533 while (l != NULL) {
1534 l->stride = cloog_stride_copy(l->stride);
1535 cloog_loop_add(&loop,&now,l) ;
1536 l = l->next ;
1539 next2 = temp->next ;
1540 cloog_loop_free_parts(temp,1,0,0,0) ;
1541 temp = next2 ;
1544 return loop ;
1549 * Return 1 if we need to continue recursing to the specified level.
1551 int cloog_loop_more(CloogLoop *loop, int level, int scalar, int nb_scattdims)
1553 return level + scalar <= nb_scattdims ||
1554 cloog_domain_dimension(loop->domain) >= level;
1558 * Return 1 if the domains of all loops in the given linked list
1559 * have a fixed value at the given level.
1560 * In principle, there would be no need to check that the fixed value is
1561 * the same for each of these loops because this function is only
1562 * called on a component. However, not all backends perform a proper
1563 * decomposition into components.
1565 int cloog_loop_is_constant(CloogLoop *loop, int level)
1567 cloog_int_t c1, c2;
1568 int r = 1;
1570 cloog_int_init(c1);
1571 cloog_int_init(c2);
1573 if (!cloog_domain_lazy_isconstant(loop->domain, level - 1, &c1))
1574 r = 0;
1576 for (loop = loop->next; r && loop; loop = loop->next) {
1577 if (!cloog_domain_lazy_isconstant(loop->domain, level - 1, &c2))
1578 r = 0;
1579 else if (cloog_int_ne(c1, c2))
1580 r = 0;
1583 cloog_int_clear(c1);
1584 cloog_int_clear(c2);
1586 return r;
1590 * Assuming all domains in the given linked list of loop
1591 * have a fixed values at level, return a single loop with
1592 * a domain corresponding to this fixed value and with as
1593 * list of inner loops the concatenation of all inner loops
1594 * in the original list.
1596 CloogLoop *cloog_loop_constant(CloogLoop *loop, int level)
1598 CloogLoop *res, *inner, *tmp;
1599 CloogDomain *domain, *t;
1601 if (!loop)
1602 return loop;
1604 inner = loop->inner;
1605 domain = loop->domain;
1606 for (tmp = loop->next; tmp; tmp = tmp->next) {
1607 inner = cloog_loop_concat(inner, tmp->inner);
1608 domain = cloog_domain_union(domain, tmp->domain);
1611 domain = cloog_domain_simple_convex(t = domain);
1612 cloog_domain_free(t);
1614 res = cloog_loop_alloc(loop->state, domain, 0, NULL, NULL, inner, NULL);
1616 cloog_loop_free_parts(loop, 0, 0, 0, 1);
1618 return res;
1621 CloogLoop *cloog_loop_generate_restricted_or_stop(CloogLoop *loop,
1622 CloogDomain *context,
1623 int level, int scalar, int *scaldims, int nb_scattdims,
1624 CloogOptions *options);
1627 * cloog_loop_generate_general function:
1628 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1629 * Quillere algorithm for polyhedron scanning from step 3 to 5.
1630 * (see the Quillere paper).
1631 * - loop is the loop for which we have to generate a scanning code,
1632 * - level is the current non-scalar dimension,
1633 * - scalar is the current scalar dimension,
1634 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1635 * - nb_scattdims is the size of the scaldims array,
1636 * - options are the general code generation options.
1638 * - October 26th 2001: first version.
1639 * - July 3rd->11th 2003: memory leaks hunt and correction.
1640 * - June 22nd 2005: Adaptation for GMP.
1641 * - September 2nd 2005: The function have been cutted out in two pieces:
1642 * cloog_loop_generate and this one, in order to handle
1643 * the scalar dimension case more efficiently with
1644 * cloog_loop_generate_scalar.
1645 * - November 15th 2005: (debug) the result of the cloog_loop_generate call may
1646 * be a list of polyhedra (especially if stop option is
1647 * used): cloog_loop_add_list instead of cloog_loop_add.
1649 CloogLoop *cloog_loop_generate_general(CloogLoop *loop,
1650 int level, int scalar, int *scaldims, int nb_scattdims,
1651 CloogOptions *options)
1653 CloogLoop * res, * now, * temp, * l, * new_loop, * inner, * now2, * end,
1654 * next, * into ;
1655 CloogDomain * domain ;
1656 int separate = 0;
1658 /* 3. Separate all projections into disjoint polyhedra. */
1659 if (level > 0 && cloog_loop_is_constant(loop, level))
1660 res = cloog_loop_constant(loop, level);
1661 else if ((options->f > level+scalar) || (options->f < 0))
1662 res = cloog_loop_merge(loop, level, options);
1663 else {
1664 res = cloog_loop_separate(loop);
1665 separate = 1;
1668 /* 3b. -correction- sort the loops to determine their textual order. */
1669 res = cloog_loop_sort(res, level);
1671 res = cloog_loop_restrict_inner(res);
1673 if (separate)
1674 res = cloog_loop_specialize(res, level, scalar, scaldims, nb_scattdims);
1676 /* 4. Recurse for each loop with the current domain as context. */
1677 temp = res ;
1678 res = NULL ;
1679 if (!level || (level+scalar < options->l) || (options->l < 0))
1680 while(temp != NULL)
1681 { if (level && options->strides)
1682 cloog_loop_stride(temp, level);
1683 if (level && options->otl)
1684 cloog_loop_otl(temp, level);
1685 inner = temp->inner ;
1686 domain = temp->domain ;
1687 into = NULL ;
1688 while (inner != NULL)
1689 { /* 4b. -ced- recurse for each sub-list of non terminal loops. */
1690 if (cloog_loop_more(inner, level + 1, scalar, nb_scattdims)) {
1691 end = inner;
1692 while ((end->next != NULL) &&
1693 cloog_loop_more(end->next, level + 1, scalar, nb_scattdims))
1694 end = end->next ;
1696 next = end->next ;
1697 end->next = NULL ;
1699 l = cloog_loop_generate_restricted_or_stop(inner, domain,
1700 level + 1, scalar, scaldims, nb_scattdims, options);
1702 if (l != NULL)
1703 cloog_loop_add_list(&into,&now,l) ;
1705 inner = next ;
1707 else
1708 { cloog_loop_add(&into,&now,inner) ;
1709 inner = inner->next ;
1712 next = temp->next ;
1713 temp->next = NULL ;
1714 temp->inner = into ;
1715 cloog_loop_add(&res,&now2,temp) ;
1716 temp = next ;
1718 else
1719 while (temp != NULL)
1720 { next = temp->next ;
1721 l = cloog_loop_nest(temp->inner, temp->domain, level+1);
1722 new_loop = cloog_loop_alloc(temp->state, temp->domain, 0, NULL,
1723 NULL, l, NULL);
1724 temp->inner = NULL ;
1725 temp->next = NULL ;
1726 cloog_loop_free_parts(temp,0,0,0,0) ;
1727 cloog_loop_add(&res,&now,new_loop) ;
1728 temp = next ;
1731 if (options->strides)
1732 res = cloog_loop_propagate_lower_bound(res, level);
1734 /* 5. eliminate unused iterations of the current level for the new one. See
1735 * the example called linearity-1-1 example with and without this part
1736 * for an idea.
1738 if (options->backtrack && level &&
1739 ((level+scalar < options->l) || (options->l < 0)) &&
1740 ((options->f <= level+scalar) && !(options->f < 0)))
1741 res = cloog_loop_generate_backtrack(res, level, options);
1743 /* Pray for my new paper to be accepted somewhere since the following stuff
1744 * is really amazing :-) !
1745 * Far long later: The paper has been accepted to PACT 2004 :-))). But there
1746 * are still some bugs and I have no time to fix them. Thus now you have to
1747 * pray for me to get an academic position for that really amazing stuff :-) !
1748 * Later again: OK, I get my academic position, but still I have not enough
1749 * time to fix and clean this part... Pray again :-) !!!
1751 /* res = cloog_loop_unisolate(res,level) ;*/
1753 return(res) ;
1757 CloogLoop *cloog_loop_generate_restricted(CloogLoop *loop,
1758 int level, int scalar, int *scaldims, int nb_scattdims,
1759 CloogOptions *options);
1763 * cloog_loop_generate_scalar function:
1764 * This function applies the simplified code generation scheme in the trivial
1765 * case of scalar dimensions. When dealing with scalar dimensions, there is
1766 * no need of costly polyhedral operations for separation or sorting: sorting
1767 * is a question of comparing scalar vectors and separation amounts to consider
1768 * only loops with the same scalar vector for the next step of the code
1769 * generation process. This function achieves the separation/sorting process
1770 * for the vector of scalar dimension that begins at dimension 'level+scalar'
1771 * and finish to the first non-scalar dimension.
1772 * - loop is the loop for which we have to generate a scanning code,
1773 * - level is the current non-scalar dimension,
1774 * - scalar is the current scalar dimension,
1775 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1776 * - nb_scattdims is the size of the scaldims array,
1777 * - options are the general code generation options.
1779 * - September 2nd 2005: First version.
1781 CloogLoop *cloog_loop_generate_scalar(CloogLoop *loop,
1782 int level, int scalar, int *scaldims, int nb_scattdims,
1783 CloogOptions *options)
1784 { CloogLoop * res, * now, * temp, * l, * end, * next, * ref ;
1785 int scalar_new;
1787 /* We sort the loop list with respect to the current scalar vector. */
1788 res = cloog_loop_scalar_sort(loop,level,scaldims,nb_scattdims,scalar) ;
1790 scalar_new = scalar + scaldims[level + scalar - 1];
1792 temp = res ;
1793 res = NULL ;
1794 while (temp != NULL)
1795 { /* Then we will appy the general code generation process to each sub-list
1796 * of loops with the same scalar vector.
1798 end = temp ;
1799 ref = temp ;
1801 while((end->next != NULL) &&
1802 cloog_loop_more(end->next, level, scalar_new, nb_scattdims) &&
1803 cloog_loop_scalar_eq(ref,end->next,level,scaldims,nb_scattdims,scalar))
1804 end = end->next ;
1806 next = end->next ;
1807 end->next = NULL ;
1809 /* For the next dimension, scalar value is updated by adding the scalar
1810 * vector size, which is stored at scaldims[level+scalar-1].
1812 if (cloog_loop_more(temp, level, scalar_new, nb_scattdims)) {
1813 l = cloog_loop_generate_restricted(temp, level, scalar_new,
1814 scaldims, nb_scattdims, options);
1816 if (l != NULL)
1817 cloog_loop_add_list(&res, &now, l);
1818 } else
1819 cloog_loop_add(&res, &now, temp);
1821 temp = next ;
1824 return res ;
1828 /* Compare loop with the next loop based on their constant dimensions.
1829 * The result is < 0, == 0 or > 0 depending on whether the constant
1830 * dimensions of loop are lexicographically smaller, equal or greater
1831 * than those of loop->next.
1832 * If loop is the last in the list, then it is assumed to be smaller
1833 * than the "next" one.
1835 static int cloog_loop_next_scal_cmp(CloogLoop *loop)
1837 int i;
1838 int nb_scaldims;
1840 if (!loop->next)
1841 return -1;
1843 nb_scaldims = loop->block->nb_scaldims;
1844 if (loop->next->block->nb_scaldims < nb_scaldims)
1845 nb_scaldims = loop->next->block->nb_scaldims;
1847 for (i = 0; i < nb_scaldims; ++i) {
1848 int cmp = cloog_int_cmp(loop->block->scaldims[i],
1849 loop->next->block->scaldims[i]);
1850 if (cmp)
1851 return cmp;
1853 return loop->block->nb_scaldims - loop->next->block->nb_scaldims;
1857 /* Check whether the globally constant dimensions of a and b
1858 * have the same value for all globally constant dimensions
1859 * that are situated before any (locally) non-constant dimension.
1861 static int cloog_loop_equal_prefix(CloogLoop *a, CloogLoop *b,
1862 int *scaldims, int nb_scattdims)
1864 int i;
1865 int cst = 0;
1866 int dim = 0;
1868 for (i = 0; i < nb_scattdims; ++i) {
1869 if (!scaldims[i]) {
1870 dim++;
1871 continue;
1873 if (!cloog_int_eq(a->block->scaldims[cst], b->block->scaldims[cst]))
1874 break;
1875 cst++;
1877 for (i = i + 1; i < nb_scattdims; ++i) {
1878 if (scaldims[i])
1879 continue;
1880 if (!cloog_domain_lazy_isconstant(a->domain, dim, NULL))
1881 return 0;
1882 /* No need to check that dim is also constant in b and that the
1883 * constant values are equal. That will happen during the check
1884 * whether the two domains are equal.
1886 dim++;
1888 return 1;
1892 /* Try to block adjacent loops in the loop list "loop".
1893 * We only attempt blocking if the constant dimensions of the loops
1894 * in the least are (not necessarily strictly) increasing.
1895 * Then we look for a sublist such that the first (begin) has constant
1896 * dimensions strictly larger than the previous loop in the complete
1897 * list and such that the loop (end) after the last loop in the sublist
1898 * has constant dimensions strictly larger than the last loop in the sublist.
1899 * Furthermore, all loops in the sublist should have the same domain
1900 * (with globally constant dimensions removed) and the difference
1901 * (if any) in constant dimensions may only occur after all the
1902 * (locally) constant dimensions.
1903 * If we find such a sublist, then the blocks of all but the first
1904 * are merged into the block of the first.
1906 * Note that this function can only be called before the global
1907 * blocklist has been created because it may otherwise modify and destroy
1908 * elements on that list.
1910 CloogLoop *cloog_loop_block(CloogLoop *loop, int *scaldims, int nb_scattdims)
1912 CloogLoop *begin, *end, *l;
1913 int begin_after_previous;
1914 int end_after_previous;
1916 if (!loop->next)
1917 return loop;
1918 for (begin = loop; begin; begin = begin->next) {
1919 if (!begin->block || !begin->block->scaldims)
1920 return loop;
1921 if (cloog_loop_next_scal_cmp(begin) > 0)
1922 return loop;
1925 begin_after_previous = 1;
1926 for (begin = loop; begin; begin = begin->next) {
1927 if (!begin_after_previous) {
1928 begin_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1929 continue;
1932 end_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1933 for (end = begin->next; end; end = end->next) {
1934 if (!cloog_loop_equal_prefix(begin, end, scaldims, nb_scattdims))
1935 break;
1936 if (!cloog_domain_lazy_equal(begin->domain, end->domain))
1937 break;
1938 end_after_previous = cloog_loop_next_scal_cmp(end) < 0;
1940 if (end != begin->next && end_after_previous) {
1941 for (l = begin->next; l != end; l = begin->next) {
1942 cloog_block_merge(begin->block, l->block);
1943 begin->next = l->next;
1944 cloog_loop_free_parts(l, 1, 0, 1, 0);
1948 begin_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1951 return loop;
1956 * Check whether for any fixed iteration of the outer loops,
1957 * there is an iteration of loop1 that is lexicographically greater
1958 * than an iteration of loop2.
1959 * Return 1 if there exists (or may exist) such a pair.
1960 * Return 0 if all iterations of loop1 are lexicographically smaller
1961 * than the iterations of loop2.
1962 * If no iteration is lexicographically greater, but if there are
1963 * iterations that are equal to iterations of loop2, then return "def".
1964 * This is useful for ensuring that such statements are not reordered.
1965 * Some users, including the test_run target in test, expect
1966 * the statements at a given point to be run in the original order.
1967 * Passing the value "0" for "def" would allow such statements to be reordered
1968 * and would allow for the detection of more components.
1970 int cloog_loop_follows(CloogLoop *loop1, CloogLoop *loop2,
1971 int level, int scalar, int *scaldims, int nb_scattdims, int def)
1973 int dim1, dim2;
1975 dim1 = cloog_domain_dimension(loop1->domain);
1976 dim2 = cloog_domain_dimension(loop2->domain);
1977 while ((level <= dim1 && level <= dim2) ||
1978 level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1979 if (level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1980 int cmp = cloog_loop_constant_cmp(loop1, loop2, level, scaldims,
1981 nb_scattdims, scalar);
1982 if (cmp > 0)
1983 return 1;
1984 if (cmp < 0)
1985 return 0;
1986 scalar += scaldims[level + scalar - 1];
1987 } else {
1988 int follows = cloog_domain_follows(loop1->domain, loop2->domain,
1989 level);
1990 if (follows > 0)
1991 return 1;
1992 if (follows < 0)
1993 return 0;
1994 level++;
1998 return def;
2002 /* Structure for representing the nodes in the graph being traversed
2003 * using Tarjan's algorithm.
2004 * index represents the order in which nodes are visited.
2005 * min_index is the index of the root of a (sub)component.
2006 * on_stack indicates whether the node is currently on the stack.
2008 struct cloog_loop_sort_node {
2009 int index;
2010 int min_index;
2011 int on_stack;
2013 /* Structure for representing the graph being traversed
2014 * using Tarjan's algorithm.
2015 * len is the number of nodes
2016 * node is an array of nodes
2017 * stack contains the nodes on the path from the root to the current node
2018 * sp is the stack pointer
2019 * index is the index of the last node visited
2020 * order contains the elements of the components separated by -1
2021 * op represents the current position in order
2023 struct cloog_loop_sort {
2024 int len;
2025 struct cloog_loop_sort_node *node;
2026 int *stack;
2027 int sp;
2028 int index;
2029 int *order;
2030 int op;
2033 /* Allocate and initialize cloog_loop_sort structure.
2035 static struct cloog_loop_sort *cloog_loop_sort_alloc(int len)
2037 struct cloog_loop_sort *s;
2038 int i;
2040 s = (struct cloog_loop_sort *)malloc(sizeof(struct cloog_loop_sort));
2041 assert(s);
2042 s->len = len;
2043 s->node = (struct cloog_loop_sort_node *)
2044 malloc(len * sizeof(struct cloog_loop_sort_node));
2045 assert(s->node);
2046 for (i = 0; i < len; ++i)
2047 s->node[i].index = -1;
2048 s->stack = (int *)malloc(len * sizeof(int));
2049 assert(s->stack);
2050 s->order = (int *)malloc(2 * len * sizeof(int));
2051 assert(s->order);
2053 s->sp = 0;
2054 s->index = 0;
2055 s->op = 0;
2057 return s;
2060 /* Free cloog_loop_sort structure.
2062 static void cloog_loop_sort_free(struct cloog_loop_sort *s)
2064 free(s->node);
2065 free(s->stack);
2066 free(s->order);
2067 free(s);
2071 /* Check whether for any fixed iteration of the outer loops,
2072 * there is an iteration of loop1 that is lexicographically greater
2073 * than an iteration of loop2, where the iteration domains are
2074 * available in the inner loops of the arguments.
2076 * By using this functions to detect components, we ensure that
2077 * two CloogLoops appear in the same component if some iterations of
2078 * each loop should be executed before some iterations of the other loop.
2079 * Since we also want two CloogLoops that have exactly the same
2080 * iteration domain at the current level to be placed in the same component,
2081 * we first check if these domains are indeed the same.
2083 static int inner_loop_follows(CloogLoop *loop1, CloogLoop *loop2,
2084 int level, int scalar, int *scaldims, int nb_scattdims, int def)
2086 int f;
2088 f = cloog_domain_lazy_equal(loop1->domain, loop2->domain);
2089 if (!f)
2090 f = cloog_loop_follows(loop1->inner, loop2->inner,
2091 level, scalar, scaldims, nb_scattdims, def);
2093 return f;
2097 /* Perform Tarjan's algorithm for computing the strongly connected components
2098 * in the graph with the individual CloogLoops as vertices.
2099 * Two CloopLoops appear in the same component if they both (indirectly)
2100 * "follow" each other, where the following relation is determined
2101 * by the follows function.
2103 static void cloog_loop_components_tarjan(struct cloog_loop_sort *s,
2104 CloogLoop **loop_array, int i, int level, int scalar, int *scaldims,
2105 int nb_scattdims,
2106 int (*follows)(CloogLoop *loop1, CloogLoop *loop2,
2107 int level, int scalar, int *scaldims, int nb_scattdims, int def))
2109 int j;
2111 s->node[i].index = s->index;
2112 s->node[i].min_index = s->index;
2113 s->node[i].on_stack = 1;
2114 s->index++;
2115 s->stack[s->sp++] = i;
2117 for (j = s->len - 1; j >= 0; --j) {
2118 int f;
2120 if (j == i)
2121 continue;
2122 if (s->node[j].index >= 0 &&
2123 (!s->node[j].on_stack ||
2124 s->node[j].index > s->node[i].min_index))
2125 continue;
2127 f = follows(loop_array[i], loop_array[j],
2128 level, scalar, scaldims, nb_scattdims, i > j);
2129 if (!f)
2130 continue;
2132 if (s->node[j].index < 0) {
2133 cloog_loop_components_tarjan(s, loop_array, j, level, scalar,
2134 scaldims, nb_scattdims, follows);
2135 if (s->node[j].min_index < s->node[i].min_index)
2136 s->node[i].min_index = s->node[j].min_index;
2137 } else if (s->node[j].index < s->node[i].min_index)
2138 s->node[i].min_index = s->node[j].index;
2141 if (s->node[i].index != s->node[i].min_index)
2142 return;
2144 do {
2145 j = s->stack[--s->sp];
2146 s->node[j].on_stack = 0;
2147 s->order[s->op++] = j;
2148 } while (j != i);
2149 s->order[s->op++] = -1;
2153 static int qsort_index_cmp(const void *p1, const void *p2)
2155 return *(int *)p1 - *(int *)p2;
2158 /* Sort the elements of the component starting at list.
2159 * The list is terminated by a -1.
2161 static void sort_component(int *list)
2163 int len;
2165 for (len = 0; list[len] != -1; ++len)
2168 qsort(list, len, sizeof(int), qsort_index_cmp);
2171 /* Given an array of indices "list" into the "loop_array" array,
2172 * terminated by -1, construct a linked list of the corresponding
2173 * entries and put the result in *res.
2174 * The value returned is the number of CloogLoops in the (linked) list
2176 static int extract_component(CloogLoop **loop_array, int *list, CloogLoop **res)
2178 int i = 0;
2180 sort_component(list);
2181 while (list[i] != -1) {
2182 *res = loop_array[list[i]];
2183 res = &(*res)->next;
2184 ++i;
2186 *res = NULL;
2188 return i;
2193 * Call cloog_loop_generate_scalar or cloog_loop_generate_general
2194 * on each of the strongly connected components in the list of CloogLoops
2195 * pointed to by "loop".
2197 * We use Tarjan's algorithm to find the strongly connected components.
2198 * Note that this algorithm also topologically sorts the components.
2200 * The components are treated separately to avoid spurious separations.
2201 * The concatentation of the results may contain successive loops
2202 * with the same bounds, so we try to combine such loops.
2204 CloogLoop *cloog_loop_generate_components(CloogLoop *loop,
2205 int level, int scalar, int *scaldims, int nb_scattdims,
2206 CloogOptions *options)
2208 int i, nb_loops;
2209 CloogLoop *tmp;
2210 CloogLoop *res, **res_next;
2211 CloogLoop **loop_array;
2212 struct cloog_loop_sort *s;
2214 if (level == 0 || !loop->next)
2215 return cloog_loop_generate_general(loop, level, scalar,
2216 scaldims, nb_scattdims, options);
2218 nb_loops = cloog_loop_count(loop);
2220 loop_array = (CloogLoop **)malloc(nb_loops * sizeof(CloogLoop *));
2221 assert(loop_array);
2223 for (i = 0, tmp = loop; i < nb_loops; i++, tmp = tmp->next)
2224 loop_array[i] = tmp;
2226 s = cloog_loop_sort_alloc(nb_loops);
2227 for (i = nb_loops - 1; i >= 0; --i) {
2228 if (s->node[i].index >= 0)
2229 continue;
2230 cloog_loop_components_tarjan(s, loop_array, i, level, scalar, scaldims,
2231 nb_scattdims, &inner_loop_follows);
2234 i = 0;
2235 res = NULL;
2236 res_next = &res;
2237 while (nb_loops) {
2238 int n = extract_component(loop_array, &s->order[i], &tmp);
2239 i += n + 1;
2240 nb_loops -= n;
2241 *res_next = cloog_loop_generate_general(tmp, level, scalar,
2242 scaldims, nb_scattdims, options);
2243 while (*res_next)
2244 res_next = &(*res_next)->next;
2247 cloog_loop_sort_free(s);
2249 free(loop_array);
2251 res = cloog_loop_combine(res);
2253 return res;
2257 /* For each loop in the list "loop", decompose the list of
2258 * inner loops into strongly connected components and put
2259 * the components into separate loops at the top level.
2261 CloogLoop *cloog_loop_decompose_inner(CloogLoop *loop,
2262 int level, int scalar, int *scaldims, int nb_scattdims)
2264 CloogLoop *l, *tmp;
2265 CloogLoop **loop_array;
2266 int i, n_loops, max_loops = 0;
2267 struct cloog_loop_sort *s;
2269 for (l = loop; l; l = l->next) {
2270 n_loops = cloog_loop_count(l->inner);
2271 if (max_loops < n_loops)
2272 max_loops = n_loops;
2275 if (max_loops <= 1)
2276 return loop;
2278 loop_array = (CloogLoop **)malloc(max_loops * sizeof(CloogLoop *));
2279 assert(loop_array);
2281 for (l = loop; l; l = l->next) {
2282 int n;
2284 for (i = 0, tmp = l->inner; tmp; i++, tmp = tmp->next)
2285 loop_array[i] = tmp;
2286 n_loops = i;
2287 if (n_loops <= 1)
2288 continue;
2290 s = cloog_loop_sort_alloc(n_loops);
2291 for (i = n_loops - 1; i >= 0; --i) {
2292 if (s->node[i].index >= 0)
2293 continue;
2294 cloog_loop_components_tarjan(s, loop_array, i, level, scalar,
2295 scaldims, nb_scattdims, &cloog_loop_follows);
2298 n = extract_component(loop_array, s->order, &l->inner);
2299 n_loops -= n;
2300 i = n + 1;
2301 while (n_loops) {
2302 CloogLoop *inner;
2304 n = extract_component(loop_array, &s->order[i], &inner);
2305 n_loops -= n;
2306 i += n + 1;
2307 tmp = cloog_loop_alloc(l->state, cloog_domain_copy(l->domain),
2308 l->otl, l->stride, l->block, inner, l->next);
2309 l->next = tmp;
2310 l = tmp;
2313 cloog_loop_sort_free(s);
2316 free(loop_array);
2318 return loop;
2322 CloogLoop *cloog_loop_generate_restricted(CloogLoop *loop,
2323 int level, int scalar, int *scaldims, int nb_scattdims,
2324 CloogOptions *options)
2326 /* To save both time and memory, we switch here depending on whether the
2327 * current dimension is scalar (simplified processing) or not (general
2328 * processing).
2330 if (level_is_constant(level, scalar, scaldims, nb_scattdims))
2331 return cloog_loop_generate_scalar(loop, level, scalar,
2332 scaldims, nb_scattdims, options);
2334 * 2. Compute the projection of each polyhedron onto the outermost
2335 * loop variable and the parameters.
2337 loop = cloog_loop_project_all(loop, level);
2339 return cloog_loop_generate_components(loop, level, scalar, scaldims,
2340 nb_scattdims, options);
2344 CloogLoop *cloog_loop_generate_restricted_or_stop(CloogLoop *loop,
2345 CloogDomain *context,
2346 int level, int scalar, int *scaldims, int nb_scattdims,
2347 CloogOptions *options)
2349 /* If the user asked to stop code generation at this level, let's stop. */
2350 if ((options->stop >= 0) && (level+scalar >= options->stop+1))
2351 return cloog_loop_stop(loop,context) ;
2353 return cloog_loop_generate_restricted(loop, level, scalar, scaldims,
2354 nb_scattdims, options);
2359 * cloog_loop_generate function:
2360 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
2361 * Quillere algorithm for polyhedron scanning from step 1 to 2.
2362 * (see the Quillere paper).
2363 * - loop is the loop for which we have to generate a scanning code,
2364 * - context is the context of the current loop (constraints on parameter and/or
2365 * on outer loop counters),
2366 * - level is the current non-scalar dimension,
2367 * - scalar is the current scalar dimension,
2368 * - scaldims is the boolean array saying whether a dimension is scalar or not,
2369 * - nb_scattdims is the size of the scaldims array,
2370 * - options are the general code generation options.
2372 * - October 26th 2001: first version.
2373 * - July 3rd->11th 2003: memory leaks hunt and correction.
2374 * - June 15th 2005: a memory leak fixed (loop was not entirely freed when
2375 * the result of cloog_loop_restrict was NULL).
2376 * - June 22nd 2005: Adaptation for GMP.
2377 * - September 2nd 2005: The function have been cutted out in two pieces:
2378 * cloog_loop_generate and this one, in order to handle
2379 * the scalar dimension case more efficiently with
2380 * cloog_loop_generate_scalar.
2381 * - November 15th 2005: (debug) Condition for stop option no more take care of
2382 * further scalar dimensions.
2384 CloogLoop *cloog_loop_generate(CloogLoop *loop, CloogDomain *context,
2385 int level, int scalar, int *scaldims, int nb_scattdims,
2386 CloogOptions *options)
2388 /* 1. Replace each polyhedron by its intersection with the context.
2390 loop = cloog_loop_restrict_all(loop, context);
2391 if (!loop)
2392 return NULL;
2394 return cloog_loop_generate_restricted_or_stop(loop, context,
2395 level, scalar, scaldims, nb_scattdims, options);
2400 * Internal function for simplifying a single loop in a list of loops.
2401 * See cloog_loop_simplify.
2403 static CloogLoop *loop_simplify(CloogLoop *loop, CloogDomain *context,
2404 int level, int nb_scattdims, CloogOptions *options)
2406 int domain_dim;
2407 CloogBlock * new_block ;
2408 CloogLoop *simplified, *inner;
2409 CloogDomain * domain, * simp, * inter, * extended_context ;
2411 if (!cloog_domain_isconvex(loop->domain))
2412 loop->domain = cloog_domain_simplify_union(loop->domain);
2414 domain = loop->domain ;
2416 domain_dim = cloog_domain_dimension(domain);
2417 extended_context = cloog_domain_extend(context, domain_dim);
2418 inter = cloog_domain_intersection(domain,extended_context) ;
2419 simp = cloog_domain_simplify(domain, extended_context);
2420 cloog_domain_free(extended_context) ;
2422 /* If the constraint system is never true, go to the next one. */
2423 if (cloog_domain_never_integral(simp)) {
2424 cloog_loop_free(loop->inner);
2425 cloog_domain_free(inter);
2426 cloog_domain_free(simp);
2427 return NULL;
2430 inner = cloog_loop_simplify(loop->inner, inter, level+1, nb_scattdims,
2431 options);
2433 if ((inner == NULL) && (loop->block == NULL)) {
2434 cloog_domain_free(inter);
2435 cloog_domain_free(simp);
2436 return NULL;
2439 new_block = cloog_block_copy(loop->block) ;
2441 simplified = cloog_loop_alloc(loop->state, simp, loop->otl, loop->stride,
2442 new_block, inner, NULL);
2444 /* Only save the domains, if it involves only scattering dimensions. */
2445 if (options->save_domains && domain_dim <= nb_scattdims)
2446 simplified->unsimplified = inter;
2447 else
2448 cloog_domain_free(inter);
2450 return(simplified) ;
2455 * cloog_loop_simplify function:
2456 * This function implements the part 6. of the Quillere algorithm, it
2457 * recursively simplifies each loop in the context of the preceding loop domain.
2458 * It returns a pointer to the simplified loop list.
2459 * The cloog_domain_simplify (DomainSimplify) behaviour is really bad with
2460 * polyhedra union and some really awful sidesteppings were written, I plan
2461 * to solve that...
2462 * - October 31th 2001: first version.
2463 * - July 3rd->11th 2003: memory leaks hunt and correction.
2464 * - April 16th 2005: a memory leak fixed (extended_context was not freed).
2465 * - June 15th 2005: a memory leak fixed (loop was not conveniently freed
2466 * when the constraint system is never true).
2467 * - October 27th 2005: - this function called before cloog_loop_fast_simplify
2468 * is now the official cloog_loop_simplify function in
2469 * replacement of a slower and more complex one (after
2470 * deep changes in the pretty printer).
2471 * - we use cloog_loop_disjoint to fix the problem when
2472 * simplifying gives a union of polyhedra (before, it
2473 * was under the responsibility of the pretty printer).
2475 CloogLoop *cloog_loop_simplify(CloogLoop *loop, CloogDomain *context, int level,
2476 int nb_scattdims, CloogOptions *options)
2478 CloogLoop *now;
2479 CloogLoop *res = NULL;
2480 CloogLoop **next = &res;
2482 for (now = loop; now; now = now->next) {
2483 *next = loop_simplify(now, context, level, nb_scattdims, options);
2485 now->inner = NULL; /* For loop integrity. */
2486 cloog_domain_free(now->domain);
2487 now->domain = NULL;
2489 if (*next)
2490 next = &(*next)->next;
2492 cloog_loop_free(loop);
2494 /* Examples like test/iftest2.cloog give unions of polyhedra after
2495 * simplifying, thus we have to make them disjoint. Another good reason to
2496 * put the simplifying step in the Quillere backtrack.
2498 res = cloog_loop_disjoint(res);
2500 return res;
2505 * cloog_loop_scatter function:
2506 * This function add the scattering (scheduling) informations in a loop.
2508 void cloog_loop_scatter(CloogLoop * loop, CloogScattering *scatt)
2510 loop->domain = cloog_domain_scatter(loop->domain, scatt);