11 #include "isl_map_private.h"
12 #include "isl_map_piplib.h"
13 #include "isl_sample.h"
16 /* Maps dst positions to src positions */
22 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
25 struct isl_dim_map *dim_map;
26 dim_map = isl_alloc(ctx, struct isl_dim_map,
27 sizeof(struct isl_dim_map) + len * sizeof(int));
30 dim_map->len = 1 + len;
32 for (i = 0; i < len; ++i)
33 dim_map->pos[1 + i] = -1;
37 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
40 case isl_dim_param: return dim->nparam;
41 case isl_dim_in: return dim->n_in;
42 case isl_dim_out: return dim->n_out;
46 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
49 case isl_dim_param: return 1;
50 case isl_dim_in: return 1 + dim->nparam;
51 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
55 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
56 enum isl_dim_type type, unsigned dst_pos)
64 src_pos = pos(dim, type);
65 for (i = 0; i < n(dim, type); ++i)
66 dim_map->pos[1 + dst_pos + i] = src_pos + i;
69 static void isl_dim_map_div(struct isl_dim_map *dim_map,
70 struct isl_basic_map *bmap, unsigned dst_pos)
75 if (!dim_map || !bmap)
78 src_pos = 1 + isl_dim_total(bmap->dim);
79 for (i = 0; i < bmap->n_div; ++i)
80 dim_map->pos[1 + dst_pos + i] = src_pos + i;
83 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
87 for (i = 0; i < dim_map->len; ++i)
88 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
89 fprintf(stderr, "\n");
92 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
93 enum isl_dim_type type)
95 struct isl_dim *dim = bmap->dim;
99 case isl_dim_out: return isl_dim_size(bmap->dim, type);
100 case isl_dim_div: return bmap->n_div;
101 case isl_dim_all: return isl_basic_map_total_dim(bmap);
105 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
107 return n(map->dim, type);
110 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
112 return n(set->dim, type);
115 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
116 enum isl_dim_type type)
118 struct isl_dim *dim = bmap->dim;
120 case isl_dim_param: return 1;
121 case isl_dim_in: return 1 + dim->nparam;
122 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
123 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
127 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
129 return pos(map->dim, type);
132 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
133 enum isl_dim_type type)
135 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
138 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
140 return bset->dim->n_out;
143 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
145 return bset->dim->nparam;
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
150 return isl_dim_total(bset->dim) + bset->n_div;
153 unsigned isl_set_n_dim(const struct isl_set *set)
155 return set->dim->n_out;
158 unsigned isl_set_n_param(const struct isl_set *set)
160 return set->dim->nparam;
163 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
165 return bmap->dim->n_in;
168 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
170 return bmap->dim->n_out;
173 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
175 return bmap->dim->nparam;
178 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
183 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
185 return isl_dim_total(bmap->dim) + bmap->n_div;
188 unsigned isl_map_n_in(const struct isl_map *map)
190 return map->dim->n_in;
193 unsigned isl_map_n_out(const struct isl_map *map)
195 return map->dim->n_out;
198 unsigned isl_map_n_param(const struct isl_map *map)
200 return map->dim->nparam;
203 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
205 return map->dim->n_in == set->dim->n_out &&
206 map->dim->nparam == set->dim->nparam;
209 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
210 struct isl_basic_set *bset)
212 return bmap->dim->n_in == bset->dim->n_out &&
213 bmap->dim->nparam == bset->dim->nparam;
216 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
217 struct isl_basic_set *bset)
219 return bmap->dim->n_out == bset->dim->n_out &&
220 bmap->dim->nparam == bset->dim->nparam;
223 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
224 struct isl_basic_map *bmap, unsigned extra,
225 unsigned n_eq, unsigned n_ineq)
228 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
230 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
231 if (isl_blk_is_error(bmap->block)) {
236 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
238 isl_blk_free(ctx, bmap->block);
244 bmap->block2 = isl_blk_empty();
247 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
248 if (isl_blk_is_error(bmap->block2)) {
250 isl_blk_free(ctx, bmap->block);
255 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
257 isl_blk_free(ctx, bmap->block2);
259 isl_blk_free(ctx, bmap->block);
265 for (i = 0; i < n_ineq + n_eq; ++i)
266 bmap->ineq[i] = bmap->block.data + i * row_size;
268 for (i = 0; i < extra; ++i)
269 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
275 bmap->c_size = n_eq + n_ineq;
276 bmap->eq = bmap->ineq + n_ineq;
285 isl_basic_map_free(bmap);
289 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
290 unsigned nparam, unsigned dim, unsigned extra,
291 unsigned n_eq, unsigned n_ineq)
293 struct isl_basic_map *bmap;
294 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
295 return (struct isl_basic_set *)bmap;
298 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
299 unsigned extra, unsigned n_eq, unsigned n_ineq)
301 struct isl_basic_map *bmap;
304 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
305 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
306 return (struct isl_basic_set *)bmap;
309 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
310 unsigned extra, unsigned n_eq, unsigned n_ineq)
312 struct isl_basic_map *bmap;
316 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
321 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
327 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
328 unsigned nparam, unsigned in, unsigned out, unsigned extra,
329 unsigned n_eq, unsigned n_ineq)
331 struct isl_basic_map *bmap;
334 dim = isl_dim_alloc(ctx, nparam, in, out);
338 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
342 static void dup_constraints(
343 struct isl_basic_map *dst, struct isl_basic_map *src)
346 unsigned total = isl_basic_map_total_dim(src);
348 for (i = 0; i < src->n_eq; ++i) {
349 int j = isl_basic_map_alloc_equality(dst);
350 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
353 for (i = 0; i < src->n_ineq; ++i) {
354 int j = isl_basic_map_alloc_inequality(dst);
355 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
358 for (i = 0; i < src->n_div; ++i) {
359 int j = isl_basic_map_alloc_div(dst);
360 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
362 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
365 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
367 struct isl_basic_map *dup;
371 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
372 bmap->n_div, bmap->n_eq, bmap->n_ineq);
375 dup_constraints(dup, bmap);
376 dup->flags = bmap->flags;
377 dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
381 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
383 struct isl_basic_map *dup;
385 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
386 return (struct isl_basic_set *)dup;
389 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
394 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
398 return isl_basic_set_dup(bset);
401 struct isl_set *isl_set_copy(struct isl_set *set)
410 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
415 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
419 return isl_basic_map_dup(bmap);
422 struct isl_map *isl_map_copy(struct isl_map *map)
431 void isl_basic_map_free(struct isl_basic_map *bmap)
439 isl_ctx_deref(bmap->ctx);
441 isl_blk_free(bmap->ctx, bmap->block2);
443 isl_blk_free(bmap->ctx, bmap->block);
444 isl_vec_free(bmap->ctx, bmap->sample);
445 isl_dim_free(bmap->dim);
449 void isl_basic_set_free(struct isl_basic_set *bset)
451 isl_basic_map_free((struct isl_basic_map *)bset);
454 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
456 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
459 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
465 isl_assert(ctx, room_for_con(bmap, 1), return -1);
466 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
468 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
469 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
470 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
471 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
472 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
473 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
475 int j = isl_basic_map_alloc_inequality(bmap);
479 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
480 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
487 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
488 bmap->extra - bmap->n_div);
492 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
494 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
497 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
501 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
506 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
511 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
513 if (pos != bmap->n_eq - 1) {
515 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
516 bmap->eq[bmap->n_eq - 1] = t;
522 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
524 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
527 void isl_basic_map_inequality_to_equality(
528 struct isl_basic_map *bmap, unsigned pos)
533 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
534 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
539 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
540 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
541 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
542 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
545 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
547 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
550 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
556 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
557 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
558 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
559 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
560 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
561 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
562 1 + isl_basic_map_total_dim(bmap),
563 bmap->extra - bmap->n_div);
564 return bmap->n_ineq++;
567 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
569 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
572 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
576 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
581 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
583 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
586 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
591 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
593 if (pos != bmap->n_ineq - 1) {
595 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
596 bmap->ineq[bmap->n_ineq - 1] = t;
597 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
603 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
605 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
608 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
612 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
613 isl_seq_clr(bmap->div[bmap->n_div] +
614 1 + 1 + isl_basic_map_total_dim(bmap),
615 bmap->extra - bmap->n_div);
616 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
617 return bmap->n_div++;
620 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
624 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
629 /* Copy constraint from src to dst, putting the vars of src at offset
630 * dim_off in dst and the divs of src at offset div_off in dst.
631 * If both sets are actually map, then dim_off applies to the input
634 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
635 struct isl_basic_map *src_map, isl_int *src,
636 unsigned in_off, unsigned out_off, unsigned div_off)
638 unsigned src_nparam = isl_basic_map_n_param(src_map);
639 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
640 unsigned src_in = isl_basic_map_n_in(src_map);
641 unsigned dst_in = isl_basic_map_n_in(dst_map);
642 unsigned src_out = isl_basic_map_n_out(src_map);
643 unsigned dst_out = isl_basic_map_n_out(dst_map);
644 isl_int_set(dst[0], src[0]);
645 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
646 if (dst_nparam > src_nparam)
647 isl_seq_clr(dst+1+src_nparam,
648 dst_nparam - src_nparam);
649 isl_seq_clr(dst+1+dst_nparam, in_off);
650 isl_seq_cpy(dst+1+dst_nparam+in_off,
652 isl_min(dst_in-in_off, src_in));
653 if (dst_in-in_off > src_in)
654 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
655 dst_in - in_off - src_in);
656 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
657 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
658 src+1+src_nparam+src_in,
659 isl_min(dst_out-out_off, src_out));
660 if (dst_out-out_off > src_out)
661 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
662 dst_out - out_off - src_out);
663 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
664 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
665 src+1+src_nparam+src_in+src_out,
666 isl_min(dst_map->extra-div_off, src_map->n_div));
667 if (dst_map->n_div-div_off > src_map->n_div)
668 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
669 div_off+src_map->n_div,
670 dst_map->n_div - div_off - src_map->n_div);
673 static void copy_div(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 isl_int_set(dst[0], src[0]);
678 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
681 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
682 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
687 if (!bmap1 || !bmap2)
690 div_off = bmap1->n_div;
692 for (i = 0; i < bmap2->n_eq; ++i) {
693 int i1 = isl_basic_map_alloc_equality(bmap1);
696 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
697 i_pos, o_pos, div_off);
700 for (i = 0; i < bmap2->n_ineq; ++i) {
701 int i1 = isl_basic_map_alloc_inequality(bmap1);
704 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
705 i_pos, o_pos, div_off);
708 for (i = 0; i < bmap2->n_div; ++i) {
709 int i1 = isl_basic_map_alloc_div(bmap1);
712 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
713 i_pos, o_pos, div_off);
716 isl_basic_map_free(bmap2);
721 isl_basic_map_free(bmap1);
722 isl_basic_map_free(bmap2);
726 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
727 struct isl_dim_map *dim_map)
731 for (i = 0; i < dim_map->len; ++i) {
732 if (dim_map->pos[i] < 0)
733 isl_int_set_si(dst[i], 0);
735 isl_int_set(dst[i], src[dim_map->pos[i]]);
739 static void copy_div_dim_map(isl_int *dst, isl_int *src,
740 struct isl_dim_map *dim_map)
742 isl_int_set(dst[0], src[0]);
743 copy_constraint_dim_map(dst+1, src+1, dim_map);
746 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
747 struct isl_basic_map *src, struct isl_dim_map *dim_map)
751 if (!src || !dst || !dim_map)
754 for (i = 0; i < src->n_eq; ++i) {
755 int i1 = isl_basic_map_alloc_equality(dst);
758 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
761 for (i = 0; i < src->n_ineq; ++i) {
762 int i1 = isl_basic_map_alloc_inequality(dst);
765 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
768 for (i = 0; i < src->n_div; ++i) {
769 int i1 = isl_basic_map_alloc_div(dst);
772 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
776 isl_basic_map_free(src);
781 isl_basic_map_free(src);
782 isl_basic_map_free(dst);
786 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
787 struct isl_basic_set *bset2, unsigned pos)
789 return (struct isl_basic_set *)
790 add_constraints((struct isl_basic_map *)bset1,
791 (struct isl_basic_map *)bset2, 0, pos);
794 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
795 struct isl_dim *dim, unsigned extra,
796 unsigned n_eq, unsigned n_ineq)
798 struct isl_basic_map *ext;
808 dims_ok = isl_dim_equal(base->dim, dim) &&
809 base->extra >= base->n_div + extra;
811 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
812 room_for_ineq(base, n_ineq)) {
817 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
818 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
819 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
820 extra += base->extra;
822 n_ineq += base->n_ineq;
824 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
830 ext = add_constraints(ext, base, 0, 0);
833 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
840 isl_basic_map_free(base);
844 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
845 struct isl_dim *dim, unsigned extra,
846 unsigned n_eq, unsigned n_ineq)
848 return (struct isl_basic_set *)
849 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
850 extra, n_eq, n_ineq);
853 struct isl_basic_map *isl_basic_map_extend_constraints(
854 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
858 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
862 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
863 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
864 unsigned n_eq, unsigned n_ineq)
866 struct isl_basic_map *bmap;
871 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
875 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
879 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
880 unsigned nparam, unsigned dim, unsigned extra,
881 unsigned n_eq, unsigned n_ineq)
883 return (struct isl_basic_set *)
884 isl_basic_map_extend((struct isl_basic_map *)base,
885 nparam, 0, dim, extra, n_eq, n_ineq);
888 struct isl_basic_set *isl_basic_set_extend_constraints(
889 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
891 return (struct isl_basic_set *)
892 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
896 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
898 return (struct isl_basic_set *)
899 isl_basic_map_cow((struct isl_basic_map *)bset);
902 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
909 bmap = isl_basic_map_dup(bmap);
911 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
915 struct isl_set *isl_set_cow(struct isl_set *set)
923 return isl_set_dup(set);
926 struct isl_map *isl_map_cow(struct isl_map *map)
934 return isl_map_dup(map);
937 static void swap_vars(struct isl_blk blk, isl_int *a,
938 unsigned a_len, unsigned b_len)
940 isl_seq_cpy(blk.data, a+a_len, b_len);
941 isl_seq_cpy(blk.data+b_len, a, a_len);
942 isl_seq_cpy(a, blk.data, b_len+a_len);
945 struct isl_basic_set *isl_basic_set_swap_vars(
946 struct isl_basic_set *bset, unsigned n)
956 nparam = isl_basic_set_n_param(bset);
957 dim = isl_basic_set_n_dim(bset);
958 isl_assert(bset->ctx, n <= dim, goto error);
963 bset = isl_basic_set_cow(bset);
967 blk = isl_blk_alloc(bset->ctx, dim);
968 if (isl_blk_is_error(blk))
971 for (i = 0; i < bset->n_eq; ++i)
973 bset->eq[i]+1+nparam, n, dim - n);
975 for (i = 0; i < bset->n_ineq; ++i)
977 bset->ineq[i]+1+nparam, n, dim - n);
979 for (i = 0; i < bset->n_div; ++i)
981 bset->div[i]+1+1+nparam, n, dim - n);
983 isl_blk_free(bset->ctx, blk);
985 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
989 isl_basic_set_free(bset);
993 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
996 set = isl_set_cow(set);
1000 for (i = 0; i < set->n; ++i) {
1001 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1007 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1011 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1017 total = isl_basic_map_total_dim(bmap);
1018 isl_basic_map_free_div(bmap, bmap->n_div);
1019 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1021 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1023 isl_basic_map_alloc_equality(bmap);
1027 isl_int_set_si(bmap->eq[i][0], 1);
1028 isl_seq_clr(bmap->eq[i]+1, total);
1029 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1030 return isl_basic_map_finalize(bmap);
1032 isl_basic_map_free(bmap);
1036 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1038 return (struct isl_basic_set *)
1039 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1042 static void swap_div(struct isl_basic_map *bmap, int a, int b)
1045 unsigned off = isl_dim_total(bmap->dim);
1046 isl_int *t = bmap->div[a];
1047 bmap->div[a] = bmap->div[b];
1050 for (i = 0; i < bmap->n_eq; ++i)
1051 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1053 for (i = 0; i < bmap->n_ineq; ++i)
1054 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1056 for (i = 0; i < bmap->n_div; ++i)
1057 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1058 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1061 /* Eliminate the specified n dimensions starting at first from the
1062 * constraints using Fourier-Motzkin, The dimensions themselves
1065 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1066 unsigned first, unsigned n)
1076 set = isl_set_cow(set);
1079 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1080 nparam = isl_set_n_param(set);
1082 for (i = 0; i < set->n; ++i) {
1083 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1094 /* Project out n dimensions starting at first using Fourier-Motzkin */
1095 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1096 unsigned first, unsigned n)
1098 set = isl_set_eliminate_dims(set, first, n);
1099 set = isl_set_drop_dims(set, first, n);
1103 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1105 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1113 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1122 set = isl_set_cow(set);
1126 for (i = 0; i < set->n; ++i) {
1127 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1137 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1138 enum isl_dim_type type, unsigned first, unsigned n)
1142 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1146 bmap = isl_basic_map_eliminate_vars(bmap,
1147 isl_basic_map_offset(bmap, type) - 1 + first, n);
1148 bmap = isl_basic_map_drop(bmap, type, first, n);
1151 isl_basic_map_free(bmap);
1155 struct isl_map *isl_map_remove(struct isl_map *map,
1156 enum isl_dim_type type, unsigned first, unsigned n)
1164 map = isl_map_cow(map);
1167 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1169 for (i = 0; i < map->n; ++i) {
1170 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1171 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1175 map = isl_map_drop(map, type, first, n);
1182 /* Project out n inputs starting at first using Fourier-Motzkin */
1183 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1184 unsigned first, unsigned n)
1186 return isl_map_remove(map, isl_dim_in, first, n);
1189 /* Project out n dimensions starting at first using Fourier-Motzkin */
1190 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1191 unsigned first, unsigned n)
1193 unsigned nparam = isl_basic_set_n_param(bset);
1194 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1195 bset = isl_basic_set_drop_dims(bset, first, n);
1199 static void dump_term(struct isl_basic_map *bmap,
1200 isl_int c, int pos, FILE *out)
1203 unsigned in = isl_basic_map_n_in(bmap);
1204 unsigned dim = in + isl_basic_map_n_out(bmap);
1205 unsigned nparam = isl_basic_map_n_param(bmap);
1207 isl_int_print(out, c, 0);
1209 if (!isl_int_is_one(c))
1210 isl_int_print(out, c, 0);
1211 if (pos < 1 + nparam) {
1212 name = isl_dim_get_name(bmap->dim,
1213 isl_dim_param, pos - 1);
1215 fprintf(out, "%s", name);
1217 fprintf(out, "p%d", pos - 1);
1218 } else if (pos < 1 + nparam + in)
1219 fprintf(out, "i%d", pos - 1 - nparam);
1220 else if (pos < 1 + nparam + dim)
1221 fprintf(out, "o%d", pos - 1 - nparam - in);
1223 fprintf(out, "e%d", pos - 1 - nparam - dim);
1227 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1228 int sign, FILE *out)
1232 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1236 for (i = 0, first = 1; i < len; ++i) {
1237 if (isl_int_sgn(c[i]) * sign <= 0)
1240 fprintf(out, " + ");
1242 isl_int_abs(v, c[i]);
1243 dump_term(bmap, v, i, out);
1250 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1251 const char *op, FILE *out, int indent)
1255 fprintf(out, "%*s", indent, "");
1257 dump_constraint_sign(bmap, c, 1, out);
1258 fprintf(out, " %s ", op);
1259 dump_constraint_sign(bmap, c, -1, out);
1263 for (i = bmap->n_div; i < bmap->extra; ++i) {
1264 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1266 fprintf(out, "%*s", indent, "");
1267 fprintf(out, "ERROR: unused div coefficient not zero\n");
1272 static void dump_constraints(struct isl_basic_map *bmap,
1273 isl_int **c, unsigned n,
1274 const char *op, FILE *out, int indent)
1278 for (i = 0; i < n; ++i)
1279 dump_constraint(bmap, c[i], op, out, indent);
1282 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1286 unsigned total = isl_basic_map_total_dim(bmap);
1288 for (j = 0; j < 1 + total; ++j) {
1289 if (isl_int_is_zero(exp[j]))
1291 if (!first && isl_int_is_pos(exp[j]))
1293 dump_term(bmap, exp[j], j, out);
1298 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1302 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1303 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1305 for (i = 0; i < bmap->n_div; ++i) {
1306 fprintf(out, "%*s", indent, "");
1307 fprintf(out, "e%d = [(", i);
1308 dump_affine(bmap, bmap->div[i]+1, out);
1310 isl_int_print(out, bmap->div[i][0], 0);
1311 fprintf(out, "]\n");
1315 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1318 fprintf(out, "null basic set\n");
1322 fprintf(out, "%*s", indent, "");
1323 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1324 bset->ref, bset->dim->nparam, bset->dim->n_out,
1325 bset->extra, bset->flags);
1326 dump((struct isl_basic_map *)bset, out, indent);
1329 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1332 fprintf(out, "null basic map\n");
1336 fprintf(out, "%*s", indent, "");
1337 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1338 "flags: %x, n_name: %d\n",
1340 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1341 bmap->extra, bmap->flags, bmap->dim->n_name);
1342 dump(bmap, out, indent);
1345 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1350 total = isl_basic_map_total_dim(bmap);
1351 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1352 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1353 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1358 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1360 struct isl_set *set;
1364 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1365 isl_assert(dim->ctx, n >= 0, return NULL);
1366 set = isl_alloc(dim->ctx, struct isl_set,
1367 sizeof(struct isl_set) +
1368 n * sizeof(struct isl_basic_set *));
1372 set->ctx = dim->ctx;
1373 isl_ctx_ref(set->ctx);
1385 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1386 unsigned nparam, unsigned dim, int n, unsigned flags)
1388 struct isl_set *set;
1389 struct isl_dim *dims;
1391 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1395 set = isl_set_alloc_dim(dims, n, flags);
1399 struct isl_set *isl_set_dup(struct isl_set *set)
1402 struct isl_set *dup;
1407 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1410 for (i = 0; i < set->n; ++i)
1411 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1415 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1417 struct isl_set *set;
1422 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1424 isl_basic_set_free(bset);
1427 return isl_set_add(set, bset);
1430 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1432 struct isl_map *map;
1437 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1439 isl_basic_map_free(bmap);
1442 return isl_map_add(map, bmap);
1445 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1449 isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1450 isl_assert(set->ctx, set->n < set->size, goto error);
1451 set->p[set->n] = bset;
1458 isl_basic_set_free(bset);
1462 void isl_set_free(struct isl_set *set)
1472 isl_ctx_deref(set->ctx);
1473 for (i = 0; i < set->n; ++i)
1474 isl_basic_set_free(set->p[i]);
1475 isl_dim_free(set->dim);
1479 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1484 fprintf(out, "null set\n");
1488 fprintf(out, "%*s", indent, "");
1489 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1490 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1492 for (i = 0; i < set->n; ++i) {
1493 fprintf(out, "%*s", indent, "");
1494 fprintf(out, "basic set %d:\n", i);
1495 isl_basic_set_dump(set->p[i], out, indent+4);
1499 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1504 fprintf(out, "null map\n");
1508 fprintf(out, "%*s", indent, "");
1509 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1510 "flags: %x, n_name: %d\n",
1511 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1512 map->dim->n_out, map->flags, map->dim->n_name);
1513 for (i = 0; i < map->n; ++i) {
1514 fprintf(out, "%*s", indent, "");
1515 fprintf(out, "basic map %d:\n", i);
1516 isl_basic_map_dump(map->p[i], out, indent+4);
1520 struct isl_basic_map *isl_basic_map_intersect_domain(
1521 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1523 struct isl_basic_map *bmap_domain;
1524 struct isl_dim *dim;
1529 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1530 bset->dim, isl_dim_param), goto error);
1532 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1533 isl_assert(bset->ctx,
1534 isl_basic_map_compatible_domain(bmap, bset), goto error);
1536 bmap = isl_basic_map_cow(bmap);
1537 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1538 bset->n_div, bset->n_eq, bset->n_ineq);
1541 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1542 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1543 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1545 bmap = isl_basic_map_simplify(bmap);
1546 return isl_basic_map_finalize(bmap);
1548 isl_basic_map_free(bmap);
1549 isl_basic_set_free(bset);
1553 struct isl_basic_map *isl_basic_map_intersect_range(
1554 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1556 struct isl_basic_map *bmap_range;
1561 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1562 bset->dim, isl_dim_param), goto error);
1564 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1565 isl_assert(bset->ctx,
1566 isl_basic_map_compatible_range(bmap, bset), goto error);
1568 bmap = isl_basic_map_cow(bmap);
1569 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1570 bset->n_div, bset->n_eq, bset->n_ineq);
1573 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1574 bmap = add_constraints(bmap, bmap_range, 0, 0);
1576 bmap = isl_basic_map_simplify(bmap);
1577 return isl_basic_map_finalize(bmap);
1579 isl_basic_map_free(bmap);
1580 isl_basic_set_free(bset);
1584 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1590 total = 1 + isl_basic_map_total_dim(bmap);
1591 if (total != vec->size)
1596 for (i = 0; i < bmap->n_eq; ++i) {
1597 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
1598 if (!isl_int_is_zero(s)) {
1604 for (i = 0; i < bmap->n_ineq; ++i) {
1605 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
1606 if (isl_int_is_neg(s)) {
1617 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1619 return basic_map_contains((struct isl_basic_map *)bset, vec);
1622 struct isl_basic_map *isl_basic_map_intersect(
1623 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1625 struct isl_vec *sample = NULL;
1627 if (!bmap1 || !bmap2)
1630 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1631 bmap2->dim, isl_dim_param), goto error);
1632 if (isl_dim_total(bmap1->dim) ==
1633 isl_dim_size(bmap1->dim, isl_dim_param) &&
1634 isl_dim_total(bmap2->dim) !=
1635 isl_dim_size(bmap2->dim, isl_dim_param))
1636 return isl_basic_map_intersect(bmap2, bmap1);
1638 if (isl_dim_total(bmap2->dim) !=
1639 isl_dim_size(bmap2->dim, isl_dim_param))
1640 isl_assert(bmap1->ctx,
1641 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1643 if (bmap1->sample &&
1644 basic_map_contains(bmap1, bmap1->sample) > 0 &&
1645 basic_map_contains(bmap2, bmap1->sample) > 0)
1646 sample = isl_vec_copy(bmap1->ctx, bmap1->sample);
1647 else if (bmap2->sample &&
1648 basic_map_contains(bmap1, bmap2->sample) > 0 &&
1649 basic_map_contains(bmap2, bmap2->sample) > 0)
1650 sample = isl_vec_copy(bmap2->ctx, bmap2->sample);
1652 bmap1 = isl_basic_map_cow(bmap1);
1653 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1654 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1657 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1660 isl_vec_free(bmap1->ctx, bmap1->sample);
1661 bmap1->sample = sample;
1664 bmap1 = isl_basic_map_simplify(bmap1);
1665 return isl_basic_map_finalize(bmap1);
1668 isl_vec_free(bmap1->ctx, sample);
1669 isl_basic_map_free(bmap1);
1670 isl_basic_map_free(bmap2);
1674 struct isl_basic_set *isl_basic_set_intersect(
1675 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1677 return (struct isl_basic_set *)
1678 isl_basic_map_intersect(
1679 (struct isl_basic_map *)bset1,
1680 (struct isl_basic_map *)bset2);
1683 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1686 struct isl_map *result;
1692 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1693 map2->dim, isl_dim_param), goto error);
1694 if (isl_dim_total(map1->dim) ==
1695 isl_dim_size(map1->dim, isl_dim_param) &&
1696 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1697 return isl_map_intersect(map2, map1);
1699 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1700 isl_assert(map1->ctx,
1701 isl_dim_equal(map1->dim, map2->dim), goto error);
1703 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1704 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1705 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1707 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1708 map1->n * map2->n, flags);
1711 for (i = 0; i < map1->n; ++i)
1712 for (j = 0; j < map2->n; ++j) {
1713 struct isl_basic_map *part;
1714 part = isl_basic_map_intersect(
1715 isl_basic_map_copy(map1->p[i]),
1716 isl_basic_map_copy(map2->p[j]));
1717 if (isl_basic_map_is_empty(part))
1718 isl_basic_map_free(part);
1720 result = isl_map_add(result, part);
1733 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1735 return (struct isl_set *)
1736 isl_map_intersect((struct isl_map *)set1,
1737 (struct isl_map *)set2);
1740 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1742 struct isl_dim *dim;
1743 struct isl_basic_set *bset;
1748 bmap = isl_basic_map_cow(bmap);
1751 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1752 in = isl_basic_map_n_in(bmap);
1753 bset = isl_basic_set_from_basic_map(bmap);
1754 bset = isl_basic_set_swap_vars(bset, in);
1755 return isl_basic_map_from_basic_set(bset, dim);
1758 /* Turn final n dimensions into existentially quantified variables.
1760 struct isl_basic_set *isl_basic_set_project_out(
1761 struct isl_basic_set *bset, unsigned n, unsigned flags)
1771 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1776 bset = isl_basic_set_cow(bset);
1778 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1779 old = bset->block2.data;
1780 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1781 (bset->extra + n) * (1 + row_size));
1782 if (!bset->block2.data)
1784 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1787 for (i = 0; i < n; ++i) {
1788 new_div[i] = bset->block2.data +
1789 (bset->extra + i) * (1 + row_size);
1790 isl_seq_clr(new_div[i], 1 + row_size);
1792 for (i = 0; i < bset->extra; ++i)
1793 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1795 bset->div = new_div;
1798 bset->dim = isl_dim_drop_outputs(bset->dim,
1799 isl_basic_set_n_dim(bset) - n, n);
1802 bset = isl_basic_set_simplify(bset);
1803 return isl_basic_set_finalize(bset);
1805 isl_basic_set_free(bset);
1809 struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1813 for (i = 0; i < n; ++i) {
1814 j = isl_basic_map_alloc_div(bmap);
1817 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1821 isl_basic_map_free(bmap);
1825 struct isl_basic_map *isl_basic_map_apply_range(
1826 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1828 struct isl_dim *dim_result = NULL;
1829 struct isl_basic_map *bmap;
1830 unsigned n_in, n_out, n, nparam, total, pos;
1831 struct isl_dim_map *dim_map1, *dim_map2;
1833 if (!bmap1 || !bmap2)
1836 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1837 isl_dim_copy(bmap2->dim));
1839 n_in = isl_basic_map_n_in(bmap1);
1840 n_out = isl_basic_map_n_out(bmap2);
1841 n = isl_basic_map_n_out(bmap1);
1842 nparam = isl_basic_map_n_param(bmap1);
1844 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1845 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1846 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1847 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1848 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1849 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1850 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1851 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1852 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1853 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1854 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1856 bmap = isl_basic_map_alloc_dim(dim_result,
1857 bmap1->n_div + bmap2->n_div + n,
1858 bmap1->n_eq + bmap2->n_eq,
1859 bmap1->n_ineq + bmap2->n_ineq);
1860 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1861 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1862 bmap = add_divs(bmap, n);
1863 bmap = isl_basic_map_simplify(bmap);
1864 return isl_basic_map_finalize(bmap);
1866 isl_basic_map_free(bmap1);
1867 isl_basic_map_free(bmap2);
1871 struct isl_basic_set *isl_basic_set_apply(
1872 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1877 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1880 return (struct isl_basic_set *)
1881 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1883 isl_basic_set_free(bset);
1884 isl_basic_map_free(bmap);
1888 struct isl_basic_map *isl_basic_map_apply_domain(
1889 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1891 if (!bmap1 || !bmap2)
1895 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1897 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1900 bmap1 = isl_basic_map_reverse(bmap1);
1901 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1902 return isl_basic_map_reverse(bmap1);
1904 isl_basic_map_free(bmap1);
1905 isl_basic_map_free(bmap2);
1909 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1910 * A \cap B -> f(A) + f(B)
1912 struct isl_basic_map *isl_basic_map_sum(
1913 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1915 unsigned n_in, n_out, nparam, total, pos;
1916 struct isl_basic_map *bmap = NULL;
1917 struct isl_dim_map *dim_map1, *dim_map2;
1920 if (!bmap1 || !bmap2)
1923 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1926 nparam = isl_basic_map_n_param(bmap1);
1927 n_in = isl_basic_map_n_in(bmap1);
1928 n_out = isl_basic_map_n_out(bmap1);
1930 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
1931 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1932 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
1933 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1934 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
1935 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1936 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1937 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
1938 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1939 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1940 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
1942 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
1943 bmap1->n_div + bmap2->n_div + 2 * n_out,
1944 bmap1->n_eq + bmap2->n_eq + n_out,
1945 bmap1->n_ineq + bmap2->n_ineq);
1946 for (i = 0; i < n_out; ++i) {
1947 int j = isl_basic_map_alloc_equality(bmap);
1950 isl_seq_clr(bmap->eq[j], 1+total);
1951 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
1952 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
1953 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
1955 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1956 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1957 bmap = add_divs(bmap, 2 * n_out);
1959 bmap = isl_basic_map_simplify(bmap);
1960 return isl_basic_map_finalize(bmap);
1962 isl_basic_map_free(bmap);
1963 isl_basic_map_free(bmap1);
1964 isl_basic_map_free(bmap2);
1968 /* Given a basic map A -> f(A), construct A -> -f(A).
1970 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
1975 bmap = isl_basic_map_cow(bmap);
1979 n = isl_basic_map_dim(bmap, isl_dim_out);
1980 off = isl_basic_map_offset(bmap, isl_dim_out);
1981 for (i = 0; i < bmap->n_eq; ++i)
1982 for (j = 0; j < n; ++j)
1983 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
1984 for (i = 0; i < bmap->n_ineq; ++i)
1985 for (j = 0; j < n; ++j)
1986 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
1987 for (i = 0; i < bmap->n_div; ++i)
1988 for (j = 0; j < n; ++j)
1989 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
1990 return isl_basic_map_finalize(bmap);
1993 /* Given a basic map A -> f(A) and an integer d, construct a basic map
1994 * A -> floor(f(A)/d).
1996 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
1999 unsigned n_in, n_out, nparam, total, pos;
2000 struct isl_basic_map *result = NULL;
2001 struct isl_dim_map *dim_map;
2007 nparam = isl_basic_map_n_param(bmap);
2008 n_in = isl_basic_map_n_in(bmap);
2009 n_out = isl_basic_map_n_out(bmap);
2011 total = nparam + n_in + n_out + bmap->n_div + n_out;
2012 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2013 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2014 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2015 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2016 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2018 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2019 bmap->n_div + n_out,
2020 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2021 result = add_constraints_dim_map(result, bmap, dim_map);
2022 result = add_divs(result, n_out);
2023 for (i = 0; i < n_out; ++i) {
2025 j = isl_basic_map_alloc_inequality(result);
2028 isl_seq_clr(result->ineq[j], 1+total);
2029 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2030 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2031 j = isl_basic_map_alloc_inequality(result);
2034 isl_seq_clr(result->ineq[j], 1+total);
2035 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2036 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2037 isl_int_sub_ui(result->ineq[j][0], d, 1);
2040 result = isl_basic_map_simplify(result);
2041 return isl_basic_map_finalize(result);
2043 isl_basic_map_free(result);
2047 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2053 i = isl_basic_map_alloc_equality(bmap);
2056 nparam = isl_basic_map_n_param(bmap);
2057 n_in = isl_basic_map_n_in(bmap);
2058 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2059 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2060 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2061 return isl_basic_map_finalize(bmap);
2063 isl_basic_map_free(bmap);
2067 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2073 i = isl_basic_map_alloc_inequality(bmap);
2076 nparam = isl_basic_map_n_param(bmap);
2077 n_in = isl_basic_map_n_in(bmap);
2078 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2079 isl_int_set_si(bmap->ineq[i][0], -1);
2080 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2081 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2082 return isl_basic_map_finalize(bmap);
2084 isl_basic_map_free(bmap);
2088 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2094 i = isl_basic_map_alloc_inequality(bmap);
2097 nparam = isl_basic_map_n_param(bmap);
2098 n_in = isl_basic_map_n_in(bmap);
2099 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2100 isl_int_set_si(bmap->ineq[i][0], -1);
2101 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2102 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2103 return isl_basic_map_finalize(bmap);
2105 isl_basic_map_free(bmap);
2109 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2112 struct isl_basic_map *bmap;
2113 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2116 for (i = 0; i < n_equal && bmap; ++i)
2117 bmap = var_equal(bmap, i);
2118 return isl_basic_map_finalize(bmap);
2121 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2124 struct isl_basic_map *bmap;
2125 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2128 for (i = 0; i < pos && bmap; ++i)
2129 bmap = var_equal(bmap, i);
2131 bmap = var_less(bmap, pos);
2132 return isl_basic_map_finalize(bmap);
2135 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2138 struct isl_basic_map *bmap;
2139 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2142 for (i = 0; i < pos && bmap; ++i)
2143 bmap = var_equal(bmap, i);
2145 bmap = var_more(bmap, pos);
2146 return isl_basic_map_finalize(bmap);
2149 struct isl_basic_map *isl_basic_map_from_basic_set(
2150 struct isl_basic_set *bset, struct isl_dim *dim)
2152 struct isl_basic_map *bmap;
2154 bset = isl_basic_set_cow(bset);
2158 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2159 isl_dim_free(bset->dim);
2160 bmap = (struct isl_basic_map *) bset;
2162 return isl_basic_map_finalize(bmap);
2164 isl_basic_set_free(bset);
2169 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2173 if (bmap->dim->n_in == 0)
2174 return (struct isl_basic_set *)bmap;
2175 bmap = isl_basic_map_cow(bmap);
2178 bmap->dim = isl_dim_cow(bmap->dim);
2181 bmap->dim->n_out += bmap->dim->n_in;
2182 bmap->dim->n_in = 0;
2183 bmap = isl_basic_map_finalize(bmap);
2184 return (struct isl_basic_set *)bmap;
2186 isl_basic_map_free(bmap);
2190 /* For a div d = floor(f/m), add the constraints
2193 * -(f-(n-1)) + m d >= 0
2195 * Note that the second constraint is the negation of
2199 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2202 unsigned total = isl_basic_map_total_dim(bmap);
2203 unsigned div_pos = 1 + total - bmap->n_div + div;
2205 i = isl_basic_map_alloc_inequality(bmap);
2208 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2209 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2211 j = isl_basic_map_alloc_inequality(bmap);
2214 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2215 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2216 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2220 struct isl_basic_set *isl_basic_map_underlying_set(
2221 struct isl_basic_map *bmap)
2225 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2226 return (struct isl_basic_set *)bmap;
2227 bmap = isl_basic_map_cow(bmap);
2230 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2233 bmap->extra -= bmap->n_div;
2235 bmap = isl_basic_map_finalize(bmap);
2236 return (struct isl_basic_set *)bmap;
2241 struct isl_basic_map *isl_basic_map_overlying_set(
2242 struct isl_basic_set *bset, struct isl_basic_map *like)
2244 struct isl_basic_map *bmap;
2245 struct isl_ctx *ctx;
2252 isl_assert(ctx, bset->n_div == 0, goto error);
2253 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2254 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2256 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2257 isl_basic_map_free(like);
2258 return (struct isl_basic_map *)bset;
2260 bset = isl_basic_set_cow(bset);
2263 total = bset->dim->n_out + bset->extra;
2264 bmap = (struct isl_basic_map *)bset;
2265 isl_dim_free(bmap->dim);
2266 bmap->dim = isl_dim_copy(like->dim);
2269 bmap->n_div = like->n_div;
2270 bmap->extra += like->n_div;
2273 ltotal = total - bmap->extra + like->extra;
2276 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2277 bmap->extra * (1 + 1 + total));
2278 if (isl_blk_is_error(bmap->block2))
2280 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2284 for (i = 0; i < bmap->extra; ++i)
2285 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2286 for (i = 0; i < like->n_div; ++i) {
2287 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2288 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2290 bmap = isl_basic_map_extend_constraints(bmap,
2291 0, 2 * like->n_div);
2292 for (i = 0; i < like->n_div; ++i) {
2293 if (isl_int_is_zero(bmap->div[i][0]))
2295 if (add_div_constraints(bmap, i) < 0)
2299 isl_basic_map_free(like);
2300 bmap = isl_basic_map_simplify(bmap);
2301 bmap = isl_basic_map_finalize(bmap);
2304 isl_basic_map_free(like);
2305 isl_basic_set_free(bset);
2309 struct isl_basic_set *isl_basic_set_from_underlying_set(
2310 struct isl_basic_set *bset, struct isl_basic_set *like)
2312 return (struct isl_basic_set *)
2313 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2316 struct isl_set *isl_set_from_underlying_set(
2317 struct isl_set *set, struct isl_basic_set *like)
2323 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2325 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2326 isl_basic_set_free(like);
2329 set = isl_set_cow(set);
2332 for (i = 0; i < set->n; ++i) {
2333 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2334 isl_basic_set_copy(like));
2338 isl_dim_free(set->dim);
2339 set->dim = isl_dim_copy(like->dim);
2342 isl_basic_set_free(like);
2345 isl_basic_set_free(like);
2350 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2354 map = isl_map_cow(map);
2357 map->dim = isl_dim_cow(map->dim);
2361 for (i = 1; i < map->n; ++i)
2362 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2364 for (i = 0; i < map->n; ++i) {
2365 map->p[i] = (struct isl_basic_map *)
2366 isl_basic_map_underlying_set(map->p[i]);
2371 map->dim = isl_dim_underlying(map->dim, 0);
2373 isl_dim_free(map->dim);
2374 map->dim = isl_dim_copy(map->p[0]->dim);
2378 return (struct isl_set *)map;
2384 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2386 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2389 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2391 struct isl_basic_set *domain;
2395 n_out = isl_basic_map_n_out(bmap);
2396 domain = isl_basic_set_from_basic_map(bmap);
2397 return isl_basic_set_project_out(domain, n_out, 0);
2400 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2402 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2405 struct isl_set *isl_map_range(struct isl_map *map)
2408 struct isl_set *set;
2412 map = isl_map_cow(map);
2416 set = (struct isl_set *) map;
2417 if (set->dim->n_in != 0) {
2418 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2422 for (i = 0; i < map->n; ++i) {
2423 set->p[i] = isl_basic_map_range(map->p[i]);
2427 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2428 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2435 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2438 struct isl_map *map = NULL;
2440 set = isl_set_cow(set);
2443 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2444 map = (struct isl_map *)set;
2445 for (i = 0; i < set->n; ++i) {
2446 map->p[i] = isl_basic_map_from_basic_set(
2447 set->p[i], isl_dim_copy(dim));
2451 isl_dim_free(map->dim);
2460 struct isl_map *isl_map_from_range(struct isl_set *set)
2462 return (struct isl_map *)set;
2465 struct isl_set *isl_set_from_map(struct isl_map *map)
2468 struct isl_set *set = NULL;
2472 map = isl_map_cow(map);
2475 map->dim = isl_dim_cow(map->dim);
2478 map->dim->n_out += map->dim->n_in;
2480 set = (struct isl_set *)map;
2481 for (i = 0; i < map->n; ++i) {
2482 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2492 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2494 struct isl_map *map;
2498 isl_assert(dim->ctx, n >= 0, return NULL);
2499 map = isl_alloc(dim->ctx, struct isl_map,
2500 sizeof(struct isl_map) +
2501 n * sizeof(struct isl_basic_map *));
2505 map->ctx = dim->ctx;
2506 isl_ctx_ref(map->ctx);
2518 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2519 unsigned nparam, unsigned in, unsigned out, int n,
2522 struct isl_map *map;
2523 struct isl_dim *dims;
2525 dims = isl_dim_alloc(ctx, nparam, in, out);
2529 map = isl_map_alloc_dim(dims, n, flags);
2533 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2534 unsigned nparam, unsigned in, unsigned out)
2536 struct isl_basic_map *bmap;
2537 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2538 bmap = isl_basic_map_set_to_empty(bmap);
2542 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2544 struct isl_basic_set *bset;
2545 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2546 bset = isl_basic_set_set_to_empty(bset);
2550 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2552 struct isl_basic_map *bmap;
2555 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2556 bmap = isl_basic_map_set_to_empty(bmap);
2560 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2562 struct isl_basic_map *bmap;
2565 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2566 bmap = isl_basic_map_set_to_empty(bmap);
2570 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2572 struct isl_basic_set *bset;
2575 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2576 bset = isl_basic_set_set_to_empty(bset);
2580 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2582 struct isl_basic_map *bmap;
2583 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2587 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2589 struct isl_basic_set *bset;
2590 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2594 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2598 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2601 struct isl_map *isl_map_empty(struct isl_dim *dim)
2603 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2606 struct isl_map *isl_map_empty_like(struct isl_map *model)
2610 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2613 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2617 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2620 struct isl_set *isl_set_empty(struct isl_dim *dim)
2622 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2625 struct isl_set *isl_set_empty_like(struct isl_set *model)
2629 return isl_set_empty(isl_dim_copy(model->dim));
2632 struct isl_set *isl_set_universe(struct isl_dim *dim)
2634 struct isl_set *set;
2637 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2638 set = isl_set_add(set, isl_basic_set_universe(dim));
2642 struct isl_map *isl_map_dup(struct isl_map *map)
2645 struct isl_map *dup;
2649 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2650 for (i = 0; i < map->n; ++i)
2651 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2655 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2659 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2660 isl_assert(map->ctx, map->n < map->size, goto error);
2661 map->p[map->n] = bmap;
2663 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2669 isl_basic_map_free(bmap);
2673 void isl_map_free(struct isl_map *map)
2683 isl_ctx_deref(map->ctx);
2684 for (i = 0; i < map->n; ++i)
2685 isl_basic_map_free(map->p[i]);
2686 isl_dim_free(map->dim);
2690 struct isl_map *isl_map_extend(struct isl_map *base,
2691 unsigned nparam, unsigned n_in, unsigned n_out)
2695 base = isl_map_cow(base);
2699 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2702 for (i = 0; i < base->n; ++i) {
2703 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2704 isl_dim_copy(base->dim), 0, 0, 0);
2714 struct isl_set *isl_set_extend(struct isl_set *base,
2715 unsigned nparam, unsigned dim)
2717 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2721 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2722 unsigned pos, int value)
2726 bmap = isl_basic_map_cow(bmap);
2727 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2728 j = isl_basic_map_alloc_equality(bmap);
2731 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2732 isl_int_set_si(bmap->eq[j][pos], -1);
2733 isl_int_set_si(bmap->eq[j][0], value);
2734 bmap = isl_basic_map_simplify(bmap);
2735 return isl_basic_map_finalize(bmap);
2737 isl_basic_map_free(bmap);
2741 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2742 enum isl_dim_type type, unsigned pos, int value)
2746 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2747 return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2750 isl_basic_map_free(bmap);
2754 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2755 enum isl_dim_type type, unsigned pos, int value)
2757 return (struct isl_basic_set *)
2758 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2762 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2763 unsigned input, int value)
2765 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2768 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2769 unsigned dim, int value)
2771 return (struct isl_basic_set *)
2772 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2773 isl_dim_set, dim, value);
2776 struct isl_map *isl_map_fix_si(struct isl_map *map,
2777 enum isl_dim_type type, unsigned pos, int value)
2781 map = isl_map_cow(map);
2785 isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2786 for (i = 0; i < map->n; ++i) {
2787 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2791 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2798 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2799 unsigned input, int value)
2801 return isl_map_fix_si(map, isl_dim_in, input, value);
2804 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2806 return (struct isl_set *)
2807 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2810 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2811 unsigned dim, isl_int value)
2816 bset = isl_basic_set_cow(bset);
2817 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2818 j = isl_basic_set_alloc_inequality(bset);
2821 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2822 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2823 isl_int_neg(bset->ineq[j][0], value);
2824 bset = isl_basic_set_simplify(bset);
2825 return isl_basic_set_finalize(bset);
2827 isl_basic_set_free(bset);
2831 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2836 set = isl_set_cow(set);
2840 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2841 for (i = 0; i < set->n; ++i) {
2842 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2852 struct isl_map *isl_map_reverse(struct isl_map *map)
2857 map = isl_map_cow(map);
2861 map->dim = isl_dim_reverse(map->dim);
2864 for (i = 0; i < map->n; ++i) {
2865 map->p[i] = isl_basic_map_reverse(map->p[i]);
2869 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2876 struct isl_map *isl_basic_map_lexmax(
2877 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2878 struct isl_set **empty)
2880 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2883 struct isl_map *isl_basic_map_lexmin(
2884 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2885 struct isl_set **empty)
2887 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2890 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2892 struct isl_basic_map *bmap = NULL;
2893 struct isl_basic_set *dom = NULL;
2894 struct isl_map *min;
2895 struct isl_dim *param_dim;
2899 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2902 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2903 dom = isl_basic_set_universe(param_dim);
2906 min = isl_basic_map_lexmin(bmap, dom, NULL);
2907 return isl_map_range(min);
2909 isl_basic_map_free(bmap);
2913 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2920 off = isl_dim_total(bmap->dim);
2921 for (i = 0; i < bmap->n_div; ++i) {
2922 if (isl_int_is_zero(bmap->div[i][0]))
2923 return isl_pip_basic_map_compute_divs(bmap);
2924 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2927 return isl_map_from_basic_map(bmap);
2929 isl_basic_map_free(bmap);
2933 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2936 struct isl_map *res;
2942 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2943 for (i = 1 ; i < map->n; ++i) {
2945 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2946 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
2947 res = isl_map_union_disjoint(res, r2);
2949 res = isl_map_union(res, r2);
2956 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2958 return (struct isl_set *)
2959 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2962 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2964 return (struct isl_set *)
2965 isl_map_compute_divs((struct isl_map *)set);
2968 struct isl_set *isl_map_domain(struct isl_map *map)
2971 struct isl_set *set;
2976 map = isl_map_cow(map);
2980 set = (struct isl_set *)map;
2981 set->dim = isl_dim_domain(set->dim);
2984 for (i = 0; i < map->n; ++i) {
2985 set->p[i] = isl_basic_map_domain(map->p[i]);
2989 ISL_F_CLR(set, ISL_MAP_DISJOINT);
2990 ISL_F_CLR(set, ISL_SET_NORMALIZED);
2997 struct isl_map *isl_map_union_disjoint(
2998 struct isl_map *map1, struct isl_map *map2)
3002 struct isl_map *map = NULL;
3016 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3018 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3019 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3020 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3022 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3023 map1->n + map2->n, flags);
3026 for (i = 0; i < map1->n; ++i) {
3027 map = isl_map_add(map,
3028 isl_basic_map_copy(map1->p[i]));
3032 for (i = 0; i < map2->n; ++i) {
3033 map = isl_map_add(map,
3034 isl_basic_map_copy(map2->p[i]));
3048 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3050 map1 = isl_map_union_disjoint(map1, map2);
3054 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3058 struct isl_set *isl_set_union_disjoint(
3059 struct isl_set *set1, struct isl_set *set2)
3061 return (struct isl_set *)
3062 isl_map_union_disjoint(
3063 (struct isl_map *)set1, (struct isl_map *)set2);
3066 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3068 return (struct isl_set *)
3069 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3072 struct isl_map *isl_map_intersect_range(
3073 struct isl_map *map, struct isl_set *set)
3076 struct isl_map *result;
3082 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3083 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3084 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3086 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3087 map->n * set->n, flags);
3090 for (i = 0; i < map->n; ++i)
3091 for (j = 0; j < set->n; ++j) {
3092 result = isl_map_add(result,
3093 isl_basic_map_intersect_range(
3094 isl_basic_map_copy(map->p[i]),
3095 isl_basic_set_copy(set->p[j])));
3108 struct isl_map *isl_map_intersect_domain(
3109 struct isl_map *map, struct isl_set *set)
3111 return isl_map_reverse(
3112 isl_map_intersect_range(isl_map_reverse(map), set));
3115 struct isl_map *isl_map_apply_domain(
3116 struct isl_map *map1, struct isl_map *map2)
3120 map1 = isl_map_reverse(map1);
3121 map1 = isl_map_apply_range(map1, map2);
3122 return isl_map_reverse(map1);
3129 struct isl_map *isl_map_apply_range(
3130 struct isl_map *map1, struct isl_map *map2)
3132 struct isl_dim *dim_result;
3133 struct isl_map *result;
3142 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3143 isl_dim_copy(map2->dim));
3145 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3148 for (i = 0; i < map1->n; ++i)
3149 for (j = 0; j < map2->n; ++j) {
3150 result = isl_map_add(result,
3151 isl_basic_map_apply_range(
3152 isl_basic_map_copy(map1->p[i]),
3153 isl_basic_map_copy(map2->p[j])));
3159 if (result && result->n <= 1)
3160 ISL_F_SET(result, ISL_MAP_DISJOINT);
3169 * returns range - domain
3171 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3173 struct isl_basic_set *bset;
3180 dim = isl_basic_map_n_in(bmap);
3181 nparam = isl_basic_map_n_param(bmap);
3182 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3183 bset = isl_basic_set_from_basic_map(bmap);
3184 bset = isl_basic_set_cow(bset);
3185 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3186 bset = isl_basic_set_swap_vars(bset, 2*dim);
3187 for (i = 0; i < dim; ++i) {
3188 int j = isl_basic_map_alloc_equality(
3189 (struct isl_basic_map *)bset);
3192 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3193 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3194 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3195 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3197 return isl_basic_set_project_out(bset, 2*dim, 0);
3199 isl_basic_map_free(bmap);
3204 * returns range - domain
3206 struct isl_set *isl_map_deltas(struct isl_map *map)
3209 struct isl_set *result;
3214 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3215 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3216 isl_map_n_in(map), map->n, map->flags);
3219 for (i = 0; i < map->n; ++i)
3220 result = isl_set_add(result,
3221 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3229 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3231 struct isl_basic_map *bmap;
3239 nparam = dims->nparam;
3241 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3245 for (i = 0; i < dim; ++i) {
3246 int j = isl_basic_map_alloc_equality(bmap);
3249 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3250 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3251 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3253 return isl_basic_map_finalize(bmap);
3255 isl_basic_map_free(bmap);
3259 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3261 struct isl_dim *dim = isl_dim_map(set_dim);
3264 return basic_map_identity(dim);
3267 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3269 if (!model || !model->dim)
3271 isl_assert(model->ctx,
3272 model->dim->n_in == model->dim->n_out, return NULL);
3273 return basic_map_identity(isl_dim_copy(model->dim));
3276 static struct isl_map *map_identity(struct isl_dim *dim)
3278 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3279 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3282 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3284 struct isl_dim *dim = isl_dim_map(set_dim);
3287 return map_identity(dim);
3290 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3292 if (!model || !model->dim)
3294 isl_assert(model->ctx,
3295 model->dim->n_in == model->dim->n_out, return NULL);
3296 return map_identity(isl_dim_copy(model->dim));
3299 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3301 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3304 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3306 return isl_map_is_subset(
3307 (struct isl_map *)set1, (struct isl_map *)set2);
3310 int isl_basic_map_is_subset(
3311 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3314 struct isl_map *map1;
3315 struct isl_map *map2;
3317 if (!bmap1 || !bmap2)
3320 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3321 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3323 is_subset = isl_map_is_subset(map1, map2);
3331 int isl_basic_map_is_equal(
3332 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3336 if (!bmap1 || !bmap2)
3338 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3341 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3345 int isl_basic_set_is_equal(
3346 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3348 return isl_basic_map_is_equal(
3349 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3352 int isl_map_is_empty(struct isl_map *map)
3359 for (i = 0; i < map->n; ++i) {
3360 is_empty = isl_basic_map_is_empty(map->p[i]);
3369 int isl_map_fast_is_empty(struct isl_map *map)
3374 int isl_set_is_empty(struct isl_set *set)
3376 return isl_map_is_empty((struct isl_map *)set);
3379 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3383 struct isl_map *diff;
3388 if (isl_map_is_empty(map1))
3391 if (isl_map_is_empty(map2))
3394 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3398 is_subset = isl_map_is_empty(diff);
3404 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3410 is_subset = isl_map_is_subset(map1, map2);
3413 is_subset = isl_map_is_subset(map2, map1);
3417 int isl_basic_map_is_strict_subset(
3418 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3422 if (!bmap1 || !bmap2)
3424 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3427 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3428 if (is_subset == -1)
3433 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3437 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3440 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3442 struct isl_basic_set *bset = NULL;
3443 struct isl_vec *sample = NULL;
3450 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3453 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3454 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3455 copy = isl_basic_map_convex_hull(copy);
3456 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3457 isl_basic_map_free(copy);
3461 total = 1 + isl_basic_map_total_dim(bmap);
3462 if (bmap->sample && bmap->sample->size == total) {
3463 int contains = basic_map_contains(bmap, bmap->sample);
3469 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3472 sample = isl_basic_set_sample(bset);
3475 empty = sample->size == 0;
3477 isl_vec_free(bmap->ctx, bmap->sample);
3478 bmap->sample = sample;
3483 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3485 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3488 struct isl_map *isl_basic_map_union(
3489 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3491 struct isl_map *map;
3492 if (!bmap1 || !bmap2)
3495 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3497 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3500 map = isl_map_add(map, bmap1);
3501 map = isl_map_add(map, bmap2);
3504 isl_basic_map_free(bmap1);
3505 isl_basic_map_free(bmap2);
3509 struct isl_set *isl_basic_set_union(
3510 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3512 return (struct isl_set *)isl_basic_map_union(
3513 (struct isl_basic_map *)bset1,
3514 (struct isl_basic_map *)bset2);
3517 /* Order divs such that any div only depends on previous divs */
3518 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3521 unsigned off = isl_dim_total(bmap->dim);
3523 for (i = 0; i < bmap->n_div; ++i) {
3525 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3529 swap_div(bmap, i, pos);
3535 /* Look for a div in dst that corresponds to the div "div" in src.
3536 * The divs before "div" in src and dst are assumed to be the same.
3538 * Returns -1 if no corresponding div was found and the position
3539 * of the corresponding div in dst otherwise.
3541 static int find_div(struct isl_basic_map *dst,
3542 struct isl_basic_map *src, unsigned div)
3546 unsigned total = isl_dim_total(src->dim);
3548 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3549 for (i = div; i < dst->n_div; ++i)
3550 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3551 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3552 dst->n_div - div) == -1)
3557 struct isl_basic_map *isl_basic_map_align_divs(
3558 struct isl_basic_map *dst, struct isl_basic_map *src)
3561 unsigned total = isl_dim_total(src->dim);
3566 if (src->n_div == 0)
3569 for (i = 0; i < src->n_div; ++i)
3570 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3572 src = order_divs(src);
3573 dst = isl_basic_map_cow(dst);
3574 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3575 src->n_div, 0, 2 * src->n_div);
3578 for (i = 0; i < src->n_div; ++i) {
3579 int j = find_div(dst, src, i);
3581 j = isl_basic_map_alloc_div(dst);
3584 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3585 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3586 if (add_div_constraints(dst, j) < 0)
3590 swap_div(dst, i, j);
3594 isl_basic_map_free(dst);
3598 struct isl_basic_set *isl_basic_set_align_divs(
3599 struct isl_basic_set *dst, struct isl_basic_set *src)
3601 return (struct isl_basic_set *)isl_basic_map_align_divs(
3602 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3605 struct isl_map *isl_map_align_divs(struct isl_map *map)
3609 map = isl_map_compute_divs(map);
3610 map = isl_map_cow(map);
3614 for (i = 1; i < map->n; ++i)
3615 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3616 for (i = 1; i < map->n; ++i)
3617 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3619 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3623 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3624 struct isl_basic_map *src, isl_int *c,
3625 unsigned len, int oppose)
3627 struct isl_basic_map *copy = NULL;
3632 copy = isl_basic_map_copy(src);
3633 copy = isl_basic_map_cow(copy);
3636 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3637 k = isl_basic_map_alloc_inequality(copy);
3641 isl_seq_neg(copy->ineq[k], c, len);
3643 isl_seq_cpy(copy->ineq[k], c, len);
3644 total = 1 + isl_basic_map_total_dim(copy);
3645 isl_seq_clr(copy->ineq[k]+len, total - len);
3646 isl_inequality_negate(copy, k);
3647 copy = isl_basic_map_simplify(copy);
3648 copy = isl_basic_map_finalize(copy);
3649 is_empty = isl_basic_map_is_empty(copy);
3653 dst = isl_map_add(dst, copy);
3655 isl_basic_map_free(copy);
3658 isl_basic_map_free(copy);
3663 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3667 struct isl_map *rest = NULL;
3669 unsigned total = isl_basic_map_total_dim(bmap);
3676 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3677 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3679 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3680 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3684 for (i = 0; i < map->n; ++i) {
3685 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3690 for (j = 0; j < map->n; ++j)
3691 map->p[j] = isl_basic_map_cow(map->p[j]);
3693 for (i = 0; i < bmap->n_eq; ++i) {
3694 for (j = 0; j < map->n; ++j) {
3695 rest = add_cut_constraint(rest,
3696 map->p[j], bmap->eq[i], 1+total, 0);
3700 rest = add_cut_constraint(rest,
3701 map->p[j], bmap->eq[i], 1+total, 1);
3705 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3709 k = isl_basic_map_alloc_equality(map->p[j]);
3712 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3713 isl_seq_clr(map->p[j]->eq[k]+1+total,
3714 map->p[j]->n_div - bmap->n_div);
3718 for (i = 0; i < bmap->n_ineq; ++i) {
3719 for (j = 0; j < map->n; ++j) {
3720 rest = add_cut_constraint(rest,
3721 map->p[j], bmap->ineq[i], 1+total, 0);
3725 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3729 k = isl_basic_map_alloc_inequality(map->p[j]);
3732 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3733 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3734 map->p[j]->n_div - bmap->n_div);
3746 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3752 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3754 if (isl_map_is_empty(map2)) {
3759 map1 = isl_map_compute_divs(map1);
3760 map2 = isl_map_compute_divs(map2);
3764 for (i = 0; map1 && i < map2->n; ++i)
3765 map1 = subtract(map1, map2->p[i]);
3775 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3777 return (struct isl_set *)
3779 (struct isl_map *)set1, (struct isl_map *)set2);
3782 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3786 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3787 map = isl_map_intersect_domain(map, set);
3788 set = isl_map_range(map);
3796 /* There is no need to cow as removing empty parts doesn't change
3797 * the meaning of the set.
3799 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3806 for (i = map->n-1; i >= 0; --i) {
3807 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3809 isl_basic_map_free(map->p[i]);
3810 if (i != map->n-1) {
3811 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3812 map->p[i] = map->p[map->n-1];
3820 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3822 return (struct isl_set *)
3823 isl_map_remove_empty_parts((struct isl_map *)set);
3826 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
3828 struct isl_basic_map *bmap;
3829 if (!map || map->n == 0)
3831 bmap = map->p[map->n-1];
3832 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
3833 return isl_basic_map_copy(bmap);
3836 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3838 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
3841 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
3842 struct isl_basic_map *bmap)
3848 for (i = map->n-1; i >= 0; --i) {
3849 if (map->p[i] != bmap)
3851 map = isl_map_cow(map);
3854 isl_basic_map_free(map->p[i]);
3855 if (i != map->n-1) {
3856 ISL_F_CLR(map, ISL_SET_NORMALIZED);
3857 map->p[i] = map->p[map->n-1];
3862 isl_basic_map_free(bmap);
3866 isl_basic_map_free(bmap);
3870 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3871 struct isl_basic_set *bset)
3873 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
3874 (struct isl_basic_map *)bset);
3877 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3878 * for any common value of the parameters and dimensions preceding dim
3879 * in both basic sets, the values of dimension pos in bset1 are
3880 * smaller or larger than those in bset2.
3883 * 1 if bset1 follows bset2
3884 * -1 if bset1 precedes bset2
3885 * 0 if bset1 and bset2 are incomparable
3886 * -2 if some error occurred.
3888 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3889 struct isl_basic_set *bset2, int pos)
3891 struct isl_dim *dims;
3892 struct isl_basic_map *bmap1 = NULL;
3893 struct isl_basic_map *bmap2 = NULL;
3894 struct isl_ctx *ctx;
3895 struct isl_vec *obj;
3898 unsigned dim1, dim2;
3900 enum isl_lp_result res;
3903 if (!bset1 || !bset2)
3906 nparam = isl_basic_set_n_param(bset1);
3907 dim1 = isl_basic_set_n_dim(bset1);
3908 dim2 = isl_basic_set_n_dim(bset2);
3909 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3910 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3911 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3912 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3913 if (!bmap1 || !bmap2)
3915 bmap1 = isl_basic_map_cow(bmap1);
3916 bmap1 = isl_basic_map_extend(bmap1, nparam,
3917 pos, (dim1 - pos) + (dim2 - pos),
3918 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3919 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
3922 total = isl_basic_map_total_dim(bmap1);
3924 obj = isl_vec_alloc(ctx, 1 + total);
3925 isl_seq_clr(obj->block.data, 1 + total);
3926 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
3927 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
3932 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3933 if (res == isl_lp_empty)
3935 else if (res == isl_lp_ok && isl_int_is_pos(num))
3937 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3938 res == isl_lp_unbounded)
3944 isl_basic_map_free(bmap1);
3945 isl_vec_free(ctx, obj);
3948 isl_basic_map_free(bmap1);
3949 isl_basic_map_free(bmap2);
3953 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
3954 unsigned pos, isl_int *val)
3962 total = isl_basic_map_total_dim(bmap);
3963 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
3964 for (; d+1 > pos; --d)
3965 if (!isl_int_is_zero(bmap->eq[i][1+d]))
3969 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
3971 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
3973 if (!isl_int_is_one(bmap->eq[i][1+d]))
3976 isl_int_neg(*val, bmap->eq[i][0]);
3982 static int isl_map_fast_has_fixed_var(struct isl_map *map,
3983 unsigned pos, isl_int *val)
3995 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
3998 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
3999 for (i = 1; fixed == 1 && i < map->n; ++i) {
4000 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4001 if (fixed == 1 && isl_int_ne(tmp, v))
4005 isl_int_set(*val, v);
4011 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4014 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4017 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4018 enum isl_dim_type type, unsigned pos, isl_int *val)
4020 if (pos >= isl_basic_map_dim(bmap, type))
4022 return isl_basic_map_fast_has_fixed_var(bmap,
4023 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4026 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4027 * then return this fixed value in *val.
4029 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4031 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4034 /* Check if input variable in has fixed value and if so and if val is not NULL,
4035 * then return this fixed value in *val.
4037 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4039 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4042 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4043 * and if val is not NULL, then return this lower bound in *val.
4045 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4046 unsigned dim, isl_int *val)
4048 int i, i_eq = -1, i_ineq = -1;
4055 total = isl_basic_set_total_dim(bset);
4056 nparam = isl_basic_set_n_param(bset);
4057 for (i = 0; i < bset->n_eq; ++i) {
4058 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4064 for (i = 0; i < bset->n_ineq; ++i) {
4065 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4067 if (i_eq != -1 || i_ineq != -1)
4071 if (i_eq == -1 && i_ineq == -1)
4073 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4074 /* The coefficient should always be one due to normalization. */
4075 if (!isl_int_is_one(c[1+nparam+dim]))
4077 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4079 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4080 total - nparam - dim - 1) != -1)
4083 isl_int_neg(*val, c[0]);
4087 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4088 unsigned dim, isl_int *val)
4100 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4104 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4106 for (i = 1; fixed == 1 && i < set->n; ++i) {
4107 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4109 if (fixed == 1 && isl_int_ne(tmp, v))
4113 isl_int_set(*val, v);
4124 static int qsort_constraint_cmp(const void *p1, const void *p2)
4126 const struct constraint *c1 = (const struct constraint *)p1;
4127 const struct constraint *c2 = (const struct constraint *)p2;
4128 unsigned size = isl_min(c1->size, c2->size);
4129 return isl_seq_cmp(c1->c, c2->c, size);
4132 static struct isl_basic_map *isl_basic_map_sort_constraints(
4133 struct isl_basic_map *bmap)
4136 struct constraint *c;
4141 total = isl_basic_map_total_dim(bmap);
4142 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4145 for (i = 0; i < bmap->n_ineq; ++i) {
4147 c[i].c = bmap->ineq[i];
4149 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4150 for (i = 0; i < bmap->n_ineq; ++i)
4151 bmap->ineq[i] = c[i].c;
4155 isl_basic_map_free(bmap);
4159 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4163 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4165 bmap = isl_basic_map_convex_hull(bmap);
4166 bmap = isl_basic_map_sort_constraints(bmap);
4167 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4171 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4173 return (struct isl_basic_set *)isl_basic_map_normalize(
4174 (struct isl_basic_map *)bset);
4177 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4178 const struct isl_basic_map *bmap2)
4185 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4186 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4187 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4188 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4189 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4190 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4191 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4192 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4194 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4196 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4198 if (bmap1->n_eq != bmap2->n_eq)
4199 return bmap1->n_eq - bmap2->n_eq;
4200 if (bmap1->n_ineq != bmap2->n_ineq)
4201 return bmap1->n_ineq - bmap2->n_ineq;
4202 if (bmap1->n_div != bmap2->n_div)
4203 return bmap1->n_div - bmap2->n_div;
4204 total = isl_basic_map_total_dim(bmap1);
4205 for (i = 0; i < bmap1->n_eq; ++i) {
4206 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4210 for (i = 0; i < bmap1->n_ineq; ++i) {
4211 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4215 for (i = 0; i < bmap1->n_div; ++i) {
4216 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4223 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4224 struct isl_basic_map *bmap2)
4226 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4229 static int qsort_bmap_cmp(const void *p1, const void *p2)
4231 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4232 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4234 return isl_basic_map_fast_cmp(bmap1, bmap2);
4237 /* We normalize in place, but if anything goes wrong we need
4238 * to return NULL, so we need to make sure we don't change the
4239 * meaning of any possible other copies of map.
4241 struct isl_map *isl_map_normalize(struct isl_map *map)
4244 struct isl_basic_map *bmap;
4248 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4250 for (i = 0; i < map->n; ++i) {
4251 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4254 isl_basic_map_free(map->p[i]);
4257 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4258 ISL_F_SET(map, ISL_MAP_NORMALIZED);
4259 map = isl_map_remove_empty_parts(map);
4262 for (i = map->n - 1; i >= 1; --i) {
4263 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4265 isl_basic_map_free(map->p[i-1]);
4266 for (j = i; j < map->n; ++j)
4267 map->p[j-1] = map->p[j];
4277 struct isl_set *isl_set_normalize(struct isl_set *set)
4279 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4282 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4292 if (!isl_dim_equal(map1->dim, map2->dim))
4295 map1 = isl_map_copy(map1);
4296 map2 = isl_map_copy(map2);
4297 map1 = isl_map_normalize(map1);
4298 map2 = isl_map_normalize(map2);
4301 equal = map1->n == map2->n;
4302 for (i = 0; equal && i < map1->n; ++i) {
4303 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4316 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4318 return isl_map_fast_is_equal((struct isl_map *)set1,
4319 (struct isl_map *)set2);
4322 /* Return an interval that ranges from min to max (inclusive)
4324 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4325 isl_int min, isl_int max)
4328 struct isl_basic_set *bset = NULL;
4330 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4334 k = isl_basic_set_alloc_inequality(bset);
4337 isl_int_set_si(bset->ineq[k][1], 1);
4338 isl_int_neg(bset->ineq[k][0], min);
4340 k = isl_basic_set_alloc_inequality(bset);
4343 isl_int_set_si(bset->ineq[k][1], -1);
4344 isl_int_set(bset->ineq[k][0], max);
4348 isl_basic_set_free(bset);
4352 /* Return the Cartesian product of the basic sets in list (in the given order).
4354 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4362 struct isl_basic_set *product = NULL;
4366 isl_assert(list->ctx, list->n > 0, goto error);
4367 isl_assert(list->ctx, list->p[0], goto error);
4368 nparam = isl_basic_set_n_param(list->p[0]);
4369 dim = isl_basic_set_n_dim(list->p[0]);
4370 extra = list->p[0]->n_div;
4371 n_eq = list->p[0]->n_eq;
4372 n_ineq = list->p[0]->n_ineq;
4373 for (i = 1; i < list->n; ++i) {
4374 isl_assert(list->ctx, list->p[i], goto error);
4375 isl_assert(list->ctx,
4376 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4377 dim += isl_basic_set_n_dim(list->p[i]);
4378 extra += list->p[i]->n_div;
4379 n_eq += list->p[i]->n_eq;
4380 n_ineq += list->p[i]->n_ineq;
4382 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4387 for (i = 0; i < list->n; ++i) {
4388 isl_basic_set_add_constraints(product,
4389 isl_basic_set_copy(list->p[i]), dim);
4390 dim += isl_basic_set_n_dim(list->p[i]);
4392 isl_basic_set_list_free(list);
4395 isl_basic_set_free(product);
4396 isl_basic_set_list_free(list);
4400 struct isl_basic_map *isl_basic_map_product(
4401 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4403 struct isl_dim *dim_result = NULL;
4404 struct isl_basic_map *bmap;
4405 unsigned in1, in2, out1, out2, nparam, total, pos;
4406 struct isl_dim_map *dim_map1, *dim_map2;
4408 if (!bmap1 || !bmap2)
4411 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4412 bmap2->dim, isl_dim_param), goto error);
4413 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4414 isl_dim_copy(bmap2->dim));
4416 in1 = isl_basic_map_n_in(bmap1);
4417 in2 = isl_basic_map_n_in(bmap2);
4418 out1 = isl_basic_map_n_out(bmap1);
4419 out2 = isl_basic_map_n_out(bmap2);
4420 nparam = isl_basic_map_n_param(bmap1);
4422 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4423 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4424 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4425 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4426 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4427 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4428 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4429 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4430 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4431 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4432 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4434 bmap = isl_basic_map_alloc_dim(dim_result,
4435 bmap1->n_div + bmap2->n_div,
4436 bmap1->n_eq + bmap2->n_eq,
4437 bmap1->n_ineq + bmap2->n_ineq);
4438 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4439 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4440 bmap = isl_basic_map_simplify(bmap);
4441 return isl_basic_map_finalize(bmap);
4443 isl_basic_map_free(bmap1);
4444 isl_basic_map_free(bmap2);
4448 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4450 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4453 struct isl_map *result;
4459 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4460 map2->dim, isl_dim_param), goto error);
4462 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4463 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4464 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4466 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4467 isl_dim_copy(map2->dim)),
4468 map1->n * map2->n, flags);
4471 for (i = 0; i < map1->n; ++i)
4472 for (j = 0; j < map2->n; ++j) {
4473 struct isl_basic_map *part;
4474 part = isl_basic_map_product(
4475 isl_basic_map_copy(map1->p[i]),
4476 isl_basic_map_copy(map2->p[j]));
4477 if (isl_basic_map_is_empty(part))
4478 isl_basic_map_free(part);
4480 result = isl_map_add(result, part);
4493 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4501 bset = isl_basic_set_copy(bset);
4502 bset = isl_basic_set_normalize(bset);
4505 total = isl_basic_set_total_dim(bset);
4506 isl_hash_byte(hash, bset->n_eq & 0xFF);
4507 for (i = 0; i < bset->n_eq; ++i) {
4509 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4510 isl_hash_hash(hash, c_hash);
4512 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4513 for (i = 0; i < bset->n_ineq; ++i) {
4515 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4516 isl_hash_hash(hash, c_hash);
4518 isl_hash_byte(hash, bset->n_div & 0xFF);
4519 for (i = 0; i < bset->n_div; ++i) {
4521 if (isl_int_is_zero(bset->div[i][0]))
4523 isl_hash_byte(hash, i & 0xFF);
4524 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4525 isl_hash_hash(hash, c_hash);
4527 isl_basic_set_free(bset);
4531 uint32_t isl_set_get_hash(struct isl_set *set)
4538 set = isl_set_copy(set);
4539 set = isl_set_normalize(set);
4543 hash = isl_hash_init();
4544 for (i = 0; i < set->n; ++i) {
4546 bset_hash = isl_basic_set_get_hash(set->p[i]);
4547 isl_hash_hash(hash, bset_hash);
4555 /* Check if the value for dimension dim is completely determined
4556 * by the values of the other parameters and variables.
4557 * That is, check if dimension dim is involved in an equality.
4559 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4566 nparam = isl_basic_set_n_param(bset);
4567 for (i = 0; i < bset->n_eq; ++i)
4568 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4573 /* Check if the value for dimension dim is completely determined
4574 * by the values of the other parameters and variables.
4575 * That is, check if dimension dim is involved in an equality
4576 * for each of the subsets.
4578 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4584 for (i = 0; i < set->n; ++i) {
4586 unique = isl_basic_set_dim_is_unique(set->p[i], dim);