isl_band: use isl_union_pw_multi_aff to represent partial schedule
[platform/upstream/isl.git] / isl_band.c
1 /*
2  * Copyright 2011      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8  * 91893 Orsay, France
9  */
10
11 #include <isl_band_private.h>
12 #include <isl_schedule_private.h>
13 #include <isl_list_private.h>
14
15 isl_ctx *isl_band_get_ctx(__isl_keep isl_band *band)
16 {
17         return band ? isl_union_pw_multi_aff_get_ctx(band->pma) : NULL;
18 }
19
20 /* We not only increment the reference count of the band,
21  * but also that of the schedule that contains this band.
22  * This ensures that the schedule won't disappear while there
23  * is still a reference to the band outside of the schedule.
24  * There is no need to increment the reference count of the parent
25  * band as the parent band is part of the same schedule.
26  */
27 __isl_give isl_band *isl_band_copy(__isl_keep isl_band *band)
28 {
29         if (!band)
30                 return NULL;
31
32         band->ref++;
33         band->schedule->ref++;
34         return band;
35 }
36
37 /* If this is not the last reference to the band (the one from within the
38  * schedule), then we also need to decrement the reference count of the
39  * containing schedule as it was incremented in isl_band_copy.
40  */
41 void *isl_band_free(__isl_take isl_band *band)
42 {
43         if (!band)
44                 return NULL;
45
46         if (--band->ref > 0)
47                 return isl_schedule_free(band->schedule);
48
49         isl_union_pw_multi_aff_free(band->pma);
50         isl_band_list_free(band->children);
51         free(band->zero);
52         free(band);
53
54         return NULL;
55 }
56
57 int isl_band_has_children(__isl_keep isl_band *band)
58 {
59         if (!band)
60                 return -1;
61
62         return band->children != NULL;
63 }
64
65 __isl_give isl_band_list *isl_band_get_children(
66         __isl_keep isl_band *band)
67 {
68         if (!band)
69                 return NULL;
70         if (!band->children)
71                 isl_die(isl_band_get_ctx(band), isl_error_invalid,
72                         "band has no children", return NULL);
73         return isl_band_list_dup(band->children);
74 }
75
76 int isl_band_n_member(__isl_keep isl_band *band)
77 {
78         return band ? band->n : 0;
79 }
80
81 /* Is the given scheduling dimension zero distance within the band and
82  * with respect to the proximity dependences.
83  */
84 int isl_band_member_is_zero_distance(__isl_keep isl_band *band, int pos)
85 {
86         if (!band)
87                 return -1;
88
89         if (pos < 0 || pos >= band->n)
90                 isl_die(isl_band_get_ctx(band), isl_error_invalid,
91                         "invalid member position", return -1);
92
93         return band->zero[pos];
94 }
95
96 /* Return the schedule that leads up to this band.
97  */
98 __isl_give isl_union_map *isl_band_get_prefix_schedule(
99         __isl_keep isl_band *band)
100 {
101         isl_union_set *domain;
102         isl_union_pw_multi_aff *prefix;
103         isl_band *a;
104
105         if (!band)
106                 return NULL;
107
108         prefix = isl_union_pw_multi_aff_copy(band->pma);
109         domain = isl_union_pw_multi_aff_domain(prefix);
110         prefix = isl_union_pw_multi_aff_from_domain(domain);
111
112         for (a = band->parent; a; a = a->parent) {
113                 isl_union_pw_multi_aff *partial;
114
115                 partial = isl_union_pw_multi_aff_copy(a->pma);
116                 prefix = isl_union_pw_multi_aff_flat_range_product(partial,
117                                                                    prefix);
118         }
119
120         return isl_union_map_from_union_pw_multi_aff(prefix);
121 }
122
123 /* Return the schedule of the band in isolation.
124  */
125 __isl_give isl_union_pw_multi_aff *
126 isl_band_get_partial_schedule_union_pw_multi_aff(__isl_keep isl_band *band)
127 {
128         return band ? isl_union_pw_multi_aff_copy(band->pma) : NULL;
129 }
130
131 /* Return the schedule of the band in isolation.
132  */
133 __isl_give isl_union_map *isl_band_get_partial_schedule(
134         __isl_keep isl_band *band)
135 {
136         isl_union_pw_multi_aff *sched;
137
138         sched = isl_band_get_partial_schedule_union_pw_multi_aff(band);
139         return isl_union_map_from_union_pw_multi_aff(sched);
140 }
141
142 /* Return the schedule for the forest underneath the given band.
143  */
144 __isl_give isl_union_pw_multi_aff *
145 isl_band_get_suffix_schedule_union_pw_multi_aff(__isl_keep isl_band *band)
146 {
147         isl_union_pw_multi_aff *suffix;
148
149         if (!band)
150                 return NULL;
151
152         if (!isl_band_has_children(band)) {
153                 isl_union_set *domain;
154
155                 suffix = isl_union_pw_multi_aff_copy(band->pma);
156                 domain = isl_union_pw_multi_aff_domain(suffix);
157                 suffix = isl_union_pw_multi_aff_from_domain(domain);
158         } else {
159                 int i, n;
160                 isl_space *space;
161                 isl_band_list *children;
162
163                 space = isl_union_pw_multi_aff_get_space(band->pma);
164                 suffix = isl_union_pw_multi_aff_empty(space);
165                 children = isl_band_get_children(band);
166                 n = isl_band_list_n_band(children);
167                 for (i = 0; i < n; ++i) {
168                         isl_band *child;
169                         isl_union_pw_multi_aff *partial_i;
170                         isl_union_pw_multi_aff *suffix_i;
171
172                         child = isl_band_list_get_band(children, i);
173                         partial_i = isl_band_get_partial_schedule_union_pw_multi_aff(child);
174                         suffix_i = isl_band_get_suffix_schedule_union_pw_multi_aff(child);
175                         suffix_i = isl_union_pw_multi_aff_flat_range_product(
176                                         partial_i, suffix_i);
177                         suffix = isl_union_pw_multi_aff_add(suffix, suffix_i);
178
179                         isl_band_free(child);
180                 }
181                 isl_band_list_free(children);
182         }
183
184         return suffix;
185 }
186
187 /* Return the schedule for the forest underneath the given band.
188  */
189 __isl_give isl_union_map *isl_band_get_suffix_schedule(
190         __isl_keep isl_band *band)
191 {
192         isl_union_pw_multi_aff *suffix;
193
194         suffix = isl_band_get_suffix_schedule_union_pw_multi_aff(band);
195         return isl_union_map_from_union_pw_multi_aff(suffix);
196 }
197
198 __isl_give isl_printer *isl_printer_print_band(__isl_take isl_printer *p,
199         __isl_keep isl_band *band)
200 {
201         isl_union_map *prefix, *partial, *suffix;
202
203         prefix = isl_band_get_prefix_schedule(band);
204         partial = isl_band_get_partial_schedule(band);
205         suffix = isl_band_get_suffix_schedule(band);
206
207         p = isl_printer_print_str(p, "(");
208         p = isl_printer_print_union_map(p, prefix);
209         p = isl_printer_print_str(p, ",");
210         p = isl_printer_print_union_map(p, partial);
211         p = isl_printer_print_str(p, ",");
212         p = isl_printer_print_union_map(p, suffix);
213         p = isl_printer_print_str(p, ")");
214
215         isl_union_map_free(prefix);
216         isl_union_map_free(partial);
217         isl_union_map_free(suffix);
218
219         return p;
220 }