isl_map_simplify.c: ok_to_set_div_from_bound: use isl_basic_map_offset
[isl.git] / isl_local_space.c
blob2051f3248288bacb855f453cb5f1c003d38ca8c3
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 #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, n;
339 isl_bool unknown;
340 isl_aff *aff;
342 ls = isl_local_space_copy(ls);
343 n = isl_local_space_dim(ls, isl_dim_div);
344 for (i = n - 1; i >= 0; --i) {
345 unknown = isl_local_space_div_is_marked_unknown(ls, i);
346 if (unknown < 0)
347 ls = isl_local_space_free(ls);
348 else if (!unknown)
349 continue;
350 ls = isl_local_space_drop_dims(ls, isl_dim_div, i, 1);
351 if (pos > i)
352 --pos;
354 aff = extract_div(ls, pos);
355 isl_local_space_free(ls);
356 return aff;
359 /* Return the argument of the integer division at position "pos" in "ls".
360 * The integer division is assumed to have a complete explicit
361 * representation. If some of the other integer divisions
362 * do not have an explicit representation, then they need
363 * to be removed first because the domain of an isl_aff
364 * is not allowed to have unknown local variables.
366 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
367 int pos)
369 isl_bool known;
371 if (!ls)
372 return NULL;
374 if (pos < 0 || pos >= ls->div->n_row)
375 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
376 "index out of bounds", return NULL);
378 known = isl_local_space_div_is_known(ls, pos);
379 if (known < 0)
380 return NULL;
381 if (!known)
382 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
383 "expression of div unknown", return NULL);
384 if (!isl_local_space_is_set(ls))
385 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
386 "cannot represent divs of map spaces", return NULL);
388 known = isl_local_space_divs_known(ls);
389 if (known < 0)
390 return NULL;
391 if (known)
392 return extract_div(ls, pos);
393 else
394 return drop_unknown_divs_and_extract_div(ls, pos);
397 /* Return the space of "ls".
399 __isl_keep isl_space *isl_local_space_peek_space(__isl_keep isl_local_space *ls)
401 if (!ls)
402 return NULL;
404 return ls->dim;
407 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
409 return isl_space_copy(isl_local_space_peek_space(ls));
412 /* Return the space of "ls".
413 * This may be either a copy or the space itself
414 * if there is only one reference to "ls".
415 * This allows the space to be modified inplace
416 * if both the local space and its space have only a single reference.
417 * The caller is not allowed to modify "ls" between this call and
418 * a subsequent call to isl_local_space_restore_space.
419 * The only exception is that isl_local_space_free can be called instead.
421 __isl_give isl_space *isl_local_space_take_space(__isl_keep isl_local_space *ls)
423 isl_space *space;
425 if (!ls)
426 return NULL;
427 if (ls->ref != 1)
428 return isl_local_space_get_space(ls);
429 space = ls->dim;
430 ls->dim = NULL;
431 return space;
434 /* Set the space of "ls" to "space", where the space of "ls" may be missing
435 * due to a preceding call to isl_local_space_take_space.
436 * However, in this case, "ls" only has a single reference and
437 * then the call to isl_local_space_cow has no effect.
439 __isl_give isl_local_space *isl_local_space_restore_space(
440 __isl_take isl_local_space *ls, __isl_take isl_space *space)
442 if (!ls || !space)
443 goto error;
445 if (ls->dim == space) {
446 isl_space_free(space);
447 return ls;
450 ls = isl_local_space_cow(ls);
451 if (!ls)
452 goto error;
453 isl_space_free(ls->dim);
454 ls->dim = space;
456 return ls;
457 error:
458 isl_local_space_free(ls);
459 isl_space_free(space);
460 return NULL;
463 /* Return the local variables of "ls".
465 __isl_keep isl_local *isl_local_space_peek_local(__isl_keep isl_local_space *ls)
467 return ls ? ls->div : NULL;
470 /* Replace the identifier of the tuple of type "type" by "id".
472 __isl_give isl_local_space *isl_local_space_set_tuple_id(
473 __isl_take isl_local_space *ls,
474 enum isl_dim_type type, __isl_take isl_id *id)
476 ls = isl_local_space_cow(ls);
477 if (!ls)
478 goto error;
479 ls->dim = isl_space_set_tuple_id(ls->dim, type, id);
480 if (!ls->dim)
481 return isl_local_space_free(ls);
482 return ls;
483 error:
484 isl_id_free(id);
485 return NULL;
488 __isl_give isl_local_space *isl_local_space_set_dim_name(
489 __isl_take isl_local_space *ls,
490 enum isl_dim_type type, unsigned pos, const char *s)
492 ls = isl_local_space_cow(ls);
493 if (!ls)
494 return NULL;
495 ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
496 if (!ls->dim)
497 return isl_local_space_free(ls);
499 return ls;
502 __isl_give isl_local_space *isl_local_space_set_dim_id(
503 __isl_take isl_local_space *ls,
504 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
506 ls = isl_local_space_cow(ls);
507 if (!ls)
508 goto error;
509 ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
510 if (!ls->dim)
511 return isl_local_space_free(ls);
513 return ls;
514 error:
515 isl_id_free(id);
516 return NULL;
519 /* Construct a zero-dimensional local space with the given parameter domain.
521 __isl_give isl_local_space *isl_local_space_set_from_params(
522 __isl_take isl_local_space *ls)
524 isl_space *space;
526 space = isl_local_space_take_space(ls);
527 space = isl_space_set_from_params(space);
528 ls = isl_local_space_restore_space(ls, space);
530 return ls;
533 __isl_give isl_local_space *isl_local_space_reset_space(
534 __isl_take isl_local_space *ls, __isl_take isl_space *dim)
536 ls = isl_local_space_cow(ls);
537 if (!ls || !dim)
538 goto error;
540 isl_space_free(ls->dim);
541 ls->dim = dim;
543 return ls;
544 error:
545 isl_local_space_free(ls);
546 isl_space_free(dim);
547 return NULL;
550 /* Reorder the dimensions of "ls" according to the given reordering.
551 * The reordering r is assumed to have been extended with the local
552 * variables, leaving them in the same order.
554 __isl_give isl_local_space *isl_local_space_realign(
555 __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
557 ls = isl_local_space_cow(ls);
558 if (!ls || !r)
559 goto error;
561 ls->div = isl_local_reorder(ls->div, isl_reordering_copy(r));
562 if (!ls->div)
563 goto error;
565 ls = isl_local_space_reset_space(ls, isl_reordering_get_space(r));
567 isl_reordering_free(r);
568 return ls;
569 error:
570 isl_local_space_free(ls);
571 isl_reordering_free(r);
572 return NULL;
575 __isl_give isl_local_space *isl_local_space_add_div(
576 __isl_take isl_local_space *ls, __isl_take isl_vec *div)
578 ls = isl_local_space_cow(ls);
579 if (!ls || !div)
580 goto error;
582 if (ls->div->n_col != div->size)
583 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
584 "incompatible dimensions", goto error);
586 ls->div = isl_mat_add_zero_cols(ls->div, 1);
587 ls->div = isl_mat_add_rows(ls->div, 1);
588 if (!ls->div)
589 goto error;
591 isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
592 isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
594 isl_vec_free(div);
595 return ls;
596 error:
597 isl_local_space_free(ls);
598 isl_vec_free(div);
599 return NULL;
602 __isl_give isl_local_space *isl_local_space_replace_divs(
603 __isl_take isl_local_space *ls, __isl_take isl_mat *div)
605 ls = isl_local_space_cow(ls);
607 if (!ls || !div)
608 goto error;
610 isl_mat_free(ls->div);
611 ls->div = div;
612 return ls;
613 error:
614 isl_mat_free(div);
615 isl_local_space_free(ls);
616 return NULL;
619 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
620 * defined by "exp".
622 static void expand_row(__isl_keep isl_mat *dst, int d,
623 __isl_keep isl_mat *src, int s, int *exp)
625 int i;
626 unsigned c = src->n_col - src->n_row;
628 isl_seq_cpy(dst->row[d], src->row[s], c);
629 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
631 for (i = 0; i < s; ++i)
632 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
635 /* Compare (known) divs.
636 * Return non-zero if at least one of the two divs is unknown.
637 * In particular, if both divs are unknown, we respect their
638 * current order. Otherwise, we sort the known div after the unknown
639 * div only if the known div depends on the unknown div.
641 static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
642 unsigned n_row, unsigned n_col)
644 int li, lj;
645 int unknown_i, unknown_j;
647 unknown_i = isl_int_is_zero(row_i[0]);
648 unknown_j = isl_int_is_zero(row_j[0]);
650 if (unknown_i && unknown_j)
651 return i - j;
653 if (unknown_i)
654 li = n_col - n_row + i;
655 else
656 li = isl_seq_last_non_zero(row_i, n_col);
657 if (unknown_j)
658 lj = n_col - n_row + j;
659 else
660 lj = isl_seq_last_non_zero(row_j, n_col);
662 if (li != lj)
663 return li - lj;
665 return isl_seq_cmp(row_i, row_j, n_col);
668 /* Call cmp_row for divs in a matrix.
670 int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
672 return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
675 /* Call cmp_row for divs in a basic map.
677 static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
678 unsigned total)
680 return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
683 /* Sort the divs in "bmap".
685 * We first make sure divs are placed after divs on which they depend.
686 * Then we perform a simple insertion sort based on the same ordering
687 * that is used in isl_merge_divs.
689 __isl_give isl_basic_map *isl_basic_map_sort_divs(
690 __isl_take isl_basic_map *bmap)
692 int i, j;
693 unsigned total;
695 bmap = isl_basic_map_order_divs(bmap);
696 if (!bmap)
697 return NULL;
698 if (bmap->n_div <= 1)
699 return bmap;
701 total = 2 + isl_basic_map_total_dim(bmap);
702 for (i = 1; i < bmap->n_div; ++i) {
703 for (j = i - 1; j >= 0; --j) {
704 if (bmap_cmp_row(bmap, j, j + 1, total) <= 0)
705 break;
706 bmap = isl_basic_map_swap_div(bmap, j, j + 1);
707 if (!bmap)
708 return NULL;
712 return bmap;
715 /* Sort the divs in the basic maps of "map".
717 __isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
719 return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
722 /* Combine the two lists of divs into a single list.
723 * For each row i in div1, exp1[i] is set to the position of the corresponding
724 * row in the result. Similarly for div2 and exp2.
725 * This function guarantees
726 * exp1[i] >= i
727 * exp1[i+1] > exp1[i]
728 * For optimal merging, the two input list should have been sorted.
730 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
731 __isl_keep isl_mat *div2, int *exp1, int *exp2)
733 int i, j, k;
734 isl_mat *div = NULL;
735 unsigned d;
737 if (!div1 || !div2)
738 return NULL;
740 d = div1->n_col - div1->n_row;
741 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
742 d + div1->n_row + div2->n_row);
743 if (!div)
744 return NULL;
746 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
747 int cmp;
749 expand_row(div, k, div1, i, exp1);
750 expand_row(div, k + 1, div2, j, exp2);
752 cmp = isl_mat_cmp_div(div, k, k + 1);
753 if (cmp == 0) {
754 exp1[i++] = k;
755 exp2[j++] = k;
756 } else if (cmp < 0) {
757 exp1[i++] = k;
758 } else {
759 exp2[j++] = k;
760 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
763 for (; i < div1->n_row; ++i, ++k) {
764 expand_row(div, k, div1, i, exp1);
765 exp1[i] = k;
767 for (; j < div2->n_row; ++j, ++k) {
768 expand_row(div, k, div2, j, exp2);
769 exp2[j] = k;
772 div->n_row = k;
773 div->n_col = d + k;
775 return div;
778 /* Swap divs "a" and "b" in "ls".
780 __isl_give isl_local_space *isl_local_space_swap_div(
781 __isl_take isl_local_space *ls, int a, int b)
783 int offset;
785 ls = isl_local_space_cow(ls);
786 if (!ls)
787 return NULL;
788 if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
789 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
790 "index out of bounds", return isl_local_space_free(ls));
791 offset = ls->div->n_col - ls->div->n_row;
792 ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
793 ls->div = isl_mat_swap_rows(ls->div, a, b);
794 if (!ls->div)
795 return isl_local_space_free(ls);
796 return ls;
799 /* Construct a local space that contains all the divs in either
800 * "ls1" or "ls2".
802 __isl_give isl_local_space *isl_local_space_intersect(
803 __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
805 isl_ctx *ctx;
806 int *exp1 = NULL;
807 int *exp2 = NULL;
808 isl_mat *div = NULL;
809 isl_bool equal;
811 if (!ls1 || !ls2)
812 goto error;
814 ctx = isl_local_space_get_ctx(ls1);
815 if (!isl_space_is_equal(ls1->dim, ls2->dim))
816 isl_die(ctx, isl_error_invalid,
817 "spaces should be identical", goto error);
819 if (ls2->div->n_row == 0) {
820 isl_local_space_free(ls2);
821 return ls1;
824 if (ls1->div->n_row == 0) {
825 isl_local_space_free(ls1);
826 return ls2;
829 exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
830 exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
831 if (!exp1 || !exp2)
832 goto error;
834 div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
835 if (!div)
836 goto error;
838 equal = isl_mat_is_equal(ls1->div, div);
839 if (equal < 0)
840 goto error;
841 if (!equal)
842 ls1 = isl_local_space_cow(ls1);
843 if (!ls1)
844 goto error;
846 free(exp1);
847 free(exp2);
848 isl_local_space_free(ls2);
849 isl_mat_free(ls1->div);
850 ls1->div = div;
852 return ls1;
853 error:
854 free(exp1);
855 free(exp2);
856 isl_mat_free(div);
857 isl_local_space_free(ls1);
858 isl_local_space_free(ls2);
859 return NULL;
862 /* Is the local variable "div" of "ls" marked as not having
863 * an explicit representation?
864 * Note that even if this variable is not marked in this way and therefore
865 * does have an explicit representation, this representation may still
866 * depend (indirectly) on other local variables that do not
867 * have an explicit representation.
869 isl_bool isl_local_space_div_is_marked_unknown(__isl_keep isl_local_space *ls,
870 int div)
872 if (!ls)
873 return isl_bool_error;
874 return isl_local_div_is_marked_unknown(ls->div, div);
877 /* Does "ls" have a complete explicit representation for div "div"?
879 isl_bool isl_local_space_div_is_known(__isl_keep isl_local_space *ls, int div)
881 if (!ls)
882 return isl_bool_error;
883 return isl_local_div_is_known(ls->div, div);
886 /* Does "ls" have an explicit representation for all local variables?
888 isl_bool isl_local_space_divs_known(__isl_keep isl_local_space *ls)
890 if (!ls)
891 return isl_bool_error;
892 return isl_local_divs_known(ls->div);
895 __isl_give isl_local_space *isl_local_space_domain(
896 __isl_take isl_local_space *ls)
898 ls = isl_local_space_drop_dims(ls, isl_dim_out,
899 0, isl_local_space_dim(ls, isl_dim_out));
900 ls = isl_local_space_cow(ls);
901 if (!ls)
902 return NULL;
903 ls->dim = isl_space_domain(ls->dim);
904 if (!ls->dim)
905 return isl_local_space_free(ls);
906 return ls;
909 __isl_give isl_local_space *isl_local_space_range(
910 __isl_take isl_local_space *ls)
912 ls = isl_local_space_drop_dims(ls, isl_dim_in,
913 0, isl_local_space_dim(ls, isl_dim_in));
914 ls = isl_local_space_cow(ls);
915 if (!ls)
916 return NULL;
918 ls->dim = isl_space_range(ls->dim);
919 if (!ls->dim)
920 return isl_local_space_free(ls);
921 return ls;
924 /* Construct a local space for a map that has the given local
925 * space as domain and that has a zero-dimensional range.
927 __isl_give isl_local_space *isl_local_space_from_domain(
928 __isl_take isl_local_space *ls)
930 ls = isl_local_space_cow(ls);
931 if (!ls)
932 return NULL;
933 ls->dim = isl_space_from_domain(ls->dim);
934 if (!ls->dim)
935 return isl_local_space_free(ls);
936 return ls;
939 __isl_give isl_local_space *isl_local_space_add_dims(
940 __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
942 int pos;
944 if (!ls)
945 return NULL;
946 pos = isl_local_space_dim(ls, type);
947 return isl_local_space_insert_dims(ls, type, pos, n);
950 /* Lift the basic set "bset", living in the space of "ls"
951 * to live in a space with extra coordinates corresponding
952 * to the local variables of "ls".
954 __isl_give isl_basic_set *isl_local_space_lift_basic_set(
955 __isl_take isl_local_space *ls, __isl_take isl_basic_set *bset)
957 unsigned n_local;
958 isl_space *space;
959 isl_basic_set *ls_bset;
961 space = isl_basic_set_peek_space(bset);
962 if (isl_local_space_check_has_space(ls, space) < 0)
963 goto error;
965 n_local = isl_local_space_dim(ls, isl_dim_div);
966 if (n_local == 0) {
967 isl_local_space_free(ls);
968 return bset;
971 bset = isl_basic_set_add_dims(bset, isl_dim_set, n_local);
972 ls_bset = isl_basic_set_from_local_space(ls);
973 ls_bset = isl_basic_set_lift(ls_bset);
974 ls_bset = isl_basic_set_flatten(ls_bset);
975 bset = isl_basic_set_intersect(bset, ls_bset);
977 return bset;
978 error:
979 isl_local_space_free(ls);
980 isl_basic_set_free(bset);
981 return NULL;
984 /* Lift the set "set", living in the space of "ls"
985 * to live in a space with extra coordinates corresponding
986 * to the local variables of "ls".
988 __isl_give isl_set *isl_local_space_lift_set(__isl_take isl_local_space *ls,
989 __isl_take isl_set *set)
991 unsigned n_local;
992 isl_basic_set *bset;
994 if (isl_local_space_check_has_space(ls, isl_set_peek_space(set)) < 0)
995 goto error;
997 n_local = isl_local_space_dim(ls, isl_dim_div);
998 if (n_local == 0) {
999 isl_local_space_free(ls);
1000 return set;
1003 set = isl_set_add_dims(set, isl_dim_set, n_local);
1004 bset = isl_basic_set_from_local_space(ls);
1005 bset = isl_basic_set_lift(bset);
1006 bset = isl_basic_set_flatten(bset);
1007 set = isl_set_intersect(set, isl_set_from_basic_set(bset));
1009 return set;
1010 error:
1011 isl_local_space_free(ls);
1012 isl_set_free(set);
1013 return NULL;
1016 /* Remove common factor of non-constant terms and denominator.
1018 static __isl_give isl_local_space *normalize_div(
1019 __isl_take isl_local_space *ls, int div)
1021 isl_ctx *ctx = ls->div->ctx;
1022 unsigned total = ls->div->n_col - 2;
1024 isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
1025 isl_int_gcd(ctx->normalize_gcd,
1026 ctx->normalize_gcd, ls->div->row[div][0]);
1027 if (isl_int_is_one(ctx->normalize_gcd))
1028 return ls;
1030 isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
1031 ctx->normalize_gcd, total);
1032 isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
1033 ctx->normalize_gcd);
1034 isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
1035 ctx->normalize_gcd);
1037 return ls;
1040 /* Exploit the equalities in "eq" to simplify the expressions of
1041 * the integer divisions in "ls".
1042 * The integer divisions in "ls" are assumed to appear as regular
1043 * dimensions in "eq".
1045 __isl_give isl_local_space *isl_local_space_substitute_equalities(
1046 __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
1048 int i, j, k;
1049 unsigned total;
1050 unsigned n_div;
1052 if (!ls || !eq)
1053 goto error;
1055 total = isl_space_dim(eq->dim, isl_dim_all);
1056 if (isl_local_space_dim(ls, isl_dim_all) != total)
1057 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1058 "spaces don't match", goto error);
1059 total++;
1060 n_div = eq->n_div;
1061 for (i = 0; i < eq->n_eq; ++i) {
1062 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1063 if (j < 0 || j == 0 || j >= total)
1064 continue;
1066 for (k = 0; k < ls->div->n_row; ++k) {
1067 if (isl_int_is_zero(ls->div->row[k][1 + j]))
1068 continue;
1069 ls = isl_local_space_cow(ls);
1070 if (!ls)
1071 goto error;
1072 ls->div = isl_mat_cow(ls->div);
1073 if (!ls->div)
1074 goto error;
1075 isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
1076 &ls->div->row[k][0]);
1077 ls = normalize_div(ls, k);
1078 if (!ls)
1079 goto error;
1083 isl_basic_set_free(eq);
1084 return ls;
1085 error:
1086 isl_basic_set_free(eq);
1087 isl_local_space_free(ls);
1088 return NULL;
1091 /* Plug in the affine expressions "subs" of length "subs_len" (including
1092 * the denominator and the constant term) into the variable at position "pos"
1093 * of the "n" div expressions starting at "first".
1095 * Let i be the dimension to replace and let "subs" be of the form
1097 * f/d
1099 * Any integer division starting at "first" with a non-zero coefficient for i,
1101 * floor((a i + g)/m)
1103 * is replaced by
1105 * floor((a f + d g)/(m d))
1107 __isl_give isl_local_space *isl_local_space_substitute_seq(
1108 __isl_take isl_local_space *ls,
1109 enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
1110 int first, int n)
1112 int i;
1113 isl_int v;
1115 if (n == 0)
1116 return ls;
1117 ls = isl_local_space_cow(ls);
1118 if (!ls)
1119 return NULL;
1120 ls->div = isl_mat_cow(ls->div);
1121 if (!ls->div)
1122 return isl_local_space_free(ls);
1124 if (first + n > ls->div->n_row)
1125 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1126 "index out of bounds", return isl_local_space_free(ls));
1128 pos += isl_local_space_offset(ls, type);
1130 isl_int_init(v);
1131 for (i = first; i < first + n; ++i) {
1132 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
1133 continue;
1134 isl_seq_substitute(ls->div->row[i], pos, subs,
1135 ls->div->n_col, subs_len, v);
1136 ls = normalize_div(ls, i);
1137 if (!ls)
1138 break;
1140 isl_int_clear(v);
1142 return ls;
1145 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
1146 * of "ls".
1148 * Let i be the dimension to replace and let "subs" be of the form
1150 * f/d
1152 * Any integer division with a non-zero coefficient for i,
1154 * floor((a i + g)/m)
1156 * is replaced by
1158 * floor((a f + d g)/(m d))
1160 __isl_give isl_local_space *isl_local_space_substitute(
1161 __isl_take isl_local_space *ls,
1162 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
1164 ls = isl_local_space_cow(ls);
1165 if (!ls || !subs)
1166 return isl_local_space_free(ls);
1168 if (!isl_space_is_equal(ls->dim, subs->ls->dim))
1169 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1170 "spaces don't match", return isl_local_space_free(ls));
1171 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
1172 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1173 "cannot handle divs yet",
1174 return isl_local_space_free(ls));
1176 return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
1177 subs->v->size, 0, ls->div->n_row);
1180 isl_bool isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
1181 enum isl_dim_type type)
1183 if (!ls)
1184 return isl_bool_error;
1185 return isl_space_is_named_or_nested(ls->dim, type);
1188 __isl_give isl_local_space *isl_local_space_drop_dims(
1189 __isl_take isl_local_space *ls,
1190 enum isl_dim_type type, unsigned first, unsigned n)
1192 if (!ls)
1193 return NULL;
1194 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1195 return ls;
1197 if (isl_local_space_check_range(ls, type, first, n) < 0)
1198 return isl_local_space_free(ls);
1200 ls = isl_local_space_cow(ls);
1201 if (!ls)
1202 return NULL;
1204 if (type == isl_dim_div) {
1205 ls->div = isl_mat_drop_rows(ls->div, first, n);
1206 } else {
1207 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
1208 if (!ls->dim)
1209 return isl_local_space_free(ls);
1212 first += 1 + isl_local_space_offset(ls, type);
1213 ls->div = isl_mat_drop_cols(ls->div, first, n);
1214 if (!ls->div)
1215 return isl_local_space_free(ls);
1217 return ls;
1220 __isl_give isl_local_space *isl_local_space_insert_dims(
1221 __isl_take isl_local_space *ls,
1222 enum isl_dim_type type, unsigned first, unsigned n)
1224 if (!ls)
1225 return NULL;
1226 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1227 return ls;
1229 if (isl_local_space_check_range(ls, type, first, 0) < 0)
1230 return isl_local_space_free(ls);
1232 ls = isl_local_space_cow(ls);
1233 if (!ls)
1234 return NULL;
1236 if (type == isl_dim_div) {
1237 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
1238 } else {
1239 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
1240 if (!ls->dim)
1241 return isl_local_space_free(ls);
1244 first += 1 + isl_local_space_offset(ls, type);
1245 ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
1246 if (!ls->div)
1247 return isl_local_space_free(ls);
1249 return ls;
1252 /* Does the linear part of "constraint" correspond to
1253 * integer division "div" in "ls"?
1255 * That is, given div = floor((c + f)/m), is the constraint of the form
1257 * f - m d + c' >= 0 [sign = 1]
1258 * or
1259 * -f + m d + c'' >= 0 [sign = -1]
1261 * If so, set *sign to the corresponding value.
1263 static isl_bool is_linear_div_constraint(__isl_keep isl_local_space *ls,
1264 isl_int *constraint, unsigned div, int *sign)
1266 isl_bool unknown;
1267 unsigned pos;
1269 unknown = isl_local_space_div_is_marked_unknown(ls, div);
1270 if (unknown < 0)
1271 return isl_bool_error;
1272 if (unknown)
1273 return isl_bool_false;
1275 pos = isl_local_space_offset(ls, isl_dim_div) + div;
1277 if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
1278 *sign = -1;
1279 if (!isl_seq_is_neg(constraint + 1,
1280 ls->div->row[div] + 2, pos - 1))
1281 return isl_bool_false;
1282 } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
1283 *sign = 1;
1284 if (!isl_seq_eq(constraint + 1, ls->div->row[div] + 2, pos - 1))
1285 return isl_bool_false;
1286 } else {
1287 return isl_bool_false;
1289 if (isl_seq_first_non_zero(constraint + pos + 1,
1290 ls->div->n_row - div - 1) != -1)
1291 return isl_bool_false;
1292 return isl_bool_true;
1295 /* Check if the constraints pointed to by "constraint" is a div
1296 * constraint corresponding to div "div" in "ls".
1298 * That is, if div = floor(f/m), then check if the constraint is
1300 * f - m d >= 0
1301 * or
1302 * -(f-(m-1)) + m d >= 0
1304 * First check if the linear part is of the right form and
1305 * then check the constant term.
1307 isl_bool isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
1308 isl_int *constraint, unsigned div)
1310 int sign;
1311 isl_bool linear;
1313 linear = is_linear_div_constraint(ls, constraint, div, &sign);
1314 if (linear < 0 || !linear)
1315 return linear;
1317 if (sign < 0) {
1318 int neg;
1319 isl_int_sub(ls->div->row[div][1],
1320 ls->div->row[div][1], ls->div->row[div][0]);
1321 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1322 neg = isl_seq_is_neg(constraint, ls->div->row[div] + 1, 1);
1323 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1324 isl_int_add(ls->div->row[div][1],
1325 ls->div->row[div][1], ls->div->row[div][0]);
1326 if (!neg)
1327 return isl_bool_false;
1328 } else {
1329 if (!isl_int_eq(constraint[0], ls->div->row[div][1]))
1330 return isl_bool_false;
1333 return isl_bool_true;
1336 /* Is the constraint pointed to by "constraint" one
1337 * of an equality that corresponds to integer division "div" in "ls"?
1339 * That is, given an integer division of the form
1341 * a = floor((f + c)/m)
1343 * is the equality of the form
1345 * -f + m d + c' = 0
1347 * Note that the constant term is not checked explicitly, but given
1348 * that this is a valid equality constraint, the constant c' necessarily
1349 * has a value close to -c.
1351 isl_bool isl_local_space_is_div_equality(__isl_keep isl_local_space *ls,
1352 isl_int *constraint, unsigned div)
1354 int sign;
1355 isl_bool linear;
1357 linear = is_linear_div_constraint(ls, constraint, div, &sign);
1358 if (linear < 0 || !linear)
1359 return linear;
1361 return sign < 0;
1365 * Set active[i] to 1 if the dimension at position i is involved
1366 * in the linear expression l.
1368 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
1370 int i, j;
1371 isl_ctx *ctx;
1372 int *active = NULL;
1373 unsigned total;
1374 unsigned offset;
1376 ctx = isl_local_space_get_ctx(ls);
1377 total = isl_local_space_dim(ls, isl_dim_all);
1378 active = isl_calloc_array(ctx, int, total);
1379 if (total && !active)
1380 return NULL;
1382 for (i = 0; i < total; ++i)
1383 active[i] = !isl_int_is_zero(l[i]);
1385 offset = isl_local_space_offset(ls, isl_dim_div) - 1;
1386 for (i = ls->div->n_row - 1; i >= 0; --i) {
1387 if (!active[offset + i])
1388 continue;
1389 for (j = 0; j < total; ++j)
1390 active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
1393 return active;
1396 /* Given a local space "ls" of a set, create a local space
1397 * for the lift of the set. In particular, the result
1398 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1399 * range of the wrapped map.
1401 __isl_give isl_local_space *isl_local_space_lift(
1402 __isl_take isl_local_space *ls)
1404 ls = isl_local_space_cow(ls);
1405 if (!ls)
1406 return NULL;
1408 ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1409 ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1410 if (!ls->dim || !ls->div)
1411 return isl_local_space_free(ls);
1413 return ls;
1416 /* Construct a basic map that maps a set living in local space "ls"
1417 * to the corresponding lifted local space.
1419 __isl_give isl_basic_map *isl_local_space_lifting(
1420 __isl_take isl_local_space *ls)
1422 isl_basic_map *lifting;
1423 isl_basic_set *bset;
1425 if (!ls)
1426 return NULL;
1427 if (!isl_local_space_is_set(ls))
1428 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1429 "lifting only defined on set spaces", goto error);
1431 bset = isl_basic_set_from_local_space(ls);
1432 lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1433 lifting = isl_basic_map_domain_map(lifting);
1434 lifting = isl_basic_map_reverse(lifting);
1436 return lifting;
1437 error:
1438 isl_local_space_free(ls);
1439 return NULL;
1442 /* Compute the preimage of "ls" under the function represented by "ma".
1443 * In other words, plug in "ma" in "ls". The result is a local space
1444 * that is part of the domain space of "ma".
1446 * If the divs in "ls" are represented as
1448 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1450 * and ma is represented by
1452 * x = D(p) + F(y) + G(divs')
1454 * then the resulting divs are
1456 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1458 * We first copy over the divs from "ma" and then
1459 * we add the modified divs from "ls".
1461 __isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1462 __isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1464 int i;
1465 isl_space *space;
1466 isl_local_space *res = NULL;
1467 int n_div_ls, n_div_ma;
1468 isl_int f, c1, c2, g;
1470 ma = isl_multi_aff_align_divs(ma);
1471 if (!ls || !ma)
1472 goto error;
1473 if (!isl_space_is_range_internal(ls->dim, ma->space))
1474 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1475 "spaces don't match", goto error);
1477 n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1478 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
1480 space = isl_space_domain(isl_multi_aff_get_space(ma));
1481 res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1482 if (!res)
1483 goto error;
1485 if (n_div_ma) {
1486 isl_mat_free(res->div);
1487 res->div = isl_mat_copy(ma->u.p[0]->ls->div);
1488 res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1489 res->div = isl_mat_add_rows(res->div, n_div_ls);
1490 if (!res->div)
1491 goto error;
1494 isl_int_init(f);
1495 isl_int_init(c1);
1496 isl_int_init(c2);
1497 isl_int_init(g);
1499 for (i = 0; i < ls->div->n_row; ++i) {
1500 if (isl_int_is_zero(ls->div->row[i][0])) {
1501 isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1502 continue;
1504 if (isl_seq_preimage(res->div->row[n_div_ma + i],
1505 ls->div->row[i],
1506 ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1) < 0)
1507 res = isl_local_space_free(res);
1508 res = normalize_div(res, n_div_ma + i);
1509 if (!res)
1510 break;
1513 isl_int_clear(f);
1514 isl_int_clear(c1);
1515 isl_int_clear(c2);
1516 isl_int_clear(g);
1518 isl_local_space_free(ls);
1519 isl_multi_aff_free(ma);
1520 return res;
1521 error:
1522 isl_local_space_free(ls);
1523 isl_multi_aff_free(ma);
1524 isl_local_space_free(res);
1525 return NULL;
1528 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1529 * to dimensions of "dst_type" at "dst_pos".
1531 * Moving to/from local dimensions is not allowed.
1532 * We currently assume that the dimension type changes.
1534 __isl_give isl_local_space *isl_local_space_move_dims(
1535 __isl_take isl_local_space *ls,
1536 enum isl_dim_type dst_type, unsigned dst_pos,
1537 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1539 unsigned g_dst_pos;
1540 unsigned g_src_pos;
1542 if (!ls)
1543 return NULL;
1544 if (n == 0 &&
1545 !isl_local_space_is_named_or_nested(ls, src_type) &&
1546 !isl_local_space_is_named_or_nested(ls, dst_type))
1547 return ls;
1549 if (isl_local_space_check_range(ls, src_type, src_pos, n) < 0)
1550 return isl_local_space_free(ls);
1551 if (isl_local_space_check_range(ls, dst_type, dst_pos, 0) < 0)
1552 return isl_local_space_free(ls);
1553 if (src_type == isl_dim_div)
1554 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1555 "cannot move divs", return isl_local_space_free(ls));
1556 if (dst_type == isl_dim_div)
1557 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1558 "cannot move to divs", return isl_local_space_free(ls));
1559 if (dst_type == src_type && dst_pos == src_pos)
1560 return ls;
1561 if (dst_type == src_type)
1562 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1563 "moving dims within the same type not supported",
1564 return isl_local_space_free(ls));
1566 ls = isl_local_space_cow(ls);
1567 if (!ls)
1568 return NULL;
1570 g_src_pos = 1 + isl_local_space_offset(ls, src_type) + src_pos;
1571 g_dst_pos = 1 + isl_local_space_offset(ls, dst_type) + dst_pos;
1572 if (dst_type > src_type)
1573 g_dst_pos -= n;
1574 ls->div = isl_mat_move_cols(ls->div, g_dst_pos, g_src_pos, n);
1575 if (!ls->div)
1576 return isl_local_space_free(ls);
1577 ls->dim = isl_space_move_dims(ls->dim, dst_type, dst_pos,
1578 src_type, src_pos, n);
1579 if (!ls->dim)
1580 return isl_local_space_free(ls);
1582 return ls;
1585 /* Remove any internal structure of the domain of "ls".
1586 * If there is any such internal structure in the input,
1587 * then the name of the corresponding space is also removed.
1589 __isl_give isl_local_space *isl_local_space_flatten_domain(
1590 __isl_take isl_local_space *ls)
1592 if (!ls)
1593 return NULL;
1595 if (!ls->dim->nested[0])
1596 return ls;
1598 ls = isl_local_space_cow(ls);
1599 if (!ls)
1600 return NULL;
1602 ls->dim = isl_space_flatten_domain(ls->dim);
1603 if (!ls->dim)
1604 return isl_local_space_free(ls);
1606 return ls;
1609 /* Remove any internal structure of the range of "ls".
1610 * If there is any such internal structure in the input,
1611 * then the name of the corresponding space is also removed.
1613 __isl_give isl_local_space *isl_local_space_flatten_range(
1614 __isl_take isl_local_space *ls)
1616 if (!ls)
1617 return NULL;
1619 if (!ls->dim->nested[1])
1620 return ls;
1622 ls = isl_local_space_cow(ls);
1623 if (!ls)
1624 return NULL;
1626 ls->dim = isl_space_flatten_range(ls->dim);
1627 if (!ls->dim)
1628 return isl_local_space_free(ls);
1630 return ls;
1633 /* Given the local space "ls" of a map, return the local space of a set
1634 * that lives in a space that wraps the space of "ls" and that has
1635 * the same divs.
1637 __isl_give isl_local_space *isl_local_space_wrap(__isl_take isl_local_space *ls)
1639 ls = isl_local_space_cow(ls);
1640 if (!ls)
1641 return NULL;
1643 ls->dim = isl_space_wrap(ls->dim);
1644 if (!ls->dim)
1645 return isl_local_space_free(ls);
1647 return ls;
1650 /* Lift the point "pnt", living in the space of "ls"
1651 * to live in a space with extra coordinates corresponding
1652 * to the local variables of "ls".
1654 __isl_give isl_point *isl_local_space_lift_point(__isl_take isl_local_space *ls,
1655 __isl_take isl_point *pnt)
1657 unsigned n_local;
1658 isl_space *space;
1659 isl_local *local;
1660 isl_vec *vec;
1662 if (isl_local_space_check_has_space(ls, isl_point_peek_space(pnt)) < 0)
1663 goto error;
1665 local = isl_local_space_peek_local(ls);
1666 n_local = isl_local_space_dim(ls, isl_dim_div);
1668 space = isl_point_take_space(pnt);
1669 vec = isl_point_take_vec(pnt);
1671 space = isl_space_lift(space, n_local);
1672 vec = isl_local_extend_point_vec(local, vec);
1674 pnt = isl_point_restore_vec(pnt, vec);
1675 pnt = isl_point_restore_space(pnt, space);
1677 isl_local_space_free(ls);
1679 return pnt;
1680 error:
1681 isl_local_space_free(ls);
1682 isl_point_free(pnt);
1683 return NULL;