2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
15 #include "isl_equalities.h"
21 #include "isl_map_private.h"
22 #include "isl_map_piplib.h"
23 #include "isl_sample.h"
27 /* Maps dst positions to src positions */
33 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
36 struct isl_dim_map *dim_map;
37 dim_map = isl_alloc(ctx, struct isl_dim_map,
38 sizeof(struct isl_dim_map) + len * sizeof(int));
41 dim_map->len = 1 + len;
43 for (i = 0; i < len; ++i)
44 dim_map->pos[1 + i] = -1;
48 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
51 case isl_dim_param: return dim->nparam;
52 case isl_dim_in: return dim->n_in;
53 case isl_dim_out: return dim->n_out;
54 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
58 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
61 case isl_dim_param: return 1;
62 case isl_dim_in: return 1 + dim->nparam;
63 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
67 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
68 enum isl_dim_type type, unsigned dst_pos)
76 src_pos = pos(dim, type);
77 for (i = 0; i < n(dim, type); ++i)
78 dim_map->pos[1 + dst_pos + i] = src_pos + i;
81 static void isl_dim_map_div(struct isl_dim_map *dim_map,
82 struct isl_basic_map *bmap, unsigned dst_pos)
87 if (!dim_map || !bmap)
90 src_pos = 1 + isl_dim_total(bmap->dim);
91 for (i = 0; i < bmap->n_div; ++i)
92 dim_map->pos[1 + dst_pos + i] = src_pos + i;
95 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
99 for (i = 0; i < dim_map->len; ++i)
100 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
101 fprintf(stderr, "\n");
104 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
105 enum isl_dim_type type)
110 case isl_dim_out: return isl_dim_size(bmap->dim, type);
111 case isl_dim_div: return bmap->n_div;
112 case isl_dim_all: return isl_basic_map_total_dim(bmap);
116 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
118 return n(map->dim, type);
121 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
123 return n(set->dim, type);
126 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
127 enum isl_dim_type type)
129 struct isl_dim *dim = bmap->dim;
131 case isl_dim_param: return 1;
132 case isl_dim_in: return 1 + dim->nparam;
133 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
134 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
138 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
140 return pos(map->dim, type);
143 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
144 enum isl_dim_type type)
146 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
149 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
151 return bset->dim->n_out;
154 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
156 return bset->dim->nparam;
159 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
161 return isl_dim_total(bset->dim) + bset->n_div;
164 unsigned isl_set_n_dim(const struct isl_set *set)
166 return set->dim->n_out;
169 unsigned isl_set_n_param(const struct isl_set *set)
171 return set->dim->nparam;
174 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
176 return bmap->dim->n_in;
179 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
181 return bmap->dim->n_out;
184 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
186 return bmap->dim->nparam;
189 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
194 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
196 return isl_dim_total(bmap->dim) + bmap->n_div;
199 unsigned isl_map_n_in(const struct isl_map *map)
201 return map->dim->n_in;
204 unsigned isl_map_n_out(const struct isl_map *map)
206 return map->dim->n_out;
209 unsigned isl_map_n_param(const struct isl_map *map)
211 return map->dim->nparam;
214 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
216 return map->dim->n_in == set->dim->n_out &&
217 map->dim->nparam == set->dim->nparam;
220 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
221 struct isl_basic_set *bset)
223 return bmap->dim->n_in == bset->dim->n_out &&
224 bmap->dim->nparam == bset->dim->nparam;
227 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
228 struct isl_basic_set *bset)
230 return bmap->dim->n_out == bset->dim->n_out &&
231 bmap->dim->nparam == bset->dim->nparam;
234 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
238 return isl_dim_copy(bmap->dim);
241 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
245 return isl_dim_copy(bset->dim);
248 struct isl_dim *isl_map_get_dim(struct isl_map *map)
252 return isl_dim_copy(map->dim);
255 struct isl_dim *isl_set_get_dim(struct isl_set *set)
259 return isl_dim_copy(set->dim);
262 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
263 struct isl_basic_map *bmap, unsigned extra,
264 unsigned n_eq, unsigned n_ineq)
267 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
269 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
270 if (isl_blk_is_error(bmap->block)) {
275 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
277 isl_blk_free(ctx, bmap->block);
283 bmap->block2 = isl_blk_empty();
286 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
287 if (isl_blk_is_error(bmap->block2)) {
289 isl_blk_free(ctx, bmap->block);
294 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
296 isl_blk_free(ctx, bmap->block2);
298 isl_blk_free(ctx, bmap->block);
304 for (i = 0; i < n_ineq + n_eq; ++i)
305 bmap->ineq[i] = bmap->block.data + i * row_size;
307 for (i = 0; i < extra; ++i)
308 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
314 bmap->c_size = n_eq + n_ineq;
315 bmap->eq = bmap->ineq + n_ineq;
324 isl_basic_map_free(bmap);
328 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
329 unsigned nparam, unsigned dim, unsigned extra,
330 unsigned n_eq, unsigned n_ineq)
332 struct isl_basic_map *bmap;
333 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
334 return (struct isl_basic_set *)bmap;
337 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
338 unsigned extra, unsigned n_eq, unsigned n_ineq)
340 struct isl_basic_map *bmap;
343 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
344 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
345 return (struct isl_basic_set *)bmap;
348 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
349 unsigned extra, unsigned n_eq, unsigned n_ineq)
351 struct isl_basic_map *bmap;
355 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
360 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
366 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
367 unsigned nparam, unsigned in, unsigned out, unsigned extra,
368 unsigned n_eq, unsigned n_ineq)
370 struct isl_basic_map *bmap;
373 dim = isl_dim_alloc(ctx, nparam, in, out);
377 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
381 static void dup_constraints(
382 struct isl_basic_map *dst, struct isl_basic_map *src)
385 unsigned total = isl_basic_map_total_dim(src);
387 for (i = 0; i < src->n_eq; ++i) {
388 int j = isl_basic_map_alloc_equality(dst);
389 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
392 for (i = 0; i < src->n_ineq; ++i) {
393 int j = isl_basic_map_alloc_inequality(dst);
394 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
397 for (i = 0; i < src->n_div; ++i) {
398 int j = isl_basic_map_alloc_div(dst);
399 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
401 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
404 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
406 struct isl_basic_map *dup;
410 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
411 bmap->n_div, bmap->n_eq, bmap->n_ineq);
414 dup_constraints(dup, bmap);
415 dup->flags = bmap->flags;
416 dup->sample = isl_vec_copy(bmap->sample);
420 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
422 struct isl_basic_map *dup;
424 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
425 return (struct isl_basic_set *)dup;
428 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
433 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
437 return isl_basic_set_dup(bset);
440 struct isl_set *isl_set_copy(struct isl_set *set)
449 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
454 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
458 return isl_basic_map_dup(bmap);
461 struct isl_map *isl_map_copy(struct isl_map *map)
470 void isl_basic_map_free(struct isl_basic_map *bmap)
478 isl_ctx_deref(bmap->ctx);
480 isl_blk_free(bmap->ctx, bmap->block2);
482 isl_blk_free(bmap->ctx, bmap->block);
483 isl_vec_free(bmap->sample);
484 isl_dim_free(bmap->dim);
488 void isl_basic_set_free(struct isl_basic_set *bset)
490 isl_basic_map_free((struct isl_basic_map *)bset);
493 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
495 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
498 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
504 isl_assert(ctx, room_for_con(bmap, 1), return -1);
505 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
507 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
508 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
509 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
510 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
511 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
512 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
514 int j = isl_basic_map_alloc_inequality(bmap);
518 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
519 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
526 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
527 bmap->extra - bmap->n_div);
531 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
533 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
536 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
540 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
545 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
547 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
550 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
555 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
557 if (pos != bmap->n_eq - 1) {
559 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
560 bmap->eq[bmap->n_eq - 1] = t;
566 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
568 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
571 void isl_basic_map_inequality_to_equality(
572 struct isl_basic_map *bmap, unsigned pos)
577 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
578 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
583 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
584 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
585 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
586 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
589 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
591 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
594 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
600 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
601 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
602 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
603 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
604 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
605 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
606 1 + isl_basic_map_total_dim(bmap),
607 bmap->extra - bmap->n_div);
608 return bmap->n_ineq++;
611 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
613 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
616 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
620 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
625 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
627 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
630 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
635 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
637 if (pos != bmap->n_ineq - 1) {
639 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
640 bmap->ineq[bmap->n_ineq - 1] = t;
641 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
647 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
649 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
652 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
657 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
660 k = isl_basic_map_alloc_equality(bmap);
663 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
666 isl_basic_map_free(bmap);
670 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
673 return (isl_basic_set *)
674 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
677 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
682 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
685 k = isl_basic_map_alloc_inequality(bmap);
688 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
691 isl_basic_map_free(bmap);
695 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
698 return (isl_basic_set *)
699 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
702 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
706 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
707 isl_seq_clr(bmap->div[bmap->n_div] +
708 1 + 1 + isl_basic_map_total_dim(bmap),
709 bmap->extra - bmap->n_div);
710 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
711 return bmap->n_div++;
714 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
716 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
719 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
723 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
728 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
730 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
733 /* Copy constraint from src to dst, putting the vars of src at offset
734 * dim_off in dst and the divs of src at offset div_off in dst.
735 * If both sets are actually map, then dim_off applies to the input
738 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
739 struct isl_basic_map *src_map, isl_int *src,
740 unsigned in_off, unsigned out_off, unsigned div_off)
742 unsigned src_nparam = isl_basic_map_n_param(src_map);
743 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
744 unsigned src_in = isl_basic_map_n_in(src_map);
745 unsigned dst_in = isl_basic_map_n_in(dst_map);
746 unsigned src_out = isl_basic_map_n_out(src_map);
747 unsigned dst_out = isl_basic_map_n_out(dst_map);
748 isl_int_set(dst[0], src[0]);
749 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
750 if (dst_nparam > src_nparam)
751 isl_seq_clr(dst+1+src_nparam,
752 dst_nparam - src_nparam);
753 isl_seq_clr(dst+1+dst_nparam, in_off);
754 isl_seq_cpy(dst+1+dst_nparam+in_off,
756 isl_min(dst_in-in_off, src_in));
757 if (dst_in-in_off > src_in)
758 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
759 dst_in - in_off - src_in);
760 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
761 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
762 src+1+src_nparam+src_in,
763 isl_min(dst_out-out_off, src_out));
764 if (dst_out-out_off > src_out)
765 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
766 dst_out - out_off - src_out);
767 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
768 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
769 src+1+src_nparam+src_in+src_out,
770 isl_min(dst_map->extra-div_off, src_map->n_div));
771 if (dst_map->n_div-div_off > src_map->n_div)
772 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
773 div_off+src_map->n_div,
774 dst_map->n_div - div_off - src_map->n_div);
777 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
778 struct isl_basic_map *src_map, isl_int *src,
779 unsigned in_off, unsigned out_off, unsigned div_off)
781 isl_int_set(dst[0], src[0]);
782 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
785 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
786 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
791 if (!bmap1 || !bmap2)
794 div_off = bmap1->n_div;
796 for (i = 0; i < bmap2->n_eq; ++i) {
797 int i1 = isl_basic_map_alloc_equality(bmap1);
800 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
801 i_pos, o_pos, div_off);
804 for (i = 0; i < bmap2->n_ineq; ++i) {
805 int i1 = isl_basic_map_alloc_inequality(bmap1);
808 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
809 i_pos, o_pos, div_off);
812 for (i = 0; i < bmap2->n_div; ++i) {
813 int i1 = isl_basic_map_alloc_div(bmap1);
816 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
817 i_pos, o_pos, div_off);
820 isl_basic_map_free(bmap2);
825 isl_basic_map_free(bmap1);
826 isl_basic_map_free(bmap2);
830 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
831 struct isl_dim_map *dim_map)
835 for (i = 0; i < dim_map->len; ++i) {
836 if (dim_map->pos[i] < 0)
837 isl_int_set_si(dst[i], 0);
839 isl_int_set(dst[i], src[dim_map->pos[i]]);
843 static void copy_div_dim_map(isl_int *dst, isl_int *src,
844 struct isl_dim_map *dim_map)
846 isl_int_set(dst[0], src[0]);
847 copy_constraint_dim_map(dst+1, src+1, dim_map);
850 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
851 struct isl_basic_map *src, struct isl_dim_map *dim_map)
855 if (!src || !dst || !dim_map)
858 for (i = 0; i < src->n_eq; ++i) {
859 int i1 = isl_basic_map_alloc_equality(dst);
862 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
865 for (i = 0; i < src->n_ineq; ++i) {
866 int i1 = isl_basic_map_alloc_inequality(dst);
869 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
872 for (i = 0; i < src->n_div; ++i) {
873 int i1 = isl_basic_map_alloc_div(dst);
876 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
880 isl_basic_map_free(src);
885 isl_basic_map_free(src);
886 isl_basic_map_free(dst);
890 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
891 struct isl_basic_set *bset2, unsigned pos)
893 return (struct isl_basic_set *)
894 add_constraints((struct isl_basic_map *)bset1,
895 (struct isl_basic_map *)bset2, 0, pos);
898 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
899 struct isl_dim *dim, unsigned extra,
900 unsigned n_eq, unsigned n_ineq)
902 struct isl_basic_map *ext;
912 dims_ok = isl_dim_equal(base->dim, dim) &&
913 base->extra >= base->n_div + extra;
915 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
916 room_for_ineq(base, n_ineq)) {
921 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
922 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
923 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
924 extra += base->extra;
926 n_ineq += base->n_ineq;
928 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
934 ext->sample = isl_vec_copy(base->sample);
936 ext = add_constraints(ext, base, 0, 0);
939 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
946 isl_basic_map_free(base);
950 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
951 struct isl_dim *dim, unsigned extra,
952 unsigned n_eq, unsigned n_ineq)
954 return (struct isl_basic_set *)
955 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
956 extra, n_eq, n_ineq);
959 struct isl_basic_map *isl_basic_map_extend_constraints(
960 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
964 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
968 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
969 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
970 unsigned n_eq, unsigned n_ineq)
972 struct isl_basic_map *bmap;
977 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
981 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
985 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
986 unsigned nparam, unsigned dim, unsigned extra,
987 unsigned n_eq, unsigned n_ineq)
989 return (struct isl_basic_set *)
990 isl_basic_map_extend((struct isl_basic_map *)base,
991 nparam, 0, dim, extra, n_eq, n_ineq);
994 struct isl_basic_set *isl_basic_set_extend_constraints(
995 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
997 return (struct isl_basic_set *)
998 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1002 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1004 return (struct isl_basic_set *)
1005 isl_basic_map_cow((struct isl_basic_map *)bset);
1008 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1013 if (bmap->ref > 1) {
1015 bmap = isl_basic_map_dup(bmap);
1017 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1021 struct isl_set *isl_set_cow(struct isl_set *set)
1029 return isl_set_dup(set);
1032 struct isl_map *isl_map_cow(struct isl_map *map)
1040 return isl_map_dup(map);
1043 static void swap_vars(struct isl_blk blk, isl_int *a,
1044 unsigned a_len, unsigned b_len)
1046 isl_seq_cpy(blk.data, a+a_len, b_len);
1047 isl_seq_cpy(blk.data+b_len, a, a_len);
1048 isl_seq_cpy(a, blk.data, b_len+a_len);
1051 struct isl_basic_set *isl_basic_set_swap_vars(
1052 struct isl_basic_set *bset, unsigned n)
1062 nparam = isl_basic_set_n_param(bset);
1063 dim = isl_basic_set_n_dim(bset);
1064 isl_assert(bset->ctx, n <= dim, goto error);
1069 bset = isl_basic_set_cow(bset);
1073 blk = isl_blk_alloc(bset->ctx, dim);
1074 if (isl_blk_is_error(blk))
1077 for (i = 0; i < bset->n_eq; ++i)
1079 bset->eq[i]+1+nparam, n, dim - n);
1081 for (i = 0; i < bset->n_ineq; ++i)
1083 bset->ineq[i]+1+nparam, n, dim - n);
1085 for (i = 0; i < bset->n_div; ++i)
1087 bset->div[i]+1+1+nparam, n, dim - n);
1089 isl_blk_free(bset->ctx, blk);
1091 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1092 return isl_basic_set_gauss(bset, NULL);
1094 isl_basic_set_free(bset);
1098 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1101 set = isl_set_cow(set);
1105 for (i = 0; i < set->n; ++i) {
1106 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1112 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1116 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1122 total = isl_basic_map_total_dim(bmap);
1123 isl_basic_map_free_div(bmap, bmap->n_div);
1124 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1126 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1128 isl_basic_map_alloc_equality(bmap);
1132 isl_int_set_si(bmap->eq[i][0], 1);
1133 isl_seq_clr(bmap->eq[i]+1, total);
1134 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1135 isl_vec_free(bmap->sample);
1136 bmap->sample = NULL;
1137 return isl_basic_map_finalize(bmap);
1139 isl_basic_map_free(bmap);
1143 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1145 return (struct isl_basic_set *)
1146 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1149 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1152 unsigned off = isl_dim_total(bmap->dim);
1153 isl_int *t = bmap->div[a];
1154 bmap->div[a] = bmap->div[b];
1157 for (i = 0; i < bmap->n_eq; ++i)
1158 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1160 for (i = 0; i < bmap->n_ineq; ++i)
1161 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1163 for (i = 0; i < bmap->n_div; ++i)
1164 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1165 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1168 /* Eliminate the specified n dimensions starting at first from the
1169 * constraints using Fourier-Motzkin, The dimensions themselves
1172 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1173 unsigned first, unsigned n)
1183 set = isl_set_cow(set);
1186 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1187 nparam = isl_set_n_param(set);
1189 for (i = 0; i < set->n; ++i) {
1190 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1201 /* Project out n dimensions starting at first using Fourier-Motzkin */
1202 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1203 unsigned first, unsigned n)
1205 set = isl_set_eliminate_dims(set, first, n);
1206 set = isl_set_drop_dims(set, first, n);
1210 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1212 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1220 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1229 set = isl_set_cow(set);
1233 for (i = 0; i < set->n; ++i) {
1234 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1244 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1245 enum isl_dim_type type, unsigned first, unsigned n)
1249 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1253 bmap = isl_basic_map_eliminate_vars(bmap,
1254 isl_basic_map_offset(bmap, type) - 1 + first, n);
1257 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1259 bmap = isl_basic_map_drop(bmap, type, first, n);
1262 isl_basic_map_free(bmap);
1266 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1267 enum isl_dim_type type, unsigned first, unsigned n)
1269 return (isl_basic_set *)
1270 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1273 struct isl_map *isl_map_remove(struct isl_map *map,
1274 enum isl_dim_type type, unsigned first, unsigned n)
1281 map = isl_map_cow(map);
1284 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1286 for (i = 0; i < map->n; ++i) {
1287 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1288 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1292 map = isl_map_drop(map, type, first, n);
1299 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1300 enum isl_dim_type type, unsigned first, unsigned n)
1302 return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1305 /* Project out n inputs starting at first using Fourier-Motzkin */
1306 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1307 unsigned first, unsigned n)
1309 return isl_map_remove(map, isl_dim_in, first, n);
1312 /* Project out n dimensions starting at first using Fourier-Motzkin */
1313 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1314 unsigned first, unsigned n)
1316 unsigned nparam = isl_basic_set_n_param(bset);
1317 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1318 bset = isl_basic_set_drop_dims(bset, first, n);
1322 static void dump_term(struct isl_basic_map *bmap,
1323 isl_int c, int pos, FILE *out)
1326 unsigned in = isl_basic_map_n_in(bmap);
1327 unsigned dim = in + isl_basic_map_n_out(bmap);
1328 unsigned nparam = isl_basic_map_n_param(bmap);
1330 isl_int_print(out, c, 0);
1332 if (!isl_int_is_one(c))
1333 isl_int_print(out, c, 0);
1334 if (pos < 1 + nparam) {
1335 name = isl_dim_get_name(bmap->dim,
1336 isl_dim_param, pos - 1);
1338 fprintf(out, "%s", name);
1340 fprintf(out, "p%d", pos - 1);
1341 } else if (pos < 1 + nparam + in)
1342 fprintf(out, "i%d", pos - 1 - nparam);
1343 else if (pos < 1 + nparam + dim)
1344 fprintf(out, "o%d", pos - 1 - nparam - in);
1346 fprintf(out, "e%d", pos - 1 - nparam - dim);
1350 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1351 int sign, FILE *out)
1355 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1359 for (i = 0, first = 1; i < len; ++i) {
1360 if (isl_int_sgn(c[i]) * sign <= 0)
1363 fprintf(out, " + ");
1365 isl_int_abs(v, c[i]);
1366 dump_term(bmap, v, i, out);
1373 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1374 const char *op, FILE *out, int indent)
1378 fprintf(out, "%*s", indent, "");
1380 dump_constraint_sign(bmap, c, 1, out);
1381 fprintf(out, " %s ", op);
1382 dump_constraint_sign(bmap, c, -1, out);
1386 for (i = bmap->n_div; i < bmap->extra; ++i) {
1387 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1389 fprintf(out, "%*s", indent, "");
1390 fprintf(out, "ERROR: unused div coefficient not zero\n");
1395 static void dump_constraints(struct isl_basic_map *bmap,
1396 isl_int **c, unsigned n,
1397 const char *op, FILE *out, int indent)
1401 for (i = 0; i < n; ++i)
1402 dump_constraint(bmap, c[i], op, out, indent);
1405 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1409 unsigned total = isl_basic_map_total_dim(bmap);
1411 for (j = 0; j < 1 + total; ++j) {
1412 if (isl_int_is_zero(exp[j]))
1414 if (!first && isl_int_is_pos(exp[j]))
1416 dump_term(bmap, exp[j], j, out);
1421 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1425 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1426 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1428 for (i = 0; i < bmap->n_div; ++i) {
1429 fprintf(out, "%*s", indent, "");
1430 fprintf(out, "e%d = [(", i);
1431 dump_affine(bmap, bmap->div[i]+1, out);
1433 isl_int_print(out, bmap->div[i][0], 0);
1434 fprintf(out, "]\n");
1438 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1441 fprintf(out, "null basic set\n");
1445 fprintf(out, "%*s", indent, "");
1446 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1447 bset->ref, bset->dim->nparam, bset->dim->n_out,
1448 bset->extra, bset->flags);
1449 dump((struct isl_basic_map *)bset, out, indent);
1452 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1455 fprintf(out, "null basic map\n");
1459 fprintf(out, "%*s", indent, "");
1460 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1461 "flags: %x, n_name: %d\n",
1463 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1464 bmap->extra, bmap->flags, bmap->dim->n_name);
1465 dump(bmap, out, indent);
1468 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1473 total = isl_basic_map_total_dim(bmap);
1474 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1475 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1476 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1481 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1483 struct isl_set *set;
1487 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1488 isl_assert(dim->ctx, n >= 0, return NULL);
1489 set = isl_alloc(dim->ctx, struct isl_set,
1490 sizeof(struct isl_set) +
1491 (n - 1) * sizeof(struct isl_basic_set *));
1495 set->ctx = dim->ctx;
1496 isl_ctx_ref(set->ctx);
1508 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1509 unsigned nparam, unsigned dim, int n, unsigned flags)
1511 struct isl_set *set;
1512 struct isl_dim *dims;
1514 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1518 set = isl_set_alloc_dim(dims, n, flags);
1522 /* Make sure "map" has room for at least "n" more basic maps.
1524 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1527 struct isl_map *grown = NULL;
1531 isl_assert(map->ctx, n >= 0, goto error);
1532 if (map->n + n <= map->size)
1534 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1537 for (i = 0; i < map->n; ++i) {
1538 grown->p[i] = isl_basic_map_copy(map->p[i]);
1546 isl_map_free(grown);
1551 /* Make sure "set" has room for at least "n" more basic sets.
1553 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1555 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1558 struct isl_set *isl_set_dup(struct isl_set *set)
1561 struct isl_set *dup;
1566 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1569 for (i = 0; i < set->n; ++i)
1570 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1574 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1576 struct isl_set *set;
1581 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1583 isl_basic_set_free(bset);
1586 return isl_set_add_basic_set(set, bset);
1589 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1591 struct isl_map *map;
1596 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1598 isl_basic_map_free(bmap);
1601 return isl_map_add_basic_map(map, bmap);
1604 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1605 __isl_take isl_basic_set *bset)
1607 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1608 (struct isl_basic_map *)bset);
1611 void isl_set_free(struct isl_set *set)
1621 isl_ctx_deref(set->ctx);
1622 for (i = 0; i < set->n; ++i)
1623 isl_basic_set_free(set->p[i]);
1624 isl_dim_free(set->dim);
1628 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1633 fprintf(out, "null set\n");
1637 fprintf(out, "%*s", indent, "");
1638 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1639 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1641 for (i = 0; i < set->n; ++i) {
1642 fprintf(out, "%*s", indent, "");
1643 fprintf(out, "basic set %d:\n", i);
1644 isl_basic_set_dump(set->p[i], out, indent+4);
1648 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1653 fprintf(out, "null map\n");
1657 fprintf(out, "%*s", indent, "");
1658 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1659 "flags: %x, n_name: %d\n",
1660 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1661 map->dim->n_out, map->flags, map->dim->n_name);
1662 for (i = 0; i < map->n; ++i) {
1663 fprintf(out, "%*s", indent, "");
1664 fprintf(out, "basic map %d:\n", i);
1665 isl_basic_map_dump(map->p[i], out, indent+4);
1669 struct isl_basic_map *isl_basic_map_intersect_domain(
1670 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1672 struct isl_basic_map *bmap_domain;
1673 struct isl_dim *dim;
1678 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1679 bset->dim, isl_dim_param), goto error);
1681 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1682 isl_assert(bset->ctx,
1683 isl_basic_map_compatible_domain(bmap, bset), goto error);
1685 bmap = isl_basic_map_cow(bmap);
1686 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1687 bset->n_div, bset->n_eq, bset->n_ineq);
1690 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1691 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1692 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1694 bmap = isl_basic_map_simplify(bmap);
1695 return isl_basic_map_finalize(bmap);
1697 isl_basic_map_free(bmap);
1698 isl_basic_set_free(bset);
1702 struct isl_basic_map *isl_basic_map_intersect_range(
1703 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1705 struct isl_basic_map *bmap_range;
1710 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1711 bset->dim, isl_dim_param), goto error);
1713 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1714 isl_assert(bset->ctx,
1715 isl_basic_map_compatible_range(bmap, bset), goto error);
1717 bmap = isl_basic_map_cow(bmap);
1718 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1719 bset->n_div, bset->n_eq, bset->n_ineq);
1722 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1723 bmap = add_constraints(bmap, bmap_range, 0, 0);
1725 bmap = isl_basic_map_simplify(bmap);
1726 return isl_basic_map_finalize(bmap);
1728 isl_basic_map_free(bmap);
1729 isl_basic_set_free(bset);
1733 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1739 total = 1 + isl_basic_map_total_dim(bmap);
1740 if (total != vec->size)
1745 for (i = 0; i < bmap->n_eq; ++i) {
1746 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1747 if (!isl_int_is_zero(s)) {
1753 for (i = 0; i < bmap->n_ineq; ++i) {
1754 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1755 if (isl_int_is_neg(s)) {
1766 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1768 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1771 struct isl_basic_map *isl_basic_map_intersect(
1772 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1774 struct isl_vec *sample = NULL;
1776 if (!bmap1 || !bmap2)
1779 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1780 bmap2->dim, isl_dim_param), goto error);
1781 if (isl_dim_total(bmap1->dim) ==
1782 isl_dim_size(bmap1->dim, isl_dim_param) &&
1783 isl_dim_total(bmap2->dim) !=
1784 isl_dim_size(bmap2->dim, isl_dim_param))
1785 return isl_basic_map_intersect(bmap2, bmap1);
1787 if (isl_dim_total(bmap2->dim) !=
1788 isl_dim_size(bmap2->dim, isl_dim_param))
1789 isl_assert(bmap1->ctx,
1790 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1792 if (bmap1->sample &&
1793 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1794 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1795 sample = isl_vec_copy(bmap1->sample);
1796 else if (bmap2->sample &&
1797 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1798 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1799 sample = isl_vec_copy(bmap2->sample);
1801 bmap1 = isl_basic_map_cow(bmap1);
1802 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1803 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1806 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1809 isl_vec_free(bmap1->sample);
1810 bmap1->sample = sample;
1813 bmap1 = isl_basic_map_simplify(bmap1);
1814 return isl_basic_map_finalize(bmap1);
1817 isl_vec_free(sample);
1818 isl_basic_map_free(bmap1);
1819 isl_basic_map_free(bmap2);
1823 struct isl_basic_set *isl_basic_set_intersect(
1824 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1826 return (struct isl_basic_set *)
1827 isl_basic_map_intersect(
1828 (struct isl_basic_map *)bset1,
1829 (struct isl_basic_map *)bset2);
1832 /* Special case of isl_map_intersect, where both map1 and map2
1833 * are convex, without any divs and such that either map1 or map2
1834 * contains a single constraint. This constraint is then simply
1835 * added to the other map.
1837 static __isl_give isl_map *map_intersect_add_constraint(
1838 __isl_take isl_map *map1, __isl_take isl_map *map2)
1840 struct isl_basic_map *bmap1;
1841 struct isl_basic_map *bmap2;
1843 isl_assert(map1->ctx, map1->n == 1, goto error);
1844 isl_assert(map2->ctx, map1->n == 1, goto error);
1845 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
1846 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
1848 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
1849 return isl_map_intersect(map2, map1);
1851 isl_assert(map2->ctx,
1852 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
1854 map1 = isl_map_cow(map1);
1857 map1->p[0] = isl_basic_map_cow(map1->p[0]);
1858 if (map2->p[0]->n_eq == 1)
1859 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
1861 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
1862 map2->p[0]->ineq[0]);
1864 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
1865 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
1878 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1881 struct isl_map *result;
1887 if (map1->n == 1 && map2->n == 1 &&
1888 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
1889 isl_dim_equal(map1->dim, map2->dim) &&
1890 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
1891 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
1892 return map_intersect_add_constraint(map1, map2);
1893 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1894 map2->dim, isl_dim_param), goto error);
1895 if (isl_dim_total(map1->dim) ==
1896 isl_dim_size(map1->dim, isl_dim_param) &&
1897 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1898 return isl_map_intersect(map2, map1);
1900 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1901 isl_assert(map1->ctx,
1902 isl_dim_equal(map1->dim, map2->dim), goto error);
1904 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1905 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1906 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1908 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1909 map1->n * map2->n, flags);
1912 for (i = 0; i < map1->n; ++i)
1913 for (j = 0; j < map2->n; ++j) {
1914 struct isl_basic_map *part;
1915 part = isl_basic_map_intersect(
1916 isl_basic_map_copy(map1->p[i]),
1917 isl_basic_map_copy(map2->p[j]));
1918 if (isl_basic_map_is_empty(part))
1919 isl_basic_map_free(part);
1921 result = isl_map_add_basic_map(result, part);
1934 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1936 return (struct isl_set *)
1937 isl_map_intersect((struct isl_map *)set1,
1938 (struct isl_map *)set2);
1941 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1943 struct isl_dim *dim;
1944 struct isl_basic_set *bset;
1949 bmap = isl_basic_map_cow(bmap);
1952 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1953 in = isl_basic_map_n_in(bmap);
1954 bset = isl_basic_set_from_basic_map(bmap);
1955 bset = isl_basic_set_swap_vars(bset, in);
1956 return isl_basic_map_from_basic_set(bset, dim);
1959 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
1960 enum isl_dim_type type, unsigned n)
1962 struct isl_dim *res_dim;
1963 struct isl_basic_map *res;
1964 struct isl_dim_map *dim_map;
1965 unsigned total, pos;
1973 res_dim = isl_dim_add(isl_basic_map_get_dim(bmap), type, n);
1975 total = isl_basic_map_total_dim(bmap) + n;
1976 dim_map = isl_dim_map_alloc(bmap->ctx, total);
1978 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos);
1979 pos += isl_dim_size(res_dim, isl_dim_param);
1980 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos);
1981 pos += isl_dim_size(res_dim, isl_dim_in);
1982 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos);
1983 pos += isl_dim_size(res_dim, isl_dim_out);
1984 isl_dim_map_div(dim_map, bmap, pos);
1986 res = isl_basic_map_alloc_dim(res_dim,
1987 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1988 res = add_constraints_dim_map(res, bmap, dim_map);
1989 res = isl_basic_map_simplify(res);
1990 return isl_basic_map_finalize(res);
1993 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
1994 enum isl_dim_type type, unsigned n)
1998 isl_assert(bset->ctx, type != isl_dim_in, goto error);
1999 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2001 isl_basic_set_free(bset);
2005 __isl_give isl_map *isl_map_add(__isl_take isl_map *map,
2006 enum isl_dim_type type, unsigned n)
2013 map = isl_map_cow(map);
2017 map->dim = isl_dim_add(map->dim, type, n);
2021 for (i = 0; i < map->n; ++i) {
2022 map->p[i] = isl_basic_map_add(map->p[i], type, n);
2033 __isl_give isl_set *isl_set_add(__isl_take isl_set *set,
2034 enum isl_dim_type type, unsigned n)
2038 isl_assert(set->ctx, type != isl_dim_in, goto error);
2039 return (isl_set *)isl_map_add((isl_map *)set, type, n);
2045 /* Turn final n dimensions into existentially quantified variables.
2047 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2048 enum isl_dim_type type, unsigned first, unsigned n)
2058 isl_assert(bset->ctx, type == isl_dim_set, goto error);
2059 isl_assert(bset->ctx, first + n == isl_basic_set_n_dim(bset), goto error);
2064 if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL))
2065 return isl_basic_set_remove(bset, type, first, n);
2067 bset = isl_basic_set_cow(bset);
2069 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
2070 old = bset->block2.data;
2071 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
2072 (bset->extra + n) * (1 + row_size));
2073 if (!bset->block2.data)
2075 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
2078 for (i = 0; i < n; ++i) {
2079 new_div[i] = bset->block2.data +
2080 (bset->extra + i) * (1 + row_size);
2081 isl_seq_clr(new_div[i], 1 + row_size);
2083 for (i = 0; i < bset->extra; ++i)
2084 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
2086 bset->div = new_div;
2089 bset->dim = isl_dim_drop_outputs(bset->dim,
2090 isl_basic_set_n_dim(bset) - n, n);
2093 bset = isl_basic_set_simplify(bset);
2094 bset = isl_basic_set_drop_redundant_divs(bset);
2095 return isl_basic_set_finalize(bset);
2097 isl_basic_set_free(bset);
2101 /* Turn final n dimensions into existentially quantified variables.
2103 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2104 enum isl_dim_type type, unsigned first, unsigned n)
2111 isl_assert(set->ctx, type == isl_dim_set, goto error);
2112 isl_assert(set->ctx, first + n == isl_set_n_dim(set), goto error);
2114 set = isl_set_cow(set);
2117 set->dim = isl_dim_drop_outputs(set->dim, first, n);
2118 for (i = 0; i < set->n; ++i) {
2119 set->p[i] = isl_basic_set_project_out(set->p[i], type, first, n);
2124 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2131 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2135 for (i = 0; i < n; ++i) {
2136 j = isl_basic_map_alloc_div(bmap);
2139 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2143 isl_basic_map_free(bmap);
2147 struct isl_basic_map *isl_basic_map_apply_range(
2148 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2150 struct isl_dim *dim_result = NULL;
2151 struct isl_basic_map *bmap;
2152 unsigned n_in, n_out, n, nparam, total, pos;
2153 struct isl_dim_map *dim_map1, *dim_map2;
2155 if (!bmap1 || !bmap2)
2158 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2159 isl_dim_copy(bmap2->dim));
2161 n_in = isl_basic_map_n_in(bmap1);
2162 n_out = isl_basic_map_n_out(bmap2);
2163 n = isl_basic_map_n_out(bmap1);
2164 nparam = isl_basic_map_n_param(bmap1);
2166 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2167 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2168 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2169 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2170 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2171 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2172 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2173 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2174 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2175 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2176 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2178 bmap = isl_basic_map_alloc_dim(dim_result,
2179 bmap1->n_div + bmap2->n_div + n,
2180 bmap1->n_eq + bmap2->n_eq,
2181 bmap1->n_ineq + bmap2->n_ineq);
2182 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2183 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2184 bmap = add_divs(bmap, n);
2185 bmap = isl_basic_map_simplify(bmap);
2186 bmap = isl_basic_map_drop_redundant_divs(bmap);
2187 return isl_basic_map_finalize(bmap);
2189 isl_basic_map_free(bmap1);
2190 isl_basic_map_free(bmap2);
2194 struct isl_basic_set *isl_basic_set_apply(
2195 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2200 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2203 return (struct isl_basic_set *)
2204 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2206 isl_basic_set_free(bset);
2207 isl_basic_map_free(bmap);
2211 struct isl_basic_map *isl_basic_map_apply_domain(
2212 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2214 if (!bmap1 || !bmap2)
2217 isl_assert(bmap1->ctx,
2218 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2219 isl_assert(bmap1->ctx,
2220 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2223 bmap1 = isl_basic_map_reverse(bmap1);
2224 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2225 return isl_basic_map_reverse(bmap1);
2227 isl_basic_map_free(bmap1);
2228 isl_basic_map_free(bmap2);
2232 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2233 * A \cap B -> f(A) + f(B)
2235 struct isl_basic_map *isl_basic_map_sum(
2236 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2238 unsigned n_in, n_out, nparam, total, pos;
2239 struct isl_basic_map *bmap = NULL;
2240 struct isl_dim_map *dim_map1, *dim_map2;
2243 if (!bmap1 || !bmap2)
2246 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2249 nparam = isl_basic_map_n_param(bmap1);
2250 n_in = isl_basic_map_n_in(bmap1);
2251 n_out = isl_basic_map_n_out(bmap1);
2253 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2254 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2255 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2256 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2257 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2258 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2259 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2260 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2261 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2262 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2263 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2265 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2266 bmap1->n_div + bmap2->n_div + 2 * n_out,
2267 bmap1->n_eq + bmap2->n_eq + n_out,
2268 bmap1->n_ineq + bmap2->n_ineq);
2269 for (i = 0; i < n_out; ++i) {
2270 int j = isl_basic_map_alloc_equality(bmap);
2273 isl_seq_clr(bmap->eq[j], 1+total);
2274 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2275 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2276 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2278 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2279 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2280 bmap = add_divs(bmap, 2 * n_out);
2282 bmap = isl_basic_map_simplify(bmap);
2283 return isl_basic_map_finalize(bmap);
2285 isl_basic_map_free(bmap);
2286 isl_basic_map_free(bmap1);
2287 isl_basic_map_free(bmap2);
2291 /* Given two maps A -> f(A) and B -> g(B), construct a map
2292 * A \cap B -> f(A) + f(B)
2294 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2296 struct isl_map *result;
2302 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2304 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2305 map1->n * map2->n, 0);
2308 for (i = 0; i < map1->n; ++i)
2309 for (j = 0; j < map2->n; ++j) {
2310 struct isl_basic_map *part;
2311 part = isl_basic_map_sum(
2312 isl_basic_map_copy(map1->p[i]),
2313 isl_basic_map_copy(map2->p[j]));
2314 if (isl_basic_map_is_empty(part))
2315 isl_basic_map_free(part);
2317 result = isl_map_add_basic_map(result, part);
2330 /* Given a basic map A -> f(A), construct A -> -f(A).
2332 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2337 bmap = isl_basic_map_cow(bmap);
2341 n = isl_basic_map_dim(bmap, isl_dim_out);
2342 off = isl_basic_map_offset(bmap, isl_dim_out);
2343 for (i = 0; i < bmap->n_eq; ++i)
2344 for (j = 0; j < n; ++j)
2345 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2346 for (i = 0; i < bmap->n_ineq; ++i)
2347 for (j = 0; j < n; ++j)
2348 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2349 for (i = 0; i < bmap->n_div; ++i)
2350 for (j = 0; j < n; ++j)
2351 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2352 return isl_basic_map_finalize(bmap);
2355 /* Given a map A -> f(A), construct A -> -f(A).
2357 struct isl_map *isl_map_neg(struct isl_map *map)
2361 map = isl_map_cow(map);
2365 for (i = 0; i < map->n; ++i) {
2366 map->p[i] = isl_basic_map_neg(map->p[i]);
2377 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2378 * A -> floor(f(A)/d).
2380 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2383 unsigned n_in, n_out, nparam, total, pos;
2384 struct isl_basic_map *result = NULL;
2385 struct isl_dim_map *dim_map;
2391 nparam = isl_basic_map_n_param(bmap);
2392 n_in = isl_basic_map_n_in(bmap);
2393 n_out = isl_basic_map_n_out(bmap);
2395 total = nparam + n_in + n_out + bmap->n_div + n_out;
2396 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2397 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2398 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2399 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2400 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2402 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2403 bmap->n_div + n_out,
2404 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2405 result = add_constraints_dim_map(result, bmap, dim_map);
2406 result = add_divs(result, n_out);
2407 for (i = 0; i < n_out; ++i) {
2409 j = isl_basic_map_alloc_inequality(result);
2412 isl_seq_clr(result->ineq[j], 1+total);
2413 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2414 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2415 j = isl_basic_map_alloc_inequality(result);
2418 isl_seq_clr(result->ineq[j], 1+total);
2419 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2420 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2421 isl_int_sub_ui(result->ineq[j][0], d, 1);
2424 result = isl_basic_map_simplify(result);
2425 return isl_basic_map_finalize(result);
2427 isl_basic_map_free(result);
2431 /* Given a map A -> f(A) and an integer d, construct a map
2432 * A -> floor(f(A)/d).
2434 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2438 map = isl_map_cow(map);
2442 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2443 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2444 for (i = 0; i < map->n; ++i) {
2445 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2456 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2462 i = isl_basic_map_alloc_equality(bmap);
2465 nparam = isl_basic_map_n_param(bmap);
2466 n_in = isl_basic_map_n_in(bmap);
2467 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2468 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2469 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2470 return isl_basic_map_finalize(bmap);
2472 isl_basic_map_free(bmap);
2476 /* Add a constraints to "bmap" expressing i_pos < o_pos
2478 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2484 i = isl_basic_map_alloc_inequality(bmap);
2487 nparam = isl_basic_map_n_param(bmap);
2488 n_in = isl_basic_map_n_in(bmap);
2489 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2490 isl_int_set_si(bmap->ineq[i][0], -1);
2491 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2492 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2493 return isl_basic_map_finalize(bmap);
2495 isl_basic_map_free(bmap);
2499 /* Add a constraints to "bmap" expressing i_pos > o_pos
2501 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2507 i = isl_basic_map_alloc_inequality(bmap);
2510 nparam = isl_basic_map_n_param(bmap);
2511 n_in = isl_basic_map_n_in(bmap);
2512 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2513 isl_int_set_si(bmap->ineq[i][0], -1);
2514 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2515 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2516 return isl_basic_map_finalize(bmap);
2518 isl_basic_map_free(bmap);
2522 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2525 struct isl_basic_map *bmap;
2526 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2529 for (i = 0; i < n_equal && bmap; ++i)
2530 bmap = var_equal(bmap, i);
2531 return isl_basic_map_finalize(bmap);
2534 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2536 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2539 struct isl_basic_map *bmap;
2540 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2543 for (i = 0; i < pos && bmap; ++i)
2544 bmap = var_equal(bmap, i);
2546 bmap = var_less(bmap, pos);
2547 return isl_basic_map_finalize(bmap);
2550 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2552 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2555 struct isl_basic_map *bmap;
2556 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2559 for (i = 0; i < pos && bmap; ++i)
2560 bmap = var_equal(bmap, i);
2562 bmap = var_more(bmap, pos);
2563 return isl_basic_map_finalize(bmap);
2566 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
2568 struct isl_map *map;
2575 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2577 for (i = 0; i < dim; ++i)
2578 map = isl_map_add_basic_map(map,
2579 isl_basic_map_less_at(isl_dim_copy(dims), i));
2581 map = isl_map_add_basic_map(map,
2582 isl_basic_map_equal(isl_dim_copy(dims), dim));
2588 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
2590 return map_lex_lte(isl_dim_map(set_dim), 0);
2593 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
2595 return map_lex_lte(isl_dim_map(set_dim), 1);
2598 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
2600 struct isl_map *map;
2607 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2609 for (i = 0; i < dim; ++i)
2610 map = isl_map_add_basic_map(map,
2611 isl_basic_map_more_at(isl_dim_copy(dims), i));
2613 map = isl_map_add_basic_map(map,
2614 isl_basic_map_equal(isl_dim_copy(dims), dim));
2620 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
2622 return map_lex_gte(isl_dim_map(set_dim), 0);
2625 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
2627 return map_lex_gte(isl_dim_map(set_dim), 1);
2630 struct isl_basic_map *isl_basic_map_from_basic_set(
2631 struct isl_basic_set *bset, struct isl_dim *dim)
2633 struct isl_basic_map *bmap;
2635 bset = isl_basic_set_cow(bset);
2639 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2640 isl_dim_free(bset->dim);
2641 bmap = (struct isl_basic_map *) bset;
2643 return isl_basic_map_finalize(bmap);
2645 isl_basic_set_free(bset);
2650 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2654 if (bmap->dim->n_in == 0)
2655 return (struct isl_basic_set *)bmap;
2656 bmap = isl_basic_map_cow(bmap);
2659 bmap->dim = isl_dim_cow(bmap->dim);
2662 bmap->dim->n_out += bmap->dim->n_in;
2663 bmap->dim->n_in = 0;
2664 bmap = isl_basic_map_finalize(bmap);
2665 return (struct isl_basic_set *)bmap;
2667 isl_basic_map_free(bmap);
2671 /* For a div d = floor(f/m), add the constraints
2674 * -(f-(n-1)) + m d >= 0
2676 * Note that the second constraint is the negation of
2680 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2683 unsigned total = isl_basic_map_total_dim(bmap);
2684 unsigned div_pos = 1 + total - bmap->n_div + div;
2686 i = isl_basic_map_alloc_inequality(bmap);
2689 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2690 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2692 j = isl_basic_map_alloc_inequality(bmap);
2695 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2696 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2697 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2701 struct isl_basic_set *isl_basic_map_underlying_set(
2702 struct isl_basic_map *bmap)
2706 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2707 return (struct isl_basic_set *)bmap;
2708 bmap = isl_basic_map_cow(bmap);
2711 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2714 bmap->extra -= bmap->n_div;
2716 bmap = isl_basic_map_finalize(bmap);
2717 return (struct isl_basic_set *)bmap;
2722 __isl_give isl_basic_set *isl_basic_set_underlying_set(
2723 __isl_take isl_basic_set *bset)
2725 return isl_basic_map_underlying_set((isl_basic_map *)bset);
2728 struct isl_basic_map *isl_basic_map_overlying_set(
2729 struct isl_basic_set *bset, struct isl_basic_map *like)
2731 struct isl_basic_map *bmap;
2732 struct isl_ctx *ctx;
2739 isl_assert(ctx, bset->n_div == 0, goto error);
2740 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2741 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2743 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2744 isl_basic_map_free(like);
2745 return (struct isl_basic_map *)bset;
2747 bset = isl_basic_set_cow(bset);
2750 total = bset->dim->n_out + bset->extra;
2751 bmap = (struct isl_basic_map *)bset;
2752 isl_dim_free(bmap->dim);
2753 bmap->dim = isl_dim_copy(like->dim);
2756 bmap->n_div = like->n_div;
2757 bmap->extra += like->n_div;
2760 ltotal = total - bmap->extra + like->extra;
2763 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2764 bmap->extra * (1 + 1 + total));
2765 if (isl_blk_is_error(bmap->block2))
2767 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2771 for (i = 0; i < bmap->extra; ++i)
2772 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2773 for (i = 0; i < like->n_div; ++i) {
2774 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2775 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2777 bmap = isl_basic_map_extend_constraints(bmap,
2778 0, 2 * like->n_div);
2779 for (i = 0; i < like->n_div; ++i) {
2780 if (isl_int_is_zero(bmap->div[i][0]))
2782 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
2786 isl_basic_map_free(like);
2787 bmap = isl_basic_map_simplify(bmap);
2788 bmap = isl_basic_map_finalize(bmap);
2791 isl_basic_map_free(like);
2792 isl_basic_set_free(bset);
2796 struct isl_basic_set *isl_basic_set_from_underlying_set(
2797 struct isl_basic_set *bset, struct isl_basic_set *like)
2799 return (struct isl_basic_set *)
2800 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2803 struct isl_set *isl_set_from_underlying_set(
2804 struct isl_set *set, struct isl_basic_set *like)
2810 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2812 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2813 isl_basic_set_free(like);
2816 set = isl_set_cow(set);
2819 for (i = 0; i < set->n; ++i) {
2820 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2821 isl_basic_set_copy(like));
2825 isl_dim_free(set->dim);
2826 set->dim = isl_dim_copy(like->dim);
2829 isl_basic_set_free(like);
2832 isl_basic_set_free(like);
2837 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2841 map = isl_map_cow(map);
2844 map->dim = isl_dim_cow(map->dim);
2848 for (i = 1; i < map->n; ++i)
2849 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2851 for (i = 0; i < map->n; ++i) {
2852 map->p[i] = (struct isl_basic_map *)
2853 isl_basic_map_underlying_set(map->p[i]);
2858 map->dim = isl_dim_underlying(map->dim, 0);
2860 isl_dim_free(map->dim);
2861 map->dim = isl_dim_copy(map->p[0]->dim);
2865 return (struct isl_set *)map;
2871 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2873 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2876 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2878 struct isl_basic_set *domain;
2883 n_in = isl_basic_map_n_in(bmap);
2884 n_out = isl_basic_map_n_out(bmap);
2885 domain = isl_basic_set_from_basic_map(bmap);
2886 return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
2889 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2891 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2894 struct isl_set *isl_map_range(struct isl_map *map)
2897 struct isl_set *set;
2901 map = isl_map_cow(map);
2905 set = (struct isl_set *) map;
2906 if (set->dim->n_in != 0) {
2907 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2911 for (i = 0; i < map->n; ++i) {
2912 set->p[i] = isl_basic_map_range(map->p[i]);
2916 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2917 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2924 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2927 struct isl_map *map = NULL;
2929 set = isl_set_cow(set);
2932 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2933 map = (struct isl_map *)set;
2934 for (i = 0; i < set->n; ++i) {
2935 map->p[i] = isl_basic_map_from_basic_set(
2936 set->p[i], isl_dim_copy(dim));
2940 isl_dim_free(map->dim);
2949 struct isl_map *isl_map_from_range(struct isl_set *set)
2951 return (struct isl_map *)set;
2954 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
2956 return isl_map_reverse(isl_map_from_range(set));;
2959 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
2960 __isl_take isl_set *range)
2962 return isl_map_product(isl_map_from_domain(domain),
2963 isl_map_from_range(range));
2966 struct isl_set *isl_set_from_map(struct isl_map *map)
2969 struct isl_set *set = NULL;
2973 map = isl_map_cow(map);
2976 map->dim = isl_dim_cow(map->dim);
2979 map->dim->n_out += map->dim->n_in;
2981 set = (struct isl_set *)map;
2982 for (i = 0; i < map->n; ++i) {
2983 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2993 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2995 struct isl_map *map;
2999 isl_assert(dim->ctx, n >= 0, return NULL);
3000 map = isl_alloc(dim->ctx, struct isl_map,
3001 sizeof(struct isl_map) +
3002 (n - 1) * sizeof(struct isl_basic_map *));
3006 map->ctx = dim->ctx;
3007 isl_ctx_ref(map->ctx);
3019 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3020 unsigned nparam, unsigned in, unsigned out, int n,
3023 struct isl_map *map;
3024 struct isl_dim *dims;
3026 dims = isl_dim_alloc(ctx, nparam, in, out);
3030 map = isl_map_alloc_dim(dims, n, flags);
3034 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3036 struct isl_basic_map *bmap;
3037 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3038 bmap = isl_basic_map_set_to_empty(bmap);
3042 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3044 struct isl_basic_set *bset;
3045 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3046 bset = isl_basic_set_set_to_empty(bset);
3050 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3052 struct isl_basic_map *bmap;
3055 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3056 bmap = isl_basic_map_set_to_empty(bmap);
3060 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3062 struct isl_basic_map *bmap;
3065 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3066 bmap = isl_basic_map_set_to_empty(bmap);
3070 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3072 struct isl_basic_set *bset;
3075 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3076 bset = isl_basic_set_set_to_empty(bset);
3080 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3082 struct isl_basic_map *bmap;
3083 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
3087 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
3089 struct isl_basic_set *bset;
3090 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
3094 __isl_give isl_basic_map *isl_basic_map_universe_like(
3095 __isl_keep isl_basic_map *model)
3099 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3102 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3106 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3109 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3110 __isl_keep isl_set *model)
3114 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3117 struct isl_map *isl_map_empty(struct isl_dim *dim)
3119 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3122 struct isl_map *isl_map_empty_like(struct isl_map *model)
3126 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3129 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
3133 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3136 struct isl_set *isl_set_empty(struct isl_dim *dim)
3138 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3141 struct isl_set *isl_set_empty_like(struct isl_set *model)
3145 return isl_set_empty(isl_dim_copy(model->dim));
3148 struct isl_map *isl_map_universe(struct isl_dim *dim)
3150 struct isl_map *map;
3153 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3154 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
3158 struct isl_set *isl_set_universe(struct isl_dim *dim)
3160 struct isl_set *set;
3163 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3164 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
3168 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3172 return isl_set_universe(isl_dim_copy(model->dim));
3175 struct isl_map *isl_map_dup(struct isl_map *map)
3178 struct isl_map *dup;
3182 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3183 for (i = 0; i < map->n; ++i)
3184 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
3188 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
3189 __isl_take isl_basic_map *bmap)
3193 if (isl_basic_map_fast_is_empty(bmap)) {
3194 isl_basic_map_free(bmap);
3197 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3198 isl_assert(map->ctx, map->n < map->size, goto error);
3199 map->p[map->n] = bmap;
3201 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3207 isl_basic_map_free(bmap);
3211 void isl_map_free(struct isl_map *map)
3221 isl_ctx_deref(map->ctx);
3222 for (i = 0; i < map->n; ++i)
3223 isl_basic_map_free(map->p[i]);
3224 isl_dim_free(map->dim);
3228 struct isl_map *isl_map_extend(struct isl_map *base,
3229 unsigned nparam, unsigned n_in, unsigned n_out)
3233 base = isl_map_cow(base);
3237 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3240 for (i = 0; i < base->n; ++i) {
3241 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3242 isl_dim_copy(base->dim), 0, 0, 0);
3252 struct isl_set *isl_set_extend(struct isl_set *base,
3253 unsigned nparam, unsigned dim)
3255 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3259 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3260 struct isl_basic_map *bmap, unsigned pos, int value)
3264 bmap = isl_basic_map_cow(bmap);
3265 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3266 j = isl_basic_map_alloc_equality(bmap);
3269 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3270 isl_int_set_si(bmap->eq[j][pos], -1);
3271 isl_int_set_si(bmap->eq[j][0], value);
3272 bmap = isl_basic_map_simplify(bmap);
3273 return isl_basic_map_finalize(bmap);
3275 isl_basic_map_free(bmap);
3279 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3280 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3284 bmap = isl_basic_map_cow(bmap);
3285 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3286 j = isl_basic_map_alloc_equality(bmap);
3289 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3290 isl_int_set_si(bmap->eq[j][pos], -1);
3291 isl_int_set(bmap->eq[j][0], value);
3292 bmap = isl_basic_map_simplify(bmap);
3293 return isl_basic_map_finalize(bmap);
3295 isl_basic_map_free(bmap);
3299 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3300 enum isl_dim_type type, unsigned pos, int value)
3304 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3305 return isl_basic_map_fix_pos_si(bmap,
3306 isl_basic_map_offset(bmap, type) + pos, value);
3308 isl_basic_map_free(bmap);
3312 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3313 enum isl_dim_type type, unsigned pos, isl_int value)
3317 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3318 return isl_basic_map_fix_pos(bmap,
3319 isl_basic_map_offset(bmap, type) + pos, value);
3321 isl_basic_map_free(bmap);
3325 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3326 enum isl_dim_type type, unsigned pos, int value)
3328 return (struct isl_basic_set *)
3329 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3333 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3334 enum isl_dim_type type, unsigned pos, isl_int value)
3336 return (struct isl_basic_set *)
3337 isl_basic_map_fix((struct isl_basic_map *)bset,
3341 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3342 unsigned input, int value)
3344 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3347 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3348 unsigned dim, int value)
3350 return (struct isl_basic_set *)
3351 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3352 isl_dim_set, dim, value);
3355 struct isl_map *isl_map_fix_si(struct isl_map *map,
3356 enum isl_dim_type type, unsigned pos, int value)
3360 map = isl_map_cow(map);
3364 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3365 for (i = 0; i < map->n; ++i) {
3366 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3370 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3377 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
3378 enum isl_dim_type type, unsigned pos, isl_int value)
3382 map = isl_map_cow(map);
3386 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3387 for (i = 0; i < map->n; ++i) {
3388 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
3392 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3399 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
3400 enum isl_dim_type type, unsigned pos, isl_int value)
3402 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
3405 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3406 unsigned input, int value)
3408 return isl_map_fix_si(map, isl_dim_in, input, value);
3411 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3413 return (struct isl_set *)
3414 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3417 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3418 unsigned dim, isl_int value)
3422 bset = isl_basic_set_cow(bset);
3423 bset = isl_basic_set_extend_constraints(bset, 0, 1);
3424 j = isl_basic_set_alloc_inequality(bset);
3427 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3428 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3429 isl_int_neg(bset->ineq[j][0], value);
3430 bset = isl_basic_set_simplify(bset);
3431 return isl_basic_set_finalize(bset);
3433 isl_basic_set_free(bset);
3437 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3442 set = isl_set_cow(set);
3446 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3447 for (i = 0; i < set->n; ++i) {
3448 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3458 struct isl_map *isl_map_reverse(struct isl_map *map)
3462 map = isl_map_cow(map);
3466 map->dim = isl_dim_reverse(map->dim);
3469 for (i = 0; i < map->n; ++i) {
3470 map->p[i] = isl_basic_map_reverse(map->p[i]);
3474 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3481 static struct isl_map *isl_basic_map_partial_lexopt(
3482 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3483 struct isl_set **empty, int max)
3487 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
3488 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3490 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3492 isl_basic_map_free(bmap);
3493 isl_basic_set_free(dom);
3499 struct isl_map *isl_basic_map_partial_lexmax(
3500 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3501 struct isl_set **empty)
3503 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
3506 struct isl_map *isl_basic_map_partial_lexmin(
3507 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3508 struct isl_set **empty)
3510 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3513 struct isl_set *isl_basic_set_partial_lexmin(
3514 struct isl_basic_set *bset, struct isl_basic_set *dom,
3515 struct isl_set **empty)
3517 return (struct isl_set *)
3518 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3522 struct isl_set *isl_basic_set_partial_lexmax(
3523 struct isl_basic_set *bset, struct isl_basic_set *dom,
3524 struct isl_set **empty)
3526 return (struct isl_set *)
3527 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3531 /* Given a basic map "bmap", compute the lexicograhically minimal
3532 * (or maximal) image element for each domain element in dom.
3533 * Set *empty to those elements in dom that do not have an image element.
3535 * We first make sure the basic sets in dom are disjoint and then
3536 * simply collect the results over each of the basic sets separately.
3537 * We could probably improve the efficiency a bit by moving the union
3538 * domain down into the parametric integer programming.
3540 static __isl_give isl_map *basic_map_partial_lexopt(
3541 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
3542 __isl_give isl_set **empty, int max)
3545 struct isl_map *res;
3547 dom = isl_set_make_disjoint(dom);
3551 if (isl_set_fast_is_empty(dom)) {
3552 res = isl_map_empty_like_basic_map(bmap);
3553 *empty = isl_set_empty_like(dom);
3555 isl_basic_map_free(bmap);
3559 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
3560 isl_basic_set_copy(dom->p[0]), empty, max);
3562 for (i = 1; i < dom->n; ++i) {
3563 struct isl_map *res_i;
3564 struct isl_set *empty_i;
3566 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
3567 isl_basic_set_copy(dom->p[i]), &empty_i, max);
3569 res = isl_map_union_disjoint(res, res_i);
3570 *empty = isl_set_union_disjoint(*empty, empty_i);
3574 isl_basic_map_free(bmap);
3579 isl_basic_map_free(bmap);
3583 /* Given a map "map", compute the lexicograhically minimal
3584 * (or maximal) image element for each domain element in dom.
3585 * Set *empty to those elements in dom that do not have an image element.
3587 * We first compute the lexicographically minimal or maximal element
3588 * in the first basic map. This results in a partial solution "res"
3589 * and a subset "todo" of dom that still need to be handled.
3590 * We then consider each of the remaining maps in "map" and successively
3591 * improve both "res" and "todo".
3593 * Let res^k and todo^k be the results after k steps and let i = k + 1.
3594 * Assume we are computing the lexicographical maximum.
3595 * We first intersect basic map i with a relation that maps elements
3596 * to elements that are lexicographically larger than the image elements
3597 * in res^k and the compute the maximum image element of this intersection.
3598 * The result ("better") corresponds to those image elements in basic map i
3599 * that are better than what we had before. The remainder ("keep") are the
3600 * domain elements for which the image element in res_k was better.
3601 * We also compute the lexicographical maximum of basic map i in todo^k.
3602 * res^i is the result of the operation + better + those elements in
3603 * res^k that we should keep
3604 * todo^i is the remainder of the maximum operation on todo^k.
3606 static __isl_give isl_map *isl_map_partial_lexopt(
3607 __isl_take isl_map *map, __isl_take isl_set *dom,
3608 __isl_give isl_set **empty, int max)
3611 struct isl_map *res;
3612 struct isl_set *todo;
3617 if (isl_map_fast_is_empty(map)) {
3625 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
3626 isl_set_copy(dom), &todo, max);
3628 for (i = 1; i < map->n; ++i) {
3630 struct isl_map *better;
3631 struct isl_set *keep;
3632 struct isl_map *res_i;
3633 struct isl_set *todo_i;
3634 struct isl_dim *dim = isl_map_get_dim(res);
3636 dim = isl_dim_range(dim);
3638 lt = isl_map_lex_lt(dim);
3640 lt = isl_map_lex_gt(dim);
3641 lt = isl_map_apply_range(isl_map_copy(res), lt);
3642 lt = isl_map_intersect(lt,
3643 isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
3644 better = isl_map_partial_lexopt(lt,
3645 isl_map_domain(isl_map_copy(res)),
3648 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
3649 todo, &todo_i, max);
3651 res = isl_map_intersect_domain(res, keep);
3652 res = isl_map_union_disjoint(res, res_i);
3653 res = isl_map_union_disjoint(res, better);
3674 __isl_give isl_map *isl_map_partial_lexmax(
3675 __isl_take isl_map *map, __isl_take isl_set *dom,
3676 __isl_give isl_set **empty)
3678 return isl_map_partial_lexopt(map, dom, empty, 1);
3681 __isl_give isl_map *isl_map_partial_lexmin(
3682 __isl_take isl_map *map, __isl_take isl_set *dom,
3683 __isl_give isl_set **empty)
3685 return isl_map_partial_lexopt(map, dom, empty, 0);
3688 __isl_give isl_set *isl_set_partial_lexmin(
3689 __isl_take isl_set *set, __isl_take isl_set *dom,
3690 __isl_give isl_set **empty)
3692 return (struct isl_set *)
3693 isl_map_partial_lexmin((struct isl_map *)set,
3697 __isl_give isl_set *isl_set_partial_lexmax(
3698 __isl_take isl_set *set, __isl_take isl_set *dom,
3699 __isl_give isl_set **empty)
3701 return (struct isl_set *)
3702 isl_map_partial_lexmax((struct isl_map *)set,
3706 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
3708 struct isl_basic_set *dom = NULL;
3709 struct isl_dim *dom_dim;
3713 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3714 dom = isl_basic_set_universe(dom_dim);
3715 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
3717 isl_basic_map_free(bmap);
3721 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
3723 return isl_basic_map_lexopt(bmap, 0);
3726 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
3728 return isl_basic_map_lexopt(bmap, 1);
3731 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
3733 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
3736 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
3738 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
3741 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
3743 struct isl_set *dom = NULL;
3744 struct isl_dim *dom_dim;
3748 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
3749 dom = isl_set_universe(dom_dim);
3750 return isl_map_partial_lexopt(map, dom, NULL, max);
3756 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
3758 return isl_map_lexopt(map, 0);
3761 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
3763 return isl_map_lexopt(map, 1);
3766 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
3768 return (isl_set *)isl_map_lexmin((isl_map *)set);
3771 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
3773 return (isl_set *)isl_map_lexmax((isl_map *)set);
3776 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
3777 struct isl_dim *dim)
3784 for (i = 0; i < map->n; ++i) {
3785 isl_dim_free(map->p[i]->dim);
3786 map->p[i]->dim = isl_dim_copy(dim);
3788 isl_dim_free(map->dim);
3798 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
3799 struct isl_dim *dim)
3801 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3804 /* Apply a preimage specified by "mat" on the parameters of "bset".
3805 * bset is assumed to have only parameters and divs.
3807 static struct isl_basic_set *basic_set_parameter_preimage(
3808 struct isl_basic_set *bset, struct isl_mat *mat)
3815 bset->dim = isl_dim_cow(bset->dim);
3819 nparam = isl_basic_set_dim(bset, isl_dim_param);
3821 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
3823 bset->dim->nparam = 0;
3824 bset->dim->n_out = nparam;
3825 bset = isl_basic_set_preimage(bset, mat);
3827 bset->dim->nparam = bset->dim->n_out;
3828 bset->dim->n_out = 0;
3833 isl_basic_set_free(bset);
3837 /* Apply a preimage specified by "mat" on the parameters of "set".
3838 * set is assumed to have only parameters and divs.
3840 static struct isl_set *set_parameter_preimage(
3841 struct isl_set *set, struct isl_mat *mat)
3843 struct isl_dim *dim = NULL;
3849 dim = isl_dim_copy(set->dim);
3850 dim = isl_dim_cow(dim);
3854 nparam = isl_set_dim(set, isl_dim_param);
3856 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
3859 dim->n_out = nparam;
3860 isl_set_reset_dim(set, dim);
3861 set = isl_set_preimage(set, mat);
3864 dim = isl_dim_copy(set->dim);
3865 dim = isl_dim_cow(dim);
3868 dim->nparam = dim->n_out;
3870 isl_set_reset_dim(set, dim);
3880 /* Intersect the basic set "bset" with the affine space specified by the
3881 * equalities in "eq".
3883 static struct isl_basic_set *basic_set_append_equalities(
3884 struct isl_basic_set *bset, struct isl_mat *eq)
3892 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
3897 len = 1 + isl_dim_total(bset->dim) + bset->extra;
3898 for (i = 0; i < eq->n_row; ++i) {
3899 k = isl_basic_set_alloc_equality(bset);
3902 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
3903 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
3910 isl_basic_set_free(bset);
3914 /* Intersect the set "set" with the affine space specified by the
3915 * equalities in "eq".
3917 static struct isl_set *set_append_equalities(struct isl_set *set,
3925 for (i = 0; i < set->n; ++i) {
3926 set->p[i] = basic_set_append_equalities(set->p[i],
3939 /* Project the given basic set onto its parameter domain, possibly introducing
3940 * new, explicit, existential variables in the constraints.
3941 * The input has parameters and (possibly implicit) existential variables.
3942 * The output has the same parameters, but only
3943 * explicit existentially quantified variables.
3945 * The actual projection is performed by pip, but pip doesn't seem
3946 * to like equalities very much, so we first remove the equalities
3947 * among the parameters by performing a variable compression on
3948 * the parameters. Afterward, an inverse transformation is performed
3949 * and the equalities among the parameters are inserted back in.
3951 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
3955 struct isl_mat *T, *T2;
3956 struct isl_set *set;
3957 unsigned nparam, n_div;
3959 bset = isl_basic_set_cow(bset);
3963 if (bset->n_eq == 0)
3964 return isl_basic_set_lexmin(bset);
3966 isl_basic_set_gauss(bset, NULL);
3968 nparam = isl_basic_set_dim(bset, isl_dim_param);
3969 n_div = isl_basic_set_dim(bset, isl_dim_div);
3971 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
3972 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
3975 if (i == bset->n_eq)
3976 return isl_basic_set_lexmin(bset);
3978 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
3980 eq = isl_mat_cow(eq);
3981 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
3982 bset = basic_set_parameter_preimage(bset, T);
3984 set = isl_basic_set_lexmin(bset);
3985 set = set_parameter_preimage(set, T2);
3986 set = set_append_equalities(set, eq);
3990 /* Compute an explicit representation for all the existentially
3991 * quantified variables.
3992 * The input and output dimensions are first turned into parameters.
3993 * compute_divs then returns a map with the same parameters and
3994 * no input or output dimensions and the dimension specification
3995 * is reset to that of the input.
3997 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
3999 struct isl_basic_set *bset;
4000 struct isl_set *set;
4001 struct isl_map *map;
4002 struct isl_dim *dim, *orig_dim = NULL;
4007 bmap = isl_basic_map_cow(bmap);
4011 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4012 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4013 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4014 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4018 orig_dim = bmap->dim;
4020 bset = (struct isl_basic_set *)bmap;
4022 set = parameter_compute_divs(bset);
4023 map = (struct isl_map *)set;
4024 map = isl_map_reset_dim(map, orig_dim);
4028 isl_basic_map_free(bmap);
4032 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4040 off = isl_dim_total(bmap->dim);
4041 for (i = 0; i < bmap->n_div; ++i) {
4042 if (isl_int_is_zero(bmap->div[i][0]))
4044 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
4050 static int map_divs_known(__isl_keep isl_map *map)
4057 for (i = 0; i < map->n; ++i) {
4058 int known = basic_map_divs_known(map->p[i]);
4066 /* If bmap contains any unknown divs, then compute explicit
4067 * expressions for them. However, this computation may be
4068 * quite expensive, so first try to remove divs that aren't
4071 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
4075 struct isl_map *map;
4077 known = basic_map_divs_known(bmap);
4081 return isl_map_from_basic_map(bmap);
4083 bmap = isl_basic_map_drop_redundant_divs(bmap);
4085 known = basic_map_divs_known(bmap);
4089 return isl_map_from_basic_map(bmap);
4091 map = compute_divs(bmap);
4094 isl_basic_map_free(bmap);
4098 struct isl_map *isl_map_compute_divs(struct isl_map *map)
4102 struct isl_map *res;
4109 known = map_divs_known(map);
4117 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
4118 for (i = 1 ; i < map->n; ++i) {
4120 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
4121 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4122 res = isl_map_union_disjoint(res, r2);
4124 res = isl_map_union(res, r2);
4131 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
4133 return (struct isl_set *)
4134 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
4137 struct isl_set *isl_set_compute_divs(struct isl_set *set)
4139 return (struct isl_set *)
4140 isl_map_compute_divs((struct isl_map *)set);
4143 struct isl_set *isl_map_domain(struct isl_map *map)
4146 struct isl_set *set;
4151 map = isl_map_cow(map);
4155 set = (struct isl_set *)map;
4156 set->dim = isl_dim_domain(set->dim);
4159 for (i = 0; i < map->n; ++i) {
4160 set->p[i] = isl_basic_map_domain(map->p[i]);
4164 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4165 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4172 struct isl_map *isl_map_union_disjoint(
4173 struct isl_map *map1, struct isl_map *map2)
4177 struct isl_map *map = NULL;
4191 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4193 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4194 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4195 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4197 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
4198 map1->n + map2->n, flags);
4201 for (i = 0; i < map1->n; ++i) {
4202 map = isl_map_add_basic_map(map,
4203 isl_basic_map_copy(map1->p[i]));
4207 for (i = 0; i < map2->n; ++i) {
4208 map = isl_map_add_basic_map(map,
4209 isl_basic_map_copy(map2->p[i]));
4223 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
4225 map1 = isl_map_union_disjoint(map1, map2);
4229 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
4233 struct isl_set *isl_set_union_disjoint(
4234 struct isl_set *set1, struct isl_set *set2)
4236 return (struct isl_set *)
4237 isl_map_union_disjoint(
4238 (struct isl_map *)set1, (struct isl_map *)set2);
4241 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
4243 return (struct isl_set *)
4244 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
4247 struct isl_map *isl_map_intersect_range(
4248 struct isl_map *map, struct isl_set *set)
4251 struct isl_map *result;
4257 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
4258 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
4259 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4261 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
4262 map->n * set->n, flags);
4265 for (i = 0; i < map->n; ++i)
4266 for (j = 0; j < set->n; ++j) {
4267 result = isl_map_add_basic_map(result,
4268 isl_basic_map_intersect_range(
4269 isl_basic_map_copy(map->p[i]),
4270 isl_basic_set_copy(set->p[j])));
4283 struct isl_map *isl_map_intersect_domain(
4284 struct isl_map *map, struct isl_set *set)
4286 return isl_map_reverse(
4287 isl_map_intersect_range(isl_map_reverse(map), set));
4290 struct isl_map *isl_map_apply_domain(
4291 struct isl_map *map1, struct isl_map *map2)
4295 map1 = isl_map_reverse(map1);
4296 map1 = isl_map_apply_range(map1, map2);
4297 return isl_map_reverse(map1);
4304 struct isl_map *isl_map_apply_range(
4305 struct isl_map *map1, struct isl_map *map2)
4307 struct isl_dim *dim_result;
4308 struct isl_map *result;
4314 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
4315 isl_dim_copy(map2->dim));
4317 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
4320 for (i = 0; i < map1->n; ++i)
4321 for (j = 0; j < map2->n; ++j) {
4322 result = isl_map_add_basic_map(result,
4323 isl_basic_map_apply_range(
4324 isl_basic_map_copy(map1->p[i]),
4325 isl_basic_map_copy(map2->p[j])));
4331 if (result && result->n <= 1)
4332 ISL_F_SET(result, ISL_MAP_DISJOINT);
4341 * returns range - domain
4343 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
4345 struct isl_basic_set *bset;
4352 dim = isl_basic_map_n_in(bmap);
4353 nparam = isl_basic_map_n_param(bmap);
4354 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
4355 bset = isl_basic_set_from_basic_map(bmap);
4356 bset = isl_basic_set_cow(bset);
4357 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
4358 bset = isl_basic_set_swap_vars(bset, 2*dim);
4359 for (i = 0; i < dim; ++i) {
4360 int j = isl_basic_map_alloc_equality(
4361 (struct isl_basic_map *)bset);
4364 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
4365 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
4366 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
4367 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
4369 return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
4371 isl_basic_map_free(bmap);
4376 * returns range - domain
4378 struct isl_set *isl_map_deltas(struct isl_map *map)
4381 struct isl_set *result;
4386 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
4387 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
4388 isl_map_n_in(map), map->n, map->flags);
4391 for (i = 0; i < map->n; ++i)
4392 result = isl_set_add_basic_set(result,
4393 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
4401 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
4403 struct isl_basic_map *bmap;
4411 nparam = dims->nparam;
4413 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
4417 for (i = 0; i < dim; ++i) {
4418 int j = isl_basic_map_alloc_equality(bmap);
4421 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
4422 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
4423 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
4425 return isl_basic_map_finalize(bmap);
4427 isl_basic_map_free(bmap);
4431 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
4433 struct isl_dim *dim = isl_dim_map(set_dim);
4436 return basic_map_identity(dim);
4439 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
4441 if (!model || !model->dim)
4443 isl_assert(model->ctx,
4444 model->dim->n_in == model->dim->n_out, return NULL);
4445 return basic_map_identity(isl_dim_copy(model->dim));
4448 static struct isl_map *map_identity(struct isl_dim *dim)
4450 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
4451 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
4454 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
4456 struct isl_dim *dim = isl_dim_map(set_dim);
4459 return map_identity(dim);
4462 struct isl_map *isl_map_identity_like(struct isl_map *model)
4464 if (!model || !model->dim)
4466 isl_assert(model->ctx,
4467 model->dim->n_in == model->dim->n_out, return NULL);
4468 return map_identity(isl_dim_copy(model->dim));
4471 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
4473 if (!model || !model->dim)
4475 isl_assert(model->ctx,
4476 model->dim->n_in == model->dim->n_out, return NULL);
4477 return map_identity(isl_dim_copy(model->dim));
4480 /* Construct a basic set with all set dimensions having only non-negative
4483 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
4488 struct isl_basic_set *bset;
4492 nparam = dims->nparam;
4494 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
4497 for (i = 0; i < dim; ++i) {
4498 int k = isl_basic_set_alloc_inequality(bset);
4501 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
4502 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
4506 isl_basic_set_free(bset);
4510 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
4512 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
4515 int isl_basic_map_is_subset(
4516 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4519 struct isl_map *map1;
4520 struct isl_map *map2;
4522 if (!bmap1 || !bmap2)
4525 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
4526 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
4528 is_subset = isl_map_is_subset(map1, map2);
4536 int isl_basic_map_is_equal(
4537 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4541 if (!bmap1 || !bmap2)
4543 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4546 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4550 int isl_basic_set_is_equal(
4551 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4553 return isl_basic_map_is_equal(
4554 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
4557 int isl_map_is_empty(struct isl_map *map)
4564 for (i = 0; i < map->n; ++i) {
4565 is_empty = isl_basic_map_is_empty(map->p[i]);
4574 int isl_map_fast_is_empty(struct isl_map *map)
4579 int isl_set_fast_is_empty(struct isl_set *set)
4584 int isl_set_is_empty(struct isl_set *set)
4586 return isl_map_is_empty((struct isl_map *)set);
4589 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
4595 is_subset = isl_map_is_subset(map1, map2);
4598 is_subset = isl_map_is_subset(map2, map1);
4602 int isl_basic_map_is_strict_subset(
4603 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4607 if (!bmap1 || !bmap2)
4609 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4612 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4613 if (is_subset == -1)
4618 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
4624 is_subset = isl_map_is_subset(map1, map2);
4627 is_subset = isl_map_is_subset(map2, map1);
4628 if (is_subset == -1)
4633 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4635 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
4638 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
4642 return bmap->n_eq == 0 && bmap->n_ineq == 0;
4645 int isl_basic_set_is_universe(struct isl_basic_set *bset)
4649 return bset->n_eq == 0 && bset->n_ineq == 0;
4652 int isl_map_fast_is_universe(__isl_keep isl_map *map)
4657 return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
4660 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
4662 struct isl_basic_set *bset = NULL;
4663 struct isl_vec *sample = NULL;
4670 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4673 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
4674 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
4675 copy = isl_basic_map_convex_hull(copy);
4676 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
4677 isl_basic_map_free(copy);
4681 total = 1 + isl_basic_map_total_dim(bmap);
4682 if (bmap->sample && bmap->sample->size == total) {
4683 int contains = isl_basic_map_contains(bmap, bmap->sample);
4689 isl_vec_free(bmap->sample);
4690 bmap->sample = NULL;
4691 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
4694 sample = isl_basic_set_sample_vec(bset);
4697 empty = sample->size == 0;
4698 isl_vec_free(bmap->sample);
4699 bmap->sample = sample;
4701 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
4706 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
4710 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
4713 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
4717 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
4720 int isl_basic_set_is_empty(struct isl_basic_set *bset)
4722 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
4725 struct isl_map *isl_basic_map_union(
4726 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4728 struct isl_map *map;
4729 if (!bmap1 || !bmap2)
4732 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
4734 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
4737 map = isl_map_add_basic_map(map, bmap1);
4738 map = isl_map_add_basic_map(map, bmap2);
4741 isl_basic_map_free(bmap1);
4742 isl_basic_map_free(bmap2);
4746 struct isl_set *isl_basic_set_union(
4747 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4749 return (struct isl_set *)isl_basic_map_union(
4750 (struct isl_basic_map *)bset1,
4751 (struct isl_basic_map *)bset2);
4754 /* Order divs such that any div only depends on previous divs */
4755 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
4758 unsigned off = isl_dim_total(bmap->dim);
4760 for (i = 0; i < bmap->n_div; ++i) {
4762 if (isl_int_is_zero(bmap->div[i][0]))
4764 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
4768 isl_basic_map_swap_div(bmap, i, i + pos);
4774 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
4776 return (struct isl_basic_set *)
4777 isl_basic_map_order_divs((struct isl_basic_map *)bset);
4780 /* Look for a div in dst that corresponds to the div "div" in src.
4781 * The divs before "div" in src and dst are assumed to be the same.
4783 * Returns -1 if no corresponding div was found and the position
4784 * of the corresponding div in dst otherwise.
4786 static int find_div(struct isl_basic_map *dst,
4787 struct isl_basic_map *src, unsigned div)
4791 unsigned total = isl_dim_total(src->dim);
4793 isl_assert(dst->ctx, div <= dst->n_div, return -1);
4794 for (i = div; i < dst->n_div; ++i)
4795 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
4796 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
4797 dst->n_div - div) == -1)
4802 struct isl_basic_map *isl_basic_map_align_divs(
4803 struct isl_basic_map *dst, struct isl_basic_map *src)
4806 unsigned total = isl_dim_total(src->dim);
4811 if (src->n_div == 0)
4814 for (i = 0; i < src->n_div; ++i)
4815 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
4817 src = isl_basic_map_order_divs(src);
4818 dst = isl_basic_map_cow(dst);
4819 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
4820 src->n_div, 0, 2 * src->n_div);
4823 for (i = 0; i < src->n_div; ++i) {
4824 int j = find_div(dst, src, i);
4826 j = isl_basic_map_alloc_div(dst);
4829 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
4830 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
4831 if (isl_basic_map_add_div_constraints(dst, j) < 0)
4835 isl_basic_map_swap_div(dst, i, j);
4839 isl_basic_map_free(dst);
4843 struct isl_basic_set *isl_basic_set_align_divs(
4844 struct isl_basic_set *dst, struct isl_basic_set *src)
4846 return (struct isl_basic_set *)isl_basic_map_align_divs(
4847 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
4850 struct isl_map *isl_map_align_divs(struct isl_map *map)
4858 map = isl_map_compute_divs(map);
4859 map = isl_map_cow(map);
4863 for (i = 1; i < map->n; ++i)
4864 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
4865 for (i = 1; i < map->n; ++i)
4866 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
4868 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4872 struct isl_set *isl_set_align_divs(struct isl_set *set)
4874 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
4877 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
4881 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
4882 map = isl_map_intersect_domain(map, set);
4883 set = isl_map_range(map);
4891 /* There is no need to cow as removing empty parts doesn't change
4892 * the meaning of the set.
4894 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
4901 for (i = map->n-1; i >= 0; --i) {
4902 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
4904 isl_basic_map_free(map->p[i]);
4905 if (i != map->n-1) {
4906 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4907 map->p[i] = map->p[map->n-1];
4915 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
4917 return (struct isl_set *)
4918 isl_map_remove_empty_parts((struct isl_map *)set);
4921 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
4923 struct isl_basic_map *bmap;
4924 if (!map || map->n == 0)
4926 bmap = map->p[map->n-1];
4927 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
4928 return isl_basic_map_copy(bmap);
4931 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
4933 return (struct isl_basic_set *)
4934 isl_map_copy_basic_map((struct isl_map *)set);
4937 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
4938 __isl_keep isl_basic_map *bmap)
4944 for (i = map->n-1; i >= 0; --i) {
4945 if (map->p[i] != bmap)
4947 map = isl_map_cow(map);
4950 isl_basic_map_free(map->p[i]);
4951 if (i != map->n-1) {
4952 ISL_F_CLR(map, ISL_SET_NORMALIZED);
4953 map->p[i] = map->p[map->n-1];
4964 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4965 struct isl_basic_set *bset)
4967 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
4968 (struct isl_basic_map *)bset);
4971 /* Given two basic sets bset1 and bset2, compute the maximal difference
4972 * between the values of dimension pos in bset1 and those in bset2
4973 * for any common value of the parameters and dimensions preceding pos.
4975 static enum isl_lp_result basic_set_maximal_difference_at(
4976 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
4977 int pos, isl_int *opt)
4979 struct isl_dim *dims;
4980 struct isl_basic_map *bmap1 = NULL;
4981 struct isl_basic_map *bmap2 = NULL;
4982 struct isl_ctx *ctx;
4983 struct isl_vec *obj;
4986 unsigned dim1, dim2;
4987 enum isl_lp_result res;
4989 if (!bset1 || !bset2)
4990 return isl_lp_error;
4992 nparam = isl_basic_set_n_param(bset1);
4993 dim1 = isl_basic_set_n_dim(bset1);
4994 dim2 = isl_basic_set_n_dim(bset2);
4995 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
4996 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
4997 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
4998 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
4999 if (!bmap1 || !bmap2)
5001 bmap1 = isl_basic_map_cow(bmap1);
5002 bmap1 = isl_basic_map_extend(bmap1, nparam,
5003 pos, (dim1 - pos) + (dim2 - pos),
5004 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
5005 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
5008 total = isl_basic_map_total_dim(bmap1);
5010 obj = isl_vec_alloc(ctx, 1 + total);
5011 isl_seq_clr(obj->block.data, 1 + total);
5012 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
5013 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
5016 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
5018 isl_basic_map_free(bmap1);
5022 isl_basic_map_free(bmap1);
5023 isl_basic_map_free(bmap2);
5024 return isl_lp_error;
5027 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5028 * for any common value of the parameters and dimensions preceding pos
5029 * in both basic sets, the values of dimension pos in bset1 are
5030 * smaller or larger than those in bset2.
5033 * 1 if bset1 follows bset2
5034 * -1 if bset1 precedes bset2
5035 * 0 if bset1 and bset2 are incomparable
5036 * -2 if some error occurred.
5038 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
5039 struct isl_basic_set *bset2, int pos)
5042 enum isl_lp_result res;
5047 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5049 if (res == isl_lp_empty)
5051 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5052 res == isl_lp_unbounded)
5054 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5063 /* Given two basic sets bset1 and bset2, check whether
5064 * for any common value of the parameters and dimensions preceding pos
5065 * there is a value of dimension pos in bset1 that is larger
5066 * than a value of the same dimension in bset2.
5069 * 1 if there exists such a pair
5070 * 0 if there is no such pair, but there is a pair of equal values
5072 * -2 if some error occurred.
5074 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
5075 __isl_keep isl_basic_set *bset2, int pos)
5078 enum isl_lp_result res;
5083 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5085 if (res == isl_lp_empty)
5087 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5088 res == isl_lp_unbounded)
5090 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5092 else if (res == isl_lp_ok)
5101 /* Given two sets set1 and set2, check whether
5102 * for any common value of the parameters and dimensions preceding pos
5103 * there is a value of dimension pos in set1 that is larger
5104 * than a value of the same dimension in set2.
5107 * 1 if there exists such a pair
5108 * 0 if there is no such pair, but there is a pair of equal values
5110 * -2 if some error occurred.
5112 int isl_set_follows_at(__isl_keep isl_set *set1,
5113 __isl_keep isl_set *set2, int pos)
5121 for (i = 0; i < set1->n; ++i)
5122 for (j = 0; j < set2->n; ++j) {
5124 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
5125 if (f == 1 || f == -2)
5134 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
5135 unsigned pos, isl_int *val)
5143 total = isl_basic_map_total_dim(bmap);
5144 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5145 for (; d+1 > pos; --d)
5146 if (!isl_int_is_zero(bmap->eq[i][1+d]))
5150 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5152 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5154 if (!isl_int_is_one(bmap->eq[i][1+d]))
5157 isl_int_neg(*val, bmap->eq[i][0]);
5163 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5164 unsigned pos, isl_int *val)
5176 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
5179 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
5180 for (i = 1; fixed == 1 && i < map->n; ++i) {
5181 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
5182 if (fixed == 1 && isl_int_ne(tmp, v))
5186 isl_int_set(*val, v);
5192 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5193 unsigned pos, isl_int *val)
5195 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
5199 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
5202 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
5205 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
5206 enum isl_dim_type type, unsigned pos, isl_int *val)
5208 if (pos >= isl_basic_map_dim(bmap, type))
5210 return isl_basic_map_fast_has_fixed_var(bmap,
5211 isl_basic_map_offset(bmap, type) - 1 + pos, val);
5214 int isl_map_fast_is_fixed(struct isl_map *map,
5215 enum isl_dim_type type, unsigned pos, isl_int *val)
5217 if (pos >= isl_map_dim(map, type))
5219 return isl_map_fast_has_fixed_var(map,
5220 map_offset(map, type) - 1 + pos, val);
5223 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5224 * then return this fixed value in *val.
5226 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
5229 return isl_basic_set_fast_has_fixed_var(bset,
5230 isl_basic_set_n_param(bset) + dim, val);
5233 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5234 * then return this fixed value in *val.
5236 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
5238 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
5241 /* Check if input variable in has fixed value and if so and if val is not NULL,
5242 * then return this fixed value in *val.
5244 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
5246 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
5249 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5250 * and if val is not NULL, then return this lower bound in *val.
5252 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
5253 unsigned dim, isl_int *val)
5255 int i, i_eq = -1, i_ineq = -1;
5262 total = isl_basic_set_total_dim(bset);
5263 nparam = isl_basic_set_n_param(bset);
5264 for (i = 0; i < bset->n_eq; ++i) {
5265 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
5271 for (i = 0; i < bset->n_ineq; ++i) {
5272 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
5274 if (i_eq != -1 || i_ineq != -1)
5278 if (i_eq == -1 && i_ineq == -1)
5280 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
5281 /* The coefficient should always be one due to normalization. */
5282 if (!isl_int_is_one(c[1+nparam+dim]))
5284 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
5286 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
5287 total - nparam - dim - 1) != -1)
5290 isl_int_neg(*val, c[0]);
5294 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
5295 unsigned dim, isl_int *val)
5307 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5311 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5313 for (i = 1; fixed == 1 && i < set->n; ++i) {
5314 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
5316 if (fixed == 1 && isl_int_ne(tmp, v))
5320 isl_int_set(*val, v);
5331 static int qsort_constraint_cmp(const void *p1, const void *p2)
5333 const struct constraint *c1 = (const struct constraint *)p1;
5334 const struct constraint *c2 = (const struct constraint *)p2;
5335 unsigned size = isl_min(c1->size, c2->size);
5336 return isl_seq_cmp(c1->c, c2->c, size);
5339 static struct isl_basic_map *isl_basic_map_sort_constraints(
5340 struct isl_basic_map *bmap)
5343 struct constraint *c;
5348 total = isl_basic_map_total_dim(bmap);
5349 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
5352 for (i = 0; i < bmap->n_ineq; ++i) {
5354 c[i].c = bmap->ineq[i];
5356 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
5357 for (i = 0; i < bmap->n_ineq; ++i)
5358 bmap->ineq[i] = c[i].c;
5362 isl_basic_map_free(bmap);
5366 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
5370 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
5372 bmap = isl_basic_map_convex_hull(bmap);
5373 bmap = isl_basic_map_sort_constraints(bmap);
5374 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
5378 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
5380 return (struct isl_basic_set *)isl_basic_map_normalize(
5381 (struct isl_basic_map *)bset);
5384 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
5385 const struct isl_basic_map *bmap2)
5392 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
5393 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
5394 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
5395 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5396 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
5397 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5398 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
5399 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5401 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
5403 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5405 if (bmap1->n_eq != bmap2->n_eq)
5406 return bmap1->n_eq - bmap2->n_eq;
5407 if (bmap1->n_ineq != bmap2->n_ineq)
5408 return bmap1->n_ineq - bmap2->n_ineq;
5409 if (bmap1->n_div != bmap2->n_div)
5410 return bmap1->n_div - bmap2->n_div;
5411 total = isl_basic_map_total_dim(bmap1);
5412 for (i = 0; i < bmap1->n_eq; ++i) {
5413 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
5417 for (i = 0; i < bmap1->n_ineq; ++i) {
5418 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
5422 for (i = 0; i < bmap1->n_div; ++i) {
5423 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
5430 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
5431 struct isl_basic_map *bmap2)
5433 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
5436 static int qsort_bmap_cmp(const void *p1, const void *p2)
5438 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
5439 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
5441 return isl_basic_map_fast_cmp(bmap1, bmap2);
5444 /* We normalize in place, but if anything goes wrong we need
5445 * to return NULL, so we need to make sure we don't change the
5446 * meaning of any possible other copies of map.
5448 struct isl_map *isl_map_normalize(struct isl_map *map)
5451 struct isl_basic_map *bmap;
5455 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
5457 for (i = 0; i < map->n; ++i) {
5458 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
5461 isl_basic_map_free(map->p[i]);
5464 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
5465 ISL_F_SET(map, ISL_MAP_NORMALIZED);
5466 map = isl_map_remove_empty_parts(map);
5469 for (i = map->n - 1; i >= 1; --i) {
5470 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
5472 isl_basic_map_free(map->p[i-1]);
5473 for (j = i; j < map->n; ++j)
5474 map->p[j-1] = map->p[j];
5484 struct isl_set *isl_set_normalize(struct isl_set *set)
5486 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
5489 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
5499 if (!isl_dim_equal(map1->dim, map2->dim))
5502 map1 = isl_map_copy(map1);
5503 map2 = isl_map_copy(map2);
5504 map1 = isl_map_normalize(map1);
5505 map2 = isl_map_normalize(map2);
5508 equal = map1->n == map2->n;
5509 for (i = 0; equal && i < map1->n; ++i) {
5510 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
5523 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
5525 return isl_map_fast_is_equal((struct isl_map *)set1,
5526 (struct isl_map *)set2);
5529 /* Return an interval that ranges from min to max (inclusive)
5531 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
5532 isl_int min, isl_int max)
5535 struct isl_basic_set *bset = NULL;
5537 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
5541 k = isl_basic_set_alloc_inequality(bset);
5544 isl_int_set_si(bset->ineq[k][1], 1);
5545 isl_int_neg(bset->ineq[k][0], min);
5547 k = isl_basic_set_alloc_inequality(bset);
5550 isl_int_set_si(bset->ineq[k][1], -1);
5551 isl_int_set(bset->ineq[k][0], max);
5555 isl_basic_set_free(bset);
5559 /* Return the Cartesian product of the basic sets in list (in the given order).
5561 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
5569 struct isl_basic_set *product = NULL;
5573 isl_assert(list->ctx, list->n > 0, goto error);
5574 isl_assert(list->ctx, list->p[0], goto error);
5575 nparam = isl_basic_set_n_param(list->p[0]);
5576 dim = isl_basic_set_n_dim(list->p[0]);
5577 extra = list->p[0]->n_div;
5578 n_eq = list->p[0]->n_eq;
5579 n_ineq = list->p[0]->n_ineq;
5580 for (i = 1; i < list->n; ++i) {
5581 isl_assert(list->ctx, list->p[i], goto error);
5582 isl_assert(list->ctx,
5583 nparam == isl_basic_set_n_param(list->p[i]), goto error);
5584 dim += isl_basic_set_n_dim(list->p[i]);
5585 extra += list->p[i]->n_div;
5586 n_eq += list->p[i]->n_eq;
5587 n_ineq += list->p[i]->n_ineq;
5589 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
5594 for (i = 0; i < list->n; ++i) {
5595 isl_basic_set_add_constraints(product,
5596 isl_basic_set_copy(list->p[i]), dim);
5597 dim += isl_basic_set_n_dim(list->p[i]);
5599 isl_basic_set_list_free(list);
5602 isl_basic_set_free(product);
5603 isl_basic_set_list_free(list);
5607 struct isl_basic_map *isl_basic_map_product(
5608 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5610 struct isl_dim *dim_result = NULL;
5611 struct isl_basic_map *bmap;
5612 unsigned in1, in2, out1, out2, nparam, total, pos;
5613 struct isl_dim_map *dim_map1, *dim_map2;
5615 if (!bmap1 || !bmap2)
5618 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
5619 bmap2->dim, isl_dim_param), goto error);
5620 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
5621 isl_dim_copy(bmap2->dim));
5623 in1 = isl_basic_map_n_in(bmap1);
5624 in2 = isl_basic_map_n_in(bmap2);
5625 out1 = isl_basic_map_n_out(bmap1);
5626 out2 = isl_basic_map_n_out(bmap2);
5627 nparam = isl_basic_map_n_param(bmap1);
5629 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
5630 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5631 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
5632 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5633 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
5634 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5635 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
5636 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
5637 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
5638 isl_dim_map_div(dim_map1, bmap1, pos += out2);
5639 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5641 bmap = isl_basic_map_alloc_dim(dim_result,
5642 bmap1->n_div + bmap2->n_div,
5643 bmap1->n_eq + bmap2->n_eq,
5644 bmap1->n_ineq + bmap2->n_ineq);
5645 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
5646 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
5647 bmap = isl_basic_map_simplify(bmap);
5648 return isl_basic_map_finalize(bmap);
5650 isl_basic_map_free(bmap1);
5651 isl_basic_map_free(bmap2);
5655 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5657 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
5660 struct isl_map *result;
5666 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
5667 map2->dim, isl_dim_param), goto error);
5669 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5670 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5671 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5673 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
5674 isl_dim_copy(map2->dim)),
5675 map1->n * map2->n, flags);
5678 for (i = 0; i < map1->n; ++i)
5679 for (j = 0; j < map2->n; ++j) {
5680 struct isl_basic_map *part;
5681 part = isl_basic_map_product(
5682 isl_basic_map_copy(map1->p[i]),
5683 isl_basic_map_copy(map2->p[j]));
5684 if (isl_basic_map_is_empty(part))
5685 isl_basic_map_free(part);
5687 result = isl_map_add_basic_map(result, part);
5700 /* Given two set A and B, construct its Cartesian product A x B.
5702 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
5704 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
5705 (struct isl_map *)set2);
5708 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
5711 uint32_t hash = isl_hash_init();
5716 bset = isl_basic_set_copy(bset);
5717 bset = isl_basic_set_normalize(bset);
5720 total = isl_basic_set_total_dim(bset);
5721 isl_hash_byte(hash, bset->n_eq & 0xFF);
5722 for (i = 0; i < bset->n_eq; ++i) {
5724 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
5725 isl_hash_hash(hash, c_hash);
5727 isl_hash_byte(hash, bset->n_ineq & 0xFF);
5728 for (i = 0; i < bset->n_ineq; ++i) {
5730 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
5731 isl_hash_hash(hash, c_hash);
5733 isl_hash_byte(hash, bset->n_div & 0xFF);
5734 for (i = 0; i < bset->n_div; ++i) {
5736 if (isl_int_is_zero(bset->div[i][0]))
5738 isl_hash_byte(hash, i & 0xFF);
5739 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
5740 isl_hash_hash(hash, c_hash);
5742 isl_basic_set_free(bset);
5746 uint32_t isl_set_get_hash(struct isl_set *set)
5753 set = isl_set_copy(set);
5754 set = isl_set_normalize(set);
5758 hash = isl_hash_init();
5759 for (i = 0; i < set->n; ++i) {
5761 bset_hash = isl_basic_set_get_hash(set->p[i]);
5762 isl_hash_hash(hash, bset_hash);
5770 /* Check if the value for dimension dim is completely determined
5771 * by the values of the other parameters and variables.
5772 * That is, check if dimension dim is involved in an equality.
5774 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
5781 nparam = isl_basic_set_n_param(bset);
5782 for (i = 0; i < bset->n_eq; ++i)
5783 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
5788 /* Check if the value for dimension dim is completely determined
5789 * by the values of the other parameters and variables.
5790 * That is, check if dimension dim is involved in an equality
5791 * for each of the subsets.
5793 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
5799 for (i = 0; i < set->n; ++i) {
5801 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
5808 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
5809 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
5816 for (i = 0; i < map->n; ++i)
5817 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
5823 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
5824 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
5831 for (i = 0; i < set->n; ++i)
5832 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
5838 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
5840 struct isl_dim *dim;
5841 struct isl_basic_map *bmap;
5848 set = isl_set_align_divs(set);
5853 dim = isl_set_get_dim(set);
5854 if (set->n == 0 || set->p[0]->n_div == 0) {
5856 return isl_map_identity(dim);
5859 n_div = set->p[0]->n_div;
5860 dim = isl_dim_map(dim);
5861 n_param = isl_dim_size(dim, isl_dim_param);
5862 n_set = isl_dim_size(dim, isl_dim_in);
5863 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
5864 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
5865 for (i = 0; i < n_set; ++i)
5866 bmap = var_equal(bmap, i);
5868 total = n_param + n_set + n_set + n_div;
5869 for (i = 0; i < n_div; ++i) {
5870 k = isl_basic_map_alloc_inequality(bmap);
5873 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
5874 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
5875 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
5876 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
5877 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
5878 set->p[0]->div[i][0]);
5880 l = isl_basic_map_alloc_inequality(bmap);
5883 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
5884 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
5885 set->p[0]->div[i][0]);
5886 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
5890 return isl_map_from_basic_map(bmap);
5893 isl_basic_map_free(bmap);
5897 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
5905 dim = isl_basic_set_total_dim(bset);
5906 size += bset->n_eq * (1 + dim);
5907 size += bset->n_ineq * (1 + dim);
5908 size += bset->n_div * (2 + dim);
5913 int isl_set_size(__isl_keep isl_set *set)
5921 for (i = 0; i < set->n; ++i)
5922 size += isl_basic_set_size(set->p[i]);