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
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;
61 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
64 case isl_dim_param: return 1;
65 case isl_dim_in: return 1 + dim->nparam;
66 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
70 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
71 struct isl_dim *dim, enum isl_dim_type type,
72 unsigned first, unsigned n, unsigned dst_pos)
80 src_pos = pos(dim, type);
81 for (i = 0; i < n; ++i)
82 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
85 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
86 enum isl_dim_type type, unsigned dst_pos)
88 isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
91 static void isl_dim_map_div(struct isl_dim_map *dim_map,
92 struct isl_basic_map *bmap, unsigned dst_pos)
97 if (!dim_map || !bmap)
100 src_pos = 1 + isl_dim_total(bmap->dim);
101 for (i = 0; i < bmap->n_div; ++i)
102 dim_map->pos[1 + dst_pos + i] = src_pos + i;
105 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
109 for (i = 0; i < dim_map->len; ++i)
110 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
111 fprintf(stderr, "\n");
114 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
115 enum isl_dim_type type)
120 case isl_dim_out: return isl_dim_size(bmap->dim, type);
121 case isl_dim_div: return bmap->n_div;
122 case isl_dim_all: return isl_basic_map_total_dim(bmap);
126 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
128 return map ? n(map->dim, type) : 0;
131 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
133 return set ? n(set->dim, type) : 0;
136 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
137 enum isl_dim_type type)
139 struct isl_dim *dim = bmap->dim;
141 case isl_dim_param: return 1;
142 case isl_dim_in: return 1 + dim->nparam;
143 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
144 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
148 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
150 return pos(map->dim, type);
153 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
154 enum isl_dim_type type)
156 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
159 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
161 return bset->dim->n_out;
164 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
166 return bset->dim->nparam;
169 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
171 return isl_dim_total(bset->dim) + bset->n_div;
174 unsigned isl_set_n_dim(const struct isl_set *set)
176 return set->dim->n_out;
179 unsigned isl_set_n_param(const struct isl_set *set)
181 return set->dim->nparam;
184 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
186 return bmap->dim->n_in;
189 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
191 return bmap->dim->n_out;
194 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
196 return bmap->dim->nparam;
199 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
204 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
206 return isl_dim_total(bmap->dim) + bmap->n_div;
209 unsigned isl_map_n_in(const struct isl_map *map)
211 return map->dim->n_in;
214 unsigned isl_map_n_out(const struct isl_map *map)
216 return map->dim->n_out;
219 unsigned isl_map_n_param(const struct isl_map *map)
221 return map->dim->nparam;
224 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
226 return map->dim->n_in == set->dim->n_out &&
227 map->dim->nparam == set->dim->nparam;
230 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
231 struct isl_basic_set *bset)
233 return bmap->dim->n_in == bset->dim->n_out &&
234 bmap->dim->nparam == bset->dim->nparam;
237 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
238 struct isl_basic_set *bset)
240 return bmap->dim->n_out == bset->dim->n_out &&
241 bmap->dim->nparam == bset->dim->nparam;
244 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
248 return isl_dim_copy(bmap->dim);
251 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
255 return isl_dim_copy(bset->dim);
258 struct isl_dim *isl_map_get_dim(struct isl_map *map)
262 return isl_dim_copy(map->dim);
265 struct isl_dim *isl_set_get_dim(struct isl_set *set)
269 return isl_dim_copy(set->dim);
272 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
273 struct isl_basic_map *bmap, unsigned extra,
274 unsigned n_eq, unsigned n_ineq)
277 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
279 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
280 if (isl_blk_is_error(bmap->block)) {
285 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
287 isl_blk_free(ctx, bmap->block);
293 bmap->block2 = isl_blk_empty();
296 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
297 if (isl_blk_is_error(bmap->block2)) {
299 isl_blk_free(ctx, bmap->block);
304 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
306 isl_blk_free(ctx, bmap->block2);
308 isl_blk_free(ctx, bmap->block);
314 for (i = 0; i < n_ineq + n_eq; ++i)
315 bmap->ineq[i] = bmap->block.data + i * row_size;
317 for (i = 0; i < extra; ++i)
318 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
324 bmap->c_size = n_eq + n_ineq;
325 bmap->eq = bmap->ineq + n_ineq;
334 isl_basic_map_free(bmap);
338 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
339 unsigned nparam, unsigned dim, unsigned extra,
340 unsigned n_eq, unsigned n_ineq)
342 struct isl_basic_map *bmap;
343 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
344 return (struct isl_basic_set *)bmap;
347 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
348 unsigned extra, unsigned n_eq, unsigned n_ineq)
350 struct isl_basic_map *bmap;
353 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
354 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
355 return (struct isl_basic_set *)bmap;
358 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
359 unsigned extra, unsigned n_eq, unsigned n_ineq)
361 struct isl_basic_map *bmap;
365 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
370 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
376 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
377 unsigned nparam, unsigned in, unsigned out, unsigned extra,
378 unsigned n_eq, unsigned n_ineq)
380 struct isl_basic_map *bmap;
383 dim = isl_dim_alloc(ctx, nparam, in, out);
387 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
391 static void dup_constraints(
392 struct isl_basic_map *dst, struct isl_basic_map *src)
395 unsigned total = isl_basic_map_total_dim(src);
397 for (i = 0; i < src->n_eq; ++i) {
398 int j = isl_basic_map_alloc_equality(dst);
399 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
402 for (i = 0; i < src->n_ineq; ++i) {
403 int j = isl_basic_map_alloc_inequality(dst);
404 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
407 for (i = 0; i < src->n_div; ++i) {
408 int j = isl_basic_map_alloc_div(dst);
409 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
411 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
414 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
416 struct isl_basic_map *dup;
420 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
421 bmap->n_div, bmap->n_eq, bmap->n_ineq);
424 dup_constraints(dup, bmap);
425 dup->flags = bmap->flags;
426 dup->sample = isl_vec_copy(bmap->sample);
430 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
432 struct isl_basic_map *dup;
434 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
435 return (struct isl_basic_set *)dup;
438 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
443 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
447 return isl_basic_set_dup(bset);
450 struct isl_set *isl_set_copy(struct isl_set *set)
459 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
464 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
468 return isl_basic_map_dup(bmap);
471 struct isl_map *isl_map_copy(struct isl_map *map)
480 void isl_basic_map_free(struct isl_basic_map *bmap)
488 isl_ctx_deref(bmap->ctx);
490 isl_blk_free(bmap->ctx, bmap->block2);
492 isl_blk_free(bmap->ctx, bmap->block);
493 isl_vec_free(bmap->sample);
494 isl_dim_free(bmap->dim);
498 void isl_basic_set_free(struct isl_basic_set *bset)
500 isl_basic_map_free((struct isl_basic_map *)bset);
503 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
505 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
508 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
514 isl_assert(ctx, room_for_con(bmap, 1), return -1);
515 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
517 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
518 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
519 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
520 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
521 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
522 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
524 int j = isl_basic_map_alloc_inequality(bmap);
528 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
529 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
536 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
537 bmap->extra - bmap->n_div);
541 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
543 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
546 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
550 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
555 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
557 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
560 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
565 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
567 if (pos != bmap->n_eq - 1) {
569 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
570 bmap->eq[bmap->n_eq - 1] = t;
576 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
578 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
581 void isl_basic_map_inequality_to_equality(
582 struct isl_basic_map *bmap, unsigned pos)
587 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
588 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
593 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
594 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
595 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
596 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
599 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
601 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
604 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
610 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
611 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
612 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
613 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
614 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
615 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
616 1 + isl_basic_map_total_dim(bmap),
617 bmap->extra - bmap->n_div);
618 return bmap->n_ineq++;
621 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
623 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
626 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
630 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
635 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
637 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
640 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
645 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
647 if (pos != bmap->n_ineq - 1) {
649 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
650 bmap->ineq[bmap->n_ineq - 1] = t;
651 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
657 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
659 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
662 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
667 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
670 k = isl_basic_map_alloc_equality(bmap);
673 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
676 isl_basic_map_free(bmap);
680 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
683 return (isl_basic_set *)
684 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
687 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
692 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
695 k = isl_basic_map_alloc_inequality(bmap);
698 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
701 isl_basic_map_free(bmap);
705 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
708 return (isl_basic_set *)
709 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
712 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
716 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
717 isl_seq_clr(bmap->div[bmap->n_div] +
718 1 + 1 + isl_basic_map_total_dim(bmap),
719 bmap->extra - bmap->n_div);
720 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
721 return bmap->n_div++;
724 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
726 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
729 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
733 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
738 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
740 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
743 /* Copy constraint from src to dst, putting the vars of src at offset
744 * dim_off in dst and the divs of src at offset div_off in dst.
745 * If both sets are actually map, then dim_off applies to the input
748 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
749 struct isl_basic_map *src_map, isl_int *src,
750 unsigned in_off, unsigned out_off, unsigned div_off)
752 unsigned src_nparam = isl_basic_map_n_param(src_map);
753 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
754 unsigned src_in = isl_basic_map_n_in(src_map);
755 unsigned dst_in = isl_basic_map_n_in(dst_map);
756 unsigned src_out = isl_basic_map_n_out(src_map);
757 unsigned dst_out = isl_basic_map_n_out(dst_map);
758 isl_int_set(dst[0], src[0]);
759 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
760 if (dst_nparam > src_nparam)
761 isl_seq_clr(dst+1+src_nparam,
762 dst_nparam - src_nparam);
763 isl_seq_clr(dst+1+dst_nparam, in_off);
764 isl_seq_cpy(dst+1+dst_nparam+in_off,
766 isl_min(dst_in-in_off, src_in));
767 if (dst_in-in_off > src_in)
768 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
769 dst_in - in_off - src_in);
770 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
771 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
772 src+1+src_nparam+src_in,
773 isl_min(dst_out-out_off, src_out));
774 if (dst_out-out_off > src_out)
775 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
776 dst_out - out_off - src_out);
777 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
778 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
779 src+1+src_nparam+src_in+src_out,
780 isl_min(dst_map->extra-div_off, src_map->n_div));
781 if (dst_map->n_div-div_off > src_map->n_div)
782 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
783 div_off+src_map->n_div,
784 dst_map->n_div - div_off - src_map->n_div);
787 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
788 struct isl_basic_map *src_map, isl_int *src,
789 unsigned in_off, unsigned out_off, unsigned div_off)
791 isl_int_set(dst[0], src[0]);
792 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
795 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
796 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
801 if (!bmap1 || !bmap2)
804 div_off = bmap1->n_div;
806 for (i = 0; i < bmap2->n_eq; ++i) {
807 int i1 = isl_basic_map_alloc_equality(bmap1);
810 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
811 i_pos, o_pos, div_off);
814 for (i = 0; i < bmap2->n_ineq; ++i) {
815 int i1 = isl_basic_map_alloc_inequality(bmap1);
818 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
819 i_pos, o_pos, div_off);
822 for (i = 0; i < bmap2->n_div; ++i) {
823 int i1 = isl_basic_map_alloc_div(bmap1);
826 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
827 i_pos, o_pos, div_off);
830 isl_basic_map_free(bmap2);
835 isl_basic_map_free(bmap1);
836 isl_basic_map_free(bmap2);
840 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
841 struct isl_dim_map *dim_map)
845 for (i = 0; i < dim_map->len; ++i) {
846 if (dim_map->pos[i] < 0)
847 isl_int_set_si(dst[i], 0);
849 isl_int_set(dst[i], src[dim_map->pos[i]]);
853 static void copy_div_dim_map(isl_int *dst, isl_int *src,
854 struct isl_dim_map *dim_map)
856 isl_int_set(dst[0], src[0]);
857 copy_constraint_dim_map(dst+1, src+1, dim_map);
860 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
861 struct isl_basic_map *src, struct isl_dim_map *dim_map)
865 if (!src || !dst || !dim_map)
868 for (i = 0; i < src->n_eq; ++i) {
869 int i1 = isl_basic_map_alloc_equality(dst);
872 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
875 for (i = 0; i < src->n_ineq; ++i) {
876 int i1 = isl_basic_map_alloc_inequality(dst);
879 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
882 for (i = 0; i < src->n_div; ++i) {
883 int i1 = isl_basic_map_alloc_div(dst);
886 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
890 isl_basic_map_free(src);
895 isl_basic_map_free(src);
896 isl_basic_map_free(dst);
900 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
901 struct isl_basic_set *bset2, unsigned pos)
903 return (struct isl_basic_set *)
904 add_constraints((struct isl_basic_map *)bset1,
905 (struct isl_basic_map *)bset2, 0, pos);
908 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
909 struct isl_dim *dim, unsigned extra,
910 unsigned n_eq, unsigned n_ineq)
912 struct isl_basic_map *ext;
922 dims_ok = isl_dim_equal(base->dim, dim) &&
923 base->extra >= base->n_div + extra;
925 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
926 room_for_ineq(base, n_ineq)) {
931 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
932 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
933 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
934 extra += base->extra;
936 n_ineq += base->n_ineq;
938 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
944 ext->sample = isl_vec_copy(base->sample);
946 ext = add_constraints(ext, base, 0, 0);
949 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
956 isl_basic_map_free(base);
960 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
961 struct isl_dim *dim, unsigned extra,
962 unsigned n_eq, unsigned n_ineq)
964 return (struct isl_basic_set *)
965 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
966 extra, n_eq, n_ineq);
969 struct isl_basic_map *isl_basic_map_extend_constraints(
970 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
974 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
978 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
979 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
980 unsigned n_eq, unsigned n_ineq)
982 struct isl_basic_map *bmap;
987 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
991 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
995 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
996 unsigned nparam, unsigned dim, unsigned extra,
997 unsigned n_eq, unsigned n_ineq)
999 return (struct isl_basic_set *)
1000 isl_basic_map_extend((struct isl_basic_map *)base,
1001 nparam, 0, dim, extra, n_eq, n_ineq);
1004 struct isl_basic_set *isl_basic_set_extend_constraints(
1005 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1007 return (struct isl_basic_set *)
1008 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1012 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1014 return (struct isl_basic_set *)
1015 isl_basic_map_cow((struct isl_basic_map *)bset);
1018 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1023 if (bmap->ref > 1) {
1025 bmap = isl_basic_map_dup(bmap);
1027 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1031 struct isl_set *isl_set_cow(struct isl_set *set)
1039 return isl_set_dup(set);
1042 struct isl_map *isl_map_cow(struct isl_map *map)
1050 return isl_map_dup(map);
1053 static void swap_vars(struct isl_blk blk, isl_int *a,
1054 unsigned a_len, unsigned b_len)
1056 isl_seq_cpy(blk.data, a+a_len, b_len);
1057 isl_seq_cpy(blk.data+b_len, a, a_len);
1058 isl_seq_cpy(a, blk.data, b_len+a_len);
1061 struct isl_basic_set *isl_basic_set_swap_vars(
1062 struct isl_basic_set *bset, unsigned n)
1072 nparam = isl_basic_set_n_param(bset);
1073 dim = isl_basic_set_n_dim(bset);
1074 isl_assert(bset->ctx, n <= dim, goto error);
1079 bset = isl_basic_set_cow(bset);
1083 blk = isl_blk_alloc(bset->ctx, dim);
1084 if (isl_blk_is_error(blk))
1087 for (i = 0; i < bset->n_eq; ++i)
1089 bset->eq[i]+1+nparam, n, dim - n);
1091 for (i = 0; i < bset->n_ineq; ++i)
1093 bset->ineq[i]+1+nparam, n, dim - n);
1095 for (i = 0; i < bset->n_div; ++i)
1097 bset->div[i]+1+1+nparam, n, dim - n);
1099 isl_blk_free(bset->ctx, blk);
1101 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1102 return isl_basic_set_gauss(bset, NULL);
1104 isl_basic_set_free(bset);
1108 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1111 set = isl_set_cow(set);
1115 for (i = 0; i < set->n; ++i) {
1116 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1122 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1126 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1132 total = isl_basic_map_total_dim(bmap);
1133 isl_basic_map_free_div(bmap, bmap->n_div);
1134 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1136 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1138 isl_basic_map_alloc_equality(bmap);
1142 isl_int_set_si(bmap->eq[i][0], 1);
1143 isl_seq_clr(bmap->eq[i]+1, total);
1144 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1145 isl_vec_free(bmap->sample);
1146 bmap->sample = NULL;
1147 return isl_basic_map_finalize(bmap);
1149 isl_basic_map_free(bmap);
1153 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1155 return (struct isl_basic_set *)
1156 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1159 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1162 unsigned off = isl_dim_total(bmap->dim);
1163 isl_int *t = bmap->div[a];
1164 bmap->div[a] = bmap->div[b];
1167 for (i = 0; i < bmap->n_eq; ++i)
1168 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1170 for (i = 0; i < bmap->n_ineq; ++i)
1171 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1173 for (i = 0; i < bmap->n_div; ++i)
1174 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1175 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1178 /* Eliminate the specified n dimensions starting at first from the
1179 * constraints using Fourier-Motzkin. The dimensions themselves
1182 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1183 enum isl_dim_type type, unsigned first, unsigned n)
1192 map = isl_map_cow(map);
1195 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1196 first += pos(map->dim, type) - 1;
1198 for (i = 0; i < map->n; ++i) {
1199 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1209 /* Eliminate the specified n dimensions starting at first from the
1210 * constraints using Fourier-Motzkin. The dimensions themselves
1213 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1214 enum isl_dim_type type, unsigned first, unsigned n)
1216 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1219 /* Eliminate the specified n dimensions starting at first from the
1220 * constraints using Fourier-Motzkin. The dimensions themselves
1223 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1224 unsigned first, unsigned n)
1226 return isl_set_eliminate(set, isl_dim_set, first, n);
1229 /* Project out n dimensions starting at first using Fourier-Motzkin */
1230 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1231 unsigned first, unsigned n)
1233 set = isl_set_eliminate_dims(set, first, n);
1234 set = isl_set_drop_dims(set, first, n);
1238 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1240 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1248 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1257 set = isl_set_cow(set);
1261 for (i = 0; i < set->n; ++i) {
1262 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1272 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1273 enum isl_dim_type type, unsigned first, unsigned n)
1277 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1281 bmap = isl_basic_map_eliminate_vars(bmap,
1282 isl_basic_map_offset(bmap, type) - 1 + first, n);
1285 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1287 bmap = isl_basic_map_drop(bmap, type, first, n);
1290 isl_basic_map_free(bmap);
1294 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1295 enum isl_dim_type type, unsigned first, unsigned n)
1297 return (isl_basic_set *)
1298 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1301 struct isl_map *isl_map_remove(struct isl_map *map,
1302 enum isl_dim_type type, unsigned first, unsigned n)
1309 map = isl_map_cow(map);
1312 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1314 for (i = 0; i < map->n; ++i) {
1315 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1316 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1320 map = isl_map_drop(map, type, first, n);
1327 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1328 enum isl_dim_type type, unsigned first, unsigned n)
1330 return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1333 /* Project out n inputs starting at first using Fourier-Motzkin */
1334 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1335 unsigned first, unsigned n)
1337 return isl_map_remove(map, isl_dim_in, first, n);
1340 /* Project out n dimensions starting at first using Fourier-Motzkin */
1341 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1342 unsigned first, unsigned n)
1344 unsigned nparam = isl_basic_set_n_param(bset);
1345 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1346 bset = isl_basic_set_drop_dims(bset, first, n);
1350 static void dump_term(struct isl_basic_map *bmap,
1351 isl_int c, int pos, FILE *out)
1354 unsigned in = isl_basic_map_n_in(bmap);
1355 unsigned dim = in + isl_basic_map_n_out(bmap);
1356 unsigned nparam = isl_basic_map_n_param(bmap);
1358 isl_int_print(out, c, 0);
1360 if (!isl_int_is_one(c))
1361 isl_int_print(out, c, 0);
1362 if (pos < 1 + nparam) {
1363 name = isl_dim_get_name(bmap->dim,
1364 isl_dim_param, pos - 1);
1366 fprintf(out, "%s", name);
1368 fprintf(out, "p%d", pos - 1);
1369 } else if (pos < 1 + nparam + in)
1370 fprintf(out, "i%d", pos - 1 - nparam);
1371 else if (pos < 1 + nparam + dim)
1372 fprintf(out, "o%d", pos - 1 - nparam - in);
1374 fprintf(out, "e%d", pos - 1 - nparam - dim);
1378 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1379 int sign, FILE *out)
1383 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1387 for (i = 0, first = 1; i < len; ++i) {
1388 if (isl_int_sgn(c[i]) * sign <= 0)
1391 fprintf(out, " + ");
1393 isl_int_abs(v, c[i]);
1394 dump_term(bmap, v, i, out);
1401 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1402 const char *op, FILE *out, int indent)
1406 fprintf(out, "%*s", indent, "");
1408 dump_constraint_sign(bmap, c, 1, out);
1409 fprintf(out, " %s ", op);
1410 dump_constraint_sign(bmap, c, -1, out);
1414 for (i = bmap->n_div; i < bmap->extra; ++i) {
1415 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1417 fprintf(out, "%*s", indent, "");
1418 fprintf(out, "ERROR: unused div coefficient not zero\n");
1423 static void dump_constraints(struct isl_basic_map *bmap,
1424 isl_int **c, unsigned n,
1425 const char *op, FILE *out, int indent)
1429 for (i = 0; i < n; ++i)
1430 dump_constraint(bmap, c[i], op, out, indent);
1433 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1437 unsigned total = isl_basic_map_total_dim(bmap);
1439 for (j = 0; j < 1 + total; ++j) {
1440 if (isl_int_is_zero(exp[j]))
1442 if (!first && isl_int_is_pos(exp[j]))
1444 dump_term(bmap, exp[j], j, out);
1449 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1453 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1454 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1456 for (i = 0; i < bmap->n_div; ++i) {
1457 fprintf(out, "%*s", indent, "");
1458 fprintf(out, "e%d = [(", i);
1459 dump_affine(bmap, bmap->div[i]+1, out);
1461 isl_int_print(out, bmap->div[i][0], 0);
1462 fprintf(out, "]\n");
1466 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1469 fprintf(out, "null basic set\n");
1473 fprintf(out, "%*s", indent, "");
1474 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1475 bset->ref, bset->dim->nparam, bset->dim->n_out,
1476 bset->extra, bset->flags);
1477 dump((struct isl_basic_map *)bset, out, indent);
1480 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1483 fprintf(out, "null basic map\n");
1487 fprintf(out, "%*s", indent, "");
1488 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1489 "flags: %x, n_name: %d\n",
1491 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1492 bmap->extra, bmap->flags, bmap->dim->n_name);
1493 dump(bmap, out, indent);
1496 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1501 total = isl_basic_map_total_dim(bmap);
1502 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1503 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1504 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1505 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1509 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1511 struct isl_set *set;
1515 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1516 isl_assert(dim->ctx, n >= 0, return NULL);
1517 set = isl_alloc(dim->ctx, struct isl_set,
1518 sizeof(struct isl_set) +
1519 (n - 1) * sizeof(struct isl_basic_set *));
1523 set->ctx = dim->ctx;
1524 isl_ctx_ref(set->ctx);
1536 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1537 unsigned nparam, unsigned dim, int n, unsigned flags)
1539 struct isl_set *set;
1540 struct isl_dim *dims;
1542 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1546 set = isl_set_alloc_dim(dims, n, flags);
1550 /* Make sure "map" has room for at least "n" more basic maps.
1552 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1555 struct isl_map *grown = NULL;
1559 isl_assert(map->ctx, n >= 0, goto error);
1560 if (map->n + n <= map->size)
1562 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1565 for (i = 0; i < map->n; ++i) {
1566 grown->p[i] = isl_basic_map_copy(map->p[i]);
1574 isl_map_free(grown);
1579 /* Make sure "set" has room for at least "n" more basic sets.
1581 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1583 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1586 struct isl_set *isl_set_dup(struct isl_set *set)
1589 struct isl_set *dup;
1594 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1597 for (i = 0; i < set->n; ++i)
1598 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1602 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1604 struct isl_set *set;
1609 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1611 isl_basic_set_free(bset);
1614 return isl_set_add_basic_set(set, bset);
1617 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1619 struct isl_map *map;
1624 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1626 isl_basic_map_free(bmap);
1629 return isl_map_add_basic_map(map, bmap);
1632 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1633 __isl_take isl_basic_set *bset)
1635 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1636 (struct isl_basic_map *)bset);
1639 void isl_set_free(struct isl_set *set)
1649 isl_ctx_deref(set->ctx);
1650 for (i = 0; i < set->n; ++i)
1651 isl_basic_set_free(set->p[i]);
1652 isl_dim_free(set->dim);
1656 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1661 fprintf(out, "null set\n");
1665 fprintf(out, "%*s", indent, "");
1666 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1667 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1669 for (i = 0; i < set->n; ++i) {
1670 fprintf(out, "%*s", indent, "");
1671 fprintf(out, "basic set %d:\n", i);
1672 isl_basic_set_dump(set->p[i], out, indent+4);
1676 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1681 fprintf(out, "null map\n");
1685 fprintf(out, "%*s", indent, "");
1686 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1687 "flags: %x, n_name: %d\n",
1688 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1689 map->dim->n_out, map->flags, map->dim->n_name);
1690 for (i = 0; i < map->n; ++i) {
1691 fprintf(out, "%*s", indent, "");
1692 fprintf(out, "basic map %d:\n", i);
1693 isl_basic_map_dump(map->p[i], out, indent+4);
1697 struct isl_basic_map *isl_basic_map_intersect_domain(
1698 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1700 struct isl_basic_map *bmap_domain;
1701 struct isl_dim *dim;
1706 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1707 bset->dim, isl_dim_param), goto error);
1709 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1710 isl_assert(bset->ctx,
1711 isl_basic_map_compatible_domain(bmap, bset), goto error);
1713 bmap = isl_basic_map_cow(bmap);
1714 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1715 bset->n_div, bset->n_eq, bset->n_ineq);
1718 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1719 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1720 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1722 bmap = isl_basic_map_simplify(bmap);
1723 return isl_basic_map_finalize(bmap);
1725 isl_basic_map_free(bmap);
1726 isl_basic_set_free(bset);
1730 struct isl_basic_map *isl_basic_map_intersect_range(
1731 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1733 struct isl_basic_map *bmap_range;
1738 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1739 bset->dim, isl_dim_param), goto error);
1741 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1742 isl_assert(bset->ctx,
1743 isl_basic_map_compatible_range(bmap, bset), goto error);
1745 bmap = isl_basic_map_cow(bmap);
1746 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1747 bset->n_div, bset->n_eq, bset->n_ineq);
1750 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1751 bmap = add_constraints(bmap, bmap_range, 0, 0);
1753 bmap = isl_basic_map_simplify(bmap);
1754 return isl_basic_map_finalize(bmap);
1756 isl_basic_map_free(bmap);
1757 isl_basic_set_free(bset);
1761 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1767 total = 1 + isl_basic_map_total_dim(bmap);
1768 if (total != vec->size)
1773 for (i = 0; i < bmap->n_eq; ++i) {
1774 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1775 if (!isl_int_is_zero(s)) {
1781 for (i = 0; i < bmap->n_ineq; ++i) {
1782 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1783 if (isl_int_is_neg(s)) {
1794 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1796 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1799 struct isl_basic_map *isl_basic_map_intersect(
1800 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1802 struct isl_vec *sample = NULL;
1804 if (!bmap1 || !bmap2)
1807 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1808 bmap2->dim, isl_dim_param), goto error);
1809 if (isl_dim_total(bmap1->dim) ==
1810 isl_dim_size(bmap1->dim, isl_dim_param) &&
1811 isl_dim_total(bmap2->dim) !=
1812 isl_dim_size(bmap2->dim, isl_dim_param))
1813 return isl_basic_map_intersect(bmap2, bmap1);
1815 if (isl_dim_total(bmap2->dim) !=
1816 isl_dim_size(bmap2->dim, isl_dim_param))
1817 isl_assert(bmap1->ctx,
1818 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1820 if (bmap1->sample &&
1821 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1822 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1823 sample = isl_vec_copy(bmap1->sample);
1824 else if (bmap2->sample &&
1825 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1826 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1827 sample = isl_vec_copy(bmap2->sample);
1829 bmap1 = isl_basic_map_cow(bmap1);
1830 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1831 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1834 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1837 isl_vec_free(bmap1->sample);
1838 bmap1->sample = sample;
1841 bmap1 = isl_basic_map_simplify(bmap1);
1842 return isl_basic_map_finalize(bmap1);
1845 isl_vec_free(sample);
1846 isl_basic_map_free(bmap1);
1847 isl_basic_map_free(bmap2);
1851 struct isl_basic_set *isl_basic_set_intersect(
1852 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1854 return (struct isl_basic_set *)
1855 isl_basic_map_intersect(
1856 (struct isl_basic_map *)bset1,
1857 (struct isl_basic_map *)bset2);
1860 /* Special case of isl_map_intersect, where both map1 and map2
1861 * are convex, without any divs and such that either map1 or map2
1862 * contains a single constraint. This constraint is then simply
1863 * added to the other map.
1865 static __isl_give isl_map *map_intersect_add_constraint(
1866 __isl_take isl_map *map1, __isl_take isl_map *map2)
1868 struct isl_basic_map *bmap1;
1869 struct isl_basic_map *bmap2;
1871 isl_assert(map1->ctx, map1->n == 1, goto error);
1872 isl_assert(map2->ctx, map1->n == 1, goto error);
1873 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
1874 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
1876 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
1877 return isl_map_intersect(map2, map1);
1879 isl_assert(map2->ctx,
1880 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
1882 map1 = isl_map_cow(map1);
1885 map1->p[0] = isl_basic_map_cow(map1->p[0]);
1886 if (map2->p[0]->n_eq == 1)
1887 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
1889 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
1890 map2->p[0]->ineq[0]);
1892 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
1893 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
1897 if (isl_basic_map_fast_is_empty(map1->p[0])) {
1898 isl_basic_map_free(map1->p[0]);
1911 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1914 struct isl_map *result;
1920 if (map1->n == 1 && map2->n == 1 &&
1921 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
1922 isl_dim_equal(map1->dim, map2->dim) &&
1923 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
1924 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
1925 return map_intersect_add_constraint(map1, map2);
1926 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1927 map2->dim, isl_dim_param), goto error);
1928 if (isl_dim_total(map1->dim) ==
1929 isl_dim_size(map1->dim, isl_dim_param) &&
1930 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1931 return isl_map_intersect(map2, map1);
1933 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1934 isl_assert(map1->ctx,
1935 isl_dim_equal(map1->dim, map2->dim), goto error);
1937 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1938 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1939 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1941 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1942 map1->n * map2->n, flags);
1945 for (i = 0; i < map1->n; ++i)
1946 for (j = 0; j < map2->n; ++j) {
1947 struct isl_basic_map *part;
1948 part = isl_basic_map_intersect(
1949 isl_basic_map_copy(map1->p[i]),
1950 isl_basic_map_copy(map2->p[j]));
1951 if (isl_basic_map_is_empty(part))
1952 isl_basic_map_free(part);
1954 result = isl_map_add_basic_map(result, part);
1967 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1969 return (struct isl_set *)
1970 isl_map_intersect((struct isl_map *)set1,
1971 (struct isl_map *)set2);
1974 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1976 struct isl_dim *dim;
1977 struct isl_basic_set *bset;
1982 bmap = isl_basic_map_cow(bmap);
1985 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1986 in = isl_basic_map_n_in(bmap);
1987 bset = isl_basic_set_from_basic_map(bmap);
1988 bset = isl_basic_set_swap_vars(bset, in);
1989 return isl_basic_map_from_basic_set(bset, dim);
1992 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
1993 enum isl_dim_type type, unsigned pos, unsigned n)
1995 struct isl_dim *res_dim;
1996 struct isl_basic_map *res;
1997 struct isl_dim_map *dim_map;
1998 unsigned total, off;
1999 enum isl_dim_type t;
2007 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2009 total = isl_basic_map_total_dim(bmap) + n;
2010 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2012 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2014 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2016 unsigned size = isl_basic_map_dim(bmap, t);
2017 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2019 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2020 pos, size - pos, off + pos + n);
2022 off += isl_dim_size(res_dim, t);
2024 isl_dim_map_div(dim_map, bmap, off);
2026 res = isl_basic_map_alloc_dim(res_dim,
2027 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2028 res = add_constraints_dim_map(res, bmap, dim_map);
2029 res = isl_basic_map_simplify(res);
2030 return isl_basic_map_finalize(res);
2033 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2034 enum isl_dim_type type, unsigned n)
2038 return isl_basic_map_insert(bmap, type,
2039 isl_basic_map_dim(bmap, type), n);
2042 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2043 enum isl_dim_type type, unsigned n)
2047 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2048 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2050 isl_basic_set_free(bset);
2054 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2055 enum isl_dim_type type, unsigned pos, unsigned n)
2062 map = isl_map_cow(map);
2066 map->dim = isl_dim_insert(map->dim, type, pos, n);
2070 for (i = 0; i < map->n; ++i) {
2071 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2082 __isl_give isl_map *isl_map_add(__isl_take isl_map *map,
2083 enum isl_dim_type type, unsigned n)
2087 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2090 __isl_give isl_set *isl_set_add(__isl_take isl_set *set,
2091 enum isl_dim_type type, unsigned n)
2095 isl_assert(set->ctx, type != isl_dim_in, goto error);
2096 return (isl_set *)isl_map_add((isl_map *)set, type, n);
2102 __isl_give isl_basic_map *isl_basic_map_move(__isl_take isl_basic_map *bmap,
2103 enum isl_dim_type dst_type, unsigned dst_pos,
2104 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2107 struct isl_dim_map *dim_map;
2108 struct isl_basic_map *res;
2109 enum isl_dim_type t;
2110 unsigned total, off;
2117 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2120 if (dst_type == src_type && dst_pos == src_pos)
2123 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2125 if (pos(bmap->dim, dst_type) + dst_pos ==
2126 pos(bmap->dim, src_type) + src_pos +
2127 ((src_type < dst_type) ? n : 0)) {
2128 bmap = isl_basic_map_cow(bmap);
2132 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2133 src_type, src_pos, n);
2140 total = isl_basic_map_total_dim(bmap);
2141 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2144 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2145 unsigned size = isl_dim_size(bmap->dim, t);
2146 if (t == dst_type) {
2147 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2150 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2153 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2154 dst_pos, size - dst_pos, off);
2155 off += size - dst_pos;
2156 } else if (t == src_type) {
2157 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2160 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2161 src_pos + n, size - src_pos - n, off);
2162 off += size - src_pos - n;
2164 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2168 isl_dim_map_div(dim_map, bmap, off + n);
2170 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2171 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2172 bmap = add_constraints_dim_map(res, bmap, dim_map);
2174 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2175 src_type, src_pos, n);
2181 isl_basic_map_free(bmap);
2185 __isl_give isl_set *isl_set_move(__isl_take isl_set *set,
2186 enum isl_dim_type dst_type, unsigned dst_pos,
2187 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2191 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2192 return (isl_set *)isl_map_move((isl_map *)set, dst_type, dst_pos,
2193 src_type, src_pos, n);
2199 __isl_give isl_map *isl_map_move(__isl_take isl_map *map,
2200 enum isl_dim_type dst_type, unsigned dst_pos,
2201 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2210 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2213 if (dst_type == src_type && dst_pos == src_pos)
2216 isl_assert(map->ctx, dst_type != src_type, goto error);
2218 map = isl_map_cow(map);
2222 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2226 for (i = 0; i < map->n; ++i) {
2227 map->p[i] = isl_basic_map_move(map->p[i], dst_type, dst_pos,
2228 src_type, src_pos, n);
2239 /* Move the specified dimensions to the last columns right before
2240 * the divs. Don't change the dimension specification of bmap.
2241 * That's the responsibility of the caller.
2243 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2244 enum isl_dim_type type, unsigned first, unsigned n)
2246 struct isl_dim_map *dim_map;
2247 struct isl_basic_map *res;
2248 enum isl_dim_type t;
2249 unsigned total, off;
2253 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2256 total = isl_basic_map_total_dim(bmap);
2257 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2260 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2261 unsigned size = isl_dim_size(bmap->dim, t);
2263 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2266 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2267 first, n, total - bmap->n_div - n);
2268 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2269 first + n, size - (first + n), off);
2270 off += size - (first + n);
2272 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2276 isl_dim_map_div(dim_map, bmap, off + n);
2278 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2279 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2280 res = add_constraints_dim_map(res, bmap, dim_map);
2284 /* Turn the n dimensions of type type, starting at first
2285 * into existentially quantified variables.
2287 __isl_give isl_basic_map *isl_basic_map_project_out(
2288 __isl_take isl_basic_map *bmap,
2289 enum isl_dim_type type, unsigned first, unsigned n)
2302 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2303 return isl_basic_map_remove(bmap, type, first, n);
2305 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2308 bmap = move_last(bmap, type, first, n);
2309 bmap = isl_basic_map_cow(bmap);
2311 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2312 old = bmap->block2.data;
2313 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2314 (bmap->extra + n) * (1 + row_size));
2315 if (!bmap->block2.data)
2317 new_div = isl_alloc_array(ctx, isl_int *, bmap->extra + n);
2320 for (i = 0; i < n; ++i) {
2321 new_div[i] = bmap->block2.data +
2322 (bmap->extra + i) * (1 + row_size);
2323 isl_seq_clr(new_div[i], 1 + row_size);
2325 for (i = 0; i < bmap->extra; ++i)
2326 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2328 bmap->div = new_div;
2332 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2335 bmap = isl_basic_map_simplify(bmap);
2336 bmap = isl_basic_map_drop_redundant_divs(bmap);
2337 return isl_basic_map_finalize(bmap);
2339 isl_basic_map_free(bmap);
2343 /* Turn the n dimensions of type type, starting at first
2344 * into existentially quantified variables.
2346 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2347 enum isl_dim_type type, unsigned first, unsigned n)
2349 return (isl_basic_set *)isl_basic_map_project_out(
2350 (isl_basic_map *)bset, type, first, n);
2353 /* Turn the n dimensions of type type, starting at first
2354 * into existentially quantified variables.
2356 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2357 enum isl_dim_type type, unsigned first, unsigned n)
2367 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2369 map = isl_map_cow(map);
2373 map->dim = isl_dim_drop(map->dim, type, first, n);
2377 for (i = 0; i < map->n; ++i) {
2378 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2389 /* Turn the n dimensions of type type, starting at first
2390 * into existentially quantified variables.
2392 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2393 enum isl_dim_type type, unsigned first, unsigned n)
2395 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2398 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2402 for (i = 0; i < n; ++i) {
2403 j = isl_basic_map_alloc_div(bmap);
2406 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2410 isl_basic_map_free(bmap);
2414 struct isl_basic_map *isl_basic_map_apply_range(
2415 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2417 struct isl_dim *dim_result = NULL;
2418 struct isl_basic_map *bmap;
2419 unsigned n_in, n_out, n, nparam, total, pos;
2420 struct isl_dim_map *dim_map1, *dim_map2;
2422 if (!bmap1 || !bmap2)
2425 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2426 isl_dim_copy(bmap2->dim));
2428 n_in = isl_basic_map_n_in(bmap1);
2429 n_out = isl_basic_map_n_out(bmap2);
2430 n = isl_basic_map_n_out(bmap1);
2431 nparam = isl_basic_map_n_param(bmap1);
2433 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2434 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2435 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2436 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2437 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2438 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2439 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2440 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2441 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2442 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2443 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2445 bmap = isl_basic_map_alloc_dim(dim_result,
2446 bmap1->n_div + bmap2->n_div + n,
2447 bmap1->n_eq + bmap2->n_eq,
2448 bmap1->n_ineq + bmap2->n_ineq);
2449 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2450 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2451 bmap = add_divs(bmap, n);
2452 bmap = isl_basic_map_simplify(bmap);
2453 bmap = isl_basic_map_drop_redundant_divs(bmap);
2454 return isl_basic_map_finalize(bmap);
2456 isl_basic_map_free(bmap1);
2457 isl_basic_map_free(bmap2);
2461 struct isl_basic_set *isl_basic_set_apply(
2462 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2467 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2470 return (struct isl_basic_set *)
2471 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2473 isl_basic_set_free(bset);
2474 isl_basic_map_free(bmap);
2478 struct isl_basic_map *isl_basic_map_apply_domain(
2479 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2481 if (!bmap1 || !bmap2)
2484 isl_assert(bmap1->ctx,
2485 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2486 isl_assert(bmap1->ctx,
2487 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2490 bmap1 = isl_basic_map_reverse(bmap1);
2491 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2492 return isl_basic_map_reverse(bmap1);
2494 isl_basic_map_free(bmap1);
2495 isl_basic_map_free(bmap2);
2499 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2500 * A \cap B -> f(A) + f(B)
2502 struct isl_basic_map *isl_basic_map_sum(
2503 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2505 unsigned n_in, n_out, nparam, total, pos;
2506 struct isl_basic_map *bmap = NULL;
2507 struct isl_dim_map *dim_map1, *dim_map2;
2510 if (!bmap1 || !bmap2)
2513 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2516 nparam = isl_basic_map_n_param(bmap1);
2517 n_in = isl_basic_map_n_in(bmap1);
2518 n_out = isl_basic_map_n_out(bmap1);
2520 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2521 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2522 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2523 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2524 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2525 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2526 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2527 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2528 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2529 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2530 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2532 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2533 bmap1->n_div + bmap2->n_div + 2 * n_out,
2534 bmap1->n_eq + bmap2->n_eq + n_out,
2535 bmap1->n_ineq + bmap2->n_ineq);
2536 for (i = 0; i < n_out; ++i) {
2537 int j = isl_basic_map_alloc_equality(bmap);
2540 isl_seq_clr(bmap->eq[j], 1+total);
2541 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2542 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2543 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2545 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2546 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2547 bmap = add_divs(bmap, 2 * n_out);
2549 bmap = isl_basic_map_simplify(bmap);
2550 return isl_basic_map_finalize(bmap);
2552 isl_basic_map_free(bmap);
2553 isl_basic_map_free(bmap1);
2554 isl_basic_map_free(bmap2);
2558 /* Given two maps A -> f(A) and B -> g(B), construct a map
2559 * A \cap B -> f(A) + f(B)
2561 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2563 struct isl_map *result;
2569 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2571 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2572 map1->n * map2->n, 0);
2575 for (i = 0; i < map1->n; ++i)
2576 for (j = 0; j < map2->n; ++j) {
2577 struct isl_basic_map *part;
2578 part = isl_basic_map_sum(
2579 isl_basic_map_copy(map1->p[i]),
2580 isl_basic_map_copy(map2->p[j]));
2581 if (isl_basic_map_is_empty(part))
2582 isl_basic_map_free(part);
2584 result = isl_map_add_basic_map(result, part);
2597 /* Given a basic map A -> f(A), construct A -> -f(A).
2599 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2604 bmap = isl_basic_map_cow(bmap);
2608 n = isl_basic_map_dim(bmap, isl_dim_out);
2609 off = isl_basic_map_offset(bmap, isl_dim_out);
2610 for (i = 0; i < bmap->n_eq; ++i)
2611 for (j = 0; j < n; ++j)
2612 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2613 for (i = 0; i < bmap->n_ineq; ++i)
2614 for (j = 0; j < n; ++j)
2615 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2616 for (i = 0; i < bmap->n_div; ++i)
2617 for (j = 0; j < n; ++j)
2618 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2619 return isl_basic_map_finalize(bmap);
2622 /* Given a map A -> f(A), construct A -> -f(A).
2624 struct isl_map *isl_map_neg(struct isl_map *map)
2628 map = isl_map_cow(map);
2632 for (i = 0; i < map->n; ++i) {
2633 map->p[i] = isl_basic_map_neg(map->p[i]);
2644 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2645 * A -> floor(f(A)/d).
2647 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2650 unsigned n_in, n_out, nparam, total, pos;
2651 struct isl_basic_map *result = NULL;
2652 struct isl_dim_map *dim_map;
2658 nparam = isl_basic_map_n_param(bmap);
2659 n_in = isl_basic_map_n_in(bmap);
2660 n_out = isl_basic_map_n_out(bmap);
2662 total = nparam + n_in + n_out + bmap->n_div + n_out;
2663 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2664 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2665 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2666 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2667 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2669 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2670 bmap->n_div + n_out,
2671 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2672 result = add_constraints_dim_map(result, bmap, dim_map);
2673 result = add_divs(result, n_out);
2674 for (i = 0; i < n_out; ++i) {
2676 j = isl_basic_map_alloc_inequality(result);
2679 isl_seq_clr(result->ineq[j], 1+total);
2680 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2681 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2682 j = isl_basic_map_alloc_inequality(result);
2685 isl_seq_clr(result->ineq[j], 1+total);
2686 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2687 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2688 isl_int_sub_ui(result->ineq[j][0], d, 1);
2691 result = isl_basic_map_simplify(result);
2692 return isl_basic_map_finalize(result);
2694 isl_basic_map_free(result);
2698 /* Given a map A -> f(A) and an integer d, construct a map
2699 * A -> floor(f(A)/d).
2701 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2705 map = isl_map_cow(map);
2709 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2710 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2711 for (i = 0; i < map->n; ++i) {
2712 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2723 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2729 i = isl_basic_map_alloc_equality(bmap);
2732 nparam = isl_basic_map_n_param(bmap);
2733 n_in = isl_basic_map_n_in(bmap);
2734 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2735 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2736 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2737 return isl_basic_map_finalize(bmap);
2739 isl_basic_map_free(bmap);
2743 /* Add a constraints to "bmap" expressing i_pos < o_pos
2745 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2751 i = isl_basic_map_alloc_inequality(bmap);
2754 nparam = isl_basic_map_n_param(bmap);
2755 n_in = isl_basic_map_n_in(bmap);
2756 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2757 isl_int_set_si(bmap->ineq[i][0], -1);
2758 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2759 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2760 return isl_basic_map_finalize(bmap);
2762 isl_basic_map_free(bmap);
2766 /* Add a constraints to "bmap" expressing i_pos > o_pos
2768 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2774 i = isl_basic_map_alloc_inequality(bmap);
2777 nparam = isl_basic_map_n_param(bmap);
2778 n_in = isl_basic_map_n_in(bmap);
2779 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2780 isl_int_set_si(bmap->ineq[i][0], -1);
2781 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2782 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2783 return isl_basic_map_finalize(bmap);
2785 isl_basic_map_free(bmap);
2789 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2792 struct isl_basic_map *bmap;
2793 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2796 for (i = 0; i < n_equal && bmap; ++i)
2797 bmap = var_equal(bmap, i);
2798 return isl_basic_map_finalize(bmap);
2801 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2803 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2806 struct isl_basic_map *bmap;
2807 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2810 for (i = 0; i < pos && bmap; ++i)
2811 bmap = var_equal(bmap, i);
2813 bmap = var_less(bmap, pos);
2814 return isl_basic_map_finalize(bmap);
2817 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2819 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2822 struct isl_basic_map *bmap;
2823 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2826 for (i = 0; i < pos && bmap; ++i)
2827 bmap = var_equal(bmap, i);
2829 bmap = var_more(bmap, pos);
2830 return isl_basic_map_finalize(bmap);
2833 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
2835 struct isl_map *map;
2842 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2844 for (i = 0; i < dim; ++i)
2845 map = isl_map_add_basic_map(map,
2846 isl_basic_map_less_at(isl_dim_copy(dims), i));
2848 map = isl_map_add_basic_map(map,
2849 isl_basic_map_equal(isl_dim_copy(dims), dim));
2855 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
2857 return map_lex_lte(isl_dim_map(set_dim), 0);
2860 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
2862 return map_lex_lte(isl_dim_map(set_dim), 1);
2865 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
2867 struct isl_map *map;
2874 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2876 for (i = 0; i < dim; ++i)
2877 map = isl_map_add_basic_map(map,
2878 isl_basic_map_more_at(isl_dim_copy(dims), i));
2880 map = isl_map_add_basic_map(map,
2881 isl_basic_map_equal(isl_dim_copy(dims), dim));
2887 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
2889 return map_lex_gte(isl_dim_map(set_dim), 0);
2892 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
2894 return map_lex_gte(isl_dim_map(set_dim), 1);
2897 struct isl_basic_map *isl_basic_map_from_basic_set(
2898 struct isl_basic_set *bset, struct isl_dim *dim)
2900 struct isl_basic_map *bmap;
2902 bset = isl_basic_set_cow(bset);
2906 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2907 isl_dim_free(bset->dim);
2908 bmap = (struct isl_basic_map *) bset;
2910 return isl_basic_map_finalize(bmap);
2912 isl_basic_set_free(bset);
2917 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2921 if (bmap->dim->n_in == 0)
2922 return (struct isl_basic_set *)bmap;
2923 bmap = isl_basic_map_cow(bmap);
2926 bmap->dim = isl_dim_cow(bmap->dim);
2929 bmap->dim->n_out += bmap->dim->n_in;
2930 bmap->dim->n_in = 0;
2931 bmap = isl_basic_map_finalize(bmap);
2932 return (struct isl_basic_set *)bmap;
2934 isl_basic_map_free(bmap);
2938 /* For a div d = floor(f/m), add the constraints
2941 * -(f-(n-1)) + m d >= 0
2943 * Note that the second constraint is the negation of
2947 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2950 unsigned total = isl_basic_map_total_dim(bmap);
2951 unsigned div_pos = 1 + total - bmap->n_div + div;
2953 i = isl_basic_map_alloc_inequality(bmap);
2956 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2957 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2959 j = isl_basic_map_alloc_inequality(bmap);
2962 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2963 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2964 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2968 struct isl_basic_set *isl_basic_map_underlying_set(
2969 struct isl_basic_map *bmap)
2973 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2974 return (struct isl_basic_set *)bmap;
2975 bmap = isl_basic_map_cow(bmap);
2978 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2981 bmap->extra -= bmap->n_div;
2983 bmap = isl_basic_map_finalize(bmap);
2984 return (struct isl_basic_set *)bmap;
2989 __isl_give isl_basic_set *isl_basic_set_underlying_set(
2990 __isl_take isl_basic_set *bset)
2992 return isl_basic_map_underlying_set((isl_basic_map *)bset);
2995 struct isl_basic_map *isl_basic_map_overlying_set(
2996 struct isl_basic_set *bset, struct isl_basic_map *like)
2998 struct isl_basic_map *bmap;
2999 struct isl_ctx *ctx;
3006 isl_assert(ctx, bset->n_div == 0, goto error);
3007 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3008 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3010 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3011 isl_basic_map_free(like);
3012 return (struct isl_basic_map *)bset;
3014 bset = isl_basic_set_cow(bset);
3017 total = bset->dim->n_out + bset->extra;
3018 bmap = (struct isl_basic_map *)bset;
3019 isl_dim_free(bmap->dim);
3020 bmap->dim = isl_dim_copy(like->dim);
3023 bmap->n_div = like->n_div;
3024 bmap->extra += like->n_div;
3027 ltotal = total - bmap->extra + like->extra;
3030 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3031 bmap->extra * (1 + 1 + total));
3032 if (isl_blk_is_error(bmap->block2))
3034 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3038 for (i = 0; i < bmap->extra; ++i)
3039 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3040 for (i = 0; i < like->n_div; ++i) {
3041 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3042 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3044 bmap = isl_basic_map_extend_constraints(bmap,
3045 0, 2 * like->n_div);
3046 for (i = 0; i < like->n_div; ++i) {
3047 if (isl_int_is_zero(bmap->div[i][0]))
3049 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3053 isl_basic_map_free(like);
3054 bmap = isl_basic_map_simplify(bmap);
3055 bmap = isl_basic_map_finalize(bmap);
3058 isl_basic_map_free(like);
3059 isl_basic_set_free(bset);
3063 struct isl_basic_set *isl_basic_set_from_underlying_set(
3064 struct isl_basic_set *bset, struct isl_basic_set *like)
3066 return (struct isl_basic_set *)
3067 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3070 struct isl_set *isl_set_from_underlying_set(
3071 struct isl_set *set, struct isl_basic_set *like)
3077 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3079 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3080 isl_basic_set_free(like);
3083 set = isl_set_cow(set);
3086 for (i = 0; i < set->n; ++i) {
3087 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3088 isl_basic_set_copy(like));
3092 isl_dim_free(set->dim);
3093 set->dim = isl_dim_copy(like->dim);
3096 isl_basic_set_free(like);
3099 isl_basic_set_free(like);
3104 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3108 map = isl_map_cow(map);
3111 map->dim = isl_dim_cow(map->dim);
3115 for (i = 1; i < map->n; ++i)
3116 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3118 for (i = 0; i < map->n; ++i) {
3119 map->p[i] = (struct isl_basic_map *)
3120 isl_basic_map_underlying_set(map->p[i]);
3125 map->dim = isl_dim_underlying(map->dim, 0);
3127 isl_dim_free(map->dim);
3128 map->dim = isl_dim_copy(map->p[0]->dim);
3132 return (struct isl_set *)map;
3138 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3140 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3143 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3145 struct isl_basic_set *domain;
3150 n_in = isl_basic_map_n_in(bmap);
3151 n_out = isl_basic_map_n_out(bmap);
3152 domain = isl_basic_set_from_basic_map(bmap);
3153 return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3156 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3158 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3161 struct isl_set *isl_map_range(struct isl_map *map)
3164 struct isl_set *set;
3168 if (isl_map_dim(map, isl_dim_in) == 0)
3169 return (isl_set *)map;
3171 map = isl_map_cow(map);
3175 set = (struct isl_set *) map;
3176 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3179 for (i = 0; i < map->n; ++i) {
3180 set->p[i] = isl_basic_map_range(map->p[i]);
3184 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3185 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3192 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3195 struct isl_map *map = NULL;
3197 set = isl_set_cow(set);
3200 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3201 map = (struct isl_map *)set;
3202 for (i = 0; i < set->n; ++i) {
3203 map->p[i] = isl_basic_map_from_basic_set(
3204 set->p[i], isl_dim_copy(dim));
3208 isl_dim_free(map->dim);
3217 struct isl_map *isl_map_from_range(struct isl_set *set)
3219 return (struct isl_map *)set;
3222 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3224 return isl_map_reverse(isl_map_from_range(set));;
3227 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3228 __isl_take isl_set *range)
3230 return isl_map_product(isl_map_from_domain(domain),
3231 isl_map_from_range(range));
3234 struct isl_set *isl_set_from_map(struct isl_map *map)
3237 struct isl_set *set = NULL;
3241 map = isl_map_cow(map);
3244 map->dim = isl_dim_cow(map->dim);
3247 map->dim->n_out += map->dim->n_in;
3249 set = (struct isl_set *)map;
3250 for (i = 0; i < map->n; ++i) {
3251 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3261 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3263 struct isl_map *map;
3267 isl_assert(dim->ctx, n >= 0, return NULL);
3268 map = isl_alloc(dim->ctx, struct isl_map,
3269 sizeof(struct isl_map) +
3270 (n - 1) * sizeof(struct isl_basic_map *));
3274 map->ctx = dim->ctx;
3275 isl_ctx_ref(map->ctx);
3287 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3288 unsigned nparam, unsigned in, unsigned out, int n,
3291 struct isl_map *map;
3292 struct isl_dim *dims;
3294 dims = isl_dim_alloc(ctx, nparam, in, out);
3298 map = isl_map_alloc_dim(dims, n, flags);
3302 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3304 struct isl_basic_map *bmap;
3305 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3306 bmap = isl_basic_map_set_to_empty(bmap);
3310 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3312 struct isl_basic_set *bset;
3313 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3314 bset = isl_basic_set_set_to_empty(bset);
3318 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3320 struct isl_basic_map *bmap;
3323 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3324 bmap = isl_basic_map_set_to_empty(bmap);
3328 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3330 struct isl_basic_map *bmap;
3333 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3334 bmap = isl_basic_map_set_to_empty(bmap);
3338 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3340 struct isl_basic_set *bset;
3343 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3344 bset = isl_basic_set_set_to_empty(bset);
3348 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3350 struct isl_basic_map *bmap;
3351 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
3355 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
3357 struct isl_basic_set *bset;
3358 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
3362 __isl_give isl_basic_map *isl_basic_map_universe_like(
3363 __isl_keep isl_basic_map *model)
3367 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3370 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3374 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3377 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3378 __isl_keep isl_set *model)
3382 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3385 struct isl_map *isl_map_empty(struct isl_dim *dim)
3387 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3390 struct isl_map *isl_map_empty_like(struct isl_map *model)
3394 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3397 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
3401 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3404 struct isl_set *isl_set_empty(struct isl_dim *dim)
3406 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3409 struct isl_set *isl_set_empty_like(struct isl_set *model)
3413 return isl_set_empty(isl_dim_copy(model->dim));
3416 struct isl_map *isl_map_universe(struct isl_dim *dim)
3418 struct isl_map *map;
3421 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3422 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
3426 struct isl_set *isl_set_universe(struct isl_dim *dim)
3428 struct isl_set *set;
3431 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3432 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
3436 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3440 return isl_set_universe(isl_dim_copy(model->dim));
3443 struct isl_map *isl_map_dup(struct isl_map *map)
3446 struct isl_map *dup;
3450 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3451 for (i = 0; i < map->n; ++i)
3452 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
3456 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
3457 __isl_take isl_basic_map *bmap)
3461 if (isl_basic_map_fast_is_empty(bmap)) {
3462 isl_basic_map_free(bmap);
3465 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3466 isl_assert(map->ctx, map->n < map->size, goto error);
3467 map->p[map->n] = bmap;
3469 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3475 isl_basic_map_free(bmap);
3479 void isl_map_free(struct isl_map *map)
3489 isl_ctx_deref(map->ctx);
3490 for (i = 0; i < map->n; ++i)
3491 isl_basic_map_free(map->p[i]);
3492 isl_dim_free(map->dim);
3496 struct isl_map *isl_map_extend(struct isl_map *base,
3497 unsigned nparam, unsigned n_in, unsigned n_out)
3501 base = isl_map_cow(base);
3505 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3508 for (i = 0; i < base->n; ++i) {
3509 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3510 isl_dim_copy(base->dim), 0, 0, 0);
3520 struct isl_set *isl_set_extend(struct isl_set *base,
3521 unsigned nparam, unsigned dim)
3523 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3527 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3528 struct isl_basic_map *bmap, unsigned pos, int value)
3532 bmap = isl_basic_map_cow(bmap);
3533 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3534 j = isl_basic_map_alloc_equality(bmap);
3537 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3538 isl_int_set_si(bmap->eq[j][pos], -1);
3539 isl_int_set_si(bmap->eq[j][0], value);
3540 bmap = isl_basic_map_simplify(bmap);
3541 return isl_basic_map_finalize(bmap);
3543 isl_basic_map_free(bmap);
3547 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3548 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3552 bmap = isl_basic_map_cow(bmap);
3553 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3554 j = isl_basic_map_alloc_equality(bmap);
3557 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3558 isl_int_set_si(bmap->eq[j][pos], -1);
3559 isl_int_set(bmap->eq[j][0], value);
3560 bmap = isl_basic_map_simplify(bmap);
3561 return isl_basic_map_finalize(bmap);
3563 isl_basic_map_free(bmap);
3567 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3568 enum isl_dim_type type, unsigned pos, int value)
3572 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3573 return isl_basic_map_fix_pos_si(bmap,
3574 isl_basic_map_offset(bmap, type) + pos, value);
3576 isl_basic_map_free(bmap);
3580 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3581 enum isl_dim_type type, unsigned pos, isl_int value)
3585 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3586 return isl_basic_map_fix_pos(bmap,
3587 isl_basic_map_offset(bmap, type) + pos, value);
3589 isl_basic_map_free(bmap);
3593 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3594 enum isl_dim_type type, unsigned pos, int value)
3596 return (struct isl_basic_set *)
3597 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3601 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3602 enum isl_dim_type type, unsigned pos, isl_int value)
3604 return (struct isl_basic_set *)
3605 isl_basic_map_fix((struct isl_basic_map *)bset,
3609 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3610 unsigned input, int value)
3612 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3615 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3616 unsigned dim, int value)
3618 return (struct isl_basic_set *)
3619 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3620 isl_dim_set, dim, value);
3623 struct isl_map *isl_map_fix_si(struct isl_map *map,
3624 enum isl_dim_type type, unsigned pos, int value)
3628 map = isl_map_cow(map);
3632 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3633 for (i = 0; i < map->n; ++i) {
3634 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3638 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3645 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
3646 enum isl_dim_type type, unsigned pos, int value)
3648 return (struct isl_set *)
3649 isl_map_fix_si((struct isl_map *)set, type, pos, value);
3652 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
3653 enum isl_dim_type type, unsigned pos, isl_int value)
3657 map = isl_map_cow(map);
3661 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3662 for (i = 0; i < map->n; ++i) {
3663 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
3667 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3674 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
3675 enum isl_dim_type type, unsigned pos, isl_int value)
3677 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
3680 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3681 unsigned input, int value)
3683 return isl_map_fix_si(map, isl_dim_in, input, value);
3686 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3688 return (struct isl_set *)
3689 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3692 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
3693 __isl_take isl_basic_map *bmap,
3694 enum isl_dim_type type, unsigned pos, int value)
3700 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3701 pos += isl_basic_map_offset(bmap, type);
3702 bmap = isl_basic_map_cow(bmap);
3703 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
3704 j = isl_basic_map_alloc_inequality(bmap);
3707 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
3708 isl_int_set_si(bmap->ineq[j][pos], 1);
3709 isl_int_set_si(bmap->ineq[j][0], -value);
3710 bmap = isl_basic_map_simplify(bmap);
3711 return isl_basic_map_finalize(bmap);
3713 isl_basic_map_free(bmap);
3717 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3718 unsigned dim, isl_int value)
3722 bset = isl_basic_set_cow(bset);
3723 bset = isl_basic_set_extend_constraints(bset, 0, 1);
3724 j = isl_basic_set_alloc_inequality(bset);
3727 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3728 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3729 isl_int_neg(bset->ineq[j][0], value);
3730 bset = isl_basic_set_simplify(bset);
3731 return isl_basic_set_finalize(bset);
3733 isl_basic_set_free(bset);
3737 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
3738 enum isl_dim_type type, unsigned pos, int value)
3742 map = isl_map_cow(map);
3746 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3747 for (i = 0; i < map->n; ++i) {
3748 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
3753 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3760 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
3761 enum isl_dim_type type, unsigned pos, int value)
3763 return (struct isl_set *)
3764 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
3767 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3772 set = isl_set_cow(set);
3776 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3777 for (i = 0; i < set->n; ++i) {
3778 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3788 struct isl_map *isl_map_reverse(struct isl_map *map)
3792 map = isl_map_cow(map);
3796 map->dim = isl_dim_reverse(map->dim);
3799 for (i = 0; i < map->n; ++i) {
3800 map->p[i] = isl_basic_map_reverse(map->p[i]);
3804 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3811 static struct isl_map *isl_basic_map_partial_lexopt(
3812 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3813 struct isl_set **empty, int max)
3817 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
3818 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3820 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3822 isl_basic_map_free(bmap);
3823 isl_basic_set_free(dom);
3829 struct isl_map *isl_basic_map_partial_lexmax(
3830 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3831 struct isl_set **empty)
3833 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
3836 struct isl_map *isl_basic_map_partial_lexmin(
3837 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3838 struct isl_set **empty)
3840 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3843 struct isl_set *isl_basic_set_partial_lexmin(
3844 struct isl_basic_set *bset, struct isl_basic_set *dom,
3845 struct isl_set **empty)
3847 return (struct isl_set *)
3848 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3852 struct isl_set *isl_basic_set_partial_lexmax(
3853 struct isl_basic_set *bset, struct isl_basic_set *dom,
3854 struct isl_set **empty)
3856 return (struct isl_set *)
3857 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3861 /* Given a basic map "bmap", compute the lexicograhically minimal
3862 * (or maximal) image element for each domain element in dom.
3863 * Set *empty to those elements in dom that do not have an image element.
3865 * We first make sure the basic sets in dom are disjoint and then
3866 * simply collect the results over each of the basic sets separately.
3867 * We could probably improve the efficiency a bit by moving the union
3868 * domain down into the parametric integer programming.
3870 static __isl_give isl_map *basic_map_partial_lexopt(
3871 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
3872 __isl_give isl_set **empty, int max)
3875 struct isl_map *res;
3877 dom = isl_set_make_disjoint(dom);
3881 if (isl_set_fast_is_empty(dom)) {
3882 res = isl_map_empty_like_basic_map(bmap);
3883 *empty = isl_set_empty_like(dom);
3885 isl_basic_map_free(bmap);
3889 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
3890 isl_basic_set_copy(dom->p[0]), empty, max);
3892 for (i = 1; i < dom->n; ++i) {
3893 struct isl_map *res_i;
3894 struct isl_set *empty_i;
3896 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
3897 isl_basic_set_copy(dom->p[i]), &empty_i, max);
3899 res = isl_map_union_disjoint(res, res_i);
3900 *empty = isl_set_union_disjoint(*empty, empty_i);
3904 isl_basic_map_free(bmap);
3909 isl_basic_map_free(bmap);
3913 /* Given a map "map", compute the lexicograhically minimal
3914 * (or maximal) image element for each domain element in dom.
3915 * Set *empty to those elements in dom that do not have an image element.
3917 * We first compute the lexicographically minimal or maximal element
3918 * in the first basic map. This results in a partial solution "res"
3919 * and a subset "todo" of dom that still need to be handled.
3920 * We then consider each of the remaining maps in "map" and successively
3921 * improve both "res" and "todo".
3923 * Let res^k and todo^k be the results after k steps and let i = k + 1.
3924 * Assume we are computing the lexicographical maximum.
3925 * We first intersect basic map i with a relation that maps elements
3926 * to elements that are lexicographically larger than the image elements
3927 * in res^k and the compute the maximum image element of this intersection.
3928 * The result ("better") corresponds to those image elements in basic map i
3929 * that are better than what we had before. The remainder ("keep") are the
3930 * domain elements for which the image element in res_k was better.
3931 * We also compute the lexicographical maximum of basic map i in todo^k.
3932 * res^i is the result of the operation + better + those elements in
3933 * res^k that we should keep
3934 * todo^i is the remainder of the maximum operation on todo^k.
3936 static __isl_give isl_map *isl_map_partial_lexopt(
3937 __isl_take isl_map *map, __isl_take isl_set *dom,
3938 __isl_give isl_set **empty, int max)
3941 struct isl_map *res;
3942 struct isl_set *todo;
3947 if (isl_map_fast_is_empty(map)) {
3955 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
3956 isl_set_copy(dom), &todo, max);
3958 for (i = 1; i < map->n; ++i) {
3960 struct isl_map *better;
3961 struct isl_set *keep;
3962 struct isl_map *res_i;
3963 struct isl_set *todo_i;
3964 struct isl_dim *dim = isl_map_get_dim(res);
3966 dim = isl_dim_range(dim);
3968 lt = isl_map_lex_lt(dim);
3970 lt = isl_map_lex_gt(dim);
3971 lt = isl_map_apply_range(isl_map_copy(res), lt);
3972 lt = isl_map_intersect(lt,
3973 isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
3974 better = isl_map_partial_lexopt(lt,
3975 isl_map_domain(isl_map_copy(res)),
3978 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
3979 todo, &todo_i, max);
3981 res = isl_map_intersect_domain(res, keep);
3982 res = isl_map_union_disjoint(res, res_i);
3983 res = isl_map_union_disjoint(res, better);
4004 __isl_give isl_map *isl_map_partial_lexmax(
4005 __isl_take isl_map *map, __isl_take isl_set *dom,
4006 __isl_give isl_set **empty)
4008 return isl_map_partial_lexopt(map, dom, empty, 1);
4011 __isl_give isl_map *isl_map_partial_lexmin(
4012 __isl_take isl_map *map, __isl_take isl_set *dom,
4013 __isl_give isl_set **empty)
4015 return isl_map_partial_lexopt(map, dom, empty, 0);
4018 __isl_give isl_set *isl_set_partial_lexmin(
4019 __isl_take isl_set *set, __isl_take isl_set *dom,
4020 __isl_give isl_set **empty)
4022 return (struct isl_set *)
4023 isl_map_partial_lexmin((struct isl_map *)set,
4027 __isl_give isl_set *isl_set_partial_lexmax(
4028 __isl_take isl_set *set, __isl_take isl_set *dom,
4029 __isl_give isl_set **empty)
4031 return (struct isl_set *)
4032 isl_map_partial_lexmax((struct isl_map *)set,
4036 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4038 struct isl_basic_set *dom = NULL;
4039 struct isl_dim *dom_dim;
4043 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4044 dom = isl_basic_set_universe(dom_dim);
4045 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4047 isl_basic_map_free(bmap);
4051 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4053 return isl_basic_map_lexopt(bmap, 0);
4056 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4058 return isl_basic_map_lexopt(bmap, 1);
4061 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4063 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4066 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4068 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4071 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4073 struct isl_set *dom = NULL;
4074 struct isl_dim *dom_dim;
4078 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4079 dom = isl_set_universe(dom_dim);
4080 return isl_map_partial_lexopt(map, dom, NULL, max);
4086 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4088 return isl_map_lexopt(map, 0);
4091 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4093 return isl_map_lexopt(map, 1);
4096 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4098 return (isl_set *)isl_map_lexmin((isl_map *)set);
4101 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4103 return (isl_set *)isl_map_lexmax((isl_map *)set);
4106 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
4107 struct isl_dim *dim)
4114 for (i = 0; i < map->n; ++i) {
4115 isl_dim_free(map->p[i]->dim);
4116 map->p[i]->dim = isl_dim_copy(dim);
4118 isl_dim_free(map->dim);
4128 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
4129 struct isl_dim *dim)
4131 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
4134 /* Apply a preimage specified by "mat" on the parameters of "bset".
4135 * bset is assumed to have only parameters and divs.
4137 static struct isl_basic_set *basic_set_parameter_preimage(
4138 struct isl_basic_set *bset, struct isl_mat *mat)
4145 bset->dim = isl_dim_cow(bset->dim);
4149 nparam = isl_basic_set_dim(bset, isl_dim_param);
4151 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4153 bset->dim->nparam = 0;
4154 bset->dim->n_out = nparam;
4155 bset = isl_basic_set_preimage(bset, mat);
4157 bset->dim->nparam = bset->dim->n_out;
4158 bset->dim->n_out = 0;
4163 isl_basic_set_free(bset);
4167 /* Apply a preimage specified by "mat" on the parameters of "set".
4168 * set is assumed to have only parameters and divs.
4170 static struct isl_set *set_parameter_preimage(
4171 struct isl_set *set, struct isl_mat *mat)
4173 struct isl_dim *dim = NULL;
4179 dim = isl_dim_copy(set->dim);
4180 dim = isl_dim_cow(dim);
4184 nparam = isl_set_dim(set, isl_dim_param);
4186 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4189 dim->n_out = nparam;
4190 isl_set_reset_dim(set, dim);
4191 set = isl_set_preimage(set, mat);
4194 dim = isl_dim_copy(set->dim);
4195 dim = isl_dim_cow(dim);
4198 dim->nparam = dim->n_out;
4200 isl_set_reset_dim(set, dim);
4210 /* Intersect the basic set "bset" with the affine space specified by the
4211 * equalities in "eq".
4213 static struct isl_basic_set *basic_set_append_equalities(
4214 struct isl_basic_set *bset, struct isl_mat *eq)
4222 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4227 len = 1 + isl_dim_total(bset->dim) + bset->extra;
4228 for (i = 0; i < eq->n_row; ++i) {
4229 k = isl_basic_set_alloc_equality(bset);
4232 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4233 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4240 isl_basic_set_free(bset);
4244 /* Intersect the set "set" with the affine space specified by the
4245 * equalities in "eq".
4247 static struct isl_set *set_append_equalities(struct isl_set *set,
4255 for (i = 0; i < set->n; ++i) {
4256 set->p[i] = basic_set_append_equalities(set->p[i],
4269 /* Project the given basic set onto its parameter domain, possibly introducing
4270 * new, explicit, existential variables in the constraints.
4271 * The input has parameters and (possibly implicit) existential variables.
4272 * The output has the same parameters, but only
4273 * explicit existentially quantified variables.
4275 * The actual projection is performed by pip, but pip doesn't seem
4276 * to like equalities very much, so we first remove the equalities
4277 * among the parameters by performing a variable compression on
4278 * the parameters. Afterward, an inverse transformation is performed
4279 * and the equalities among the parameters are inserted back in.
4281 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4285 struct isl_mat *T, *T2;
4286 struct isl_set *set;
4287 unsigned nparam, n_div;
4289 bset = isl_basic_set_cow(bset);
4293 if (bset->n_eq == 0)
4294 return isl_basic_set_lexmin(bset);
4296 isl_basic_set_gauss(bset, NULL);
4298 nparam = isl_basic_set_dim(bset, isl_dim_param);
4299 n_div = isl_basic_set_dim(bset, isl_dim_div);
4301 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4302 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4305 if (i == bset->n_eq)
4306 return isl_basic_set_lexmin(bset);
4308 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4310 eq = isl_mat_cow(eq);
4311 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4312 bset = basic_set_parameter_preimage(bset, T);
4314 set = isl_basic_set_lexmin(bset);
4315 set = set_parameter_preimage(set, T2);
4316 set = set_append_equalities(set, eq);
4320 /* Compute an explicit representation for all the existentially
4321 * quantified variables.
4322 * The input and output dimensions are first turned into parameters.
4323 * compute_divs then returns a map with the same parameters and
4324 * no input or output dimensions and the dimension specification
4325 * is reset to that of the input.
4327 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4329 struct isl_basic_set *bset;
4330 struct isl_set *set;
4331 struct isl_map *map;
4332 struct isl_dim *dim, *orig_dim = NULL;
4337 bmap = isl_basic_map_cow(bmap);
4341 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4342 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4343 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4344 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4348 orig_dim = bmap->dim;
4350 bset = (struct isl_basic_set *)bmap;
4352 set = parameter_compute_divs(bset);
4353 map = (struct isl_map *)set;
4354 map = isl_map_reset_dim(map, orig_dim);
4358 isl_basic_map_free(bmap);
4362 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4370 off = isl_dim_total(bmap->dim);
4371 for (i = 0; i < bmap->n_div; ++i) {
4372 if (isl_int_is_zero(bmap->div[i][0]))
4374 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
4380 static int map_divs_known(__isl_keep isl_map *map)
4387 for (i = 0; i < map->n; ++i) {
4388 int known = basic_map_divs_known(map->p[i]);
4396 /* If bmap contains any unknown divs, then compute explicit
4397 * expressions for them. However, this computation may be
4398 * quite expensive, so first try to remove divs that aren't
4401 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
4405 struct isl_map *map;
4407 known = basic_map_divs_known(bmap);
4411 return isl_map_from_basic_map(bmap);
4413 bmap = isl_basic_map_drop_redundant_divs(bmap);
4415 known = basic_map_divs_known(bmap);
4419 return isl_map_from_basic_map(bmap);
4421 map = compute_divs(bmap);
4424 isl_basic_map_free(bmap);
4428 struct isl_map *isl_map_compute_divs(struct isl_map *map)
4432 struct isl_map *res;
4439 known = map_divs_known(map);
4447 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
4448 for (i = 1 ; i < map->n; ++i) {
4450 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
4451 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4452 res = isl_map_union_disjoint(res, r2);
4454 res = isl_map_union(res, r2);
4461 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
4463 return (struct isl_set *)
4464 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
4467 struct isl_set *isl_set_compute_divs(struct isl_set *set)
4469 return (struct isl_set *)
4470 isl_map_compute_divs((struct isl_map *)set);
4473 struct isl_set *isl_map_domain(struct isl_map *map)
4476 struct isl_set *set;
4481 map = isl_map_cow(map);
4485 set = (struct isl_set *)map;
4486 set->dim = isl_dim_domain(set->dim);
4489 for (i = 0; i < map->n; ++i) {
4490 set->p[i] = isl_basic_map_domain(map->p[i]);
4494 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4495 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4502 struct isl_map *isl_map_union_disjoint(
4503 struct isl_map *map1, struct isl_map *map2)
4507 struct isl_map *map = NULL;
4521 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4523 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4524 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4525 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4527 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
4528 map1->n + map2->n, flags);
4531 for (i = 0; i < map1->n; ++i) {
4532 map = isl_map_add_basic_map(map,
4533 isl_basic_map_copy(map1->p[i]));
4537 for (i = 0; i < map2->n; ++i) {
4538 map = isl_map_add_basic_map(map,
4539 isl_basic_map_copy(map2->p[i]));
4553 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
4555 map1 = isl_map_union_disjoint(map1, map2);
4559 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
4563 struct isl_set *isl_set_union_disjoint(
4564 struct isl_set *set1, struct isl_set *set2)
4566 return (struct isl_set *)
4567 isl_map_union_disjoint(
4568 (struct isl_map *)set1, (struct isl_map *)set2);
4571 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
4573 return (struct isl_set *)
4574 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
4577 struct isl_map *isl_map_intersect_range(
4578 struct isl_map *map, struct isl_set *set)
4581 struct isl_map *result;
4587 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
4588 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
4589 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4591 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
4592 map->n * set->n, flags);
4595 for (i = 0; i < map->n; ++i)
4596 for (j = 0; j < set->n; ++j) {
4597 result = isl_map_add_basic_map(result,
4598 isl_basic_map_intersect_range(
4599 isl_basic_map_copy(map->p[i]),
4600 isl_basic_set_copy(set->p[j])));
4613 struct isl_map *isl_map_intersect_domain(
4614 struct isl_map *map, struct isl_set *set)
4616 return isl_map_reverse(
4617 isl_map_intersect_range(isl_map_reverse(map), set));
4620 struct isl_map *isl_map_apply_domain(
4621 struct isl_map *map1, struct isl_map *map2)
4625 map1 = isl_map_reverse(map1);
4626 map1 = isl_map_apply_range(map1, map2);
4627 return isl_map_reverse(map1);
4634 struct isl_map *isl_map_apply_range(
4635 struct isl_map *map1, struct isl_map *map2)
4637 struct isl_dim *dim_result;
4638 struct isl_map *result;
4644 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
4645 isl_dim_copy(map2->dim));
4647 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
4650 for (i = 0; i < map1->n; ++i)
4651 for (j = 0; j < map2->n; ++j) {
4652 result = isl_map_add_basic_map(result,
4653 isl_basic_map_apply_range(
4654 isl_basic_map_copy(map1->p[i]),
4655 isl_basic_map_copy(map2->p[j])));
4661 if (result && result->n <= 1)
4662 ISL_F_SET(result, ISL_MAP_DISJOINT);
4671 * returns range - domain
4673 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
4675 struct isl_basic_set *bset;
4682 dim = isl_basic_map_n_in(bmap);
4683 nparam = isl_basic_map_n_param(bmap);
4684 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
4685 bset = isl_basic_set_from_basic_map(bmap);
4686 bset = isl_basic_set_cow(bset);
4687 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
4688 bset = isl_basic_set_swap_vars(bset, 2*dim);
4689 for (i = 0; i < dim; ++i) {
4690 int j = isl_basic_map_alloc_equality(
4691 (struct isl_basic_map *)bset);
4694 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
4695 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
4696 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
4697 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
4699 return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
4701 isl_basic_map_free(bmap);
4706 * returns range - domain
4708 struct isl_set *isl_map_deltas(struct isl_map *map)
4711 struct isl_set *result;
4716 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
4717 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
4718 isl_map_n_in(map), map->n, map->flags);
4721 for (i = 0; i < map->n; ++i)
4722 result = isl_set_add_basic_set(result,
4723 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
4731 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
4733 struct isl_basic_map *bmap;
4741 nparam = dims->nparam;
4743 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
4747 for (i = 0; i < dim; ++i) {
4748 int j = isl_basic_map_alloc_equality(bmap);
4751 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
4752 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
4753 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
4755 return isl_basic_map_finalize(bmap);
4757 isl_basic_map_free(bmap);
4761 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
4763 struct isl_dim *dim = isl_dim_map(set_dim);
4766 return basic_map_identity(dim);
4769 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
4771 if (!model || !model->dim)
4773 isl_assert(model->ctx,
4774 model->dim->n_in == model->dim->n_out, return NULL);
4775 return basic_map_identity(isl_dim_copy(model->dim));
4778 static struct isl_map *map_identity(struct isl_dim *dim)
4780 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
4781 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
4784 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
4786 struct isl_dim *dim = isl_dim_map(set_dim);
4789 return map_identity(dim);
4792 struct isl_map *isl_map_identity_like(struct isl_map *model)
4794 if (!model || !model->dim)
4796 isl_assert(model->ctx,
4797 model->dim->n_in == model->dim->n_out, return NULL);
4798 return map_identity(isl_dim_copy(model->dim));
4801 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
4803 if (!model || !model->dim)
4805 isl_assert(model->ctx,
4806 model->dim->n_in == model->dim->n_out, return NULL);
4807 return map_identity(isl_dim_copy(model->dim));
4810 /* Construct a basic set with all set dimensions having only non-negative
4813 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
4818 struct isl_basic_set *bset;
4822 nparam = dims->nparam;
4824 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
4827 for (i = 0; i < dim; ++i) {
4828 int k = isl_basic_set_alloc_inequality(bset);
4831 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
4832 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
4836 isl_basic_set_free(bset);
4840 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
4841 enum isl_dim_type type, unsigned first, unsigned n)
4844 isl_basic_set *nonneg = NULL;
4845 isl_basic_set *neg = NULL;
4852 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
4854 for (i = 0; i < n; ++i) {
4858 nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
4859 k = isl_basic_set_alloc_inequality(nonneg);
4862 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
4863 isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
4865 neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
4866 k = isl_basic_set_alloc_inequality(neg);
4869 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
4870 isl_int_set_si(neg->ineq[k][0], -1);
4871 isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
4873 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
4878 isl_basic_set_free(nonneg);
4879 isl_basic_set_free(neg);
4884 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
4886 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
4889 int isl_basic_map_is_subset(
4890 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4893 struct isl_map *map1;
4894 struct isl_map *map2;
4896 if (!bmap1 || !bmap2)
4899 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
4900 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
4902 is_subset = isl_map_is_subset(map1, map2);
4910 int isl_basic_map_is_equal(
4911 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4915 if (!bmap1 || !bmap2)
4917 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4920 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4924 int isl_basic_set_is_equal(
4925 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4927 return isl_basic_map_is_equal(
4928 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
4931 int isl_map_is_empty(struct isl_map *map)
4938 for (i = 0; i < map->n; ++i) {
4939 is_empty = isl_basic_map_is_empty(map->p[i]);
4948 int isl_map_fast_is_empty(struct isl_map *map)
4953 int isl_set_fast_is_empty(struct isl_set *set)
4958 int isl_set_is_empty(struct isl_set *set)
4960 return isl_map_is_empty((struct isl_map *)set);
4963 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
4969 is_subset = isl_map_is_subset(map1, map2);
4972 is_subset = isl_map_is_subset(map2, map1);
4976 int isl_basic_map_is_strict_subset(
4977 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4981 if (!bmap1 || !bmap2)
4983 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4986 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4987 if (is_subset == -1)
4992 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
4998 is_subset = isl_map_is_subset(map1, map2);
5001 is_subset = isl_map_is_subset(map2, map1);
5002 if (is_subset == -1)
5007 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5009 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5012 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5016 return bmap->n_eq == 0 && bmap->n_ineq == 0;
5019 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5023 return bset->n_eq == 0 && bset->n_ineq == 0;
5026 int isl_map_fast_is_universe(__isl_keep isl_map *map)
5031 return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
5034 int isl_set_fast_is_universe(__isl_keep isl_set *set)
5036 return isl_map_fast_is_universe((isl_map *) set);
5039 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
5041 struct isl_basic_set *bset = NULL;
5042 struct isl_vec *sample = NULL;
5049 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
5052 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
5053 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
5054 copy = isl_basic_map_convex_hull(copy);
5055 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
5056 isl_basic_map_free(copy);
5060 total = 1 + isl_basic_map_total_dim(bmap);
5061 if (bmap->sample && bmap->sample->size == total) {
5062 int contains = isl_basic_map_contains(bmap, bmap->sample);
5068 isl_vec_free(bmap->sample);
5069 bmap->sample = NULL;
5070 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
5073 sample = isl_basic_set_sample_vec(bset);
5076 empty = sample->size == 0;
5077 isl_vec_free(bmap->sample);
5078 bmap->sample = sample;
5080 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
5085 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
5089 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
5092 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
5096 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
5099 int isl_basic_set_is_empty(struct isl_basic_set *bset)
5101 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
5104 struct isl_map *isl_basic_map_union(
5105 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5107 struct isl_map *map;
5108 if (!bmap1 || !bmap2)
5111 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
5113 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
5116 map = isl_map_add_basic_map(map, bmap1);
5117 map = isl_map_add_basic_map(map, bmap2);
5120 isl_basic_map_free(bmap1);
5121 isl_basic_map_free(bmap2);
5125 struct isl_set *isl_basic_set_union(
5126 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5128 return (struct isl_set *)isl_basic_map_union(
5129 (struct isl_basic_map *)bset1,
5130 (struct isl_basic_map *)bset2);
5133 /* Order divs such that any div only depends on previous divs */
5134 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
5137 unsigned off = isl_dim_total(bmap->dim);
5139 for (i = 0; i < bmap->n_div; ++i) {
5141 if (isl_int_is_zero(bmap->div[i][0]))
5143 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
5147 isl_basic_map_swap_div(bmap, i, i + pos);
5153 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
5155 return (struct isl_basic_set *)
5156 isl_basic_map_order_divs((struct isl_basic_map *)bset);
5159 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
5166 for (i = 0; i < map->n; ++i) {
5167 map->p[i] = isl_basic_map_order_divs(map->p[i]);
5178 /* Look for a div in dst that corresponds to the div "div" in src.
5179 * The divs before "div" in src and dst are assumed to be the same.
5181 * Returns -1 if no corresponding div was found and the position
5182 * of the corresponding div in dst otherwise.
5184 static int find_div(struct isl_basic_map *dst,
5185 struct isl_basic_map *src, unsigned div)
5189 unsigned total = isl_dim_total(src->dim);
5191 isl_assert(dst->ctx, div <= dst->n_div, return -1);
5192 for (i = div; i < dst->n_div; ++i)
5193 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
5194 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
5195 dst->n_div - div) == -1)
5200 struct isl_basic_map *isl_basic_map_align_divs(
5201 struct isl_basic_map *dst, struct isl_basic_map *src)
5204 unsigned total = isl_dim_total(src->dim);
5209 if (src->n_div == 0)
5212 for (i = 0; i < src->n_div; ++i)
5213 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
5215 src = isl_basic_map_order_divs(src);
5216 dst = isl_basic_map_cow(dst);
5217 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
5218 src->n_div, 0, 2 * src->n_div);
5221 for (i = 0; i < src->n_div; ++i) {
5222 int j = find_div(dst, src, i);
5224 j = isl_basic_map_alloc_div(dst);
5227 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
5228 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
5229 if (isl_basic_map_add_div_constraints(dst, j) < 0)
5233 isl_basic_map_swap_div(dst, i, j);
5237 isl_basic_map_free(dst);
5241 struct isl_basic_set *isl_basic_set_align_divs(
5242 struct isl_basic_set *dst, struct isl_basic_set *src)
5244 return (struct isl_basic_set *)isl_basic_map_align_divs(
5245 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
5248 struct isl_map *isl_map_align_divs(struct isl_map *map)
5256 map = isl_map_compute_divs(map);
5257 map = isl_map_cow(map);
5261 for (i = 1; i < map->n; ++i)
5262 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5263 for (i = 1; i < map->n; ++i)
5264 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5266 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5270 struct isl_set *isl_set_align_divs(struct isl_set *set)
5272 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5275 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5279 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5280 map = isl_map_intersect_domain(map, set);
5281 set = isl_map_range(map);
5289 /* There is no need to cow as removing empty parts doesn't change
5290 * the meaning of the set.
5292 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5299 for (i = map->n-1; i >= 0; --i) {
5300 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5302 isl_basic_map_free(map->p[i]);
5303 if (i != map->n-1) {
5304 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5305 map->p[i] = map->p[map->n-1];
5313 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
5315 return (struct isl_set *)
5316 isl_map_remove_empty_parts((struct isl_map *)set);
5319 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
5321 struct isl_basic_map *bmap;
5322 if (!map || map->n == 0)
5324 bmap = map->p[map->n-1];
5325 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
5326 return isl_basic_map_copy(bmap);
5329 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
5331 return (struct isl_basic_set *)
5332 isl_map_copy_basic_map((struct isl_map *)set);
5335 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
5336 __isl_keep isl_basic_map *bmap)
5342 for (i = map->n-1; i >= 0; --i) {
5343 if (map->p[i] != bmap)
5345 map = isl_map_cow(map);
5348 isl_basic_map_free(map->p[i]);
5349 if (i != map->n-1) {
5350 ISL_F_CLR(map, ISL_SET_NORMALIZED);
5351 map->p[i] = map->p[map->n-1];
5362 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
5363 struct isl_basic_set *bset)
5365 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
5366 (struct isl_basic_map *)bset);
5369 /* Given two basic sets bset1 and bset2, compute the maximal difference
5370 * between the values of dimension pos in bset1 and those in bset2
5371 * for any common value of the parameters and dimensions preceding pos.
5373 static enum isl_lp_result basic_set_maximal_difference_at(
5374 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
5375 int pos, isl_int *opt)
5377 struct isl_dim *dims;
5378 struct isl_basic_map *bmap1 = NULL;
5379 struct isl_basic_map *bmap2 = NULL;
5380 struct isl_ctx *ctx;
5381 struct isl_vec *obj;
5384 unsigned dim1, dim2;
5385 enum isl_lp_result res;
5387 if (!bset1 || !bset2)
5388 return isl_lp_error;
5390 nparam = isl_basic_set_n_param(bset1);
5391 dim1 = isl_basic_set_n_dim(bset1);
5392 dim2 = isl_basic_set_n_dim(bset2);
5393 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
5394 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
5395 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
5396 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
5397 if (!bmap1 || !bmap2)
5399 bmap1 = isl_basic_map_cow(bmap1);
5400 bmap1 = isl_basic_map_extend(bmap1, nparam,
5401 pos, (dim1 - pos) + (dim2 - pos),
5402 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
5403 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
5406 total = isl_basic_map_total_dim(bmap1);
5408 obj = isl_vec_alloc(ctx, 1 + total);
5409 isl_seq_clr(obj->block.data, 1 + total);
5410 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
5411 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
5414 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
5416 isl_basic_map_free(bmap1);
5420 isl_basic_map_free(bmap1);
5421 isl_basic_map_free(bmap2);
5422 return isl_lp_error;
5425 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5426 * for any common value of the parameters and dimensions preceding pos
5427 * in both basic sets, the values of dimension pos in bset1 are
5428 * smaller or larger than those in bset2.
5431 * 1 if bset1 follows bset2
5432 * -1 if bset1 precedes bset2
5433 * 0 if bset1 and bset2 are incomparable
5434 * -2 if some error occurred.
5436 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
5437 struct isl_basic_set *bset2, int pos)
5440 enum isl_lp_result res;
5445 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5447 if (res == isl_lp_empty)
5449 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5450 res == isl_lp_unbounded)
5452 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5461 /* Given two basic sets bset1 and bset2, check whether
5462 * for any common value of the parameters and dimensions preceding pos
5463 * there is a value of dimension pos in bset1 that is larger
5464 * than a value of the same dimension in bset2.
5467 * 1 if there exists such a pair
5468 * 0 if there is no such pair, but there is a pair of equal values
5470 * -2 if some error occurred.
5472 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
5473 __isl_keep isl_basic_set *bset2, int pos)
5476 enum isl_lp_result res;
5481 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5483 if (res == isl_lp_empty)
5485 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5486 res == isl_lp_unbounded)
5488 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5490 else if (res == isl_lp_ok)
5499 /* Given two sets set1 and set2, check whether
5500 * for any common value of the parameters and dimensions preceding pos
5501 * there is a value of dimension pos in set1 that is larger
5502 * than a value of the same dimension in set2.
5505 * 1 if there exists such a pair
5506 * 0 if there is no such pair, but there is a pair of equal values
5508 * -2 if some error occurred.
5510 int isl_set_follows_at(__isl_keep isl_set *set1,
5511 __isl_keep isl_set *set2, int pos)
5519 for (i = 0; i < set1->n; ++i)
5520 for (j = 0; j < set2->n; ++j) {
5522 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
5523 if (f == 1 || f == -2)
5532 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
5533 unsigned pos, isl_int *val)
5541 total = isl_basic_map_total_dim(bmap);
5542 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5543 for (; d+1 > pos; --d)
5544 if (!isl_int_is_zero(bmap->eq[i][1+d]))
5548 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5550 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5552 if (!isl_int_is_one(bmap->eq[i][1+d]))
5555 isl_int_neg(*val, bmap->eq[i][0]);
5561 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5562 unsigned pos, isl_int *val)
5574 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
5577 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
5578 for (i = 1; fixed == 1 && i < map->n; ++i) {
5579 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
5580 if (fixed == 1 && isl_int_ne(tmp, v))
5584 isl_int_set(*val, v);
5590 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5591 unsigned pos, isl_int *val)
5593 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
5597 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
5600 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
5603 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
5604 enum isl_dim_type type, unsigned pos, isl_int *val)
5606 if (pos >= isl_basic_map_dim(bmap, type))
5608 return isl_basic_map_fast_has_fixed_var(bmap,
5609 isl_basic_map_offset(bmap, type) - 1 + pos, val);
5612 int isl_map_fast_is_fixed(struct isl_map *map,
5613 enum isl_dim_type type, unsigned pos, isl_int *val)
5615 if (pos >= isl_map_dim(map, type))
5617 return isl_map_fast_has_fixed_var(map,
5618 map_offset(map, type) - 1 + pos, val);
5621 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5622 * then return this fixed value in *val.
5624 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
5627 return isl_basic_set_fast_has_fixed_var(bset,
5628 isl_basic_set_n_param(bset) + dim, val);
5631 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5632 * then return this fixed value in *val.
5634 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
5636 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
5639 /* Check if input variable in has fixed value and if so and if val is not NULL,
5640 * then return this fixed value in *val.
5642 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
5644 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
5647 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5648 * and if val is not NULL, then return this lower bound in *val.
5650 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
5651 unsigned dim, isl_int *val)
5653 int i, i_eq = -1, i_ineq = -1;
5660 total = isl_basic_set_total_dim(bset);
5661 nparam = isl_basic_set_n_param(bset);
5662 for (i = 0; i < bset->n_eq; ++i) {
5663 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
5669 for (i = 0; i < bset->n_ineq; ++i) {
5670 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
5672 if (i_eq != -1 || i_ineq != -1)
5676 if (i_eq == -1 && i_ineq == -1)
5678 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
5679 /* The coefficient should always be one due to normalization. */
5680 if (!isl_int_is_one(c[1+nparam+dim]))
5682 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
5684 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
5685 total - nparam - dim - 1) != -1)
5688 isl_int_neg(*val, c[0]);
5692 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
5693 unsigned dim, isl_int *val)
5705 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5709 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5711 for (i = 1; fixed == 1 && i < set->n; ++i) {
5712 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
5714 if (fixed == 1 && isl_int_ne(tmp, v))
5718 isl_int_set(*val, v);
5729 static int qsort_constraint_cmp(const void *p1, const void *p2)
5731 const struct constraint *c1 = (const struct constraint *)p1;
5732 const struct constraint *c2 = (const struct constraint *)p2;
5733 unsigned size = isl_min(c1->size, c2->size);
5734 return isl_seq_cmp(c1->c, c2->c, size);
5737 static struct isl_basic_map *isl_basic_map_sort_constraints(
5738 struct isl_basic_map *bmap)
5741 struct constraint *c;
5746 total = isl_basic_map_total_dim(bmap);
5747 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
5750 for (i = 0; i < bmap->n_ineq; ++i) {
5752 c[i].c = bmap->ineq[i];
5754 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
5755 for (i = 0; i < bmap->n_ineq; ++i)
5756 bmap->ineq[i] = c[i].c;
5760 isl_basic_map_free(bmap);
5764 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
5768 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
5770 bmap = isl_basic_map_convex_hull(bmap);
5771 bmap = isl_basic_map_sort_constraints(bmap);
5772 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
5776 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
5778 return (struct isl_basic_set *)isl_basic_map_normalize(
5779 (struct isl_basic_map *)bset);
5782 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
5783 const struct isl_basic_map *bmap2)
5790 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
5791 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
5792 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
5793 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5794 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
5795 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5796 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
5797 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5799 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
5801 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5803 if (bmap1->n_eq != bmap2->n_eq)
5804 return bmap1->n_eq - bmap2->n_eq;
5805 if (bmap1->n_ineq != bmap2->n_ineq)
5806 return bmap1->n_ineq - bmap2->n_ineq;
5807 if (bmap1->n_div != bmap2->n_div)
5808 return bmap1->n_div - bmap2->n_div;
5809 total = isl_basic_map_total_dim(bmap1);
5810 for (i = 0; i < bmap1->n_eq; ++i) {
5811 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
5815 for (i = 0; i < bmap1->n_ineq; ++i) {
5816 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
5820 for (i = 0; i < bmap1->n_div; ++i) {
5821 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
5828 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
5829 struct isl_basic_map *bmap2)
5831 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
5834 static int qsort_bmap_cmp(const void *p1, const void *p2)
5836 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
5837 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
5839 return isl_basic_map_fast_cmp(bmap1, bmap2);
5842 /* We normalize in place, but if anything goes wrong we need
5843 * to return NULL, so we need to make sure we don't change the
5844 * meaning of any possible other copies of map.
5846 struct isl_map *isl_map_normalize(struct isl_map *map)
5849 struct isl_basic_map *bmap;
5853 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
5855 for (i = 0; i < map->n; ++i) {
5856 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
5859 isl_basic_map_free(map->p[i]);
5862 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
5863 ISL_F_SET(map, ISL_MAP_NORMALIZED);
5864 map = isl_map_remove_empty_parts(map);
5867 for (i = map->n - 1; i >= 1; --i) {
5868 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
5870 isl_basic_map_free(map->p[i-1]);
5871 for (j = i; j < map->n; ++j)
5872 map->p[j-1] = map->p[j];
5882 struct isl_set *isl_set_normalize(struct isl_set *set)
5884 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
5887 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
5897 if (!isl_dim_equal(map1->dim, map2->dim))
5900 map1 = isl_map_copy(map1);
5901 map2 = isl_map_copy(map2);
5902 map1 = isl_map_normalize(map1);
5903 map2 = isl_map_normalize(map2);
5906 equal = map1->n == map2->n;
5907 for (i = 0; equal && i < map1->n; ++i) {
5908 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
5921 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
5923 return isl_map_fast_is_equal((struct isl_map *)set1,
5924 (struct isl_map *)set2);
5927 /* Return an interval that ranges from min to max (inclusive)
5929 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
5930 isl_int min, isl_int max)
5933 struct isl_basic_set *bset = NULL;
5935 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
5939 k = isl_basic_set_alloc_inequality(bset);
5942 isl_int_set_si(bset->ineq[k][1], 1);
5943 isl_int_neg(bset->ineq[k][0], min);
5945 k = isl_basic_set_alloc_inequality(bset);
5948 isl_int_set_si(bset->ineq[k][1], -1);
5949 isl_int_set(bset->ineq[k][0], max);
5953 isl_basic_set_free(bset);
5957 /* Return the Cartesian product of the basic sets in list (in the given order).
5959 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
5967 struct isl_basic_set *product = NULL;
5971 isl_assert(list->ctx, list->n > 0, goto error);
5972 isl_assert(list->ctx, list->p[0], goto error);
5973 nparam = isl_basic_set_n_param(list->p[0]);
5974 dim = isl_basic_set_n_dim(list->p[0]);
5975 extra = list->p[0]->n_div;
5976 n_eq = list->p[0]->n_eq;
5977 n_ineq = list->p[0]->n_ineq;
5978 for (i = 1; i < list->n; ++i) {
5979 isl_assert(list->ctx, list->p[i], goto error);
5980 isl_assert(list->ctx,
5981 nparam == isl_basic_set_n_param(list->p[i]), goto error);
5982 dim += isl_basic_set_n_dim(list->p[i]);
5983 extra += list->p[i]->n_div;
5984 n_eq += list->p[i]->n_eq;
5985 n_ineq += list->p[i]->n_ineq;
5987 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
5992 for (i = 0; i < list->n; ++i) {
5993 isl_basic_set_add_constraints(product,
5994 isl_basic_set_copy(list->p[i]), dim);
5995 dim += isl_basic_set_n_dim(list->p[i]);
5997 isl_basic_set_list_free(list);
6000 isl_basic_set_free(product);
6001 isl_basic_set_list_free(list);
6005 struct isl_basic_map *isl_basic_map_product(
6006 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6008 struct isl_dim *dim_result = NULL;
6009 struct isl_basic_map *bmap;
6010 unsigned in1, in2, out1, out2, nparam, total, pos;
6011 struct isl_dim_map *dim_map1, *dim_map2;
6013 if (!bmap1 || !bmap2)
6016 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
6017 bmap2->dim, isl_dim_param), goto error);
6018 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
6019 isl_dim_copy(bmap2->dim));
6021 in1 = isl_basic_map_n_in(bmap1);
6022 in2 = isl_basic_map_n_in(bmap2);
6023 out1 = isl_basic_map_n_out(bmap1);
6024 out2 = isl_basic_map_n_out(bmap2);
6025 nparam = isl_basic_map_n_param(bmap1);
6027 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
6028 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
6029 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
6030 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
6031 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
6032 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
6033 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
6034 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
6035 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
6036 isl_dim_map_div(dim_map1, bmap1, pos += out2);
6037 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
6039 bmap = isl_basic_map_alloc_dim(dim_result,
6040 bmap1->n_div + bmap2->n_div,
6041 bmap1->n_eq + bmap2->n_eq,
6042 bmap1->n_ineq + bmap2->n_ineq);
6043 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
6044 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
6045 bmap = isl_basic_map_simplify(bmap);
6046 return isl_basic_map_finalize(bmap);
6048 isl_basic_map_free(bmap1);
6049 isl_basic_map_free(bmap2);
6053 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6055 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
6058 struct isl_map *result;
6064 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
6065 map2->dim, isl_dim_param), goto error);
6067 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6068 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6069 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6071 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
6072 isl_dim_copy(map2->dim)),
6073 map1->n * map2->n, flags);
6076 for (i = 0; i < map1->n; ++i)
6077 for (j = 0; j < map2->n; ++j) {
6078 struct isl_basic_map *part;
6079 part = isl_basic_map_product(
6080 isl_basic_map_copy(map1->p[i]),
6081 isl_basic_map_copy(map2->p[j]));
6082 if (isl_basic_map_is_empty(part))
6083 isl_basic_map_free(part);
6085 result = isl_map_add_basic_map(result, part);
6098 /* Given two set A and B, construct its Cartesian product A x B.
6100 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
6102 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
6103 (struct isl_map *)set2);
6106 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
6109 uint32_t hash = isl_hash_init();
6114 bset = isl_basic_set_copy(bset);
6115 bset = isl_basic_set_normalize(bset);
6118 total = isl_basic_set_total_dim(bset);
6119 isl_hash_byte(hash, bset->n_eq & 0xFF);
6120 for (i = 0; i < bset->n_eq; ++i) {
6122 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
6123 isl_hash_hash(hash, c_hash);
6125 isl_hash_byte(hash, bset->n_ineq & 0xFF);
6126 for (i = 0; i < bset->n_ineq; ++i) {
6128 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
6129 isl_hash_hash(hash, c_hash);
6131 isl_hash_byte(hash, bset->n_div & 0xFF);
6132 for (i = 0; i < bset->n_div; ++i) {
6134 if (isl_int_is_zero(bset->div[i][0]))
6136 isl_hash_byte(hash, i & 0xFF);
6137 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
6138 isl_hash_hash(hash, c_hash);
6140 isl_basic_set_free(bset);
6144 uint32_t isl_set_get_hash(struct isl_set *set)
6151 set = isl_set_copy(set);
6152 set = isl_set_normalize(set);
6156 hash = isl_hash_init();
6157 for (i = 0; i < set->n; ++i) {
6159 bset_hash = isl_basic_set_get_hash(set->p[i]);
6160 isl_hash_hash(hash, bset_hash);
6168 /* Check if the value for dimension dim is completely determined
6169 * by the values of the other parameters and variables.
6170 * That is, check if dimension dim is involved in an equality.
6172 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
6179 nparam = isl_basic_set_n_param(bset);
6180 for (i = 0; i < bset->n_eq; ++i)
6181 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
6186 /* Check if the value for dimension dim is completely determined
6187 * by the values of the other parameters and variables.
6188 * That is, check if dimension dim is involved in an equality
6189 * for each of the subsets.
6191 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
6197 for (i = 0; i < set->n; ++i) {
6199 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
6206 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
6207 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
6214 for (i = 0; i < map->n; ++i)
6215 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
6221 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
6222 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
6229 for (i = 0; i < set->n; ++i)
6230 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
6236 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
6238 struct isl_dim *dim;
6243 if (bset->n_div == 0)
6246 bset = isl_basic_set_cow(bset);
6250 dim = isl_basic_set_get_dim(bset);
6251 dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
6254 isl_dim_free(bset->dim);
6260 isl_basic_set_free(bset);
6264 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
6267 struct isl_dim *dim;
6270 set = isl_set_align_divs(set);
6274 if (set->n == 0 || set->p[0]->n_div == 0)
6277 set = isl_set_cow(set);
6281 n_div = set->p[0]->n_div;
6282 dim = isl_set_get_dim(set);
6283 dim = isl_dim_add(dim, isl_dim_set, n_div);
6286 isl_dim_free(set->dim);
6289 for (i = 0; i < set->n; ++i) {
6290 set->p[i] = isl_basic_set_lift(set->p[i]);
6301 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
6303 struct isl_dim *dim;
6304 struct isl_basic_map *bmap;
6311 set = isl_set_align_divs(set);
6316 dim = isl_set_get_dim(set);
6317 if (set->n == 0 || set->p[0]->n_div == 0) {
6319 return isl_map_identity(dim);
6322 n_div = set->p[0]->n_div;
6323 dim = isl_dim_map(dim);
6324 n_param = isl_dim_size(dim, isl_dim_param);
6325 n_set = isl_dim_size(dim, isl_dim_in);
6326 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
6327 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
6328 for (i = 0; i < n_set; ++i)
6329 bmap = var_equal(bmap, i);
6331 total = n_param + n_set + n_set + n_div;
6332 for (i = 0; i < n_div; ++i) {
6333 k = isl_basic_map_alloc_inequality(bmap);
6336 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
6337 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
6338 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
6339 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
6340 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
6341 set->p[0]->div[i][0]);
6343 l = isl_basic_map_alloc_inequality(bmap);
6346 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
6347 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
6348 set->p[0]->div[i][0]);
6349 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
6353 return isl_map_from_basic_map(bmap);
6356 isl_basic_map_free(bmap);
6360 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
6368 dim = isl_basic_set_total_dim(bset);
6369 size += bset->n_eq * (1 + dim);
6370 size += bset->n_ineq * (1 + dim);
6371 size += bset->n_div * (2 + dim);
6376 int isl_set_size(__isl_keep isl_set *set)
6384 for (i = 0; i < set->n; ++i)
6385 size += isl_basic_set_size(set->p[i]);
6390 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
6391 enum isl_dim_type type, unsigned pos)
6399 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
6401 pos += isl_basic_map_offset(bmap, type);
6403 for (i = 0; i < bmap->n_eq; ++i)
6404 if (!isl_int_is_zero(bmap->eq[i][pos]))
6408 for (i = 0; i < bmap->n_ineq; ++i) {
6409 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
6416 return lower && upper;
6419 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
6420 enum isl_dim_type type, unsigned pos)
6427 for (i = 0; i < map->n; ++i) {
6429 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
6430 if (bounded < 0 || !bounded)
6437 /* Return 1 if the specified dim is involved in both an upper bound
6438 * and a lower bound.
6440 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
6441 enum isl_dim_type type, unsigned pos)
6443 return isl_map_dim_is_bounded((isl_map *)set, type, pos);