isl_ast_build_expr.c: ast_expr_from_pw_aff: delay construction of expression
[isl.git] / isl_local_space.c
blob7c06b59d2abfe83cd0322b78974b94cfe0e4dd8b
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>
22 isl_ctx *isl_local_space_get_ctx(__isl_keep isl_local_space *ls)
24 return ls ? ls->dim->ctx : NULL;
27 __isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
28 __isl_take isl_mat *div)
30 isl_ctx *ctx;
31 isl_local_space *ls = NULL;
33 if (!dim || !div)
34 goto error;
36 ctx = isl_space_get_ctx(dim);
37 ls = isl_calloc_type(ctx, struct isl_local_space);
38 if (!ls)
39 goto error;
41 ls->ref = 1;
42 ls->dim = dim;
43 ls->div = div;
45 return ls;
46 error:
47 isl_mat_free(div);
48 isl_space_free(dim);
49 isl_local_space_free(ls);
50 return NULL;
53 __isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *dim,
54 unsigned n_div)
56 isl_ctx *ctx;
57 isl_mat *div;
58 unsigned total;
60 if (!dim)
61 return NULL;
63 total = isl_space_dim(dim, isl_dim_all);
65 ctx = isl_space_get_ctx(dim);
66 div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
67 return isl_local_space_alloc_div(dim, div);
70 __isl_give isl_local_space *isl_local_space_from_space(__isl_take isl_space *dim)
72 return isl_local_space_alloc(dim, 0);
75 __isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
77 if (!ls)
78 return NULL;
80 ls->ref++;
81 return ls;
84 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
86 if (!ls)
87 return NULL;
89 return isl_local_space_alloc_div(isl_space_copy(ls->dim),
90 isl_mat_copy(ls->div));
94 __isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
96 if (!ls)
97 return NULL;
99 if (ls->ref == 1)
100 return ls;
101 ls->ref--;
102 return isl_local_space_dup(ls);
105 __isl_null isl_local_space *isl_local_space_free(
106 __isl_take isl_local_space *ls)
108 if (!ls)
109 return NULL;
111 if (--ls->ref > 0)
112 return NULL;
114 isl_space_free(ls->dim);
115 isl_mat_free(ls->div);
117 free(ls);
119 return NULL;
122 /* Is the local space that of a parameter domain?
124 isl_bool isl_local_space_is_params(__isl_keep isl_local_space *ls)
126 if (!ls)
127 return isl_bool_error;
128 return isl_space_is_params(ls->dim);
131 /* Is the local space that of a set?
133 isl_bool isl_local_space_is_set(__isl_keep isl_local_space *ls)
135 return ls ? isl_space_is_set(ls->dim) : isl_bool_error;
138 /* Return true if the two local spaces are identical, with identical
139 * expressions for the integer divisions.
141 isl_bool isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
142 __isl_keep isl_local_space *ls2)
144 isl_bool equal;
146 if (!ls1 || !ls2)
147 return isl_bool_error;
149 equal = isl_space_is_equal(ls1->dim, ls2->dim);
150 if (equal < 0 || !equal)
151 return equal;
153 if (!isl_local_space_divs_known(ls1))
154 return isl_bool_false;
155 if (!isl_local_space_divs_known(ls2))
156 return isl_bool_false;
158 return isl_mat_is_equal(ls1->div, ls2->div);
161 /* Compare two isl_local_spaces.
163 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
164 * than "ls2" and 0 if they are equal.
166 * The order is fairly arbitrary. We do "prefer" divs that only involve
167 * earlier dimensions in the sense that we consider local spaces where
168 * the first differing div involves earlier dimensions to be smaller.
170 int isl_local_space_cmp(__isl_keep isl_local_space *ls1,
171 __isl_keep isl_local_space *ls2)
173 int i;
174 int cmp;
175 int known1, known2;
176 int last1, last2;
177 int n_col;
179 if (ls1 == ls2)
180 return 0;
181 if (!ls1)
182 return -1;
183 if (!ls2)
184 return 1;
186 cmp = isl_space_cmp(ls1->dim, ls2->dim);
187 if (cmp != 0)
188 return cmp;
190 if (ls1->div->n_row != ls2->div->n_row)
191 return ls1->div->n_row - ls2->div->n_row;
193 n_col = isl_mat_cols(ls1->div);
194 for (i = 0; i < ls1->div->n_row; ++i) {
195 known1 = isl_local_space_div_is_known(ls1, i);
196 known2 = isl_local_space_div_is_known(ls2, i);
197 if (!known1 && !known2)
198 continue;
199 if (!known1)
200 return 1;
201 if (!known2)
202 return -1;
203 last1 = isl_seq_last_non_zero(ls1->div->row[i] + 1, n_col - 1);
204 last2 = isl_seq_last_non_zero(ls2->div->row[i] + 1, n_col - 1);
205 if (last1 != last2)
206 return last1 - last2;
207 cmp = isl_seq_cmp(ls1->div->row[i], ls2->div->row[i], n_col);
208 if (cmp != 0)
209 return cmp;
212 return 0;
215 int isl_local_space_dim(__isl_keep isl_local_space *ls,
216 enum isl_dim_type type)
218 if (!ls)
219 return 0;
220 if (type == isl_dim_div)
221 return ls->div->n_row;
222 if (type == isl_dim_all)
223 return isl_space_dim(ls->dim, isl_dim_all) + ls->div->n_row;
224 return isl_space_dim(ls->dim, type);
227 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
228 enum isl_dim_type type)
230 isl_space *dim;
232 if (!ls)
233 return 0;
235 dim = ls->dim;
236 switch (type) {
237 case isl_dim_cst: return 0;
238 case isl_dim_param: return 1;
239 case isl_dim_in: return 1 + dim->nparam;
240 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
241 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
242 default: return 0;
246 /* Return the position of the dimension of the given type and name
247 * in "ls".
248 * Return -1 if no such dimension can be found.
250 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space *ls,
251 enum isl_dim_type type, const char *name)
253 if (!ls)
254 return -1;
255 if (type == isl_dim_div)
256 return -1;
257 return isl_space_find_dim_by_name(ls->dim, type, name);
260 /* Does the given dimension have a name?
262 isl_bool isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
263 enum isl_dim_type type, unsigned pos)
265 return ls ? isl_space_has_dim_name(ls->dim, type, pos) : isl_bool_error;
268 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
269 enum isl_dim_type type, unsigned pos)
271 return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
274 isl_bool isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
275 enum isl_dim_type type, unsigned pos)
277 return ls ? isl_space_has_dim_id(ls->dim, type, pos) : isl_bool_error;
280 __isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
281 enum isl_dim_type type, unsigned pos)
283 return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
286 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
287 int pos)
289 isl_aff *aff;
291 if (!ls)
292 return NULL;
294 if (pos < 0 || pos >= ls->div->n_row)
295 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
296 "index out of bounds", return NULL);
298 if (isl_int_is_zero(ls->div->row[pos][0]))
299 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
300 "expression of div unknown", return NULL);
301 if (!isl_local_space_is_set(ls))
302 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
303 "cannot represent divs of map spaces", return NULL);
305 aff = isl_aff_alloc(isl_local_space_copy(ls));
306 if (!aff)
307 return NULL;
308 isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
309 return aff;
312 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
314 if (!ls)
315 return NULL;
317 return isl_space_copy(ls->dim);
320 /* Replace the identifier of the tuple of type "type" by "id".
322 __isl_give isl_local_space *isl_local_space_set_tuple_id(
323 __isl_take isl_local_space *ls,
324 enum isl_dim_type type, __isl_take isl_id *id)
326 ls = isl_local_space_cow(ls);
327 if (!ls)
328 goto error;
329 ls->dim = isl_space_set_tuple_id(ls->dim, type, id);
330 if (!ls->dim)
331 return isl_local_space_free(ls);
332 return ls;
333 error:
334 isl_id_free(id);
335 return NULL;
338 __isl_give isl_local_space *isl_local_space_set_dim_name(
339 __isl_take isl_local_space *ls,
340 enum isl_dim_type type, unsigned pos, const char *s)
342 ls = isl_local_space_cow(ls);
343 if (!ls)
344 return NULL;
345 ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
346 if (!ls->dim)
347 return isl_local_space_free(ls);
349 return ls;
352 __isl_give isl_local_space *isl_local_space_set_dim_id(
353 __isl_take isl_local_space *ls,
354 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
356 ls = isl_local_space_cow(ls);
357 if (!ls)
358 goto error;
359 ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
360 if (!ls->dim)
361 return isl_local_space_free(ls);
363 return ls;
364 error:
365 isl_id_free(id);
366 return NULL;
369 __isl_give isl_local_space *isl_local_space_reset_space(
370 __isl_take isl_local_space *ls, __isl_take isl_space *dim)
372 ls = isl_local_space_cow(ls);
373 if (!ls || !dim)
374 goto error;
376 isl_space_free(ls->dim);
377 ls->dim = dim;
379 return ls;
380 error:
381 isl_local_space_free(ls);
382 isl_space_free(dim);
383 return NULL;
386 /* Reorder the columns of the given div definitions according to the
387 * given reordering.
388 * The order of the divs themselves is assumed not to change.
390 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
391 __isl_take isl_reordering *r)
393 int i, j;
394 isl_mat *mat;
395 int extra;
397 if (!div || !r)
398 goto error;
400 extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
401 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
402 if (!mat)
403 goto error;
405 for (i = 0; i < div->n_row; ++i) {
406 isl_seq_cpy(mat->row[i], div->row[i], 2);
407 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
408 for (j = 0; j < r->len; ++j)
409 isl_int_set(mat->row[i][2 + r->pos[j]],
410 div->row[i][2 + j]);
413 isl_reordering_free(r);
414 isl_mat_free(div);
415 return mat;
416 error:
417 isl_reordering_free(r);
418 isl_mat_free(div);
419 return NULL;
422 /* Reorder the dimensions of "ls" according to the given reordering.
423 * The reordering r is assumed to have been extended with the local
424 * variables, leaving them in the same order.
426 __isl_give isl_local_space *isl_local_space_realign(
427 __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
429 ls = isl_local_space_cow(ls);
430 if (!ls || !r)
431 goto error;
433 ls->div = reorder_divs(ls->div, isl_reordering_copy(r));
434 if (!ls->div)
435 goto error;
437 ls = isl_local_space_reset_space(ls, isl_space_copy(r->dim));
439 isl_reordering_free(r);
440 return ls;
441 error:
442 isl_local_space_free(ls);
443 isl_reordering_free(r);
444 return NULL;
447 __isl_give isl_local_space *isl_local_space_add_div(
448 __isl_take isl_local_space *ls, __isl_take isl_vec *div)
450 ls = isl_local_space_cow(ls);
451 if (!ls || !div)
452 goto error;
454 if (ls->div->n_col != div->size)
455 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
456 "incompatible dimensions", goto error);
458 ls->div = isl_mat_add_zero_cols(ls->div, 1);
459 ls->div = isl_mat_add_rows(ls->div, 1);
460 if (!ls->div)
461 goto error;
463 isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
464 isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
466 isl_vec_free(div);
467 return ls;
468 error:
469 isl_local_space_free(ls);
470 isl_vec_free(div);
471 return NULL;
474 __isl_give isl_local_space *isl_local_space_replace_divs(
475 __isl_take isl_local_space *ls, __isl_take isl_mat *div)
477 ls = isl_local_space_cow(ls);
479 if (!ls || !div)
480 goto error;
482 isl_mat_free(ls->div);
483 ls->div = div;
484 return ls;
485 error:
486 isl_mat_free(div);
487 isl_local_space_free(ls);
488 return NULL;
491 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
492 * defined by "exp".
494 static void expand_row(__isl_keep isl_mat *dst, int d,
495 __isl_keep isl_mat *src, int s, int *exp)
497 int i;
498 unsigned c = src->n_col - src->n_row;
500 isl_seq_cpy(dst->row[d], src->row[s], c);
501 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
503 for (i = 0; i < s; ++i)
504 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
507 /* Compare (known) divs.
508 * Return non-zero if at least one of the two divs is unknown.
509 * In particular, if both divs are unknown, we respect their
510 * current order. Otherwise, we sort the known div after the unknown
511 * div only if the known div depends on the unknown div.
513 static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
514 unsigned n_row, unsigned n_col)
516 int li, lj;
517 int unknown_i, unknown_j;
519 unknown_i = isl_int_is_zero(row_i[0]);
520 unknown_j = isl_int_is_zero(row_j[0]);
522 if (unknown_i && unknown_j)
523 return i - j;
525 if (unknown_i)
526 li = n_col - n_row + i;
527 else
528 li = isl_seq_last_non_zero(row_i, n_col);
529 if (unknown_j)
530 lj = n_col - n_row + j;
531 else
532 lj = isl_seq_last_non_zero(row_j, n_col);
534 if (li != lj)
535 return li - lj;
537 return isl_seq_cmp(row_i, row_j, n_col);
540 /* Call cmp_row for divs in a matrix.
542 int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
544 return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
547 /* Call cmp_row for divs in a basic map.
549 static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
550 unsigned total)
552 return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
555 /* Sort the divs in "bmap".
557 * We first make sure divs are placed after divs on which they depend.
558 * Then we perform a simple insertion sort based on the same ordering
559 * that is used in isl_merge_divs.
561 __isl_give isl_basic_map *isl_basic_map_sort_divs(
562 __isl_take isl_basic_map *bmap)
564 int i, j;
565 unsigned total;
567 bmap = isl_basic_map_order_divs(bmap);
568 if (!bmap)
569 return NULL;
570 if (bmap->n_div <= 1)
571 return bmap;
573 total = 2 + isl_basic_map_total_dim(bmap);
574 for (i = 1; i < bmap->n_div; ++i) {
575 for (j = i - 1; j >= 0; --j) {
576 if (bmap_cmp_row(bmap, j, j + 1, total) <= 0)
577 break;
578 isl_basic_map_swap_div(bmap, j, j + 1);
582 return bmap;
585 /* Sort the divs in the basic maps of "map".
587 __isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
589 return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
592 /* Combine the two lists of divs into a single list.
593 * For each row i in div1, exp1[i] is set to the position of the corresponding
594 * row in the result. Similarly for div2 and exp2.
595 * This function guarantees
596 * exp1[i] >= i
597 * exp1[i+1] > exp1[i]
598 * For optimal merging, the two input list should have been sorted.
600 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
601 __isl_keep isl_mat *div2, int *exp1, int *exp2)
603 int i, j, k;
604 isl_mat *div = NULL;
605 unsigned d;
607 if (!div1 || !div2)
608 return NULL;
610 d = div1->n_col - div1->n_row;
611 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
612 d + div1->n_row + div2->n_row);
613 if (!div)
614 return NULL;
616 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
617 int cmp;
619 expand_row(div, k, div1, i, exp1);
620 expand_row(div, k + 1, div2, j, exp2);
622 cmp = isl_mat_cmp_div(div, k, k + 1);
623 if (cmp == 0) {
624 exp1[i++] = k;
625 exp2[j++] = k;
626 } else if (cmp < 0) {
627 exp1[i++] = k;
628 } else {
629 exp2[j++] = k;
630 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
633 for (; i < div1->n_row; ++i, ++k) {
634 expand_row(div, k, div1, i, exp1);
635 exp1[i] = k;
637 for (; j < div2->n_row; ++j, ++k) {
638 expand_row(div, k, div2, j, exp2);
639 exp2[j] = k;
642 div->n_row = k;
643 div->n_col = d + k;
645 return div;
648 /* Swap divs "a" and "b" in "ls".
650 __isl_give isl_local_space *isl_local_space_swap_div(
651 __isl_take isl_local_space *ls, int a, int b)
653 int offset;
655 ls = isl_local_space_cow(ls);
656 if (!ls)
657 return NULL;
658 if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
659 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
660 "index out of bounds", return isl_local_space_free(ls));
661 offset = ls->div->n_col - ls->div->n_row;
662 ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
663 ls->div = isl_mat_swap_rows(ls->div, a, b);
664 if (!ls->div)
665 return isl_local_space_free(ls);
666 return ls;
669 /* Construct a local space that contains all the divs in either
670 * "ls1" or "ls2".
672 __isl_give isl_local_space *isl_local_space_intersect(
673 __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
675 isl_ctx *ctx;
676 int *exp1 = NULL;
677 int *exp2 = NULL;
678 isl_mat *div;
679 int equal;
681 if (!ls1 || !ls2)
682 goto error;
684 ctx = isl_local_space_get_ctx(ls1);
685 if (!isl_space_is_equal(ls1->dim, ls2->dim))
686 isl_die(ctx, isl_error_invalid,
687 "spaces should be identical", goto error);
689 if (ls2->div->n_row == 0) {
690 isl_local_space_free(ls2);
691 return ls1;
694 if (ls1->div->n_row == 0) {
695 isl_local_space_free(ls1);
696 return ls2;
699 exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
700 exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
701 if (!exp1 || !exp2)
702 goto error;
704 div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
705 if (!div)
706 goto error;
708 equal = isl_mat_is_equal(ls1->div, div);
709 if (equal < 0)
710 goto error;
711 if (!equal)
712 ls1 = isl_local_space_cow(ls1);
713 if (!ls1)
714 goto error;
716 free(exp1);
717 free(exp2);
718 isl_local_space_free(ls2);
719 isl_mat_free(ls1->div);
720 ls1->div = div;
722 return ls1;
723 error:
724 free(exp1);
725 free(exp2);
726 isl_local_space_free(ls1);
727 isl_local_space_free(ls2);
728 return NULL;
731 /* Does "ls" have an explicit representation for div "div"?
733 int isl_local_space_div_is_known(__isl_keep isl_local_space *ls, int div)
735 if (!ls)
736 return -1;
737 if (div < 0 || div >= ls->div->n_row)
738 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
739 "position out of bounds", return -1);
740 return !isl_int_is_zero(ls->div->row[div][0]);
743 /* Does "ls" have an explicit representation for all local variables?
745 isl_bool isl_local_space_divs_known(__isl_keep isl_local_space *ls)
747 int i;
749 if (!ls)
750 return isl_bool_error;
752 for (i = 0; i < ls->div->n_row; ++i)
753 if (isl_int_is_zero(ls->div->row[i][0]))
754 return isl_bool_false;
756 return isl_bool_true;
759 __isl_give isl_local_space *isl_local_space_domain(
760 __isl_take isl_local_space *ls)
762 ls = isl_local_space_drop_dims(ls, isl_dim_out,
763 0, isl_local_space_dim(ls, isl_dim_out));
764 ls = isl_local_space_cow(ls);
765 if (!ls)
766 return NULL;
767 ls->dim = isl_space_domain(ls->dim);
768 if (!ls->dim)
769 return isl_local_space_free(ls);
770 return ls;
773 __isl_give isl_local_space *isl_local_space_range(
774 __isl_take isl_local_space *ls)
776 ls = isl_local_space_drop_dims(ls, isl_dim_in,
777 0, isl_local_space_dim(ls, isl_dim_in));
778 ls = isl_local_space_cow(ls);
779 if (!ls)
780 return NULL;
782 ls->dim = isl_space_range(ls->dim);
783 if (!ls->dim)
784 return isl_local_space_free(ls);
785 return ls;
788 /* Construct a local space for a map that has the given local
789 * space as domain and that has a zero-dimensional range.
791 __isl_give isl_local_space *isl_local_space_from_domain(
792 __isl_take isl_local_space *ls)
794 ls = isl_local_space_cow(ls);
795 if (!ls)
796 return NULL;
797 ls->dim = isl_space_from_domain(ls->dim);
798 if (!ls->dim)
799 return isl_local_space_free(ls);
800 return ls;
803 __isl_give isl_local_space *isl_local_space_add_dims(
804 __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
806 int pos;
808 if (!ls)
809 return NULL;
810 pos = isl_local_space_dim(ls, type);
811 return isl_local_space_insert_dims(ls, type, pos, n);
814 /* Remove common factor of non-constant terms and denominator.
816 static void normalize_div(__isl_keep isl_local_space *ls, int div)
818 isl_ctx *ctx = ls->div->ctx;
819 unsigned total = ls->div->n_col - 2;
821 isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
822 isl_int_gcd(ctx->normalize_gcd,
823 ctx->normalize_gcd, ls->div->row[div][0]);
824 if (isl_int_is_one(ctx->normalize_gcd))
825 return;
827 isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
828 ctx->normalize_gcd, total);
829 isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
830 ctx->normalize_gcd);
831 isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
832 ctx->normalize_gcd);
835 /* Exploit the equalities in "eq" to simplify the expressions of
836 * the integer divisions in "ls".
837 * The integer divisions in "ls" are assumed to appear as regular
838 * dimensions in "eq".
840 __isl_give isl_local_space *isl_local_space_substitute_equalities(
841 __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
843 int i, j, k;
844 unsigned total;
845 unsigned n_div;
847 if (!ls || !eq)
848 goto error;
850 total = isl_space_dim(eq->dim, isl_dim_all);
851 if (isl_local_space_dim(ls, isl_dim_all) != total)
852 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
853 "spaces don't match", goto error);
854 total++;
855 n_div = eq->n_div;
856 for (i = 0; i < eq->n_eq; ++i) {
857 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
858 if (j < 0 || j == 0 || j >= total)
859 continue;
861 for (k = 0; k < ls->div->n_row; ++k) {
862 if (isl_int_is_zero(ls->div->row[k][1 + j]))
863 continue;
864 ls = isl_local_space_cow(ls);
865 if (!ls)
866 goto error;
867 ls->div = isl_mat_cow(ls->div);
868 if (!ls->div)
869 goto error;
870 isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
871 &ls->div->row[k][0]);
872 normalize_div(ls, k);
876 isl_basic_set_free(eq);
877 return ls;
878 error:
879 isl_basic_set_free(eq);
880 isl_local_space_free(ls);
881 return NULL;
884 /* Plug in the affine expressions "subs" of length "subs_len" (including
885 * the denominator and the constant term) into the variable at position "pos"
886 * of the "n" div expressions starting at "first".
888 * Let i be the dimension to replace and let "subs" be of the form
890 * f/d
892 * Any integer division starting at "first" with a non-zero coefficient for i,
894 * floor((a i + g)/m)
896 * is replaced by
898 * floor((a f + d g)/(m d))
900 __isl_give isl_local_space *isl_local_space_substitute_seq(
901 __isl_take isl_local_space *ls,
902 enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
903 int first, int n)
905 int i;
906 isl_int v;
908 if (n == 0)
909 return ls;
910 ls = isl_local_space_cow(ls);
911 if (!ls)
912 return NULL;
913 ls->div = isl_mat_cow(ls->div);
914 if (!ls->div)
915 return isl_local_space_free(ls);
917 if (first + n > ls->div->n_row)
918 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
919 "index out of bounds", return isl_local_space_free(ls));
921 pos += isl_local_space_offset(ls, type);
923 isl_int_init(v);
924 for (i = first; i < first + n; ++i) {
925 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
926 continue;
927 isl_seq_substitute(ls->div->row[i], pos, subs,
928 ls->div->n_col, subs_len, v);
929 normalize_div(ls, i);
931 isl_int_clear(v);
933 return ls;
936 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
937 * of "ls".
939 * Let i be the dimension to replace and let "subs" be of the form
941 * f/d
943 * Any integer division with a non-zero coefficient for i,
945 * floor((a i + g)/m)
947 * is replaced by
949 * floor((a f + d g)/(m d))
951 __isl_give isl_local_space *isl_local_space_substitute(
952 __isl_take isl_local_space *ls,
953 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
955 ls = isl_local_space_cow(ls);
956 if (!ls || !subs)
957 return isl_local_space_free(ls);
959 if (!isl_space_is_equal(ls->dim, subs->ls->dim))
960 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
961 "spaces don't match", return isl_local_space_free(ls));
962 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
963 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
964 "cannot handle divs yet",
965 return isl_local_space_free(ls));
967 return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
968 subs->v->size, 0, ls->div->n_row);
971 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
972 enum isl_dim_type type)
974 if (!ls)
975 return -1;
976 return isl_space_is_named_or_nested(ls->dim, type);
979 __isl_give isl_local_space *isl_local_space_drop_dims(
980 __isl_take isl_local_space *ls,
981 enum isl_dim_type type, unsigned first, unsigned n)
983 isl_ctx *ctx;
985 if (!ls)
986 return NULL;
987 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
988 return ls;
990 ctx = isl_local_space_get_ctx(ls);
991 if (first + n > isl_local_space_dim(ls, type))
992 isl_die(ctx, isl_error_invalid, "range out of bounds",
993 return isl_local_space_free(ls));
995 ls = isl_local_space_cow(ls);
996 if (!ls)
997 return NULL;
999 if (type == isl_dim_div) {
1000 ls->div = isl_mat_drop_rows(ls->div, first, n);
1001 } else {
1002 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
1003 if (!ls->dim)
1004 return isl_local_space_free(ls);
1007 first += 1 + isl_local_space_offset(ls, type);
1008 ls->div = isl_mat_drop_cols(ls->div, first, n);
1009 if (!ls->div)
1010 return isl_local_space_free(ls);
1012 return ls;
1015 __isl_give isl_local_space *isl_local_space_insert_dims(
1016 __isl_take isl_local_space *ls,
1017 enum isl_dim_type type, unsigned first, unsigned n)
1019 isl_ctx *ctx;
1021 if (!ls)
1022 return NULL;
1023 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1024 return ls;
1026 ctx = isl_local_space_get_ctx(ls);
1027 if (first > isl_local_space_dim(ls, type))
1028 isl_die(ctx, isl_error_invalid, "position out of bounds",
1029 return isl_local_space_free(ls));
1031 ls = isl_local_space_cow(ls);
1032 if (!ls)
1033 return NULL;
1035 if (type == isl_dim_div) {
1036 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
1037 } else {
1038 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
1039 if (!ls->dim)
1040 return isl_local_space_free(ls);
1043 first += 1 + isl_local_space_offset(ls, type);
1044 ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
1045 if (!ls->div)
1046 return isl_local_space_free(ls);
1048 return ls;
1051 /* Check if the constraints pointed to by "constraint" is a div
1052 * constraint corresponding to div "div" in "ls".
1054 * That is, if div = floor(f/m), then check if the constraint is
1056 * f - m d >= 0
1057 * or
1058 * -(f-(m-1)) + m d >= 0
1060 int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
1061 isl_int *constraint, unsigned div)
1063 unsigned pos;
1065 if (!ls)
1066 return -1;
1068 if (isl_int_is_zero(ls->div->row[div][0]))
1069 return 0;
1071 pos = isl_local_space_offset(ls, isl_dim_div) + div;
1073 if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
1074 int neg;
1075 isl_int_sub(ls->div->row[div][1],
1076 ls->div->row[div][1], ls->div->row[div][0]);
1077 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1078 neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
1079 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1080 isl_int_add(ls->div->row[div][1],
1081 ls->div->row[div][1], ls->div->row[div][0]);
1082 if (!neg)
1083 return 0;
1084 if (isl_seq_first_non_zero(constraint+pos+1,
1085 ls->div->n_row-div-1) != -1)
1086 return 0;
1087 } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
1088 if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
1089 return 0;
1090 if (isl_seq_first_non_zero(constraint+pos+1,
1091 ls->div->n_row-div-1) != -1)
1092 return 0;
1093 } else
1094 return 0;
1096 return 1;
1100 * Set active[i] to 1 if the dimension at position i is involved
1101 * in the linear expression l.
1103 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
1105 int i, j;
1106 isl_ctx *ctx;
1107 int *active = NULL;
1108 unsigned total;
1109 unsigned offset;
1111 ctx = isl_local_space_get_ctx(ls);
1112 total = isl_local_space_dim(ls, isl_dim_all);
1113 active = isl_calloc_array(ctx, int, total);
1114 if (total && !active)
1115 return NULL;
1117 for (i = 0; i < total; ++i)
1118 active[i] = !isl_int_is_zero(l[i]);
1120 offset = isl_local_space_offset(ls, isl_dim_div) - 1;
1121 for (i = ls->div->n_row - 1; i >= 0; --i) {
1122 if (!active[offset + i])
1123 continue;
1124 for (j = 0; j < total; ++j)
1125 active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
1128 return active;
1131 /* Given a local space "ls" of a set, create a local space
1132 * for the lift of the set. In particular, the result
1133 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1134 * range of the wrapped map.
1136 __isl_give isl_local_space *isl_local_space_lift(
1137 __isl_take isl_local_space *ls)
1139 ls = isl_local_space_cow(ls);
1140 if (!ls)
1141 return NULL;
1143 ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1144 ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1145 if (!ls->dim || !ls->div)
1146 return isl_local_space_free(ls);
1148 return ls;
1151 /* Construct a basic map that maps a set living in local space "ls"
1152 * to the corresponding lifted local space.
1154 __isl_give isl_basic_map *isl_local_space_lifting(
1155 __isl_take isl_local_space *ls)
1157 isl_basic_map *lifting;
1158 isl_basic_set *bset;
1160 if (!ls)
1161 return NULL;
1162 if (!isl_local_space_is_set(ls))
1163 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1164 "lifting only defined on set spaces", goto error);
1166 bset = isl_basic_set_from_local_space(ls);
1167 lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1168 lifting = isl_basic_map_domain_map(lifting);
1169 lifting = isl_basic_map_reverse(lifting);
1171 return lifting;
1172 error:
1173 isl_local_space_free(ls);
1174 return NULL;
1177 /* Compute the preimage of "ls" under the function represented by "ma".
1178 * In other words, plug in "ma" in "ls". The result is a local space
1179 * that is part of the domain space of "ma".
1181 * If the divs in "ls" are represented as
1183 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1185 * and ma is represented by
1187 * x = D(p) + F(y) + G(divs')
1189 * then the resulting divs are
1191 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1193 * We first copy over the divs from "ma" and then
1194 * we add the modified divs from "ls".
1196 __isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1197 __isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1199 int i;
1200 isl_space *space;
1201 isl_local_space *res = NULL;
1202 int n_div_ls, n_div_ma;
1203 isl_int f, c1, c2, g;
1205 ma = isl_multi_aff_align_divs(ma);
1206 if (!ls || !ma)
1207 goto error;
1208 if (!isl_space_is_range_internal(ls->dim, ma->space))
1209 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1210 "spaces don't match", goto error);
1212 n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1213 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
1215 space = isl_space_domain(isl_multi_aff_get_space(ma));
1216 res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1217 if (!res)
1218 goto error;
1220 if (n_div_ma) {
1221 isl_mat_free(res->div);
1222 res->div = isl_mat_copy(ma->p[0]->ls->div);
1223 res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1224 res->div = isl_mat_add_rows(res->div, n_div_ls);
1225 if (!res->div)
1226 goto error;
1229 isl_int_init(f);
1230 isl_int_init(c1);
1231 isl_int_init(c2);
1232 isl_int_init(g);
1234 for (i = 0; i < ls->div->n_row; ++i) {
1235 if (isl_int_is_zero(ls->div->row[i][0])) {
1236 isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1237 continue;
1239 isl_seq_preimage(res->div->row[n_div_ma + i], ls->div->row[i],
1240 ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1);
1241 normalize_div(res, n_div_ma + i);
1244 isl_int_clear(f);
1245 isl_int_clear(c1);
1246 isl_int_clear(c2);
1247 isl_int_clear(g);
1249 isl_local_space_free(ls);
1250 isl_multi_aff_free(ma);
1251 return res;
1252 error:
1253 isl_local_space_free(ls);
1254 isl_multi_aff_free(ma);
1255 isl_local_space_free(res);
1256 return NULL;
1259 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1260 * to dimensions of "dst_type" at "dst_pos".
1262 * Moving to/from local dimensions is not allowed.
1263 * We currently assume that the dimension type changes.
1265 __isl_give isl_local_space *isl_local_space_move_dims(
1266 __isl_take isl_local_space *ls,
1267 enum isl_dim_type dst_type, unsigned dst_pos,
1268 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1270 unsigned g_dst_pos;
1271 unsigned g_src_pos;
1273 if (!ls)
1274 return NULL;
1275 if (n == 0 &&
1276 !isl_local_space_is_named_or_nested(ls, src_type) &&
1277 !isl_local_space_is_named_or_nested(ls, dst_type))
1278 return ls;
1280 if (src_pos + n > isl_local_space_dim(ls, src_type))
1281 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1282 "range out of bounds", return isl_local_space_free(ls));
1283 if (dst_pos > isl_local_space_dim(ls, dst_type))
1284 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1285 "position out of bounds",
1286 return isl_local_space_free(ls));
1287 if (src_type == isl_dim_div)
1288 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1289 "cannot move divs", return isl_local_space_free(ls));
1290 if (dst_type == isl_dim_div)
1291 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1292 "cannot move to divs", return isl_local_space_free(ls));
1293 if (dst_type == src_type && dst_pos == src_pos)
1294 return ls;
1295 if (dst_type == src_type)
1296 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1297 "moving dims within the same type not supported",
1298 return isl_local_space_free(ls));
1300 ls = isl_local_space_cow(ls);
1301 if (!ls)
1302 return NULL;
1304 g_src_pos = 1 + isl_local_space_offset(ls, src_type) + src_pos;
1305 g_dst_pos = 1 + isl_local_space_offset(ls, dst_type) + dst_pos;
1306 if (dst_type > src_type)
1307 g_dst_pos -= n;
1308 ls->div = isl_mat_move_cols(ls->div, g_dst_pos, g_src_pos, n);
1309 if (!ls->div)
1310 return isl_local_space_free(ls);
1311 ls->dim = isl_space_move_dims(ls->dim, dst_type, dst_pos,
1312 src_type, src_pos, n);
1313 if (!ls->dim)
1314 return isl_local_space_free(ls);
1316 return ls;
1319 /* Remove any internal structure of the domain of "ls".
1320 * If there is any such internal structure in the input,
1321 * then the name of the corresponding space is also removed.
1323 __isl_give isl_local_space *isl_local_space_flatten_domain(
1324 __isl_take isl_local_space *ls)
1326 if (!ls)
1327 return NULL;
1329 if (!ls->dim->nested[0])
1330 return ls;
1332 ls = isl_local_space_cow(ls);
1333 if (!ls)
1334 return NULL;
1336 ls->dim = isl_space_flatten_domain(ls->dim);
1337 if (!ls->dim)
1338 return isl_local_space_free(ls);
1340 return ls;
1343 /* Remove any internal structure of the range of "ls".
1344 * If there is any such internal structure in the input,
1345 * then the name of the corresponding space is also removed.
1347 __isl_give isl_local_space *isl_local_space_flatten_range(
1348 __isl_take isl_local_space *ls)
1350 if (!ls)
1351 return NULL;
1353 if (!ls->dim->nested[1])
1354 return ls;
1356 ls = isl_local_space_cow(ls);
1357 if (!ls)
1358 return NULL;
1360 ls->dim = isl_space_flatten_range(ls->dim);
1361 if (!ls->dim)
1362 return isl_local_space_free(ls);
1364 return ls;
1367 /* Given the local space "ls" of a map, return the local space of a set
1368 * that lives in a space that wraps the space of "ls" and that has
1369 * the same divs.
1371 __isl_give isl_local_space *isl_local_space_wrap(__isl_take isl_local_space *ls)
1373 ls = isl_local_space_cow(ls);
1374 if (!ls)
1375 return NULL;
1377 ls->dim = isl_space_wrap(ls->dim);
1378 if (!ls->dim)
1379 return isl_local_space_free(ls);
1381 return ls;