add isl_band_dup
[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 __isl_give isl_band *isl_band_alloc(isl_ctx *ctx)
21 {
22         isl_band *band;
23
24         band = isl_calloc_type(ctx, isl_band);
25         if (!band)
26                 return NULL;
27
28         band->ref = 1;
29
30         return band;
31 }
32
33 /* Create a duplicate of the given band.  The duplicate refers
34  * to the same schedule and parent as the input, but does not
35  * increment their reference counts.
36  */
37 __isl_give isl_band *isl_band_dup(__isl_keep isl_band *band)
38 {
39         int i;
40         isl_ctx *ctx;
41         isl_band *dup;
42
43         if (!band)
44                 return NULL;
45
46         ctx = isl_band_get_ctx(band);
47         dup = isl_band_alloc(ctx);
48         if (!dup)
49                 return NULL;
50
51         dup->n = band->n;
52         dup->zero = isl_alloc_array(ctx, int, band->n);
53         if (!dup->zero)
54                 goto error;
55
56         for (i = 0; i < band->n; ++i)
57                 dup->zero[i] = band->zero[i];
58
59         dup->pma = isl_union_pw_multi_aff_copy(band->pma);
60         dup->schedule = band->schedule;
61         dup->parent = band->parent;
62
63         if (!dup->pma)
64                 goto error;
65
66         return dup;
67 error:
68         isl_band_free(dup);
69         return NULL;
70 }
71
72 /* We not only increment the reference count of the band,
73  * but also that of the schedule that contains this band.
74  * This ensures that the schedule won't disappear while there
75  * is still a reference to the band outside of the schedule.
76  * There is no need to increment the reference count of the parent
77  * band as the parent band is part of the same schedule.
78  */
79 __isl_give isl_band *isl_band_copy(__isl_keep isl_band *band)
80 {
81         if (!band)
82                 return NULL;
83
84         band->ref++;
85         band->schedule->ref++;
86         return band;
87 }
88
89 /* If this is not the last reference to the band (the one from within the
90  * schedule), then we also need to decrement the reference count of the
91  * containing schedule as it was incremented in isl_band_copy.
92  */
93 void *isl_band_free(__isl_take isl_band *band)
94 {
95         if (!band)
96                 return NULL;
97
98         if (--band->ref > 0)
99                 return isl_schedule_free(band->schedule);
100
101         isl_union_pw_multi_aff_free(band->pma);
102         isl_band_list_free(band->children);
103         free(band->zero);
104         free(band);
105
106         return NULL;
107 }
108
109 int isl_band_has_children(__isl_keep isl_band *band)
110 {
111         if (!band)
112                 return -1;
113
114         return band->children != NULL;
115 }
116
117 __isl_give isl_band_list *isl_band_get_children(
118         __isl_keep isl_band *band)
119 {
120         if (!band)
121                 return NULL;
122         if (!band->children)
123                 isl_die(isl_band_get_ctx(band), isl_error_invalid,
124                         "band has no children", return NULL);
125         return isl_band_list_dup(band->children);
126 }
127
128 int isl_band_n_member(__isl_keep isl_band *band)
129 {
130         return band ? band->n : 0;
131 }
132
133 /* Is the given scheduling dimension zero distance within the band and
134  * with respect to the proximity dependences.
135  */
136 int isl_band_member_is_zero_distance(__isl_keep isl_band *band, int pos)
137 {
138         if (!band)
139                 return -1;
140
141         if (pos < 0 || pos >= band->n)
142                 isl_die(isl_band_get_ctx(band), isl_error_invalid,
143                         "invalid member position", return -1);
144
145         return band->zero[pos];
146 }
147
148 /* Return the schedule that leads up to this band.
149  */
150 __isl_give isl_union_map *isl_band_get_prefix_schedule(
151         __isl_keep isl_band *band)
152 {
153         isl_union_set *domain;
154         isl_union_pw_multi_aff *prefix;
155         isl_band *a;
156
157         if (!band)
158                 return NULL;
159
160         prefix = isl_union_pw_multi_aff_copy(band->pma);
161         domain = isl_union_pw_multi_aff_domain(prefix);
162         prefix = isl_union_pw_multi_aff_from_domain(domain);
163
164         for (a = band->parent; a; a = a->parent) {
165                 isl_union_pw_multi_aff *partial;
166
167                 partial = isl_union_pw_multi_aff_copy(a->pma);
168                 prefix = isl_union_pw_multi_aff_flat_range_product(partial,
169                                                                    prefix);
170         }
171
172         return isl_union_map_from_union_pw_multi_aff(prefix);
173 }
174
175 /* Return the schedule of the band in isolation.
176  */
177 __isl_give isl_union_pw_multi_aff *
178 isl_band_get_partial_schedule_union_pw_multi_aff(__isl_keep isl_band *band)
179 {
180         return band ? isl_union_pw_multi_aff_copy(band->pma) : NULL;
181 }
182
183 /* Return the schedule of the band in isolation.
184  */
185 __isl_give isl_union_map *isl_band_get_partial_schedule(
186         __isl_keep isl_band *band)
187 {
188         isl_union_pw_multi_aff *sched;
189
190         sched = isl_band_get_partial_schedule_union_pw_multi_aff(band);
191         return isl_union_map_from_union_pw_multi_aff(sched);
192 }
193
194 __isl_give isl_union_pw_multi_aff *
195 isl_band_get_suffix_schedule_union_pw_multi_aff(__isl_keep isl_band *band);
196
197 /* Return the schedule for the given band list.
198  * For each band in the list, the schedule is composed of the partial
199  * and suffix schedules of that band.
200  */
201 __isl_give isl_union_pw_multi_aff *
202 isl_band_list_get_suffix_schedule_union_pw_multi_aff(
203         __isl_keep isl_band_list *list)
204 {
205         isl_ctx *ctx;
206         int i, n;
207         isl_space *space;
208         isl_union_pw_multi_aff *suffix;
209
210         if (!list)
211                 return NULL;
212
213         ctx = isl_band_list_get_ctx(list);
214         space = isl_space_alloc(ctx, 0, 0, 0);
215         suffix = isl_union_pw_multi_aff_empty(space);
216         n = isl_band_list_n_band(list);
217         for (i = 0; i < n; ++i) {
218                 isl_band *el;
219                 isl_union_pw_multi_aff *partial;
220                 isl_union_pw_multi_aff *suffix_i;
221
222                 el = isl_band_list_get_band(list, i);
223                 partial = isl_band_get_partial_schedule_union_pw_multi_aff(el);
224                 suffix_i = isl_band_get_suffix_schedule_union_pw_multi_aff(el);
225                 suffix_i = isl_union_pw_multi_aff_flat_range_product(
226                                 partial, suffix_i);
227                 suffix = isl_union_pw_multi_aff_add(suffix, suffix_i);
228
229                 isl_band_free(el);
230         }
231
232         return suffix;
233 }
234
235 /* Return the schedule for the given band list.
236  * For each band in the list, the schedule is composed of the partial
237  * and suffix schedules of that band.
238  */
239 __isl_give isl_union_map *isl_band_list_get_suffix_schedule(
240         __isl_keep isl_band_list *list)
241 {
242         isl_union_pw_multi_aff *suffix;
243
244         suffix = isl_band_list_get_suffix_schedule_union_pw_multi_aff(list);
245         return isl_union_map_from_union_pw_multi_aff(suffix);
246 }
247
248 /* Return the schedule for the forest underneath the given band.
249  */
250 __isl_give isl_union_pw_multi_aff *
251 isl_band_get_suffix_schedule_union_pw_multi_aff(__isl_keep isl_band *band)
252 {
253         isl_union_pw_multi_aff *suffix;
254
255         if (!band)
256                 return NULL;
257
258         if (!isl_band_has_children(band)) {
259                 isl_union_set *domain;
260
261                 suffix = isl_union_pw_multi_aff_copy(band->pma);
262                 domain = isl_union_pw_multi_aff_domain(suffix);
263                 suffix = isl_union_pw_multi_aff_from_domain(domain);
264         } else {
265                 isl_band_list *list;
266
267                 list = isl_band_get_children(band);
268                 suffix =
269                     isl_band_list_get_suffix_schedule_union_pw_multi_aff(list);
270                 isl_band_list_free(list);
271         }
272
273         return suffix;
274 }
275
276 /* Return the schedule for the forest underneath the given band.
277  */
278 __isl_give isl_union_map *isl_band_get_suffix_schedule(
279         __isl_keep isl_band *band)
280 {
281         isl_union_pw_multi_aff *suffix;
282
283         suffix = isl_band_get_suffix_schedule_union_pw_multi_aff(band);
284         return isl_union_map_from_union_pw_multi_aff(suffix);
285 }
286
287 __isl_give isl_printer *isl_printer_print_band(__isl_take isl_printer *p,
288         __isl_keep isl_band *band)
289 {
290         isl_union_map *prefix, *partial, *suffix;
291
292         prefix = isl_band_get_prefix_schedule(band);
293         partial = isl_band_get_partial_schedule(band);
294         suffix = isl_band_get_suffix_schedule(band);
295
296         p = isl_printer_print_str(p, "(");
297         p = isl_printer_print_union_map(p, prefix);
298         p = isl_printer_print_str(p, ",");
299         p = isl_printer_print_union_map(p, partial);
300         p = isl_printer_print_str(p, ",");
301         p = isl_printer_print_union_map(p, suffix);
302         p = isl_printer_print_str(p, ")");
303
304         isl_union_map_free(prefix);
305         isl_union_map_free(partial);
306         isl_union_map_free(suffix);
307
308         return p;
309 }