6 #include "isl_equalities.h"
12 #include "isl_map_private.h"
13 #include "isl_map_piplib.h"
14 #include "isl_sample.h"
18 /* Maps dst positions to src positions */
24 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
27 struct isl_dim_map *dim_map;
28 dim_map = isl_alloc(ctx, struct isl_dim_map,
29 sizeof(struct isl_dim_map) + len * sizeof(int));
32 dim_map->len = 1 + len;
34 for (i = 0; i < len; ++i)
35 dim_map->pos[1 + i] = -1;
39 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
42 case isl_dim_param: return dim->nparam;
43 case isl_dim_in: return dim->n_in;
44 case isl_dim_out: return dim->n_out;
45 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
49 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
52 case isl_dim_param: return 1;
53 case isl_dim_in: return 1 + dim->nparam;
54 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
58 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
59 enum isl_dim_type type, unsigned dst_pos)
67 src_pos = pos(dim, type);
68 for (i = 0; i < n(dim, type); ++i)
69 dim_map->pos[1 + dst_pos + i] = src_pos + i;
72 static void isl_dim_map_div(struct isl_dim_map *dim_map,
73 struct isl_basic_map *bmap, unsigned dst_pos)
78 if (!dim_map || !bmap)
81 src_pos = 1 + isl_dim_total(bmap->dim);
82 for (i = 0; i < bmap->n_div; ++i)
83 dim_map->pos[1 + dst_pos + i] = src_pos + i;
86 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
90 for (i = 0; i < dim_map->len; ++i)
91 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
92 fprintf(stderr, "\n");
95 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
96 enum isl_dim_type type)
101 case isl_dim_out: return isl_dim_size(bmap->dim, type);
102 case isl_dim_div: return bmap->n_div;
103 case isl_dim_all: return isl_basic_map_total_dim(bmap);
107 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
109 return n(map->dim, type);
112 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
114 return n(set->dim, type);
117 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
118 enum isl_dim_type type)
120 struct isl_dim *dim = bmap->dim;
122 case isl_dim_param: return 1;
123 case isl_dim_in: return 1 + dim->nparam;
124 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
125 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
129 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
131 return pos(map->dim, type);
134 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
135 enum isl_dim_type type)
137 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
140 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
142 return bset->dim->n_out;
145 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
147 return bset->dim->nparam;
150 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
152 return isl_dim_total(bset->dim) + bset->n_div;
155 unsigned isl_set_n_dim(const struct isl_set *set)
157 return set->dim->n_out;
160 unsigned isl_set_n_param(const struct isl_set *set)
162 return set->dim->nparam;
165 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
167 return bmap->dim->n_in;
170 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
172 return bmap->dim->n_out;
175 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
177 return bmap->dim->nparam;
180 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
185 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
187 return isl_dim_total(bmap->dim) + bmap->n_div;
190 unsigned isl_map_n_in(const struct isl_map *map)
192 return map->dim->n_in;
195 unsigned isl_map_n_out(const struct isl_map *map)
197 return map->dim->n_out;
200 unsigned isl_map_n_param(const struct isl_map *map)
202 return map->dim->nparam;
205 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
207 return map->dim->n_in == set->dim->n_out &&
208 map->dim->nparam == set->dim->nparam;
211 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
212 struct isl_basic_set *bset)
214 return bmap->dim->n_in == bset->dim->n_out &&
215 bmap->dim->nparam == bset->dim->nparam;
218 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
219 struct isl_basic_set *bset)
221 return bmap->dim->n_out == bset->dim->n_out &&
222 bmap->dim->nparam == bset->dim->nparam;
225 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
229 return isl_dim_copy(bmap->dim);
232 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
236 return isl_dim_copy(bset->dim);
239 struct isl_dim *isl_map_get_dim(struct isl_map *map)
243 return isl_dim_copy(map->dim);
246 struct isl_dim *isl_set_get_dim(struct isl_set *set)
250 return isl_dim_copy(set->dim);
253 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
254 struct isl_basic_map *bmap, unsigned extra,
255 unsigned n_eq, unsigned n_ineq)
258 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
260 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
261 if (isl_blk_is_error(bmap->block)) {
266 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
268 isl_blk_free(ctx, bmap->block);
274 bmap->block2 = isl_blk_empty();
277 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
278 if (isl_blk_is_error(bmap->block2)) {
280 isl_blk_free(ctx, bmap->block);
285 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
287 isl_blk_free(ctx, bmap->block2);
289 isl_blk_free(ctx, bmap->block);
295 for (i = 0; i < n_ineq + n_eq; ++i)
296 bmap->ineq[i] = bmap->block.data + i * row_size;
298 for (i = 0; i < extra; ++i)
299 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
305 bmap->c_size = n_eq + n_ineq;
306 bmap->eq = bmap->ineq + n_ineq;
315 isl_basic_map_free(bmap);
319 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
320 unsigned nparam, unsigned dim, unsigned extra,
321 unsigned n_eq, unsigned n_ineq)
323 struct isl_basic_map *bmap;
324 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
325 return (struct isl_basic_set *)bmap;
328 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
329 unsigned extra, unsigned n_eq, unsigned n_ineq)
331 struct isl_basic_map *bmap;
334 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
335 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
336 return (struct isl_basic_set *)bmap;
339 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
340 unsigned extra, unsigned n_eq, unsigned n_ineq)
342 struct isl_basic_map *bmap;
346 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
351 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
357 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
358 unsigned nparam, unsigned in, unsigned out, unsigned extra,
359 unsigned n_eq, unsigned n_ineq)
361 struct isl_basic_map *bmap;
364 dim = isl_dim_alloc(ctx, nparam, in, out);
368 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
372 static void dup_constraints(
373 struct isl_basic_map *dst, struct isl_basic_map *src)
376 unsigned total = isl_basic_map_total_dim(src);
378 for (i = 0; i < src->n_eq; ++i) {
379 int j = isl_basic_map_alloc_equality(dst);
380 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
383 for (i = 0; i < src->n_ineq; ++i) {
384 int j = isl_basic_map_alloc_inequality(dst);
385 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
388 for (i = 0; i < src->n_div; ++i) {
389 int j = isl_basic_map_alloc_div(dst);
390 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
392 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
395 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
397 struct isl_basic_map *dup;
401 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
402 bmap->n_div, bmap->n_eq, bmap->n_ineq);
405 dup_constraints(dup, bmap);
406 dup->flags = bmap->flags;
407 dup->sample = isl_vec_copy(bmap->sample);
411 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
413 struct isl_basic_map *dup;
415 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
416 return (struct isl_basic_set *)dup;
419 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
424 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
428 return isl_basic_set_dup(bset);
431 struct isl_set *isl_set_copy(struct isl_set *set)
440 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
445 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
449 return isl_basic_map_dup(bmap);
452 struct isl_map *isl_map_copy(struct isl_map *map)
461 void isl_basic_map_free(struct isl_basic_map *bmap)
469 isl_ctx_deref(bmap->ctx);
471 isl_blk_free(bmap->ctx, bmap->block2);
473 isl_blk_free(bmap->ctx, bmap->block);
474 isl_vec_free(bmap->sample);
475 isl_dim_free(bmap->dim);
479 void isl_basic_set_free(struct isl_basic_set *bset)
481 isl_basic_map_free((struct isl_basic_map *)bset);
484 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
486 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
489 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
495 isl_assert(ctx, room_for_con(bmap, 1), return -1);
496 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
498 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
499 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
500 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
501 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
502 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
503 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
505 int j = isl_basic_map_alloc_inequality(bmap);
509 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
510 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
517 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
518 bmap->extra - bmap->n_div);
522 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
524 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
527 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
531 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
536 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
538 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
541 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
546 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
548 if (pos != bmap->n_eq - 1) {
550 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
551 bmap->eq[bmap->n_eq - 1] = t;
557 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
559 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
562 void isl_basic_map_inequality_to_equality(
563 struct isl_basic_map *bmap, unsigned pos)
568 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
569 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
574 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
575 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
576 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
577 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
580 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
582 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
585 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
591 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
592 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
593 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
594 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
595 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
596 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
597 1 + isl_basic_map_total_dim(bmap),
598 bmap->extra - bmap->n_div);
599 return bmap->n_ineq++;
602 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
604 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
607 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
611 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
616 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
618 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
621 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
626 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
628 if (pos != bmap->n_ineq - 1) {
630 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
631 bmap->ineq[bmap->n_ineq - 1] = t;
632 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
638 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
640 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
643 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
647 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
648 isl_seq_clr(bmap->div[bmap->n_div] +
649 1 + 1 + isl_basic_map_total_dim(bmap),
650 bmap->extra - bmap->n_div);
651 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
652 return bmap->n_div++;
655 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
657 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
660 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
664 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
669 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
671 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
674 /* Copy constraint from src to dst, putting the vars of src at offset
675 * dim_off in dst and the divs of src at offset div_off in dst.
676 * If both sets are actually map, then dim_off applies to the input
679 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
680 struct isl_basic_map *src_map, isl_int *src,
681 unsigned in_off, unsigned out_off, unsigned div_off)
683 unsigned src_nparam = isl_basic_map_n_param(src_map);
684 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
685 unsigned src_in = isl_basic_map_n_in(src_map);
686 unsigned dst_in = isl_basic_map_n_in(dst_map);
687 unsigned src_out = isl_basic_map_n_out(src_map);
688 unsigned dst_out = isl_basic_map_n_out(dst_map);
689 isl_int_set(dst[0], src[0]);
690 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
691 if (dst_nparam > src_nparam)
692 isl_seq_clr(dst+1+src_nparam,
693 dst_nparam - src_nparam);
694 isl_seq_clr(dst+1+dst_nparam, in_off);
695 isl_seq_cpy(dst+1+dst_nparam+in_off,
697 isl_min(dst_in-in_off, src_in));
698 if (dst_in-in_off > src_in)
699 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
700 dst_in - in_off - src_in);
701 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
702 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
703 src+1+src_nparam+src_in,
704 isl_min(dst_out-out_off, src_out));
705 if (dst_out-out_off > src_out)
706 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
707 dst_out - out_off - src_out);
708 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
709 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
710 src+1+src_nparam+src_in+src_out,
711 isl_min(dst_map->extra-div_off, src_map->n_div));
712 if (dst_map->n_div-div_off > src_map->n_div)
713 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
714 div_off+src_map->n_div,
715 dst_map->n_div - div_off - src_map->n_div);
718 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
719 struct isl_basic_map *src_map, isl_int *src,
720 unsigned in_off, unsigned out_off, unsigned div_off)
722 isl_int_set(dst[0], src[0]);
723 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
726 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
727 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
732 if (!bmap1 || !bmap2)
735 div_off = bmap1->n_div;
737 for (i = 0; i < bmap2->n_eq; ++i) {
738 int i1 = isl_basic_map_alloc_equality(bmap1);
741 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
742 i_pos, o_pos, div_off);
745 for (i = 0; i < bmap2->n_ineq; ++i) {
746 int i1 = isl_basic_map_alloc_inequality(bmap1);
749 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
750 i_pos, o_pos, div_off);
753 for (i = 0; i < bmap2->n_div; ++i) {
754 int i1 = isl_basic_map_alloc_div(bmap1);
757 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
758 i_pos, o_pos, div_off);
761 isl_basic_map_free(bmap2);
766 isl_basic_map_free(bmap1);
767 isl_basic_map_free(bmap2);
771 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
772 struct isl_dim_map *dim_map)
776 for (i = 0; i < dim_map->len; ++i) {
777 if (dim_map->pos[i] < 0)
778 isl_int_set_si(dst[i], 0);
780 isl_int_set(dst[i], src[dim_map->pos[i]]);
784 static void copy_div_dim_map(isl_int *dst, isl_int *src,
785 struct isl_dim_map *dim_map)
787 isl_int_set(dst[0], src[0]);
788 copy_constraint_dim_map(dst+1, src+1, dim_map);
791 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
792 struct isl_basic_map *src, struct isl_dim_map *dim_map)
796 if (!src || !dst || !dim_map)
799 for (i = 0; i < src->n_eq; ++i) {
800 int i1 = isl_basic_map_alloc_equality(dst);
803 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
806 for (i = 0; i < src->n_ineq; ++i) {
807 int i1 = isl_basic_map_alloc_inequality(dst);
810 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
813 for (i = 0; i < src->n_div; ++i) {
814 int i1 = isl_basic_map_alloc_div(dst);
817 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
821 isl_basic_map_free(src);
826 isl_basic_map_free(src);
827 isl_basic_map_free(dst);
831 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
832 struct isl_basic_set *bset2, unsigned pos)
834 return (struct isl_basic_set *)
835 add_constraints((struct isl_basic_map *)bset1,
836 (struct isl_basic_map *)bset2, 0, pos);
839 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
840 struct isl_dim *dim, unsigned extra,
841 unsigned n_eq, unsigned n_ineq)
843 struct isl_basic_map *ext;
853 dims_ok = isl_dim_equal(base->dim, dim) &&
854 base->extra >= base->n_div + extra;
856 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
857 room_for_ineq(base, n_ineq)) {
862 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
863 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
864 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
865 extra += base->extra;
867 n_ineq += base->n_ineq;
869 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
875 ext->sample = isl_vec_copy(base->sample);
877 ext = add_constraints(ext, base, 0, 0);
880 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
887 isl_basic_map_free(base);
891 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
892 struct isl_dim *dim, unsigned extra,
893 unsigned n_eq, unsigned n_ineq)
895 return (struct isl_basic_set *)
896 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
897 extra, n_eq, n_ineq);
900 struct isl_basic_map *isl_basic_map_extend_constraints(
901 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
905 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
909 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
910 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
911 unsigned n_eq, unsigned n_ineq)
913 struct isl_basic_map *bmap;
918 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
922 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
926 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
927 unsigned nparam, unsigned dim, unsigned extra,
928 unsigned n_eq, unsigned n_ineq)
930 return (struct isl_basic_set *)
931 isl_basic_map_extend((struct isl_basic_map *)base,
932 nparam, 0, dim, extra, n_eq, n_ineq);
935 struct isl_basic_set *isl_basic_set_extend_constraints(
936 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
938 return (struct isl_basic_set *)
939 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
943 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
945 return (struct isl_basic_set *)
946 isl_basic_map_cow((struct isl_basic_map *)bset);
949 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
956 bmap = isl_basic_map_dup(bmap);
958 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
962 struct isl_set *isl_set_cow(struct isl_set *set)
970 return isl_set_dup(set);
973 struct isl_map *isl_map_cow(struct isl_map *map)
981 return isl_map_dup(map);
984 static void swap_vars(struct isl_blk blk, isl_int *a,
985 unsigned a_len, unsigned b_len)
987 isl_seq_cpy(blk.data, a+a_len, b_len);
988 isl_seq_cpy(blk.data+b_len, a, a_len);
989 isl_seq_cpy(a, blk.data, b_len+a_len);
992 struct isl_basic_set *isl_basic_set_swap_vars(
993 struct isl_basic_set *bset, unsigned n)
1003 nparam = isl_basic_set_n_param(bset);
1004 dim = isl_basic_set_n_dim(bset);
1005 isl_assert(bset->ctx, n <= dim, goto error);
1010 bset = isl_basic_set_cow(bset);
1014 blk = isl_blk_alloc(bset->ctx, dim);
1015 if (isl_blk_is_error(blk))
1018 for (i = 0; i < bset->n_eq; ++i)
1020 bset->eq[i]+1+nparam, n, dim - n);
1022 for (i = 0; i < bset->n_ineq; ++i)
1024 bset->ineq[i]+1+nparam, n, dim - n);
1026 for (i = 0; i < bset->n_div; ++i)
1028 bset->div[i]+1+1+nparam, n, dim - n);
1030 isl_blk_free(bset->ctx, blk);
1032 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1036 isl_basic_set_free(bset);
1040 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1043 set = isl_set_cow(set);
1047 for (i = 0; i < set->n; ++i) {
1048 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1054 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1058 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1064 total = isl_basic_map_total_dim(bmap);
1065 isl_basic_map_free_div(bmap, bmap->n_div);
1066 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1068 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1070 isl_basic_map_alloc_equality(bmap);
1074 isl_int_set_si(bmap->eq[i][0], 1);
1075 isl_seq_clr(bmap->eq[i]+1, total);
1076 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1077 return isl_basic_map_finalize(bmap);
1079 isl_basic_map_free(bmap);
1083 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1085 return (struct isl_basic_set *)
1086 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1089 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1092 unsigned off = isl_dim_total(bmap->dim);
1093 isl_int *t = bmap->div[a];
1094 bmap->div[a] = bmap->div[b];
1097 for (i = 0; i < bmap->n_eq; ++i)
1098 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1100 for (i = 0; i < bmap->n_ineq; ++i)
1101 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1103 for (i = 0; i < bmap->n_div; ++i)
1104 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1105 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1108 /* Eliminate the specified n dimensions starting at first from the
1109 * constraints using Fourier-Motzkin, The dimensions themselves
1112 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1113 unsigned first, unsigned n)
1123 set = isl_set_cow(set);
1126 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1127 nparam = isl_set_n_param(set);
1129 for (i = 0; i < set->n; ++i) {
1130 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1141 /* Project out n dimensions starting at first using Fourier-Motzkin */
1142 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1143 unsigned first, unsigned n)
1145 set = isl_set_eliminate_dims(set, first, n);
1146 set = isl_set_drop_dims(set, first, n);
1150 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1152 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1160 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1169 set = isl_set_cow(set);
1173 for (i = 0; i < set->n; ++i) {
1174 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1184 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1185 enum isl_dim_type type, unsigned first, unsigned n)
1189 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1193 bmap = isl_basic_map_eliminate_vars(bmap,
1194 isl_basic_map_offset(bmap, type) - 1 + first, n);
1197 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1199 bmap = isl_basic_map_drop(bmap, type, first, n);
1202 isl_basic_map_free(bmap);
1206 struct isl_map *isl_map_remove(struct isl_map *map,
1207 enum isl_dim_type type, unsigned first, unsigned n)
1214 map = isl_map_cow(map);
1217 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1219 for (i = 0; i < map->n; ++i) {
1220 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1221 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1225 map = isl_map_drop(map, type, first, n);
1232 /* Project out n inputs starting at first using Fourier-Motzkin */
1233 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1234 unsigned first, unsigned n)
1236 return isl_map_remove(map, isl_dim_in, first, n);
1239 /* Project out n dimensions starting at first using Fourier-Motzkin */
1240 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1241 unsigned first, unsigned n)
1243 unsigned nparam = isl_basic_set_n_param(bset);
1244 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1245 bset = isl_basic_set_drop_dims(bset, first, n);
1249 static void dump_term(struct isl_basic_map *bmap,
1250 isl_int c, int pos, FILE *out)
1253 unsigned in = isl_basic_map_n_in(bmap);
1254 unsigned dim = in + isl_basic_map_n_out(bmap);
1255 unsigned nparam = isl_basic_map_n_param(bmap);
1257 isl_int_print(out, c, 0);
1259 if (!isl_int_is_one(c))
1260 isl_int_print(out, c, 0);
1261 if (pos < 1 + nparam) {
1262 name = isl_dim_get_name(bmap->dim,
1263 isl_dim_param, pos - 1);
1265 fprintf(out, "%s", name);
1267 fprintf(out, "p%d", pos - 1);
1268 } else if (pos < 1 + nparam + in)
1269 fprintf(out, "i%d", pos - 1 - nparam);
1270 else if (pos < 1 + nparam + dim)
1271 fprintf(out, "o%d", pos - 1 - nparam - in);
1273 fprintf(out, "e%d", pos - 1 - nparam - dim);
1277 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1278 int sign, FILE *out)
1282 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1286 for (i = 0, first = 1; i < len; ++i) {
1287 if (isl_int_sgn(c[i]) * sign <= 0)
1290 fprintf(out, " + ");
1292 isl_int_abs(v, c[i]);
1293 dump_term(bmap, v, i, out);
1300 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1301 const char *op, FILE *out, int indent)
1305 fprintf(out, "%*s", indent, "");
1307 dump_constraint_sign(bmap, c, 1, out);
1308 fprintf(out, " %s ", op);
1309 dump_constraint_sign(bmap, c, -1, out);
1313 for (i = bmap->n_div; i < bmap->extra; ++i) {
1314 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1316 fprintf(out, "%*s", indent, "");
1317 fprintf(out, "ERROR: unused div coefficient not zero\n");
1322 static void dump_constraints(struct isl_basic_map *bmap,
1323 isl_int **c, unsigned n,
1324 const char *op, FILE *out, int indent)
1328 for (i = 0; i < n; ++i)
1329 dump_constraint(bmap, c[i], op, out, indent);
1332 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1336 unsigned total = isl_basic_map_total_dim(bmap);
1338 for (j = 0; j < 1 + total; ++j) {
1339 if (isl_int_is_zero(exp[j]))
1341 if (!first && isl_int_is_pos(exp[j]))
1343 dump_term(bmap, exp[j], j, out);
1348 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1352 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1353 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1355 for (i = 0; i < bmap->n_div; ++i) {
1356 fprintf(out, "%*s", indent, "");
1357 fprintf(out, "e%d = [(", i);
1358 dump_affine(bmap, bmap->div[i]+1, out);
1360 isl_int_print(out, bmap->div[i][0], 0);
1361 fprintf(out, "]\n");
1365 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1368 fprintf(out, "null basic set\n");
1372 fprintf(out, "%*s", indent, "");
1373 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1374 bset->ref, bset->dim->nparam, bset->dim->n_out,
1375 bset->extra, bset->flags);
1376 dump((struct isl_basic_map *)bset, out, indent);
1379 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1382 fprintf(out, "null basic map\n");
1386 fprintf(out, "%*s", indent, "");
1387 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1388 "flags: %x, n_name: %d\n",
1390 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1391 bmap->extra, bmap->flags, bmap->dim->n_name);
1392 dump(bmap, out, indent);
1395 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1400 total = isl_basic_map_total_dim(bmap);
1401 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1402 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1403 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1404 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1408 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1410 struct isl_set *set;
1414 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1415 isl_assert(dim->ctx, n >= 0, return NULL);
1416 set = isl_alloc(dim->ctx, struct isl_set,
1417 sizeof(struct isl_set) +
1418 (n - 1) * sizeof(struct isl_basic_set *));
1422 set->ctx = dim->ctx;
1423 isl_ctx_ref(set->ctx);
1435 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1436 unsigned nparam, unsigned dim, int n, unsigned flags)
1438 struct isl_set *set;
1439 struct isl_dim *dims;
1441 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1445 set = isl_set_alloc_dim(dims, n, flags);
1449 /* Make sure "map" has room for at least "n" more basic maps.
1451 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1454 struct isl_map *grown = NULL;
1458 isl_assert(map->ctx, n >= 0, goto error);
1459 if (map->n + n <= map->size)
1461 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1464 for (i = 0; i < map->n; ++i) {
1465 grown->p[i] = isl_basic_map_copy(map->p[i]);
1473 isl_map_free(grown);
1478 /* Make sure "set" has room for at least "n" more basic sets.
1480 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1482 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1485 struct isl_set *isl_set_dup(struct isl_set *set)
1488 struct isl_set *dup;
1493 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1496 for (i = 0; i < set->n; ++i)
1497 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1501 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1503 struct isl_set *set;
1508 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1510 isl_basic_set_free(bset);
1513 return isl_set_add(set, bset);
1516 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1518 struct isl_map *map;
1523 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1525 isl_basic_map_free(bmap);
1528 return isl_map_add(map, bmap);
1531 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1533 return (struct isl_set *)isl_map_add((struct isl_map *)set,
1534 (struct isl_basic_map *)bset);
1537 void isl_set_free(struct isl_set *set)
1547 isl_ctx_deref(set->ctx);
1548 for (i = 0; i < set->n; ++i)
1549 isl_basic_set_free(set->p[i]);
1550 isl_dim_free(set->dim);
1554 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1559 fprintf(out, "null set\n");
1563 fprintf(out, "%*s", indent, "");
1564 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1565 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1567 for (i = 0; i < set->n; ++i) {
1568 fprintf(out, "%*s", indent, "");
1569 fprintf(out, "basic set %d:\n", i);
1570 isl_basic_set_dump(set->p[i], out, indent+4);
1574 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1579 fprintf(out, "null map\n");
1583 fprintf(out, "%*s", indent, "");
1584 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1585 "flags: %x, n_name: %d\n",
1586 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1587 map->dim->n_out, map->flags, map->dim->n_name);
1588 for (i = 0; i < map->n; ++i) {
1589 fprintf(out, "%*s", indent, "");
1590 fprintf(out, "basic map %d:\n", i);
1591 isl_basic_map_dump(map->p[i], out, indent+4);
1595 struct isl_basic_map *isl_basic_map_intersect_domain(
1596 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1598 struct isl_basic_map *bmap_domain;
1599 struct isl_dim *dim;
1604 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1605 bset->dim, isl_dim_param), goto error);
1607 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1608 isl_assert(bset->ctx,
1609 isl_basic_map_compatible_domain(bmap, bset), goto error);
1611 bmap = isl_basic_map_cow(bmap);
1612 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1613 bset->n_div, bset->n_eq, bset->n_ineq);
1616 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1617 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1618 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1620 bmap = isl_basic_map_simplify(bmap);
1621 return isl_basic_map_finalize(bmap);
1623 isl_basic_map_free(bmap);
1624 isl_basic_set_free(bset);
1628 struct isl_basic_map *isl_basic_map_intersect_range(
1629 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1631 struct isl_basic_map *bmap_range;
1636 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1637 bset->dim, isl_dim_param), goto error);
1639 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1640 isl_assert(bset->ctx,
1641 isl_basic_map_compatible_range(bmap, bset), goto error);
1643 bmap = isl_basic_map_cow(bmap);
1644 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1645 bset->n_div, bset->n_eq, bset->n_ineq);
1648 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1649 bmap = add_constraints(bmap, bmap_range, 0, 0);
1651 bmap = isl_basic_map_simplify(bmap);
1652 return isl_basic_map_finalize(bmap);
1654 isl_basic_map_free(bmap);
1655 isl_basic_set_free(bset);
1659 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1665 total = 1 + isl_basic_map_total_dim(bmap);
1666 if (total != vec->size)
1671 for (i = 0; i < bmap->n_eq; ++i) {
1672 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1673 if (!isl_int_is_zero(s)) {
1679 for (i = 0; i < bmap->n_ineq; ++i) {
1680 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1681 if (isl_int_is_neg(s)) {
1692 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1694 return basic_map_contains((struct isl_basic_map *)bset, vec);
1697 struct isl_basic_map *isl_basic_map_intersect(
1698 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1700 struct isl_vec *sample = NULL;
1702 if (!bmap1 || !bmap2)
1705 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1706 bmap2->dim, isl_dim_param), goto error);
1707 if (isl_dim_total(bmap1->dim) ==
1708 isl_dim_size(bmap1->dim, isl_dim_param) &&
1709 isl_dim_total(bmap2->dim) !=
1710 isl_dim_size(bmap2->dim, isl_dim_param))
1711 return isl_basic_map_intersect(bmap2, bmap1);
1713 if (isl_dim_total(bmap2->dim) !=
1714 isl_dim_size(bmap2->dim, isl_dim_param))
1715 isl_assert(bmap1->ctx,
1716 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1718 if (bmap1->sample &&
1719 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1720 basic_map_contains(bmap2, bmap1->sample) > 0)
1721 sample = isl_vec_copy(bmap1->sample);
1722 else if (bmap2->sample &&
1723 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1724 basic_map_contains(bmap2, bmap2->sample) > 0)
1725 sample = isl_vec_copy(bmap2->sample);
1727 bmap1 = isl_basic_map_cow(bmap1);
1728 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1729 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1732 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1735 isl_vec_free(bmap1->sample);
1736 bmap1->sample = sample;
1739 bmap1 = isl_basic_map_simplify(bmap1);
1740 return isl_basic_map_finalize(bmap1);
1743 isl_vec_free(sample);
1744 isl_basic_map_free(bmap1);
1745 isl_basic_map_free(bmap2);
1749 struct isl_basic_set *isl_basic_set_intersect(
1750 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1752 return (struct isl_basic_set *)
1753 isl_basic_map_intersect(
1754 (struct isl_basic_map *)bset1,
1755 (struct isl_basic_map *)bset2);
1758 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1761 struct isl_map *result;
1767 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1768 map2->dim, isl_dim_param), goto error);
1769 if (isl_dim_total(map1->dim) ==
1770 isl_dim_size(map1->dim, isl_dim_param) &&
1771 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1772 return isl_map_intersect(map2, map1);
1774 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1775 isl_assert(map1->ctx,
1776 isl_dim_equal(map1->dim, map2->dim), goto error);
1778 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1779 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1780 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1782 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1783 map1->n * map2->n, flags);
1786 for (i = 0; i < map1->n; ++i)
1787 for (j = 0; j < map2->n; ++j) {
1788 struct isl_basic_map *part;
1789 part = isl_basic_map_intersect(
1790 isl_basic_map_copy(map1->p[i]),
1791 isl_basic_map_copy(map2->p[j]));
1792 if (isl_basic_map_is_empty(part))
1793 isl_basic_map_free(part);
1795 result = isl_map_add(result, part);
1808 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1810 return (struct isl_set *)
1811 isl_map_intersect((struct isl_map *)set1,
1812 (struct isl_map *)set2);
1815 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1817 struct isl_dim *dim;
1818 struct isl_basic_set *bset;
1823 bmap = isl_basic_map_cow(bmap);
1826 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1827 in = isl_basic_map_n_in(bmap);
1828 bset = isl_basic_set_from_basic_map(bmap);
1829 bset = isl_basic_set_swap_vars(bset, in);
1830 return isl_basic_map_from_basic_set(bset, dim);
1833 /* Turn final n dimensions into existentially quantified variables.
1835 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
1836 enum isl_dim_type type, unsigned first, unsigned n)
1846 isl_assert(bset->ctx, type == isl_dim_set, goto error);
1847 isl_assert(bset->ctx, first + n == isl_basic_set_n_dim(bset), goto error);
1852 bset = isl_basic_set_cow(bset);
1854 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1855 old = bset->block2.data;
1856 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1857 (bset->extra + n) * (1 + row_size));
1858 if (!bset->block2.data)
1860 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1863 for (i = 0; i < n; ++i) {
1864 new_div[i] = bset->block2.data +
1865 (bset->extra + i) * (1 + row_size);
1866 isl_seq_clr(new_div[i], 1 + row_size);
1868 for (i = 0; i < bset->extra; ++i)
1869 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1871 bset->div = new_div;
1874 bset->dim = isl_dim_drop_outputs(bset->dim,
1875 isl_basic_set_n_dim(bset) - n, n);
1878 bset = isl_basic_set_simplify(bset);
1879 bset = isl_basic_set_drop_redundant_divs(bset);
1880 return isl_basic_set_finalize(bset);
1882 isl_basic_set_free(bset);
1886 /* Turn final n dimensions into existentially quantified variables.
1888 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
1889 enum isl_dim_type type, unsigned first, unsigned n)
1896 isl_assert(set->ctx, type == isl_dim_set, goto error);
1897 isl_assert(set->ctx, first + n == isl_set_n_dim(set), goto error);
1902 set = isl_set_cow(set);
1905 set->dim = isl_dim_drop_outputs(set->dim, first, n);
1906 for (i = 0; i < set->n; ++i) {
1907 set->p[i] = isl_basic_set_project_out(set->p[i], type, first, n);
1912 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1919 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1923 for (i = 0; i < n; ++i) {
1924 j = isl_basic_map_alloc_div(bmap);
1927 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1931 isl_basic_map_free(bmap);
1935 struct isl_basic_map *isl_basic_map_apply_range(
1936 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1938 struct isl_dim *dim_result = NULL;
1939 struct isl_basic_map *bmap;
1940 unsigned n_in, n_out, n, nparam, total, pos;
1941 struct isl_dim_map *dim_map1, *dim_map2;
1943 if (!bmap1 || !bmap2)
1946 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1947 isl_dim_copy(bmap2->dim));
1949 n_in = isl_basic_map_n_in(bmap1);
1950 n_out = isl_basic_map_n_out(bmap2);
1951 n = isl_basic_map_n_out(bmap1);
1952 nparam = isl_basic_map_n_param(bmap1);
1954 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1955 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1956 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1957 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1958 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1959 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1960 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1961 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1962 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1963 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1964 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1966 bmap = isl_basic_map_alloc_dim(dim_result,
1967 bmap1->n_div + bmap2->n_div + n,
1968 bmap1->n_eq + bmap2->n_eq,
1969 bmap1->n_ineq + bmap2->n_ineq);
1970 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1971 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1972 bmap = add_divs(bmap, n);
1973 bmap = isl_basic_map_simplify(bmap);
1974 bmap = isl_basic_map_drop_redundant_divs(bmap);
1975 return isl_basic_map_finalize(bmap);
1977 isl_basic_map_free(bmap1);
1978 isl_basic_map_free(bmap2);
1982 struct isl_basic_set *isl_basic_set_apply(
1983 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1988 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
1991 return (struct isl_basic_set *)
1992 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1994 isl_basic_set_free(bset);
1995 isl_basic_map_free(bmap);
1999 struct isl_basic_map *isl_basic_map_apply_domain(
2000 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2002 if (!bmap1 || !bmap2)
2005 isl_assert(bmap1->ctx,
2006 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2007 isl_assert(bmap1->ctx,
2008 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2011 bmap1 = isl_basic_map_reverse(bmap1);
2012 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2013 return isl_basic_map_reverse(bmap1);
2015 isl_basic_map_free(bmap1);
2016 isl_basic_map_free(bmap2);
2020 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2021 * A \cap B -> f(A) + f(B)
2023 struct isl_basic_map *isl_basic_map_sum(
2024 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2026 unsigned n_in, n_out, nparam, total, pos;
2027 struct isl_basic_map *bmap = NULL;
2028 struct isl_dim_map *dim_map1, *dim_map2;
2031 if (!bmap1 || !bmap2)
2034 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2037 nparam = isl_basic_map_n_param(bmap1);
2038 n_in = isl_basic_map_n_in(bmap1);
2039 n_out = isl_basic_map_n_out(bmap1);
2041 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2042 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2043 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2044 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2045 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2046 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2047 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2048 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2049 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2050 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2051 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2053 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2054 bmap1->n_div + bmap2->n_div + 2 * n_out,
2055 bmap1->n_eq + bmap2->n_eq + n_out,
2056 bmap1->n_ineq + bmap2->n_ineq);
2057 for (i = 0; i < n_out; ++i) {
2058 int j = isl_basic_map_alloc_equality(bmap);
2061 isl_seq_clr(bmap->eq[j], 1+total);
2062 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2063 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2064 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2066 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2067 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2068 bmap = add_divs(bmap, 2 * n_out);
2070 bmap = isl_basic_map_simplify(bmap);
2071 return isl_basic_map_finalize(bmap);
2073 isl_basic_map_free(bmap);
2074 isl_basic_map_free(bmap1);
2075 isl_basic_map_free(bmap2);
2079 /* Given two maps A -> f(A) and B -> g(B), construct a map
2080 * A \cap B -> f(A) + f(B)
2082 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2084 struct isl_map *result;
2090 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2092 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2093 map1->n * map2->n, 0);
2096 for (i = 0; i < map1->n; ++i)
2097 for (j = 0; j < map2->n; ++j) {
2098 struct isl_basic_map *part;
2099 part = isl_basic_map_sum(
2100 isl_basic_map_copy(map1->p[i]),
2101 isl_basic_map_copy(map2->p[j]));
2102 if (isl_basic_map_is_empty(part))
2103 isl_basic_map_free(part);
2105 result = isl_map_add(result, part);
2118 /* Given a basic map A -> f(A), construct A -> -f(A).
2120 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2125 bmap = isl_basic_map_cow(bmap);
2129 n = isl_basic_map_dim(bmap, isl_dim_out);
2130 off = isl_basic_map_offset(bmap, isl_dim_out);
2131 for (i = 0; i < bmap->n_eq; ++i)
2132 for (j = 0; j < n; ++j)
2133 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2134 for (i = 0; i < bmap->n_ineq; ++i)
2135 for (j = 0; j < n; ++j)
2136 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2137 for (i = 0; i < bmap->n_div; ++i)
2138 for (j = 0; j < n; ++j)
2139 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2140 return isl_basic_map_finalize(bmap);
2143 /* Given a map A -> f(A), construct A -> -f(A).
2145 struct isl_map *isl_map_neg(struct isl_map *map)
2149 map = isl_map_cow(map);
2153 for (i = 0; i < map->n; ++i) {
2154 map->p[i] = isl_basic_map_neg(map->p[i]);
2165 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2166 * A -> floor(f(A)/d).
2168 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2171 unsigned n_in, n_out, nparam, total, pos;
2172 struct isl_basic_map *result = NULL;
2173 struct isl_dim_map *dim_map;
2179 nparam = isl_basic_map_n_param(bmap);
2180 n_in = isl_basic_map_n_in(bmap);
2181 n_out = isl_basic_map_n_out(bmap);
2183 total = nparam + n_in + n_out + bmap->n_div + n_out;
2184 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2185 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2186 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2187 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2188 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2190 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2191 bmap->n_div + n_out,
2192 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2193 result = add_constraints_dim_map(result, bmap, dim_map);
2194 result = add_divs(result, n_out);
2195 for (i = 0; i < n_out; ++i) {
2197 j = isl_basic_map_alloc_inequality(result);
2200 isl_seq_clr(result->ineq[j], 1+total);
2201 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2202 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2203 j = isl_basic_map_alloc_inequality(result);
2206 isl_seq_clr(result->ineq[j], 1+total);
2207 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2208 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2209 isl_int_sub_ui(result->ineq[j][0], d, 1);
2212 result = isl_basic_map_simplify(result);
2213 return isl_basic_map_finalize(result);
2215 isl_basic_map_free(result);
2219 /* Given a map A -> f(A) and an integer d, construct a map
2220 * A -> floor(f(A)/d).
2222 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2226 map = isl_map_cow(map);
2230 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2231 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2232 for (i = 0; i < map->n; ++i) {
2233 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2244 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2250 i = isl_basic_map_alloc_equality(bmap);
2253 nparam = isl_basic_map_n_param(bmap);
2254 n_in = isl_basic_map_n_in(bmap);
2255 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2256 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2257 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2258 return isl_basic_map_finalize(bmap);
2260 isl_basic_map_free(bmap);
2264 /* Add a constraints to "bmap" expressing i_pos < o_pos
2266 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2272 i = isl_basic_map_alloc_inequality(bmap);
2275 nparam = isl_basic_map_n_param(bmap);
2276 n_in = isl_basic_map_n_in(bmap);
2277 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2278 isl_int_set_si(bmap->ineq[i][0], -1);
2279 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2280 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2281 return isl_basic_map_finalize(bmap);
2283 isl_basic_map_free(bmap);
2287 /* Add a constraints to "bmap" expressing i_pos > o_pos
2289 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2295 i = isl_basic_map_alloc_inequality(bmap);
2298 nparam = isl_basic_map_n_param(bmap);
2299 n_in = isl_basic_map_n_in(bmap);
2300 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2301 isl_int_set_si(bmap->ineq[i][0], -1);
2302 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2303 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2304 return isl_basic_map_finalize(bmap);
2306 isl_basic_map_free(bmap);
2310 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2313 struct isl_basic_map *bmap;
2314 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2317 for (i = 0; i < n_equal && bmap; ++i)
2318 bmap = var_equal(bmap, i);
2319 return isl_basic_map_finalize(bmap);
2322 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2324 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2327 struct isl_basic_map *bmap;
2328 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2331 for (i = 0; i < pos && bmap; ++i)
2332 bmap = var_equal(bmap, i);
2334 bmap = var_less(bmap, pos);
2335 return isl_basic_map_finalize(bmap);
2338 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2340 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2343 struct isl_basic_map *bmap;
2344 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2347 for (i = 0; i < pos && bmap; ++i)
2348 bmap = var_equal(bmap, i);
2350 bmap = var_more(bmap, pos);
2351 return isl_basic_map_finalize(bmap);
2354 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
2356 struct isl_map *map;
2363 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2365 for (i = 0; i < dim; ++i)
2366 map = isl_map_add(map,
2367 isl_basic_map_less_at(isl_dim_copy(dims), i));
2369 map = isl_map_add(map,
2370 isl_basic_map_equal(isl_dim_copy(dims), dim));
2376 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
2378 return map_lex_lte(isl_dim_map(set_dim), 0);
2381 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
2383 return map_lex_lte(isl_dim_map(set_dim), 1);
2386 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
2388 struct isl_map *map;
2395 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2397 for (i = 0; i < dim; ++i)
2398 map = isl_map_add(map,
2399 isl_basic_map_more_at(isl_dim_copy(dims), i));
2401 map = isl_map_add(map,
2402 isl_basic_map_equal(isl_dim_copy(dims), dim));
2408 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
2410 return map_lex_gte(isl_dim_map(set_dim), 0);
2413 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
2415 return map_lex_gte(isl_dim_map(set_dim), 1);
2418 struct isl_basic_map *isl_basic_map_from_basic_set(
2419 struct isl_basic_set *bset, struct isl_dim *dim)
2421 struct isl_basic_map *bmap;
2423 bset = isl_basic_set_cow(bset);
2427 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2428 isl_dim_free(bset->dim);
2429 bmap = (struct isl_basic_map *) bset;
2431 return isl_basic_map_finalize(bmap);
2433 isl_basic_set_free(bset);
2438 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2442 if (bmap->dim->n_in == 0)
2443 return (struct isl_basic_set *)bmap;
2444 bmap = isl_basic_map_cow(bmap);
2447 bmap->dim = isl_dim_cow(bmap->dim);
2450 bmap->dim->n_out += bmap->dim->n_in;
2451 bmap->dim->n_in = 0;
2452 bmap = isl_basic_map_finalize(bmap);
2453 return (struct isl_basic_set *)bmap;
2455 isl_basic_map_free(bmap);
2459 /* For a div d = floor(f/m), add the constraints
2462 * -(f-(n-1)) + m d >= 0
2464 * Note that the second constraint is the negation of
2468 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2471 unsigned total = isl_basic_map_total_dim(bmap);
2472 unsigned div_pos = 1 + total - bmap->n_div + div;
2474 i = isl_basic_map_alloc_inequality(bmap);
2477 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2478 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2480 j = isl_basic_map_alloc_inequality(bmap);
2483 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2484 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2485 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2489 struct isl_basic_set *isl_basic_map_underlying_set(
2490 struct isl_basic_map *bmap)
2494 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2495 return (struct isl_basic_set *)bmap;
2496 bmap = isl_basic_map_cow(bmap);
2499 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2502 bmap->extra -= bmap->n_div;
2504 bmap = isl_basic_map_finalize(bmap);
2505 return (struct isl_basic_set *)bmap;
2510 struct isl_basic_map *isl_basic_map_overlying_set(
2511 struct isl_basic_set *bset, struct isl_basic_map *like)
2513 struct isl_basic_map *bmap;
2514 struct isl_ctx *ctx;
2521 isl_assert(ctx, bset->n_div == 0, goto error);
2522 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2523 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2525 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2526 isl_basic_map_free(like);
2527 return (struct isl_basic_map *)bset;
2529 bset = isl_basic_set_cow(bset);
2532 total = bset->dim->n_out + bset->extra;
2533 bmap = (struct isl_basic_map *)bset;
2534 isl_dim_free(bmap->dim);
2535 bmap->dim = isl_dim_copy(like->dim);
2538 bmap->n_div = like->n_div;
2539 bmap->extra += like->n_div;
2542 ltotal = total - bmap->extra + like->extra;
2545 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2546 bmap->extra * (1 + 1 + total));
2547 if (isl_blk_is_error(bmap->block2))
2549 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2553 for (i = 0; i < bmap->extra; ++i)
2554 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2555 for (i = 0; i < like->n_div; ++i) {
2556 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2557 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2559 bmap = isl_basic_map_extend_constraints(bmap,
2560 0, 2 * like->n_div);
2561 for (i = 0; i < like->n_div; ++i) {
2562 if (isl_int_is_zero(bmap->div[i][0]))
2564 if (add_div_constraints(bmap, i) < 0)
2568 isl_basic_map_free(like);
2569 bmap = isl_basic_map_simplify(bmap);
2570 bmap = isl_basic_map_finalize(bmap);
2573 isl_basic_map_free(like);
2574 isl_basic_set_free(bset);
2578 struct isl_basic_set *isl_basic_set_from_underlying_set(
2579 struct isl_basic_set *bset, struct isl_basic_set *like)
2581 return (struct isl_basic_set *)
2582 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2585 struct isl_set *isl_set_from_underlying_set(
2586 struct isl_set *set, struct isl_basic_set *like)
2592 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2594 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2595 isl_basic_set_free(like);
2598 set = isl_set_cow(set);
2601 for (i = 0; i < set->n; ++i) {
2602 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2603 isl_basic_set_copy(like));
2607 isl_dim_free(set->dim);
2608 set->dim = isl_dim_copy(like->dim);
2611 isl_basic_set_free(like);
2614 isl_basic_set_free(like);
2619 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2623 map = isl_map_cow(map);
2626 map->dim = isl_dim_cow(map->dim);
2630 for (i = 1; i < map->n; ++i)
2631 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2633 for (i = 0; i < map->n; ++i) {
2634 map->p[i] = (struct isl_basic_map *)
2635 isl_basic_map_underlying_set(map->p[i]);
2640 map->dim = isl_dim_underlying(map->dim, 0);
2642 isl_dim_free(map->dim);
2643 map->dim = isl_dim_copy(map->p[0]->dim);
2647 return (struct isl_set *)map;
2653 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2655 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2658 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2660 struct isl_basic_set *domain;
2665 n_in = isl_basic_map_n_in(bmap);
2666 n_out = isl_basic_map_n_out(bmap);
2667 domain = isl_basic_set_from_basic_map(bmap);
2668 return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
2671 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2673 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2676 struct isl_set *isl_map_range(struct isl_map *map)
2679 struct isl_set *set;
2683 map = isl_map_cow(map);
2687 set = (struct isl_set *) map;
2688 if (set->dim->n_in != 0) {
2689 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2693 for (i = 0; i < map->n; ++i) {
2694 set->p[i] = isl_basic_map_range(map->p[i]);
2698 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2699 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2706 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2709 struct isl_map *map = NULL;
2711 set = isl_set_cow(set);
2714 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2715 map = (struct isl_map *)set;
2716 for (i = 0; i < set->n; ++i) {
2717 map->p[i] = isl_basic_map_from_basic_set(
2718 set->p[i], isl_dim_copy(dim));
2722 isl_dim_free(map->dim);
2731 struct isl_map *isl_map_from_range(struct isl_set *set)
2733 return (struct isl_map *)set;
2736 struct isl_set *isl_set_from_map(struct isl_map *map)
2739 struct isl_set *set = NULL;
2743 map = isl_map_cow(map);
2746 map->dim = isl_dim_cow(map->dim);
2749 map->dim->n_out += map->dim->n_in;
2751 set = (struct isl_set *)map;
2752 for (i = 0; i < map->n; ++i) {
2753 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2763 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2765 struct isl_map *map;
2769 isl_assert(dim->ctx, n >= 0, return NULL);
2770 map = isl_alloc(dim->ctx, struct isl_map,
2771 sizeof(struct isl_map) +
2772 (n - 1) * sizeof(struct isl_basic_map *));
2776 map->ctx = dim->ctx;
2777 isl_ctx_ref(map->ctx);
2789 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2790 unsigned nparam, unsigned in, unsigned out, int n,
2793 struct isl_map *map;
2794 struct isl_dim *dims;
2796 dims = isl_dim_alloc(ctx, nparam, in, out);
2800 map = isl_map_alloc_dim(dims, n, flags);
2804 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
2806 struct isl_basic_map *bmap;
2807 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
2808 bmap = isl_basic_map_set_to_empty(bmap);
2812 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2814 struct isl_basic_set *bset;
2815 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2816 bset = isl_basic_set_set_to_empty(bset);
2820 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2822 struct isl_basic_map *bmap;
2825 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2826 bmap = isl_basic_map_set_to_empty(bmap);
2830 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2832 struct isl_basic_map *bmap;
2835 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2836 bmap = isl_basic_map_set_to_empty(bmap);
2840 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2842 struct isl_basic_set *bset;
2845 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2846 bset = isl_basic_set_set_to_empty(bset);
2850 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2852 struct isl_basic_map *bmap;
2853 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2857 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2859 struct isl_basic_set *bset;
2860 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2864 __isl_give isl_basic_map *isl_basic_map_universe_like(
2865 __isl_keep isl_basic_map *model)
2869 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2872 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2876 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2879 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
2880 __isl_keep isl_set *model)
2884 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2887 struct isl_map *isl_map_empty(struct isl_dim *dim)
2889 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2892 struct isl_map *isl_map_empty_like(struct isl_map *model)
2896 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2899 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2903 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2906 struct isl_set *isl_set_empty(struct isl_dim *dim)
2908 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2911 struct isl_set *isl_set_empty_like(struct isl_set *model)
2915 return isl_set_empty(isl_dim_copy(model->dim));
2918 struct isl_map *isl_map_universe(struct isl_dim *dim)
2920 struct isl_map *map;
2923 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2924 map = isl_map_add(map, isl_basic_map_universe(dim));
2928 struct isl_set *isl_set_universe(struct isl_dim *dim)
2930 struct isl_set *set;
2933 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2934 set = isl_set_add(set, isl_basic_set_universe(dim));
2938 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
2942 return isl_set_universe(isl_dim_copy(model->dim));
2945 struct isl_map *isl_map_dup(struct isl_map *map)
2948 struct isl_map *dup;
2952 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2953 for (i = 0; i < map->n; ++i)
2954 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2958 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2962 if (isl_basic_map_fast_is_empty(bmap)) {
2963 isl_basic_map_free(bmap);
2966 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2967 isl_assert(map->ctx, map->n < map->size, goto error);
2968 map->p[map->n] = bmap;
2970 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2976 isl_basic_map_free(bmap);
2980 void isl_map_free(struct isl_map *map)
2990 isl_ctx_deref(map->ctx);
2991 for (i = 0; i < map->n; ++i)
2992 isl_basic_map_free(map->p[i]);
2993 isl_dim_free(map->dim);
2997 struct isl_map *isl_map_extend(struct isl_map *base,
2998 unsigned nparam, unsigned n_in, unsigned n_out)
3002 base = isl_map_cow(base);
3006 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3009 for (i = 0; i < base->n; ++i) {
3010 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3011 isl_dim_copy(base->dim), 0, 0, 0);
3021 struct isl_set *isl_set_extend(struct isl_set *base,
3022 unsigned nparam, unsigned dim)
3024 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3028 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
3029 unsigned pos, int value)
3033 bmap = isl_basic_map_cow(bmap);
3034 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3035 j = isl_basic_map_alloc_equality(bmap);
3038 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3039 isl_int_set_si(bmap->eq[j][pos], -1);
3040 isl_int_set_si(bmap->eq[j][0], value);
3041 bmap = isl_basic_map_simplify(bmap);
3042 return isl_basic_map_finalize(bmap);
3044 isl_basic_map_free(bmap);
3048 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3049 enum isl_dim_type type, unsigned pos, int value)
3053 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3054 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
3057 isl_basic_map_free(bmap);
3061 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3062 enum isl_dim_type type, unsigned pos, int value)
3064 return (struct isl_basic_set *)
3065 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3069 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3070 unsigned input, int value)
3072 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3075 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3076 unsigned dim, int value)
3078 return (struct isl_basic_set *)
3079 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3080 isl_dim_set, dim, value);
3083 struct isl_map *isl_map_fix_si(struct isl_map *map,
3084 enum isl_dim_type type, unsigned pos, int value)
3088 map = isl_map_cow(map);
3092 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3093 for (i = 0; i < map->n; ++i) {
3094 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3098 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3105 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3106 unsigned input, int value)
3108 return isl_map_fix_si(map, isl_dim_in, input, value);
3111 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3113 return (struct isl_set *)
3114 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3117 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3118 unsigned dim, isl_int value)
3122 bset = isl_basic_set_cow(bset);
3123 bset = isl_basic_set_extend_constraints(bset, 0, 1);
3124 j = isl_basic_set_alloc_inequality(bset);
3127 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3128 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3129 isl_int_neg(bset->ineq[j][0], value);
3130 bset = isl_basic_set_simplify(bset);
3131 return isl_basic_set_finalize(bset);
3133 isl_basic_set_free(bset);
3137 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3142 set = isl_set_cow(set);
3146 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3147 for (i = 0; i < set->n; ++i) {
3148 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3158 struct isl_map *isl_map_reverse(struct isl_map *map)
3162 map = isl_map_cow(map);
3166 map->dim = isl_dim_reverse(map->dim);
3169 for (i = 0; i < map->n; ++i) {
3170 map->p[i] = isl_basic_map_reverse(map->p[i]);
3174 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3181 static struct isl_map *isl_basic_map_partial_lexopt(
3182 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3183 struct isl_set **empty, int max)
3187 if (bmap->ctx->pip == ISL_PIP_PIP)
3188 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3190 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3192 isl_basic_map_free(bmap);
3193 isl_basic_set_free(dom);
3199 struct isl_map *isl_basic_map_partial_lexmax(
3200 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3201 struct isl_set **empty)
3203 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
3206 struct isl_map *isl_basic_map_partial_lexmin(
3207 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3208 struct isl_set **empty)
3210 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3213 struct isl_set *isl_basic_set_partial_lexmin(
3214 struct isl_basic_set *bset, struct isl_basic_set *dom,
3215 struct isl_set **empty)
3217 return (struct isl_set *)
3218 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3222 struct isl_set *isl_basic_set_partial_lexmax(
3223 struct isl_basic_set *bset, struct isl_basic_set *dom,
3224 struct isl_set **empty)
3226 return (struct isl_set *)
3227 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3231 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
3233 struct isl_basic_set *dom = NULL;
3234 struct isl_map *min;
3235 struct isl_dim *dom_dim;
3239 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3240 dom = isl_basic_set_universe(dom_dim);
3241 return isl_basic_map_partial_lexmin(bmap, dom, NULL);
3243 isl_basic_map_free(bmap);
3247 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
3249 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
3252 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
3253 struct isl_dim *dim)
3260 for (i = 0; i < map->n; ++i) {
3261 isl_dim_free(map->p[i]->dim);
3262 map->p[i]->dim = isl_dim_copy(dim);
3264 isl_dim_free(map->dim);
3274 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
3275 struct isl_dim *dim)
3277 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3280 /* Apply a preimage specified by "mat" on the parameters of "bset".
3281 * bset is assumed to have only parameters and divs.
3283 static struct isl_basic_set *basic_set_parameter_preimage(
3284 struct isl_basic_set *bset, struct isl_mat *mat)
3291 bset->dim = isl_dim_cow(bset->dim);
3295 nparam = isl_basic_set_dim(bset, isl_dim_param);
3297 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
3299 bset->dim->nparam = 0;
3300 bset->dim->n_out = nparam;
3301 bset = isl_basic_set_preimage(bset, mat);
3303 bset->dim->nparam = bset->dim->n_out;
3304 bset->dim->n_out = 0;
3309 isl_basic_set_free(bset);
3313 /* Apply a preimage specified by "mat" on the parameters of "set".
3314 * set is assumed to have only parameters and divs.
3316 static struct isl_set *set_parameter_preimage(
3317 struct isl_set *set, struct isl_mat *mat)
3319 struct isl_dim *dim = NULL;
3325 dim = isl_dim_copy(set->dim);
3326 dim = isl_dim_cow(dim);
3330 nparam = isl_set_dim(set, isl_dim_param);
3332 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
3335 dim->n_out = nparam;
3336 isl_set_reset_dim(set, dim);
3337 set = isl_set_preimage(set, mat);
3340 dim = isl_dim_copy(set->dim);
3341 dim = isl_dim_cow(dim);
3344 dim->nparam = dim->n_out;
3346 isl_set_reset_dim(set, dim);
3356 /* Intersect the basic set "bset" with the affine space specified by the
3357 * equalities in "eq".
3359 static struct isl_basic_set *basic_set_append_equalities(
3360 struct isl_basic_set *bset, struct isl_mat *eq)
3368 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
3373 len = 1 + isl_dim_total(bset->dim) + bset->extra;
3374 for (i = 0; i < eq->n_row; ++i) {
3375 k = isl_basic_set_alloc_equality(bset);
3378 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
3379 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
3386 isl_basic_set_free(bset);
3390 /* Intersect the set "set" with the affine space specified by the
3391 * equalities in "eq".
3393 static struct isl_set *set_append_equalities(struct isl_set *set,
3401 for (i = 0; i < set->n; ++i) {
3402 set->p[i] = basic_set_append_equalities(set->p[i],
3415 /* Project the given basic set onto its parameter domain, possibly introducing
3416 * new, explicit, existential variables in the constraints.
3417 * The input has parameters and (possibly implicit) existential variables.
3418 * The output has the same parameters, but only
3419 * explicit existentially quantified variables.
3421 * The actual projection is performed by pip, but pip doesn't seem
3422 * to like equalities very much, so we first remove the equalities
3423 * among the parameters by performing a variable compression on
3424 * the parameters. Afterward, an inverse transformation is performed
3425 * and the equalities among the parameters are inserted back in.
3427 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
3431 struct isl_mat *T, *T2;
3432 struct isl_set *set;
3433 unsigned nparam, n_div;
3435 bset = isl_basic_set_cow(bset);
3439 if (bset->n_eq == 0)
3440 return isl_basic_set_lexmin(bset);
3442 isl_basic_set_gauss(bset, NULL);
3444 nparam = isl_basic_set_dim(bset, isl_dim_param);
3445 n_div = isl_basic_set_dim(bset, isl_dim_div);
3447 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
3448 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
3451 if (i == bset->n_eq)
3452 return isl_basic_set_lexmin(bset);
3454 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
3456 eq = isl_mat_cow(eq);
3457 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
3458 bset = basic_set_parameter_preimage(bset, T);
3460 set = isl_basic_set_lexmin(bset);
3461 set = set_parameter_preimage(set, T2);
3462 set = set_append_equalities(set, eq);
3466 /* Compute an explicit representation for all the existentially
3467 * quantified variables.
3468 * The input and output dimensions are first turned into parameters.
3469 * compute_divs then returns a map with the same parameters and
3470 * no input or output dimensions and the dimension specification
3471 * is reset to that of the input.
3473 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
3475 struct isl_basic_set *bset;
3476 struct isl_set *set;
3477 struct isl_map *map;
3478 struct isl_dim *dim, *orig_dim = NULL;
3483 bmap = isl_basic_map_cow(bmap);
3487 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3488 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3489 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3490 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
3494 orig_dim = bmap->dim;
3496 bset = (struct isl_basic_set *)bmap;
3498 set = parameter_compute_divs(bset);
3499 map = (struct isl_map *)set;
3500 map = isl_map_reset_dim(map, orig_dim);
3504 isl_basic_map_free(bmap);
3508 /* If bmap contains any unknown divs, then compute explicit
3509 * expressions for them. However, this computation may be
3510 * quite expensive, so first try to remove divs that aren't
3513 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
3520 off = isl_dim_total(bmap->dim);
3521 for (i = 0; i < bmap->n_div; ++i) {
3522 if (isl_int_is_zero(bmap->div[i][0]))
3524 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
3527 if (i == bmap->n_div)
3528 return isl_map_from_basic_map(bmap);
3529 bmap = isl_basic_map_drop_redundant_divs(bmap);
3532 for (i = 0; i < bmap->n_div; ++i)
3533 if (isl_int_is_zero(bmap->div[i][0]))
3535 if (i == bmap->n_div)
3536 return isl_map_from_basic_map(bmap);
3537 struct isl_map *map = compute_divs(bmap);
3540 isl_basic_map_free(bmap);
3544 struct isl_map *isl_map_compute_divs(struct isl_map *map)
3547 struct isl_map *res;
3553 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
3554 for (i = 1 ; i < map->n; ++i) {
3556 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
3557 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3558 res = isl_map_union_disjoint(res, r2);
3560 res = isl_map_union(res, r2);
3567 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3569 return (struct isl_set *)
3570 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3573 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3575 return (struct isl_set *)
3576 isl_map_compute_divs((struct isl_map *)set);
3579 struct isl_set *isl_map_domain(struct isl_map *map)
3582 struct isl_set *set;
3587 map = isl_map_cow(map);
3591 set = (struct isl_set *)map;
3592 set->dim = isl_dim_domain(set->dim);
3595 for (i = 0; i < map->n; ++i) {
3596 set->p[i] = isl_basic_map_domain(map->p[i]);
3600 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3601 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3608 struct isl_map *isl_map_union_disjoint(
3609 struct isl_map *map1, struct isl_map *map2)
3613 struct isl_map *map = NULL;
3627 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3629 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3630 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3631 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3633 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3634 map1->n + map2->n, flags);
3637 for (i = 0; i < map1->n; ++i) {
3638 map = isl_map_add(map,
3639 isl_basic_map_copy(map1->p[i]));
3643 for (i = 0; i < map2->n; ++i) {
3644 map = isl_map_add(map,
3645 isl_basic_map_copy(map2->p[i]));
3659 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3661 map1 = isl_map_union_disjoint(map1, map2);
3665 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3669 struct isl_set *isl_set_union_disjoint(
3670 struct isl_set *set1, struct isl_set *set2)
3672 return (struct isl_set *)
3673 isl_map_union_disjoint(
3674 (struct isl_map *)set1, (struct isl_map *)set2);
3677 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3679 return (struct isl_set *)
3680 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3683 struct isl_map *isl_map_intersect_range(
3684 struct isl_map *map, struct isl_set *set)
3687 struct isl_map *result;
3693 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3694 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3695 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3697 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3698 map->n * set->n, flags);
3701 for (i = 0; i < map->n; ++i)
3702 for (j = 0; j < set->n; ++j) {
3703 result = isl_map_add(result,
3704 isl_basic_map_intersect_range(
3705 isl_basic_map_copy(map->p[i]),
3706 isl_basic_set_copy(set->p[j])));
3719 struct isl_map *isl_map_intersect_domain(
3720 struct isl_map *map, struct isl_set *set)
3722 return isl_map_reverse(
3723 isl_map_intersect_range(isl_map_reverse(map), set));
3726 struct isl_map *isl_map_apply_domain(
3727 struct isl_map *map1, struct isl_map *map2)
3731 map1 = isl_map_reverse(map1);
3732 map1 = isl_map_apply_range(map1, map2);
3733 return isl_map_reverse(map1);
3740 struct isl_map *isl_map_apply_range(
3741 struct isl_map *map1, struct isl_map *map2)
3743 struct isl_dim *dim_result;
3744 struct isl_map *result;
3750 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3751 isl_dim_copy(map2->dim));
3753 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3756 for (i = 0; i < map1->n; ++i)
3757 for (j = 0; j < map2->n; ++j) {
3758 result = isl_map_add(result,
3759 isl_basic_map_apply_range(
3760 isl_basic_map_copy(map1->p[i]),
3761 isl_basic_map_copy(map2->p[j])));
3767 if (result && result->n <= 1)
3768 ISL_F_SET(result, ISL_MAP_DISJOINT);
3777 * returns range - domain
3779 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3781 struct isl_basic_set *bset;
3788 dim = isl_basic_map_n_in(bmap);
3789 nparam = isl_basic_map_n_param(bmap);
3790 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3791 bset = isl_basic_set_from_basic_map(bmap);
3792 bset = isl_basic_set_cow(bset);
3793 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3794 bset = isl_basic_set_swap_vars(bset, 2*dim);
3795 for (i = 0; i < dim; ++i) {
3796 int j = isl_basic_map_alloc_equality(
3797 (struct isl_basic_map *)bset);
3800 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3801 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3802 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3803 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3805 return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
3807 isl_basic_map_free(bmap);
3812 * returns range - domain
3814 struct isl_set *isl_map_deltas(struct isl_map *map)
3817 struct isl_set *result;
3822 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3823 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3824 isl_map_n_in(map), map->n, map->flags);
3827 for (i = 0; i < map->n; ++i)
3828 result = isl_set_add(result,
3829 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3837 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3839 struct isl_basic_map *bmap;
3847 nparam = dims->nparam;
3849 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3853 for (i = 0; i < dim; ++i) {
3854 int j = isl_basic_map_alloc_equality(bmap);
3857 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3858 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3859 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3861 return isl_basic_map_finalize(bmap);
3863 isl_basic_map_free(bmap);
3867 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3869 struct isl_dim *dim = isl_dim_map(set_dim);
3872 return basic_map_identity(dim);
3875 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3877 if (!model || !model->dim)
3879 isl_assert(model->ctx,
3880 model->dim->n_in == model->dim->n_out, return NULL);
3881 return basic_map_identity(isl_dim_copy(model->dim));
3884 static struct isl_map *map_identity(struct isl_dim *dim)
3886 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3887 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3890 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3892 struct isl_dim *dim = isl_dim_map(set_dim);
3895 return map_identity(dim);
3898 struct isl_map *isl_map_identity_like(struct isl_map *model)
3900 if (!model || !model->dim)
3902 isl_assert(model->ctx,
3903 model->dim->n_in == model->dim->n_out, return NULL);
3904 return map_identity(isl_dim_copy(model->dim));
3907 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
3909 if (!model || !model->dim)
3911 isl_assert(model->ctx,
3912 model->dim->n_in == model->dim->n_out, return NULL);
3913 return map_identity(isl_dim_copy(model->dim));
3916 /* Construct a basic set with all set dimensions having only non-negative
3919 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
3924 struct isl_basic_set *bset;
3928 nparam = dims->nparam;
3930 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
3933 for (i = 0; i < dim; ++i) {
3934 int k = isl_basic_set_alloc_inequality(bset);
3937 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
3938 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
3942 isl_basic_set_free(bset);
3946 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3948 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3951 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3953 return isl_map_is_subset(
3954 (struct isl_map *)set1, (struct isl_map *)set2);
3957 int isl_basic_map_is_subset(
3958 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3961 struct isl_map *map1;
3962 struct isl_map *map2;
3964 if (!bmap1 || !bmap2)
3967 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3968 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3970 is_subset = isl_map_is_subset(map1, map2);
3978 int isl_basic_map_is_equal(
3979 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3983 if (!bmap1 || !bmap2)
3985 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3988 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3992 int isl_basic_set_is_equal(
3993 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3995 return isl_basic_map_is_equal(
3996 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3999 int isl_map_is_empty(struct isl_map *map)
4006 for (i = 0; i < map->n; ++i) {
4007 is_empty = isl_basic_map_is_empty(map->p[i]);
4016 int isl_map_fast_is_empty(struct isl_map *map)
4021 int isl_set_is_empty(struct isl_set *set)
4023 return isl_map_is_empty((struct isl_map *)set);
4026 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
4029 struct isl_map *diff;
4034 if (isl_map_is_empty(map1))
4037 if (isl_map_is_empty(map2))
4040 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
4044 is_subset = isl_map_is_empty(diff);
4050 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
4056 is_subset = isl_map_is_subset(map1, map2);
4059 is_subset = isl_map_is_subset(map2, map1);
4063 int isl_basic_map_is_strict_subset(
4064 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4068 if (!bmap1 || !bmap2)
4070 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4073 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4074 if (is_subset == -1)
4079 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
4085 is_subset = isl_map_is_subset(map1, map2);
4088 is_subset = isl_map_is_subset(map2, map1);
4089 if (is_subset == -1)
4094 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4096 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
4099 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
4103 return bmap->n_eq == 0 && bmap->n_ineq == 0;
4106 int isl_basic_set_is_universe(struct isl_basic_set *bset)
4110 return bset->n_eq == 0 && bset->n_ineq == 0;
4113 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
4115 struct isl_basic_set *bset = NULL;
4116 struct isl_vec *sample = NULL;
4123 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4126 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
4127 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
4128 copy = isl_basic_map_convex_hull(copy);
4129 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
4130 isl_basic_map_free(copy);
4134 total = 1 + isl_basic_map_total_dim(bmap);
4135 if (bmap->sample && bmap->sample->size == total) {
4136 int contains = basic_map_contains(bmap, bmap->sample);
4142 isl_vec_free(bmap->sample);
4143 bmap->sample = NULL;
4144 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
4147 sample = isl_basic_set_sample_vec(bset);
4150 empty = sample->size == 0;
4151 isl_vec_free(bmap->sample);
4152 bmap->sample = sample;
4154 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
4159 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
4163 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
4166 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
4170 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
4173 int isl_basic_set_is_empty(struct isl_basic_set *bset)
4175 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
4178 struct isl_map *isl_basic_map_union(
4179 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4181 struct isl_map *map;
4182 if (!bmap1 || !bmap2)
4185 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
4187 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
4190 map = isl_map_add(map, bmap1);
4191 map = isl_map_add(map, bmap2);
4194 isl_basic_map_free(bmap1);
4195 isl_basic_map_free(bmap2);
4199 struct isl_set *isl_basic_set_union(
4200 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4202 return (struct isl_set *)isl_basic_map_union(
4203 (struct isl_basic_map *)bset1,
4204 (struct isl_basic_map *)bset2);
4207 /* Order divs such that any div only depends on previous divs */
4208 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
4211 unsigned off = isl_dim_total(bmap->dim);
4213 for (i = 0; i < bmap->n_div; ++i) {
4215 if (isl_int_is_zero(bmap->div[i][0]))
4217 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
4221 isl_basic_map_swap_div(bmap, i, i + pos);
4227 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
4229 return (struct isl_basic_set *)
4230 isl_basic_map_order_divs((struct isl_basic_map *)bset);
4233 /* Look for a div in dst that corresponds to the div "div" in src.
4234 * The divs before "div" in src and dst are assumed to be the same.
4236 * Returns -1 if no corresponding div was found and the position
4237 * of the corresponding div in dst otherwise.
4239 static int find_div(struct isl_basic_map *dst,
4240 struct isl_basic_map *src, unsigned div)
4244 unsigned total = isl_dim_total(src->dim);
4246 isl_assert(dst->ctx, div <= dst->n_div, return -1);
4247 for (i = div; i < dst->n_div; ++i)
4248 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
4249 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
4250 dst->n_div - div) == -1)
4255 struct isl_basic_map *isl_basic_map_align_divs(
4256 struct isl_basic_map *dst, struct isl_basic_map *src)
4259 unsigned total = isl_dim_total(src->dim);
4264 if (src->n_div == 0)
4267 for (i = 0; i < src->n_div; ++i)
4268 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
4270 src = isl_basic_map_order_divs(src);
4271 dst = isl_basic_map_cow(dst);
4272 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
4273 src->n_div, 0, 2 * src->n_div);
4276 for (i = 0; i < src->n_div; ++i) {
4277 int j = find_div(dst, src, i);
4279 j = isl_basic_map_alloc_div(dst);
4282 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
4283 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
4284 if (add_div_constraints(dst, j) < 0)
4288 isl_basic_map_swap_div(dst, i, j);
4292 isl_basic_map_free(dst);
4296 struct isl_basic_set *isl_basic_set_align_divs(
4297 struct isl_basic_set *dst, struct isl_basic_set *src)
4299 return (struct isl_basic_set *)isl_basic_map_align_divs(
4300 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
4303 struct isl_map *isl_map_align_divs(struct isl_map *map)
4311 map = isl_map_compute_divs(map);
4312 map = isl_map_cow(map);
4316 for (i = 1; i < map->n; ++i)
4317 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
4318 for (i = 1; i < map->n; ++i)
4319 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
4321 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4325 struct isl_set *isl_set_align_divs(struct isl_set *set)
4327 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
4330 static struct isl_map *add_cut_constraint(struct isl_map *dst,
4331 struct isl_basic_map *src, isl_int *c,
4332 unsigned len, int oppose)
4334 struct isl_basic_map *copy = NULL;
4339 copy = isl_basic_map_copy(src);
4340 copy = isl_basic_map_cow(copy);
4343 copy = isl_basic_map_extend_constraints(copy, 0, 1);
4344 k = isl_basic_map_alloc_inequality(copy);
4348 isl_seq_neg(copy->ineq[k], c, len);
4350 isl_seq_cpy(copy->ineq[k], c, len);
4351 total = 1 + isl_basic_map_total_dim(copy);
4352 isl_seq_clr(copy->ineq[k]+len, total - len);
4353 isl_inequality_negate(copy, k);
4354 copy = isl_basic_map_simplify(copy);
4355 copy = isl_basic_map_finalize(copy);
4356 is_empty = isl_basic_map_is_empty(copy);
4360 dst = isl_map_add(dst, copy);
4362 isl_basic_map_free(copy);
4365 isl_basic_map_free(copy);
4370 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
4374 struct isl_map *rest = NULL;
4376 unsigned total = isl_basic_map_total_dim(bmap);
4383 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4384 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4386 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
4387 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
4391 for (i = 0; i < map->n; ++i) {
4392 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
4397 for (j = 0; j < map->n; ++j)
4398 map->p[j] = isl_basic_map_cow(map->p[j]);
4400 for (i = 0; i < bmap->n_eq; ++i) {
4401 for (j = 0; j < map->n; ++j) {
4402 rest = add_cut_constraint(rest,
4403 map->p[j], bmap->eq[i], 1+total, 0);
4407 rest = add_cut_constraint(rest,
4408 map->p[j], bmap->eq[i], 1+total, 1);
4412 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4416 k = isl_basic_map_alloc_equality(map->p[j]);
4419 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
4420 isl_seq_clr(map->p[j]->eq[k]+1+total,
4421 map->p[j]->n_div - bmap->n_div);
4425 for (i = 0; i < bmap->n_ineq; ++i) {
4426 for (j = 0; j < map->n; ++j) {
4427 rest = add_cut_constraint(rest,
4428 map->p[j], bmap->ineq[i], 1+total, 0);
4432 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4436 k = isl_basic_map_alloc_inequality(map->p[j]);
4439 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
4440 isl_seq_clr(map->p[j]->ineq[k]+1+total,
4441 map->p[j]->n_div - bmap->n_div);
4453 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
4459 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4461 if (isl_map_is_empty(map2)) {
4466 map1 = isl_map_compute_divs(map1);
4467 map2 = isl_map_compute_divs(map2);
4471 for (i = 0; map1 && i < map2->n; ++i)
4472 map1 = subtract(map1, map2->p[i]);
4482 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
4484 return (struct isl_set *)
4486 (struct isl_map *)set1, (struct isl_map *)set2);
4489 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
4493 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
4494 map = isl_map_intersect_domain(map, set);
4495 set = isl_map_range(map);
4503 /* There is no need to cow as removing empty parts doesn't change
4504 * the meaning of the set.
4506 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
4513 for (i = map->n-1; i >= 0; --i) {
4514 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
4516 isl_basic_map_free(map->p[i]);
4517 if (i != map->n-1) {
4518 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4519 map->p[i] = map->p[map->n-1];
4527 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
4529 return (struct isl_set *)
4530 isl_map_remove_empty_parts((struct isl_map *)set);
4533 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
4535 struct isl_basic_map *bmap;
4536 if (!map || map->n == 0)
4538 bmap = map->p[map->n-1];
4539 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
4540 return isl_basic_map_copy(bmap);
4543 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
4545 return (struct isl_basic_set *)
4546 isl_map_copy_basic_map((struct isl_map *)set);
4549 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
4550 struct isl_basic_map *bmap)
4556 for (i = map->n-1; i >= 0; --i) {
4557 if (map->p[i] != bmap)
4559 map = isl_map_cow(map);
4562 isl_basic_map_free(map->p[i]);
4563 if (i != map->n-1) {
4564 ISL_F_CLR(map, ISL_SET_NORMALIZED);
4565 map->p[i] = map->p[map->n-1];
4570 isl_basic_map_free(bmap);
4574 isl_basic_map_free(bmap);
4578 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4579 struct isl_basic_set *bset)
4581 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
4582 (struct isl_basic_map *)bset);
4585 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4586 * for any common value of the parameters and dimensions preceding dim
4587 * in both basic sets, the values of dimension pos in bset1 are
4588 * smaller or larger than those in bset2.
4591 * 1 if bset1 follows bset2
4592 * -1 if bset1 precedes bset2
4593 * 0 if bset1 and bset2 are incomparable
4594 * -2 if some error occurred.
4596 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4597 struct isl_basic_set *bset2, int pos)
4599 struct isl_dim *dims;
4600 struct isl_basic_map *bmap1 = NULL;
4601 struct isl_basic_map *bmap2 = NULL;
4602 struct isl_ctx *ctx;
4603 struct isl_vec *obj;
4606 unsigned dim1, dim2;
4608 enum isl_lp_result res;
4611 if (!bset1 || !bset2)
4614 nparam = isl_basic_set_n_param(bset1);
4615 dim1 = isl_basic_set_n_dim(bset1);
4616 dim2 = isl_basic_set_n_dim(bset2);
4617 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
4618 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
4619 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
4620 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
4621 if (!bmap1 || !bmap2)
4623 bmap1 = isl_basic_map_cow(bmap1);
4624 bmap1 = isl_basic_map_extend(bmap1, nparam,
4625 pos, (dim1 - pos) + (dim2 - pos),
4626 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4627 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
4630 total = isl_basic_map_total_dim(bmap1);
4632 obj = isl_vec_alloc(ctx, 1 + total);
4633 isl_seq_clr(obj->block.data, 1 + total);
4634 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
4635 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
4640 res = isl_basic_map_solve_lp(bmap1, 0, obj->block.data, ctx->one,
4642 if (res == isl_lp_empty)
4644 else if (res == isl_lp_ok && isl_int_is_pos(num))
4646 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4647 res == isl_lp_unbounded)
4653 isl_basic_map_free(bmap1);
4657 isl_basic_map_free(bmap1);
4658 isl_basic_map_free(bmap2);
4662 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4663 unsigned pos, isl_int *val)
4671 total = isl_basic_map_total_dim(bmap);
4672 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4673 for (; d+1 > pos; --d)
4674 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4678 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4680 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4682 if (!isl_int_is_one(bmap->eq[i][1+d]))
4685 isl_int_neg(*val, bmap->eq[i][0]);
4691 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4692 unsigned pos, isl_int *val)
4704 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4707 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4708 for (i = 1; fixed == 1 && i < map->n; ++i) {
4709 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4710 if (fixed == 1 && isl_int_ne(tmp, v))
4714 isl_int_set(*val, v);
4720 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
4721 unsigned pos, isl_int *val)
4723 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
4727 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4730 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4733 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4734 enum isl_dim_type type, unsigned pos, isl_int *val)
4736 if (pos >= isl_basic_map_dim(bmap, type))
4738 return isl_basic_map_fast_has_fixed_var(bmap,
4739 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4742 int isl_map_fast_is_fixed(struct isl_map *map,
4743 enum isl_dim_type type, unsigned pos, isl_int *val)
4745 if (pos >= isl_map_dim(map, type))
4747 return isl_map_fast_has_fixed_var(map,
4748 map_offset(map, type) - 1 + pos, val);
4751 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4752 * then return this fixed value in *val.
4754 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
4757 return isl_basic_set_fast_has_fixed_var(bset,
4758 isl_basic_set_n_param(bset) + dim, val);
4761 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4762 * then return this fixed value in *val.
4764 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4766 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4769 /* Check if input variable in has fixed value and if so and if val is not NULL,
4770 * then return this fixed value in *val.
4772 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4774 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4777 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4778 * and if val is not NULL, then return this lower bound in *val.
4780 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4781 unsigned dim, isl_int *val)
4783 int i, i_eq = -1, i_ineq = -1;
4790 total = isl_basic_set_total_dim(bset);
4791 nparam = isl_basic_set_n_param(bset);
4792 for (i = 0; i < bset->n_eq; ++i) {
4793 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4799 for (i = 0; i < bset->n_ineq; ++i) {
4800 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4802 if (i_eq != -1 || i_ineq != -1)
4806 if (i_eq == -1 && i_ineq == -1)
4808 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4809 /* The coefficient should always be one due to normalization. */
4810 if (!isl_int_is_one(c[1+nparam+dim]))
4812 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4814 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4815 total - nparam - dim - 1) != -1)
4818 isl_int_neg(*val, c[0]);
4822 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4823 unsigned dim, isl_int *val)
4835 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4839 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4841 for (i = 1; fixed == 1 && i < set->n; ++i) {
4842 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4844 if (fixed == 1 && isl_int_ne(tmp, v))
4848 isl_int_set(*val, v);
4859 static int qsort_constraint_cmp(const void *p1, const void *p2)
4861 const struct constraint *c1 = (const struct constraint *)p1;
4862 const struct constraint *c2 = (const struct constraint *)p2;
4863 unsigned size = isl_min(c1->size, c2->size);
4864 return isl_seq_cmp(c1->c, c2->c, size);
4867 static struct isl_basic_map *isl_basic_map_sort_constraints(
4868 struct isl_basic_map *bmap)
4871 struct constraint *c;
4876 total = isl_basic_map_total_dim(bmap);
4877 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4880 for (i = 0; i < bmap->n_ineq; ++i) {
4882 c[i].c = bmap->ineq[i];
4884 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4885 for (i = 0; i < bmap->n_ineq; ++i)
4886 bmap->ineq[i] = c[i].c;
4890 isl_basic_map_free(bmap);
4894 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4898 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4900 bmap = isl_basic_map_convex_hull(bmap);
4901 bmap = isl_basic_map_sort_constraints(bmap);
4902 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4906 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4908 return (struct isl_basic_set *)isl_basic_map_normalize(
4909 (struct isl_basic_map *)bset);
4912 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4913 const struct isl_basic_map *bmap2)
4920 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4921 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4922 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4923 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4924 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4925 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4926 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4927 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4929 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4931 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4933 if (bmap1->n_eq != bmap2->n_eq)
4934 return bmap1->n_eq - bmap2->n_eq;
4935 if (bmap1->n_ineq != bmap2->n_ineq)
4936 return bmap1->n_ineq - bmap2->n_ineq;
4937 if (bmap1->n_div != bmap2->n_div)
4938 return bmap1->n_div - bmap2->n_div;
4939 total = isl_basic_map_total_dim(bmap1);
4940 for (i = 0; i < bmap1->n_eq; ++i) {
4941 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4945 for (i = 0; i < bmap1->n_ineq; ++i) {
4946 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4950 for (i = 0; i < bmap1->n_div; ++i) {
4951 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4958 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4959 struct isl_basic_map *bmap2)
4961 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4964 static int qsort_bmap_cmp(const void *p1, const void *p2)
4966 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4967 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4969 return isl_basic_map_fast_cmp(bmap1, bmap2);
4972 /* We normalize in place, but if anything goes wrong we need
4973 * to return NULL, so we need to make sure we don't change the
4974 * meaning of any possible other copies of map.
4976 struct isl_map *isl_map_normalize(struct isl_map *map)
4979 struct isl_basic_map *bmap;
4983 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4985 for (i = 0; i < map->n; ++i) {
4986 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4989 isl_basic_map_free(map->p[i]);
4992 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4993 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4994 map = isl_map_remove_empty_parts(map);
4997 for (i = map->n - 1; i >= 1; --i) {
4998 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
5000 isl_basic_map_free(map->p[i-1]);
5001 for (j = i; j < map->n; ++j)
5002 map->p[j-1] = map->p[j];
5012 struct isl_set *isl_set_normalize(struct isl_set *set)
5014 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
5017 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
5027 if (!isl_dim_equal(map1->dim, map2->dim))
5030 map1 = isl_map_copy(map1);
5031 map2 = isl_map_copy(map2);
5032 map1 = isl_map_normalize(map1);
5033 map2 = isl_map_normalize(map2);
5036 equal = map1->n == map2->n;
5037 for (i = 0; equal && i < map1->n; ++i) {
5038 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
5051 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
5053 return isl_map_fast_is_equal((struct isl_map *)set1,
5054 (struct isl_map *)set2);
5057 /* Return an interval that ranges from min to max (inclusive)
5059 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
5060 isl_int min, isl_int max)
5063 struct isl_basic_set *bset = NULL;
5065 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
5069 k = isl_basic_set_alloc_inequality(bset);
5072 isl_int_set_si(bset->ineq[k][1], 1);
5073 isl_int_neg(bset->ineq[k][0], min);
5075 k = isl_basic_set_alloc_inequality(bset);
5078 isl_int_set_si(bset->ineq[k][1], -1);
5079 isl_int_set(bset->ineq[k][0], max);
5083 isl_basic_set_free(bset);
5087 /* Return the Cartesian product of the basic sets in list (in the given order).
5089 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
5097 struct isl_basic_set *product = NULL;
5101 isl_assert(list->ctx, list->n > 0, goto error);
5102 isl_assert(list->ctx, list->p[0], goto error);
5103 nparam = isl_basic_set_n_param(list->p[0]);
5104 dim = isl_basic_set_n_dim(list->p[0]);
5105 extra = list->p[0]->n_div;
5106 n_eq = list->p[0]->n_eq;
5107 n_ineq = list->p[0]->n_ineq;
5108 for (i = 1; i < list->n; ++i) {
5109 isl_assert(list->ctx, list->p[i], goto error);
5110 isl_assert(list->ctx,
5111 nparam == isl_basic_set_n_param(list->p[i]), goto error);
5112 dim += isl_basic_set_n_dim(list->p[i]);
5113 extra += list->p[i]->n_div;
5114 n_eq += list->p[i]->n_eq;
5115 n_ineq += list->p[i]->n_ineq;
5117 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
5122 for (i = 0; i < list->n; ++i) {
5123 isl_basic_set_add_constraints(product,
5124 isl_basic_set_copy(list->p[i]), dim);
5125 dim += isl_basic_set_n_dim(list->p[i]);
5127 isl_basic_set_list_free(list);
5130 isl_basic_set_free(product);
5131 isl_basic_set_list_free(list);
5135 struct isl_basic_map *isl_basic_map_product(
5136 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5138 struct isl_dim *dim_result = NULL;
5139 struct isl_basic_map *bmap;
5140 unsigned in1, in2, out1, out2, nparam, total, pos;
5141 struct isl_dim_map *dim_map1, *dim_map2;
5143 if (!bmap1 || !bmap2)
5146 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
5147 bmap2->dim, isl_dim_param), goto error);
5148 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
5149 isl_dim_copy(bmap2->dim));
5151 in1 = isl_basic_map_n_in(bmap1);
5152 in2 = isl_basic_map_n_in(bmap2);
5153 out1 = isl_basic_map_n_out(bmap1);
5154 out2 = isl_basic_map_n_out(bmap2);
5155 nparam = isl_basic_map_n_param(bmap1);
5157 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
5158 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5159 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
5160 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5161 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
5162 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5163 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
5164 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
5165 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
5166 isl_dim_map_div(dim_map1, bmap1, pos += out2);
5167 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5169 bmap = isl_basic_map_alloc_dim(dim_result,
5170 bmap1->n_div + bmap2->n_div,
5171 bmap1->n_eq + bmap2->n_eq,
5172 bmap1->n_ineq + bmap2->n_ineq);
5173 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
5174 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
5175 bmap = isl_basic_map_simplify(bmap);
5176 return isl_basic_map_finalize(bmap);
5178 isl_basic_map_free(bmap1);
5179 isl_basic_map_free(bmap2);
5183 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5185 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
5188 struct isl_map *result;
5194 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
5195 map2->dim, isl_dim_param), goto error);
5197 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5198 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5199 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5201 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
5202 isl_dim_copy(map2->dim)),
5203 map1->n * map2->n, flags);
5206 for (i = 0; i < map1->n; ++i)
5207 for (j = 0; j < map2->n; ++j) {
5208 struct isl_basic_map *part;
5209 part = isl_basic_map_product(
5210 isl_basic_map_copy(map1->p[i]),
5211 isl_basic_map_copy(map2->p[j]));
5212 if (isl_basic_map_is_empty(part))
5213 isl_basic_map_free(part);
5215 result = isl_map_add(result, part);
5228 /* Given two set A and B, construct its Cartesian product A x B.
5230 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
5232 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
5233 (struct isl_map *)set2);
5236 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
5239 uint32_t hash = isl_hash_init();
5244 bset = isl_basic_set_copy(bset);
5245 bset = isl_basic_set_normalize(bset);
5248 total = isl_basic_set_total_dim(bset);
5249 isl_hash_byte(hash, bset->n_eq & 0xFF);
5250 for (i = 0; i < bset->n_eq; ++i) {
5252 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
5253 isl_hash_hash(hash, c_hash);
5255 isl_hash_byte(hash, bset->n_ineq & 0xFF);
5256 for (i = 0; i < bset->n_ineq; ++i) {
5258 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
5259 isl_hash_hash(hash, c_hash);
5261 isl_hash_byte(hash, bset->n_div & 0xFF);
5262 for (i = 0; i < bset->n_div; ++i) {
5264 if (isl_int_is_zero(bset->div[i][0]))
5266 isl_hash_byte(hash, i & 0xFF);
5267 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
5268 isl_hash_hash(hash, c_hash);
5270 isl_basic_set_free(bset);
5274 uint32_t isl_set_get_hash(struct isl_set *set)
5281 set = isl_set_copy(set);
5282 set = isl_set_normalize(set);
5286 hash = isl_hash_init();
5287 for (i = 0; i < set->n; ++i) {
5289 bset_hash = isl_basic_set_get_hash(set->p[i]);
5290 isl_hash_hash(hash, bset_hash);
5298 /* Check if the value for dimension dim is completely determined
5299 * by the values of the other parameters and variables.
5300 * That is, check if dimension dim is involved in an equality.
5302 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
5309 nparam = isl_basic_set_n_param(bset);
5310 for (i = 0; i < bset->n_eq; ++i)
5311 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
5316 /* Check if the value for dimension dim is completely determined
5317 * by the values of the other parameters and variables.
5318 * That is, check if dimension dim is involved in an equality
5319 * for each of the subsets.
5321 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
5327 for (i = 0; i < set->n; ++i) {
5329 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
5336 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
5337 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
5344 for (i = 0; i < map->n; ++i)
5345 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
5351 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
5352 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
5359 for (i = 0; i < set->n; ++i)
5360 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
5366 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
5368 struct isl_dim *dim;
5369 struct isl_basic_map *bmap;
5376 set = isl_set_align_divs(set);
5381 dim = isl_set_get_dim(set);
5382 if (set->n == 0 || set->p[0]->n_div == 0) {
5384 return isl_map_identity(dim);
5387 n_div = set->p[0]->n_div;
5388 dim = isl_dim_map(dim);
5389 n_param = isl_dim_size(dim, isl_dim_param);
5390 n_set = isl_dim_size(dim, isl_dim_in);
5391 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
5392 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
5393 for (i = 0; i < n_set; ++i)
5394 bmap = var_equal(bmap, i);
5396 total = n_param + n_set + n_set + n_div;
5397 for (i = 0; i < n_div; ++i) {
5398 k = isl_basic_map_alloc_inequality(bmap);
5401 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
5402 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
5403 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
5404 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
5405 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
5406 set->p[0]->div[i][0]);
5408 l = isl_basic_map_alloc_inequality(bmap);
5411 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
5412 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
5413 set->p[0]->div[i][0]);
5414 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
5418 return isl_map_from_basic_map(bmap);
5421 isl_basic_map_free(bmap);