AUTHORS: change Uday's name to Uday Bondhugula
[isl.git] / isl_local_space.c
blob619173bf09faf8f122470bb41cea46a3e3cdbf8c
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(__isl_take isl_space *dim,
49 __isl_take isl_mat *div)
51 isl_ctx *ctx;
52 isl_local_space *ls = NULL;
54 if (!dim || !div)
55 goto error;
57 ctx = isl_space_get_ctx(dim);
58 ls = isl_calloc_type(ctx, struct isl_local_space);
59 if (!ls)
60 goto error;
62 ls->ref = 1;
63 ls->dim = dim;
64 ls->div = div;
66 return ls;
67 error:
68 isl_mat_free(div);
69 isl_space_free(dim);
70 isl_local_space_free(ls);
71 return NULL;
74 __isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *dim,
75 unsigned n_div)
77 isl_ctx *ctx;
78 isl_mat *div;
79 unsigned total;
81 if (!dim)
82 return NULL;
84 total = isl_space_dim(dim, isl_dim_all);
86 ctx = isl_space_get_ctx(dim);
87 div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
88 return isl_local_space_alloc_div(dim, div);
91 __isl_give isl_local_space *isl_local_space_from_space(__isl_take isl_space *dim)
93 return isl_local_space_alloc(dim, 0);
96 __isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
98 if (!ls)
99 return NULL;
101 ls->ref++;
102 return ls;
105 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
107 if (!ls)
108 return NULL;
110 return isl_local_space_alloc_div(isl_space_copy(ls->dim),
111 isl_mat_copy(ls->div));
115 __isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
117 if (!ls)
118 return NULL;
120 if (ls->ref == 1)
121 return ls;
122 ls->ref--;
123 return isl_local_space_dup(ls);
126 __isl_null isl_local_space *isl_local_space_free(
127 __isl_take isl_local_space *ls)
129 if (!ls)
130 return NULL;
132 if (--ls->ref > 0)
133 return NULL;
135 isl_space_free(ls->dim);
136 isl_mat_free(ls->div);
138 free(ls);
140 return NULL;
143 /* Is the local space that of a parameter domain?
145 isl_bool isl_local_space_is_params(__isl_keep isl_local_space *ls)
147 if (!ls)
148 return isl_bool_error;
149 return isl_space_is_params(ls->dim);
152 /* Is the local space that of a set?
154 isl_bool isl_local_space_is_set(__isl_keep isl_local_space *ls)
156 return ls ? isl_space_is_set(ls->dim) : isl_bool_error;
159 /* Do "ls1" and "ls2" have the same space?
161 isl_bool isl_local_space_has_equal_space(__isl_keep isl_local_space *ls1,
162 __isl_keep isl_local_space *ls2)
164 if (!ls1 || !ls2)
165 return isl_bool_error;
167 return isl_space_is_equal(ls1->dim, ls2->dim);
170 /* Is the space of "ls" equal to "space"?
172 isl_bool isl_local_space_has_space(__isl_keep isl_local_space *ls,
173 __isl_keep isl_space *space)
175 return isl_space_is_equal(isl_local_space_peek_space(ls), space);
178 /* Check that the space of "ls" is equal to "space".
180 static isl_stat isl_local_space_check_has_space(__isl_keep isl_local_space *ls,
181 __isl_keep isl_space *space)
183 isl_bool ok;
185 ok = isl_local_space_has_space(ls, space);
186 if (ok < 0)
187 return isl_stat_error;
188 if (!ok)
189 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
190 "spaces don't match", return isl_stat_error);
191 return isl_stat_ok;
194 /* Return true if the two local spaces are identical, with identical
195 * expressions for the integer divisions.
197 isl_bool isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
198 __isl_keep isl_local_space *ls2)
200 isl_bool equal;
202 equal = isl_local_space_has_equal_space(ls1, ls2);
203 if (equal < 0 || !equal)
204 return equal;
206 if (!isl_local_space_divs_known(ls1))
207 return isl_bool_false;
208 if (!isl_local_space_divs_known(ls2))
209 return isl_bool_false;
211 return isl_mat_is_equal(ls1->div, ls2->div);
214 /* Compare two isl_local_spaces.
216 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
217 * than "ls2" and 0 if they are equal.
219 int isl_local_space_cmp(__isl_keep isl_local_space *ls1,
220 __isl_keep isl_local_space *ls2)
222 int cmp;
224 if (ls1 == ls2)
225 return 0;
226 if (!ls1)
227 return -1;
228 if (!ls2)
229 return 1;
231 cmp = isl_space_cmp(ls1->dim, ls2->dim);
232 if (cmp != 0)
233 return cmp;
235 return isl_local_cmp(ls1->div, ls2->div);
238 int isl_local_space_dim(__isl_keep isl_local_space *ls,
239 enum isl_dim_type type)
241 if (!ls)
242 return 0;
243 if (type == isl_dim_div)
244 return ls->div->n_row;
245 if (type == isl_dim_all)
246 return isl_space_dim(ls->dim, isl_dim_all) + ls->div->n_row;
247 return isl_space_dim(ls->dim, type);
250 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
251 enum isl_dim_type type)
253 isl_space *dim;
255 if (!ls)
256 return 0;
258 dim = ls->dim;
259 switch (type) {
260 case isl_dim_cst: return 0;
261 case isl_dim_param: return 1;
262 case isl_dim_in: return 1 + dim->nparam;
263 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
264 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
265 default: return 0;
269 /* Return the position of the dimension of the given type and name
270 * in "ls".
271 * Return -1 if no such dimension can be found.
273 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space *ls,
274 enum isl_dim_type type, const char *name)
276 if (!ls)
277 return -1;
278 if (type == isl_dim_div)
279 return -1;
280 return isl_space_find_dim_by_name(ls->dim, type, name);
283 /* Does the given dimension have a name?
285 isl_bool isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
286 enum isl_dim_type type, unsigned pos)
288 return ls ? isl_space_has_dim_name(ls->dim, type, pos) : isl_bool_error;
291 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
292 enum isl_dim_type type, unsigned pos)
294 return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
297 isl_bool isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
298 enum isl_dim_type type, unsigned pos)
300 return ls ? isl_space_has_dim_id(ls->dim, type, pos) : isl_bool_error;
303 __isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
304 enum isl_dim_type type, unsigned pos)
306 return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
309 /* Return the argument of the integer division at position "pos" in "ls".
310 * All local variables in "ls" are known to have a (complete) explicit
311 * representation.
313 static __isl_give isl_aff *extract_div(__isl_keep isl_local_space *ls, int pos)
315 isl_aff *aff;
317 aff = isl_aff_alloc(isl_local_space_copy(ls));
318 if (!aff)
319 return NULL;
320 isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
321 return aff;
324 /* Return the argument of the integer division at position "pos" in "ls".
325 * The integer division at that position is known to have a complete
326 * explicit representation, but some of the others do not.
327 * Remove them first because the domain of an isl_aff
328 * is not allowed to have unknown local variables.
330 static __isl_give isl_aff *drop_unknown_divs_and_extract_div(
331 __isl_keep isl_local_space *ls, int pos)
333 int i, n;
334 isl_bool unknown;
335 isl_aff *aff;
337 ls = isl_local_space_copy(ls);
338 n = isl_local_space_dim(ls, isl_dim_div);
339 for (i = n - 1; i >= 0; --i) {
340 unknown = isl_local_space_div_is_marked_unknown(ls, i);
341 if (unknown < 0)
342 ls = isl_local_space_free(ls);
343 else if (!unknown)
344 continue;
345 ls = isl_local_space_drop_dims(ls, isl_dim_div, i, 1);
346 if (pos > i)
347 --pos;
349 aff = extract_div(ls, pos);
350 isl_local_space_free(ls);
351 return aff;
354 /* Return the argument of the integer division at position "pos" in "ls".
355 * The integer division is assumed to have a complete explicit
356 * representation. If some of the other integer divisions
357 * do not have an explicit representation, then they need
358 * to be removed first because the domain of an isl_aff
359 * is not allowed to have unknown local variables.
361 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
362 int pos)
364 isl_bool known;
366 if (!ls)
367 return NULL;
369 if (pos < 0 || pos >= ls->div->n_row)
370 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
371 "index out of bounds", return NULL);
373 known = isl_local_space_div_is_known(ls, pos);
374 if (known < 0)
375 return NULL;
376 if (!known)
377 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
378 "expression of div unknown", return NULL);
379 if (!isl_local_space_is_set(ls))
380 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
381 "cannot represent divs of map spaces", return NULL);
383 known = isl_local_space_divs_known(ls);
384 if (known < 0)
385 return NULL;
386 if (known)
387 return extract_div(ls, pos);
388 else
389 return drop_unknown_divs_and_extract_div(ls, pos);
392 /* Return the space of "ls".
394 __isl_keep isl_space *isl_local_space_peek_space(__isl_keep isl_local_space *ls)
396 if (!ls)
397 return NULL;
399 return ls->dim;
402 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
404 return isl_space_copy(isl_local_space_peek_space(ls));
407 /* Return the space of "ls".
408 * This may be either a copy or the space itself
409 * if there is only one reference to "ls".
410 * This allows the space to be modified inplace
411 * if both the local space and its space have only a single reference.
412 * The caller is not allowed to modify "ls" between this call and
413 * a subsequent call to isl_local_space_restore_space.
414 * The only exception is that isl_local_space_free can be called instead.
416 __isl_give isl_space *isl_local_space_take_space(__isl_keep isl_local_space *ls)
418 isl_space *space;
420 if (!ls)
421 return NULL;
422 if (ls->ref != 1)
423 return isl_local_space_get_space(ls);
424 space = ls->dim;
425 ls->dim = NULL;
426 return space;
429 /* Set the space of "ls" to "space", where the space of "ls" may be missing
430 * due to a preceding call to isl_local_space_take_space.
431 * However, in this case, "ls" only has a single reference and
432 * then the call to isl_local_space_cow has no effect.
434 __isl_give isl_local_space *isl_local_space_restore_space(
435 __isl_take isl_local_space *ls, __isl_take isl_space *space)
437 if (!ls || !space)
438 goto error;
440 if (ls->dim == space) {
441 isl_space_free(space);
442 return ls;
445 ls = isl_local_space_cow(ls);
446 if (!ls)
447 goto error;
448 isl_space_free(ls->dim);
449 ls->dim = space;
451 return ls;
452 error:
453 isl_local_space_free(ls);
454 isl_space_free(space);
455 return NULL;
458 /* Return the local variables of "ls".
460 __isl_keep isl_local *isl_local_space_peek_local(__isl_keep isl_local_space *ls)
462 return ls ? ls->div : NULL;
465 /* Replace the identifier of the tuple of type "type" by "id".
467 __isl_give isl_local_space *isl_local_space_set_tuple_id(
468 __isl_take isl_local_space *ls,
469 enum isl_dim_type type, __isl_take isl_id *id)
471 ls = isl_local_space_cow(ls);
472 if (!ls)
473 goto error;
474 ls->dim = isl_space_set_tuple_id(ls->dim, type, id);
475 if (!ls->dim)
476 return isl_local_space_free(ls);
477 return ls;
478 error:
479 isl_id_free(id);
480 return NULL;
483 __isl_give isl_local_space *isl_local_space_set_dim_name(
484 __isl_take isl_local_space *ls,
485 enum isl_dim_type type, unsigned pos, const char *s)
487 ls = isl_local_space_cow(ls);
488 if (!ls)
489 return NULL;
490 ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
491 if (!ls->dim)
492 return isl_local_space_free(ls);
494 return ls;
497 __isl_give isl_local_space *isl_local_space_set_dim_id(
498 __isl_take isl_local_space *ls,
499 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
501 ls = isl_local_space_cow(ls);
502 if (!ls)
503 goto error;
504 ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
505 if (!ls->dim)
506 return isl_local_space_free(ls);
508 return ls;
509 error:
510 isl_id_free(id);
511 return NULL;
514 /* Construct a zero-dimensional local space with the given parameter domain.
516 __isl_give isl_local_space *isl_local_space_set_from_params(
517 __isl_take isl_local_space *ls)
519 isl_space *space;
521 space = isl_local_space_take_space(ls);
522 space = isl_space_set_from_params(space);
523 ls = isl_local_space_restore_space(ls, space);
525 return ls;
528 __isl_give isl_local_space *isl_local_space_reset_space(
529 __isl_take isl_local_space *ls, __isl_take isl_space *dim)
531 ls = isl_local_space_cow(ls);
532 if (!ls || !dim)
533 goto error;
535 isl_space_free(ls->dim);
536 ls->dim = dim;
538 return ls;
539 error:
540 isl_local_space_free(ls);
541 isl_space_free(dim);
542 return NULL;
545 /* Reorder the columns of the given div definitions according to the
546 * given reordering.
547 * The order of the divs themselves is assumed not to change.
549 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
550 __isl_take isl_reordering *r)
552 int i, j;
553 isl_mat *mat;
554 int extra;
556 if (!div || !r)
557 goto error;
559 extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
560 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
561 if (!mat)
562 goto error;
564 for (i = 0; i < div->n_row; ++i) {
565 isl_seq_cpy(mat->row[i], div->row[i], 2);
566 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
567 for (j = 0; j < r->len; ++j)
568 isl_int_set(mat->row[i][2 + r->pos[j]],
569 div->row[i][2 + j]);
572 isl_reordering_free(r);
573 isl_mat_free(div);
574 return mat;
575 error:
576 isl_reordering_free(r);
577 isl_mat_free(div);
578 return NULL;
581 /* Reorder the dimensions of "ls" according to the given reordering.
582 * The reordering r is assumed to have been extended with the local
583 * variables, leaving them in the same order.
585 __isl_give isl_local_space *isl_local_space_realign(
586 __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
588 ls = isl_local_space_cow(ls);
589 if (!ls || !r)
590 goto error;
592 ls->div = reorder_divs(ls->div, isl_reordering_copy(r));
593 if (!ls->div)
594 goto error;
596 ls = isl_local_space_reset_space(ls, isl_space_copy(r->dim));
598 isl_reordering_free(r);
599 return ls;
600 error:
601 isl_local_space_free(ls);
602 isl_reordering_free(r);
603 return NULL;
606 __isl_give isl_local_space *isl_local_space_add_div(
607 __isl_take isl_local_space *ls, __isl_take isl_vec *div)
609 ls = isl_local_space_cow(ls);
610 if (!ls || !div)
611 goto error;
613 if (ls->div->n_col != div->size)
614 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
615 "incompatible dimensions", goto error);
617 ls->div = isl_mat_add_zero_cols(ls->div, 1);
618 ls->div = isl_mat_add_rows(ls->div, 1);
619 if (!ls->div)
620 goto error;
622 isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
623 isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
625 isl_vec_free(div);
626 return ls;
627 error:
628 isl_local_space_free(ls);
629 isl_vec_free(div);
630 return NULL;
633 __isl_give isl_local_space *isl_local_space_replace_divs(
634 __isl_take isl_local_space *ls, __isl_take isl_mat *div)
636 ls = isl_local_space_cow(ls);
638 if (!ls || !div)
639 goto error;
641 isl_mat_free(ls->div);
642 ls->div = div;
643 return ls;
644 error:
645 isl_mat_free(div);
646 isl_local_space_free(ls);
647 return NULL;
650 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
651 * defined by "exp".
653 static void expand_row(__isl_keep isl_mat *dst, int d,
654 __isl_keep isl_mat *src, int s, int *exp)
656 int i;
657 unsigned c = src->n_col - src->n_row;
659 isl_seq_cpy(dst->row[d], src->row[s], c);
660 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
662 for (i = 0; i < s; ++i)
663 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
666 /* Compare (known) divs.
667 * Return non-zero if at least one of the two divs is unknown.
668 * In particular, if both divs are unknown, we respect their
669 * current order. Otherwise, we sort the known div after the unknown
670 * div only if the known div depends on the unknown div.
672 static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
673 unsigned n_row, unsigned n_col)
675 int li, lj;
676 int unknown_i, unknown_j;
678 unknown_i = isl_int_is_zero(row_i[0]);
679 unknown_j = isl_int_is_zero(row_j[0]);
681 if (unknown_i && unknown_j)
682 return i - j;
684 if (unknown_i)
685 li = n_col - n_row + i;
686 else
687 li = isl_seq_last_non_zero(row_i, n_col);
688 if (unknown_j)
689 lj = n_col - n_row + j;
690 else
691 lj = isl_seq_last_non_zero(row_j, n_col);
693 if (li != lj)
694 return li - lj;
696 return isl_seq_cmp(row_i, row_j, n_col);
699 /* Call cmp_row for divs in a matrix.
701 int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
703 return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
706 /* Call cmp_row for divs in a basic map.
708 static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
709 unsigned total)
711 return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
714 /* Sort the divs in "bmap".
716 * We first make sure divs are placed after divs on which they depend.
717 * Then we perform a simple insertion sort based on the same ordering
718 * that is used in isl_merge_divs.
720 __isl_give isl_basic_map *isl_basic_map_sort_divs(
721 __isl_take isl_basic_map *bmap)
723 int i, j;
724 unsigned total;
726 bmap = isl_basic_map_order_divs(bmap);
727 if (!bmap)
728 return NULL;
729 if (bmap->n_div <= 1)
730 return bmap;
732 total = 2 + isl_basic_map_total_dim(bmap);
733 for (i = 1; i < bmap->n_div; ++i) {
734 for (j = i - 1; j >= 0; --j) {
735 if (bmap_cmp_row(bmap, j, j + 1, total) <= 0)
736 break;
737 isl_basic_map_swap_div(bmap, j, j + 1);
741 return bmap;
744 /* Sort the divs in the basic maps of "map".
746 __isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
748 return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
751 /* Combine the two lists of divs into a single list.
752 * For each row i in div1, exp1[i] is set to the position of the corresponding
753 * row in the result. Similarly for div2 and exp2.
754 * This function guarantees
755 * exp1[i] >= i
756 * exp1[i+1] > exp1[i]
757 * For optimal merging, the two input list should have been sorted.
759 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
760 __isl_keep isl_mat *div2, int *exp1, int *exp2)
762 int i, j, k;
763 isl_mat *div = NULL;
764 unsigned d;
766 if (!div1 || !div2)
767 return NULL;
769 d = div1->n_col - div1->n_row;
770 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
771 d + div1->n_row + div2->n_row);
772 if (!div)
773 return NULL;
775 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
776 int cmp;
778 expand_row(div, k, div1, i, exp1);
779 expand_row(div, k + 1, div2, j, exp2);
781 cmp = isl_mat_cmp_div(div, k, k + 1);
782 if (cmp == 0) {
783 exp1[i++] = k;
784 exp2[j++] = k;
785 } else if (cmp < 0) {
786 exp1[i++] = k;
787 } else {
788 exp2[j++] = k;
789 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
792 for (; i < div1->n_row; ++i, ++k) {
793 expand_row(div, k, div1, i, exp1);
794 exp1[i] = k;
796 for (; j < div2->n_row; ++j, ++k) {
797 expand_row(div, k, div2, j, exp2);
798 exp2[j] = k;
801 div->n_row = k;
802 div->n_col = d + k;
804 return div;
807 /* Swap divs "a" and "b" in "ls".
809 __isl_give isl_local_space *isl_local_space_swap_div(
810 __isl_take isl_local_space *ls, int a, int b)
812 int offset;
814 ls = isl_local_space_cow(ls);
815 if (!ls)
816 return NULL;
817 if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
818 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
819 "index out of bounds", return isl_local_space_free(ls));
820 offset = ls->div->n_col - ls->div->n_row;
821 ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
822 ls->div = isl_mat_swap_rows(ls->div, a, b);
823 if (!ls->div)
824 return isl_local_space_free(ls);
825 return ls;
828 /* Construct a local space that contains all the divs in either
829 * "ls1" or "ls2".
831 __isl_give isl_local_space *isl_local_space_intersect(
832 __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
834 isl_ctx *ctx;
835 int *exp1 = NULL;
836 int *exp2 = NULL;
837 isl_mat *div = NULL;
838 isl_bool equal;
840 if (!ls1 || !ls2)
841 goto error;
843 ctx = isl_local_space_get_ctx(ls1);
844 if (!isl_space_is_equal(ls1->dim, ls2->dim))
845 isl_die(ctx, isl_error_invalid,
846 "spaces should be identical", goto error);
848 if (ls2->div->n_row == 0) {
849 isl_local_space_free(ls2);
850 return ls1;
853 if (ls1->div->n_row == 0) {
854 isl_local_space_free(ls1);
855 return ls2;
858 exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
859 exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
860 if (!exp1 || !exp2)
861 goto error;
863 div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
864 if (!div)
865 goto error;
867 equal = isl_mat_is_equal(ls1->div, div);
868 if (equal < 0)
869 goto error;
870 if (!equal)
871 ls1 = isl_local_space_cow(ls1);
872 if (!ls1)
873 goto error;
875 free(exp1);
876 free(exp2);
877 isl_local_space_free(ls2);
878 isl_mat_free(ls1->div);
879 ls1->div = div;
881 return ls1;
882 error:
883 free(exp1);
884 free(exp2);
885 isl_mat_free(div);
886 isl_local_space_free(ls1);
887 isl_local_space_free(ls2);
888 return NULL;
891 /* Is the local variable "div" of "ls" marked as not having
892 * an explicit representation?
893 * Note that even if this variable is not marked in this way and therefore
894 * does have an explicit representation, this representation may still
895 * depend (indirectly) on other local variables that do not
896 * have an explicit representation.
898 isl_bool isl_local_space_div_is_marked_unknown(__isl_keep isl_local_space *ls,
899 int div)
901 if (!ls)
902 return isl_bool_error;
903 return isl_local_div_is_marked_unknown(ls->div, div);
906 /* Does "ls" have a complete explicit representation for div "div"?
908 isl_bool isl_local_space_div_is_known(__isl_keep isl_local_space *ls, int div)
910 if (!ls)
911 return isl_bool_error;
912 return isl_local_div_is_known(ls->div, div);
915 /* Does "ls" have an explicit representation for all local variables?
917 isl_bool isl_local_space_divs_known(__isl_keep isl_local_space *ls)
919 if (!ls)
920 return isl_bool_error;
921 return isl_local_divs_known(ls->div);
924 __isl_give isl_local_space *isl_local_space_domain(
925 __isl_take isl_local_space *ls)
927 ls = isl_local_space_drop_dims(ls, isl_dim_out,
928 0, isl_local_space_dim(ls, isl_dim_out));
929 ls = isl_local_space_cow(ls);
930 if (!ls)
931 return NULL;
932 ls->dim = isl_space_domain(ls->dim);
933 if (!ls->dim)
934 return isl_local_space_free(ls);
935 return ls;
938 __isl_give isl_local_space *isl_local_space_range(
939 __isl_take isl_local_space *ls)
941 ls = isl_local_space_drop_dims(ls, isl_dim_in,
942 0, isl_local_space_dim(ls, isl_dim_in));
943 ls = isl_local_space_cow(ls);
944 if (!ls)
945 return NULL;
947 ls->dim = isl_space_range(ls->dim);
948 if (!ls->dim)
949 return isl_local_space_free(ls);
950 return ls;
953 /* Construct a local space for a map that has the given local
954 * space as domain and that has a zero-dimensional range.
956 __isl_give isl_local_space *isl_local_space_from_domain(
957 __isl_take isl_local_space *ls)
959 ls = isl_local_space_cow(ls);
960 if (!ls)
961 return NULL;
962 ls->dim = isl_space_from_domain(ls->dim);
963 if (!ls->dim)
964 return isl_local_space_free(ls);
965 return ls;
968 __isl_give isl_local_space *isl_local_space_add_dims(
969 __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
971 int pos;
973 if (!ls)
974 return NULL;
975 pos = isl_local_space_dim(ls, type);
976 return isl_local_space_insert_dims(ls, type, pos, n);
979 /* Remove common factor of non-constant terms and denominator.
981 static void normalize_div(__isl_keep isl_local_space *ls, int div)
983 isl_ctx *ctx = ls->div->ctx;
984 unsigned total = ls->div->n_col - 2;
986 isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
987 isl_int_gcd(ctx->normalize_gcd,
988 ctx->normalize_gcd, ls->div->row[div][0]);
989 if (isl_int_is_one(ctx->normalize_gcd))
990 return;
992 isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
993 ctx->normalize_gcd, total);
994 isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
995 ctx->normalize_gcd);
996 isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
997 ctx->normalize_gcd);
1000 /* Exploit the equalities in "eq" to simplify the expressions of
1001 * the integer divisions in "ls".
1002 * The integer divisions in "ls" are assumed to appear as regular
1003 * dimensions in "eq".
1005 __isl_give isl_local_space *isl_local_space_substitute_equalities(
1006 __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
1008 int i, j, k;
1009 unsigned total;
1010 unsigned n_div;
1012 if (!ls || !eq)
1013 goto error;
1015 total = isl_space_dim(eq->dim, isl_dim_all);
1016 if (isl_local_space_dim(ls, isl_dim_all) != total)
1017 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1018 "spaces don't match", goto error);
1019 total++;
1020 n_div = eq->n_div;
1021 for (i = 0; i < eq->n_eq; ++i) {
1022 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1023 if (j < 0 || j == 0 || j >= total)
1024 continue;
1026 for (k = 0; k < ls->div->n_row; ++k) {
1027 if (isl_int_is_zero(ls->div->row[k][1 + j]))
1028 continue;
1029 ls = isl_local_space_cow(ls);
1030 if (!ls)
1031 goto error;
1032 ls->div = isl_mat_cow(ls->div);
1033 if (!ls->div)
1034 goto error;
1035 isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
1036 &ls->div->row[k][0]);
1037 normalize_div(ls, k);
1041 isl_basic_set_free(eq);
1042 return ls;
1043 error:
1044 isl_basic_set_free(eq);
1045 isl_local_space_free(ls);
1046 return NULL;
1049 /* Plug in the affine expressions "subs" of length "subs_len" (including
1050 * the denominator and the constant term) into the variable at position "pos"
1051 * of the "n" div expressions starting at "first".
1053 * Let i be the dimension to replace and let "subs" be of the form
1055 * f/d
1057 * Any integer division starting at "first" with a non-zero coefficient for i,
1059 * floor((a i + g)/m)
1061 * is replaced by
1063 * floor((a f + d g)/(m d))
1065 __isl_give isl_local_space *isl_local_space_substitute_seq(
1066 __isl_take isl_local_space *ls,
1067 enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
1068 int first, int n)
1070 int i;
1071 isl_int v;
1073 if (n == 0)
1074 return ls;
1075 ls = isl_local_space_cow(ls);
1076 if (!ls)
1077 return NULL;
1078 ls->div = isl_mat_cow(ls->div);
1079 if (!ls->div)
1080 return isl_local_space_free(ls);
1082 if (first + n > ls->div->n_row)
1083 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1084 "index out of bounds", return isl_local_space_free(ls));
1086 pos += isl_local_space_offset(ls, type);
1088 isl_int_init(v);
1089 for (i = first; i < first + n; ++i) {
1090 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
1091 continue;
1092 isl_seq_substitute(ls->div->row[i], pos, subs,
1093 ls->div->n_col, subs_len, v);
1094 normalize_div(ls, i);
1096 isl_int_clear(v);
1098 return ls;
1101 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
1102 * of "ls".
1104 * Let i be the dimension to replace and let "subs" be of the form
1106 * f/d
1108 * Any integer division with a non-zero coefficient for i,
1110 * floor((a i + g)/m)
1112 * is replaced by
1114 * floor((a f + d g)/(m d))
1116 __isl_give isl_local_space *isl_local_space_substitute(
1117 __isl_take isl_local_space *ls,
1118 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
1120 ls = isl_local_space_cow(ls);
1121 if (!ls || !subs)
1122 return isl_local_space_free(ls);
1124 if (!isl_space_is_equal(ls->dim, subs->ls->dim))
1125 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1126 "spaces don't match", return isl_local_space_free(ls));
1127 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
1128 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1129 "cannot handle divs yet",
1130 return isl_local_space_free(ls));
1132 return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
1133 subs->v->size, 0, ls->div->n_row);
1136 isl_bool isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
1137 enum isl_dim_type type)
1139 if (!ls)
1140 return isl_bool_error;
1141 return isl_space_is_named_or_nested(ls->dim, type);
1144 __isl_give isl_local_space *isl_local_space_drop_dims(
1145 __isl_take isl_local_space *ls,
1146 enum isl_dim_type type, unsigned first, unsigned n)
1148 isl_ctx *ctx;
1150 if (!ls)
1151 return NULL;
1152 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1153 return ls;
1155 ctx = isl_local_space_get_ctx(ls);
1156 if (first + n > isl_local_space_dim(ls, type))
1157 isl_die(ctx, isl_error_invalid, "range out of bounds",
1158 return isl_local_space_free(ls));
1160 ls = isl_local_space_cow(ls);
1161 if (!ls)
1162 return NULL;
1164 if (type == isl_dim_div) {
1165 ls->div = isl_mat_drop_rows(ls->div, first, n);
1166 } else {
1167 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
1168 if (!ls->dim)
1169 return isl_local_space_free(ls);
1172 first += 1 + isl_local_space_offset(ls, type);
1173 ls->div = isl_mat_drop_cols(ls->div, first, n);
1174 if (!ls->div)
1175 return isl_local_space_free(ls);
1177 return ls;
1180 __isl_give isl_local_space *isl_local_space_insert_dims(
1181 __isl_take isl_local_space *ls,
1182 enum isl_dim_type type, unsigned first, unsigned n)
1184 isl_ctx *ctx;
1186 if (!ls)
1187 return NULL;
1188 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1189 return ls;
1191 ctx = isl_local_space_get_ctx(ls);
1192 if (first > isl_local_space_dim(ls, type))
1193 isl_die(ctx, isl_error_invalid, "position out of bounds",
1194 return isl_local_space_free(ls));
1196 ls = isl_local_space_cow(ls);
1197 if (!ls)
1198 return NULL;
1200 if (type == isl_dim_div) {
1201 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
1202 } else {
1203 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
1204 if (!ls->dim)
1205 return isl_local_space_free(ls);
1208 first += 1 + isl_local_space_offset(ls, type);
1209 ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
1210 if (!ls->div)
1211 return isl_local_space_free(ls);
1213 return ls;
1216 /* Check if the constraints pointed to by "constraint" is a div
1217 * constraint corresponding to div "div" in "ls".
1219 * That is, if div = floor(f/m), then check if the constraint is
1221 * f - m d >= 0
1222 * or
1223 * -(f-(m-1)) + m d >= 0
1225 isl_bool isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
1226 isl_int *constraint, unsigned div)
1228 unsigned pos;
1230 if (!ls)
1231 return isl_bool_error;
1233 if (isl_int_is_zero(ls->div->row[div][0]))
1234 return isl_bool_false;
1236 pos = isl_local_space_offset(ls, isl_dim_div) + div;
1238 if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
1239 int neg;
1240 isl_int_sub(ls->div->row[div][1],
1241 ls->div->row[div][1], ls->div->row[div][0]);
1242 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1243 neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
1244 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1245 isl_int_add(ls->div->row[div][1],
1246 ls->div->row[div][1], ls->div->row[div][0]);
1247 if (!neg)
1248 return isl_bool_false;
1249 if (isl_seq_first_non_zero(constraint+pos+1,
1250 ls->div->n_row-div-1) != -1)
1251 return isl_bool_false;
1252 } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
1253 if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
1254 return isl_bool_false;
1255 if (isl_seq_first_non_zero(constraint+pos+1,
1256 ls->div->n_row-div-1) != -1)
1257 return isl_bool_false;
1258 } else
1259 return isl_bool_false;
1261 return isl_bool_true;
1265 * Set active[i] to 1 if the dimension at position i is involved
1266 * in the linear expression l.
1268 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
1270 int i, j;
1271 isl_ctx *ctx;
1272 int *active = NULL;
1273 unsigned total;
1274 unsigned offset;
1276 ctx = isl_local_space_get_ctx(ls);
1277 total = isl_local_space_dim(ls, isl_dim_all);
1278 active = isl_calloc_array(ctx, int, total);
1279 if (total && !active)
1280 return NULL;
1282 for (i = 0; i < total; ++i)
1283 active[i] = !isl_int_is_zero(l[i]);
1285 offset = isl_local_space_offset(ls, isl_dim_div) - 1;
1286 for (i = ls->div->n_row - 1; i >= 0; --i) {
1287 if (!active[offset + i])
1288 continue;
1289 for (j = 0; j < total; ++j)
1290 active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
1293 return active;
1296 /* Given a local space "ls" of a set, create a local space
1297 * for the lift of the set. In particular, the result
1298 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1299 * range of the wrapped map.
1301 __isl_give isl_local_space *isl_local_space_lift(
1302 __isl_take isl_local_space *ls)
1304 ls = isl_local_space_cow(ls);
1305 if (!ls)
1306 return NULL;
1308 ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1309 ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1310 if (!ls->dim || !ls->div)
1311 return isl_local_space_free(ls);
1313 return ls;
1316 /* Construct a basic map that maps a set living in local space "ls"
1317 * to the corresponding lifted local space.
1319 __isl_give isl_basic_map *isl_local_space_lifting(
1320 __isl_take isl_local_space *ls)
1322 isl_basic_map *lifting;
1323 isl_basic_set *bset;
1325 if (!ls)
1326 return NULL;
1327 if (!isl_local_space_is_set(ls))
1328 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1329 "lifting only defined on set spaces", goto error);
1331 bset = isl_basic_set_from_local_space(ls);
1332 lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1333 lifting = isl_basic_map_domain_map(lifting);
1334 lifting = isl_basic_map_reverse(lifting);
1336 return lifting;
1337 error:
1338 isl_local_space_free(ls);
1339 return NULL;
1342 /* Compute the preimage of "ls" under the function represented by "ma".
1343 * In other words, plug in "ma" in "ls". The result is a local space
1344 * that is part of the domain space of "ma".
1346 * If the divs in "ls" are represented as
1348 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1350 * and ma is represented by
1352 * x = D(p) + F(y) + G(divs')
1354 * then the resulting divs are
1356 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1358 * We first copy over the divs from "ma" and then
1359 * we add the modified divs from "ls".
1361 __isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1362 __isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1364 int i;
1365 isl_space *space;
1366 isl_local_space *res = NULL;
1367 int n_div_ls, n_div_ma;
1368 isl_int f, c1, c2, g;
1370 ma = isl_multi_aff_align_divs(ma);
1371 if (!ls || !ma)
1372 goto error;
1373 if (!isl_space_is_range_internal(ls->dim, ma->space))
1374 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1375 "spaces don't match", goto error);
1377 n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1378 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
1380 space = isl_space_domain(isl_multi_aff_get_space(ma));
1381 res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1382 if (!res)
1383 goto error;
1385 if (n_div_ma) {
1386 isl_mat_free(res->div);
1387 res->div = isl_mat_copy(ma->u.p[0]->ls->div);
1388 res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1389 res->div = isl_mat_add_rows(res->div, n_div_ls);
1390 if (!res->div)
1391 goto error;
1394 isl_int_init(f);
1395 isl_int_init(c1);
1396 isl_int_init(c2);
1397 isl_int_init(g);
1399 for (i = 0; i < ls->div->n_row; ++i) {
1400 if (isl_int_is_zero(ls->div->row[i][0])) {
1401 isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1402 continue;
1404 isl_seq_preimage(res->div->row[n_div_ma + i], ls->div->row[i],
1405 ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1);
1406 normalize_div(res, n_div_ma + i);
1409 isl_int_clear(f);
1410 isl_int_clear(c1);
1411 isl_int_clear(c2);
1412 isl_int_clear(g);
1414 isl_local_space_free(ls);
1415 isl_multi_aff_free(ma);
1416 return res;
1417 error:
1418 isl_local_space_free(ls);
1419 isl_multi_aff_free(ma);
1420 isl_local_space_free(res);
1421 return NULL;
1424 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1425 * to dimensions of "dst_type" at "dst_pos".
1427 * Moving to/from local dimensions is not allowed.
1428 * We currently assume that the dimension type changes.
1430 __isl_give isl_local_space *isl_local_space_move_dims(
1431 __isl_take isl_local_space *ls,
1432 enum isl_dim_type dst_type, unsigned dst_pos,
1433 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1435 unsigned g_dst_pos;
1436 unsigned g_src_pos;
1438 if (!ls)
1439 return NULL;
1440 if (n == 0 &&
1441 !isl_local_space_is_named_or_nested(ls, src_type) &&
1442 !isl_local_space_is_named_or_nested(ls, dst_type))
1443 return ls;
1445 if (src_pos + n > isl_local_space_dim(ls, src_type))
1446 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1447 "range out of bounds", return isl_local_space_free(ls));
1448 if (dst_pos > isl_local_space_dim(ls, dst_type))
1449 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1450 "position out of bounds",
1451 return isl_local_space_free(ls));
1452 if (src_type == isl_dim_div)
1453 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1454 "cannot move divs", return isl_local_space_free(ls));
1455 if (dst_type == isl_dim_div)
1456 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1457 "cannot move to divs", return isl_local_space_free(ls));
1458 if (dst_type == src_type && dst_pos == src_pos)
1459 return ls;
1460 if (dst_type == src_type)
1461 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1462 "moving dims within the same type not supported",
1463 return isl_local_space_free(ls));
1465 ls = isl_local_space_cow(ls);
1466 if (!ls)
1467 return NULL;
1469 g_src_pos = 1 + isl_local_space_offset(ls, src_type) + src_pos;
1470 g_dst_pos = 1 + isl_local_space_offset(ls, dst_type) + dst_pos;
1471 if (dst_type > src_type)
1472 g_dst_pos -= n;
1473 ls->div = isl_mat_move_cols(ls->div, g_dst_pos, g_src_pos, n);
1474 if (!ls->div)
1475 return isl_local_space_free(ls);
1476 ls->dim = isl_space_move_dims(ls->dim, dst_type, dst_pos,
1477 src_type, src_pos, n);
1478 if (!ls->dim)
1479 return isl_local_space_free(ls);
1481 return ls;
1484 /* Remove any internal structure of the domain of "ls".
1485 * If there is any such internal structure in the input,
1486 * then the name of the corresponding space is also removed.
1488 __isl_give isl_local_space *isl_local_space_flatten_domain(
1489 __isl_take isl_local_space *ls)
1491 if (!ls)
1492 return NULL;
1494 if (!ls->dim->nested[0])
1495 return ls;
1497 ls = isl_local_space_cow(ls);
1498 if (!ls)
1499 return NULL;
1501 ls->dim = isl_space_flatten_domain(ls->dim);
1502 if (!ls->dim)
1503 return isl_local_space_free(ls);
1505 return ls;
1508 /* Remove any internal structure of the range of "ls".
1509 * If there is any such internal structure in the input,
1510 * then the name of the corresponding space is also removed.
1512 __isl_give isl_local_space *isl_local_space_flatten_range(
1513 __isl_take isl_local_space *ls)
1515 if (!ls)
1516 return NULL;
1518 if (!ls->dim->nested[1])
1519 return ls;
1521 ls = isl_local_space_cow(ls);
1522 if (!ls)
1523 return NULL;
1525 ls->dim = isl_space_flatten_range(ls->dim);
1526 if (!ls->dim)
1527 return isl_local_space_free(ls);
1529 return ls;
1532 /* Given the local space "ls" of a map, return the local space of a set
1533 * that lives in a space that wraps the space of "ls" and that has
1534 * the same divs.
1536 __isl_give isl_local_space *isl_local_space_wrap(__isl_take isl_local_space *ls)
1538 ls = isl_local_space_cow(ls);
1539 if (!ls)
1540 return NULL;
1542 ls->dim = isl_space_wrap(ls->dim);
1543 if (!ls->dim)
1544 return isl_local_space_free(ls);
1546 return ls;
1549 /* Lift the point "pnt", living in the space of "ls"
1550 * to live in a space with extra coordinates corresponding
1551 * to the local variables of "ls".
1553 __isl_give isl_point *isl_local_space_lift_point(__isl_take isl_local_space *ls,
1554 __isl_take isl_point *pnt)
1556 unsigned n_local;
1557 isl_space *space;
1558 isl_local *local;
1559 isl_vec *vec;
1561 if (isl_local_space_check_has_space(ls, isl_point_peek_space(pnt)) < 0)
1562 goto error;
1564 local = isl_local_space_peek_local(ls);
1565 n_local = isl_local_space_dim(ls, isl_dim_div);
1567 space = isl_point_take_space(pnt);
1568 vec = isl_point_take_vec(pnt);
1570 space = isl_space_lift(space, n_local);
1571 vec = isl_local_extend_point_vec(local, vec);
1573 pnt = isl_point_restore_vec(pnt, vec);
1574 pnt = isl_point_restore_space(pnt, space);
1576 isl_local_space_free(ls);
1578 return pnt;
1579 error:
1580 isl_local_space_free(ls);
1581 isl_point_free(pnt);
1582 return NULL;