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