isl 0.12
[isl.git] / isl_local_space.c
blob4577cde5124213d60f07fdf09c0bc97233c4b498
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012 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,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_local_space_private.h>
16 #include <isl_space_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_aff_private.h>
19 #include <isl/seq.h>
21 isl_ctx *isl_local_space_get_ctx(__isl_keep isl_local_space *ls)
23 return ls ? ls->dim->ctx : NULL;
26 __isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
27 __isl_take isl_mat *div)
29 isl_ctx *ctx;
30 isl_local_space *ls = NULL;
32 if (!dim || !div)
33 goto error;
35 ctx = isl_space_get_ctx(dim);
36 ls = isl_calloc_type(ctx, struct isl_local_space);
37 if (!ls)
38 goto error;
40 ls->ref = 1;
41 ls->dim = dim;
42 ls->div = div;
44 return ls;
45 error:
46 isl_mat_free(div);
47 isl_space_free(dim);
48 isl_local_space_free(ls);
49 return NULL;
52 __isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *dim,
53 unsigned n_div)
55 isl_ctx *ctx;
56 isl_mat *div;
57 unsigned total;
59 if (!dim)
60 return NULL;
62 total = isl_space_dim(dim, isl_dim_all);
64 ctx = isl_space_get_ctx(dim);
65 div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
66 return isl_local_space_alloc_div(dim, div);
69 __isl_give isl_local_space *isl_local_space_from_space(__isl_take isl_space *dim)
71 return isl_local_space_alloc(dim, 0);
74 __isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
76 if (!ls)
77 return NULL;
79 ls->ref++;
80 return ls;
83 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
85 if (!ls)
86 return NULL;
88 return isl_local_space_alloc_div(isl_space_copy(ls->dim),
89 isl_mat_copy(ls->div));
93 __isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
95 if (!ls)
96 return NULL;
98 if (ls->ref == 1)
99 return ls;
100 ls->ref--;
101 return isl_local_space_dup(ls);
104 void *isl_local_space_free(__isl_take isl_local_space *ls)
106 if (!ls)
107 return NULL;
109 if (--ls->ref > 0)
110 return NULL;
112 isl_space_free(ls->dim);
113 isl_mat_free(ls->div);
115 free(ls);
117 return NULL;
120 /* Is the local space that of a set?
122 int isl_local_space_is_set(__isl_keep isl_local_space *ls)
124 return ls ? isl_space_is_set(ls->dim) : -1;
127 /* Return true if the two local spaces are identical, with identical
128 * expressions for the integer divisions.
130 int isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
131 __isl_keep isl_local_space *ls2)
133 int equal;
135 if (!ls1 || !ls2)
136 return -1;
138 equal = isl_space_is_equal(ls1->dim, ls2->dim);
139 if (equal < 0 || !equal)
140 return equal;
142 if (!isl_local_space_divs_known(ls1))
143 return 0;
144 if (!isl_local_space_divs_known(ls2))
145 return 0;
147 return isl_mat_is_equal(ls1->div, ls2->div);
150 int isl_local_space_dim(__isl_keep isl_local_space *ls,
151 enum isl_dim_type type)
153 if (!ls)
154 return 0;
155 if (type == isl_dim_div)
156 return ls->div->n_row;
157 if (type == isl_dim_all)
158 return isl_space_dim(ls->dim, isl_dim_all) + ls->div->n_row;
159 return isl_space_dim(ls->dim, type);
162 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
163 enum isl_dim_type type)
165 isl_space *dim;
167 if (!ls)
168 return 0;
170 dim = ls->dim;
171 switch (type) {
172 case isl_dim_cst: return 0;
173 case isl_dim_param: return 1;
174 case isl_dim_in: return 1 + dim->nparam;
175 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
176 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
177 default: return 0;
181 /* Does the given dimension have a name?
183 int isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
184 enum isl_dim_type type, unsigned pos)
186 return ls ? isl_space_has_dim_name(ls->dim, type, pos) : -1;
189 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
190 enum isl_dim_type type, unsigned pos)
192 return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
195 int isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
196 enum isl_dim_type type, unsigned pos)
198 return ls ? isl_space_has_dim_id(ls->dim, type, pos) : -1;
201 __isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
202 enum isl_dim_type type, unsigned pos)
204 return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
207 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
208 int pos)
210 isl_aff *aff;
212 if (!ls)
213 return NULL;
215 if (pos < 0 || pos >= ls->div->n_row)
216 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
217 "index out of bounds", return NULL);
219 if (isl_int_is_zero(ls->div->row[pos][0]))
220 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
221 "expression of div unknown", return NULL);
222 if (!isl_local_space_is_set(ls))
223 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
224 "cannot represent divs of map spaces", return NULL);
226 aff = isl_aff_alloc(isl_local_space_copy(ls));
227 if (!aff)
228 return NULL;
229 isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
230 return aff;
233 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
235 if (!ls)
236 return NULL;
238 return isl_space_copy(ls->dim);
241 __isl_give isl_local_space *isl_local_space_set_dim_name(
242 __isl_take isl_local_space *ls,
243 enum isl_dim_type type, unsigned pos, const char *s)
245 ls = isl_local_space_cow(ls);
246 if (!ls)
247 return NULL;
248 ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
249 if (!ls->dim)
250 return isl_local_space_free(ls);
252 return ls;
255 __isl_give isl_local_space *isl_local_space_set_dim_id(
256 __isl_take isl_local_space *ls,
257 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
259 ls = isl_local_space_cow(ls);
260 if (!ls)
261 return isl_id_free(id);
262 ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
263 if (!ls->dim)
264 return isl_local_space_free(ls);
266 return ls;
269 __isl_give isl_local_space *isl_local_space_reset_space(
270 __isl_take isl_local_space *ls, __isl_take isl_space *dim)
272 ls = isl_local_space_cow(ls);
273 if (!ls || !dim)
274 goto error;
276 isl_space_free(ls->dim);
277 ls->dim = dim;
279 return ls;
280 error:
281 isl_local_space_free(ls);
282 isl_space_free(dim);
283 return NULL;
286 /* Reorder the columns of the given div definitions according to the
287 * given reordering.
288 * The order of the divs themselves is assumed not to change.
290 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
291 __isl_take isl_reordering *r)
293 int i, j;
294 isl_mat *mat;
295 int extra;
297 if (!div || !r)
298 goto error;
300 extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
301 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
302 if (!mat)
303 goto error;
305 for (i = 0; i < div->n_row; ++i) {
306 isl_seq_cpy(mat->row[i], div->row[i], 2);
307 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
308 for (j = 0; j < r->len; ++j)
309 isl_int_set(mat->row[i][2 + r->pos[j]],
310 div->row[i][2 + j]);
313 isl_reordering_free(r);
314 isl_mat_free(div);
315 return mat;
316 error:
317 isl_reordering_free(r);
318 isl_mat_free(div);
319 return NULL;
322 /* Reorder the dimensions of "ls" according to the given reordering.
323 * The reordering r is assumed to have been extended with the local
324 * variables, leaving them in the same order.
326 __isl_give isl_local_space *isl_local_space_realign(
327 __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
329 ls = isl_local_space_cow(ls);
330 if (!ls || !r)
331 goto error;
333 ls->div = reorder_divs(ls->div, isl_reordering_copy(r));
334 if (!ls->div)
335 goto error;
337 ls = isl_local_space_reset_space(ls, isl_space_copy(r->dim));
339 isl_reordering_free(r);
340 return ls;
341 error:
342 isl_local_space_free(ls);
343 isl_reordering_free(r);
344 return NULL;
347 __isl_give isl_local_space *isl_local_space_add_div(
348 __isl_take isl_local_space *ls, __isl_take isl_vec *div)
350 ls = isl_local_space_cow(ls);
351 if (!ls || !div)
352 goto error;
354 if (ls->div->n_col != div->size)
355 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
356 "incompatible dimensions", goto error);
358 ls->div = isl_mat_add_zero_cols(ls->div, 1);
359 ls->div = isl_mat_add_rows(ls->div, 1);
360 if (!ls->div)
361 goto error;
363 isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
364 isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
366 isl_vec_free(div);
367 return ls;
368 error:
369 isl_local_space_free(ls);
370 isl_vec_free(div);
371 return NULL;
374 __isl_give isl_local_space *isl_local_space_replace_divs(
375 __isl_take isl_local_space *ls, __isl_take isl_mat *div)
377 ls = isl_local_space_cow(ls);
379 if (!ls || !div)
380 goto error;
382 isl_mat_free(ls->div);
383 ls->div = div;
384 return ls;
385 error:
386 isl_mat_free(div);
387 isl_local_space_free(ls);
388 return NULL;
391 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
392 * defined by "exp".
394 static void expand_row(__isl_keep isl_mat *dst, int d,
395 __isl_keep isl_mat *src, int s, int *exp)
397 int i;
398 unsigned c = src->n_col - src->n_row;
400 isl_seq_cpy(dst->row[d], src->row[s], c);
401 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
403 for (i = 0; i < s; ++i)
404 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
407 /* Compare (known) divs.
408 * Return non-zero if at least one of the two divs is unknown.
409 * In particular, if both divs are unknown, we respect their
410 * current order. Otherwise, we sort the known div after the unknown
411 * div only if the known div depends on the unknown div.
413 static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
414 unsigned n_row, unsigned n_col)
416 int li, lj;
417 int unknown_i, unknown_j;
419 unknown_i = isl_int_is_zero(row_i[0]);
420 unknown_j = isl_int_is_zero(row_j[0]);
422 if (unknown_i && unknown_j)
423 return i - j;
425 if (unknown_i)
426 li = n_col - n_row + i;
427 else
428 li = isl_seq_last_non_zero(row_i, n_col);
429 if (unknown_j)
430 lj = n_col - n_row + j;
431 else
432 lj = isl_seq_last_non_zero(row_j, n_col);
434 if (li != lj)
435 return li - lj;
437 return isl_seq_cmp(row_i, row_j, n_col);
440 /* Call cmp_row for divs in a matrix.
442 int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
444 return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
447 /* Call cmp_row for divs in a basic map.
449 static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
450 unsigned total)
452 return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
455 /* Sort the divs in "bmap".
457 * We first make sure divs are placed after divs on which they depend.
458 * Then we perform a simple insertion sort based on the same ordering
459 * that is used in isl_merge_divs.
461 __isl_give isl_basic_map *isl_basic_map_sort_divs(
462 __isl_take isl_basic_map *bmap)
464 int i, j;
465 unsigned total;
467 bmap = isl_basic_map_order_divs(bmap);
468 if (!bmap)
469 return NULL;
470 if (bmap->n_div <= 1)
471 return bmap;
473 total = 2 + isl_basic_map_total_dim(bmap);
474 for (i = 1; i < bmap->n_div; ++i) {
475 for (j = i - 1; j >= 0; --j) {
476 if (bmap_cmp_row(bmap, j, j + 1, total) <= 0)
477 break;
478 isl_basic_map_swap_div(bmap, j, j + 1);
482 return bmap;
485 /* Sort the divs in the basic maps of "map".
487 __isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
489 return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
492 /* Combine the two lists of divs into a single list.
493 * For each row i in div1, exp1[i] is set to the position of the corresponding
494 * row in the result. Similarly for div2 and exp2.
495 * This function guarantees
496 * exp1[i] >= i
497 * exp1[i+1] > exp1[i]
498 * For optimal merging, the two input list should have been sorted.
500 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
501 __isl_keep isl_mat *div2, int *exp1, int *exp2)
503 int i, j, k;
504 isl_mat *div = NULL;
505 unsigned d;
507 if (!div1 || !div2)
508 return NULL;
510 d = div1->n_col - div1->n_row;
511 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
512 d + div1->n_row + div2->n_row);
513 if (!div)
514 return NULL;
516 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
517 int cmp;
519 expand_row(div, k, div1, i, exp1);
520 expand_row(div, k + 1, div2, j, exp2);
522 cmp = isl_mat_cmp_div(div, k, k + 1);
523 if (cmp == 0) {
524 exp1[i++] = k;
525 exp2[j++] = k;
526 } else if (cmp < 0) {
527 exp1[i++] = k;
528 } else {
529 exp2[j++] = k;
530 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
533 for (; i < div1->n_row; ++i, ++k) {
534 expand_row(div, k, div1, i, exp1);
535 exp1[i] = k;
537 for (; j < div2->n_row; ++j, ++k) {
538 expand_row(div, k, div2, j, exp2);
539 exp2[j] = k;
542 div->n_row = k;
543 div->n_col = d + k;
545 return div;
548 /* Swap divs "a" and "b" in "ls".
550 __isl_give isl_local_space *isl_local_space_swap_div(
551 __isl_take isl_local_space *ls, int a, int b)
553 int offset;
555 ls = isl_local_space_cow(ls);
556 if (!ls)
557 return NULL;
558 if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
559 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
560 "index out of bounds", return isl_local_space_free(ls));
561 offset = ls->div->n_col - ls->div->n_row;
562 ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
563 ls->div = isl_mat_swap_rows(ls->div, a, b);
564 if (!ls->div)
565 return isl_local_space_free(ls);
566 return ls;
569 /* Construct a local space that contains all the divs in either
570 * "ls1" or "ls2".
572 __isl_give isl_local_space *isl_local_space_intersect(
573 __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
575 isl_ctx *ctx;
576 int *exp1 = NULL;
577 int *exp2 = NULL;
578 isl_mat *div;
580 if (!ls1 || !ls2)
581 goto error;
583 ctx = isl_local_space_get_ctx(ls1);
584 if (!isl_space_is_equal(ls1->dim, ls2->dim))
585 isl_die(ctx, isl_error_invalid,
586 "spaces should be identical", goto error);
588 if (ls2->div->n_row == 0) {
589 isl_local_space_free(ls2);
590 return ls1;
593 if (ls1->div->n_row == 0) {
594 isl_local_space_free(ls1);
595 return ls2;
598 exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
599 exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
600 if (!exp1 || !exp2)
601 goto error;
603 div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
604 if (!div)
605 goto error;
607 free(exp1);
608 free(exp2);
609 isl_local_space_free(ls2);
610 isl_mat_free(ls1->div);
611 ls1->div = div;
613 return ls1;
614 error:
615 free(exp1);
616 free(exp2);
617 isl_local_space_free(ls1);
618 isl_local_space_free(ls2);
619 return NULL;
622 int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
624 int i;
626 if (!ls)
627 return -1;
629 for (i = 0; i < ls->div->n_row; ++i)
630 if (isl_int_is_zero(ls->div->row[i][0]))
631 return 0;
633 return 1;
636 __isl_give isl_local_space *isl_local_space_domain(
637 __isl_take isl_local_space *ls)
639 ls = isl_local_space_drop_dims(ls, isl_dim_out,
640 0, isl_local_space_dim(ls, isl_dim_out));
641 ls = isl_local_space_cow(ls);
642 if (!ls)
643 return NULL;
644 ls->dim = isl_space_domain(ls->dim);
645 if (!ls->dim)
646 return isl_local_space_free(ls);
647 return ls;
650 __isl_give isl_local_space *isl_local_space_range(
651 __isl_take isl_local_space *ls)
653 ls = isl_local_space_drop_dims(ls, isl_dim_in,
654 0, isl_local_space_dim(ls, isl_dim_in));
655 ls = isl_local_space_cow(ls);
656 if (!ls)
657 return NULL;
659 ls->dim = isl_space_range(ls->dim);
660 if (!ls->dim)
661 return isl_local_space_free(ls);
662 return ls;
665 /* Construct a local space for a map that has the given local
666 * space as domain and that has a zero-dimensional range.
668 __isl_give isl_local_space *isl_local_space_from_domain(
669 __isl_take isl_local_space *ls)
671 ls = isl_local_space_cow(ls);
672 if (!ls)
673 return NULL;
674 ls->dim = isl_space_from_domain(ls->dim);
675 if (!ls->dim)
676 return isl_local_space_free(ls);
677 return ls;
680 __isl_give isl_local_space *isl_local_space_add_dims(
681 __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
683 int pos;
685 if (!ls)
686 return NULL;
687 pos = isl_local_space_dim(ls, type);
688 return isl_local_space_insert_dims(ls, type, pos, n);
691 /* Remove common factor of non-constant terms and denominator.
693 static void normalize_div(__isl_keep isl_local_space *ls, int div)
695 isl_ctx *ctx = ls->div->ctx;
696 unsigned total = ls->div->n_col - 2;
698 isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
699 isl_int_gcd(ctx->normalize_gcd,
700 ctx->normalize_gcd, ls->div->row[div][0]);
701 if (isl_int_is_one(ctx->normalize_gcd))
702 return;
704 isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
705 ctx->normalize_gcd, total);
706 isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
707 ctx->normalize_gcd);
708 isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
709 ctx->normalize_gcd);
712 /* Exploit the equalities in "eq" to simplify the expressions of
713 * the integer divisions in "ls".
714 * The integer divisions in "ls" are assumed to appear as regular
715 * dimensions in "eq".
717 __isl_give isl_local_space *isl_local_space_substitute_equalities(
718 __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
720 int i, j, k;
721 unsigned total;
722 unsigned n_div;
724 if (!ls || !eq)
725 goto error;
727 total = isl_space_dim(eq->dim, isl_dim_all);
728 if (isl_local_space_dim(ls, isl_dim_all) != total)
729 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
730 "spaces don't match", goto error);
731 total++;
732 n_div = eq->n_div;
733 for (i = 0; i < eq->n_eq; ++i) {
734 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
735 if (j < 0 || j == 0 || j >= total)
736 continue;
738 for (k = 0; k < ls->div->n_row; ++k) {
739 if (isl_int_is_zero(ls->div->row[k][1 + j]))
740 continue;
741 ls = isl_local_space_cow(ls);
742 if (!ls)
743 goto error;
744 ls->div = isl_mat_cow(ls->div);
745 if (!ls->div)
746 goto error;
747 isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
748 &ls->div->row[k][0]);
749 normalize_div(ls, k);
753 isl_basic_set_free(eq);
754 return ls;
755 error:
756 isl_basic_set_free(eq);
757 isl_local_space_free(ls);
758 return NULL;
761 /* Plug in the affine expressions "subs" of length "subs_len" (including
762 * the denominator and the constant term) into the variable at position "pos"
763 * of the "n" div expressions starting at "first".
765 * Let i be the dimension to replace and let "subs" be of the form
767 * f/d
769 * Any integer division starting at "first" with a non-zero coefficient for i,
771 * floor((a i + g)/m)
773 * is replaced by
775 * floor((a f + d g)/(m d))
777 __isl_give isl_local_space *isl_local_space_substitute_seq(
778 __isl_take isl_local_space *ls,
779 enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
780 int first, int n)
782 int i;
783 isl_int v;
785 if (n == 0)
786 return ls;
787 ls = isl_local_space_cow(ls);
788 if (!ls)
789 return NULL;
790 ls->div = isl_mat_cow(ls->div);
791 if (!ls->div)
792 return isl_local_space_free(ls);
794 if (first + n > ls->div->n_row)
795 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
796 "index out of bounds", return isl_local_space_free(ls));
798 pos += isl_local_space_offset(ls, type);
800 isl_int_init(v);
801 for (i = first; i < ls->div->n_row; ++i) {
802 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
803 continue;
804 isl_seq_substitute(ls->div->row[i], pos, subs,
805 ls->div->n_col, subs_len, v);
806 normalize_div(ls, i);
808 isl_int_clear(v);
810 return ls;
813 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
814 * of "ls".
816 * Let i be the dimension to replace and let "subs" be of the form
818 * f/d
820 * Any integer division with a non-zero coefficient for i,
822 * floor((a i + g)/m)
824 * is replaced by
826 * floor((a f + d g)/(m d))
828 __isl_give isl_local_space *isl_local_space_substitute(
829 __isl_take isl_local_space *ls,
830 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
832 ls = isl_local_space_cow(ls);
833 if (!ls || !subs)
834 return isl_local_space_free(ls);
836 if (!isl_space_is_equal(ls->dim, subs->ls->dim))
837 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
838 "spaces don't match", return isl_local_space_free(ls));
839 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
840 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
841 "cannot handle divs yet",
842 return isl_local_space_free(ls));
844 return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
845 subs->v->size, 0, ls->div->n_row);
848 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
849 enum isl_dim_type type)
851 if (!ls)
852 return -1;
853 return isl_space_is_named_or_nested(ls->dim, type);
856 __isl_give isl_local_space *isl_local_space_drop_dims(
857 __isl_take isl_local_space *ls,
858 enum isl_dim_type type, unsigned first, unsigned n)
860 isl_ctx *ctx;
862 if (!ls)
863 return NULL;
864 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
865 return ls;
867 ctx = isl_local_space_get_ctx(ls);
868 if (first + n > isl_local_space_dim(ls, type))
869 isl_die(ctx, isl_error_invalid, "range out of bounds",
870 return isl_local_space_free(ls));
872 ls = isl_local_space_cow(ls);
873 if (!ls)
874 return NULL;
876 if (type == isl_dim_div) {
877 ls->div = isl_mat_drop_rows(ls->div, first, n);
878 } else {
879 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
880 if (!ls->dim)
881 return isl_local_space_free(ls);
884 first += 1 + isl_local_space_offset(ls, type);
885 ls->div = isl_mat_drop_cols(ls->div, first, n);
886 if (!ls->div)
887 return isl_local_space_free(ls);
889 return ls;
892 __isl_give isl_local_space *isl_local_space_insert_dims(
893 __isl_take isl_local_space *ls,
894 enum isl_dim_type type, unsigned first, unsigned n)
896 isl_ctx *ctx;
898 if (!ls)
899 return NULL;
900 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
901 return ls;
903 ctx = isl_local_space_get_ctx(ls);
904 if (first > isl_local_space_dim(ls, type))
905 isl_die(ctx, isl_error_invalid, "position out of bounds",
906 return isl_local_space_free(ls));
908 ls = isl_local_space_cow(ls);
909 if (!ls)
910 return NULL;
912 if (type == isl_dim_div) {
913 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
914 } else {
915 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
916 if (!ls->dim)
917 return isl_local_space_free(ls);
920 first += 1 + isl_local_space_offset(ls, type);
921 ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
922 if (!ls->div)
923 return isl_local_space_free(ls);
925 return ls;
928 /* Check if the constraints pointed to by "constraint" is a div
929 * constraint corresponding to div "div" in "ls".
931 * That is, if div = floor(f/m), then check if the constraint is
933 * f - m d >= 0
934 * or
935 * -(f-(m-1)) + m d >= 0
937 int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
938 isl_int *constraint, unsigned div)
940 unsigned pos;
942 if (!ls)
943 return -1;
945 if (isl_int_is_zero(ls->div->row[div][0]))
946 return 0;
948 pos = isl_local_space_offset(ls, isl_dim_div) + div;
950 if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
951 int neg;
952 isl_int_sub(ls->div->row[div][1],
953 ls->div->row[div][1], ls->div->row[div][0]);
954 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
955 neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
956 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
957 isl_int_add(ls->div->row[div][1],
958 ls->div->row[div][1], ls->div->row[div][0]);
959 if (!neg)
960 return 0;
961 if (isl_seq_first_non_zero(constraint+pos+1,
962 ls->div->n_row-div-1) != -1)
963 return 0;
964 } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
965 if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
966 return 0;
967 if (isl_seq_first_non_zero(constraint+pos+1,
968 ls->div->n_row-div-1) != -1)
969 return 0;
970 } else
971 return 0;
973 return 1;
977 * Set active[i] to 1 if the dimension at position i is involved
978 * in the linear expression l.
980 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
982 int i, j;
983 isl_ctx *ctx;
984 int *active = NULL;
985 unsigned total;
986 unsigned offset;
988 ctx = isl_local_space_get_ctx(ls);
989 total = isl_local_space_dim(ls, isl_dim_all);
990 active = isl_calloc_array(ctx, int, total);
991 if (!active)
992 return NULL;
994 for (i = 0; i < total; ++i)
995 active[i] = !isl_int_is_zero(l[i]);
997 offset = isl_local_space_offset(ls, isl_dim_div) - 1;
998 for (i = ls->div->n_row - 1; i >= 0; --i) {
999 if (!active[offset + i])
1000 continue;
1001 for (j = 0; j < total; ++j)
1002 active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
1005 return active;
1008 /* Given a local space "ls" of a set, create a local space
1009 * for the lift of the set. In particular, the result
1010 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1011 * range of the wrapped map.
1013 __isl_give isl_local_space *isl_local_space_lift(
1014 __isl_take isl_local_space *ls)
1016 ls = isl_local_space_cow(ls);
1017 if (!ls)
1018 return NULL;
1020 ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1021 ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1022 if (!ls->dim || !ls->div)
1023 return isl_local_space_free(ls);
1025 return ls;
1028 /* Construct a basic map that maps a set living in local space "ls"
1029 * to the corresponding lifted local space.
1031 __isl_give isl_basic_map *isl_local_space_lifting(
1032 __isl_take isl_local_space *ls)
1034 isl_basic_map *lifting;
1035 isl_basic_set *bset;
1037 if (!ls)
1038 return NULL;
1039 if (!isl_local_space_is_set(ls))
1040 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1041 "lifting only defined on set spaces",
1042 return isl_local_space_free(ls));
1044 bset = isl_basic_set_from_local_space(ls);
1045 lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1046 lifting = isl_basic_map_domain_map(lifting);
1047 lifting = isl_basic_map_reverse(lifting);
1049 return lifting;
1052 /* Compute the preimage of "ls" under the function represented by "ma".
1053 * In other words, plug in "ma" in "ls". The result is a local space
1054 * that is part of the domain space of "ma".
1056 * If the divs in "ls" are represented as
1058 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1060 * and ma is represented by
1062 * x = D(p) + F(y) + G(divs')
1064 * then the resulting divs are
1066 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1068 * We first copy over the divs from "ma" and then
1069 * we add the modified divs from "ls".
1071 __isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1072 __isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1074 int i;
1075 isl_space *space;
1076 isl_local_space *res = NULL;
1077 int n_div_ls, n_div_ma;
1078 isl_int f, c1, c2, g;
1080 ma = isl_multi_aff_align_divs(ma);
1081 if (!ls || !ma)
1082 goto error;
1083 if (!isl_space_is_range_internal(ls->dim, ma->space))
1084 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1085 "spaces don't match", goto error);
1087 n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1088 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
1090 space = isl_space_domain(isl_multi_aff_get_space(ma));
1091 res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1092 if (!res)
1093 goto error;
1095 if (n_div_ma) {
1096 isl_mat_free(res->div);
1097 res->div = isl_mat_copy(ma->p[0]->ls->div);
1098 res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1099 res->div = isl_mat_add_rows(res->div, n_div_ls);
1100 if (!res->div)
1101 goto error;
1104 isl_int_init(f);
1105 isl_int_init(c1);
1106 isl_int_init(c2);
1107 isl_int_init(g);
1109 for (i = 0; i < ls->div->n_row; ++i) {
1110 if (isl_int_is_zero(ls->div->row[i][0])) {
1111 isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1112 continue;
1114 isl_seq_preimage(res->div->row[n_div_ma + i], ls->div->row[i],
1115 ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1);
1116 normalize_div(res, n_div_ma + i);
1119 isl_int_clear(f);
1120 isl_int_clear(c1);
1121 isl_int_clear(c2);
1122 isl_int_clear(g);
1124 isl_local_space_free(ls);
1125 isl_multi_aff_free(ma);
1126 return res;
1127 error:
1128 isl_local_space_free(ls);
1129 isl_multi_aff_free(ma);
1130 isl_local_space_free(res);
1131 return NULL;