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 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
648 bset = isl_basic_set_extend_constraints(bset, 1, 0);
651 k = isl_basic_set_alloc_equality(bset);
654 isl_seq_cpy(bset->eq[k], eq, 1 + isl_basic_set_total_dim(bset));
657 isl_basic_set_free(bset);
661 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
666 bset = isl_basic_set_extend_constraints(bset, 0, 1);
669 k = isl_basic_set_alloc_inequality(bset);
672 isl_seq_cpy(bset->ineq[k], ineq, 1 + isl_basic_set_total_dim(bset));
675 isl_basic_set_free(bset);
679 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
683 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
684 isl_seq_clr(bmap->div[bmap->n_div] +
685 1 + 1 + isl_basic_map_total_dim(bmap),
686 bmap->extra - bmap->n_div);
687 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
688 return bmap->n_div++;
691 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
693 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
696 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
700 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
705 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
707 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
710 /* Copy constraint from src to dst, putting the vars of src at offset
711 * dim_off in dst and the divs of src at offset div_off in dst.
712 * If both sets are actually map, then dim_off applies to the input
715 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
716 struct isl_basic_map *src_map, isl_int *src,
717 unsigned in_off, unsigned out_off, unsigned div_off)
719 unsigned src_nparam = isl_basic_map_n_param(src_map);
720 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
721 unsigned src_in = isl_basic_map_n_in(src_map);
722 unsigned dst_in = isl_basic_map_n_in(dst_map);
723 unsigned src_out = isl_basic_map_n_out(src_map);
724 unsigned dst_out = isl_basic_map_n_out(dst_map);
725 isl_int_set(dst[0], src[0]);
726 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
727 if (dst_nparam > src_nparam)
728 isl_seq_clr(dst+1+src_nparam,
729 dst_nparam - src_nparam);
730 isl_seq_clr(dst+1+dst_nparam, in_off);
731 isl_seq_cpy(dst+1+dst_nparam+in_off,
733 isl_min(dst_in-in_off, src_in));
734 if (dst_in-in_off > src_in)
735 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
736 dst_in - in_off - src_in);
737 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
738 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
739 src+1+src_nparam+src_in,
740 isl_min(dst_out-out_off, src_out));
741 if (dst_out-out_off > src_out)
742 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
743 dst_out - out_off - src_out);
744 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
745 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
746 src+1+src_nparam+src_in+src_out,
747 isl_min(dst_map->extra-div_off, src_map->n_div));
748 if (dst_map->n_div-div_off > src_map->n_div)
749 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
750 div_off+src_map->n_div,
751 dst_map->n_div - div_off - src_map->n_div);
754 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
755 struct isl_basic_map *src_map, isl_int *src,
756 unsigned in_off, unsigned out_off, unsigned div_off)
758 isl_int_set(dst[0], src[0]);
759 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
762 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
763 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
768 if (!bmap1 || !bmap2)
771 div_off = bmap1->n_div;
773 for (i = 0; i < bmap2->n_eq; ++i) {
774 int i1 = isl_basic_map_alloc_equality(bmap1);
777 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
778 i_pos, o_pos, div_off);
781 for (i = 0; i < bmap2->n_ineq; ++i) {
782 int i1 = isl_basic_map_alloc_inequality(bmap1);
785 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
786 i_pos, o_pos, div_off);
789 for (i = 0; i < bmap2->n_div; ++i) {
790 int i1 = isl_basic_map_alloc_div(bmap1);
793 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
794 i_pos, o_pos, div_off);
797 isl_basic_map_free(bmap2);
802 isl_basic_map_free(bmap1);
803 isl_basic_map_free(bmap2);
807 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
808 struct isl_dim_map *dim_map)
812 for (i = 0; i < dim_map->len; ++i) {
813 if (dim_map->pos[i] < 0)
814 isl_int_set_si(dst[i], 0);
816 isl_int_set(dst[i], src[dim_map->pos[i]]);
820 static void copy_div_dim_map(isl_int *dst, isl_int *src,
821 struct isl_dim_map *dim_map)
823 isl_int_set(dst[0], src[0]);
824 copy_constraint_dim_map(dst+1, src+1, dim_map);
827 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
828 struct isl_basic_map *src, struct isl_dim_map *dim_map)
832 if (!src || !dst || !dim_map)
835 for (i = 0; i < src->n_eq; ++i) {
836 int i1 = isl_basic_map_alloc_equality(dst);
839 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
842 for (i = 0; i < src->n_ineq; ++i) {
843 int i1 = isl_basic_map_alloc_inequality(dst);
846 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
849 for (i = 0; i < src->n_div; ++i) {
850 int i1 = isl_basic_map_alloc_div(dst);
853 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
857 isl_basic_map_free(src);
862 isl_basic_map_free(src);
863 isl_basic_map_free(dst);
867 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
868 struct isl_basic_set *bset2, unsigned pos)
870 return (struct isl_basic_set *)
871 add_constraints((struct isl_basic_map *)bset1,
872 (struct isl_basic_map *)bset2, 0, pos);
875 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
876 struct isl_dim *dim, unsigned extra,
877 unsigned n_eq, unsigned n_ineq)
879 struct isl_basic_map *ext;
889 dims_ok = isl_dim_equal(base->dim, dim) &&
890 base->extra >= base->n_div + extra;
892 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
893 room_for_ineq(base, n_ineq)) {
898 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
899 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
900 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
901 extra += base->extra;
903 n_ineq += base->n_ineq;
905 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
911 ext->sample = isl_vec_copy(base->sample);
913 ext = add_constraints(ext, base, 0, 0);
916 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
923 isl_basic_map_free(base);
927 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
928 struct isl_dim *dim, unsigned extra,
929 unsigned n_eq, unsigned n_ineq)
931 return (struct isl_basic_set *)
932 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
933 extra, n_eq, n_ineq);
936 struct isl_basic_map *isl_basic_map_extend_constraints(
937 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
941 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
945 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
946 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
947 unsigned n_eq, unsigned n_ineq)
949 struct isl_basic_map *bmap;
954 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
958 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
962 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
963 unsigned nparam, unsigned dim, unsigned extra,
964 unsigned n_eq, unsigned n_ineq)
966 return (struct isl_basic_set *)
967 isl_basic_map_extend((struct isl_basic_map *)base,
968 nparam, 0, dim, extra, n_eq, n_ineq);
971 struct isl_basic_set *isl_basic_set_extend_constraints(
972 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
974 return (struct isl_basic_set *)
975 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
979 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
981 return (struct isl_basic_set *)
982 isl_basic_map_cow((struct isl_basic_map *)bset);
985 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
992 bmap = isl_basic_map_dup(bmap);
994 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
998 struct isl_set *isl_set_cow(struct isl_set *set)
1006 return isl_set_dup(set);
1009 struct isl_map *isl_map_cow(struct isl_map *map)
1017 return isl_map_dup(map);
1020 static void swap_vars(struct isl_blk blk, isl_int *a,
1021 unsigned a_len, unsigned b_len)
1023 isl_seq_cpy(blk.data, a+a_len, b_len);
1024 isl_seq_cpy(blk.data+b_len, a, a_len);
1025 isl_seq_cpy(a, blk.data, b_len+a_len);
1028 struct isl_basic_set *isl_basic_set_swap_vars(
1029 struct isl_basic_set *bset, unsigned n)
1039 nparam = isl_basic_set_n_param(bset);
1040 dim = isl_basic_set_n_dim(bset);
1041 isl_assert(bset->ctx, n <= dim, goto error);
1046 bset = isl_basic_set_cow(bset);
1050 blk = isl_blk_alloc(bset->ctx, dim);
1051 if (isl_blk_is_error(blk))
1054 for (i = 0; i < bset->n_eq; ++i)
1056 bset->eq[i]+1+nparam, n, dim - n);
1058 for (i = 0; i < bset->n_ineq; ++i)
1060 bset->ineq[i]+1+nparam, n, dim - n);
1062 for (i = 0; i < bset->n_div; ++i)
1064 bset->div[i]+1+1+nparam, n, dim - n);
1066 isl_blk_free(bset->ctx, blk);
1068 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1072 isl_basic_set_free(bset);
1076 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1079 set = isl_set_cow(set);
1083 for (i = 0; i < set->n; ++i) {
1084 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1090 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1094 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1100 total = isl_basic_map_total_dim(bmap);
1101 isl_basic_map_free_div(bmap, bmap->n_div);
1102 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1104 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1106 isl_basic_map_alloc_equality(bmap);
1110 isl_int_set_si(bmap->eq[i][0], 1);
1111 isl_seq_clr(bmap->eq[i]+1, total);
1112 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1113 return isl_basic_map_finalize(bmap);
1115 isl_basic_map_free(bmap);
1119 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1121 return (struct isl_basic_set *)
1122 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1125 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1128 unsigned off = isl_dim_total(bmap->dim);
1129 isl_int *t = bmap->div[a];
1130 bmap->div[a] = bmap->div[b];
1133 for (i = 0; i < bmap->n_eq; ++i)
1134 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1136 for (i = 0; i < bmap->n_ineq; ++i)
1137 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1139 for (i = 0; i < bmap->n_div; ++i)
1140 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1141 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1144 /* Eliminate the specified n dimensions starting at first from the
1145 * constraints using Fourier-Motzkin, The dimensions themselves
1148 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1149 unsigned first, unsigned n)
1159 set = isl_set_cow(set);
1162 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1163 nparam = isl_set_n_param(set);
1165 for (i = 0; i < set->n; ++i) {
1166 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1177 /* Project out n dimensions starting at first using Fourier-Motzkin */
1178 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1179 unsigned first, unsigned n)
1181 set = isl_set_eliminate_dims(set, first, n);
1182 set = isl_set_drop_dims(set, first, n);
1186 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1188 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1196 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1205 set = isl_set_cow(set);
1209 for (i = 0; i < set->n; ++i) {
1210 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1220 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1221 enum isl_dim_type type, unsigned first, unsigned n)
1225 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1229 bmap = isl_basic_map_eliminate_vars(bmap,
1230 isl_basic_map_offset(bmap, type) - 1 + first, n);
1233 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1235 bmap = isl_basic_map_drop(bmap, type, first, n);
1238 isl_basic_map_free(bmap);
1242 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1243 enum isl_dim_type type, unsigned first, unsigned n)
1245 return (isl_basic_set *)
1246 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1249 struct isl_map *isl_map_remove(struct isl_map *map,
1250 enum isl_dim_type type, unsigned first, unsigned n)
1257 map = isl_map_cow(map);
1260 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1262 for (i = 0; i < map->n; ++i) {
1263 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1264 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1268 map = isl_map_drop(map, type, first, n);
1275 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1276 enum isl_dim_type type, unsigned first, unsigned n)
1278 return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1281 /* Project out n inputs starting at first using Fourier-Motzkin */
1282 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1283 unsigned first, unsigned n)
1285 return isl_map_remove(map, isl_dim_in, first, n);
1288 /* Project out n dimensions starting at first using Fourier-Motzkin */
1289 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1290 unsigned first, unsigned n)
1292 unsigned nparam = isl_basic_set_n_param(bset);
1293 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1294 bset = isl_basic_set_drop_dims(bset, first, n);
1298 static void dump_term(struct isl_basic_map *bmap,
1299 isl_int c, int pos, FILE *out)
1302 unsigned in = isl_basic_map_n_in(bmap);
1303 unsigned dim = in + isl_basic_map_n_out(bmap);
1304 unsigned nparam = isl_basic_map_n_param(bmap);
1306 isl_int_print(out, c, 0);
1308 if (!isl_int_is_one(c))
1309 isl_int_print(out, c, 0);
1310 if (pos < 1 + nparam) {
1311 name = isl_dim_get_name(bmap->dim,
1312 isl_dim_param, pos - 1);
1314 fprintf(out, "%s", name);
1316 fprintf(out, "p%d", pos - 1);
1317 } else if (pos < 1 + nparam + in)
1318 fprintf(out, "i%d", pos - 1 - nparam);
1319 else if (pos < 1 + nparam + dim)
1320 fprintf(out, "o%d", pos - 1 - nparam - in);
1322 fprintf(out, "e%d", pos - 1 - nparam - dim);
1326 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1327 int sign, FILE *out)
1331 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1335 for (i = 0, first = 1; i < len; ++i) {
1336 if (isl_int_sgn(c[i]) * sign <= 0)
1339 fprintf(out, " + ");
1341 isl_int_abs(v, c[i]);
1342 dump_term(bmap, v, i, out);
1349 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1350 const char *op, FILE *out, int indent)
1354 fprintf(out, "%*s", indent, "");
1356 dump_constraint_sign(bmap, c, 1, out);
1357 fprintf(out, " %s ", op);
1358 dump_constraint_sign(bmap, c, -1, out);
1362 for (i = bmap->n_div; i < bmap->extra; ++i) {
1363 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1365 fprintf(out, "%*s", indent, "");
1366 fprintf(out, "ERROR: unused div coefficient not zero\n");
1371 static void dump_constraints(struct isl_basic_map *bmap,
1372 isl_int **c, unsigned n,
1373 const char *op, FILE *out, int indent)
1377 for (i = 0; i < n; ++i)
1378 dump_constraint(bmap, c[i], op, out, indent);
1381 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1385 unsigned total = isl_basic_map_total_dim(bmap);
1387 for (j = 0; j < 1 + total; ++j) {
1388 if (isl_int_is_zero(exp[j]))
1390 if (!first && isl_int_is_pos(exp[j]))
1392 dump_term(bmap, exp[j], j, out);
1397 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1401 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1402 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1404 for (i = 0; i < bmap->n_div; ++i) {
1405 fprintf(out, "%*s", indent, "");
1406 fprintf(out, "e%d = [(", i);
1407 dump_affine(bmap, bmap->div[i]+1, out);
1409 isl_int_print(out, bmap->div[i][0], 0);
1410 fprintf(out, "]\n");
1414 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1417 fprintf(out, "null basic set\n");
1421 fprintf(out, "%*s", indent, "");
1422 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1423 bset->ref, bset->dim->nparam, bset->dim->n_out,
1424 bset->extra, bset->flags);
1425 dump((struct isl_basic_map *)bset, out, indent);
1428 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1431 fprintf(out, "null basic map\n");
1435 fprintf(out, "%*s", indent, "");
1436 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1437 "flags: %x, n_name: %d\n",
1439 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1440 bmap->extra, bmap->flags, bmap->dim->n_name);
1441 dump(bmap, out, indent);
1444 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1449 total = isl_basic_map_total_dim(bmap);
1450 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1451 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1452 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1453 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1457 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1459 struct isl_set *set;
1463 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1464 isl_assert(dim->ctx, n >= 0, return NULL);
1465 set = isl_alloc(dim->ctx, struct isl_set,
1466 sizeof(struct isl_set) +
1467 (n - 1) * sizeof(struct isl_basic_set *));
1471 set->ctx = dim->ctx;
1472 isl_ctx_ref(set->ctx);
1484 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1485 unsigned nparam, unsigned dim, int n, unsigned flags)
1487 struct isl_set *set;
1488 struct isl_dim *dims;
1490 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1494 set = isl_set_alloc_dim(dims, n, flags);
1498 /* Make sure "map" has room for at least "n" more basic maps.
1500 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1503 struct isl_map *grown = NULL;
1507 isl_assert(map->ctx, n >= 0, goto error);
1508 if (map->n + n <= map->size)
1510 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1513 for (i = 0; i < map->n; ++i) {
1514 grown->p[i] = isl_basic_map_copy(map->p[i]);
1522 isl_map_free(grown);
1527 /* Make sure "set" has room for at least "n" more basic sets.
1529 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1531 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1534 struct isl_set *isl_set_dup(struct isl_set *set)
1537 struct isl_set *dup;
1542 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1545 for (i = 0; i < set->n; ++i)
1546 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1550 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1552 struct isl_set *set;
1557 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1559 isl_basic_set_free(bset);
1562 return isl_set_add(set, bset);
1565 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1567 struct isl_map *map;
1572 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1574 isl_basic_map_free(bmap);
1577 return isl_map_add(map, bmap);
1580 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1582 return (struct isl_set *)isl_map_add((struct isl_map *)set,
1583 (struct isl_basic_map *)bset);
1586 void isl_set_free(struct isl_set *set)
1596 isl_ctx_deref(set->ctx);
1597 for (i = 0; i < set->n; ++i)
1598 isl_basic_set_free(set->p[i]);
1599 isl_dim_free(set->dim);
1603 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1608 fprintf(out, "null set\n");
1612 fprintf(out, "%*s", indent, "");
1613 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1614 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1616 for (i = 0; i < set->n; ++i) {
1617 fprintf(out, "%*s", indent, "");
1618 fprintf(out, "basic set %d:\n", i);
1619 isl_basic_set_dump(set->p[i], out, indent+4);
1623 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1628 fprintf(out, "null map\n");
1632 fprintf(out, "%*s", indent, "");
1633 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1634 "flags: %x, n_name: %d\n",
1635 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1636 map->dim->n_out, map->flags, map->dim->n_name);
1637 for (i = 0; i < map->n; ++i) {
1638 fprintf(out, "%*s", indent, "");
1639 fprintf(out, "basic map %d:\n", i);
1640 isl_basic_map_dump(map->p[i], out, indent+4);
1644 struct isl_basic_map *isl_basic_map_intersect_domain(
1645 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1647 struct isl_basic_map *bmap_domain;
1648 struct isl_dim *dim;
1653 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1654 bset->dim, isl_dim_param), goto error);
1656 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1657 isl_assert(bset->ctx,
1658 isl_basic_map_compatible_domain(bmap, bset), goto error);
1660 bmap = isl_basic_map_cow(bmap);
1661 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1662 bset->n_div, bset->n_eq, bset->n_ineq);
1665 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1666 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1667 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1669 bmap = isl_basic_map_simplify(bmap);
1670 return isl_basic_map_finalize(bmap);
1672 isl_basic_map_free(bmap);
1673 isl_basic_set_free(bset);
1677 struct isl_basic_map *isl_basic_map_intersect_range(
1678 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1680 struct isl_basic_map *bmap_range;
1685 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1686 bset->dim, isl_dim_param), goto error);
1688 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1689 isl_assert(bset->ctx,
1690 isl_basic_map_compatible_range(bmap, bset), goto error);
1692 bmap = isl_basic_map_cow(bmap);
1693 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1694 bset->n_div, bset->n_eq, bset->n_ineq);
1697 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1698 bmap = add_constraints(bmap, bmap_range, 0, 0);
1700 bmap = isl_basic_map_simplify(bmap);
1701 return isl_basic_map_finalize(bmap);
1703 isl_basic_map_free(bmap);
1704 isl_basic_set_free(bset);
1708 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1714 total = 1 + isl_basic_map_total_dim(bmap);
1715 if (total != vec->size)
1720 for (i = 0; i < bmap->n_eq; ++i) {
1721 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1722 if (!isl_int_is_zero(s)) {
1728 for (i = 0; i < bmap->n_ineq; ++i) {
1729 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1730 if (isl_int_is_neg(s)) {
1741 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1743 return basic_map_contains((struct isl_basic_map *)bset, vec);
1746 struct isl_basic_map *isl_basic_map_intersect(
1747 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1749 struct isl_vec *sample = NULL;
1751 if (!bmap1 || !bmap2)
1754 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1755 bmap2->dim, isl_dim_param), goto error);
1756 if (isl_dim_total(bmap1->dim) ==
1757 isl_dim_size(bmap1->dim, isl_dim_param) &&
1758 isl_dim_total(bmap2->dim) !=
1759 isl_dim_size(bmap2->dim, isl_dim_param))
1760 return isl_basic_map_intersect(bmap2, bmap1);
1762 if (isl_dim_total(bmap2->dim) !=
1763 isl_dim_size(bmap2->dim, isl_dim_param))
1764 isl_assert(bmap1->ctx,
1765 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1767 if (bmap1->sample &&
1768 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1769 basic_map_contains(bmap2, bmap1->sample) > 0)
1770 sample = isl_vec_copy(bmap1->sample);
1771 else if (bmap2->sample &&
1772 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1773 basic_map_contains(bmap2, bmap2->sample) > 0)
1774 sample = isl_vec_copy(bmap2->sample);
1776 bmap1 = isl_basic_map_cow(bmap1);
1777 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1778 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1781 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1784 isl_vec_free(bmap1->sample);
1785 bmap1->sample = sample;
1788 bmap1 = isl_basic_map_simplify(bmap1);
1789 return isl_basic_map_finalize(bmap1);
1792 isl_vec_free(sample);
1793 isl_basic_map_free(bmap1);
1794 isl_basic_map_free(bmap2);
1798 struct isl_basic_set *isl_basic_set_intersect(
1799 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1801 return (struct isl_basic_set *)
1802 isl_basic_map_intersect(
1803 (struct isl_basic_map *)bset1,
1804 (struct isl_basic_map *)bset2);
1807 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1810 struct isl_map *result;
1816 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1817 map2->dim, isl_dim_param), goto error);
1818 if (isl_dim_total(map1->dim) ==
1819 isl_dim_size(map1->dim, isl_dim_param) &&
1820 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1821 return isl_map_intersect(map2, map1);
1823 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1824 isl_assert(map1->ctx,
1825 isl_dim_equal(map1->dim, map2->dim), goto error);
1827 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1828 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1829 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1831 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1832 map1->n * map2->n, flags);
1835 for (i = 0; i < map1->n; ++i)
1836 for (j = 0; j < map2->n; ++j) {
1837 struct isl_basic_map *part;
1838 part = isl_basic_map_intersect(
1839 isl_basic_map_copy(map1->p[i]),
1840 isl_basic_map_copy(map2->p[j]));
1841 if (isl_basic_map_is_empty(part))
1842 isl_basic_map_free(part);
1844 result = isl_map_add(result, part);
1857 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1859 return (struct isl_set *)
1860 isl_map_intersect((struct isl_map *)set1,
1861 (struct isl_map *)set2);
1864 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1866 struct isl_dim *dim;
1867 struct isl_basic_set *bset;
1872 bmap = isl_basic_map_cow(bmap);
1875 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1876 in = isl_basic_map_n_in(bmap);
1877 bset = isl_basic_set_from_basic_map(bmap);
1878 bset = isl_basic_set_swap_vars(bset, in);
1879 return isl_basic_map_from_basic_set(bset, dim);
1882 /* Turn final n dimensions into existentially quantified variables.
1884 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
1885 enum isl_dim_type type, unsigned first, unsigned n)
1895 isl_assert(bset->ctx, type == isl_dim_set, goto error);
1896 isl_assert(bset->ctx, first + n == isl_basic_set_n_dim(bset), goto error);
1901 if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL))
1902 return isl_basic_set_remove(bset, type, first, n);
1904 bset = isl_basic_set_cow(bset);
1906 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1907 old = bset->block2.data;
1908 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1909 (bset->extra + n) * (1 + row_size));
1910 if (!bset->block2.data)
1912 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1915 for (i = 0; i < n; ++i) {
1916 new_div[i] = bset->block2.data +
1917 (bset->extra + i) * (1 + row_size);
1918 isl_seq_clr(new_div[i], 1 + row_size);
1920 for (i = 0; i < bset->extra; ++i)
1921 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1923 bset->div = new_div;
1926 bset->dim = isl_dim_drop_outputs(bset->dim,
1927 isl_basic_set_n_dim(bset) - n, n);
1930 bset = isl_basic_set_simplify(bset);
1931 bset = isl_basic_set_drop_redundant_divs(bset);
1932 return isl_basic_set_finalize(bset);
1934 isl_basic_set_free(bset);
1938 /* Turn final n dimensions into existentially quantified variables.
1940 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
1941 enum isl_dim_type type, unsigned first, unsigned n)
1948 isl_assert(set->ctx, type == isl_dim_set, goto error);
1949 isl_assert(set->ctx, first + n == isl_set_n_dim(set), goto error);
1954 set = isl_set_cow(set);
1957 set->dim = isl_dim_drop_outputs(set->dim, first, n);
1958 for (i = 0; i < set->n; ++i) {
1959 set->p[i] = isl_basic_set_project_out(set->p[i], type, first, n);
1964 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1971 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1975 for (i = 0; i < n; ++i) {
1976 j = isl_basic_map_alloc_div(bmap);
1979 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1983 isl_basic_map_free(bmap);
1987 struct isl_basic_map *isl_basic_map_apply_range(
1988 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1990 struct isl_dim *dim_result = NULL;
1991 struct isl_basic_map *bmap;
1992 unsigned n_in, n_out, n, nparam, total, pos;
1993 struct isl_dim_map *dim_map1, *dim_map2;
1995 if (!bmap1 || !bmap2)
1998 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1999 isl_dim_copy(bmap2->dim));
2001 n_in = isl_basic_map_n_in(bmap1);
2002 n_out = isl_basic_map_n_out(bmap2);
2003 n = isl_basic_map_n_out(bmap1);
2004 nparam = isl_basic_map_n_param(bmap1);
2006 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2007 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2008 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2009 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2010 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2011 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2012 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2013 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2014 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2015 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2016 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2018 bmap = isl_basic_map_alloc_dim(dim_result,
2019 bmap1->n_div + bmap2->n_div + n,
2020 bmap1->n_eq + bmap2->n_eq,
2021 bmap1->n_ineq + bmap2->n_ineq);
2022 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2023 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2024 bmap = add_divs(bmap, n);
2025 bmap = isl_basic_map_simplify(bmap);
2026 bmap = isl_basic_map_drop_redundant_divs(bmap);
2027 return isl_basic_map_finalize(bmap);
2029 isl_basic_map_free(bmap1);
2030 isl_basic_map_free(bmap2);
2034 struct isl_basic_set *isl_basic_set_apply(
2035 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2040 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2043 return (struct isl_basic_set *)
2044 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2046 isl_basic_set_free(bset);
2047 isl_basic_map_free(bmap);
2051 struct isl_basic_map *isl_basic_map_apply_domain(
2052 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2054 if (!bmap1 || !bmap2)
2057 isl_assert(bmap1->ctx,
2058 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2059 isl_assert(bmap1->ctx,
2060 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2063 bmap1 = isl_basic_map_reverse(bmap1);
2064 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2065 return isl_basic_map_reverse(bmap1);
2067 isl_basic_map_free(bmap1);
2068 isl_basic_map_free(bmap2);
2072 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2073 * A \cap B -> f(A) + f(B)
2075 struct isl_basic_map *isl_basic_map_sum(
2076 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2078 unsigned n_in, n_out, nparam, total, pos;
2079 struct isl_basic_map *bmap = NULL;
2080 struct isl_dim_map *dim_map1, *dim_map2;
2083 if (!bmap1 || !bmap2)
2086 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2089 nparam = isl_basic_map_n_param(bmap1);
2090 n_in = isl_basic_map_n_in(bmap1);
2091 n_out = isl_basic_map_n_out(bmap1);
2093 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2094 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2095 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2096 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2097 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2098 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2099 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2100 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2101 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2102 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2103 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2105 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2106 bmap1->n_div + bmap2->n_div + 2 * n_out,
2107 bmap1->n_eq + bmap2->n_eq + n_out,
2108 bmap1->n_ineq + bmap2->n_ineq);
2109 for (i = 0; i < n_out; ++i) {
2110 int j = isl_basic_map_alloc_equality(bmap);
2113 isl_seq_clr(bmap->eq[j], 1+total);
2114 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2115 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2116 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2118 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2119 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2120 bmap = add_divs(bmap, 2 * n_out);
2122 bmap = isl_basic_map_simplify(bmap);
2123 return isl_basic_map_finalize(bmap);
2125 isl_basic_map_free(bmap);
2126 isl_basic_map_free(bmap1);
2127 isl_basic_map_free(bmap2);
2131 /* Given two maps A -> f(A) and B -> g(B), construct a map
2132 * A \cap B -> f(A) + f(B)
2134 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2136 struct isl_map *result;
2142 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2144 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2145 map1->n * map2->n, 0);
2148 for (i = 0; i < map1->n; ++i)
2149 for (j = 0; j < map2->n; ++j) {
2150 struct isl_basic_map *part;
2151 part = isl_basic_map_sum(
2152 isl_basic_map_copy(map1->p[i]),
2153 isl_basic_map_copy(map2->p[j]));
2154 if (isl_basic_map_is_empty(part))
2155 isl_basic_map_free(part);
2157 result = isl_map_add(result, part);
2170 /* Given a basic map A -> f(A), construct A -> -f(A).
2172 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2177 bmap = isl_basic_map_cow(bmap);
2181 n = isl_basic_map_dim(bmap, isl_dim_out);
2182 off = isl_basic_map_offset(bmap, isl_dim_out);
2183 for (i = 0; i < bmap->n_eq; ++i)
2184 for (j = 0; j < n; ++j)
2185 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2186 for (i = 0; i < bmap->n_ineq; ++i)
2187 for (j = 0; j < n; ++j)
2188 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2189 for (i = 0; i < bmap->n_div; ++i)
2190 for (j = 0; j < n; ++j)
2191 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2192 return isl_basic_map_finalize(bmap);
2195 /* Given a map A -> f(A), construct A -> -f(A).
2197 struct isl_map *isl_map_neg(struct isl_map *map)
2201 map = isl_map_cow(map);
2205 for (i = 0; i < map->n; ++i) {
2206 map->p[i] = isl_basic_map_neg(map->p[i]);
2217 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2218 * A -> floor(f(A)/d).
2220 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2223 unsigned n_in, n_out, nparam, total, pos;
2224 struct isl_basic_map *result = NULL;
2225 struct isl_dim_map *dim_map;
2231 nparam = isl_basic_map_n_param(bmap);
2232 n_in = isl_basic_map_n_in(bmap);
2233 n_out = isl_basic_map_n_out(bmap);
2235 total = nparam + n_in + n_out + bmap->n_div + n_out;
2236 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2237 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2238 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2239 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2240 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2242 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2243 bmap->n_div + n_out,
2244 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2245 result = add_constraints_dim_map(result, bmap, dim_map);
2246 result = add_divs(result, n_out);
2247 for (i = 0; i < n_out; ++i) {
2249 j = isl_basic_map_alloc_inequality(result);
2252 isl_seq_clr(result->ineq[j], 1+total);
2253 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2254 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2255 j = isl_basic_map_alloc_inequality(result);
2258 isl_seq_clr(result->ineq[j], 1+total);
2259 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2260 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2261 isl_int_sub_ui(result->ineq[j][0], d, 1);
2264 result = isl_basic_map_simplify(result);
2265 return isl_basic_map_finalize(result);
2267 isl_basic_map_free(result);
2271 /* Given a map A -> f(A) and an integer d, construct a map
2272 * A -> floor(f(A)/d).
2274 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2278 map = isl_map_cow(map);
2282 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2283 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2284 for (i = 0; i < map->n; ++i) {
2285 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2296 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2302 i = isl_basic_map_alloc_equality(bmap);
2305 nparam = isl_basic_map_n_param(bmap);
2306 n_in = isl_basic_map_n_in(bmap);
2307 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2308 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2309 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2310 return isl_basic_map_finalize(bmap);
2312 isl_basic_map_free(bmap);
2316 /* Add a constraints to "bmap" expressing i_pos < o_pos
2318 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2324 i = isl_basic_map_alloc_inequality(bmap);
2327 nparam = isl_basic_map_n_param(bmap);
2328 n_in = isl_basic_map_n_in(bmap);
2329 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2330 isl_int_set_si(bmap->ineq[i][0], -1);
2331 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2332 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2333 return isl_basic_map_finalize(bmap);
2335 isl_basic_map_free(bmap);
2339 /* Add a constraints to "bmap" expressing i_pos > o_pos
2341 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2347 i = isl_basic_map_alloc_inequality(bmap);
2350 nparam = isl_basic_map_n_param(bmap);
2351 n_in = isl_basic_map_n_in(bmap);
2352 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2353 isl_int_set_si(bmap->ineq[i][0], -1);
2354 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2355 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2356 return isl_basic_map_finalize(bmap);
2358 isl_basic_map_free(bmap);
2362 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2365 struct isl_basic_map *bmap;
2366 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2369 for (i = 0; i < n_equal && bmap; ++i)
2370 bmap = var_equal(bmap, i);
2371 return isl_basic_map_finalize(bmap);
2374 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2376 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2379 struct isl_basic_map *bmap;
2380 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2383 for (i = 0; i < pos && bmap; ++i)
2384 bmap = var_equal(bmap, i);
2386 bmap = var_less(bmap, pos);
2387 return isl_basic_map_finalize(bmap);
2390 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2392 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2395 struct isl_basic_map *bmap;
2396 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2399 for (i = 0; i < pos && bmap; ++i)
2400 bmap = var_equal(bmap, i);
2402 bmap = var_more(bmap, pos);
2403 return isl_basic_map_finalize(bmap);
2406 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
2408 struct isl_map *map;
2415 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2417 for (i = 0; i < dim; ++i)
2418 map = isl_map_add(map,
2419 isl_basic_map_less_at(isl_dim_copy(dims), i));
2421 map = isl_map_add(map,
2422 isl_basic_map_equal(isl_dim_copy(dims), dim));
2428 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
2430 return map_lex_lte(isl_dim_map(set_dim), 0);
2433 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
2435 return map_lex_lte(isl_dim_map(set_dim), 1);
2438 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
2440 struct isl_map *map;
2447 map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2449 for (i = 0; i < dim; ++i)
2450 map = isl_map_add(map,
2451 isl_basic_map_more_at(isl_dim_copy(dims), i));
2453 map = isl_map_add(map,
2454 isl_basic_map_equal(isl_dim_copy(dims), dim));
2460 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
2462 return map_lex_gte(isl_dim_map(set_dim), 0);
2465 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
2467 return map_lex_gte(isl_dim_map(set_dim), 1);
2470 struct isl_basic_map *isl_basic_map_from_basic_set(
2471 struct isl_basic_set *bset, struct isl_dim *dim)
2473 struct isl_basic_map *bmap;
2475 bset = isl_basic_set_cow(bset);
2479 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2480 isl_dim_free(bset->dim);
2481 bmap = (struct isl_basic_map *) bset;
2483 return isl_basic_map_finalize(bmap);
2485 isl_basic_set_free(bset);
2490 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2494 if (bmap->dim->n_in == 0)
2495 return (struct isl_basic_set *)bmap;
2496 bmap = isl_basic_map_cow(bmap);
2499 bmap->dim = isl_dim_cow(bmap->dim);
2502 bmap->dim->n_out += bmap->dim->n_in;
2503 bmap->dim->n_in = 0;
2504 bmap = isl_basic_map_finalize(bmap);
2505 return (struct isl_basic_set *)bmap;
2507 isl_basic_map_free(bmap);
2511 /* For a div d = floor(f/m), add the constraints
2514 * -(f-(n-1)) + m d >= 0
2516 * Note that the second constraint is the negation of
2520 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2523 unsigned total = isl_basic_map_total_dim(bmap);
2524 unsigned div_pos = 1 + total - bmap->n_div + div;
2526 i = isl_basic_map_alloc_inequality(bmap);
2529 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2530 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2532 j = isl_basic_map_alloc_inequality(bmap);
2535 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2536 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2537 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2541 struct isl_basic_set *isl_basic_map_underlying_set(
2542 struct isl_basic_map *bmap)
2546 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2547 return (struct isl_basic_set *)bmap;
2548 bmap = isl_basic_map_cow(bmap);
2551 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2554 bmap->extra -= bmap->n_div;
2556 bmap = isl_basic_map_finalize(bmap);
2557 return (struct isl_basic_set *)bmap;
2562 __isl_give isl_basic_set *isl_basic_set_underlying_set(
2563 __isl_take isl_basic_set *bset)
2565 return isl_basic_map_underlying_set((isl_basic_map *)bset);
2568 struct isl_basic_map *isl_basic_map_overlying_set(
2569 struct isl_basic_set *bset, struct isl_basic_map *like)
2571 struct isl_basic_map *bmap;
2572 struct isl_ctx *ctx;
2579 isl_assert(ctx, bset->n_div == 0, goto error);
2580 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2581 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2583 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2584 isl_basic_map_free(like);
2585 return (struct isl_basic_map *)bset;
2587 bset = isl_basic_set_cow(bset);
2590 total = bset->dim->n_out + bset->extra;
2591 bmap = (struct isl_basic_map *)bset;
2592 isl_dim_free(bmap->dim);
2593 bmap->dim = isl_dim_copy(like->dim);
2596 bmap->n_div = like->n_div;
2597 bmap->extra += like->n_div;
2600 ltotal = total - bmap->extra + like->extra;
2603 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2604 bmap->extra * (1 + 1 + total));
2605 if (isl_blk_is_error(bmap->block2))
2607 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2611 for (i = 0; i < bmap->extra; ++i)
2612 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2613 for (i = 0; i < like->n_div; ++i) {
2614 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2615 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2617 bmap = isl_basic_map_extend_constraints(bmap,
2618 0, 2 * like->n_div);
2619 for (i = 0; i < like->n_div; ++i) {
2620 if (isl_int_is_zero(bmap->div[i][0]))
2622 if (add_div_constraints(bmap, i) < 0)
2626 isl_basic_map_free(like);
2627 bmap = isl_basic_map_simplify(bmap);
2628 bmap = isl_basic_map_finalize(bmap);
2631 isl_basic_map_free(like);
2632 isl_basic_set_free(bset);
2636 struct isl_basic_set *isl_basic_set_from_underlying_set(
2637 struct isl_basic_set *bset, struct isl_basic_set *like)
2639 return (struct isl_basic_set *)
2640 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2643 struct isl_set *isl_set_from_underlying_set(
2644 struct isl_set *set, struct isl_basic_set *like)
2650 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2652 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2653 isl_basic_set_free(like);
2656 set = isl_set_cow(set);
2659 for (i = 0; i < set->n; ++i) {
2660 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2661 isl_basic_set_copy(like));
2665 isl_dim_free(set->dim);
2666 set->dim = isl_dim_copy(like->dim);
2669 isl_basic_set_free(like);
2672 isl_basic_set_free(like);
2677 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2681 map = isl_map_cow(map);
2684 map->dim = isl_dim_cow(map->dim);
2688 for (i = 1; i < map->n; ++i)
2689 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2691 for (i = 0; i < map->n; ++i) {
2692 map->p[i] = (struct isl_basic_map *)
2693 isl_basic_map_underlying_set(map->p[i]);
2698 map->dim = isl_dim_underlying(map->dim, 0);
2700 isl_dim_free(map->dim);
2701 map->dim = isl_dim_copy(map->p[0]->dim);
2705 return (struct isl_set *)map;
2711 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2713 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2716 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2718 struct isl_basic_set *domain;
2723 n_in = isl_basic_map_n_in(bmap);
2724 n_out = isl_basic_map_n_out(bmap);
2725 domain = isl_basic_set_from_basic_map(bmap);
2726 return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
2729 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2731 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2734 struct isl_set *isl_map_range(struct isl_map *map)
2737 struct isl_set *set;
2741 map = isl_map_cow(map);
2745 set = (struct isl_set *) map;
2746 if (set->dim->n_in != 0) {
2747 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2751 for (i = 0; i < map->n; ++i) {
2752 set->p[i] = isl_basic_map_range(map->p[i]);
2756 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2757 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2764 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2767 struct isl_map *map = NULL;
2769 set = isl_set_cow(set);
2772 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2773 map = (struct isl_map *)set;
2774 for (i = 0; i < set->n; ++i) {
2775 map->p[i] = isl_basic_map_from_basic_set(
2776 set->p[i], isl_dim_copy(dim));
2780 isl_dim_free(map->dim);
2789 struct isl_map *isl_map_from_range(struct isl_set *set)
2791 return (struct isl_map *)set;
2794 struct isl_set *isl_set_from_map(struct isl_map *map)
2797 struct isl_set *set = NULL;
2801 map = isl_map_cow(map);
2804 map->dim = isl_dim_cow(map->dim);
2807 map->dim->n_out += map->dim->n_in;
2809 set = (struct isl_set *)map;
2810 for (i = 0; i < map->n; ++i) {
2811 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2821 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2823 struct isl_map *map;
2827 isl_assert(dim->ctx, n >= 0, return NULL);
2828 map = isl_alloc(dim->ctx, struct isl_map,
2829 sizeof(struct isl_map) +
2830 (n - 1) * sizeof(struct isl_basic_map *));
2834 map->ctx = dim->ctx;
2835 isl_ctx_ref(map->ctx);
2847 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2848 unsigned nparam, unsigned in, unsigned out, int n,
2851 struct isl_map *map;
2852 struct isl_dim *dims;
2854 dims = isl_dim_alloc(ctx, nparam, in, out);
2858 map = isl_map_alloc_dim(dims, n, flags);
2862 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
2864 struct isl_basic_map *bmap;
2865 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
2866 bmap = isl_basic_map_set_to_empty(bmap);
2870 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2872 struct isl_basic_set *bset;
2873 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2874 bset = isl_basic_set_set_to_empty(bset);
2878 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2880 struct isl_basic_map *bmap;
2883 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2884 bmap = isl_basic_map_set_to_empty(bmap);
2888 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2890 struct isl_basic_map *bmap;
2893 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2894 bmap = isl_basic_map_set_to_empty(bmap);
2898 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2900 struct isl_basic_set *bset;
2903 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2904 bset = isl_basic_set_set_to_empty(bset);
2908 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2910 struct isl_basic_map *bmap;
2911 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2915 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2917 struct isl_basic_set *bset;
2918 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2922 __isl_give isl_basic_map *isl_basic_map_universe_like(
2923 __isl_keep isl_basic_map *model)
2927 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2930 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2934 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2937 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
2938 __isl_keep isl_set *model)
2942 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2945 struct isl_map *isl_map_empty(struct isl_dim *dim)
2947 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2950 struct isl_map *isl_map_empty_like(struct isl_map *model)
2954 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2957 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2961 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2964 struct isl_set *isl_set_empty(struct isl_dim *dim)
2966 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2969 struct isl_set *isl_set_empty_like(struct isl_set *model)
2973 return isl_set_empty(isl_dim_copy(model->dim));
2976 struct isl_map *isl_map_universe(struct isl_dim *dim)
2978 struct isl_map *map;
2981 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2982 map = isl_map_add(map, isl_basic_map_universe(dim));
2986 struct isl_set *isl_set_universe(struct isl_dim *dim)
2988 struct isl_set *set;
2991 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2992 set = isl_set_add(set, isl_basic_set_universe(dim));
2996 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3000 return isl_set_universe(isl_dim_copy(model->dim));
3003 struct isl_map *isl_map_dup(struct isl_map *map)
3006 struct isl_map *dup;
3010 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3011 for (i = 0; i < map->n; ++i)
3012 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
3016 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
3020 if (isl_basic_map_fast_is_empty(bmap)) {
3021 isl_basic_map_free(bmap);
3024 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3025 isl_assert(map->ctx, map->n < map->size, goto error);
3026 map->p[map->n] = bmap;
3028 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3034 isl_basic_map_free(bmap);
3038 void isl_map_free(struct isl_map *map)
3048 isl_ctx_deref(map->ctx);
3049 for (i = 0; i < map->n; ++i)
3050 isl_basic_map_free(map->p[i]);
3051 isl_dim_free(map->dim);
3055 struct isl_map *isl_map_extend(struct isl_map *base,
3056 unsigned nparam, unsigned n_in, unsigned n_out)
3060 base = isl_map_cow(base);
3064 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3067 for (i = 0; i < base->n; ++i) {
3068 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3069 isl_dim_copy(base->dim), 0, 0, 0);
3079 struct isl_set *isl_set_extend(struct isl_set *base,
3080 unsigned nparam, unsigned dim)
3082 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3086 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3087 struct isl_basic_map *bmap, unsigned pos, int value)
3091 bmap = isl_basic_map_cow(bmap);
3092 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3093 j = isl_basic_map_alloc_equality(bmap);
3096 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3097 isl_int_set_si(bmap->eq[j][pos], -1);
3098 isl_int_set_si(bmap->eq[j][0], value);
3099 bmap = isl_basic_map_simplify(bmap);
3100 return isl_basic_map_finalize(bmap);
3102 isl_basic_map_free(bmap);
3106 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3107 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3111 bmap = isl_basic_map_cow(bmap);
3112 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3113 j = isl_basic_map_alloc_equality(bmap);
3116 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3117 isl_int_set_si(bmap->eq[j][pos], -1);
3118 isl_int_set(bmap->eq[j][0], value);
3119 bmap = isl_basic_map_simplify(bmap);
3120 return isl_basic_map_finalize(bmap);
3122 isl_basic_map_free(bmap);
3126 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3127 enum isl_dim_type type, unsigned pos, int value)
3131 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3132 return isl_basic_map_fix_pos_si(bmap,
3133 isl_basic_map_offset(bmap, type) + pos, value);
3135 isl_basic_map_free(bmap);
3139 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3140 enum isl_dim_type type, unsigned pos, isl_int value)
3144 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3145 return isl_basic_map_fix_pos(bmap,
3146 isl_basic_map_offset(bmap, type) + pos, value);
3148 isl_basic_map_free(bmap);
3152 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3153 enum isl_dim_type type, unsigned pos, int value)
3155 return (struct isl_basic_set *)
3156 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3160 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3161 enum isl_dim_type type, unsigned pos, isl_int value)
3163 return (struct isl_basic_set *)
3164 isl_basic_map_fix((struct isl_basic_map *)bset,
3168 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3169 unsigned input, int value)
3171 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3174 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3175 unsigned dim, int value)
3177 return (struct isl_basic_set *)
3178 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3179 isl_dim_set, dim, value);
3182 struct isl_map *isl_map_fix_si(struct isl_map *map,
3183 enum isl_dim_type type, unsigned pos, int value)
3187 map = isl_map_cow(map);
3191 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3192 for (i = 0; i < map->n; ++i) {
3193 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3197 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3204 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
3205 enum isl_dim_type type, unsigned pos, isl_int value)
3209 map = isl_map_cow(map);
3213 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3214 for (i = 0; i < map->n; ++i) {
3215 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
3219 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3226 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
3227 enum isl_dim_type type, unsigned pos, isl_int value)
3229 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
3232 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3233 unsigned input, int value)
3235 return isl_map_fix_si(map, isl_dim_in, input, value);
3238 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3240 return (struct isl_set *)
3241 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3244 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3245 unsigned dim, isl_int value)
3249 bset = isl_basic_set_cow(bset);
3250 bset = isl_basic_set_extend_constraints(bset, 0, 1);
3251 j = isl_basic_set_alloc_inequality(bset);
3254 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3255 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3256 isl_int_neg(bset->ineq[j][0], value);
3257 bset = isl_basic_set_simplify(bset);
3258 return isl_basic_set_finalize(bset);
3260 isl_basic_set_free(bset);
3264 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3269 set = isl_set_cow(set);
3273 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3274 for (i = 0; i < set->n; ++i) {
3275 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3285 struct isl_map *isl_map_reverse(struct isl_map *map)
3289 map = isl_map_cow(map);
3293 map->dim = isl_dim_reverse(map->dim);
3296 for (i = 0; i < map->n; ++i) {
3297 map->p[i] = isl_basic_map_reverse(map->p[i]);
3301 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3308 static struct isl_map *isl_basic_map_partial_lexopt(
3309 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3310 struct isl_set **empty, int max)
3314 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
3315 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3317 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3319 isl_basic_map_free(bmap);
3320 isl_basic_set_free(dom);
3326 struct isl_map *isl_basic_map_partial_lexmax(
3327 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3328 struct isl_set **empty)
3330 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
3333 struct isl_map *isl_basic_map_partial_lexmin(
3334 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3335 struct isl_set **empty)
3337 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3340 struct isl_set *isl_basic_set_partial_lexmin(
3341 struct isl_basic_set *bset, struct isl_basic_set *dom,
3342 struct isl_set **empty)
3344 return (struct isl_set *)
3345 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3349 struct isl_set *isl_basic_set_partial_lexmax(
3350 struct isl_basic_set *bset, struct isl_basic_set *dom,
3351 struct isl_set **empty)
3353 return (struct isl_set *)
3354 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3358 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
3360 struct isl_basic_set *dom = NULL;
3361 struct isl_map *min;
3362 struct isl_dim *dom_dim;
3366 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3367 dom = isl_basic_set_universe(dom_dim);
3368 return isl_basic_map_partial_lexmin(bmap, dom, NULL);
3370 isl_basic_map_free(bmap);
3374 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
3376 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
3379 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
3380 struct isl_dim *dim)
3387 for (i = 0; i < map->n; ++i) {
3388 isl_dim_free(map->p[i]->dim);
3389 map->p[i]->dim = isl_dim_copy(dim);
3391 isl_dim_free(map->dim);
3401 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
3402 struct isl_dim *dim)
3404 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3407 /* Apply a preimage specified by "mat" on the parameters of "bset".
3408 * bset is assumed to have only parameters and divs.
3410 static struct isl_basic_set *basic_set_parameter_preimage(
3411 struct isl_basic_set *bset, struct isl_mat *mat)
3418 bset->dim = isl_dim_cow(bset->dim);
3422 nparam = isl_basic_set_dim(bset, isl_dim_param);
3424 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
3426 bset->dim->nparam = 0;
3427 bset->dim->n_out = nparam;
3428 bset = isl_basic_set_preimage(bset, mat);
3430 bset->dim->nparam = bset->dim->n_out;
3431 bset->dim->n_out = 0;
3436 isl_basic_set_free(bset);
3440 /* Apply a preimage specified by "mat" on the parameters of "set".
3441 * set is assumed to have only parameters and divs.
3443 static struct isl_set *set_parameter_preimage(
3444 struct isl_set *set, struct isl_mat *mat)
3446 struct isl_dim *dim = NULL;
3452 dim = isl_dim_copy(set->dim);
3453 dim = isl_dim_cow(dim);
3457 nparam = isl_set_dim(set, isl_dim_param);
3459 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
3462 dim->n_out = nparam;
3463 isl_set_reset_dim(set, dim);
3464 set = isl_set_preimage(set, mat);
3467 dim = isl_dim_copy(set->dim);
3468 dim = isl_dim_cow(dim);
3471 dim->nparam = dim->n_out;
3473 isl_set_reset_dim(set, dim);
3483 /* Intersect the basic set "bset" with the affine space specified by the
3484 * equalities in "eq".
3486 static struct isl_basic_set *basic_set_append_equalities(
3487 struct isl_basic_set *bset, struct isl_mat *eq)
3495 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
3500 len = 1 + isl_dim_total(bset->dim) + bset->extra;
3501 for (i = 0; i < eq->n_row; ++i) {
3502 k = isl_basic_set_alloc_equality(bset);
3505 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
3506 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
3513 isl_basic_set_free(bset);
3517 /* Intersect the set "set" with the affine space specified by the
3518 * equalities in "eq".
3520 static struct isl_set *set_append_equalities(struct isl_set *set,
3528 for (i = 0; i < set->n; ++i) {
3529 set->p[i] = basic_set_append_equalities(set->p[i],
3542 /* Project the given basic set onto its parameter domain, possibly introducing
3543 * new, explicit, existential variables in the constraints.
3544 * The input has parameters and (possibly implicit) existential variables.
3545 * The output has the same parameters, but only
3546 * explicit existentially quantified variables.
3548 * The actual projection is performed by pip, but pip doesn't seem
3549 * to like equalities very much, so we first remove the equalities
3550 * among the parameters by performing a variable compression on
3551 * the parameters. Afterward, an inverse transformation is performed
3552 * and the equalities among the parameters are inserted back in.
3554 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
3558 struct isl_mat *T, *T2;
3559 struct isl_set *set;
3560 unsigned nparam, n_div;
3562 bset = isl_basic_set_cow(bset);
3566 if (bset->n_eq == 0)
3567 return isl_basic_set_lexmin(bset);
3569 isl_basic_set_gauss(bset, NULL);
3571 nparam = isl_basic_set_dim(bset, isl_dim_param);
3572 n_div = isl_basic_set_dim(bset, isl_dim_div);
3574 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
3575 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
3578 if (i == bset->n_eq)
3579 return isl_basic_set_lexmin(bset);
3581 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
3583 eq = isl_mat_cow(eq);
3584 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
3585 bset = basic_set_parameter_preimage(bset, T);
3587 set = isl_basic_set_lexmin(bset);
3588 set = set_parameter_preimage(set, T2);
3589 set = set_append_equalities(set, eq);
3593 /* Compute an explicit representation for all the existentially
3594 * quantified variables.
3595 * The input and output dimensions are first turned into parameters.
3596 * compute_divs then returns a map with the same parameters and
3597 * no input or output dimensions and the dimension specification
3598 * is reset to that of the input.
3600 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
3602 struct isl_basic_set *bset;
3603 struct isl_set *set;
3604 struct isl_map *map;
3605 struct isl_dim *dim, *orig_dim = NULL;
3610 bmap = isl_basic_map_cow(bmap);
3614 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3615 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3616 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3617 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
3621 orig_dim = bmap->dim;
3623 bset = (struct isl_basic_set *)bmap;
3625 set = parameter_compute_divs(bset);
3626 map = (struct isl_map *)set;
3627 map = isl_map_reset_dim(map, orig_dim);
3631 isl_basic_map_free(bmap);
3635 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
3643 off = isl_dim_total(bmap->dim);
3644 for (i = 0; i < bmap->n_div; ++i) {
3645 if (isl_int_is_zero(bmap->div[i][0]))
3647 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
3653 static int map_divs_known(__isl_keep isl_map *map)
3660 for (i = 0; i < map->n; ++i) {
3661 int known = basic_map_divs_known(map->p[i]);
3669 /* If bmap contains any unknown divs, then compute explicit
3670 * expressions for them. However, this computation may be
3671 * quite expensive, so first try to remove divs that aren't
3674 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
3678 struct isl_map *map;
3680 known = basic_map_divs_known(bmap);
3684 return isl_map_from_basic_map(bmap);
3686 bmap = isl_basic_map_drop_redundant_divs(bmap);
3688 known = basic_map_divs_known(bmap);
3692 return isl_map_from_basic_map(bmap);
3694 map = compute_divs(bmap);
3697 isl_basic_map_free(bmap);
3701 struct isl_map *isl_map_compute_divs(struct isl_map *map)
3705 struct isl_map *res;
3712 known = map_divs_known(map);
3720 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
3721 for (i = 1 ; i < map->n; ++i) {
3723 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
3724 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3725 res = isl_map_union_disjoint(res, r2);
3727 res = isl_map_union(res, r2);
3734 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3736 return (struct isl_set *)
3737 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3740 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3742 return (struct isl_set *)
3743 isl_map_compute_divs((struct isl_map *)set);
3746 struct isl_set *isl_map_domain(struct isl_map *map)
3749 struct isl_set *set;
3754 map = isl_map_cow(map);
3758 set = (struct isl_set *)map;
3759 set->dim = isl_dim_domain(set->dim);
3762 for (i = 0; i < map->n; ++i) {
3763 set->p[i] = isl_basic_map_domain(map->p[i]);
3767 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3768 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3775 struct isl_map *isl_map_union_disjoint(
3776 struct isl_map *map1, struct isl_map *map2)
3780 struct isl_map *map = NULL;
3794 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3796 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3797 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3798 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3800 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3801 map1->n + map2->n, flags);
3804 for (i = 0; i < map1->n; ++i) {
3805 map = isl_map_add(map,
3806 isl_basic_map_copy(map1->p[i]));
3810 for (i = 0; i < map2->n; ++i) {
3811 map = isl_map_add(map,
3812 isl_basic_map_copy(map2->p[i]));
3826 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3828 map1 = isl_map_union_disjoint(map1, map2);
3832 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3836 struct isl_set *isl_set_union_disjoint(
3837 struct isl_set *set1, struct isl_set *set2)
3839 return (struct isl_set *)
3840 isl_map_union_disjoint(
3841 (struct isl_map *)set1, (struct isl_map *)set2);
3844 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3846 return (struct isl_set *)
3847 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3850 struct isl_map *isl_map_intersect_range(
3851 struct isl_map *map, struct isl_set *set)
3854 struct isl_map *result;
3860 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3861 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3862 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3864 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3865 map->n * set->n, flags);
3868 for (i = 0; i < map->n; ++i)
3869 for (j = 0; j < set->n; ++j) {
3870 result = isl_map_add(result,
3871 isl_basic_map_intersect_range(
3872 isl_basic_map_copy(map->p[i]),
3873 isl_basic_set_copy(set->p[j])));
3886 struct isl_map *isl_map_intersect_domain(
3887 struct isl_map *map, struct isl_set *set)
3889 return isl_map_reverse(
3890 isl_map_intersect_range(isl_map_reverse(map), set));
3893 struct isl_map *isl_map_apply_domain(
3894 struct isl_map *map1, struct isl_map *map2)
3898 map1 = isl_map_reverse(map1);
3899 map1 = isl_map_apply_range(map1, map2);
3900 return isl_map_reverse(map1);
3907 struct isl_map *isl_map_apply_range(
3908 struct isl_map *map1, struct isl_map *map2)
3910 struct isl_dim *dim_result;
3911 struct isl_map *result;
3917 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3918 isl_dim_copy(map2->dim));
3920 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3923 for (i = 0; i < map1->n; ++i)
3924 for (j = 0; j < map2->n; ++j) {
3925 result = isl_map_add(result,
3926 isl_basic_map_apply_range(
3927 isl_basic_map_copy(map1->p[i]),
3928 isl_basic_map_copy(map2->p[j])));
3934 if (result && result->n <= 1)
3935 ISL_F_SET(result, ISL_MAP_DISJOINT);
3944 * returns range - domain
3946 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3948 struct isl_basic_set *bset;
3955 dim = isl_basic_map_n_in(bmap);
3956 nparam = isl_basic_map_n_param(bmap);
3957 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3958 bset = isl_basic_set_from_basic_map(bmap);
3959 bset = isl_basic_set_cow(bset);
3960 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3961 bset = isl_basic_set_swap_vars(bset, 2*dim);
3962 for (i = 0; i < dim; ++i) {
3963 int j = isl_basic_map_alloc_equality(
3964 (struct isl_basic_map *)bset);
3967 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3968 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3969 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3970 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3972 return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
3974 isl_basic_map_free(bmap);
3979 * returns range - domain
3981 struct isl_set *isl_map_deltas(struct isl_map *map)
3984 struct isl_set *result;
3989 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3990 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3991 isl_map_n_in(map), map->n, map->flags);
3994 for (i = 0; i < map->n; ++i)
3995 result = isl_set_add(result,
3996 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
4004 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
4006 struct isl_basic_map *bmap;
4014 nparam = dims->nparam;
4016 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
4020 for (i = 0; i < dim; ++i) {
4021 int j = isl_basic_map_alloc_equality(bmap);
4024 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
4025 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
4026 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
4028 return isl_basic_map_finalize(bmap);
4030 isl_basic_map_free(bmap);
4034 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
4036 struct isl_dim *dim = isl_dim_map(set_dim);
4039 return basic_map_identity(dim);
4042 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
4044 if (!model || !model->dim)
4046 isl_assert(model->ctx,
4047 model->dim->n_in == model->dim->n_out, return NULL);
4048 return basic_map_identity(isl_dim_copy(model->dim));
4051 static struct isl_map *map_identity(struct isl_dim *dim)
4053 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
4054 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
4057 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
4059 struct isl_dim *dim = isl_dim_map(set_dim);
4062 return map_identity(dim);
4065 struct isl_map *isl_map_identity_like(struct isl_map *model)
4067 if (!model || !model->dim)
4069 isl_assert(model->ctx,
4070 model->dim->n_in == model->dim->n_out, return NULL);
4071 return map_identity(isl_dim_copy(model->dim));
4074 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
4076 if (!model || !model->dim)
4078 isl_assert(model->ctx,
4079 model->dim->n_in == model->dim->n_out, return NULL);
4080 return map_identity(isl_dim_copy(model->dim));
4083 /* Construct a basic set with all set dimensions having only non-negative
4086 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
4091 struct isl_basic_set *bset;
4095 nparam = dims->nparam;
4097 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
4100 for (i = 0; i < dim; ++i) {
4101 int k = isl_basic_set_alloc_inequality(bset);
4104 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
4105 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
4109 isl_basic_set_free(bset);
4113 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
4115 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
4118 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
4120 return isl_map_is_subset(
4121 (struct isl_map *)set1, (struct isl_map *)set2);
4124 int isl_basic_map_is_subset(
4125 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4128 struct isl_map *map1;
4129 struct isl_map *map2;
4131 if (!bmap1 || !bmap2)
4134 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
4135 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
4137 is_subset = isl_map_is_subset(map1, map2);
4145 int isl_basic_map_is_equal(
4146 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4150 if (!bmap1 || !bmap2)
4152 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4155 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4159 int isl_basic_set_is_equal(
4160 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4162 return isl_basic_map_is_equal(
4163 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
4166 int isl_map_is_empty(struct isl_map *map)
4173 for (i = 0; i < map->n; ++i) {
4174 is_empty = isl_basic_map_is_empty(map->p[i]);
4183 int isl_map_fast_is_empty(struct isl_map *map)
4188 int isl_set_is_empty(struct isl_set *set)
4190 return isl_map_is_empty((struct isl_map *)set);
4193 /* Return 1 if "bmap" contains a single element.
4195 int isl_basic_map_is_singleton(__isl_keep isl_basic_map *bmap)
4203 return bmap->n_eq == isl_basic_map_total_dim(bmap);
4206 /* Return 1 if "map" contains a single element.
4208 int isl_map_is_singleton(__isl_keep isl_map *map)
4215 return isl_basic_map_is_singleton(map->p[0]);
4218 /* Given a singleton basic map, extract the single element
4221 static __isl_give isl_vec *singleton_extract_point(__isl_keep isl_basic_map *bmap)
4225 struct isl_vec *point;
4231 dim = isl_basic_map_total_dim(bmap);
4232 isl_assert(bmap->ctx, bmap->n_eq == dim, return NULL);
4233 point = isl_vec_alloc(bmap->ctx, 1 + dim);
4239 isl_int_set_si(point->el[0], 1);
4240 for (j = 0; j < bmap->n_eq; ++j) {
4242 int i = dim - 1 - j;
4243 isl_assert(bmap->ctx,
4244 isl_seq_first_non_zero(bmap->eq[j] + 1, i) == -1,
4246 isl_assert(bmap->ctx,
4247 isl_int_is_one(bmap->eq[j][1 + i]) ||
4248 isl_int_is_negone(bmap->eq[j][1 + i]),
4250 isl_assert(bmap->ctx,
4251 isl_seq_first_non_zero(bmap->eq[j]+1+i+1, dim-i-1) == -1,
4254 isl_int_gcd(m, point->el[0], bmap->eq[j][1 + i]);
4255 isl_int_divexact(m, bmap->eq[j][1 + i], m);
4257 isl_seq_scale(point->el, point->el, m, 1 + i);
4258 isl_int_divexact(m, point->el[0], bmap->eq[j][1 + i]);
4260 isl_int_mul(point->el[1 + i], m, bmap->eq[j][0]);
4267 isl_vec_free(point);
4271 /* Return 1 if "bmap" contains the point "point".
4272 * "bmap" is assumed to have known divs.
4273 * The point is first extended with the divs and then passed
4274 * to basic_map_contains.
4276 static int basic_map_contains_point(__isl_keep isl_basic_map *bmap,
4277 __isl_keep isl_vec *point)
4280 struct isl_vec *vec;
4284 if (!bmap || !point)
4286 if (bmap->n_div == 0)
4287 return basic_map_contains(bmap, point);
4289 dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
4290 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
4294 isl_seq_cpy(vec->el, point->el, point->size);
4295 for (i = 0; i < bmap->n_div; ++i) {
4296 isl_seq_inner_product(bmap->div[i] + 1, vec->el,
4297 1 + dim + i, &vec->el[1+dim+i]);
4298 isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
4302 contains = basic_map_contains(bmap, vec);
4308 /* Return 1 is the singleton map "map1" is a subset of "map2",
4309 * i.e., if the single element of "map1" is also an element of "map2".
4311 static int map_is_singleton_subset(__isl_keep isl_map *map1,
4312 __isl_keep isl_map *map2)
4316 struct isl_vec *point;
4323 point = singleton_extract_point(map1->p[0]);
4327 for (i = 0; i < map2->n; ++i) {
4328 is_subset = basic_map_contains_point(map2->p[i], point);
4333 isl_vec_free(point);
4337 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
4340 struct isl_map *diff;
4345 if (isl_map_is_empty(map1))
4348 if (isl_map_is_empty(map2))
4351 if (isl_map_fast_is_universe(map2))
4354 map2 = isl_map_compute_divs(isl_map_copy(map2));
4355 if (isl_map_is_singleton(map1)) {
4356 is_subset = map_is_singleton_subset(map1, map2);
4360 diff = isl_map_subtract(isl_map_copy(map1), map2);
4364 is_subset = isl_map_is_empty(diff);
4370 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
4376 is_subset = isl_map_is_subset(map1, map2);
4379 is_subset = isl_map_is_subset(map2, map1);
4383 int isl_basic_map_is_strict_subset(
4384 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4388 if (!bmap1 || !bmap2)
4390 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4393 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4394 if (is_subset == -1)
4399 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
4405 is_subset = isl_map_is_subset(map1, map2);
4408 is_subset = isl_map_is_subset(map2, map1);
4409 if (is_subset == -1)
4414 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4416 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
4419 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
4423 return bmap->n_eq == 0 && bmap->n_ineq == 0;
4426 int isl_basic_set_is_universe(struct isl_basic_set *bset)
4430 return bset->n_eq == 0 && bset->n_ineq == 0;
4433 int isl_map_fast_is_universe(__isl_keep isl_map *map)
4438 return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
4441 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
4443 struct isl_basic_set *bset = NULL;
4444 struct isl_vec *sample = NULL;
4451 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4454 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
4455 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
4456 copy = isl_basic_map_convex_hull(copy);
4457 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
4458 isl_basic_map_free(copy);
4462 total = 1 + isl_basic_map_total_dim(bmap);
4463 if (bmap->sample && bmap->sample->size == total) {
4464 int contains = basic_map_contains(bmap, bmap->sample);
4470 isl_vec_free(bmap->sample);
4471 bmap->sample = NULL;
4472 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
4475 sample = isl_basic_set_sample_vec(bset);
4478 empty = sample->size == 0;
4479 isl_vec_free(bmap->sample);
4480 bmap->sample = sample;
4482 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
4487 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
4491 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
4494 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
4498 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
4501 int isl_basic_set_is_empty(struct isl_basic_set *bset)
4503 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
4506 struct isl_map *isl_basic_map_union(
4507 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4509 struct isl_map *map;
4510 if (!bmap1 || !bmap2)
4513 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
4515 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
4518 map = isl_map_add(map, bmap1);
4519 map = isl_map_add(map, bmap2);
4522 isl_basic_map_free(bmap1);
4523 isl_basic_map_free(bmap2);
4527 struct isl_set *isl_basic_set_union(
4528 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4530 return (struct isl_set *)isl_basic_map_union(
4531 (struct isl_basic_map *)bset1,
4532 (struct isl_basic_map *)bset2);
4535 /* Order divs such that any div only depends on previous divs */
4536 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
4539 unsigned off = isl_dim_total(bmap->dim);
4541 for (i = 0; i < bmap->n_div; ++i) {
4543 if (isl_int_is_zero(bmap->div[i][0]))
4545 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
4549 isl_basic_map_swap_div(bmap, i, i + pos);
4555 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
4557 return (struct isl_basic_set *)
4558 isl_basic_map_order_divs((struct isl_basic_map *)bset);
4561 /* Look for a div in dst that corresponds to the div "div" in src.
4562 * The divs before "div" in src and dst are assumed to be the same.
4564 * Returns -1 if no corresponding div was found and the position
4565 * of the corresponding div in dst otherwise.
4567 static int find_div(struct isl_basic_map *dst,
4568 struct isl_basic_map *src, unsigned div)
4572 unsigned total = isl_dim_total(src->dim);
4574 isl_assert(dst->ctx, div <= dst->n_div, return -1);
4575 for (i = div; i < dst->n_div; ++i)
4576 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
4577 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
4578 dst->n_div - div) == -1)
4583 struct isl_basic_map *isl_basic_map_align_divs(
4584 struct isl_basic_map *dst, struct isl_basic_map *src)
4587 unsigned total = isl_dim_total(src->dim);
4592 if (src->n_div == 0)
4595 for (i = 0; i < src->n_div; ++i)
4596 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
4598 src = isl_basic_map_order_divs(src);
4599 dst = isl_basic_map_cow(dst);
4600 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
4601 src->n_div, 0, 2 * src->n_div);
4604 for (i = 0; i < src->n_div; ++i) {
4605 int j = find_div(dst, src, i);
4607 j = isl_basic_map_alloc_div(dst);
4610 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
4611 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
4612 if (add_div_constraints(dst, j) < 0)
4616 isl_basic_map_swap_div(dst, i, j);
4620 isl_basic_map_free(dst);
4624 struct isl_basic_set *isl_basic_set_align_divs(
4625 struct isl_basic_set *dst, struct isl_basic_set *src)
4627 return (struct isl_basic_set *)isl_basic_map_align_divs(
4628 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
4631 struct isl_map *isl_map_align_divs(struct isl_map *map)
4639 map = isl_map_compute_divs(map);
4640 map = isl_map_cow(map);
4644 for (i = 1; i < map->n; ++i)
4645 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
4646 for (i = 1; i < map->n; ++i)
4647 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
4649 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4653 struct isl_set *isl_set_align_divs(struct isl_set *set)
4655 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
4658 static struct isl_map *add_cut_constraint(struct isl_map *dst,
4659 struct isl_basic_map *src, isl_int *c,
4660 unsigned len, int oppose)
4662 struct isl_basic_map *copy = NULL;
4667 copy = isl_basic_map_copy(src);
4668 copy = isl_basic_map_cow(copy);
4671 copy = isl_basic_map_extend_constraints(copy, 0, 1);
4672 k = isl_basic_map_alloc_inequality(copy);
4676 isl_seq_neg(copy->ineq[k], c, len);
4678 isl_seq_cpy(copy->ineq[k], c, len);
4679 total = 1 + isl_basic_map_total_dim(copy);
4680 isl_seq_clr(copy->ineq[k]+len, total - len);
4681 isl_inequality_negate(copy, k);
4682 copy = isl_basic_map_simplify(copy);
4683 copy = isl_basic_map_finalize(copy);
4684 is_empty = isl_basic_map_is_empty(copy);
4688 dst = isl_map_add(dst, copy);
4690 isl_basic_map_free(copy);
4693 isl_basic_map_free(copy);
4698 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
4702 struct isl_map *rest = NULL;
4704 unsigned total = isl_basic_map_total_dim(bmap);
4706 map = isl_map_cow(map);
4711 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4712 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4714 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
4715 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
4719 for (i = 0; i < map->n; ++i) {
4720 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
4725 for (j = 0; j < map->n; ++j)
4726 map->p[j] = isl_basic_map_cow(map->p[j]);
4728 for (i = 0; i < bmap->n_eq; ++i) {
4729 for (j = 0; j < map->n; ++j) {
4730 rest = add_cut_constraint(rest,
4731 map->p[j], bmap->eq[i], 1+total, 0);
4735 rest = add_cut_constraint(rest,
4736 map->p[j], bmap->eq[i], 1+total, 1);
4740 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4744 k = isl_basic_map_alloc_equality(map->p[j]);
4747 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
4748 isl_seq_clr(map->p[j]->eq[k]+1+total,
4749 map->p[j]->n_div - bmap->n_div);
4753 for (i = 0; i < bmap->n_ineq; ++i) {
4754 for (j = 0; j < map->n; ++j) {
4755 rest = add_cut_constraint(rest,
4756 map->p[j], bmap->ineq[i], 1+total, 0);
4760 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
4764 k = isl_basic_map_alloc_inequality(map->p[j]);
4767 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
4768 isl_seq_clr(map->p[j]->ineq[k]+1+total,
4769 map->p[j]->n_div - bmap->n_div);
4781 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
4787 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4789 if (isl_map_is_empty(map2)) {
4794 map1 = isl_map_compute_divs(map1);
4795 map2 = isl_map_compute_divs(map2);
4799 map1 = isl_map_remove_empty_parts(map1);
4800 map2 = isl_map_remove_empty_parts(map2);
4802 for (i = 0; map1 && i < map2->n; ++i)
4803 map1 = subtract(map1, map2->p[i]);
4813 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
4815 return (struct isl_set *)
4817 (struct isl_map *)set1, (struct isl_map *)set2);
4820 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
4824 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
4825 map = isl_map_intersect_domain(map, set);
4826 set = isl_map_range(map);
4834 /* There is no need to cow as removing empty parts doesn't change
4835 * the meaning of the set.
4837 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
4844 for (i = map->n-1; i >= 0; --i) {
4845 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
4847 isl_basic_map_free(map->p[i]);
4848 if (i != map->n-1) {
4849 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4850 map->p[i] = map->p[map->n-1];
4858 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
4860 return (struct isl_set *)
4861 isl_map_remove_empty_parts((struct isl_map *)set);
4864 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
4866 struct isl_basic_map *bmap;
4867 if (!map || map->n == 0)
4869 bmap = map->p[map->n-1];
4870 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
4871 return isl_basic_map_copy(bmap);
4874 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
4876 return (struct isl_basic_set *)
4877 isl_map_copy_basic_map((struct isl_map *)set);
4880 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
4881 struct isl_basic_map *bmap)
4887 for (i = map->n-1; i >= 0; --i) {
4888 if (map->p[i] != bmap)
4890 map = isl_map_cow(map);
4893 isl_basic_map_free(map->p[i]);
4894 if (i != map->n-1) {
4895 ISL_F_CLR(map, ISL_SET_NORMALIZED);
4896 map->p[i] = map->p[map->n-1];
4901 isl_basic_map_free(bmap);
4905 isl_basic_map_free(bmap);
4909 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4910 struct isl_basic_set *bset)
4912 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
4913 (struct isl_basic_map *)bset);
4916 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4917 * for any common value of the parameters and dimensions preceding dim
4918 * in both basic sets, the values of dimension pos in bset1 are
4919 * smaller or larger than those in bset2.
4922 * 1 if bset1 follows bset2
4923 * -1 if bset1 precedes bset2
4924 * 0 if bset1 and bset2 are incomparable
4925 * -2 if some error occurred.
4927 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4928 struct isl_basic_set *bset2, int pos)
4930 struct isl_dim *dims;
4931 struct isl_basic_map *bmap1 = NULL;
4932 struct isl_basic_map *bmap2 = NULL;
4933 struct isl_ctx *ctx;
4934 struct isl_vec *obj;
4937 unsigned dim1, dim2;
4939 enum isl_lp_result res;
4942 if (!bset1 || !bset2)
4945 nparam = isl_basic_set_n_param(bset1);
4946 dim1 = isl_basic_set_n_dim(bset1);
4947 dim2 = isl_basic_set_n_dim(bset2);
4948 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
4949 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
4950 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
4951 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
4952 if (!bmap1 || !bmap2)
4954 bmap1 = isl_basic_map_cow(bmap1);
4955 bmap1 = isl_basic_map_extend(bmap1, nparam,
4956 pos, (dim1 - pos) + (dim2 - pos),
4957 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4958 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
4961 total = isl_basic_map_total_dim(bmap1);
4963 obj = isl_vec_alloc(ctx, 1 + total);
4964 isl_seq_clr(obj->block.data, 1 + total);
4965 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
4966 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
4971 res = isl_basic_map_solve_lp(bmap1, 0, obj->block.data, ctx->one,
4973 if (res == isl_lp_empty)
4975 else if (res == isl_lp_ok && isl_int_is_pos(num))
4977 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4978 res == isl_lp_unbounded)
4984 isl_basic_map_free(bmap1);
4988 isl_basic_map_free(bmap1);
4989 isl_basic_map_free(bmap2);
4993 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4994 unsigned pos, isl_int *val)
5002 total = isl_basic_map_total_dim(bmap);
5003 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5004 for (; d+1 > pos; --d)
5005 if (!isl_int_is_zero(bmap->eq[i][1+d]))
5009 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5011 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5013 if (!isl_int_is_one(bmap->eq[i][1+d]))
5016 isl_int_neg(*val, bmap->eq[i][0]);
5022 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5023 unsigned pos, isl_int *val)
5035 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
5038 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
5039 for (i = 1; fixed == 1 && i < map->n; ++i) {
5040 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
5041 if (fixed == 1 && isl_int_ne(tmp, v))
5045 isl_int_set(*val, v);
5051 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5052 unsigned pos, isl_int *val)
5054 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
5058 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
5061 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
5064 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
5065 enum isl_dim_type type, unsigned pos, isl_int *val)
5067 if (pos >= isl_basic_map_dim(bmap, type))
5069 return isl_basic_map_fast_has_fixed_var(bmap,
5070 isl_basic_map_offset(bmap, type) - 1 + pos, val);
5073 int isl_map_fast_is_fixed(struct isl_map *map,
5074 enum isl_dim_type type, unsigned pos, isl_int *val)
5076 if (pos >= isl_map_dim(map, type))
5078 return isl_map_fast_has_fixed_var(map,
5079 map_offset(map, type) - 1 + pos, val);
5082 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5083 * then return this fixed value in *val.
5085 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
5088 return isl_basic_set_fast_has_fixed_var(bset,
5089 isl_basic_set_n_param(bset) + dim, val);
5092 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5093 * then return this fixed value in *val.
5095 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
5097 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
5100 /* Check if input variable in has fixed value and if so and if val is not NULL,
5101 * then return this fixed value in *val.
5103 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
5105 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
5108 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5109 * and if val is not NULL, then return this lower bound in *val.
5111 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
5112 unsigned dim, isl_int *val)
5114 int i, i_eq = -1, i_ineq = -1;
5121 total = isl_basic_set_total_dim(bset);
5122 nparam = isl_basic_set_n_param(bset);
5123 for (i = 0; i < bset->n_eq; ++i) {
5124 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
5130 for (i = 0; i < bset->n_ineq; ++i) {
5131 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
5133 if (i_eq != -1 || i_ineq != -1)
5137 if (i_eq == -1 && i_ineq == -1)
5139 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
5140 /* The coefficient should always be one due to normalization. */
5141 if (!isl_int_is_one(c[1+nparam+dim]))
5143 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
5145 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
5146 total - nparam - dim - 1) != -1)
5149 isl_int_neg(*val, c[0]);
5153 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
5154 unsigned dim, isl_int *val)
5166 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5170 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5172 for (i = 1; fixed == 1 && i < set->n; ++i) {
5173 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
5175 if (fixed == 1 && isl_int_ne(tmp, v))
5179 isl_int_set(*val, v);
5190 static int qsort_constraint_cmp(const void *p1, const void *p2)
5192 const struct constraint *c1 = (const struct constraint *)p1;
5193 const struct constraint *c2 = (const struct constraint *)p2;
5194 unsigned size = isl_min(c1->size, c2->size);
5195 return isl_seq_cmp(c1->c, c2->c, size);
5198 static struct isl_basic_map *isl_basic_map_sort_constraints(
5199 struct isl_basic_map *bmap)
5202 struct constraint *c;
5207 total = isl_basic_map_total_dim(bmap);
5208 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
5211 for (i = 0; i < bmap->n_ineq; ++i) {
5213 c[i].c = bmap->ineq[i];
5215 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
5216 for (i = 0; i < bmap->n_ineq; ++i)
5217 bmap->ineq[i] = c[i].c;
5221 isl_basic_map_free(bmap);
5225 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
5229 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
5231 bmap = isl_basic_map_convex_hull(bmap);
5232 bmap = isl_basic_map_sort_constraints(bmap);
5233 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
5237 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
5239 return (struct isl_basic_set *)isl_basic_map_normalize(
5240 (struct isl_basic_map *)bset);
5243 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
5244 const struct isl_basic_map *bmap2)
5251 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
5252 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
5253 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
5254 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5255 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
5256 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5257 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
5258 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5260 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
5262 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5264 if (bmap1->n_eq != bmap2->n_eq)
5265 return bmap1->n_eq - bmap2->n_eq;
5266 if (bmap1->n_ineq != bmap2->n_ineq)
5267 return bmap1->n_ineq - bmap2->n_ineq;
5268 if (bmap1->n_div != bmap2->n_div)
5269 return bmap1->n_div - bmap2->n_div;
5270 total = isl_basic_map_total_dim(bmap1);
5271 for (i = 0; i < bmap1->n_eq; ++i) {
5272 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
5276 for (i = 0; i < bmap1->n_ineq; ++i) {
5277 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
5281 for (i = 0; i < bmap1->n_div; ++i) {
5282 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
5289 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
5290 struct isl_basic_map *bmap2)
5292 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
5295 static int qsort_bmap_cmp(const void *p1, const void *p2)
5297 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
5298 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
5300 return isl_basic_map_fast_cmp(bmap1, bmap2);
5303 /* We normalize in place, but if anything goes wrong we need
5304 * to return NULL, so we need to make sure we don't change the
5305 * meaning of any possible other copies of map.
5307 struct isl_map *isl_map_normalize(struct isl_map *map)
5310 struct isl_basic_map *bmap;
5314 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
5316 for (i = 0; i < map->n; ++i) {
5317 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
5320 isl_basic_map_free(map->p[i]);
5323 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
5324 ISL_F_SET(map, ISL_MAP_NORMALIZED);
5325 map = isl_map_remove_empty_parts(map);
5328 for (i = map->n - 1; i >= 1; --i) {
5329 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
5331 isl_basic_map_free(map->p[i-1]);
5332 for (j = i; j < map->n; ++j)
5333 map->p[j-1] = map->p[j];
5343 struct isl_set *isl_set_normalize(struct isl_set *set)
5345 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
5348 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
5358 if (!isl_dim_equal(map1->dim, map2->dim))
5361 map1 = isl_map_copy(map1);
5362 map2 = isl_map_copy(map2);
5363 map1 = isl_map_normalize(map1);
5364 map2 = isl_map_normalize(map2);
5367 equal = map1->n == map2->n;
5368 for (i = 0; equal && i < map1->n; ++i) {
5369 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
5382 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
5384 return isl_map_fast_is_equal((struct isl_map *)set1,
5385 (struct isl_map *)set2);
5388 /* Return an interval that ranges from min to max (inclusive)
5390 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
5391 isl_int min, isl_int max)
5394 struct isl_basic_set *bset = NULL;
5396 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
5400 k = isl_basic_set_alloc_inequality(bset);
5403 isl_int_set_si(bset->ineq[k][1], 1);
5404 isl_int_neg(bset->ineq[k][0], min);
5406 k = isl_basic_set_alloc_inequality(bset);
5409 isl_int_set_si(bset->ineq[k][1], -1);
5410 isl_int_set(bset->ineq[k][0], max);
5414 isl_basic_set_free(bset);
5418 /* Return the Cartesian product of the basic sets in list (in the given order).
5420 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
5428 struct isl_basic_set *product = NULL;
5432 isl_assert(list->ctx, list->n > 0, goto error);
5433 isl_assert(list->ctx, list->p[0], goto error);
5434 nparam = isl_basic_set_n_param(list->p[0]);
5435 dim = isl_basic_set_n_dim(list->p[0]);
5436 extra = list->p[0]->n_div;
5437 n_eq = list->p[0]->n_eq;
5438 n_ineq = list->p[0]->n_ineq;
5439 for (i = 1; i < list->n; ++i) {
5440 isl_assert(list->ctx, list->p[i], goto error);
5441 isl_assert(list->ctx,
5442 nparam == isl_basic_set_n_param(list->p[i]), goto error);
5443 dim += isl_basic_set_n_dim(list->p[i]);
5444 extra += list->p[i]->n_div;
5445 n_eq += list->p[i]->n_eq;
5446 n_ineq += list->p[i]->n_ineq;
5448 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
5453 for (i = 0; i < list->n; ++i) {
5454 isl_basic_set_add_constraints(product,
5455 isl_basic_set_copy(list->p[i]), dim);
5456 dim += isl_basic_set_n_dim(list->p[i]);
5458 isl_basic_set_list_free(list);
5461 isl_basic_set_free(product);
5462 isl_basic_set_list_free(list);
5466 struct isl_basic_map *isl_basic_map_product(
5467 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5469 struct isl_dim *dim_result = NULL;
5470 struct isl_basic_map *bmap;
5471 unsigned in1, in2, out1, out2, nparam, total, pos;
5472 struct isl_dim_map *dim_map1, *dim_map2;
5474 if (!bmap1 || !bmap2)
5477 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
5478 bmap2->dim, isl_dim_param), goto error);
5479 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
5480 isl_dim_copy(bmap2->dim));
5482 in1 = isl_basic_map_n_in(bmap1);
5483 in2 = isl_basic_map_n_in(bmap2);
5484 out1 = isl_basic_map_n_out(bmap1);
5485 out2 = isl_basic_map_n_out(bmap2);
5486 nparam = isl_basic_map_n_param(bmap1);
5488 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
5489 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5490 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
5491 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5492 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
5493 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5494 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
5495 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
5496 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
5497 isl_dim_map_div(dim_map1, bmap1, pos += out2);
5498 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5500 bmap = isl_basic_map_alloc_dim(dim_result,
5501 bmap1->n_div + bmap2->n_div,
5502 bmap1->n_eq + bmap2->n_eq,
5503 bmap1->n_ineq + bmap2->n_ineq);
5504 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
5505 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
5506 bmap = isl_basic_map_simplify(bmap);
5507 return isl_basic_map_finalize(bmap);
5509 isl_basic_map_free(bmap1);
5510 isl_basic_map_free(bmap2);
5514 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5516 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
5519 struct isl_map *result;
5525 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
5526 map2->dim, isl_dim_param), goto error);
5528 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5529 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5530 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5532 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
5533 isl_dim_copy(map2->dim)),
5534 map1->n * map2->n, flags);
5537 for (i = 0; i < map1->n; ++i)
5538 for (j = 0; j < map2->n; ++j) {
5539 struct isl_basic_map *part;
5540 part = isl_basic_map_product(
5541 isl_basic_map_copy(map1->p[i]),
5542 isl_basic_map_copy(map2->p[j]));
5543 if (isl_basic_map_is_empty(part))
5544 isl_basic_map_free(part);
5546 result = isl_map_add(result, part);
5559 /* Given two set A and B, construct its Cartesian product A x B.
5561 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
5563 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
5564 (struct isl_map *)set2);
5567 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
5570 uint32_t hash = isl_hash_init();
5575 bset = isl_basic_set_copy(bset);
5576 bset = isl_basic_set_normalize(bset);
5579 total = isl_basic_set_total_dim(bset);
5580 isl_hash_byte(hash, bset->n_eq & 0xFF);
5581 for (i = 0; i < bset->n_eq; ++i) {
5583 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
5584 isl_hash_hash(hash, c_hash);
5586 isl_hash_byte(hash, bset->n_ineq & 0xFF);
5587 for (i = 0; i < bset->n_ineq; ++i) {
5589 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
5590 isl_hash_hash(hash, c_hash);
5592 isl_hash_byte(hash, bset->n_div & 0xFF);
5593 for (i = 0; i < bset->n_div; ++i) {
5595 if (isl_int_is_zero(bset->div[i][0]))
5597 isl_hash_byte(hash, i & 0xFF);
5598 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
5599 isl_hash_hash(hash, c_hash);
5601 isl_basic_set_free(bset);
5605 uint32_t isl_set_get_hash(struct isl_set *set)
5612 set = isl_set_copy(set);
5613 set = isl_set_normalize(set);
5617 hash = isl_hash_init();
5618 for (i = 0; i < set->n; ++i) {
5620 bset_hash = isl_basic_set_get_hash(set->p[i]);
5621 isl_hash_hash(hash, bset_hash);
5629 /* Check if the value for dimension dim is completely determined
5630 * by the values of the other parameters and variables.
5631 * That is, check if dimension dim is involved in an equality.
5633 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
5640 nparam = isl_basic_set_n_param(bset);
5641 for (i = 0; i < bset->n_eq; ++i)
5642 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
5647 /* Check if the value for dimension dim is completely determined
5648 * by the values of the other parameters and variables.
5649 * That is, check if dimension dim is involved in an equality
5650 * for each of the subsets.
5652 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
5658 for (i = 0; i < set->n; ++i) {
5660 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
5667 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
5668 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
5675 for (i = 0; i < map->n; ++i)
5676 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
5682 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
5683 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
5690 for (i = 0; i < set->n; ++i)
5691 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
5697 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
5699 struct isl_dim *dim;
5700 struct isl_basic_map *bmap;
5707 set = isl_set_align_divs(set);
5712 dim = isl_set_get_dim(set);
5713 if (set->n == 0 || set->p[0]->n_div == 0) {
5715 return isl_map_identity(dim);
5718 n_div = set->p[0]->n_div;
5719 dim = isl_dim_map(dim);
5720 n_param = isl_dim_size(dim, isl_dim_param);
5721 n_set = isl_dim_size(dim, isl_dim_in);
5722 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
5723 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
5724 for (i = 0; i < n_set; ++i)
5725 bmap = var_equal(bmap, i);
5727 total = n_param + n_set + n_set + n_div;
5728 for (i = 0; i < n_div; ++i) {
5729 k = isl_basic_map_alloc_inequality(bmap);
5732 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
5733 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
5734 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
5735 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
5736 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
5737 set->p[0]->div[i][0]);
5739 l = isl_basic_map_alloc_inequality(bmap);
5742 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
5743 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
5744 set->p[0]->div[i][0]);
5745 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
5749 return isl_map_from_basic_map(bmap);
5752 isl_basic_map_free(bmap);
5756 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
5764 dim = isl_basic_set_total_dim(bset);
5765 size += bset->n_eq * (1 + dim);
5766 size += bset->n_ineq * (1 + dim);
5767 size += bset->n_div * (2 + dim);
5772 int isl_set_size(__isl_keep isl_set *set)
5780 for (i = 0; i < set->n; ++i)
5781 size += isl_basic_set_size(set->p[i]);