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,
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>
18 #include <isl_local_private.h>
20 /* Return the isl_ctx to which "local" belongs.
22 isl_ctx
*isl_local_get_ctx(__isl_keep isl_local
*local
)
27 return isl_mat_get_ctx(local
);
30 /* Create an isl_local object from a matrix describing
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
)
41 /* Free "local" and return NULL.
43 __isl_null isl_local
*isl_local_free(__isl_take isl_local
*local
)
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 isl_size
isl_local_dim(__isl_keep isl_local
*local
, enum isl_dim_type type
)
60 return isl_size_error
;
61 if (type
== isl_dim_div
)
62 return isl_mat_rows(mat
);
63 if (type
== isl_dim_all
) {
64 isl_size cols
= isl_mat_cols(mat
);
66 return isl_size_error
;
69 if (type
== isl_dim_set
) {
70 isl_size total
, n_div
;
72 total
= isl_local_dim(local
, isl_dim_all
);
73 n_div
= isl_local_dim(local
, isl_dim_div
);
74 if (total
< 0 || n_div
< 0)
75 return isl_size_error
;
78 isl_die(isl_local_get_ctx(local
), isl_error_unsupported
,
79 "unsupported dimension type", return isl_size_error
);
83 #define TYPE isl_local
85 #include "check_type_range_templ.c"
87 /* Check that "pos" is a valid position for a variable in "local".
89 static isl_stat
isl_local_check_pos(__isl_keep isl_local
*local
, int pos
)
91 return isl_local_check_range(local
, isl_dim_div
, pos
, 1);
94 /* Given local variables "local",
95 * is the variable at position "pos" marked as not having
96 * an explicit representation?
97 * Note that even if this variable is not marked in this way and therefore
98 * does have an explicit representation, this representation may still
99 * depend (indirectly) on other local variables that do not
100 * have an explicit representation.
102 isl_bool
isl_local_div_is_marked_unknown(__isl_keep isl_local
*local
, int pos
)
104 isl_mat
*mat
= local
;
106 if (isl_local_check_pos(local
, pos
) < 0)
107 return isl_bool_error
;
108 return isl_bool_ok(isl_int_is_zero(mat
->row
[pos
][0]));
111 /* Given local variables "local",
112 * does the variable at position "pos" have a complete explicit representation?
113 * Having a complete explicit representation requires not only
114 * an explicit representation, but also that all local variables
115 * that appear in this explicit representation in turn have
116 * a complete explicit representation.
118 isl_bool
isl_local_div_is_known(__isl_keep isl_local
*local
, int pos
)
123 isl_mat
*mat
= local
;
125 if (isl_local_check_pos(local
, pos
) < 0)
126 return isl_bool_error
;
128 marked
= isl_local_div_is_marked_unknown(local
, pos
);
129 if (marked
< 0 || marked
)
130 return isl_bool_not(marked
);
132 n
= isl_local_dim(local
, isl_dim_div
);
133 cols
= isl_mat_cols(mat
);
134 if (n
< 0 || cols
< 0)
135 return isl_bool_error
;
138 for (i
= n
- 1; i
>= 0; --i
) {
141 if (isl_int_is_zero(mat
->row
[pos
][off
+ i
]))
143 known
= isl_local_div_is_known(local
, i
);
144 if (known
< 0 || !known
)
148 return isl_bool_true
;
151 /* Does "local" have an explicit representation for all local variables?
153 isl_bool
isl_local_divs_known(__isl_keep isl_local
*local
)
158 n
= isl_local_dim(local
, isl_dim_div
);
160 return isl_bool_error
;
162 for (i
= 0; i
< n
; ++i
) {
163 isl_bool unknown
= isl_local_div_is_marked_unknown(local
, i
);
164 if (unknown
< 0 || unknown
)
165 return isl_bool_not(unknown
);
168 return isl_bool_true
;
171 /* Compare two sets of local variables, defined over
174 * Return -1 if "local1" is "smaller" than "local2", 1 if "local1" is "greater"
175 * than "local2" and 0 if they are equal.
177 * The order is fairly arbitrary. We do "prefer" divs that only involve
178 * earlier dimensions in the sense that we consider matrices where
179 * the first differing div involves earlier dimensions to be smaller.
181 int isl_local_cmp(__isl_keep isl_local
*local1
, __isl_keep isl_local
*local2
)
185 isl_bool unknown1
, unknown2
;
188 isl_mat
*mat1
= local1
;
189 isl_mat
*mat2
= local2
;
191 if (local1
== local2
)
198 if (mat1
->n_row
!= mat2
->n_row
)
199 return mat1
->n_row
- mat2
->n_row
;
201 n_col
= isl_mat_cols(mat1
);
204 for (i
= 0; i
< mat1
->n_row
; ++i
) {
205 unknown1
= isl_local_div_is_marked_unknown(local1
, i
);
206 unknown2
= isl_local_div_is_marked_unknown(local2
, i
);
207 if (unknown1
&& unknown2
)
213 last1
= isl_seq_last_non_zero(mat1
->row
[i
] + 1, n_col
- 1);
214 last2
= isl_seq_last_non_zero(mat2
->row
[i
] + 1, n_col
- 1);
216 return last1
- last2
;
217 cmp
= isl_seq_cmp(mat1
->row
[i
], mat2
->row
[i
], n_col
);
225 /* Reorder the columns of the given local variables according to the
227 * The order of the local variables themselves is assumed not to change.
229 __isl_give isl_local
*isl_local_reorder(__isl_take isl_local
*local
,
230 __isl_take isl_reordering
*r
)
232 isl_mat
*div
= local
;
242 space
= isl_reordering_peek_space(r
);
243 dim
= isl_space_dim(space
, isl_dim_all
);
246 extra
= dim
+ div
->n_row
- r
->len
;
247 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
251 for (i
= 0; i
< div
->n_row
; ++i
) {
252 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
253 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
254 for (j
= 0; j
< r
->len
; ++j
)
255 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
259 isl_reordering_free(r
);
260 isl_local_free(local
);
261 return isl_local_alloc_from_mat(mat
);
263 isl_reordering_free(r
);
264 isl_local_free(local
);
268 /* Extend a vector "v" representing an integer point
269 * in the domain space of "local"
270 * to one that also includes values for the local variables.
271 * All local variables are required to have an explicit representation.
272 * If there are no local variables, then the point is not required
275 __isl_give isl_vec
*isl_local_extend_point_vec(__isl_keep isl_local
*local
,
276 __isl_take isl_vec
*v
)
278 isl_size dim
, n_div
, size
;
280 isl_mat
*mat
= local
;
283 return isl_vec_free(v
);
284 known
= isl_local_divs_known(local
);
286 return isl_vec_free(v
);
288 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
289 "unknown local variables", return isl_vec_free(v
));
290 dim
= isl_local_dim(local
, isl_dim_set
);
291 n_div
= isl_local_dim(local
, isl_dim_div
);
292 size
= isl_vec_size(v
);
293 if (dim
< 0 || n_div
< 0 || size
< 0)
294 return isl_vec_free(v
);
296 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
297 "incorrect size", return isl_vec_free(v
));
300 if (!isl_int_is_one(v
->el
[0]))
301 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
302 "expecting integer point", return isl_vec_free(v
));
305 v
= isl_vec_add_els(v
, n_div
);
309 for (i
= 0; i
< n_div
; ++i
) {
310 isl_seq_inner_product(mat
->row
[i
] + 1, v
->el
,
311 1 + dim
+ i
, &v
->el
[1+dim
+i
]);
312 isl_int_fdiv_q(v
->el
[1+dim
+i
], v
->el
[1+dim
+i
],