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;
62 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
65 case isl_dim_param: return 1;
66 case isl_dim_in: return 1 + dim->nparam;
67 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
72 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
73 struct isl_dim *dim, enum isl_dim_type type,
74 unsigned first, unsigned n, unsigned dst_pos)
82 src_pos = pos(dim, type);
83 for (i = 0; i < n; ++i)
84 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
87 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
88 enum isl_dim_type type, unsigned dst_pos)
90 isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
93 static void isl_dim_map_div(struct isl_dim_map *dim_map,
94 struct isl_basic_map *bmap, unsigned dst_pos)
99 if (!dim_map || !bmap)
102 src_pos = 1 + isl_dim_total(bmap->dim);
103 for (i = 0; i < bmap->n_div; ++i)
104 dim_map->pos[1 + dst_pos + i] = src_pos + i;
107 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
111 for (i = 0; i < dim_map->len; ++i)
112 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
113 fprintf(stderr, "\n");
116 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
117 enum isl_dim_type type)
122 case isl_dim_out: return isl_dim_size(bmap->dim, type);
123 case isl_dim_div: return bmap->n_div;
124 case isl_dim_all: return isl_basic_map_total_dim(bmap);
129 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
131 return map ? n(map->dim, type) : 0;
134 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
136 return set ? n(set->dim, type) : 0;
139 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
140 enum isl_dim_type type)
142 struct isl_dim *dim = bmap->dim;
144 case isl_dim_param: return 1;
145 case isl_dim_in: return 1 + dim->nparam;
146 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
147 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
152 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
154 return pos(map->dim, type);
157 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
158 enum isl_dim_type type)
160 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
163 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
165 return bset->dim->n_out;
168 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
170 return bset->dim->nparam;
173 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
175 return isl_dim_total(bset->dim) + bset->n_div;
178 unsigned isl_set_n_dim(const struct isl_set *set)
180 return set->dim->n_out;
183 unsigned isl_set_n_param(const struct isl_set *set)
185 return set->dim->nparam;
188 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
190 return bmap->dim->n_in;
193 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
195 return bmap->dim->n_out;
198 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
200 return bmap->dim->nparam;
203 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
208 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
210 return isl_dim_total(bmap->dim) + bmap->n_div;
213 unsigned isl_map_n_in(const struct isl_map *map)
215 return map->dim->n_in;
218 unsigned isl_map_n_out(const struct isl_map *map)
220 return map->dim->n_out;
223 unsigned isl_map_n_param(const struct isl_map *map)
225 return map->dim->nparam;
228 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
230 return map->dim->n_in == set->dim->n_out &&
231 map->dim->nparam == set->dim->nparam;
234 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
235 struct isl_basic_set *bset)
237 return bmap->dim->n_in == bset->dim->n_out &&
238 bmap->dim->nparam == bset->dim->nparam;
241 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
242 struct isl_basic_set *bset)
244 return bmap->dim->n_out == bset->dim->n_out &&
245 bmap->dim->nparam == bset->dim->nparam;
248 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
252 return isl_dim_copy(bmap->dim);
255 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
259 return isl_dim_copy(bset->dim);
262 struct isl_dim *isl_map_get_dim(struct isl_map *map)
266 return isl_dim_copy(map->dim);
269 struct isl_dim *isl_set_get_dim(struct isl_set *set)
273 return isl_dim_copy(set->dim);
276 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
277 struct isl_basic_map *bmap, unsigned extra,
278 unsigned n_eq, unsigned n_ineq)
281 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
283 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
284 if (isl_blk_is_error(bmap->block)) {
289 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
291 isl_blk_free(ctx, bmap->block);
297 bmap->block2 = isl_blk_empty();
300 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
301 if (isl_blk_is_error(bmap->block2)) {
303 isl_blk_free(ctx, bmap->block);
308 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
310 isl_blk_free(ctx, bmap->block2);
312 isl_blk_free(ctx, bmap->block);
318 for (i = 0; i < n_ineq + n_eq; ++i)
319 bmap->ineq[i] = bmap->block.data + i * row_size;
321 for (i = 0; i < extra; ++i)
322 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
328 bmap->c_size = n_eq + n_ineq;
329 bmap->eq = bmap->ineq + n_ineq;
339 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
340 unsigned nparam, unsigned dim, unsigned extra,
341 unsigned n_eq, unsigned n_ineq)
343 struct isl_basic_map *bmap;
344 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
345 return (struct isl_basic_set *)bmap;
348 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
349 unsigned extra, unsigned n_eq, unsigned n_ineq)
351 struct isl_basic_map *bmap;
354 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
355 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
356 return (struct isl_basic_set *)bmap;
359 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
360 unsigned extra, unsigned n_eq, unsigned n_ineq)
362 struct isl_basic_map *bmap;
366 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
371 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
377 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
378 unsigned nparam, unsigned in, unsigned out, unsigned extra,
379 unsigned n_eq, unsigned n_ineq)
381 struct isl_basic_map *bmap;
384 dim = isl_dim_alloc(ctx, nparam, in, out);
388 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
392 static void dup_constraints(
393 struct isl_basic_map *dst, struct isl_basic_map *src)
396 unsigned total = isl_basic_map_total_dim(src);
398 for (i = 0; i < src->n_eq; ++i) {
399 int j = isl_basic_map_alloc_equality(dst);
400 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
403 for (i = 0; i < src->n_ineq; ++i) {
404 int j = isl_basic_map_alloc_inequality(dst);
405 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
408 for (i = 0; i < src->n_div; ++i) {
409 int j = isl_basic_map_alloc_div(dst);
410 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
412 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
415 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
417 struct isl_basic_map *dup;
421 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
422 bmap->n_div, bmap->n_eq, bmap->n_ineq);
425 dup_constraints(dup, bmap);
426 dup->flags = bmap->flags;
427 dup->sample = isl_vec_copy(bmap->sample);
431 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
433 struct isl_basic_map *dup;
435 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
436 return (struct isl_basic_set *)dup;
439 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
444 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
448 return isl_basic_set_dup(bset);
451 struct isl_set *isl_set_copy(struct isl_set *set)
460 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
465 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
469 return isl_basic_map_dup(bmap);
472 struct isl_map *isl_map_copy(struct isl_map *map)
481 void isl_basic_map_free(struct isl_basic_map *bmap)
489 isl_ctx_deref(bmap->ctx);
491 isl_blk_free(bmap->ctx, bmap->block2);
493 isl_blk_free(bmap->ctx, bmap->block);
494 isl_vec_free(bmap->sample);
495 isl_dim_free(bmap->dim);
499 void isl_basic_set_free(struct isl_basic_set *bset)
501 isl_basic_map_free((struct isl_basic_map *)bset);
504 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
506 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
509 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
515 isl_assert(ctx, room_for_con(bmap, 1), return -1);
516 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
518 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
519 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
520 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
521 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
522 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
523 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
525 int j = isl_basic_map_alloc_inequality(bmap);
529 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
530 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
537 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
538 bmap->extra - bmap->n_div);
542 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
544 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
547 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
551 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
556 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
558 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
561 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
566 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
568 if (pos != bmap->n_eq - 1) {
570 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
571 bmap->eq[bmap->n_eq - 1] = t;
577 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
579 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
582 void isl_basic_map_inequality_to_equality(
583 struct isl_basic_map *bmap, unsigned pos)
588 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
589 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
594 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
595 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
596 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
597 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
600 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
602 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
605 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
611 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
612 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
613 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
614 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
615 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
616 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
617 1 + isl_basic_map_total_dim(bmap),
618 bmap->extra - bmap->n_div);
619 return bmap->n_ineq++;
622 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
624 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
627 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
631 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
636 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
638 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
641 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
646 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
648 if (pos != bmap->n_ineq - 1) {
650 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
651 bmap->ineq[bmap->n_ineq - 1] = t;
652 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
658 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
660 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
663 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
668 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
671 k = isl_basic_map_alloc_equality(bmap);
674 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
677 isl_basic_map_free(bmap);
681 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
684 return (isl_basic_set *)
685 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
688 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
693 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
696 k = isl_basic_map_alloc_inequality(bmap);
699 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
702 isl_basic_map_free(bmap);
706 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
709 return (isl_basic_set *)
710 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
713 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
717 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
718 isl_seq_clr(bmap->div[bmap->n_div] +
719 1 + 1 + isl_basic_map_total_dim(bmap),
720 bmap->extra - bmap->n_div);
721 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
722 return bmap->n_div++;
725 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
727 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
730 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
734 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
739 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
741 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
744 /* Copy constraint from src to dst, putting the vars of src at offset
745 * dim_off in dst and the divs of src at offset div_off in dst.
746 * If both sets are actually map, then dim_off applies to the input
749 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
750 struct isl_basic_map *src_map, isl_int *src,
751 unsigned in_off, unsigned out_off, unsigned div_off)
753 unsigned src_nparam = isl_basic_map_n_param(src_map);
754 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
755 unsigned src_in = isl_basic_map_n_in(src_map);
756 unsigned dst_in = isl_basic_map_n_in(dst_map);
757 unsigned src_out = isl_basic_map_n_out(src_map);
758 unsigned dst_out = isl_basic_map_n_out(dst_map);
759 isl_int_set(dst[0], src[0]);
760 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
761 if (dst_nparam > src_nparam)
762 isl_seq_clr(dst+1+src_nparam,
763 dst_nparam - src_nparam);
764 isl_seq_clr(dst+1+dst_nparam, in_off);
765 isl_seq_cpy(dst+1+dst_nparam+in_off,
767 isl_min(dst_in-in_off, src_in));
768 if (dst_in-in_off > src_in)
769 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
770 dst_in - in_off - src_in);
771 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
772 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
773 src+1+src_nparam+src_in,
774 isl_min(dst_out-out_off, src_out));
775 if (dst_out-out_off > src_out)
776 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
777 dst_out - out_off - src_out);
778 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
779 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
780 src+1+src_nparam+src_in+src_out,
781 isl_min(dst_map->extra-div_off, src_map->n_div));
782 if (dst_map->n_div-div_off > src_map->n_div)
783 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
784 div_off+src_map->n_div,
785 dst_map->n_div - div_off - src_map->n_div);
788 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
789 struct isl_basic_map *src_map, isl_int *src,
790 unsigned in_off, unsigned out_off, unsigned div_off)
792 isl_int_set(dst[0], src[0]);
793 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
796 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
797 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
802 if (!bmap1 || !bmap2)
805 div_off = bmap1->n_div;
807 for (i = 0; i < bmap2->n_eq; ++i) {
808 int i1 = isl_basic_map_alloc_equality(bmap1);
811 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
812 i_pos, o_pos, div_off);
815 for (i = 0; i < bmap2->n_ineq; ++i) {
816 int i1 = isl_basic_map_alloc_inequality(bmap1);
819 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
820 i_pos, o_pos, div_off);
823 for (i = 0; i < bmap2->n_div; ++i) {
824 int i1 = isl_basic_map_alloc_div(bmap1);
827 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
828 i_pos, o_pos, div_off);
831 isl_basic_map_free(bmap2);
836 isl_basic_map_free(bmap1);
837 isl_basic_map_free(bmap2);
841 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
842 struct isl_dim_map *dim_map)
846 for (i = 0; i < dim_map->len; ++i) {
847 if (dim_map->pos[i] < 0)
848 isl_int_set_si(dst[i], 0);
850 isl_int_set(dst[i], src[dim_map->pos[i]]);
854 static void copy_div_dim_map(isl_int *dst, isl_int *src,
855 struct isl_dim_map *dim_map)
857 isl_int_set(dst[0], src[0]);
858 copy_constraint_dim_map(dst+1, src+1, dim_map);
861 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
862 struct isl_basic_map *src, struct isl_dim_map *dim_map)
866 if (!src || !dst || !dim_map)
869 for (i = 0; i < src->n_eq; ++i) {
870 int i1 = isl_basic_map_alloc_equality(dst);
873 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
876 for (i = 0; i < src->n_ineq; ++i) {
877 int i1 = isl_basic_map_alloc_inequality(dst);
880 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
883 for (i = 0; i < src->n_div; ++i) {
884 int i1 = isl_basic_map_alloc_div(dst);
887 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
891 isl_basic_map_free(src);
896 isl_basic_map_free(src);
897 isl_basic_map_free(dst);
901 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
902 struct isl_basic_set *bset2, unsigned pos)
904 return (struct isl_basic_set *)
905 add_constraints((struct isl_basic_map *)bset1,
906 (struct isl_basic_map *)bset2, 0, pos);
909 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
910 struct isl_dim *dim, unsigned extra,
911 unsigned n_eq, unsigned n_ineq)
913 struct isl_basic_map *ext;
923 dims_ok = isl_dim_equal(base->dim, dim) &&
924 base->extra >= base->n_div + extra;
926 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
927 room_for_ineq(base, n_ineq)) {
932 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
933 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
934 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
935 extra += base->extra;
937 n_ineq += base->n_ineq;
939 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
945 ext->sample = isl_vec_copy(base->sample);
947 ext = add_constraints(ext, base, 0, 0);
950 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
957 isl_basic_map_free(base);
961 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
962 struct isl_dim *dim, unsigned extra,
963 unsigned n_eq, unsigned n_ineq)
965 return (struct isl_basic_set *)
966 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
967 extra, n_eq, n_ineq);
970 struct isl_basic_map *isl_basic_map_extend_constraints(
971 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
975 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
979 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
980 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
981 unsigned n_eq, unsigned n_ineq)
983 struct isl_basic_map *bmap;
988 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
992 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
996 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
997 unsigned nparam, unsigned dim, unsigned extra,
998 unsigned n_eq, unsigned n_ineq)
1000 return (struct isl_basic_set *)
1001 isl_basic_map_extend((struct isl_basic_map *)base,
1002 nparam, 0, dim, extra, n_eq, n_ineq);
1005 struct isl_basic_set *isl_basic_set_extend_constraints(
1006 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1008 return (struct isl_basic_set *)
1009 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1013 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1015 return (struct isl_basic_set *)
1016 isl_basic_map_cow((struct isl_basic_map *)bset);
1019 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1024 if (bmap->ref > 1) {
1026 bmap = isl_basic_map_dup(bmap);
1028 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1032 struct isl_set *isl_set_cow(struct isl_set *set)
1040 return isl_set_dup(set);
1043 struct isl_map *isl_map_cow(struct isl_map *map)
1051 return isl_map_dup(map);
1054 static void swap_vars(struct isl_blk blk, isl_int *a,
1055 unsigned a_len, unsigned b_len)
1057 isl_seq_cpy(blk.data, a+a_len, b_len);
1058 isl_seq_cpy(blk.data+b_len, a, a_len);
1059 isl_seq_cpy(a, blk.data, b_len+a_len);
1062 struct isl_basic_set *isl_basic_set_swap_vars(
1063 struct isl_basic_set *bset, unsigned n)
1073 nparam = isl_basic_set_n_param(bset);
1074 dim = isl_basic_set_n_dim(bset);
1075 isl_assert(bset->ctx, n <= dim, goto error);
1080 bset = isl_basic_set_cow(bset);
1084 blk = isl_blk_alloc(bset->ctx, dim);
1085 if (isl_blk_is_error(blk))
1088 for (i = 0; i < bset->n_eq; ++i)
1090 bset->eq[i]+1+nparam, n, dim - n);
1092 for (i = 0; i < bset->n_ineq; ++i)
1094 bset->ineq[i]+1+nparam, n, dim - n);
1096 for (i = 0; i < bset->n_div; ++i)
1098 bset->div[i]+1+1+nparam, n, dim - n);
1100 isl_blk_free(bset->ctx, blk);
1102 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1103 return isl_basic_set_gauss(bset, NULL);
1105 isl_basic_set_free(bset);
1109 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1112 set = isl_set_cow(set);
1116 for (i = 0; i < set->n; ++i) {
1117 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1123 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1127 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1133 total = isl_basic_map_total_dim(bmap);
1134 isl_basic_map_free_div(bmap, bmap->n_div);
1135 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1137 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1139 i = isl_basic_map_alloc_equality(bmap);
1143 isl_int_set_si(bmap->eq[i][0], 1);
1144 isl_seq_clr(bmap->eq[i]+1, total);
1145 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1146 isl_vec_free(bmap->sample);
1147 bmap->sample = NULL;
1148 return isl_basic_map_finalize(bmap);
1150 isl_basic_map_free(bmap);
1154 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1156 return (struct isl_basic_set *)
1157 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1160 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1163 unsigned off = isl_dim_total(bmap->dim);
1164 isl_int *t = bmap->div[a];
1165 bmap->div[a] = bmap->div[b];
1168 for (i = 0; i < bmap->n_eq; ++i)
1169 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1171 for (i = 0; i < bmap->n_ineq; ++i)
1172 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1174 for (i = 0; i < bmap->n_div; ++i)
1175 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1176 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1179 /* Eliminate the specified n dimensions starting at first from the
1180 * constraints using Fourier-Motzkin. The dimensions themselves
1183 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1184 enum isl_dim_type type, unsigned first, unsigned n)
1193 map = isl_map_cow(map);
1196 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1197 first += pos(map->dim, type) - 1;
1199 for (i = 0; i < map->n; ++i) {
1200 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1210 /* Eliminate the specified n dimensions starting at first from the
1211 * constraints using Fourier-Motzkin. The dimensions themselves
1214 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1215 enum isl_dim_type type, unsigned first, unsigned n)
1217 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1220 /* Eliminate the specified n dimensions starting at first from the
1221 * constraints using Fourier-Motzkin. The dimensions themselves
1224 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1225 unsigned first, unsigned n)
1227 return isl_set_eliminate(set, isl_dim_set, first, n);
1230 /* Project out n dimensions starting at first using Fourier-Motzkin */
1231 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1232 unsigned first, unsigned n)
1234 set = isl_set_eliminate_dims(set, first, n);
1235 set = isl_set_drop_dims(set, first, n);
1239 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1241 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1249 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1258 set = isl_set_cow(set);
1262 for (i = 0; i < set->n; ++i) {
1263 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1273 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1274 enum isl_dim_type type, unsigned first, unsigned n)
1278 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1282 bmap = isl_basic_map_eliminate_vars(bmap,
1283 isl_basic_map_offset(bmap, type) - 1 + first, n);
1286 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1288 bmap = isl_basic_map_drop(bmap, type, first, n);
1291 isl_basic_map_free(bmap);
1295 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1296 enum isl_dim_type type, unsigned first, unsigned n)
1298 return (isl_basic_set *)
1299 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1302 struct isl_map *isl_map_remove(struct isl_map *map,
1303 enum isl_dim_type type, unsigned first, unsigned n)
1310 map = isl_map_cow(map);
1313 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1315 for (i = 0; i < map->n; ++i) {
1316 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1317 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1321 map = isl_map_drop(map, type, first, n);
1328 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1329 enum isl_dim_type type, unsigned first, unsigned n)
1331 return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1334 /* Project out n inputs starting at first using Fourier-Motzkin */
1335 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1336 unsigned first, unsigned n)
1338 return isl_map_remove(map, isl_dim_in, first, n);
1341 /* Project out n dimensions starting at first using Fourier-Motzkin */
1342 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1343 unsigned first, unsigned n)
1345 unsigned nparam = isl_basic_set_n_param(bset);
1346 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1347 bset = isl_basic_set_drop_dims(bset, first, n);
1351 static void dump_term(struct isl_basic_map *bmap,
1352 isl_int c, int pos, FILE *out)
1355 unsigned in = isl_basic_map_n_in(bmap);
1356 unsigned dim = in + isl_basic_map_n_out(bmap);
1357 unsigned nparam = isl_basic_map_n_param(bmap);
1359 isl_int_print(out, c, 0);
1361 if (!isl_int_is_one(c))
1362 isl_int_print(out, c, 0);
1363 if (pos < 1 + nparam) {
1364 name = isl_dim_get_name(bmap->dim,
1365 isl_dim_param, pos - 1);
1367 fprintf(out, "%s", name);
1369 fprintf(out, "p%d", pos - 1);
1370 } else if (pos < 1 + nparam + in)
1371 fprintf(out, "i%d", pos - 1 - nparam);
1372 else if (pos < 1 + nparam + dim)
1373 fprintf(out, "o%d", pos - 1 - nparam - in);
1375 fprintf(out, "e%d", pos - 1 - nparam - dim);
1379 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1380 int sign, FILE *out)
1384 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1388 for (i = 0, first = 1; i < len; ++i) {
1389 if (isl_int_sgn(c[i]) * sign <= 0)
1392 fprintf(out, " + ");
1394 isl_int_abs(v, c[i]);
1395 dump_term(bmap, v, i, out);
1402 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1403 const char *op, FILE *out, int indent)
1407 fprintf(out, "%*s", indent, "");
1409 dump_constraint_sign(bmap, c, 1, out);
1410 fprintf(out, " %s ", op);
1411 dump_constraint_sign(bmap, c, -1, out);
1415 for (i = bmap->n_div; i < bmap->extra; ++i) {
1416 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1418 fprintf(out, "%*s", indent, "");
1419 fprintf(out, "ERROR: unused div coefficient not zero\n");
1424 static void dump_constraints(struct isl_basic_map *bmap,
1425 isl_int **c, unsigned n,
1426 const char *op, FILE *out, int indent)
1430 for (i = 0; i < n; ++i)
1431 dump_constraint(bmap, c[i], op, out, indent);
1434 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1438 unsigned total = isl_basic_map_total_dim(bmap);
1440 for (j = 0; j < 1 + total; ++j) {
1441 if (isl_int_is_zero(exp[j]))
1443 if (!first && isl_int_is_pos(exp[j]))
1445 dump_term(bmap, exp[j], j, out);
1450 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1454 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1455 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1457 for (i = 0; i < bmap->n_div; ++i) {
1458 fprintf(out, "%*s", indent, "");
1459 fprintf(out, "e%d = [(", i);
1460 dump_affine(bmap, bmap->div[i]+1, out);
1462 isl_int_print(out, bmap->div[i][0], 0);
1463 fprintf(out, "]\n");
1467 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1470 fprintf(out, "null basic set\n");
1474 fprintf(out, "%*s", indent, "");
1475 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1476 bset->ref, bset->dim->nparam, bset->dim->n_out,
1477 bset->extra, bset->flags);
1478 dump((struct isl_basic_map *)bset, out, indent);
1481 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1484 fprintf(out, "null basic map\n");
1488 fprintf(out, "%*s", indent, "");
1489 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1490 "flags: %x, n_name: %d\n",
1492 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1493 bmap->extra, bmap->flags, bmap->dim->n_name);
1494 dump(bmap, out, indent);
1497 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1502 total = isl_basic_map_total_dim(bmap);
1503 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1504 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1505 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1506 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1510 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1512 struct isl_set *set;
1516 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1517 isl_assert(dim->ctx, n >= 0, return NULL);
1518 set = isl_alloc(dim->ctx, struct isl_set,
1519 sizeof(struct isl_set) +
1520 (n - 1) * sizeof(struct isl_basic_set *));
1524 set->ctx = dim->ctx;
1525 isl_ctx_ref(set->ctx);
1537 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1538 unsigned nparam, unsigned dim, int n, unsigned flags)
1540 struct isl_set *set;
1541 struct isl_dim *dims;
1543 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1547 set = isl_set_alloc_dim(dims, n, flags);
1551 /* Make sure "map" has room for at least "n" more basic maps.
1553 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1556 struct isl_map *grown = NULL;
1560 isl_assert(map->ctx, n >= 0, goto error);
1561 if (map->n + n <= map->size)
1563 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1566 for (i = 0; i < map->n; ++i) {
1567 grown->p[i] = isl_basic_map_copy(map->p[i]);
1575 isl_map_free(grown);
1580 /* Make sure "set" has room for at least "n" more basic sets.
1582 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1584 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1587 struct isl_set *isl_set_dup(struct isl_set *set)
1590 struct isl_set *dup;
1595 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1598 for (i = 0; i < set->n; ++i)
1599 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1603 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1605 struct isl_set *set;
1610 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1612 isl_basic_set_free(bset);
1615 return isl_set_add_basic_set(set, bset);
1618 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1620 struct isl_map *map;
1625 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1627 isl_basic_map_free(bmap);
1630 return isl_map_add_basic_map(map, bmap);
1633 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1634 __isl_take isl_basic_set *bset)
1636 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1637 (struct isl_basic_map *)bset);
1640 void isl_set_free(struct isl_set *set)
1650 isl_ctx_deref(set->ctx);
1651 for (i = 0; i < set->n; ++i)
1652 isl_basic_set_free(set->p[i]);
1653 isl_dim_free(set->dim);
1657 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1662 fprintf(out, "null set\n");
1666 fprintf(out, "%*s", indent, "");
1667 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1668 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1670 for (i = 0; i < set->n; ++i) {
1671 fprintf(out, "%*s", indent, "");
1672 fprintf(out, "basic set %d:\n", i);
1673 isl_basic_set_dump(set->p[i], out, indent+4);
1677 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1682 fprintf(out, "null map\n");
1686 fprintf(out, "%*s", indent, "");
1687 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1688 "flags: %x, n_name: %d\n",
1689 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1690 map->dim->n_out, map->flags, map->dim->n_name);
1691 for (i = 0; i < map->n; ++i) {
1692 fprintf(out, "%*s", indent, "");
1693 fprintf(out, "basic map %d:\n", i);
1694 isl_basic_map_dump(map->p[i], out, indent+4);
1698 struct isl_basic_map *isl_basic_map_intersect_domain(
1699 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1701 struct isl_basic_map *bmap_domain;
1702 struct isl_dim *dim;
1707 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1708 bset->dim, isl_dim_param), goto error);
1710 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1711 isl_assert(bset->ctx,
1712 isl_basic_map_compatible_domain(bmap, bset), goto error);
1714 bmap = isl_basic_map_cow(bmap);
1715 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1716 bset->n_div, bset->n_eq, bset->n_ineq);
1719 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1720 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1721 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1723 bmap = isl_basic_map_simplify(bmap);
1724 return isl_basic_map_finalize(bmap);
1726 isl_basic_map_free(bmap);
1727 isl_basic_set_free(bset);
1731 struct isl_basic_map *isl_basic_map_intersect_range(
1732 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1734 struct isl_basic_map *bmap_range;
1739 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1740 bset->dim, isl_dim_param), goto error);
1742 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1743 isl_assert(bset->ctx,
1744 isl_basic_map_compatible_range(bmap, bset), goto error);
1746 bmap = isl_basic_map_cow(bmap);
1747 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1748 bset->n_div, bset->n_eq, bset->n_ineq);
1751 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1752 bmap = add_constraints(bmap, bmap_range, 0, 0);
1754 bmap = isl_basic_map_simplify(bmap);
1755 return isl_basic_map_finalize(bmap);
1757 isl_basic_map_free(bmap);
1758 isl_basic_set_free(bset);
1762 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1768 total = 1 + isl_basic_map_total_dim(bmap);
1769 if (total != vec->size)
1774 for (i = 0; i < bmap->n_eq; ++i) {
1775 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1776 if (!isl_int_is_zero(s)) {
1782 for (i = 0; i < bmap->n_ineq; ++i) {
1783 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1784 if (isl_int_is_neg(s)) {
1795 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1797 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1800 struct isl_basic_map *isl_basic_map_intersect(
1801 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1803 struct isl_vec *sample = NULL;
1805 if (!bmap1 || !bmap2)
1808 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1809 bmap2->dim, isl_dim_param), goto error);
1810 if (isl_dim_total(bmap1->dim) ==
1811 isl_dim_size(bmap1->dim, isl_dim_param) &&
1812 isl_dim_total(bmap2->dim) !=
1813 isl_dim_size(bmap2->dim, isl_dim_param))
1814 return isl_basic_map_intersect(bmap2, bmap1);
1816 if (isl_dim_total(bmap2->dim) !=
1817 isl_dim_size(bmap2->dim, isl_dim_param))
1818 isl_assert(bmap1->ctx,
1819 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1821 if (bmap1->sample &&
1822 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1823 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1824 sample = isl_vec_copy(bmap1->sample);
1825 else if (bmap2->sample &&
1826 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1827 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1828 sample = isl_vec_copy(bmap2->sample);
1830 bmap1 = isl_basic_map_cow(bmap1);
1831 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1832 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1835 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1838 isl_vec_free(bmap1->sample);
1839 bmap1->sample = sample;
1842 bmap1 = isl_basic_map_simplify(bmap1);
1843 return isl_basic_map_finalize(bmap1);
1846 isl_vec_free(sample);
1847 isl_basic_map_free(bmap1);
1848 isl_basic_map_free(bmap2);
1852 struct isl_basic_set *isl_basic_set_intersect(
1853 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1855 return (struct isl_basic_set *)
1856 isl_basic_map_intersect(
1857 (struct isl_basic_map *)bset1,
1858 (struct isl_basic_map *)bset2);
1861 /* Special case of isl_map_intersect, where both map1 and map2
1862 * are convex, without any divs and such that either map1 or map2
1863 * contains a single constraint. This constraint is then simply
1864 * added to the other map.
1866 static __isl_give isl_map *map_intersect_add_constraint(
1867 __isl_take isl_map *map1, __isl_take isl_map *map2)
1869 struct isl_basic_map *bmap1;
1870 struct isl_basic_map *bmap2;
1872 isl_assert(map1->ctx, map1->n == 1, goto error);
1873 isl_assert(map2->ctx, map1->n == 1, goto error);
1874 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
1875 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
1877 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
1878 return isl_map_intersect(map2, map1);
1880 isl_assert(map2->ctx,
1881 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
1883 map1 = isl_map_cow(map1);
1886 if (isl_map_fast_is_empty(map1)) {
1890 map1->p[0] = isl_basic_map_cow(map1->p[0]);
1891 if (map2->p[0]->n_eq == 1)
1892 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
1894 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
1895 map2->p[0]->ineq[0]);
1897 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
1898 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
1902 if (isl_basic_map_fast_is_empty(map1->p[0])) {
1903 isl_basic_map_free(map1->p[0]);
1916 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1919 struct isl_map *result;
1925 if (isl_map_fast_is_empty(map1)) {
1929 if (isl_map_fast_is_empty(map2)) {
1934 if (map1->n == 1 && map2->n == 1 &&
1935 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
1936 isl_dim_equal(map1->dim, map2->dim) &&
1937 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
1938 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
1939 return map_intersect_add_constraint(map1, map2);
1940 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1941 map2->dim, isl_dim_param), goto error);
1942 if (isl_dim_total(map1->dim) ==
1943 isl_dim_size(map1->dim, isl_dim_param) &&
1944 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1945 return isl_map_intersect(map2, map1);
1947 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1948 isl_assert(map1->ctx,
1949 isl_dim_equal(map1->dim, map2->dim), goto error);
1951 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1952 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1953 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1955 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1956 map1->n * map2->n, flags);
1959 for (i = 0; i < map1->n; ++i)
1960 for (j = 0; j < map2->n; ++j) {
1961 struct isl_basic_map *part;
1962 part = isl_basic_map_intersect(
1963 isl_basic_map_copy(map1->p[i]),
1964 isl_basic_map_copy(map2->p[j]));
1965 if (isl_basic_map_is_empty(part))
1966 isl_basic_map_free(part);
1968 result = isl_map_add_basic_map(result, part);
1981 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1983 return (struct isl_set *)
1984 isl_map_intersect((struct isl_map *)set1,
1985 (struct isl_map *)set2);
1988 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1990 struct isl_dim *dim;
1991 struct isl_basic_set *bset;
1996 bmap = isl_basic_map_cow(bmap);
1999 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2000 in = isl_basic_map_n_in(bmap);
2001 bset = isl_basic_set_from_basic_map(bmap);
2002 bset = isl_basic_set_swap_vars(bset, in);
2003 return isl_basic_map_from_basic_set(bset, dim);
2006 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2007 enum isl_dim_type type, unsigned pos, unsigned n)
2009 struct isl_dim *res_dim;
2010 struct isl_basic_map *res;
2011 struct isl_dim_map *dim_map;
2012 unsigned total, off;
2013 enum isl_dim_type t;
2021 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2023 total = isl_basic_map_total_dim(bmap) + n;
2024 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2026 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2028 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2030 unsigned size = isl_basic_map_dim(bmap, t);
2031 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2033 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2034 pos, size - pos, off + pos + n);
2036 off += isl_dim_size(res_dim, t);
2038 isl_dim_map_div(dim_map, bmap, off);
2040 res = isl_basic_map_alloc_dim(res_dim,
2041 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2042 res = add_constraints_dim_map(res, bmap, dim_map);
2043 res = isl_basic_map_simplify(res);
2044 return isl_basic_map_finalize(res);
2047 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2048 enum isl_dim_type type, unsigned n)
2052 return isl_basic_map_insert(bmap, type,
2053 isl_basic_map_dim(bmap, type), n);
2056 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2057 enum isl_dim_type type, unsigned n)
2061 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2062 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2064 isl_basic_set_free(bset);
2068 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2069 enum isl_dim_type type, unsigned pos, unsigned n)
2076 map = isl_map_cow(map);
2080 map->dim = isl_dim_insert(map->dim, type, pos, n);
2084 for (i = 0; i < map->n; ++i) {
2085 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2096 __isl_give isl_map *isl_map_add(__isl_take isl_map *map,
2097 enum isl_dim_type type, unsigned n)
2101 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2104 __isl_give isl_set *isl_set_add(__isl_take isl_set *set,
2105 enum isl_dim_type type, unsigned n)
2109 isl_assert(set->ctx, type != isl_dim_in, goto error);
2110 return (isl_set *)isl_map_add((isl_map *)set, type, n);
2116 __isl_give isl_basic_map *isl_basic_map_move_dims(
2117 __isl_take isl_basic_map *bmap,
2118 enum isl_dim_type dst_type, unsigned dst_pos,
2119 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2122 struct isl_dim_map *dim_map;
2123 struct isl_basic_map *res;
2124 enum isl_dim_type t;
2125 unsigned total, off;
2132 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2135 if (dst_type == src_type && dst_pos == src_pos)
2138 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2140 if (pos(bmap->dim, dst_type) + dst_pos ==
2141 pos(bmap->dim, src_type) + src_pos +
2142 ((src_type < dst_type) ? n : 0)) {
2143 bmap = isl_basic_map_cow(bmap);
2147 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2148 src_type, src_pos, n);
2152 bmap = isl_basic_map_finalize(bmap);
2157 total = isl_basic_map_total_dim(bmap);
2158 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2161 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2162 unsigned size = isl_dim_size(bmap->dim, t);
2163 if (t == dst_type) {
2164 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2167 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2170 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2171 dst_pos, size - dst_pos, off);
2172 off += size - dst_pos;
2173 } else if (t == src_type) {
2174 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2177 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2178 src_pos + n, size - src_pos - n, off);
2179 off += size - src_pos - n;
2181 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2185 isl_dim_map_div(dim_map, bmap, off + n);
2187 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2188 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2189 bmap = add_constraints_dim_map(res, bmap, dim_map);
2191 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2192 src_type, src_pos, n);
2196 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2197 bmap = isl_basic_map_gauss(bmap, NULL);
2198 bmap = isl_basic_map_finalize(bmap);
2202 isl_basic_map_free(bmap);
2206 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2207 enum isl_dim_type dst_type, unsigned dst_pos,
2208 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2210 return (isl_basic_set *)isl_basic_map_move_dims(
2211 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2214 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2215 enum isl_dim_type dst_type, unsigned dst_pos,
2216 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2220 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2221 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2222 src_type, src_pos, n);
2228 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2229 enum isl_dim_type dst_type, unsigned dst_pos,
2230 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2239 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2242 if (dst_type == src_type && dst_pos == src_pos)
2245 isl_assert(map->ctx, dst_type != src_type, goto error);
2247 map = isl_map_cow(map);
2251 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2255 for (i = 0; i < map->n; ++i) {
2256 map->p[i] = isl_basic_map_move_dims(map->p[i],
2258 src_type, src_pos, n);
2269 /* Move the specified dimensions to the last columns right before
2270 * the divs. Don't change the dimension specification of bmap.
2271 * That's the responsibility of the caller.
2273 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2274 enum isl_dim_type type, unsigned first, unsigned n)
2276 struct isl_dim_map *dim_map;
2277 struct isl_basic_map *res;
2278 enum isl_dim_type t;
2279 unsigned total, off;
2283 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2286 total = isl_basic_map_total_dim(bmap);
2287 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2290 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2291 unsigned size = isl_dim_size(bmap->dim, t);
2293 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2296 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2297 first, n, total - bmap->n_div - n);
2298 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2299 first + n, size - (first + n), off);
2300 off += size - (first + n);
2302 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2306 isl_dim_map_div(dim_map, bmap, off + n);
2308 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2309 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2310 res = add_constraints_dim_map(res, bmap, dim_map);
2314 /* Turn the n dimensions of type type, starting at first
2315 * into existentially quantified variables.
2317 __isl_give isl_basic_map *isl_basic_map_project_out(
2318 __isl_take isl_basic_map *bmap,
2319 enum isl_dim_type type, unsigned first, unsigned n)
2332 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2333 return isl_basic_map_remove(bmap, type, first, n);
2335 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2338 bmap = move_last(bmap, type, first, n);
2339 bmap = isl_basic_map_cow(bmap);
2341 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2342 old = bmap->block2.data;
2343 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2344 (bmap->extra + n) * (1 + row_size));
2345 if (!bmap->block2.data)
2347 new_div = isl_alloc_array(ctx, isl_int *, bmap->extra + n);
2350 for (i = 0; i < n; ++i) {
2351 new_div[i] = bmap->block2.data +
2352 (bmap->extra + i) * (1 + row_size);
2353 isl_seq_clr(new_div[i], 1 + row_size);
2355 for (i = 0; i < bmap->extra; ++i)
2356 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2358 bmap->div = new_div;
2362 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2365 bmap = isl_basic_map_simplify(bmap);
2366 bmap = isl_basic_map_drop_redundant_divs(bmap);
2367 return isl_basic_map_finalize(bmap);
2369 isl_basic_map_free(bmap);
2373 /* Turn the n dimensions of type type, starting at first
2374 * into existentially quantified variables.
2376 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2377 enum isl_dim_type type, unsigned first, unsigned n)
2379 return (isl_basic_set *)isl_basic_map_project_out(
2380 (isl_basic_map *)bset, type, first, n);
2383 /* Turn the n dimensions of type type, starting at first
2384 * into existentially quantified variables.
2386 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2387 enum isl_dim_type type, unsigned first, unsigned n)
2397 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2399 map = isl_map_cow(map);
2403 map->dim = isl_dim_drop(map->dim, type, first, n);
2407 for (i = 0; i < map->n; ++i) {
2408 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2419 /* Turn the n dimensions of type type, starting at first
2420 * into existentially quantified variables.
2422 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2423 enum isl_dim_type type, unsigned first, unsigned n)
2425 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2428 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2432 for (i = 0; i < n; ++i) {
2433 j = isl_basic_map_alloc_div(bmap);
2436 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2440 isl_basic_map_free(bmap);
2444 struct isl_basic_map *isl_basic_map_apply_range(
2445 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2447 struct isl_dim *dim_result = NULL;
2448 struct isl_basic_map *bmap;
2449 unsigned n_in, n_out, n, nparam, total, pos;
2450 struct isl_dim_map *dim_map1, *dim_map2;
2452 if (!bmap1 || !bmap2)
2455 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2456 isl_dim_copy(bmap2->dim));
2458 n_in = isl_basic_map_n_in(bmap1);
2459 n_out = isl_basic_map_n_out(bmap2);
2460 n = isl_basic_map_n_out(bmap1);
2461 nparam = isl_basic_map_n_param(bmap1);
2463 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2464 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2465 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2466 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2467 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2468 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2469 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2470 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2471 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2472 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2473 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2475 bmap = isl_basic_map_alloc_dim(dim_result,
2476 bmap1->n_div + bmap2->n_div + n,
2477 bmap1->n_eq + bmap2->n_eq,
2478 bmap1->n_ineq + bmap2->n_ineq);
2479 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2480 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2481 bmap = add_divs(bmap, n);
2482 bmap = isl_basic_map_simplify(bmap);
2483 bmap = isl_basic_map_drop_redundant_divs(bmap);
2484 return isl_basic_map_finalize(bmap);
2486 isl_basic_map_free(bmap1);
2487 isl_basic_map_free(bmap2);
2491 struct isl_basic_set *isl_basic_set_apply(
2492 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2497 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2500 return (struct isl_basic_set *)
2501 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2503 isl_basic_set_free(bset);
2504 isl_basic_map_free(bmap);
2508 struct isl_basic_map *isl_basic_map_apply_domain(
2509 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2511 if (!bmap1 || !bmap2)
2514 isl_assert(bmap1->ctx,
2515 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2516 isl_assert(bmap1->ctx,
2517 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2520 bmap1 = isl_basic_map_reverse(bmap1);
2521 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2522 return isl_basic_map_reverse(bmap1);
2524 isl_basic_map_free(bmap1);
2525 isl_basic_map_free(bmap2);
2529 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2530 * A \cap B -> f(A) + f(B)
2532 struct isl_basic_map *isl_basic_map_sum(
2533 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2535 unsigned n_in, n_out, nparam, total, pos;
2536 struct isl_basic_map *bmap = NULL;
2537 struct isl_dim_map *dim_map1, *dim_map2;
2540 if (!bmap1 || !bmap2)
2543 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2546 nparam = isl_basic_map_n_param(bmap1);
2547 n_in = isl_basic_map_n_in(bmap1);
2548 n_out = isl_basic_map_n_out(bmap1);
2550 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2551 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2552 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2553 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2554 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2555 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2556 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2557 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2558 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2559 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2560 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2562 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2563 bmap1->n_div + bmap2->n_div + 2 * n_out,
2564 bmap1->n_eq + bmap2->n_eq + n_out,
2565 bmap1->n_ineq + bmap2->n_ineq);
2566 for (i = 0; i < n_out; ++i) {
2567 int j = isl_basic_map_alloc_equality(bmap);
2570 isl_seq_clr(bmap->eq[j], 1+total);
2571 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2572 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2573 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2575 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2576 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2577 bmap = add_divs(bmap, 2 * n_out);
2579 bmap = isl_basic_map_simplify(bmap);
2580 return isl_basic_map_finalize(bmap);
2582 isl_basic_map_free(bmap);
2583 isl_basic_map_free(bmap1);
2584 isl_basic_map_free(bmap2);
2588 /* Given two maps A -> f(A) and B -> g(B), construct a map
2589 * A \cap B -> f(A) + f(B)
2591 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2593 struct isl_map *result;
2599 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2601 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2602 map1->n * map2->n, 0);
2605 for (i = 0; i < map1->n; ++i)
2606 for (j = 0; j < map2->n; ++j) {
2607 struct isl_basic_map *part;
2608 part = isl_basic_map_sum(
2609 isl_basic_map_copy(map1->p[i]),
2610 isl_basic_map_copy(map2->p[j]));
2611 if (isl_basic_map_is_empty(part))
2612 isl_basic_map_free(part);
2614 result = isl_map_add_basic_map(result, part);
2627 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2628 __isl_take isl_set *set2)
2630 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2633 /* Given a basic map A -> f(A), construct A -> -f(A).
2635 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2640 bmap = isl_basic_map_cow(bmap);
2644 n = isl_basic_map_dim(bmap, isl_dim_out);
2645 off = isl_basic_map_offset(bmap, isl_dim_out);
2646 for (i = 0; i < bmap->n_eq; ++i)
2647 for (j = 0; j < n; ++j)
2648 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2649 for (i = 0; i < bmap->n_ineq; ++i)
2650 for (j = 0; j < n; ++j)
2651 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2652 for (i = 0; i < bmap->n_div; ++i)
2653 for (j = 0; j < n; ++j)
2654 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2655 return isl_basic_map_finalize(bmap);
2658 /* Given a map A -> f(A), construct A -> -f(A).
2660 struct isl_map *isl_map_neg(struct isl_map *map)
2664 map = isl_map_cow(map);
2668 for (i = 0; i < map->n; ++i) {
2669 map->p[i] = isl_basic_map_neg(map->p[i]);
2680 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
2682 return (isl_set *)isl_map_neg((isl_map *)set);
2685 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2686 * A -> floor(f(A)/d).
2688 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2691 unsigned n_in, n_out, nparam, total, pos;
2692 struct isl_basic_map *result = NULL;
2693 struct isl_dim_map *dim_map;
2699 nparam = isl_basic_map_n_param(bmap);
2700 n_in = isl_basic_map_n_in(bmap);
2701 n_out = isl_basic_map_n_out(bmap);
2703 total = nparam + n_in + n_out + bmap->n_div + n_out;
2704 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2705 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2706 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2707 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2708 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2710 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2711 bmap->n_div + n_out,
2712 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2713 result = add_constraints_dim_map(result, bmap, dim_map);
2714 result = add_divs(result, n_out);
2715 for (i = 0; i < n_out; ++i) {
2717 j = isl_basic_map_alloc_inequality(result);
2720 isl_seq_clr(result->ineq[j], 1+total);
2721 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2722 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2723 j = isl_basic_map_alloc_inequality(result);
2726 isl_seq_clr(result->ineq[j], 1+total);
2727 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2728 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2729 isl_int_sub_ui(result->ineq[j][0], d, 1);
2732 result = isl_basic_map_simplify(result);
2733 return isl_basic_map_finalize(result);
2735 isl_basic_map_free(result);
2739 /* Given a map A -> f(A) and an integer d, construct a map
2740 * A -> floor(f(A)/d).
2742 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2746 map = isl_map_cow(map);
2750 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2751 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2752 for (i = 0; i < map->n; ++i) {
2753 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2764 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2770 i = isl_basic_map_alloc_equality(bmap);
2773 nparam = isl_basic_map_n_param(bmap);
2774 n_in = isl_basic_map_n_in(bmap);
2775 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2776 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2777 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2778 return isl_basic_map_finalize(bmap);
2780 isl_basic_map_free(bmap);
2784 /* Add a constraints to "bmap" expressing i_pos < o_pos
2786 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2792 i = isl_basic_map_alloc_inequality(bmap);
2795 nparam = isl_basic_map_n_param(bmap);
2796 n_in = isl_basic_map_n_in(bmap);
2797 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2798 isl_int_set_si(bmap->ineq[i][0], -1);
2799 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2800 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2801 return isl_basic_map_finalize(bmap);
2803 isl_basic_map_free(bmap);
2807 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2809 static __isl_give isl_basic_map *var_less_or_equal(
2810 __isl_take isl_basic_map *bmap, unsigned pos)
2816 i = isl_basic_map_alloc_inequality(bmap);
2819 nparam = isl_basic_map_n_param(bmap);
2820 n_in = isl_basic_map_n_in(bmap);
2821 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2822 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2823 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2824 return isl_basic_map_finalize(bmap);
2826 isl_basic_map_free(bmap);
2830 /* Add a constraints to "bmap" expressing i_pos > o_pos
2832 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2838 i = isl_basic_map_alloc_inequality(bmap);
2841 nparam = isl_basic_map_n_param(bmap);
2842 n_in = isl_basic_map_n_in(bmap);
2843 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2844 isl_int_set_si(bmap->ineq[i][0], -1);
2845 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2846 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2847 return isl_basic_map_finalize(bmap);
2849 isl_basic_map_free(bmap);
2853 /* Add a constraint to "bmap" expressing i_pos >= o_pos
2855 static __isl_give isl_basic_map *var_more_or_equal(
2856 __isl_take isl_basic_map *bmap, unsigned pos)
2862 i = isl_basic_map_alloc_inequality(bmap);
2865 nparam = isl_basic_map_n_param(bmap);
2866 n_in = isl_basic_map_n_in(bmap);
2867 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2868 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2869 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2870 return isl_basic_map_finalize(bmap);
2872 isl_basic_map_free(bmap);
2876 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2879 struct isl_basic_map *bmap;
2880 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2883 for (i = 0; i < n_equal && bmap; ++i)
2884 bmap = var_equal(bmap, i);
2885 return isl_basic_map_finalize(bmap);
2888 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
2890 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2893 struct isl_basic_map *bmap;
2894 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2897 for (i = 0; i < pos && bmap; ++i)
2898 bmap = var_equal(bmap, i);
2900 bmap = var_less(bmap, pos);
2901 return isl_basic_map_finalize(bmap);
2904 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
2906 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
2907 __isl_take isl_dim *dim, unsigned pos)
2910 isl_basic_map *bmap;
2912 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2913 for (i = 0; i < pos; ++i)
2914 bmap = var_equal(bmap, i);
2915 bmap = var_less_or_equal(bmap, pos);
2916 return isl_basic_map_finalize(bmap);
2919 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2921 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2924 struct isl_basic_map *bmap;
2925 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2928 for (i = 0; i < pos && bmap; ++i)
2929 bmap = var_equal(bmap, i);
2931 bmap = var_more(bmap, pos);
2932 return isl_basic_map_finalize(bmap);
2935 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
2937 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
2938 __isl_take isl_dim *dim, unsigned pos)
2941 isl_basic_map *bmap;
2943 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2944 for (i = 0; i < pos; ++i)
2945 bmap = var_equal(bmap, i);
2946 bmap = var_more_or_equal(bmap, pos);
2947 return isl_basic_map_finalize(bmap);
2950 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
2951 unsigned n, int equal)
2953 struct isl_map *map;
2956 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
2958 for (i = 0; i + 1 < n; ++i)
2959 map = isl_map_add_basic_map(map,
2960 isl_basic_map_less_at(isl_dim_copy(dims), i));
2963 map = isl_map_add_basic_map(map,
2964 isl_basic_map_less_or_equal_at(dims, n - 1));
2966 map = isl_map_add_basic_map(map,
2967 isl_basic_map_less_at(dims, n - 1));
2974 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
2978 return map_lex_lte_first(dims, dims->n_out, equal);
2981 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
2983 return map_lex_lte_first(dim, n, 0);
2986 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
2988 return map_lex_lte_first(dim, n, 1);
2991 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
2993 return map_lex_lte(isl_dim_map(set_dim), 0);
2996 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
2998 return map_lex_lte(isl_dim_map(set_dim), 1);
3001 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3002 unsigned n, int equal)
3004 struct isl_map *map;
3007 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3009 for (i = 0; i + 1 < n; ++i)
3010 map = isl_map_add_basic_map(map,
3011 isl_basic_map_more_at(isl_dim_copy(dims), i));
3014 map = isl_map_add_basic_map(map,
3015 isl_basic_map_more_or_equal_at(dims, n - 1));
3017 map = isl_map_add_basic_map(map,
3018 isl_basic_map_more_at(dims, n - 1));
3025 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3029 return map_lex_gte_first(dims, dims->n_out, equal);
3032 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3034 return map_lex_gte_first(dim, n, 0);
3037 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3039 return map_lex_gte_first(dim, n, 1);
3042 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3044 return map_lex_gte(isl_dim_map(set_dim), 0);
3047 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3049 return map_lex_gte(isl_dim_map(set_dim), 1);
3052 struct isl_basic_map *isl_basic_map_from_basic_set(
3053 struct isl_basic_set *bset, struct isl_dim *dim)
3055 struct isl_basic_map *bmap;
3057 bset = isl_basic_set_cow(bset);
3061 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3062 isl_dim_free(bset->dim);
3063 bmap = (struct isl_basic_map *) bset;
3065 return isl_basic_map_finalize(bmap);
3067 isl_basic_set_free(bset);
3072 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3076 if (bmap->dim->n_in == 0)
3077 return (struct isl_basic_set *)bmap;
3078 bmap = isl_basic_map_cow(bmap);
3081 bmap->dim = isl_dim_cow(bmap->dim);
3084 bmap->dim->n_out += bmap->dim->n_in;
3085 bmap->dim->n_in = 0;
3086 bmap = isl_basic_map_finalize(bmap);
3087 return (struct isl_basic_set *)bmap;
3089 isl_basic_map_free(bmap);
3093 /* For a div d = floor(f/m), add the constraints
3096 * -(f-(n-1)) + m d >= 0
3098 * Note that the second constraint is the negation of
3102 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3103 unsigned pos, isl_int *div)
3106 unsigned total = isl_basic_map_total_dim(bmap);
3108 i = isl_basic_map_alloc_inequality(bmap);
3111 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3112 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3114 j = isl_basic_map_alloc_inequality(bmap);
3117 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3118 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3119 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3123 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3124 unsigned pos, isl_int *div)
3126 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3130 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3132 unsigned total = isl_basic_map_total_dim(bmap);
3133 unsigned div_pos = total - bmap->n_div + div;
3135 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3139 struct isl_basic_set *isl_basic_map_underlying_set(
3140 struct isl_basic_map *bmap)
3144 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
3145 return (struct isl_basic_set *)bmap;
3146 bmap = isl_basic_map_cow(bmap);
3149 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3152 bmap->extra -= bmap->n_div;
3154 bmap = isl_basic_map_finalize(bmap);
3155 return (struct isl_basic_set *)bmap;
3160 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3161 __isl_take isl_basic_set *bset)
3163 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3166 struct isl_basic_map *isl_basic_map_overlying_set(
3167 struct isl_basic_set *bset, struct isl_basic_map *like)
3169 struct isl_basic_map *bmap;
3170 struct isl_ctx *ctx;
3177 isl_assert(ctx, bset->n_div == 0, goto error);
3178 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3179 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3181 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3182 isl_basic_map_free(like);
3183 return (struct isl_basic_map *)bset;
3185 bset = isl_basic_set_cow(bset);
3188 total = bset->dim->n_out + bset->extra;
3189 bmap = (struct isl_basic_map *)bset;
3190 isl_dim_free(bmap->dim);
3191 bmap->dim = isl_dim_copy(like->dim);
3194 bmap->n_div = like->n_div;
3195 bmap->extra += like->n_div;
3198 ltotal = total - bmap->extra + like->extra;
3201 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3202 bmap->extra * (1 + 1 + total));
3203 if (isl_blk_is_error(bmap->block2))
3205 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3209 for (i = 0; i < bmap->extra; ++i)
3210 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3211 for (i = 0; i < like->n_div; ++i) {
3212 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3213 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3215 bmap = isl_basic_map_extend_constraints(bmap,
3216 0, 2 * like->n_div);
3217 for (i = 0; i < like->n_div; ++i) {
3218 if (isl_int_is_zero(bmap->div[i][0]))
3220 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3224 isl_basic_map_free(like);
3225 bmap = isl_basic_map_simplify(bmap);
3226 bmap = isl_basic_map_finalize(bmap);
3229 isl_basic_map_free(like);
3230 isl_basic_set_free(bset);
3234 struct isl_basic_set *isl_basic_set_from_underlying_set(
3235 struct isl_basic_set *bset, struct isl_basic_set *like)
3237 return (struct isl_basic_set *)
3238 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3241 struct isl_set *isl_set_from_underlying_set(
3242 struct isl_set *set, struct isl_basic_set *like)
3248 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3250 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3251 isl_basic_set_free(like);
3254 set = isl_set_cow(set);
3257 for (i = 0; i < set->n; ++i) {
3258 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3259 isl_basic_set_copy(like));
3263 isl_dim_free(set->dim);
3264 set->dim = isl_dim_copy(like->dim);
3267 isl_basic_set_free(like);
3270 isl_basic_set_free(like);
3275 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3279 map = isl_map_cow(map);
3282 map->dim = isl_dim_cow(map->dim);
3286 for (i = 1; i < map->n; ++i)
3287 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3289 for (i = 0; i < map->n; ++i) {
3290 map->p[i] = (struct isl_basic_map *)
3291 isl_basic_map_underlying_set(map->p[i]);
3296 map->dim = isl_dim_underlying(map->dim, 0);
3298 isl_dim_free(map->dim);
3299 map->dim = isl_dim_copy(map->p[0]->dim);
3303 return (struct isl_set *)map;
3309 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3311 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3314 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3316 struct isl_basic_set *domain;
3321 n_in = isl_basic_map_n_in(bmap);
3322 n_out = isl_basic_map_n_out(bmap);
3323 domain = isl_basic_set_from_basic_map(bmap);
3324 return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3327 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3329 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3332 struct isl_set *isl_map_range(struct isl_map *map)
3335 struct isl_set *set;
3339 if (isl_map_dim(map, isl_dim_in) == 0)
3340 return (isl_set *)map;
3342 map = isl_map_cow(map);
3346 set = (struct isl_set *) map;
3347 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3350 for (i = 0; i < map->n; ++i) {
3351 set->p[i] = isl_basic_map_range(map->p[i]);
3355 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3356 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3363 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3366 struct isl_map *map = NULL;
3368 set = isl_set_cow(set);
3371 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3372 map = (struct isl_map *)set;
3373 for (i = 0; i < set->n; ++i) {
3374 map->p[i] = isl_basic_map_from_basic_set(
3375 set->p[i], isl_dim_copy(dim));
3379 isl_dim_free(map->dim);
3388 struct isl_map *isl_map_from_range(struct isl_set *set)
3390 return (struct isl_map *)set;
3393 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3395 return isl_map_reverse(isl_map_from_range(set));;
3398 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3399 __isl_take isl_set *range)
3401 return isl_map_product(isl_map_from_domain(domain),
3402 isl_map_from_range(range));
3405 struct isl_set *isl_set_from_map(struct isl_map *map)
3408 struct isl_set *set = NULL;
3412 map = isl_map_cow(map);
3415 map->dim = isl_dim_cow(map->dim);
3418 map->dim->n_out += map->dim->n_in;
3420 set = (struct isl_set *)map;
3421 for (i = 0; i < map->n; ++i) {
3422 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3432 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3434 struct isl_map *map;
3438 isl_assert(dim->ctx, n >= 0, return NULL);
3439 map = isl_alloc(dim->ctx, struct isl_map,
3440 sizeof(struct isl_map) +
3441 (n - 1) * sizeof(struct isl_basic_map *));
3445 map->ctx = dim->ctx;
3446 isl_ctx_ref(map->ctx);
3458 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3459 unsigned nparam, unsigned in, unsigned out, int n,
3462 struct isl_map *map;
3463 struct isl_dim *dims;
3465 dims = isl_dim_alloc(ctx, nparam, in, out);
3469 map = isl_map_alloc_dim(dims, n, flags);
3473 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3475 struct isl_basic_map *bmap;
3476 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3477 bmap = isl_basic_map_set_to_empty(bmap);
3481 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3483 struct isl_basic_set *bset;
3484 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3485 bset = isl_basic_set_set_to_empty(bset);
3489 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3491 struct isl_basic_map *bmap;
3494 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3495 bmap = isl_basic_map_set_to_empty(bmap);
3499 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3501 struct isl_basic_map *bmap;
3504 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3505 bmap = isl_basic_map_set_to_empty(bmap);
3509 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3511 struct isl_basic_set *bset;
3514 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3515 bset = isl_basic_set_set_to_empty(bset);
3519 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3521 struct isl_basic_map *bmap;
3522 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
3526 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
3528 struct isl_basic_set *bset;
3529 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
3533 __isl_give isl_basic_map *isl_basic_map_universe_like(
3534 __isl_keep isl_basic_map *model)
3538 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3541 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3545 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3548 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3549 __isl_keep isl_set *model)
3553 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3556 struct isl_map *isl_map_empty(struct isl_dim *dim)
3558 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3561 struct isl_map *isl_map_empty_like(struct isl_map *model)
3565 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3568 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
3572 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3575 struct isl_set *isl_set_empty(struct isl_dim *dim)
3577 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3580 struct isl_set *isl_set_empty_like(struct isl_set *model)
3584 return isl_set_empty(isl_dim_copy(model->dim));
3587 struct isl_map *isl_map_universe(struct isl_dim *dim)
3589 struct isl_map *map;
3592 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3593 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
3597 struct isl_set *isl_set_universe(struct isl_dim *dim)
3599 struct isl_set *set;
3602 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3603 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
3607 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3611 return isl_set_universe(isl_dim_copy(model->dim));
3614 struct isl_map *isl_map_dup(struct isl_map *map)
3617 struct isl_map *dup;
3621 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3622 for (i = 0; i < map->n; ++i)
3623 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
3627 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
3628 __isl_take isl_basic_map *bmap)
3632 if (isl_basic_map_fast_is_empty(bmap)) {
3633 isl_basic_map_free(bmap);
3636 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3637 isl_assert(map->ctx, map->n < map->size, goto error);
3638 map->p[map->n] = bmap;
3640 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3646 isl_basic_map_free(bmap);
3650 void isl_map_free(struct isl_map *map)
3660 isl_ctx_deref(map->ctx);
3661 for (i = 0; i < map->n; ++i)
3662 isl_basic_map_free(map->p[i]);
3663 isl_dim_free(map->dim);
3667 struct isl_map *isl_map_extend(struct isl_map *base,
3668 unsigned nparam, unsigned n_in, unsigned n_out)
3672 base = isl_map_cow(base);
3676 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3679 for (i = 0; i < base->n; ++i) {
3680 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3681 isl_dim_copy(base->dim), 0, 0, 0);
3691 struct isl_set *isl_set_extend(struct isl_set *base,
3692 unsigned nparam, unsigned dim)
3694 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3698 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3699 struct isl_basic_map *bmap, unsigned pos, int value)
3703 bmap = isl_basic_map_cow(bmap);
3704 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3705 j = isl_basic_map_alloc_equality(bmap);
3708 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3709 isl_int_set_si(bmap->eq[j][pos], -1);
3710 isl_int_set_si(bmap->eq[j][0], value);
3711 bmap = isl_basic_map_simplify(bmap);
3712 return isl_basic_map_finalize(bmap);
3714 isl_basic_map_free(bmap);
3718 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3719 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3723 bmap = isl_basic_map_cow(bmap);
3724 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3725 j = isl_basic_map_alloc_equality(bmap);
3728 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3729 isl_int_set_si(bmap->eq[j][pos], -1);
3730 isl_int_set(bmap->eq[j][0], value);
3731 bmap = isl_basic_map_simplify(bmap);
3732 return isl_basic_map_finalize(bmap);
3734 isl_basic_map_free(bmap);
3738 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3739 enum isl_dim_type type, unsigned pos, int value)
3743 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3744 return isl_basic_map_fix_pos_si(bmap,
3745 isl_basic_map_offset(bmap, type) + pos, value);
3747 isl_basic_map_free(bmap);
3751 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3752 enum isl_dim_type type, unsigned pos, isl_int value)
3756 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3757 return isl_basic_map_fix_pos(bmap,
3758 isl_basic_map_offset(bmap, type) + pos, value);
3760 isl_basic_map_free(bmap);
3764 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3765 enum isl_dim_type type, unsigned pos, int value)
3767 return (struct isl_basic_set *)
3768 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3772 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3773 enum isl_dim_type type, unsigned pos, isl_int value)
3775 return (struct isl_basic_set *)
3776 isl_basic_map_fix((struct isl_basic_map *)bset,
3780 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3781 unsigned input, int value)
3783 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3786 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3787 unsigned dim, int value)
3789 return (struct isl_basic_set *)
3790 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3791 isl_dim_set, dim, value);
3794 struct isl_map *isl_map_fix_si(struct isl_map *map,
3795 enum isl_dim_type type, unsigned pos, int value)
3799 map = isl_map_cow(map);
3803 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3804 for (i = 0; i < map->n; ++i) {
3805 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3809 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3816 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
3817 enum isl_dim_type type, unsigned pos, int value)
3819 return (struct isl_set *)
3820 isl_map_fix_si((struct isl_map *)set, type, pos, value);
3823 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
3824 enum isl_dim_type type, unsigned pos, isl_int value)
3828 map = isl_map_cow(map);
3832 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3833 for (i = 0; i < map->n; ++i) {
3834 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
3838 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3845 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
3846 enum isl_dim_type type, unsigned pos, isl_int value)
3848 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
3851 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3852 unsigned input, int value)
3854 return isl_map_fix_si(map, isl_dim_in, input, value);
3857 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3859 return (struct isl_set *)
3860 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3863 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
3864 __isl_take isl_basic_map *bmap,
3865 enum isl_dim_type type, unsigned pos, int value)
3871 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3872 pos += isl_basic_map_offset(bmap, type);
3873 bmap = isl_basic_map_cow(bmap);
3874 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
3875 j = isl_basic_map_alloc_inequality(bmap);
3878 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
3879 isl_int_set_si(bmap->ineq[j][pos], 1);
3880 isl_int_set_si(bmap->ineq[j][0], -value);
3881 bmap = isl_basic_map_simplify(bmap);
3882 return isl_basic_map_finalize(bmap);
3884 isl_basic_map_free(bmap);
3888 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3889 unsigned dim, isl_int value)
3893 bset = isl_basic_set_cow(bset);
3894 bset = isl_basic_set_extend_constraints(bset, 0, 1);
3895 j = isl_basic_set_alloc_inequality(bset);
3898 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3899 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3900 isl_int_neg(bset->ineq[j][0], value);
3901 bset = isl_basic_set_simplify(bset);
3902 return isl_basic_set_finalize(bset);
3904 isl_basic_set_free(bset);
3908 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
3909 enum isl_dim_type type, unsigned pos, int value)
3913 map = isl_map_cow(map);
3917 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3918 for (i = 0; i < map->n; ++i) {
3919 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
3924 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3931 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
3932 enum isl_dim_type type, unsigned pos, int value)
3934 return (struct isl_set *)
3935 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
3938 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3943 set = isl_set_cow(set);
3947 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3948 for (i = 0; i < set->n; ++i) {
3949 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3959 struct isl_map *isl_map_reverse(struct isl_map *map)
3963 map = isl_map_cow(map);
3967 map->dim = isl_dim_reverse(map->dim);
3970 for (i = 0; i < map->n; ++i) {
3971 map->p[i] = isl_basic_map_reverse(map->p[i]);
3975 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3982 static struct isl_map *isl_basic_map_partial_lexopt(
3983 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3984 struct isl_set **empty, int max)
3988 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
3989 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3991 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3993 isl_basic_map_free(bmap);
3994 isl_basic_set_free(dom);
4000 struct isl_map *isl_basic_map_partial_lexmax(
4001 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4002 struct isl_set **empty)
4004 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4007 struct isl_map *isl_basic_map_partial_lexmin(
4008 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4009 struct isl_set **empty)
4011 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4014 struct isl_set *isl_basic_set_partial_lexmin(
4015 struct isl_basic_set *bset, struct isl_basic_set *dom,
4016 struct isl_set **empty)
4018 return (struct isl_set *)
4019 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4023 struct isl_set *isl_basic_set_partial_lexmax(
4024 struct isl_basic_set *bset, struct isl_basic_set *dom,
4025 struct isl_set **empty)
4027 return (struct isl_set *)
4028 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4032 /* Given a basic map "bmap", compute the lexicograhically minimal
4033 * (or maximal) image element for each domain element in dom.
4034 * Set *empty to those elements in dom that do not have an image element.
4036 * We first make sure the basic sets in dom are disjoint and then
4037 * simply collect the results over each of the basic sets separately.
4038 * We could probably improve the efficiency a bit by moving the union
4039 * domain down into the parametric integer programming.
4041 static __isl_give isl_map *basic_map_partial_lexopt(
4042 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4043 __isl_give isl_set **empty, int max)
4046 struct isl_map *res;
4048 dom = isl_set_make_disjoint(dom);
4052 if (isl_set_fast_is_empty(dom)) {
4053 res = isl_map_empty_like_basic_map(bmap);
4054 *empty = isl_set_empty_like(dom);
4056 isl_basic_map_free(bmap);
4060 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4061 isl_basic_set_copy(dom->p[0]), empty, max);
4063 for (i = 1; i < dom->n; ++i) {
4064 struct isl_map *res_i;
4065 struct isl_set *empty_i;
4067 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4068 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4070 res = isl_map_union_disjoint(res, res_i);
4071 *empty = isl_set_union_disjoint(*empty, empty_i);
4075 isl_basic_map_free(bmap);
4080 isl_basic_map_free(bmap);
4084 /* Given a map "map", compute the lexicograhically minimal
4085 * (or maximal) image element for each domain element in dom.
4086 * Set *empty to those elements in dom that do not have an image element.
4088 * We first compute the lexicographically minimal or maximal element
4089 * in the first basic map. This results in a partial solution "res"
4090 * and a subset "todo" of dom that still need to be handled.
4091 * We then consider each of the remaining maps in "map" and successively
4092 * improve both "res" and "todo".
4094 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4095 * Assume we are computing the lexicographical maximum.
4096 * We first intersect basic map i with a relation that maps elements
4097 * to elements that are lexicographically larger than the image elements
4098 * in res^k and the compute the maximum image element of this intersection.
4099 * The result ("better") corresponds to those image elements in basic map i
4100 * that are better than what we had before. The remainder ("keep") are the
4101 * domain elements for which the image element in res_k was better.
4102 * We also compute the lexicographical maximum of basic map i in todo^k.
4103 * res^i is the result of the operation + better + those elements in
4104 * res^k that we should keep
4105 * todo^i is the remainder of the maximum operation on todo^k.
4107 static __isl_give isl_map *isl_map_partial_lexopt(
4108 __isl_take isl_map *map, __isl_take isl_set *dom,
4109 __isl_give isl_set **empty, int max)
4112 struct isl_map *res;
4113 struct isl_set *todo;
4118 if (isl_map_fast_is_empty(map)) {
4126 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4127 isl_set_copy(dom), &todo, max);
4129 for (i = 1; i < map->n; ++i) {
4131 struct isl_map *better;
4132 struct isl_set *keep;
4133 struct isl_map *res_i;
4134 struct isl_set *todo_i;
4135 struct isl_dim *dim = isl_map_get_dim(res);
4137 dim = isl_dim_range(dim);
4139 lt = isl_map_lex_lt(dim);
4141 lt = isl_map_lex_gt(dim);
4142 lt = isl_map_apply_range(isl_map_copy(res), lt);
4143 lt = isl_map_intersect(lt,
4144 isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
4145 better = isl_map_partial_lexopt(lt,
4146 isl_map_domain(isl_map_copy(res)),
4149 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4150 todo, &todo_i, max);
4152 res = isl_map_intersect_domain(res, keep);
4153 res = isl_map_union_disjoint(res, res_i);
4154 res = isl_map_union_disjoint(res, better);
4175 __isl_give isl_map *isl_map_partial_lexmax(
4176 __isl_take isl_map *map, __isl_take isl_set *dom,
4177 __isl_give isl_set **empty)
4179 return isl_map_partial_lexopt(map, dom, empty, 1);
4182 __isl_give isl_map *isl_map_partial_lexmin(
4183 __isl_take isl_map *map, __isl_take isl_set *dom,
4184 __isl_give isl_set **empty)
4186 return isl_map_partial_lexopt(map, dom, empty, 0);
4189 __isl_give isl_set *isl_set_partial_lexmin(
4190 __isl_take isl_set *set, __isl_take isl_set *dom,
4191 __isl_give isl_set **empty)
4193 return (struct isl_set *)
4194 isl_map_partial_lexmin((struct isl_map *)set,
4198 __isl_give isl_set *isl_set_partial_lexmax(
4199 __isl_take isl_set *set, __isl_take isl_set *dom,
4200 __isl_give isl_set **empty)
4202 return (struct isl_set *)
4203 isl_map_partial_lexmax((struct isl_map *)set,
4207 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4209 struct isl_basic_set *dom = NULL;
4210 struct isl_dim *dom_dim;
4214 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4215 dom = isl_basic_set_universe(dom_dim);
4216 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4218 isl_basic_map_free(bmap);
4222 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4224 return isl_basic_map_lexopt(bmap, 0);
4227 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4229 return isl_basic_map_lexopt(bmap, 1);
4232 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4234 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4237 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4239 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4242 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4244 struct isl_set *dom = NULL;
4245 struct isl_dim *dom_dim;
4249 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4250 dom = isl_set_universe(dom_dim);
4251 return isl_map_partial_lexopt(map, dom, NULL, max);
4257 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4259 return isl_map_lexopt(map, 0);
4262 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4264 return isl_map_lexopt(map, 1);
4267 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4269 return (isl_set *)isl_map_lexmin((isl_map *)set);
4272 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4274 return (isl_set *)isl_map_lexmax((isl_map *)set);
4277 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
4278 struct isl_dim *dim)
4285 for (i = 0; i < map->n; ++i) {
4286 isl_dim_free(map->p[i]->dim);
4287 map->p[i]->dim = isl_dim_copy(dim);
4289 isl_dim_free(map->dim);
4299 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
4300 struct isl_dim *dim)
4302 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
4305 /* Apply a preimage specified by "mat" on the parameters of "bset".
4306 * bset is assumed to have only parameters and divs.
4308 static struct isl_basic_set *basic_set_parameter_preimage(
4309 struct isl_basic_set *bset, struct isl_mat *mat)
4316 bset->dim = isl_dim_cow(bset->dim);
4320 nparam = isl_basic_set_dim(bset, isl_dim_param);
4322 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4324 bset->dim->nparam = 0;
4325 bset->dim->n_out = nparam;
4326 bset = isl_basic_set_preimage(bset, mat);
4328 bset->dim->nparam = bset->dim->n_out;
4329 bset->dim->n_out = 0;
4334 isl_basic_set_free(bset);
4338 /* Apply a preimage specified by "mat" on the parameters of "set".
4339 * set is assumed to have only parameters and divs.
4341 static struct isl_set *set_parameter_preimage(
4342 struct isl_set *set, struct isl_mat *mat)
4344 struct isl_dim *dim = NULL;
4350 dim = isl_dim_copy(set->dim);
4351 dim = isl_dim_cow(dim);
4355 nparam = isl_set_dim(set, isl_dim_param);
4357 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4360 dim->n_out = nparam;
4361 isl_set_reset_dim(set, dim);
4362 set = isl_set_preimage(set, mat);
4365 dim = isl_dim_copy(set->dim);
4366 dim = isl_dim_cow(dim);
4369 dim->nparam = dim->n_out;
4371 isl_set_reset_dim(set, dim);
4381 /* Intersect the basic set "bset" with the affine space specified by the
4382 * equalities in "eq".
4384 static struct isl_basic_set *basic_set_append_equalities(
4385 struct isl_basic_set *bset, struct isl_mat *eq)
4393 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4398 len = 1 + isl_dim_total(bset->dim) + bset->extra;
4399 for (i = 0; i < eq->n_row; ++i) {
4400 k = isl_basic_set_alloc_equality(bset);
4403 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4404 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4408 bset = isl_basic_set_gauss(bset, NULL);
4409 bset = isl_basic_set_finalize(bset);
4414 isl_basic_set_free(bset);
4418 /* Intersect the set "set" with the affine space specified by the
4419 * equalities in "eq".
4421 static struct isl_set *set_append_equalities(struct isl_set *set,
4429 for (i = 0; i < set->n; ++i) {
4430 set->p[i] = basic_set_append_equalities(set->p[i],
4443 /* Project the given basic set onto its parameter domain, possibly introducing
4444 * new, explicit, existential variables in the constraints.
4445 * The input has parameters and (possibly implicit) existential variables.
4446 * The output has the same parameters, but only
4447 * explicit existentially quantified variables.
4449 * The actual projection is performed by pip, but pip doesn't seem
4450 * to like equalities very much, so we first remove the equalities
4451 * among the parameters by performing a variable compression on
4452 * the parameters. Afterward, an inverse transformation is performed
4453 * and the equalities among the parameters are inserted back in.
4455 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4459 struct isl_mat *T, *T2;
4460 struct isl_set *set;
4461 unsigned nparam, n_div;
4463 bset = isl_basic_set_cow(bset);
4467 if (bset->n_eq == 0)
4468 return isl_basic_set_lexmin(bset);
4470 isl_basic_set_gauss(bset, NULL);
4472 nparam = isl_basic_set_dim(bset, isl_dim_param);
4473 n_div = isl_basic_set_dim(bset, isl_dim_div);
4475 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4476 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4479 if (i == bset->n_eq)
4480 return isl_basic_set_lexmin(bset);
4482 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4484 eq = isl_mat_cow(eq);
4485 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4486 if (T && T->n_col == 0) {
4490 bset = isl_basic_set_set_to_empty(bset);
4491 return isl_set_from_basic_set(bset);
4493 bset = basic_set_parameter_preimage(bset, T);
4495 set = isl_basic_set_lexmin(bset);
4496 set = set_parameter_preimage(set, T2);
4497 set = set_append_equalities(set, eq);
4501 /* Compute an explicit representation for all the existentially
4502 * quantified variables.
4503 * The input and output dimensions are first turned into parameters.
4504 * compute_divs then returns a map with the same parameters and
4505 * no input or output dimensions and the dimension specification
4506 * is reset to that of the input.
4508 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4510 struct isl_basic_set *bset;
4511 struct isl_set *set;
4512 struct isl_map *map;
4513 struct isl_dim *dim, *orig_dim = NULL;
4518 bmap = isl_basic_map_cow(bmap);
4522 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4523 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4524 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4525 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4529 orig_dim = bmap->dim;
4531 bset = (struct isl_basic_set *)bmap;
4533 set = parameter_compute_divs(bset);
4534 map = (struct isl_map *)set;
4535 map = isl_map_reset_dim(map, orig_dim);
4539 isl_basic_map_free(bmap);
4543 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4551 off = isl_dim_total(bmap->dim);
4552 for (i = 0; i < bmap->n_div; ++i) {
4553 if (isl_int_is_zero(bmap->div[i][0]))
4555 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
4561 static int map_divs_known(__isl_keep isl_map *map)
4568 for (i = 0; i < map->n; ++i) {
4569 int known = basic_map_divs_known(map->p[i]);
4577 /* If bmap contains any unknown divs, then compute explicit
4578 * expressions for them. However, this computation may be
4579 * quite expensive, so first try to remove divs that aren't
4582 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
4586 struct isl_map *map;
4588 known = basic_map_divs_known(bmap);
4592 return isl_map_from_basic_map(bmap);
4594 bmap = isl_basic_map_drop_redundant_divs(bmap);
4596 known = basic_map_divs_known(bmap);
4600 return isl_map_from_basic_map(bmap);
4602 map = compute_divs(bmap);
4605 isl_basic_map_free(bmap);
4609 struct isl_map *isl_map_compute_divs(struct isl_map *map)
4613 struct isl_map *res;
4620 known = map_divs_known(map);
4628 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
4629 for (i = 1 ; i < map->n; ++i) {
4631 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
4632 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4633 res = isl_map_union_disjoint(res, r2);
4635 res = isl_map_union(res, r2);
4642 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
4644 return (struct isl_set *)
4645 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
4648 struct isl_set *isl_set_compute_divs(struct isl_set *set)
4650 return (struct isl_set *)
4651 isl_map_compute_divs((struct isl_map *)set);
4654 struct isl_set *isl_map_domain(struct isl_map *map)
4657 struct isl_set *set;
4662 map = isl_map_cow(map);
4666 set = (struct isl_set *)map;
4667 set->dim = isl_dim_domain(set->dim);
4670 for (i = 0; i < map->n; ++i) {
4671 set->p[i] = isl_basic_map_domain(map->p[i]);
4675 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4676 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4683 struct isl_map *isl_map_union_disjoint(
4684 struct isl_map *map1, struct isl_map *map2)
4688 struct isl_map *map = NULL;
4702 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4704 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4705 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4706 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4708 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
4709 map1->n + map2->n, flags);
4712 for (i = 0; i < map1->n; ++i) {
4713 map = isl_map_add_basic_map(map,
4714 isl_basic_map_copy(map1->p[i]));
4718 for (i = 0; i < map2->n; ++i) {
4719 map = isl_map_add_basic_map(map,
4720 isl_basic_map_copy(map2->p[i]));
4734 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
4736 map1 = isl_map_union_disjoint(map1, map2);
4740 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
4744 struct isl_set *isl_set_union_disjoint(
4745 struct isl_set *set1, struct isl_set *set2)
4747 return (struct isl_set *)
4748 isl_map_union_disjoint(
4749 (struct isl_map *)set1, (struct isl_map *)set2);
4752 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
4754 return (struct isl_set *)
4755 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
4758 struct isl_map *isl_map_intersect_range(
4759 struct isl_map *map, struct isl_set *set)
4762 struct isl_map *result;
4768 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
4769 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
4770 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4772 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
4773 map->n * set->n, flags);
4776 for (i = 0; i < map->n; ++i)
4777 for (j = 0; j < set->n; ++j) {
4778 result = isl_map_add_basic_map(result,
4779 isl_basic_map_intersect_range(
4780 isl_basic_map_copy(map->p[i]),
4781 isl_basic_set_copy(set->p[j])));
4794 struct isl_map *isl_map_intersect_domain(
4795 struct isl_map *map, struct isl_set *set)
4797 return isl_map_reverse(
4798 isl_map_intersect_range(isl_map_reverse(map), set));
4801 struct isl_map *isl_map_apply_domain(
4802 struct isl_map *map1, struct isl_map *map2)
4806 map1 = isl_map_reverse(map1);
4807 map1 = isl_map_apply_range(map1, map2);
4808 return isl_map_reverse(map1);
4815 struct isl_map *isl_map_apply_range(
4816 struct isl_map *map1, struct isl_map *map2)
4818 struct isl_dim *dim_result;
4819 struct isl_map *result;
4825 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
4826 isl_dim_copy(map2->dim));
4828 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
4831 for (i = 0; i < map1->n; ++i)
4832 for (j = 0; j < map2->n; ++j) {
4833 result = isl_map_add_basic_map(result,
4834 isl_basic_map_apply_range(
4835 isl_basic_map_copy(map1->p[i]),
4836 isl_basic_map_copy(map2->p[j])));
4842 if (result && result->n <= 1)
4843 ISL_F_SET(result, ISL_MAP_DISJOINT);
4852 * returns range - domain
4854 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
4857 struct isl_basic_set *bset;
4864 dim = isl_basic_map_n_in(bmap);
4865 nparam = isl_basic_map_n_param(bmap);
4866 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
4867 bset = isl_basic_set_from_basic_map(bmap);
4868 bset = isl_basic_set_cow(bset);
4869 dims = isl_basic_set_get_dim(bset);
4870 dims = isl_dim_add(dims, isl_dim_set, dim);
4871 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
4872 bset = isl_basic_set_swap_vars(bset, 2*dim);
4873 for (i = 0; i < dim; ++i) {
4874 int j = isl_basic_map_alloc_equality(
4875 (struct isl_basic_map *)bset);
4878 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
4879 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
4880 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
4881 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
4883 return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
4885 isl_basic_map_free(bmap);
4890 * returns range - domain
4892 struct isl_set *isl_map_deltas(struct isl_map *map)
4896 struct isl_set *result;
4901 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
4902 dim = isl_map_get_dim(map);
4903 dim = isl_dim_domain(dim);
4904 result = isl_set_alloc_dim(dim, map->n, map->flags);
4907 for (i = 0; i < map->n; ++i)
4908 result = isl_set_add_basic_set(result,
4909 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
4917 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
4919 struct isl_basic_map *bmap;
4927 nparam = dims->nparam;
4929 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
4933 for (i = 0; i < dim; ++i) {
4934 int j = isl_basic_map_alloc_equality(bmap);
4937 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
4938 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
4939 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
4941 return isl_basic_map_finalize(bmap);
4943 isl_basic_map_free(bmap);
4947 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
4949 struct isl_dim *dim = isl_dim_map(set_dim);
4952 return basic_map_identity(dim);
4955 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
4957 if (!model || !model->dim)
4959 isl_assert(model->ctx,
4960 model->dim->n_in == model->dim->n_out, return NULL);
4961 return basic_map_identity(isl_dim_copy(model->dim));
4964 static struct isl_map *map_identity(struct isl_dim *dim)
4966 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
4967 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
4970 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
4972 struct isl_dim *dim = isl_dim_map(set_dim);
4975 return map_identity(dim);
4978 struct isl_map *isl_map_identity_like(struct isl_map *model)
4980 if (!model || !model->dim)
4982 isl_assert(model->ctx,
4983 model->dim->n_in == model->dim->n_out, return NULL);
4984 return map_identity(isl_dim_copy(model->dim));
4987 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
4989 if (!model || !model->dim)
4991 isl_assert(model->ctx,
4992 model->dim->n_in == model->dim->n_out, return NULL);
4993 return map_identity(isl_dim_copy(model->dim));
4996 /* Construct a basic set with all set dimensions having only non-negative
4999 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5004 struct isl_basic_set *bset;
5008 nparam = dims->nparam;
5010 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5013 for (i = 0; i < dim; ++i) {
5014 int k = isl_basic_set_alloc_inequality(bset);
5017 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5018 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5022 isl_basic_set_free(bset);
5026 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5027 enum isl_dim_type type, unsigned first, unsigned n)
5030 isl_basic_set *nonneg = NULL;
5031 isl_basic_set *neg = NULL;
5038 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5040 for (i = 0; i < n; ++i) {
5044 nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5045 k = isl_basic_set_alloc_inequality(nonneg);
5048 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5049 isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
5051 neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5052 k = isl_basic_set_alloc_inequality(neg);
5055 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5056 isl_int_set_si(neg->ineq[k][0], -1);
5057 isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
5059 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5064 isl_basic_set_free(nonneg);
5065 isl_basic_set_free(neg);
5070 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5072 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5075 int isl_basic_map_is_subset(
5076 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5079 struct isl_map *map1;
5080 struct isl_map *map2;
5082 if (!bmap1 || !bmap2)
5085 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5086 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5088 is_subset = isl_map_is_subset(map1, map2);
5096 int isl_basic_map_is_equal(
5097 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5101 if (!bmap1 || !bmap2)
5103 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5106 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5110 int isl_basic_set_is_equal(
5111 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5113 return isl_basic_map_is_equal(
5114 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5117 int isl_map_is_empty(struct isl_map *map)
5124 for (i = 0; i < map->n; ++i) {
5125 is_empty = isl_basic_map_is_empty(map->p[i]);
5134 int isl_map_fast_is_empty(struct isl_map *map)
5139 int isl_set_fast_is_empty(struct isl_set *set)
5144 int isl_set_is_empty(struct isl_set *set)
5146 return isl_map_is_empty((struct isl_map *)set);
5149 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
5155 is_subset = isl_map_is_subset(map1, map2);
5158 is_subset = isl_map_is_subset(map2, map1);
5162 int isl_basic_map_is_strict_subset(
5163 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5167 if (!bmap1 || !bmap2)
5169 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5172 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5173 if (is_subset == -1)
5178 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
5184 is_subset = isl_map_is_subset(map1, map2);
5187 is_subset = isl_map_is_subset(map2, map1);
5188 if (is_subset == -1)
5193 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5195 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5198 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5202 return bmap->n_eq == 0 && bmap->n_ineq == 0;
5205 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5209 return bset->n_eq == 0 && bset->n_ineq == 0;
5212 int isl_map_fast_is_universe(__isl_keep isl_map *map)
5217 return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
5220 int isl_set_fast_is_universe(__isl_keep isl_set *set)
5222 return isl_map_fast_is_universe((isl_map *) set);
5225 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
5227 struct isl_basic_set *bset = NULL;
5228 struct isl_vec *sample = NULL;
5235 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
5238 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
5239 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
5240 copy = isl_basic_map_convex_hull(copy);
5241 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
5242 isl_basic_map_free(copy);
5246 total = 1 + isl_basic_map_total_dim(bmap);
5247 if (bmap->sample && bmap->sample->size == total) {
5248 int contains = isl_basic_map_contains(bmap, bmap->sample);
5254 isl_vec_free(bmap->sample);
5255 bmap->sample = NULL;
5256 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
5259 sample = isl_basic_set_sample_vec(bset);
5262 empty = sample->size == 0;
5263 isl_vec_free(bmap->sample);
5264 bmap->sample = sample;
5266 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
5271 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
5275 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
5278 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
5282 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
5285 int isl_basic_set_is_empty(struct isl_basic_set *bset)
5287 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
5290 struct isl_map *isl_basic_map_union(
5291 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5293 struct isl_map *map;
5294 if (!bmap1 || !bmap2)
5297 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
5299 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
5302 map = isl_map_add_basic_map(map, bmap1);
5303 map = isl_map_add_basic_map(map, bmap2);
5306 isl_basic_map_free(bmap1);
5307 isl_basic_map_free(bmap2);
5311 struct isl_set *isl_basic_set_union(
5312 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5314 return (struct isl_set *)isl_basic_map_union(
5315 (struct isl_basic_map *)bset1,
5316 (struct isl_basic_map *)bset2);
5319 /* Order divs such that any div only depends on previous divs */
5320 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
5323 unsigned off = isl_dim_total(bmap->dim);
5325 for (i = 0; i < bmap->n_div; ++i) {
5327 if (isl_int_is_zero(bmap->div[i][0]))
5329 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
5333 isl_basic_map_swap_div(bmap, i, i + pos);
5339 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
5341 return (struct isl_basic_set *)
5342 isl_basic_map_order_divs((struct isl_basic_map *)bset);
5345 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
5352 for (i = 0; i < map->n; ++i) {
5353 map->p[i] = isl_basic_map_order_divs(map->p[i]);
5364 /* Look for a div in dst that corresponds to the div "div" in src.
5365 * The divs before "div" in src and dst are assumed to be the same.
5367 * Returns -1 if no corresponding div was found and the position
5368 * of the corresponding div in dst otherwise.
5370 static int find_div(struct isl_basic_map *dst,
5371 struct isl_basic_map *src, unsigned div)
5375 unsigned total = isl_dim_total(src->dim);
5377 isl_assert(dst->ctx, div <= dst->n_div, return -1);
5378 for (i = div; i < dst->n_div; ++i)
5379 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
5380 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
5381 dst->n_div - div) == -1)
5386 struct isl_basic_map *isl_basic_map_align_divs(
5387 struct isl_basic_map *dst, struct isl_basic_map *src)
5390 unsigned total = isl_dim_total(src->dim);
5395 if (src->n_div == 0)
5398 for (i = 0; i < src->n_div; ++i)
5399 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
5401 src = isl_basic_map_order_divs(src);
5402 dst = isl_basic_map_cow(dst);
5403 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
5404 src->n_div, 0, 2 * src->n_div);
5407 for (i = 0; i < src->n_div; ++i) {
5408 int j = find_div(dst, src, i);
5410 j = isl_basic_map_alloc_div(dst);
5413 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
5414 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
5415 if (isl_basic_map_add_div_constraints(dst, j) < 0)
5419 isl_basic_map_swap_div(dst, i, j);
5423 isl_basic_map_free(dst);
5427 struct isl_basic_set *isl_basic_set_align_divs(
5428 struct isl_basic_set *dst, struct isl_basic_set *src)
5430 return (struct isl_basic_set *)isl_basic_map_align_divs(
5431 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
5434 struct isl_map *isl_map_align_divs(struct isl_map *map)
5442 map = isl_map_compute_divs(map);
5443 map = isl_map_cow(map);
5447 for (i = 1; i < map->n; ++i)
5448 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5449 for (i = 1; i < map->n; ++i)
5450 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5452 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5456 struct isl_set *isl_set_align_divs(struct isl_set *set)
5458 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5461 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5465 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5466 map = isl_map_intersect_domain(map, set);
5467 set = isl_map_range(map);
5475 /* There is no need to cow as removing empty parts doesn't change
5476 * the meaning of the set.
5478 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5485 for (i = map->n-1; i >= 0; --i) {
5486 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5488 isl_basic_map_free(map->p[i]);
5489 if (i != map->n-1) {
5490 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5491 map->p[i] = map->p[map->n-1];
5499 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
5501 return (struct isl_set *)
5502 isl_map_remove_empty_parts((struct isl_map *)set);
5505 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
5507 struct isl_basic_map *bmap;
5508 if (!map || map->n == 0)
5510 bmap = map->p[map->n-1];
5511 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
5512 return isl_basic_map_copy(bmap);
5515 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
5517 return (struct isl_basic_set *)
5518 isl_map_copy_basic_map((struct isl_map *)set);
5521 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
5522 __isl_keep isl_basic_map *bmap)
5528 for (i = map->n-1; i >= 0; --i) {
5529 if (map->p[i] != bmap)
5531 map = isl_map_cow(map);
5534 isl_basic_map_free(map->p[i]);
5535 if (i != map->n-1) {
5536 ISL_F_CLR(map, ISL_SET_NORMALIZED);
5537 map->p[i] = map->p[map->n-1];
5548 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
5549 struct isl_basic_set *bset)
5551 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
5552 (struct isl_basic_map *)bset);
5555 /* Given two basic sets bset1 and bset2, compute the maximal difference
5556 * between the values of dimension pos in bset1 and those in bset2
5557 * for any common value of the parameters and dimensions preceding pos.
5559 static enum isl_lp_result basic_set_maximal_difference_at(
5560 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
5561 int pos, isl_int *opt)
5563 struct isl_dim *dims;
5564 struct isl_basic_map *bmap1 = NULL;
5565 struct isl_basic_map *bmap2 = NULL;
5566 struct isl_ctx *ctx;
5567 struct isl_vec *obj;
5570 unsigned dim1, dim2;
5571 enum isl_lp_result res;
5573 if (!bset1 || !bset2)
5574 return isl_lp_error;
5576 nparam = isl_basic_set_n_param(bset1);
5577 dim1 = isl_basic_set_n_dim(bset1);
5578 dim2 = isl_basic_set_n_dim(bset2);
5579 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
5580 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
5581 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
5582 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
5583 if (!bmap1 || !bmap2)
5585 bmap1 = isl_basic_map_cow(bmap1);
5586 bmap1 = isl_basic_map_extend(bmap1, nparam,
5587 pos, (dim1 - pos) + (dim2 - pos),
5588 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
5589 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
5592 total = isl_basic_map_total_dim(bmap1);
5594 obj = isl_vec_alloc(ctx, 1 + total);
5595 isl_seq_clr(obj->block.data, 1 + total);
5596 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
5597 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
5600 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
5602 isl_basic_map_free(bmap1);
5606 isl_basic_map_free(bmap1);
5607 isl_basic_map_free(bmap2);
5608 return isl_lp_error;
5611 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5612 * for any common value of the parameters and dimensions preceding pos
5613 * in both basic sets, the values of dimension pos in bset1 are
5614 * smaller or larger than those in bset2.
5617 * 1 if bset1 follows bset2
5618 * -1 if bset1 precedes bset2
5619 * 0 if bset1 and bset2 are incomparable
5620 * -2 if some error occurred.
5622 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
5623 struct isl_basic_set *bset2, int pos)
5626 enum isl_lp_result res;
5631 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5633 if (res == isl_lp_empty)
5635 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5636 res == isl_lp_unbounded)
5638 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5647 /* Given two basic sets bset1 and bset2, check whether
5648 * for any common value of the parameters and dimensions preceding pos
5649 * there is a value of dimension pos in bset1 that is larger
5650 * than a value of the same dimension in bset2.
5653 * 1 if there exists such a pair
5654 * 0 if there is no such pair, but there is a pair of equal values
5656 * -2 if some error occurred.
5658 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
5659 __isl_keep isl_basic_set *bset2, int pos)
5662 enum isl_lp_result res;
5667 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5669 if (res == isl_lp_empty)
5671 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5672 res == isl_lp_unbounded)
5674 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5676 else if (res == isl_lp_ok)
5685 /* Given two sets set1 and set2, check whether
5686 * for any common value of the parameters and dimensions preceding pos
5687 * there is a value of dimension pos in set1 that is larger
5688 * than a value of the same dimension in set2.
5691 * 1 if there exists such a pair
5692 * 0 if there is no such pair, but there is a pair of equal values
5694 * -2 if some error occurred.
5696 int isl_set_follows_at(__isl_keep isl_set *set1,
5697 __isl_keep isl_set *set2, int pos)
5705 for (i = 0; i < set1->n; ++i)
5706 for (j = 0; j < set2->n; ++j) {
5708 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
5709 if (f == 1 || f == -2)
5718 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
5719 unsigned pos, isl_int *val)
5727 total = isl_basic_map_total_dim(bmap);
5728 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5729 for (; d+1 > pos; --d)
5730 if (!isl_int_is_zero(bmap->eq[i][1+d]))
5734 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5736 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5738 if (!isl_int_is_one(bmap->eq[i][1+d]))
5741 isl_int_neg(*val, bmap->eq[i][0]);
5747 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5748 unsigned pos, isl_int *val)
5760 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
5763 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
5764 for (i = 1; fixed == 1 && i < map->n; ++i) {
5765 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
5766 if (fixed == 1 && isl_int_ne(tmp, v))
5770 isl_int_set(*val, v);
5776 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5777 unsigned pos, isl_int *val)
5779 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
5783 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
5786 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
5789 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
5790 enum isl_dim_type type, unsigned pos, isl_int *val)
5792 if (pos >= isl_basic_map_dim(bmap, type))
5794 return isl_basic_map_fast_has_fixed_var(bmap,
5795 isl_basic_map_offset(bmap, type) - 1 + pos, val);
5798 int isl_map_fast_is_fixed(struct isl_map *map,
5799 enum isl_dim_type type, unsigned pos, isl_int *val)
5801 if (pos >= isl_map_dim(map, type))
5803 return isl_map_fast_has_fixed_var(map,
5804 map_offset(map, type) - 1 + pos, val);
5807 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5808 * then return this fixed value in *val.
5810 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
5813 return isl_basic_set_fast_has_fixed_var(bset,
5814 isl_basic_set_n_param(bset) + dim, val);
5817 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5818 * then return this fixed value in *val.
5820 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
5822 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
5825 /* Check if input variable in has fixed value and if so and if val is not NULL,
5826 * then return this fixed value in *val.
5828 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
5830 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
5833 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5834 * and if val is not NULL, then return this lower bound in *val.
5836 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
5837 unsigned dim, isl_int *val)
5839 int i, i_eq = -1, i_ineq = -1;
5846 total = isl_basic_set_total_dim(bset);
5847 nparam = isl_basic_set_n_param(bset);
5848 for (i = 0; i < bset->n_eq; ++i) {
5849 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
5855 for (i = 0; i < bset->n_ineq; ++i) {
5856 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
5858 if (i_eq != -1 || i_ineq != -1)
5862 if (i_eq == -1 && i_ineq == -1)
5864 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
5865 /* The coefficient should always be one due to normalization. */
5866 if (!isl_int_is_one(c[1+nparam+dim]))
5868 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
5870 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
5871 total - nparam - dim - 1) != -1)
5874 isl_int_neg(*val, c[0]);
5878 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
5879 unsigned dim, isl_int *val)
5891 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5895 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5897 for (i = 1; fixed == 1 && i < set->n; ++i) {
5898 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
5900 if (fixed == 1 && isl_int_ne(tmp, v))
5904 isl_int_set(*val, v);
5915 static int qsort_constraint_cmp(const void *p1, const void *p2)
5917 const struct constraint *c1 = (const struct constraint *)p1;
5918 const struct constraint *c2 = (const struct constraint *)p2;
5919 unsigned size = isl_min(c1->size, c2->size);
5920 return isl_seq_cmp(c1->c, c2->c, size);
5923 static struct isl_basic_map *isl_basic_map_sort_constraints(
5924 struct isl_basic_map *bmap)
5927 struct constraint *c;
5932 total = isl_basic_map_total_dim(bmap);
5933 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
5936 for (i = 0; i < bmap->n_ineq; ++i) {
5938 c[i].c = bmap->ineq[i];
5940 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
5941 for (i = 0; i < bmap->n_ineq; ++i)
5942 bmap->ineq[i] = c[i].c;
5946 isl_basic_map_free(bmap);
5950 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
5954 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
5956 bmap = isl_basic_map_convex_hull(bmap);
5957 bmap = isl_basic_map_sort_constraints(bmap);
5958 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
5962 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
5964 return (struct isl_basic_set *)isl_basic_map_normalize(
5965 (struct isl_basic_map *)bset);
5968 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
5969 const struct isl_basic_map *bmap2)
5976 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
5977 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
5978 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
5979 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5980 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
5981 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5982 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
5983 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5985 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
5987 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5989 if (bmap1->n_eq != bmap2->n_eq)
5990 return bmap1->n_eq - bmap2->n_eq;
5991 if (bmap1->n_ineq != bmap2->n_ineq)
5992 return bmap1->n_ineq - bmap2->n_ineq;
5993 if (bmap1->n_div != bmap2->n_div)
5994 return bmap1->n_div - bmap2->n_div;
5995 total = isl_basic_map_total_dim(bmap1);
5996 for (i = 0; i < bmap1->n_eq; ++i) {
5997 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6001 for (i = 0; i < bmap1->n_ineq; ++i) {
6002 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6006 for (i = 0; i < bmap1->n_div; ++i) {
6007 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6014 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
6015 struct isl_basic_map *bmap2)
6017 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6020 static int qsort_bmap_cmp(const void *p1, const void *p2)
6022 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6023 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6025 return isl_basic_map_fast_cmp(bmap1, bmap2);
6028 /* We normalize in place, but if anything goes wrong we need
6029 * to return NULL, so we need to make sure we don't change the
6030 * meaning of any possible other copies of map.
6032 struct isl_map *isl_map_normalize(struct isl_map *map)
6035 struct isl_basic_map *bmap;
6039 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6041 for (i = 0; i < map->n; ++i) {
6042 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6045 isl_basic_map_free(map->p[i]);
6048 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6049 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6050 map = isl_map_remove_empty_parts(map);
6053 for (i = map->n - 1; i >= 1; --i) {
6054 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6056 isl_basic_map_free(map->p[i-1]);
6057 for (j = i; j < map->n; ++j)
6058 map->p[j-1] = map->p[j];
6068 struct isl_set *isl_set_normalize(struct isl_set *set)
6070 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
6073 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
6083 if (!isl_dim_equal(map1->dim, map2->dim))
6086 map1 = isl_map_copy(map1);
6087 map2 = isl_map_copy(map2);
6088 map1 = isl_map_normalize(map1);
6089 map2 = isl_map_normalize(map2);
6092 equal = map1->n == map2->n;
6093 for (i = 0; equal && i < map1->n; ++i) {
6094 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
6107 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
6109 return isl_map_fast_is_equal((struct isl_map *)set1,
6110 (struct isl_map *)set2);
6113 /* Return an interval that ranges from min to max (inclusive)
6115 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
6116 isl_int min, isl_int max)
6119 struct isl_basic_set *bset = NULL;
6121 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
6125 k = isl_basic_set_alloc_inequality(bset);
6128 isl_int_set_si(bset->ineq[k][1], 1);
6129 isl_int_neg(bset->ineq[k][0], min);
6131 k = isl_basic_set_alloc_inequality(bset);
6134 isl_int_set_si(bset->ineq[k][1], -1);
6135 isl_int_set(bset->ineq[k][0], max);
6139 isl_basic_set_free(bset);
6143 /* Return the Cartesian product of the basic sets in list (in the given order).
6145 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
6153 struct isl_basic_set *product = NULL;
6157 isl_assert(list->ctx, list->n > 0, goto error);
6158 isl_assert(list->ctx, list->p[0], goto error);
6159 nparam = isl_basic_set_n_param(list->p[0]);
6160 dim = isl_basic_set_n_dim(list->p[0]);
6161 extra = list->p[0]->n_div;
6162 n_eq = list->p[0]->n_eq;
6163 n_ineq = list->p[0]->n_ineq;
6164 for (i = 1; i < list->n; ++i) {
6165 isl_assert(list->ctx, list->p[i], goto error);
6166 isl_assert(list->ctx,
6167 nparam == isl_basic_set_n_param(list->p[i]), goto error);
6168 dim += isl_basic_set_n_dim(list->p[i]);
6169 extra += list->p[i]->n_div;
6170 n_eq += list->p[i]->n_eq;
6171 n_ineq += list->p[i]->n_ineq;
6173 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
6178 for (i = 0; i < list->n; ++i) {
6179 isl_basic_set_add_constraints(product,
6180 isl_basic_set_copy(list->p[i]), dim);
6181 dim += isl_basic_set_n_dim(list->p[i]);
6183 isl_basic_set_list_free(list);
6186 isl_basic_set_free(product);
6187 isl_basic_set_list_free(list);
6191 struct isl_basic_map *isl_basic_map_product(
6192 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6194 struct isl_dim *dim_result = NULL;
6195 struct isl_basic_map *bmap;
6196 unsigned in1, in2, out1, out2, nparam, total, pos;
6197 struct isl_dim_map *dim_map1, *dim_map2;
6199 if (!bmap1 || !bmap2)
6202 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
6203 bmap2->dim, isl_dim_param), goto error);
6204 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
6205 isl_dim_copy(bmap2->dim));
6207 in1 = isl_basic_map_n_in(bmap1);
6208 in2 = isl_basic_map_n_in(bmap2);
6209 out1 = isl_basic_map_n_out(bmap1);
6210 out2 = isl_basic_map_n_out(bmap2);
6211 nparam = isl_basic_map_n_param(bmap1);
6213 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
6214 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
6215 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
6216 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
6217 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
6218 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
6219 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
6220 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
6221 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
6222 isl_dim_map_div(dim_map1, bmap1, pos += out2);
6223 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
6225 bmap = isl_basic_map_alloc_dim(dim_result,
6226 bmap1->n_div + bmap2->n_div,
6227 bmap1->n_eq + bmap2->n_eq,
6228 bmap1->n_ineq + bmap2->n_ineq);
6229 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
6230 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
6231 bmap = isl_basic_map_simplify(bmap);
6232 return isl_basic_map_finalize(bmap);
6234 isl_basic_map_free(bmap1);
6235 isl_basic_map_free(bmap2);
6239 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6241 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
6244 struct isl_map *result;
6250 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
6251 map2->dim, isl_dim_param), goto error);
6253 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6254 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6255 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6257 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
6258 isl_dim_copy(map2->dim)),
6259 map1->n * map2->n, flags);
6262 for (i = 0; i < map1->n; ++i)
6263 for (j = 0; j < map2->n; ++j) {
6264 struct isl_basic_map *part;
6265 part = isl_basic_map_product(
6266 isl_basic_map_copy(map1->p[i]),
6267 isl_basic_map_copy(map2->p[j]));
6268 if (isl_basic_map_is_empty(part))
6269 isl_basic_map_free(part);
6271 result = isl_map_add_basic_map(result, part);
6284 /* Given two set A and B, construct its Cartesian product A x B.
6286 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
6288 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
6289 (struct isl_map *)set2);
6292 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
6295 uint32_t hash = isl_hash_init();
6300 bset = isl_basic_set_copy(bset);
6301 bset = isl_basic_set_normalize(bset);
6304 total = isl_basic_set_total_dim(bset);
6305 isl_hash_byte(hash, bset->n_eq & 0xFF);
6306 for (i = 0; i < bset->n_eq; ++i) {
6308 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
6309 isl_hash_hash(hash, c_hash);
6311 isl_hash_byte(hash, bset->n_ineq & 0xFF);
6312 for (i = 0; i < bset->n_ineq; ++i) {
6314 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
6315 isl_hash_hash(hash, c_hash);
6317 isl_hash_byte(hash, bset->n_div & 0xFF);
6318 for (i = 0; i < bset->n_div; ++i) {
6320 if (isl_int_is_zero(bset->div[i][0]))
6322 isl_hash_byte(hash, i & 0xFF);
6323 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
6324 isl_hash_hash(hash, c_hash);
6326 isl_basic_set_free(bset);
6330 uint32_t isl_set_get_hash(struct isl_set *set)
6337 set = isl_set_copy(set);
6338 set = isl_set_normalize(set);
6342 hash = isl_hash_init();
6343 for (i = 0; i < set->n; ++i) {
6345 bset_hash = isl_basic_set_get_hash(set->p[i]);
6346 isl_hash_hash(hash, bset_hash);
6354 /* Check if the value for dimension dim is completely determined
6355 * by the values of the other parameters and variables.
6356 * That is, check if dimension dim is involved in an equality.
6358 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
6365 nparam = isl_basic_set_n_param(bset);
6366 for (i = 0; i < bset->n_eq; ++i)
6367 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
6372 /* Check if the value for dimension dim is completely determined
6373 * by the values of the other parameters and variables.
6374 * That is, check if dimension dim is involved in an equality
6375 * for each of the subsets.
6377 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
6383 for (i = 0; i < set->n; ++i) {
6385 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
6392 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
6393 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
6400 for (i = 0; i < map->n; ++i)
6401 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
6407 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
6408 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
6415 for (i = 0; i < set->n; ++i)
6416 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
6422 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
6424 struct isl_dim *dim;
6429 if (bset->n_div == 0)
6432 bset = isl_basic_set_cow(bset);
6436 dim = isl_basic_set_get_dim(bset);
6437 dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
6440 isl_dim_free(bset->dim);
6446 isl_basic_set_free(bset);
6450 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
6453 struct isl_dim *dim;
6456 set = isl_set_align_divs(set);
6460 if (set->n == 0 || set->p[0]->n_div == 0)
6463 set = isl_set_cow(set);
6467 n_div = set->p[0]->n_div;
6468 dim = isl_set_get_dim(set);
6469 dim = isl_dim_add(dim, isl_dim_set, n_div);
6472 isl_dim_free(set->dim);
6475 for (i = 0; i < set->n; ++i) {
6476 set->p[i] = isl_basic_set_lift(set->p[i]);
6487 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
6489 struct isl_dim *dim;
6490 struct isl_basic_map *bmap;
6497 set = isl_set_align_divs(set);
6502 dim = isl_set_get_dim(set);
6503 if (set->n == 0 || set->p[0]->n_div == 0) {
6505 return isl_map_identity(dim);
6508 n_div = set->p[0]->n_div;
6509 dim = isl_dim_map(dim);
6510 n_param = isl_dim_size(dim, isl_dim_param);
6511 n_set = isl_dim_size(dim, isl_dim_in);
6512 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
6513 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
6514 for (i = 0; i < n_set; ++i)
6515 bmap = var_equal(bmap, i);
6517 total = n_param + n_set + n_set + n_div;
6518 for (i = 0; i < n_div; ++i) {
6519 k = isl_basic_map_alloc_inequality(bmap);
6522 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
6523 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
6524 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
6525 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
6526 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
6527 set->p[0]->div[i][0]);
6529 l = isl_basic_map_alloc_inequality(bmap);
6532 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
6533 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
6534 set->p[0]->div[i][0]);
6535 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
6539 return isl_map_from_basic_map(bmap);
6542 isl_basic_map_free(bmap);
6546 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
6554 dim = isl_basic_set_total_dim(bset);
6555 size += bset->n_eq * (1 + dim);
6556 size += bset->n_ineq * (1 + dim);
6557 size += bset->n_div * (2 + dim);
6562 int isl_set_size(__isl_keep isl_set *set)
6570 for (i = 0; i < set->n; ++i)
6571 size += isl_basic_set_size(set->p[i]);
6576 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
6577 enum isl_dim_type type, unsigned pos)
6585 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
6587 pos += isl_basic_map_offset(bmap, type);
6589 for (i = 0; i < bmap->n_eq; ++i)
6590 if (!isl_int_is_zero(bmap->eq[i][pos]))
6594 for (i = 0; i < bmap->n_ineq; ++i) {
6595 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
6602 return lower && upper;
6605 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
6606 enum isl_dim_type type, unsigned pos)
6613 for (i = 0; i < map->n; ++i) {
6615 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
6616 if (bounded < 0 || !bounded)
6623 /* Return 1 if the specified dim is involved in both an upper bound
6624 * and a lower bound.
6626 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
6627 enum isl_dim_type type, unsigned pos)
6629 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
6632 /* For each of the "n" variables starting at "first", determine
6633 * the sign of the variable and put the results in the first "n"
6634 * elements of the array "signs".
6636 * 1 means that the variable is non-negative
6637 * -1 means that the variable is non-positive
6638 * 0 means the variable attains both positive and negative values.
6640 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
6641 unsigned first, unsigned n, int *signs)
6643 isl_vec *bound = NULL;
6644 struct isl_tab *tab = NULL;
6645 struct isl_tab_undo *snap;
6648 if (!bset || !signs)
6651 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
6652 tab = isl_tab_from_basic_set(bset);
6656 isl_seq_clr(bound->el, bound->size);
6657 isl_int_set_si(bound->el[0], -1);
6659 snap = isl_tab_snap(tab);
6660 for (i = 0; i < n; ++i) {
6663 isl_int_set_si(bound->el[1 + first + i], -1);
6664 if (isl_tab_add_ineq(tab, bound->el) < 0)
6667 isl_int_set_si(bound->el[1 + first + i], 0);
6668 if (isl_tab_rollback(tab, snap) < 0)
6676 isl_int_set_si(bound->el[1 + first + i], 1);
6677 if (isl_tab_add_ineq(tab, bound->el) < 0)
6680 isl_int_set_si(bound->el[1 + first + i], 0);
6681 if (isl_tab_rollback(tab, snap) < 0)
6684 signs[i] = empty ? -1 : 0;
6688 isl_vec_free(bound);
6692 isl_vec_free(bound);
6696 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
6697 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
6699 if (!bset || !signs)
6701 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
6704 first += pos(bset->dim, type) - 1;
6705 return isl_basic_set_vars_get_sign(bset, first, n, signs);