isl_union_map.c:map_excludes: use isl_bool_not
[isl.git] / isl_local_space.c
blob6b19e923b79ca8ff041d1799be8f60cd44d4d49b
1 /*
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,
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/id.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>
22 #include <isl_seq.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;
36 if (!ls)
37 return 0;
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);
45 return hash;
48 __isl_give isl_local_space *isl_local_space_alloc_div(
49 __isl_take isl_space *space, __isl_take isl_mat *div)
51 isl_ctx *ctx;
52 isl_local_space *ls = NULL;
54 if (!space || !div)
55 goto error;
57 ctx = isl_space_get_ctx(space);
58 ls = isl_calloc_type(ctx, struct isl_local_space);
59 if (!ls)
60 goto error;
62 ls->ref = 1;
63 ls->dim = space;
64 ls->div = div;
66 return ls;
67 error:
68 isl_mat_free(div);
69 isl_space_free(space);
70 isl_local_space_free(ls);
71 return NULL;
74 __isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *space,
75 unsigned n_div)
77 isl_ctx *ctx;
78 isl_mat *div;
79 unsigned total;
81 if (!space)
82 return NULL;
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)
98 if (!ls)
99 return NULL;
101 ls->ref++;
102 return ls;
105 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
107 if (!ls)
108 return NULL;
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)
117 if (!ls)
118 return NULL;
120 if (ls->ref == 1)
121 return ls;
122 ls->ref--;
123 return isl_local_space_dup(ls);
126 __isl_null isl_local_space *isl_local_space_free(
127 __isl_take isl_local_space *ls)
129 if (!ls)
130 return NULL;
132 if (--ls->ref > 0)
133 return NULL;
135 isl_space_free(ls->dim);
136 isl_mat_free(ls->div);
138 free(ls);
140 return NULL;
143 /* Is the local space that of a parameter domain?
145 isl_bool isl_local_space_is_params(__isl_keep isl_local_space *ls)
147 if (!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)
164 if (!ls1 || !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)
183 isl_bool ok;
185 ok = isl_local_space_has_space(ls, space);
186 if (ok < 0)
187 return isl_stat_error;
188 if (!ok)
189 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
190 "spaces don't match", return isl_stat_error);
191 return isl_stat_ok;
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)
200 isl_bool equal;
202 equal = isl_local_space_has_equal_space(ls1, ls2);
203 if (equal < 0 || !equal)
204 return 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)
222 int cmp;
224 if (ls1 == ls2)
225 return 0;
226 if (!ls1)
227 return -1;
228 if (!ls2)
229 return 1;
231 cmp = isl_space_cmp(ls1->dim, ls2->dim);
232 if (cmp != 0)
233 return cmp;
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)
241 if (!ls)
242 return 0;
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)
253 isl_space *space;
255 if (!ls)
256 return 0;
258 space = ls->dim;
259 switch (type) {
260 case isl_dim_cst: return 0;
261 case isl_dim_param: return 1;
262 case isl_dim_in: return 1 + space->nparam;
263 case isl_dim_out: return 1 + space->nparam + space->n_in;
264 case isl_dim_div:
265 return 1 + space->nparam + space->n_in + space->n_out;
266 default: return 0;
270 /* Return the position of the dimension of the given type and name
271 * in "ls".
272 * Return -1 if no such dimension can be found.
274 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space *ls,
275 enum isl_dim_type type, const char *name)
277 if (!ls)
278 return -1;
279 if (type == isl_dim_div)
280 return -1;
281 return isl_space_find_dim_by_name(ls->dim, type, name);
284 /* Does the given dimension have a name?
286 isl_bool isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
287 enum isl_dim_type type, unsigned pos)
289 return ls ? isl_space_has_dim_name(ls->dim, type, pos) : isl_bool_error;
292 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
293 enum isl_dim_type type, unsigned pos)
295 return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
298 isl_bool isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
299 enum isl_dim_type type, unsigned pos)
301 return ls ? isl_space_has_dim_id(ls->dim, type, pos) : isl_bool_error;
304 __isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
305 enum isl_dim_type type, unsigned pos)
307 return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
310 /* Return the argument of the integer division at position "pos" in "ls".
311 * All local variables in "ls" are known to have a (complete) explicit
312 * representation.
314 static __isl_give isl_aff *extract_div(__isl_keep isl_local_space *ls, int pos)
316 isl_aff *aff;
318 aff = isl_aff_alloc(isl_local_space_copy(ls));
319 if (!aff)
320 return NULL;
321 isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
322 return aff;
325 /* Return the argument of the integer division at position "pos" in "ls".
326 * The integer division at that position is known to have a complete
327 * explicit representation, but some of the others do not.
328 * Remove them first because the domain of an isl_aff
329 * is not allowed to have unknown local variables.
331 static __isl_give isl_aff *drop_unknown_divs_and_extract_div(
332 __isl_keep isl_local_space *ls, int pos)
334 int i, n;
335 isl_bool unknown;
336 isl_aff *aff;
338 ls = isl_local_space_copy(ls);
339 n = isl_local_space_dim(ls, isl_dim_div);
340 for (i = n - 1; i >= 0; --i) {
341 unknown = isl_local_space_div_is_marked_unknown(ls, i);
342 if (unknown < 0)
343 ls = isl_local_space_free(ls);
344 else if (!unknown)
345 continue;
346 ls = isl_local_space_drop_dims(ls, isl_dim_div, i, 1);
347 if (pos > i)
348 --pos;
350 aff = extract_div(ls, pos);
351 isl_local_space_free(ls);
352 return aff;
355 /* Return the argument of the integer division at position "pos" in "ls".
356 * The integer division is assumed to have a complete explicit
357 * representation. If some of the other integer divisions
358 * do not have an explicit representation, then they need
359 * to be removed first because the domain of an isl_aff
360 * is not allowed to have unknown local variables.
362 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
363 int pos)
365 isl_bool known;
367 if (!ls)
368 return NULL;
370 if (pos < 0 || pos >= ls->div->n_row)
371 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
372 "index out of bounds", return NULL);
374 known = isl_local_space_div_is_known(ls, pos);
375 if (known < 0)
376 return NULL;
377 if (!known)
378 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
379 "expression of div unknown", return NULL);
380 if (!isl_local_space_is_set(ls))
381 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
382 "cannot represent divs of map spaces", return NULL);
384 known = isl_local_space_divs_known(ls);
385 if (known < 0)
386 return NULL;
387 if (known)
388 return extract_div(ls, pos);
389 else
390 return drop_unknown_divs_and_extract_div(ls, pos);
393 /* Return the space of "ls".
395 __isl_keep isl_space *isl_local_space_peek_space(__isl_keep isl_local_space *ls)
397 if (!ls)
398 return NULL;
400 return ls->dim;
403 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
405 return isl_space_copy(isl_local_space_peek_space(ls));
408 /* Return the space of "ls".
409 * This may be either a copy or the space itself
410 * if there is only one reference to "ls".
411 * This allows the space to be modified inplace
412 * if both the local space and its space have only a single reference.
413 * The caller is not allowed to modify "ls" between this call and
414 * a subsequent call to isl_local_space_restore_space.
415 * The only exception is that isl_local_space_free can be called instead.
417 __isl_give isl_space *isl_local_space_take_space(__isl_keep isl_local_space *ls)
419 isl_space *space;
421 if (!ls)
422 return NULL;
423 if (ls->ref != 1)
424 return isl_local_space_get_space(ls);
425 space = ls->dim;
426 ls->dim = NULL;
427 return space;
430 /* Set the space of "ls" to "space", where the space of "ls" may be missing
431 * due to a preceding call to isl_local_space_take_space.
432 * However, in this case, "ls" only has a single reference and
433 * then the call to isl_local_space_cow has no effect.
435 __isl_give isl_local_space *isl_local_space_restore_space(
436 __isl_take isl_local_space *ls, __isl_take isl_space *space)
438 if (!ls || !space)
439 goto error;
441 if (ls->dim == space) {
442 isl_space_free(space);
443 return ls;
446 ls = isl_local_space_cow(ls);
447 if (!ls)
448 goto error;
449 isl_space_free(ls->dim);
450 ls->dim = space;
452 return ls;
453 error:
454 isl_local_space_free(ls);
455 isl_space_free(space);
456 return NULL;
459 /* Return the local variables of "ls".
461 __isl_keep isl_local *isl_local_space_peek_local(__isl_keep isl_local_space *ls)
463 return ls ? ls->div : NULL;
466 /* Replace the identifier of the tuple of type "type" by "id".
468 __isl_give isl_local_space *isl_local_space_set_tuple_id(
469 __isl_take isl_local_space *ls,
470 enum isl_dim_type type, __isl_take isl_id *id)
472 ls = isl_local_space_cow(ls);
473 if (!ls)
474 goto error;
475 ls->dim = isl_space_set_tuple_id(ls->dim, type, id);
476 if (!ls->dim)
477 return isl_local_space_free(ls);
478 return ls;
479 error:
480 isl_id_free(id);
481 return NULL;
484 __isl_give isl_local_space *isl_local_space_set_dim_name(
485 __isl_take isl_local_space *ls,
486 enum isl_dim_type type, unsigned pos, const char *s)
488 ls = isl_local_space_cow(ls);
489 if (!ls)
490 return NULL;
491 ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
492 if (!ls->dim)
493 return isl_local_space_free(ls);
495 return ls;
498 __isl_give isl_local_space *isl_local_space_set_dim_id(
499 __isl_take isl_local_space *ls,
500 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
502 ls = isl_local_space_cow(ls);
503 if (!ls)
504 goto error;
505 ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
506 if (!ls->dim)
507 return isl_local_space_free(ls);
509 return ls;
510 error:
511 isl_id_free(id);
512 return NULL;
515 /* Construct a zero-dimensional local space with the given parameter domain.
517 __isl_give isl_local_space *isl_local_space_set_from_params(
518 __isl_take isl_local_space *ls)
520 isl_space *space;
522 space = isl_local_space_take_space(ls);
523 space = isl_space_set_from_params(space);
524 ls = isl_local_space_restore_space(ls, space);
526 return ls;
529 __isl_give isl_local_space *isl_local_space_reset_space(
530 __isl_take isl_local_space *ls, __isl_take isl_space *dim)
532 ls = isl_local_space_cow(ls);
533 if (!ls || !dim)
534 goto error;
536 isl_space_free(ls->dim);
537 ls->dim = dim;
539 return ls;
540 error:
541 isl_local_space_free(ls);
542 isl_space_free(dim);
543 return NULL;
546 /* Reorder the dimensions of "ls" according to the given reordering.
547 * The reordering r is assumed to have been extended with the local
548 * variables, leaving them in the same order.
550 __isl_give isl_local_space *isl_local_space_realign(
551 __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
553 ls = isl_local_space_cow(ls);
554 if (!ls || !r)
555 goto error;
557 ls->div = isl_local_reorder(ls->div, isl_reordering_copy(r));
558 if (!ls->div)
559 goto error;
561 ls = isl_local_space_reset_space(ls, isl_reordering_get_space(r));
563 isl_reordering_free(r);
564 return ls;
565 error:
566 isl_local_space_free(ls);
567 isl_reordering_free(r);
568 return NULL;
571 __isl_give isl_local_space *isl_local_space_add_div(
572 __isl_take isl_local_space *ls, __isl_take isl_vec *div)
574 ls = isl_local_space_cow(ls);
575 if (!ls || !div)
576 goto error;
578 if (ls->div->n_col != div->size)
579 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
580 "incompatible dimensions", goto error);
582 ls->div = isl_mat_add_zero_cols(ls->div, 1);
583 ls->div = isl_mat_add_rows(ls->div, 1);
584 if (!ls->div)
585 goto error;
587 isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
588 isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
590 isl_vec_free(div);
591 return ls;
592 error:
593 isl_local_space_free(ls);
594 isl_vec_free(div);
595 return NULL;
598 __isl_give isl_local_space *isl_local_space_replace_divs(
599 __isl_take isl_local_space *ls, __isl_take isl_mat *div)
601 ls = isl_local_space_cow(ls);
603 if (!ls || !div)
604 goto error;
606 isl_mat_free(ls->div);
607 ls->div = div;
608 return ls;
609 error:
610 isl_mat_free(div);
611 isl_local_space_free(ls);
612 return NULL;
615 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
616 * defined by "exp".
618 static void expand_row(__isl_keep isl_mat *dst, int d,
619 __isl_keep isl_mat *src, int s, int *exp)
621 int i;
622 unsigned c = src->n_col - src->n_row;
624 isl_seq_cpy(dst->row[d], src->row[s], c);
625 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
627 for (i = 0; i < s; ++i)
628 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
631 /* Compare (known) divs.
632 * Return non-zero if at least one of the two divs is unknown.
633 * In particular, if both divs are unknown, we respect their
634 * current order. Otherwise, we sort the known div after the unknown
635 * div only if the known div depends on the unknown div.
637 static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
638 unsigned n_row, unsigned n_col)
640 int li, lj;
641 int unknown_i, unknown_j;
643 unknown_i = isl_int_is_zero(row_i[0]);
644 unknown_j = isl_int_is_zero(row_j[0]);
646 if (unknown_i && unknown_j)
647 return i - j;
649 if (unknown_i)
650 li = n_col - n_row + i;
651 else
652 li = isl_seq_last_non_zero(row_i, n_col);
653 if (unknown_j)
654 lj = n_col - n_row + j;
655 else
656 lj = isl_seq_last_non_zero(row_j, n_col);
658 if (li != lj)
659 return li - lj;
661 return isl_seq_cmp(row_i, row_j, n_col);
664 /* Call cmp_row for divs in a matrix.
666 int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
668 return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
671 /* Call cmp_row for divs in a basic map.
673 static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
674 unsigned total)
676 return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
679 /* Sort the divs in "bmap".
681 * We first make sure divs are placed after divs on which they depend.
682 * Then we perform a simple insertion sort based on the same ordering
683 * that is used in isl_merge_divs.
685 __isl_give isl_basic_map *isl_basic_map_sort_divs(
686 __isl_take isl_basic_map *bmap)
688 int i, j;
689 unsigned total;
691 bmap = isl_basic_map_order_divs(bmap);
692 if (!bmap)
693 return NULL;
694 if (bmap->n_div <= 1)
695 return bmap;
697 total = 2 + isl_basic_map_total_dim(bmap);
698 for (i = 1; i < bmap->n_div; ++i) {
699 for (j = i - 1; j >= 0; --j) {
700 if (bmap_cmp_row(bmap, j, j + 1, total) <= 0)
701 break;
702 isl_basic_map_swap_div(bmap, j, j + 1);
706 return bmap;
709 /* Sort the divs in the basic maps of "map".
711 __isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
713 return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
716 /* Combine the two lists of divs into a single list.
717 * For each row i in div1, exp1[i] is set to the position of the corresponding
718 * row in the result. Similarly for div2 and exp2.
719 * This function guarantees
720 * exp1[i] >= i
721 * exp1[i+1] > exp1[i]
722 * For optimal merging, the two input list should have been sorted.
724 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
725 __isl_keep isl_mat *div2, int *exp1, int *exp2)
727 int i, j, k;
728 isl_mat *div = NULL;
729 unsigned d;
731 if (!div1 || !div2)
732 return NULL;
734 d = div1->n_col - div1->n_row;
735 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
736 d + div1->n_row + div2->n_row);
737 if (!div)
738 return NULL;
740 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
741 int cmp;
743 expand_row(div, k, div1, i, exp1);
744 expand_row(div, k + 1, div2, j, exp2);
746 cmp = isl_mat_cmp_div(div, k, k + 1);
747 if (cmp == 0) {
748 exp1[i++] = k;
749 exp2[j++] = k;
750 } else if (cmp < 0) {
751 exp1[i++] = k;
752 } else {
753 exp2[j++] = k;
754 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
757 for (; i < div1->n_row; ++i, ++k) {
758 expand_row(div, k, div1, i, exp1);
759 exp1[i] = k;
761 for (; j < div2->n_row; ++j, ++k) {
762 expand_row(div, k, div2, j, exp2);
763 exp2[j] = k;
766 div->n_row = k;
767 div->n_col = d + k;
769 return div;
772 /* Swap divs "a" and "b" in "ls".
774 __isl_give isl_local_space *isl_local_space_swap_div(
775 __isl_take isl_local_space *ls, int a, int b)
777 int offset;
779 ls = isl_local_space_cow(ls);
780 if (!ls)
781 return NULL;
782 if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
783 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
784 "index out of bounds", return isl_local_space_free(ls));
785 offset = ls->div->n_col - ls->div->n_row;
786 ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
787 ls->div = isl_mat_swap_rows(ls->div, a, b);
788 if (!ls->div)
789 return isl_local_space_free(ls);
790 return ls;
793 /* Construct a local space that contains all the divs in either
794 * "ls1" or "ls2".
796 __isl_give isl_local_space *isl_local_space_intersect(
797 __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
799 isl_ctx *ctx;
800 int *exp1 = NULL;
801 int *exp2 = NULL;
802 isl_mat *div = NULL;
803 isl_bool equal;
805 if (!ls1 || !ls2)
806 goto error;
808 ctx = isl_local_space_get_ctx(ls1);
809 if (!isl_space_is_equal(ls1->dim, ls2->dim))
810 isl_die(ctx, isl_error_invalid,
811 "spaces should be identical", goto error);
813 if (ls2->div->n_row == 0) {
814 isl_local_space_free(ls2);
815 return ls1;
818 if (ls1->div->n_row == 0) {
819 isl_local_space_free(ls1);
820 return ls2;
823 exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
824 exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
825 if (!exp1 || !exp2)
826 goto error;
828 div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
829 if (!div)
830 goto error;
832 equal = isl_mat_is_equal(ls1->div, div);
833 if (equal < 0)
834 goto error;
835 if (!equal)
836 ls1 = isl_local_space_cow(ls1);
837 if (!ls1)
838 goto error;
840 free(exp1);
841 free(exp2);
842 isl_local_space_free(ls2);
843 isl_mat_free(ls1->div);
844 ls1->div = div;
846 return ls1;
847 error:
848 free(exp1);
849 free(exp2);
850 isl_mat_free(div);
851 isl_local_space_free(ls1);
852 isl_local_space_free(ls2);
853 return NULL;
856 /* Is the local variable "div" of "ls" marked as not having
857 * an explicit representation?
858 * Note that even if this variable is not marked in this way and therefore
859 * does have an explicit representation, this representation may still
860 * depend (indirectly) on other local variables that do not
861 * have an explicit representation.
863 isl_bool isl_local_space_div_is_marked_unknown(__isl_keep isl_local_space *ls,
864 int div)
866 if (!ls)
867 return isl_bool_error;
868 return isl_local_div_is_marked_unknown(ls->div, div);
871 /* Does "ls" have a complete explicit representation for div "div"?
873 isl_bool isl_local_space_div_is_known(__isl_keep isl_local_space *ls, int div)
875 if (!ls)
876 return isl_bool_error;
877 return isl_local_div_is_known(ls->div, div);
880 /* Does "ls" have an explicit representation for all local variables?
882 isl_bool isl_local_space_divs_known(__isl_keep isl_local_space *ls)
884 if (!ls)
885 return isl_bool_error;
886 return isl_local_divs_known(ls->div);
889 __isl_give isl_local_space *isl_local_space_domain(
890 __isl_take isl_local_space *ls)
892 ls = isl_local_space_drop_dims(ls, isl_dim_out,
893 0, isl_local_space_dim(ls, isl_dim_out));
894 ls = isl_local_space_cow(ls);
895 if (!ls)
896 return NULL;
897 ls->dim = isl_space_domain(ls->dim);
898 if (!ls->dim)
899 return isl_local_space_free(ls);
900 return ls;
903 __isl_give isl_local_space *isl_local_space_range(
904 __isl_take isl_local_space *ls)
906 ls = isl_local_space_drop_dims(ls, isl_dim_in,
907 0, isl_local_space_dim(ls, isl_dim_in));
908 ls = isl_local_space_cow(ls);
909 if (!ls)
910 return NULL;
912 ls->dim = isl_space_range(ls->dim);
913 if (!ls->dim)
914 return isl_local_space_free(ls);
915 return ls;
918 /* Construct a local space for a map that has the given local
919 * space as domain and that has a zero-dimensional range.
921 __isl_give isl_local_space *isl_local_space_from_domain(
922 __isl_take isl_local_space *ls)
924 ls = isl_local_space_cow(ls);
925 if (!ls)
926 return NULL;
927 ls->dim = isl_space_from_domain(ls->dim);
928 if (!ls->dim)
929 return isl_local_space_free(ls);
930 return ls;
933 __isl_give isl_local_space *isl_local_space_add_dims(
934 __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
936 int pos;
938 if (!ls)
939 return NULL;
940 pos = isl_local_space_dim(ls, type);
941 return isl_local_space_insert_dims(ls, type, pos, n);
944 /* Lift the basic set "bset", living in the space of "ls"
945 * to live in a space with extra coordinates corresponding
946 * to the local variables of "ls".
948 __isl_give isl_basic_set *isl_local_space_lift_basic_set(
949 __isl_take isl_local_space *ls, __isl_take isl_basic_set *bset)
951 unsigned n_local;
952 isl_space *space;
953 isl_basic_set *ls_bset;
955 space = isl_basic_set_peek_space(bset);
956 if (isl_local_space_check_has_space(ls, space) < 0)
957 goto error;
959 n_local = isl_local_space_dim(ls, isl_dim_div);
960 if (n_local == 0) {
961 isl_local_space_free(ls);
962 return bset;
965 bset = isl_basic_set_add_dims(bset, isl_dim_set, n_local);
966 ls_bset = isl_basic_set_from_local_space(ls);
967 ls_bset = isl_basic_set_lift(ls_bset);
968 ls_bset = isl_basic_set_flatten(ls_bset);
969 bset = isl_basic_set_intersect(bset, ls_bset);
971 return bset;
972 error:
973 isl_local_space_free(ls);
974 isl_basic_set_free(bset);
975 return NULL;
978 /* Lift the set "set", living in the space of "ls"
979 * to live in a space with extra coordinates corresponding
980 * to the local variables of "ls".
982 __isl_give isl_set *isl_local_space_lift_set(__isl_take isl_local_space *ls,
983 __isl_take isl_set *set)
985 unsigned n_local;
986 isl_basic_set *bset;
988 if (isl_local_space_check_has_space(ls, isl_set_peek_space(set)) < 0)
989 goto error;
991 n_local = isl_local_space_dim(ls, isl_dim_div);
992 if (n_local == 0) {
993 isl_local_space_free(ls);
994 return set;
997 set = isl_set_add_dims(set, isl_dim_set, n_local);
998 bset = isl_basic_set_from_local_space(ls);
999 bset = isl_basic_set_lift(bset);
1000 bset = isl_basic_set_flatten(bset);
1001 set = isl_set_intersect(set, isl_set_from_basic_set(bset));
1003 return set;
1004 error:
1005 isl_local_space_free(ls);
1006 isl_set_free(set);
1007 return NULL;
1010 /* Remove common factor of non-constant terms and denominator.
1012 static void normalize_div(__isl_keep isl_local_space *ls, int div)
1014 isl_ctx *ctx = ls->div->ctx;
1015 unsigned total = ls->div->n_col - 2;
1017 isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
1018 isl_int_gcd(ctx->normalize_gcd,
1019 ctx->normalize_gcd, ls->div->row[div][0]);
1020 if (isl_int_is_one(ctx->normalize_gcd))
1021 return;
1023 isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
1024 ctx->normalize_gcd, total);
1025 isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
1026 ctx->normalize_gcd);
1027 isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
1028 ctx->normalize_gcd);
1031 /* Exploit the equalities in "eq" to simplify the expressions of
1032 * the integer divisions in "ls".
1033 * The integer divisions in "ls" are assumed to appear as regular
1034 * dimensions in "eq".
1036 __isl_give isl_local_space *isl_local_space_substitute_equalities(
1037 __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
1039 int i, j, k;
1040 unsigned total;
1041 unsigned n_div;
1043 if (!ls || !eq)
1044 goto error;
1046 total = isl_space_dim(eq->dim, isl_dim_all);
1047 if (isl_local_space_dim(ls, isl_dim_all) != total)
1048 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1049 "spaces don't match", goto error);
1050 total++;
1051 n_div = eq->n_div;
1052 for (i = 0; i < eq->n_eq; ++i) {
1053 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1054 if (j < 0 || j == 0 || j >= total)
1055 continue;
1057 for (k = 0; k < ls->div->n_row; ++k) {
1058 if (isl_int_is_zero(ls->div->row[k][1 + j]))
1059 continue;
1060 ls = isl_local_space_cow(ls);
1061 if (!ls)
1062 goto error;
1063 ls->div = isl_mat_cow(ls->div);
1064 if (!ls->div)
1065 goto error;
1066 isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
1067 &ls->div->row[k][0]);
1068 normalize_div(ls, k);
1072 isl_basic_set_free(eq);
1073 return ls;
1074 error:
1075 isl_basic_set_free(eq);
1076 isl_local_space_free(ls);
1077 return NULL;
1080 /* Plug in the affine expressions "subs" of length "subs_len" (including
1081 * the denominator and the constant term) into the variable at position "pos"
1082 * of the "n" div expressions starting at "first".
1084 * Let i be the dimension to replace and let "subs" be of the form
1086 * f/d
1088 * Any integer division starting at "first" with a non-zero coefficient for i,
1090 * floor((a i + g)/m)
1092 * is replaced by
1094 * floor((a f + d g)/(m d))
1096 __isl_give isl_local_space *isl_local_space_substitute_seq(
1097 __isl_take isl_local_space *ls,
1098 enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
1099 int first, int n)
1101 int i;
1102 isl_int v;
1104 if (n == 0)
1105 return ls;
1106 ls = isl_local_space_cow(ls);
1107 if (!ls)
1108 return NULL;
1109 ls->div = isl_mat_cow(ls->div);
1110 if (!ls->div)
1111 return isl_local_space_free(ls);
1113 if (first + n > ls->div->n_row)
1114 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1115 "index out of bounds", return isl_local_space_free(ls));
1117 pos += isl_local_space_offset(ls, type);
1119 isl_int_init(v);
1120 for (i = first; i < first + n; ++i) {
1121 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
1122 continue;
1123 isl_seq_substitute(ls->div->row[i], pos, subs,
1124 ls->div->n_col, subs_len, v);
1125 normalize_div(ls, i);
1127 isl_int_clear(v);
1129 return ls;
1132 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
1133 * of "ls".
1135 * Let i be the dimension to replace and let "subs" be of the form
1137 * f/d
1139 * Any integer division with a non-zero coefficient for i,
1141 * floor((a i + g)/m)
1143 * is replaced by
1145 * floor((a f + d g)/(m d))
1147 __isl_give isl_local_space *isl_local_space_substitute(
1148 __isl_take isl_local_space *ls,
1149 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
1151 ls = isl_local_space_cow(ls);
1152 if (!ls || !subs)
1153 return isl_local_space_free(ls);
1155 if (!isl_space_is_equal(ls->dim, subs->ls->dim))
1156 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1157 "spaces don't match", return isl_local_space_free(ls));
1158 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
1159 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1160 "cannot handle divs yet",
1161 return isl_local_space_free(ls));
1163 return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
1164 subs->v->size, 0, ls->div->n_row);
1167 isl_bool isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
1168 enum isl_dim_type type)
1170 if (!ls)
1171 return isl_bool_error;
1172 return isl_space_is_named_or_nested(ls->dim, type);
1175 __isl_give isl_local_space *isl_local_space_drop_dims(
1176 __isl_take isl_local_space *ls,
1177 enum isl_dim_type type, unsigned first, unsigned n)
1179 isl_ctx *ctx;
1181 if (!ls)
1182 return NULL;
1183 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1184 return ls;
1186 ctx = isl_local_space_get_ctx(ls);
1187 if (first + n > isl_local_space_dim(ls, type))
1188 isl_die(ctx, isl_error_invalid, "range out of bounds",
1189 return isl_local_space_free(ls));
1191 ls = isl_local_space_cow(ls);
1192 if (!ls)
1193 return NULL;
1195 if (type == isl_dim_div) {
1196 ls->div = isl_mat_drop_rows(ls->div, first, n);
1197 } else {
1198 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
1199 if (!ls->dim)
1200 return isl_local_space_free(ls);
1203 first += 1 + isl_local_space_offset(ls, type);
1204 ls->div = isl_mat_drop_cols(ls->div, first, n);
1205 if (!ls->div)
1206 return isl_local_space_free(ls);
1208 return ls;
1211 __isl_give isl_local_space *isl_local_space_insert_dims(
1212 __isl_take isl_local_space *ls,
1213 enum isl_dim_type type, unsigned first, unsigned n)
1215 isl_ctx *ctx;
1217 if (!ls)
1218 return NULL;
1219 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1220 return ls;
1222 ctx = isl_local_space_get_ctx(ls);
1223 if (first > isl_local_space_dim(ls, type))
1224 isl_die(ctx, isl_error_invalid, "position out of bounds",
1225 return isl_local_space_free(ls));
1227 ls = isl_local_space_cow(ls);
1228 if (!ls)
1229 return NULL;
1231 if (type == isl_dim_div) {
1232 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
1233 } else {
1234 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
1235 if (!ls->dim)
1236 return isl_local_space_free(ls);
1239 first += 1 + isl_local_space_offset(ls, type);
1240 ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
1241 if (!ls->div)
1242 return isl_local_space_free(ls);
1244 return ls;
1247 /* Does the linear part of "constraint" correspond to
1248 * integer division "div" in "ls"?
1250 * That is, given div = floor((c + f)/m), is the constraint of the form
1252 * f - m d + c' >= 0 [sign = 1]
1253 * or
1254 * -f + m d + c'' >= 0 [sign = -1]
1256 * If so, set *sign to the corresponding value.
1258 static isl_bool is_linear_div_constraint(__isl_keep isl_local_space *ls,
1259 isl_int *constraint, unsigned div, int *sign)
1261 isl_bool unknown;
1262 unsigned pos;
1264 unknown = isl_local_space_div_is_marked_unknown(ls, div);
1265 if (unknown < 0)
1266 return isl_bool_error;
1267 if (unknown)
1268 return isl_bool_false;
1270 pos = isl_local_space_offset(ls, isl_dim_div) + div;
1272 if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
1273 *sign = -1;
1274 if (!isl_seq_is_neg(constraint + 1,
1275 ls->div->row[div] + 2, pos - 1))
1276 return isl_bool_false;
1277 } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
1278 *sign = 1;
1279 if (!isl_seq_eq(constraint + 1, ls->div->row[div] + 2, pos - 1))
1280 return isl_bool_false;
1281 } else {
1282 return isl_bool_false;
1284 if (isl_seq_first_non_zero(constraint + pos + 1,
1285 ls->div->n_row - div - 1) != -1)
1286 return isl_bool_false;
1287 return isl_bool_true;
1290 /* Check if the constraints pointed to by "constraint" is a div
1291 * constraint corresponding to div "div" in "ls".
1293 * That is, if div = floor(f/m), then check if the constraint is
1295 * f - m d >= 0
1296 * or
1297 * -(f-(m-1)) + m d >= 0
1299 * First check if the linear part is of the right form and
1300 * then check the constant term.
1302 isl_bool isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
1303 isl_int *constraint, unsigned div)
1305 int sign;
1306 isl_bool linear;
1308 linear = is_linear_div_constraint(ls, constraint, div, &sign);
1309 if (linear < 0 || !linear)
1310 return linear;
1312 if (sign < 0) {
1313 int neg;
1314 isl_int_sub(ls->div->row[div][1],
1315 ls->div->row[div][1], ls->div->row[div][0]);
1316 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1317 neg = isl_seq_is_neg(constraint, ls->div->row[div] + 1, 1);
1318 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1319 isl_int_add(ls->div->row[div][1],
1320 ls->div->row[div][1], ls->div->row[div][0]);
1321 if (!neg)
1322 return isl_bool_false;
1323 } else {
1324 if (!isl_int_eq(constraint[0], ls->div->row[div][1]))
1325 return isl_bool_false;
1328 return isl_bool_true;
1331 /* Is the constraint pointed to by "constraint" one
1332 * of an equality that corresponds to integer division "div" in "ls"?
1334 * That is, given an integer division of the form
1336 * a = floor((f + c)/m)
1338 * is the equality of the form
1340 * -f + m d + c' = 0
1342 * Note that the constant term is not checked explicitly, but given
1343 * that this is a valid equality constraint, the constant c' necessarily
1344 * has a value close to -c.
1346 isl_bool isl_local_space_is_div_equality(__isl_keep isl_local_space *ls,
1347 isl_int *constraint, unsigned div)
1349 int sign;
1350 isl_bool linear;
1352 linear = is_linear_div_constraint(ls, constraint, div, &sign);
1353 if (linear < 0 || !linear)
1354 return linear;
1356 return sign < 0;
1360 * Set active[i] to 1 if the dimension at position i is involved
1361 * in the linear expression l.
1363 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
1365 int i, j;
1366 isl_ctx *ctx;
1367 int *active = NULL;
1368 unsigned total;
1369 unsigned offset;
1371 ctx = isl_local_space_get_ctx(ls);
1372 total = isl_local_space_dim(ls, isl_dim_all);
1373 active = isl_calloc_array(ctx, int, total);
1374 if (total && !active)
1375 return NULL;
1377 for (i = 0; i < total; ++i)
1378 active[i] = !isl_int_is_zero(l[i]);
1380 offset = isl_local_space_offset(ls, isl_dim_div) - 1;
1381 for (i = ls->div->n_row - 1; i >= 0; --i) {
1382 if (!active[offset + i])
1383 continue;
1384 for (j = 0; j < total; ++j)
1385 active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
1388 return active;
1391 /* Given a local space "ls" of a set, create a local space
1392 * for the lift of the set. In particular, the result
1393 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1394 * range of the wrapped map.
1396 __isl_give isl_local_space *isl_local_space_lift(
1397 __isl_take isl_local_space *ls)
1399 ls = isl_local_space_cow(ls);
1400 if (!ls)
1401 return NULL;
1403 ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1404 ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1405 if (!ls->dim || !ls->div)
1406 return isl_local_space_free(ls);
1408 return ls;
1411 /* Construct a basic map that maps a set living in local space "ls"
1412 * to the corresponding lifted local space.
1414 __isl_give isl_basic_map *isl_local_space_lifting(
1415 __isl_take isl_local_space *ls)
1417 isl_basic_map *lifting;
1418 isl_basic_set *bset;
1420 if (!ls)
1421 return NULL;
1422 if (!isl_local_space_is_set(ls))
1423 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1424 "lifting only defined on set spaces", goto error);
1426 bset = isl_basic_set_from_local_space(ls);
1427 lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1428 lifting = isl_basic_map_domain_map(lifting);
1429 lifting = isl_basic_map_reverse(lifting);
1431 return lifting;
1432 error:
1433 isl_local_space_free(ls);
1434 return NULL;
1437 /* Compute the preimage of "ls" under the function represented by "ma".
1438 * In other words, plug in "ma" in "ls". The result is a local space
1439 * that is part of the domain space of "ma".
1441 * If the divs in "ls" are represented as
1443 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1445 * and ma is represented by
1447 * x = D(p) + F(y) + G(divs')
1449 * then the resulting divs are
1451 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1453 * We first copy over the divs from "ma" and then
1454 * we add the modified divs from "ls".
1456 __isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1457 __isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1459 int i;
1460 isl_space *space;
1461 isl_local_space *res = NULL;
1462 int n_div_ls, n_div_ma;
1463 isl_int f, c1, c2, g;
1465 ma = isl_multi_aff_align_divs(ma);
1466 if (!ls || !ma)
1467 goto error;
1468 if (!isl_space_is_range_internal(ls->dim, ma->space))
1469 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1470 "spaces don't match", goto error);
1472 n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1473 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
1475 space = isl_space_domain(isl_multi_aff_get_space(ma));
1476 res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1477 if (!res)
1478 goto error;
1480 if (n_div_ma) {
1481 isl_mat_free(res->div);
1482 res->div = isl_mat_copy(ma->u.p[0]->ls->div);
1483 res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1484 res->div = isl_mat_add_rows(res->div, n_div_ls);
1485 if (!res->div)
1486 goto error;
1489 isl_int_init(f);
1490 isl_int_init(c1);
1491 isl_int_init(c2);
1492 isl_int_init(g);
1494 for (i = 0; i < ls->div->n_row; ++i) {
1495 if (isl_int_is_zero(ls->div->row[i][0])) {
1496 isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1497 continue;
1499 isl_seq_preimage(res->div->row[n_div_ma + i], ls->div->row[i],
1500 ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1);
1501 normalize_div(res, n_div_ma + i);
1504 isl_int_clear(f);
1505 isl_int_clear(c1);
1506 isl_int_clear(c2);
1507 isl_int_clear(g);
1509 isl_local_space_free(ls);
1510 isl_multi_aff_free(ma);
1511 return res;
1512 error:
1513 isl_local_space_free(ls);
1514 isl_multi_aff_free(ma);
1515 isl_local_space_free(res);
1516 return NULL;
1519 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1520 * to dimensions of "dst_type" at "dst_pos".
1522 * Moving to/from local dimensions is not allowed.
1523 * We currently assume that the dimension type changes.
1525 __isl_give isl_local_space *isl_local_space_move_dims(
1526 __isl_take isl_local_space *ls,
1527 enum isl_dim_type dst_type, unsigned dst_pos,
1528 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1530 unsigned g_dst_pos;
1531 unsigned g_src_pos;
1533 if (!ls)
1534 return NULL;
1535 if (n == 0 &&
1536 !isl_local_space_is_named_or_nested(ls, src_type) &&
1537 !isl_local_space_is_named_or_nested(ls, dst_type))
1538 return ls;
1540 if (src_pos + n > isl_local_space_dim(ls, src_type))
1541 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1542 "range out of bounds", return isl_local_space_free(ls));
1543 if (dst_pos > isl_local_space_dim(ls, dst_type))
1544 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1545 "position out of bounds",
1546 return isl_local_space_free(ls));
1547 if (src_type == isl_dim_div)
1548 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1549 "cannot move divs", return isl_local_space_free(ls));
1550 if (dst_type == isl_dim_div)
1551 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1552 "cannot move to divs", return isl_local_space_free(ls));
1553 if (dst_type == src_type && dst_pos == src_pos)
1554 return ls;
1555 if (dst_type == src_type)
1556 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1557 "moving dims within the same type not supported",
1558 return isl_local_space_free(ls));
1560 ls = isl_local_space_cow(ls);
1561 if (!ls)
1562 return NULL;
1564 g_src_pos = 1 + isl_local_space_offset(ls, src_type) + src_pos;
1565 g_dst_pos = 1 + isl_local_space_offset(ls, dst_type) + dst_pos;
1566 if (dst_type > src_type)
1567 g_dst_pos -= n;
1568 ls->div = isl_mat_move_cols(ls->div, g_dst_pos, g_src_pos, n);
1569 if (!ls->div)
1570 return isl_local_space_free(ls);
1571 ls->dim = isl_space_move_dims(ls->dim, dst_type, dst_pos,
1572 src_type, src_pos, n);
1573 if (!ls->dim)
1574 return isl_local_space_free(ls);
1576 return ls;
1579 /* Remove any internal structure of the domain of "ls".
1580 * If there is any such internal structure in the input,
1581 * then the name of the corresponding space is also removed.
1583 __isl_give isl_local_space *isl_local_space_flatten_domain(
1584 __isl_take isl_local_space *ls)
1586 if (!ls)
1587 return NULL;
1589 if (!ls->dim->nested[0])
1590 return ls;
1592 ls = isl_local_space_cow(ls);
1593 if (!ls)
1594 return NULL;
1596 ls->dim = isl_space_flatten_domain(ls->dim);
1597 if (!ls->dim)
1598 return isl_local_space_free(ls);
1600 return ls;
1603 /* Remove any internal structure of the range of "ls".
1604 * If there is any such internal structure in the input,
1605 * then the name of the corresponding space is also removed.
1607 __isl_give isl_local_space *isl_local_space_flatten_range(
1608 __isl_take isl_local_space *ls)
1610 if (!ls)
1611 return NULL;
1613 if (!ls->dim->nested[1])
1614 return ls;
1616 ls = isl_local_space_cow(ls);
1617 if (!ls)
1618 return NULL;
1620 ls->dim = isl_space_flatten_range(ls->dim);
1621 if (!ls->dim)
1622 return isl_local_space_free(ls);
1624 return ls;
1627 /* Given the local space "ls" of a map, return the local space of a set
1628 * that lives in a space that wraps the space of "ls" and that has
1629 * the same divs.
1631 __isl_give isl_local_space *isl_local_space_wrap(__isl_take isl_local_space *ls)
1633 ls = isl_local_space_cow(ls);
1634 if (!ls)
1635 return NULL;
1637 ls->dim = isl_space_wrap(ls->dim);
1638 if (!ls->dim)
1639 return isl_local_space_free(ls);
1641 return ls;
1644 /* Lift the point "pnt", living in the space of "ls"
1645 * to live in a space with extra coordinates corresponding
1646 * to the local variables of "ls".
1648 __isl_give isl_point *isl_local_space_lift_point(__isl_take isl_local_space *ls,
1649 __isl_take isl_point *pnt)
1651 unsigned n_local;
1652 isl_space *space;
1653 isl_local *local;
1654 isl_vec *vec;
1656 if (isl_local_space_check_has_space(ls, isl_point_peek_space(pnt)) < 0)
1657 goto error;
1659 local = isl_local_space_peek_local(ls);
1660 n_local = isl_local_space_dim(ls, isl_dim_div);
1662 space = isl_point_take_space(pnt);
1663 vec = isl_point_take_vec(pnt);
1665 space = isl_space_lift(space, n_local);
1666 vec = isl_local_extend_point_vec(local, vec);
1668 pnt = isl_point_restore_vec(pnt, vec);
1669 pnt = isl_point_restore_space(pnt, space);
1671 isl_local_space_free(ls);
1673 return pnt;
1674 error:
1675 isl_local_space_free(ls);
1676 isl_point_free(pnt);
1677 return NULL;