2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
18 #include "isl_equalities.h"
24 #include "isl_map_private.h"
25 #include "isl_map_piplib.h"
26 #include "isl_sample.h"
30 /* Maps dst positions to src positions */
36 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
39 struct isl_dim_map *dim_map;
40 dim_map = isl_alloc(ctx, struct isl_dim_map,
41 sizeof(struct isl_dim_map) + len * sizeof(int));
44 dim_map->len = 1 + len;
46 for (i = 0; i < len; ++i)
47 dim_map->pos[1 + i] = -1;
51 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
54 case isl_dim_param: return dim->nparam;
55 case isl_dim_in: return dim->n_in;
56 case isl_dim_out: return dim->n_out;
57 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
64 case isl_dim_param: return 1;
65 case isl_dim_in: return 1 + dim->nparam;
66 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
70 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
71 struct isl_dim *dim, enum isl_dim_type type,
72 unsigned first, unsigned n, unsigned dst_pos)
80 src_pos = pos(dim, type);
81 for (i = 0; i < n; ++i)
82 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
85 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
86 enum isl_dim_type type, unsigned dst_pos)
88 isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
91 static void isl_dim_map_div(struct isl_dim_map *dim_map,
92 struct isl_basic_map *bmap, unsigned dst_pos)
97 if (!dim_map || !bmap)
100 src_pos = 1 + isl_dim_total(bmap->dim);
101 for (i = 0; i < bmap->n_div; ++i)
102 dim_map->pos[1 + dst_pos + i] = src_pos + i;
105 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
109 for (i = 0; i < dim_map->len; ++i)
110 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
111 fprintf(stderr, "\n");
114 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
115 enum isl_dim_type type)
120 case isl_dim_out: return isl_dim_size(bmap->dim, type);
121 case isl_dim_div: return bmap->n_div;
122 case isl_dim_all: return isl_basic_map_total_dim(bmap);
126 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
128 return map ? n(map->dim, type) : 0;
131 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
133 return set ? n(set->dim, type) : 0;
136 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
137 enum isl_dim_type type)
139 struct isl_dim *dim = bmap->dim;
141 case isl_dim_param: return 1;
142 case isl_dim_in: return 1 + dim->nparam;
143 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
144 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
148 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
150 return pos(map->dim, type);
153 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
154 enum isl_dim_type type)
156 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
159 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
161 return bset->dim->n_out;
164 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
166 return bset->dim->nparam;
169 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
171 return isl_dim_total(bset->dim) + bset->n_div;
174 unsigned isl_set_n_dim(const struct isl_set *set)
176 return set->dim->n_out;
179 unsigned isl_set_n_param(const struct isl_set *set)
181 return set->dim->nparam;
184 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
186 return bmap->dim->n_in;
189 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
191 return bmap->dim->n_out;
194 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
196 return bmap->dim->nparam;
199 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
204 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
206 return isl_dim_total(bmap->dim) + bmap->n_div;
209 unsigned isl_map_n_in(const struct isl_map *map)
211 return map->dim->n_in;
214 unsigned isl_map_n_out(const struct isl_map *map)
216 return map->dim->n_out;
219 unsigned isl_map_n_param(const struct isl_map *map)
221 return map->dim->nparam;
224 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
226 return map->dim->n_in == set->dim->n_out &&
227 map->dim->nparam == set->dim->nparam;
230 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
231 struct isl_basic_set *bset)
233 return bmap->dim->n_in == bset->dim->n_out &&
234 bmap->dim->nparam == bset->dim->nparam;
237 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
238 struct isl_basic_set *bset)
240 return bmap->dim->n_out == bset->dim->n_out &&
241 bmap->dim->nparam == bset->dim->nparam;
244 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
248 return isl_dim_copy(bmap->dim);
251 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
255 return isl_dim_copy(bset->dim);
258 struct isl_dim *isl_map_get_dim(struct isl_map *map)
262 return isl_dim_copy(map->dim);
265 struct isl_dim *isl_set_get_dim(struct isl_set *set)
269 return isl_dim_copy(set->dim);
272 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
273 struct isl_basic_map *bmap, unsigned extra,
274 unsigned n_eq, unsigned n_ineq)
277 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
279 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
280 if (isl_blk_is_error(bmap->block)) {
285 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
287 isl_blk_free(ctx, bmap->block);
293 bmap->block2 = isl_blk_empty();
296 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
297 if (isl_blk_is_error(bmap->block2)) {
299 isl_blk_free(ctx, bmap->block);
304 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
306 isl_blk_free(ctx, bmap->block2);
308 isl_blk_free(ctx, bmap->block);
314 for (i = 0; i < n_ineq + n_eq; ++i)
315 bmap->ineq[i] = bmap->block.data + i * row_size;
317 for (i = 0; i < extra; ++i)
318 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
324 bmap->c_size = n_eq + n_ineq;
325 bmap->eq = bmap->ineq + n_ineq;
334 isl_basic_map_free(bmap);
338 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
339 unsigned nparam, unsigned dim, unsigned extra,
340 unsigned n_eq, unsigned n_ineq)
342 struct isl_basic_map *bmap;
343 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
344 return (struct isl_basic_set *)bmap;
347 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
348 unsigned extra, unsigned n_eq, unsigned n_ineq)
350 struct isl_basic_map *bmap;
353 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
354 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
355 return (struct isl_basic_set *)bmap;
358 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
359 unsigned extra, unsigned n_eq, unsigned n_ineq)
361 struct isl_basic_map *bmap;
365 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
370 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
376 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
377 unsigned nparam, unsigned in, unsigned out, unsigned extra,
378 unsigned n_eq, unsigned n_ineq)
380 struct isl_basic_map *bmap;
383 dim = isl_dim_alloc(ctx, nparam, in, out);
387 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
391 static void dup_constraints(
392 struct isl_basic_map *dst, struct isl_basic_map *src)
395 unsigned total = isl_basic_map_total_dim(src);
397 for (i = 0; i < src->n_eq; ++i) {
398 int j = isl_basic_map_alloc_equality(dst);
399 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
402 for (i = 0; i < src->n_ineq; ++i) {
403 int j = isl_basic_map_alloc_inequality(dst);
404 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
407 for (i = 0; i < src->n_div; ++i) {
408 int j = isl_basic_map_alloc_div(dst);
409 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
411 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
414 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
416 struct isl_basic_map *dup;
420 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
421 bmap->n_div, bmap->n_eq, bmap->n_ineq);
424 dup_constraints(dup, bmap);
425 dup->flags = bmap->flags;
426 dup->sample = isl_vec_copy(bmap->sample);
430 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
432 struct isl_basic_map *dup;
434 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
435 return (struct isl_basic_set *)dup;
438 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
443 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
447 return isl_basic_set_dup(bset);
450 struct isl_set *isl_set_copy(struct isl_set *set)
459 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
464 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
468 return isl_basic_map_dup(bmap);
471 struct isl_map *isl_map_copy(struct isl_map *map)
480 void isl_basic_map_free(struct isl_basic_map *bmap)
488 isl_ctx_deref(bmap->ctx);
490 isl_blk_free(bmap->ctx, bmap->block2);
492 isl_blk_free(bmap->ctx, bmap->block);
493 isl_vec_free(bmap->sample);
494 isl_dim_free(bmap->dim);
498 void isl_basic_set_free(struct isl_basic_set *bset)
500 isl_basic_map_free((struct isl_basic_map *)bset);
503 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
505 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
508 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
514 isl_assert(ctx, room_for_con(bmap, 1), return -1);
515 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
517 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
518 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
519 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
520 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
521 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
522 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
524 int j = isl_basic_map_alloc_inequality(bmap);
528 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
529 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
536 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
537 bmap->extra - bmap->n_div);
541 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
543 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
546 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
550 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
555 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
557 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
560 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
565 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
567 if (pos != bmap->n_eq - 1) {
569 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
570 bmap->eq[bmap->n_eq - 1] = t;
576 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
578 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
581 void isl_basic_map_inequality_to_equality(
582 struct isl_basic_map *bmap, unsigned pos)
587 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
588 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
593 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
594 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
595 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
596 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
599 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
601 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
604 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
610 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
611 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
612 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
613 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
614 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
615 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
616 1 + isl_basic_map_total_dim(bmap),
617 bmap->extra - bmap->n_div);
618 return bmap->n_ineq++;
621 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
623 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
626 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
630 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
635 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
637 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
640 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
645 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
647 if (pos != bmap->n_ineq - 1) {
649 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
650 bmap->ineq[bmap->n_ineq - 1] = t;
651 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
657 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
659 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
662 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
667 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
670 k = isl_basic_map_alloc_equality(bmap);
673 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
676 isl_basic_map_free(bmap);
680 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
683 return (isl_basic_set *)
684 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
687 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
692 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
695 k = isl_basic_map_alloc_inequality(bmap);
698 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
701 isl_basic_map_free(bmap);
705 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
708 return (isl_basic_set *)
709 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
712 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
716 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
717 isl_seq_clr(bmap->div[bmap->n_div] +
718 1 + 1 + isl_basic_map_total_dim(bmap),
719 bmap->extra - bmap->n_div);
720 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
721 return bmap->n_div++;
724 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
726 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
729 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
733 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
738 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
740 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
743 /* Copy constraint from src to dst, putting the vars of src at offset
744 * dim_off in dst and the divs of src at offset div_off in dst.
745 * If both sets are actually map, then dim_off applies to the input
748 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
749 struct isl_basic_map *src_map, isl_int *src,
750 unsigned in_off, unsigned out_off, unsigned div_off)
752 unsigned src_nparam = isl_basic_map_n_param(src_map);
753 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
754 unsigned src_in = isl_basic_map_n_in(src_map);
755 unsigned dst_in = isl_basic_map_n_in(dst_map);
756 unsigned src_out = isl_basic_map_n_out(src_map);
757 unsigned dst_out = isl_basic_map_n_out(dst_map);
758 isl_int_set(dst[0], src[0]);
759 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
760 if (dst_nparam > src_nparam)
761 isl_seq_clr(dst+1+src_nparam,
762 dst_nparam - src_nparam);
763 isl_seq_clr(dst+1+dst_nparam, in_off);
764 isl_seq_cpy(dst+1+dst_nparam+in_off,
766 isl_min(dst_in-in_off, src_in));
767 if (dst_in-in_off > src_in)
768 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
769 dst_in - in_off - src_in);
770 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
771 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
772 src+1+src_nparam+src_in,
773 isl_min(dst_out-out_off, src_out));
774 if (dst_out-out_off > src_out)
775 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
776 dst_out - out_off - src_out);
777 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
778 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
779 src+1+src_nparam+src_in+src_out,
780 isl_min(dst_map->extra-div_off, src_map->n_div));
781 if (dst_map->n_div-div_off > src_map->n_div)
782 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
783 div_off+src_map->n_div,
784 dst_map->n_div - div_off - src_map->n_div);
787 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
788 struct isl_basic_map *src_map, isl_int *src,
789 unsigned in_off, unsigned out_off, unsigned div_off)
791 isl_int_set(dst[0], src[0]);
792 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
795 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
796 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
801 if (!bmap1 || !bmap2)
804 div_off = bmap1->n_div;
806 for (i = 0; i < bmap2->n_eq; ++i) {
807 int i1 = isl_basic_map_alloc_equality(bmap1);
810 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
811 i_pos, o_pos, div_off);
814 for (i = 0; i < bmap2->n_ineq; ++i) {
815 int i1 = isl_basic_map_alloc_inequality(bmap1);
818 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
819 i_pos, o_pos, div_off);
822 for (i = 0; i < bmap2->n_div; ++i) {
823 int i1 = isl_basic_map_alloc_div(bmap1);
826 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
827 i_pos, o_pos, div_off);
830 isl_basic_map_free(bmap2);
835 isl_basic_map_free(bmap1);
836 isl_basic_map_free(bmap2);
840 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
841 struct isl_dim_map *dim_map)
845 for (i = 0; i < dim_map->len; ++i) {
846 if (dim_map->pos[i] < 0)
847 isl_int_set_si(dst[i], 0);
849 isl_int_set(dst[i], src[dim_map->pos[i]]);
853 static void copy_div_dim_map(isl_int *dst, isl_int *src,
854 struct isl_dim_map *dim_map)
856 isl_int_set(dst[0], src[0]);
857 copy_constraint_dim_map(dst+1, src+1, dim_map);
860 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
861 struct isl_basic_map *src, struct isl_dim_map *dim_map)
865 if (!src || !dst || !dim_map)
868 for (i = 0; i < src->n_eq; ++i) {
869 int i1 = isl_basic_map_alloc_equality(dst);
872 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
875 for (i = 0; i < src->n_ineq; ++i) {
876 int i1 = isl_basic_map_alloc_inequality(dst);
879 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
882 for (i = 0; i < src->n_div; ++i) {
883 int i1 = isl_basic_map_alloc_div(dst);
886 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
890 isl_basic_map_free(src);
895 isl_basic_map_free(src);
896 isl_basic_map_free(dst);
900 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
901 struct isl_basic_set *bset2, unsigned pos)
903 return (struct isl_basic_set *)
904 add_constraints((struct isl_basic_map *)bset1,
905 (struct isl_basic_map *)bset2, 0, pos);
908 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
909 struct isl_dim *dim, unsigned extra,
910 unsigned n_eq, unsigned n_ineq)
912 struct isl_basic_map *ext;
922 dims_ok = isl_dim_equal(base->dim, dim) &&
923 base->extra >= base->n_div + extra;
925 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
926 room_for_ineq(base, n_ineq)) {
931 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
932 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
933 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
934 extra += base->extra;
936 n_ineq += base->n_ineq;
938 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
944 ext->sample = isl_vec_copy(base->sample);
946 ext = add_constraints(ext, base, 0, 0);
949 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
956 isl_basic_map_free(base);
960 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
961 struct isl_dim *dim, unsigned extra,
962 unsigned n_eq, unsigned n_ineq)
964 return (struct isl_basic_set *)
965 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
966 extra, n_eq, n_ineq);
969 struct isl_basic_map *isl_basic_map_extend_constraints(
970 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
974 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
978 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
979 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
980 unsigned n_eq, unsigned n_ineq)
982 struct isl_basic_map *bmap;
987 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
991 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
995 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
996 unsigned nparam, unsigned dim, unsigned extra,
997 unsigned n_eq, unsigned n_ineq)
999 return (struct isl_basic_set *)
1000 isl_basic_map_extend((struct isl_basic_map *)base,
1001 nparam, 0, dim, extra, n_eq, n_ineq);
1004 struct isl_basic_set *isl_basic_set_extend_constraints(
1005 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1007 return (struct isl_basic_set *)
1008 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1012 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1014 return (struct isl_basic_set *)
1015 isl_basic_map_cow((struct isl_basic_map *)bset);
1018 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1023 if (bmap->ref > 1) {
1025 bmap = isl_basic_map_dup(bmap);
1027 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1031 struct isl_set *isl_set_cow(struct isl_set *set)
1039 return isl_set_dup(set);
1042 struct isl_map *isl_map_cow(struct isl_map *map)
1050 return isl_map_dup(map);
1053 static void swap_vars(struct isl_blk blk, isl_int *a,
1054 unsigned a_len, unsigned b_len)
1056 isl_seq_cpy(blk.data, a+a_len, b_len);
1057 isl_seq_cpy(blk.data+b_len, a, a_len);
1058 isl_seq_cpy(a, blk.data, b_len+a_len);
1061 struct isl_basic_set *isl_basic_set_swap_vars(
1062 struct isl_basic_set *bset, unsigned n)
1072 nparam = isl_basic_set_n_param(bset);
1073 dim = isl_basic_set_n_dim(bset);
1074 isl_assert(bset->ctx, n <= dim, goto error);
1079 bset = isl_basic_set_cow(bset);
1083 blk = isl_blk_alloc(bset->ctx, dim);
1084 if (isl_blk_is_error(blk))
1087 for (i = 0; i < bset->n_eq; ++i)
1089 bset->eq[i]+1+nparam, n, dim - n);
1091 for (i = 0; i < bset->n_ineq; ++i)
1093 bset->ineq[i]+1+nparam, n, dim - n);
1095 for (i = 0; i < bset->n_div; ++i)
1097 bset->div[i]+1+1+nparam, n, dim - n);
1099 isl_blk_free(bset->ctx, blk);
1101 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1102 return isl_basic_set_gauss(bset, NULL);
1104 isl_basic_set_free(bset);
1108 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1111 set = isl_set_cow(set);
1115 for (i = 0; i < set->n; ++i) {
1116 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1122 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1126 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1132 total = isl_basic_map_total_dim(bmap);
1133 isl_basic_map_free_div(bmap, bmap->n_div);
1134 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1136 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1138 isl_basic_map_alloc_equality(bmap);
1142 isl_int_set_si(bmap->eq[i][0], 1);
1143 isl_seq_clr(bmap->eq[i]+1, total);
1144 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1145 isl_vec_free(bmap->sample);
1146 bmap->sample = NULL;
1147 return isl_basic_map_finalize(bmap);
1149 isl_basic_map_free(bmap);
1153 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1155 return (struct isl_basic_set *)
1156 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1159 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1162 unsigned off = isl_dim_total(bmap->dim);
1163 isl_int *t = bmap->div[a];
1164 bmap->div[a] = bmap->div[b];
1167 for (i = 0; i < bmap->n_eq; ++i)
1168 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1170 for (i = 0; i < bmap->n_ineq; ++i)
1171 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1173 for (i = 0; i < bmap->n_div; ++i)
1174 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1175 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1178 /* Eliminate the specified n dimensions starting at first from the
1179 * constraints using Fourier-Motzkin, The dimensions themselves
1182 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1183 unsigned first, unsigned n)
1193 set = isl_set_cow(set);
1196 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1197 nparam = isl_set_n_param(set);
1199 for (i = 0; i < set->n; ++i) {
1200 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1211 /* Project out n dimensions starting at first using Fourier-Motzkin */
1212 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1213 unsigned first, unsigned n)
1215 set = isl_set_eliminate_dims(set, first, n);
1216 set = isl_set_drop_dims(set, first, n);
1220 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1222 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1230 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1239 set = isl_set_cow(set);
1243 for (i = 0; i < set->n; ++i) {
1244 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1254 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1255 enum isl_dim_type type, unsigned first, unsigned n)
1259 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1263 bmap = isl_basic_map_eliminate_vars(bmap,
1264 isl_basic_map_offset(bmap, type) - 1 + first, n);
1267 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1269 bmap = isl_basic_map_drop(bmap, type, first, n);
1272 isl_basic_map_free(bmap);
1276 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1277 enum isl_dim_type type, unsigned first, unsigned n)
1279 return (isl_basic_set *)
1280 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1283 struct isl_map *isl_map_remove(struct isl_map *map,
1284 enum isl_dim_type type, unsigned first, unsigned n)
1291 map = isl_map_cow(map);
1294 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1296 for (i = 0; i < map->n; ++i) {
1297 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1298 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1302 map = isl_map_drop(map, type, first, n);
1309 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1310 enum isl_dim_type type, unsigned first, unsigned n)
1312 return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1315 /* Project out n inputs starting at first using Fourier-Motzkin */
1316 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1317 unsigned first, unsigned n)
1319 return isl_map_remove(map, isl_dim_in, first, n);
1322 /* Project out n dimensions starting at first using Fourier-Motzkin */
1323 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1324 unsigned first, unsigned n)
1326 unsigned nparam = isl_basic_set_n_param(bset);
1327 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1328 bset = isl_basic_set_drop_dims(bset, first, n);
1332 static void dump_term(struct isl_basic_map *bmap,
1333 isl_int c, int pos, FILE *out)
1336 unsigned in = isl_basic_map_n_in(bmap);
1337 unsigned dim = in + isl_basic_map_n_out(bmap);
1338 unsigned nparam = isl_basic_map_n_param(bmap);
1340 isl_int_print(out, c, 0);
1342 if (!isl_int_is_one(c))
1343 isl_int_print(out, c, 0);
1344 if (pos < 1 + nparam) {
1345 name = isl_dim_get_name(bmap->dim,
1346 isl_dim_param, pos - 1);
1348 fprintf(out, "%s", name);
1350 fprintf(out, "p%d", pos - 1);
1351 } else if (pos < 1 + nparam + in)
1352 fprintf(out, "i%d", pos - 1 - nparam);
1353 else if (pos < 1 + nparam + dim)
1354 fprintf(out, "o%d", pos - 1 - nparam - in);
1356 fprintf(out, "e%d", pos - 1 - nparam - dim);
1360 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1361 int sign, FILE *out)
1365 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1369 for (i = 0, first = 1; i < len; ++i) {
1370 if (isl_int_sgn(c[i]) * sign <= 0)
1373 fprintf(out, " + ");
1375 isl_int_abs(v, c[i]);
1376 dump_term(bmap, v, i, out);
1383 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1384 const char *op, FILE *out, int indent)
1388 fprintf(out, "%*s", indent, "");
1390 dump_constraint_sign(bmap, c, 1, out);
1391 fprintf(out, " %s ", op);
1392 dump_constraint_sign(bmap, c, -1, out);
1396 for (i = bmap->n_div; i < bmap->extra; ++i) {
1397 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1399 fprintf(out, "%*s", indent, "");
1400 fprintf(out, "ERROR: unused div coefficient not zero\n");
1405 static void dump_constraints(struct isl_basic_map *bmap,
1406 isl_int **c, unsigned n,
1407 const char *op, FILE *out, int indent)
1411 for (i = 0; i < n; ++i)
1412 dump_constraint(bmap, c[i], op, out, indent);
1415 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1419 unsigned total = isl_basic_map_total_dim(bmap);
1421 for (j = 0; j < 1 + total; ++j) {
1422 if (isl_int_is_zero(exp[j]))
1424 if (!first && isl_int_is_pos(exp[j]))
1426 dump_term(bmap, exp[j], j, out);
1431 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1435 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1436 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1438 for (i = 0; i < bmap->n_div; ++i) {
1439 fprintf(out, "%*s", indent, "");
1440 fprintf(out, "e%d = [(", i);
1441 dump_affine(bmap, bmap->div[i]+1, out);
1443 isl_int_print(out, bmap->div[i][0], 0);
1444 fprintf(out, "]\n");
1448 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1451 fprintf(out, "null basic set\n");
1455 fprintf(out, "%*s", indent, "");
1456 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1457 bset->ref, bset->dim->nparam, bset->dim->n_out,
1458 bset->extra, bset->flags);
1459 dump((struct isl_basic_map *)bset, out, indent);
1462 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1465 fprintf(out, "null basic map\n");
1469 fprintf(out, "%*s", indent, "");
1470 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1471 "flags: %x, n_name: %d\n",
1473 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1474 bmap->extra, bmap->flags, bmap->dim->n_name);
1475 dump(bmap, out, indent);
1478 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1483 total = isl_basic_map_total_dim(bmap);
1484 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1485 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1486 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1487 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1491 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1493 struct isl_set *set;
1497 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1498 isl_assert(dim->ctx, n >= 0, return NULL);
1499 set = isl_alloc(dim->ctx, struct isl_set,
1500 sizeof(struct isl_set) +
1501 (n - 1) * sizeof(struct isl_basic_set *));
1505 set->ctx = dim->ctx;
1506 isl_ctx_ref(set->ctx);
1518 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1519 unsigned nparam, unsigned dim, int n, unsigned flags)
1521 struct isl_set *set;
1522 struct isl_dim *dims;
1524 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1528 set = isl_set_alloc_dim(dims, n, flags);
1532 /* Make sure "map" has room for at least "n" more basic maps.
1534 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1537 struct isl_map *grown = NULL;
1541 isl_assert(map->ctx, n >= 0, goto error);
1542 if (map->n + n <= map->size)
1544 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1547 for (i = 0; i < map->n; ++i) {
1548 grown->p[i] = isl_basic_map_copy(map->p[i]);
1556 isl_map_free(grown);
1561 /* Make sure "set" has room for at least "n" more basic sets.
1563 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1565 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1568 struct isl_set *isl_set_dup(struct isl_set *set)
1571 struct isl_set *dup;
1576 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1579 for (i = 0; i < set->n; ++i)
1580 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1584 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1586 struct isl_set *set;
1591 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1593 isl_basic_set_free(bset);
1596 return isl_set_add_basic_set(set, bset);
1599 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1601 struct isl_map *map;
1606 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1608 isl_basic_map_free(bmap);
1611 return isl_map_add_basic_map(map, bmap);
1614 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1615 __isl_take isl_basic_set *bset)
1617 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1618 (struct isl_basic_map *)bset);
1621 void isl_set_free(struct isl_set *set)
1631 isl_ctx_deref(set->ctx);
1632 for (i = 0; i < set->n; ++i)
1633 isl_basic_set_free(set->p[i]);
1634 isl_dim_free(set->dim);
1638 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1643 fprintf(out, "null set\n");
1647 fprintf(out, "%*s", indent, "");
1648 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1649 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1651 for (i = 0; i < set->n; ++i) {
1652 fprintf(out, "%*s", indent, "");
1653 fprintf(out, "basic set %d:\n", i);
1654 isl_basic_set_dump(set->p[i], out, indent+4);
1658 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1663 fprintf(out, "null map\n");
1667 fprintf(out, "%*s", indent, "");
1668 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1669 "flags: %x, n_name: %d\n",
1670 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1671 map->dim->n_out, map->flags, map->dim->n_name);
1672 for (i = 0; i < map->n; ++i) {
1673 fprintf(out, "%*s", indent, "");
1674 fprintf(out, "basic map %d:\n", i);
1675 isl_basic_map_dump(map->p[i], out, indent+4);
1679 struct isl_basic_map *isl_basic_map_intersect_domain(
1680 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1682 struct isl_basic_map *bmap_domain;
1683 struct isl_dim *dim;
1688 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1689 bset->dim, isl_dim_param), goto error);
1691 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1692 isl_assert(bset->ctx,
1693 isl_basic_map_compatible_domain(bmap, bset), goto error);
1695 bmap = isl_basic_map_cow(bmap);
1696 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1697 bset->n_div, bset->n_eq, bset->n_ineq);
1700 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1701 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1702 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1704 bmap = isl_basic_map_simplify(bmap);
1705 return isl_basic_map_finalize(bmap);
1707 isl_basic_map_free(bmap);
1708 isl_basic_set_free(bset);
1712 struct isl_basic_map *isl_basic_map_intersect_range(
1713 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1715 struct isl_basic_map *bmap_range;
1720 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1721 bset->dim, isl_dim_param), goto error);
1723 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1724 isl_assert(bset->ctx,
1725 isl_basic_map_compatible_range(bmap, bset), goto error);
1727 bmap = isl_basic_map_cow(bmap);
1728 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1729 bset->n_div, bset->n_eq, bset->n_ineq);
1732 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1733 bmap = add_constraints(bmap, bmap_range, 0, 0);
1735 bmap = isl_basic_map_simplify(bmap);
1736 return isl_basic_map_finalize(bmap);
1738 isl_basic_map_free(bmap);
1739 isl_basic_set_free(bset);
1743 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1749 total = 1 + isl_basic_map_total_dim(bmap);
1750 if (total != vec->size)
1755 for (i = 0; i < bmap->n_eq; ++i) {
1756 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1757 if (!isl_int_is_zero(s)) {
1763 for (i = 0; i < bmap->n_ineq; ++i) {
1764 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1765 if (isl_int_is_neg(s)) {
1776 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1778 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1781 struct isl_basic_map *isl_basic_map_intersect(
1782 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1784 struct isl_vec *sample = NULL;
1786 if (!bmap1 || !bmap2)
1789 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1790 bmap2->dim, isl_dim_param), goto error);
1791 if (isl_dim_total(bmap1->dim) ==
1792 isl_dim_size(bmap1->dim, isl_dim_param) &&
1793 isl_dim_total(bmap2->dim) !=
1794 isl_dim_size(bmap2->dim, isl_dim_param))
1795 return isl_basic_map_intersect(bmap2, bmap1);
1797 if (isl_dim_total(bmap2->dim) !=
1798 isl_dim_size(bmap2->dim, isl_dim_param))
1799 isl_assert(bmap1->ctx,
1800 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1802 if (bmap1->sample &&
1803 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1804 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1805 sample = isl_vec_copy(bmap1->sample);
1806 else if (bmap2->sample &&
1807 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1808 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1809 sample = isl_vec_copy(bmap2->sample);
1811 bmap1 = isl_basic_map_cow(bmap1);
1812 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1813 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1816 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1819 isl_vec_free(bmap1->sample);
1820 bmap1->sample = sample;
1823 bmap1 = isl_basic_map_simplify(bmap1);
1824 return isl_basic_map_finalize(bmap1);
1827 isl_vec_free(sample);
1828 isl_basic_map_free(bmap1);
1829 isl_basic_map_free(bmap2);
1833 struct isl_basic_set *isl_basic_set_intersect(
1834 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1836 return (struct isl_basic_set *)
1837 isl_basic_map_intersect(
1838 (struct isl_basic_map *)bset1,
1839 (struct isl_basic_map *)bset2);
1842 /* Special case of isl_map_intersect, where both map1 and map2
1843 * are convex, without any divs and such that either map1 or map2
1844 * contains a single constraint. This constraint is then simply
1845 * added to the other map.
1847 static __isl_give isl_map *map_intersect_add_constraint(
1848 __isl_take isl_map *map1, __isl_take isl_map *map2)
1850 struct isl_basic_map *bmap1;
1851 struct isl_basic_map *bmap2;
1853 isl_assert(map1->ctx, map1->n == 1, goto error);
1854 isl_assert(map2->ctx, map1->n == 1, goto error);
1855 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
1856 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
1858 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
1859 return isl_map_intersect(map2, map1);
1861 isl_assert(map2->ctx,
1862 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
1864 map1 = isl_map_cow(map1);
1867 map1->p[0] = isl_basic_map_cow(map1->p[0]);
1868 if (map2->p[0]->n_eq == 1)
1869 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
1871 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
1872 map2->p[0]->ineq[0]);
1874 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
1875 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
1879 if (isl_basic_map_fast_is_empty(map1->p[0])) {
1880 isl_basic_map_free(map1->p[0]);
1893 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1896 struct isl_map *result;
1902 if (map1->n == 1 && map2->n == 1 &&
1903 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
1904 isl_dim_equal(map1->dim, map2->dim) &&
1905 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
1906 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
1907 return map_intersect_add_constraint(map1, map2);
1908 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1909 map2->dim, isl_dim_param), goto error);
1910 if (isl_dim_total(map1->dim) ==
1911 isl_dim_size(map1->dim, isl_dim_param) &&
1912 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1913 return isl_map_intersect(map2, map1);
1915 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1916 isl_assert(map1->ctx,
1917 isl_dim_equal(map1->dim, map2->dim), goto error);
1919 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1920 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1921 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1923 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1924 map1->n * map2->n, flags);
1927 for (i = 0; i < map1->n; ++i)
1928 for (j = 0; j < map2->n; ++j) {
1929 struct isl_basic_map *part;
1930 part = isl_basic_map_intersect(
1931 isl_basic_map_copy(map1->p[i]),
1932 isl_basic_map_copy(map2->p[j]));
1933 if (isl_basic_map_is_empty(part))
1934 isl_basic_map_free(part);
1936 result = isl_map_add_basic_map(result, part);
1949 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1951 return (struct isl_set *)
1952 isl_map_intersect((struct isl_map *)set1,
1953 (struct isl_map *)set2);
1956 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1958 struct isl_dim *dim;
1959 struct isl_basic_set *bset;
1964 bmap = isl_basic_map_cow(bmap);
1967 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1968 in = isl_basic_map_n_in(bmap);
1969 bset = isl_basic_set_from_basic_map(bmap);
1970 bset = isl_basic_set_swap_vars(bset, in);
1971 return isl_basic_map_from_basic_set(bset, dim);
1974 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
1975 enum isl_dim_type type, unsigned n)
1977 struct isl_dim *res_dim;
1978 struct isl_basic_map *res;
1979 struct isl_dim_map *dim_map;
1980 unsigned total, pos;
1988 res_dim = isl_dim_add(isl_basic_map_get_dim(bmap), type, n);
1990 total = isl_basic_map_total_dim(bmap) + n;
1991 dim_map = isl_dim_map_alloc(bmap->ctx, total);
1993 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos);
1994 pos += isl_dim_size(res_dim, isl_dim_param);
1995 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos);
1996 pos += isl_dim_size(res_dim, isl_dim_in);
1997 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos);
1998 pos += isl_dim_size(res_dim, isl_dim_out);
1999 isl_dim_map_div(dim_map, bmap, pos);
2001 res = isl_basic_map_alloc_dim(res_dim,
2002 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2003 res = add_constraints_dim_map(res, bmap, dim_map);
2004 res = isl_basic_map_simplify(res);
2005 return isl_basic_map_finalize(res);
2008 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2009 enum isl_dim_type type, unsigned n)
2013 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2014 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2016 isl_basic_set_free(bset);
2020 __isl_give isl_map *isl_map_add(__isl_take isl_map *map,
2021 enum isl_dim_type type, unsigned n)
2028 map = isl_map_cow(map);
2032 map->dim = isl_dim_add(map->dim, type, n);
2036 for (i = 0; i < map->n; ++i) {
2037 map->p[i] = isl_basic_map_add(map->p[i], type, n);
2048 __isl_give isl_set *isl_set_add(__isl_take isl_set *set,
2049 enum isl_dim_type type, unsigned n)
2053 isl_assert(set->ctx, type != isl_dim_in, goto error);
2054 return (isl_set *)isl_map_add((isl_map *)set, type, n);
2060 /* Move the specified dimensions to the last columns right before
2061 * the divs. Don't change the dimension specification of bmap.
2062 * That's the responsibility of the caller.
2064 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2065 enum isl_dim_type type, unsigned first, unsigned n)
2067 struct isl_dim_map *dim_map;
2068 struct isl_basic_map *res;
2069 enum isl_dim_type t;
2070 unsigned total, off;
2074 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2077 total = isl_basic_map_total_dim(bmap);
2078 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2081 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2082 unsigned size = isl_dim_size(bmap->dim, t);
2084 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2087 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2088 first, n, total - bmap->n_div - n);
2089 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2090 first + n, size - (first + n), off);
2091 off += size - (first + n);
2093 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2097 isl_dim_map_div(dim_map, bmap, off + n);
2099 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2100 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2101 res = add_constraints_dim_map(res, bmap, dim_map);
2105 /* Turn the n dimensions of type type, starting at first
2106 * into existentially quantified variables.
2108 __isl_give isl_basic_map *isl_basic_map_project_out(
2109 __isl_take isl_basic_map *bmap,
2110 enum isl_dim_type type, unsigned first, unsigned n)
2123 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2124 return isl_basic_map_remove(bmap, type, first, n);
2126 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2129 bmap = move_last(bmap, type, first, n);
2130 bmap = isl_basic_map_cow(bmap);
2132 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2133 old = bmap->block2.data;
2134 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2135 (bmap->extra + n) * (1 + row_size));
2136 if (!bmap->block2.data)
2138 new_div = isl_alloc_array(ctx, isl_int *, bmap->extra + n);
2141 for (i = 0; i < n; ++i) {
2142 new_div[i] = bmap->block2.data +
2143 (bmap->extra + i) * (1 + row_size);
2144 isl_seq_clr(new_div[i], 1 + row_size);
2146 for (i = 0; i < bmap->extra; ++i)
2147 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2149 bmap->div = new_div;
2153 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2156 bmap = isl_basic_map_simplify(bmap);
2157 bmap = isl_basic_map_drop_redundant_divs(bmap);
2158 return isl_basic_map_finalize(bmap);
2160 isl_basic_map_free(bmap);
2164 /* Turn the n dimensions of type type, starting at first
2165 * into existentially quantified variables.
2167 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2168 enum isl_dim_type type, unsigned first, unsigned n)
2170 return (isl_basic_set *)isl_basic_map_project_out(
2171 (isl_basic_map *)bset, type, first, n);
2174 /* Turn the n dimensions of type type, starting at first
2175 * into existentially quantified variables.
2177 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2178 enum isl_dim_type type, unsigned first, unsigned n)
2188 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2190 map = isl_map_cow(map);
2194 map->dim = isl_dim_drop(map->dim, type, first, n);
2198 for (i = 0; i < map->n; ++i) {
2199 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2210 /* Turn the n dimensions of type type, starting at first
2211 * into existentially quantified variables.
2213 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2214 enum isl_dim_type type, unsigned first, unsigned n)
2216 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2219 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2223 for (i = 0; i < n; ++i) {
2224 j = isl_basic_map_alloc_div(bmap);
2227 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2231 isl_basic_map_free(bmap);
2235 struct isl_basic_map *isl_basic_map_apply_range(
2236 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2238 struct isl_dim *dim_result = NULL;
2239 struct isl_basic_map *bmap;
2240 unsigned n_in, n_out, n, nparam, total, pos;
2241 struct isl_dim_map *dim_map1, *dim_map2;
2243 if (!bmap1 || !bmap2)
2246 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2247 isl_dim_copy(bmap2->dim));
2249 n_in = isl_basic_map_n_in(bmap1);
2250 n_out = isl_basic_map_n_out(bmap2);
2251 n = isl_basic_map_n_out(bmap1);
2252 nparam = isl_basic_map_n_param(bmap1);
2254 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2255 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2256 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2257 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2258 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2259 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2260 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2261 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2262 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2263 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2264 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2266 bmap = isl_basic_map_alloc_dim(dim_result,
2267 bmap1->n_div + bmap2->n_div + n,
2268 bmap1->n_eq + bmap2->n_eq,
2269 bmap1->n_ineq + bmap2->n_ineq);
2270 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2271 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2272 bmap = add_divs(bmap, n);
2273 bmap = isl_basic_map_simplify(bmap);
2274 bmap = isl_basic_map_drop_redundant_divs(bmap);
2275 return isl_basic_map_finalize(bmap);
2277 isl_basic_map_free(bmap1);
2278 isl_basic_map_free(bmap2);
2282 struct isl_basic_set *isl_basic_set_apply(
2283 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2288 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2291 return (struct isl_basic_set *)
2292 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2294 isl_basic_set_free(bset);
2295 isl_basic_map_free(bmap);
2299 struct isl_basic_map *isl_basic_map_apply_domain(
2300 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2302 if (!bmap1 || !bmap2)
2305 isl_assert(bmap1->ctx,
2306 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2307 isl_assert(bmap1->ctx,
2308 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2311 bmap1 = isl_basic_map_reverse(bmap1);
2312 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2313 return isl_basic_map_reverse(bmap1);
2315 isl_basic_map_free(bmap1);
2316 isl_basic_map_free(bmap2);
2320 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2321 * A \cap B -> f(A) + f(B)
2323 struct isl_basic_map *isl_basic_map_sum(
2324 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2326 unsigned n_in, n_out, nparam, total, pos;
2327 struct isl_basic_map *bmap = NULL;
2328 struct isl_dim_map *dim_map1, *dim_map2;
2331 if (!bmap1 || !bmap2)
2334 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2337 nparam = isl_basic_map_n_param(bmap1);
2338 n_in = isl_basic_map_n_in(bmap1);
2339 n_out = isl_basic_map_n_out(bmap1);
2341 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2342 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2343 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2344 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2345 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2346 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2347 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2348 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2349 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2350 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2351 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2353 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2354 bmap1->n_div + bmap2->n_div + 2 * n_out,
2355 bmap1->n_eq + bmap2->n_eq + n_out,
2356 bmap1->n_ineq + bmap2->n_ineq);
2357 for (i = 0; i < n_out; ++i) {
2358 int j = isl_basic_map_alloc_equality(bmap);
2361 isl_seq_clr(bmap->eq[j], 1+total);
2362 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2363 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2364 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2366 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2367 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2368 bmap = add_divs(bmap, 2 * n_out);
2370 bmap = isl_basic_map_simplify(bmap);
2371 return isl_basic_map_finalize(bmap);
2373 isl_basic_map_free(bmap);
2374 isl_basic_map_free(bmap1);
2375 isl_basic_map_free(bmap2);
2379 /* Given two maps A -> f(A) and B -> g(B), construct a map
2380 * A \cap B -> f(A) + f(B)
2382 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2384 struct isl_map *result;
2390 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2392 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2393 map1->n * map2->n, 0);
2396 for (i = 0; i < map1->n; ++i)
2397 for (j = 0; j < map2->n; ++j) {
2398 struct isl_basic_map *part;
2399 part = isl_basic_map_sum(
2400 isl_basic_map_copy(map1->p[i]),
2401 isl_basic_map_copy(map2->p[j]));
2402 if (isl_basic_map_is_empty(part))
2403 isl_basic_map_free(part);
2405 result = isl_map_add_basic_map(result, part);
2418 /* Given a basic map A -> f(A), construct A -> -f(A).
2420 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2425 bmap = isl_basic_map_cow(bmap);
2429 n = isl_basic_map_dim(bmap, isl_dim_out);
2430 off = isl_basic_map_offset(bmap, isl_dim_out);
2431 for (i = 0; i < bmap->n_eq; ++i)
2432 for (j = 0; j < n; ++j)
2433 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2434 for (i = 0; i < bmap->n_ineq; ++i)
2435 for (j = 0; j < n; ++j)
2436 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2437 for (i = 0; i < bmap->n_div; ++i)
2438 for (j = 0; j < n; ++j)
2439 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2440 return isl_basic_map_finalize(bmap);
2443 /* Given a map A -> f(A), construct A -> -f(A).
2445 struct isl_map *isl_map_neg(struct isl_map *map)
2449 map = isl_map_cow(map);
2453 for (i = 0; i < map->n; ++i) {
2454 map->p[i] = isl_basic_map_neg(map->p[i]);
2465 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2466 * A -> floor(f(A)/d).
2468 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2471 unsigned n_in, n_out, nparam, total, pos;
2472 struct isl_basic_map *result = NULL;
2473 struct isl_dim_map *dim_map;
2479 nparam = isl_basic_map_n_param(bmap);
2480 n_in = isl_basic_map_n_in(bmap);
2481 n_out = isl_basic_map_n_out(bmap);
2483 total = nparam + n_in + n_out + bmap->n_div + n_out;
2484 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2485 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2486 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2487 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2488 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2490 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2491 bmap->n_div + n_out,
2492 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2493 result = add_constraints_dim_map(result, bmap, dim_map);
2494 result = add_divs(result, n_out);
2495 for (i = 0; i < n_out; ++i) {
2497 j = isl_basic_map_alloc_inequality(result);
2500 isl_seq_clr(result->ineq[j], 1+total);
2501 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2502 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2503 j = isl_basic_map_alloc_inequality(result);
2506 isl_seq_clr(result->ineq[j], 1+total);
2507 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2508 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2509 isl_int_sub_ui(result->ineq[j][0], d, 1);
2512 result = isl_basic_map_simplify(result);
2513 return isl_basic_map_finalize(result);
2515 isl_basic_map_free(result);
2519 /* Given a map A -> f(A) and an integer d, construct a map
2520 * A -> floor(f(A)/d).
2522 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2526 map = isl_map_cow(map);
2530 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2531 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2532 for (i = 0; i < map->n; ++i) {
2533 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2544 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2550 i = isl_basic_map_alloc_equality(bmap);
2553 nparam = isl_basic_map_n_param(bmap);
2554 n_in = isl_basic_map_n_in(bmap);
2555 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2556 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2557 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2558 return isl_basic_map_finalize(bmap);
2560 isl_basic_map_free(bmap);
2564 /* Add a constraints to "bmap" expressing i_pos < o_pos
2566 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2572 i = isl_basic_map_alloc_inequality(bmap);
2575 nparam = isl_basic_map_n_param(bmap);
2576 n_in = isl_basic_map_n_in(bmap);
2577 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2578 isl_int_set_si(bmap->ineq[i][0], -1);
2579 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2580 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2581 return isl_basic_map_finalize(bmap);
2583 isl_basic_map_free(bmap);
2587 /* Add a constraints to "bmap" expressing i_pos > o_pos
2589 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2595 i = isl_basic_map_alloc_inequality(bmap);
2598 nparam = isl_basic_map_n_param(bmap);
2599 n_in = isl_basic_map_n_in(bmap);
2600 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2601 isl_int_set_si(bmap->ineq[i][0], -1);
2602 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2603 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2604 return isl_basic_map_finalize(bmap);
2606 isl_basic_map_free(bmap);
2610 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2613 struct isl_basic_map *bmap;
2614 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2617 for (i = 0; i < n_equal && bmap; ++i)
2618 bmap = var_equal(bmap, i);
2619 return isl_basic_map_finalize(bmap);
2622 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2624 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2627 struct isl_basic_map *bmap;
2628 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2631 for (i = 0; i < pos && bmap; ++i)
2632 bmap = var_equal(bmap, i);
2634 bmap = var_less(bmap, pos);
2635 return isl_basic_map_finalize(bmap);
2638 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2640 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2643 struct isl_basic_map *bmap;
2644 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2647 for (i = 0; i < pos && bmap; ++i)
2648 bmap = var_equal(bmap, i);
2650 bmap = var_more(bmap, pos);
2651 return isl_basic_map_finalize(bmap);
2654 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
2656 struct isl_map *map;
2663 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2665 for (i = 0; i < dim; ++i)
2666 map = isl_map_add_basic_map(map,
2667 isl_basic_map_less_at(isl_dim_copy(dims), i));
2669 map = isl_map_add_basic_map(map,
2670 isl_basic_map_equal(isl_dim_copy(dims), dim));
2676 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
2678 return map_lex_lte(isl_dim_map(set_dim), 0);
2681 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
2683 return map_lex_lte(isl_dim_map(set_dim), 1);
2686 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
2688 struct isl_map *map;
2695 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2697 for (i = 0; i < dim; ++i)
2698 map = isl_map_add_basic_map(map,
2699 isl_basic_map_more_at(isl_dim_copy(dims), i));
2701 map = isl_map_add_basic_map(map,
2702 isl_basic_map_equal(isl_dim_copy(dims), dim));
2708 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
2710 return map_lex_gte(isl_dim_map(set_dim), 0);
2713 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
2715 return map_lex_gte(isl_dim_map(set_dim), 1);
2718 struct isl_basic_map *isl_basic_map_from_basic_set(
2719 struct isl_basic_set *bset, struct isl_dim *dim)
2721 struct isl_basic_map *bmap;
2723 bset = isl_basic_set_cow(bset);
2727 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2728 isl_dim_free(bset->dim);
2729 bmap = (struct isl_basic_map *) bset;
2731 return isl_basic_map_finalize(bmap);
2733 isl_basic_set_free(bset);
2738 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2742 if (bmap->dim->n_in == 0)
2743 return (struct isl_basic_set *)bmap;
2744 bmap = isl_basic_map_cow(bmap);
2747 bmap->dim = isl_dim_cow(bmap->dim);
2750 bmap->dim->n_out += bmap->dim->n_in;
2751 bmap->dim->n_in = 0;
2752 bmap = isl_basic_map_finalize(bmap);
2753 return (struct isl_basic_set *)bmap;
2755 isl_basic_map_free(bmap);
2759 /* For a div d = floor(f/m), add the constraints
2762 * -(f-(n-1)) + m d >= 0
2764 * Note that the second constraint is the negation of
2768 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2771 unsigned total = isl_basic_map_total_dim(bmap);
2772 unsigned div_pos = 1 + total - bmap->n_div + div;
2774 i = isl_basic_map_alloc_inequality(bmap);
2777 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2778 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2780 j = isl_basic_map_alloc_inequality(bmap);
2783 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2784 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2785 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2789 struct isl_basic_set *isl_basic_map_underlying_set(
2790 struct isl_basic_map *bmap)
2794 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2795 return (struct isl_basic_set *)bmap;
2796 bmap = isl_basic_map_cow(bmap);
2799 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2802 bmap->extra -= bmap->n_div;
2804 bmap = isl_basic_map_finalize(bmap);
2805 return (struct isl_basic_set *)bmap;
2810 __isl_give isl_basic_set *isl_basic_set_underlying_set(
2811 __isl_take isl_basic_set *bset)
2813 return isl_basic_map_underlying_set((isl_basic_map *)bset);
2816 struct isl_basic_map *isl_basic_map_overlying_set(
2817 struct isl_basic_set *bset, struct isl_basic_map *like)
2819 struct isl_basic_map *bmap;
2820 struct isl_ctx *ctx;
2827 isl_assert(ctx, bset->n_div == 0, goto error);
2828 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2829 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2831 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2832 isl_basic_map_free(like);
2833 return (struct isl_basic_map *)bset;
2835 bset = isl_basic_set_cow(bset);
2838 total = bset->dim->n_out + bset->extra;
2839 bmap = (struct isl_basic_map *)bset;
2840 isl_dim_free(bmap->dim);
2841 bmap->dim = isl_dim_copy(like->dim);
2844 bmap->n_div = like->n_div;
2845 bmap->extra += like->n_div;
2848 ltotal = total - bmap->extra + like->extra;
2851 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2852 bmap->extra * (1 + 1 + total));
2853 if (isl_blk_is_error(bmap->block2))
2855 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2859 for (i = 0; i < bmap->extra; ++i)
2860 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2861 for (i = 0; i < like->n_div; ++i) {
2862 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2863 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2865 bmap = isl_basic_map_extend_constraints(bmap,
2866 0, 2 * like->n_div);
2867 for (i = 0; i < like->n_div; ++i) {
2868 if (isl_int_is_zero(bmap->div[i][0]))
2870 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
2874 isl_basic_map_free(like);
2875 bmap = isl_basic_map_simplify(bmap);
2876 bmap = isl_basic_map_finalize(bmap);
2879 isl_basic_map_free(like);
2880 isl_basic_set_free(bset);
2884 struct isl_basic_set *isl_basic_set_from_underlying_set(
2885 struct isl_basic_set *bset, struct isl_basic_set *like)
2887 return (struct isl_basic_set *)
2888 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2891 struct isl_set *isl_set_from_underlying_set(
2892 struct isl_set *set, struct isl_basic_set *like)
2898 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2900 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2901 isl_basic_set_free(like);
2904 set = isl_set_cow(set);
2907 for (i = 0; i < set->n; ++i) {
2908 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2909 isl_basic_set_copy(like));
2913 isl_dim_free(set->dim);
2914 set->dim = isl_dim_copy(like->dim);
2917 isl_basic_set_free(like);
2920 isl_basic_set_free(like);
2925 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2929 map = isl_map_cow(map);
2932 map->dim = isl_dim_cow(map->dim);
2936 for (i = 1; i < map->n; ++i)
2937 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2939 for (i = 0; i < map->n; ++i) {
2940 map->p[i] = (struct isl_basic_map *)
2941 isl_basic_map_underlying_set(map->p[i]);
2946 map->dim = isl_dim_underlying(map->dim, 0);
2948 isl_dim_free(map->dim);
2949 map->dim = isl_dim_copy(map->p[0]->dim);
2953 return (struct isl_set *)map;
2959 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2961 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2964 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2966 struct isl_basic_set *domain;
2971 n_in = isl_basic_map_n_in(bmap);
2972 n_out = isl_basic_map_n_out(bmap);
2973 domain = isl_basic_set_from_basic_map(bmap);
2974 return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
2977 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2979 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2982 struct isl_set *isl_map_range(struct isl_map *map)
2985 struct isl_set *set;
2989 map = isl_map_cow(map);
2993 set = (struct isl_set *) map;
2994 if (set->dim->n_in != 0) {
2995 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2999 for (i = 0; i < map->n; ++i) {
3000 set->p[i] = isl_basic_map_range(map->p[i]);
3004 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3005 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3012 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3015 struct isl_map *map = NULL;
3017 set = isl_set_cow(set);
3020 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3021 map = (struct isl_map *)set;
3022 for (i = 0; i < set->n; ++i) {
3023 map->p[i] = isl_basic_map_from_basic_set(
3024 set->p[i], isl_dim_copy(dim));
3028 isl_dim_free(map->dim);
3037 struct isl_map *isl_map_from_range(struct isl_set *set)
3039 return (struct isl_map *)set;
3042 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3044 return isl_map_reverse(isl_map_from_range(set));;
3047 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3048 __isl_take isl_set *range)
3050 return isl_map_product(isl_map_from_domain(domain),
3051 isl_map_from_range(range));
3054 struct isl_set *isl_set_from_map(struct isl_map *map)
3057 struct isl_set *set = NULL;
3061 map = isl_map_cow(map);
3064 map->dim = isl_dim_cow(map->dim);
3067 map->dim->n_out += map->dim->n_in;
3069 set = (struct isl_set *)map;
3070 for (i = 0; i < map->n; ++i) {
3071 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3081 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3083 struct isl_map *map;
3087 isl_assert(dim->ctx, n >= 0, return NULL);
3088 map = isl_alloc(dim->ctx, struct isl_map,
3089 sizeof(struct isl_map) +
3090 (n - 1) * sizeof(struct isl_basic_map *));
3094 map->ctx = dim->ctx;
3095 isl_ctx_ref(map->ctx);
3107 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3108 unsigned nparam, unsigned in, unsigned out, int n,
3111 struct isl_map *map;
3112 struct isl_dim *dims;
3114 dims = isl_dim_alloc(ctx, nparam, in, out);
3118 map = isl_map_alloc_dim(dims, n, flags);
3122 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3124 struct isl_basic_map *bmap;
3125 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3126 bmap = isl_basic_map_set_to_empty(bmap);
3130 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3132 struct isl_basic_set *bset;
3133 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3134 bset = isl_basic_set_set_to_empty(bset);
3138 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3140 struct isl_basic_map *bmap;
3143 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3144 bmap = isl_basic_map_set_to_empty(bmap);
3148 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3150 struct isl_basic_map *bmap;
3153 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3154 bmap = isl_basic_map_set_to_empty(bmap);
3158 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3160 struct isl_basic_set *bset;
3163 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3164 bset = isl_basic_set_set_to_empty(bset);
3168 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3170 struct isl_basic_map *bmap;
3171 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
3175 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
3177 struct isl_basic_set *bset;
3178 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
3182 __isl_give isl_basic_map *isl_basic_map_universe_like(
3183 __isl_keep isl_basic_map *model)
3187 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3190 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3194 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3197 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3198 __isl_keep isl_set *model)
3202 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3205 struct isl_map *isl_map_empty(struct isl_dim *dim)
3207 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3210 struct isl_map *isl_map_empty_like(struct isl_map *model)
3214 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3217 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
3221 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3224 struct isl_set *isl_set_empty(struct isl_dim *dim)
3226 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3229 struct isl_set *isl_set_empty_like(struct isl_set *model)
3233 return isl_set_empty(isl_dim_copy(model->dim));
3236 struct isl_map *isl_map_universe(struct isl_dim *dim)
3238 struct isl_map *map;
3241 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3242 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
3246 struct isl_set *isl_set_universe(struct isl_dim *dim)
3248 struct isl_set *set;
3251 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3252 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
3256 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3260 return isl_set_universe(isl_dim_copy(model->dim));
3263 struct isl_map *isl_map_dup(struct isl_map *map)
3266 struct isl_map *dup;
3270 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3271 for (i = 0; i < map->n; ++i)
3272 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
3276 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
3277 __isl_take isl_basic_map *bmap)
3281 if (isl_basic_map_fast_is_empty(bmap)) {
3282 isl_basic_map_free(bmap);
3285 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3286 isl_assert(map->ctx, map->n < map->size, goto error);
3287 map->p[map->n] = bmap;
3289 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3295 isl_basic_map_free(bmap);
3299 void isl_map_free(struct isl_map *map)
3309 isl_ctx_deref(map->ctx);
3310 for (i = 0; i < map->n; ++i)
3311 isl_basic_map_free(map->p[i]);
3312 isl_dim_free(map->dim);
3316 struct isl_map *isl_map_extend(struct isl_map *base,
3317 unsigned nparam, unsigned n_in, unsigned n_out)
3321 base = isl_map_cow(base);
3325 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3328 for (i = 0; i < base->n; ++i) {
3329 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3330 isl_dim_copy(base->dim), 0, 0, 0);
3340 struct isl_set *isl_set_extend(struct isl_set *base,
3341 unsigned nparam, unsigned dim)
3343 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3347 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3348 struct isl_basic_map *bmap, unsigned pos, int value)
3352 bmap = isl_basic_map_cow(bmap);
3353 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3354 j = isl_basic_map_alloc_equality(bmap);
3357 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3358 isl_int_set_si(bmap->eq[j][pos], -1);
3359 isl_int_set_si(bmap->eq[j][0], value);
3360 bmap = isl_basic_map_simplify(bmap);
3361 return isl_basic_map_finalize(bmap);
3363 isl_basic_map_free(bmap);
3367 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3368 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3372 bmap = isl_basic_map_cow(bmap);
3373 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3374 j = isl_basic_map_alloc_equality(bmap);
3377 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3378 isl_int_set_si(bmap->eq[j][pos], -1);
3379 isl_int_set(bmap->eq[j][0], value);
3380 bmap = isl_basic_map_simplify(bmap);
3381 return isl_basic_map_finalize(bmap);
3383 isl_basic_map_free(bmap);
3387 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3388 enum isl_dim_type type, unsigned pos, int value)
3392 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3393 return isl_basic_map_fix_pos_si(bmap,
3394 isl_basic_map_offset(bmap, type) + pos, value);
3396 isl_basic_map_free(bmap);
3400 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3401 enum isl_dim_type type, unsigned pos, isl_int value)
3405 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3406 return isl_basic_map_fix_pos(bmap,
3407 isl_basic_map_offset(bmap, type) + pos, value);
3409 isl_basic_map_free(bmap);
3413 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3414 enum isl_dim_type type, unsigned pos, int value)
3416 return (struct isl_basic_set *)
3417 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3421 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3422 enum isl_dim_type type, unsigned pos, isl_int value)
3424 return (struct isl_basic_set *)
3425 isl_basic_map_fix((struct isl_basic_map *)bset,
3429 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3430 unsigned input, int value)
3432 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3435 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3436 unsigned dim, int value)
3438 return (struct isl_basic_set *)
3439 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3440 isl_dim_set, dim, value);
3443 struct isl_map *isl_map_fix_si(struct isl_map *map,
3444 enum isl_dim_type type, unsigned pos, int value)
3448 map = isl_map_cow(map);
3452 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3453 for (i = 0; i < map->n; ++i) {
3454 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3458 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3465 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
3466 enum isl_dim_type type, unsigned pos, int value)
3468 return (struct isl_set *)
3469 isl_map_fix_si((struct isl_map *)set, type, pos, value);
3472 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
3473 enum isl_dim_type type, unsigned pos, isl_int value)
3477 map = isl_map_cow(map);
3481 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3482 for (i = 0; i < map->n; ++i) {
3483 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
3487 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3494 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
3495 enum isl_dim_type type, unsigned pos, isl_int value)
3497 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
3500 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3501 unsigned input, int value)
3503 return isl_map_fix_si(map, isl_dim_in, input, value);
3506 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3508 return (struct isl_set *)
3509 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3512 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
3513 __isl_take isl_basic_map *bmap,
3514 enum isl_dim_type type, unsigned pos, int value)
3520 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3521 pos += isl_basic_map_offset(bmap, type);
3522 bmap = isl_basic_map_cow(bmap);
3523 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
3524 j = isl_basic_map_alloc_inequality(bmap);
3527 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
3528 isl_int_set_si(bmap->ineq[j][pos], 1);
3529 isl_int_set_si(bmap->ineq[j][0], -value);
3530 bmap = isl_basic_map_simplify(bmap);
3531 return isl_basic_map_finalize(bmap);
3533 isl_basic_map_free(bmap);
3537 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3538 unsigned dim, isl_int value)
3542 bset = isl_basic_set_cow(bset);
3543 bset = isl_basic_set_extend_constraints(bset, 0, 1);
3544 j = isl_basic_set_alloc_inequality(bset);
3547 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3548 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3549 isl_int_neg(bset->ineq[j][0], value);
3550 bset = isl_basic_set_simplify(bset);
3551 return isl_basic_set_finalize(bset);
3553 isl_basic_set_free(bset);
3557 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
3558 enum isl_dim_type type, unsigned pos, int value)
3562 map = isl_map_cow(map);
3566 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3567 for (i = 0; i < map->n; ++i) {
3568 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
3573 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3580 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
3581 enum isl_dim_type type, unsigned pos, int value)
3583 return (struct isl_set *)
3584 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
3587 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3592 set = isl_set_cow(set);
3596 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3597 for (i = 0; i < set->n; ++i) {
3598 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3608 struct isl_map *isl_map_reverse(struct isl_map *map)
3612 map = isl_map_cow(map);
3616 map->dim = isl_dim_reverse(map->dim);
3619 for (i = 0; i < map->n; ++i) {
3620 map->p[i] = isl_basic_map_reverse(map->p[i]);
3624 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3631 static struct isl_map *isl_basic_map_partial_lexopt(
3632 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3633 struct isl_set **empty, int max)
3637 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
3638 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3640 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3642 isl_basic_map_free(bmap);
3643 isl_basic_set_free(dom);
3649 struct isl_map *isl_basic_map_partial_lexmax(
3650 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3651 struct isl_set **empty)
3653 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
3656 struct isl_map *isl_basic_map_partial_lexmin(
3657 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3658 struct isl_set **empty)
3660 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3663 struct isl_set *isl_basic_set_partial_lexmin(
3664 struct isl_basic_set *bset, struct isl_basic_set *dom,
3665 struct isl_set **empty)
3667 return (struct isl_set *)
3668 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3672 struct isl_set *isl_basic_set_partial_lexmax(
3673 struct isl_basic_set *bset, struct isl_basic_set *dom,
3674 struct isl_set **empty)
3676 return (struct isl_set *)
3677 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3681 /* Given a basic map "bmap", compute the lexicograhically minimal
3682 * (or maximal) image element for each domain element in dom.
3683 * Set *empty to those elements in dom that do not have an image element.
3685 * We first make sure the basic sets in dom are disjoint and then
3686 * simply collect the results over each of the basic sets separately.
3687 * We could probably improve the efficiency a bit by moving the union
3688 * domain down into the parametric integer programming.
3690 static __isl_give isl_map *basic_map_partial_lexopt(
3691 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
3692 __isl_give isl_set **empty, int max)
3695 struct isl_map *res;
3697 dom = isl_set_make_disjoint(dom);
3701 if (isl_set_fast_is_empty(dom)) {
3702 res = isl_map_empty_like_basic_map(bmap);
3703 *empty = isl_set_empty_like(dom);
3705 isl_basic_map_free(bmap);
3709 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
3710 isl_basic_set_copy(dom->p[0]), empty, max);
3712 for (i = 1; i < dom->n; ++i) {
3713 struct isl_map *res_i;
3714 struct isl_set *empty_i;
3716 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
3717 isl_basic_set_copy(dom->p[i]), &empty_i, max);
3719 res = isl_map_union_disjoint(res, res_i);
3720 *empty = isl_set_union_disjoint(*empty, empty_i);
3724 isl_basic_map_free(bmap);
3729 isl_basic_map_free(bmap);
3733 /* Given a map "map", compute the lexicograhically minimal
3734 * (or maximal) image element for each domain element in dom.
3735 * Set *empty to those elements in dom that do not have an image element.
3737 * We first compute the lexicographically minimal or maximal element
3738 * in the first basic map. This results in a partial solution "res"
3739 * and a subset "todo" of dom that still need to be handled.
3740 * We then consider each of the remaining maps in "map" and successively
3741 * improve both "res" and "todo".
3743 * Let res^k and todo^k be the results after k steps and let i = k + 1.
3744 * Assume we are computing the lexicographical maximum.
3745 * We first intersect basic map i with a relation that maps elements
3746 * to elements that are lexicographically larger than the image elements
3747 * in res^k and the compute the maximum image element of this intersection.
3748 * The result ("better") corresponds to those image elements in basic map i
3749 * that are better than what we had before. The remainder ("keep") are the
3750 * domain elements for which the image element in res_k was better.
3751 * We also compute the lexicographical maximum of basic map i in todo^k.
3752 * res^i is the result of the operation + better + those elements in
3753 * res^k that we should keep
3754 * todo^i is the remainder of the maximum operation on todo^k.
3756 static __isl_give isl_map *isl_map_partial_lexopt(
3757 __isl_take isl_map *map, __isl_take isl_set *dom,
3758 __isl_give isl_set **empty, int max)
3761 struct isl_map *res;
3762 struct isl_set *todo;
3767 if (isl_map_fast_is_empty(map)) {
3775 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
3776 isl_set_copy(dom), &todo, max);
3778 for (i = 1; i < map->n; ++i) {
3780 struct isl_map *better;
3781 struct isl_set *keep;
3782 struct isl_map *res_i;
3783 struct isl_set *todo_i;
3784 struct isl_dim *dim = isl_map_get_dim(res);
3786 dim = isl_dim_range(dim);
3788 lt = isl_map_lex_lt(dim);
3790 lt = isl_map_lex_gt(dim);
3791 lt = isl_map_apply_range(isl_map_copy(res), lt);
3792 lt = isl_map_intersect(lt,
3793 isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
3794 better = isl_map_partial_lexopt(lt,
3795 isl_map_domain(isl_map_copy(res)),
3798 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
3799 todo, &todo_i, max);
3801 res = isl_map_intersect_domain(res, keep);
3802 res = isl_map_union_disjoint(res, res_i);
3803 res = isl_map_union_disjoint(res, better);
3824 __isl_give isl_map *isl_map_partial_lexmax(
3825 __isl_take isl_map *map, __isl_take isl_set *dom,
3826 __isl_give isl_set **empty)
3828 return isl_map_partial_lexopt(map, dom, empty, 1);
3831 __isl_give isl_map *isl_map_partial_lexmin(
3832 __isl_take isl_map *map, __isl_take isl_set *dom,
3833 __isl_give isl_set **empty)
3835 return isl_map_partial_lexopt(map, dom, empty, 0);
3838 __isl_give isl_set *isl_set_partial_lexmin(
3839 __isl_take isl_set *set, __isl_take isl_set *dom,
3840 __isl_give isl_set **empty)
3842 return (struct isl_set *)
3843 isl_map_partial_lexmin((struct isl_map *)set,
3847 __isl_give isl_set *isl_set_partial_lexmax(
3848 __isl_take isl_set *set, __isl_take isl_set *dom,
3849 __isl_give isl_set **empty)
3851 return (struct isl_set *)
3852 isl_map_partial_lexmax((struct isl_map *)set,
3856 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
3858 struct isl_basic_set *dom = NULL;
3859 struct isl_dim *dom_dim;
3863 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3864 dom = isl_basic_set_universe(dom_dim);
3865 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
3867 isl_basic_map_free(bmap);
3871 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
3873 return isl_basic_map_lexopt(bmap, 0);
3876 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
3878 return isl_basic_map_lexopt(bmap, 1);
3881 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
3883 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
3886 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
3888 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
3891 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
3893 struct isl_set *dom = NULL;
3894 struct isl_dim *dom_dim;
3898 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
3899 dom = isl_set_universe(dom_dim);
3900 return isl_map_partial_lexopt(map, dom, NULL, max);
3906 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
3908 return isl_map_lexopt(map, 0);
3911 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
3913 return isl_map_lexopt(map, 1);
3916 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
3918 return (isl_set *)isl_map_lexmin((isl_map *)set);
3921 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
3923 return (isl_set *)isl_map_lexmax((isl_map *)set);
3926 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
3927 struct isl_dim *dim)
3934 for (i = 0; i < map->n; ++i) {
3935 isl_dim_free(map->p[i]->dim);
3936 map->p[i]->dim = isl_dim_copy(dim);
3938 isl_dim_free(map->dim);
3948 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
3949 struct isl_dim *dim)
3951 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3954 /* Apply a preimage specified by "mat" on the parameters of "bset".
3955 * bset is assumed to have only parameters and divs.
3957 static struct isl_basic_set *basic_set_parameter_preimage(
3958 struct isl_basic_set *bset, struct isl_mat *mat)
3965 bset->dim = isl_dim_cow(bset->dim);
3969 nparam = isl_basic_set_dim(bset, isl_dim_param);
3971 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
3973 bset->dim->nparam = 0;
3974 bset->dim->n_out = nparam;
3975 bset = isl_basic_set_preimage(bset, mat);
3977 bset->dim->nparam = bset->dim->n_out;
3978 bset->dim->n_out = 0;
3983 isl_basic_set_free(bset);
3987 /* Apply a preimage specified by "mat" on the parameters of "set".
3988 * set is assumed to have only parameters and divs.
3990 static struct isl_set *set_parameter_preimage(
3991 struct isl_set *set, struct isl_mat *mat)
3993 struct isl_dim *dim = NULL;
3999 dim = isl_dim_copy(set->dim);
4000 dim = isl_dim_cow(dim);
4004 nparam = isl_set_dim(set, isl_dim_param);
4006 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4009 dim->n_out = nparam;
4010 isl_set_reset_dim(set, dim);
4011 set = isl_set_preimage(set, mat);
4014 dim = isl_dim_copy(set->dim);
4015 dim = isl_dim_cow(dim);
4018 dim->nparam = dim->n_out;
4020 isl_set_reset_dim(set, dim);
4030 /* Intersect the basic set "bset" with the affine space specified by the
4031 * equalities in "eq".
4033 static struct isl_basic_set *basic_set_append_equalities(
4034 struct isl_basic_set *bset, struct isl_mat *eq)
4042 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4047 len = 1 + isl_dim_total(bset->dim) + bset->extra;
4048 for (i = 0; i < eq->n_row; ++i) {
4049 k = isl_basic_set_alloc_equality(bset);
4052 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4053 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4060 isl_basic_set_free(bset);
4064 /* Intersect the set "set" with the affine space specified by the
4065 * equalities in "eq".
4067 static struct isl_set *set_append_equalities(struct isl_set *set,
4075 for (i = 0; i < set->n; ++i) {
4076 set->p[i] = basic_set_append_equalities(set->p[i],
4089 /* Project the given basic set onto its parameter domain, possibly introducing
4090 * new, explicit, existential variables in the constraints.
4091 * The input has parameters and (possibly implicit) existential variables.
4092 * The output has the same parameters, but only
4093 * explicit existentially quantified variables.
4095 * The actual projection is performed by pip, but pip doesn't seem
4096 * to like equalities very much, so we first remove the equalities
4097 * among the parameters by performing a variable compression on
4098 * the parameters. Afterward, an inverse transformation is performed
4099 * and the equalities among the parameters are inserted back in.
4101 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4105 struct isl_mat *T, *T2;
4106 struct isl_set *set;
4107 unsigned nparam, n_div;
4109 bset = isl_basic_set_cow(bset);
4113 if (bset->n_eq == 0)
4114 return isl_basic_set_lexmin(bset);
4116 isl_basic_set_gauss(bset, NULL);
4118 nparam = isl_basic_set_dim(bset, isl_dim_param);
4119 n_div = isl_basic_set_dim(bset, isl_dim_div);
4121 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4122 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4125 if (i == bset->n_eq)
4126 return isl_basic_set_lexmin(bset);
4128 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4130 eq = isl_mat_cow(eq);
4131 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4132 bset = basic_set_parameter_preimage(bset, T);
4134 set = isl_basic_set_lexmin(bset);
4135 set = set_parameter_preimage(set, T2);
4136 set = set_append_equalities(set, eq);
4140 /* Compute an explicit representation for all the existentially
4141 * quantified variables.
4142 * The input and output dimensions are first turned into parameters.
4143 * compute_divs then returns a map with the same parameters and
4144 * no input or output dimensions and the dimension specification
4145 * is reset to that of the input.
4147 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4149 struct isl_basic_set *bset;
4150 struct isl_set *set;
4151 struct isl_map *map;
4152 struct isl_dim *dim, *orig_dim = NULL;
4157 bmap = isl_basic_map_cow(bmap);
4161 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4162 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4163 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4164 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4168 orig_dim = bmap->dim;
4170 bset = (struct isl_basic_set *)bmap;
4172 set = parameter_compute_divs(bset);
4173 map = (struct isl_map *)set;
4174 map = isl_map_reset_dim(map, orig_dim);
4178 isl_basic_map_free(bmap);
4182 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4190 off = isl_dim_total(bmap->dim);
4191 for (i = 0; i < bmap->n_div; ++i) {
4192 if (isl_int_is_zero(bmap->div[i][0]))
4194 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
4200 static int map_divs_known(__isl_keep isl_map *map)
4207 for (i = 0; i < map->n; ++i) {
4208 int known = basic_map_divs_known(map->p[i]);
4216 /* If bmap contains any unknown divs, then compute explicit
4217 * expressions for them. However, this computation may be
4218 * quite expensive, so first try to remove divs that aren't
4221 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
4225 struct isl_map *map;
4227 known = basic_map_divs_known(bmap);
4231 return isl_map_from_basic_map(bmap);
4233 bmap = isl_basic_map_drop_redundant_divs(bmap);
4235 known = basic_map_divs_known(bmap);
4239 return isl_map_from_basic_map(bmap);
4241 map = compute_divs(bmap);
4244 isl_basic_map_free(bmap);
4248 struct isl_map *isl_map_compute_divs(struct isl_map *map)
4252 struct isl_map *res;
4259 known = map_divs_known(map);
4267 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
4268 for (i = 1 ; i < map->n; ++i) {
4270 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
4271 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4272 res = isl_map_union_disjoint(res, r2);
4274 res = isl_map_union(res, r2);
4281 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
4283 return (struct isl_set *)
4284 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
4287 struct isl_set *isl_set_compute_divs(struct isl_set *set)
4289 return (struct isl_set *)
4290 isl_map_compute_divs((struct isl_map *)set);
4293 struct isl_set *isl_map_domain(struct isl_map *map)
4296 struct isl_set *set;
4301 map = isl_map_cow(map);
4305 set = (struct isl_set *)map;
4306 set->dim = isl_dim_domain(set->dim);
4309 for (i = 0; i < map->n; ++i) {
4310 set->p[i] = isl_basic_map_domain(map->p[i]);
4314 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4315 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4322 struct isl_map *isl_map_union_disjoint(
4323 struct isl_map *map1, struct isl_map *map2)
4327 struct isl_map *map = NULL;
4341 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4343 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4344 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4345 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4347 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
4348 map1->n + map2->n, flags);
4351 for (i = 0; i < map1->n; ++i) {
4352 map = isl_map_add_basic_map(map,
4353 isl_basic_map_copy(map1->p[i]));
4357 for (i = 0; i < map2->n; ++i) {
4358 map = isl_map_add_basic_map(map,
4359 isl_basic_map_copy(map2->p[i]));
4373 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
4375 map1 = isl_map_union_disjoint(map1, map2);
4379 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
4383 struct isl_set *isl_set_union_disjoint(
4384 struct isl_set *set1, struct isl_set *set2)
4386 return (struct isl_set *)
4387 isl_map_union_disjoint(
4388 (struct isl_map *)set1, (struct isl_map *)set2);
4391 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
4393 return (struct isl_set *)
4394 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
4397 struct isl_map *isl_map_intersect_range(
4398 struct isl_map *map, struct isl_set *set)
4401 struct isl_map *result;
4407 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
4408 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
4409 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4411 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
4412 map->n * set->n, flags);
4415 for (i = 0; i < map->n; ++i)
4416 for (j = 0; j < set->n; ++j) {
4417 result = isl_map_add_basic_map(result,
4418 isl_basic_map_intersect_range(
4419 isl_basic_map_copy(map->p[i]),
4420 isl_basic_set_copy(set->p[j])));
4433 struct isl_map *isl_map_intersect_domain(
4434 struct isl_map *map, struct isl_set *set)
4436 return isl_map_reverse(
4437 isl_map_intersect_range(isl_map_reverse(map), set));
4440 struct isl_map *isl_map_apply_domain(
4441 struct isl_map *map1, struct isl_map *map2)
4445 map1 = isl_map_reverse(map1);
4446 map1 = isl_map_apply_range(map1, map2);
4447 return isl_map_reverse(map1);
4454 struct isl_map *isl_map_apply_range(
4455 struct isl_map *map1, struct isl_map *map2)
4457 struct isl_dim *dim_result;
4458 struct isl_map *result;
4464 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
4465 isl_dim_copy(map2->dim));
4467 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
4470 for (i = 0; i < map1->n; ++i)
4471 for (j = 0; j < map2->n; ++j) {
4472 result = isl_map_add_basic_map(result,
4473 isl_basic_map_apply_range(
4474 isl_basic_map_copy(map1->p[i]),
4475 isl_basic_map_copy(map2->p[j])));
4481 if (result && result->n <= 1)
4482 ISL_F_SET(result, ISL_MAP_DISJOINT);
4491 * returns range - domain
4493 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
4495 struct isl_basic_set *bset;
4502 dim = isl_basic_map_n_in(bmap);
4503 nparam = isl_basic_map_n_param(bmap);
4504 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
4505 bset = isl_basic_set_from_basic_map(bmap);
4506 bset = isl_basic_set_cow(bset);
4507 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
4508 bset = isl_basic_set_swap_vars(bset, 2*dim);
4509 for (i = 0; i < dim; ++i) {
4510 int j = isl_basic_map_alloc_equality(
4511 (struct isl_basic_map *)bset);
4514 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
4515 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
4516 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
4517 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
4519 return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
4521 isl_basic_map_free(bmap);
4526 * returns range - domain
4528 struct isl_set *isl_map_deltas(struct isl_map *map)
4531 struct isl_set *result;
4536 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
4537 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
4538 isl_map_n_in(map), map->n, map->flags);
4541 for (i = 0; i < map->n; ++i)
4542 result = isl_set_add_basic_set(result,
4543 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
4551 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
4553 struct isl_basic_map *bmap;
4561 nparam = dims->nparam;
4563 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
4567 for (i = 0; i < dim; ++i) {
4568 int j = isl_basic_map_alloc_equality(bmap);
4571 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
4572 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
4573 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
4575 return isl_basic_map_finalize(bmap);
4577 isl_basic_map_free(bmap);
4581 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
4583 struct isl_dim *dim = isl_dim_map(set_dim);
4586 return basic_map_identity(dim);
4589 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
4591 if (!model || !model->dim)
4593 isl_assert(model->ctx,
4594 model->dim->n_in == model->dim->n_out, return NULL);
4595 return basic_map_identity(isl_dim_copy(model->dim));
4598 static struct isl_map *map_identity(struct isl_dim *dim)
4600 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
4601 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
4604 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
4606 struct isl_dim *dim = isl_dim_map(set_dim);
4609 return map_identity(dim);
4612 struct isl_map *isl_map_identity_like(struct isl_map *model)
4614 if (!model || !model->dim)
4616 isl_assert(model->ctx,
4617 model->dim->n_in == model->dim->n_out, return NULL);
4618 return map_identity(isl_dim_copy(model->dim));
4621 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
4623 if (!model || !model->dim)
4625 isl_assert(model->ctx,
4626 model->dim->n_in == model->dim->n_out, return NULL);
4627 return map_identity(isl_dim_copy(model->dim));
4630 /* Construct a basic set with all set dimensions having only non-negative
4633 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
4638 struct isl_basic_set *bset;
4642 nparam = dims->nparam;
4644 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
4647 for (i = 0; i < dim; ++i) {
4648 int k = isl_basic_set_alloc_inequality(bset);
4651 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
4652 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
4656 isl_basic_set_free(bset);
4660 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
4662 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
4665 int isl_basic_map_is_subset(
4666 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4669 struct isl_map *map1;
4670 struct isl_map *map2;
4672 if (!bmap1 || !bmap2)
4675 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
4676 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
4678 is_subset = isl_map_is_subset(map1, map2);
4686 int isl_basic_map_is_equal(
4687 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4691 if (!bmap1 || !bmap2)
4693 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4696 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4700 int isl_basic_set_is_equal(
4701 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4703 return isl_basic_map_is_equal(
4704 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
4707 int isl_map_is_empty(struct isl_map *map)
4714 for (i = 0; i < map->n; ++i) {
4715 is_empty = isl_basic_map_is_empty(map->p[i]);
4724 int isl_map_fast_is_empty(struct isl_map *map)
4729 int isl_set_fast_is_empty(struct isl_set *set)
4734 int isl_set_is_empty(struct isl_set *set)
4736 return isl_map_is_empty((struct isl_map *)set);
4739 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
4745 is_subset = isl_map_is_subset(map1, map2);
4748 is_subset = isl_map_is_subset(map2, map1);
4752 int isl_basic_map_is_strict_subset(
4753 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4757 if (!bmap1 || !bmap2)
4759 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4762 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4763 if (is_subset == -1)
4768 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
4774 is_subset = isl_map_is_subset(map1, map2);
4777 is_subset = isl_map_is_subset(map2, map1);
4778 if (is_subset == -1)
4783 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4785 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
4788 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
4792 return bmap->n_eq == 0 && bmap->n_ineq == 0;
4795 int isl_basic_set_is_universe(struct isl_basic_set *bset)
4799 return bset->n_eq == 0 && bset->n_ineq == 0;
4802 int isl_map_fast_is_universe(__isl_keep isl_map *map)
4807 return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
4810 int isl_set_fast_is_universe(__isl_keep isl_set *set)
4812 return isl_map_fast_is_universe((isl_map *) set);
4815 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
4817 struct isl_basic_set *bset = NULL;
4818 struct isl_vec *sample = NULL;
4825 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4828 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
4829 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
4830 copy = isl_basic_map_convex_hull(copy);
4831 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
4832 isl_basic_map_free(copy);
4836 total = 1 + isl_basic_map_total_dim(bmap);
4837 if (bmap->sample && bmap->sample->size == total) {
4838 int contains = isl_basic_map_contains(bmap, bmap->sample);
4844 isl_vec_free(bmap->sample);
4845 bmap->sample = NULL;
4846 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
4849 sample = isl_basic_set_sample_vec(bset);
4852 empty = sample->size == 0;
4853 isl_vec_free(bmap->sample);
4854 bmap->sample = sample;
4856 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
4861 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
4865 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
4868 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
4872 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
4875 int isl_basic_set_is_empty(struct isl_basic_set *bset)
4877 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
4880 struct isl_map *isl_basic_map_union(
4881 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4883 struct isl_map *map;
4884 if (!bmap1 || !bmap2)
4887 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
4889 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
4892 map = isl_map_add_basic_map(map, bmap1);
4893 map = isl_map_add_basic_map(map, bmap2);
4896 isl_basic_map_free(bmap1);
4897 isl_basic_map_free(bmap2);
4901 struct isl_set *isl_basic_set_union(
4902 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4904 return (struct isl_set *)isl_basic_map_union(
4905 (struct isl_basic_map *)bset1,
4906 (struct isl_basic_map *)bset2);
4909 /* Order divs such that any div only depends on previous divs */
4910 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
4913 unsigned off = isl_dim_total(bmap->dim);
4915 for (i = 0; i < bmap->n_div; ++i) {
4917 if (isl_int_is_zero(bmap->div[i][0]))
4919 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
4923 isl_basic_map_swap_div(bmap, i, i + pos);
4929 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
4931 return (struct isl_basic_set *)
4932 isl_basic_map_order_divs((struct isl_basic_map *)bset);
4935 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
4942 for (i = 0; i < map->n; ++i) {
4943 map->p[i] = isl_basic_map_order_divs(map->p[i]);
4954 /* Look for a div in dst that corresponds to the div "div" in src.
4955 * The divs before "div" in src and dst are assumed to be the same.
4957 * Returns -1 if no corresponding div was found and the position
4958 * of the corresponding div in dst otherwise.
4960 static int find_div(struct isl_basic_map *dst,
4961 struct isl_basic_map *src, unsigned div)
4965 unsigned total = isl_dim_total(src->dim);
4967 isl_assert(dst->ctx, div <= dst->n_div, return -1);
4968 for (i = div; i < dst->n_div; ++i)
4969 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
4970 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
4971 dst->n_div - div) == -1)
4976 struct isl_basic_map *isl_basic_map_align_divs(
4977 struct isl_basic_map *dst, struct isl_basic_map *src)
4980 unsigned total = isl_dim_total(src->dim);
4985 if (src->n_div == 0)
4988 for (i = 0; i < src->n_div; ++i)
4989 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
4991 src = isl_basic_map_order_divs(src);
4992 dst = isl_basic_map_cow(dst);
4993 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
4994 src->n_div, 0, 2 * src->n_div);
4997 for (i = 0; i < src->n_div; ++i) {
4998 int j = find_div(dst, src, i);
5000 j = isl_basic_map_alloc_div(dst);
5003 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
5004 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
5005 if (isl_basic_map_add_div_constraints(dst, j) < 0)
5009 isl_basic_map_swap_div(dst, i, j);
5013 isl_basic_map_free(dst);
5017 struct isl_basic_set *isl_basic_set_align_divs(
5018 struct isl_basic_set *dst, struct isl_basic_set *src)
5020 return (struct isl_basic_set *)isl_basic_map_align_divs(
5021 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
5024 struct isl_map *isl_map_align_divs(struct isl_map *map)
5032 map = isl_map_compute_divs(map);
5033 map = isl_map_cow(map);
5037 for (i = 1; i < map->n; ++i)
5038 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5039 for (i = 1; i < map->n; ++i)
5040 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5042 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5046 struct isl_set *isl_set_align_divs(struct isl_set *set)
5048 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5051 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5055 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5056 map = isl_map_intersect_domain(map, set);
5057 set = isl_map_range(map);
5065 /* There is no need to cow as removing empty parts doesn't change
5066 * the meaning of the set.
5068 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5075 for (i = map->n-1; i >= 0; --i) {
5076 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5078 isl_basic_map_free(map->p[i]);
5079 if (i != map->n-1) {
5080 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5081 map->p[i] = map->p[map->n-1];
5089 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
5091 return (struct isl_set *)
5092 isl_map_remove_empty_parts((struct isl_map *)set);
5095 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
5097 struct isl_basic_map *bmap;
5098 if (!map || map->n == 0)
5100 bmap = map->p[map->n-1];
5101 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
5102 return isl_basic_map_copy(bmap);
5105 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
5107 return (struct isl_basic_set *)
5108 isl_map_copy_basic_map((struct isl_map *)set);
5111 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
5112 __isl_keep isl_basic_map *bmap)
5118 for (i = map->n-1; i >= 0; --i) {
5119 if (map->p[i] != bmap)
5121 map = isl_map_cow(map);
5124 isl_basic_map_free(map->p[i]);
5125 if (i != map->n-1) {
5126 ISL_F_CLR(map, ISL_SET_NORMALIZED);
5127 map->p[i] = map->p[map->n-1];
5138 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
5139 struct isl_basic_set *bset)
5141 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
5142 (struct isl_basic_map *)bset);
5145 /* Given two basic sets bset1 and bset2, compute the maximal difference
5146 * between the values of dimension pos in bset1 and those in bset2
5147 * for any common value of the parameters and dimensions preceding pos.
5149 static enum isl_lp_result basic_set_maximal_difference_at(
5150 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
5151 int pos, isl_int *opt)
5153 struct isl_dim *dims;
5154 struct isl_basic_map *bmap1 = NULL;
5155 struct isl_basic_map *bmap2 = NULL;
5156 struct isl_ctx *ctx;
5157 struct isl_vec *obj;
5160 unsigned dim1, dim2;
5161 enum isl_lp_result res;
5163 if (!bset1 || !bset2)
5164 return isl_lp_error;
5166 nparam = isl_basic_set_n_param(bset1);
5167 dim1 = isl_basic_set_n_dim(bset1);
5168 dim2 = isl_basic_set_n_dim(bset2);
5169 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
5170 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
5171 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
5172 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
5173 if (!bmap1 || !bmap2)
5175 bmap1 = isl_basic_map_cow(bmap1);
5176 bmap1 = isl_basic_map_extend(bmap1, nparam,
5177 pos, (dim1 - pos) + (dim2 - pos),
5178 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
5179 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
5182 total = isl_basic_map_total_dim(bmap1);
5184 obj = isl_vec_alloc(ctx, 1 + total);
5185 isl_seq_clr(obj->block.data, 1 + total);
5186 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
5187 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
5190 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
5192 isl_basic_map_free(bmap1);
5196 isl_basic_map_free(bmap1);
5197 isl_basic_map_free(bmap2);
5198 return isl_lp_error;
5201 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5202 * for any common value of the parameters and dimensions preceding pos
5203 * in both basic sets, the values of dimension pos in bset1 are
5204 * smaller or larger than those in bset2.
5207 * 1 if bset1 follows bset2
5208 * -1 if bset1 precedes bset2
5209 * 0 if bset1 and bset2 are incomparable
5210 * -2 if some error occurred.
5212 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
5213 struct isl_basic_set *bset2, int pos)
5216 enum isl_lp_result res;
5221 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5223 if (res == isl_lp_empty)
5225 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5226 res == isl_lp_unbounded)
5228 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5237 /* Given two basic sets bset1 and bset2, check whether
5238 * for any common value of the parameters and dimensions preceding pos
5239 * there is a value of dimension pos in bset1 that is larger
5240 * than a value of the same dimension in bset2.
5243 * 1 if there exists such a pair
5244 * 0 if there is no such pair, but there is a pair of equal values
5246 * -2 if some error occurred.
5248 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
5249 __isl_keep isl_basic_set *bset2, int pos)
5252 enum isl_lp_result res;
5257 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5259 if (res == isl_lp_empty)
5261 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5262 res == isl_lp_unbounded)
5264 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5266 else if (res == isl_lp_ok)
5275 /* Given two sets set1 and set2, check whether
5276 * for any common value of the parameters and dimensions preceding pos
5277 * there is a value of dimension pos in set1 that is larger
5278 * than a value of the same dimension in set2.
5281 * 1 if there exists such a pair
5282 * 0 if there is no such pair, but there is a pair of equal values
5284 * -2 if some error occurred.
5286 int isl_set_follows_at(__isl_keep isl_set *set1,
5287 __isl_keep isl_set *set2, int pos)
5295 for (i = 0; i < set1->n; ++i)
5296 for (j = 0; j < set2->n; ++j) {
5298 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
5299 if (f == 1 || f == -2)
5308 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
5309 unsigned pos, isl_int *val)
5317 total = isl_basic_map_total_dim(bmap);
5318 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5319 for (; d+1 > pos; --d)
5320 if (!isl_int_is_zero(bmap->eq[i][1+d]))
5324 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5326 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5328 if (!isl_int_is_one(bmap->eq[i][1+d]))
5331 isl_int_neg(*val, bmap->eq[i][0]);
5337 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5338 unsigned pos, isl_int *val)
5350 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
5353 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
5354 for (i = 1; fixed == 1 && i < map->n; ++i) {
5355 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
5356 if (fixed == 1 && isl_int_ne(tmp, v))
5360 isl_int_set(*val, v);
5366 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5367 unsigned pos, isl_int *val)
5369 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
5373 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
5376 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
5379 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
5380 enum isl_dim_type type, unsigned pos, isl_int *val)
5382 if (pos >= isl_basic_map_dim(bmap, type))
5384 return isl_basic_map_fast_has_fixed_var(bmap,
5385 isl_basic_map_offset(bmap, type) - 1 + pos, val);
5388 int isl_map_fast_is_fixed(struct isl_map *map,
5389 enum isl_dim_type type, unsigned pos, isl_int *val)
5391 if (pos >= isl_map_dim(map, type))
5393 return isl_map_fast_has_fixed_var(map,
5394 map_offset(map, type) - 1 + pos, val);
5397 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5398 * then return this fixed value in *val.
5400 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
5403 return isl_basic_set_fast_has_fixed_var(bset,
5404 isl_basic_set_n_param(bset) + dim, val);
5407 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5408 * then return this fixed value in *val.
5410 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
5412 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
5415 /* Check if input variable in has fixed value and if so and if val is not NULL,
5416 * then return this fixed value in *val.
5418 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
5420 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
5423 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5424 * and if val is not NULL, then return this lower bound in *val.
5426 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
5427 unsigned dim, isl_int *val)
5429 int i, i_eq = -1, i_ineq = -1;
5436 total = isl_basic_set_total_dim(bset);
5437 nparam = isl_basic_set_n_param(bset);
5438 for (i = 0; i < bset->n_eq; ++i) {
5439 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
5445 for (i = 0; i < bset->n_ineq; ++i) {
5446 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
5448 if (i_eq != -1 || i_ineq != -1)
5452 if (i_eq == -1 && i_ineq == -1)
5454 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
5455 /* The coefficient should always be one due to normalization. */
5456 if (!isl_int_is_one(c[1+nparam+dim]))
5458 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
5460 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
5461 total - nparam - dim - 1) != -1)
5464 isl_int_neg(*val, c[0]);
5468 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
5469 unsigned dim, isl_int *val)
5481 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5485 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5487 for (i = 1; fixed == 1 && i < set->n; ++i) {
5488 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
5490 if (fixed == 1 && isl_int_ne(tmp, v))
5494 isl_int_set(*val, v);
5505 static int qsort_constraint_cmp(const void *p1, const void *p2)
5507 const struct constraint *c1 = (const struct constraint *)p1;
5508 const struct constraint *c2 = (const struct constraint *)p2;
5509 unsigned size = isl_min(c1->size, c2->size);
5510 return isl_seq_cmp(c1->c, c2->c, size);
5513 static struct isl_basic_map *isl_basic_map_sort_constraints(
5514 struct isl_basic_map *bmap)
5517 struct constraint *c;
5522 total = isl_basic_map_total_dim(bmap);
5523 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
5526 for (i = 0; i < bmap->n_ineq; ++i) {
5528 c[i].c = bmap->ineq[i];
5530 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
5531 for (i = 0; i < bmap->n_ineq; ++i)
5532 bmap->ineq[i] = c[i].c;
5536 isl_basic_map_free(bmap);
5540 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
5544 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
5546 bmap = isl_basic_map_convex_hull(bmap);
5547 bmap = isl_basic_map_sort_constraints(bmap);
5548 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
5552 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
5554 return (struct isl_basic_set *)isl_basic_map_normalize(
5555 (struct isl_basic_map *)bset);
5558 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
5559 const struct isl_basic_map *bmap2)
5566 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
5567 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
5568 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
5569 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5570 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
5571 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5572 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
5573 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5575 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
5577 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5579 if (bmap1->n_eq != bmap2->n_eq)
5580 return bmap1->n_eq - bmap2->n_eq;
5581 if (bmap1->n_ineq != bmap2->n_ineq)
5582 return bmap1->n_ineq - bmap2->n_ineq;
5583 if (bmap1->n_div != bmap2->n_div)
5584 return bmap1->n_div - bmap2->n_div;
5585 total = isl_basic_map_total_dim(bmap1);
5586 for (i = 0; i < bmap1->n_eq; ++i) {
5587 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
5591 for (i = 0; i < bmap1->n_ineq; ++i) {
5592 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
5596 for (i = 0; i < bmap1->n_div; ++i) {
5597 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
5604 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
5605 struct isl_basic_map *bmap2)
5607 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
5610 static int qsort_bmap_cmp(const void *p1, const void *p2)
5612 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
5613 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
5615 return isl_basic_map_fast_cmp(bmap1, bmap2);
5618 /* We normalize in place, but if anything goes wrong we need
5619 * to return NULL, so we need to make sure we don't change the
5620 * meaning of any possible other copies of map.
5622 struct isl_map *isl_map_normalize(struct isl_map *map)
5625 struct isl_basic_map *bmap;
5629 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
5631 for (i = 0; i < map->n; ++i) {
5632 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
5635 isl_basic_map_free(map->p[i]);
5638 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
5639 ISL_F_SET(map, ISL_MAP_NORMALIZED);
5640 map = isl_map_remove_empty_parts(map);
5643 for (i = map->n - 1; i >= 1; --i) {
5644 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
5646 isl_basic_map_free(map->p[i-1]);
5647 for (j = i; j < map->n; ++j)
5648 map->p[j-1] = map->p[j];
5658 struct isl_set *isl_set_normalize(struct isl_set *set)
5660 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
5663 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
5673 if (!isl_dim_equal(map1->dim, map2->dim))
5676 map1 = isl_map_copy(map1);
5677 map2 = isl_map_copy(map2);
5678 map1 = isl_map_normalize(map1);
5679 map2 = isl_map_normalize(map2);
5682 equal = map1->n == map2->n;
5683 for (i = 0; equal && i < map1->n; ++i) {
5684 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
5697 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
5699 return isl_map_fast_is_equal((struct isl_map *)set1,
5700 (struct isl_map *)set2);
5703 /* Return an interval that ranges from min to max (inclusive)
5705 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
5706 isl_int min, isl_int max)
5709 struct isl_basic_set *bset = NULL;
5711 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
5715 k = isl_basic_set_alloc_inequality(bset);
5718 isl_int_set_si(bset->ineq[k][1], 1);
5719 isl_int_neg(bset->ineq[k][0], min);
5721 k = isl_basic_set_alloc_inequality(bset);
5724 isl_int_set_si(bset->ineq[k][1], -1);
5725 isl_int_set(bset->ineq[k][0], max);
5729 isl_basic_set_free(bset);
5733 /* Return the Cartesian product of the basic sets in list (in the given order).
5735 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
5743 struct isl_basic_set *product = NULL;
5747 isl_assert(list->ctx, list->n > 0, goto error);
5748 isl_assert(list->ctx, list->p[0], goto error);
5749 nparam = isl_basic_set_n_param(list->p[0]);
5750 dim = isl_basic_set_n_dim(list->p[0]);
5751 extra = list->p[0]->n_div;
5752 n_eq = list->p[0]->n_eq;
5753 n_ineq = list->p[0]->n_ineq;
5754 for (i = 1; i < list->n; ++i) {
5755 isl_assert(list->ctx, list->p[i], goto error);
5756 isl_assert(list->ctx,
5757 nparam == isl_basic_set_n_param(list->p[i]), goto error);
5758 dim += isl_basic_set_n_dim(list->p[i]);
5759 extra += list->p[i]->n_div;
5760 n_eq += list->p[i]->n_eq;
5761 n_ineq += list->p[i]->n_ineq;
5763 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
5768 for (i = 0; i < list->n; ++i) {
5769 isl_basic_set_add_constraints(product,
5770 isl_basic_set_copy(list->p[i]), dim);
5771 dim += isl_basic_set_n_dim(list->p[i]);
5773 isl_basic_set_list_free(list);
5776 isl_basic_set_free(product);
5777 isl_basic_set_list_free(list);
5781 struct isl_basic_map *isl_basic_map_product(
5782 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5784 struct isl_dim *dim_result = NULL;
5785 struct isl_basic_map *bmap;
5786 unsigned in1, in2, out1, out2, nparam, total, pos;
5787 struct isl_dim_map *dim_map1, *dim_map2;
5789 if (!bmap1 || !bmap2)
5792 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
5793 bmap2->dim, isl_dim_param), goto error);
5794 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
5795 isl_dim_copy(bmap2->dim));
5797 in1 = isl_basic_map_n_in(bmap1);
5798 in2 = isl_basic_map_n_in(bmap2);
5799 out1 = isl_basic_map_n_out(bmap1);
5800 out2 = isl_basic_map_n_out(bmap2);
5801 nparam = isl_basic_map_n_param(bmap1);
5803 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
5804 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5805 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
5806 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5807 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
5808 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5809 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
5810 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
5811 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
5812 isl_dim_map_div(dim_map1, bmap1, pos += out2);
5813 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5815 bmap = isl_basic_map_alloc_dim(dim_result,
5816 bmap1->n_div + bmap2->n_div,
5817 bmap1->n_eq + bmap2->n_eq,
5818 bmap1->n_ineq + bmap2->n_ineq);
5819 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
5820 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
5821 bmap = isl_basic_map_simplify(bmap);
5822 return isl_basic_map_finalize(bmap);
5824 isl_basic_map_free(bmap1);
5825 isl_basic_map_free(bmap2);
5829 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5831 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
5834 struct isl_map *result;
5840 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
5841 map2->dim, isl_dim_param), goto error);
5843 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5844 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5845 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5847 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
5848 isl_dim_copy(map2->dim)),
5849 map1->n * map2->n, flags);
5852 for (i = 0; i < map1->n; ++i)
5853 for (j = 0; j < map2->n; ++j) {
5854 struct isl_basic_map *part;
5855 part = isl_basic_map_product(
5856 isl_basic_map_copy(map1->p[i]),
5857 isl_basic_map_copy(map2->p[j]));
5858 if (isl_basic_map_is_empty(part))
5859 isl_basic_map_free(part);
5861 result = isl_map_add_basic_map(result, part);
5874 /* Given two set A and B, construct its Cartesian product A x B.
5876 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
5878 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
5879 (struct isl_map *)set2);
5882 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
5885 uint32_t hash = isl_hash_init();
5890 bset = isl_basic_set_copy(bset);
5891 bset = isl_basic_set_normalize(bset);
5894 total = isl_basic_set_total_dim(bset);
5895 isl_hash_byte(hash, bset->n_eq & 0xFF);
5896 for (i = 0; i < bset->n_eq; ++i) {
5898 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
5899 isl_hash_hash(hash, c_hash);
5901 isl_hash_byte(hash, bset->n_ineq & 0xFF);
5902 for (i = 0; i < bset->n_ineq; ++i) {
5904 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
5905 isl_hash_hash(hash, c_hash);
5907 isl_hash_byte(hash, bset->n_div & 0xFF);
5908 for (i = 0; i < bset->n_div; ++i) {
5910 if (isl_int_is_zero(bset->div[i][0]))
5912 isl_hash_byte(hash, i & 0xFF);
5913 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
5914 isl_hash_hash(hash, c_hash);
5916 isl_basic_set_free(bset);
5920 uint32_t isl_set_get_hash(struct isl_set *set)
5927 set = isl_set_copy(set);
5928 set = isl_set_normalize(set);
5932 hash = isl_hash_init();
5933 for (i = 0; i < set->n; ++i) {
5935 bset_hash = isl_basic_set_get_hash(set->p[i]);
5936 isl_hash_hash(hash, bset_hash);
5944 /* Check if the value for dimension dim is completely determined
5945 * by the values of the other parameters and variables.
5946 * That is, check if dimension dim is involved in an equality.
5948 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
5955 nparam = isl_basic_set_n_param(bset);
5956 for (i = 0; i < bset->n_eq; ++i)
5957 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
5962 /* Check if the value for dimension dim is completely determined
5963 * by the values of the other parameters and variables.
5964 * That is, check if dimension dim is involved in an equality
5965 * for each of the subsets.
5967 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
5973 for (i = 0; i < set->n; ++i) {
5975 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
5982 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
5983 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
5990 for (i = 0; i < map->n; ++i)
5991 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
5997 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
5998 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
6005 for (i = 0; i < set->n; ++i)
6006 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
6012 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
6014 struct isl_dim *dim;
6015 struct isl_basic_map *bmap;
6022 set = isl_set_align_divs(set);
6027 dim = isl_set_get_dim(set);
6028 if (set->n == 0 || set->p[0]->n_div == 0) {
6030 return isl_map_identity(dim);
6033 n_div = set->p[0]->n_div;
6034 dim = isl_dim_map(dim);
6035 n_param = isl_dim_size(dim, isl_dim_param);
6036 n_set = isl_dim_size(dim, isl_dim_in);
6037 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
6038 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
6039 for (i = 0; i < n_set; ++i)
6040 bmap = var_equal(bmap, i);
6042 total = n_param + n_set + n_set + n_div;
6043 for (i = 0; i < n_div; ++i) {
6044 k = isl_basic_map_alloc_inequality(bmap);
6047 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
6048 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
6049 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
6050 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
6051 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
6052 set->p[0]->div[i][0]);
6054 l = isl_basic_map_alloc_inequality(bmap);
6057 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
6058 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
6059 set->p[0]->div[i][0]);
6060 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
6064 return isl_map_from_basic_map(bmap);
6067 isl_basic_map_free(bmap);
6071 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
6079 dim = isl_basic_set_total_dim(bset);
6080 size += bset->n_eq * (1 + dim);
6081 size += bset->n_ineq * (1 + dim);
6082 size += bset->n_div * (2 + dim);
6087 int isl_set_size(__isl_keep isl_set *set)
6095 for (i = 0; i < set->n; ++i)
6096 size += isl_basic_set_size(set->p[i]);