11 #include "isl_map_private.h"
12 #include "isl_map_piplib.h"
13 #include "isl_sample.h"
16 /* Maps dst positions to src positions */
22 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
25 struct isl_dim_map *dim_map;
26 dim_map = isl_alloc(ctx, struct isl_dim_map,
27 sizeof(struct isl_dim_map) + len * sizeof(int));
30 dim_map->len = 1 + len;
32 for (i = 0; i < len; ++i)
33 dim_map->pos[1 + i] = -1;
37 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
40 case isl_dim_param: return dim->nparam;
41 case isl_dim_in: return dim->n_in;
42 case isl_dim_out: return dim->n_out;
46 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
49 case isl_dim_param: return 1;
50 case isl_dim_in: return 1 + dim->nparam;
51 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
55 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
56 enum isl_dim_type type, unsigned dst_pos)
64 src_pos = pos(dim, type);
65 for (i = 0; i < n(dim, type); ++i)
66 dim_map->pos[1 + dst_pos + i] = src_pos + i;
69 static void isl_dim_map_div(struct isl_dim_map *dim_map,
70 struct isl_basic_map *bmap, unsigned dst_pos)
75 if (!dim_map || !bmap)
78 src_pos = 1 + isl_dim_total(bmap->dim);
79 for (i = 0; i < bmap->n_div; ++i)
80 dim_map->pos[1 + dst_pos + i] = src_pos + i;
83 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
87 for (i = 0; i < dim_map->len; ++i)
88 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
89 fprintf(stderr, "\n");
92 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
93 enum isl_dim_type type)
95 struct isl_dim *dim = bmap->dim;
99 case isl_dim_out: return isl_dim_size(bmap->dim, type);
100 case isl_dim_div: return bmap->n_div;
101 case isl_dim_all: return isl_basic_map_total_dim(bmap);
105 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
107 return n(map->dim, type);
110 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
112 return n(set->dim, type);
115 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
116 enum isl_dim_type type)
118 struct isl_dim *dim = bmap->dim;
120 case isl_dim_param: return 1;
121 case isl_dim_in: return 1 + dim->nparam;
122 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
123 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
127 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
129 return pos(map->dim, type);
132 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
133 enum isl_dim_type type)
135 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
138 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
140 return bset->dim->n_out;
143 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
145 return bset->dim->nparam;
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
150 return isl_dim_total(bset->dim) + bset->n_div;
153 unsigned isl_set_n_dim(const struct isl_set *set)
155 return set->dim->n_out;
158 unsigned isl_set_n_param(const struct isl_set *set)
160 return set->dim->nparam;
163 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
165 return bmap->dim->n_in;
168 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
170 return bmap->dim->n_out;
173 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
175 return bmap->dim->nparam;
178 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
183 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
185 return isl_dim_total(bmap->dim) + bmap->n_div;
188 unsigned isl_map_n_in(const struct isl_map *map)
190 return map->dim->n_in;
193 unsigned isl_map_n_out(const struct isl_map *map)
195 return map->dim->n_out;
198 unsigned isl_map_n_param(const struct isl_map *map)
200 return map->dim->nparam;
203 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
205 return map->dim->n_in == set->dim->n_out &&
206 map->dim->nparam == set->dim->nparam;
209 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
210 struct isl_basic_set *bset)
212 return bmap->dim->n_in == bset->dim->n_out &&
213 bmap->dim->nparam == bset->dim->nparam;
216 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
217 struct isl_basic_set *bset)
219 return bmap->dim->n_out == bset->dim->n_out &&
220 bmap->dim->nparam == bset->dim->nparam;
223 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
224 struct isl_basic_map *bmap, unsigned extra,
225 unsigned n_eq, unsigned n_ineq)
228 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
230 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
231 if (isl_blk_is_error(bmap->block)) {
236 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
238 isl_blk_free(ctx, bmap->block);
244 bmap->block2 = isl_blk_empty();
247 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
248 if (isl_blk_is_error(bmap->block2)) {
250 isl_blk_free(ctx, bmap->block);
255 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
257 isl_blk_free(ctx, bmap->block2);
259 isl_blk_free(ctx, bmap->block);
265 for (i = 0; i < n_ineq + n_eq; ++i)
266 bmap->ineq[i] = bmap->block.data + i * row_size;
268 for (i = 0; i < extra; ++i)
269 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
275 bmap->c_size = n_eq + n_ineq;
276 bmap->eq = bmap->ineq + n_ineq;
285 isl_basic_map_free(bmap);
289 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
290 unsigned nparam, unsigned dim, unsigned extra,
291 unsigned n_eq, unsigned n_ineq)
293 struct isl_basic_map *bmap;
294 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
295 return (struct isl_basic_set *)bmap;
298 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
299 unsigned extra, unsigned n_eq, unsigned n_ineq)
301 struct isl_basic_map *bmap;
304 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
305 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
306 return (struct isl_basic_set *)bmap;
309 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
310 unsigned extra, unsigned n_eq, unsigned n_ineq)
312 struct isl_basic_map *bmap;
316 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
321 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
327 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
328 unsigned nparam, unsigned in, unsigned out, unsigned extra,
329 unsigned n_eq, unsigned n_ineq)
331 struct isl_basic_map *bmap;
334 dim = isl_dim_alloc(ctx, nparam, in, out);
338 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
342 static void dup_constraints(
343 struct isl_basic_map *dst, struct isl_basic_map *src)
346 unsigned total = isl_basic_map_total_dim(src);
348 for (i = 0; i < src->n_eq; ++i) {
349 int j = isl_basic_map_alloc_equality(dst);
350 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
353 for (i = 0; i < src->n_ineq; ++i) {
354 int j = isl_basic_map_alloc_inequality(dst);
355 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
358 for (i = 0; i < src->n_div; ++i) {
359 int j = isl_basic_map_alloc_div(dst);
360 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
362 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
365 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
367 struct isl_basic_map *dup;
371 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
372 bmap->n_div, bmap->n_eq, bmap->n_ineq);
375 dup->flags = bmap->flags;
376 dup_constraints(dup, bmap);
377 dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
381 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
383 struct isl_basic_map *dup;
385 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
386 return (struct isl_basic_set *)dup;
389 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
394 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
398 return isl_basic_set_dup(bset);
401 struct isl_set *isl_set_copy(struct isl_set *set)
410 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
415 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
419 return isl_basic_map_dup(bmap);
422 struct isl_map *isl_map_copy(struct isl_map *map)
431 void isl_basic_map_free(struct isl_basic_map *bmap)
439 isl_ctx_deref(bmap->ctx);
441 isl_blk_free(bmap->ctx, bmap->block2);
443 isl_blk_free(bmap->ctx, bmap->block);
444 isl_vec_free(bmap->ctx, bmap->sample);
445 isl_dim_free(bmap->dim);
449 void isl_basic_set_free(struct isl_basic_set *bset)
451 isl_basic_map_free((struct isl_basic_map *)bset);
454 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
456 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
459 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
465 isl_assert(ctx, room_for_con(bmap, 1), return -1);
466 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
468 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
469 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
471 int j = isl_basic_map_alloc_inequality(bmap);
475 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
476 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
483 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
484 bmap->extra - bmap->n_div);
488 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
490 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
493 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
497 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
502 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
507 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
509 if (pos != bmap->n_eq - 1) {
511 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
512 bmap->eq[bmap->n_eq - 1] = t;
518 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
520 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
523 void isl_basic_map_inequality_to_equality(
524 struct isl_basic_map *bmap, unsigned pos)
529 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
530 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
535 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
536 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
537 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
540 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
542 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
545 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
551 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
552 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
553 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
554 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
555 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
556 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
557 1 + isl_basic_map_total_dim(bmap),
558 bmap->extra - bmap->n_div);
559 return bmap->n_ineq++;
562 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
564 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
567 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
571 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
576 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
578 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
581 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
586 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
588 if (pos != bmap->n_ineq - 1) {
590 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
591 bmap->ineq[bmap->n_ineq - 1] = t;
592 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
598 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
600 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
603 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
607 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
608 isl_seq_clr(bmap->div[bmap->n_div] +
609 1 + 1 + isl_basic_map_total_dim(bmap),
610 bmap->extra - bmap->n_div);
611 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
612 return bmap->n_div++;
615 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
619 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
624 /* Copy constraint from src to dst, putting the vars of src at offset
625 * dim_off in dst and the divs of src at offset div_off in dst.
626 * If both sets are actually map, then dim_off applies to the input
629 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
630 struct isl_basic_map *src_map, isl_int *src,
631 unsigned in_off, unsigned out_off, unsigned div_off)
633 unsigned src_nparam = isl_basic_map_n_param(src_map);
634 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
635 unsigned src_in = isl_basic_map_n_in(src_map);
636 unsigned dst_in = isl_basic_map_n_in(dst_map);
637 unsigned src_out = isl_basic_map_n_out(src_map);
638 unsigned dst_out = isl_basic_map_n_out(dst_map);
639 isl_int_set(dst[0], src[0]);
640 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
641 if (dst_nparam > src_nparam)
642 isl_seq_clr(dst+1+src_nparam,
643 dst_nparam - src_nparam);
644 isl_seq_clr(dst+1+dst_nparam, in_off);
645 isl_seq_cpy(dst+1+dst_nparam+in_off,
647 isl_min(dst_in-in_off, src_in));
648 if (dst_in-in_off > src_in)
649 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
650 dst_in - in_off - src_in);
651 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
652 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
653 src+1+src_nparam+src_in,
654 isl_min(dst_out-out_off, src_out));
655 if (dst_out-out_off > src_out)
656 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
657 dst_out - out_off - src_out);
658 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
659 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
660 src+1+src_nparam+src_in+src_out,
661 isl_min(dst_map->extra-div_off, src_map->n_div));
662 if (dst_map->n_div-div_off > src_map->n_div)
663 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
664 div_off+src_map->n_div,
665 dst_map->n_div - div_off - src_map->n_div);
668 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
669 struct isl_basic_map *src_map, isl_int *src,
670 unsigned in_off, unsigned out_off, unsigned div_off)
672 isl_int_set(dst[0], src[0]);
673 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
676 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
677 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
682 if (!bmap1 || !bmap2)
685 div_off = bmap1->n_div;
687 for (i = 0; i < bmap2->n_eq; ++i) {
688 int i1 = isl_basic_map_alloc_equality(bmap1);
691 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
692 i_pos, o_pos, div_off);
695 for (i = 0; i < bmap2->n_ineq; ++i) {
696 int i1 = isl_basic_map_alloc_inequality(bmap1);
699 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
700 i_pos, o_pos, div_off);
703 for (i = 0; i < bmap2->n_div; ++i) {
704 int i1 = isl_basic_map_alloc_div(bmap1);
707 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
708 i_pos, o_pos, div_off);
711 isl_basic_map_free(bmap2);
716 isl_basic_map_free(bmap1);
717 isl_basic_map_free(bmap2);
721 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
722 struct isl_dim_map *dim_map)
726 for (i = 0; i < dim_map->len; ++i) {
727 if (dim_map->pos[i] < 0)
728 isl_int_set_si(dst[i], 0);
730 isl_int_set(dst[i], src[dim_map->pos[i]]);
734 static void copy_div_dim_map(isl_int *dst, isl_int *src,
735 struct isl_dim_map *dim_map)
737 isl_int_set(dst[0], src[0]);
738 copy_constraint_dim_map(dst+1, src+1, dim_map);
741 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
742 struct isl_basic_map *src, struct isl_dim_map *dim_map)
746 if (!src || !dst || !dim_map)
749 for (i = 0; i < src->n_eq; ++i) {
750 int i1 = isl_basic_map_alloc_equality(dst);
753 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
756 for (i = 0; i < src->n_ineq; ++i) {
757 int i1 = isl_basic_map_alloc_inequality(dst);
760 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
763 for (i = 0; i < src->n_div; ++i) {
764 int i1 = isl_basic_map_alloc_div(dst);
767 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
771 isl_basic_map_free(src);
776 isl_basic_map_free(src);
777 isl_basic_map_free(dst);
781 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
782 struct isl_basic_set *bset2, unsigned pos)
784 return (struct isl_basic_set *)
785 add_constraints((struct isl_basic_map *)bset1,
786 (struct isl_basic_map *)bset2, 0, pos);
789 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
790 struct isl_dim *dim, unsigned extra,
791 unsigned n_eq, unsigned n_ineq)
793 struct isl_basic_map *ext;
800 base = isl_basic_map_cow(base);
804 dims_ok = isl_dim_equal(base->dim, dim) &&
805 base->extra >= base->n_div + extra;
807 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
808 room_for_ineq(base, n_ineq)) {
813 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
814 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
815 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
816 extra += base->extra;
818 n_ineq += base->n_ineq;
820 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
826 ext = add_constraints(ext, base, 0, 0);
829 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
836 isl_basic_map_free(base);
840 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
841 struct isl_dim *dim, unsigned extra,
842 unsigned n_eq, unsigned n_ineq)
844 return (struct isl_basic_set *)
845 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
846 extra, n_eq, n_ineq);
849 struct isl_basic_map *isl_basic_map_extend_constraints(
850 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
854 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
858 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
859 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
860 unsigned n_eq, unsigned n_ineq)
862 struct isl_basic_map *bmap;
867 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
871 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
875 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
876 unsigned nparam, unsigned dim, unsigned extra,
877 unsigned n_eq, unsigned n_ineq)
879 return (struct isl_basic_set *)
880 isl_basic_map_extend((struct isl_basic_map *)base,
881 nparam, 0, dim, extra, n_eq, n_ineq);
884 struct isl_basic_set *isl_basic_set_extend_constraints(
885 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
887 return (struct isl_basic_set *)
888 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
892 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
894 return (struct isl_basic_set *)
895 isl_basic_map_cow((struct isl_basic_map *)bset);
898 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
905 bmap = isl_basic_map_dup(bmap);
907 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
911 struct isl_set *isl_set_cow(struct isl_set *set)
919 return isl_set_dup(set);
922 struct isl_map *isl_map_cow(struct isl_map *map)
930 return isl_map_dup(map);
933 static void swap_vars(struct isl_blk blk, isl_int *a,
934 unsigned a_len, unsigned b_len)
936 isl_seq_cpy(blk.data, a+a_len, b_len);
937 isl_seq_cpy(blk.data+b_len, a, a_len);
938 isl_seq_cpy(a, blk.data, b_len+a_len);
941 struct isl_basic_set *isl_basic_set_swap_vars(
942 struct isl_basic_set *bset, unsigned n)
952 nparam = isl_basic_set_n_param(bset);
953 dim = isl_basic_set_n_dim(bset);
954 isl_assert(bset->ctx, n <= dim, goto error);
959 bset = isl_basic_set_cow(bset);
963 blk = isl_blk_alloc(bset->ctx, dim);
964 if (isl_blk_is_error(blk))
967 for (i = 0; i < bset->n_eq; ++i)
969 bset->eq[i]+1+nparam, n, dim - n);
971 for (i = 0; i < bset->n_ineq; ++i)
973 bset->ineq[i]+1+nparam, n, dim - n);
975 for (i = 0; i < bset->n_div; ++i)
977 bset->div[i]+1+1+nparam, n, dim - n);
979 isl_blk_free(bset->ctx, blk);
981 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
985 isl_basic_set_free(bset);
989 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
992 set = isl_set_cow(set);
996 for (i = 0; i < set->n; ++i) {
997 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1003 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1007 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1013 total = isl_basic_map_total_dim(bmap);
1014 isl_basic_map_free_div(bmap, bmap->n_div);
1015 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1017 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1019 isl_basic_map_alloc_equality(bmap);
1023 isl_int_set_si(bmap->eq[i][0], 1);
1024 isl_seq_clr(bmap->eq[i]+1, total);
1025 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1026 return isl_basic_map_finalize(bmap);
1028 isl_basic_map_free(bmap);
1032 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1034 return (struct isl_basic_set *)
1035 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1038 static void swap_div(struct isl_basic_map *bmap, int a, int b)
1041 unsigned off = isl_dim_total(bmap->dim);
1042 isl_int *t = bmap->div[a];
1043 bmap->div[a] = bmap->div[b];
1046 for (i = 0; i < bmap->n_eq; ++i)
1047 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1049 for (i = 0; i < bmap->n_ineq; ++i)
1050 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1052 for (i = 0; i < bmap->n_div; ++i)
1053 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1054 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1057 /* Eliminate the specified n dimensions starting at first from the
1058 * constraints using Fourier-Motzkin, The dimensions themselves
1061 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1062 unsigned first, unsigned n)
1072 set = isl_set_cow(set);
1075 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1076 nparam = isl_set_n_param(set);
1078 for (i = 0; i < set->n; ++i) {
1079 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1090 /* Project out n dimensions starting at first using Fourier-Motzkin */
1091 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1092 unsigned first, unsigned n)
1094 set = isl_set_eliminate_dims(set, first, n);
1095 set = isl_set_drop_dims(set, first, n);
1099 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1101 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1109 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1118 set = isl_set_cow(set);
1122 for (i = 0; i < set->n; ++i) {
1123 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1133 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1134 enum isl_dim_type type, unsigned first, unsigned n)
1138 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1142 bmap = isl_basic_map_eliminate_vars(bmap,
1143 isl_basic_map_offset(bmap, type) - 1 + first, n);
1144 bmap = isl_basic_map_drop(bmap, type, first, n);
1147 isl_basic_map_free(bmap);
1151 struct isl_map *isl_map_remove(struct isl_map *map,
1152 enum isl_dim_type type, unsigned first, unsigned n)
1160 map = isl_map_cow(map);
1163 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1165 for (i = 0; i < map->n; ++i) {
1166 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1167 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1171 map = isl_map_drop(map, type, first, n);
1178 /* Project out n inputs starting at first using Fourier-Motzkin */
1179 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1180 unsigned first, unsigned n)
1182 return isl_map_remove(map, isl_dim_in, first, n);
1185 /* Project out n dimensions starting at first using Fourier-Motzkin */
1186 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1187 unsigned first, unsigned n)
1189 unsigned nparam = isl_basic_set_n_param(bset);
1190 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1191 bset = isl_basic_set_drop_dims(bset, first, n);
1195 static void dump_term(struct isl_basic_map *bmap,
1196 isl_int c, int pos, FILE *out)
1199 unsigned in = isl_basic_map_n_in(bmap);
1200 unsigned dim = in + isl_basic_map_n_out(bmap);
1201 unsigned nparam = isl_basic_map_n_param(bmap);
1203 isl_int_print(out, c, 0);
1205 if (!isl_int_is_one(c))
1206 isl_int_print(out, c, 0);
1207 if (pos < 1 + nparam) {
1208 name = isl_dim_get_name(bmap->dim,
1209 isl_dim_param, pos - 1);
1211 fprintf(out, "%s", name);
1213 fprintf(out, "p%d", pos - 1);
1214 } else if (pos < 1 + nparam + in)
1215 fprintf(out, "i%d", pos - 1 - nparam);
1216 else if (pos < 1 + nparam + dim)
1217 fprintf(out, "o%d", pos - 1 - nparam - in);
1219 fprintf(out, "e%d", pos - 1 - nparam - dim);
1223 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1224 int sign, FILE *out)
1228 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1232 for (i = 0, first = 1; i < len; ++i) {
1233 if (isl_int_sgn(c[i]) * sign <= 0)
1236 fprintf(out, " + ");
1238 isl_int_abs(v, c[i]);
1239 dump_term(bmap, v, i, out);
1246 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1247 const char *op, FILE *out, int indent)
1251 fprintf(out, "%*s", indent, "");
1253 dump_constraint_sign(bmap, c, 1, out);
1254 fprintf(out, " %s ", op);
1255 dump_constraint_sign(bmap, c, -1, out);
1259 for (i = bmap->n_div; i < bmap->extra; ++i) {
1260 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1262 fprintf(out, "%*s", indent, "");
1263 fprintf(out, "ERROR: unused div coefficient not zero\n");
1268 static void dump_constraints(struct isl_basic_map *bmap,
1269 isl_int **c, unsigned n,
1270 const char *op, FILE *out, int indent)
1274 for (i = 0; i < n; ++i)
1275 dump_constraint(bmap, c[i], op, out, indent);
1278 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1282 unsigned total = isl_basic_map_total_dim(bmap);
1284 for (j = 0; j < 1 + total; ++j) {
1285 if (isl_int_is_zero(exp[j]))
1287 if (!first && isl_int_is_pos(exp[j]))
1289 dump_term(bmap, exp[j], j, out);
1294 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1298 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1299 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1301 for (i = 0; i < bmap->n_div; ++i) {
1302 fprintf(out, "%*s", indent, "");
1303 fprintf(out, "e%d = [(", i);
1304 dump_affine(bmap, bmap->div[i]+1, out);
1306 isl_int_print(out, bmap->div[i][0], 0);
1307 fprintf(out, "]\n");
1311 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1314 fprintf(out, "null basic set\n");
1318 fprintf(out, "%*s", indent, "");
1319 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1320 bset->ref, bset->dim->nparam, bset->dim->n_out,
1321 bset->extra, bset->flags);
1322 dump((struct isl_basic_map *)bset, out, indent);
1325 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1328 fprintf(out, "null basic map\n");
1332 fprintf(out, "%*s", indent, "");
1333 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1334 "flags: %x, n_name: %d\n",
1336 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1337 bmap->extra, bmap->flags, bmap->dim->n_name);
1338 dump(bmap, out, indent);
1341 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1346 total = isl_basic_map_total_dim(bmap);
1347 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1348 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1349 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1350 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1354 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1356 struct isl_set *set;
1360 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1361 isl_assert(dim->ctx, n >= 0, return NULL);
1362 set = isl_alloc(dim->ctx, struct isl_set,
1363 sizeof(struct isl_set) +
1364 n * sizeof(struct isl_basic_set *));
1368 set->ctx = dim->ctx;
1369 isl_ctx_ref(set->ctx);
1381 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1382 unsigned nparam, unsigned dim, int n, unsigned flags)
1384 struct isl_set *set;
1385 struct isl_dim *dims;
1387 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1391 set = isl_set_alloc_dim(dims, n, flags);
1395 struct isl_set *isl_set_dup(struct isl_set *set)
1398 struct isl_set *dup;
1403 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1406 for (i = 0; i < set->n; ++i)
1407 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1411 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1413 struct isl_set *set;
1418 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1420 isl_basic_set_free(bset);
1423 return isl_set_add(set, bset);
1426 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1428 struct isl_map *map;
1433 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1435 isl_basic_map_free(bmap);
1438 return isl_map_add(map, bmap);
1441 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1445 isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1446 isl_assert(set->ctx, set->n < set->size, goto error);
1447 set->p[set->n] = bset;
1454 isl_basic_set_free(bset);
1458 void isl_set_free(struct isl_set *set)
1468 isl_ctx_deref(set->ctx);
1469 for (i = 0; i < set->n; ++i)
1470 isl_basic_set_free(set->p[i]);
1471 isl_dim_free(set->dim);
1475 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1480 fprintf(out, "null set\n");
1484 fprintf(out, "%*s", indent, "");
1485 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1486 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1488 for (i = 0; i < set->n; ++i) {
1489 fprintf(out, "%*s", indent, "");
1490 fprintf(out, "basic set %d:\n", i);
1491 isl_basic_set_dump(set->p[i], out, indent+4);
1495 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1500 fprintf(out, "null map\n");
1504 fprintf(out, "%*s", indent, "");
1505 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1506 "flags: %x, n_name: %d\n",
1507 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1508 map->dim->n_out, map->flags, map->dim->n_name);
1509 for (i = 0; i < map->n; ++i) {
1510 fprintf(out, "%*s", indent, "");
1511 fprintf(out, "basic map %d:\n", i);
1512 isl_basic_map_dump(map->p[i], out, indent+4);
1516 struct isl_basic_map *isl_basic_map_intersect_domain(
1517 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1519 struct isl_basic_map *bmap_domain;
1520 struct isl_dim *dim;
1525 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1526 bset->dim, isl_dim_param), goto error);
1528 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1529 isl_assert(bset->ctx,
1530 isl_basic_map_compatible_domain(bmap, bset), goto error);
1532 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1533 bset->n_div, bset->n_eq, bset->n_ineq);
1536 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1537 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1538 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1540 bmap = isl_basic_map_simplify(bmap);
1541 return isl_basic_map_finalize(bmap);
1543 isl_basic_map_free(bmap);
1544 isl_basic_set_free(bset);
1548 struct isl_basic_map *isl_basic_map_intersect_range(
1549 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1551 struct isl_basic_map *bmap_range;
1556 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1557 bset->dim, isl_dim_param), goto error);
1559 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1560 isl_assert(bset->ctx,
1561 isl_basic_map_compatible_range(bmap, bset), goto error);
1563 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1564 bset->n_div, bset->n_eq, bset->n_ineq);
1567 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1568 bmap = add_constraints(bmap, bmap_range, 0, 0);
1570 bmap = isl_basic_map_simplify(bmap);
1571 return isl_basic_map_finalize(bmap);
1573 isl_basic_map_free(bmap);
1574 isl_basic_set_free(bset);
1578 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1584 total = 1 + isl_basic_map_total_dim(bmap);
1585 if (total != vec->size)
1590 for (i = 0; i < bmap->n_eq; ++i) {
1591 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
1592 if (!isl_int_is_zero(s)) {
1598 for (i = 0; i < bmap->n_ineq; ++i) {
1599 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
1600 if (isl_int_is_neg(s)) {
1611 struct isl_basic_map *isl_basic_map_intersect(
1612 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1614 struct isl_vec *sample = NULL;
1616 if (!bmap1 || !bmap2)
1619 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1620 bmap2->dim, isl_dim_param), goto error);
1621 if (isl_dim_total(bmap1->dim) ==
1622 isl_dim_size(bmap1->dim, isl_dim_param) &&
1623 isl_dim_total(bmap2->dim) !=
1624 isl_dim_size(bmap2->dim, isl_dim_param))
1625 return isl_basic_map_intersect(bmap2, bmap1);
1627 if (isl_dim_total(bmap2->dim) !=
1628 isl_dim_size(bmap2->dim, isl_dim_param))
1629 isl_assert(bmap1->ctx,
1630 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1632 if (bmap1->sample &&
1633 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1634 basic_map_contains(bmap2, bmap1->sample) > 0)
1635 sample = isl_vec_copy(bmap1->ctx, bmap1->sample);
1636 else if (bmap2->sample &&
1637 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1638 basic_map_contains(bmap2, bmap2->sample) > 0)
1639 sample = isl_vec_copy(bmap2->ctx, bmap2->sample);
1641 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1642 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1645 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1648 isl_vec_free(bmap1->ctx, bmap1->sample);
1649 bmap1->sample = sample;
1652 bmap1 = isl_basic_map_simplify(bmap1);
1653 return isl_basic_map_finalize(bmap1);
1656 isl_vec_free(bmap1->ctx, sample);
1657 isl_basic_map_free(bmap1);
1658 isl_basic_map_free(bmap2);
1662 struct isl_basic_set *isl_basic_set_intersect(
1663 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1665 return (struct isl_basic_set *)
1666 isl_basic_map_intersect(
1667 (struct isl_basic_map *)bset1,
1668 (struct isl_basic_map *)bset2);
1671 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1674 struct isl_map *result;
1680 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1681 map2->dim, isl_dim_param), goto error);
1682 if (isl_dim_total(map1->dim) ==
1683 isl_dim_size(map1->dim, isl_dim_param) &&
1684 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1685 return isl_map_intersect(map2, map1);
1687 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1688 isl_assert(map1->ctx,
1689 isl_dim_equal(map1->dim, map2->dim), goto error);
1691 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1692 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1693 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1695 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1696 map1->n * map2->n, flags);
1699 for (i = 0; i < map1->n; ++i)
1700 for (j = 0; j < map2->n; ++j) {
1701 struct isl_basic_map *part;
1702 part = isl_basic_map_intersect(
1703 isl_basic_map_copy(map1->p[i]),
1704 isl_basic_map_copy(map2->p[j]));
1705 if (isl_basic_map_is_empty(part))
1706 isl_basic_map_free(part);
1708 result = isl_map_add(result, part);
1721 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1723 return (struct isl_set *)
1724 isl_map_intersect((struct isl_map *)set1,
1725 (struct isl_map *)set2);
1728 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1730 struct isl_dim *dim;
1731 struct isl_basic_set *bset;
1736 bmap = isl_basic_map_cow(bmap);
1739 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1740 in = isl_basic_map_n_in(bmap);
1741 bset = isl_basic_set_from_basic_map(bmap);
1742 bset = isl_basic_set_swap_vars(bset, in);
1743 return isl_basic_map_from_basic_set(bset, dim);
1746 /* Turn final n dimensions into existentially quantified variables.
1748 struct isl_basic_set *isl_basic_set_project_out(
1749 struct isl_basic_set *bset, unsigned n, unsigned flags)
1759 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1764 bset = isl_basic_set_cow(bset);
1766 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1767 old = bset->block2.data;
1768 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1769 (bset->extra + n) * (1 + row_size));
1770 if (!bset->block2.data)
1772 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1775 for (i = 0; i < n; ++i) {
1776 new_div[i] = bset->block2.data +
1777 (bset->extra + i) * (1 + row_size);
1778 isl_seq_clr(new_div[i], 1 + row_size);
1780 for (i = 0; i < bset->extra; ++i)
1781 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1783 bset->div = new_div;
1786 bset->dim = isl_dim_drop_outputs(bset->dim,
1787 isl_basic_set_n_dim(bset) - n, n);
1790 bset = isl_basic_set_simplify(bset);
1791 return isl_basic_set_finalize(bset);
1793 isl_basic_set_free(bset);
1797 struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1801 for (i = 0; i < n; ++i) {
1802 j = isl_basic_map_alloc_div(bmap);
1805 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1809 isl_basic_map_free(bmap);
1813 struct isl_basic_map *isl_basic_map_apply_range(
1814 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1816 struct isl_dim *dim_result = NULL;
1817 struct isl_basic_map *bmap;
1818 unsigned n_in, n_out, n, nparam, total, pos;
1819 struct isl_dim_map *dim_map1, *dim_map2;
1821 if (!bmap1 || !bmap2)
1824 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1825 isl_dim_copy(bmap2->dim));
1827 n_in = isl_basic_map_n_in(bmap1);
1828 n_out = isl_basic_map_n_out(bmap2);
1829 n = isl_basic_map_n_out(bmap1);
1830 nparam = isl_basic_map_n_param(bmap1);
1832 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1833 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1834 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1835 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1836 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1837 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1838 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1839 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1840 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1841 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1842 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1844 bmap = isl_basic_map_alloc_dim(dim_result,
1845 bmap1->n_div + bmap2->n_div + n,
1846 bmap1->n_eq + bmap2->n_eq,
1847 bmap1->n_ineq + bmap2->n_ineq);
1848 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1849 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1850 bmap = add_divs(bmap, n);
1851 bmap = isl_basic_map_simplify(bmap);
1852 return isl_basic_map_finalize(bmap);
1854 isl_basic_map_free(bmap1);
1855 isl_basic_map_free(bmap2);
1859 struct isl_basic_set *isl_basic_set_apply(
1860 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1865 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1868 return (struct isl_basic_set *)
1869 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1871 isl_basic_set_free(bset);
1872 isl_basic_map_free(bmap);
1876 struct isl_basic_map *isl_basic_map_apply_domain(
1877 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1879 if (!bmap1 || !bmap2)
1883 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1885 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1888 bmap1 = isl_basic_map_reverse(bmap1);
1889 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1890 return isl_basic_map_reverse(bmap1);
1892 isl_basic_map_free(bmap1);
1893 isl_basic_map_free(bmap2);
1897 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1898 * A \cap B -> f(A) + f(B)
1900 struct isl_basic_map *isl_basic_map_sum(
1901 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1903 unsigned n_in, n_out, nparam, total, pos;
1904 struct isl_basic_map *bmap = NULL;
1905 struct isl_dim_map *dim_map1, *dim_map2;
1908 if (!bmap1 || !bmap2)
1911 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1914 nparam = isl_basic_map_n_param(bmap1);
1915 n_in = isl_basic_map_n_in(bmap1);
1916 n_out = isl_basic_map_n_out(bmap1);
1918 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
1919 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1920 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
1921 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1922 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
1923 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1924 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1925 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
1926 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1927 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1928 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
1930 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
1931 bmap1->n_div + bmap2->n_div + 2 * n_out,
1932 bmap1->n_eq + bmap2->n_eq + n_out,
1933 bmap1->n_ineq + bmap2->n_ineq);
1934 for (i = 0; i < n_out; ++i) {
1935 int j = isl_basic_map_alloc_equality(bmap);
1938 isl_seq_clr(bmap->eq[j], 1+total);
1939 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
1940 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
1941 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
1943 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1944 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1945 bmap = add_divs(bmap, 2 * n_out);
1947 bmap = isl_basic_map_simplify(bmap);
1948 return isl_basic_map_finalize(bmap);
1950 isl_basic_map_free(bmap);
1951 isl_basic_map_free(bmap1);
1952 isl_basic_map_free(bmap2);
1956 /* Given a basic map A -> f(A), construct A -> -f(A).
1958 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
1963 bmap = isl_basic_map_cow(bmap);
1967 n = isl_basic_map_dim(bmap, isl_dim_out);
1968 off = isl_basic_map_offset(bmap, isl_dim_out);
1969 for (i = 0; i < bmap->n_eq; ++i)
1970 for (j = 0; j < n; ++j)
1971 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
1972 for (i = 0; i < bmap->n_ineq; ++i)
1973 for (j = 0; j < n; ++j)
1974 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
1975 for (i = 0; i < bmap->n_div; ++i)
1976 for (j = 0; j < n; ++j)
1977 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
1978 return isl_basic_map_finalize(bmap);
1981 /* Given a basic map A -> f(A) and an integer d, construct a basic map
1982 * A -> floor(f(A)/d).
1984 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
1987 unsigned n_in, n_out, nparam, total, pos;
1988 struct isl_basic_map *result = NULL;
1989 struct isl_dim_map *dim_map;
1995 nparam = isl_basic_map_n_param(bmap);
1996 n_in = isl_basic_map_n_in(bmap);
1997 n_out = isl_basic_map_n_out(bmap);
1999 total = nparam + n_in + n_out + bmap->n_div + n_out;
2000 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2001 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2002 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2003 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2004 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2006 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2007 bmap->n_div + n_out,
2008 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2009 result = add_constraints_dim_map(result, bmap, dim_map);
2010 result = add_divs(result, n_out);
2011 for (i = 0; i < n_out; ++i) {
2013 j = isl_basic_map_alloc_inequality(result);
2016 isl_seq_clr(result->ineq[j], 1+total);
2017 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2018 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2019 j = isl_basic_map_alloc_inequality(result);
2022 isl_seq_clr(result->ineq[j], 1+total);
2023 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2024 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2025 isl_int_sub_ui(result->ineq[j][0], d, 1);
2028 result = isl_basic_map_simplify(result);
2029 return isl_basic_map_finalize(result);
2031 isl_basic_map_free(result);
2035 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2041 i = isl_basic_map_alloc_equality(bmap);
2044 nparam = isl_basic_map_n_param(bmap);
2045 n_in = isl_basic_map_n_in(bmap);
2046 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2047 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2048 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2049 return isl_basic_map_finalize(bmap);
2051 isl_basic_map_free(bmap);
2055 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2061 i = isl_basic_map_alloc_inequality(bmap);
2064 nparam = isl_basic_map_n_param(bmap);
2065 n_in = isl_basic_map_n_in(bmap);
2066 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2067 isl_int_set_si(bmap->ineq[i][0], -1);
2068 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2069 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2070 return isl_basic_map_finalize(bmap);
2072 isl_basic_map_free(bmap);
2076 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2082 i = isl_basic_map_alloc_inequality(bmap);
2085 nparam = isl_basic_map_n_param(bmap);
2086 n_in = isl_basic_map_n_in(bmap);
2087 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2088 isl_int_set_si(bmap->ineq[i][0], -1);
2089 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2090 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2091 return isl_basic_map_finalize(bmap);
2093 isl_basic_map_free(bmap);
2097 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2100 struct isl_basic_map *bmap;
2101 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2104 for (i = 0; i < n_equal && bmap; ++i)
2105 bmap = var_equal(bmap, i);
2106 return isl_basic_map_finalize(bmap);
2109 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2112 struct isl_basic_map *bmap;
2113 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2116 for (i = 0; i < pos && bmap; ++i)
2117 bmap = var_equal(bmap, i);
2119 bmap = var_less(bmap, pos);
2120 return isl_basic_map_finalize(bmap);
2123 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2126 struct isl_basic_map *bmap;
2127 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2130 for (i = 0; i < pos && bmap; ++i)
2131 bmap = var_equal(bmap, i);
2133 bmap = var_more(bmap, pos);
2134 return isl_basic_map_finalize(bmap);
2137 struct isl_basic_map *isl_basic_map_from_basic_set(
2138 struct isl_basic_set *bset, struct isl_dim *dim)
2140 struct isl_basic_map *bmap;
2142 bset = isl_basic_set_cow(bset);
2146 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2147 isl_dim_free(bset->dim);
2148 bmap = (struct isl_basic_map *) bset;
2150 return isl_basic_map_finalize(bmap);
2152 isl_basic_set_free(bset);
2157 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2161 if (bmap->dim->n_in == 0)
2162 return (struct isl_basic_set *)bmap;
2163 bmap = isl_basic_map_cow(bmap);
2166 bmap->dim = isl_dim_cow(bmap->dim);
2169 bmap->dim->n_out += bmap->dim->n_in;
2170 bmap->dim->n_in = 0;
2171 bmap = isl_basic_map_finalize(bmap);
2172 return (struct isl_basic_set *)bmap;
2174 isl_basic_map_free(bmap);
2178 /* For a div d = floor(f/m), add the constraints
2181 * -(f-(n-1)) + m d >= 0
2183 * Note that the second constraint is the negation of
2187 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2190 unsigned total = isl_basic_map_total_dim(bmap);
2191 unsigned div_pos = 1 + total - bmap->n_div + div;
2193 i = isl_basic_map_alloc_inequality(bmap);
2196 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2197 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2199 j = isl_basic_map_alloc_inequality(bmap);
2202 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2203 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2204 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2208 struct isl_basic_set *isl_basic_map_underlying_set(
2209 struct isl_basic_map *bmap)
2213 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2214 return (struct isl_basic_set *)bmap;
2215 bmap = isl_basic_map_cow(bmap);
2218 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2221 bmap->extra -= bmap->n_div;
2223 bmap = isl_basic_map_finalize(bmap);
2224 return (struct isl_basic_set *)bmap;
2229 struct isl_basic_map *isl_basic_map_overlying_set(
2230 struct isl_basic_set *bset, struct isl_basic_map *like)
2232 struct isl_basic_map *bmap;
2233 struct isl_ctx *ctx;
2240 isl_assert(ctx, bset->n_div == 0, goto error);
2241 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2242 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2244 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2245 isl_basic_map_free(like);
2246 return (struct isl_basic_map *)bset;
2248 bset = isl_basic_set_cow(bset);
2251 total = bset->dim->n_out + bset->extra;
2252 bmap = (struct isl_basic_map *)bset;
2253 isl_dim_free(bmap->dim);
2254 bmap->dim = isl_dim_copy(like->dim);
2257 bmap->n_div = like->n_div;
2258 bmap->extra += like->n_div;
2261 ltotal = total - bmap->extra + like->extra;
2264 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2265 bmap->extra * (1 + 1 + total));
2266 if (isl_blk_is_error(bmap->block2))
2268 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2272 for (i = 0; i < bmap->extra; ++i)
2273 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2274 for (i = 0; i < like->n_div; ++i) {
2275 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2276 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2278 bmap = isl_basic_map_extend_constraints(bmap,
2279 0, 2 * like->n_div);
2280 for (i = 0; i < like->n_div; ++i) {
2281 if (isl_int_is_zero(bmap->div[i][0]))
2283 if (add_div_constraints(bmap, i) < 0)
2287 isl_basic_map_free(like);
2288 bmap = isl_basic_map_simplify(bmap);
2289 bmap = isl_basic_map_finalize(bmap);
2292 isl_basic_map_free(like);
2293 isl_basic_set_free(bset);
2297 struct isl_basic_set *isl_basic_set_from_underlying_set(
2298 struct isl_basic_set *bset, struct isl_basic_set *like)
2300 return (struct isl_basic_set *)
2301 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2304 struct isl_set *isl_set_from_underlying_set(
2305 struct isl_set *set, struct isl_basic_set *like)
2311 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2313 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2314 isl_basic_set_free(like);
2317 set = isl_set_cow(set);
2320 for (i = 0; i < set->n; ++i) {
2321 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2322 isl_basic_set_copy(like));
2326 isl_dim_free(set->dim);
2327 set->dim = isl_dim_copy(like->dim);
2330 isl_basic_set_free(like);
2333 isl_basic_set_free(like);
2338 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2342 map = isl_map_cow(map);
2345 map->dim = isl_dim_cow(map->dim);
2349 for (i = 1; i < map->n; ++i)
2350 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2352 for (i = 0; i < map->n; ++i) {
2353 map->p[i] = (struct isl_basic_map *)
2354 isl_basic_map_underlying_set(map->p[i]);
2359 map->dim = isl_dim_underlying(map->dim, 0);
2361 isl_dim_free(map->dim);
2362 map->dim = isl_dim_copy(map->p[0]->dim);
2366 return (struct isl_set *)map;
2372 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2374 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2377 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2379 struct isl_basic_set *domain;
2383 n_out = isl_basic_map_n_out(bmap);
2384 domain = isl_basic_set_from_basic_map(bmap);
2385 return isl_basic_set_project_out(domain, n_out, 0);
2388 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2390 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2393 struct isl_set *isl_map_range(struct isl_map *map)
2396 struct isl_set *set;
2400 map = isl_map_cow(map);
2404 set = (struct isl_set *) map;
2405 if (set->dim->n_in != 0) {
2406 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2410 for (i = 0; i < map->n; ++i) {
2411 set->p[i] = isl_basic_map_range(map->p[i]);
2415 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2416 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2423 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2426 struct isl_map *map = NULL;
2428 set = isl_set_cow(set);
2431 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2432 map = (struct isl_map *)set;
2433 for (i = 0; i < set->n; ++i) {
2434 map->p[i] = isl_basic_map_from_basic_set(
2435 set->p[i], isl_dim_copy(dim));
2439 isl_dim_free(map->dim);
2448 struct isl_map *isl_map_from_range(struct isl_set *set)
2450 return (struct isl_map *)set;
2453 struct isl_set *isl_set_from_map(struct isl_map *map)
2456 struct isl_set *set = NULL;
2460 map = isl_map_cow(map);
2463 map->dim = isl_dim_cow(map->dim);
2466 map->dim->n_out += map->dim->n_in;
2468 set = (struct isl_set *)map;
2469 for (i = 0; i < map->n; ++i) {
2470 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2480 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2482 struct isl_map *map;
2486 isl_assert(dim->ctx, n >= 0, return NULL);
2487 map = isl_alloc(dim->ctx, struct isl_map,
2488 sizeof(struct isl_map) +
2489 n * sizeof(struct isl_basic_map *));
2493 map->ctx = dim->ctx;
2494 isl_ctx_ref(map->ctx);
2506 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2507 unsigned nparam, unsigned in, unsigned out, int n,
2510 struct isl_map *map;
2511 struct isl_dim *dims;
2513 dims = isl_dim_alloc(ctx, nparam, in, out);
2517 map = isl_map_alloc_dim(dims, n, flags);
2521 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2522 unsigned nparam, unsigned in, unsigned out)
2524 struct isl_basic_map *bmap;
2525 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2526 bmap = isl_basic_map_set_to_empty(bmap);
2530 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2532 struct isl_basic_set *bset;
2533 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2534 bset = isl_basic_set_set_to_empty(bset);
2538 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2540 struct isl_basic_map *bmap;
2543 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2544 bmap = isl_basic_map_set_to_empty(bmap);
2548 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2550 struct isl_basic_map *bmap;
2553 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2554 bmap = isl_basic_map_set_to_empty(bmap);
2558 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2560 struct isl_basic_set *bset;
2563 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2564 bset = isl_basic_set_set_to_empty(bset);
2568 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2570 struct isl_basic_map *bmap;
2571 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2575 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2577 struct isl_basic_set *bset;
2578 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2582 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2586 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2589 struct isl_map *isl_map_empty(struct isl_dim *dim)
2591 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2594 struct isl_map *isl_map_empty_like(struct isl_map *model)
2598 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2601 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2605 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2608 struct isl_set *isl_set_empty(struct isl_dim *dim)
2610 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2613 struct isl_set *isl_set_empty_like(struct isl_set *model)
2617 return isl_set_empty(isl_dim_copy(model->dim));
2620 struct isl_set *isl_set_universe(struct isl_dim *dim)
2622 struct isl_set *set;
2625 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2626 set = isl_set_add(set, isl_basic_set_universe(dim));
2630 struct isl_map *isl_map_dup(struct isl_map *map)
2633 struct isl_map *dup;
2637 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2638 for (i = 0; i < map->n; ++i)
2639 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2643 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2647 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2648 isl_assert(map->ctx, map->n < map->size, goto error);
2649 map->p[map->n] = bmap;
2651 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2657 isl_basic_map_free(bmap);
2661 void isl_map_free(struct isl_map *map)
2671 isl_ctx_deref(map->ctx);
2672 for (i = 0; i < map->n; ++i)
2673 isl_basic_map_free(map->p[i]);
2674 isl_dim_free(map->dim);
2678 struct isl_map *isl_map_extend(struct isl_map *base,
2679 unsigned nparam, unsigned n_in, unsigned n_out)
2683 base = isl_map_cow(base);
2687 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2690 for (i = 0; i < base->n; ++i) {
2691 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2692 isl_dim_copy(base->dim), 0, 0, 0);
2702 struct isl_set *isl_set_extend(struct isl_set *base,
2703 unsigned nparam, unsigned dim)
2705 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2709 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2710 unsigned pos, int value)
2714 bmap = isl_basic_map_cow(bmap);
2715 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2716 j = isl_basic_map_alloc_equality(bmap);
2719 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2720 isl_int_set_si(bmap->eq[j][pos], -1);
2721 isl_int_set_si(bmap->eq[j][0], value);
2722 bmap = isl_basic_map_simplify(bmap);
2723 return isl_basic_map_finalize(bmap);
2725 isl_basic_map_free(bmap);
2729 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2730 enum isl_dim_type type, unsigned pos, int value)
2734 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2735 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2738 isl_basic_map_free(bmap);
2742 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2743 enum isl_dim_type type, unsigned pos, int value)
2745 return (struct isl_basic_set *)
2746 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2750 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2751 unsigned input, int value)
2753 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2756 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2757 unsigned dim, int value)
2759 return (struct isl_basic_set *)
2760 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2761 isl_dim_set, dim, value);
2764 struct isl_map *isl_map_fix_si(struct isl_map *map,
2765 enum isl_dim_type type, unsigned pos, int value)
2769 map = isl_map_cow(map);
2773 isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2774 for (i = 0; i < map->n; ++i) {
2775 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2779 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2786 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2787 unsigned input, int value)
2789 return isl_map_fix_si(map, isl_dim_in, input, value);
2792 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2794 return (struct isl_set *)
2795 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2798 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2799 unsigned dim, isl_int value)
2804 bset = isl_basic_set_cow(bset);
2805 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2806 j = isl_basic_set_alloc_inequality(bset);
2809 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2810 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2811 isl_int_neg(bset->ineq[j][0], value);
2812 bset = isl_basic_set_simplify(bset);
2813 return isl_basic_set_finalize(bset);
2815 isl_basic_set_free(bset);
2819 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2824 set = isl_set_cow(set);
2828 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2829 for (i = 0; i < set->n; ++i) {
2830 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2840 struct isl_map *isl_map_reverse(struct isl_map *map)
2845 map = isl_map_cow(map);
2849 map->dim = isl_dim_reverse(map->dim);
2852 for (i = 0; i < map->n; ++i) {
2853 map->p[i] = isl_basic_map_reverse(map->p[i]);
2857 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2864 struct isl_map *isl_basic_map_lexmax(
2865 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2866 struct isl_set **empty)
2868 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2871 struct isl_map *isl_basic_map_lexmin(
2872 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2873 struct isl_set **empty)
2875 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2878 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2880 struct isl_basic_map *bmap = NULL;
2881 struct isl_basic_set *dom = NULL;
2882 struct isl_map *min;
2883 struct isl_dim *param_dim;
2887 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2890 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2891 dom = isl_basic_set_universe(param_dim);
2894 min = isl_basic_map_lexmin(bmap, dom, NULL);
2895 return isl_map_range(min);
2897 isl_basic_map_free(bmap);
2901 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2908 off = isl_dim_total(bmap->dim);
2909 for (i = 0; i < bmap->n_div; ++i) {
2910 if (isl_int_is_zero(bmap->div[i][0]))
2911 return isl_pip_basic_map_compute_divs(bmap);
2912 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2915 return isl_map_from_basic_map(bmap);
2917 isl_basic_map_free(bmap);
2921 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2924 struct isl_map *res;
2930 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2931 for (i = 1 ; i < map->n; ++i) {
2933 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2934 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
2935 res = isl_map_union_disjoint(res, r2);
2937 res = isl_map_union(res, r2);
2944 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2946 return (struct isl_set *)
2947 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2950 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2952 return (struct isl_set *)
2953 isl_map_compute_divs((struct isl_map *)set);
2956 struct isl_set *isl_map_domain(struct isl_map *map)
2959 struct isl_set *set;
2964 map = isl_map_cow(map);
2968 set = (struct isl_set *)map;
2969 set->dim = isl_dim_domain(set->dim);
2972 for (i = 0; i < map->n; ++i) {
2973 set->p[i] = isl_basic_map_domain(map->p[i]);
2977 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2978 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2985 struct isl_map *isl_map_union_disjoint(
2986 struct isl_map *map1, struct isl_map *map2)
2990 struct isl_map *map = NULL;
3004 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3006 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3007 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3008 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3010 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3011 map1->n + map2->n, flags);
3014 for (i = 0; i < map1->n; ++i) {
3015 map = isl_map_add(map,
3016 isl_basic_map_copy(map1->p[i]));
3020 for (i = 0; i < map2->n; ++i) {
3021 map = isl_map_add(map,
3022 isl_basic_map_copy(map2->p[i]));
3036 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3038 map1 = isl_map_union_disjoint(map1, map2);
3042 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3046 struct isl_set *isl_set_union_disjoint(
3047 struct isl_set *set1, struct isl_set *set2)
3049 return (struct isl_set *)
3050 isl_map_union_disjoint(
3051 (struct isl_map *)set1, (struct isl_map *)set2);
3054 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3056 return (struct isl_set *)
3057 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3060 struct isl_map *isl_map_intersect_range(
3061 struct isl_map *map, struct isl_set *set)
3064 struct isl_map *result;
3070 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3071 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3072 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3074 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3075 map->n * set->n, flags);
3078 for (i = 0; i < map->n; ++i)
3079 for (j = 0; j < set->n; ++j) {
3080 result = isl_map_add(result,
3081 isl_basic_map_intersect_range(
3082 isl_basic_map_copy(map->p[i]),
3083 isl_basic_set_copy(set->p[j])));
3096 struct isl_map *isl_map_intersect_domain(
3097 struct isl_map *map, struct isl_set *set)
3099 return isl_map_reverse(
3100 isl_map_intersect_range(isl_map_reverse(map), set));
3103 struct isl_map *isl_map_apply_domain(
3104 struct isl_map *map1, struct isl_map *map2)
3108 map1 = isl_map_reverse(map1);
3109 map1 = isl_map_apply_range(map1, map2);
3110 return isl_map_reverse(map1);
3117 struct isl_map *isl_map_apply_range(
3118 struct isl_map *map1, struct isl_map *map2)
3120 struct isl_dim *dim_result;
3121 struct isl_map *result;
3130 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3131 isl_dim_copy(map2->dim));
3133 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3136 for (i = 0; i < map1->n; ++i)
3137 for (j = 0; j < map2->n; ++j) {
3138 result = isl_map_add(result,
3139 isl_basic_map_apply_range(
3140 isl_basic_map_copy(map1->p[i]),
3141 isl_basic_map_copy(map2->p[j])));
3147 if (result && result->n <= 1)
3148 ISL_F_SET(result, ISL_MAP_DISJOINT);
3157 * returns range - domain
3159 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3161 struct isl_basic_set *bset;
3168 dim = isl_basic_map_n_in(bmap);
3169 nparam = isl_basic_map_n_param(bmap);
3170 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3171 bset = isl_basic_set_from_basic_map(bmap);
3172 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3173 bset = isl_basic_set_swap_vars(bset, 2*dim);
3174 for (i = 0; i < dim; ++i) {
3175 int j = isl_basic_map_alloc_equality(
3176 (struct isl_basic_map *)bset);
3179 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3180 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3181 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3182 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3184 return isl_basic_set_project_out(bset, 2*dim, 0);
3186 isl_basic_map_free(bmap);
3191 * returns range - domain
3193 struct isl_set *isl_map_deltas(struct isl_map *map)
3196 struct isl_set *result;
3201 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3202 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3203 isl_map_n_in(map), map->n, map->flags);
3206 for (i = 0; i < map->n; ++i)
3207 result = isl_set_add(result,
3208 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3216 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3218 struct isl_basic_map *bmap;
3226 nparam = dims->nparam;
3228 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3232 for (i = 0; i < dim; ++i) {
3233 int j = isl_basic_map_alloc_equality(bmap);
3236 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3237 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3238 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3240 return isl_basic_map_finalize(bmap);
3242 isl_basic_map_free(bmap);
3246 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3248 struct isl_dim *dim = isl_dim_map(set_dim);
3251 return basic_map_identity(dim);
3254 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3256 if (!model || !model->dim)
3258 isl_assert(model->ctx,
3259 model->dim->n_in == model->dim->n_out, return NULL);
3260 return basic_map_identity(isl_dim_copy(model->dim));
3263 static struct isl_map *map_identity(struct isl_dim *dim)
3265 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3266 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3269 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3271 struct isl_dim *dim = isl_dim_map(set_dim);
3274 return map_identity(dim);
3277 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3279 if (!model || !model->dim)
3281 isl_assert(model->ctx,
3282 model->dim->n_in == model->dim->n_out, return NULL);
3283 return map_identity(isl_dim_copy(model->dim));
3286 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3288 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3291 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3293 return isl_map_is_subset(
3294 (struct isl_map *)set1, (struct isl_map *)set2);
3297 int isl_basic_map_is_subset(
3298 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3301 struct isl_map *map1;
3302 struct isl_map *map2;
3304 if (!bmap1 || !bmap2)
3307 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3308 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3310 is_subset = isl_map_is_subset(map1, map2);
3318 int isl_basic_map_is_equal(
3319 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3323 if (!bmap1 || !bmap2)
3325 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3328 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3332 int isl_basic_set_is_equal(
3333 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3335 return isl_basic_map_is_equal(
3336 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3339 int isl_map_is_empty(struct isl_map *map)
3346 for (i = 0; i < map->n; ++i) {
3347 is_empty = isl_basic_map_is_empty(map->p[i]);
3356 int isl_map_fast_is_empty(struct isl_map *map)
3361 int isl_set_is_empty(struct isl_set *set)
3363 return isl_map_is_empty((struct isl_map *)set);
3366 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3370 struct isl_map *diff;
3375 if (isl_map_is_empty(map1))
3378 if (isl_map_is_empty(map2))
3381 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3385 is_subset = isl_map_is_empty(diff);
3391 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3397 is_subset = isl_map_is_subset(map1, map2);
3400 is_subset = isl_map_is_subset(map2, map1);
3404 int isl_basic_map_is_strict_subset(
3405 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3409 if (!bmap1 || !bmap2)
3411 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3414 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3415 if (is_subset == -1)
3420 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3424 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3427 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3429 struct isl_basic_set *bset = NULL;
3430 struct isl_vec *sample = NULL;
3437 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3440 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3441 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3442 copy = isl_basic_map_convex_hull(copy);
3443 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3444 isl_basic_map_free(copy);
3448 total = 1 + isl_basic_map_total_dim(bmap);
3449 if (bmap->sample && bmap->sample->size == total) {
3450 int contains = basic_map_contains(bmap, bmap->sample);
3456 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3459 sample = isl_basic_set_sample(bset);
3462 empty = sample->size == 0;
3464 isl_vec_free(bmap->ctx, bmap->sample);
3465 bmap->sample = sample;
3470 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3472 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3475 struct isl_map *isl_basic_map_union(
3476 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3478 struct isl_map *map;
3479 if (!bmap1 || !bmap2)
3482 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3484 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3487 map = isl_map_add(map, bmap1);
3488 map = isl_map_add(map, bmap2);
3491 isl_basic_map_free(bmap1);
3492 isl_basic_map_free(bmap2);
3496 struct isl_set *isl_basic_set_union(
3497 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3499 return (struct isl_set *)isl_basic_map_union(
3500 (struct isl_basic_map *)bset1,
3501 (struct isl_basic_map *)bset2);
3504 /* Order divs such that any div only depends on previous divs */
3505 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3508 unsigned off = isl_dim_total(bmap->dim);
3510 for (i = 0; i < bmap->n_div; ++i) {
3512 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3516 swap_div(bmap, i, pos);
3522 /* Look for a div in dst that corresponds to the div "div" in src.
3523 * The divs before "div" in src and dst are assumed to be the same.
3525 * Returns -1 if no corresponding div was found and the position
3526 * of the corresponding div in dst otherwise.
3528 static int find_div(struct isl_basic_map *dst,
3529 struct isl_basic_map *src, unsigned div)
3533 unsigned total = isl_dim_total(src->dim);
3535 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3536 for (i = div; i < dst->n_div; ++i)
3537 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3538 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3539 dst->n_div - div) == -1)
3544 struct isl_basic_map *isl_basic_map_align_divs(
3545 struct isl_basic_map *dst, struct isl_basic_map *src)
3548 unsigned total = isl_dim_total(src->dim);
3553 if (src->n_div == 0)
3556 for (i = 0; i < src->n_div; ++i)
3557 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3559 src = order_divs(src);
3560 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3561 src->n_div, 0, 2 * src->n_div);
3564 for (i = 0; i < src->n_div; ++i) {
3565 int j = find_div(dst, src, i);
3567 j = isl_basic_map_alloc_div(dst);
3570 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3571 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3572 if (add_div_constraints(dst, j) < 0)
3576 swap_div(dst, i, j);
3580 isl_basic_map_free(dst);
3584 struct isl_basic_set *isl_basic_set_align_divs(
3585 struct isl_basic_set *dst, struct isl_basic_set *src)
3587 return (struct isl_basic_set *)isl_basic_map_align_divs(
3588 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3591 struct isl_map *isl_map_align_divs(struct isl_map *map)
3595 map = isl_map_compute_divs(map);
3596 map = isl_map_cow(map);
3600 for (i = 1; i < map->n; ++i)
3601 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3602 for (i = 1; i < map->n; ++i)
3603 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3605 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3609 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3610 struct isl_basic_map *src, isl_int *c,
3611 unsigned len, int oppose)
3613 struct isl_basic_map *copy = NULL;
3618 copy = isl_basic_map_copy(src);
3619 copy = isl_basic_map_cow(copy);
3622 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3623 k = isl_basic_map_alloc_inequality(copy);
3627 isl_seq_neg(copy->ineq[k], c, len);
3629 isl_seq_cpy(copy->ineq[k], c, len);
3630 total = 1 + isl_basic_map_total_dim(copy);
3631 isl_seq_clr(copy->ineq[k]+len, total - len);
3632 isl_inequality_negate(copy, k);
3633 copy = isl_basic_map_simplify(copy);
3634 copy = isl_basic_map_finalize(copy);
3635 is_empty = isl_basic_map_is_empty(copy);
3639 dst = isl_map_add(dst, copy);
3641 isl_basic_map_free(copy);
3644 isl_basic_map_free(copy);
3649 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3653 struct isl_map *rest = NULL;
3655 unsigned total = isl_basic_map_total_dim(bmap);
3662 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3663 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3665 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3666 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3670 for (i = 0; i < map->n; ++i) {
3671 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3676 for (j = 0; j < map->n; ++j)
3677 map->p[j] = isl_basic_map_cow(map->p[j]);
3679 for (i = 0; i < bmap->n_eq; ++i) {
3680 for (j = 0; j < map->n; ++j) {
3681 rest = add_cut_constraint(rest,
3682 map->p[j], bmap->eq[i], 1+total, 0);
3686 rest = add_cut_constraint(rest,
3687 map->p[j], bmap->eq[i], 1+total, 1);
3691 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3695 k = isl_basic_map_alloc_equality(map->p[j]);
3698 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3699 isl_seq_clr(map->p[j]->eq[k]+1+total,
3700 map->p[j]->n_div - bmap->n_div);
3704 for (i = 0; i < bmap->n_ineq; ++i) {
3705 for (j = 0; j < map->n; ++j) {
3706 rest = add_cut_constraint(rest,
3707 map->p[j], bmap->ineq[i], 1+total, 0);
3711 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3715 k = isl_basic_map_alloc_inequality(map->p[j]);
3718 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3719 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3720 map->p[j]->n_div - bmap->n_div);
3732 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3738 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3740 if (isl_map_is_empty(map2)) {
3745 map1 = isl_map_compute_divs(map1);
3746 map2 = isl_map_compute_divs(map2);
3750 for (i = 0; map1 && i < map2->n; ++i)
3751 map1 = subtract(map1, map2->p[i]);
3761 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3763 return (struct isl_set *)
3765 (struct isl_map *)set1, (struct isl_map *)set2);
3768 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3772 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3773 map = isl_map_intersect_domain(map, set);
3774 set = isl_map_range(map);
3782 /* There is no need to cow as removing empty parts doesn't change
3783 * the meaning of the set.
3785 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3792 for (i = map->n-1; i >= 0; --i) {
3793 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3795 isl_basic_map_free(map->p[i]);
3796 if (i != map->n-1) {
3797 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3798 map->p[i] = map->p[map->n-1];
3806 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3808 return (struct isl_set *)
3809 isl_map_remove_empty_parts((struct isl_map *)set);
3812 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
3814 struct isl_basic_map *bmap;
3815 if (!map || map->n == 0)
3817 bmap = map->p[map->n-1];
3818 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
3819 return isl_basic_map_copy(bmap);
3822 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3824 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
3827 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
3828 struct isl_basic_map *bmap)
3834 for (i = map->n-1; i >= 0; --i) {
3835 if (map->p[i] != bmap)
3837 map = isl_map_cow(map);
3840 isl_basic_map_free(map->p[i]);
3841 if (i != map->n-1) {
3842 ISL_F_CLR(map, ISL_SET_NORMALIZED);
3843 map->p[i] = map->p[map->n-1];
3848 isl_basic_map_free(bmap);
3852 isl_basic_map_free(bmap);
3856 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3857 struct isl_basic_set *bset)
3859 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
3860 (struct isl_basic_map *)bset);
3863 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3864 * for any common value of the parameters and dimensions preceding dim
3865 * in both basic sets, the values of dimension pos in bset1 are
3866 * smaller or larger than those in bset2.
3869 * 1 if bset1 follows bset2
3870 * -1 if bset1 precedes bset2
3871 * 0 if bset1 and bset2 are incomparable
3872 * -2 if some error occurred.
3874 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3875 struct isl_basic_set *bset2, int pos)
3877 struct isl_dim *dims;
3878 struct isl_basic_map *bmap1 = NULL;
3879 struct isl_basic_map *bmap2 = NULL;
3880 struct isl_ctx *ctx;
3881 struct isl_vec *obj;
3884 unsigned dim1, dim2;
3886 enum isl_lp_result res;
3889 if (!bset1 || !bset2)
3892 nparam = isl_basic_set_n_param(bset1);
3893 dim1 = isl_basic_set_n_dim(bset1);
3894 dim2 = isl_basic_set_n_dim(bset2);
3895 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3896 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3897 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3898 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3899 if (!bmap1 || !bmap2)
3901 bmap1 = isl_basic_map_extend(bmap1, nparam,
3902 pos, (dim1 - pos) + (dim2 - pos),
3903 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3904 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
3907 total = isl_basic_map_total_dim(bmap1);
3909 obj = isl_vec_alloc(ctx, 1 + total);
3910 isl_seq_clr(obj->block.data, 1 + total);
3911 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
3912 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
3917 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3918 if (res == isl_lp_empty)
3920 else if (res == isl_lp_ok && isl_int_is_pos(num))
3922 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3923 res == isl_lp_unbounded)
3929 isl_basic_map_free(bmap1);
3930 isl_vec_free(ctx, obj);
3933 isl_basic_map_free(bmap1);
3934 isl_basic_map_free(bmap2);
3938 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
3939 unsigned pos, isl_int *val)
3947 total = isl_basic_map_total_dim(bmap);
3948 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
3949 for (; d+1 > pos; --d)
3950 if (!isl_int_is_zero(bmap->eq[i][1+d]))
3954 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
3956 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
3958 if (!isl_int_is_one(bmap->eq[i][1+d]))
3961 isl_int_neg(*val, bmap->eq[i][0]);
3967 static int isl_map_fast_has_fixed_var(struct isl_map *map,
3968 unsigned pos, isl_int *val)
3980 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
3983 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
3984 for (i = 1; fixed == 1 && i < map->n; ++i) {
3985 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
3986 if (fixed == 1 && isl_int_ne(tmp, v))
3990 isl_int_set(*val, v);
3996 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
3999 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4002 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4003 enum isl_dim_type type, unsigned pos, isl_int *val)
4005 if (pos >= isl_basic_map_dim(bmap, type))
4007 return isl_basic_map_fast_has_fixed_var(bmap,
4008 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4011 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4012 * then return this fixed value in *val.
4014 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4016 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4019 /* Check if input variable in has fixed value and if so and if val is not NULL,
4020 * then return this fixed value in *val.
4022 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4024 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4027 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4028 * and if val is not NULL, then return this lower bound in *val.
4030 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4031 unsigned dim, isl_int *val)
4033 int i, i_eq = -1, i_ineq = -1;
4040 total = isl_basic_set_total_dim(bset);
4041 nparam = isl_basic_set_n_param(bset);
4042 for (i = 0; i < bset->n_eq; ++i) {
4043 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4049 for (i = 0; i < bset->n_ineq; ++i) {
4050 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4052 if (i_eq != -1 || i_ineq != -1)
4056 if (i_eq == -1 && i_ineq == -1)
4058 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4059 /* The coefficient should always be one due to normalization. */
4060 if (!isl_int_is_one(c[1+nparam+dim]))
4062 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4064 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4065 total - nparam - dim - 1) != -1)
4068 isl_int_neg(*val, c[0]);
4072 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4073 unsigned dim, isl_int *val)
4085 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4089 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4091 for (i = 1; fixed == 1 && i < set->n; ++i) {
4092 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4094 if (fixed == 1 && isl_int_ne(tmp, v))
4098 isl_int_set(*val, v);
4109 static int qsort_constraint_cmp(const void *p1, const void *p2)
4111 const struct constraint *c1 = (const struct constraint *)p1;
4112 const struct constraint *c2 = (const struct constraint *)p2;
4113 unsigned size = isl_min(c1->size, c2->size);
4114 return isl_seq_cmp(c1->c, c2->c, size);
4117 static struct isl_basic_map *isl_basic_map_sort_constraints(
4118 struct isl_basic_map *bmap)
4121 struct constraint *c;
4126 total = isl_basic_map_total_dim(bmap);
4127 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4130 for (i = 0; i < bmap->n_ineq; ++i) {
4132 c[i].c = bmap->ineq[i];
4134 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4135 for (i = 0; i < bmap->n_ineq; ++i)
4136 bmap->ineq[i] = c[i].c;
4140 isl_basic_map_free(bmap);
4144 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4148 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4150 bmap = isl_basic_map_convex_hull(bmap);
4151 bmap = isl_basic_map_sort_constraints(bmap);
4152 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4156 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4158 return (struct isl_basic_set *)isl_basic_map_normalize(
4159 (struct isl_basic_map *)bset);
4162 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4163 const struct isl_basic_map *bmap2)
4170 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4171 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4172 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4173 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4174 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4175 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4176 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4177 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4179 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4181 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4183 if (bmap1->n_eq != bmap2->n_eq)
4184 return bmap1->n_eq - bmap2->n_eq;
4185 if (bmap1->n_ineq != bmap2->n_ineq)
4186 return bmap1->n_ineq - bmap2->n_ineq;
4187 if (bmap1->n_div != bmap2->n_div)
4188 return bmap1->n_div - bmap2->n_div;
4189 total = isl_basic_map_total_dim(bmap1);
4190 for (i = 0; i < bmap1->n_eq; ++i) {
4191 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4195 for (i = 0; i < bmap1->n_ineq; ++i) {
4196 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4200 for (i = 0; i < bmap1->n_div; ++i) {
4201 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4208 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4209 struct isl_basic_map *bmap2)
4211 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4214 static int qsort_bmap_cmp(const void *p1, const void *p2)
4216 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4217 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4219 return isl_basic_map_fast_cmp(bmap1, bmap2);
4222 /* We normalize in place, but if anything goes wrong we need
4223 * to return NULL, so we need to make sure we don't change the
4224 * meaning of any possible other copies of map.
4226 struct isl_map *isl_map_normalize(struct isl_map *map)
4229 struct isl_basic_map *bmap;
4233 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4235 for (i = 0; i < map->n; ++i) {
4236 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4239 isl_basic_map_free(map->p[i]);
4242 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4243 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4244 map = isl_map_remove_empty_parts(map);
4247 for (i = map->n - 1; i >= 1; --i) {
4248 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4250 isl_basic_map_free(map->p[i-1]);
4251 for (j = i; j < map->n; ++j)
4252 map->p[j-1] = map->p[j];
4262 struct isl_set *isl_set_normalize(struct isl_set *set)
4264 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4267 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4277 if (!isl_dim_equal(map1->dim, map2->dim))
4280 map1 = isl_map_copy(map1);
4281 map2 = isl_map_copy(map2);
4282 map1 = isl_map_normalize(map1);
4283 map2 = isl_map_normalize(map2);
4286 equal = map1->n == map2->n;
4287 for (i = 0; equal && i < map1->n; ++i) {
4288 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4301 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4303 return isl_map_fast_is_equal((struct isl_map *)set1,
4304 (struct isl_map *)set2);
4307 /* Return an interval that ranges from min to max (inclusive)
4309 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4310 isl_int min, isl_int max)
4313 struct isl_basic_set *bset = NULL;
4315 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4319 k = isl_basic_set_alloc_inequality(bset);
4322 isl_int_set_si(bset->ineq[k][1], 1);
4323 isl_int_neg(bset->ineq[k][0], min);
4325 k = isl_basic_set_alloc_inequality(bset);
4328 isl_int_set_si(bset->ineq[k][1], -1);
4329 isl_int_set(bset->ineq[k][0], max);
4333 isl_basic_set_free(bset);
4337 /* Return the Cartesian product of the basic sets in list (in the given order).
4339 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4347 struct isl_basic_set *product = NULL;
4351 isl_assert(list->ctx, list->n > 0, goto error);
4352 isl_assert(list->ctx, list->p[0], goto error);
4353 nparam = isl_basic_set_n_param(list->p[0]);
4354 dim = isl_basic_set_n_dim(list->p[0]);
4355 extra = list->p[0]->n_div;
4356 n_eq = list->p[0]->n_eq;
4357 n_ineq = list->p[0]->n_ineq;
4358 for (i = 1; i < list->n; ++i) {
4359 isl_assert(list->ctx, list->p[i], goto error);
4360 isl_assert(list->ctx,
4361 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4362 dim += isl_basic_set_n_dim(list->p[i]);
4363 extra += list->p[i]->n_div;
4364 n_eq += list->p[i]->n_eq;
4365 n_ineq += list->p[i]->n_ineq;
4367 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4372 for (i = 0; i < list->n; ++i) {
4373 isl_basic_set_add_constraints(product,
4374 isl_basic_set_copy(list->p[i]), dim);
4375 dim += isl_basic_set_n_dim(list->p[i]);
4377 isl_basic_set_list_free(list);
4380 isl_basic_set_free(product);
4381 isl_basic_set_list_free(list);
4385 struct isl_basic_map *isl_basic_map_product(
4386 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4388 struct isl_dim *dim_result = NULL;
4389 struct isl_basic_map *bmap;
4390 unsigned in1, in2, out1, out2, nparam, total, pos;
4391 struct isl_dim_map *dim_map1, *dim_map2;
4393 if (!bmap1 || !bmap2)
4396 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4397 bmap2->dim, isl_dim_param), goto error);
4398 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4399 isl_dim_copy(bmap2->dim));
4401 in1 = isl_basic_map_n_in(bmap1);
4402 in2 = isl_basic_map_n_in(bmap2);
4403 out1 = isl_basic_map_n_out(bmap1);
4404 out2 = isl_basic_map_n_out(bmap2);
4405 nparam = isl_basic_map_n_param(bmap1);
4407 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4408 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4409 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4410 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4411 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4412 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4413 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4414 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4415 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4416 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4417 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4419 bmap = isl_basic_map_alloc_dim(dim_result,
4420 bmap1->n_div + bmap2->n_div,
4421 bmap1->n_eq + bmap2->n_eq,
4422 bmap1->n_ineq + bmap2->n_ineq);
4423 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4424 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4425 bmap = isl_basic_map_simplify(bmap);
4426 return isl_basic_map_finalize(bmap);
4428 isl_basic_map_free(bmap1);
4429 isl_basic_map_free(bmap2);
4433 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4435 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4438 struct isl_map *result;
4444 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4445 map2->dim, isl_dim_param), goto error);
4447 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4448 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4449 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4451 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4452 isl_dim_copy(map2->dim)),
4453 map1->n * map2->n, flags);
4456 for (i = 0; i < map1->n; ++i)
4457 for (j = 0; j < map2->n; ++j) {
4458 struct isl_basic_map *part;
4459 part = isl_basic_map_product(
4460 isl_basic_map_copy(map1->p[i]),
4461 isl_basic_map_copy(map2->p[j]));
4462 if (isl_basic_map_is_empty(part))
4463 isl_basic_map_free(part);
4465 result = isl_map_add(result, part);
4478 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4486 bset = isl_basic_set_copy(bset);
4487 bset = isl_basic_set_normalize(bset);
4490 total = isl_basic_set_total_dim(bset);
4491 isl_hash_byte(hash, bset->n_eq & 0xFF);
4492 for (i = 0; i < bset->n_eq; ++i) {
4494 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4495 isl_hash_hash(hash, c_hash);
4497 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4498 for (i = 0; i < bset->n_ineq; ++i) {
4500 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4501 isl_hash_hash(hash, c_hash);
4503 isl_hash_byte(hash, bset->n_div & 0xFF);
4504 for (i = 0; i < bset->n_div; ++i) {
4506 if (isl_int_is_zero(bset->div[i][0]))
4508 isl_hash_byte(hash, i & 0xFF);
4509 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4510 isl_hash_hash(hash, c_hash);
4512 isl_basic_set_free(bset);
4516 uint32_t isl_set_get_hash(struct isl_set *set)
4523 set = isl_set_copy(set);
4524 set = isl_set_normalize(set);
4528 hash = isl_hash_init();
4529 for (i = 0; i < set->n; ++i) {
4531 bset_hash = isl_basic_set_get_hash(set->p[i]);
4532 isl_hash_hash(hash, bset_hash);
4540 /* Check if the value for dimension dim is completely determined
4541 * by the values of the other parameters and variables.
4542 * That is, check if dimension dim is involved in an equality.
4544 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4551 nparam = isl_basic_set_n_param(bset);
4552 for (i = 0; i < bset->n_eq; ++i)
4553 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4558 /* Check if the value for dimension dim is completely determined
4559 * by the values of the other parameters and variables.
4560 * That is, check if dimension dim is involved in an equality
4561 * for each of the subsets.
4563 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4569 for (i = 0; i < set->n; ++i) {
4571 unique = isl_basic_set_dim_is_unique(set->p[i], dim);