isl_vertices.c: tab_for_shifted_cone: allocate room for equality constraints
[isl.git] / isl_local_space.c
blob9bc8c8dea454cba853ee41b5304810ceb9c58d9a
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_map_private.h>
15 #include <isl_local_space_private.h>
16 #include <isl_space_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_vec_private.h>
20 #include <isl_seq.h>
21 #include <isl_local.h>
23 isl_ctx *isl_local_space_get_ctx(__isl_keep isl_local_space *ls)
25 return ls ? ls->dim->ctx : NULL;
28 /* Return a hash value that digests "ls".
30 uint32_t isl_local_space_get_hash(__isl_keep isl_local_space *ls)
32 uint32_t hash, space_hash, div_hash;
34 if (!ls)
35 return 0;
37 hash = isl_hash_init();
38 space_hash = isl_space_get_hash(ls->dim);
39 isl_hash_hash(hash, space_hash);
40 div_hash = isl_mat_get_hash(ls->div);
41 isl_hash_hash(hash, div_hash);
43 return hash;
46 __isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
47 __isl_take isl_mat *div)
49 isl_ctx *ctx;
50 isl_local_space *ls = NULL;
52 if (!dim || !div)
53 goto error;
55 ctx = isl_space_get_ctx(dim);
56 ls = isl_calloc_type(ctx, struct isl_local_space);
57 if (!ls)
58 goto error;
60 ls->ref = 1;
61 ls->dim = dim;
62 ls->div = div;
64 return ls;
65 error:
66 isl_mat_free(div);
67 isl_space_free(dim);
68 isl_local_space_free(ls);
69 return NULL;
72 __isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *dim,
73 unsigned n_div)
75 isl_ctx *ctx;
76 isl_mat *div;
77 unsigned total;
79 if (!dim)
80 return NULL;
82 total = isl_space_dim(dim, isl_dim_all);
84 ctx = isl_space_get_ctx(dim);
85 div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
86 return isl_local_space_alloc_div(dim, div);
89 __isl_give isl_local_space *isl_local_space_from_space(__isl_take isl_space *dim)
91 return isl_local_space_alloc(dim, 0);
94 __isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
96 if (!ls)
97 return NULL;
99 ls->ref++;
100 return ls;
103 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
105 if (!ls)
106 return NULL;
108 return isl_local_space_alloc_div(isl_space_copy(ls->dim),
109 isl_mat_copy(ls->div));
113 __isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
115 if (!ls)
116 return NULL;
118 if (ls->ref == 1)
119 return ls;
120 ls->ref--;
121 return isl_local_space_dup(ls);
124 __isl_null isl_local_space *isl_local_space_free(
125 __isl_take isl_local_space *ls)
127 if (!ls)
128 return NULL;
130 if (--ls->ref > 0)
131 return NULL;
133 isl_space_free(ls->dim);
134 isl_mat_free(ls->div);
136 free(ls);
138 return NULL;
141 /* Is the local space that of a parameter domain?
143 isl_bool isl_local_space_is_params(__isl_keep isl_local_space *ls)
145 if (!ls)
146 return isl_bool_error;
147 return isl_space_is_params(ls->dim);
150 /* Is the local space that of a set?
152 isl_bool isl_local_space_is_set(__isl_keep isl_local_space *ls)
154 return ls ? isl_space_is_set(ls->dim) : isl_bool_error;
157 /* Do "ls1" and "ls2" have the same space?
159 isl_bool isl_local_space_has_equal_space(__isl_keep isl_local_space *ls1,
160 __isl_keep isl_local_space *ls2)
162 if (!ls1 || !ls2)
163 return isl_bool_error;
165 return isl_space_is_equal(ls1->dim, ls2->dim);
168 /* Return true if the two local spaces are identical, with identical
169 * expressions for the integer divisions.
171 isl_bool isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
172 __isl_keep isl_local_space *ls2)
174 isl_bool equal;
176 equal = isl_local_space_has_equal_space(ls1, ls2);
177 if (equal < 0 || !equal)
178 return equal;
180 if (!isl_local_space_divs_known(ls1))
181 return isl_bool_false;
182 if (!isl_local_space_divs_known(ls2))
183 return isl_bool_false;
185 return isl_mat_is_equal(ls1->div, ls2->div);
188 /* Compare two isl_local_spaces.
190 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
191 * than "ls2" and 0 if they are equal.
193 int isl_local_space_cmp(__isl_keep isl_local_space *ls1,
194 __isl_keep isl_local_space *ls2)
196 int cmp;
198 if (ls1 == ls2)
199 return 0;
200 if (!ls1)
201 return -1;
202 if (!ls2)
203 return 1;
205 cmp = isl_space_cmp(ls1->dim, ls2->dim);
206 if (cmp != 0)
207 return cmp;
209 return isl_local_cmp(ls1->div, ls2->div);
212 int isl_local_space_dim(__isl_keep isl_local_space *ls,
213 enum isl_dim_type type)
215 if (!ls)
216 return 0;
217 if (type == isl_dim_div)
218 return ls->div->n_row;
219 if (type == isl_dim_all)
220 return isl_space_dim(ls->dim, isl_dim_all) + ls->div->n_row;
221 return isl_space_dim(ls->dim, type);
224 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
225 enum isl_dim_type type)
227 isl_space *dim;
229 if (!ls)
230 return 0;
232 dim = ls->dim;
233 switch (type) {
234 case isl_dim_cst: return 0;
235 case isl_dim_param: return 1;
236 case isl_dim_in: return 1 + dim->nparam;
237 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
238 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
239 default: return 0;
243 /* Return the position of the dimension of the given type and name
244 * in "ls".
245 * Return -1 if no such dimension can be found.
247 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space *ls,
248 enum isl_dim_type type, const char *name)
250 if (!ls)
251 return -1;
252 if (type == isl_dim_div)
253 return -1;
254 return isl_space_find_dim_by_name(ls->dim, type, name);
257 /* Does the given dimension have a name?
259 isl_bool isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
260 enum isl_dim_type type, unsigned pos)
262 return ls ? isl_space_has_dim_name(ls->dim, type, pos) : isl_bool_error;
265 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
266 enum isl_dim_type type, unsigned pos)
268 return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
271 isl_bool isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
272 enum isl_dim_type type, unsigned pos)
274 return ls ? isl_space_has_dim_id(ls->dim, type, pos) : isl_bool_error;
277 __isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
278 enum isl_dim_type type, unsigned pos)
280 return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
283 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
284 int pos)
286 isl_aff *aff;
288 if (!ls)
289 return NULL;
291 if (pos < 0 || pos >= ls->div->n_row)
292 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
293 "index out of bounds", return NULL);
295 if (isl_int_is_zero(ls->div->row[pos][0]))
296 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
297 "expression of div unknown", return NULL);
298 if (!isl_local_space_is_set(ls))
299 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
300 "cannot represent divs of map spaces", return NULL);
302 aff = isl_aff_alloc(isl_local_space_copy(ls));
303 if (!aff)
304 return NULL;
305 isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
306 return aff;
309 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
311 if (!ls)
312 return NULL;
314 return isl_space_copy(ls->dim);
317 /* Replace the identifier of the tuple of type "type" by "id".
319 __isl_give isl_local_space *isl_local_space_set_tuple_id(
320 __isl_take isl_local_space *ls,
321 enum isl_dim_type type, __isl_take isl_id *id)
323 ls = isl_local_space_cow(ls);
324 if (!ls)
325 goto error;
326 ls->dim = isl_space_set_tuple_id(ls->dim, type, id);
327 if (!ls->dim)
328 return isl_local_space_free(ls);
329 return ls;
330 error:
331 isl_id_free(id);
332 return NULL;
335 __isl_give isl_local_space *isl_local_space_set_dim_name(
336 __isl_take isl_local_space *ls,
337 enum isl_dim_type type, unsigned pos, const char *s)
339 ls = isl_local_space_cow(ls);
340 if (!ls)
341 return NULL;
342 ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
343 if (!ls->dim)
344 return isl_local_space_free(ls);
346 return ls;
349 __isl_give isl_local_space *isl_local_space_set_dim_id(
350 __isl_take isl_local_space *ls,
351 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
353 ls = isl_local_space_cow(ls);
354 if (!ls)
355 goto error;
356 ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
357 if (!ls->dim)
358 return isl_local_space_free(ls);
360 return ls;
361 error:
362 isl_id_free(id);
363 return NULL;
366 __isl_give isl_local_space *isl_local_space_reset_space(
367 __isl_take isl_local_space *ls, __isl_take isl_space *dim)
369 ls = isl_local_space_cow(ls);
370 if (!ls || !dim)
371 goto error;
373 isl_space_free(ls->dim);
374 ls->dim = dim;
376 return ls;
377 error:
378 isl_local_space_free(ls);
379 isl_space_free(dim);
380 return NULL;
383 /* Reorder the columns of the given div definitions according to the
384 * given reordering.
385 * The order of the divs themselves is assumed not to change.
387 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
388 __isl_take isl_reordering *r)
390 int i, j;
391 isl_mat *mat;
392 int extra;
394 if (!div || !r)
395 goto error;
397 extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
398 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
399 if (!mat)
400 goto error;
402 for (i = 0; i < div->n_row; ++i) {
403 isl_seq_cpy(mat->row[i], div->row[i], 2);
404 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
405 for (j = 0; j < r->len; ++j)
406 isl_int_set(mat->row[i][2 + r->pos[j]],
407 div->row[i][2 + j]);
410 isl_reordering_free(r);
411 isl_mat_free(div);
412 return mat;
413 error:
414 isl_reordering_free(r);
415 isl_mat_free(div);
416 return NULL;
419 /* Reorder the dimensions of "ls" according to the given reordering.
420 * The reordering r is assumed to have been extended with the local
421 * variables, leaving them in the same order.
423 __isl_give isl_local_space *isl_local_space_realign(
424 __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
426 ls = isl_local_space_cow(ls);
427 if (!ls || !r)
428 goto error;
430 ls->div = reorder_divs(ls->div, isl_reordering_copy(r));
431 if (!ls->div)
432 goto error;
434 ls = isl_local_space_reset_space(ls, isl_space_copy(r->dim));
436 isl_reordering_free(r);
437 return ls;
438 error:
439 isl_local_space_free(ls);
440 isl_reordering_free(r);
441 return NULL;
444 __isl_give isl_local_space *isl_local_space_add_div(
445 __isl_take isl_local_space *ls, __isl_take isl_vec *div)
447 ls = isl_local_space_cow(ls);
448 if (!ls || !div)
449 goto error;
451 if (ls->div->n_col != div->size)
452 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
453 "incompatible dimensions", goto error);
455 ls->div = isl_mat_add_zero_cols(ls->div, 1);
456 ls->div = isl_mat_add_rows(ls->div, 1);
457 if (!ls->div)
458 goto error;
460 isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
461 isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
463 isl_vec_free(div);
464 return ls;
465 error:
466 isl_local_space_free(ls);
467 isl_vec_free(div);
468 return NULL;
471 __isl_give isl_local_space *isl_local_space_replace_divs(
472 __isl_take isl_local_space *ls, __isl_take isl_mat *div)
474 ls = isl_local_space_cow(ls);
476 if (!ls || !div)
477 goto error;
479 isl_mat_free(ls->div);
480 ls->div = div;
481 return ls;
482 error:
483 isl_mat_free(div);
484 isl_local_space_free(ls);
485 return NULL;
488 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
489 * defined by "exp".
491 static void expand_row(__isl_keep isl_mat *dst, int d,
492 __isl_keep isl_mat *src, int s, int *exp)
494 int i;
495 unsigned c = src->n_col - src->n_row;
497 isl_seq_cpy(dst->row[d], src->row[s], c);
498 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
500 for (i = 0; i < s; ++i)
501 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
504 /* Compare (known) divs.
505 * Return non-zero if at least one of the two divs is unknown.
506 * In particular, if both divs are unknown, we respect their
507 * current order. Otherwise, we sort the known div after the unknown
508 * div only if the known div depends on the unknown div.
510 static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
511 unsigned n_row, unsigned n_col)
513 int li, lj;
514 int unknown_i, unknown_j;
516 unknown_i = isl_int_is_zero(row_i[0]);
517 unknown_j = isl_int_is_zero(row_j[0]);
519 if (unknown_i && unknown_j)
520 return i - j;
522 if (unknown_i)
523 li = n_col - n_row + i;
524 else
525 li = isl_seq_last_non_zero(row_i, n_col);
526 if (unknown_j)
527 lj = n_col - n_row + j;
528 else
529 lj = isl_seq_last_non_zero(row_j, n_col);
531 if (li != lj)
532 return li - lj;
534 return isl_seq_cmp(row_i, row_j, n_col);
537 /* Call cmp_row for divs in a matrix.
539 int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
541 return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
544 /* Call cmp_row for divs in a basic map.
546 static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
547 unsigned total)
549 return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
552 /* Sort the divs in "bmap".
554 * We first make sure divs are placed after divs on which they depend.
555 * Then we perform a simple insertion sort based on the same ordering
556 * that is used in isl_merge_divs.
558 __isl_give isl_basic_map *isl_basic_map_sort_divs(
559 __isl_take isl_basic_map *bmap)
561 int i, j;
562 unsigned total;
564 bmap = isl_basic_map_order_divs(bmap);
565 if (!bmap)
566 return NULL;
567 if (bmap->n_div <= 1)
568 return bmap;
570 total = 2 + isl_basic_map_total_dim(bmap);
571 for (i = 1; i < bmap->n_div; ++i) {
572 for (j = i - 1; j >= 0; --j) {
573 if (bmap_cmp_row(bmap, j, j + 1, total) <= 0)
574 break;
575 isl_basic_map_swap_div(bmap, j, j + 1);
579 return bmap;
582 /* Sort the divs in the basic maps of "map".
584 __isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
586 return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
589 /* Combine the two lists of divs into a single list.
590 * For each row i in div1, exp1[i] is set to the position of the corresponding
591 * row in the result. Similarly for div2 and exp2.
592 * This function guarantees
593 * exp1[i] >= i
594 * exp1[i+1] > exp1[i]
595 * For optimal merging, the two input list should have been sorted.
597 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
598 __isl_keep isl_mat *div2, int *exp1, int *exp2)
600 int i, j, k;
601 isl_mat *div = NULL;
602 unsigned d;
604 if (!div1 || !div2)
605 return NULL;
607 d = div1->n_col - div1->n_row;
608 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
609 d + div1->n_row + div2->n_row);
610 if (!div)
611 return NULL;
613 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
614 int cmp;
616 expand_row(div, k, div1, i, exp1);
617 expand_row(div, k + 1, div2, j, exp2);
619 cmp = isl_mat_cmp_div(div, k, k + 1);
620 if (cmp == 0) {
621 exp1[i++] = k;
622 exp2[j++] = k;
623 } else if (cmp < 0) {
624 exp1[i++] = k;
625 } else {
626 exp2[j++] = k;
627 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
630 for (; i < div1->n_row; ++i, ++k) {
631 expand_row(div, k, div1, i, exp1);
632 exp1[i] = k;
634 for (; j < div2->n_row; ++j, ++k) {
635 expand_row(div, k, div2, j, exp2);
636 exp2[j] = k;
639 div->n_row = k;
640 div->n_col = d + k;
642 return div;
645 /* Swap divs "a" and "b" in "ls".
647 __isl_give isl_local_space *isl_local_space_swap_div(
648 __isl_take isl_local_space *ls, int a, int b)
650 int offset;
652 ls = isl_local_space_cow(ls);
653 if (!ls)
654 return NULL;
655 if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
656 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
657 "index out of bounds", return isl_local_space_free(ls));
658 offset = ls->div->n_col - ls->div->n_row;
659 ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
660 ls->div = isl_mat_swap_rows(ls->div, a, b);
661 if (!ls->div)
662 return isl_local_space_free(ls);
663 return ls;
666 /* Construct a local space that contains all the divs in either
667 * "ls1" or "ls2".
669 __isl_give isl_local_space *isl_local_space_intersect(
670 __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
672 isl_ctx *ctx;
673 int *exp1 = NULL;
674 int *exp2 = NULL;
675 isl_mat *div;
676 int equal;
678 if (!ls1 || !ls2)
679 goto error;
681 ctx = isl_local_space_get_ctx(ls1);
682 if (!isl_space_is_equal(ls1->dim, ls2->dim))
683 isl_die(ctx, isl_error_invalid,
684 "spaces should be identical", goto error);
686 if (ls2->div->n_row == 0) {
687 isl_local_space_free(ls2);
688 return ls1;
691 if (ls1->div->n_row == 0) {
692 isl_local_space_free(ls1);
693 return ls2;
696 exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
697 exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
698 if (!exp1 || !exp2)
699 goto error;
701 div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
702 if (!div)
703 goto error;
705 equal = isl_mat_is_equal(ls1->div, div);
706 if (equal < 0)
707 goto error;
708 if (!equal)
709 ls1 = isl_local_space_cow(ls1);
710 if (!ls1)
711 goto error;
713 free(exp1);
714 free(exp2);
715 isl_local_space_free(ls2);
716 isl_mat_free(ls1->div);
717 ls1->div = div;
719 return ls1;
720 error:
721 free(exp1);
722 free(exp2);
723 isl_local_space_free(ls1);
724 isl_local_space_free(ls2);
725 return NULL;
728 /* Does "ls" have an explicit representation for div "div"?
730 isl_bool isl_local_space_div_is_known(__isl_keep isl_local_space *ls, int div)
732 if (!ls)
733 return isl_bool_error;
734 return isl_local_div_is_known(ls->div, div);
737 /* Does "ls" have an explicit representation for all local variables?
739 isl_bool isl_local_space_divs_known(__isl_keep isl_local_space *ls)
741 int i;
743 if (!ls)
744 return isl_bool_error;
746 for (i = 0; i < ls->div->n_row; ++i) {
747 isl_bool known = isl_local_space_div_is_known(ls, i);
748 if (known < 0 || !known)
749 return known;
752 return isl_bool_true;
755 __isl_give isl_local_space *isl_local_space_domain(
756 __isl_take isl_local_space *ls)
758 ls = isl_local_space_drop_dims(ls, isl_dim_out,
759 0, isl_local_space_dim(ls, isl_dim_out));
760 ls = isl_local_space_cow(ls);
761 if (!ls)
762 return NULL;
763 ls->dim = isl_space_domain(ls->dim);
764 if (!ls->dim)
765 return isl_local_space_free(ls);
766 return ls;
769 __isl_give isl_local_space *isl_local_space_range(
770 __isl_take isl_local_space *ls)
772 ls = isl_local_space_drop_dims(ls, isl_dim_in,
773 0, isl_local_space_dim(ls, isl_dim_in));
774 ls = isl_local_space_cow(ls);
775 if (!ls)
776 return NULL;
778 ls->dim = isl_space_range(ls->dim);
779 if (!ls->dim)
780 return isl_local_space_free(ls);
781 return ls;
784 /* Construct a local space for a map that has the given local
785 * space as domain and that has a zero-dimensional range.
787 __isl_give isl_local_space *isl_local_space_from_domain(
788 __isl_take isl_local_space *ls)
790 ls = isl_local_space_cow(ls);
791 if (!ls)
792 return NULL;
793 ls->dim = isl_space_from_domain(ls->dim);
794 if (!ls->dim)
795 return isl_local_space_free(ls);
796 return ls;
799 __isl_give isl_local_space *isl_local_space_add_dims(
800 __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
802 int pos;
804 if (!ls)
805 return NULL;
806 pos = isl_local_space_dim(ls, type);
807 return isl_local_space_insert_dims(ls, type, pos, n);
810 /* Remove common factor of non-constant terms and denominator.
812 static void normalize_div(__isl_keep isl_local_space *ls, int div)
814 isl_ctx *ctx = ls->div->ctx;
815 unsigned total = ls->div->n_col - 2;
817 isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
818 isl_int_gcd(ctx->normalize_gcd,
819 ctx->normalize_gcd, ls->div->row[div][0]);
820 if (isl_int_is_one(ctx->normalize_gcd))
821 return;
823 isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
824 ctx->normalize_gcd, total);
825 isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
826 ctx->normalize_gcd);
827 isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
828 ctx->normalize_gcd);
831 /* Exploit the equalities in "eq" to simplify the expressions of
832 * the integer divisions in "ls".
833 * The integer divisions in "ls" are assumed to appear as regular
834 * dimensions in "eq".
836 __isl_give isl_local_space *isl_local_space_substitute_equalities(
837 __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
839 int i, j, k;
840 unsigned total;
841 unsigned n_div;
843 if (!ls || !eq)
844 goto error;
846 total = isl_space_dim(eq->dim, isl_dim_all);
847 if (isl_local_space_dim(ls, isl_dim_all) != total)
848 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
849 "spaces don't match", goto error);
850 total++;
851 n_div = eq->n_div;
852 for (i = 0; i < eq->n_eq; ++i) {
853 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
854 if (j < 0 || j == 0 || j >= total)
855 continue;
857 for (k = 0; k < ls->div->n_row; ++k) {
858 if (isl_int_is_zero(ls->div->row[k][1 + j]))
859 continue;
860 ls = isl_local_space_cow(ls);
861 if (!ls)
862 goto error;
863 ls->div = isl_mat_cow(ls->div);
864 if (!ls->div)
865 goto error;
866 isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
867 &ls->div->row[k][0]);
868 normalize_div(ls, k);
872 isl_basic_set_free(eq);
873 return ls;
874 error:
875 isl_basic_set_free(eq);
876 isl_local_space_free(ls);
877 return NULL;
880 /* Plug in the affine expressions "subs" of length "subs_len" (including
881 * the denominator and the constant term) into the variable at position "pos"
882 * of the "n" div expressions starting at "first".
884 * Let i be the dimension to replace and let "subs" be of the form
886 * f/d
888 * Any integer division starting at "first" with a non-zero coefficient for i,
890 * floor((a i + g)/m)
892 * is replaced by
894 * floor((a f + d g)/(m d))
896 __isl_give isl_local_space *isl_local_space_substitute_seq(
897 __isl_take isl_local_space *ls,
898 enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
899 int first, int n)
901 int i;
902 isl_int v;
904 if (n == 0)
905 return ls;
906 ls = isl_local_space_cow(ls);
907 if (!ls)
908 return NULL;
909 ls->div = isl_mat_cow(ls->div);
910 if (!ls->div)
911 return isl_local_space_free(ls);
913 if (first + n > ls->div->n_row)
914 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
915 "index out of bounds", return isl_local_space_free(ls));
917 pos += isl_local_space_offset(ls, type);
919 isl_int_init(v);
920 for (i = first; i < first + n; ++i) {
921 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
922 continue;
923 isl_seq_substitute(ls->div->row[i], pos, subs,
924 ls->div->n_col, subs_len, v);
925 normalize_div(ls, i);
927 isl_int_clear(v);
929 return ls;
932 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
933 * of "ls".
935 * Let i be the dimension to replace and let "subs" be of the form
937 * f/d
939 * Any integer division with a non-zero coefficient for i,
941 * floor((a i + g)/m)
943 * is replaced by
945 * floor((a f + d g)/(m d))
947 __isl_give isl_local_space *isl_local_space_substitute(
948 __isl_take isl_local_space *ls,
949 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
951 ls = isl_local_space_cow(ls);
952 if (!ls || !subs)
953 return isl_local_space_free(ls);
955 if (!isl_space_is_equal(ls->dim, subs->ls->dim))
956 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
957 "spaces don't match", return isl_local_space_free(ls));
958 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
959 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
960 "cannot handle divs yet",
961 return isl_local_space_free(ls));
963 return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
964 subs->v->size, 0, ls->div->n_row);
967 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
968 enum isl_dim_type type)
970 if (!ls)
971 return -1;
972 return isl_space_is_named_or_nested(ls->dim, type);
975 __isl_give isl_local_space *isl_local_space_drop_dims(
976 __isl_take isl_local_space *ls,
977 enum isl_dim_type type, unsigned first, unsigned n)
979 isl_ctx *ctx;
981 if (!ls)
982 return NULL;
983 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
984 return ls;
986 ctx = isl_local_space_get_ctx(ls);
987 if (first + n > isl_local_space_dim(ls, type))
988 isl_die(ctx, isl_error_invalid, "range out of bounds",
989 return isl_local_space_free(ls));
991 ls = isl_local_space_cow(ls);
992 if (!ls)
993 return NULL;
995 if (type == isl_dim_div) {
996 ls->div = isl_mat_drop_rows(ls->div, first, n);
997 } else {
998 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
999 if (!ls->dim)
1000 return isl_local_space_free(ls);
1003 first += 1 + isl_local_space_offset(ls, type);
1004 ls->div = isl_mat_drop_cols(ls->div, first, n);
1005 if (!ls->div)
1006 return isl_local_space_free(ls);
1008 return ls;
1011 __isl_give isl_local_space *isl_local_space_insert_dims(
1012 __isl_take isl_local_space *ls,
1013 enum isl_dim_type type, unsigned first, unsigned n)
1015 isl_ctx *ctx;
1017 if (!ls)
1018 return NULL;
1019 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1020 return ls;
1022 ctx = isl_local_space_get_ctx(ls);
1023 if (first > isl_local_space_dim(ls, type))
1024 isl_die(ctx, isl_error_invalid, "position out of bounds",
1025 return isl_local_space_free(ls));
1027 ls = isl_local_space_cow(ls);
1028 if (!ls)
1029 return NULL;
1031 if (type == isl_dim_div) {
1032 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
1033 } else {
1034 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
1035 if (!ls->dim)
1036 return isl_local_space_free(ls);
1039 first += 1 + isl_local_space_offset(ls, type);
1040 ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
1041 if (!ls->div)
1042 return isl_local_space_free(ls);
1044 return ls;
1047 /* Check if the constraints pointed to by "constraint" is a div
1048 * constraint corresponding to div "div" in "ls".
1050 * That is, if div = floor(f/m), then check if the constraint is
1052 * f - m d >= 0
1053 * or
1054 * -(f-(m-1)) + m d >= 0
1056 int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
1057 isl_int *constraint, unsigned div)
1059 unsigned pos;
1061 if (!ls)
1062 return -1;
1064 if (isl_int_is_zero(ls->div->row[div][0]))
1065 return 0;
1067 pos = isl_local_space_offset(ls, isl_dim_div) + div;
1069 if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
1070 int neg;
1071 isl_int_sub(ls->div->row[div][1],
1072 ls->div->row[div][1], ls->div->row[div][0]);
1073 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1074 neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
1075 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1076 isl_int_add(ls->div->row[div][1],
1077 ls->div->row[div][1], ls->div->row[div][0]);
1078 if (!neg)
1079 return 0;
1080 if (isl_seq_first_non_zero(constraint+pos+1,
1081 ls->div->n_row-div-1) != -1)
1082 return 0;
1083 } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
1084 if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
1085 return 0;
1086 if (isl_seq_first_non_zero(constraint+pos+1,
1087 ls->div->n_row-div-1) != -1)
1088 return 0;
1089 } else
1090 return 0;
1092 return 1;
1096 * Set active[i] to 1 if the dimension at position i is involved
1097 * in the linear expression l.
1099 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
1101 int i, j;
1102 isl_ctx *ctx;
1103 int *active = NULL;
1104 unsigned total;
1105 unsigned offset;
1107 ctx = isl_local_space_get_ctx(ls);
1108 total = isl_local_space_dim(ls, isl_dim_all);
1109 active = isl_calloc_array(ctx, int, total);
1110 if (total && !active)
1111 return NULL;
1113 for (i = 0; i < total; ++i)
1114 active[i] = !isl_int_is_zero(l[i]);
1116 offset = isl_local_space_offset(ls, isl_dim_div) - 1;
1117 for (i = ls->div->n_row - 1; i >= 0; --i) {
1118 if (!active[offset + i])
1119 continue;
1120 for (j = 0; j < total; ++j)
1121 active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
1124 return active;
1127 /* Given a local space "ls" of a set, create a local space
1128 * for the lift of the set. In particular, the result
1129 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1130 * range of the wrapped map.
1132 __isl_give isl_local_space *isl_local_space_lift(
1133 __isl_take isl_local_space *ls)
1135 ls = isl_local_space_cow(ls);
1136 if (!ls)
1137 return NULL;
1139 ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1140 ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1141 if (!ls->dim || !ls->div)
1142 return isl_local_space_free(ls);
1144 return ls;
1147 /* Construct a basic map that maps a set living in local space "ls"
1148 * to the corresponding lifted local space.
1150 __isl_give isl_basic_map *isl_local_space_lifting(
1151 __isl_take isl_local_space *ls)
1153 isl_basic_map *lifting;
1154 isl_basic_set *bset;
1156 if (!ls)
1157 return NULL;
1158 if (!isl_local_space_is_set(ls))
1159 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1160 "lifting only defined on set spaces", goto error);
1162 bset = isl_basic_set_from_local_space(ls);
1163 lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1164 lifting = isl_basic_map_domain_map(lifting);
1165 lifting = isl_basic_map_reverse(lifting);
1167 return lifting;
1168 error:
1169 isl_local_space_free(ls);
1170 return NULL;
1173 /* Compute the preimage of "ls" under the function represented by "ma".
1174 * In other words, plug in "ma" in "ls". The result is a local space
1175 * that is part of the domain space of "ma".
1177 * If the divs in "ls" are represented as
1179 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1181 * and ma is represented by
1183 * x = D(p) + F(y) + G(divs')
1185 * then the resulting divs are
1187 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1189 * We first copy over the divs from "ma" and then
1190 * we add the modified divs from "ls".
1192 __isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1193 __isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1195 int i;
1196 isl_space *space;
1197 isl_local_space *res = NULL;
1198 int n_div_ls, n_div_ma;
1199 isl_int f, c1, c2, g;
1201 ma = isl_multi_aff_align_divs(ma);
1202 if (!ls || !ma)
1203 goto error;
1204 if (!isl_space_is_range_internal(ls->dim, ma->space))
1205 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1206 "spaces don't match", goto error);
1208 n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1209 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
1211 space = isl_space_domain(isl_multi_aff_get_space(ma));
1212 res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1213 if (!res)
1214 goto error;
1216 if (n_div_ma) {
1217 isl_mat_free(res->div);
1218 res->div = isl_mat_copy(ma->p[0]->ls->div);
1219 res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1220 res->div = isl_mat_add_rows(res->div, n_div_ls);
1221 if (!res->div)
1222 goto error;
1225 isl_int_init(f);
1226 isl_int_init(c1);
1227 isl_int_init(c2);
1228 isl_int_init(g);
1230 for (i = 0; i < ls->div->n_row; ++i) {
1231 if (isl_int_is_zero(ls->div->row[i][0])) {
1232 isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1233 continue;
1235 isl_seq_preimage(res->div->row[n_div_ma + i], ls->div->row[i],
1236 ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1);
1237 normalize_div(res, n_div_ma + i);
1240 isl_int_clear(f);
1241 isl_int_clear(c1);
1242 isl_int_clear(c2);
1243 isl_int_clear(g);
1245 isl_local_space_free(ls);
1246 isl_multi_aff_free(ma);
1247 return res;
1248 error:
1249 isl_local_space_free(ls);
1250 isl_multi_aff_free(ma);
1251 isl_local_space_free(res);
1252 return NULL;
1255 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1256 * to dimensions of "dst_type" at "dst_pos".
1258 * Moving to/from local dimensions is not allowed.
1259 * We currently assume that the dimension type changes.
1261 __isl_give isl_local_space *isl_local_space_move_dims(
1262 __isl_take isl_local_space *ls,
1263 enum isl_dim_type dst_type, unsigned dst_pos,
1264 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1266 unsigned g_dst_pos;
1267 unsigned g_src_pos;
1269 if (!ls)
1270 return NULL;
1271 if (n == 0 &&
1272 !isl_local_space_is_named_or_nested(ls, src_type) &&
1273 !isl_local_space_is_named_or_nested(ls, dst_type))
1274 return ls;
1276 if (src_pos + n > isl_local_space_dim(ls, src_type))
1277 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1278 "range out of bounds", return isl_local_space_free(ls));
1279 if (dst_pos > isl_local_space_dim(ls, dst_type))
1280 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1281 "position out of bounds",
1282 return isl_local_space_free(ls));
1283 if (src_type == isl_dim_div)
1284 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1285 "cannot move divs", return isl_local_space_free(ls));
1286 if (dst_type == isl_dim_div)
1287 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1288 "cannot move to divs", return isl_local_space_free(ls));
1289 if (dst_type == src_type && dst_pos == src_pos)
1290 return ls;
1291 if (dst_type == src_type)
1292 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1293 "moving dims within the same type not supported",
1294 return isl_local_space_free(ls));
1296 ls = isl_local_space_cow(ls);
1297 if (!ls)
1298 return NULL;
1300 g_src_pos = 1 + isl_local_space_offset(ls, src_type) + src_pos;
1301 g_dst_pos = 1 + isl_local_space_offset(ls, dst_type) + dst_pos;
1302 if (dst_type > src_type)
1303 g_dst_pos -= n;
1304 ls->div = isl_mat_move_cols(ls->div, g_dst_pos, g_src_pos, n);
1305 if (!ls->div)
1306 return isl_local_space_free(ls);
1307 ls->dim = isl_space_move_dims(ls->dim, dst_type, dst_pos,
1308 src_type, src_pos, n);
1309 if (!ls->dim)
1310 return isl_local_space_free(ls);
1312 return ls;
1315 /* Remove any internal structure of the domain of "ls".
1316 * If there is any such internal structure in the input,
1317 * then the name of the corresponding space is also removed.
1319 __isl_give isl_local_space *isl_local_space_flatten_domain(
1320 __isl_take isl_local_space *ls)
1322 if (!ls)
1323 return NULL;
1325 if (!ls->dim->nested[0])
1326 return ls;
1328 ls = isl_local_space_cow(ls);
1329 if (!ls)
1330 return NULL;
1332 ls->dim = isl_space_flatten_domain(ls->dim);
1333 if (!ls->dim)
1334 return isl_local_space_free(ls);
1336 return ls;
1339 /* Remove any internal structure of the range of "ls".
1340 * If there is any such internal structure in the input,
1341 * then the name of the corresponding space is also removed.
1343 __isl_give isl_local_space *isl_local_space_flatten_range(
1344 __isl_take isl_local_space *ls)
1346 if (!ls)
1347 return NULL;
1349 if (!ls->dim->nested[1])
1350 return ls;
1352 ls = isl_local_space_cow(ls);
1353 if (!ls)
1354 return NULL;
1356 ls->dim = isl_space_flatten_range(ls->dim);
1357 if (!ls->dim)
1358 return isl_local_space_free(ls);
1360 return ls;
1363 /* Given the local space "ls" of a map, return the local space of a set
1364 * that lives in a space that wraps the space of "ls" and that has
1365 * the same divs.
1367 __isl_give isl_local_space *isl_local_space_wrap(__isl_take isl_local_space *ls)
1369 ls = isl_local_space_cow(ls);
1370 if (!ls)
1371 return NULL;
1373 ls->dim = isl_space_wrap(ls->dim);
1374 if (!ls->dim)
1375 return isl_local_space_free(ls);
1377 return ls;