11 #include "isl_map_private.h"
12 #include "isl_map_piplib.h"
13 #include "isl_sample.h"
16 /* Maps dst positions to src positions */
22 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
25 struct isl_dim_map *dim_map;
26 dim_map = isl_alloc(ctx, struct isl_dim_map,
27 sizeof(struct isl_dim_map) + len * sizeof(int));
30 dim_map->len = 1 + len;
32 for (i = 0; i < len; ++i)
33 dim_map->pos[1 + i] = -1;
37 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
40 case isl_dim_param: return dim->nparam;
41 case isl_dim_in: return dim->n_in;
42 case isl_dim_out: return dim->n_out;
43 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
47 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
50 case isl_dim_param: return 1;
51 case isl_dim_in: return 1 + dim->nparam;
52 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
56 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
57 enum isl_dim_type type, unsigned dst_pos)
65 src_pos = pos(dim, type);
66 for (i = 0; i < n(dim, type); ++i)
67 dim_map->pos[1 + dst_pos + i] = src_pos + i;
70 static void isl_dim_map_div(struct isl_dim_map *dim_map,
71 struct isl_basic_map *bmap, unsigned dst_pos)
76 if (!dim_map || !bmap)
79 src_pos = 1 + isl_dim_total(bmap->dim);
80 for (i = 0; i < bmap->n_div; ++i)
81 dim_map->pos[1 + dst_pos + i] = src_pos + i;
84 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
88 for (i = 0; i < dim_map->len; ++i)
89 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
90 fprintf(stderr, "\n");
93 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
94 enum isl_dim_type type)
96 struct isl_dim *dim = bmap->dim;
100 case isl_dim_out: return isl_dim_size(bmap->dim, type);
101 case isl_dim_div: return bmap->n_div;
102 case isl_dim_all: return isl_basic_map_total_dim(bmap);
106 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
108 return n(map->dim, type);
111 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
113 return n(set->dim, type);
116 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
117 enum isl_dim_type type)
119 struct isl_dim *dim = bmap->dim;
121 case isl_dim_param: return 1;
122 case isl_dim_in: return 1 + dim->nparam;
123 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
124 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
128 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
130 return pos(map->dim, type);
133 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
134 enum isl_dim_type type)
136 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
139 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
141 return bset->dim->n_out;
144 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
146 return bset->dim->nparam;
149 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
151 return isl_dim_total(bset->dim) + bset->n_div;
154 unsigned isl_set_n_dim(const struct isl_set *set)
156 return set->dim->n_out;
159 unsigned isl_set_n_param(const struct isl_set *set)
161 return set->dim->nparam;
164 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
166 return bmap->dim->n_in;
169 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
171 return bmap->dim->n_out;
174 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
176 return bmap->dim->nparam;
179 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
184 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
186 return isl_dim_total(bmap->dim) + bmap->n_div;
189 unsigned isl_map_n_in(const struct isl_map *map)
191 return map->dim->n_in;
194 unsigned isl_map_n_out(const struct isl_map *map)
196 return map->dim->n_out;
199 unsigned isl_map_n_param(const struct isl_map *map)
201 return map->dim->nparam;
204 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
206 return map->dim->n_in == set->dim->n_out &&
207 map->dim->nparam == set->dim->nparam;
210 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
211 struct isl_basic_set *bset)
213 return bmap->dim->n_in == bset->dim->n_out &&
214 bmap->dim->nparam == bset->dim->nparam;
217 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
218 struct isl_basic_set *bset)
220 return bmap->dim->n_out == bset->dim->n_out &&
221 bmap->dim->nparam == bset->dim->nparam;
224 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
228 return isl_dim_copy(bmap->dim);
231 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
235 return isl_dim_copy(bset->dim);
238 struct isl_dim *isl_map_get_dim(struct isl_map *map)
242 return isl_dim_copy(map->dim);
245 struct isl_dim *isl_set_get_dim(struct isl_set *set)
249 return isl_dim_copy(set->dim);
252 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
253 struct isl_basic_map *bmap, unsigned extra,
254 unsigned n_eq, unsigned n_ineq)
257 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
259 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
260 if (isl_blk_is_error(bmap->block)) {
265 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
267 isl_blk_free(ctx, bmap->block);
273 bmap->block2 = isl_blk_empty();
276 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
277 if (isl_blk_is_error(bmap->block2)) {
279 isl_blk_free(ctx, bmap->block);
284 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
286 isl_blk_free(ctx, bmap->block2);
288 isl_blk_free(ctx, bmap->block);
294 for (i = 0; i < n_ineq + n_eq; ++i)
295 bmap->ineq[i] = bmap->block.data + i * row_size;
297 for (i = 0; i < extra; ++i)
298 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
304 bmap->c_size = n_eq + n_ineq;
305 bmap->eq = bmap->ineq + n_ineq;
314 isl_basic_map_free(bmap);
318 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
319 unsigned nparam, unsigned dim, unsigned extra,
320 unsigned n_eq, unsigned n_ineq)
322 struct isl_basic_map *bmap;
323 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
324 return (struct isl_basic_set *)bmap;
327 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
328 unsigned extra, unsigned n_eq, unsigned n_ineq)
330 struct isl_basic_map *bmap;
333 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
334 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
335 return (struct isl_basic_set *)bmap;
338 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
339 unsigned extra, unsigned n_eq, unsigned n_ineq)
341 struct isl_basic_map *bmap;
345 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
350 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
356 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
357 unsigned nparam, unsigned in, unsigned out, unsigned extra,
358 unsigned n_eq, unsigned n_ineq)
360 struct isl_basic_map *bmap;
363 dim = isl_dim_alloc(ctx, nparam, in, out);
367 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
371 static void dup_constraints(
372 struct isl_basic_map *dst, struct isl_basic_map *src)
375 unsigned total = isl_basic_map_total_dim(src);
377 for (i = 0; i < src->n_eq; ++i) {
378 int j = isl_basic_map_alloc_equality(dst);
379 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
382 for (i = 0; i < src->n_ineq; ++i) {
383 int j = isl_basic_map_alloc_inequality(dst);
384 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
387 for (i = 0; i < src->n_div; ++i) {
388 int j = isl_basic_map_alloc_div(dst);
389 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
391 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
394 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
396 struct isl_basic_map *dup;
400 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
401 bmap->n_div, bmap->n_eq, bmap->n_ineq);
404 dup_constraints(dup, bmap);
405 dup->flags = bmap->flags;
406 dup->sample = isl_vec_copy(bmap->sample);
410 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
412 struct isl_basic_map *dup;
414 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
415 return (struct isl_basic_set *)dup;
418 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
423 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
427 return isl_basic_set_dup(bset);
430 struct isl_set *isl_set_copy(struct isl_set *set)
439 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
444 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
448 return isl_basic_map_dup(bmap);
451 struct isl_map *isl_map_copy(struct isl_map *map)
460 void isl_basic_map_free(struct isl_basic_map *bmap)
468 isl_ctx_deref(bmap->ctx);
470 isl_blk_free(bmap->ctx, bmap->block2);
472 isl_blk_free(bmap->ctx, bmap->block);
473 isl_vec_free(bmap->sample);
474 isl_dim_free(bmap->dim);
478 void isl_basic_set_free(struct isl_basic_set *bset)
480 isl_basic_map_free((struct isl_basic_map *)bset);
483 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
485 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
488 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
494 isl_assert(ctx, room_for_con(bmap, 1), return -1);
495 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
497 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
498 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
499 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
500 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
501 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
502 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
504 int j = isl_basic_map_alloc_inequality(bmap);
508 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
509 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
516 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
517 bmap->extra - bmap->n_div);
521 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
523 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
526 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
530 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
535 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
537 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
540 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
545 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
547 if (pos != bmap->n_eq - 1) {
549 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
550 bmap->eq[bmap->n_eq - 1] = t;
556 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
558 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
561 void isl_basic_map_inequality_to_equality(
562 struct isl_basic_map *bmap, unsigned pos)
567 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
568 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
573 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
574 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
575 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
576 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
579 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
581 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
584 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
590 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
591 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
592 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
593 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
594 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
595 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
596 1 + isl_basic_map_total_dim(bmap),
597 bmap->extra - bmap->n_div);
598 return bmap->n_ineq++;
601 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
603 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
606 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
610 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
615 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
617 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
620 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
625 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
627 if (pos != bmap->n_ineq - 1) {
629 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
630 bmap->ineq[bmap->n_ineq - 1] = t;
631 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
637 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
639 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
642 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
646 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
647 isl_seq_clr(bmap->div[bmap->n_div] +
648 1 + 1 + isl_basic_map_total_dim(bmap),
649 bmap->extra - bmap->n_div);
650 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
651 return bmap->n_div++;
654 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
656 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
659 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
663 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
668 /* Copy constraint from src to dst, putting the vars of src at offset
669 * dim_off in dst and the divs of src at offset div_off in dst.
670 * If both sets are actually map, then dim_off applies to the input
673 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
674 struct isl_basic_map *src_map, isl_int *src,
675 unsigned in_off, unsigned out_off, unsigned div_off)
677 unsigned src_nparam = isl_basic_map_n_param(src_map);
678 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
679 unsigned src_in = isl_basic_map_n_in(src_map);
680 unsigned dst_in = isl_basic_map_n_in(dst_map);
681 unsigned src_out = isl_basic_map_n_out(src_map);
682 unsigned dst_out = isl_basic_map_n_out(dst_map);
683 isl_int_set(dst[0], src[0]);
684 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
685 if (dst_nparam > src_nparam)
686 isl_seq_clr(dst+1+src_nparam,
687 dst_nparam - src_nparam);
688 isl_seq_clr(dst+1+dst_nparam, in_off);
689 isl_seq_cpy(dst+1+dst_nparam+in_off,
691 isl_min(dst_in-in_off, src_in));
692 if (dst_in-in_off > src_in)
693 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
694 dst_in - in_off - src_in);
695 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
696 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
697 src+1+src_nparam+src_in,
698 isl_min(dst_out-out_off, src_out));
699 if (dst_out-out_off > src_out)
700 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
701 dst_out - out_off - src_out);
702 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
703 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
704 src+1+src_nparam+src_in+src_out,
705 isl_min(dst_map->extra-div_off, src_map->n_div));
706 if (dst_map->n_div-div_off > src_map->n_div)
707 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
708 div_off+src_map->n_div,
709 dst_map->n_div - div_off - src_map->n_div);
712 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
713 struct isl_basic_map *src_map, isl_int *src,
714 unsigned in_off, unsigned out_off, unsigned div_off)
716 isl_int_set(dst[0], src[0]);
717 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
720 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
721 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
726 if (!bmap1 || !bmap2)
729 div_off = bmap1->n_div;
731 for (i = 0; i < bmap2->n_eq; ++i) {
732 int i1 = isl_basic_map_alloc_equality(bmap1);
735 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
736 i_pos, o_pos, div_off);
739 for (i = 0; i < bmap2->n_ineq; ++i) {
740 int i1 = isl_basic_map_alloc_inequality(bmap1);
743 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
744 i_pos, o_pos, div_off);
747 for (i = 0; i < bmap2->n_div; ++i) {
748 int i1 = isl_basic_map_alloc_div(bmap1);
751 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
752 i_pos, o_pos, div_off);
755 isl_basic_map_free(bmap2);
760 isl_basic_map_free(bmap1);
761 isl_basic_map_free(bmap2);
765 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
766 struct isl_dim_map *dim_map)
770 for (i = 0; i < dim_map->len; ++i) {
771 if (dim_map->pos[i] < 0)
772 isl_int_set_si(dst[i], 0);
774 isl_int_set(dst[i], src[dim_map->pos[i]]);
778 static void copy_div_dim_map(isl_int *dst, isl_int *src,
779 struct isl_dim_map *dim_map)
781 isl_int_set(dst[0], src[0]);
782 copy_constraint_dim_map(dst+1, src+1, dim_map);
785 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
786 struct isl_basic_map *src, struct isl_dim_map *dim_map)
790 if (!src || !dst || !dim_map)
793 for (i = 0; i < src->n_eq; ++i) {
794 int i1 = isl_basic_map_alloc_equality(dst);
797 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
800 for (i = 0; i < src->n_ineq; ++i) {
801 int i1 = isl_basic_map_alloc_inequality(dst);
804 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
807 for (i = 0; i < src->n_div; ++i) {
808 int i1 = isl_basic_map_alloc_div(dst);
811 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
815 isl_basic_map_free(src);
820 isl_basic_map_free(src);
821 isl_basic_map_free(dst);
825 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
826 struct isl_basic_set *bset2, unsigned pos)
828 return (struct isl_basic_set *)
829 add_constraints((struct isl_basic_map *)bset1,
830 (struct isl_basic_map *)bset2, 0, pos);
833 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
834 struct isl_dim *dim, unsigned extra,
835 unsigned n_eq, unsigned n_ineq)
837 struct isl_basic_map *ext;
847 dims_ok = isl_dim_equal(base->dim, dim) &&
848 base->extra >= base->n_div + extra;
850 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
851 room_for_ineq(base, n_ineq)) {
856 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
857 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
858 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
859 extra += base->extra;
861 n_ineq += base->n_ineq;
863 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
869 ext = add_constraints(ext, base, 0, 0);
872 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
879 isl_basic_map_free(base);
883 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
884 struct isl_dim *dim, unsigned extra,
885 unsigned n_eq, unsigned n_ineq)
887 return (struct isl_basic_set *)
888 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
889 extra, n_eq, n_ineq);
892 struct isl_basic_map *isl_basic_map_extend_constraints(
893 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
897 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
901 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
902 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
903 unsigned n_eq, unsigned n_ineq)
905 struct isl_basic_map *bmap;
910 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
914 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
918 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
919 unsigned nparam, unsigned dim, unsigned extra,
920 unsigned n_eq, unsigned n_ineq)
922 return (struct isl_basic_set *)
923 isl_basic_map_extend((struct isl_basic_map *)base,
924 nparam, 0, dim, extra, n_eq, n_ineq);
927 struct isl_basic_set *isl_basic_set_extend_constraints(
928 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
930 return (struct isl_basic_set *)
931 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
935 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
937 return (struct isl_basic_set *)
938 isl_basic_map_cow((struct isl_basic_map *)bset);
941 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
948 bmap = isl_basic_map_dup(bmap);
950 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
954 struct isl_set *isl_set_cow(struct isl_set *set)
962 return isl_set_dup(set);
965 struct isl_map *isl_map_cow(struct isl_map *map)
973 return isl_map_dup(map);
976 static void swap_vars(struct isl_blk blk, isl_int *a,
977 unsigned a_len, unsigned b_len)
979 isl_seq_cpy(blk.data, a+a_len, b_len);
980 isl_seq_cpy(blk.data+b_len, a, a_len);
981 isl_seq_cpy(a, blk.data, b_len+a_len);
984 struct isl_basic_set *isl_basic_set_swap_vars(
985 struct isl_basic_set *bset, unsigned n)
995 nparam = isl_basic_set_n_param(bset);
996 dim = isl_basic_set_n_dim(bset);
997 isl_assert(bset->ctx, n <= dim, goto error);
1002 bset = isl_basic_set_cow(bset);
1006 blk = isl_blk_alloc(bset->ctx, dim);
1007 if (isl_blk_is_error(blk))
1010 for (i = 0; i < bset->n_eq; ++i)
1012 bset->eq[i]+1+nparam, n, dim - n);
1014 for (i = 0; i < bset->n_ineq; ++i)
1016 bset->ineq[i]+1+nparam, n, dim - n);
1018 for (i = 0; i < bset->n_div; ++i)
1020 bset->div[i]+1+1+nparam, n, dim - n);
1022 isl_blk_free(bset->ctx, blk);
1024 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1028 isl_basic_set_free(bset);
1032 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1035 set = isl_set_cow(set);
1039 for (i = 0; i < set->n; ++i) {
1040 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1046 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1050 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1056 total = isl_basic_map_total_dim(bmap);
1057 isl_basic_map_free_div(bmap, bmap->n_div);
1058 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1060 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1062 isl_basic_map_alloc_equality(bmap);
1066 isl_int_set_si(bmap->eq[i][0], 1);
1067 isl_seq_clr(bmap->eq[i]+1, total);
1068 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1069 return isl_basic_map_finalize(bmap);
1071 isl_basic_map_free(bmap);
1075 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1077 return (struct isl_basic_set *)
1078 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1081 static void swap_div(struct isl_basic_map *bmap, int a, int b)
1084 unsigned off = isl_dim_total(bmap->dim);
1085 isl_int *t = bmap->div[a];
1086 bmap->div[a] = bmap->div[b];
1089 for (i = 0; i < bmap->n_eq; ++i)
1090 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1092 for (i = 0; i < bmap->n_ineq; ++i)
1093 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1095 for (i = 0; i < bmap->n_div; ++i)
1096 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1097 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1100 /* Eliminate the specified n dimensions starting at first from the
1101 * constraints using Fourier-Motzkin, The dimensions themselves
1104 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1105 unsigned first, unsigned n)
1115 set = isl_set_cow(set);
1118 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1119 nparam = isl_set_n_param(set);
1121 for (i = 0; i < set->n; ++i) {
1122 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1133 /* Project out n dimensions starting at first using Fourier-Motzkin */
1134 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1135 unsigned first, unsigned n)
1137 set = isl_set_eliminate_dims(set, first, n);
1138 set = isl_set_drop_dims(set, first, n);
1142 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1144 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1152 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1161 set = isl_set_cow(set);
1165 for (i = 0; i < set->n; ++i) {
1166 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1176 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1177 enum isl_dim_type type, unsigned first, unsigned n)
1181 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1185 bmap = isl_basic_map_eliminate_vars(bmap,
1186 isl_basic_map_offset(bmap, type) - 1 + first, n);
1187 bmap = isl_basic_map_drop(bmap, type, first, n);
1190 isl_basic_map_free(bmap);
1194 struct isl_map *isl_map_remove(struct isl_map *map,
1195 enum isl_dim_type type, unsigned first, unsigned n)
1203 map = isl_map_cow(map);
1206 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1208 for (i = 0; i < map->n; ++i) {
1209 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1210 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1214 map = isl_map_drop(map, type, first, n);
1221 /* Project out n inputs starting at first using Fourier-Motzkin */
1222 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1223 unsigned first, unsigned n)
1225 return isl_map_remove(map, isl_dim_in, first, n);
1228 /* Project out n dimensions starting at first using Fourier-Motzkin */
1229 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1230 unsigned first, unsigned n)
1232 unsigned nparam = isl_basic_set_n_param(bset);
1233 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1234 bset = isl_basic_set_drop_dims(bset, first, n);
1238 static void dump_term(struct isl_basic_map *bmap,
1239 isl_int c, int pos, FILE *out)
1242 unsigned in = isl_basic_map_n_in(bmap);
1243 unsigned dim = in + isl_basic_map_n_out(bmap);
1244 unsigned nparam = isl_basic_map_n_param(bmap);
1246 isl_int_print(out, c, 0);
1248 if (!isl_int_is_one(c))
1249 isl_int_print(out, c, 0);
1250 if (pos < 1 + nparam) {
1251 name = isl_dim_get_name(bmap->dim,
1252 isl_dim_param, pos - 1);
1254 fprintf(out, "%s", name);
1256 fprintf(out, "p%d", pos - 1);
1257 } else if (pos < 1 + nparam + in)
1258 fprintf(out, "i%d", pos - 1 - nparam);
1259 else if (pos < 1 + nparam + dim)
1260 fprintf(out, "o%d", pos - 1 - nparam - in);
1262 fprintf(out, "e%d", pos - 1 - nparam - dim);
1266 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1267 int sign, FILE *out)
1271 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1275 for (i = 0, first = 1; i < len; ++i) {
1276 if (isl_int_sgn(c[i]) * sign <= 0)
1279 fprintf(out, " + ");
1281 isl_int_abs(v, c[i]);
1282 dump_term(bmap, v, i, out);
1289 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1290 const char *op, FILE *out, int indent)
1294 fprintf(out, "%*s", indent, "");
1296 dump_constraint_sign(bmap, c, 1, out);
1297 fprintf(out, " %s ", op);
1298 dump_constraint_sign(bmap, c, -1, out);
1302 for (i = bmap->n_div; i < bmap->extra; ++i) {
1303 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1305 fprintf(out, "%*s", indent, "");
1306 fprintf(out, "ERROR: unused div coefficient not zero\n");
1311 static void dump_constraints(struct isl_basic_map *bmap,
1312 isl_int **c, unsigned n,
1313 const char *op, FILE *out, int indent)
1317 for (i = 0; i < n; ++i)
1318 dump_constraint(bmap, c[i], op, out, indent);
1321 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1325 unsigned total = isl_basic_map_total_dim(bmap);
1327 for (j = 0; j < 1 + total; ++j) {
1328 if (isl_int_is_zero(exp[j]))
1330 if (!first && isl_int_is_pos(exp[j]))
1332 dump_term(bmap, exp[j], j, out);
1337 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1341 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1342 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1344 for (i = 0; i < bmap->n_div; ++i) {
1345 fprintf(out, "%*s", indent, "");
1346 fprintf(out, "e%d = [(", i);
1347 dump_affine(bmap, bmap->div[i]+1, out);
1349 isl_int_print(out, bmap->div[i][0], 0);
1350 fprintf(out, "]\n");
1354 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1357 fprintf(out, "null basic set\n");
1361 fprintf(out, "%*s", indent, "");
1362 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1363 bset->ref, bset->dim->nparam, bset->dim->n_out,
1364 bset->extra, bset->flags);
1365 dump((struct isl_basic_map *)bset, out, indent);
1368 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1371 fprintf(out, "null basic map\n");
1375 fprintf(out, "%*s", indent, "");
1376 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1377 "flags: %x, n_name: %d\n",
1379 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1380 bmap->extra, bmap->flags, bmap->dim->n_name);
1381 dump(bmap, out, indent);
1384 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1389 total = isl_basic_map_total_dim(bmap);
1390 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1391 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1392 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1393 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1397 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1399 struct isl_set *set;
1403 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1404 isl_assert(dim->ctx, n >= 0, return NULL);
1405 set = isl_alloc(dim->ctx, struct isl_set,
1406 sizeof(struct isl_set) +
1407 n * sizeof(struct isl_basic_set *));
1411 set->ctx = dim->ctx;
1412 isl_ctx_ref(set->ctx);
1424 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1425 unsigned nparam, unsigned dim, int n, unsigned flags)
1427 struct isl_set *set;
1428 struct isl_dim *dims;
1430 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1434 set = isl_set_alloc_dim(dims, n, flags);
1438 /* Make sure "map" has room for at least "n" more basic maps.
1440 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1443 struct isl_map *grown = NULL;
1447 isl_assert(map->ctx, n >= 0, goto error);
1448 if (map->n + n <= map->size)
1450 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1453 for (i = 0; i < map->n; ++i) {
1454 grown->p[i] = isl_basic_map_copy(map->p[i]);
1462 isl_map_free(grown);
1467 /* Make sure "set" has room for at least "n" more basic sets.
1469 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1471 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1474 struct isl_set *isl_set_dup(struct isl_set *set)
1477 struct isl_set *dup;
1482 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1485 for (i = 0; i < set->n; ++i)
1486 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1490 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1492 struct isl_set *set;
1497 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1499 isl_basic_set_free(bset);
1502 return isl_set_add(set, bset);
1505 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1507 struct isl_map *map;
1512 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1514 isl_basic_map_free(bmap);
1517 return isl_map_add(map, bmap);
1520 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1524 isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1525 isl_assert(set->ctx, set->n < set->size, goto error);
1526 set->p[set->n] = bset;
1533 isl_basic_set_free(bset);
1537 void isl_set_free(struct isl_set *set)
1547 isl_ctx_deref(set->ctx);
1548 for (i = 0; i < set->n; ++i)
1549 isl_basic_set_free(set->p[i]);
1550 isl_dim_free(set->dim);
1554 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1559 fprintf(out, "null set\n");
1563 fprintf(out, "%*s", indent, "");
1564 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1565 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1567 for (i = 0; i < set->n; ++i) {
1568 fprintf(out, "%*s", indent, "");
1569 fprintf(out, "basic set %d:\n", i);
1570 isl_basic_set_dump(set->p[i], out, indent+4);
1574 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1579 fprintf(out, "null map\n");
1583 fprintf(out, "%*s", indent, "");
1584 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1585 "flags: %x, n_name: %d\n",
1586 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1587 map->dim->n_out, map->flags, map->dim->n_name);
1588 for (i = 0; i < map->n; ++i) {
1589 fprintf(out, "%*s", indent, "");
1590 fprintf(out, "basic map %d:\n", i);
1591 isl_basic_map_dump(map->p[i], out, indent+4);
1595 struct isl_basic_map *isl_basic_map_intersect_domain(
1596 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1598 struct isl_basic_map *bmap_domain;
1599 struct isl_dim *dim;
1604 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1605 bset->dim, isl_dim_param), goto error);
1607 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1608 isl_assert(bset->ctx,
1609 isl_basic_map_compatible_domain(bmap, bset), goto error);
1611 bmap = isl_basic_map_cow(bmap);
1612 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1613 bset->n_div, bset->n_eq, bset->n_ineq);
1616 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1617 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1618 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1620 bmap = isl_basic_map_simplify(bmap);
1621 return isl_basic_map_finalize(bmap);
1623 isl_basic_map_free(bmap);
1624 isl_basic_set_free(bset);
1628 struct isl_basic_map *isl_basic_map_intersect_range(
1629 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1631 struct isl_basic_map *bmap_range;
1636 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1637 bset->dim, isl_dim_param), goto error);
1639 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1640 isl_assert(bset->ctx,
1641 isl_basic_map_compatible_range(bmap, bset), goto error);
1643 bmap = isl_basic_map_cow(bmap);
1644 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1645 bset->n_div, bset->n_eq, bset->n_ineq);
1648 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1649 bmap = add_constraints(bmap, bmap_range, 0, 0);
1651 bmap = isl_basic_map_simplify(bmap);
1652 return isl_basic_map_finalize(bmap);
1654 isl_basic_map_free(bmap);
1655 isl_basic_set_free(bset);
1659 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1665 total = 1 + isl_basic_map_total_dim(bmap);
1666 if (total != vec->size)
1671 for (i = 0; i < bmap->n_eq; ++i) {
1672 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1673 if (!isl_int_is_zero(s)) {
1679 for (i = 0; i < bmap->n_ineq; ++i) {
1680 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1681 if (isl_int_is_neg(s)) {
1692 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1694 return basic_map_contains((struct isl_basic_map *)bset, vec);
1697 struct isl_basic_map *isl_basic_map_intersect(
1698 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1700 struct isl_vec *sample = NULL;
1702 if (!bmap1 || !bmap2)
1705 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1706 bmap2->dim, isl_dim_param), goto error);
1707 if (isl_dim_total(bmap1->dim) ==
1708 isl_dim_size(bmap1->dim, isl_dim_param) &&
1709 isl_dim_total(bmap2->dim) !=
1710 isl_dim_size(bmap2->dim, isl_dim_param))
1711 return isl_basic_map_intersect(bmap2, bmap1);
1713 if (isl_dim_total(bmap2->dim) !=
1714 isl_dim_size(bmap2->dim, isl_dim_param))
1715 isl_assert(bmap1->ctx,
1716 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1718 if (bmap1->sample &&
1719 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1720 basic_map_contains(bmap2, bmap1->sample) > 0)
1721 sample = isl_vec_copy(bmap1->sample);
1722 else if (bmap2->sample &&
1723 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1724 basic_map_contains(bmap2, bmap2->sample) > 0)
1725 sample = isl_vec_copy(bmap2->sample);
1727 bmap1 = isl_basic_map_cow(bmap1);
1728 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1729 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1732 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1735 isl_vec_free(bmap1->sample);
1736 bmap1->sample = sample;
1739 bmap1 = isl_basic_map_simplify(bmap1);
1740 return isl_basic_map_finalize(bmap1);
1743 isl_vec_free(sample);
1744 isl_basic_map_free(bmap1);
1745 isl_basic_map_free(bmap2);
1749 struct isl_basic_set *isl_basic_set_intersect(
1750 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1752 return (struct isl_basic_set *)
1753 isl_basic_map_intersect(
1754 (struct isl_basic_map *)bset1,
1755 (struct isl_basic_map *)bset2);
1758 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1761 struct isl_map *result;
1767 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1768 map2->dim, isl_dim_param), goto error);
1769 if (isl_dim_total(map1->dim) ==
1770 isl_dim_size(map1->dim, isl_dim_param) &&
1771 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1772 return isl_map_intersect(map2, map1);
1774 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1775 isl_assert(map1->ctx,
1776 isl_dim_equal(map1->dim, map2->dim), goto error);
1778 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1779 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1780 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1782 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1783 map1->n * map2->n, flags);
1786 for (i = 0; i < map1->n; ++i)
1787 for (j = 0; j < map2->n; ++j) {
1788 struct isl_basic_map *part;
1789 part = isl_basic_map_intersect(
1790 isl_basic_map_copy(map1->p[i]),
1791 isl_basic_map_copy(map2->p[j]));
1792 if (isl_basic_map_is_empty(part))
1793 isl_basic_map_free(part);
1795 result = isl_map_add(result, part);
1808 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1810 return (struct isl_set *)
1811 isl_map_intersect((struct isl_map *)set1,
1812 (struct isl_map *)set2);
1815 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1817 struct isl_dim *dim;
1818 struct isl_basic_set *bset;
1823 bmap = isl_basic_map_cow(bmap);
1826 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1827 in = isl_basic_map_n_in(bmap);
1828 bset = isl_basic_set_from_basic_map(bmap);
1829 bset = isl_basic_set_swap_vars(bset, in);
1830 return isl_basic_map_from_basic_set(bset, dim);
1833 /* Turn final n dimensions into existentially quantified variables.
1835 struct isl_basic_set *isl_basic_set_project_out(
1836 struct isl_basic_set *bset, unsigned n, unsigned flags)
1846 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1851 bset = isl_basic_set_cow(bset);
1853 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1854 old = bset->block2.data;
1855 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1856 (bset->extra + n) * (1 + row_size));
1857 if (!bset->block2.data)
1859 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1862 for (i = 0; i < n; ++i) {
1863 new_div[i] = bset->block2.data +
1864 (bset->extra + i) * (1 + row_size);
1865 isl_seq_clr(new_div[i], 1 + row_size);
1867 for (i = 0; i < bset->extra; ++i)
1868 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1870 bset->div = new_div;
1873 bset->dim = isl_dim_drop_outputs(bset->dim,
1874 isl_basic_set_n_dim(bset) - n, n);
1877 bset = isl_basic_set_simplify(bset);
1878 return isl_basic_set_finalize(bset);
1880 isl_basic_set_free(bset);
1884 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1888 for (i = 0; i < n; ++i) {
1889 j = isl_basic_map_alloc_div(bmap);
1892 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1896 isl_basic_map_free(bmap);
1900 struct isl_basic_map *isl_basic_map_apply_range(
1901 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1903 struct isl_dim *dim_result = NULL;
1904 struct isl_basic_map *bmap;
1905 unsigned n_in, n_out, n, nparam, total, pos;
1906 struct isl_dim_map *dim_map1, *dim_map2;
1908 if (!bmap1 || !bmap2)
1911 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1912 isl_dim_copy(bmap2->dim));
1914 n_in = isl_basic_map_n_in(bmap1);
1915 n_out = isl_basic_map_n_out(bmap2);
1916 n = isl_basic_map_n_out(bmap1);
1917 nparam = isl_basic_map_n_param(bmap1);
1919 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1920 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1921 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1922 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1923 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1924 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1925 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1926 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1927 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1928 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1929 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1931 bmap = isl_basic_map_alloc_dim(dim_result,
1932 bmap1->n_div + bmap2->n_div + n,
1933 bmap1->n_eq + bmap2->n_eq,
1934 bmap1->n_ineq + bmap2->n_ineq);
1935 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1936 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1937 bmap = add_divs(bmap, n);
1938 bmap = isl_basic_map_simplify(bmap);
1939 return isl_basic_map_finalize(bmap);
1941 isl_basic_map_free(bmap1);
1942 isl_basic_map_free(bmap2);
1946 struct isl_basic_set *isl_basic_set_apply(
1947 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1952 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1955 return (struct isl_basic_set *)
1956 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1958 isl_basic_set_free(bset);
1959 isl_basic_map_free(bmap);
1963 struct isl_basic_map *isl_basic_map_apply_domain(
1964 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1966 if (!bmap1 || !bmap2)
1970 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1972 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1975 bmap1 = isl_basic_map_reverse(bmap1);
1976 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1977 return isl_basic_map_reverse(bmap1);
1979 isl_basic_map_free(bmap1);
1980 isl_basic_map_free(bmap2);
1984 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1985 * A \cap B -> f(A) + f(B)
1987 struct isl_basic_map *isl_basic_map_sum(
1988 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1990 unsigned n_in, n_out, nparam, total, pos;
1991 struct isl_basic_map *bmap = NULL;
1992 struct isl_dim_map *dim_map1, *dim_map2;
1995 if (!bmap1 || !bmap2)
1998 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2001 nparam = isl_basic_map_n_param(bmap1);
2002 n_in = isl_basic_map_n_in(bmap1);
2003 n_out = isl_basic_map_n_out(bmap1);
2005 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2006 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2007 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2008 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2009 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2010 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2011 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2012 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2013 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2014 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2015 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2017 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2018 bmap1->n_div + bmap2->n_div + 2 * n_out,
2019 bmap1->n_eq + bmap2->n_eq + n_out,
2020 bmap1->n_ineq + bmap2->n_ineq);
2021 for (i = 0; i < n_out; ++i) {
2022 int j = isl_basic_map_alloc_equality(bmap);
2025 isl_seq_clr(bmap->eq[j], 1+total);
2026 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2027 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2028 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2030 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2031 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2032 bmap = add_divs(bmap, 2 * n_out);
2034 bmap = isl_basic_map_simplify(bmap);
2035 return isl_basic_map_finalize(bmap);
2037 isl_basic_map_free(bmap);
2038 isl_basic_map_free(bmap1);
2039 isl_basic_map_free(bmap2);
2043 /* Given a basic map A -> f(A), construct A -> -f(A).
2045 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2050 bmap = isl_basic_map_cow(bmap);
2054 n = isl_basic_map_dim(bmap, isl_dim_out);
2055 off = isl_basic_map_offset(bmap, isl_dim_out);
2056 for (i = 0; i < bmap->n_eq; ++i)
2057 for (j = 0; j < n; ++j)
2058 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2059 for (i = 0; i < bmap->n_ineq; ++i)
2060 for (j = 0; j < n; ++j)
2061 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2062 for (i = 0; i < bmap->n_div; ++i)
2063 for (j = 0; j < n; ++j)
2064 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2065 return isl_basic_map_finalize(bmap);
2068 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2069 * A -> floor(f(A)/d).
2071 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2074 unsigned n_in, n_out, nparam, total, pos;
2075 struct isl_basic_map *result = NULL;
2076 struct isl_dim_map *dim_map;
2082 nparam = isl_basic_map_n_param(bmap);
2083 n_in = isl_basic_map_n_in(bmap);
2084 n_out = isl_basic_map_n_out(bmap);
2086 total = nparam + n_in + n_out + bmap->n_div + n_out;
2087 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2088 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2089 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2090 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2091 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2093 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2094 bmap->n_div + n_out,
2095 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2096 result = add_constraints_dim_map(result, bmap, dim_map);
2097 result = add_divs(result, n_out);
2098 for (i = 0; i < n_out; ++i) {
2100 j = isl_basic_map_alloc_inequality(result);
2103 isl_seq_clr(result->ineq[j], 1+total);
2104 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2105 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2106 j = isl_basic_map_alloc_inequality(result);
2109 isl_seq_clr(result->ineq[j], 1+total);
2110 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2111 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2112 isl_int_sub_ui(result->ineq[j][0], d, 1);
2115 result = isl_basic_map_simplify(result);
2116 return isl_basic_map_finalize(result);
2118 isl_basic_map_free(result);
2122 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2128 i = isl_basic_map_alloc_equality(bmap);
2131 nparam = isl_basic_map_n_param(bmap);
2132 n_in = isl_basic_map_n_in(bmap);
2133 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2134 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2135 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2136 return isl_basic_map_finalize(bmap);
2138 isl_basic_map_free(bmap);
2142 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2148 i = isl_basic_map_alloc_inequality(bmap);
2151 nparam = isl_basic_map_n_param(bmap);
2152 n_in = isl_basic_map_n_in(bmap);
2153 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2154 isl_int_set_si(bmap->ineq[i][0], -1);
2155 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2156 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2157 return isl_basic_map_finalize(bmap);
2159 isl_basic_map_free(bmap);
2163 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2169 i = isl_basic_map_alloc_inequality(bmap);
2172 nparam = isl_basic_map_n_param(bmap);
2173 n_in = isl_basic_map_n_in(bmap);
2174 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2175 isl_int_set_si(bmap->ineq[i][0], -1);
2176 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2177 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2178 return isl_basic_map_finalize(bmap);
2180 isl_basic_map_free(bmap);
2184 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2187 struct isl_basic_map *bmap;
2188 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2191 for (i = 0; i < n_equal && bmap; ++i)
2192 bmap = var_equal(bmap, i);
2193 return isl_basic_map_finalize(bmap);
2196 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2199 struct isl_basic_map *bmap;
2200 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2203 for (i = 0; i < pos && bmap; ++i)
2204 bmap = var_equal(bmap, i);
2206 bmap = var_less(bmap, pos);
2207 return isl_basic_map_finalize(bmap);
2210 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2213 struct isl_basic_map *bmap;
2214 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2217 for (i = 0; i < pos && bmap; ++i)
2218 bmap = var_equal(bmap, i);
2220 bmap = var_more(bmap, pos);
2221 return isl_basic_map_finalize(bmap);
2224 struct isl_basic_map *isl_basic_map_from_basic_set(
2225 struct isl_basic_set *bset, struct isl_dim *dim)
2227 struct isl_basic_map *bmap;
2229 bset = isl_basic_set_cow(bset);
2233 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2234 isl_dim_free(bset->dim);
2235 bmap = (struct isl_basic_map *) bset;
2237 return isl_basic_map_finalize(bmap);
2239 isl_basic_set_free(bset);
2244 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2248 if (bmap->dim->n_in == 0)
2249 return (struct isl_basic_set *)bmap;
2250 bmap = isl_basic_map_cow(bmap);
2253 bmap->dim = isl_dim_cow(bmap->dim);
2256 bmap->dim->n_out += bmap->dim->n_in;
2257 bmap->dim->n_in = 0;
2258 bmap = isl_basic_map_finalize(bmap);
2259 return (struct isl_basic_set *)bmap;
2261 isl_basic_map_free(bmap);
2265 /* For a div d = floor(f/m), add the constraints
2268 * -(f-(n-1)) + m d >= 0
2270 * Note that the second constraint is the negation of
2274 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2277 unsigned total = isl_basic_map_total_dim(bmap);
2278 unsigned div_pos = 1 + total - bmap->n_div + div;
2280 i = isl_basic_map_alloc_inequality(bmap);
2283 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2284 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2286 j = isl_basic_map_alloc_inequality(bmap);
2289 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2290 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2291 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2295 struct isl_basic_set *isl_basic_map_underlying_set(
2296 struct isl_basic_map *bmap)
2300 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2301 return (struct isl_basic_set *)bmap;
2302 bmap = isl_basic_map_cow(bmap);
2305 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2308 bmap->extra -= bmap->n_div;
2310 bmap = isl_basic_map_finalize(bmap);
2311 return (struct isl_basic_set *)bmap;
2316 struct isl_basic_map *isl_basic_map_overlying_set(
2317 struct isl_basic_set *bset, struct isl_basic_map *like)
2319 struct isl_basic_map *bmap;
2320 struct isl_ctx *ctx;
2327 isl_assert(ctx, bset->n_div == 0, goto error);
2328 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2329 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2331 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2332 isl_basic_map_free(like);
2333 return (struct isl_basic_map *)bset;
2335 bset = isl_basic_set_cow(bset);
2338 total = bset->dim->n_out + bset->extra;
2339 bmap = (struct isl_basic_map *)bset;
2340 isl_dim_free(bmap->dim);
2341 bmap->dim = isl_dim_copy(like->dim);
2344 bmap->n_div = like->n_div;
2345 bmap->extra += like->n_div;
2348 ltotal = total - bmap->extra + like->extra;
2351 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2352 bmap->extra * (1 + 1 + total));
2353 if (isl_blk_is_error(bmap->block2))
2355 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2359 for (i = 0; i < bmap->extra; ++i)
2360 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2361 for (i = 0; i < like->n_div; ++i) {
2362 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2363 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2365 bmap = isl_basic_map_extend_constraints(bmap,
2366 0, 2 * like->n_div);
2367 for (i = 0; i < like->n_div; ++i) {
2368 if (isl_int_is_zero(bmap->div[i][0]))
2370 if (add_div_constraints(bmap, i) < 0)
2374 isl_basic_map_free(like);
2375 bmap = isl_basic_map_simplify(bmap);
2376 bmap = isl_basic_map_finalize(bmap);
2379 isl_basic_map_free(like);
2380 isl_basic_set_free(bset);
2384 struct isl_basic_set *isl_basic_set_from_underlying_set(
2385 struct isl_basic_set *bset, struct isl_basic_set *like)
2387 return (struct isl_basic_set *)
2388 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2391 struct isl_set *isl_set_from_underlying_set(
2392 struct isl_set *set, struct isl_basic_set *like)
2398 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2400 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2401 isl_basic_set_free(like);
2404 set = isl_set_cow(set);
2407 for (i = 0; i < set->n; ++i) {
2408 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2409 isl_basic_set_copy(like));
2413 isl_dim_free(set->dim);
2414 set->dim = isl_dim_copy(like->dim);
2417 isl_basic_set_free(like);
2420 isl_basic_set_free(like);
2425 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2429 map = isl_map_cow(map);
2432 map->dim = isl_dim_cow(map->dim);
2436 for (i = 1; i < map->n; ++i)
2437 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2439 for (i = 0; i < map->n; ++i) {
2440 map->p[i] = (struct isl_basic_map *)
2441 isl_basic_map_underlying_set(map->p[i]);
2446 map->dim = isl_dim_underlying(map->dim, 0);
2448 isl_dim_free(map->dim);
2449 map->dim = isl_dim_copy(map->p[0]->dim);
2453 return (struct isl_set *)map;
2459 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2461 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2464 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2466 struct isl_basic_set *domain;
2470 n_out = isl_basic_map_n_out(bmap);
2471 domain = isl_basic_set_from_basic_map(bmap);
2472 return isl_basic_set_project_out(domain, n_out, 0);
2475 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2477 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2480 struct isl_set *isl_map_range(struct isl_map *map)
2483 struct isl_set *set;
2487 map = isl_map_cow(map);
2491 set = (struct isl_set *) map;
2492 if (set->dim->n_in != 0) {
2493 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2497 for (i = 0; i < map->n; ++i) {
2498 set->p[i] = isl_basic_map_range(map->p[i]);
2502 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2503 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2510 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2513 struct isl_map *map = NULL;
2515 set = isl_set_cow(set);
2518 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2519 map = (struct isl_map *)set;
2520 for (i = 0; i < set->n; ++i) {
2521 map->p[i] = isl_basic_map_from_basic_set(
2522 set->p[i], isl_dim_copy(dim));
2526 isl_dim_free(map->dim);
2535 struct isl_map *isl_map_from_range(struct isl_set *set)
2537 return (struct isl_map *)set;
2540 struct isl_set *isl_set_from_map(struct isl_map *map)
2543 struct isl_set *set = NULL;
2547 map = isl_map_cow(map);
2550 map->dim = isl_dim_cow(map->dim);
2553 map->dim->n_out += map->dim->n_in;
2555 set = (struct isl_set *)map;
2556 for (i = 0; i < map->n; ++i) {
2557 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2567 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2569 struct isl_map *map;
2573 isl_assert(dim->ctx, n >= 0, return NULL);
2574 map = isl_alloc(dim->ctx, struct isl_map,
2575 sizeof(struct isl_map) +
2576 n * sizeof(struct isl_basic_map *));
2580 map->ctx = dim->ctx;
2581 isl_ctx_ref(map->ctx);
2593 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2594 unsigned nparam, unsigned in, unsigned out, int n,
2597 struct isl_map *map;
2598 struct isl_dim *dims;
2600 dims = isl_dim_alloc(ctx, nparam, in, out);
2604 map = isl_map_alloc_dim(dims, n, flags);
2608 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2609 unsigned nparam, unsigned in, unsigned out)
2611 struct isl_basic_map *bmap;
2612 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2613 bmap = isl_basic_map_set_to_empty(bmap);
2617 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2619 struct isl_basic_set *bset;
2620 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2621 bset = isl_basic_set_set_to_empty(bset);
2625 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2627 struct isl_basic_map *bmap;
2630 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2631 bmap = isl_basic_map_set_to_empty(bmap);
2635 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2637 struct isl_basic_map *bmap;
2640 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2641 bmap = isl_basic_map_set_to_empty(bmap);
2645 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2647 struct isl_basic_set *bset;
2650 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2651 bset = isl_basic_set_set_to_empty(bset);
2655 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2657 struct isl_basic_map *bmap;
2658 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2662 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2664 struct isl_basic_set *bset;
2665 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2669 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2673 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2676 struct isl_map *isl_map_empty(struct isl_dim *dim)
2678 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2681 struct isl_map *isl_map_empty_like(struct isl_map *model)
2685 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2688 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2692 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2695 struct isl_set *isl_set_empty(struct isl_dim *dim)
2697 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2700 struct isl_set *isl_set_empty_like(struct isl_set *model)
2704 return isl_set_empty(isl_dim_copy(model->dim));
2707 struct isl_map *isl_map_universe(struct isl_dim *dim)
2709 struct isl_map *map;
2712 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2713 map = isl_map_add(map, isl_basic_map_universe(dim));
2717 struct isl_set *isl_set_universe(struct isl_dim *dim)
2719 struct isl_set *set;
2722 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2723 set = isl_set_add(set, isl_basic_set_universe(dim));
2727 struct isl_map *isl_map_dup(struct isl_map *map)
2730 struct isl_map *dup;
2734 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2735 for (i = 0; i < map->n; ++i)
2736 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2740 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2744 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2745 isl_assert(map->ctx, map->n < map->size, goto error);
2746 map->p[map->n] = bmap;
2748 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2754 isl_basic_map_free(bmap);
2758 void isl_map_free(struct isl_map *map)
2768 isl_ctx_deref(map->ctx);
2769 for (i = 0; i < map->n; ++i)
2770 isl_basic_map_free(map->p[i]);
2771 isl_dim_free(map->dim);
2775 struct isl_map *isl_map_extend(struct isl_map *base,
2776 unsigned nparam, unsigned n_in, unsigned n_out)
2780 base = isl_map_cow(base);
2784 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2787 for (i = 0; i < base->n; ++i) {
2788 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2789 isl_dim_copy(base->dim), 0, 0, 0);
2799 struct isl_set *isl_set_extend(struct isl_set *base,
2800 unsigned nparam, unsigned dim)
2802 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2806 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2807 unsigned pos, int value)
2811 bmap = isl_basic_map_cow(bmap);
2812 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2813 j = isl_basic_map_alloc_equality(bmap);
2816 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
2817 isl_int_set_si(bmap->eq[j][pos], -1);
2818 isl_int_set_si(bmap->eq[j][0], value);
2819 bmap = isl_basic_map_simplify(bmap);
2820 return isl_basic_map_finalize(bmap);
2822 isl_basic_map_free(bmap);
2826 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2827 enum isl_dim_type type, unsigned pos, int value)
2831 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2832 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2835 isl_basic_map_free(bmap);
2839 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2840 enum isl_dim_type type, unsigned pos, int value)
2842 return (struct isl_basic_set *)
2843 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2847 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2848 unsigned input, int value)
2850 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2853 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2854 unsigned dim, int value)
2856 return (struct isl_basic_set *)
2857 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2858 isl_dim_set, dim, value);
2861 struct isl_map *isl_map_fix_si(struct isl_map *map,
2862 enum isl_dim_type type, unsigned pos, int value)
2866 map = isl_map_cow(map);
2870 isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2871 for (i = 0; i < map->n; ++i) {
2872 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2876 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2883 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2884 unsigned input, int value)
2886 return isl_map_fix_si(map, isl_dim_in, input, value);
2889 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2891 return (struct isl_set *)
2892 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2895 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2896 unsigned dim, isl_int value)
2901 bset = isl_basic_set_cow(bset);
2902 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2903 j = isl_basic_set_alloc_inequality(bset);
2906 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2907 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2908 isl_int_neg(bset->ineq[j][0], value);
2909 bset = isl_basic_set_simplify(bset);
2910 return isl_basic_set_finalize(bset);
2912 isl_basic_set_free(bset);
2916 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2921 set = isl_set_cow(set);
2925 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2926 for (i = 0; i < set->n; ++i) {
2927 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2937 struct isl_map *isl_map_reverse(struct isl_map *map)
2942 map = isl_map_cow(map);
2946 map->dim = isl_dim_reverse(map->dim);
2949 for (i = 0; i < map->n; ++i) {
2950 map->p[i] = isl_basic_map_reverse(map->p[i]);
2954 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2961 struct isl_map *isl_basic_map_lexmax(
2962 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2963 struct isl_set **empty)
2965 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2968 struct isl_map *isl_basic_map_lexmin(
2969 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2970 struct isl_set **empty)
2972 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2975 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2977 struct isl_basic_map *bmap = NULL;
2978 struct isl_basic_set *dom = NULL;
2979 struct isl_map *min;
2980 struct isl_dim *param_dim;
2984 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2987 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2988 dom = isl_basic_set_universe(param_dim);
2991 min = isl_basic_map_lexmin(bmap, dom, NULL);
2992 return isl_map_range(min);
2994 isl_basic_map_free(bmap);
2998 /* If bmap contains any unknown divs, then compute explicit
2999 * expressions for them. However, this computation may be
3000 * quite expensive, so first try to remove divs that aren't
3003 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
3010 off = isl_dim_total(bmap->dim);
3011 for (i = 0; i < bmap->n_div; ++i) {
3012 if (isl_int_is_zero(bmap->div[i][0]))
3014 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
3017 if (i == bmap->n_div)
3018 return isl_map_from_basic_map(bmap);
3019 bmap = isl_basic_map_drop_redundant_divs(bmap);
3022 for (i = 0; i < bmap->n_div; ++i)
3023 if (isl_int_is_zero(bmap->div[i][0]))
3025 if (i == bmap->n_div)
3026 return isl_map_from_basic_map(bmap);
3027 struct isl_map *map = isl_pip_basic_map_compute_divs(bmap);
3030 isl_basic_map_free(bmap);
3034 struct isl_map *isl_map_compute_divs(struct isl_map *map)
3037 struct isl_map *res;
3043 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
3044 for (i = 1 ; i < map->n; ++i) {
3046 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
3047 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3048 res = isl_map_union_disjoint(res, r2);
3050 res = isl_map_union(res, r2);
3057 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3059 return (struct isl_set *)
3060 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3063 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3065 return (struct isl_set *)
3066 isl_map_compute_divs((struct isl_map *)set);
3069 struct isl_set *isl_map_domain(struct isl_map *map)
3072 struct isl_set *set;
3077 map = isl_map_cow(map);
3081 set = (struct isl_set *)map;
3082 set->dim = isl_dim_domain(set->dim);
3085 for (i = 0; i < map->n; ++i) {
3086 set->p[i] = isl_basic_map_domain(map->p[i]);
3090 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3091 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3098 struct isl_map *isl_map_union_disjoint(
3099 struct isl_map *map1, struct isl_map *map2)
3103 struct isl_map *map = NULL;
3117 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3119 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3120 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3121 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3123 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3124 map1->n + map2->n, flags);
3127 for (i = 0; i < map1->n; ++i) {
3128 map = isl_map_add(map,
3129 isl_basic_map_copy(map1->p[i]));
3133 for (i = 0; i < map2->n; ++i) {
3134 map = isl_map_add(map,
3135 isl_basic_map_copy(map2->p[i]));
3149 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3151 map1 = isl_map_union_disjoint(map1, map2);
3155 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3159 struct isl_set *isl_set_union_disjoint(
3160 struct isl_set *set1, struct isl_set *set2)
3162 return (struct isl_set *)
3163 isl_map_union_disjoint(
3164 (struct isl_map *)set1, (struct isl_map *)set2);
3167 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3169 return (struct isl_set *)
3170 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3173 struct isl_map *isl_map_intersect_range(
3174 struct isl_map *map, struct isl_set *set)
3177 struct isl_map *result;
3183 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3184 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3185 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3187 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3188 map->n * set->n, flags);
3191 for (i = 0; i < map->n; ++i)
3192 for (j = 0; j < set->n; ++j) {
3193 result = isl_map_add(result,
3194 isl_basic_map_intersect_range(
3195 isl_basic_map_copy(map->p[i]),
3196 isl_basic_set_copy(set->p[j])));
3209 struct isl_map *isl_map_intersect_domain(
3210 struct isl_map *map, struct isl_set *set)
3212 return isl_map_reverse(
3213 isl_map_intersect_range(isl_map_reverse(map), set));
3216 struct isl_map *isl_map_apply_domain(
3217 struct isl_map *map1, struct isl_map *map2)
3221 map1 = isl_map_reverse(map1);
3222 map1 = isl_map_apply_range(map1, map2);
3223 return isl_map_reverse(map1);
3230 struct isl_map *isl_map_apply_range(
3231 struct isl_map *map1, struct isl_map *map2)
3233 struct isl_dim *dim_result;
3234 struct isl_map *result;
3243 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3244 isl_dim_copy(map2->dim));
3246 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3249 for (i = 0; i < map1->n; ++i)
3250 for (j = 0; j < map2->n; ++j) {
3251 result = isl_map_add(result,
3252 isl_basic_map_apply_range(
3253 isl_basic_map_copy(map1->p[i]),
3254 isl_basic_map_copy(map2->p[j])));
3260 if (result && result->n <= 1)
3261 ISL_F_SET(result, ISL_MAP_DISJOINT);
3270 * returns range - domain
3272 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3274 struct isl_basic_set *bset;
3281 dim = isl_basic_map_n_in(bmap);
3282 nparam = isl_basic_map_n_param(bmap);
3283 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3284 bset = isl_basic_set_from_basic_map(bmap);
3285 bset = isl_basic_set_cow(bset);
3286 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3287 bset = isl_basic_set_swap_vars(bset, 2*dim);
3288 for (i = 0; i < dim; ++i) {
3289 int j = isl_basic_map_alloc_equality(
3290 (struct isl_basic_map *)bset);
3293 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3294 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3295 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3296 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3298 return isl_basic_set_project_out(bset, 2*dim, 0);
3300 isl_basic_map_free(bmap);
3305 * returns range - domain
3307 struct isl_set *isl_map_deltas(struct isl_map *map)
3310 struct isl_set *result;
3315 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3316 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3317 isl_map_n_in(map), map->n, map->flags);
3320 for (i = 0; i < map->n; ++i)
3321 result = isl_set_add(result,
3322 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3330 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3332 struct isl_basic_map *bmap;
3340 nparam = dims->nparam;
3342 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3346 for (i = 0; i < dim; ++i) {
3347 int j = isl_basic_map_alloc_equality(bmap);
3350 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3351 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3352 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3354 return isl_basic_map_finalize(bmap);
3356 isl_basic_map_free(bmap);
3360 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3362 struct isl_dim *dim = isl_dim_map(set_dim);
3365 return basic_map_identity(dim);
3368 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3370 if (!model || !model->dim)
3372 isl_assert(model->ctx,
3373 model->dim->n_in == model->dim->n_out, return NULL);
3374 return basic_map_identity(isl_dim_copy(model->dim));
3377 static struct isl_map *map_identity(struct isl_dim *dim)
3379 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3380 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3383 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3385 struct isl_dim *dim = isl_dim_map(set_dim);
3388 return map_identity(dim);
3391 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3393 if (!model || !model->dim)
3395 isl_assert(model->ctx,
3396 model->dim->n_in == model->dim->n_out, return NULL);
3397 return map_identity(isl_dim_copy(model->dim));
3400 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3402 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3405 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3407 return isl_map_is_subset(
3408 (struct isl_map *)set1, (struct isl_map *)set2);
3411 int isl_basic_map_is_subset(
3412 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3415 struct isl_map *map1;
3416 struct isl_map *map2;
3418 if (!bmap1 || !bmap2)
3421 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3422 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3424 is_subset = isl_map_is_subset(map1, map2);
3432 int isl_basic_map_is_equal(
3433 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3437 if (!bmap1 || !bmap2)
3439 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3442 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3446 int isl_basic_set_is_equal(
3447 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3449 return isl_basic_map_is_equal(
3450 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3453 int isl_map_is_empty(struct isl_map *map)
3460 for (i = 0; i < map->n; ++i) {
3461 is_empty = isl_basic_map_is_empty(map->p[i]);
3470 int isl_map_fast_is_empty(struct isl_map *map)
3475 int isl_set_is_empty(struct isl_set *set)
3477 return isl_map_is_empty((struct isl_map *)set);
3480 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3484 struct isl_map *diff;
3489 if (isl_map_is_empty(map1))
3492 if (isl_map_is_empty(map2))
3495 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3499 is_subset = isl_map_is_empty(diff);
3505 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3511 is_subset = isl_map_is_subset(map1, map2);
3514 is_subset = isl_map_is_subset(map2, map1);
3518 int isl_basic_map_is_strict_subset(
3519 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3523 if (!bmap1 || !bmap2)
3525 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3528 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3529 if (is_subset == -1)
3534 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3538 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3541 int isl_basic_set_is_universe(struct isl_basic_set *bset)
3545 return bset->n_eq == 0 && bset->n_ineq == 0;
3548 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3550 struct isl_basic_set *bset = NULL;
3551 struct isl_vec *sample = NULL;
3558 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3561 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3562 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3563 copy = isl_basic_map_convex_hull(copy);
3564 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3565 isl_basic_map_free(copy);
3569 total = 1 + isl_basic_map_total_dim(bmap);
3570 if (bmap->sample && bmap->sample->size == total) {
3571 int contains = basic_map_contains(bmap, bmap->sample);
3577 isl_vec_free(bmap->sample);
3578 bmap->sample = NULL;
3579 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3582 sample = isl_basic_set_sample(bset);
3585 empty = sample->size == 0;
3586 isl_vec_free(bmap->sample);
3587 bmap->sample = sample;
3589 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
3594 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
3598 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
3601 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
3605 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
3608 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3610 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3613 struct isl_map *isl_basic_map_union(
3614 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3616 struct isl_map *map;
3617 if (!bmap1 || !bmap2)
3620 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3622 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3625 map = isl_map_add(map, bmap1);
3626 map = isl_map_add(map, bmap2);
3629 isl_basic_map_free(bmap1);
3630 isl_basic_map_free(bmap2);
3634 struct isl_set *isl_basic_set_union(
3635 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3637 return (struct isl_set *)isl_basic_map_union(
3638 (struct isl_basic_map *)bset1,
3639 (struct isl_basic_map *)bset2);
3642 /* Order divs such that any div only depends on previous divs */
3643 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3646 unsigned off = isl_dim_total(bmap->dim);
3648 for (i = 0; i < bmap->n_div; ++i) {
3650 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3654 swap_div(bmap, i, i + pos);
3660 /* Look for a div in dst that corresponds to the div "div" in src.
3661 * The divs before "div" in src and dst are assumed to be the same.
3663 * Returns -1 if no corresponding div was found and the position
3664 * of the corresponding div in dst otherwise.
3666 static int find_div(struct isl_basic_map *dst,
3667 struct isl_basic_map *src, unsigned div)
3671 unsigned total = isl_dim_total(src->dim);
3673 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3674 for (i = div; i < dst->n_div; ++i)
3675 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3676 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3677 dst->n_div - div) == -1)
3682 struct isl_basic_map *isl_basic_map_align_divs(
3683 struct isl_basic_map *dst, struct isl_basic_map *src)
3686 unsigned total = isl_dim_total(src->dim);
3691 if (src->n_div == 0)
3694 for (i = 0; i < src->n_div; ++i)
3695 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3697 src = order_divs(src);
3698 dst = isl_basic_map_cow(dst);
3699 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3700 src->n_div, 0, 2 * src->n_div);
3703 for (i = 0; i < src->n_div; ++i) {
3704 int j = find_div(dst, src, i);
3706 j = isl_basic_map_alloc_div(dst);
3709 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3710 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3711 if (add_div_constraints(dst, j) < 0)
3715 swap_div(dst, i, j);
3719 isl_basic_map_free(dst);
3723 struct isl_basic_set *isl_basic_set_align_divs(
3724 struct isl_basic_set *dst, struct isl_basic_set *src)
3726 return (struct isl_basic_set *)isl_basic_map_align_divs(
3727 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3730 struct isl_map *isl_map_align_divs(struct isl_map *map)
3734 map = isl_map_compute_divs(map);
3735 map = isl_map_cow(map);
3739 for (i = 1; i < map->n; ++i)
3740 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3741 for (i = 1; i < map->n; ++i)
3742 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3744 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3748 struct isl_set *isl_set_align_divs(struct isl_set *set)
3750 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
3753 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3754 struct isl_basic_map *src, isl_int *c,
3755 unsigned len, int oppose)
3757 struct isl_basic_map *copy = NULL;
3762 copy = isl_basic_map_copy(src);
3763 copy = isl_basic_map_cow(copy);
3766 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3767 k = isl_basic_map_alloc_inequality(copy);
3771 isl_seq_neg(copy->ineq[k], c, len);
3773 isl_seq_cpy(copy->ineq[k], c, len);
3774 total = 1 + isl_basic_map_total_dim(copy);
3775 isl_seq_clr(copy->ineq[k]+len, total - len);
3776 isl_inequality_negate(copy, k);
3777 copy = isl_basic_map_simplify(copy);
3778 copy = isl_basic_map_finalize(copy);
3779 is_empty = isl_basic_map_is_empty(copy);
3783 dst = isl_map_add(dst, copy);
3785 isl_basic_map_free(copy);
3788 isl_basic_map_free(copy);
3793 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3797 struct isl_map *rest = NULL;
3799 unsigned total = isl_basic_map_total_dim(bmap);
3806 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3807 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3809 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3810 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3814 for (i = 0; i < map->n; ++i) {
3815 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3820 for (j = 0; j < map->n; ++j)
3821 map->p[j] = isl_basic_map_cow(map->p[j]);
3823 for (i = 0; i < bmap->n_eq; ++i) {
3824 for (j = 0; j < map->n; ++j) {
3825 rest = add_cut_constraint(rest,
3826 map->p[j], bmap->eq[i], 1+total, 0);
3830 rest = add_cut_constraint(rest,
3831 map->p[j], bmap->eq[i], 1+total, 1);
3835 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3839 k = isl_basic_map_alloc_equality(map->p[j]);
3842 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3843 isl_seq_clr(map->p[j]->eq[k]+1+total,
3844 map->p[j]->n_div - bmap->n_div);
3848 for (i = 0; i < bmap->n_ineq; ++i) {
3849 for (j = 0; j < map->n; ++j) {
3850 rest = add_cut_constraint(rest,
3851 map->p[j], bmap->ineq[i], 1+total, 0);
3855 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3859 k = isl_basic_map_alloc_inequality(map->p[j]);
3862 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3863 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3864 map->p[j]->n_div - bmap->n_div);
3876 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3882 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3884 if (isl_map_is_empty(map2)) {
3889 map1 = isl_map_compute_divs(map1);
3890 map2 = isl_map_compute_divs(map2);
3894 for (i = 0; map1 && i < map2->n; ++i)
3895 map1 = subtract(map1, map2->p[i]);
3905 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3907 return (struct isl_set *)
3909 (struct isl_map *)set1, (struct isl_map *)set2);
3912 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3916 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3917 map = isl_map_intersect_domain(map, set);
3918 set = isl_map_range(map);
3926 /* There is no need to cow as removing empty parts doesn't change
3927 * the meaning of the set.
3929 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3936 for (i = map->n-1; i >= 0; --i) {
3937 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3939 isl_basic_map_free(map->p[i]);
3940 if (i != map->n-1) {
3941 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3942 map->p[i] = map->p[map->n-1];
3950 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3952 return (struct isl_set *)
3953 isl_map_remove_empty_parts((struct isl_map *)set);
3956 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
3958 struct isl_basic_map *bmap;
3959 if (!map || map->n == 0)
3961 bmap = map->p[map->n-1];
3962 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
3963 return isl_basic_map_copy(bmap);
3966 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3968 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
3971 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
3972 struct isl_basic_map *bmap)
3978 for (i = map->n-1; i >= 0; --i) {
3979 if (map->p[i] != bmap)
3981 map = isl_map_cow(map);
3984 isl_basic_map_free(map->p[i]);
3985 if (i != map->n-1) {
3986 ISL_F_CLR(map, ISL_SET_NORMALIZED);
3987 map->p[i] = map->p[map->n-1];
3992 isl_basic_map_free(bmap);
3996 isl_basic_map_free(bmap);
4000 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4001 struct isl_basic_set *bset)
4003 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
4004 (struct isl_basic_map *)bset);
4007 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4008 * for any common value of the parameters and dimensions preceding dim
4009 * in both basic sets, the values of dimension pos in bset1 are
4010 * smaller or larger than those in bset2.
4013 * 1 if bset1 follows bset2
4014 * -1 if bset1 precedes bset2
4015 * 0 if bset1 and bset2 are incomparable
4016 * -2 if some error occurred.
4018 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4019 struct isl_basic_set *bset2, int pos)
4021 struct isl_dim *dims;
4022 struct isl_basic_map *bmap1 = NULL;
4023 struct isl_basic_map *bmap2 = NULL;
4024 struct isl_ctx *ctx;
4025 struct isl_vec *obj;
4028 unsigned dim1, dim2;
4030 enum isl_lp_result res;
4033 if (!bset1 || !bset2)
4036 nparam = isl_basic_set_n_param(bset1);
4037 dim1 = isl_basic_set_n_dim(bset1);
4038 dim2 = isl_basic_set_n_dim(bset2);
4039 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
4040 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
4041 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
4042 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
4043 if (!bmap1 || !bmap2)
4045 bmap1 = isl_basic_map_cow(bmap1);
4046 bmap1 = isl_basic_map_extend(bmap1, nparam,
4047 pos, (dim1 - pos) + (dim2 - pos),
4048 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4049 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
4052 total = isl_basic_map_total_dim(bmap1);
4054 obj = isl_vec_alloc(ctx, 1 + total);
4055 isl_seq_clr(obj->block.data, 1 + total);
4056 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
4057 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
4062 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
4063 if (res == isl_lp_empty)
4065 else if (res == isl_lp_ok && isl_int_is_pos(num))
4067 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4068 res == isl_lp_unbounded)
4074 isl_basic_map_free(bmap1);
4078 isl_basic_map_free(bmap1);
4079 isl_basic_map_free(bmap2);
4083 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4084 unsigned pos, isl_int *val)
4092 total = isl_basic_map_total_dim(bmap);
4093 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4094 for (; d+1 > pos; --d)
4095 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4099 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4101 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4103 if (!isl_int_is_one(bmap->eq[i][1+d]))
4106 isl_int_neg(*val, bmap->eq[i][0]);
4112 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4113 unsigned pos, isl_int *val)
4125 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4128 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4129 for (i = 1; fixed == 1 && i < map->n; ++i) {
4130 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4131 if (fixed == 1 && isl_int_ne(tmp, v))
4135 isl_int_set(*val, v);
4141 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
4142 unsigned pos, isl_int *val)
4144 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
4148 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4151 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4154 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4155 enum isl_dim_type type, unsigned pos, isl_int *val)
4157 if (pos >= isl_basic_map_dim(bmap, type))
4159 return isl_basic_map_fast_has_fixed_var(bmap,
4160 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4163 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4164 * then return this fixed value in *val.
4166 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
4169 return isl_basic_set_fast_has_fixed_var(bset,
4170 isl_basic_set_n_param(bset) + dim, val);
4173 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4174 * then return this fixed value in *val.
4176 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4178 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4181 /* Check if input variable in has fixed value and if so and if val is not NULL,
4182 * then return this fixed value in *val.
4184 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4186 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4189 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4190 * and if val is not NULL, then return this lower bound in *val.
4192 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4193 unsigned dim, isl_int *val)
4195 int i, i_eq = -1, i_ineq = -1;
4202 total = isl_basic_set_total_dim(bset);
4203 nparam = isl_basic_set_n_param(bset);
4204 for (i = 0; i < bset->n_eq; ++i) {
4205 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4211 for (i = 0; i < bset->n_ineq; ++i) {
4212 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4214 if (i_eq != -1 || i_ineq != -1)
4218 if (i_eq == -1 && i_ineq == -1)
4220 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4221 /* The coefficient should always be one due to normalization. */
4222 if (!isl_int_is_one(c[1+nparam+dim]))
4224 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4226 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4227 total - nparam - dim - 1) != -1)
4230 isl_int_neg(*val, c[0]);
4234 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4235 unsigned dim, isl_int *val)
4247 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4251 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4253 for (i = 1; fixed == 1 && i < set->n; ++i) {
4254 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4256 if (fixed == 1 && isl_int_ne(tmp, v))
4260 isl_int_set(*val, v);
4271 static int qsort_constraint_cmp(const void *p1, const void *p2)
4273 const struct constraint *c1 = (const struct constraint *)p1;
4274 const struct constraint *c2 = (const struct constraint *)p2;
4275 unsigned size = isl_min(c1->size, c2->size);
4276 return isl_seq_cmp(c1->c, c2->c, size);
4279 static struct isl_basic_map *isl_basic_map_sort_constraints(
4280 struct isl_basic_map *bmap)
4283 struct constraint *c;
4288 total = isl_basic_map_total_dim(bmap);
4289 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4292 for (i = 0; i < bmap->n_ineq; ++i) {
4294 c[i].c = bmap->ineq[i];
4296 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4297 for (i = 0; i < bmap->n_ineq; ++i)
4298 bmap->ineq[i] = c[i].c;
4302 isl_basic_map_free(bmap);
4306 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4310 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4312 bmap = isl_basic_map_convex_hull(bmap);
4313 bmap = isl_basic_map_sort_constraints(bmap);
4314 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4318 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4320 return (struct isl_basic_set *)isl_basic_map_normalize(
4321 (struct isl_basic_map *)bset);
4324 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4325 const struct isl_basic_map *bmap2)
4332 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4333 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4334 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4335 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4336 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4337 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4338 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4339 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4341 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4343 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4345 if (bmap1->n_eq != bmap2->n_eq)
4346 return bmap1->n_eq - bmap2->n_eq;
4347 if (bmap1->n_ineq != bmap2->n_ineq)
4348 return bmap1->n_ineq - bmap2->n_ineq;
4349 if (bmap1->n_div != bmap2->n_div)
4350 return bmap1->n_div - bmap2->n_div;
4351 total = isl_basic_map_total_dim(bmap1);
4352 for (i = 0; i < bmap1->n_eq; ++i) {
4353 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4357 for (i = 0; i < bmap1->n_ineq; ++i) {
4358 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4362 for (i = 0; i < bmap1->n_div; ++i) {
4363 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4370 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4371 struct isl_basic_map *bmap2)
4373 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4376 static int qsort_bmap_cmp(const void *p1, const void *p2)
4378 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4379 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4381 return isl_basic_map_fast_cmp(bmap1, bmap2);
4384 /* We normalize in place, but if anything goes wrong we need
4385 * to return NULL, so we need to make sure we don't change the
4386 * meaning of any possible other copies of map.
4388 struct isl_map *isl_map_normalize(struct isl_map *map)
4391 struct isl_basic_map *bmap;
4395 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4397 for (i = 0; i < map->n; ++i) {
4398 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4401 isl_basic_map_free(map->p[i]);
4404 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4405 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4406 map = isl_map_remove_empty_parts(map);
4409 for (i = map->n - 1; i >= 1; --i) {
4410 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4412 isl_basic_map_free(map->p[i-1]);
4413 for (j = i; j < map->n; ++j)
4414 map->p[j-1] = map->p[j];
4424 struct isl_set *isl_set_normalize(struct isl_set *set)
4426 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4429 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4439 if (!isl_dim_equal(map1->dim, map2->dim))
4442 map1 = isl_map_copy(map1);
4443 map2 = isl_map_copy(map2);
4444 map1 = isl_map_normalize(map1);
4445 map2 = isl_map_normalize(map2);
4448 equal = map1->n == map2->n;
4449 for (i = 0; equal && i < map1->n; ++i) {
4450 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4463 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4465 return isl_map_fast_is_equal((struct isl_map *)set1,
4466 (struct isl_map *)set2);
4469 /* Return an interval that ranges from min to max (inclusive)
4471 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4472 isl_int min, isl_int max)
4475 struct isl_basic_set *bset = NULL;
4477 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4481 k = isl_basic_set_alloc_inequality(bset);
4484 isl_int_set_si(bset->ineq[k][1], 1);
4485 isl_int_neg(bset->ineq[k][0], min);
4487 k = isl_basic_set_alloc_inequality(bset);
4490 isl_int_set_si(bset->ineq[k][1], -1);
4491 isl_int_set(bset->ineq[k][0], max);
4495 isl_basic_set_free(bset);
4499 /* Return the Cartesian product of the basic sets in list (in the given order).
4501 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4509 struct isl_basic_set *product = NULL;
4513 isl_assert(list->ctx, list->n > 0, goto error);
4514 isl_assert(list->ctx, list->p[0], goto error);
4515 nparam = isl_basic_set_n_param(list->p[0]);
4516 dim = isl_basic_set_n_dim(list->p[0]);
4517 extra = list->p[0]->n_div;
4518 n_eq = list->p[0]->n_eq;
4519 n_ineq = list->p[0]->n_ineq;
4520 for (i = 1; i < list->n; ++i) {
4521 isl_assert(list->ctx, list->p[i], goto error);
4522 isl_assert(list->ctx,
4523 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4524 dim += isl_basic_set_n_dim(list->p[i]);
4525 extra += list->p[i]->n_div;
4526 n_eq += list->p[i]->n_eq;
4527 n_ineq += list->p[i]->n_ineq;
4529 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4534 for (i = 0; i < list->n; ++i) {
4535 isl_basic_set_add_constraints(product,
4536 isl_basic_set_copy(list->p[i]), dim);
4537 dim += isl_basic_set_n_dim(list->p[i]);
4539 isl_basic_set_list_free(list);
4542 isl_basic_set_free(product);
4543 isl_basic_set_list_free(list);
4547 struct isl_basic_map *isl_basic_map_product(
4548 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4550 struct isl_dim *dim_result = NULL;
4551 struct isl_basic_map *bmap;
4552 unsigned in1, in2, out1, out2, nparam, total, pos;
4553 struct isl_dim_map *dim_map1, *dim_map2;
4555 if (!bmap1 || !bmap2)
4558 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4559 bmap2->dim, isl_dim_param), goto error);
4560 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4561 isl_dim_copy(bmap2->dim));
4563 in1 = isl_basic_map_n_in(bmap1);
4564 in2 = isl_basic_map_n_in(bmap2);
4565 out1 = isl_basic_map_n_out(bmap1);
4566 out2 = isl_basic_map_n_out(bmap2);
4567 nparam = isl_basic_map_n_param(bmap1);
4569 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4570 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4571 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4572 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4573 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4574 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4575 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4576 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4577 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4578 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4579 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4581 bmap = isl_basic_map_alloc_dim(dim_result,
4582 bmap1->n_div + bmap2->n_div,
4583 bmap1->n_eq + bmap2->n_eq,
4584 bmap1->n_ineq + bmap2->n_ineq);
4585 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4586 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4587 bmap = isl_basic_map_simplify(bmap);
4588 return isl_basic_map_finalize(bmap);
4590 isl_basic_map_free(bmap1);
4591 isl_basic_map_free(bmap2);
4595 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4597 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4600 struct isl_map *result;
4606 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4607 map2->dim, isl_dim_param), goto error);
4609 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4610 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4611 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4613 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4614 isl_dim_copy(map2->dim)),
4615 map1->n * map2->n, flags);
4618 for (i = 0; i < map1->n; ++i)
4619 for (j = 0; j < map2->n; ++j) {
4620 struct isl_basic_map *part;
4621 part = isl_basic_map_product(
4622 isl_basic_map_copy(map1->p[i]),
4623 isl_basic_map_copy(map2->p[j]));
4624 if (isl_basic_map_is_empty(part))
4625 isl_basic_map_free(part);
4627 result = isl_map_add(result, part);
4640 /* Given two set A and B, construct its Cartesian product A x B.
4642 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
4644 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
4645 (struct isl_map *)set2);
4648 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4656 bset = isl_basic_set_copy(bset);
4657 bset = isl_basic_set_normalize(bset);
4660 total = isl_basic_set_total_dim(bset);
4661 isl_hash_byte(hash, bset->n_eq & 0xFF);
4662 for (i = 0; i < bset->n_eq; ++i) {
4664 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4665 isl_hash_hash(hash, c_hash);
4667 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4668 for (i = 0; i < bset->n_ineq; ++i) {
4670 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4671 isl_hash_hash(hash, c_hash);
4673 isl_hash_byte(hash, bset->n_div & 0xFF);
4674 for (i = 0; i < bset->n_div; ++i) {
4676 if (isl_int_is_zero(bset->div[i][0]))
4678 isl_hash_byte(hash, i & 0xFF);
4679 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4680 isl_hash_hash(hash, c_hash);
4682 isl_basic_set_free(bset);
4686 uint32_t isl_set_get_hash(struct isl_set *set)
4693 set = isl_set_copy(set);
4694 set = isl_set_normalize(set);
4698 hash = isl_hash_init();
4699 for (i = 0; i < set->n; ++i) {
4701 bset_hash = isl_basic_set_get_hash(set->p[i]);
4702 isl_hash_hash(hash, bset_hash);
4710 /* Check if the value for dimension dim is completely determined
4711 * by the values of the other parameters and variables.
4712 * That is, check if dimension dim is involved in an equality.
4714 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4721 nparam = isl_basic_set_n_param(bset);
4722 for (i = 0; i < bset->n_eq; ++i)
4723 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4728 /* Check if the value for dimension dim is completely determined
4729 * by the values of the other parameters and variables.
4730 * That is, check if dimension dim is involved in an equality
4731 * for each of the subsets.
4733 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4739 for (i = 0; i < set->n; ++i) {
4741 unique = isl_basic_set_dim_is_unique(set->p[i], dim);