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
, *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
);
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
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
))
552 { next
= loop
->next
;
554 cloog_loop_add_disjoint(&res
,&now
,loop
) ;
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
) ;
586 new_domain
= cloog_domain_intersection(context
,loop
->domain
) ;
588 if (cloog_domain_isempty(new_domain
))
589 { cloog_domain_free(new_domain
) ;
593 new_loop
= cloog_loop_alloc(loop
->state
, new_domain
,
594 loop
->state
->one
, loop
->state
->zero
,
595 loop
->block
, loop
->inner
, NULL
);
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
)
608 CloogLoop
*res
= NULL
;
609 CloogLoop
**res_next
= &res
;
611 for (; loop
; loop
= next
) {
614 *res_next
= cloog_loop_restrict(loop
, context
);
616 res_next
= &(*res_next
)->next
;
617 cloog_loop_free_parts(loop
, 1, 0, 0, 0);
620 cloog_loop_free(loop
);
628 * cloog_loop_project function:
629 * This function returns the projection of (loop) on the (level) first
630 * dimensions (outer loops). It makes the projection of the (loop) domain,
631 * then it returns a pointer to a new loop, with this projection as domain.
633 * - October 27th 2001: first version.
634 * - July 3rd->11th 2003: memory leaks hunt and correction.
635 * - June 22nd 2005: Adaptation for GMP.
637 CloogLoop
* cloog_loop_project(CloogLoop
* loop
, int level
)
639 CloogDomain
* new_domain
;
640 CloogLoop
* new_loop
, * copy
;
642 copy
= cloog_loop_alloc(loop
->state
, loop
->domain
, loop
->stride
, loop
->offset
,
643 loop
->block
, loop
->inner
, NULL
);
645 if (cloog_domain_dimension(loop
->domain
) == level
)
646 new_domain
= cloog_domain_copy(loop
->domain
) ;
648 new_domain
= cloog_domain_project(loop
->domain
, level
);
650 new_loop
= cloog_loop_alloc(loop
->state
, new_domain
, loop
->state
->one
,
651 loop
->state
->zero
, NULL
, copy
, NULL
);
658 * Call cloog_loop_project on each loop in the list "loop" and return
659 * the concatenated result.
661 CloogLoop
*cloog_loop_project_all(CloogLoop
*loop
, int level
)
664 CloogLoop
*res
= NULL
;
665 CloogLoop
**res_next
= &res
;
667 for (; loop
; loop
= next
) {
670 *res_next
= cloog_loop_project(loop
, level
);
671 res_next
= &(*res_next
)->next
;
672 cloog_loop_free_parts(loop
, 0, 0, 0, 0);
680 * cloog_loop_concat function:
681 * This function returns a pointer to the concatenation of the
682 * CloogLoop lists given as input.
683 * - October 28th 2001: first version.
685 CloogLoop
* cloog_loop_concat(CloogLoop
* a
, CloogLoop
* b
)
686 { CloogLoop
* loop
, * temp
;
691 { while (temp
->next
!= NULL
)
703 * cloog_loop_combine:
704 * Combine consecutive loops with identical domains into
705 * a single loop with the concatenation of their inner loops
708 CloogLoop
*cloog_loop_combine(CloogLoop
*loop
)
710 CloogLoop
*first
, *second
;
712 for (first
= loop
; first
; first
= first
->next
) {
713 while (first
->next
) {
714 if (!cloog_domain_lazy_equal(first
->domain
, first
->next
->domain
))
716 second
= first
->next
;
717 first
->inner
= cloog_loop_concat(first
->inner
, second
->inner
);
718 first
->next
= second
->next
;
719 cloog_loop_free_parts(second
, 1, 0, 0, 0);
727 * cloog_loop_separate function:
728 * This function implements the Quillere algorithm for separation of multiple
729 * loops: for a given set of polyhedra (loop), it computes a set of disjoint
730 * polyhedra such that the unions of these sets are equal, and returns this set.
731 * - October 28th 2001: first version.
732 * - November 14th 2001: elimination of some unused blocks.
733 * - August 13th 2002: (debug) in the case of union of polyhedra for one
734 * loop, redundant constraints are fired.
735 * - July 3rd->11th 2003: memory leaks hunt and correction.
736 * - June 22nd 2005: Adaptation for GMP.
737 * - October 16th 2005: Removal of the non-shared constraint elimination when
738 * there is only one loop in the list (seems to work
739 * without now, DomainSimplify may have been improved).
740 * The problem was visible with test/iftest2.cloog.
742 CloogLoop
* cloog_loop_separate(CloogLoop
* loop
)
743 { int lazy_equal
=0, disjoint
= 0;
744 CloogLoop
* new_loop
, * new_inner
, * res
, * now
, * temp
, * Q
,
745 * inner
, * old
/*, * previous, * next*/ ;
746 CloogDomain
*UQ
, *domain
;
751 loop
= cloog_loop_combine(loop
);
753 if (loop
->next
== NULL
)
754 return cloog_loop_disjoint(loop
) ;
756 UQ
= cloog_domain_copy(loop
->domain
) ;
757 domain
= cloog_domain_copy(loop
->domain
) ;
758 res
= cloog_loop_alloc(loop
->state
, domain
, loop
->state
->one
,
759 loop
->state
->zero
, loop
->block
, loop
->inner
, NULL
);
762 while((loop
= loop
->next
) != NULL
)
765 /* For all Q, add Q-loop associated with the blocks of Q alone,
766 * and Q inter loop associated with the blocks of Q and loop.
768 for (Q
= res
; Q
; Q
= Q
->next
) {
769 /* Add (Q inter loop). */
770 if ((disjoint
= cloog_domain_lazy_disjoint(Q
->domain
,loop
->domain
)))
773 { if ((lazy_equal
= cloog_domain_lazy_equal(Q
->domain
,loop
->domain
)))
774 domain
= cloog_domain_copy(Q
->domain
) ;
776 domain
= cloog_domain_intersection(Q
->domain
,loop
->domain
) ;
778 if (!cloog_domain_isempty(domain
))
779 { new_inner
= cloog_loop_concat(cloog_loop_copy(Q
->inner
),
780 cloog_loop_copy(loop
->inner
)) ;
781 new_loop
= cloog_loop_alloc(loop
->state
, domain
, loop
->state
->one
,
782 loop
->state
->zero
, NULL
, new_inner
, NULL
);
783 cloog_loop_add_disjoint(&temp
,&now
,new_loop
) ;
787 cloog_domain_free(domain
);
791 /* Add (Q - loop). */
793 domain
= cloog_domain_copy(Q
->domain
) ;
796 domain
= cloog_domain_empty(Q
->domain
);
798 domain
= cloog_domain_difference(Q
->domain
,loop
->domain
) ;
801 if (!cloog_domain_isempty(domain
)) {
802 new_loop
= cloog_loop_alloc(loop
->state
, domain
, loop
->state
->one
,
803 loop
->state
->zero
, NULL
, Q
->inner
, NULL
);
804 cloog_loop_add_disjoint(&temp
,&now
,new_loop
) ;
807 { cloog_domain_free(domain
) ;
808 /* If Q->inner is no more useful, we can free it. */
811 cloog_loop_free(inner
) ;
815 /* Add loop-UQ associated with the blocks of loop alone.*/
816 if (cloog_domain_lazy_disjoint(loop
->domain
,UQ
))
817 domain
= cloog_domain_copy(loop
->domain
) ;
819 { if (cloog_domain_lazy_equal(loop
->domain
,UQ
))
820 domain
= cloog_domain_empty(UQ
);
822 domain
= cloog_domain_difference(loop
->domain
,UQ
) ;
825 if (!cloog_domain_isempty(domain
)) {
826 new_loop
= cloog_loop_alloc(loop
->state
, domain
, loop
->state
->one
,
827 loop
->state
->zero
, NULL
, loop
->inner
, NULL
);
828 cloog_loop_add_disjoint(&temp
,&now
,new_loop
) ;
831 { cloog_domain_free(domain
) ;
832 /* If loop->inner is no more useful, we can free it. */
833 cloog_loop_free(loop
->inner
) ;
838 if (loop
->next
!= NULL
)
839 UQ
= cloog_domain_union(UQ
, cloog_domain_copy(loop
->domain
));
841 cloog_domain_free(UQ
);
843 cloog_loop_free_parts(res
,1,0,0,1) ;
847 cloog_loop_free_parts(old
,1,0,0,1) ;
853 static CloogDomain
*bounding_domain(CloogDomain
*dom
, CloogOptions
*options
)
856 return cloog_domain_simple_convex(dom
);
858 return cloog_domain_convex(dom
);
863 * cloog_loop_merge function:
864 * This function is the 'soft' version of loop_separate if we are looking for
865 * a code much simpler (and less efficicient). This function returns the new
867 * - October 29th 2001: first version.
868 * - July 3rd->11th 2003: memory leaks hunt and correction.
869 * - June 22nd 2005: Adaptation for GMP.
871 CloogLoop
*cloog_loop_merge(CloogLoop
*loop
, int level
, CloogOptions
*options
)
873 CloogLoop
*res
, *new_inner
, *old
;
874 CloogDomain
*new_domain
, *temp
;
879 if (loop
->next
== NULL
)
880 return cloog_loop_disjoint(loop
);
885 new_inner
= loop
->inner
;
887 for (loop
= loop
->next
; loop
; loop
= loop
->next
) {
888 temp
= cloog_domain_union(temp
, loop
->domain
);
890 new_inner
= cloog_loop_concat(new_inner
, loop
->inner
);
893 new_domain
= bounding_domain(temp
, options
);
895 if (level
> 0 && !cloog_domain_is_bounded(new_domain
, level
) &&
896 cloog_domain_is_bounded(temp
, level
)) {
897 CloogDomain
*splitter
, *t2
;
899 cloog_domain_free(new_domain
);
900 splitter
= cloog_domain_bound_splitter(temp
, level
);
903 while (!cloog_domain_isconvex(splitter
)) {
904 CloogDomain
*first
, *rest
;
905 first
= cloog_domain_cut_first(splitter
, &rest
);
907 t2
= cloog_domain_intersection(first
, temp
);
908 cloog_domain_free(first
);
910 new_domain
= bounding_domain(t2
, options
);
911 cloog_domain_free(t2
);
913 if (cloog_domain_isempty(new_domain
)) {
914 cloog_domain_free(new_domain
);
917 res
= cloog_loop_alloc(old
->state
, new_domain
, old
->state
->one
,
918 old
->state
->zero
, NULL
,
919 cloog_loop_copy(new_inner
), res
);
922 t2
= cloog_domain_intersection(splitter
, temp
);
923 cloog_domain_free(splitter
);
925 new_domain
= bounding_domain(t2
, options
);
926 cloog_domain_free(t2
);
928 if (cloog_domain_isempty(new_domain
)) {
929 cloog_domain_free(new_domain
);
930 cloog_loop_free(new_inner
);
932 res
= cloog_loop_alloc(old
->state
, new_domain
, old
->state
->one
,
933 old
->state
->zero
, NULL
, new_inner
, res
);
935 res
= cloog_loop_alloc(old
->state
, new_domain
, old
->state
->one
,
936 old
->state
->zero
, NULL
, new_inner
, NULL
);
938 cloog_domain_free(temp
);
940 cloog_loop_free_parts(old
, 0, 0, 0, 1);
946 static int cloog_loop_count(CloogLoop
*loop
)
950 for (nb_loops
= 0; loop
; loop
= loop
->next
)
958 * cloog_loop_sort function:
959 * Adaptation from LoopGen 0.4 by F. Quillere. This function sorts a list of
960 * parameterized disjoint polyhedra, in order to not have lexicographic order
961 * violation (see Quillere paper).
962 * - September 16th 2005: inclusion of cloog_loop_number (October 29th 2001).
964 CloogLoop
*cloog_loop_sort(CloogLoop
*loop
, int level
)
966 CloogLoop
*res
, *now
, **loop_array
;
968 int i
, nb_loops
=0, * permut
;
970 /* There is no need to sort the parameter domains. */
974 /* We will need to know how many loops are in the list. */
975 nb_loops
= cloog_loop_count(loop
);
977 /* If there is only one loop, it's the end. */
981 /* We have to allocate memory for some useful components:
982 * - loop_array: the loop array,
983 * - doms: the array of domains to sort,
984 * - permut: will give us a possible sort (maybe not the only one).
986 loop_array
= (CloogLoop
**)malloc(nb_loops
*sizeof(CloogLoop
*)) ;
987 doms
= (CloogDomain
**)malloc(nb_loops
*sizeof(CloogDomain
*));
988 permut
= (int *)malloc(nb_loops
*sizeof(int)) ;
990 /* We fill up the loop and domain arrays. */
991 for (i
=0;i
<nb_loops
;i
++,loop
=loop
->next
)
992 { loop_array
[i
] = loop
;
993 doms
[i
] = loop_array
[i
]->domain
;
996 /* cloog_domain_sort will fill up permut. */
997 cloog_domain_sort(doms
, nb_loops
, level
, permut
);
999 /* With permut and loop_array we build the sorted list. */
1001 for (i
=0;i
<nb_loops
;i
++)
1002 { /* To avoid pointer looping... loop_add will rebuild the list. */
1003 loop_array
[permut
[i
]-1]->next
= NULL
;
1004 cloog_loop_add(&res
,&now
,loop_array
[permut
[i
]-1]) ;
1016 * cloog_loop_nest function:
1017 * This function changes the loop list in such a way that we have no more than
1018 * one dimension added by level. It returns an equivalent loop list with
1020 * - October 29th 2001: first version.
1021 * - July 3rd->11th 2003: memory leaks hunt and correction.
1022 * - June 22nd 2005: Adaptation for GMP.
1023 * - November 21th 2005: (debug) now OK when cloog_loop_restrict returns NULL.
1025 CloogLoop
*cloog_loop_nest(CloogLoop
*loop
, CloogDomain
*context
, int level
)
1027 CloogLoop
* p
, * temp
, * res
, * now
, * next
;
1028 CloogDomain
* new_domain
;
1030 loop
= cloog_loop_disjoint(loop
);
1033 /* Each domain is changed by its intersection with the context. */
1034 while (loop
!= NULL
)
1035 { p
= cloog_loop_restrict(loop
, context
);
1039 { cloog_loop_free_parts(loop
,1,0,0,0) ;
1041 temp
= cloog_loop_alloc(p
->state
, p
->domain
, p
->state
->one
,
1042 p
->state
->zero
, p
->block
, p
->inner
, NULL
);
1044 /* If the intersection dimension is too big, we make projections smaller
1045 * and smaller, and each projection includes the preceding projection
1046 * (thus, in the target list, dimensions are added one by one).
1048 if (cloog_domain_dimension(p
->domain
) >= level
)
1049 for (l
= cloog_domain_dimension(p
->domain
); l
>= level
; l
--) {
1050 new_domain
= cloog_domain_project(p
->domain
, l
);
1051 temp
= cloog_loop_alloc(p
->state
, new_domain
, p
->state
->one
,
1052 p
->state
->zero
, NULL
, temp
, NULL
);
1055 /* p is no more useful (but its content yes !). */
1056 cloog_loop_free_parts(p
,0,0,0,0) ;
1058 cloog_loop_add(&res
,&now
,temp
) ;
1061 cloog_loop_free_parts(loop
,1,1,1,0) ;
1071 * cloog_loop_stride function:
1072 * This function will find the stride of a loop for the iterator at the column
1073 * number 'level' in the constraint matrix. It will update the lower bound of
1074 * the iterator accordingly. Basically, the function will try to find in the
1075 * inner loops a common condition on this iterator for the inner loop iterators
1076 * to be integral. For instance, let us consider a loop with the iterator i,
1077 * the iteration domain -4<=i<=n, and its two inner loops with the iterator j.
1078 * The first inner loop has the constraint 3j=i, and the second one has the
1079 * constraint 6j=i. Then the common constraint on i for j to be integral is
1080 * i%3=0, the stride for i is 3. Lastly, we have to find the new lower bound
1081 * for i: the first value satisfying the common constraint: -3. At the end, the
1082 * iteration domain for i is -3<=i<=n and the stride for i is 3.
1083 * - loop is the loop including the iteration domain of the considered iterator,
1084 * - level is the column number of the iterator in the matrix of contraints.
1086 * - June 29th 2003: first version (work in progress since June 26th 2003).
1087 * - July 14th 2003: simpler version.
1088 * - June 22nd 2005: Adaptation for GMP (from S. Verdoolaege's 0.12.1 version).
1090 void cloog_loop_stride(CloogLoop
* loop
, int level
)
1091 { int first_search
;
1092 cloog_int_t stride
, ref_offset
, offset
, potential
, lower
;
1095 cloog_int_init(stride
);
1096 cloog_int_init(ref_offset
);
1097 cloog_int_init(offset
);
1098 cloog_int_init(potential
);
1099 cloog_int_init(lower
);
1101 cloog_int_set_si(ref_offset
, 0);
1102 cloog_int_set_si(offset
, 0);
1103 cloog_int_set_si(lower
, 0);
1105 /* Default stride. */
1106 cloog_int_set_si(stride
, 1);
1108 inner
= loop
->inner
;
1110 if (cloog_domain_integral_lowerbound(loop
->domain
,level
,&lower
))
1111 while (inner
!= NULL
)
1112 { /* If the minimun stride has not been found yet, find the stride. */
1113 if ((first_search
) || (!cloog_int_is_one(stride
)))
1115 cloog_domain_stride(inner
->domain
, level
, &potential
, &offset
);
1116 if (!cloog_int_is_one(potential
) && (!first_search
))
1117 { /* Offsets must be the same for common stride. */
1118 cloog_int_gcd(stride
, potential
, stride
);
1119 if (!cloog_int_is_zero(stride
)) {
1120 cloog_int_fdiv_r(offset
, offset
, stride
);
1121 cloog_int_fdiv_r(ref_offset
, ref_offset
, stride
);
1123 if (cloog_int_ne(offset
,ref_offset
))
1124 cloog_int_set_si(stride
, 1);
1127 cloog_int_set(stride
, potential
);
1128 cloog_int_set(ref_offset
, offset
);
1134 inner
= inner
->next
;
1137 if (cloog_int_is_zero(stride
))
1138 cloog_int_set_si(stride
, 1);
1140 /* Update the values if necessary. */
1141 if (!cloog_int_is_one(stride
))
1142 { /* Update the stride value. */
1143 cloog_int_set(loop
->stride
, stride
);
1144 if (!cloog_int_is_zero(offset
))
1145 cloog_int_sub(loop
->offset
, stride
, offset
);
1148 cloog_int_clear(stride
);
1149 cloog_int_clear(ref_offset
);
1150 cloog_int_clear(offset
);
1151 cloog_int_clear(potential
);
1152 cloog_int_clear(lower
);
1157 * cloog_loop_stop function:
1158 * This function implements the 'stop' option : each domain of each loop
1159 * in the list 'loop' is replaced by 'context'. 'context' should be the
1160 * domain of the outer loop. By using this method, there are no more dimensions
1161 * to scan and the simplification step will automaticaly remove the domains
1162 * since they are the same as the corresponding contexts. The effect of this
1163 * function is to stop the code generation at the level this function is called,
1164 * the resulting code do not consider the next dimensions.
1165 * - January 11th 2005: first version.
1167 CloogLoop
* cloog_loop_stop(CloogLoop
* loop
, CloogDomain
* context
)
1171 { cloog_domain_free(loop
->domain
) ;
1172 loop
->domain
= cloog_domain_copy(context
) ;
1173 loop
->next
= cloog_loop_stop(loop
->next
, context
) ;
1180 static int level_is_constant(int level
, int scalar
, int *scaldims
, int nb_scattdims
)
1182 return level
&& (level
+scalar
<= nb_scattdims
) && (scaldims
[level
+scalar
-1]);
1187 * Compare the constant dimensions of loops 'l1' and 'l2' starting at 'scalar'
1188 * and return -1 if the vector of constant dimensions of 'l1' is smaller
1189 * than that of 'l2', 0 if they are the same and +1 if that of 'l1' is
1190 * greater than that of 'l2'.
1191 * This function should be called on the innermost loop (the loop
1192 * containing a block).
1193 * \param l1 Loop to be compared with l2.
1194 * \param l2 Loop to be compared with l1.
1195 * \param level Current non-scalar dimension.
1196 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1197 * \param nb_scattdims Size of the scaldims array.
1198 * \param scalar Current scalar dimension.
1199 * \return -1 if (l1 < l2), 0 if (l1 == l2) and +1 if (l1 > l2)
1201 int cloog_loop_constant_cmp(CloogLoop
*l1
, CloogLoop
*l2
, int level
,
1202 int *scaldims
, int nb_scattdims
, int scalar
)
1204 CloogBlock
*b1
, *b2
;
1207 while (level_is_constant(level
, scalar
, scaldims
, nb_scattdims
)) {
1208 int cmp
= cloog_int_cmp(b1
->scaldims
[scalar
], b2
->scaldims
[scalar
]);
1218 * cloog_loop_scalar_gt function:
1219 * This function returns 1 if loop 'l1' is greater than loop 'l2' for the
1220 * scalar dimension vector that begins at dimension 'scalar', 0 otherwise. What
1221 * we want to know is whether a loop is scheduled before another one or not.
1222 * This function solves the problem when the considered dimension for scheduling
1223 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1224 * this function will reason about the vector of scalar dimension that begins
1225 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1226 * \param l1 Loop to be compared with l2.
1227 * \param l2 Loop to be compared with l1.
1228 * \param level Current non-scalar dimension.
1229 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1230 * \param nb_scattdims Size of the scaldims array.
1231 * \param scalar Current scalar dimension.
1232 * \return 1 if (l1 > l2), 0 otherwise.
1234 * - September 9th 2005: first version.
1235 * - October 15nd 2007: now "greater than" instead of "greater or equal".
1237 int cloog_loop_scalar_gt(l1
, l2
, level
, scaldims
, nb_scattdims
, scalar
)
1238 CloogLoop
* l1
, * l2
;
1239 int level
, * scaldims
, nb_scattdims
, scalar
;
1241 return cloog_loop_constant_cmp(l1
, l2
, level
, scaldims
, nb_scattdims
, scalar
) > 0;
1246 * cloog_loop_scalar_eq function:
1247 * This function returns 1 if loop 'l1' is equal to loop 'l2' for the scalar
1248 * dimension vector that begins at dimension 'scalar', 0 otherwise. What we want
1249 * to know is whether two loops are scheduled for the same time or not.
1250 * This function solves the problem when the considered dimension for scheduling
1251 * is a scalar dimension. Since there may be a succession of scalar dimensions,
1252 * this function will reason about the vector of scalar dimension that begins
1253 * at dimension 'level+scalar' and finish to the first non-scalar dimension.
1254 * - l1 and l2 are the loops to compare,
1255 * - level is the current non-scalar dimension,
1256 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1257 * - nb_scattdims is the size of the scaldims array,
1258 * - scalar is the current scalar dimension.
1260 * - September 9th 2005 : first version.
1262 int cloog_loop_scalar_eq(l1
, l2
, level
, scaldims
, nb_scattdims
, scalar
)
1263 CloogLoop
* l1
, * l2
;
1264 int level
, * scaldims
, nb_scattdims
, scalar
;
1266 return cloog_loop_constant_cmp(l1
, l2
, level
, scaldims
, nb_scattdims
, scalar
) == 0;
1271 * cloog_loop_scalar_sort function:
1272 * This function sorts a linked list of loops (loop) with respect to the
1273 * scalar dimension vector that begins at dimension 'scalar'. Since there may
1274 * be a succession of scalar dimensions, this function will reason about the
1275 * vector of scalar dimension that begins at dimension 'level+scalar' and
1276 * finish to the first non-scalar dimension.
1277 * \param loop Loop list to sort.
1278 * \param level Current non-scalar dimension.
1279 * \param scaldims Boolean array saying whether a dimension is scalar or not.
1280 * \param nb_scattdims Size of the scaldims array.
1281 * \param scalar Current scalar dimension.
1282 * \return A pointer to the sorted list.
1284 * - July 2nd 2005: first developments.
1285 * - September 2nd 2005: first version.
1286 * - October 15nd 2007: complete rewrite to remove bugs, now a bubble sort.
1288 CloogLoop
* cloog_loop_scalar_sort(loop
, level
, scaldims
, nb_scattdims
, scalar
)
1290 int level
, * scaldims
, nb_scattdims
, scalar
;
1292 CloogLoop
**current
;
1296 for (current
= &loop
; (*current
)->next
; current
= &(*current
)->next
) {
1297 CloogLoop
*next
= (*current
)->next
;
1298 if (cloog_loop_scalar_gt(*current
,next
,level
,scaldims
,nb_scattdims
,scalar
)) {
1300 (*current
)->next
= next
->next
;
1301 next
->next
= *current
;
1312 * cloog_loop_generate_backtrack function:
1313 * adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1314 * backtrack of the Quillere et al. algorithm (see the Quillere paper).
1315 * It eliminates unused iterations of the current level for the new one. See the
1316 * example called linearity-1-1 example with and without this part for an idea.
1317 * - October 26th 2001: first version in cloog_loop_generate_general.
1318 * - July 31th 2002: (debug) no more parasite loops (REALLY hard !).
1319 * - October 30th 2005: extraction from cloog_loop_generate_general.
1321 CloogLoop
*cloog_loop_generate_backtrack(CloogLoop
*loop
,
1322 int level
, CloogOptions
*options
)
1324 CloogDomain
* domain
;
1325 CloogLoop
* now
, * now2
, * next
, * next2
, * end
, * temp
, * l
, * inner
,
1331 while (temp
!= NULL
)
1333 inner
= temp
->inner
;
1335 while (inner
!= NULL
)
1336 { next
= inner
->next
;
1337 /* This 'if' and its first part is the debug of july 31th 2002. */
1338 if (inner
->block
!= NULL
) {
1339 end
= cloog_loop_alloc(temp
->state
, inner
->domain
, temp
->state
->one
,
1340 temp
->state
->zero
, inner
->block
, NULL
, NULL
);
1341 domain
= cloog_domain_copy(temp
->domain
) ;
1342 new_loop
= cloog_loop_alloc(temp
->state
, domain
, temp
->state
->one
,
1343 temp
->state
->zero
, NULL
, end
, NULL
);
1346 new_loop
= cloog_loop_project(inner
, level
);
1348 cloog_loop_free_parts(inner
,0,0,0,0) ;
1349 cloog_loop_add(&l
,&now2
,new_loop
) ;
1353 temp
->inner
= NULL
;
1356 { l
= cloog_loop_separate(l
) ;
1357 l
= cloog_loop_sort(l
, level
);
1359 cloog_int_set(l
->stride
, temp
->stride
);
1360 cloog_int_set(l
->offset
, temp
->offset
);
1361 cloog_loop_add(&loop
,&now
,l
) ;
1365 next2
= temp
->next
;
1366 cloog_loop_free_parts(temp
,1,0,0,0) ;
1375 * Return 1 if we need to continue recursing to the specified level.
1377 int cloog_loop_more(CloogLoop
*loop
, int level
, int scalar
, int nb_scattdims
)
1379 return level
+ scalar
<= nb_scattdims
||
1380 cloog_domain_dimension(loop
->domain
) >= level
;
1385 * cloog_loop_generate_general function:
1386 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1387 * Quillere algorithm for polyhedron scanning from step 3 to 5.
1388 * (see the Quillere paper).
1389 * - loop is the loop for which we have to generate a scanning code,
1390 * - level is the current non-scalar dimension,
1391 * - scalar is the current scalar dimension,
1392 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1393 * - nb_scattdims is the size of the scaldims array,
1394 * - options are the general code generation options.
1396 * - October 26th 2001: first version.
1397 * - July 3rd->11th 2003: memory leaks hunt and correction.
1398 * - June 22nd 2005: Adaptation for GMP.
1399 * - September 2nd 2005: The function have been cutted out in two pieces:
1400 * cloog_loop_generate and this one, in order to handle
1401 * the scalar dimension case more efficiently with
1402 * cloog_loop_generate_scalar.
1403 * - November 15th 2005: (debug) the result of the cloog_loop_generate call may
1404 * be a list of polyhedra (especially if stop option is
1405 * used): cloog_loop_add_list instead of cloog_loop_add.
1407 CloogLoop
*cloog_loop_generate_general(CloogLoop
*loop
,
1408 int level
, int scalar
, int *scaldims
, int nb_scattdims
,
1409 CloogOptions
*options
)
1411 CloogLoop
* res
, * now
, * temp
, * l
, * new_loop
, * inner
, * now2
, * end
,
1413 CloogDomain
* domain
;
1415 /* 3. Separate all projections into disjoint polyhedra. */
1416 res
= ((options
->f
> level
+scalar
) || (options
->f
< 0)) ?
1417 cloog_loop_merge(loop
, level
, options
) : cloog_loop_separate(loop
);
1419 /* 3b. -correction- sort the loops to determine their textual order. */
1420 res
= cloog_loop_sort(res
, level
);
1422 /* 4. Recurse for each loop with the current domain as context. */
1425 if (!level
|| (level
+scalar
< options
->l
) || (options
->l
< 0))
1427 { if (level
&& options
->strides
)
1428 cloog_loop_stride(temp
, level
);
1429 inner
= temp
->inner
;
1430 domain
= temp
->domain
;
1432 while (inner
!= NULL
)
1433 { /* 4b. -ced- recurse for each sub-list of non terminal loops. */
1434 if (cloog_loop_more(inner
, level
+ 1, scalar
, nb_scattdims
)) {
1436 while ((end
->next
!= NULL
) &&
1437 cloog_loop_more(end
->next
, level
+ 1, scalar
, nb_scattdims
))
1443 l
= cloog_loop_generate(inner
,domain
,level
+1,scalar
,
1444 scaldims
, nb_scattdims
, options
);
1447 cloog_loop_add_list(&into
,&now
,l
) ;
1452 { cloog_loop_add(&into
,&now
,inner
) ;
1453 inner
= inner
->next
;
1458 temp
->inner
= into
;
1459 cloog_loop_add(&res
,&now2
,temp
) ;
1463 while (temp
!= NULL
)
1464 { next
= temp
->next
;
1465 l
= cloog_loop_nest(temp
->inner
, temp
->domain
, level
+1);
1466 new_loop
= cloog_loop_alloc(temp
->state
, temp
->domain
, temp
->state
->one
,
1467 temp
->state
->zero
, NULL
, l
, NULL
);
1468 temp
->inner
= NULL
;
1470 cloog_loop_free_parts(temp
,0,0,0,0) ;
1471 cloog_loop_add(&res
,&now
,new_loop
) ;
1475 /* 5. eliminate unused iterations of the current level for the new one. See
1476 * the example called linearity-1-1 example with and without this part
1479 if ((!options
->nobacktrack
) && level
&&
1480 ((level
+scalar
< options
->l
) || (options
->l
< 0)) &&
1481 ((options
->f
<= level
+scalar
) && !(options
->f
< 0)))
1482 res
= cloog_loop_generate_backtrack(res
, level
, options
);
1484 /* Pray for my new paper to be accepted somewhere since the following stuff
1485 * is really amazing :-) !
1486 * Far long later: The paper has been accepted to PACT 2004 :-))). But there
1487 * are still some bugs and I have no time to fix them. Thus now you have to
1488 * pray for me to get an academic position for that really amazing stuff :-) !
1489 * Later again: OK, I get my academic position, but still I have not enough
1490 * time to fix and clean this part... Pray again :-) !!!
1492 /* res = cloog_loop_unisolate(res,level) ;*/
1498 CloogLoop
*cloog_loop_generate_restricted(CloogLoop
*loop
,
1499 int level
, int scalar
, int *scaldims
, int nb_scattdims
,
1500 CloogOptions
*options
);
1504 * cloog_loop_generate_scalar function:
1505 * This function applies the simplified code generation scheme in the trivial
1506 * case of scalar dimensions. When dealing with scalar dimensions, there is
1507 * no need of costly polyhedral operations for separation or sorting: sorting
1508 * is a question of comparing scalar vectors and separation amounts to consider
1509 * only loops with the same scalar vector for the next step of the code
1510 * generation process. This function achieves the separation/sorting process
1511 * for the vector of scalar dimension that begins at dimension 'level+scalar'
1512 * and finish to the first non-scalar dimension.
1513 * - loop is the loop for which we have to generate a scanning code,
1514 * - level is the current non-scalar dimension,
1515 * - scalar is the current scalar dimension,
1516 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1517 * - nb_scattdims is the size of the scaldims array,
1518 * - options are the general code generation options.
1520 * - September 2nd 2005: First version.
1522 CloogLoop
*cloog_loop_generate_scalar(CloogLoop
*loop
,
1523 int level
, int scalar
, int *scaldims
, int nb_scattdims
,
1524 CloogOptions
*options
)
1525 { CloogLoop
* res
, * now
, * temp
, * l
, * end
, * next
, * ref
;
1528 /* We sort the loop list with respect to the current scalar vector. */
1529 res
= cloog_loop_scalar_sort(loop
,level
,scaldims
,nb_scattdims
,scalar
) ;
1531 scalar_new
= scalar
+ scaldims
[level
+ scalar
- 1];
1535 while (temp
!= NULL
)
1536 { /* Then we will appy the general code generation process to each sub-list
1537 * of loops with the same scalar vector.
1542 while((end
->next
!= NULL
) &&
1543 cloog_loop_more(end
->next
, level
, scalar_new
, nb_scattdims
) &&
1544 cloog_loop_scalar_eq(ref
,end
->next
,level
,scaldims
,nb_scattdims
,scalar
))
1550 /* For the next dimension, scalar value is updated by adding the scalar
1551 * vector size, which is stored at scaldims[level+scalar-1].
1553 if (cloog_loop_more(temp
, level
, scalar_new
, nb_scattdims
)) {
1554 l
= cloog_loop_generate_restricted(temp
, level
, scalar_new
,
1555 scaldims
, nb_scattdims
, options
);
1558 cloog_loop_add_list(&res
, &now
, l
);
1560 cloog_loop_add(&res
, &now
, temp
);
1569 /* Compare loop with the next loop based on their constant dimensions.
1570 * The result is < 0, == 0 or > 0 depending on whether the constant
1571 * dimensions of loop are lexicographically smaller, equal or greater
1572 * than those of loop->next.
1573 * If loop is the last in the list, then it is assumed to be smaller
1574 * than the "next" one.
1576 static int cloog_loop_next_scal_cmp(CloogLoop
*loop
)
1584 nb_scaldims
= loop
->block
->nb_scaldims
;
1585 if (loop
->next
->block
->nb_scaldims
< nb_scaldims
)
1586 nb_scaldims
= loop
->next
->block
->nb_scaldims
;
1588 for (i
= 0; i
< nb_scaldims
; ++i
) {
1589 int cmp
= cloog_int_cmp(loop
->block
->scaldims
[i
],
1590 loop
->next
->block
->scaldims
[i
]);
1594 return loop
->block
->nb_scaldims
- loop
->next
->block
->nb_scaldims
;
1598 /* Check whether the globally constant dimensions of a and b
1599 * have the same value for all globally constant dimensions
1600 * that are situated before any (locally) non-constant dimension.
1602 static int cloog_loop_equal_prefix(CloogLoop
*a
, CloogLoop
*b
,
1603 int *scaldims
, int nb_scattdims
)
1609 for (i
= 0; i
< nb_scattdims
; ++i
) {
1614 if (!cloog_int_eq(a
->block
->scaldims
[cst
], b
->block
->scaldims
[cst
]))
1618 for (i
= i
+ 1; i
< nb_scattdims
; ++i
) {
1621 if (!cloog_domain_lazy_isconstant(a
->domain
, dim
))
1623 /* No need to check that dim is also constant in b and that the
1624 * constant values are equal. That will happen during the check
1625 * whether the two domains are equal.
1633 /* Try to block adjacent loops in the loop list "loop".
1634 * We only attempt blocking if the constant dimensions of the loops
1635 * in the least are (not necessarily strictly) increasing.
1636 * Then we look for a sublist such that the first (begin) has constant
1637 * dimensions strictly larger than the previous loop in the complete
1638 * list and such that the loop (end) after the last loop in the sublist
1639 * has constant dimensions strictly larger than the last loop in the sublist.
1640 * Furthermore, all loops in the sublist should have the same domain
1641 * (with globally constant dimensions removed) and the difference
1642 * (if any) in constant dimensions may only occur after all the
1643 * (locally) constant dimensions.
1644 * If we find such a sublist, then the blocks of all but the first
1645 * are merged into the block of the first.
1647 * Note that this function can only be called before the global
1648 * blocklist has been created because it may otherwise modify and destroy
1649 * elements on that list.
1651 CloogLoop
*cloog_loop_block(CloogLoop
*loop
, int *scaldims
, int nb_scattdims
)
1653 CloogLoop
*begin
, *end
, *l
;
1654 int begin_after_previous
;
1655 int end_after_previous
;
1659 for (begin
= loop
; begin
; begin
= begin
->next
) {
1660 if (!begin
->block
|| !begin
->block
->scaldims
)
1662 if (cloog_loop_next_scal_cmp(loop
) > 0)
1666 begin_after_previous
= 1;
1667 for (begin
= loop
; begin
; begin
= begin
->next
) {
1668 if (!begin_after_previous
) {
1669 begin_after_previous
= cloog_loop_next_scal_cmp(begin
) < 0;
1673 end_after_previous
= cloog_loop_next_scal_cmp(begin
) < 0;
1674 for (end
= begin
->next
; end
; end
= end
->next
) {
1675 if (!cloog_loop_equal_prefix(begin
, end
, scaldims
, nb_scattdims
))
1677 if (!cloog_domain_lazy_equal(begin
->domain
, end
->domain
))
1679 end_after_previous
= cloog_loop_next_scal_cmp(end
) < 0;
1681 if (end
!= begin
->next
&& end_after_previous
) {
1682 for (l
= begin
->next
; l
!= end
; l
= begin
->next
) {
1683 cloog_block_merge(begin
->block
, l
->block
);
1684 begin
->next
= l
->next
;
1685 cloog_loop_free_parts(l
, 1, 0, 1, 0);
1689 begin_after_previous
= cloog_loop_next_scal_cmp(begin
) < 0;
1697 * Check whether for any fixed iteration of the outer loops,
1698 * there is an iteration of loop1 that is lexicographically greater
1699 * than an iteration of loop2.
1700 * Return 1 if there exists (or may exist) such a pair.
1701 * Return 0 if all iterations of loop1 are lexicographically smaller
1702 * than the iterations of loop2.
1703 * If no iteration is lexicographically greater, but if there are
1704 * iterations that are equal to iterations of loop2, then return "def".
1705 * This is useful for ensuring that such statements are not reordered.
1706 * Some users, including the test_run target in test, expect
1707 * the statements at a given point to be run in the original order.
1708 * Passing the value "0" for "def" would allow such statements to be reordered
1709 * and would allow for the detection of more components.
1711 int cloog_loop_follows(CloogLoop
*loop1
, CloogLoop
*loop2
,
1712 int level
, int scalar
, int *scaldims
, int nb_scattdims
, int def
)
1716 dim1
= cloog_domain_dimension(loop1
->domain
);
1717 dim2
= cloog_domain_dimension(loop2
->domain
);
1718 while ((level
<= dim1
&& level
<= dim2
) ||
1719 level_is_constant(level
, scalar
, scaldims
, nb_scattdims
)) {
1720 if (level_is_constant(level
, scalar
, scaldims
, nb_scattdims
)) {
1721 int cmp
= cloog_loop_constant_cmp(loop1
, loop2
, level
, scaldims
,
1722 nb_scattdims
, scalar
);
1727 scalar
+= scaldims
[level
+ scalar
- 1];
1729 int follows
= cloog_domain_follows(loop1
->domain
, loop2
->domain
,
1743 /* Structure for representing the nodes in the graph being traversed
1744 * using Tarjan's algorithm.
1745 * index represents the order in which nodes are visited.
1746 * min_index is the index of the root of a (sub)component.
1747 * on_stack indicates whether the node is currently on the stack.
1749 struct cloog_loop_sort_node
{
1754 /* Structure for representing the graph being traversed
1755 * using Tarjan's algorithm.
1756 * len is the number of nodes
1757 * node is an array of nodes
1758 * stack contains the nodes on the path from the root to the current node
1759 * sp is the stack pointer
1760 * index is the index of the last node visited
1761 * order contains the elements of the components separated by -1
1762 * op represents the current position in order
1764 struct cloog_loop_sort
{
1766 struct cloog_loop_sort_node
*node
;
1774 /* Allocate and initialize cloog_loop_sort structure.
1776 static struct cloog_loop_sort
*cloog_loop_sort_alloc(int len
)
1778 struct cloog_loop_sort
*s
;
1781 s
= (struct cloog_loop_sort
*)malloc(sizeof(struct cloog_loop_sort
));
1784 s
->node
= (struct cloog_loop_sort_node
*)
1785 malloc(len
* sizeof(struct cloog_loop_sort_node
));
1787 for (i
= 0; i
< len
; ++i
)
1788 s
->node
[i
].index
= -1;
1789 s
->stack
= (int *)malloc(len
* sizeof(int));
1791 s
->order
= (int *)malloc(2 * len
* sizeof(int));
1801 /* Free cloog_loop_sort structure.
1803 static void cloog_loop_sort_free(struct cloog_loop_sort
*s
)
1812 /* Perform Tarjan's algorithm for computing the strongly connected components
1813 * in the graph with the individual CloogLoops as vertices.
1814 * Two CloogLoops appear in the same component if some iterations of
1815 * each loop should be executed before some iterations of the other loop.
1816 * If two CloogLoops have exactly the same iteration domain, then they
1817 * are also placed in the same component.
1819 static void cloog_loop_components_tarjan(struct cloog_loop_sort
*s
,
1820 CloogLoop
**loop_array
, int i
, int level
, int scalar
, int *scaldims
,
1825 s
->node
[i
].index
= s
->index
;
1826 s
->node
[i
].min_index
= s
->index
;
1827 s
->node
[i
].on_stack
= 1;
1829 s
->stack
[s
->sp
++] = i
;
1831 for (j
= s
->len
- 1; j
>= 0; --j
) {
1836 if (s
->node
[j
].index
>= 0 &&
1837 (!s
->node
[j
].on_stack
||
1838 s
->node
[j
].index
> s
->node
[i
].min_index
))
1841 f
= cloog_domain_lazy_equal(loop_array
[i
]->domain
, loop_array
[j
]->domain
);
1843 f
= cloog_loop_follows(loop_array
[i
]->inner
, loop_array
[j
]->inner
,
1844 level
, scalar
, scaldims
, nb_scattdims
, i
> j
);
1848 if (s
->node
[j
].index
< 0) {
1849 cloog_loop_components_tarjan(s
, loop_array
, j
, level
, scalar
,
1850 scaldims
, nb_scattdims
);
1851 if (s
->node
[j
].min_index
< s
->node
[i
].min_index
)
1852 s
->node
[i
].min_index
= s
->node
[j
].min_index
;
1853 } else if (s
->node
[j
].index
< s
->node
[i
].min_index
)
1854 s
->node
[i
].min_index
= s
->node
[j
].index
;
1857 if (s
->node
[i
].index
!= s
->node
[i
].min_index
)
1861 j
= s
->stack
[--s
->sp
];
1862 s
->node
[j
].on_stack
= 0;
1863 s
->order
[s
->op
++] = j
;
1865 s
->order
[s
->op
++] = -1;
1869 static int qsort_index_cmp(const void *p1
, const void *p2
)
1871 return *(int *)p1
- *(int *)p2
;
1874 /* Sort the elements of the component starting at list.
1875 * The list is terminated by a -1.
1877 static void sort_component(int *list
)
1881 for (len
= 0; list
[len
] != -1; ++len
)
1884 qsort(list
, len
, sizeof(int), qsort_index_cmp
);
1889 * Call cloog_loop_generate_scalar or cloog_loop_generate_general
1890 * on each of the strongly connected components in the list of CloogLoops
1891 * pointed to by "loop".
1893 * We use Tarjan's algorithm to find the strongly connected components.
1894 * Note that this algorithm also topologically sorts the components.
1896 * The components are treated separately to avoid spurious separations.
1897 * The concatentation of the results may contain successive loops
1898 * with the same bounds, so we try to combine such loops.
1900 CloogLoop
*cloog_loop_generate_components(CloogLoop
*loop
,
1901 int level
, int scalar
, int *scaldims
, int nb_scattdims
,
1902 CloogOptions
*options
)
1905 CloogLoop
*tmp
, **tmp_next
;
1906 CloogLoop
*res
, **res_next
;
1907 CloogLoop
**loop_array
;
1908 struct cloog_loop_sort
*s
;
1910 if (level
== 0 || !loop
->next
)
1911 return cloog_loop_generate_general(loop
, level
, scalar
,
1912 scaldims
, nb_scattdims
, options
);
1914 nb_loops
= cloog_loop_count(loop
);
1916 loop_array
= (CloogLoop
**)malloc(nb_loops
* sizeof(CloogLoop
*));
1919 for (i
= 0, tmp
= loop
; i
< nb_loops
; i
++, tmp
= tmp
->next
)
1920 loop_array
[i
] = tmp
;
1922 s
= cloog_loop_sort_alloc(nb_loops
);
1923 for (i
= nb_loops
- 1; i
>= 0; --i
) {
1924 if (s
->node
[i
].index
>= 0)
1926 cloog_loop_components_tarjan(s
, loop_array
, i
, level
, scalar
, scaldims
,
1935 sort_component(&s
->order
[i
]);
1936 while (s
->order
[i
] != -1) {
1937 *tmp_next
= loop_array
[s
->order
[i
]];
1938 tmp_next
= &(*tmp_next
)->next
;
1944 *res_next
= cloog_loop_generate_general(tmp
, level
, scalar
,
1945 scaldims
, nb_scattdims
, options
);
1947 res_next
= &(*res_next
)->next
;
1950 cloog_loop_sort_free(s
);
1954 res
= cloog_loop_combine(res
);
1960 CloogLoop
*cloog_loop_generate_restricted(CloogLoop
*loop
,
1961 int level
, int scalar
, int *scaldims
, int nb_scattdims
,
1962 CloogOptions
*options
)
1964 /* To save both time and memory, we switch here depending on whether the
1965 * current dimension is scalar (simplified processing) or not (general
1968 if (level_is_constant(level
, scalar
, scaldims
, nb_scattdims
))
1969 return cloog_loop_generate_scalar(loop
, level
, scalar
,
1970 scaldims
, nb_scattdims
, options
);
1972 * 2. Compute the projection of each polyhedron onto the outermost
1973 * loop variable and the parameters.
1975 loop
= cloog_loop_project_all(loop
, level
);
1977 return cloog_loop_generate_components(loop
, level
, scalar
, scaldims
,
1978 nb_scattdims
, options
);
1983 * cloog_loop_generate function:
1984 * Adaptation from LoopGen 0.4 by F. Quillere. This function implements the
1985 * Quillere algorithm for polyhedron scanning from step 1 to 2.
1986 * (see the Quillere paper).
1987 * - loop is the loop for which we have to generate a scanning code,
1988 * - context is the context of the current loop (constraints on parameter and/or
1989 * on outer loop counters),
1990 * - level is the current non-scalar dimension,
1991 * - scalar is the current scalar dimension,
1992 * - scaldims is the boolean array saying whether a dimension is scalar or not,
1993 * - nb_scattdims is the size of the scaldims array,
1994 * - options are the general code generation options.
1996 * - October 26th 2001: first version.
1997 * - July 3rd->11th 2003: memory leaks hunt and correction.
1998 * - June 15th 2005: a memory leak fixed (loop was not entirely freed when
1999 * the result of cloog_loop_restrict was NULL).
2000 * - June 22nd 2005: Adaptation for GMP.
2001 * - September 2nd 2005: The function have been cutted out in two pieces:
2002 * cloog_loop_generate and this one, in order to handle
2003 * the scalar dimension case more efficiently with
2004 * cloog_loop_generate_scalar.
2005 * - November 15th 2005: (debug) Condition for stop option no more take care of
2006 * further scalar dimensions.
2008 CloogLoop
*cloog_loop_generate(CloogLoop
*loop
, CloogDomain
*context
,
2009 int level
, int scalar
, int *scaldims
, int nb_scattdims
,
2010 CloogOptions
*options
)
2012 /* If the user asked to stop code generation at this level, let's stop. */
2013 if ((options
->stop
>= 0) && (level
+scalar
>= options
->stop
+1))
2014 return cloog_loop_stop(loop
,context
) ;
2016 /* 1. Replace each polyhedron by its intersection with the context.
2018 loop
= cloog_loop_restrict_all(loop
, context
);
2022 return cloog_loop_generate_restricted(loop
, level
, scalar
, scaldims
,
2023 nb_scattdims
, options
);
2028 * Internal function for simplifying a single loop in a list of loops.
2029 * See cloog_loop_simplify.
2031 static CloogLoop
*loop_simplify(CloogLoop
*loop
, CloogDomain
*context
,
2035 CloogBlock
* new_block
;
2036 CloogLoop
*simplified
, *inner
;
2037 CloogDomain
* domain
, * simp
, * inter
, * extended_context
;
2039 if (!cloog_domain_isconvex(loop
->domain
))
2040 loop
->domain
= cloog_domain_simplify_union(loop
->domain
);
2042 domain
= loop
->domain
;
2044 domain_dim
= cloog_domain_dimension(domain
);
2045 extended_context
= cloog_domain_extend(context
, domain_dim
);
2046 inter
= cloog_domain_intersection(domain
,extended_context
) ;
2047 simp
= cloog_domain_simplify(inter
,extended_context
) ;
2048 cloog_domain_free(extended_context
) ;
2050 /* If the constraint system is never true, go to the next one. */
2051 if (cloog_domain_never_integral(simp
)) {
2052 cloog_loop_free(loop
->inner
);
2053 cloog_domain_free(inter
);
2054 cloog_domain_free(simp
);
2058 inner
= cloog_loop_simplify(loop
->inner
, inter
, level
+1);
2059 cloog_domain_free(inter
) ;
2061 if ((inner
== NULL
) && (loop
->block
== NULL
)) {
2062 cloog_domain_free(simp
);
2066 new_block
= cloog_block_copy(loop
->block
) ;
2068 simplified
= cloog_loop_alloc(loop
->state
, simp
, loop
->stride
, loop
->offset
,
2069 new_block
, inner
, NULL
);
2071 return(simplified
) ;
2076 * cloog_loop_simplify function:
2077 * This function implements the part 6. of the Quillere algorithm, it
2078 * recursively simplifies each loop in the context of the preceding loop domain.
2079 * It returns a pointer to the simplified loop list.
2080 * The cloog_domain_simplify (DomainSimplify) behaviour is really bad with
2081 * polyhedra union and some really awful sidesteppings were written, I plan
2083 * - October 31th 2001: first version.
2084 * - July 3rd->11th 2003: memory leaks hunt and correction.
2085 * - April 16th 2005: a memory leak fixed (extended_context was not freed).
2086 * - June 15th 2005: a memory leak fixed (loop was not conveniently freed
2087 * when the constraint system is never true).
2088 * - October 27th 2005: - this function called before cloog_loop_fast_simplify
2089 * is now the official cloog_loop_simplify function in
2090 * replacement of a slower and more complex one (after
2091 * deep changes in the pretty printer).
2092 * - we use cloog_loop_disjoint to fix the problem when
2093 * simplifying gives a union of polyhedra (before, it
2094 * was under the responsibility of the pretty printer).
2096 CloogLoop
*cloog_loop_simplify(CloogLoop
*loop
, CloogDomain
*context
, int level
)
2099 CloogLoop
*res
= NULL
;
2100 CloogLoop
**next
= &res
;
2102 for (now
= loop
; now
; now
= now
->next
) {
2103 *next
= loop_simplify(now
, context
, level
);
2105 now
->inner
= NULL
; /* For loop integrity. */
2106 cloog_domain_free(now
->domain
);
2110 next
= &(*next
)->next
;
2112 cloog_loop_free(loop
);
2114 /* Examples like test/iftest2.cloog give unions of polyhedra after
2115 * simplifying, thus we we have to disjoint them. Another good reason to
2116 * put the simplifying step in the Quillere backtrack.
2118 res
= cloog_loop_disjoint(res
);
2125 * cloog_loop_scatter function:
2126 * This function add the scattering (scheduling) informations in a loop.
2128 void cloog_loop_scatter(CloogLoop
* loop
, CloogScattering
*scatt
)
2130 loop
->domain
= cloog_domain_scatter(loop
->domain
, scatt
);