isl_morph.c: isl_morph_set: extract out isl_set_basic_set_check_equal_space
[isl.git] / isl_local_space.c
blob2ef9ed0c8689072c8548ceda926cc7e87acd29a3
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 #undef TYPE
162 #define TYPE isl_local_space
164 #include "isl_type_has_equal_space_bin_templ.c"
166 /* Is the space of "ls" equal to "space"?
168 isl_bool isl_local_space_has_space(__isl_keep isl_local_space *ls,
169 __isl_keep isl_space *space)
171 return isl_space_is_equal(isl_local_space_peek_space(ls), space);
174 /* Check that the space of "ls" is equal to "space".
176 static isl_stat isl_local_space_check_has_space(__isl_keep isl_local_space *ls,
177 __isl_keep isl_space *space)
179 isl_bool ok;
181 ok = isl_local_space_has_space(ls, space);
182 if (ok < 0)
183 return isl_stat_error;
184 if (!ok)
185 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
186 "spaces don't match", return isl_stat_error);
187 return isl_stat_ok;
190 /* Return true if the two local spaces are identical, with identical
191 * expressions for the integer divisions.
193 isl_bool isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
194 __isl_keep isl_local_space *ls2)
196 isl_bool equal;
198 equal = isl_local_space_has_equal_space(ls1, ls2);
199 if (equal < 0 || !equal)
200 return equal;
202 if (!isl_local_space_divs_known(ls1))
203 return isl_bool_false;
204 if (!isl_local_space_divs_known(ls2))
205 return isl_bool_false;
207 return isl_mat_is_equal(ls1->div, ls2->div);
210 /* Compare two isl_local_spaces.
212 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
213 * than "ls2" and 0 if they are equal.
215 int isl_local_space_cmp(__isl_keep isl_local_space *ls1,
216 __isl_keep isl_local_space *ls2)
218 int cmp;
220 if (ls1 == ls2)
221 return 0;
222 if (!ls1)
223 return -1;
224 if (!ls2)
225 return 1;
227 cmp = isl_space_cmp(ls1->dim, ls2->dim);
228 if (cmp != 0)
229 return cmp;
231 return isl_local_cmp(ls1->div, ls2->div);
234 isl_size isl_local_space_dim(__isl_keep isl_local_space *ls,
235 enum isl_dim_type type)
237 if (!ls)
238 return isl_size_error;
239 if (type == isl_dim_div)
240 return ls->div->n_row;
241 if (type == isl_dim_all) {
242 isl_size dim = isl_space_dim(ls->dim, isl_dim_all);
243 if (dim < 0)
244 return isl_size_error;
245 return dim + ls->div->n_row;
247 return isl_space_dim(ls->dim, type);
250 #undef TYPE
251 #define TYPE isl_local_space
252 #include "check_type_range_templ.c"
254 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
255 enum isl_dim_type type)
257 isl_space *space;
259 if (!ls)
260 return 0;
262 space = ls->dim;
263 switch (type) {
264 case isl_dim_cst: return 0;
265 case isl_dim_param: return 1;
266 case isl_dim_in: return 1 + space->nparam;
267 case isl_dim_out: return 1 + space->nparam + space->n_in;
268 case isl_dim_div:
269 return 1 + space->nparam + space->n_in + space->n_out;
270 default: return 0;
274 /* Return the position of the dimension of the given type and name
275 * in "ls".
276 * Return -1 if no such dimension can be found.
278 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space *ls,
279 enum isl_dim_type type, const char *name)
281 if (!ls)
282 return -1;
283 if (type == isl_dim_div)
284 return -1;
285 return isl_space_find_dim_by_name(ls->dim, type, name);
288 /* Does the given dimension have a name?
290 isl_bool isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
291 enum isl_dim_type type, unsigned pos)
293 return ls ? isl_space_has_dim_name(ls->dim, type, pos) : isl_bool_error;
296 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
297 enum isl_dim_type type, unsigned pos)
299 return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
302 isl_bool isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
303 enum isl_dim_type type, unsigned pos)
305 return ls ? isl_space_has_dim_id(ls->dim, type, pos) : isl_bool_error;
308 __isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
309 enum isl_dim_type type, unsigned pos)
311 return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
314 /* Return the argument of the integer division at position "pos" in "ls".
315 * All local variables in "ls" are known to have a (complete) explicit
316 * representation.
318 static __isl_give isl_aff *extract_div(__isl_keep isl_local_space *ls, int pos)
320 isl_aff *aff;
322 aff = isl_aff_alloc(isl_local_space_copy(ls));
323 if (!aff)
324 return NULL;
325 isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
326 return aff;
329 /* Return the argument of the integer division at position "pos" in "ls".
330 * The integer division at that position is known to have a complete
331 * explicit representation, but some of the others do not.
332 * Remove them first because the domain of an isl_aff
333 * is not allowed to have unknown local variables.
335 static __isl_give isl_aff *drop_unknown_divs_and_extract_div(
336 __isl_keep isl_local_space *ls, int pos)
338 int i;
339 isl_size n;
340 isl_bool unknown;
341 isl_aff *aff;
343 n = isl_local_space_dim(ls, isl_dim_div);
344 if (n < 0)
345 return NULL;
346 ls = isl_local_space_copy(ls);
347 for (i = n - 1; i >= 0; --i) {
348 unknown = isl_local_space_div_is_marked_unknown(ls, i);
349 if (unknown < 0)
350 ls = isl_local_space_free(ls);
351 else if (!unknown)
352 continue;
353 ls = isl_local_space_drop_dims(ls, isl_dim_div, i, 1);
354 if (pos > i)
355 --pos;
357 aff = extract_div(ls, pos);
358 isl_local_space_free(ls);
359 return aff;
362 /* Return the argument of the integer division at position "pos" in "ls".
363 * The integer division is assumed to have a complete explicit
364 * representation. If some of the other integer divisions
365 * do not have an explicit representation, then they need
366 * to be removed first because the domain of an isl_aff
367 * is not allowed to have unknown local variables.
369 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
370 int pos)
372 isl_bool known;
374 if (!ls)
375 return NULL;
377 if (pos < 0 || pos >= ls->div->n_row)
378 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
379 "index out of bounds", return NULL);
381 known = isl_local_space_div_is_known(ls, pos);
382 if (known < 0)
383 return NULL;
384 if (!known)
385 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
386 "expression of div unknown", return NULL);
387 if (!isl_local_space_is_set(ls))
388 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
389 "cannot represent divs of map spaces", return NULL);
391 known = isl_local_space_divs_known(ls);
392 if (known < 0)
393 return NULL;
394 if (known)
395 return extract_div(ls, pos);
396 else
397 return drop_unknown_divs_and_extract_div(ls, pos);
400 /* Return the space of "ls".
402 __isl_keep isl_space *isl_local_space_peek_space(__isl_keep isl_local_space *ls)
404 if (!ls)
405 return NULL;
407 return ls->dim;
410 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
412 return isl_space_copy(isl_local_space_peek_space(ls));
415 /* Return the space of "ls".
416 * This may be either a copy or the space itself
417 * if there is only one reference to "ls".
418 * This allows the space to be modified inplace
419 * if both the local space and its space have only a single reference.
420 * The caller is not allowed to modify "ls" between this call and
421 * a subsequent call to isl_local_space_restore_space.
422 * The only exception is that isl_local_space_free can be called instead.
424 __isl_give isl_space *isl_local_space_take_space(__isl_keep isl_local_space *ls)
426 isl_space *space;
428 if (!ls)
429 return NULL;
430 if (ls->ref != 1)
431 return isl_local_space_get_space(ls);
432 space = ls->dim;
433 ls->dim = NULL;
434 return space;
437 /* Set the space of "ls" to "space", where the space of "ls" may be missing
438 * due to a preceding call to isl_local_space_take_space.
439 * However, in this case, "ls" only has a single reference and
440 * then the call to isl_local_space_cow has no effect.
442 __isl_give isl_local_space *isl_local_space_restore_space(
443 __isl_take isl_local_space *ls, __isl_take isl_space *space)
445 if (!ls || !space)
446 goto error;
448 if (ls->dim == space) {
449 isl_space_free(space);
450 return ls;
453 ls = isl_local_space_cow(ls);
454 if (!ls)
455 goto error;
456 isl_space_free(ls->dim);
457 ls->dim = space;
459 return ls;
460 error:
461 isl_local_space_free(ls);
462 isl_space_free(space);
463 return NULL;
466 /* Return the local variables of "ls".
468 __isl_keep isl_local *isl_local_space_peek_local(__isl_keep isl_local_space *ls)
470 return ls ? ls->div : NULL;
473 /* Replace the identifier of the tuple of type "type" by "id".
475 __isl_give isl_local_space *isl_local_space_set_tuple_id(
476 __isl_take isl_local_space *ls,
477 enum isl_dim_type type, __isl_take isl_id *id)
479 ls = isl_local_space_cow(ls);
480 if (!ls)
481 goto error;
482 ls->dim = isl_space_set_tuple_id(ls->dim, type, id);
483 if (!ls->dim)
484 return isl_local_space_free(ls);
485 return ls;
486 error:
487 isl_id_free(id);
488 return NULL;
491 __isl_give isl_local_space *isl_local_space_set_dim_name(
492 __isl_take isl_local_space *ls,
493 enum isl_dim_type type, unsigned pos, const char *s)
495 ls = isl_local_space_cow(ls);
496 if (!ls)
497 return NULL;
498 ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
499 if (!ls->dim)
500 return isl_local_space_free(ls);
502 return ls;
505 __isl_give isl_local_space *isl_local_space_set_dim_id(
506 __isl_take isl_local_space *ls,
507 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
509 ls = isl_local_space_cow(ls);
510 if (!ls)
511 goto error;
512 ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
513 if (!ls->dim)
514 return isl_local_space_free(ls);
516 return ls;
517 error:
518 isl_id_free(id);
519 return NULL;
522 /* Construct a zero-dimensional local space with the given parameter domain.
524 __isl_give isl_local_space *isl_local_space_set_from_params(
525 __isl_take isl_local_space *ls)
527 isl_space *space;
529 space = isl_local_space_take_space(ls);
530 space = isl_space_set_from_params(space);
531 ls = isl_local_space_restore_space(ls, space);
533 return ls;
536 __isl_give isl_local_space *isl_local_space_reset_space(
537 __isl_take isl_local_space *ls, __isl_take isl_space *dim)
539 ls = isl_local_space_cow(ls);
540 if (!ls || !dim)
541 goto error;
543 isl_space_free(ls->dim);
544 ls->dim = dim;
546 return ls;
547 error:
548 isl_local_space_free(ls);
549 isl_space_free(dim);
550 return NULL;
553 /* Reorder the dimensions of "ls" according to the given reordering.
554 * The reordering r is assumed to have been extended with the local
555 * variables, leaving them in the same order.
557 __isl_give isl_local_space *isl_local_space_realign(
558 __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
560 ls = isl_local_space_cow(ls);
561 if (!ls || !r)
562 goto error;
564 ls->div = isl_local_reorder(ls->div, isl_reordering_copy(r));
565 if (!ls->div)
566 goto error;
568 ls = isl_local_space_reset_space(ls, isl_reordering_get_space(r));
570 isl_reordering_free(r);
571 return ls;
572 error:
573 isl_local_space_free(ls);
574 isl_reordering_free(r);
575 return NULL;
578 __isl_give isl_local_space *isl_local_space_add_div(
579 __isl_take isl_local_space *ls, __isl_take isl_vec *div)
581 ls = isl_local_space_cow(ls);
582 if (!ls || !div)
583 goto error;
585 if (ls->div->n_col != div->size)
586 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
587 "incompatible dimensions", goto error);
589 ls->div = isl_mat_add_zero_cols(ls->div, 1);
590 ls->div = isl_mat_add_rows(ls->div, 1);
591 if (!ls->div)
592 goto error;
594 isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
595 isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
597 isl_vec_free(div);
598 return ls;
599 error:
600 isl_local_space_free(ls);
601 isl_vec_free(div);
602 return NULL;
605 __isl_give isl_local_space *isl_local_space_replace_divs(
606 __isl_take isl_local_space *ls, __isl_take isl_mat *div)
608 ls = isl_local_space_cow(ls);
610 if (!ls || !div)
611 goto error;
613 isl_mat_free(ls->div);
614 ls->div = div;
615 return ls;
616 error:
617 isl_mat_free(div);
618 isl_local_space_free(ls);
619 return NULL;
622 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
623 * defined by "exp".
625 static void expand_row(__isl_keep isl_mat *dst, int d,
626 __isl_keep isl_mat *src, int s, int *exp)
628 int i;
629 unsigned c = src->n_col - src->n_row;
631 isl_seq_cpy(dst->row[d], src->row[s], c);
632 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
634 for (i = 0; i < s; ++i)
635 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
638 /* Compare (known) divs.
639 * Return non-zero if at least one of the two divs is unknown.
640 * In particular, if both divs are unknown, we respect their
641 * current order. Otherwise, we sort the known div after the unknown
642 * div only if the known div depends on the unknown div.
644 static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
645 unsigned n_row, unsigned n_col)
647 int li, lj;
648 int unknown_i, unknown_j;
650 unknown_i = isl_int_is_zero(row_i[0]);
651 unknown_j = isl_int_is_zero(row_j[0]);
653 if (unknown_i && unknown_j)
654 return i - j;
656 if (unknown_i)
657 li = n_col - n_row + i;
658 else
659 li = isl_seq_last_non_zero(row_i, n_col);
660 if (unknown_j)
661 lj = n_col - n_row + j;
662 else
663 lj = isl_seq_last_non_zero(row_j, n_col);
665 if (li != lj)
666 return li - lj;
668 return isl_seq_cmp(row_i, row_j, n_col);
671 /* Call cmp_row for divs in a matrix.
673 int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
675 return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
678 /* Call cmp_row for divs in a basic map.
680 static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
681 unsigned total)
683 return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
686 /* Sort the divs in "bmap".
688 * We first make sure divs are placed after divs on which they depend.
689 * Then we perform a simple insertion sort based on the same ordering
690 * that is used in isl_merge_divs.
692 __isl_give isl_basic_map *isl_basic_map_sort_divs(
693 __isl_take isl_basic_map *bmap)
695 int i, j;
696 isl_size total;
698 bmap = isl_basic_map_order_divs(bmap);
699 if (!bmap)
700 return NULL;
701 if (bmap->n_div <= 1)
702 return bmap;
704 total = isl_basic_map_dim(bmap, isl_dim_all);
705 if (total < 0)
706 return isl_basic_map_free(bmap);
707 for (i = 1; i < bmap->n_div; ++i) {
708 for (j = i - 1; j >= 0; --j) {
709 if (bmap_cmp_row(bmap, j, j + 1, 2 + total) <= 0)
710 break;
711 bmap = isl_basic_map_swap_div(bmap, j, j + 1);
712 if (!bmap)
713 return NULL;
717 return bmap;
720 /* Sort the divs in the basic maps of "map".
722 __isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
724 return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
727 /* Combine the two lists of divs into a single list.
728 * For each row i in div1, exp1[i] is set to the position of the corresponding
729 * row in the result. Similarly for div2 and exp2.
730 * This function guarantees
731 * exp1[i] >= i
732 * exp1[i+1] > exp1[i]
733 * For optimal merging, the two input list should have been sorted.
735 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
736 __isl_keep isl_mat *div2, int *exp1, int *exp2)
738 int i, j, k;
739 isl_mat *div = NULL;
740 unsigned d;
742 if (!div1 || !div2)
743 return NULL;
745 d = div1->n_col - div1->n_row;
746 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
747 d + div1->n_row + div2->n_row);
748 if (!div)
749 return NULL;
751 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
752 int cmp;
754 expand_row(div, k, div1, i, exp1);
755 expand_row(div, k + 1, div2, j, exp2);
757 cmp = isl_mat_cmp_div(div, k, k + 1);
758 if (cmp == 0) {
759 exp1[i++] = k;
760 exp2[j++] = k;
761 } else if (cmp < 0) {
762 exp1[i++] = k;
763 } else {
764 exp2[j++] = k;
765 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
768 for (; i < div1->n_row; ++i, ++k) {
769 expand_row(div, k, div1, i, exp1);
770 exp1[i] = k;
772 for (; j < div2->n_row; ++j, ++k) {
773 expand_row(div, k, div2, j, exp2);
774 exp2[j] = k;
777 div->n_row = k;
778 div->n_col = d + k;
780 return div;
783 /* Swap divs "a" and "b" in "ls".
785 __isl_give isl_local_space *isl_local_space_swap_div(
786 __isl_take isl_local_space *ls, int a, int b)
788 int offset;
790 ls = isl_local_space_cow(ls);
791 if (!ls)
792 return NULL;
793 if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
794 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
795 "index out of bounds", return isl_local_space_free(ls));
796 offset = ls->div->n_col - ls->div->n_row;
797 ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
798 ls->div = isl_mat_swap_rows(ls->div, a, b);
799 if (!ls->div)
800 return isl_local_space_free(ls);
801 return ls;
804 /* Construct a local space that contains all the divs in either
805 * "ls1" or "ls2".
807 __isl_give isl_local_space *isl_local_space_intersect(
808 __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
810 isl_ctx *ctx;
811 int *exp1 = NULL;
812 int *exp2 = NULL;
813 isl_mat *div = NULL;
814 isl_bool equal;
816 if (!ls1 || !ls2)
817 goto error;
819 ctx = isl_local_space_get_ctx(ls1);
820 if (!isl_space_is_equal(ls1->dim, ls2->dim))
821 isl_die(ctx, isl_error_invalid,
822 "spaces should be identical", goto error);
824 if (ls2->div->n_row == 0) {
825 isl_local_space_free(ls2);
826 return ls1;
829 if (ls1->div->n_row == 0) {
830 isl_local_space_free(ls1);
831 return ls2;
834 exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
835 exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
836 if (!exp1 || !exp2)
837 goto error;
839 div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
840 if (!div)
841 goto error;
843 equal = isl_mat_is_equal(ls1->div, div);
844 if (equal < 0)
845 goto error;
846 if (!equal)
847 ls1 = isl_local_space_cow(ls1);
848 if (!ls1)
849 goto error;
851 free(exp1);
852 free(exp2);
853 isl_local_space_free(ls2);
854 isl_mat_free(ls1->div);
855 ls1->div = div;
857 return ls1;
858 error:
859 free(exp1);
860 free(exp2);
861 isl_mat_free(div);
862 isl_local_space_free(ls1);
863 isl_local_space_free(ls2);
864 return NULL;
867 /* Is the local variable "div" of "ls" marked as not having
868 * an explicit representation?
869 * Note that even if this variable is not marked in this way and therefore
870 * does have an explicit representation, this representation may still
871 * depend (indirectly) on other local variables that do not
872 * have an explicit representation.
874 isl_bool isl_local_space_div_is_marked_unknown(__isl_keep isl_local_space *ls,
875 int div)
877 if (!ls)
878 return isl_bool_error;
879 return isl_local_div_is_marked_unknown(ls->div, div);
882 /* Does "ls" have a complete explicit representation for div "div"?
884 isl_bool isl_local_space_div_is_known(__isl_keep isl_local_space *ls, int div)
886 if (!ls)
887 return isl_bool_error;
888 return isl_local_div_is_known(ls->div, div);
891 /* Does "ls" have an explicit representation for all local variables?
893 isl_bool isl_local_space_divs_known(__isl_keep isl_local_space *ls)
895 if (!ls)
896 return isl_bool_error;
897 return isl_local_divs_known(ls->div);
900 __isl_give isl_local_space *isl_local_space_domain(
901 __isl_take isl_local_space *ls)
903 isl_size n_out;
905 n_out = isl_local_space_dim(ls, isl_dim_out);
906 if (n_out < 0)
907 return isl_local_space_free(ls);
908 ls = isl_local_space_drop_dims(ls, isl_dim_out, 0, n_out);
909 ls = isl_local_space_cow(ls);
910 if (!ls)
911 return NULL;
912 ls->dim = isl_space_domain(ls->dim);
913 if (!ls->dim)
914 return isl_local_space_free(ls);
915 return ls;
918 __isl_give isl_local_space *isl_local_space_range(
919 __isl_take isl_local_space *ls)
921 isl_size n_in;
923 n_in = isl_local_space_dim(ls, isl_dim_in);
924 if (n_in < 0)
925 return isl_local_space_free(ls);
926 ls = isl_local_space_drop_dims(ls, isl_dim_in, 0, n_in);
927 ls = isl_local_space_cow(ls);
928 if (!ls)
929 return NULL;
931 ls->dim = isl_space_range(ls->dim);
932 if (!ls->dim)
933 return isl_local_space_free(ls);
934 return ls;
937 /* Construct a local space for a map that has the given local
938 * space as domain and that has a zero-dimensional range.
940 __isl_give isl_local_space *isl_local_space_from_domain(
941 __isl_take isl_local_space *ls)
943 ls = isl_local_space_cow(ls);
944 if (!ls)
945 return NULL;
946 ls->dim = isl_space_from_domain(ls->dim);
947 if (!ls->dim)
948 return isl_local_space_free(ls);
949 return ls;
952 __isl_give isl_local_space *isl_local_space_add_dims(
953 __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
955 isl_size pos;
957 pos = isl_local_space_dim(ls, type);
958 if (pos < 0)
959 return isl_local_space_free(ls);
960 return isl_local_space_insert_dims(ls, type, pos, n);
963 /* Lift the basic set "bset", living in the space of "ls"
964 * to live in a space with extra coordinates corresponding
965 * to the local variables of "ls".
967 __isl_give isl_basic_set *isl_local_space_lift_basic_set(
968 __isl_take isl_local_space *ls, __isl_take isl_basic_set *bset)
970 isl_size n_local;
971 isl_space *space;
972 isl_basic_set *ls_bset;
974 n_local = isl_local_space_dim(ls, isl_dim_div);
975 space = isl_basic_set_peek_space(bset);
976 if (n_local < 0 ||
977 isl_local_space_check_has_space(ls, space) < 0)
978 goto error;
980 if (n_local == 0) {
981 isl_local_space_free(ls);
982 return bset;
985 bset = isl_basic_set_add_dims(bset, isl_dim_set, n_local);
986 ls_bset = isl_basic_set_from_local_space(ls);
987 ls_bset = isl_basic_set_lift(ls_bset);
988 ls_bset = isl_basic_set_flatten(ls_bset);
989 bset = isl_basic_set_intersect(bset, ls_bset);
991 return bset;
992 error:
993 isl_local_space_free(ls);
994 isl_basic_set_free(bset);
995 return NULL;
998 /* Lift the set "set", living in the space of "ls"
999 * to live in a space with extra coordinates corresponding
1000 * to the local variables of "ls".
1002 __isl_give isl_set *isl_local_space_lift_set(__isl_take isl_local_space *ls,
1003 __isl_take isl_set *set)
1005 isl_size n_local;
1006 isl_basic_set *bset;
1008 n_local = isl_local_space_dim(ls, isl_dim_div);
1009 if (n_local < 0 ||
1010 isl_local_space_check_has_space(ls, isl_set_peek_space(set)) < 0)
1011 goto error;
1013 if (n_local == 0) {
1014 isl_local_space_free(ls);
1015 return set;
1018 set = isl_set_add_dims(set, isl_dim_set, n_local);
1019 bset = isl_basic_set_from_local_space(ls);
1020 bset = isl_basic_set_lift(bset);
1021 bset = isl_basic_set_flatten(bset);
1022 set = isl_set_intersect(set, isl_set_from_basic_set(bset));
1024 return set;
1025 error:
1026 isl_local_space_free(ls);
1027 isl_set_free(set);
1028 return NULL;
1031 /* Remove common factor of non-constant terms and denominator.
1033 static __isl_give isl_local_space *normalize_div(
1034 __isl_take isl_local_space *ls, int div)
1036 isl_ctx *ctx = ls->div->ctx;
1037 unsigned total = ls->div->n_col - 2;
1039 isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
1040 isl_int_gcd(ctx->normalize_gcd,
1041 ctx->normalize_gcd, ls->div->row[div][0]);
1042 if (isl_int_is_one(ctx->normalize_gcd))
1043 return ls;
1045 isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
1046 ctx->normalize_gcd, total);
1047 isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
1048 ctx->normalize_gcd);
1049 isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
1050 ctx->normalize_gcd);
1052 return ls;
1055 /* Exploit the equalities in "eq" to simplify the expressions of
1056 * the integer divisions in "ls".
1057 * The integer divisions in "ls" are assumed to appear as regular
1058 * dimensions in "eq".
1060 __isl_give isl_local_space *isl_local_space_substitute_equalities(
1061 __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
1063 int i, j, k;
1064 isl_size total, dim;
1065 unsigned n_div;
1067 if (!ls || !eq)
1068 goto error;
1070 total = isl_space_dim(eq->dim, isl_dim_all);
1071 dim = isl_local_space_dim(ls, isl_dim_all);
1072 if (dim < 0 || total < 0)
1073 goto error;
1074 if (dim != total)
1075 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1076 "spaces don't match", goto error);
1077 total++;
1078 n_div = eq->n_div;
1079 for (i = 0; i < eq->n_eq; ++i) {
1080 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1081 if (j < 0 || j == 0 || j >= total)
1082 continue;
1084 for (k = 0; k < ls->div->n_row; ++k) {
1085 if (isl_int_is_zero(ls->div->row[k][1 + j]))
1086 continue;
1087 ls = isl_local_space_cow(ls);
1088 if (!ls)
1089 goto error;
1090 ls->div = isl_mat_cow(ls->div);
1091 if (!ls->div)
1092 goto error;
1093 isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
1094 &ls->div->row[k][0]);
1095 ls = normalize_div(ls, k);
1096 if (!ls)
1097 goto error;
1101 isl_basic_set_free(eq);
1102 return ls;
1103 error:
1104 isl_basic_set_free(eq);
1105 isl_local_space_free(ls);
1106 return NULL;
1109 /* Plug in the affine expressions "subs" of length "subs_len" (including
1110 * the denominator and the constant term) into the variable at position "pos"
1111 * of the "n" div expressions starting at "first".
1113 * Let i be the dimension to replace and let "subs" be of the form
1115 * f/d
1117 * Any integer division starting at "first" with a non-zero coefficient for i,
1119 * floor((a i + g)/m)
1121 * is replaced by
1123 * floor((a f + d g)/(m d))
1125 __isl_give isl_local_space *isl_local_space_substitute_seq(
1126 __isl_take isl_local_space *ls,
1127 enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
1128 int first, int n)
1130 int i;
1131 isl_int v;
1133 if (n == 0)
1134 return ls;
1135 ls = isl_local_space_cow(ls);
1136 if (!ls)
1137 return NULL;
1138 ls->div = isl_mat_cow(ls->div);
1139 if (!ls->div)
1140 return isl_local_space_free(ls);
1142 if (first + n > ls->div->n_row)
1143 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1144 "index out of bounds", return isl_local_space_free(ls));
1146 pos += isl_local_space_offset(ls, type);
1148 isl_int_init(v);
1149 for (i = first; i < first + n; ++i) {
1150 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
1151 continue;
1152 isl_seq_substitute(ls->div->row[i], pos, subs,
1153 ls->div->n_col, subs_len, v);
1154 ls = normalize_div(ls, i);
1155 if (!ls)
1156 break;
1158 isl_int_clear(v);
1160 return ls;
1163 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
1164 * of "ls".
1166 * Let i be the dimension to replace and let "subs" be of the form
1168 * f/d
1170 * Any integer division with a non-zero coefficient for i,
1172 * floor((a i + g)/m)
1174 * is replaced by
1176 * floor((a f + d g)/(m d))
1178 __isl_give isl_local_space *isl_local_space_substitute(
1179 __isl_take isl_local_space *ls,
1180 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
1182 isl_size n_div;
1184 ls = isl_local_space_cow(ls);
1185 if (!ls || !subs)
1186 return isl_local_space_free(ls);
1188 if (!isl_space_is_equal(ls->dim, subs->ls->dim))
1189 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1190 "spaces don't match", return isl_local_space_free(ls));
1191 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
1192 if (n_div < 0)
1193 return isl_local_space_free(ls);
1194 if (n_div != 0)
1195 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1196 "cannot handle divs yet",
1197 return isl_local_space_free(ls));
1199 return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
1200 subs->v->size, 0, ls->div->n_row);
1203 isl_bool isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
1204 enum isl_dim_type type)
1206 if (!ls)
1207 return isl_bool_error;
1208 return isl_space_is_named_or_nested(ls->dim, type);
1211 __isl_give isl_local_space *isl_local_space_drop_dims(
1212 __isl_take isl_local_space *ls,
1213 enum isl_dim_type type, unsigned first, unsigned n)
1215 if (!ls)
1216 return NULL;
1217 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1218 return ls;
1220 if (isl_local_space_check_range(ls, type, first, n) < 0)
1221 return isl_local_space_free(ls);
1223 ls = isl_local_space_cow(ls);
1224 if (!ls)
1225 return NULL;
1227 if (type == isl_dim_div) {
1228 ls->div = isl_mat_drop_rows(ls->div, first, n);
1229 } else {
1230 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
1231 if (!ls->dim)
1232 return isl_local_space_free(ls);
1235 first += 1 + isl_local_space_offset(ls, type);
1236 ls->div = isl_mat_drop_cols(ls->div, first, n);
1237 if (!ls->div)
1238 return isl_local_space_free(ls);
1240 return ls;
1243 __isl_give isl_local_space *isl_local_space_insert_dims(
1244 __isl_take isl_local_space *ls,
1245 enum isl_dim_type type, unsigned first, unsigned n)
1247 if (!ls)
1248 return NULL;
1249 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1250 return ls;
1252 if (isl_local_space_check_range(ls, type, first, 0) < 0)
1253 return isl_local_space_free(ls);
1255 ls = isl_local_space_cow(ls);
1256 if (!ls)
1257 return NULL;
1259 if (type == isl_dim_div) {
1260 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
1261 } else {
1262 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
1263 if (!ls->dim)
1264 return isl_local_space_free(ls);
1267 first += 1 + isl_local_space_offset(ls, type);
1268 ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
1269 if (!ls->div)
1270 return isl_local_space_free(ls);
1272 return ls;
1275 /* Does the linear part of "constraint" correspond to
1276 * integer division "div" in "ls"?
1278 * That is, given div = floor((c + f)/m), is the constraint of the form
1280 * f - m d + c' >= 0 [sign = 1]
1281 * or
1282 * -f + m d + c'' >= 0 [sign = -1]
1284 * If so, set *sign to the corresponding value.
1286 static isl_bool is_linear_div_constraint(__isl_keep isl_local_space *ls,
1287 isl_int *constraint, unsigned div, int *sign)
1289 isl_bool unknown;
1290 unsigned pos;
1292 unknown = isl_local_space_div_is_marked_unknown(ls, div);
1293 if (unknown < 0)
1294 return isl_bool_error;
1295 if (unknown)
1296 return isl_bool_false;
1298 pos = isl_local_space_offset(ls, isl_dim_div) + div;
1300 if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
1301 *sign = -1;
1302 if (!isl_seq_is_neg(constraint + 1,
1303 ls->div->row[div] + 2, pos - 1))
1304 return isl_bool_false;
1305 } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
1306 *sign = 1;
1307 if (!isl_seq_eq(constraint + 1, ls->div->row[div] + 2, pos - 1))
1308 return isl_bool_false;
1309 } else {
1310 return isl_bool_false;
1312 if (isl_seq_first_non_zero(constraint + pos + 1,
1313 ls->div->n_row - div - 1) != -1)
1314 return isl_bool_false;
1315 return isl_bool_true;
1318 /* Check if the constraints pointed to by "constraint" is a div
1319 * constraint corresponding to div "div" in "ls".
1321 * That is, if div = floor(f/m), then check if the constraint is
1323 * f - m d >= 0
1324 * or
1325 * -(f-(m-1)) + m d >= 0
1327 * First check if the linear part is of the right form and
1328 * then check the constant term.
1330 isl_bool isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
1331 isl_int *constraint, unsigned div)
1333 int sign;
1334 isl_bool linear;
1336 linear = is_linear_div_constraint(ls, constraint, div, &sign);
1337 if (linear < 0 || !linear)
1338 return linear;
1340 if (sign < 0) {
1341 int neg;
1342 isl_int_sub(ls->div->row[div][1],
1343 ls->div->row[div][1], ls->div->row[div][0]);
1344 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1345 neg = isl_seq_is_neg(constraint, ls->div->row[div] + 1, 1);
1346 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1347 isl_int_add(ls->div->row[div][1],
1348 ls->div->row[div][1], ls->div->row[div][0]);
1349 if (!neg)
1350 return isl_bool_false;
1351 } else {
1352 if (!isl_int_eq(constraint[0], ls->div->row[div][1]))
1353 return isl_bool_false;
1356 return isl_bool_true;
1359 /* Is the constraint pointed to by "constraint" one
1360 * of an equality that corresponds to integer division "div" in "ls"?
1362 * That is, given an integer division of the form
1364 * a = floor((f + c)/m)
1366 * is the equality of the form
1368 * -f + m d + c' = 0
1370 * Note that the constant term is not checked explicitly, but given
1371 * that this is a valid equality constraint, the constant c' necessarily
1372 * has a value close to -c.
1374 isl_bool isl_local_space_is_div_equality(__isl_keep isl_local_space *ls,
1375 isl_int *constraint, unsigned div)
1377 int sign;
1378 isl_bool linear;
1380 linear = is_linear_div_constraint(ls, constraint, div, &sign);
1381 if (linear < 0 || !linear)
1382 return linear;
1384 return isl_bool_ok(sign < 0);
1388 * Set active[i] to 1 if the dimension at position i is involved
1389 * in the linear expression l.
1391 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
1393 int i, j;
1394 isl_ctx *ctx;
1395 int *active = NULL;
1396 isl_size total;
1397 unsigned offset;
1399 ctx = isl_local_space_get_ctx(ls);
1400 total = isl_local_space_dim(ls, isl_dim_all);
1401 if (total < 0)
1402 return NULL;
1403 active = isl_calloc_array(ctx, int, total);
1404 if (total && !active)
1405 return NULL;
1407 for (i = 0; i < total; ++i)
1408 active[i] = !isl_int_is_zero(l[i]);
1410 offset = isl_local_space_offset(ls, isl_dim_div) - 1;
1411 for (i = ls->div->n_row - 1; i >= 0; --i) {
1412 if (!active[offset + i])
1413 continue;
1414 for (j = 0; j < total; ++j)
1415 active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
1418 return active;
1421 /* Given a local space "ls" of a set, create a local space
1422 * for the lift of the set. In particular, the result
1423 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1424 * range of the wrapped map.
1426 __isl_give isl_local_space *isl_local_space_lift(
1427 __isl_take isl_local_space *ls)
1429 ls = isl_local_space_cow(ls);
1430 if (!ls)
1431 return NULL;
1433 ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1434 ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1435 if (!ls->dim || !ls->div)
1436 return isl_local_space_free(ls);
1438 return ls;
1441 /* Construct a basic map that maps a set living in local space "ls"
1442 * to the corresponding lifted local space.
1444 __isl_give isl_basic_map *isl_local_space_lifting(
1445 __isl_take isl_local_space *ls)
1447 isl_basic_map *lifting;
1448 isl_basic_set *bset;
1450 if (!ls)
1451 return NULL;
1452 if (!isl_local_space_is_set(ls))
1453 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1454 "lifting only defined on set spaces", goto error);
1456 bset = isl_basic_set_from_local_space(ls);
1457 lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1458 lifting = isl_basic_map_domain_map(lifting);
1459 lifting = isl_basic_map_reverse(lifting);
1461 return lifting;
1462 error:
1463 isl_local_space_free(ls);
1464 return NULL;
1467 /* Compute the preimage of "ls" under the function represented by "ma".
1468 * In other words, plug in "ma" in "ls". The result is a local space
1469 * that is part of the domain space of "ma".
1471 * If the divs in "ls" are represented as
1473 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1475 * and ma is represented by
1477 * x = D(p) + F(y) + G(divs')
1479 * then the resulting divs are
1481 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1483 * We first copy over the divs from "ma" and then
1484 * we add the modified divs from "ls".
1486 __isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1487 __isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1489 int i;
1490 isl_space *space;
1491 isl_local_space *res = NULL;
1492 isl_size n_div_ls, n_div_ma;
1493 isl_int f, c1, c2, g;
1495 ma = isl_multi_aff_align_divs(ma);
1496 if (!ls || !ma)
1497 goto error;
1498 if (!isl_space_is_range_internal(ls->dim, ma->space))
1499 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1500 "spaces don't match", goto error);
1502 n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1503 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
1504 if (n_div_ls < 0 || n_div_ma < 0)
1505 goto error;
1507 space = isl_space_domain(isl_multi_aff_get_space(ma));
1508 res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1509 if (!res)
1510 goto error;
1512 if (n_div_ma) {
1513 isl_mat_free(res->div);
1514 res->div = isl_mat_copy(ma->u.p[0]->ls->div);
1515 res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1516 res->div = isl_mat_add_rows(res->div, n_div_ls);
1517 if (!res->div)
1518 goto error;
1521 isl_int_init(f);
1522 isl_int_init(c1);
1523 isl_int_init(c2);
1524 isl_int_init(g);
1526 for (i = 0; i < ls->div->n_row; ++i) {
1527 if (isl_int_is_zero(ls->div->row[i][0])) {
1528 isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1529 continue;
1531 if (isl_seq_preimage(res->div->row[n_div_ma + i],
1532 ls->div->row[i],
1533 ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1) < 0)
1534 res = isl_local_space_free(res);
1535 res = normalize_div(res, n_div_ma + i);
1536 if (!res)
1537 break;
1540 isl_int_clear(f);
1541 isl_int_clear(c1);
1542 isl_int_clear(c2);
1543 isl_int_clear(g);
1545 isl_local_space_free(ls);
1546 isl_multi_aff_free(ma);
1547 return res;
1548 error:
1549 isl_local_space_free(ls);
1550 isl_multi_aff_free(ma);
1551 isl_local_space_free(res);
1552 return NULL;
1555 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1556 * to dimensions of "dst_type" at "dst_pos".
1558 * Moving to/from local dimensions is not allowed.
1559 * We currently assume that the dimension type changes.
1561 __isl_give isl_local_space *isl_local_space_move_dims(
1562 __isl_take isl_local_space *ls,
1563 enum isl_dim_type dst_type, unsigned dst_pos,
1564 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1566 unsigned g_dst_pos;
1567 unsigned g_src_pos;
1569 if (!ls)
1570 return NULL;
1571 if (n == 0 &&
1572 !isl_local_space_is_named_or_nested(ls, src_type) &&
1573 !isl_local_space_is_named_or_nested(ls, dst_type))
1574 return ls;
1576 if (isl_local_space_check_range(ls, src_type, src_pos, n) < 0)
1577 return isl_local_space_free(ls);
1578 if (isl_local_space_check_range(ls, dst_type, dst_pos, 0) < 0)
1579 return isl_local_space_free(ls);
1580 if (src_type == isl_dim_div)
1581 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1582 "cannot move divs", return isl_local_space_free(ls));
1583 if (dst_type == isl_dim_div)
1584 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1585 "cannot move to divs", return isl_local_space_free(ls));
1586 if (dst_type == src_type && dst_pos == src_pos)
1587 return ls;
1588 if (dst_type == src_type)
1589 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1590 "moving dims within the same type not supported",
1591 return isl_local_space_free(ls));
1593 ls = isl_local_space_cow(ls);
1594 if (!ls)
1595 return NULL;
1597 g_src_pos = 1 + isl_local_space_offset(ls, src_type) + src_pos;
1598 g_dst_pos = 1 + isl_local_space_offset(ls, dst_type) + dst_pos;
1599 if (dst_type > src_type)
1600 g_dst_pos -= n;
1601 ls->div = isl_mat_move_cols(ls->div, g_dst_pos, g_src_pos, n);
1602 if (!ls->div)
1603 return isl_local_space_free(ls);
1604 ls->dim = isl_space_move_dims(ls->dim, dst_type, dst_pos,
1605 src_type, src_pos, n);
1606 if (!ls->dim)
1607 return isl_local_space_free(ls);
1609 return ls;
1612 /* Remove any internal structure of the domain of "ls".
1613 * If there is any such internal structure in the input,
1614 * then the name of the corresponding space is also removed.
1616 __isl_give isl_local_space *isl_local_space_flatten_domain(
1617 __isl_take isl_local_space *ls)
1619 if (!ls)
1620 return NULL;
1622 if (!ls->dim->nested[0])
1623 return ls;
1625 ls = isl_local_space_cow(ls);
1626 if (!ls)
1627 return NULL;
1629 ls->dim = isl_space_flatten_domain(ls->dim);
1630 if (!ls->dim)
1631 return isl_local_space_free(ls);
1633 return ls;
1636 /* Remove any internal structure of the range of "ls".
1637 * If there is any such internal structure in the input,
1638 * then the name of the corresponding space is also removed.
1640 __isl_give isl_local_space *isl_local_space_flatten_range(
1641 __isl_take isl_local_space *ls)
1643 if (!ls)
1644 return NULL;
1646 if (!ls->dim->nested[1])
1647 return ls;
1649 ls = isl_local_space_cow(ls);
1650 if (!ls)
1651 return NULL;
1653 ls->dim = isl_space_flatten_range(ls->dim);
1654 if (!ls->dim)
1655 return isl_local_space_free(ls);
1657 return ls;
1660 /* Given the local space "ls" of a map, return the local space of a set
1661 * that lives in a space that wraps the space of "ls" and that has
1662 * the same divs.
1664 __isl_give isl_local_space *isl_local_space_wrap(__isl_take isl_local_space *ls)
1666 ls = isl_local_space_cow(ls);
1667 if (!ls)
1668 return NULL;
1670 ls->dim = isl_space_wrap(ls->dim);
1671 if (!ls->dim)
1672 return isl_local_space_free(ls);
1674 return ls;
1677 /* Lift the point "pnt", living in the space of "ls"
1678 * to live in a space with extra coordinates corresponding
1679 * to the local variables of "ls".
1681 __isl_give isl_point *isl_local_space_lift_point(__isl_take isl_local_space *ls,
1682 __isl_take isl_point *pnt)
1684 isl_size n_local;
1685 isl_space *space;
1686 isl_local *local;
1687 isl_vec *vec;
1689 if (isl_local_space_check_has_space(ls, isl_point_peek_space(pnt)) < 0)
1690 goto error;
1692 local = isl_local_space_peek_local(ls);
1693 n_local = isl_local_space_dim(ls, isl_dim_div);
1694 if (n_local < 0)
1695 goto error;
1697 space = isl_point_take_space(pnt);
1698 vec = isl_point_take_vec(pnt);
1700 space = isl_space_lift(space, n_local);
1701 vec = isl_local_extend_point_vec(local, vec);
1703 pnt = isl_point_restore_vec(pnt, vec);
1704 pnt = isl_point_restore_space(pnt, space);
1706 isl_local_space_free(ls);
1708 return pnt;
1709 error:
1710 isl_local_space_free(ls);
1711 isl_point_free(pnt);
1712 return NULL;