forked from periscop/cloog
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathloop.c
2781 lines (2344 loc) · 86.5 KB
/
loop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**-------------------------------------------------------------------**
** CLooG **
**-------------------------------------------------------------------**
** loop.c **
**-------------------------------------------------------------------**
** First version: october 26th 2001 **
**-------------------------------------------------------------------**/
/******************************************************************************
* CLooG : the Chunky Loop Generator (experimental) *
******************************************************************************
* *
* Copyright (C) 2001-2005 Cedric Bastoul *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
* CLooG, the Chunky Loop Generator *
* Written by Cedric Bastoul, [email protected] *
* *
******************************************************************************/
/* CAUTION: the english used for comments is probably the worst you ever read,
* please feel free to correct and improve it !
*/
# include <stdlib.h>
# include <stdio.h>
# include "../include/cloog/cloog.h"
#define ALLOC(type) (type*)malloc(sizeof(type))
static void cloog_loop_free_parts(CloogLoop *loop, int domain, int block,
int inner, int next);
static CloogLoop *cloog_loop_alloc(CloogState *state, CloogDomain *domain,
int otl, CloogStride *stride, CloogBlock *block, CloogLoop *inner,
CloogLoop *next);
static void cloog_loop_add_disjoint(CloogLoop **start, CloogLoop **now,
CloogLoop *loop);
static CloogLoop * cloog_loop_disjoint(CloogLoop * loop);
static CloogLoop *cloog_loop_restrict(CloogLoop *loop, CloogDomain *context);
static CloogLoop *cloog_loop_restrict_all(CloogLoop *loop,
CloogDomain *context);
static CloogLoop *cloog_loop_restrict_inner(CloogLoop *loop);
static CloogLoop * cloog_loop_project(CloogLoop * loop, int level);
static CloogLoop *cloog_loop_project_all(CloogLoop *loop, int level);
static CloogLoop *cloog_loop_combine(CloogLoop *loop);
static CloogLoop *cloog_loop_remove_empty_domain_loops(CloogLoop *loop);
static CloogLoop *cloog_loop_specialize(CloogLoop *loop, int level, int scalar,
int *scaldims, int nb_scattdims);
static CloogLoop *cloog_loop_propagate_lower_bound(CloogLoop *loop, int level);
static CloogLoop * cloog_loop_separate(CloogLoop * loop);
static CloogLoop *cloog_loop_merge(CloogLoop *loop, int level,
CloogOptions *options);
static CloogLoop *cloog_loop_nest(CloogLoop *loop, CloogDomain *context,
int level);
static void cloog_loop_stride(CloogLoop * loop, int level);
static void cloog_loop_otl(CloogLoop *loop, int level);
static CloogLoop * cloog_loop_stop(CloogLoop * loop, CloogDomain * context);
static int cloog_loop_constant_cmp(CloogLoop *l1, CloogLoop *l2, int level,
int *scaldims, int nb_scattdims, int scalar);
static int cloog_loop_scalar_gt(CloogLoop *l1, CloogLoop *l2, int level,
int *scaldims, int nb_scattdims, int scalar);
static int cloog_loop_scalar_eq(CloogLoop *l1, CloogLoop *l2, int level,
int *scaldims, int nb_scattdims, int scalar);
static CloogLoop * cloog_loop_scalar_sort(CloogLoop *loop, int level,
int *scaldims, int nb_scattdims, int scalar);
static CloogLoop *cloog_loop_generate_backtrack(CloogLoop *loop, int level,
CloogOptions *options);
static int cloog_loop_more(CloogLoop *loop, int level, int scalar,
int nb_scattdims);
static int cloog_loop_is_constant(CloogLoop *loop, int level);
static CloogLoop *cloog_loop_constant(CloogLoop *loop, int level);
static CloogLoop *cloog_loop_unroll(CloogLoop *loop, int level);
static void cloog_statement_get_fl(CloogStatement *s, int *f, int *l,
CloogOptions *options);
static void cloog_loop_get_fl(CloogLoop *loop, int *f, int *l,
CloogOptions *options);
static CloogLoop *cloog_loop_generate_general(CloogLoop *loop, int level,
int scalar, int *scaldims, int nb_scattdims, CloogOptions *options);
static CloogLoop *cloog_loop_generate_scalar(CloogLoop *loop, int level,
int scalar, int *scaldims, int nb_scattdims, CloogOptions *options);
static int cloog_loop_follows(CloogLoop *loop1, CloogLoop *loop2, int level,
int scalar, int *scaldims, int nb_scattdims, int def);
static CloogLoop *cloog_loop_generate_components(CloogLoop *loop, int level,
int scalar, int *scaldims, int nb_scattdims, CloogOptions *options);
/******************************************************************************
* Memory leaks hunting *
******************************************************************************/
/**
* These functions and global variables are devoted to memory leaks hunting: we
* want to know at each moment how many CloogLoop structures had been allocated
* (cloog_loop_allocated) and how many had been freed (cloog_loop_freed).
* Each time a CloogLoog structure is allocated, a call to the function
* cloog_loop_leak_up() must be carried out, and respectively
* cloog_loop_leak_down() when a CloogLoop structure is freed. The special
* variable cloog_loop_max gives the maximal number of CloogLoop structures
* simultaneously alive (i.e. allocated and non-freed) in memory.
* - July 3rd->11th 2003: first version (memory leaks hunt and correction).
*/
static void cloog_loop_leak_up(CloogState *state)
{
state->loop_allocated++;
if ((state->loop_allocated - state->loop_freed) > state->loop_max)
state->loop_max = state->loop_allocated - state->loop_freed;
}
static void cloog_loop_leak_down(CloogState *state)
{
state->loop_freed++;
}
/******************************************************************************
* Structure display function *
******************************************************************************/
/**
* cloog_loop_print_structure function:
* Displays a loop structure in a way that trends to be understandable without
* falling in a deep depression or, for the lucky ones, getting a headache...
* Written by Olivier Chorier, Luc Marchaud, Pierre Martin and Romain Tartiere.
* - April 24th 2005: Initial version.
* - May 21rd 2005: - New parameter `F' for destination file (ie stdout),
* - Minor tweaks.
* - May 26th 2005: Memory leak hunt.
* - June 2nd 2005: (Ced) Integration and minor fixes.
* -June 22nd 2005: (Ced) Adaptation for GMP.
*/
void cloog_loop_print_structure(FILE * file, CloogLoop * loop, int level)
{ int i, j, first=1 ;
if (loop)
{ /* Go to the right level. */
for (i=0; i<level; i++)
fprintf(file,"|\t") ;
fprintf(file,"+-- CloogLoop\n") ;
}
/* For each loop. */
while (loop)
{ if (!first)
{ /* Go to the right level. */
for (i=0; i<level; i++)
fprintf(file,"|\t") ;
fprintf(file,"| CloogLoop\n") ;
}
else
first = 0 ;
/* A blank line. */
for(j=0; j<=level+1; j++)
fprintf(file,"|\t") ;
fprintf(file,"\n") ;
/* Print the domain. */
cloog_domain_print_structure(file, loop->domain, level+1, "CloogDomain");
/* Print the stride. */
for(j=0; j<=level; j++)
fprintf(file,"|\t") ;
if (loop->stride) {
fprintf(file, "Stride: ");
cloog_int_print(file, loop->stride->stride);
fprintf(file, "\n");
fprintf(file, "Offset: ");
cloog_int_print(file, loop->stride->offset);
fprintf(file, "\n");
}
/* A blank line. */
for(j=0; j<=level+1; j++)
fprintf(file,"|\t") ;
fprintf(file,"\n") ;
/* Print the block. */
cloog_block_print_structure(file,loop->block,level+1) ;
/* A blank line. */
for (i=0; i<=level+1; i++)
fprintf(file,"|\t") ;
fprintf(file,"\n") ;
/* Print inner if any. */
if (loop->inner)
cloog_loop_print_structure(file,loop->inner,level+1) ;
/* And let's go for the next one. */
loop = loop->next ;
/* One more time something that is here only for a better look. */
if (!loop)
{ /* Two blank lines if this is the end of the linked list. */
for (j=0; j<2; j++)
{ for (i=0; i<=level; i++)
fprintf(file,"|\t") ;
fprintf(file,"\n") ;
}
}
else
{ /* A special blank line if the is a next loop. */
for (i=0; i<=level; i++)
fprintf(file,"|\t") ;
fprintf(file,"V\n") ;
}
}
}
/**
* cloog_loop_print function:
* This function prints the content of a CloogLoop structure (start) into a
* file (file, possibly stdout).
* - June 2nd 2005: Now this very old function (probably as old as CLooG) is
* only a frontend to cloog_loop_print_structure, with a quite
* better human-readable representation.
*/
void cloog_loop_print(FILE * file, CloogLoop * loop)
{ cloog_loop_print_structure(file,loop,0) ;
}
/******************************************************************************
* Memory deallocation function *
******************************************************************************/
/**
* cloog_loop_free function:
* This function frees the allocated memory for a CloogLoop structure (loop),
* and frees its inner loops and its next loops.
* - June 22nd 2005: Adaptation for GMP.
*/
void cloog_loop_free(CloogLoop * loop)
{ CloogLoop * next ;
while (loop != NULL) {
cloog_loop_leak_down(loop->state);
next = loop->next ;
cloog_domain_free(loop->domain) ;
cloog_domain_free(loop->unsimplified);
cloog_block_free(loop->block) ;
if (loop->inner != NULL)
cloog_loop_free(loop->inner) ;
cloog_stride_free(loop->stride);
free(loop) ;
loop = next ;
}
}
/**
* cloog_loop_free_parts function:
* This function frees the allocated memory for some parts of a CloogLoop
* structure (loop), each other argument is a boolean having to be set to 1 if
* we want to free the corresponding part, 0 otherwise. This function applies
* the same freeing policy to its inner ans next loops recursively.
* - July 3rd 2003: first version.
* - June 22nd 2005: Adaptation for GMP.
*/
void cloog_loop_free_parts(loop, domain, block, inner, next)
CloogLoop * loop ;
int domain, block, inner, next ;
{ CloogLoop * follow ;
while (loop != NULL) {
cloog_loop_leak_down(loop->state);
follow = loop->next ;
if (domain)
cloog_domain_free(loop->domain) ;
if (block)
cloog_block_free(loop->block) ;
if ((inner) && (loop->inner != NULL))
cloog_loop_free_parts(loop->inner,domain,block,inner,1) ;
cloog_domain_free(loop->unsimplified);
cloog_stride_free(loop->stride);
free(loop) ;
if (next)
loop = follow ;
else
loop = NULL ;
}
}
/******************************************************************************
* Reading functions *
******************************************************************************/
/**
* Construct a CloogLoop structure from a given iteration domain
* and statement number.
*/
CloogLoop *cloog_loop_from_domain(CloogState *state, CloogDomain *domain,
int number)
{
int nb_iterators;
CloogLoop * loop ;
CloogStatement * statement ;
/* Memory allocation and information reading for the first domain: */
loop = cloog_loop_malloc(state);
/* domain. */
loop->domain = domain;
if (loop->domain != NULL)
nb_iterators = cloog_domain_dimension(loop->domain);
else
nb_iterators = 0 ;
/* included statement block. */
statement = cloog_statement_alloc(state, number + 1);
loop->block = cloog_block_alloc(statement, 0, NULL, nb_iterators);
return loop ;
}
/**
* cloog_loop_read function:
* This function reads loop data from a file (foo, possibly stdin) and
* returns a pointer to a CloogLoop structure containing the read information.
* This function can be used only for input file reading, when one loop is
* associated with one statement.
* - number is the statement block number carried by the loop (-1 if none).
* - nb_parameters is the number of parameters.
**
* - September 9th 2002: first version.
* - April 16th 2005: adaptation to new CloogStatement struct (with number).
* - June 11th 2005: adaptation to new CloogBlock structure.
* - June 22nd 2005: Adaptation for GMP.
*/
CloogLoop *cloog_loop_read(CloogState *state,
FILE *foo, int number, int nb_parameters)
{
int op1, op2, op3;
char s[MAX_STRING];
CloogDomain *domain;
domain = cloog_domain_union_read(state, foo, nb_parameters);
/* To read that stupid "0 0 0" line. */
while (fgets(s,MAX_STRING,foo) == 0) ;
while ((*s=='#' || *s=='\n') || (sscanf(s," %d %d %d",&op1,&op2,&op3)<3))
if (fgets(s, MAX_STRING, foo))
continue;
return cloog_loop_from_domain(state, domain, number);
}
/******************************************************************************
* Processing functions *
******************************************************************************/
/**
* cloog_loop_malloc function:
* This function allocates the memory space for a CloogLoop structure and
* sets its fields with default values. Then it returns a pointer to the
* allocated space.
* - November 21th 2005: first version.
*/
CloogLoop *cloog_loop_malloc(CloogState *state)
{ CloogLoop * loop ;
/* Memory allocation for the CloogLoop structure. */
loop = (CloogLoop *)malloc(sizeof(CloogLoop)) ;
if (loop == NULL)
cloog_die("memory overflow.\n");
cloog_loop_leak_up(state);
/* We set the various fields with default values. */
loop->state = state;
loop->domain = NULL ;
loop->unsimplified = NULL;
loop->block = NULL ;
loop->usr = NULL;
loop->inner = NULL ;
loop->next = NULL ;
loop->otl = 0;
loop->stride = NULL;
return loop ;
}
/**
* cloog_loop_alloc function:
* This function allocates the memory space for a CloogLoop structure and
* sets its fields with those given as input. Then it returns a pointer to the
* allocated space.
* - October 27th 2001: first version.
* - June 22nd 2005: Adaptation for GMP.
* - November 21th 2005: use of cloog_loop_malloc.
*/
CloogLoop *cloog_loop_alloc(CloogState *state,
CloogDomain *domain, int otl, CloogStride *stride,
CloogBlock *block, CloogLoop *inner, CloogLoop *next)
{ CloogLoop * loop ;
loop = cloog_loop_malloc(state);
loop->domain = domain ;
loop->block = block ;
loop->inner = inner ;
loop->next = next ;
loop->otl = otl;
loop->stride = cloog_stride_copy(stride);
return(loop) ;
}
/**
* cloog_loop_add function:
* This function adds a CloogLoop structure (loop) at a given place (now) of a
* NULL terminated list of CloogLoop structures. The beginning of this list
* is (start). This function updates (now) to (loop), and updates (start) if the
* added element is the first one -that is when (start) is NULL-.
* - October 28th 2001: first version.
*/
void cloog_loop_add(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
{ if (*start == NULL)
{ *start = loop ;
*now = *start ;
}
else
{ (*now)->next = loop ;
*now = (*now)->next ;
}
}
/**
* cloog_loop_add function:
* This function adds a CloogLoop structure (loop) at a given place (now) of a
* NULL terminated list of CloogLoop structures. The beginning of this list
* is (start). This function updates (now) to the end of the loop list (loop),
* and updates (start) if the added element is the first one -that is when
* (start) is NULL-.
* - September 9th 2005: first version.
*/
void cloog_loop_add_list(CloogLoop ** start, CloogLoop ** now, CloogLoop * loop)
{ if (*start == NULL)
{ *start = loop ;
*now = *start ;
}
else
{ (*now)->next = loop ;
*now = (*now)->next ;
}
while ((*now)->next != NULL)
*now = (*now)->next ;
}
/**
* cloog_loop_copy function:
* This function returns a copy of the CloogLoop structure given as input. In
* fact, there is just new allocations for the CloogLoop structures, but their
* contents are the same.
* - October 28th 2001: first version.
* - July 3rd->11th 2003: memory leaks hunt and correction.
*/
CloogLoop * cloog_loop_copy(CloogLoop * source)
{ CloogLoop * loop ;
CloogBlock * block ;
CloogDomain * domain ;
loop = NULL ;
if (source != NULL)
{ domain = cloog_domain_copy(source->domain) ;
block = cloog_block_copy(source->block) ;
loop = cloog_loop_alloc(source->state, domain, source->otl,
source->stride, block, NULL, NULL);
loop->usr = source->usr;
loop->inner = cloog_loop_copy(source->inner) ;
loop->next = cloog_loop_copy(source->next) ;
}
return(loop) ;
}
/**
* cloog_loop_add_disjoint function:
* This function adds some CloogLoop structures at a given place (now) of a
* NULL terminated list of CloogLoop structures. The beginning of this list
* is (start). (loop) can be an union of polyhedra, this function separates the
* union into a list of *disjoint* polyhedra then adds the list. This function
* updates (now) to the end of the list and updates (start) if first added
* element is the first of the principal list -that is when (start) is NULL-.
* (loop) can be freed by this function, basically when its domain is actually
* a union of polyhedra, but don't worry, all the useful data are now stored
* inside the list (start). We do not use PolyLib's Domain_Disjoint function,
* since the number of union components is often higher (thus code size too).
* - October 28th 2001: first version.
* - November 14th 2001: bug correction (this one was hard to find !).
* - July 3rd->11th 2003: memory leaks hunt and correction.
* - June 22nd 2005: Adaptation for GMP.
* - October 27th 2005: (debug) included blocks were not copied for new loops.
*/
void cloog_loop_add_disjoint(start, now, loop)
CloogLoop ** start, ** now, * loop ;
{
CloogLoop * sep, * inner ;
CloogDomain *domain, *seen, *temp, *rest;
CloogBlock * block ;
if (cloog_domain_isconvex(loop->domain))
cloog_loop_add(start,now,loop) ;
else {
domain = cloog_domain_simplify_union(loop->domain);
loop->domain = NULL ;
/* We separate the first element of the rest of the union. */
domain = cloog_domain_cut_first(domain, &rest);
/* This first element is the first of the list of disjoint polyhedra. */
sep = cloog_loop_alloc(loop->state, domain, 0, NULL,
loop->block, loop->inner, NULL);
cloog_loop_add(start,now,sep) ;
seen = cloog_domain_copy(domain);
while (!cloog_domain_isempty(domain = rest)) {
temp = cloog_domain_cut_first(domain, &rest);
domain = cloog_domain_difference(temp, seen);
cloog_domain_free(temp);
if (cloog_domain_isempty(domain)) {
cloog_domain_free(domain);
continue;
}
/* Each new loop will have its own life, for instance we can free its
* inner loop and included block. Then each one must have its own copy
* of both 'inner' and 'block'.
*/
inner = cloog_loop_copy(loop->inner) ;
block = cloog_block_copy(loop->block) ;
sep = cloog_loop_alloc(loop->state, cloog_domain_copy(domain),
0, NULL, block, inner, NULL);
/* domain can be an union too. If so: recursion. */
if (cloog_domain_isconvex(domain))
cloog_loop_add(start,now,sep) ;
else
cloog_loop_add_disjoint(start,now,sep) ;
if (cloog_domain_isempty(rest)) {
cloog_domain_free(domain);
break;
}
seen = cloog_domain_union(seen, domain);
}
cloog_domain_free(rest);
cloog_domain_free(seen);
cloog_loop_free_parts(loop,0,0,0,0) ;
}
}
/**
* cloog_loop_disjoint function:
* This function returns a list of loops such that each loop with non-convex
* domain in the input list (loop) is separated into several loops where the
* domains are the components of the union of *disjoint* polyhedra equivalent
* to the original non-convex domain. See cloog_loop_add_disjoint comments
* for more details.
* - September 16th 2005: first version.
*/
CloogLoop * cloog_loop_disjoint(CloogLoop * loop)
{ CloogLoop *res=NULL, * now=NULL, * next ;
/* Because this is often the case, don't waste time ! */
if (loop && !loop->next && cloog_domain_isconvex(loop->domain))
return loop ;
while (loop != NULL)
{ next = loop->next ;
loop->next = NULL ;
cloog_loop_add_disjoint(&res,&now,loop) ;
loop = next ;
}
return res ;
}
/**
* cloog_loop_restrict function:
* This function returns the (loop) in the context of (context): it makes the
* intersection between the (loop) domain and the (context), then it returns
* a pointer to a new loop, with this intersection as domain.
**
* - October 27th 2001: first version.
* - June 15th 2005: a memory leak fixed (domain was not freed when empty).
* - June 22nd 2005: Adaptation for GMP.
*/
CloogLoop *cloog_loop_restrict(CloogLoop *loop, CloogDomain *context)
{ int new_dimension ;
CloogDomain * domain, * extended_context, * new_domain ;
CloogLoop * new_loop ;
domain = loop->domain ;
if (cloog_domain_dimension(domain) > cloog_domain_dimension(context))
{
new_dimension = cloog_domain_dimension(domain);
extended_context = cloog_domain_extend(context, new_dimension);
new_domain = cloog_domain_intersection(extended_context,loop->domain) ;
cloog_domain_free(extended_context) ;
}
else
new_domain = cloog_domain_intersection(context,loop->domain) ;
if (cloog_domain_isempty(new_domain))
{ cloog_domain_free(new_domain) ;
return(NULL) ;
}
else {
new_loop = cloog_loop_alloc(loop->state, new_domain,
0, NULL, loop->block, loop->inner, NULL);
return(new_loop) ;
}
}
/**
* Call cloog_loop_restrict on each loop in the list "loop" and return
* the concatenated result.
*/
CloogLoop *cloog_loop_restrict_all(CloogLoop *loop, CloogDomain *context)
{
CloogLoop *next;
CloogLoop *res = NULL;
CloogLoop **res_next = &res;
for (; loop; loop = next) {
next = loop->next;
*res_next = cloog_loop_restrict(loop, context);
if (*res_next) {
res_next = &(*res_next)->next;
cloog_loop_free_parts(loop, 1, 0, 0, 0);
} else {
loop->next = NULL;
cloog_loop_free(loop);
}
}
return res;
}
/**
* Restrict the domains of the inner loops of each loop l in the given
* list of loops to the domain of the loop l. If the domains of all
* inner loops of a given loop l turn out to be empty, then remove l
* from the list.
*/
CloogLoop *cloog_loop_restrict_inner(CloogLoop *loop)
{
CloogLoop *next;
CloogLoop *res;
CloogLoop **res_next = &res;
for (; loop; loop = next) {
next = loop->next;
loop->inner = cloog_loop_restrict_all(loop->inner, loop->domain);
if (loop->inner) {
*res_next = loop;
res_next = &(*res_next)->next;
} else {
loop->next = NULL;
cloog_loop_free(loop);
}
}
*res_next = NULL;
return res;
}
/**
* cloog_loop_project function:
* This function returns the projection of (loop) on the (level) first
* dimensions (outer loops). It makes the projection of the (loop) domain,
* then it returns a pointer to a new loop, with this projection as domain.
**
* - October 27th 2001: first version.
* - July 3rd->11th 2003: memory leaks hunt and correction.
* - June 22nd 2005: Adaptation for GMP.
*/
CloogLoop * cloog_loop_project(CloogLoop * loop, int level)
{
CloogDomain * new_domain ;
CloogLoop * new_loop, * copy ;
copy = cloog_loop_alloc(loop->state, loop->domain, loop->otl, loop->stride,
loop->block, loop->inner, NULL);
if (cloog_domain_dimension(loop->domain) == level)
new_domain = cloog_domain_copy(loop->domain) ;
else
new_domain = cloog_domain_project(loop->domain, level);
new_loop = cloog_loop_alloc(loop->state, new_domain, 0, NULL,
NULL, copy, NULL);
return(new_loop) ;
}
/**
* Call cloog_loop_project on each loop in the list "loop" and return
* the concatenated result.
*/
CloogLoop *cloog_loop_project_all(CloogLoop *loop, int level)
{
CloogLoop *next;
CloogLoop *res = NULL;
CloogLoop **res_next = &res;
for (; loop; loop = next) {
next = loop->next;
*res_next = cloog_loop_project(loop, level);
res_next = &(*res_next)->next;
cloog_loop_free_parts(loop, 0, 0, 0, 0);
}
return res;
}
/**
* cloog_loop_concat function:
* This function returns a pointer to the concatenation of the
* CloogLoop lists given as input.
* - October 28th 2001: first version.
*/
CloogLoop * cloog_loop_concat(CloogLoop * a, CloogLoop * b)
{ CloogLoop * loop, * temp ;
loop = a ;
temp = loop ;
if (loop != NULL)
{ while (temp->next != NULL)
temp = temp->next ;
temp->next = b ;
}
else
loop = b ;
return(loop) ;
}
/**
* cloog_loop_combine:
* Combine consecutive loops with identical domains into
* a single loop with the concatenation of their inner loops
* as inner loop.
*/
CloogLoop *cloog_loop_combine(CloogLoop *loop)
{
CloogLoop *first, *second;
for (first = loop; first; first = first->next) {
while (first->next) {
if (!cloog_domain_lazy_equal(first->domain, first->next->domain))
break;
second = first->next;
first->inner = cloog_loop_concat(first->inner, second->inner);
first->next = second->next;
cloog_loop_free_parts(second, 1, 0, 0, 0);
}
}
return loop;
}
/**
* Remove loops from list that have an empty domain.
*/
CloogLoop *cloog_loop_remove_empty_domain_loops(CloogLoop *loop)
{
CloogLoop *l, *res, *next, **res_next;
res = NULL;
res_next = &res;
for (l = loop; l; l = next) {
next = l->next;
if (cloog_domain_isempty(l->domain))
cloog_loop_free_parts(l, 1, 1, 1, 0);
else {
*res_next = l;
res_next = &(*res_next)->next;
}
}
*res_next = NULL;
return res;
}
CloogLoop *cloog_loop_decompose_inner(CloogLoop *loop,
int level, int scalar, int *scaldims, int nb_scattdims);
/* For each loop with only one inner loop, replace the domain
* of the loop with the projection of the domain of the inner
* loop. To increase the number of loops with a single inner
* we first decompose the inner loops into strongly connected
* components.
*/
CloogLoop *cloog_loop_specialize(CloogLoop *loop,
int level, int scalar, int *scaldims, int nb_scattdims)
{
int dim;
CloogDomain *domain;
CloogLoop *l;
loop = cloog_loop_decompose_inner(loop, level, scalar,
scaldims, nb_scattdims);
for (l = loop; l; l = l->next) {
if (l->inner->next)
continue;
if (!cloog_domain_isconvex(l->inner->domain))
continue;
dim = cloog_domain_dimension(l->domain);
domain = cloog_domain_project(l->inner->domain, dim);
if (cloog_domain_isconvex(domain)) {
cloog_domain_free(l->domain);
l->domain = domain;
} else {
cloog_domain_free(domain);
}
}
return cloog_loop_remove_empty_domain_loops(loop);
}
/* For each loop with only one inner loop, propagate the bounds from
* the inner loop domain to the outer loop domain. This is especially
* useful if the inner loop domain has a non-trivial stride which
* results in an update of the lower bound.
*/
CloogLoop *cloog_loop_propagate_lower_bound(CloogLoop *loop, int level)
{
int dim;
CloogDomain *domain, *t;
CloogLoop *l;
(void) level;
for (l = loop; l; l = l->next) {
if (l->inner->next)
continue;
if (!cloog_domain_isconvex(l->inner->domain))
continue;
dim = cloog_domain_dimension(l->domain);
domain = cloog_domain_project(l->inner->domain, dim);
if (cloog_domain_isconvex(domain)) {
t = cloog_domain_intersection(domain, l->domain);
cloog_domain_free(l->domain);
l->domain = t;
}
cloog_domain_free(domain);
}
return loop;
}
/**
* cloog_loop_separate function:
* This function implements the Quillere algorithm for separation of multiple
* loops: for a given set of polyhedra (loop), it computes a set of disjoint
* polyhedra such that the unions of these sets are equal, and returns this set.
* - October 28th 2001: first version.
* - November 14th 2001: elimination of some unused blocks.
* - August 13th 2002: (debug) in the case of union of polyhedra for one
* loop, redundant constraints are fired.
* - July 3rd->11th 2003: memory leaks hunt and correction.
* - June 22nd 2005: Adaptation for GMP.
* - October 16th 2005: Removal of the non-shared constraint elimination when
* there is only one loop in the list (seems to work
* without now, DomainSimplify may have been improved).
* The problem was visible with test/iftest2.cloog.
*/
CloogLoop * cloog_loop_separate(CloogLoop * loop)
{ int lazy_equal=0, disjoint = 0;
CloogLoop * new_loop, * new_inner, * res, * now, * temp, * Q,
* inner, * old /*, * previous, * next*/ ;
CloogDomain *UQ, *domain;
if (loop == NULL)
return NULL ;
loop = cloog_loop_combine(loop);
if (loop->next == NULL)
return cloog_loop_disjoint(loop) ;
UQ = cloog_domain_copy(loop->domain) ;
domain = cloog_domain_copy(loop->domain) ;
res = cloog_loop_alloc(loop->state, domain, 0, NULL,
loop->block, loop->inner, NULL);
old = loop ;
while((loop = loop->next) != NULL)
{ temp = NULL ;
/* For all Q, add Q-loop associated with the blocks of Q alone,
* and Q inter loop associated with the blocks of Q and loop.
*/
for (Q = res; Q; Q = Q->next) {
/* Add (Q inter loop). */
if ((disjoint = cloog_domain_lazy_disjoint(Q->domain,loop->domain)))
domain = NULL ;
else
{ if ((lazy_equal = cloog_domain_lazy_equal(Q->domain,loop->domain)))
domain = cloog_domain_copy(Q->domain) ;
else
domain = cloog_domain_intersection(Q->domain,loop->domain) ;
if (!cloog_domain_isempty(domain))
{ new_inner = cloog_loop_concat(cloog_loop_copy(Q->inner),
cloog_loop_copy(loop->inner)) ;
new_loop = cloog_loop_alloc(loop->state, domain, 0, NULL,
NULL, new_inner, NULL);
cloog_loop_add_disjoint(&temp,&now,new_loop) ;
}
else {
disjoint = 1;
cloog_domain_free(domain);
}
}
/* Add (Q - loop). */
if (disjoint)
domain = cloog_domain_copy(Q->domain) ;
else
{ if (lazy_equal)
domain = cloog_domain_empty(Q->domain);
else
domain = cloog_domain_difference(Q->domain,loop->domain) ;
}
if (!cloog_domain_isempty(domain)) {
new_loop = cloog_loop_alloc(loop->state, domain, 0, NULL,
NULL, Q->inner, NULL);
cloog_loop_add_disjoint(&temp,&now,new_loop) ;
}
else
{ cloog_domain_free(domain) ;
/* If Q->inner is no more useful, we can free it. */
inner = Q->inner ;
Q->inner = NULL ;
cloog_loop_free(inner) ;
}
}
/* Add loop-UQ associated with the blocks of loop alone.*/
if (cloog_domain_lazy_disjoint(loop->domain,UQ))
domain = cloog_domain_copy(loop->domain) ;
else