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(ls
->dim
);
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(__isl_take isl_space
*dim
)
95 return isl_local_space_alloc(dim
, 0);
98 __isl_give isl_local_space
*isl_local_space_copy(__isl_keep isl_local_space
*ls
)
107 __isl_give isl_local_space
*isl_local_space_dup(__isl_keep isl_local_space
*ls
)
112 return isl_local_space_alloc_div(isl_space_copy(ls
->dim
),
113 isl_mat_copy(ls
->div
));
117 __isl_give isl_local_space
*isl_local_space_cow(__isl_take isl_local_space
*ls
)
125 return isl_local_space_dup(ls
);
128 __isl_null isl_local_space
*isl_local_space_free(
129 __isl_take isl_local_space
*ls
)
137 isl_space_free(ls
->dim
);
138 isl_mat_free(ls
->div
);
145 /* Is the local space that of a parameter domain?
147 isl_bool
isl_local_space_is_params(__isl_keep isl_local_space
*ls
)
150 return isl_bool_error
;
151 return isl_space_is_params(ls
->dim
);
154 /* Is the local space that of a set?
156 isl_bool
isl_local_space_is_set(__isl_keep isl_local_space
*ls
)
158 return ls
? isl_space_is_set(ls
->dim
) : isl_bool_error
;
161 /* Do "ls1" and "ls2" have the same space?
163 isl_bool
isl_local_space_has_equal_space(__isl_keep isl_local_space
*ls1
,
164 __isl_keep isl_local_space
*ls2
)
167 return isl_bool_error
;
169 return isl_space_is_equal(ls1
->dim
, ls2
->dim
);
172 /* Is the space of "ls" equal to "space"?
174 isl_bool
isl_local_space_has_space(__isl_keep isl_local_space
*ls
,
175 __isl_keep isl_space
*space
)
177 return isl_space_is_equal(isl_local_space_peek_space(ls
), space
);
180 /* Check that the space of "ls" is equal to "space".
182 static isl_stat
isl_local_space_check_has_space(__isl_keep isl_local_space
*ls
,
183 __isl_keep isl_space
*space
)
187 ok
= isl_local_space_has_space(ls
, space
);
189 return isl_stat_error
;
191 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
192 "spaces don't match", return isl_stat_error
);
196 /* Return true if the two local spaces are identical, with identical
197 * expressions for the integer divisions.
199 isl_bool
isl_local_space_is_equal(__isl_keep isl_local_space
*ls1
,
200 __isl_keep isl_local_space
*ls2
)
204 equal
= isl_local_space_has_equal_space(ls1
, ls2
);
205 if (equal
< 0 || !equal
)
208 if (!isl_local_space_divs_known(ls1
))
209 return isl_bool_false
;
210 if (!isl_local_space_divs_known(ls2
))
211 return isl_bool_false
;
213 return isl_mat_is_equal(ls1
->div
, ls2
->div
);
216 /* Compare two isl_local_spaces.
218 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
219 * than "ls2" and 0 if they are equal.
221 int isl_local_space_cmp(__isl_keep isl_local_space
*ls1
,
222 __isl_keep isl_local_space
*ls2
)
233 cmp
= isl_space_cmp(ls1
->dim
, ls2
->dim
);
237 return isl_local_cmp(ls1
->div
, ls2
->div
);
240 isl_size
isl_local_space_dim(__isl_keep isl_local_space
*ls
,
241 enum isl_dim_type type
)
244 return isl_size_error
;
245 if (type
== isl_dim_div
)
246 return ls
->div
->n_row
;
247 if (type
== isl_dim_all
) {
248 isl_size dim
= isl_space_dim(ls
->dim
, isl_dim_all
);
250 return isl_size_error
;
251 return dim
+ ls
->div
->n_row
;
253 return isl_space_dim(ls
->dim
, type
);
257 #define TYPE isl_local_space
258 #include "check_type_range_templ.c"
260 unsigned isl_local_space_offset(__isl_keep isl_local_space
*ls
,
261 enum isl_dim_type type
)
270 case isl_dim_cst
: return 0;
271 case isl_dim_param
: return 1;
272 case isl_dim_in
: return 1 + space
->nparam
;
273 case isl_dim_out
: return 1 + space
->nparam
+ space
->n_in
;
275 return 1 + space
->nparam
+ space
->n_in
+ space
->n_out
;
280 /* Return the position of the dimension of the given type and name
282 * Return -1 if no such dimension can be found.
284 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space
*ls
,
285 enum isl_dim_type type
, const char *name
)
289 if (type
== isl_dim_div
)
291 return isl_space_find_dim_by_name(ls
->dim
, type
, name
);
294 /* Does the given dimension have a name?
296 isl_bool
isl_local_space_has_dim_name(__isl_keep isl_local_space
*ls
,
297 enum isl_dim_type type
, unsigned pos
)
299 return ls
? isl_space_has_dim_name(ls
->dim
, type
, pos
) : isl_bool_error
;
302 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space
*ls
,
303 enum isl_dim_type type
, unsigned pos
)
305 return ls
? isl_space_get_dim_name(ls
->dim
, type
, pos
) : NULL
;
308 isl_bool
isl_local_space_has_dim_id(__isl_keep isl_local_space
*ls
,
309 enum isl_dim_type type
, unsigned pos
)
311 return ls
? isl_space_has_dim_id(ls
->dim
, type
, pos
) : isl_bool_error
;
314 __isl_give isl_id
*isl_local_space_get_dim_id(__isl_keep isl_local_space
*ls
,
315 enum isl_dim_type type
, unsigned pos
)
317 return ls
? isl_space_get_dim_id(ls
->dim
, type
, pos
) : NULL
;
320 /* Return the argument of the integer division at position "pos" in "ls".
321 * All local variables in "ls" are known to have a (complete) explicit
324 static __isl_give isl_aff
*extract_div(__isl_keep isl_local_space
*ls
, int pos
)
328 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
331 isl_seq_cpy(aff
->v
->el
, ls
->div
->row
[pos
], aff
->v
->size
);
335 /* Return the argument of the integer division at position "pos" in "ls".
336 * The integer division at that position is known to have a complete
337 * explicit representation, but some of the others do not.
338 * Remove them first because the domain of an isl_aff
339 * is not allowed to have unknown local variables.
341 static __isl_give isl_aff
*drop_unknown_divs_and_extract_div(
342 __isl_keep isl_local_space
*ls
, int pos
)
349 n
= isl_local_space_dim(ls
, isl_dim_div
);
352 ls
= isl_local_space_copy(ls
);
353 for (i
= n
- 1; i
>= 0; --i
) {
354 unknown
= isl_local_space_div_is_marked_unknown(ls
, i
);
356 ls
= isl_local_space_free(ls
);
359 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
, i
, 1);
363 aff
= extract_div(ls
, pos
);
364 isl_local_space_free(ls
);
368 /* Return the argument of the integer division at position "pos" in "ls".
369 * The integer division is assumed to have a complete explicit
370 * representation. If some of the other integer divisions
371 * do not have an explicit representation, then they need
372 * to be removed first because the domain of an isl_aff
373 * is not allowed to have unknown local variables.
375 __isl_give isl_aff
*isl_local_space_get_div(__isl_keep isl_local_space
*ls
,
383 if (pos
< 0 || pos
>= ls
->div
->n_row
)
384 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
385 "index out of bounds", return NULL
);
387 known
= isl_local_space_div_is_known(ls
, pos
);
391 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
392 "expression of div unknown", return NULL
);
393 if (!isl_local_space_is_set(ls
))
394 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
395 "cannot represent divs of map spaces", return NULL
);
397 known
= isl_local_space_divs_known(ls
);
401 return extract_div(ls
, pos
);
403 return drop_unknown_divs_and_extract_div(ls
, pos
);
406 /* Return the space of "ls".
408 __isl_keep isl_space
*isl_local_space_peek_space(__isl_keep isl_local_space
*ls
)
416 __isl_give isl_space
*isl_local_space_get_space(__isl_keep isl_local_space
*ls
)
418 return isl_space_copy(isl_local_space_peek_space(ls
));
421 /* Return the space of "ls".
422 * This may be either a copy or the space itself
423 * if there is only one reference to "ls".
424 * This allows the space to be modified inplace
425 * if both the local space and its space have only a single reference.
426 * The caller is not allowed to modify "ls" between this call and
427 * a subsequent call to isl_local_space_restore_space.
428 * The only exception is that isl_local_space_free can be called instead.
430 __isl_give isl_space
*isl_local_space_take_space(__isl_keep isl_local_space
*ls
)
437 return isl_local_space_get_space(ls
);
443 /* Set the space of "ls" to "space", where the space of "ls" may be missing
444 * due to a preceding call to isl_local_space_take_space.
445 * However, in this case, "ls" only has a single reference and
446 * then the call to isl_local_space_cow has no effect.
448 __isl_give isl_local_space
*isl_local_space_restore_space(
449 __isl_take isl_local_space
*ls
, __isl_take isl_space
*space
)
454 if (ls
->dim
== space
) {
455 isl_space_free(space
);
459 ls
= isl_local_space_cow(ls
);
462 isl_space_free(ls
->dim
);
467 isl_local_space_free(ls
);
468 isl_space_free(space
);
472 /* Return the local variables of "ls".
474 __isl_keep isl_local
*isl_local_space_peek_local(__isl_keep isl_local_space
*ls
)
476 return ls
? ls
->div
: NULL
;
479 /* Replace the identifier of the tuple of type "type" by "id".
481 __isl_give isl_local_space
*isl_local_space_set_tuple_id(
482 __isl_take isl_local_space
*ls
,
483 enum isl_dim_type type
, __isl_take isl_id
*id
)
485 ls
= isl_local_space_cow(ls
);
488 ls
->dim
= isl_space_set_tuple_id(ls
->dim
, type
, id
);
490 return isl_local_space_free(ls
);
497 __isl_give isl_local_space
*isl_local_space_set_dim_name(
498 __isl_take isl_local_space
*ls
,
499 enum isl_dim_type type
, unsigned pos
, const char *s
)
501 ls
= isl_local_space_cow(ls
);
504 ls
->dim
= isl_space_set_dim_name(ls
->dim
, type
, pos
, s
);
506 return isl_local_space_free(ls
);
511 __isl_give isl_local_space
*isl_local_space_set_dim_id(
512 __isl_take isl_local_space
*ls
,
513 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
515 ls
= isl_local_space_cow(ls
);
518 ls
->dim
= isl_space_set_dim_id(ls
->dim
, type
, pos
, id
);
520 return isl_local_space_free(ls
);
528 /* Construct a zero-dimensional local space with the given parameter domain.
530 __isl_give isl_local_space
*isl_local_space_set_from_params(
531 __isl_take isl_local_space
*ls
)
535 space
= isl_local_space_take_space(ls
);
536 space
= isl_space_set_from_params(space
);
537 ls
= isl_local_space_restore_space(ls
, space
);
542 __isl_give isl_local_space
*isl_local_space_reset_space(
543 __isl_take isl_local_space
*ls
, __isl_take isl_space
*dim
)
545 ls
= isl_local_space_cow(ls
);
549 isl_space_free(ls
->dim
);
554 isl_local_space_free(ls
);
559 /* Reorder the dimensions of "ls" according to the given reordering.
560 * The reordering r is assumed to have been extended with the local
561 * variables, leaving them in the same order.
563 __isl_give isl_local_space
*isl_local_space_realign(
564 __isl_take isl_local_space
*ls
, __isl_take isl_reordering
*r
)
566 ls
= isl_local_space_cow(ls
);
570 ls
->div
= isl_local_reorder(ls
->div
, isl_reordering_copy(r
));
574 ls
= isl_local_space_reset_space(ls
, isl_reordering_get_space(r
));
576 isl_reordering_free(r
);
579 isl_local_space_free(ls
);
580 isl_reordering_free(r
);
584 __isl_give isl_local_space
*isl_local_space_add_div(
585 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*div
)
587 ls
= isl_local_space_cow(ls
);
591 if (ls
->div
->n_col
!= div
->size
)
592 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
593 "incompatible dimensions", goto error
);
595 ls
->div
= isl_mat_add_zero_cols(ls
->div
, 1);
596 ls
->div
= isl_mat_add_rows(ls
->div
, 1);
600 isl_seq_cpy(ls
->div
->row
[ls
->div
->n_row
- 1], div
->el
, div
->size
);
601 isl_int_set_si(ls
->div
->row
[ls
->div
->n_row
- 1][div
->size
], 0);
606 isl_local_space_free(ls
);
611 __isl_give isl_local_space
*isl_local_space_replace_divs(
612 __isl_take isl_local_space
*ls
, __isl_take isl_mat
*div
)
614 ls
= isl_local_space_cow(ls
);
619 isl_mat_free(ls
->div
);
624 isl_local_space_free(ls
);
628 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
631 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
632 __isl_keep isl_mat
*src
, int s
, int *exp
)
635 unsigned c
= src
->n_col
- src
->n_row
;
637 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
638 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
640 for (i
= 0; i
< s
; ++i
)
641 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
644 /* Compare (known) divs.
645 * Return non-zero if at least one of the two divs is unknown.
646 * In particular, if both divs are unknown, we respect their
647 * current order. Otherwise, we sort the known div after the unknown
648 * div only if the known div depends on the unknown div.
650 static int cmp_row(isl_int
*row_i
, isl_int
*row_j
, int i
, int j
,
651 unsigned n_row
, unsigned n_col
)
654 int unknown_i
, unknown_j
;
656 unknown_i
= isl_int_is_zero(row_i
[0]);
657 unknown_j
= isl_int_is_zero(row_j
[0]);
659 if (unknown_i
&& unknown_j
)
663 li
= n_col
- n_row
+ i
;
665 li
= isl_seq_last_non_zero(row_i
, n_col
);
667 lj
= n_col
- n_row
+ j
;
669 lj
= isl_seq_last_non_zero(row_j
, n_col
);
674 return isl_seq_cmp(row_i
, row_j
, n_col
);
677 /* Call cmp_row for divs in a matrix.
679 int isl_mat_cmp_div(__isl_keep isl_mat
*div
, int i
, int j
)
681 return cmp_row(div
->row
[i
], div
->row
[j
], i
, j
, div
->n_row
, div
->n_col
);
684 /* Call cmp_row for divs in a basic map.
686 static int bmap_cmp_row(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
689 return cmp_row(bmap
->div
[i
], bmap
->div
[j
], i
, j
, bmap
->n_div
, total
);
692 /* Sort the divs in "bmap".
694 * We first make sure divs are placed after divs on which they depend.
695 * Then we perform a simple insertion sort based on the same ordering
696 * that is used in isl_merge_divs.
698 __isl_give isl_basic_map
*isl_basic_map_sort_divs(
699 __isl_take isl_basic_map
*bmap
)
704 bmap
= isl_basic_map_order_divs(bmap
);
707 if (bmap
->n_div
<= 1)
710 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
712 return isl_basic_map_free(bmap
);
713 for (i
= 1; i
< bmap
->n_div
; ++i
) {
714 for (j
= i
- 1; j
>= 0; --j
) {
715 if (bmap_cmp_row(bmap
, j
, j
+ 1, 2 + total
) <= 0)
717 bmap
= isl_basic_map_swap_div(bmap
, j
, j
+ 1);
726 /* Sort the divs in the basic maps of "map".
728 __isl_give isl_map
*isl_map_sort_divs(__isl_take isl_map
*map
)
730 return isl_map_inline_foreach_basic_map(map
, &isl_basic_map_sort_divs
);
733 /* Combine the two lists of divs into a single list.
734 * For each row i in div1, exp1[i] is set to the position of the corresponding
735 * row in the result. Similarly for div2 and exp2.
736 * This function guarantees
738 * exp1[i+1] > exp1[i]
739 * For optimal merging, the two input list should have been sorted.
741 __isl_give isl_mat
*isl_merge_divs(__isl_keep isl_mat
*div1
,
742 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
751 d
= div1
->n_col
- div1
->n_row
;
752 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
753 d
+ div1
->n_row
+ div2
->n_row
);
757 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
760 expand_row(div
, k
, div1
, i
, exp1
);
761 expand_row(div
, k
+ 1, div2
, j
, exp2
);
763 cmp
= isl_mat_cmp_div(div
, k
, k
+ 1);
767 } else if (cmp
< 0) {
771 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
774 for (; i
< div1
->n_row
; ++i
, ++k
) {
775 expand_row(div
, k
, div1
, i
, exp1
);
778 for (; j
< div2
->n_row
; ++j
, ++k
) {
779 expand_row(div
, k
, div2
, j
, exp2
);
789 /* Swap divs "a" and "b" in "ls".
791 __isl_give isl_local_space
*isl_local_space_swap_div(
792 __isl_take isl_local_space
*ls
, int a
, int b
)
796 ls
= isl_local_space_cow(ls
);
799 if (a
< 0 || a
>= ls
->div
->n_row
|| b
< 0 || b
>= ls
->div
->n_row
)
800 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
801 "index out of bounds", return isl_local_space_free(ls
));
802 offset
= ls
->div
->n_col
- ls
->div
->n_row
;
803 ls
->div
= isl_mat_swap_cols(ls
->div
, offset
+ a
, offset
+ b
);
804 ls
->div
= isl_mat_swap_rows(ls
->div
, a
, b
);
806 return isl_local_space_free(ls
);
810 /* Construct a local space that contains all the divs in either
813 __isl_give isl_local_space
*isl_local_space_intersect(
814 __isl_take isl_local_space
*ls1
, __isl_take isl_local_space
*ls2
)
825 ctx
= isl_local_space_get_ctx(ls1
);
826 if (!isl_space_is_equal(ls1
->dim
, ls2
->dim
))
827 isl_die(ctx
, isl_error_invalid
,
828 "spaces should be identical", goto error
);
830 if (ls2
->div
->n_row
== 0) {
831 isl_local_space_free(ls2
);
835 if (ls1
->div
->n_row
== 0) {
836 isl_local_space_free(ls1
);
840 exp1
= isl_alloc_array(ctx
, int, ls1
->div
->n_row
);
841 exp2
= isl_alloc_array(ctx
, int, ls2
->div
->n_row
);
845 div
= isl_merge_divs(ls1
->div
, ls2
->div
, exp1
, exp2
);
849 equal
= isl_mat_is_equal(ls1
->div
, div
);
853 ls1
= isl_local_space_cow(ls1
);
859 isl_local_space_free(ls2
);
860 isl_mat_free(ls1
->div
);
868 isl_local_space_free(ls1
);
869 isl_local_space_free(ls2
);
873 /* Is the local variable "div" of "ls" marked as not having
874 * an explicit representation?
875 * Note that even if this variable is not marked in this way and therefore
876 * does have an explicit representation, this representation may still
877 * depend (indirectly) on other local variables that do not
878 * have an explicit representation.
880 isl_bool
isl_local_space_div_is_marked_unknown(__isl_keep isl_local_space
*ls
,
884 return isl_bool_error
;
885 return isl_local_div_is_marked_unknown(ls
->div
, div
);
888 /* Does "ls" have a complete explicit representation for div "div"?
890 isl_bool
isl_local_space_div_is_known(__isl_keep isl_local_space
*ls
, int div
)
893 return isl_bool_error
;
894 return isl_local_div_is_known(ls
->div
, div
);
897 /* Does "ls" have an explicit representation for all local variables?
899 isl_bool
isl_local_space_divs_known(__isl_keep isl_local_space
*ls
)
902 return isl_bool_error
;
903 return isl_local_divs_known(ls
->div
);
906 __isl_give isl_local_space
*isl_local_space_domain(
907 __isl_take isl_local_space
*ls
)
911 n_out
= isl_local_space_dim(ls
, isl_dim_out
);
913 return isl_local_space_free(ls
);
914 ls
= isl_local_space_drop_dims(ls
, isl_dim_out
, 0, n_out
);
915 ls
= isl_local_space_cow(ls
);
918 ls
->dim
= isl_space_domain(ls
->dim
);
920 return isl_local_space_free(ls
);
924 __isl_give isl_local_space
*isl_local_space_range(
925 __isl_take isl_local_space
*ls
)
929 n_in
= isl_local_space_dim(ls
, isl_dim_in
);
931 return isl_local_space_free(ls
);
932 ls
= isl_local_space_drop_dims(ls
, isl_dim_in
, 0, n_in
);
933 ls
= isl_local_space_cow(ls
);
937 ls
->dim
= isl_space_range(ls
->dim
);
939 return isl_local_space_free(ls
);
943 /* Construct a local space for a map that has the given local
944 * space as domain and that has a zero-dimensional range.
946 __isl_give isl_local_space
*isl_local_space_from_domain(
947 __isl_take isl_local_space
*ls
)
949 ls
= isl_local_space_cow(ls
);
952 ls
->dim
= isl_space_from_domain(ls
->dim
);
954 return isl_local_space_free(ls
);
958 __isl_give isl_local_space
*isl_local_space_add_dims(
959 __isl_take isl_local_space
*ls
, enum isl_dim_type type
, unsigned n
)
963 pos
= isl_local_space_dim(ls
, type
);
965 return isl_local_space_free(ls
);
966 return isl_local_space_insert_dims(ls
, type
, pos
, n
);
969 /* Lift the basic set "bset", living in the space of "ls"
970 * to live in a space with extra coordinates corresponding
971 * to the local variables of "ls".
973 __isl_give isl_basic_set
*isl_local_space_lift_basic_set(
974 __isl_take isl_local_space
*ls
, __isl_take isl_basic_set
*bset
)
978 isl_basic_set
*ls_bset
;
980 n_local
= isl_local_space_dim(ls
, isl_dim_div
);
981 space
= isl_basic_set_peek_space(bset
);
983 isl_local_space_check_has_space(ls
, space
) < 0)
987 isl_local_space_free(ls
);
991 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, n_local
);
992 ls_bset
= isl_basic_set_from_local_space(ls
);
993 ls_bset
= isl_basic_set_lift(ls_bset
);
994 ls_bset
= isl_basic_set_flatten(ls_bset
);
995 bset
= isl_basic_set_intersect(bset
, ls_bset
);
999 isl_local_space_free(ls
);
1000 isl_basic_set_free(bset
);
1004 /* Lift the set "set", living in the space of "ls"
1005 * to live in a space with extra coordinates corresponding
1006 * to the local variables of "ls".
1008 __isl_give isl_set
*isl_local_space_lift_set(__isl_take isl_local_space
*ls
,
1009 __isl_take isl_set
*set
)
1012 isl_basic_set
*bset
;
1014 n_local
= isl_local_space_dim(ls
, isl_dim_div
);
1016 isl_local_space_check_has_space(ls
, isl_set_peek_space(set
)) < 0)
1020 isl_local_space_free(ls
);
1024 set
= isl_set_add_dims(set
, isl_dim_set
, n_local
);
1025 bset
= isl_basic_set_from_local_space(ls
);
1026 bset
= isl_basic_set_lift(bset
);
1027 bset
= isl_basic_set_flatten(bset
);
1028 set
= isl_set_intersect(set
, isl_set_from_basic_set(bset
));
1032 isl_local_space_free(ls
);
1037 /* Remove common factor of non-constant terms and denominator.
1039 static __isl_give isl_local_space
*normalize_div(
1040 __isl_take isl_local_space
*ls
, int div
)
1042 isl_ctx
*ctx
= ls
->div
->ctx
;
1043 unsigned total
= ls
->div
->n_col
- 2;
1045 isl_seq_gcd(ls
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
1046 isl_int_gcd(ctx
->normalize_gcd
,
1047 ctx
->normalize_gcd
, ls
->div
->row
[div
][0]);
1048 if (isl_int_is_one(ctx
->normalize_gcd
))
1051 isl_seq_scale_down(ls
->div
->row
[div
] + 2, ls
->div
->row
[div
] + 2,
1052 ctx
->normalize_gcd
, total
);
1053 isl_int_divexact(ls
->div
->row
[div
][0], ls
->div
->row
[div
][0],
1054 ctx
->normalize_gcd
);
1055 isl_int_fdiv_q(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1],
1056 ctx
->normalize_gcd
);
1061 /* Exploit the equalities in "eq" to simplify the expressions of
1062 * the integer divisions in "ls".
1063 * The integer divisions in "ls" are assumed to appear as regular
1064 * dimensions in "eq".
1066 __isl_give isl_local_space
*isl_local_space_substitute_equalities(
1067 __isl_take isl_local_space
*ls
, __isl_take isl_basic_set
*eq
)
1070 isl_size total
, dim
;
1076 total
= isl_space_dim(eq
->dim
, isl_dim_all
);
1077 dim
= isl_local_space_dim(ls
, isl_dim_all
);
1078 if (dim
< 0 || total
< 0)
1081 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1082 "spaces don't match", goto error
);
1085 for (i
= 0; i
< eq
->n_eq
; ++i
) {
1086 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
1087 if (j
< 0 || j
== 0 || j
>= total
)
1090 for (k
= 0; k
< ls
->div
->n_row
; ++k
) {
1091 if (isl_int_is_zero(ls
->div
->row
[k
][1 + j
]))
1093 ls
= isl_local_space_cow(ls
);
1096 ls
->div
= isl_mat_cow(ls
->div
);
1099 isl_seq_elim(ls
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
1100 &ls
->div
->row
[k
][0]);
1101 ls
= normalize_div(ls
, k
);
1107 isl_basic_set_free(eq
);
1110 isl_basic_set_free(eq
);
1111 isl_local_space_free(ls
);
1115 /* Plug in the affine expressions "subs" of length "subs_len" (including
1116 * the denominator and the constant term) into the variable at position "pos"
1117 * of the "n" div expressions starting at "first".
1119 * Let i be the dimension to replace and let "subs" be of the form
1123 * Any integer division starting at "first" with a non-zero coefficient for i,
1125 * floor((a i + g)/m)
1129 * floor((a f + d g)/(m d))
1131 __isl_give isl_local_space
*isl_local_space_substitute_seq(
1132 __isl_take isl_local_space
*ls
,
1133 enum isl_dim_type type
, unsigned pos
, isl_int
*subs
, int subs_len
,
1141 ls
= isl_local_space_cow(ls
);
1144 ls
->div
= isl_mat_cow(ls
->div
);
1146 return isl_local_space_free(ls
);
1148 if (first
+ n
> ls
->div
->n_row
)
1149 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1150 "index out of bounds", return isl_local_space_free(ls
));
1152 pos
+= isl_local_space_offset(ls
, type
);
1155 for (i
= first
; i
< first
+ n
; ++i
) {
1156 if (isl_int_is_zero(ls
->div
->row
[i
][1 + pos
]))
1158 isl_seq_substitute(ls
->div
->row
[i
], pos
, subs
,
1159 ls
->div
->n_col
, subs_len
, v
);
1160 ls
= normalize_div(ls
, i
);
1169 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
1172 * Let i be the dimension to replace and let "subs" be of the form
1176 * Any integer division with a non-zero coefficient for i,
1178 * floor((a i + g)/m)
1182 * floor((a f + d g)/(m d))
1184 __isl_give isl_local_space
*isl_local_space_substitute(
1185 __isl_take isl_local_space
*ls
,
1186 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
1190 ls
= isl_local_space_cow(ls
);
1192 return isl_local_space_free(ls
);
1194 if (!isl_space_is_equal(ls
->dim
, subs
->ls
->dim
))
1195 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1196 "spaces don't match", return isl_local_space_free(ls
));
1197 n_div
= isl_local_space_dim(subs
->ls
, isl_dim_div
);
1199 return isl_local_space_free(ls
);
1201 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
1202 "cannot handle divs yet",
1203 return isl_local_space_free(ls
));
1205 return isl_local_space_substitute_seq(ls
, type
, pos
, subs
->v
->el
,
1206 subs
->v
->size
, 0, ls
->div
->n_row
);
1209 isl_bool
isl_local_space_is_named_or_nested(__isl_keep isl_local_space
*ls
,
1210 enum isl_dim_type type
)
1213 return isl_bool_error
;
1214 return isl_space_is_named_or_nested(ls
->dim
, type
);
1217 __isl_give isl_local_space
*isl_local_space_drop_dims(
1218 __isl_take isl_local_space
*ls
,
1219 enum isl_dim_type type
, unsigned first
, unsigned n
)
1223 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
1226 if (isl_local_space_check_range(ls
, type
, first
, n
) < 0)
1227 return isl_local_space_free(ls
);
1229 ls
= isl_local_space_cow(ls
);
1233 if (type
== isl_dim_div
) {
1234 ls
->div
= isl_mat_drop_rows(ls
->div
, first
, n
);
1236 ls
->dim
= isl_space_drop_dims(ls
->dim
, type
, first
, n
);
1238 return isl_local_space_free(ls
);
1241 first
+= 1 + isl_local_space_offset(ls
, type
);
1242 ls
->div
= isl_mat_drop_cols(ls
->div
, first
, n
);
1244 return isl_local_space_free(ls
);
1249 __isl_give isl_local_space
*isl_local_space_insert_dims(
1250 __isl_take isl_local_space
*ls
,
1251 enum isl_dim_type type
, unsigned first
, unsigned n
)
1255 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
1258 if (isl_local_space_check_range(ls
, type
, first
, 0) < 0)
1259 return isl_local_space_free(ls
);
1261 ls
= isl_local_space_cow(ls
);
1265 if (type
== isl_dim_div
) {
1266 ls
->div
= isl_mat_insert_zero_rows(ls
->div
, first
, n
);
1268 ls
->dim
= isl_space_insert_dims(ls
->dim
, type
, first
, n
);
1270 return isl_local_space_free(ls
);
1273 first
+= 1 + isl_local_space_offset(ls
, type
);
1274 ls
->div
= isl_mat_insert_zero_cols(ls
->div
, first
, n
);
1276 return isl_local_space_free(ls
);
1281 /* Does the linear part of "constraint" correspond to
1282 * integer division "div" in "ls"?
1284 * That is, given div = floor((c + f)/m), is the constraint of the form
1286 * f - m d + c' >= 0 [sign = 1]
1288 * -f + m d + c'' >= 0 [sign = -1]
1290 * If so, set *sign to the corresponding value.
1292 static isl_bool
is_linear_div_constraint(__isl_keep isl_local_space
*ls
,
1293 isl_int
*constraint
, unsigned div
, int *sign
)
1298 unknown
= isl_local_space_div_is_marked_unknown(ls
, div
);
1300 return isl_bool_error
;
1302 return isl_bool_false
;
1304 pos
= isl_local_space_offset(ls
, isl_dim_div
) + div
;
1306 if (isl_int_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
1308 if (!isl_seq_is_neg(constraint
+ 1,
1309 ls
->div
->row
[div
] + 2, pos
- 1))
1310 return isl_bool_false
;
1311 } else if (isl_int_abs_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
1313 if (!isl_seq_eq(constraint
+ 1, ls
->div
->row
[div
] + 2, pos
- 1))
1314 return isl_bool_false
;
1316 return isl_bool_false
;
1318 if (isl_seq_first_non_zero(constraint
+ pos
+ 1,
1319 ls
->div
->n_row
- div
- 1) != -1)
1320 return isl_bool_false
;
1321 return isl_bool_true
;
1324 /* Check if the constraints pointed to by "constraint" is a div
1325 * constraint corresponding to div "div" in "ls".
1327 * That is, if div = floor(f/m), then check if the constraint is
1331 * -(f-(m-1)) + m d >= 0
1333 * First check if the linear part is of the right form and
1334 * then check the constant term.
1336 isl_bool
isl_local_space_is_div_constraint(__isl_keep isl_local_space
*ls
,
1337 isl_int
*constraint
, unsigned div
)
1342 linear
= is_linear_div_constraint(ls
, constraint
, div
, &sign
);
1343 if (linear
< 0 || !linear
)
1348 isl_int_sub(ls
->div
->row
[div
][1],
1349 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
1350 isl_int_add_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
1351 neg
= isl_seq_is_neg(constraint
, ls
->div
->row
[div
] + 1, 1);
1352 isl_int_sub_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
1353 isl_int_add(ls
->div
->row
[div
][1],
1354 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
1356 return isl_bool_false
;
1358 if (!isl_int_eq(constraint
[0], ls
->div
->row
[div
][1]))
1359 return isl_bool_false
;
1362 return isl_bool_true
;
1365 /* Is the constraint pointed to by "constraint" one
1366 * of an equality that corresponds to integer division "div" in "ls"?
1368 * That is, given an integer division of the form
1370 * a = floor((f + c)/m)
1372 * is the equality of the form
1376 * Note that the constant term is not checked explicitly, but given
1377 * that this is a valid equality constraint, the constant c' necessarily
1378 * has a value close to -c.
1380 isl_bool
isl_local_space_is_div_equality(__isl_keep isl_local_space
*ls
,
1381 isl_int
*constraint
, unsigned div
)
1386 linear
= is_linear_div_constraint(ls
, constraint
, div
, &sign
);
1387 if (linear
< 0 || !linear
)
1390 return isl_bool_ok(sign
< 0);
1394 * Set active[i] to 1 if the dimension at position i is involved
1395 * in the linear expression l.
1397 int *isl_local_space_get_active(__isl_keep isl_local_space
*ls
, isl_int
*l
)
1405 ctx
= isl_local_space_get_ctx(ls
);
1406 total
= isl_local_space_dim(ls
, isl_dim_all
);
1409 active
= isl_calloc_array(ctx
, int, total
);
1410 if (total
&& !active
)
1413 for (i
= 0; i
< total
; ++i
)
1414 active
[i
] = !isl_int_is_zero(l
[i
]);
1416 offset
= isl_local_space_offset(ls
, isl_dim_div
) - 1;
1417 for (i
= ls
->div
->n_row
- 1; i
>= 0; --i
) {
1418 if (!active
[offset
+ i
])
1420 for (j
= 0; j
< total
; ++j
)
1421 active
[j
] |= !isl_int_is_zero(ls
->div
->row
[i
][2 + j
]);
1427 /* Given a local space "ls" of a set, create a local space
1428 * for the lift of the set. In particular, the result
1429 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1430 * range of the wrapped map.
1432 __isl_give isl_local_space
*isl_local_space_lift(
1433 __isl_take isl_local_space
*ls
)
1435 ls
= isl_local_space_cow(ls
);
1439 ls
->dim
= isl_space_lift(ls
->dim
, ls
->div
->n_row
);
1440 ls
->div
= isl_mat_drop_rows(ls
->div
, 0, ls
->div
->n_row
);
1441 if (!ls
->dim
|| !ls
->div
)
1442 return isl_local_space_free(ls
);
1447 /* Construct a basic map that maps a set living in local space "ls"
1448 * to the corresponding lifted local space.
1450 __isl_give isl_basic_map
*isl_local_space_lifting(
1451 __isl_take isl_local_space
*ls
)
1453 isl_basic_map
*lifting
;
1454 isl_basic_set
*bset
;
1458 if (!isl_local_space_is_set(ls
))
1459 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1460 "lifting only defined on set spaces", goto error
);
1462 bset
= isl_basic_set_from_local_space(ls
);
1463 lifting
= isl_basic_set_unwrap(isl_basic_set_lift(bset
));
1464 lifting
= isl_basic_map_domain_map(lifting
);
1465 lifting
= isl_basic_map_reverse(lifting
);
1469 isl_local_space_free(ls
);
1473 /* Compute the preimage of "ls" under the function represented by "ma".
1474 * In other words, plug in "ma" in "ls". The result is a local space
1475 * that is part of the domain space of "ma".
1477 * If the divs in "ls" are represented as
1479 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1481 * and ma is represented by
1483 * x = D(p) + F(y) + G(divs')
1485 * then the resulting divs are
1487 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1489 * We first copy over the divs from "ma" and then
1490 * we add the modified divs from "ls".
1492 __isl_give isl_local_space
*isl_local_space_preimage_multi_aff(
1493 __isl_take isl_local_space
*ls
, __isl_take isl_multi_aff
*ma
)
1497 isl_local_space
*res
= NULL
;
1498 isl_size n_div_ls
, n_div_ma
;
1499 isl_int f
, c1
, c2
, g
;
1501 ma
= isl_multi_aff_align_divs(ma
);
1504 if (!isl_space_is_range_internal(ls
->dim
, ma
->space
))
1505 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1506 "spaces don't match", goto error
);
1508 n_div_ls
= isl_local_space_dim(ls
, isl_dim_div
);
1509 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
1510 if (n_div_ls
< 0 || n_div_ma
< 0)
1513 space
= isl_space_domain(isl_multi_aff_get_space(ma
));
1514 res
= isl_local_space_alloc(space
, n_div_ma
+ n_div_ls
);
1519 isl_mat_free(res
->div
);
1520 res
->div
= isl_mat_copy(ma
->u
.p
[0]->ls
->div
);
1521 res
->div
= isl_mat_add_zero_cols(res
->div
, n_div_ls
);
1522 res
->div
= isl_mat_add_rows(res
->div
, n_div_ls
);
1532 for (i
= 0; i
< ls
->div
->n_row
; ++i
) {
1533 if (isl_int_is_zero(ls
->div
->row
[i
][0])) {
1534 isl_int_set_si(res
->div
->row
[n_div_ma
+ i
][0], 0);
1537 if (isl_seq_preimage(res
->div
->row
[n_div_ma
+ i
],
1539 ma
, 0, 0, n_div_ma
, n_div_ls
, f
, c1
, c2
, g
, 1) < 0)
1540 res
= isl_local_space_free(res
);
1541 res
= normalize_div(res
, n_div_ma
+ i
);
1551 isl_local_space_free(ls
);
1552 isl_multi_aff_free(ma
);
1555 isl_local_space_free(ls
);
1556 isl_multi_aff_free(ma
);
1557 isl_local_space_free(res
);
1561 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1562 * to dimensions of "dst_type" at "dst_pos".
1564 * Moving to/from local dimensions is not allowed.
1565 * We currently assume that the dimension type changes.
1567 __isl_give isl_local_space
*isl_local_space_move_dims(
1568 __isl_take isl_local_space
*ls
,
1569 enum isl_dim_type dst_type
, unsigned dst_pos
,
1570 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1578 !isl_local_space_is_named_or_nested(ls
, src_type
) &&
1579 !isl_local_space_is_named_or_nested(ls
, dst_type
))
1582 if (isl_local_space_check_range(ls
, src_type
, src_pos
, n
) < 0)
1583 return isl_local_space_free(ls
);
1584 if (isl_local_space_check_range(ls
, dst_type
, dst_pos
, 0) < 0)
1585 return isl_local_space_free(ls
);
1586 if (src_type
== isl_dim_div
)
1587 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1588 "cannot move divs", return isl_local_space_free(ls
));
1589 if (dst_type
== isl_dim_div
)
1590 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1591 "cannot move to divs", return isl_local_space_free(ls
));
1592 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1594 if (dst_type
== src_type
)
1595 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
1596 "moving dims within the same type not supported",
1597 return isl_local_space_free(ls
));
1599 ls
= isl_local_space_cow(ls
);
1603 g_src_pos
= 1 + isl_local_space_offset(ls
, src_type
) + src_pos
;
1604 g_dst_pos
= 1 + isl_local_space_offset(ls
, dst_type
) + dst_pos
;
1605 if (dst_type
> src_type
)
1607 ls
->div
= isl_mat_move_cols(ls
->div
, g_dst_pos
, g_src_pos
, n
);
1609 return isl_local_space_free(ls
);
1610 ls
->dim
= isl_space_move_dims(ls
->dim
, dst_type
, dst_pos
,
1611 src_type
, src_pos
, n
);
1613 return isl_local_space_free(ls
);
1618 /* Remove any internal structure of the domain of "ls".
1619 * If there is any such internal structure in the input,
1620 * then the name of the corresponding space is also removed.
1622 __isl_give isl_local_space
*isl_local_space_flatten_domain(
1623 __isl_take isl_local_space
*ls
)
1628 if (!ls
->dim
->nested
[0])
1631 ls
= isl_local_space_cow(ls
);
1635 ls
->dim
= isl_space_flatten_domain(ls
->dim
);
1637 return isl_local_space_free(ls
);
1642 /* Remove any internal structure of the range of "ls".
1643 * If there is any such internal structure in the input,
1644 * then the name of the corresponding space is also removed.
1646 __isl_give isl_local_space
*isl_local_space_flatten_range(
1647 __isl_take isl_local_space
*ls
)
1652 if (!ls
->dim
->nested
[1])
1655 ls
= isl_local_space_cow(ls
);
1659 ls
->dim
= isl_space_flatten_range(ls
->dim
);
1661 return isl_local_space_free(ls
);
1666 /* Given the local space "ls" of a map, return the local space of a set
1667 * that lives in a space that wraps the space of "ls" and that has
1670 __isl_give isl_local_space
*isl_local_space_wrap(__isl_take isl_local_space
*ls
)
1672 ls
= isl_local_space_cow(ls
);
1676 ls
->dim
= isl_space_wrap(ls
->dim
);
1678 return isl_local_space_free(ls
);
1683 /* Lift the point "pnt", living in the space of "ls"
1684 * to live in a space with extra coordinates corresponding
1685 * to the local variables of "ls".
1687 __isl_give isl_point
*isl_local_space_lift_point(__isl_take isl_local_space
*ls
,
1688 __isl_take isl_point
*pnt
)
1695 if (isl_local_space_check_has_space(ls
, isl_point_peek_space(pnt
)) < 0)
1698 local
= isl_local_space_peek_local(ls
);
1699 n_local
= isl_local_space_dim(ls
, isl_dim_div
);
1703 space
= isl_point_take_space(pnt
);
1704 vec
= isl_point_take_vec(pnt
);
1706 space
= isl_space_lift(space
, n_local
);
1707 vec
= isl_local_extend_point_vec(local
, vec
);
1709 pnt
= isl_point_restore_vec(pnt
, vec
);
1710 pnt
= isl_point_restore_space(pnt
, space
);
1712 isl_local_space_free(ls
);
1716 isl_local_space_free(ls
);
1717 isl_point_free(pnt
);