2 * Copyright 2011 INRIA Saclay
3 * Copyright 2014 Ecole Normale Superieure
4 * Copyright 2015 Sven Verdoolaege
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 #include <isl/space.h>
15 #include <isl_vec_private.h>
16 #include <isl_mat_private.h>
17 #include <isl_reordering.h>
19 #include <isl_local_private.h>
21 /* Return the isl_ctx to which "local" belongs.
23 isl_ctx
*isl_local_get_ctx(__isl_keep isl_local
*local
)
28 return isl_mat_get_ctx(local
);
31 /* Create an isl_local object from a matrix describing
34 * An isl_local object is current defined as exactly such a matrix,
35 * so simply return the input.
37 __isl_give isl_local
*isl_local_alloc_from_mat(__isl_take isl_mat
*mat
)
42 /* Return a new reference to "local".
44 __isl_give isl_local
*isl_local_copy(__isl_keep isl_local
*local
)
46 return isl_local_alloc_from_mat(isl_mat_copy(local
));
49 /* Free "local" and return NULL.
51 __isl_null isl_local
*isl_local_free(__isl_take isl_local
*local
)
57 /* Return the number of local variables (isl_dim_div),
58 * the number of other variables (isl_dim_set) or
59 * the total number of variables (isl_dim_all) in "local".
61 * Other types do not have any meaning for an isl_local object.
63 isl_size
isl_local_dim(__isl_keep isl_local
*local
, enum isl_dim_type type
)
68 return isl_size_error
;
69 if (type
== isl_dim_div
)
70 return isl_mat_rows(mat
);
71 if (type
== isl_dim_all
) {
72 isl_size cols
= isl_mat_cols(mat
);
74 return isl_size_error
;
77 if (type
== isl_dim_set
) {
78 isl_size total
, n_div
;
80 total
= isl_local_dim(local
, isl_dim_all
);
81 n_div
= isl_local_dim(local
, isl_dim_div
);
82 if (total
< 0 || n_div
< 0)
83 return isl_size_error
;
86 isl_die(isl_local_get_ctx(local
), isl_error_unsupported
,
87 "unsupported dimension type", return isl_size_error
);
91 #define TYPE isl_local
93 #include "check_type_range_templ.c"
95 /* Check that "pos" is a valid position for a variable in "local".
97 static isl_stat
isl_local_check_pos(__isl_keep isl_local
*local
, int pos
)
99 return isl_local_check_range(local
, isl_dim_div
, pos
, 1);
102 /* Given local variables "local",
103 * is the variable at position "pos" marked as not having
104 * an explicit representation?
105 * Note that even if this variable is not marked in this way and therefore
106 * does have an explicit representation, this representation may still
107 * depend (indirectly) on other local variables that do not
108 * have an explicit representation.
110 isl_bool
isl_local_div_is_marked_unknown(__isl_keep isl_local
*local
, int pos
)
112 isl_mat
*mat
= local
;
114 if (isl_local_check_pos(local
, pos
) < 0)
115 return isl_bool_error
;
116 return isl_bool_ok(isl_int_is_zero(mat
->row
[pos
][0]));
119 /* Given local variables "local",
120 * does the variable at position "pos" have a complete explicit representation?
121 * Having a complete explicit representation requires not only
122 * an explicit representation, but also that all local variables
123 * that appear in this explicit representation in turn have
124 * a complete explicit representation.
126 isl_bool
isl_local_div_is_known(__isl_keep isl_local
*local
, int pos
)
131 isl_mat
*mat
= local
;
133 if (isl_local_check_pos(local
, pos
) < 0)
134 return isl_bool_error
;
136 marked
= isl_local_div_is_marked_unknown(local
, pos
);
137 if (marked
< 0 || marked
)
138 return isl_bool_not(marked
);
140 n
= isl_local_dim(local
, isl_dim_div
);
141 cols
= isl_mat_cols(mat
);
142 if (n
< 0 || cols
< 0)
143 return isl_bool_error
;
146 for (i
= n
- 1; i
>= 0; --i
) {
149 if (isl_int_is_zero(mat
->row
[pos
][off
+ i
]))
151 known
= isl_local_div_is_known(local
, i
);
152 if (known
< 0 || !known
)
156 return isl_bool_true
;
159 /* Does "local" have an explicit representation for all local variables?
161 isl_bool
isl_local_divs_known(__isl_keep isl_local
*local
)
166 n
= isl_local_dim(local
, isl_dim_div
);
168 return isl_bool_error
;
170 for (i
= 0; i
< n
; ++i
) {
171 isl_bool unknown
= isl_local_div_is_marked_unknown(local
, i
);
172 if (unknown
< 0 || unknown
)
173 return isl_bool_not(unknown
);
176 return isl_bool_true
;
179 /* Compare two sets of local variables, defined over
182 * Return -1 if "local1" is "smaller" than "local2", 1 if "local1" is "greater"
183 * than "local2" and 0 if they are equal.
185 * The order is fairly arbitrary. We do "prefer" divs that only involve
186 * earlier dimensions in the sense that we consider matrices where
187 * the first differing div involves earlier dimensions to be smaller.
189 int isl_local_cmp(__isl_keep isl_local
*local1
, __isl_keep isl_local
*local2
)
193 isl_bool unknown1
, unknown2
;
196 isl_mat
*mat1
= local1
;
197 isl_mat
*mat2
= local2
;
199 if (local1
== local2
)
206 if (mat1
->n_row
!= mat2
->n_row
)
207 return mat1
->n_row
- mat2
->n_row
;
209 n_col
= isl_mat_cols(mat1
);
212 for (i
= 0; i
< mat1
->n_row
; ++i
) {
213 unknown1
= isl_local_div_is_marked_unknown(local1
, i
);
214 unknown2
= isl_local_div_is_marked_unknown(local2
, i
);
215 if (unknown1
&& unknown2
)
221 last1
= isl_seq_last_non_zero(mat1
->row
[i
] + 1, n_col
- 1);
222 last2
= isl_seq_last_non_zero(mat2
->row
[i
] + 1, n_col
- 1);
224 return last1
- last2
;
225 cmp
= isl_seq_cmp(mat1
->row
[i
], mat2
->row
[i
], n_col
);
233 /* Return the position of the variables of the given type
234 * within the sequence of variables of "local".
236 * Only the position of the local variables can be obtained.
237 * It is equal to the total number of variables minus
238 * the number of local variables.
240 isl_size
isl_local_var_offset(__isl_keep isl_local
*local
,
241 enum isl_dim_type type
)
243 isl_size n_div
, n_all
;
246 return isl_size_error
;
247 if (type
!= isl_dim_div
)
248 isl_die(isl_local_get_ctx(local
), isl_error_unsupported
,
249 "only the offset of the local variables "
250 "can be obtained", return isl_size_error
);
252 n_div
= isl_local_dim(local
, isl_dim_div
);
253 n_all
= isl_local_dim(local
, isl_dim_all
);
254 if (n_div
< 0 || n_all
< 0)
255 return isl_size_error
;
256 return n_all
- n_div
;
259 /* Reorder the columns of the given local variables according to the
261 * The order of the local variables themselves is assumed not to change.
263 __isl_give isl_local
*isl_local_reorder(__isl_take isl_local
*local
,
264 __isl_take isl_reordering
*r
)
266 isl_mat
*div
= local
;
274 extra
= r
->dst_len
- r
->src_len
;
275 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
279 for (i
= 0; i
< div
->n_row
; ++i
) {
280 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
281 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
282 for (j
= 0; j
< r
->src_len
; ++j
)
283 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
287 isl_reordering_free(r
);
288 isl_local_free(local
);
289 return isl_local_alloc_from_mat(mat
);
291 isl_reordering_free(r
);
292 isl_local_free(local
);
296 /* Move the "n" variables starting at "src_pos" of "local" to "dst_pos".
298 * Moving local variables is not allowed.
300 __isl_give isl_local
*isl_local_move_vars(__isl_take isl_local
*local
,
301 unsigned dst_pos
, unsigned src_pos
, unsigned n
)
303 isl_mat
*mat
= local
;
306 v_div
= isl_local_var_offset(local
, isl_dim_div
);
308 return isl_local_free(local
);
312 if (dst_pos
>= v_div
|| src_pos
>= v_div
)
313 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
314 "cannot move local variables",
315 return isl_local_free(local
));
317 mat
= isl_mat_move_cols(mat
, 2 + dst_pos
, 2 + src_pos
, n
);
319 return isl_local_alloc_from_mat(mat
);
322 /* Does "local" depend on the specified variables?
324 * If the specified variables are local variables themselves,
325 * then only later local variables could possibly depend on them.
327 isl_bool
isl_local_involves_vars(__isl_keep isl_local
*local
,
328 unsigned first
, unsigned n
)
330 isl_mat
*mat
= local
;
332 isl_size v_div
, n_div
;
334 v_div
= isl_local_var_offset(local
, isl_dim_div
);
335 n_div
= isl_local_dim(local
, isl_dim_div
);
336 if (v_div
< 0 || n_div
< 0 ||
337 isl_local_check_range(local
, isl_dim_all
, first
, n
) < 0)
338 return isl_bool_error
;
340 first_div
= (first
>= v_div
) ? first
- v_div
+ 1 : 0;
341 for (i
= first_div
; i
< n_div
; ++i
) {
344 unknown
= isl_local_div_is_marked_unknown(local
, i
);
346 return isl_bool_error
;
349 if (isl_seq_first_non_zero(mat
->row
[i
] + 1 + 1 + first
, n
) >= 0)
350 return isl_bool_true
;
353 return isl_bool_false
;
356 /* Extend a vector "v" representing an integer point
357 * in the domain space of "local"
358 * to one that also includes values for the local variables.
359 * All local variables are required to have an explicit representation.
360 * If there are no local variables, then the point is not required
363 __isl_give isl_vec
*isl_local_extend_point_vec(__isl_keep isl_local
*local
,
364 __isl_take isl_vec
*v
)
366 isl_size dim
, n_div
, size
;
368 isl_mat
*mat
= local
;
371 return isl_vec_free(v
);
372 known
= isl_local_divs_known(local
);
374 return isl_vec_free(v
);
376 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
377 "unknown local variables", return isl_vec_free(v
));
378 dim
= isl_local_dim(local
, isl_dim_set
);
379 n_div
= isl_local_dim(local
, isl_dim_div
);
380 size
= isl_vec_size(v
);
381 if (dim
< 0 || n_div
< 0 || size
< 0)
382 return isl_vec_free(v
);
384 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
385 "incorrect size", return isl_vec_free(v
));
388 if (!isl_int_is_one(v
->el
[0]))
389 isl_die(isl_local_get_ctx(local
), isl_error_invalid
,
390 "expecting integer point", return isl_vec_free(v
));
393 v
= isl_vec_add_els(v
, n_div
);
397 for (i
= 0; i
< n_div
; ++i
) {
398 isl_seq_inner_product(mat
->row
[i
] + 1, v
->el
,
399 1 + dim
+ i
, &v
->el
[1+dim
+i
]);
400 isl_int_fdiv_q(v
->el
[1+dim
+i
], v
->el
[1+dim
+i
],