add isl_basic_map_has_dim_id
[isl.git] / isl_local_space.c
blob42527c714343fb390f3668a4b6bee07248ffb7a2
1 /*
2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <isl_ctx_private.h>
12 #include <isl_map_private.h>
13 #include <isl_local_space_private.h>
14 #include <isl_space_private.h>
15 #include <isl_mat_private.h>
16 #include <isl_aff_private.h>
17 #include <isl/seq.h>
19 isl_ctx *isl_local_space_get_ctx(__isl_keep isl_local_space *ls)
21 return ls ? ls->dim->ctx : NULL;
24 __isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
25 __isl_take isl_mat *div)
27 isl_ctx *ctx;
28 isl_local_space *ls = NULL;
30 if (!dim)
31 goto error;
33 ctx = isl_space_get_ctx(dim);
34 ls = isl_calloc_type(ctx, struct isl_local_space);
35 if (!ls)
36 goto error;
38 ls->ref = 1;
39 ls->dim = dim;
40 ls->div = div;
42 return ls;
43 error:
44 isl_space_free(dim);
45 isl_local_space_free(ls);
46 return NULL;
49 __isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *dim,
50 unsigned n_div)
52 isl_ctx *ctx;
53 isl_mat *div;
54 unsigned total;
56 if (!dim)
57 return NULL;
59 total = isl_space_dim(dim, isl_dim_all);
61 ctx = isl_space_get_ctx(dim);
62 div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
63 return isl_local_space_alloc_div(dim, div);
66 __isl_give isl_local_space *isl_local_space_from_space(__isl_take isl_space *dim)
68 return isl_local_space_alloc(dim, 0);
71 __isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
73 if (!ls)
74 return NULL;
76 ls->ref++;
77 return ls;
80 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
82 if (!ls)
83 return NULL;
85 return isl_local_space_alloc_div(isl_space_copy(ls->dim),
86 isl_mat_copy(ls->div));
90 __isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
92 if (!ls)
93 return NULL;
95 if (ls->ref == 1)
96 return ls;
97 ls->ref--;
98 return isl_local_space_dup(ls);
101 void *isl_local_space_free(__isl_take isl_local_space *ls)
103 if (!ls)
104 return NULL;
106 if (--ls->ref > 0)
107 return NULL;
109 isl_space_free(ls->dim);
110 isl_mat_free(ls->div);
112 free(ls);
114 return NULL;
117 /* Is the local space that of a set?
119 int isl_local_space_is_set(__isl_keep isl_local_space *ls)
121 return ls ? isl_space_is_set(ls->dim) : -1;
124 /* Return true if the two local spaces are identical, with identical
125 * expressions for the integer divisions.
127 int isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
128 __isl_keep isl_local_space *ls2)
130 int equal;
132 if (!ls1 || !ls2)
133 return -1;
135 equal = isl_space_is_equal(ls1->dim, ls2->dim);
136 if (equal < 0 || !equal)
137 return equal;
139 if (!isl_local_space_divs_known(ls1))
140 return 0;
141 if (!isl_local_space_divs_known(ls2))
142 return 0;
144 return isl_mat_is_equal(ls1->div, ls2->div);
147 int isl_local_space_dim(__isl_keep isl_local_space *ls,
148 enum isl_dim_type type)
150 if (!ls)
151 return 0;
152 if (type == isl_dim_div)
153 return ls->div->n_row;
154 if (type == isl_dim_all)
155 return isl_space_dim(ls->dim, isl_dim_all) + ls->div->n_row;
156 return isl_space_dim(ls->dim, type);
159 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
160 enum isl_dim_type type)
162 isl_space *dim;
164 if (!ls)
165 return 0;
167 dim = ls->dim;
168 switch (type) {
169 case isl_dim_cst: return 0;
170 case isl_dim_param: return 1;
171 case isl_dim_in: return 1 + dim->nparam;
172 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
173 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
174 default: return 0;
178 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
179 enum isl_dim_type type, unsigned pos)
181 return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
184 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
185 int pos)
187 isl_aff *aff;
189 if (!ls)
190 return NULL;
192 if (pos < 0 || pos >= ls->div->n_row)
193 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
194 "index out of bounds", return NULL);
196 if (isl_int_is_zero(ls->div->row[pos][0]))
197 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
198 "expression of div unknown", return NULL);
200 aff = isl_aff_alloc(isl_local_space_copy(ls));
201 if (!aff)
202 return NULL;
203 isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
204 return aff;
207 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
209 if (!ls)
210 return NULL;
212 return isl_space_copy(ls->dim);
215 __isl_give isl_local_space *isl_local_space_set_dim_name(
216 __isl_take isl_local_space *ls,
217 enum isl_dim_type type, unsigned pos, const char *s)
219 ls = isl_local_space_cow(ls);
220 if (!ls)
221 return NULL;
222 ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
223 if (!ls->dim)
224 return isl_local_space_free(ls);
226 return ls;
229 __isl_give isl_local_space *isl_local_space_set_dim_id(
230 __isl_take isl_local_space *ls,
231 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
233 ls = isl_local_space_cow(ls);
234 if (!ls)
235 return isl_id_free(id);
236 ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
237 if (!ls->dim)
238 return isl_local_space_free(ls);
240 return ls;
243 __isl_give isl_local_space *isl_local_space_reset_space(
244 __isl_take isl_local_space *ls, __isl_take isl_space *dim)
246 ls = isl_local_space_cow(ls);
247 if (!ls || !dim)
248 goto error;
250 isl_space_free(ls->dim);
251 ls->dim = dim;
253 return ls;
254 error:
255 isl_local_space_free(ls);
256 isl_space_free(dim);
257 return NULL;
260 /* Reorder the columns of the given div definitions according to the
261 * given reordering.
262 * The order of the divs themselves is assumed not to change.
264 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
265 __isl_take isl_reordering *r)
267 int i, j;
268 isl_mat *mat;
269 int extra;
271 if (!div || !r)
272 goto error;
274 extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
275 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
276 if (!mat)
277 goto error;
279 for (i = 0; i < div->n_row; ++i) {
280 isl_seq_cpy(mat->row[i], div->row[i], 2);
281 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
282 for (j = 0; j < r->len; ++j)
283 isl_int_set(mat->row[i][2 + r->pos[j]],
284 div->row[i][2 + j]);
287 isl_reordering_free(r);
288 isl_mat_free(div);
289 return mat;
290 error:
291 isl_reordering_free(r);
292 isl_mat_free(div);
293 return NULL;
296 /* Reorder the dimensions of "ls" according to the given reordering.
297 * The reordering r is assumed to have been extended with the local
298 * variables, leaving them in the same order.
300 __isl_give isl_local_space *isl_local_space_realign(
301 __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
303 ls = isl_local_space_cow(ls);
304 if (!ls || !r)
305 goto error;
307 ls->div = reorder_divs(ls->div, isl_reordering_copy(r));
308 if (!ls->div)
309 goto error;
311 ls = isl_local_space_reset_space(ls, isl_space_copy(r->dim));
313 isl_reordering_free(r);
314 return ls;
315 error:
316 isl_local_space_free(ls);
317 isl_reordering_free(r);
318 return NULL;
321 __isl_give isl_local_space *isl_local_space_add_div(
322 __isl_take isl_local_space *ls, __isl_take isl_vec *div)
324 ls = isl_local_space_cow(ls);
325 if (!ls || !div)
326 goto error;
328 if (ls->div->n_col != div->size)
329 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
330 "incompatible dimensions", goto error);
332 ls->div = isl_mat_add_zero_cols(ls->div, 1);
333 ls->div = isl_mat_add_rows(ls->div, 1);
334 if (!ls->div)
335 goto error;
337 isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
338 isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
340 isl_vec_free(div);
341 return ls;
342 error:
343 isl_local_space_free(ls);
344 isl_vec_free(div);
345 return NULL;
348 __isl_give isl_local_space *isl_local_space_replace_divs(
349 __isl_take isl_local_space *ls, __isl_take isl_mat *div)
351 ls = isl_local_space_cow(ls);
353 if (!ls || !div)
354 goto error;
356 isl_mat_free(ls->div);
357 ls->div = div;
358 return ls;
359 error:
360 isl_mat_free(div);
361 isl_local_space_free(ls);
362 return NULL;
365 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
366 * defined by "exp".
368 static void expand_row(__isl_keep isl_mat *dst, int d,
369 __isl_keep isl_mat *src, int s, int *exp)
371 int i;
372 unsigned c = src->n_col - src->n_row;
374 isl_seq_cpy(dst->row[d], src->row[s], c);
375 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
377 for (i = 0; i < s; ++i)
378 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
381 /* Compare (known) divs.
382 * Return non-zero if at least one of the two divs is unknown.
384 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
386 int li, lj;
388 if (isl_int_is_zero(div->row[j][0]))
389 return -1;
390 if (isl_int_is_zero(div->row[i][0]))
391 return 1;
393 li = isl_seq_last_non_zero(div->row[i], div->n_col);
394 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
396 if (li != lj)
397 return li - lj;
399 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
402 /* Combine the two lists of divs into a single list.
403 * For each row i in div1, exp1[i] is set to the position of the corresponding
404 * row in the result. Similarly for div2 and exp2.
405 * This function guarantees
406 * exp1[i] >= i
407 * exp1[i+1] > exp1[i]
408 * For optimal merging, the two input list should have been sorted.
410 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
411 __isl_keep isl_mat *div2, int *exp1, int *exp2)
413 int i, j, k;
414 isl_mat *div = NULL;
415 unsigned d = div1->n_col - div1->n_row;
417 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
418 d + div1->n_row + div2->n_row);
419 if (!div)
420 return NULL;
422 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
423 int cmp;
425 expand_row(div, k, div1, i, exp1);
426 expand_row(div, k + 1, div2, j, exp2);
428 cmp = cmp_row(div, k, k + 1);
429 if (cmp == 0) {
430 exp1[i++] = k;
431 exp2[j++] = k;
432 } else if (cmp < 0) {
433 exp1[i++] = k;
434 } else {
435 exp2[j++] = k;
436 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
439 for (; i < div1->n_row; ++i, ++k) {
440 expand_row(div, k, div1, i, exp1);
441 exp1[i] = k;
443 for (; j < div2->n_row; ++j, ++k) {
444 expand_row(div, k, div2, j, exp2);
445 exp2[j] = k;
448 div->n_row = k;
449 div->n_col = d + k;
451 return div;
454 int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
456 int i;
458 if (!ls)
459 return -1;
461 for (i = 0; i < ls->div->n_row; ++i)
462 if (isl_int_is_zero(ls->div->row[i][0]))
463 return 0;
465 return 1;
468 __isl_give isl_local_space *isl_local_space_domain(
469 __isl_take isl_local_space *ls)
471 ls = isl_local_space_drop_dims(ls, isl_dim_out,
472 0, isl_local_space_dim(ls, isl_dim_out));
473 ls = isl_local_space_cow(ls);
474 if (!ls)
475 return NULL;
476 ls->dim = isl_space_domain(ls->dim);
477 if (!ls->dim)
478 return isl_local_space_free(ls);
479 return ls;
482 /* Construct a local space for a map that has the given local
483 * space as domain and that has a zero-dimensional range.
485 __isl_give isl_local_space *isl_local_space_from_domain(
486 __isl_take isl_local_space *ls)
488 ls = isl_local_space_cow(ls);
489 if (!ls)
490 return NULL;
491 ls->dim = isl_space_from_domain(ls->dim);
492 if (!ls->dim)
493 return isl_local_space_free(ls);
494 return ls;
497 __isl_give isl_local_space *isl_local_space_add_dims(
498 __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
500 int pos;
502 if (!ls)
503 return NULL;
504 pos = isl_local_space_dim(ls, type);
505 return isl_local_space_insert_dims(ls, type, pos, n);
508 /* Remove common factor of non-constant terms and denominator.
510 static void normalize_div(__isl_keep isl_local_space *ls, int div)
512 isl_ctx *ctx = ls->div->ctx;
513 unsigned total = ls->div->n_col - 2;
515 isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
516 isl_int_gcd(ctx->normalize_gcd,
517 ctx->normalize_gcd, ls->div->row[div][0]);
518 if (isl_int_is_one(ctx->normalize_gcd))
519 return;
521 isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
522 ctx->normalize_gcd, total);
523 isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
524 ctx->normalize_gcd);
525 isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
526 ctx->normalize_gcd);
529 /* Exploit the equalities in "eq" to simplify the expressions of
530 * the integer divisions in "ls".
531 * The integer divisions in "ls" are assumed to appear as regular
532 * dimensions in "eq".
534 __isl_give isl_local_space *isl_local_space_substitute_equalities(
535 __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
537 int i, j, k;
538 unsigned total;
539 unsigned n_div;
541 ls = isl_local_space_cow(ls);
542 if (!ls || !eq)
543 goto error;
545 total = isl_space_dim(eq->dim, isl_dim_all);
546 if (isl_local_space_dim(ls, isl_dim_all) != total)
547 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
548 "dimensions don't match", goto error);
549 total++;
550 n_div = eq->n_div;
551 for (i = 0; i < eq->n_eq; ++i) {
552 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
553 if (j < 0 || j == 0 || j >= total)
554 continue;
556 for (k = 0; k < ls->div->n_row; ++k) {
557 if (isl_int_is_zero(ls->div->row[k][1 + j]))
558 continue;
559 isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
560 &ls->div->row[k][0]);
561 normalize_div(ls, k);
565 isl_basic_set_free(eq);
566 return ls;
567 error:
568 isl_basic_set_free(eq);
569 isl_local_space_free(ls);
570 return NULL;
573 int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
574 enum isl_dim_type type)
576 if (!ls)
577 return -1;
578 return isl_space_is_named_or_nested(ls->dim, type);
581 __isl_give isl_local_space *isl_local_space_drop_dims(
582 __isl_take isl_local_space *ls,
583 enum isl_dim_type type, unsigned first, unsigned n)
585 isl_ctx *ctx;
587 if (!ls)
588 return NULL;
589 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
590 return ls;
592 ctx = isl_local_space_get_ctx(ls);
593 if (first + n > isl_local_space_dim(ls, type))
594 isl_die(ctx, isl_error_invalid, "range out of bounds",
595 return isl_local_space_free(ls));
597 ls = isl_local_space_cow(ls);
598 if (!ls)
599 return NULL;
601 if (type == isl_dim_div) {
602 ls->div = isl_mat_drop_rows(ls->div, first, n);
603 } else {
604 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
605 if (!ls->dim)
606 return isl_local_space_free(ls);
609 first += 1 + isl_local_space_offset(ls, type);
610 ls->div = isl_mat_drop_cols(ls->div, first, n);
611 if (!ls->div)
612 return isl_local_space_free(ls);
614 return ls;
617 __isl_give isl_local_space *isl_local_space_insert_dims(
618 __isl_take isl_local_space *ls,
619 enum isl_dim_type type, unsigned first, unsigned n)
621 isl_ctx *ctx;
623 if (!ls)
624 return NULL;
625 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
626 return ls;
628 ctx = isl_local_space_get_ctx(ls);
629 if (first > isl_local_space_dim(ls, type))
630 isl_die(ctx, isl_error_invalid, "position out of bounds",
631 return isl_local_space_free(ls));
633 ls = isl_local_space_cow(ls);
634 if (!ls)
635 return NULL;
637 if (type == isl_dim_div) {
638 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
639 } else {
640 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
641 if (!ls->dim)
642 return isl_local_space_free(ls);
645 first += 1 + isl_local_space_offset(ls, type);
646 ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
647 if (!ls->div)
648 return isl_local_space_free(ls);
650 return ls;
653 /* Check if the constraints pointed to by "constraint" is a div
654 * constraint corresponding to div "div" in "ls".
656 * That is, if div = floor(f/m), then check if the constraint is
658 * f - m d >= 0
659 * or
660 * -(f-(m-1)) + m d >= 0
662 int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
663 isl_int *constraint, unsigned div)
665 unsigned pos;
667 if (!ls)
668 return -1;
670 if (isl_int_is_zero(ls->div->row[div][0]))
671 return 0;
673 pos = isl_local_space_offset(ls, isl_dim_div) + div;
675 if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
676 int neg;
677 isl_int_sub(ls->div->row[div][1],
678 ls->div->row[div][1], ls->div->row[div][0]);
679 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
680 neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
681 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
682 isl_int_add(ls->div->row[div][1],
683 ls->div->row[div][1], ls->div->row[div][0]);
684 if (!neg)
685 return 0;
686 if (isl_seq_first_non_zero(constraint+pos+1,
687 ls->div->n_row-div-1) != -1)
688 return 0;
689 } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
690 if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
691 return 0;
692 if (isl_seq_first_non_zero(constraint+pos+1,
693 ls->div->n_row-div-1) != -1)
694 return 0;
695 } else
696 return 0;
698 return 1;
702 * Set active[i] to 1 if the dimension at position i is involved
703 * in the linear expression l.
705 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
707 int i, j;
708 isl_ctx *ctx;
709 int *active = NULL;
710 unsigned total;
711 unsigned offset;
713 ctx = isl_local_space_get_ctx(ls);
714 total = isl_local_space_dim(ls, isl_dim_all);
715 active = isl_calloc_array(ctx, int, total);
716 if (!active)
717 return NULL;
719 for (i = 0; i < total; ++i)
720 active[i] = !isl_int_is_zero(l[i]);
722 offset = isl_local_space_offset(ls, isl_dim_div) - 1;
723 for (i = ls->div->n_row - 1; i >= 0; --i) {
724 if (!active[offset + i])
725 continue;
726 for (j = 0; j < total; ++j)
727 active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
730 return active;