2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
33 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
36 case isl_dim_param: return dim->nparam;
37 case isl_dim_in: return dim->n_in;
38 case isl_dim_out: return dim->n_out;
39 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
44 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
47 case isl_dim_param: return 1;
48 case isl_dim_in: return 1 + dim->nparam;
49 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
54 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
55 enum isl_dim_type type)
60 case isl_dim_cst: return 1;
63 case isl_dim_out: return isl_dim_size(bmap->dim, type);
64 case isl_dim_div: return bmap->n_div;
65 case isl_dim_all: return isl_basic_map_total_dim(bmap);
70 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
72 return map ? n(map->dim, type) : 0;
75 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
77 return set ? n(set->dim, type) : 0;
80 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
81 enum isl_dim_type type)
83 struct isl_dim *dim = bmap->dim;
85 case isl_dim_cst: return 0;
86 case isl_dim_param: return 1;
87 case isl_dim_in: return 1 + dim->nparam;
88 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
89 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
94 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
95 enum isl_dim_type type)
97 return isl_basic_map_offset(bset, type);
100 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
102 return pos(map->dim, type);
105 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
106 enum isl_dim_type type)
108 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
111 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
113 return isl_basic_set_dim(bset, isl_dim_set);
116 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
118 return isl_basic_set_dim(bset, isl_dim_param);
121 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
123 return isl_dim_total(bset->dim) + bset->n_div;
126 unsigned isl_set_n_dim(const struct isl_set *set)
128 return isl_set_dim(set, isl_dim_set);
131 unsigned isl_set_n_param(const struct isl_set *set)
133 return isl_set_dim(set, isl_dim_param);
136 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
138 return bmap ? bmap->dim->n_in : 0;
141 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
143 return bmap ? bmap->dim->n_out : 0;
146 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
148 return bmap ? bmap->dim->nparam : 0;
151 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
153 return bmap ? bmap->n_div : 0;
156 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
158 return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
161 unsigned isl_map_n_in(const struct isl_map *map)
163 return map->dim->n_in;
166 unsigned isl_map_n_out(const struct isl_map *map)
168 return map->dim->n_out;
171 unsigned isl_map_n_param(const struct isl_map *map)
173 return map->dim->nparam;
176 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
181 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
184 return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
187 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
188 struct isl_basic_set *bset)
193 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
196 return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
199 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
204 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
207 return isl_dim_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
210 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
211 struct isl_basic_set *bset)
216 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
219 return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
222 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
224 return bmap ? bmap->ctx : NULL;
227 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
229 return bset ? bset->ctx : NULL;
232 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
234 return map ? map->ctx : NULL;
237 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
239 return set ? set->ctx : NULL;
242 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
246 return isl_dim_copy(bmap->dim);
249 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
253 return isl_dim_copy(bset->dim);
256 __isl_give isl_local_space *isl_basic_map_get_local_space(
257 __isl_keep isl_basic_map *bmap)
266 total = isl_basic_map_total_dim(bmap);
267 ls = isl_local_space_alloc(isl_dim_copy(bmap->dim), bmap->n_div);
271 for (i = 0; i < bmap->n_div; ++i)
272 isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total);
277 __isl_give isl_local_space *isl_basic_set_get_local_space(
278 __isl_keep isl_basic_set *bset)
280 return isl_basic_map_get_local_space(bset);
283 __isl_give isl_basic_map *isl_basic_map_from_local_space(
284 __isl_take isl_local_space *ls)
293 n_div = isl_local_space_dim(ls, isl_dim_div);
294 bmap = isl_basic_map_alloc_dim(isl_local_space_get_dim(ls),
295 n_div, 0, 2 * n_div);
297 for (i = 0; i < n_div; ++i)
298 if (isl_basic_map_alloc_div(bmap) < 0)
301 for (i = 0; i < n_div; ++i) {
302 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
303 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
307 isl_local_space_free(ls);
310 isl_local_space_free(ls);
311 isl_basic_map_free(bmap);
315 __isl_give isl_basic_set *isl_basic_set_from_local_space(
316 __isl_take isl_local_space *ls)
318 return isl_basic_map_from_local_space(ls);
321 struct isl_dim *isl_map_get_dim(struct isl_map *map)
325 return isl_dim_copy(map->dim);
328 struct isl_dim *isl_set_get_dim(struct isl_set *set)
332 return isl_dim_copy(set->dim);
335 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
336 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
338 bmap = isl_basic_map_cow(bmap);
341 bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
344 bmap = isl_basic_map_finalize(bmap);
347 isl_basic_map_free(bmap);
351 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
352 enum isl_dim_type type)
354 return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
357 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
358 enum isl_dim_type type, const char *s)
362 map = isl_map_cow(map);
366 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
370 for (i = 0; i < map->n; ++i) {
371 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
382 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
383 enum isl_dim_type type)
385 return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
388 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
391 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
394 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
396 return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
399 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
401 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
404 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
405 enum isl_dim_type type, unsigned pos)
407 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
410 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
411 enum isl_dim_type type, unsigned pos)
413 return bset ? isl_dim_get_name(bset->dim, type, pos) : NULL;
416 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
417 enum isl_dim_type type, unsigned pos)
419 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
422 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
423 enum isl_dim_type type, unsigned pos)
425 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
428 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
429 __isl_take isl_basic_map *bmap,
430 enum isl_dim_type type, unsigned pos, const char *s)
434 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
439 isl_basic_map_free(bmap);
443 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
444 enum isl_dim_type type, unsigned pos, const char *s)
451 map->dim = isl_dim_set_name(map->dim, type, pos, s);
455 for (i = 0; i < map->n; ++i) {
456 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
467 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
468 __isl_take isl_basic_set *bset,
469 enum isl_dim_type type, unsigned pos, const char *s)
471 return (isl_basic_set *)isl_basic_map_set_dim_name(
472 (isl_basic_map *)bset, type, pos, s);
475 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
476 enum isl_dim_type type, unsigned pos, const char *s)
478 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
481 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
485 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
488 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
490 return isl_basic_map_is_rational(bset);
493 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
494 struct isl_basic_map *bmap, unsigned extra,
495 unsigned n_eq, unsigned n_ineq)
498 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
503 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
504 if (isl_blk_is_error(bmap->block))
507 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
512 bmap->block2 = isl_blk_empty();
515 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
516 if (isl_blk_is_error(bmap->block2))
519 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
524 for (i = 0; i < n_ineq + n_eq; ++i)
525 bmap->ineq[i] = bmap->block.data + i * row_size;
527 for (i = 0; i < extra; ++i)
528 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
532 bmap->c_size = n_eq + n_ineq;
533 bmap->eq = bmap->ineq + n_ineq;
542 isl_basic_map_free(bmap);
546 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
547 unsigned nparam, unsigned dim, unsigned extra,
548 unsigned n_eq, unsigned n_ineq)
550 struct isl_basic_map *bmap;
551 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
552 return (struct isl_basic_set *)bmap;
555 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
556 unsigned extra, unsigned n_eq, unsigned n_ineq)
558 struct isl_basic_map *bmap;
561 isl_assert(dim->ctx, dim->n_in == 0, goto error);
562 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
563 return (struct isl_basic_set *)bmap;
569 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
570 unsigned extra, unsigned n_eq, unsigned n_ineq)
572 struct isl_basic_map *bmap;
576 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
581 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
587 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
588 unsigned nparam, unsigned in, unsigned out, unsigned extra,
589 unsigned n_eq, unsigned n_ineq)
591 struct isl_basic_map *bmap;
594 dim = isl_dim_alloc(ctx, nparam, in, out);
598 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
602 static void dup_constraints(
603 struct isl_basic_map *dst, struct isl_basic_map *src)
606 unsigned total = isl_basic_map_total_dim(src);
608 for (i = 0; i < src->n_eq; ++i) {
609 int j = isl_basic_map_alloc_equality(dst);
610 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
613 for (i = 0; i < src->n_ineq; ++i) {
614 int j = isl_basic_map_alloc_inequality(dst);
615 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
618 for (i = 0; i < src->n_div; ++i) {
619 int j = isl_basic_map_alloc_div(dst);
620 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
622 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
625 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
627 struct isl_basic_map *dup;
631 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
632 bmap->n_div, bmap->n_eq, bmap->n_ineq);
635 dup_constraints(dup, bmap);
636 dup->flags = bmap->flags;
637 dup->sample = isl_vec_copy(bmap->sample);
641 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
643 struct isl_basic_map *dup;
645 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
646 return (struct isl_basic_set *)dup;
649 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
654 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
658 return isl_basic_set_dup(bset);
661 struct isl_set *isl_set_copy(struct isl_set *set)
670 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
675 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
679 bmap = isl_basic_map_dup(bmap);
681 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
685 struct isl_map *isl_map_copy(struct isl_map *map)
694 void isl_basic_map_free(struct isl_basic_map *bmap)
702 isl_ctx_deref(bmap->ctx);
704 isl_blk_free(bmap->ctx, bmap->block2);
706 isl_blk_free(bmap->ctx, bmap->block);
707 isl_vec_free(bmap->sample);
708 isl_dim_free(bmap->dim);
712 void isl_basic_set_free(struct isl_basic_set *bset)
714 isl_basic_map_free((struct isl_basic_map *)bset);
717 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
719 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
722 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
728 isl_assert(ctx, room_for_con(bmap, 1), return -1);
729 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
731 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
732 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
733 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
734 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
735 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
736 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
738 int j = isl_basic_map_alloc_inequality(bmap);
742 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
743 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
750 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
751 bmap->extra - bmap->n_div);
755 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
757 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
760 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
764 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
769 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
771 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
774 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
779 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
781 if (pos != bmap->n_eq - 1) {
783 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
784 bmap->eq[bmap->n_eq - 1] = t;
790 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
792 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
795 void isl_basic_map_inequality_to_equality(
796 struct isl_basic_map *bmap, unsigned pos)
801 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
802 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
807 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
808 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
809 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
810 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
813 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
815 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
818 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
824 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
825 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
826 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
827 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
828 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
829 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
830 1 + isl_basic_map_total_dim(bmap),
831 bmap->extra - bmap->n_div);
832 return bmap->n_ineq++;
835 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
837 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
840 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
844 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
849 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
851 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
854 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
859 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
861 if (pos != bmap->n_ineq - 1) {
863 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
864 bmap->ineq[bmap->n_ineq - 1] = t;
865 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
871 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
873 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
876 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
881 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
884 k = isl_basic_map_alloc_equality(bmap);
887 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
890 isl_basic_map_free(bmap);
894 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
897 return (isl_basic_set *)
898 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
901 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
906 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
909 k = isl_basic_map_alloc_inequality(bmap);
912 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
915 isl_basic_map_free(bmap);
919 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
922 return (isl_basic_set *)
923 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
926 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
930 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
931 isl_seq_clr(bmap->div[bmap->n_div] +
932 1 + 1 + isl_basic_map_total_dim(bmap),
933 bmap->extra - bmap->n_div);
934 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
935 return bmap->n_div++;
938 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
940 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
943 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
947 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
952 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
954 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
957 /* Copy constraint from src to dst, putting the vars of src at offset
958 * dim_off in dst and the divs of src at offset div_off in dst.
959 * If both sets are actually map, then dim_off applies to the input
962 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
963 struct isl_basic_map *src_map, isl_int *src,
964 unsigned in_off, unsigned out_off, unsigned div_off)
966 unsigned src_nparam = isl_basic_map_n_param(src_map);
967 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
968 unsigned src_in = isl_basic_map_n_in(src_map);
969 unsigned dst_in = isl_basic_map_n_in(dst_map);
970 unsigned src_out = isl_basic_map_n_out(src_map);
971 unsigned dst_out = isl_basic_map_n_out(dst_map);
972 isl_int_set(dst[0], src[0]);
973 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
974 if (dst_nparam > src_nparam)
975 isl_seq_clr(dst+1+src_nparam,
976 dst_nparam - src_nparam);
977 isl_seq_clr(dst+1+dst_nparam, in_off);
978 isl_seq_cpy(dst+1+dst_nparam+in_off,
980 isl_min(dst_in-in_off, src_in));
981 if (dst_in-in_off > src_in)
982 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
983 dst_in - in_off - src_in);
984 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
985 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
986 src+1+src_nparam+src_in,
987 isl_min(dst_out-out_off, src_out));
988 if (dst_out-out_off > src_out)
989 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
990 dst_out - out_off - src_out);
991 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
992 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
993 src+1+src_nparam+src_in+src_out,
994 isl_min(dst_map->extra-div_off, src_map->n_div));
995 if (dst_map->n_div-div_off > src_map->n_div)
996 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
997 div_off+src_map->n_div,
998 dst_map->n_div - div_off - src_map->n_div);
1001 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1002 struct isl_basic_map *src_map, isl_int *src,
1003 unsigned in_off, unsigned out_off, unsigned div_off)
1005 isl_int_set(dst[0], src[0]);
1006 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1009 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1010 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1015 if (!bmap1 || !bmap2)
1018 div_off = bmap1->n_div;
1020 for (i = 0; i < bmap2->n_eq; ++i) {
1021 int i1 = isl_basic_map_alloc_equality(bmap1);
1024 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1025 i_pos, o_pos, div_off);
1028 for (i = 0; i < bmap2->n_ineq; ++i) {
1029 int i1 = isl_basic_map_alloc_inequality(bmap1);
1032 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1033 i_pos, o_pos, div_off);
1036 for (i = 0; i < bmap2->n_div; ++i) {
1037 int i1 = isl_basic_map_alloc_div(bmap1);
1040 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1041 i_pos, o_pos, div_off);
1044 isl_basic_map_free(bmap2);
1049 isl_basic_map_free(bmap1);
1050 isl_basic_map_free(bmap2);
1054 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1055 struct isl_basic_set *bset2, unsigned pos)
1057 return (struct isl_basic_set *)
1058 add_constraints((struct isl_basic_map *)bset1,
1059 (struct isl_basic_map *)bset2, 0, pos);
1062 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1063 struct isl_dim *dim, unsigned extra,
1064 unsigned n_eq, unsigned n_ineq)
1066 struct isl_basic_map *ext;
1076 dims_ok = isl_dim_equal(base->dim, dim) &&
1077 base->extra >= base->n_div + extra;
1079 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1080 room_for_ineq(base, n_ineq)) {
1085 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1086 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1087 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1088 extra += base->extra;
1090 n_ineq += base->n_ineq;
1092 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1098 ext->sample = isl_vec_copy(base->sample);
1099 flags = base->flags;
1100 ext = add_constraints(ext, base, 0, 0);
1103 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1110 isl_basic_map_free(base);
1114 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1115 struct isl_dim *dim, unsigned extra,
1116 unsigned n_eq, unsigned n_ineq)
1118 return (struct isl_basic_set *)
1119 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1120 extra, n_eq, n_ineq);
1123 struct isl_basic_map *isl_basic_map_extend_constraints(
1124 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1128 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1132 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1133 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1134 unsigned n_eq, unsigned n_ineq)
1136 struct isl_basic_map *bmap;
1137 struct isl_dim *dim;
1141 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1145 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1148 isl_basic_map_free(base);
1152 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1153 unsigned nparam, unsigned dim, unsigned extra,
1154 unsigned n_eq, unsigned n_ineq)
1156 return (struct isl_basic_set *)
1157 isl_basic_map_extend((struct isl_basic_map *)base,
1158 nparam, 0, dim, extra, n_eq, n_ineq);
1161 struct isl_basic_set *isl_basic_set_extend_constraints(
1162 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1164 return (struct isl_basic_set *)
1165 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1169 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1171 return (struct isl_basic_set *)
1172 isl_basic_map_cow((struct isl_basic_map *)bset);
1175 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1180 if (bmap->ref > 1) {
1182 bmap = isl_basic_map_dup(bmap);
1185 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1189 struct isl_set *isl_set_cow(struct isl_set *set)
1197 return isl_set_dup(set);
1200 struct isl_map *isl_map_cow(struct isl_map *map)
1208 return isl_map_dup(map);
1211 static void swap_vars(struct isl_blk blk, isl_int *a,
1212 unsigned a_len, unsigned b_len)
1214 isl_seq_cpy(blk.data, a+a_len, b_len);
1215 isl_seq_cpy(blk.data+b_len, a, a_len);
1216 isl_seq_cpy(a, blk.data, b_len+a_len);
1219 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1220 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1228 isl_assert(bmap->ctx,
1229 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1231 if (n1 == 0 || n2 == 0)
1234 bmap = isl_basic_map_cow(bmap);
1238 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1239 if (isl_blk_is_error(blk))
1242 for (i = 0; i < bmap->n_eq; ++i)
1244 bmap->eq[i] + pos, n1, n2);
1246 for (i = 0; i < bmap->n_ineq; ++i)
1248 bmap->ineq[i] + pos, n1, n2);
1250 for (i = 0; i < bmap->n_div; ++i)
1252 bmap->div[i]+1 + pos, n1, n2);
1254 isl_blk_free(bmap->ctx, blk);
1256 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1257 bmap = isl_basic_map_gauss(bmap, NULL);
1258 return isl_basic_map_finalize(bmap);
1260 isl_basic_map_free(bmap);
1264 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1265 __isl_take isl_basic_set *bset, unsigned n)
1270 nparam = isl_basic_set_n_param(bset);
1271 dim = isl_basic_set_n_dim(bset);
1272 isl_assert(bset->ctx, n <= dim, goto error);
1274 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1276 isl_basic_set_free(bset);
1280 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1286 total = isl_basic_map_total_dim(bmap);
1287 isl_basic_map_free_div(bmap, bmap->n_div);
1288 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1290 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1292 i = isl_basic_map_alloc_equality(bmap);
1296 isl_int_set_si(bmap->eq[i][0], 1);
1297 isl_seq_clr(bmap->eq[i]+1, total);
1298 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1299 isl_vec_free(bmap->sample);
1300 bmap->sample = NULL;
1301 return isl_basic_map_finalize(bmap);
1303 isl_basic_map_free(bmap);
1307 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1309 return (struct isl_basic_set *)
1310 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1313 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1316 unsigned off = isl_dim_total(bmap->dim);
1317 isl_int *t = bmap->div[a];
1318 bmap->div[a] = bmap->div[b];
1321 for (i = 0; i < bmap->n_eq; ++i)
1322 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1324 for (i = 0; i < bmap->n_ineq; ++i)
1325 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1327 for (i = 0; i < bmap->n_div; ++i)
1328 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1329 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1332 /* Eliminate the specified n dimensions starting at first from the
1333 * constraints using Fourier-Motzkin. The dimensions themselves
1336 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1337 enum isl_dim_type type, unsigned first, unsigned n)
1346 map = isl_map_cow(map);
1349 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1350 first += pos(map->dim, type) - 1;
1352 for (i = 0; i < map->n; ++i) {
1353 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1363 /* Eliminate the specified n dimensions starting at first from the
1364 * constraints using Fourier-Motzkin. The dimensions themselves
1367 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1368 enum isl_dim_type type, unsigned first, unsigned n)
1370 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1373 /* Eliminate the specified n dimensions starting at first from the
1374 * constraints using Fourier-Motzkin. The dimensions themselves
1377 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1378 unsigned first, unsigned n)
1380 return isl_set_eliminate(set, isl_dim_set, first, n);
1383 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1384 __isl_take isl_basic_map *bmap)
1386 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1391 return isl_basic_map_finalize(bmap);
1394 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1395 __isl_take isl_basic_set *bset)
1397 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1398 (struct isl_basic_map *)bset);
1401 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1410 map = isl_map_cow(map);
1414 for (i = 0; i < map->n; ++i) {
1415 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1425 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1427 return isl_map_remove_divs(set);
1430 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1431 enum isl_dim_type type, unsigned first, unsigned n)
1435 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1439 bmap = isl_basic_map_eliminate_vars(bmap,
1440 isl_basic_map_offset(bmap, type) - 1 + first, n);
1443 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1445 bmap = isl_basic_map_drop(bmap, type, first, n);
1448 isl_basic_map_free(bmap);
1452 /* Return true if the definition of the given div (recursively) involves
1453 * any of the given variables.
1455 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1456 unsigned first, unsigned n)
1459 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1461 if (isl_int_is_zero(bmap->div[div][0]))
1463 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1466 for (i = bmap->n_div - 1; i >= 0; --i) {
1467 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1469 if (div_involves_vars(bmap, i, first, n))
1476 /* Remove all divs (recursively) involving any of the given dimensions
1477 * in their definitions.
1479 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1480 __isl_take isl_basic_map *bmap,
1481 enum isl_dim_type type, unsigned first, unsigned n)
1487 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1489 first += isl_basic_map_offset(bmap, type);
1491 for (i = bmap->n_div - 1; i >= 0; --i) {
1492 if (!div_involves_vars(bmap, i, first, n))
1494 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1499 isl_basic_map_free(bmap);
1503 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1504 enum isl_dim_type type, unsigned first, unsigned n)
1513 map = isl_map_cow(map);
1517 for (i = 0; i < map->n; ++i) {
1518 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1529 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1530 enum isl_dim_type type, unsigned first, unsigned n)
1532 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1536 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1537 enum isl_dim_type type, unsigned first, unsigned n)
1544 if (first + n > isl_basic_set_dim(bset, type))
1545 isl_die(bset->ctx, isl_error_invalid,
1546 "index out of bounds", return -1);
1548 first += isl_basic_set_offset(bset, type);
1549 for (i = 0; i < bset->n_eq; ++i)
1550 if (isl_seq_first_non_zero(bset->eq[i] + first, n) >= 0)
1552 for (i = 0; i < bset->n_ineq; ++i)
1553 if (isl_seq_first_non_zero(bset->ineq[i] + first, n) >= 0)
1559 int isl_set_involves_dims(__isl_keep isl_set *set,
1560 enum isl_dim_type type, unsigned first, unsigned n)
1567 if (first + n > isl_set_dim(set, type))
1568 isl_die(set->ctx, isl_error_invalid,
1569 "index out of bounds", return -1);
1571 for (i = 0; i < set->n; ++i) {
1572 int involves = isl_basic_set_involves_dims(set->p[i],
1574 if (involves < 0 || !involves)
1581 /* Return true if the definition of the given div is unknown or depends
1584 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1587 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1589 if (isl_int_is_zero(bmap->div[div][0]))
1592 for (i = bmap->n_div - 1; i >= 0; --i) {
1593 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1595 if (div_is_unknown(bmap, i))
1602 /* Remove all divs that are unknown or defined in terms of unknown divs.
1604 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1605 __isl_take isl_basic_map *bmap)
1612 for (i = bmap->n_div - 1; i >= 0; --i) {
1613 if (!div_is_unknown(bmap, i))
1615 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1621 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1630 map = isl_map_cow(map);
1634 for (i = 0; i < map->n; ++i) {
1635 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1645 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1647 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1650 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1651 __isl_take isl_basic_set *bset,
1652 enum isl_dim_type type, unsigned first, unsigned n)
1654 return (isl_basic_set *)
1655 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1658 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1659 enum isl_dim_type type, unsigned first, unsigned n)
1666 map = isl_map_cow(map);
1669 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1671 for (i = 0; i < map->n; ++i) {
1672 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1673 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1677 map = isl_map_drop(map, type, first, n);
1684 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1685 enum isl_dim_type type, unsigned first, unsigned n)
1687 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1690 /* Project out n inputs starting at first using Fourier-Motzkin */
1691 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1692 unsigned first, unsigned n)
1694 return isl_map_remove_dims(map, isl_dim_in, first, n);
1697 static void dump_term(struct isl_basic_map *bmap,
1698 isl_int c, int pos, FILE *out)
1701 unsigned in = isl_basic_map_n_in(bmap);
1702 unsigned dim = in + isl_basic_map_n_out(bmap);
1703 unsigned nparam = isl_basic_map_n_param(bmap);
1705 isl_int_print(out, c, 0);
1707 if (!isl_int_is_one(c))
1708 isl_int_print(out, c, 0);
1709 if (pos < 1 + nparam) {
1710 name = isl_dim_get_name(bmap->dim,
1711 isl_dim_param, pos - 1);
1713 fprintf(out, "%s", name);
1715 fprintf(out, "p%d", pos - 1);
1716 } else if (pos < 1 + nparam + in)
1717 fprintf(out, "i%d", pos - 1 - nparam);
1718 else if (pos < 1 + nparam + dim)
1719 fprintf(out, "o%d", pos - 1 - nparam - in);
1721 fprintf(out, "e%d", pos - 1 - nparam - dim);
1725 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1726 int sign, FILE *out)
1730 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1734 for (i = 0, first = 1; i < len; ++i) {
1735 if (isl_int_sgn(c[i]) * sign <= 0)
1738 fprintf(out, " + ");
1740 isl_int_abs(v, c[i]);
1741 dump_term(bmap, v, i, out);
1748 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1749 const char *op, FILE *out, int indent)
1753 fprintf(out, "%*s", indent, "");
1755 dump_constraint_sign(bmap, c, 1, out);
1756 fprintf(out, " %s ", op);
1757 dump_constraint_sign(bmap, c, -1, out);
1761 for (i = bmap->n_div; i < bmap->extra; ++i) {
1762 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1764 fprintf(out, "%*s", indent, "");
1765 fprintf(out, "ERROR: unused div coefficient not zero\n");
1770 static void dump_constraints(struct isl_basic_map *bmap,
1771 isl_int **c, unsigned n,
1772 const char *op, FILE *out, int indent)
1776 for (i = 0; i < n; ++i)
1777 dump_constraint(bmap, c[i], op, out, indent);
1780 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1784 unsigned total = isl_basic_map_total_dim(bmap);
1786 for (j = 0; j < 1 + total; ++j) {
1787 if (isl_int_is_zero(exp[j]))
1789 if (!first && isl_int_is_pos(exp[j]))
1791 dump_term(bmap, exp[j], j, out);
1796 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1800 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1801 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1803 for (i = 0; i < bmap->n_div; ++i) {
1804 fprintf(out, "%*s", indent, "");
1805 fprintf(out, "e%d = [(", i);
1806 dump_affine(bmap, bmap->div[i]+1, out);
1808 isl_int_print(out, bmap->div[i][0], 0);
1809 fprintf(out, "]\n");
1813 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1814 FILE *out, int indent)
1817 fprintf(out, "null basic set\n");
1821 fprintf(out, "%*s", indent, "");
1822 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1823 bset->ref, bset->dim->nparam, bset->dim->n_out,
1824 bset->extra, bset->flags);
1825 dump((struct isl_basic_map *)bset, out, indent);
1828 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1829 FILE *out, int indent)
1832 fprintf(out, "null basic map\n");
1836 fprintf(out, "%*s", indent, "");
1837 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1838 "flags: %x, n_name: %d\n",
1840 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1841 bmap->extra, bmap->flags, bmap->dim->n_name);
1842 dump(bmap, out, indent);
1845 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1850 total = isl_basic_map_total_dim(bmap);
1851 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1852 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1853 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1854 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1858 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1860 struct isl_set *set;
1864 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1865 isl_assert(dim->ctx, n >= 0, goto error);
1866 set = isl_alloc(dim->ctx, struct isl_set,
1867 sizeof(struct isl_set) +
1868 (n - 1) * sizeof(struct isl_basic_set *));
1872 set->ctx = dim->ctx;
1873 isl_ctx_ref(set->ctx);
1885 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1886 unsigned nparam, unsigned dim, int n, unsigned flags)
1888 struct isl_set *set;
1889 struct isl_dim *dims;
1891 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1895 set = isl_set_alloc_dim(dims, n, flags);
1899 /* Make sure "map" has room for at least "n" more basic maps.
1901 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1904 struct isl_map *grown = NULL;
1908 isl_assert(map->ctx, n >= 0, goto error);
1909 if (map->n + n <= map->size)
1911 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1914 for (i = 0; i < map->n; ++i) {
1915 grown->p[i] = isl_basic_map_copy(map->p[i]);
1923 isl_map_free(grown);
1928 /* Make sure "set" has room for at least "n" more basic sets.
1930 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1932 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1935 struct isl_set *isl_set_dup(struct isl_set *set)
1938 struct isl_set *dup;
1943 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1946 for (i = 0; i < set->n; ++i)
1947 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1951 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1953 return isl_map_from_basic_map(bset);
1956 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1958 struct isl_map *map;
1963 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1964 return isl_map_add_basic_map(map, bmap);
1967 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1968 __isl_take isl_basic_set *bset)
1970 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1971 (struct isl_basic_map *)bset);
1974 void isl_set_free(struct isl_set *set)
1984 isl_ctx_deref(set->ctx);
1985 for (i = 0; i < set->n; ++i)
1986 isl_basic_set_free(set->p[i]);
1987 isl_dim_free(set->dim);
1991 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
1996 fprintf(out, "null set\n");
2000 fprintf(out, "%*s", indent, "");
2001 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2002 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2004 for (i = 0; i < set->n; ++i) {
2005 fprintf(out, "%*s", indent, "");
2006 fprintf(out, "basic set %d:\n", i);
2007 isl_basic_set_print_internal(set->p[i], out, indent+4);
2011 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2016 fprintf(out, "null map\n");
2020 fprintf(out, "%*s", indent, "");
2021 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2022 "flags: %x, n_name: %d\n",
2023 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2024 map->dim->n_out, map->flags, map->dim->n_name);
2025 for (i = 0; i < map->n; ++i) {
2026 fprintf(out, "%*s", indent, "");
2027 fprintf(out, "basic map %d:\n", i);
2028 isl_basic_map_print_internal(map->p[i], out, indent+4);
2032 struct isl_basic_map *isl_basic_map_intersect_domain(
2033 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2035 struct isl_basic_map *bmap_domain;
2040 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2041 bset->dim, isl_dim_param), goto error);
2043 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2044 isl_assert(bset->ctx,
2045 isl_basic_map_compatible_domain(bmap, bset), goto error);
2047 bmap = isl_basic_map_cow(bmap);
2050 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2051 bset->n_div, bset->n_eq, bset->n_ineq);
2052 bmap_domain = isl_basic_map_from_domain(bset);
2053 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2055 bmap = isl_basic_map_simplify(bmap);
2056 return isl_basic_map_finalize(bmap);
2058 isl_basic_map_free(bmap);
2059 isl_basic_set_free(bset);
2063 struct isl_basic_map *isl_basic_map_intersect_range(
2064 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2066 struct isl_basic_map *bmap_range;
2071 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2072 bset->dim, isl_dim_param), goto error);
2074 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2075 isl_assert(bset->ctx,
2076 isl_basic_map_compatible_range(bmap, bset), goto error);
2078 if (isl_basic_set_is_universe(bset)) {
2079 isl_basic_set_free(bset);
2083 bmap = isl_basic_map_cow(bmap);
2086 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2087 bset->n_div, bset->n_eq, bset->n_ineq);
2088 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2089 bmap = add_constraints(bmap, bmap_range, 0, 0);
2091 bmap = isl_basic_map_simplify(bmap);
2092 return isl_basic_map_finalize(bmap);
2094 isl_basic_map_free(bmap);
2095 isl_basic_set_free(bset);
2099 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2105 total = 1 + isl_basic_map_total_dim(bmap);
2106 if (total != vec->size)
2111 for (i = 0; i < bmap->n_eq; ++i) {
2112 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2113 if (!isl_int_is_zero(s)) {
2119 for (i = 0; i < bmap->n_ineq; ++i) {
2120 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2121 if (isl_int_is_neg(s)) {
2132 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2134 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2137 struct isl_basic_map *isl_basic_map_intersect(
2138 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2140 struct isl_vec *sample = NULL;
2142 if (!bmap1 || !bmap2)
2145 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2146 bmap2->dim, isl_dim_param), goto error);
2147 if (isl_dim_total(bmap1->dim) ==
2148 isl_dim_size(bmap1->dim, isl_dim_param) &&
2149 isl_dim_total(bmap2->dim) !=
2150 isl_dim_size(bmap2->dim, isl_dim_param))
2151 return isl_basic_map_intersect(bmap2, bmap1);
2153 if (isl_dim_total(bmap2->dim) !=
2154 isl_dim_size(bmap2->dim, isl_dim_param))
2155 isl_assert(bmap1->ctx,
2156 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2158 if (bmap1->sample &&
2159 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2160 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2161 sample = isl_vec_copy(bmap1->sample);
2162 else if (bmap2->sample &&
2163 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2164 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2165 sample = isl_vec_copy(bmap2->sample);
2167 bmap1 = isl_basic_map_cow(bmap1);
2170 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2171 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2172 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2175 isl_vec_free(sample);
2177 isl_vec_free(bmap1->sample);
2178 bmap1->sample = sample;
2181 bmap1 = isl_basic_map_simplify(bmap1);
2182 return isl_basic_map_finalize(bmap1);
2185 isl_vec_free(sample);
2186 isl_basic_map_free(bmap1);
2187 isl_basic_map_free(bmap2);
2191 struct isl_basic_set *isl_basic_set_intersect(
2192 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2194 return (struct isl_basic_set *)
2195 isl_basic_map_intersect(
2196 (struct isl_basic_map *)bset1,
2197 (struct isl_basic_map *)bset2);
2200 /* Special case of isl_map_intersect, where both map1 and map2
2201 * are convex, without any divs and such that either map1 or map2
2202 * contains a single constraint. This constraint is then simply
2203 * added to the other map.
2205 static __isl_give isl_map *map_intersect_add_constraint(
2206 __isl_take isl_map *map1, __isl_take isl_map *map2)
2208 isl_assert(map1->ctx, map1->n == 1, goto error);
2209 isl_assert(map2->ctx, map1->n == 1, goto error);
2210 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2211 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2213 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2214 return isl_map_intersect(map2, map1);
2216 isl_assert(map2->ctx,
2217 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2219 map1 = isl_map_cow(map1);
2222 if (isl_map_plain_is_empty(map1)) {
2226 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2227 if (map2->p[0]->n_eq == 1)
2228 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2230 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2231 map2->p[0]->ineq[0]);
2233 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2234 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2238 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2239 isl_basic_map_free(map1->p[0]);
2252 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2255 struct isl_map *result;
2261 if (isl_map_plain_is_empty(map1)) {
2265 if (isl_map_plain_is_empty(map2)) {
2270 if (map1->n == 1 && map2->n == 1 &&
2271 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2272 isl_dim_equal(map1->dim, map2->dim) &&
2273 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2274 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2275 return map_intersect_add_constraint(map1, map2);
2276 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2277 map2->dim, isl_dim_param), goto error);
2278 if (isl_dim_total(map1->dim) ==
2279 isl_dim_size(map1->dim, isl_dim_param) &&
2280 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2281 return isl_map_intersect(map2, map1);
2283 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2284 isl_assert(map1->ctx,
2285 isl_dim_equal(map1->dim, map2->dim), goto error);
2287 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2288 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2289 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2291 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2292 map1->n * map2->n, flags);
2295 for (i = 0; i < map1->n; ++i)
2296 for (j = 0; j < map2->n; ++j) {
2297 struct isl_basic_map *part;
2298 part = isl_basic_map_intersect(
2299 isl_basic_map_copy(map1->p[i]),
2300 isl_basic_map_copy(map2->p[j]));
2301 if (isl_basic_map_is_empty(part))
2302 isl_basic_map_free(part);
2304 result = isl_map_add_basic_map(result, part);
2317 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2319 return (struct isl_set *)
2320 isl_map_intersect((struct isl_map *)set1,
2321 (struct isl_map *)set2);
2324 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2326 struct isl_dim *dim;
2327 struct isl_basic_set *bset;
2332 bmap = isl_basic_map_cow(bmap);
2335 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2336 in = isl_basic_map_n_in(bmap);
2337 bset = isl_basic_set_from_basic_map(bmap);
2338 bset = isl_basic_set_swap_vars(bset, in);
2339 return isl_basic_map_from_basic_set(bset, dim);
2342 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2343 enum isl_dim_type type, unsigned pos, unsigned n)
2345 struct isl_dim *res_dim;
2346 struct isl_basic_map *res;
2347 struct isl_dim_map *dim_map;
2348 unsigned total, off;
2349 enum isl_dim_type t;
2357 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2359 total = isl_basic_map_total_dim(bmap) + n;
2360 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2362 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2364 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2366 unsigned size = isl_basic_map_dim(bmap, t);
2367 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2369 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2370 pos, size - pos, off + pos + n);
2372 off += isl_dim_size(res_dim, t);
2374 isl_dim_map_div(dim_map, bmap, off);
2376 res = isl_basic_map_alloc_dim(res_dim,
2377 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2378 if (isl_basic_map_is_rational(bmap))
2379 res = isl_basic_map_set_rational(res);
2380 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2381 return isl_basic_map_finalize(res);
2384 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2385 enum isl_dim_type type, unsigned n)
2389 return isl_basic_map_insert(bmap, type,
2390 isl_basic_map_dim(bmap, type), n);
2393 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2394 enum isl_dim_type type, unsigned n)
2398 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2399 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2401 isl_basic_set_free(bset);
2405 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2406 enum isl_dim_type type, unsigned pos, unsigned n)
2413 map = isl_map_cow(map);
2417 map->dim = isl_dim_insert(map->dim, type, pos, n);
2421 for (i = 0; i < map->n; ++i) {
2422 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2433 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2434 enum isl_dim_type type, unsigned pos, unsigned n)
2436 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2439 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2440 enum isl_dim_type type, unsigned n)
2444 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2447 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2448 enum isl_dim_type type, unsigned n)
2452 isl_assert(set->ctx, type != isl_dim_in, goto error);
2453 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2459 __isl_give isl_basic_map *isl_basic_map_move_dims(
2460 __isl_take isl_basic_map *bmap,
2461 enum isl_dim_type dst_type, unsigned dst_pos,
2462 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2464 struct isl_dim_map *dim_map;
2465 struct isl_basic_map *res;
2466 enum isl_dim_type t;
2467 unsigned total, off;
2474 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2477 if (dst_type == src_type && dst_pos == src_pos)
2480 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2482 if (pos(bmap->dim, dst_type) + dst_pos ==
2483 pos(bmap->dim, src_type) + src_pos +
2484 ((src_type < dst_type) ? n : 0)) {
2485 bmap = isl_basic_map_cow(bmap);
2489 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2490 src_type, src_pos, n);
2494 bmap = isl_basic_map_finalize(bmap);
2499 total = isl_basic_map_total_dim(bmap);
2500 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2503 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2504 unsigned size = isl_dim_size(bmap->dim, t);
2505 if (t == dst_type) {
2506 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2509 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2512 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2513 dst_pos, size - dst_pos, off);
2514 off += size - dst_pos;
2515 } else if (t == src_type) {
2516 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2519 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2520 src_pos + n, size - src_pos - n, off);
2521 off += size - src_pos - n;
2523 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2527 isl_dim_map_div(dim_map, bmap, off);
2529 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2530 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2531 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2533 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2534 src_type, src_pos, n);
2538 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2539 bmap = isl_basic_map_gauss(bmap, NULL);
2540 bmap = isl_basic_map_finalize(bmap);
2544 isl_basic_map_free(bmap);
2548 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2549 enum isl_dim_type dst_type, unsigned dst_pos,
2550 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2552 return (isl_basic_set *)isl_basic_map_move_dims(
2553 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2556 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2557 enum isl_dim_type dst_type, unsigned dst_pos,
2558 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2562 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2563 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2564 src_type, src_pos, n);
2570 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2571 enum isl_dim_type dst_type, unsigned dst_pos,
2572 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2581 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2584 if (dst_type == src_type && dst_pos == src_pos)
2587 isl_assert(map->ctx, dst_type != src_type, goto error);
2589 map = isl_map_cow(map);
2593 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2597 for (i = 0; i < map->n; ++i) {
2598 map->p[i] = isl_basic_map_move_dims(map->p[i],
2600 src_type, src_pos, n);
2611 /* Move the specified dimensions to the last columns right before
2612 * the divs. Don't change the dimension specification of bmap.
2613 * That's the responsibility of the caller.
2615 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2616 enum isl_dim_type type, unsigned first, unsigned n)
2618 struct isl_dim_map *dim_map;
2619 struct isl_basic_map *res;
2620 enum isl_dim_type t;
2621 unsigned total, off;
2625 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2628 total = isl_basic_map_total_dim(bmap);
2629 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2632 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2633 unsigned size = isl_dim_size(bmap->dim, t);
2635 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2638 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2639 first, n, total - bmap->n_div - n);
2640 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2641 first + n, size - (first + n), off);
2642 off += size - (first + n);
2644 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2648 isl_dim_map_div(dim_map, bmap, off + n);
2650 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2651 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2652 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2656 /* Turn the n dimensions of type type, starting at first
2657 * into existentially quantified variables.
2659 __isl_give isl_basic_map *isl_basic_map_project_out(
2660 __isl_take isl_basic_map *bmap,
2661 enum isl_dim_type type, unsigned first, unsigned n)
2674 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2675 return isl_basic_map_remove_dims(bmap, type, first, n);
2677 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2680 bmap = move_last(bmap, type, first, n);
2681 bmap = isl_basic_map_cow(bmap);
2685 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2686 old = bmap->block2.data;
2687 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2688 (bmap->extra + n) * (1 + row_size));
2689 if (!bmap->block2.data)
2691 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2694 for (i = 0; i < n; ++i) {
2695 new_div[i] = bmap->block2.data +
2696 (bmap->extra + i) * (1 + row_size);
2697 isl_seq_clr(new_div[i], 1 + row_size);
2699 for (i = 0; i < bmap->extra; ++i)
2700 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2702 bmap->div = new_div;
2706 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2709 bmap = isl_basic_map_simplify(bmap);
2710 bmap = isl_basic_map_drop_redundant_divs(bmap);
2711 return isl_basic_map_finalize(bmap);
2713 isl_basic_map_free(bmap);
2717 /* Turn the n dimensions of type type, starting at first
2718 * into existentially quantified variables.
2720 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2721 enum isl_dim_type type, unsigned first, unsigned n)
2723 return (isl_basic_set *)isl_basic_map_project_out(
2724 (isl_basic_map *)bset, type, first, n);
2727 /* Turn the n dimensions of type type, starting at first
2728 * into existentially quantified variables.
2730 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2731 enum isl_dim_type type, unsigned first, unsigned n)
2741 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2743 map = isl_map_cow(map);
2747 map->dim = isl_dim_drop(map->dim, type, first, n);
2751 for (i = 0; i < map->n; ++i) {
2752 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2763 /* Turn the n dimensions of type type, starting at first
2764 * into existentially quantified variables.
2766 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2767 enum isl_dim_type type, unsigned first, unsigned n)
2769 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2772 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2776 for (i = 0; i < n; ++i) {
2777 j = isl_basic_map_alloc_div(bmap);
2780 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2784 isl_basic_map_free(bmap);
2788 struct isl_basic_map *isl_basic_map_apply_range(
2789 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2791 struct isl_dim *dim_result = NULL;
2792 struct isl_basic_map *bmap;
2793 unsigned n_in, n_out, n, nparam, total, pos;
2794 struct isl_dim_map *dim_map1, *dim_map2;
2796 if (!bmap1 || !bmap2)
2799 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2800 isl_dim_copy(bmap2->dim));
2802 n_in = isl_basic_map_n_in(bmap1);
2803 n_out = isl_basic_map_n_out(bmap2);
2804 n = isl_basic_map_n_out(bmap1);
2805 nparam = isl_basic_map_n_param(bmap1);
2807 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2808 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2809 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2810 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2811 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2812 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2813 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2814 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2815 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2816 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2817 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2819 bmap = isl_basic_map_alloc_dim(dim_result,
2820 bmap1->n_div + bmap2->n_div + n,
2821 bmap1->n_eq + bmap2->n_eq,
2822 bmap1->n_ineq + bmap2->n_ineq);
2823 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2824 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2825 bmap = add_divs(bmap, n);
2826 bmap = isl_basic_map_simplify(bmap);
2827 bmap = isl_basic_map_drop_redundant_divs(bmap);
2828 return isl_basic_map_finalize(bmap);
2830 isl_basic_map_free(bmap1);
2831 isl_basic_map_free(bmap2);
2835 struct isl_basic_set *isl_basic_set_apply(
2836 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2841 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2844 return (struct isl_basic_set *)
2845 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2847 isl_basic_set_free(bset);
2848 isl_basic_map_free(bmap);
2852 struct isl_basic_map *isl_basic_map_apply_domain(
2853 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2855 if (!bmap1 || !bmap2)
2858 isl_assert(bmap1->ctx,
2859 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2860 isl_assert(bmap1->ctx,
2861 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2864 bmap1 = isl_basic_map_reverse(bmap1);
2865 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2866 return isl_basic_map_reverse(bmap1);
2868 isl_basic_map_free(bmap1);
2869 isl_basic_map_free(bmap2);
2873 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2874 * A \cap B -> f(A) + f(B)
2876 struct isl_basic_map *isl_basic_map_sum(
2877 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2879 unsigned n_in, n_out, nparam, total, pos;
2880 struct isl_basic_map *bmap = NULL;
2881 struct isl_dim_map *dim_map1, *dim_map2;
2884 if (!bmap1 || !bmap2)
2887 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2890 nparam = isl_basic_map_n_param(bmap1);
2891 n_in = isl_basic_map_n_in(bmap1);
2892 n_out = isl_basic_map_n_out(bmap1);
2894 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2895 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2896 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2897 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2898 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2899 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2900 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2901 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2902 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2903 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2904 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2906 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2907 bmap1->n_div + bmap2->n_div + 2 * n_out,
2908 bmap1->n_eq + bmap2->n_eq + n_out,
2909 bmap1->n_ineq + bmap2->n_ineq);
2910 for (i = 0; i < n_out; ++i) {
2911 int j = isl_basic_map_alloc_equality(bmap);
2914 isl_seq_clr(bmap->eq[j], 1+total);
2915 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2916 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2917 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2919 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2920 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2921 bmap = add_divs(bmap, 2 * n_out);
2923 bmap = isl_basic_map_simplify(bmap);
2924 return isl_basic_map_finalize(bmap);
2926 isl_basic_map_free(bmap);
2927 isl_basic_map_free(bmap1);
2928 isl_basic_map_free(bmap2);
2932 /* Given two maps A -> f(A) and B -> g(B), construct a map
2933 * A \cap B -> f(A) + f(B)
2935 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2937 struct isl_map *result;
2943 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2945 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2946 map1->n * map2->n, 0);
2949 for (i = 0; i < map1->n; ++i)
2950 for (j = 0; j < map2->n; ++j) {
2951 struct isl_basic_map *part;
2952 part = isl_basic_map_sum(
2953 isl_basic_map_copy(map1->p[i]),
2954 isl_basic_map_copy(map2->p[j]));
2955 if (isl_basic_map_is_empty(part))
2956 isl_basic_map_free(part);
2958 result = isl_map_add_basic_map(result, part);
2971 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2972 __isl_take isl_set *set2)
2974 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2977 /* Given a basic map A -> f(A), construct A -> -f(A).
2979 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2984 bmap = isl_basic_map_cow(bmap);
2988 n = isl_basic_map_dim(bmap, isl_dim_out);
2989 off = isl_basic_map_offset(bmap, isl_dim_out);
2990 for (i = 0; i < bmap->n_eq; ++i)
2991 for (j = 0; j < n; ++j)
2992 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2993 for (i = 0; i < bmap->n_ineq; ++i)
2994 for (j = 0; j < n; ++j)
2995 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2996 for (i = 0; i < bmap->n_div; ++i)
2997 for (j = 0; j < n; ++j)
2998 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2999 return isl_basic_map_finalize(bmap);
3002 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3004 return isl_basic_map_neg(bset);
3007 /* Given a map A -> f(A), construct A -> -f(A).
3009 struct isl_map *isl_map_neg(struct isl_map *map)
3013 map = isl_map_cow(map);
3017 for (i = 0; i < map->n; ++i) {
3018 map->p[i] = isl_basic_map_neg(map->p[i]);
3029 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3031 return (isl_set *)isl_map_neg((isl_map *)set);
3034 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3035 * A -> floor(f(A)/d).
3037 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3040 unsigned n_in, n_out, nparam, total, pos;
3041 struct isl_basic_map *result = NULL;
3042 struct isl_dim_map *dim_map;
3048 nparam = isl_basic_map_n_param(bmap);
3049 n_in = isl_basic_map_n_in(bmap);
3050 n_out = isl_basic_map_n_out(bmap);
3052 total = nparam + n_in + n_out + bmap->n_div + n_out;
3053 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3054 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3055 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3056 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3057 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3059 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3060 bmap->n_div + n_out,
3061 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3062 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3063 result = add_divs(result, n_out);
3064 for (i = 0; i < n_out; ++i) {
3066 j = isl_basic_map_alloc_inequality(result);
3069 isl_seq_clr(result->ineq[j], 1+total);
3070 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3071 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3072 j = isl_basic_map_alloc_inequality(result);
3075 isl_seq_clr(result->ineq[j], 1+total);
3076 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3077 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3078 isl_int_sub_ui(result->ineq[j][0], d, 1);
3081 result = isl_basic_map_simplify(result);
3082 return isl_basic_map_finalize(result);
3084 isl_basic_map_free(result);
3088 /* Given a map A -> f(A) and an integer d, construct a map
3089 * A -> floor(f(A)/d).
3091 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3095 map = isl_map_cow(map);
3099 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3100 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3101 for (i = 0; i < map->n; ++i) {
3102 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3113 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3119 i = isl_basic_map_alloc_equality(bmap);
3122 nparam = isl_basic_map_n_param(bmap);
3123 n_in = isl_basic_map_n_in(bmap);
3124 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3125 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3126 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3127 return isl_basic_map_finalize(bmap);
3129 isl_basic_map_free(bmap);
3133 /* Add a constraints to "bmap" expressing i_pos < o_pos
3135 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3141 i = isl_basic_map_alloc_inequality(bmap);
3144 nparam = isl_basic_map_n_param(bmap);
3145 n_in = isl_basic_map_n_in(bmap);
3146 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3147 isl_int_set_si(bmap->ineq[i][0], -1);
3148 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3149 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3150 return isl_basic_map_finalize(bmap);
3152 isl_basic_map_free(bmap);
3156 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3158 static __isl_give isl_basic_map *var_less_or_equal(
3159 __isl_take isl_basic_map *bmap, unsigned pos)
3165 i = isl_basic_map_alloc_inequality(bmap);
3168 nparam = isl_basic_map_n_param(bmap);
3169 n_in = isl_basic_map_n_in(bmap);
3170 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3171 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3172 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3173 return isl_basic_map_finalize(bmap);
3175 isl_basic_map_free(bmap);
3179 /* Add a constraints to "bmap" expressing i_pos > o_pos
3181 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3187 i = isl_basic_map_alloc_inequality(bmap);
3190 nparam = isl_basic_map_n_param(bmap);
3191 n_in = isl_basic_map_n_in(bmap);
3192 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3193 isl_int_set_si(bmap->ineq[i][0], -1);
3194 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3195 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3196 return isl_basic_map_finalize(bmap);
3198 isl_basic_map_free(bmap);
3202 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3204 static __isl_give isl_basic_map *var_more_or_equal(
3205 __isl_take isl_basic_map *bmap, unsigned pos)
3211 i = isl_basic_map_alloc_inequality(bmap);
3214 nparam = isl_basic_map_n_param(bmap);
3215 n_in = isl_basic_map_n_in(bmap);
3216 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3217 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3218 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3219 return isl_basic_map_finalize(bmap);
3221 isl_basic_map_free(bmap);
3225 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3228 struct isl_basic_map *bmap;
3229 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3232 for (i = 0; i < n_equal && bmap; ++i)
3233 bmap = var_equal(bmap, i);
3234 return isl_basic_map_finalize(bmap);
3237 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3239 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3242 struct isl_basic_map *bmap;
3243 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3246 for (i = 0; i < pos && bmap; ++i)
3247 bmap = var_equal(bmap, i);
3249 bmap = var_less(bmap, pos);
3250 return isl_basic_map_finalize(bmap);
3253 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3255 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3256 __isl_take isl_dim *dim, unsigned pos)
3259 isl_basic_map *bmap;
3261 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3262 for (i = 0; i < pos; ++i)
3263 bmap = var_equal(bmap, i);
3264 bmap = var_less_or_equal(bmap, pos);
3265 return isl_basic_map_finalize(bmap);
3268 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3270 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3273 struct isl_basic_map *bmap;
3274 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3277 for (i = 0; i < pos && bmap; ++i)
3278 bmap = var_equal(bmap, i);
3280 bmap = var_more(bmap, pos);
3281 return isl_basic_map_finalize(bmap);
3284 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3286 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3287 __isl_take isl_dim *dim, unsigned pos)
3290 isl_basic_map *bmap;
3292 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3293 for (i = 0; i < pos; ++i)
3294 bmap = var_equal(bmap, i);
3295 bmap = var_more_or_equal(bmap, pos);
3296 return isl_basic_map_finalize(bmap);
3299 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3300 unsigned n, int equal)
3302 struct isl_map *map;
3305 if (n == 0 && equal)
3306 return isl_map_universe(dims);
3308 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3310 for (i = 0; i + 1 < n; ++i)
3311 map = isl_map_add_basic_map(map,
3312 isl_basic_map_less_at(isl_dim_copy(dims), i));
3315 map = isl_map_add_basic_map(map,
3316 isl_basic_map_less_or_equal_at(dims, n - 1));
3318 map = isl_map_add_basic_map(map,
3319 isl_basic_map_less_at(dims, n - 1));
3326 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3330 return map_lex_lte_first(dims, dims->n_out, equal);
3333 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3335 return map_lex_lte_first(dim, n, 0);
3338 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3340 return map_lex_lte_first(dim, n, 1);
3343 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3345 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3348 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3350 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3353 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3354 unsigned n, int equal)
3356 struct isl_map *map;
3359 if (n == 0 && equal)
3360 return isl_map_universe(dims);
3362 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3364 for (i = 0; i + 1 < n; ++i)
3365 map = isl_map_add_basic_map(map,
3366 isl_basic_map_more_at(isl_dim_copy(dims), i));
3369 map = isl_map_add_basic_map(map,
3370 isl_basic_map_more_or_equal_at(dims, n - 1));
3372 map = isl_map_add_basic_map(map,
3373 isl_basic_map_more_at(dims, n - 1));
3380 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3384 return map_lex_gte_first(dims, dims->n_out, equal);
3387 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3389 return map_lex_gte_first(dim, n, 0);
3392 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3394 return map_lex_gte_first(dim, n, 1);
3397 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3399 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3402 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3404 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3407 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3408 __isl_take isl_set *set2)
3411 map = isl_map_lex_le(isl_set_get_dim(set1));
3412 map = isl_map_intersect_domain(map, set1);
3413 map = isl_map_intersect_range(map, set2);
3417 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3418 __isl_take isl_set *set2)
3421 map = isl_map_lex_lt(isl_set_get_dim(set1));
3422 map = isl_map_intersect_domain(map, set1);
3423 map = isl_map_intersect_range(map, set2);
3427 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3428 __isl_take isl_set *set2)
3431 map = isl_map_lex_ge(isl_set_get_dim(set1));
3432 map = isl_map_intersect_domain(map, set1);
3433 map = isl_map_intersect_range(map, set2);
3437 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3438 __isl_take isl_set *set2)
3441 map = isl_map_lex_gt(isl_set_get_dim(set1));
3442 map = isl_map_intersect_domain(map, set1);
3443 map = isl_map_intersect_range(map, set2);
3447 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3448 __isl_take isl_map *map2)
3451 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3452 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3453 map = isl_map_apply_range(map, isl_map_reverse(map2));
3457 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3458 __isl_take isl_map *map2)
3461 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3462 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3463 map = isl_map_apply_range(map, isl_map_reverse(map2));
3467 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3468 __isl_take isl_map *map2)
3471 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3472 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3473 map = isl_map_apply_range(map, isl_map_reverse(map2));
3477 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3478 __isl_take isl_map *map2)
3481 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3482 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3483 map = isl_map_apply_range(map, isl_map_reverse(map2));
3487 struct isl_basic_map *isl_basic_map_from_basic_set(
3488 struct isl_basic_set *bset, struct isl_dim *dim)
3490 struct isl_basic_map *bmap;
3492 bset = isl_basic_set_cow(bset);
3496 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3497 isl_dim_free(bset->dim);
3498 bmap = (struct isl_basic_map *) bset;
3500 return isl_basic_map_finalize(bmap);
3502 isl_basic_set_free(bset);
3507 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3511 if (bmap->dim->n_in == 0)
3512 return (struct isl_basic_set *)bmap;
3513 bmap = isl_basic_map_cow(bmap);
3516 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3519 bmap = isl_basic_map_finalize(bmap);
3520 return (struct isl_basic_set *)bmap;
3522 isl_basic_map_free(bmap);
3526 /* For a div d = floor(f/m), add the constraints
3529 * -(f-(n-1)) + m d >= 0
3531 * Note that the second constraint is the negation of
3535 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3536 unsigned pos, isl_int *div)
3539 unsigned total = isl_basic_map_total_dim(bmap);
3541 i = isl_basic_map_alloc_inequality(bmap);
3544 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3545 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3547 j = isl_basic_map_alloc_inequality(bmap);
3550 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3551 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3552 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3556 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3557 unsigned pos, isl_int *div)
3559 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3563 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3565 unsigned total = isl_basic_map_total_dim(bmap);
3566 unsigned div_pos = total - bmap->n_div + div;
3568 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3572 struct isl_basic_set *isl_basic_map_underlying_set(
3573 struct isl_basic_map *bmap)
3577 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3579 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3580 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3581 return (struct isl_basic_set *)bmap;
3582 bmap = isl_basic_map_cow(bmap);
3585 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3588 bmap->extra -= bmap->n_div;
3590 bmap = isl_basic_map_finalize(bmap);
3591 return (struct isl_basic_set *)bmap;
3596 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3597 __isl_take isl_basic_set *bset)
3599 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3602 struct isl_basic_map *isl_basic_map_overlying_set(
3603 struct isl_basic_set *bset, struct isl_basic_map *like)
3605 struct isl_basic_map *bmap;
3606 struct isl_ctx *ctx;
3613 isl_assert(ctx, bset->n_div == 0, goto error);
3614 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3615 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3617 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3618 isl_basic_map_free(like);
3619 return (struct isl_basic_map *)bset;
3621 bset = isl_basic_set_cow(bset);
3624 total = bset->dim->n_out + bset->extra;
3625 bmap = (struct isl_basic_map *)bset;
3626 isl_dim_free(bmap->dim);
3627 bmap->dim = isl_dim_copy(like->dim);
3630 bmap->n_div = like->n_div;
3631 bmap->extra += like->n_div;
3635 ltotal = total - bmap->extra + like->extra;
3638 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3639 bmap->extra * (1 + 1 + total));
3640 if (isl_blk_is_error(bmap->block2))
3642 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3646 for (i = 0; i < bmap->extra; ++i)
3647 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3648 for (i = 0; i < like->n_div; ++i) {
3649 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3650 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3652 bmap = isl_basic_map_extend_constraints(bmap,
3653 0, 2 * like->n_div);
3654 for (i = 0; i < like->n_div; ++i) {
3655 if (isl_int_is_zero(bmap->div[i][0]))
3657 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3661 isl_basic_map_free(like);
3662 bmap = isl_basic_map_simplify(bmap);
3663 bmap = isl_basic_map_finalize(bmap);
3666 isl_basic_map_free(like);
3667 isl_basic_set_free(bset);
3671 struct isl_basic_set *isl_basic_set_from_underlying_set(
3672 struct isl_basic_set *bset, struct isl_basic_set *like)
3674 return (struct isl_basic_set *)
3675 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3678 struct isl_set *isl_set_from_underlying_set(
3679 struct isl_set *set, struct isl_basic_set *like)
3685 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3687 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3688 isl_basic_set_free(like);
3691 set = isl_set_cow(set);
3694 for (i = 0; i < set->n; ++i) {
3695 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3696 isl_basic_set_copy(like));
3700 isl_dim_free(set->dim);
3701 set->dim = isl_dim_copy(like->dim);
3704 isl_basic_set_free(like);
3707 isl_basic_set_free(like);
3712 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3716 map = isl_map_cow(map);
3719 map->dim = isl_dim_cow(map->dim);
3723 for (i = 1; i < map->n; ++i)
3724 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3726 for (i = 0; i < map->n; ++i) {
3727 map->p[i] = (struct isl_basic_map *)
3728 isl_basic_map_underlying_set(map->p[i]);
3733 map->dim = isl_dim_underlying(map->dim, 0);
3735 isl_dim_free(map->dim);
3736 map->dim = isl_dim_copy(map->p[0]->dim);
3740 return (struct isl_set *)map;
3746 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3748 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3751 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3752 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3754 bmap = isl_basic_map_cow(bmap);
3758 isl_dim_free(bmap->dim);
3761 bmap = isl_basic_map_finalize(bmap);
3765 isl_basic_map_free(bmap);
3770 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3771 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3773 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3777 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3778 __isl_take isl_dim *dim)
3782 map = isl_map_cow(map);
3786 for (i = 0; i < map->n; ++i) {
3787 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3792 isl_dim_free(map->dim);
3802 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3803 __isl_take isl_dim *dim)
3805 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3808 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3811 struct isl_basic_set *domain;
3817 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3819 n_in = isl_basic_map_n_in(bmap);
3820 n_out = isl_basic_map_n_out(bmap);
3821 domain = isl_basic_set_from_basic_map(bmap);
3822 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3824 domain = isl_basic_set_reset_dim(domain, dim);
3829 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
3833 return isl_dim_may_be_set(bmap->dim);
3836 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3840 if (isl_basic_map_may_be_set(bmap))
3842 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3845 __isl_give isl_basic_map *isl_basic_map_domain_map(
3846 __isl_take isl_basic_map *bmap)
3850 isl_basic_map *domain;
3851 int nparam, n_in, n_out;
3854 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3855 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3856 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3858 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3859 domain = isl_basic_map_universe(dim);
3861 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3862 bmap = isl_basic_map_apply_range(bmap, domain);
3863 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3865 total = isl_basic_map_total_dim(bmap);
3867 for (i = 0; i < n_in; ++i) {
3868 k = isl_basic_map_alloc_equality(bmap);
3871 isl_seq_clr(bmap->eq[k], 1 + total);
3872 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3873 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3876 bmap = isl_basic_map_gauss(bmap, NULL);
3877 return isl_basic_map_finalize(bmap);
3879 isl_basic_map_free(bmap);
3883 __isl_give isl_basic_map *isl_basic_map_range_map(
3884 __isl_take isl_basic_map *bmap)
3888 isl_basic_map *range;
3889 int nparam, n_in, n_out;
3892 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3893 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3894 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3896 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3897 range = isl_basic_map_universe(dim);
3899 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3900 bmap = isl_basic_map_apply_range(bmap, range);
3901 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3903 total = isl_basic_map_total_dim(bmap);
3905 for (i = 0; i < n_out; ++i) {
3906 k = isl_basic_map_alloc_equality(bmap);
3909 isl_seq_clr(bmap->eq[k], 1 + total);
3910 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3911 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3914 bmap = isl_basic_map_gauss(bmap, NULL);
3915 return isl_basic_map_finalize(bmap);
3917 isl_basic_map_free(bmap);
3921 int isl_map_may_be_set(__isl_keep isl_map *map)
3925 return isl_dim_may_be_set(map->dim);
3928 struct isl_set *isl_map_range(struct isl_map *map)
3931 struct isl_set *set;
3935 if (isl_map_may_be_set(map))
3936 return (isl_set *)map;
3938 map = isl_map_cow(map);
3942 set = (struct isl_set *) map;
3943 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3946 for (i = 0; i < map->n; ++i) {
3947 set->p[i] = isl_basic_map_range(map->p[i]);
3951 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3952 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3959 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3962 isl_dim *domain_dim;
3964 map = isl_map_cow(map);
3968 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3969 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3970 map->dim = isl_dim_join(map->dim, domain_dim);
3973 for (i = 0; i < map->n; ++i) {
3974 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3978 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3979 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3986 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3991 map = isl_map_cow(map);
3995 range_dim = isl_dim_range(isl_map_get_dim(map));
3996 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3997 map->dim = isl_dim_join(map->dim, range_dim);
4000 for (i = 0; i < map->n; ++i) {
4001 map->p[i] = isl_basic_map_range_map(map->p[i]);
4005 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4006 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4013 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4016 struct isl_map *map = NULL;
4018 set = isl_set_cow(set);
4021 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4022 map = (struct isl_map *)set;
4023 for (i = 0; i < set->n; ++i) {
4024 map->p[i] = isl_basic_map_from_basic_set(
4025 set->p[i], isl_dim_copy(dim));
4029 isl_dim_free(map->dim);
4038 __isl_give isl_basic_map *isl_basic_map_from_domain(
4039 __isl_take isl_basic_set *bset)
4041 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4044 __isl_give isl_basic_map *isl_basic_map_from_range(
4045 __isl_take isl_basic_set *bset)
4047 return (isl_basic_map *)bset;
4050 struct isl_map *isl_map_from_range(struct isl_set *set)
4052 return (struct isl_map *)set;
4055 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4057 return isl_map_reverse(isl_map_from_range(set));
4060 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4061 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4063 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4064 isl_basic_map_from_range(range));
4067 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4068 __isl_take isl_set *range)
4070 return isl_map_apply_range(isl_map_from_domain(domain),
4071 isl_map_from_range(range));
4074 struct isl_set *isl_set_from_map(struct isl_map *map)
4077 struct isl_set *set = NULL;
4081 map = isl_map_cow(map);
4084 map->dim = isl_dim_as_set_dim(map->dim);
4087 set = (struct isl_set *)map;
4088 for (i = 0; i < map->n; ++i) {
4089 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4099 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4101 struct isl_map *map;
4105 isl_assert(dim->ctx, n >= 0, return NULL);
4106 map = isl_alloc(dim->ctx, struct isl_map,
4107 sizeof(struct isl_map) +
4108 (n - 1) * sizeof(struct isl_basic_map *));
4112 map->ctx = dim->ctx;
4113 isl_ctx_ref(map->ctx);
4125 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4126 unsigned nparam, unsigned in, unsigned out, int n,
4129 struct isl_map *map;
4130 struct isl_dim *dims;
4132 dims = isl_dim_alloc(ctx, nparam, in, out);
4136 map = isl_map_alloc_dim(dims, n, flags);
4140 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4142 struct isl_basic_map *bmap;
4143 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4144 bmap = isl_basic_map_set_to_empty(bmap);
4148 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4150 struct isl_basic_set *bset;
4151 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4152 bset = isl_basic_set_set_to_empty(bset);
4156 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4158 struct isl_basic_map *bmap;
4161 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4162 bmap = isl_basic_map_set_to_empty(bmap);
4166 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4168 struct isl_basic_map *bmap;
4171 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4172 bmap = isl_basic_map_set_to_empty(bmap);
4176 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4178 struct isl_basic_set *bset;
4181 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4182 bset = isl_basic_set_set_to_empty(bset);
4186 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4188 struct isl_basic_map *bmap;
4189 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4190 bmap = isl_basic_map_finalize(bmap);
4194 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4196 struct isl_basic_set *bset;
4197 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4198 bset = isl_basic_set_finalize(bset);
4202 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4205 unsigned total = isl_dim_total(dim);
4206 isl_basic_map *bmap;
4208 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4209 for (i = 0; i < total; ++i) {
4210 int k = isl_basic_map_alloc_inequality(bmap);
4213 isl_seq_clr(bmap->ineq[k], 1 + total);
4214 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4218 isl_basic_map_free(bmap);
4222 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4224 return isl_basic_map_nat_universe(dim);
4227 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4229 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4232 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4234 return isl_map_nat_universe(dim);
4237 __isl_give isl_basic_map *isl_basic_map_universe_like(
4238 __isl_keep isl_basic_map *model)
4242 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4245 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4249 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4252 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4253 __isl_keep isl_set *model)
4257 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4260 struct isl_map *isl_map_empty(struct isl_dim *dim)
4262 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4265 struct isl_map *isl_map_empty_like(struct isl_map *model)
4269 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4272 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4276 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4279 struct isl_set *isl_set_empty(struct isl_dim *dim)
4281 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4284 struct isl_set *isl_set_empty_like(struct isl_set *model)
4288 return isl_set_empty(isl_dim_copy(model->dim));
4291 struct isl_map *isl_map_universe(struct isl_dim *dim)
4293 struct isl_map *map;
4296 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4297 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4301 struct isl_set *isl_set_universe(struct isl_dim *dim)
4303 struct isl_set *set;
4306 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4307 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4311 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4315 return isl_set_universe(isl_dim_copy(model->dim));
4318 struct isl_map *isl_map_dup(struct isl_map *map)
4321 struct isl_map *dup;
4325 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4326 for (i = 0; i < map->n; ++i)
4327 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4331 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4332 __isl_take isl_basic_map *bmap)
4336 if (isl_basic_map_plain_is_empty(bmap)) {
4337 isl_basic_map_free(bmap);
4340 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4341 isl_assert(map->ctx, map->n < map->size, goto error);
4342 map->p[map->n] = bmap;
4344 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4350 isl_basic_map_free(bmap);
4354 void isl_map_free(struct isl_map *map)
4364 isl_ctx_deref(map->ctx);
4365 for (i = 0; i < map->n; ++i)
4366 isl_basic_map_free(map->p[i]);
4367 isl_dim_free(map->dim);
4371 struct isl_map *isl_map_extend(struct isl_map *base,
4372 unsigned nparam, unsigned n_in, unsigned n_out)
4376 base = isl_map_cow(base);
4380 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4383 for (i = 0; i < base->n; ++i) {
4384 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4385 isl_dim_copy(base->dim), 0, 0, 0);
4395 struct isl_set *isl_set_extend(struct isl_set *base,
4396 unsigned nparam, unsigned dim)
4398 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4402 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4403 struct isl_basic_map *bmap, unsigned pos, int value)
4407 bmap = isl_basic_map_cow(bmap);
4408 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4409 j = isl_basic_map_alloc_equality(bmap);
4412 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4413 isl_int_set_si(bmap->eq[j][pos], -1);
4414 isl_int_set_si(bmap->eq[j][0], value);
4415 bmap = isl_basic_map_simplify(bmap);
4416 return isl_basic_map_finalize(bmap);
4418 isl_basic_map_free(bmap);
4422 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4423 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4427 bmap = isl_basic_map_cow(bmap);
4428 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4429 j = isl_basic_map_alloc_equality(bmap);
4432 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4433 isl_int_set_si(bmap->eq[j][pos], -1);
4434 isl_int_set(bmap->eq[j][0], value);
4435 bmap = isl_basic_map_simplify(bmap);
4436 return isl_basic_map_finalize(bmap);
4438 isl_basic_map_free(bmap);
4442 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4443 enum isl_dim_type type, unsigned pos, int value)
4447 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4448 return isl_basic_map_fix_pos_si(bmap,
4449 isl_basic_map_offset(bmap, type) + pos, value);
4451 isl_basic_map_free(bmap);
4455 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4456 enum isl_dim_type type, unsigned pos, isl_int value)
4460 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4461 return isl_basic_map_fix_pos(bmap,
4462 isl_basic_map_offset(bmap, type) + pos, value);
4464 isl_basic_map_free(bmap);
4468 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4469 enum isl_dim_type type, unsigned pos, int value)
4471 return (struct isl_basic_set *)
4472 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4476 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4477 enum isl_dim_type type, unsigned pos, isl_int value)
4479 return (struct isl_basic_set *)
4480 isl_basic_map_fix((struct isl_basic_map *)bset,
4484 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4485 unsigned input, int value)
4487 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4490 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4491 unsigned dim, int value)
4493 return (struct isl_basic_set *)
4494 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4495 isl_dim_set, dim, value);
4498 struct isl_map *isl_map_fix_si(struct isl_map *map,
4499 enum isl_dim_type type, unsigned pos, int value)
4503 map = isl_map_cow(map);
4507 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4508 for (i = 0; i < map->n; ++i) {
4509 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4513 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4520 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4521 enum isl_dim_type type, unsigned pos, int value)
4523 return (struct isl_set *)
4524 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4527 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4528 enum isl_dim_type type, unsigned pos, isl_int value)
4532 map = isl_map_cow(map);
4536 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4537 for (i = 0; i < map->n; ++i) {
4538 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4542 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4549 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4550 enum isl_dim_type type, unsigned pos, isl_int value)
4552 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4555 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4556 unsigned input, int value)
4558 return isl_map_fix_si(map, isl_dim_in, input, value);
4561 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4563 return (struct isl_set *)
4564 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4567 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4568 __isl_take isl_basic_map *bmap,
4569 enum isl_dim_type type, unsigned pos, int value)
4575 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4576 pos += isl_basic_map_offset(bmap, type);
4577 bmap = isl_basic_map_cow(bmap);
4578 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4579 j = isl_basic_map_alloc_inequality(bmap);
4582 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4583 isl_int_set_si(bmap->ineq[j][pos], 1);
4584 isl_int_set_si(bmap->ineq[j][0], -value);
4585 bmap = isl_basic_map_simplify(bmap);
4586 return isl_basic_map_finalize(bmap);
4588 isl_basic_map_free(bmap);
4592 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4593 unsigned dim, isl_int value)
4597 bset = isl_basic_set_cow(bset);
4598 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4599 j = isl_basic_set_alloc_inequality(bset);
4602 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4603 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4604 isl_int_neg(bset->ineq[j][0], value);
4605 bset = isl_basic_set_simplify(bset);
4606 return isl_basic_set_finalize(bset);
4608 isl_basic_set_free(bset);
4612 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4613 enum isl_dim_type type, unsigned pos, int value)
4617 map = isl_map_cow(map);
4621 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4622 for (i = 0; i < map->n; ++i) {
4623 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4628 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4635 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4636 enum isl_dim_type type, unsigned pos, int value)
4638 return (struct isl_set *)
4639 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4642 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4647 set = isl_set_cow(set);
4651 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4652 for (i = 0; i < set->n; ++i) {
4653 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4663 struct isl_map *isl_map_reverse(struct isl_map *map)
4667 map = isl_map_cow(map);
4671 map->dim = isl_dim_reverse(map->dim);
4674 for (i = 0; i < map->n; ++i) {
4675 map->p[i] = isl_basic_map_reverse(map->p[i]);
4679 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4686 static struct isl_map *isl_basic_map_partial_lexopt(
4687 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4688 struct isl_set **empty, int max)
4692 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4693 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4695 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4697 isl_basic_map_free(bmap);
4698 isl_basic_set_free(dom);
4704 struct isl_map *isl_basic_map_partial_lexmax(
4705 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4706 struct isl_set **empty)
4708 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4711 struct isl_map *isl_basic_map_partial_lexmin(
4712 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4713 struct isl_set **empty)
4715 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4718 struct isl_set *isl_basic_set_partial_lexmin(
4719 struct isl_basic_set *bset, struct isl_basic_set *dom,
4720 struct isl_set **empty)
4722 return (struct isl_set *)
4723 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4727 struct isl_set *isl_basic_set_partial_lexmax(
4728 struct isl_basic_set *bset, struct isl_basic_set *dom,
4729 struct isl_set **empty)
4731 return (struct isl_set *)
4732 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4736 /* Given a basic map "bmap", compute the lexicographically minimal
4737 * (or maximal) image element for each domain element in dom.
4738 * Set *empty to those elements in dom that do not have an image element.
4740 * We first make sure the basic sets in dom are disjoint and then
4741 * simply collect the results over each of the basic sets separately.
4742 * We could probably improve the efficiency a bit by moving the union
4743 * domain down into the parametric integer programming.
4745 static __isl_give isl_map *basic_map_partial_lexopt(
4746 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4747 __isl_give isl_set **empty, int max)
4750 struct isl_map *res;
4752 dom = isl_set_make_disjoint(dom);
4756 if (isl_set_plain_is_empty(dom)) {
4757 res = isl_map_empty_like_basic_map(bmap);
4758 *empty = isl_set_empty_like(dom);
4760 isl_basic_map_free(bmap);
4764 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4765 isl_basic_set_copy(dom->p[0]), empty, max);
4767 for (i = 1; i < dom->n; ++i) {
4768 struct isl_map *res_i;
4769 struct isl_set *empty_i;
4771 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4772 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4774 res = isl_map_union_disjoint(res, res_i);
4775 *empty = isl_set_union_disjoint(*empty, empty_i);
4779 isl_basic_map_free(bmap);
4784 isl_basic_map_free(bmap);
4788 /* Given a map "map", compute the lexicographically minimal
4789 * (or maximal) image element for each domain element in dom.
4790 * Set *empty to those elements in dom that do not have an image element.
4792 * We first compute the lexicographically minimal or maximal element
4793 * in the first basic map. This results in a partial solution "res"
4794 * and a subset "todo" of dom that still need to be handled.
4795 * We then consider each of the remaining maps in "map" and successively
4796 * improve both "res" and "todo".
4798 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4799 * Assume we are computing the lexicographical maximum.
4800 * We first compute the lexicographically maximal element in basic map i.
4801 * This results in a partial solution res_i and a subset todo_i.
4802 * Then we combine these results with those obtain for the first k basic maps
4803 * to obtain a result that is valid for the first k+1 basic maps.
4804 * In particular, the set where there is no solution is the set where
4805 * there is no solution for the first k basic maps and also no solution
4806 * for the ith basic map, i.e.,
4808 * todo^i = todo^k * todo_i
4810 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4811 * solutions, arbitrarily breaking ties in favor of res^k.
4812 * That is, when res^k(a) >= res_i(a), we pick res^k and
4813 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4814 * the lexicographic order.)
4815 * In practice, we compute
4817 * res^k * (res_i . "<=")
4821 * res_i * (res^k . "<")
4823 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4824 * where only one of res^k and res_i provides a solution and we simply pick
4831 * Note that we only compute these intersections when dom(res^k) intersects
4832 * dom(res_i). Otherwise, the only effect of these intersections is to
4833 * potentially break up res^k and res_i into smaller pieces.
4834 * We want to avoid such splintering as much as possible.
4835 * In fact, an earlier implementation of this function would look for
4836 * better results in the domain of res^k and for extra results in todo^k,
4837 * but this would always result in a splintering according to todo^k,
4838 * even when the domain of basic map i is disjoint from the domains of
4839 * the previous basic maps.
4841 static __isl_give isl_map *isl_map_partial_lexopt(
4842 __isl_take isl_map *map, __isl_take isl_set *dom,
4843 __isl_give isl_set **empty, int max)
4846 struct isl_map *res;
4847 struct isl_set *todo;
4852 if (isl_map_plain_is_empty(map)) {
4860 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4861 isl_set_copy(dom), &todo, max);
4863 for (i = 1; i < map->n; ++i) {
4867 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4869 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4870 isl_set_copy(dom), &todo_i, max);
4873 lt = isl_map_lex_lt(isl_dim_copy(dim));
4874 le = isl_map_lex_le(dim);
4876 lt = isl_map_lex_gt(isl_dim_copy(dim));
4877 le = isl_map_lex_ge(dim);
4879 lt = isl_map_apply_range(isl_map_copy(res), lt);
4880 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4881 le = isl_map_apply_range(isl_map_copy(res_i), le);
4882 le = isl_map_intersect(le, isl_map_copy(res));
4884 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4885 res = isl_map_intersect_domain(res,
4886 isl_set_copy(todo_i));
4887 res_i = isl_map_intersect_domain(res_i,
4888 isl_set_copy(todo));
4891 res = isl_map_union_disjoint(res, res_i);
4892 res = isl_map_union_disjoint(res, lt);
4893 res = isl_map_union_disjoint(res, le);
4895 todo = isl_set_intersect(todo, todo_i);
4915 __isl_give isl_map *isl_map_partial_lexmax(
4916 __isl_take isl_map *map, __isl_take isl_set *dom,
4917 __isl_give isl_set **empty)
4919 return isl_map_partial_lexopt(map, dom, empty, 1);
4922 __isl_give isl_map *isl_map_partial_lexmin(
4923 __isl_take isl_map *map, __isl_take isl_set *dom,
4924 __isl_give isl_set **empty)
4926 return isl_map_partial_lexopt(map, dom, empty, 0);
4929 __isl_give isl_set *isl_set_partial_lexmin(
4930 __isl_take isl_set *set, __isl_take isl_set *dom,
4931 __isl_give isl_set **empty)
4933 return (struct isl_set *)
4934 isl_map_partial_lexmin((struct isl_map *)set,
4938 __isl_give isl_set *isl_set_partial_lexmax(
4939 __isl_take isl_set *set, __isl_take isl_set *dom,
4940 __isl_give isl_set **empty)
4942 return (struct isl_set *)
4943 isl_map_partial_lexmax((struct isl_map *)set,
4947 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4949 struct isl_basic_set *dom = NULL;
4950 struct isl_dim *dom_dim;
4954 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4955 dom = isl_basic_set_universe(dom_dim);
4956 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4958 isl_basic_map_free(bmap);
4962 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4964 return isl_basic_map_lexopt(bmap, 0);
4967 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4969 return isl_basic_map_lexopt(bmap, 1);
4972 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4974 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4977 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4979 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4982 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4984 struct isl_set *dom = NULL;
4985 struct isl_dim *dom_dim;
4989 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4990 dom = isl_set_universe(dom_dim);
4991 return isl_map_partial_lexopt(map, dom, NULL, max);
4997 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4999 return isl_map_lexopt(map, 0);
5002 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5004 return isl_map_lexopt(map, 1);
5007 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5009 return (isl_set *)isl_map_lexmin((isl_map *)set);
5012 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5014 return (isl_set *)isl_map_lexmax((isl_map *)set);
5017 /* Apply a preimage specified by "mat" on the parameters of "bset".
5018 * bset is assumed to have only parameters and divs.
5020 static struct isl_basic_set *basic_set_parameter_preimage(
5021 struct isl_basic_set *bset, struct isl_mat *mat)
5028 bset->dim = isl_dim_cow(bset->dim);
5032 nparam = isl_basic_set_dim(bset, isl_dim_param);
5034 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5036 bset->dim->nparam = 0;
5037 bset->dim->n_out = nparam;
5038 bset = isl_basic_set_preimage(bset, mat);
5040 bset->dim->nparam = bset->dim->n_out;
5041 bset->dim->n_out = 0;
5046 isl_basic_set_free(bset);
5050 /* Apply a preimage specified by "mat" on the parameters of "set".
5051 * set is assumed to have only parameters and divs.
5053 static struct isl_set *set_parameter_preimage(
5054 struct isl_set *set, struct isl_mat *mat)
5056 struct isl_dim *dim = NULL;
5062 dim = isl_dim_copy(set->dim);
5063 dim = isl_dim_cow(dim);
5067 nparam = isl_set_dim(set, isl_dim_param);
5069 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5072 dim->n_out = nparam;
5073 isl_set_reset_dim(set, dim);
5074 set = isl_set_preimage(set, mat);
5077 dim = isl_dim_copy(set->dim);
5078 dim = isl_dim_cow(dim);
5081 dim->nparam = dim->n_out;
5083 isl_set_reset_dim(set, dim);
5093 /* Intersect the basic set "bset" with the affine space specified by the
5094 * equalities in "eq".
5096 static struct isl_basic_set *basic_set_append_equalities(
5097 struct isl_basic_set *bset, struct isl_mat *eq)
5105 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5110 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5111 for (i = 0; i < eq->n_row; ++i) {
5112 k = isl_basic_set_alloc_equality(bset);
5115 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5116 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5120 bset = isl_basic_set_gauss(bset, NULL);
5121 bset = isl_basic_set_finalize(bset);
5126 isl_basic_set_free(bset);
5130 /* Intersect the set "set" with the affine space specified by the
5131 * equalities in "eq".
5133 static struct isl_set *set_append_equalities(struct isl_set *set,
5141 for (i = 0; i < set->n; ++i) {
5142 set->p[i] = basic_set_append_equalities(set->p[i],
5155 /* Project the given basic set onto its parameter domain, possibly introducing
5156 * new, explicit, existential variables in the constraints.
5157 * The input has parameters and (possibly implicit) existential variables.
5158 * The output has the same parameters, but only
5159 * explicit existentially quantified variables.
5161 * The actual projection is performed by pip, but pip doesn't seem
5162 * to like equalities very much, so we first remove the equalities
5163 * among the parameters by performing a variable compression on
5164 * the parameters. Afterward, an inverse transformation is performed
5165 * and the equalities among the parameters are inserted back in.
5167 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5171 struct isl_mat *T, *T2;
5172 struct isl_set *set;
5173 unsigned nparam, n_div;
5175 bset = isl_basic_set_cow(bset);
5179 if (bset->n_eq == 0)
5180 return isl_basic_set_lexmin(bset);
5182 isl_basic_set_gauss(bset, NULL);
5184 nparam = isl_basic_set_dim(bset, isl_dim_param);
5185 n_div = isl_basic_set_dim(bset, isl_dim_div);
5187 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5188 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5191 if (i == bset->n_eq)
5192 return isl_basic_set_lexmin(bset);
5194 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5196 eq = isl_mat_cow(eq);
5197 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5198 if (T && T->n_col == 0) {
5202 bset = isl_basic_set_set_to_empty(bset);
5203 return isl_set_from_basic_set(bset);
5205 bset = basic_set_parameter_preimage(bset, T);
5207 set = isl_basic_set_lexmin(bset);
5208 set = set_parameter_preimage(set, T2);
5209 set = set_append_equalities(set, eq);
5213 /* Compute an explicit representation for all the existentially
5214 * quantified variables.
5215 * The input and output dimensions are first turned into parameters.
5216 * compute_divs then returns a map with the same parameters and
5217 * no input or output dimensions and the dimension specification
5218 * is reset to that of the input.
5220 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5222 struct isl_basic_set *bset;
5223 struct isl_set *set;
5224 struct isl_map *map;
5225 struct isl_dim *dim, *orig_dim = NULL;
5230 bmap = isl_basic_map_cow(bmap);
5234 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5235 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5236 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5237 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5241 orig_dim = bmap->dim;
5243 bset = (struct isl_basic_set *)bmap;
5245 set = parameter_compute_divs(bset);
5246 map = (struct isl_map *)set;
5247 map = isl_map_reset_dim(map, orig_dim);
5251 isl_basic_map_free(bmap);
5255 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5263 off = isl_dim_total(bmap->dim);
5264 for (i = 0; i < bmap->n_div; ++i) {
5265 if (isl_int_is_zero(bmap->div[i][0]))
5267 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5273 static int map_divs_known(__isl_keep isl_map *map)
5280 for (i = 0; i < map->n; ++i) {
5281 int known = basic_map_divs_known(map->p[i]);
5289 /* If bmap contains any unknown divs, then compute explicit
5290 * expressions for them. However, this computation may be
5291 * quite expensive, so first try to remove divs that aren't
5294 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5297 struct isl_map *map;
5299 known = basic_map_divs_known(bmap);
5303 return isl_map_from_basic_map(bmap);
5305 bmap = isl_basic_map_drop_redundant_divs(bmap);
5307 known = basic_map_divs_known(bmap);
5311 return isl_map_from_basic_map(bmap);
5313 map = compute_divs(bmap);
5316 isl_basic_map_free(bmap);
5320 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5324 struct isl_map *res;
5331 known = map_divs_known(map);
5339 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5340 for (i = 1 ; i < map->n; ++i) {
5342 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5343 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5344 res = isl_map_union_disjoint(res, r2);
5346 res = isl_map_union(res, r2);
5353 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5355 return (struct isl_set *)
5356 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5359 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5361 return (struct isl_set *)
5362 isl_map_compute_divs((struct isl_map *)set);
5365 struct isl_set *isl_map_domain(struct isl_map *map)
5368 struct isl_set *set;
5373 map = isl_map_cow(map);
5377 set = (struct isl_set *)map;
5378 set->dim = isl_dim_domain(set->dim);
5381 for (i = 0; i < map->n; ++i) {
5382 set->p[i] = isl_basic_map_domain(map->p[i]);
5386 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5387 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5394 struct isl_map *isl_map_union_disjoint(
5395 struct isl_map *map1, struct isl_map *map2)
5399 struct isl_map *map = NULL;
5413 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5415 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5416 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5417 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5419 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5420 map1->n + map2->n, flags);
5423 for (i = 0; i < map1->n; ++i) {
5424 map = isl_map_add_basic_map(map,
5425 isl_basic_map_copy(map1->p[i]));
5429 for (i = 0; i < map2->n; ++i) {
5430 map = isl_map_add_basic_map(map,
5431 isl_basic_map_copy(map2->p[i]));
5445 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5447 map1 = isl_map_union_disjoint(map1, map2);
5451 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5455 struct isl_set *isl_set_union_disjoint(
5456 struct isl_set *set1, struct isl_set *set2)
5458 return (struct isl_set *)
5459 isl_map_union_disjoint(
5460 (struct isl_map *)set1, (struct isl_map *)set2);
5463 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5465 return (struct isl_set *)
5466 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5469 struct isl_map *isl_map_intersect_range(
5470 struct isl_map *map, struct isl_set *set)
5473 struct isl_map *result;
5479 if (!isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5480 isl_die(set->ctx, isl_error_invalid,
5481 "parameters don't match", goto error);
5483 if (isl_dim_size(set->dim, isl_dim_set) != 0 &&
5484 !isl_map_compatible_range(map, set))
5485 isl_die(set->ctx, isl_error_invalid,
5486 "incompatible spaces", goto error);
5488 if (isl_set_plain_is_universe(set)) {
5493 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5494 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5495 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5497 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5498 map->n * set->n, flags);
5501 for (i = 0; i < map->n; ++i)
5502 for (j = 0; j < set->n; ++j) {
5503 result = isl_map_add_basic_map(result,
5504 isl_basic_map_intersect_range(
5505 isl_basic_map_copy(map->p[i]),
5506 isl_basic_set_copy(set->p[j])));
5519 struct isl_map *isl_map_intersect_domain(
5520 struct isl_map *map, struct isl_set *set)
5522 return isl_map_reverse(
5523 isl_map_intersect_range(isl_map_reverse(map), set));
5526 struct isl_map *isl_map_apply_domain(
5527 struct isl_map *map1, struct isl_map *map2)
5531 map1 = isl_map_reverse(map1);
5532 map1 = isl_map_apply_range(map1, map2);
5533 return isl_map_reverse(map1);
5540 struct isl_map *isl_map_apply_range(
5541 struct isl_map *map1, struct isl_map *map2)
5543 struct isl_dim *dim_result;
5544 struct isl_map *result;
5550 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5551 isl_dim_copy(map2->dim));
5553 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5556 for (i = 0; i < map1->n; ++i)
5557 for (j = 0; j < map2->n; ++j) {
5558 result = isl_map_add_basic_map(result,
5559 isl_basic_map_apply_range(
5560 isl_basic_map_copy(map1->p[i]),
5561 isl_basic_map_copy(map2->p[j])));
5567 if (result && result->n <= 1)
5568 ISL_F_SET(result, ISL_MAP_DISJOINT);
5577 * returns range - domain
5579 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5581 isl_dim *dims, *target_dim;
5582 struct isl_basic_set *bset;
5589 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5590 bmap->dim, isl_dim_out),
5592 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5593 dim = isl_basic_map_n_in(bmap);
5594 nparam = isl_basic_map_n_param(bmap);
5595 bset = isl_basic_set_from_basic_map(bmap);
5596 bset = isl_basic_set_cow(bset);
5597 dims = isl_basic_set_get_dim(bset);
5598 dims = isl_dim_add(dims, isl_dim_set, dim);
5599 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5600 bset = isl_basic_set_swap_vars(bset, 2*dim);
5601 for (i = 0; i < dim; ++i) {
5602 int j = isl_basic_map_alloc_equality(
5603 (struct isl_basic_map *)bset);
5606 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5607 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5608 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5609 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5611 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5612 bset = isl_basic_set_reset_dim(bset, target_dim);
5615 isl_basic_map_free(bmap);
5620 * returns range - domain
5622 struct isl_set *isl_map_deltas(struct isl_map *map)
5626 struct isl_set *result;
5631 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5632 map->dim, isl_dim_out),
5634 dim = isl_map_get_dim(map);
5635 dim = isl_dim_domain(dim);
5636 result = isl_set_alloc_dim(dim, map->n, 0);
5639 for (i = 0; i < map->n; ++i)
5640 result = isl_set_add_basic_set(result,
5641 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5650 * returns [domain -> range] -> range - domain
5652 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5653 __isl_take isl_basic_map *bmap)
5657 isl_basic_map *domain;
5661 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5662 isl_die(bmap->ctx, isl_error_invalid,
5663 "domain and range don't match", goto error);
5665 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5666 n = isl_basic_map_dim(bmap, isl_dim_in);
5668 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5669 domain = isl_basic_map_universe(dim);
5671 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5672 bmap = isl_basic_map_apply_range(bmap, domain);
5673 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5675 total = isl_basic_map_total_dim(bmap);
5677 for (i = 0; i < n; ++i) {
5678 k = isl_basic_map_alloc_equality(bmap);
5681 isl_seq_clr(bmap->eq[k], 1 + total);
5682 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5683 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5684 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5687 bmap = isl_basic_map_gauss(bmap, NULL);
5688 return isl_basic_map_finalize(bmap);
5690 isl_basic_map_free(bmap);
5695 * returns [domain -> range] -> range - domain
5697 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5700 isl_dim *domain_dim;
5705 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5706 isl_die(map->ctx, isl_error_invalid,
5707 "domain and range don't match", goto error);
5709 map = isl_map_cow(map);
5713 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5714 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5715 map->dim = isl_dim_join(map->dim, domain_dim);
5718 for (i = 0; i < map->n; ++i) {
5719 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5723 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5730 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5732 struct isl_basic_map *bmap;
5740 nparam = dims->nparam;
5742 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5746 for (i = 0; i < dim; ++i) {
5747 int j = isl_basic_map_alloc_equality(bmap);
5750 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5751 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5752 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5754 return isl_basic_map_finalize(bmap);
5756 isl_basic_map_free(bmap);
5760 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5764 if (dim->n_in != dim->n_out)
5765 isl_die(dim->ctx, isl_error_invalid,
5766 "number of input and output dimensions needs to be "
5767 "the same", goto error);
5768 return basic_map_identity(dim);
5774 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5776 if (!model || !model->dim)
5778 return isl_basic_map_identity(isl_dim_copy(model->dim));
5781 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5783 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5786 struct isl_map *isl_map_identity_like(struct isl_map *model)
5788 if (!model || !model->dim)
5790 return isl_map_identity(isl_dim_copy(model->dim));
5793 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5795 if (!model || !model->dim)
5797 return isl_map_identity(isl_dim_copy(model->dim));
5800 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5802 isl_dim *dim = isl_set_get_dim(set);
5804 id = isl_map_identity(isl_dim_map_from_set(dim));
5805 return isl_map_intersect_range(id, set);
5808 /* Construct a basic set with all set dimensions having only non-negative
5811 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5816 struct isl_basic_set *bset;
5820 nparam = dims->nparam;
5822 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5825 for (i = 0; i < dim; ++i) {
5826 int k = isl_basic_set_alloc_inequality(bset);
5829 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5830 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5834 isl_basic_set_free(bset);
5838 /* Construct the half-space x_pos >= 0.
5840 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5844 isl_basic_set *nonneg;
5846 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5847 k = isl_basic_set_alloc_inequality(nonneg);
5850 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5851 isl_int_set_si(nonneg->ineq[k][pos], 1);
5853 return isl_basic_set_finalize(nonneg);
5855 isl_basic_set_free(nonneg);
5859 /* Construct the half-space x_pos <= -1.
5861 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5866 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5867 k = isl_basic_set_alloc_inequality(neg);
5870 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5871 isl_int_set_si(neg->ineq[k][0], -1);
5872 isl_int_set_si(neg->ineq[k][pos], -1);
5874 return isl_basic_set_finalize(neg);
5876 isl_basic_set_free(neg);
5880 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5881 enum isl_dim_type type, unsigned first, unsigned n)
5884 isl_basic_set *nonneg;
5892 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5894 for (i = 0; i < n; ++i) {
5895 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5896 pos(set->dim, type) + first + i);
5897 neg = neg_halfspace(isl_set_get_dim(set),
5898 pos(set->dim, type) + first + i);
5900 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5909 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5910 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5917 if (isl_set_plain_is_empty(set)) {
5922 return fn(set, signs, user);
5925 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5927 half = isl_set_intersect(half, isl_set_copy(set));
5928 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5932 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5934 half = isl_set_intersect(half, set);
5935 return foreach_orthant(half, signs, first + 1, len, fn, user);
5941 /* Call "fn" on the intersections of "set" with each of the orthants
5942 * (except for obviously empty intersections). The orthant is identified
5943 * by the signs array, with each entry having value 1 or -1 according
5944 * to the sign of the corresponding variable.
5946 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5947 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5957 if (isl_set_plain_is_empty(set))
5960 nparam = isl_set_dim(set, isl_dim_param);
5961 nvar = isl_set_dim(set, isl_dim_set);
5963 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5965 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5973 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5975 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5978 int isl_basic_map_is_subset(
5979 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5982 struct isl_map *map1;
5983 struct isl_map *map2;
5985 if (!bmap1 || !bmap2)
5988 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5989 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5991 is_subset = isl_map_is_subset(map1, map2);
5999 int isl_basic_map_is_equal(
6000 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6004 if (!bmap1 || !bmap2)
6006 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6009 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6013 int isl_basic_set_is_equal(
6014 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6016 return isl_basic_map_is_equal(
6017 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6020 int isl_map_is_empty(struct isl_map *map)
6027 for (i = 0; i < map->n; ++i) {
6028 is_empty = isl_basic_map_is_empty(map->p[i]);
6037 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6039 return map ? map->n == 0 : -1;
6042 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6044 return isl_map_plain_is_empty(map);
6047 int isl_set_plain_is_empty(struct isl_set *set)
6049 return set ? set->n == 0 : -1;
6052 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6054 return isl_set_plain_is_empty(set);
6057 int isl_set_is_empty(struct isl_set *set)
6059 return isl_map_is_empty((struct isl_map *)set);
6062 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6067 return isl_dim_equal(map1->dim, map2->dim);
6070 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6075 return isl_dim_equal(set1->dim, set2->dim);
6078 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6084 is_subset = isl_map_is_subset(map1, map2);
6087 is_subset = isl_map_is_subset(map2, map1);
6091 int isl_basic_map_is_strict_subset(
6092 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6096 if (!bmap1 || !bmap2)
6098 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6101 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6102 if (is_subset == -1)
6107 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6113 is_subset = isl_map_is_subset(map1, map2);
6116 is_subset = isl_map_is_subset(map2, map1);
6117 if (is_subset == -1)
6122 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6124 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6127 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6131 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6134 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6138 return bset->n_eq == 0 && bset->n_ineq == 0;
6141 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6148 for (i = 0; i < map->n; ++i) {
6149 int r = isl_basic_map_is_universe(map->p[i]);
6157 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6159 return isl_map_plain_is_universe((isl_map *) set);
6162 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6164 return isl_set_plain_is_universe(set);
6167 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6169 struct isl_basic_set *bset = NULL;
6170 struct isl_vec *sample = NULL;
6177 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6180 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6181 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6182 copy = isl_basic_map_remove_redundancies(copy);
6183 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6184 isl_basic_map_free(copy);
6188 total = 1 + isl_basic_map_total_dim(bmap);
6189 if (bmap->sample && bmap->sample->size == total) {
6190 int contains = isl_basic_map_contains(bmap, bmap->sample);
6196 isl_vec_free(bmap->sample);
6197 bmap->sample = NULL;
6198 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6201 sample = isl_basic_set_sample_vec(bset);
6204 empty = sample->size == 0;
6205 isl_vec_free(bmap->sample);
6206 bmap->sample = sample;
6208 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6213 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6217 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6220 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6222 return isl_basic_map_plain_is_empty(bmap);
6225 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6229 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6232 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6234 return isl_basic_set_plain_is_empty(bset);
6237 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6239 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6242 struct isl_map *isl_basic_map_union(
6243 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6245 struct isl_map *map;
6246 if (!bmap1 || !bmap2)
6249 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6251 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6254 map = isl_map_add_basic_map(map, bmap1);
6255 map = isl_map_add_basic_map(map, bmap2);
6258 isl_basic_map_free(bmap1);
6259 isl_basic_map_free(bmap2);
6263 struct isl_set *isl_basic_set_union(
6264 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6266 return (struct isl_set *)isl_basic_map_union(
6267 (struct isl_basic_map *)bset1,
6268 (struct isl_basic_map *)bset2);
6271 /* Order divs such that any div only depends on previous divs */
6272 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6280 off = isl_dim_total(bmap->dim);
6282 for (i = 0; i < bmap->n_div; ++i) {
6284 if (isl_int_is_zero(bmap->div[i][0]))
6286 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6290 isl_basic_map_swap_div(bmap, i, i + pos);
6296 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6298 return (struct isl_basic_set *)
6299 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6302 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6309 for (i = 0; i < map->n; ++i) {
6310 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6321 /* Apply the expansion computed by isl_merge_divs.
6322 * The expansion itself is given by "exp" while the resulting
6323 * list of divs is given by "div".
6325 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6326 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6331 bset = isl_basic_set_cow(bset);
6335 if (div->n_row < bset->n_div)
6336 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6337 "not an expansion", goto error);
6339 bset = isl_basic_map_extend_dim(bset, isl_dim_copy(bset->dim),
6340 div->n_row - bset->n_div, 0,
6341 2 * (div->n_row - bset->n_div));
6343 n_div = bset->n_div;
6344 for (i = n_div; i < div->n_row; ++i)
6345 if (isl_basic_set_alloc_div(bset) < 0)
6349 for (i = div->n_row - 1; i >= 0; --i) {
6350 if (j >= 0 && exp[j] == i) {
6352 isl_basic_map_swap_div(bset, i, j);
6355 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6356 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6364 isl_basic_set_free(bset);
6369 /* Look for a div in dst that corresponds to the div "div" in src.
6370 * The divs before "div" in src and dst are assumed to be the same.
6372 * Returns -1 if no corresponding div was found and the position
6373 * of the corresponding div in dst otherwise.
6375 static int find_div(struct isl_basic_map *dst,
6376 struct isl_basic_map *src, unsigned div)
6380 unsigned total = isl_dim_total(src->dim);
6382 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6383 for (i = div; i < dst->n_div; ++i)
6384 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6385 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6386 dst->n_div - div) == -1)
6391 struct isl_basic_map *isl_basic_map_align_divs(
6392 struct isl_basic_map *dst, struct isl_basic_map *src)
6395 unsigned total = isl_dim_total(src->dim);
6400 if (src->n_div == 0)
6403 for (i = 0; i < src->n_div; ++i)
6404 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6406 src = isl_basic_map_order_divs(src);
6407 dst = isl_basic_map_cow(dst);
6408 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6409 src->n_div, 0, 2 * src->n_div);
6412 for (i = 0; i < src->n_div; ++i) {
6413 int j = find_div(dst, src, i);
6415 j = isl_basic_map_alloc_div(dst);
6418 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6419 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6420 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6424 isl_basic_map_swap_div(dst, i, j);
6428 isl_basic_map_free(dst);
6432 struct isl_basic_set *isl_basic_set_align_divs(
6433 struct isl_basic_set *dst, struct isl_basic_set *src)
6435 return (struct isl_basic_set *)isl_basic_map_align_divs(
6436 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6439 struct isl_map *isl_map_align_divs(struct isl_map *map)
6447 map = isl_map_compute_divs(map);
6448 map = isl_map_cow(map);
6452 for (i = 1; i < map->n; ++i)
6453 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6454 for (i = 1; i < map->n; ++i)
6455 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6457 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6461 struct isl_set *isl_set_align_divs(struct isl_set *set)
6463 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6466 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6470 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6471 map = isl_map_intersect_domain(map, set);
6472 set = isl_map_range(map);
6480 /* There is no need to cow as removing empty parts doesn't change
6481 * the meaning of the set.
6483 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6490 for (i = map->n-1; i >= 0; --i) {
6491 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6493 isl_basic_map_free(map->p[i]);
6494 if (i != map->n-1) {
6495 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6496 map->p[i] = map->p[map->n-1];
6504 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6506 return (struct isl_set *)
6507 isl_map_remove_empty_parts((struct isl_map *)set);
6510 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6512 struct isl_basic_map *bmap;
6513 if (!map || map->n == 0)
6515 bmap = map->p[map->n-1];
6516 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6517 return isl_basic_map_copy(bmap);
6520 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6522 return (struct isl_basic_set *)
6523 isl_map_copy_basic_map((struct isl_map *)set);
6526 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6527 __isl_keep isl_basic_map *bmap)
6533 for (i = map->n-1; i >= 0; --i) {
6534 if (map->p[i] != bmap)
6536 map = isl_map_cow(map);
6539 isl_basic_map_free(map->p[i]);
6540 if (i != map->n-1) {
6541 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6542 map->p[i] = map->p[map->n-1];
6553 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6554 struct isl_basic_set *bset)
6556 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6557 (struct isl_basic_map *)bset);
6560 /* Given two basic sets bset1 and bset2, compute the maximal difference
6561 * between the values of dimension pos in bset1 and those in bset2
6562 * for any common value of the parameters and dimensions preceding pos.
6564 static enum isl_lp_result basic_set_maximal_difference_at(
6565 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6566 int pos, isl_int *opt)
6568 struct isl_dim *dims;
6569 struct isl_basic_map *bmap1 = NULL;
6570 struct isl_basic_map *bmap2 = NULL;
6571 struct isl_ctx *ctx;
6572 struct isl_vec *obj;
6575 unsigned dim1, dim2;
6576 enum isl_lp_result res;
6578 if (!bset1 || !bset2)
6579 return isl_lp_error;
6581 nparam = isl_basic_set_n_param(bset1);
6582 dim1 = isl_basic_set_n_dim(bset1);
6583 dim2 = isl_basic_set_n_dim(bset2);
6584 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6585 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6586 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6587 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6588 if (!bmap1 || !bmap2)
6590 bmap1 = isl_basic_map_cow(bmap1);
6591 bmap1 = isl_basic_map_extend(bmap1, nparam,
6592 pos, (dim1 - pos) + (dim2 - pos),
6593 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6594 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6597 total = isl_basic_map_total_dim(bmap1);
6599 obj = isl_vec_alloc(ctx, 1 + total);
6600 isl_seq_clr(obj->block.data, 1 + total);
6601 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6602 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6605 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6607 isl_basic_map_free(bmap1);
6611 isl_basic_map_free(bmap1);
6612 isl_basic_map_free(bmap2);
6613 return isl_lp_error;
6616 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6617 * for any common value of the parameters and dimensions preceding pos
6618 * in both basic sets, the values of dimension pos in bset1 are
6619 * smaller or larger than those in bset2.
6622 * 1 if bset1 follows bset2
6623 * -1 if bset1 precedes bset2
6624 * 0 if bset1 and bset2 are incomparable
6625 * -2 if some error occurred.
6627 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6628 struct isl_basic_set *bset2, int pos)
6631 enum isl_lp_result res;
6636 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6638 if (res == isl_lp_empty)
6640 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6641 res == isl_lp_unbounded)
6643 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6652 /* Given two basic sets bset1 and bset2, check whether
6653 * for any common value of the parameters and dimensions preceding pos
6654 * there is a value of dimension pos in bset1 that is larger
6655 * than a value of the same dimension in bset2.
6658 * 1 if there exists such a pair
6659 * 0 if there is no such pair, but there is a pair of equal values
6661 * -2 if some error occurred.
6663 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6664 __isl_keep isl_basic_set *bset2, int pos)
6667 enum isl_lp_result res;
6672 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6674 if (res == isl_lp_empty)
6676 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6677 res == isl_lp_unbounded)
6679 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6681 else if (res == isl_lp_ok)
6690 /* Given two sets set1 and set2, check whether
6691 * for any common value of the parameters and dimensions preceding pos
6692 * there is a value of dimension pos in set1 that is larger
6693 * than a value of the same dimension in set2.
6696 * 1 if there exists such a pair
6697 * 0 if there is no such pair, but there is a pair of equal values
6699 * -2 if some error occurred.
6701 int isl_set_follows_at(__isl_keep isl_set *set1,
6702 __isl_keep isl_set *set2, int pos)
6710 for (i = 0; i < set1->n; ++i)
6711 for (j = 0; j < set2->n; ++j) {
6713 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6714 if (f == 1 || f == -2)
6723 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
6724 unsigned pos, isl_int *val)
6732 total = isl_basic_map_total_dim(bmap);
6733 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6734 for (; d+1 > pos; --d)
6735 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6739 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6741 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6743 if (!isl_int_is_one(bmap->eq[i][1+d]))
6746 isl_int_neg(*val, bmap->eq[i][0]);
6752 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
6753 unsigned pos, isl_int *val)
6765 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
6768 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
6769 for (i = 1; fixed == 1 && i < map->n; ++i) {
6770 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
6771 if (fixed == 1 && isl_int_ne(tmp, v))
6775 isl_int_set(*val, v);
6781 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
6782 unsigned pos, isl_int *val)
6784 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
6788 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
6791 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
6794 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
6795 enum isl_dim_type type, unsigned pos, isl_int *val)
6797 if (pos >= isl_basic_map_dim(bmap, type))
6799 return isl_basic_map_plain_has_fixed_var(bmap,
6800 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6803 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
6804 enum isl_dim_type type, unsigned pos, isl_int *val)
6806 if (pos >= isl_map_dim(map, type))
6808 return isl_map_plain_has_fixed_var(map,
6809 map_offset(map, type) - 1 + pos, val);
6812 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
6813 enum isl_dim_type type, unsigned pos, isl_int *val)
6815 return isl_map_plain_is_fixed(map, type, pos, val);
6818 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6819 * then return this fixed value in *val.
6821 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
6822 unsigned dim, isl_int *val)
6824 return isl_basic_set_plain_has_fixed_var(bset,
6825 isl_basic_set_n_param(bset) + dim, val);
6828 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6829 * then return this fixed value in *val.
6831 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
6832 unsigned dim, isl_int *val)
6834 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6837 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
6838 unsigned dim, isl_int *val)
6840 return isl_set_plain_dim_is_fixed(set, dim, val);
6843 /* Check if input variable in has fixed value and if so and if val is not NULL,
6844 * then return this fixed value in *val.
6846 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
6847 unsigned in, isl_int *val)
6849 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
6852 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6853 * and if val is not NULL, then return this lower bound in *val.
6855 int isl_basic_set_plain_dim_has_fixed_lower_bound(
6856 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
6858 int i, i_eq = -1, i_ineq = -1;
6865 total = isl_basic_set_total_dim(bset);
6866 nparam = isl_basic_set_n_param(bset);
6867 for (i = 0; i < bset->n_eq; ++i) {
6868 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6874 for (i = 0; i < bset->n_ineq; ++i) {
6875 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6877 if (i_eq != -1 || i_ineq != -1)
6881 if (i_eq == -1 && i_ineq == -1)
6883 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6884 /* The coefficient should always be one due to normalization. */
6885 if (!isl_int_is_one(c[1+nparam+dim]))
6887 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6889 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6890 total - nparam - dim - 1) != -1)
6893 isl_int_neg(*val, c[0]);
6897 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
6898 unsigned dim, isl_int *val)
6910 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
6914 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
6916 for (i = 1; fixed == 1 && i < set->n; ++i) {
6917 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
6919 if (fixed == 1 && isl_int_ne(tmp, v))
6923 isl_int_set(*val, v);
6934 /* uset_gist depends on constraints without existentially quantified
6935 * variables sorting first.
6937 static int qsort_constraint_cmp(const void *p1, const void *p2)
6939 const struct constraint *c1 = (const struct constraint *)p1;
6940 const struct constraint *c2 = (const struct constraint *)p2;
6942 unsigned size = isl_min(c1->size, c2->size);
6944 l1 = isl_seq_last_non_zero(c1->c, size);
6945 l2 = isl_seq_last_non_zero(c2->c, size);
6950 return isl_seq_cmp(c1->c, c2->c, size);
6953 static struct isl_basic_map *isl_basic_map_sort_constraints(
6954 struct isl_basic_map *bmap)
6957 struct constraint *c;
6962 total = isl_basic_map_total_dim(bmap);
6963 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6966 for (i = 0; i < bmap->n_ineq; ++i) {
6968 c[i].c = bmap->ineq[i];
6970 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6971 for (i = 0; i < bmap->n_ineq; ++i)
6972 bmap->ineq[i] = c[i].c;
6976 isl_basic_map_free(bmap);
6980 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6981 __isl_take isl_basic_set *bset)
6983 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6984 (struct isl_basic_map *)bset);
6987 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6991 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6993 bmap = isl_basic_map_remove_redundancies(bmap);
6994 bmap = isl_basic_map_sort_constraints(bmap);
6995 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6999 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7001 return (struct isl_basic_set *)isl_basic_map_normalize(
7002 (struct isl_basic_map *)bset);
7005 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7006 const __isl_keep isl_basic_map *bmap2)
7013 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7014 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7015 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7016 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7017 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7018 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7019 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7020 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7021 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7022 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7023 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7025 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7027 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7029 if (bmap1->n_eq != bmap2->n_eq)
7030 return bmap1->n_eq - bmap2->n_eq;
7031 if (bmap1->n_ineq != bmap2->n_ineq)
7032 return bmap1->n_ineq - bmap2->n_ineq;
7033 if (bmap1->n_div != bmap2->n_div)
7034 return bmap1->n_div - bmap2->n_div;
7035 total = isl_basic_map_total_dim(bmap1);
7036 for (i = 0; i < bmap1->n_eq; ++i) {
7037 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7041 for (i = 0; i < bmap1->n_ineq; ++i) {
7042 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7046 for (i = 0; i < bmap1->n_div; ++i) {
7047 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7054 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7055 __isl_keep isl_basic_map *bmap2)
7057 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7060 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7061 __isl_keep isl_basic_set *bset2)
7063 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7064 (isl_basic_map *)bset2);
7067 static int qsort_bmap_cmp(const void *p1, const void *p2)
7069 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7070 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7072 return isl_basic_map_plain_cmp(bmap1, bmap2);
7075 /* We normalize in place, but if anything goes wrong we need
7076 * to return NULL, so we need to make sure we don't change the
7077 * meaning of any possible other copies of map.
7079 struct isl_map *isl_map_normalize(struct isl_map *map)
7082 struct isl_basic_map *bmap;
7086 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7088 for (i = 0; i < map->n; ++i) {
7089 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7092 isl_basic_map_free(map->p[i]);
7095 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7096 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7097 map = isl_map_remove_empty_parts(map);
7100 for (i = map->n - 1; i >= 1; --i) {
7101 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7103 isl_basic_map_free(map->p[i-1]);
7104 for (j = i; j < map->n; ++j)
7105 map->p[j-1] = map->p[j];
7115 struct isl_set *isl_set_normalize(struct isl_set *set)
7117 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7120 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7130 if (!isl_dim_equal(map1->dim, map2->dim))
7133 map1 = isl_map_copy(map1);
7134 map2 = isl_map_copy(map2);
7135 map1 = isl_map_normalize(map1);
7136 map2 = isl_map_normalize(map2);
7139 equal = map1->n == map2->n;
7140 for (i = 0; equal && i < map1->n; ++i) {
7141 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7154 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7156 return isl_map_plain_is_equal(map1, map2);
7159 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7161 return isl_map_plain_is_equal((struct isl_map *)set1,
7162 (struct isl_map *)set2);
7165 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7167 return isl_set_plain_is_equal(set1, set2);
7170 /* Return an interval that ranges from min to max (inclusive)
7172 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7173 isl_int min, isl_int max)
7176 struct isl_basic_set *bset = NULL;
7178 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7182 k = isl_basic_set_alloc_inequality(bset);
7185 isl_int_set_si(bset->ineq[k][1], 1);
7186 isl_int_neg(bset->ineq[k][0], min);
7188 k = isl_basic_set_alloc_inequality(bset);
7191 isl_int_set_si(bset->ineq[k][1], -1);
7192 isl_int_set(bset->ineq[k][0], max);
7196 isl_basic_set_free(bset);
7200 /* Return the Cartesian product of the basic sets in list (in the given order).
7202 __isl_give isl_basic_set *isl_basic_set_list_product(
7203 __isl_take struct isl_basic_set_list *list)
7211 struct isl_basic_set *product = NULL;
7215 isl_assert(list->ctx, list->n > 0, goto error);
7216 isl_assert(list->ctx, list->p[0], goto error);
7217 nparam = isl_basic_set_n_param(list->p[0]);
7218 dim = isl_basic_set_n_dim(list->p[0]);
7219 extra = list->p[0]->n_div;
7220 n_eq = list->p[0]->n_eq;
7221 n_ineq = list->p[0]->n_ineq;
7222 for (i = 1; i < list->n; ++i) {
7223 isl_assert(list->ctx, list->p[i], goto error);
7224 isl_assert(list->ctx,
7225 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7226 dim += isl_basic_set_n_dim(list->p[i]);
7227 extra += list->p[i]->n_div;
7228 n_eq += list->p[i]->n_eq;
7229 n_ineq += list->p[i]->n_ineq;
7231 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7236 for (i = 0; i < list->n; ++i) {
7237 isl_basic_set_add_constraints(product,
7238 isl_basic_set_copy(list->p[i]), dim);
7239 dim += isl_basic_set_n_dim(list->p[i]);
7241 isl_basic_set_list_free(list);
7244 isl_basic_set_free(product);
7245 isl_basic_set_list_free(list);
7249 struct isl_basic_map *isl_basic_map_product(
7250 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7252 struct isl_dim *dim_result = NULL;
7253 struct isl_basic_map *bmap;
7254 unsigned in1, in2, out1, out2, nparam, total, pos;
7255 struct isl_dim_map *dim_map1, *dim_map2;
7257 if (!bmap1 || !bmap2)
7260 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7261 bmap2->dim, isl_dim_param), goto error);
7262 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7263 isl_dim_copy(bmap2->dim));
7265 in1 = isl_basic_map_n_in(bmap1);
7266 in2 = isl_basic_map_n_in(bmap2);
7267 out1 = isl_basic_map_n_out(bmap1);
7268 out2 = isl_basic_map_n_out(bmap2);
7269 nparam = isl_basic_map_n_param(bmap1);
7271 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7272 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7273 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7274 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7275 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7276 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7277 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7278 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7279 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7280 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7281 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7283 bmap = isl_basic_map_alloc_dim(dim_result,
7284 bmap1->n_div + bmap2->n_div,
7285 bmap1->n_eq + bmap2->n_eq,
7286 bmap1->n_ineq + bmap2->n_ineq);
7287 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7288 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7289 bmap = isl_basic_map_simplify(bmap);
7290 return isl_basic_map_finalize(bmap);
7292 isl_basic_map_free(bmap1);
7293 isl_basic_map_free(bmap2);
7297 __isl_give isl_basic_map *isl_basic_map_flat_product(
7298 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7300 isl_basic_map *prod;
7302 prod = isl_basic_map_product(bmap1, bmap2);
7303 prod = isl_basic_map_flatten(prod);
7307 __isl_give isl_basic_set *isl_basic_set_flat_product(
7308 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7310 return isl_basic_map_flat_product(bset1, bset2);
7313 __isl_give isl_basic_map *isl_basic_map_range_product(
7314 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7316 isl_dim *dim_result = NULL;
7317 isl_basic_map *bmap;
7318 unsigned in, out1, out2, nparam, total, pos;
7319 struct isl_dim_map *dim_map1, *dim_map2;
7321 if (!bmap1 || !bmap2)
7324 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7325 isl_dim_copy(bmap2->dim));
7327 in = isl_basic_map_dim(bmap1, isl_dim_in);
7328 out1 = isl_basic_map_n_out(bmap1);
7329 out2 = isl_basic_map_n_out(bmap2);
7330 nparam = isl_basic_map_n_param(bmap1);
7332 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7333 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7334 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7335 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7336 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7337 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7338 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7339 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7340 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7341 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7342 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7344 bmap = isl_basic_map_alloc_dim(dim_result,
7345 bmap1->n_div + bmap2->n_div,
7346 bmap1->n_eq + bmap2->n_eq,
7347 bmap1->n_ineq + bmap2->n_ineq);
7348 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7349 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7350 bmap = isl_basic_map_simplify(bmap);
7351 return isl_basic_map_finalize(bmap);
7353 isl_basic_map_free(bmap1);
7354 isl_basic_map_free(bmap2);
7358 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7359 __isl_take isl_map *map2,
7360 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7361 __isl_take isl_dim *right),
7362 __isl_give isl_basic_map *(*basic_map_product)(
7363 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7366 struct isl_map *result;
7372 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7373 map2->dim, isl_dim_param), goto error);
7375 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7376 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7377 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7379 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7380 isl_dim_copy(map2->dim)),
7381 map1->n * map2->n, flags);
7384 for (i = 0; i < map1->n; ++i)
7385 for (j = 0; j < map2->n; ++j) {
7386 struct isl_basic_map *part;
7387 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7388 isl_basic_map_copy(map2->p[j]));
7389 if (isl_basic_map_is_empty(part))
7390 isl_basic_map_free(part);
7392 result = isl_map_add_basic_map(result, part);
7405 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7407 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7409 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7412 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7414 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7415 __isl_take isl_map *map2)
7419 prod = isl_map_product(map1, map2);
7420 prod = isl_map_flatten(prod);
7424 /* Given two set A and B, construct its Cartesian product A x B.
7426 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7428 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7429 (struct isl_map *)set2);
7432 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7433 __isl_take isl_set *set2)
7435 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7438 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7440 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7441 __isl_take isl_map *map2)
7443 return map_product(map1, map2, &isl_dim_range_product,
7444 &isl_basic_map_range_product);
7447 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7449 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
7450 __isl_take isl_map *map2)
7454 prod = isl_map_range_product(map1, map2);
7455 prod = isl_map_flatten_range(prod);
7459 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7462 uint32_t hash = isl_hash_init();
7467 bmap = isl_basic_map_copy(bmap);
7468 bmap = isl_basic_map_normalize(bmap);
7471 total = isl_basic_map_total_dim(bmap);
7472 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7473 for (i = 0; i < bmap->n_eq; ++i) {
7475 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7476 isl_hash_hash(hash, c_hash);
7478 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7479 for (i = 0; i < bmap->n_ineq; ++i) {
7481 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7482 isl_hash_hash(hash, c_hash);
7484 isl_hash_byte(hash, bmap->n_div & 0xFF);
7485 for (i = 0; i < bmap->n_div; ++i) {
7487 if (isl_int_is_zero(bmap->div[i][0]))
7489 isl_hash_byte(hash, i & 0xFF);
7490 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7491 isl_hash_hash(hash, c_hash);
7493 isl_basic_map_free(bmap);
7497 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7499 return isl_basic_map_get_hash((isl_basic_map *)bset);
7502 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7509 map = isl_map_copy(map);
7510 map = isl_map_normalize(map);
7514 hash = isl_hash_init();
7515 for (i = 0; i < map->n; ++i) {
7517 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7518 isl_hash_hash(hash, bmap_hash);
7526 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7528 return isl_map_get_hash((isl_map *)set);
7531 /* Check if the value for dimension dim is completely determined
7532 * by the values of the other parameters and variables.
7533 * That is, check if dimension dim is involved in an equality.
7535 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7542 nparam = isl_basic_set_n_param(bset);
7543 for (i = 0; i < bset->n_eq; ++i)
7544 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7549 /* Check if the value for dimension dim is completely determined
7550 * by the values of the other parameters and variables.
7551 * That is, check if dimension dim is involved in an equality
7552 * for each of the subsets.
7554 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7560 for (i = 0; i < set->n; ++i) {
7562 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7569 int isl_set_n_basic_set(__isl_keep isl_set *set)
7571 return set ? set->n : 0;
7574 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7575 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7582 for (i = 0; i < map->n; ++i)
7583 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7589 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7590 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7597 for (i = 0; i < set->n; ++i)
7598 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7604 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7606 struct isl_dim *dim;
7611 bset = isl_basic_set_cow(bset);
7615 dim = isl_basic_set_get_dim(bset);
7616 dim = isl_dim_lift(dim, bset->n_div);
7619 isl_dim_free(bset->dim);
7621 bset->extra -= bset->n_div;
7624 bset = isl_basic_set_finalize(bset);
7628 isl_basic_set_free(bset);
7632 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7635 struct isl_dim *dim;
7638 set = isl_set_align_divs(set);
7643 set = isl_set_cow(set);
7647 n_div = set->p[0]->n_div;
7648 dim = isl_set_get_dim(set);
7649 dim = isl_dim_lift(dim, n_div);
7652 isl_dim_free(set->dim);
7655 for (i = 0; i < set->n; ++i) {
7656 set->p[i] = isl_basic_set_lift(set->p[i]);
7667 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7669 struct isl_dim *dim;
7670 struct isl_basic_map *bmap;
7677 set = isl_set_align_divs(set);
7682 dim = isl_set_get_dim(set);
7683 if (set->n == 0 || set->p[0]->n_div == 0) {
7685 return isl_map_identity(isl_dim_map_from_set(dim));
7688 n_div = set->p[0]->n_div;
7689 dim = isl_dim_map_from_set(dim);
7690 n_param = isl_dim_size(dim, isl_dim_param);
7691 n_set = isl_dim_size(dim, isl_dim_in);
7692 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7693 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7694 for (i = 0; i < n_set; ++i)
7695 bmap = var_equal(bmap, i);
7697 total = n_param + n_set + n_set + n_div;
7698 for (i = 0; i < n_div; ++i) {
7699 k = isl_basic_map_alloc_inequality(bmap);
7702 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7703 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7704 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7705 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7706 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7707 set->p[0]->div[i][0]);
7709 l = isl_basic_map_alloc_inequality(bmap);
7712 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7713 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7714 set->p[0]->div[i][0]);
7715 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7719 bmap = isl_basic_map_simplify(bmap);
7720 bmap = isl_basic_map_finalize(bmap);
7721 return isl_map_from_basic_map(bmap);
7724 isl_basic_map_free(bmap);
7728 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7736 dim = isl_basic_set_total_dim(bset);
7737 size += bset->n_eq * (1 + dim);
7738 size += bset->n_ineq * (1 + dim);
7739 size += bset->n_div * (2 + dim);
7744 int isl_set_size(__isl_keep isl_set *set)
7752 for (i = 0; i < set->n; ++i)
7753 size += isl_basic_set_size(set->p[i]);
7758 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7759 enum isl_dim_type type, unsigned pos)
7767 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7769 pos += isl_basic_map_offset(bmap, type);
7771 for (i = 0; i < bmap->n_eq; ++i)
7772 if (!isl_int_is_zero(bmap->eq[i][pos]))
7776 for (i = 0; i < bmap->n_ineq; ++i) {
7777 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7784 return lower && upper;
7787 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7788 enum isl_dim_type type, unsigned pos)
7795 for (i = 0; i < map->n; ++i) {
7797 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7798 if (bounded < 0 || !bounded)
7805 /* Return 1 if the specified dim is involved in both an upper bound
7806 * and a lower bound.
7808 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7809 enum isl_dim_type type, unsigned pos)
7811 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7814 /* For each of the "n" variables starting at "first", determine
7815 * the sign of the variable and put the results in the first "n"
7816 * elements of the array "signs".
7818 * 1 means that the variable is non-negative
7819 * -1 means that the variable is non-positive
7820 * 0 means the variable attains both positive and negative values.
7822 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7823 unsigned first, unsigned n, int *signs)
7825 isl_vec *bound = NULL;
7826 struct isl_tab *tab = NULL;
7827 struct isl_tab_undo *snap;
7830 if (!bset || !signs)
7833 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7834 tab = isl_tab_from_basic_set(bset);
7838 isl_seq_clr(bound->el, bound->size);
7839 isl_int_set_si(bound->el[0], -1);
7841 snap = isl_tab_snap(tab);
7842 for (i = 0; i < n; ++i) {
7845 isl_int_set_si(bound->el[1 + first + i], -1);
7846 if (isl_tab_add_ineq(tab, bound->el) < 0)
7849 isl_int_set_si(bound->el[1 + first + i], 0);
7850 if (isl_tab_rollback(tab, snap) < 0)
7858 isl_int_set_si(bound->el[1 + first + i], 1);
7859 if (isl_tab_add_ineq(tab, bound->el) < 0)
7862 isl_int_set_si(bound->el[1 + first + i], 0);
7863 if (isl_tab_rollback(tab, snap) < 0)
7866 signs[i] = empty ? -1 : 0;
7870 isl_vec_free(bound);
7874 isl_vec_free(bound);
7878 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7879 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7881 if (!bset || !signs)
7883 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7886 first += pos(bset->dim, type) - 1;
7887 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7890 /* Check if the given basic map is obviously single-valued.
7891 * In particular, for each output dimension, check that there is
7892 * an equality that defines the output dimension in terms of
7893 * earlier dimensions.
7895 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
7905 total = 1 + isl_basic_map_total_dim(bmap);
7906 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7907 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7909 for (i = 0; i < n_out; ++i) {
7910 for (j = 0; j < bmap->n_eq; ++j) {
7911 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7913 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7914 total - (o_out + i + 1)) == -1)
7917 if (j >= bmap->n_eq)
7924 /* Check if the given map is obviously single-valued.
7926 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
7935 return isl_basic_map_plain_is_single_valued(map->p[0]);
7938 /* Check if the given map is single-valued.
7943 * and check if the result is a subset of the identity mapping.
7945 int isl_map_is_single_valued(__isl_keep isl_map *map)
7952 sv = isl_map_plain_is_single_valued(map);
7956 test = isl_map_reverse(isl_map_copy(map));
7957 test = isl_map_apply_range(test, isl_map_copy(map));
7959 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
7960 id = isl_map_identity(dim);
7962 sv = isl_map_is_subset(test, id);
7970 int isl_map_is_injective(__isl_keep isl_map *map)
7974 map = isl_map_copy(map);
7975 map = isl_map_reverse(map);
7976 in = isl_map_is_single_valued(map);
7982 /* Check if the given map is obviously injective.
7984 int isl_map_plain_is_injective(__isl_keep isl_map *map)
7988 map = isl_map_copy(map);
7989 map = isl_map_reverse(map);
7990 in = isl_map_plain_is_single_valued(map);
7996 int isl_map_is_bijective(__isl_keep isl_map *map)
8000 sv = isl_map_is_single_valued(map);
8004 return isl_map_is_injective(map);
8007 int isl_set_is_singleton(__isl_keep isl_set *set)
8009 return isl_map_is_single_valued((isl_map *)set);
8012 int isl_map_is_translation(__isl_keep isl_map *map)
8017 delta = isl_map_deltas(isl_map_copy(map));
8018 ok = isl_set_is_singleton(delta);
8019 isl_set_free(delta);
8024 static int unique(isl_int *p, unsigned pos, unsigned len)
8026 if (isl_seq_first_non_zero(p, pos) != -1)
8028 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8033 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8042 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8045 nvar = isl_basic_set_dim(bset, isl_dim_set);
8046 ovar = isl_dim_offset(bset->dim, isl_dim_set);
8047 for (j = 0; j < nvar; ++j) {
8048 int lower = 0, upper = 0;
8049 for (i = 0; i < bset->n_eq; ++i) {
8050 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8052 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8058 for (i = 0; i < bset->n_ineq; ++i) {
8059 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8061 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8063 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8068 if (!lower || !upper)
8075 int isl_set_is_box(__isl_keep isl_set *set)
8082 return isl_basic_set_is_box(set->p[0]);
8085 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8090 return isl_dim_is_wrapping(bset->dim);
8093 int isl_set_is_wrapping(__isl_keep isl_set *set)
8098 return isl_dim_is_wrapping(set->dim);
8101 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8103 bmap = isl_basic_map_cow(bmap);
8107 bmap->dim = isl_dim_wrap(bmap->dim);
8111 bmap = isl_basic_map_finalize(bmap);
8113 return (isl_basic_set *)bmap;
8115 isl_basic_map_free(bmap);
8119 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8123 map = isl_map_cow(map);
8127 for (i = 0; i < map->n; ++i) {
8128 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8132 map->dim = isl_dim_wrap(map->dim);
8136 return (isl_set *)map;
8142 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8144 bset = isl_basic_set_cow(bset);
8148 bset->dim = isl_dim_unwrap(bset->dim);
8152 bset = isl_basic_set_finalize(bset);
8154 return (isl_basic_map *)bset;
8156 isl_basic_set_free(bset);
8160 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8167 if (!isl_set_is_wrapping(set))
8168 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8171 set = isl_set_cow(set);
8175 for (i = 0; i < set->n; ++i) {
8176 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8181 set->dim = isl_dim_unwrap(set->dim);
8185 return (isl_map *)set;
8191 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8192 enum isl_dim_type type)
8197 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8200 bmap = isl_basic_map_cow(bmap);
8204 bmap->dim = isl_dim_reset(bmap->dim, type);
8208 bmap = isl_basic_map_finalize(bmap);
8212 isl_basic_map_free(bmap);
8216 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8217 enum isl_dim_type type)
8224 if (!isl_dim_is_named_or_nested(map->dim, type))
8227 map = isl_map_cow(map);
8231 for (i = 0; i < map->n; ++i) {
8232 map->p[i] = isl_basic_map_reset(map->p[i], type);
8236 map->dim = isl_dim_reset(map->dim, type);
8246 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8251 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8254 bmap = isl_basic_map_cow(bmap);
8258 bmap->dim = isl_dim_flatten(bmap->dim);
8262 bmap = isl_basic_map_finalize(bmap);
8266 isl_basic_map_free(bmap);
8270 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8272 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8275 __isl_give isl_basic_map *isl_basic_map_flatten_range(
8276 __isl_take isl_basic_map *bmap)
8281 if (!bmap->dim->nested[1])
8284 bmap = isl_basic_map_cow(bmap);
8288 bmap->dim = isl_dim_flatten_range(bmap->dim);
8292 bmap = isl_basic_map_finalize(bmap);
8296 isl_basic_map_free(bmap);
8300 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8307 if (!map->dim->nested[0] && !map->dim->nested[1])
8310 map = isl_map_cow(map);
8314 for (i = 0; i < map->n; ++i) {
8315 map->p[i] = isl_basic_map_flatten(map->p[i]);
8319 map->dim = isl_dim_flatten(map->dim);
8329 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8331 return (isl_set *)isl_map_flatten((isl_map *)set);
8334 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8336 isl_dim *dim, *flat_dim;
8339 dim = isl_set_get_dim(set);
8340 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8341 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8342 map = isl_map_intersect_domain(map, set);
8347 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
8354 if (!map->dim->nested[1])
8357 map = isl_map_cow(map);
8361 for (i = 0; i < map->n; ++i) {
8362 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
8366 map->dim = isl_dim_flatten_range(map->dim);
8376 /* Reorder the dimensions of "bmap" according to the given dim_map
8377 * and set the dimension specification to "dim".
8379 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8380 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8384 bmap = isl_basic_map_cow(bmap);
8385 if (!bmap || !dim || !dim_map)
8388 res = isl_basic_map_alloc_dim(dim,
8389 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8390 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8391 res = isl_basic_map_finalize(res);
8395 isl_basic_map_free(bmap);
8400 /* Reorder the dimensions of "map" according to given reordering.
8402 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8403 __isl_take isl_reordering *r)
8406 struct isl_dim_map *dim_map;
8408 map = isl_map_cow(map);
8409 dim_map = isl_dim_map_from_reordering(r);
8410 if (!map || !r || !dim_map)
8413 for (i = 0; i < map->n; ++i) {
8414 struct isl_dim_map *dim_map_i;
8416 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8418 map->p[i] = isl_basic_map_realign(map->p[i],
8419 isl_dim_copy(r->dim), dim_map_i);
8425 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8427 isl_reordering_free(r);
8433 isl_reordering_free(r);
8437 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8438 __isl_take isl_reordering *r)
8440 return (isl_set *)isl_map_realign((isl_map *)set, r);
8443 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
8444 __isl_take isl_dim *model)
8451 ctx = isl_dim_get_ctx(model);
8452 if (!isl_dim_has_named_params(model))
8453 isl_die(ctx, isl_error_invalid,
8454 "model has unnamed parameters", goto error);
8455 if (!isl_dim_has_named_params(map->dim))
8456 isl_die(ctx, isl_error_invalid,
8457 "relation has unnamed parameters", goto error);
8458 if (!isl_dim_match(map->dim, isl_dim_param, model, isl_dim_param)) {
8459 isl_reordering *exp;
8461 model = isl_dim_drop(model, isl_dim_in,
8462 0, isl_dim_size(model, isl_dim_in));
8463 model = isl_dim_drop(model, isl_dim_out,
8464 0, isl_dim_size(model, isl_dim_out));
8465 exp = isl_parameter_alignment_reordering(map->dim, model);
8466 exp = isl_reordering_extend_dim(exp, isl_map_get_dim(map));
8467 map = isl_map_realign(map, exp);
8470 isl_dim_free(model);
8473 isl_dim_free(model);
8478 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
8479 __isl_take isl_dim *model)
8481 return isl_map_align_params(set, model);
8484 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8485 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8486 enum isl_dim_type c2, enum isl_dim_type c3,
8487 enum isl_dim_type c4, enum isl_dim_type c5)
8489 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8490 struct isl_mat *mat;
8496 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8497 isl_basic_map_total_dim(bmap) + 1);
8500 for (i = 0; i < bmap->n_eq; ++i)
8501 for (j = 0, pos = 0; j < 5; ++j) {
8502 int off = isl_basic_map_offset(bmap, c[j]);
8503 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8504 isl_int_set(mat->row[i][pos],
8505 bmap->eq[i][off + k]);
8513 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8514 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8515 enum isl_dim_type c2, enum isl_dim_type c3,
8516 enum isl_dim_type c4, enum isl_dim_type c5)
8518 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8519 struct isl_mat *mat;
8525 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8526 isl_basic_map_total_dim(bmap) + 1);
8529 for (i = 0; i < bmap->n_ineq; ++i)
8530 for (j = 0, pos = 0; j < 5; ++j) {
8531 int off = isl_basic_map_offset(bmap, c[j]);
8532 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8533 isl_int_set(mat->row[i][pos],
8534 bmap->ineq[i][off + k]);
8542 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8543 __isl_take isl_dim *dim,
8544 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8545 enum isl_dim_type c2, enum isl_dim_type c3,
8546 enum isl_dim_type c4, enum isl_dim_type c5)
8548 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8549 isl_basic_map *bmap;
8555 if (!dim || !eq || !ineq)
8558 if (eq->n_col != ineq->n_col)
8559 isl_die(dim->ctx, isl_error_invalid,
8560 "equalities and inequalities matrices should have "
8561 "same number of columns", goto error);
8563 total = 1 + isl_dim_total(dim);
8565 if (eq->n_col < total)
8566 isl_die(dim->ctx, isl_error_invalid,
8567 "number of columns too small", goto error);
8569 extra = eq->n_col - total;
8571 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8572 eq->n_row, ineq->n_row);
8575 for (i = 0; i < extra; ++i) {
8576 k = isl_basic_map_alloc_div(bmap);
8579 isl_int_set_si(bmap->div[k][0], 0);
8581 for (i = 0; i < eq->n_row; ++i) {
8582 l = isl_basic_map_alloc_equality(bmap);
8585 for (j = 0, pos = 0; j < 5; ++j) {
8586 int off = isl_basic_map_offset(bmap, c[j]);
8587 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8588 isl_int_set(bmap->eq[l][off + k],
8594 for (i = 0; i < ineq->n_row; ++i) {
8595 l = isl_basic_map_alloc_inequality(bmap);
8598 for (j = 0, pos = 0; j < 5; ++j) {
8599 int off = isl_basic_map_offset(bmap, c[j]);
8600 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8601 isl_int_set(bmap->ineq[l][off + k],
8620 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8621 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8622 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8624 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8625 c1, c2, c3, c4, isl_dim_in);
8628 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8629 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8630 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8632 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8633 c1, c2, c3, c4, isl_dim_in);
8636 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8637 __isl_take isl_dim *dim,
8638 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8639 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8641 return (isl_basic_set*)
8642 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8643 c1, c2, c3, c4, isl_dim_in);
8646 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8651 return isl_dim_can_zip(bmap->dim);
8654 int isl_map_can_zip(__isl_keep isl_map *map)
8659 return isl_dim_can_zip(map->dim);
8662 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8663 * (A -> C) -> (B -> D).
8665 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8674 if (!isl_basic_map_can_zip(bmap))
8675 isl_die(bmap->ctx, isl_error_invalid,
8676 "basic map cannot be zipped", goto error);
8677 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8678 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8679 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8680 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8681 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8684 bmap->dim = isl_dim_zip(bmap->dim);
8689 isl_basic_map_free(bmap);
8693 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8694 * (A -> C) -> (B -> D).
8696 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8703 if (!isl_map_can_zip(map))
8704 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8707 map = isl_map_cow(map);
8711 for (i = 0; i < map->n; ++i) {
8712 map->p[i] = isl_basic_map_zip(map->p[i]);
8717 map->dim = isl_dim_zip(map->dim);