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 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1890 for (i = 0; i < n; ++i) {
1891 j = isl_basic_map_alloc_div(bmap);
1894 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1898 isl_basic_map_free(bmap);
1902 struct isl_basic_map *isl_basic_map_apply_range(
1903 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1905 struct isl_dim *dim_result = NULL;
1906 struct isl_basic_map *bmap;
1907 unsigned n_in, n_out, n, nparam, total, pos;
1908 struct isl_dim_map *dim_map1, *dim_map2;
1910 if (!bmap1 || !bmap2)
1913 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1914 isl_dim_copy(bmap2->dim));
1916 n_in = isl_basic_map_n_in(bmap1);
1917 n_out = isl_basic_map_n_out(bmap2);
1918 n = isl_basic_map_n_out(bmap1);
1919 nparam = isl_basic_map_n_param(bmap1);
1921 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1922 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1923 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1924 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1925 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1926 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1927 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1928 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1929 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1930 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1931 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1933 bmap = isl_basic_map_alloc_dim(dim_result,
1934 bmap1->n_div + bmap2->n_div + n,
1935 bmap1->n_eq + bmap2->n_eq,
1936 bmap1->n_ineq + bmap2->n_ineq);
1937 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1938 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1939 bmap = add_divs(bmap, n);
1940 bmap = isl_basic_map_simplify(bmap);
1941 bmap = isl_basic_map_drop_redundant_divs(bmap);
1942 return isl_basic_map_finalize(bmap);
1944 isl_basic_map_free(bmap1);
1945 isl_basic_map_free(bmap2);
1949 struct isl_basic_set *isl_basic_set_apply(
1950 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1955 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
1958 return (struct isl_basic_set *)
1959 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1961 isl_basic_set_free(bset);
1962 isl_basic_map_free(bmap);
1966 struct isl_basic_map *isl_basic_map_apply_domain(
1967 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1969 if (!bmap1 || !bmap2)
1972 isl_assert(bmap1->ctx,
1973 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1974 isl_assert(bmap1->ctx,
1975 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1978 bmap1 = isl_basic_map_reverse(bmap1);
1979 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1980 return isl_basic_map_reverse(bmap1);
1982 isl_basic_map_free(bmap1);
1983 isl_basic_map_free(bmap2);
1987 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1988 * A \cap B -> f(A) + f(B)
1990 struct isl_basic_map *isl_basic_map_sum(
1991 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1993 unsigned n_in, n_out, nparam, total, pos;
1994 struct isl_basic_map *bmap = NULL;
1995 struct isl_dim_map *dim_map1, *dim_map2;
1998 if (!bmap1 || !bmap2)
2001 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2004 nparam = isl_basic_map_n_param(bmap1);
2005 n_in = isl_basic_map_n_in(bmap1);
2006 n_out = isl_basic_map_n_out(bmap1);
2008 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2009 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2010 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2011 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2012 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2013 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2014 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2015 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2016 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2017 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2018 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2020 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2021 bmap1->n_div + bmap2->n_div + 2 * n_out,
2022 bmap1->n_eq + bmap2->n_eq + n_out,
2023 bmap1->n_ineq + bmap2->n_ineq);
2024 for (i = 0; i < n_out; ++i) {
2025 int j = isl_basic_map_alloc_equality(bmap);
2028 isl_seq_clr(bmap->eq[j], 1+total);
2029 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2030 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2031 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2033 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2034 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2035 bmap = add_divs(bmap, 2 * n_out);
2037 bmap = isl_basic_map_simplify(bmap);
2038 return isl_basic_map_finalize(bmap);
2040 isl_basic_map_free(bmap);
2041 isl_basic_map_free(bmap1);
2042 isl_basic_map_free(bmap2);
2046 /* Given two maps A -> f(A) and B -> g(B), construct a map
2047 * A \cap B -> f(A) + f(B)
2049 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2051 struct isl_map *result;
2057 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2059 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2060 map1->n * map2->n, 0);
2063 for (i = 0; i < map1->n; ++i)
2064 for (j = 0; j < map2->n; ++j) {
2065 struct isl_basic_map *part;
2066 part = isl_basic_map_sum(
2067 isl_basic_map_copy(map1->p[i]),
2068 isl_basic_map_copy(map2->p[j]));
2069 if (isl_basic_map_is_empty(part))
2070 isl_basic_map_free(part);
2072 result = isl_map_add(result, part);
2085 /* Given a basic map A -> f(A), construct A -> -f(A).
2087 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2092 bmap = isl_basic_map_cow(bmap);
2096 n = isl_basic_map_dim(bmap, isl_dim_out);
2097 off = isl_basic_map_offset(bmap, isl_dim_out);
2098 for (i = 0; i < bmap->n_eq; ++i)
2099 for (j = 0; j < n; ++j)
2100 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2101 for (i = 0; i < bmap->n_ineq; ++i)
2102 for (j = 0; j < n; ++j)
2103 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2104 for (i = 0; i < bmap->n_div; ++i)
2105 for (j = 0; j < n; ++j)
2106 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2107 return isl_basic_map_finalize(bmap);
2110 /* Given a map A -> f(A), construct A -> -f(A).
2112 struct isl_map *isl_map_neg(struct isl_map *map)
2116 map = isl_map_cow(map);
2120 for (i = 0; i < map->n; ++i) {
2121 map->p[i] = isl_basic_map_neg(map->p[i]);
2132 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2133 * A -> floor(f(A)/d).
2135 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2138 unsigned n_in, n_out, nparam, total, pos;
2139 struct isl_basic_map *result = NULL;
2140 struct isl_dim_map *dim_map;
2146 nparam = isl_basic_map_n_param(bmap);
2147 n_in = isl_basic_map_n_in(bmap);
2148 n_out = isl_basic_map_n_out(bmap);
2150 total = nparam + n_in + n_out + bmap->n_div + n_out;
2151 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2152 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2153 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2154 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2155 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2157 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2158 bmap->n_div + n_out,
2159 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2160 result = add_constraints_dim_map(result, bmap, dim_map);
2161 result = add_divs(result, n_out);
2162 for (i = 0; i < n_out; ++i) {
2164 j = isl_basic_map_alloc_inequality(result);
2167 isl_seq_clr(result->ineq[j], 1+total);
2168 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2169 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2170 j = isl_basic_map_alloc_inequality(result);
2173 isl_seq_clr(result->ineq[j], 1+total);
2174 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2175 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2176 isl_int_sub_ui(result->ineq[j][0], d, 1);
2179 result = isl_basic_map_simplify(result);
2180 return isl_basic_map_finalize(result);
2182 isl_basic_map_free(result);
2186 /* Given a map A -> f(A) and an integer d, construct a map
2187 * A -> floor(f(A)/d).
2189 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2193 map = isl_map_cow(map);
2197 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2198 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2199 for (i = 0; i < map->n; ++i) {
2200 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2211 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2217 i = isl_basic_map_alloc_equality(bmap);
2220 nparam = isl_basic_map_n_param(bmap);
2221 n_in = isl_basic_map_n_in(bmap);
2222 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2223 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2224 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2225 return isl_basic_map_finalize(bmap);
2227 isl_basic_map_free(bmap);
2231 /* Add a constraints to "bmap" expressing i_pos < o_pos
2233 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2239 i = isl_basic_map_alloc_inequality(bmap);
2242 nparam = isl_basic_map_n_param(bmap);
2243 n_in = isl_basic_map_n_in(bmap);
2244 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2245 isl_int_set_si(bmap->ineq[i][0], -1);
2246 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2247 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2248 return isl_basic_map_finalize(bmap);
2250 isl_basic_map_free(bmap);
2254 /* Add a constraints to "bmap" expressing i_pos > o_pos
2256 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2262 i = isl_basic_map_alloc_inequality(bmap);
2265 nparam = isl_basic_map_n_param(bmap);
2266 n_in = isl_basic_map_n_in(bmap);
2267 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2268 isl_int_set_si(bmap->ineq[i][0], -1);
2269 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2270 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2271 return isl_basic_map_finalize(bmap);
2273 isl_basic_map_free(bmap);
2277 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2280 struct isl_basic_map *bmap;
2281 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2284 for (i = 0; i < n_equal && bmap; ++i)
2285 bmap = var_equal(bmap, i);
2286 return isl_basic_map_finalize(bmap);
2289 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2291 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2294 struct isl_basic_map *bmap;
2295 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2298 for (i = 0; i < pos && bmap; ++i)
2299 bmap = var_equal(bmap, i);
2301 bmap = var_less(bmap, pos);
2302 return isl_basic_map_finalize(bmap);
2305 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2307 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2310 struct isl_basic_map *bmap;
2311 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2314 for (i = 0; i < pos && bmap; ++i)
2315 bmap = var_equal(bmap, i);
2317 bmap = var_more(bmap, pos);
2318 return isl_basic_map_finalize(bmap);
2321 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
2323 struct isl_map *map;
2330 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2332 for (i = 0; i < dim; ++i)
2333 map = isl_map_add(map,
2334 isl_basic_map_less_at(isl_dim_copy(dims), i));
2336 map = isl_map_add(map,
2337 isl_basic_map_equal(isl_dim_copy(dims), dim));
2343 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
2345 return map_lex_lte(isl_dim_map(set_dim), 0);
2348 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
2350 return map_lex_lte(isl_dim_map(set_dim), 1);
2353 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
2355 struct isl_map *map;
2362 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2364 for (i = 0; i < dim; ++i)
2365 map = isl_map_add(map,
2366 isl_basic_map_more_at(isl_dim_copy(dims), i));
2368 map = isl_map_add(map,
2369 isl_basic_map_equal(isl_dim_copy(dims), dim));
2375 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
2377 return map_lex_gte(isl_dim_map(set_dim), 0);
2380 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
2382 return map_lex_gte(isl_dim_map(set_dim), 1);
2385 struct isl_basic_map *isl_basic_map_from_basic_set(
2386 struct isl_basic_set *bset, struct isl_dim *dim)
2388 struct isl_basic_map *bmap;
2390 bset = isl_basic_set_cow(bset);
2394 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2395 isl_dim_free(bset->dim);
2396 bmap = (struct isl_basic_map *) bset;
2398 return isl_basic_map_finalize(bmap);
2400 isl_basic_set_free(bset);
2405 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2409 if (bmap->dim->n_in == 0)
2410 return (struct isl_basic_set *)bmap;
2411 bmap = isl_basic_map_cow(bmap);
2414 bmap->dim = isl_dim_cow(bmap->dim);
2417 bmap->dim->n_out += bmap->dim->n_in;
2418 bmap->dim->n_in = 0;
2419 bmap = isl_basic_map_finalize(bmap);
2420 return (struct isl_basic_set *)bmap;
2422 isl_basic_map_free(bmap);
2426 /* For a div d = floor(f/m), add the constraints
2429 * -(f-(n-1)) + m d >= 0
2431 * Note that the second constraint is the negation of
2435 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2438 unsigned total = isl_basic_map_total_dim(bmap);
2439 unsigned div_pos = 1 + total - bmap->n_div + div;
2441 i = isl_basic_map_alloc_inequality(bmap);
2444 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2445 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2447 j = isl_basic_map_alloc_inequality(bmap);
2450 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2451 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2452 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2456 struct isl_basic_set *isl_basic_map_underlying_set(
2457 struct isl_basic_map *bmap)
2461 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2462 return (struct isl_basic_set *)bmap;
2463 bmap = isl_basic_map_cow(bmap);
2466 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2469 bmap->extra -= bmap->n_div;
2471 bmap = isl_basic_map_finalize(bmap);
2472 return (struct isl_basic_set *)bmap;
2477 struct isl_basic_map *isl_basic_map_overlying_set(
2478 struct isl_basic_set *bset, struct isl_basic_map *like)
2480 struct isl_basic_map *bmap;
2481 struct isl_ctx *ctx;
2488 isl_assert(ctx, bset->n_div == 0, goto error);
2489 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2490 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2492 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2493 isl_basic_map_free(like);
2494 return (struct isl_basic_map *)bset;
2496 bset = isl_basic_set_cow(bset);
2499 total = bset->dim->n_out + bset->extra;
2500 bmap = (struct isl_basic_map *)bset;
2501 isl_dim_free(bmap->dim);
2502 bmap->dim = isl_dim_copy(like->dim);
2505 bmap->n_div = like->n_div;
2506 bmap->extra += like->n_div;
2509 ltotal = total - bmap->extra + like->extra;
2512 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2513 bmap->extra * (1 + 1 + total));
2514 if (isl_blk_is_error(bmap->block2))
2516 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2520 for (i = 0; i < bmap->extra; ++i)
2521 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2522 for (i = 0; i < like->n_div; ++i) {
2523 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2524 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2526 bmap = isl_basic_map_extend_constraints(bmap,
2527 0, 2 * like->n_div);
2528 for (i = 0; i < like->n_div; ++i) {
2529 if (isl_int_is_zero(bmap->div[i][0]))
2531 if (add_div_constraints(bmap, i) < 0)
2535 isl_basic_map_free(like);
2536 bmap = isl_basic_map_simplify(bmap);
2537 bmap = isl_basic_map_finalize(bmap);
2540 isl_basic_map_free(like);
2541 isl_basic_set_free(bset);
2545 struct isl_basic_set *isl_basic_set_from_underlying_set(
2546 struct isl_basic_set *bset, struct isl_basic_set *like)
2548 return (struct isl_basic_set *)
2549 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2552 struct isl_set *isl_set_from_underlying_set(
2553 struct isl_set *set, struct isl_basic_set *like)
2559 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2561 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2562 isl_basic_set_free(like);
2565 set = isl_set_cow(set);
2568 for (i = 0; i < set->n; ++i) {
2569 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2570 isl_basic_set_copy(like));
2574 isl_dim_free(set->dim);
2575 set->dim = isl_dim_copy(like->dim);
2578 isl_basic_set_free(like);
2581 isl_basic_set_free(like);
2586 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2590 map = isl_map_cow(map);
2593 map->dim = isl_dim_cow(map->dim);
2597 for (i = 1; i < map->n; ++i)
2598 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2600 for (i = 0; i < map->n; ++i) {
2601 map->p[i] = (struct isl_basic_map *)
2602 isl_basic_map_underlying_set(map->p[i]);
2607 map->dim = isl_dim_underlying(map->dim, 0);
2609 isl_dim_free(map->dim);
2610 map->dim = isl_dim_copy(map->p[0]->dim);
2614 return (struct isl_set *)map;
2620 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2622 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2625 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2627 struct isl_basic_set *domain;
2632 n_in = isl_basic_map_n_in(bmap);
2633 n_out = isl_basic_map_n_out(bmap);
2634 domain = isl_basic_set_from_basic_map(bmap);
2635 return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
2638 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2640 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2643 struct isl_set *isl_map_range(struct isl_map *map)
2646 struct isl_set *set;
2650 map = isl_map_cow(map);
2654 set = (struct isl_set *) map;
2655 if (set->dim->n_in != 0) {
2656 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2660 for (i = 0; i < map->n; ++i) {
2661 set->p[i] = isl_basic_map_range(map->p[i]);
2665 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2666 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2673 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2676 struct isl_map *map = NULL;
2678 set = isl_set_cow(set);
2681 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2682 map = (struct isl_map *)set;
2683 for (i = 0; i < set->n; ++i) {
2684 map->p[i] = isl_basic_map_from_basic_set(
2685 set->p[i], isl_dim_copy(dim));
2689 isl_dim_free(map->dim);
2698 struct isl_map *isl_map_from_range(struct isl_set *set)
2700 return (struct isl_map *)set;
2703 struct isl_set *isl_set_from_map(struct isl_map *map)
2706 struct isl_set *set = NULL;
2710 map = isl_map_cow(map);
2713 map->dim = isl_dim_cow(map->dim);
2716 map->dim->n_out += map->dim->n_in;
2718 set = (struct isl_set *)map;
2719 for (i = 0; i < map->n; ++i) {
2720 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2730 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2732 struct isl_map *map;
2736 isl_assert(dim->ctx, n >= 0, return NULL);
2737 map = isl_alloc(dim->ctx, struct isl_map,
2738 sizeof(struct isl_map) +
2739 (n - 1) * sizeof(struct isl_basic_map *));
2743 map->ctx = dim->ctx;
2744 isl_ctx_ref(map->ctx);
2756 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2757 unsigned nparam, unsigned in, unsigned out, int n,
2760 struct isl_map *map;
2761 struct isl_dim *dims;
2763 dims = isl_dim_alloc(ctx, nparam, in, out);
2767 map = isl_map_alloc_dim(dims, n, flags);
2771 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
2773 struct isl_basic_map *bmap;
2774 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
2775 bmap = isl_basic_map_set_to_empty(bmap);
2779 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2781 struct isl_basic_set *bset;
2782 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2783 bset = isl_basic_set_set_to_empty(bset);
2787 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2789 struct isl_basic_map *bmap;
2792 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2793 bmap = isl_basic_map_set_to_empty(bmap);
2797 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2799 struct isl_basic_map *bmap;
2802 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2803 bmap = isl_basic_map_set_to_empty(bmap);
2807 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2809 struct isl_basic_set *bset;
2812 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2813 bset = isl_basic_set_set_to_empty(bset);
2817 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2819 struct isl_basic_map *bmap;
2820 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2824 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2826 struct isl_basic_set *bset;
2827 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2831 __isl_give isl_basic_map *isl_basic_map_universe_like(
2832 __isl_keep isl_basic_map *model)
2836 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2839 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2843 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2846 struct isl_map *isl_map_empty(struct isl_dim *dim)
2848 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2851 struct isl_map *isl_map_empty_like(struct isl_map *model)
2855 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2858 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2862 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2865 struct isl_set *isl_set_empty(struct isl_dim *dim)
2867 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2870 struct isl_set *isl_set_empty_like(struct isl_set *model)
2874 return isl_set_empty(isl_dim_copy(model->dim));
2877 struct isl_map *isl_map_universe(struct isl_dim *dim)
2879 struct isl_map *map;
2882 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2883 map = isl_map_add(map, isl_basic_map_universe(dim));
2887 struct isl_set *isl_set_universe(struct isl_dim *dim)
2889 struct isl_set *set;
2892 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2893 set = isl_set_add(set, isl_basic_set_universe(dim));
2897 struct isl_map *isl_map_dup(struct isl_map *map)
2900 struct isl_map *dup;
2904 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2905 for (i = 0; i < map->n; ++i)
2906 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2910 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2914 if (isl_basic_map_fast_is_empty(bmap)) {
2915 isl_basic_map_free(bmap);
2918 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2919 isl_assert(map->ctx, map->n < map->size, goto error);
2920 map->p[map->n] = bmap;
2922 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2928 isl_basic_map_free(bmap);
2932 void isl_map_free(struct isl_map *map)
2942 isl_ctx_deref(map->ctx);
2943 for (i = 0; i < map->n; ++i)
2944 isl_basic_map_free(map->p[i]);
2945 isl_dim_free(map->dim);
2949 struct isl_map *isl_map_extend(struct isl_map *base,
2950 unsigned nparam, unsigned n_in, unsigned n_out)
2954 base = isl_map_cow(base);
2958 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2961 for (i = 0; i < base->n; ++i) {
2962 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2963 isl_dim_copy(base->dim), 0, 0, 0);
2973 struct isl_set *isl_set_extend(struct isl_set *base,
2974 unsigned nparam, unsigned dim)
2976 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2980 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2981 unsigned pos, int value)
2985 bmap = isl_basic_map_cow(bmap);
2986 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2987 j = isl_basic_map_alloc_equality(bmap);
2990 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
2991 isl_int_set_si(bmap->eq[j][pos], -1);
2992 isl_int_set_si(bmap->eq[j][0], value);
2993 bmap = isl_basic_map_simplify(bmap);
2994 return isl_basic_map_finalize(bmap);
2996 isl_basic_map_free(bmap);
3000 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3001 enum isl_dim_type type, unsigned pos, int value)
3005 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3006 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
3009 isl_basic_map_free(bmap);
3013 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3014 enum isl_dim_type type, unsigned pos, int value)
3016 return (struct isl_basic_set *)
3017 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3021 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3022 unsigned input, int value)
3024 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3027 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3028 unsigned dim, int value)
3030 return (struct isl_basic_set *)
3031 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3032 isl_dim_set, dim, value);
3035 struct isl_map *isl_map_fix_si(struct isl_map *map,
3036 enum isl_dim_type type, unsigned pos, int value)
3040 map = isl_map_cow(map);
3044 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3045 for (i = 0; i < map->n; ++i) {
3046 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3050 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3057 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3058 unsigned input, int value)
3060 return isl_map_fix_si(map, isl_dim_in, input, value);
3063 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3065 return (struct isl_set *)
3066 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3069 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3070 unsigned dim, isl_int value)
3074 bset = isl_basic_set_cow(bset);
3075 bset = isl_basic_set_extend_constraints(bset, 0, 1);
3076 j = isl_basic_set_alloc_inequality(bset);
3079 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3080 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3081 isl_int_neg(bset->ineq[j][0], value);
3082 bset = isl_basic_set_simplify(bset);
3083 return isl_basic_set_finalize(bset);
3085 isl_basic_set_free(bset);
3089 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3094 set = isl_set_cow(set);
3098 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3099 for (i = 0; i < set->n; ++i) {
3100 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3110 struct isl_map *isl_map_reverse(struct isl_map *map)
3114 map = isl_map_cow(map);
3118 map->dim = isl_dim_reverse(map->dim);
3121 for (i = 0; i < map->n; ++i) {
3122 map->p[i] = isl_basic_map_reverse(map->p[i]);
3126 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3133 static struct isl_map *isl_basic_map_partial_lexopt(
3134 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3135 struct isl_set **empty, int max)
3139 if (bmap->ctx->pip == ISL_PIP_PIP)
3140 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3142 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3144 isl_basic_map_free(bmap);
3145 isl_basic_set_free(dom);
3151 struct isl_map *isl_basic_map_partial_lexmax(
3152 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3153 struct isl_set **empty)
3155 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
3158 struct isl_map *isl_basic_map_partial_lexmin(
3159 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3160 struct isl_set **empty)
3162 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3165 struct isl_set *isl_basic_set_partial_lexmin(
3166 struct isl_basic_set *bset, struct isl_basic_set *dom,
3167 struct isl_set **empty)
3169 return (struct isl_set *)
3170 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3174 struct isl_set *isl_basic_set_partial_lexmax(
3175 struct isl_basic_set *bset, struct isl_basic_set *dom,
3176 struct isl_set **empty)
3178 return (struct isl_set *)
3179 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3183 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
3185 struct isl_basic_map *bmap = NULL;
3186 struct isl_basic_set *dom = NULL;
3187 struct isl_map *min;
3188 struct isl_dim *param_dim;
3192 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
3195 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3196 dom = isl_basic_set_universe(param_dim);
3199 min = isl_basic_map_partial_lexmin(bmap, dom, NULL);
3200 return isl_map_range(min);
3202 isl_basic_map_free(bmap);
3206 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
3207 struct isl_dim *dim)
3214 for (i = 0; i < map->n; ++i) {
3215 isl_dim_free(map->p[i]->dim);
3216 map->p[i]->dim = isl_dim_copy(dim);
3218 isl_dim_free(map->dim);
3228 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
3229 struct isl_dim *dim)
3231 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3234 /* Apply a preimage specified by "mat" on the parameters of "bset".
3235 * bset is assumed to have only parameters and divs.
3237 static struct isl_basic_set *basic_set_parameter_preimage(
3238 struct isl_basic_set *bset, struct isl_mat *mat)
3245 bset->dim = isl_dim_cow(bset->dim);
3249 nparam = isl_basic_set_dim(bset, isl_dim_param);
3251 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
3253 bset->dim->nparam = 0;
3254 bset->dim->n_out = nparam;
3255 bset = isl_basic_set_preimage(bset, mat);
3257 bset->dim->nparam = bset->dim->n_out;
3258 bset->dim->n_out = 0;
3263 isl_basic_set_free(bset);
3267 /* Apply a preimage specified by "mat" on the parameters of "set".
3268 * set is assumed to have only parameters and divs.
3270 static struct isl_set *set_parameter_preimage(
3271 struct isl_set *set, struct isl_mat *mat)
3273 struct isl_dim *dim = NULL;
3279 dim = isl_dim_copy(set->dim);
3280 dim = isl_dim_cow(dim);
3284 nparam = isl_set_dim(set, isl_dim_param);
3286 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
3289 dim->n_out = nparam;
3290 isl_set_reset_dim(set, dim);
3291 set = isl_set_preimage(set, mat);
3294 dim = isl_dim_copy(set->dim);
3295 dim = isl_dim_cow(dim);
3298 dim->nparam = dim->n_out;
3300 isl_set_reset_dim(set, dim);
3310 /* Intersect the basic set "bset" with the affine space specified by the
3311 * equalities in "eq".
3313 static struct isl_basic_set *basic_set_append_equalities(
3314 struct isl_basic_set *bset, struct isl_mat *eq)
3322 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
3327 len = 1 + isl_dim_total(bset->dim) + bset->extra;
3328 for (i = 0; i < eq->n_row; ++i) {
3329 k = isl_basic_set_alloc_equality(bset);
3332 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
3333 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
3340 isl_basic_set_free(bset);
3344 /* Intersect the set "set" with the affine space specified by the
3345 * equalities in "eq".
3347 static struct isl_set *set_append_equalities(struct isl_set *set,
3355 for (i = 0; i < set->n; ++i) {
3356 set->p[i] = basic_set_append_equalities(set->p[i],
3369 /* Project the given basic set onto its parameter domain, possibly introducing
3370 * new, explicit, existential variables in the constraints.
3371 * The input has parameters and (possibly implicit) existential variables.
3372 * The output has the same parameters, but only
3373 * explicit existentially quantified variables.
3375 * The actual projection is performed by pip, but pip doesn't seem
3376 * to like equalities very much, so we first remove the equalities
3377 * among the parameters by performing a variable compression on
3378 * the parameters. Afterward, an inverse transformation is performed
3379 * and the equalities among the parameters are inserted back in.
3381 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
3385 struct isl_mat *T, *T2;
3386 struct isl_set *set;
3387 unsigned nparam, n_div;
3389 bset = isl_basic_set_cow(bset);
3393 if (bset->n_eq == 0)
3394 return isl_basic_set_lexmin(bset);
3396 isl_basic_set_gauss(bset, NULL);
3398 nparam = isl_basic_set_dim(bset, isl_dim_param);
3399 n_div = isl_basic_set_dim(bset, isl_dim_div);
3401 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
3402 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
3405 if (i == bset->n_eq)
3406 return isl_basic_set_lexmin(bset);
3408 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
3410 eq = isl_mat_cow(eq);
3411 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
3412 bset = basic_set_parameter_preimage(bset, T);
3414 set = isl_basic_set_lexmin(bset);
3415 set = set_parameter_preimage(set, T2);
3416 set = set_append_equalities(set, eq);
3420 /* Compute an explicit representation for all the existentially
3421 * quantified variables.
3422 * The input and output dimensions are first turned into parameters.
3423 * compute_divs then returns a map with the same parameters and
3424 * no input or output dimensions and the dimension specification
3425 * is reset to that of the input.
3427 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
3429 struct isl_basic_set *bset;
3430 struct isl_set *set;
3431 struct isl_map *map;
3432 struct isl_dim *dim, *orig_dim = NULL;
3437 bmap = isl_basic_map_cow(bmap);
3441 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3442 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3443 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3444 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
3448 orig_dim = bmap->dim;
3450 bset = (struct isl_basic_set *)bmap;
3452 set = parameter_compute_divs(bset);
3453 map = (struct isl_map *)set;
3454 map = isl_map_reset_dim(map, orig_dim);
3458 isl_basic_map_free(bmap);
3462 /* If bmap contains any unknown divs, then compute explicit
3463 * expressions for them. However, this computation may be
3464 * quite expensive, so first try to remove divs that aren't
3467 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
3474 off = isl_dim_total(bmap->dim);
3475 for (i = 0; i < bmap->n_div; ++i) {
3476 if (isl_int_is_zero(bmap->div[i][0]))
3478 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
3481 if (i == bmap->n_div)
3482 return isl_map_from_basic_map(bmap);
3483 bmap = isl_basic_map_drop_redundant_divs(bmap);
3486 for (i = 0; i < bmap->n_div; ++i)
3487 if (isl_int_is_zero(bmap->div[i][0]))
3489 if (i == bmap->n_div)
3490 return isl_map_from_basic_map(bmap);
3491 struct isl_map *map = compute_divs(bmap);
3494 isl_basic_map_free(bmap);
3498 struct isl_map *isl_map_compute_divs(struct isl_map *map)
3501 struct isl_map *res;
3507 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
3508 for (i = 1 ; i < map->n; ++i) {
3510 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
3511 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3512 res = isl_map_union_disjoint(res, r2);
3514 res = isl_map_union(res, r2);
3521 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3523 return (struct isl_set *)
3524 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3527 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3529 return (struct isl_set *)
3530 isl_map_compute_divs((struct isl_map *)set);
3533 struct isl_set *isl_map_domain(struct isl_map *map)
3536 struct isl_set *set;
3541 map = isl_map_cow(map);
3545 set = (struct isl_set *)map;
3546 set->dim = isl_dim_domain(set->dim);
3549 for (i = 0; i < map->n; ++i) {
3550 set->p[i] = isl_basic_map_domain(map->p[i]);
3554 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3555 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3562 struct isl_map *isl_map_union_disjoint(
3563 struct isl_map *map1, struct isl_map *map2)
3567 struct isl_map *map = NULL;
3581 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3583 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3584 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3585 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3587 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3588 map1->n + map2->n, flags);
3591 for (i = 0; i < map1->n; ++i) {
3592 map = isl_map_add(map,
3593 isl_basic_map_copy(map1->p[i]));
3597 for (i = 0; i < map2->n; ++i) {
3598 map = isl_map_add(map,
3599 isl_basic_map_copy(map2->p[i]));
3613 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3615 map1 = isl_map_union_disjoint(map1, map2);
3619 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3623 struct isl_set *isl_set_union_disjoint(
3624 struct isl_set *set1, struct isl_set *set2)
3626 return (struct isl_set *)
3627 isl_map_union_disjoint(
3628 (struct isl_map *)set1, (struct isl_map *)set2);
3631 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3633 return (struct isl_set *)
3634 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3637 struct isl_map *isl_map_intersect_range(
3638 struct isl_map *map, struct isl_set *set)
3641 struct isl_map *result;
3647 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3648 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3649 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3651 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3652 map->n * set->n, flags);
3655 for (i = 0; i < map->n; ++i)
3656 for (j = 0; j < set->n; ++j) {
3657 result = isl_map_add(result,
3658 isl_basic_map_intersect_range(
3659 isl_basic_map_copy(map->p[i]),
3660 isl_basic_set_copy(set->p[j])));
3673 struct isl_map *isl_map_intersect_domain(
3674 struct isl_map *map, struct isl_set *set)
3676 return isl_map_reverse(
3677 isl_map_intersect_range(isl_map_reverse(map), set));
3680 struct isl_map *isl_map_apply_domain(
3681 struct isl_map *map1, struct isl_map *map2)
3685 map1 = isl_map_reverse(map1);
3686 map1 = isl_map_apply_range(map1, map2);
3687 return isl_map_reverse(map1);
3694 struct isl_map *isl_map_apply_range(
3695 struct isl_map *map1, struct isl_map *map2)
3697 struct isl_dim *dim_result;
3698 struct isl_map *result;
3704 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3705 isl_dim_copy(map2->dim));
3707 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3710 for (i = 0; i < map1->n; ++i)
3711 for (j = 0; j < map2->n; ++j) {
3712 result = isl_map_add(result,
3713 isl_basic_map_apply_range(
3714 isl_basic_map_copy(map1->p[i]),
3715 isl_basic_map_copy(map2->p[j])));
3721 if (result && result->n <= 1)
3722 ISL_F_SET(result, ISL_MAP_DISJOINT);
3731 * returns range - domain
3733 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3735 struct isl_basic_set *bset;
3742 dim = isl_basic_map_n_in(bmap);
3743 nparam = isl_basic_map_n_param(bmap);
3744 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3745 bset = isl_basic_set_from_basic_map(bmap);
3746 bset = isl_basic_set_cow(bset);
3747 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3748 bset = isl_basic_set_swap_vars(bset, 2*dim);
3749 for (i = 0; i < dim; ++i) {
3750 int j = isl_basic_map_alloc_equality(
3751 (struct isl_basic_map *)bset);
3754 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3755 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3756 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3757 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3759 return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
3761 isl_basic_map_free(bmap);
3766 * returns range - domain
3768 struct isl_set *isl_map_deltas(struct isl_map *map)
3771 struct isl_set *result;
3776 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3777 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3778 isl_map_n_in(map), map->n, map->flags);
3781 for (i = 0; i < map->n; ++i)
3782 result = isl_set_add(result,
3783 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3791 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3793 struct isl_basic_map *bmap;
3801 nparam = dims->nparam;
3803 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3807 for (i = 0; i < dim; ++i) {
3808 int j = isl_basic_map_alloc_equality(bmap);
3811 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3812 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3813 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3815 return isl_basic_map_finalize(bmap);
3817 isl_basic_map_free(bmap);
3821 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3823 struct isl_dim *dim = isl_dim_map(set_dim);
3826 return basic_map_identity(dim);
3829 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3831 if (!model || !model->dim)
3833 isl_assert(model->ctx,
3834 model->dim->n_in == model->dim->n_out, return NULL);
3835 return basic_map_identity(isl_dim_copy(model->dim));
3838 static struct isl_map *map_identity(struct isl_dim *dim)
3840 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3841 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3844 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3846 struct isl_dim *dim = isl_dim_map(set_dim);
3849 return map_identity(dim);
3852 struct isl_map *isl_map_identity_like(struct isl_map *model)
3854 if (!model || !model->dim)
3856 isl_assert(model->ctx,
3857 model->dim->n_in == model->dim->n_out, return NULL);
3858 return map_identity(isl_dim_copy(model->dim));
3861 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
3863 if (!model || !model->dim)
3865 isl_assert(model->ctx,
3866 model->dim->n_in == model->dim->n_out, return NULL);
3867 return map_identity(isl_dim_copy(model->dim));
3870 /* Construct a basic set with all set dimensions having only non-negative
3873 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
3878 struct isl_basic_set *bset;
3882 nparam = dims->nparam;
3884 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
3887 for (i = 0; i < dim; ++i) {
3888 int k = isl_basic_set_alloc_inequality(bset);
3891 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
3892 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
3896 isl_basic_set_free(bset);
3900 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3902 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3905 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3907 return isl_map_is_subset(
3908 (struct isl_map *)set1, (struct isl_map *)set2);
3911 int isl_basic_map_is_subset(
3912 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3915 struct isl_map *map1;
3916 struct isl_map *map2;
3918 if (!bmap1 || !bmap2)
3921 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3922 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3924 is_subset = isl_map_is_subset(map1, map2);
3932 int isl_basic_map_is_equal(
3933 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3937 if (!bmap1 || !bmap2)
3939 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3942 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3946 int isl_basic_set_is_equal(
3947 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3949 return isl_basic_map_is_equal(
3950 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3953 int isl_map_is_empty(struct isl_map *map)
3960 for (i = 0; i < map->n; ++i) {
3961 is_empty = isl_basic_map_is_empty(map->p[i]);
3970 int isl_map_fast_is_empty(struct isl_map *map)
3975 int isl_set_is_empty(struct isl_set *set)
3977 return isl_map_is_empty((struct isl_map *)set);
3980 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3983 struct isl_map *diff;
3988 if (isl_map_is_empty(map1))
3991 if (isl_map_is_empty(map2))
3994 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3998 is_subset = isl_map_is_empty(diff);
4004 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
4010 is_subset = isl_map_is_subset(map1, map2);
4013 is_subset = isl_map_is_subset(map2, map1);
4017 int isl_basic_map_is_strict_subset(
4018 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4022 if (!bmap1 || !bmap2)
4024 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4027 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4028 if (is_subset == -1)
4033 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
4039 is_subset = isl_map_is_subset(map1, map2);
4042 is_subset = isl_map_is_subset(map2, map1);
4043 if (is_subset == -1)
4048 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
4052 return bmap->n_eq == 0 && bmap->n_ineq == 0;
4055 int isl_basic_set_is_universe(struct isl_basic_set *bset)
4059 return bset->n_eq == 0 && bset->n_ineq == 0;
4062 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
4064 struct isl_basic_set *bset = NULL;
4065 struct isl_vec *sample = NULL;
4072 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4075 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
4076 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
4077 copy = isl_basic_map_convex_hull(copy);
4078 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
4079 isl_basic_map_free(copy);
4083 total = 1 + isl_basic_map_total_dim(bmap);
4084 if (bmap->sample && bmap->sample->size == total) {
4085 int contains = basic_map_contains(bmap, bmap->sample);
4091 isl_vec_free(bmap->sample);
4092 bmap->sample = NULL;
4093 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
4096 sample = isl_basic_set_sample_vec(bset);
4099 empty = sample->size == 0;
4100 isl_vec_free(bmap->sample);
4101 bmap->sample = sample;
4103 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
4108 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
4112 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
4115 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
4119 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
4122 int isl_basic_set_is_empty(struct isl_basic_set *bset)
4124 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
4127 struct isl_map *isl_basic_map_union(
4128 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4130 struct isl_map *map;
4131 if (!bmap1 || !bmap2)
4134 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
4136 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
4139 map = isl_map_add(map, bmap1);
4140 map = isl_map_add(map, bmap2);
4143 isl_basic_map_free(bmap1);
4144 isl_basic_map_free(bmap2);
4148 struct isl_set *isl_basic_set_union(
4149 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4151 return (struct isl_set *)isl_basic_map_union(
4152 (struct isl_basic_map *)bset1,
4153 (struct isl_basic_map *)bset2);
4156 /* Order divs such that any div only depends on previous divs */
4157 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
4160 unsigned off = isl_dim_total(bmap->dim);
4162 for (i = 0; i < bmap->n_div; ++i) {
4164 if (isl_int_is_zero(bmap->div[i][0]))
4166 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
4170 isl_basic_map_swap_div(bmap, i, i + pos);
4176 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
4178 return (struct isl_basic_set *)
4179 isl_basic_map_order_divs((struct isl_basic_map *)bset);
4182 /* Look for a div in dst that corresponds to the div "div" in src.
4183 * The divs before "div" in src and dst are assumed to be the same.
4185 * Returns -1 if no corresponding div was found and the position
4186 * of the corresponding div in dst otherwise.
4188 static int find_div(struct isl_basic_map *dst,
4189 struct isl_basic_map *src, unsigned div)
4193 unsigned total = isl_dim_total(src->dim);
4195 isl_assert(dst->ctx, div <= dst->n_div, return -1);
4196 for (i = div; i < dst->n_div; ++i)
4197 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
4198 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
4199 dst->n_div - div) == -1)
4204 struct isl_basic_map *isl_basic_map_align_divs(
4205 struct isl_basic_map *dst, struct isl_basic_map *src)
4208 unsigned total = isl_dim_total(src->dim);
4213 if (src->n_div == 0)
4216 for (i = 0; i < src->n_div; ++i)
4217 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
4219 src = isl_basic_map_order_divs(src);
4220 dst = isl_basic_map_cow(dst);
4221 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
4222 src->n_div, 0, 2 * src->n_div);
4225 for (i = 0; i < src->n_div; ++i) {
4226 int j = find_div(dst, src, i);
4228 j = isl_basic_map_alloc_div(dst);
4231 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
4232 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
4233 if (add_div_constraints(dst, j) < 0)
4237 isl_basic_map_swap_div(dst, i, j);
4241 isl_basic_map_free(dst);
4245 struct isl_basic_set *isl_basic_set_align_divs(
4246 struct isl_basic_set *dst, struct isl_basic_set *src)
4248 return (struct isl_basic_set *)isl_basic_map_align_divs(
4249 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
4252 struct isl_map *isl_map_align_divs(struct isl_map *map)
4256 map = isl_map_compute_divs(map);
4257 map = isl_map_cow(map);
4261 for (i = 1; i < map->n; ++i)
4262 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
4263 for (i = 1; i < map->n; ++i)
4264 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
4266 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4270 struct isl_set *isl_set_align_divs(struct isl_set *set)
4272 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
4275 static struct isl_map *add_cut_constraint(struct isl_map *dst,
4276 struct isl_basic_map *src, isl_int *c,
4277 unsigned len, int oppose)
4279 struct isl_basic_map *copy = NULL;
4284 copy = isl_basic_map_copy(src);
4285 copy = isl_basic_map_cow(copy);
4288 copy = isl_basic_map_extend_constraints(copy, 0, 1);
4289 k = isl_basic_map_alloc_inequality(copy);
4293 isl_seq_neg(copy->ineq[k], c, len);
4295 isl_seq_cpy(copy->ineq[k], c, len);
4296 total = 1 + isl_basic_map_total_dim(copy);
4297 isl_seq_clr(copy->ineq[k]+len, total - len);
4298 isl_inequality_negate(copy, k);
4299 copy = isl_basic_map_simplify(copy);
4300 copy = isl_basic_map_finalize(copy);
4301 is_empty = isl_basic_map_is_empty(copy);
4305 dst = isl_map_add(dst, copy);
4307 isl_basic_map_free(copy);
4310 isl_basic_map_free(copy);
4315 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
4319 struct isl_map *rest = NULL;
4321 unsigned total = isl_basic_map_total_dim(bmap);
4328 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4329 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4331 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
4332 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
4336 for (i = 0; i < map->n; ++i) {
4337 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
4342 for (j = 0; j < map->n; ++j)
4343 map->p[j] = isl_basic_map_cow(map->p[j]);
4345 for (i = 0; i < bmap->n_eq; ++i) {
4346 for (j = 0; j < map->n; ++j) {
4347 rest = add_cut_constraint(rest,
4348 map->p[j], bmap->eq[i], 1+total, 0);
4352 rest = add_cut_constraint(rest,
4353 map->p[j], bmap->eq[i], 1+total, 1);
4357 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4361 k = isl_basic_map_alloc_equality(map->p[j]);
4364 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
4365 isl_seq_clr(map->p[j]->eq[k]+1+total,
4366 map->p[j]->n_div - bmap->n_div);
4370 for (i = 0; i < bmap->n_ineq; ++i) {
4371 for (j = 0; j < map->n; ++j) {
4372 rest = add_cut_constraint(rest,
4373 map->p[j], bmap->ineq[i], 1+total, 0);
4377 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4381 k = isl_basic_map_alloc_inequality(map->p[j]);
4384 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
4385 isl_seq_clr(map->p[j]->ineq[k]+1+total,
4386 map->p[j]->n_div - bmap->n_div);
4398 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
4404 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4406 if (isl_map_is_empty(map2)) {
4411 map1 = isl_map_compute_divs(map1);
4412 map2 = isl_map_compute_divs(map2);
4416 for (i = 0; map1 && i < map2->n; ++i)
4417 map1 = subtract(map1, map2->p[i]);
4427 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
4429 return (struct isl_set *)
4431 (struct isl_map *)set1, (struct isl_map *)set2);
4434 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
4438 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
4439 map = isl_map_intersect_domain(map, set);
4440 set = isl_map_range(map);
4448 /* There is no need to cow as removing empty parts doesn't change
4449 * the meaning of the set.
4451 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
4458 for (i = map->n-1; i >= 0; --i) {
4459 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
4461 isl_basic_map_free(map->p[i]);
4462 if (i != map->n-1) {
4463 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4464 map->p[i] = map->p[map->n-1];
4472 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
4474 return (struct isl_set *)
4475 isl_map_remove_empty_parts((struct isl_map *)set);
4478 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
4480 struct isl_basic_map *bmap;
4481 if (!map || map->n == 0)
4483 bmap = map->p[map->n-1];
4484 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
4485 return isl_basic_map_copy(bmap);
4488 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
4490 return (struct isl_basic_set *)
4491 isl_map_copy_basic_map((struct isl_map *)set);
4494 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
4495 struct isl_basic_map *bmap)
4501 for (i = map->n-1; i >= 0; --i) {
4502 if (map->p[i] != bmap)
4504 map = isl_map_cow(map);
4507 isl_basic_map_free(map->p[i]);
4508 if (i != map->n-1) {
4509 ISL_F_CLR(map, ISL_SET_NORMALIZED);
4510 map->p[i] = map->p[map->n-1];
4515 isl_basic_map_free(bmap);
4519 isl_basic_map_free(bmap);
4523 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4524 struct isl_basic_set *bset)
4526 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
4527 (struct isl_basic_map *)bset);
4530 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4531 * for any common value of the parameters and dimensions preceding dim
4532 * in both basic sets, the values of dimension pos in bset1 are
4533 * smaller or larger than those in bset2.
4536 * 1 if bset1 follows bset2
4537 * -1 if bset1 precedes bset2
4538 * 0 if bset1 and bset2 are incomparable
4539 * -2 if some error occurred.
4541 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4542 struct isl_basic_set *bset2, int pos)
4544 struct isl_dim *dims;
4545 struct isl_basic_map *bmap1 = NULL;
4546 struct isl_basic_map *bmap2 = NULL;
4547 struct isl_ctx *ctx;
4548 struct isl_vec *obj;
4551 unsigned dim1, dim2;
4553 enum isl_lp_result res;
4556 if (!bset1 || !bset2)
4559 nparam = isl_basic_set_n_param(bset1);
4560 dim1 = isl_basic_set_n_dim(bset1);
4561 dim2 = isl_basic_set_n_dim(bset2);
4562 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
4563 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
4564 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
4565 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
4566 if (!bmap1 || !bmap2)
4568 bmap1 = isl_basic_map_cow(bmap1);
4569 bmap1 = isl_basic_map_extend(bmap1, nparam,
4570 pos, (dim1 - pos) + (dim2 - pos),
4571 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4572 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
4575 total = isl_basic_map_total_dim(bmap1);
4577 obj = isl_vec_alloc(ctx, 1 + total);
4578 isl_seq_clr(obj->block.data, 1 + total);
4579 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
4580 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
4585 res = isl_basic_map_solve_lp(bmap1, 0, obj->block.data, ctx->one,
4587 if (res == isl_lp_empty)
4589 else if (res == isl_lp_ok && isl_int_is_pos(num))
4591 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4592 res == isl_lp_unbounded)
4598 isl_basic_map_free(bmap1);
4602 isl_basic_map_free(bmap1);
4603 isl_basic_map_free(bmap2);
4607 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4608 unsigned pos, isl_int *val)
4616 total = isl_basic_map_total_dim(bmap);
4617 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4618 for (; d+1 > pos; --d)
4619 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4623 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4625 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4627 if (!isl_int_is_one(bmap->eq[i][1+d]))
4630 isl_int_neg(*val, bmap->eq[i][0]);
4636 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4637 unsigned pos, isl_int *val)
4649 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4652 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4653 for (i = 1; fixed == 1 && i < map->n; ++i) {
4654 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4655 if (fixed == 1 && isl_int_ne(tmp, v))
4659 isl_int_set(*val, v);
4665 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
4666 unsigned pos, isl_int *val)
4668 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
4672 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4675 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4678 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4679 enum isl_dim_type type, unsigned pos, isl_int *val)
4681 if (pos >= isl_basic_map_dim(bmap, type))
4683 return isl_basic_map_fast_has_fixed_var(bmap,
4684 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4687 int isl_map_fast_is_fixed(struct isl_map *map,
4688 enum isl_dim_type type, unsigned pos, isl_int *val)
4690 if (pos >= isl_map_dim(map, type))
4692 return isl_map_fast_has_fixed_var(map,
4693 map_offset(map, type) - 1 + pos, val);
4696 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4697 * then return this fixed value in *val.
4699 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
4702 return isl_basic_set_fast_has_fixed_var(bset,
4703 isl_basic_set_n_param(bset) + dim, val);
4706 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4707 * then return this fixed value in *val.
4709 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4711 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4714 /* Check if input variable in has fixed value and if so and if val is not NULL,
4715 * then return this fixed value in *val.
4717 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4719 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4722 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4723 * and if val is not NULL, then return this lower bound in *val.
4725 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4726 unsigned dim, isl_int *val)
4728 int i, i_eq = -1, i_ineq = -1;
4735 total = isl_basic_set_total_dim(bset);
4736 nparam = isl_basic_set_n_param(bset);
4737 for (i = 0; i < bset->n_eq; ++i) {
4738 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4744 for (i = 0; i < bset->n_ineq; ++i) {
4745 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4747 if (i_eq != -1 || i_ineq != -1)
4751 if (i_eq == -1 && i_ineq == -1)
4753 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4754 /* The coefficient should always be one due to normalization. */
4755 if (!isl_int_is_one(c[1+nparam+dim]))
4757 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4759 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4760 total - nparam - dim - 1) != -1)
4763 isl_int_neg(*val, c[0]);
4767 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4768 unsigned dim, isl_int *val)
4780 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4784 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4786 for (i = 1; fixed == 1 && i < set->n; ++i) {
4787 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4789 if (fixed == 1 && isl_int_ne(tmp, v))
4793 isl_int_set(*val, v);
4804 static int qsort_constraint_cmp(const void *p1, const void *p2)
4806 const struct constraint *c1 = (const struct constraint *)p1;
4807 const struct constraint *c2 = (const struct constraint *)p2;
4808 unsigned size = isl_min(c1->size, c2->size);
4809 return isl_seq_cmp(c1->c, c2->c, size);
4812 static struct isl_basic_map *isl_basic_map_sort_constraints(
4813 struct isl_basic_map *bmap)
4816 struct constraint *c;
4821 total = isl_basic_map_total_dim(bmap);
4822 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4825 for (i = 0; i < bmap->n_ineq; ++i) {
4827 c[i].c = bmap->ineq[i];
4829 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4830 for (i = 0; i < bmap->n_ineq; ++i)
4831 bmap->ineq[i] = c[i].c;
4835 isl_basic_map_free(bmap);
4839 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4843 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4845 bmap = isl_basic_map_convex_hull(bmap);
4846 bmap = isl_basic_map_sort_constraints(bmap);
4847 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4851 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4853 return (struct isl_basic_set *)isl_basic_map_normalize(
4854 (struct isl_basic_map *)bset);
4857 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4858 const struct isl_basic_map *bmap2)
4865 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4866 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4867 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4868 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4869 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4870 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4871 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4872 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4874 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4876 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4878 if (bmap1->n_eq != bmap2->n_eq)
4879 return bmap1->n_eq - bmap2->n_eq;
4880 if (bmap1->n_ineq != bmap2->n_ineq)
4881 return bmap1->n_ineq - bmap2->n_ineq;
4882 if (bmap1->n_div != bmap2->n_div)
4883 return bmap1->n_div - bmap2->n_div;
4884 total = isl_basic_map_total_dim(bmap1);
4885 for (i = 0; i < bmap1->n_eq; ++i) {
4886 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4890 for (i = 0; i < bmap1->n_ineq; ++i) {
4891 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4895 for (i = 0; i < bmap1->n_div; ++i) {
4896 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4903 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4904 struct isl_basic_map *bmap2)
4906 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4909 static int qsort_bmap_cmp(const void *p1, const void *p2)
4911 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4912 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4914 return isl_basic_map_fast_cmp(bmap1, bmap2);
4917 /* We normalize in place, but if anything goes wrong we need
4918 * to return NULL, so we need to make sure we don't change the
4919 * meaning of any possible other copies of map.
4921 struct isl_map *isl_map_normalize(struct isl_map *map)
4924 struct isl_basic_map *bmap;
4928 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4930 for (i = 0; i < map->n; ++i) {
4931 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4934 isl_basic_map_free(map->p[i]);
4937 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4938 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4939 map = isl_map_remove_empty_parts(map);
4942 for (i = map->n - 1; i >= 1; --i) {
4943 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4945 isl_basic_map_free(map->p[i-1]);
4946 for (j = i; j < map->n; ++j)
4947 map->p[j-1] = map->p[j];
4957 struct isl_set *isl_set_normalize(struct isl_set *set)
4959 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4962 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4972 if (!isl_dim_equal(map1->dim, map2->dim))
4975 map1 = isl_map_copy(map1);
4976 map2 = isl_map_copy(map2);
4977 map1 = isl_map_normalize(map1);
4978 map2 = isl_map_normalize(map2);
4981 equal = map1->n == map2->n;
4982 for (i = 0; equal && i < map1->n; ++i) {
4983 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4996 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4998 return isl_map_fast_is_equal((struct isl_map *)set1,
4999 (struct isl_map *)set2);
5002 /* Return an interval that ranges from min to max (inclusive)
5004 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
5005 isl_int min, isl_int max)
5008 struct isl_basic_set *bset = NULL;
5010 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
5014 k = isl_basic_set_alloc_inequality(bset);
5017 isl_int_set_si(bset->ineq[k][1], 1);
5018 isl_int_neg(bset->ineq[k][0], min);
5020 k = isl_basic_set_alloc_inequality(bset);
5023 isl_int_set_si(bset->ineq[k][1], -1);
5024 isl_int_set(bset->ineq[k][0], max);
5028 isl_basic_set_free(bset);
5032 /* Return the Cartesian product of the basic sets in list (in the given order).
5034 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
5042 struct isl_basic_set *product = NULL;
5046 isl_assert(list->ctx, list->n > 0, goto error);
5047 isl_assert(list->ctx, list->p[0], goto error);
5048 nparam = isl_basic_set_n_param(list->p[0]);
5049 dim = isl_basic_set_n_dim(list->p[0]);
5050 extra = list->p[0]->n_div;
5051 n_eq = list->p[0]->n_eq;
5052 n_ineq = list->p[0]->n_ineq;
5053 for (i = 1; i < list->n; ++i) {
5054 isl_assert(list->ctx, list->p[i], goto error);
5055 isl_assert(list->ctx,
5056 nparam == isl_basic_set_n_param(list->p[i]), goto error);
5057 dim += isl_basic_set_n_dim(list->p[i]);
5058 extra += list->p[i]->n_div;
5059 n_eq += list->p[i]->n_eq;
5060 n_ineq += list->p[i]->n_ineq;
5062 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
5067 for (i = 0; i < list->n; ++i) {
5068 isl_basic_set_add_constraints(product,
5069 isl_basic_set_copy(list->p[i]), dim);
5070 dim += isl_basic_set_n_dim(list->p[i]);
5072 isl_basic_set_list_free(list);
5075 isl_basic_set_free(product);
5076 isl_basic_set_list_free(list);
5080 struct isl_basic_map *isl_basic_map_product(
5081 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5083 struct isl_dim *dim_result = NULL;
5084 struct isl_basic_map *bmap;
5085 unsigned in1, in2, out1, out2, nparam, total, pos;
5086 struct isl_dim_map *dim_map1, *dim_map2;
5088 if (!bmap1 || !bmap2)
5091 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
5092 bmap2->dim, isl_dim_param), goto error);
5093 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
5094 isl_dim_copy(bmap2->dim));
5096 in1 = isl_basic_map_n_in(bmap1);
5097 in2 = isl_basic_map_n_in(bmap2);
5098 out1 = isl_basic_map_n_out(bmap1);
5099 out2 = isl_basic_map_n_out(bmap2);
5100 nparam = isl_basic_map_n_param(bmap1);
5102 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
5103 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5104 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
5105 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5106 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
5107 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5108 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
5109 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
5110 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
5111 isl_dim_map_div(dim_map1, bmap1, pos += out2);
5112 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5114 bmap = isl_basic_map_alloc_dim(dim_result,
5115 bmap1->n_div + bmap2->n_div,
5116 bmap1->n_eq + bmap2->n_eq,
5117 bmap1->n_ineq + bmap2->n_ineq);
5118 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
5119 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
5120 bmap = isl_basic_map_simplify(bmap);
5121 return isl_basic_map_finalize(bmap);
5123 isl_basic_map_free(bmap1);
5124 isl_basic_map_free(bmap2);
5128 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5130 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
5133 struct isl_map *result;
5139 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
5140 map2->dim, isl_dim_param), goto error);
5142 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5143 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5144 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5146 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
5147 isl_dim_copy(map2->dim)),
5148 map1->n * map2->n, flags);
5151 for (i = 0; i < map1->n; ++i)
5152 for (j = 0; j < map2->n; ++j) {
5153 struct isl_basic_map *part;
5154 part = isl_basic_map_product(
5155 isl_basic_map_copy(map1->p[i]),
5156 isl_basic_map_copy(map2->p[j]));
5157 if (isl_basic_map_is_empty(part))
5158 isl_basic_map_free(part);
5160 result = isl_map_add(result, part);
5173 /* Given two set A and B, construct its Cartesian product A x B.
5175 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
5177 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
5178 (struct isl_map *)set2);
5181 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
5184 uint32_t hash = isl_hash_init();
5189 bset = isl_basic_set_copy(bset);
5190 bset = isl_basic_set_normalize(bset);
5193 total = isl_basic_set_total_dim(bset);
5194 isl_hash_byte(hash, bset->n_eq & 0xFF);
5195 for (i = 0; i < bset->n_eq; ++i) {
5197 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
5198 isl_hash_hash(hash, c_hash);
5200 isl_hash_byte(hash, bset->n_ineq & 0xFF);
5201 for (i = 0; i < bset->n_ineq; ++i) {
5203 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
5204 isl_hash_hash(hash, c_hash);
5206 isl_hash_byte(hash, bset->n_div & 0xFF);
5207 for (i = 0; i < bset->n_div; ++i) {
5209 if (isl_int_is_zero(bset->div[i][0]))
5211 isl_hash_byte(hash, i & 0xFF);
5212 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
5213 isl_hash_hash(hash, c_hash);
5215 isl_basic_set_free(bset);
5219 uint32_t isl_set_get_hash(struct isl_set *set)
5226 set = isl_set_copy(set);
5227 set = isl_set_normalize(set);
5231 hash = isl_hash_init();
5232 for (i = 0; i < set->n; ++i) {
5234 bset_hash = isl_basic_set_get_hash(set->p[i]);
5235 isl_hash_hash(hash, bset_hash);
5243 /* Check if the value for dimension dim is completely determined
5244 * by the values of the other parameters and variables.
5245 * That is, check if dimension dim is involved in an equality.
5247 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
5254 nparam = isl_basic_set_n_param(bset);
5255 for (i = 0; i < bset->n_eq; ++i)
5256 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
5261 /* Check if the value for dimension dim is completely determined
5262 * by the values of the other parameters and variables.
5263 * That is, check if dimension dim is involved in an equality
5264 * for each of the subsets.
5266 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
5272 for (i = 0; i < set->n; ++i) {
5274 unique = isl_basic_set_dim_is_unique(set->p[i], dim);