cloog_loop_remove_empty_domain_loops: properly terminate linked list
[cloog.git] / source / loop.c
blobf8d21afefeb6482e1747d3dbca252b2634f2826a
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 cloog_loop_leak_up(state);
277 /* Memory allocation and information reading for the first domain: */
278 loop = (CloogLoop *)malloc(sizeof(CloogLoop)) ;
279 if (loop == NULL)
280 cloog_die("memory overflow.\n");
281 /* domain. */
282 loop->state = state;
283 loop->domain = domain;
284 if (loop->domain != NULL)
285 nb_iterators = cloog_domain_dimension(loop->domain);
286 else
287 nb_iterators = 0 ;
288 loop->otl = 0;
289 /* assume no stride */
290 loop->stride = NULL;
291 /* included statement block. */
292 statement = cloog_statement_alloc(state, number + 1);
293 loop->block = cloog_block_alloc(statement, 0, NULL, nb_iterators);
294 loop->usr = NULL;
295 /* inner is NULL at beginning. */
296 loop->inner = NULL ;
297 /* next element. */
298 loop->next = NULL ;
300 return loop ;
305 * cloog_loop_read function:
306 * This function reads loop data from a file (foo, possibly stdin) and
307 * returns a pointer to a CloogLoop structure containing the read information.
308 * This function can be used only for input file reading, when one loop is
309 * associated with one statement.
310 * - number is the statement block number carried by the loop (-1 if none).
311 * - nb_parameters is the number of parameters.
313 * - September 9th 2002: first version.
314 * - April 16th 2005: adaptation to new CloogStatement struct (with number).
315 * - June 11th 2005: adaptation to new CloogBlock structure.
316 * - June 22nd 2005: Adaptation for GMP.
318 CloogLoop *cloog_loop_read(CloogState *state,
319 FILE *foo, int number, int nb_parameters)
321 int op1, op2, op3;
322 char s[MAX_STRING];
323 CloogDomain *domain;
325 domain = cloog_domain_union_read(state, foo, nb_parameters);
327 /* To read that stupid "0 0 0" line. */
328 while (fgets(s,MAX_STRING,foo) == 0) ;
329 while ((*s=='#' || *s=='\n') || (sscanf(s," %d %d %d",&op1,&op2,&op3)<3))
330 fgets(s,MAX_STRING,foo) ;
332 return cloog_loop_from_domain(state, domain, number);
336 /******************************************************************************
337 * Processing functions *
338 ******************************************************************************/
342 * cloog_loop_malloc function:
343 * This function allocates the memory space for a CloogLoop structure and
344 * sets its fields with default values. Then it returns a pointer to the
345 * allocated space.
346 * - November 21th 2005: first version.
348 CloogLoop *cloog_loop_malloc(CloogState *state)
349 { CloogLoop * loop ;
351 /* Memory allocation for the CloogLoop structure. */
352 loop = (CloogLoop *)malloc(sizeof(CloogLoop)) ;
353 if (loop == NULL)
354 cloog_die("memory overflow.\n");
355 cloog_loop_leak_up(state);
358 /* We set the various fields with default values. */
359 loop->state = state;
360 loop->domain = NULL ;
361 loop->block = NULL ;
362 loop->usr = NULL;
363 loop->inner = NULL ;
364 loop->next = NULL ;
365 loop->otl = 0;
366 loop->stride = NULL;
368 return loop ;
373 * cloog_loop_alloc function:
374 * This function allocates the memory space for a CloogLoop structure and
375 * sets its fields with those given as input. Then it returns a pointer to the
376 * allocated space.
377 * - October 27th 2001: first version.
378 * - June 22nd 2005: Adaptation for GMP.
379 * - November 21th 2005: use of cloog_loop_malloc.
381 CloogLoop *cloog_loop_alloc(CloogState *state,
382 CloogDomain *domain, int otl, CloogStride *stride,
383 CloogBlock *block, CloogLoop *inner, CloogLoop *next)
384 { CloogLoop * loop ;
386 loop = cloog_loop_malloc(state);
388 loop->domain = domain ;
389 loop->block = block ;
390 loop->inner = inner ;
391 loop->next = next ;
392 loop->otl = otl;
393 loop->stride = cloog_stride_copy(stride);
395 return(loop) ;
400 * cloog_loop_add function:
401 * This function adds a CloogLoop structure (loop) at a given place (now) of a
402 * NULL terminated list of CloogLoop structures. The beginning of this list
403 * is (start). This function updates (now) to (loop), and updates (start) if the
404 * added element is the first one -that is when (start) is NULL-.
405 * - October 28th 2001: first version.
407 void cloog_loop_add(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
408 { if (*start == NULL)
409 { *start = loop ;
410 *now = *start ;
412 else
413 { (*now)->next = loop ;
414 *now = (*now)->next ;
420 * cloog_loop_add function:
421 * This function adds a CloogLoop structure (loop) at a given place (now) of a
422 * NULL terminated list of CloogLoop structures. The beginning of this list
423 * is (start). This function updates (now) to the end of the loop list (loop),
424 * and updates (start) if the added element is the first one -that is when
425 * (start) is NULL-.
426 * - September 9th 2005: first version.
428 void cloog_loop_add_list(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
429 { if (*start == NULL)
430 { *start = loop ;
431 *now = *start ;
433 else
434 { (*now)->next = loop ;
435 *now = (*now)->next ;
438 while ((*now)->next != NULL)
439 *now = (*now)->next ;
444 * cloog_loop_copy function:
445 * This function returns a copy of the CloogLoop structure given as input. In
446 * fact, there is just new allocations for the CloogLoop structures, but their
447 * contents are the same.
448 * - October 28th 2001: first version.
449 * - July 3rd->11th 2003: memory leaks hunt and correction.
451 CloogLoop * cloog_loop_copy(CloogLoop * source)
452 { CloogLoop * loop ;
453 CloogBlock * block ;
454 CloogDomain * domain ;
456 loop = NULL ;
457 if (source != NULL)
458 { domain = cloog_domain_copy(source->domain) ;
459 block = cloog_block_copy(source->block) ;
460 loop = cloog_loop_alloc(source->state, domain, source->otl,
461 source->stride, block, NULL, NULL);
462 loop->usr = source->usr;
463 loop->inner = cloog_loop_copy(source->inner) ;
464 loop->next = cloog_loop_copy(source->next) ;
466 return(loop) ;
471 * cloog_loop_add_disjoint function:
472 * This function adds some CloogLoop structures at a given place (now) of a
473 * NULL terminated list of CloogLoop structures. The beginning of this list
474 * is (start). (loop) can be an union of polyhedra, this function separates the
475 * union into a list of *disjoint* polyhedra then adds the list. This function
476 * updates (now) to the end of the list and updates (start) if first added
477 * element is the first of the principal list -that is when (start) is NULL-.
478 * (loop) can be freed by this function, basically when its domain is actually
479 * a union of polyhedra, but don't worry, all the useful data are now stored
480 * inside the list (start). We do not use PolyLib's Domain_Disjoint function,
481 * since the number of union components is often higher (thus code size too).
482 * - October 28th 2001: first version.
483 * - November 14th 2001: bug correction (this one was hard to find !).
484 * - July 3rd->11th 2003: memory leaks hunt and correction.
485 * - June 22nd 2005: Adaptation for GMP.
486 * - October 27th 2005: (debug) included blocks were not copied for new loops.
488 void cloog_loop_add_disjoint(start, now, loop)
489 CloogLoop ** start, ** now, * loop ;
491 CloogLoop * sep, * inner ;
492 CloogDomain *domain, *seen, *temp, *rest;
493 CloogBlock * block ;
495 if (cloog_domain_isconvex(loop->domain))
496 cloog_loop_add(start,now,loop) ;
497 else {
498 domain = cloog_domain_simplify_union(loop->domain);
499 loop->domain = NULL ;
501 /* We separate the first element of the rest of the union. */
502 domain = cloog_domain_cut_first(domain, &rest);
504 /* This first element is the first of the list of disjoint polyhedra. */
505 sep = cloog_loop_alloc(loop->state, domain, 0, NULL,
506 loop->block, loop->inner, NULL);
507 cloog_loop_add(start,now,sep) ;
509 seen = cloog_domain_copy(domain);
510 while (!cloog_domain_isempty(domain = rest)) {
511 temp = cloog_domain_cut_first(domain, &rest);
512 domain = cloog_domain_difference(temp, seen);
513 cloog_domain_free(temp);
515 if (cloog_domain_isempty(domain)) {
516 cloog_domain_free(domain);
517 continue;
520 /* Each new loop will have its own life, for instance we can free its
521 * inner loop and included block. Then each one must have its own copy
522 * of both 'inner' and 'block'.
524 inner = cloog_loop_copy(loop->inner) ;
525 block = cloog_block_copy(loop->block) ;
527 sep = cloog_loop_alloc(loop->state, cloog_domain_copy(domain),
528 0, NULL, block, inner, NULL);
529 /* domain can be an union too. If so: recursion. */
530 if (cloog_domain_isconvex(domain))
531 cloog_loop_add(start,now,sep) ;
532 else
533 cloog_loop_add_disjoint(start,now,sep) ;
535 if (cloog_domain_isempty(rest)) {
536 cloog_domain_free(domain);
537 break;
540 seen = cloog_domain_union(seen, domain);
542 cloog_domain_free(rest);
543 cloog_domain_free(seen);
544 cloog_loop_free_parts(loop,0,0,0,0) ;
550 * cloog_loop_disjoint function:
551 * This function returns a list of loops such that each loop with non-convex
552 * domain in the input list (loop) is separated into several loops where the
553 * domains are the components of the union of *disjoint* polyhedra equivalent
554 * to the original non-convex domain. See cloog_loop_add_disjoint comments
555 * for more details.
556 * - September 16th 2005: first version.
558 CloogLoop * cloog_loop_disjoint(CloogLoop * loop)
559 { CloogLoop *res=NULL, * now=NULL, * next ;
561 /* Because this is often the case, don't waste time ! */
562 if (loop && !loop->next && cloog_domain_isconvex(loop->domain))
563 return loop ;
565 while (loop != NULL)
566 { next = loop->next ;
567 loop->next = NULL ;
568 cloog_loop_add_disjoint(&res,&now,loop) ;
569 loop = next ;
572 return res ;
577 * cloog_loop_restrict function:
578 * This function returns the (loop) in the context of (context): it makes the
579 * intersection between the (loop) domain and the (context), then it returns
580 * a pointer to a new loop, with this intersection as domain.
582 * - October 27th 2001: first version.
583 * - June 15th 2005: a memory leak fixed (domain was not freed when empty).
584 * - June 22nd 2005: Adaptation for GMP.
586 CloogLoop *cloog_loop_restrict(CloogLoop *loop, CloogDomain *context)
587 { int new_dimension ;
588 CloogDomain * domain, * extended_context, * new_domain ;
589 CloogLoop * new_loop ;
591 domain = loop->domain ;
592 if (cloog_domain_dimension(domain) > cloog_domain_dimension(context))
594 new_dimension = cloog_domain_dimension(domain);
595 extended_context = cloog_domain_extend(context, new_dimension);
596 new_domain = cloog_domain_intersection(extended_context,loop->domain) ;
597 cloog_domain_free(extended_context) ;
599 else
600 new_domain = cloog_domain_intersection(context,loop->domain) ;
602 if (cloog_domain_isempty(new_domain))
603 { cloog_domain_free(new_domain) ;
604 return(NULL) ;
606 else {
607 new_loop = cloog_loop_alloc(loop->state, new_domain,
608 0, NULL, loop->block, loop->inner, NULL);
609 return(new_loop) ;
615 * Call cloog_loop_restrict on each loop in the list "loop" and return
616 * the concatenated result.
618 CloogLoop *cloog_loop_restrict_all(CloogLoop *loop, CloogDomain *context)
620 CloogLoop *next;
621 CloogLoop *res = NULL;
622 CloogLoop **res_next = &res;
624 for (; loop; loop = next) {
625 next = loop->next;
627 *res_next = cloog_loop_restrict(loop, context);
628 if (*res_next) {
629 res_next = &(*res_next)->next;
630 cloog_loop_free_parts(loop, 1, 0, 0, 0);
631 } else {
632 loop->next = NULL;
633 cloog_loop_free(loop);
637 return res;
640 CloogLoop *cloog_loop_restrict_inner(CloogLoop *loop)
642 CloogLoop *l;
644 for (l = loop; l; l = l->next)
645 l->inner = cloog_loop_restrict_all(l->inner, l->domain);
647 return loop;
651 * cloog_loop_project function:
652 * This function returns the projection of (loop) on the (level) first
653 * dimensions (outer loops). It makes the projection of the (loop) domain,
654 * then it returns a pointer to a new loop, with this projection as domain.
656 * - October 27th 2001: first version.
657 * - July 3rd->11th 2003: memory leaks hunt and correction.
658 * - June 22nd 2005: Adaptation for GMP.
660 CloogLoop * cloog_loop_project(CloogLoop * loop, int level)
662 CloogDomain * new_domain ;
663 CloogLoop * new_loop, * copy ;
665 copy = cloog_loop_alloc(loop->state, loop->domain, loop->otl, loop->stride,
666 loop->block, loop->inner, NULL);
668 if (cloog_domain_dimension(loop->domain) == level)
669 new_domain = cloog_domain_copy(loop->domain) ;
670 else
671 new_domain = cloog_domain_project(loop->domain, level);
673 new_loop = cloog_loop_alloc(loop->state, new_domain, 0, NULL,
674 NULL, copy, NULL);
676 return(new_loop) ;
681 * Call cloog_loop_project on each loop in the list "loop" and return
682 * the concatenated result.
684 CloogLoop *cloog_loop_project_all(CloogLoop *loop, int level)
686 CloogLoop *next;
687 CloogLoop *res = NULL;
688 CloogLoop **res_next = &res;
690 for (; loop; loop = next) {
691 next = loop->next;
693 *res_next = cloog_loop_project(loop, level);
694 res_next = &(*res_next)->next;
695 cloog_loop_free_parts(loop, 0, 0, 0, 0);
698 return res;
703 * cloog_loop_concat function:
704 * This function returns a pointer to the concatenation of the
705 * CloogLoop lists given as input.
706 * - October 28th 2001: first version.
708 CloogLoop * cloog_loop_concat(CloogLoop * a, CloogLoop * b)
709 { CloogLoop * loop, * temp ;
711 loop = a ;
712 temp = loop ;
713 if (loop != NULL)
714 { while (temp->next != NULL)
715 temp = temp->next ;
716 temp->next = b ;
718 else
719 loop = b ;
721 return(loop) ;
726 * cloog_loop_combine:
727 * Combine consecutive loops with identical domains into
728 * a single loop with the concatenation of their inner loops
729 * as inner loop.
731 CloogLoop *cloog_loop_combine(CloogLoop *loop)
733 CloogLoop *first, *second;
735 for (first = loop; first; first = first->next) {
736 while (first->next) {
737 if (!cloog_domain_lazy_equal(first->domain, first->next->domain))
738 break;
739 second = first->next;
740 first->inner = cloog_loop_concat(first->inner, second->inner);
741 first->next = second->next;
742 cloog_loop_free_parts(second, 1, 0, 0, 0);
746 return loop;
750 * Remove loops from list that have an empty domain.
752 CloogLoop *cloog_loop_remove_empty_domain_loops(CloogLoop *loop)
754 CloogLoop *l, *res, *next, **res_next;
756 res = NULL;
757 res_next = &res;
758 for (l = loop; l; l = next) {
759 next = l->next;
760 if (cloog_domain_isempty(l->domain))
761 cloog_loop_free_parts(l, 1, 1, 1, 0);
762 else {
763 *res_next = l;
764 res_next = &(*res_next)->next;
767 *res_next = NULL;
769 return res;
772 CloogLoop *cloog_loop_decompose_inner(CloogLoop *loop,
773 int level, int scalar, int *scaldims, int nb_scattdims);
775 /* For each loop with only one inner loop, replace the domain
776 * of the loop with the projection of the domain of the inner
777 * loop. To increase the number of loops with a single inner
778 * we first decompose the inner loops into strongly connected
779 * components.
781 CloogLoop *cloog_loop_specialize(CloogLoop *loop,
782 int level, int scalar, int *scaldims, int nb_scattdims)
784 int dim;
785 CloogLoop *l;
787 loop = cloog_loop_decompose_inner(loop, level, scalar,
788 scaldims, nb_scattdims);
790 for (l = loop; l; l = l->next) {
791 if (l->inner->next)
792 continue;
793 if (!cloog_domain_isconvex(l->inner->domain))
794 continue;
796 dim = cloog_domain_dimension(l->domain);
797 cloog_domain_free(l->domain);
798 l->domain = cloog_domain_project(l->inner->domain, dim);
801 return cloog_loop_remove_empty_domain_loops(loop);
805 * cloog_loop_separate function:
806 * This function implements the Quillere algorithm for separation of multiple
807 * loops: for a given set of polyhedra (loop), it computes a set of disjoint
808 * polyhedra such that the unions of these sets are equal, and returns this set.
809 * - October 28th 2001: first version.
810 * - November 14th 2001: elimination of some unused blocks.
811 * - August 13th 2002: (debug) in the case of union of polyhedra for one
812 * loop, redundant constraints are fired.
813 * - July 3rd->11th 2003: memory leaks hunt and correction.
814 * - June 22nd 2005: Adaptation for GMP.
815 * - October 16th 2005: Removal of the non-shared constraint elimination when
816 * there is only one loop in the list (seems to work
817 * without now, DomainSimplify may have been improved).
818 * The problem was visible with test/iftest2.cloog.
820 CloogLoop * cloog_loop_separate(CloogLoop * loop)
821 { int lazy_equal=0, disjoint = 0;
822 CloogLoop * new_loop, * new_inner, * res, * now, * temp, * Q,
823 * inner, * old /*, * previous, * next*/ ;
824 CloogDomain *UQ, *domain;
826 if (loop == NULL)
827 return NULL ;
829 loop = cloog_loop_combine(loop);
831 if (loop->next == NULL)
832 return cloog_loop_disjoint(loop) ;
834 UQ = cloog_domain_copy(loop->domain) ;
835 domain = cloog_domain_copy(loop->domain) ;
836 res = cloog_loop_alloc(loop->state, domain, 0, NULL,
837 loop->block, loop->inner, NULL);
839 old = loop ;
840 while((loop = loop->next) != NULL)
841 { temp = NULL ;
843 /* For all Q, add Q-loop associated with the blocks of Q alone,
844 * and Q inter loop associated with the blocks of Q and loop.
846 for (Q = res; Q; Q = Q->next) {
847 /* Add (Q inter loop). */
848 if ((disjoint = cloog_domain_lazy_disjoint(Q->domain,loop->domain)))
849 domain = NULL ;
850 else
851 { if ((lazy_equal = cloog_domain_lazy_equal(Q->domain,loop->domain)))
852 domain = cloog_domain_copy(Q->domain) ;
853 else
854 domain = cloog_domain_intersection(Q->domain,loop->domain) ;
856 if (!cloog_domain_isempty(domain))
857 { new_inner = cloog_loop_concat(cloog_loop_copy(Q->inner),
858 cloog_loop_copy(loop->inner)) ;
859 new_loop = cloog_loop_alloc(loop->state, domain, 0, NULL,
860 NULL, new_inner, NULL);
861 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
863 else {
864 disjoint = 1;
865 cloog_domain_free(domain);
869 /* Add (Q - loop). */
870 if (disjoint)
871 domain = cloog_domain_copy(Q->domain) ;
872 else
873 { if (lazy_equal)
874 domain = cloog_domain_empty(Q->domain);
875 else
876 domain = cloog_domain_difference(Q->domain,loop->domain) ;
879 if (!cloog_domain_isempty(domain)) {
880 new_loop = cloog_loop_alloc(loop->state, domain, 0, NULL,
881 NULL, Q->inner, NULL);
882 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
884 else
885 { cloog_domain_free(domain) ;
886 /* If Q->inner is no more useful, we can free it. */
887 inner = Q->inner ;
888 Q->inner = NULL ;
889 cloog_loop_free(inner) ;
893 /* Add loop-UQ associated with the blocks of loop alone.*/
894 if (cloog_domain_lazy_disjoint(loop->domain,UQ))
895 domain = cloog_domain_copy(loop->domain) ;
896 else
897 { if (cloog_domain_lazy_equal(loop->domain,UQ))
898 domain = cloog_domain_empty(UQ);
899 else
900 domain = cloog_domain_difference(loop->domain,UQ) ;
903 if (!cloog_domain_isempty(domain)) {
904 new_loop = cloog_loop_alloc(loop->state, domain, 0, NULL,
905 NULL, loop->inner, NULL);
906 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
908 else
909 { cloog_domain_free(domain) ;
910 /* If loop->inner is no more useful, we can free it. */
911 cloog_loop_free(loop->inner) ;
914 loop->inner = NULL ;
916 if (loop->next != NULL)
917 UQ = cloog_domain_union(UQ, cloog_domain_copy(loop->domain));
918 else
919 cloog_domain_free(UQ);
921 cloog_loop_free_parts(res,1,0,0,1) ;
923 res = temp ;
925 cloog_loop_free_parts(old,1,0,0,1) ;
927 return(res) ;
931 static CloogDomain *bounding_domain(CloogDomain *dom, CloogOptions *options)
933 if (options->sh)
934 return cloog_domain_simple_convex(dom);
935 else
936 return cloog_domain_convex(dom);
941 * cloog_loop_merge function:
942 * This function is the 'soft' version of loop_separate if we are looking for
943 * a code much simpler (and less efficicient). This function returns the new
944 * CloogLoop list.
945 * - October 29th 2001: first version.
946 * - July 3rd->11th 2003: memory leaks hunt and correction.
947 * - June 22nd 2005: Adaptation for GMP.
949 CloogLoop *cloog_loop_merge(CloogLoop *loop, int level, CloogOptions *options)
951 CloogLoop *res, *new_inner, *old;
952 CloogDomain *new_domain, *temp;
954 if (loop == NULL)
955 return loop;
957 if (loop->next == NULL)
958 return cloog_loop_disjoint(loop);
960 old = loop;
961 temp = loop->domain;
962 loop->domain = NULL;
963 new_inner = loop->inner;
965 for (loop = loop->next; loop; loop = loop->next) {
966 temp = cloog_domain_union(temp, loop->domain);
967 loop->domain = NULL;
968 new_inner = cloog_loop_concat(new_inner, loop->inner);
971 new_domain = bounding_domain(temp, options);
973 if (level > 0 && !cloog_domain_is_bounded(new_domain, level) &&
974 cloog_domain_is_bounded(temp, level)) {
975 CloogDomain *splitter, *t2;
977 cloog_domain_free(new_domain);
978 splitter = cloog_domain_bound_splitter(temp, level);
980 res = NULL;
981 while (!cloog_domain_isconvex(splitter)) {
982 CloogDomain *first, *rest;
983 first = cloog_domain_cut_first(splitter, &rest);
984 splitter = rest;
985 t2 = cloog_domain_intersection(first, temp);
986 cloog_domain_free(first);
988 new_domain = bounding_domain(t2, options);
989 cloog_domain_free(t2);
991 if (cloog_domain_isempty(new_domain)) {
992 cloog_domain_free(new_domain);
993 continue;
995 res = cloog_loop_alloc(old->state, new_domain, 0, NULL,
996 NULL, cloog_loop_copy(new_inner), res);
999 t2 = cloog_domain_intersection(splitter, temp);
1000 cloog_domain_free(splitter);
1002 new_domain = bounding_domain(t2, options);
1003 cloog_domain_free(t2);
1005 if (cloog_domain_isempty(new_domain)) {
1006 cloog_domain_free(new_domain);
1007 cloog_loop_free(new_inner);
1008 } else
1009 res = cloog_loop_alloc(old->state, new_domain, 0, NULL,
1010 NULL, new_inner, res);
1011 } else {
1012 res = cloog_loop_alloc(old->state, new_domain, 0, NULL,
1013 NULL, new_inner, NULL);
1015 cloog_domain_free(temp);
1017 cloog_loop_free_parts(old, 0, 0, 0, 1);
1019 return res;
1023 static int cloog_loop_count(CloogLoop *loop)
1025 int nb_loops;
1027 for (nb_loops = 0; loop; loop = loop->next)
1028 nb_loops++;
1030 return nb_loops;
1035 * cloog_loop_sort function:
1036 * Adaptation from LoopGen 0.4 by F. Quillere. This function sorts a list of
1037 * parameterized disjoint polyhedra, in order to not have lexicographic order
1038 * violation (see Quillere paper).
1039 * - September 16th 2005: inclusion of cloog_loop_number (October 29th 2001).
1041 CloogLoop *cloog_loop_sort(CloogLoop *loop, int level)
1043 CloogLoop *res, *now, **loop_array;
1044 CloogDomain **doms;
1045 int i, nb_loops=0, * permut ;
1047 /* There is no need to sort the parameter domains. */
1048 if (!level)
1049 return loop;
1051 /* We will need to know how many loops are in the list. */
1052 nb_loops = cloog_loop_count(loop);
1054 /* If there is only one loop, it's the end. */
1055 if (nb_loops == 1)
1056 return(loop) ;
1058 /* We have to allocate memory for some useful components:
1059 * - loop_array: the loop array,
1060 * - doms: the array of domains to sort,
1061 * - permut: will give us a possible sort (maybe not the only one).
1063 loop_array = (CloogLoop **)malloc(nb_loops*sizeof(CloogLoop *)) ;
1064 doms = (CloogDomain **)malloc(nb_loops*sizeof(CloogDomain *));
1065 permut = (int *)malloc(nb_loops*sizeof(int)) ;
1067 /* We fill up the loop and domain arrays. */
1068 for (i=0;i<nb_loops;i++,loop=loop->next)
1069 { loop_array[i] = loop ;
1070 doms[i] = loop_array[i]->domain;
1073 /* cloog_domain_sort will fill up permut. */
1074 cloog_domain_sort(doms, nb_loops, level, permut);
1076 /* With permut and loop_array we build the sorted list. */
1077 res = NULL ;
1078 for (i=0;i<nb_loops;i++)
1079 { /* To avoid pointer looping... loop_add will rebuild the list. */
1080 loop_array[permut[i]-1]->next = NULL ;
1081 cloog_loop_add(&res,&now,loop_array[permut[i]-1]) ;
1084 free(permut) ;
1085 free(doms);
1086 free(loop_array) ;
1088 return res;
1093 * cloog_loop_nest function:
1094 * This function changes the loop list in such a way that we have no more than
1095 * one dimension added by level. It returns an equivalent loop list with
1096 * this property.
1097 * - October 29th 2001: first version.
1098 * - July 3rd->11th 2003: memory leaks hunt and correction.
1099 * - June 22nd 2005: Adaptation for GMP.
1100 * - November 21th 2005: (debug) now OK when cloog_loop_restrict returns NULL.
1102 CloogLoop *cloog_loop_nest(CloogLoop *loop, CloogDomain *context, int level)
1103 { int l ;
1104 CloogLoop * p, * temp, * res, * now, * next ;
1105 CloogDomain * new_domain ;
1107 loop = cloog_loop_disjoint(loop);
1109 res = NULL ;
1110 /* Each domain is changed by its intersection with the context. */
1111 while (loop != NULL)
1112 { p = cloog_loop_restrict(loop, context);
1113 next = loop->next ;
1115 if (p != NULL)
1116 { cloog_loop_free_parts(loop,1,0,0,0) ;
1118 temp = cloog_loop_alloc(p->state, p->domain, 0, NULL,
1119 p->block, p->inner, NULL);
1121 /* If the intersection dimension is too big, we make projections smaller
1122 * and smaller, and each projection includes the preceding projection
1123 * (thus, in the target list, dimensions are added one by one).
1125 if (cloog_domain_dimension(p->domain) >= level)
1126 for (l = cloog_domain_dimension(p->domain); l >= level; l--) {
1127 new_domain = cloog_domain_project(p->domain, l);
1128 temp = cloog_loop_alloc(p->state, new_domain, 0, NULL,
1129 NULL, temp, NULL);
1132 /* p is no more useful (but its content yes !). */
1133 cloog_loop_free_parts(p,0,0,0,0) ;
1135 cloog_loop_add(&res,&now,temp) ;
1137 else
1138 cloog_loop_free_parts(loop,1,1,1,0) ;
1140 loop = next ;
1143 return(res) ;
1147 /* Check if the domains of the inner loops impose a stride constraint
1148 * on the given level.
1149 * The core of the search is implemented in cloog_domain_list_stride.
1150 * Here, we simply construct a list of domains to pass to this function
1151 * and if a stride is found, we adjust the lower bounds by calling
1152 * cloog_domain_stride_lower_bound.
1154 static int cloog_loop_variable_offset_stride(CloogLoop *loop, int level)
1156 CloogDomainList *list = NULL;
1157 CloogLoop *inner;
1158 CloogStride *stride;
1160 for (inner = loop->inner; inner; inner = inner->next) {
1161 CloogDomainList *entry = ALLOC(CloogDomainList);
1162 entry->domain = cloog_domain_copy(inner->domain);
1163 entry->next = list;
1164 list = entry;
1167 stride = cloog_domain_list_stride(list, level);
1169 cloog_domain_list_free(list);
1171 if (!stride)
1172 return 0;
1174 loop->stride = stride;
1175 loop->domain = cloog_domain_stride_lower_bound(loop->domain, level, stride);
1177 return 1;
1182 * cloog_loop_stride function:
1183 * This function will find the stride of a loop for the iterator at the column
1184 * number 'level' in the constraint matrix. It will update the lower bound of
1185 * the iterator accordingly. Basically, the function will try to find in the
1186 * inner loops a common condition on this iterator for the inner loop iterators
1187 * to be integral. For instance, let us consider a loop with the iterator i,
1188 * the iteration domain -4<=i<=n, and its two inner loops with the iterator j.
1189 * The first inner loop has the constraint 3j=i, and the second one has the
1190 * constraint 6j=i. Then the common constraint on i for j to be integral is
1191 * i%3=0, the stride for i is 3. Lastly, we have to find the new lower bound
1192 * for i: the first value satisfying the common constraint: -3. At the end, the
1193 * iteration domain for i is -3<=i<=n and the stride for i is 3.
1195 * The algorithm implemented in this function only allows for strides
1196 * on loops with a lower bound that has a constant remainder on division
1197 * by the stride. Before initiating this procedure, we first check
1198 * if we can find a stride with a lower bound with a variable offset in
1199 * cloog_loop_variable_offset_stride.
1201 * - loop is the loop including the iteration domain of the considered iterator,
1202 * - level is the column number of the iterator in the matrix of contraints.
1204 * - June 29th 2003: first version (work in progress since June 26th 2003).
1205 * - July 14th 2003: simpler version.
1206 * - June 22nd 2005: Adaptation for GMP (from S. Verdoolaege's 0.12.1 version).
1208 void cloog_loop_stride(CloogLoop * loop, int level)
1209 { int first_search ;
1210 cloog_int_t stride, ref_offset, offset, potential;
1211 CloogLoop * inner ;
1213 if (!cloog_domain_can_stride(loop->domain, level))
1214 return;
1216 if (cloog_loop_variable_offset_stride(loop, level))
1217 return;
1219 cloog_int_init(stride);
1220 cloog_int_init(ref_offset);
1221 cloog_int_init(offset);
1222 cloog_int_init(potential);
1224 cloog_int_set_si(ref_offset, 0);
1225 cloog_int_set_si(offset, 0);
1227 /* Default stride. */
1228 cloog_int_set_si(stride, 1);
1229 first_search = 1 ;
1230 inner = loop->inner ;
1232 while (inner != NULL)
1233 { /* If the minimun stride has not been found yet, find the stride. */
1234 if ((first_search) || (!cloog_int_is_one(stride)))
1236 cloog_domain_stride(inner->domain, level, &potential, &offset);
1237 if (!cloog_int_is_one(potential) && (!first_search))
1238 { /* Offsets must be the same for common stride. */
1239 cloog_int_gcd(stride, potential, stride);
1240 if (!cloog_int_is_zero(stride)) {
1241 cloog_int_fdiv_r(offset, offset, stride);
1242 cloog_int_fdiv_r(ref_offset, ref_offset, stride);
1244 if (cloog_int_ne(offset,ref_offset))
1245 cloog_int_set_si(stride, 1);
1247 else {
1248 cloog_int_set(stride, potential);
1249 cloog_int_set(ref_offset, offset);
1252 first_search = 0 ;
1255 inner = inner->next ;
1258 if (cloog_int_is_zero(stride))
1259 cloog_int_set_si(stride, 1);
1261 /* Update the values if necessary. */
1262 if (!cloog_int_is_one(stride))
1263 { /* Update the stride value. */
1264 if (!cloog_int_is_zero(offset))
1265 cloog_int_sub(offset, stride, offset);
1266 loop->stride = cloog_stride_alloc(stride, offset);
1267 loop->domain = cloog_domain_stride_lower_bound(loop->domain, level,
1268 loop->stride);
1271 cloog_int_clear(stride);
1272 cloog_int_clear(ref_offset);
1273 cloog_int_clear(offset);
1274 cloog_int_clear(potential);
1278 void cloog_loop_otl(CloogLoop *loop, int level)
1280 if (cloog_domain_is_otl(loop->domain, level))
1281 loop->otl = 1;
1286 * cloog_loop_stop function:
1287 * This function implements the 'stop' option : each domain of each loop
1288 * in the list 'loop' is replaced by 'context'. 'context' should be the
1289 * domain of the outer loop. By using this method, there are no more dimensions
1290 * to scan and the simplification step will automaticaly remove the domains
1291 * since they are the same as the corresponding contexts. The effect of this
1292 * function is to stop the code generation at the level this function is called,
1293 * the resulting code do not consider the next dimensions.
1294 * - January 11th 2005: first version.
1296 CloogLoop * cloog_loop_stop(CloogLoop * loop, CloogDomain * context)
1297 { if (loop == NULL)
1298 return NULL ;
1299 else
1300 { cloog_domain_free(loop->domain) ;
1301 loop->domain = cloog_domain_copy(context) ;
1302 loop->next = cloog_loop_stop(loop->next, context) ;
1305 return loop ;
1309 static int level_is_constant(int level, int scalar, int *scaldims, int nb_scattdims)
1311 return level && (level+scalar <= nb_scattdims) && (scaldims[level+scalar-1]);
1316 * Compare the constant dimensions of loops 'l1' and 'l2' starting at 'scalar'
1317 * and return -1 if the vector of constant dimensions of 'l1' is smaller
1318 * than that of 'l2', 0 if they are the same and +1 if that of 'l1' is
1319 * greater than that of 'l2'.
1320 * This function should be called on the innermost loop (the loop
1321 * containing a block).
1322 * \param l1 Loop to be compared with l2.
1323 * \param l2 Loop to be compared with l1.
1324 * \param level Current non-scalar dimension.
1325 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1326 * \param nb_scattdims Size of the scaldims array.
1327 * \param scalar Current scalar dimension.
1328 * \return -1 if (l1 < l2), 0 if (l1 == l2) and +1 if (l1 > l2)
1330 int cloog_loop_constant_cmp(CloogLoop *l1, CloogLoop *l2, int level,
1331 int *scaldims, int nb_scattdims, int scalar)
1333 CloogBlock *b1, *b2;
1334 b1 = l1->block;
1335 b2 = l2->block;
1336 while (level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1337 int cmp = cloog_int_cmp(b1->scaldims[scalar], b2->scaldims[scalar]);
1338 if (cmp)
1339 return cmp;
1340 scalar++;
1342 return 0;
1347 * cloog_loop_scalar_gt function:
1348 * This function returns 1 if loop 'l1' is greater than loop 'l2' for the
1349 * scalar dimension vector that begins at dimension 'scalar', 0 otherwise. What
1350 * we want to know is whether a loop is scheduled before another one or not.
1351 * This function solves the problem when the considered dimension for scheduling
1352 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1353 * this function will reason about the vector of scalar dimension that begins
1354 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1355 * \param l1 Loop to be compared with l2.
1356 * \param l2 Loop to be compared with l1.
1357 * \param level Current non-scalar dimension.
1358 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1359 * \param nb_scattdims Size of the scaldims array.
1360 * \param scalar Current scalar dimension.
1361 * \return 1 if (l1 > l2), 0 otherwise.
1363 * - September 9th 2005: first version.
1364 * - October 15nd 2007: now "greater than" instead of "greater or equal".
1366 int cloog_loop_scalar_gt(l1, l2, level, scaldims, nb_scattdims, scalar)
1367 CloogLoop * l1, * l2 ;
1368 int level, * scaldims, nb_scattdims, scalar ;
1370 return cloog_loop_constant_cmp(l1, l2, level, scaldims, nb_scattdims, scalar) > 0;
1375 * cloog_loop_scalar_eq function:
1376 * This function returns 1 if loop 'l1' is equal to loop 'l2' for the scalar
1377 * dimension vector that begins at dimension 'scalar', 0 otherwise. What we want
1378 * to know is whether two loops are scheduled for the same time or not.
1379 * This function solves the problem when the considered dimension for scheduling
1380 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1381 * this function will reason about the vector of scalar dimension that begins
1382 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1383 * - l1 and l2 are the loops to compare,
1384 * - level is the current non-scalar dimension,
1385 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1386 * - nb_scattdims is the size of the scaldims array,
1387 * - scalar is the current scalar dimension.
1389 * - September 9th 2005 : first version.
1391 int cloog_loop_scalar_eq(l1, l2, level, scaldims, nb_scattdims, scalar)
1392 CloogLoop * l1, * l2 ;
1393 int level, * scaldims, nb_scattdims, scalar ;
1395 return cloog_loop_constant_cmp(l1, l2, level, scaldims, nb_scattdims, scalar) == 0;
1400 * cloog_loop_scalar_sort function:
1401 * This function sorts a linked list of loops (loop) with respect to the
1402 * scalar dimension vector that begins at dimension 'scalar'. Since there may
1403 * be a succession of scalar dimensions, this function will reason about the
1404 * vector of scalar dimension that begins at dimension 'level+scalar' and
1405 * finish to the first non-scalar dimension.
1406 * \param loop Loop list to sort.
1407 * \param level Current non-scalar dimension.
1408 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1409 * \param nb_scattdims Size of the scaldims array.
1410 * \param scalar Current scalar dimension.
1411 * \return A pointer to the sorted list.
1413 * - July 2nd 2005: first developments.
1414 * - September 2nd 2005: first version.
1415 * - October 15nd 2007: complete rewrite to remove bugs, now a bubble sort.
1417 CloogLoop * cloog_loop_scalar_sort(loop, level, scaldims, nb_scattdims, scalar)
1418 CloogLoop * loop ;
1419 int level, * scaldims, nb_scattdims, scalar ;
1420 { int ok ;
1421 CloogLoop **current;
1423 do {
1424 ok = 1;
1425 for (current = &loop; (*current)->next; current = &(*current)->next) {
1426 CloogLoop *next = (*current)->next;
1427 if (cloog_loop_scalar_gt(*current,next,level,scaldims,nb_scattdims,scalar)) {
1428 ok = 0;
1429 (*current)->next = next->next;
1430 next->next = *current;
1431 *current = next;
1434 } while (!ok);
1436 return loop ;
1441 * cloog_loop_generate_backtrack function:
1442 * adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1443 * backtrack of the Quillere et al. algorithm (see the Quillere paper).
1444 * It eliminates unused iterations of the current level for the new one. See the
1445 * example called linearity-1-1 example with and without this part for an idea.
1446 * - October 26th 2001: first version in cloog_loop_generate_general.
1447 * - July 31th 2002: (debug) no more parasite loops (REALLY hard !).
1448 * - October 30th 2005: extraction from cloog_loop_generate_general.
1450 CloogLoop *cloog_loop_generate_backtrack(CloogLoop *loop,
1451 int level, CloogOptions *options)
1453 CloogDomain * domain ;
1454 CloogLoop * now, * now2, * next, * next2, * end, * temp, * l, * inner,
1455 * new_loop ;
1457 temp = loop ;
1458 loop = NULL ;
1460 while (temp != NULL)
1461 { l = NULL ;
1462 inner = temp->inner ;
1464 while (inner != NULL)
1465 { next = inner->next ;
1466 /* This 'if' and its first part is the debug of july 31th 2002. */
1467 if (inner->block != NULL) {
1468 end = cloog_loop_alloc(temp->state, inner->domain, 0, NULL,
1469 inner->block, NULL, NULL);
1470 domain = cloog_domain_copy(temp->domain) ;
1471 new_loop = cloog_loop_alloc(temp->state, domain, 0, NULL,
1472 NULL, end, NULL);
1474 else
1475 new_loop = cloog_loop_project(inner, level);
1477 cloog_loop_free_parts(inner,0,0,0,0) ;
1478 cloog_loop_add(&l,&now2,new_loop) ;
1479 inner = next ;
1482 temp->inner = NULL ;
1484 if (l != NULL)
1485 { l = cloog_loop_separate(l) ;
1486 l = cloog_loop_sort(l, level);
1487 while (l != NULL) {
1488 l->stride = cloog_stride_copy(l->stride);
1489 cloog_loop_add(&loop,&now,l) ;
1490 l = l->next ;
1493 next2 = temp->next ;
1494 cloog_loop_free_parts(temp,1,0,0,0) ;
1495 temp = next2 ;
1498 return loop ;
1503 * Return 1 if we need to continue recursing to the specified level.
1505 int cloog_loop_more(CloogLoop *loop, int level, int scalar, int nb_scattdims)
1507 return level + scalar <= nb_scattdims ||
1508 cloog_domain_dimension(loop->domain) >= level;
1511 CloogLoop *cloog_loop_generate_restricted_or_stop(CloogLoop *loop,
1512 CloogDomain *context,
1513 int level, int scalar, int *scaldims, int nb_scattdims,
1514 CloogOptions *options);
1517 * cloog_loop_generate_general function:
1518 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1519 * Quillere algorithm for polyhedron scanning from step 3 to 5.
1520 * (see the Quillere paper).
1521 * - loop is the loop for which we have to generate a scanning code,
1522 * - level is the current non-scalar dimension,
1523 * - scalar is the current scalar dimension,
1524 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1525 * - nb_scattdims is the size of the scaldims array,
1526 * - options are the general code generation options.
1528 * - October 26th 2001: first version.
1529 * - July 3rd->11th 2003: memory leaks hunt and correction.
1530 * - June 22nd 2005: Adaptation for GMP.
1531 * - September 2nd 2005: The function have been cutted out in two pieces:
1532 * cloog_loop_generate and this one, in order to handle
1533 * the scalar dimension case more efficiently with
1534 * cloog_loop_generate_scalar.
1535 * - November 15th 2005: (debug) the result of the cloog_loop_generate call may
1536 * be a list of polyhedra (especially if stop option is
1537 * used): cloog_loop_add_list instead of cloog_loop_add.
1539 CloogLoop *cloog_loop_generate_general(CloogLoop *loop,
1540 int level, int scalar, int *scaldims, int nb_scattdims,
1541 CloogOptions *options)
1543 CloogLoop * res, * now, * temp, * l, * new_loop, * inner, * now2, * end,
1544 * next, * into ;
1545 CloogDomain * domain ;
1546 int separate = 0;
1548 /* 3. Separate all projections into disjoint polyhedra. */
1549 if ((options->f > level+scalar) || (options->f < 0))
1550 res = cloog_loop_merge(loop, level, options);
1551 else {
1552 res = cloog_loop_separate(loop);
1553 separate = 1;
1556 /* 3b. -correction- sort the loops to determine their textual order. */
1557 res = cloog_loop_sort(res, level);
1559 res = cloog_loop_restrict_inner(res);
1561 if (separate)
1562 res = cloog_loop_specialize(res, level, scalar, scaldims, nb_scattdims);
1564 /* 4. Recurse for each loop with the current domain as context. */
1565 temp = res ;
1566 res = NULL ;
1567 if (!level || (level+scalar < options->l) || (options->l < 0))
1568 while(temp != NULL)
1569 { if (level && options->strides)
1570 cloog_loop_stride(temp, level);
1571 if (level && options->otl)
1572 cloog_loop_otl(temp, level);
1573 inner = temp->inner ;
1574 domain = temp->domain ;
1575 into = NULL ;
1576 while (inner != NULL)
1577 { /* 4b. -ced- recurse for each sub-list of non terminal loops. */
1578 if (cloog_loop_more(inner, level + 1, scalar, nb_scattdims)) {
1579 end = inner;
1580 while ((end->next != NULL) &&
1581 cloog_loop_more(end->next, level + 1, scalar, nb_scattdims))
1582 end = end->next ;
1584 next = end->next ;
1585 end->next = NULL ;
1587 l = cloog_loop_generate_restricted_or_stop(inner, domain,
1588 level + 1, scalar, scaldims, nb_scattdims, options);
1590 if (l != NULL)
1591 cloog_loop_add_list(&into,&now,l) ;
1593 inner = next ;
1595 else
1596 { cloog_loop_add(&into,&now,inner) ;
1597 inner = inner->next ;
1600 next = temp->next ;
1601 temp->next = NULL ;
1602 temp->inner = into ;
1603 cloog_loop_add(&res,&now2,temp) ;
1604 temp = next ;
1606 else
1607 while (temp != NULL)
1608 { next = temp->next ;
1609 l = cloog_loop_nest(temp->inner, temp->domain, level+1);
1610 new_loop = cloog_loop_alloc(temp->state, temp->domain, 0, NULL,
1611 NULL, l, NULL);
1612 temp->inner = NULL ;
1613 temp->next = NULL ;
1614 cloog_loop_free_parts(temp,0,0,0,0) ;
1615 cloog_loop_add(&res,&now,new_loop) ;
1616 temp = next ;
1619 /* 5. eliminate unused iterations of the current level for the new one. See
1620 * the example called linearity-1-1 example with and without this part
1621 * for an idea.
1623 if (options->backtrack && level &&
1624 ((level+scalar < options->l) || (options->l < 0)) &&
1625 ((options->f <= level+scalar) && !(options->f < 0)))
1626 res = cloog_loop_generate_backtrack(res, level, options);
1628 /* Pray for my new paper to be accepted somewhere since the following stuff
1629 * is really amazing :-) !
1630 * Far long later: The paper has been accepted to PACT 2004 :-))). But there
1631 * are still some bugs and I have no time to fix them. Thus now you have to
1632 * pray for me to get an academic position for that really amazing stuff :-) !
1633 * Later again: OK, I get my academic position, but still I have not enough
1634 * time to fix and clean this part... Pray again :-) !!!
1636 /* res = cloog_loop_unisolate(res,level) ;*/
1638 return(res) ;
1642 CloogLoop *cloog_loop_generate_restricted(CloogLoop *loop,
1643 int level, int scalar, int *scaldims, int nb_scattdims,
1644 CloogOptions *options);
1648 * cloog_loop_generate_scalar function:
1649 * This function applies the simplified code generation scheme in the trivial
1650 * case of scalar dimensions. When dealing with scalar dimensions, there is
1651 * no need of costly polyhedral operations for separation or sorting: sorting
1652 * is a question of comparing scalar vectors and separation amounts to consider
1653 * only loops with the same scalar vector for the next step of the code
1654 * generation process. This function achieves the separation/sorting process
1655 * for the vector of scalar dimension that begins at dimension 'level+scalar'
1656 * and finish to the first non-scalar dimension.
1657 * - loop is the loop for which we have to generate a scanning code,
1658 * - level is the current non-scalar dimension,
1659 * - scalar is the current scalar dimension,
1660 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1661 * - nb_scattdims is the size of the scaldims array,
1662 * - options are the general code generation options.
1664 * - September 2nd 2005: First version.
1666 CloogLoop *cloog_loop_generate_scalar(CloogLoop *loop,
1667 int level, int scalar, int *scaldims, int nb_scattdims,
1668 CloogOptions *options)
1669 { CloogLoop * res, * now, * temp, * l, * end, * next, * ref ;
1670 int scalar_new;
1672 /* We sort the loop list with respect to the current scalar vector. */
1673 res = cloog_loop_scalar_sort(loop,level,scaldims,nb_scattdims,scalar) ;
1675 scalar_new = scalar + scaldims[level + scalar - 1];
1677 temp = res ;
1678 res = NULL ;
1679 while (temp != NULL)
1680 { /* Then we will appy the general code generation process to each sub-list
1681 * of loops with the same scalar vector.
1683 end = temp ;
1684 ref = temp ;
1686 while((end->next != NULL) &&
1687 cloog_loop_more(end->next, level, scalar_new, nb_scattdims) &&
1688 cloog_loop_scalar_eq(ref,end->next,level,scaldims,nb_scattdims,scalar))
1689 end = end->next ;
1691 next = end->next ;
1692 end->next = NULL ;
1694 /* For the next dimension, scalar value is updated by adding the scalar
1695 * vector size, which is stored at scaldims[level+scalar-1].
1697 if (cloog_loop_more(temp, level, scalar_new, nb_scattdims)) {
1698 l = cloog_loop_generate_restricted(temp, level, scalar_new,
1699 scaldims, nb_scattdims, options);
1701 if (l != NULL)
1702 cloog_loop_add_list(&res, &now, l);
1703 } else
1704 cloog_loop_add(&res, &now, temp);
1706 temp = next ;
1709 return res ;
1713 /* Compare loop with the next loop based on their constant dimensions.
1714 * The result is < 0, == 0 or > 0 depending on whether the constant
1715 * dimensions of loop are lexicographically smaller, equal or greater
1716 * than those of loop->next.
1717 * If loop is the last in the list, then it is assumed to be smaller
1718 * than the "next" one.
1720 static int cloog_loop_next_scal_cmp(CloogLoop *loop)
1722 int i;
1723 int nb_scaldims;
1725 if (!loop->next)
1726 return -1;
1728 nb_scaldims = loop->block->nb_scaldims;
1729 if (loop->next->block->nb_scaldims < nb_scaldims)
1730 nb_scaldims = loop->next->block->nb_scaldims;
1732 for (i = 0; i < nb_scaldims; ++i) {
1733 int cmp = cloog_int_cmp(loop->block->scaldims[i],
1734 loop->next->block->scaldims[i]);
1735 if (cmp)
1736 return cmp;
1738 return loop->block->nb_scaldims - loop->next->block->nb_scaldims;
1742 /* Check whether the globally constant dimensions of a and b
1743 * have the same value for all globally constant dimensions
1744 * that are situated before any (locally) non-constant dimension.
1746 static int cloog_loop_equal_prefix(CloogLoop *a, CloogLoop *b,
1747 int *scaldims, int nb_scattdims)
1749 int i;
1750 int cst = 0;
1751 int dim = 0;
1753 for (i = 0; i < nb_scattdims; ++i) {
1754 if (!scaldims[i]) {
1755 dim++;
1756 continue;
1758 if (!cloog_int_eq(a->block->scaldims[cst], b->block->scaldims[cst]))
1759 break;
1760 cst++;
1762 for (i = i + 1; i < nb_scattdims; ++i) {
1763 if (scaldims[i])
1764 continue;
1765 if (!cloog_domain_lazy_isconstant(a->domain, dim))
1766 return 0;
1767 /* No need to check that dim is also constant in b and that the
1768 * constant values are equal. That will happen during the check
1769 * whether the two domains are equal.
1771 dim++;
1773 return 1;
1777 /* Try to block adjacent loops in the loop list "loop".
1778 * We only attempt blocking if the constant dimensions of the loops
1779 * in the least are (not necessarily strictly) increasing.
1780 * Then we look for a sublist such that the first (begin) has constant
1781 * dimensions strictly larger than the previous loop in the complete
1782 * list and such that the loop (end) after the last loop in the sublist
1783 * has constant dimensions strictly larger than the last loop in the sublist.
1784 * Furthermore, all loops in the sublist should have the same domain
1785 * (with globally constant dimensions removed) and the difference
1786 * (if any) in constant dimensions may only occur after all the
1787 * (locally) constant dimensions.
1788 * If we find such a sublist, then the blocks of all but the first
1789 * are merged into the block of the first.
1791 * Note that this function can only be called before the global
1792 * blocklist has been created because it may otherwise modify and destroy
1793 * elements on that list.
1795 CloogLoop *cloog_loop_block(CloogLoop *loop, int *scaldims, int nb_scattdims)
1797 CloogLoop *begin, *end, *l;
1798 int begin_after_previous;
1799 int end_after_previous;
1801 if (!loop->next)
1802 return loop;
1803 for (begin = loop; begin; begin = begin->next) {
1804 if (!begin->block || !begin->block->scaldims)
1805 return loop;
1806 if (cloog_loop_next_scal_cmp(begin) > 0)
1807 return loop;
1810 begin_after_previous = 1;
1811 for (begin = loop; begin; begin = begin->next) {
1812 if (!begin_after_previous) {
1813 begin_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1814 continue;
1817 end_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1818 for (end = begin->next; end; end = end->next) {
1819 if (!cloog_loop_equal_prefix(begin, end, scaldims, nb_scattdims))
1820 break;
1821 if (!cloog_domain_lazy_equal(begin->domain, end->domain))
1822 break;
1823 end_after_previous = cloog_loop_next_scal_cmp(end) < 0;
1825 if (end != begin->next && end_after_previous) {
1826 for (l = begin->next; l != end; l = begin->next) {
1827 cloog_block_merge(begin->block, l->block);
1828 begin->next = l->next;
1829 cloog_loop_free_parts(l, 1, 0, 1, 0);
1833 begin_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1836 return loop;
1841 * Check whether for any fixed iteration of the outer loops,
1842 * there is an iteration of loop1 that is lexicographically greater
1843 * than an iteration of loop2.
1844 * Return 1 if there exists (or may exist) such a pair.
1845 * Return 0 if all iterations of loop1 are lexicographically smaller
1846 * than the iterations of loop2.
1847 * If no iteration is lexicographically greater, but if there are
1848 * iterations that are equal to iterations of loop2, then return "def".
1849 * This is useful for ensuring that such statements are not reordered.
1850 * Some users, including the test_run target in test, expect
1851 * the statements at a given point to be run in the original order.
1852 * Passing the value "0" for "def" would allow such statements to be reordered
1853 * and would allow for the detection of more components.
1855 int cloog_loop_follows(CloogLoop *loop1, CloogLoop *loop2,
1856 int level, int scalar, int *scaldims, int nb_scattdims, int def)
1858 int dim1, dim2;
1860 dim1 = cloog_domain_dimension(loop1->domain);
1861 dim2 = cloog_domain_dimension(loop2->domain);
1862 while ((level <= dim1 && level <= dim2) ||
1863 level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1864 if (level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1865 int cmp = cloog_loop_constant_cmp(loop1, loop2, level, scaldims,
1866 nb_scattdims, scalar);
1867 if (cmp > 0)
1868 return 1;
1869 if (cmp < 0)
1870 return 0;
1871 scalar += scaldims[level + scalar - 1];
1872 } else {
1873 int follows = cloog_domain_follows(loop1->domain, loop2->domain,
1874 level);
1875 if (follows > 0)
1876 return 1;
1877 if (follows < 0)
1878 return 0;
1879 level++;
1883 return def;
1887 /* Structure for representing the nodes in the graph being traversed
1888 * using Tarjan's algorithm.
1889 * index represents the order in which nodes are visited.
1890 * min_index is the index of the root of a (sub)component.
1891 * on_stack indicates whether the node is currently on the stack.
1893 struct cloog_loop_sort_node {
1894 int index;
1895 int min_index;
1896 int on_stack;
1898 /* Structure for representing the graph being traversed
1899 * using Tarjan's algorithm.
1900 * len is the number of nodes
1901 * node is an array of nodes
1902 * stack contains the nodes on the path from the root to the current node
1903 * sp is the stack pointer
1904 * index is the index of the last node visited
1905 * order contains the elements of the components separated by -1
1906 * op represents the current position in order
1908 struct cloog_loop_sort {
1909 int len;
1910 struct cloog_loop_sort_node *node;
1911 int *stack;
1912 int sp;
1913 int index;
1914 int *order;
1915 int op;
1918 /* Allocate and initialize cloog_loop_sort structure.
1920 static struct cloog_loop_sort *cloog_loop_sort_alloc(int len)
1922 struct cloog_loop_sort *s;
1923 int i;
1925 s = (struct cloog_loop_sort *)malloc(sizeof(struct cloog_loop_sort));
1926 assert(s);
1927 s->len = len;
1928 s->node = (struct cloog_loop_sort_node *)
1929 malloc(len * sizeof(struct cloog_loop_sort_node));
1930 assert(s->node);
1931 for (i = 0; i < len; ++i)
1932 s->node[i].index = -1;
1933 s->stack = (int *)malloc(len * sizeof(int));
1934 assert(s->stack);
1935 s->order = (int *)malloc(2 * len * sizeof(int));
1936 assert(s->order);
1938 s->sp = 0;
1939 s->index = 0;
1940 s->op = 0;
1942 return s;
1945 /* Free cloog_loop_sort structure.
1947 static void cloog_loop_sort_free(struct cloog_loop_sort *s)
1949 free(s->node);
1950 free(s->stack);
1951 free(s->order);
1952 free(s);
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, where the iteration domains are
1959 * available in the inner loops of the arguments.
1961 * By using this functions to detect components, we ensure that
1962 * two CloogLoops appear in the same component if some iterations of
1963 * each loop should be executed before some iterations of the other loop.
1964 * Since we also want two CloogLoops that have exactly the same
1965 * iteration domain at the current level to be placed in the same component,
1966 * we first check if these domains are indeed the same.
1968 static int inner_loop_follows(CloogLoop *loop1, CloogLoop *loop2,
1969 int level, int scalar, int *scaldims, int nb_scattdims, int def)
1971 int f;
1973 f = cloog_domain_lazy_equal(loop1->domain, loop2->domain);
1974 if (!f)
1975 f = cloog_loop_follows(loop1->inner, loop2->inner,
1976 level, scalar, scaldims, nb_scattdims, def);
1978 return f;
1982 /* Perform Tarjan's algorithm for computing the strongly connected components
1983 * in the graph with the individual CloogLoops as vertices.
1984 * Two CloopLoops appear in the same component if they both (indirectly)
1985 * "follow" each other, where the following relation is determined
1986 * by the follows function.
1988 static void cloog_loop_components_tarjan(struct cloog_loop_sort *s,
1989 CloogLoop **loop_array, int i, int level, int scalar, int *scaldims,
1990 int nb_scattdims,
1991 int (*follows)(CloogLoop *loop1, CloogLoop *loop2,
1992 int level, int scalar, int *scaldims, int nb_scattdims, int def))
1994 int j;
1996 s->node[i].index = s->index;
1997 s->node[i].min_index = s->index;
1998 s->node[i].on_stack = 1;
1999 s->index++;
2000 s->stack[s->sp++] = i;
2002 for (j = s->len - 1; j >= 0; --j) {
2003 int f;
2005 if (j == i)
2006 continue;
2007 if (s->node[j].index >= 0 &&
2008 (!s->node[j].on_stack ||
2009 s->node[j].index > s->node[i].min_index))
2010 continue;
2012 f = follows(loop_array[i], loop_array[j],
2013 level, scalar, scaldims, nb_scattdims, i > j);
2014 if (!f)
2015 continue;
2017 if (s->node[j].index < 0) {
2018 cloog_loop_components_tarjan(s, loop_array, j, level, scalar,
2019 scaldims, nb_scattdims, follows);
2020 if (s->node[j].min_index < s->node[i].min_index)
2021 s->node[i].min_index = s->node[j].min_index;
2022 } else if (s->node[j].index < s->node[i].min_index)
2023 s->node[i].min_index = s->node[j].index;
2026 if (s->node[i].index != s->node[i].min_index)
2027 return;
2029 do {
2030 j = s->stack[--s->sp];
2031 s->node[j].on_stack = 0;
2032 s->order[s->op++] = j;
2033 } while (j != i);
2034 s->order[s->op++] = -1;
2038 static int qsort_index_cmp(const void *p1, const void *p2)
2040 return *(int *)p1 - *(int *)p2;
2043 /* Sort the elements of the component starting at list.
2044 * The list is terminated by a -1.
2046 static void sort_component(int *list)
2048 int len;
2050 for (len = 0; list[len] != -1; ++len)
2053 qsort(list, len, sizeof(int), qsort_index_cmp);
2056 /* Given an array of indices "list" into the "loop_array" array,
2057 * terminated by -1, construct a linked list of the corresponding
2058 * entries and put the result in *res.
2059 * The value returned is the number of CloogLoops in the (linked) list
2061 static int extract_component(CloogLoop **loop_array, int *list, CloogLoop **res)
2063 int i = 0;
2065 sort_component(list);
2066 while (list[i] != -1) {
2067 *res = loop_array[list[i]];
2068 res = &(*res)->next;
2069 ++i;
2071 *res = NULL;
2073 return i;
2078 * Call cloog_loop_generate_scalar or cloog_loop_generate_general
2079 * on each of the strongly connected components in the list of CloogLoops
2080 * pointed to by "loop".
2082 * We use Tarjan's algorithm to find the strongly connected components.
2083 * Note that this algorithm also topologically sorts the components.
2085 * The components are treated separately to avoid spurious separations.
2086 * The concatentation of the results may contain successive loops
2087 * with the same bounds, so we try to combine such loops.
2089 CloogLoop *cloog_loop_generate_components(CloogLoop *loop,
2090 int level, int scalar, int *scaldims, int nb_scattdims,
2091 CloogOptions *options)
2093 int i, nb_loops;
2094 CloogLoop *tmp;
2095 CloogLoop *res, **res_next;
2096 CloogLoop **loop_array;
2097 struct cloog_loop_sort *s;
2099 if (level == 0 || !loop->next)
2100 return cloog_loop_generate_general(loop, level, scalar,
2101 scaldims, nb_scattdims, options);
2103 nb_loops = cloog_loop_count(loop);
2105 loop_array = (CloogLoop **)malloc(nb_loops * sizeof(CloogLoop *));
2106 assert(loop_array);
2108 for (i = 0, tmp = loop; i < nb_loops; i++, tmp = tmp->next)
2109 loop_array[i] = tmp;
2111 s = cloog_loop_sort_alloc(nb_loops);
2112 for (i = nb_loops - 1; i >= 0; --i) {
2113 if (s->node[i].index >= 0)
2114 continue;
2115 cloog_loop_components_tarjan(s, loop_array, i, level, scalar, scaldims,
2116 nb_scattdims, &inner_loop_follows);
2119 i = 0;
2120 res = NULL;
2121 res_next = &res;
2122 while (nb_loops) {
2123 int n = extract_component(loop_array, &s->order[i], &tmp);
2124 i += n + 1;
2125 nb_loops -= n;
2126 *res_next = cloog_loop_generate_general(tmp, level, scalar,
2127 scaldims, nb_scattdims, options);
2128 while (*res_next)
2129 res_next = &(*res_next)->next;
2132 cloog_loop_sort_free(s);
2134 free(loop_array);
2136 res = cloog_loop_combine(res);
2138 return res;
2142 /* For each loop in the list "loop", decompose the list of
2143 * inner loops into strongly connected components and put
2144 * the components into separate loops at the top level.
2146 CloogLoop *cloog_loop_decompose_inner(CloogLoop *loop,
2147 int level, int scalar, int *scaldims, int nb_scattdims)
2149 CloogLoop *l, *tmp;
2150 CloogLoop **loop_array;
2151 int i, n_loops, max_loops = 0;
2152 struct cloog_loop_sort *s;
2154 for (l = loop; l; l = l->next) {
2155 n_loops = cloog_loop_count(l->inner);
2156 if (max_loops < n_loops)
2157 max_loops = n_loops;
2160 if (max_loops <= 1)
2161 return loop;
2163 loop_array = (CloogLoop **)malloc(max_loops * sizeof(CloogLoop *));
2164 assert(loop_array);
2166 for (l = loop; l; l = l->next) {
2167 int n;
2169 for (i = 0, tmp = l->inner; tmp; i++, tmp = tmp->next)
2170 loop_array[i] = tmp;
2171 n_loops = i;
2172 if (n_loops <= 1)
2173 continue;
2175 s = cloog_loop_sort_alloc(n_loops);
2176 for (i = n_loops - 1; i >= 0; --i) {
2177 if (s->node[i].index >= 0)
2178 continue;
2179 cloog_loop_components_tarjan(s, loop_array, i, level, scalar,
2180 scaldims, nb_scattdims, &cloog_loop_follows);
2183 n = extract_component(loop_array, s->order, &l->inner);
2184 n_loops -= n;
2185 i = n + 1;
2186 while (n_loops) {
2187 CloogLoop *inner;
2189 n = extract_component(loop_array, &s->order[i], &inner);
2190 n_loops -= n;
2191 i += n + 1;
2192 tmp = cloog_loop_alloc(l->state, cloog_domain_copy(l->domain),
2193 l->otl, l->stride, l->block, inner, l->next);
2194 l->next = tmp;
2195 l = tmp;
2198 cloog_loop_sort_free(s);
2201 free(loop_array);
2203 return loop;
2207 CloogLoop *cloog_loop_generate_restricted(CloogLoop *loop,
2208 int level, int scalar, int *scaldims, int nb_scattdims,
2209 CloogOptions *options)
2211 /* To save both time and memory, we switch here depending on whether the
2212 * current dimension is scalar (simplified processing) or not (general
2213 * processing).
2215 if (level_is_constant(level, scalar, scaldims, nb_scattdims))
2216 return cloog_loop_generate_scalar(loop, level, scalar,
2217 scaldims, nb_scattdims, options);
2219 * 2. Compute the projection of each polyhedron onto the outermost
2220 * loop variable and the parameters.
2222 loop = cloog_loop_project_all(loop, level);
2224 return cloog_loop_generate_components(loop, level, scalar, scaldims,
2225 nb_scattdims, options);
2229 CloogLoop *cloog_loop_generate_restricted_or_stop(CloogLoop *loop,
2230 CloogDomain *context,
2231 int level, int scalar, int *scaldims, int nb_scattdims,
2232 CloogOptions *options)
2234 /* If the user asked to stop code generation at this level, let's stop. */
2235 if ((options->stop >= 0) && (level+scalar >= options->stop+1))
2236 return cloog_loop_stop(loop,context) ;
2238 return cloog_loop_generate_restricted(loop, level, scalar, scaldims,
2239 nb_scattdims, options);
2244 * cloog_loop_generate function:
2245 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
2246 * Quillere algorithm for polyhedron scanning from step 1 to 2.
2247 * (see the Quillere paper).
2248 * - loop is the loop for which we have to generate a scanning code,
2249 * - context is the context of the current loop (constraints on parameter and/or
2250 * on outer loop counters),
2251 * - level is the current non-scalar dimension,
2252 * - scalar is the current scalar dimension,
2253 * - scaldims is the boolean array saying whether a dimension is scalar or not,
2254 * - nb_scattdims is the size of the scaldims array,
2255 * - options are the general code generation options.
2257 * - October 26th 2001: first version.
2258 * - July 3rd->11th 2003: memory leaks hunt and correction.
2259 * - June 15th 2005: a memory leak fixed (loop was not entirely freed when
2260 * the result of cloog_loop_restrict was NULL).
2261 * - June 22nd 2005: Adaptation for GMP.
2262 * - September 2nd 2005: The function have been cutted out in two pieces:
2263 * cloog_loop_generate and this one, in order to handle
2264 * the scalar dimension case more efficiently with
2265 * cloog_loop_generate_scalar.
2266 * - November 15th 2005: (debug) Condition for stop option no more take care of
2267 * further scalar dimensions.
2269 CloogLoop *cloog_loop_generate(CloogLoop *loop, CloogDomain *context,
2270 int level, int scalar, int *scaldims, int nb_scattdims,
2271 CloogOptions *options)
2273 /* 1. Replace each polyhedron by its intersection with the context.
2275 loop = cloog_loop_restrict_all(loop, context);
2276 if (!loop)
2277 return NULL;
2279 return cloog_loop_generate_restricted_or_stop(loop, context,
2280 level, scalar, scaldims, nb_scattdims, options);
2285 * Internal function for simplifying a single loop in a list of loops.
2286 * See cloog_loop_simplify.
2288 static CloogLoop *loop_simplify(CloogLoop *loop, CloogDomain *context,
2289 int level)
2291 int domain_dim;
2292 CloogBlock * new_block ;
2293 CloogLoop *simplified, *inner;
2294 CloogDomain * domain, * simp, * inter, * extended_context ;
2296 if (!cloog_domain_isconvex(loop->domain))
2297 loop->domain = cloog_domain_simplify_union(loop->domain);
2299 domain = loop->domain ;
2301 domain_dim = cloog_domain_dimension(domain);
2302 extended_context = cloog_domain_extend(context, domain_dim);
2303 inter = cloog_domain_intersection(domain,extended_context) ;
2304 simp = cloog_domain_simplify(inter,extended_context) ;
2305 cloog_domain_free(extended_context) ;
2307 /* If the constraint system is never true, go to the next one. */
2308 if (cloog_domain_never_integral(simp)) {
2309 cloog_loop_free(loop->inner);
2310 cloog_domain_free(inter);
2311 cloog_domain_free(simp);
2312 return NULL;
2315 inner = cloog_loop_simplify(loop->inner, inter, level+1);
2316 cloog_domain_free(inter) ;
2318 if ((inner == NULL) && (loop->block == NULL)) {
2319 cloog_domain_free(simp);
2320 return NULL;
2323 new_block = cloog_block_copy(loop->block) ;
2325 simplified = cloog_loop_alloc(loop->state, simp, loop->otl, loop->stride,
2326 new_block, inner, NULL);
2328 return(simplified) ;
2333 * cloog_loop_simplify function:
2334 * This function implements the part 6. of the Quillere algorithm, it
2335 * recursively simplifies each loop in the context of the preceding loop domain.
2336 * It returns a pointer to the simplified loop list.
2337 * The cloog_domain_simplify (DomainSimplify) behaviour is really bad with
2338 * polyhedra union and some really awful sidesteppings were written, I plan
2339 * to solve that...
2340 * - October 31th 2001: first version.
2341 * - July 3rd->11th 2003: memory leaks hunt and correction.
2342 * - April 16th 2005: a memory leak fixed (extended_context was not freed).
2343 * - June 15th 2005: a memory leak fixed (loop was not conveniently freed
2344 * when the constraint system is never true).
2345 * - October 27th 2005: - this function called before cloog_loop_fast_simplify
2346 * is now the official cloog_loop_simplify function in
2347 * replacement of a slower and more complex one (after
2348 * deep changes in the pretty printer).
2349 * - we use cloog_loop_disjoint to fix the problem when
2350 * simplifying gives a union of polyhedra (before, it
2351 * was under the responsibility of the pretty printer).
2353 CloogLoop *cloog_loop_simplify(CloogLoop *loop, CloogDomain *context, int level)
2355 CloogLoop *now;
2356 CloogLoop *res = NULL;
2357 CloogLoop **next = &res;
2359 for (now = loop; now; now = now->next) {
2360 *next = loop_simplify(now, context, level);
2362 now->inner = NULL; /* For loop integrity. */
2363 cloog_domain_free(now->domain);
2364 now->domain = NULL;
2366 if (*next)
2367 next = &(*next)->next;
2369 cloog_loop_free(loop);
2371 /* Examples like test/iftest2.cloog give unions of polyhedra after
2372 * simplifying, thus we we have to disjoint them. Another good reason to
2373 * put the simplifying step in the Quillere backtrack.
2375 res = cloog_loop_disjoint(res);
2377 return res;
2382 * cloog_loop_scatter function:
2383 * This function add the scattering (scheduling) informations in a loop.
2385 void cloog_loop_scatter(CloogLoop * loop, CloogScattering *scatt)
2387 loop->domain = cloog_domain_scatter(loop->domain, scatt);