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 ctx
= isl_space_get_ctx(space
);
87 div
= isl_mat_alloc(ctx
, n_div
, 1 + 1 + total
+ n_div
);
88 return isl_local_space_alloc_div(space
, div
);
91 __isl_give isl_local_space
*isl_local_space_from_space(__isl_take isl_space
*dim
)
93 return isl_local_space_alloc(dim
, 0);
96 __isl_give isl_local_space
*isl_local_space_copy(__isl_keep isl_local_space
*ls
)
105 __isl_give isl_local_space
*isl_local_space_dup(__isl_keep isl_local_space
*ls
)
110 return isl_local_space_alloc_div(isl_space_copy(ls
->dim
),
111 isl_mat_copy(ls
->div
));
115 __isl_give isl_local_space
*isl_local_space_cow(__isl_take isl_local_space
*ls
)
123 return isl_local_space_dup(ls
);
126 __isl_null isl_local_space
*isl_local_space_free(
127 __isl_take isl_local_space
*ls
)
135 isl_space_free(ls
->dim
);
136 isl_mat_free(ls
->div
);
143 /* Is the local space that of a parameter domain?
145 isl_bool
isl_local_space_is_params(__isl_keep isl_local_space
*ls
)
148 return isl_bool_error
;
149 return isl_space_is_params(ls
->dim
);
152 /* Is the local space that of a set?
154 isl_bool
isl_local_space_is_set(__isl_keep isl_local_space
*ls
)
156 return ls
? isl_space_is_set(ls
->dim
) : isl_bool_error
;
159 /* Do "ls1" and "ls2" have the same space?
161 isl_bool
isl_local_space_has_equal_space(__isl_keep isl_local_space
*ls1
,
162 __isl_keep isl_local_space
*ls2
)
165 return isl_bool_error
;
167 return isl_space_is_equal(ls1
->dim
, ls2
->dim
);
170 /* Is the space of "ls" equal to "space"?
172 isl_bool
isl_local_space_has_space(__isl_keep isl_local_space
*ls
,
173 __isl_keep isl_space
*space
)
175 return isl_space_is_equal(isl_local_space_peek_space(ls
), space
);
178 /* Check that the space of "ls" is equal to "space".
180 static isl_stat
isl_local_space_check_has_space(__isl_keep isl_local_space
*ls
,
181 __isl_keep isl_space
*space
)
185 ok
= isl_local_space_has_space(ls
, space
);
187 return isl_stat_error
;
189 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
190 "spaces don't match", return isl_stat_error
);
194 /* Return true if the two local spaces are identical, with identical
195 * expressions for the integer divisions.
197 isl_bool
isl_local_space_is_equal(__isl_keep isl_local_space
*ls1
,
198 __isl_keep isl_local_space
*ls2
)
202 equal
= isl_local_space_has_equal_space(ls1
, ls2
);
203 if (equal
< 0 || !equal
)
206 if (!isl_local_space_divs_known(ls1
))
207 return isl_bool_false
;
208 if (!isl_local_space_divs_known(ls2
))
209 return isl_bool_false
;
211 return isl_mat_is_equal(ls1
->div
, ls2
->div
);
214 /* Compare two isl_local_spaces.
216 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
217 * than "ls2" and 0 if they are equal.
219 int isl_local_space_cmp(__isl_keep isl_local_space
*ls1
,
220 __isl_keep isl_local_space
*ls2
)
231 cmp
= isl_space_cmp(ls1
->dim
, ls2
->dim
);
235 return isl_local_cmp(ls1
->div
, ls2
->div
);
238 int isl_local_space_dim(__isl_keep isl_local_space
*ls
,
239 enum isl_dim_type type
)
243 if (type
== isl_dim_div
)
244 return ls
->div
->n_row
;
245 if (type
== isl_dim_all
)
246 return isl_space_dim(ls
->dim
, isl_dim_all
) + ls
->div
->n_row
;
247 return isl_space_dim(ls
->dim
, type
);
250 unsigned isl_local_space_offset(__isl_keep isl_local_space
*ls
,
251 enum isl_dim_type type
)
260 case isl_dim_cst
: return 0;
261 case isl_dim_param
: return 1;
262 case isl_dim_in
: return 1 + dim
->nparam
;
263 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
264 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
269 /* Return the position of the dimension of the given type and name
271 * Return -1 if no such dimension can be found.
273 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space
*ls
,
274 enum isl_dim_type type
, const char *name
)
278 if (type
== isl_dim_div
)
280 return isl_space_find_dim_by_name(ls
->dim
, type
, name
);
283 /* Does the given dimension have a name?
285 isl_bool
isl_local_space_has_dim_name(__isl_keep isl_local_space
*ls
,
286 enum isl_dim_type type
, unsigned pos
)
288 return ls
? isl_space_has_dim_name(ls
->dim
, type
, pos
) : isl_bool_error
;
291 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space
*ls
,
292 enum isl_dim_type type
, unsigned pos
)
294 return ls
? isl_space_get_dim_name(ls
->dim
, type
, pos
) : NULL
;
297 isl_bool
isl_local_space_has_dim_id(__isl_keep isl_local_space
*ls
,
298 enum isl_dim_type type
, unsigned pos
)
300 return ls
? isl_space_has_dim_id(ls
->dim
, type
, pos
) : isl_bool_error
;
303 __isl_give isl_id
*isl_local_space_get_dim_id(__isl_keep isl_local_space
*ls
,
304 enum isl_dim_type type
, unsigned pos
)
306 return ls
? isl_space_get_dim_id(ls
->dim
, type
, pos
) : NULL
;
309 /* Return the argument of the integer division at position "pos" in "ls".
310 * All local variables in "ls" are known to have a (complete) explicit
313 static __isl_give isl_aff
*extract_div(__isl_keep isl_local_space
*ls
, int pos
)
317 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
320 isl_seq_cpy(aff
->v
->el
, ls
->div
->row
[pos
], aff
->v
->size
);
324 /* Return the argument of the integer division at position "pos" in "ls".
325 * The integer division at that position is known to have a complete
326 * explicit representation, but some of the others do not.
327 * Remove them first because the domain of an isl_aff
328 * is not allowed to have unknown local variables.
330 static __isl_give isl_aff
*drop_unknown_divs_and_extract_div(
331 __isl_keep isl_local_space
*ls
, int pos
)
337 ls
= isl_local_space_copy(ls
);
338 n
= isl_local_space_dim(ls
, isl_dim_div
);
339 for (i
= n
- 1; i
>= 0; --i
) {
340 unknown
= isl_local_space_div_is_marked_unknown(ls
, i
);
342 ls
= isl_local_space_free(ls
);
345 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
, i
, 1);
349 aff
= extract_div(ls
, pos
);
350 isl_local_space_free(ls
);
354 /* Return the argument of the integer division at position "pos" in "ls".
355 * The integer division is assumed to have a complete explicit
356 * representation. If some of the other integer divisions
357 * do not have an explicit representation, then they need
358 * to be removed first because the domain of an isl_aff
359 * is not allowed to have unknown local variables.
361 __isl_give isl_aff
*isl_local_space_get_div(__isl_keep isl_local_space
*ls
,
369 if (pos
< 0 || pos
>= ls
->div
->n_row
)
370 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
371 "index out of bounds", return NULL
);
373 known
= isl_local_space_div_is_known(ls
, pos
);
377 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
378 "expression of div unknown", return NULL
);
379 if (!isl_local_space_is_set(ls
))
380 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
381 "cannot represent divs of map spaces", return NULL
);
383 known
= isl_local_space_divs_known(ls
);
387 return extract_div(ls
, pos
);
389 return drop_unknown_divs_and_extract_div(ls
, pos
);
392 /* Return the space of "ls".
394 __isl_keep isl_space
*isl_local_space_peek_space(__isl_keep isl_local_space
*ls
)
402 __isl_give isl_space
*isl_local_space_get_space(__isl_keep isl_local_space
*ls
)
404 return isl_space_copy(isl_local_space_peek_space(ls
));
407 /* Return the space of "ls".
408 * This may be either a copy or the space itself
409 * if there is only one reference to "ls".
410 * This allows the space to be modified inplace
411 * if both the local space and its space have only a single reference.
412 * The caller is not allowed to modify "ls" between this call and
413 * a subsequent call to isl_local_space_restore_space.
414 * The only exception is that isl_local_space_free can be called instead.
416 __isl_give isl_space
*isl_local_space_take_space(__isl_keep isl_local_space
*ls
)
423 return isl_local_space_get_space(ls
);
429 /* Set the space of "ls" to "space", where the space of "ls" may be missing
430 * due to a preceding call to isl_local_space_take_space.
431 * However, in this case, "ls" only has a single reference and
432 * then the call to isl_local_space_cow has no effect.
434 __isl_give isl_local_space
*isl_local_space_restore_space(
435 __isl_take isl_local_space
*ls
, __isl_take isl_space
*space
)
440 if (ls
->dim
== space
) {
441 isl_space_free(space
);
445 ls
= isl_local_space_cow(ls
);
448 isl_space_free(ls
->dim
);
453 isl_local_space_free(ls
);
454 isl_space_free(space
);
458 /* Return the local variables of "ls".
460 __isl_keep isl_local
*isl_local_space_peek_local(__isl_keep isl_local_space
*ls
)
462 return ls
? ls
->div
: NULL
;
465 /* Replace the identifier of the tuple of type "type" by "id".
467 __isl_give isl_local_space
*isl_local_space_set_tuple_id(
468 __isl_take isl_local_space
*ls
,
469 enum isl_dim_type type
, __isl_take isl_id
*id
)
471 ls
= isl_local_space_cow(ls
);
474 ls
->dim
= isl_space_set_tuple_id(ls
->dim
, type
, id
);
476 return isl_local_space_free(ls
);
483 __isl_give isl_local_space
*isl_local_space_set_dim_name(
484 __isl_take isl_local_space
*ls
,
485 enum isl_dim_type type
, unsigned pos
, const char *s
)
487 ls
= isl_local_space_cow(ls
);
490 ls
->dim
= isl_space_set_dim_name(ls
->dim
, type
, pos
, s
);
492 return isl_local_space_free(ls
);
497 __isl_give isl_local_space
*isl_local_space_set_dim_id(
498 __isl_take isl_local_space
*ls
,
499 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
501 ls
= isl_local_space_cow(ls
);
504 ls
->dim
= isl_space_set_dim_id(ls
->dim
, type
, pos
, id
);
506 return isl_local_space_free(ls
);
514 /* Construct a zero-dimensional local space with the given parameter domain.
516 __isl_give isl_local_space
*isl_local_space_set_from_params(
517 __isl_take isl_local_space
*ls
)
521 space
= isl_local_space_take_space(ls
);
522 space
= isl_space_set_from_params(space
);
523 ls
= isl_local_space_restore_space(ls
, space
);
528 __isl_give isl_local_space
*isl_local_space_reset_space(
529 __isl_take isl_local_space
*ls
, __isl_take isl_space
*dim
)
531 ls
= isl_local_space_cow(ls
);
535 isl_space_free(ls
->dim
);
540 isl_local_space_free(ls
);
545 /* Reorder the dimensions of "ls" according to the given reordering.
546 * The reordering r is assumed to have been extended with the local
547 * variables, leaving them in the same order.
549 __isl_give isl_local_space
*isl_local_space_realign(
550 __isl_take isl_local_space
*ls
, __isl_take isl_reordering
*r
)
552 ls
= isl_local_space_cow(ls
);
556 ls
->div
= isl_local_reorder(ls
->div
, isl_reordering_copy(r
));
560 ls
= isl_local_space_reset_space(ls
, isl_reordering_get_space(r
));
562 isl_reordering_free(r
);
565 isl_local_space_free(ls
);
566 isl_reordering_free(r
);
570 __isl_give isl_local_space
*isl_local_space_add_div(
571 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*div
)
573 ls
= isl_local_space_cow(ls
);
577 if (ls
->div
->n_col
!= div
->size
)
578 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
579 "incompatible dimensions", goto error
);
581 ls
->div
= isl_mat_add_zero_cols(ls
->div
, 1);
582 ls
->div
= isl_mat_add_rows(ls
->div
, 1);
586 isl_seq_cpy(ls
->div
->row
[ls
->div
->n_row
- 1], div
->el
, div
->size
);
587 isl_int_set_si(ls
->div
->row
[ls
->div
->n_row
- 1][div
->size
], 0);
592 isl_local_space_free(ls
);
597 __isl_give isl_local_space
*isl_local_space_replace_divs(
598 __isl_take isl_local_space
*ls
, __isl_take isl_mat
*div
)
600 ls
= isl_local_space_cow(ls
);
605 isl_mat_free(ls
->div
);
610 isl_local_space_free(ls
);
614 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
617 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
618 __isl_keep isl_mat
*src
, int s
, int *exp
)
621 unsigned c
= src
->n_col
- src
->n_row
;
623 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
624 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
626 for (i
= 0; i
< s
; ++i
)
627 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
630 /* Compare (known) divs.
631 * Return non-zero if at least one of the two divs is unknown.
632 * In particular, if both divs are unknown, we respect their
633 * current order. Otherwise, we sort the known div after the unknown
634 * div only if the known div depends on the unknown div.
636 static int cmp_row(isl_int
*row_i
, isl_int
*row_j
, int i
, int j
,
637 unsigned n_row
, unsigned n_col
)
640 int unknown_i
, unknown_j
;
642 unknown_i
= isl_int_is_zero(row_i
[0]);
643 unknown_j
= isl_int_is_zero(row_j
[0]);
645 if (unknown_i
&& unknown_j
)
649 li
= n_col
- n_row
+ i
;
651 li
= isl_seq_last_non_zero(row_i
, n_col
);
653 lj
= n_col
- n_row
+ j
;
655 lj
= isl_seq_last_non_zero(row_j
, n_col
);
660 return isl_seq_cmp(row_i
, row_j
, n_col
);
663 /* Call cmp_row for divs in a matrix.
665 int isl_mat_cmp_div(__isl_keep isl_mat
*div
, int i
, int j
)
667 return cmp_row(div
->row
[i
], div
->row
[j
], i
, j
, div
->n_row
, div
->n_col
);
670 /* Call cmp_row for divs in a basic map.
672 static int bmap_cmp_row(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
675 return cmp_row(bmap
->div
[i
], bmap
->div
[j
], i
, j
, bmap
->n_div
, total
);
678 /* Sort the divs in "bmap".
680 * We first make sure divs are placed after divs on which they depend.
681 * Then we perform a simple insertion sort based on the same ordering
682 * that is used in isl_merge_divs.
684 __isl_give isl_basic_map
*isl_basic_map_sort_divs(
685 __isl_take isl_basic_map
*bmap
)
690 bmap
= isl_basic_map_order_divs(bmap
);
693 if (bmap
->n_div
<= 1)
696 total
= 2 + isl_basic_map_total_dim(bmap
);
697 for (i
= 1; i
< bmap
->n_div
; ++i
) {
698 for (j
= i
- 1; j
>= 0; --j
) {
699 if (bmap_cmp_row(bmap
, j
, j
+ 1, total
) <= 0)
701 isl_basic_map_swap_div(bmap
, j
, j
+ 1);
708 /* Sort the divs in the basic maps of "map".
710 __isl_give isl_map
*isl_map_sort_divs(__isl_take isl_map
*map
)
712 return isl_map_inline_foreach_basic_map(map
, &isl_basic_map_sort_divs
);
715 /* Combine the two lists of divs into a single list.
716 * For each row i in div1, exp1[i] is set to the position of the corresponding
717 * row in the result. Similarly for div2 and exp2.
718 * This function guarantees
720 * exp1[i+1] > exp1[i]
721 * For optimal merging, the two input list should have been sorted.
723 __isl_give isl_mat
*isl_merge_divs(__isl_keep isl_mat
*div1
,
724 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
733 d
= div1
->n_col
- div1
->n_row
;
734 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
735 d
+ div1
->n_row
+ div2
->n_row
);
739 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
742 expand_row(div
, k
, div1
, i
, exp1
);
743 expand_row(div
, k
+ 1, div2
, j
, exp2
);
745 cmp
= isl_mat_cmp_div(div
, k
, k
+ 1);
749 } else if (cmp
< 0) {
753 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
756 for (; i
< div1
->n_row
; ++i
, ++k
) {
757 expand_row(div
, k
, div1
, i
, exp1
);
760 for (; j
< div2
->n_row
; ++j
, ++k
) {
761 expand_row(div
, k
, div2
, j
, exp2
);
771 /* Swap divs "a" and "b" in "ls".
773 __isl_give isl_local_space
*isl_local_space_swap_div(
774 __isl_take isl_local_space
*ls
, int a
, int b
)
778 ls
= isl_local_space_cow(ls
);
781 if (a
< 0 || a
>= ls
->div
->n_row
|| b
< 0 || b
>= ls
->div
->n_row
)
782 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
783 "index out of bounds", return isl_local_space_free(ls
));
784 offset
= ls
->div
->n_col
- ls
->div
->n_row
;
785 ls
->div
= isl_mat_swap_cols(ls
->div
, offset
+ a
, offset
+ b
);
786 ls
->div
= isl_mat_swap_rows(ls
->div
, a
, b
);
788 return isl_local_space_free(ls
);
792 /* Construct a local space that contains all the divs in either
795 __isl_give isl_local_space
*isl_local_space_intersect(
796 __isl_take isl_local_space
*ls1
, __isl_take isl_local_space
*ls2
)
807 ctx
= isl_local_space_get_ctx(ls1
);
808 if (!isl_space_is_equal(ls1
->dim
, ls2
->dim
))
809 isl_die(ctx
, isl_error_invalid
,
810 "spaces should be identical", goto error
);
812 if (ls2
->div
->n_row
== 0) {
813 isl_local_space_free(ls2
);
817 if (ls1
->div
->n_row
== 0) {
818 isl_local_space_free(ls1
);
822 exp1
= isl_alloc_array(ctx
, int, ls1
->div
->n_row
);
823 exp2
= isl_alloc_array(ctx
, int, ls2
->div
->n_row
);
827 div
= isl_merge_divs(ls1
->div
, ls2
->div
, exp1
, exp2
);
831 equal
= isl_mat_is_equal(ls1
->div
, div
);
835 ls1
= isl_local_space_cow(ls1
);
841 isl_local_space_free(ls2
);
842 isl_mat_free(ls1
->div
);
850 isl_local_space_free(ls1
);
851 isl_local_space_free(ls2
);
855 /* Is the local variable "div" of "ls" marked as not having
856 * an explicit representation?
857 * Note that even if this variable is not marked in this way and therefore
858 * does have an explicit representation, this representation may still
859 * depend (indirectly) on other local variables that do not
860 * have an explicit representation.
862 isl_bool
isl_local_space_div_is_marked_unknown(__isl_keep isl_local_space
*ls
,
866 return isl_bool_error
;
867 return isl_local_div_is_marked_unknown(ls
->div
, div
);
870 /* Does "ls" have a complete explicit representation for div "div"?
872 isl_bool
isl_local_space_div_is_known(__isl_keep isl_local_space
*ls
, int div
)
875 return isl_bool_error
;
876 return isl_local_div_is_known(ls
->div
, div
);
879 /* Does "ls" have an explicit representation for all local variables?
881 isl_bool
isl_local_space_divs_known(__isl_keep isl_local_space
*ls
)
884 return isl_bool_error
;
885 return isl_local_divs_known(ls
->div
);
888 __isl_give isl_local_space
*isl_local_space_domain(
889 __isl_take isl_local_space
*ls
)
891 ls
= isl_local_space_drop_dims(ls
, isl_dim_out
,
892 0, isl_local_space_dim(ls
, isl_dim_out
));
893 ls
= isl_local_space_cow(ls
);
896 ls
->dim
= isl_space_domain(ls
->dim
);
898 return isl_local_space_free(ls
);
902 __isl_give isl_local_space
*isl_local_space_range(
903 __isl_take isl_local_space
*ls
)
905 ls
= isl_local_space_drop_dims(ls
, isl_dim_in
,
906 0, isl_local_space_dim(ls
, isl_dim_in
));
907 ls
= isl_local_space_cow(ls
);
911 ls
->dim
= isl_space_range(ls
->dim
);
913 return isl_local_space_free(ls
);
917 /* Construct a local space for a map that has the given local
918 * space as domain and that has a zero-dimensional range.
920 __isl_give isl_local_space
*isl_local_space_from_domain(
921 __isl_take isl_local_space
*ls
)
923 ls
= isl_local_space_cow(ls
);
926 ls
->dim
= isl_space_from_domain(ls
->dim
);
928 return isl_local_space_free(ls
);
932 __isl_give isl_local_space
*isl_local_space_add_dims(
933 __isl_take isl_local_space
*ls
, enum isl_dim_type type
, unsigned n
)
939 pos
= isl_local_space_dim(ls
, type
);
940 return isl_local_space_insert_dims(ls
, type
, pos
, n
);
943 /* Remove common factor of non-constant terms and denominator.
945 static void normalize_div(__isl_keep isl_local_space
*ls
, int div
)
947 isl_ctx
*ctx
= ls
->div
->ctx
;
948 unsigned total
= ls
->div
->n_col
- 2;
950 isl_seq_gcd(ls
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
951 isl_int_gcd(ctx
->normalize_gcd
,
952 ctx
->normalize_gcd
, ls
->div
->row
[div
][0]);
953 if (isl_int_is_one(ctx
->normalize_gcd
))
956 isl_seq_scale_down(ls
->div
->row
[div
] + 2, ls
->div
->row
[div
] + 2,
957 ctx
->normalize_gcd
, total
);
958 isl_int_divexact(ls
->div
->row
[div
][0], ls
->div
->row
[div
][0],
960 isl_int_fdiv_q(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1],
964 /* Exploit the equalities in "eq" to simplify the expressions of
965 * the integer divisions in "ls".
966 * The integer divisions in "ls" are assumed to appear as regular
967 * dimensions in "eq".
969 __isl_give isl_local_space
*isl_local_space_substitute_equalities(
970 __isl_take isl_local_space
*ls
, __isl_take isl_basic_set
*eq
)
979 total
= isl_space_dim(eq
->dim
, isl_dim_all
);
980 if (isl_local_space_dim(ls
, isl_dim_all
) != total
)
981 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
982 "spaces don't match", goto error
);
985 for (i
= 0; i
< eq
->n_eq
; ++i
) {
986 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
987 if (j
< 0 || j
== 0 || j
>= total
)
990 for (k
= 0; k
< ls
->div
->n_row
; ++k
) {
991 if (isl_int_is_zero(ls
->div
->row
[k
][1 + j
]))
993 ls
= isl_local_space_cow(ls
);
996 ls
->div
= isl_mat_cow(ls
->div
);
999 isl_seq_elim(ls
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
1000 &ls
->div
->row
[k
][0]);
1001 normalize_div(ls
, k
);
1005 isl_basic_set_free(eq
);
1008 isl_basic_set_free(eq
);
1009 isl_local_space_free(ls
);
1013 /* Plug in the affine expressions "subs" of length "subs_len" (including
1014 * the denominator and the constant term) into the variable at position "pos"
1015 * of the "n" div expressions starting at "first".
1017 * Let i be the dimension to replace and let "subs" be of the form
1021 * Any integer division starting at "first" with a non-zero coefficient for i,
1023 * floor((a i + g)/m)
1027 * floor((a f + d g)/(m d))
1029 __isl_give isl_local_space
*isl_local_space_substitute_seq(
1030 __isl_take isl_local_space
*ls
,
1031 enum isl_dim_type type
, unsigned pos
, isl_int
*subs
, int subs_len
,
1039 ls
= isl_local_space_cow(ls
);
1042 ls
->div
= isl_mat_cow(ls
->div
);
1044 return isl_local_space_free(ls
);
1046 if (first
+ n
> ls
->div
->n_row
)
1047 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1048 "index out of bounds", return isl_local_space_free(ls
));
1050 pos
+= isl_local_space_offset(ls
, type
);
1053 for (i
= first
; i
< first
+ n
; ++i
) {
1054 if (isl_int_is_zero(ls
->div
->row
[i
][1 + pos
]))
1056 isl_seq_substitute(ls
->div
->row
[i
], pos
, subs
,
1057 ls
->div
->n_col
, subs_len
, v
);
1058 normalize_div(ls
, i
);
1065 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
1068 * Let i be the dimension to replace and let "subs" be of the form
1072 * Any integer division with a non-zero coefficient for i,
1074 * floor((a i + g)/m)
1078 * floor((a f + d g)/(m d))
1080 __isl_give isl_local_space
*isl_local_space_substitute(
1081 __isl_take isl_local_space
*ls
,
1082 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
1084 ls
= isl_local_space_cow(ls
);
1086 return isl_local_space_free(ls
);
1088 if (!isl_space_is_equal(ls
->dim
, subs
->ls
->dim
))
1089 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1090 "spaces don't match", return isl_local_space_free(ls
));
1091 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
1092 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
1093 "cannot handle divs yet",
1094 return isl_local_space_free(ls
));
1096 return isl_local_space_substitute_seq(ls
, type
, pos
, subs
->v
->el
,
1097 subs
->v
->size
, 0, ls
->div
->n_row
);
1100 isl_bool
isl_local_space_is_named_or_nested(__isl_keep isl_local_space
*ls
,
1101 enum isl_dim_type type
)
1104 return isl_bool_error
;
1105 return isl_space_is_named_or_nested(ls
->dim
, type
);
1108 __isl_give isl_local_space
*isl_local_space_drop_dims(
1109 __isl_take isl_local_space
*ls
,
1110 enum isl_dim_type type
, unsigned first
, unsigned n
)
1116 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
1119 ctx
= isl_local_space_get_ctx(ls
);
1120 if (first
+ n
> isl_local_space_dim(ls
, type
))
1121 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
1122 return isl_local_space_free(ls
));
1124 ls
= isl_local_space_cow(ls
);
1128 if (type
== isl_dim_div
) {
1129 ls
->div
= isl_mat_drop_rows(ls
->div
, first
, n
);
1131 ls
->dim
= isl_space_drop_dims(ls
->dim
, type
, first
, n
);
1133 return isl_local_space_free(ls
);
1136 first
+= 1 + isl_local_space_offset(ls
, type
);
1137 ls
->div
= isl_mat_drop_cols(ls
->div
, first
, n
);
1139 return isl_local_space_free(ls
);
1144 __isl_give isl_local_space
*isl_local_space_insert_dims(
1145 __isl_take isl_local_space
*ls
,
1146 enum isl_dim_type type
, unsigned first
, unsigned n
)
1152 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
1155 ctx
= isl_local_space_get_ctx(ls
);
1156 if (first
> isl_local_space_dim(ls
, type
))
1157 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1158 return isl_local_space_free(ls
));
1160 ls
= isl_local_space_cow(ls
);
1164 if (type
== isl_dim_div
) {
1165 ls
->div
= isl_mat_insert_zero_rows(ls
->div
, first
, n
);
1167 ls
->dim
= isl_space_insert_dims(ls
->dim
, type
, first
, n
);
1169 return isl_local_space_free(ls
);
1172 first
+= 1 + isl_local_space_offset(ls
, type
);
1173 ls
->div
= isl_mat_insert_zero_cols(ls
->div
, first
, n
);
1175 return isl_local_space_free(ls
);
1180 /* Does the linear part of "constraint" correspond to
1181 * integer division "div" in "ls"?
1183 * That is, given div = floor((c + f)/m), is the constraint of the form
1185 * f - m d + c' >= 0 [sign = 1]
1187 * -f + m d + c'' >= 0 [sign = -1]
1189 * If so, set *sign to the corresponding value.
1191 static isl_bool
is_linear_div_constraint(__isl_keep isl_local_space
*ls
,
1192 isl_int
*constraint
, unsigned div
, int *sign
)
1197 unknown
= isl_local_space_div_is_marked_unknown(ls
, div
);
1199 return isl_bool_error
;
1201 return isl_bool_false
;
1203 pos
= isl_local_space_offset(ls
, isl_dim_div
) + div
;
1205 if (isl_int_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
1207 if (!isl_seq_is_neg(constraint
+ 1,
1208 ls
->div
->row
[div
] + 2, pos
- 1))
1209 return isl_bool_false
;
1210 } else if (isl_int_abs_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
1212 if (!isl_seq_eq(constraint
+ 1, ls
->div
->row
[div
] + 2, pos
- 1))
1213 return isl_bool_false
;
1215 return isl_bool_false
;
1217 if (isl_seq_first_non_zero(constraint
+ pos
+ 1,
1218 ls
->div
->n_row
- div
- 1) != -1)
1219 return isl_bool_false
;
1220 return isl_bool_true
;
1223 /* Check if the constraints pointed to by "constraint" is a div
1224 * constraint corresponding to div "div" in "ls".
1226 * That is, if div = floor(f/m), then check if the constraint is
1230 * -(f-(m-1)) + m d >= 0
1232 * First check if the linear part is of the right form and
1233 * then check the constant term.
1235 isl_bool
isl_local_space_is_div_constraint(__isl_keep isl_local_space
*ls
,
1236 isl_int
*constraint
, unsigned div
)
1241 linear
= is_linear_div_constraint(ls
, constraint
, div
, &sign
);
1242 if (linear
< 0 || !linear
)
1247 isl_int_sub(ls
->div
->row
[div
][1],
1248 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
1249 isl_int_add_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
1250 neg
= isl_seq_is_neg(constraint
, ls
->div
->row
[div
] + 1, 1);
1251 isl_int_sub_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
1252 isl_int_add(ls
->div
->row
[div
][1],
1253 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
1255 return isl_bool_false
;
1257 if (!isl_int_eq(constraint
[0], ls
->div
->row
[div
][1]))
1258 return isl_bool_false
;
1261 return isl_bool_true
;
1264 /* Is the constraint pointed to by "constraint" one
1265 * of an equality that corresponds to integer division "div" in "ls"?
1267 * That is, given an integer division of the form
1269 * a = floor((f + c)/m)
1271 * is the equality of the form
1275 * Note that the constant term is not checked explicitly, but given
1276 * that this is a valid equality constraint, the constant c' necessarily
1277 * has a value close to -c.
1279 isl_bool
isl_local_space_is_div_equality(__isl_keep isl_local_space
*ls
,
1280 isl_int
*constraint
, unsigned div
)
1285 linear
= is_linear_div_constraint(ls
, constraint
, div
, &sign
);
1286 if (linear
< 0 || !linear
)
1293 * Set active[i] to 1 if the dimension at position i is involved
1294 * in the linear expression l.
1296 int *isl_local_space_get_active(__isl_keep isl_local_space
*ls
, isl_int
*l
)
1304 ctx
= isl_local_space_get_ctx(ls
);
1305 total
= isl_local_space_dim(ls
, isl_dim_all
);
1306 active
= isl_calloc_array(ctx
, int, total
);
1307 if (total
&& !active
)
1310 for (i
= 0; i
< total
; ++i
)
1311 active
[i
] = !isl_int_is_zero(l
[i
]);
1313 offset
= isl_local_space_offset(ls
, isl_dim_div
) - 1;
1314 for (i
= ls
->div
->n_row
- 1; i
>= 0; --i
) {
1315 if (!active
[offset
+ i
])
1317 for (j
= 0; j
< total
; ++j
)
1318 active
[j
] |= !isl_int_is_zero(ls
->div
->row
[i
][2 + j
]);
1324 /* Given a local space "ls" of a set, create a local space
1325 * for the lift of the set. In particular, the result
1326 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1327 * range of the wrapped map.
1329 __isl_give isl_local_space
*isl_local_space_lift(
1330 __isl_take isl_local_space
*ls
)
1332 ls
= isl_local_space_cow(ls
);
1336 ls
->dim
= isl_space_lift(ls
->dim
, ls
->div
->n_row
);
1337 ls
->div
= isl_mat_drop_rows(ls
->div
, 0, ls
->div
->n_row
);
1338 if (!ls
->dim
|| !ls
->div
)
1339 return isl_local_space_free(ls
);
1344 /* Construct a basic map that maps a set living in local space "ls"
1345 * to the corresponding lifted local space.
1347 __isl_give isl_basic_map
*isl_local_space_lifting(
1348 __isl_take isl_local_space
*ls
)
1350 isl_basic_map
*lifting
;
1351 isl_basic_set
*bset
;
1355 if (!isl_local_space_is_set(ls
))
1356 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1357 "lifting only defined on set spaces", goto error
);
1359 bset
= isl_basic_set_from_local_space(ls
);
1360 lifting
= isl_basic_set_unwrap(isl_basic_set_lift(bset
));
1361 lifting
= isl_basic_map_domain_map(lifting
);
1362 lifting
= isl_basic_map_reverse(lifting
);
1366 isl_local_space_free(ls
);
1370 /* Compute the preimage of "ls" under the function represented by "ma".
1371 * In other words, plug in "ma" in "ls". The result is a local space
1372 * that is part of the domain space of "ma".
1374 * If the divs in "ls" are represented as
1376 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1378 * and ma is represented by
1380 * x = D(p) + F(y) + G(divs')
1382 * then the resulting divs are
1384 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1386 * We first copy over the divs from "ma" and then
1387 * we add the modified divs from "ls".
1389 __isl_give isl_local_space
*isl_local_space_preimage_multi_aff(
1390 __isl_take isl_local_space
*ls
, __isl_take isl_multi_aff
*ma
)
1394 isl_local_space
*res
= NULL
;
1395 int n_div_ls
, n_div_ma
;
1396 isl_int f
, c1
, c2
, g
;
1398 ma
= isl_multi_aff_align_divs(ma
);
1401 if (!isl_space_is_range_internal(ls
->dim
, ma
->space
))
1402 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1403 "spaces don't match", goto error
);
1405 n_div_ls
= isl_local_space_dim(ls
, isl_dim_div
);
1406 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
1408 space
= isl_space_domain(isl_multi_aff_get_space(ma
));
1409 res
= isl_local_space_alloc(space
, n_div_ma
+ n_div_ls
);
1414 isl_mat_free(res
->div
);
1415 res
->div
= isl_mat_copy(ma
->u
.p
[0]->ls
->div
);
1416 res
->div
= isl_mat_add_zero_cols(res
->div
, n_div_ls
);
1417 res
->div
= isl_mat_add_rows(res
->div
, n_div_ls
);
1427 for (i
= 0; i
< ls
->div
->n_row
; ++i
) {
1428 if (isl_int_is_zero(ls
->div
->row
[i
][0])) {
1429 isl_int_set_si(res
->div
->row
[n_div_ma
+ i
][0], 0);
1432 isl_seq_preimage(res
->div
->row
[n_div_ma
+ i
], ls
->div
->row
[i
],
1433 ma
, 0, 0, n_div_ma
, n_div_ls
, f
, c1
, c2
, g
, 1);
1434 normalize_div(res
, n_div_ma
+ i
);
1442 isl_local_space_free(ls
);
1443 isl_multi_aff_free(ma
);
1446 isl_local_space_free(ls
);
1447 isl_multi_aff_free(ma
);
1448 isl_local_space_free(res
);
1452 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1453 * to dimensions of "dst_type" at "dst_pos".
1455 * Moving to/from local dimensions is not allowed.
1456 * We currently assume that the dimension type changes.
1458 __isl_give isl_local_space
*isl_local_space_move_dims(
1459 __isl_take isl_local_space
*ls
,
1460 enum isl_dim_type dst_type
, unsigned dst_pos
,
1461 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1469 !isl_local_space_is_named_or_nested(ls
, src_type
) &&
1470 !isl_local_space_is_named_or_nested(ls
, dst_type
))
1473 if (src_pos
+ n
> isl_local_space_dim(ls
, src_type
))
1474 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1475 "range out of bounds", return isl_local_space_free(ls
));
1476 if (dst_pos
> isl_local_space_dim(ls
, dst_type
))
1477 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1478 "position out of bounds",
1479 return isl_local_space_free(ls
));
1480 if (src_type
== isl_dim_div
)
1481 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1482 "cannot move divs", return isl_local_space_free(ls
));
1483 if (dst_type
== isl_dim_div
)
1484 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
1485 "cannot move to divs", return isl_local_space_free(ls
));
1486 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1488 if (dst_type
== src_type
)
1489 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
1490 "moving dims within the same type not supported",
1491 return isl_local_space_free(ls
));
1493 ls
= isl_local_space_cow(ls
);
1497 g_src_pos
= 1 + isl_local_space_offset(ls
, src_type
) + src_pos
;
1498 g_dst_pos
= 1 + isl_local_space_offset(ls
, dst_type
) + dst_pos
;
1499 if (dst_type
> src_type
)
1501 ls
->div
= isl_mat_move_cols(ls
->div
, g_dst_pos
, g_src_pos
, n
);
1503 return isl_local_space_free(ls
);
1504 ls
->dim
= isl_space_move_dims(ls
->dim
, dst_type
, dst_pos
,
1505 src_type
, src_pos
, n
);
1507 return isl_local_space_free(ls
);
1512 /* Remove any internal structure of the domain of "ls".
1513 * If there is any such internal structure in the input,
1514 * then the name of the corresponding space is also removed.
1516 __isl_give isl_local_space
*isl_local_space_flatten_domain(
1517 __isl_take isl_local_space
*ls
)
1522 if (!ls
->dim
->nested
[0])
1525 ls
= isl_local_space_cow(ls
);
1529 ls
->dim
= isl_space_flatten_domain(ls
->dim
);
1531 return isl_local_space_free(ls
);
1536 /* Remove any internal structure of the range of "ls".
1537 * If there is any such internal structure in the input,
1538 * then the name of the corresponding space is also removed.
1540 __isl_give isl_local_space
*isl_local_space_flatten_range(
1541 __isl_take isl_local_space
*ls
)
1546 if (!ls
->dim
->nested
[1])
1549 ls
= isl_local_space_cow(ls
);
1553 ls
->dim
= isl_space_flatten_range(ls
->dim
);
1555 return isl_local_space_free(ls
);
1560 /* Given the local space "ls" of a map, return the local space of a set
1561 * that lives in a space that wraps the space of "ls" and that has
1564 __isl_give isl_local_space
*isl_local_space_wrap(__isl_take isl_local_space
*ls
)
1566 ls
= isl_local_space_cow(ls
);
1570 ls
->dim
= isl_space_wrap(ls
->dim
);
1572 return isl_local_space_free(ls
);
1577 /* Lift the point "pnt", living in the space of "ls"
1578 * to live in a space with extra coordinates corresponding
1579 * to the local variables of "ls".
1581 __isl_give isl_point
*isl_local_space_lift_point(__isl_take isl_local_space
*ls
,
1582 __isl_take isl_point
*pnt
)
1589 if (isl_local_space_check_has_space(ls
, isl_point_peek_space(pnt
)) < 0)
1592 local
= isl_local_space_peek_local(ls
);
1593 n_local
= isl_local_space_dim(ls
, isl_dim_div
);
1595 space
= isl_point_take_space(pnt
);
1596 vec
= isl_point_take_vec(pnt
);
1598 space
= isl_space_lift(space
, n_local
);
1599 vec
= isl_local_extend_point_vec(local
, vec
);
1601 pnt
= isl_point_restore_vec(pnt
, vec
);
1602 pnt
= isl_point_restore_space(pnt
, space
);
1604 isl_local_space_free(ls
);
1608 isl_local_space_free(ls
);
1609 isl_point_free(pnt
);