2 /**-------------------------------------------------------------------**
4 **-------------------------------------------------------------------**
6 **-------------------------------------------------------------------**
7 ** First version: october 26th 2001 **
8 **-------------------------------------------------------------------**/
11 /******************************************************************************
12 * CLooG : the Chunky Loop Generator (experimental) *
13 ******************************************************************************
15 * Copyright (C) 2001-2005 Cedric Bastoul *
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. *
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. *
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 *
32 * CLooG, the Chunky Loop Generator *
33 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
35 ******************************************************************************/
36 /* CAUTION: the english used for comments is probably the worst you ever read,
37 * please feel free to correct and improve it !
42 # include "../include/cloog/cloog.h"
45 /******************************************************************************
46 * Memory leaks hunting *
47 ******************************************************************************/
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
)
77 /******************************************************************************
78 * Structure display function *
79 ******************************************************************************/
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),
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
)
98 { /* Go to the right level. */
99 for (i
=0; i
<level
; i
++)
100 fprintf(file
,"|\t") ;
102 fprintf(file
,"+-- CloogLoop\n") ;
108 { /* Go to the right level. */
109 for (i
=0; i
<level
; i
++)
110 fprintf(file
,"|\t") ;
112 fprintf(file
,"| CloogLoop\n") ;
118 for(j
=0; j
<=level
+1; j
++)
119 fprintf(file
,"|\t") ;
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") ;
136 for(j
=0; j
<=level
+1; j
++)
137 fprintf(file
,"|\t") ;
140 /* Print the block. */
141 cloog_block_print_structure(file
,loop
->block
,level
+1) ;
144 for (i
=0; i
<=level
+1; i
++)
145 fprintf(file
,"|\t") ;
148 /* Print inner if any. */
150 cloog_loop_print_structure(file
,loop
->inner
,level
+1) ;
152 /* And let's go for the next one. */
155 /* One more time something that is here only for a better look. */
157 { /* Two blank lines if this is the end of the linked list. */
159 { for (i
=0; i
<=level
; i
++)
160 fprintf(file
,"|\t") ;
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
)
202 while (loop
!= NULL
) {
203 cloog_loop_leak_down(loop
->state
);
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
);
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
)
230 int domain
, block
, inner
, next
;
231 { CloogLoop
* follow
;
233 while (loop
!= NULL
) {
234 cloog_loop_leak_down(loop
->state
);
235 follow
= loop
->next
;
238 cloog_domain_free(loop
->domain
) ;
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
);
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
;
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
)) ;
288 cloog_die("memory overflow.\n");
291 loop
->domain
= cloog_domain_union_read(state
, foo
, nb_parameters
);
292 if (loop
->domain
!= NULL
)
293 nb_iterators
= cloog_domain_dimension(loop
->domain
);
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
);
305 /* inner is NULL at beginning. */
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
) ;
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
329 * - November 21th 2005: first version.
331 CloogLoop
*cloog_loop_malloc(CloogState
*state
)
334 /* Memory allocation for the CloogLoop structure. */
335 loop
= (CloogLoop
*)malloc(sizeof(CloogLoop
)) ;
337 cloog_die("memory overflow.\n");
338 cloog_loop_leak_up(state
);
341 /* We set the various fields with default values. */
343 loop
->domain
= 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);
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
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
)
371 loop
= cloog_loop_malloc(state
);
373 loop
->domain
= domain
;
374 loop
->block
= block
;
375 loop
->inner
= inner
;
377 cloog_int_set(loop
->stride
, stride
);
378 cloog_int_set(loop
->offset
, offset
);
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
)
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
411 * - September 9th 2005: first version.
413 void cloog_loop_add_list(CloogLoop
** start
, CloogLoop
** now
, CloogLoop
* loop
)
414 { if (*start
== NULL
)
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
)
439 CloogDomain
* domain
;
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
) ;
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
, *seen_before
, *temp
, *rest
;
480 if (cloog_domain_isconvex(loop
->domain
))
481 cloog_loop_add(start
,now
,loop
) ;
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
);
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
,
515 /* domain can be an union too. If so: recursion. */
516 if (cloog_domain_isconvex(domain
))
517 cloog_loop_add(start
,now
,sep
) ;
519 cloog_loop_add_disjoint(start
,now
,sep
) ;
521 if (cloog_domain_isempty(rest
)) {
522 cloog_domain_free(domain
);
527 seen
= cloog_domain_union(seen_before
, domain
);
528 cloog_domain_free(domain
);
529 cloog_domain_free(seen_before
);
531 cloog_domain_free(rest
);
532 cloog_domain_free(seen
);
533 cloog_loop_free_parts(loop
,0,0,0,0) ;
539 * cloog_loop_disjoint function:
540 * This function returns a list of loops such that each loop with non-convex
541 * domain in the input list (loop) is separated into several loops where the
542 * domains are the components of the union of *disjoint* polyhedra equivalent
543 * to the original non-convex domain. See cloog_loop_add_disjoint comments
545 * - September 16th 2005: first version.
547 CloogLoop
* cloog_loop_disjoint(CloogLoop
* loop
)
548 { CloogLoop
*res
=NULL
, * now
=NULL
, * next
;
550 /* Because this is often the case, don't waste time ! */
551 if (loop
&& !loop
->next
&& cloog_domain_isconvex(loop
->domain
))
555 { next
= loop
->next
;
557 cloog_loop_add_disjoint(&res
,&now
,loop
) ;
566 * cloog_loop_restrict function:
567 * This function returns the (loop) in the context of (context): it makes the
568 * intersection between the (loop) domain and the (context), then it returns
569 * a pointer to a new loop, with this intersection as domain.
570 * - nb_par is the number of parameters.
572 * - October 27th 2001: first version.
573 * - June 15th 2005: a memory leak fixed (domain was not freed when empty).
574 * - June 22nd 2005: Adaptation for GMP.
576 CloogLoop
* cloog_loop_restrict(loop
, context
, nb_par
)
578 CloogDomain
* context
;
580 { int new_dimension
;
581 CloogDomain
* domain
, * extended_context
, * new_domain
;
582 CloogLoop
* new_loop
;
584 domain
= loop
->domain
;
585 if (cloog_domain_dimension(domain
) > cloog_domain_dimension(context
))
587 new_dimension
= cloog_domain_dimension(domain
);
588 extended_context
= cloog_domain_extend(context
, new_dimension
);
589 new_domain
= cloog_domain_intersection(extended_context
,loop
->domain
) ;
590 cloog_domain_free(extended_context
) ;
593 new_domain
= cloog_domain_intersection(context
,loop
->domain
) ;
595 if (cloog_domain_isempty(new_domain
))
596 { cloog_domain_free(new_domain
) ;
600 new_loop
= cloog_loop_alloc(loop
->state
, new_domain
,
601 loop
->state
->one
, loop
->state
->zero
,
602 loop
->block
, loop
->inner
, NULL
);
609 * cloog_loop_project function:
610 * This function returns the projection of (loop) on the (level) first
611 * dimensions (outer loops). It makes the projection of the (loop) domain,
612 * then it returns a pointer to a new loop, with this projection as domain.
613 * - nb_par is the number of parameters.
615 * - October 27th 2001: first version.
616 * - July 3rd->11th 2003: memory leaks hunt and correction.
617 * - June 22nd 2005: Adaptation for GMP.
619 CloogLoop
* cloog_loop_project(CloogLoop
* loop
, int level
, int nb_par
)
621 CloogDomain
* new_domain
;
622 CloogLoop
* new_loop
, * copy
;
624 copy
= cloog_loop_alloc(loop
->state
, loop
->domain
, loop
->stride
, loop
->offset
,
625 loop
->block
, loop
->inner
, NULL
);
627 if (cloog_domain_dimension(loop
->domain
) == level
)
628 new_domain
= cloog_domain_copy(loop
->domain
) ;
630 new_domain
= cloog_domain_project(loop
->domain
, level
);
632 new_loop
= cloog_loop_alloc(loop
->state
, new_domain
, loop
->state
->one
,
633 loop
->state
->zero
, NULL
, copy
, NULL
);
640 * cloog_loop_concat function:
641 * This function returns a pointer to the concatenation of the
642 * CloogLoop lists given as input.
643 * - October 28th 2001: first version.
645 CloogLoop
* cloog_loop_concat(CloogLoop
* a
, CloogLoop
* b
)
646 { CloogLoop
* loop
, * temp
;
651 { while (temp
->next
!= NULL
)
663 * cloog_loop_combine:
664 * Combine consecutive loops with identical domains into
665 * a single loop with the concatenation of their inner loops
668 CloogLoop
*cloog_loop_combine(CloogLoop
*loop
)
670 CloogLoop
*first
, *second
;
672 for (first
= loop
; first
; first
= first
->next
) {
673 while (first
->next
) {
674 if (!cloog_domain_lazy_equal(first
->domain
, first
->next
->domain
))
676 second
= first
->next
;
677 first
->inner
= cloog_loop_concat(first
->inner
, second
->inner
);
678 first
->next
= second
->next
;
679 cloog_loop_free_parts(second
, 1, 0, 0, 0);
687 * cloog_loop_separate function:
688 * This function implements the Quillere algorithm for separation of multiple
689 * loops: for a given set of polyhedra (loop), it computes a set of disjoint
690 * polyhedra such that the unions of these sets are equal, and returns this set.
691 * - October 28th 2001: first version.
692 * - November 14th 2001: elimination of some unused blocks.
693 * - August 13th 2002: (debug) in the case of union of polyhedra for one
694 * loop, redundant constraints are fired.
695 * - July 3rd->11th 2003: memory leaks hunt and correction.
696 * - June 22nd 2005: Adaptation for GMP.
697 * - October 16th 2005: Removal of the non-shared constraint elimination when
698 * there is only one loop in the list (seems to work
699 * without now, DomainSimplify may have been improved).
700 * The problem was visible with test/iftest2.cloog.
702 CloogLoop
* cloog_loop_separate(CloogLoop
* loop
)
703 { int lazy_equal
=0, disjoint
= 0;
704 CloogLoop
* new_loop
, * new_inner
, * res
, * now
, * temp
, * Q
,
705 * inner
, * old
/*, * previous, * next*/ ;
706 CloogDomain
* UQ
, * old_UQ
, * domain
;
711 loop
= cloog_loop_combine(loop
);
713 if (loop
->next
== NULL
)
714 return cloog_loop_disjoint(loop
) ;
716 UQ
= cloog_domain_copy(loop
->domain
) ;
717 domain
= cloog_domain_copy(loop
->domain
) ;
718 res
= cloog_loop_alloc(loop
->state
, domain
, loop
->state
->one
,
719 loop
->state
->zero
, loop
->block
, loop
->inner
, NULL
);
722 while((loop
= loop
->next
) != NULL
)
725 /* For all Q, add Q-loop associated with the blocks of Q alone,
726 * and Q inter loop associated with the blocks of Q and loop.
728 for (Q
= res
; Q
; Q
= Q
->next
) {
729 /* Add (Q inter loop). */
730 if ((disjoint
= cloog_domain_lazy_disjoint(Q
->domain
,loop
->domain
)))
733 { if ((lazy_equal
= cloog_domain_lazy_equal(Q
->domain
,loop
->domain
)))
734 domain
= cloog_domain_copy(Q
->domain
) ;
736 domain
= cloog_domain_intersection(Q
->domain
,loop
->domain
) ;
738 if (!cloog_domain_isempty(domain
))
739 { new_inner
= cloog_loop_concat(cloog_loop_copy(Q
->inner
),
740 cloog_loop_copy(loop
->inner
)) ;
741 new_loop
= cloog_loop_alloc(loop
->state
, domain
, loop
->state
->one
,
742 loop
->state
->zero
, NULL
, new_inner
, NULL
);
743 cloog_loop_add_disjoint(&temp
,&now
,new_loop
) ;
747 cloog_domain_free(domain
);
751 /* Add (Q - loop). */
753 domain
= cloog_domain_copy(Q
->domain
) ;
756 domain
= cloog_domain_empty(Q
->domain
);
758 domain
= cloog_domain_difference(Q
->domain
,loop
->domain
) ;
761 if (!cloog_domain_isempty(domain
)) {
762 new_loop
= cloog_loop_alloc(loop
->state
, domain
, loop
->state
->one
,
763 loop
->state
->zero
, NULL
, Q
->inner
, NULL
);
764 cloog_loop_add_disjoint(&temp
,&now
,new_loop
) ;
767 { cloog_domain_free(domain
) ;
768 /* If Q->inner is no more useful, we can free it. */
771 cloog_loop_free(inner
) ;
775 /* Add loop-UQ associated with the blocks of loop alone.*/
776 if (cloog_domain_lazy_disjoint(loop
->domain
,UQ
))
777 domain
= cloog_domain_copy(loop
->domain
) ;
779 { if (cloog_domain_lazy_equal(loop
->domain
,UQ
))
780 domain
= cloog_domain_empty(UQ
);
782 domain
= cloog_domain_difference(loop
->domain
,UQ
) ;
785 if (!cloog_domain_isempty(domain
)) {
786 new_loop
= cloog_loop_alloc(loop
->state
, domain
, loop
->state
->one
,
787 loop
->state
->zero
, NULL
, loop
->inner
, NULL
);
788 cloog_loop_add_disjoint(&temp
,&now
,new_loop
) ;
791 { cloog_domain_free(domain
) ;
792 /* If loop->inner is no more useful, we can free it. */
793 cloog_loop_free(loop
->inner
) ;
799 if (loop
->next
!= NULL
)
800 UQ
= cloog_domain_union(UQ
,loop
->domain
) ;
802 cloog_domain_free(old_UQ
) ;
803 cloog_loop_free_parts(res
,1,0,0,1) ;
807 cloog_loop_free_parts(old
,1,0,0,1) ;
814 * cloog_loop_merge_list
815 * Merge two lists of CloogLoops. The new list contains the
816 * elements of the two lists in the same order, but they may
818 * In particular, if the elements of a and b are ordered
819 * according to the inner loops of the order list, then so are the elements
822 static CloogLoop
*cloog_loop_merge_inner_list(CloogLoop
*a
, CloogLoop
*b
,
825 CloogLoop
*loop
, **next
;
828 for ( ; order
&& (a
||b
); order
= order
->next
) {
829 if (a
&& order
->inner
->block
== a
->block
) {
832 next
= &(*next
)->next
;
835 if (b
&& order
->inner
->block
== b
->block
) {
838 next
= &(*next
)->next
;
845 * cloog_loop_merge function:
846 * This function is the 'soft' version of loop_separate if we are looking for
847 * a code much simpler (and less efficicient). Here we merge loops if they have
848 * common parts in the iteration space (if the intersection of their domains is
849 * not empty), and let them isolated otherwise. This function returns the new
851 * - October 29th 2001: first version.
852 * - July 3rd->11th 2003: memory leaks hunt and correction.
853 * - June 22nd 2005: Adaptation for GMP.
855 CloogLoop
* cloog_loop_merge(CloogLoop
* loop
, int nb_par
, CloogOptions
* options
)
857 CloogLoop
* res
, * merge
, * now
, * Q
, * P
, * new_inner
, * next
, * old
;
858 CloogDomain
* new_domain
, * temp
;
863 if (loop
->next
== NULL
)
864 return cloog_loop_disjoint(loop
);
866 /* First loop is added to the target list. */
867 res
= cloog_loop_alloc(loop
->state
, loop
->domain
, loop
->state
->one
,
868 loop
->state
->zero
, loop
->block
, loop
->inner
, NULL
);
870 /* Now the domain is in 'res' and it will be freed. */
871 loop
->domain
= NULL
;
873 /* And one by one, we see if we have to merge or to add the other loops. */
874 while((loop
= loop
->next
) != NULL
)
876 P
= cloog_loop_alloc(loop
->state
, loop
->domain
, loop
->state
->one
,
877 loop
->state
->zero
, loop
->block
, loop
->inner
, NULL
);
879 /* Now the domain is in 'P' and it will be freed. */
880 loop
->domain
= NULL
;
882 /* For each loop in the target list, if the intersection with the new loop
883 * is empty, we can add the new loop directly, otherwise, we can merge then
887 { temp
= cloog_domain_intersection(Q
->domain
,P
->domain
) ;
889 if (cloog_domain_isempty(temp
))
890 { cloog_domain_free(temp
) ;
891 cloog_loop_add_disjoint(&merge
,&now
,Q
) ;
894 { cloog_domain_free(temp
) ;
895 new_inner
= cloog_loop_merge_inner_list(Q
->inner
, P
->inner
, old
);
896 temp
= cloog_domain_union(P
->domain
,Q
->domain
) ;
898 new_domain
= cloog_domain_simple_convex(temp
);
900 new_domain
= cloog_domain_convex(temp
);
901 cloog_domain_free(temp
) ;
902 /* Q and P are no more used (but their content yes !).*/
903 cloog_loop_free_parts(P
,1,0,0,0) ;
904 cloog_loop_free_parts(Q
,1,0,0,0) ;
905 P
= cloog_loop_alloc(loop
->state
, new_domain
, loop
->state
->one
,
906 loop
->state
->zero
, NULL
, new_inner
, NULL
);
911 /* If there was merging, add it, otherwise add the loop lonely.
912 * DEBUG : ici pas besoin de s'assurer que P->next est NULL (possible que
913 * non si pas de fusion) car le dernier loop etudie a loop->next = NULL.
915 cloog_loop_add_disjoint(&merge
,&now
,P
) ;
918 cloog_loop_free_parts(old
,0,0,0,1) ;
925 * cloog_loop_sort function:
926 * Adaptation from LoopGen 0.4 by F. Quillere. This function sorts a list of
927 * parameterized disjoint polyhedra, in order to not have lexicographic order
928 * violation (see Quillere paper).
929 * - September 16th 2005: inclusion of cloog_loop_number (October 29th 2001).
931 CloogLoop
* cloog_loop_sort(CloogLoop
* loop
, int level
, int nb_par
)
932 { CloogLoop
* res
, * now
, * temp
, ** loop_array
;
934 int i
, nb_loops
=0, * permut
;
936 /* There is no need to sort the parameter domains. */
940 /* We will need to know how many loops are in the list. */
947 /* If there is only one loop, it's the end. */
951 /* We have to allocate memory for some useful components:
952 * - loop_array: the loop array,
953 * - doms: the array of domains to sort,
954 * - permut: will give us a possible sort (maybe not the only one).
956 loop_array
= (CloogLoop
**)malloc(nb_loops
*sizeof(CloogLoop
*)) ;
957 doms
= (CloogDomain
**)malloc(nb_loops
*sizeof(CloogDomain
*));
958 permut
= (int *)malloc(nb_loops
*sizeof(int)) ;
960 /* We fill up the loop and domain arrays. */
961 for (i
=0;i
<nb_loops
;i
++,loop
=loop
->next
)
962 { loop_array
[i
] = loop
;
963 doms
[i
] = loop_array
[i
]->domain
;
966 /* cloog_domain_sort will fill up permut. */
967 cloog_domain_sort(doms
, nb_loops
, level
, permut
);
969 /* With permut and loop_array we build the sorted list. */
971 for (i
=0;i
<nb_loops
;i
++)
972 { /* To avoid pointer looping... loop_add will rebuild the list. */
973 loop_array
[permut
[i
]-1]->next
= NULL
;
974 cloog_loop_add(&res
,&now
,loop_array
[permut
[i
]-1]) ;
986 * cloog_loop_nest function:
987 * This function changes the loop list in such a way that we have no more than
988 * one dimension added by level. It returns an equivalent loop list with
990 * - October 29th 2001: first version.
991 * - July 3rd->11th 2003: memory leaks hunt and correction.
992 * - June 22nd 2005: Adaptation for GMP.
993 * - November 21th 2005: (debug) now OK when cloog_loop_restrict returns NULL.
995 CloogLoop
* cloog_loop_nest(loop
, context
, level
, nb_par
)
997 CloogDomain
* context
;
1000 CloogLoop
* p
, * temp
, * res
, * now
, * next
;
1001 CloogDomain
* new_domain
;
1003 loop
= cloog_loop_disjoint(loop
);
1006 /* Each domain is changed by its intersection with the context. */
1007 while (loop
!= NULL
)
1008 { p
= cloog_loop_restrict(loop
,context
,nb_par
) ;
1012 { cloog_loop_free_parts(loop
,1,0,0,0) ;
1014 temp
= cloog_loop_alloc(p
->state
, p
->domain
, p
->state
->one
,
1015 p
->state
->zero
, p
->block
, p
->inner
, NULL
);
1017 /* If the intersection dimension is too big, we make projections smaller
1018 * and smaller, and each projection includes the preceding projection
1019 * (thus, in the target list, dimensions are added one by one).
1021 if (cloog_domain_dimension(p
->domain
) >= level
)
1022 for (l
= cloog_domain_dimension(p
->domain
); l
>= level
; l
--) {
1023 new_domain
= cloog_domain_project(p
->domain
, l
);
1024 temp
= cloog_loop_alloc(p
->state
, new_domain
, p
->state
->one
,
1025 p
->state
->zero
, NULL
, temp
, NULL
);
1028 /* p is no more useful (but its content yes !). */
1029 cloog_loop_free_parts(p
,0,0,0,0) ;
1031 cloog_loop_add(&res
,&now
,temp
) ;
1034 cloog_loop_free_parts(loop
,1,1,1,0) ;
1044 * cloog_loop_stride function:
1045 * This function will find the stride of a loop for the iterator at the column
1046 * number 'level' in the constraint matrix. It will update the lower bound of
1047 * the iterator accordingly. Basically, the function will try to find in the
1048 * inner loops a common condition on this iterator for the inner loop iterators
1049 * to be integral. For instance, let us consider a loop with the iterator i,
1050 * the iteration domain -4<=i<=n, and its two inner loops with the iterator j.
1051 * The first inner loop has the constraint 3j=i, and the second one has the
1052 * constraint 6j=i. Then the common constraint on i for j to be integral is
1053 * i%3=0, the stride for i is 3. Lastly, we have to find the new lower bound
1054 * for i: the first value satisfying the common constraint: -3. At the end, the
1055 * iteration domain for i is -3<=i<=n and the stride for i is 3.
1056 * - loop is the loop including the iteration domain of the considered iterator,
1057 * - level is the column number of the iterator in the matrix of contraints.
1059 * - June 29th 2003: first version (work in progress since June 26th 2003).
1060 * - July 14th 2003: simpler version.
1061 * - June 22nd 2005: Adaptation for GMP (from S. Verdoolaege's 0.12.1 version).
1063 void cloog_loop_stride(CloogLoop
* loop
, int level
, int nb_par
)
1064 { int first_search
;
1065 cloog_int_t stride
, ref_offset
, offset
, potential
, lower
;
1068 cloog_int_init(stride
);
1069 cloog_int_init(ref_offset
);
1070 cloog_int_init(offset
);
1071 cloog_int_init(potential
);
1072 cloog_int_init(lower
);
1074 cloog_int_set_si(ref_offset
, 0);
1075 cloog_int_set_si(offset
, 0);
1076 cloog_int_set_si(lower
, 0);
1078 /* Default stride. */
1079 cloog_int_set_si(stride
, 1);
1081 inner
= loop
->inner
;
1083 if (cloog_domain_integral_lowerbound(loop
->domain
,level
,&lower
))
1084 while (inner
!= NULL
)
1085 { /* If the minimun stride has not been found yet, find the stride. */
1086 if ((first_search
) || (!cloog_int_is_one(stride
)))
1088 cloog_domain_stride(inner
->domain
, level
, &potential
, &offset
);
1089 if (!cloog_int_is_one(potential
) && (!first_search
))
1090 { /* Offsets must be the same for common stride. */
1091 cloog_int_gcd(stride
, potential
, stride
);
1092 if (!cloog_int_is_zero(stride
)) {
1093 cloog_int_fdiv_r(offset
, offset
, stride
);
1094 cloog_int_fdiv_r(ref_offset
, ref_offset
, stride
);
1096 if (cloog_int_ne(offset
,ref_offset
))
1097 cloog_int_set_si(stride
, 1);
1100 cloog_int_set(stride
, potential
);
1101 cloog_int_set(ref_offset
, offset
);
1107 inner
= inner
->next
;
1110 if (cloog_int_is_zero(stride
))
1111 cloog_int_set_si(stride
, 1);
1113 /* Update the values if necessary. */
1114 if (!cloog_int_is_one(stride
))
1115 { /* Update the stride value. */
1116 cloog_int_set(loop
->stride
, stride
);
1117 /* The new lower bound l' is such that
1118 * (l' + offset) % s = 0 and l <= l' <= l+(s-1)
1119 * Let l' = k s - offset, then
1120 * k s - offset <= l + (s-1) <= k s - offset + (s-1)
1121 * Or l' = floor((l+offset+(s-1))/s) * s - offset
1122 * = (floor((l+offset-1)/s) + 1) * s - offset
1124 cloog_int_add(lower
, lower
, offset
);
1125 cloog_int_sub_ui(lower
, lower
, 1);
1126 cloog_int_fdiv_q(lower
, lower
, stride
);
1127 cloog_int_add_ui(lower
, lower
, 1);
1128 cloog_int_mul(lower
, lower
, stride
);
1129 cloog_int_sub(lower
, lower
, offset
);
1130 loop
->domain
= cloog_domain_lowerbound_update(loop
->domain
, level
, lower
);
1131 cloog_int_set(loop
->offset
, lower
);
1134 cloog_int_clear(stride
);
1135 cloog_int_clear(ref_offset
);
1136 cloog_int_clear(offset
);
1137 cloog_int_clear(potential
);
1138 cloog_int_clear(lower
);
1143 * cloog_loop_stop function:
1144 * This function implements the 'stop' option : each domain of each loop
1145 * in the list 'loop' is replaced by 'context'. 'context' should be the
1146 * domain of the outer loop. By using this method, there are no more dimensions
1147 * to scan and the simplification step will automaticaly remove the domains
1148 * since they are the same as the corresponding contexts. The effect of this
1149 * function is to stop the code generation at the level this function is called,
1150 * the resulting code do not consider the next dimensions.
1151 * - January 11th 2005: first version.
1153 CloogLoop
* cloog_loop_stop(CloogLoop
* loop
, CloogDomain
* context
)
1157 { cloog_domain_free(loop
->domain
) ;
1158 loop
->domain
= cloog_domain_copy(context
) ;
1159 loop
->next
= cloog_loop_stop(loop
->next
, context
) ;
1167 * cloog_loop_scalar_gt function:
1168 * This function returns 1 if loop 'l1' is greater than loop 'l2' for the
1169 * scalar dimension vector that begins at dimension 'scalar', 0 otherwise. What
1170 * we want to know is whether a loop is scheduled before another one or not.
1171 * This function solves the problem when the considered dimension for scheduling
1172 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1173 * this function will reason about the vector of scalar dimension that begins
1174 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1175 * \param l1 Loop to be compared with l2.
1176 * \param l2 Loop to be compared with l1.
1177 * \param level Current non-scalar dimension.
1178 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1179 * \param nb_scattdims Size of the scaldims array.
1180 * \param scalar Current scalar dimension.
1181 * \return 1 if (l1 > l2), 0 otherwise.
1183 * - September 9th 2005: first version.
1184 * - October 15nd 2007: now "greater than" instead of "greater or equal".
1186 int cloog_loop_scalar_gt(l1
, l2
, level
, scaldims
, nb_scattdims
, scalar
)
1187 CloogLoop
* l1
, * l2
;
1188 int level
, * scaldims
, nb_scattdims
, scalar
;
1189 { while ((scalar
< l1
->inner
->block
->nb_scaldims
) && scaldims
[level
+scalar
-1])
1190 { if (cloog_int_gt(l1
->inner
->block
->scaldims
[scalar
],
1191 l2
->inner
->block
->scaldims
[scalar
]))
1201 * cloog_loop_scalar_eq function:
1202 * This function returns 1 if loop 'l1' is equal to loop 'l2' for the scalar
1203 * dimension vector that begins at dimension 'scalar', 0 otherwise. What we want
1204 * to know is whether two loops are scheduled for the same time or not.
1205 * This function solves the problem when the considered dimension for scheduling
1206 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1207 * this function will reason about the vector of scalar dimension that begins
1208 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1209 * - l1 and l2 are the loops to compare,
1210 * - level is the current non-scalar dimension,
1211 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1212 * - nb_scattdims is the size of the scaldims array,
1213 * - scalar is the current scalar dimension.
1215 * - September 9th 2005 : first version.
1217 int cloog_loop_scalar_eq(l1
, l2
, level
, scaldims
, nb_scattdims
, scalar
)
1218 CloogLoop
* l1
, * l2
;
1219 int level
, * scaldims
, nb_scattdims
, scalar
;
1220 { while ((scalar
< l1
->inner
->block
->nb_scaldims
) && scaldims
[level
+scalar
-1])
1221 { if (cloog_int_eq(l1
->inner
->block
->scaldims
[scalar
],
1222 l2
->inner
->block
->scaldims
[scalar
]))
1232 * cloog_loop_scalar_sort function:
1233 * This function sorts a linked list of loops (loop) with respect to the
1234 * scalar dimension vector that begins at dimension 'scalar'. Since there may
1235 * be a succession of scalar dimensions, this function will reason about the
1236 * vector of scalar dimension that begins at dimension 'level+scalar' and
1237 * finish to the first non-scalar dimension.
1238 * \param loop Loop list to sort.
1239 * \param level Current non-scalar dimension.
1240 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1241 * \param nb_scattdims Size of the scaldims array.
1242 * \param scalar Current scalar dimension.
1243 * \return A pointer to the sorted list.
1245 * - July 2nd 2005: first developments.
1246 * - September 2nd 2005: first version.
1247 * - October 15nd 2007: complete rewrite to remove bugs, now a bubble sort.
1249 CloogLoop
* cloog_loop_scalar_sort(loop
, level
, scaldims
, nb_scattdims
, scalar
)
1251 int level
, * scaldims
, nb_scattdims
, scalar
;
1253 CloogLoop
**current
;
1257 for (current
= &loop
; (*current
)->next
; current
= &(*current
)->next
) {
1258 CloogLoop
*next
= (*current
)->next
;
1259 if (cloog_loop_scalar_gt(*current
,next
,level
,scaldims
,nb_scattdims
,scalar
)) {
1261 (*current
)->next
= next
->next
;
1262 next
->next
= *current
;
1273 * cloog_loop_generate_backtrack function:
1274 * adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1275 * backtrack of the Quillere et al. algorithm (see the Quillere paper).
1276 * It eliminates unused iterations of the current level for the new one. See the
1277 * example called linearity-1-1 example with and without this part for an idea.
1278 * - October 26th 2001: first version in cloog_loop_generate_general.
1279 * - July 31th 2002: (debug) no more parasite loops (REALLY hard !).
1280 * - October 30th 2005: extraction from cloog_loop_generate_general.
1282 CloogLoop
* cloog_loop_generate_backtrack(loop
, context
, level
, nb_par
, options
)
1284 CloogDomain
* context
;
1286 CloogOptions
* options
;
1288 CloogDomain
* domain
;
1289 CloogLoop
* now
, * now2
, * next
, * next2
, * end
, * temp
, * l
, * inner
,
1295 while (temp
!= NULL
)
1297 inner
= temp
->inner
;
1299 while (inner
!= NULL
)
1300 { next
= inner
->next
;
1301 /* This 'if' and its first part is the debug of july 31th 2002. */
1302 if (inner
->block
!= NULL
) {
1303 end
= cloog_loop_alloc(temp
->state
, inner
->domain
, temp
->state
->one
,
1304 temp
->state
->zero
, inner
->block
, NULL
, NULL
);
1305 domain
= cloog_domain_copy(temp
->domain
) ;
1306 new_loop
= cloog_loop_alloc(temp
->state
, domain
, temp
->state
->one
,
1307 temp
->state
->zero
, NULL
, end
, NULL
);
1310 new_loop
= cloog_loop_project(inner
,level
,nb_par
) ;
1312 cloog_loop_free_parts(inner
,0,0,0,0) ;
1313 cloog_loop_add(&l
,&now2
,new_loop
) ;
1317 temp
->inner
= NULL
;
1320 { l
= cloog_loop_separate(l
) ;
1321 l
= cloog_loop_sort(l
,level
,nb_par
) ;
1323 cloog_int_set(l
->stride
, temp
->stride
);
1324 cloog_int_set(l
->offset
, temp
->offset
);
1325 cloog_loop_add(&loop
,&now
,l
) ;
1329 next2
= temp
->next
;
1330 cloog_loop_free_parts(temp
,1,0,0,0) ;
1339 * cloog_loop_generate_general function:
1340 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1341 * Quillere algorithm for polyhedron scanning from step 3 to 5.
1342 * (see the Quillere paper).
1343 * - loop is the loop for which we have to generate a scanning code,
1344 * - context is the context of the current loop (constraints on parameter and/or
1345 * on outer loop counters),
1346 * - level is the current non-scalar dimension,
1347 * - scalar is the current scalar dimension,
1348 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1349 * - nb_scattdims is the size of the scaldims array,
1350 * - nb_par is the number of parameters,
1351 * - options are the general code generation options.
1353 * - October 26th 2001: first version.
1354 * - July 3rd->11th 2003: memory leaks hunt and correction.
1355 * - June 22nd 2005: Adaptation for GMP.
1356 * - September 2nd 2005: The function have been cutted out in two pieces:
1357 * cloog_loop_generate and this one, in order to handle
1358 * the scalar dimension case more efficiently with
1359 * cloog_loop_generate_scalar.
1360 * - November 15th 2005: (debug) the result of the cloog_loop_generate call may
1361 * be a list of polyhedra (especially if stop option is
1362 * used): cloog_loop_add_list instead of cloog_loop_add.
1364 CloogLoop
* cloog_loop_generate_general(loop
, context
, level
, scalar
,
1365 scaldims
, nb_scattdims
, nb_par
, options
)
1367 CloogDomain
* context
;
1368 int level
, scalar
, * scaldims
, nb_scattdims
, nb_par
;
1369 CloogOptions
* options
;
1371 CloogLoop
* res
, * now
, * temp
, * l
, * new_loop
, * inner
, * now2
, * end
,
1373 CloogDomain
* domain
;
1375 /* 3. Separate all projections into disjoint polyhedra. */
1376 res
= ((options
->f
> level
+scalar
) || (options
->f
< 0)) ?
1377 cloog_loop_merge(loop
, nb_par
, options
) : cloog_loop_separate(loop
);
1379 /* 3b. -correction- sort the loops to determine their textual order. */
1380 res
= cloog_loop_sort(res
,level
,nb_par
) ;
1382 /* 4. Recurse for each loop with the current domain as context. */
1385 if (!level
|| (level
+scalar
< options
->l
) || (options
->l
< 0))
1387 { if (level
&& options
->strides
)
1388 cloog_loop_stride(temp
,level
,nb_par
) ;
1389 inner
= temp
->inner
;
1390 domain
= temp
->domain
;
1392 while (inner
!= NULL
)
1393 { /* 4b. -ced- recurse for each sub-list of non terminal loops. */
1394 if (cloog_domain_dimension(inner
->domain
) > level
)
1396 while ((end
->next
!= NULL
) &&
1397 (cloog_domain_dimension(end
->next
->domain
) > level
))
1403 l
= cloog_loop_generate(inner
,domain
,level
+1,scalar
,
1404 scaldims
,nb_scattdims
,nb_par
,options
) ;
1407 cloog_loop_add_list(&into
,&now
,l
) ;
1412 { cloog_loop_add(&into
,&now
,inner
) ;
1413 inner
= inner
->next
;
1418 temp
->inner
= into
;
1419 cloog_loop_add(&res
,&now2
,temp
) ;
1423 while (temp
!= NULL
)
1424 { next
= temp
->next
;
1425 l
= cloog_loop_nest(temp
->inner
,temp
->domain
,level
+1,nb_par
) ;
1426 new_loop
= cloog_loop_alloc(temp
->state
, temp
->domain
, temp
->state
->one
,
1427 temp
->state
->zero
, NULL
, l
, NULL
);
1428 temp
->inner
= NULL
;
1430 cloog_loop_free_parts(temp
,0,0,0,0) ;
1431 cloog_loop_add(&res
,&now
,new_loop
) ;
1435 /* 5. eliminate unused iterations of the current level for the new one. See
1436 * the example called linearity-1-1 example with and without this part
1439 if ((!options
->nobacktrack
) && level
&&
1440 ((level
+scalar
< options
->l
) || (options
->l
< 0)) &&
1441 ((options
->f
<= level
+scalar
) && !(options
->f
< 0)))
1442 res
= cloog_loop_generate_backtrack(res
,context
,level
,nb_par
,options
) ;
1444 /* Pray for my new paper to be accepted somewhere since the following stuff
1445 * is really amazing :-) !
1446 * Far long later: The paper has been accepted to PACT 2004 :-))). But there
1447 * are still some bugs and I have no time to fix them. Thus now you have to
1448 * pray for me to get an academic position for that really amazing stuff :-) !
1449 * Later again: OK, I get my academic position, but still I have not enough
1450 * time to fix and clean this part... Pray again :-) !!!
1452 /* res = cloog_loop_unisolate(res,context,level,nb_par) ;*/
1459 * cloog_loop_generate_scalar function:
1460 * This function applies the simplified code generation scheme in the trivial
1461 * case of scalar dimensions. When dealing with scalar dimensions, there is
1462 * no need of costly polyhedral operations for separation or sorting: sorting
1463 * is a question of comparing scalar vectors and separation amounts to consider
1464 * only loops with the same scalar vector for the next step of the code
1465 * generation process. This function achieves the separation/sorting process
1466 * for the vector of scalar dimension that begins at dimension 'level+scalar'
1467 * and finish to the first non-scalar dimension.
1468 * - loop is the loop for which we have to generate a scanning code,
1469 * - context is the context of the current loop (constraints on parameter and/or
1470 * on outer loop counters),
1471 * - level is the current non-scalar dimension,
1472 * - scalar is the current scalar dimension,
1473 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1474 * - nb_scattdims is the size of the scaldims array,
1475 * - nb_par is the number of parameters,
1476 * - options are the general code generation options.
1478 * - September 2nd 2005: First version.
1480 CloogLoop
* cloog_loop_generate_scalar(loop
, context
, level
, scalar
,
1481 scaldims
, nb_scattdims
, nb_par
, options
)
1483 CloogDomain
* context
;
1484 int level
, scalar
, * scaldims
, nb_scattdims
, nb_par
;
1485 CloogOptions
* options
;
1486 { CloogLoop
* res
, * now
, * temp
, * l
, * end
, * next
, * ref
;
1488 /* We sort the loop list with respect to the current scalar vector. */
1489 res
= cloog_loop_scalar_sort(loop
,level
,scaldims
,nb_scattdims
,scalar
) ;
1493 while (temp
!= NULL
)
1494 { /* Then we will appy the general code generation process to each sub-list
1495 * of loops with the same scalar vector.
1500 while((end
->next
!= NULL
) &&
1501 cloog_loop_scalar_eq(ref
,end
->next
,level
,scaldims
,nb_scattdims
,scalar
))
1507 /* For the next dimension, scalar value is updated by adding the scalar
1508 * vector size, which is stored at scaldims[level+scalar-1].
1510 l
= cloog_loop_generate_general(temp
,context
,level
,
1511 scalar
+scaldims
[level
+scalar
-1],
1512 scaldims
,nb_scattdims
,nb_par
,options
) ;
1515 cloog_loop_add_list(&res
,&now
,l
) ;
1524 /* Compare loop with the next loop based on their constant dimensions.
1525 * The result is < 0, == 0 or > 0 depending on whether the constant
1526 * dimensions of loop are lexicographically smaller, equal or greater
1527 * than those of loop->next.
1528 * If loop is the last in the list, then it is assumed to be smaller
1529 * than the "next" one.
1531 static int cloog_loop_next_scal_cmp(CloogLoop
*loop
)
1539 nb_scaldims
= loop
->block
->nb_scaldims
;
1540 if (loop
->next
->block
->nb_scaldims
< nb_scaldims
)
1541 nb_scaldims
= loop
->next
->block
->nb_scaldims
;
1543 for (i
= 0; i
< nb_scaldims
; ++i
) {
1544 int cmp
= cloog_int_cmp(loop
->block
->scaldims
[i
],
1545 loop
->next
->block
->scaldims
[i
]);
1549 return loop
->block
->nb_scaldims
- loop
->next
->block
->nb_scaldims
;
1553 /* Check whether the globally constant dimensions of a and b
1554 * have the same value for all globally constant dimensions
1555 * that are situated before any (locally) non-constant dimension.
1557 static int cloog_loop_equal_prefix(CloogLoop
*a
, CloogLoop
*b
,
1558 int *scaldims
, int nb_scattdims
)
1564 for (i
= 0; i
< nb_scattdims
; ++i
) {
1569 if (!cloog_int_eq(a
->block
->scaldims
[cst
], b
->block
->scaldims
[cst
]))
1573 for (i
= i
+ 1; i
< nb_scattdims
; ++i
) {
1576 if (!cloog_domain_lazy_isconstant(a
->domain
, dim
))
1578 /* No need to check that dim is also constant in b and that the
1579 * constant values are equal. That will happen during the check
1580 * whether the two domains are equal.
1588 /* Try to block adjacent loops in the loop list "loop".
1589 * We only attempt blocking if the constant dimensions of the loops
1590 * in the least are (not necessarily strictly) increasing.
1591 * Then we look for a sublist such that the first (begin) has constant
1592 * dimensions strictly larger than the previous loop in the complete
1593 * list and such that the loop (end) after the last loop in the sublist
1594 * has constant dimensions strictly larger than the last loop in the sublist.
1595 * Furthermore, all loops in the sublist should have the same domain
1596 * (with globally constant dimensions removed) and the difference
1597 * (if any) in constant dimensions may only occur after all the
1598 * (locally) constant dimensions.
1599 * If we find such a sublist, then the blocks of all but the first
1600 * are merged into the block of the first.
1602 * Note that this function can only be called before the global
1603 * blocklist has been created because it may otherwise modify and destroy
1604 * elements on that list.
1606 CloogLoop
*cloog_loop_block(CloogLoop
*loop
, int *scaldims
, int nb_scattdims
)
1608 CloogLoop
*begin
, *end
, *l
;
1609 int begin_after_previous
;
1610 int end_after_previous
;
1614 for (begin
= loop
; begin
; begin
= begin
->next
) {
1615 if (!begin
->block
|| !begin
->block
->scaldims
)
1617 if (cloog_loop_next_scal_cmp(loop
) > 0)
1621 begin_after_previous
= 1;
1622 for (begin
= loop
; begin
; begin
= begin
->next
) {
1623 if (!begin_after_previous
) {
1624 begin_after_previous
= cloog_loop_next_scal_cmp(begin
) < 0;
1628 end_after_previous
= cloog_loop_next_scal_cmp(begin
) < 0;
1629 for (end
= begin
->next
; end
; end
= end
->next
) {
1630 if (!cloog_loop_equal_prefix(begin
, end
, scaldims
, nb_scattdims
))
1632 if (!cloog_domain_lazy_equal(begin
->domain
, end
->domain
))
1634 end_after_previous
= cloog_loop_next_scal_cmp(end
) < 0;
1636 if (end
!= begin
->next
&& end_after_previous
) {
1637 for (l
= begin
->next
; l
!= end
; l
= begin
->next
) {
1638 cloog_block_merge(begin
->block
, l
->block
);
1639 begin
->next
= l
->next
;
1640 cloog_loop_free_parts(l
, 1, 0, 1, 0);
1644 begin_after_previous
= cloog_loop_next_scal_cmp(begin
) < 0;
1652 * cloog_loop_generate function:
1653 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1654 * Quillere algorithm for polyhedron scanning from step 1 to 2.
1655 * (see the Quillere paper).
1656 * - loop is the loop for which we have to generate a scanning code,
1657 * - context is the context of the current loop (constraints on parameter and/or
1658 * on outer loop counters),
1659 * - level is the current non-scalar dimension,
1660 * - scalar is the current scalar dimension,
1661 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1662 * - nb_scattdims is the size of the scaldims array,
1663 * - nb_par is the number of parameters,
1664 * - options are the general code generation options.
1666 * - October 26th 2001: first version.
1667 * - July 3rd->11th 2003: memory leaks hunt and correction.
1668 * - June 15th 2005: a memory leak fixed (loop was not entirely freed when
1669 * the result of cloog_loop_restrict was NULL).
1670 * - June 22nd 2005: Adaptation for GMP.
1671 * - September 2nd 2005: The function have been cutted out in two pieces:
1672 * cloog_loop_generate and this one, in order to handle
1673 * the scalar dimension case more efficiently with
1674 * cloog_loop_generate_scalar.
1675 * - November 15th 2005: (debug) Condition for stop option no more take care of
1676 * further scalar dimensions.
1678 CloogLoop
* cloog_loop_generate(loop
, context
, level
, scalar
,
1679 scaldims
, nb_scattdims
, nb_par
, options
)
1681 CloogDomain
* context
;
1682 int level
, scalar
, * scaldims
, nb_scattdims
, nb_par
;
1683 CloogOptions
* options
;
1684 { CloogLoop
* res
, * now
, * temp
, * next
, * old
;
1686 /* If the user asked to stop code generation at this level, let's stop. */
1687 if ((options
->stop
>= 0) && (level
+scalar
>= options
->stop
+1))
1688 return cloog_loop_stop(loop
,context
) ;
1692 /* 1. Replace each polyhedron by its intersection with the context.
1693 * 2. Compute the projection of each polyhedron onto the outermost
1694 * loop variable and the parameters.
1696 while (loop
!= NULL
)
1697 { next
= loop
->next
;
1698 temp
= cloog_loop_restrict(loop
,context
,nb_par
) ;
1702 temp
= cloog_loop_project(temp
,level
,nb_par
) ;
1703 cloog_loop_free_parts(old
,0,0,0,0) ;
1704 cloog_loop_add(&res
,&now
,temp
) ;
1705 cloog_loop_free_parts(loop
,1,0,0,0) ;
1708 { loop
->next
= NULL
;
1709 cloog_loop_free(loop
) ;
1717 /* To save both time and memory, we switch here depending on whether the
1718 * current dimension is scalar (simplified processing) or not (general
1721 if (level
&& (level
+scalar
<= nb_scattdims
) && (scaldims
[level
+scalar
-1]))
1722 res
= cloog_loop_generate_scalar(res
,context
,level
,scalar
,
1723 scaldims
,nb_scattdims
,nb_par
,options
) ;
1725 res
= cloog_loop_generate_general(res
,context
,level
,scalar
,
1726 scaldims
,nb_scattdims
,nb_par
,options
) ;
1733 * Internal function for simplifying a single loop in a list of loops.
1734 * See cloog_loop_simplify.
1736 static CloogLoop
*loop_simplify(CloogLoop
*loop
, CloogDomain
*context
,
1737 int level
, int nb_par
)
1740 CloogBlock
* new_block
;
1741 CloogLoop
*simplified
, *inner
;
1742 CloogDomain
* domain
, * simp
, * inter
, * extended_context
;
1744 domain
= loop
->domain
;
1746 domain_dim
= cloog_domain_dimension(domain
);
1747 extended_context
= cloog_domain_extend(context
, domain_dim
);
1748 inter
= cloog_domain_intersection(domain
,extended_context
) ;
1749 simp
= cloog_domain_simplify(inter
,extended_context
) ;
1750 cloog_domain_free(extended_context
) ;
1752 /* If the constraint system is never true, go to the next one. */
1753 if (cloog_domain_never_integral(simp
)) {
1754 cloog_loop_free(loop
->inner
);
1755 cloog_domain_free(inter
);
1756 cloog_domain_free(simp
);
1760 inner
= cloog_loop_simplify(loop
->inner
,inter
,level
+1,nb_par
) ;
1761 cloog_domain_free(inter
) ;
1763 if ((inner
== NULL
) && (loop
->block
== NULL
)) {
1764 cloog_domain_free(simp
);
1768 new_block
= cloog_block_copy(loop
->block
) ;
1770 simplified
= cloog_loop_alloc(loop
->state
, simp
, loop
->stride
, loop
->offset
,
1771 new_block
, inner
, NULL
);
1773 return(simplified
) ;
1778 * cloog_loop_simplify function:
1779 * This function implements the part 6. of the Quillere algorithm, it
1780 * recursively simplifies each loop in the context of the preceding loop domain.
1781 * It returns a pointer to the simplified loop list.
1782 * The cloog_domain_simplify (DomainSimplify) behaviour is really bad with
1783 * polyhedra union and some really awful sidesteppings were written, I plan
1785 * - October 31th 2001: first version.
1786 * - July 3rd->11th 2003: memory leaks hunt and correction.
1787 * - April 16th 2005: a memory leak fixed (extended_context was not freed).
1788 * - June 15th 2005: a memory leak fixed (loop was not conveniently freed
1789 * when the constraint system is never true).
1790 * - October 27th 2005: - this function called before cloog_loop_fast_simplify
1791 * is now the official cloog_loop_simplify function in
1792 * replacement of a slower and more complex one (after
1793 * deep changes in the pretty printer).
1794 * - we use cloog_loop_disjoint to fix the problem when
1795 * simplifying gives a union of polyhedra (before, it
1796 * was under the responsibility of the pretty printer).
1798 CloogLoop
* cloog_loop_simplify(loop
, context
, level
, nb_par
)
1800 CloogDomain
* context
;
1804 CloogLoop
*res
= NULL
;
1805 CloogLoop
**next
= &res
;
1807 for (now
= loop
; now
; now
= now
->next
) {
1808 *next
= loop_simplify(now
, context
, level
, nb_par
);
1810 now
->inner
= NULL
; /* For loop integrity. */
1811 cloog_domain_free(now
->domain
);
1815 next
= &(*next
)->next
;
1817 cloog_loop_free(loop
);
1819 /* Examples like test/iftest2.cloog give unions of polyhedra after
1820 * simplifying, thus we we have to disjoint them. Another good reason to
1821 * put the simplifying step in the Quillere backtrack.
1823 res
= cloog_loop_disjoint(res
);
1830 * cloog_loop_scatter function:
1831 * This function add the scattering (scheduling) informations in a loop.
1833 void cloog_loop_scatter(CloogLoop
* loop
, CloogScattering
*scatt
)
1835 loop
->domain
= cloog_domain_scatter(loop
->domain
, scatt
);