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 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
227 return isl_dim_copy(bset->dim);
230 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
231 struct isl_basic_map *bmap, unsigned extra,
232 unsigned n_eq, unsigned n_ineq)
235 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
237 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
238 if (isl_blk_is_error(bmap->block)) {
243 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
245 isl_blk_free(ctx, bmap->block);
251 bmap->block2 = isl_blk_empty();
254 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
255 if (isl_blk_is_error(bmap->block2)) {
257 isl_blk_free(ctx, bmap->block);
262 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
264 isl_blk_free(ctx, bmap->block2);
266 isl_blk_free(ctx, bmap->block);
272 for (i = 0; i < n_ineq + n_eq; ++i)
273 bmap->ineq[i] = bmap->block.data + i * row_size;
275 for (i = 0; i < extra; ++i)
276 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
282 bmap->c_size = n_eq + n_ineq;
283 bmap->eq = bmap->ineq + n_ineq;
292 isl_basic_map_free(bmap);
296 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
297 unsigned nparam, unsigned dim, unsigned extra,
298 unsigned n_eq, unsigned n_ineq)
300 struct isl_basic_map *bmap;
301 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
302 return (struct isl_basic_set *)bmap;
305 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
306 unsigned extra, unsigned n_eq, unsigned n_ineq)
308 struct isl_basic_map *bmap;
311 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
312 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
313 return (struct isl_basic_set *)bmap;
316 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
317 unsigned extra, unsigned n_eq, unsigned n_ineq)
319 struct isl_basic_map *bmap;
323 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
328 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
334 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
335 unsigned nparam, unsigned in, unsigned out, unsigned extra,
336 unsigned n_eq, unsigned n_ineq)
338 struct isl_basic_map *bmap;
341 dim = isl_dim_alloc(ctx, nparam, in, out);
345 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
349 static void dup_constraints(
350 struct isl_basic_map *dst, struct isl_basic_map *src)
353 unsigned total = isl_basic_map_total_dim(src);
355 for (i = 0; i < src->n_eq; ++i) {
356 int j = isl_basic_map_alloc_equality(dst);
357 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
360 for (i = 0; i < src->n_ineq; ++i) {
361 int j = isl_basic_map_alloc_inequality(dst);
362 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
365 for (i = 0; i < src->n_div; ++i) {
366 int j = isl_basic_map_alloc_div(dst);
367 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
369 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
372 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
374 struct isl_basic_map *dup;
378 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
379 bmap->n_div, bmap->n_eq, bmap->n_ineq);
382 dup_constraints(dup, bmap);
383 dup->flags = bmap->flags;
384 dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
388 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
390 struct isl_basic_map *dup;
392 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
393 return (struct isl_basic_set *)dup;
396 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
401 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
405 return isl_basic_set_dup(bset);
408 struct isl_set *isl_set_copy(struct isl_set *set)
417 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
422 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
426 return isl_basic_map_dup(bmap);
429 struct isl_map *isl_map_copy(struct isl_map *map)
438 void isl_basic_map_free(struct isl_basic_map *bmap)
446 isl_ctx_deref(bmap->ctx);
448 isl_blk_free(bmap->ctx, bmap->block2);
450 isl_blk_free(bmap->ctx, bmap->block);
451 isl_vec_free(bmap->ctx, bmap->sample);
452 isl_dim_free(bmap->dim);
456 void isl_basic_set_free(struct isl_basic_set *bset)
458 isl_basic_map_free((struct isl_basic_map *)bset);
461 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
463 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
466 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
472 isl_assert(ctx, room_for_con(bmap, 1), return -1);
473 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
475 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
476 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
478 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
479 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
480 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
482 int j = isl_basic_map_alloc_inequality(bmap);
486 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
487 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
494 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
495 bmap->extra - bmap->n_div);
499 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
501 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
504 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
508 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
513 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
515 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
518 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
523 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
525 if (pos != bmap->n_eq - 1) {
527 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
528 bmap->eq[bmap->n_eq - 1] = t;
534 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
536 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
539 void isl_basic_map_inequality_to_equality(
540 struct isl_basic_map *bmap, unsigned pos)
545 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
546 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
551 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
552 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
553 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
554 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
557 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
559 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
562 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
568 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
569 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
570 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
571 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
572 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
573 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
574 1 + isl_basic_map_total_dim(bmap),
575 bmap->extra - bmap->n_div);
576 return bmap->n_ineq++;
579 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
581 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
584 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
588 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
593 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
595 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
598 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
603 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
605 if (pos != bmap->n_ineq - 1) {
607 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
608 bmap->ineq[bmap->n_ineq - 1] = t;
609 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
615 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
617 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
620 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
624 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
625 isl_seq_clr(bmap->div[bmap->n_div] +
626 1 + 1 + isl_basic_map_total_dim(bmap),
627 bmap->extra - bmap->n_div);
628 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
629 return bmap->n_div++;
632 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
634 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
637 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
641 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
646 /* Copy constraint from src to dst, putting the vars of src at offset
647 * dim_off in dst and the divs of src at offset div_off in dst.
648 * If both sets are actually map, then dim_off applies to the input
651 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
652 struct isl_basic_map *src_map, isl_int *src,
653 unsigned in_off, unsigned out_off, unsigned div_off)
655 unsigned src_nparam = isl_basic_map_n_param(src_map);
656 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
657 unsigned src_in = isl_basic_map_n_in(src_map);
658 unsigned dst_in = isl_basic_map_n_in(dst_map);
659 unsigned src_out = isl_basic_map_n_out(src_map);
660 unsigned dst_out = isl_basic_map_n_out(dst_map);
661 isl_int_set(dst[0], src[0]);
662 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
663 if (dst_nparam > src_nparam)
664 isl_seq_clr(dst+1+src_nparam,
665 dst_nparam - src_nparam);
666 isl_seq_clr(dst+1+dst_nparam, in_off);
667 isl_seq_cpy(dst+1+dst_nparam+in_off,
669 isl_min(dst_in-in_off, src_in));
670 if (dst_in-in_off > src_in)
671 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
672 dst_in - in_off - src_in);
673 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
674 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
675 src+1+src_nparam+src_in,
676 isl_min(dst_out-out_off, src_out));
677 if (dst_out-out_off > src_out)
678 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
679 dst_out - out_off - src_out);
680 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
681 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
682 src+1+src_nparam+src_in+src_out,
683 isl_min(dst_map->extra-div_off, src_map->n_div));
684 if (dst_map->n_div-div_off > src_map->n_div)
685 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
686 div_off+src_map->n_div,
687 dst_map->n_div - div_off - src_map->n_div);
690 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
691 struct isl_basic_map *src_map, isl_int *src,
692 unsigned in_off, unsigned out_off, unsigned div_off)
694 isl_int_set(dst[0], src[0]);
695 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
698 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
699 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
704 if (!bmap1 || !bmap2)
707 div_off = bmap1->n_div;
709 for (i = 0; i < bmap2->n_eq; ++i) {
710 int i1 = isl_basic_map_alloc_equality(bmap1);
713 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
714 i_pos, o_pos, div_off);
717 for (i = 0; i < bmap2->n_ineq; ++i) {
718 int i1 = isl_basic_map_alloc_inequality(bmap1);
721 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
722 i_pos, o_pos, div_off);
725 for (i = 0; i < bmap2->n_div; ++i) {
726 int i1 = isl_basic_map_alloc_div(bmap1);
729 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
730 i_pos, o_pos, div_off);
733 isl_basic_map_free(bmap2);
738 isl_basic_map_free(bmap1);
739 isl_basic_map_free(bmap2);
743 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
744 struct isl_dim_map *dim_map)
748 for (i = 0; i < dim_map->len; ++i) {
749 if (dim_map->pos[i] < 0)
750 isl_int_set_si(dst[i], 0);
752 isl_int_set(dst[i], src[dim_map->pos[i]]);
756 static void copy_div_dim_map(isl_int *dst, isl_int *src,
757 struct isl_dim_map *dim_map)
759 isl_int_set(dst[0], src[0]);
760 copy_constraint_dim_map(dst+1, src+1, dim_map);
763 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
764 struct isl_basic_map *src, struct isl_dim_map *dim_map)
768 if (!src || !dst || !dim_map)
771 for (i = 0; i < src->n_eq; ++i) {
772 int i1 = isl_basic_map_alloc_equality(dst);
775 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
778 for (i = 0; i < src->n_ineq; ++i) {
779 int i1 = isl_basic_map_alloc_inequality(dst);
782 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
785 for (i = 0; i < src->n_div; ++i) {
786 int i1 = isl_basic_map_alloc_div(dst);
789 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
793 isl_basic_map_free(src);
798 isl_basic_map_free(src);
799 isl_basic_map_free(dst);
803 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
804 struct isl_basic_set *bset2, unsigned pos)
806 return (struct isl_basic_set *)
807 add_constraints((struct isl_basic_map *)bset1,
808 (struct isl_basic_map *)bset2, 0, pos);
811 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
812 struct isl_dim *dim, unsigned extra,
813 unsigned n_eq, unsigned n_ineq)
815 struct isl_basic_map *ext;
825 dims_ok = isl_dim_equal(base->dim, dim) &&
826 base->extra >= base->n_div + extra;
828 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
829 room_for_ineq(base, n_ineq)) {
834 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
835 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
836 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
837 extra += base->extra;
839 n_ineq += base->n_ineq;
841 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
847 ext = add_constraints(ext, base, 0, 0);
850 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
857 isl_basic_map_free(base);
861 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
862 struct isl_dim *dim, unsigned extra,
863 unsigned n_eq, unsigned n_ineq)
865 return (struct isl_basic_set *)
866 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
867 extra, n_eq, n_ineq);
870 struct isl_basic_map *isl_basic_map_extend_constraints(
871 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
875 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
879 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
880 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
881 unsigned n_eq, unsigned n_ineq)
883 struct isl_basic_map *bmap;
888 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
892 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
896 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
897 unsigned nparam, unsigned dim, unsigned extra,
898 unsigned n_eq, unsigned n_ineq)
900 return (struct isl_basic_set *)
901 isl_basic_map_extend((struct isl_basic_map *)base,
902 nparam, 0, dim, extra, n_eq, n_ineq);
905 struct isl_basic_set *isl_basic_set_extend_constraints(
906 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
908 return (struct isl_basic_set *)
909 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
913 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
915 return (struct isl_basic_set *)
916 isl_basic_map_cow((struct isl_basic_map *)bset);
919 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
926 bmap = isl_basic_map_dup(bmap);
928 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
932 struct isl_set *isl_set_cow(struct isl_set *set)
940 return isl_set_dup(set);
943 struct isl_map *isl_map_cow(struct isl_map *map)
951 return isl_map_dup(map);
954 static void swap_vars(struct isl_blk blk, isl_int *a,
955 unsigned a_len, unsigned b_len)
957 isl_seq_cpy(blk.data, a+a_len, b_len);
958 isl_seq_cpy(blk.data+b_len, a, a_len);
959 isl_seq_cpy(a, blk.data, b_len+a_len);
962 struct isl_basic_set *isl_basic_set_swap_vars(
963 struct isl_basic_set *bset, unsigned n)
973 nparam = isl_basic_set_n_param(bset);
974 dim = isl_basic_set_n_dim(bset);
975 isl_assert(bset->ctx, n <= dim, goto error);
980 bset = isl_basic_set_cow(bset);
984 blk = isl_blk_alloc(bset->ctx, dim);
985 if (isl_blk_is_error(blk))
988 for (i = 0; i < bset->n_eq; ++i)
990 bset->eq[i]+1+nparam, n, dim - n);
992 for (i = 0; i < bset->n_ineq; ++i)
994 bset->ineq[i]+1+nparam, n, dim - n);
996 for (i = 0; i < bset->n_div; ++i)
998 bset->div[i]+1+1+nparam, n, dim - n);
1000 isl_blk_free(bset->ctx, blk);
1002 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1006 isl_basic_set_free(bset);
1010 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1013 set = isl_set_cow(set);
1017 for (i = 0; i < set->n; ++i) {
1018 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1024 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1028 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1034 total = isl_basic_map_total_dim(bmap);
1035 isl_basic_map_free_div(bmap, bmap->n_div);
1036 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1038 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1040 isl_basic_map_alloc_equality(bmap);
1044 isl_int_set_si(bmap->eq[i][0], 1);
1045 isl_seq_clr(bmap->eq[i]+1, total);
1046 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1047 return isl_basic_map_finalize(bmap);
1049 isl_basic_map_free(bmap);
1053 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1055 return (struct isl_basic_set *)
1056 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1059 static void swap_div(struct isl_basic_map *bmap, int a, int b)
1062 unsigned off = isl_dim_total(bmap->dim);
1063 isl_int *t = bmap->div[a];
1064 bmap->div[a] = bmap->div[b];
1067 for (i = 0; i < bmap->n_eq; ++i)
1068 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1070 for (i = 0; i < bmap->n_ineq; ++i)
1071 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1073 for (i = 0; i < bmap->n_div; ++i)
1074 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1075 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1078 /* Eliminate the specified n dimensions starting at first from the
1079 * constraints using Fourier-Motzkin, The dimensions themselves
1082 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1083 unsigned first, unsigned n)
1093 set = isl_set_cow(set);
1096 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1097 nparam = isl_set_n_param(set);
1099 for (i = 0; i < set->n; ++i) {
1100 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1111 /* Project out n dimensions starting at first using Fourier-Motzkin */
1112 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1113 unsigned first, unsigned n)
1115 set = isl_set_eliminate_dims(set, first, n);
1116 set = isl_set_drop_dims(set, first, n);
1120 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1122 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1130 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1139 set = isl_set_cow(set);
1143 for (i = 0; i < set->n; ++i) {
1144 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1154 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1155 enum isl_dim_type type, unsigned first, unsigned n)
1159 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1163 bmap = isl_basic_map_eliminate_vars(bmap,
1164 isl_basic_map_offset(bmap, type) - 1 + first, n);
1165 bmap = isl_basic_map_drop(bmap, type, first, n);
1168 isl_basic_map_free(bmap);
1172 struct isl_map *isl_map_remove(struct isl_map *map,
1173 enum isl_dim_type type, unsigned first, unsigned n)
1181 map = isl_map_cow(map);
1184 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1186 for (i = 0; i < map->n; ++i) {
1187 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1188 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1192 map = isl_map_drop(map, type, first, n);
1199 /* Project out n inputs starting at first using Fourier-Motzkin */
1200 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1201 unsigned first, unsigned n)
1203 return isl_map_remove(map, isl_dim_in, first, n);
1206 /* Project out n dimensions starting at first using Fourier-Motzkin */
1207 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1208 unsigned first, unsigned n)
1210 unsigned nparam = isl_basic_set_n_param(bset);
1211 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1212 bset = isl_basic_set_drop_dims(bset, first, n);
1216 static void dump_term(struct isl_basic_map *bmap,
1217 isl_int c, int pos, FILE *out)
1220 unsigned in = isl_basic_map_n_in(bmap);
1221 unsigned dim = in + isl_basic_map_n_out(bmap);
1222 unsigned nparam = isl_basic_map_n_param(bmap);
1224 isl_int_print(out, c, 0);
1226 if (!isl_int_is_one(c))
1227 isl_int_print(out, c, 0);
1228 if (pos < 1 + nparam) {
1229 name = isl_dim_get_name(bmap->dim,
1230 isl_dim_param, pos - 1);
1232 fprintf(out, "%s", name);
1234 fprintf(out, "p%d", pos - 1);
1235 } else if (pos < 1 + nparam + in)
1236 fprintf(out, "i%d", pos - 1 - nparam);
1237 else if (pos < 1 + nparam + dim)
1238 fprintf(out, "o%d", pos - 1 - nparam - in);
1240 fprintf(out, "e%d", pos - 1 - nparam - dim);
1244 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1245 int sign, FILE *out)
1249 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1253 for (i = 0, first = 1; i < len; ++i) {
1254 if (isl_int_sgn(c[i]) * sign <= 0)
1257 fprintf(out, " + ");
1259 isl_int_abs(v, c[i]);
1260 dump_term(bmap, v, i, out);
1267 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1268 const char *op, FILE *out, int indent)
1272 fprintf(out, "%*s", indent, "");
1274 dump_constraint_sign(bmap, c, 1, out);
1275 fprintf(out, " %s ", op);
1276 dump_constraint_sign(bmap, c, -1, out);
1280 for (i = bmap->n_div; i < bmap->extra; ++i) {
1281 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1283 fprintf(out, "%*s", indent, "");
1284 fprintf(out, "ERROR: unused div coefficient not zero\n");
1289 static void dump_constraints(struct isl_basic_map *bmap,
1290 isl_int **c, unsigned n,
1291 const char *op, FILE *out, int indent)
1295 for (i = 0; i < n; ++i)
1296 dump_constraint(bmap, c[i], op, out, indent);
1299 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1303 unsigned total = isl_basic_map_total_dim(bmap);
1305 for (j = 0; j < 1 + total; ++j) {
1306 if (isl_int_is_zero(exp[j]))
1308 if (!first && isl_int_is_pos(exp[j]))
1310 dump_term(bmap, exp[j], j, out);
1315 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1319 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1320 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1322 for (i = 0; i < bmap->n_div; ++i) {
1323 fprintf(out, "%*s", indent, "");
1324 fprintf(out, "e%d = [(", i);
1325 dump_affine(bmap, bmap->div[i]+1, out);
1327 isl_int_print(out, bmap->div[i][0], 0);
1328 fprintf(out, "]\n");
1332 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1335 fprintf(out, "null basic set\n");
1339 fprintf(out, "%*s", indent, "");
1340 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1341 bset->ref, bset->dim->nparam, bset->dim->n_out,
1342 bset->extra, bset->flags);
1343 dump((struct isl_basic_map *)bset, out, indent);
1346 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1349 fprintf(out, "null basic map\n");
1353 fprintf(out, "%*s", indent, "");
1354 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1355 "flags: %x, n_name: %d\n",
1357 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1358 bmap->extra, bmap->flags, bmap->dim->n_name);
1359 dump(bmap, out, indent);
1362 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1367 total = isl_basic_map_total_dim(bmap);
1368 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1369 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1370 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1371 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1375 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1377 struct isl_set *set;
1381 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1382 isl_assert(dim->ctx, n >= 0, return NULL);
1383 set = isl_alloc(dim->ctx, struct isl_set,
1384 sizeof(struct isl_set) +
1385 n * sizeof(struct isl_basic_set *));
1389 set->ctx = dim->ctx;
1390 isl_ctx_ref(set->ctx);
1402 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1403 unsigned nparam, unsigned dim, int n, unsigned flags)
1405 struct isl_set *set;
1406 struct isl_dim *dims;
1408 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1412 set = isl_set_alloc_dim(dims, n, flags);
1416 struct isl_set *isl_set_dup(struct isl_set *set)
1419 struct isl_set *dup;
1424 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1427 for (i = 0; i < set->n; ++i)
1428 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1432 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1434 struct isl_set *set;
1439 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1441 isl_basic_set_free(bset);
1444 return isl_set_add(set, bset);
1447 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1449 struct isl_map *map;
1454 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1456 isl_basic_map_free(bmap);
1459 return isl_map_add(map, bmap);
1462 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1466 isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1467 isl_assert(set->ctx, set->n < set->size, goto error);
1468 set->p[set->n] = bset;
1475 isl_basic_set_free(bset);
1479 void isl_set_free(struct isl_set *set)
1489 isl_ctx_deref(set->ctx);
1490 for (i = 0; i < set->n; ++i)
1491 isl_basic_set_free(set->p[i]);
1492 isl_dim_free(set->dim);
1496 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1501 fprintf(out, "null set\n");
1505 fprintf(out, "%*s", indent, "");
1506 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1507 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1509 for (i = 0; i < set->n; ++i) {
1510 fprintf(out, "%*s", indent, "");
1511 fprintf(out, "basic set %d:\n", i);
1512 isl_basic_set_dump(set->p[i], out, indent+4);
1516 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1521 fprintf(out, "null map\n");
1525 fprintf(out, "%*s", indent, "");
1526 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1527 "flags: %x, n_name: %d\n",
1528 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1529 map->dim->n_out, map->flags, map->dim->n_name);
1530 for (i = 0; i < map->n; ++i) {
1531 fprintf(out, "%*s", indent, "");
1532 fprintf(out, "basic map %d:\n", i);
1533 isl_basic_map_dump(map->p[i], out, indent+4);
1537 struct isl_basic_map *isl_basic_map_intersect_domain(
1538 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1540 struct isl_basic_map *bmap_domain;
1541 struct isl_dim *dim;
1546 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1547 bset->dim, isl_dim_param), goto error);
1549 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1550 isl_assert(bset->ctx,
1551 isl_basic_map_compatible_domain(bmap, bset), goto error);
1553 bmap = isl_basic_map_cow(bmap);
1554 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1555 bset->n_div, bset->n_eq, bset->n_ineq);
1558 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1559 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1560 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1562 bmap = isl_basic_map_simplify(bmap);
1563 return isl_basic_map_finalize(bmap);
1565 isl_basic_map_free(bmap);
1566 isl_basic_set_free(bset);
1570 struct isl_basic_map *isl_basic_map_intersect_range(
1571 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1573 struct isl_basic_map *bmap_range;
1578 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1579 bset->dim, isl_dim_param), goto error);
1581 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1582 isl_assert(bset->ctx,
1583 isl_basic_map_compatible_range(bmap, bset), goto error);
1585 bmap = isl_basic_map_cow(bmap);
1586 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1587 bset->n_div, bset->n_eq, bset->n_ineq);
1590 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1591 bmap = add_constraints(bmap, bmap_range, 0, 0);
1593 bmap = isl_basic_map_simplify(bmap);
1594 return isl_basic_map_finalize(bmap);
1596 isl_basic_map_free(bmap);
1597 isl_basic_set_free(bset);
1601 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1607 total = 1 + isl_basic_map_total_dim(bmap);
1608 if (total != vec->size)
1613 for (i = 0; i < bmap->n_eq; ++i) {
1614 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
1615 if (!isl_int_is_zero(s)) {
1621 for (i = 0; i < bmap->n_ineq; ++i) {
1622 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
1623 if (isl_int_is_neg(s)) {
1634 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1636 return basic_map_contains((struct isl_basic_map *)bset, vec);
1639 struct isl_basic_map *isl_basic_map_intersect(
1640 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1642 struct isl_vec *sample = NULL;
1644 if (!bmap1 || !bmap2)
1647 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1648 bmap2->dim, isl_dim_param), goto error);
1649 if (isl_dim_total(bmap1->dim) ==
1650 isl_dim_size(bmap1->dim, isl_dim_param) &&
1651 isl_dim_total(bmap2->dim) !=
1652 isl_dim_size(bmap2->dim, isl_dim_param))
1653 return isl_basic_map_intersect(bmap2, bmap1);
1655 if (isl_dim_total(bmap2->dim) !=
1656 isl_dim_size(bmap2->dim, isl_dim_param))
1657 isl_assert(bmap1->ctx,
1658 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1660 if (bmap1->sample &&
1661 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1662 basic_map_contains(bmap2, bmap1->sample) > 0)
1663 sample = isl_vec_copy(bmap1->ctx, bmap1->sample);
1664 else if (bmap2->sample &&
1665 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1666 basic_map_contains(bmap2, bmap2->sample) > 0)
1667 sample = isl_vec_copy(bmap2->ctx, bmap2->sample);
1669 bmap1 = isl_basic_map_cow(bmap1);
1670 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1671 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1674 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1677 isl_vec_free(bmap1->ctx, bmap1->sample);
1678 bmap1->sample = sample;
1681 bmap1 = isl_basic_map_simplify(bmap1);
1682 return isl_basic_map_finalize(bmap1);
1685 isl_vec_free(bmap1->ctx, sample);
1686 isl_basic_map_free(bmap1);
1687 isl_basic_map_free(bmap2);
1691 struct isl_basic_set *isl_basic_set_intersect(
1692 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1694 return (struct isl_basic_set *)
1695 isl_basic_map_intersect(
1696 (struct isl_basic_map *)bset1,
1697 (struct isl_basic_map *)bset2);
1700 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1703 struct isl_map *result;
1709 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1710 map2->dim, isl_dim_param), goto error);
1711 if (isl_dim_total(map1->dim) ==
1712 isl_dim_size(map1->dim, isl_dim_param) &&
1713 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1714 return isl_map_intersect(map2, map1);
1716 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1717 isl_assert(map1->ctx,
1718 isl_dim_equal(map1->dim, map2->dim), goto error);
1720 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1721 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1722 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1724 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1725 map1->n * map2->n, flags);
1728 for (i = 0; i < map1->n; ++i)
1729 for (j = 0; j < map2->n; ++j) {
1730 struct isl_basic_map *part;
1731 part = isl_basic_map_intersect(
1732 isl_basic_map_copy(map1->p[i]),
1733 isl_basic_map_copy(map2->p[j]));
1734 if (isl_basic_map_is_empty(part))
1735 isl_basic_map_free(part);
1737 result = isl_map_add(result, part);
1750 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1752 return (struct isl_set *)
1753 isl_map_intersect((struct isl_map *)set1,
1754 (struct isl_map *)set2);
1757 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1759 struct isl_dim *dim;
1760 struct isl_basic_set *bset;
1765 bmap = isl_basic_map_cow(bmap);
1768 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1769 in = isl_basic_map_n_in(bmap);
1770 bset = isl_basic_set_from_basic_map(bmap);
1771 bset = isl_basic_set_swap_vars(bset, in);
1772 return isl_basic_map_from_basic_set(bset, dim);
1775 /* Turn final n dimensions into existentially quantified variables.
1777 struct isl_basic_set *isl_basic_set_project_out(
1778 struct isl_basic_set *bset, unsigned n, unsigned flags)
1788 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1793 bset = isl_basic_set_cow(bset);
1795 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1796 old = bset->block2.data;
1797 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1798 (bset->extra + n) * (1 + row_size));
1799 if (!bset->block2.data)
1801 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1804 for (i = 0; i < n; ++i) {
1805 new_div[i] = bset->block2.data +
1806 (bset->extra + i) * (1 + row_size);
1807 isl_seq_clr(new_div[i], 1 + row_size);
1809 for (i = 0; i < bset->extra; ++i)
1810 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1812 bset->div = new_div;
1815 bset->dim = isl_dim_drop_outputs(bset->dim,
1816 isl_basic_set_n_dim(bset) - n, n);
1819 bset = isl_basic_set_simplify(bset);
1820 return isl_basic_set_finalize(bset);
1822 isl_basic_set_free(bset);
1826 struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1830 for (i = 0; i < n; ++i) {
1831 j = isl_basic_map_alloc_div(bmap);
1834 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1838 isl_basic_map_free(bmap);
1842 struct isl_basic_map *isl_basic_map_apply_range(
1843 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1845 struct isl_dim *dim_result = NULL;
1846 struct isl_basic_map *bmap;
1847 unsigned n_in, n_out, n, nparam, total, pos;
1848 struct isl_dim_map *dim_map1, *dim_map2;
1850 if (!bmap1 || !bmap2)
1853 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1854 isl_dim_copy(bmap2->dim));
1856 n_in = isl_basic_map_n_in(bmap1);
1857 n_out = isl_basic_map_n_out(bmap2);
1858 n = isl_basic_map_n_out(bmap1);
1859 nparam = isl_basic_map_n_param(bmap1);
1861 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1862 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1863 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1864 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1865 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1866 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1867 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1868 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1869 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1870 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1871 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1873 bmap = isl_basic_map_alloc_dim(dim_result,
1874 bmap1->n_div + bmap2->n_div + n,
1875 bmap1->n_eq + bmap2->n_eq,
1876 bmap1->n_ineq + bmap2->n_ineq);
1877 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1878 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1879 bmap = add_divs(bmap, n);
1880 bmap = isl_basic_map_simplify(bmap);
1881 return isl_basic_map_finalize(bmap);
1883 isl_basic_map_free(bmap1);
1884 isl_basic_map_free(bmap2);
1888 struct isl_basic_set *isl_basic_set_apply(
1889 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1894 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1897 return (struct isl_basic_set *)
1898 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1900 isl_basic_set_free(bset);
1901 isl_basic_map_free(bmap);
1905 struct isl_basic_map *isl_basic_map_apply_domain(
1906 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1908 if (!bmap1 || !bmap2)
1912 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1914 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1917 bmap1 = isl_basic_map_reverse(bmap1);
1918 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1919 return isl_basic_map_reverse(bmap1);
1921 isl_basic_map_free(bmap1);
1922 isl_basic_map_free(bmap2);
1926 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1927 * A \cap B -> f(A) + f(B)
1929 struct isl_basic_map *isl_basic_map_sum(
1930 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1932 unsigned n_in, n_out, nparam, total, pos;
1933 struct isl_basic_map *bmap = NULL;
1934 struct isl_dim_map *dim_map1, *dim_map2;
1937 if (!bmap1 || !bmap2)
1940 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1943 nparam = isl_basic_map_n_param(bmap1);
1944 n_in = isl_basic_map_n_in(bmap1);
1945 n_out = isl_basic_map_n_out(bmap1);
1947 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
1948 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1949 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
1950 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1951 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
1952 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1953 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1954 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
1955 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1956 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1957 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
1959 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
1960 bmap1->n_div + bmap2->n_div + 2 * n_out,
1961 bmap1->n_eq + bmap2->n_eq + n_out,
1962 bmap1->n_ineq + bmap2->n_ineq);
1963 for (i = 0; i < n_out; ++i) {
1964 int j = isl_basic_map_alloc_equality(bmap);
1967 isl_seq_clr(bmap->eq[j], 1+total);
1968 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
1969 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
1970 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
1972 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1973 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1974 bmap = add_divs(bmap, 2 * n_out);
1976 bmap = isl_basic_map_simplify(bmap);
1977 return isl_basic_map_finalize(bmap);
1979 isl_basic_map_free(bmap);
1980 isl_basic_map_free(bmap1);
1981 isl_basic_map_free(bmap2);
1985 /* Given a basic map A -> f(A), construct A -> -f(A).
1987 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
1992 bmap = isl_basic_map_cow(bmap);
1996 n = isl_basic_map_dim(bmap, isl_dim_out);
1997 off = isl_basic_map_offset(bmap, isl_dim_out);
1998 for (i = 0; i < bmap->n_eq; ++i)
1999 for (j = 0; j < n; ++j)
2000 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2001 for (i = 0; i < bmap->n_ineq; ++i)
2002 for (j = 0; j < n; ++j)
2003 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2004 for (i = 0; i < bmap->n_div; ++i)
2005 for (j = 0; j < n; ++j)
2006 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2007 return isl_basic_map_finalize(bmap);
2010 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2011 * A -> floor(f(A)/d).
2013 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2016 unsigned n_in, n_out, nparam, total, pos;
2017 struct isl_basic_map *result = NULL;
2018 struct isl_dim_map *dim_map;
2024 nparam = isl_basic_map_n_param(bmap);
2025 n_in = isl_basic_map_n_in(bmap);
2026 n_out = isl_basic_map_n_out(bmap);
2028 total = nparam + n_in + n_out + bmap->n_div + n_out;
2029 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2030 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2031 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2032 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2033 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2035 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2036 bmap->n_div + n_out,
2037 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2038 result = add_constraints_dim_map(result, bmap, dim_map);
2039 result = add_divs(result, n_out);
2040 for (i = 0; i < n_out; ++i) {
2042 j = isl_basic_map_alloc_inequality(result);
2045 isl_seq_clr(result->ineq[j], 1+total);
2046 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2047 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2048 j = isl_basic_map_alloc_inequality(result);
2051 isl_seq_clr(result->ineq[j], 1+total);
2052 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2053 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2054 isl_int_sub_ui(result->ineq[j][0], d, 1);
2057 result = isl_basic_map_simplify(result);
2058 return isl_basic_map_finalize(result);
2060 isl_basic_map_free(result);
2064 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2070 i = isl_basic_map_alloc_equality(bmap);
2073 nparam = isl_basic_map_n_param(bmap);
2074 n_in = isl_basic_map_n_in(bmap);
2075 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2076 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2077 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2078 return isl_basic_map_finalize(bmap);
2080 isl_basic_map_free(bmap);
2084 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2090 i = isl_basic_map_alloc_inequality(bmap);
2093 nparam = isl_basic_map_n_param(bmap);
2094 n_in = isl_basic_map_n_in(bmap);
2095 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2096 isl_int_set_si(bmap->ineq[i][0], -1);
2097 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2098 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2099 return isl_basic_map_finalize(bmap);
2101 isl_basic_map_free(bmap);
2105 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2111 i = isl_basic_map_alloc_inequality(bmap);
2114 nparam = isl_basic_map_n_param(bmap);
2115 n_in = isl_basic_map_n_in(bmap);
2116 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2117 isl_int_set_si(bmap->ineq[i][0], -1);
2118 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2119 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2120 return isl_basic_map_finalize(bmap);
2122 isl_basic_map_free(bmap);
2126 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2129 struct isl_basic_map *bmap;
2130 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2133 for (i = 0; i < n_equal && bmap; ++i)
2134 bmap = var_equal(bmap, i);
2135 return isl_basic_map_finalize(bmap);
2138 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2141 struct isl_basic_map *bmap;
2142 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2145 for (i = 0; i < pos && bmap; ++i)
2146 bmap = var_equal(bmap, i);
2148 bmap = var_less(bmap, pos);
2149 return isl_basic_map_finalize(bmap);
2152 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2155 struct isl_basic_map *bmap;
2156 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2159 for (i = 0; i < pos && bmap; ++i)
2160 bmap = var_equal(bmap, i);
2162 bmap = var_more(bmap, pos);
2163 return isl_basic_map_finalize(bmap);
2166 struct isl_basic_map *isl_basic_map_from_basic_set(
2167 struct isl_basic_set *bset, struct isl_dim *dim)
2169 struct isl_basic_map *bmap;
2171 bset = isl_basic_set_cow(bset);
2175 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2176 isl_dim_free(bset->dim);
2177 bmap = (struct isl_basic_map *) bset;
2179 return isl_basic_map_finalize(bmap);
2181 isl_basic_set_free(bset);
2186 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2190 if (bmap->dim->n_in == 0)
2191 return (struct isl_basic_set *)bmap;
2192 bmap = isl_basic_map_cow(bmap);
2195 bmap->dim = isl_dim_cow(bmap->dim);
2198 bmap->dim->n_out += bmap->dim->n_in;
2199 bmap->dim->n_in = 0;
2200 bmap = isl_basic_map_finalize(bmap);
2201 return (struct isl_basic_set *)bmap;
2203 isl_basic_map_free(bmap);
2207 /* For a div d = floor(f/m), add the constraints
2210 * -(f-(n-1)) + m d >= 0
2212 * Note that the second constraint is the negation of
2216 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2219 unsigned total = isl_basic_map_total_dim(bmap);
2220 unsigned div_pos = 1 + total - bmap->n_div + div;
2222 i = isl_basic_map_alloc_inequality(bmap);
2225 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2226 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2228 j = isl_basic_map_alloc_inequality(bmap);
2231 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2232 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2233 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2237 struct isl_basic_set *isl_basic_map_underlying_set(
2238 struct isl_basic_map *bmap)
2242 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2243 return (struct isl_basic_set *)bmap;
2244 bmap = isl_basic_map_cow(bmap);
2247 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2250 bmap->extra -= bmap->n_div;
2252 bmap = isl_basic_map_finalize(bmap);
2253 return (struct isl_basic_set *)bmap;
2258 struct isl_basic_map *isl_basic_map_overlying_set(
2259 struct isl_basic_set *bset, struct isl_basic_map *like)
2261 struct isl_basic_map *bmap;
2262 struct isl_ctx *ctx;
2269 isl_assert(ctx, bset->n_div == 0, goto error);
2270 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2271 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2273 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2274 isl_basic_map_free(like);
2275 return (struct isl_basic_map *)bset;
2277 bset = isl_basic_set_cow(bset);
2280 total = bset->dim->n_out + bset->extra;
2281 bmap = (struct isl_basic_map *)bset;
2282 isl_dim_free(bmap->dim);
2283 bmap->dim = isl_dim_copy(like->dim);
2286 bmap->n_div = like->n_div;
2287 bmap->extra += like->n_div;
2290 ltotal = total - bmap->extra + like->extra;
2293 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2294 bmap->extra * (1 + 1 + total));
2295 if (isl_blk_is_error(bmap->block2))
2297 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2301 for (i = 0; i < bmap->extra; ++i)
2302 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2303 for (i = 0; i < like->n_div; ++i) {
2304 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2305 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2307 bmap = isl_basic_map_extend_constraints(bmap,
2308 0, 2 * like->n_div);
2309 for (i = 0; i < like->n_div; ++i) {
2310 if (isl_int_is_zero(bmap->div[i][0]))
2312 if (add_div_constraints(bmap, i) < 0)
2316 isl_basic_map_free(like);
2317 bmap = isl_basic_map_simplify(bmap);
2318 bmap = isl_basic_map_finalize(bmap);
2321 isl_basic_map_free(like);
2322 isl_basic_set_free(bset);
2326 struct isl_basic_set *isl_basic_set_from_underlying_set(
2327 struct isl_basic_set *bset, struct isl_basic_set *like)
2329 return (struct isl_basic_set *)
2330 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2333 struct isl_set *isl_set_from_underlying_set(
2334 struct isl_set *set, struct isl_basic_set *like)
2340 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2342 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2343 isl_basic_set_free(like);
2346 set = isl_set_cow(set);
2349 for (i = 0; i < set->n; ++i) {
2350 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2351 isl_basic_set_copy(like));
2355 isl_dim_free(set->dim);
2356 set->dim = isl_dim_copy(like->dim);
2359 isl_basic_set_free(like);
2362 isl_basic_set_free(like);
2367 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2371 map = isl_map_cow(map);
2374 map->dim = isl_dim_cow(map->dim);
2378 for (i = 1; i < map->n; ++i)
2379 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2381 for (i = 0; i < map->n; ++i) {
2382 map->p[i] = (struct isl_basic_map *)
2383 isl_basic_map_underlying_set(map->p[i]);
2388 map->dim = isl_dim_underlying(map->dim, 0);
2390 isl_dim_free(map->dim);
2391 map->dim = isl_dim_copy(map->p[0]->dim);
2395 return (struct isl_set *)map;
2401 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2403 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2406 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2408 struct isl_basic_set *domain;
2412 n_out = isl_basic_map_n_out(bmap);
2413 domain = isl_basic_set_from_basic_map(bmap);
2414 return isl_basic_set_project_out(domain, n_out, 0);
2417 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2419 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2422 struct isl_set *isl_map_range(struct isl_map *map)
2425 struct isl_set *set;
2429 map = isl_map_cow(map);
2433 set = (struct isl_set *) map;
2434 if (set->dim->n_in != 0) {
2435 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2439 for (i = 0; i < map->n; ++i) {
2440 set->p[i] = isl_basic_map_range(map->p[i]);
2444 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2445 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2452 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2455 struct isl_map *map = NULL;
2457 set = isl_set_cow(set);
2460 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2461 map = (struct isl_map *)set;
2462 for (i = 0; i < set->n; ++i) {
2463 map->p[i] = isl_basic_map_from_basic_set(
2464 set->p[i], isl_dim_copy(dim));
2468 isl_dim_free(map->dim);
2477 struct isl_map *isl_map_from_range(struct isl_set *set)
2479 return (struct isl_map *)set;
2482 struct isl_set *isl_set_from_map(struct isl_map *map)
2485 struct isl_set *set = NULL;
2489 map = isl_map_cow(map);
2492 map->dim = isl_dim_cow(map->dim);
2495 map->dim->n_out += map->dim->n_in;
2497 set = (struct isl_set *)map;
2498 for (i = 0; i < map->n; ++i) {
2499 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2509 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2511 struct isl_map *map;
2515 isl_assert(dim->ctx, n >= 0, return NULL);
2516 map = isl_alloc(dim->ctx, struct isl_map,
2517 sizeof(struct isl_map) +
2518 n * sizeof(struct isl_basic_map *));
2522 map->ctx = dim->ctx;
2523 isl_ctx_ref(map->ctx);
2535 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2536 unsigned nparam, unsigned in, unsigned out, int n,
2539 struct isl_map *map;
2540 struct isl_dim *dims;
2542 dims = isl_dim_alloc(ctx, nparam, in, out);
2546 map = isl_map_alloc_dim(dims, n, flags);
2550 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2551 unsigned nparam, unsigned in, unsigned out)
2553 struct isl_basic_map *bmap;
2554 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2555 bmap = isl_basic_map_set_to_empty(bmap);
2559 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2561 struct isl_basic_set *bset;
2562 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2563 bset = isl_basic_set_set_to_empty(bset);
2567 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2569 struct isl_basic_map *bmap;
2572 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2573 bmap = isl_basic_map_set_to_empty(bmap);
2577 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2579 struct isl_basic_map *bmap;
2582 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2583 bmap = isl_basic_map_set_to_empty(bmap);
2587 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2589 struct isl_basic_set *bset;
2592 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2593 bset = isl_basic_set_set_to_empty(bset);
2597 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2599 struct isl_basic_map *bmap;
2600 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2604 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2606 struct isl_basic_set *bset;
2607 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2611 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2615 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2618 struct isl_map *isl_map_empty(struct isl_dim *dim)
2620 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2623 struct isl_map *isl_map_empty_like(struct isl_map *model)
2627 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2630 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2634 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2637 struct isl_set *isl_set_empty(struct isl_dim *dim)
2639 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2642 struct isl_set *isl_set_empty_like(struct isl_set *model)
2646 return isl_set_empty(isl_dim_copy(model->dim));
2649 struct isl_map *isl_map_universe(struct isl_dim *dim)
2651 struct isl_map *map;
2654 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2655 map = isl_map_add(map, isl_basic_map_universe(dim));
2659 struct isl_set *isl_set_universe(struct isl_dim *dim)
2661 struct isl_set *set;
2664 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2665 set = isl_set_add(set, isl_basic_set_universe(dim));
2669 struct isl_map *isl_map_dup(struct isl_map *map)
2672 struct isl_map *dup;
2676 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2677 for (i = 0; i < map->n; ++i)
2678 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2682 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2686 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2687 isl_assert(map->ctx, map->n < map->size, goto error);
2688 map->p[map->n] = bmap;
2690 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2696 isl_basic_map_free(bmap);
2700 void isl_map_free(struct isl_map *map)
2710 isl_ctx_deref(map->ctx);
2711 for (i = 0; i < map->n; ++i)
2712 isl_basic_map_free(map->p[i]);
2713 isl_dim_free(map->dim);
2717 struct isl_map *isl_map_extend(struct isl_map *base,
2718 unsigned nparam, unsigned n_in, unsigned n_out)
2722 base = isl_map_cow(base);
2726 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2729 for (i = 0; i < base->n; ++i) {
2730 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2731 isl_dim_copy(base->dim), 0, 0, 0);
2741 struct isl_set *isl_set_extend(struct isl_set *base,
2742 unsigned nparam, unsigned dim)
2744 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2748 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2749 unsigned pos, int value)
2753 bmap = isl_basic_map_cow(bmap);
2754 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2755 j = isl_basic_map_alloc_equality(bmap);
2758 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2759 isl_int_set_si(bmap->eq[j][pos], -1);
2760 isl_int_set_si(bmap->eq[j][0], value);
2761 bmap = isl_basic_map_simplify(bmap);
2762 return isl_basic_map_finalize(bmap);
2764 isl_basic_map_free(bmap);
2768 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2769 enum isl_dim_type type, unsigned pos, int value)
2773 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2774 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2777 isl_basic_map_free(bmap);
2781 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2782 enum isl_dim_type type, unsigned pos, int value)
2784 return (struct isl_basic_set *)
2785 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2789 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2790 unsigned input, int value)
2792 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2795 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2796 unsigned dim, int value)
2798 return (struct isl_basic_set *)
2799 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2800 isl_dim_set, dim, value);
2803 struct isl_map *isl_map_fix_si(struct isl_map *map,
2804 enum isl_dim_type type, unsigned pos, int value)
2808 map = isl_map_cow(map);
2812 isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2813 for (i = 0; i < map->n; ++i) {
2814 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2818 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2825 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2826 unsigned input, int value)
2828 return isl_map_fix_si(map, isl_dim_in, input, value);
2831 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2833 return (struct isl_set *)
2834 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2837 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2838 unsigned dim, isl_int value)
2843 bset = isl_basic_set_cow(bset);
2844 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2845 j = isl_basic_set_alloc_inequality(bset);
2848 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2849 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2850 isl_int_neg(bset->ineq[j][0], value);
2851 bset = isl_basic_set_simplify(bset);
2852 return isl_basic_set_finalize(bset);
2854 isl_basic_set_free(bset);
2858 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2863 set = isl_set_cow(set);
2867 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2868 for (i = 0; i < set->n; ++i) {
2869 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2879 struct isl_map *isl_map_reverse(struct isl_map *map)
2884 map = isl_map_cow(map);
2888 map->dim = isl_dim_reverse(map->dim);
2891 for (i = 0; i < map->n; ++i) {
2892 map->p[i] = isl_basic_map_reverse(map->p[i]);
2896 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2903 struct isl_map *isl_basic_map_lexmax(
2904 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2905 struct isl_set **empty)
2907 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2910 struct isl_map *isl_basic_map_lexmin(
2911 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2912 struct isl_set **empty)
2914 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2917 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2919 struct isl_basic_map *bmap = NULL;
2920 struct isl_basic_set *dom = NULL;
2921 struct isl_map *min;
2922 struct isl_dim *param_dim;
2926 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2929 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2930 dom = isl_basic_set_universe(param_dim);
2933 min = isl_basic_map_lexmin(bmap, dom, NULL);
2934 return isl_map_range(min);
2936 isl_basic_map_free(bmap);
2940 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2947 off = isl_dim_total(bmap->dim);
2948 for (i = 0; i < bmap->n_div; ++i) {
2949 if (isl_int_is_zero(bmap->div[i][0]))
2950 return isl_pip_basic_map_compute_divs(bmap);
2951 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2954 return isl_map_from_basic_map(bmap);
2956 isl_basic_map_free(bmap);
2960 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2963 struct isl_map *res;
2969 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2970 for (i = 1 ; i < map->n; ++i) {
2972 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2973 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
2974 res = isl_map_union_disjoint(res, r2);
2976 res = isl_map_union(res, r2);
2983 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2985 return (struct isl_set *)
2986 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2989 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2991 return (struct isl_set *)
2992 isl_map_compute_divs((struct isl_map *)set);
2995 struct isl_set *isl_map_domain(struct isl_map *map)
2998 struct isl_set *set;
3003 map = isl_map_cow(map);
3007 set = (struct isl_set *)map;
3008 set->dim = isl_dim_domain(set->dim);
3011 for (i = 0; i < map->n; ++i) {
3012 set->p[i] = isl_basic_map_domain(map->p[i]);
3016 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3017 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3024 struct isl_map *isl_map_union_disjoint(
3025 struct isl_map *map1, struct isl_map *map2)
3029 struct isl_map *map = NULL;
3043 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3045 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3046 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3047 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3049 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3050 map1->n + map2->n, flags);
3053 for (i = 0; i < map1->n; ++i) {
3054 map = isl_map_add(map,
3055 isl_basic_map_copy(map1->p[i]));
3059 for (i = 0; i < map2->n; ++i) {
3060 map = isl_map_add(map,
3061 isl_basic_map_copy(map2->p[i]));
3075 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3077 map1 = isl_map_union_disjoint(map1, map2);
3081 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3085 struct isl_set *isl_set_union_disjoint(
3086 struct isl_set *set1, struct isl_set *set2)
3088 return (struct isl_set *)
3089 isl_map_union_disjoint(
3090 (struct isl_map *)set1, (struct isl_map *)set2);
3093 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3095 return (struct isl_set *)
3096 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3099 struct isl_map *isl_map_intersect_range(
3100 struct isl_map *map, struct isl_set *set)
3103 struct isl_map *result;
3109 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3110 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3111 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3113 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3114 map->n * set->n, flags);
3117 for (i = 0; i < map->n; ++i)
3118 for (j = 0; j < set->n; ++j) {
3119 result = isl_map_add(result,
3120 isl_basic_map_intersect_range(
3121 isl_basic_map_copy(map->p[i]),
3122 isl_basic_set_copy(set->p[j])));
3135 struct isl_map *isl_map_intersect_domain(
3136 struct isl_map *map, struct isl_set *set)
3138 return isl_map_reverse(
3139 isl_map_intersect_range(isl_map_reverse(map), set));
3142 struct isl_map *isl_map_apply_domain(
3143 struct isl_map *map1, struct isl_map *map2)
3147 map1 = isl_map_reverse(map1);
3148 map1 = isl_map_apply_range(map1, map2);
3149 return isl_map_reverse(map1);
3156 struct isl_map *isl_map_apply_range(
3157 struct isl_map *map1, struct isl_map *map2)
3159 struct isl_dim *dim_result;
3160 struct isl_map *result;
3169 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3170 isl_dim_copy(map2->dim));
3172 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3175 for (i = 0; i < map1->n; ++i)
3176 for (j = 0; j < map2->n; ++j) {
3177 result = isl_map_add(result,
3178 isl_basic_map_apply_range(
3179 isl_basic_map_copy(map1->p[i]),
3180 isl_basic_map_copy(map2->p[j])));
3186 if (result && result->n <= 1)
3187 ISL_F_SET(result, ISL_MAP_DISJOINT);
3196 * returns range - domain
3198 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3200 struct isl_basic_set *bset;
3207 dim = isl_basic_map_n_in(bmap);
3208 nparam = isl_basic_map_n_param(bmap);
3209 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3210 bset = isl_basic_set_from_basic_map(bmap);
3211 bset = isl_basic_set_cow(bset);
3212 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3213 bset = isl_basic_set_swap_vars(bset, 2*dim);
3214 for (i = 0; i < dim; ++i) {
3215 int j = isl_basic_map_alloc_equality(
3216 (struct isl_basic_map *)bset);
3219 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3220 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3221 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3222 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3224 return isl_basic_set_project_out(bset, 2*dim, 0);
3226 isl_basic_map_free(bmap);
3231 * returns range - domain
3233 struct isl_set *isl_map_deltas(struct isl_map *map)
3236 struct isl_set *result;
3241 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3242 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3243 isl_map_n_in(map), map->n, map->flags);
3246 for (i = 0; i < map->n; ++i)
3247 result = isl_set_add(result,
3248 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3256 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3258 struct isl_basic_map *bmap;
3266 nparam = dims->nparam;
3268 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3272 for (i = 0; i < dim; ++i) {
3273 int j = isl_basic_map_alloc_equality(bmap);
3276 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3277 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3278 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3280 return isl_basic_map_finalize(bmap);
3282 isl_basic_map_free(bmap);
3286 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3288 struct isl_dim *dim = isl_dim_map(set_dim);
3291 return basic_map_identity(dim);
3294 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3296 if (!model || !model->dim)
3298 isl_assert(model->ctx,
3299 model->dim->n_in == model->dim->n_out, return NULL);
3300 return basic_map_identity(isl_dim_copy(model->dim));
3303 static struct isl_map *map_identity(struct isl_dim *dim)
3305 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3306 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3309 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3311 struct isl_dim *dim = isl_dim_map(set_dim);
3314 return map_identity(dim);
3317 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3319 if (!model || !model->dim)
3321 isl_assert(model->ctx,
3322 model->dim->n_in == model->dim->n_out, return NULL);
3323 return map_identity(isl_dim_copy(model->dim));
3326 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3328 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3331 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3333 return isl_map_is_subset(
3334 (struct isl_map *)set1, (struct isl_map *)set2);
3337 int isl_basic_map_is_subset(
3338 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3341 struct isl_map *map1;
3342 struct isl_map *map2;
3344 if (!bmap1 || !bmap2)
3347 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3348 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3350 is_subset = isl_map_is_subset(map1, map2);
3358 int isl_basic_map_is_equal(
3359 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3363 if (!bmap1 || !bmap2)
3365 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3368 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3372 int isl_basic_set_is_equal(
3373 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3375 return isl_basic_map_is_equal(
3376 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3379 int isl_map_is_empty(struct isl_map *map)
3386 for (i = 0; i < map->n; ++i) {
3387 is_empty = isl_basic_map_is_empty(map->p[i]);
3396 int isl_map_fast_is_empty(struct isl_map *map)
3401 int isl_set_is_empty(struct isl_set *set)
3403 return isl_map_is_empty((struct isl_map *)set);
3406 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3410 struct isl_map *diff;
3415 if (isl_map_is_empty(map1))
3418 if (isl_map_is_empty(map2))
3421 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3425 is_subset = isl_map_is_empty(diff);
3431 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3437 is_subset = isl_map_is_subset(map1, map2);
3440 is_subset = isl_map_is_subset(map2, map1);
3444 int isl_basic_map_is_strict_subset(
3445 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3449 if (!bmap1 || !bmap2)
3451 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3454 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3455 if (is_subset == -1)
3460 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3464 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3467 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3469 struct isl_basic_set *bset = NULL;
3470 struct isl_vec *sample = NULL;
3477 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3480 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3481 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3482 copy = isl_basic_map_convex_hull(copy);
3483 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3484 isl_basic_map_free(copy);
3488 total = 1 + isl_basic_map_total_dim(bmap);
3489 if (bmap->sample && bmap->sample->size == total) {
3490 int contains = basic_map_contains(bmap, bmap->sample);
3496 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3499 sample = isl_basic_set_sample(bset);
3502 empty = sample->size == 0;
3504 isl_vec_free(bmap->ctx, bmap->sample);
3505 bmap->sample = sample;
3510 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
3514 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
3517 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
3521 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
3524 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3526 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3529 struct isl_map *isl_basic_map_union(
3530 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3532 struct isl_map *map;
3533 if (!bmap1 || !bmap2)
3536 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3538 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3541 map = isl_map_add(map, bmap1);
3542 map = isl_map_add(map, bmap2);
3545 isl_basic_map_free(bmap1);
3546 isl_basic_map_free(bmap2);
3550 struct isl_set *isl_basic_set_union(
3551 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3553 return (struct isl_set *)isl_basic_map_union(
3554 (struct isl_basic_map *)bset1,
3555 (struct isl_basic_map *)bset2);
3558 /* Order divs such that any div only depends on previous divs */
3559 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3562 unsigned off = isl_dim_total(bmap->dim);
3564 for (i = 0; i < bmap->n_div; ++i) {
3566 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3570 swap_div(bmap, i, pos);
3576 /* Look for a div in dst that corresponds to the div "div" in src.
3577 * The divs before "div" in src and dst are assumed to be the same.
3579 * Returns -1 if no corresponding div was found and the position
3580 * of the corresponding div in dst otherwise.
3582 static int find_div(struct isl_basic_map *dst,
3583 struct isl_basic_map *src, unsigned div)
3587 unsigned total = isl_dim_total(src->dim);
3589 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3590 for (i = div; i < dst->n_div; ++i)
3591 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3592 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3593 dst->n_div - div) == -1)
3598 struct isl_basic_map *isl_basic_map_align_divs(
3599 struct isl_basic_map *dst, struct isl_basic_map *src)
3602 unsigned total = isl_dim_total(src->dim);
3607 if (src->n_div == 0)
3610 for (i = 0; i < src->n_div; ++i)
3611 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3613 src = order_divs(src);
3614 dst = isl_basic_map_cow(dst);
3615 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3616 src->n_div, 0, 2 * src->n_div);
3619 for (i = 0; i < src->n_div; ++i) {
3620 int j = find_div(dst, src, i);
3622 j = isl_basic_map_alloc_div(dst);
3625 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3626 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3627 if (add_div_constraints(dst, j) < 0)
3631 swap_div(dst, i, j);
3635 isl_basic_map_free(dst);
3639 struct isl_basic_set *isl_basic_set_align_divs(
3640 struct isl_basic_set *dst, struct isl_basic_set *src)
3642 return (struct isl_basic_set *)isl_basic_map_align_divs(
3643 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3646 struct isl_map *isl_map_align_divs(struct isl_map *map)
3650 map = isl_map_compute_divs(map);
3651 map = isl_map_cow(map);
3655 for (i = 1; i < map->n; ++i)
3656 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3657 for (i = 1; i < map->n; ++i)
3658 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3660 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3664 struct isl_set *isl_set_align_divs(struct isl_set *set)
3666 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
3669 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3670 struct isl_basic_map *src, isl_int *c,
3671 unsigned len, int oppose)
3673 struct isl_basic_map *copy = NULL;
3678 copy = isl_basic_map_copy(src);
3679 copy = isl_basic_map_cow(copy);
3682 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3683 k = isl_basic_map_alloc_inequality(copy);
3687 isl_seq_neg(copy->ineq[k], c, len);
3689 isl_seq_cpy(copy->ineq[k], c, len);
3690 total = 1 + isl_basic_map_total_dim(copy);
3691 isl_seq_clr(copy->ineq[k]+len, total - len);
3692 isl_inequality_negate(copy, k);
3693 copy = isl_basic_map_simplify(copy);
3694 copy = isl_basic_map_finalize(copy);
3695 is_empty = isl_basic_map_is_empty(copy);
3699 dst = isl_map_add(dst, copy);
3701 isl_basic_map_free(copy);
3704 isl_basic_map_free(copy);
3709 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3713 struct isl_map *rest = NULL;
3715 unsigned total = isl_basic_map_total_dim(bmap);
3722 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3723 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3725 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3726 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3730 for (i = 0; i < map->n; ++i) {
3731 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3736 for (j = 0; j < map->n; ++j)
3737 map->p[j] = isl_basic_map_cow(map->p[j]);
3739 for (i = 0; i < bmap->n_eq; ++i) {
3740 for (j = 0; j < map->n; ++j) {
3741 rest = add_cut_constraint(rest,
3742 map->p[j], bmap->eq[i], 1+total, 0);
3746 rest = add_cut_constraint(rest,
3747 map->p[j], bmap->eq[i], 1+total, 1);
3751 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3755 k = isl_basic_map_alloc_equality(map->p[j]);
3758 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3759 isl_seq_clr(map->p[j]->eq[k]+1+total,
3760 map->p[j]->n_div - bmap->n_div);
3764 for (i = 0; i < bmap->n_ineq; ++i) {
3765 for (j = 0; j < map->n; ++j) {
3766 rest = add_cut_constraint(rest,
3767 map->p[j], bmap->ineq[i], 1+total, 0);
3771 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3775 k = isl_basic_map_alloc_inequality(map->p[j]);
3778 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3779 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3780 map->p[j]->n_div - bmap->n_div);
3792 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3798 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3800 if (isl_map_is_empty(map2)) {
3805 map1 = isl_map_compute_divs(map1);
3806 map2 = isl_map_compute_divs(map2);
3810 for (i = 0; map1 && i < map2->n; ++i)
3811 map1 = subtract(map1, map2->p[i]);
3821 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3823 return (struct isl_set *)
3825 (struct isl_map *)set1, (struct isl_map *)set2);
3828 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3832 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3833 map = isl_map_intersect_domain(map, set);
3834 set = isl_map_range(map);
3842 /* There is no need to cow as removing empty parts doesn't change
3843 * the meaning of the set.
3845 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3852 for (i = map->n-1; i >= 0; --i) {
3853 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3855 isl_basic_map_free(map->p[i]);
3856 if (i != map->n-1) {
3857 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3858 map->p[i] = map->p[map->n-1];
3866 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3868 return (struct isl_set *)
3869 isl_map_remove_empty_parts((struct isl_map *)set);
3872 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
3874 struct isl_basic_map *bmap;
3875 if (!map || map->n == 0)
3877 bmap = map->p[map->n-1];
3878 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
3879 return isl_basic_map_copy(bmap);
3882 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3884 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
3887 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
3888 struct isl_basic_map *bmap)
3894 for (i = map->n-1; i >= 0; --i) {
3895 if (map->p[i] != bmap)
3897 map = isl_map_cow(map);
3900 isl_basic_map_free(map->p[i]);
3901 if (i != map->n-1) {
3902 ISL_F_CLR(map, ISL_SET_NORMALIZED);
3903 map->p[i] = map->p[map->n-1];
3908 isl_basic_map_free(bmap);
3912 isl_basic_map_free(bmap);
3916 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3917 struct isl_basic_set *bset)
3919 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
3920 (struct isl_basic_map *)bset);
3923 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3924 * for any common value of the parameters and dimensions preceding dim
3925 * in both basic sets, the values of dimension pos in bset1 are
3926 * smaller or larger than those in bset2.
3929 * 1 if bset1 follows bset2
3930 * -1 if bset1 precedes bset2
3931 * 0 if bset1 and bset2 are incomparable
3932 * -2 if some error occurred.
3934 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3935 struct isl_basic_set *bset2, int pos)
3937 struct isl_dim *dims;
3938 struct isl_basic_map *bmap1 = NULL;
3939 struct isl_basic_map *bmap2 = NULL;
3940 struct isl_ctx *ctx;
3941 struct isl_vec *obj;
3944 unsigned dim1, dim2;
3946 enum isl_lp_result res;
3949 if (!bset1 || !bset2)
3952 nparam = isl_basic_set_n_param(bset1);
3953 dim1 = isl_basic_set_n_dim(bset1);
3954 dim2 = isl_basic_set_n_dim(bset2);
3955 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3956 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3957 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3958 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3959 if (!bmap1 || !bmap2)
3961 bmap1 = isl_basic_map_cow(bmap1);
3962 bmap1 = isl_basic_map_extend(bmap1, nparam,
3963 pos, (dim1 - pos) + (dim2 - pos),
3964 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3965 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
3968 total = isl_basic_map_total_dim(bmap1);
3970 obj = isl_vec_alloc(ctx, 1 + total);
3971 isl_seq_clr(obj->block.data, 1 + total);
3972 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
3973 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
3978 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3979 if (res == isl_lp_empty)
3981 else if (res == isl_lp_ok && isl_int_is_pos(num))
3983 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3984 res == isl_lp_unbounded)
3990 isl_basic_map_free(bmap1);
3991 isl_vec_free(ctx, obj);
3994 isl_basic_map_free(bmap1);
3995 isl_basic_map_free(bmap2);
3999 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4000 unsigned pos, isl_int *val)
4008 total = isl_basic_map_total_dim(bmap);
4009 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4010 for (; d+1 > pos; --d)
4011 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4015 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4017 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4019 if (!isl_int_is_one(bmap->eq[i][1+d]))
4022 isl_int_neg(*val, bmap->eq[i][0]);
4028 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4029 unsigned pos, isl_int *val)
4041 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4044 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4045 for (i = 1; fixed == 1 && i < map->n; ++i) {
4046 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4047 if (fixed == 1 && isl_int_ne(tmp, v))
4051 isl_int_set(*val, v);
4057 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4060 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4063 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4064 enum isl_dim_type type, unsigned pos, isl_int *val)
4066 if (pos >= isl_basic_map_dim(bmap, type))
4068 return isl_basic_map_fast_has_fixed_var(bmap,
4069 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4072 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4073 * then return this fixed value in *val.
4075 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4077 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4080 /* Check if input variable in has fixed value and if so and if val is not NULL,
4081 * then return this fixed value in *val.
4083 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4085 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4088 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4089 * and if val is not NULL, then return this lower bound in *val.
4091 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4092 unsigned dim, isl_int *val)
4094 int i, i_eq = -1, i_ineq = -1;
4101 total = isl_basic_set_total_dim(bset);
4102 nparam = isl_basic_set_n_param(bset);
4103 for (i = 0; i < bset->n_eq; ++i) {
4104 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4110 for (i = 0; i < bset->n_ineq; ++i) {
4111 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4113 if (i_eq != -1 || i_ineq != -1)
4117 if (i_eq == -1 && i_ineq == -1)
4119 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4120 /* The coefficient should always be one due to normalization. */
4121 if (!isl_int_is_one(c[1+nparam+dim]))
4123 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4125 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4126 total - nparam - dim - 1) != -1)
4129 isl_int_neg(*val, c[0]);
4133 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4134 unsigned dim, isl_int *val)
4146 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4150 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4152 for (i = 1; fixed == 1 && i < set->n; ++i) {
4153 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4155 if (fixed == 1 && isl_int_ne(tmp, v))
4159 isl_int_set(*val, v);
4170 static int qsort_constraint_cmp(const void *p1, const void *p2)
4172 const struct constraint *c1 = (const struct constraint *)p1;
4173 const struct constraint *c2 = (const struct constraint *)p2;
4174 unsigned size = isl_min(c1->size, c2->size);
4175 return isl_seq_cmp(c1->c, c2->c, size);
4178 static struct isl_basic_map *isl_basic_map_sort_constraints(
4179 struct isl_basic_map *bmap)
4182 struct constraint *c;
4187 total = isl_basic_map_total_dim(bmap);
4188 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4191 for (i = 0; i < bmap->n_ineq; ++i) {
4193 c[i].c = bmap->ineq[i];
4195 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4196 for (i = 0; i < bmap->n_ineq; ++i)
4197 bmap->ineq[i] = c[i].c;
4201 isl_basic_map_free(bmap);
4205 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4209 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4211 bmap = isl_basic_map_convex_hull(bmap);
4212 bmap = isl_basic_map_sort_constraints(bmap);
4213 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4217 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4219 return (struct isl_basic_set *)isl_basic_map_normalize(
4220 (struct isl_basic_map *)bset);
4223 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4224 const struct isl_basic_map *bmap2)
4231 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4232 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4233 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4234 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4235 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4236 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4237 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4238 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4240 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4242 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4244 if (bmap1->n_eq != bmap2->n_eq)
4245 return bmap1->n_eq - bmap2->n_eq;
4246 if (bmap1->n_ineq != bmap2->n_ineq)
4247 return bmap1->n_ineq - bmap2->n_ineq;
4248 if (bmap1->n_div != bmap2->n_div)
4249 return bmap1->n_div - bmap2->n_div;
4250 total = isl_basic_map_total_dim(bmap1);
4251 for (i = 0; i < bmap1->n_eq; ++i) {
4252 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4256 for (i = 0; i < bmap1->n_ineq; ++i) {
4257 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4261 for (i = 0; i < bmap1->n_div; ++i) {
4262 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4269 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4270 struct isl_basic_map *bmap2)
4272 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4275 static int qsort_bmap_cmp(const void *p1, const void *p2)
4277 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4278 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4280 return isl_basic_map_fast_cmp(bmap1, bmap2);
4283 /* We normalize in place, but if anything goes wrong we need
4284 * to return NULL, so we need to make sure we don't change the
4285 * meaning of any possible other copies of map.
4287 struct isl_map *isl_map_normalize(struct isl_map *map)
4290 struct isl_basic_map *bmap;
4294 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4296 for (i = 0; i < map->n; ++i) {
4297 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4300 isl_basic_map_free(map->p[i]);
4303 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4304 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4305 map = isl_map_remove_empty_parts(map);
4308 for (i = map->n - 1; i >= 1; --i) {
4309 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4311 isl_basic_map_free(map->p[i-1]);
4312 for (j = i; j < map->n; ++j)
4313 map->p[j-1] = map->p[j];
4323 struct isl_set *isl_set_normalize(struct isl_set *set)
4325 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4328 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4338 if (!isl_dim_equal(map1->dim, map2->dim))
4341 map1 = isl_map_copy(map1);
4342 map2 = isl_map_copy(map2);
4343 map1 = isl_map_normalize(map1);
4344 map2 = isl_map_normalize(map2);
4347 equal = map1->n == map2->n;
4348 for (i = 0; equal && i < map1->n; ++i) {
4349 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4362 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4364 return isl_map_fast_is_equal((struct isl_map *)set1,
4365 (struct isl_map *)set2);
4368 /* Return an interval that ranges from min to max (inclusive)
4370 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4371 isl_int min, isl_int max)
4374 struct isl_basic_set *bset = NULL;
4376 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4380 k = isl_basic_set_alloc_inequality(bset);
4383 isl_int_set_si(bset->ineq[k][1], 1);
4384 isl_int_neg(bset->ineq[k][0], min);
4386 k = isl_basic_set_alloc_inequality(bset);
4389 isl_int_set_si(bset->ineq[k][1], -1);
4390 isl_int_set(bset->ineq[k][0], max);
4394 isl_basic_set_free(bset);
4398 /* Return the Cartesian product of the basic sets in list (in the given order).
4400 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4408 struct isl_basic_set *product = NULL;
4412 isl_assert(list->ctx, list->n > 0, goto error);
4413 isl_assert(list->ctx, list->p[0], goto error);
4414 nparam = isl_basic_set_n_param(list->p[0]);
4415 dim = isl_basic_set_n_dim(list->p[0]);
4416 extra = list->p[0]->n_div;
4417 n_eq = list->p[0]->n_eq;
4418 n_ineq = list->p[0]->n_ineq;
4419 for (i = 1; i < list->n; ++i) {
4420 isl_assert(list->ctx, list->p[i], goto error);
4421 isl_assert(list->ctx,
4422 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4423 dim += isl_basic_set_n_dim(list->p[i]);
4424 extra += list->p[i]->n_div;
4425 n_eq += list->p[i]->n_eq;
4426 n_ineq += list->p[i]->n_ineq;
4428 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4433 for (i = 0; i < list->n; ++i) {
4434 isl_basic_set_add_constraints(product,
4435 isl_basic_set_copy(list->p[i]), dim);
4436 dim += isl_basic_set_n_dim(list->p[i]);
4438 isl_basic_set_list_free(list);
4441 isl_basic_set_free(product);
4442 isl_basic_set_list_free(list);
4446 struct isl_basic_map *isl_basic_map_product(
4447 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4449 struct isl_dim *dim_result = NULL;
4450 struct isl_basic_map *bmap;
4451 unsigned in1, in2, out1, out2, nparam, total, pos;
4452 struct isl_dim_map *dim_map1, *dim_map2;
4454 if (!bmap1 || !bmap2)
4457 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4458 bmap2->dim, isl_dim_param), goto error);
4459 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4460 isl_dim_copy(bmap2->dim));
4462 in1 = isl_basic_map_n_in(bmap1);
4463 in2 = isl_basic_map_n_in(bmap2);
4464 out1 = isl_basic_map_n_out(bmap1);
4465 out2 = isl_basic_map_n_out(bmap2);
4466 nparam = isl_basic_map_n_param(bmap1);
4468 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4469 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4470 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4471 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4472 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4473 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4474 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4475 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4476 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4477 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4478 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4480 bmap = isl_basic_map_alloc_dim(dim_result,
4481 bmap1->n_div + bmap2->n_div,
4482 bmap1->n_eq + bmap2->n_eq,
4483 bmap1->n_ineq + bmap2->n_ineq);
4484 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4485 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4486 bmap = isl_basic_map_simplify(bmap);
4487 return isl_basic_map_finalize(bmap);
4489 isl_basic_map_free(bmap1);
4490 isl_basic_map_free(bmap2);
4494 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4496 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4499 struct isl_map *result;
4505 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4506 map2->dim, isl_dim_param), goto error);
4508 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4509 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4510 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4512 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4513 isl_dim_copy(map2->dim)),
4514 map1->n * map2->n, flags);
4517 for (i = 0; i < map1->n; ++i)
4518 for (j = 0; j < map2->n; ++j) {
4519 struct isl_basic_map *part;
4520 part = isl_basic_map_product(
4521 isl_basic_map_copy(map1->p[i]),
4522 isl_basic_map_copy(map2->p[j]));
4523 if (isl_basic_map_is_empty(part))
4524 isl_basic_map_free(part);
4526 result = isl_map_add(result, part);
4539 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4547 bset = isl_basic_set_copy(bset);
4548 bset = isl_basic_set_normalize(bset);
4551 total = isl_basic_set_total_dim(bset);
4552 isl_hash_byte(hash, bset->n_eq & 0xFF);
4553 for (i = 0; i < bset->n_eq; ++i) {
4555 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4556 isl_hash_hash(hash, c_hash);
4558 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4559 for (i = 0; i < bset->n_ineq; ++i) {
4561 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4562 isl_hash_hash(hash, c_hash);
4564 isl_hash_byte(hash, bset->n_div & 0xFF);
4565 for (i = 0; i < bset->n_div; ++i) {
4567 if (isl_int_is_zero(bset->div[i][0]))
4569 isl_hash_byte(hash, i & 0xFF);
4570 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4571 isl_hash_hash(hash, c_hash);
4573 isl_basic_set_free(bset);
4577 uint32_t isl_set_get_hash(struct isl_set *set)
4584 set = isl_set_copy(set);
4585 set = isl_set_normalize(set);
4589 hash = isl_hash_init();
4590 for (i = 0; i < set->n; ++i) {
4592 bset_hash = isl_basic_set_get_hash(set->p[i]);
4593 isl_hash_hash(hash, bset_hash);
4601 /* Check if the value for dimension dim is completely determined
4602 * by the values of the other parameters and variables.
4603 * That is, check if dimension dim is involved in an equality.
4605 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4612 nparam = isl_basic_set_n_param(bset);
4613 for (i = 0; i < bset->n_eq; ++i)
4614 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4619 /* Check if the value for dimension dim is completely determined
4620 * by the values of the other parameters and variables.
4621 * That is, check if dimension dim is involved in an equality
4622 * for each of the subsets.
4624 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4630 for (i = 0; i < set->n; ++i) {
4632 unique = isl_basic_set_dim_is_unique(set->p[i], dim);