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
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
18 #include "isl_dim_private.h"
19 #include "isl_equalities.h"
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
30 #include <isl_mat_private.h>
31 #include <isl_dim_map.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 struct isl_dim *isl_map_get_dim(struct isl_map *map)
260 return isl_dim_copy(map->dim);
263 struct isl_dim *isl_set_get_dim(struct isl_set *set)
267 return isl_dim_copy(set->dim);
270 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
271 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
273 bmap = isl_basic_map_cow(bmap);
276 bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
279 bmap = isl_basic_map_finalize(bmap);
282 isl_basic_map_free(bmap);
286 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
287 enum isl_dim_type type)
289 return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
292 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
293 enum isl_dim_type type, const char *s)
297 map = isl_map_cow(map);
301 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
305 for (i = 0; i < map->n; ++i) {
306 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
317 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
318 enum isl_dim_type type)
320 return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
323 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
326 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
329 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
331 return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
334 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
336 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
339 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
340 enum isl_dim_type type, unsigned pos)
342 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
345 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
346 enum isl_dim_type type, unsigned pos)
348 return bset ? isl_dim_get_name(bset->dim, type, pos) : NULL;
351 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
352 enum isl_dim_type type, unsigned pos)
354 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
357 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
358 enum isl_dim_type type, unsigned pos)
360 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
363 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
364 __isl_take isl_basic_map *bmap,
365 enum isl_dim_type type, unsigned pos, const char *s)
369 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
374 isl_basic_map_free(bmap);
378 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
379 enum isl_dim_type type, unsigned pos, const char *s)
386 map->dim = isl_dim_set_name(map->dim, type, pos, s);
390 for (i = 0; i < map->n; ++i) {
391 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
402 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
403 __isl_take isl_basic_set *bset,
404 enum isl_dim_type type, unsigned pos, const char *s)
406 return (isl_basic_set *)isl_basic_map_set_dim_name(
407 (isl_basic_map *)bset, type, pos, s);
410 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
411 enum isl_dim_type type, unsigned pos, const char *s)
413 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
416 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
420 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
423 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
425 return isl_basic_map_is_rational(bset);
428 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
429 struct isl_basic_map *bmap, unsigned extra,
430 unsigned n_eq, unsigned n_ineq)
433 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
438 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
439 if (isl_blk_is_error(bmap->block))
442 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
447 bmap->block2 = isl_blk_empty();
450 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
451 if (isl_blk_is_error(bmap->block2))
454 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
459 for (i = 0; i < n_ineq + n_eq; ++i)
460 bmap->ineq[i] = bmap->block.data + i * row_size;
462 for (i = 0; i < extra; ++i)
463 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
467 bmap->c_size = n_eq + n_ineq;
468 bmap->eq = bmap->ineq + n_ineq;
477 isl_basic_map_free(bmap);
481 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
482 unsigned nparam, unsigned dim, unsigned extra,
483 unsigned n_eq, unsigned n_ineq)
485 struct isl_basic_map *bmap;
486 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
487 return (struct isl_basic_set *)bmap;
490 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
491 unsigned extra, unsigned n_eq, unsigned n_ineq)
493 struct isl_basic_map *bmap;
496 isl_assert(dim->ctx, dim->n_in == 0, goto error);
497 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
498 return (struct isl_basic_set *)bmap;
504 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
505 unsigned extra, unsigned n_eq, unsigned n_ineq)
507 struct isl_basic_map *bmap;
511 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
516 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
522 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
523 unsigned nparam, unsigned in, unsigned out, unsigned extra,
524 unsigned n_eq, unsigned n_ineq)
526 struct isl_basic_map *bmap;
529 dim = isl_dim_alloc(ctx, nparam, in, out);
533 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
537 static void dup_constraints(
538 struct isl_basic_map *dst, struct isl_basic_map *src)
541 unsigned total = isl_basic_map_total_dim(src);
543 for (i = 0; i < src->n_eq; ++i) {
544 int j = isl_basic_map_alloc_equality(dst);
545 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
548 for (i = 0; i < src->n_ineq; ++i) {
549 int j = isl_basic_map_alloc_inequality(dst);
550 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
553 for (i = 0; i < src->n_div; ++i) {
554 int j = isl_basic_map_alloc_div(dst);
555 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
557 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
560 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
562 struct isl_basic_map *dup;
566 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
567 bmap->n_div, bmap->n_eq, bmap->n_ineq);
570 dup_constraints(dup, bmap);
571 dup->flags = bmap->flags;
572 dup->sample = isl_vec_copy(bmap->sample);
576 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
578 struct isl_basic_map *dup;
580 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
581 return (struct isl_basic_set *)dup;
584 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
589 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
593 return isl_basic_set_dup(bset);
596 struct isl_set *isl_set_copy(struct isl_set *set)
605 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
610 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
614 bmap = isl_basic_map_dup(bmap);
616 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
620 struct isl_map *isl_map_copy(struct isl_map *map)
629 void isl_basic_map_free(struct isl_basic_map *bmap)
637 isl_ctx_deref(bmap->ctx);
639 isl_blk_free(bmap->ctx, bmap->block2);
641 isl_blk_free(bmap->ctx, bmap->block);
642 isl_vec_free(bmap->sample);
643 isl_dim_free(bmap->dim);
647 void isl_basic_set_free(struct isl_basic_set *bset)
649 isl_basic_map_free((struct isl_basic_map *)bset);
652 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
654 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
657 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
663 isl_assert(ctx, room_for_con(bmap, 1), return -1);
664 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
666 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
667 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
668 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
669 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
670 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
671 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
673 int j = isl_basic_map_alloc_inequality(bmap);
677 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
678 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
685 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
686 bmap->extra - bmap->n_div);
690 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
692 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
695 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
699 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
704 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
706 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
709 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
714 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
716 if (pos != bmap->n_eq - 1) {
718 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
719 bmap->eq[bmap->n_eq - 1] = t;
725 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
727 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
730 void isl_basic_map_inequality_to_equality(
731 struct isl_basic_map *bmap, unsigned pos)
736 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
737 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
742 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
743 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
744 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
745 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
748 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
750 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
753 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
759 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
760 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
761 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
762 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
763 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
764 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
765 1 + isl_basic_map_total_dim(bmap),
766 bmap->extra - bmap->n_div);
767 return bmap->n_ineq++;
770 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
772 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
775 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
779 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
784 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
786 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
789 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
794 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
796 if (pos != bmap->n_ineq - 1) {
798 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
799 bmap->ineq[bmap->n_ineq - 1] = t;
800 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
806 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
808 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
811 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
816 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
819 k = isl_basic_map_alloc_equality(bmap);
822 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
825 isl_basic_map_free(bmap);
829 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
832 return (isl_basic_set *)
833 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
836 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
841 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
844 k = isl_basic_map_alloc_inequality(bmap);
847 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
850 isl_basic_map_free(bmap);
854 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
857 return (isl_basic_set *)
858 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
861 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
865 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
866 isl_seq_clr(bmap->div[bmap->n_div] +
867 1 + 1 + isl_basic_map_total_dim(bmap),
868 bmap->extra - bmap->n_div);
869 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
870 return bmap->n_div++;
873 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
875 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
878 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
882 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
887 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
889 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
892 /* Copy constraint from src to dst, putting the vars of src at offset
893 * dim_off in dst and the divs of src at offset div_off in dst.
894 * If both sets are actually map, then dim_off applies to the input
897 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
898 struct isl_basic_map *src_map, isl_int *src,
899 unsigned in_off, unsigned out_off, unsigned div_off)
901 unsigned src_nparam = isl_basic_map_n_param(src_map);
902 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
903 unsigned src_in = isl_basic_map_n_in(src_map);
904 unsigned dst_in = isl_basic_map_n_in(dst_map);
905 unsigned src_out = isl_basic_map_n_out(src_map);
906 unsigned dst_out = isl_basic_map_n_out(dst_map);
907 isl_int_set(dst[0], src[0]);
908 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
909 if (dst_nparam > src_nparam)
910 isl_seq_clr(dst+1+src_nparam,
911 dst_nparam - src_nparam);
912 isl_seq_clr(dst+1+dst_nparam, in_off);
913 isl_seq_cpy(dst+1+dst_nparam+in_off,
915 isl_min(dst_in-in_off, src_in));
916 if (dst_in-in_off > src_in)
917 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
918 dst_in - in_off - src_in);
919 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
920 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
921 src+1+src_nparam+src_in,
922 isl_min(dst_out-out_off, src_out));
923 if (dst_out-out_off > src_out)
924 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
925 dst_out - out_off - src_out);
926 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
927 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
928 src+1+src_nparam+src_in+src_out,
929 isl_min(dst_map->extra-div_off, src_map->n_div));
930 if (dst_map->n_div-div_off > src_map->n_div)
931 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
932 div_off+src_map->n_div,
933 dst_map->n_div - div_off - src_map->n_div);
936 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
937 struct isl_basic_map *src_map, isl_int *src,
938 unsigned in_off, unsigned out_off, unsigned div_off)
940 isl_int_set(dst[0], src[0]);
941 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
944 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
945 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
950 if (!bmap1 || !bmap2)
953 div_off = bmap1->n_div;
955 for (i = 0; i < bmap2->n_eq; ++i) {
956 int i1 = isl_basic_map_alloc_equality(bmap1);
959 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
960 i_pos, o_pos, div_off);
963 for (i = 0; i < bmap2->n_ineq; ++i) {
964 int i1 = isl_basic_map_alloc_inequality(bmap1);
967 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
968 i_pos, o_pos, div_off);
971 for (i = 0; i < bmap2->n_div; ++i) {
972 int i1 = isl_basic_map_alloc_div(bmap1);
975 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
976 i_pos, o_pos, div_off);
979 isl_basic_map_free(bmap2);
984 isl_basic_map_free(bmap1);
985 isl_basic_map_free(bmap2);
989 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
990 struct isl_basic_set *bset2, unsigned pos)
992 return (struct isl_basic_set *)
993 add_constraints((struct isl_basic_map *)bset1,
994 (struct isl_basic_map *)bset2, 0, pos);
997 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
998 struct isl_dim *dim, unsigned extra,
999 unsigned n_eq, unsigned n_ineq)
1001 struct isl_basic_map *ext;
1011 dims_ok = isl_dim_equal(base->dim, dim) &&
1012 base->extra >= base->n_div + extra;
1014 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1015 room_for_ineq(base, n_ineq)) {
1020 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1021 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1022 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1023 extra += base->extra;
1025 n_ineq += base->n_ineq;
1027 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1033 ext->sample = isl_vec_copy(base->sample);
1034 flags = base->flags;
1035 ext = add_constraints(ext, base, 0, 0);
1038 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1045 isl_basic_map_free(base);
1049 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1050 struct isl_dim *dim, unsigned extra,
1051 unsigned n_eq, unsigned n_ineq)
1053 return (struct isl_basic_set *)
1054 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1055 extra, n_eq, n_ineq);
1058 struct isl_basic_map *isl_basic_map_extend_constraints(
1059 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1063 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1067 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1068 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1069 unsigned n_eq, unsigned n_ineq)
1071 struct isl_basic_map *bmap;
1072 struct isl_dim *dim;
1076 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1080 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1083 isl_basic_map_free(base);
1087 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1088 unsigned nparam, unsigned dim, unsigned extra,
1089 unsigned n_eq, unsigned n_ineq)
1091 return (struct isl_basic_set *)
1092 isl_basic_map_extend((struct isl_basic_map *)base,
1093 nparam, 0, dim, extra, n_eq, n_ineq);
1096 struct isl_basic_set *isl_basic_set_extend_constraints(
1097 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1099 return (struct isl_basic_set *)
1100 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1104 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1106 return (struct isl_basic_set *)
1107 isl_basic_map_cow((struct isl_basic_map *)bset);
1110 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1115 if (bmap->ref > 1) {
1117 bmap = isl_basic_map_dup(bmap);
1120 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1124 struct isl_set *isl_set_cow(struct isl_set *set)
1132 return isl_set_dup(set);
1135 struct isl_map *isl_map_cow(struct isl_map *map)
1143 return isl_map_dup(map);
1146 static void swap_vars(struct isl_blk blk, isl_int *a,
1147 unsigned a_len, unsigned b_len)
1149 isl_seq_cpy(blk.data, a+a_len, b_len);
1150 isl_seq_cpy(blk.data+b_len, a, a_len);
1151 isl_seq_cpy(a, blk.data, b_len+a_len);
1154 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1155 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1163 isl_assert(bmap->ctx,
1164 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1166 if (n1 == 0 || n2 == 0)
1169 bmap = isl_basic_map_cow(bmap);
1173 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1174 if (isl_blk_is_error(blk))
1177 for (i = 0; i < bmap->n_eq; ++i)
1179 bmap->eq[i] + pos, n1, n2);
1181 for (i = 0; i < bmap->n_ineq; ++i)
1183 bmap->ineq[i] + pos, n1, n2);
1185 for (i = 0; i < bmap->n_div; ++i)
1187 bmap->div[i]+1 + pos, n1, n2);
1189 isl_blk_free(bmap->ctx, blk);
1191 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1192 bmap = isl_basic_map_gauss(bmap, NULL);
1193 return isl_basic_map_finalize(bmap);
1195 isl_basic_map_free(bmap);
1199 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1200 __isl_take isl_basic_set *bset, unsigned n)
1205 nparam = isl_basic_set_n_param(bset);
1206 dim = isl_basic_set_n_dim(bset);
1207 isl_assert(bset->ctx, n <= dim, goto error);
1209 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1211 isl_basic_set_free(bset);
1215 static __isl_give isl_set *isl_set_swap_vars(__isl_take isl_set *set,
1219 set = isl_set_cow(set);
1223 for (i = 0; i < set->n; ++i) {
1224 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1230 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1234 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1240 total = isl_basic_map_total_dim(bmap);
1241 isl_basic_map_free_div(bmap, bmap->n_div);
1242 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1244 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1246 i = isl_basic_map_alloc_equality(bmap);
1250 isl_int_set_si(bmap->eq[i][0], 1);
1251 isl_seq_clr(bmap->eq[i]+1, total);
1252 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1253 isl_vec_free(bmap->sample);
1254 bmap->sample = NULL;
1255 return isl_basic_map_finalize(bmap);
1257 isl_basic_map_free(bmap);
1261 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1263 return (struct isl_basic_set *)
1264 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1267 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1270 unsigned off = isl_dim_total(bmap->dim);
1271 isl_int *t = bmap->div[a];
1272 bmap->div[a] = bmap->div[b];
1275 for (i = 0; i < bmap->n_eq; ++i)
1276 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1278 for (i = 0; i < bmap->n_ineq; ++i)
1279 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1281 for (i = 0; i < bmap->n_div; ++i)
1282 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1283 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1286 /* Eliminate the specified n dimensions starting at first from the
1287 * constraints using Fourier-Motzkin. The dimensions themselves
1290 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1291 enum isl_dim_type type, unsigned first, unsigned n)
1300 map = isl_map_cow(map);
1303 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1304 first += pos(map->dim, type) - 1;
1306 for (i = 0; i < map->n; ++i) {
1307 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1317 /* Eliminate the specified n dimensions starting at first from the
1318 * constraints using Fourier-Motzkin. The dimensions themselves
1321 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1322 enum isl_dim_type type, unsigned first, unsigned n)
1324 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1327 /* Eliminate the specified n dimensions starting at first from the
1328 * constraints using Fourier-Motzkin. The dimensions themselves
1331 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1332 unsigned first, unsigned n)
1334 return isl_set_eliminate(set, isl_dim_set, first, n);
1337 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1338 __isl_take isl_basic_map *bmap)
1340 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1345 return isl_basic_map_finalize(bmap);
1348 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1349 __isl_take isl_basic_set *bset)
1351 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1352 (struct isl_basic_map *)bset);
1355 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1364 map = isl_map_cow(map);
1368 for (i = 0; i < map->n; ++i) {
1369 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1379 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1381 return isl_map_remove_divs(set);
1384 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1385 enum isl_dim_type type, unsigned first, unsigned n)
1389 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1393 bmap = isl_basic_map_eliminate_vars(bmap,
1394 isl_basic_map_offset(bmap, type) - 1 + first, n);
1397 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1399 bmap = isl_basic_map_drop(bmap, type, first, n);
1402 isl_basic_map_free(bmap);
1406 /* Return true if the definition of the given div (recursively) involves
1407 * any of the given variables.
1409 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1410 unsigned first, unsigned n)
1413 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1415 if (isl_int_is_zero(bmap->div[div][0]))
1417 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1420 for (i = bmap->n_div - 1; i >= 0; --i) {
1421 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1423 if (div_involves_vars(bmap, i, first, n))
1430 /* Remove all divs (recursively) involving any of the given dimensions
1431 * in their definitions.
1433 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1434 __isl_take isl_basic_map *bmap,
1435 enum isl_dim_type type, unsigned first, unsigned n)
1441 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1443 first += isl_basic_map_offset(bmap, type);
1445 for (i = bmap->n_div - 1; i >= 0; --i) {
1446 if (!div_involves_vars(bmap, i, first, n))
1448 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1453 isl_basic_map_free(bmap);
1457 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1458 enum isl_dim_type type, unsigned first, unsigned n)
1467 map = isl_map_cow(map);
1471 for (i = 0; i < map->n; ++i) {
1472 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1483 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1484 enum isl_dim_type type, unsigned first, unsigned n)
1486 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1490 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1491 enum isl_dim_type type, unsigned first, unsigned n)
1498 if (first + n > isl_basic_set_dim(bset, type))
1499 isl_die(bset->ctx, isl_error_invalid,
1500 "index out of bounds", return -1);
1502 first += isl_basic_set_offset(bset, type);
1503 for (i = 0; i < bset->n_eq; ++i)
1504 if (isl_seq_first_non_zero(bset->eq[i] + first, n) >= 0)
1506 for (i = 0; i < bset->n_ineq; ++i)
1507 if (isl_seq_first_non_zero(bset->ineq[i] + first, n) >= 0)
1513 int isl_set_involves_dims(__isl_keep isl_set *set,
1514 enum isl_dim_type type, unsigned first, unsigned n)
1521 if (first + n > isl_set_dim(set, type))
1522 isl_die(set->ctx, isl_error_invalid,
1523 "index out of bounds", return -1);
1525 for (i = 0; i < set->n; ++i) {
1526 int involves = isl_basic_set_involves_dims(set->p[i],
1528 if (involves < 0 || !involves)
1535 /* Return true if the definition of the given div is unknown or depends
1538 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1541 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1543 if (isl_int_is_zero(bmap->div[div][0]))
1546 for (i = bmap->n_div - 1; i >= 0; --i) {
1547 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1549 if (div_is_unknown(bmap, i))
1556 /* Remove all divs that are unknown or defined in terms of unknown divs.
1558 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1559 __isl_take isl_basic_map *bmap)
1566 for (i = bmap->n_div - 1; i >= 0; --i) {
1567 if (!div_is_unknown(bmap, i))
1569 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1575 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1584 map = isl_map_cow(map);
1588 for (i = 0; i < map->n; ++i) {
1589 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1599 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1601 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1604 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1605 __isl_take isl_basic_set *bset,
1606 enum isl_dim_type type, unsigned first, unsigned n)
1608 return (isl_basic_set *)
1609 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1612 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1613 enum isl_dim_type type, unsigned first, unsigned n)
1620 map = isl_map_cow(map);
1623 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1625 for (i = 0; i < map->n; ++i) {
1626 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1627 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1631 map = isl_map_drop(map, type, first, n);
1638 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1639 enum isl_dim_type type, unsigned first, unsigned n)
1641 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1644 /* Project out n inputs starting at first using Fourier-Motzkin */
1645 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1646 unsigned first, unsigned n)
1648 return isl_map_remove_dims(map, isl_dim_in, first, n);
1651 static void dump_term(struct isl_basic_map *bmap,
1652 isl_int c, int pos, FILE *out)
1655 unsigned in = isl_basic_map_n_in(bmap);
1656 unsigned dim = in + isl_basic_map_n_out(bmap);
1657 unsigned nparam = isl_basic_map_n_param(bmap);
1659 isl_int_print(out, c, 0);
1661 if (!isl_int_is_one(c))
1662 isl_int_print(out, c, 0);
1663 if (pos < 1 + nparam) {
1664 name = isl_dim_get_name(bmap->dim,
1665 isl_dim_param, pos - 1);
1667 fprintf(out, "%s", name);
1669 fprintf(out, "p%d", pos - 1);
1670 } else if (pos < 1 + nparam + in)
1671 fprintf(out, "i%d", pos - 1 - nparam);
1672 else if (pos < 1 + nparam + dim)
1673 fprintf(out, "o%d", pos - 1 - nparam - in);
1675 fprintf(out, "e%d", pos - 1 - nparam - dim);
1679 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1680 int sign, FILE *out)
1684 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1688 for (i = 0, first = 1; i < len; ++i) {
1689 if (isl_int_sgn(c[i]) * sign <= 0)
1692 fprintf(out, " + ");
1694 isl_int_abs(v, c[i]);
1695 dump_term(bmap, v, i, out);
1702 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1703 const char *op, FILE *out, int indent)
1707 fprintf(out, "%*s", indent, "");
1709 dump_constraint_sign(bmap, c, 1, out);
1710 fprintf(out, " %s ", op);
1711 dump_constraint_sign(bmap, c, -1, out);
1715 for (i = bmap->n_div; i < bmap->extra; ++i) {
1716 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1718 fprintf(out, "%*s", indent, "");
1719 fprintf(out, "ERROR: unused div coefficient not zero\n");
1724 static void dump_constraints(struct isl_basic_map *bmap,
1725 isl_int **c, unsigned n,
1726 const char *op, FILE *out, int indent)
1730 for (i = 0; i < n; ++i)
1731 dump_constraint(bmap, c[i], op, out, indent);
1734 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1738 unsigned total = isl_basic_map_total_dim(bmap);
1740 for (j = 0; j < 1 + total; ++j) {
1741 if (isl_int_is_zero(exp[j]))
1743 if (!first && isl_int_is_pos(exp[j]))
1745 dump_term(bmap, exp[j], j, out);
1750 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1754 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1755 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1757 for (i = 0; i < bmap->n_div; ++i) {
1758 fprintf(out, "%*s", indent, "");
1759 fprintf(out, "e%d = [(", i);
1760 dump_affine(bmap, bmap->div[i]+1, out);
1762 isl_int_print(out, bmap->div[i][0], 0);
1763 fprintf(out, "]\n");
1767 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1768 FILE *out, int indent)
1771 fprintf(out, "null basic set\n");
1775 fprintf(out, "%*s", indent, "");
1776 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1777 bset->ref, bset->dim->nparam, bset->dim->n_out,
1778 bset->extra, bset->flags);
1779 dump((struct isl_basic_map *)bset, out, indent);
1782 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1783 FILE *out, int indent)
1786 fprintf(out, "null basic map\n");
1790 fprintf(out, "%*s", indent, "");
1791 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1792 "flags: %x, n_name: %d\n",
1794 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1795 bmap->extra, bmap->flags, bmap->dim->n_name);
1796 dump(bmap, out, indent);
1799 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1804 total = isl_basic_map_total_dim(bmap);
1805 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1806 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1807 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1808 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1812 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1814 struct isl_set *set;
1818 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1819 isl_assert(dim->ctx, n >= 0, goto error);
1820 set = isl_alloc(dim->ctx, struct isl_set,
1821 sizeof(struct isl_set) +
1822 (n - 1) * sizeof(struct isl_basic_set *));
1826 set->ctx = dim->ctx;
1827 isl_ctx_ref(set->ctx);
1839 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1840 unsigned nparam, unsigned dim, int n, unsigned flags)
1842 struct isl_set *set;
1843 struct isl_dim *dims;
1845 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1849 set = isl_set_alloc_dim(dims, n, flags);
1853 /* Make sure "map" has room for at least "n" more basic maps.
1855 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1858 struct isl_map *grown = NULL;
1862 isl_assert(map->ctx, n >= 0, goto error);
1863 if (map->n + n <= map->size)
1865 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1868 for (i = 0; i < map->n; ++i) {
1869 grown->p[i] = isl_basic_map_copy(map->p[i]);
1877 isl_map_free(grown);
1882 /* Make sure "set" has room for at least "n" more basic sets.
1884 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1886 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1889 struct isl_set *isl_set_dup(struct isl_set *set)
1892 struct isl_set *dup;
1897 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1900 for (i = 0; i < set->n; ++i)
1901 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1905 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1907 return isl_map_from_basic_map(bset);
1910 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1912 struct isl_map *map;
1917 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1918 return isl_map_add_basic_map(map, bmap);
1921 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1922 __isl_take isl_basic_set *bset)
1924 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1925 (struct isl_basic_map *)bset);
1928 void isl_set_free(struct isl_set *set)
1938 isl_ctx_deref(set->ctx);
1939 for (i = 0; i < set->n; ++i)
1940 isl_basic_set_free(set->p[i]);
1941 isl_dim_free(set->dim);
1945 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
1950 fprintf(out, "null set\n");
1954 fprintf(out, "%*s", indent, "");
1955 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1956 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1958 for (i = 0; i < set->n; ++i) {
1959 fprintf(out, "%*s", indent, "");
1960 fprintf(out, "basic set %d:\n", i);
1961 isl_basic_set_print_internal(set->p[i], out, indent+4);
1965 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
1970 fprintf(out, "null map\n");
1974 fprintf(out, "%*s", indent, "");
1975 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1976 "flags: %x, n_name: %d\n",
1977 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1978 map->dim->n_out, map->flags, map->dim->n_name);
1979 for (i = 0; i < map->n; ++i) {
1980 fprintf(out, "%*s", indent, "");
1981 fprintf(out, "basic map %d:\n", i);
1982 isl_basic_map_print_internal(map->p[i], out, indent+4);
1986 struct isl_basic_map *isl_basic_map_intersect_domain(
1987 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1989 struct isl_basic_map *bmap_domain;
1994 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1995 bset->dim, isl_dim_param), goto error);
1997 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1998 isl_assert(bset->ctx,
1999 isl_basic_map_compatible_domain(bmap, bset), goto error);
2001 bmap = isl_basic_map_cow(bmap);
2004 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2005 bset->n_div, bset->n_eq, bset->n_ineq);
2006 bmap_domain = isl_basic_map_from_domain(bset);
2007 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2009 bmap = isl_basic_map_simplify(bmap);
2010 return isl_basic_map_finalize(bmap);
2012 isl_basic_map_free(bmap);
2013 isl_basic_set_free(bset);
2017 struct isl_basic_map *isl_basic_map_intersect_range(
2018 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2020 struct isl_basic_map *bmap_range;
2025 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2026 bset->dim, isl_dim_param), goto error);
2028 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2029 isl_assert(bset->ctx,
2030 isl_basic_map_compatible_range(bmap, bset), goto error);
2032 if (isl_basic_set_is_universe(bset)) {
2033 isl_basic_set_free(bset);
2037 bmap = isl_basic_map_cow(bmap);
2040 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2041 bset->n_div, bset->n_eq, bset->n_ineq);
2042 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2043 bmap = add_constraints(bmap, bmap_range, 0, 0);
2045 bmap = isl_basic_map_simplify(bmap);
2046 return isl_basic_map_finalize(bmap);
2048 isl_basic_map_free(bmap);
2049 isl_basic_set_free(bset);
2053 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2059 total = 1 + isl_basic_map_total_dim(bmap);
2060 if (total != vec->size)
2065 for (i = 0; i < bmap->n_eq; ++i) {
2066 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2067 if (!isl_int_is_zero(s)) {
2073 for (i = 0; i < bmap->n_ineq; ++i) {
2074 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2075 if (isl_int_is_neg(s)) {
2086 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2088 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2091 struct isl_basic_map *isl_basic_map_intersect(
2092 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2094 struct isl_vec *sample = NULL;
2096 if (!bmap1 || !bmap2)
2099 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2100 bmap2->dim, isl_dim_param), goto error);
2101 if (isl_dim_total(bmap1->dim) ==
2102 isl_dim_size(bmap1->dim, isl_dim_param) &&
2103 isl_dim_total(bmap2->dim) !=
2104 isl_dim_size(bmap2->dim, isl_dim_param))
2105 return isl_basic_map_intersect(bmap2, bmap1);
2107 if (isl_dim_total(bmap2->dim) !=
2108 isl_dim_size(bmap2->dim, isl_dim_param))
2109 isl_assert(bmap1->ctx,
2110 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2112 if (bmap1->sample &&
2113 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2114 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2115 sample = isl_vec_copy(bmap1->sample);
2116 else if (bmap2->sample &&
2117 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2118 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2119 sample = isl_vec_copy(bmap2->sample);
2121 bmap1 = isl_basic_map_cow(bmap1);
2124 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2125 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2126 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2129 isl_vec_free(sample);
2131 isl_vec_free(bmap1->sample);
2132 bmap1->sample = sample;
2135 bmap1 = isl_basic_map_simplify(bmap1);
2136 return isl_basic_map_finalize(bmap1);
2139 isl_vec_free(sample);
2140 isl_basic_map_free(bmap1);
2141 isl_basic_map_free(bmap2);
2145 struct isl_basic_set *isl_basic_set_intersect(
2146 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2148 return (struct isl_basic_set *)
2149 isl_basic_map_intersect(
2150 (struct isl_basic_map *)bset1,
2151 (struct isl_basic_map *)bset2);
2154 /* Special case of isl_map_intersect, where both map1 and map2
2155 * are convex, without any divs and such that either map1 or map2
2156 * contains a single constraint. This constraint is then simply
2157 * added to the other map.
2159 static __isl_give isl_map *map_intersect_add_constraint(
2160 __isl_take isl_map *map1, __isl_take isl_map *map2)
2162 struct isl_basic_map *bmap1;
2163 struct isl_basic_map *bmap2;
2165 isl_assert(map1->ctx, map1->n == 1, goto error);
2166 isl_assert(map2->ctx, map1->n == 1, goto error);
2167 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2168 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2170 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2171 return isl_map_intersect(map2, map1);
2173 isl_assert(map2->ctx,
2174 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2176 map1 = isl_map_cow(map1);
2179 if (isl_map_plain_is_empty(map1)) {
2183 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2184 if (map2->p[0]->n_eq == 1)
2185 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2187 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2188 map2->p[0]->ineq[0]);
2190 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2191 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2195 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2196 isl_basic_map_free(map1->p[0]);
2209 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2212 struct isl_map *result;
2218 if (isl_map_plain_is_empty(map1)) {
2222 if (isl_map_plain_is_empty(map2)) {
2227 if (map1->n == 1 && map2->n == 1 &&
2228 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2229 isl_dim_equal(map1->dim, map2->dim) &&
2230 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2231 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2232 return map_intersect_add_constraint(map1, map2);
2233 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2234 map2->dim, isl_dim_param), goto error);
2235 if (isl_dim_total(map1->dim) ==
2236 isl_dim_size(map1->dim, isl_dim_param) &&
2237 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2238 return isl_map_intersect(map2, map1);
2240 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2241 isl_assert(map1->ctx,
2242 isl_dim_equal(map1->dim, map2->dim), goto error);
2244 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2245 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2246 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2248 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2249 map1->n * map2->n, flags);
2252 for (i = 0; i < map1->n; ++i)
2253 for (j = 0; j < map2->n; ++j) {
2254 struct isl_basic_map *part;
2255 part = isl_basic_map_intersect(
2256 isl_basic_map_copy(map1->p[i]),
2257 isl_basic_map_copy(map2->p[j]));
2258 if (isl_basic_map_is_empty(part))
2259 isl_basic_map_free(part);
2261 result = isl_map_add_basic_map(result, part);
2274 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2276 return (struct isl_set *)
2277 isl_map_intersect((struct isl_map *)set1,
2278 (struct isl_map *)set2);
2281 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2283 struct isl_dim *dim;
2284 struct isl_basic_set *bset;
2289 bmap = isl_basic_map_cow(bmap);
2292 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2293 in = isl_basic_map_n_in(bmap);
2294 bset = isl_basic_set_from_basic_map(bmap);
2295 bset = isl_basic_set_swap_vars(bset, in);
2296 return isl_basic_map_from_basic_set(bset, dim);
2299 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2300 enum isl_dim_type type, unsigned pos, unsigned n)
2302 struct isl_dim *res_dim;
2303 struct isl_basic_map *res;
2304 struct isl_dim_map *dim_map;
2305 unsigned total, off;
2306 enum isl_dim_type t;
2314 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2316 total = isl_basic_map_total_dim(bmap) + n;
2317 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2319 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2321 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2323 unsigned size = isl_basic_map_dim(bmap, t);
2324 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2326 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2327 pos, size - pos, off + pos + n);
2329 off += isl_dim_size(res_dim, t);
2331 isl_dim_map_div(dim_map, bmap, off);
2333 res = isl_basic_map_alloc_dim(res_dim,
2334 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2335 if (isl_basic_map_is_rational(bmap))
2336 res = isl_basic_map_set_rational(res);
2337 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2338 return isl_basic_map_finalize(res);
2341 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2342 enum isl_dim_type type, unsigned n)
2346 return isl_basic_map_insert(bmap, type,
2347 isl_basic_map_dim(bmap, type), n);
2350 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2351 enum isl_dim_type type, unsigned n)
2355 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2356 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2358 isl_basic_set_free(bset);
2362 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2363 enum isl_dim_type type, unsigned pos, unsigned n)
2370 map = isl_map_cow(map);
2374 map->dim = isl_dim_insert(map->dim, type, pos, n);
2378 for (i = 0; i < map->n; ++i) {
2379 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2390 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2391 enum isl_dim_type type, unsigned pos, unsigned n)
2393 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2396 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2397 enum isl_dim_type type, unsigned n)
2401 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2404 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2405 enum isl_dim_type type, unsigned n)
2409 isl_assert(set->ctx, type != isl_dim_in, goto error);
2410 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2416 __isl_give isl_basic_map *isl_basic_map_move_dims(
2417 __isl_take isl_basic_map *bmap,
2418 enum isl_dim_type dst_type, unsigned dst_pos,
2419 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2422 struct isl_dim_map *dim_map;
2423 struct isl_basic_map *res;
2424 enum isl_dim_type t;
2425 unsigned total, off;
2432 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2435 if (dst_type == src_type && dst_pos == src_pos)
2438 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2440 if (pos(bmap->dim, dst_type) + dst_pos ==
2441 pos(bmap->dim, src_type) + src_pos +
2442 ((src_type < dst_type) ? n : 0)) {
2443 bmap = isl_basic_map_cow(bmap);
2447 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2448 src_type, src_pos, n);
2452 bmap = isl_basic_map_finalize(bmap);
2457 total = isl_basic_map_total_dim(bmap);
2458 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2461 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2462 unsigned size = isl_dim_size(bmap->dim, t);
2463 if (t == dst_type) {
2464 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2467 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2470 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2471 dst_pos, size - dst_pos, off);
2472 off += size - dst_pos;
2473 } else if (t == src_type) {
2474 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2477 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2478 src_pos + n, size - src_pos - n, off);
2479 off += size - src_pos - n;
2481 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2485 isl_dim_map_div(dim_map, bmap, off);
2487 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2488 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2489 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2491 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2492 src_type, src_pos, n);
2496 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2497 bmap = isl_basic_map_gauss(bmap, NULL);
2498 bmap = isl_basic_map_finalize(bmap);
2502 isl_basic_map_free(bmap);
2506 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2507 enum isl_dim_type dst_type, unsigned dst_pos,
2508 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2510 return (isl_basic_set *)isl_basic_map_move_dims(
2511 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2514 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2515 enum isl_dim_type dst_type, unsigned dst_pos,
2516 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2520 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2521 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2522 src_type, src_pos, n);
2528 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2529 enum isl_dim_type dst_type, unsigned dst_pos,
2530 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2539 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2542 if (dst_type == src_type && dst_pos == src_pos)
2545 isl_assert(map->ctx, dst_type != src_type, goto error);
2547 map = isl_map_cow(map);
2551 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2555 for (i = 0; i < map->n; ++i) {
2556 map->p[i] = isl_basic_map_move_dims(map->p[i],
2558 src_type, src_pos, n);
2569 /* Move the specified dimensions to the last columns right before
2570 * the divs. Don't change the dimension specification of bmap.
2571 * That's the responsibility of the caller.
2573 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2574 enum isl_dim_type type, unsigned first, unsigned n)
2576 struct isl_dim_map *dim_map;
2577 struct isl_basic_map *res;
2578 enum isl_dim_type t;
2579 unsigned total, off;
2583 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2586 total = isl_basic_map_total_dim(bmap);
2587 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2590 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2591 unsigned size = isl_dim_size(bmap->dim, t);
2593 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2596 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2597 first, n, total - bmap->n_div - n);
2598 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2599 first + n, size - (first + n), off);
2600 off += size - (first + n);
2602 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2606 isl_dim_map_div(dim_map, bmap, off + n);
2608 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2609 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2610 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2614 /* Turn the n dimensions of type type, starting at first
2615 * into existentially quantified variables.
2617 __isl_give isl_basic_map *isl_basic_map_project_out(
2618 __isl_take isl_basic_map *bmap,
2619 enum isl_dim_type type, unsigned first, unsigned n)
2632 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2633 return isl_basic_map_remove_dims(bmap, type, first, n);
2635 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2638 bmap = move_last(bmap, type, first, n);
2639 bmap = isl_basic_map_cow(bmap);
2643 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2644 old = bmap->block2.data;
2645 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2646 (bmap->extra + n) * (1 + row_size));
2647 if (!bmap->block2.data)
2649 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2652 for (i = 0; i < n; ++i) {
2653 new_div[i] = bmap->block2.data +
2654 (bmap->extra + i) * (1 + row_size);
2655 isl_seq_clr(new_div[i], 1 + row_size);
2657 for (i = 0; i < bmap->extra; ++i)
2658 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2660 bmap->div = new_div;
2664 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2667 bmap = isl_basic_map_simplify(bmap);
2668 bmap = isl_basic_map_drop_redundant_divs(bmap);
2669 return isl_basic_map_finalize(bmap);
2671 isl_basic_map_free(bmap);
2675 /* Turn the n dimensions of type type, starting at first
2676 * into existentially quantified variables.
2678 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2679 enum isl_dim_type type, unsigned first, unsigned n)
2681 return (isl_basic_set *)isl_basic_map_project_out(
2682 (isl_basic_map *)bset, type, first, n);
2685 /* Turn the n dimensions of type type, starting at first
2686 * into existentially quantified variables.
2688 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2689 enum isl_dim_type type, unsigned first, unsigned n)
2699 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2701 map = isl_map_cow(map);
2705 map->dim = isl_dim_drop(map->dim, type, first, n);
2709 for (i = 0; i < map->n; ++i) {
2710 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2721 /* Turn the n dimensions of type type, starting at first
2722 * into existentially quantified variables.
2724 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2725 enum isl_dim_type type, unsigned first, unsigned n)
2727 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2730 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2734 for (i = 0; i < n; ++i) {
2735 j = isl_basic_map_alloc_div(bmap);
2738 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2742 isl_basic_map_free(bmap);
2746 struct isl_basic_map *isl_basic_map_apply_range(
2747 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2749 struct isl_dim *dim_result = NULL;
2750 struct isl_basic_map *bmap;
2751 unsigned n_in, n_out, n, nparam, total, pos;
2752 struct isl_dim_map *dim_map1, *dim_map2;
2754 if (!bmap1 || !bmap2)
2757 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2758 isl_dim_copy(bmap2->dim));
2760 n_in = isl_basic_map_n_in(bmap1);
2761 n_out = isl_basic_map_n_out(bmap2);
2762 n = isl_basic_map_n_out(bmap1);
2763 nparam = isl_basic_map_n_param(bmap1);
2765 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2766 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2767 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2768 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2769 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2770 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2771 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2772 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2773 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2774 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2775 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2777 bmap = isl_basic_map_alloc_dim(dim_result,
2778 bmap1->n_div + bmap2->n_div + n,
2779 bmap1->n_eq + bmap2->n_eq,
2780 bmap1->n_ineq + bmap2->n_ineq);
2781 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2782 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2783 bmap = add_divs(bmap, n);
2784 bmap = isl_basic_map_simplify(bmap);
2785 bmap = isl_basic_map_drop_redundant_divs(bmap);
2786 return isl_basic_map_finalize(bmap);
2788 isl_basic_map_free(bmap1);
2789 isl_basic_map_free(bmap2);
2793 struct isl_basic_set *isl_basic_set_apply(
2794 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2799 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2802 return (struct isl_basic_set *)
2803 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2805 isl_basic_set_free(bset);
2806 isl_basic_map_free(bmap);
2810 struct isl_basic_map *isl_basic_map_apply_domain(
2811 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2813 if (!bmap1 || !bmap2)
2816 isl_assert(bmap1->ctx,
2817 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2818 isl_assert(bmap1->ctx,
2819 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2822 bmap1 = isl_basic_map_reverse(bmap1);
2823 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2824 return isl_basic_map_reverse(bmap1);
2826 isl_basic_map_free(bmap1);
2827 isl_basic_map_free(bmap2);
2831 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2832 * A \cap B -> f(A) + f(B)
2834 struct isl_basic_map *isl_basic_map_sum(
2835 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2837 unsigned n_in, n_out, nparam, total, pos;
2838 struct isl_basic_map *bmap = NULL;
2839 struct isl_dim_map *dim_map1, *dim_map2;
2842 if (!bmap1 || !bmap2)
2845 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2848 nparam = isl_basic_map_n_param(bmap1);
2849 n_in = isl_basic_map_n_in(bmap1);
2850 n_out = isl_basic_map_n_out(bmap1);
2852 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2853 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2854 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2855 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2856 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2857 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2858 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2859 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2860 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2861 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2862 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2864 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2865 bmap1->n_div + bmap2->n_div + 2 * n_out,
2866 bmap1->n_eq + bmap2->n_eq + n_out,
2867 bmap1->n_ineq + bmap2->n_ineq);
2868 for (i = 0; i < n_out; ++i) {
2869 int j = isl_basic_map_alloc_equality(bmap);
2872 isl_seq_clr(bmap->eq[j], 1+total);
2873 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2874 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2875 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2877 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2878 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2879 bmap = add_divs(bmap, 2 * n_out);
2881 bmap = isl_basic_map_simplify(bmap);
2882 return isl_basic_map_finalize(bmap);
2884 isl_basic_map_free(bmap);
2885 isl_basic_map_free(bmap1);
2886 isl_basic_map_free(bmap2);
2890 /* Given two maps A -> f(A) and B -> g(B), construct a map
2891 * A \cap B -> f(A) + f(B)
2893 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2895 struct isl_map *result;
2901 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2903 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2904 map1->n * map2->n, 0);
2907 for (i = 0; i < map1->n; ++i)
2908 for (j = 0; j < map2->n; ++j) {
2909 struct isl_basic_map *part;
2910 part = isl_basic_map_sum(
2911 isl_basic_map_copy(map1->p[i]),
2912 isl_basic_map_copy(map2->p[j]));
2913 if (isl_basic_map_is_empty(part))
2914 isl_basic_map_free(part);
2916 result = isl_map_add_basic_map(result, part);
2929 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2930 __isl_take isl_set *set2)
2932 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2935 /* Given a basic map A -> f(A), construct A -> -f(A).
2937 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2942 bmap = isl_basic_map_cow(bmap);
2946 n = isl_basic_map_dim(bmap, isl_dim_out);
2947 off = isl_basic_map_offset(bmap, isl_dim_out);
2948 for (i = 0; i < bmap->n_eq; ++i)
2949 for (j = 0; j < n; ++j)
2950 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2951 for (i = 0; i < bmap->n_ineq; ++i)
2952 for (j = 0; j < n; ++j)
2953 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2954 for (i = 0; i < bmap->n_div; ++i)
2955 for (j = 0; j < n; ++j)
2956 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2957 return isl_basic_map_finalize(bmap);
2960 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
2962 return isl_basic_map_neg(bset);
2965 /* Given a map A -> f(A), construct A -> -f(A).
2967 struct isl_map *isl_map_neg(struct isl_map *map)
2971 map = isl_map_cow(map);
2975 for (i = 0; i < map->n; ++i) {
2976 map->p[i] = isl_basic_map_neg(map->p[i]);
2987 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
2989 return (isl_set *)isl_map_neg((isl_map *)set);
2992 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2993 * A -> floor(f(A)/d).
2995 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2998 unsigned n_in, n_out, nparam, total, pos;
2999 struct isl_basic_map *result = NULL;
3000 struct isl_dim_map *dim_map;
3006 nparam = isl_basic_map_n_param(bmap);
3007 n_in = isl_basic_map_n_in(bmap);
3008 n_out = isl_basic_map_n_out(bmap);
3010 total = nparam + n_in + n_out + bmap->n_div + n_out;
3011 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3012 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3013 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3014 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3015 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3017 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3018 bmap->n_div + n_out,
3019 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3020 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3021 result = add_divs(result, n_out);
3022 for (i = 0; i < n_out; ++i) {
3024 j = isl_basic_map_alloc_inequality(result);
3027 isl_seq_clr(result->ineq[j], 1+total);
3028 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3029 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3030 j = isl_basic_map_alloc_inequality(result);
3033 isl_seq_clr(result->ineq[j], 1+total);
3034 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3035 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3036 isl_int_sub_ui(result->ineq[j][0], d, 1);
3039 result = isl_basic_map_simplify(result);
3040 return isl_basic_map_finalize(result);
3042 isl_basic_map_free(result);
3046 /* Given a map A -> f(A) and an integer d, construct a map
3047 * A -> floor(f(A)/d).
3049 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3053 map = isl_map_cow(map);
3057 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3058 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3059 for (i = 0; i < map->n; ++i) {
3060 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3071 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3077 i = isl_basic_map_alloc_equality(bmap);
3080 nparam = isl_basic_map_n_param(bmap);
3081 n_in = isl_basic_map_n_in(bmap);
3082 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3083 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3084 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3085 return isl_basic_map_finalize(bmap);
3087 isl_basic_map_free(bmap);
3091 /* Add a constraints to "bmap" expressing i_pos < o_pos
3093 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3099 i = isl_basic_map_alloc_inequality(bmap);
3102 nparam = isl_basic_map_n_param(bmap);
3103 n_in = isl_basic_map_n_in(bmap);
3104 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3105 isl_int_set_si(bmap->ineq[i][0], -1);
3106 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3107 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3108 return isl_basic_map_finalize(bmap);
3110 isl_basic_map_free(bmap);
3114 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3116 static __isl_give isl_basic_map *var_less_or_equal(
3117 __isl_take isl_basic_map *bmap, unsigned pos)
3123 i = isl_basic_map_alloc_inequality(bmap);
3126 nparam = isl_basic_map_n_param(bmap);
3127 n_in = isl_basic_map_n_in(bmap);
3128 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3129 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3130 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3131 return isl_basic_map_finalize(bmap);
3133 isl_basic_map_free(bmap);
3137 /* Add a constraints to "bmap" expressing i_pos > o_pos
3139 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3145 i = isl_basic_map_alloc_inequality(bmap);
3148 nparam = isl_basic_map_n_param(bmap);
3149 n_in = isl_basic_map_n_in(bmap);
3150 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3151 isl_int_set_si(bmap->ineq[i][0], -1);
3152 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3153 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3154 return isl_basic_map_finalize(bmap);
3156 isl_basic_map_free(bmap);
3160 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3162 static __isl_give isl_basic_map *var_more_or_equal(
3163 __isl_take isl_basic_map *bmap, unsigned pos)
3169 i = isl_basic_map_alloc_inequality(bmap);
3172 nparam = isl_basic_map_n_param(bmap);
3173 n_in = isl_basic_map_n_in(bmap);
3174 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3175 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3176 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3177 return isl_basic_map_finalize(bmap);
3179 isl_basic_map_free(bmap);
3183 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3186 struct isl_basic_map *bmap;
3187 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3190 for (i = 0; i < n_equal && bmap; ++i)
3191 bmap = var_equal(bmap, i);
3192 return isl_basic_map_finalize(bmap);
3195 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3197 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3200 struct isl_basic_map *bmap;
3201 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3204 for (i = 0; i < pos && bmap; ++i)
3205 bmap = var_equal(bmap, i);
3207 bmap = var_less(bmap, pos);
3208 return isl_basic_map_finalize(bmap);
3211 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3213 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3214 __isl_take isl_dim *dim, unsigned pos)
3217 isl_basic_map *bmap;
3219 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3220 for (i = 0; i < pos; ++i)
3221 bmap = var_equal(bmap, i);
3222 bmap = var_less_or_equal(bmap, pos);
3223 return isl_basic_map_finalize(bmap);
3226 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3228 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3231 struct isl_basic_map *bmap;
3232 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3235 for (i = 0; i < pos && bmap; ++i)
3236 bmap = var_equal(bmap, i);
3238 bmap = var_more(bmap, pos);
3239 return isl_basic_map_finalize(bmap);
3242 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3244 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3245 __isl_take isl_dim *dim, unsigned pos)
3248 isl_basic_map *bmap;
3250 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3251 for (i = 0; i < pos; ++i)
3252 bmap = var_equal(bmap, i);
3253 bmap = var_more_or_equal(bmap, pos);
3254 return isl_basic_map_finalize(bmap);
3257 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3258 unsigned n, int equal)
3260 struct isl_map *map;
3263 if (n == 0 && equal)
3264 return isl_map_universe(dims);
3266 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3268 for (i = 0; i + 1 < n; ++i)
3269 map = isl_map_add_basic_map(map,
3270 isl_basic_map_less_at(isl_dim_copy(dims), i));
3273 map = isl_map_add_basic_map(map,
3274 isl_basic_map_less_or_equal_at(dims, n - 1));
3276 map = isl_map_add_basic_map(map,
3277 isl_basic_map_less_at(dims, n - 1));
3284 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3288 return map_lex_lte_first(dims, dims->n_out, equal);
3291 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3293 return map_lex_lte_first(dim, n, 0);
3296 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3298 return map_lex_lte_first(dim, n, 1);
3301 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3303 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3306 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3308 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3311 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3312 unsigned n, int equal)
3314 struct isl_map *map;
3317 if (n == 0 && equal)
3318 return isl_map_universe(dims);
3320 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3322 for (i = 0; i + 1 < n; ++i)
3323 map = isl_map_add_basic_map(map,
3324 isl_basic_map_more_at(isl_dim_copy(dims), i));
3327 map = isl_map_add_basic_map(map,
3328 isl_basic_map_more_or_equal_at(dims, n - 1));
3330 map = isl_map_add_basic_map(map,
3331 isl_basic_map_more_at(dims, n - 1));
3338 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3342 return map_lex_gte_first(dims, dims->n_out, equal);
3345 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3347 return map_lex_gte_first(dim, n, 0);
3350 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3352 return map_lex_gte_first(dim, n, 1);
3355 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3357 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3360 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3362 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3365 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3366 __isl_take isl_set *set2)
3369 map = isl_map_lex_le(isl_set_get_dim(set1));
3370 map = isl_map_intersect_domain(map, set1);
3371 map = isl_map_intersect_range(map, set2);
3375 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3376 __isl_take isl_set *set2)
3379 map = isl_map_lex_lt(isl_set_get_dim(set1));
3380 map = isl_map_intersect_domain(map, set1);
3381 map = isl_map_intersect_range(map, set2);
3385 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3386 __isl_take isl_set *set2)
3389 map = isl_map_lex_ge(isl_set_get_dim(set1));
3390 map = isl_map_intersect_domain(map, set1);
3391 map = isl_map_intersect_range(map, set2);
3395 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3396 __isl_take isl_set *set2)
3399 map = isl_map_lex_gt(isl_set_get_dim(set1));
3400 map = isl_map_intersect_domain(map, set1);
3401 map = isl_map_intersect_range(map, set2);
3405 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3406 __isl_take isl_map *map2)
3409 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3410 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3411 map = isl_map_apply_range(map, isl_map_reverse(map2));
3415 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3416 __isl_take isl_map *map2)
3419 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3420 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3421 map = isl_map_apply_range(map, isl_map_reverse(map2));
3425 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3426 __isl_take isl_map *map2)
3429 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3430 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3431 map = isl_map_apply_range(map, isl_map_reverse(map2));
3435 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3436 __isl_take isl_map *map2)
3439 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3440 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3441 map = isl_map_apply_range(map, isl_map_reverse(map2));
3445 struct isl_basic_map *isl_basic_map_from_basic_set(
3446 struct isl_basic_set *bset, struct isl_dim *dim)
3448 struct isl_basic_map *bmap;
3450 bset = isl_basic_set_cow(bset);
3454 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3455 isl_dim_free(bset->dim);
3456 bmap = (struct isl_basic_map *) bset;
3458 return isl_basic_map_finalize(bmap);
3460 isl_basic_set_free(bset);
3465 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3469 if (bmap->dim->n_in == 0)
3470 return (struct isl_basic_set *)bmap;
3471 bmap = isl_basic_map_cow(bmap);
3474 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3477 bmap = isl_basic_map_finalize(bmap);
3478 return (struct isl_basic_set *)bmap;
3480 isl_basic_map_free(bmap);
3484 /* For a div d = floor(f/m), add the constraints
3487 * -(f-(n-1)) + m d >= 0
3489 * Note that the second constraint is the negation of
3493 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3494 unsigned pos, isl_int *div)
3497 unsigned total = isl_basic_map_total_dim(bmap);
3499 i = isl_basic_map_alloc_inequality(bmap);
3502 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3503 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3505 j = isl_basic_map_alloc_inequality(bmap);
3508 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3509 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3510 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3514 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3515 unsigned pos, isl_int *div)
3517 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3521 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3523 unsigned total = isl_basic_map_total_dim(bmap);
3524 unsigned div_pos = total - bmap->n_div + div;
3526 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3530 struct isl_basic_set *isl_basic_map_underlying_set(
3531 struct isl_basic_map *bmap)
3535 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3537 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3538 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3539 return (struct isl_basic_set *)bmap;
3540 bmap = isl_basic_map_cow(bmap);
3543 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3546 bmap->extra -= bmap->n_div;
3548 bmap = isl_basic_map_finalize(bmap);
3549 return (struct isl_basic_set *)bmap;
3554 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3555 __isl_take isl_basic_set *bset)
3557 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3560 struct isl_basic_map *isl_basic_map_overlying_set(
3561 struct isl_basic_set *bset, struct isl_basic_map *like)
3563 struct isl_basic_map *bmap;
3564 struct isl_ctx *ctx;
3571 isl_assert(ctx, bset->n_div == 0, goto error);
3572 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3573 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3575 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3576 isl_basic_map_free(like);
3577 return (struct isl_basic_map *)bset;
3579 bset = isl_basic_set_cow(bset);
3582 total = bset->dim->n_out + bset->extra;
3583 bmap = (struct isl_basic_map *)bset;
3584 isl_dim_free(bmap->dim);
3585 bmap->dim = isl_dim_copy(like->dim);
3588 bmap->n_div = like->n_div;
3589 bmap->extra += like->n_div;
3593 ltotal = total - bmap->extra + like->extra;
3596 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3597 bmap->extra * (1 + 1 + total));
3598 if (isl_blk_is_error(bmap->block2))
3600 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3604 for (i = 0; i < bmap->extra; ++i)
3605 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3606 for (i = 0; i < like->n_div; ++i) {
3607 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3608 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3610 bmap = isl_basic_map_extend_constraints(bmap,
3611 0, 2 * like->n_div);
3612 for (i = 0; i < like->n_div; ++i) {
3613 if (isl_int_is_zero(bmap->div[i][0]))
3615 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3619 isl_basic_map_free(like);
3620 bmap = isl_basic_map_simplify(bmap);
3621 bmap = isl_basic_map_finalize(bmap);
3624 isl_basic_map_free(like);
3625 isl_basic_set_free(bset);
3629 struct isl_basic_set *isl_basic_set_from_underlying_set(
3630 struct isl_basic_set *bset, struct isl_basic_set *like)
3632 return (struct isl_basic_set *)
3633 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3636 struct isl_set *isl_set_from_underlying_set(
3637 struct isl_set *set, struct isl_basic_set *like)
3643 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3645 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3646 isl_basic_set_free(like);
3649 set = isl_set_cow(set);
3652 for (i = 0; i < set->n; ++i) {
3653 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3654 isl_basic_set_copy(like));
3658 isl_dim_free(set->dim);
3659 set->dim = isl_dim_copy(like->dim);
3662 isl_basic_set_free(like);
3665 isl_basic_set_free(like);
3670 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3674 map = isl_map_cow(map);
3677 map->dim = isl_dim_cow(map->dim);
3681 for (i = 1; i < map->n; ++i)
3682 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3684 for (i = 0; i < map->n; ++i) {
3685 map->p[i] = (struct isl_basic_map *)
3686 isl_basic_map_underlying_set(map->p[i]);
3691 map->dim = isl_dim_underlying(map->dim, 0);
3693 isl_dim_free(map->dim);
3694 map->dim = isl_dim_copy(map->p[0]->dim);
3698 return (struct isl_set *)map;
3704 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3706 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3709 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3710 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3712 bmap = isl_basic_map_cow(bmap);
3716 isl_dim_free(bmap->dim);
3719 bmap = isl_basic_map_finalize(bmap);
3723 isl_basic_map_free(bmap);
3728 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3729 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3731 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3735 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3736 __isl_take isl_dim *dim)
3740 map = isl_map_cow(map);
3744 for (i = 0; i < map->n; ++i) {
3745 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3750 isl_dim_free(map->dim);
3760 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3761 __isl_take isl_dim *dim)
3763 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3766 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3769 struct isl_basic_set *domain;
3775 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3777 n_in = isl_basic_map_n_in(bmap);
3778 n_out = isl_basic_map_n_out(bmap);
3779 domain = isl_basic_set_from_basic_map(bmap);
3780 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3782 domain = isl_basic_set_reset_dim(domain, dim);
3787 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3789 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3792 __isl_give isl_basic_map *isl_basic_map_domain_map(
3793 __isl_take isl_basic_map *bmap)
3797 isl_basic_map *domain;
3798 isl_basic_set *bset;
3799 int nparam, n_in, n_out;
3802 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3803 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3804 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3806 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3807 domain = isl_basic_map_universe(dim);
3809 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3810 bmap = isl_basic_map_apply_range(bmap, domain);
3811 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3813 total = isl_basic_map_total_dim(bmap);
3815 for (i = 0; i < n_in; ++i) {
3816 k = isl_basic_map_alloc_equality(bmap);
3819 isl_seq_clr(bmap->eq[k], 1 + total);
3820 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3821 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3824 bmap = isl_basic_map_gauss(bmap, NULL);
3825 return isl_basic_map_finalize(bmap);
3827 isl_basic_map_free(bmap);
3831 __isl_give isl_basic_map *isl_basic_map_range_map(
3832 __isl_take isl_basic_map *bmap)
3836 isl_basic_map *range;
3837 isl_basic_set *bset;
3838 int nparam, n_in, n_out;
3841 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3842 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3843 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3845 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3846 range = isl_basic_map_universe(dim);
3848 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3849 bmap = isl_basic_map_apply_range(bmap, range);
3850 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3852 total = isl_basic_map_total_dim(bmap);
3854 for (i = 0; i < n_out; ++i) {
3855 k = isl_basic_map_alloc_equality(bmap);
3858 isl_seq_clr(bmap->eq[k], 1 + total);
3859 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3860 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3863 bmap = isl_basic_map_gauss(bmap, NULL);
3864 return isl_basic_map_finalize(bmap);
3866 isl_basic_map_free(bmap);
3870 struct isl_set *isl_map_range(struct isl_map *map)
3873 struct isl_set *set;
3877 if (isl_map_dim(map, isl_dim_in) == 0 &&
3878 !isl_dim_is_named_or_nested(map->dim, isl_dim_in))
3879 return (isl_set *)map;
3881 map = isl_map_cow(map);
3885 set = (struct isl_set *) map;
3886 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3889 for (i = 0; i < map->n; ++i) {
3890 set->p[i] = isl_basic_map_range(map->p[i]);
3894 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3895 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3902 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3905 isl_dim *domain_dim;
3907 map = isl_map_cow(map);
3911 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3912 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3913 map->dim = isl_dim_join(map->dim, domain_dim);
3916 for (i = 0; i < map->n; ++i) {
3917 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3921 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3922 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3929 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3934 map = isl_map_cow(map);
3938 range_dim = isl_dim_range(isl_map_get_dim(map));
3939 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3940 map->dim = isl_dim_join(map->dim, range_dim);
3943 for (i = 0; i < map->n; ++i) {
3944 map->p[i] = isl_basic_map_range_map(map->p[i]);
3948 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3949 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3956 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3959 struct isl_map *map = NULL;
3961 set = isl_set_cow(set);
3964 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3965 map = (struct isl_map *)set;
3966 for (i = 0; i < set->n; ++i) {
3967 map->p[i] = isl_basic_map_from_basic_set(
3968 set->p[i], isl_dim_copy(dim));
3972 isl_dim_free(map->dim);
3981 __isl_give isl_basic_map *isl_basic_map_from_domain(
3982 __isl_take isl_basic_set *bset)
3984 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
3987 __isl_give isl_basic_map *isl_basic_map_from_range(
3988 __isl_take isl_basic_set *bset)
3990 return (isl_basic_map *)bset;
3993 struct isl_map *isl_map_from_range(struct isl_set *set)
3995 return (struct isl_map *)set;
3998 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4000 return isl_map_reverse(isl_map_from_range(set));
4003 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4004 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4006 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4007 isl_basic_map_from_range(range));
4010 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4011 __isl_take isl_set *range)
4013 return isl_map_apply_range(isl_map_from_domain(domain),
4014 isl_map_from_range(range));
4017 struct isl_set *isl_set_from_map(struct isl_map *map)
4020 struct isl_set *set = NULL;
4024 map = isl_map_cow(map);
4027 map->dim = isl_dim_as_set_dim(map->dim);
4030 set = (struct isl_set *)map;
4031 for (i = 0; i < map->n; ++i) {
4032 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4042 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4044 struct isl_map *map;
4048 isl_assert(dim->ctx, n >= 0, return NULL);
4049 map = isl_alloc(dim->ctx, struct isl_map,
4050 sizeof(struct isl_map) +
4051 (n - 1) * sizeof(struct isl_basic_map *));
4055 map->ctx = dim->ctx;
4056 isl_ctx_ref(map->ctx);
4068 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4069 unsigned nparam, unsigned in, unsigned out, int n,
4072 struct isl_map *map;
4073 struct isl_dim *dims;
4075 dims = isl_dim_alloc(ctx, nparam, in, out);
4079 map = isl_map_alloc_dim(dims, n, flags);
4083 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4085 struct isl_basic_map *bmap;
4086 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4087 bmap = isl_basic_map_set_to_empty(bmap);
4091 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4093 struct isl_basic_set *bset;
4094 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4095 bset = isl_basic_set_set_to_empty(bset);
4099 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4101 struct isl_basic_map *bmap;
4104 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4105 bmap = isl_basic_map_set_to_empty(bmap);
4109 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4111 struct isl_basic_map *bmap;
4114 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4115 bmap = isl_basic_map_set_to_empty(bmap);
4119 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4121 struct isl_basic_set *bset;
4124 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4125 bset = isl_basic_set_set_to_empty(bset);
4129 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4131 struct isl_basic_map *bmap;
4132 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4133 bmap = isl_basic_map_finalize(bmap);
4137 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4139 struct isl_basic_set *bset;
4140 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4141 bset = isl_basic_set_finalize(bset);
4145 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4148 unsigned total = isl_dim_total(dim);
4149 isl_basic_map *bmap;
4151 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4152 for (i = 0; i < total; ++i) {
4153 int k = isl_basic_map_alloc_inequality(bmap);
4156 isl_seq_clr(bmap->ineq[k], 1 + total);
4157 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4161 isl_basic_map_free(bmap);
4165 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4167 return isl_basic_map_nat_universe(dim);
4170 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4172 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4175 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4177 return isl_map_nat_universe(dim);
4180 __isl_give isl_basic_map *isl_basic_map_universe_like(
4181 __isl_keep isl_basic_map *model)
4185 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4188 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4192 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4195 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4196 __isl_keep isl_set *model)
4200 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4203 struct isl_map *isl_map_empty(struct isl_dim *dim)
4205 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4208 struct isl_map *isl_map_empty_like(struct isl_map *model)
4212 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4215 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4219 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4222 struct isl_set *isl_set_empty(struct isl_dim *dim)
4224 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4227 struct isl_set *isl_set_empty_like(struct isl_set *model)
4231 return isl_set_empty(isl_dim_copy(model->dim));
4234 struct isl_map *isl_map_universe(struct isl_dim *dim)
4236 struct isl_map *map;
4239 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4240 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4244 struct isl_set *isl_set_universe(struct isl_dim *dim)
4246 struct isl_set *set;
4249 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4250 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4254 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4258 return isl_set_universe(isl_dim_copy(model->dim));
4261 struct isl_map *isl_map_dup(struct isl_map *map)
4264 struct isl_map *dup;
4268 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4269 for (i = 0; i < map->n; ++i)
4270 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4274 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4275 __isl_take isl_basic_map *bmap)
4279 if (isl_basic_map_plain_is_empty(bmap)) {
4280 isl_basic_map_free(bmap);
4283 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4284 isl_assert(map->ctx, map->n < map->size, goto error);
4285 map->p[map->n] = bmap;
4287 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4293 isl_basic_map_free(bmap);
4297 void isl_map_free(struct isl_map *map)
4307 isl_ctx_deref(map->ctx);
4308 for (i = 0; i < map->n; ++i)
4309 isl_basic_map_free(map->p[i]);
4310 isl_dim_free(map->dim);
4314 struct isl_map *isl_map_extend(struct isl_map *base,
4315 unsigned nparam, unsigned n_in, unsigned n_out)
4319 base = isl_map_cow(base);
4323 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4326 for (i = 0; i < base->n; ++i) {
4327 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4328 isl_dim_copy(base->dim), 0, 0, 0);
4338 struct isl_set *isl_set_extend(struct isl_set *base,
4339 unsigned nparam, unsigned dim)
4341 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4345 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4346 struct isl_basic_map *bmap, unsigned pos, int value)
4350 bmap = isl_basic_map_cow(bmap);
4351 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4352 j = isl_basic_map_alloc_equality(bmap);
4355 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4356 isl_int_set_si(bmap->eq[j][pos], -1);
4357 isl_int_set_si(bmap->eq[j][0], value);
4358 bmap = isl_basic_map_simplify(bmap);
4359 return isl_basic_map_finalize(bmap);
4361 isl_basic_map_free(bmap);
4365 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4366 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4370 bmap = isl_basic_map_cow(bmap);
4371 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4372 j = isl_basic_map_alloc_equality(bmap);
4375 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4376 isl_int_set_si(bmap->eq[j][pos], -1);
4377 isl_int_set(bmap->eq[j][0], value);
4378 bmap = isl_basic_map_simplify(bmap);
4379 return isl_basic_map_finalize(bmap);
4381 isl_basic_map_free(bmap);
4385 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4386 enum isl_dim_type type, unsigned pos, int value)
4390 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4391 return isl_basic_map_fix_pos_si(bmap,
4392 isl_basic_map_offset(bmap, type) + pos, value);
4394 isl_basic_map_free(bmap);
4398 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4399 enum isl_dim_type type, unsigned pos, isl_int value)
4403 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4404 return isl_basic_map_fix_pos(bmap,
4405 isl_basic_map_offset(bmap, type) + pos, value);
4407 isl_basic_map_free(bmap);
4411 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4412 enum isl_dim_type type, unsigned pos, int value)
4414 return (struct isl_basic_set *)
4415 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4419 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4420 enum isl_dim_type type, unsigned pos, isl_int value)
4422 return (struct isl_basic_set *)
4423 isl_basic_map_fix((struct isl_basic_map *)bset,
4427 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4428 unsigned input, int value)
4430 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4433 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4434 unsigned dim, int value)
4436 return (struct isl_basic_set *)
4437 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4438 isl_dim_set, dim, value);
4441 struct isl_map *isl_map_fix_si(struct isl_map *map,
4442 enum isl_dim_type type, unsigned pos, int value)
4446 map = isl_map_cow(map);
4450 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4451 for (i = 0; i < map->n; ++i) {
4452 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4456 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4463 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4464 enum isl_dim_type type, unsigned pos, int value)
4466 return (struct isl_set *)
4467 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4470 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4471 enum isl_dim_type type, unsigned pos, isl_int value)
4475 map = isl_map_cow(map);
4479 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4480 for (i = 0; i < map->n; ++i) {
4481 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4485 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4492 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4493 enum isl_dim_type type, unsigned pos, isl_int value)
4495 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4498 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4499 unsigned input, int value)
4501 return isl_map_fix_si(map, isl_dim_in, input, value);
4504 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4506 return (struct isl_set *)
4507 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4510 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4511 __isl_take isl_basic_map *bmap,
4512 enum isl_dim_type type, unsigned pos, int value)
4518 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4519 pos += isl_basic_map_offset(bmap, type);
4520 bmap = isl_basic_map_cow(bmap);
4521 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4522 j = isl_basic_map_alloc_inequality(bmap);
4525 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4526 isl_int_set_si(bmap->ineq[j][pos], 1);
4527 isl_int_set_si(bmap->ineq[j][0], -value);
4528 bmap = isl_basic_map_simplify(bmap);
4529 return isl_basic_map_finalize(bmap);
4531 isl_basic_map_free(bmap);
4535 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4536 unsigned dim, isl_int value)
4540 bset = isl_basic_set_cow(bset);
4541 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4542 j = isl_basic_set_alloc_inequality(bset);
4545 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4546 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4547 isl_int_neg(bset->ineq[j][0], value);
4548 bset = isl_basic_set_simplify(bset);
4549 return isl_basic_set_finalize(bset);
4551 isl_basic_set_free(bset);
4555 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4556 enum isl_dim_type type, unsigned pos, int value)
4560 map = isl_map_cow(map);
4564 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4565 for (i = 0; i < map->n; ++i) {
4566 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4571 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4578 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4579 enum isl_dim_type type, unsigned pos, int value)
4581 return (struct isl_set *)
4582 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4585 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4590 set = isl_set_cow(set);
4594 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4595 for (i = 0; i < set->n; ++i) {
4596 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4606 struct isl_map *isl_map_reverse(struct isl_map *map)
4610 map = isl_map_cow(map);
4614 map->dim = isl_dim_reverse(map->dim);
4617 for (i = 0; i < map->n; ++i) {
4618 map->p[i] = isl_basic_map_reverse(map->p[i]);
4622 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4629 static struct isl_map *isl_basic_map_partial_lexopt(
4630 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4631 struct isl_set **empty, int max)
4635 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4636 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4638 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4640 isl_basic_map_free(bmap);
4641 isl_basic_set_free(dom);
4647 struct isl_map *isl_basic_map_partial_lexmax(
4648 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4649 struct isl_set **empty)
4651 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4654 struct isl_map *isl_basic_map_partial_lexmin(
4655 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4656 struct isl_set **empty)
4658 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4661 struct isl_set *isl_basic_set_partial_lexmin(
4662 struct isl_basic_set *bset, struct isl_basic_set *dom,
4663 struct isl_set **empty)
4665 return (struct isl_set *)
4666 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4670 struct isl_set *isl_basic_set_partial_lexmax(
4671 struct isl_basic_set *bset, struct isl_basic_set *dom,
4672 struct isl_set **empty)
4674 return (struct isl_set *)
4675 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4679 /* Given a basic map "bmap", compute the lexicographically minimal
4680 * (or maximal) image element for each domain element in dom.
4681 * Set *empty to those elements in dom that do not have an image element.
4683 * We first make sure the basic sets in dom are disjoint and then
4684 * simply collect the results over each of the basic sets separately.
4685 * We could probably improve the efficiency a bit by moving the union
4686 * domain down into the parametric integer programming.
4688 static __isl_give isl_map *basic_map_partial_lexopt(
4689 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4690 __isl_give isl_set **empty, int max)
4693 struct isl_map *res;
4695 dom = isl_set_make_disjoint(dom);
4699 if (isl_set_plain_is_empty(dom)) {
4700 res = isl_map_empty_like_basic_map(bmap);
4701 *empty = isl_set_empty_like(dom);
4703 isl_basic_map_free(bmap);
4707 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4708 isl_basic_set_copy(dom->p[0]), empty, max);
4710 for (i = 1; i < dom->n; ++i) {
4711 struct isl_map *res_i;
4712 struct isl_set *empty_i;
4714 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4715 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4717 res = isl_map_union_disjoint(res, res_i);
4718 *empty = isl_set_union_disjoint(*empty, empty_i);
4722 isl_basic_map_free(bmap);
4727 isl_basic_map_free(bmap);
4731 /* Given a map "map", compute the lexicographically minimal
4732 * (or maximal) image element for each domain element in dom.
4733 * Set *empty to those elements in dom that do not have an image element.
4735 * We first compute the lexicographically minimal or maximal element
4736 * in the first basic map. This results in a partial solution "res"
4737 * and a subset "todo" of dom that still need to be handled.
4738 * We then consider each of the remaining maps in "map" and successively
4739 * improve both "res" and "todo".
4741 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4742 * Assume we are computing the lexicographical maximum.
4743 * We first compute the lexicographically maximal element in basic map i.
4744 * This results in a partial solution res_i and a subset todo_i.
4745 * Then we combine these results with those obtain for the first k basic maps
4746 * to obtain a result that is valid for the first k+1 basic maps.
4747 * In particular, the set where there is no solution is the set where
4748 * there is no solution for the first k basic maps and also no solution
4749 * for the ith basic map, i.e.,
4751 * todo^i = todo^k * todo_i
4753 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4754 * solutions, arbitrarily breaking ties in favor of res^k.
4755 * That is, when res^k(a) >= res_i(a), we pick res^k and
4756 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4757 * the lexicographic order.)
4758 * In practice, we compute
4760 * res^k * (res_i . "<=")
4764 * res_i * (res^k . "<")
4766 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4767 * where only one of res^k and res_i provides a solution and we simply pick
4774 * Note that we only compute these intersections when dom(res^k) intersects
4775 * dom(res_i). Otherwise, the only effect of these intersections is to
4776 * potentially break up res^k and res_i into smaller pieces.
4777 * We want to avoid such splintering as much as possible.
4778 * In fact, an earlier implementation of this function would look for
4779 * better results in the domain of res^k and for extra results in todo^k,
4780 * but this would always result in a splintering according to todo^k,
4781 * even when the domain of basic map i is disjoint from the domains of
4782 * the previous basic maps.
4784 static __isl_give isl_map *isl_map_partial_lexopt(
4785 __isl_take isl_map *map, __isl_take isl_set *dom,
4786 __isl_give isl_set **empty, int max)
4789 struct isl_map *res;
4790 struct isl_set *todo;
4795 if (isl_map_plain_is_empty(map)) {
4803 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4804 isl_set_copy(dom), &todo, max);
4806 for (i = 1; i < map->n; ++i) {
4810 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4812 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4813 isl_set_copy(dom), &todo_i, max);
4816 lt = isl_map_lex_lt(isl_dim_copy(dim));
4817 le = isl_map_lex_le(dim);
4819 lt = isl_map_lex_gt(isl_dim_copy(dim));
4820 le = isl_map_lex_ge(dim);
4822 lt = isl_map_apply_range(isl_map_copy(res), lt);
4823 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4824 le = isl_map_apply_range(isl_map_copy(res_i), le);
4825 le = isl_map_intersect(le, isl_map_copy(res));
4827 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4828 res = isl_map_intersect_domain(res,
4829 isl_set_copy(todo_i));
4830 res_i = isl_map_intersect_domain(res_i,
4831 isl_set_copy(todo));
4834 res = isl_map_union_disjoint(res, res_i);
4835 res = isl_map_union_disjoint(res, lt);
4836 res = isl_map_union_disjoint(res, le);
4838 todo = isl_set_intersect(todo, todo_i);
4858 __isl_give isl_map *isl_map_partial_lexmax(
4859 __isl_take isl_map *map, __isl_take isl_set *dom,
4860 __isl_give isl_set **empty)
4862 return isl_map_partial_lexopt(map, dom, empty, 1);
4865 __isl_give isl_map *isl_map_partial_lexmin(
4866 __isl_take isl_map *map, __isl_take isl_set *dom,
4867 __isl_give isl_set **empty)
4869 return isl_map_partial_lexopt(map, dom, empty, 0);
4872 __isl_give isl_set *isl_set_partial_lexmin(
4873 __isl_take isl_set *set, __isl_take isl_set *dom,
4874 __isl_give isl_set **empty)
4876 return (struct isl_set *)
4877 isl_map_partial_lexmin((struct isl_map *)set,
4881 __isl_give isl_set *isl_set_partial_lexmax(
4882 __isl_take isl_set *set, __isl_take isl_set *dom,
4883 __isl_give isl_set **empty)
4885 return (struct isl_set *)
4886 isl_map_partial_lexmax((struct isl_map *)set,
4890 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4892 struct isl_basic_set *dom = NULL;
4893 struct isl_dim *dom_dim;
4897 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4898 dom = isl_basic_set_universe(dom_dim);
4899 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4901 isl_basic_map_free(bmap);
4905 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4907 return isl_basic_map_lexopt(bmap, 0);
4910 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4912 return isl_basic_map_lexopt(bmap, 1);
4915 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4917 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4920 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4922 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4925 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4927 struct isl_set *dom = NULL;
4928 struct isl_dim *dom_dim;
4932 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4933 dom = isl_set_universe(dom_dim);
4934 return isl_map_partial_lexopt(map, dom, NULL, max);
4940 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4942 return isl_map_lexopt(map, 0);
4945 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4947 return isl_map_lexopt(map, 1);
4950 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4952 return (isl_set *)isl_map_lexmin((isl_map *)set);
4955 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4957 return (isl_set *)isl_map_lexmax((isl_map *)set);
4960 /* Apply a preimage specified by "mat" on the parameters of "bset".
4961 * bset is assumed to have only parameters and divs.
4963 static struct isl_basic_set *basic_set_parameter_preimage(
4964 struct isl_basic_set *bset, struct isl_mat *mat)
4971 bset->dim = isl_dim_cow(bset->dim);
4975 nparam = isl_basic_set_dim(bset, isl_dim_param);
4977 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4979 bset->dim->nparam = 0;
4980 bset->dim->n_out = nparam;
4981 bset = isl_basic_set_preimage(bset, mat);
4983 bset->dim->nparam = bset->dim->n_out;
4984 bset->dim->n_out = 0;
4989 isl_basic_set_free(bset);
4993 /* Apply a preimage specified by "mat" on the parameters of "set".
4994 * set is assumed to have only parameters and divs.
4996 static struct isl_set *set_parameter_preimage(
4997 struct isl_set *set, struct isl_mat *mat)
4999 struct isl_dim *dim = NULL;
5005 dim = isl_dim_copy(set->dim);
5006 dim = isl_dim_cow(dim);
5010 nparam = isl_set_dim(set, isl_dim_param);
5012 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5015 dim->n_out = nparam;
5016 isl_set_reset_dim(set, dim);
5017 set = isl_set_preimage(set, mat);
5020 dim = isl_dim_copy(set->dim);
5021 dim = isl_dim_cow(dim);
5024 dim->nparam = dim->n_out;
5026 isl_set_reset_dim(set, dim);
5036 /* Intersect the basic set "bset" with the affine space specified by the
5037 * equalities in "eq".
5039 static struct isl_basic_set *basic_set_append_equalities(
5040 struct isl_basic_set *bset, struct isl_mat *eq)
5048 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5053 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5054 for (i = 0; i < eq->n_row; ++i) {
5055 k = isl_basic_set_alloc_equality(bset);
5058 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5059 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5063 bset = isl_basic_set_gauss(bset, NULL);
5064 bset = isl_basic_set_finalize(bset);
5069 isl_basic_set_free(bset);
5073 /* Intersect the set "set" with the affine space specified by the
5074 * equalities in "eq".
5076 static struct isl_set *set_append_equalities(struct isl_set *set,
5084 for (i = 0; i < set->n; ++i) {
5085 set->p[i] = basic_set_append_equalities(set->p[i],
5098 /* Project the given basic set onto its parameter domain, possibly introducing
5099 * new, explicit, existential variables in the constraints.
5100 * The input has parameters and (possibly implicit) existential variables.
5101 * The output has the same parameters, but only
5102 * explicit existentially quantified variables.
5104 * The actual projection is performed by pip, but pip doesn't seem
5105 * to like equalities very much, so we first remove the equalities
5106 * among the parameters by performing a variable compression on
5107 * the parameters. Afterward, an inverse transformation is performed
5108 * and the equalities among the parameters are inserted back in.
5110 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5114 struct isl_mat *T, *T2;
5115 struct isl_set *set;
5116 unsigned nparam, n_div;
5118 bset = isl_basic_set_cow(bset);
5122 if (bset->n_eq == 0)
5123 return isl_basic_set_lexmin(bset);
5125 isl_basic_set_gauss(bset, NULL);
5127 nparam = isl_basic_set_dim(bset, isl_dim_param);
5128 n_div = isl_basic_set_dim(bset, isl_dim_div);
5130 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5131 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5134 if (i == bset->n_eq)
5135 return isl_basic_set_lexmin(bset);
5137 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5139 eq = isl_mat_cow(eq);
5140 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5141 if (T && T->n_col == 0) {
5145 bset = isl_basic_set_set_to_empty(bset);
5146 return isl_set_from_basic_set(bset);
5148 bset = basic_set_parameter_preimage(bset, T);
5150 set = isl_basic_set_lexmin(bset);
5151 set = set_parameter_preimage(set, T2);
5152 set = set_append_equalities(set, eq);
5156 /* Compute an explicit representation for all the existentially
5157 * quantified variables.
5158 * The input and output dimensions are first turned into parameters.
5159 * compute_divs then returns a map with the same parameters and
5160 * no input or output dimensions and the dimension specification
5161 * is reset to that of the input.
5163 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5165 struct isl_basic_set *bset;
5166 struct isl_set *set;
5167 struct isl_map *map;
5168 struct isl_dim *dim, *orig_dim = NULL;
5173 bmap = isl_basic_map_cow(bmap);
5177 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5178 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5179 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5180 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5184 orig_dim = bmap->dim;
5186 bset = (struct isl_basic_set *)bmap;
5188 set = parameter_compute_divs(bset);
5189 map = (struct isl_map *)set;
5190 map = isl_map_reset_dim(map, orig_dim);
5194 isl_basic_map_free(bmap);
5198 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5206 off = isl_dim_total(bmap->dim);
5207 for (i = 0; i < bmap->n_div; ++i) {
5208 if (isl_int_is_zero(bmap->div[i][0]))
5210 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5216 static int map_divs_known(__isl_keep isl_map *map)
5223 for (i = 0; i < map->n; ++i) {
5224 int known = basic_map_divs_known(map->p[i]);
5232 /* If bmap contains any unknown divs, then compute explicit
5233 * expressions for them. However, this computation may be
5234 * quite expensive, so first try to remove divs that aren't
5237 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5241 struct isl_map *map;
5243 known = basic_map_divs_known(bmap);
5247 return isl_map_from_basic_map(bmap);
5249 bmap = isl_basic_map_drop_redundant_divs(bmap);
5251 known = basic_map_divs_known(bmap);
5255 return isl_map_from_basic_map(bmap);
5257 map = compute_divs(bmap);
5260 isl_basic_map_free(bmap);
5264 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5268 struct isl_map *res;
5275 known = map_divs_known(map);
5283 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5284 for (i = 1 ; i < map->n; ++i) {
5286 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5287 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5288 res = isl_map_union_disjoint(res, r2);
5290 res = isl_map_union(res, r2);
5297 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5299 return (struct isl_set *)
5300 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5303 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5305 return (struct isl_set *)
5306 isl_map_compute_divs((struct isl_map *)set);
5309 struct isl_set *isl_map_domain(struct isl_map *map)
5312 struct isl_set *set;
5317 map = isl_map_cow(map);
5321 set = (struct isl_set *)map;
5322 set->dim = isl_dim_domain(set->dim);
5325 for (i = 0; i < map->n; ++i) {
5326 set->p[i] = isl_basic_map_domain(map->p[i]);
5330 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5331 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5338 struct isl_map *isl_map_union_disjoint(
5339 struct isl_map *map1, struct isl_map *map2)
5343 struct isl_map *map = NULL;
5357 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5359 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5360 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5361 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5363 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5364 map1->n + map2->n, flags);
5367 for (i = 0; i < map1->n; ++i) {
5368 map = isl_map_add_basic_map(map,
5369 isl_basic_map_copy(map1->p[i]));
5373 for (i = 0; i < map2->n; ++i) {
5374 map = isl_map_add_basic_map(map,
5375 isl_basic_map_copy(map2->p[i]));
5389 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5391 map1 = isl_map_union_disjoint(map1, map2);
5395 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5399 struct isl_set *isl_set_union_disjoint(
5400 struct isl_set *set1, struct isl_set *set2)
5402 return (struct isl_set *)
5403 isl_map_union_disjoint(
5404 (struct isl_map *)set1, (struct isl_map *)set2);
5407 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5409 return (struct isl_set *)
5410 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5413 struct isl_map *isl_map_intersect_range(
5414 struct isl_map *map, struct isl_set *set)
5417 struct isl_map *result;
5423 if (!isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5424 isl_die(set->ctx, isl_error_invalid,
5425 "parameters don't match", goto error);
5427 if (isl_dim_size(set->dim, isl_dim_set) != 0 &&
5428 !isl_map_compatible_range(map, set))
5429 isl_die(set->ctx, isl_error_invalid,
5430 "incompatible spaces", goto error);
5432 if (isl_set_plain_is_universe(set)) {
5437 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5438 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5439 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5441 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5442 map->n * set->n, flags);
5445 for (i = 0; i < map->n; ++i)
5446 for (j = 0; j < set->n; ++j) {
5447 result = isl_map_add_basic_map(result,
5448 isl_basic_map_intersect_range(
5449 isl_basic_map_copy(map->p[i]),
5450 isl_basic_set_copy(set->p[j])));
5463 struct isl_map *isl_map_intersect_domain(
5464 struct isl_map *map, struct isl_set *set)
5466 return isl_map_reverse(
5467 isl_map_intersect_range(isl_map_reverse(map), set));
5470 struct isl_map *isl_map_apply_domain(
5471 struct isl_map *map1, struct isl_map *map2)
5475 map1 = isl_map_reverse(map1);
5476 map1 = isl_map_apply_range(map1, map2);
5477 return isl_map_reverse(map1);
5484 struct isl_map *isl_map_apply_range(
5485 struct isl_map *map1, struct isl_map *map2)
5487 struct isl_dim *dim_result;
5488 struct isl_map *result;
5494 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5495 isl_dim_copy(map2->dim));
5497 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5500 for (i = 0; i < map1->n; ++i)
5501 for (j = 0; j < map2->n; ++j) {
5502 result = isl_map_add_basic_map(result,
5503 isl_basic_map_apply_range(
5504 isl_basic_map_copy(map1->p[i]),
5505 isl_basic_map_copy(map2->p[j])));
5511 if (result && result->n <= 1)
5512 ISL_F_SET(result, ISL_MAP_DISJOINT);
5521 * returns range - domain
5523 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5525 isl_dim *dims, *target_dim;
5526 struct isl_basic_set *bset;
5533 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5534 bmap->dim, isl_dim_out),
5536 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5537 dim = isl_basic_map_n_in(bmap);
5538 nparam = isl_basic_map_n_param(bmap);
5539 bset = isl_basic_set_from_basic_map(bmap);
5540 bset = isl_basic_set_cow(bset);
5541 dims = isl_basic_set_get_dim(bset);
5542 dims = isl_dim_add(dims, isl_dim_set, dim);
5543 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5544 bset = isl_basic_set_swap_vars(bset, 2*dim);
5545 for (i = 0; i < dim; ++i) {
5546 int j = isl_basic_map_alloc_equality(
5547 (struct isl_basic_map *)bset);
5550 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5551 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5552 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5553 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5555 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5556 bset = isl_basic_set_reset_dim(bset, target_dim);
5559 isl_basic_map_free(bmap);
5564 * returns range - domain
5566 struct isl_set *isl_map_deltas(struct isl_map *map)
5570 struct isl_set *result;
5575 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5576 map->dim, isl_dim_out),
5578 dim = isl_map_get_dim(map);
5579 dim = isl_dim_domain(dim);
5580 result = isl_set_alloc_dim(dim, map->n, 0);
5583 for (i = 0; i < map->n; ++i)
5584 result = isl_set_add_basic_set(result,
5585 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5594 * returns [domain -> range] -> range - domain
5596 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5597 __isl_take isl_basic_map *bmap)
5601 isl_basic_map *domain;
5602 isl_basic_set *bset;
5606 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5607 isl_die(bmap->ctx, isl_error_invalid,
5608 "domain and range don't match", goto error);
5610 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5611 n = isl_basic_map_dim(bmap, isl_dim_in);
5613 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5614 domain = isl_basic_map_universe(dim);
5616 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5617 bmap = isl_basic_map_apply_range(bmap, domain);
5618 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5620 total = isl_basic_map_total_dim(bmap);
5622 for (i = 0; i < n; ++i) {
5623 k = isl_basic_map_alloc_equality(bmap);
5626 isl_seq_clr(bmap->eq[k], 1 + total);
5627 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5628 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5629 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5632 bmap = isl_basic_map_gauss(bmap, NULL);
5633 return isl_basic_map_finalize(bmap);
5635 isl_basic_map_free(bmap);
5640 * returns [domain -> range] -> range - domain
5642 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5645 isl_dim *domain_dim;
5650 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5651 isl_die(map->ctx, isl_error_invalid,
5652 "domain and range don't match", goto error);
5654 map = isl_map_cow(map);
5658 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5659 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5660 map->dim = isl_dim_join(map->dim, domain_dim);
5663 for (i = 0; i < map->n; ++i) {
5664 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5668 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5675 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5677 struct isl_basic_map *bmap;
5685 nparam = dims->nparam;
5687 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5691 for (i = 0; i < dim; ++i) {
5692 int j = isl_basic_map_alloc_equality(bmap);
5695 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5696 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5697 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5699 return isl_basic_map_finalize(bmap);
5701 isl_basic_map_free(bmap);
5705 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5709 if (dim->n_in != dim->n_out)
5710 isl_die(dim->ctx, isl_error_invalid,
5711 "number of input and output dimensions needs to be "
5712 "the same", goto error);
5713 return basic_map_identity(dim);
5719 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5721 if (!model || !model->dim)
5723 return isl_basic_map_identity(isl_dim_copy(model->dim));
5726 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5728 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5731 struct isl_map *isl_map_identity_like(struct isl_map *model)
5733 if (!model || !model->dim)
5735 return isl_map_identity(isl_dim_copy(model->dim));
5738 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5740 if (!model || !model->dim)
5742 return isl_map_identity(isl_dim_copy(model->dim));
5745 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5747 isl_dim *dim = isl_set_get_dim(set);
5749 id = isl_map_identity(isl_dim_map_from_set(dim));
5750 return isl_map_intersect_range(id, set);
5753 /* Construct a basic set with all set dimensions having only non-negative
5756 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5761 struct isl_basic_set *bset;
5765 nparam = dims->nparam;
5767 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5770 for (i = 0; i < dim; ++i) {
5771 int k = isl_basic_set_alloc_inequality(bset);
5774 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5775 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5779 isl_basic_set_free(bset);
5783 /* Construct the half-space x_pos >= 0.
5785 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5789 isl_basic_set *nonneg;
5791 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5792 k = isl_basic_set_alloc_inequality(nonneg);
5795 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5796 isl_int_set_si(nonneg->ineq[k][pos], 1);
5798 return isl_basic_set_finalize(nonneg);
5800 isl_basic_set_free(nonneg);
5804 /* Construct the half-space x_pos <= -1.
5806 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5811 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5812 k = isl_basic_set_alloc_inequality(neg);
5815 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5816 isl_int_set_si(neg->ineq[k][0], -1);
5817 isl_int_set_si(neg->ineq[k][pos], -1);
5819 return isl_basic_set_finalize(neg);
5821 isl_basic_set_free(neg);
5825 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5826 enum isl_dim_type type, unsigned first, unsigned n)
5829 isl_basic_set *nonneg;
5837 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5839 for (i = 0; i < n; ++i) {
5840 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5841 pos(set->dim, type) + first + i);
5842 neg = neg_halfspace(isl_set_get_dim(set),
5843 pos(set->dim, type) + first + i);
5845 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5854 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5855 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5862 if (isl_set_plain_is_empty(set)) {
5867 return fn(set, signs, user);
5870 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5872 half = isl_set_intersect(half, isl_set_copy(set));
5873 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5877 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5879 half = isl_set_intersect(half, set);
5880 return foreach_orthant(half, signs, first + 1, len, fn, user);
5886 /* Call "fn" on the intersections of "set" with each of the orthants
5887 * (except for obviously empty intersections). The orthant is identified
5888 * by the signs array, with each entry having value 1 or -1 according
5889 * to the sign of the corresponding variable.
5891 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5892 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5902 if (isl_set_plain_is_empty(set))
5905 nparam = isl_set_dim(set, isl_dim_param);
5906 nvar = isl_set_dim(set, isl_dim_set);
5908 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5910 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5918 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5920 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5923 int isl_basic_map_is_subset(
5924 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5927 struct isl_map *map1;
5928 struct isl_map *map2;
5930 if (!bmap1 || !bmap2)
5933 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5934 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5936 is_subset = isl_map_is_subset(map1, map2);
5944 int isl_basic_map_is_equal(
5945 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5949 if (!bmap1 || !bmap2)
5951 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5954 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5958 int isl_basic_set_is_equal(
5959 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5961 return isl_basic_map_is_equal(
5962 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5965 int isl_map_is_empty(struct isl_map *map)
5972 for (i = 0; i < map->n; ++i) {
5973 is_empty = isl_basic_map_is_empty(map->p[i]);
5982 int isl_map_plain_is_empty(__isl_keep isl_map *map)
5984 return map ? map->n == 0 : -1;
5987 int isl_map_fast_is_empty(__isl_keep isl_map *map)
5989 return isl_map_plain_is_empty(map);
5992 int isl_set_plain_is_empty(struct isl_set *set)
5994 return set ? set->n == 0 : -1;
5997 int isl_set_fast_is_empty(__isl_keep isl_set *set)
5999 return isl_set_plain_is_empty(set);
6002 int isl_set_is_empty(struct isl_set *set)
6004 return isl_map_is_empty((struct isl_map *)set);
6007 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6012 return isl_dim_equal(map1->dim, map2->dim);
6015 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6020 return isl_dim_equal(set1->dim, set2->dim);
6023 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6029 is_subset = isl_map_is_subset(map1, map2);
6032 is_subset = isl_map_is_subset(map2, map1);
6036 int isl_basic_map_is_strict_subset(
6037 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6041 if (!bmap1 || !bmap2)
6043 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6046 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6047 if (is_subset == -1)
6052 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6058 is_subset = isl_map_is_subset(map1, map2);
6061 is_subset = isl_map_is_subset(map2, map1);
6062 if (is_subset == -1)
6067 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6069 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6072 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6076 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6079 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6083 return bset->n_eq == 0 && bset->n_ineq == 0;
6086 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6093 for (i = 0; i < map->n; ++i) {
6094 int r = isl_basic_map_is_universe(map->p[i]);
6102 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6104 return isl_map_plain_is_universe((isl_map *) set);
6107 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6109 return isl_set_plain_is_universe(set);
6112 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6114 struct isl_basic_set *bset = NULL;
6115 struct isl_vec *sample = NULL;
6122 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6125 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6126 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6127 copy = isl_basic_map_remove_redundancies(copy);
6128 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6129 isl_basic_map_free(copy);
6133 total = 1 + isl_basic_map_total_dim(bmap);
6134 if (bmap->sample && bmap->sample->size == total) {
6135 int contains = isl_basic_map_contains(bmap, bmap->sample);
6141 isl_vec_free(bmap->sample);
6142 bmap->sample = NULL;
6143 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6146 sample = isl_basic_set_sample_vec(bset);
6149 empty = sample->size == 0;
6150 isl_vec_free(bmap->sample);
6151 bmap->sample = sample;
6153 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6158 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6162 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6165 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6167 return isl_basic_map_plain_is_empty(bmap);
6170 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6174 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6177 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6179 return isl_basic_set_plain_is_empty(bset);
6182 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6184 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6187 struct isl_map *isl_basic_map_union(
6188 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6190 struct isl_map *map;
6191 if (!bmap1 || !bmap2)
6194 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6196 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6199 map = isl_map_add_basic_map(map, bmap1);
6200 map = isl_map_add_basic_map(map, bmap2);
6203 isl_basic_map_free(bmap1);
6204 isl_basic_map_free(bmap2);
6208 struct isl_set *isl_basic_set_union(
6209 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6211 return (struct isl_set *)isl_basic_map_union(
6212 (struct isl_basic_map *)bset1,
6213 (struct isl_basic_map *)bset2);
6216 /* Order divs such that any div only depends on previous divs */
6217 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6225 off = isl_dim_total(bmap->dim);
6227 for (i = 0; i < bmap->n_div; ++i) {
6229 if (isl_int_is_zero(bmap->div[i][0]))
6231 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6235 isl_basic_map_swap_div(bmap, i, i + pos);
6241 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6243 return (struct isl_basic_set *)
6244 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6247 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6254 for (i = 0; i < map->n; ++i) {
6255 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6266 /* Look for a div in dst that corresponds to the div "div" in src.
6267 * The divs before "div" in src and dst are assumed to be the same.
6269 * Returns -1 if no corresponding div was found and the position
6270 * of the corresponding div in dst otherwise.
6272 static int find_div(struct isl_basic_map *dst,
6273 struct isl_basic_map *src, unsigned div)
6277 unsigned total = isl_dim_total(src->dim);
6279 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6280 for (i = div; i < dst->n_div; ++i)
6281 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6282 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6283 dst->n_div - div) == -1)
6288 struct isl_basic_map *isl_basic_map_align_divs(
6289 struct isl_basic_map *dst, struct isl_basic_map *src)
6292 unsigned total = isl_dim_total(src->dim);
6297 if (src->n_div == 0)
6300 for (i = 0; i < src->n_div; ++i)
6301 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6303 src = isl_basic_map_order_divs(src);
6304 dst = isl_basic_map_cow(dst);
6305 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6306 src->n_div, 0, 2 * src->n_div);
6309 for (i = 0; i < src->n_div; ++i) {
6310 int j = find_div(dst, src, i);
6312 j = isl_basic_map_alloc_div(dst);
6315 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6316 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6317 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6321 isl_basic_map_swap_div(dst, i, j);
6325 isl_basic_map_free(dst);
6329 struct isl_basic_set *isl_basic_set_align_divs(
6330 struct isl_basic_set *dst, struct isl_basic_set *src)
6332 return (struct isl_basic_set *)isl_basic_map_align_divs(
6333 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6336 struct isl_map *isl_map_align_divs(struct isl_map *map)
6344 map = isl_map_compute_divs(map);
6345 map = isl_map_cow(map);
6349 for (i = 1; i < map->n; ++i)
6350 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6351 for (i = 1; i < map->n; ++i)
6352 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6354 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6358 struct isl_set *isl_set_align_divs(struct isl_set *set)
6360 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6363 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6367 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6368 map = isl_map_intersect_domain(map, set);
6369 set = isl_map_range(map);
6377 /* There is no need to cow as removing empty parts doesn't change
6378 * the meaning of the set.
6380 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6387 for (i = map->n-1; i >= 0; --i) {
6388 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6390 isl_basic_map_free(map->p[i]);
6391 if (i != map->n-1) {
6392 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6393 map->p[i] = map->p[map->n-1];
6401 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6403 return (struct isl_set *)
6404 isl_map_remove_empty_parts((struct isl_map *)set);
6407 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6409 struct isl_basic_map *bmap;
6410 if (!map || map->n == 0)
6412 bmap = map->p[map->n-1];
6413 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6414 return isl_basic_map_copy(bmap);
6417 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6419 return (struct isl_basic_set *)
6420 isl_map_copy_basic_map((struct isl_map *)set);
6423 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6424 __isl_keep isl_basic_map *bmap)
6430 for (i = map->n-1; i >= 0; --i) {
6431 if (map->p[i] != bmap)
6433 map = isl_map_cow(map);
6436 isl_basic_map_free(map->p[i]);
6437 if (i != map->n-1) {
6438 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6439 map->p[i] = map->p[map->n-1];
6450 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6451 struct isl_basic_set *bset)
6453 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6454 (struct isl_basic_map *)bset);
6457 /* Given two basic sets bset1 and bset2, compute the maximal difference
6458 * between the values of dimension pos in bset1 and those in bset2
6459 * for any common value of the parameters and dimensions preceding pos.
6461 static enum isl_lp_result basic_set_maximal_difference_at(
6462 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6463 int pos, isl_int *opt)
6465 struct isl_dim *dims;
6466 struct isl_basic_map *bmap1 = NULL;
6467 struct isl_basic_map *bmap2 = NULL;
6468 struct isl_ctx *ctx;
6469 struct isl_vec *obj;
6472 unsigned dim1, dim2;
6473 enum isl_lp_result res;
6475 if (!bset1 || !bset2)
6476 return isl_lp_error;
6478 nparam = isl_basic_set_n_param(bset1);
6479 dim1 = isl_basic_set_n_dim(bset1);
6480 dim2 = isl_basic_set_n_dim(bset2);
6481 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6482 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6483 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6484 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6485 if (!bmap1 || !bmap2)
6487 bmap1 = isl_basic_map_cow(bmap1);
6488 bmap1 = isl_basic_map_extend(bmap1, nparam,
6489 pos, (dim1 - pos) + (dim2 - pos),
6490 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6491 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6494 total = isl_basic_map_total_dim(bmap1);
6496 obj = isl_vec_alloc(ctx, 1 + total);
6497 isl_seq_clr(obj->block.data, 1 + total);
6498 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6499 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6502 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6504 isl_basic_map_free(bmap1);
6508 isl_basic_map_free(bmap1);
6509 isl_basic_map_free(bmap2);
6510 return isl_lp_error;
6513 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6514 * for any common value of the parameters and dimensions preceding pos
6515 * in both basic sets, the values of dimension pos in bset1 are
6516 * smaller or larger than those in bset2.
6519 * 1 if bset1 follows bset2
6520 * -1 if bset1 precedes bset2
6521 * 0 if bset1 and bset2 are incomparable
6522 * -2 if some error occurred.
6524 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6525 struct isl_basic_set *bset2, int pos)
6528 enum isl_lp_result res;
6533 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6535 if (res == isl_lp_empty)
6537 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6538 res == isl_lp_unbounded)
6540 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6549 /* Given two basic sets bset1 and bset2, check whether
6550 * for any common value of the parameters and dimensions preceding pos
6551 * there is a value of dimension pos in bset1 that is larger
6552 * than a value of the same dimension in bset2.
6555 * 1 if there exists such a pair
6556 * 0 if there is no such pair, but there is a pair of equal values
6558 * -2 if some error occurred.
6560 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6561 __isl_keep isl_basic_set *bset2, int pos)
6564 enum isl_lp_result res;
6569 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6571 if (res == isl_lp_empty)
6573 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6574 res == isl_lp_unbounded)
6576 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6578 else if (res == isl_lp_ok)
6587 /* Given two sets set1 and set2, check whether
6588 * for any common value of the parameters and dimensions preceding pos
6589 * there is a value of dimension pos in set1 that is larger
6590 * than a value of the same dimension in set2.
6593 * 1 if there exists such a pair
6594 * 0 if there is no such pair, but there is a pair of equal values
6596 * -2 if some error occurred.
6598 int isl_set_follows_at(__isl_keep isl_set *set1,
6599 __isl_keep isl_set *set2, int pos)
6607 for (i = 0; i < set1->n; ++i)
6608 for (j = 0; j < set2->n; ++j) {
6610 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6611 if (f == 1 || f == -2)
6620 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
6621 unsigned pos, isl_int *val)
6629 total = isl_basic_map_total_dim(bmap);
6630 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6631 for (; d+1 > pos; --d)
6632 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6636 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6638 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6640 if (!isl_int_is_one(bmap->eq[i][1+d]))
6643 isl_int_neg(*val, bmap->eq[i][0]);
6649 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
6650 unsigned pos, isl_int *val)
6662 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
6665 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
6666 for (i = 1; fixed == 1 && i < map->n; ++i) {
6667 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
6668 if (fixed == 1 && isl_int_ne(tmp, v))
6672 isl_int_set(*val, v);
6678 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
6679 unsigned pos, isl_int *val)
6681 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
6685 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
6688 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
6691 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
6692 enum isl_dim_type type, unsigned pos, isl_int *val)
6694 if (pos >= isl_basic_map_dim(bmap, type))
6696 return isl_basic_map_plain_has_fixed_var(bmap,
6697 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6700 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
6701 enum isl_dim_type type, unsigned pos, isl_int *val)
6703 if (pos >= isl_map_dim(map, type))
6705 return isl_map_plain_has_fixed_var(map,
6706 map_offset(map, type) - 1 + pos, val);
6709 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
6710 enum isl_dim_type type, unsigned pos, isl_int *val)
6712 return isl_map_plain_is_fixed(map, type, pos, val);
6715 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6716 * then return this fixed value in *val.
6718 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
6719 unsigned dim, isl_int *val)
6721 return isl_basic_set_plain_has_fixed_var(bset,
6722 isl_basic_set_n_param(bset) + dim, val);
6725 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6726 * then return this fixed value in *val.
6728 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
6729 unsigned dim, isl_int *val)
6731 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6734 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
6735 unsigned dim, isl_int *val)
6737 return isl_set_plain_dim_is_fixed(set, dim, val);
6740 /* Check if input variable in has fixed value and if so and if val is not NULL,
6741 * then return this fixed value in *val.
6743 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
6744 unsigned in, isl_int *val)
6746 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
6749 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6750 * and if val is not NULL, then return this lower bound in *val.
6752 int isl_basic_set_plain_dim_has_fixed_lower_bound(
6753 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
6755 int i, i_eq = -1, i_ineq = -1;
6762 total = isl_basic_set_total_dim(bset);
6763 nparam = isl_basic_set_n_param(bset);
6764 for (i = 0; i < bset->n_eq; ++i) {
6765 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6771 for (i = 0; i < bset->n_ineq; ++i) {
6772 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6774 if (i_eq != -1 || i_ineq != -1)
6778 if (i_eq == -1 && i_ineq == -1)
6780 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6781 /* The coefficient should always be one due to normalization. */
6782 if (!isl_int_is_one(c[1+nparam+dim]))
6784 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6786 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6787 total - nparam - dim - 1) != -1)
6790 isl_int_neg(*val, c[0]);
6794 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
6795 unsigned dim, isl_int *val)
6807 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
6811 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
6813 for (i = 1; fixed == 1 && i < set->n; ++i) {
6814 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
6816 if (fixed == 1 && isl_int_ne(tmp, v))
6820 isl_int_set(*val, v);
6831 /* uset_gist depends on constraints without existentially quantified
6832 * variables sorting first.
6834 static int qsort_constraint_cmp(const void *p1, const void *p2)
6836 const struct constraint *c1 = (const struct constraint *)p1;
6837 const struct constraint *c2 = (const struct constraint *)p2;
6839 unsigned size = isl_min(c1->size, c2->size);
6841 l1 = isl_seq_last_non_zero(c1->c, size);
6842 l2 = isl_seq_last_non_zero(c2->c, size);
6847 return isl_seq_cmp(c1->c, c2->c, size);
6850 static struct isl_basic_map *isl_basic_map_sort_constraints(
6851 struct isl_basic_map *bmap)
6854 struct constraint *c;
6859 total = isl_basic_map_total_dim(bmap);
6860 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6863 for (i = 0; i < bmap->n_ineq; ++i) {
6865 c[i].c = bmap->ineq[i];
6867 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6868 for (i = 0; i < bmap->n_ineq; ++i)
6869 bmap->ineq[i] = c[i].c;
6873 isl_basic_map_free(bmap);
6877 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6878 __isl_take isl_basic_set *bset)
6880 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6881 (struct isl_basic_map *)bset);
6884 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6888 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6890 bmap = isl_basic_map_remove_redundancies(bmap);
6891 bmap = isl_basic_map_sort_constraints(bmap);
6892 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6896 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6898 return (struct isl_basic_set *)isl_basic_map_normalize(
6899 (struct isl_basic_map *)bset);
6902 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
6903 const __isl_keep isl_basic_map *bmap2)
6910 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
6911 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
6912 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
6913 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6914 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6915 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6916 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6917 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6918 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6919 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6920 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6922 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6924 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6926 if (bmap1->n_eq != bmap2->n_eq)
6927 return bmap1->n_eq - bmap2->n_eq;
6928 if (bmap1->n_ineq != bmap2->n_ineq)
6929 return bmap1->n_ineq - bmap2->n_ineq;
6930 if (bmap1->n_div != bmap2->n_div)
6931 return bmap1->n_div - bmap2->n_div;
6932 total = isl_basic_map_total_dim(bmap1);
6933 for (i = 0; i < bmap1->n_eq; ++i) {
6934 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6938 for (i = 0; i < bmap1->n_ineq; ++i) {
6939 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6943 for (i = 0; i < bmap1->n_div; ++i) {
6944 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6951 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
6952 __isl_keep isl_basic_map *bmap2)
6954 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
6957 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
6958 __isl_keep isl_basic_set *bset2)
6960 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
6961 (isl_basic_map *)bset2);
6964 static int qsort_bmap_cmp(const void *p1, const void *p2)
6966 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6967 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6969 return isl_basic_map_plain_cmp(bmap1, bmap2);
6972 /* We normalize in place, but if anything goes wrong we need
6973 * to return NULL, so we need to make sure we don't change the
6974 * meaning of any possible other copies of map.
6976 struct isl_map *isl_map_normalize(struct isl_map *map)
6979 struct isl_basic_map *bmap;
6983 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6985 for (i = 0; i < map->n; ++i) {
6986 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6989 isl_basic_map_free(map->p[i]);
6992 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6993 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6994 map = isl_map_remove_empty_parts(map);
6997 for (i = map->n - 1; i >= 1; --i) {
6998 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7000 isl_basic_map_free(map->p[i-1]);
7001 for (j = i; j < map->n; ++j)
7002 map->p[j-1] = map->p[j];
7012 struct isl_set *isl_set_normalize(struct isl_set *set)
7014 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7017 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7027 if (!isl_dim_equal(map1->dim, map2->dim))
7030 map1 = isl_map_copy(map1);
7031 map2 = isl_map_copy(map2);
7032 map1 = isl_map_normalize(map1);
7033 map2 = isl_map_normalize(map2);
7036 equal = map1->n == map2->n;
7037 for (i = 0; equal && i < map1->n; ++i) {
7038 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7051 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7053 return isl_map_plain_is_equal(map1, map2);
7056 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7058 return isl_map_plain_is_equal((struct isl_map *)set1,
7059 (struct isl_map *)set2);
7062 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7064 return isl_set_plain_is_equal(set1, set2);
7067 /* Return an interval that ranges from min to max (inclusive)
7069 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7070 isl_int min, isl_int max)
7073 struct isl_basic_set *bset = NULL;
7075 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7079 k = isl_basic_set_alloc_inequality(bset);
7082 isl_int_set_si(bset->ineq[k][1], 1);
7083 isl_int_neg(bset->ineq[k][0], min);
7085 k = isl_basic_set_alloc_inequality(bset);
7088 isl_int_set_si(bset->ineq[k][1], -1);
7089 isl_int_set(bset->ineq[k][0], max);
7093 isl_basic_set_free(bset);
7097 /* Return the Cartesian product of the basic sets in list (in the given order).
7099 __isl_give isl_basic_set *isl_basic_set_list_product(
7100 __isl_take struct isl_basic_set_list *list)
7108 struct isl_basic_set *product = NULL;
7112 isl_assert(list->ctx, list->n > 0, goto error);
7113 isl_assert(list->ctx, list->p[0], goto error);
7114 nparam = isl_basic_set_n_param(list->p[0]);
7115 dim = isl_basic_set_n_dim(list->p[0]);
7116 extra = list->p[0]->n_div;
7117 n_eq = list->p[0]->n_eq;
7118 n_ineq = list->p[0]->n_ineq;
7119 for (i = 1; i < list->n; ++i) {
7120 isl_assert(list->ctx, list->p[i], goto error);
7121 isl_assert(list->ctx,
7122 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7123 dim += isl_basic_set_n_dim(list->p[i]);
7124 extra += list->p[i]->n_div;
7125 n_eq += list->p[i]->n_eq;
7126 n_ineq += list->p[i]->n_ineq;
7128 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7133 for (i = 0; i < list->n; ++i) {
7134 isl_basic_set_add_constraints(product,
7135 isl_basic_set_copy(list->p[i]), dim);
7136 dim += isl_basic_set_n_dim(list->p[i]);
7138 isl_basic_set_list_free(list);
7141 isl_basic_set_free(product);
7142 isl_basic_set_list_free(list);
7146 struct isl_basic_map *isl_basic_map_product(
7147 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7149 struct isl_dim *dim_result = NULL;
7150 struct isl_basic_map *bmap;
7151 unsigned in1, in2, out1, out2, nparam, total, pos;
7152 struct isl_dim_map *dim_map1, *dim_map2;
7154 if (!bmap1 || !bmap2)
7157 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7158 bmap2->dim, isl_dim_param), goto error);
7159 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7160 isl_dim_copy(bmap2->dim));
7162 in1 = isl_basic_map_n_in(bmap1);
7163 in2 = isl_basic_map_n_in(bmap2);
7164 out1 = isl_basic_map_n_out(bmap1);
7165 out2 = isl_basic_map_n_out(bmap2);
7166 nparam = isl_basic_map_n_param(bmap1);
7168 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7169 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7170 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7171 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7172 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7173 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7174 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7175 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7176 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7177 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7178 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7180 bmap = isl_basic_map_alloc_dim(dim_result,
7181 bmap1->n_div + bmap2->n_div,
7182 bmap1->n_eq + bmap2->n_eq,
7183 bmap1->n_ineq + bmap2->n_ineq);
7184 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7185 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7186 bmap = isl_basic_map_simplify(bmap);
7187 return isl_basic_map_finalize(bmap);
7189 isl_basic_map_free(bmap1);
7190 isl_basic_map_free(bmap2);
7194 __isl_give isl_basic_map *isl_basic_map_flat_product(
7195 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7197 isl_basic_map *prod;
7199 prod = isl_basic_map_product(bmap1, bmap2);
7200 prod = isl_basic_map_flatten(prod);
7204 __isl_give isl_basic_set *isl_basic_set_flat_product(
7205 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7207 return isl_basic_map_flat_product(bset1, bset2);
7210 __isl_give isl_basic_map *isl_basic_map_range_product(
7211 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7213 isl_dim *dim_result = NULL;
7214 isl_basic_map *bmap;
7215 unsigned in, out1, out2, nparam, total, pos;
7216 struct isl_dim_map *dim_map1, *dim_map2;
7218 if (!bmap1 || !bmap2)
7221 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7222 isl_dim_copy(bmap2->dim));
7224 in = isl_basic_map_dim(bmap1, isl_dim_in);
7225 out1 = isl_basic_map_n_out(bmap1);
7226 out2 = isl_basic_map_n_out(bmap2);
7227 nparam = isl_basic_map_n_param(bmap1);
7229 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7230 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7231 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7232 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7233 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7234 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7235 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7236 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7237 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7238 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7239 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7241 bmap = isl_basic_map_alloc_dim(dim_result,
7242 bmap1->n_div + bmap2->n_div,
7243 bmap1->n_eq + bmap2->n_eq,
7244 bmap1->n_ineq + bmap2->n_ineq);
7245 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7246 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7247 bmap = isl_basic_map_simplify(bmap);
7248 return isl_basic_map_finalize(bmap);
7250 isl_basic_map_free(bmap1);
7251 isl_basic_map_free(bmap2);
7255 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7256 __isl_take isl_map *map2,
7257 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7258 __isl_take isl_dim *right),
7259 __isl_give isl_basic_map *(*basic_map_product)(
7260 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7263 struct isl_map *result;
7269 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7270 map2->dim, isl_dim_param), goto error);
7272 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7273 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7274 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7276 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7277 isl_dim_copy(map2->dim)),
7278 map1->n * map2->n, flags);
7281 for (i = 0; i < map1->n; ++i)
7282 for (j = 0; j < map2->n; ++j) {
7283 struct isl_basic_map *part;
7284 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7285 isl_basic_map_copy(map2->p[j]));
7286 if (isl_basic_map_is_empty(part))
7287 isl_basic_map_free(part);
7289 result = isl_map_add_basic_map(result, part);
7302 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7304 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7306 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7309 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7311 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7312 __isl_take isl_map *map2)
7316 prod = isl_map_product(map1, map2);
7317 prod = isl_map_flatten(prod);
7321 /* Given two set A and B, construct its Cartesian product A x B.
7323 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7325 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7326 (struct isl_map *)set2);
7329 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7330 __isl_take isl_set *set2)
7332 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7335 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7337 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7338 __isl_take isl_map *map2)
7340 return map_product(map1, map2, &isl_dim_range_product,
7341 &isl_basic_map_range_product);
7344 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7347 uint32_t hash = isl_hash_init();
7352 bmap = isl_basic_map_copy(bmap);
7353 bmap = isl_basic_map_normalize(bmap);
7356 total = isl_basic_map_total_dim(bmap);
7357 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7358 for (i = 0; i < bmap->n_eq; ++i) {
7360 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7361 isl_hash_hash(hash, c_hash);
7363 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7364 for (i = 0; i < bmap->n_ineq; ++i) {
7366 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7367 isl_hash_hash(hash, c_hash);
7369 isl_hash_byte(hash, bmap->n_div & 0xFF);
7370 for (i = 0; i < bmap->n_div; ++i) {
7372 if (isl_int_is_zero(bmap->div[i][0]))
7374 isl_hash_byte(hash, i & 0xFF);
7375 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7376 isl_hash_hash(hash, c_hash);
7378 isl_basic_map_free(bmap);
7382 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7384 return isl_basic_map_get_hash((isl_basic_map *)bset);
7387 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7394 map = isl_map_copy(map);
7395 map = isl_map_normalize(map);
7399 hash = isl_hash_init();
7400 for (i = 0; i < map->n; ++i) {
7402 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7403 isl_hash_hash(hash, bmap_hash);
7411 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7413 return isl_map_get_hash((isl_map *)set);
7416 /* Check if the value for dimension dim is completely determined
7417 * by the values of the other parameters and variables.
7418 * That is, check if dimension dim is involved in an equality.
7420 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7427 nparam = isl_basic_set_n_param(bset);
7428 for (i = 0; i < bset->n_eq; ++i)
7429 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7434 /* Check if the value for dimension dim is completely determined
7435 * by the values of the other parameters and variables.
7436 * That is, check if dimension dim is involved in an equality
7437 * for each of the subsets.
7439 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7445 for (i = 0; i < set->n; ++i) {
7447 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7454 int isl_set_n_basic_set(__isl_keep isl_set *set)
7456 return set ? set->n : 0;
7459 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7460 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7467 for (i = 0; i < map->n; ++i)
7468 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7474 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7475 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7482 for (i = 0; i < set->n; ++i)
7483 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7489 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7491 struct isl_dim *dim;
7496 bset = isl_basic_set_cow(bset);
7500 dim = isl_basic_set_get_dim(bset);
7501 dim = isl_dim_lift(dim, bset->n_div);
7504 isl_dim_free(bset->dim);
7506 bset->extra -= bset->n_div;
7509 bset = isl_basic_set_finalize(bset);
7513 isl_basic_set_free(bset);
7517 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7520 struct isl_dim *dim;
7523 set = isl_set_align_divs(set);
7528 set = isl_set_cow(set);
7532 n_div = set->p[0]->n_div;
7533 dim = isl_set_get_dim(set);
7534 dim = isl_dim_lift(dim, n_div);
7537 isl_dim_free(set->dim);
7540 for (i = 0; i < set->n; ++i) {
7541 set->p[i] = isl_basic_set_lift(set->p[i]);
7552 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7554 struct isl_dim *dim;
7555 struct isl_basic_map *bmap;
7562 set = isl_set_align_divs(set);
7567 dim = isl_set_get_dim(set);
7568 if (set->n == 0 || set->p[0]->n_div == 0) {
7570 return isl_map_identity(isl_dim_map_from_set(dim));
7573 n_div = set->p[0]->n_div;
7574 dim = isl_dim_map_from_set(dim);
7575 n_param = isl_dim_size(dim, isl_dim_param);
7576 n_set = isl_dim_size(dim, isl_dim_in);
7577 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7578 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7579 for (i = 0; i < n_set; ++i)
7580 bmap = var_equal(bmap, i);
7582 total = n_param + n_set + n_set + n_div;
7583 for (i = 0; i < n_div; ++i) {
7584 k = isl_basic_map_alloc_inequality(bmap);
7587 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7588 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7589 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7590 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7591 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7592 set->p[0]->div[i][0]);
7594 l = isl_basic_map_alloc_inequality(bmap);
7597 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7598 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7599 set->p[0]->div[i][0]);
7600 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7604 bmap = isl_basic_map_simplify(bmap);
7605 bmap = isl_basic_map_finalize(bmap);
7606 return isl_map_from_basic_map(bmap);
7609 isl_basic_map_free(bmap);
7613 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7621 dim = isl_basic_set_total_dim(bset);
7622 size += bset->n_eq * (1 + dim);
7623 size += bset->n_ineq * (1 + dim);
7624 size += bset->n_div * (2 + dim);
7629 int isl_set_size(__isl_keep isl_set *set)
7637 for (i = 0; i < set->n; ++i)
7638 size += isl_basic_set_size(set->p[i]);
7643 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7644 enum isl_dim_type type, unsigned pos)
7652 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7654 pos += isl_basic_map_offset(bmap, type);
7656 for (i = 0; i < bmap->n_eq; ++i)
7657 if (!isl_int_is_zero(bmap->eq[i][pos]))
7661 for (i = 0; i < bmap->n_ineq; ++i) {
7662 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7669 return lower && upper;
7672 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7673 enum isl_dim_type type, unsigned pos)
7680 for (i = 0; i < map->n; ++i) {
7682 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7683 if (bounded < 0 || !bounded)
7690 /* Return 1 if the specified dim is involved in both an upper bound
7691 * and a lower bound.
7693 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7694 enum isl_dim_type type, unsigned pos)
7696 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7699 /* For each of the "n" variables starting at "first", determine
7700 * the sign of the variable and put the results in the first "n"
7701 * elements of the array "signs".
7703 * 1 means that the variable is non-negative
7704 * -1 means that the variable is non-positive
7705 * 0 means the variable attains both positive and negative values.
7707 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7708 unsigned first, unsigned n, int *signs)
7710 isl_vec *bound = NULL;
7711 struct isl_tab *tab = NULL;
7712 struct isl_tab_undo *snap;
7715 if (!bset || !signs)
7718 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7719 tab = isl_tab_from_basic_set(bset);
7723 isl_seq_clr(bound->el, bound->size);
7724 isl_int_set_si(bound->el[0], -1);
7726 snap = isl_tab_snap(tab);
7727 for (i = 0; i < n; ++i) {
7730 isl_int_set_si(bound->el[1 + first + i], -1);
7731 if (isl_tab_add_ineq(tab, bound->el) < 0)
7734 isl_int_set_si(bound->el[1 + first + i], 0);
7735 if (isl_tab_rollback(tab, snap) < 0)
7743 isl_int_set_si(bound->el[1 + first + i], 1);
7744 if (isl_tab_add_ineq(tab, bound->el) < 0)
7747 isl_int_set_si(bound->el[1 + first + i], 0);
7748 if (isl_tab_rollback(tab, snap) < 0)
7751 signs[i] = empty ? -1 : 0;
7755 isl_vec_free(bound);
7759 isl_vec_free(bound);
7763 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7764 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7766 if (!bset || !signs)
7768 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7771 first += pos(bset->dim, type) - 1;
7772 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7775 /* Check if the given basic map is obviously single-valued.
7776 * In particular, for each output dimension, check that there is
7777 * an equality that defines the output dimension in terms of
7778 * earlier dimensions.
7780 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
7790 total = 1 + isl_basic_map_total_dim(bmap);
7791 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7792 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7794 for (i = 0; i < n_out; ++i) {
7795 for (j = 0; j < bmap->n_eq; ++j) {
7796 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7798 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7799 total - (o_out + i + 1)) == -1)
7802 if (j >= bmap->n_eq)
7809 /* Check if the given map is obviously single-valued.
7811 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
7822 return isl_basic_map_plain_is_single_valued(map->p[0]);
7825 /* Check if the given map is single-valued.
7830 * and check if the result is a subset of the identity mapping.
7832 int isl_map_is_single_valued(__isl_keep isl_map *map)
7839 sv = isl_map_plain_is_single_valued(map);
7843 test = isl_map_reverse(isl_map_copy(map));
7844 test = isl_map_apply_range(test, isl_map_copy(map));
7846 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
7847 id = isl_map_identity(dim);
7849 sv = isl_map_is_subset(test, id);
7857 int isl_map_is_injective(__isl_keep isl_map *map)
7861 map = isl_map_copy(map);
7862 map = isl_map_reverse(map);
7863 in = isl_map_is_single_valued(map);
7869 /* Check if the given map is obviously injective.
7871 int isl_map_plain_is_injective(__isl_keep isl_map *map)
7875 map = isl_map_copy(map);
7876 map = isl_map_reverse(map);
7877 in = isl_map_plain_is_single_valued(map);
7883 int isl_map_is_bijective(__isl_keep isl_map *map)
7887 sv = isl_map_is_single_valued(map);
7891 return isl_map_is_injective(map);
7894 int isl_set_is_singleton(__isl_keep isl_set *set)
7896 return isl_map_is_single_valued((isl_map *)set);
7899 int isl_map_is_translation(__isl_keep isl_map *map)
7904 delta = isl_map_deltas(isl_map_copy(map));
7905 ok = isl_set_is_singleton(delta);
7906 isl_set_free(delta);
7911 static int unique(isl_int *p, unsigned pos, unsigned len)
7913 if (isl_seq_first_non_zero(p, pos) != -1)
7915 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7920 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7929 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7932 nvar = isl_basic_set_dim(bset, isl_dim_set);
7933 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7934 for (j = 0; j < nvar; ++j) {
7935 int lower = 0, upper = 0;
7936 for (i = 0; i < bset->n_eq; ++i) {
7937 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7939 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7945 for (i = 0; i < bset->n_ineq; ++i) {
7946 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7948 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7950 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7955 if (!lower || !upper)
7962 int isl_set_is_box(__isl_keep isl_set *set)
7969 return isl_basic_set_is_box(set->p[0]);
7972 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7977 return isl_dim_is_wrapping(bset->dim);
7980 int isl_set_is_wrapping(__isl_keep isl_set *set)
7985 return isl_dim_is_wrapping(set->dim);
7988 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7990 bmap = isl_basic_map_cow(bmap);
7994 bmap->dim = isl_dim_wrap(bmap->dim);
7998 bmap = isl_basic_map_finalize(bmap);
8000 return (isl_basic_set *)bmap;
8002 isl_basic_map_free(bmap);
8006 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8010 map = isl_map_cow(map);
8014 for (i = 0; i < map->n; ++i) {
8015 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8019 map->dim = isl_dim_wrap(map->dim);
8023 return (isl_set *)map;
8029 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8031 bset = isl_basic_set_cow(bset);
8035 bset->dim = isl_dim_unwrap(bset->dim);
8039 bset = isl_basic_set_finalize(bset);
8041 return (isl_basic_map *)bset;
8043 isl_basic_set_free(bset);
8047 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8054 if (!isl_set_is_wrapping(set))
8055 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8058 set = isl_set_cow(set);
8062 for (i = 0; i < set->n; ++i) {
8063 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8068 set->dim = isl_dim_unwrap(set->dim);
8072 return (isl_map *)set;
8078 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8079 enum isl_dim_type type)
8084 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8087 bmap = isl_basic_map_cow(bmap);
8091 bmap->dim = isl_dim_reset(bmap->dim, type);
8095 bmap = isl_basic_map_finalize(bmap);
8099 isl_basic_map_free(bmap);
8103 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8104 enum isl_dim_type type)
8111 if (!isl_dim_is_named_or_nested(map->dim, type))
8114 map = isl_map_cow(map);
8118 for (i = 0; i < map->n; ++i) {
8119 map->p[i] = isl_basic_map_reset(map->p[i], type);
8123 map->dim = isl_dim_reset(map->dim, type);
8133 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8138 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8141 bmap = isl_basic_map_cow(bmap);
8145 bmap->dim = isl_dim_flatten(bmap->dim);
8149 bmap = isl_basic_map_finalize(bmap);
8153 isl_basic_map_free(bmap);
8157 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8159 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8162 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8169 if (!map->dim->nested[0] && !map->dim->nested[1])
8172 map = isl_map_cow(map);
8176 for (i = 0; i < map->n; ++i) {
8177 map->p[i] = isl_basic_map_flatten(map->p[i]);
8181 map->dim = isl_dim_flatten(map->dim);
8191 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8193 return (isl_set *)isl_map_flatten((isl_map *)set);
8196 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8198 isl_dim *dim, *flat_dim;
8201 dim = isl_set_get_dim(set);
8202 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8203 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8204 map = isl_map_intersect_domain(map, set);
8209 /* Reorder the dimensions of "bmap" according to the given dim_map
8210 * and set the dimension specification to "dim".
8212 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8213 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8217 bmap = isl_basic_map_cow(bmap);
8218 if (!bmap || !dim || !dim_map)
8221 res = isl_basic_map_alloc_dim(dim,
8222 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8223 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8224 res = isl_basic_map_finalize(res);
8228 isl_basic_map_free(bmap);
8233 /* Reorder the dimensions of "map" according to given reordering.
8235 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8236 __isl_take isl_reordering *r)
8239 struct isl_dim_map *dim_map;
8241 map = isl_map_cow(map);
8242 dim_map = isl_dim_map_from_reordering(r);
8243 if (!map || !r || !dim_map)
8246 for (i = 0; i < map->n; ++i) {
8247 struct isl_dim_map *dim_map_i;
8249 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8251 map->p[i] = isl_basic_map_realign(map->p[i],
8252 isl_dim_copy(r->dim), dim_map_i);
8258 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8260 isl_reordering_free(r);
8266 isl_reordering_free(r);
8270 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8271 __isl_take isl_reordering *r)
8273 return (isl_set *)isl_map_realign((isl_map *)set, r);
8276 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
8277 __isl_take isl_dim *model)
8284 ctx = isl_dim_get_ctx(model);
8285 if (!isl_dim_has_named_params(model))
8286 isl_die(ctx, isl_error_invalid,
8287 "model has unnamed parameters", goto error);
8288 if (!isl_dim_has_named_params(map->dim))
8289 isl_die(ctx, isl_error_invalid,
8290 "relation has unnamed parameters", goto error);
8291 if (!isl_dim_match(map->dim, isl_dim_param, model, isl_dim_param)) {
8292 isl_reordering *exp;
8294 model = isl_dim_drop(model, isl_dim_in,
8295 0, isl_dim_size(model, isl_dim_in));
8296 model = isl_dim_drop(model, isl_dim_out,
8297 0, isl_dim_size(model, isl_dim_out));
8298 exp = isl_parameter_alignment_reordering(map->dim, model);
8299 exp = isl_reordering_extend_dim(exp, isl_map_get_dim(map));
8300 map = isl_map_realign(map, exp);
8303 isl_dim_free(model);
8306 isl_dim_free(model);
8311 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
8312 __isl_take isl_dim *model)
8314 return isl_map_align_params(set, model);
8317 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8318 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8319 enum isl_dim_type c2, enum isl_dim_type c3,
8320 enum isl_dim_type c4, enum isl_dim_type c5)
8322 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8323 struct isl_mat *mat;
8329 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8330 isl_basic_map_total_dim(bmap) + 1);
8333 for (i = 0; i < bmap->n_eq; ++i)
8334 for (j = 0, pos = 0; j < 5; ++j) {
8335 int off = isl_basic_map_offset(bmap, c[j]);
8336 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8337 isl_int_set(mat->row[i][pos],
8338 bmap->eq[i][off + k]);
8346 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8347 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8348 enum isl_dim_type c2, enum isl_dim_type c3,
8349 enum isl_dim_type c4, enum isl_dim_type c5)
8351 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8352 struct isl_mat *mat;
8358 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8359 isl_basic_map_total_dim(bmap) + 1);
8362 for (i = 0; i < bmap->n_ineq; ++i)
8363 for (j = 0, pos = 0; j < 5; ++j) {
8364 int off = isl_basic_map_offset(bmap, c[j]);
8365 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8366 isl_int_set(mat->row[i][pos],
8367 bmap->ineq[i][off + k]);
8375 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8376 __isl_take isl_dim *dim,
8377 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8378 enum isl_dim_type c2, enum isl_dim_type c3,
8379 enum isl_dim_type c4, enum isl_dim_type c5)
8381 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8382 isl_basic_map *bmap;
8388 if (!dim || !eq || !ineq)
8391 if (eq->n_col != ineq->n_col)
8392 isl_die(dim->ctx, isl_error_invalid,
8393 "equalities and inequalities matrices should have "
8394 "same number of columns", goto error);
8396 total = 1 + isl_dim_total(dim);
8398 if (eq->n_col < total)
8399 isl_die(dim->ctx, isl_error_invalid,
8400 "number of columns too small", goto error);
8402 extra = eq->n_col - total;
8404 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8405 eq->n_row, ineq->n_row);
8408 for (i = 0; i < extra; ++i) {
8409 k = isl_basic_map_alloc_div(bmap);
8412 isl_int_set_si(bmap->div[k][0], 0);
8414 for (i = 0; i < eq->n_row; ++i) {
8415 l = isl_basic_map_alloc_equality(bmap);
8418 for (j = 0, pos = 0; j < 5; ++j) {
8419 int off = isl_basic_map_offset(bmap, c[j]);
8420 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8421 isl_int_set(bmap->eq[l][off + k],
8427 for (i = 0; i < ineq->n_row; ++i) {
8428 l = isl_basic_map_alloc_inequality(bmap);
8431 for (j = 0, pos = 0; j < 5; ++j) {
8432 int off = isl_basic_map_offset(bmap, c[j]);
8433 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8434 isl_int_set(bmap->ineq[l][off + k],
8453 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8454 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8455 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8457 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8458 c1, c2, c3, c4, isl_dim_in);
8461 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8462 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8463 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8465 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8466 c1, c2, c3, c4, isl_dim_in);
8469 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8470 __isl_take isl_dim *dim,
8471 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8472 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8474 return (isl_basic_set*)
8475 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8476 c1, c2, c3, c4, isl_dim_in);
8479 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8484 return isl_dim_can_zip(bmap->dim);
8487 int isl_map_can_zip(__isl_keep isl_map *map)
8492 return isl_dim_can_zip(map->dim);
8495 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8496 * (A -> C) -> (B -> D).
8498 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8507 if (!isl_basic_map_can_zip(bmap))
8508 isl_die(bmap->ctx, isl_error_invalid,
8509 "basic map cannot be zipped", goto error);
8510 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8511 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8512 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8513 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8514 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8517 bmap->dim = isl_dim_zip(bmap->dim);
8522 isl_basic_map_free(bmap);
8526 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8527 * (A -> C) -> (B -> D).
8529 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8536 if (!isl_map_can_zip(map))
8537 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8540 map = isl_map_cow(map);
8544 for (i = 0; i < map->n; ++i) {
8545 map->p[i] = isl_basic_map_zip(map->p[i]);
8550 map->dim = isl_dim_zip(map->dim);