cloog_loop_generate: optimize the loop domains after separation
[cloog/uuh.git] / source / loop.c
blobbefab934829d089c71e688157a25ca6998abc722
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"
45 /******************************************************************************
46 * Memory leaks hunting *
47 ******************************************************************************/
50 /**
51 * These functions and global variables are devoted to memory leaks hunting: we
52 * want to know at each moment how many CloogLoop structures had been allocated
53 * (cloog_loop_allocated) and how many had been freed (cloog_loop_freed).
54 * Each time a CloogLoog structure is allocated, a call to the function
55 * cloog_loop_leak_up() must be carried out, and respectively
56 * cloog_loop_leak_down() when a CloogLoop structure is freed. The special
57 * variable cloog_loop_max gives the maximal number of CloogLoop structures
58 * simultaneously alive (i.e. allocated and non-freed) in memory.
59 * - July 3rd->11th 2003: first version (memory leaks hunt and correction).
63 static void cloog_loop_leak_up(CloogState *state)
65 state->loop_allocated++;
66 if ((state->loop_allocated - state->loop_freed) > state->loop_max)
67 state->loop_max = state->loop_allocated - state->loop_freed;
71 static void cloog_loop_leak_down(CloogState *state)
73 state->loop_freed++;
77 /******************************************************************************
78 * Structure display function *
79 ******************************************************************************/
82 /**
83 * cloog_loop_print_structure function:
84 * Displays a loop structure in a way that trends to be understandable without
85 * falling in a deep depression or, for the lucky ones, getting a headache...
86 * Written by Olivier Chorier, Luc Marchaud, Pierre Martin and Romain Tartiere.
87 * - April 24th 2005: Initial version.
88 * - May 21rd 2005: - New parameter `F' for destination file (ie stdout),
89 * - Minor tweaks.
90 * - May 26th 2005: Memory leak hunt.
91 * - June 2nd 2005: (Ced) Integration and minor fixes.
92 * -June 22nd 2005: (Ced) Adaptation for GMP.
94 void cloog_loop_print_structure(FILE * file, CloogLoop * loop, int level)
95 { int i, j, first=1 ;
97 if (loop)
98 { /* Go to the right level. */
99 for (i=0; i<level; i++)
100 fprintf(file,"|\t") ;
102 fprintf(file,"+-- CloogLoop\n") ;
105 /* For each loop. */
106 while (loop)
107 { if (!first)
108 { /* Go to the right level. */
109 for (i=0; i<level; i++)
110 fprintf(file,"|\t") ;
112 fprintf(file,"| CloogLoop\n") ;
114 else
115 first = 0 ;
117 /* A blank line. */
118 for(j=0; j<=level+1; j++)
119 fprintf(file,"|\t") ;
120 fprintf(file,"\n") ;
122 /* Print the domain. */
123 cloog_domain_print_structure(file, loop->domain, level+1, "CloogDomain");
125 /* Print the stride. */
126 for(j=0; j<=level; j++)
127 fprintf(file,"|\t") ;
128 fprintf(file, "Stride: ") ;
129 cloog_int_print(file, loop->stride);
130 fprintf(file, "\n") ;
131 fprintf(file, "Offset: ") ;
132 cloog_int_print(file, loop->offset);
133 fprintf(file, "\n") ;
135 /* A blank line. */
136 for(j=0; j<=level+1; j++)
137 fprintf(file,"|\t") ;
138 fprintf(file,"\n") ;
140 /* Print the block. */
141 cloog_block_print_structure(file,loop->block,level+1) ;
143 /* A blank line. */
144 for (i=0; i<=level+1; i++)
145 fprintf(file,"|\t") ;
146 fprintf(file,"\n") ;
148 /* Print inner if any. */
149 if (loop->inner)
150 cloog_loop_print_structure(file,loop->inner,level+1) ;
152 /* And let's go for the next one. */
153 loop = loop->next ;
155 /* One more time something that is here only for a better look. */
156 if (!loop)
157 { /* Two blank lines if this is the end of the linked list. */
158 for (j=0; j<2; j++)
159 { for (i=0; i<=level; i++)
160 fprintf(file,"|\t") ;
162 fprintf(file,"\n") ;
165 else
166 { /* A special blank line if the is a next loop. */
167 for (i=0; i<=level; i++)
168 fprintf(file,"|\t") ;
169 fprintf(file,"V\n") ;
176 * cloog_loop_print function:
177 * This function prints the content of a CloogLoop structure (start) into a
178 * file (file, possibly stdout).
179 * - June 2nd 2005: Now this very old function (probably as old as CLooG) is
180 * only a frontend to cloog_loop_print_structure, with a quite
181 * better human-readable representation.
183 void cloog_loop_print(FILE * file, CloogLoop * loop)
184 { cloog_loop_print_structure(file,loop,0) ;
188 /******************************************************************************
189 * Memory deallocation function *
190 ******************************************************************************/
194 * cloog_loop_free function:
195 * This function frees the allocated memory for a CloogLoop structure (loop),
196 * and frees its inner loops and its next loops.
197 * - June 22nd 2005: Adaptation for GMP.
199 void cloog_loop_free(CloogLoop * loop)
200 { CloogLoop * next ;
202 while (loop != NULL) {
203 cloog_loop_leak_down(loop->state);
205 next = loop->next ;
206 cloog_domain_free(loop->domain) ;
207 cloog_block_free(loop->block) ;
208 if (loop->inner != NULL)
209 cloog_loop_free(loop->inner) ;
211 cloog_int_clear(loop->stride);
212 cloog_int_clear(loop->offset);
213 free(loop) ;
214 loop = next ;
220 * cloog_loop_free_parts function:
221 * This function frees the allocated memory for some parts of a CloogLoop
222 * structure (loop), each other argument is a boolean having to be set to 1 if
223 * we want to free the corresponding part, 0 otherwise. This function applies
224 * the same freeing policy to its inner ans next loops recursively.
225 * - July 3rd 2003: first version.
226 * - June 22nd 2005: Adaptation for GMP.
228 void cloog_loop_free_parts(loop, domain, block, inner, next)
229 CloogLoop * loop ;
230 int domain, block, inner, next ;
231 { CloogLoop * follow ;
233 while (loop != NULL) {
234 cloog_loop_leak_down(loop->state);
235 follow = loop->next ;
237 if (domain)
238 cloog_domain_free(loop->domain) ;
240 if (block)
241 cloog_block_free(loop->block) ;
243 if ((inner) && (loop->inner != NULL))
244 cloog_loop_free_parts(loop->inner,domain,block,inner,1) ;
246 cloog_int_clear(loop->stride);
247 cloog_int_clear(loop->offset);
248 free(loop) ;
249 if (next)
250 loop = follow ;
251 else
252 loop = NULL ;
257 /******************************************************************************
258 * Reading functions *
259 ******************************************************************************/
263 * cloog_loop_read function:
264 * This function reads loop data into a file (foo, possibly stdin) and
265 * returns a pointer to a CloogLoop structure containing the read information.
266 * This function can be used only for input file reading, when one loop is
267 * associated with one statement.
268 * - number is the statement block number carried by the loop (-1 if none).
269 * - nb_parameters is the number of parameters.
271 * - September 9th 2002: first version.
272 * - April 16th 2005: adaptation to new CloogStatement struct (with number).
273 * - June 11th 2005: adaptation to new CloogBlock structure.
274 * - June 22nd 2005: Adaptation for GMP.
276 CloogLoop *cloog_loop_read(CloogState *state,
277 FILE * foo, int number, int nb_parameters)
278 { int nb_iterators, op1, op2, op3 ;
279 char s[MAX_STRING] ;
280 CloogLoop * loop ;
281 CloogStatement * statement ;
283 cloog_loop_leak_up(state);
285 /* Memory allocation and information reading for the first domain: */
286 loop = (CloogLoop *)malloc(sizeof(CloogLoop)) ;
287 if (loop == NULL)
288 cloog_die("memory overflow.\n");
289 /* domain. */
290 loop->state = state;
291 loop->domain = cloog_domain_union_read(state, foo, nb_parameters);
292 if (loop->domain != NULL)
293 nb_iterators = cloog_domain_dimension(loop->domain);
294 else
295 nb_iterators = 0 ;
296 /* stride is initialized to 1. */
297 cloog_int_init(loop->stride);
298 cloog_int_set_si(loop->stride, 1);
299 cloog_int_init(loop->offset);
300 cloog_int_set_si(loop->offset, 0);
301 /* included statement block. */
302 statement = cloog_statement_alloc(state, number + 1);
303 loop->block = cloog_block_alloc(statement, 0, NULL, nb_iterators);
304 loop->usr = NULL;
305 /* inner is NULL at beginning. */
306 loop->inner = NULL ;
307 /* next element. */
308 loop->next = NULL ;
310 /* To read that stupid "0 0 0" line. */
311 while (fgets(s,MAX_STRING,foo) == 0) ;
312 while ((*s=='#' || *s=='\n') || (sscanf(s," %d %d %d",&op1,&op2,&op3)<3))
313 fgets(s,MAX_STRING,foo) ;
315 return loop ;
319 /******************************************************************************
320 * Processing functions *
321 ******************************************************************************/
325 * cloog_loop_malloc function:
326 * This function allocates the memory space for a CloogLoop structure and
327 * sets its fields with default values. Then it returns a pointer to the
328 * allocated space.
329 * - November 21th 2005: first version.
331 CloogLoop *cloog_loop_malloc(CloogState *state)
332 { CloogLoop * loop ;
334 /* Memory allocation for the CloogLoop structure. */
335 loop = (CloogLoop *)malloc(sizeof(CloogLoop)) ;
336 if (loop == NULL)
337 cloog_die("memory overflow.\n");
338 cloog_loop_leak_up(state);
341 /* We set the various fields with default values. */
342 loop->state = state;
343 loop->domain = NULL ;
344 loop->block = NULL ;
345 loop->usr = NULL;
346 loop->inner = NULL ;
347 loop->next = NULL ;
348 cloog_int_init(loop->stride);
349 cloog_int_set_si(loop->stride, 1);
350 cloog_int_init(loop->offset);
351 cloog_int_set_si(loop->offset, 0);
353 return loop ;
358 * cloog_loop_alloc function:
359 * This function allocates the memory space for a CloogLoop structure and
360 * sets its fields with those given as input. Then it returns a pointer to the
361 * allocated space.
362 * - October 27th 2001: first version.
363 * - June 22nd 2005: Adaptation for GMP.
364 * - November 21th 2005: use of cloog_loop_malloc.
366 CloogLoop *cloog_loop_alloc(CloogState *state,
367 CloogDomain *domain, cloog_int_t stride, cloog_int_t offset,
368 CloogBlock *block, CloogLoop *inner, CloogLoop *next)
369 { CloogLoop * loop ;
371 loop = cloog_loop_malloc(state);
373 loop->domain = domain ;
374 loop->block = block ;
375 loop->inner = inner ;
376 loop->next = next ;
377 cloog_int_set(loop->stride, stride);
378 cloog_int_set(loop->offset, offset);
380 return(loop) ;
385 * cloog_loop_add function:
386 * This function adds a CloogLoop structure (loop) at a given place (now) of a
387 * NULL terminated list of CloogLoop structures. The beginning of this list
388 * is (start). This function updates (now) to (loop), and updates (start) if the
389 * added element is the first one -that is when (start) is NULL-.
390 * - October 28th 2001: first version.
392 void cloog_loop_add(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
393 { if (*start == NULL)
394 { *start = loop ;
395 *now = *start ;
397 else
398 { (*now)->next = loop ;
399 *now = (*now)->next ;
405 * cloog_loop_add function:
406 * This function adds a CloogLoop structure (loop) at a given place (now) of a
407 * NULL terminated list of CloogLoop structures. The beginning of this list
408 * is (start). This function updates (now) to the end of the loop list (loop),
409 * and updates (start) if the added element is the first one -that is when
410 * (start) is NULL-.
411 * - September 9th 2005: first version.
413 void cloog_loop_add_list(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
414 { if (*start == NULL)
415 { *start = loop ;
416 *now = *start ;
418 else
419 { (*now)->next = loop ;
420 *now = (*now)->next ;
423 while ((*now)->next != NULL)
424 *now = (*now)->next ;
429 * cloog_loop_copy function:
430 * This function returns a copy of the CloogLoop structure given as input. In
431 * fact, there is just new allocations for the CloogLoop structures, but their
432 * contents are the same.
433 * - October 28th 2001: first version.
434 * - July 3rd->11th 2003: memory leaks hunt and correction.
436 CloogLoop * cloog_loop_copy(CloogLoop * source)
437 { CloogLoop * loop ;
438 CloogBlock * block ;
439 CloogDomain * domain ;
441 loop = NULL ;
442 if (source != NULL)
443 { domain = cloog_domain_copy(source->domain) ;
444 block = cloog_block_copy(source->block) ;
445 loop = cloog_loop_alloc(source->state, domain, source->stride,
446 source->offset, block, NULL, NULL);
447 loop->usr = source->usr;
448 loop->inner = cloog_loop_copy(source->inner) ;
449 loop->next = cloog_loop_copy(source->next) ;
451 return(loop) ;
456 * cloog_loop_add_disjoint function:
457 * This function adds some CloogLoop structures at a given place (now) of a
458 * NULL terminated list of CloogLoop structures. The beginning of this list
459 * is (start). (loop) can be an union of polyhedra, this function separates the
460 * union into a list of *disjoint* polyhedra then adds the list. This function
461 * updates (now) to the end of the list and updates (start) if first added
462 * element is the first of the principal list -that is when (start) is NULL-.
463 * (loop) can be freed by this function, basically when its domain is actually
464 * a union of polyhedra, but don't worry, all the useful data are now stored
465 * inside the list (start). We do not use PolyLib's Domain_Disjoint function,
466 * since the number of union components is often higher (thus code size too).
467 * - October 28th 2001: first version.
468 * - November 14th 2001: bug correction (this one was hard to find !).
469 * - July 3rd->11th 2003: memory leaks hunt and correction.
470 * - June 22nd 2005: Adaptation for GMP.
471 * - October 27th 2005: (debug) included blocks were not copied for new loops.
473 void cloog_loop_add_disjoint(start, now, loop)
474 CloogLoop ** start, ** now, * loop ;
476 CloogLoop * sep, * inner ;
477 CloogDomain *domain, *seen, *temp, *rest;
478 CloogBlock * block ;
480 if (cloog_domain_isconvex(loop->domain))
481 cloog_loop_add(start,now,loop) ;
482 else {
483 domain = cloog_domain_simplify_union(loop->domain);
484 loop->domain = NULL ;
486 /* We separate the first element of the rest of the union. */
487 domain = cloog_domain_cut_first(domain, &rest);
489 /* This first element is the first of the list of disjoint polyhedra. */
490 sep = cloog_loop_alloc(loop->state, domain, loop->state->one,
491 loop->state->zero, loop->block, loop->inner, NULL);
492 cloog_loop_add(start,now,sep) ;
494 seen = cloog_domain_copy(domain);
495 while (!cloog_domain_isempty(domain = rest)) {
496 temp = cloog_domain_cut_first(domain, &rest);
497 domain = cloog_domain_difference(temp, seen);
498 cloog_domain_free(temp);
500 if (cloog_domain_isempty(domain)) {
501 cloog_domain_free(domain);
502 continue;
505 /* Each new loop will have its own life, for instance we can free its
506 * inner loop and included block. Then each one must have its own copy
507 * of both 'inner' and 'block'.
509 inner = cloog_loop_copy(loop->inner) ;
510 block = cloog_block_copy(loop->block) ;
512 sep = cloog_loop_alloc(loop->state, cloog_domain_copy(domain),
513 loop->state->one, loop->state->zero,
514 block, inner, NULL);
515 /* domain can be an union too. If so: recursion. */
516 if (cloog_domain_isconvex(domain))
517 cloog_loop_add(start,now,sep) ;
518 else
519 cloog_loop_add_disjoint(start,now,sep) ;
521 if (cloog_domain_isempty(rest)) {
522 cloog_domain_free(domain);
523 break;
526 seen = cloog_domain_union(seen, domain);
528 cloog_domain_free(rest);
529 cloog_domain_free(seen);
530 cloog_loop_free_parts(loop,0,0,0,0) ;
536 * cloog_loop_disjoint function:
537 * This function returns a list of loops such that each loop with non-convex
538 * domain in the input list (loop) is separated into several loops where the
539 * domains are the components of the union of *disjoint* polyhedra equivalent
540 * to the original non-convex domain. See cloog_loop_add_disjoint comments
541 * for more details.
542 * - September 16th 2005: first version.
544 CloogLoop * cloog_loop_disjoint(CloogLoop * loop)
545 { CloogLoop *res=NULL, * now=NULL, * next ;
547 /* Because this is often the case, don't waste time ! */
548 if (loop && !loop->next && cloog_domain_isconvex(loop->domain))
549 return loop ;
551 while (loop != NULL)
552 { next = loop->next ;
553 loop->next = NULL ;
554 cloog_loop_add_disjoint(&res,&now,loop) ;
555 loop = next ;
558 return res ;
563 * cloog_loop_restrict function:
564 * This function returns the (loop) in the context of (context): it makes the
565 * intersection between the (loop) domain and the (context), then it returns
566 * a pointer to a new loop, with this intersection as domain.
568 * - October 27th 2001: first version.
569 * - June 15th 2005: a memory leak fixed (domain was not freed when empty).
570 * - June 22nd 2005: Adaptation for GMP.
572 CloogLoop *cloog_loop_restrict(CloogLoop *loop, CloogDomain *context)
573 { int new_dimension ;
574 CloogDomain * domain, * extended_context, * new_domain ;
575 CloogLoop * new_loop ;
577 domain = loop->domain ;
578 if (cloog_domain_dimension(domain) > cloog_domain_dimension(context))
580 new_dimension = cloog_domain_dimension(domain);
581 extended_context = cloog_domain_extend(context, new_dimension);
582 new_domain = cloog_domain_intersection(extended_context,loop->domain) ;
583 cloog_domain_free(extended_context) ;
585 else
586 new_domain = cloog_domain_intersection(context,loop->domain) ;
588 if (cloog_domain_isempty(new_domain))
589 { cloog_domain_free(new_domain) ;
590 return(NULL) ;
592 else {
593 new_loop = cloog_loop_alloc(loop->state, new_domain,
594 loop->state->one, loop->state->zero,
595 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->stride, loop->offset,
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, loop->state->one,
661 loop->state->zero, 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 CloogLoop *l;
774 loop = cloog_loop_decompose_inner(loop, level, scalar,
775 scaldims, nb_scattdims);
777 for (l = loop; l; l = l->next) {
778 if (l->inner->next)
779 continue;
780 if (!cloog_domain_isconvex(l->inner->domain))
781 continue;
783 dim = cloog_domain_dimension(l->domain);
784 cloog_domain_free(l->domain);
785 l->domain = cloog_domain_project(l->inner->domain, dim);
788 return cloog_loop_remove_empty_domain_loops(loop);
792 * cloog_loop_separate function:
793 * This function implements the Quillere algorithm for separation of multiple
794 * loops: for a given set of polyhedra (loop), it computes a set of disjoint
795 * polyhedra such that the unions of these sets are equal, and returns this set.
796 * - October 28th 2001: first version.
797 * - November 14th 2001: elimination of some unused blocks.
798 * - August 13th 2002: (debug) in the case of union of polyhedra for one
799 * loop, redundant constraints are fired.
800 * - July 3rd->11th 2003: memory leaks hunt and correction.
801 * - June 22nd 2005: Adaptation for GMP.
802 * - October 16th 2005: Removal of the non-shared constraint elimination when
803 * there is only one loop in the list (seems to work
804 * without now, DomainSimplify may have been improved).
805 * The problem was visible with test/iftest2.cloog.
807 CloogLoop * cloog_loop_separate(CloogLoop * loop)
808 { int lazy_equal=0, disjoint = 0;
809 CloogLoop * new_loop, * new_inner, * res, * now, * temp, * Q,
810 * inner, * old /*, * previous, * next*/ ;
811 CloogDomain *UQ, *domain;
813 if (loop == NULL)
814 return NULL ;
816 loop = cloog_loop_combine(loop);
818 if (loop->next == NULL)
819 return cloog_loop_disjoint(loop) ;
821 UQ = cloog_domain_copy(loop->domain) ;
822 domain = cloog_domain_copy(loop->domain) ;
823 res = cloog_loop_alloc(loop->state, domain, loop->state->one,
824 loop->state->zero, loop->block, loop->inner, NULL);
826 old = loop ;
827 while((loop = loop->next) != NULL)
828 { temp = NULL ;
830 /* For all Q, add Q-loop associated with the blocks of Q alone,
831 * and Q inter loop associated with the blocks of Q and loop.
833 for (Q = res; Q; Q = Q->next) {
834 /* Add (Q inter loop). */
835 if ((disjoint = cloog_domain_lazy_disjoint(Q->domain,loop->domain)))
836 domain = NULL ;
837 else
838 { if ((lazy_equal = cloog_domain_lazy_equal(Q->domain,loop->domain)))
839 domain = cloog_domain_copy(Q->domain) ;
840 else
841 domain = cloog_domain_intersection(Q->domain,loop->domain) ;
843 if (!cloog_domain_isempty(domain))
844 { new_inner = cloog_loop_concat(cloog_loop_copy(Q->inner),
845 cloog_loop_copy(loop->inner)) ;
846 new_loop = cloog_loop_alloc(loop->state, domain, loop->state->one,
847 loop->state->zero, NULL, new_inner, NULL);
848 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
850 else {
851 disjoint = 1;
852 cloog_domain_free(domain);
856 /* Add (Q - loop). */
857 if (disjoint)
858 domain = cloog_domain_copy(Q->domain) ;
859 else
860 { if (lazy_equal)
861 domain = cloog_domain_empty(Q->domain);
862 else
863 domain = cloog_domain_difference(Q->domain,loop->domain) ;
866 if (!cloog_domain_isempty(domain)) {
867 new_loop = cloog_loop_alloc(loop->state, domain, loop->state->one,
868 loop->state->zero, NULL, Q->inner, NULL);
869 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
871 else
872 { cloog_domain_free(domain) ;
873 /* If Q->inner is no more useful, we can free it. */
874 inner = Q->inner ;
875 Q->inner = NULL ;
876 cloog_loop_free(inner) ;
880 /* Add loop-UQ associated with the blocks of loop alone.*/
881 if (cloog_domain_lazy_disjoint(loop->domain,UQ))
882 domain = cloog_domain_copy(loop->domain) ;
883 else
884 { if (cloog_domain_lazy_equal(loop->domain,UQ))
885 domain = cloog_domain_empty(UQ);
886 else
887 domain = cloog_domain_difference(loop->domain,UQ) ;
890 if (!cloog_domain_isempty(domain)) {
891 new_loop = cloog_loop_alloc(loop->state, domain, loop->state->one,
892 loop->state->zero, NULL, loop->inner, NULL);
893 cloog_loop_add_disjoint(&temp,&now,new_loop) ;
895 else
896 { cloog_domain_free(domain) ;
897 /* If loop->inner is no more useful, we can free it. */
898 cloog_loop_free(loop->inner) ;
901 loop->inner = NULL ;
903 if (loop->next != NULL)
904 UQ = cloog_domain_union(UQ, cloog_domain_copy(loop->domain));
905 else
906 cloog_domain_free(UQ);
908 cloog_loop_free_parts(res,1,0,0,1) ;
910 res = temp ;
912 cloog_loop_free_parts(old,1,0,0,1) ;
914 return(res) ;
918 static CloogDomain *bounding_domain(CloogDomain *dom, CloogOptions *options)
920 if (options->sh)
921 return cloog_domain_simple_convex(dom);
922 else
923 return cloog_domain_convex(dom);
928 * cloog_loop_merge function:
929 * This function is the 'soft' version of loop_separate if we are looking for
930 * a code much simpler (and less efficicient). This function returns the new
931 * CloogLoop list.
932 * - October 29th 2001: first version.
933 * - July 3rd->11th 2003: memory leaks hunt and correction.
934 * - June 22nd 2005: Adaptation for GMP.
936 CloogLoop *cloog_loop_merge(CloogLoop *loop, int level, CloogOptions *options)
938 CloogLoop *res, *new_inner, *old;
939 CloogDomain *new_domain, *temp;
941 if (loop == NULL)
942 return loop;
944 if (loop->next == NULL)
945 return cloog_loop_disjoint(loop);
947 old = loop;
948 temp = loop->domain;
949 loop->domain = NULL;
950 new_inner = loop->inner;
952 for (loop = loop->next; loop; loop = loop->next) {
953 temp = cloog_domain_union(temp, loop->domain);
954 loop->domain = NULL;
955 new_inner = cloog_loop_concat(new_inner, loop->inner);
958 new_domain = bounding_domain(temp, options);
960 if (level > 0 && !cloog_domain_is_bounded(new_domain, level) &&
961 cloog_domain_is_bounded(temp, level)) {
962 CloogDomain *splitter, *t2;
964 cloog_domain_free(new_domain);
965 splitter = cloog_domain_bound_splitter(temp, level);
967 res = NULL;
968 while (!cloog_domain_isconvex(splitter)) {
969 CloogDomain *first, *rest;
970 first = cloog_domain_cut_first(splitter, &rest);
971 splitter = rest;
972 t2 = cloog_domain_intersection(first, temp);
973 cloog_domain_free(first);
975 new_domain = bounding_domain(t2, options);
976 cloog_domain_free(t2);
978 if (cloog_domain_isempty(new_domain)) {
979 cloog_domain_free(new_domain);
980 continue;
982 res = cloog_loop_alloc(old->state, new_domain, old->state->one,
983 old->state->zero, NULL,
984 cloog_loop_copy(new_inner), res);
987 t2 = cloog_domain_intersection(splitter, temp);
988 cloog_domain_free(splitter);
990 new_domain = bounding_domain(t2, options);
991 cloog_domain_free(t2);
993 if (cloog_domain_isempty(new_domain)) {
994 cloog_domain_free(new_domain);
995 cloog_loop_free(new_inner);
996 } else
997 res = cloog_loop_alloc(old->state, new_domain, old->state->one,
998 old->state->zero, NULL, new_inner, res);
999 } else {
1000 res = cloog_loop_alloc(old->state, new_domain, old->state->one,
1001 old->state->zero, NULL, new_inner, NULL);
1003 cloog_domain_free(temp);
1005 cloog_loop_free_parts(old, 0, 0, 0, 1);
1007 return res;
1011 static int cloog_loop_count(CloogLoop *loop)
1013 int nb_loops;
1015 for (nb_loops = 0; loop; loop = loop->next)
1016 nb_loops++;
1018 return nb_loops;
1023 * cloog_loop_sort function:
1024 * Adaptation from LoopGen 0.4 by F. Quillere. This function sorts a list of
1025 * parameterized disjoint polyhedra, in order to not have lexicographic order
1026 * violation (see Quillere paper).
1027 * - September 16th 2005: inclusion of cloog_loop_number (October 29th 2001).
1029 CloogLoop *cloog_loop_sort(CloogLoop *loop, int level)
1031 CloogLoop *res, *now, **loop_array;
1032 CloogDomain **doms;
1033 int i, nb_loops=0, * permut ;
1035 /* There is no need to sort the parameter domains. */
1036 if (!level)
1037 return loop;
1039 /* We will need to know how many loops are in the list. */
1040 nb_loops = cloog_loop_count(loop);
1042 /* If there is only one loop, it's the end. */
1043 if (nb_loops == 1)
1044 return(loop) ;
1046 /* We have to allocate memory for some useful components:
1047 * - loop_array: the loop array,
1048 * - doms: the array of domains to sort,
1049 * - permut: will give us a possible sort (maybe not the only one).
1051 loop_array = (CloogLoop **)malloc(nb_loops*sizeof(CloogLoop *)) ;
1052 doms = (CloogDomain **)malloc(nb_loops*sizeof(CloogDomain *));
1053 permut = (int *)malloc(nb_loops*sizeof(int)) ;
1055 /* We fill up the loop and domain arrays. */
1056 for (i=0;i<nb_loops;i++,loop=loop->next)
1057 { loop_array[i] = loop ;
1058 doms[i] = loop_array[i]->domain;
1061 /* cloog_domain_sort will fill up permut. */
1062 cloog_domain_sort(doms, nb_loops, level, permut);
1064 /* With permut and loop_array we build the sorted list. */
1065 res = NULL ;
1066 for (i=0;i<nb_loops;i++)
1067 { /* To avoid pointer looping... loop_add will rebuild the list. */
1068 loop_array[permut[i]-1]->next = NULL ;
1069 cloog_loop_add(&res,&now,loop_array[permut[i]-1]) ;
1072 free(permut) ;
1073 free(doms);
1074 free(loop_array) ;
1076 return res;
1081 * cloog_loop_nest function:
1082 * This function changes the loop list in such a way that we have no more than
1083 * one dimension added by level. It returns an equivalent loop list with
1084 * this property.
1085 * - October 29th 2001: first version.
1086 * - July 3rd->11th 2003: memory leaks hunt and correction.
1087 * - June 22nd 2005: Adaptation for GMP.
1088 * - November 21th 2005: (debug) now OK when cloog_loop_restrict returns NULL.
1090 CloogLoop *cloog_loop_nest(CloogLoop *loop, CloogDomain *context, int level)
1091 { int l ;
1092 CloogLoop * p, * temp, * res, * now, * next ;
1093 CloogDomain * new_domain ;
1095 loop = cloog_loop_disjoint(loop);
1097 res = NULL ;
1098 /* Each domain is changed by its intersection with the context. */
1099 while (loop != NULL)
1100 { p = cloog_loop_restrict(loop, context);
1101 next = loop->next ;
1103 if (p != NULL)
1104 { cloog_loop_free_parts(loop,1,0,0,0) ;
1106 temp = cloog_loop_alloc(p->state, p->domain, p->state->one,
1107 p->state->zero, p->block, p->inner, NULL);
1109 /* If the intersection dimension is too big, we make projections smaller
1110 * and smaller, and each projection includes the preceding projection
1111 * (thus, in the target list, dimensions are added one by one).
1113 if (cloog_domain_dimension(p->domain) >= level)
1114 for (l = cloog_domain_dimension(p->domain); l >= level; l--) {
1115 new_domain = cloog_domain_project(p->domain, l);
1116 temp = cloog_loop_alloc(p->state, new_domain, p->state->one,
1117 p->state->zero, NULL, temp, NULL);
1120 /* p is no more useful (but its content yes !). */
1121 cloog_loop_free_parts(p,0,0,0,0) ;
1123 cloog_loop_add(&res,&now,temp) ;
1125 else
1126 cloog_loop_free_parts(loop,1,1,1,0) ;
1128 loop = next ;
1131 return(res) ;
1136 * cloog_loop_stride function:
1137 * This function will find the stride of a loop for the iterator at the column
1138 * number 'level' in the constraint matrix. It will update the lower bound of
1139 * the iterator accordingly. Basically, the function will try to find in the
1140 * inner loops a common condition on this iterator for the inner loop iterators
1141 * to be integral. For instance, let us consider a loop with the iterator i,
1142 * the iteration domain -4<=i<=n, and its two inner loops with the iterator j.
1143 * The first inner loop has the constraint 3j=i, and the second one has the
1144 * constraint 6j=i. Then the common constraint on i for j to be integral is
1145 * i%3=0, the stride for i is 3. Lastly, we have to find the new lower bound
1146 * for i: the first value satisfying the common constraint: -3. At the end, the
1147 * iteration domain for i is -3<=i<=n and the stride for i is 3.
1148 * - loop is the loop including the iteration domain of the considered iterator,
1149 * - level is the column number of the iterator in the matrix of contraints.
1151 * - June 29th 2003: first version (work in progress since June 26th 2003).
1152 * - July 14th 2003: simpler version.
1153 * - June 22nd 2005: Adaptation for GMP (from S. Verdoolaege's 0.12.1 version).
1155 void cloog_loop_stride(CloogLoop * loop, int level)
1156 { int first_search ;
1157 cloog_int_t stride, ref_offset, offset, potential, lower;
1158 CloogLoop * inner ;
1160 cloog_int_init(stride);
1161 cloog_int_init(ref_offset);
1162 cloog_int_init(offset);
1163 cloog_int_init(potential);
1164 cloog_int_init(lower);
1166 cloog_int_set_si(ref_offset, 0);
1167 cloog_int_set_si(offset, 0);
1168 cloog_int_set_si(lower, 0);
1170 /* Default stride. */
1171 cloog_int_set_si(stride, 1);
1172 first_search = 1 ;
1173 inner = loop->inner ;
1175 if (cloog_domain_integral_lowerbound(loop->domain,level,&lower))
1176 while (inner != NULL)
1177 { /* If the minimun stride has not been found yet, find the stride. */
1178 if ((first_search) || (!cloog_int_is_one(stride)))
1180 cloog_domain_stride(inner->domain, level, &potential, &offset);
1181 if (!cloog_int_is_one(potential) && (!first_search))
1182 { /* Offsets must be the same for common stride. */
1183 cloog_int_gcd(stride, potential, stride);
1184 if (!cloog_int_is_zero(stride)) {
1185 cloog_int_fdiv_r(offset, offset, stride);
1186 cloog_int_fdiv_r(ref_offset, ref_offset, stride);
1188 if (cloog_int_ne(offset,ref_offset))
1189 cloog_int_set_si(stride, 1);
1191 else {
1192 cloog_int_set(stride, potential);
1193 cloog_int_set(ref_offset, offset);
1196 first_search = 0 ;
1199 inner = inner->next ;
1202 if (cloog_int_is_zero(stride))
1203 cloog_int_set_si(stride, 1);
1205 /* Update the values if necessary. */
1206 if (!cloog_int_is_one(stride))
1207 { /* Update the stride value. */
1208 cloog_int_set(loop->stride, stride);
1209 if (!cloog_int_is_zero(offset))
1210 cloog_int_sub(loop->offset, stride, offset);
1213 cloog_int_clear(stride);
1214 cloog_int_clear(ref_offset);
1215 cloog_int_clear(offset);
1216 cloog_int_clear(potential);
1217 cloog_int_clear(lower);
1222 * cloog_loop_stop function:
1223 * This function implements the 'stop' option : each domain of each loop
1224 * in the list 'loop' is replaced by 'context'. 'context' should be the
1225 * domain of the outer loop. By using this method, there are no more dimensions
1226 * to scan and the simplification step will automaticaly remove the domains
1227 * since they are the same as the corresponding contexts. The effect of this
1228 * function is to stop the code generation at the level this function is called,
1229 * the resulting code do not consider the next dimensions.
1230 * - January 11th 2005: first version.
1232 CloogLoop * cloog_loop_stop(CloogLoop * loop, CloogDomain * context)
1233 { if (loop == NULL)
1234 return NULL ;
1235 else
1236 { cloog_domain_free(loop->domain) ;
1237 loop->domain = cloog_domain_copy(context) ;
1238 loop->next = cloog_loop_stop(loop->next, context) ;
1241 return loop ;
1245 static int level_is_constant(int level, int scalar, int *scaldims, int nb_scattdims)
1247 return level && (level+scalar <= nb_scattdims) && (scaldims[level+scalar-1]);
1252 * Compare the constant dimensions of loops 'l1' and 'l2' starting at 'scalar'
1253 * and return -1 if the vector of constant dimensions of 'l1' is smaller
1254 * than that of 'l2', 0 if they are the same and +1 if that of 'l1' is
1255 * greater than that of 'l2'.
1256 * This function should be called on the innermost loop (the loop
1257 * containing a block).
1258 * \param l1 Loop to be compared with l2.
1259 * \param l2 Loop to be compared with l1.
1260 * \param level Current non-scalar dimension.
1261 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1262 * \param nb_scattdims Size of the scaldims array.
1263 * \param scalar Current scalar dimension.
1264 * \return -1 if (l1 < l2), 0 if (l1 == l2) and +1 if (l1 > l2)
1266 int cloog_loop_constant_cmp(CloogLoop *l1, CloogLoop *l2, int level,
1267 int *scaldims, int nb_scattdims, int scalar)
1269 CloogBlock *b1, *b2;
1270 b1 = l1->block;
1271 b2 = l2->block;
1272 while (level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1273 int cmp = cloog_int_cmp(b1->scaldims[scalar], b2->scaldims[scalar]);
1274 if (cmp)
1275 return cmp;
1276 scalar++;
1278 return 0;
1283 * cloog_loop_scalar_gt function:
1284 * This function returns 1 if loop 'l1' is greater than loop 'l2' for the
1285 * scalar dimension vector that begins at dimension 'scalar', 0 otherwise. What
1286 * we want to know is whether a loop is scheduled before another one or not.
1287 * This function solves the problem when the considered dimension for scheduling
1288 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1289 * this function will reason about the vector of scalar dimension that begins
1290 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1291 * \param l1 Loop to be compared with l2.
1292 * \param l2 Loop to be compared with l1.
1293 * \param level Current non-scalar dimension.
1294 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1295 * \param nb_scattdims Size of the scaldims array.
1296 * \param scalar Current scalar dimension.
1297 * \return 1 if (l1 > l2), 0 otherwise.
1299 * - September 9th 2005: first version.
1300 * - October 15nd 2007: now "greater than" instead of "greater or equal".
1302 int cloog_loop_scalar_gt(l1, l2, level, scaldims, nb_scattdims, scalar)
1303 CloogLoop * l1, * l2 ;
1304 int level, * scaldims, nb_scattdims, scalar ;
1306 return cloog_loop_constant_cmp(l1, l2, level, scaldims, nb_scattdims, scalar) > 0;
1311 * cloog_loop_scalar_eq function:
1312 * This function returns 1 if loop 'l1' is equal to loop 'l2' for the scalar
1313 * dimension vector that begins at dimension 'scalar', 0 otherwise. What we want
1314 * to know is whether two loops are scheduled for the same time or not.
1315 * This function solves the problem when the considered dimension for scheduling
1316 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1317 * this function will reason about the vector of scalar dimension that begins
1318 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1319 * - l1 and l2 are the loops to compare,
1320 * - level is the current non-scalar dimension,
1321 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1322 * - nb_scattdims is the size of the scaldims array,
1323 * - scalar is the current scalar dimension.
1325 * - September 9th 2005 : first version.
1327 int cloog_loop_scalar_eq(l1, l2, level, scaldims, nb_scattdims, scalar)
1328 CloogLoop * l1, * l2 ;
1329 int level, * scaldims, nb_scattdims, scalar ;
1331 return cloog_loop_constant_cmp(l1, l2, level, scaldims, nb_scattdims, scalar) == 0;
1336 * cloog_loop_scalar_sort function:
1337 * This function sorts a linked list of loops (loop) with respect to the
1338 * scalar dimension vector that begins at dimension 'scalar'. Since there may
1339 * be a succession of scalar dimensions, this function will reason about the
1340 * vector of scalar dimension that begins at dimension 'level+scalar' and
1341 * finish to the first non-scalar dimension.
1342 * \param loop Loop list to sort.
1343 * \param level Current non-scalar dimension.
1344 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1345 * \param nb_scattdims Size of the scaldims array.
1346 * \param scalar Current scalar dimension.
1347 * \return A pointer to the sorted list.
1349 * - July 2nd 2005: first developments.
1350 * - September 2nd 2005: first version.
1351 * - October 15nd 2007: complete rewrite to remove bugs, now a bubble sort.
1353 CloogLoop * cloog_loop_scalar_sort(loop, level, scaldims, nb_scattdims, scalar)
1354 CloogLoop * loop ;
1355 int level, * scaldims, nb_scattdims, scalar ;
1356 { int ok ;
1357 CloogLoop **current;
1359 do {
1360 ok = 1;
1361 for (current = &loop; (*current)->next; current = &(*current)->next) {
1362 CloogLoop *next = (*current)->next;
1363 if (cloog_loop_scalar_gt(*current,next,level,scaldims,nb_scattdims,scalar)) {
1364 ok = 0;
1365 (*current)->next = next->next;
1366 next->next = *current;
1367 *current = next;
1370 } while (!ok);
1372 return loop ;
1377 * cloog_loop_generate_backtrack function:
1378 * adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1379 * backtrack of the Quillere et al. algorithm (see the Quillere paper).
1380 * It eliminates unused iterations of the current level for the new one. See the
1381 * example called linearity-1-1 example with and without this part for an idea.
1382 * - October 26th 2001: first version in cloog_loop_generate_general.
1383 * - July 31th 2002: (debug) no more parasite loops (REALLY hard !).
1384 * - October 30th 2005: extraction from cloog_loop_generate_general.
1386 CloogLoop *cloog_loop_generate_backtrack(CloogLoop *loop,
1387 int level, CloogOptions *options)
1389 CloogDomain * domain ;
1390 CloogLoop * now, * now2, * next, * next2, * end, * temp, * l, * inner,
1391 * new_loop ;
1393 temp = loop ;
1394 loop = NULL ;
1396 while (temp != NULL)
1397 { l = NULL ;
1398 inner = temp->inner ;
1400 while (inner != NULL)
1401 { next = inner->next ;
1402 /* This 'if' and its first part is the debug of july 31th 2002. */
1403 if (inner->block != NULL) {
1404 end = cloog_loop_alloc(temp->state, inner->domain, temp->state->one,
1405 temp->state->zero, inner->block, NULL, NULL);
1406 domain = cloog_domain_copy(temp->domain) ;
1407 new_loop = cloog_loop_alloc(temp->state, domain, temp->state->one,
1408 temp->state->zero, NULL, end, NULL);
1410 else
1411 new_loop = cloog_loop_project(inner, level);
1413 cloog_loop_free_parts(inner,0,0,0,0) ;
1414 cloog_loop_add(&l,&now2,new_loop) ;
1415 inner = next ;
1418 temp->inner = NULL ;
1420 if (l != NULL)
1421 { l = cloog_loop_separate(l) ;
1422 l = cloog_loop_sort(l, level);
1423 while (l != NULL) {
1424 cloog_int_set(l->stride, temp->stride);
1425 cloog_int_set(l->offset, temp->offset);
1426 cloog_loop_add(&loop,&now,l) ;
1427 l = l->next ;
1430 next2 = temp->next ;
1431 cloog_loop_free_parts(temp,1,0,0,0) ;
1432 temp = next2 ;
1435 return loop ;
1440 * Return 1 if we need to continue recursing to the specified level.
1442 int cloog_loop_more(CloogLoop *loop, int level, int scalar, int nb_scattdims)
1444 return level + scalar <= nb_scattdims ||
1445 cloog_domain_dimension(loop->domain) >= level;
1448 CloogLoop *cloog_loop_generate_restricted_or_stop(CloogLoop *loop,
1449 CloogDomain *context,
1450 int level, int scalar, int *scaldims, int nb_scattdims,
1451 CloogOptions *options);
1454 * cloog_loop_generate_general function:
1455 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1456 * Quillere algorithm for polyhedron scanning from step 3 to 5.
1457 * (see the Quillere paper).
1458 * - loop is the loop for which we have to generate a scanning code,
1459 * - level is the current non-scalar dimension,
1460 * - scalar is the current scalar dimension,
1461 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1462 * - nb_scattdims is the size of the scaldims array,
1463 * - options are the general code generation options.
1465 * - October 26th 2001: first version.
1466 * - July 3rd->11th 2003: memory leaks hunt and correction.
1467 * - June 22nd 2005: Adaptation for GMP.
1468 * - September 2nd 2005: The function have been cutted out in two pieces:
1469 * cloog_loop_generate and this one, in order to handle
1470 * the scalar dimension case more efficiently with
1471 * cloog_loop_generate_scalar.
1472 * - November 15th 2005: (debug) the result of the cloog_loop_generate call may
1473 * be a list of polyhedra (especially if stop option is
1474 * used): cloog_loop_add_list instead of cloog_loop_add.
1476 CloogLoop *cloog_loop_generate_general(CloogLoop *loop,
1477 int level, int scalar, int *scaldims, int nb_scattdims,
1478 CloogOptions *options)
1480 CloogLoop * res, * now, * temp, * l, * new_loop, * inner, * now2, * end,
1481 * next, * into ;
1482 CloogDomain * domain ;
1483 int separate = 0;
1485 /* 3. Separate all projections into disjoint polyhedra. */
1486 if ((options->f > level+scalar) || (options->f < 0))
1487 res = cloog_loop_merge(loop, level, options);
1488 else {
1489 res = cloog_loop_separate(loop);
1490 separate = 1;
1493 /* 3b. -correction- sort the loops to determine their textual order. */
1494 res = cloog_loop_sort(res, level);
1496 res = cloog_loop_restrict_inner(res);
1498 if (separate)
1499 res = cloog_loop_specialize(res, level, scalar, scaldims, nb_scattdims);
1501 /* 4. Recurse for each loop with the current domain as context. */
1502 temp = res ;
1503 res = NULL ;
1504 if (!level || (level+scalar < options->l) || (options->l < 0))
1505 while(temp != NULL)
1506 { if (level && options->strides)
1507 cloog_loop_stride(temp, level);
1508 inner = temp->inner ;
1509 domain = temp->domain ;
1510 into = NULL ;
1511 while (inner != NULL)
1512 { /* 4b. -ced- recurse for each sub-list of non terminal loops. */
1513 if (cloog_loop_more(inner, level + 1, scalar, nb_scattdims)) {
1514 end = inner;
1515 while ((end->next != NULL) &&
1516 cloog_loop_more(end->next, level + 1, scalar, nb_scattdims))
1517 end = end->next ;
1519 next = end->next ;
1520 end->next = NULL ;
1522 l = cloog_loop_generate_restricted_or_stop(inner, domain,
1523 level + 1, scalar, scaldims, nb_scattdims, options);
1525 if (l != NULL)
1526 cloog_loop_add_list(&into,&now,l) ;
1528 inner = next ;
1530 else
1531 { cloog_loop_add(&into,&now,inner) ;
1532 inner = inner->next ;
1535 next = temp->next ;
1536 temp->next = NULL ;
1537 temp->inner = into ;
1538 cloog_loop_add(&res,&now2,temp) ;
1539 temp = next ;
1541 else
1542 while (temp != NULL)
1543 { next = temp->next ;
1544 l = cloog_loop_nest(temp->inner, temp->domain, level+1);
1545 new_loop = cloog_loop_alloc(temp->state, temp->domain, temp->state->one,
1546 temp->state->zero, NULL, l, NULL);
1547 temp->inner = NULL ;
1548 temp->next = NULL ;
1549 cloog_loop_free_parts(temp,0,0,0,0) ;
1550 cloog_loop_add(&res,&now,new_loop) ;
1551 temp = next ;
1554 /* 5. eliminate unused iterations of the current level for the new one. See
1555 * the example called linearity-1-1 example with and without this part
1556 * for an idea.
1558 if ((!options->nobacktrack) && level &&
1559 ((level+scalar < options->l) || (options->l < 0)) &&
1560 ((options->f <= level+scalar) && !(options->f < 0)))
1561 res = cloog_loop_generate_backtrack(res, level, options);
1563 /* Pray for my new paper to be accepted somewhere since the following stuff
1564 * is really amazing :-) !
1565 * Far long later: The paper has been accepted to PACT 2004 :-))). But there
1566 * are still some bugs and I have no time to fix them. Thus now you have to
1567 * pray for me to get an academic position for that really amazing stuff :-) !
1568 * Later again: OK, I get my academic position, but still I have not enough
1569 * time to fix and clean this part... Pray again :-) !!!
1571 /* res = cloog_loop_unisolate(res,level) ;*/
1573 return(res) ;
1577 CloogLoop *cloog_loop_generate_restricted(CloogLoop *loop,
1578 int level, int scalar, int *scaldims, int nb_scattdims,
1579 CloogOptions *options);
1583 * cloog_loop_generate_scalar function:
1584 * This function applies the simplified code generation scheme in the trivial
1585 * case of scalar dimensions. When dealing with scalar dimensions, there is
1586 * no need of costly polyhedral operations for separation or sorting: sorting
1587 * is a question of comparing scalar vectors and separation amounts to consider
1588 * only loops with the same scalar vector for the next step of the code
1589 * generation process. This function achieves the separation/sorting process
1590 * for the vector of scalar dimension that begins at dimension 'level+scalar'
1591 * and finish to the first non-scalar dimension.
1592 * - loop is the loop for which we have to generate a scanning code,
1593 * - level is the current non-scalar dimension,
1594 * - scalar is the current scalar dimension,
1595 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1596 * - nb_scattdims is the size of the scaldims array,
1597 * - options are the general code generation options.
1599 * - September 2nd 2005: First version.
1601 CloogLoop *cloog_loop_generate_scalar(CloogLoop *loop,
1602 int level, int scalar, int *scaldims, int nb_scattdims,
1603 CloogOptions *options)
1604 { CloogLoop * res, * now, * temp, * l, * end, * next, * ref ;
1605 int scalar_new;
1607 /* We sort the loop list with respect to the current scalar vector. */
1608 res = cloog_loop_scalar_sort(loop,level,scaldims,nb_scattdims,scalar) ;
1610 scalar_new = scalar + scaldims[level + scalar - 1];
1612 temp = res ;
1613 res = NULL ;
1614 while (temp != NULL)
1615 { /* Then we will appy the general code generation process to each sub-list
1616 * of loops with the same scalar vector.
1618 end = temp ;
1619 ref = temp ;
1621 while((end->next != NULL) &&
1622 cloog_loop_more(end->next, level, scalar_new, nb_scattdims) &&
1623 cloog_loop_scalar_eq(ref,end->next,level,scaldims,nb_scattdims,scalar))
1624 end = end->next ;
1626 next = end->next ;
1627 end->next = NULL ;
1629 /* For the next dimension, scalar value is updated by adding the scalar
1630 * vector size, which is stored at scaldims[level+scalar-1].
1632 if (cloog_loop_more(temp, level, scalar_new, nb_scattdims)) {
1633 l = cloog_loop_generate_restricted(temp, level, scalar_new,
1634 scaldims, nb_scattdims, options);
1636 if (l != NULL)
1637 cloog_loop_add_list(&res, &now, l);
1638 } else
1639 cloog_loop_add(&res, &now, temp);
1641 temp = next ;
1644 return res ;
1648 /* Compare loop with the next loop based on their constant dimensions.
1649 * The result is < 0, == 0 or > 0 depending on whether the constant
1650 * dimensions of loop are lexicographically smaller, equal or greater
1651 * than those of loop->next.
1652 * If loop is the last in the list, then it is assumed to be smaller
1653 * than the "next" one.
1655 static int cloog_loop_next_scal_cmp(CloogLoop *loop)
1657 int i;
1658 int nb_scaldims;
1660 if (!loop->next)
1661 return -1;
1663 nb_scaldims = loop->block->nb_scaldims;
1664 if (loop->next->block->nb_scaldims < nb_scaldims)
1665 nb_scaldims = loop->next->block->nb_scaldims;
1667 for (i = 0; i < nb_scaldims; ++i) {
1668 int cmp = cloog_int_cmp(loop->block->scaldims[i],
1669 loop->next->block->scaldims[i]);
1670 if (cmp)
1671 return cmp;
1673 return loop->block->nb_scaldims - loop->next->block->nb_scaldims;
1677 /* Check whether the globally constant dimensions of a and b
1678 * have the same value for all globally constant dimensions
1679 * that are situated before any (locally) non-constant dimension.
1681 static int cloog_loop_equal_prefix(CloogLoop *a, CloogLoop *b,
1682 int *scaldims, int nb_scattdims)
1684 int i;
1685 int cst = 0;
1686 int dim = 0;
1688 for (i = 0; i < nb_scattdims; ++i) {
1689 if (!scaldims[i]) {
1690 dim++;
1691 continue;
1693 if (!cloog_int_eq(a->block->scaldims[cst], b->block->scaldims[cst]))
1694 break;
1695 cst++;
1697 for (i = i + 1; i < nb_scattdims; ++i) {
1698 if (scaldims[i])
1699 continue;
1700 if (!cloog_domain_lazy_isconstant(a->domain, dim))
1701 return 0;
1702 /* No need to check that dim is also constant in b and that the
1703 * constant values are equal. That will happen during the check
1704 * whether the two domains are equal.
1706 dim++;
1708 return 1;
1712 /* Try to block adjacent loops in the loop list "loop".
1713 * We only attempt blocking if the constant dimensions of the loops
1714 * in the least are (not necessarily strictly) increasing.
1715 * Then we look for a sublist such that the first (begin) has constant
1716 * dimensions strictly larger than the previous loop in the complete
1717 * list and such that the loop (end) after the last loop in the sublist
1718 * has constant dimensions strictly larger than the last loop in the sublist.
1719 * Furthermore, all loops in the sublist should have the same domain
1720 * (with globally constant dimensions removed) and the difference
1721 * (if any) in constant dimensions may only occur after all the
1722 * (locally) constant dimensions.
1723 * If we find such a sublist, then the blocks of all but the first
1724 * are merged into the block of the first.
1726 * Note that this function can only be called before the global
1727 * blocklist has been created because it may otherwise modify and destroy
1728 * elements on that list.
1730 CloogLoop *cloog_loop_block(CloogLoop *loop, int *scaldims, int nb_scattdims)
1732 CloogLoop *begin, *end, *l;
1733 int begin_after_previous;
1734 int end_after_previous;
1736 if (!loop->next)
1737 return loop;
1738 for (begin = loop; begin; begin = begin->next) {
1739 if (!begin->block || !begin->block->scaldims)
1740 return loop;
1741 if (cloog_loop_next_scal_cmp(loop) > 0)
1742 return loop;
1745 begin_after_previous = 1;
1746 for (begin = loop; begin; begin = begin->next) {
1747 if (!begin_after_previous) {
1748 begin_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1749 continue;
1752 end_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1753 for (end = begin->next; end; end = end->next) {
1754 if (!cloog_loop_equal_prefix(begin, end, scaldims, nb_scattdims))
1755 break;
1756 if (!cloog_domain_lazy_equal(begin->domain, end->domain))
1757 break;
1758 end_after_previous = cloog_loop_next_scal_cmp(end) < 0;
1760 if (end != begin->next && end_after_previous) {
1761 for (l = begin->next; l != end; l = begin->next) {
1762 cloog_block_merge(begin->block, l->block);
1763 begin->next = l->next;
1764 cloog_loop_free_parts(l, 1, 0, 1, 0);
1768 begin_after_previous = cloog_loop_next_scal_cmp(begin) < 0;
1771 return loop;
1776 * Check whether for any fixed iteration of the outer loops,
1777 * there is an iteration of loop1 that is lexicographically greater
1778 * than an iteration of loop2.
1779 * Return 1 if there exists (or may exist) such a pair.
1780 * Return 0 if all iterations of loop1 are lexicographically smaller
1781 * than the iterations of loop2.
1782 * If no iteration is lexicographically greater, but if there are
1783 * iterations that are equal to iterations of loop2, then return "def".
1784 * This is useful for ensuring that such statements are not reordered.
1785 * Some users, including the test_run target in test, expect
1786 * the statements at a given point to be run in the original order.
1787 * Passing the value "0" for "def" would allow such statements to be reordered
1788 * and would allow for the detection of more components.
1790 int cloog_loop_follows(CloogLoop *loop1, CloogLoop *loop2,
1791 int level, int scalar, int *scaldims, int nb_scattdims, int def)
1793 int dim1, dim2;
1795 dim1 = cloog_domain_dimension(loop1->domain);
1796 dim2 = cloog_domain_dimension(loop2->domain);
1797 while ((level <= dim1 && level <= dim2) ||
1798 level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1799 if (level_is_constant(level, scalar, scaldims, nb_scattdims)) {
1800 int cmp = cloog_loop_constant_cmp(loop1, loop2, level, scaldims,
1801 nb_scattdims, scalar);
1802 if (cmp > 0)
1803 return 1;
1804 if (cmp < 0)
1805 return 0;
1806 scalar += scaldims[level + scalar - 1];
1807 } else {
1808 int follows = cloog_domain_follows(loop1->domain, loop2->domain,
1809 level);
1810 if (follows > 0)
1811 return 1;
1812 if (follows < 0)
1813 return 0;
1814 level++;
1818 return def;
1822 /* Structure for representing the nodes in the graph being traversed
1823 * using Tarjan's algorithm.
1824 * index represents the order in which nodes are visited.
1825 * min_index is the index of the root of a (sub)component.
1826 * on_stack indicates whether the node is currently on the stack.
1828 struct cloog_loop_sort_node {
1829 int index;
1830 int min_index;
1831 int on_stack;
1833 /* Structure for representing the graph being traversed
1834 * using Tarjan's algorithm.
1835 * len is the number of nodes
1836 * node is an array of nodes
1837 * stack contains the nodes on the path from the root to the current node
1838 * sp is the stack pointer
1839 * index is the index of the last node visited
1840 * order contains the elements of the components separated by -1
1841 * op represents the current position in order
1843 struct cloog_loop_sort {
1844 int len;
1845 struct cloog_loop_sort_node *node;
1846 int *stack;
1847 int sp;
1848 int index;
1849 int *order;
1850 int op;
1853 /* Allocate and initialize cloog_loop_sort structure.
1855 static struct cloog_loop_sort *cloog_loop_sort_alloc(int len)
1857 struct cloog_loop_sort *s;
1858 int i;
1860 s = (struct cloog_loop_sort *)malloc(sizeof(struct cloog_loop_sort));
1861 assert(s);
1862 s->len = len;
1863 s->node = (struct cloog_loop_sort_node *)
1864 malloc(len * sizeof(struct cloog_loop_sort_node));
1865 assert(s->node);
1866 for (i = 0; i < len; ++i)
1867 s->node[i].index = -1;
1868 s->stack = (int *)malloc(len * sizeof(int));
1869 assert(s->stack);
1870 s->order = (int *)malloc(2 * len * sizeof(int));
1871 assert(s->order);
1873 s->sp = 0;
1874 s->index = 0;
1875 s->op = 0;
1877 return s;
1880 /* Free cloog_loop_sort structure.
1882 static void cloog_loop_sort_free(struct cloog_loop_sort *s)
1884 free(s->node);
1885 free(s->stack);
1886 free(s->order);
1887 free(s);
1891 /* Check whether for any fixed iteration of the outer loops,
1892 * there is an iteration of loop1 that is lexicographically greater
1893 * than an iteration of loop2, where the iteration domains are
1894 * available in the inner loops of the arguments.
1896 * By using this functions to detect components, we ensure that
1897 * two CloogLoops appear in the same component if some iterations of
1898 * each loop should be executed before some iterations of the other loop.
1899 * Since we also want two CloogLoops that have exactly the same
1900 * iteration domain at the current level to be placed in the same component,
1901 * we first check if these domains are indeed the same.
1903 static int inner_loop_follows(CloogLoop *loop1, CloogLoop *loop2,
1904 int level, int scalar, int *scaldims, int nb_scattdims, int def)
1906 int f;
1908 f = cloog_domain_lazy_equal(loop1->domain, loop2->domain);
1909 if (!f)
1910 f = cloog_loop_follows(loop1->inner, loop2->inner,
1911 level, scalar, scaldims, nb_scattdims, def);
1913 return f;
1917 /* Perform Tarjan's algorithm for computing the strongly connected components
1918 * in the graph with the individual CloogLoops as vertices.
1919 * Two CloopLoops appear in the same component if they both (indirectly)
1920 * "follow" each other, where the following relation is determined
1921 * by the follows function.
1923 static void cloog_loop_components_tarjan(struct cloog_loop_sort *s,
1924 CloogLoop **loop_array, int i, int level, int scalar, int *scaldims,
1925 int nb_scattdims,
1926 int (*follows)(CloogLoop *loop1, CloogLoop *loop2,
1927 int level, int scalar, int *scaldims, int nb_scattdims, int def))
1929 int j;
1931 s->node[i].index = s->index;
1932 s->node[i].min_index = s->index;
1933 s->node[i].on_stack = 1;
1934 s->index++;
1935 s->stack[s->sp++] = i;
1937 for (j = s->len - 1; j >= 0; --j) {
1938 int f;
1940 if (j == i)
1941 continue;
1942 if (s->node[j].index >= 0 &&
1943 (!s->node[j].on_stack ||
1944 s->node[j].index > s->node[i].min_index))
1945 continue;
1947 f = follows(loop_array[i], loop_array[j],
1948 level, scalar, scaldims, nb_scattdims, i > j);
1949 if (!f)
1950 continue;
1952 if (s->node[j].index < 0) {
1953 cloog_loop_components_tarjan(s, loop_array, j, level, scalar,
1954 scaldims, nb_scattdims, follows);
1955 if (s->node[j].min_index < s->node[i].min_index)
1956 s->node[i].min_index = s->node[j].min_index;
1957 } else if (s->node[j].index < s->node[i].min_index)
1958 s->node[i].min_index = s->node[j].index;
1961 if (s->node[i].index != s->node[i].min_index)
1962 return;
1964 do {
1965 j = s->stack[--s->sp];
1966 s->node[j].on_stack = 0;
1967 s->order[s->op++] = j;
1968 } while (j != i);
1969 s->order[s->op++] = -1;
1973 static int qsort_index_cmp(const void *p1, const void *p2)
1975 return *(int *)p1 - *(int *)p2;
1978 /* Sort the elements of the component starting at list.
1979 * The list is terminated by a -1.
1981 static void sort_component(int *list)
1983 int len;
1985 for (len = 0; list[len] != -1; ++len)
1988 qsort(list, len, sizeof(int), qsort_index_cmp);
1991 /* Given an array of indices "list" into the "loop_array" array,
1992 * terminated by -1, construct a linked list of the corresponding
1993 * entries and put the result in *res.
1994 * The value returned is the number of CloogLoops in the (linked) list
1996 static int extract_component(CloogLoop **loop_array, int *list, CloogLoop **res)
1998 int i = 0;
2000 sort_component(list);
2001 while (list[i] != -1) {
2002 *res = loop_array[list[i]];
2003 res = &(*res)->next;
2004 ++i;
2006 *res = NULL;
2008 return i;
2013 * Call cloog_loop_generate_scalar or cloog_loop_generate_general
2014 * on each of the strongly connected components in the list of CloogLoops
2015 * pointed to by "loop".
2017 * We use Tarjan's algorithm to find the strongly connected components.
2018 * Note that this algorithm also topologically sorts the components.
2020 * The components are treated separately to avoid spurious separations.
2021 * The concatentation of the results may contain successive loops
2022 * with the same bounds, so we try to combine such loops.
2024 CloogLoop *cloog_loop_generate_components(CloogLoop *loop,
2025 int level, int scalar, int *scaldims, int nb_scattdims,
2026 CloogOptions *options)
2028 int i, nb_loops;
2029 CloogLoop *tmp;
2030 CloogLoop *res, **res_next;
2031 CloogLoop **loop_array;
2032 struct cloog_loop_sort *s;
2034 if (level == 0 || !loop->next)
2035 return cloog_loop_generate_general(loop, level, scalar,
2036 scaldims, nb_scattdims, options);
2038 nb_loops = cloog_loop_count(loop);
2040 loop_array = (CloogLoop **)malloc(nb_loops * sizeof(CloogLoop *));
2041 assert(loop_array);
2043 for (i = 0, tmp = loop; i < nb_loops; i++, tmp = tmp->next)
2044 loop_array[i] = tmp;
2046 s = cloog_loop_sort_alloc(nb_loops);
2047 for (i = nb_loops - 1; i >= 0; --i) {
2048 if (s->node[i].index >= 0)
2049 continue;
2050 cloog_loop_components_tarjan(s, loop_array, i, level, scalar, scaldims,
2051 nb_scattdims, &inner_loop_follows);
2054 i = 0;
2055 res = NULL;
2056 res_next = &res;
2057 while (nb_loops) {
2058 int n = extract_component(loop_array, &s->order[i], &tmp);
2059 i += n + 1;
2060 nb_loops -= n;
2061 *res_next = cloog_loop_generate_general(tmp, level, scalar,
2062 scaldims, nb_scattdims, options);
2063 while (*res_next)
2064 res_next = &(*res_next)->next;
2067 cloog_loop_sort_free(s);
2069 free(loop_array);
2071 res = cloog_loop_combine(res);
2073 return res;
2077 /* For each loop in the list "loop", decompose the list of
2078 * inner loops into strongly connected components and put
2079 * the components into separate loops at the top level.
2081 CloogLoop *cloog_loop_decompose_inner(CloogLoop *loop,
2082 int level, int scalar, int *scaldims, int nb_scattdims)
2084 CloogLoop *l, *tmp;
2085 CloogLoop **loop_array;
2086 int i, n_loops, max_loops = 0;
2087 struct cloog_loop_sort *s;
2089 for (l = loop; l; l = l->next) {
2090 n_loops = cloog_loop_count(l->inner);
2091 if (max_loops < n_loops)
2092 max_loops = n_loops;
2095 if (max_loops <= 1)
2096 return loop;
2098 loop_array = (CloogLoop **)malloc(max_loops * sizeof(CloogLoop *));
2099 assert(loop_array);
2101 for (l = loop; l; l = l->next) {
2102 int n;
2104 for (i = 0, tmp = l->inner; tmp; i++, tmp = tmp->next)
2105 loop_array[i] = tmp;
2106 n_loops = i;
2107 if (n_loops <= 1)
2108 continue;
2110 s = cloog_loop_sort_alloc(n_loops);
2111 for (i = n_loops - 1; i >= 0; --i) {
2112 if (s->node[i].index >= 0)
2113 continue;
2114 cloog_loop_components_tarjan(s, loop_array, i, level, scalar,
2115 scaldims, nb_scattdims, &cloog_loop_follows);
2118 n = extract_component(loop_array, s->order, &l->inner);
2119 n_loops -= n;
2120 i = n + 1;
2121 while (n_loops) {
2122 CloogLoop *inner;
2124 n = extract_component(loop_array, &s->order[i], &inner);
2125 n_loops -= n;
2126 i += n + 1;
2127 tmp = cloog_loop_alloc(l->state, cloog_domain_copy(l->domain),
2128 l->stride, l->offset, l->block, inner, l->next);
2129 l->next = tmp;
2130 l = tmp;
2133 cloog_loop_sort_free(s);
2136 free(loop_array);
2138 return loop;
2142 CloogLoop *cloog_loop_generate_restricted(CloogLoop *loop,
2143 int level, int scalar, int *scaldims, int nb_scattdims,
2144 CloogOptions *options)
2146 /* To save both time and memory, we switch here depending on whether the
2147 * current dimension is scalar (simplified processing) or not (general
2148 * processing).
2150 if (level_is_constant(level, scalar, scaldims, nb_scattdims))
2151 return cloog_loop_generate_scalar(loop, level, scalar,
2152 scaldims, nb_scattdims, options);
2154 * 2. Compute the projection of each polyhedron onto the outermost
2155 * loop variable and the parameters.
2157 loop = cloog_loop_project_all(loop, level);
2159 return cloog_loop_generate_components(loop, level, scalar, scaldims,
2160 nb_scattdims, options);
2164 CloogLoop *cloog_loop_generate_restricted_or_stop(CloogLoop *loop,
2165 CloogDomain *context,
2166 int level, int scalar, int *scaldims, int nb_scattdims,
2167 CloogOptions *options)
2169 /* If the user asked to stop code generation at this level, let's stop. */
2170 if ((options->stop >= 0) && (level+scalar >= options->stop+1))
2171 return cloog_loop_stop(loop,context) ;
2173 return cloog_loop_generate_restricted(loop, level, scalar, scaldims,
2174 nb_scattdims, options);
2179 * cloog_loop_generate function:
2180 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
2181 * Quillere algorithm for polyhedron scanning from step 1 to 2.
2182 * (see the Quillere paper).
2183 * - loop is the loop for which we have to generate a scanning code,
2184 * - context is the context of the current loop (constraints on parameter and/or
2185 * on outer loop counters),
2186 * - level is the current non-scalar dimension,
2187 * - scalar is the current scalar dimension,
2188 * - scaldims is the boolean array saying whether a dimension is scalar or not,
2189 * - nb_scattdims is the size of the scaldims array,
2190 * - options are the general code generation options.
2192 * - October 26th 2001: first version.
2193 * - July 3rd->11th 2003: memory leaks hunt and correction.
2194 * - June 15th 2005: a memory leak fixed (loop was not entirely freed when
2195 * the result of cloog_loop_restrict was NULL).
2196 * - June 22nd 2005: Adaptation for GMP.
2197 * - September 2nd 2005: The function have been cutted out in two pieces:
2198 * cloog_loop_generate and this one, in order to handle
2199 * the scalar dimension case more efficiently with
2200 * cloog_loop_generate_scalar.
2201 * - November 15th 2005: (debug) Condition for stop option no more take care of
2202 * further scalar dimensions.
2204 CloogLoop *cloog_loop_generate(CloogLoop *loop, CloogDomain *context,
2205 int level, int scalar, int *scaldims, int nb_scattdims,
2206 CloogOptions *options)
2208 /* 1. Replace each polyhedron by its intersection with the context.
2210 loop = cloog_loop_restrict_all(loop, context);
2211 if (!loop)
2212 return NULL;
2214 return cloog_loop_generate_restricted_or_stop(loop, context,
2215 level, scalar, scaldims, nb_scattdims, options);
2220 * Internal function for simplifying a single loop in a list of loops.
2221 * See cloog_loop_simplify.
2223 static CloogLoop *loop_simplify(CloogLoop *loop, CloogDomain *context,
2224 int level)
2226 int domain_dim;
2227 CloogBlock * new_block ;
2228 CloogLoop *simplified, *inner;
2229 CloogDomain * domain, * simp, * inter, * extended_context ;
2231 if (!cloog_domain_isconvex(loop->domain))
2232 loop->domain = cloog_domain_simplify_union(loop->domain);
2234 domain = loop->domain ;
2236 domain_dim = cloog_domain_dimension(domain);
2237 extended_context = cloog_domain_extend(context, domain_dim);
2238 inter = cloog_domain_intersection(domain,extended_context) ;
2239 simp = cloog_domain_simplify(inter,extended_context) ;
2240 cloog_domain_free(extended_context) ;
2242 /* If the constraint system is never true, go to the next one. */
2243 if (cloog_domain_never_integral(simp)) {
2244 cloog_loop_free(loop->inner);
2245 cloog_domain_free(inter);
2246 cloog_domain_free(simp);
2247 return NULL;
2250 inner = cloog_loop_simplify(loop->inner, inter, level+1);
2251 cloog_domain_free(inter) ;
2253 if ((inner == NULL) && (loop->block == NULL)) {
2254 cloog_domain_free(simp);
2255 return NULL;
2258 new_block = cloog_block_copy(loop->block) ;
2260 simplified = cloog_loop_alloc(loop->state, simp, loop->stride, loop->offset,
2261 new_block, inner, NULL);
2263 return(simplified) ;
2268 * cloog_loop_simplify function:
2269 * This function implements the part 6. of the Quillere algorithm, it
2270 * recursively simplifies each loop in the context of the preceding loop domain.
2271 * It returns a pointer to the simplified loop list.
2272 * The cloog_domain_simplify (DomainSimplify) behaviour is really bad with
2273 * polyhedra union and some really awful sidesteppings were written, I plan
2274 * to solve that...
2275 * - October 31th 2001: first version.
2276 * - July 3rd->11th 2003: memory leaks hunt and correction.
2277 * - April 16th 2005: a memory leak fixed (extended_context was not freed).
2278 * - June 15th 2005: a memory leak fixed (loop was not conveniently freed
2279 * when the constraint system is never true).
2280 * - October 27th 2005: - this function called before cloog_loop_fast_simplify
2281 * is now the official cloog_loop_simplify function in
2282 * replacement of a slower and more complex one (after
2283 * deep changes in the pretty printer).
2284 * - we use cloog_loop_disjoint to fix the problem when
2285 * simplifying gives a union of polyhedra (before, it
2286 * was under the responsibility of the pretty printer).
2288 CloogLoop *cloog_loop_simplify(CloogLoop *loop, CloogDomain *context, int level)
2290 CloogLoop *now;
2291 CloogLoop *res = NULL;
2292 CloogLoop **next = &res;
2294 for (now = loop; now; now = now->next) {
2295 *next = loop_simplify(now, context, level);
2297 now->inner = NULL; /* For loop integrity. */
2298 cloog_domain_free(now->domain);
2299 now->domain = NULL;
2301 if (*next)
2302 next = &(*next)->next;
2304 cloog_loop_free(loop);
2306 /* Examples like test/iftest2.cloog give unions of polyhedra after
2307 * simplifying, thus we we have to disjoint them. Another good reason to
2308 * put the simplifying step in the Quillere backtrack.
2310 res = cloog_loop_disjoint(res);
2312 return res;
2317 * cloog_loop_scatter function:
2318 * This function add the scattering (scheduling) informations in a loop.
2320 void cloog_loop_scatter(CloogLoop * loop, CloogScattering *scatt)
2322 loop->domain = cloog_domain_scatter(loop->domain, scatt);