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_basic_map_offset(struct isl_basic_map *bmap,
111 enum isl_dim_type type)
113 struct isl_dim *dim = bmap->dim;
115 case isl_dim_param: return 1;
116 case isl_dim_in: return 1 + dim->nparam;
117 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
118 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
122 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
124 return pos(map->dim, type);
127 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
128 enum isl_dim_type type)
130 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
133 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
135 return bset->dim->n_out;
138 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
140 return bset->dim->nparam;
143 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
145 return isl_dim_total(bset->dim) + bset->n_div;
148 unsigned isl_set_n_dim(const struct isl_set *set)
150 return set->dim->n_out;
153 unsigned isl_set_n_param(const struct isl_set *set)
155 return set->dim->nparam;
158 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
160 return bmap->dim->n_in;
163 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
165 return bmap->dim->n_out;
168 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
170 return bmap->dim->nparam;
173 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
178 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
180 return isl_dim_total(bmap->dim) + bmap->n_div;
183 unsigned isl_map_n_in(const struct isl_map *map)
185 return map->dim->n_in;
188 unsigned isl_map_n_out(const struct isl_map *map)
190 return map->dim->n_out;
193 unsigned isl_map_n_param(const struct isl_map *map)
195 return map->dim->nparam;
198 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
200 return map->dim->n_in == set->dim->n_out &&
201 map->dim->nparam == set->dim->nparam;
204 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
205 struct isl_basic_set *bset)
207 return bmap->dim->n_in == bset->dim->n_out &&
208 bmap->dim->nparam == bset->dim->nparam;
211 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
212 struct isl_basic_set *bset)
214 return bmap->dim->n_out == bset->dim->n_out &&
215 bmap->dim->nparam == bset->dim->nparam;
218 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
219 struct isl_basic_map *bmap, unsigned extra,
220 unsigned n_eq, unsigned n_ineq)
223 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
225 bmap->block = isl_blk_alloc(ctx, (n_eq + n_ineq) * row_size);
226 if (isl_blk_is_error(bmap->block)) {
231 bmap->eq = isl_alloc_array(ctx, isl_int *, n_eq + n_ineq);
233 isl_blk_free(ctx, bmap->block);
239 bmap->block2 = isl_blk_empty();
242 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
243 if (isl_blk_is_error(bmap->block2)) {
245 isl_blk_free(ctx, bmap->block);
250 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
252 isl_blk_free(ctx, bmap->block2);
254 isl_blk_free(ctx, bmap->block);
260 for (i = 0; i < n_eq + n_ineq; ++i)
261 bmap->eq[i] = bmap->block.data + i * row_size;
263 for (i = 0; i < extra; ++i)
264 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
270 bmap->c_size = n_eq + n_ineq;
271 bmap->ineq = bmap->eq + n_eq;
280 isl_basic_map_free(bmap);
284 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
285 unsigned nparam, unsigned dim, unsigned extra,
286 unsigned n_eq, unsigned n_ineq)
288 struct isl_basic_map *bmap;
289 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
290 return (struct isl_basic_set *)bmap;
293 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
294 unsigned extra, unsigned n_eq, unsigned n_ineq)
296 struct isl_basic_map *bmap;
299 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
300 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
301 return (struct isl_basic_set *)bmap;
304 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
305 unsigned extra, unsigned n_eq, unsigned n_ineq)
307 struct isl_basic_map *bmap;
311 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
316 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
322 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
323 unsigned nparam, unsigned in, unsigned out, unsigned extra,
324 unsigned n_eq, unsigned n_ineq)
326 struct isl_basic_map *bmap;
329 dim = isl_dim_alloc(ctx, nparam, in, out);
333 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
337 static void dup_constraints(
338 struct isl_basic_map *dst, struct isl_basic_map *src)
341 unsigned total = isl_basic_map_total_dim(src);
343 for (i = 0; i < src->n_eq; ++i) {
344 int j = isl_basic_map_alloc_equality(dst);
345 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
348 for (i = 0; i < src->n_ineq; ++i) {
349 int j = isl_basic_map_alloc_inequality(dst);
350 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
353 for (i = 0; i < src->n_div; ++i) {
354 int j = isl_basic_map_alloc_div(dst);
355 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
357 F_SET(dst, ISL_BASIC_SET_FINAL);
360 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
362 struct isl_basic_map *dup;
366 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
367 bmap->n_div, bmap->n_eq, bmap->n_ineq);
370 dup->flags = bmap->flags;
371 dup_constraints(dup, bmap);
372 dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
376 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
378 struct isl_basic_map *dup;
380 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
381 return (struct isl_basic_set *)dup;
384 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
389 if (F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
393 return isl_basic_set_dup(bset);
396 struct isl_set *isl_set_copy(struct isl_set *set)
405 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
410 if (F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
414 return isl_basic_map_dup(bmap);
417 struct isl_map *isl_map_copy(struct isl_map *map)
426 void isl_basic_map_free(struct isl_basic_map *bmap)
434 isl_ctx_deref(bmap->ctx);
436 isl_blk_free(bmap->ctx, bmap->block2);
438 isl_blk_free(bmap->ctx, bmap->block);
439 isl_vec_free(bmap->ctx, bmap->sample);
440 isl_dim_free(bmap->dim);
444 void isl_basic_set_free(struct isl_basic_set *bset)
446 isl_basic_map_free((struct isl_basic_map *)bset);
449 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
455 isl_assert(ctx, bmap->n_eq + bmap->n_ineq < bmap->c_size, return -1);
456 isl_assert(ctx, bmap->eq + bmap->n_eq <= bmap->ineq, return -1);
457 if (bmap->eq + bmap->n_eq == bmap->ineq) {
459 int j = isl_basic_map_alloc_inequality(bmap);
463 bmap->ineq[0] = bmap->ineq[j];
468 isl_seq_clr(bmap->eq[bmap->n_eq] +
469 1 + isl_basic_map_total_dim(bmap),
470 bmap->extra - bmap->n_div);
471 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
475 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
477 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
480 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
484 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
489 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
494 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
496 if (pos != bmap->n_eq - 1) {
498 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
499 bmap->eq[bmap->n_eq - 1] = t;
505 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
507 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
510 void isl_basic_map_inequality_to_equality(
511 struct isl_basic_map *bmap, unsigned pos)
516 bmap->ineq[pos] = bmap->ineq[0];
517 bmap->ineq[0] = bmap->eq[bmap->n_eq];
518 bmap->eq[bmap->n_eq] = t;
522 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
523 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
524 F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
527 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
533 isl_assert(ctx, (bmap->ineq - bmap->eq) + bmap->n_ineq < bmap->c_size,
535 F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
536 F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
537 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
538 F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
539 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
540 1 + isl_basic_map_total_dim(bmap),
541 bmap->extra - bmap->n_div);
542 return bmap->n_ineq++;
545 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
547 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
550 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
554 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
559 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
561 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
564 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
569 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
571 if (pos != bmap->n_ineq - 1) {
573 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
574 bmap->ineq[bmap->n_ineq - 1] = t;
575 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
581 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
583 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
586 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
590 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
591 isl_seq_clr(bmap->div[bmap->n_div] +
592 1 + 1 + isl_basic_map_total_dim(bmap),
593 bmap->extra - bmap->n_div);
594 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
595 return bmap->n_div++;
598 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
602 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
607 /* Copy constraint from src to dst, putting the vars of src at offset
608 * dim_off in dst and the divs of src at offset div_off in dst.
609 * If both sets are actually map, then dim_off applies to the input
612 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
613 struct isl_basic_map *src_map, isl_int *src,
614 unsigned in_off, unsigned out_off, unsigned div_off)
616 unsigned src_nparam = isl_basic_map_n_param(src_map);
617 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
618 unsigned src_in = isl_basic_map_n_in(src_map);
619 unsigned dst_in = isl_basic_map_n_in(dst_map);
620 unsigned src_out = isl_basic_map_n_out(src_map);
621 unsigned dst_out = isl_basic_map_n_out(dst_map);
622 isl_int_set(dst[0], src[0]);
623 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
624 if (dst_nparam > src_nparam)
625 isl_seq_clr(dst+1+src_nparam,
626 dst_nparam - src_nparam);
627 isl_seq_clr(dst+1+dst_nparam, in_off);
628 isl_seq_cpy(dst+1+dst_nparam+in_off,
630 isl_min(dst_in-in_off, src_in));
631 if (dst_in-in_off > src_in)
632 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
633 dst_in - in_off - src_in);
634 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
635 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
636 src+1+src_nparam+src_in,
637 isl_min(dst_out-out_off, src_out));
638 if (dst_out-out_off > src_out)
639 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
640 dst_out - out_off - src_out);
641 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
642 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
643 src+1+src_nparam+src_in+src_out,
644 isl_min(dst_map->extra-div_off, src_map->n_div));
645 if (dst_map->n_div-div_off > src_map->n_div)
646 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
647 div_off+src_map->n_div,
648 dst_map->n_div - div_off - src_map->n_div);
651 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
652 struct isl_basic_map *src_map, isl_int *src,
653 unsigned in_off, unsigned out_off, unsigned div_off)
655 isl_int_set(dst[0], src[0]);
656 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
659 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
660 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
665 if (!bmap1 || !bmap2)
668 div_off = bmap1->n_div;
670 for (i = 0; i < bmap2->n_eq; ++i) {
671 int i1 = isl_basic_map_alloc_equality(bmap1);
674 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
675 i_pos, o_pos, div_off);
678 for (i = 0; i < bmap2->n_ineq; ++i) {
679 int i1 = isl_basic_map_alloc_inequality(bmap1);
682 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
683 i_pos, o_pos, div_off);
686 for (i = 0; i < bmap2->n_div; ++i) {
687 int i1 = isl_basic_map_alloc_div(bmap1);
690 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
691 i_pos, o_pos, div_off);
694 isl_basic_map_free(bmap2);
699 isl_basic_map_free(bmap1);
700 isl_basic_map_free(bmap2);
704 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
705 struct isl_dim_map *dim_map)
709 for (i = 0; i < dim_map->len; ++i) {
710 if (dim_map->pos[i] < 0)
711 isl_int_set_si(dst[i], 0);
713 isl_int_set(dst[i], src[dim_map->pos[i]]);
717 static void copy_div_dim_map(isl_int *dst, isl_int *src,
718 struct isl_dim_map *dim_map)
720 isl_int_set(dst[0], src[0]);
721 copy_constraint_dim_map(dst+1, src+1, dim_map);
724 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
725 struct isl_basic_map *src, struct isl_dim_map *dim_map)
729 if (!src || !dst || !dim_map)
732 for (i = 0; i < src->n_eq; ++i) {
733 int i1 = isl_basic_map_alloc_equality(dst);
736 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
739 for (i = 0; i < src->n_ineq; ++i) {
740 int i1 = isl_basic_map_alloc_inequality(dst);
743 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
746 for (i = 0; i < src->n_div; ++i) {
747 int i1 = isl_basic_map_alloc_div(dst);
750 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
754 isl_basic_map_free(src);
759 isl_basic_map_free(src);
760 isl_basic_map_free(dst);
764 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
765 struct isl_basic_set *bset2, unsigned pos)
767 return (struct isl_basic_set *)
768 add_constraints((struct isl_basic_map *)bset1,
769 (struct isl_basic_map *)bset2, 0, pos);
772 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
773 struct isl_dim *dim, unsigned extra,
774 unsigned n_eq, unsigned n_ineq)
776 struct isl_basic_map *ext;
783 base = isl_basic_map_cow(base);
787 dims_ok = isl_dim_equal(base->dim, dim) &&
788 base->extra >= base->n_div + extra;
790 if (dims_ok && n_eq == 0 && n_ineq == 0) {
795 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
796 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
797 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
798 extra += base->extra;
800 n_ineq += base->n_ineq;
802 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
808 ext = add_constraints(ext, base, 0, 0);
811 F_CLR(ext, ISL_BASIC_SET_FINAL);
818 isl_basic_map_free(base);
822 struct isl_basic_map *isl_basic_map_extend_constraints(
823 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
827 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
831 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
832 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
833 unsigned n_eq, unsigned n_ineq)
835 struct isl_basic_map *bmap;
840 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
844 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
848 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
849 unsigned nparam, unsigned dim, unsigned extra,
850 unsigned n_eq, unsigned n_ineq)
852 return (struct isl_basic_set *)
853 isl_basic_map_extend((struct isl_basic_map *)base,
854 nparam, 0, dim, extra, n_eq, n_ineq);
857 struct isl_basic_set *isl_basic_set_extend_constraints(
858 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
860 return (struct isl_basic_set *)
861 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
865 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
867 return (struct isl_basic_set *)
868 isl_basic_map_cow((struct isl_basic_map *)bset);
871 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
878 bmap = isl_basic_map_dup(bmap);
880 F_CLR(bmap, ISL_BASIC_SET_FINAL);
884 struct isl_set *isl_set_cow(struct isl_set *set)
892 return isl_set_dup(set);
895 struct isl_map *isl_map_cow(struct isl_map *map)
903 return isl_map_dup(map);
906 static void swap_vars(struct isl_blk blk, isl_int *a,
907 unsigned a_len, unsigned b_len)
909 isl_seq_cpy(blk.data, a+a_len, b_len);
910 isl_seq_cpy(blk.data+b_len, a, a_len);
911 isl_seq_cpy(a, blk.data, b_len+a_len);
914 struct isl_basic_set *isl_basic_set_swap_vars(
915 struct isl_basic_set *bset, unsigned n)
925 nparam = isl_basic_set_n_param(bset);
926 dim = isl_basic_set_n_dim(bset);
927 isl_assert(bset->ctx, n <= dim, goto error);
932 bset = isl_basic_set_cow(bset);
936 blk = isl_blk_alloc(bset->ctx, dim);
937 if (isl_blk_is_error(blk))
940 for (i = 0; i < bset->n_eq; ++i)
942 bset->eq[i]+1+nparam, n, dim - n);
944 for (i = 0; i < bset->n_ineq; ++i)
946 bset->ineq[i]+1+nparam, n, dim - n);
948 for (i = 0; i < bset->n_div; ++i)
950 bset->div[i]+1+1+nparam, n, dim - n);
952 isl_blk_free(bset->ctx, blk);
954 F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
958 isl_basic_set_free(bset);
962 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
965 set = isl_set_cow(set);
969 for (i = 0; i < set->n; ++i) {
970 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
976 F_CLR(set, ISL_SET_NORMALIZED);
980 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
986 total = isl_basic_map_total_dim(bmap);
987 isl_basic_map_free_div(bmap, bmap->n_div);
988 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
990 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
992 isl_basic_map_alloc_equality(bmap);
996 isl_int_set_si(bmap->eq[i][0], 1);
997 isl_seq_clr(bmap->eq[i]+1, total);
998 F_SET(bmap, ISL_BASIC_MAP_EMPTY);
999 return isl_basic_map_finalize(bmap);
1001 isl_basic_map_free(bmap);
1005 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1007 return (struct isl_basic_set *)
1008 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1011 static void swap_div(struct isl_basic_map *bmap, int a, int b)
1014 unsigned off = isl_dim_total(bmap->dim);
1015 isl_int *t = bmap->div[a];
1016 bmap->div[a] = bmap->div[b];
1019 for (i = 0; i < bmap->n_eq; ++i)
1020 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1022 for (i = 0; i < bmap->n_ineq; ++i)
1023 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1025 for (i = 0; i < bmap->n_div; ++i)
1026 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1027 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1030 /* Eliminate the specified n dimensions starting at first from the
1031 * constraints using Fourier-Motzkin, The dimensions themselves
1034 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1035 unsigned first, unsigned n)
1045 set = isl_set_cow(set);
1048 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1049 nparam = isl_set_n_param(set);
1051 for (i = 0; i < set->n; ++i) {
1052 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1063 /* Project out n dimensions starting at first using Fourier-Motzkin */
1064 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1065 unsigned first, unsigned n)
1067 set = isl_set_eliminate_dims(set, first, n);
1068 set = isl_set_drop_dims(set, first, n);
1072 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1074 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1082 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1091 set = isl_set_cow(set);
1095 for (i = 0; i < set->n; ++i) {
1096 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1106 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1107 enum isl_dim_type type, unsigned first, unsigned n)
1111 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1115 bmap = isl_basic_map_eliminate_vars(bmap,
1116 isl_basic_map_offset(bmap, type) - 1 + first, n);
1117 bmap = isl_basic_map_drop(bmap, type, first, n);
1120 isl_basic_map_free(bmap);
1124 struct isl_map *isl_map_remove(struct isl_map *map,
1125 enum isl_dim_type type, unsigned first, unsigned n)
1133 map = isl_map_cow(map);
1136 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1138 for (i = 0; i < map->n; ++i) {
1139 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1140 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1144 map = isl_map_drop(map, type, first, n);
1151 /* Project out n inputs starting at first using Fourier-Motzkin */
1152 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1153 unsigned first, unsigned n)
1155 return isl_map_remove(map, isl_dim_in, first, n);
1158 /* Project out n dimensions starting at first using Fourier-Motzkin */
1159 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1160 unsigned first, unsigned n)
1162 unsigned nparam = isl_basic_set_n_param(bset);
1163 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1164 bset = isl_basic_set_drop_dims(bset, first, n);
1168 static void dump_term(struct isl_basic_map *bmap,
1169 isl_int c, int pos, FILE *out)
1172 unsigned in = isl_basic_map_n_in(bmap);
1173 unsigned dim = in + isl_basic_map_n_out(bmap);
1174 unsigned nparam = isl_basic_map_n_param(bmap);
1176 isl_int_print(out, c, 0);
1178 if (!isl_int_is_one(c))
1179 isl_int_print(out, c, 0);
1180 if (pos < 1 + nparam) {
1181 name = isl_dim_get_name(bmap->dim,
1182 isl_dim_param, pos - 1);
1184 fprintf(out, "%s", name);
1186 fprintf(out, "p%d", pos - 1);
1187 } else if (pos < 1 + nparam + in)
1188 fprintf(out, "i%d", pos - 1 - nparam);
1189 else if (pos < 1 + nparam + dim)
1190 fprintf(out, "o%d", pos - 1 - nparam - in);
1192 fprintf(out, "e%d", pos - 1 - nparam - dim);
1196 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1197 int sign, FILE *out)
1201 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1205 for (i = 0, first = 1; i < len; ++i) {
1206 if (isl_int_sgn(c[i]) * sign <= 0)
1209 fprintf(out, " + ");
1211 isl_int_abs(v, c[i]);
1212 dump_term(bmap, v, i, out);
1219 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1220 const char *op, FILE *out, int indent)
1224 fprintf(out, "%*s", indent, "");
1226 dump_constraint_sign(bmap, c, 1, out);
1227 fprintf(out, " %s ", op);
1228 dump_constraint_sign(bmap, c, -1, out);
1232 for (i = bmap->n_div; i < bmap->extra; ++i) {
1233 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1235 fprintf(out, "%*s", indent, "");
1236 fprintf(out, "ERROR: unused div coefficient not zero\n");
1241 static void dump_constraints(struct isl_basic_map *bmap,
1242 isl_int **c, unsigned n,
1243 const char *op, FILE *out, int indent)
1247 for (i = 0; i < n; ++i)
1248 dump_constraint(bmap, c[i], op, out, indent);
1251 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1255 unsigned total = isl_basic_map_total_dim(bmap);
1257 for (j = 0; j < 1 + total; ++j) {
1258 if (isl_int_is_zero(exp[j]))
1260 if (!first && isl_int_is_pos(exp[j]))
1262 dump_term(bmap, exp[j], j, out);
1267 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1271 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1272 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1274 for (i = 0; i < bmap->n_div; ++i) {
1275 fprintf(out, "%*s", indent, "");
1276 fprintf(out, "e%d = [(", i);
1277 dump_affine(bmap, bmap->div[i]+1, out);
1279 isl_int_print(out, bmap->div[i][0], 0);
1280 fprintf(out, "]\n");
1284 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1287 fprintf(out, "null basic set\n");
1291 fprintf(out, "%*s", indent, "");
1292 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1293 bset->ref, bset->dim->nparam, bset->dim->n_out,
1294 bset->extra, bset->flags);
1295 dump((struct isl_basic_map *)bset, out, indent);
1298 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1301 fprintf(out, "null basic map\n");
1305 fprintf(out, "%*s", indent, "");
1306 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1307 "flags: %x, n_name: %d\n",
1309 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1310 bmap->extra, bmap->flags, bmap->dim->n_name);
1311 dump(bmap, out, indent);
1314 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1319 total = isl_basic_map_total_dim(bmap);
1320 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1321 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1322 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1323 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1327 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1329 struct isl_set *set;
1333 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1334 isl_assert(dim->ctx, n >= 0, return NULL);
1335 set = isl_alloc(dim->ctx, struct isl_set,
1336 sizeof(struct isl_set) +
1337 n * sizeof(struct isl_basic_set *));
1341 set->ctx = dim->ctx;
1342 isl_ctx_ref(set->ctx);
1354 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1355 unsigned nparam, unsigned dim, int n, unsigned flags)
1357 struct isl_set *set;
1358 struct isl_dim *dims;
1360 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1364 set = isl_set_alloc_dim(dims, n, flags);
1368 struct isl_set *isl_set_dup(struct isl_set *set)
1371 struct isl_set *dup;
1376 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1379 for (i = 0; i < set->n; ++i)
1380 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1384 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1386 struct isl_set *set;
1391 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1393 isl_basic_set_free(bset);
1396 return isl_set_add(set, bset);
1399 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1401 struct isl_map *map;
1406 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1408 isl_basic_map_free(bmap);
1411 return isl_map_add(map, bmap);
1414 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1418 isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1419 isl_assert(set->ctx, set->n < set->size, goto error);
1420 set->p[set->n] = bset;
1427 isl_basic_set_free(bset);
1431 void isl_set_free(struct isl_set *set)
1441 isl_ctx_deref(set->ctx);
1442 for (i = 0; i < set->n; ++i)
1443 isl_basic_set_free(set->p[i]);
1444 isl_dim_free(set->dim);
1448 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1453 fprintf(out, "null set\n");
1457 fprintf(out, "%*s", indent, "");
1458 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1459 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1461 for (i = 0; i < set->n; ++i) {
1462 fprintf(out, "%*s", indent, "");
1463 fprintf(out, "basic set %d:\n", i);
1464 isl_basic_set_dump(set->p[i], out, indent+4);
1468 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1473 fprintf(out, "null map\n");
1477 fprintf(out, "%*s", indent, "");
1478 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1479 "flags: %x, n_name: %d\n",
1480 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1481 map->dim->n_out, map->flags, map->dim->n_name);
1482 for (i = 0; i < map->n; ++i) {
1483 fprintf(out, "%*s", indent, "");
1484 fprintf(out, "basic map %d:\n", i);
1485 isl_basic_map_dump(map->p[i], out, indent+4);
1489 struct isl_basic_map *isl_basic_map_intersect_domain(
1490 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1492 struct isl_basic_map *bmap_domain;
1493 struct isl_dim *dim;
1498 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1499 bset->dim, isl_dim_param), goto error);
1501 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1502 isl_assert(bset->ctx,
1503 isl_basic_map_compatible_domain(bmap, bset), goto error);
1505 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1506 bset->n_div, bset->n_eq, bset->n_ineq);
1509 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1510 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1511 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1513 bmap = isl_basic_map_simplify(bmap);
1514 return isl_basic_map_finalize(bmap);
1516 isl_basic_map_free(bmap);
1517 isl_basic_set_free(bset);
1521 struct isl_basic_map *isl_basic_map_intersect_range(
1522 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1524 struct isl_basic_map *bmap_range;
1529 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1530 bset->dim, isl_dim_param), goto error);
1532 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1533 isl_assert(bset->ctx,
1534 isl_basic_map_compatible_range(bmap, bset), goto error);
1536 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1537 bset->n_div, bset->n_eq, bset->n_ineq);
1540 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1541 bmap = add_constraints(bmap, bmap_range, 0, 0);
1543 bmap = isl_basic_map_simplify(bmap);
1544 return isl_basic_map_finalize(bmap);
1546 isl_basic_map_free(bmap);
1547 isl_basic_set_free(bset);
1551 struct isl_basic_map *isl_basic_map_intersect(
1552 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1554 if (!bmap1 || !bmap2)
1557 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1558 bmap2->dim, isl_dim_param), goto error);
1559 if (isl_dim_total(bmap1->dim) ==
1560 isl_dim_size(bmap1->dim, isl_dim_param) &&
1561 isl_dim_total(bmap2->dim) !=
1562 isl_dim_size(bmap2->dim, isl_dim_param))
1563 return isl_basic_map_intersect(bmap2, bmap1);
1565 if (isl_dim_total(bmap2->dim) !=
1566 isl_dim_size(bmap2->dim, isl_dim_param))
1567 isl_assert(bmap1->ctx,
1568 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1570 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1571 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1574 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1576 bmap1 = isl_basic_map_simplify(bmap1);
1577 return isl_basic_map_finalize(bmap1);
1579 isl_basic_map_free(bmap1);
1580 isl_basic_map_free(bmap2);
1584 struct isl_basic_set *isl_basic_set_intersect(
1585 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1587 return (struct isl_basic_set *)
1588 isl_basic_map_intersect(
1589 (struct isl_basic_map *)bset1,
1590 (struct isl_basic_map *)bset2);
1593 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1596 struct isl_map *result;
1602 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1603 map2->dim, isl_dim_param), goto error);
1604 if (isl_dim_total(map1->dim) ==
1605 isl_dim_size(map1->dim, isl_dim_param) &&
1606 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1607 return isl_map_intersect(map2, map1);
1609 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1610 isl_assert(map1->ctx,
1611 isl_dim_equal(map1->dim, map2->dim), goto error);
1613 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
1614 F_ISSET(map2, ISL_MAP_DISJOINT))
1615 FL_SET(flags, ISL_MAP_DISJOINT);
1617 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1618 map1->n * map2->n, flags);
1621 for (i = 0; i < map1->n; ++i)
1622 for (j = 0; j < map2->n; ++j) {
1623 struct isl_basic_map *part;
1624 part = isl_basic_map_intersect(
1625 isl_basic_map_copy(map1->p[i]),
1626 isl_basic_map_copy(map2->p[j]));
1627 if (isl_basic_map_is_empty(part))
1628 isl_basic_map_free(part);
1630 result = isl_map_add(result, part);
1643 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1645 return (struct isl_set *)
1646 isl_map_intersect((struct isl_map *)set1,
1647 (struct isl_map *)set2);
1650 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1652 struct isl_dim *dim;
1653 struct isl_basic_set *bset;
1658 bmap = isl_basic_map_cow(bmap);
1661 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1662 in = isl_basic_map_n_in(bmap);
1663 bset = isl_basic_set_from_basic_map(bmap);
1664 bset = isl_basic_set_swap_vars(bset, in);
1665 return isl_basic_map_from_basic_set(bset, dim);
1668 /* Turn final n dimensions into existentially quantified variables.
1670 struct isl_basic_set *isl_basic_set_project_out(
1671 struct isl_basic_set *bset, unsigned n, unsigned flags)
1681 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1686 bset = isl_basic_set_cow(bset);
1688 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1689 old = bset->block2.data;
1690 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1691 (bset->extra + n) * (1 + row_size));
1692 if (!bset->block2.data)
1694 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1697 for (i = 0; i < n; ++i) {
1698 new_div[i] = bset->block2.data +
1699 (bset->extra + i) * (1 + row_size);
1700 isl_seq_clr(new_div[i], 1 + row_size);
1702 for (i = 0; i < bset->extra; ++i)
1703 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1705 bset->div = new_div;
1708 bset->dim = isl_dim_drop_outputs(bset->dim,
1709 isl_basic_set_n_dim(bset) - n, n);
1712 bset = isl_basic_set_simplify(bset);
1713 return isl_basic_set_finalize(bset);
1715 isl_basic_set_free(bset);
1719 struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1723 for (i = 0; i < n; ++i) {
1724 j = isl_basic_map_alloc_div(bmap);
1727 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1731 isl_basic_map_free(bmap);
1735 struct isl_basic_map *isl_basic_map_apply_range(
1736 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1738 struct isl_dim *dim_result = NULL;
1739 struct isl_basic_map *bmap;
1740 unsigned n_in, n_out, n, nparam, total, pos;
1741 struct isl_dim_map *dim_map1, *dim_map2;
1743 if (!bmap1 || !bmap2)
1746 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1747 isl_dim_copy(bmap2->dim));
1749 n_in = isl_basic_map_n_in(bmap1);
1750 n_out = isl_basic_map_n_out(bmap2);
1751 n = isl_basic_map_n_out(bmap1);
1752 nparam = isl_basic_map_n_param(bmap1);
1754 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1755 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1756 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1757 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1758 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1759 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1760 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1761 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1762 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1763 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1764 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1766 bmap = isl_basic_map_alloc_dim(dim_result,
1767 bmap1->n_div + bmap2->n_div + n,
1768 bmap1->n_eq + bmap2->n_eq,
1769 bmap1->n_ineq + bmap2->n_ineq);
1770 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1771 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1772 bmap = add_divs(bmap, n);
1773 bmap = isl_basic_map_simplify(bmap);
1774 return isl_basic_map_finalize(bmap);
1776 isl_basic_map_free(bmap1);
1777 isl_basic_map_free(bmap2);
1781 struct isl_basic_set *isl_basic_set_apply(
1782 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1787 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1790 return (struct isl_basic_set *)
1791 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1793 isl_basic_set_free(bset);
1794 isl_basic_map_free(bmap);
1798 struct isl_basic_map *isl_basic_map_apply_domain(
1799 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1801 if (!bmap1 || !bmap2)
1805 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1807 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1810 bmap1 = isl_basic_map_reverse(bmap1);
1811 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1812 return isl_basic_map_reverse(bmap1);
1814 isl_basic_map_free(bmap1);
1815 isl_basic_map_free(bmap2);
1819 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1820 * A \cap B -> f(A) + f(B)
1822 struct isl_basic_map *isl_basic_map_sum(
1823 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1825 unsigned n_in, n_out, nparam, total, pos;
1826 struct isl_basic_map *bmap = NULL;
1827 struct isl_dim_map *dim_map1, *dim_map2;
1830 if (!bmap1 || !bmap2)
1833 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1836 nparam = isl_basic_map_n_param(bmap1);
1837 n_in = isl_basic_map_n_in(bmap1);
1838 n_out = isl_basic_map_n_out(bmap1);
1840 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
1841 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1842 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
1843 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1844 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
1845 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1846 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1847 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
1848 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1849 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1850 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
1852 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
1853 bmap1->n_div + bmap2->n_div + 2 * n_out,
1854 bmap1->n_eq + bmap2->n_eq + n_out,
1855 bmap1->n_ineq + bmap2->n_ineq);
1856 for (i = 0; i < n_out; ++i) {
1857 int j = isl_basic_map_alloc_equality(bmap);
1860 isl_seq_clr(bmap->eq[j], 1+total);
1861 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
1862 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
1863 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
1865 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1866 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1867 bmap = add_divs(bmap, 2 * n_out);
1869 bmap = isl_basic_map_simplify(bmap);
1870 return isl_basic_map_finalize(bmap);
1872 isl_basic_map_free(bmap);
1873 isl_basic_map_free(bmap1);
1874 isl_basic_map_free(bmap2);
1878 /* Given a basic map A -> f(A), construct A -> -f(A).
1880 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
1885 bmap = isl_basic_map_cow(bmap);
1889 n = isl_basic_map_dim(bmap, isl_dim_out);
1890 off = isl_basic_map_offset(bmap, isl_dim_out);
1891 for (i = 0; i < bmap->n_eq; ++i)
1892 for (j = 0; j < n; ++j)
1893 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
1894 for (i = 0; i < bmap->n_ineq; ++i)
1895 for (j = 0; j < n; ++j)
1896 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
1897 for (i = 0; i < bmap->n_div; ++i)
1898 for (j = 0; j < n; ++j)
1899 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
1900 return isl_basic_map_finalize(bmap);
1903 /* Given a basic map A -> f(A) and an integer d, construct a basic map
1904 * A -> floor(f(A)/d).
1906 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
1909 unsigned n_in, n_out, nparam, total, pos;
1910 struct isl_basic_map *result = NULL;
1911 struct isl_dim_map *dim_map;
1917 nparam = isl_basic_map_n_param(bmap);
1918 n_in = isl_basic_map_n_in(bmap);
1919 n_out = isl_basic_map_n_out(bmap);
1921 total = nparam + n_in + n_out + bmap->n_div + n_out;
1922 dim_map = isl_dim_map_alloc(bmap->ctx, total);
1923 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
1924 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
1925 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
1926 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
1928 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
1929 bmap->n_div + n_out,
1930 bmap->n_eq, bmap->n_ineq + 2 * n_out);
1931 result = add_constraints_dim_map(result, bmap, dim_map);
1932 result = add_divs(result, n_out);
1933 for (i = 0; i < n_out; ++i) {
1935 j = isl_basic_map_alloc_inequality(result);
1938 isl_seq_clr(result->ineq[j], 1+total);
1939 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
1940 isl_int_set_si(result->ineq[j][1+pos+i], 1);
1941 j = isl_basic_map_alloc_inequality(result);
1944 isl_seq_clr(result->ineq[j], 1+total);
1945 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
1946 isl_int_set_si(result->ineq[j][1+pos+i], -1);
1947 isl_int_sub_ui(result->ineq[j][0], d, 1);
1950 result = isl_basic_map_simplify(result);
1951 return isl_basic_map_finalize(result);
1953 isl_basic_map_free(result);
1957 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
1963 i = isl_basic_map_alloc_equality(bmap);
1966 nparam = isl_basic_map_n_param(bmap);
1967 n_in = isl_basic_map_n_in(bmap);
1968 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
1969 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
1970 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
1971 return isl_basic_map_finalize(bmap);
1973 isl_basic_map_free(bmap);
1977 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
1983 i = isl_basic_map_alloc_inequality(bmap);
1986 nparam = isl_basic_map_n_param(bmap);
1987 n_in = isl_basic_map_n_in(bmap);
1988 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
1989 isl_int_set_si(bmap->ineq[i][0], -1);
1990 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
1991 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
1992 return isl_basic_map_finalize(bmap);
1994 isl_basic_map_free(bmap);
1998 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2004 i = isl_basic_map_alloc_inequality(bmap);
2007 nparam = isl_basic_map_n_param(bmap);
2008 n_in = isl_basic_map_n_in(bmap);
2009 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2010 isl_int_set_si(bmap->ineq[i][0], -1);
2011 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2012 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2013 return isl_basic_map_finalize(bmap);
2015 isl_basic_map_free(bmap);
2019 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2022 struct isl_basic_map *bmap;
2023 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2026 for (i = 0; i < n_equal && bmap; ++i)
2027 bmap = var_equal(bmap, i);
2028 return isl_basic_map_finalize(bmap);
2031 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2034 struct isl_basic_map *bmap;
2035 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2038 for (i = 0; i < pos && bmap; ++i)
2039 bmap = var_equal(bmap, i);
2041 bmap = var_less(bmap, pos);
2042 return isl_basic_map_finalize(bmap);
2045 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2048 struct isl_basic_map *bmap;
2049 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2052 for (i = 0; i < pos && bmap; ++i)
2053 bmap = var_equal(bmap, i);
2055 bmap = var_more(bmap, pos);
2056 return isl_basic_map_finalize(bmap);
2059 struct isl_basic_map *isl_basic_map_from_basic_set(
2060 struct isl_basic_set *bset, struct isl_dim *dim)
2062 struct isl_basic_map *bmap;
2064 bset = isl_basic_set_cow(bset);
2068 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2069 isl_dim_free(bset->dim);
2070 bmap = (struct isl_basic_map *) bset;
2072 return isl_basic_map_finalize(bmap);
2074 isl_basic_set_free(bset);
2079 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2083 if (bmap->dim->n_in == 0)
2084 return (struct isl_basic_set *)bmap;
2085 bmap = isl_basic_map_cow(bmap);
2088 bmap->dim = isl_dim_cow(bmap->dim);
2091 bmap->dim->n_out += bmap->dim->n_in;
2092 bmap->dim->n_in = 0;
2093 bmap = isl_basic_map_finalize(bmap);
2094 return (struct isl_basic_set *)bmap;
2096 isl_basic_map_free(bmap);
2100 /* For a div d = floor(f/m), add the constraints
2103 * -(f-(n-1)) + m d >= 0
2105 * Note that the second constraint is the negation of
2109 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2112 unsigned total = isl_basic_map_total_dim(bmap);
2113 unsigned div_pos = 1 + total - bmap->n_div + div;
2115 i = isl_basic_map_alloc_inequality(bmap);
2118 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2119 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2121 j = isl_basic_map_alloc_inequality(bmap);
2124 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2125 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2126 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2130 struct isl_basic_set *isl_basic_map_underlying_set(
2131 struct isl_basic_map *bmap)
2135 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2136 return (struct isl_basic_set *)bmap;
2137 bmap = isl_basic_map_cow(bmap);
2140 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2143 bmap->extra -= bmap->n_div;
2145 bmap = isl_basic_map_finalize(bmap);
2146 return (struct isl_basic_set *)bmap;
2151 struct isl_basic_map *isl_basic_map_overlying_set(
2152 struct isl_basic_set *bset, struct isl_basic_map *like)
2154 struct isl_basic_map *bmap;
2155 struct isl_ctx *ctx;
2162 isl_assert(ctx, bset->n_div == 0, goto error);
2163 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2164 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2166 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2167 isl_basic_map_free(like);
2168 return (struct isl_basic_map *)bset;
2170 bset = isl_basic_set_cow(bset);
2173 total = bset->dim->n_out + bset->extra;
2174 bmap = (struct isl_basic_map *)bset;
2175 isl_dim_free(bmap->dim);
2176 bmap->dim = isl_dim_copy(like->dim);
2179 bmap->n_div = like->n_div;
2180 bmap->extra += like->n_div;
2183 ltotal = total - bmap->extra + like->extra;
2186 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2187 bmap->extra * (1 + 1 + total));
2188 if (isl_blk_is_error(bmap->block2))
2190 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2194 for (i = 0; i < bmap->extra; ++i)
2195 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2196 for (i = 0; i < like->n_div; ++i) {
2197 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2198 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2200 bmap = isl_basic_map_extend_constraints(bmap,
2201 0, 2 * like->n_div);
2202 for (i = 0; i < like->n_div; ++i) {
2203 if (isl_int_is_zero(bmap->div[i][0]))
2205 if (add_div_constraints(bmap, i) < 0)
2209 isl_basic_map_free(like);
2210 bmap = isl_basic_map_simplify(bmap);
2211 bmap = isl_basic_map_finalize(bmap);
2214 isl_basic_map_free(like);
2215 isl_basic_set_free(bset);
2219 struct isl_basic_set *isl_basic_set_from_underlying_set(
2220 struct isl_basic_set *bset, struct isl_basic_set *like)
2222 return (struct isl_basic_set *)
2223 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2226 struct isl_set *isl_set_from_underlying_set(
2227 struct isl_set *set, struct isl_basic_set *like)
2233 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2235 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2236 isl_basic_set_free(like);
2239 set = isl_set_cow(set);
2242 for (i = 0; i < set->n; ++i) {
2243 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2244 isl_basic_set_copy(like));
2248 isl_dim_free(set->dim);
2249 set->dim = isl_dim_copy(like->dim);
2252 isl_basic_set_free(like);
2255 isl_basic_set_free(like);
2260 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2264 map = isl_map_cow(map);
2267 map->dim = isl_dim_cow(map->dim);
2271 for (i = 1; i < map->n; ++i)
2272 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2274 for (i = 0; i < map->n; ++i) {
2275 map->p[i] = (struct isl_basic_map *)
2276 isl_basic_map_underlying_set(map->p[i]);
2281 map->dim = isl_dim_underlying(map->dim, 0);
2283 isl_dim_free(map->dim);
2284 map->dim = isl_dim_copy(map->p[0]->dim);
2288 return (struct isl_set *)map;
2294 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2296 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2299 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2301 struct isl_basic_set *domain;
2305 n_out = isl_basic_map_n_out(bmap);
2306 domain = isl_basic_set_from_basic_map(bmap);
2307 return isl_basic_set_project_out(domain, n_out, 0);
2310 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2312 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2315 struct isl_set *isl_map_range(struct isl_map *map)
2318 struct isl_set *set;
2322 map = isl_map_cow(map);
2326 set = (struct isl_set *) map;
2327 if (set->dim->n_in != 0) {
2328 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2332 for (i = 0; i < map->n; ++i) {
2333 set->p[i] = isl_basic_map_range(map->p[i]);
2337 F_CLR(set, ISL_MAP_DISJOINT);
2338 F_CLR(set, ISL_SET_NORMALIZED);
2345 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2348 struct isl_map *map = NULL;
2350 set = isl_set_cow(set);
2353 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2354 map = (struct isl_map *)set;
2355 for (i = 0; i < set->n; ++i) {
2356 map->p[i] = isl_basic_map_from_basic_set(
2357 set->p[i], isl_dim_copy(dim));
2361 isl_dim_free(map->dim);
2370 struct isl_map *isl_map_from_range(struct isl_set *set)
2372 return (struct isl_map *)set;
2375 struct isl_set *isl_set_from_map(struct isl_map *map)
2378 struct isl_set *set = NULL;
2382 map = isl_map_cow(map);
2385 map->dim = isl_dim_cow(map->dim);
2388 map->dim->n_out += map->dim->n_in;
2390 set = (struct isl_set *)map;
2391 for (i = 0; i < map->n; ++i) {
2392 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2402 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2404 struct isl_map *map;
2408 isl_assert(dim->ctx, n >= 0, return NULL);
2409 map = isl_alloc(dim->ctx, struct isl_map,
2410 sizeof(struct isl_map) +
2411 n * sizeof(struct isl_basic_map *));
2415 map->ctx = dim->ctx;
2416 isl_ctx_ref(map->ctx);
2428 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2429 unsigned nparam, unsigned in, unsigned out, int n,
2432 struct isl_map *map;
2433 struct isl_dim *dims;
2435 dims = isl_dim_alloc(ctx, nparam, in, out);
2439 map = isl_map_alloc_dim(dims, n, flags);
2443 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2444 unsigned nparam, unsigned in, unsigned out)
2446 struct isl_basic_map *bmap;
2447 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2448 bmap = isl_basic_map_set_to_empty(bmap);
2452 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2454 struct isl_basic_set *bset;
2455 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2456 bset = isl_basic_set_set_to_empty(bset);
2460 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2462 struct isl_basic_map *bmap;
2465 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2466 bmap = isl_basic_map_set_to_empty(bmap);
2470 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2472 struct isl_basic_map *bmap;
2475 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2476 bmap = isl_basic_map_set_to_empty(bmap);
2480 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2482 struct isl_basic_set *bset;
2485 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2486 bset = isl_basic_set_set_to_empty(bset);
2490 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2492 struct isl_basic_map *bmap;
2493 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2497 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2499 struct isl_basic_set *bset;
2500 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2504 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2508 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2511 struct isl_map *isl_map_empty(struct isl_dim *dim)
2513 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2516 struct isl_map *isl_map_empty_like(struct isl_map *model)
2520 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2523 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2527 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2530 struct isl_set *isl_set_empty(struct isl_dim *dim)
2532 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2535 struct isl_set *isl_set_empty_like(struct isl_set *model)
2539 return isl_set_empty(isl_dim_copy(model->dim));
2542 struct isl_set *isl_set_universe(struct isl_dim *dim)
2544 struct isl_set *set;
2547 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2548 set = isl_set_add(set, isl_basic_set_universe(dim));
2552 struct isl_map *isl_map_dup(struct isl_map *map)
2555 struct isl_map *dup;
2559 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2560 for (i = 0; i < map->n; ++i)
2561 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2565 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2569 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2570 isl_assert(map->ctx, map->n < map->size, goto error);
2571 map->p[map->n] = bmap;
2573 F_CLR(map, ISL_MAP_NORMALIZED);
2579 isl_basic_map_free(bmap);
2583 void isl_map_free(struct isl_map *map)
2593 isl_ctx_deref(map->ctx);
2594 for (i = 0; i < map->n; ++i)
2595 isl_basic_map_free(map->p[i]);
2596 isl_dim_free(map->dim);
2600 struct isl_map *isl_map_extend(struct isl_map *base,
2601 unsigned nparam, unsigned n_in, unsigned n_out)
2605 base = isl_map_cow(base);
2609 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2612 for (i = 0; i < base->n; ++i) {
2613 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2614 isl_dim_copy(base->dim), 0, 0, 0);
2624 struct isl_set *isl_set_extend(struct isl_set *base,
2625 unsigned nparam, unsigned dim)
2627 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2631 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2632 unsigned pos, int value)
2636 bmap = isl_basic_map_cow(bmap);
2637 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2638 j = isl_basic_map_alloc_equality(bmap);
2641 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2642 isl_int_set_si(bmap->eq[j][pos], -1);
2643 isl_int_set_si(bmap->eq[j][0], value);
2644 bmap = isl_basic_map_simplify(bmap);
2645 return isl_basic_map_finalize(bmap);
2647 isl_basic_map_free(bmap);
2651 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2652 enum isl_dim_type type, unsigned pos, int value)
2656 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2657 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2660 isl_basic_map_free(bmap);
2664 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2665 enum isl_dim_type type, unsigned pos, int value)
2667 return (struct isl_basic_set *)
2668 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2672 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2673 unsigned input, int value)
2675 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2678 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2679 unsigned dim, int value)
2681 return (struct isl_basic_set *)
2682 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2683 isl_dim_set, dim, value);
2686 struct isl_map *isl_map_fix_si(struct isl_map *map,
2687 enum isl_dim_type type, unsigned pos, int value)
2691 map = isl_map_cow(map);
2695 isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2696 for (i = 0; i < map->n; ++i) {
2697 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2701 F_CLR(map, ISL_MAP_NORMALIZED);
2708 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2709 unsigned input, int value)
2711 return isl_map_fix_si(map, isl_dim_in, input, value);
2714 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2716 return (struct isl_set *)
2717 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2720 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2721 unsigned dim, isl_int value)
2726 bset = isl_basic_set_cow(bset);
2727 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2728 j = isl_basic_set_alloc_inequality(bset);
2731 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2732 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2733 isl_int_neg(bset->ineq[j][0], value);
2734 bset = isl_basic_set_simplify(bset);
2735 return isl_basic_set_finalize(bset);
2737 isl_basic_set_free(bset);
2741 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2746 set = isl_set_cow(set);
2750 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2751 for (i = 0; i < set->n; ++i) {
2752 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2762 struct isl_map *isl_map_reverse(struct isl_map *map)
2767 map = isl_map_cow(map);
2771 map->dim = isl_dim_reverse(map->dim);
2774 for (i = 0; i < map->n; ++i) {
2775 map->p[i] = isl_basic_map_reverse(map->p[i]);
2779 F_CLR(map, ISL_MAP_NORMALIZED);
2786 struct isl_map *isl_basic_map_lexmax(
2787 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2788 struct isl_set **empty)
2790 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2793 struct isl_map *isl_basic_map_lexmin(
2794 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2795 struct isl_set **empty)
2797 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2800 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2802 struct isl_basic_map *bmap = NULL;
2803 struct isl_basic_set *dom = NULL;
2804 struct isl_map *min;
2805 struct isl_dim *param_dim;
2809 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2812 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2813 dom = isl_basic_set_universe(param_dim);
2816 min = isl_basic_map_lexmin(bmap, dom, NULL);
2817 return isl_map_range(min);
2819 isl_basic_map_free(bmap);
2823 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2830 off = isl_dim_total(bmap->dim);
2831 for (i = 0; i < bmap->n_div; ++i) {
2832 if (isl_int_is_zero(bmap->div[i][0]))
2833 return isl_pip_basic_map_compute_divs(bmap);
2834 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2837 return isl_map_from_basic_map(bmap);
2839 isl_basic_map_free(bmap);
2843 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2846 struct isl_map *res;
2852 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2853 for (i = 1 ; i < map->n; ++i) {
2855 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2856 if (F_ISSET(map, ISL_MAP_DISJOINT))
2857 res = isl_map_union_disjoint(res, r2);
2859 res = isl_map_union(res, r2);
2866 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2868 return (struct isl_set *)
2869 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2872 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2874 return (struct isl_set *)
2875 isl_map_compute_divs((struct isl_map *)set);
2878 struct isl_set *isl_map_domain(struct isl_map *map)
2881 struct isl_set *set;
2886 map = isl_map_cow(map);
2890 set = (struct isl_set *)map;
2891 set->dim = isl_dim_domain(set->dim);
2894 for (i = 0; i < map->n; ++i) {
2895 set->p[i] = isl_basic_map_domain(map->p[i]);
2899 F_CLR(set, ISL_MAP_DISJOINT);
2900 F_CLR(set, ISL_SET_NORMALIZED);
2907 struct isl_map *isl_map_union_disjoint(
2908 struct isl_map *map1, struct isl_map *map2)
2912 struct isl_map *map = NULL;
2926 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2928 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2929 F_ISSET(map2, ISL_MAP_DISJOINT))
2930 FL_SET(flags, ISL_MAP_DISJOINT);
2932 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2933 map1->n + map2->n, flags);
2936 for (i = 0; i < map1->n; ++i) {
2937 map = isl_map_add(map,
2938 isl_basic_map_copy(map1->p[i]));
2942 for (i = 0; i < map2->n; ++i) {
2943 map = isl_map_add(map,
2944 isl_basic_map_copy(map2->p[i]));
2958 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
2960 map1 = isl_map_union_disjoint(map1, map2);
2964 F_CLR(map1, ISL_MAP_DISJOINT);
2968 struct isl_set *isl_set_union_disjoint(
2969 struct isl_set *set1, struct isl_set *set2)
2971 return (struct isl_set *)
2972 isl_map_union_disjoint(
2973 (struct isl_map *)set1, (struct isl_map *)set2);
2976 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
2978 return (struct isl_set *)
2979 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
2982 struct isl_map *isl_map_intersect_range(
2983 struct isl_map *map, struct isl_set *set)
2986 struct isl_map *result;
2992 if (F_ISSET(map, ISL_MAP_DISJOINT) &&
2993 F_ISSET(set, ISL_MAP_DISJOINT))
2994 FL_SET(flags, ISL_MAP_DISJOINT);
2996 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
2997 map->n * set->n, flags);
3000 for (i = 0; i < map->n; ++i)
3001 for (j = 0; j < set->n; ++j) {
3002 result = isl_map_add(result,
3003 isl_basic_map_intersect_range(
3004 isl_basic_map_copy(map->p[i]),
3005 isl_basic_set_copy(set->p[j])));
3018 struct isl_map *isl_map_intersect_domain(
3019 struct isl_map *map, struct isl_set *set)
3021 return isl_map_reverse(
3022 isl_map_intersect_range(isl_map_reverse(map), set));
3025 struct isl_map *isl_map_apply_domain(
3026 struct isl_map *map1, struct isl_map *map2)
3030 map1 = isl_map_reverse(map1);
3031 map1 = isl_map_apply_range(map1, map2);
3032 return isl_map_reverse(map1);
3039 struct isl_map *isl_map_apply_range(
3040 struct isl_map *map1, struct isl_map *map2)
3042 struct isl_dim *dim_result;
3043 struct isl_map *result;
3052 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3053 isl_dim_copy(map2->dim));
3055 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3058 for (i = 0; i < map1->n; ++i)
3059 for (j = 0; j < map2->n; ++j) {
3060 result = isl_map_add(result,
3061 isl_basic_map_apply_range(
3062 isl_basic_map_copy(map1->p[i]),
3063 isl_basic_map_copy(map2->p[j])));
3069 if (result && result->n <= 1)
3070 F_SET(result, ISL_MAP_DISJOINT);
3079 * returns range - domain
3081 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3083 struct isl_basic_set *bset;
3090 dim = isl_basic_map_n_in(bmap);
3091 nparam = isl_basic_map_n_param(bmap);
3092 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3093 bset = isl_basic_set_from_basic_map(bmap);
3094 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3095 bset = isl_basic_set_swap_vars(bset, 2*dim);
3096 for (i = 0; i < dim; ++i) {
3097 int j = isl_basic_map_alloc_equality(
3098 (struct isl_basic_map *)bset);
3101 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3102 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3103 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3104 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3106 return isl_basic_set_project_out(bset, 2*dim, 0);
3108 isl_basic_map_free(bmap);
3113 * returns range - domain
3115 struct isl_set *isl_map_deltas(struct isl_map *map)
3118 struct isl_set *result;
3123 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3124 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3125 isl_map_n_in(map), map->n, map->flags);
3128 for (i = 0; i < map->n; ++i)
3129 result = isl_set_add(result,
3130 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3138 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3140 struct isl_basic_map *bmap;
3148 nparam = dims->nparam;
3150 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3154 for (i = 0; i < dim; ++i) {
3155 int j = isl_basic_map_alloc_equality(bmap);
3158 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3159 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3160 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3162 return isl_basic_map_finalize(bmap);
3164 isl_basic_map_free(bmap);
3168 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3170 struct isl_dim *dim = isl_dim_map(set_dim);
3173 return basic_map_identity(dim);
3176 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3178 if (!model || !model->dim)
3180 isl_assert(model->ctx,
3181 model->dim->n_in == model->dim->n_out, return NULL);
3182 return basic_map_identity(isl_dim_copy(model->dim));
3185 static struct isl_map *map_identity(struct isl_dim *dim)
3187 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3188 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3191 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3193 struct isl_dim *dim = isl_dim_map(set_dim);
3196 return map_identity(dim);
3199 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3201 if (!model || !model->dim)
3203 isl_assert(model->ctx,
3204 model->dim->n_in == model->dim->n_out, return NULL);
3205 return map_identity(isl_dim_copy(model->dim));
3208 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3210 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3213 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3215 return isl_map_is_subset(
3216 (struct isl_map *)set1, (struct isl_map *)set2);
3219 int isl_basic_map_is_subset(
3220 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3223 struct isl_map *map1;
3224 struct isl_map *map2;
3226 if (!bmap1 || !bmap2)
3229 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3230 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3232 is_subset = isl_map_is_subset(map1, map2);
3240 int isl_basic_map_is_equal(
3241 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3245 if (!bmap1 || !bmap2)
3247 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3250 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3254 int isl_basic_set_is_equal(
3255 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3257 return isl_basic_map_is_equal(
3258 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3261 int isl_map_is_empty(struct isl_map *map)
3268 for (i = 0; i < map->n; ++i) {
3269 is_empty = isl_basic_map_is_empty(map->p[i]);
3278 int isl_map_fast_is_empty(struct isl_map *map)
3283 int isl_set_is_empty(struct isl_set *set)
3285 return isl_map_is_empty((struct isl_map *)set);
3288 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3292 struct isl_map *diff;
3297 if (isl_map_is_empty(map1))
3300 if (isl_map_is_empty(map2))
3303 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3307 is_subset = isl_map_is_empty(diff);
3313 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3319 is_subset = isl_map_is_subset(map1, map2);
3322 is_subset = isl_map_is_subset(map2, map1);
3326 int isl_basic_map_is_strict_subset(
3327 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3331 if (!bmap1 || !bmap2)
3333 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3336 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3337 if (is_subset == -1)
3342 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3348 total = 1 + isl_basic_map_total_dim(bmap);
3349 if (total != vec->size)
3354 for (i = 0; i < bmap->n_eq; ++i) {
3355 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3356 if (!isl_int_is_zero(s)) {
3362 for (i = 0; i < bmap->n_ineq; ++i) {
3363 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3364 if (isl_int_is_neg(s)) {
3375 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3379 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3382 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3384 struct isl_basic_set *bset = NULL;
3385 struct isl_vec *sample = NULL;
3392 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3395 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3396 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3397 copy = isl_basic_map_convex_hull(copy);
3398 empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3399 isl_basic_map_free(copy);
3403 total = 1 + isl_basic_map_total_dim(bmap);
3404 if (bmap->sample && bmap->sample->size == total) {
3405 int contains = basic_map_contains(bmap, bmap->sample);
3411 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3414 sample = isl_basic_set_sample(bset);
3417 empty = sample->size == 0;
3419 isl_vec_free(bmap->ctx, bmap->sample);
3420 bmap->sample = sample;
3425 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3427 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3430 struct isl_map *isl_basic_map_union(
3431 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3433 struct isl_map *map;
3434 if (!bmap1 || !bmap2)
3437 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3439 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3442 map = isl_map_add(map, bmap1);
3443 map = isl_map_add(map, bmap2);
3446 isl_basic_map_free(bmap1);
3447 isl_basic_map_free(bmap2);
3451 struct isl_set *isl_basic_set_union(
3452 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3454 return (struct isl_set *)isl_basic_map_union(
3455 (struct isl_basic_map *)bset1,
3456 (struct isl_basic_map *)bset2);
3459 /* Order divs such that any div only depends on previous divs */
3460 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3463 unsigned off = isl_dim_total(bmap->dim);
3465 for (i = 0; i < bmap->n_div; ++i) {
3467 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3471 swap_div(bmap, i, pos);
3477 /* Look for a div in dst that corresponds to the div "div" in src.
3478 * The divs before "div" in src and dst are assumed to be the same.
3480 * Returns -1 if no corresponding div was found and the position
3481 * of the corresponding div in dst otherwise.
3483 static int find_div(struct isl_basic_map *dst,
3484 struct isl_basic_map *src, unsigned div)
3488 unsigned total = isl_dim_total(src->dim);
3490 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3491 for (i = div; i < dst->n_div; ++i)
3492 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3493 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3494 dst->n_div - div) == -1)
3499 struct isl_basic_map *isl_basic_map_align_divs(
3500 struct isl_basic_map *dst, struct isl_basic_map *src)
3503 unsigned total = isl_dim_total(src->dim);
3508 if (src->n_div == 0)
3511 for (i = 0; i < src->n_div; ++i)
3512 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3514 src = order_divs(src);
3515 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3516 src->n_div, 0, 2 * src->n_div);
3519 for (i = 0; i < src->n_div; ++i) {
3520 int j = find_div(dst, src, i);
3522 j = isl_basic_map_alloc_div(dst);
3525 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3526 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3527 if (add_div_constraints(dst, j) < 0)
3531 swap_div(dst, i, j);
3535 isl_basic_map_free(dst);
3539 struct isl_basic_set *isl_basic_set_align_divs(
3540 struct isl_basic_set *dst, struct isl_basic_set *src)
3542 return (struct isl_basic_set *)isl_basic_map_align_divs(
3543 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3546 struct isl_map *isl_map_align_divs(struct isl_map *map)
3550 map = isl_map_compute_divs(map);
3551 map = isl_map_cow(map);
3555 for (i = 1; i < map->n; ++i)
3556 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3557 for (i = 1; i < map->n; ++i)
3558 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3560 F_CLR(map, ISL_MAP_NORMALIZED);
3564 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3565 struct isl_basic_map *src, isl_int *c,
3566 unsigned len, int oppose)
3568 struct isl_basic_map *copy = NULL;
3573 copy = isl_basic_map_copy(src);
3574 copy = isl_basic_map_cow(copy);
3577 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3578 k = isl_basic_map_alloc_inequality(copy);
3582 isl_seq_neg(copy->ineq[k], c, len);
3584 isl_seq_cpy(copy->ineq[k], c, len);
3585 total = 1 + isl_basic_map_total_dim(copy);
3586 isl_seq_clr(copy->ineq[k]+len, total - len);
3587 isl_inequality_negate(copy, k);
3588 copy = isl_basic_map_simplify(copy);
3589 copy = isl_basic_map_finalize(copy);
3590 is_empty = isl_basic_map_is_empty(copy);
3594 dst = isl_map_add(dst, copy);
3596 isl_basic_map_free(copy);
3599 isl_basic_map_free(copy);
3604 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3608 struct isl_map *rest = NULL;
3610 unsigned total = isl_basic_map_total_dim(bmap);
3617 if (F_ISSET(map, ISL_MAP_DISJOINT))
3618 FL_SET(flags, ISL_MAP_DISJOINT);
3620 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3621 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3625 for (i = 0; i < map->n; ++i) {
3626 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3631 for (j = 0; j < map->n; ++j)
3632 map->p[j] = isl_basic_map_cow(map->p[j]);
3634 for (i = 0; i < bmap->n_eq; ++i) {
3635 for (j = 0; j < map->n; ++j) {
3636 rest = add_cut_constraint(rest,
3637 map->p[j], bmap->eq[i], 1+total, 0);
3641 rest = add_cut_constraint(rest,
3642 map->p[j], bmap->eq[i], 1+total, 1);
3646 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3650 k = isl_basic_map_alloc_equality(map->p[j]);
3653 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3654 isl_seq_clr(map->p[j]->eq[k]+1+total,
3655 map->p[j]->n_div - bmap->n_div);
3659 for (i = 0; i < bmap->n_ineq; ++i) {
3660 for (j = 0; j < map->n; ++j) {
3661 rest = add_cut_constraint(rest,
3662 map->p[j], bmap->ineq[i], 1+total, 0);
3666 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3670 k = isl_basic_map_alloc_inequality(map->p[j]);
3673 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3674 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3675 map->p[j]->n_div - bmap->n_div);
3687 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3693 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3695 if (isl_map_is_empty(map2)) {
3700 map1 = isl_map_compute_divs(map1);
3701 map2 = isl_map_compute_divs(map2);
3705 for (i = 0; map1 && i < map2->n; ++i)
3706 map1 = subtract(map1, map2->p[i]);
3716 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3718 return (struct isl_set *)
3720 (struct isl_map *)set1, (struct isl_map *)set2);
3723 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3727 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3728 map = isl_map_intersect_domain(map, set);
3729 set = isl_map_range(map);
3737 /* There is no need to cow as removing empty parts doesn't change
3738 * the meaning of the set.
3740 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3747 for (i = map->n-1; i >= 0; --i) {
3748 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3750 isl_basic_map_free(map->p[i]);
3751 if (i != map->n-1) {
3752 F_CLR(map, ISL_MAP_NORMALIZED);
3753 map->p[i] = map->p[map->n-1];
3761 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3763 return (struct isl_set *)
3764 isl_map_remove_empty_parts((struct isl_map *)set);
3767 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
3769 struct isl_basic_map *bmap;
3770 if (!map || map->n == 0)
3772 bmap = map->p[map->n-1];
3773 isl_assert(map->ctx, F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
3774 return isl_basic_map_copy(bmap);
3777 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3779 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
3782 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
3783 struct isl_basic_map *bmap)
3789 for (i = map->n-1; i >= 0; --i) {
3790 if (map->p[i] != bmap)
3792 map = isl_map_cow(map);
3795 isl_basic_map_free(map->p[i]);
3796 if (i != map->n-1) {
3797 F_CLR(map, ISL_SET_NORMALIZED);
3798 map->p[i] = map->p[map->n-1];
3803 isl_basic_map_free(bmap);
3807 isl_basic_map_free(bmap);
3811 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3812 struct isl_basic_set *bset)
3814 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
3815 (struct isl_basic_map *)bset);
3818 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3819 * for any common value of the parameters and dimensions preceding dim
3820 * in both basic sets, the values of dimension pos in bset1 are
3821 * smaller or larger than those in bset2.
3824 * 1 if bset1 follows bset2
3825 * -1 if bset1 precedes bset2
3826 * 0 if bset1 and bset2 are incomparable
3827 * -2 if some error occurred.
3829 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3830 struct isl_basic_set *bset2, int pos)
3832 struct isl_dim *dims;
3833 struct isl_basic_map *bmap1 = NULL;
3834 struct isl_basic_map *bmap2 = NULL;
3835 struct isl_ctx *ctx;
3836 struct isl_vec *obj;
3839 unsigned dim1, dim2;
3841 enum isl_lp_result res;
3844 if (!bset1 || !bset2)
3847 nparam = isl_basic_set_n_param(bset1);
3848 dim1 = isl_basic_set_n_dim(bset1);
3849 dim2 = isl_basic_set_n_dim(bset2);
3850 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3851 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3852 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3853 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3854 if (!bmap1 || !bmap2)
3856 bmap1 = isl_basic_map_extend(bmap1, nparam,
3857 pos, (dim1 - pos) + (dim2 - pos),
3858 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3859 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
3862 total = isl_basic_map_total_dim(bmap1);
3864 obj = isl_vec_alloc(ctx, total);
3865 isl_seq_clr(obj->block.data, total);
3866 isl_int_set_si(obj->block.data[nparam+pos], 1);
3867 isl_int_set_si(obj->block.data[nparam+pos+(dim1-pos)], -1);
3872 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3873 if (res == isl_lp_empty)
3875 else if (res == isl_lp_ok && isl_int_is_pos(num))
3877 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3878 res == isl_lp_unbounded)
3884 isl_basic_map_free(bmap1);
3885 isl_vec_free(ctx, obj);
3888 isl_basic_map_free(bmap1);
3889 isl_basic_map_free(bmap2);
3893 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
3894 unsigned pos, isl_int *val)
3902 total = isl_basic_map_total_dim(bmap);
3903 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
3904 for (; d+1 > pos; --d)
3905 if (!isl_int_is_zero(bmap->eq[i][1+d]))
3909 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
3911 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
3913 if (!isl_int_is_one(bmap->eq[i][1+d]))
3916 isl_int_neg(*val, bmap->eq[i][0]);
3922 static int isl_map_fast_has_fixed_var(struct isl_map *map,
3923 unsigned pos, isl_int *val)
3935 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
3938 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
3939 for (i = 1; fixed == 1 && i < map->n; ++i) {
3940 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
3941 if (fixed == 1 && isl_int_ne(tmp, v))
3945 isl_int_set(*val, v);
3951 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
3954 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
3957 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
3958 enum isl_dim_type type, unsigned pos, isl_int *val)
3960 if (pos >= isl_basic_map_dim(bmap, type))
3962 return isl_basic_map_fast_has_fixed_var(bmap,
3963 isl_basic_map_offset(bmap, type) - 1 + pos, val);
3966 /* Check if dimension dim has fixed value and if so and if val is not NULL,
3967 * then return this fixed value in *val.
3969 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
3971 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
3974 /* Check if input variable in has fixed value and if so and if val is not NULL,
3975 * then return this fixed value in *val.
3977 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
3979 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
3982 /* Check if dimension dim has an (obvious) fixed lower bound and if so
3983 * and if val is not NULL, then return this lower bound in *val.
3985 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
3986 unsigned dim, isl_int *val)
3988 int i, i_eq = -1, i_ineq = -1;
3995 total = isl_basic_set_total_dim(bset);
3996 nparam = isl_basic_set_n_param(bset);
3997 for (i = 0; i < bset->n_eq; ++i) {
3998 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4004 for (i = 0; i < bset->n_ineq; ++i) {
4005 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4007 if (i_eq != -1 || i_ineq != -1)
4011 if (i_eq == -1 && i_ineq == -1)
4013 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4014 /* The coefficient should always be one due to normalization. */
4015 if (!isl_int_is_one(c[1+nparam+dim]))
4017 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4019 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4020 total - nparam - dim - 1) != -1)
4023 isl_int_neg(*val, c[0]);
4027 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4028 unsigned dim, isl_int *val)
4040 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4044 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4046 for (i = 1; fixed == 1 && i < set->n; ++i) {
4047 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4049 if (fixed == 1 && isl_int_ne(tmp, v))
4053 isl_int_set(*val, v);
4064 static int qsort_constraint_cmp(const void *p1, const void *p2)
4066 const struct constraint *c1 = (const struct constraint *)p1;
4067 const struct constraint *c2 = (const struct constraint *)p2;
4068 unsigned size = isl_min(c1->size, c2->size);
4069 return isl_seq_cmp(c1->c, c2->c, size);
4072 static struct isl_basic_map *isl_basic_map_sort_constraints(
4073 struct isl_basic_map *bmap)
4076 struct constraint *c;
4081 total = isl_basic_map_total_dim(bmap);
4082 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4085 for (i = 0; i < bmap->n_ineq; ++i) {
4087 c[i].c = bmap->ineq[i];
4089 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4090 for (i = 0; i < bmap->n_ineq; ++i)
4091 bmap->ineq[i] = c[i].c;
4095 isl_basic_map_free(bmap);
4099 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4103 if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4105 bmap = isl_basic_map_convex_hull(bmap);
4106 bmap = isl_basic_map_sort_constraints(bmap);
4107 F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4111 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4113 return (struct isl_basic_set *)isl_basic_map_normalize(
4114 (struct isl_basic_map *)bset);
4117 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4118 const struct isl_basic_map *bmap2)
4125 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4126 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4127 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4128 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4129 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4130 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4131 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4132 F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4134 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4136 if (F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4138 if (bmap1->n_eq != bmap2->n_eq)
4139 return bmap1->n_eq - bmap2->n_eq;
4140 if (bmap1->n_ineq != bmap2->n_ineq)
4141 return bmap1->n_ineq - bmap2->n_ineq;
4142 if (bmap1->n_div != bmap2->n_div)
4143 return bmap1->n_div - bmap2->n_div;
4144 total = isl_basic_map_total_dim(bmap1);
4145 for (i = 0; i < bmap1->n_eq; ++i) {
4146 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4150 for (i = 0; i < bmap1->n_ineq; ++i) {
4151 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4155 for (i = 0; i < bmap1->n_div; ++i) {
4156 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4163 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4164 struct isl_basic_map *bmap2)
4166 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4169 static int qsort_bmap_cmp(const void *p1, const void *p2)
4171 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4172 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4174 return isl_basic_map_fast_cmp(bmap1, bmap2);
4177 /* We normalize in place, but if anything goes wrong we need
4178 * to return NULL, so we need to make sure we don't change the
4179 * meaning of any possible other copies of map.
4181 struct isl_map *isl_map_normalize(struct isl_map *map)
4184 struct isl_basic_map *bmap;
4188 if (F_ISSET(map, ISL_MAP_NORMALIZED))
4190 for (i = 0; i < map->n; ++i) {
4191 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4194 isl_basic_map_free(map->p[i]);
4197 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4198 F_SET(map, ISL_MAP_NORMALIZED);
4199 map = isl_map_remove_empty_parts(map);
4202 for (i = map->n - 1; i >= 1; --i) {
4203 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4205 isl_basic_map_free(map->p[i-1]);
4206 for (j = i; j < map->n; ++j)
4207 map->p[j-1] = map->p[j];
4217 struct isl_set *isl_set_normalize(struct isl_set *set)
4219 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4222 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4232 if (!isl_dim_equal(map1->dim, map2->dim))
4235 map1 = isl_map_copy(map1);
4236 map2 = isl_map_copy(map2);
4237 map1 = isl_map_normalize(map1);
4238 map2 = isl_map_normalize(map2);
4241 equal = map1->n == map2->n;
4242 for (i = 0; equal && i < map1->n; ++i) {
4243 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4256 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4258 return isl_map_fast_is_equal((struct isl_map *)set1,
4259 (struct isl_map *)set2);
4262 /* Return an interval that ranges from min to max (inclusive)
4264 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4265 isl_int min, isl_int max)
4268 struct isl_basic_set *bset = NULL;
4270 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4274 k = isl_basic_set_alloc_inequality(bset);
4277 isl_int_set_si(bset->ineq[k][1], 1);
4278 isl_int_neg(bset->ineq[k][0], min);
4280 k = isl_basic_set_alloc_inequality(bset);
4283 isl_int_set_si(bset->ineq[k][1], -1);
4284 isl_int_set(bset->ineq[k][0], max);
4288 isl_basic_set_free(bset);
4292 /* Return the Cartesian product of the basic sets in list (in the given order).
4294 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4302 struct isl_basic_set *product = NULL;
4306 isl_assert(list->ctx, list->n > 0, goto error);
4307 isl_assert(list->ctx, list->p[0], goto error);
4308 nparam = isl_basic_set_n_param(list->p[0]);
4309 dim = isl_basic_set_n_dim(list->p[0]);
4310 extra = list->p[0]->n_div;
4311 n_eq = list->p[0]->n_eq;
4312 n_ineq = list->p[0]->n_ineq;
4313 for (i = 1; i < list->n; ++i) {
4314 isl_assert(list->ctx, list->p[i], goto error);
4315 isl_assert(list->ctx,
4316 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4317 dim += isl_basic_set_n_dim(list->p[i]);
4318 extra += list->p[i]->n_div;
4319 n_eq += list->p[i]->n_eq;
4320 n_ineq += list->p[i]->n_ineq;
4322 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4327 for (i = 0; i < list->n; ++i) {
4328 isl_basic_set_add_constraints(product,
4329 isl_basic_set_copy(list->p[i]), dim);
4330 dim += isl_basic_set_n_dim(list->p[i]);
4332 isl_basic_set_list_free(list);
4335 isl_basic_set_free(product);
4336 isl_basic_set_list_free(list);
4340 struct isl_basic_map *isl_basic_map_product(
4341 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4343 struct isl_dim *dim_result = NULL;
4344 struct isl_basic_map *bmap;
4345 unsigned in1, in2, out1, out2, nparam, total, pos;
4346 struct isl_dim_map *dim_map1, *dim_map2;
4348 if (!bmap1 || !bmap2)
4351 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4352 bmap2->dim, isl_dim_param), goto error);
4353 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4354 isl_dim_copy(bmap2->dim));
4356 in1 = isl_basic_map_n_in(bmap1);
4357 in2 = isl_basic_map_n_in(bmap2);
4358 out1 = isl_basic_map_n_out(bmap1);
4359 out2 = isl_basic_map_n_out(bmap2);
4360 nparam = isl_basic_map_n_param(bmap1);
4362 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4363 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4364 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4365 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4366 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4367 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4368 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4369 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4370 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4371 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4372 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4374 bmap = isl_basic_map_alloc_dim(dim_result,
4375 bmap1->n_div + bmap2->n_div,
4376 bmap1->n_eq + bmap2->n_eq,
4377 bmap1->n_ineq + bmap2->n_ineq);
4378 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4379 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4380 bmap = isl_basic_map_simplify(bmap);
4381 return isl_basic_map_finalize(bmap);
4383 isl_basic_map_free(bmap1);
4384 isl_basic_map_free(bmap2);
4388 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4390 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4393 struct isl_map *result;
4399 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4400 map2->dim, isl_dim_param), goto error);
4402 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
4403 F_ISSET(map2, ISL_MAP_DISJOINT))
4404 FL_SET(flags, ISL_MAP_DISJOINT);
4406 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4407 isl_dim_copy(map2->dim)),
4408 map1->n * map2->n, flags);
4411 for (i = 0; i < map1->n; ++i)
4412 for (j = 0; j < map2->n; ++j) {
4413 struct isl_basic_map *part;
4414 part = isl_basic_map_product(
4415 isl_basic_map_copy(map1->p[i]),
4416 isl_basic_map_copy(map2->p[j]));
4417 if (isl_basic_map_is_empty(part))
4418 isl_basic_map_free(part);
4420 result = isl_map_add(result, part);
4433 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4441 bset = isl_basic_set_copy(bset);
4442 bset = isl_basic_set_normalize(bset);
4445 total = isl_basic_set_total_dim(bset);
4446 isl_hash_byte(hash, bset->n_eq & 0xFF);
4447 for (i = 0; i < bset->n_eq; ++i) {
4449 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4450 isl_hash_hash(hash, c_hash);
4452 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4453 for (i = 0; i < bset->n_ineq; ++i) {
4455 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4456 isl_hash_hash(hash, c_hash);
4458 isl_hash_byte(hash, bset->n_div & 0xFF);
4459 for (i = 0; i < bset->n_div; ++i) {
4461 if (isl_int_is_zero(bset->div[i][0]))
4463 isl_hash_byte(hash, i & 0xFF);
4464 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4465 isl_hash_hash(hash, c_hash);
4467 isl_basic_set_free(bset);
4471 uint32_t isl_set_get_hash(struct isl_set *set)
4478 set = isl_set_copy(set);
4479 set = isl_set_normalize(set);
4483 hash = isl_hash_init();
4484 for (i = 0; i < set->n; ++i) {
4486 bset_hash = isl_basic_set_get_hash(set->p[i]);
4487 isl_hash_hash(hash, bset_hash);
4495 /* Check if the value for dimension dim is completely determined
4496 * by the values of the other parameters and variables.
4497 * That is, check if dimension dim is involved in an equality.
4499 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4506 nparam = isl_basic_set_n_param(bset);
4507 for (i = 0; i < bset->n_eq; ++i)
4508 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4513 /* Check if the value for dimension dim is completely determined
4514 * by the values of the other parameters and variables.
4515 * That is, check if dimension dim is involved in an equality
4516 * for each of the subsets.
4518 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4524 for (i = 0; i < set->n; ++i) {
4526 unique = isl_basic_set_dim_is_unique(set->p[i], dim);