11 #include "isl_map_private.h"
12 #include "isl_map_piplib.h"
13 #include "isl_sample.h"
16 /* Maps dst positions to src positions */
22 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
25 struct isl_dim_map *dim_map;
26 dim_map = isl_alloc(ctx, struct isl_dim_map,
27 sizeof(struct isl_dim_map) + len * sizeof(int));
30 dim_map->len = 1 + len;
32 for (i = 0; i < len; ++i)
33 dim_map->pos[1 + i] = -1;
37 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
40 case isl_dim_param: return dim->nparam;
41 case isl_dim_in: return dim->n_in;
42 case isl_dim_out: return dim->n_out;
46 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
49 case isl_dim_param: return 1;
50 case isl_dim_in: return 1 + dim->nparam;
51 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
55 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
56 enum isl_dim_type type, unsigned dst_pos)
64 src_pos = pos(dim, type);
65 for (i = 0; i < n(dim, type); ++i)
66 dim_map->pos[1 + dst_pos + i] = src_pos + i;
69 static void isl_dim_map_div(struct isl_dim_map *dim_map,
70 struct isl_basic_map *bmap, unsigned dst_pos)
75 if (!dim_map || !bmap)
78 src_pos = 1 + isl_dim_total(bmap->dim);
79 for (i = 0; i < bmap->n_div; ++i)
80 dim_map->pos[1 + dst_pos + i] = src_pos + i;
83 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
87 for (i = 0; i < dim_map->len; ++i)
88 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
89 fprintf(stderr, "\n");
92 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
93 enum isl_dim_type type)
95 struct isl_dim *dim = bmap->dim;
99 case isl_dim_out: return isl_dim_size(bmap->dim, type);
100 case isl_dim_div: return bmap->n_div;
101 case isl_dim_all: return isl_basic_map_total_dim(bmap);
105 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
107 return n(map->dim, type);
110 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
112 return n(set->dim, type);
115 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
116 enum isl_dim_type type)
118 struct isl_dim *dim = bmap->dim;
120 case isl_dim_param: return 1;
121 case isl_dim_in: return 1 + dim->nparam;
122 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
123 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
127 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
129 return pos(map->dim, type);
132 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
133 enum isl_dim_type type)
135 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
138 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
140 return bset->dim->n_out;
143 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
145 return bset->dim->nparam;
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
150 return isl_dim_total(bset->dim) + bset->n_div;
153 unsigned isl_set_n_dim(const struct isl_set *set)
155 return set->dim->n_out;
158 unsigned isl_set_n_param(const struct isl_set *set)
160 return set->dim->nparam;
163 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
165 return bmap->dim->n_in;
168 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
170 return bmap->dim->n_out;
173 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
175 return bmap->dim->nparam;
178 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
183 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
185 return isl_dim_total(bmap->dim) + bmap->n_div;
188 unsigned isl_map_n_in(const struct isl_map *map)
190 return map->dim->n_in;
193 unsigned isl_map_n_out(const struct isl_map *map)
195 return map->dim->n_out;
198 unsigned isl_map_n_param(const struct isl_map *map)
200 return map->dim->nparam;
203 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
205 return map->dim->n_in == set->dim->n_out &&
206 map->dim->nparam == set->dim->nparam;
209 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
210 struct isl_basic_set *bset)
212 return bmap->dim->n_in == bset->dim->n_out &&
213 bmap->dim->nparam == bset->dim->nparam;
216 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
217 struct isl_basic_set *bset)
219 return bmap->dim->n_out == bset->dim->n_out &&
220 bmap->dim->nparam == bset->dim->nparam;
223 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
224 struct isl_basic_map *bmap, unsigned extra,
225 unsigned n_eq, unsigned n_ineq)
228 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
230 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
231 if (isl_blk_is_error(bmap->block)) {
236 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
238 isl_blk_free(ctx, bmap->block);
244 bmap->block2 = isl_blk_empty();
247 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
248 if (isl_blk_is_error(bmap->block2)) {
250 isl_blk_free(ctx, bmap->block);
255 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
257 isl_blk_free(ctx, bmap->block2);
259 isl_blk_free(ctx, bmap->block);
265 for (i = 0; i < n_ineq + n_eq; ++i)
266 bmap->ineq[i] = bmap->block.data + i * row_size;
268 for (i = 0; i < extra; ++i)
269 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
275 bmap->c_size = n_eq + n_ineq;
276 bmap->eq = bmap->ineq + n_ineq;
285 isl_basic_map_free(bmap);
289 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
290 unsigned nparam, unsigned dim, unsigned extra,
291 unsigned n_eq, unsigned n_ineq)
293 struct isl_basic_map *bmap;
294 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
295 return (struct isl_basic_set *)bmap;
298 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
299 unsigned extra, unsigned n_eq, unsigned n_ineq)
301 struct isl_basic_map *bmap;
304 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
305 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
306 return (struct isl_basic_set *)bmap;
309 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
310 unsigned extra, unsigned n_eq, unsigned n_ineq)
312 struct isl_basic_map *bmap;
316 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
321 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
327 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
328 unsigned nparam, unsigned in, unsigned out, unsigned extra,
329 unsigned n_eq, unsigned n_ineq)
331 struct isl_basic_map *bmap;
334 dim = isl_dim_alloc(ctx, nparam, in, out);
338 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
342 static void dup_constraints(
343 struct isl_basic_map *dst, struct isl_basic_map *src)
346 unsigned total = isl_basic_map_total_dim(src);
348 for (i = 0; i < src->n_eq; ++i) {
349 int j = isl_basic_map_alloc_equality(dst);
350 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
353 for (i = 0; i < src->n_ineq; ++i) {
354 int j = isl_basic_map_alloc_inequality(dst);
355 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
358 for (i = 0; i < src->n_div; ++i) {
359 int j = isl_basic_map_alloc_div(dst);
360 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
362 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
365 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
367 struct isl_basic_map *dup;
371 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
372 bmap->n_div, bmap->n_eq, bmap->n_ineq);
375 dup_constraints(dup, bmap);
376 dup->flags = bmap->flags;
377 dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
381 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
383 struct isl_basic_map *dup;
385 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
386 return (struct isl_basic_set *)dup;
389 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
394 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
398 return isl_basic_set_dup(bset);
401 struct isl_set *isl_set_copy(struct isl_set *set)
410 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
415 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
419 return isl_basic_map_dup(bmap);
422 struct isl_map *isl_map_copy(struct isl_map *map)
431 void isl_basic_map_free(struct isl_basic_map *bmap)
439 isl_ctx_deref(bmap->ctx);
441 isl_blk_free(bmap->ctx, bmap->block2);
443 isl_blk_free(bmap->ctx, bmap->block);
444 isl_vec_free(bmap->ctx, bmap->sample);
445 isl_dim_free(bmap->dim);
449 void isl_basic_set_free(struct isl_basic_set *bset)
451 isl_basic_map_free((struct isl_basic_map *)bset);
454 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
456 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
459 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
465 isl_assert(ctx, room_for_con(bmap, 1), return -1);
466 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
468 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
469 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
470 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
471 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
472 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
473 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
475 int j = isl_basic_map_alloc_inequality(bmap);
479 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
480 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
487 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
488 bmap->extra - bmap->n_div);
492 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
494 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
497 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
501 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
506 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
508 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
511 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
516 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
518 if (pos != bmap->n_eq - 1) {
520 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
521 bmap->eq[bmap->n_eq - 1] = t;
527 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
529 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
532 void isl_basic_map_inequality_to_equality(
533 struct isl_basic_map *bmap, unsigned pos)
538 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
539 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
544 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
545 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
546 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
547 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
550 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
552 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
555 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
561 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
562 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
563 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
564 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
565 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
566 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
567 1 + isl_basic_map_total_dim(bmap),
568 bmap->extra - bmap->n_div);
569 return bmap->n_ineq++;
572 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
574 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
577 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
581 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
586 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
588 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
591 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
596 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
598 if (pos != bmap->n_ineq - 1) {
600 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
601 bmap->ineq[bmap->n_ineq - 1] = t;
602 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
608 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
610 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
613 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
617 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
618 isl_seq_clr(bmap->div[bmap->n_div] +
619 1 + 1 + isl_basic_map_total_dim(bmap),
620 bmap->extra - bmap->n_div);
621 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
622 return bmap->n_div++;
625 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
627 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
630 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
634 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
639 /* Copy constraint from src to dst, putting the vars of src at offset
640 * dim_off in dst and the divs of src at offset div_off in dst.
641 * If both sets are actually map, then dim_off applies to the input
644 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
645 struct isl_basic_map *src_map, isl_int *src,
646 unsigned in_off, unsigned out_off, unsigned div_off)
648 unsigned src_nparam = isl_basic_map_n_param(src_map);
649 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
650 unsigned src_in = isl_basic_map_n_in(src_map);
651 unsigned dst_in = isl_basic_map_n_in(dst_map);
652 unsigned src_out = isl_basic_map_n_out(src_map);
653 unsigned dst_out = isl_basic_map_n_out(dst_map);
654 isl_int_set(dst[0], src[0]);
655 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
656 if (dst_nparam > src_nparam)
657 isl_seq_clr(dst+1+src_nparam,
658 dst_nparam - src_nparam);
659 isl_seq_clr(dst+1+dst_nparam, in_off);
660 isl_seq_cpy(dst+1+dst_nparam+in_off,
662 isl_min(dst_in-in_off, src_in));
663 if (dst_in-in_off > src_in)
664 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
665 dst_in - in_off - src_in);
666 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
667 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
668 src+1+src_nparam+src_in,
669 isl_min(dst_out-out_off, src_out));
670 if (dst_out-out_off > src_out)
671 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
672 dst_out - out_off - src_out);
673 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
674 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
675 src+1+src_nparam+src_in+src_out,
676 isl_min(dst_map->extra-div_off, src_map->n_div));
677 if (dst_map->n_div-div_off > src_map->n_div)
678 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
679 div_off+src_map->n_div,
680 dst_map->n_div - div_off - src_map->n_div);
683 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
684 struct isl_basic_map *src_map, isl_int *src,
685 unsigned in_off, unsigned out_off, unsigned div_off)
687 isl_int_set(dst[0], src[0]);
688 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
691 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
692 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
697 if (!bmap1 || !bmap2)
700 div_off = bmap1->n_div;
702 for (i = 0; i < bmap2->n_eq; ++i) {
703 int i1 = isl_basic_map_alloc_equality(bmap1);
706 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
707 i_pos, o_pos, div_off);
710 for (i = 0; i < bmap2->n_ineq; ++i) {
711 int i1 = isl_basic_map_alloc_inequality(bmap1);
714 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
715 i_pos, o_pos, div_off);
718 for (i = 0; i < bmap2->n_div; ++i) {
719 int i1 = isl_basic_map_alloc_div(bmap1);
722 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
723 i_pos, o_pos, div_off);
726 isl_basic_map_free(bmap2);
731 isl_basic_map_free(bmap1);
732 isl_basic_map_free(bmap2);
736 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
737 struct isl_dim_map *dim_map)
741 for (i = 0; i < dim_map->len; ++i) {
742 if (dim_map->pos[i] < 0)
743 isl_int_set_si(dst[i], 0);
745 isl_int_set(dst[i], src[dim_map->pos[i]]);
749 static void copy_div_dim_map(isl_int *dst, isl_int *src,
750 struct isl_dim_map *dim_map)
752 isl_int_set(dst[0], src[0]);
753 copy_constraint_dim_map(dst+1, src+1, dim_map);
756 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
757 struct isl_basic_map *src, struct isl_dim_map *dim_map)
761 if (!src || !dst || !dim_map)
764 for (i = 0; i < src->n_eq; ++i) {
765 int i1 = isl_basic_map_alloc_equality(dst);
768 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
771 for (i = 0; i < src->n_ineq; ++i) {
772 int i1 = isl_basic_map_alloc_inequality(dst);
775 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
778 for (i = 0; i < src->n_div; ++i) {
779 int i1 = isl_basic_map_alloc_div(dst);
782 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
786 isl_basic_map_free(src);
791 isl_basic_map_free(src);
792 isl_basic_map_free(dst);
796 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
797 struct isl_basic_set *bset2, unsigned pos)
799 return (struct isl_basic_set *)
800 add_constraints((struct isl_basic_map *)bset1,
801 (struct isl_basic_map *)bset2, 0, pos);
804 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
805 struct isl_dim *dim, unsigned extra,
806 unsigned n_eq, unsigned n_ineq)
808 struct isl_basic_map *ext;
818 dims_ok = isl_dim_equal(base->dim, dim) &&
819 base->extra >= base->n_div + extra;
821 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
822 room_for_ineq(base, n_ineq)) {
827 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
828 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
829 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
830 extra += base->extra;
832 n_ineq += base->n_ineq;
834 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
840 ext = add_constraints(ext, base, 0, 0);
843 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
850 isl_basic_map_free(base);
854 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
855 struct isl_dim *dim, unsigned extra,
856 unsigned n_eq, unsigned n_ineq)
858 return (struct isl_basic_set *)
859 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
860 extra, n_eq, n_ineq);
863 struct isl_basic_map *isl_basic_map_extend_constraints(
864 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
868 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
872 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
873 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
874 unsigned n_eq, unsigned n_ineq)
876 struct isl_basic_map *bmap;
881 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
885 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
889 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
890 unsigned nparam, unsigned dim, unsigned extra,
891 unsigned n_eq, unsigned n_ineq)
893 return (struct isl_basic_set *)
894 isl_basic_map_extend((struct isl_basic_map *)base,
895 nparam, 0, dim, extra, n_eq, n_ineq);
898 struct isl_basic_set *isl_basic_set_extend_constraints(
899 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
901 return (struct isl_basic_set *)
902 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
906 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
908 return (struct isl_basic_set *)
909 isl_basic_map_cow((struct isl_basic_map *)bset);
912 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
919 bmap = isl_basic_map_dup(bmap);
921 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
925 struct isl_set *isl_set_cow(struct isl_set *set)
933 return isl_set_dup(set);
936 struct isl_map *isl_map_cow(struct isl_map *map)
944 return isl_map_dup(map);
947 static void swap_vars(struct isl_blk blk, isl_int *a,
948 unsigned a_len, unsigned b_len)
950 isl_seq_cpy(blk.data, a+a_len, b_len);
951 isl_seq_cpy(blk.data+b_len, a, a_len);
952 isl_seq_cpy(a, blk.data, b_len+a_len);
955 struct isl_basic_set *isl_basic_set_swap_vars(
956 struct isl_basic_set *bset, unsigned n)
966 nparam = isl_basic_set_n_param(bset);
967 dim = isl_basic_set_n_dim(bset);
968 isl_assert(bset->ctx, n <= dim, goto error);
973 bset = isl_basic_set_cow(bset);
977 blk = isl_blk_alloc(bset->ctx, dim);
978 if (isl_blk_is_error(blk))
981 for (i = 0; i < bset->n_eq; ++i)
983 bset->eq[i]+1+nparam, n, dim - n);
985 for (i = 0; i < bset->n_ineq; ++i)
987 bset->ineq[i]+1+nparam, n, dim - n);
989 for (i = 0; i < bset->n_div; ++i)
991 bset->div[i]+1+1+nparam, n, dim - n);
993 isl_blk_free(bset->ctx, blk);
995 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
999 isl_basic_set_free(bset);
1003 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1006 set = isl_set_cow(set);
1010 for (i = 0; i < set->n; ++i) {
1011 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1017 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1021 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1027 total = isl_basic_map_total_dim(bmap);
1028 isl_basic_map_free_div(bmap, bmap->n_div);
1029 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1031 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1033 isl_basic_map_alloc_equality(bmap);
1037 isl_int_set_si(bmap->eq[i][0], 1);
1038 isl_seq_clr(bmap->eq[i]+1, total);
1039 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1040 return isl_basic_map_finalize(bmap);
1042 isl_basic_map_free(bmap);
1046 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1048 return (struct isl_basic_set *)
1049 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1052 static void swap_div(struct isl_basic_map *bmap, int a, int b)
1055 unsigned off = isl_dim_total(bmap->dim);
1056 isl_int *t = bmap->div[a];
1057 bmap->div[a] = bmap->div[b];
1060 for (i = 0; i < bmap->n_eq; ++i)
1061 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1063 for (i = 0; i < bmap->n_ineq; ++i)
1064 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1066 for (i = 0; i < bmap->n_div; ++i)
1067 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1068 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1071 /* Eliminate the specified n dimensions starting at first from the
1072 * constraints using Fourier-Motzkin, The dimensions themselves
1075 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1076 unsigned first, unsigned n)
1086 set = isl_set_cow(set);
1089 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1090 nparam = isl_set_n_param(set);
1092 for (i = 0; i < set->n; ++i) {
1093 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1104 /* Project out n dimensions starting at first using Fourier-Motzkin */
1105 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1106 unsigned first, unsigned n)
1108 set = isl_set_eliminate_dims(set, first, n);
1109 set = isl_set_drop_dims(set, first, n);
1113 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1115 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1123 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1132 set = isl_set_cow(set);
1136 for (i = 0; i < set->n; ++i) {
1137 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1147 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1148 enum isl_dim_type type, unsigned first, unsigned n)
1152 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1156 bmap = isl_basic_map_eliminate_vars(bmap,
1157 isl_basic_map_offset(bmap, type) - 1 + first, n);
1158 bmap = isl_basic_map_drop(bmap, type, first, n);
1161 isl_basic_map_free(bmap);
1165 struct isl_map *isl_map_remove(struct isl_map *map,
1166 enum isl_dim_type type, unsigned first, unsigned n)
1174 map = isl_map_cow(map);
1177 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1179 for (i = 0; i < map->n; ++i) {
1180 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1181 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1185 map = isl_map_drop(map, type, first, n);
1192 /* Project out n inputs starting at first using Fourier-Motzkin */
1193 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1194 unsigned first, unsigned n)
1196 return isl_map_remove(map, isl_dim_in, first, n);
1199 /* Project out n dimensions starting at first using Fourier-Motzkin */
1200 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1201 unsigned first, unsigned n)
1203 unsigned nparam = isl_basic_set_n_param(bset);
1204 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1205 bset = isl_basic_set_drop_dims(bset, first, n);
1209 static void dump_term(struct isl_basic_map *bmap,
1210 isl_int c, int pos, FILE *out)
1213 unsigned in = isl_basic_map_n_in(bmap);
1214 unsigned dim = in + isl_basic_map_n_out(bmap);
1215 unsigned nparam = isl_basic_map_n_param(bmap);
1217 isl_int_print(out, c, 0);
1219 if (!isl_int_is_one(c))
1220 isl_int_print(out, c, 0);
1221 if (pos < 1 + nparam) {
1222 name = isl_dim_get_name(bmap->dim,
1223 isl_dim_param, pos - 1);
1225 fprintf(out, "%s", name);
1227 fprintf(out, "p%d", pos - 1);
1228 } else if (pos < 1 + nparam + in)
1229 fprintf(out, "i%d", pos - 1 - nparam);
1230 else if (pos < 1 + nparam + dim)
1231 fprintf(out, "o%d", pos - 1 - nparam - in);
1233 fprintf(out, "e%d", pos - 1 - nparam - dim);
1237 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1238 int sign, FILE *out)
1242 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1246 for (i = 0, first = 1; i < len; ++i) {
1247 if (isl_int_sgn(c[i]) * sign <= 0)
1250 fprintf(out, " + ");
1252 isl_int_abs(v, c[i]);
1253 dump_term(bmap, v, i, out);
1260 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1261 const char *op, FILE *out, int indent)
1265 fprintf(out, "%*s", indent, "");
1267 dump_constraint_sign(bmap, c, 1, out);
1268 fprintf(out, " %s ", op);
1269 dump_constraint_sign(bmap, c, -1, out);
1273 for (i = bmap->n_div; i < bmap->extra; ++i) {
1274 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1276 fprintf(out, "%*s", indent, "");
1277 fprintf(out, "ERROR: unused div coefficient not zero\n");
1282 static void dump_constraints(struct isl_basic_map *bmap,
1283 isl_int **c, unsigned n,
1284 const char *op, FILE *out, int indent)
1288 for (i = 0; i < n; ++i)
1289 dump_constraint(bmap, c[i], op, out, indent);
1292 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1296 unsigned total = isl_basic_map_total_dim(bmap);
1298 for (j = 0; j < 1 + total; ++j) {
1299 if (isl_int_is_zero(exp[j]))
1301 if (!first && isl_int_is_pos(exp[j]))
1303 dump_term(bmap, exp[j], j, out);
1308 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1312 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1313 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1315 for (i = 0; i < bmap->n_div; ++i) {
1316 fprintf(out, "%*s", indent, "");
1317 fprintf(out, "e%d = [(", i);
1318 dump_affine(bmap, bmap->div[i]+1, out);
1320 isl_int_print(out, bmap->div[i][0], 0);
1321 fprintf(out, "]\n");
1325 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1328 fprintf(out, "null basic set\n");
1332 fprintf(out, "%*s", indent, "");
1333 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1334 bset->ref, bset->dim->nparam, bset->dim->n_out,
1335 bset->extra, bset->flags);
1336 dump((struct isl_basic_map *)bset, out, indent);
1339 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1342 fprintf(out, "null basic map\n");
1346 fprintf(out, "%*s", indent, "");
1347 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1348 "flags: %x, n_name: %d\n",
1350 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1351 bmap->extra, bmap->flags, bmap->dim->n_name);
1352 dump(bmap, out, indent);
1355 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1360 total = isl_basic_map_total_dim(bmap);
1361 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1362 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1363 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1364 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1368 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1370 struct isl_set *set;
1374 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1375 isl_assert(dim->ctx, n >= 0, return NULL);
1376 set = isl_alloc(dim->ctx, struct isl_set,
1377 sizeof(struct isl_set) +
1378 n * sizeof(struct isl_basic_set *));
1382 set->ctx = dim->ctx;
1383 isl_ctx_ref(set->ctx);
1395 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1396 unsigned nparam, unsigned dim, int n, unsigned flags)
1398 struct isl_set *set;
1399 struct isl_dim *dims;
1401 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1405 set = isl_set_alloc_dim(dims, n, flags);
1409 struct isl_set *isl_set_dup(struct isl_set *set)
1412 struct isl_set *dup;
1417 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1420 for (i = 0; i < set->n; ++i)
1421 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1425 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1427 struct isl_set *set;
1432 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1434 isl_basic_set_free(bset);
1437 return isl_set_add(set, bset);
1440 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1442 struct isl_map *map;
1447 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1449 isl_basic_map_free(bmap);
1452 return isl_map_add(map, bmap);
1455 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1459 isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1460 isl_assert(set->ctx, set->n < set->size, goto error);
1461 set->p[set->n] = bset;
1468 isl_basic_set_free(bset);
1472 void isl_set_free(struct isl_set *set)
1482 isl_ctx_deref(set->ctx);
1483 for (i = 0; i < set->n; ++i)
1484 isl_basic_set_free(set->p[i]);
1485 isl_dim_free(set->dim);
1489 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1494 fprintf(out, "null set\n");
1498 fprintf(out, "%*s", indent, "");
1499 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1500 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1502 for (i = 0; i < set->n; ++i) {
1503 fprintf(out, "%*s", indent, "");
1504 fprintf(out, "basic set %d:\n", i);
1505 isl_basic_set_dump(set->p[i], out, indent+4);
1509 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1514 fprintf(out, "null map\n");
1518 fprintf(out, "%*s", indent, "");
1519 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1520 "flags: %x, n_name: %d\n",
1521 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1522 map->dim->n_out, map->flags, map->dim->n_name);
1523 for (i = 0; i < map->n; ++i) {
1524 fprintf(out, "%*s", indent, "");
1525 fprintf(out, "basic map %d:\n", i);
1526 isl_basic_map_dump(map->p[i], out, indent+4);
1530 struct isl_basic_map *isl_basic_map_intersect_domain(
1531 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1533 struct isl_basic_map *bmap_domain;
1534 struct isl_dim *dim;
1539 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1540 bset->dim, isl_dim_param), goto error);
1542 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1543 isl_assert(bset->ctx,
1544 isl_basic_map_compatible_domain(bmap, bset), goto error);
1546 bmap = isl_basic_map_cow(bmap);
1547 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1548 bset->n_div, bset->n_eq, bset->n_ineq);
1551 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1552 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1553 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1555 bmap = isl_basic_map_simplify(bmap);
1556 return isl_basic_map_finalize(bmap);
1558 isl_basic_map_free(bmap);
1559 isl_basic_set_free(bset);
1563 struct isl_basic_map *isl_basic_map_intersect_range(
1564 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1566 struct isl_basic_map *bmap_range;
1571 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1572 bset->dim, isl_dim_param), goto error);
1574 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1575 isl_assert(bset->ctx,
1576 isl_basic_map_compatible_range(bmap, bset), goto error);
1578 bmap = isl_basic_map_cow(bmap);
1579 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1580 bset->n_div, bset->n_eq, bset->n_ineq);
1583 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1584 bmap = add_constraints(bmap, bmap_range, 0, 0);
1586 bmap = isl_basic_map_simplify(bmap);
1587 return isl_basic_map_finalize(bmap);
1589 isl_basic_map_free(bmap);
1590 isl_basic_set_free(bset);
1594 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1600 total = 1 + isl_basic_map_total_dim(bmap);
1601 if (total != vec->size)
1606 for (i = 0; i < bmap->n_eq; ++i) {
1607 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
1608 if (!isl_int_is_zero(s)) {
1614 for (i = 0; i < bmap->n_ineq; ++i) {
1615 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
1616 if (isl_int_is_neg(s)) {
1627 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1629 return basic_map_contains((struct isl_basic_map *)bset, vec);
1632 struct isl_basic_map *isl_basic_map_intersect(
1633 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1635 struct isl_vec *sample = NULL;
1637 if (!bmap1 || !bmap2)
1640 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1641 bmap2->dim, isl_dim_param), goto error);
1642 if (isl_dim_total(bmap1->dim) ==
1643 isl_dim_size(bmap1->dim, isl_dim_param) &&
1644 isl_dim_total(bmap2->dim) !=
1645 isl_dim_size(bmap2->dim, isl_dim_param))
1646 return isl_basic_map_intersect(bmap2, bmap1);
1648 if (isl_dim_total(bmap2->dim) !=
1649 isl_dim_size(bmap2->dim, isl_dim_param))
1650 isl_assert(bmap1->ctx,
1651 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1653 if (bmap1->sample &&
1654 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1655 basic_map_contains(bmap2, bmap1->sample) > 0)
1656 sample = isl_vec_copy(bmap1->ctx, bmap1->sample);
1657 else if (bmap2->sample &&
1658 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1659 basic_map_contains(bmap2, bmap2->sample) > 0)
1660 sample = isl_vec_copy(bmap2->ctx, bmap2->sample);
1662 bmap1 = isl_basic_map_cow(bmap1);
1663 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1664 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1667 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1670 isl_vec_free(bmap1->ctx, bmap1->sample);
1671 bmap1->sample = sample;
1674 bmap1 = isl_basic_map_simplify(bmap1);
1675 return isl_basic_map_finalize(bmap1);
1678 isl_vec_free(bmap1->ctx, sample);
1679 isl_basic_map_free(bmap1);
1680 isl_basic_map_free(bmap2);
1684 struct isl_basic_set *isl_basic_set_intersect(
1685 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1687 return (struct isl_basic_set *)
1688 isl_basic_map_intersect(
1689 (struct isl_basic_map *)bset1,
1690 (struct isl_basic_map *)bset2);
1693 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1696 struct isl_map *result;
1702 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1703 map2->dim, isl_dim_param), goto error);
1704 if (isl_dim_total(map1->dim) ==
1705 isl_dim_size(map1->dim, isl_dim_param) &&
1706 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1707 return isl_map_intersect(map2, map1);
1709 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1710 isl_assert(map1->ctx,
1711 isl_dim_equal(map1->dim, map2->dim), goto error);
1713 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1714 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1715 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1717 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1718 map1->n * map2->n, flags);
1721 for (i = 0; i < map1->n; ++i)
1722 for (j = 0; j < map2->n; ++j) {
1723 struct isl_basic_map *part;
1724 part = isl_basic_map_intersect(
1725 isl_basic_map_copy(map1->p[i]),
1726 isl_basic_map_copy(map2->p[j]));
1727 if (isl_basic_map_is_empty(part))
1728 isl_basic_map_free(part);
1730 result = isl_map_add(result, part);
1743 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1745 return (struct isl_set *)
1746 isl_map_intersect((struct isl_map *)set1,
1747 (struct isl_map *)set2);
1750 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1752 struct isl_dim *dim;
1753 struct isl_basic_set *bset;
1758 bmap = isl_basic_map_cow(bmap);
1761 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1762 in = isl_basic_map_n_in(bmap);
1763 bset = isl_basic_set_from_basic_map(bmap);
1764 bset = isl_basic_set_swap_vars(bset, in);
1765 return isl_basic_map_from_basic_set(bset, dim);
1768 /* Turn final n dimensions into existentially quantified variables.
1770 struct isl_basic_set *isl_basic_set_project_out(
1771 struct isl_basic_set *bset, unsigned n, unsigned flags)
1781 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1786 bset = isl_basic_set_cow(bset);
1788 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1789 old = bset->block2.data;
1790 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1791 (bset->extra + n) * (1 + row_size));
1792 if (!bset->block2.data)
1794 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1797 for (i = 0; i < n; ++i) {
1798 new_div[i] = bset->block2.data +
1799 (bset->extra + i) * (1 + row_size);
1800 isl_seq_clr(new_div[i], 1 + row_size);
1802 for (i = 0; i < bset->extra; ++i)
1803 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1805 bset->div = new_div;
1808 bset->dim = isl_dim_drop_outputs(bset->dim,
1809 isl_basic_set_n_dim(bset) - n, n);
1812 bset = isl_basic_set_simplify(bset);
1813 return isl_basic_set_finalize(bset);
1815 isl_basic_set_free(bset);
1819 struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1823 for (i = 0; i < n; ++i) {
1824 j = isl_basic_map_alloc_div(bmap);
1827 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1831 isl_basic_map_free(bmap);
1835 struct isl_basic_map *isl_basic_map_apply_range(
1836 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1838 struct isl_dim *dim_result = NULL;
1839 struct isl_basic_map *bmap;
1840 unsigned n_in, n_out, n, nparam, total, pos;
1841 struct isl_dim_map *dim_map1, *dim_map2;
1843 if (!bmap1 || !bmap2)
1846 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1847 isl_dim_copy(bmap2->dim));
1849 n_in = isl_basic_map_n_in(bmap1);
1850 n_out = isl_basic_map_n_out(bmap2);
1851 n = isl_basic_map_n_out(bmap1);
1852 nparam = isl_basic_map_n_param(bmap1);
1854 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1855 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1856 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1857 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1858 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1859 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1860 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1861 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1862 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1863 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1864 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1866 bmap = isl_basic_map_alloc_dim(dim_result,
1867 bmap1->n_div + bmap2->n_div + n,
1868 bmap1->n_eq + bmap2->n_eq,
1869 bmap1->n_ineq + bmap2->n_ineq);
1870 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1871 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1872 bmap = add_divs(bmap, n);
1873 bmap = isl_basic_map_simplify(bmap);
1874 return isl_basic_map_finalize(bmap);
1876 isl_basic_map_free(bmap1);
1877 isl_basic_map_free(bmap2);
1881 struct isl_basic_set *isl_basic_set_apply(
1882 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1887 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1890 return (struct isl_basic_set *)
1891 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1893 isl_basic_set_free(bset);
1894 isl_basic_map_free(bmap);
1898 struct isl_basic_map *isl_basic_map_apply_domain(
1899 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1901 if (!bmap1 || !bmap2)
1905 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1907 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1910 bmap1 = isl_basic_map_reverse(bmap1);
1911 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1912 return isl_basic_map_reverse(bmap1);
1914 isl_basic_map_free(bmap1);
1915 isl_basic_map_free(bmap2);
1919 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1920 * A \cap B -> f(A) + f(B)
1922 struct isl_basic_map *isl_basic_map_sum(
1923 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1925 unsigned n_in, n_out, nparam, total, pos;
1926 struct isl_basic_map *bmap = NULL;
1927 struct isl_dim_map *dim_map1, *dim_map2;
1930 if (!bmap1 || !bmap2)
1933 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1936 nparam = isl_basic_map_n_param(bmap1);
1937 n_in = isl_basic_map_n_in(bmap1);
1938 n_out = isl_basic_map_n_out(bmap1);
1940 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
1941 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1942 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
1943 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1944 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
1945 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1946 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1947 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
1948 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1949 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1950 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
1952 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
1953 bmap1->n_div + bmap2->n_div + 2 * n_out,
1954 bmap1->n_eq + bmap2->n_eq + n_out,
1955 bmap1->n_ineq + bmap2->n_ineq);
1956 for (i = 0; i < n_out; ++i) {
1957 int j = isl_basic_map_alloc_equality(bmap);
1960 isl_seq_clr(bmap->eq[j], 1+total);
1961 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
1962 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
1963 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
1965 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1966 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1967 bmap = add_divs(bmap, 2 * n_out);
1969 bmap = isl_basic_map_simplify(bmap);
1970 return isl_basic_map_finalize(bmap);
1972 isl_basic_map_free(bmap);
1973 isl_basic_map_free(bmap1);
1974 isl_basic_map_free(bmap2);
1978 /* Given a basic map A -> f(A), construct A -> -f(A).
1980 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
1985 bmap = isl_basic_map_cow(bmap);
1989 n = isl_basic_map_dim(bmap, isl_dim_out);
1990 off = isl_basic_map_offset(bmap, isl_dim_out);
1991 for (i = 0; i < bmap->n_eq; ++i)
1992 for (j = 0; j < n; ++j)
1993 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
1994 for (i = 0; i < bmap->n_ineq; ++i)
1995 for (j = 0; j < n; ++j)
1996 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
1997 for (i = 0; i < bmap->n_div; ++i)
1998 for (j = 0; j < n; ++j)
1999 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2000 return isl_basic_map_finalize(bmap);
2003 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2004 * A -> floor(f(A)/d).
2006 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2009 unsigned n_in, n_out, nparam, total, pos;
2010 struct isl_basic_map *result = NULL;
2011 struct isl_dim_map *dim_map;
2017 nparam = isl_basic_map_n_param(bmap);
2018 n_in = isl_basic_map_n_in(bmap);
2019 n_out = isl_basic_map_n_out(bmap);
2021 total = nparam + n_in + n_out + bmap->n_div + n_out;
2022 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2023 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2024 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2025 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2026 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2028 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2029 bmap->n_div + n_out,
2030 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2031 result = add_constraints_dim_map(result, bmap, dim_map);
2032 result = add_divs(result, n_out);
2033 for (i = 0; i < n_out; ++i) {
2035 j = isl_basic_map_alloc_inequality(result);
2038 isl_seq_clr(result->ineq[j], 1+total);
2039 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2040 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2041 j = isl_basic_map_alloc_inequality(result);
2044 isl_seq_clr(result->ineq[j], 1+total);
2045 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2046 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2047 isl_int_sub_ui(result->ineq[j][0], d, 1);
2050 result = isl_basic_map_simplify(result);
2051 return isl_basic_map_finalize(result);
2053 isl_basic_map_free(result);
2057 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2063 i = isl_basic_map_alloc_equality(bmap);
2066 nparam = isl_basic_map_n_param(bmap);
2067 n_in = isl_basic_map_n_in(bmap);
2068 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2069 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2070 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2071 return isl_basic_map_finalize(bmap);
2073 isl_basic_map_free(bmap);
2077 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2083 i = isl_basic_map_alloc_inequality(bmap);
2086 nparam = isl_basic_map_n_param(bmap);
2087 n_in = isl_basic_map_n_in(bmap);
2088 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2089 isl_int_set_si(bmap->ineq[i][0], -1);
2090 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2091 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2092 return isl_basic_map_finalize(bmap);
2094 isl_basic_map_free(bmap);
2098 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2104 i = isl_basic_map_alloc_inequality(bmap);
2107 nparam = isl_basic_map_n_param(bmap);
2108 n_in = isl_basic_map_n_in(bmap);
2109 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2110 isl_int_set_si(bmap->ineq[i][0], -1);
2111 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2112 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2113 return isl_basic_map_finalize(bmap);
2115 isl_basic_map_free(bmap);
2119 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2122 struct isl_basic_map *bmap;
2123 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2126 for (i = 0; i < n_equal && bmap; ++i)
2127 bmap = var_equal(bmap, i);
2128 return isl_basic_map_finalize(bmap);
2131 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2134 struct isl_basic_map *bmap;
2135 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2138 for (i = 0; i < pos && bmap; ++i)
2139 bmap = var_equal(bmap, i);
2141 bmap = var_less(bmap, pos);
2142 return isl_basic_map_finalize(bmap);
2145 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2148 struct isl_basic_map *bmap;
2149 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2152 for (i = 0; i < pos && bmap; ++i)
2153 bmap = var_equal(bmap, i);
2155 bmap = var_more(bmap, pos);
2156 return isl_basic_map_finalize(bmap);
2159 struct isl_basic_map *isl_basic_map_from_basic_set(
2160 struct isl_basic_set *bset, struct isl_dim *dim)
2162 struct isl_basic_map *bmap;
2164 bset = isl_basic_set_cow(bset);
2168 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2169 isl_dim_free(bset->dim);
2170 bmap = (struct isl_basic_map *) bset;
2172 return isl_basic_map_finalize(bmap);
2174 isl_basic_set_free(bset);
2179 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2183 if (bmap->dim->n_in == 0)
2184 return (struct isl_basic_set *)bmap;
2185 bmap = isl_basic_map_cow(bmap);
2188 bmap->dim = isl_dim_cow(bmap->dim);
2191 bmap->dim->n_out += bmap->dim->n_in;
2192 bmap->dim->n_in = 0;
2193 bmap = isl_basic_map_finalize(bmap);
2194 return (struct isl_basic_set *)bmap;
2196 isl_basic_map_free(bmap);
2200 /* For a div d = floor(f/m), add the constraints
2203 * -(f-(n-1)) + m d >= 0
2205 * Note that the second constraint is the negation of
2209 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2212 unsigned total = isl_basic_map_total_dim(bmap);
2213 unsigned div_pos = 1 + total - bmap->n_div + div;
2215 i = isl_basic_map_alloc_inequality(bmap);
2218 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2219 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2221 j = isl_basic_map_alloc_inequality(bmap);
2224 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2225 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2226 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2230 struct isl_basic_set *isl_basic_map_underlying_set(
2231 struct isl_basic_map *bmap)
2235 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2236 return (struct isl_basic_set *)bmap;
2237 bmap = isl_basic_map_cow(bmap);
2240 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2243 bmap->extra -= bmap->n_div;
2245 bmap = isl_basic_map_finalize(bmap);
2246 return (struct isl_basic_set *)bmap;
2251 struct isl_basic_map *isl_basic_map_overlying_set(
2252 struct isl_basic_set *bset, struct isl_basic_map *like)
2254 struct isl_basic_map *bmap;
2255 struct isl_ctx *ctx;
2262 isl_assert(ctx, bset->n_div == 0, goto error);
2263 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2264 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2266 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2267 isl_basic_map_free(like);
2268 return (struct isl_basic_map *)bset;
2270 bset = isl_basic_set_cow(bset);
2273 total = bset->dim->n_out + bset->extra;
2274 bmap = (struct isl_basic_map *)bset;
2275 isl_dim_free(bmap->dim);
2276 bmap->dim = isl_dim_copy(like->dim);
2279 bmap->n_div = like->n_div;
2280 bmap->extra += like->n_div;
2283 ltotal = total - bmap->extra + like->extra;
2286 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2287 bmap->extra * (1 + 1 + total));
2288 if (isl_blk_is_error(bmap->block2))
2290 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2294 for (i = 0; i < bmap->extra; ++i)
2295 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2296 for (i = 0; i < like->n_div; ++i) {
2297 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2298 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2300 bmap = isl_basic_map_extend_constraints(bmap,
2301 0, 2 * like->n_div);
2302 for (i = 0; i < like->n_div; ++i) {
2303 if (isl_int_is_zero(bmap->div[i][0]))
2305 if (add_div_constraints(bmap, i) < 0)
2309 isl_basic_map_free(like);
2310 bmap = isl_basic_map_simplify(bmap);
2311 bmap = isl_basic_map_finalize(bmap);
2314 isl_basic_map_free(like);
2315 isl_basic_set_free(bset);
2319 struct isl_basic_set *isl_basic_set_from_underlying_set(
2320 struct isl_basic_set *bset, struct isl_basic_set *like)
2322 return (struct isl_basic_set *)
2323 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2326 struct isl_set *isl_set_from_underlying_set(
2327 struct isl_set *set, struct isl_basic_set *like)
2333 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2335 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2336 isl_basic_set_free(like);
2339 set = isl_set_cow(set);
2342 for (i = 0; i < set->n; ++i) {
2343 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2344 isl_basic_set_copy(like));
2348 isl_dim_free(set->dim);
2349 set->dim = isl_dim_copy(like->dim);
2352 isl_basic_set_free(like);
2355 isl_basic_set_free(like);
2360 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2364 map = isl_map_cow(map);
2367 map->dim = isl_dim_cow(map->dim);
2371 for (i = 1; i < map->n; ++i)
2372 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2374 for (i = 0; i < map->n; ++i) {
2375 map->p[i] = (struct isl_basic_map *)
2376 isl_basic_map_underlying_set(map->p[i]);
2381 map->dim = isl_dim_underlying(map->dim, 0);
2383 isl_dim_free(map->dim);
2384 map->dim = isl_dim_copy(map->p[0]->dim);
2388 return (struct isl_set *)map;
2394 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2396 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2399 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2401 struct isl_basic_set *domain;
2405 n_out = isl_basic_map_n_out(bmap);
2406 domain = isl_basic_set_from_basic_map(bmap);
2407 return isl_basic_set_project_out(domain, n_out, 0);
2410 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2412 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2415 struct isl_set *isl_map_range(struct isl_map *map)
2418 struct isl_set *set;
2422 map = isl_map_cow(map);
2426 set = (struct isl_set *) map;
2427 if (set->dim->n_in != 0) {
2428 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2432 for (i = 0; i < map->n; ++i) {
2433 set->p[i] = isl_basic_map_range(map->p[i]);
2437 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2438 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2445 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2448 struct isl_map *map = NULL;
2450 set = isl_set_cow(set);
2453 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2454 map = (struct isl_map *)set;
2455 for (i = 0; i < set->n; ++i) {
2456 map->p[i] = isl_basic_map_from_basic_set(
2457 set->p[i], isl_dim_copy(dim));
2461 isl_dim_free(map->dim);
2470 struct isl_map *isl_map_from_range(struct isl_set *set)
2472 return (struct isl_map *)set;
2475 struct isl_set *isl_set_from_map(struct isl_map *map)
2478 struct isl_set *set = NULL;
2482 map = isl_map_cow(map);
2485 map->dim = isl_dim_cow(map->dim);
2488 map->dim->n_out += map->dim->n_in;
2490 set = (struct isl_set *)map;
2491 for (i = 0; i < map->n; ++i) {
2492 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2502 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2504 struct isl_map *map;
2508 isl_assert(dim->ctx, n >= 0, return NULL);
2509 map = isl_alloc(dim->ctx, struct isl_map,
2510 sizeof(struct isl_map) +
2511 n * sizeof(struct isl_basic_map *));
2515 map->ctx = dim->ctx;
2516 isl_ctx_ref(map->ctx);
2528 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2529 unsigned nparam, unsigned in, unsigned out, int n,
2532 struct isl_map *map;
2533 struct isl_dim *dims;
2535 dims = isl_dim_alloc(ctx, nparam, in, out);
2539 map = isl_map_alloc_dim(dims, n, flags);
2543 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2544 unsigned nparam, unsigned in, unsigned out)
2546 struct isl_basic_map *bmap;
2547 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2548 bmap = isl_basic_map_set_to_empty(bmap);
2552 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2554 struct isl_basic_set *bset;
2555 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2556 bset = isl_basic_set_set_to_empty(bset);
2560 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2562 struct isl_basic_map *bmap;
2565 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2566 bmap = isl_basic_map_set_to_empty(bmap);
2570 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2572 struct isl_basic_map *bmap;
2575 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2576 bmap = isl_basic_map_set_to_empty(bmap);
2580 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2582 struct isl_basic_set *bset;
2585 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2586 bset = isl_basic_set_set_to_empty(bset);
2590 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2592 struct isl_basic_map *bmap;
2593 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2597 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2599 struct isl_basic_set *bset;
2600 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2604 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2608 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2611 struct isl_map *isl_map_empty(struct isl_dim *dim)
2613 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2616 struct isl_map *isl_map_empty_like(struct isl_map *model)
2620 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2623 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2627 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2630 struct isl_set *isl_set_empty(struct isl_dim *dim)
2632 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2635 struct isl_set *isl_set_empty_like(struct isl_set *model)
2639 return isl_set_empty(isl_dim_copy(model->dim));
2642 struct isl_set *isl_set_universe(struct isl_dim *dim)
2644 struct isl_set *set;
2647 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2648 set = isl_set_add(set, isl_basic_set_universe(dim));
2652 struct isl_map *isl_map_dup(struct isl_map *map)
2655 struct isl_map *dup;
2659 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2660 for (i = 0; i < map->n; ++i)
2661 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2665 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2669 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2670 isl_assert(map->ctx, map->n < map->size, goto error);
2671 map->p[map->n] = bmap;
2673 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2679 isl_basic_map_free(bmap);
2683 void isl_map_free(struct isl_map *map)
2693 isl_ctx_deref(map->ctx);
2694 for (i = 0; i < map->n; ++i)
2695 isl_basic_map_free(map->p[i]);
2696 isl_dim_free(map->dim);
2700 struct isl_map *isl_map_extend(struct isl_map *base,
2701 unsigned nparam, unsigned n_in, unsigned n_out)
2705 base = isl_map_cow(base);
2709 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2712 for (i = 0; i < base->n; ++i) {
2713 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2714 isl_dim_copy(base->dim), 0, 0, 0);
2724 struct isl_set *isl_set_extend(struct isl_set *base,
2725 unsigned nparam, unsigned dim)
2727 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2731 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2732 unsigned pos, int value)
2736 bmap = isl_basic_map_cow(bmap);
2737 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2738 j = isl_basic_map_alloc_equality(bmap);
2741 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2742 isl_int_set_si(bmap->eq[j][pos], -1);
2743 isl_int_set_si(bmap->eq[j][0], value);
2744 bmap = isl_basic_map_simplify(bmap);
2745 return isl_basic_map_finalize(bmap);
2747 isl_basic_map_free(bmap);
2751 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2752 enum isl_dim_type type, unsigned pos, int value)
2756 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2757 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2760 isl_basic_map_free(bmap);
2764 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2765 enum isl_dim_type type, unsigned pos, int value)
2767 return (struct isl_basic_set *)
2768 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2772 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2773 unsigned input, int value)
2775 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2778 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2779 unsigned dim, int value)
2781 return (struct isl_basic_set *)
2782 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2783 isl_dim_set, dim, value);
2786 struct isl_map *isl_map_fix_si(struct isl_map *map,
2787 enum isl_dim_type type, unsigned pos, int value)
2791 map = isl_map_cow(map);
2795 isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2796 for (i = 0; i < map->n; ++i) {
2797 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2801 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2808 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2809 unsigned input, int value)
2811 return isl_map_fix_si(map, isl_dim_in, input, value);
2814 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2816 return (struct isl_set *)
2817 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2820 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2821 unsigned dim, isl_int value)
2826 bset = isl_basic_set_cow(bset);
2827 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2828 j = isl_basic_set_alloc_inequality(bset);
2831 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2832 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2833 isl_int_neg(bset->ineq[j][0], value);
2834 bset = isl_basic_set_simplify(bset);
2835 return isl_basic_set_finalize(bset);
2837 isl_basic_set_free(bset);
2841 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2846 set = isl_set_cow(set);
2850 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2851 for (i = 0; i < set->n; ++i) {
2852 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2862 struct isl_map *isl_map_reverse(struct isl_map *map)
2867 map = isl_map_cow(map);
2871 map->dim = isl_dim_reverse(map->dim);
2874 for (i = 0; i < map->n; ++i) {
2875 map->p[i] = isl_basic_map_reverse(map->p[i]);
2879 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2886 struct isl_map *isl_basic_map_lexmax(
2887 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2888 struct isl_set **empty)
2890 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2893 struct isl_map *isl_basic_map_lexmin(
2894 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2895 struct isl_set **empty)
2897 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2900 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2902 struct isl_basic_map *bmap = NULL;
2903 struct isl_basic_set *dom = NULL;
2904 struct isl_map *min;
2905 struct isl_dim *param_dim;
2909 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2912 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2913 dom = isl_basic_set_universe(param_dim);
2916 min = isl_basic_map_lexmin(bmap, dom, NULL);
2917 return isl_map_range(min);
2919 isl_basic_map_free(bmap);
2923 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2930 off = isl_dim_total(bmap->dim);
2931 for (i = 0; i < bmap->n_div; ++i) {
2932 if (isl_int_is_zero(bmap->div[i][0]))
2933 return isl_pip_basic_map_compute_divs(bmap);
2934 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2937 return isl_map_from_basic_map(bmap);
2939 isl_basic_map_free(bmap);
2943 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2946 struct isl_map *res;
2952 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2953 for (i = 1 ; i < map->n; ++i) {
2955 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2956 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
2957 res = isl_map_union_disjoint(res, r2);
2959 res = isl_map_union(res, r2);
2966 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2968 return (struct isl_set *)
2969 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2972 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2974 return (struct isl_set *)
2975 isl_map_compute_divs((struct isl_map *)set);
2978 struct isl_set *isl_map_domain(struct isl_map *map)
2981 struct isl_set *set;
2986 map = isl_map_cow(map);
2990 set = (struct isl_set *)map;
2991 set->dim = isl_dim_domain(set->dim);
2994 for (i = 0; i < map->n; ++i) {
2995 set->p[i] = isl_basic_map_domain(map->p[i]);
2999 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3000 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3007 struct isl_map *isl_map_union_disjoint(
3008 struct isl_map *map1, struct isl_map *map2)
3012 struct isl_map *map = NULL;
3026 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3028 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3029 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3030 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3032 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3033 map1->n + map2->n, flags);
3036 for (i = 0; i < map1->n; ++i) {
3037 map = isl_map_add(map,
3038 isl_basic_map_copy(map1->p[i]));
3042 for (i = 0; i < map2->n; ++i) {
3043 map = isl_map_add(map,
3044 isl_basic_map_copy(map2->p[i]));
3058 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3060 map1 = isl_map_union_disjoint(map1, map2);
3064 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3068 struct isl_set *isl_set_union_disjoint(
3069 struct isl_set *set1, struct isl_set *set2)
3071 return (struct isl_set *)
3072 isl_map_union_disjoint(
3073 (struct isl_map *)set1, (struct isl_map *)set2);
3076 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3078 return (struct isl_set *)
3079 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3082 struct isl_map *isl_map_intersect_range(
3083 struct isl_map *map, struct isl_set *set)
3086 struct isl_map *result;
3092 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3093 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3094 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3096 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3097 map->n * set->n, flags);
3100 for (i = 0; i < map->n; ++i)
3101 for (j = 0; j < set->n; ++j) {
3102 result = isl_map_add(result,
3103 isl_basic_map_intersect_range(
3104 isl_basic_map_copy(map->p[i]),
3105 isl_basic_set_copy(set->p[j])));
3118 struct isl_map *isl_map_intersect_domain(
3119 struct isl_map *map, struct isl_set *set)
3121 return isl_map_reverse(
3122 isl_map_intersect_range(isl_map_reverse(map), set));
3125 struct isl_map *isl_map_apply_domain(
3126 struct isl_map *map1, struct isl_map *map2)
3130 map1 = isl_map_reverse(map1);
3131 map1 = isl_map_apply_range(map1, map2);
3132 return isl_map_reverse(map1);
3139 struct isl_map *isl_map_apply_range(
3140 struct isl_map *map1, struct isl_map *map2)
3142 struct isl_dim *dim_result;
3143 struct isl_map *result;
3152 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3153 isl_dim_copy(map2->dim));
3155 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3158 for (i = 0; i < map1->n; ++i)
3159 for (j = 0; j < map2->n; ++j) {
3160 result = isl_map_add(result,
3161 isl_basic_map_apply_range(
3162 isl_basic_map_copy(map1->p[i]),
3163 isl_basic_map_copy(map2->p[j])));
3169 if (result && result->n <= 1)
3170 ISL_F_SET(result, ISL_MAP_DISJOINT);
3179 * returns range - domain
3181 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3183 struct isl_basic_set *bset;
3190 dim = isl_basic_map_n_in(bmap);
3191 nparam = isl_basic_map_n_param(bmap);
3192 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3193 bset = isl_basic_set_from_basic_map(bmap);
3194 bset = isl_basic_set_cow(bset);
3195 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3196 bset = isl_basic_set_swap_vars(bset, 2*dim);
3197 for (i = 0; i < dim; ++i) {
3198 int j = isl_basic_map_alloc_equality(
3199 (struct isl_basic_map *)bset);
3202 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3203 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3204 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3205 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3207 return isl_basic_set_project_out(bset, 2*dim, 0);
3209 isl_basic_map_free(bmap);
3214 * returns range - domain
3216 struct isl_set *isl_map_deltas(struct isl_map *map)
3219 struct isl_set *result;
3224 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3225 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3226 isl_map_n_in(map), map->n, map->flags);
3229 for (i = 0; i < map->n; ++i)
3230 result = isl_set_add(result,
3231 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3239 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3241 struct isl_basic_map *bmap;
3249 nparam = dims->nparam;
3251 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3255 for (i = 0; i < dim; ++i) {
3256 int j = isl_basic_map_alloc_equality(bmap);
3259 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3260 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3261 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3263 return isl_basic_map_finalize(bmap);
3265 isl_basic_map_free(bmap);
3269 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3271 struct isl_dim *dim = isl_dim_map(set_dim);
3274 return basic_map_identity(dim);
3277 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3279 if (!model || !model->dim)
3281 isl_assert(model->ctx,
3282 model->dim->n_in == model->dim->n_out, return NULL);
3283 return basic_map_identity(isl_dim_copy(model->dim));
3286 static struct isl_map *map_identity(struct isl_dim *dim)
3288 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3289 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3292 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3294 struct isl_dim *dim = isl_dim_map(set_dim);
3297 return map_identity(dim);
3300 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3302 if (!model || !model->dim)
3304 isl_assert(model->ctx,
3305 model->dim->n_in == model->dim->n_out, return NULL);
3306 return map_identity(isl_dim_copy(model->dim));
3309 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3311 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3314 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3316 return isl_map_is_subset(
3317 (struct isl_map *)set1, (struct isl_map *)set2);
3320 int isl_basic_map_is_subset(
3321 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3324 struct isl_map *map1;
3325 struct isl_map *map2;
3327 if (!bmap1 || !bmap2)
3330 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3331 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3333 is_subset = isl_map_is_subset(map1, map2);
3341 int isl_basic_map_is_equal(
3342 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3346 if (!bmap1 || !bmap2)
3348 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3351 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3355 int isl_basic_set_is_equal(
3356 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3358 return isl_basic_map_is_equal(
3359 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3362 int isl_map_is_empty(struct isl_map *map)
3369 for (i = 0; i < map->n; ++i) {
3370 is_empty = isl_basic_map_is_empty(map->p[i]);
3379 int isl_map_fast_is_empty(struct isl_map *map)
3384 int isl_set_is_empty(struct isl_set *set)
3386 return isl_map_is_empty((struct isl_map *)set);
3389 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3393 struct isl_map *diff;
3398 if (isl_map_is_empty(map1))
3401 if (isl_map_is_empty(map2))
3404 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3408 is_subset = isl_map_is_empty(diff);
3414 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3420 is_subset = isl_map_is_subset(map1, map2);
3423 is_subset = isl_map_is_subset(map2, map1);
3427 int isl_basic_map_is_strict_subset(
3428 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3432 if (!bmap1 || !bmap2)
3434 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3437 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3438 if (is_subset == -1)
3443 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3447 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3450 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3452 struct isl_basic_set *bset = NULL;
3453 struct isl_vec *sample = NULL;
3460 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3463 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3464 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3465 copy = isl_basic_map_convex_hull(copy);
3466 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3467 isl_basic_map_free(copy);
3471 total = 1 + isl_basic_map_total_dim(bmap);
3472 if (bmap->sample && bmap->sample->size == total) {
3473 int contains = basic_map_contains(bmap, bmap->sample);
3479 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3482 sample = isl_basic_set_sample(bset);
3485 empty = sample->size == 0;
3487 isl_vec_free(bmap->ctx, bmap->sample);
3488 bmap->sample = sample;
3493 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
3497 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
3500 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3502 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3505 struct isl_map *isl_basic_map_union(
3506 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3508 struct isl_map *map;
3509 if (!bmap1 || !bmap2)
3512 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3514 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3517 map = isl_map_add(map, bmap1);
3518 map = isl_map_add(map, bmap2);
3521 isl_basic_map_free(bmap1);
3522 isl_basic_map_free(bmap2);
3526 struct isl_set *isl_basic_set_union(
3527 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3529 return (struct isl_set *)isl_basic_map_union(
3530 (struct isl_basic_map *)bset1,
3531 (struct isl_basic_map *)bset2);
3534 /* Order divs such that any div only depends on previous divs */
3535 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3538 unsigned off = isl_dim_total(bmap->dim);
3540 for (i = 0; i < bmap->n_div; ++i) {
3542 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3546 swap_div(bmap, i, pos);
3552 /* Look for a div in dst that corresponds to the div "div" in src.
3553 * The divs before "div" in src and dst are assumed to be the same.
3555 * Returns -1 if no corresponding div was found and the position
3556 * of the corresponding div in dst otherwise.
3558 static int find_div(struct isl_basic_map *dst,
3559 struct isl_basic_map *src, unsigned div)
3563 unsigned total = isl_dim_total(src->dim);
3565 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3566 for (i = div; i < dst->n_div; ++i)
3567 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3568 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3569 dst->n_div - div) == -1)
3574 struct isl_basic_map *isl_basic_map_align_divs(
3575 struct isl_basic_map *dst, struct isl_basic_map *src)
3578 unsigned total = isl_dim_total(src->dim);
3583 if (src->n_div == 0)
3586 for (i = 0; i < src->n_div; ++i)
3587 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3589 src = order_divs(src);
3590 dst = isl_basic_map_cow(dst);
3591 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3592 src->n_div, 0, 2 * src->n_div);
3595 for (i = 0; i < src->n_div; ++i) {
3596 int j = find_div(dst, src, i);
3598 j = isl_basic_map_alloc_div(dst);
3601 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3602 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3603 if (add_div_constraints(dst, j) < 0)
3607 swap_div(dst, i, j);
3611 isl_basic_map_free(dst);
3615 struct isl_basic_set *isl_basic_set_align_divs(
3616 struct isl_basic_set *dst, struct isl_basic_set *src)
3618 return (struct isl_basic_set *)isl_basic_map_align_divs(
3619 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3622 struct isl_map *isl_map_align_divs(struct isl_map *map)
3626 map = isl_map_compute_divs(map);
3627 map = isl_map_cow(map);
3631 for (i = 1; i < map->n; ++i)
3632 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3633 for (i = 1; i < map->n; ++i)
3634 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3636 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3640 struct isl_set *isl_set_align_divs(struct isl_set *set)
3642 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
3645 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3646 struct isl_basic_map *src, isl_int *c,
3647 unsigned len, int oppose)
3649 struct isl_basic_map *copy = NULL;
3654 copy = isl_basic_map_copy(src);
3655 copy = isl_basic_map_cow(copy);
3658 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3659 k = isl_basic_map_alloc_inequality(copy);
3663 isl_seq_neg(copy->ineq[k], c, len);
3665 isl_seq_cpy(copy->ineq[k], c, len);
3666 total = 1 + isl_basic_map_total_dim(copy);
3667 isl_seq_clr(copy->ineq[k]+len, total - len);
3668 isl_inequality_negate(copy, k);
3669 copy = isl_basic_map_simplify(copy);
3670 copy = isl_basic_map_finalize(copy);
3671 is_empty = isl_basic_map_is_empty(copy);
3675 dst = isl_map_add(dst, copy);
3677 isl_basic_map_free(copy);
3680 isl_basic_map_free(copy);
3685 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3689 struct isl_map *rest = NULL;
3691 unsigned total = isl_basic_map_total_dim(bmap);
3698 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3699 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3701 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3702 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3706 for (i = 0; i < map->n; ++i) {
3707 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3712 for (j = 0; j < map->n; ++j)
3713 map->p[j] = isl_basic_map_cow(map->p[j]);
3715 for (i = 0; i < bmap->n_eq; ++i) {
3716 for (j = 0; j < map->n; ++j) {
3717 rest = add_cut_constraint(rest,
3718 map->p[j], bmap->eq[i], 1+total, 0);
3722 rest = add_cut_constraint(rest,
3723 map->p[j], bmap->eq[i], 1+total, 1);
3727 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3731 k = isl_basic_map_alloc_equality(map->p[j]);
3734 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3735 isl_seq_clr(map->p[j]->eq[k]+1+total,
3736 map->p[j]->n_div - bmap->n_div);
3740 for (i = 0; i < bmap->n_ineq; ++i) {
3741 for (j = 0; j < map->n; ++j) {
3742 rest = add_cut_constraint(rest,
3743 map->p[j], bmap->ineq[i], 1+total, 0);
3747 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3751 k = isl_basic_map_alloc_inequality(map->p[j]);
3754 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3755 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3756 map->p[j]->n_div - bmap->n_div);
3768 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3774 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3776 if (isl_map_is_empty(map2)) {
3781 map1 = isl_map_compute_divs(map1);
3782 map2 = isl_map_compute_divs(map2);
3786 for (i = 0; map1 && i < map2->n; ++i)
3787 map1 = subtract(map1, map2->p[i]);
3797 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3799 return (struct isl_set *)
3801 (struct isl_map *)set1, (struct isl_map *)set2);
3804 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3808 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3809 map = isl_map_intersect_domain(map, set);
3810 set = isl_map_range(map);
3818 /* There is no need to cow as removing empty parts doesn't change
3819 * the meaning of the set.
3821 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3828 for (i = map->n-1; i >= 0; --i) {
3829 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3831 isl_basic_map_free(map->p[i]);
3832 if (i != map->n-1) {
3833 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3834 map->p[i] = map->p[map->n-1];
3842 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3844 return (struct isl_set *)
3845 isl_map_remove_empty_parts((struct isl_map *)set);
3848 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
3850 struct isl_basic_map *bmap;
3851 if (!map || map->n == 0)
3853 bmap = map->p[map->n-1];
3854 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
3855 return isl_basic_map_copy(bmap);
3858 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3860 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
3863 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
3864 struct isl_basic_map *bmap)
3870 for (i = map->n-1; i >= 0; --i) {
3871 if (map->p[i] != bmap)
3873 map = isl_map_cow(map);
3876 isl_basic_map_free(map->p[i]);
3877 if (i != map->n-1) {
3878 ISL_F_CLR(map, ISL_SET_NORMALIZED);
3879 map->p[i] = map->p[map->n-1];
3884 isl_basic_map_free(bmap);
3888 isl_basic_map_free(bmap);
3892 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3893 struct isl_basic_set *bset)
3895 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
3896 (struct isl_basic_map *)bset);
3899 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3900 * for any common value of the parameters and dimensions preceding dim
3901 * in both basic sets, the values of dimension pos in bset1 are
3902 * smaller or larger than those in bset2.
3905 * 1 if bset1 follows bset2
3906 * -1 if bset1 precedes bset2
3907 * 0 if bset1 and bset2 are incomparable
3908 * -2 if some error occurred.
3910 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3911 struct isl_basic_set *bset2, int pos)
3913 struct isl_dim *dims;
3914 struct isl_basic_map *bmap1 = NULL;
3915 struct isl_basic_map *bmap2 = NULL;
3916 struct isl_ctx *ctx;
3917 struct isl_vec *obj;
3920 unsigned dim1, dim2;
3922 enum isl_lp_result res;
3925 if (!bset1 || !bset2)
3928 nparam = isl_basic_set_n_param(bset1);
3929 dim1 = isl_basic_set_n_dim(bset1);
3930 dim2 = isl_basic_set_n_dim(bset2);
3931 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3932 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3933 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3934 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3935 if (!bmap1 || !bmap2)
3937 bmap1 = isl_basic_map_cow(bmap1);
3938 bmap1 = isl_basic_map_extend(bmap1, nparam,
3939 pos, (dim1 - pos) + (dim2 - pos),
3940 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3941 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
3944 total = isl_basic_map_total_dim(bmap1);
3946 obj = isl_vec_alloc(ctx, 1 + total);
3947 isl_seq_clr(obj->block.data, 1 + total);
3948 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
3949 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
3954 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3955 if (res == isl_lp_empty)
3957 else if (res == isl_lp_ok && isl_int_is_pos(num))
3959 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3960 res == isl_lp_unbounded)
3966 isl_basic_map_free(bmap1);
3967 isl_vec_free(ctx, obj);
3970 isl_basic_map_free(bmap1);
3971 isl_basic_map_free(bmap2);
3975 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
3976 unsigned pos, isl_int *val)
3984 total = isl_basic_map_total_dim(bmap);
3985 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
3986 for (; d+1 > pos; --d)
3987 if (!isl_int_is_zero(bmap->eq[i][1+d]))
3991 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
3993 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
3995 if (!isl_int_is_one(bmap->eq[i][1+d]))
3998 isl_int_neg(*val, bmap->eq[i][0]);
4004 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4005 unsigned pos, isl_int *val)
4017 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4020 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4021 for (i = 1; fixed == 1 && i < map->n; ++i) {
4022 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4023 if (fixed == 1 && isl_int_ne(tmp, v))
4027 isl_int_set(*val, v);
4033 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4036 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4039 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4040 enum isl_dim_type type, unsigned pos, isl_int *val)
4042 if (pos >= isl_basic_map_dim(bmap, type))
4044 return isl_basic_map_fast_has_fixed_var(bmap,
4045 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4048 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4049 * then return this fixed value in *val.
4051 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4053 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4056 /* Check if input variable in has fixed value and if so and if val is not NULL,
4057 * then return this fixed value in *val.
4059 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4061 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4064 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4065 * and if val is not NULL, then return this lower bound in *val.
4067 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4068 unsigned dim, isl_int *val)
4070 int i, i_eq = -1, i_ineq = -1;
4077 total = isl_basic_set_total_dim(bset);
4078 nparam = isl_basic_set_n_param(bset);
4079 for (i = 0; i < bset->n_eq; ++i) {
4080 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4086 for (i = 0; i < bset->n_ineq; ++i) {
4087 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4089 if (i_eq != -1 || i_ineq != -1)
4093 if (i_eq == -1 && i_ineq == -1)
4095 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4096 /* The coefficient should always be one due to normalization. */
4097 if (!isl_int_is_one(c[1+nparam+dim]))
4099 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4101 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4102 total - nparam - dim - 1) != -1)
4105 isl_int_neg(*val, c[0]);
4109 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4110 unsigned dim, isl_int *val)
4122 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4126 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4128 for (i = 1; fixed == 1 && i < set->n; ++i) {
4129 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4131 if (fixed == 1 && isl_int_ne(tmp, v))
4135 isl_int_set(*val, v);
4146 static int qsort_constraint_cmp(const void *p1, const void *p2)
4148 const struct constraint *c1 = (const struct constraint *)p1;
4149 const struct constraint *c2 = (const struct constraint *)p2;
4150 unsigned size = isl_min(c1->size, c2->size);
4151 return isl_seq_cmp(c1->c, c2->c, size);
4154 static struct isl_basic_map *isl_basic_map_sort_constraints(
4155 struct isl_basic_map *bmap)
4158 struct constraint *c;
4163 total = isl_basic_map_total_dim(bmap);
4164 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4167 for (i = 0; i < bmap->n_ineq; ++i) {
4169 c[i].c = bmap->ineq[i];
4171 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4172 for (i = 0; i < bmap->n_ineq; ++i)
4173 bmap->ineq[i] = c[i].c;
4177 isl_basic_map_free(bmap);
4181 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4185 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4187 bmap = isl_basic_map_convex_hull(bmap);
4188 bmap = isl_basic_map_sort_constraints(bmap);
4189 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4193 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4195 return (struct isl_basic_set *)isl_basic_map_normalize(
4196 (struct isl_basic_map *)bset);
4199 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4200 const struct isl_basic_map *bmap2)
4207 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4208 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4209 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4210 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4211 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4212 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4213 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4214 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4216 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4218 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4220 if (bmap1->n_eq != bmap2->n_eq)
4221 return bmap1->n_eq - bmap2->n_eq;
4222 if (bmap1->n_ineq != bmap2->n_ineq)
4223 return bmap1->n_ineq - bmap2->n_ineq;
4224 if (bmap1->n_div != bmap2->n_div)
4225 return bmap1->n_div - bmap2->n_div;
4226 total = isl_basic_map_total_dim(bmap1);
4227 for (i = 0; i < bmap1->n_eq; ++i) {
4228 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4232 for (i = 0; i < bmap1->n_ineq; ++i) {
4233 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4237 for (i = 0; i < bmap1->n_div; ++i) {
4238 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4245 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4246 struct isl_basic_map *bmap2)
4248 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4251 static int qsort_bmap_cmp(const void *p1, const void *p2)
4253 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4254 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4256 return isl_basic_map_fast_cmp(bmap1, bmap2);
4259 /* We normalize in place, but if anything goes wrong we need
4260 * to return NULL, so we need to make sure we don't change the
4261 * meaning of any possible other copies of map.
4263 struct isl_map *isl_map_normalize(struct isl_map *map)
4266 struct isl_basic_map *bmap;
4270 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4272 for (i = 0; i < map->n; ++i) {
4273 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4276 isl_basic_map_free(map->p[i]);
4279 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4280 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4281 map = isl_map_remove_empty_parts(map);
4284 for (i = map->n - 1; i >= 1; --i) {
4285 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4287 isl_basic_map_free(map->p[i-1]);
4288 for (j = i; j < map->n; ++j)
4289 map->p[j-1] = map->p[j];
4299 struct isl_set *isl_set_normalize(struct isl_set *set)
4301 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4304 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4314 if (!isl_dim_equal(map1->dim, map2->dim))
4317 map1 = isl_map_copy(map1);
4318 map2 = isl_map_copy(map2);
4319 map1 = isl_map_normalize(map1);
4320 map2 = isl_map_normalize(map2);
4323 equal = map1->n == map2->n;
4324 for (i = 0; equal && i < map1->n; ++i) {
4325 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4338 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4340 return isl_map_fast_is_equal((struct isl_map *)set1,
4341 (struct isl_map *)set2);
4344 /* Return an interval that ranges from min to max (inclusive)
4346 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4347 isl_int min, isl_int max)
4350 struct isl_basic_set *bset = NULL;
4352 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4356 k = isl_basic_set_alloc_inequality(bset);
4359 isl_int_set_si(bset->ineq[k][1], 1);
4360 isl_int_neg(bset->ineq[k][0], min);
4362 k = isl_basic_set_alloc_inequality(bset);
4365 isl_int_set_si(bset->ineq[k][1], -1);
4366 isl_int_set(bset->ineq[k][0], max);
4370 isl_basic_set_free(bset);
4374 /* Return the Cartesian product of the basic sets in list (in the given order).
4376 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4384 struct isl_basic_set *product = NULL;
4388 isl_assert(list->ctx, list->n > 0, goto error);
4389 isl_assert(list->ctx, list->p[0], goto error);
4390 nparam = isl_basic_set_n_param(list->p[0]);
4391 dim = isl_basic_set_n_dim(list->p[0]);
4392 extra = list->p[0]->n_div;
4393 n_eq = list->p[0]->n_eq;
4394 n_ineq = list->p[0]->n_ineq;
4395 for (i = 1; i < list->n; ++i) {
4396 isl_assert(list->ctx, list->p[i], goto error);
4397 isl_assert(list->ctx,
4398 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4399 dim += isl_basic_set_n_dim(list->p[i]);
4400 extra += list->p[i]->n_div;
4401 n_eq += list->p[i]->n_eq;
4402 n_ineq += list->p[i]->n_ineq;
4404 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4409 for (i = 0; i < list->n; ++i) {
4410 isl_basic_set_add_constraints(product,
4411 isl_basic_set_copy(list->p[i]), dim);
4412 dim += isl_basic_set_n_dim(list->p[i]);
4414 isl_basic_set_list_free(list);
4417 isl_basic_set_free(product);
4418 isl_basic_set_list_free(list);
4422 struct isl_basic_map *isl_basic_map_product(
4423 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4425 struct isl_dim *dim_result = NULL;
4426 struct isl_basic_map *bmap;
4427 unsigned in1, in2, out1, out2, nparam, total, pos;
4428 struct isl_dim_map *dim_map1, *dim_map2;
4430 if (!bmap1 || !bmap2)
4433 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4434 bmap2->dim, isl_dim_param), goto error);
4435 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4436 isl_dim_copy(bmap2->dim));
4438 in1 = isl_basic_map_n_in(bmap1);
4439 in2 = isl_basic_map_n_in(bmap2);
4440 out1 = isl_basic_map_n_out(bmap1);
4441 out2 = isl_basic_map_n_out(bmap2);
4442 nparam = isl_basic_map_n_param(bmap1);
4444 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4445 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4446 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4447 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4448 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4449 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4450 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4451 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4452 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4453 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4454 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4456 bmap = isl_basic_map_alloc_dim(dim_result,
4457 bmap1->n_div + bmap2->n_div,
4458 bmap1->n_eq + bmap2->n_eq,
4459 bmap1->n_ineq + bmap2->n_ineq);
4460 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4461 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4462 bmap = isl_basic_map_simplify(bmap);
4463 return isl_basic_map_finalize(bmap);
4465 isl_basic_map_free(bmap1);
4466 isl_basic_map_free(bmap2);
4470 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4472 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4475 struct isl_map *result;
4481 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4482 map2->dim, isl_dim_param), goto error);
4484 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4485 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4486 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4488 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4489 isl_dim_copy(map2->dim)),
4490 map1->n * map2->n, flags);
4493 for (i = 0; i < map1->n; ++i)
4494 for (j = 0; j < map2->n; ++j) {
4495 struct isl_basic_map *part;
4496 part = isl_basic_map_product(
4497 isl_basic_map_copy(map1->p[i]),
4498 isl_basic_map_copy(map2->p[j]));
4499 if (isl_basic_map_is_empty(part))
4500 isl_basic_map_free(part);
4502 result = isl_map_add(result, part);
4515 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4523 bset = isl_basic_set_copy(bset);
4524 bset = isl_basic_set_normalize(bset);
4527 total = isl_basic_set_total_dim(bset);
4528 isl_hash_byte(hash, bset->n_eq & 0xFF);
4529 for (i = 0; i < bset->n_eq; ++i) {
4531 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4532 isl_hash_hash(hash, c_hash);
4534 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4535 for (i = 0; i < bset->n_ineq; ++i) {
4537 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4538 isl_hash_hash(hash, c_hash);
4540 isl_hash_byte(hash, bset->n_div & 0xFF);
4541 for (i = 0; i < bset->n_div; ++i) {
4543 if (isl_int_is_zero(bset->div[i][0]))
4545 isl_hash_byte(hash, i & 0xFF);
4546 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4547 isl_hash_hash(hash, c_hash);
4549 isl_basic_set_free(bset);
4553 uint32_t isl_set_get_hash(struct isl_set *set)
4560 set = isl_set_copy(set);
4561 set = isl_set_normalize(set);
4565 hash = isl_hash_init();
4566 for (i = 0; i < set->n; ++i) {
4568 bset_hash = isl_basic_set_get_hash(set->p[i]);
4569 isl_hash_hash(hash, bset_hash);
4577 /* Check if the value for dimension dim is completely determined
4578 * by the values of the other parameters and variables.
4579 * That is, check if dimension dim is involved in an equality.
4581 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4588 nparam = isl_basic_set_n_param(bset);
4589 for (i = 0; i < bset->n_eq; ++i)
4590 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4595 /* Check if the value for dimension dim is completely determined
4596 * by the values of the other parameters and variables.
4597 * That is, check if dimension dim is involved in an equality
4598 * for each of the subsets.
4600 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4606 for (i = 0; i < set->n; ++i) {
4608 unique = isl_basic_set_dim_is_unique(set->p[i], dim);