2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-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_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl_local_space_private.h>
17 #include <isl_space_private.h>
18 #include <isl_mat_private.h>
19 #include <isl_aff_private.h>
20 #include <isl_vec_private.h>
21 #include <isl_point_private.h>
23 #include <isl_local.h>
25 isl_ctx
*isl_local_space_get_ctx(__isl_keep isl_local_space
*ls
)
27 return ls
? ls
->dim
->ctx
: NULL
;
30 /* Return a hash value that digests "ls".
32 uint32_t isl_local_space_get_hash(__isl_keep isl_local_space
*ls
)
34 uint32_t hash
, space_hash
, div_hash
;
39 hash
= isl_hash_init();
40 space_hash
= isl_space_get_hash(isl_local_space_peek_space(ls
));
41 isl_hash_hash(hash
, space_hash
);
42 div_hash
= isl_mat_get_hash(ls
->div
);
43 isl_hash_hash(hash
, div_hash
);
48 __isl_give isl_local_space
*isl_local_space_alloc_div(
49 __isl_take isl_space
*space
, __isl_take isl_mat
*div
)
52 isl_local_space
*ls
= NULL
;
57 ctx
= isl_space_get_ctx(space
);
58 ls
= isl_calloc_type(ctx
, struct isl_local_space
);
69 isl_space_free(space
);
70 isl_local_space_free(ls
);
74 __isl_give isl_local_space
*isl_local_space_alloc(__isl_take isl_space
*space
,
84 total
= isl_space_dim(space
, isl_dim_all
);
86 return isl_local_space_from_space(isl_space_free(space
));
88 ctx
= isl_space_get_ctx(space
);
89 div
= isl_mat_alloc(ctx
, n_div
, 1 + 1 + total
+ n_div
);
90 return isl_local_space_alloc_div(space
, div
);
93 __isl_give isl_local_space
*isl_local_space_from_space(
94 __isl_take isl_space
*space
)
96 return isl_local_space_alloc(space
, 0);
99 __isl_give isl_local_space
*isl_local_space_copy(__isl_keep isl_local_space
*ls
)
108 __isl_give isl_local_space
*isl_local_space_dup(__isl_keep isl_local_space
*ls
)
113 return isl_local_space_alloc_div(isl_space_copy(ls
->dim
),
114 isl_mat_copy(ls
->div
));
118 __isl_give isl_local_space
*isl_local_space_cow(__isl_take isl_local_space
*ls
)
126 return isl_local_space_dup(ls
);
129 __isl_null isl_local_space
*isl_local_space_free(
130 __isl_take isl_local_space
*ls
)
138 isl_space_free(ls
->dim
);
139 isl_mat_free(ls
->div
);
146 /* Is the local space that of a parameter domain?
148 isl_bool
isl_local_space_is_params(__isl_keep isl_local_space
*ls
)
151 return isl_bool_error
;
152 return isl_space_is_params(ls
->dim
);
155 /* Is the local space that of a set?
157 isl_bool
isl_local_space_is_set(__isl_keep isl_local_space
*ls
)
159 return ls
? isl_space_is_set(ls
->dim
) : isl_bool_error
;
163 #define TYPE isl_local_space
165 #include "isl_type_has_equal_space_bin_templ.c"
167 /* Is the space of "ls" equal to "space"?
169 isl_bool
isl_local_space_has_space(__isl_keep isl_local_space
*ls
,
170 __isl_keep isl_space
*space
)
172 return isl_space_is_equal(isl_local_space_peek_space(ls
), space
);
175 /* Check that the space of "ls" is equal to "space".
177 static isl_stat
isl_local_space_check_has_space(__isl_keep isl_local_space
*ls
,
178 __isl_keep isl_space
*space
)
182 ok
= isl_local_space_has_space(ls
, space
);
184 return isl_stat_error
;
186 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
187 "spaces don't match", return isl_stat_error
);
191 /* Return true if the two local spaces are identical, with identical
192 * expressions for the integer divisions.
194 isl_bool
isl_local_space_is_equal(__isl_keep isl_local_space
*ls1
,
195 __isl_keep isl_local_space
*ls2
)
199 equal
= isl_local_space_has_equal_space(ls1
, ls2
);
200 if (equal
< 0 || !equal
)
203 if (!isl_local_space_divs_known(ls1
))
204 return isl_bool_false
;
205 if (!isl_local_space_divs_known(ls2
))
206 return isl_bool_false
;
208 return isl_mat_is_equal(ls1
->div
, ls2
->div
);
211 /* Compare two isl_local_spaces.
213 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
214 * than "ls2" and 0 if they are equal.
216 int isl_local_space_cmp(__isl_keep isl_local_space
*ls1
,
217 __isl_keep isl_local_space
*ls2
)
228 cmp
= isl_space_cmp(ls1
->dim
, ls2
->dim
);
232 return isl_local_cmp(ls1
->div
, ls2
->div
);
235 isl_size
isl_local_space_dim(__isl_keep isl_local_space
*ls
,
236 enum isl_dim_type type
)
239 return isl_size_error
;
240 if (type
== isl_dim_div
)
241 return ls
->div
->n_row
;
242 if (type
== isl_dim_all
) {
243 isl_size dim
= isl_space_dim(ls
->dim
, isl_dim_all
);
245 return isl_size_error
;
246 return dim
+ ls
->div
->n_row
;
248 return isl_space_dim(ls
->dim
, type
);
252 #define TYPE isl_local_space
253 #include "check_type_range_templ.c"
255 unsigned isl_local_space_offset(__isl_keep isl_local_space
*ls
,
256 enum isl_dim_type type
)
265 case isl_dim_cst
: return 0;
266 case isl_dim_param
: return 1;
267 case isl_dim_in
: return 1 + space
->nparam
;
268 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
270 return 1 + space
->nparam
+ space
->n_in
+ space
->n_out
;
275 /* Return the position of the dimension of the given type and name
277 * Return -1 if no such dimension can be found.
279 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space
*ls
,
280 enum isl_dim_type type
, const char *name
)
284 if (type
== isl_dim_div
)
286 return isl_space_find_dim_by_name(ls
->dim
, type
, name
);
289 /* Does the given dimension have a name?
291 isl_bool
isl_local_space_has_dim_name(__isl_keep isl_local_space
*ls
,
292 enum isl_dim_type type
, unsigned pos
)
294 return ls
? isl_space_has_dim_name(ls
->dim
, type
, pos
) : isl_bool_error
;
297 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space
*ls
,
298 enum isl_dim_type type
, unsigned pos
)
300 return ls
? isl_space_get_dim_name(ls
->dim
, type
, pos
) : NULL
;
303 isl_bool
isl_local_space_has_dim_id(__isl_keep isl_local_space
*ls
,
304 enum isl_dim_type type
, unsigned pos
)
306 return ls
? isl_space_has_dim_id(ls
->dim
, type
, pos
) : isl_bool_error
;
309 __isl_give isl_id
*isl_local_space_get_dim_id(__isl_keep isl_local_space
*ls
,
310 enum isl_dim_type type
, unsigned pos
)
312 return ls
? isl_space_get_dim_id(ls
->dim
, type
, pos
) : NULL
;
315 /* Return the argument of the integer division at position "pos" in "ls".
316 * All local variables in "ls" are known to have a (complete) explicit
319 static __isl_give isl_aff
*extract_div(__isl_keep isl_local_space
*ls
, int pos
)
323 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
326 isl_seq_cpy(aff
->v
->el
, ls
->div
->row
[pos
], aff
->v
->size
);
330 /* Return the argument of the integer division at position "pos" in "ls".
331 * The integer division at that position is known to have a complete
332 * explicit representation, but some of the others do not.
333 * Remove them first because the domain of an isl_aff
334 * is not allowed to have unknown local variables.
336 static __isl_give isl_aff
*drop_unknown_divs_and_extract_div(
337 __isl_keep isl_local_space
*ls
, int pos
)
344 n
= isl_local_space_dim(ls
, isl_dim_div
);
347 ls
= isl_local_space_copy(ls
);
348 for (i
= n
- 1; i
>= 0; --i
) {
349 unknown
= isl_local_space_div_is_marked_unknown(ls
, i
);
351 ls
= isl_local_space_free(ls
);
354 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
, i
, 1);
358 aff
= extract_div(ls
, pos
);
359 isl_local_space_free(ls
);
363 /* Return the argument of the integer division at position "pos" in "ls".
364 * The integer division is assumed to have a complete explicit
365 * representation. If some of the other integer divisions
366 * do not have an explicit representation, then they need
367 * to be removed first because the domain of an isl_aff
368 * is not allowed to have unknown local variables.
370 __isl_give isl_aff
*isl_local_space_get_div(__isl_keep isl_local_space
*ls
,
378 if (pos
< 0 || pos
>= ls
->div
->n_row
)
379 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
380 "index out of bounds", return NULL
);
382 known
= isl_local_space_div_is_known(ls
, pos
);
386 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
387 "expression of div unknown", return NULL
);
388 if (!isl_local_space_is_set(ls
))
389 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
390 "cannot represent divs of map spaces", return NULL
);
392 known
= isl_local_space_divs_known(ls
);
396 return extract_div(ls
, pos
);
398 return drop_unknown_divs_and_extract_div(ls
, pos
);
401 /* Return the space of "ls".
403 __isl_keep isl_space
*isl_local_space_peek_space(__isl_keep isl_local_space
*ls
)
411 __isl_give isl_space
*isl_local_space_get_space(__isl_keep isl_local_space
*ls
)
413 return isl_space_copy(isl_local_space_peek_space(ls
));
416 /* Return the space of "ls".
417 * This may be either a copy or the space itself
418 * if there is only one reference to "ls".
419 * This allows the space to be modified inplace
420 * if both the local space and its space have only a single reference.
421 * The caller is not allowed to modify "ls" between this call and
422 * a subsequent call to isl_local_space_restore_space.
423 * The only exception is that isl_local_space_free can be called instead.
425 __isl_give isl_space
*isl_local_space_take_space(__isl_keep isl_local_space
*ls
)
432 return isl_local_space_get_space(ls
);
438 /* Set the space of "ls" to "space", where the space of "ls" may be missing
439 * due to a preceding call to isl_local_space_take_space.
440 * However, in this case, "ls" only has a single reference and
441 * then the call to isl_local_space_cow has no effect.
443 __isl_give isl_local_space
*isl_local_space_restore_space(
444 __isl_take isl_local_space
*ls
, __isl_take isl_space
*space
)
449 if (ls
->dim
== space
) {
450 isl_space_free(space
);
454 ls
= isl_local_space_cow(ls
);
457 isl_space_free(ls
->dim
);
462 isl_local_space_free(ls
);
463 isl_space_free(space
);
467 /* Return the local variables of "ls".
469 __isl_keep isl_local
*isl_local_space_peek_local(__isl_keep isl_local_space
*ls
)
471 return ls
? ls
->div
: NULL
;
474 /* Replace the identifier of the tuple of type "type" by "id".
476 __isl_give isl_local_space
*isl_local_space_set_tuple_id(
477 __isl_take isl_local_space
*ls
,
478 enum isl_dim_type type
, __isl_take isl_id
*id
)
480 ls
= isl_local_space_cow(ls
);
483 ls
->dim
= isl_space_set_tuple_id(ls
->dim
, type
, id
);
485 return isl_local_space_free(ls
);
492 __isl_give isl_local_space
*isl_local_space_set_dim_name(
493 __isl_take isl_local_space
*ls
,
494 enum isl_dim_type type
, unsigned pos
, const char *s
)
496 ls
= isl_local_space_cow(ls
);
499 ls
->dim
= isl_space_set_dim_name(ls
->dim
, type
, pos
, s
);
501 return isl_local_space_free(ls
);
506 __isl_give isl_local_space
*isl_local_space_set_dim_id(
507 __isl_take isl_local_space
*ls
,
508 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
510 ls
= isl_local_space_cow(ls
);
513 ls
->dim
= isl_space_set_dim_id(ls
->dim
, type
, pos
, id
);
515 return isl_local_space_free(ls
);
523 /* Construct a zero-dimensional local space with the given parameter domain.
525 __isl_give isl_local_space
*isl_local_space_set_from_params(
526 __isl_take isl_local_space
*ls
)
530 space
= isl_local_space_take_space(ls
);
531 space
= isl_space_set_from_params(space
);
532 ls
= isl_local_space_restore_space(ls
, space
);
537 __isl_give isl_local_space
*isl_local_space_reset_space(
538 __isl_take isl_local_space
*ls
, __isl_take isl_space
*space
)
540 ls
= isl_local_space_cow(ls
);
544 isl_space_free(ls
->dim
);
549 isl_local_space_free(ls
);
550 isl_space_free(space
);
554 /* Reorder the dimensions of "ls" according to the given reordering.
555 * The reordering r is assumed to have been extended with the local
556 * variables, leaving them in the same order.
558 __isl_give isl_local_space
*isl_local_space_realign(
559 __isl_take isl_local_space
*ls
, __isl_take isl_reordering
*r
)
561 ls
= isl_local_space_cow(ls
);
565 ls
->div
= isl_local_reorder(ls
->div
, isl_reordering_copy(r
));
569 ls
= isl_local_space_reset_space(ls
, isl_reordering_get_space(r
));
571 isl_reordering_free(r
);
574 isl_local_space_free(ls
);
575 isl_reordering_free(r
);
579 __isl_give isl_local_space
*isl_local_space_add_div(
580 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*div
)
582 ls
= isl_local_space_cow(ls
);
586 if (ls
->div
->n_col
!= div
->size
)
587 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
588 "incompatible dimensions", goto error
);
590 ls
->div
= isl_mat_add_zero_cols(ls
->div
, 1);
591 ls
->div
= isl_mat_add_rows(ls
->div
, 1);
595 isl_seq_cpy(ls
->div
->row
[ls
->div
->n_row
- 1], div
->el
, div
->size
);
596 isl_int_set_si(ls
->div
->row
[ls
->div
->n_row
- 1][div
->size
], 0);
601 isl_local_space_free(ls
);
606 __isl_give isl_local_space
*isl_local_space_replace_divs(
607 __isl_take isl_local_space
*ls
, __isl_take isl_mat
*div
)
609 ls
= isl_local_space_cow(ls
);
614 isl_mat_free(ls
->div
);
619 isl_local_space_free(ls
);
623 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
626 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
627 __isl_keep isl_mat
*src
, int s
, int *exp
)
630 unsigned c
= src
->n_col
- src
->n_row
;
632 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
633 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
635 for (i
= 0; i
< s
; ++i
)
636 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
639 /* Compare (known) divs.
640 * Return non-zero if at least one of the two divs is unknown.
641 * In particular, if both divs are unknown, we respect their
642 * current order. Otherwise, we sort the known div after the unknown
643 * div only if the known div depends on the unknown div.
645 static int cmp_row(isl_int
*row_i
, isl_int
*row_j
, int i
, int j
,
646 unsigned n_row
, unsigned n_col
)
649 int unknown_i
, unknown_j
;
651 unknown_i
= isl_int_is_zero(row_i
[0]);
652 unknown_j
= isl_int_is_zero(row_j
[0]);
654 if (unknown_i
&& unknown_j
)
658 li
= n_col
- n_row
+ i
;
660 li
= isl_seq_last_non_zero(row_i
, n_col
);
662 lj
= n_col
- n_row
+ j
;
664 lj
= isl_seq_last_non_zero(row_j
, n_col
);
669 return isl_seq_cmp(row_i
, row_j
, n_col
);
672 /* Call cmp_row for divs in a matrix.
674 int isl_mat_cmp_div(__isl_keep isl_mat
*div
, int i
, int j
)
676 return cmp_row(div
->row
[i
], div
->row
[j
], i
, j
, div
->n_row
, div
->n_col
);
679 /* Call cmp_row for divs in a basic map.
681 static int bmap_cmp_row(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
684 return cmp_row(bmap
->div
[i
], bmap
->div
[j
], i
, j
, bmap
->n_div
, total
);
687 /* Sort the divs in "bmap".
689 * We first make sure divs are placed after divs on which they depend.
690 * Then we perform a simple insertion sort based on the same ordering
691 * that is used in isl_merge_divs.
693 __isl_give isl_basic_map
*isl_basic_map_sort_divs(
694 __isl_take isl_basic_map
*bmap
)
699 bmap
= isl_basic_map_order_divs(bmap
);
702 if (bmap
->n_div
<= 1)
705 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
707 return isl_basic_map_free(bmap
);
708 for (i
= 1; i
< bmap
->n_div
; ++i
) {
709 for (j
= i
- 1; j
>= 0; --j
) {
710 if (bmap_cmp_row(bmap
, j
, j
+ 1, 2 + total
) <= 0)
712 bmap
= isl_basic_map_swap_div(bmap
, j
, j
+ 1);
721 /* Sort the divs in the basic maps of "map".
723 __isl_give isl_map
*isl_map_sort_divs(__isl_take isl_map
*map
)
725 return isl_map_inline_foreach_basic_map(map
, &isl_basic_map_sort_divs
);
728 /* Combine the two lists of divs into a single list.
729 * For each row i in div1, exp1[i] is set to the position of the corresponding
730 * row in the result. Similarly for div2 and exp2.
731 * This function guarantees
733 * exp1[i+1] > exp1[i]
734 * For optimal merging, the two input list should have been sorted.
736 __isl_give isl_mat
*isl_merge_divs(__isl_keep isl_mat
*div1
,
737 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
746 d
= div1
->n_col
- div1
->n_row
;
747 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
748 d
+ div1
->n_row
+ div2
->n_row
);
752 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
755 expand_row(div
, k
, div1
, i
, exp1
);
756 expand_row(div
, k
+ 1, div2
, j
, exp2
);
758 cmp
= isl_mat_cmp_div(div
, k
, k
+ 1);
762 } else if (cmp
< 0) {
766 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
769 for (; i
< div1
->n_row
; ++i
, ++k
) {
770 expand_row(div
, k
, div1
, i
, exp1
);
773 for (; j
< div2
->n_row
; ++j
, ++k
) {
774 expand_row(div
, k
, div2
, j
, exp2
);
784 /* Swap divs "a" and "b" in "ls".
786 __isl_give isl_local_space
*isl_local_space_swap_div(
787 __isl_take isl_local_space
*ls
, int a
, int b
)
791 ls
= isl_local_space_cow(ls
);
794 if (a
< 0 || a
>= ls
->div
->n_row
|| b
< 0 || b
>= ls
->div
->n_row
)
795 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
796 "index out of bounds", return isl_local_space_free(ls
));
797 offset
= ls
->div
->n_col
- ls
->div
->n_row
;
798 ls
->div
= isl_mat_swap_cols(ls
->div
, offset
+ a
, offset
+ b
);
799 ls
->div
= isl_mat_swap_rows(ls
->div
, a
, b
);
801 return isl_local_space_free(ls
);
805 /* Construct a local space that contains all the divs in either
808 __isl_give isl_local_space
*isl_local_space_intersect(
809 __isl_take isl_local_space
*ls1
, __isl_take isl_local_space
*ls2
)
820 ctx
= isl_local_space_get_ctx(ls1
);
821 if (!isl_space_is_equal(ls1
->dim
, ls2
->dim
))
822 isl_die(ctx
, isl_error_invalid
,
823 "spaces should be identical", goto error
);
825 if (ls2
->div
->n_row
== 0) {
826 isl_local_space_free(ls2
);
830 if (ls1
->div
->n_row
== 0) {
831 isl_local_space_free(ls1
);
835 exp1
= isl_alloc_array(ctx
, int, ls1
->div
->n_row
);
836 exp2
= isl_alloc_array(ctx
, int, ls2
->div
->n_row
);
840 div
= isl_merge_divs(ls1
->div
, ls2
->div
, exp1
, exp2
);
844 equal
= isl_mat_is_equal(ls1
->div
, div
);
848 ls1
= isl_local_space_cow(ls1
);
854 isl_local_space_free(ls2
);
855 isl_mat_free(ls1
->div
);
863 isl_local_space_free(ls1
);
864 isl_local_space_free(ls2
);
868 /* Is the local variable "div" of "ls" marked as not having
869 * an explicit representation?
870 * Note that even if this variable is not marked in this way and therefore
871 * does have an explicit representation, this representation may still
872 * depend (indirectly) on other local variables that do not
873 * have an explicit representation.
875 isl_bool
isl_local_space_div_is_marked_unknown(__isl_keep isl_local_space
*ls
,
879 return isl_bool_error
;
880 return isl_local_div_is_marked_unknown(ls
->div
, div
);
883 /* Does "ls" have a complete explicit representation for div "div"?
885 isl_bool
isl_local_space_div_is_known(__isl_keep isl_local_space
*ls
, int div
)
888 return isl_bool_error
;
889 return isl_local_div_is_known(ls
->div
, div
);
892 /* Does "ls" have an explicit representation for all local variables?
894 isl_bool
isl_local_space_divs_known(__isl_keep isl_local_space
*ls
)
897 return isl_bool_error
;
898 return isl_local_divs_known(ls
->div
);
901 __isl_give isl_local_space
*isl_local_space_domain(
902 __isl_take isl_local_space
*ls
)
906 n_out
= isl_local_space_dim(ls
, isl_dim_out
);
908 return isl_local_space_free(ls
);
909 ls
= isl_local_space_drop_dims(ls
, isl_dim_out
, 0, n_out
);
910 ls
= isl_local_space_cow(ls
);
913 ls
->dim
= isl_space_domain(ls
->dim
);
915 return isl_local_space_free(ls
);
919 __isl_give isl_local_space
*isl_local_space_range(
920 __isl_take isl_local_space
*ls
)
924 n_in
= isl_local_space_dim(ls
, isl_dim_in
);
926 return isl_local_space_free(ls
);
927 ls
= isl_local_space_drop_dims(ls
, isl_dim_in
, 0, n_in
);
928 ls
= isl_local_space_cow(ls
);
932 ls
->dim
= isl_space_range(ls
->dim
);
934 return isl_local_space_free(ls
);
938 /* Construct a local space for a map that has the given local
939 * space as domain and that has a zero-dimensional range.
941 __isl_give isl_local_space
*isl_local_space_from_domain(
942 __isl_take isl_local_space
*ls
)
944 ls
= isl_local_space_cow(ls
);
947 ls
->dim
= isl_space_from_domain(ls
->dim
);
949 return isl_local_space_free(ls
);
953 __isl_give isl_local_space
*isl_local_space_add_dims(
954 __isl_take isl_local_space
*ls
, enum isl_dim_type type
, unsigned n
)
958 pos
= isl_local_space_dim(ls
, type
);
960 return isl_local_space_free(ls
);
961 return isl_local_space_insert_dims(ls
, type
, pos
, n
);
964 /* Lift the basic set "bset", living in the space of "ls"
965 * to live in a space with extra coordinates corresponding
966 * to the local variables of "ls".
968 __isl_give isl_basic_set
*isl_local_space_lift_basic_set(
969 __isl_take isl_local_space
*ls
, __isl_take isl_basic_set
*bset
)
973 isl_basic_set
*ls_bset
;
975 n_local
= isl_local_space_dim(ls
, isl_dim_div
);
976 space
= isl_basic_set_peek_space(bset
);
978 isl_local_space_check_has_space(ls
, space
) < 0)
982 isl_local_space_free(ls
);
986 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, n_local
);
987 ls_bset
= isl_basic_set_from_local_space(ls
);
988 ls_bset
= isl_basic_set_lift(ls_bset
);
989 ls_bset
= isl_basic_set_flatten(ls_bset
);
990 bset
= isl_basic_set_intersect(bset
, ls_bset
);
994 isl_local_space_free(ls
);
995 isl_basic_set_free(bset
);
999 /* Lift the set "set", living in the space of "ls"
1000 * to live in a space with extra coordinates corresponding
1001 * to the local variables of "ls".
1003 __isl_give isl_set
*isl_local_space_lift_set(__isl_take isl_local_space
*ls
,
1004 __isl_take isl_set
*set
)
1007 isl_basic_set
*bset
;
1009 n_local
= isl_local_space_dim(ls
, isl_dim_div
);
1011 isl_local_space_check_has_space(ls
, isl_set_peek_space(set
)) < 0)
1015 isl_local_space_free(ls
);
1019 set
= isl_set_add_dims(set
, isl_dim_set
, n_local
);
1020 bset
= isl_basic_set_from_local_space(ls
);
1021 bset
= isl_basic_set_lift(bset
);
1022 bset
= isl_basic_set_flatten(bset
);
1023 set
= isl_set_intersect(set
, isl_set_from_basic_set(bset
));
1027 isl_local_space_free(ls
);
1032 /* Remove common factor of non-constant terms and denominator.
1034 static __isl_give isl_local_space
*normalize_div(
1035 __isl_take isl_local_space
*ls
, int div
)
1037 isl_ctx
*ctx
= ls
->div
->ctx
;
1038 unsigned total
= ls
->div
->n_col
- 2;
1040 isl_seq_gcd(ls
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
1041 isl_int_gcd(ctx
->normalize_gcd
,
1042 ctx
->normalize_gcd
, ls
->div
->row
[div
][0]);
1043 if (isl_int_is_one(ctx
->normalize_gcd
))
1046 isl_seq_scale_down(ls
->div
->row
[div
] + 2, ls
->div
->row
[div
] + 2,
1047 ctx
->normalize_gcd
, total
);
1048 isl_int_divexact(ls
->div
->row
[div
][0], ls
->div
->row
[div
][0],
1049 ctx
->normalize_gcd
);
1050 isl_int_fdiv_q(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1],
1051 ctx
->normalize_gcd
);
1056 /* Exploit the equalities in "eq" to simplify the expressions of
1057 * the integer divisions in "ls".
1058 * The integer divisions in "ls" are assumed to appear as regular
1059 * dimensions in "eq".
1061 __isl_give isl_local_space
*isl_local_space_substitute_equalities(
1062 __isl_take isl_local_space
*ls
, __isl_take isl_basic_set
*eq
)
1065 isl_size total
, dim
;
1071 total
= isl_space_dim(eq
->dim
, isl_dim_all
);
1072 dim
= isl_local_space_dim(ls
, isl_dim_all
);
1073 if (dim
< 0 || total
< 0)
1076 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1077 "spaces don't match", goto error
);
1080 for (i
= 0; i
< eq
->n_eq
; ++i
) {
1081 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
1082 if (j
< 0 || j
== 0 || j
>= total
)
1085 for (k
= 0; k
< ls
->div
->n_row
; ++k
) {
1086 if (isl_int_is_zero(ls
->div
->row
[k
][1 + j
]))
1088 ls
= isl_local_space_cow(ls
);
1091 ls
->div
= isl_mat_cow(ls
->div
);
1094 isl_seq_elim(ls
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
1095 &ls
->div
->row
[k
][0]);
1096 ls
= normalize_div(ls
, k
);
1102 isl_basic_set_free(eq
);
1105 isl_basic_set_free(eq
);
1106 isl_local_space_free(ls
);
1110 /* Plug in the affine expressions "subs" of length "subs_len" (including
1111 * the denominator and the constant term) into the variable at position "pos"
1112 * of the "n" div expressions starting at "first".
1114 * Let i be the dimension to replace and let "subs" be of the form
1118 * Any integer division starting at "first" with a non-zero coefficient for i,
1120 * floor((a i + g)/m)
1124 * floor((a f + d g)/(m d))
1126 __isl_give isl_local_space
*isl_local_space_substitute_seq(
1127 __isl_take isl_local_space
*ls
,
1128 enum isl_dim_type type
, unsigned pos
, isl_int
*subs
, int subs_len
,
1136 ls
= isl_local_space_cow(ls
);
1139 ls
->div
= isl_mat_cow(ls
->div
);
1141 return isl_local_space_free(ls
);
1143 if (first
+ n
> ls
->div
->n_row
)
1144 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1145 "index out of bounds", return isl_local_space_free(ls
));
1147 pos
+= isl_local_space_offset(ls
, type
);
1150 for (i
= first
; i
< first
+ n
; ++i
) {
1151 if (isl_int_is_zero(ls
->div
->row
[i
][1 + pos
]))
1153 isl_seq_substitute(ls
->div
->row
[i
], pos
, subs
,
1154 ls
->div
->n_col
, subs_len
, v
);
1155 ls
= normalize_div(ls
, i
);
1164 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
1167 * Let i be the dimension to replace and let "subs" be of the form
1171 * Any integer division with a non-zero coefficient for i,
1173 * floor((a i + g)/m)
1177 * floor((a f + d g)/(m d))
1179 __isl_give isl_local_space
*isl_local_space_substitute(
1180 __isl_take isl_local_space
*ls
,
1181 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
1185 ls
= isl_local_space_cow(ls
);
1187 return isl_local_space_free(ls
);
1189 if (!isl_space_is_equal(ls
->dim
, subs
->ls
->dim
))
1190 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1191 "spaces don't match", return isl_local_space_free(ls
));
1192 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
1194 return isl_local_space_free(ls
);
1196 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
1197 "cannot handle divs yet",
1198 return isl_local_space_free(ls
));
1200 return isl_local_space_substitute_seq(ls
, type
, pos
, subs
->v
->el
,
1201 subs
->v
->size
, 0, ls
->div
->n_row
);
1204 isl_bool
isl_local_space_is_named_or_nested(__isl_keep isl_local_space
*ls
,
1205 enum isl_dim_type type
)
1208 return isl_bool_error
;
1209 return isl_space_is_named_or_nested(ls
->dim
, type
);
1212 __isl_give isl_local_space
*isl_local_space_drop_dims(
1213 __isl_take isl_local_space
*ls
,
1214 enum isl_dim_type type
, unsigned first
, unsigned n
)
1218 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
1221 if (isl_local_space_check_range(ls
, type
, first
, n
) < 0)
1222 return isl_local_space_free(ls
);
1224 ls
= isl_local_space_cow(ls
);
1228 if (type
== isl_dim_div
) {
1229 ls
->div
= isl_mat_drop_rows(ls
->div
, first
, n
);
1231 ls
->dim
= isl_space_drop_dims(ls
->dim
, type
, first
, n
);
1233 return isl_local_space_free(ls
);
1236 first
+= 1 + isl_local_space_offset(ls
, type
);
1237 ls
->div
= isl_mat_drop_cols(ls
->div
, first
, n
);
1239 return isl_local_space_free(ls
);
1244 __isl_give isl_local_space
*isl_local_space_insert_dims(
1245 __isl_take isl_local_space
*ls
,
1246 enum isl_dim_type type
, unsigned first
, unsigned n
)
1250 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
1253 if (isl_local_space_check_range(ls
, type
, first
, 0) < 0)
1254 return isl_local_space_free(ls
);
1256 ls
= isl_local_space_cow(ls
);
1260 if (type
== isl_dim_div
) {
1261 ls
->div
= isl_mat_insert_zero_rows(ls
->div
, first
, n
);
1263 ls
->dim
= isl_space_insert_dims(ls
->dim
, type
, first
, n
);
1265 return isl_local_space_free(ls
);
1268 first
+= 1 + isl_local_space_offset(ls
, type
);
1269 ls
->div
= isl_mat_insert_zero_cols(ls
->div
, first
, n
);
1271 return isl_local_space_free(ls
);
1276 /* Does the linear part of "constraint" correspond to
1277 * integer division "div" in "ls"?
1279 * That is, given div = floor((c + f)/m), is the constraint of the form
1281 * f - m d + c' >= 0 [sign = 1]
1283 * -f + m d + c'' >= 0 [sign = -1]
1285 * If so, set *sign to the corresponding value.
1287 static isl_bool
is_linear_div_constraint(__isl_keep isl_local_space
*ls
,
1288 isl_int
*constraint
, unsigned div
, int *sign
)
1293 unknown
= isl_local_space_div_is_marked_unknown(ls
, div
);
1295 return isl_bool_error
;
1297 return isl_bool_false
;
1299 pos
= isl_local_space_offset(ls
, isl_dim_div
) + div
;
1301 if (isl_int_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
1303 if (!isl_seq_is_neg(constraint
+ 1,
1304 ls
->div
->row
[div
] + 2, pos
- 1))
1305 return isl_bool_false
;
1306 } else if (isl_int_abs_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
1308 if (!isl_seq_eq(constraint
+ 1, ls
->div
->row
[div
] + 2, pos
- 1))
1309 return isl_bool_false
;
1311 return isl_bool_false
;
1313 if (isl_seq_first_non_zero(constraint
+ pos
+ 1,
1314 ls
->div
->n_row
- div
- 1) != -1)
1315 return isl_bool_false
;
1316 return isl_bool_true
;
1319 /* Check if the constraints pointed to by "constraint" is a div
1320 * constraint corresponding to div "div" in "ls".
1322 * That is, if div = floor(f/m), then check if the constraint is
1326 * -(f-(m-1)) + m d >= 0
1328 * First check if the linear part is of the right form and
1329 * then check the constant term.
1331 isl_bool
isl_local_space_is_div_constraint(__isl_keep isl_local_space
*ls
,
1332 isl_int
*constraint
, unsigned div
)
1337 linear
= is_linear_div_constraint(ls
, constraint
, div
, &sign
);
1338 if (linear
< 0 || !linear
)
1343 isl_int_sub(ls
->div
->row
[div
][1],
1344 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
1345 isl_int_add_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
1346 neg
= isl_seq_is_neg(constraint
, ls
->div
->row
[div
] + 1, 1);
1347 isl_int_sub_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
1348 isl_int_add(ls
->div
->row
[div
][1],
1349 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
1351 return isl_bool_false
;
1353 if (!isl_int_eq(constraint
[0], ls
->div
->row
[div
][1]))
1354 return isl_bool_false
;
1357 return isl_bool_true
;
1360 /* Is the constraint pointed to by "constraint" one
1361 * of an equality that corresponds to integer division "div" in "ls"?
1363 * That is, given an integer division of the form
1365 * a = floor((f + c)/m)
1367 * is the equality of the form
1371 * Note that the constant term is not checked explicitly, but given
1372 * that this is a valid equality constraint, the constant c' necessarily
1373 * has a value close to -c.
1375 isl_bool
isl_local_space_is_div_equality(__isl_keep isl_local_space
*ls
,
1376 isl_int
*constraint
, unsigned div
)
1381 linear
= is_linear_div_constraint(ls
, constraint
, div
, &sign
);
1382 if (linear
< 0 || !linear
)
1385 return isl_bool_ok(sign
< 0);
1389 * Set active[i] to 1 if the dimension at position i is involved
1390 * in the linear expression l.
1392 int *isl_local_space_get_active(__isl_keep isl_local_space
*ls
, isl_int
*l
)
1400 ctx
= isl_local_space_get_ctx(ls
);
1401 total
= isl_local_space_dim(ls
, isl_dim_all
);
1404 active
= isl_calloc_array(ctx
, int, total
);
1405 if (total
&& !active
)
1408 for (i
= 0; i
< total
; ++i
)
1409 active
[i
] = !isl_int_is_zero(l
[i
]);
1411 offset
= isl_local_space_offset(ls
, isl_dim_div
) - 1;
1412 for (i
= ls
->div
->n_row
- 1; i
>= 0; --i
) {
1413 if (!active
[offset
+ i
])
1415 for (j
= 0; j
< total
; ++j
)
1416 active
[j
] |= !isl_int_is_zero(ls
->div
->row
[i
][2 + j
]);
1422 /* Given a local space "ls" of a set, create a local space
1423 * for the lift of the set. In particular, the result
1424 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1425 * range of the wrapped map.
1427 __isl_give isl_local_space
*isl_local_space_lift(
1428 __isl_take isl_local_space
*ls
)
1430 ls
= isl_local_space_cow(ls
);
1434 ls
->dim
= isl_space_lift(ls
->dim
, ls
->div
->n_row
);
1435 ls
->div
= isl_mat_drop_rows(ls
->div
, 0, ls
->div
->n_row
);
1436 if (!ls
->dim
|| !ls
->div
)
1437 return isl_local_space_free(ls
);
1442 /* Construct a basic map that maps a set living in local space "ls"
1443 * to the corresponding lifted local space.
1445 __isl_give isl_basic_map
*isl_local_space_lifting(
1446 __isl_take isl_local_space
*ls
)
1448 isl_basic_map
*lifting
;
1449 isl_basic_set
*bset
;
1453 if (!isl_local_space_is_set(ls
))
1454 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1455 "lifting only defined on set spaces", goto error
);
1457 bset
= isl_basic_set_from_local_space(ls
);
1458 lifting
= isl_basic_set_unwrap(isl_basic_set_lift(bset
));
1459 lifting
= isl_basic_map_domain_map(lifting
);
1460 lifting
= isl_basic_map_reverse(lifting
);
1464 isl_local_space_free(ls
);
1468 /* Compute the preimage of "ls" under the function represented by "ma".
1469 * In other words, plug in "ma" in "ls". The result is a local space
1470 * that is part of the domain space of "ma".
1472 * If the divs in "ls" are represented as
1474 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1476 * and ma is represented by
1478 * x = D(p) + F(y) + G(divs')
1480 * then the resulting divs are
1482 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1484 * We first copy over the divs from "ma" and then
1485 * we add the modified divs from "ls".
1487 __isl_give isl_local_space
*isl_local_space_preimage_multi_aff(
1488 __isl_take isl_local_space
*ls
, __isl_take isl_multi_aff
*ma
)
1492 isl_local_space
*res
= NULL
;
1493 isl_size n_div_ls
, n_div_ma
;
1494 isl_int f
, c1
, c2
, g
;
1496 ma
= isl_multi_aff_align_divs(ma
);
1499 if (!isl_space_is_range_internal(ls
->dim
, ma
->space
))
1500 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1501 "spaces don't match", goto error
);
1503 n_div_ls
= isl_local_space_dim(ls
, isl_dim_div
);
1504 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
1505 if (n_div_ls
< 0 || n_div_ma
< 0)
1508 space
= isl_space_domain(isl_multi_aff_get_space(ma
));
1509 res
= isl_local_space_alloc(space
, n_div_ma
+ n_div_ls
);
1514 isl_mat_free(res
->div
);
1515 res
->div
= isl_mat_copy(ma
->u
.p
[0]->ls
->div
);
1516 res
->div
= isl_mat_add_zero_cols(res
->div
, n_div_ls
);
1517 res
->div
= isl_mat_add_rows(res
->div
, n_div_ls
);
1527 for (i
= 0; i
< ls
->div
->n_row
; ++i
) {
1528 if (isl_int_is_zero(ls
->div
->row
[i
][0])) {
1529 isl_int_set_si(res
->div
->row
[n_div_ma
+ i
][0], 0);
1532 if (isl_seq_preimage(res
->div
->row
[n_div_ma
+ i
],
1534 ma
, 0, 0, n_div_ma
, n_div_ls
, f
, c1
, c2
, g
, 1) < 0)
1535 res
= isl_local_space_free(res
);
1536 res
= normalize_div(res
, n_div_ma
+ i
);
1546 isl_local_space_free(ls
);
1547 isl_multi_aff_free(ma
);
1550 isl_local_space_free(ls
);
1551 isl_multi_aff_free(ma
);
1552 isl_local_space_free(res
);
1556 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1557 * to dimensions of "dst_type" at "dst_pos".
1559 * Moving to/from local dimensions is not allowed.
1560 * We currently assume that the dimension type changes.
1562 __isl_give isl_local_space
*isl_local_space_move_dims(
1563 __isl_take isl_local_space
*ls
,
1564 enum isl_dim_type dst_type
, unsigned dst_pos
,
1565 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1573 !isl_local_space_is_named_or_nested(ls
, src_type
) &&
1574 !isl_local_space_is_named_or_nested(ls
, dst_type
))
1577 if (isl_local_space_check_range(ls
, src_type
, src_pos
, n
) < 0)
1578 return isl_local_space_free(ls
);
1579 if (isl_local_space_check_range(ls
, dst_type
, dst_pos
, 0) < 0)
1580 return isl_local_space_free(ls
);
1581 if (src_type
== isl_dim_div
)
1582 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1583 "cannot move divs", return isl_local_space_free(ls
));
1584 if (dst_type
== isl_dim_div
)
1585 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1586 "cannot move to divs", return isl_local_space_free(ls
));
1587 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1589 if (dst_type
== src_type
)
1590 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
1591 "moving dims within the same type not supported",
1592 return isl_local_space_free(ls
));
1594 ls
= isl_local_space_cow(ls
);
1598 g_src_pos
= 1 + isl_local_space_offset(ls
, src_type
) + src_pos
;
1599 g_dst_pos
= 1 + isl_local_space_offset(ls
, dst_type
) + dst_pos
;
1600 if (dst_type
> src_type
)
1602 ls
->div
= isl_mat_move_cols(ls
->div
, g_dst_pos
, g_src_pos
, n
);
1604 return isl_local_space_free(ls
);
1605 ls
->dim
= isl_space_move_dims(ls
->dim
, dst_type
, dst_pos
,
1606 src_type
, src_pos
, n
);
1608 return isl_local_space_free(ls
);
1613 /* Remove any internal structure of the domain of "ls".
1614 * If there is any such internal structure in the input,
1615 * then the name of the corresponding space is also removed.
1617 __isl_give isl_local_space
*isl_local_space_flatten_domain(
1618 __isl_take isl_local_space
*ls
)
1623 if (!ls
->dim
->nested
[0])
1626 ls
= isl_local_space_cow(ls
);
1630 ls
->dim
= isl_space_flatten_domain(ls
->dim
);
1632 return isl_local_space_free(ls
);
1637 /* Remove any internal structure of the range of "ls".
1638 * If there is any such internal structure in the input,
1639 * then the name of the corresponding space is also removed.
1641 __isl_give isl_local_space
*isl_local_space_flatten_range(
1642 __isl_take isl_local_space
*ls
)
1647 if (!ls
->dim
->nested
[1])
1650 ls
= isl_local_space_cow(ls
);
1654 ls
->dim
= isl_space_flatten_range(ls
->dim
);
1656 return isl_local_space_free(ls
);
1661 /* Given the local space "ls" of a map, return the local space of a set
1662 * that lives in a space that wraps the space of "ls" and that has
1665 __isl_give isl_local_space
*isl_local_space_wrap(__isl_take isl_local_space
*ls
)
1667 ls
= isl_local_space_cow(ls
);
1671 ls
->dim
= isl_space_wrap(ls
->dim
);
1673 return isl_local_space_free(ls
);
1678 /* Lift the point "pnt", living in the (set) space of "ls"
1679 * to live in a space with extra coordinates corresponding
1680 * to the local variables of "ls".
1682 __isl_give isl_point
*isl_local_space_lift_point(__isl_take isl_local_space
*ls
,
1683 __isl_take isl_point
*pnt
)
1690 if (isl_local_space_check_has_space(ls
, isl_point_peek_space(pnt
)) < 0)
1693 local
= isl_local_space_peek_local(ls
);
1694 n_local
= isl_local_space_dim(ls
, isl_dim_div
);
1698 space
= isl_point_take_space(pnt
);
1699 vec
= isl_point_take_vec(pnt
);
1701 space
= isl_space_lift(space
, n_local
);
1702 vec
= isl_local_extend_point_vec(local
, vec
);
1704 pnt
= isl_point_restore_vec(pnt
, vec
);
1705 pnt
= isl_point_restore_space(pnt
, space
);
1707 isl_local_space_free(ls
);
1711 isl_local_space_free(ls
);
1712 isl_point_free(pnt
);