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;
97 case isl_dim_param: return dim->nparam;
98 case isl_dim_in: return dim->n_in;
99 case isl_dim_out: return dim->n_out;
100 case isl_dim_div: return bmap->n_div;
101 case isl_dim_all: return isl_basic_map_total_dim(bmap);
105 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
106 enum isl_dim_type type)
108 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
111 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
113 return bset->dim->n_out;
116 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
118 return bset->dim->nparam;
121 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
123 return isl_dim_total(bset->dim) + bset->n_div;
126 unsigned isl_set_n_dim(const struct isl_set *set)
128 return set->dim->n_out;
131 unsigned isl_set_n_param(const struct isl_set *set)
133 return set->dim->nparam;
136 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
138 return bmap->dim->n_in;
141 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
143 return bmap->dim->n_out;
146 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
148 return bmap->dim->nparam;
151 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
156 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
158 return isl_dim_total(bmap->dim) + bmap->n_div;
161 unsigned isl_map_n_in(const struct isl_map *map)
163 return map->dim->n_in;
166 unsigned isl_map_n_out(const struct isl_map *map)
168 return map->dim->n_out;
171 unsigned isl_map_n_param(const struct isl_map *map)
173 return map->dim->nparam;
176 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
178 return map->dim->n_in == set->dim->n_out &&
179 map->dim->nparam == set->dim->nparam;
182 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
183 struct isl_basic_set *bset)
185 return bmap->dim->n_in == bset->dim->n_out &&
186 bmap->dim->nparam == bset->dim->nparam;
189 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
190 struct isl_basic_set *bset)
192 return bmap->dim->n_out == bset->dim->n_out &&
193 bmap->dim->nparam == bset->dim->nparam;
196 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
197 struct isl_basic_map *bmap, unsigned extra,
198 unsigned n_eq, unsigned n_ineq)
201 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
203 bmap->block = isl_blk_alloc(ctx, (n_eq + n_ineq) * row_size);
204 if (isl_blk_is_error(bmap->block)) {
209 bmap->eq = isl_alloc_array(ctx, isl_int *, n_eq + n_ineq);
211 isl_blk_free(ctx, bmap->block);
217 bmap->block2 = isl_blk_empty();
220 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
221 if (isl_blk_is_error(bmap->block2)) {
223 isl_blk_free(ctx, bmap->block);
228 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
230 isl_blk_free(ctx, bmap->block2);
232 isl_blk_free(ctx, bmap->block);
238 for (i = 0; i < n_eq + n_ineq; ++i)
239 bmap->eq[i] = bmap->block.data + i * row_size;
241 for (i = 0; i < extra; ++i)
242 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
248 bmap->c_size = n_eq + n_ineq;
249 bmap->ineq = bmap->eq + n_eq;
258 isl_basic_map_free(bmap);
262 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
263 unsigned nparam, unsigned dim, unsigned extra,
264 unsigned n_eq, unsigned n_ineq)
266 struct isl_basic_map *bmap;
267 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
268 return (struct isl_basic_set *)bmap;
271 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
272 unsigned extra, unsigned n_eq, unsigned n_ineq)
274 struct isl_basic_map *bmap;
277 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
278 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
279 return (struct isl_basic_set *)bmap;
282 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
283 unsigned extra, unsigned n_eq, unsigned n_ineq)
285 struct isl_basic_map *bmap;
289 bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
294 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
300 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
301 unsigned nparam, unsigned in, unsigned out, unsigned extra,
302 unsigned n_eq, unsigned n_ineq)
304 struct isl_basic_map *bmap;
307 dim = isl_dim_alloc(ctx, nparam, in, out);
311 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
315 static void dup_constraints(
316 struct isl_basic_map *dst, struct isl_basic_map *src)
319 unsigned total = isl_basic_map_total_dim(src);
321 for (i = 0; i < src->n_eq; ++i) {
322 int j = isl_basic_map_alloc_equality(dst);
323 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
326 for (i = 0; i < src->n_ineq; ++i) {
327 int j = isl_basic_map_alloc_inequality(dst);
328 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
331 for (i = 0; i < src->n_div; ++i) {
332 int j = isl_basic_map_alloc_div(dst);
333 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
335 F_SET(dst, ISL_BASIC_SET_FINAL);
338 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
340 struct isl_basic_map *dup;
344 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
345 bmap->n_div, bmap->n_eq, bmap->n_ineq);
348 dup->flags = bmap->flags;
349 dup_constraints(dup, bmap);
350 dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
354 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
356 struct isl_basic_map *dup;
358 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
359 return (struct isl_basic_set *)dup;
362 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
367 if (F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
371 return isl_basic_set_dup(bset);
374 struct isl_set *isl_set_copy(struct isl_set *set)
383 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
388 if (F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
392 return isl_basic_map_dup(bmap);
395 struct isl_map *isl_map_copy(struct isl_map *map)
404 void isl_basic_map_free(struct isl_basic_map *bmap)
412 isl_ctx_deref(bmap->ctx);
414 isl_blk_free(bmap->ctx, bmap->block2);
416 isl_blk_free(bmap->ctx, bmap->block);
417 isl_vec_free(bmap->ctx, bmap->sample);
418 isl_dim_free(bmap->dim);
422 void isl_basic_set_free(struct isl_basic_set *bset)
424 isl_basic_map_free((struct isl_basic_map *)bset);
427 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
433 isl_assert(ctx, bmap->n_eq + bmap->n_ineq < bmap->c_size, return -1);
434 isl_assert(ctx, bmap->eq + bmap->n_eq <= bmap->ineq, return -1);
435 if (bmap->eq + bmap->n_eq == bmap->ineq) {
437 int j = isl_basic_map_alloc_inequality(bmap);
441 bmap->ineq[0] = bmap->ineq[j];
446 isl_seq_clr(bmap->eq[bmap->n_eq] +
447 1 + isl_basic_map_total_dim(bmap),
448 bmap->extra - bmap->n_div);
452 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
454 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
457 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
461 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
466 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
471 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
473 if (pos != bmap->n_eq - 1) {
475 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
476 bmap->eq[bmap->n_eq - 1] = t;
482 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
484 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
487 void isl_basic_map_inequality_to_equality(
488 struct isl_basic_map *bmap, unsigned pos)
493 bmap->ineq[pos] = bmap->ineq[0];
494 bmap->ineq[0] = bmap->eq[bmap->n_eq];
495 bmap->eq[bmap->n_eq] = t;
499 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
502 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
508 isl_assert(ctx, (bmap->ineq - bmap->eq) + bmap->n_ineq < bmap->c_size,
510 F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
511 F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
512 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
513 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
514 1 + isl_basic_map_total_dim(bmap),
515 bmap->extra - bmap->n_div);
516 return bmap->n_ineq++;
519 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
521 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
524 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
528 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
533 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
535 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
538 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
543 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
545 if (pos != bmap->n_ineq - 1) {
547 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
548 bmap->ineq[bmap->n_ineq - 1] = t;
549 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
555 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
557 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
560 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
564 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
565 isl_seq_clr(bmap->div[bmap->n_div] +
566 1 + 1 + isl_basic_map_total_dim(bmap),
567 bmap->extra - bmap->n_div);
568 return bmap->n_div++;
571 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
575 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
580 /* Copy constraint from src to dst, putting the vars of src at offset
581 * dim_off in dst and the divs of src at offset div_off in dst.
582 * If both sets are actually map, then dim_off applies to the input
585 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
586 struct isl_basic_map *src_map, isl_int *src,
587 unsigned in_off, unsigned out_off, unsigned div_off)
589 unsigned src_nparam = isl_basic_map_n_param(src_map);
590 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
591 unsigned src_in = isl_basic_map_n_in(src_map);
592 unsigned dst_in = isl_basic_map_n_in(dst_map);
593 unsigned src_out = isl_basic_map_n_out(src_map);
594 unsigned dst_out = isl_basic_map_n_out(dst_map);
595 isl_int_set(dst[0], src[0]);
596 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
597 if (dst_nparam > src_nparam)
598 isl_seq_clr(dst+1+src_nparam,
599 dst_nparam - src_nparam);
600 isl_seq_clr(dst+1+dst_nparam, in_off);
601 isl_seq_cpy(dst+1+dst_nparam+in_off,
603 isl_min(dst_in-in_off, src_in));
604 if (dst_in-in_off > src_in)
605 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
606 dst_in - in_off - src_in);
607 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
608 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
609 src+1+src_nparam+src_in,
610 isl_min(dst_out-out_off, src_out));
611 if (dst_out-out_off > src_out)
612 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
613 dst_out - out_off - src_out);
614 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
615 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
616 src+1+src_nparam+src_in+src_out,
617 isl_min(dst_map->extra-div_off, src_map->n_div));
618 if (dst_map->n_div-div_off > src_map->n_div)
619 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
620 div_off+src_map->n_div,
621 dst_map->n_div - div_off - src_map->n_div);
624 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
625 struct isl_basic_map *src_map, isl_int *src,
626 unsigned in_off, unsigned out_off, unsigned div_off)
628 isl_int_set(dst[0], src[0]);
629 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
632 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
633 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
638 if (!bmap1 || !bmap2)
641 div_off = bmap1->n_div;
643 for (i = 0; i < bmap2->n_eq; ++i) {
644 int i1 = isl_basic_map_alloc_equality(bmap1);
647 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
648 i_pos, o_pos, div_off);
651 for (i = 0; i < bmap2->n_ineq; ++i) {
652 int i1 = isl_basic_map_alloc_inequality(bmap1);
655 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
656 i_pos, o_pos, div_off);
659 for (i = 0; i < bmap2->n_div; ++i) {
660 int i1 = isl_basic_map_alloc_div(bmap1);
663 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
664 i_pos, o_pos, div_off);
667 isl_basic_map_free(bmap2);
672 isl_basic_map_free(bmap1);
673 isl_basic_map_free(bmap2);
677 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
678 struct isl_dim_map *dim_map)
682 for (i = 0; i < dim_map->len; ++i) {
683 if (dim_map->pos[i] < 0)
684 isl_int_set_si(dst[i], 0);
686 isl_int_set(dst[i], src[dim_map->pos[i]]);
690 static void copy_div_dim_map(isl_int *dst, isl_int *src,
691 struct isl_dim_map *dim_map)
693 isl_int_set(dst[0], src[0]);
694 copy_constraint_dim_map(dst+1, src+1, dim_map);
697 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
698 struct isl_basic_map *src, struct isl_dim_map *dim_map)
702 if (!src || !dst || !dim_map)
705 for (i = 0; i < src->n_eq; ++i) {
706 int i1 = isl_basic_map_alloc_equality(dst);
709 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
712 for (i = 0; i < src->n_ineq; ++i) {
713 int i1 = isl_basic_map_alloc_inequality(dst);
716 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
719 for (i = 0; i < src->n_div; ++i) {
720 int i1 = isl_basic_map_alloc_div(dst);
723 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
727 isl_basic_map_free(src);
732 isl_basic_map_free(src);
733 isl_basic_map_free(dst);
737 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
738 struct isl_basic_set *bset2, unsigned pos)
740 return (struct isl_basic_set *)
741 add_constraints((struct isl_basic_map *)bset1,
742 (struct isl_basic_map *)bset2, 0, pos);
745 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
746 struct isl_dim *dim, unsigned extra,
747 unsigned n_eq, unsigned n_ineq)
749 struct isl_basic_map *ext;
756 base = isl_basic_map_cow(base);
760 dims_ok = isl_dim_equal(base->dim, dim) &&
761 base->extra >= base->n_div + extra;
763 if (dims_ok && n_eq == 0 && n_ineq == 0) {
768 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
769 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
770 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
771 extra += base->extra;
773 n_ineq += base->n_ineq;
775 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
781 ext = add_constraints(ext, base, 0, 0);
784 F_CLR(ext, ISL_BASIC_SET_FINAL);
791 isl_basic_map_free(base);
795 struct isl_basic_map *isl_basic_map_extend_constraints(
796 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
800 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
804 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
805 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
806 unsigned n_eq, unsigned n_ineq)
808 struct isl_basic_map *bmap;
813 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
817 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
821 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
822 unsigned nparam, unsigned dim, unsigned extra,
823 unsigned n_eq, unsigned n_ineq)
825 return (struct isl_basic_set *)
826 isl_basic_map_extend((struct isl_basic_map *)base,
827 nparam, 0, dim, extra, n_eq, n_ineq);
830 struct isl_basic_set *isl_basic_set_extend_constraints(
831 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
833 return (struct isl_basic_set *)
834 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
838 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
840 return (struct isl_basic_set *)
841 isl_basic_map_cow((struct isl_basic_map *)bset);
844 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
851 bmap = isl_basic_map_dup(bmap);
853 F_CLR(bmap, ISL_BASIC_SET_FINAL);
857 struct isl_set *isl_set_cow(struct isl_set *set)
865 return isl_set_dup(set);
868 struct isl_map *isl_map_cow(struct isl_map *map)
876 return isl_map_dup(map);
879 static void swap_vars(struct isl_blk blk, isl_int *a,
880 unsigned a_len, unsigned b_len)
882 isl_seq_cpy(blk.data, a+a_len, b_len);
883 isl_seq_cpy(blk.data+b_len, a, a_len);
884 isl_seq_cpy(a, blk.data, b_len+a_len);
887 struct isl_basic_set *isl_basic_set_swap_vars(
888 struct isl_basic_set *bset, unsigned n)
898 nparam = isl_basic_set_n_param(bset);
899 dim = isl_basic_set_n_dim(bset);
900 isl_assert(bset->ctx, n <= dim, goto error);
905 bset = isl_basic_set_cow(bset);
909 blk = isl_blk_alloc(bset->ctx, dim);
910 if (isl_blk_is_error(blk))
913 for (i = 0; i < bset->n_eq; ++i)
915 bset->eq[i]+1+nparam, n, dim - n);
917 for (i = 0; i < bset->n_ineq; ++i)
919 bset->ineq[i]+1+nparam, n, dim - n);
921 for (i = 0; i < bset->n_div; ++i)
923 bset->div[i]+1+1+nparam, n, dim - n);
925 isl_blk_free(bset->ctx, blk);
927 F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
931 isl_basic_set_free(bset);
935 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
938 set = isl_set_cow(set);
942 for (i = 0; i < set->n; ++i) {
943 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
949 F_CLR(set, ISL_SET_NORMALIZED);
953 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
959 total = isl_basic_map_total_dim(bmap);
960 isl_basic_map_free_div(bmap, bmap->n_div);
961 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
963 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
965 isl_basic_map_alloc_equality(bmap);
969 isl_int_set_si(bmap->eq[i][0], 1);
970 isl_seq_clr(bmap->eq[i]+1, total);
971 F_SET(bmap, ISL_BASIC_MAP_EMPTY);
972 return isl_basic_map_finalize(bmap);
974 isl_basic_map_free(bmap);
978 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
980 return (struct isl_basic_set *)
981 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
984 static void swap_div(struct isl_basic_map *bmap, int a, int b)
987 unsigned off = isl_dim_total(bmap->dim);
988 isl_int *t = bmap->div[a];
989 bmap->div[a] = bmap->div[b];
992 for (i = 0; i < bmap->n_eq; ++i)
993 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
995 for (i = 0; i < bmap->n_ineq; ++i)
996 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
998 for (i = 0; i < bmap->n_div; ++i)
999 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1000 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1003 /* Eliminate the specified n dimensions starting at first from the
1004 * constraints using Fourier-Motzkin, The dimensions themselves
1007 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1008 unsigned first, unsigned n)
1018 set = isl_set_cow(set);
1021 isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1022 nparam = isl_set_n_param(set);
1024 for (i = 0; i < set->n; ++i) {
1025 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1036 /* Project out n dimensions starting at first using Fourier-Motzkin */
1037 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1038 unsigned first, unsigned n)
1040 set = isl_set_eliminate_dims(set, first, n);
1041 set = isl_set_drop_dims(set, first, n);
1045 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1047 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1055 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1064 set = isl_set_cow(set);
1068 for (i = 0; i < set->n; ++i) {
1069 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1079 /* Project out n inputs starting at first using Fourier-Motzkin */
1080 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1081 unsigned first, unsigned n)
1089 map = isl_map_cow(map);
1092 nparam = isl_map_n_param(map);
1093 isl_assert(map->ctx, first+n <= isl_map_n_in(map), goto error);
1095 for (i = 0; i < map->n; ++i) {
1096 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1101 map = isl_map_drop_inputs(map, first, n);
1108 /* Project out n dimensions starting at first using Fourier-Motzkin */
1109 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1110 unsigned first, unsigned n)
1112 unsigned nparam = isl_basic_set_n_param(bset);
1113 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1114 bset = isl_basic_set_drop_dims(bset, first, n);
1118 static void dump_term(struct isl_basic_map *bmap,
1119 isl_int c, int pos, FILE *out)
1122 unsigned in = isl_basic_map_n_in(bmap);
1123 unsigned dim = in + isl_basic_map_n_out(bmap);
1124 unsigned nparam = isl_basic_map_n_param(bmap);
1126 isl_int_print(out, c, 0);
1128 if (!isl_int_is_one(c))
1129 isl_int_print(out, c, 0);
1130 if (pos < 1 + nparam) {
1131 name = isl_dim_get_name(bmap->dim,
1132 isl_dim_param, pos - 1);
1134 fprintf(out, "%s", name);
1136 fprintf(out, "p%d", pos - 1);
1137 } else if (pos < 1 + nparam + in)
1138 fprintf(out, "i%d", pos - 1 - nparam);
1139 else if (pos < 1 + nparam + dim)
1140 fprintf(out, "o%d", pos - 1 - nparam - in);
1142 fprintf(out, "e%d", pos - 1 - nparam - dim);
1146 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1147 int sign, FILE *out)
1151 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1155 for (i = 0, first = 1; i < len; ++i) {
1156 if (isl_int_sgn(c[i]) * sign <= 0)
1159 fprintf(out, " + ");
1161 isl_int_abs(v, c[i]);
1162 dump_term(bmap, v, i, out);
1169 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1170 const char *op, FILE *out, int indent)
1174 fprintf(out, "%*s", indent, "");
1176 dump_constraint_sign(bmap, c, 1, out);
1177 fprintf(out, " %s ", op);
1178 dump_constraint_sign(bmap, c, -1, out);
1182 for (i = bmap->n_div; i < bmap->extra; ++i) {
1183 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1185 fprintf(out, "%*s", indent, "");
1186 fprintf(out, "ERROR: unused div coefficient not zero\n");
1191 static void dump_constraints(struct isl_basic_map *bmap,
1192 isl_int **c, unsigned n,
1193 const char *op, FILE *out, int indent)
1197 for (i = 0; i < n; ++i)
1198 dump_constraint(bmap, c[i], op, out, indent);
1201 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1205 unsigned total = isl_basic_map_total_dim(bmap);
1207 for (j = 0; j < 1 + total; ++j) {
1208 if (isl_int_is_zero(exp[j]))
1210 if (!first && isl_int_is_pos(exp[j]))
1212 dump_term(bmap, exp[j], j, out);
1217 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1221 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1222 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1224 for (i = 0; i < bmap->n_div; ++i) {
1225 fprintf(out, "%*s", indent, "");
1226 fprintf(out, "e%d = [(", i);
1227 dump_affine(bmap, bmap->div[i]+1, out);
1229 isl_int_print(out, bmap->div[i][0], 0);
1230 fprintf(out, "]\n");
1234 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1237 fprintf(out, "null basic set\n");
1241 fprintf(out, "%*s", indent, "");
1242 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1243 bset->ref, bset->dim->nparam, bset->dim->n_out,
1244 bset->extra, bset->flags);
1245 dump((struct isl_basic_map *)bset, out, indent);
1248 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1251 fprintf(out, "null basic map\n");
1255 fprintf(out, "%*s", indent, "");
1256 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1257 "flags: %x, n_name: %d\n",
1259 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1260 bmap->extra, bmap->flags, bmap->dim->n_name);
1261 dump(bmap, out, indent);
1264 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1269 total = isl_basic_map_total_dim(bmap);
1270 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1271 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1272 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1273 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1277 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1279 struct isl_set *set;
1283 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1284 isl_assert(dim->ctx, n >= 0, return NULL);
1285 set = isl_alloc(dim->ctx, struct isl_set,
1286 sizeof(struct isl_set) +
1287 n * sizeof(struct isl_basic_set *));
1291 set->ctx = dim->ctx;
1292 isl_ctx_ref(set->ctx);
1304 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1305 unsigned nparam, unsigned dim, int n, unsigned flags)
1307 struct isl_set *set;
1308 struct isl_dim *dims;
1310 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1314 set = isl_set_alloc_dim(dims, n, flags);
1318 struct isl_set *isl_set_dup(struct isl_set *set)
1321 struct isl_set *dup;
1326 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1329 for (i = 0; i < set->n; ++i)
1330 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1334 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1336 struct isl_set *set;
1341 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1343 isl_basic_set_free(bset);
1346 return isl_set_add(set, bset);
1349 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1351 struct isl_map *map;
1356 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1358 isl_basic_map_free(bmap);
1361 return isl_map_add(map, bmap);
1364 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1368 isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1369 isl_assert(set->ctx, set->n < set->size, goto error);
1370 set->p[set->n] = bset;
1377 isl_basic_set_free(bset);
1381 void isl_set_free(struct isl_set *set)
1391 isl_ctx_deref(set->ctx);
1392 for (i = 0; i < set->n; ++i)
1393 isl_basic_set_free(set->p[i]);
1394 isl_dim_free(set->dim);
1398 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1403 fprintf(out, "null set\n");
1407 fprintf(out, "%*s", indent, "");
1408 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1409 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1411 for (i = 0; i < set->n; ++i) {
1412 fprintf(out, "%*s", indent, "");
1413 fprintf(out, "basic set %d:\n", i);
1414 isl_basic_set_dump(set->p[i], out, indent+4);
1418 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1423 fprintf(out, "null map\n");
1427 fprintf(out, "%*s", indent, "");
1428 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1429 "flags: %x, n_name: %d\n",
1430 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1431 map->dim->n_out, map->flags, map->dim->n_name);
1432 for (i = 0; i < map->n; ++i) {
1433 fprintf(out, "%*s", indent, "");
1434 fprintf(out, "basic map %d:\n", i);
1435 isl_basic_map_dump(map->p[i], out, indent+4);
1439 struct isl_basic_map *isl_basic_map_intersect_domain(
1440 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1442 struct isl_basic_map *bmap_domain;
1443 struct isl_dim *dim;
1448 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1451 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1452 bset->n_div, bset->n_eq, bset->n_ineq);
1455 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1456 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1457 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1459 bmap = isl_basic_map_simplify(bmap);
1460 return isl_basic_map_finalize(bmap);
1462 isl_basic_map_free(bmap);
1463 isl_basic_set_free(bset);
1467 struct isl_basic_map *isl_basic_map_intersect_range(
1468 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1470 struct isl_basic_map *bmap_range;
1475 isl_assert(bset->ctx, isl_basic_map_compatible_range(bmap, bset),
1478 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1479 bset->n_div, bset->n_eq, bset->n_ineq);
1482 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1483 bmap = add_constraints(bmap, bmap_range, 0, 0);
1485 bmap = isl_basic_map_simplify(bmap);
1486 return isl_basic_map_finalize(bmap);
1488 isl_basic_map_free(bmap);
1489 isl_basic_set_free(bset);
1493 struct isl_basic_map *isl_basic_map_intersect(
1494 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1496 if (!bmap1 || !bmap2)
1499 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1501 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1502 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1505 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1507 bmap1 = isl_basic_map_simplify(bmap1);
1508 return isl_basic_map_finalize(bmap1);
1510 isl_basic_map_free(bmap1);
1511 isl_basic_map_free(bmap2);
1515 struct isl_basic_set *isl_basic_set_intersect(
1516 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1518 return (struct isl_basic_set *)
1519 isl_basic_map_intersect(
1520 (struct isl_basic_map *)bset1,
1521 (struct isl_basic_map *)bset2);
1524 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1527 struct isl_map *result;
1533 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
1534 F_ISSET(map2, ISL_MAP_DISJOINT))
1535 FL_SET(flags, ISL_MAP_DISJOINT);
1537 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1538 map1->n * map2->n, flags);
1541 for (i = 0; i < map1->n; ++i)
1542 for (j = 0; j < map2->n; ++j) {
1543 struct isl_basic_map *part;
1544 part = isl_basic_map_intersect(
1545 isl_basic_map_copy(map1->p[i]),
1546 isl_basic_map_copy(map2->p[j]));
1547 if (isl_basic_map_is_empty(part))
1548 isl_basic_map_free(part);
1550 result = isl_map_add(result, part);
1563 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1565 return (struct isl_set *)
1566 isl_map_intersect((struct isl_map *)set1,
1567 (struct isl_map *)set2);
1570 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1572 struct isl_dim *dim;
1573 struct isl_basic_set *bset;
1578 bmap = isl_basic_map_cow(bmap);
1581 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1582 in = isl_basic_map_n_in(bmap);
1583 bset = isl_basic_set_from_basic_map(bmap);
1584 bset = isl_basic_set_swap_vars(bset, in);
1585 return isl_basic_map_from_basic_set(bset, dim);
1588 /* Turn final n dimensions into existentially quantified variables.
1590 struct isl_basic_set *isl_basic_set_project_out(
1591 struct isl_basic_set *bset, unsigned n, unsigned flags)
1601 isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1606 bset = isl_basic_set_cow(bset);
1608 row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1609 old = bset->block2.data;
1610 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1611 (bset->extra + n) * (1 + row_size));
1612 if (!bset->block2.data)
1614 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1617 for (i = 0; i < n; ++i) {
1618 new_div[i] = bset->block2.data +
1619 (bset->extra + i) * (1 + row_size);
1620 isl_seq_clr(new_div[i], 1 + row_size);
1622 for (i = 0; i < bset->extra; ++i)
1623 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1625 bset->div = new_div;
1628 bset->dim = isl_dim_drop_outputs(bset->dim,
1629 isl_basic_set_n_dim(bset) - n, n);
1632 bset = isl_basic_set_simplify(bset);
1633 return isl_basic_set_finalize(bset);
1635 isl_basic_set_free(bset);
1639 struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1643 for (i = 0; i < n; ++i) {
1644 j = isl_basic_map_alloc_div(bmap);
1647 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1651 isl_basic_map_free(bmap);
1655 struct isl_basic_map *isl_basic_map_apply_range(
1656 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1658 struct isl_dim *dim_result = NULL;
1659 struct isl_basic_map *bmap;
1660 unsigned n_in, n_out, n, nparam, total, pos;
1661 struct isl_dim_map *dim_map1, *dim_map2;
1663 if (!bmap1 || !bmap2)
1666 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1667 isl_dim_copy(bmap2->dim));
1669 n_in = isl_basic_map_n_in(bmap1);
1670 n_out = isl_basic_map_n_out(bmap2);
1671 n = isl_basic_map_n_out(bmap1);
1672 nparam = isl_basic_map_n_param(bmap1);
1674 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1675 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1676 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1677 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1678 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1679 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1680 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1681 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1682 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1683 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1684 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1686 bmap = isl_basic_map_alloc_dim(dim_result,
1687 bmap1->n_div + bmap2->n_div + n,
1688 bmap1->n_eq + bmap2->n_eq,
1689 bmap1->n_ineq + bmap2->n_ineq);
1690 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1691 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1692 bmap = add_divs(bmap, n);
1693 bmap = isl_basic_map_simplify(bmap);
1694 return isl_basic_map_finalize(bmap);
1696 isl_basic_map_free(bmap1);
1697 isl_basic_map_free(bmap2);
1701 struct isl_basic_set *isl_basic_set_apply(
1702 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1707 isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1710 return (struct isl_basic_set *)
1711 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1713 isl_basic_set_free(bset);
1714 isl_basic_map_free(bmap);
1718 struct isl_basic_map *isl_basic_map_apply_domain(
1719 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1721 if (!bmap1 || !bmap2)
1725 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1727 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1730 bmap1 = isl_basic_map_reverse(bmap1);
1731 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1732 return isl_basic_map_reverse(bmap1);
1734 isl_basic_map_free(bmap1);
1735 isl_basic_map_free(bmap2);
1739 static struct isl_basic_map *var_equal(struct isl_ctx *ctx,
1740 struct isl_basic_map *bmap, unsigned pos)
1746 i = isl_basic_map_alloc_equality(bmap);
1749 nparam = isl_basic_map_n_param(bmap);
1750 n_in = isl_basic_map_n_in(bmap);
1751 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
1752 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
1753 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
1754 return isl_basic_map_finalize(bmap);
1756 isl_basic_map_free(bmap);
1760 static struct isl_basic_map *var_more(struct isl_ctx *ctx,
1761 struct isl_basic_map *bmap, unsigned pos)
1767 i = isl_basic_map_alloc_inequality(bmap);
1770 nparam = isl_basic_map_n_param(bmap);
1771 n_in = isl_basic_map_n_in(bmap);
1772 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
1773 isl_int_set_si(bmap->ineq[i][0], -1);
1774 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
1775 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
1776 return isl_basic_map_finalize(bmap);
1778 isl_basic_map_free(bmap);
1782 static struct isl_basic_map *var_less(struct isl_ctx *ctx,
1783 struct isl_basic_map *bmap, unsigned pos)
1789 i = isl_basic_map_alloc_inequality(bmap);
1792 nparam = isl_basic_map_n_param(bmap);
1793 n_in = isl_basic_map_n_in(bmap);
1794 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
1795 isl_int_set_si(bmap->ineq[i][0], -1);
1796 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
1797 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
1798 return isl_basic_map_finalize(bmap);
1800 isl_basic_map_free(bmap);
1804 struct isl_basic_map *isl_basic_map_equal(struct isl_ctx *ctx,
1805 unsigned nparam, unsigned in, unsigned out, unsigned n_equal)
1808 struct isl_basic_map *bmap;
1809 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, n_equal, 0);
1812 for (i = 0; i < n_equal && bmap; ++i)
1813 bmap = var_equal(ctx, bmap, i);
1814 return isl_basic_map_finalize(bmap);
1817 struct isl_basic_map *isl_basic_map_less_at(struct isl_ctx *ctx,
1818 unsigned nparam, unsigned in, unsigned out, unsigned pos)
1821 struct isl_basic_map *bmap;
1822 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
1825 for (i = 0; i < pos && bmap; ++i)
1826 bmap = var_equal(ctx, bmap, i);
1828 bmap = var_less(ctx, bmap, pos);
1829 return isl_basic_map_finalize(bmap);
1832 struct isl_basic_map *isl_basic_map_more_at(struct isl_ctx *ctx,
1833 unsigned nparam, unsigned in, unsigned out, unsigned pos)
1836 struct isl_basic_map *bmap;
1837 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
1840 for (i = 0; i < pos && bmap; ++i)
1841 bmap = var_equal(ctx, bmap, i);
1843 bmap = var_more(ctx, bmap, pos);
1844 return isl_basic_map_finalize(bmap);
1847 struct isl_basic_map *isl_basic_map_from_basic_set(
1848 struct isl_basic_set *bset, struct isl_dim *dim)
1850 struct isl_basic_map *bmap;
1852 bset = isl_basic_set_cow(bset);
1856 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
1857 isl_dim_free(bset->dim);
1858 bmap = (struct isl_basic_map *) bset;
1860 return isl_basic_map_finalize(bmap);
1862 isl_basic_set_free(bset);
1867 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
1871 if (bmap->dim->n_in == 0)
1872 return (struct isl_basic_set *)bmap;
1873 bmap = isl_basic_map_cow(bmap);
1876 bmap->dim = isl_dim_cow(bmap->dim);
1879 bmap->dim->n_out += bmap->dim->n_in;
1880 bmap->dim->n_in = 0;
1881 bmap = isl_basic_map_finalize(bmap);
1882 return (struct isl_basic_set *)bmap;
1884 isl_basic_map_free(bmap);
1888 /* For a div d = floor(f/m), add the constraints
1891 * -(f-(n-1)) + m d >= 0
1893 * Note that the second constraint is the negation of
1897 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
1900 unsigned total = isl_basic_map_total_dim(bmap);
1901 unsigned div_pos = 1 + total - bmap->n_div + div;
1903 i = isl_basic_map_alloc_inequality(bmap);
1906 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
1907 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
1909 j = isl_basic_map_alloc_inequality(bmap);
1912 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
1913 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
1914 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
1918 struct isl_basic_set *isl_basic_map_underlying_set(
1919 struct isl_basic_map *bmap)
1923 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
1924 return (struct isl_basic_set *)bmap;
1925 bmap = isl_basic_map_cow(bmap);
1928 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
1931 bmap->extra -= bmap->n_div;
1933 bmap = isl_basic_map_finalize(bmap);
1934 return (struct isl_basic_set *)bmap;
1939 struct isl_basic_map *isl_basic_map_overlying_set(
1940 struct isl_basic_set *bset, struct isl_basic_map *like)
1942 struct isl_basic_map *bmap;
1943 struct isl_ctx *ctx;
1950 isl_assert(ctx, bset->n_div == 0, goto error);
1951 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
1952 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
1954 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
1955 isl_basic_map_free(like);
1956 return (struct isl_basic_map *)bset;
1958 bset = isl_basic_set_cow(bset);
1961 total = bset->dim->n_out + bset->extra;
1962 bmap = (struct isl_basic_map *)bset;
1963 isl_dim_free(bmap->dim);
1964 bmap->dim = isl_dim_copy(like->dim);
1967 bmap->n_div = like->n_div;
1968 bmap->extra += like->n_div;
1971 ltotal = total - bmap->extra + like->extra;
1974 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
1975 bmap->extra * (1 + 1 + total));
1976 if (isl_blk_is_error(bmap->block2))
1978 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
1982 for (i = 0; i < bmap->extra; ++i)
1983 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
1984 for (i = 0; i < like->n_div; ++i) {
1985 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
1986 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
1988 bmap = isl_basic_map_extend_constraints(bmap,
1989 0, 2 * like->n_div);
1990 for (i = 0; i < like->n_div; ++i)
1991 if (add_div_constraints(bmap, i) < 0)
1994 isl_basic_map_free(like);
1995 bmap = isl_basic_map_simplify(bmap);
1996 bmap = isl_basic_map_finalize(bmap);
1999 isl_basic_map_free(like);
2000 isl_basic_set_free(bset);
2004 struct isl_basic_set *isl_basic_set_from_underlying_set(
2005 struct isl_basic_set *bset, struct isl_basic_set *like)
2007 return (struct isl_basic_set *)
2008 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2011 struct isl_set *isl_set_from_underlying_set(
2012 struct isl_set *set, struct isl_basic_set *like)
2018 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2020 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2021 isl_basic_set_free(like);
2024 set = isl_set_cow(set);
2027 for (i = 0; i < set->n; ++i) {
2028 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2029 isl_basic_set_copy(like));
2033 isl_dim_free(set->dim);
2034 set->dim = isl_dim_copy(like->dim);
2037 isl_basic_set_free(like);
2040 isl_basic_set_free(like);
2045 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2049 map = isl_map_cow(map);
2052 map->dim = isl_dim_cow(map->dim);
2056 for (i = 1; i < map->n; ++i)
2057 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2059 for (i = 0; i < map->n; ++i) {
2060 map->p[i] = (struct isl_basic_map *)
2061 isl_basic_map_underlying_set(map->p[i]);
2066 map->dim = isl_dim_underlying(map->dim, 0);
2068 isl_dim_free(map->dim);
2069 map->dim = isl_dim_copy(map->p[0]->dim);
2073 return (struct isl_set *)map;
2079 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2081 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2084 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2086 struct isl_basic_set *domain;
2090 n_out = isl_basic_map_n_out(bmap);
2091 domain = isl_basic_set_from_basic_map(bmap);
2092 return isl_basic_set_project_out(domain, n_out, 0);
2095 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2097 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2100 struct isl_set *isl_map_range(struct isl_map *map)
2103 struct isl_set *set;
2107 map = isl_map_cow(map);
2111 set = (struct isl_set *) map;
2112 if (set->dim->n_in != 0) {
2113 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2117 for (i = 0; i < map->n; ++i) {
2118 set->p[i] = isl_basic_map_range(map->p[i]);
2122 F_CLR(set, ISL_MAP_DISJOINT);
2123 F_CLR(set, ISL_SET_NORMALIZED);
2130 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2133 struct isl_map *map = NULL;
2135 set = isl_set_cow(set);
2138 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2139 map = (struct isl_map *)set;
2140 for (i = 0; i < set->n; ++i) {
2141 map->p[i] = isl_basic_map_from_basic_set(
2142 set->p[i], isl_dim_copy(dim));
2146 isl_dim_free(map->dim);
2155 struct isl_set *isl_set_from_map(struct isl_map *map)
2158 struct isl_set *set = NULL;
2162 map = isl_map_cow(map);
2165 map->dim = isl_dim_cow(map->dim);
2168 map->dim->n_out += map->dim->n_in;
2170 set = (struct isl_set *)map;
2171 for (i = 0; i < map->n; ++i) {
2172 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2182 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2184 struct isl_map *map;
2188 isl_assert(dim->ctx, n >= 0, return NULL);
2189 map = isl_alloc(dim->ctx, struct isl_map,
2190 sizeof(struct isl_map) +
2191 n * sizeof(struct isl_basic_map *));
2195 map->ctx = dim->ctx;
2196 isl_ctx_ref(map->ctx);
2208 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2209 unsigned nparam, unsigned in, unsigned out, int n,
2212 struct isl_map *map;
2213 struct isl_dim *dims;
2215 dims = isl_dim_alloc(ctx, nparam, in, out);
2219 map = isl_map_alloc_dim(dims, n, flags);
2223 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2224 unsigned nparam, unsigned in, unsigned out)
2226 struct isl_basic_map *bmap;
2227 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2228 bmap = isl_basic_map_set_to_empty(bmap);
2232 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2234 struct isl_basic_set *bset;
2235 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2236 bset = isl_basic_set_set_to_empty(bset);
2240 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2242 struct isl_basic_map *bmap;
2245 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2246 bmap = isl_basic_map_set_to_empty(bmap);
2250 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2252 struct isl_basic_map *bmap;
2255 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2256 bmap = isl_basic_map_set_to_empty(bmap);
2260 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2262 struct isl_basic_set *bset;
2265 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2266 bset = isl_basic_set_set_to_empty(bset);
2270 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2272 struct isl_basic_map *bmap;
2273 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2277 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2279 struct isl_basic_set *bset;
2280 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2284 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2288 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2291 struct isl_map *isl_map_empty(struct isl_ctx *ctx,
2292 unsigned nparam, unsigned in, unsigned out)
2294 return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
2297 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2301 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2304 struct isl_set *isl_set_empty(struct isl_dim *dim)
2306 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2309 struct isl_set *isl_set_empty_like(struct isl_set *model)
2313 return isl_set_empty(isl_dim_copy(model->dim));
2316 struct isl_set *isl_set_universe(struct isl_dim *dim)
2318 struct isl_set *set;
2321 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2322 set = isl_set_add(set, isl_basic_set_universe(dim));
2326 struct isl_map *isl_map_dup(struct isl_map *map)
2329 struct isl_map *dup;
2333 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2334 for (i = 0; i < map->n; ++i)
2335 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2339 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2343 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2344 isl_assert(map->ctx, map->n < map->size, goto error);
2345 map->p[map->n] = bmap;
2347 F_CLR(map, ISL_MAP_NORMALIZED);
2353 isl_basic_map_free(bmap);
2357 void isl_map_free(struct isl_map *map)
2367 isl_ctx_deref(map->ctx);
2368 for (i = 0; i < map->n; ++i)
2369 isl_basic_map_free(map->p[i]);
2370 isl_dim_free(map->dim);
2374 struct isl_map *isl_map_extend(struct isl_map *base,
2375 unsigned nparam, unsigned n_in, unsigned n_out)
2379 base = isl_map_cow(base);
2383 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2386 for (i = 0; i < base->n; ++i) {
2387 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2388 isl_dim_copy(base->dim), 0, 0, 0);
2398 struct isl_set *isl_set_extend(struct isl_set *base,
2399 unsigned nparam, unsigned dim)
2401 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2405 static struct isl_basic_map *isl_basic_map_fix_var(struct isl_basic_map *bmap,
2406 unsigned var, int value)
2410 bmap = isl_basic_map_cow(bmap);
2411 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2412 j = isl_basic_map_alloc_equality(bmap);
2415 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2416 isl_int_set_si(bmap->eq[j][1 + var], -1);
2417 isl_int_set_si(bmap->eq[j][0], value);
2418 bmap = isl_basic_map_simplify(bmap);
2419 return isl_basic_map_finalize(bmap);
2421 isl_basic_map_free(bmap);
2425 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2426 unsigned input, int value)
2430 isl_assert(bmap->ctx, input < isl_basic_map_n_in(bmap), goto error);
2431 return isl_basic_map_fix_var(bmap, isl_basic_map_n_param(bmap) + input,
2434 isl_basic_map_free(bmap);
2438 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2439 unsigned dim, int value)
2443 isl_assert(bset->ctx, dim < isl_basic_set_n_dim(bset), goto error);
2444 return (struct isl_basic_set *)
2445 isl_basic_map_fix_var((struct isl_basic_map *)bset,
2446 isl_basic_set_n_param(bset) + dim, value);
2448 isl_basic_set_free(bset);
2452 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2453 unsigned input, int value)
2457 map = isl_map_cow(map);
2461 isl_assert(ctx, input < isl_map_n_in(map), goto error);
2462 for (i = 0; i < map->n; ++i) {
2463 map->p[i] = isl_basic_map_fix_input_si(map->p[i], input, value);
2467 F_CLR(map, ISL_MAP_NORMALIZED);
2474 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2478 set = isl_set_cow(set);
2482 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2483 for (i = 0; i < set->n; ++i) {
2484 set->p[i] = isl_basic_set_fix_dim_si(set->p[i], dim, value);
2494 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2495 unsigned dim, isl_int value)
2500 bset = isl_basic_set_cow(bset);
2501 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2502 j = isl_basic_set_alloc_inequality(bset);
2505 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2506 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2507 isl_int_neg(bset->ineq[j][0], value);
2508 bset = isl_basic_set_simplify(bset);
2509 return isl_basic_set_finalize(bset);
2511 isl_basic_set_free(bset);
2515 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2520 set = isl_set_cow(set);
2524 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2525 for (i = 0; i < set->n; ++i) {
2526 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2536 struct isl_map *isl_map_reverse(struct isl_map *map)
2541 map = isl_map_cow(map);
2545 map->dim = isl_dim_reverse(map->dim);
2548 for (i = 0; i < map->n; ++i) {
2549 map->p[i] = isl_basic_map_reverse(map->p[i]);
2553 F_CLR(map, ISL_MAP_NORMALIZED);
2560 struct isl_map *isl_basic_map_lexmax(
2561 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2562 struct isl_set **empty)
2564 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2567 struct isl_map *isl_basic_map_lexmin(
2568 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2569 struct isl_set **empty)
2571 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2574 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2576 struct isl_basic_map *bmap = NULL;
2577 struct isl_basic_set *dom = NULL;
2578 struct isl_map *min;
2579 struct isl_dim *param_dim;
2583 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2586 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2587 dom = isl_basic_set_universe(param_dim);
2590 min = isl_basic_map_lexmin(bmap, dom, NULL);
2591 return isl_map_range(min);
2593 isl_basic_map_free(bmap);
2597 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2604 off = isl_dim_total(bmap->dim);
2605 for (i = 0; i < bmap->n_div; ++i) {
2606 if (isl_int_is_zero(bmap->div[i][0]))
2607 return isl_pip_basic_map_compute_divs(bmap);
2608 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2611 return isl_map_from_basic_map(bmap);
2613 isl_basic_map_free(bmap);
2617 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2620 struct isl_map *res;
2626 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2627 for (i = 1 ; i < map->n; ++i) {
2629 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2630 if (F_ISSET(map, ISL_MAP_DISJOINT))
2631 res = isl_map_union_disjoint(res, r2);
2633 res = isl_map_union(res, r2);
2640 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2642 return (struct isl_set *)
2643 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2646 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2648 return (struct isl_set *)
2649 isl_map_compute_divs((struct isl_map *)set);
2652 struct isl_set *isl_map_domain(struct isl_map *map)
2655 struct isl_set *set;
2660 map = isl_map_cow(map);
2664 set = (struct isl_set *)map;
2665 set->dim = isl_dim_domain(set->dim);
2668 for (i = 0; i < map->n; ++i) {
2669 set->p[i] = isl_basic_map_domain(map->p[i]);
2673 F_CLR(set, ISL_MAP_DISJOINT);
2674 F_CLR(set, ISL_SET_NORMALIZED);
2681 struct isl_map *isl_map_union_disjoint(
2682 struct isl_map *map1, struct isl_map *map2)
2686 struct isl_map *map = NULL;
2700 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2702 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2703 F_ISSET(map2, ISL_MAP_DISJOINT))
2704 FL_SET(flags, ISL_MAP_DISJOINT);
2706 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2707 map1->n + map2->n, flags);
2710 for (i = 0; i < map1->n; ++i) {
2711 map = isl_map_add(map,
2712 isl_basic_map_copy(map1->p[i]));
2716 for (i = 0; i < map2->n; ++i) {
2717 map = isl_map_add(map,
2718 isl_basic_map_copy(map2->p[i]));
2732 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
2734 map1 = isl_map_union_disjoint(map1, map2);
2738 F_CLR(map1, ISL_MAP_DISJOINT);
2742 struct isl_set *isl_set_union_disjoint(
2743 struct isl_set *set1, struct isl_set *set2)
2745 return (struct isl_set *)
2746 isl_map_union_disjoint(
2747 (struct isl_map *)set1, (struct isl_map *)set2);
2750 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
2752 return (struct isl_set *)
2753 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
2756 struct isl_map *isl_map_intersect_range(
2757 struct isl_map *map, struct isl_set *set)
2760 struct isl_map *result;
2766 if (F_ISSET(map, ISL_MAP_DISJOINT) &&
2767 F_ISSET(set, ISL_MAP_DISJOINT))
2768 FL_SET(flags, ISL_MAP_DISJOINT);
2770 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
2771 map->n * set->n, flags);
2774 for (i = 0; i < map->n; ++i)
2775 for (j = 0; j < set->n; ++j) {
2776 result = isl_map_add(result,
2777 isl_basic_map_intersect_range(
2778 isl_basic_map_copy(map->p[i]),
2779 isl_basic_set_copy(set->p[j])));
2792 struct isl_map *isl_map_intersect_domain(
2793 struct isl_map *map, struct isl_set *set)
2795 return isl_map_reverse(
2796 isl_map_intersect_range(isl_map_reverse(map), set));
2799 struct isl_map *isl_map_apply_domain(
2800 struct isl_map *map1, struct isl_map *map2)
2804 map1 = isl_map_reverse(map1);
2805 map1 = isl_map_apply_range(map1, map2);
2806 return isl_map_reverse(map1);
2813 struct isl_map *isl_map_apply_range(
2814 struct isl_map *map1, struct isl_map *map2)
2816 struct isl_dim *dim_result;
2817 struct isl_map *result;
2826 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
2827 isl_dim_copy(map2->dim));
2829 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
2832 for (i = 0; i < map1->n; ++i)
2833 for (j = 0; j < map2->n; ++j) {
2834 result = isl_map_add(result,
2835 isl_basic_map_apply_range(
2836 isl_basic_map_copy(map1->p[i]),
2837 isl_basic_map_copy(map2->p[j])));
2843 if (result && result->n <= 1)
2844 F_SET(result, ISL_MAP_DISJOINT);
2853 * returns range - domain
2855 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
2857 struct isl_basic_set *bset;
2864 dim = isl_basic_map_n_in(bmap);
2865 nparam = isl_basic_map_n_param(bmap);
2866 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
2867 bset = isl_basic_set_from_basic_map(bmap);
2868 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
2869 bset = isl_basic_set_swap_vars(bset, 2*dim);
2870 for (i = 0; i < dim; ++i) {
2871 int j = isl_basic_map_alloc_equality(
2872 (struct isl_basic_map *)bset);
2875 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
2876 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
2877 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
2878 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
2880 return isl_basic_set_project_out(bset, 2*dim, 0);
2882 isl_basic_map_free(bmap);
2887 * returns range - domain
2889 struct isl_set *isl_map_deltas(struct isl_map *map)
2892 struct isl_set *result;
2897 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
2898 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
2899 isl_map_n_in(map), map->n, map->flags);
2902 for (i = 0; i < map->n; ++i)
2903 result = isl_set_add(result,
2904 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
2912 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
2914 struct isl_basic_map *bmap;
2922 nparam = dims->nparam;
2924 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
2928 for (i = 0; i < dim; ++i) {
2929 int j = isl_basic_map_alloc_equality(bmap);
2932 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2933 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
2934 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
2936 return isl_basic_map_finalize(bmap);
2938 isl_basic_map_free(bmap);
2942 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
2944 struct isl_dim *dim = isl_dim_map(set_dim);
2947 return basic_map_identity(dim);
2950 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
2952 if (!model || !model->dim)
2954 isl_assert(model->ctx,
2955 model->dim->n_in == model->dim->n_out, return NULL);
2956 return basic_map_identity(isl_dim_copy(model->dim));
2959 static struct isl_map *map_identity(struct isl_dim *dim)
2961 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
2962 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
2965 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
2967 struct isl_dim *dim = isl_dim_map(set_dim);
2970 return map_identity(dim);
2973 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
2975 if (!model || !model->dim)
2977 isl_assert(model->ctx,
2978 model->dim->n_in == model->dim->n_out, return NULL);
2979 return map_identity(isl_dim_copy(model->dim));
2982 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
2984 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
2987 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
2989 return isl_map_is_subset(
2990 (struct isl_map *)set1, (struct isl_map *)set2);
2993 int isl_basic_map_is_subset(
2994 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2997 struct isl_map *map1;
2998 struct isl_map *map2;
3000 if (!bmap1 || !bmap2)
3003 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3004 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3006 is_subset = isl_map_is_subset(map1, map2);
3014 int isl_basic_map_is_equal(
3015 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3019 if (!bmap1 || !bmap2)
3021 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3024 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3028 int isl_basic_set_is_equal(
3029 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3031 return isl_basic_map_is_equal(
3032 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3035 int isl_map_is_empty(struct isl_map *map)
3042 for (i = 0; i < map->n; ++i) {
3043 is_empty = isl_basic_map_is_empty(map->p[i]);
3052 int isl_set_is_empty(struct isl_set *set)
3054 return isl_map_is_empty((struct isl_map *)set);
3057 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3061 struct isl_map *diff;
3066 if (isl_map_is_empty(map1))
3069 if (isl_map_is_empty(map2))
3072 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3076 is_subset = isl_map_is_empty(diff);
3082 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3088 is_subset = isl_map_is_subset(map1, map2);
3091 is_subset = isl_map_is_subset(map2, map1);
3095 int isl_basic_map_is_strict_subset(
3096 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3100 if (!bmap1 || !bmap2)
3102 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3105 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3106 if (is_subset == -1)
3111 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3117 total = 1 + isl_basic_map_total_dim(bmap);
3118 if (total != vec->size)
3123 for (i = 0; i < bmap->n_eq; ++i) {
3124 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3125 if (!isl_int_is_zero(s)) {
3131 for (i = 0; i < bmap->n_ineq; ++i) {
3132 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3133 if (isl_int_is_neg(s)) {
3144 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3148 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3151 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3153 struct isl_basic_set *bset = NULL;
3154 struct isl_vec *sample = NULL;
3161 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3164 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3165 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3166 copy = isl_basic_map_convex_hull(copy);
3167 empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3168 isl_basic_map_free(copy);
3172 total = 1 + isl_basic_map_total_dim(bmap);
3173 if (bmap->sample && bmap->sample->size == total) {
3174 int contains = basic_map_contains(bmap, bmap->sample);
3180 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3183 sample = isl_basic_set_sample(bset);
3186 empty = sample->size == 0;
3188 isl_vec_free(bmap->ctx, bmap->sample);
3189 bmap->sample = sample;
3194 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3196 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3199 struct isl_map *isl_basic_map_union(
3200 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3202 struct isl_map *map;
3203 if (!bmap1 || !bmap2)
3206 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3208 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3211 map = isl_map_add(map, bmap1);
3212 map = isl_map_add(map, bmap2);
3215 isl_basic_map_free(bmap1);
3216 isl_basic_map_free(bmap2);
3220 struct isl_set *isl_basic_set_union(
3221 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3223 return (struct isl_set *)isl_basic_map_union(
3224 (struct isl_basic_map *)bset1,
3225 (struct isl_basic_map *)bset2);
3228 /* Order divs such that any div only depends on previous divs */
3229 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3232 unsigned off = isl_dim_total(bmap->dim);
3234 for (i = 0; i < bmap->n_div; ++i) {
3236 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3240 swap_div(bmap, i, pos);
3246 /* Look for a div in dst that corresponds to the div "div" in src.
3247 * The divs before "div" in src and dst are assumed to be the same.
3249 * Returns -1 if no corresponding div was found and the position
3250 * of the corresponding div in dst otherwise.
3252 static int find_div(struct isl_basic_map *dst,
3253 struct isl_basic_map *src, unsigned div)
3257 unsigned total = isl_dim_total(src->dim);
3259 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3260 for (i = div; i < dst->n_div; ++i)
3261 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3262 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3263 dst->n_div - div) == -1)
3268 struct isl_basic_map *isl_basic_map_align_divs(
3269 struct isl_basic_map *dst, struct isl_basic_map *src)
3272 unsigned total = isl_dim_total(src->dim);
3277 if (src->n_div == 0)
3280 for (i = 0; i < src->n_div; ++i)
3281 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3283 src = order_divs(src);
3284 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3285 src->n_div, 0, 2 * src->n_div);
3288 for (i = 0; i < src->n_div; ++i) {
3289 int j = find_div(dst, src, i);
3291 j = isl_basic_map_alloc_div(dst);
3294 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3295 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3296 if (add_div_constraints(dst, j) < 0)
3300 swap_div(dst, i, j);
3304 isl_basic_map_free(dst);
3308 struct isl_basic_set *isl_basic_set_align_divs(
3309 struct isl_basic_set *dst, struct isl_basic_set *src)
3311 return (struct isl_basic_set *)isl_basic_map_align_divs(
3312 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3315 struct isl_map *isl_map_align_divs(struct isl_map *map)
3319 map = isl_map_compute_divs(map);
3320 map = isl_map_cow(map);
3324 for (i = 1; i < map->n; ++i)
3325 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3326 for (i = 1; i < map->n; ++i)
3327 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3329 F_CLR(map, ISL_MAP_NORMALIZED);
3333 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3334 struct isl_basic_map *src, isl_int *c,
3335 unsigned len, int oppose)
3337 struct isl_basic_map *copy = NULL;
3342 copy = isl_basic_map_copy(src);
3343 copy = isl_basic_map_cow(copy);
3346 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3347 k = isl_basic_map_alloc_inequality(copy);
3351 isl_seq_neg(copy->ineq[k], c, len);
3353 isl_seq_cpy(copy->ineq[k], c, len);
3354 total = 1 + isl_basic_map_total_dim(copy);
3355 isl_seq_clr(copy->ineq[k]+len, total - len);
3356 isl_inequality_negate(copy, k);
3357 copy = isl_basic_map_simplify(copy);
3358 copy = isl_basic_map_finalize(copy);
3359 is_empty = isl_basic_map_is_empty(copy);
3363 dst = isl_map_add(dst, copy);
3365 isl_basic_map_free(copy);
3368 isl_basic_map_free(copy);
3373 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3377 struct isl_map *rest = NULL;
3379 unsigned total = isl_basic_map_total_dim(bmap);
3386 if (F_ISSET(map, ISL_MAP_DISJOINT))
3387 FL_SET(flags, ISL_MAP_DISJOINT);
3389 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3390 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3394 for (i = 0; i < map->n; ++i) {
3395 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3400 for (j = 0; j < map->n; ++j)
3401 map->p[j] = isl_basic_map_cow(map->p[j]);
3403 for (i = 0; i < bmap->n_eq; ++i) {
3404 for (j = 0; j < map->n; ++j) {
3405 rest = add_cut_constraint(rest,
3406 map->p[j], bmap->eq[i], 1+total, 0);
3410 rest = add_cut_constraint(rest,
3411 map->p[j], bmap->eq[i], 1+total, 1);
3415 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3419 k = isl_basic_map_alloc_equality(map->p[j]);
3422 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3423 isl_seq_clr(map->p[j]->eq[k]+1+total,
3424 map->p[j]->n_div - bmap->n_div);
3428 for (i = 0; i < bmap->n_ineq; ++i) {
3429 for (j = 0; j < map->n; ++j) {
3430 rest = add_cut_constraint(rest,
3431 map->p[j], bmap->ineq[i], 1+total, 0);
3435 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3439 k = isl_basic_map_alloc_inequality(map->p[j]);
3442 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3443 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3444 map->p[j]->n_div - bmap->n_div);
3456 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3462 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3464 if (isl_map_is_empty(map2)) {
3469 map1 = isl_map_compute_divs(map1);
3470 map2 = isl_map_compute_divs(map2);
3474 for (i = 0; map1 && i < map2->n; ++i)
3475 map1 = subtract(map1, map2->p[i]);
3485 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3487 return (struct isl_set *)
3489 (struct isl_map *)set1, (struct isl_map *)set2);
3492 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3496 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3497 map = isl_map_intersect_domain(map, set);
3498 set = isl_map_range(map);
3506 /* There is no need to cow as removing empty parts doesn't change
3507 * the meaning of the set.
3509 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3516 for (i = map->n-1; i >= 0; --i) {
3517 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3519 isl_basic_map_free(map->p[i]);
3520 if (i != map->n-1) {
3521 F_CLR(map, ISL_MAP_NORMALIZED);
3522 map->p[i] = map->p[map->n-1];
3530 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3532 return (struct isl_set *)
3533 isl_map_remove_empty_parts((struct isl_map *)set);
3536 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3538 struct isl_basic_set *bset;
3539 if (!set || set->n == 0)
3541 bset = set->p[set->n-1];
3542 isl_assert(set->ctx, F_ISSET(bset, ISL_BASIC_SET_FINAL), return NULL);
3543 return isl_basic_set_copy(bset);
3546 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3547 struct isl_basic_set *bset)
3553 for (i = set->n-1; i >= 0; --i) {
3554 if (set->p[i] != bset)
3556 set = isl_set_cow(set);
3559 isl_basic_set_free(set->p[i]);
3560 if (i != set->n-1) {
3561 F_CLR(set, ISL_SET_NORMALIZED);
3562 set->p[i] = set->p[set->n-1];
3567 isl_basic_set_free(bset);
3571 isl_basic_set_free(bset);
3575 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3576 * for any common value of the parameters and dimensions preceding dim
3577 * in both basic sets, the values of dimension pos in bset1 are
3578 * smaller or larger than those in bset2.
3581 * 1 if bset1 follows bset2
3582 * -1 if bset1 precedes bset2
3583 * 0 if bset1 and bset2 are incomparable
3584 * -2 if some error occurred.
3586 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3587 struct isl_basic_set *bset2, int pos)
3589 struct isl_dim *dims;
3590 struct isl_basic_map *bmap1 = NULL;
3591 struct isl_basic_map *bmap2 = NULL;
3592 struct isl_ctx *ctx;
3593 struct isl_vec *obj;
3596 unsigned dim1, dim2;
3598 enum isl_lp_result res;
3601 if (!bset1 || !bset2)
3604 nparam = isl_basic_set_n_param(bset1);
3605 dim1 = isl_basic_set_n_dim(bset1);
3606 dim2 = isl_basic_set_n_dim(bset2);
3607 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3608 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3609 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3610 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3611 if (!bmap1 || !bmap2)
3613 bmap1 = isl_basic_map_extend(bmap1, nparam,
3614 pos, (dim1 - pos) + (dim2 - pos),
3615 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3616 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
3619 total = isl_basic_map_total_dim(bmap1);
3621 obj = isl_vec_alloc(ctx, total);
3622 isl_seq_clr(obj->block.data, total);
3623 isl_int_set_si(obj->block.data[nparam+pos], 1);
3624 isl_int_set_si(obj->block.data[nparam+pos+(dim1-pos)], -1);
3629 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3630 if (res == isl_lp_empty)
3632 else if (res == isl_lp_ok && isl_int_is_pos(num))
3634 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3635 res == isl_lp_unbounded)
3641 isl_basic_map_free(bmap1);
3642 isl_vec_free(ctx, obj);
3645 isl_basic_map_free(bmap1);
3646 isl_basic_map_free(bmap2);
3650 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
3651 unsigned pos, isl_int *val)
3659 total = isl_basic_map_total_dim(bmap);
3660 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
3661 for (; d+1 > pos; --d)
3662 if (!isl_int_is_zero(bmap->eq[i][1+d]))
3666 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
3668 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
3670 if (!isl_int_is_one(bmap->eq[i][1+d]))
3673 isl_int_neg(*val, bmap->eq[i][0]);
3679 static int isl_map_fast_has_fixed_var(struct isl_map *map,
3680 unsigned pos, isl_int *val)
3692 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
3695 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
3696 for (i = 1; fixed == 1 && i < map->n; ++i) {
3697 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
3698 if (fixed == 1 && isl_int_ne(tmp, v))
3702 isl_int_set(*val, v);
3708 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
3711 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
3714 /* Check if dimension dim has fixed value and if so and if val is not NULL,
3715 * then return this fixed value in *val.
3717 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
3719 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
3722 /* Check if input variable in has fixed value and if so and if val is not NULL,
3723 * then return this fixed value in *val.
3725 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
3727 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
3730 /* Check if dimension dim has an (obvious) fixed lower bound and if so
3731 * and if val is not NULL, then return this lower bound in *val.
3733 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
3734 unsigned dim, isl_int *val)
3736 int i, i_eq = -1, i_ineq = -1;
3743 total = isl_basic_set_total_dim(bset);
3744 nparam = isl_basic_set_n_param(bset);
3745 for (i = 0; i < bset->n_eq; ++i) {
3746 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
3752 for (i = 0; i < bset->n_ineq; ++i) {
3753 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
3755 if (i_eq != -1 || i_ineq != -1)
3759 if (i_eq == -1 && i_ineq == -1)
3761 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
3762 /* The coefficient should always be one due to normalization. */
3763 if (!isl_int_is_one(c[1+nparam+dim]))
3765 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
3767 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
3768 total - nparam - dim - 1) != -1)
3771 isl_int_neg(*val, c[0]);
3775 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
3776 unsigned dim, isl_int *val)
3788 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
3792 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
3794 for (i = 1; fixed == 1 && i < set->n; ++i) {
3795 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
3797 if (fixed == 1 && isl_int_ne(tmp, v))
3801 isl_int_set(*val, v);
3812 static int qsort_constraint_cmp(const void *p1, const void *p2)
3814 const struct constraint *c1 = (const struct constraint *)p1;
3815 const struct constraint *c2 = (const struct constraint *)p2;
3816 unsigned size = isl_min(c1->size, c2->size);
3817 return isl_seq_cmp(c1->c, c2->c, size);
3820 static struct isl_basic_map *isl_basic_map_sort_constraints(
3821 struct isl_basic_map *bmap)
3824 struct constraint *c;
3829 total = isl_basic_map_total_dim(bmap);
3830 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
3833 for (i = 0; i < bmap->n_ineq; ++i) {
3835 c[i].c = bmap->ineq[i];
3837 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
3838 for (i = 0; i < bmap->n_ineq; ++i)
3839 bmap->ineq[i] = c[i].c;
3843 isl_basic_map_free(bmap);
3847 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
3851 if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
3853 bmap = isl_basic_map_convex_hull(bmap);
3854 bmap = isl_basic_map_sort_constraints(bmap);
3855 F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
3859 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
3861 return (struct isl_basic_set *)isl_basic_map_normalize(
3862 (struct isl_basic_map *)bset);
3865 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
3866 const struct isl_basic_map *bmap2)
3873 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
3874 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
3875 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
3876 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
3877 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
3878 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
3879 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
3880 F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
3882 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
3884 if (F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
3886 if (bmap1->n_eq != bmap2->n_eq)
3887 return bmap1->n_eq - bmap2->n_eq;
3888 if (bmap1->n_ineq != bmap2->n_ineq)
3889 return bmap1->n_ineq - bmap2->n_ineq;
3890 if (bmap1->n_div != bmap2->n_div)
3891 return bmap1->n_div - bmap2->n_div;
3892 total = isl_basic_map_total_dim(bmap1);
3893 for (i = 0; i < bmap1->n_eq; ++i) {
3894 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
3898 for (i = 0; i < bmap1->n_ineq; ++i) {
3899 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
3903 for (i = 0; i < bmap1->n_div; ++i) {
3904 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
3911 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
3912 struct isl_basic_map *bmap2)
3914 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
3917 static int qsort_bmap_cmp(const void *p1, const void *p2)
3919 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
3920 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
3922 return isl_basic_map_fast_cmp(bmap1, bmap2);
3925 /* We normalize in place, but if anything goes wrong we need
3926 * to return NULL, so we need to make sure we don't change the
3927 * meaning of any possible other copies of map.
3929 struct isl_map *isl_map_normalize(struct isl_map *map)
3932 struct isl_basic_map *bmap;
3936 if (F_ISSET(map, ISL_MAP_NORMALIZED))
3938 for (i = 0; i < map->n; ++i) {
3939 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
3942 isl_basic_map_free(map->p[i]);
3945 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
3946 F_SET(map, ISL_MAP_NORMALIZED);
3947 map = isl_map_remove_empty_parts(map);
3950 for (i = map->n - 1; i >= 1; --i) {
3951 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
3953 isl_basic_map_free(map->p[i-1]);
3954 for (j = i; j < map->n; ++j)
3955 map->p[j-1] = map->p[j];
3965 struct isl_set *isl_set_normalize(struct isl_set *set)
3967 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
3970 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
3980 if (!isl_dim_equal(map1->dim, map2->dim))
3983 map1 = isl_map_copy(map1);
3984 map2 = isl_map_copy(map2);
3985 map1 = isl_map_normalize(map1);
3986 map2 = isl_map_normalize(map2);
3989 equal = map1->n == map2->n;
3990 for (i = 0; equal && i < map1->n; ++i) {
3991 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4004 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4006 return isl_map_fast_is_equal((struct isl_map *)set1,
4007 (struct isl_map *)set2);
4010 /* Return an interval that ranges from min to max (inclusive)
4012 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4013 isl_int min, isl_int max)
4016 struct isl_basic_set *bset = NULL;
4018 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4022 k = isl_basic_set_alloc_inequality(bset);
4025 isl_int_set_si(bset->ineq[k][1], 1);
4026 isl_int_neg(bset->ineq[k][0], min);
4028 k = isl_basic_set_alloc_inequality(bset);
4031 isl_int_set_si(bset->ineq[k][1], -1);
4032 isl_int_set(bset->ineq[k][0], max);
4036 isl_basic_set_free(bset);
4040 /* Return the Cartesian product of the basic sets in list (in the given order).
4042 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4050 struct isl_basic_set *product = NULL;
4054 isl_assert(list->ctx, list->n > 0, goto error);
4055 isl_assert(list->ctx, list->p[0], goto error);
4056 nparam = isl_basic_set_n_param(list->p[0]);
4057 dim = isl_basic_set_n_dim(list->p[0]);
4058 extra = list->p[0]->n_div;
4059 n_eq = list->p[0]->n_eq;
4060 n_ineq = list->p[0]->n_ineq;
4061 for (i = 1; i < list->n; ++i) {
4062 isl_assert(list->ctx, list->p[i], goto error);
4063 isl_assert(list->ctx,
4064 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4065 dim += isl_basic_set_n_dim(list->p[i]);
4066 extra += list->p[i]->n_div;
4067 n_eq += list->p[i]->n_eq;
4068 n_ineq += list->p[i]->n_ineq;
4070 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4075 for (i = 0; i < list->n; ++i) {
4076 isl_basic_set_add_constraints(product,
4077 isl_basic_set_copy(list->p[i]), dim);
4078 dim += isl_basic_set_n_dim(list->p[i]);
4080 isl_basic_set_list_free(list);
4083 isl_basic_set_free(product);
4084 isl_basic_set_list_free(list);
4088 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4096 bset = isl_basic_set_copy(bset);
4097 bset = isl_basic_set_normalize(bset);
4100 total = isl_basic_set_total_dim(bset);
4101 isl_hash_byte(hash, bset->n_eq & 0xFF);
4102 for (i = 0; i < bset->n_eq; ++i) {
4104 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4105 isl_hash_hash(hash, c_hash);
4107 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4108 for (i = 0; i < bset->n_ineq; ++i) {
4110 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4111 isl_hash_hash(hash, c_hash);
4113 isl_hash_byte(hash, bset->n_div & 0xFF);
4114 for (i = 0; i < bset->n_div; ++i) {
4116 if (isl_int_is_zero(bset->div[i][0]))
4118 isl_hash_byte(hash, i & 0xFF);
4119 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4120 isl_hash_hash(hash, c_hash);
4122 isl_basic_set_free(bset);
4126 uint32_t isl_set_get_hash(struct isl_set *set)
4133 set = isl_set_copy(set);
4134 set = isl_set_normalize(set);
4138 hash = isl_hash_init();
4139 for (i = 0; i < set->n; ++i) {
4141 bset_hash = isl_basic_set_get_hash(set->p[i]);
4142 isl_hash_hash(hash, bset_hash);
4150 /* Check if the value for dimension dim is completely determined
4151 * by the values of the other parameters and variables.
4152 * That is, check if dimension dim is involved in an equality.
4154 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4161 nparam = isl_basic_set_n_param(bset);
4162 for (i = 0; i < bset->n_eq; ++i)
4163 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4168 /* Check if the value for dimension dim is completely determined
4169 * by the values of the other parameters and variables.
4170 * That is, check if dimension dim is involved in an equality
4171 * for each of the subsets.
4173 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4179 for (i = 0; i < set->n; ++i) {
4181 unique = isl_basic_set_dim_is_unique(set->p[i], dim);