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
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
24 #include "isl_map_private.h"
25 #include "isl_map_piplib.h"
26 #include "isl_sample.h"
30 /* Maps dst positions to src positions */
36 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
39 struct isl_dim_map *dim_map;
40 dim_map = isl_alloc(ctx, struct isl_dim_map,
41 sizeof(struct isl_dim_map) + len * sizeof(int));
44 dim_map->len = 1 + len;
46 for (i = 0; i < len; ++i)
47 dim_map->pos[1 + i] = -1;
51 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
54 case isl_dim_param: return dim->nparam;
55 case isl_dim_in: return dim->n_in;
56 case isl_dim_out: return dim->n_out;
57 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
62 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
65 case isl_dim_param: return 1;
66 case isl_dim_in: return 1 + dim->nparam;
67 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
72 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
73 struct isl_dim *dim, enum isl_dim_type type,
74 unsigned first, unsigned n, unsigned dst_pos)
82 src_pos = pos(dim, type);
83 for (i = 0; i < n; ++i)
84 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
87 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
88 enum isl_dim_type type, unsigned dst_pos)
90 isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
93 static void isl_dim_map_div(struct isl_dim_map *dim_map,
94 struct isl_basic_map *bmap, unsigned dst_pos)
99 if (!dim_map || !bmap)
102 src_pos = 1 + isl_dim_total(bmap->dim);
103 for (i = 0; i < bmap->n_div; ++i)
104 dim_map->pos[1 + dst_pos + i] = src_pos + i;
107 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
111 for (i = 0; i < dim_map->len; ++i)
112 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
113 fprintf(stderr, "\n");
116 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
117 enum isl_dim_type type)
124 case isl_dim_out: return isl_dim_size(bmap->dim, type);
125 case isl_dim_div: return bmap->n_div;
126 case isl_dim_all: return isl_basic_map_total_dim(bmap);
131 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
133 return map ? n(map->dim, type) : 0;
136 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
138 return set ? n(set->dim, type) : 0;
141 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
142 enum isl_dim_type type)
144 struct isl_dim *dim = bmap->dim;
146 case isl_dim_param: return 1;
147 case isl_dim_in: return 1 + dim->nparam;
148 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
149 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
154 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
156 return pos(map->dim, type);
159 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
160 enum isl_dim_type type)
162 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
165 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
167 return isl_basic_set_dim(bset, isl_dim_set);
170 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
172 return isl_basic_set_dim(bset, isl_dim_param);
175 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
177 return isl_dim_total(bset->dim) + bset->n_div;
180 unsigned isl_set_n_dim(const struct isl_set *set)
182 return isl_set_dim(set, isl_dim_set);
185 unsigned isl_set_n_param(const struct isl_set *set)
187 return isl_set_dim(set, isl_dim_param);
190 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
192 return bmap ? bmap->dim->n_in : 0;
195 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
197 return bmap ? bmap->dim->n_out : 0;
200 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
202 return bmap ? bmap->dim->nparam : 0;
205 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
207 return bmap ? bmap->n_div : 0;
210 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
212 return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
215 unsigned isl_map_n_in(const struct isl_map *map)
217 return map->dim->n_in;
220 unsigned isl_map_n_out(const struct isl_map *map)
222 return map->dim->n_out;
225 unsigned isl_map_n_param(const struct isl_map *map)
227 return map->dim->nparam;
230 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
235 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
238 return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
241 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
242 struct isl_basic_set *bset)
247 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
250 return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
253 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
254 struct isl_basic_set *bset)
259 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
262 return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
265 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
267 return map ? map->ctx : NULL;
270 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
274 return isl_dim_copy(bmap->dim);
277 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
281 return isl_dim_copy(bset->dim);
284 struct isl_dim *isl_map_get_dim(struct isl_map *map)
288 return isl_dim_copy(map->dim);
291 struct isl_dim *isl_set_get_dim(struct isl_set *set)
295 return isl_dim_copy(set->dim);
298 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
299 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
301 bmap = isl_basic_map_cow(bmap);
304 bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
309 isl_basic_map_free(bmap);
313 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
314 enum isl_dim_type type, const char *s)
318 map = isl_map_cow(map);
322 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
326 for (i = 0; i < map->n; ++i) {
327 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
338 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
339 __isl_take isl_basic_map *bmap,
340 enum isl_dim_type type, unsigned pos, const char *s)
344 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
349 isl_basic_map_free(bmap);
353 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
354 enum isl_dim_type type, unsigned pos, const char *s)
361 map->dim = isl_dim_set_name(map->dim, type, pos, s);
365 for (i = 0; i < map->n; ++i) {
366 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
377 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
378 __isl_take isl_basic_set *bset,
379 enum isl_dim_type type, unsigned pos, const char *s)
381 return (isl_basic_set *)isl_basic_map_set_dim_name(
382 (isl_basic_map *)bset, type, pos, s);
385 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
386 enum isl_dim_type type, unsigned pos, const char *s)
388 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
391 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
395 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
398 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
399 struct isl_basic_map *bmap, unsigned extra,
400 unsigned n_eq, unsigned n_ineq)
403 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
408 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
409 if (isl_blk_is_error(bmap->block))
412 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
417 bmap->block2 = isl_blk_empty();
420 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
421 if (isl_blk_is_error(bmap->block2))
424 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
429 for (i = 0; i < n_ineq + n_eq; ++i)
430 bmap->ineq[i] = bmap->block.data + i * row_size;
432 for (i = 0; i < extra; ++i)
433 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
437 bmap->c_size = n_eq + n_ineq;
438 bmap->eq = bmap->ineq + n_ineq;
447 isl_basic_map_free(bmap);
451 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
452 unsigned nparam, unsigned dim, unsigned extra,
453 unsigned n_eq, unsigned n_ineq)
455 struct isl_basic_map *bmap;
456 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
457 return (struct isl_basic_set *)bmap;
460 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
461 unsigned extra, unsigned n_eq, unsigned n_ineq)
463 struct isl_basic_map *bmap;
466 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
467 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
468 return (struct isl_basic_set *)bmap;
471 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
472 unsigned extra, unsigned n_eq, unsigned n_ineq)
474 struct isl_basic_map *bmap;
478 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
483 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
489 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
490 unsigned nparam, unsigned in, unsigned out, unsigned extra,
491 unsigned n_eq, unsigned n_ineq)
493 struct isl_basic_map *bmap;
496 dim = isl_dim_alloc(ctx, nparam, in, out);
500 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
504 static void dup_constraints(
505 struct isl_basic_map *dst, struct isl_basic_map *src)
508 unsigned total = isl_basic_map_total_dim(src);
510 for (i = 0; i < src->n_eq; ++i) {
511 int j = isl_basic_map_alloc_equality(dst);
512 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
515 for (i = 0; i < src->n_ineq; ++i) {
516 int j = isl_basic_map_alloc_inequality(dst);
517 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
520 for (i = 0; i < src->n_div; ++i) {
521 int j = isl_basic_map_alloc_div(dst);
522 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
524 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
527 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
529 struct isl_basic_map *dup;
533 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
534 bmap->n_div, bmap->n_eq, bmap->n_ineq);
537 dup_constraints(dup, bmap);
538 dup->flags = bmap->flags;
539 dup->sample = isl_vec_copy(bmap->sample);
543 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
545 struct isl_basic_map *dup;
547 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
548 return (struct isl_basic_set *)dup;
551 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
556 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
560 return isl_basic_set_dup(bset);
563 struct isl_set *isl_set_copy(struct isl_set *set)
572 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
577 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
581 bmap = isl_basic_map_dup(bmap);
583 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
587 struct isl_map *isl_map_copy(struct isl_map *map)
596 void isl_basic_map_free(struct isl_basic_map *bmap)
604 isl_ctx_deref(bmap->ctx);
606 isl_blk_free(bmap->ctx, bmap->block2);
608 isl_blk_free(bmap->ctx, bmap->block);
609 isl_vec_free(bmap->sample);
610 isl_dim_free(bmap->dim);
614 void isl_basic_set_free(struct isl_basic_set *bset)
616 isl_basic_map_free((struct isl_basic_map *)bset);
619 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
621 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
624 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
630 isl_assert(ctx, room_for_con(bmap, 1), return -1);
631 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
633 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
634 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
635 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
636 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
637 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
638 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
640 int j = isl_basic_map_alloc_inequality(bmap);
644 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
645 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
652 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
653 bmap->extra - bmap->n_div);
657 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
659 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
662 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
666 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
671 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
673 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
676 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
681 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
683 if (pos != bmap->n_eq - 1) {
685 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
686 bmap->eq[bmap->n_eq - 1] = t;
692 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
694 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
697 void isl_basic_map_inequality_to_equality(
698 struct isl_basic_map *bmap, unsigned pos)
703 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
704 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
709 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
710 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
711 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
712 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
715 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
717 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
720 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
726 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
727 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
728 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
729 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
730 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
731 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
732 1 + isl_basic_map_total_dim(bmap),
733 bmap->extra - bmap->n_div);
734 return bmap->n_ineq++;
737 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
739 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
742 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
746 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
751 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
753 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
756 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
761 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
763 if (pos != bmap->n_ineq - 1) {
765 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
766 bmap->ineq[bmap->n_ineq - 1] = t;
767 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
773 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
775 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
778 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
783 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
786 k = isl_basic_map_alloc_equality(bmap);
789 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
792 isl_basic_map_free(bmap);
796 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
799 return (isl_basic_set *)
800 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
803 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
808 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
811 k = isl_basic_map_alloc_inequality(bmap);
814 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
817 isl_basic_map_free(bmap);
821 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
824 return (isl_basic_set *)
825 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
828 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
832 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
833 isl_seq_clr(bmap->div[bmap->n_div] +
834 1 + 1 + isl_basic_map_total_dim(bmap),
835 bmap->extra - bmap->n_div);
836 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
837 return bmap->n_div++;
840 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
842 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
845 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
849 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
854 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
856 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
859 /* Copy constraint from src to dst, putting the vars of src at offset
860 * dim_off in dst and the divs of src at offset div_off in dst.
861 * If both sets are actually map, then dim_off applies to the input
864 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
865 struct isl_basic_map *src_map, isl_int *src,
866 unsigned in_off, unsigned out_off, unsigned div_off)
868 unsigned src_nparam = isl_basic_map_n_param(src_map);
869 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
870 unsigned src_in = isl_basic_map_n_in(src_map);
871 unsigned dst_in = isl_basic_map_n_in(dst_map);
872 unsigned src_out = isl_basic_map_n_out(src_map);
873 unsigned dst_out = isl_basic_map_n_out(dst_map);
874 isl_int_set(dst[0], src[0]);
875 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
876 if (dst_nparam > src_nparam)
877 isl_seq_clr(dst+1+src_nparam,
878 dst_nparam - src_nparam);
879 isl_seq_clr(dst+1+dst_nparam, in_off);
880 isl_seq_cpy(dst+1+dst_nparam+in_off,
882 isl_min(dst_in-in_off, src_in));
883 if (dst_in-in_off > src_in)
884 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
885 dst_in - in_off - src_in);
886 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
887 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
888 src+1+src_nparam+src_in,
889 isl_min(dst_out-out_off, src_out));
890 if (dst_out-out_off > src_out)
891 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
892 dst_out - out_off - src_out);
893 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
894 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
895 src+1+src_nparam+src_in+src_out,
896 isl_min(dst_map->extra-div_off, src_map->n_div));
897 if (dst_map->n_div-div_off > src_map->n_div)
898 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
899 div_off+src_map->n_div,
900 dst_map->n_div - div_off - src_map->n_div);
903 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
904 struct isl_basic_map *src_map, isl_int *src,
905 unsigned in_off, unsigned out_off, unsigned div_off)
907 isl_int_set(dst[0], src[0]);
908 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
911 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
912 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
917 if (!bmap1 || !bmap2)
920 div_off = bmap1->n_div;
922 for (i = 0; i < bmap2->n_eq; ++i) {
923 int i1 = isl_basic_map_alloc_equality(bmap1);
926 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
927 i_pos, o_pos, div_off);
930 for (i = 0; i < bmap2->n_ineq; ++i) {
931 int i1 = isl_basic_map_alloc_inequality(bmap1);
934 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
935 i_pos, o_pos, div_off);
938 for (i = 0; i < bmap2->n_div; ++i) {
939 int i1 = isl_basic_map_alloc_div(bmap1);
942 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
943 i_pos, o_pos, div_off);
946 isl_basic_map_free(bmap2);
951 isl_basic_map_free(bmap1);
952 isl_basic_map_free(bmap2);
956 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
957 struct isl_dim_map *dim_map)
961 for (i = 0; i < dim_map->len; ++i) {
962 if (dim_map->pos[i] < 0)
963 isl_int_set_si(dst[i], 0);
965 isl_int_set(dst[i], src[dim_map->pos[i]]);
969 static void copy_div_dim_map(isl_int *dst, isl_int *src,
970 struct isl_dim_map *dim_map)
972 isl_int_set(dst[0], src[0]);
973 copy_constraint_dim_map(dst+1, src+1, dim_map);
976 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
977 struct isl_basic_map *src, struct isl_dim_map *dim_map)
981 if (!src || !dst || !dim_map)
984 for (i = 0; i < src->n_eq; ++i) {
985 int i1 = isl_basic_map_alloc_equality(dst);
988 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
991 for (i = 0; i < src->n_ineq; ++i) {
992 int i1 = isl_basic_map_alloc_inequality(dst);
995 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
998 for (i = 0; i < src->n_div; ++i) {
999 int i1 = isl_basic_map_alloc_div(dst);
1002 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
1006 isl_basic_map_free(src);
1011 isl_basic_map_free(src);
1012 isl_basic_map_free(dst);
1016 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1017 struct isl_basic_set *bset2, unsigned pos)
1019 return (struct isl_basic_set *)
1020 add_constraints((struct isl_basic_map *)bset1,
1021 (struct isl_basic_map *)bset2, 0, pos);
1024 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1025 struct isl_dim *dim, unsigned extra,
1026 unsigned n_eq, unsigned n_ineq)
1028 struct isl_basic_map *ext;
1038 dims_ok = isl_dim_equal(base->dim, dim) &&
1039 base->extra >= base->n_div + extra;
1041 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1042 room_for_ineq(base, n_ineq)) {
1047 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1048 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1049 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1050 extra += base->extra;
1052 n_ineq += base->n_ineq;
1054 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1060 ext->sample = isl_vec_copy(base->sample);
1061 flags = base->flags;
1062 ext = add_constraints(ext, base, 0, 0);
1065 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1072 isl_basic_map_free(base);
1076 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1077 struct isl_dim *dim, unsigned extra,
1078 unsigned n_eq, unsigned n_ineq)
1080 return (struct isl_basic_set *)
1081 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1082 extra, n_eq, n_ineq);
1085 struct isl_basic_map *isl_basic_map_extend_constraints(
1086 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1090 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1094 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1095 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1096 unsigned n_eq, unsigned n_ineq)
1098 struct isl_basic_map *bmap;
1099 struct isl_dim *dim;
1103 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1107 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1110 isl_basic_map_free(base);
1114 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1115 unsigned nparam, unsigned dim, unsigned extra,
1116 unsigned n_eq, unsigned n_ineq)
1118 return (struct isl_basic_set *)
1119 isl_basic_map_extend((struct isl_basic_map *)base,
1120 nparam, 0, dim, extra, n_eq, n_ineq);
1123 struct isl_basic_set *isl_basic_set_extend_constraints(
1124 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1126 return (struct isl_basic_set *)
1127 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1131 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1133 return (struct isl_basic_set *)
1134 isl_basic_map_cow((struct isl_basic_map *)bset);
1137 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1142 if (bmap->ref > 1) {
1144 bmap = isl_basic_map_dup(bmap);
1147 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1151 struct isl_set *isl_set_cow(struct isl_set *set)
1159 return isl_set_dup(set);
1162 struct isl_map *isl_map_cow(struct isl_map *map)
1170 return isl_map_dup(map);
1173 static void swap_vars(struct isl_blk blk, isl_int *a,
1174 unsigned a_len, unsigned b_len)
1176 isl_seq_cpy(blk.data, a+a_len, b_len);
1177 isl_seq_cpy(blk.data+b_len, a, a_len);
1178 isl_seq_cpy(a, blk.data, b_len+a_len);
1181 struct isl_basic_set *isl_basic_set_swap_vars(
1182 struct isl_basic_set *bset, unsigned n)
1192 nparam = isl_basic_set_n_param(bset);
1193 dim = isl_basic_set_n_dim(bset);
1194 isl_assert(bset->ctx, n <= dim, goto error);
1199 bset = isl_basic_set_cow(bset);
1203 blk = isl_blk_alloc(bset->ctx, dim);
1204 if (isl_blk_is_error(blk))
1207 for (i = 0; i < bset->n_eq; ++i)
1209 bset->eq[i]+1+nparam, n, dim - n);
1211 for (i = 0; i < bset->n_ineq; ++i)
1213 bset->ineq[i]+1+nparam, n, dim - n);
1215 for (i = 0; i < bset->n_div; ++i)
1217 bset->div[i]+1+1+nparam, n, dim - n);
1219 isl_blk_free(bset->ctx, blk);
1221 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1222 return isl_basic_set_gauss(bset, NULL);
1224 isl_basic_set_free(bset);
1228 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1231 set = isl_set_cow(set);
1235 for (i = 0; i < set->n; ++i) {
1236 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1242 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1246 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1252 total = isl_basic_map_total_dim(bmap);
1253 isl_basic_map_free_div(bmap, bmap->n_div);
1254 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1256 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1258 i = isl_basic_map_alloc_equality(bmap);
1262 isl_int_set_si(bmap->eq[i][0], 1);
1263 isl_seq_clr(bmap->eq[i]+1, total);
1264 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1265 isl_vec_free(bmap->sample);
1266 bmap->sample = NULL;
1267 return isl_basic_map_finalize(bmap);
1269 isl_basic_map_free(bmap);
1273 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1275 return (struct isl_basic_set *)
1276 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1279 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1282 unsigned off = isl_dim_total(bmap->dim);
1283 isl_int *t = bmap->div[a];
1284 bmap->div[a] = bmap->div[b];
1287 for (i = 0; i < bmap->n_eq; ++i)
1288 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1290 for (i = 0; i < bmap->n_ineq; ++i)
1291 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1293 for (i = 0; i < bmap->n_div; ++i)
1294 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1295 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1298 /* Eliminate the specified n dimensions starting at first from the
1299 * constraints using Fourier-Motzkin. The dimensions themselves
1302 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1303 enum isl_dim_type type, unsigned first, unsigned n)
1312 map = isl_map_cow(map);
1315 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1316 first += pos(map->dim, type) - 1;
1318 for (i = 0; i < map->n; ++i) {
1319 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1329 /* Eliminate the specified n dimensions starting at first from the
1330 * constraints using Fourier-Motzkin. The dimensions themselves
1333 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1334 enum isl_dim_type type, unsigned first, unsigned n)
1336 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1339 /* Eliminate the specified n dimensions starting at first from the
1340 * constraints using Fourier-Motzkin. The dimensions themselves
1343 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1344 unsigned first, unsigned n)
1346 return isl_set_eliminate(set, isl_dim_set, first, n);
1349 /* Project out n dimensions starting at first using Fourier-Motzkin */
1350 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1351 unsigned first, unsigned n)
1353 set = isl_set_eliminate_dims(set, first, n);
1354 set = isl_set_drop_dims(set, first, n);
1358 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1360 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1368 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1377 set = isl_set_cow(set);
1381 for (i = 0; i < set->n; ++i) {
1382 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1392 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1393 enum isl_dim_type type, unsigned first, unsigned n)
1397 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1401 bmap = isl_basic_map_eliminate_vars(bmap,
1402 isl_basic_map_offset(bmap, type) - 1 + first, n);
1405 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1407 bmap = isl_basic_map_drop(bmap, type, first, n);
1410 isl_basic_map_free(bmap);
1414 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1415 enum isl_dim_type type, unsigned first, unsigned n)
1417 return (isl_basic_set *)
1418 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1421 struct isl_map *isl_map_remove(struct isl_map *map,
1422 enum isl_dim_type type, unsigned first, unsigned n)
1429 map = isl_map_cow(map);
1432 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1434 for (i = 0; i < map->n; ++i) {
1435 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1436 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1440 map = isl_map_drop(map, type, first, n);
1447 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1448 enum isl_dim_type type, unsigned first, unsigned n)
1450 return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1453 /* Project out n inputs starting at first using Fourier-Motzkin */
1454 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1455 unsigned first, unsigned n)
1457 return isl_map_remove(map, isl_dim_in, first, n);
1460 /* Project out n dimensions starting at first using Fourier-Motzkin */
1461 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1462 unsigned first, unsigned n)
1464 unsigned nparam = isl_basic_set_n_param(bset);
1465 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1466 bset = isl_basic_set_drop_dims(bset, first, n);
1470 static void dump_term(struct isl_basic_map *bmap,
1471 isl_int c, int pos, FILE *out)
1474 unsigned in = isl_basic_map_n_in(bmap);
1475 unsigned dim = in + isl_basic_map_n_out(bmap);
1476 unsigned nparam = isl_basic_map_n_param(bmap);
1478 isl_int_print(out, c, 0);
1480 if (!isl_int_is_one(c))
1481 isl_int_print(out, c, 0);
1482 if (pos < 1 + nparam) {
1483 name = isl_dim_get_name(bmap->dim,
1484 isl_dim_param, pos - 1);
1486 fprintf(out, "%s", name);
1488 fprintf(out, "p%d", pos - 1);
1489 } else if (pos < 1 + nparam + in)
1490 fprintf(out, "i%d", pos - 1 - nparam);
1491 else if (pos < 1 + nparam + dim)
1492 fprintf(out, "o%d", pos - 1 - nparam - in);
1494 fprintf(out, "e%d", pos - 1 - nparam - dim);
1498 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1499 int sign, FILE *out)
1503 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1507 for (i = 0, first = 1; i < len; ++i) {
1508 if (isl_int_sgn(c[i]) * sign <= 0)
1511 fprintf(out, " + ");
1513 isl_int_abs(v, c[i]);
1514 dump_term(bmap, v, i, out);
1521 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1522 const char *op, FILE *out, int indent)
1526 fprintf(out, "%*s", indent, "");
1528 dump_constraint_sign(bmap, c, 1, out);
1529 fprintf(out, " %s ", op);
1530 dump_constraint_sign(bmap, c, -1, out);
1534 for (i = bmap->n_div; i < bmap->extra; ++i) {
1535 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1537 fprintf(out, "%*s", indent, "");
1538 fprintf(out, "ERROR: unused div coefficient not zero\n");
1543 static void dump_constraints(struct isl_basic_map *bmap,
1544 isl_int **c, unsigned n,
1545 const char *op, FILE *out, int indent)
1549 for (i = 0; i < n; ++i)
1550 dump_constraint(bmap, c[i], op, out, indent);
1553 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1557 unsigned total = isl_basic_map_total_dim(bmap);
1559 for (j = 0; j < 1 + total; ++j) {
1560 if (isl_int_is_zero(exp[j]))
1562 if (!first && isl_int_is_pos(exp[j]))
1564 dump_term(bmap, exp[j], j, out);
1569 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1573 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1574 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1576 for (i = 0; i < bmap->n_div; ++i) {
1577 fprintf(out, "%*s", indent, "");
1578 fprintf(out, "e%d = [(", i);
1579 dump_affine(bmap, bmap->div[i]+1, out);
1581 isl_int_print(out, bmap->div[i][0], 0);
1582 fprintf(out, "]\n");
1586 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1589 fprintf(out, "null basic set\n");
1593 fprintf(out, "%*s", indent, "");
1594 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1595 bset->ref, bset->dim->nparam, bset->dim->n_out,
1596 bset->extra, bset->flags);
1597 dump((struct isl_basic_map *)bset, out, indent);
1600 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1603 fprintf(out, "null basic map\n");
1607 fprintf(out, "%*s", indent, "");
1608 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1609 "flags: %x, n_name: %d\n",
1611 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1612 bmap->extra, bmap->flags, bmap->dim->n_name);
1613 dump(bmap, out, indent);
1616 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1621 total = isl_basic_map_total_dim(bmap);
1622 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1623 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1624 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1625 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1629 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1631 struct isl_set *set;
1635 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1636 isl_assert(dim->ctx, n >= 0, return NULL);
1637 set = isl_alloc(dim->ctx, struct isl_set,
1638 sizeof(struct isl_set) +
1639 (n - 1) * sizeof(struct isl_basic_set *));
1643 set->ctx = dim->ctx;
1644 isl_ctx_ref(set->ctx);
1656 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1657 unsigned nparam, unsigned dim, int n, unsigned flags)
1659 struct isl_set *set;
1660 struct isl_dim *dims;
1662 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1666 set = isl_set_alloc_dim(dims, n, flags);
1670 /* Make sure "map" has room for at least "n" more basic maps.
1672 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1675 struct isl_map *grown = NULL;
1679 isl_assert(map->ctx, n >= 0, goto error);
1680 if (map->n + n <= map->size)
1682 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1685 for (i = 0; i < map->n; ++i) {
1686 grown->p[i] = isl_basic_map_copy(map->p[i]);
1694 isl_map_free(grown);
1699 /* Make sure "set" has room for at least "n" more basic sets.
1701 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1703 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1706 struct isl_set *isl_set_dup(struct isl_set *set)
1709 struct isl_set *dup;
1714 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1717 for (i = 0; i < set->n; ++i)
1718 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1722 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1724 struct isl_set *set;
1729 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1731 isl_basic_set_free(bset);
1734 return isl_set_add_basic_set(set, bset);
1737 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1739 struct isl_map *map;
1744 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1746 isl_basic_map_free(bmap);
1749 return isl_map_add_basic_map(map, bmap);
1752 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1753 __isl_take isl_basic_set *bset)
1755 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1756 (struct isl_basic_map *)bset);
1759 void isl_set_free(struct isl_set *set)
1769 isl_ctx_deref(set->ctx);
1770 for (i = 0; i < set->n; ++i)
1771 isl_basic_set_free(set->p[i]);
1772 isl_dim_free(set->dim);
1776 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1781 fprintf(out, "null set\n");
1785 fprintf(out, "%*s", indent, "");
1786 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1787 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1789 for (i = 0; i < set->n; ++i) {
1790 fprintf(out, "%*s", indent, "");
1791 fprintf(out, "basic set %d:\n", i);
1792 isl_basic_set_dump(set->p[i], out, indent+4);
1796 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1801 fprintf(out, "null map\n");
1805 fprintf(out, "%*s", indent, "");
1806 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1807 "flags: %x, n_name: %d\n",
1808 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1809 map->dim->n_out, map->flags, map->dim->n_name);
1810 for (i = 0; i < map->n; ++i) {
1811 fprintf(out, "%*s", indent, "");
1812 fprintf(out, "basic map %d:\n", i);
1813 isl_basic_map_dump(map->p[i], out, indent+4);
1817 struct isl_basic_map *isl_basic_map_intersect_domain(
1818 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1820 struct isl_basic_map *bmap_domain;
1821 struct isl_dim *dim;
1826 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1827 bset->dim, isl_dim_param), goto error);
1829 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1830 isl_assert(bset->ctx,
1831 isl_basic_map_compatible_domain(bmap, bset), goto error);
1833 bmap = isl_basic_map_cow(bmap);
1836 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1837 bset->n_div, bset->n_eq, bset->n_ineq);
1838 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1839 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1840 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1842 bmap = isl_basic_map_simplify(bmap);
1843 return isl_basic_map_finalize(bmap);
1845 isl_basic_map_free(bmap);
1846 isl_basic_set_free(bset);
1850 struct isl_basic_map *isl_basic_map_intersect_range(
1851 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1853 struct isl_basic_map *bmap_range;
1858 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1859 bset->dim, isl_dim_param), goto error);
1861 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1862 isl_assert(bset->ctx,
1863 isl_basic_map_compatible_range(bmap, bset), goto error);
1865 bmap = isl_basic_map_cow(bmap);
1868 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1869 bset->n_div, bset->n_eq, bset->n_ineq);
1870 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1871 bmap = add_constraints(bmap, bmap_range, 0, 0);
1873 bmap = isl_basic_map_simplify(bmap);
1874 return isl_basic_map_finalize(bmap);
1876 isl_basic_map_free(bmap);
1877 isl_basic_set_free(bset);
1881 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1887 total = 1 + isl_basic_map_total_dim(bmap);
1888 if (total != vec->size)
1893 for (i = 0; i < bmap->n_eq; ++i) {
1894 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1895 if (!isl_int_is_zero(s)) {
1901 for (i = 0; i < bmap->n_ineq; ++i) {
1902 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1903 if (isl_int_is_neg(s)) {
1914 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1916 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1919 struct isl_basic_map *isl_basic_map_intersect(
1920 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1922 struct isl_vec *sample = NULL;
1924 if (!bmap1 || !bmap2)
1927 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1928 bmap2->dim, isl_dim_param), goto error);
1929 if (isl_dim_total(bmap1->dim) ==
1930 isl_dim_size(bmap1->dim, isl_dim_param) &&
1931 isl_dim_total(bmap2->dim) !=
1932 isl_dim_size(bmap2->dim, isl_dim_param))
1933 return isl_basic_map_intersect(bmap2, bmap1);
1935 if (isl_dim_total(bmap2->dim) !=
1936 isl_dim_size(bmap2->dim, isl_dim_param))
1937 isl_assert(bmap1->ctx,
1938 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1940 if (bmap1->sample &&
1941 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1942 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1943 sample = isl_vec_copy(bmap1->sample);
1944 else if (bmap2->sample &&
1945 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1946 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1947 sample = isl_vec_copy(bmap2->sample);
1949 bmap1 = isl_basic_map_cow(bmap1);
1952 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1953 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1954 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1957 isl_vec_free(sample);
1959 isl_vec_free(bmap1->sample);
1960 bmap1->sample = sample;
1963 bmap1 = isl_basic_map_simplify(bmap1);
1964 return isl_basic_map_finalize(bmap1);
1967 isl_vec_free(sample);
1968 isl_basic_map_free(bmap1);
1969 isl_basic_map_free(bmap2);
1973 struct isl_basic_set *isl_basic_set_intersect(
1974 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1976 return (struct isl_basic_set *)
1977 isl_basic_map_intersect(
1978 (struct isl_basic_map *)bset1,
1979 (struct isl_basic_map *)bset2);
1982 /* Special case of isl_map_intersect, where both map1 and map2
1983 * are convex, without any divs and such that either map1 or map2
1984 * contains a single constraint. This constraint is then simply
1985 * added to the other map.
1987 static __isl_give isl_map *map_intersect_add_constraint(
1988 __isl_take isl_map *map1, __isl_take isl_map *map2)
1990 struct isl_basic_map *bmap1;
1991 struct isl_basic_map *bmap2;
1993 isl_assert(map1->ctx, map1->n == 1, goto error);
1994 isl_assert(map2->ctx, map1->n == 1, goto error);
1995 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
1996 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
1998 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
1999 return isl_map_intersect(map2, map1);
2001 isl_assert(map2->ctx,
2002 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2004 map1 = isl_map_cow(map1);
2007 if (isl_map_fast_is_empty(map1)) {
2011 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2012 if (map2->p[0]->n_eq == 1)
2013 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2015 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2016 map2->p[0]->ineq[0]);
2018 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2019 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2023 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2024 isl_basic_map_free(map1->p[0]);
2037 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2040 struct isl_map *result;
2046 if (isl_map_fast_is_empty(map1)) {
2050 if (isl_map_fast_is_empty(map2)) {
2055 if (map1->n == 1 && map2->n == 1 &&
2056 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2057 isl_dim_equal(map1->dim, map2->dim) &&
2058 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2059 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2060 return map_intersect_add_constraint(map1, map2);
2061 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2062 map2->dim, isl_dim_param), goto error);
2063 if (isl_dim_total(map1->dim) ==
2064 isl_dim_size(map1->dim, isl_dim_param) &&
2065 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2066 return isl_map_intersect(map2, map1);
2068 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2069 isl_assert(map1->ctx,
2070 isl_dim_equal(map1->dim, map2->dim), goto error);
2072 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2073 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2074 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2076 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2077 map1->n * map2->n, flags);
2080 for (i = 0; i < map1->n; ++i)
2081 for (j = 0; j < map2->n; ++j) {
2082 struct isl_basic_map *part;
2083 part = isl_basic_map_intersect(
2084 isl_basic_map_copy(map1->p[i]),
2085 isl_basic_map_copy(map2->p[j]));
2086 if (isl_basic_map_is_empty(part))
2087 isl_basic_map_free(part);
2089 result = isl_map_add_basic_map(result, part);
2102 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2104 return (struct isl_set *)
2105 isl_map_intersect((struct isl_map *)set1,
2106 (struct isl_map *)set2);
2109 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2111 struct isl_dim *dim;
2112 struct isl_basic_set *bset;
2117 bmap = isl_basic_map_cow(bmap);
2120 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2121 in = isl_basic_map_n_in(bmap);
2122 bset = isl_basic_set_from_basic_map(bmap);
2123 bset = isl_basic_set_swap_vars(bset, in);
2124 return isl_basic_map_from_basic_set(bset, dim);
2127 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2128 enum isl_dim_type type, unsigned pos, unsigned n)
2130 struct isl_dim *res_dim;
2131 struct isl_basic_map *res;
2132 struct isl_dim_map *dim_map;
2133 unsigned total, off;
2134 enum isl_dim_type t;
2142 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2144 total = isl_basic_map_total_dim(bmap) + n;
2145 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2147 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2149 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2151 unsigned size = isl_basic_map_dim(bmap, t);
2152 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2154 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2155 pos, size - pos, off + pos + n);
2157 off += isl_dim_size(res_dim, t);
2159 isl_dim_map_div(dim_map, bmap, off);
2161 res = isl_basic_map_alloc_dim(res_dim,
2162 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2163 res = add_constraints_dim_map(res, bmap, dim_map);
2164 res = isl_basic_map_simplify(res);
2165 return isl_basic_map_finalize(res);
2168 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2169 enum isl_dim_type type, unsigned n)
2173 return isl_basic_map_insert(bmap, type,
2174 isl_basic_map_dim(bmap, type), n);
2177 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2178 enum isl_dim_type type, unsigned n)
2182 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2183 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2185 isl_basic_set_free(bset);
2189 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2190 enum isl_dim_type type, unsigned pos, unsigned n)
2197 map = isl_map_cow(map);
2201 map->dim = isl_dim_insert(map->dim, type, pos, n);
2205 for (i = 0; i < map->n; ++i) {
2206 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2217 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2218 enum isl_dim_type type, unsigned pos, unsigned n)
2220 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2223 __isl_give isl_map *isl_map_add(__isl_take isl_map *map,
2224 enum isl_dim_type type, unsigned n)
2228 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2231 __isl_give isl_set *isl_set_add(__isl_take isl_set *set,
2232 enum isl_dim_type type, unsigned n)
2236 isl_assert(set->ctx, type != isl_dim_in, goto error);
2237 return (isl_set *)isl_map_add((isl_map *)set, type, n);
2243 __isl_give isl_basic_map *isl_basic_map_move_dims(
2244 __isl_take isl_basic_map *bmap,
2245 enum isl_dim_type dst_type, unsigned dst_pos,
2246 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2249 struct isl_dim_map *dim_map;
2250 struct isl_basic_map *res;
2251 enum isl_dim_type t;
2252 unsigned total, off;
2259 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2262 if (dst_type == src_type && dst_pos == src_pos)
2265 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2267 if (pos(bmap->dim, dst_type) + dst_pos ==
2268 pos(bmap->dim, src_type) + src_pos +
2269 ((src_type < dst_type) ? n : 0)) {
2270 bmap = isl_basic_map_cow(bmap);
2274 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2275 src_type, src_pos, n);
2279 bmap = isl_basic_map_finalize(bmap);
2284 total = isl_basic_map_total_dim(bmap);
2285 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2288 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2289 unsigned size = isl_dim_size(bmap->dim, t);
2290 if (t == dst_type) {
2291 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2294 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2297 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2298 dst_pos, size - dst_pos, off);
2299 off += size - dst_pos;
2300 } else if (t == src_type) {
2301 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2304 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2305 src_pos + n, size - src_pos - n, off);
2306 off += size - src_pos - n;
2308 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2312 isl_dim_map_div(dim_map, bmap, off);
2314 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2315 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2316 bmap = add_constraints_dim_map(res, bmap, dim_map);
2318 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2319 src_type, src_pos, n);
2323 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2324 bmap = isl_basic_map_gauss(bmap, NULL);
2325 bmap = isl_basic_map_finalize(bmap);
2329 isl_basic_map_free(bmap);
2333 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2334 enum isl_dim_type dst_type, unsigned dst_pos,
2335 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2337 return (isl_basic_set *)isl_basic_map_move_dims(
2338 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2341 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2342 enum isl_dim_type dst_type, unsigned dst_pos,
2343 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2347 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2348 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2349 src_type, src_pos, n);
2355 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2356 enum isl_dim_type dst_type, unsigned dst_pos,
2357 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2366 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2369 if (dst_type == src_type && dst_pos == src_pos)
2372 isl_assert(map->ctx, dst_type != src_type, goto error);
2374 map = isl_map_cow(map);
2378 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2382 for (i = 0; i < map->n; ++i) {
2383 map->p[i] = isl_basic_map_move_dims(map->p[i],
2385 src_type, src_pos, n);
2396 /* Move the specified dimensions to the last columns right before
2397 * the divs. Don't change the dimension specification of bmap.
2398 * That's the responsibility of the caller.
2400 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2401 enum isl_dim_type type, unsigned first, unsigned n)
2403 struct isl_dim_map *dim_map;
2404 struct isl_basic_map *res;
2405 enum isl_dim_type t;
2406 unsigned total, off;
2410 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2413 total = isl_basic_map_total_dim(bmap);
2414 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2417 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2418 unsigned size = isl_dim_size(bmap->dim, t);
2420 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2423 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2424 first, n, total - bmap->n_div - n);
2425 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2426 first + n, size - (first + n), off);
2427 off += size - (first + n);
2429 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2433 isl_dim_map_div(dim_map, bmap, off + n);
2435 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2436 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2437 res = add_constraints_dim_map(res, bmap, dim_map);
2441 /* Turn the n dimensions of type type, starting at first
2442 * into existentially quantified variables.
2444 __isl_give isl_basic_map *isl_basic_map_project_out(
2445 __isl_take isl_basic_map *bmap,
2446 enum isl_dim_type type, unsigned first, unsigned n)
2459 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2460 return isl_basic_map_remove(bmap, type, first, n);
2462 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2465 bmap = move_last(bmap, type, first, n);
2466 bmap = isl_basic_map_cow(bmap);
2470 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2471 old = bmap->block2.data;
2472 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2473 (bmap->extra + n) * (1 + row_size));
2474 if (!bmap->block2.data)
2476 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2479 for (i = 0; i < n; ++i) {
2480 new_div[i] = bmap->block2.data +
2481 (bmap->extra + i) * (1 + row_size);
2482 isl_seq_clr(new_div[i], 1 + row_size);
2484 for (i = 0; i < bmap->extra; ++i)
2485 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2487 bmap->div = new_div;
2491 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2494 bmap = isl_basic_map_simplify(bmap);
2495 bmap = isl_basic_map_drop_redundant_divs(bmap);
2496 return isl_basic_map_finalize(bmap);
2498 isl_basic_map_free(bmap);
2502 /* Turn the n dimensions of type type, starting at first
2503 * into existentially quantified variables.
2505 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2506 enum isl_dim_type type, unsigned first, unsigned n)
2508 return (isl_basic_set *)isl_basic_map_project_out(
2509 (isl_basic_map *)bset, type, first, n);
2512 /* Turn the n dimensions of type type, starting at first
2513 * into existentially quantified variables.
2515 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2516 enum isl_dim_type type, unsigned first, unsigned n)
2526 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2528 map = isl_map_cow(map);
2532 map->dim = isl_dim_drop(map->dim, type, first, n);
2536 for (i = 0; i < map->n; ++i) {
2537 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2548 /* Turn the n dimensions of type type, starting at first
2549 * into existentially quantified variables.
2551 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2552 enum isl_dim_type type, unsigned first, unsigned n)
2554 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2557 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2561 for (i = 0; i < n; ++i) {
2562 j = isl_basic_map_alloc_div(bmap);
2565 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2569 isl_basic_map_free(bmap);
2573 struct isl_basic_map *isl_basic_map_apply_range(
2574 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2576 struct isl_dim *dim_result = NULL;
2577 struct isl_basic_map *bmap;
2578 unsigned n_in, n_out, n, nparam, total, pos;
2579 struct isl_dim_map *dim_map1, *dim_map2;
2581 if (!bmap1 || !bmap2)
2584 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2585 isl_dim_copy(bmap2->dim));
2587 n_in = isl_basic_map_n_in(bmap1);
2588 n_out = isl_basic_map_n_out(bmap2);
2589 n = isl_basic_map_n_out(bmap1);
2590 nparam = isl_basic_map_n_param(bmap1);
2592 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2593 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2594 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2595 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2596 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2597 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2598 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2599 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2600 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2601 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2602 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2604 bmap = isl_basic_map_alloc_dim(dim_result,
2605 bmap1->n_div + bmap2->n_div + n,
2606 bmap1->n_eq + bmap2->n_eq,
2607 bmap1->n_ineq + bmap2->n_ineq);
2608 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2609 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2610 bmap = add_divs(bmap, n);
2611 bmap = isl_basic_map_simplify(bmap);
2612 bmap = isl_basic_map_drop_redundant_divs(bmap);
2613 return isl_basic_map_finalize(bmap);
2615 isl_basic_map_free(bmap1);
2616 isl_basic_map_free(bmap2);
2620 struct isl_basic_set *isl_basic_set_apply(
2621 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2626 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2629 return (struct isl_basic_set *)
2630 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2632 isl_basic_set_free(bset);
2633 isl_basic_map_free(bmap);
2637 struct isl_basic_map *isl_basic_map_apply_domain(
2638 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2640 if (!bmap1 || !bmap2)
2643 isl_assert(bmap1->ctx,
2644 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2645 isl_assert(bmap1->ctx,
2646 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2649 bmap1 = isl_basic_map_reverse(bmap1);
2650 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2651 return isl_basic_map_reverse(bmap1);
2653 isl_basic_map_free(bmap1);
2654 isl_basic_map_free(bmap2);
2658 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2659 * A \cap B -> f(A) + f(B)
2661 struct isl_basic_map *isl_basic_map_sum(
2662 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2664 unsigned n_in, n_out, nparam, total, pos;
2665 struct isl_basic_map *bmap = NULL;
2666 struct isl_dim_map *dim_map1, *dim_map2;
2669 if (!bmap1 || !bmap2)
2672 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2675 nparam = isl_basic_map_n_param(bmap1);
2676 n_in = isl_basic_map_n_in(bmap1);
2677 n_out = isl_basic_map_n_out(bmap1);
2679 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2680 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2681 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2682 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2683 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2684 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2685 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2686 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2687 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2688 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2689 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2691 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2692 bmap1->n_div + bmap2->n_div + 2 * n_out,
2693 bmap1->n_eq + bmap2->n_eq + n_out,
2694 bmap1->n_ineq + bmap2->n_ineq);
2695 for (i = 0; i < n_out; ++i) {
2696 int j = isl_basic_map_alloc_equality(bmap);
2699 isl_seq_clr(bmap->eq[j], 1+total);
2700 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2701 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2702 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2704 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2705 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2706 bmap = add_divs(bmap, 2 * n_out);
2708 bmap = isl_basic_map_simplify(bmap);
2709 return isl_basic_map_finalize(bmap);
2711 isl_basic_map_free(bmap);
2712 isl_basic_map_free(bmap1);
2713 isl_basic_map_free(bmap2);
2717 /* Given two maps A -> f(A) and B -> g(B), construct a map
2718 * A \cap B -> f(A) + f(B)
2720 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2722 struct isl_map *result;
2728 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2730 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2731 map1->n * map2->n, 0);
2734 for (i = 0; i < map1->n; ++i)
2735 for (j = 0; j < map2->n; ++j) {
2736 struct isl_basic_map *part;
2737 part = isl_basic_map_sum(
2738 isl_basic_map_copy(map1->p[i]),
2739 isl_basic_map_copy(map2->p[j]));
2740 if (isl_basic_map_is_empty(part))
2741 isl_basic_map_free(part);
2743 result = isl_map_add_basic_map(result, part);
2756 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2757 __isl_take isl_set *set2)
2759 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2762 /* Given a basic map A -> f(A), construct A -> -f(A).
2764 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2769 bmap = isl_basic_map_cow(bmap);
2773 n = isl_basic_map_dim(bmap, isl_dim_out);
2774 off = isl_basic_map_offset(bmap, isl_dim_out);
2775 for (i = 0; i < bmap->n_eq; ++i)
2776 for (j = 0; j < n; ++j)
2777 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2778 for (i = 0; i < bmap->n_ineq; ++i)
2779 for (j = 0; j < n; ++j)
2780 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2781 for (i = 0; i < bmap->n_div; ++i)
2782 for (j = 0; j < n; ++j)
2783 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2784 return isl_basic_map_finalize(bmap);
2787 /* Given a map A -> f(A), construct A -> -f(A).
2789 struct isl_map *isl_map_neg(struct isl_map *map)
2793 map = isl_map_cow(map);
2797 for (i = 0; i < map->n; ++i) {
2798 map->p[i] = isl_basic_map_neg(map->p[i]);
2809 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
2811 return (isl_set *)isl_map_neg((isl_map *)set);
2814 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2815 * A -> floor(f(A)/d).
2817 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2820 unsigned n_in, n_out, nparam, total, pos;
2821 struct isl_basic_map *result = NULL;
2822 struct isl_dim_map *dim_map;
2828 nparam = isl_basic_map_n_param(bmap);
2829 n_in = isl_basic_map_n_in(bmap);
2830 n_out = isl_basic_map_n_out(bmap);
2832 total = nparam + n_in + n_out + bmap->n_div + n_out;
2833 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2834 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2835 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2836 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2837 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2839 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2840 bmap->n_div + n_out,
2841 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2842 result = add_constraints_dim_map(result, bmap, dim_map);
2843 result = add_divs(result, n_out);
2844 for (i = 0; i < n_out; ++i) {
2846 j = isl_basic_map_alloc_inequality(result);
2849 isl_seq_clr(result->ineq[j], 1+total);
2850 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2851 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2852 j = isl_basic_map_alloc_inequality(result);
2855 isl_seq_clr(result->ineq[j], 1+total);
2856 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2857 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2858 isl_int_sub_ui(result->ineq[j][0], d, 1);
2861 result = isl_basic_map_simplify(result);
2862 return isl_basic_map_finalize(result);
2864 isl_basic_map_free(result);
2868 /* Given a map A -> f(A) and an integer d, construct a map
2869 * A -> floor(f(A)/d).
2871 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2875 map = isl_map_cow(map);
2879 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2880 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2881 for (i = 0; i < map->n; ++i) {
2882 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2893 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2899 i = isl_basic_map_alloc_equality(bmap);
2902 nparam = isl_basic_map_n_param(bmap);
2903 n_in = isl_basic_map_n_in(bmap);
2904 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2905 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2906 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2907 return isl_basic_map_finalize(bmap);
2909 isl_basic_map_free(bmap);
2913 /* Add a constraints to "bmap" expressing i_pos < o_pos
2915 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2921 i = isl_basic_map_alloc_inequality(bmap);
2924 nparam = isl_basic_map_n_param(bmap);
2925 n_in = isl_basic_map_n_in(bmap);
2926 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2927 isl_int_set_si(bmap->ineq[i][0], -1);
2928 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2929 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2930 return isl_basic_map_finalize(bmap);
2932 isl_basic_map_free(bmap);
2936 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2938 static __isl_give isl_basic_map *var_less_or_equal(
2939 __isl_take isl_basic_map *bmap, unsigned pos)
2945 i = isl_basic_map_alloc_inequality(bmap);
2948 nparam = isl_basic_map_n_param(bmap);
2949 n_in = isl_basic_map_n_in(bmap);
2950 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2951 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2952 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2953 return isl_basic_map_finalize(bmap);
2955 isl_basic_map_free(bmap);
2959 /* Add a constraints to "bmap" expressing i_pos > o_pos
2961 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2967 i = isl_basic_map_alloc_inequality(bmap);
2970 nparam = isl_basic_map_n_param(bmap);
2971 n_in = isl_basic_map_n_in(bmap);
2972 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2973 isl_int_set_si(bmap->ineq[i][0], -1);
2974 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2975 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2976 return isl_basic_map_finalize(bmap);
2978 isl_basic_map_free(bmap);
2982 /* Add a constraint to "bmap" expressing i_pos >= o_pos
2984 static __isl_give isl_basic_map *var_more_or_equal(
2985 __isl_take isl_basic_map *bmap, unsigned pos)
2991 i = isl_basic_map_alloc_inequality(bmap);
2994 nparam = isl_basic_map_n_param(bmap);
2995 n_in = isl_basic_map_n_in(bmap);
2996 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2997 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2998 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2999 return isl_basic_map_finalize(bmap);
3001 isl_basic_map_free(bmap);
3005 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3008 struct isl_basic_map *bmap;
3009 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3012 for (i = 0; i < n_equal && bmap; ++i)
3013 bmap = var_equal(bmap, i);
3014 return isl_basic_map_finalize(bmap);
3017 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3019 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3022 struct isl_basic_map *bmap;
3023 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3026 for (i = 0; i < pos && bmap; ++i)
3027 bmap = var_equal(bmap, i);
3029 bmap = var_less(bmap, pos);
3030 return isl_basic_map_finalize(bmap);
3033 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3035 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3036 __isl_take isl_dim *dim, unsigned pos)
3039 isl_basic_map *bmap;
3041 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3042 for (i = 0; i < pos; ++i)
3043 bmap = var_equal(bmap, i);
3044 bmap = var_less_or_equal(bmap, pos);
3045 return isl_basic_map_finalize(bmap);
3048 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3050 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3053 struct isl_basic_map *bmap;
3054 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3057 for (i = 0; i < pos && bmap; ++i)
3058 bmap = var_equal(bmap, i);
3060 bmap = var_more(bmap, pos);
3061 return isl_basic_map_finalize(bmap);
3064 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3066 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3067 __isl_take isl_dim *dim, unsigned pos)
3070 isl_basic_map *bmap;
3072 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3073 for (i = 0; i < pos; ++i)
3074 bmap = var_equal(bmap, i);
3075 bmap = var_more_or_equal(bmap, pos);
3076 return isl_basic_map_finalize(bmap);
3079 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3080 unsigned n, int equal)
3082 struct isl_map *map;
3085 if (n == 0 && equal)
3086 return isl_map_universe(dims);
3088 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3090 for (i = 0; i + 1 < n; ++i)
3091 map = isl_map_add_basic_map(map,
3092 isl_basic_map_less_at(isl_dim_copy(dims), i));
3095 map = isl_map_add_basic_map(map,
3096 isl_basic_map_less_or_equal_at(dims, n - 1));
3098 map = isl_map_add_basic_map(map,
3099 isl_basic_map_less_at(dims, n - 1));
3106 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3110 return map_lex_lte_first(dims, dims->n_out, equal);
3113 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3115 return map_lex_lte_first(dim, n, 0);
3118 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3120 return map_lex_lte_first(dim, n, 1);
3123 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3125 return map_lex_lte(isl_dim_map(set_dim), 0);
3128 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3130 return map_lex_lte(isl_dim_map(set_dim), 1);
3133 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3134 unsigned n, int equal)
3136 struct isl_map *map;
3139 if (n == 0 && equal)
3140 return isl_map_universe(dims);
3142 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3144 for (i = 0; i + 1 < n; ++i)
3145 map = isl_map_add_basic_map(map,
3146 isl_basic_map_more_at(isl_dim_copy(dims), i));
3149 map = isl_map_add_basic_map(map,
3150 isl_basic_map_more_or_equal_at(dims, n - 1));
3152 map = isl_map_add_basic_map(map,
3153 isl_basic_map_more_at(dims, n - 1));
3160 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3164 return map_lex_gte_first(dims, dims->n_out, equal);
3167 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3169 return map_lex_gte_first(dim, n, 0);
3172 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3174 return map_lex_gte_first(dim, n, 1);
3177 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3179 return map_lex_gte(isl_dim_map(set_dim), 0);
3182 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3184 return map_lex_gte(isl_dim_map(set_dim), 1);
3187 struct isl_basic_map *isl_basic_map_from_basic_set(
3188 struct isl_basic_set *bset, struct isl_dim *dim)
3190 struct isl_basic_map *bmap;
3192 bset = isl_basic_set_cow(bset);
3196 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3197 isl_dim_free(bset->dim);
3198 bmap = (struct isl_basic_map *) bset;
3200 return isl_basic_map_finalize(bmap);
3202 isl_basic_set_free(bset);
3207 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3211 if (bmap->dim->n_in == 0)
3212 return (struct isl_basic_set *)bmap;
3213 bmap = isl_basic_map_cow(bmap);
3216 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3219 bmap = isl_basic_map_finalize(bmap);
3220 return (struct isl_basic_set *)bmap;
3222 isl_basic_map_free(bmap);
3226 /* For a div d = floor(f/m), add the constraints
3229 * -(f-(n-1)) + m d >= 0
3231 * Note that the second constraint is the negation of
3235 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3236 unsigned pos, isl_int *div)
3239 unsigned total = isl_basic_map_total_dim(bmap);
3241 i = isl_basic_map_alloc_inequality(bmap);
3244 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3245 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3247 j = isl_basic_map_alloc_inequality(bmap);
3250 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3251 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3252 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3256 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3257 unsigned pos, isl_int *div)
3259 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3263 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3265 unsigned total = isl_basic_map_total_dim(bmap);
3266 unsigned div_pos = total - bmap->n_div + div;
3268 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3272 struct isl_basic_set *isl_basic_map_underlying_set(
3273 struct isl_basic_map *bmap)
3277 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3278 bmap->n_div == 0 && !isl_dim_get_tuple_name(bmap->dim, isl_dim_out))
3279 return (struct isl_basic_set *)bmap;
3280 bmap = isl_basic_map_cow(bmap);
3283 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3286 bmap->extra -= bmap->n_div;
3288 bmap = isl_basic_map_finalize(bmap);
3289 return (struct isl_basic_set *)bmap;
3294 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3295 __isl_take isl_basic_set *bset)
3297 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3300 struct isl_basic_map *isl_basic_map_overlying_set(
3301 struct isl_basic_set *bset, struct isl_basic_map *like)
3303 struct isl_basic_map *bmap;
3304 struct isl_ctx *ctx;
3311 isl_assert(ctx, bset->n_div == 0, goto error);
3312 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3313 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3315 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3316 isl_basic_map_free(like);
3317 return (struct isl_basic_map *)bset;
3319 bset = isl_basic_set_cow(bset);
3322 total = bset->dim->n_out + bset->extra;
3323 bmap = (struct isl_basic_map *)bset;
3324 isl_dim_free(bmap->dim);
3325 bmap->dim = isl_dim_copy(like->dim);
3328 bmap->n_div = like->n_div;
3329 bmap->extra += like->n_div;
3332 ltotal = total - bmap->extra + like->extra;
3335 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3336 bmap->extra * (1 + 1 + total));
3337 if (isl_blk_is_error(bmap->block2))
3339 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3343 for (i = 0; i < bmap->extra; ++i)
3344 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3345 for (i = 0; i < like->n_div; ++i) {
3346 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3347 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3349 bmap = isl_basic_map_extend_constraints(bmap,
3350 0, 2 * like->n_div);
3351 for (i = 0; i < like->n_div; ++i) {
3352 if (isl_int_is_zero(bmap->div[i][0]))
3354 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3358 isl_basic_map_free(like);
3359 bmap = isl_basic_map_simplify(bmap);
3360 bmap = isl_basic_map_finalize(bmap);
3363 isl_basic_map_free(like);
3364 isl_basic_set_free(bset);
3368 struct isl_basic_set *isl_basic_set_from_underlying_set(
3369 struct isl_basic_set *bset, struct isl_basic_set *like)
3371 return (struct isl_basic_set *)
3372 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3375 struct isl_set *isl_set_from_underlying_set(
3376 struct isl_set *set, struct isl_basic_set *like)
3382 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3384 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3385 isl_basic_set_free(like);
3388 set = isl_set_cow(set);
3391 for (i = 0; i < set->n; ++i) {
3392 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3393 isl_basic_set_copy(like));
3397 isl_dim_free(set->dim);
3398 set->dim = isl_dim_copy(like->dim);
3401 isl_basic_set_free(like);
3404 isl_basic_set_free(like);
3409 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3413 map = isl_map_cow(map);
3416 map->dim = isl_dim_cow(map->dim);
3420 for (i = 1; i < map->n; ++i)
3421 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3423 for (i = 0; i < map->n; ++i) {
3424 map->p[i] = (struct isl_basic_map *)
3425 isl_basic_map_underlying_set(map->p[i]);
3430 map->dim = isl_dim_underlying(map->dim, 0);
3432 isl_dim_free(map->dim);
3433 map->dim = isl_dim_copy(map->p[0]->dim);
3437 return (struct isl_set *)map;
3443 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3445 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3448 static __isl_give isl_basic_map *isl_basic_map_reset_dim(
3449 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3451 bmap = isl_basic_map_cow(bmap);
3455 isl_dim_free(bmap->dim);
3458 bmap = isl_basic_map_finalize(bmap);
3462 isl_basic_map_free(bmap);
3467 static __isl_give isl_basic_set *isl_basic_set_reset_dim(
3468 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3470 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3474 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3475 __isl_take isl_dim *dim)
3479 map = isl_map_cow(map);
3483 for (i = 0; i < map->n; ++i) {
3484 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3489 isl_dim_free(map->dim);
3499 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3500 __isl_take isl_dim *dim)
3502 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3505 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3508 struct isl_basic_set *domain;
3514 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3516 n_in = isl_basic_map_n_in(bmap);
3517 n_out = isl_basic_map_n_out(bmap);
3518 domain = isl_basic_set_from_basic_map(bmap);
3519 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3521 domain = isl_basic_set_reset_dim(domain, dim);
3526 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3528 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3531 struct isl_set *isl_map_range(struct isl_map *map)
3534 struct isl_set *set;
3538 if (isl_map_dim(map, isl_dim_in) == 0)
3539 return (isl_set *)map;
3541 map = isl_map_cow(map);
3545 set = (struct isl_set *) map;
3546 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3549 for (i = 0; i < map->n; ++i) {
3550 set->p[i] = isl_basic_map_range(map->p[i]);
3554 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3555 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3562 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3565 struct isl_map *map = NULL;
3567 set = isl_set_cow(set);
3570 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3571 map = (struct isl_map *)set;
3572 for (i = 0; i < set->n; ++i) {
3573 map->p[i] = isl_basic_map_from_basic_set(
3574 set->p[i], isl_dim_copy(dim));
3578 isl_dim_free(map->dim);
3587 struct isl_map *isl_map_from_range(struct isl_set *set)
3589 return (struct isl_map *)set;
3592 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3594 return isl_map_reverse(isl_map_from_range(set));
3597 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3598 __isl_take isl_set *range)
3600 return isl_map_apply_range(isl_map_from_domain(domain),
3601 isl_map_from_range(range));
3604 struct isl_set *isl_set_from_map(struct isl_map *map)
3607 struct isl_set *set = NULL;
3611 map = isl_map_cow(map);
3614 map->dim = isl_dim_as_set_dim(map->dim);
3617 set = (struct isl_set *)map;
3618 for (i = 0; i < map->n; ++i) {
3619 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3629 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3631 struct isl_map *map;
3635 isl_assert(dim->ctx, n >= 0, return NULL);
3636 map = isl_alloc(dim->ctx, struct isl_map,
3637 sizeof(struct isl_map) +
3638 (n - 1) * sizeof(struct isl_basic_map *));
3642 map->ctx = dim->ctx;
3643 isl_ctx_ref(map->ctx);
3655 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3656 unsigned nparam, unsigned in, unsigned out, int n,
3659 struct isl_map *map;
3660 struct isl_dim *dims;
3662 dims = isl_dim_alloc(ctx, nparam, in, out);
3666 map = isl_map_alloc_dim(dims, n, flags);
3670 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3672 struct isl_basic_map *bmap;
3673 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3674 bmap = isl_basic_map_set_to_empty(bmap);
3678 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3680 struct isl_basic_set *bset;
3681 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3682 bset = isl_basic_set_set_to_empty(bset);
3686 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3688 struct isl_basic_map *bmap;
3691 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3692 bmap = isl_basic_map_set_to_empty(bmap);
3696 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3698 struct isl_basic_map *bmap;
3701 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3702 bmap = isl_basic_map_set_to_empty(bmap);
3706 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3708 struct isl_basic_set *bset;
3711 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3712 bset = isl_basic_set_set_to_empty(bset);
3716 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3718 struct isl_basic_map *bmap;
3719 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
3723 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
3725 struct isl_basic_set *bset;
3726 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
3730 __isl_give isl_basic_map *isl_basic_map_universe_like(
3731 __isl_keep isl_basic_map *model)
3735 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3738 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3742 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3745 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3746 __isl_keep isl_set *model)
3750 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3753 struct isl_map *isl_map_empty(struct isl_dim *dim)
3755 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3758 struct isl_map *isl_map_empty_like(struct isl_map *model)
3762 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3765 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
3769 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3772 struct isl_set *isl_set_empty(struct isl_dim *dim)
3774 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3777 struct isl_set *isl_set_empty_like(struct isl_set *model)
3781 return isl_set_empty(isl_dim_copy(model->dim));
3784 struct isl_map *isl_map_universe(struct isl_dim *dim)
3786 struct isl_map *map;
3789 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3790 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
3794 struct isl_set *isl_set_universe(struct isl_dim *dim)
3796 struct isl_set *set;
3799 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3800 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
3804 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3808 return isl_set_universe(isl_dim_copy(model->dim));
3811 struct isl_map *isl_map_dup(struct isl_map *map)
3814 struct isl_map *dup;
3818 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3819 for (i = 0; i < map->n; ++i)
3820 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
3824 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
3825 __isl_take isl_basic_map *bmap)
3829 if (isl_basic_map_fast_is_empty(bmap)) {
3830 isl_basic_map_free(bmap);
3833 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3834 isl_assert(map->ctx, map->n < map->size, goto error);
3835 map->p[map->n] = bmap;
3837 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3843 isl_basic_map_free(bmap);
3847 void isl_map_free(struct isl_map *map)
3857 isl_ctx_deref(map->ctx);
3858 for (i = 0; i < map->n; ++i)
3859 isl_basic_map_free(map->p[i]);
3860 isl_dim_free(map->dim);
3864 struct isl_map *isl_map_extend(struct isl_map *base,
3865 unsigned nparam, unsigned n_in, unsigned n_out)
3869 base = isl_map_cow(base);
3873 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3876 for (i = 0; i < base->n; ++i) {
3877 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3878 isl_dim_copy(base->dim), 0, 0, 0);
3888 struct isl_set *isl_set_extend(struct isl_set *base,
3889 unsigned nparam, unsigned dim)
3891 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3895 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3896 struct isl_basic_map *bmap, unsigned pos, int value)
3900 bmap = isl_basic_map_cow(bmap);
3901 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3902 j = isl_basic_map_alloc_equality(bmap);
3905 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3906 isl_int_set_si(bmap->eq[j][pos], -1);
3907 isl_int_set_si(bmap->eq[j][0], value);
3908 bmap = isl_basic_map_simplify(bmap);
3909 return isl_basic_map_finalize(bmap);
3911 isl_basic_map_free(bmap);
3915 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3916 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3920 bmap = isl_basic_map_cow(bmap);
3921 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3922 j = isl_basic_map_alloc_equality(bmap);
3925 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3926 isl_int_set_si(bmap->eq[j][pos], -1);
3927 isl_int_set(bmap->eq[j][0], value);
3928 bmap = isl_basic_map_simplify(bmap);
3929 return isl_basic_map_finalize(bmap);
3931 isl_basic_map_free(bmap);
3935 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3936 enum isl_dim_type type, unsigned pos, int value)
3940 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3941 return isl_basic_map_fix_pos_si(bmap,
3942 isl_basic_map_offset(bmap, type) + pos, value);
3944 isl_basic_map_free(bmap);
3948 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3949 enum isl_dim_type type, unsigned pos, isl_int value)
3953 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3954 return isl_basic_map_fix_pos(bmap,
3955 isl_basic_map_offset(bmap, type) + pos, value);
3957 isl_basic_map_free(bmap);
3961 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3962 enum isl_dim_type type, unsigned pos, int value)
3964 return (struct isl_basic_set *)
3965 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3969 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3970 enum isl_dim_type type, unsigned pos, isl_int value)
3972 return (struct isl_basic_set *)
3973 isl_basic_map_fix((struct isl_basic_map *)bset,
3977 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3978 unsigned input, int value)
3980 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3983 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3984 unsigned dim, int value)
3986 return (struct isl_basic_set *)
3987 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3988 isl_dim_set, dim, value);
3991 struct isl_map *isl_map_fix_si(struct isl_map *map,
3992 enum isl_dim_type type, unsigned pos, int value)
3996 map = isl_map_cow(map);
4000 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4001 for (i = 0; i < map->n; ++i) {
4002 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4006 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4013 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4014 enum isl_dim_type type, unsigned pos, int value)
4016 return (struct isl_set *)
4017 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4020 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4021 enum isl_dim_type type, unsigned pos, isl_int value)
4025 map = isl_map_cow(map);
4029 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4030 for (i = 0; i < map->n; ++i) {
4031 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4035 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4042 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4043 enum isl_dim_type type, unsigned pos, isl_int value)
4045 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4048 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4049 unsigned input, int value)
4051 return isl_map_fix_si(map, isl_dim_in, input, value);
4054 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4056 return (struct isl_set *)
4057 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4060 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4061 __isl_take isl_basic_map *bmap,
4062 enum isl_dim_type type, unsigned pos, int value)
4068 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4069 pos += isl_basic_map_offset(bmap, type);
4070 bmap = isl_basic_map_cow(bmap);
4071 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4072 j = isl_basic_map_alloc_inequality(bmap);
4075 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4076 isl_int_set_si(bmap->ineq[j][pos], 1);
4077 isl_int_set_si(bmap->ineq[j][0], -value);
4078 bmap = isl_basic_map_simplify(bmap);
4079 return isl_basic_map_finalize(bmap);
4081 isl_basic_map_free(bmap);
4085 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4086 unsigned dim, isl_int value)
4090 bset = isl_basic_set_cow(bset);
4091 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4092 j = isl_basic_set_alloc_inequality(bset);
4095 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4096 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4097 isl_int_neg(bset->ineq[j][0], value);
4098 bset = isl_basic_set_simplify(bset);
4099 return isl_basic_set_finalize(bset);
4101 isl_basic_set_free(bset);
4105 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4106 enum isl_dim_type type, unsigned pos, int value)
4110 map = isl_map_cow(map);
4114 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4115 for (i = 0; i < map->n; ++i) {
4116 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4121 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4128 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4129 enum isl_dim_type type, unsigned pos, int value)
4131 return (struct isl_set *)
4132 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4135 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4140 set = isl_set_cow(set);
4144 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4145 for (i = 0; i < set->n; ++i) {
4146 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4156 struct isl_map *isl_map_reverse(struct isl_map *map)
4160 map = isl_map_cow(map);
4164 map->dim = isl_dim_reverse(map->dim);
4167 for (i = 0; i < map->n; ++i) {
4168 map->p[i] = isl_basic_map_reverse(map->p[i]);
4172 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4179 static struct isl_map *isl_basic_map_partial_lexopt(
4180 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4181 struct isl_set **empty, int max)
4185 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4186 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4188 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4190 isl_basic_map_free(bmap);
4191 isl_basic_set_free(dom);
4197 struct isl_map *isl_basic_map_partial_lexmax(
4198 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4199 struct isl_set **empty)
4201 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4204 struct isl_map *isl_basic_map_partial_lexmin(
4205 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4206 struct isl_set **empty)
4208 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4211 struct isl_set *isl_basic_set_partial_lexmin(
4212 struct isl_basic_set *bset, struct isl_basic_set *dom,
4213 struct isl_set **empty)
4215 return (struct isl_set *)
4216 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4220 struct isl_set *isl_basic_set_partial_lexmax(
4221 struct isl_basic_set *bset, struct isl_basic_set *dom,
4222 struct isl_set **empty)
4224 return (struct isl_set *)
4225 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4229 /* Given a basic map "bmap", compute the lexicograhically minimal
4230 * (or maximal) image element for each domain element in dom.
4231 * Set *empty to those elements in dom that do not have an image element.
4233 * We first make sure the basic sets in dom are disjoint and then
4234 * simply collect the results over each of the basic sets separately.
4235 * We could probably improve the efficiency a bit by moving the union
4236 * domain down into the parametric integer programming.
4238 static __isl_give isl_map *basic_map_partial_lexopt(
4239 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4240 __isl_give isl_set **empty, int max)
4243 struct isl_map *res;
4245 dom = isl_set_make_disjoint(dom);
4249 if (isl_set_fast_is_empty(dom)) {
4250 res = isl_map_empty_like_basic_map(bmap);
4251 *empty = isl_set_empty_like(dom);
4253 isl_basic_map_free(bmap);
4257 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4258 isl_basic_set_copy(dom->p[0]), empty, max);
4260 for (i = 1; i < dom->n; ++i) {
4261 struct isl_map *res_i;
4262 struct isl_set *empty_i;
4264 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4265 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4267 res = isl_map_union_disjoint(res, res_i);
4268 *empty = isl_set_union_disjoint(*empty, empty_i);
4272 isl_basic_map_free(bmap);
4277 isl_basic_map_free(bmap);
4281 /* Given a map "map", compute the lexicograhically minimal
4282 * (or maximal) image element for each domain element in dom.
4283 * Set *empty to those elements in dom that do not have an image element.
4285 * We first compute the lexicographically minimal or maximal element
4286 * in the first basic map. This results in a partial solution "res"
4287 * and a subset "todo" of dom that still need to be handled.
4288 * We then consider each of the remaining maps in "map" and successively
4289 * improve both "res" and "todo".
4291 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4292 * Assume we are computing the lexicographical maximum.
4293 * We first intersect basic map i with a relation that maps elements
4294 * to elements that are lexicographically larger than the image elements
4295 * in res^k and the compute the maximum image element of this intersection.
4296 * The result ("better") corresponds to those image elements in basic map i
4297 * that are better than what we had before. The remainder ("keep") are the
4298 * domain elements for which the image element in res_k was better.
4299 * We also compute the lexicographical maximum of basic map i in todo^k.
4300 * res^i is the result of the operation + better + those elements in
4301 * res^k that we should keep
4302 * todo^i is the remainder of the maximum operation on todo^k.
4304 static __isl_give isl_map *isl_map_partial_lexopt(
4305 __isl_take isl_map *map, __isl_take isl_set *dom,
4306 __isl_give isl_set **empty, int max)
4309 struct isl_map *res;
4310 struct isl_set *todo;
4315 if (isl_map_fast_is_empty(map)) {
4323 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4324 isl_set_copy(dom), &todo, max);
4326 for (i = 1; i < map->n; ++i) {
4328 struct isl_map *better;
4329 struct isl_set *keep;
4330 struct isl_map *res_i;
4331 struct isl_set *todo_i;
4332 struct isl_dim *dim = isl_map_get_dim(res);
4334 dim = isl_dim_range(dim);
4336 lt = isl_map_lex_lt(dim);
4338 lt = isl_map_lex_gt(dim);
4339 lt = isl_map_apply_range(isl_map_copy(res), lt);
4340 lt = isl_map_intersect(lt,
4341 isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
4342 better = isl_map_partial_lexopt(lt,
4343 isl_map_domain(isl_map_copy(res)),
4346 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4347 todo, &todo_i, max);
4349 res = isl_map_intersect_domain(res, keep);
4350 res = isl_map_union_disjoint(res, res_i);
4351 res = isl_map_union_disjoint(res, better);
4372 __isl_give isl_map *isl_map_partial_lexmax(
4373 __isl_take isl_map *map, __isl_take isl_set *dom,
4374 __isl_give isl_set **empty)
4376 return isl_map_partial_lexopt(map, dom, empty, 1);
4379 __isl_give isl_map *isl_map_partial_lexmin(
4380 __isl_take isl_map *map, __isl_take isl_set *dom,
4381 __isl_give isl_set **empty)
4383 return isl_map_partial_lexopt(map, dom, empty, 0);
4386 __isl_give isl_set *isl_set_partial_lexmin(
4387 __isl_take isl_set *set, __isl_take isl_set *dom,
4388 __isl_give isl_set **empty)
4390 return (struct isl_set *)
4391 isl_map_partial_lexmin((struct isl_map *)set,
4395 __isl_give isl_set *isl_set_partial_lexmax(
4396 __isl_take isl_set *set, __isl_take isl_set *dom,
4397 __isl_give isl_set **empty)
4399 return (struct isl_set *)
4400 isl_map_partial_lexmax((struct isl_map *)set,
4404 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4406 struct isl_basic_set *dom = NULL;
4407 struct isl_dim *dom_dim;
4411 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4412 dom = isl_basic_set_universe(dom_dim);
4413 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4415 isl_basic_map_free(bmap);
4419 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4421 return isl_basic_map_lexopt(bmap, 0);
4424 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4426 return isl_basic_map_lexopt(bmap, 1);
4429 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4431 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4434 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4436 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4439 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4441 struct isl_set *dom = NULL;
4442 struct isl_dim *dom_dim;
4446 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4447 dom = isl_set_universe(dom_dim);
4448 return isl_map_partial_lexopt(map, dom, NULL, max);
4454 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4456 return isl_map_lexopt(map, 0);
4459 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4461 return isl_map_lexopt(map, 1);
4464 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4466 return (isl_set *)isl_map_lexmin((isl_map *)set);
4469 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4471 return (isl_set *)isl_map_lexmax((isl_map *)set);
4474 /* Apply a preimage specified by "mat" on the parameters of "bset".
4475 * bset is assumed to have only parameters and divs.
4477 static struct isl_basic_set *basic_set_parameter_preimage(
4478 struct isl_basic_set *bset, struct isl_mat *mat)
4485 bset->dim = isl_dim_cow(bset->dim);
4489 nparam = isl_basic_set_dim(bset, isl_dim_param);
4491 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4493 bset->dim->nparam = 0;
4494 bset->dim->n_out = nparam;
4495 bset = isl_basic_set_preimage(bset, mat);
4497 bset->dim->nparam = bset->dim->n_out;
4498 bset->dim->n_out = 0;
4503 isl_basic_set_free(bset);
4507 /* Apply a preimage specified by "mat" on the parameters of "set".
4508 * set is assumed to have only parameters and divs.
4510 static struct isl_set *set_parameter_preimage(
4511 struct isl_set *set, struct isl_mat *mat)
4513 struct isl_dim *dim = NULL;
4519 dim = isl_dim_copy(set->dim);
4520 dim = isl_dim_cow(dim);
4524 nparam = isl_set_dim(set, isl_dim_param);
4526 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4529 dim->n_out = nparam;
4530 isl_set_reset_dim(set, dim);
4531 set = isl_set_preimage(set, mat);
4534 dim = isl_dim_copy(set->dim);
4535 dim = isl_dim_cow(dim);
4538 dim->nparam = dim->n_out;
4540 isl_set_reset_dim(set, dim);
4550 /* Intersect the basic set "bset" with the affine space specified by the
4551 * equalities in "eq".
4553 static struct isl_basic_set *basic_set_append_equalities(
4554 struct isl_basic_set *bset, struct isl_mat *eq)
4562 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4567 len = 1 + isl_dim_total(bset->dim) + bset->extra;
4568 for (i = 0; i < eq->n_row; ++i) {
4569 k = isl_basic_set_alloc_equality(bset);
4572 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4573 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4577 bset = isl_basic_set_gauss(bset, NULL);
4578 bset = isl_basic_set_finalize(bset);
4583 isl_basic_set_free(bset);
4587 /* Intersect the set "set" with the affine space specified by the
4588 * equalities in "eq".
4590 static struct isl_set *set_append_equalities(struct isl_set *set,
4598 for (i = 0; i < set->n; ++i) {
4599 set->p[i] = basic_set_append_equalities(set->p[i],
4612 /* Project the given basic set onto its parameter domain, possibly introducing
4613 * new, explicit, existential variables in the constraints.
4614 * The input has parameters and (possibly implicit) existential variables.
4615 * The output has the same parameters, but only
4616 * explicit existentially quantified variables.
4618 * The actual projection is performed by pip, but pip doesn't seem
4619 * to like equalities very much, so we first remove the equalities
4620 * among the parameters by performing a variable compression on
4621 * the parameters. Afterward, an inverse transformation is performed
4622 * and the equalities among the parameters are inserted back in.
4624 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4628 struct isl_mat *T, *T2;
4629 struct isl_set *set;
4630 unsigned nparam, n_div;
4632 bset = isl_basic_set_cow(bset);
4636 if (bset->n_eq == 0)
4637 return isl_basic_set_lexmin(bset);
4639 isl_basic_set_gauss(bset, NULL);
4641 nparam = isl_basic_set_dim(bset, isl_dim_param);
4642 n_div = isl_basic_set_dim(bset, isl_dim_div);
4644 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4645 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4648 if (i == bset->n_eq)
4649 return isl_basic_set_lexmin(bset);
4651 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4653 eq = isl_mat_cow(eq);
4654 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4655 if (T && T->n_col == 0) {
4659 bset = isl_basic_set_set_to_empty(bset);
4660 return isl_set_from_basic_set(bset);
4662 bset = basic_set_parameter_preimage(bset, T);
4664 set = isl_basic_set_lexmin(bset);
4665 set = set_parameter_preimage(set, T2);
4666 set = set_append_equalities(set, eq);
4670 /* Compute an explicit representation for all the existentially
4671 * quantified variables.
4672 * The input and output dimensions are first turned into parameters.
4673 * compute_divs then returns a map with the same parameters and
4674 * no input or output dimensions and the dimension specification
4675 * is reset to that of the input.
4677 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4679 struct isl_basic_set *bset;
4680 struct isl_set *set;
4681 struct isl_map *map;
4682 struct isl_dim *dim, *orig_dim = NULL;
4687 bmap = isl_basic_map_cow(bmap);
4691 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4692 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4693 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4694 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4698 orig_dim = bmap->dim;
4700 bset = (struct isl_basic_set *)bmap;
4702 set = parameter_compute_divs(bset);
4703 map = (struct isl_map *)set;
4704 map = isl_map_reset_dim(map, orig_dim);
4708 isl_basic_map_free(bmap);
4712 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4720 off = isl_dim_total(bmap->dim);
4721 for (i = 0; i < bmap->n_div; ++i) {
4722 if (isl_int_is_zero(bmap->div[i][0]))
4724 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
4730 static int map_divs_known(__isl_keep isl_map *map)
4737 for (i = 0; i < map->n; ++i) {
4738 int known = basic_map_divs_known(map->p[i]);
4746 /* If bmap contains any unknown divs, then compute explicit
4747 * expressions for them. However, this computation may be
4748 * quite expensive, so first try to remove divs that aren't
4751 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
4755 struct isl_map *map;
4757 known = basic_map_divs_known(bmap);
4761 return isl_map_from_basic_map(bmap);
4763 bmap = isl_basic_map_drop_redundant_divs(bmap);
4765 known = basic_map_divs_known(bmap);
4769 return isl_map_from_basic_map(bmap);
4771 map = compute_divs(bmap);
4774 isl_basic_map_free(bmap);
4778 struct isl_map *isl_map_compute_divs(struct isl_map *map)
4782 struct isl_map *res;
4789 known = map_divs_known(map);
4797 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
4798 for (i = 1 ; i < map->n; ++i) {
4800 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
4801 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4802 res = isl_map_union_disjoint(res, r2);
4804 res = isl_map_union(res, r2);
4811 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
4813 return (struct isl_set *)
4814 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
4817 struct isl_set *isl_set_compute_divs(struct isl_set *set)
4819 return (struct isl_set *)
4820 isl_map_compute_divs((struct isl_map *)set);
4823 struct isl_set *isl_map_domain(struct isl_map *map)
4826 struct isl_set *set;
4831 map = isl_map_cow(map);
4835 set = (struct isl_set *)map;
4836 set->dim = isl_dim_domain(set->dim);
4839 for (i = 0; i < map->n; ++i) {
4840 set->p[i] = isl_basic_map_domain(map->p[i]);
4844 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4845 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4852 struct isl_map *isl_map_union_disjoint(
4853 struct isl_map *map1, struct isl_map *map2)
4857 struct isl_map *map = NULL;
4871 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4873 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4874 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4875 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4877 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
4878 map1->n + map2->n, flags);
4881 for (i = 0; i < map1->n; ++i) {
4882 map = isl_map_add_basic_map(map,
4883 isl_basic_map_copy(map1->p[i]));
4887 for (i = 0; i < map2->n; ++i) {
4888 map = isl_map_add_basic_map(map,
4889 isl_basic_map_copy(map2->p[i]));
4903 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
4905 map1 = isl_map_union_disjoint(map1, map2);
4909 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
4913 struct isl_set *isl_set_union_disjoint(
4914 struct isl_set *set1, struct isl_set *set2)
4916 return (struct isl_set *)
4917 isl_map_union_disjoint(
4918 (struct isl_map *)set1, (struct isl_map *)set2);
4921 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
4923 return (struct isl_set *)
4924 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
4927 struct isl_map *isl_map_intersect_range(
4928 struct isl_map *map, struct isl_set *set)
4931 struct isl_map *result;
4937 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
4938 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
4939 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4941 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
4942 map->n * set->n, flags);
4945 for (i = 0; i < map->n; ++i)
4946 for (j = 0; j < set->n; ++j) {
4947 result = isl_map_add_basic_map(result,
4948 isl_basic_map_intersect_range(
4949 isl_basic_map_copy(map->p[i]),
4950 isl_basic_set_copy(set->p[j])));
4963 struct isl_map *isl_map_intersect_domain(
4964 struct isl_map *map, struct isl_set *set)
4966 return isl_map_reverse(
4967 isl_map_intersect_range(isl_map_reverse(map), set));
4970 struct isl_map *isl_map_apply_domain(
4971 struct isl_map *map1, struct isl_map *map2)
4975 map1 = isl_map_reverse(map1);
4976 map1 = isl_map_apply_range(map1, map2);
4977 return isl_map_reverse(map1);
4984 struct isl_map *isl_map_apply_range(
4985 struct isl_map *map1, struct isl_map *map2)
4987 struct isl_dim *dim_result;
4988 struct isl_map *result;
4994 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
4995 isl_dim_copy(map2->dim));
4997 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5000 for (i = 0; i < map1->n; ++i)
5001 for (j = 0; j < map2->n; ++j) {
5002 result = isl_map_add_basic_map(result,
5003 isl_basic_map_apply_range(
5004 isl_basic_map_copy(map1->p[i]),
5005 isl_basic_map_copy(map2->p[j])));
5011 if (result && result->n <= 1)
5012 ISL_F_SET(result, ISL_MAP_DISJOINT);
5021 * returns range - domain
5023 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5025 isl_dim *dims, *target_dim;
5026 struct isl_basic_set *bset;
5033 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5034 bmap->dim, isl_dim_out),
5036 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5037 dim = isl_basic_map_n_in(bmap);
5038 nparam = isl_basic_map_n_param(bmap);
5039 bset = isl_basic_set_from_basic_map(bmap);
5040 bset = isl_basic_set_cow(bset);
5041 dims = isl_basic_set_get_dim(bset);
5042 dims = isl_dim_add(dims, isl_dim_set, dim);
5043 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5044 bset = isl_basic_set_swap_vars(bset, 2*dim);
5045 for (i = 0; i < dim; ++i) {
5046 int j = isl_basic_map_alloc_equality(
5047 (struct isl_basic_map *)bset);
5050 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5051 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5052 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5053 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5055 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5056 bset = isl_basic_set_reset_dim(bset, target_dim);
5059 isl_basic_map_free(bmap);
5064 * returns range - domain
5066 struct isl_set *isl_map_deltas(struct isl_map *map)
5070 struct isl_set *result;
5075 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5076 map->dim, isl_dim_out),
5078 dim = isl_map_get_dim(map);
5079 dim = isl_dim_domain(dim);
5080 result = isl_set_alloc_dim(dim, map->n, map->flags);
5083 for (i = 0; i < map->n; ++i)
5084 result = isl_set_add_basic_set(result,
5085 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5093 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5095 struct isl_basic_map *bmap;
5103 nparam = dims->nparam;
5105 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5109 for (i = 0; i < dim; ++i) {
5110 int j = isl_basic_map_alloc_equality(bmap);
5113 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5114 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5115 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5117 return isl_basic_map_finalize(bmap);
5119 isl_basic_map_free(bmap);
5123 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
5125 struct isl_dim *dim = isl_dim_map(set_dim);
5128 return basic_map_identity(dim);
5131 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5133 if (!model || !model->dim)
5135 isl_assert(model->ctx,
5136 model->dim->n_in == model->dim->n_out, return NULL);
5137 return basic_map_identity(isl_dim_copy(model->dim));
5140 static struct isl_map *map_identity(struct isl_dim *dim)
5142 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
5143 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
5146 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
5148 struct isl_dim *dim = isl_dim_map(set_dim);
5151 return map_identity(dim);
5154 struct isl_map *isl_map_identity_like(struct isl_map *model)
5156 if (!model || !model->dim)
5158 isl_assert(model->ctx,
5159 model->dim->n_in == model->dim->n_out, return NULL);
5160 return map_identity(isl_dim_copy(model->dim));
5163 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5165 if (!model || !model->dim)
5167 isl_assert(model->ctx,
5168 model->dim->n_in == model->dim->n_out, return NULL);
5169 return map_identity(isl_dim_copy(model->dim));
5172 /* Construct a basic set with all set dimensions having only non-negative
5175 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5180 struct isl_basic_set *bset;
5184 nparam = dims->nparam;
5186 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5189 for (i = 0; i < dim; ++i) {
5190 int k = isl_basic_set_alloc_inequality(bset);
5193 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5194 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5198 isl_basic_set_free(bset);
5202 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5203 enum isl_dim_type type, unsigned first, unsigned n)
5206 isl_basic_set *nonneg = NULL;
5207 isl_basic_set *neg = NULL;
5214 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5216 for (i = 0; i < n; ++i) {
5220 nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5221 k = isl_basic_set_alloc_inequality(nonneg);
5224 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5225 isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
5227 neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5228 k = isl_basic_set_alloc_inequality(neg);
5231 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5232 isl_int_set_si(neg->ineq[k][0], -1);
5233 isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
5235 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5240 isl_basic_set_free(nonneg);
5241 isl_basic_set_free(neg);
5246 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5248 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5251 int isl_basic_map_is_subset(
5252 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5255 struct isl_map *map1;
5256 struct isl_map *map2;
5258 if (!bmap1 || !bmap2)
5261 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5262 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5264 is_subset = isl_map_is_subset(map1, map2);
5272 int isl_basic_map_is_equal(
5273 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5277 if (!bmap1 || !bmap2)
5279 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5282 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5286 int isl_basic_set_is_equal(
5287 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5289 return isl_basic_map_is_equal(
5290 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5293 int isl_map_is_empty(struct isl_map *map)
5300 for (i = 0; i < map->n; ++i) {
5301 is_empty = isl_basic_map_is_empty(map->p[i]);
5310 int isl_map_fast_is_empty(struct isl_map *map)
5312 return map ? map->n == 0 : -1;
5315 int isl_set_fast_is_empty(struct isl_set *set)
5317 return set ? set->n == 0 : -1;
5320 int isl_set_is_empty(struct isl_set *set)
5322 return isl_map_is_empty((struct isl_map *)set);
5325 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
5330 return isl_dim_equal(map1->dim, map2->dim);
5333 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5338 return isl_dim_equal(set1->dim, set2->dim);
5341 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
5347 is_subset = isl_map_is_subset(map1, map2);
5350 is_subset = isl_map_is_subset(map2, map1);
5354 int isl_basic_map_is_strict_subset(
5355 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5359 if (!bmap1 || !bmap2)
5361 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5364 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5365 if (is_subset == -1)
5370 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
5376 is_subset = isl_map_is_subset(map1, map2);
5379 is_subset = isl_map_is_subset(map2, map1);
5380 if (is_subset == -1)
5385 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5387 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5390 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5394 return bmap->n_eq == 0 && bmap->n_ineq == 0;
5397 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5401 return bset->n_eq == 0 && bset->n_ineq == 0;
5404 int isl_map_fast_is_universe(__isl_keep isl_map *map)
5411 for (i = 0; i < map->n; ++i) {
5412 int r = isl_basic_map_is_universe(map->p[i]);
5420 int isl_set_fast_is_universe(__isl_keep isl_set *set)
5422 return isl_map_fast_is_universe((isl_map *) set);
5425 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
5427 struct isl_basic_set *bset = NULL;
5428 struct isl_vec *sample = NULL;
5435 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
5438 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
5439 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
5440 copy = isl_basic_map_remove_redundancies(copy);
5441 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
5442 isl_basic_map_free(copy);
5446 total = 1 + isl_basic_map_total_dim(bmap);
5447 if (bmap->sample && bmap->sample->size == total) {
5448 int contains = isl_basic_map_contains(bmap, bmap->sample);
5454 isl_vec_free(bmap->sample);
5455 bmap->sample = NULL;
5456 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
5459 sample = isl_basic_set_sample_vec(bset);
5462 empty = sample->size == 0;
5463 isl_vec_free(bmap->sample);
5464 bmap->sample = sample;
5466 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
5471 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
5475 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
5478 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
5482 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
5485 int isl_basic_set_is_empty(struct isl_basic_set *bset)
5487 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
5490 struct isl_map *isl_basic_map_union(
5491 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5493 struct isl_map *map;
5494 if (!bmap1 || !bmap2)
5497 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
5499 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
5502 map = isl_map_add_basic_map(map, bmap1);
5503 map = isl_map_add_basic_map(map, bmap2);
5506 isl_basic_map_free(bmap1);
5507 isl_basic_map_free(bmap2);
5511 struct isl_set *isl_basic_set_union(
5512 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5514 return (struct isl_set *)isl_basic_map_union(
5515 (struct isl_basic_map *)bset1,
5516 (struct isl_basic_map *)bset2);
5519 /* Order divs such that any div only depends on previous divs */
5520 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
5528 off = isl_dim_total(bmap->dim);
5530 for (i = 0; i < bmap->n_div; ++i) {
5532 if (isl_int_is_zero(bmap->div[i][0]))
5534 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
5538 isl_basic_map_swap_div(bmap, i, i + pos);
5544 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
5546 return (struct isl_basic_set *)
5547 isl_basic_map_order_divs((struct isl_basic_map *)bset);
5550 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
5557 for (i = 0; i < map->n; ++i) {
5558 map->p[i] = isl_basic_map_order_divs(map->p[i]);
5569 /* Look for a div in dst that corresponds to the div "div" in src.
5570 * The divs before "div" in src and dst are assumed to be the same.
5572 * Returns -1 if no corresponding div was found and the position
5573 * of the corresponding div in dst otherwise.
5575 static int find_div(struct isl_basic_map *dst,
5576 struct isl_basic_map *src, unsigned div)
5580 unsigned total = isl_dim_total(src->dim);
5582 isl_assert(dst->ctx, div <= dst->n_div, return -1);
5583 for (i = div; i < dst->n_div; ++i)
5584 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
5585 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
5586 dst->n_div - div) == -1)
5591 struct isl_basic_map *isl_basic_map_align_divs(
5592 struct isl_basic_map *dst, struct isl_basic_map *src)
5595 unsigned total = isl_dim_total(src->dim);
5600 if (src->n_div == 0)
5603 for (i = 0; i < src->n_div; ++i)
5604 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
5606 src = isl_basic_map_order_divs(src);
5607 dst = isl_basic_map_cow(dst);
5608 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
5609 src->n_div, 0, 2 * src->n_div);
5612 for (i = 0; i < src->n_div; ++i) {
5613 int j = find_div(dst, src, i);
5615 j = isl_basic_map_alloc_div(dst);
5618 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
5619 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
5620 if (isl_basic_map_add_div_constraints(dst, j) < 0)
5624 isl_basic_map_swap_div(dst, i, j);
5628 isl_basic_map_free(dst);
5632 struct isl_basic_set *isl_basic_set_align_divs(
5633 struct isl_basic_set *dst, struct isl_basic_set *src)
5635 return (struct isl_basic_set *)isl_basic_map_align_divs(
5636 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
5639 struct isl_map *isl_map_align_divs(struct isl_map *map)
5647 map = isl_map_compute_divs(map);
5648 map = isl_map_cow(map);
5652 for (i = 1; i < map->n; ++i)
5653 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5654 for (i = 1; i < map->n; ++i)
5655 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5657 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5661 struct isl_set *isl_set_align_divs(struct isl_set *set)
5663 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5666 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5670 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5671 map = isl_map_intersect_domain(map, set);
5672 set = isl_map_range(map);
5680 /* There is no need to cow as removing empty parts doesn't change
5681 * the meaning of the set.
5683 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5690 for (i = map->n-1; i >= 0; --i) {
5691 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5693 isl_basic_map_free(map->p[i]);
5694 if (i != map->n-1) {
5695 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5696 map->p[i] = map->p[map->n-1];
5704 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
5706 return (struct isl_set *)
5707 isl_map_remove_empty_parts((struct isl_map *)set);
5710 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
5712 struct isl_basic_map *bmap;
5713 if (!map || map->n == 0)
5715 bmap = map->p[map->n-1];
5716 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
5717 return isl_basic_map_copy(bmap);
5720 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
5722 return (struct isl_basic_set *)
5723 isl_map_copy_basic_map((struct isl_map *)set);
5726 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
5727 __isl_keep isl_basic_map *bmap)
5733 for (i = map->n-1; i >= 0; --i) {
5734 if (map->p[i] != bmap)
5736 map = isl_map_cow(map);
5739 isl_basic_map_free(map->p[i]);
5740 if (i != map->n-1) {
5741 ISL_F_CLR(map, ISL_SET_NORMALIZED);
5742 map->p[i] = map->p[map->n-1];
5753 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
5754 struct isl_basic_set *bset)
5756 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
5757 (struct isl_basic_map *)bset);
5760 /* Given two basic sets bset1 and bset2, compute the maximal difference
5761 * between the values of dimension pos in bset1 and those in bset2
5762 * for any common value of the parameters and dimensions preceding pos.
5764 static enum isl_lp_result basic_set_maximal_difference_at(
5765 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
5766 int pos, isl_int *opt)
5768 struct isl_dim *dims;
5769 struct isl_basic_map *bmap1 = NULL;
5770 struct isl_basic_map *bmap2 = NULL;
5771 struct isl_ctx *ctx;
5772 struct isl_vec *obj;
5775 unsigned dim1, dim2;
5776 enum isl_lp_result res;
5778 if (!bset1 || !bset2)
5779 return isl_lp_error;
5781 nparam = isl_basic_set_n_param(bset1);
5782 dim1 = isl_basic_set_n_dim(bset1);
5783 dim2 = isl_basic_set_n_dim(bset2);
5784 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
5785 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
5786 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
5787 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
5788 if (!bmap1 || !bmap2)
5790 bmap1 = isl_basic_map_cow(bmap1);
5791 bmap1 = isl_basic_map_extend(bmap1, nparam,
5792 pos, (dim1 - pos) + (dim2 - pos),
5793 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
5794 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
5797 total = isl_basic_map_total_dim(bmap1);
5799 obj = isl_vec_alloc(ctx, 1 + total);
5800 isl_seq_clr(obj->block.data, 1 + total);
5801 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
5802 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
5805 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
5807 isl_basic_map_free(bmap1);
5811 isl_basic_map_free(bmap1);
5812 isl_basic_map_free(bmap2);
5813 return isl_lp_error;
5816 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5817 * for any common value of the parameters and dimensions preceding pos
5818 * in both basic sets, the values of dimension pos in bset1 are
5819 * smaller or larger than those in bset2.
5822 * 1 if bset1 follows bset2
5823 * -1 if bset1 precedes bset2
5824 * 0 if bset1 and bset2 are incomparable
5825 * -2 if some error occurred.
5827 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
5828 struct isl_basic_set *bset2, int pos)
5831 enum isl_lp_result res;
5836 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5838 if (res == isl_lp_empty)
5840 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5841 res == isl_lp_unbounded)
5843 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5852 /* Given two basic sets bset1 and bset2, check whether
5853 * for any common value of the parameters and dimensions preceding pos
5854 * there is a value of dimension pos in bset1 that is larger
5855 * than a value of the same dimension in bset2.
5858 * 1 if there exists such a pair
5859 * 0 if there is no such pair, but there is a pair of equal values
5861 * -2 if some error occurred.
5863 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
5864 __isl_keep isl_basic_set *bset2, int pos)
5867 enum isl_lp_result res;
5872 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5874 if (res == isl_lp_empty)
5876 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5877 res == isl_lp_unbounded)
5879 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5881 else if (res == isl_lp_ok)
5890 /* Given two sets set1 and set2, check whether
5891 * for any common value of the parameters and dimensions preceding pos
5892 * there is a value of dimension pos in set1 that is larger
5893 * than a value of the same dimension in set2.
5896 * 1 if there exists such a pair
5897 * 0 if there is no such pair, but there is a pair of equal values
5899 * -2 if some error occurred.
5901 int isl_set_follows_at(__isl_keep isl_set *set1,
5902 __isl_keep isl_set *set2, int pos)
5910 for (i = 0; i < set1->n; ++i)
5911 for (j = 0; j < set2->n; ++j) {
5913 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
5914 if (f == 1 || f == -2)
5923 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
5924 unsigned pos, isl_int *val)
5932 total = isl_basic_map_total_dim(bmap);
5933 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5934 for (; d+1 > pos; --d)
5935 if (!isl_int_is_zero(bmap->eq[i][1+d]))
5939 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5941 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5943 if (!isl_int_is_one(bmap->eq[i][1+d]))
5946 isl_int_neg(*val, bmap->eq[i][0]);
5952 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5953 unsigned pos, isl_int *val)
5965 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
5968 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
5969 for (i = 1; fixed == 1 && i < map->n; ++i) {
5970 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
5971 if (fixed == 1 && isl_int_ne(tmp, v))
5975 isl_int_set(*val, v);
5981 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5982 unsigned pos, isl_int *val)
5984 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
5988 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
5991 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
5994 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
5995 enum isl_dim_type type, unsigned pos, isl_int *val)
5997 if (pos >= isl_basic_map_dim(bmap, type))
5999 return isl_basic_map_fast_has_fixed_var(bmap,
6000 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6003 int isl_map_fast_is_fixed(struct isl_map *map,
6004 enum isl_dim_type type, unsigned pos, isl_int *val)
6006 if (pos >= isl_map_dim(map, type))
6008 return isl_map_fast_has_fixed_var(map,
6009 map_offset(map, type) - 1 + pos, val);
6012 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6013 * then return this fixed value in *val.
6015 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6018 return isl_basic_set_fast_has_fixed_var(bset,
6019 isl_basic_set_n_param(bset) + dim, val);
6022 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6023 * then return this fixed value in *val.
6025 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6027 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6030 /* Check if input variable in has fixed value and if so and if val is not NULL,
6031 * then return this fixed value in *val.
6033 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6035 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6038 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6039 * and if val is not NULL, then return this lower bound in *val.
6041 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6042 unsigned dim, isl_int *val)
6044 int i, i_eq = -1, i_ineq = -1;
6051 total = isl_basic_set_total_dim(bset);
6052 nparam = isl_basic_set_n_param(bset);
6053 for (i = 0; i < bset->n_eq; ++i) {
6054 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6060 for (i = 0; i < bset->n_ineq; ++i) {
6061 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6063 if (i_eq != -1 || i_ineq != -1)
6067 if (i_eq == -1 && i_ineq == -1)
6069 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6070 /* The coefficient should always be one due to normalization. */
6071 if (!isl_int_is_one(c[1+nparam+dim]))
6073 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6075 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6076 total - nparam - dim - 1) != -1)
6079 isl_int_neg(*val, c[0]);
6083 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6084 unsigned dim, isl_int *val)
6096 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6100 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6102 for (i = 1; fixed == 1 && i < set->n; ++i) {
6103 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6105 if (fixed == 1 && isl_int_ne(tmp, v))
6109 isl_int_set(*val, v);
6120 static int qsort_constraint_cmp(const void *p1, const void *p2)
6122 const struct constraint *c1 = (const struct constraint *)p1;
6123 const struct constraint *c2 = (const struct constraint *)p2;
6124 unsigned size = isl_min(c1->size, c2->size);
6125 return isl_seq_cmp(c1->c, c2->c, size);
6128 static struct isl_basic_map *isl_basic_map_sort_constraints(
6129 struct isl_basic_map *bmap)
6132 struct constraint *c;
6137 total = isl_basic_map_total_dim(bmap);
6138 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6141 for (i = 0; i < bmap->n_ineq; ++i) {
6143 c[i].c = bmap->ineq[i];
6145 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6146 for (i = 0; i < bmap->n_ineq; ++i)
6147 bmap->ineq[i] = c[i].c;
6151 isl_basic_map_free(bmap);
6155 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6156 __isl_take isl_basic_set *bset)
6158 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6159 (struct isl_basic_map *)bset);
6162 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6166 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6168 bmap = isl_basic_map_remove_redundancies(bmap);
6169 bmap = isl_basic_map_sort_constraints(bmap);
6170 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6174 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6176 return (struct isl_basic_set *)isl_basic_map_normalize(
6177 (struct isl_basic_map *)bset);
6180 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6181 const __isl_keep isl_basic_map *bmap2)
6188 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6189 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6190 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6191 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6192 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6193 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6194 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6195 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6197 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6199 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6201 if (bmap1->n_eq != bmap2->n_eq)
6202 return bmap1->n_eq - bmap2->n_eq;
6203 if (bmap1->n_ineq != bmap2->n_ineq)
6204 return bmap1->n_ineq - bmap2->n_ineq;
6205 if (bmap1->n_div != bmap2->n_div)
6206 return bmap1->n_div - bmap2->n_div;
6207 total = isl_basic_map_total_dim(bmap1);
6208 for (i = 0; i < bmap1->n_eq; ++i) {
6209 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6213 for (i = 0; i < bmap1->n_ineq; ++i) {
6214 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6218 for (i = 0; i < bmap1->n_div; ++i) {
6219 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6226 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6227 __isl_keep isl_basic_map *bmap2)
6229 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6232 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6233 __isl_keep isl_basic_set *bset2)
6235 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6236 (isl_basic_map *)bset2);
6239 static int qsort_bmap_cmp(const void *p1, const void *p2)
6241 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6242 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6244 return isl_basic_map_fast_cmp(bmap1, bmap2);
6247 /* We normalize in place, but if anything goes wrong we need
6248 * to return NULL, so we need to make sure we don't change the
6249 * meaning of any possible other copies of map.
6251 struct isl_map *isl_map_normalize(struct isl_map *map)
6254 struct isl_basic_map *bmap;
6258 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6260 for (i = 0; i < map->n; ++i) {
6261 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6264 isl_basic_map_free(map->p[i]);
6267 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6268 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6269 map = isl_map_remove_empty_parts(map);
6272 for (i = map->n - 1; i >= 1; --i) {
6273 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6275 isl_basic_map_free(map->p[i-1]);
6276 for (j = i; j < map->n; ++j)
6277 map->p[j-1] = map->p[j];
6287 struct isl_set *isl_set_normalize(struct isl_set *set)
6289 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
6292 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
6302 if (!isl_dim_equal(map1->dim, map2->dim))
6305 map1 = isl_map_copy(map1);
6306 map2 = isl_map_copy(map2);
6307 map1 = isl_map_normalize(map1);
6308 map2 = isl_map_normalize(map2);
6311 equal = map1->n == map2->n;
6312 for (i = 0; equal && i < map1->n; ++i) {
6313 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
6326 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
6328 return isl_map_fast_is_equal((struct isl_map *)set1,
6329 (struct isl_map *)set2);
6332 /* Return an interval that ranges from min to max (inclusive)
6334 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
6335 isl_int min, isl_int max)
6338 struct isl_basic_set *bset = NULL;
6340 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
6344 k = isl_basic_set_alloc_inequality(bset);
6347 isl_int_set_si(bset->ineq[k][1], 1);
6348 isl_int_neg(bset->ineq[k][0], min);
6350 k = isl_basic_set_alloc_inequality(bset);
6353 isl_int_set_si(bset->ineq[k][1], -1);
6354 isl_int_set(bset->ineq[k][0], max);
6358 isl_basic_set_free(bset);
6362 /* Return the Cartesian product of the basic sets in list (in the given order).
6364 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
6372 struct isl_basic_set *product = NULL;
6376 isl_assert(list->ctx, list->n > 0, goto error);
6377 isl_assert(list->ctx, list->p[0], goto error);
6378 nparam = isl_basic_set_n_param(list->p[0]);
6379 dim = isl_basic_set_n_dim(list->p[0]);
6380 extra = list->p[0]->n_div;
6381 n_eq = list->p[0]->n_eq;
6382 n_ineq = list->p[0]->n_ineq;
6383 for (i = 1; i < list->n; ++i) {
6384 isl_assert(list->ctx, list->p[i], goto error);
6385 isl_assert(list->ctx,
6386 nparam == isl_basic_set_n_param(list->p[i]), goto error);
6387 dim += isl_basic_set_n_dim(list->p[i]);
6388 extra += list->p[i]->n_div;
6389 n_eq += list->p[i]->n_eq;
6390 n_ineq += list->p[i]->n_ineq;
6392 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
6397 for (i = 0; i < list->n; ++i) {
6398 isl_basic_set_add_constraints(product,
6399 isl_basic_set_copy(list->p[i]), dim);
6400 dim += isl_basic_set_n_dim(list->p[i]);
6402 isl_basic_set_list_free(list);
6405 isl_basic_set_free(product);
6406 isl_basic_set_list_free(list);
6410 struct isl_basic_map *isl_basic_map_product(
6411 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6413 struct isl_dim *dim_result = NULL;
6414 struct isl_basic_map *bmap;
6415 unsigned in1, in2, out1, out2, nparam, total, pos;
6416 struct isl_dim_map *dim_map1, *dim_map2;
6418 if (!bmap1 || !bmap2)
6421 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
6422 bmap2->dim, isl_dim_param), goto error);
6423 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
6424 isl_dim_copy(bmap2->dim));
6426 in1 = isl_basic_map_n_in(bmap1);
6427 in2 = isl_basic_map_n_in(bmap2);
6428 out1 = isl_basic_map_n_out(bmap1);
6429 out2 = isl_basic_map_n_out(bmap2);
6430 nparam = isl_basic_map_n_param(bmap1);
6432 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
6433 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
6434 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
6435 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
6436 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
6437 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
6438 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
6439 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
6440 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
6441 isl_dim_map_div(dim_map1, bmap1, pos += out2);
6442 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
6444 bmap = isl_basic_map_alloc_dim(dim_result,
6445 bmap1->n_div + bmap2->n_div,
6446 bmap1->n_eq + bmap2->n_eq,
6447 bmap1->n_ineq + bmap2->n_ineq);
6448 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
6449 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
6450 bmap = isl_basic_map_simplify(bmap);
6451 return isl_basic_map_finalize(bmap);
6453 isl_basic_map_free(bmap1);
6454 isl_basic_map_free(bmap2);
6458 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6460 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
6463 struct isl_map *result;
6469 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
6470 map2->dim, isl_dim_param), goto error);
6472 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6473 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6474 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6476 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
6477 isl_dim_copy(map2->dim)),
6478 map1->n * map2->n, flags);
6481 for (i = 0; i < map1->n; ++i)
6482 for (j = 0; j < map2->n; ++j) {
6483 struct isl_basic_map *part;
6484 part = isl_basic_map_product(
6485 isl_basic_map_copy(map1->p[i]),
6486 isl_basic_map_copy(map2->p[j]));
6487 if (isl_basic_map_is_empty(part))
6488 isl_basic_map_free(part);
6490 result = isl_map_add_basic_map(result, part);
6503 /* Given two set A and B, construct its Cartesian product A x B.
6505 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
6507 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
6508 (struct isl_map *)set2);
6511 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
6514 uint32_t hash = isl_hash_init();
6519 bset = isl_basic_set_copy(bset);
6520 bset = isl_basic_set_normalize(bset);
6523 total = isl_basic_set_total_dim(bset);
6524 isl_hash_byte(hash, bset->n_eq & 0xFF);
6525 for (i = 0; i < bset->n_eq; ++i) {
6527 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
6528 isl_hash_hash(hash, c_hash);
6530 isl_hash_byte(hash, bset->n_ineq & 0xFF);
6531 for (i = 0; i < bset->n_ineq; ++i) {
6533 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
6534 isl_hash_hash(hash, c_hash);
6536 isl_hash_byte(hash, bset->n_div & 0xFF);
6537 for (i = 0; i < bset->n_div; ++i) {
6539 if (isl_int_is_zero(bset->div[i][0]))
6541 isl_hash_byte(hash, i & 0xFF);
6542 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
6543 isl_hash_hash(hash, c_hash);
6545 isl_basic_set_free(bset);
6549 uint32_t isl_set_get_hash(struct isl_set *set)
6556 set = isl_set_copy(set);
6557 set = isl_set_normalize(set);
6561 hash = isl_hash_init();
6562 for (i = 0; i < set->n; ++i) {
6564 bset_hash = isl_basic_set_get_hash(set->p[i]);
6565 isl_hash_hash(hash, bset_hash);
6573 /* Check if the value for dimension dim is completely determined
6574 * by the values of the other parameters and variables.
6575 * That is, check if dimension dim is involved in an equality.
6577 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
6584 nparam = isl_basic_set_n_param(bset);
6585 for (i = 0; i < bset->n_eq; ++i)
6586 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
6591 /* Check if the value for dimension dim is completely determined
6592 * by the values of the other parameters and variables.
6593 * That is, check if dimension dim is involved in an equality
6594 * for each of the subsets.
6596 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
6602 for (i = 0; i < set->n; ++i) {
6604 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
6611 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
6612 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
6619 for (i = 0; i < map->n; ++i)
6620 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
6626 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
6627 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
6634 for (i = 0; i < set->n; ++i)
6635 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
6641 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
6643 struct isl_dim *dim;
6648 if (bset->n_div == 0)
6651 bset = isl_basic_set_cow(bset);
6655 dim = isl_basic_set_get_dim(bset);
6656 dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
6659 isl_dim_free(bset->dim);
6663 bset = isl_basic_set_finalize(bset);
6667 isl_basic_set_free(bset);
6671 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
6674 struct isl_dim *dim;
6677 set = isl_set_align_divs(set);
6681 if (set->n == 0 || set->p[0]->n_div == 0)
6684 set = isl_set_cow(set);
6688 n_div = set->p[0]->n_div;
6689 dim = isl_set_get_dim(set);
6690 dim = isl_dim_add(dim, isl_dim_set, n_div);
6693 isl_dim_free(set->dim);
6696 for (i = 0; i < set->n; ++i) {
6697 set->p[i] = isl_basic_set_lift(set->p[i]);
6708 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
6710 struct isl_dim *dim;
6711 struct isl_basic_map *bmap;
6718 set = isl_set_align_divs(set);
6723 dim = isl_set_get_dim(set);
6724 if (set->n == 0 || set->p[0]->n_div == 0) {
6726 return isl_map_identity(dim);
6729 n_div = set->p[0]->n_div;
6730 dim = isl_dim_map(dim);
6731 n_param = isl_dim_size(dim, isl_dim_param);
6732 n_set = isl_dim_size(dim, isl_dim_in);
6733 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
6734 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
6735 for (i = 0; i < n_set; ++i)
6736 bmap = var_equal(bmap, i);
6738 total = n_param + n_set + n_set + n_div;
6739 for (i = 0; i < n_div; ++i) {
6740 k = isl_basic_map_alloc_inequality(bmap);
6743 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
6744 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
6745 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
6746 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
6747 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
6748 set->p[0]->div[i][0]);
6750 l = isl_basic_map_alloc_inequality(bmap);
6753 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
6754 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
6755 set->p[0]->div[i][0]);
6756 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
6760 return isl_map_from_basic_map(bmap);
6763 isl_basic_map_free(bmap);
6767 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
6775 dim = isl_basic_set_total_dim(bset);
6776 size += bset->n_eq * (1 + dim);
6777 size += bset->n_ineq * (1 + dim);
6778 size += bset->n_div * (2 + dim);
6783 int isl_set_size(__isl_keep isl_set *set)
6791 for (i = 0; i < set->n; ++i)
6792 size += isl_basic_set_size(set->p[i]);
6797 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
6798 enum isl_dim_type type, unsigned pos)
6806 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
6808 pos += isl_basic_map_offset(bmap, type);
6810 for (i = 0; i < bmap->n_eq; ++i)
6811 if (!isl_int_is_zero(bmap->eq[i][pos]))
6815 for (i = 0; i < bmap->n_ineq; ++i) {
6816 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
6823 return lower && upper;
6826 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
6827 enum isl_dim_type type, unsigned pos)
6834 for (i = 0; i < map->n; ++i) {
6836 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
6837 if (bounded < 0 || !bounded)
6844 /* Return 1 if the specified dim is involved in both an upper bound
6845 * and a lower bound.
6847 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
6848 enum isl_dim_type type, unsigned pos)
6850 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
6853 /* For each of the "n" variables starting at "first", determine
6854 * the sign of the variable and put the results in the first "n"
6855 * elements of the array "signs".
6857 * 1 means that the variable is non-negative
6858 * -1 means that the variable is non-positive
6859 * 0 means the variable attains both positive and negative values.
6861 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
6862 unsigned first, unsigned n, int *signs)
6864 isl_vec *bound = NULL;
6865 struct isl_tab *tab = NULL;
6866 struct isl_tab_undo *snap;
6869 if (!bset || !signs)
6872 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
6873 tab = isl_tab_from_basic_set(bset);
6877 isl_seq_clr(bound->el, bound->size);
6878 isl_int_set_si(bound->el[0], -1);
6880 snap = isl_tab_snap(tab);
6881 for (i = 0; i < n; ++i) {
6884 isl_int_set_si(bound->el[1 + first + i], -1);
6885 if (isl_tab_add_ineq(tab, bound->el) < 0)
6888 isl_int_set_si(bound->el[1 + first + i], 0);
6889 if (isl_tab_rollback(tab, snap) < 0)
6897 isl_int_set_si(bound->el[1 + first + i], 1);
6898 if (isl_tab_add_ineq(tab, bound->el) < 0)
6901 isl_int_set_si(bound->el[1 + first + i], 0);
6902 if (isl_tab_rollback(tab, snap) < 0)
6905 signs[i] = empty ? -1 : 0;
6909 isl_vec_free(bound);
6913 isl_vec_free(bound);
6917 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
6918 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
6920 if (!bset || !signs)
6922 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
6925 first += pos(bset->dim, type) - 1;
6926 return isl_basic_set_vars_get_sign(bset, first, n, signs);
6929 /* Check if the given map is single-valued.
6934 * and check if the result is a subset of the identity mapping.
6936 int isl_map_is_single_valued(__isl_keep isl_map *map)
6942 test = isl_map_reverse(isl_map_copy(map));
6943 test = isl_map_apply_range(test, isl_map_copy(map));
6945 id = isl_map_identity(isl_dim_range(isl_map_get_dim(map)));
6947 sv = isl_map_is_subset(test, id);
6955 int isl_map_is_bijective(__isl_keep isl_map *map)
6959 sv = isl_map_is_single_valued(map);
6963 map = isl_map_copy(map);
6964 map = isl_map_reverse(map);
6965 sv = isl_map_is_single_valued(map);
6971 int isl_set_is_singleton(__isl_keep isl_set *set)
6973 return isl_map_is_single_valued((isl_map *)set);
6976 int isl_map_is_translation(__isl_keep isl_map *map)
6981 delta = isl_map_deltas(isl_map_copy(map));
6982 ok = isl_set_is_singleton(delta);
6983 isl_set_free(delta);
6988 static int unique(isl_int *p, unsigned pos, unsigned len)
6990 if (isl_seq_first_non_zero(p, pos) != -1)
6992 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
6997 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7006 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7009 nvar = isl_basic_set_dim(bset, isl_dim_set);
7010 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7011 for (j = 0; j < nvar; ++j) {
7012 int lower = 0, upper = 0;
7013 for (i = 0; i < bset->n_eq; ++i) {
7014 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7016 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7022 for (i = 0; i < bset->n_ineq; ++i) {
7023 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7025 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7027 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7032 if (!lower || !upper)
7039 int isl_set_is_box(__isl_keep isl_set *set)
7046 return isl_basic_set_is_box(set->p[0]);
7049 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7054 return isl_dim_is_wrapping(bset->dim);
7057 int isl_set_is_wrapping(__isl_keep isl_set *set)
7062 return isl_dim_is_wrapping(set->dim);
7065 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7067 bmap = isl_basic_map_cow(bmap);
7071 bmap->dim = isl_dim_wrap(bmap->dim);
7075 bmap = isl_basic_map_finalize(bmap);
7077 return (isl_basic_set *)bmap;
7079 isl_basic_map_free(bmap);
7083 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7087 map = isl_map_cow(map);
7091 for (i = 0; i < map->n; ++i) {
7092 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7096 map->dim = isl_dim_wrap(map->dim);
7100 return (isl_set *)map;
7106 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
7108 bset = isl_basic_set_cow(bset);
7112 bset->dim = isl_dim_unwrap(bset->dim);
7116 bset = isl_basic_set_finalize(bset);
7118 return (isl_basic_map *)bset;
7120 isl_basic_set_free(bset);
7124 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
7131 if (!isl_set_is_wrapping(set))
7132 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
7135 set = isl_set_cow(set);
7139 for (i = 0; i < set->n; ++i) {
7140 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
7145 set->dim = isl_dim_unwrap(set->dim);
7149 return (isl_map *)set;
7155 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
7156 enum isl_dim_type type)
7161 if (!isl_dim_is_named_or_nested(bmap->dim, type))
7164 bmap = isl_basic_map_cow(bmap);
7168 bmap->dim = isl_dim_reset(bmap->dim, type);
7172 bmap = isl_basic_map_finalize(bmap);
7176 isl_basic_map_free(bmap);
7180 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
7181 enum isl_dim_type type)
7188 if (!isl_dim_is_named_or_nested(map->dim, type))
7191 map = isl_map_cow(map);
7195 for (i = 0; i < map->n; ++i) {
7196 map->p[i] = isl_basic_map_reset(map->p[i], type);
7200 map->dim = isl_dim_reset(map->dim, type);