export isl_space_is_wrapping
[isl.git] / isl_local_space.c
blob5248d632ef55ceb1c403a8bbbb73cc9918540a24
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 isl_size total;
81 if (!space)
82 return NULL;
84 total = isl_space_dim(space, isl_dim_all);
85 if (total < 0)
86 return isl_local_space_from_space(isl_space_free(space));
88 ctx = isl_space_get_ctx(space);
89 div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
90 return isl_local_space_alloc_div(space, div);
93 __isl_give isl_local_space *isl_local_space_from_space(__isl_take isl_space *dim)
95 return isl_local_space_alloc(dim, 0);
98 __isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
100 if (!ls)
101 return NULL;
103 ls->ref++;
104 return ls;
107 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
109 if (!ls)
110 return NULL;
112 return isl_local_space_alloc_div(isl_space_copy(ls->dim),
113 isl_mat_copy(ls->div));
117 __isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
119 if (!ls)
120 return NULL;
122 if (ls->ref == 1)
123 return ls;
124 ls->ref--;
125 return isl_local_space_dup(ls);
128 __isl_null isl_local_space *isl_local_space_free(
129 __isl_take isl_local_space *ls)
131 if (!ls)
132 return NULL;
134 if (--ls->ref > 0)
135 return NULL;
137 isl_space_free(ls->dim);
138 isl_mat_free(ls->div);
140 free(ls);
142 return NULL;
145 /* Is the local space that of a parameter domain?
147 isl_bool isl_local_space_is_params(__isl_keep isl_local_space *ls)
149 if (!ls)
150 return isl_bool_error;
151 return isl_space_is_params(ls->dim);
154 /* Is the local space that of a set?
156 isl_bool isl_local_space_is_set(__isl_keep isl_local_space *ls)
158 return ls ? isl_space_is_set(ls->dim) : isl_bool_error;
161 /* Do "ls1" and "ls2" have the same space?
163 isl_bool isl_local_space_has_equal_space(__isl_keep isl_local_space *ls1,
164 __isl_keep isl_local_space *ls2)
166 if (!ls1 || !ls2)
167 return isl_bool_error;
169 return isl_space_is_equal(ls1->dim, ls2->dim);
172 /* Is the space of "ls" equal to "space"?
174 isl_bool isl_local_space_has_space(__isl_keep isl_local_space *ls,
175 __isl_keep isl_space *space)
177 return isl_space_is_equal(isl_local_space_peek_space(ls), space);
180 /* Check that the space of "ls" is equal to "space".
182 static isl_stat isl_local_space_check_has_space(__isl_keep isl_local_space *ls,
183 __isl_keep isl_space *space)
185 isl_bool ok;
187 ok = isl_local_space_has_space(ls, space);
188 if (ok < 0)
189 return isl_stat_error;
190 if (!ok)
191 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
192 "spaces don't match", return isl_stat_error);
193 return isl_stat_ok;
196 /* Return true if the two local spaces are identical, with identical
197 * expressions for the integer divisions.
199 isl_bool isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
200 __isl_keep isl_local_space *ls2)
202 isl_bool equal;
204 equal = isl_local_space_has_equal_space(ls1, ls2);
205 if (equal < 0 || !equal)
206 return equal;
208 if (!isl_local_space_divs_known(ls1))
209 return isl_bool_false;
210 if (!isl_local_space_divs_known(ls2))
211 return isl_bool_false;
213 return isl_mat_is_equal(ls1->div, ls2->div);
216 /* Compare two isl_local_spaces.
218 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
219 * than "ls2" and 0 if they are equal.
221 int isl_local_space_cmp(__isl_keep isl_local_space *ls1,
222 __isl_keep isl_local_space *ls2)
224 int cmp;
226 if (ls1 == ls2)
227 return 0;
228 if (!ls1)
229 return -1;
230 if (!ls2)
231 return 1;
233 cmp = isl_space_cmp(ls1->dim, ls2->dim);
234 if (cmp != 0)
235 return cmp;
237 return isl_local_cmp(ls1->div, ls2->div);
240 isl_size isl_local_space_dim(__isl_keep isl_local_space *ls,
241 enum isl_dim_type type)
243 if (!ls)
244 return isl_size_error;
245 if (type == isl_dim_div)
246 return ls->div->n_row;
247 if (type == isl_dim_all) {
248 isl_size dim = isl_space_dim(ls->dim, isl_dim_all);
249 if (dim < 0)
250 return isl_size_error;
251 return dim + ls->div->n_row;
253 return isl_space_dim(ls->dim, type);
256 #undef TYPE
257 #define TYPE isl_local_space
258 #include "check_type_range_templ.c"
260 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
261 enum isl_dim_type type)
263 isl_space *space;
265 if (!ls)
266 return 0;
268 space = ls->dim;
269 switch (type) {
270 case isl_dim_cst: return 0;
271 case isl_dim_param: return 1;
272 case isl_dim_in: return 1 + space->nparam;
273 case isl_dim_out: return 1 + space->nparam + space->n_in;
274 case isl_dim_div:
275 return 1 + space->nparam + space->n_in + space->n_out;
276 default: return 0;
280 /* Return the position of the dimension of the given type and name
281 * in "ls".
282 * Return -1 if no such dimension can be found.
284 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space *ls,
285 enum isl_dim_type type, const char *name)
287 if (!ls)
288 return -1;
289 if (type == isl_dim_div)
290 return -1;
291 return isl_space_find_dim_by_name(ls->dim, type, name);
294 /* Does the given dimension have a name?
296 isl_bool isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
297 enum isl_dim_type type, unsigned pos)
299 return ls ? isl_space_has_dim_name(ls->dim, type, pos) : isl_bool_error;
302 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
303 enum isl_dim_type type, unsigned pos)
305 return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
308 isl_bool isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
309 enum isl_dim_type type, unsigned pos)
311 return ls ? isl_space_has_dim_id(ls->dim, type, pos) : isl_bool_error;
314 __isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
315 enum isl_dim_type type, unsigned pos)
317 return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
320 /* Return the argument of the integer division at position "pos" in "ls".
321 * All local variables in "ls" are known to have a (complete) explicit
322 * representation.
324 static __isl_give isl_aff *extract_div(__isl_keep isl_local_space *ls, int pos)
326 isl_aff *aff;
328 aff = isl_aff_alloc(isl_local_space_copy(ls));
329 if (!aff)
330 return NULL;
331 isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
332 return aff;
335 /* Return the argument of the integer division at position "pos" in "ls".
336 * The integer division at that position is known to have a complete
337 * explicit representation, but some of the others do not.
338 * Remove them first because the domain of an isl_aff
339 * is not allowed to have unknown local variables.
341 static __isl_give isl_aff *drop_unknown_divs_and_extract_div(
342 __isl_keep isl_local_space *ls, int pos)
344 int i;
345 isl_size n;
346 isl_bool unknown;
347 isl_aff *aff;
349 n = isl_local_space_dim(ls, isl_dim_div);
350 if (n < 0)
351 return NULL;
352 ls = isl_local_space_copy(ls);
353 for (i = n - 1; i >= 0; --i) {
354 unknown = isl_local_space_div_is_marked_unknown(ls, i);
355 if (unknown < 0)
356 ls = isl_local_space_free(ls);
357 else if (!unknown)
358 continue;
359 ls = isl_local_space_drop_dims(ls, isl_dim_div, i, 1);
360 if (pos > i)
361 --pos;
363 aff = extract_div(ls, pos);
364 isl_local_space_free(ls);
365 return aff;
368 /* Return the argument of the integer division at position "pos" in "ls".
369 * The integer division is assumed to have a complete explicit
370 * representation. If some of the other integer divisions
371 * do not have an explicit representation, then they need
372 * to be removed first because the domain of an isl_aff
373 * is not allowed to have unknown local variables.
375 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
376 int pos)
378 isl_bool known;
380 if (!ls)
381 return NULL;
383 if (pos < 0 || pos >= ls->div->n_row)
384 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
385 "index out of bounds", return NULL);
387 known = isl_local_space_div_is_known(ls, pos);
388 if (known < 0)
389 return NULL;
390 if (!known)
391 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
392 "expression of div unknown", return NULL);
393 if (!isl_local_space_is_set(ls))
394 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
395 "cannot represent divs of map spaces", return NULL);
397 known = isl_local_space_divs_known(ls);
398 if (known < 0)
399 return NULL;
400 if (known)
401 return extract_div(ls, pos);
402 else
403 return drop_unknown_divs_and_extract_div(ls, pos);
406 /* Return the space of "ls".
408 __isl_keep isl_space *isl_local_space_peek_space(__isl_keep isl_local_space *ls)
410 if (!ls)
411 return NULL;
413 return ls->dim;
416 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
418 return isl_space_copy(isl_local_space_peek_space(ls));
421 /* Return the space of "ls".
422 * This may be either a copy or the space itself
423 * if there is only one reference to "ls".
424 * This allows the space to be modified inplace
425 * if both the local space and its space have only a single reference.
426 * The caller is not allowed to modify "ls" between this call and
427 * a subsequent call to isl_local_space_restore_space.
428 * The only exception is that isl_local_space_free can be called instead.
430 __isl_give isl_space *isl_local_space_take_space(__isl_keep isl_local_space *ls)
432 isl_space *space;
434 if (!ls)
435 return NULL;
436 if (ls->ref != 1)
437 return isl_local_space_get_space(ls);
438 space = ls->dim;
439 ls->dim = NULL;
440 return space;
443 /* Set the space of "ls" to "space", where the space of "ls" may be missing
444 * due to a preceding call to isl_local_space_take_space.
445 * However, in this case, "ls" only has a single reference and
446 * then the call to isl_local_space_cow has no effect.
448 __isl_give isl_local_space *isl_local_space_restore_space(
449 __isl_take isl_local_space *ls, __isl_take isl_space *space)
451 if (!ls || !space)
452 goto error;
454 if (ls->dim == space) {
455 isl_space_free(space);
456 return ls;
459 ls = isl_local_space_cow(ls);
460 if (!ls)
461 goto error;
462 isl_space_free(ls->dim);
463 ls->dim = space;
465 return ls;
466 error:
467 isl_local_space_free(ls);
468 isl_space_free(space);
469 return NULL;
472 /* Return the local variables of "ls".
474 __isl_keep isl_local *isl_local_space_peek_local(__isl_keep isl_local_space *ls)
476 return ls ? ls->div : NULL;
479 /* Replace the identifier of the tuple of type "type" by "id".
481 __isl_give isl_local_space *isl_local_space_set_tuple_id(
482 __isl_take isl_local_space *ls,
483 enum isl_dim_type type, __isl_take isl_id *id)
485 ls = isl_local_space_cow(ls);
486 if (!ls)
487 goto error;
488 ls->dim = isl_space_set_tuple_id(ls->dim, type, id);
489 if (!ls->dim)
490 return isl_local_space_free(ls);
491 return ls;
492 error:
493 isl_id_free(id);
494 return NULL;
497 __isl_give isl_local_space *isl_local_space_set_dim_name(
498 __isl_take isl_local_space *ls,
499 enum isl_dim_type type, unsigned pos, const char *s)
501 ls = isl_local_space_cow(ls);
502 if (!ls)
503 return NULL;
504 ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
505 if (!ls->dim)
506 return isl_local_space_free(ls);
508 return ls;
511 __isl_give isl_local_space *isl_local_space_set_dim_id(
512 __isl_take isl_local_space *ls,
513 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
515 ls = isl_local_space_cow(ls);
516 if (!ls)
517 goto error;
518 ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
519 if (!ls->dim)
520 return isl_local_space_free(ls);
522 return ls;
523 error:
524 isl_id_free(id);
525 return NULL;
528 /* Construct a zero-dimensional local space with the given parameter domain.
530 __isl_give isl_local_space *isl_local_space_set_from_params(
531 __isl_take isl_local_space *ls)
533 isl_space *space;
535 space = isl_local_space_take_space(ls);
536 space = isl_space_set_from_params(space);
537 ls = isl_local_space_restore_space(ls, space);
539 return ls;
542 __isl_give isl_local_space *isl_local_space_reset_space(
543 __isl_take isl_local_space *ls, __isl_take isl_space *dim)
545 ls = isl_local_space_cow(ls);
546 if (!ls || !dim)
547 goto error;
549 isl_space_free(ls->dim);
550 ls->dim = dim;
552 return ls;
553 error:
554 isl_local_space_free(ls);
555 isl_space_free(dim);
556 return NULL;
559 /* Reorder the dimensions of "ls" according to the given reordering.
560 * The reordering r is assumed to have been extended with the local
561 * variables, leaving them in the same order.
563 __isl_give isl_local_space *isl_local_space_realign(
564 __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
566 ls = isl_local_space_cow(ls);
567 if (!ls || !r)
568 goto error;
570 ls->div = isl_local_reorder(ls->div, isl_reordering_copy(r));
571 if (!ls->div)
572 goto error;
574 ls = isl_local_space_reset_space(ls, isl_reordering_get_space(r));
576 isl_reordering_free(r);
577 return ls;
578 error:
579 isl_local_space_free(ls);
580 isl_reordering_free(r);
581 return NULL;
584 __isl_give isl_local_space *isl_local_space_add_div(
585 __isl_take isl_local_space *ls, __isl_take isl_vec *div)
587 ls = isl_local_space_cow(ls);
588 if (!ls || !div)
589 goto error;
591 if (ls->div->n_col != div->size)
592 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
593 "incompatible dimensions", goto error);
595 ls->div = isl_mat_add_zero_cols(ls->div, 1);
596 ls->div = isl_mat_add_rows(ls->div, 1);
597 if (!ls->div)
598 goto error;
600 isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
601 isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
603 isl_vec_free(div);
604 return ls;
605 error:
606 isl_local_space_free(ls);
607 isl_vec_free(div);
608 return NULL;
611 __isl_give isl_local_space *isl_local_space_replace_divs(
612 __isl_take isl_local_space *ls, __isl_take isl_mat *div)
614 ls = isl_local_space_cow(ls);
616 if (!ls || !div)
617 goto error;
619 isl_mat_free(ls->div);
620 ls->div = div;
621 return ls;
622 error:
623 isl_mat_free(div);
624 isl_local_space_free(ls);
625 return NULL;
628 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
629 * defined by "exp".
631 static void expand_row(__isl_keep isl_mat *dst, int d,
632 __isl_keep isl_mat *src, int s, int *exp)
634 int i;
635 unsigned c = src->n_col - src->n_row;
637 isl_seq_cpy(dst->row[d], src->row[s], c);
638 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
640 for (i = 0; i < s; ++i)
641 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
644 /* Compare (known) divs.
645 * Return non-zero if at least one of the two divs is unknown.
646 * In particular, if both divs are unknown, we respect their
647 * current order. Otherwise, we sort the known div after the unknown
648 * div only if the known div depends on the unknown div.
650 static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
651 unsigned n_row, unsigned n_col)
653 int li, lj;
654 int unknown_i, unknown_j;
656 unknown_i = isl_int_is_zero(row_i[0]);
657 unknown_j = isl_int_is_zero(row_j[0]);
659 if (unknown_i && unknown_j)
660 return i - j;
662 if (unknown_i)
663 li = n_col - n_row + i;
664 else
665 li = isl_seq_last_non_zero(row_i, n_col);
666 if (unknown_j)
667 lj = n_col - n_row + j;
668 else
669 lj = isl_seq_last_non_zero(row_j, n_col);
671 if (li != lj)
672 return li - lj;
674 return isl_seq_cmp(row_i, row_j, n_col);
677 /* Call cmp_row for divs in a matrix.
679 int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
681 return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
684 /* Call cmp_row for divs in a basic map.
686 static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
687 unsigned total)
689 return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
692 /* Sort the divs in "bmap".
694 * We first make sure divs are placed after divs on which they depend.
695 * Then we perform a simple insertion sort based on the same ordering
696 * that is used in isl_merge_divs.
698 __isl_give isl_basic_map *isl_basic_map_sort_divs(
699 __isl_take isl_basic_map *bmap)
701 int i, j;
702 isl_size total;
704 bmap = isl_basic_map_order_divs(bmap);
705 if (!bmap)
706 return NULL;
707 if (bmap->n_div <= 1)
708 return bmap;
710 total = isl_basic_map_dim(bmap, isl_dim_all);
711 if (total < 0)
712 return isl_basic_map_free(bmap);
713 for (i = 1; i < bmap->n_div; ++i) {
714 for (j = i - 1; j >= 0; --j) {
715 if (bmap_cmp_row(bmap, j, j + 1, 2 + total) <= 0)
716 break;
717 bmap = isl_basic_map_swap_div(bmap, j, j + 1);
718 if (!bmap)
719 return NULL;
723 return bmap;
726 /* Sort the divs in the basic maps of "map".
728 __isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
730 return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
733 /* Combine the two lists of divs into a single list.
734 * For each row i in div1, exp1[i] is set to the position of the corresponding
735 * row in the result. Similarly for div2 and exp2.
736 * This function guarantees
737 * exp1[i] >= i
738 * exp1[i+1] > exp1[i]
739 * For optimal merging, the two input list should have been sorted.
741 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
742 __isl_keep isl_mat *div2, int *exp1, int *exp2)
744 int i, j, k;
745 isl_mat *div = NULL;
746 unsigned d;
748 if (!div1 || !div2)
749 return NULL;
751 d = div1->n_col - div1->n_row;
752 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
753 d + div1->n_row + div2->n_row);
754 if (!div)
755 return NULL;
757 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
758 int cmp;
760 expand_row(div, k, div1, i, exp1);
761 expand_row(div, k + 1, div2, j, exp2);
763 cmp = isl_mat_cmp_div(div, k, k + 1);
764 if (cmp == 0) {
765 exp1[i++] = k;
766 exp2[j++] = k;
767 } else if (cmp < 0) {
768 exp1[i++] = k;
769 } else {
770 exp2[j++] = k;
771 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
774 for (; i < div1->n_row; ++i, ++k) {
775 expand_row(div, k, div1, i, exp1);
776 exp1[i] = k;
778 for (; j < div2->n_row; ++j, ++k) {
779 expand_row(div, k, div2, j, exp2);
780 exp2[j] = k;
783 div->n_row = k;
784 div->n_col = d + k;
786 return div;
789 /* Swap divs "a" and "b" in "ls".
791 __isl_give isl_local_space *isl_local_space_swap_div(
792 __isl_take isl_local_space *ls, int a, int b)
794 int offset;
796 ls = isl_local_space_cow(ls);
797 if (!ls)
798 return NULL;
799 if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
800 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
801 "index out of bounds", return isl_local_space_free(ls));
802 offset = ls->div->n_col - ls->div->n_row;
803 ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
804 ls->div = isl_mat_swap_rows(ls->div, a, b);
805 if (!ls->div)
806 return isl_local_space_free(ls);
807 return ls;
810 /* Construct a local space that contains all the divs in either
811 * "ls1" or "ls2".
813 __isl_give isl_local_space *isl_local_space_intersect(
814 __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
816 isl_ctx *ctx;
817 int *exp1 = NULL;
818 int *exp2 = NULL;
819 isl_mat *div = NULL;
820 isl_bool equal;
822 if (!ls1 || !ls2)
823 goto error;
825 ctx = isl_local_space_get_ctx(ls1);
826 if (!isl_space_is_equal(ls1->dim, ls2->dim))
827 isl_die(ctx, isl_error_invalid,
828 "spaces should be identical", goto error);
830 if (ls2->div->n_row == 0) {
831 isl_local_space_free(ls2);
832 return ls1;
835 if (ls1->div->n_row == 0) {
836 isl_local_space_free(ls1);
837 return ls2;
840 exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
841 exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
842 if (!exp1 || !exp2)
843 goto error;
845 div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
846 if (!div)
847 goto error;
849 equal = isl_mat_is_equal(ls1->div, div);
850 if (equal < 0)
851 goto error;
852 if (!equal)
853 ls1 = isl_local_space_cow(ls1);
854 if (!ls1)
855 goto error;
857 free(exp1);
858 free(exp2);
859 isl_local_space_free(ls2);
860 isl_mat_free(ls1->div);
861 ls1->div = div;
863 return ls1;
864 error:
865 free(exp1);
866 free(exp2);
867 isl_mat_free(div);
868 isl_local_space_free(ls1);
869 isl_local_space_free(ls2);
870 return NULL;
873 /* Is the local variable "div" of "ls" marked as not having
874 * an explicit representation?
875 * Note that even if this variable is not marked in this way and therefore
876 * does have an explicit representation, this representation may still
877 * depend (indirectly) on other local variables that do not
878 * have an explicit representation.
880 isl_bool isl_local_space_div_is_marked_unknown(__isl_keep isl_local_space *ls,
881 int div)
883 if (!ls)
884 return isl_bool_error;
885 return isl_local_div_is_marked_unknown(ls->div, div);
888 /* Does "ls" have a complete explicit representation for div "div"?
890 isl_bool isl_local_space_div_is_known(__isl_keep isl_local_space *ls, int div)
892 if (!ls)
893 return isl_bool_error;
894 return isl_local_div_is_known(ls->div, div);
897 /* Does "ls" have an explicit representation for all local variables?
899 isl_bool isl_local_space_divs_known(__isl_keep isl_local_space *ls)
901 if (!ls)
902 return isl_bool_error;
903 return isl_local_divs_known(ls->div);
906 __isl_give isl_local_space *isl_local_space_domain(
907 __isl_take isl_local_space *ls)
909 isl_size n_out;
911 n_out = isl_local_space_dim(ls, isl_dim_out);
912 if (n_out < 0)
913 return isl_local_space_free(ls);
914 ls = isl_local_space_drop_dims(ls, isl_dim_out, 0, n_out);
915 ls = isl_local_space_cow(ls);
916 if (!ls)
917 return NULL;
918 ls->dim = isl_space_domain(ls->dim);
919 if (!ls->dim)
920 return isl_local_space_free(ls);
921 return ls;
924 __isl_give isl_local_space *isl_local_space_range(
925 __isl_take isl_local_space *ls)
927 isl_size n_in;
929 n_in = isl_local_space_dim(ls, isl_dim_in);
930 if (n_in < 0)
931 return isl_local_space_free(ls);
932 ls = isl_local_space_drop_dims(ls, isl_dim_in, 0, n_in);
933 ls = isl_local_space_cow(ls);
934 if (!ls)
935 return NULL;
937 ls->dim = isl_space_range(ls->dim);
938 if (!ls->dim)
939 return isl_local_space_free(ls);
940 return ls;
943 /* Construct a local space for a map that has the given local
944 * space as domain and that has a zero-dimensional range.
946 __isl_give isl_local_space *isl_local_space_from_domain(
947 __isl_take isl_local_space *ls)
949 ls = isl_local_space_cow(ls);
950 if (!ls)
951 return NULL;
952 ls->dim = isl_space_from_domain(ls->dim);
953 if (!ls->dim)
954 return isl_local_space_free(ls);
955 return ls;
958 __isl_give isl_local_space *isl_local_space_add_dims(
959 __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
961 isl_size pos;
963 pos = isl_local_space_dim(ls, type);
964 if (pos < 0)
965 return isl_local_space_free(ls);
966 return isl_local_space_insert_dims(ls, type, pos, n);
969 /* Lift the basic set "bset", living in the space of "ls"
970 * to live in a space with extra coordinates corresponding
971 * to the local variables of "ls".
973 __isl_give isl_basic_set *isl_local_space_lift_basic_set(
974 __isl_take isl_local_space *ls, __isl_take isl_basic_set *bset)
976 isl_size n_local;
977 isl_space *space;
978 isl_basic_set *ls_bset;
980 n_local = isl_local_space_dim(ls, isl_dim_div);
981 space = isl_basic_set_peek_space(bset);
982 if (n_local < 0 ||
983 isl_local_space_check_has_space(ls, space) < 0)
984 goto error;
986 if (n_local == 0) {
987 isl_local_space_free(ls);
988 return bset;
991 bset = isl_basic_set_add_dims(bset, isl_dim_set, n_local);
992 ls_bset = isl_basic_set_from_local_space(ls);
993 ls_bset = isl_basic_set_lift(ls_bset);
994 ls_bset = isl_basic_set_flatten(ls_bset);
995 bset = isl_basic_set_intersect(bset, ls_bset);
997 return bset;
998 error:
999 isl_local_space_free(ls);
1000 isl_basic_set_free(bset);
1001 return NULL;
1004 /* Lift the set "set", living in the space of "ls"
1005 * to live in a space with extra coordinates corresponding
1006 * to the local variables of "ls".
1008 __isl_give isl_set *isl_local_space_lift_set(__isl_take isl_local_space *ls,
1009 __isl_take isl_set *set)
1011 isl_size n_local;
1012 isl_basic_set *bset;
1014 n_local = isl_local_space_dim(ls, isl_dim_div);
1015 if (n_local < 0 ||
1016 isl_local_space_check_has_space(ls, isl_set_peek_space(set)) < 0)
1017 goto error;
1019 if (n_local == 0) {
1020 isl_local_space_free(ls);
1021 return set;
1024 set = isl_set_add_dims(set, isl_dim_set, n_local);
1025 bset = isl_basic_set_from_local_space(ls);
1026 bset = isl_basic_set_lift(bset);
1027 bset = isl_basic_set_flatten(bset);
1028 set = isl_set_intersect(set, isl_set_from_basic_set(bset));
1030 return set;
1031 error:
1032 isl_local_space_free(ls);
1033 isl_set_free(set);
1034 return NULL;
1037 /* Remove common factor of non-constant terms and denominator.
1039 static __isl_give isl_local_space *normalize_div(
1040 __isl_take isl_local_space *ls, int div)
1042 isl_ctx *ctx = ls->div->ctx;
1043 unsigned total = ls->div->n_col - 2;
1045 isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
1046 isl_int_gcd(ctx->normalize_gcd,
1047 ctx->normalize_gcd, ls->div->row[div][0]);
1048 if (isl_int_is_one(ctx->normalize_gcd))
1049 return ls;
1051 isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
1052 ctx->normalize_gcd, total);
1053 isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
1054 ctx->normalize_gcd);
1055 isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
1056 ctx->normalize_gcd);
1058 return ls;
1061 /* Exploit the equalities in "eq" to simplify the expressions of
1062 * the integer divisions in "ls".
1063 * The integer divisions in "ls" are assumed to appear as regular
1064 * dimensions in "eq".
1066 __isl_give isl_local_space *isl_local_space_substitute_equalities(
1067 __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
1069 int i, j, k;
1070 isl_size total, dim;
1071 unsigned n_div;
1073 if (!ls || !eq)
1074 goto error;
1076 total = isl_space_dim(eq->dim, isl_dim_all);
1077 dim = isl_local_space_dim(ls, isl_dim_all);
1078 if (dim < 0 || total < 0)
1079 goto error;
1080 if (dim != total)
1081 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1082 "spaces don't match", goto error);
1083 total++;
1084 n_div = eq->n_div;
1085 for (i = 0; i < eq->n_eq; ++i) {
1086 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1087 if (j < 0 || j == 0 || j >= total)
1088 continue;
1090 for (k = 0; k < ls->div->n_row; ++k) {
1091 if (isl_int_is_zero(ls->div->row[k][1 + j]))
1092 continue;
1093 ls = isl_local_space_cow(ls);
1094 if (!ls)
1095 goto error;
1096 ls->div = isl_mat_cow(ls->div);
1097 if (!ls->div)
1098 goto error;
1099 isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
1100 &ls->div->row[k][0]);
1101 ls = normalize_div(ls, k);
1102 if (!ls)
1103 goto error;
1107 isl_basic_set_free(eq);
1108 return ls;
1109 error:
1110 isl_basic_set_free(eq);
1111 isl_local_space_free(ls);
1112 return NULL;
1115 /* Plug in the affine expressions "subs" of length "subs_len" (including
1116 * the denominator and the constant term) into the variable at position "pos"
1117 * of the "n" div expressions starting at "first".
1119 * Let i be the dimension to replace and let "subs" be of the form
1121 * f/d
1123 * Any integer division starting at "first" with a non-zero coefficient for i,
1125 * floor((a i + g)/m)
1127 * is replaced by
1129 * floor((a f + d g)/(m d))
1131 __isl_give isl_local_space *isl_local_space_substitute_seq(
1132 __isl_take isl_local_space *ls,
1133 enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
1134 int first, int n)
1136 int i;
1137 isl_int v;
1139 if (n == 0)
1140 return ls;
1141 ls = isl_local_space_cow(ls);
1142 if (!ls)
1143 return NULL;
1144 ls->div = isl_mat_cow(ls->div);
1145 if (!ls->div)
1146 return isl_local_space_free(ls);
1148 if (first + n > ls->div->n_row)
1149 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1150 "index out of bounds", return isl_local_space_free(ls));
1152 pos += isl_local_space_offset(ls, type);
1154 isl_int_init(v);
1155 for (i = first; i < first + n; ++i) {
1156 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
1157 continue;
1158 isl_seq_substitute(ls->div->row[i], pos, subs,
1159 ls->div->n_col, subs_len, v);
1160 ls = normalize_div(ls, i);
1161 if (!ls)
1162 break;
1164 isl_int_clear(v);
1166 return ls;
1169 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
1170 * of "ls".
1172 * Let i be the dimension to replace and let "subs" be of the form
1174 * f/d
1176 * Any integer division with a non-zero coefficient for i,
1178 * floor((a i + g)/m)
1180 * is replaced by
1182 * floor((a f + d g)/(m d))
1184 __isl_give isl_local_space *isl_local_space_substitute(
1185 __isl_take isl_local_space *ls,
1186 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
1188 isl_size n_div;
1190 ls = isl_local_space_cow(ls);
1191 if (!ls || !subs)
1192 return isl_local_space_free(ls);
1194 if (!isl_space_is_equal(ls->dim, subs->ls->dim))
1195 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1196 "spaces don't match", return isl_local_space_free(ls));
1197 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
1198 if (n_div < 0)
1199 return isl_local_space_free(ls);
1200 if (n_div != 0)
1201 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1202 "cannot handle divs yet",
1203 return isl_local_space_free(ls));
1205 return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
1206 subs->v->size, 0, ls->div->n_row);
1209 isl_bool isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
1210 enum isl_dim_type type)
1212 if (!ls)
1213 return isl_bool_error;
1214 return isl_space_is_named_or_nested(ls->dim, type);
1217 __isl_give isl_local_space *isl_local_space_drop_dims(
1218 __isl_take isl_local_space *ls,
1219 enum isl_dim_type type, unsigned first, unsigned n)
1221 if (!ls)
1222 return NULL;
1223 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1224 return ls;
1226 if (isl_local_space_check_range(ls, type, first, n) < 0)
1227 return isl_local_space_free(ls);
1229 ls = isl_local_space_cow(ls);
1230 if (!ls)
1231 return NULL;
1233 if (type == isl_dim_div) {
1234 ls->div = isl_mat_drop_rows(ls->div, first, n);
1235 } else {
1236 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
1237 if (!ls->dim)
1238 return isl_local_space_free(ls);
1241 first += 1 + isl_local_space_offset(ls, type);
1242 ls->div = isl_mat_drop_cols(ls->div, first, n);
1243 if (!ls->div)
1244 return isl_local_space_free(ls);
1246 return ls;
1249 __isl_give isl_local_space *isl_local_space_insert_dims(
1250 __isl_take isl_local_space *ls,
1251 enum isl_dim_type type, unsigned first, unsigned n)
1253 if (!ls)
1254 return NULL;
1255 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1256 return ls;
1258 if (isl_local_space_check_range(ls, type, first, 0) < 0)
1259 return isl_local_space_free(ls);
1261 ls = isl_local_space_cow(ls);
1262 if (!ls)
1263 return NULL;
1265 if (type == isl_dim_div) {
1266 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
1267 } else {
1268 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
1269 if (!ls->dim)
1270 return isl_local_space_free(ls);
1273 first += 1 + isl_local_space_offset(ls, type);
1274 ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
1275 if (!ls->div)
1276 return isl_local_space_free(ls);
1278 return ls;
1281 /* Does the linear part of "constraint" correspond to
1282 * integer division "div" in "ls"?
1284 * That is, given div = floor((c + f)/m), is the constraint of the form
1286 * f - m d + c' >= 0 [sign = 1]
1287 * or
1288 * -f + m d + c'' >= 0 [sign = -1]
1290 * If so, set *sign to the corresponding value.
1292 static isl_bool is_linear_div_constraint(__isl_keep isl_local_space *ls,
1293 isl_int *constraint, unsigned div, int *sign)
1295 isl_bool unknown;
1296 unsigned pos;
1298 unknown = isl_local_space_div_is_marked_unknown(ls, div);
1299 if (unknown < 0)
1300 return isl_bool_error;
1301 if (unknown)
1302 return isl_bool_false;
1304 pos = isl_local_space_offset(ls, isl_dim_div) + div;
1306 if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
1307 *sign = -1;
1308 if (!isl_seq_is_neg(constraint + 1,
1309 ls->div->row[div] + 2, pos - 1))
1310 return isl_bool_false;
1311 } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
1312 *sign = 1;
1313 if (!isl_seq_eq(constraint + 1, ls->div->row[div] + 2, pos - 1))
1314 return isl_bool_false;
1315 } else {
1316 return isl_bool_false;
1318 if (isl_seq_first_non_zero(constraint + pos + 1,
1319 ls->div->n_row - div - 1) != -1)
1320 return isl_bool_false;
1321 return isl_bool_true;
1324 /* Check if the constraints pointed to by "constraint" is a div
1325 * constraint corresponding to div "div" in "ls".
1327 * That is, if div = floor(f/m), then check if the constraint is
1329 * f - m d >= 0
1330 * or
1331 * -(f-(m-1)) + m d >= 0
1333 * First check if the linear part is of the right form and
1334 * then check the constant term.
1336 isl_bool isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
1337 isl_int *constraint, unsigned div)
1339 int sign;
1340 isl_bool linear;
1342 linear = is_linear_div_constraint(ls, constraint, div, &sign);
1343 if (linear < 0 || !linear)
1344 return linear;
1346 if (sign < 0) {
1347 int neg;
1348 isl_int_sub(ls->div->row[div][1],
1349 ls->div->row[div][1], ls->div->row[div][0]);
1350 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1351 neg = isl_seq_is_neg(constraint, ls->div->row[div] + 1, 1);
1352 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1353 isl_int_add(ls->div->row[div][1],
1354 ls->div->row[div][1], ls->div->row[div][0]);
1355 if (!neg)
1356 return isl_bool_false;
1357 } else {
1358 if (!isl_int_eq(constraint[0], ls->div->row[div][1]))
1359 return isl_bool_false;
1362 return isl_bool_true;
1365 /* Is the constraint pointed to by "constraint" one
1366 * of an equality that corresponds to integer division "div" in "ls"?
1368 * That is, given an integer division of the form
1370 * a = floor((f + c)/m)
1372 * is the equality of the form
1374 * -f + m d + c' = 0
1376 * Note that the constant term is not checked explicitly, but given
1377 * that this is a valid equality constraint, the constant c' necessarily
1378 * has a value close to -c.
1380 isl_bool isl_local_space_is_div_equality(__isl_keep isl_local_space *ls,
1381 isl_int *constraint, unsigned div)
1383 int sign;
1384 isl_bool linear;
1386 linear = is_linear_div_constraint(ls, constraint, div, &sign);
1387 if (linear < 0 || !linear)
1388 return linear;
1390 return isl_bool_ok(sign < 0);
1394 * Set active[i] to 1 if the dimension at position i is involved
1395 * in the linear expression l.
1397 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
1399 int i, j;
1400 isl_ctx *ctx;
1401 int *active = NULL;
1402 isl_size total;
1403 unsigned offset;
1405 ctx = isl_local_space_get_ctx(ls);
1406 total = isl_local_space_dim(ls, isl_dim_all);
1407 if (total < 0)
1408 return NULL;
1409 active = isl_calloc_array(ctx, int, total);
1410 if (total && !active)
1411 return NULL;
1413 for (i = 0; i < total; ++i)
1414 active[i] = !isl_int_is_zero(l[i]);
1416 offset = isl_local_space_offset(ls, isl_dim_div) - 1;
1417 for (i = ls->div->n_row - 1; i >= 0; --i) {
1418 if (!active[offset + i])
1419 continue;
1420 for (j = 0; j < total; ++j)
1421 active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
1424 return active;
1427 /* Given a local space "ls" of a set, create a local space
1428 * for the lift of the set. In particular, the result
1429 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1430 * range of the wrapped map.
1432 __isl_give isl_local_space *isl_local_space_lift(
1433 __isl_take isl_local_space *ls)
1435 ls = isl_local_space_cow(ls);
1436 if (!ls)
1437 return NULL;
1439 ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1440 ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1441 if (!ls->dim || !ls->div)
1442 return isl_local_space_free(ls);
1444 return ls;
1447 /* Construct a basic map that maps a set living in local space "ls"
1448 * to the corresponding lifted local space.
1450 __isl_give isl_basic_map *isl_local_space_lifting(
1451 __isl_take isl_local_space *ls)
1453 isl_basic_map *lifting;
1454 isl_basic_set *bset;
1456 if (!ls)
1457 return NULL;
1458 if (!isl_local_space_is_set(ls))
1459 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1460 "lifting only defined on set spaces", goto error);
1462 bset = isl_basic_set_from_local_space(ls);
1463 lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1464 lifting = isl_basic_map_domain_map(lifting);
1465 lifting = isl_basic_map_reverse(lifting);
1467 return lifting;
1468 error:
1469 isl_local_space_free(ls);
1470 return NULL;
1473 /* Compute the preimage of "ls" under the function represented by "ma".
1474 * In other words, plug in "ma" in "ls". The result is a local space
1475 * that is part of the domain space of "ma".
1477 * If the divs in "ls" are represented as
1479 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1481 * and ma is represented by
1483 * x = D(p) + F(y) + G(divs')
1485 * then the resulting divs are
1487 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1489 * We first copy over the divs from "ma" and then
1490 * we add the modified divs from "ls".
1492 __isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1493 __isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1495 int i;
1496 isl_space *space;
1497 isl_local_space *res = NULL;
1498 isl_size n_div_ls, n_div_ma;
1499 isl_int f, c1, c2, g;
1501 ma = isl_multi_aff_align_divs(ma);
1502 if (!ls || !ma)
1503 goto error;
1504 if (!isl_space_is_range_internal(ls->dim, ma->space))
1505 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1506 "spaces don't match", goto error);
1508 n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1509 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
1510 if (n_div_ls < 0 || n_div_ma < 0)
1511 goto error;
1513 space = isl_space_domain(isl_multi_aff_get_space(ma));
1514 res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1515 if (!res)
1516 goto error;
1518 if (n_div_ma) {
1519 isl_mat_free(res->div);
1520 res->div = isl_mat_copy(ma->u.p[0]->ls->div);
1521 res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1522 res->div = isl_mat_add_rows(res->div, n_div_ls);
1523 if (!res->div)
1524 goto error;
1527 isl_int_init(f);
1528 isl_int_init(c1);
1529 isl_int_init(c2);
1530 isl_int_init(g);
1532 for (i = 0; i < ls->div->n_row; ++i) {
1533 if (isl_int_is_zero(ls->div->row[i][0])) {
1534 isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1535 continue;
1537 if (isl_seq_preimage(res->div->row[n_div_ma + i],
1538 ls->div->row[i],
1539 ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1) < 0)
1540 res = isl_local_space_free(res);
1541 res = normalize_div(res, n_div_ma + i);
1542 if (!res)
1543 break;
1546 isl_int_clear(f);
1547 isl_int_clear(c1);
1548 isl_int_clear(c2);
1549 isl_int_clear(g);
1551 isl_local_space_free(ls);
1552 isl_multi_aff_free(ma);
1553 return res;
1554 error:
1555 isl_local_space_free(ls);
1556 isl_multi_aff_free(ma);
1557 isl_local_space_free(res);
1558 return NULL;
1561 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1562 * to dimensions of "dst_type" at "dst_pos".
1564 * Moving to/from local dimensions is not allowed.
1565 * We currently assume that the dimension type changes.
1567 __isl_give isl_local_space *isl_local_space_move_dims(
1568 __isl_take isl_local_space *ls,
1569 enum isl_dim_type dst_type, unsigned dst_pos,
1570 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1572 unsigned g_dst_pos;
1573 unsigned g_src_pos;
1575 if (!ls)
1576 return NULL;
1577 if (n == 0 &&
1578 !isl_local_space_is_named_or_nested(ls, src_type) &&
1579 !isl_local_space_is_named_or_nested(ls, dst_type))
1580 return ls;
1582 if (isl_local_space_check_range(ls, src_type, src_pos, n) < 0)
1583 return isl_local_space_free(ls);
1584 if (isl_local_space_check_range(ls, dst_type, dst_pos, 0) < 0)
1585 return isl_local_space_free(ls);
1586 if (src_type == isl_dim_div)
1587 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1588 "cannot move divs", return isl_local_space_free(ls));
1589 if (dst_type == isl_dim_div)
1590 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1591 "cannot move to divs", return isl_local_space_free(ls));
1592 if (dst_type == src_type && dst_pos == src_pos)
1593 return ls;
1594 if (dst_type == src_type)
1595 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1596 "moving dims within the same type not supported",
1597 return isl_local_space_free(ls));
1599 ls = isl_local_space_cow(ls);
1600 if (!ls)
1601 return NULL;
1603 g_src_pos = 1 + isl_local_space_offset(ls, src_type) + src_pos;
1604 g_dst_pos = 1 + isl_local_space_offset(ls, dst_type) + dst_pos;
1605 if (dst_type > src_type)
1606 g_dst_pos -= n;
1607 ls->div = isl_mat_move_cols(ls->div, g_dst_pos, g_src_pos, n);
1608 if (!ls->div)
1609 return isl_local_space_free(ls);
1610 ls->dim = isl_space_move_dims(ls->dim, dst_type, dst_pos,
1611 src_type, src_pos, n);
1612 if (!ls->dim)
1613 return isl_local_space_free(ls);
1615 return ls;
1618 /* Remove any internal structure of the domain of "ls".
1619 * If there is any such internal structure in the input,
1620 * then the name of the corresponding space is also removed.
1622 __isl_give isl_local_space *isl_local_space_flatten_domain(
1623 __isl_take isl_local_space *ls)
1625 if (!ls)
1626 return NULL;
1628 if (!ls->dim->nested[0])
1629 return ls;
1631 ls = isl_local_space_cow(ls);
1632 if (!ls)
1633 return NULL;
1635 ls->dim = isl_space_flatten_domain(ls->dim);
1636 if (!ls->dim)
1637 return isl_local_space_free(ls);
1639 return ls;
1642 /* Remove any internal structure of the range of "ls".
1643 * If there is any such internal structure in the input,
1644 * then the name of the corresponding space is also removed.
1646 __isl_give isl_local_space *isl_local_space_flatten_range(
1647 __isl_take isl_local_space *ls)
1649 if (!ls)
1650 return NULL;
1652 if (!ls->dim->nested[1])
1653 return ls;
1655 ls = isl_local_space_cow(ls);
1656 if (!ls)
1657 return NULL;
1659 ls->dim = isl_space_flatten_range(ls->dim);
1660 if (!ls->dim)
1661 return isl_local_space_free(ls);
1663 return ls;
1666 /* Given the local space "ls" of a map, return the local space of a set
1667 * that lives in a space that wraps the space of "ls" and that has
1668 * the same divs.
1670 __isl_give isl_local_space *isl_local_space_wrap(__isl_take isl_local_space *ls)
1672 ls = isl_local_space_cow(ls);
1673 if (!ls)
1674 return NULL;
1676 ls->dim = isl_space_wrap(ls->dim);
1677 if (!ls->dim)
1678 return isl_local_space_free(ls);
1680 return ls;
1683 /* Lift the point "pnt", living in the space of "ls"
1684 * to live in a space with extra coordinates corresponding
1685 * to the local variables of "ls".
1687 __isl_give isl_point *isl_local_space_lift_point(__isl_take isl_local_space *ls,
1688 __isl_take isl_point *pnt)
1690 isl_size n_local;
1691 isl_space *space;
1692 isl_local *local;
1693 isl_vec *vec;
1695 if (isl_local_space_check_has_space(ls, isl_point_peek_space(pnt)) < 0)
1696 goto error;
1698 local = isl_local_space_peek_local(ls);
1699 n_local = isl_local_space_dim(ls, isl_dim_div);
1700 if (n_local < 0)
1701 goto error;
1703 space = isl_point_take_space(pnt);
1704 vec = isl_point_take_vec(pnt);
1706 space = isl_space_lift(space, n_local);
1707 vec = isl_local_extend_point_vec(local, vec);
1709 pnt = isl_point_restore_vec(pnt, vec);
1710 pnt = isl_point_restore_space(pnt, space);
1712 isl_local_space_free(ls);
1714 return pnt;
1715 error:
1716 isl_local_space_free(ls);
1717 isl_point_free(pnt);
1718 return NULL;