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_cow(bmap->dim);
1931 bmap->dim->n_out += bmap->dim->nparam + bmap->dim->n_in + bmap->n_div;
1932 bmap->dim->nparam = 0;
1933 bmap->dim->n_in = 0;
1934 bmap->extra -= bmap->n_div;
1936 bmap = isl_basic_map_finalize(bmap);
1937 return (struct isl_basic_set *)bmap;
1942 struct isl_basic_map *isl_basic_map_overlying_set(
1943 struct isl_basic_set *bset, struct isl_basic_map *like)
1945 struct isl_basic_map *bmap;
1946 struct isl_ctx *ctx;
1953 isl_assert(ctx, bset->n_div == 0, goto error);
1954 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
1955 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
1957 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
1958 isl_basic_map_free(like);
1959 return (struct isl_basic_map *)bset;
1961 bset = isl_basic_set_cow(bset);
1964 total = bset->dim->n_out + bset->extra;
1965 bmap = (struct isl_basic_map *)bset;
1966 isl_dim_free(bmap->dim);
1967 bmap->dim = isl_dim_copy(like->dim);
1970 bmap->n_div = like->n_div;
1971 bmap->extra += like->n_div;
1974 ltotal = total - bmap->extra + like->extra;
1977 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
1978 bmap->extra * (1 + 1 + total));
1979 if (isl_blk_is_error(bmap->block2))
1981 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
1985 for (i = 0; i < bmap->extra; ++i)
1986 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
1987 for (i = 0; i < like->n_div; ++i) {
1988 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
1989 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
1991 bmap = isl_basic_map_extend_constraints(bmap,
1992 0, 2 * like->n_div);
1993 for (i = 0; i < like->n_div; ++i)
1994 if (add_div_constraints(bmap, i) < 0)
1997 isl_basic_map_free(like);
1998 bmap = isl_basic_map_simplify(bmap);
1999 bmap = isl_basic_map_finalize(bmap);
2002 isl_basic_map_free(like);
2003 isl_basic_set_free(bset);
2007 struct isl_basic_set *isl_basic_set_from_underlying_set(
2008 struct isl_basic_set *bset, struct isl_basic_set *like)
2010 return (struct isl_basic_set *)
2011 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2014 struct isl_set *isl_set_from_underlying_set(
2015 struct isl_set *set, struct isl_basic_set *like)
2021 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2023 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2024 isl_basic_set_free(like);
2027 set = isl_set_cow(set);
2030 for (i = 0; i < set->n; ++i) {
2031 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2032 isl_basic_set_copy(like));
2036 isl_dim_free(set->dim);
2037 set->dim = isl_dim_copy(like->dim);
2040 isl_basic_set_free(like);
2043 isl_basic_set_free(like);
2048 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2052 map = isl_map_cow(map);
2055 map->dim = isl_dim_cow(map->dim);
2059 for (i = 1; i < map->n; ++i)
2060 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2062 for (i = 0; i < map->n; ++i) {
2063 map->p[i] = (struct isl_basic_map *)
2064 isl_basic_map_underlying_set(map->p[i]);
2069 map->dim->n_out += map->dim->nparam + map->dim->n_in;
2071 map->dim->n_out = map->p[0]->dim->n_out;
2072 map->dim->nparam = 0;
2074 return (struct isl_set *)map;
2080 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2082 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2085 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2087 struct isl_basic_set *domain;
2091 n_out = isl_basic_map_n_out(bmap);
2092 domain = isl_basic_set_from_basic_map(bmap);
2093 return isl_basic_set_project_out(domain, n_out, 0);
2096 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2098 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2101 struct isl_set *isl_map_range(struct isl_map *map)
2104 struct isl_set *set;
2108 map = isl_map_cow(map);
2112 set = (struct isl_set *) map;
2113 if (set->dim->n_in != 0) {
2114 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2118 for (i = 0; i < map->n; ++i) {
2119 set->p[i] = isl_basic_map_range(map->p[i]);
2123 F_CLR(set, ISL_MAP_DISJOINT);
2124 F_CLR(set, ISL_SET_NORMALIZED);
2131 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2134 struct isl_map *map = NULL;
2136 set = isl_set_cow(set);
2139 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2140 map = (struct isl_map *)set;
2141 for (i = 0; i < set->n; ++i) {
2142 map->p[i] = isl_basic_map_from_basic_set(
2143 set->p[i], isl_dim_copy(dim));
2147 isl_dim_free(map->dim);
2156 struct isl_set *isl_set_from_map(struct isl_map *map)
2159 struct isl_set *set = NULL;
2163 map = isl_map_cow(map);
2166 map->dim = isl_dim_cow(map->dim);
2169 map->dim->n_out += map->dim->n_in;
2171 set = (struct isl_set *)map;
2172 for (i = 0; i < map->n; ++i) {
2173 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2183 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2185 struct isl_map *map;
2189 isl_assert(dim->ctx, n >= 0, return NULL);
2190 map = isl_alloc(dim->ctx, struct isl_map,
2191 sizeof(struct isl_map) +
2192 n * sizeof(struct isl_basic_map *));
2196 map->ctx = dim->ctx;
2197 isl_ctx_ref(map->ctx);
2209 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2210 unsigned nparam, unsigned in, unsigned out, int n,
2213 struct isl_map *map;
2214 struct isl_dim *dims;
2216 dims = isl_dim_alloc(ctx, nparam, in, out);
2220 map = isl_map_alloc_dim(dims, n, flags);
2224 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2225 unsigned nparam, unsigned in, unsigned out)
2227 struct isl_basic_map *bmap;
2228 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2229 bmap = isl_basic_map_set_to_empty(bmap);
2233 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2235 struct isl_basic_set *bset;
2236 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2237 bset = isl_basic_set_set_to_empty(bset);
2241 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2243 struct isl_basic_map *bmap;
2246 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2247 bmap = isl_basic_map_set_to_empty(bmap);
2251 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2253 struct isl_basic_map *bmap;
2256 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2257 bmap = isl_basic_map_set_to_empty(bmap);
2261 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2263 struct isl_basic_set *bset;
2266 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2267 bset = isl_basic_set_set_to_empty(bset);
2271 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2273 struct isl_basic_map *bmap;
2274 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2278 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2280 struct isl_basic_set *bset;
2281 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2285 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2289 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2292 struct isl_map *isl_map_empty(struct isl_ctx *ctx,
2293 unsigned nparam, unsigned in, unsigned out)
2295 return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
2298 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2302 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2305 struct isl_set *isl_set_empty(struct isl_dim *dim)
2307 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2310 struct isl_set *isl_set_empty_like(struct isl_set *model)
2314 return isl_set_empty(isl_dim_copy(model->dim));
2317 struct isl_set *isl_set_universe(struct isl_dim *dim)
2319 struct isl_set *set;
2322 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2323 set = isl_set_add(set, isl_basic_set_universe(dim));
2327 struct isl_map *isl_map_dup(struct isl_map *map)
2330 struct isl_map *dup;
2334 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2335 for (i = 0; i < map->n; ++i)
2336 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2340 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2344 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2345 isl_assert(map->ctx, map->n < map->size, goto error);
2346 map->p[map->n] = bmap;
2348 F_CLR(map, ISL_MAP_NORMALIZED);
2354 isl_basic_map_free(bmap);
2358 void isl_map_free(struct isl_map *map)
2368 isl_ctx_deref(map->ctx);
2369 for (i = 0; i < map->n; ++i)
2370 isl_basic_map_free(map->p[i]);
2371 isl_dim_free(map->dim);
2375 struct isl_map *isl_map_extend(struct isl_map *base,
2376 unsigned nparam, unsigned n_in, unsigned n_out)
2380 base = isl_map_cow(base);
2384 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2387 for (i = 0; i < base->n; ++i) {
2388 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2389 isl_dim_copy(base->dim), 0, 0, 0);
2399 struct isl_set *isl_set_extend(struct isl_set *base,
2400 unsigned nparam, unsigned dim)
2402 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2406 static struct isl_basic_map *isl_basic_map_fix_var(struct isl_basic_map *bmap,
2407 unsigned var, int value)
2411 bmap = isl_basic_map_cow(bmap);
2412 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2413 j = isl_basic_map_alloc_equality(bmap);
2416 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2417 isl_int_set_si(bmap->eq[j][1 + var], -1);
2418 isl_int_set_si(bmap->eq[j][0], value);
2419 bmap = isl_basic_map_simplify(bmap);
2420 return isl_basic_map_finalize(bmap);
2422 isl_basic_map_free(bmap);
2426 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2427 unsigned input, int value)
2431 isl_assert(bmap->ctx, input < isl_basic_map_n_in(bmap), goto error);
2432 return isl_basic_map_fix_var(bmap, isl_basic_map_n_param(bmap) + input,
2435 isl_basic_map_free(bmap);
2439 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2440 unsigned dim, int value)
2444 isl_assert(bset->ctx, dim < isl_basic_set_n_dim(bset), goto error);
2445 return (struct isl_basic_set *)
2446 isl_basic_map_fix_var((struct isl_basic_map *)bset,
2447 isl_basic_set_n_param(bset) + dim, value);
2449 isl_basic_set_free(bset);
2453 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2454 unsigned input, int value)
2458 map = isl_map_cow(map);
2462 isl_assert(ctx, input < isl_map_n_in(map), goto error);
2463 for (i = 0; i < map->n; ++i) {
2464 map->p[i] = isl_basic_map_fix_input_si(map->p[i], input, value);
2468 F_CLR(map, ISL_MAP_NORMALIZED);
2475 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2479 set = isl_set_cow(set);
2483 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2484 for (i = 0; i < set->n; ++i) {
2485 set->p[i] = isl_basic_set_fix_dim_si(set->p[i], dim, value);
2495 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2496 unsigned dim, isl_int value)
2501 bset = isl_basic_set_cow(bset);
2502 bset = isl_basic_set_extend_constraints(bset, 0, 1);
2503 j = isl_basic_set_alloc_inequality(bset);
2506 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2507 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2508 isl_int_neg(bset->ineq[j][0], value);
2509 bset = isl_basic_set_simplify(bset);
2510 return isl_basic_set_finalize(bset);
2512 isl_basic_set_free(bset);
2516 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2521 set = isl_set_cow(set);
2525 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2526 for (i = 0; i < set->n; ++i) {
2527 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2537 struct isl_map *isl_map_reverse(struct isl_map *map)
2542 map = isl_map_cow(map);
2546 map->dim = isl_dim_reverse(map->dim);
2549 for (i = 0; i < map->n; ++i) {
2550 map->p[i] = isl_basic_map_reverse(map->p[i]);
2554 F_CLR(map, ISL_MAP_NORMALIZED);
2561 struct isl_map *isl_basic_map_lexmax(
2562 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2563 struct isl_set **empty)
2565 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2568 struct isl_map *isl_basic_map_lexmin(
2569 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2570 struct isl_set **empty)
2572 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2575 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2577 struct isl_basic_map *bmap = NULL;
2578 struct isl_basic_set *dom = NULL;
2579 struct isl_map *min;
2580 struct isl_dim *param_dim;
2584 bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2587 param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2588 dom = isl_basic_set_universe(param_dim);
2591 min = isl_basic_map_lexmin(bmap, dom, NULL);
2592 return isl_map_range(min);
2594 isl_basic_map_free(bmap);
2598 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2605 off = isl_dim_total(bmap->dim);
2606 for (i = 0; i < bmap->n_div; ++i) {
2607 if (isl_int_is_zero(bmap->div[i][0]))
2608 return isl_pip_basic_map_compute_divs(bmap);
2609 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2612 return isl_map_from_basic_map(bmap);
2614 isl_basic_map_free(bmap);
2618 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2621 struct isl_map *res;
2627 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2628 for (i = 1 ; i < map->n; ++i) {
2630 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2631 if (F_ISSET(map, ISL_MAP_DISJOINT))
2632 res = isl_map_union_disjoint(res, r2);
2634 res = isl_map_union(res, r2);
2641 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2643 return (struct isl_set *)
2644 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2647 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2649 return (struct isl_set *)
2650 isl_map_compute_divs((struct isl_map *)set);
2653 struct isl_set *isl_map_domain(struct isl_map *map)
2656 struct isl_set *set;
2661 map = isl_map_cow(map);
2665 set = (struct isl_set *)map;
2666 set->dim = isl_dim_domain(set->dim);
2669 for (i = 0; i < map->n; ++i) {
2670 set->p[i] = isl_basic_map_domain(map->p[i]);
2674 F_CLR(set, ISL_MAP_DISJOINT);
2675 F_CLR(set, ISL_SET_NORMALIZED);
2682 struct isl_map *isl_map_union_disjoint(
2683 struct isl_map *map1, struct isl_map *map2)
2687 struct isl_map *map = NULL;
2701 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2703 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2704 F_ISSET(map2, ISL_MAP_DISJOINT))
2705 FL_SET(flags, ISL_MAP_DISJOINT);
2707 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2708 map1->n + map2->n, flags);
2711 for (i = 0; i < map1->n; ++i) {
2712 map = isl_map_add(map,
2713 isl_basic_map_copy(map1->p[i]));
2717 for (i = 0; i < map2->n; ++i) {
2718 map = isl_map_add(map,
2719 isl_basic_map_copy(map2->p[i]));
2733 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
2735 map1 = isl_map_union_disjoint(map1, map2);
2739 F_CLR(map1, ISL_MAP_DISJOINT);
2743 struct isl_set *isl_set_union_disjoint(
2744 struct isl_set *set1, struct isl_set *set2)
2746 return (struct isl_set *)
2747 isl_map_union_disjoint(
2748 (struct isl_map *)set1, (struct isl_map *)set2);
2751 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
2753 return (struct isl_set *)
2754 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
2757 struct isl_map *isl_map_intersect_range(
2758 struct isl_map *map, struct isl_set *set)
2761 struct isl_map *result;
2767 if (F_ISSET(map, ISL_MAP_DISJOINT) &&
2768 F_ISSET(set, ISL_MAP_DISJOINT))
2769 FL_SET(flags, ISL_MAP_DISJOINT);
2771 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
2772 map->n * set->n, flags);
2775 for (i = 0; i < map->n; ++i)
2776 for (j = 0; j < set->n; ++j) {
2777 result = isl_map_add(result,
2778 isl_basic_map_intersect_range(
2779 isl_basic_map_copy(map->p[i]),
2780 isl_basic_set_copy(set->p[j])));
2793 struct isl_map *isl_map_intersect_domain(
2794 struct isl_map *map, struct isl_set *set)
2796 return isl_map_reverse(
2797 isl_map_intersect_range(isl_map_reverse(map), set));
2800 struct isl_map *isl_map_apply_domain(
2801 struct isl_map *map1, struct isl_map *map2)
2805 map1 = isl_map_reverse(map1);
2806 map1 = isl_map_apply_range(map1, map2);
2807 return isl_map_reverse(map1);
2814 struct isl_map *isl_map_apply_range(
2815 struct isl_map *map1, struct isl_map *map2)
2817 struct isl_dim *dim_result;
2818 struct isl_map *result;
2827 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
2828 isl_dim_copy(map2->dim));
2830 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
2833 for (i = 0; i < map1->n; ++i)
2834 for (j = 0; j < map2->n; ++j) {
2835 result = isl_map_add(result,
2836 isl_basic_map_apply_range(
2837 isl_basic_map_copy(map1->p[i]),
2838 isl_basic_map_copy(map2->p[j])));
2844 if (result && result->n <= 1)
2845 F_SET(result, ISL_MAP_DISJOINT);
2854 * returns range - domain
2856 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
2858 struct isl_basic_set *bset;
2865 dim = isl_basic_map_n_in(bmap);
2866 nparam = isl_basic_map_n_param(bmap);
2867 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
2868 bset = isl_basic_set_from_basic_map(bmap);
2869 bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
2870 bset = isl_basic_set_swap_vars(bset, 2*dim);
2871 for (i = 0; i < dim; ++i) {
2872 int j = isl_basic_map_alloc_equality(
2873 (struct isl_basic_map *)bset);
2876 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
2877 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
2878 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
2879 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
2881 return isl_basic_set_project_out(bset, 2*dim, 0);
2883 isl_basic_map_free(bmap);
2888 * returns range - domain
2890 struct isl_set *isl_map_deltas(struct isl_map *map)
2893 struct isl_set *result;
2898 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
2899 result = isl_set_alloc(map->ctx, isl_map_n_param(map),
2900 isl_map_n_in(map), map->n, map->flags);
2903 for (i = 0; i < map->n; ++i)
2904 result = isl_set_add(result,
2905 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
2913 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
2915 struct isl_basic_map *bmap;
2923 nparam = dims->nparam;
2925 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
2929 for (i = 0; i < dim; ++i) {
2930 int j = isl_basic_map_alloc_equality(bmap);
2933 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2934 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
2935 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
2937 return isl_basic_map_finalize(bmap);
2939 isl_basic_map_free(bmap);
2943 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
2945 struct isl_dim *dim = isl_dim_map(set_dim);
2948 return basic_map_identity(dim);
2951 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
2953 if (!model || !model->dim)
2955 isl_assert(model->ctx,
2956 model->dim->n_in == model->dim->n_out, return NULL);
2957 return basic_map_identity(isl_dim_copy(model->dim));
2960 static struct isl_map *map_identity(struct isl_dim *dim)
2962 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
2963 return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
2966 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
2968 struct isl_dim *dim = isl_dim_map(set_dim);
2971 return map_identity(dim);
2974 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
2976 if (!model || !model->dim)
2978 isl_assert(model->ctx,
2979 model->dim->n_in == model->dim->n_out, return NULL);
2980 return map_identity(isl_dim_copy(model->dim));
2983 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
2985 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
2988 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
2990 return isl_map_is_subset(
2991 (struct isl_map *)set1, (struct isl_map *)set2);
2994 int isl_basic_map_is_subset(
2995 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2998 struct isl_map *map1;
2999 struct isl_map *map2;
3001 if (!bmap1 || !bmap2)
3004 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3005 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3007 is_subset = isl_map_is_subset(map1, map2);
3015 int isl_basic_map_is_equal(
3016 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3020 if (!bmap1 || !bmap2)
3022 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3025 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3029 int isl_basic_set_is_equal(
3030 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3032 return isl_basic_map_is_equal(
3033 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3036 int isl_map_is_empty(struct isl_map *map)
3043 for (i = 0; i < map->n; ++i) {
3044 is_empty = isl_basic_map_is_empty(map->p[i]);
3053 int isl_set_is_empty(struct isl_set *set)
3055 return isl_map_is_empty((struct isl_map *)set);
3058 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3062 struct isl_map *diff;
3067 if (isl_map_is_empty(map1))
3070 if (isl_map_is_empty(map2))
3073 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3077 is_subset = isl_map_is_empty(diff);
3083 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3089 is_subset = isl_map_is_subset(map1, map2);
3092 is_subset = isl_map_is_subset(map2, map1);
3096 int isl_basic_map_is_strict_subset(
3097 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3101 if (!bmap1 || !bmap2)
3103 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3106 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3107 if (is_subset == -1)
3112 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3118 total = 1 + isl_basic_map_total_dim(bmap);
3119 if (total != vec->size)
3124 for (i = 0; i < bmap->n_eq; ++i) {
3125 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3126 if (!isl_int_is_zero(s)) {
3132 for (i = 0; i < bmap->n_ineq; ++i) {
3133 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3134 if (isl_int_is_neg(s)) {
3145 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3149 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3152 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3154 struct isl_basic_set *bset = NULL;
3155 struct isl_vec *sample = NULL;
3162 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3165 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3166 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3167 copy = isl_basic_map_convex_hull(copy);
3168 empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3169 isl_basic_map_free(copy);
3173 total = 1 + isl_basic_map_total_dim(bmap);
3174 if (bmap->sample && bmap->sample->size == total) {
3175 int contains = basic_map_contains(bmap, bmap->sample);
3181 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3184 sample = isl_basic_set_sample(bset);
3187 empty = sample->size == 0;
3189 isl_vec_free(bmap->ctx, bmap->sample);
3190 bmap->sample = sample;
3195 struct isl_map *isl_basic_map_union(
3196 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3198 struct isl_map *map;
3199 if (!bmap1 || !bmap2)
3202 isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3204 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3207 map = isl_map_add(map, bmap1);
3208 map = isl_map_add(map, bmap2);
3211 isl_basic_map_free(bmap1);
3212 isl_basic_map_free(bmap2);
3216 struct isl_set *isl_basic_set_union(
3217 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3219 return (struct isl_set *)isl_basic_map_union(
3220 (struct isl_basic_map *)bset1,
3221 (struct isl_basic_map *)bset2);
3224 /* Order divs such that any div only depends on previous divs */
3225 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3228 unsigned off = isl_dim_total(bmap->dim);
3230 for (i = 0; i < bmap->n_div; ++i) {
3232 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3236 swap_div(bmap, i, pos);
3242 /* Look for a div in dst that corresponds to the div "div" in src.
3243 * The divs before "div" in src and dst are assumed to be the same.
3245 * Returns -1 if no corresponding div was found and the position
3246 * of the corresponding div in dst otherwise.
3248 static int find_div(struct isl_basic_map *dst,
3249 struct isl_basic_map *src, unsigned div)
3253 unsigned total = isl_dim_total(src->dim);
3255 isl_assert(dst->ctx, div <= dst->n_div, return -1);
3256 for (i = div; i < dst->n_div; ++i)
3257 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3258 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3259 dst->n_div - div) == -1)
3264 struct isl_basic_map *isl_basic_map_align_divs(
3265 struct isl_basic_map *dst, struct isl_basic_map *src)
3268 unsigned total = isl_dim_total(src->dim);
3273 if (src->n_div == 0)
3276 for (i = 0; i < src->n_div; ++i)
3277 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3279 src = order_divs(src);
3280 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3281 src->n_div, 0, 2 * src->n_div);
3284 for (i = 0; i < src->n_div; ++i) {
3285 int j = find_div(dst, src, i);
3287 j = isl_basic_map_alloc_div(dst);
3290 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3291 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3292 if (add_div_constraints(dst, j) < 0)
3296 swap_div(dst, i, j);
3300 isl_basic_map_free(dst);
3304 struct isl_basic_set *isl_basic_set_align_divs(
3305 struct isl_basic_set *dst, struct isl_basic_set *src)
3307 return (struct isl_basic_set *)isl_basic_map_align_divs(
3308 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3311 struct isl_map *isl_map_align_divs(struct isl_map *map)
3315 map = isl_map_compute_divs(map);
3316 map = isl_map_cow(map);
3320 for (i = 1; i < map->n; ++i)
3321 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3322 for (i = 1; i < map->n; ++i)
3323 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3325 F_CLR(map, ISL_MAP_NORMALIZED);
3329 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3330 struct isl_basic_map *src, isl_int *c,
3331 unsigned len, int oppose)
3333 struct isl_basic_map *copy = NULL;
3338 copy = isl_basic_map_copy(src);
3339 copy = isl_basic_map_cow(copy);
3342 copy = isl_basic_map_extend_constraints(copy, 0, 1);
3343 k = isl_basic_map_alloc_inequality(copy);
3347 isl_seq_neg(copy->ineq[k], c, len);
3349 isl_seq_cpy(copy->ineq[k], c, len);
3350 total = 1 + isl_basic_map_total_dim(copy);
3351 isl_seq_clr(copy->ineq[k]+len, total - len);
3352 isl_inequality_negate(copy, k);
3353 copy = isl_basic_map_simplify(copy);
3354 copy = isl_basic_map_finalize(copy);
3355 is_empty = isl_basic_map_is_empty(copy);
3359 dst = isl_map_add(dst, copy);
3361 isl_basic_map_free(copy);
3364 isl_basic_map_free(copy);
3369 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3373 struct isl_map *rest = NULL;
3375 unsigned total = isl_basic_map_total_dim(bmap);
3382 if (F_ISSET(map, ISL_MAP_DISJOINT))
3383 FL_SET(flags, ISL_MAP_DISJOINT);
3385 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3386 rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3390 for (i = 0; i < map->n; ++i) {
3391 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3396 for (j = 0; j < map->n; ++j)
3397 map->p[j] = isl_basic_map_cow(map->p[j]);
3399 for (i = 0; i < bmap->n_eq; ++i) {
3400 for (j = 0; j < map->n; ++j) {
3401 rest = add_cut_constraint(rest,
3402 map->p[j], bmap->eq[i], 1+total, 0);
3406 rest = add_cut_constraint(rest,
3407 map->p[j], bmap->eq[i], 1+total, 1);
3411 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3415 k = isl_basic_map_alloc_equality(map->p[j]);
3418 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3419 isl_seq_clr(map->p[j]->eq[k]+1+total,
3420 map->p[j]->n_div - bmap->n_div);
3424 for (i = 0; i < bmap->n_ineq; ++i) {
3425 for (j = 0; j < map->n; ++j) {
3426 rest = add_cut_constraint(rest,
3427 map->p[j], bmap->ineq[i], 1+total, 0);
3431 map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3435 k = isl_basic_map_alloc_inequality(map->p[j]);
3438 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3439 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3440 map->p[j]->n_div - bmap->n_div);
3452 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3458 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3460 if (isl_map_is_empty(map2)) {
3465 map1 = isl_map_compute_divs(map1);
3466 map2 = isl_map_compute_divs(map2);
3470 for (i = 0; map1 && i < map2->n; ++i)
3471 map1 = subtract(map1, map2->p[i]);
3481 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3483 return (struct isl_set *)
3485 (struct isl_map *)set1, (struct isl_map *)set2);
3488 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3492 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3493 map = isl_map_intersect_domain(map, set);
3494 set = isl_map_range(map);
3502 /* There is no need to cow as removing empty parts doesn't change
3503 * the meaning of the set.
3505 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3512 for (i = map->n-1; i >= 0; --i) {
3513 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3515 isl_basic_map_free(map->p[i]);
3516 if (i != map->n-1) {
3517 F_CLR(map, ISL_MAP_NORMALIZED);
3518 map->p[i] = map->p[map->n-1];
3526 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3528 return (struct isl_set *)
3529 isl_map_remove_empty_parts((struct isl_map *)set);
3532 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3534 struct isl_basic_set *bset;
3535 if (!set || set->n == 0)
3537 bset = set->p[set->n-1];
3538 isl_assert(set->ctx, F_ISSET(bset, ISL_BASIC_SET_FINAL), return NULL);
3539 return isl_basic_set_copy(bset);
3542 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3543 struct isl_basic_set *bset)
3549 for (i = set->n-1; i >= 0; --i) {
3550 if (set->p[i] != bset)
3552 set = isl_set_cow(set);
3555 isl_basic_set_free(set->p[i]);
3556 if (i != set->n-1) {
3557 F_CLR(set, ISL_SET_NORMALIZED);
3558 set->p[i] = set->p[set->n-1];
3563 isl_basic_set_free(bset);
3567 isl_basic_set_free(bset);
3571 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3572 * for any common value of the parameters and dimensions preceding dim
3573 * in both basic sets, the values of dimension pos in bset1 are
3574 * smaller or larger than those in bset2.
3577 * 1 if bset1 follows bset2
3578 * -1 if bset1 precedes bset2
3579 * 0 if bset1 and bset2 are incomparable
3580 * -2 if some error occurred.
3582 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3583 struct isl_basic_set *bset2, int pos)
3585 struct isl_dim *dims;
3586 struct isl_basic_map *bmap1 = NULL;
3587 struct isl_basic_map *bmap2 = NULL;
3588 struct isl_ctx *ctx;
3589 struct isl_vec *obj;
3592 unsigned dim1, dim2;
3594 enum isl_lp_result res;
3597 if (!bset1 || !bset2)
3600 nparam = isl_basic_set_n_param(bset1);
3601 dim1 = isl_basic_set_n_dim(bset1);
3602 dim2 = isl_basic_set_n_dim(bset2);
3603 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3604 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3605 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3606 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3607 if (!bmap1 || !bmap2)
3609 bmap1 = isl_basic_map_extend(bmap1, nparam,
3610 pos, (dim1 - pos) + (dim2 - pos),
3611 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3612 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
3615 total = isl_basic_map_total_dim(bmap1);
3617 obj = isl_vec_alloc(ctx, total);
3618 isl_seq_clr(obj->block.data, total);
3619 isl_int_set_si(obj->block.data[nparam+pos], 1);
3620 isl_int_set_si(obj->block.data[nparam+pos+(dim1-pos)], -1);
3625 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3626 if (res == isl_lp_empty)
3628 else if (res == isl_lp_ok && isl_int_is_pos(num))
3630 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3631 res == isl_lp_unbounded)
3637 isl_basic_map_free(bmap1);
3638 isl_vec_free(ctx, obj);
3641 isl_basic_map_free(bmap1);
3642 isl_basic_map_free(bmap2);
3646 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
3647 unsigned pos, isl_int *val)
3655 total = isl_basic_map_total_dim(bmap);
3656 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
3657 for (; d+1 > pos; --d)
3658 if (!isl_int_is_zero(bmap->eq[i][1+d]))
3662 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
3664 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
3666 if (!isl_int_is_one(bmap->eq[i][1+d]))
3669 isl_int_neg(*val, bmap->eq[i][0]);
3675 static int isl_map_fast_has_fixed_var(struct isl_map *map,
3676 unsigned pos, isl_int *val)
3688 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
3691 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
3692 for (i = 1; fixed == 1 && i < map->n; ++i) {
3693 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
3694 if (fixed == 1 && isl_int_ne(tmp, v))
3698 isl_int_set(*val, v);
3704 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
3707 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
3710 /* Check if dimension dim has fixed value and if so and if val is not NULL,
3711 * then return this fixed value in *val.
3713 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
3715 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
3718 /* Check if input variable in has fixed value and if so and if val is not NULL,
3719 * then return this fixed value in *val.
3721 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
3723 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
3726 /* Check if dimension dim has an (obvious) fixed lower bound and if so
3727 * and if val is not NULL, then return this lower bound in *val.
3729 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
3730 unsigned dim, isl_int *val)
3732 int i, i_eq = -1, i_ineq = -1;
3739 total = isl_basic_set_total_dim(bset);
3740 nparam = isl_basic_set_n_param(bset);
3741 for (i = 0; i < bset->n_eq; ++i) {
3742 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
3748 for (i = 0; i < bset->n_ineq; ++i) {
3749 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
3751 if (i_eq != -1 || i_ineq != -1)
3755 if (i_eq == -1 && i_ineq == -1)
3757 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
3758 /* The coefficient should always be one due to normalization. */
3759 if (!isl_int_is_one(c[1+nparam+dim]))
3761 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
3763 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
3764 total - nparam - dim - 1) != -1)
3767 isl_int_neg(*val, c[0]);
3771 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
3772 unsigned dim, isl_int *val)
3784 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
3788 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
3790 for (i = 1; fixed == 1 && i < set->n; ++i) {
3791 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
3793 if (fixed == 1 && isl_int_ne(tmp, v))
3797 isl_int_set(*val, v);
3808 static int qsort_constraint_cmp(const void *p1, const void *p2)
3810 const struct constraint *c1 = (const struct constraint *)p1;
3811 const struct constraint *c2 = (const struct constraint *)p2;
3812 unsigned size = isl_min(c1->size, c2->size);
3813 return isl_seq_cmp(c1->c, c2->c, size);
3816 static struct isl_basic_map *isl_basic_map_sort_constraints(
3817 struct isl_basic_map *bmap)
3820 struct constraint *c;
3825 total = isl_basic_map_total_dim(bmap);
3826 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
3829 for (i = 0; i < bmap->n_ineq; ++i) {
3831 c[i].c = bmap->ineq[i];
3833 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
3834 for (i = 0; i < bmap->n_ineq; ++i)
3835 bmap->ineq[i] = c[i].c;
3839 isl_basic_map_free(bmap);
3843 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
3847 if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
3849 bmap = isl_basic_map_convex_hull(bmap);
3850 bmap = isl_basic_map_sort_constraints(bmap);
3851 F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
3855 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
3857 return (struct isl_basic_set *)isl_basic_map_normalize(
3858 (struct isl_basic_map *)bset);
3861 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
3862 const struct isl_basic_map *bmap2)
3869 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
3870 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
3871 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
3872 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
3873 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
3874 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
3875 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
3876 F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
3878 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
3880 if (F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
3882 if (bmap1->n_eq != bmap2->n_eq)
3883 return bmap1->n_eq - bmap2->n_eq;
3884 if (bmap1->n_ineq != bmap2->n_ineq)
3885 return bmap1->n_ineq - bmap2->n_ineq;
3886 if (bmap1->n_div != bmap2->n_div)
3887 return bmap1->n_div - bmap2->n_div;
3888 total = isl_basic_map_total_dim(bmap1);
3889 for (i = 0; i < bmap1->n_eq; ++i) {
3890 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
3894 for (i = 0; i < bmap1->n_ineq; ++i) {
3895 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
3899 for (i = 0; i < bmap1->n_div; ++i) {
3900 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
3907 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
3908 struct isl_basic_map *bmap2)
3910 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
3913 static int qsort_bmap_cmp(const void *p1, const void *p2)
3915 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
3916 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
3918 return isl_basic_map_fast_cmp(bmap1, bmap2);
3921 /* We normalize in place, but if anything goes wrong we need
3922 * to return NULL, so we need to make sure we don't change the
3923 * meaning of any possible other copies of map.
3925 struct isl_map *isl_map_normalize(struct isl_map *map)
3928 struct isl_basic_map *bmap;
3932 if (F_ISSET(map, ISL_MAP_NORMALIZED))
3934 for (i = 0; i < map->n; ++i) {
3935 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
3938 isl_basic_map_free(map->p[i]);
3941 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
3942 F_SET(map, ISL_MAP_NORMALIZED);
3943 map = isl_map_remove_empty_parts(map);
3946 for (i = map->n - 1; i >= 1; --i) {
3947 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
3949 isl_basic_map_free(map->p[i-1]);
3950 for (j = i; j < map->n; ++j)
3951 map->p[j-1] = map->p[j];
3961 struct isl_set *isl_set_normalize(struct isl_set *set)
3963 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
3966 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
3976 if (!isl_dim_equal(map1->dim, map2->dim))
3979 map1 = isl_map_copy(map1);
3980 map2 = isl_map_copy(map2);
3981 map1 = isl_map_normalize(map1);
3982 map2 = isl_map_normalize(map2);
3985 equal = map1->n == map2->n;
3986 for (i = 0; equal && i < map1->n; ++i) {
3987 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4000 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4002 return isl_map_fast_is_equal((struct isl_map *)set1,
4003 (struct isl_map *)set2);
4006 /* Return an interval that ranges from min to max (inclusive)
4008 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4009 isl_int min, isl_int max)
4012 struct isl_basic_set *bset = NULL;
4014 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4018 k = isl_basic_set_alloc_inequality(bset);
4021 isl_int_set_si(bset->ineq[k][1], 1);
4022 isl_int_neg(bset->ineq[k][0], min);
4024 k = isl_basic_set_alloc_inequality(bset);
4027 isl_int_set_si(bset->ineq[k][1], -1);
4028 isl_int_set(bset->ineq[k][0], max);
4032 isl_basic_set_free(bset);
4036 /* Return the Cartesian product of the basic sets in list (in the given order).
4038 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4046 struct isl_basic_set *product = NULL;
4050 isl_assert(list->ctx, list->n > 0, goto error);
4051 isl_assert(list->ctx, list->p[0], goto error);
4052 nparam = isl_basic_set_n_param(list->p[0]);
4053 dim = isl_basic_set_n_dim(list->p[0]);
4054 extra = list->p[0]->n_div;
4055 n_eq = list->p[0]->n_eq;
4056 n_ineq = list->p[0]->n_ineq;
4057 for (i = 1; i < list->n; ++i) {
4058 isl_assert(list->ctx, list->p[i], goto error);
4059 isl_assert(list->ctx,
4060 nparam == isl_basic_set_n_param(list->p[i]), goto error);
4061 dim += isl_basic_set_n_dim(list->p[i]);
4062 extra += list->p[i]->n_div;
4063 n_eq += list->p[i]->n_eq;
4064 n_ineq += list->p[i]->n_ineq;
4066 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4071 for (i = 0; i < list->n; ++i) {
4072 isl_basic_set_add_constraints(product,
4073 isl_basic_set_copy(list->p[i]), dim);
4074 dim += isl_basic_set_n_dim(list->p[i]);
4076 isl_basic_set_list_free(list);
4079 isl_basic_set_free(product);
4080 isl_basic_set_list_free(list);
4084 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4092 bset = isl_basic_set_copy(bset);
4093 bset = isl_basic_set_normalize(bset);
4096 total = isl_basic_set_total_dim(bset);
4097 isl_hash_byte(hash, bset->n_eq & 0xFF);
4098 for (i = 0; i < bset->n_eq; ++i) {
4100 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4101 isl_hash_hash(hash, c_hash);
4103 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4104 for (i = 0; i < bset->n_ineq; ++i) {
4106 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4107 isl_hash_hash(hash, c_hash);
4109 isl_hash_byte(hash, bset->n_div & 0xFF);
4110 for (i = 0; i < bset->n_div; ++i) {
4112 if (isl_int_is_zero(bset->div[i][0]))
4114 isl_hash_byte(hash, i & 0xFF);
4115 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4116 isl_hash_hash(hash, c_hash);
4118 isl_basic_set_free(bset);
4122 uint32_t isl_set_get_hash(struct isl_set *set)
4129 set = isl_set_copy(set);
4130 set = isl_set_normalize(set);
4134 hash = isl_hash_init();
4135 for (i = 0; i < set->n; ++i) {
4137 bset_hash = isl_basic_set_get_hash(set->p[i]);
4138 isl_hash_hash(hash, bset_hash);
4146 /* Check if the value for dimension dim is completely determined
4147 * by the values of the other parameters and variables.
4148 * That is, check if dimension dim is involved in an equality.
4150 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4157 nparam = isl_basic_set_n_param(bset);
4158 for (i = 0; i < bset->n_eq; ++i)
4159 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4164 /* Check if the value for dimension dim is completely determined
4165 * by the values of the other parameters and variables.
4166 * That is, check if dimension dim is involved in an equality
4167 * for each of the subsets.
4169 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4175 for (i = 0; i < set->n; ++i) {
4177 unique = isl_basic_set_dim_is_unique(set->p[i], dim);