isl_test: avoid use of band forests
[isl.git] / isl_schedule_band.c
blob6c93d62d02ee46615656dcbc4f780681c06f1ac7
1 /*
2 * Copyright 2013-2014 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
10 #include <isl_schedule_band.h>
11 #include <isl_schedule_private.h>
13 isl_ctx *isl_schedule_band_get_ctx(__isl_keep isl_schedule_band *band)
15 return band ? isl_multi_union_pw_aff_get_ctx(band->mupa) : NULL;
18 /* Return a new uninitialized isl_schedule_band.
20 static __isl_give isl_schedule_band *isl_schedule_band_alloc(isl_ctx *ctx)
22 isl_schedule_band *band;
24 band = isl_calloc_type(ctx, isl_schedule_band);
25 if (!band)
26 return NULL;
28 band->ref = 1;
30 return band;
33 /* Return a new isl_schedule_band with partial schedule "mupa".
34 * First replace "mupa" by its greatest integer part to ensure
35 * that the schedule is always integral.
36 * The band is not marked permutable and the dimensions are not
37 * marked coincident.
39 __isl_give isl_schedule_band *isl_schedule_band_from_multi_union_pw_aff(
40 __isl_take isl_multi_union_pw_aff *mupa)
42 isl_ctx *ctx;
43 isl_schedule_band *band;
45 mupa = isl_multi_union_pw_aff_floor(mupa);
46 if (!mupa)
47 return NULL;
48 ctx = isl_multi_union_pw_aff_get_ctx(mupa);
49 band = isl_schedule_band_alloc(ctx);
50 if (!band)
51 goto error;
53 band->n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
54 band->coincident = isl_calloc_array(ctx, int, band->n);
55 band->mupa = mupa;
57 if (band->n && !band->coincident)
58 return isl_schedule_band_free(band);
60 return band;
61 error:
62 isl_multi_union_pw_aff_free(mupa);
63 return NULL;
66 /* Create a duplicate of the given isl_schedule_band.
68 __isl_give isl_schedule_band *isl_schedule_band_dup(
69 __isl_keep isl_schedule_band *band)
71 int i;
72 isl_ctx *ctx;
73 isl_schedule_band *dup;
75 if (!band)
76 return NULL;
78 ctx = isl_schedule_band_get_ctx(band);
79 dup = isl_schedule_band_alloc(ctx);
80 if (!dup)
81 return NULL;
83 dup->n = band->n;
84 dup->coincident = isl_alloc_array(ctx, int, band->n);
85 if (band->n && !dup->coincident)
86 return isl_schedule_band_free(dup);
88 for (i = 0; i < band->n; ++i)
89 dup->coincident[i] = band->coincident[i];
90 dup->permutable = band->permutable;
92 dup->mupa = isl_multi_union_pw_aff_copy(band->mupa);
93 if (!dup->mupa)
94 return isl_schedule_band_free(dup);
96 return dup;
99 /* Return an isl_schedule_band that is equal to "band" and that has only
100 * a single reference.
102 __isl_give isl_schedule_band *isl_schedule_band_cow(
103 __isl_take isl_schedule_band *band)
105 if (!band)
106 return NULL;
108 if (band->ref == 1)
109 return band;
110 band->ref--;
111 return isl_schedule_band_dup(band);
114 /* Return a new reference to "band".
116 __isl_give isl_schedule_band *isl_schedule_band_copy(
117 __isl_keep isl_schedule_band *band)
119 if (!band)
120 return NULL;
122 band->ref++;
123 return band;
126 /* Free a reference to "band" and return NULL.
128 __isl_null isl_schedule_band *isl_schedule_band_free(
129 __isl_take isl_schedule_band *band)
131 if (!band)
132 return NULL;
134 if (--band->ref > 0)
135 return NULL;
137 isl_multi_union_pw_aff_free(band->mupa);
138 free(band->coincident);
139 free(band);
141 return NULL;
144 /* Return the number of scheduling dimensions in the band.
146 int isl_schedule_band_n_member(__isl_keep isl_schedule_band *band)
148 return band ? band->n : 0;
151 /* Is the given scheduling dimension coincident within the band and
152 * with respect to the coincidence constraints?
154 int isl_schedule_band_member_get_coincident(__isl_keep isl_schedule_band *band,
155 int pos)
157 if (!band)
158 return -1;
160 if (pos < 0 || pos >= band->n)
161 isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
162 "invalid member position", return -1);
164 return band->coincident[pos];
167 /* Mark the given scheduling dimension as being coincident or not
168 * according to "coincident".
170 __isl_give isl_schedule_band *isl_schedule_band_member_set_coincident(
171 __isl_take isl_schedule_band *band, int pos, int coincident)
173 if (!band)
174 return NULL;
175 if (isl_schedule_band_member_get_coincident(band, pos) == coincident)
176 return band;
177 band = isl_schedule_band_cow(band);
178 if (!band)
179 return NULL;
181 if (pos < 0 || pos >= band->n)
182 isl_die(isl_schedule_band_get_ctx(band), isl_error_invalid,
183 "invalid member position",
184 isl_schedule_band_free(band));
186 band->coincident[pos] = coincident;
188 return band;
191 /* Is the schedule band mark permutable?
193 int isl_schedule_band_get_permutable(__isl_keep isl_schedule_band *band)
195 if (!band)
196 return -1;
197 return band->permutable;
200 /* Mark the schedule band permutable or not according to "permutable"?
202 __isl_give isl_schedule_band *isl_schedule_band_set_permutable(
203 __isl_take isl_schedule_band *band, int permutable)
205 if (!band)
206 return NULL;
207 if (band->permutable == permutable)
208 return band;
209 band = isl_schedule_band_cow(band);
210 if (!band)
211 return NULL;
213 band->permutable = permutable;
215 return band;
218 /* Return the schedule of the band in isolation.
220 __isl_give isl_multi_union_pw_aff *isl_schedule_band_get_partial_schedule(
221 __isl_keep isl_schedule_band *band)
223 return band ? isl_multi_union_pw_aff_copy(band->mupa) : NULL;