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)
416 unsigned d = div1->n_col - div1->n_row;
418 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
419 d + div1->n_row + div2->n_row);
423 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
426 expand_row(div, k, div1, i, exp1);
427 expand_row(div, k + 1, div2, j, exp2);
429 cmp = cmp_row(div, k, k + 1);
433 } else if (cmp < 0) {
437 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
440 for (; i < div1->n_row; ++i, ++k) {
441 expand_row(div, k, div1, i, exp1);
444 for (; j < div2->n_row; ++j, ++k) {
445 expand_row(div, k, div2, j, exp2);
455 /* Construct a local space that contains all the divs in either
458 __isl_give isl_local_space *isl_local_space_intersect(
459 __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
469 ctx = isl_local_space_get_ctx(ls1);
470 if (!isl_space_is_equal(ls1->dim, ls2->dim))
471 isl_die(ctx, isl_error_invalid,
472 "spaces should be identical", goto error);
474 if (ls2->div->n_row == 0) {
475 isl_local_space_free(ls2);
479 if (ls1->div->n_row == 0) {
480 isl_local_space_free(ls1);
484 exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
485 exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
489 div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
495 isl_local_space_free(ls2);
496 isl_mat_free(ls1->div);
503 isl_local_space_free(ls1);
504 isl_local_space_free(ls2);
508 int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
515 for (i = 0; i < ls->div->n_row; ++i)
516 if (isl_int_is_zero(ls->div->row[i][0]))
522 __isl_give isl_local_space *isl_local_space_domain(
523 __isl_take isl_local_space *ls)
525 ls = isl_local_space_drop_dims(ls, isl_dim_out,
526 0, isl_local_space_dim(ls, isl_dim_out));
527 ls = isl_local_space_cow(ls);
530 ls->dim = isl_space_domain(ls->dim);
532 return isl_local_space_free(ls);
536 __isl_give isl_local_space *isl_local_space_range(
537 __isl_take isl_local_space *ls)
539 ls = isl_local_space_drop_dims(ls, isl_dim_in,
540 0, isl_local_space_dim(ls, isl_dim_in));
541 ls = isl_local_space_cow(ls);
545 ls->dim = isl_space_range(ls->dim);
547 return isl_local_space_free(ls);
551 /* Construct a local space for a map that has the given local
552 * space as domain and that has a zero-dimensional range.
554 __isl_give isl_local_space *isl_local_space_from_domain(
555 __isl_take isl_local_space *ls)
557 ls = isl_local_space_cow(ls);
560 ls->dim = isl_space_from_domain(ls->dim);
562 return isl_local_space_free(ls);
566 __isl_give isl_local_space *isl_local_space_add_dims(
567 __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
573 pos = isl_local_space_dim(ls, type);
574 return isl_local_space_insert_dims(ls, type, pos, n);
577 /* Remove common factor of non-constant terms and denominator.
579 static void normalize_div(__isl_keep isl_local_space *ls, int div)
581 isl_ctx *ctx = ls->div->ctx;
582 unsigned total = ls->div->n_col - 2;
584 isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
585 isl_int_gcd(ctx->normalize_gcd,
586 ctx->normalize_gcd, ls->div->row[div][0]);
587 if (isl_int_is_one(ctx->normalize_gcd))
590 isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
591 ctx->normalize_gcd, total);
592 isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
594 isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
598 /* Exploit the equalities in "eq" to simplify the expressions of
599 * the integer divisions in "ls".
600 * The integer divisions in "ls" are assumed to appear as regular
601 * dimensions in "eq".
603 __isl_give isl_local_space *isl_local_space_substitute_equalities(
604 __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
610 ls = isl_local_space_cow(ls);
614 total = isl_space_dim(eq->dim, isl_dim_all);
615 if (isl_local_space_dim(ls, isl_dim_all) != total)
616 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
617 "dimensions don't match", goto error);
620 for (i = 0; i < eq->n_eq; ++i) {
621 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
622 if (j < 0 || j == 0 || j >= total)
625 for (k = 0; k < ls->div->n_row; ++k) {
626 if (isl_int_is_zero(ls->div->row[k][1 + j]))
628 isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
629 &ls->div->row[k][0]);
630 normalize_div(ls, k);
634 isl_basic_set_free(eq);
637 isl_basic_set_free(eq);
638 isl_local_space_free(ls);
642 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
645 * Let i be the dimension to replace and let "subs" be of the form
649 * Any integer division with a non-zero coefficient for i,
655 * floor((a f + d g)/(m d))
657 __isl_give isl_local_space *isl_local_space_substitute(
658 __isl_take isl_local_space *ls,
659 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
664 ls = isl_local_space_cow(ls);
666 return isl_local_space_free(ls);
668 if (!isl_space_is_equal(ls->dim, subs->ls->dim))
669 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
670 "spaces don't match", return isl_local_space_free(ls));
671 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
672 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
673 "cannot handle divs yet",
674 return isl_local_space_free(ls));
676 pos += isl_local_space_offset(ls, type);
679 for (i = 0; i < ls->div->n_row; ++i) {
680 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
682 isl_int_set(v, ls->div->row[i][1 + pos]);
683 isl_int_set_si(ls->div->row[i][1 + pos], 0);
684 isl_seq_combine(ls->div->row[i] + 1,
685 subs->v->el[0], ls->div->row[i] + 1,
686 v, subs->v->el + 1, subs->v->size - 1);
687 isl_int_mul(ls->div->row[i][0],
688 ls->div->row[i][0], subs->v->el[0]);
689 normalize_div(ls, i);
696 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
697 enum isl_dim_type type)
701 return isl_space_is_named_or_nested(ls->dim, type);
704 __isl_give isl_local_space *isl_local_space_drop_dims(
705 __isl_take isl_local_space *ls,
706 enum isl_dim_type type, unsigned first, unsigned n)
712 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
715 ctx = isl_local_space_get_ctx(ls);
716 if (first + n > isl_local_space_dim(ls, type))
717 isl_die(ctx, isl_error_invalid, "range out of bounds",
718 return isl_local_space_free(ls));
720 ls = isl_local_space_cow(ls);
724 if (type == isl_dim_div) {
725 ls->div = isl_mat_drop_rows(ls->div, first, n);
727 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
729 return isl_local_space_free(ls);
732 first += 1 + isl_local_space_offset(ls, type);
733 ls->div = isl_mat_drop_cols(ls->div, first, n);
735 return isl_local_space_free(ls);
740 __isl_give isl_local_space *isl_local_space_insert_dims(
741 __isl_take isl_local_space *ls,
742 enum isl_dim_type type, unsigned first, unsigned n)
748 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
751 ctx = isl_local_space_get_ctx(ls);
752 if (first > isl_local_space_dim(ls, type))
753 isl_die(ctx, isl_error_invalid, "position out of bounds",
754 return isl_local_space_free(ls));
756 ls = isl_local_space_cow(ls);
760 if (type == isl_dim_div) {
761 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
763 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
765 return isl_local_space_free(ls);
768 first += 1 + isl_local_space_offset(ls, type);
769 ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
771 return isl_local_space_free(ls);
776 /* Check if the constraints pointed to by "constraint" is a div
777 * constraint corresponding to div "div" in "ls".
779 * That is, if div = floor(f/m), then check if the constraint is
783 * -(f-(m-1)) + m d >= 0
785 int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
786 isl_int *constraint, unsigned div)
793 if (isl_int_is_zero(ls->div->row[div][0]))
796 pos = isl_local_space_offset(ls, isl_dim_div) + div;
798 if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
800 isl_int_sub(ls->div->row[div][1],
801 ls->div->row[div][1], ls->div->row[div][0]);
802 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
803 neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
804 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
805 isl_int_add(ls->div->row[div][1],
806 ls->div->row[div][1], ls->div->row[div][0]);
809 if (isl_seq_first_non_zero(constraint+pos+1,
810 ls->div->n_row-div-1) != -1)
812 } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
813 if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
815 if (isl_seq_first_non_zero(constraint+pos+1,
816 ls->div->n_row-div-1) != -1)
825 * Set active[i] to 1 if the dimension at position i is involved
826 * in the linear expression l.
828 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
836 ctx = isl_local_space_get_ctx(ls);
837 total = isl_local_space_dim(ls, isl_dim_all);
838 active = isl_calloc_array(ctx, int, total);
842 for (i = 0; i < total; ++i)
843 active[i] = !isl_int_is_zero(l[i]);
845 offset = isl_local_space_offset(ls, isl_dim_div) - 1;
846 for (i = ls->div->n_row - 1; i >= 0; --i) {
847 if (!active[offset + i])
849 for (j = 0; j < total; ++j)
850 active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
856 /* Given a local space "ls" of a set, create a local space
857 * for the lift of the set. In particular, the result
858 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
859 * range of the wrapped map.
861 __isl_give isl_local_space *isl_local_space_lift(
862 __isl_take isl_local_space *ls)
864 ls = isl_local_space_cow(ls);
868 ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
869 ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
870 if (!ls->dim || !ls->div)
871 return isl_local_space_free(ls);
876 /* Construct a basic map that maps a set living in local space "ls"
877 * to the corresponding lifted local space.
879 __isl_give isl_basic_map *isl_local_space_lifting(
880 __isl_take isl_local_space *ls)
882 isl_basic_map *lifting;
887 if (!isl_local_space_is_set(ls))
888 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
889 "lifting only defined on set spaces",
890 return isl_local_space_free(ls));
892 bset = isl_basic_set_from_local_space(ls);
893 lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
894 lifting = isl_basic_map_domain_map(lifting);
895 lifting = isl_basic_map_reverse(lifting);