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 static unsigned basic_map_offset(struct isl_basic_map *bmap,
111 enum isl_dim_type type)
113 struct isl_dim *dim = bmap->dim;
115 case isl_dim_param: return 1;
116 case isl_dim_in: return 1 + dim->nparam;
117 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
118 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
122 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
124 return pos(map->dim, type);
127 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
128 enum isl_dim_type type)
130 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
133 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
135 return bset->dim->n_out;
138 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
140 return bset->dim->nparam;
143 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
145 return isl_dim_total(bset->dim) + bset->n_div;
148 unsigned isl_set_n_dim(const struct isl_set *set)
150 return set->dim->n_out;
153 unsigned isl_set_n_param(const struct isl_set *set)
155 return set->dim->nparam;
158 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
160 return bmap->dim->n_in;
163 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
165 return bmap->dim->n_out;
168 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
170 return bmap->dim->nparam;
173 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
178 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
180 return isl_dim_total(bmap->dim) + bmap->n_div;
183 unsigned isl_map_n_in(const struct isl_map *map)
185 return map->dim->n_in;
188 unsigned isl_map_n_out(const struct isl_map *map)
190 return map->dim->n_out;
193 unsigned isl_map_n_param(const struct isl_map *map)
195 return map->dim->nparam;
198 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
200 return map->dim->n_in == set->dim->n_out &&
201 map->dim->nparam == set->dim->nparam;
204 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
205 struct isl_basic_set *bset)
207 return bmap->dim->n_in == bset->dim->n_out &&
208 bmap->dim->nparam == bset->dim->nparam;
211 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
212 struct isl_basic_set *bset)
214 return bmap->dim->n_out == bset->dim->n_out &&
215 bmap->dim->nparam == bset->dim->nparam;
218 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
219 struct isl_basic_map *bmap, unsigned extra,
220 unsigned n_eq, unsigned n_ineq)
223 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
225 bmap->block = isl_blk_alloc(ctx, (n_eq + n_ineq) * row_size);
226 if (isl_blk_is_error(bmap->block)) {
231 bmap->eq = isl_alloc_array(ctx, isl_int *, n_eq + n_ineq);
233 isl_blk_free(ctx, bmap->block);
239 bmap->block2 = isl_blk_empty();
242 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
243 if (isl_blk_is_error(bmap->block2)) {
245 isl_blk_free(ctx, bmap->block);
250 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
252 isl_blk_free(ctx, bmap->block2);
254 isl_blk_free(ctx, bmap->block);
260 for (i = 0; i < n_eq + n_ineq; ++i)
261 bmap->eq[i] = bmap->block.data + i * row_size;
263 for (i = 0; i < extra; ++i)
264 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
270 bmap->c_size = n_eq + n_ineq;
271 bmap->ineq = bmap->eq + n_eq;
280 isl_basic_map_free(bmap);
284 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
285 unsigned nparam, unsigned dim, unsigned extra,
286 unsigned n_eq, unsigned n_ineq)
288 struct isl_basic_map *bmap;
289 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
290 return (struct isl_basic_set *)bmap;
293 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
294 unsigned extra, unsigned n_eq, unsigned n_ineq)
296 struct isl_basic_map *bmap;
299 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
300 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
301 return (struct isl_basic_set *)bmap;
304 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
305 unsigned extra, unsigned n_eq, unsigned n_ineq)
307 struct isl_basic_map *bmap;
311 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
316 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
322 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
323 unsigned nparam, unsigned in, unsigned out, unsigned extra,
324 unsigned n_eq, unsigned n_ineq)
326 struct isl_basic_map *bmap;
329 dim = isl_dim_alloc(ctx, nparam, in, out);
333 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
337 static void dup_constraints(
338 struct isl_basic_map *dst, struct isl_basic_map *src)
341 unsigned total = isl_basic_map_total_dim(src);
343 for (i = 0; i < src->n_eq; ++i) {
344 int j = isl_basic_map_alloc_equality(dst);
345 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
348 for (i = 0; i < src->n_ineq; ++i) {
349 int j = isl_basic_map_alloc_inequality(dst);
350 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
353 for (i = 0; i < src->n_div; ++i) {
354 int j = isl_basic_map_alloc_div(dst);
355 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
357 F_SET(dst, ISL_BASIC_SET_FINAL);
360 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
362 struct isl_basic_map *dup;
366 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
367 bmap->n_div, bmap->n_eq, bmap->n_ineq);
370 dup->flags = bmap->flags;
371 dup_constraints(dup, bmap);
372 dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
376 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
378 struct isl_basic_map *dup;
380 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
381 return (struct isl_basic_set *)dup;
384 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
389 if (F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
393 return isl_basic_set_dup(bset);
396 struct isl_set *isl_set_copy(struct isl_set *set)
405 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
410 if (F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
414 return isl_basic_map_dup(bmap);
417 struct isl_map *isl_map_copy(struct isl_map *map)
426 void isl_basic_map_free(struct isl_basic_map *bmap)
434 isl_ctx_deref(bmap->ctx);
436 isl_blk_free(bmap->ctx, bmap->block2);
438 isl_blk_free(bmap->ctx, bmap->block);
439 isl_vec_free(bmap->ctx, bmap->sample);
440 isl_dim_free(bmap->dim);
444 void isl_basic_set_free(struct isl_basic_set *bset)
446 isl_basic_map_free((struct isl_basic_map *)bset);
449 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
455 isl_assert(ctx, bmap->n_eq + bmap->n_ineq < bmap->c_size, return -1);
456 isl_assert(ctx, bmap->eq + bmap->n_eq <= bmap->ineq, return -1);
457 if (bmap->eq + bmap->n_eq == bmap->ineq) {
459 int j = isl_basic_map_alloc_inequality(bmap);
463 bmap->ineq[0] = bmap->ineq[j];
468 isl_seq_clr(bmap->eq[bmap->n_eq] +
469 1 + isl_basic_map_total_dim(bmap),
470 bmap->extra - bmap->n_div);
471 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
475 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
477 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
480 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
484 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
489 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
494 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
496 if (pos != bmap->n_eq - 1) {
498 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
499 bmap->eq[bmap->n_eq - 1] = t;
505 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
507 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
510 void isl_basic_map_inequality_to_equality(
511 struct isl_basic_map *bmap, unsigned pos)
516 bmap->ineq[pos] = bmap->ineq[0];
517 bmap->ineq[0] = bmap->eq[bmap->n_eq];
518 bmap->eq[bmap->n_eq] = t;
522 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
523 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
526 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
532 isl_assert(ctx, (bmap->ineq - bmap->eq) + bmap->n_ineq < bmap->c_size,
534 F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
535 F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
536 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
537 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
538 1 + isl_basic_map_total_dim(bmap),
539 bmap->extra - bmap->n_div);
540 return bmap->n_ineq++;
543 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
545 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
548 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
552 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
557 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
559 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
562 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
567 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
569 if (pos != bmap->n_ineq - 1) {
571 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
572 bmap->ineq[bmap->n_ineq - 1] = t;
573 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
579 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
581 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
584 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
588 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
589 isl_seq_clr(bmap->div[bmap->n_div] +
590 1 + 1 + isl_basic_map_total_dim(bmap),
591 bmap->extra - bmap->n_div);
592 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
593 return bmap->n_div++;
596 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
600 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
605 /* Copy constraint from src to dst, putting the vars of src at offset
606 * dim_off in dst and the divs of src at offset div_off in dst.
607 * If both sets are actually map, then dim_off applies to the input
610 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
611 struct isl_basic_map *src_map, isl_int *src,
612 unsigned in_off, unsigned out_off, unsigned div_off)
614 unsigned src_nparam = isl_basic_map_n_param(src_map);
615 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
616 unsigned src_in = isl_basic_map_n_in(src_map);
617 unsigned dst_in = isl_basic_map_n_in(dst_map);
618 unsigned src_out = isl_basic_map_n_out(src_map);
619 unsigned dst_out = isl_basic_map_n_out(dst_map);
620 isl_int_set(dst[0], src[0]);
621 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
622 if (dst_nparam > src_nparam)
623 isl_seq_clr(dst+1+src_nparam,
624 dst_nparam - src_nparam);
625 isl_seq_clr(dst+1+dst_nparam, in_off);
626 isl_seq_cpy(dst+1+dst_nparam+in_off,
628 isl_min(dst_in-in_off, src_in));
629 if (dst_in-in_off > src_in)
630 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
631 dst_in - in_off - src_in);
632 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
633 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
634 src+1+src_nparam+src_in,
635 isl_min(dst_out-out_off, src_out));
636 if (dst_out-out_off > src_out)
637 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
638 dst_out - out_off - src_out);
639 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
640 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
641 src+1+src_nparam+src_in+src_out,
642 isl_min(dst_map->extra-div_off, src_map->n_div));
643 if (dst_map->n_div-div_off > src_map->n_div)
644 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
645 div_off+src_map->n_div,
646 dst_map->n_div - div_off - src_map->n_div);
649 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
650 struct isl_basic_map *src_map, isl_int *src,
651 unsigned in_off, unsigned out_off, unsigned div_off)
653 isl_int_set(dst[0], src[0]);
654 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
657 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
658 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
663 if (!bmap1 || !bmap2)
666 div_off = bmap1->n_div;
668 for (i = 0; i < bmap2->n_eq; ++i) {
669 int i1 = isl_basic_map_alloc_equality(bmap1);
672 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
673 i_pos, o_pos, div_off);
676 for (i = 0; i < bmap2->n_ineq; ++i) {
677 int i1 = isl_basic_map_alloc_inequality(bmap1);
680 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
681 i_pos, o_pos, div_off);
684 for (i = 0; i < bmap2->n_div; ++i) {
685 int i1 = isl_basic_map_alloc_div(bmap1);
688 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
689 i_pos, o_pos, div_off);
692 isl_basic_map_free(bmap2);
697 isl_basic_map_free(bmap1);
698 isl_basic_map_free(bmap2);
702 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
703 struct isl_dim_map *dim_map)
707 for (i = 0; i < dim_map->len; ++i) {
708 if (dim_map->pos[i] < 0)
709 isl_int_set_si(dst[i], 0);
711 isl_int_set(dst[i], src[dim_map->pos[i]]);
715 static void copy_div_dim_map(isl_int *dst, isl_int *src,
716 struct isl_dim_map *dim_map)
718 isl_int_set(dst[0], src[0]);
719 copy_constraint_dim_map(dst+1, src+1, dim_map);
722 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
723 struct isl_basic_map *src, struct isl_dim_map *dim_map)
727 if (!src || !dst || !dim_map)
730 for (i = 0; i < src->n_eq; ++i) {
731 int i1 = isl_basic_map_alloc_equality(dst);
734 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
737 for (i = 0; i < src->n_ineq; ++i) {
738 int i1 = isl_basic_map_alloc_inequality(dst);
741 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
744 for (i = 0; i < src->n_div; ++i) {
745 int i1 = isl_basic_map_alloc_div(dst);
748 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
752 isl_basic_map_free(src);
757 isl_basic_map_free(src);
758 isl_basic_map_free(dst);
762 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
763 struct isl_basic_set *bset2, unsigned pos)
765 return (struct isl_basic_set *)
766 add_constraints((struct isl_basic_map *)bset1,
767 (struct isl_basic_map *)bset2, 0, pos);
770 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
771 struct isl_dim *dim, unsigned extra,
772 unsigned n_eq, unsigned n_ineq)
774 struct isl_basic_map *ext;
781 base = isl_basic_map_cow(base);
785 dims_ok = isl_dim_equal(base->dim, dim) &&
786 base->extra >= base->n_div + extra;
788 if (dims_ok && n_eq == 0 && n_ineq == 0) {
793 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
794 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
795 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
796 extra += base->extra;
798 n_ineq += base->n_ineq;
800 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
806 ext = add_constraints(ext, base, 0, 0);
809 F_CLR(ext, ISL_BASIC_SET_FINAL);
816 isl_basic_map_free(base);
820 struct isl_basic_map *isl_basic_map_extend_constraints(
821 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
825 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
829 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
830 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
831 unsigned n_eq, unsigned n_ineq)
833 struct isl_basic_map *bmap;
838 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
842 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
846 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
847 unsigned nparam, unsigned dim, unsigned extra,
848 unsigned n_eq, unsigned n_ineq)
850 return (struct isl_basic_set *)
851 isl_basic_map_extend((struct isl_basic_map *)base,
852 nparam, 0, dim, extra, n_eq, n_ineq);
855 struct isl_basic_set *isl_basic_set_extend_constraints(
856 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
858 return (struct isl_basic_set *)
859 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
863 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
865 return (struct isl_basic_set *)
866 isl_basic_map_cow((struct isl_basic_map *)bset);
869 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
876 bmap = isl_basic_map_dup(bmap);
878 F_CLR(bmap, ISL_BASIC_SET_FINAL);
882 struct isl_set *isl_set_cow(struct isl_set *set)
890 return isl_set_dup(set);
893 struct isl_map *isl_map_cow(struct isl_map *map)
901 return isl_map_dup(map);
904 static void swap_vars(struct isl_blk blk, isl_int *a,
905 unsigned a_len, unsigned b_len)
907 isl_seq_cpy(blk.data, a+a_len, b_len);
908 isl_seq_cpy(blk.data+b_len, a, a_len);
909 isl_seq_cpy(a, blk.data, b_len+a_len);
912 struct isl_basic_set *isl_basic_set_swap_vars(
913 struct isl_basic_set *bset, unsigned n)
923 nparam = isl_basic_set_n_param(bset);
924 dim = isl_basic_set_n_dim(bset);
925 isl_assert(bset->ctx, n <= dim, goto error);
930 bset = isl_basic_set_cow(bset);
934 blk = isl_blk_alloc(bset->ctx, dim);
935 if (isl_blk_is_error(blk))
938 for (i = 0; i < bset->n_eq; ++i)
940 bset->eq[i]+1+nparam, n, dim - n);
942 for (i = 0; i < bset->n_ineq; ++i)
944 bset->ineq[i]+1+nparam, n, dim - n);
946 for (i = 0; i < bset->n_div; ++i)
948 bset->div[i]+1+1+nparam, n, dim - n);
950 isl_blk_free(bset->ctx, blk);
952 F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
956 isl_basic_set_free(bset);
960 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
963 set = isl_set_cow(set);
967 for (i = 0; i < set->n; ++i) {
968 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
974 F_CLR(set, ISL_SET_NORMALIZED);
978 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
984 total = isl_basic_map_total_dim(bmap);
985 isl_basic_map_free_div(bmap, bmap->n_div);
986 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
988 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
990 isl_basic_map_alloc_equality(bmap);
994 isl_int_set_si(bmap->eq[i][0], 1);
995 isl_seq_clr(bmap->eq[i]+1, total);
996 F_SET(bmap, ISL_BASIC_MAP_EMPTY);
997 return isl_basic_map_finalize(bmap);
999 isl_basic_map_free(bmap);
1003 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1005 return (struct isl_basic_set *)
1006 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1009 static void swap_div(struct isl_basic_map *bmap, int a, int b)
1012 unsigned off = isl_dim_total(bmap->dim);
1013 isl_int *t = bmap->div[a];
1014 bmap->div[a] = bmap->div[b];
1017 for (i = 0; i < bmap->n_eq; ++i)
1018 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1020 for (i = 0; i < bmap->n_ineq; ++i)
1021 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1023 for (i = 0; i < bmap->n_div; ++i)
1024 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1025 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1028 /* Eliminate the specified n dimensions starting at first from the
1029 * constraints using Fourier-Motzkin, The dimensions themselves
1032 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1033 unsigned first, unsigned n)
1043 set = isl_set_cow(set);
1046 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1047 nparam = isl_set_n_param(set);
1049 for (i = 0; i < set->n; ++i) {
1050 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1061 /* Project out n dimensions starting at first using Fourier-Motzkin */
1062 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1063 unsigned first, unsigned n)
1065 set = isl_set_eliminate_dims(set, first, n);
1066 set = isl_set_drop_dims(set, first, n);
1070 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1072 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1080 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1089 set = isl_set_cow(set);
1093 for (i = 0; i < set->n; ++i) {
1094 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1104 /* Project out n inputs starting at first using Fourier-Motzkin */
1105 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1106 unsigned first, unsigned n)
1114 map = isl_map_cow(map);
1117 nparam = isl_map_n_param(map);
1118 isl_assert(map->ctx, first+n <= isl_map_n_in(map), goto error);
1120 for (i = 0; i < map->n; ++i) {
1121 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1126 map = isl_map_drop_inputs(map, first, n);
1133 /* Project out n dimensions starting at first using Fourier-Motzkin */
1134 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1135 unsigned first, unsigned n)
1137 unsigned nparam = isl_basic_set_n_param(bset);
1138 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1139 bset = isl_basic_set_drop_dims(bset, first, n);
1143 static void dump_term(struct isl_basic_map *bmap,
1144 isl_int c, int pos, FILE *out)
1147 unsigned in = isl_basic_map_n_in(bmap);
1148 unsigned dim = in + isl_basic_map_n_out(bmap);
1149 unsigned nparam = isl_basic_map_n_param(bmap);
1151 isl_int_print(out, c, 0);
1153 if (!isl_int_is_one(c))
1154 isl_int_print(out, c, 0);
1155 if (pos < 1 + nparam) {
1156 name = isl_dim_get_name(bmap->dim,
1157 isl_dim_param, pos - 1);
1159 fprintf(out, "%s", name);
1161 fprintf(out, "p%d", pos - 1);
1162 } else if (pos < 1 + nparam + in)
1163 fprintf(out, "i%d", pos - 1 - nparam);
1164 else if (pos < 1 + nparam + dim)
1165 fprintf(out, "o%d", pos - 1 - nparam - in);
1167 fprintf(out, "e%d", pos - 1 - nparam - dim);
1171 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1172 int sign, FILE *out)
1176 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1180 for (i = 0, first = 1; i < len; ++i) {
1181 if (isl_int_sgn(c[i]) * sign <= 0)
1184 fprintf(out, " + ");
1186 isl_int_abs(v, c[i]);
1187 dump_term(bmap, v, i, out);
1194 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1195 const char *op, FILE *out, int indent)
1199 fprintf(out, "%*s", indent, "");
1201 dump_constraint_sign(bmap, c, 1, out);
1202 fprintf(out, " %s ", op);
1203 dump_constraint_sign(bmap, c, -1, out);
1207 for (i = bmap->n_div; i < bmap->extra; ++i) {
1208 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1210 fprintf(out, "%*s", indent, "");
1211 fprintf(out, "ERROR: unused div coefficient not zero\n");
1216 static void dump_constraints(struct isl_basic_map *bmap,
1217 isl_int **c, unsigned n,
1218 const char *op, FILE *out, int indent)
1222 for (i = 0; i < n; ++i)
1223 dump_constraint(bmap, c[i], op, out, indent);
1226 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1230 unsigned total = isl_basic_map_total_dim(bmap);
1232 for (j = 0; j < 1 + total; ++j) {
1233 if (isl_int_is_zero(exp[j]))
1235 if (!first && isl_int_is_pos(exp[j]))
1237 dump_term(bmap, exp[j], j, out);
1242 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1246 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1247 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1249 for (i = 0; i < bmap->n_div; ++i) {
1250 fprintf(out, "%*s", indent, "");
1251 fprintf(out, "e%d = [(", i);
1252 dump_affine(bmap, bmap->div[i]+1, out);
1254 isl_int_print(out, bmap->div[i][0], 0);
1255 fprintf(out, "]\n");
1259 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1262 fprintf(out, "null basic set\n");
1266 fprintf(out, "%*s", indent, "");
1267 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1268 bset->ref, bset->dim->nparam, bset->dim->n_out,
1269 bset->extra, bset->flags);
1270 dump((struct isl_basic_map *)bset, out, indent);
1273 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1276 fprintf(out, "null basic map\n");
1280 fprintf(out, "%*s", indent, "");
1281 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1282 "flags: %x, n_name: %d\n",
1284 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1285 bmap->extra, bmap->flags, bmap->dim->n_name);
1286 dump(bmap, out, indent);
1289 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1294 total = isl_basic_map_total_dim(bmap);
1295 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1296 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1297 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1298 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1302 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1304 struct isl_set *set;
1308 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1309 isl_assert(dim->ctx, n >= 0, return NULL);
1310 set = isl_alloc(dim->ctx, struct isl_set,
1311 sizeof(struct isl_set) +
1312 n * sizeof(struct isl_basic_set *));
1316 set->ctx = dim->ctx;
1317 isl_ctx_ref(set->ctx);
1329 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1330 unsigned nparam, unsigned dim, int n, unsigned flags)
1332 struct isl_set *set;
1333 struct isl_dim *dims;
1335 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1339 set = isl_set_alloc_dim(dims, n, flags);
1343 struct isl_set *isl_set_dup(struct isl_set *set)
1346 struct isl_set *dup;
1351 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1354 for (i = 0; i < set->n; ++i)
1355 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1359 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1361 struct isl_set *set;
1366 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1368 isl_basic_set_free(bset);
1371 return isl_set_add(set, bset);
1374 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1376 struct isl_map *map;
1381 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1383 isl_basic_map_free(bmap);
1386 return isl_map_add(map, bmap);
1389 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1393 isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1394 isl_assert(set->ctx, set->n < set->size, goto error);
1395 set->p[set->n] = bset;
1402 isl_basic_set_free(bset);
1406 void isl_set_free(struct isl_set *set)
1416 isl_ctx_deref(set->ctx);
1417 for (i = 0; i < set->n; ++i)
1418 isl_basic_set_free(set->p[i]);
1419 isl_dim_free(set->dim);
1423 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1428 fprintf(out, "null set\n");
1432 fprintf(out, "%*s", indent, "");
1433 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1434 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1436 for (i = 0; i < set->n; ++i) {
1437 fprintf(out, "%*s", indent, "");
1438 fprintf(out, "basic set %d:\n", i);
1439 isl_basic_set_dump(set->p[i], out, indent+4);
1443 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1448 fprintf(out, "null map\n");
1452 fprintf(out, "%*s", indent, "");
1453 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1454 "flags: %x, n_name: %d\n",
1455 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1456 map->dim->n_out, map->flags, map->dim->n_name);
1457 for (i = 0; i < map->n; ++i) {
1458 fprintf(out, "%*s", indent, "");
1459 fprintf(out, "basic map %d:\n", i);
1460 isl_basic_map_dump(map->p[i], out, indent+4);
1464 struct isl_basic_map *isl_basic_map_intersect_domain(
1465 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1467 struct isl_basic_map *bmap_domain;
1468 struct isl_dim *dim;
1473 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1474 bset->dim, isl_dim_param), goto error);
1476 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1477 isl_assert(bset->ctx,
1478 isl_basic_map_compatible_domain(bmap, bset), goto error);
1480 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1481 bset->n_div, bset->n_eq, bset->n_ineq);
1484 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1485 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1486 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1488 bmap = isl_basic_map_simplify(bmap);
1489 return isl_basic_map_finalize(bmap);
1491 isl_basic_map_free(bmap);
1492 isl_basic_set_free(bset);
1496 struct isl_basic_map *isl_basic_map_intersect_range(
1497 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1499 struct isl_basic_map *bmap_range;
1504 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1505 bset->dim, isl_dim_param), goto error);
1507 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1508 isl_assert(bset->ctx,
1509 isl_basic_map_compatible_range(bmap, bset), goto error);
1511 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1512 bset->n_div, bset->n_eq, bset->n_ineq);
1515 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1516 bmap = add_constraints(bmap, bmap_range, 0, 0);
1518 bmap = isl_basic_map_simplify(bmap);
1519 return isl_basic_map_finalize(bmap);
1521 isl_basic_map_free(bmap);
1522 isl_basic_set_free(bset);
1526 struct isl_basic_map *isl_basic_map_intersect(
1527 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1529 if (!bmap1 || !bmap2)
1532 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1533 bmap2->dim, isl_dim_param), goto error);
1534 if (isl_dim_total(bmap1->dim) ==
1535 isl_dim_size(bmap1->dim, isl_dim_param) &&
1536 isl_dim_total(bmap2->dim) !=
1537 isl_dim_size(bmap2->dim, isl_dim_param))
1538 return isl_basic_map_intersect(bmap2, bmap1);
1540 if (isl_dim_total(bmap2->dim) !=
1541 isl_dim_size(bmap2->dim, isl_dim_param))
1542 isl_assert(bmap1->ctx,
1543 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1545 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1546 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1549 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1551 bmap1 = isl_basic_map_simplify(bmap1);
1552 return isl_basic_map_finalize(bmap1);
1554 isl_basic_map_free(bmap1);
1555 isl_basic_map_free(bmap2);
1559 struct isl_basic_set *isl_basic_set_intersect(
1560 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1562 return (struct isl_basic_set *)
1563 isl_basic_map_intersect(
1564 (struct isl_basic_map *)bset1,
1565 (struct isl_basic_map *)bset2);
1568 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1571 struct isl_map *result;
1577 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1578 map2->dim, isl_dim_param), goto error);
1579 if (isl_dim_total(map1->dim) ==
1580 isl_dim_size(map1->dim, isl_dim_param) &&
1581 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1582 return isl_map_intersect(map2, map1);
1584 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1585 isl_assert(map1->ctx,
1586 isl_dim_equal(map1->dim, map2->dim), goto error);
1588 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
1589 F_ISSET(map2, ISL_MAP_DISJOINT))
1590 FL_SET(flags, ISL_MAP_DISJOINT);
1592 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1593 map1->n * map2->n, flags);
1596 for (i = 0; i < map1->n; ++i)
1597 for (j = 0; j < map2->n; ++j) {
1598 struct isl_basic_map *part;
1599 part = isl_basic_map_intersect(
1600 isl_basic_map_copy(map1->p[i]),
1601 isl_basic_map_copy(map2->p[j]));
1602 if (isl_basic_map_is_empty(part))
1603 isl_basic_map_free(part);
1605 result = isl_map_add(result, part);
1618 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1620 return (struct isl_set *)
1621 isl_map_intersect((struct isl_map *)set1,
1622 (struct isl_map *)set2);
1625 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1627 struct isl_dim *dim;
1628 struct isl_basic_set *bset;
1633 bmap = isl_basic_map_cow(bmap);
1636 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1637 in = isl_basic_map_n_in(bmap);
1638 bset = isl_basic_set_from_basic_map(bmap);
1639 bset = isl_basic_set_swap_vars(bset, in);
1640 return isl_basic_map_from_basic_set(bset, dim);
1643 /* Turn final n dimensions into existentially quantified variables.
1645 struct isl_basic_set *isl_basic_set_project_out(
1646 struct isl_basic_set *bset, unsigned n, unsigned flags)
1656 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1661 bset = isl_basic_set_cow(bset);
1663 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1664 old = bset->block2.data;
1665 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1666 (bset->extra + n) * (1 + row_size));
1667 if (!bset->block2.data)
1669 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1672 for (i = 0; i < n; ++i) {
1673 new_div[i] = bset->block2.data +
1674 (bset->extra + i) * (1 + row_size);
1675 isl_seq_clr(new_div[i], 1 + row_size);
1677 for (i = 0; i < bset->extra; ++i)
1678 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1680 bset->div = new_div;
1683 bset->dim = isl_dim_drop_outputs(bset->dim,
1684 isl_basic_set_n_dim(bset) - n, n);
1687 bset = isl_basic_set_simplify(bset);
1688 return isl_basic_set_finalize(bset);
1690 isl_basic_set_free(bset);
1694 struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1698 for (i = 0; i < n; ++i) {
1699 j = isl_basic_map_alloc_div(bmap);
1702 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1706 isl_basic_map_free(bmap);
1710 struct isl_basic_map *isl_basic_map_apply_range(
1711 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1713 struct isl_dim *dim_result = NULL;
1714 struct isl_basic_map *bmap;
1715 unsigned n_in, n_out, n, nparam, total, pos;
1716 struct isl_dim_map *dim_map1, *dim_map2;
1718 if (!bmap1 || !bmap2)
1721 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1722 isl_dim_copy(bmap2->dim));
1724 n_in = isl_basic_map_n_in(bmap1);
1725 n_out = isl_basic_map_n_out(bmap2);
1726 n = isl_basic_map_n_out(bmap1);
1727 nparam = isl_basic_map_n_param(bmap1);
1729 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1730 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1731 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1732 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1733 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1734 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1735 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1736 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1737 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1738 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1739 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1741 bmap = isl_basic_map_alloc_dim(dim_result,
1742 bmap1->n_div + bmap2->n_div + n,
1743 bmap1->n_eq + bmap2->n_eq,
1744 bmap1->n_ineq + bmap2->n_ineq);
1745 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1746 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1747 bmap = add_divs(bmap, n);
1748 bmap = isl_basic_map_simplify(bmap);
1749 return isl_basic_map_finalize(bmap);
1751 isl_basic_map_free(bmap1);
1752 isl_basic_map_free(bmap2);
1756 struct isl_basic_set *isl_basic_set_apply(
1757 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1762 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1765 return (struct isl_basic_set *)
1766 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1768 isl_basic_set_free(bset);
1769 isl_basic_map_free(bmap);
1773 struct isl_basic_map *isl_basic_map_apply_domain(
1774 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1776 if (!bmap1 || !bmap2)
1780 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1782 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1785 bmap1 = isl_basic_map_reverse(bmap1);
1786 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1787 return isl_basic_map_reverse(bmap1);
1789 isl_basic_map_free(bmap1);
1790 isl_basic_map_free(bmap2);
1794 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1795 * A \cap B -> f(A) + f(B)
1797 struct isl_basic_map *isl_basic_map_sum(
1798 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1800 unsigned n_in, n_out, nparam, total, pos;
1801 struct isl_basic_map *bmap = NULL;
1802 struct isl_dim_map *dim_map1, *dim_map2;
1805 if (!bmap1 || !bmap2)
1808 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1811 nparam = isl_basic_map_n_param(bmap1);
1812 n_in = isl_basic_map_n_in(bmap1);
1813 n_out = isl_basic_map_n_out(bmap1);
1815 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
1816 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1817 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
1818 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1819 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
1820 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1821 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1822 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
1823 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1824 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1825 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
1827 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
1828 bmap1->n_div + bmap2->n_div + 2 * n_out,
1829 bmap1->n_eq + bmap2->n_eq + n_out,
1830 bmap1->n_ineq + bmap2->n_ineq);
1831 for (i = 0; i < n_out; ++i) {
1832 int j = isl_basic_map_alloc_equality(bmap);
1835 isl_seq_clr(bmap->eq[j], 1+total);
1836 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
1837 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
1838 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
1840 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1841 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1842 bmap = add_divs(bmap, 2 * n_out);
1844 bmap = isl_basic_map_simplify(bmap);
1845 return isl_basic_map_finalize(bmap);
1847 isl_basic_map_free(bmap);
1848 isl_basic_map_free(bmap1);
1849 isl_basic_map_free(bmap2);
1853 /* Given a basic map A -> f(A), construct A -> -f(A).
1855 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
1860 bmap = isl_basic_map_cow(bmap);
1864 n = isl_basic_map_dim(bmap, isl_dim_out);
1865 off = basic_map_offset(bmap, isl_dim_out);
1866 for (i = 0; i < bmap->n_eq; ++i)
1867 for (j = 0; j < n; ++j)
1868 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
1869 for (i = 0; i < bmap->n_ineq; ++i)
1870 for (j = 0; j < n; ++j)
1871 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
1872 for (i = 0; i < bmap->n_div; ++i)
1873 for (j = 0; j < n; ++j)
1874 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
1875 return isl_basic_map_finalize(bmap);
1878 /* Given a basic map A -> f(A) and an integer d, construct a basic map
1879 * A -> floor(f(A)/d).
1881 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
1884 unsigned n_in, n_out, nparam, total, pos;
1885 struct isl_basic_map *result = NULL;
1886 struct isl_dim_map *dim_map;
1892 nparam = isl_basic_map_n_param(bmap);
1893 n_in = isl_basic_map_n_in(bmap);
1894 n_out = isl_basic_map_n_out(bmap);
1896 total = nparam + n_in + n_out + bmap->n_div + n_out;
1897 dim_map = isl_dim_map_alloc(bmap->ctx, total);
1898 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
1899 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
1900 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
1901 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
1903 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
1904 bmap->n_div + n_out,
1905 bmap->n_eq, bmap->n_ineq + 2 * n_out);
1906 result = add_constraints_dim_map(result, bmap, dim_map);
1907 result = add_divs(result, n_out);
1908 for (i = 0; i < n_out; ++i) {
1910 j = isl_basic_map_alloc_inequality(result);
1913 isl_seq_clr(result->ineq[j], 1+total);
1914 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
1915 isl_int_set_si(result->ineq[j][1+pos+i], 1);
1916 j = isl_basic_map_alloc_inequality(result);
1919 isl_seq_clr(result->ineq[j], 1+total);
1920 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
1921 isl_int_set_si(result->ineq[j][1+pos+i], -1);
1922 isl_int_sub_ui(result->ineq[j][0], d, 1);
1925 result = isl_basic_map_simplify(result);
1926 return isl_basic_map_finalize(result);
1928 isl_basic_map_free(result);
1932 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
1938 i = isl_basic_map_alloc_equality(bmap);
1941 nparam = isl_basic_map_n_param(bmap);
1942 n_in = isl_basic_map_n_in(bmap);
1943 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
1944 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
1945 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
1946 return isl_basic_map_finalize(bmap);
1948 isl_basic_map_free(bmap);
1952 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
1958 i = isl_basic_map_alloc_inequality(bmap);
1961 nparam = isl_basic_map_n_param(bmap);
1962 n_in = isl_basic_map_n_in(bmap);
1963 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
1964 isl_int_set_si(bmap->ineq[i][0], -1);
1965 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
1966 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
1967 return isl_basic_map_finalize(bmap);
1969 isl_basic_map_free(bmap);
1973 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
1979 i = isl_basic_map_alloc_inequality(bmap);
1982 nparam = isl_basic_map_n_param(bmap);
1983 n_in = isl_basic_map_n_in(bmap);
1984 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
1985 isl_int_set_si(bmap->ineq[i][0], -1);
1986 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
1987 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
1988 return isl_basic_map_finalize(bmap);
1990 isl_basic_map_free(bmap);
1994 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
1997 struct isl_basic_map *bmap;
1998 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2001 for (i = 0; i < n_equal && bmap; ++i)
2002 bmap = var_equal(bmap, i);
2003 return isl_basic_map_finalize(bmap);
2006 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2009 struct isl_basic_map *bmap;
2010 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2013 for (i = 0; i < pos && bmap; ++i)
2014 bmap = var_equal(bmap, i);
2016 bmap = var_less(bmap, pos);
2017 return isl_basic_map_finalize(bmap);
2020 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2023 struct isl_basic_map *bmap;
2024 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2027 for (i = 0; i < pos && bmap; ++i)
2028 bmap = var_equal(bmap, i);
2030 bmap = var_more(bmap, pos);
2031 return isl_basic_map_finalize(bmap);
2034 struct isl_basic_map *isl_basic_map_from_basic_set(
2035 struct isl_basic_set *bset, struct isl_dim *dim)
2037 struct isl_basic_map *bmap;
2039 bset = isl_basic_set_cow(bset);
2043 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2044 isl_dim_free(bset->dim);
2045 bmap = (struct isl_basic_map *) bset;
2047 return isl_basic_map_finalize(bmap);
2049 isl_basic_set_free(bset);
2054 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2058 if (bmap->dim->n_in == 0)
2059 return (struct isl_basic_set *)bmap;
2060 bmap = isl_basic_map_cow(bmap);
2063 bmap->dim = isl_dim_cow(bmap->dim);
2066 bmap->dim->n_out += bmap->dim->n_in;
2067 bmap->dim->n_in = 0;
2068 bmap = isl_basic_map_finalize(bmap);
2069 return (struct isl_basic_set *)bmap;
2071 isl_basic_map_free(bmap);
2075 /* For a div d = floor(f/m), add the constraints
2078 * -(f-(n-1)) + m d >= 0
2080 * Note that the second constraint is the negation of
2084 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2087 unsigned total = isl_basic_map_total_dim(bmap);
2088 unsigned div_pos = 1 + total - bmap->n_div + div;
2090 i = isl_basic_map_alloc_inequality(bmap);
2093 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2094 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2096 j = isl_basic_map_alloc_inequality(bmap);
2099 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2100 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2101 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2105 struct isl_basic_set *isl_basic_map_underlying_set(
2106 struct isl_basic_map *bmap)
2110 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2111 return (struct isl_basic_set *)bmap;
2112 bmap = isl_basic_map_cow(bmap);
2115 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2118 bmap->extra -= bmap->n_div;
2120 bmap = isl_basic_map_finalize(bmap);
2121 return (struct isl_basic_set *)bmap;
2126 struct isl_basic_map *isl_basic_map_overlying_set(
2127 struct isl_basic_set *bset, struct isl_basic_map *like)
2129 struct isl_basic_map *bmap;
2130 struct isl_ctx *ctx;
2137 isl_assert(ctx, bset->n_div == 0, goto error);
2138 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2139 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2141 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2142 isl_basic_map_free(like);
2143 return (struct isl_basic_map *)bset;
2145 bset = isl_basic_set_cow(bset);
2148 total = bset->dim->n_out + bset->extra;
2149 bmap = (struct isl_basic_map *)bset;
2150 isl_dim_free(bmap->dim);
2151 bmap->dim = isl_dim_copy(like->dim);
2154 bmap->n_div = like->n_div;
2155 bmap->extra += like->n_div;
2158 ltotal = total - bmap->extra + like->extra;
2161 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2162 bmap->extra * (1 + 1 + total));
2163 if (isl_blk_is_error(bmap->block2))
2165 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2169 for (i = 0; i < bmap->extra; ++i)
2170 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2171 for (i = 0; i < like->n_div; ++i) {
2172 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2173 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2175 bmap = isl_basic_map_extend_constraints(bmap,
2176 0, 2 * like->n_div);
2177 for (i = 0; i < like->n_div; ++i) {
2178 if (isl_int_is_zero(bmap->div[i][0]))
2180 if (add_div_constraints(bmap, i) < 0)
2184 isl_basic_map_free(like);
2185 bmap = isl_basic_map_simplify(bmap);
2186 bmap = isl_basic_map_finalize(bmap);
2189 isl_basic_map_free(like);
2190 isl_basic_set_free(bset);
2194 struct isl_basic_set *isl_basic_set_from_underlying_set(
2195 struct isl_basic_set *bset, struct isl_basic_set *like)
2197 return (struct isl_basic_set *)
2198 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2201 struct isl_set *isl_set_from_underlying_set(
2202 struct isl_set *set, struct isl_basic_set *like)
2208 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2210 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2211 isl_basic_set_free(like);
2214 set = isl_set_cow(set);
2217 for (i = 0; i < set->n; ++i) {
2218 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2219 isl_basic_set_copy(like));
2223 isl_dim_free(set->dim);
2224 set->dim = isl_dim_copy(like->dim);
2227 isl_basic_set_free(like);
2230 isl_basic_set_free(like);
2235 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2239 map = isl_map_cow(map);
2242 map->dim = isl_dim_cow(map->dim);
2246 for (i = 1; i < map->n; ++i)
2247 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2249 for (i = 0; i < map->n; ++i) {
2250 map->p[i] = (struct isl_basic_map *)
2251 isl_basic_map_underlying_set(map->p[i]);
2256 map->dim = isl_dim_underlying(map->dim, 0);
2258 isl_dim_free(map->dim);
2259 map->dim = isl_dim_copy(map->p[0]->dim);
2263 return (struct isl_set *)map;
2269 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2271 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2274 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2276 struct isl_basic_set *domain;
2280 n_out = isl_basic_map_n_out(bmap);
2281 domain = isl_basic_set_from_basic_map(bmap);
2282 return isl_basic_set_project_out(domain, n_out, 0);
2285 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2287 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2290 struct isl_set *isl_map_range(struct isl_map *map)
2293 struct isl_set *set;
2297 map = isl_map_cow(map);
2301 set = (struct isl_set *) map;
2302 if (set->dim->n_in != 0) {
2303 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2307 for (i = 0; i < map->n; ++i) {
2308 set->p[i] = isl_basic_map_range(map->p[i]);
2312 F_CLR(set, ISL_MAP_DISJOINT);
2313 F_CLR(set, ISL_SET_NORMALIZED);
2320 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2323 struct isl_map *map = NULL;
2325 set = isl_set_cow(set);
2328 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2329 map = (struct isl_map *)set;
2330 for (i = 0; i < set->n; ++i) {
2331 map->p[i] = isl_basic_map_from_basic_set(
2332 set->p[i], isl_dim_copy(dim));
2336 isl_dim_free(map->dim);
2345 struct isl_map *isl_map_from_range(struct isl_set *set)
2347 return (struct isl_map *)set;
2350 struct isl_set *isl_set_from_map(struct isl_map *map)
2353 struct isl_set *set = NULL;
2357 map = isl_map_cow(map);
2360 map->dim = isl_dim_cow(map->dim);
2363 map->dim->n_out += map->dim->n_in;
2365 set = (struct isl_set *)map;
2366 for (i = 0; i < map->n; ++i) {
2367 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2377 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2379 struct isl_map *map;
2383 isl_assert(dim->ctx, n >= 0, return NULL);
2384 map = isl_alloc(dim->ctx, struct isl_map,
2385 sizeof(struct isl_map) +
2386 n * sizeof(struct isl_basic_map *));
2390 map->ctx = dim->ctx;
2391 isl_ctx_ref(map->ctx);
2403 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2404 unsigned nparam, unsigned in, unsigned out, int n,
2407 struct isl_map *map;
2408 struct isl_dim *dims;
2410 dims = isl_dim_alloc(ctx, nparam, in, out);
2414 map = isl_map_alloc_dim(dims, n, flags);
2418 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2419 unsigned nparam, unsigned in, unsigned out)
2421 struct isl_basic_map *bmap;
2422 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2423 bmap = isl_basic_map_set_to_empty(bmap);
2427 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2429 struct isl_basic_set *bset;
2430 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2431 bset = isl_basic_set_set_to_empty(bset);
2435 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2437 struct isl_basic_map *bmap;
2440 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2441 bmap = isl_basic_map_set_to_empty(bmap);
2445 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2447 struct isl_basic_map *bmap;
2450 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2451 bmap = isl_basic_map_set_to_empty(bmap);
2455 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2457 struct isl_basic_set *bset;
2460 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2461 bset = isl_basic_set_set_to_empty(bset);
2465 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2467 struct isl_basic_map *bmap;
2468 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2472 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2474 struct isl_basic_set *bset;
2475 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2479 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2483 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2486 struct isl_map *isl_map_empty(struct isl_dim *dim)
2488 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2491 struct isl_map *isl_map_empty_like(struct isl_map *model)
2495 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2498 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2502 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2505 struct isl_set *isl_set_empty(struct isl_dim *dim)
2507 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2510 struct isl_set *isl_set_empty_like(struct isl_set *model)
2514 return isl_set_empty(isl_dim_copy(model->dim));
2517 struct isl_set *isl_set_universe(struct isl_dim *dim)
2519 struct isl_set *set;
2522 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2523 set = isl_set_add(set, isl_basic_set_universe(dim));
2527 struct isl_map *isl_map_dup(struct isl_map *map)
2530 struct isl_map *dup;
2534 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2535 for (i = 0; i < map->n; ++i)
2536 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2540 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2544 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2545 isl_assert(map->ctx, map->n < map->size, goto error);
2546 map->p[map->n] = bmap;
2548 F_CLR(map, ISL_MAP_NORMALIZED);
2554 isl_basic_map_free(bmap);
2558 void isl_map_free(struct isl_map *map)
2568 isl_ctx_deref(map->ctx);
2569 for (i = 0; i < map->n; ++i)
2570 isl_basic_map_free(map->p[i]);
2571 isl_dim_free(map->dim);
2575 struct isl_map *isl_map_extend(struct isl_map *base,
2576 unsigned nparam, unsigned n_in, unsigned n_out)
2580 base = isl_map_cow(base);
2584 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2587 for (i = 0; i < base->n; ++i) {
2588 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2589 isl_dim_copy(base->dim), 0, 0, 0);
2599 struct isl_set *isl_set_extend(struct isl_set *base,
2600 unsigned nparam, unsigned dim)
2602 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2606 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2607 unsigned pos, int value)
2611 bmap = isl_basic_map_cow(bmap);
2612 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2613 j = isl_basic_map_alloc_equality(bmap);
2616 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2617 isl_int_set_si(bmap->eq[j][pos], -1);
2618 isl_int_set_si(bmap->eq[j][0], value);
2619 bmap = isl_basic_map_simplify(bmap);
2620 return isl_basic_map_finalize(bmap);
2622 isl_basic_map_free(bmap);
2626 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2627 enum isl_dim_type type, unsigned pos, int value)
2631 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2632 return isl_basic_map_fix_pos(bmap, basic_map_offset(bmap, type) + pos,
2635 isl_basic_map_free(bmap);
2639 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2640 enum isl_dim_type type, unsigned pos, int value)
2642 return (struct isl_basic_set *)
2643 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2647 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2648 unsigned input, int value)
2650 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2653 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2654 unsigned dim, int value)
2656 return (struct isl_basic_set *)
2657 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2658 isl_dim_set, dim, value);
2661 struct isl_map *isl_map_fix_si(struct isl_map *map,
2662 enum isl_dim_type type, unsigned pos, int value)
2666 map = isl_map_cow(map);
2670 isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2671 for (i = 0; i < map->n; ++i) {
2672 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2676 F_CLR(map, ISL_MAP_NORMALIZED);
2683 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2684 unsigned input, int value)
2686 return isl_map_fix_si(map, isl_dim_in, input, value);
2689 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2691 return (struct isl_set *)
2692 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2695 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2696 unsigned dim, isl_int value)
2701 bset = isl_basic_set_cow(bset);
2702 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2703 j = isl_basic_set_alloc_inequality(bset);
2706 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2707 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2708 isl_int_neg(bset->ineq[j][0], value);
2709 bset = isl_basic_set_simplify(bset);
2710 return isl_basic_set_finalize(bset);
2712 isl_basic_set_free(bset);
2716 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2721 set = isl_set_cow(set);
2725 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2726 for (i = 0; i < set->n; ++i) {
2727 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2737 struct isl_map *isl_map_reverse(struct isl_map *map)
2742 map = isl_map_cow(map);
2746 map->dim = isl_dim_reverse(map->dim);
2749 for (i = 0; i < map->n; ++i) {
2750 map->p[i] = isl_basic_map_reverse(map->p[i]);
2754 F_CLR(map, ISL_MAP_NORMALIZED);
2761 struct isl_map *isl_basic_map_lexmax(
2762 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2763 struct isl_set **empty)
2765 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2768 struct isl_map *isl_basic_map_lexmin(
2769 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2770 struct isl_set **empty)
2772 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2775 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2777 struct isl_basic_map *bmap = NULL;
2778 struct isl_basic_set *dom = NULL;
2779 struct isl_map *min;
2780 struct isl_dim *param_dim;
2784 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2787 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2788 dom = isl_basic_set_universe(param_dim);
2791 min = isl_basic_map_lexmin(bmap, dom, NULL);
2792 return isl_map_range(min);
2794 isl_basic_map_free(bmap);
2798 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2805 off = isl_dim_total(bmap->dim);
2806 for (i = 0; i < bmap->n_div; ++i) {
2807 if (isl_int_is_zero(bmap->div[i][0]))
2808 return isl_pip_basic_map_compute_divs(bmap);
2809 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2812 return isl_map_from_basic_map(bmap);
2814 isl_basic_map_free(bmap);
2818 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2821 struct isl_map *res;
2827 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2828 for (i = 1 ; i < map->n; ++i) {
2830 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2831 if (F_ISSET(map, ISL_MAP_DISJOINT))
2832 res = isl_map_union_disjoint(res, r2);
2834 res = isl_map_union(res, r2);
2841 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2843 return (struct isl_set *)
2844 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2847 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2849 return (struct isl_set *)
2850 isl_map_compute_divs((struct isl_map *)set);
2853 struct isl_set *isl_map_domain(struct isl_map *map)
2856 struct isl_set *set;
2861 map = isl_map_cow(map);
2865 set = (struct isl_set *)map;
2866 set->dim = isl_dim_domain(set->dim);
2869 for (i = 0; i < map->n; ++i) {
2870 set->p[i] = isl_basic_map_domain(map->p[i]);
2874 F_CLR(set, ISL_MAP_DISJOINT);
2875 F_CLR(set, ISL_SET_NORMALIZED);
2882 struct isl_map *isl_map_union_disjoint(
2883 struct isl_map *map1, struct isl_map *map2)
2887 struct isl_map *map = NULL;
2901 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2903 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2904 F_ISSET(map2, ISL_MAP_DISJOINT))
2905 FL_SET(flags, ISL_MAP_DISJOINT);
2907 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2908 map1->n + map2->n, flags);
2911 for (i = 0; i < map1->n; ++i) {
2912 map = isl_map_add(map,
2913 isl_basic_map_copy(map1->p[i]));
2917 for (i = 0; i < map2->n; ++i) {
2918 map = isl_map_add(map,
2919 isl_basic_map_copy(map2->p[i]));
2933 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
2935 map1 = isl_map_union_disjoint(map1, map2);
2939 F_CLR(map1, ISL_MAP_DISJOINT);
2943 struct isl_set *isl_set_union_disjoint(
2944 struct isl_set *set1, struct isl_set *set2)
2946 return (struct isl_set *)
2947 isl_map_union_disjoint(
2948 (struct isl_map *)set1, (struct isl_map *)set2);
2951 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
2953 return (struct isl_set *)
2954 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
2957 struct isl_map *isl_map_intersect_range(
2958 struct isl_map *map, struct isl_set *set)
2961 struct isl_map *result;
2967 if (F_ISSET(map, ISL_MAP_DISJOINT) &&
2968 F_ISSET(set, ISL_MAP_DISJOINT))
2969 FL_SET(flags, ISL_MAP_DISJOINT);
2971 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
2972 map->n * set->n, flags);
2975 for (i = 0; i < map->n; ++i)
2976 for (j = 0; j < set->n; ++j) {
2977 result = isl_map_add(result,
2978 isl_basic_map_intersect_range(
2979 isl_basic_map_copy(map->p[i]),
2980 isl_basic_set_copy(set->p[j])));
2993 struct isl_map *isl_map_intersect_domain(
2994 struct isl_map *map, struct isl_set *set)
2996 return isl_map_reverse(
2997 isl_map_intersect_range(isl_map_reverse(map), set));
3000 struct isl_map *isl_map_apply_domain(
3001 struct isl_map *map1, struct isl_map *map2)
3005 map1 = isl_map_reverse(map1);
3006 map1 = isl_map_apply_range(map1, map2);
3007 return isl_map_reverse(map1);
3014 struct isl_map *isl_map_apply_range(
3015 struct isl_map *map1, struct isl_map *map2)
3017 struct isl_dim *dim_result;
3018 struct isl_map *result;
3027 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3028 isl_dim_copy(map2->dim));
3030 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3033 for (i = 0; i < map1->n; ++i)
3034 for (j = 0; j < map2->n; ++j) {
3035 result = isl_map_add(result,
3036 isl_basic_map_apply_range(
3037 isl_basic_map_copy(map1->p[i]),
3038 isl_basic_map_copy(map2->p[j])));
3044 if (result && result->n <= 1)
3045 F_SET(result, ISL_MAP_DISJOINT);
3054 * returns range - domain
3056 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3058 struct isl_basic_set *bset;
3065 dim = isl_basic_map_n_in(bmap);
3066 nparam = isl_basic_map_n_param(bmap);
3067 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3068 bset = isl_basic_set_from_basic_map(bmap);
3069 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3070 bset = isl_basic_set_swap_vars(bset, 2*dim);
3071 for (i = 0; i < dim; ++i) {
3072 int j = isl_basic_map_alloc_equality(
3073 (struct isl_basic_map *)bset);
3076 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3077 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3078 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3079 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3081 return isl_basic_set_project_out(bset, 2*dim, 0);
3083 isl_basic_map_free(bmap);
3088 * returns range - domain
3090 struct isl_set *isl_map_deltas(struct isl_map *map)
3093 struct isl_set *result;
3098 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3099 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3100 isl_map_n_in(map), map->n, map->flags);
3103 for (i = 0; i < map->n; ++i)
3104 result = isl_set_add(result,
3105 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3113 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3115 struct isl_basic_map *bmap;
3123 nparam = dims->nparam;
3125 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3129 for (i = 0; i < dim; ++i) {
3130 int j = isl_basic_map_alloc_equality(bmap);
3133 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3134 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3135 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3137 return isl_basic_map_finalize(bmap);
3139 isl_basic_map_free(bmap);
3143 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3145 struct isl_dim *dim = isl_dim_map(set_dim);
3148 return basic_map_identity(dim);
3151 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3153 if (!model || !model->dim)
3155 isl_assert(model->ctx,
3156 model->dim->n_in == model->dim->n_out, return NULL);
3157 return basic_map_identity(isl_dim_copy(model->dim));
3160 static struct isl_map *map_identity(struct isl_dim *dim)
3162 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3163 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3166 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3168 struct isl_dim *dim = isl_dim_map(set_dim);
3171 return map_identity(dim);
3174 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3176 if (!model || !model->dim)
3178 isl_assert(model->ctx,
3179 model->dim->n_in == model->dim->n_out, return NULL);
3180 return map_identity(isl_dim_copy(model->dim));
3183 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3185 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3188 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3190 return isl_map_is_subset(
3191 (struct isl_map *)set1, (struct isl_map *)set2);
3194 int isl_basic_map_is_subset(
3195 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3198 struct isl_map *map1;
3199 struct isl_map *map2;
3201 if (!bmap1 || !bmap2)
3204 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3205 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3207 is_subset = isl_map_is_subset(map1, map2);
3215 int isl_basic_map_is_equal(
3216 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3220 if (!bmap1 || !bmap2)
3222 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3225 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3229 int isl_basic_set_is_equal(
3230 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3232 return isl_basic_map_is_equal(
3233 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3236 int isl_map_is_empty(struct isl_map *map)
3243 for (i = 0; i < map->n; ++i) {
3244 is_empty = isl_basic_map_is_empty(map->p[i]);
3253 int isl_map_fast_is_empty(struct isl_map *map)
3258 int isl_set_is_empty(struct isl_set *set)
3260 return isl_map_is_empty((struct isl_map *)set);
3263 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3267 struct isl_map *diff;
3272 if (isl_map_is_empty(map1))
3275 if (isl_map_is_empty(map2))
3278 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3282 is_subset = isl_map_is_empty(diff);
3288 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3294 is_subset = isl_map_is_subset(map1, map2);
3297 is_subset = isl_map_is_subset(map2, map1);
3301 int isl_basic_map_is_strict_subset(
3302 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3306 if (!bmap1 || !bmap2)
3308 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3311 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3312 if (is_subset == -1)
3317 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3323 total = 1 + isl_basic_map_total_dim(bmap);
3324 if (total != vec->size)
3329 for (i = 0; i < bmap->n_eq; ++i) {
3330 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3331 if (!isl_int_is_zero(s)) {
3337 for (i = 0; i < bmap->n_ineq; ++i) {
3338 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3339 if (isl_int_is_neg(s)) {
3350 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3354 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3357 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3359 struct isl_basic_set *bset = NULL;
3360 struct isl_vec *sample = NULL;
3367 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3370 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3371 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3372 copy = isl_basic_map_convex_hull(copy);
3373 empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3374 isl_basic_map_free(copy);
3378 total = 1 + isl_basic_map_total_dim(bmap);
3379 if (bmap->sample && bmap->sample->size == total) {
3380 int contains = basic_map_contains(bmap, bmap->sample);
3386 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3389 sample = isl_basic_set_sample(bset);
3392 empty = sample->size == 0;
3394 isl_vec_free(bmap->ctx, bmap->sample);
3395 bmap->sample = sample;
3400 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3402 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3405 struct isl_map *isl_basic_map_union(
3406 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3408 struct isl_map *map;
3409 if (!bmap1 || !bmap2)
3412 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3414 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3417 map = isl_map_add(map, bmap1);
3418 map = isl_map_add(map, bmap2);
3421 isl_basic_map_free(bmap1);
3422 isl_basic_map_free(bmap2);
3426 struct isl_set *isl_basic_set_union(
3427 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3429 return (struct isl_set *)isl_basic_map_union(
3430 (struct isl_basic_map *)bset1,
3431 (struct isl_basic_map *)bset2);
3434 /* Order divs such that any div only depends on previous divs */
3435 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3438 unsigned off = isl_dim_total(bmap->dim);
3440 for (i = 0; i < bmap->n_div; ++i) {
3442 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3446 swap_div(bmap, i, pos);
3452 /* Look for a div in dst that corresponds to the div "div" in src.
3453 * The divs before "div" in src and dst are assumed to be the same.
3455 * Returns -1 if no corresponding div was found and the position
3456 * of the corresponding div in dst otherwise.
3458 static int find_div(struct isl_basic_map *dst,
3459 struct isl_basic_map *src, unsigned div)
3463 unsigned total = isl_dim_total(src->dim);
3465 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3466 for (i = div; i < dst->n_div; ++i)
3467 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3468 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3469 dst->n_div - div) == -1)
3474 struct isl_basic_map *isl_basic_map_align_divs(
3475 struct isl_basic_map *dst, struct isl_basic_map *src)
3478 unsigned total = isl_dim_total(src->dim);
3483 if (src->n_div == 0)
3486 for (i = 0; i < src->n_div; ++i)
3487 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3489 src = order_divs(src);
3490 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3491 src->n_div, 0, 2 * src->n_div);
3494 for (i = 0; i < src->n_div; ++i) {
3495 int j = find_div(dst, src, i);
3497 j = isl_basic_map_alloc_div(dst);
3500 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3501 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3502 if (add_div_constraints(dst, j) < 0)
3506 swap_div(dst, i, j);
3510 isl_basic_map_free(dst);
3514 struct isl_basic_set *isl_basic_set_align_divs(
3515 struct isl_basic_set *dst, struct isl_basic_set *src)
3517 return (struct isl_basic_set *)isl_basic_map_align_divs(
3518 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3521 struct isl_map *isl_map_align_divs(struct isl_map *map)
3525 map = isl_map_compute_divs(map);
3526 map = isl_map_cow(map);
3530 for (i = 1; i < map->n; ++i)
3531 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3532 for (i = 1; i < map->n; ++i)
3533 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3535 F_CLR(map, ISL_MAP_NORMALIZED);
3539 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3540 struct isl_basic_map *src, isl_int *c,
3541 unsigned len, int oppose)
3543 struct isl_basic_map *copy = NULL;
3548 copy = isl_basic_map_copy(src);
3549 copy = isl_basic_map_cow(copy);
3552 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3553 k = isl_basic_map_alloc_inequality(copy);
3557 isl_seq_neg(copy->ineq[k], c, len);
3559 isl_seq_cpy(copy->ineq[k], c, len);
3560 total = 1 + isl_basic_map_total_dim(copy);
3561 isl_seq_clr(copy->ineq[k]+len, total - len);
3562 isl_inequality_negate(copy, k);
3563 copy = isl_basic_map_simplify(copy);
3564 copy = isl_basic_map_finalize(copy);
3565 is_empty = isl_basic_map_is_empty(copy);
3569 dst = isl_map_add(dst, copy);
3571 isl_basic_map_free(copy);
3574 isl_basic_map_free(copy);
3579 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3583 struct isl_map *rest = NULL;
3585 unsigned total = isl_basic_map_total_dim(bmap);
3592 if (F_ISSET(map, ISL_MAP_DISJOINT))
3593 FL_SET(flags, ISL_MAP_DISJOINT);
3595 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3596 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3600 for (i = 0; i < map->n; ++i) {
3601 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3606 for (j = 0; j < map->n; ++j)
3607 map->p[j] = isl_basic_map_cow(map->p[j]);
3609 for (i = 0; i < bmap->n_eq; ++i) {
3610 for (j = 0; j < map->n; ++j) {
3611 rest = add_cut_constraint(rest,
3612 map->p[j], bmap->eq[i], 1+total, 0);
3616 rest = add_cut_constraint(rest,
3617 map->p[j], bmap->eq[i], 1+total, 1);
3621 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3625 k = isl_basic_map_alloc_equality(map->p[j]);
3628 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3629 isl_seq_clr(map->p[j]->eq[k]+1+total,
3630 map->p[j]->n_div - bmap->n_div);
3634 for (i = 0; i < bmap->n_ineq; ++i) {
3635 for (j = 0; j < map->n; ++j) {
3636 rest = add_cut_constraint(rest,
3637 map->p[j], bmap->ineq[i], 1+total, 0);
3641 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3645 k = isl_basic_map_alloc_inequality(map->p[j]);
3648 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3649 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3650 map->p[j]->n_div - bmap->n_div);
3662 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3668 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3670 if (isl_map_is_empty(map2)) {
3675 map1 = isl_map_compute_divs(map1);
3676 map2 = isl_map_compute_divs(map2);
3680 for (i = 0; map1 && i < map2->n; ++i)
3681 map1 = subtract(map1, map2->p[i]);
3691 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3693 return (struct isl_set *)
3695 (struct isl_map *)set1, (struct isl_map *)set2);
3698 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3702 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3703 map = isl_map_intersect_domain(map, set);
3704 set = isl_map_range(map);
3712 /* There is no need to cow as removing empty parts doesn't change
3713 * the meaning of the set.
3715 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3722 for (i = map->n-1; i >= 0; --i) {
3723 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3725 isl_basic_map_free(map->p[i]);
3726 if (i != map->n-1) {
3727 F_CLR(map, ISL_MAP_NORMALIZED);
3728 map->p[i] = map->p[map->n-1];
3736 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3738 return (struct isl_set *)
3739 isl_map_remove_empty_parts((struct isl_map *)set);
3742 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
3744 struct isl_basic_map *bmap;
3745 if (!map || map->n == 0)
3747 bmap = map->p[map->n-1];
3748 isl_assert(map->ctx, F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
3749 return isl_basic_map_copy(bmap);
3752 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3754 (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
3757 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
3758 struct isl_basic_map *bmap)
3764 for (i = map->n-1; i >= 0; --i) {
3765 if (map->p[i] != bmap)
3767 map = isl_map_cow(map);
3770 isl_basic_map_free(map->p[i]);
3771 if (i != map->n-1) {
3772 F_CLR(map, ISL_SET_NORMALIZED);
3773 map->p[i] = map->p[map->n-1];
3778 isl_basic_map_free(bmap);
3782 isl_basic_map_free(bmap);
3786 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3787 struct isl_basic_set *bset)
3789 (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
3790 (struct isl_basic_map *)bset);
3793 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3794 * for any common value of the parameters and dimensions preceding dim
3795 * in both basic sets, the values of dimension pos in bset1 are
3796 * smaller or larger than those in bset2.
3799 * 1 if bset1 follows bset2
3800 * -1 if bset1 precedes bset2
3801 * 0 if bset1 and bset2 are incomparable
3802 * -2 if some error occurred.
3804 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3805 struct isl_basic_set *bset2, int pos)
3807 struct isl_dim *dims;
3808 struct isl_basic_map *bmap1 = NULL;
3809 struct isl_basic_map *bmap2 = NULL;
3810 struct isl_ctx *ctx;
3811 struct isl_vec *obj;
3814 unsigned dim1, dim2;
3816 enum isl_lp_result res;
3819 if (!bset1 || !bset2)
3822 nparam = isl_basic_set_n_param(bset1);
3823 dim1 = isl_basic_set_n_dim(bset1);
3824 dim2 = isl_basic_set_n_dim(bset2);
3825 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3826 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3827 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3828 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3829 if (!bmap1 || !bmap2)
3831 bmap1 = isl_basic_map_extend(bmap1, nparam,
3832 pos, (dim1 - pos) + (dim2 - pos),
3833 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3834 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
3837 total = isl_basic_map_total_dim(bmap1);
3839 obj = isl_vec_alloc(ctx, total);
3840 isl_seq_clr(obj->block.data, total);
3841 isl_int_set_si(obj->block.data[nparam+pos], 1);
3842 isl_int_set_si(obj->block.data[nparam+pos+(dim1-pos)], -1);
3847 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3848 if (res == isl_lp_empty)
3850 else if (res == isl_lp_ok && isl_int_is_pos(num))
3852 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3853 res == isl_lp_unbounded)
3859 isl_basic_map_free(bmap1);
3860 isl_vec_free(ctx, obj);
3863 isl_basic_map_free(bmap1);
3864 isl_basic_map_free(bmap2);
3868 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
3869 unsigned pos, isl_int *val)
3877 total = isl_basic_map_total_dim(bmap);
3878 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
3879 for (; d+1 > pos; --d)
3880 if (!isl_int_is_zero(bmap->eq[i][1+d]))
3884 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
3886 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
3888 if (!isl_int_is_one(bmap->eq[i][1+d]))
3891 isl_int_neg(*val, bmap->eq[i][0]);
3897 static int isl_map_fast_has_fixed_var(struct isl_map *map,
3898 unsigned pos, isl_int *val)
3910 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
3913 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
3914 for (i = 1; fixed == 1 && i < map->n; ++i) {
3915 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
3916 if (fixed == 1 && isl_int_ne(tmp, v))
3920 isl_int_set(*val, v);
3926 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
3929 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
3932 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
3933 enum isl_dim_type type, unsigned pos, isl_int *val)
3935 if (pos >= isl_basic_map_dim(bmap, type))
3937 return isl_basic_map_fast_has_fixed_var(bmap,
3938 basic_map_offset(bmap, type) - 1 + pos, val);
3941 /* Check if dimension dim has fixed value and if so and if val is not NULL,
3942 * then return this fixed value in *val.
3944 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
3946 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
3949 /* Check if input variable in has fixed value and if so and if val is not NULL,
3950 * then return this fixed value in *val.
3952 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
3954 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
3957 /* Check if dimension dim has an (obvious) fixed lower bound and if so
3958 * and if val is not NULL, then return this lower bound in *val.
3960 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
3961 unsigned dim, isl_int *val)
3963 int i, i_eq = -1, i_ineq = -1;
3970 total = isl_basic_set_total_dim(bset);
3971 nparam = isl_basic_set_n_param(bset);
3972 for (i = 0; i < bset->n_eq; ++i) {
3973 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
3979 for (i = 0; i < bset->n_ineq; ++i) {
3980 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
3982 if (i_eq != -1 || i_ineq != -1)
3986 if (i_eq == -1 && i_ineq == -1)
3988 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
3989 /* The coefficient should always be one due to normalization. */
3990 if (!isl_int_is_one(c[1+nparam+dim]))
3992 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
3994 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
3995 total - nparam - dim - 1) != -1)
3998 isl_int_neg(*val, c[0]);
4002 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4003 unsigned dim, isl_int *val)
4015 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4019 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4021 for (i = 1; fixed == 1 && i < set->n; ++i) {
4022 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4024 if (fixed == 1 && isl_int_ne(tmp, v))
4028 isl_int_set(*val, v);
4039 static int qsort_constraint_cmp(const void *p1, const void *p2)
4041 const struct constraint *c1 = (const struct constraint *)p1;
4042 const struct constraint *c2 = (const struct constraint *)p2;
4043 unsigned size = isl_min(c1->size, c2->size);
4044 return isl_seq_cmp(c1->c, c2->c, size);
4047 static struct isl_basic_map *isl_basic_map_sort_constraints(
4048 struct isl_basic_map *bmap)
4051 struct constraint *c;
4056 total = isl_basic_map_total_dim(bmap);
4057 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4060 for (i = 0; i < bmap->n_ineq; ++i) {
4062 c[i].c = bmap->ineq[i];
4064 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4065 for (i = 0; i < bmap->n_ineq; ++i)
4066 bmap->ineq[i] = c[i].c;
4070 isl_basic_map_free(bmap);
4074 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4078 if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4080 bmap = isl_basic_map_convex_hull(bmap);
4081 bmap = isl_basic_map_sort_constraints(bmap);
4082 F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4086 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4088 return (struct isl_basic_set *)isl_basic_map_normalize(
4089 (struct isl_basic_map *)bset);
4092 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4093 const struct isl_basic_map *bmap2)
4100 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4101 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4102 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4103 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4104 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4105 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4106 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4107 F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4109 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4111 if (F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4113 if (bmap1->n_eq != bmap2->n_eq)
4114 return bmap1->n_eq - bmap2->n_eq;
4115 if (bmap1->n_ineq != bmap2->n_ineq)
4116 return bmap1->n_ineq - bmap2->n_ineq;
4117 if (bmap1->n_div != bmap2->n_div)
4118 return bmap1->n_div - bmap2->n_div;
4119 total = isl_basic_map_total_dim(bmap1);
4120 for (i = 0; i < bmap1->n_eq; ++i) {
4121 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4125 for (i = 0; i < bmap1->n_ineq; ++i) {
4126 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4130 for (i = 0; i < bmap1->n_div; ++i) {
4131 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4138 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4139 struct isl_basic_map *bmap2)
4141 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4144 static int qsort_bmap_cmp(const void *p1, const void *p2)
4146 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4147 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4149 return isl_basic_map_fast_cmp(bmap1, bmap2);
4152 /* We normalize in place, but if anything goes wrong we need
4153 * to return NULL, so we need to make sure we don't change the
4154 * meaning of any possible other copies of map.
4156 struct isl_map *isl_map_normalize(struct isl_map *map)
4159 struct isl_basic_map *bmap;
4163 if (F_ISSET(map, ISL_MAP_NORMALIZED))
4165 for (i = 0; i < map->n; ++i) {
4166 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4169 isl_basic_map_free(map->p[i]);
4172 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4173 F_SET(map, ISL_MAP_NORMALIZED);
4174 map = isl_map_remove_empty_parts(map);
4177 for (i = map->n - 1; i >= 1; --i) {
4178 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4180 isl_basic_map_free(map->p[i-1]);
4181 for (j = i; j < map->n; ++j)
4182 map->p[j-1] = map->p[j];
4192 struct isl_set *isl_set_normalize(struct isl_set *set)
4194 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4197 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4207 if (!isl_dim_equal(map1->dim, map2->dim))
4210 map1 = isl_map_copy(map1);
4211 map2 = isl_map_copy(map2);
4212 map1 = isl_map_normalize(map1);
4213 map2 = isl_map_normalize(map2);
4216 equal = map1->n == map2->n;
4217 for (i = 0; equal && i < map1->n; ++i) {
4218 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4231 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4233 return isl_map_fast_is_equal((struct isl_map *)set1,
4234 (struct isl_map *)set2);
4237 /* Return an interval that ranges from min to max (inclusive)
4239 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4240 isl_int min, isl_int max)
4243 struct isl_basic_set *bset = NULL;
4245 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4249 k = isl_basic_set_alloc_inequality(bset);
4252 isl_int_set_si(bset->ineq[k][1], 1);
4253 isl_int_neg(bset->ineq[k][0], min);
4255 k = isl_basic_set_alloc_inequality(bset);
4258 isl_int_set_si(bset->ineq[k][1], -1);
4259 isl_int_set(bset->ineq[k][0], max);
4263 isl_basic_set_free(bset);
4267 /* Return the Cartesian product of the basic sets in list (in the given order).
4269 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4277 struct isl_basic_set *product = NULL;
4281 isl_assert(list->ctx, list->n > 0, goto error);
4282 isl_assert(list->ctx, list->p[0], goto error);
4283 nparam = isl_basic_set_n_param(list->p[0]);
4284 dim = isl_basic_set_n_dim(list->p[0]);
4285 extra = list->p[0]->n_div;
4286 n_eq = list->p[0]->n_eq;
4287 n_ineq = list->p[0]->n_ineq;
4288 for (i = 1; i < list->n; ++i) {
4289 isl_assert(list->ctx, list->p[i], goto error);
4290 isl_assert(list->ctx,
4291 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4292 dim += isl_basic_set_n_dim(list->p[i]);
4293 extra += list->p[i]->n_div;
4294 n_eq += list->p[i]->n_eq;
4295 n_ineq += list->p[i]->n_ineq;
4297 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4302 for (i = 0; i < list->n; ++i) {
4303 isl_basic_set_add_constraints(product,
4304 isl_basic_set_copy(list->p[i]), dim);
4305 dim += isl_basic_set_n_dim(list->p[i]);
4307 isl_basic_set_list_free(list);
4310 isl_basic_set_free(product);
4311 isl_basic_set_list_free(list);
4315 struct isl_basic_map *isl_basic_map_product(
4316 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4318 struct isl_dim *dim_result = NULL;
4319 struct isl_basic_map *bmap;
4320 unsigned in1, in2, out1, out2, nparam, total, pos;
4321 struct isl_dim_map *dim_map1, *dim_map2;
4323 if (!bmap1 || !bmap2)
4326 isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4327 bmap2->dim, isl_dim_param), goto error);
4328 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4329 isl_dim_copy(bmap2->dim));
4331 in1 = isl_basic_map_n_in(bmap1);
4332 in2 = isl_basic_map_n_in(bmap2);
4333 out1 = isl_basic_map_n_out(bmap1);
4334 out2 = isl_basic_map_n_out(bmap2);
4335 nparam = isl_basic_map_n_param(bmap1);
4337 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4338 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4339 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4340 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4341 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4342 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4343 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4344 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4345 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4346 isl_dim_map_div(dim_map1, bmap1, pos += out2);
4347 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4349 bmap = isl_basic_map_alloc_dim(dim_result,
4350 bmap1->n_div + bmap2->n_div,
4351 bmap1->n_eq + bmap2->n_eq,
4352 bmap1->n_ineq + bmap2->n_ineq);
4353 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4354 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4355 bmap = isl_basic_map_simplify(bmap);
4356 return isl_basic_map_finalize(bmap);
4358 isl_basic_map_free(bmap1);
4359 isl_basic_map_free(bmap2);
4363 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4365 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4368 struct isl_map *result;
4374 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4375 map2->dim, isl_dim_param), goto error);
4377 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
4378 F_ISSET(map2, ISL_MAP_DISJOINT))
4379 FL_SET(flags, ISL_MAP_DISJOINT);
4381 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4382 isl_dim_copy(map2->dim)),
4383 map1->n * map2->n, flags);
4386 for (i = 0; i < map1->n; ++i)
4387 for (j = 0; j < map2->n; ++j) {
4388 struct isl_basic_map *part;
4389 part = isl_basic_map_product(
4390 isl_basic_map_copy(map1->p[i]),
4391 isl_basic_map_copy(map2->p[j]));
4392 if (isl_basic_map_is_empty(part))
4393 isl_basic_map_free(part);
4395 result = isl_map_add(result, part);
4408 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4416 bset = isl_basic_set_copy(bset);
4417 bset = isl_basic_set_normalize(bset);
4420 total = isl_basic_set_total_dim(bset);
4421 isl_hash_byte(hash, bset->n_eq & 0xFF);
4422 for (i = 0; i < bset->n_eq; ++i) {
4424 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4425 isl_hash_hash(hash, c_hash);
4427 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4428 for (i = 0; i < bset->n_ineq; ++i) {
4430 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4431 isl_hash_hash(hash, c_hash);
4433 isl_hash_byte(hash, bset->n_div & 0xFF);
4434 for (i = 0; i < bset->n_div; ++i) {
4436 if (isl_int_is_zero(bset->div[i][0]))
4438 isl_hash_byte(hash, i & 0xFF);
4439 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4440 isl_hash_hash(hash, c_hash);
4442 isl_basic_set_free(bset);
4446 uint32_t isl_set_get_hash(struct isl_set *set)
4453 set = isl_set_copy(set);
4454 set = isl_set_normalize(set);
4458 hash = isl_hash_init();
4459 for (i = 0; i < set->n; ++i) {
4461 bset_hash = isl_basic_set_get_hash(set->p[i]);
4462 isl_hash_hash(hash, bset_hash);
4470 /* Check if the value for dimension dim is completely determined
4471 * by the values of the other parameters and variables.
4472 * That is, check if dimension dim is involved in an equality.
4474 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4481 nparam = isl_basic_set_n_param(bset);
4482 for (i = 0; i < bset->n_eq; ++i)
4483 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4488 /* Check if the value for dimension dim is completely determined
4489 * by the values of the other parameters and variables.
4490 * That is, check if dimension dim is involved in an equality
4491 * for each of the subsets.
4493 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4499 for (i = 0; i < set->n; ++i) {
4501 unique = isl_basic_set_dim_is_unique(set->p[i], dim);