2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #include <isl_ctx_private.h>
12 #include <isl_map_private.h>
13 #include <isl_local_space_private.h>
14 #include <isl_space_private.h>
15 #include <isl_mat_private.h>
16 #include <isl_aff_private.h>
19 isl_ctx
*isl_local_space_get_ctx(__isl_keep isl_local_space
*ls
)
21 return ls
? ls
->dim
->ctx
: NULL
;
24 __isl_give isl_local_space
*isl_local_space_alloc_div(__isl_take isl_space
*dim
,
25 __isl_take isl_mat
*div
)
28 isl_local_space
*ls
= NULL
;
33 ctx
= isl_space_get_ctx(dim
);
34 ls
= isl_calloc_type(ctx
, struct isl_local_space
);
46 isl_local_space_free(ls
);
50 __isl_give isl_local_space
*isl_local_space_alloc(__isl_take isl_space
*dim
,
60 total
= isl_space_dim(dim
, isl_dim_all
);
62 ctx
= isl_space_get_ctx(dim
);
63 div
= isl_mat_alloc(ctx
, n_div
, 1 + 1 + total
+ n_div
);
64 return isl_local_space_alloc_div(dim
, div
);
67 __isl_give isl_local_space
*isl_local_space_from_space(__isl_take isl_space
*dim
)
69 return isl_local_space_alloc(dim
, 0);
72 __isl_give isl_local_space
*isl_local_space_copy(__isl_keep isl_local_space
*ls
)
81 __isl_give isl_local_space
*isl_local_space_dup(__isl_keep isl_local_space
*ls
)
86 return isl_local_space_alloc_div(isl_space_copy(ls
->dim
),
87 isl_mat_copy(ls
->div
));
91 __isl_give isl_local_space
*isl_local_space_cow(__isl_take isl_local_space
*ls
)
99 return isl_local_space_dup(ls
);
102 void *isl_local_space_free(__isl_take isl_local_space
*ls
)
110 isl_space_free(ls
->dim
);
111 isl_mat_free(ls
->div
);
118 /* Is the local space that of a set?
120 int isl_local_space_is_set(__isl_keep isl_local_space
*ls
)
122 return ls
? isl_space_is_set(ls
->dim
) : -1;
125 /* Return true if the two local spaces are identical, with identical
126 * expressions for the integer divisions.
128 int isl_local_space_is_equal(__isl_keep isl_local_space
*ls1
,
129 __isl_keep isl_local_space
*ls2
)
136 equal
= isl_space_is_equal(ls1
->dim
, ls2
->dim
);
137 if (equal
< 0 || !equal
)
140 if (!isl_local_space_divs_known(ls1
))
142 if (!isl_local_space_divs_known(ls2
))
145 return isl_mat_is_equal(ls1
->div
, ls2
->div
);
148 int isl_local_space_dim(__isl_keep isl_local_space
*ls
,
149 enum isl_dim_type type
)
153 if (type
== isl_dim_div
)
154 return ls
->div
->n_row
;
155 if (type
== isl_dim_all
)
156 return isl_space_dim(ls
->dim
, isl_dim_all
) + ls
->div
->n_row
;
157 return isl_space_dim(ls
->dim
, type
);
160 unsigned isl_local_space_offset(__isl_keep isl_local_space
*ls
,
161 enum isl_dim_type type
)
170 case isl_dim_cst
: return 0;
171 case isl_dim_param
: return 1;
172 case isl_dim_in
: return 1 + dim
->nparam
;
173 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
174 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
179 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space
*ls
,
180 enum isl_dim_type type
, unsigned pos
)
182 return ls
? isl_space_get_dim_name(ls
->dim
, type
, pos
) : NULL
;
185 __isl_give isl_aff
*isl_local_space_get_div(__isl_keep isl_local_space
*ls
,
193 if (pos
< 0 || pos
>= ls
->div
->n_row
)
194 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
195 "index out of bounds", return NULL
);
197 if (isl_int_is_zero(ls
->div
->row
[pos
][0]))
198 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
199 "expression of div unknown", return NULL
);
201 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
204 isl_seq_cpy(aff
->v
->el
, ls
->div
->row
[pos
], aff
->v
->size
);
208 __isl_give isl_space
*isl_local_space_get_space(__isl_keep isl_local_space
*ls
)
213 return isl_space_copy(ls
->dim
);
216 __isl_give isl_local_space
*isl_local_space_set_dim_name(
217 __isl_take isl_local_space
*ls
,
218 enum isl_dim_type type
, unsigned pos
, const char *s
)
220 ls
= isl_local_space_cow(ls
);
223 ls
->dim
= isl_space_set_dim_name(ls
->dim
, type
, pos
, s
);
225 return isl_local_space_free(ls
);
230 __isl_give isl_local_space
*isl_local_space_set_dim_id(
231 __isl_take isl_local_space
*ls
,
232 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
234 ls
= isl_local_space_cow(ls
);
236 return isl_id_free(id
);
237 ls
->dim
= isl_space_set_dim_id(ls
->dim
, type
, pos
, id
);
239 return isl_local_space_free(ls
);
244 __isl_give isl_local_space
*isl_local_space_reset_space(
245 __isl_take isl_local_space
*ls
, __isl_take isl_space
*dim
)
247 ls
= isl_local_space_cow(ls
);
251 isl_space_free(ls
->dim
);
256 isl_local_space_free(ls
);
261 /* Reorder the columns of the given div definitions according to the
263 * The order of the divs themselves is assumed not to change.
265 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
266 __isl_take isl_reordering
*r
)
275 extra
= isl_space_dim(r
->dim
, isl_dim_all
) + div
->n_row
- r
->len
;
276 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
280 for (i
= 0; i
< div
->n_row
; ++i
) {
281 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
282 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
283 for (j
= 0; j
< r
->len
; ++j
)
284 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
288 isl_reordering_free(r
);
292 isl_reordering_free(r
);
297 /* Reorder the dimensions of "ls" according to the given reordering.
298 * The reordering r is assumed to have been extended with the local
299 * variables, leaving them in the same order.
301 __isl_give isl_local_space
*isl_local_space_realign(
302 __isl_take isl_local_space
*ls
, __isl_take isl_reordering
*r
)
304 ls
= isl_local_space_cow(ls
);
308 ls
->div
= reorder_divs(ls
->div
, isl_reordering_copy(r
));
312 ls
= isl_local_space_reset_space(ls
, isl_space_copy(r
->dim
));
314 isl_reordering_free(r
);
317 isl_local_space_free(ls
);
318 isl_reordering_free(r
);
322 __isl_give isl_local_space
*isl_local_space_add_div(
323 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*div
)
325 ls
= isl_local_space_cow(ls
);
329 if (ls
->div
->n_col
!= div
->size
)
330 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
331 "incompatible dimensions", goto error
);
333 ls
->div
= isl_mat_add_zero_cols(ls
->div
, 1);
334 ls
->div
= isl_mat_add_rows(ls
->div
, 1);
338 isl_seq_cpy(ls
->div
->row
[ls
->div
->n_row
- 1], div
->el
, div
->size
);
339 isl_int_set_si(ls
->div
->row
[ls
->div
->n_row
- 1][div
->size
], 0);
344 isl_local_space_free(ls
);
349 __isl_give isl_local_space
*isl_local_space_replace_divs(
350 __isl_take isl_local_space
*ls
, __isl_take isl_mat
*div
)
352 ls
= isl_local_space_cow(ls
);
357 isl_mat_free(ls
->div
);
362 isl_local_space_free(ls
);
366 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
369 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
370 __isl_keep isl_mat
*src
, int s
, int *exp
)
373 unsigned c
= src
->n_col
- src
->n_row
;
375 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
376 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
378 for (i
= 0; i
< s
; ++i
)
379 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
382 /* Compare (known) divs.
383 * Return non-zero if at least one of the two divs is unknown.
385 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
389 if (isl_int_is_zero(div
->row
[j
][0]))
391 if (isl_int_is_zero(div
->row
[i
][0]))
394 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
395 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
400 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
403 /* Combine the two lists of divs into a single list.
404 * For each row i in div1, exp1[i] is set to the position of the corresponding
405 * row in the result. Similarly for div2 and exp2.
406 * This function guarantees
408 * exp1[i+1] > exp1[i]
409 * For optimal merging, the two input list should have been sorted.
411 __isl_give isl_mat
*isl_merge_divs(__isl_keep isl_mat
*div1
,
412 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
421 d
= div1
->n_col
- div1
->n_row
;
422 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
423 d
+ div1
->n_row
+ div2
->n_row
);
427 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
430 expand_row(div
, k
, div1
, i
, exp1
);
431 expand_row(div
, k
+ 1, div2
, j
, exp2
);
433 cmp
= cmp_row(div
, k
, k
+ 1);
437 } else if (cmp
< 0) {
441 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
444 for (; i
< div1
->n_row
; ++i
, ++k
) {
445 expand_row(div
, k
, div1
, i
, exp1
);
448 for (; j
< div2
->n_row
; ++j
, ++k
) {
449 expand_row(div
, k
, div2
, j
, exp2
);
459 /* Construct a local space that contains all the divs in either
462 __isl_give isl_local_space
*isl_local_space_intersect(
463 __isl_take isl_local_space
*ls1
, __isl_take isl_local_space
*ls2
)
473 ctx
= isl_local_space_get_ctx(ls1
);
474 if (!isl_space_is_equal(ls1
->dim
, ls2
->dim
))
475 isl_die(ctx
, isl_error_invalid
,
476 "spaces should be identical", goto error
);
478 if (ls2
->div
->n_row
== 0) {
479 isl_local_space_free(ls2
);
483 if (ls1
->div
->n_row
== 0) {
484 isl_local_space_free(ls1
);
488 exp1
= isl_alloc_array(ctx
, int, ls1
->div
->n_row
);
489 exp2
= isl_alloc_array(ctx
, int, ls2
->div
->n_row
);
493 div
= isl_merge_divs(ls1
->div
, ls2
->div
, exp1
, exp2
);
499 isl_local_space_free(ls2
);
500 isl_mat_free(ls1
->div
);
507 isl_local_space_free(ls1
);
508 isl_local_space_free(ls2
);
512 int isl_local_space_divs_known(__isl_keep isl_local_space
*ls
)
519 for (i
= 0; i
< ls
->div
->n_row
; ++i
)
520 if (isl_int_is_zero(ls
->div
->row
[i
][0]))
526 __isl_give isl_local_space
*isl_local_space_domain(
527 __isl_take isl_local_space
*ls
)
529 ls
= isl_local_space_drop_dims(ls
, isl_dim_out
,
530 0, isl_local_space_dim(ls
, isl_dim_out
));
531 ls
= isl_local_space_cow(ls
);
534 ls
->dim
= isl_space_domain(ls
->dim
);
536 return isl_local_space_free(ls
);
540 __isl_give isl_local_space
*isl_local_space_range(
541 __isl_take isl_local_space
*ls
)
543 ls
= isl_local_space_drop_dims(ls
, isl_dim_in
,
544 0, isl_local_space_dim(ls
, isl_dim_in
));
545 ls
= isl_local_space_cow(ls
);
549 ls
->dim
= isl_space_range(ls
->dim
);
551 return isl_local_space_free(ls
);
555 /* Construct a local space for a map that has the given local
556 * space as domain and that has a zero-dimensional range.
558 __isl_give isl_local_space
*isl_local_space_from_domain(
559 __isl_take isl_local_space
*ls
)
561 ls
= isl_local_space_cow(ls
);
564 ls
->dim
= isl_space_from_domain(ls
->dim
);
566 return isl_local_space_free(ls
);
570 __isl_give isl_local_space
*isl_local_space_add_dims(
571 __isl_take isl_local_space
*ls
, enum isl_dim_type type
, unsigned n
)
577 pos
= isl_local_space_dim(ls
, type
);
578 return isl_local_space_insert_dims(ls
, type
, pos
, n
);
581 /* Remove common factor of non-constant terms and denominator.
583 static void normalize_div(__isl_keep isl_local_space
*ls
, int div
)
585 isl_ctx
*ctx
= ls
->div
->ctx
;
586 unsigned total
= ls
->div
->n_col
- 2;
588 isl_seq_gcd(ls
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
589 isl_int_gcd(ctx
->normalize_gcd
,
590 ctx
->normalize_gcd
, ls
->div
->row
[div
][0]);
591 if (isl_int_is_one(ctx
->normalize_gcd
))
594 isl_seq_scale_down(ls
->div
->row
[div
] + 2, ls
->div
->row
[div
] + 2,
595 ctx
->normalize_gcd
, total
);
596 isl_int_divexact(ls
->div
->row
[div
][0], ls
->div
->row
[div
][0],
598 isl_int_fdiv_q(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1],
602 /* Exploit the equalities in "eq" to simplify the expressions of
603 * the integer divisions in "ls".
604 * The integer divisions in "ls" are assumed to appear as regular
605 * dimensions in "eq".
607 __isl_give isl_local_space
*isl_local_space_substitute_equalities(
608 __isl_take isl_local_space
*ls
, __isl_take isl_basic_set
*eq
)
614 ls
= isl_local_space_cow(ls
);
618 total
= isl_space_dim(eq
->dim
, isl_dim_all
);
619 if (isl_local_space_dim(ls
, isl_dim_all
) != total
)
620 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
621 "dimensions don't match", goto error
);
624 for (i
= 0; i
< eq
->n_eq
; ++i
) {
625 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
626 if (j
< 0 || j
== 0 || j
>= total
)
629 for (k
= 0; k
< ls
->div
->n_row
; ++k
) {
630 if (isl_int_is_zero(ls
->div
->row
[k
][1 + j
]))
632 isl_seq_elim(ls
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
633 &ls
->div
->row
[k
][0]);
634 normalize_div(ls
, k
);
638 isl_basic_set_free(eq
);
641 isl_basic_set_free(eq
);
642 isl_local_space_free(ls
);
646 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
649 * Let i be the dimension to replace and let "subs" be of the form
653 * Any integer division with a non-zero coefficient for i,
659 * floor((a f + d g)/(m d))
661 __isl_give isl_local_space
*isl_local_space_substitute(
662 __isl_take isl_local_space
*ls
,
663 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
668 ls
= isl_local_space_cow(ls
);
670 return isl_local_space_free(ls
);
672 if (!isl_space_is_equal(ls
->dim
, subs
->ls
->dim
))
673 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
674 "spaces don't match", return isl_local_space_free(ls
));
675 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
676 isl_die(isl_local_space_get_ctx(ls
), isl_error_unsupported
,
677 "cannot handle divs yet",
678 return isl_local_space_free(ls
));
680 pos
+= isl_local_space_offset(ls
, type
);
683 for (i
= 0; i
< ls
->div
->n_row
; ++i
) {
684 if (isl_int_is_zero(ls
->div
->row
[i
][1 + pos
]))
686 isl_int_set(v
, ls
->div
->row
[i
][1 + pos
]);
687 isl_int_set_si(ls
->div
->row
[i
][1 + pos
], 0);
688 isl_seq_combine(ls
->div
->row
[i
] + 1,
689 subs
->v
->el
[0], ls
->div
->row
[i
] + 1,
690 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
691 isl_int_mul(ls
->div
->row
[i
][0],
692 ls
->div
->row
[i
][0], subs
->v
->el
[0]);
693 normalize_div(ls
, i
);
700 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space
*ls
,
701 enum isl_dim_type type
)
705 return isl_space_is_named_or_nested(ls
->dim
, type
);
708 __isl_give isl_local_space
*isl_local_space_drop_dims(
709 __isl_take isl_local_space
*ls
,
710 enum isl_dim_type type
, unsigned first
, unsigned n
)
716 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
719 ctx
= isl_local_space_get_ctx(ls
);
720 if (first
+ n
> isl_local_space_dim(ls
, type
))
721 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
722 return isl_local_space_free(ls
));
724 ls
= isl_local_space_cow(ls
);
728 if (type
== isl_dim_div
) {
729 ls
->div
= isl_mat_drop_rows(ls
->div
, first
, n
);
731 ls
->dim
= isl_space_drop_dims(ls
->dim
, type
, first
, n
);
733 return isl_local_space_free(ls
);
736 first
+= 1 + isl_local_space_offset(ls
, type
);
737 ls
->div
= isl_mat_drop_cols(ls
->div
, first
, n
);
739 return isl_local_space_free(ls
);
744 __isl_give isl_local_space
*isl_local_space_insert_dims(
745 __isl_take isl_local_space
*ls
,
746 enum isl_dim_type type
, unsigned first
, unsigned n
)
752 if (n
== 0 && !isl_local_space_is_named_or_nested(ls
, type
))
755 ctx
= isl_local_space_get_ctx(ls
);
756 if (first
> isl_local_space_dim(ls
, type
))
757 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
758 return isl_local_space_free(ls
));
760 ls
= isl_local_space_cow(ls
);
764 if (type
== isl_dim_div
) {
765 ls
->div
= isl_mat_insert_zero_rows(ls
->div
, first
, n
);
767 ls
->dim
= isl_space_insert_dims(ls
->dim
, type
, first
, n
);
769 return isl_local_space_free(ls
);
772 first
+= 1 + isl_local_space_offset(ls
, type
);
773 ls
->div
= isl_mat_insert_zero_cols(ls
->div
, first
, n
);
775 return isl_local_space_free(ls
);
780 /* Check if the constraints pointed to by "constraint" is a div
781 * constraint corresponding to div "div" in "ls".
783 * That is, if div = floor(f/m), then check if the constraint is
787 * -(f-(m-1)) + m d >= 0
789 int isl_local_space_is_div_constraint(__isl_keep isl_local_space
*ls
,
790 isl_int
*constraint
, unsigned div
)
797 if (isl_int_is_zero(ls
->div
->row
[div
][0]))
800 pos
= isl_local_space_offset(ls
, isl_dim_div
) + div
;
802 if (isl_int_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
804 isl_int_sub(ls
->div
->row
[div
][1],
805 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
806 isl_int_add_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
807 neg
= isl_seq_is_neg(constraint
, ls
->div
->row
[div
]+1, pos
);
808 isl_int_sub_ui(ls
->div
->row
[div
][1], ls
->div
->row
[div
][1], 1);
809 isl_int_add(ls
->div
->row
[div
][1],
810 ls
->div
->row
[div
][1], ls
->div
->row
[div
][0]);
813 if (isl_seq_first_non_zero(constraint
+pos
+1,
814 ls
->div
->n_row
-div
-1) != -1)
816 } else if (isl_int_abs_eq(constraint
[pos
], ls
->div
->row
[div
][0])) {
817 if (!isl_seq_eq(constraint
, ls
->div
->row
[div
]+1, pos
))
819 if (isl_seq_first_non_zero(constraint
+pos
+1,
820 ls
->div
->n_row
-div
-1) != -1)
829 * Set active[i] to 1 if the dimension at position i is involved
830 * in the linear expression l.
832 int *isl_local_space_get_active(__isl_keep isl_local_space
*ls
, isl_int
*l
)
840 ctx
= isl_local_space_get_ctx(ls
);
841 total
= isl_local_space_dim(ls
, isl_dim_all
);
842 active
= isl_calloc_array(ctx
, int, total
);
846 for (i
= 0; i
< total
; ++i
)
847 active
[i
] = !isl_int_is_zero(l
[i
]);
849 offset
= isl_local_space_offset(ls
, isl_dim_div
) - 1;
850 for (i
= ls
->div
->n_row
- 1; i
>= 0; --i
) {
851 if (!active
[offset
+ i
])
853 for (j
= 0; j
< total
; ++j
)
854 active
[j
] |= !isl_int_is_zero(ls
->div
->row
[i
][2 + j
]);
860 /* Given a local space "ls" of a set, create a local space
861 * for the lift of the set. In particular, the result
862 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
863 * range of the wrapped map.
865 __isl_give isl_local_space
*isl_local_space_lift(
866 __isl_take isl_local_space
*ls
)
868 ls
= isl_local_space_cow(ls
);
872 ls
->dim
= isl_space_lift(ls
->dim
, ls
->div
->n_row
);
873 ls
->div
= isl_mat_drop_rows(ls
->div
, 0, ls
->div
->n_row
);
874 if (!ls
->dim
|| !ls
->div
)
875 return isl_local_space_free(ls
);
880 /* Construct a basic map that maps a set living in local space "ls"
881 * to the corresponding lifted local space.
883 __isl_give isl_basic_map
*isl_local_space_lifting(
884 __isl_take isl_local_space
*ls
)
886 isl_basic_map
*lifting
;
891 if (!isl_local_space_is_set(ls
))
892 isl_die(isl_local_space_get_ctx(ls
), isl_error_invalid
,
893 "lifting only defined on set spaces",
894 return isl_local_space_free(ls
));
896 bset
= isl_basic_set_from_local_space(ls
);
897 lifting
= isl_basic_set_unwrap(isl_basic_set_lift(bset
));
898 lifting
= isl_basic_map_domain_map(lifting
);
899 lifting
= isl_basic_map_reverse(lifting
);