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(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(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(map, bmap);
1604 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1606 return (struct isl_set *)isl_map_add((struct isl_map *)set,
1607 (struct isl_basic_map *)bset);
1610 void isl_set_free(struct isl_set *set)
1620 isl_ctx_deref(set->ctx);
1621 for (i = 0; i < set->n; ++i)
1622 isl_basic_set_free(set->p[i]);
1623 isl_dim_free(set->dim);
1627 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1632 fprintf(out, "null set\n");
1636 fprintf(out, "%*s", indent, "");
1637 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1638 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1640 for (i = 0; i < set->n; ++i) {
1641 fprintf(out, "%*s", indent, "");
1642 fprintf(out, "basic set %d:\n", i);
1643 isl_basic_set_dump(set->p[i], out, indent+4);
1647 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1652 fprintf(out, "null map\n");
1656 fprintf(out, "%*s", indent, "");
1657 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1658 "flags: %x, n_name: %d\n",
1659 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1660 map->dim->n_out, map->flags, map->dim->n_name);
1661 for (i = 0; i < map->n; ++i) {
1662 fprintf(out, "%*s", indent, "");
1663 fprintf(out, "basic map %d:\n", i);
1664 isl_basic_map_dump(map->p[i], out, indent+4);
1668 struct isl_basic_map *isl_basic_map_intersect_domain(
1669 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1671 struct isl_basic_map *bmap_domain;
1672 struct isl_dim *dim;
1677 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1678 bset->dim, isl_dim_param), goto error);
1680 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1681 isl_assert(bset->ctx,
1682 isl_basic_map_compatible_domain(bmap, bset), goto error);
1684 bmap = isl_basic_map_cow(bmap);
1685 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1686 bset->n_div, bset->n_eq, bset->n_ineq);
1689 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1690 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1691 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1693 bmap = isl_basic_map_simplify(bmap);
1694 return isl_basic_map_finalize(bmap);
1696 isl_basic_map_free(bmap);
1697 isl_basic_set_free(bset);
1701 struct isl_basic_map *isl_basic_map_intersect_range(
1702 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1704 struct isl_basic_map *bmap_range;
1709 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1710 bset->dim, isl_dim_param), goto error);
1712 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1713 isl_assert(bset->ctx,
1714 isl_basic_map_compatible_range(bmap, bset), goto error);
1716 bmap = isl_basic_map_cow(bmap);
1717 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1718 bset->n_div, bset->n_eq, bset->n_ineq);
1721 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1722 bmap = add_constraints(bmap, bmap_range, 0, 0);
1724 bmap = isl_basic_map_simplify(bmap);
1725 return isl_basic_map_finalize(bmap);
1727 isl_basic_map_free(bmap);
1728 isl_basic_set_free(bset);
1732 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1738 total = 1 + isl_basic_map_total_dim(bmap);
1739 if (total != vec->size)
1744 for (i = 0; i < bmap->n_eq; ++i) {
1745 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1746 if (!isl_int_is_zero(s)) {
1752 for (i = 0; i < bmap->n_ineq; ++i) {
1753 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1754 if (isl_int_is_neg(s)) {
1765 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1767 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1770 struct isl_basic_map *isl_basic_map_intersect(
1771 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1773 struct isl_vec *sample = NULL;
1775 if (!bmap1 || !bmap2)
1778 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1779 bmap2->dim, isl_dim_param), goto error);
1780 if (isl_dim_total(bmap1->dim) ==
1781 isl_dim_size(bmap1->dim, isl_dim_param) &&
1782 isl_dim_total(bmap2->dim) !=
1783 isl_dim_size(bmap2->dim, isl_dim_param))
1784 return isl_basic_map_intersect(bmap2, bmap1);
1786 if (isl_dim_total(bmap2->dim) !=
1787 isl_dim_size(bmap2->dim, isl_dim_param))
1788 isl_assert(bmap1->ctx,
1789 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1791 if (bmap1->sample &&
1792 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1793 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1794 sample = isl_vec_copy(bmap1->sample);
1795 else if (bmap2->sample &&
1796 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1797 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1798 sample = isl_vec_copy(bmap2->sample);
1800 bmap1 = isl_basic_map_cow(bmap1);
1801 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1802 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1805 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1808 isl_vec_free(bmap1->sample);
1809 bmap1->sample = sample;
1812 bmap1 = isl_basic_map_simplify(bmap1);
1813 return isl_basic_map_finalize(bmap1);
1816 isl_vec_free(sample);
1817 isl_basic_map_free(bmap1);
1818 isl_basic_map_free(bmap2);
1822 struct isl_basic_set *isl_basic_set_intersect(
1823 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1825 return (struct isl_basic_set *)
1826 isl_basic_map_intersect(
1827 (struct isl_basic_map *)bset1,
1828 (struct isl_basic_map *)bset2);
1831 /* Special case of isl_map_intersect, where both map1 and map2
1832 * are convex, without any divs and such that either map1 or map2
1833 * contains a single constraint. This constraint is then simply
1834 * added to the other map.
1836 static __isl_give isl_map *map_intersect_add_constraint(
1837 __isl_take isl_map *map1, __isl_take isl_map *map2)
1839 struct isl_basic_map *bmap1;
1840 struct isl_basic_map *bmap2;
1842 isl_assert(map1->ctx, map1->n == 1, goto error);
1843 isl_assert(map2->ctx, map1->n == 1, goto error);
1844 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
1845 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
1847 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
1848 return isl_map_intersect(map2, map1);
1850 isl_assert(map2->ctx,
1851 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
1853 map1 = isl_map_cow(map1);
1856 map1->p[0] = isl_basic_map_cow(map1->p[0]);
1857 if (map2->p[0]->n_eq == 1)
1858 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
1860 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
1861 map2->p[0]->ineq[0]);
1863 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
1864 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
1877 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1880 struct isl_map *result;
1886 if (map1->n == 1 && map2->n == 1 &&
1887 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
1888 isl_dim_equal(map1->dim, map2->dim) &&
1889 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
1890 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
1891 return map_intersect_add_constraint(map1, map2);
1892 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1893 map2->dim, isl_dim_param), goto error);
1894 if (isl_dim_total(map1->dim) ==
1895 isl_dim_size(map1->dim, isl_dim_param) &&
1896 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1897 return isl_map_intersect(map2, map1);
1899 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1900 isl_assert(map1->ctx,
1901 isl_dim_equal(map1->dim, map2->dim), goto error);
1903 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1904 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1905 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1907 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1908 map1->n * map2->n, flags);
1911 for (i = 0; i < map1->n; ++i)
1912 for (j = 0; j < map2->n; ++j) {
1913 struct isl_basic_map *part;
1914 part = isl_basic_map_intersect(
1915 isl_basic_map_copy(map1->p[i]),
1916 isl_basic_map_copy(map2->p[j]));
1917 if (isl_basic_map_is_empty(part))
1918 isl_basic_map_free(part);
1920 result = isl_map_add(result, part);
1933 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1935 return (struct isl_set *)
1936 isl_map_intersect((struct isl_map *)set1,
1937 (struct isl_map *)set2);
1940 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1942 struct isl_dim *dim;
1943 struct isl_basic_set *bset;
1948 bmap = isl_basic_map_cow(bmap);
1951 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1952 in = isl_basic_map_n_in(bmap);
1953 bset = isl_basic_set_from_basic_map(bmap);
1954 bset = isl_basic_set_swap_vars(bset, in);
1955 return isl_basic_map_from_basic_set(bset, dim);
1958 /* Turn final n dimensions into existentially quantified variables.
1960 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
1961 enum isl_dim_type type, unsigned first, unsigned n)
1971 isl_assert(bset->ctx, type == isl_dim_set, goto error);
1972 isl_assert(bset->ctx, first + n == isl_basic_set_n_dim(bset), goto error);
1977 if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL))
1978 return isl_basic_set_remove(bset, type, first, n);
1980 bset = isl_basic_set_cow(bset);
1982 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1983 old = bset->block2.data;
1984 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1985 (bset->extra + n) * (1 + row_size));
1986 if (!bset->block2.data)
1988 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1991 for (i = 0; i < n; ++i) {
1992 new_div[i] = bset->block2.data +
1993 (bset->extra + i) * (1 + row_size);
1994 isl_seq_clr(new_div[i], 1 + row_size);
1996 for (i = 0; i < bset->extra; ++i)
1997 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1999 bset->div = new_div;
2002 bset->dim = isl_dim_drop_outputs(bset->dim,
2003 isl_basic_set_n_dim(bset) - n, n);
2006 bset = isl_basic_set_simplify(bset);
2007 bset = isl_basic_set_drop_redundant_divs(bset);
2008 return isl_basic_set_finalize(bset);
2010 isl_basic_set_free(bset);
2014 /* Turn final n dimensions into existentially quantified variables.
2016 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2017 enum isl_dim_type type, unsigned first, unsigned n)
2024 isl_assert(set->ctx, type == isl_dim_set, goto error);
2025 isl_assert(set->ctx, first + n == isl_set_n_dim(set), goto error);
2030 set = isl_set_cow(set);
2033 set->dim = isl_dim_drop_outputs(set->dim, first, n);
2034 for (i = 0; i < set->n; ++i) {
2035 set->p[i] = isl_basic_set_project_out(set->p[i], type, first, n);
2040 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2047 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2051 for (i = 0; i < n; ++i) {
2052 j = isl_basic_map_alloc_div(bmap);
2055 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2059 isl_basic_map_free(bmap);
2063 struct isl_basic_map *isl_basic_map_apply_range(
2064 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2066 struct isl_dim *dim_result = NULL;
2067 struct isl_basic_map *bmap;
2068 unsigned n_in, n_out, n, nparam, total, pos;
2069 struct isl_dim_map *dim_map1, *dim_map2;
2071 if (!bmap1 || !bmap2)
2074 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2075 isl_dim_copy(bmap2->dim));
2077 n_in = isl_basic_map_n_in(bmap1);
2078 n_out = isl_basic_map_n_out(bmap2);
2079 n = isl_basic_map_n_out(bmap1);
2080 nparam = isl_basic_map_n_param(bmap1);
2082 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2083 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2084 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2085 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2086 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2087 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2088 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2089 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2090 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2091 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2092 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2094 bmap = isl_basic_map_alloc_dim(dim_result,
2095 bmap1->n_div + bmap2->n_div + n,
2096 bmap1->n_eq + bmap2->n_eq,
2097 bmap1->n_ineq + bmap2->n_ineq);
2098 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2099 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2100 bmap = add_divs(bmap, n);
2101 bmap = isl_basic_map_simplify(bmap);
2102 bmap = isl_basic_map_drop_redundant_divs(bmap);
2103 return isl_basic_map_finalize(bmap);
2105 isl_basic_map_free(bmap1);
2106 isl_basic_map_free(bmap2);
2110 struct isl_basic_set *isl_basic_set_apply(
2111 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2116 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2119 return (struct isl_basic_set *)
2120 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2122 isl_basic_set_free(bset);
2123 isl_basic_map_free(bmap);
2127 struct isl_basic_map *isl_basic_map_apply_domain(
2128 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2130 if (!bmap1 || !bmap2)
2133 isl_assert(bmap1->ctx,
2134 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2135 isl_assert(bmap1->ctx,
2136 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2139 bmap1 = isl_basic_map_reverse(bmap1);
2140 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2141 return isl_basic_map_reverse(bmap1);
2143 isl_basic_map_free(bmap1);
2144 isl_basic_map_free(bmap2);
2148 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2149 * A \cap B -> f(A) + f(B)
2151 struct isl_basic_map *isl_basic_map_sum(
2152 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2154 unsigned n_in, n_out, nparam, total, pos;
2155 struct isl_basic_map *bmap = NULL;
2156 struct isl_dim_map *dim_map1, *dim_map2;
2159 if (!bmap1 || !bmap2)
2162 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2165 nparam = isl_basic_map_n_param(bmap1);
2166 n_in = isl_basic_map_n_in(bmap1);
2167 n_out = isl_basic_map_n_out(bmap1);
2169 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2170 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2171 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2172 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2173 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2174 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2175 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2176 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2177 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2178 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2179 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2181 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2182 bmap1->n_div + bmap2->n_div + 2 * n_out,
2183 bmap1->n_eq + bmap2->n_eq + n_out,
2184 bmap1->n_ineq + bmap2->n_ineq);
2185 for (i = 0; i < n_out; ++i) {
2186 int j = isl_basic_map_alloc_equality(bmap);
2189 isl_seq_clr(bmap->eq[j], 1+total);
2190 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2191 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2192 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2194 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2195 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2196 bmap = add_divs(bmap, 2 * n_out);
2198 bmap = isl_basic_map_simplify(bmap);
2199 return isl_basic_map_finalize(bmap);
2201 isl_basic_map_free(bmap);
2202 isl_basic_map_free(bmap1);
2203 isl_basic_map_free(bmap2);
2207 /* Given two maps A -> f(A) and B -> g(B), construct a map
2208 * A \cap B -> f(A) + f(B)
2210 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2212 struct isl_map *result;
2218 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2220 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2221 map1->n * map2->n, 0);
2224 for (i = 0; i < map1->n; ++i)
2225 for (j = 0; j < map2->n; ++j) {
2226 struct isl_basic_map *part;
2227 part = isl_basic_map_sum(
2228 isl_basic_map_copy(map1->p[i]),
2229 isl_basic_map_copy(map2->p[j]));
2230 if (isl_basic_map_is_empty(part))
2231 isl_basic_map_free(part);
2233 result = isl_map_add(result, part);
2246 /* Given a basic map A -> f(A), construct A -> -f(A).
2248 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2253 bmap = isl_basic_map_cow(bmap);
2257 n = isl_basic_map_dim(bmap, isl_dim_out);
2258 off = isl_basic_map_offset(bmap, isl_dim_out);
2259 for (i = 0; i < bmap->n_eq; ++i)
2260 for (j = 0; j < n; ++j)
2261 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2262 for (i = 0; i < bmap->n_ineq; ++i)
2263 for (j = 0; j < n; ++j)
2264 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2265 for (i = 0; i < bmap->n_div; ++i)
2266 for (j = 0; j < n; ++j)
2267 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2268 return isl_basic_map_finalize(bmap);
2271 /* Given a map A -> f(A), construct A -> -f(A).
2273 struct isl_map *isl_map_neg(struct isl_map *map)
2277 map = isl_map_cow(map);
2281 for (i = 0; i < map->n; ++i) {
2282 map->p[i] = isl_basic_map_neg(map->p[i]);
2293 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2294 * A -> floor(f(A)/d).
2296 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2299 unsigned n_in, n_out, nparam, total, pos;
2300 struct isl_basic_map *result = NULL;
2301 struct isl_dim_map *dim_map;
2307 nparam = isl_basic_map_n_param(bmap);
2308 n_in = isl_basic_map_n_in(bmap);
2309 n_out = isl_basic_map_n_out(bmap);
2311 total = nparam + n_in + n_out + bmap->n_div + n_out;
2312 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2313 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2314 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2315 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2316 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2318 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2319 bmap->n_div + n_out,
2320 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2321 result = add_constraints_dim_map(result, bmap, dim_map);
2322 result = add_divs(result, n_out);
2323 for (i = 0; i < n_out; ++i) {
2325 j = isl_basic_map_alloc_inequality(result);
2328 isl_seq_clr(result->ineq[j], 1+total);
2329 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2330 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2331 j = isl_basic_map_alloc_inequality(result);
2334 isl_seq_clr(result->ineq[j], 1+total);
2335 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2336 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2337 isl_int_sub_ui(result->ineq[j][0], d, 1);
2340 result = isl_basic_map_simplify(result);
2341 return isl_basic_map_finalize(result);
2343 isl_basic_map_free(result);
2347 /* Given a map A -> f(A) and an integer d, construct a map
2348 * A -> floor(f(A)/d).
2350 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2354 map = isl_map_cow(map);
2358 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2359 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2360 for (i = 0; i < map->n; ++i) {
2361 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2372 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2378 i = isl_basic_map_alloc_equality(bmap);
2381 nparam = isl_basic_map_n_param(bmap);
2382 n_in = isl_basic_map_n_in(bmap);
2383 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2384 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2385 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2386 return isl_basic_map_finalize(bmap);
2388 isl_basic_map_free(bmap);
2392 /* Add a constraints to "bmap" expressing i_pos < o_pos
2394 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2400 i = isl_basic_map_alloc_inequality(bmap);
2403 nparam = isl_basic_map_n_param(bmap);
2404 n_in = isl_basic_map_n_in(bmap);
2405 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2406 isl_int_set_si(bmap->ineq[i][0], -1);
2407 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2408 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2409 return isl_basic_map_finalize(bmap);
2411 isl_basic_map_free(bmap);
2415 /* Add a constraints to "bmap" expressing i_pos > o_pos
2417 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2423 i = isl_basic_map_alloc_inequality(bmap);
2426 nparam = isl_basic_map_n_param(bmap);
2427 n_in = isl_basic_map_n_in(bmap);
2428 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2429 isl_int_set_si(bmap->ineq[i][0], -1);
2430 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2431 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2432 return isl_basic_map_finalize(bmap);
2434 isl_basic_map_free(bmap);
2438 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2441 struct isl_basic_map *bmap;
2442 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2445 for (i = 0; i < n_equal && bmap; ++i)
2446 bmap = var_equal(bmap, i);
2447 return isl_basic_map_finalize(bmap);
2450 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2452 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2455 struct isl_basic_map *bmap;
2456 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2459 for (i = 0; i < pos && bmap; ++i)
2460 bmap = var_equal(bmap, i);
2462 bmap = var_less(bmap, pos);
2463 return isl_basic_map_finalize(bmap);
2466 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2468 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2471 struct isl_basic_map *bmap;
2472 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2475 for (i = 0; i < pos && bmap; ++i)
2476 bmap = var_equal(bmap, i);
2478 bmap = var_more(bmap, pos);
2479 return isl_basic_map_finalize(bmap);
2482 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
2484 struct isl_map *map;
2491 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2493 for (i = 0; i < dim; ++i)
2494 map = isl_map_add(map,
2495 isl_basic_map_less_at(isl_dim_copy(dims), i));
2497 map = isl_map_add(map,
2498 isl_basic_map_equal(isl_dim_copy(dims), dim));
2504 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
2506 return map_lex_lte(isl_dim_map(set_dim), 0);
2509 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
2511 return map_lex_lte(isl_dim_map(set_dim), 1);
2514 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
2516 struct isl_map *map;
2523 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2525 for (i = 0; i < dim; ++i)
2526 map = isl_map_add(map,
2527 isl_basic_map_more_at(isl_dim_copy(dims), i));
2529 map = isl_map_add(map,
2530 isl_basic_map_equal(isl_dim_copy(dims), dim));
2536 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
2538 return map_lex_gte(isl_dim_map(set_dim), 0);
2541 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
2543 return map_lex_gte(isl_dim_map(set_dim), 1);
2546 struct isl_basic_map *isl_basic_map_from_basic_set(
2547 struct isl_basic_set *bset, struct isl_dim *dim)
2549 struct isl_basic_map *bmap;
2551 bset = isl_basic_set_cow(bset);
2555 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2556 isl_dim_free(bset->dim);
2557 bmap = (struct isl_basic_map *) bset;
2559 return isl_basic_map_finalize(bmap);
2561 isl_basic_set_free(bset);
2566 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2570 if (bmap->dim->n_in == 0)
2571 return (struct isl_basic_set *)bmap;
2572 bmap = isl_basic_map_cow(bmap);
2575 bmap->dim = isl_dim_cow(bmap->dim);
2578 bmap->dim->n_out += bmap->dim->n_in;
2579 bmap->dim->n_in = 0;
2580 bmap = isl_basic_map_finalize(bmap);
2581 return (struct isl_basic_set *)bmap;
2583 isl_basic_map_free(bmap);
2587 /* For a div d = floor(f/m), add the constraints
2590 * -(f-(n-1)) + m d >= 0
2592 * Note that the second constraint is the negation of
2596 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2599 unsigned total = isl_basic_map_total_dim(bmap);
2600 unsigned div_pos = 1 + total - bmap->n_div + div;
2602 i = isl_basic_map_alloc_inequality(bmap);
2605 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2606 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2608 j = isl_basic_map_alloc_inequality(bmap);
2611 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2612 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2613 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2617 struct isl_basic_set *isl_basic_map_underlying_set(
2618 struct isl_basic_map *bmap)
2622 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2623 return (struct isl_basic_set *)bmap;
2624 bmap = isl_basic_map_cow(bmap);
2627 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2630 bmap->extra -= bmap->n_div;
2632 bmap = isl_basic_map_finalize(bmap);
2633 return (struct isl_basic_set *)bmap;
2638 __isl_give isl_basic_set *isl_basic_set_underlying_set(
2639 __isl_take isl_basic_set *bset)
2641 return isl_basic_map_underlying_set((isl_basic_map *)bset);
2644 struct isl_basic_map *isl_basic_map_overlying_set(
2645 struct isl_basic_set *bset, struct isl_basic_map *like)
2647 struct isl_basic_map *bmap;
2648 struct isl_ctx *ctx;
2655 isl_assert(ctx, bset->n_div == 0, goto error);
2656 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2657 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2659 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2660 isl_basic_map_free(like);
2661 return (struct isl_basic_map *)bset;
2663 bset = isl_basic_set_cow(bset);
2666 total = bset->dim->n_out + bset->extra;
2667 bmap = (struct isl_basic_map *)bset;
2668 isl_dim_free(bmap->dim);
2669 bmap->dim = isl_dim_copy(like->dim);
2672 bmap->n_div = like->n_div;
2673 bmap->extra += like->n_div;
2676 ltotal = total - bmap->extra + like->extra;
2679 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2680 bmap->extra * (1 + 1 + total));
2681 if (isl_blk_is_error(bmap->block2))
2683 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2687 for (i = 0; i < bmap->extra; ++i)
2688 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2689 for (i = 0; i < like->n_div; ++i) {
2690 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2691 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2693 bmap = isl_basic_map_extend_constraints(bmap,
2694 0, 2 * like->n_div);
2695 for (i = 0; i < like->n_div; ++i) {
2696 if (isl_int_is_zero(bmap->div[i][0]))
2698 if (add_div_constraints(bmap, i) < 0)
2702 isl_basic_map_free(like);
2703 bmap = isl_basic_map_simplify(bmap);
2704 bmap = isl_basic_map_finalize(bmap);
2707 isl_basic_map_free(like);
2708 isl_basic_set_free(bset);
2712 struct isl_basic_set *isl_basic_set_from_underlying_set(
2713 struct isl_basic_set *bset, struct isl_basic_set *like)
2715 return (struct isl_basic_set *)
2716 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2719 struct isl_set *isl_set_from_underlying_set(
2720 struct isl_set *set, struct isl_basic_set *like)
2726 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2728 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2729 isl_basic_set_free(like);
2732 set = isl_set_cow(set);
2735 for (i = 0; i < set->n; ++i) {
2736 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2737 isl_basic_set_copy(like));
2741 isl_dim_free(set->dim);
2742 set->dim = isl_dim_copy(like->dim);
2745 isl_basic_set_free(like);
2748 isl_basic_set_free(like);
2753 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2757 map = isl_map_cow(map);
2760 map->dim = isl_dim_cow(map->dim);
2764 for (i = 1; i < map->n; ++i)
2765 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2767 for (i = 0; i < map->n; ++i) {
2768 map->p[i] = (struct isl_basic_map *)
2769 isl_basic_map_underlying_set(map->p[i]);
2774 map->dim = isl_dim_underlying(map->dim, 0);
2776 isl_dim_free(map->dim);
2777 map->dim = isl_dim_copy(map->p[0]->dim);
2781 return (struct isl_set *)map;
2787 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2789 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2792 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2794 struct isl_basic_set *domain;
2799 n_in = isl_basic_map_n_in(bmap);
2800 n_out = isl_basic_map_n_out(bmap);
2801 domain = isl_basic_set_from_basic_map(bmap);
2802 return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
2805 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2807 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2810 struct isl_set *isl_map_range(struct isl_map *map)
2813 struct isl_set *set;
2817 map = isl_map_cow(map);
2821 set = (struct isl_set *) map;
2822 if (set->dim->n_in != 0) {
2823 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2827 for (i = 0; i < map->n; ++i) {
2828 set->p[i] = isl_basic_map_range(map->p[i]);
2832 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2833 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2840 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2843 struct isl_map *map = NULL;
2845 set = isl_set_cow(set);
2848 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2849 map = (struct isl_map *)set;
2850 for (i = 0; i < set->n; ++i) {
2851 map->p[i] = isl_basic_map_from_basic_set(
2852 set->p[i], isl_dim_copy(dim));
2856 isl_dim_free(map->dim);
2865 struct isl_map *isl_map_from_range(struct isl_set *set)
2867 return (struct isl_map *)set;
2870 struct isl_set *isl_set_from_map(struct isl_map *map)
2873 struct isl_set *set = NULL;
2877 map = isl_map_cow(map);
2880 map->dim = isl_dim_cow(map->dim);
2883 map->dim->n_out += map->dim->n_in;
2885 set = (struct isl_set *)map;
2886 for (i = 0; i < map->n; ++i) {
2887 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2897 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2899 struct isl_map *map;
2903 isl_assert(dim->ctx, n >= 0, return NULL);
2904 map = isl_alloc(dim->ctx, struct isl_map,
2905 sizeof(struct isl_map) +
2906 (n - 1) * sizeof(struct isl_basic_map *));
2910 map->ctx = dim->ctx;
2911 isl_ctx_ref(map->ctx);
2923 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2924 unsigned nparam, unsigned in, unsigned out, int n,
2927 struct isl_map *map;
2928 struct isl_dim *dims;
2930 dims = isl_dim_alloc(ctx, nparam, in, out);
2934 map = isl_map_alloc_dim(dims, n, flags);
2938 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
2940 struct isl_basic_map *bmap;
2941 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
2942 bmap = isl_basic_map_set_to_empty(bmap);
2946 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2948 struct isl_basic_set *bset;
2949 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2950 bset = isl_basic_set_set_to_empty(bset);
2954 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2956 struct isl_basic_map *bmap;
2959 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2960 bmap = isl_basic_map_set_to_empty(bmap);
2964 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2966 struct isl_basic_map *bmap;
2969 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2970 bmap = isl_basic_map_set_to_empty(bmap);
2974 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2976 struct isl_basic_set *bset;
2979 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2980 bset = isl_basic_set_set_to_empty(bset);
2984 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2986 struct isl_basic_map *bmap;
2987 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2991 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2993 struct isl_basic_set *bset;
2994 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2998 __isl_give isl_basic_map *isl_basic_map_universe_like(
2999 __isl_keep isl_basic_map *model)
3003 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3006 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3010 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3013 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3014 __isl_keep isl_set *model)
3018 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3021 struct isl_map *isl_map_empty(struct isl_dim *dim)
3023 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3026 struct isl_map *isl_map_empty_like(struct isl_map *model)
3030 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3033 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
3037 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3040 struct isl_set *isl_set_empty(struct isl_dim *dim)
3042 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3045 struct isl_set *isl_set_empty_like(struct isl_set *model)
3049 return isl_set_empty(isl_dim_copy(model->dim));
3052 struct isl_map *isl_map_universe(struct isl_dim *dim)
3054 struct isl_map *map;
3057 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3058 map = isl_map_add(map, isl_basic_map_universe(dim));
3062 struct isl_set *isl_set_universe(struct isl_dim *dim)
3064 struct isl_set *set;
3067 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3068 set = isl_set_add(set, isl_basic_set_universe(dim));
3072 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3076 return isl_set_universe(isl_dim_copy(model->dim));
3079 struct isl_map *isl_map_dup(struct isl_map *map)
3082 struct isl_map *dup;
3086 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3087 for (i = 0; i < map->n; ++i)
3088 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
3092 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
3096 if (isl_basic_map_fast_is_empty(bmap)) {
3097 isl_basic_map_free(bmap);
3100 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3101 isl_assert(map->ctx, map->n < map->size, goto error);
3102 map->p[map->n] = bmap;
3104 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3110 isl_basic_map_free(bmap);
3114 void isl_map_free(struct isl_map *map)
3124 isl_ctx_deref(map->ctx);
3125 for (i = 0; i < map->n; ++i)
3126 isl_basic_map_free(map->p[i]);
3127 isl_dim_free(map->dim);
3131 struct isl_map *isl_map_extend(struct isl_map *base,
3132 unsigned nparam, unsigned n_in, unsigned n_out)
3136 base = isl_map_cow(base);
3140 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3143 for (i = 0; i < base->n; ++i) {
3144 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3145 isl_dim_copy(base->dim), 0, 0, 0);
3155 struct isl_set *isl_set_extend(struct isl_set *base,
3156 unsigned nparam, unsigned dim)
3158 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3162 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3163 struct isl_basic_map *bmap, unsigned pos, int value)
3167 bmap = isl_basic_map_cow(bmap);
3168 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3169 j = isl_basic_map_alloc_equality(bmap);
3172 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3173 isl_int_set_si(bmap->eq[j][pos], -1);
3174 isl_int_set_si(bmap->eq[j][0], value);
3175 bmap = isl_basic_map_simplify(bmap);
3176 return isl_basic_map_finalize(bmap);
3178 isl_basic_map_free(bmap);
3182 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3183 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3187 bmap = isl_basic_map_cow(bmap);
3188 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3189 j = isl_basic_map_alloc_equality(bmap);
3192 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3193 isl_int_set_si(bmap->eq[j][pos], -1);
3194 isl_int_set(bmap->eq[j][0], value);
3195 bmap = isl_basic_map_simplify(bmap);
3196 return isl_basic_map_finalize(bmap);
3198 isl_basic_map_free(bmap);
3202 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3203 enum isl_dim_type type, unsigned pos, int value)
3207 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3208 return isl_basic_map_fix_pos_si(bmap,
3209 isl_basic_map_offset(bmap, type) + pos, value);
3211 isl_basic_map_free(bmap);
3215 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3216 enum isl_dim_type type, unsigned pos, isl_int value)
3220 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3221 return isl_basic_map_fix_pos(bmap,
3222 isl_basic_map_offset(bmap, type) + pos, value);
3224 isl_basic_map_free(bmap);
3228 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3229 enum isl_dim_type type, unsigned pos, int value)
3231 return (struct isl_basic_set *)
3232 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3236 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3237 enum isl_dim_type type, unsigned pos, isl_int value)
3239 return (struct isl_basic_set *)
3240 isl_basic_map_fix((struct isl_basic_map *)bset,
3244 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3245 unsigned input, int value)
3247 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3250 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3251 unsigned dim, int value)
3253 return (struct isl_basic_set *)
3254 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3255 isl_dim_set, dim, value);
3258 struct isl_map *isl_map_fix_si(struct isl_map *map,
3259 enum isl_dim_type type, unsigned pos, int value)
3263 map = isl_map_cow(map);
3267 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3268 for (i = 0; i < map->n; ++i) {
3269 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3273 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3280 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
3281 enum isl_dim_type type, unsigned pos, isl_int value)
3285 map = isl_map_cow(map);
3289 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3290 for (i = 0; i < map->n; ++i) {
3291 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
3295 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3302 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
3303 enum isl_dim_type type, unsigned pos, isl_int value)
3305 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
3308 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3309 unsigned input, int value)
3311 return isl_map_fix_si(map, isl_dim_in, input, value);
3314 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3316 return (struct isl_set *)
3317 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3320 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3321 unsigned dim, isl_int value)
3325 bset = isl_basic_set_cow(bset);
3326 bset = isl_basic_set_extend_constraints(bset, 0, 1);
3327 j = isl_basic_set_alloc_inequality(bset);
3330 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3331 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3332 isl_int_neg(bset->ineq[j][0], value);
3333 bset = isl_basic_set_simplify(bset);
3334 return isl_basic_set_finalize(bset);
3336 isl_basic_set_free(bset);
3340 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3345 set = isl_set_cow(set);
3349 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3350 for (i = 0; i < set->n; ++i) {
3351 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3361 struct isl_map *isl_map_reverse(struct isl_map *map)
3365 map = isl_map_cow(map);
3369 map->dim = isl_dim_reverse(map->dim);
3372 for (i = 0; i < map->n; ++i) {
3373 map->p[i] = isl_basic_map_reverse(map->p[i]);
3377 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3384 static struct isl_map *isl_basic_map_partial_lexopt(
3385 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3386 struct isl_set **empty, int max)
3390 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
3391 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3393 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3395 isl_basic_map_free(bmap);
3396 isl_basic_set_free(dom);
3402 struct isl_map *isl_basic_map_partial_lexmax(
3403 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3404 struct isl_set **empty)
3406 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
3409 struct isl_map *isl_basic_map_partial_lexmin(
3410 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3411 struct isl_set **empty)
3413 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3416 struct isl_set *isl_basic_set_partial_lexmin(
3417 struct isl_basic_set *bset, struct isl_basic_set *dom,
3418 struct isl_set **empty)
3420 return (struct isl_set *)
3421 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3425 struct isl_set *isl_basic_set_partial_lexmax(
3426 struct isl_basic_set *bset, struct isl_basic_set *dom,
3427 struct isl_set **empty)
3429 return (struct isl_set *)
3430 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3434 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
3436 struct isl_basic_set *dom = NULL;
3437 struct isl_dim *dom_dim;
3441 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3442 dom = isl_basic_set_universe(dom_dim);
3443 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
3445 isl_basic_map_free(bmap);
3449 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
3451 return isl_basic_map_lexopt(bmap, 0);
3454 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
3456 return isl_basic_map_lexopt(bmap, 1);
3459 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
3461 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
3464 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
3466 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
3469 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
3470 struct isl_dim *dim)
3477 for (i = 0; i < map->n; ++i) {
3478 isl_dim_free(map->p[i]->dim);
3479 map->p[i]->dim = isl_dim_copy(dim);
3481 isl_dim_free(map->dim);
3491 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
3492 struct isl_dim *dim)
3494 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3497 /* Apply a preimage specified by "mat" on the parameters of "bset".
3498 * bset is assumed to have only parameters and divs.
3500 static struct isl_basic_set *basic_set_parameter_preimage(
3501 struct isl_basic_set *bset, struct isl_mat *mat)
3508 bset->dim = isl_dim_cow(bset->dim);
3512 nparam = isl_basic_set_dim(bset, isl_dim_param);
3514 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
3516 bset->dim->nparam = 0;
3517 bset->dim->n_out = nparam;
3518 bset = isl_basic_set_preimage(bset, mat);
3520 bset->dim->nparam = bset->dim->n_out;
3521 bset->dim->n_out = 0;
3526 isl_basic_set_free(bset);
3530 /* Apply a preimage specified by "mat" on the parameters of "set".
3531 * set is assumed to have only parameters and divs.
3533 static struct isl_set *set_parameter_preimage(
3534 struct isl_set *set, struct isl_mat *mat)
3536 struct isl_dim *dim = NULL;
3542 dim = isl_dim_copy(set->dim);
3543 dim = isl_dim_cow(dim);
3547 nparam = isl_set_dim(set, isl_dim_param);
3549 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
3552 dim->n_out = nparam;
3553 isl_set_reset_dim(set, dim);
3554 set = isl_set_preimage(set, mat);
3557 dim = isl_dim_copy(set->dim);
3558 dim = isl_dim_cow(dim);
3561 dim->nparam = dim->n_out;
3563 isl_set_reset_dim(set, dim);
3573 /* Intersect the basic set "bset" with the affine space specified by the
3574 * equalities in "eq".
3576 static struct isl_basic_set *basic_set_append_equalities(
3577 struct isl_basic_set *bset, struct isl_mat *eq)
3585 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
3590 len = 1 + isl_dim_total(bset->dim) + bset->extra;
3591 for (i = 0; i < eq->n_row; ++i) {
3592 k = isl_basic_set_alloc_equality(bset);
3595 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
3596 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
3603 isl_basic_set_free(bset);
3607 /* Intersect the set "set" with the affine space specified by the
3608 * equalities in "eq".
3610 static struct isl_set *set_append_equalities(struct isl_set *set,
3618 for (i = 0; i < set->n; ++i) {
3619 set->p[i] = basic_set_append_equalities(set->p[i],
3632 /* Project the given basic set onto its parameter domain, possibly introducing
3633 * new, explicit, existential variables in the constraints.
3634 * The input has parameters and (possibly implicit) existential variables.
3635 * The output has the same parameters, but only
3636 * explicit existentially quantified variables.
3638 * The actual projection is performed by pip, but pip doesn't seem
3639 * to like equalities very much, so we first remove the equalities
3640 * among the parameters by performing a variable compression on
3641 * the parameters. Afterward, an inverse transformation is performed
3642 * and the equalities among the parameters are inserted back in.
3644 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
3648 struct isl_mat *T, *T2;
3649 struct isl_set *set;
3650 unsigned nparam, n_div;
3652 bset = isl_basic_set_cow(bset);
3656 if (bset->n_eq == 0)
3657 return isl_basic_set_lexmin(bset);
3659 isl_basic_set_gauss(bset, NULL);
3661 nparam = isl_basic_set_dim(bset, isl_dim_param);
3662 n_div = isl_basic_set_dim(bset, isl_dim_div);
3664 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
3665 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
3668 if (i == bset->n_eq)
3669 return isl_basic_set_lexmin(bset);
3671 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
3673 eq = isl_mat_cow(eq);
3674 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
3675 bset = basic_set_parameter_preimage(bset, T);
3677 set = isl_basic_set_lexmin(bset);
3678 set = set_parameter_preimage(set, T2);
3679 set = set_append_equalities(set, eq);
3683 /* Compute an explicit representation for all the existentially
3684 * quantified variables.
3685 * The input and output dimensions are first turned into parameters.
3686 * compute_divs then returns a map with the same parameters and
3687 * no input or output dimensions and the dimension specification
3688 * is reset to that of the input.
3690 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
3692 struct isl_basic_set *bset;
3693 struct isl_set *set;
3694 struct isl_map *map;
3695 struct isl_dim *dim, *orig_dim = NULL;
3700 bmap = isl_basic_map_cow(bmap);
3704 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3705 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3706 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3707 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
3711 orig_dim = bmap->dim;
3713 bset = (struct isl_basic_set *)bmap;
3715 set = parameter_compute_divs(bset);
3716 map = (struct isl_map *)set;
3717 map = isl_map_reset_dim(map, orig_dim);
3721 isl_basic_map_free(bmap);
3725 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
3733 off = isl_dim_total(bmap->dim);
3734 for (i = 0; i < bmap->n_div; ++i) {
3735 if (isl_int_is_zero(bmap->div[i][0]))
3737 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
3743 static int map_divs_known(__isl_keep isl_map *map)
3750 for (i = 0; i < map->n; ++i) {
3751 int known = basic_map_divs_known(map->p[i]);
3759 /* If bmap contains any unknown divs, then compute explicit
3760 * expressions for them. However, this computation may be
3761 * quite expensive, so first try to remove divs that aren't
3764 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
3768 struct isl_map *map;
3770 known = basic_map_divs_known(bmap);
3774 return isl_map_from_basic_map(bmap);
3776 bmap = isl_basic_map_drop_redundant_divs(bmap);
3778 known = basic_map_divs_known(bmap);
3782 return isl_map_from_basic_map(bmap);
3784 map = compute_divs(bmap);
3787 isl_basic_map_free(bmap);
3791 struct isl_map *isl_map_compute_divs(struct isl_map *map)
3795 struct isl_map *res;
3802 known = map_divs_known(map);
3810 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
3811 for (i = 1 ; i < map->n; ++i) {
3813 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
3814 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3815 res = isl_map_union_disjoint(res, r2);
3817 res = isl_map_union(res, r2);
3824 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3826 return (struct isl_set *)
3827 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3830 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3832 return (struct isl_set *)
3833 isl_map_compute_divs((struct isl_map *)set);
3836 struct isl_set *isl_map_domain(struct isl_map *map)
3839 struct isl_set *set;
3844 map = isl_map_cow(map);
3848 set = (struct isl_set *)map;
3849 set->dim = isl_dim_domain(set->dim);
3852 for (i = 0; i < map->n; ++i) {
3853 set->p[i] = isl_basic_map_domain(map->p[i]);
3857 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3858 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3865 struct isl_map *isl_map_union_disjoint(
3866 struct isl_map *map1, struct isl_map *map2)
3870 struct isl_map *map = NULL;
3884 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3886 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3887 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3888 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3890 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3891 map1->n + map2->n, flags);
3894 for (i = 0; i < map1->n; ++i) {
3895 map = isl_map_add(map,
3896 isl_basic_map_copy(map1->p[i]));
3900 for (i = 0; i < map2->n; ++i) {
3901 map = isl_map_add(map,
3902 isl_basic_map_copy(map2->p[i]));
3916 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3918 map1 = isl_map_union_disjoint(map1, map2);
3922 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3926 struct isl_set *isl_set_union_disjoint(
3927 struct isl_set *set1, struct isl_set *set2)
3929 return (struct isl_set *)
3930 isl_map_union_disjoint(
3931 (struct isl_map *)set1, (struct isl_map *)set2);
3934 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3936 return (struct isl_set *)
3937 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3940 struct isl_map *isl_map_intersect_range(
3941 struct isl_map *map, struct isl_set *set)
3944 struct isl_map *result;
3950 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3951 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3952 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3954 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3955 map->n * set->n, flags);
3958 for (i = 0; i < map->n; ++i)
3959 for (j = 0; j < set->n; ++j) {
3960 result = isl_map_add(result,
3961 isl_basic_map_intersect_range(
3962 isl_basic_map_copy(map->p[i]),
3963 isl_basic_set_copy(set->p[j])));
3976 struct isl_map *isl_map_intersect_domain(
3977 struct isl_map *map, struct isl_set *set)
3979 return isl_map_reverse(
3980 isl_map_intersect_range(isl_map_reverse(map), set));
3983 struct isl_map *isl_map_apply_domain(
3984 struct isl_map *map1, struct isl_map *map2)
3988 map1 = isl_map_reverse(map1);
3989 map1 = isl_map_apply_range(map1, map2);
3990 return isl_map_reverse(map1);
3997 struct isl_map *isl_map_apply_range(
3998 struct isl_map *map1, struct isl_map *map2)
4000 struct isl_dim *dim_result;
4001 struct isl_map *result;
4007 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
4008 isl_dim_copy(map2->dim));
4010 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
4013 for (i = 0; i < map1->n; ++i)
4014 for (j = 0; j < map2->n; ++j) {
4015 result = isl_map_add(result,
4016 isl_basic_map_apply_range(
4017 isl_basic_map_copy(map1->p[i]),
4018 isl_basic_map_copy(map2->p[j])));
4024 if (result && result->n <= 1)
4025 ISL_F_SET(result, ISL_MAP_DISJOINT);
4034 * returns range - domain
4036 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
4038 struct isl_basic_set *bset;
4045 dim = isl_basic_map_n_in(bmap);
4046 nparam = isl_basic_map_n_param(bmap);
4047 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
4048 bset = isl_basic_set_from_basic_map(bmap);
4049 bset = isl_basic_set_cow(bset);
4050 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
4051 bset = isl_basic_set_swap_vars(bset, 2*dim);
4052 for (i = 0; i < dim; ++i) {
4053 int j = isl_basic_map_alloc_equality(
4054 (struct isl_basic_map *)bset);
4057 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
4058 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
4059 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
4060 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
4062 return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
4064 isl_basic_map_free(bmap);
4069 * returns range - domain
4071 struct isl_set *isl_map_deltas(struct isl_map *map)
4074 struct isl_set *result;
4079 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
4080 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
4081 isl_map_n_in(map), map->n, map->flags);
4084 for (i = 0; i < map->n; ++i)
4085 result = isl_set_add(result,
4086 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
4094 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
4096 struct isl_basic_map *bmap;
4104 nparam = dims->nparam;
4106 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
4110 for (i = 0; i < dim; ++i) {
4111 int j = isl_basic_map_alloc_equality(bmap);
4114 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
4115 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
4116 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
4118 return isl_basic_map_finalize(bmap);
4120 isl_basic_map_free(bmap);
4124 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
4126 struct isl_dim *dim = isl_dim_map(set_dim);
4129 return basic_map_identity(dim);
4132 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
4134 if (!model || !model->dim)
4136 isl_assert(model->ctx,
4137 model->dim->n_in == model->dim->n_out, return NULL);
4138 return basic_map_identity(isl_dim_copy(model->dim));
4141 static struct isl_map *map_identity(struct isl_dim *dim)
4143 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
4144 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
4147 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
4149 struct isl_dim *dim = isl_dim_map(set_dim);
4152 return map_identity(dim);
4155 struct isl_map *isl_map_identity_like(struct isl_map *model)
4157 if (!model || !model->dim)
4159 isl_assert(model->ctx,
4160 model->dim->n_in == model->dim->n_out, return NULL);
4161 return map_identity(isl_dim_copy(model->dim));
4164 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
4166 if (!model || !model->dim)
4168 isl_assert(model->ctx,
4169 model->dim->n_in == model->dim->n_out, return NULL);
4170 return map_identity(isl_dim_copy(model->dim));
4173 /* Construct a basic set with all set dimensions having only non-negative
4176 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
4181 struct isl_basic_set *bset;
4185 nparam = dims->nparam;
4187 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
4190 for (i = 0; i < dim; ++i) {
4191 int k = isl_basic_set_alloc_inequality(bset);
4194 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
4195 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
4199 isl_basic_set_free(bset);
4203 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
4205 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
4208 int isl_basic_map_is_subset(
4209 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4212 struct isl_map *map1;
4213 struct isl_map *map2;
4215 if (!bmap1 || !bmap2)
4218 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
4219 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
4221 is_subset = isl_map_is_subset(map1, map2);
4229 int isl_basic_map_is_equal(
4230 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4234 if (!bmap1 || !bmap2)
4236 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4239 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4243 int isl_basic_set_is_equal(
4244 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4246 return isl_basic_map_is_equal(
4247 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
4250 int isl_map_is_empty(struct isl_map *map)
4257 for (i = 0; i < map->n; ++i) {
4258 is_empty = isl_basic_map_is_empty(map->p[i]);
4267 int isl_map_fast_is_empty(struct isl_map *map)
4272 int isl_set_fast_is_empty(struct isl_set *set)
4277 int isl_set_is_empty(struct isl_set *set)
4279 return isl_map_is_empty((struct isl_map *)set);
4282 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
4288 is_subset = isl_map_is_subset(map1, map2);
4291 is_subset = isl_map_is_subset(map2, map1);
4295 int isl_basic_map_is_strict_subset(
4296 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4300 if (!bmap1 || !bmap2)
4302 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4305 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4306 if (is_subset == -1)
4311 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
4317 is_subset = isl_map_is_subset(map1, map2);
4320 is_subset = isl_map_is_subset(map2, map1);
4321 if (is_subset == -1)
4326 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4328 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
4331 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
4335 return bmap->n_eq == 0 && bmap->n_ineq == 0;
4338 int isl_basic_set_is_universe(struct isl_basic_set *bset)
4342 return bset->n_eq == 0 && bset->n_ineq == 0;
4345 int isl_map_fast_is_universe(__isl_keep isl_map *map)
4350 return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
4353 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
4355 struct isl_basic_set *bset = NULL;
4356 struct isl_vec *sample = NULL;
4363 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4366 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
4367 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
4368 copy = isl_basic_map_convex_hull(copy);
4369 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
4370 isl_basic_map_free(copy);
4374 total = 1 + isl_basic_map_total_dim(bmap);
4375 if (bmap->sample && bmap->sample->size == total) {
4376 int contains = isl_basic_map_contains(bmap, bmap->sample);
4382 isl_vec_free(bmap->sample);
4383 bmap->sample = NULL;
4384 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
4387 sample = isl_basic_set_sample_vec(bset);
4390 empty = sample->size == 0;
4391 isl_vec_free(bmap->sample);
4392 bmap->sample = sample;
4394 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
4399 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
4403 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
4406 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
4410 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
4413 int isl_basic_set_is_empty(struct isl_basic_set *bset)
4415 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
4418 struct isl_map *isl_basic_map_union(
4419 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4421 struct isl_map *map;
4422 if (!bmap1 || !bmap2)
4425 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
4427 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
4430 map = isl_map_add(map, bmap1);
4431 map = isl_map_add(map, bmap2);
4434 isl_basic_map_free(bmap1);
4435 isl_basic_map_free(bmap2);
4439 struct isl_set *isl_basic_set_union(
4440 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4442 return (struct isl_set *)isl_basic_map_union(
4443 (struct isl_basic_map *)bset1,
4444 (struct isl_basic_map *)bset2);
4447 /* Order divs such that any div only depends on previous divs */
4448 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
4451 unsigned off = isl_dim_total(bmap->dim);
4453 for (i = 0; i < bmap->n_div; ++i) {
4455 if (isl_int_is_zero(bmap->div[i][0]))
4457 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
4461 isl_basic_map_swap_div(bmap, i, i + pos);
4467 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
4469 return (struct isl_basic_set *)
4470 isl_basic_map_order_divs((struct isl_basic_map *)bset);
4473 /* Look for a div in dst that corresponds to the div "div" in src.
4474 * The divs before "div" in src and dst are assumed to be the same.
4476 * Returns -1 if no corresponding div was found and the position
4477 * of the corresponding div in dst otherwise.
4479 static int find_div(struct isl_basic_map *dst,
4480 struct isl_basic_map *src, unsigned div)
4484 unsigned total = isl_dim_total(src->dim);
4486 isl_assert(dst->ctx, div <= dst->n_div, return -1);
4487 for (i = div; i < dst->n_div; ++i)
4488 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
4489 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
4490 dst->n_div - div) == -1)
4495 struct isl_basic_map *isl_basic_map_align_divs(
4496 struct isl_basic_map *dst, struct isl_basic_map *src)
4499 unsigned total = isl_dim_total(src->dim);
4504 if (src->n_div == 0)
4507 for (i = 0; i < src->n_div; ++i)
4508 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
4510 src = isl_basic_map_order_divs(src);
4511 dst = isl_basic_map_cow(dst);
4512 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
4513 src->n_div, 0, 2 * src->n_div);
4516 for (i = 0; i < src->n_div; ++i) {
4517 int j = find_div(dst, src, i);
4519 j = isl_basic_map_alloc_div(dst);
4522 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
4523 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
4524 if (add_div_constraints(dst, j) < 0)
4528 isl_basic_map_swap_div(dst, i, j);
4532 isl_basic_map_free(dst);
4536 struct isl_basic_set *isl_basic_set_align_divs(
4537 struct isl_basic_set *dst, struct isl_basic_set *src)
4539 return (struct isl_basic_set *)isl_basic_map_align_divs(
4540 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
4543 struct isl_map *isl_map_align_divs(struct isl_map *map)
4551 map = isl_map_compute_divs(map);
4552 map = isl_map_cow(map);
4556 for (i = 1; i < map->n; ++i)
4557 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
4558 for (i = 1; i < map->n; ++i)
4559 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
4561 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4565 struct isl_set *isl_set_align_divs(struct isl_set *set)
4567 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
4570 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
4574 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
4575 map = isl_map_intersect_domain(map, set);
4576 set = isl_map_range(map);
4584 /* There is no need to cow as removing empty parts doesn't change
4585 * the meaning of the set.
4587 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
4594 for (i = map->n-1; i >= 0; --i) {
4595 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
4597 isl_basic_map_free(map->p[i]);
4598 if (i != map->n-1) {
4599 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4600 map->p[i] = map->p[map->n-1];
4608 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
4610 return (struct isl_set *)
4611 isl_map_remove_empty_parts((struct isl_map *)set);
4614 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
4616 struct isl_basic_map *bmap;
4617 if (!map || map->n == 0)
4619 bmap = map->p[map->n-1];
4620 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
4621 return isl_basic_map_copy(bmap);
4624 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
4626 return (struct isl_basic_set *)
4627 isl_map_copy_basic_map((struct isl_map *)set);
4630 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
4631 __isl_keep isl_basic_map *bmap)
4637 for (i = map->n-1; i >= 0; --i) {
4638 if (map->p[i] != bmap)
4640 map = isl_map_cow(map);
4643 isl_basic_map_free(map->p[i]);
4644 if (i != map->n-1) {
4645 ISL_F_CLR(map, ISL_SET_NORMALIZED);
4646 map->p[i] = map->p[map->n-1];
4657 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4658 struct isl_basic_set *bset)
4660 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
4661 (struct isl_basic_map *)bset);
4664 /* Given two basic sets bset1 and bset2, compute the maximal difference
4665 * between the values of dimension pos in bset1 and those in bset2
4666 * for any common value of the parameters and dimensions preceding pos.
4668 static enum isl_lp_result basic_set_maximal_difference_at(
4669 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
4670 int pos, isl_int *opt)
4672 struct isl_dim *dims;
4673 struct isl_basic_map *bmap1 = NULL;
4674 struct isl_basic_map *bmap2 = NULL;
4675 struct isl_ctx *ctx;
4676 struct isl_vec *obj;
4679 unsigned dim1, dim2;
4680 enum isl_lp_result res;
4682 if (!bset1 || !bset2)
4683 return isl_lp_error;
4685 nparam = isl_basic_set_n_param(bset1);
4686 dim1 = isl_basic_set_n_dim(bset1);
4687 dim2 = isl_basic_set_n_dim(bset2);
4688 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
4689 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
4690 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
4691 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
4692 if (!bmap1 || !bmap2)
4694 bmap1 = isl_basic_map_cow(bmap1);
4695 bmap1 = isl_basic_map_extend(bmap1, nparam,
4696 pos, (dim1 - pos) + (dim2 - pos),
4697 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4698 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
4701 total = isl_basic_map_total_dim(bmap1);
4703 obj = isl_vec_alloc(ctx, 1 + total);
4704 isl_seq_clr(obj->block.data, 1 + total);
4705 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
4706 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
4709 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
4711 isl_basic_map_free(bmap1);
4715 isl_basic_map_free(bmap1);
4716 isl_basic_map_free(bmap2);
4717 return isl_lp_error;
4720 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4721 * for any common value of the parameters and dimensions preceding pos
4722 * in both basic sets, the values of dimension pos in bset1 are
4723 * smaller or larger than those in bset2.
4726 * 1 if bset1 follows bset2
4727 * -1 if bset1 precedes bset2
4728 * 0 if bset1 and bset2 are incomparable
4729 * -2 if some error occurred.
4731 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4732 struct isl_basic_set *bset2, int pos)
4735 enum isl_lp_result res;
4740 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
4742 if (res == isl_lp_empty)
4744 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
4745 res == isl_lp_unbounded)
4747 else if (res == isl_lp_ok && isl_int_is_neg(opt))
4756 /* Given two basic sets bset1 and bset2, check whether
4757 * for any common value of the parameters and dimensions preceding pos
4758 * there is a value of dimension pos in bset1 that is larger
4759 * than a value of the same dimension in bset2.
4762 * 1 if there exists such a pair
4763 * 0 if there is no such pair, but there is a pair of equal values
4765 * -2 if some error occurred.
4767 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
4768 __isl_keep isl_basic_set *bset2, int pos)
4771 enum isl_lp_result res;
4776 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
4778 if (res == isl_lp_empty)
4780 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
4781 res == isl_lp_unbounded)
4783 else if (res == isl_lp_ok && isl_int_is_neg(opt))
4785 else if (res == isl_lp_ok)
4794 /* Given two sets set1 and set2, check whether
4795 * for any common value of the parameters and dimensions preceding pos
4796 * there is a value of dimension pos in set1 that is larger
4797 * than a value of the same dimension in set2.
4800 * 1 if there exists such a pair
4801 * 0 if there is no such pair, but there is a pair of equal values
4803 * -2 if some error occurred.
4805 int isl_set_follows_at(__isl_keep isl_set *set1,
4806 __isl_keep isl_set *set2, int pos)
4814 for (i = 0; i < set1->n; ++i)
4815 for (j = 0; j < set2->n; ++j) {
4817 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
4818 if (f == 1 || f == -2)
4827 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4828 unsigned pos, isl_int *val)
4836 total = isl_basic_map_total_dim(bmap);
4837 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4838 for (; d+1 > pos; --d)
4839 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4843 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4845 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4847 if (!isl_int_is_one(bmap->eq[i][1+d]))
4850 isl_int_neg(*val, bmap->eq[i][0]);
4856 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4857 unsigned pos, isl_int *val)
4869 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4872 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4873 for (i = 1; fixed == 1 && i < map->n; ++i) {
4874 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4875 if (fixed == 1 && isl_int_ne(tmp, v))
4879 isl_int_set(*val, v);
4885 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
4886 unsigned pos, isl_int *val)
4888 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
4892 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4895 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4898 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4899 enum isl_dim_type type, unsigned pos, isl_int *val)
4901 if (pos >= isl_basic_map_dim(bmap, type))
4903 return isl_basic_map_fast_has_fixed_var(bmap,
4904 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4907 int isl_map_fast_is_fixed(struct isl_map *map,
4908 enum isl_dim_type type, unsigned pos, isl_int *val)
4910 if (pos >= isl_map_dim(map, type))
4912 return isl_map_fast_has_fixed_var(map,
4913 map_offset(map, type) - 1 + pos, val);
4916 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4917 * then return this fixed value in *val.
4919 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
4922 return isl_basic_set_fast_has_fixed_var(bset,
4923 isl_basic_set_n_param(bset) + dim, val);
4926 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4927 * then return this fixed value in *val.
4929 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4931 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4934 /* Check if input variable in has fixed value and if so and if val is not NULL,
4935 * then return this fixed value in *val.
4937 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4939 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4942 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4943 * and if val is not NULL, then return this lower bound in *val.
4945 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4946 unsigned dim, isl_int *val)
4948 int i, i_eq = -1, i_ineq = -1;
4955 total = isl_basic_set_total_dim(bset);
4956 nparam = isl_basic_set_n_param(bset);
4957 for (i = 0; i < bset->n_eq; ++i) {
4958 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4964 for (i = 0; i < bset->n_ineq; ++i) {
4965 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4967 if (i_eq != -1 || i_ineq != -1)
4971 if (i_eq == -1 && i_ineq == -1)
4973 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4974 /* The coefficient should always be one due to normalization. */
4975 if (!isl_int_is_one(c[1+nparam+dim]))
4977 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4979 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4980 total - nparam - dim - 1) != -1)
4983 isl_int_neg(*val, c[0]);
4987 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4988 unsigned dim, isl_int *val)
5000 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5004 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5006 for (i = 1; fixed == 1 && i < set->n; ++i) {
5007 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
5009 if (fixed == 1 && isl_int_ne(tmp, v))
5013 isl_int_set(*val, v);
5024 static int qsort_constraint_cmp(const void *p1, const void *p2)
5026 const struct constraint *c1 = (const struct constraint *)p1;
5027 const struct constraint *c2 = (const struct constraint *)p2;
5028 unsigned size = isl_min(c1->size, c2->size);
5029 return isl_seq_cmp(c1->c, c2->c, size);
5032 static struct isl_basic_map *isl_basic_map_sort_constraints(
5033 struct isl_basic_map *bmap)
5036 struct constraint *c;
5041 total = isl_basic_map_total_dim(bmap);
5042 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
5045 for (i = 0; i < bmap->n_ineq; ++i) {
5047 c[i].c = bmap->ineq[i];
5049 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
5050 for (i = 0; i < bmap->n_ineq; ++i)
5051 bmap->ineq[i] = c[i].c;
5055 isl_basic_map_free(bmap);
5059 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
5063 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
5065 bmap = isl_basic_map_convex_hull(bmap);
5066 bmap = isl_basic_map_sort_constraints(bmap);
5067 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
5071 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
5073 return (struct isl_basic_set *)isl_basic_map_normalize(
5074 (struct isl_basic_map *)bset);
5077 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
5078 const struct isl_basic_map *bmap2)
5085 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
5086 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
5087 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
5088 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5089 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
5090 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5091 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
5092 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5094 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
5096 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5098 if (bmap1->n_eq != bmap2->n_eq)
5099 return bmap1->n_eq - bmap2->n_eq;
5100 if (bmap1->n_ineq != bmap2->n_ineq)
5101 return bmap1->n_ineq - bmap2->n_ineq;
5102 if (bmap1->n_div != bmap2->n_div)
5103 return bmap1->n_div - bmap2->n_div;
5104 total = isl_basic_map_total_dim(bmap1);
5105 for (i = 0; i < bmap1->n_eq; ++i) {
5106 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
5110 for (i = 0; i < bmap1->n_ineq; ++i) {
5111 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
5115 for (i = 0; i < bmap1->n_div; ++i) {
5116 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
5123 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
5124 struct isl_basic_map *bmap2)
5126 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
5129 static int qsort_bmap_cmp(const void *p1, const void *p2)
5131 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
5132 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
5134 return isl_basic_map_fast_cmp(bmap1, bmap2);
5137 /* We normalize in place, but if anything goes wrong we need
5138 * to return NULL, so we need to make sure we don't change the
5139 * meaning of any possible other copies of map.
5141 struct isl_map *isl_map_normalize(struct isl_map *map)
5144 struct isl_basic_map *bmap;
5148 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
5150 for (i = 0; i < map->n; ++i) {
5151 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
5154 isl_basic_map_free(map->p[i]);
5157 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
5158 ISL_F_SET(map, ISL_MAP_NORMALIZED);
5159 map = isl_map_remove_empty_parts(map);
5162 for (i = map->n - 1; i >= 1; --i) {
5163 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
5165 isl_basic_map_free(map->p[i-1]);
5166 for (j = i; j < map->n; ++j)
5167 map->p[j-1] = map->p[j];
5177 struct isl_set *isl_set_normalize(struct isl_set *set)
5179 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
5182 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
5192 if (!isl_dim_equal(map1->dim, map2->dim))
5195 map1 = isl_map_copy(map1);
5196 map2 = isl_map_copy(map2);
5197 map1 = isl_map_normalize(map1);
5198 map2 = isl_map_normalize(map2);
5201 equal = map1->n == map2->n;
5202 for (i = 0; equal && i < map1->n; ++i) {
5203 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
5216 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
5218 return isl_map_fast_is_equal((struct isl_map *)set1,
5219 (struct isl_map *)set2);
5222 /* Return an interval that ranges from min to max (inclusive)
5224 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
5225 isl_int min, isl_int max)
5228 struct isl_basic_set *bset = NULL;
5230 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
5234 k = isl_basic_set_alloc_inequality(bset);
5237 isl_int_set_si(bset->ineq[k][1], 1);
5238 isl_int_neg(bset->ineq[k][0], min);
5240 k = isl_basic_set_alloc_inequality(bset);
5243 isl_int_set_si(bset->ineq[k][1], -1);
5244 isl_int_set(bset->ineq[k][0], max);
5248 isl_basic_set_free(bset);
5252 /* Return the Cartesian product of the basic sets in list (in the given order).
5254 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
5262 struct isl_basic_set *product = NULL;
5266 isl_assert(list->ctx, list->n > 0, goto error);
5267 isl_assert(list->ctx, list->p[0], goto error);
5268 nparam = isl_basic_set_n_param(list->p[0]);
5269 dim = isl_basic_set_n_dim(list->p[0]);
5270 extra = list->p[0]->n_div;
5271 n_eq = list->p[0]->n_eq;
5272 n_ineq = list->p[0]->n_ineq;
5273 for (i = 1; i < list->n; ++i) {
5274 isl_assert(list->ctx, list->p[i], goto error);
5275 isl_assert(list->ctx,
5276 nparam == isl_basic_set_n_param(list->p[i]), goto error);
5277 dim += isl_basic_set_n_dim(list->p[i]);
5278 extra += list->p[i]->n_div;
5279 n_eq += list->p[i]->n_eq;
5280 n_ineq += list->p[i]->n_ineq;
5282 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
5287 for (i = 0; i < list->n; ++i) {
5288 isl_basic_set_add_constraints(product,
5289 isl_basic_set_copy(list->p[i]), dim);
5290 dim += isl_basic_set_n_dim(list->p[i]);
5292 isl_basic_set_list_free(list);
5295 isl_basic_set_free(product);
5296 isl_basic_set_list_free(list);
5300 struct isl_basic_map *isl_basic_map_product(
5301 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5303 struct isl_dim *dim_result = NULL;
5304 struct isl_basic_map *bmap;
5305 unsigned in1, in2, out1, out2, nparam, total, pos;
5306 struct isl_dim_map *dim_map1, *dim_map2;
5308 if (!bmap1 || !bmap2)
5311 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
5312 bmap2->dim, isl_dim_param), goto error);
5313 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
5314 isl_dim_copy(bmap2->dim));
5316 in1 = isl_basic_map_n_in(bmap1);
5317 in2 = isl_basic_map_n_in(bmap2);
5318 out1 = isl_basic_map_n_out(bmap1);
5319 out2 = isl_basic_map_n_out(bmap2);
5320 nparam = isl_basic_map_n_param(bmap1);
5322 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
5323 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5324 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
5325 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5326 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
5327 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5328 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
5329 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
5330 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
5331 isl_dim_map_div(dim_map1, bmap1, pos += out2);
5332 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5334 bmap = isl_basic_map_alloc_dim(dim_result,
5335 bmap1->n_div + bmap2->n_div,
5336 bmap1->n_eq + bmap2->n_eq,
5337 bmap1->n_ineq + bmap2->n_ineq);
5338 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
5339 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
5340 bmap = isl_basic_map_simplify(bmap);
5341 return isl_basic_map_finalize(bmap);
5343 isl_basic_map_free(bmap1);
5344 isl_basic_map_free(bmap2);
5348 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5350 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
5353 struct isl_map *result;
5359 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
5360 map2->dim, isl_dim_param), goto error);
5362 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5363 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5364 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5366 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
5367 isl_dim_copy(map2->dim)),
5368 map1->n * map2->n, flags);
5371 for (i = 0; i < map1->n; ++i)
5372 for (j = 0; j < map2->n; ++j) {
5373 struct isl_basic_map *part;
5374 part = isl_basic_map_product(
5375 isl_basic_map_copy(map1->p[i]),
5376 isl_basic_map_copy(map2->p[j]));
5377 if (isl_basic_map_is_empty(part))
5378 isl_basic_map_free(part);
5380 result = isl_map_add(result, part);
5393 /* Given two set A and B, construct its Cartesian product A x B.
5395 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
5397 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
5398 (struct isl_map *)set2);
5401 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
5404 uint32_t hash = isl_hash_init();
5409 bset = isl_basic_set_copy(bset);
5410 bset = isl_basic_set_normalize(bset);
5413 total = isl_basic_set_total_dim(bset);
5414 isl_hash_byte(hash, bset->n_eq & 0xFF);
5415 for (i = 0; i < bset->n_eq; ++i) {
5417 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
5418 isl_hash_hash(hash, c_hash);
5420 isl_hash_byte(hash, bset->n_ineq & 0xFF);
5421 for (i = 0; i < bset->n_ineq; ++i) {
5423 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
5424 isl_hash_hash(hash, c_hash);
5426 isl_hash_byte(hash, bset->n_div & 0xFF);
5427 for (i = 0; i < bset->n_div; ++i) {
5429 if (isl_int_is_zero(bset->div[i][0]))
5431 isl_hash_byte(hash, i & 0xFF);
5432 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
5433 isl_hash_hash(hash, c_hash);
5435 isl_basic_set_free(bset);
5439 uint32_t isl_set_get_hash(struct isl_set *set)
5446 set = isl_set_copy(set);
5447 set = isl_set_normalize(set);
5451 hash = isl_hash_init();
5452 for (i = 0; i < set->n; ++i) {
5454 bset_hash = isl_basic_set_get_hash(set->p[i]);
5455 isl_hash_hash(hash, bset_hash);
5463 /* Check if the value for dimension dim is completely determined
5464 * by the values of the other parameters and variables.
5465 * That is, check if dimension dim is involved in an equality.
5467 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
5474 nparam = isl_basic_set_n_param(bset);
5475 for (i = 0; i < bset->n_eq; ++i)
5476 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
5481 /* Check if the value for dimension dim is completely determined
5482 * by the values of the other parameters and variables.
5483 * That is, check if dimension dim is involved in an equality
5484 * for each of the subsets.
5486 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
5492 for (i = 0; i < set->n; ++i) {
5494 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
5501 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
5502 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
5509 for (i = 0; i < map->n; ++i)
5510 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
5516 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
5517 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
5524 for (i = 0; i < set->n; ++i)
5525 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
5531 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
5533 struct isl_dim *dim;
5534 struct isl_basic_map *bmap;
5541 set = isl_set_align_divs(set);
5546 dim = isl_set_get_dim(set);
5547 if (set->n == 0 || set->p[0]->n_div == 0) {
5549 return isl_map_identity(dim);
5552 n_div = set->p[0]->n_div;
5553 dim = isl_dim_map(dim);
5554 n_param = isl_dim_size(dim, isl_dim_param);
5555 n_set = isl_dim_size(dim, isl_dim_in);
5556 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
5557 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
5558 for (i = 0; i < n_set; ++i)
5559 bmap = var_equal(bmap, i);
5561 total = n_param + n_set + n_set + n_div;
5562 for (i = 0; i < n_div; ++i) {
5563 k = isl_basic_map_alloc_inequality(bmap);
5566 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
5567 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
5568 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
5569 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
5570 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
5571 set->p[0]->div[i][0]);
5573 l = isl_basic_map_alloc_inequality(bmap);
5576 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
5577 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
5578 set->p[0]->div[i][0]);
5579 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
5583 return isl_map_from_basic_map(bmap);
5586 isl_basic_map_free(bmap);
5590 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
5598 dim = isl_basic_set_total_dim(bset);
5599 size += bset->n_eq * (1 + dim);
5600 size += bset->n_ineq * (1 + dim);
5601 size += bset->n_div * (2 + dim);
5606 int isl_set_size(__isl_keep isl_set *set)
5614 for (i = 0; i < set->n; ++i)
5615 size += isl_basic_set_size(set->p[i]);