no longer use whole-component scheduler by default
[isl.git] / isl_local.c
blob4fb42bb3b52b39d5b38af060d22c0cab916b16b5
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl/space.h>
14 #include <isl_vec_private.h>
15 #include <isl_mat_private.h>
16 #include <isl_reordering.h>
17 #include <isl_seq.h>
18 #include <isl_local.h>
20 /* Return the isl_ctx to which "local" belongs.
22 isl_ctx *isl_local_get_ctx(__isl_keep isl_local *local)
24 if (!local)
25 return NULL;
27 return isl_mat_get_ctx(local);
30 /* Create an isl_local object from a matrix describing
31 * integer divisions.
33 * An isl_local object is current defined as exactly such a matrix,
34 * so simply return the input.
36 __isl_give isl_local *isl_local_alloc_from_mat(__isl_take isl_mat *mat)
38 return mat;
41 /* Free "local" and return NULL.
43 __isl_null isl_local *isl_local_free(__isl_take isl_local *local)
45 isl_mat_free(local);
46 return NULL;
49 /* Return the number of local variables (isl_dim_div),
50 * the number of other variables (isl_dim_set) or
51 * the total number of variables (isl_dim_all) in "local".
53 * Other types do not have any meaning for an isl_local object.
55 int isl_local_dim(__isl_keep isl_local *local, enum isl_dim_type type)
57 isl_mat *mat = local;
59 if (!local)
60 return 0;
61 if (type == isl_dim_div)
62 return isl_mat_rows(mat);
63 if (type == isl_dim_all)
64 return isl_mat_cols(mat) - 2;
65 if (type == isl_dim_set)
66 return isl_local_dim(local, isl_dim_all) -
67 isl_local_dim(local, isl_dim_div);
68 isl_die(isl_local_get_ctx(local), isl_error_unsupported,
69 "unsupported dimension type", return 0);
72 /* Check that "pos" is a valid position for a variable in "local".
74 static isl_stat isl_local_check_pos(__isl_keep isl_local *local, int pos)
76 if (!local)
77 return isl_stat_error;
78 if (pos < 0 || pos >= isl_local_dim(local, isl_dim_div))
79 isl_die(isl_local_get_ctx(local), isl_error_invalid,
80 "position out of bounds", return isl_stat_error);
81 return isl_stat_ok;
84 /* Given local variables "local",
85 * is the variable at position "pos" marked as not having
86 * an explicit representation?
87 * Note that even if this variable is not marked in this way and therefore
88 * does have an explicit representation, this representation may still
89 * depend (indirectly) on other local variables that do not
90 * have an explicit representation.
92 isl_bool isl_local_div_is_marked_unknown(__isl_keep isl_local *local, int pos)
94 isl_mat *mat = local;
96 if (isl_local_check_pos(local, pos) < 0)
97 return isl_bool_error;
98 return isl_int_is_zero(mat->row[pos][0]);
101 /* Given local variables "local",
102 * does the variable at position "pos" have a complete explicit representation?
103 * Having a complete explicit representation requires not only
104 * an explicit representation, but also that all local variables
105 * that appear in this explicit representation in turn have
106 * a complete explicit representation.
108 isl_bool isl_local_div_is_known(__isl_keep isl_local *local, int pos)
110 isl_bool marked;
111 int i, n, off;
112 isl_mat *mat = local;
114 if (isl_local_check_pos(local, pos) < 0)
115 return isl_bool_error;
117 marked = isl_local_div_is_marked_unknown(local, pos);
118 if (marked < 0 || marked)
119 return isl_bool_not(marked);
121 n = isl_local_dim(local, isl_dim_div);
122 off = isl_mat_cols(mat) - n;
124 for (i = n - 1; i >= 0; --i) {
125 isl_bool known;
127 if (isl_int_is_zero(mat->row[pos][off + i]))
128 continue;
129 known = isl_local_div_is_known(local, i);
130 if (known < 0 || !known)
131 return known;
134 return isl_bool_true;
137 /* Does "local" have an explicit representation for all local variables?
139 isl_bool isl_local_divs_known(__isl_keep isl_local *local)
141 int i, n;
143 if (!local)
144 return isl_bool_error;
146 n = isl_local_dim(local, isl_dim_div);
147 for (i = 0; i < n; ++i) {
148 isl_bool unknown = isl_local_div_is_marked_unknown(local, i);
149 if (unknown < 0 || unknown)
150 return isl_bool_not(unknown);
153 return isl_bool_true;
156 /* Compare two sets of local variables, defined over
157 * the same space.
159 * Return -1 if "local1" is "smaller" than "local2", 1 if "local1" is "greater"
160 * than "local2" and 0 if they are equal.
162 * The order is fairly arbitrary. We do "prefer" divs that only involve
163 * earlier dimensions in the sense that we consider matrices where
164 * the first differing div involves earlier dimensions to be smaller.
166 int isl_local_cmp(__isl_keep isl_local *local1, __isl_keep isl_local *local2)
168 int i;
169 int cmp;
170 isl_bool unknown1, unknown2;
171 int last1, last2;
172 int n_col;
173 isl_mat *mat1 = local1;
174 isl_mat *mat2 = local2;
176 if (local1 == local2)
177 return 0;
178 if (!local1)
179 return -1;
180 if (!local2)
181 return 1;
183 if (mat1->n_row != mat2->n_row)
184 return mat1->n_row - mat2->n_row;
186 n_col = isl_mat_cols(mat1);
187 for (i = 0; i < mat1->n_row; ++i) {
188 unknown1 = isl_local_div_is_marked_unknown(local1, i);
189 unknown2 = isl_local_div_is_marked_unknown(local2, i);
190 if (unknown1 && unknown2)
191 continue;
192 if (unknown1)
193 return 1;
194 if (unknown2)
195 return -1;
196 last1 = isl_seq_last_non_zero(mat1->row[i] + 1, n_col - 1);
197 last2 = isl_seq_last_non_zero(mat2->row[i] + 1, n_col - 1);
198 if (last1 != last2)
199 return last1 - last2;
200 cmp = isl_seq_cmp(mat1->row[i], mat2->row[i], n_col);
201 if (cmp != 0)
202 return cmp;
205 return 0;
208 /* Reorder the columns of the given local variables according to the
209 * given reordering.
210 * The order of the local variables themselves is assumed not to change.
212 __isl_give isl_local *isl_local_reorder(__isl_take isl_local *local,
213 __isl_take isl_reordering *r)
215 isl_mat *div = local;
216 int i, j;
217 isl_space *space;
218 isl_mat *mat;
219 int extra;
221 if (!local || !r)
222 goto error;
224 space = isl_reordering_peek_space(r);
225 extra = isl_space_dim(space, isl_dim_all) + div->n_row - r->len;
226 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
227 if (!mat)
228 goto error;
230 for (i = 0; i < div->n_row; ++i) {
231 isl_seq_cpy(mat->row[i], div->row[i], 2);
232 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
233 for (j = 0; j < r->len; ++j)
234 isl_int_set(mat->row[i][2 + r->pos[j]],
235 div->row[i][2 + j]);
238 isl_reordering_free(r);
239 isl_local_free(local);
240 return isl_local_alloc_from_mat(mat);
241 error:
242 isl_reordering_free(r);
243 isl_local_free(local);
244 return NULL;
247 /* Extend a vector "v" representing an integer point
248 * in the domain space of "local"
249 * to one that also includes values for the local variables.
250 * All local variables are required to have an explicit representation.
252 __isl_give isl_vec *isl_local_extend_point_vec(__isl_keep isl_local *local,
253 __isl_take isl_vec *v)
255 unsigned n_div;
256 isl_bool known;
257 isl_mat *mat = local;
259 if (!local || !v)
260 return isl_vec_free(v);
261 known = isl_local_divs_known(local);
262 if (known < 0)
263 return isl_vec_free(v);
264 if (!known)
265 isl_die(isl_local_get_ctx(local), isl_error_invalid,
266 "unknown local variables", return isl_vec_free(v));
267 if (isl_vec_size(v) != 1 + isl_local_dim(local, isl_dim_set))
268 isl_die(isl_local_get_ctx(local), isl_error_invalid,
269 "incorrect size", return isl_vec_free(v));
270 if (!isl_int_is_one(v->el[0]))
271 isl_die(isl_local_get_ctx(local), isl_error_invalid,
272 "expecting integer point", return isl_vec_free(v));
273 n_div = isl_local_dim(local, isl_dim_div);
274 if (n_div != 0) {
275 int i;
276 unsigned dim = isl_local_dim(local, isl_dim_set);
277 v = isl_vec_add_els(v, n_div);
278 if (!v)
279 return NULL;
281 for (i = 0; i < n_div; ++i) {
282 isl_seq_inner_product(mat->row[i] + 1, v->el,
283 1 + dim + i, &v->el[1+dim+i]);
284 isl_int_fdiv_q(v->el[1+dim+i], v->el[1+dim+i],
285 mat->row[i][0]);
289 return v;