source/loop.c: fix typo in comment
[cloog.git] / source / loop.c
blob926aad586f9db039ca1b0f5d9cdab9cb15d5d82c
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_block_free(loop->block) ;
212 if (loop->inner != NULL)
213 cloog_loop_free(loop->inner) ;
215 cloog_stride_free(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(loop->state);
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 cloog_stride_free(loop->stride);
250 free(loop) ;
251 if (next)
252 loop = follow ;
253 else
254 loop = NULL ;
259 /******************************************************************************
260 * Reading functions *
261 ******************************************************************************/
265 * Construct a CloogLoop structure from a given iteration domain
266 * and statement number.
268 CloogLoop *cloog_loop_from_domain(CloogState *state, CloogDomain *domain,
269 int number)
271 int nb_iterators;
272 CloogLoop * loop ;
273 CloogStatement * statement ;
275 /* Memory allocation and information reading for the first domain: */
276 loop = cloog_loop_malloc(state);
277 /* domain. */
278 loop->domain = domain;
279 if (loop->domain != NULL)
280 nb_iterators = cloog_domain_dimension(loop->domain);
281 else
282 nb_iterators = 0 ;
283 /* included statement block. */
284 statement = cloog_statement_alloc(state, number + 1);
285 loop->block = cloog_block_alloc(statement, 0, NULL, nb_iterators);
287 return loop ;
292 * cloog_loop_read function:
293 * This function reads loop data from a file (foo, possibly stdin) and
294 * returns a pointer to a CloogLoop structure containing the read information.
295 * This function can be used only for input file reading, when one loop is
296 * associated with one statement.
297 * - number is the statement block number carried by the loop (-1 if none).
298 * - nb_parameters is the number of parameters.
300 * - September 9th 2002: first version.
301 * - April 16th 2005: adaptation to new CloogStatement struct (with number).
302 * - June 11th 2005: adaptation to new CloogBlock structure.
303 * - June 22nd 2005: Adaptation for GMP.
305 CloogLoop *cloog_loop_read(CloogState *state,
306 FILE *foo, int number, int nb_parameters)
308 int op1, op2, op3;
309 char s[MAX_STRING];
310 CloogDomain *domain;
312 domain = cloog_domain_union_read(state, foo, nb_parameters);
314 /* To read that stupid "0 0 0" line. */
315 while (fgets(s,MAX_STRING,foo) == 0) ;
316 while ((*s=='#' || *s=='\n') || (sscanf(s," %d %d %d",&op1,&op2,&op3)<3))
317 fgets(s,MAX_STRING,foo) ;
319 return cloog_loop_from_domain(state, domain, number);
323 /******************************************************************************
324 * Processing functions *
325 ******************************************************************************/
329 * cloog_loop_malloc function:
330 * This function allocates the memory space for a CloogLoop structure and
331 * sets its fields with default values. Then it returns a pointer to the
332 * allocated space.
333 * - November 21th 2005: first version.
335 CloogLoop *cloog_loop_malloc(CloogState *state)
336 { CloogLoop * loop ;
338 /* Memory allocation for the CloogLoop structure. */
339 loop = (CloogLoop *)malloc(sizeof(CloogLoop)) ;
340 if (loop == NULL)
341 cloog_die("memory overflow.\n");
342 cloog_loop_leak_up(state);
345 /* We set the various fields with default values. */
346 loop->state = state;
347 loop->domain = NULL ;
348 loop->block = NULL ;
349 loop->usr = NULL;
350 loop->inner = NULL ;
351 loop->next = NULL ;
352 loop->otl = 0;
353 loop->stride = NULL;
355 return loop ;
360 * cloog_loop_alloc function:
361 * This function allocates the memory space for a CloogLoop structure and
362 * sets its fields with those given as input. Then it returns a pointer to the
363 * allocated space.
364 * - October 27th 2001: first version.
365 * - June 22nd 2005: Adaptation for GMP.
366 * - November 21th 2005: use of cloog_loop_malloc.
368 CloogLoop *cloog_loop_alloc(CloogState *state,
369 CloogDomain *domain, int otl, CloogStride *stride,
370 CloogBlock *block, CloogLoop *inner, CloogLoop *next)
371 { CloogLoop * loop ;
373 loop = cloog_loop_malloc(state);
375 loop->domain = domain ;
376 loop->block = block ;
377 loop->inner = inner ;
378 loop->next = next ;
379 loop->otl = otl;
380 loop->stride = cloog_stride_copy(stride);
382 return(loop) ;
387 * cloog_loop_add function:
388 * This function adds a CloogLoop structure (loop) at a given place (now) of a
389 * NULL terminated list of CloogLoop structures. The beginning of this list
390 * is (start). This function updates (now) to (loop), and updates (start) if the
391 * added element is the first one -that is when (start) is NULL-.
392 * - October 28th 2001: first version.
394 void cloog_loop_add(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
395 { if (*start == NULL)
396 { *start = loop ;
397 *now = *start ;
399 else
400 { (*now)->next = loop ;
401 *now = (*now)->next ;
407 * cloog_loop_add function:
408 * This function adds a CloogLoop structure (loop) at a given place (now) of a
409 * NULL terminated list of CloogLoop structures. The beginning of this list
410 * is (start). This function updates (now) to the end of the loop list (loop),
411 * and updates (start) if the added element is the first one -that is when
412 * (start) is NULL-.
413 * - September 9th 2005: first version.
415 void cloog_loop_add_list(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
416 { if (*start == NULL)
417 { *start = loop ;
418 *now = *start ;
420 else
421 { (*now)->next = loop ;
422 *now = (*now)->next ;
425 while ((*now)->next != NULL)
426 *now = (*now)->next ;
431 * cloog_loop_copy function:
432 * This function returns a copy of the CloogLoop structure given as input. In
433 * fact, there is just new allocations for the CloogLoop structures, but their
434 * contents are the same.
435 * - October 28th 2001: first version.
436 * - July 3rd->11th 2003: memory leaks hunt and correction.
438 CloogLoop * cloog_loop_copy(CloogLoop * source)
439 { CloogLoop * loop ;
440 CloogBlock * block ;
441 CloogDomain * domain ;
443 loop = NULL ;
444 if (source != NULL)
445 { domain = cloog_domain_copy(source->domain) ;
446 block = cloog_block_copy(source->block) ;
447 loop = cloog_loop_alloc(source->state, domain, source->otl,
448 source->stride, block, NULL, NULL);
449 loop->usr = source->usr;
450 loop->inner = cloog_loop_copy(source->inner) ;
451 loop->next = cloog_loop_copy(source->next) ;
453 return(loop) ;
458 * cloog_loop_add_disjoint function:
459 * This function adds some CloogLoop structures at a given place (now) of a
460 * NULL terminated list of CloogLoop structures. The beginning of this list
461 * is (start). (loop) can be an union of polyhedra, this function separates the
462 * union into a list of *disjoint* polyhedra then adds the list. This function
463 * updates (now) to the end of the list and updates (start) if first added
464 * element is the first of the principal list -that is when (start) is NULL-.
465 * (loop) can be freed by this function, basically when its domain is actually
466 * a union of polyhedra, but don't worry, all the useful data are now stored
467 * inside the list (start). We do not use PolyLib's Domain_Disjoint function,
468 * since the number of union components is often higher (thus code size too).
469 * - October 28th 2001: first version.
470 * - November 14th 2001: bug correction (this one was hard to find !).
471 * - July 3rd->11th 2003: memory leaks hunt and correction.
472 * - June 22nd 2005: Adaptation for GMP.
473 * - October 27th 2005: (debug) included blocks were not copied for new loops.
475 void cloog_loop_add_disjoint(start, now, loop)
476 CloogLoop ** start, ** now, * loop ;
478 CloogLoop * sep, * inner ;
479 CloogDomain *domain, *seen, *temp, *rest;
480 CloogBlock * block ;
482 if (cloog_domain_isconvex(loop->domain))
483 cloog_loop_add(start,now,loop) ;
484 else {
485 domain = cloog_domain_simplify_union(loop->domain);
486 loop->domain = NULL ;
488 /* We separate the first element of the rest of the union. */
489 domain = cloog_domain_cut_first(domain, &rest);
491 /* This first element is the first of the list of disjoint polyhedra. */
492 sep = cloog_loop_alloc(loop->state, domain, 0, NULL,
493 loop->block, loop->inner, NULL);
494 cloog_loop_add(start,now,sep) ;
496 seen = cloog_domain_copy(domain);
497 while (!cloog_domain_isempty(domain = rest)) {
498 temp = cloog_domain_cut_first(domain, &rest);
499 domain = cloog_domain_difference(temp, seen);
500 cloog_domain_free(temp);
502 if (cloog_domain_isempty(domain)) {
503 cloog_domain_free(domain);
504 continue;
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(loop->state, cloog_domain_copy(domain),
515 0, NULL, block, inner, NULL);
516 /* domain can be an union too. If so: recursion. */
517 if (cloog_domain_isconvex(domain))
518 cloog_loop_add(start,now,sep) ;
519 else
520 cloog_loop_add_disjoint(start,now,sep) ;
522 if (cloog_domain_isempty(rest)) {
523 cloog_domain_free(domain);
524 break;
527 seen = cloog_domain_union(seen, domain);
529 cloog_domain_free(rest);
530 cloog_domain_free(seen);
531 cloog_loop_free_parts(loop,0,0,0,0) ;
537 * cloog_loop_disjoint function:
538 * This function returns a list of loops such that each loop with non-convex
539 * domain in the input list (loop) is separated into several loops where the
540 * domains are the components of the union of *disjoint* polyhedra equivalent
541 * to the original non-convex domain. See cloog_loop_add_disjoint comments
542 * for more details.
543 * - September 16th 2005: first version.
545 CloogLoop * cloog_loop_disjoint(CloogLoop * loop)
546 { CloogLoop *res=NULL, * now=NULL, * next ;
548 /* Because this is often the case, don't waste time ! */
549 if (loop && !loop->next && cloog_domain_isconvex(loop->domain))
550 return loop ;
552 while (loop != NULL)
553 { next = loop->next ;
554 loop->next = NULL ;
555 cloog_loop_add_disjoint(&res,&now,loop) ;
556 loop = next ;
559 return res ;
564 * cloog_loop_restrict function:
565 * This function returns the (loop) in the context of (context): it makes the
566 * intersection between the (loop) domain and the (context), then it returns
567 * a pointer to a new loop, with this intersection as domain.
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(CloogLoop *loop, CloogDomain *context)
574 { int new_dimension ;
575 CloogDomain * domain, * extended_context, * new_domain ;
576 CloogLoop * new_loop ;
578 domain = loop->domain ;
579 if (cloog_domain_dimension(domain) > cloog_domain_dimension(context))
581 new_dimension = cloog_domain_dimension(domain);
582 extended_context = cloog_domain_extend(context, new_dimension);
583 new_domain = cloog_domain_intersection(extended_context,loop->domain) ;
584 cloog_domain_free(extended_context) ;
586 else
587 new_domain = cloog_domain_intersection(context,loop->domain) ;
589 if (cloog_domain_isempty(new_domain))
590 { cloog_domain_free(new_domain) ;
591 return(NULL) ;
593 else {
594 new_loop = cloog_loop_alloc(loop->state, new_domain,
595 0, NULL, loop->block, loop->inner, NULL);
596 return(new_loop) ;
602 * Call cloog_loop_restrict on each loop in the list "loop" and return
603 * the concatenated result.
605 CloogLoop *cloog_loop_restrict_all(CloogLoop *loop, CloogDomain *context)
607 CloogLoop *next;
608 CloogLoop *res = NULL;
609 CloogLoop **res_next = &res;
611 for (; loop; loop = next) {
612 next = loop->next;
614 *res_next = cloog_loop_restrict(loop, context);
615 if (*res_next) {
616 res_next = &(*res_next)->next;
617 cloog_loop_free_parts(loop, 1, 0, 0, 0);
618 } else {
619 loop->next = NULL;
620 cloog_loop_free(loop);
624 return res;
627 CloogLoop *cloog_loop_restrict_inner(CloogLoop *loop)
629 CloogLoop *l;
631 for (l = loop; l; l = l->next)
632 l->inner = cloog_loop_restrict_all(l->inner, l->domain);
634 return loop;
638 * cloog_loop_project function:
639 * This function returns the projection of (loop) on the (level) first
640 * dimensions (outer loops). It makes the projection of the (loop) domain,
641 * then it returns a pointer to a new loop, with this projection as domain.
643 * - October 27th 2001: first version.
644 * - July 3rd->11th 2003: memory leaks hunt and correction.
645 * - June 22nd 2005: Adaptation for GMP.
647 CloogLoop * cloog_loop_project(CloogLoop * loop, int level)
649 CloogDomain * new_domain ;
650 CloogLoop * new_loop, * copy ;
652 copy = cloog_loop_alloc(loop->state, loop->domain, loop->otl, loop->stride,
653 loop->block, loop->inner, NULL);
655 if (cloog_domain_dimension(loop->domain) == level)
656 new_domain = cloog_domain_copy(loop->domain) ;
657 else
658 new_domain = cloog_domain_project(loop->domain, level);
660 new_loop = cloog_loop_alloc(loop->state, new_domain, 0, NULL,
661 NULL, copy, NULL);
663 return(new_loop) ;
668 * Call cloog_loop_project on each loop in the list "loop" and return
669 * the concatenated result.
671 CloogLoop *cloog_loop_project_all(CloogLoop *loop, int level)
673 CloogLoop *next;
674 CloogLoop *res = NULL;
675 CloogLoop **res_next = &res;
677 for (; loop; loop = next) {
678 next = loop->next;
680 *res_next = cloog_loop_project(loop, level);
681 res_next = &(*res_next)->next;
682 cloog_loop_free_parts(loop, 0, 0, 0, 0);
685 return res;
690 * cloog_loop_concat function:
691 * This function returns a pointer to the concatenation of the
692 * CloogLoop lists given as input.
693 * - October 28th 2001: first version.
695 CloogLoop * cloog_loop_concat(CloogLoop * a, CloogLoop * b)
696 { CloogLoop * loop, * temp ;
698 loop = a ;
699 temp = loop ;
700 if (loop != NULL)
701 { while (temp->next != NULL)
702 temp = temp->next ;
703 temp->next = b ;
705 else
706 loop = b ;
708 return(loop) ;
713 * cloog_loop_combine:
714 * Combine consecutive loops with identical domains into
715 * a single loop with the concatenation of their inner loops
716 * as inner loop.
718 CloogLoop *cloog_loop_combine(CloogLoop *loop)
720 CloogLoop *first, *second;
722 for (first = loop; first; first = first->next) {
723 while (first->next) {
724 if (!cloog_domain_lazy_equal(first->domain, first->next->domain))
725 break;
726 second = first->next;
727 first->inner = cloog_loop_concat(first->inner, second->inner);
728 first->next = second->next;
729 cloog_loop_free_parts(second, 1, 0, 0, 0);
733 return loop;
737 * Remove loops from list that have an empty domain.
739 CloogLoop *cloog_loop_remove_empty_domain_loops(CloogLoop *loop)
741 CloogLoop *l, *res, *next, **res_next;
743 res = NULL;
744 res_next = &res;
745 for (l = loop; l; l = next) {
746 next = l->next;
747 if (cloog_domain_isempty(l->domain))
748 cloog_loop_free_parts(l, 1, 1, 1, 0);
749 else {
750 *res_next = l;
751 res_next = &(*res_next)->next;
754 *res_next = NULL;
756 return res;
759 CloogLoop *cloog_loop_decompose_inner(CloogLoop *loop,
760 int level, int scalar, int *scaldims, int nb_scattdims);
762 /* For each loop with only one inner loop, replace the domain
763 * of the loop with the projection of the domain of the inner
764 * loop. To increase the number of loops with a single inner
765 * we first decompose the inner loops into strongly connected
766 * components.
768 CloogLoop *cloog_loop_specialize(CloogLoop *loop,
769 int level, int scalar, int *scaldims, int nb_scattdims)
771 int dim;
772 CloogDomain *domain;
773 CloogLoop *l;
775 loop = cloog_loop_decompose_inner(loop, level, scalar,
776 scaldims, nb_scattdims);
778 for (l = loop; l; l = l->next) {
779 if (l->inner->next)
780 continue;
781 if (!cloog_domain_isconvex(l->inner->domain))
782 continue;
784 dim = cloog_domain_dimension(l->domain);
785 domain = cloog_domain_project(l->inner->domain, dim);
786 if (cloog_domain_isconvex(domain)) {
787 cloog_domain_free(l->domain);
788 l->domain = domain;
789 } else {
790 cloog_domain_free(domain);
794 return cloog_loop_remove_empty_domain_loops(loop);
798 * cloog_loop_separate function:
799 * This function implements the Quillere algorithm for separation of multiple
800 * loops: for a given set of polyhedra (loop), it computes a set of disjoint
801 * polyhedra such that the unions of these sets are equal, and returns this set.
802 * - October 28th 2001: first version.
803 * - November 14th 2001: elimination of some unused blocks.
804 * - August 13th 2002: (debug) in the case of union of polyhedra for one
805 * loop, redundant constraints are fired.
806 * - July 3rd->11th 2003: memory leaks hunt and correction.
807 * - June 22nd 2005: Adaptation for GMP.
808 * - October 16th 2005: Removal of the non-shared constraint elimination when
809 * there is only one loop in the list (seems to work
810 * without now, DomainSimplify may have been improved).
811 * The problem was visible with test/iftest2.cloog.
813 CloogLoop * cloog_loop_separate(CloogLoop * loop)
814 { int lazy_equal=0, disjoint = 0;
815 CloogLoop * new_loop, * new_inner, * res, * now, * temp, * Q,
816 * inner, * old /*, * previous, * next*/ ;
817 CloogDomain *UQ, *domain;
819 if (loop == NULL)
820 return NULL ;
822 loop = cloog_loop_combine(loop);
824 if (loop->next == NULL)
825 return cloog_loop_disjoint(loop) ;
827 UQ = cloog_domain_copy(loop->domain) ;
828 domain = cloog_domain_copy(loop->domain) ;
829 res = cloog_loop_alloc(loop->state, domain, 0, NULL,
830 loop->block, loop->inner, NULL);
832 old = loop ;
833 while((loop = loop->next) != NULL)
834 { temp = NULL ;
836 /* For all Q, add Q-loop associated with the blocks of Q alone,
837 * and Q inter loop associated with the blocks of Q and loop.
839 for (Q = res; Q; Q = Q->next) {
840 /* Add (Q inter loop). */
841 if ((disjoint = cloog_domain_lazy_disjoint(Q->domain,loop->domain)))
842 domain = NULL ;
843 else
844 { if ((lazy_equal = cloog_domain_lazy_equal(Q->domain,loop->domain)))
845 domain = cloog_domain_copy(Q->domain) ;
846 else
847 domain = cloog_domain_intersection(Q->domain,loop->domain) ;
849 if (!cloog_domain_isempty(domain))
850 { new_inner = cloog_loop_concat(cloog_loop_copy(Q->inner),
851 cloog_loop_copy(loop->inner)) ;
852 new_loop = cloog_loop_alloc(loop->state, domain, 0, NULL,
853 NULL, new_inner, NULL);
854 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
856 else {
857 disjoint = 1;
858 cloog_domain_free(domain);
862 /* Add (Q - loop). */
863 if (disjoint)
864 domain = cloog_domain_copy(Q->domain) ;
865 else
866 { if (lazy_equal)
867 domain = cloog_domain_empty(Q->domain);
868 else
869 domain = cloog_domain_difference(Q->domain,loop->domain) ;
872 if (!cloog_domain_isempty(domain)) {
873 new_loop = cloog_loop_alloc(loop->state, domain, 0, NULL,
874 NULL, Q->inner, NULL);
875 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
877 else
878 { cloog_domain_free(domain) ;
879 /* If Q->inner is no more useful, we can free it. */
880 inner = Q->inner ;
881 Q->inner = NULL ;
882 cloog_loop_free(inner) ;
886 /* Add loop-UQ associated with the blocks of loop alone.*/
887 if (cloog_domain_lazy_disjoint(loop->domain,UQ))
888 domain = cloog_domain_copy(loop->domain) ;
889 else
890 { if (cloog_domain_lazy_equal(loop->domain,UQ))
891 domain = cloog_domain_empty(UQ);
892 else
893 domain = cloog_domain_difference(loop->domain,UQ) ;
896 if (!cloog_domain_isempty(domain)) {
897 new_loop = cloog_loop_alloc(loop->state, domain, 0, NULL,
898 NULL, loop->inner, NULL);
899 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
901 else
902 { cloog_domain_free(domain) ;
903 /* If loop->inner is no more useful, we can free it. */
904 cloog_loop_free(loop->inner) ;
907 loop->inner = NULL ;
909 if (loop->next != NULL)
910 UQ = cloog_domain_union(UQ, cloog_domain_copy(loop->domain));
911 else
912 cloog_domain_free(UQ);
914 cloog_loop_free_parts(res,1,0,0,1) ;
916 res = temp ;
918 cloog_loop_free_parts(old,1,0,0,1) ;
920 return(res) ;
924 static CloogDomain *bounding_domain(CloogDomain *dom, CloogOptions *options)
926 if (options->sh)
927 return cloog_domain_simple_convex(dom);
928 else
929 return cloog_domain_convex(dom);
934 * cloog_loop_merge function:
935 * This function is the 'soft' version of loop_separate if we are looking for
936 * a code much simpler (and less efficicient). This function returns the new
937 * CloogLoop list.
938 * - October 29th 2001: first version.
939 * - July 3rd->11th 2003: memory leaks hunt and correction.
940 * - June 22nd 2005: Adaptation for GMP.
942 CloogLoop *cloog_loop_merge(CloogLoop *loop, int level, CloogOptions *options)
944 CloogLoop *res, *new_inner, *old;
945 CloogDomain *new_domain, *temp;
947 if (loop == NULL)
948 return loop;
950 if (loop->next == NULL)
951 return cloog_loop_disjoint(loop);
953 old = loop;
954 temp = loop->domain;
955 loop->domain = NULL;
956 new_inner = loop->inner;
958 for (loop = loop->next; loop; loop = loop->next) {
959 temp = cloog_domain_union(temp, loop->domain);
960 loop->domain = NULL;
961 new_inner = cloog_loop_concat(new_inner, loop->inner);
964 new_domain = bounding_domain(temp, options);
966 if (level > 0 && !cloog_domain_is_bounded(new_domain, level) &&
967 cloog_domain_is_bounded(temp, level)) {
968 CloogDomain *splitter, *t2;
970 cloog_domain_free(new_domain);
971 splitter = cloog_domain_bound_splitter(temp, level);
973 res = NULL;
974 while (!cloog_domain_isconvex(splitter)) {
975 CloogDomain *first, *rest;
976 first = cloog_domain_cut_first(splitter, &rest);
977 splitter = rest;
978 t2 = cloog_domain_intersection(first, temp);
979 cloog_domain_free(first);
981 new_domain = bounding_domain(t2, options);
982 cloog_domain_free(t2);
984 if (cloog_domain_isempty(new_domain)) {
985 cloog_domain_free(new_domain);
986 continue;
988 res = cloog_loop_alloc(old->state, new_domain, 0, NULL,
989 NULL, cloog_loop_copy(new_inner), res);
992 t2 = cloog_domain_intersection(splitter, temp);
993 cloog_domain_free(splitter);
995 new_domain = bounding_domain(t2, options);
996 cloog_domain_free(t2);
998 if (cloog_domain_isempty(new_domain)) {
999 cloog_domain_free(new_domain);
1000 cloog_loop_free(new_inner);
1001 } else
1002 res = cloog_loop_alloc(old->state, new_domain, 0, NULL,
1003 NULL, new_inner, res);
1004 } else {
1005 res = cloog_loop_alloc(old->state, new_domain, 0, NULL,
1006 NULL, new_inner, NULL);
1008 cloog_domain_free(temp);
1010 cloog_loop_free_parts(old, 0, 0, 0, 1);
1012 return res;
1016 static int cloog_loop_count(CloogLoop *loop)
1018 int nb_loops;
1020 for (nb_loops = 0; loop; loop = loop->next)
1021 nb_loops++;
1023 return nb_loops;
1028 * cloog_loop_sort function:
1029 * Adaptation from LoopGen 0.4 by F. Quillere. This function sorts a list of
1030 * parameterized disjoint polyhedra, in order to not have lexicographic order
1031 * violation (see Quillere paper).
1032 * - September 16th 2005: inclusion of cloog_loop_number (October 29th 2001).
1034 CloogLoop *cloog_loop_sort(CloogLoop *loop, int level)
1036 CloogLoop *res, *now, **loop_array;
1037 CloogDomain **doms;
1038 int i, nb_loops=0, * permut ;
1040 /* There is no need to sort the parameter domains. */
1041 if (!level)
1042 return loop;
1044 /* We will need to know how many loops are in the list. */
1045 nb_loops = cloog_loop_count(loop);
1047 /* If there is only one loop, it's the end. */
1048 if (nb_loops == 1)
1049 return(loop) ;
1051 /* We have to allocate memory for some useful components:
1052 * - loop_array: the loop array,
1053 * - doms: the array of domains to sort,
1054 * - permut: will give us a possible sort (maybe not the only one).
1056 loop_array = (CloogLoop **)malloc(nb_loops*sizeof(CloogLoop *)) ;
1057 doms = (CloogDomain **)malloc(nb_loops*sizeof(CloogDomain *));
1058 permut = (int *)malloc(nb_loops*sizeof(int)) ;
1060 /* We fill up the loop and domain arrays. */
1061 for (i=0;i<nb_loops;i++,loop=loop->next)
1062 { loop_array[i] = loop ;
1063 doms[i] = loop_array[i]->domain;
1066 /* cloog_domain_sort will fill up permut. */
1067 cloog_domain_sort(doms, nb_loops, level, permut);
1069 /* With permut and loop_array we build the sorted list. */
1070 res = NULL ;
1071 for (i=0;i<nb_loops;i++)
1072 { /* To avoid pointer looping... loop_add will rebuild the list. */
1073 loop_array[permut[i]-1]->next = NULL ;
1074 cloog_loop_add(&res,&now,loop_array[permut[i]-1]) ;
1077 free(permut) ;
1078 free(doms);
1079 free(loop_array) ;
1081 return res;
1086 * cloog_loop_nest function:
1087 * This function changes the loop list in such a way that we have no more than
1088 * one dimension added by level. It returns an equivalent loop list with
1089 * this property.
1090 * - October 29th 2001: first version.
1091 * - July 3rd->11th 2003: memory leaks hunt and correction.
1092 * - June 22nd 2005: Adaptation for GMP.
1093 * - November 21th 2005: (debug) now OK when cloog_loop_restrict returns NULL.
1095 CloogLoop *cloog_loop_nest(CloogLoop *loop, CloogDomain *context, int level)
1096 { int l ;
1097 CloogLoop * p, * temp, * res, * now, * next ;
1098 CloogDomain * new_domain ;
1100 loop = cloog_loop_disjoint(loop);
1102 res = NULL ;
1103 /* Each domain is changed by its intersection with the context. */
1104 while (loop != NULL)
1105 { p = cloog_loop_restrict(loop, context);
1106 next = loop->next ;
1108 if (p != NULL)
1109 { cloog_loop_free_parts(loop,1,0,0,0) ;
1111 temp = cloog_loop_alloc(p->state, p->domain, 0, NULL,
1112 p->block, p->inner, NULL);
1114 /* If the intersection dimension is too big, we make projections smaller
1115 * and smaller, and each projection includes the preceding projection
1116 * (thus, in the target list, dimensions are added one by one).
1118 if (cloog_domain_dimension(p->domain) >= level)
1119 for (l = cloog_domain_dimension(p->domain); l >= level; l--) {
1120 new_domain = cloog_domain_project(p->domain, l);
1121 temp = cloog_loop_alloc(p->state, new_domain, 0, NULL,
1122 NULL, temp, NULL);
1125 /* p is no more useful (but its content yes !). */
1126 cloog_loop_free_parts(p,0,0,0,0) ;
1128 cloog_loop_add(&res,&now,temp) ;
1130 else
1131 cloog_loop_free_parts(loop,1,1,1,0) ;
1133 loop = next ;
1136 return(res) ;
1140 /* Check if the domains of the inner loops impose a stride constraint
1141 * on the given level.
1142 * The core of the search is implemented in cloog_domain_list_stride.
1143 * Here, we simply construct a list of domains to pass to this function
1144 * and if a stride is found, we adjust the lower bounds by calling
1145 * cloog_domain_stride_lower_bound.
1147 static int cloog_loop_variable_offset_stride(CloogLoop *loop, int level)
1149 CloogDomainList *list = NULL;
1150 CloogLoop *inner;
1151 CloogStride *stride;
1153 for (inner = loop->inner; inner; inner = inner->next) {
1154 CloogDomainList *entry = ALLOC(CloogDomainList);
1155 entry->domain = cloog_domain_copy(inner->domain);
1156 entry->next = list;
1157 list = entry;
1160 stride = cloog_domain_list_stride(list, level);
1162 cloog_domain_list_free(list);
1164 if (!stride)
1165 return 0;
1167 loop->stride = stride;
1168 loop->domain = cloog_domain_stride_lower_bound(loop->domain, level, stride);
1170 return 1;
1175 * cloog_loop_stride function:
1176 * This function will find the stride of a loop for the iterator at the column
1177 * number 'level' in the constraint matrix. It will update the lower bound of
1178 * the iterator accordingly. Basically, the function will try to find in the
1179 * inner loops a common condition on this iterator for the inner loop iterators
1180 * to be integral. For instance, let us consider a loop with the iterator i,
1181 * the iteration domain -4<=i<=n, and its two inner loops with the iterator j.
1182 * The first inner loop has the constraint 3j=i, and the second one has the
1183 * constraint 6j=i. Then the common constraint on i for j to be integral is
1184 * i%3=0, the stride for i is 3. Lastly, we have to find the new lower bound
1185 * for i: the first value satisfying the common constraint: -3. At the end, the
1186 * iteration domain for i is -3<=i<=n and the stride for i is 3.
1188 * The algorithm implemented in this function only allows for strides
1189 * on loops with a lower bound that has a constant remainder on division
1190 * by the stride. Before initiating this procedure, we first check
1191 * if we can find a stride with a lower bound with a variable offset in
1192 * cloog_loop_variable_offset_stride.
1194 * - loop is the loop including the iteration domain of the considered iterator,
1195 * - level is the column number of the iterator in the matrix of contraints.
1197 * - June 29th 2003: first version (work in progress since June 26th 2003).
1198 * - July 14th 2003: simpler version.
1199 * - June 22nd 2005: Adaptation for GMP (from S. Verdoolaege's 0.12.1 version).
1201 void cloog_loop_stride(CloogLoop * loop, int level)
1202 { int first_search ;
1203 cloog_int_t stride, ref_offset, offset, potential;
1204 CloogLoop * inner ;
1206 if (!cloog_domain_can_stride(loop->domain, level))
1207 return;
1209 if (cloog_loop_variable_offset_stride(loop, level))
1210 return;
1212 cloog_int_init(stride);
1213 cloog_int_init(ref_offset);
1214 cloog_int_init(offset);
1215 cloog_int_init(potential);
1217 cloog_int_set_si(ref_offset, 0);
1218 cloog_int_set_si(offset, 0);
1220 /* Default stride. */
1221 cloog_int_set_si(stride, 1);
1222 first_search = 1 ;
1223 inner = loop->inner ;
1225 while (inner != NULL)
1226 { /* If the minimun stride has not been found yet, find the stride. */
1227 if ((first_search) || (!cloog_int_is_one(stride)))
1229 cloog_domain_stride(inner->domain, level, &potential, &offset);
1230 if (!cloog_int_is_one(potential) && (!first_search))
1231 { /* Offsets must be the same for common stride. */
1232 cloog_int_gcd(stride, potential, stride);
1233 if (!cloog_int_is_zero(stride)) {
1234 cloog_int_fdiv_r(offset, offset, stride);
1235 cloog_int_fdiv_r(ref_offset, ref_offset, stride);
1237 if (cloog_int_ne(offset,ref_offset))
1238 cloog_int_set_si(stride, 1);
1240 else {
1241 cloog_int_set(stride, potential);
1242 cloog_int_set(ref_offset, offset);
1245 first_search = 0 ;
1248 inner = inner->next ;
1251 if (cloog_int_is_zero(stride))
1252 cloog_int_set_si(stride, 1);
1254 /* Update the values if necessary. */
1255 if (!cloog_int_is_one(stride))
1256 { /* Update the stride value. */
1257 if (!cloog_int_is_zero(offset))
1258 cloog_int_sub(offset, stride, offset);
1259 loop->stride = cloog_stride_alloc(stride, offset);
1260 loop->domain = cloog_domain_stride_lower_bound(loop->domain, level,
1261 loop->stride);
1264 cloog_int_clear(stride);
1265 cloog_int_clear(ref_offset);
1266 cloog_int_clear(offset);
1267 cloog_int_clear(potential);
1271 void cloog_loop_otl(CloogLoop *loop, int level)
1273 if (cloog_domain_is_otl(loop->domain, level))
1274 loop->otl = 1;
1279 * cloog_loop_stop function:
1280 * This function implements the 'stop' option : each domain of each loop
1281 * in the list 'loop' is replaced by 'context'. 'context' should be the
1282 * domain of the outer loop. By using this method, there are no more dimensions
1283 * to scan and the simplification step will automaticaly remove the domains
1284 * since they are the same as the corresponding contexts. The effect of this
1285 * function is to stop the code generation at the level this function is called,
1286 * the resulting code do not consider the next dimensions.
1287 * - January 11th 2005: first version.
1289 CloogLoop * cloog_loop_stop(CloogLoop * loop, CloogDomain * context)
1290 { if (loop == NULL)
1291 return NULL ;
1292 else
1293 { cloog_domain_free(loop->domain) ;
1294 loop->domain = cloog_domain_copy(context) ;
1295 loop->next = cloog_loop_stop(loop->next, context) ;
1298 return loop ;
1302 static int level_is_constant(int level, int scalar, int *scaldims, int nb_scattdims)
1304 return level && (level+scalar <= nb_scattdims) && (scaldims[level+scalar-1]);
1309 * Compare the constant dimensions of loops 'l1' and 'l2' starting at 'scalar'
1310 * and return -1 if the vector of constant dimensions of 'l1' is smaller
1311 * than that of 'l2', 0 if they are the same and +1 if that of 'l1' is
1312 * greater than that of 'l2'.
1313 * This function should be called on the innermost loop (the loop
1314 * containing a block).
1315 * \param l1 Loop to be compared with l2.
1316 * \param l2 Loop to be compared with l1.
1317 * \param level Current non-scalar dimension.
1318 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1319 * \param nb_scattdims Size of the scaldims array.
1320 * \param scalar Current scalar dimension.
1321 * \return -1 if (l1 < l2), 0 if (l1 == l2) and +1 if (l1 > l2)
1323 int cloog_loop_constant_cmp(CloogLoop *l1, CloogLoop *l2, int level,
1324 int *scaldims, int nb_scattdims, int scalar)
1326 CloogBlock *b1, *b2;
1327 b1 = l1->block;
1328 b2 = l2->block;
1329 while (level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1330 int cmp = cloog_int_cmp(b1->scaldims[scalar], b2->scaldims[scalar]);
1331 if (cmp)
1332 return cmp;
1333 scalar++;
1335 return 0;
1340 * cloog_loop_scalar_gt function:
1341 * This function returns 1 if loop 'l1' is greater than loop 'l2' for the
1342 * scalar dimension vector that begins at dimension 'scalar', 0 otherwise. What
1343 * we want to know is whether a loop is scheduled before another one or not.
1344 * This function solves the problem when the considered dimension for scheduling
1345 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1346 * this function will reason about the vector of scalar dimension that begins
1347 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1348 * \param l1 Loop to be compared with l2.
1349 * \param l2 Loop to be compared with l1.
1350 * \param level Current non-scalar dimension.
1351 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1352 * \param nb_scattdims Size of the scaldims array.
1353 * \param scalar Current scalar dimension.
1354 * \return 1 if (l1 > l2), 0 otherwise.
1356 * - September 9th 2005: first version.
1357 * - October 15nd 2007: now "greater than" instead of "greater or equal".
1359 int cloog_loop_scalar_gt(l1, l2, level, scaldims, nb_scattdims, scalar)
1360 CloogLoop * l1, * l2 ;
1361 int level, * scaldims, nb_scattdims, scalar ;
1363 return cloog_loop_constant_cmp(l1, l2, level, scaldims, nb_scattdims, scalar) > 0;
1368 * cloog_loop_scalar_eq function:
1369 * This function returns 1 if loop 'l1' is equal to loop 'l2' for the scalar
1370 * dimension vector that begins at dimension 'scalar', 0 otherwise. What we want
1371 * to know is whether two loops are scheduled for the same time or not.
1372 * This function solves the problem when the considered dimension for scheduling
1373 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1374 * this function will reason about the vector of scalar dimension that begins
1375 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1376 * - l1 and l2 are the loops to compare,
1377 * - level is the current non-scalar dimension,
1378 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1379 * - nb_scattdims is the size of the scaldims array,
1380 * - scalar is the current scalar dimension.
1382 * - September 9th 2005 : first version.
1384 int cloog_loop_scalar_eq(l1, l2, level, scaldims, nb_scattdims, scalar)
1385 CloogLoop * l1, * l2 ;
1386 int level, * scaldims, nb_scattdims, scalar ;
1388 return cloog_loop_constant_cmp(l1, l2, level, scaldims, nb_scattdims, scalar) == 0;
1393 * cloog_loop_scalar_sort function:
1394 * This function sorts a linked list of loops (loop) with respect to the
1395 * scalar dimension vector that begins at dimension 'scalar'. Since there may
1396 * be a succession of scalar dimensions, this function will reason about the
1397 * vector of scalar dimension that begins at dimension 'level+scalar' and
1398 * finish to the first non-scalar dimension.
1399 * \param loop Loop list to sort.
1400 * \param level Current non-scalar dimension.
1401 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1402 * \param nb_scattdims Size of the scaldims array.
1403 * \param scalar Current scalar dimension.
1404 * \return A pointer to the sorted list.
1406 * - July 2nd 2005: first developments.
1407 * - September 2nd 2005: first version.
1408 * - October 15nd 2007: complete rewrite to remove bugs, now a bubble sort.
1410 CloogLoop * cloog_loop_scalar_sort(loop, level, scaldims, nb_scattdims, scalar)
1411 CloogLoop * loop ;
1412 int level, * scaldims, nb_scattdims, scalar ;
1413 { int ok ;
1414 CloogLoop **current;
1416 do {
1417 ok = 1;
1418 for (current = &loop; (*current)->next; current = &(*current)->next) {
1419 CloogLoop *next = (*current)->next;
1420 if (cloog_loop_scalar_gt(*current,next,level,scaldims,nb_scattdims,scalar)) {
1421 ok = 0;
1422 (*current)->next = next->next;
1423 next->next = *current;
1424 *current = next;
1427 } while (!ok);
1429 return loop ;
1434 * cloog_loop_generate_backtrack function:
1435 * adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1436 * backtrack of the Quillere et al. algorithm (see the Quillere paper).
1437 * It eliminates unused iterations of the current level for the new one. See the
1438 * example called linearity-1-1 example with and without this part for an idea.
1439 * - October 26th 2001: first version in cloog_loop_generate_general.
1440 * - July 31th 2002: (debug) no more parasite loops (REALLY hard !).
1441 * - October 30th 2005: extraction from cloog_loop_generate_general.
1443 CloogLoop *cloog_loop_generate_backtrack(CloogLoop *loop,
1444 int level, CloogOptions *options)
1446 CloogDomain * domain ;
1447 CloogLoop * now, * now2, * next, * next2, * end, * temp, * l, * inner,
1448 * new_loop ;
1450 temp = loop ;
1451 loop = NULL ;
1453 while (temp != NULL)
1454 { l = NULL ;
1455 inner = temp->inner ;
1457 while (inner != NULL)
1458 { next = inner->next ;
1459 /* This 'if' and its first part is the debug of july 31th 2002. */
1460 if (inner->block != NULL) {
1461 end = cloog_loop_alloc(temp->state, inner->domain, 0, NULL,
1462 inner->block, NULL, NULL);
1463 domain = cloog_domain_copy(temp->domain) ;
1464 new_loop = cloog_loop_alloc(temp->state, domain, 0, NULL,
1465 NULL, end, NULL);
1467 else
1468 new_loop = cloog_loop_project(inner, level);
1470 cloog_loop_free_parts(inner,0,0,0,0) ;
1471 cloog_loop_add(&l,&now2,new_loop) ;
1472 inner = next ;
1475 temp->inner = NULL ;
1477 if (l != NULL)
1478 { l = cloog_loop_separate(l) ;
1479 l = cloog_loop_sort(l, level);
1480 while (l != NULL) {
1481 l->stride = cloog_stride_copy(l->stride);
1482 cloog_loop_add(&loop,&now,l) ;
1483 l = l->next ;
1486 next2 = temp->next ;
1487 cloog_loop_free_parts(temp,1,0,0,0) ;
1488 temp = next2 ;
1491 return loop ;
1496 * Return 1 if we need to continue recursing to the specified level.
1498 int cloog_loop_more(CloogLoop *loop, int level, int scalar, int nb_scattdims)
1500 return level + scalar <= nb_scattdims ||
1501 cloog_domain_dimension(loop->domain) >= level;
1504 CloogLoop *cloog_loop_generate_restricted_or_stop(CloogLoop *loop,
1505 CloogDomain *context,
1506 int level, int scalar, int *scaldims, int nb_scattdims,
1507 CloogOptions *options);
1510 * cloog_loop_generate_general function:
1511 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1512 * Quillere algorithm for polyhedron scanning from step 3 to 5.
1513 * (see the Quillere paper).
1514 * - loop is the loop for which we have to generate a scanning code,
1515 * - level is the current non-scalar dimension,
1516 * - scalar is the current scalar dimension,
1517 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1518 * - nb_scattdims is the size of the scaldims array,
1519 * - options are the general code generation options.
1521 * - October 26th 2001: first version.
1522 * - July 3rd->11th 2003: memory leaks hunt and correction.
1523 * - June 22nd 2005: Adaptation for GMP.
1524 * - September 2nd 2005: The function have been cutted out in two pieces:
1525 * cloog_loop_generate and this one, in order to handle
1526 * the scalar dimension case more efficiently with
1527 * cloog_loop_generate_scalar.
1528 * - November 15th 2005: (debug) the result of the cloog_loop_generate call may
1529 * be a list of polyhedra (especially if stop option is
1530 * used): cloog_loop_add_list instead of cloog_loop_add.
1532 CloogLoop *cloog_loop_generate_general(CloogLoop *loop,
1533 int level, int scalar, int *scaldims, int nb_scattdims,
1534 CloogOptions *options)
1536 CloogLoop * res, * now, * temp, * l, * new_loop, * inner, * now2, * end,
1537 * next, * into ;
1538 CloogDomain * domain ;
1539 int separate = 0;
1541 /* 3. Separate all projections into disjoint polyhedra. */
1542 if ((options->f > level+scalar) || (options->f < 0))
1543 res = cloog_loop_merge(loop, level, options);
1544 else {
1545 res = cloog_loop_separate(loop);
1546 separate = 1;
1549 /* 3b. -correction- sort the loops to determine their textual order. */
1550 res = cloog_loop_sort(res, level);
1552 res = cloog_loop_restrict_inner(res);
1554 if (separate)
1555 res = cloog_loop_specialize(res, level, scalar, scaldims, nb_scattdims);
1557 /* 4. Recurse for each loop with the current domain as context. */
1558 temp = res ;
1559 res = NULL ;
1560 if (!level || (level+scalar < options->l) || (options->l < 0))
1561 while(temp != NULL)
1562 { if (level && options->strides)
1563 cloog_loop_stride(temp, level);
1564 if (level && options->otl)
1565 cloog_loop_otl(temp, level);
1566 inner = temp->inner ;
1567 domain = temp->domain ;
1568 into = NULL ;
1569 while (inner != NULL)
1570 { /* 4b. -ced- recurse for each sub-list of non terminal loops. */
1571 if (cloog_loop_more(inner, level + 1, scalar, nb_scattdims)) {
1572 end = inner;
1573 while ((end->next != NULL) &&
1574 cloog_loop_more(end->next, level + 1, scalar, nb_scattdims))
1575 end = end->next ;
1577 next = end->next ;
1578 end->next = NULL ;
1580 l = cloog_loop_generate_restricted_or_stop(inner, domain,
1581 level + 1, scalar, scaldims, nb_scattdims, options);
1583 if (l != NULL)
1584 cloog_loop_add_list(&into,&now,l) ;
1586 inner = next ;
1588 else
1589 { cloog_loop_add(&into,&now,inner) ;
1590 inner = inner->next ;
1593 next = temp->next ;
1594 temp->next = NULL ;
1595 temp->inner = into ;
1596 cloog_loop_add(&res,&now2,temp) ;
1597 temp = next ;
1599 else
1600 while (temp != NULL)
1601 { next = temp->next ;
1602 l = cloog_loop_nest(temp->inner, temp->domain, level+1);
1603 new_loop = cloog_loop_alloc(temp->state, temp->domain, 0, NULL,
1604 NULL, l, NULL);
1605 temp->inner = NULL ;
1606 temp->next = NULL ;
1607 cloog_loop_free_parts(temp,0,0,0,0) ;
1608 cloog_loop_add(&res,&now,new_loop) ;
1609 temp = next ;
1612 /* 5. eliminate unused iterations of the current level for the new one. See
1613 * the example called linearity-1-1 example with and without this part
1614 * for an idea.
1616 if (options->backtrack && level &&
1617 ((level+scalar < options->l) || (options->l < 0)) &&
1618 ((options->f <= level+scalar) && !(options->f < 0)))
1619 res = cloog_loop_generate_backtrack(res, level, options);
1621 /* Pray for my new paper to be accepted somewhere since the following stuff
1622 * is really amazing :-) !
1623 * Far long later: The paper has been accepted to PACT 2004 :-))). But there
1624 * are still some bugs and I have no time to fix them. Thus now you have to
1625 * pray for me to get an academic position for that really amazing stuff :-) !
1626 * Later again: OK, I get my academic position, but still I have not enough
1627 * time to fix and clean this part... Pray again :-) !!!
1629 /* res = cloog_loop_unisolate(res,level) ;*/
1631 return(res) ;
1635 CloogLoop *cloog_loop_generate_restricted(CloogLoop *loop,
1636 int level, int scalar, int *scaldims, int nb_scattdims,
1637 CloogOptions *options);
1641 * cloog_loop_generate_scalar function:
1642 * This function applies the simplified code generation scheme in the trivial
1643 * case of scalar dimensions. When dealing with scalar dimensions, there is
1644 * no need of costly polyhedral operations for separation or sorting: sorting
1645 * is a question of comparing scalar vectors and separation amounts to consider
1646 * only loops with the same scalar vector for the next step of the code
1647 * generation process. This function achieves the separation/sorting process
1648 * for the vector of scalar dimension that begins at dimension 'level+scalar'
1649 * and finish to the first non-scalar dimension.
1650 * - loop is the loop for which we have to generate a scanning code,
1651 * - level is the current non-scalar dimension,
1652 * - scalar is the current scalar dimension,
1653 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1654 * - nb_scattdims is the size of the scaldims array,
1655 * - options are the general code generation options.
1657 * - September 2nd 2005: First version.
1659 CloogLoop *cloog_loop_generate_scalar(CloogLoop *loop,
1660 int level, int scalar, int *scaldims, int nb_scattdims,
1661 CloogOptions *options)
1662 { CloogLoop * res, * now, * temp, * l, * end, * next, * ref ;
1663 int scalar_new;
1665 /* We sort the loop list with respect to the current scalar vector. */
1666 res = cloog_loop_scalar_sort(loop,level,scaldims,nb_scattdims,scalar) ;
1668 scalar_new = scalar + scaldims[level + scalar - 1];
1670 temp = res ;
1671 res = NULL ;
1672 while (temp != NULL)
1673 { /* Then we will appy the general code generation process to each sub-list
1674 * of loops with the same scalar vector.
1676 end = temp ;
1677 ref = temp ;
1679 while((end->next != NULL) &&
1680 cloog_loop_more(end->next, level, scalar_new, nb_scattdims) &&
1681 cloog_loop_scalar_eq(ref,end->next,level,scaldims,nb_scattdims,scalar))
1682 end = end->next ;
1684 next = end->next ;
1685 end->next = NULL ;
1687 /* For the next dimension, scalar value is updated by adding the scalar
1688 * vector size, which is stored at scaldims[level+scalar-1].
1690 if (cloog_loop_more(temp, level, scalar_new, nb_scattdims)) {
1691 l = cloog_loop_generate_restricted(temp, level, scalar_new,
1692 scaldims, nb_scattdims, options);
1694 if (l != NULL)
1695 cloog_loop_add_list(&res, &now, l);
1696 } else
1697 cloog_loop_add(&res, &now, temp);
1699 temp = next ;
1702 return res ;
1706 /* Compare loop with the next loop based on their constant dimensions.
1707 * The result is < 0, == 0 or > 0 depending on whether the constant
1708 * dimensions of loop are lexicographically smaller, equal or greater
1709 * than those of loop->next.
1710 * If loop is the last in the list, then it is assumed to be smaller
1711 * than the "next" one.
1713 static int cloog_loop_next_scal_cmp(CloogLoop *loop)
1715 int i;
1716 int nb_scaldims;
1718 if (!loop->next)
1719 return -1;
1721 nb_scaldims = loop->block->nb_scaldims;
1722 if (loop->next->block->nb_scaldims < nb_scaldims)
1723 nb_scaldims = loop->next->block->nb_scaldims;
1725 for (i = 0; i < nb_scaldims; ++i) {
1726 int cmp = cloog_int_cmp(loop->block->scaldims[i],
1727 loop->next->block->scaldims[i]);
1728 if (cmp)
1729 return cmp;
1731 return loop->block->nb_scaldims - loop->next->block->nb_scaldims;
1735 /* Check whether the globally constant dimensions of a and b
1736 * have the same value for all globally constant dimensions
1737 * that are situated before any (locally) non-constant dimension.
1739 static int cloog_loop_equal_prefix(CloogLoop *a, CloogLoop *b,
1740 int *scaldims, int nb_scattdims)
1742 int i;
1743 int cst = 0;
1744 int dim = 0;
1746 for (i = 0; i < nb_scattdims; ++i) {
1747 if (!scaldims[i]) {
1748 dim++;
1749 continue;
1751 if (!cloog_int_eq(a->block->scaldims[cst], b->block->scaldims[cst]))
1752 break;
1753 cst++;
1755 for (i = i + 1; i < nb_scattdims; ++i) {
1756 if (scaldims[i])
1757 continue;
1758 if (!cloog_domain_lazy_isconstant(a->domain, dim))
1759 return 0;
1760 /* No need to check that dim is also constant in b and that the
1761 * constant values are equal. That will happen during the check
1762 * whether the two domains are equal.
1764 dim++;
1766 return 1;
1770 /* Try to block adjacent loops in the loop list "loop".
1771 * We only attempt blocking if the constant dimensions of the loops
1772 * in the least are (not necessarily strictly) increasing.
1773 * Then we look for a sublist such that the first (begin) has constant
1774 * dimensions strictly larger than the previous loop in the complete
1775 * list and such that the loop (end) after the last loop in the sublist
1776 * has constant dimensions strictly larger than the last loop in the sublist.
1777 * Furthermore, all loops in the sublist should have the same domain
1778 * (with globally constant dimensions removed) and the difference
1779 * (if any) in constant dimensions may only occur after all the
1780 * (locally) constant dimensions.
1781 * If we find such a sublist, then the blocks of all but the first
1782 * are merged into the block of the first.
1784 * Note that this function can only be called before the global
1785 * blocklist has been created because it may otherwise modify and destroy
1786 * elements on that list.
1788 CloogLoop *cloog_loop_block(CloogLoop *loop, int *scaldims, int nb_scattdims)
1790 CloogLoop *begin, *end, *l;
1791 int begin_after_previous;
1792 int end_after_previous;
1794 if (!loop->next)
1795 return loop;
1796 for (begin = loop; begin; begin = begin->next) {
1797 if (!begin->block || !begin->block->scaldims)
1798 return loop;
1799 if (cloog_loop_next_scal_cmp(begin) > 0)
1800 return loop;
1803 begin_after_previous = 1;
1804 for (begin = loop; begin; begin = begin->next) {
1805 if (!begin_after_previous) {
1806 begin_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1807 continue;
1810 end_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1811 for (end = begin->next; end; end = end->next) {
1812 if (!cloog_loop_equal_prefix(begin, end, scaldims, nb_scattdims))
1813 break;
1814 if (!cloog_domain_lazy_equal(begin->domain, end->domain))
1815 break;
1816 end_after_previous = cloog_loop_next_scal_cmp(end) < 0;
1818 if (end != begin->next && end_after_previous) {
1819 for (l = begin->next; l != end; l = begin->next) {
1820 cloog_block_merge(begin->block, l->block);
1821 begin->next = l->next;
1822 cloog_loop_free_parts(l, 1, 0, 1, 0);
1826 begin_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1829 return loop;
1834 * Check whether for any fixed iteration of the outer loops,
1835 * there is an iteration of loop1 that is lexicographically greater
1836 * than an iteration of loop2.
1837 * Return 1 if there exists (or may exist) such a pair.
1838 * Return 0 if all iterations of loop1 are lexicographically smaller
1839 * than the iterations of loop2.
1840 * If no iteration is lexicographically greater, but if there are
1841 * iterations that are equal to iterations of loop2, then return "def".
1842 * This is useful for ensuring that such statements are not reordered.
1843 * Some users, including the test_run target in test, expect
1844 * the statements at a given point to be run in the original order.
1845 * Passing the value "0" for "def" would allow such statements to be reordered
1846 * and would allow for the detection of more components.
1848 int cloog_loop_follows(CloogLoop *loop1, CloogLoop *loop2,
1849 int level, int scalar, int *scaldims, int nb_scattdims, int def)
1851 int dim1, dim2;
1853 dim1 = cloog_domain_dimension(loop1->domain);
1854 dim2 = cloog_domain_dimension(loop2->domain);
1855 while ((level <= dim1 && level <= dim2) ||
1856 level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1857 if (level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1858 int cmp = cloog_loop_constant_cmp(loop1, loop2, level, scaldims,
1859 nb_scattdims, scalar);
1860 if (cmp > 0)
1861 return 1;
1862 if (cmp < 0)
1863 return 0;
1864 scalar += scaldims[level + scalar - 1];
1865 } else {
1866 int follows = cloog_domain_follows(loop1->domain, loop2->domain,
1867 level);
1868 if (follows > 0)
1869 return 1;
1870 if (follows < 0)
1871 return 0;
1872 level++;
1876 return def;
1880 /* Structure for representing the nodes in the graph being traversed
1881 * using Tarjan's algorithm.
1882 * index represents the order in which nodes are visited.
1883 * min_index is the index of the root of a (sub)component.
1884 * on_stack indicates whether the node is currently on the stack.
1886 struct cloog_loop_sort_node {
1887 int index;
1888 int min_index;
1889 int on_stack;
1891 /* Structure for representing the graph being traversed
1892 * using Tarjan's algorithm.
1893 * len is the number of nodes
1894 * node is an array of nodes
1895 * stack contains the nodes on the path from the root to the current node
1896 * sp is the stack pointer
1897 * index is the index of the last node visited
1898 * order contains the elements of the components separated by -1
1899 * op represents the current position in order
1901 struct cloog_loop_sort {
1902 int len;
1903 struct cloog_loop_sort_node *node;
1904 int *stack;
1905 int sp;
1906 int index;
1907 int *order;
1908 int op;
1911 /* Allocate and initialize cloog_loop_sort structure.
1913 static struct cloog_loop_sort *cloog_loop_sort_alloc(int len)
1915 struct cloog_loop_sort *s;
1916 int i;
1918 s = (struct cloog_loop_sort *)malloc(sizeof(struct cloog_loop_sort));
1919 assert(s);
1920 s->len = len;
1921 s->node = (struct cloog_loop_sort_node *)
1922 malloc(len * sizeof(struct cloog_loop_sort_node));
1923 assert(s->node);
1924 for (i = 0; i < len; ++i)
1925 s->node[i].index = -1;
1926 s->stack = (int *)malloc(len * sizeof(int));
1927 assert(s->stack);
1928 s->order = (int *)malloc(2 * len * sizeof(int));
1929 assert(s->order);
1931 s->sp = 0;
1932 s->index = 0;
1933 s->op = 0;
1935 return s;
1938 /* Free cloog_loop_sort structure.
1940 static void cloog_loop_sort_free(struct cloog_loop_sort *s)
1942 free(s->node);
1943 free(s->stack);
1944 free(s->order);
1945 free(s);
1949 /* Check whether for any fixed iteration of the outer loops,
1950 * there is an iteration of loop1 that is lexicographically greater
1951 * than an iteration of loop2, where the iteration domains are
1952 * available in the inner loops of the arguments.
1954 * By using this functions to detect components, we ensure that
1955 * two CloogLoops appear in the same component if some iterations of
1956 * each loop should be executed before some iterations of the other loop.
1957 * Since we also want two CloogLoops that have exactly the same
1958 * iteration domain at the current level to be placed in the same component,
1959 * we first check if these domains are indeed the same.
1961 static int inner_loop_follows(CloogLoop *loop1, CloogLoop *loop2,
1962 int level, int scalar, int *scaldims, int nb_scattdims, int def)
1964 int f;
1966 f = cloog_domain_lazy_equal(loop1->domain, loop2->domain);
1967 if (!f)
1968 f = cloog_loop_follows(loop1->inner, loop2->inner,
1969 level, scalar, scaldims, nb_scattdims, def);
1971 return f;
1975 /* Perform Tarjan's algorithm for computing the strongly connected components
1976 * in the graph with the individual CloogLoops as vertices.
1977 * Two CloopLoops appear in the same component if they both (indirectly)
1978 * "follow" each other, where the following relation is determined
1979 * by the follows function.
1981 static void cloog_loop_components_tarjan(struct cloog_loop_sort *s,
1982 CloogLoop **loop_array, int i, int level, int scalar, int *scaldims,
1983 int nb_scattdims,
1984 int (*follows)(CloogLoop *loop1, CloogLoop *loop2,
1985 int level, int scalar, int *scaldims, int nb_scattdims, int def))
1987 int j;
1989 s->node[i].index = s->index;
1990 s->node[i].min_index = s->index;
1991 s->node[i].on_stack = 1;
1992 s->index++;
1993 s->stack[s->sp++] = i;
1995 for (j = s->len - 1; j >= 0; --j) {
1996 int f;
1998 if (j == i)
1999 continue;
2000 if (s->node[j].index >= 0 &&
2001 (!s->node[j].on_stack ||
2002 s->node[j].index > s->node[i].min_index))
2003 continue;
2005 f = follows(loop_array[i], loop_array[j],
2006 level, scalar, scaldims, nb_scattdims, i > j);
2007 if (!f)
2008 continue;
2010 if (s->node[j].index < 0) {
2011 cloog_loop_components_tarjan(s, loop_array, j, level, scalar,
2012 scaldims, nb_scattdims, follows);
2013 if (s->node[j].min_index < s->node[i].min_index)
2014 s->node[i].min_index = s->node[j].min_index;
2015 } else if (s->node[j].index < s->node[i].min_index)
2016 s->node[i].min_index = s->node[j].index;
2019 if (s->node[i].index != s->node[i].min_index)
2020 return;
2022 do {
2023 j = s->stack[--s->sp];
2024 s->node[j].on_stack = 0;
2025 s->order[s->op++] = j;
2026 } while (j != i);
2027 s->order[s->op++] = -1;
2031 static int qsort_index_cmp(const void *p1, const void *p2)
2033 return *(int *)p1 - *(int *)p2;
2036 /* Sort the elements of the component starting at list.
2037 * The list is terminated by a -1.
2039 static void sort_component(int *list)
2041 int len;
2043 for (len = 0; list[len] != -1; ++len)
2046 qsort(list, len, sizeof(int), qsort_index_cmp);
2049 /* Given an array of indices "list" into the "loop_array" array,
2050 * terminated by -1, construct a linked list of the corresponding
2051 * entries and put the result in *res.
2052 * The value returned is the number of CloogLoops in the (linked) list
2054 static int extract_component(CloogLoop **loop_array, int *list, CloogLoop **res)
2056 int i = 0;
2058 sort_component(list);
2059 while (list[i] != -1) {
2060 *res = loop_array[list[i]];
2061 res = &(*res)->next;
2062 ++i;
2064 *res = NULL;
2066 return i;
2071 * Call cloog_loop_generate_scalar or cloog_loop_generate_general
2072 * on each of the strongly connected components in the list of CloogLoops
2073 * pointed to by "loop".
2075 * We use Tarjan's algorithm to find the strongly connected components.
2076 * Note that this algorithm also topologically sorts the components.
2078 * The components are treated separately to avoid spurious separations.
2079 * The concatentation of the results may contain successive loops
2080 * with the same bounds, so we try to combine such loops.
2082 CloogLoop *cloog_loop_generate_components(CloogLoop *loop,
2083 int level, int scalar, int *scaldims, int nb_scattdims,
2084 CloogOptions *options)
2086 int i, nb_loops;
2087 CloogLoop *tmp;
2088 CloogLoop *res, **res_next;
2089 CloogLoop **loop_array;
2090 struct cloog_loop_sort *s;
2092 if (level == 0 || !loop->next)
2093 return cloog_loop_generate_general(loop, level, scalar,
2094 scaldims, nb_scattdims, options);
2096 nb_loops = cloog_loop_count(loop);
2098 loop_array = (CloogLoop **)malloc(nb_loops * sizeof(CloogLoop *));
2099 assert(loop_array);
2101 for (i = 0, tmp = loop; i < nb_loops; i++, tmp = tmp->next)
2102 loop_array[i] = tmp;
2104 s = cloog_loop_sort_alloc(nb_loops);
2105 for (i = nb_loops - 1; i >= 0; --i) {
2106 if (s->node[i].index >= 0)
2107 continue;
2108 cloog_loop_components_tarjan(s, loop_array, i, level, scalar, scaldims,
2109 nb_scattdims, &inner_loop_follows);
2112 i = 0;
2113 res = NULL;
2114 res_next = &res;
2115 while (nb_loops) {
2116 int n = extract_component(loop_array, &s->order[i], &tmp);
2117 i += n + 1;
2118 nb_loops -= n;
2119 *res_next = cloog_loop_generate_general(tmp, level, scalar,
2120 scaldims, nb_scattdims, options);
2121 while (*res_next)
2122 res_next = &(*res_next)->next;
2125 cloog_loop_sort_free(s);
2127 free(loop_array);
2129 res = cloog_loop_combine(res);
2131 return res;
2135 /* For each loop in the list "loop", decompose the list of
2136 * inner loops into strongly connected components and put
2137 * the components into separate loops at the top level.
2139 CloogLoop *cloog_loop_decompose_inner(CloogLoop *loop,
2140 int level, int scalar, int *scaldims, int nb_scattdims)
2142 CloogLoop *l, *tmp;
2143 CloogLoop **loop_array;
2144 int i, n_loops, max_loops = 0;
2145 struct cloog_loop_sort *s;
2147 for (l = loop; l; l = l->next) {
2148 n_loops = cloog_loop_count(l->inner);
2149 if (max_loops < n_loops)
2150 max_loops = n_loops;
2153 if (max_loops <= 1)
2154 return loop;
2156 loop_array = (CloogLoop **)malloc(max_loops * sizeof(CloogLoop *));
2157 assert(loop_array);
2159 for (l = loop; l; l = l->next) {
2160 int n;
2162 for (i = 0, tmp = l->inner; tmp; i++, tmp = tmp->next)
2163 loop_array[i] = tmp;
2164 n_loops = i;
2165 if (n_loops <= 1)
2166 continue;
2168 s = cloog_loop_sort_alloc(n_loops);
2169 for (i = n_loops - 1; i >= 0; --i) {
2170 if (s->node[i].index >= 0)
2171 continue;
2172 cloog_loop_components_tarjan(s, loop_array, i, level, scalar,
2173 scaldims, nb_scattdims, &cloog_loop_follows);
2176 n = extract_component(loop_array, s->order, &l->inner);
2177 n_loops -= n;
2178 i = n + 1;
2179 while (n_loops) {
2180 CloogLoop *inner;
2182 n = extract_component(loop_array, &s->order[i], &inner);
2183 n_loops -= n;
2184 i += n + 1;
2185 tmp = cloog_loop_alloc(l->state, cloog_domain_copy(l->domain),
2186 l->otl, l->stride, l->block, inner, l->next);
2187 l->next = tmp;
2188 l = tmp;
2191 cloog_loop_sort_free(s);
2194 free(loop_array);
2196 return loop;
2200 CloogLoop *cloog_loop_generate_restricted(CloogLoop *loop,
2201 int level, int scalar, int *scaldims, int nb_scattdims,
2202 CloogOptions *options)
2204 /* To save both time and memory, we switch here depending on whether the
2205 * current dimension is scalar (simplified processing) or not (general
2206 * processing).
2208 if (level_is_constant(level, scalar, scaldims, nb_scattdims))
2209 return cloog_loop_generate_scalar(loop, level, scalar,
2210 scaldims, nb_scattdims, options);
2212 * 2. Compute the projection of each polyhedron onto the outermost
2213 * loop variable and the parameters.
2215 loop = cloog_loop_project_all(loop, level);
2217 return cloog_loop_generate_components(loop, level, scalar, scaldims,
2218 nb_scattdims, options);
2222 CloogLoop *cloog_loop_generate_restricted_or_stop(CloogLoop *loop,
2223 CloogDomain *context,
2224 int level, int scalar, int *scaldims, int nb_scattdims,
2225 CloogOptions *options)
2227 /* If the user asked to stop code generation at this level, let's stop. */
2228 if ((options->stop >= 0) && (level+scalar >= options->stop+1))
2229 return cloog_loop_stop(loop,context) ;
2231 return cloog_loop_generate_restricted(loop, level, scalar, scaldims,
2232 nb_scattdims, options);
2237 * cloog_loop_generate function:
2238 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
2239 * Quillere algorithm for polyhedron scanning from step 1 to 2.
2240 * (see the Quillere paper).
2241 * - loop is the loop for which we have to generate a scanning code,
2242 * - context is the context of the current loop (constraints on parameter and/or
2243 * on outer loop counters),
2244 * - level is the current non-scalar dimension,
2245 * - scalar is the current scalar dimension,
2246 * - scaldims is the boolean array saying whether a dimension is scalar or not,
2247 * - nb_scattdims is the size of the scaldims array,
2248 * - options are the general code generation options.
2250 * - October 26th 2001: first version.
2251 * - July 3rd->11th 2003: memory leaks hunt and correction.
2252 * - June 15th 2005: a memory leak fixed (loop was not entirely freed when
2253 * the result of cloog_loop_restrict was NULL).
2254 * - June 22nd 2005: Adaptation for GMP.
2255 * - September 2nd 2005: The function have been cutted out in two pieces:
2256 * cloog_loop_generate and this one, in order to handle
2257 * the scalar dimension case more efficiently with
2258 * cloog_loop_generate_scalar.
2259 * - November 15th 2005: (debug) Condition for stop option no more take care of
2260 * further scalar dimensions.
2262 CloogLoop *cloog_loop_generate(CloogLoop *loop, CloogDomain *context,
2263 int level, int scalar, int *scaldims, int nb_scattdims,
2264 CloogOptions *options)
2266 /* 1. Replace each polyhedron by its intersection with the context.
2268 loop = cloog_loop_restrict_all(loop, context);
2269 if (!loop)
2270 return NULL;
2272 return cloog_loop_generate_restricted_or_stop(loop, context,
2273 level, scalar, scaldims, nb_scattdims, options);
2278 * Internal function for simplifying a single loop in a list of loops.
2279 * See cloog_loop_simplify.
2281 static CloogLoop *loop_simplify(CloogLoop *loop, CloogDomain *context,
2282 int level)
2284 int domain_dim;
2285 CloogBlock * new_block ;
2286 CloogLoop *simplified, *inner;
2287 CloogDomain * domain, * simp, * inter, * extended_context ;
2289 if (!cloog_domain_isconvex(loop->domain))
2290 loop->domain = cloog_domain_simplify_union(loop->domain);
2292 domain = loop->domain ;
2294 domain_dim = cloog_domain_dimension(domain);
2295 extended_context = cloog_domain_extend(context, domain_dim);
2296 inter = cloog_domain_intersection(domain,extended_context) ;
2297 simp = cloog_domain_simplify(inter,extended_context) ;
2298 cloog_domain_free(extended_context) ;
2300 /* If the constraint system is never true, go to the next one. */
2301 if (cloog_domain_never_integral(simp)) {
2302 cloog_loop_free(loop->inner);
2303 cloog_domain_free(inter);
2304 cloog_domain_free(simp);
2305 return NULL;
2308 inner = cloog_loop_simplify(loop->inner, inter, level+1);
2309 cloog_domain_free(inter) ;
2311 if ((inner == NULL) && (loop->block == NULL)) {
2312 cloog_domain_free(simp);
2313 return NULL;
2316 new_block = cloog_block_copy(loop->block) ;
2318 simplified = cloog_loop_alloc(loop->state, simp, loop->otl, loop->stride,
2319 new_block, inner, NULL);
2321 return(simplified) ;
2326 * cloog_loop_simplify function:
2327 * This function implements the part 6. of the Quillere algorithm, it
2328 * recursively simplifies each loop in the context of the preceding loop domain.
2329 * It returns a pointer to the simplified loop list.
2330 * The cloog_domain_simplify (DomainSimplify) behaviour is really bad with
2331 * polyhedra union and some really awful sidesteppings were written, I plan
2332 * to solve that...
2333 * - October 31th 2001: first version.
2334 * - July 3rd->11th 2003: memory leaks hunt and correction.
2335 * - April 16th 2005: a memory leak fixed (extended_context was not freed).
2336 * - June 15th 2005: a memory leak fixed (loop was not conveniently freed
2337 * when the constraint system is never true).
2338 * - October 27th 2005: - this function called before cloog_loop_fast_simplify
2339 * is now the official cloog_loop_simplify function in
2340 * replacement of a slower and more complex one (after
2341 * deep changes in the pretty printer).
2342 * - we use cloog_loop_disjoint to fix the problem when
2343 * simplifying gives a union of polyhedra (before, it
2344 * was under the responsibility of the pretty printer).
2346 CloogLoop *cloog_loop_simplify(CloogLoop *loop, CloogDomain *context, int level)
2348 CloogLoop *now;
2349 CloogLoop *res = NULL;
2350 CloogLoop **next = &res;
2352 for (now = loop; now; now = now->next) {
2353 *next = loop_simplify(now, context, level);
2355 now->inner = NULL; /* For loop integrity. */
2356 cloog_domain_free(now->domain);
2357 now->domain = NULL;
2359 if (*next)
2360 next = &(*next)->next;
2362 cloog_loop_free(loop);
2364 /* Examples like test/iftest2.cloog give unions of polyhedra after
2365 * simplifying, thus we have to make them disjoint. Another good reason to
2366 * put the simplifying step in the Quillere backtrack.
2368 res = cloog_loop_disjoint(res);
2370 return res;
2375 * cloog_loop_scatter function:
2376 * This function add the scattering (scheduling) informations in a loop.
2378 void cloog_loop_scatter(CloogLoop * loop, CloogScattering *scatt)
2380 loop->domain = cloog_domain_scatter(loop->domain, scatt);