add isl_local_space_wrapped_reverse
[isl.git] / isl_local_space.c
blobe5abcb24afacaab18f6b8b5d5954ead6aea33c6e
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2012-2014 Ecole Normale Superieure
4 * Copyright 2019 Cerebras Systems
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
15 #include <isl_ctx_private.h>
16 #include <isl/id.h>
17 #include <isl_map_private.h>
18 #include <isl_local_space_private.h>
19 #include <isl_space_private.h>
20 #include <isl_mat_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_vec_private.h>
23 #include <isl_point_private.h>
24 #include <isl_seq.h>
25 #include <isl_local.h>
27 isl_ctx *isl_local_space_get_ctx(__isl_keep isl_local_space *ls)
29 return ls ? ls->dim->ctx : NULL;
32 /* Return a hash value that digests "ls".
34 uint32_t isl_local_space_get_hash(__isl_keep isl_local_space *ls)
36 uint32_t hash, space_hash, div_hash;
38 if (!ls)
39 return 0;
41 hash = isl_hash_init();
42 space_hash = isl_space_get_full_hash(isl_local_space_peek_space(ls));
43 isl_hash_hash(hash, space_hash);
44 div_hash = isl_mat_get_hash(ls->div);
45 isl_hash_hash(hash, div_hash);
47 return hash;
50 __isl_give isl_local_space *isl_local_space_alloc_div(
51 __isl_take isl_space *space, __isl_take isl_mat *div)
53 isl_ctx *ctx;
54 isl_local_space *ls = NULL;
56 if (!space || !div)
57 goto error;
59 ctx = isl_space_get_ctx(space);
60 ls = isl_calloc_type(ctx, struct isl_local_space);
61 if (!ls)
62 goto error;
64 ls->ref = 1;
65 ls->dim = space;
66 ls->div = div;
68 return ls;
69 error:
70 isl_mat_free(div);
71 isl_space_free(space);
72 isl_local_space_free(ls);
73 return NULL;
76 __isl_give isl_local_space *isl_local_space_alloc(__isl_take isl_space *space,
77 unsigned n_div)
79 isl_ctx *ctx;
80 isl_mat *div;
81 isl_size total;
83 if (!space)
84 return NULL;
86 total = isl_space_dim(space, isl_dim_all);
87 if (total < 0)
88 return isl_local_space_from_space(isl_space_free(space));
90 ctx = isl_space_get_ctx(space);
91 div = isl_mat_alloc(ctx, n_div, 1 + 1 + total + n_div);
92 return isl_local_space_alloc_div(space, div);
95 __isl_give isl_local_space *isl_local_space_from_space(
96 __isl_take isl_space *space)
98 return isl_local_space_alloc(space, 0);
101 __isl_give isl_local_space *isl_local_space_copy(__isl_keep isl_local_space *ls)
103 if (!ls)
104 return NULL;
106 ls->ref++;
107 return ls;
110 __isl_give isl_local_space *isl_local_space_dup(__isl_keep isl_local_space *ls)
112 if (!ls)
113 return NULL;
115 return isl_local_space_alloc_div(isl_space_copy(ls->dim),
116 isl_mat_copy(ls->div));
120 __isl_give isl_local_space *isl_local_space_cow(__isl_take isl_local_space *ls)
122 if (!ls)
123 return NULL;
125 if (ls->ref == 1)
126 return ls;
127 ls->ref--;
128 return isl_local_space_dup(ls);
131 __isl_null isl_local_space *isl_local_space_free(
132 __isl_take isl_local_space *ls)
134 if (!ls)
135 return NULL;
137 if (--ls->ref > 0)
138 return NULL;
140 isl_space_free(ls->dim);
141 isl_mat_free(ls->div);
143 free(ls);
145 return NULL;
148 /* Is the local space that of a parameter domain?
150 isl_bool isl_local_space_is_params(__isl_keep isl_local_space *ls)
152 if (!ls)
153 return isl_bool_error;
154 return isl_space_is_params(ls->dim);
157 /* Is the local space that of a set?
159 isl_bool isl_local_space_is_set(__isl_keep isl_local_space *ls)
161 return ls ? isl_space_is_set(ls->dim) : isl_bool_error;
164 #undef TYPE
165 #define TYPE isl_local_space
167 #include "isl_type_has_equal_space_bin_templ.c"
168 #include "isl_type_has_space_templ.c"
170 /* Check that the space of "ls" is equal to "space".
172 static isl_stat isl_local_space_check_has_space(__isl_keep isl_local_space *ls,
173 __isl_keep isl_space *space)
175 isl_bool ok;
177 ok = isl_local_space_has_space(ls, space);
178 if (ok < 0)
179 return isl_stat_error;
180 if (!ok)
181 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
182 "spaces don't match", return isl_stat_error);
183 return isl_stat_ok;
186 /* Return true if the two local spaces are identical, with identical
187 * expressions for the integer divisions.
189 isl_bool isl_local_space_is_equal(__isl_keep isl_local_space *ls1,
190 __isl_keep isl_local_space *ls2)
192 isl_bool equal;
194 equal = isl_local_space_has_equal_space(ls1, ls2);
195 if (equal < 0 || !equal)
196 return equal;
198 if (!isl_local_space_divs_known(ls1))
199 return isl_bool_false;
200 if (!isl_local_space_divs_known(ls2))
201 return isl_bool_false;
203 return isl_mat_is_equal(ls1->div, ls2->div);
206 /* Compare two isl_local_spaces.
208 * Return -1 if "ls1" is "smaller" than "ls2", 1 if "ls1" is "greater"
209 * than "ls2" and 0 if they are equal.
211 int isl_local_space_cmp(__isl_keep isl_local_space *ls1,
212 __isl_keep isl_local_space *ls2)
214 int cmp;
216 if (ls1 == ls2)
217 return 0;
218 if (!ls1)
219 return -1;
220 if (!ls2)
221 return 1;
223 cmp = isl_space_cmp(ls1->dim, ls2->dim);
224 if (cmp != 0)
225 return cmp;
227 return isl_local_cmp(ls1->div, ls2->div);
230 isl_size isl_local_space_dim(__isl_keep isl_local_space *ls,
231 enum isl_dim_type type)
233 if (!ls)
234 return isl_size_error;
235 if (type == isl_dim_div)
236 return ls->div->n_row;
237 if (type == isl_dim_all) {
238 isl_size dim = isl_space_dim(ls->dim, isl_dim_all);
239 if (dim < 0)
240 return isl_size_error;
241 return dim + ls->div->n_row;
243 return isl_space_dim(ls->dim, type);
246 #undef TYPE
247 #define TYPE isl_local_space
248 #include "check_type_range_templ.c"
250 /* Return the position of the variables of the given type
251 * within the sequence of variables of "ls".
253 isl_size isl_local_space_var_offset(__isl_keep isl_local_space *ls,
254 enum isl_dim_type type)
256 isl_space *space;
258 space = isl_local_space_peek_space(ls);
259 if (space < 0)
260 return isl_size_error;
261 switch (type) {
262 case isl_dim_param:
263 case isl_dim_in:
264 case isl_dim_out: return isl_space_offset(space, type);
265 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
266 case isl_dim_cst:
267 default:
268 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
269 "invalid dimension type", return isl_size_error);
273 /* Return the position of the coefficients of the variables of the given type
274 * within the sequence of coefficients of "ls".
276 unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
277 enum isl_dim_type type)
279 if (!ls)
280 return 0;
282 switch (type) {
283 case isl_dim_cst: return 0;
284 case isl_dim_param:
285 case isl_dim_in:
286 case isl_dim_out:
287 case isl_dim_div: return 1 + isl_local_space_var_offset(ls, type);
288 default: return 0;
292 /* Return the position of the dimension of the given type and name
293 * in "ls".
294 * Return -1 if no such dimension can be found.
296 int isl_local_space_find_dim_by_name(__isl_keep isl_local_space *ls,
297 enum isl_dim_type type, const char *name)
299 if (!ls)
300 return -1;
301 if (type == isl_dim_div)
302 return -1;
303 return isl_space_find_dim_by_name(ls->dim, type, name);
306 /* Does the given dimension have a name?
308 isl_bool isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
309 enum isl_dim_type type, unsigned pos)
311 return ls ? isl_space_has_dim_name(ls->dim, type, pos) : isl_bool_error;
314 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
315 enum isl_dim_type type, unsigned pos)
317 return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
320 isl_bool isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
321 enum isl_dim_type type, unsigned pos)
323 return ls ? isl_space_has_dim_id(ls->dim, type, pos) : isl_bool_error;
326 __isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
327 enum isl_dim_type type, unsigned pos)
329 return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
332 /* Return the argument of the integer division at position "pos" in "ls".
333 * All local variables in "ls" are known to have a (complete) explicit
334 * representation.
336 static __isl_give isl_aff *extract_div(__isl_keep isl_local_space *ls, int pos)
338 isl_aff *aff;
340 aff = isl_aff_alloc(isl_local_space_copy(ls));
341 if (!aff)
342 return NULL;
343 isl_seq_cpy(aff->v->el, ls->div->row[pos], aff->v->size);
344 return aff;
347 /* Return the argument of the integer division at position "pos" in "ls".
348 * The integer division at that position is known to have a complete
349 * explicit representation, but some of the others do not.
350 * Remove them first because the domain of an isl_aff
351 * is not allowed to have unknown local variables.
353 static __isl_give isl_aff *drop_unknown_divs_and_extract_div(
354 __isl_keep isl_local_space *ls, int pos)
356 int i;
357 isl_size n;
358 isl_bool unknown;
359 isl_aff *aff;
361 n = isl_local_space_dim(ls, isl_dim_div);
362 if (n < 0)
363 return NULL;
364 ls = isl_local_space_copy(ls);
365 for (i = n - 1; i >= 0; --i) {
366 unknown = isl_local_space_div_is_marked_unknown(ls, i);
367 if (unknown < 0)
368 ls = isl_local_space_free(ls);
369 else if (!unknown)
370 continue;
371 ls = isl_local_space_drop_dims(ls, isl_dim_div, i, 1);
372 if (pos > i)
373 --pos;
375 aff = extract_div(ls, pos);
376 isl_local_space_free(ls);
377 return aff;
380 /* Return the argument of the integer division at position "pos" in "ls".
381 * The integer division is assumed to have a complete explicit
382 * representation. If some of the other integer divisions
383 * do not have an explicit representation, then they need
384 * to be removed first because the domain of an isl_aff
385 * is not allowed to have unknown local variables.
387 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
388 int pos)
390 isl_bool known;
392 if (!ls)
393 return NULL;
395 if (pos < 0 || pos >= ls->div->n_row)
396 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
397 "index out of bounds", return NULL);
399 known = isl_local_space_div_is_known(ls, pos);
400 if (known < 0)
401 return NULL;
402 if (!known)
403 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
404 "expression of div unknown", return NULL);
405 if (!isl_local_space_is_set(ls))
406 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
407 "cannot represent divs of map spaces", return NULL);
409 known = isl_local_space_divs_known(ls);
410 if (known < 0)
411 return NULL;
412 if (known)
413 return extract_div(ls, pos);
414 else
415 return drop_unknown_divs_and_extract_div(ls, pos);
418 /* Return the space of "ls".
420 __isl_keep isl_space *isl_local_space_peek_space(__isl_keep isl_local_space *ls)
422 if (!ls)
423 return NULL;
425 return ls->dim;
428 __isl_give isl_space *isl_local_space_get_space(__isl_keep isl_local_space *ls)
430 return isl_space_copy(isl_local_space_peek_space(ls));
433 /* Return the space of "ls".
434 * This may be either a copy or the space itself
435 * if there is only one reference to "ls".
436 * This allows the space to be modified inplace
437 * if both the local space and its space have only a single reference.
438 * The caller is not allowed to modify "ls" between this call and
439 * a subsequent call to isl_local_space_restore_space.
440 * The only exception is that isl_local_space_free can be called instead.
442 __isl_give isl_space *isl_local_space_take_space(__isl_keep isl_local_space *ls)
444 isl_space *space;
446 if (!ls)
447 return NULL;
448 if (ls->ref != 1)
449 return isl_local_space_get_space(ls);
450 space = ls->dim;
451 ls->dim = NULL;
452 return space;
455 /* Set the space of "ls" to "space", where the space of "ls" may be missing
456 * due to a preceding call to isl_local_space_take_space.
457 * However, in this case, "ls" only has a single reference and
458 * then the call to isl_local_space_cow has no effect.
460 __isl_give isl_local_space *isl_local_space_restore_space(
461 __isl_take isl_local_space *ls, __isl_take isl_space *space)
463 if (!ls || !space)
464 goto error;
466 if (ls->dim == space) {
467 isl_space_free(space);
468 return ls;
471 ls = isl_local_space_cow(ls);
472 if (!ls)
473 goto error;
474 isl_space_free(ls->dim);
475 ls->dim = space;
477 return ls;
478 error:
479 isl_local_space_free(ls);
480 isl_space_free(space);
481 return NULL;
484 /* Return the local variables of "ls".
486 __isl_keep isl_local *isl_local_space_peek_local(__isl_keep isl_local_space *ls)
488 return ls ? ls->div : NULL;
491 /* Return a copy of the local variables of "ls".
493 __isl_keep isl_local *isl_local_space_get_local(__isl_keep isl_local_space *ls)
495 return isl_local_copy(isl_local_space_peek_local(ls));
498 /* Return the local variables of "ls".
499 * This may be either a copy or the local variables itself
500 * if there is only one reference to "ls".
501 * This allows the local variables to be modified inplace
502 * if both the local space and its local variables have only a single reference.
503 * The caller is not allowed to modify "ls" between this call and
504 * the subsequent call to isl_local_space_restore_local.
505 * The only exception is that isl_local_space_free can be called instead.
507 static __isl_give isl_local *isl_local_space_take_local(
508 __isl_keep isl_local_space *ls)
510 isl_local *local;
512 if (!ls)
513 return NULL;
514 if (ls->ref != 1)
515 return isl_local_space_get_local(ls);
516 local = ls->div;
517 ls->div = NULL;
518 return local;
521 /* Set the local variables of "ls" to "local",
522 * where the local variables of "ls" may be missing
523 * due to a preceding call to isl_local_space_take_local.
524 * However, in this case, "ls" only has a single reference and
525 * then the call to isl_local_space_cow has no effect.
527 static __isl_give isl_local_space *isl_local_space_restore_local(
528 __isl_take isl_local_space *ls, __isl_take isl_local *local)
530 if (!ls || !local)
531 goto error;
533 if (ls->div == local) {
534 isl_local_free(local);
535 return ls;
538 ls = isl_local_space_cow(ls);
539 if (!ls)
540 goto error;
541 isl_local_free(ls->div);
542 ls->div = local;
544 return ls;
545 error:
546 isl_local_space_free(ls);
547 isl_local_free(local);
548 return NULL;
551 /* Replace the identifier of the tuple of type "type" by "id".
553 __isl_give isl_local_space *isl_local_space_set_tuple_id(
554 __isl_take isl_local_space *ls,
555 enum isl_dim_type type, __isl_take isl_id *id)
557 ls = isl_local_space_cow(ls);
558 if (!ls)
559 goto error;
560 ls->dim = isl_space_set_tuple_id(ls->dim, type, id);
561 if (!ls->dim)
562 return isl_local_space_free(ls);
563 return ls;
564 error:
565 isl_id_free(id);
566 return NULL;
569 __isl_give isl_local_space *isl_local_space_set_dim_name(
570 __isl_take isl_local_space *ls,
571 enum isl_dim_type type, unsigned pos, const char *s)
573 ls = isl_local_space_cow(ls);
574 if (!ls)
575 return NULL;
576 ls->dim = isl_space_set_dim_name(ls->dim, type, pos, s);
577 if (!ls->dim)
578 return isl_local_space_free(ls);
580 return ls;
583 __isl_give isl_local_space *isl_local_space_set_dim_id(
584 __isl_take isl_local_space *ls,
585 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
587 ls = isl_local_space_cow(ls);
588 if (!ls)
589 goto error;
590 ls->dim = isl_space_set_dim_id(ls->dim, type, pos, id);
591 if (!ls->dim)
592 return isl_local_space_free(ls);
594 return ls;
595 error:
596 isl_id_free(id);
597 return NULL;
600 /* Construct a zero-dimensional local space with the given parameter domain.
602 __isl_give isl_local_space *isl_local_space_set_from_params(
603 __isl_take isl_local_space *ls)
605 isl_space *space;
607 space = isl_local_space_take_space(ls);
608 space = isl_space_set_from_params(space);
609 ls = isl_local_space_restore_space(ls, space);
611 return ls;
614 __isl_give isl_local_space *isl_local_space_reset_space(
615 __isl_take isl_local_space *ls, __isl_take isl_space *space)
617 ls = isl_local_space_cow(ls);
618 if (!ls || !space)
619 goto error;
621 isl_space_free(ls->dim);
622 ls->dim = space;
624 return ls;
625 error:
626 isl_local_space_free(ls);
627 isl_space_free(space);
628 return NULL;
631 /* Reorder the dimensions of "ls" according to the given reordering.
632 * The reordering r is assumed to have been extended with the local
633 * variables, leaving them in the same order.
635 __isl_give isl_local_space *isl_local_space_realign(
636 __isl_take isl_local_space *ls, __isl_take isl_reordering *r)
638 isl_local *local;
640 local = isl_local_space_take_local(ls);
641 local = isl_local_reorder(local, isl_reordering_copy(r));
642 ls = isl_local_space_restore_local(ls, local);
644 ls = isl_local_space_reset_space(ls, isl_reordering_get_space(r));
646 isl_reordering_free(r);
647 return ls;
650 __isl_give isl_local_space *isl_local_space_add_div(
651 __isl_take isl_local_space *ls, __isl_take isl_vec *div)
653 ls = isl_local_space_cow(ls);
654 if (!ls || !div)
655 goto error;
657 if (ls->div->n_col != div->size)
658 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
659 "incompatible dimensions", goto error);
661 ls->div = isl_mat_add_zero_cols(ls->div, 1);
662 ls->div = isl_mat_add_rows(ls->div, 1);
663 if (!ls->div)
664 goto error;
666 isl_seq_cpy(ls->div->row[ls->div->n_row - 1], div->el, div->size);
667 isl_int_set_si(ls->div->row[ls->div->n_row - 1][div->size], 0);
669 isl_vec_free(div);
670 return ls;
671 error:
672 isl_local_space_free(ls);
673 isl_vec_free(div);
674 return NULL;
677 __isl_give isl_local_space *isl_local_space_replace_divs(
678 __isl_take isl_local_space *ls, __isl_take isl_mat *div)
680 ls = isl_local_space_cow(ls);
682 if (!ls || !div)
683 goto error;
685 isl_mat_free(ls->div);
686 ls->div = div;
687 return ls;
688 error:
689 isl_mat_free(div);
690 isl_local_space_free(ls);
691 return NULL;
694 /* Copy row "s" of "src" to row "d" of "dst", applying the expansion
695 * defined by "exp".
697 static void expand_row(__isl_keep isl_mat *dst, int d,
698 __isl_keep isl_mat *src, int s, int *exp)
700 int i;
701 unsigned c = src->n_col - src->n_row;
703 isl_seq_cpy(dst->row[d], src->row[s], c);
704 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
706 for (i = 0; i < s; ++i)
707 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
710 /* Compare (known) divs.
711 * Return non-zero if at least one of the two divs is unknown.
712 * In particular, if both divs are unknown, we respect their
713 * current order. Otherwise, we sort the known div after the unknown
714 * div only if the known div depends on the unknown div.
716 static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
717 unsigned n_row, unsigned n_col)
719 int li, lj;
720 int unknown_i, unknown_j;
722 unknown_i = isl_int_is_zero(row_i[0]);
723 unknown_j = isl_int_is_zero(row_j[0]);
725 if (unknown_i && unknown_j)
726 return i - j;
728 if (unknown_i)
729 li = n_col - n_row + i;
730 else
731 li = isl_seq_last_non_zero(row_i, n_col);
732 if (unknown_j)
733 lj = n_col - n_row + j;
734 else
735 lj = isl_seq_last_non_zero(row_j, n_col);
737 if (li != lj)
738 return li - lj;
740 return isl_seq_cmp(row_i, row_j, n_col);
743 /* Call cmp_row for divs in a matrix.
745 int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
747 return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
750 /* Call cmp_row for divs in a basic map.
752 static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
753 unsigned total)
755 return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
758 /* Sort the divs in "bmap".
760 * We first make sure divs are placed after divs on which they depend.
761 * Then we perform a simple insertion sort based on the same ordering
762 * that is used in isl_merge_divs.
764 __isl_give isl_basic_map *isl_basic_map_sort_divs(
765 __isl_take isl_basic_map *bmap)
767 int i, j;
768 isl_size total;
770 bmap = isl_basic_map_order_divs(bmap);
771 if (!bmap)
772 return NULL;
773 if (bmap->n_div <= 1)
774 return bmap;
776 total = isl_basic_map_dim(bmap, isl_dim_all);
777 if (total < 0)
778 return isl_basic_map_free(bmap);
779 for (i = 1; i < bmap->n_div; ++i) {
780 for (j = i - 1; j >= 0; --j) {
781 if (bmap_cmp_row(bmap, j, j + 1, 2 + total) <= 0)
782 break;
783 bmap = isl_basic_map_swap_div(bmap, j, j + 1);
784 if (!bmap)
785 return NULL;
789 return bmap;
792 /* Sort the divs in the basic maps of "map".
794 __isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
796 return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
799 /* Combine the two lists of divs into a single list.
800 * For each row i in div1, exp1[i] is set to the position of the corresponding
801 * row in the result. Similarly for div2 and exp2.
802 * This function guarantees
803 * exp1[i] >= i
804 * exp1[i+1] > exp1[i]
805 * For optimal merging, the two input list should have been sorted.
807 __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
808 __isl_keep isl_mat *div2, int *exp1, int *exp2)
810 int i, j, k;
811 isl_mat *div = NULL;
812 unsigned d;
814 if (!div1 || !div2)
815 return NULL;
817 d = div1->n_col - div1->n_row;
818 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
819 d + div1->n_row + div2->n_row);
820 if (!div)
821 return NULL;
823 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
824 int cmp;
826 expand_row(div, k, div1, i, exp1);
827 expand_row(div, k + 1, div2, j, exp2);
829 cmp = isl_mat_cmp_div(div, k, k + 1);
830 if (cmp == 0) {
831 exp1[i++] = k;
832 exp2[j++] = k;
833 } else if (cmp < 0) {
834 exp1[i++] = k;
835 } else {
836 exp2[j++] = k;
837 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
840 for (; i < div1->n_row; ++i, ++k) {
841 expand_row(div, k, div1, i, exp1);
842 exp1[i] = k;
844 for (; j < div2->n_row; ++j, ++k) {
845 expand_row(div, k, div2, j, exp2);
846 exp2[j] = k;
849 div->n_row = k;
850 div->n_col = d + k;
852 return div;
855 /* Swap divs "a" and "b" in "ls".
857 __isl_give isl_local_space *isl_local_space_swap_div(
858 __isl_take isl_local_space *ls, int a, int b)
860 int offset;
862 ls = isl_local_space_cow(ls);
863 if (!ls)
864 return NULL;
865 if (a < 0 || a >= ls->div->n_row || b < 0 || b >= ls->div->n_row)
866 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
867 "index out of bounds", return isl_local_space_free(ls));
868 offset = ls->div->n_col - ls->div->n_row;
869 ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
870 ls->div = isl_mat_swap_rows(ls->div, a, b);
871 if (!ls->div)
872 return isl_local_space_free(ls);
873 return ls;
876 /* Construct a local space that contains all the divs in either
877 * "ls1" or "ls2".
879 __isl_give isl_local_space *isl_local_space_intersect(
880 __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
882 isl_ctx *ctx;
883 int *exp1 = NULL;
884 int *exp2 = NULL;
885 isl_mat *div = NULL;
886 isl_bool equal;
888 if (!ls1 || !ls2)
889 goto error;
891 ctx = isl_local_space_get_ctx(ls1);
892 if (!isl_space_is_equal(ls1->dim, ls2->dim))
893 isl_die(ctx, isl_error_invalid,
894 "spaces should be identical", goto error);
896 if (ls2->div->n_row == 0) {
897 isl_local_space_free(ls2);
898 return ls1;
901 if (ls1->div->n_row == 0) {
902 isl_local_space_free(ls1);
903 return ls2;
906 exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
907 exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
908 if (!exp1 || !exp2)
909 goto error;
911 div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
912 if (!div)
913 goto error;
915 equal = isl_mat_is_equal(ls1->div, div);
916 if (equal < 0)
917 goto error;
918 if (!equal)
919 ls1 = isl_local_space_cow(ls1);
920 if (!ls1)
921 goto error;
923 free(exp1);
924 free(exp2);
925 isl_local_space_free(ls2);
926 isl_mat_free(ls1->div);
927 ls1->div = div;
929 return ls1;
930 error:
931 free(exp1);
932 free(exp2);
933 isl_mat_free(div);
934 isl_local_space_free(ls1);
935 isl_local_space_free(ls2);
936 return NULL;
939 /* Is the local variable "div" of "ls" marked as not having
940 * an explicit representation?
941 * Note that even if this variable is not marked in this way and therefore
942 * does have an explicit representation, this representation may still
943 * depend (indirectly) on other local variables that do not
944 * have an explicit representation.
946 isl_bool isl_local_space_div_is_marked_unknown(__isl_keep isl_local_space *ls,
947 int div)
949 if (!ls)
950 return isl_bool_error;
951 return isl_local_div_is_marked_unknown(ls->div, div);
954 /* Does "ls" have a complete explicit representation for div "div"?
956 isl_bool isl_local_space_div_is_known(__isl_keep isl_local_space *ls, int div)
958 if (!ls)
959 return isl_bool_error;
960 return isl_local_div_is_known(ls->div, div);
963 /* Does "ls" have an explicit representation for all local variables?
965 isl_bool isl_local_space_divs_known(__isl_keep isl_local_space *ls)
967 if (!ls)
968 return isl_bool_error;
969 return isl_local_divs_known(ls->div);
972 __isl_give isl_local_space *isl_local_space_domain(
973 __isl_take isl_local_space *ls)
975 isl_size n_out;
977 n_out = isl_local_space_dim(ls, isl_dim_out);
978 if (n_out < 0)
979 return isl_local_space_free(ls);
980 ls = isl_local_space_drop_dims(ls, isl_dim_out, 0, n_out);
981 ls = isl_local_space_cow(ls);
982 if (!ls)
983 return NULL;
984 ls->dim = isl_space_domain(ls->dim);
985 if (!ls->dim)
986 return isl_local_space_free(ls);
987 return ls;
990 __isl_give isl_local_space *isl_local_space_range(
991 __isl_take isl_local_space *ls)
993 isl_size n_in;
995 n_in = isl_local_space_dim(ls, isl_dim_in);
996 if (n_in < 0)
997 return isl_local_space_free(ls);
998 ls = isl_local_space_drop_dims(ls, isl_dim_in, 0, n_in);
999 ls = isl_local_space_cow(ls);
1000 if (!ls)
1001 return NULL;
1003 ls->dim = isl_space_range(ls->dim);
1004 if (!ls->dim)
1005 return isl_local_space_free(ls);
1006 return ls;
1009 /* Construct a local space for a map that has the given local
1010 * space as domain and that has a zero-dimensional range.
1012 __isl_give isl_local_space *isl_local_space_from_domain(
1013 __isl_take isl_local_space *ls)
1015 ls = isl_local_space_cow(ls);
1016 if (!ls)
1017 return NULL;
1018 ls->dim = isl_space_from_domain(ls->dim);
1019 if (!ls->dim)
1020 return isl_local_space_free(ls);
1021 return ls;
1024 __isl_give isl_local_space *isl_local_space_add_dims(
1025 __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
1027 isl_size pos;
1029 pos = isl_local_space_dim(ls, type);
1030 if (pos < 0)
1031 return isl_local_space_free(ls);
1032 return isl_local_space_insert_dims(ls, type, pos, n);
1035 /* Lift the basic set "bset", living in the space of "ls"
1036 * to live in a space with extra coordinates corresponding
1037 * to the local variables of "ls".
1039 __isl_give isl_basic_set *isl_local_space_lift_basic_set(
1040 __isl_take isl_local_space *ls, __isl_take isl_basic_set *bset)
1042 isl_size n_local;
1043 isl_space *space;
1044 isl_basic_set *ls_bset;
1046 n_local = isl_local_space_dim(ls, isl_dim_div);
1047 space = isl_basic_set_peek_space(bset);
1048 if (n_local < 0 ||
1049 isl_local_space_check_has_space(ls, space) < 0)
1050 goto error;
1052 if (n_local == 0) {
1053 isl_local_space_free(ls);
1054 return bset;
1057 bset = isl_basic_set_add_dims(bset, isl_dim_set, n_local);
1058 ls_bset = isl_basic_set_from_local_space(ls);
1059 ls_bset = isl_basic_set_lift(ls_bset);
1060 ls_bset = isl_basic_set_flatten(ls_bset);
1061 bset = isl_basic_set_intersect(bset, ls_bset);
1063 return bset;
1064 error:
1065 isl_local_space_free(ls);
1066 isl_basic_set_free(bset);
1067 return NULL;
1070 /* Lift the set "set", living in the space of "ls"
1071 * to live in a space with extra coordinates corresponding
1072 * to the local variables of "ls".
1074 __isl_give isl_set *isl_local_space_lift_set(__isl_take isl_local_space *ls,
1075 __isl_take isl_set *set)
1077 isl_size n_local;
1078 isl_basic_set *bset;
1080 n_local = isl_local_space_dim(ls, isl_dim_div);
1081 if (n_local < 0 ||
1082 isl_local_space_check_has_space(ls, isl_set_peek_space(set)) < 0)
1083 goto error;
1085 if (n_local == 0) {
1086 isl_local_space_free(ls);
1087 return set;
1090 set = isl_set_add_dims(set, isl_dim_set, n_local);
1091 bset = isl_basic_set_from_local_space(ls);
1092 bset = isl_basic_set_lift(bset);
1093 bset = isl_basic_set_flatten(bset);
1094 set = isl_set_intersect(set, isl_set_from_basic_set(bset));
1096 return set;
1097 error:
1098 isl_local_space_free(ls);
1099 isl_set_free(set);
1100 return NULL;
1103 /* Remove common factor of non-constant terms and denominator.
1105 static __isl_give isl_local_space *normalize_div(
1106 __isl_take isl_local_space *ls, int div)
1108 isl_ctx *ctx = ls->div->ctx;
1109 unsigned total = ls->div->n_col - 2;
1111 isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
1112 isl_int_gcd(ctx->normalize_gcd,
1113 ctx->normalize_gcd, ls->div->row[div][0]);
1114 if (isl_int_is_one(ctx->normalize_gcd))
1115 return ls;
1117 isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
1118 ctx->normalize_gcd, total);
1119 isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
1120 ctx->normalize_gcd);
1121 isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
1122 ctx->normalize_gcd);
1124 return ls;
1127 /* Exploit the equalities in "eq" to simplify the expressions of
1128 * the integer divisions in "ls".
1129 * The integer divisions in "ls" are assumed to appear as regular
1130 * dimensions in "eq".
1132 __isl_give isl_local_space *isl_local_space_substitute_equalities(
1133 __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
1135 int i, j, k;
1136 isl_size total, dim;
1137 unsigned n_div;
1139 if (!ls || !eq)
1140 goto error;
1142 total = isl_space_dim(eq->dim, isl_dim_all);
1143 dim = isl_local_space_dim(ls, isl_dim_all);
1144 if (dim < 0 || total < 0)
1145 goto error;
1146 if (dim != total)
1147 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1148 "spaces don't match", goto error);
1149 total++;
1150 n_div = eq->n_div;
1151 for (i = 0; i < eq->n_eq; ++i) {
1152 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1153 if (j < 0 || j == 0 || j >= total)
1154 continue;
1156 for (k = 0; k < ls->div->n_row; ++k) {
1157 if (isl_int_is_zero(ls->div->row[k][1 + j]))
1158 continue;
1159 ls = isl_local_space_cow(ls);
1160 if (!ls)
1161 goto error;
1162 ls->div = isl_mat_cow(ls->div);
1163 if (!ls->div)
1164 goto error;
1165 isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
1166 &ls->div->row[k][0]);
1167 ls = normalize_div(ls, k);
1168 if (!ls)
1169 goto error;
1173 isl_basic_set_free(eq);
1174 return ls;
1175 error:
1176 isl_basic_set_free(eq);
1177 isl_local_space_free(ls);
1178 return NULL;
1181 /* Plug in the affine expressions "subs" of length "subs_len" (including
1182 * the denominator and the constant term) into the variable at position "pos"
1183 * of the "n" div expressions starting at "first".
1185 * Let i be the dimension to replace and let "subs" be of the form
1187 * f/d
1189 * Any integer division starting at "first" with a non-zero coefficient for i,
1191 * floor((a i + g)/m)
1193 * is replaced by
1195 * floor((a f + d g)/(m d))
1197 __isl_give isl_local_space *isl_local_space_substitute_seq(
1198 __isl_take isl_local_space *ls,
1199 enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
1200 int first, int n)
1202 int i;
1203 isl_int v;
1205 if (n == 0)
1206 return ls;
1207 ls = isl_local_space_cow(ls);
1208 if (!ls)
1209 return NULL;
1210 ls->div = isl_mat_cow(ls->div);
1211 if (!ls->div)
1212 return isl_local_space_free(ls);
1214 if (first + n > ls->div->n_row)
1215 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1216 "index out of bounds", return isl_local_space_free(ls));
1218 pos += isl_local_space_offset(ls, type);
1220 isl_int_init(v);
1221 for (i = first; i < first + n; ++i) {
1222 if (isl_int_is_zero(ls->div->row[i][1 + pos]))
1223 continue;
1224 isl_seq_substitute(ls->div->row[i], pos, subs,
1225 ls->div->n_col, subs_len, v);
1226 ls = normalize_div(ls, i);
1227 if (!ls)
1228 break;
1230 isl_int_clear(v);
1232 return ls;
1235 /* Plug in "subs" for dimension "type", "pos" in the integer divisions
1236 * of "ls".
1238 * Let i be the dimension to replace and let "subs" be of the form
1240 * f/d
1242 * Any integer division with a non-zero coefficient for i,
1244 * floor((a i + g)/m)
1246 * is replaced by
1248 * floor((a f + d g)/(m d))
1250 __isl_give isl_local_space *isl_local_space_substitute(
1251 __isl_take isl_local_space *ls,
1252 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
1254 isl_size n_div;
1256 ls = isl_local_space_cow(ls);
1257 if (!ls || !subs)
1258 return isl_local_space_free(ls);
1260 if (!isl_space_is_equal(ls->dim, subs->ls->dim))
1261 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1262 "spaces don't match", return isl_local_space_free(ls));
1263 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
1264 if (n_div < 0)
1265 return isl_local_space_free(ls);
1266 if (n_div != 0)
1267 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1268 "cannot handle divs yet",
1269 return isl_local_space_free(ls));
1271 return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
1272 subs->v->size, 0, ls->div->n_row);
1275 isl_bool isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
1276 enum isl_dim_type type)
1278 if (!ls)
1279 return isl_bool_error;
1280 return isl_space_is_named_or_nested(ls->dim, type);
1283 __isl_give isl_local_space *isl_local_space_drop_dims(
1284 __isl_take isl_local_space *ls,
1285 enum isl_dim_type type, unsigned first, unsigned n)
1287 if (!ls)
1288 return NULL;
1289 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1290 return ls;
1292 if (isl_local_space_check_range(ls, type, first, n) < 0)
1293 return isl_local_space_free(ls);
1295 ls = isl_local_space_cow(ls);
1296 if (!ls)
1297 return NULL;
1299 if (type == isl_dim_div) {
1300 ls->div = isl_mat_drop_rows(ls->div, first, n);
1301 } else {
1302 ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
1303 if (!ls->dim)
1304 return isl_local_space_free(ls);
1307 first += 1 + isl_local_space_offset(ls, type);
1308 ls->div = isl_mat_drop_cols(ls->div, first, n);
1309 if (!ls->div)
1310 return isl_local_space_free(ls);
1312 return ls;
1315 __isl_give isl_local_space *isl_local_space_insert_dims(
1316 __isl_take isl_local_space *ls,
1317 enum isl_dim_type type, unsigned first, unsigned n)
1319 if (!ls)
1320 return NULL;
1321 if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
1322 return ls;
1324 if (isl_local_space_check_range(ls, type, first, 0) < 0)
1325 return isl_local_space_free(ls);
1327 ls = isl_local_space_cow(ls);
1328 if (!ls)
1329 return NULL;
1331 if (type == isl_dim_div) {
1332 ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
1333 } else {
1334 ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
1335 if (!ls->dim)
1336 return isl_local_space_free(ls);
1339 first += 1 + isl_local_space_offset(ls, type);
1340 ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
1341 if (!ls->div)
1342 return isl_local_space_free(ls);
1344 return ls;
1347 /* Does the linear part of "constraint" correspond to
1348 * integer division "div" in "ls"?
1350 * That is, given div = floor((c + f)/m), is the constraint of the form
1352 * f - m d + c' >= 0 [sign = 1]
1353 * or
1354 * -f + m d + c'' >= 0 [sign = -1]
1356 * If so, set *sign to the corresponding value.
1358 static isl_bool is_linear_div_constraint(__isl_keep isl_local_space *ls,
1359 isl_int *constraint, unsigned div, int *sign)
1361 isl_bool unknown;
1362 unsigned pos;
1364 unknown = isl_local_space_div_is_marked_unknown(ls, div);
1365 if (unknown < 0)
1366 return isl_bool_error;
1367 if (unknown)
1368 return isl_bool_false;
1370 pos = isl_local_space_offset(ls, isl_dim_div) + div;
1372 if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
1373 *sign = -1;
1374 if (!isl_seq_is_neg(constraint + 1,
1375 ls->div->row[div] + 2, pos - 1))
1376 return isl_bool_false;
1377 } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
1378 *sign = 1;
1379 if (!isl_seq_eq(constraint + 1, ls->div->row[div] + 2, pos - 1))
1380 return isl_bool_false;
1381 } else {
1382 return isl_bool_false;
1384 if (isl_seq_first_non_zero(constraint + pos + 1,
1385 ls->div->n_row - div - 1) != -1)
1386 return isl_bool_false;
1387 return isl_bool_true;
1390 /* Check if the constraints pointed to by "constraint" is a div
1391 * constraint corresponding to div "div" in "ls".
1393 * That is, if div = floor(f/m), then check if the constraint is
1395 * f - m d >= 0
1396 * or
1397 * -(f-(m-1)) + m d >= 0
1399 * First check if the linear part is of the right form and
1400 * then check the constant term.
1402 isl_bool isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
1403 isl_int *constraint, unsigned div)
1405 int sign;
1406 isl_bool linear;
1408 linear = is_linear_div_constraint(ls, constraint, div, &sign);
1409 if (linear < 0 || !linear)
1410 return linear;
1412 if (sign < 0) {
1413 int neg;
1414 isl_int_sub(ls->div->row[div][1],
1415 ls->div->row[div][1], ls->div->row[div][0]);
1416 isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1417 neg = isl_seq_is_neg(constraint, ls->div->row[div] + 1, 1);
1418 isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
1419 isl_int_add(ls->div->row[div][1],
1420 ls->div->row[div][1], ls->div->row[div][0]);
1421 if (!neg)
1422 return isl_bool_false;
1423 } else {
1424 if (!isl_int_eq(constraint[0], ls->div->row[div][1]))
1425 return isl_bool_false;
1428 return isl_bool_true;
1431 /* Is the constraint pointed to by "constraint" one
1432 * of an equality that corresponds to integer division "div" in "ls"?
1434 * That is, given an integer division of the form
1436 * a = floor((f + c)/m)
1438 * is the equality of the form
1440 * -f + m d + c' = 0
1442 * Note that the constant term is not checked explicitly, but given
1443 * that this is a valid equality constraint, the constant c' necessarily
1444 * has a value close to -c.
1446 isl_bool isl_local_space_is_div_equality(__isl_keep isl_local_space *ls,
1447 isl_int *constraint, unsigned div)
1449 int sign;
1450 isl_bool linear;
1452 linear = is_linear_div_constraint(ls, constraint, div, &sign);
1453 if (linear < 0 || !linear)
1454 return linear;
1456 return isl_bool_ok(sign < 0);
1460 * Set active[i] to 1 if the dimension at position i is involved
1461 * in the linear expression l.
1463 int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
1465 int i, j;
1466 isl_ctx *ctx;
1467 int *active = NULL;
1468 isl_size total;
1469 unsigned offset;
1471 ctx = isl_local_space_get_ctx(ls);
1472 total = isl_local_space_dim(ls, isl_dim_all);
1473 if (total < 0)
1474 return NULL;
1475 active = isl_calloc_array(ctx, int, total);
1476 if (total && !active)
1477 return NULL;
1479 for (i = 0; i < total; ++i)
1480 active[i] = !isl_int_is_zero(l[i]);
1482 offset = isl_local_space_offset(ls, isl_dim_div) - 1;
1483 for (i = ls->div->n_row - 1; i >= 0; --i) {
1484 if (!active[offset + i])
1485 continue;
1486 for (j = 0; j < total; ++j)
1487 active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
1490 return active;
1493 /* Given a local space "ls" of a set, create a local space
1494 * for the lift of the set. In particular, the result
1495 * is of the form [dim -> local[..]], with ls->div->n_row variables in the
1496 * range of the wrapped map.
1498 __isl_give isl_local_space *isl_local_space_lift(
1499 __isl_take isl_local_space *ls)
1501 ls = isl_local_space_cow(ls);
1502 if (!ls)
1503 return NULL;
1505 ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
1506 ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
1507 if (!ls->dim || !ls->div)
1508 return isl_local_space_free(ls);
1510 return ls;
1513 /* Construct a basic map that maps a set living in local space "ls"
1514 * to the corresponding lifted local space.
1516 __isl_give isl_basic_map *isl_local_space_lifting(
1517 __isl_take isl_local_space *ls)
1519 isl_basic_map *lifting;
1520 isl_basic_set *bset;
1522 if (!ls)
1523 return NULL;
1524 if (!isl_local_space_is_set(ls))
1525 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1526 "lifting only defined on set spaces", goto error);
1528 bset = isl_basic_set_from_local_space(ls);
1529 lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
1530 lifting = isl_basic_map_domain_map(lifting);
1531 lifting = isl_basic_map_reverse(lifting);
1533 return lifting;
1534 error:
1535 isl_local_space_free(ls);
1536 return NULL;
1539 /* Compute the preimage of "ls" under the function represented by "ma".
1540 * In other words, plug in "ma" in "ls". The result is a local space
1541 * that is part of the domain space of "ma".
1543 * If the divs in "ls" are represented as
1545 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
1547 * and ma is represented by
1549 * x = D(p) + F(y) + G(divs')
1551 * then the resulting divs are
1553 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
1555 * We first copy over the divs from "ma" and then
1556 * we add the modified divs from "ls".
1558 __isl_give isl_local_space *isl_local_space_preimage_multi_aff(
1559 __isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
1561 int i;
1562 isl_space *space;
1563 isl_local_space *res = NULL;
1564 isl_size n_div_ls, n_div_ma;
1565 isl_int f, c1, c2, g;
1567 ma = isl_multi_aff_align_divs(ma);
1568 if (!ls || !ma)
1569 goto error;
1570 if (!isl_space_is_range_internal(ls->dim, ma->space))
1571 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1572 "spaces don't match", goto error);
1574 n_div_ls = isl_local_space_dim(ls, isl_dim_div);
1575 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
1576 if (n_div_ls < 0 || n_div_ma < 0)
1577 goto error;
1579 space = isl_space_domain(isl_multi_aff_get_space(ma));
1580 res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
1581 if (!res)
1582 goto error;
1584 if (n_div_ma) {
1585 isl_mat_free(res->div);
1586 res->div = isl_mat_copy(ma->u.p[0]->ls->div);
1587 res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
1588 res->div = isl_mat_add_rows(res->div, n_div_ls);
1589 if (!res->div)
1590 goto error;
1593 isl_int_init(f);
1594 isl_int_init(c1);
1595 isl_int_init(c2);
1596 isl_int_init(g);
1598 for (i = 0; i < ls->div->n_row; ++i) {
1599 if (isl_int_is_zero(ls->div->row[i][0])) {
1600 isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
1601 continue;
1603 if (isl_seq_preimage(res->div->row[n_div_ma + i],
1604 ls->div->row[i],
1605 ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1) < 0)
1606 res = isl_local_space_free(res);
1607 res = normalize_div(res, n_div_ma + i);
1608 if (!res)
1609 break;
1612 isl_int_clear(f);
1613 isl_int_clear(c1);
1614 isl_int_clear(c2);
1615 isl_int_clear(g);
1617 isl_local_space_free(ls);
1618 isl_multi_aff_free(ma);
1619 return res;
1620 error:
1621 isl_local_space_free(ls);
1622 isl_multi_aff_free(ma);
1623 isl_local_space_free(res);
1624 return NULL;
1627 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "ls"
1628 * to dimensions of "dst_type" at "dst_pos".
1630 * Moving to/from local dimensions is not allowed.
1631 * We currently assume that the dimension type changes.
1633 __isl_give isl_local_space *isl_local_space_move_dims(
1634 __isl_take isl_local_space *ls,
1635 enum isl_dim_type dst_type, unsigned dst_pos,
1636 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1638 isl_space *space;
1639 isl_local *local;
1640 isl_size v_src, v_dst;
1641 unsigned g_dst_pos;
1642 unsigned g_src_pos;
1644 if (!ls)
1645 return NULL;
1646 if (n == 0 &&
1647 !isl_local_space_is_named_or_nested(ls, src_type) &&
1648 !isl_local_space_is_named_or_nested(ls, dst_type))
1649 return ls;
1651 if (isl_local_space_check_range(ls, src_type, src_pos, n) < 0)
1652 return isl_local_space_free(ls);
1653 if (isl_local_space_check_range(ls, dst_type, dst_pos, 0) < 0)
1654 return isl_local_space_free(ls);
1655 if (src_type == isl_dim_div)
1656 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1657 "cannot move divs", return isl_local_space_free(ls));
1658 if (dst_type == isl_dim_div)
1659 isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
1660 "cannot move to divs", return isl_local_space_free(ls));
1661 if (dst_type == src_type && dst_pos == src_pos)
1662 return ls;
1663 if (dst_type == src_type)
1664 isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
1665 "moving dims within the same type not supported",
1666 return isl_local_space_free(ls));
1668 v_src = isl_local_space_var_offset(ls, src_type);
1669 v_dst = isl_local_space_var_offset(ls, dst_type);
1670 if (v_src < 0 || v_dst < 0)
1671 return isl_local_space_free(ls);
1672 g_src_pos = v_src + src_pos;
1673 g_dst_pos = v_dst + dst_pos;
1674 if (dst_type > src_type)
1675 g_dst_pos -= n;
1677 local = isl_local_space_take_local(ls);
1678 local = isl_local_move_vars(local, g_dst_pos, g_src_pos, n);
1679 ls = isl_local_space_restore_local(ls, local);
1681 space = isl_local_space_take_space(ls);
1682 space = isl_space_move_dims(space, dst_type, dst_pos,
1683 src_type, src_pos, n);
1684 ls = isl_local_space_restore_space(ls, space);
1686 return ls;
1689 /* Given a local space (A -> B), return the corresponding local space
1690 * (B -> A).
1692 __isl_give isl_local_space *isl_local_space_wrapped_reverse(
1693 __isl_take isl_local_space *ls)
1695 isl_space *space;
1696 isl_local *local;
1697 isl_size n_in, n_out;
1698 unsigned offset;
1700 space = isl_local_space_peek_space(ls);
1701 offset = isl_space_offset(space, isl_dim_set);
1702 n_in = isl_space_wrapped_dim(space, isl_dim_set, isl_dim_in);
1703 n_out = isl_space_wrapped_dim(space, isl_dim_set, isl_dim_out);
1704 if (n_in < 0 || n_out < 0)
1705 return isl_local_space_free(ls);
1707 space = isl_local_space_take_space(ls);
1708 space = isl_space_wrapped_reverse(space);
1709 ls = isl_local_space_restore_space(ls, space);
1711 local = isl_local_space_take_local(ls);
1712 local = isl_local_move_vars(local, offset, offset + n_in, n_out);
1713 ls = isl_local_space_restore_local(ls, local);
1715 return ls;
1718 /* Remove any internal structure of the domain of "ls".
1719 * If there is any such internal structure in the input,
1720 * then the name of the corresponding space is also removed.
1722 __isl_give isl_local_space *isl_local_space_flatten_domain(
1723 __isl_take isl_local_space *ls)
1725 if (!ls)
1726 return NULL;
1728 if (!ls->dim->nested[0])
1729 return ls;
1731 ls = isl_local_space_cow(ls);
1732 if (!ls)
1733 return NULL;
1735 ls->dim = isl_space_flatten_domain(ls->dim);
1736 if (!ls->dim)
1737 return isl_local_space_free(ls);
1739 return ls;
1742 /* Remove any internal structure of the range of "ls".
1743 * If there is any such internal structure in the input,
1744 * then the name of the corresponding space is also removed.
1746 __isl_give isl_local_space *isl_local_space_flatten_range(
1747 __isl_take isl_local_space *ls)
1749 if (!ls)
1750 return NULL;
1752 if (!ls->dim->nested[1])
1753 return ls;
1755 ls = isl_local_space_cow(ls);
1756 if (!ls)
1757 return NULL;
1759 ls->dim = isl_space_flatten_range(ls->dim);
1760 if (!ls->dim)
1761 return isl_local_space_free(ls);
1763 return ls;
1766 /* Given the local space "ls" of a map, return the local space of a set
1767 * that lives in a space that wraps the space of "ls" and that has
1768 * the same divs.
1770 __isl_give isl_local_space *isl_local_space_wrap(__isl_take isl_local_space *ls)
1772 ls = isl_local_space_cow(ls);
1773 if (!ls)
1774 return NULL;
1776 ls->dim = isl_space_wrap(ls->dim);
1777 if (!ls->dim)
1778 return isl_local_space_free(ls);
1780 return ls;
1783 /* Lift the point "pnt", living in the (set) space of "ls"
1784 * to live in a space with extra coordinates corresponding
1785 * to the local variables of "ls".
1787 __isl_give isl_point *isl_local_space_lift_point(__isl_take isl_local_space *ls,
1788 __isl_take isl_point *pnt)
1790 isl_size n_local;
1791 isl_space *space;
1792 isl_local *local;
1793 isl_vec *vec;
1795 if (isl_local_space_check_has_space(ls, isl_point_peek_space(pnt)) < 0)
1796 goto error;
1798 local = isl_local_space_peek_local(ls);
1799 n_local = isl_local_space_dim(ls, isl_dim_div);
1800 if (n_local < 0)
1801 goto error;
1803 space = isl_point_take_space(pnt);
1804 vec = isl_point_take_vec(pnt);
1806 space = isl_space_lift(space, n_local);
1807 vec = isl_local_extend_point_vec(local, vec);
1809 pnt = isl_point_restore_vec(pnt, vec);
1810 pnt = isl_point_restore_space(pnt, space);
1812 isl_local_space_free(ls);
1814 return pnt;
1815 error:
1816 isl_local_space_free(ls);
1817 isl_point_free(pnt);
1818 return NULL;