8 #include "isl_map_private.h"
9 #include "isl_map_piplib.h"
10 #include "isl_sample.h"
13 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
14 struct isl_basic_map *bmap,
15 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
16 unsigned n_eq, unsigned n_ineq)
19 size_t row_size = 1 + nparam + n_in + n_out + extra;
21 bmap->block = isl_blk_alloc(ctx, (n_eq + n_ineq) * row_size);
22 if (isl_blk_is_error(bmap->block)) {
27 bmap->eq = isl_alloc_array(ctx, isl_int *, n_eq + n_ineq);
29 isl_blk_free(ctx, bmap->block);
35 bmap->block2 = isl_blk_empty();
38 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
39 if (isl_blk_is_error(bmap->block2)) {
41 isl_blk_free(ctx, bmap->block);
46 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
48 isl_blk_free(ctx, bmap->block2);
50 isl_blk_free(ctx, bmap->block);
56 for (i = 0; i < n_eq + n_ineq; ++i)
57 bmap->eq[i] = bmap->block.data + i * row_size;
59 for (i = 0; i < extra; ++i)
60 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
66 bmap->c_size = n_eq + n_ineq;
67 bmap->ineq = bmap->eq + n_eq;
68 bmap->nparam = nparam;
80 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
81 unsigned nparam, unsigned dim, unsigned extra,
82 unsigned n_eq, unsigned n_ineq)
84 struct isl_basic_map *bmap;
85 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
86 return (struct isl_basic_set *)bmap;
89 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
90 unsigned nparam, unsigned in, unsigned out, unsigned extra,
91 unsigned n_eq, unsigned n_ineq)
93 struct isl_basic_map *bmap;
95 bmap = isl_alloc_type(ctx, struct isl_basic_map);
99 return basic_map_init(ctx, bmap,
100 nparam, in, out, extra, n_eq, n_ineq);
103 static void dup_constraints(
104 struct isl_basic_map *dst, struct isl_basic_map *src)
107 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
109 for (i = 0; i < src->n_eq; ++i) {
110 int j = isl_basic_map_alloc_equality(dst);
111 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
114 for (i = 0; i < src->n_ineq; ++i) {
115 int j = isl_basic_map_alloc_inequality(dst);
116 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
119 for (i = 0; i < src->n_div; ++i) {
120 int j = isl_basic_map_alloc_div(dst);
121 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
123 F_SET(dst, ISL_BASIC_SET_FINAL);
126 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
128 struct isl_basic_map *dup;
132 dup = isl_basic_map_alloc(bmap->ctx, bmap->nparam,
133 bmap->n_in, bmap->n_out,
134 bmap->n_div, bmap->n_eq, bmap->n_ineq);
137 dup->flags = bmap->flags;
138 dup_constraints(dup, bmap);
139 dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
143 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
145 struct isl_basic_map *dup;
147 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
148 return (struct isl_basic_set *)dup;
151 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
156 if (F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
160 return isl_basic_set_dup(bset);
163 struct isl_set *isl_set_copy(struct isl_set *set)
172 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
177 if (F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
181 return isl_basic_map_dup(bmap);
184 struct isl_map *isl_map_copy(struct isl_map *map)
193 void isl_basic_map_free(struct isl_basic_map *bmap)
201 isl_ctx_deref(bmap->ctx);
203 isl_blk_free(bmap->ctx, bmap->block2);
205 isl_blk_free(bmap->ctx, bmap->block);
206 isl_vec_free(bmap->ctx, bmap->sample);
210 void isl_basic_set_free(struct isl_basic_set *bset)
212 isl_basic_map_free((struct isl_basic_map *)bset);
215 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
221 isl_assert(ctx, bmap->n_eq + bmap->n_ineq < bmap->c_size, return -1);
222 isl_assert(ctx, bmap->eq + bmap->n_eq <= bmap->ineq, return -1);
223 if (bmap->eq + bmap->n_eq == bmap->ineq) {
225 int j = isl_basic_map_alloc_inequality(bmap);
229 bmap->ineq[0] = bmap->ineq[j];
237 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
239 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
242 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
246 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
251 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
256 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
258 if (pos != bmap->n_eq - 1) {
260 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
261 bmap->eq[bmap->n_eq - 1] = t;
267 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
269 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
272 void isl_basic_map_inequality_to_equality(
273 struct isl_basic_map *bmap, unsigned pos)
278 bmap->ineq[pos] = bmap->ineq[0];
279 bmap->ineq[0] = bmap->eq[bmap->n_eq];
280 bmap->eq[bmap->n_eq] = t;
286 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
292 isl_assert(ctx, (bmap->ineq - bmap->eq) + bmap->n_ineq < bmap->c_size,
294 F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
295 F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
296 return bmap->n_ineq++;
299 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
301 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
304 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
308 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
313 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
315 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
318 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
323 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
325 if (pos != bmap->n_ineq - 1) {
327 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
328 bmap->ineq[bmap->n_ineq - 1] = t;
334 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
336 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
339 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
343 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
344 return bmap->n_div++;
347 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
351 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
356 /* Copy constraint from src to dst, putting the vars of src at offset
357 * dim_off in dst and the divs of src at offset div_off in dst.
358 * If both sets are actually map, then dim_off applies to the input
361 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
362 struct isl_basic_map *src_map, isl_int *src,
363 unsigned in_off, unsigned out_off, unsigned div_off)
365 isl_int_set(dst[0], src[0]);
366 isl_seq_cpy(dst+1, src+1, isl_min(dst_map->nparam, src_map->nparam));
367 if (dst_map->nparam > src_map->nparam)
368 isl_seq_clr(dst+1+src_map->nparam,
369 dst_map->nparam - src_map->nparam);
370 isl_seq_clr(dst+1+dst_map->nparam, in_off);
371 isl_seq_cpy(dst+1+dst_map->nparam+in_off,
372 src+1+src_map->nparam,
373 isl_min(dst_map->n_in-in_off, src_map->n_in));
374 if (dst_map->n_in-in_off > src_map->n_in)
375 isl_seq_clr(dst+1+dst_map->nparam+in_off+src_map->n_in,
376 dst_map->n_in - in_off - src_map->n_in);
377 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in, out_off);
378 isl_seq_cpy(dst+1+dst_map->nparam+dst_map->n_in+out_off,
379 src+1+src_map->nparam+src_map->n_in,
380 isl_min(dst_map->n_out-out_off, src_map->n_out));
381 if (dst_map->n_out-out_off > src_map->n_out)
382 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+out_off+src_map->n_out,
383 dst_map->n_out - out_off - src_map->n_out);
384 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out, div_off);
385 isl_seq_cpy(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out+div_off,
386 src+1+src_map->nparam+src_map->n_in+src_map->n_out,
387 isl_min(dst_map->extra-div_off, src_map->extra));
388 if (dst_map->extra-div_off > src_map->extra)
389 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out+
390 div_off+src_map->extra,
391 dst_map->extra - div_off - src_map->extra);
394 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
395 struct isl_basic_map *src_map, isl_int *src,
396 unsigned in_off, unsigned out_off, unsigned div_off)
398 isl_int_set(dst[0], src[0]);
399 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
402 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
403 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
406 unsigned div_off = bmap1->n_div;
408 for (i = 0; i < bmap2->n_eq; ++i) {
409 int i1 = isl_basic_map_alloc_equality(bmap1);
412 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
413 i_pos, o_pos, div_off);
416 for (i = 0; i < bmap2->n_ineq; ++i) {
417 int i1 = isl_basic_map_alloc_inequality(bmap1);
420 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
421 i_pos, o_pos, div_off);
424 for (i = 0; i < bmap2->n_div; ++i) {
425 int i1 = isl_basic_map_alloc_div(bmap1);
428 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
429 i_pos, o_pos, div_off);
432 isl_basic_map_free(bmap2);
437 isl_basic_map_free(bmap1);
438 isl_basic_map_free(bmap2);
442 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
443 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
444 unsigned n_eq, unsigned n_ineq)
446 struct isl_basic_map *ext;
450 base = isl_basic_map_cow(base);
454 dims_ok = base && base->nparam == nparam &&
455 base->n_in == n_in && base->n_out == n_out &&
456 base->extra >= base->n_div + extra;
458 if (dims_ok && n_eq == 0 && n_ineq == 0)
461 isl_assert(base->ctx, base->nparam <= nparam, goto error);
462 isl_assert(base->ctx, base->n_in <= n_in, goto error);
463 isl_assert(base->ctx, base->n_out <= n_out, goto error);
464 extra += base->extra;
466 n_ineq += base->n_ineq;
468 ext = isl_basic_map_alloc(base->ctx, nparam, n_in, n_out,
469 extra, n_eq, n_ineq);
476 ext = add_constraints(ext, base, 0, 0);
479 F_CLR(ext, ISL_BASIC_SET_FINAL);
485 isl_basic_map_free(base);
489 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
490 unsigned nparam, unsigned dim, unsigned extra,
491 unsigned n_eq, unsigned n_ineq)
493 return (struct isl_basic_set *)
494 isl_basic_map_extend((struct isl_basic_map *)base,
495 nparam, 0, dim, extra, n_eq, n_ineq);
498 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
500 return (struct isl_basic_set *)
501 isl_basic_map_cow((struct isl_basic_map *)bset);
504 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
511 bmap = isl_basic_map_dup(bmap);
513 F_CLR(bmap, ISL_BASIC_SET_FINAL);
517 struct isl_set *isl_set_cow(struct isl_set *set)
525 return isl_set_dup(set);
528 struct isl_map *isl_map_cow(struct isl_map *map)
536 return isl_map_dup(map);
539 static void swap_vars(struct isl_blk blk, isl_int *a,
540 unsigned a_len, unsigned b_len)
542 isl_seq_cpy(blk.data, a+a_len, b_len);
543 isl_seq_cpy(blk.data+b_len, a, a_len);
544 isl_seq_cpy(a, blk.data, b_len+a_len);
547 struct isl_basic_set *isl_basic_set_swap_vars(
548 struct isl_basic_set *bset, unsigned n)
556 isl_assert(bset->ctx, n <= bset->dim, goto error);
561 bset = isl_basic_set_cow(bset);
565 blk = isl_blk_alloc(bset->ctx, bset->dim);
566 if (isl_blk_is_error(blk))
569 for (i = 0; i < bset->n_eq; ++i)
571 bset->eq[i]+1+bset->nparam, n, bset->dim - n);
573 for (i = 0; i < bset->n_ineq; ++i)
575 bset->ineq[i]+1+bset->nparam, n, bset->dim - n);
577 for (i = 0; i < bset->n_div; ++i)
579 bset->div[i]+1+1+bset->nparam, n, bset->dim - n);
581 isl_blk_free(bset->ctx, blk);
586 isl_basic_set_free(bset);
590 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
593 set = isl_set_cow(set);
597 for (i = 0; i < set->n; ++i) {
598 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
607 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
609 isl_seq_cpy(c, c + n, rem);
610 isl_seq_clr(c + rem, n);
613 struct isl_basic_set *isl_basic_set_drop_dims(
614 struct isl_basic_set *bset, unsigned first, unsigned n)
621 isl_assert(bset->ctx, first + n <= bset->dim, goto error);
626 bset = isl_basic_set_cow(bset);
630 for (i = 0; i < bset->n_eq; ++i)
631 constraint_drop_vars(bset->eq[i]+1+bset->nparam+first, n,
632 (bset->dim-first-n)+bset->extra);
634 for (i = 0; i < bset->n_ineq; ++i)
635 constraint_drop_vars(bset->ineq[i]+1+bset->nparam+first, n,
636 (bset->dim-first-n)+bset->extra);
638 for (i = 0; i < bset->n_div; ++i)
639 constraint_drop_vars(bset->div[i]+1+1+bset->nparam+first, n,
640 (bset->dim-first-n)+bset->extra);
645 return isl_basic_set_finalize(bset);
647 isl_basic_set_free(bset);
651 static struct isl_set *isl_set_drop_dims(
652 struct isl_set *set, unsigned first, unsigned n)
659 isl_assert(set->ctx, first + n <= set->dim, goto error);
663 set = isl_set_cow(set);
667 for (i = 0; i < set->n; ++i) {
668 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
681 * We don't cow, as the div is assumed to be redundant.
683 static struct isl_basic_map *isl_basic_map_drop_div(
684 struct isl_basic_map *bmap, unsigned div)
692 pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
694 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
696 for (i = 0; i < bmap->n_eq; ++i)
697 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
699 for (i = 0; i < bmap->n_ineq; ++i) {
700 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
701 isl_basic_map_drop_inequality(bmap, i);
705 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
708 for (i = 0; i < bmap->n_div; ++i)
709 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
711 if (div != bmap->n_div - 1) {
713 isl_int *t = bmap->div[div];
715 for (j = div; j < bmap->n_div - 1; ++j)
716 bmap->div[j] = bmap->div[j+1];
718 bmap->div[bmap->n_div - 1] = t;
720 isl_basic_map_free_div(bmap, 1);
724 isl_basic_map_free(bmap);
728 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
731 unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
733 len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
735 for (i = 0; i < bmap->n_eq; ++i)
736 if (bmap->eq[i] != eq)
737 isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
739 for (i = 0; i < bmap->n_ineq; ++i)
740 isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
742 /* We need to be careful about circular definitions,
743 * so for now we just remove the definitions of other divs that
744 * depend on this div and (possibly) recompute them later.
746 for (i = 0; i < bmap->n_div; ++i)
747 if (!isl_int_is_zero(bmap->div[i][0]) &&
748 !isl_int_is_zero(bmap->div[i][1 + pos]))
749 isl_seq_clr(bmap->div[i], 1 + len);
751 isl_basic_map_drop_div(bmap, div);
754 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
760 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
761 isl_basic_map_free_div(bmap, bmap->n_div);
762 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
764 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
766 isl_basic_map_alloc_equality(bmap);
770 isl_int_set_si(bmap->eq[i][0], 1);
771 isl_seq_clr(bmap->eq[i]+1, total);
772 F_SET(bmap, ISL_BASIC_MAP_EMPTY);
773 return isl_basic_map_finalize(bmap);
775 isl_basic_map_free(bmap);
779 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
781 return (struct isl_basic_set *)
782 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
785 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
787 isl_int *t = bmap->eq[a];
788 bmap->eq[a] = bmap->eq[b];
792 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
794 isl_int *t = bmap->ineq[a];
795 bmap->ineq[a] = bmap->ineq[b];
799 static void swap_div(struct isl_basic_map *bmap, int a, int b)
802 unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
803 isl_int *t = bmap->div[a];
804 bmap->div[a] = bmap->div[b];
807 for (i = 0; i < bmap->n_eq; ++i)
808 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
810 for (i = 0; i < bmap->n_ineq; ++i)
811 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
813 for (i = 0; i < bmap->n_div; ++i)
814 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
817 struct isl_basic_map *isl_basic_map_gauss(
818 struct isl_basic_map *bmap, int *progress)
829 total_var = bmap->nparam + bmap->n_in + bmap->n_out;
830 total = total_var + bmap->n_div;
832 last_var = total - 1;
833 for (done = 0; done < bmap->n_eq; ++done) {
834 for (; last_var >= 0; --last_var) {
835 for (k = done; k < bmap->n_eq; ++k)
836 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
844 swap_equality(bmap, k, done);
845 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
846 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
848 for (k = 0; k < bmap->n_eq; ++k) {
851 if (isl_int_is_zero(bmap->eq[k][1+last_var]))
855 isl_seq_elim(bmap->eq[k], bmap->eq[done],
856 1+last_var, 1+total, NULL);
859 for (k = 0; k < bmap->n_ineq; ++k) {
860 if (isl_int_is_zero(bmap->ineq[k][1+last_var]))
864 isl_seq_elim(bmap->ineq[k], bmap->eq[done],
865 1+last_var, 1+total, NULL);
868 for (k = 0; k < bmap->n_div; ++k) {
869 if (isl_int_is_zero(bmap->div[k][0]))
871 if (isl_int_is_zero(bmap->div[k][1+1+last_var]))
875 isl_seq_elim(bmap->div[k]+1, bmap->eq[done],
876 1+last_var, 1+total, &bmap->div[k][0]);
879 if (last_var >= total_var &&
880 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
881 unsigned div = last_var - total_var;
882 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
883 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
884 isl_int_set(bmap->div[div][0],
885 bmap->eq[done][1+last_var]);
888 if (done == bmap->n_eq)
890 for (k = done; k < bmap->n_eq; ++k) {
891 if (isl_int_is_zero(bmap->eq[k][0]))
893 return isl_basic_map_set_to_empty(bmap);
895 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
899 struct isl_basic_set *isl_basic_set_gauss(
900 struct isl_basic_set *bset, int *progress)
902 return (struct isl_basic_set*)isl_basic_map_gauss(
903 (struct isl_basic_map *)bset, progress);
906 static unsigned int round_up(unsigned int v)
917 static int hash_index(int *index, unsigned int size, int bits,
918 struct isl_basic_map *bmap, int k)
921 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
922 u_int32_t hash = isl_seq_hash(bmap->ineq[k]+1, total, bits);
923 for (h = hash; index[h]; h = (h+1) % size)
924 if (k != index[h]-1 &&
925 isl_seq_eq(bmap->ineq[k]+1,
926 bmap->ineq[index[h]-1]+1, total))
931 static struct isl_basic_map *remove_duplicate_constraints(
932 struct isl_basic_map *bmap, int *progress)
938 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
941 if (bmap->n_ineq <= 1)
944 size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
945 bits = ffs(size) - 1;
946 index = isl_alloc_array(ctx, int, size);
947 memset(index, 0, size * sizeof(int));
951 index[isl_seq_hash(bmap->ineq[0]+1, total, bits)] = 1;
952 for (k = 1; k < bmap->n_ineq; ++k) {
953 h = hash_index(index, size, bits, bmap, k);
961 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
962 swap_inequality(bmap, k, l);
963 isl_basic_map_drop_inequality(bmap, k);
967 for (k = 0; k < bmap->n_ineq-1; ++k) {
968 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
969 h = hash_index(index, size, bits, bmap, k);
970 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
974 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
975 if (isl_int_is_pos(sum))
977 if (isl_int_is_zero(sum)) {
978 /* We need to break out of the loop after these
979 * changes since the contents of the hash
980 * will no longer be valid.
981 * Plus, we probably we want to regauss first.
983 isl_basic_map_drop_inequality(bmap, l);
984 isl_basic_map_inequality_to_equality(bmap, k);
986 bmap = isl_basic_map_set_to_empty(bmap);
995 static struct isl_basic_map *remove_duplicate_divs(
996 struct isl_basic_map *bmap, int *progress)
1003 unsigned total_var = bmap->nparam + bmap->n_in + bmap->n_out;
1004 unsigned total = total_var + bmap->n_div;
1005 struct isl_ctx *ctx;
1007 if (bmap->n_div <= 1)
1011 for (k = bmap->n_div - 1; k >= 0; --k)
1012 if (!isl_int_is_zero(bmap->div[k][0]))
1017 size = round_up(4 * bmap->n_div / 3 - 1);
1018 bits = ffs(size) - 1;
1019 index = isl_alloc_array(ctx, int, size);
1020 memset(index, 0, size * sizeof(int));
1023 eq = isl_blk_alloc(ctx, 1+total);
1024 if (isl_blk_is_error(eq))
1027 isl_seq_clr(eq.data, 1+total);
1028 index[isl_seq_hash(bmap->div[k], 2+total, bits)] = k + 1;
1029 for (--k; k >= 0; --k) {
1032 if (isl_int_is_zero(bmap->div[k][0]))
1035 hash = isl_seq_hash(bmap->div[k], 2+total, bits);
1036 for (h = hash; index[h]; h = (h+1) % size)
1037 if (isl_seq_eq(bmap->div[k],
1038 bmap->div[index[h]-1], 2+total))
1043 isl_int_set_si(eq.data[1+total_var+k], -1);
1044 isl_int_set_si(eq.data[1+total_var+l], 1);
1045 eliminate_div(bmap, eq.data, l);
1046 isl_int_set_si(eq.data[1+total_var+k], 0);
1047 isl_int_set_si(eq.data[1+total_var+l], 0);
1052 isl_blk_free(ctx, eq);
1058 /* Elimininate divs based on equalities
1060 static struct isl_basic_map *eliminate_divs_eq(
1061 struct isl_basic_map *bmap, int *progress)
1071 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1073 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1074 for (i = 0; i < bmap->n_eq; ++i) {
1075 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
1076 !isl_int_is_negone(bmap->eq[i][off + d]))
1080 eliminate_div(bmap, bmap->eq[i], d);
1081 isl_basic_map_drop_equality(bmap, i);
1086 return eliminate_divs_eq(bmap, progress);
1090 static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
1094 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1097 for (i = bmap->n_eq - 1; i >= 0; --i) {
1098 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
1099 if (isl_int_is_zero(gcd)) {
1100 if (!isl_int_is_zero(bmap->eq[i][0])) {
1101 bmap = isl_basic_map_set_to_empty(bmap);
1104 isl_basic_map_drop_equality(bmap, i);
1107 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1108 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
1109 if (isl_int_is_one(gcd))
1111 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
1112 bmap = isl_basic_map_set_to_empty(bmap);
1115 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
1118 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1119 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
1120 if (isl_int_is_zero(gcd)) {
1121 if (isl_int_is_neg(bmap->ineq[i][0])) {
1122 bmap = isl_basic_map_set_to_empty(bmap);
1125 isl_basic_map_drop_inequality(bmap, i);
1128 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1129 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
1130 if (isl_int_is_one(gcd))
1132 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
1133 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
1141 /* Eliminate the specified variables from the constraints using
1142 * Fourier-Motzkin. The variables themselves are not removed.
1144 struct isl_basic_map *isl_basic_map_eliminate_vars(
1145 struct isl_basic_map *bmap, int pos, unsigned n)
1154 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1155 extra = bmap->extra - bmap->n_div;
1157 bmap = isl_basic_map_cow(bmap);
1158 bmap = isl_basic_map_gauss(bmap, NULL);
1159 for (d = pos + n - 1; d >= pos; --d) {
1160 int n_lower, n_upper;
1163 for (i = 0; i < bmap->n_eq; ++i) {
1164 if (isl_int_is_zero(bmap->eq[i][1+d]))
1166 isl_basic_map_drop_equality(bmap, i);
1173 for (i = 0; i < bmap->n_ineq; ++i) {
1174 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1176 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1179 bmap = isl_basic_map_extend(bmap,
1180 bmap->nparam, bmap->n_in, bmap->n_out, 0,
1181 0, n_lower * n_upper);
1182 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1184 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1187 for (j = 0; j < i; ++j) {
1188 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1191 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1192 isl_int_sgn(bmap->ineq[j][1+d]))
1194 k = isl_basic_map_alloc_inequality(bmap);
1197 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1199 isl_seq_clr(bmap->ineq[k]+1+total, extra);
1200 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1201 1+d, 1+total, NULL);
1203 isl_basic_map_drop_inequality(bmap, i);
1206 if (n_lower > 0 && n_upper > 0) {
1207 bmap = normalize_constraints(bmap);
1208 bmap = remove_duplicate_constraints(bmap, NULL);
1209 bmap = isl_basic_map_gauss(bmap, NULL);
1210 bmap = isl_basic_map_convex_hull(bmap);
1213 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1219 isl_basic_map_free(bmap);
1223 struct isl_basic_set *isl_basic_set_eliminate_vars(
1224 struct isl_basic_set *bset, unsigned pos, unsigned n)
1226 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1227 (struct isl_basic_map *)bset, pos, n);
1230 /* Project out n dimensions starting at first using Fourier-Motzkin */
1231 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1232 unsigned first, unsigned n)
1239 set = isl_set_cow(set);
1242 isl_assert(set->ctx, first+n <= set->dim, goto error);
1244 for (i = 0; i < set->n; ++i) {
1245 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1246 set->nparam + first, n);
1250 set = isl_set_drop_dims(set, first, n);
1257 /* Project out n dimensions starting at first using Fourier-Motzkin */
1258 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1259 unsigned first, unsigned n)
1261 bset = isl_basic_set_eliminate_vars(bset, bset->nparam + first, n);
1262 bset = isl_basic_set_drop_dims(bset, first, n);
1266 /* Elimininate divs based on inequalities
1268 static struct isl_basic_map *eliminate_divs_ineq(
1269 struct isl_basic_map *bmap, int *progress)
1274 struct isl_ctx *ctx;
1280 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1282 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1283 for (i = 0; i < bmap->n_eq; ++i)
1284 if (!isl_int_is_zero(bmap->eq[i][off + d]))
1288 for (i = 0; i < bmap->n_ineq; ++i)
1289 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
1291 if (i < bmap->n_ineq)
1294 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
1295 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1297 bmap = isl_basic_map_drop_div(bmap, d);
1304 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1311 bmap = normalize_constraints(bmap);
1312 bmap = eliminate_divs_eq(bmap, &progress);
1313 bmap = eliminate_divs_ineq(bmap, &progress);
1314 bmap = isl_basic_map_gauss(bmap, &progress);
1315 bmap = remove_duplicate_divs(bmap, &progress);
1316 bmap = remove_duplicate_constraints(bmap, &progress);
1321 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1323 return (struct isl_basic_set *)
1324 isl_basic_map_simplify((struct isl_basic_map *)bset);
1327 static void dump_term(struct isl_basic_map *bmap,
1328 isl_int c, int pos, FILE *out)
1330 unsigned in = bmap->n_in;
1331 unsigned dim = bmap->n_in + bmap->n_out;
1333 isl_int_print(out, c);
1335 if (!isl_int_is_one(c))
1336 isl_int_print(out, c);
1337 if (pos < 1 + bmap->nparam)
1338 fprintf(out, "p%d", pos - 1);
1339 else if (pos < 1 + bmap->nparam + in)
1340 fprintf(out, "i%d", pos - 1 - bmap->nparam);
1341 else if (pos < 1 + bmap->nparam + dim)
1342 fprintf(out, "o%d", pos - 1 - bmap->nparam - in);
1344 fprintf(out, "e%d", pos - 1 - bmap->nparam - dim);
1348 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1349 int sign, FILE *out)
1353 unsigned len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1357 for (i = 0, first = 1; i < len; ++i) {
1358 if (isl_int_sgn(c[i]) * sign <= 0)
1361 fprintf(out, " + ");
1363 isl_int_abs(v, c[i]);
1364 dump_term(bmap, v, i, out);
1371 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1372 const char *op, FILE *out, int indent)
1376 fprintf(out, "%*s", indent, "");
1378 dump_constraint_sign(bmap, c, 1, out);
1379 fprintf(out, " %s ", op);
1380 dump_constraint_sign(bmap, c, -1, out);
1384 for (i = bmap->n_div; i < bmap->extra; ++i) {
1385 if (isl_int_is_zero(c[1+bmap->nparam+bmap->n_in+bmap->n_out+i]))
1387 fprintf(out, "%*s", indent, "");
1388 fprintf(out, "ERROR: unused div coefficient not zero\n");
1392 static void dump_constraints(struct isl_basic_map *bmap,
1393 isl_int **c, unsigned n,
1394 const char *op, FILE *out, int indent)
1398 for (i = 0; i < n; ++i)
1399 dump_constraint(bmap, c[i], op, out, indent);
1402 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1406 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1408 for (j = 0; j < 1 + total; ++j) {
1409 if (isl_int_is_zero(exp[j]))
1411 if (!first && isl_int_is_pos(exp[j]))
1413 dump_term(bmap, exp[j], j, out);
1418 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1422 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1423 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1425 for (i = 0; i < bmap->n_div; ++i) {
1426 fprintf(out, "%*s", indent, "");
1427 fprintf(out, "e%d = [(", i);
1428 dump_affine(bmap, bmap->div[i]+1, out);
1430 isl_int_print(out, bmap->div[i][0]);
1431 fprintf(out, "]\n");
1435 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1438 fprintf(out, "null basic set\n");
1442 fprintf(out, "%*s", indent, "");
1443 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1444 bset->ref, bset->nparam, bset->dim, bset->extra,
1446 dump((struct isl_basic_map *)bset, out, indent);
1449 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1452 fprintf(out, "null basic map\n");
1456 fprintf(out, "%*s", indent, "");
1457 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, flags: %x\n",
1459 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, bmap->flags);
1460 dump(bmap, out, indent);
1463 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1465 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1468 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1469 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1470 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1474 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1475 unsigned nparam, unsigned dim, int n, unsigned flags)
1477 struct isl_set *set;
1479 isl_assert(ctx, n >= 0, return NULL);
1480 set = isl_alloc(ctx, struct isl_set,
1481 sizeof(struct isl_set) +
1482 n * sizeof(struct isl_basic_set *));
1491 set->nparam = nparam;
1498 struct isl_set *isl_set_dup(struct isl_set *set)
1501 struct isl_set *dup;
1506 dup = isl_set_alloc(set->ctx, set->nparam, set->dim, set->n, set->flags);
1509 for (i = 0; i < set->n; ++i)
1510 dup = isl_set_add(dup,
1511 isl_basic_set_dup(set->p[i]));
1515 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1517 struct isl_set *set;
1522 set = isl_set_alloc(bset->ctx, bset->nparam, bset->dim, 1, ISL_MAP_DISJOINT);
1524 isl_basic_set_free(bset);
1527 return isl_set_add(set, bset);
1530 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1532 struct isl_map *map;
1537 map = isl_map_alloc(bmap->ctx, bmap->nparam, bmap->n_in, bmap->n_out, 1,
1540 isl_basic_map_free(bmap);
1543 return isl_map_add(map, bmap);
1546 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1550 isl_assert(set->ctx, set->nparam == bset->nparam, goto error);
1551 isl_assert(set->ctx, set->dim == bset->dim, goto error);
1552 isl_assert(set->ctx, set->n < set->size, goto error);
1553 set->p[set->n] = bset;
1560 isl_basic_set_free(bset);
1564 void isl_set_free(struct isl_set *set)
1574 isl_ctx_deref(set->ctx);
1575 for (i = 0; i < set->n; ++i)
1576 isl_basic_set_free(set->p[i]);
1580 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1585 fprintf(out, "null set\n");
1589 fprintf(out, "%*s", indent, "");
1590 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1591 set->ref, set->n, set->nparam, set->dim, set->flags);
1592 for (i = 0; i < set->n; ++i) {
1593 fprintf(out, "%*s", indent, "");
1594 fprintf(out, "basic set %d:\n", i);
1595 isl_basic_set_dump(set->p[i], out, indent+4);
1599 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1604 fprintf(out, "null map\n");
1608 fprintf(out, "%*s", indent, "");
1609 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, flags: %x\n",
1610 map->ref, map->n, map->nparam, map->n_in, map->n_out,
1612 for (i = 0; i < map->n; ++i) {
1613 fprintf(out, "%*s", indent, "");
1614 fprintf(out, "basic map %d:\n", i);
1615 isl_basic_map_dump(map->p[i], out, indent+4);
1619 struct isl_basic_map *isl_basic_map_intersect_domain(
1620 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1622 struct isl_basic_map *bmap_domain;
1627 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1628 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1630 bmap = isl_basic_map_extend(bmap,
1631 bset->nparam, bmap->n_in, bmap->n_out,
1632 bset->n_div, bset->n_eq, bset->n_ineq);
1635 bmap_domain = isl_basic_map_from_basic_set(bset,
1637 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1639 bmap = isl_basic_map_simplify(bmap);
1640 return isl_basic_map_finalize(bmap);
1642 isl_basic_map_free(bmap);
1643 isl_basic_set_free(bset);
1647 struct isl_basic_map *isl_basic_map_intersect_range(
1648 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1650 struct isl_basic_map *bmap_range;
1655 isl_assert(bset->ctx, bset->dim == bmap->n_out, goto error);
1656 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1658 bmap = isl_basic_map_extend(bmap,
1659 bset->nparam, bmap->n_in, bmap->n_out,
1660 bset->n_div, bset->n_eq, bset->n_ineq);
1663 bmap_range = isl_basic_map_from_basic_set(bset,
1665 bmap = add_constraints(bmap, bmap_range, 0, 0);
1667 bmap = isl_basic_map_simplify(bmap);
1668 return isl_basic_map_finalize(bmap);
1670 isl_basic_map_free(bmap);
1671 isl_basic_set_free(bset);
1675 struct isl_basic_map *isl_basic_map_intersect(
1676 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1678 if (!bmap1 || !bmap2)
1681 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
1682 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
1683 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
1685 bmap1 = isl_basic_map_extend(bmap1,
1686 bmap1->nparam, bmap1->n_in, bmap1->n_out,
1687 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1690 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1692 bmap1 = isl_basic_map_simplify(bmap1);
1693 return isl_basic_map_finalize(bmap1);
1695 isl_basic_map_free(bmap1);
1696 isl_basic_map_free(bmap2);
1700 struct isl_basic_set *isl_basic_set_intersect(
1701 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1703 return (struct isl_basic_set *)
1704 isl_basic_map_intersect(
1705 (struct isl_basic_map *)bset1,
1706 (struct isl_basic_map *)bset2);
1709 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1712 struct isl_map *result;
1718 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
1719 F_ISSET(map2, ISL_MAP_DISJOINT))
1720 FL_SET(flags, ISL_MAP_DISJOINT);
1722 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
1723 map1->n * map2->n, flags);
1726 for (i = 0; i < map1->n; ++i)
1727 for (j = 0; j < map2->n; ++j) {
1728 struct isl_basic_map *part;
1729 part = isl_basic_map_intersect(
1730 isl_basic_map_copy(map1->p[i]),
1731 isl_basic_map_copy(map2->p[j]));
1732 if (isl_basic_map_is_empty(part))
1733 isl_basic_map_free(part);
1735 result = isl_map_add(result, part);
1748 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1750 return (struct isl_set *)
1751 isl_map_intersect((struct isl_map *)set1,
1752 (struct isl_map *)set2);
1755 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1757 struct isl_basic_set *bset;
1762 bmap = isl_basic_map_cow(bmap);
1766 bset = isl_basic_set_from_basic_map(bmap);
1767 bset = isl_basic_set_swap_vars(bset, in);
1768 return isl_basic_map_from_basic_set(bset, bset->dim-in, in);
1771 /* Turn final n dimensions into existentially quantified variables.
1773 struct isl_basic_set *isl_basic_set_project_out(
1774 struct isl_basic_set *bset, unsigned n, unsigned flags)
1784 isl_assert(bset->ctx, n <= bset->dim, goto error);
1789 bset = isl_basic_set_cow(bset);
1791 row_size = 1 + bset->nparam + bset->dim + bset->extra;
1792 old = bset->block2.data;
1793 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1794 (bset->extra + n) * (1 + row_size));
1795 if (!bset->block2.data)
1797 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1800 for (i = 0; i < n; ++i) {
1801 new_div[i] = bset->block2.data +
1802 (bset->extra + i) * (1 + row_size);
1803 isl_seq_clr(new_div[i], 1 + row_size);
1805 for (i = 0; i < bset->extra; ++i)
1806 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1808 bset->div = new_div;
1812 bset = isl_basic_set_simplify(bset);
1813 return isl_basic_set_finalize(bset);
1815 isl_basic_set_free(bset);
1819 struct isl_basic_map *isl_basic_map_apply_range(
1820 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1822 struct isl_basic_set *bset;
1823 unsigned n_in, n_out;
1825 if (!bmap1 || !bmap2)
1828 isl_assert(bmap->ctx, bmap1->n_out == bmap2->n_in, goto error);
1829 isl_assert(bmap->ctx, bmap1->nparam == bmap2->nparam, goto error);
1832 n_out = bmap2->n_out;
1834 bmap2 = isl_basic_map_reverse(bmap2);
1837 bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
1838 bmap1->n_in + bmap2->n_in, bmap1->n_out,
1839 bmap2->extra, bmap2->n_eq, bmap2->n_ineq);
1842 bmap1 = add_constraints(bmap1, bmap2, bmap1->n_in - bmap2->n_in, 0);
1843 bmap1 = isl_basic_map_simplify(bmap1);
1844 bset = isl_basic_set_from_basic_map(bmap1);
1845 bset = isl_basic_set_project_out(bset,
1846 bset->dim - (n_in + n_out), 0);
1847 return isl_basic_map_from_basic_set(bset, n_in, n_out);
1849 isl_basic_map_free(bmap1);
1850 isl_basic_map_free(bmap2);
1854 struct isl_basic_set *isl_basic_set_apply(
1855 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1860 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1862 return (struct isl_basic_set *)
1863 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1865 isl_basic_set_free(bset);
1866 isl_basic_map_free(bmap);
1870 struct isl_basic_map *isl_basic_map_apply_domain(
1871 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1873 if (!bmap1 || !bmap2)
1876 isl_assert(ctx, bmap1->n_in == bmap2->n_in, goto error);
1877 isl_assert(ctx, bmap1->nparam == bmap2->nparam, goto error);
1879 bmap1 = isl_basic_map_reverse(bmap1);
1880 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1881 return isl_basic_map_reverse(bmap1);
1883 isl_basic_map_free(bmap1);
1884 isl_basic_map_free(bmap2);
1888 static struct isl_basic_map *var_equal(struct isl_ctx *ctx,
1889 struct isl_basic_map *bmap, unsigned pos)
1892 i = isl_basic_map_alloc_equality(bmap);
1895 isl_seq_clr(bmap->eq[i],
1896 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1897 isl_int_set_si(bmap->eq[i][1+bmap->nparam+pos], -1);
1898 isl_int_set_si(bmap->eq[i][1+bmap->nparam+bmap->n_in+pos], 1);
1899 return isl_basic_map_finalize(bmap);
1901 isl_basic_map_free(bmap);
1905 static struct isl_basic_map *var_more(struct isl_ctx *ctx,
1906 struct isl_basic_map *bmap, unsigned pos)
1909 i = isl_basic_map_alloc_inequality(bmap);
1912 isl_seq_clr(bmap->ineq[i],
1913 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1914 isl_int_set_si(bmap->ineq[i][0], -1);
1915 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], -1);
1916 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], 1);
1917 return isl_basic_map_finalize(bmap);
1919 isl_basic_map_free(bmap);
1923 static struct isl_basic_map *var_less(struct isl_ctx *ctx,
1924 struct isl_basic_map *bmap, unsigned pos)
1927 i = isl_basic_map_alloc_inequality(bmap);
1930 isl_seq_clr(bmap->ineq[i],
1931 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1932 isl_int_set_si(bmap->ineq[i][0], -1);
1933 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], 1);
1934 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], -1);
1935 return isl_basic_map_finalize(bmap);
1937 isl_basic_map_free(bmap);
1941 struct isl_basic_map *isl_basic_map_equal(struct isl_ctx *ctx,
1942 unsigned nparam, unsigned in, unsigned out, unsigned n_equal)
1945 struct isl_basic_map *bmap;
1946 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, n_equal, 0);
1949 for (i = 0; i < n_equal && bmap; ++i)
1950 bmap = var_equal(ctx, bmap, i);
1951 return isl_basic_map_finalize(bmap);
1954 struct isl_basic_map *isl_basic_map_less_at(struct isl_ctx *ctx,
1955 unsigned nparam, unsigned in, unsigned out, unsigned pos)
1958 struct isl_basic_map *bmap;
1959 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
1962 for (i = 0; i < pos && bmap; ++i)
1963 bmap = var_equal(ctx, bmap, i);
1965 bmap = var_less(ctx, bmap, pos);
1966 return isl_basic_map_finalize(bmap);
1969 struct isl_basic_map *isl_basic_map_more_at(struct isl_ctx *ctx,
1970 unsigned nparam, unsigned in, unsigned out, unsigned pos)
1973 struct isl_basic_map *bmap;
1974 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
1977 for (i = 0; i < pos && bmap; ++i)
1978 bmap = var_equal(ctx, bmap, i);
1980 bmap = var_more(ctx, bmap, pos);
1981 return isl_basic_map_finalize(bmap);
1984 struct isl_basic_map *isl_basic_map_from_basic_set(
1985 struct isl_basic_set *bset, unsigned n_in, unsigned n_out)
1987 struct isl_basic_map *bmap;
1989 bset = isl_basic_set_cow(bset);
1993 isl_assert(ctx, bset->dim == n_in + n_out, goto error);
1994 bmap = (struct isl_basic_map *) bset;
1996 bmap->n_out = n_out;
1997 return isl_basic_map_finalize(bmap);
1999 isl_basic_set_free(bset);
2003 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2007 if (bmap->n_in == 0)
2008 return (struct isl_basic_set *)bmap;
2009 bmap = isl_basic_map_cow(bmap);
2012 bmap->n_out += bmap->n_in;
2014 bmap = isl_basic_map_finalize(bmap);
2015 return (struct isl_basic_set *)bmap;
2020 /* For a div d = floor(f/m), add the constraints
2023 * -(f-(n-1)) + m d >= 0
2025 * Note that the second constraint is the negation of
2029 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2032 unsigned div_pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
2033 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
2035 i = isl_basic_map_alloc_inequality(bmap);
2038 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2039 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2041 j = isl_basic_map_alloc_inequality(bmap);
2044 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2045 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2046 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2050 struct isl_basic_set *isl_basic_map_underlying_set(
2051 struct isl_basic_map *bmap)
2055 if (bmap->nparam == 0 && bmap->n_in == 0 && bmap->n_div == 0)
2056 return (struct isl_basic_set *)bmap;
2057 bmap = isl_basic_map_cow(bmap);
2060 bmap->n_out += bmap->nparam + bmap->n_in + bmap->n_div;
2063 bmap->extra -= bmap->n_div;
2065 bmap = isl_basic_map_finalize(bmap);
2066 return (struct isl_basic_set *)bmap;
2071 struct isl_basic_map *isl_basic_map_overlying_set(
2072 struct isl_basic_set *bset, struct isl_basic_map *like)
2074 struct isl_basic_map *bmap;
2075 struct isl_ctx *ctx;
2082 isl_assert(ctx, bset->dim ==
2083 like->nparam + like->n_in + like->n_out + like->n_div,
2085 if (like->nparam == 0 && like->n_in == 0 && like->n_div == 0) {
2086 isl_basic_map_free(like);
2087 return (struct isl_basic_map *)bset;
2089 bset = isl_basic_set_cow(bset);
2092 total = bset->dim + bset->extra;
2093 bmap = (struct isl_basic_map *)bset;
2094 bmap->nparam = like->nparam;
2095 bmap->n_in = like->n_in;
2096 bmap->n_out = like->n_out;
2097 bmap->extra += like->n_div;
2100 ltotal = total - bmap->extra + like->extra;
2103 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2104 bmap->extra * (1 + 1 + total));
2105 if (isl_blk_is_error(bmap->block2))
2107 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2111 bmap = isl_basic_map_extend(bmap, bmap->nparam,
2112 bmap->n_in, bmap->n_out, 0, 0, 2 * like->n_div);
2113 for (i = 0; i < like->n_div; ++i) {
2114 k = isl_basic_map_alloc_div(bmap);
2117 isl_seq_cpy(bmap->div[k], like->div[i], 1 + 1 + ltotal);
2118 isl_seq_clr(bmap->div[k]+1+1+ltotal, total - ltotal);
2119 if (add_div_constraints(bmap, k) < 0)
2123 isl_basic_map_free(like);
2124 bmap = isl_basic_map_finalize(bmap);
2127 isl_basic_map_free(like);
2128 isl_basic_set_free(bset);
2132 struct isl_basic_set *isl_basic_set_from_underlying_set(
2133 struct isl_basic_set *bset, struct isl_basic_set *like)
2135 return (struct isl_basic_set *)
2136 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2139 struct isl_set *isl_set_from_underlying_set(
2140 struct isl_set *set, struct isl_basic_set *like)
2146 isl_assert(set->ctx, set->dim == like->nparam + like->dim + like->n_div,
2148 if (like->nparam == 0 && like->n_div == 0) {
2149 isl_basic_set_free(like);
2152 set = isl_set_cow(set);
2155 for (i = 0; i < set->n; ++i) {
2156 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2157 isl_basic_set_copy(like));
2161 set->nparam = like->nparam;
2162 set->dim = like->dim;
2163 isl_basic_set_free(like);
2166 isl_basic_set_free(like);
2171 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2175 map = isl_map_align_divs(map);
2176 map = isl_map_cow(map);
2180 for (i = 0; i < map->n; ++i) {
2181 map->p[i] = (struct isl_basic_map *)
2182 isl_basic_map_underlying_set(map->p[i]);
2187 map->n_out += map->nparam + map->n_in;
2189 map->n_out = map->p[0]->n_out;
2192 return (struct isl_set *)map;
2198 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2200 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2203 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2205 struct isl_basic_set *domain;
2209 n_out = bmap->n_out;
2210 domain = isl_basic_set_from_basic_map(bmap);
2211 return isl_basic_set_project_out(domain, n_out, 0);
2214 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2216 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2219 struct isl_set *isl_map_range(struct isl_map *map)
2222 struct isl_set *set;
2226 map = isl_map_cow(map);
2230 set = (struct isl_set *) map;
2232 for (i = 0; i < map->n; ++i) {
2233 set->p[i] = isl_basic_map_range(map->p[i]);
2237 F_CLR(set, ISL_MAP_DISJOINT);
2244 struct isl_map *isl_map_from_set(struct isl_set *set,
2245 unsigned n_in, unsigned n_out)
2248 struct isl_map *map = NULL;
2252 isl_assert(set->ctx, set->dim == n_in + n_out, goto error);
2253 set = isl_set_cow(set);
2256 map = (struct isl_map *)set;
2257 for (i = 0; i < set->n; ++i) {
2258 map->p[i] = isl_basic_map_from_basic_set(
2259 set->p[i], n_in, n_out);
2271 struct isl_set *isl_set_from_map(struct isl_map *map)
2274 struct isl_set *set = NULL;
2278 map = isl_map_cow(map);
2281 map->n_out += map->n_in;
2283 set = (struct isl_set *)map;
2284 for (i = 0; i < map->n; ++i) {
2285 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2295 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2296 unsigned nparam, unsigned in, unsigned out, int n,
2299 struct isl_map *map;
2301 map = isl_alloc(ctx, struct isl_map,
2302 sizeof(struct isl_map) +
2303 n * sizeof(struct isl_basic_map *));
2312 map->nparam = nparam;
2319 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2320 unsigned nparam, unsigned in, unsigned out)
2322 struct isl_basic_map *bmap;
2323 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2324 bmap = isl_basic_map_set_to_empty(bmap);
2328 struct isl_basic_set *isl_basic_set_empty(struct isl_ctx *ctx,
2329 unsigned nparam, unsigned dim)
2331 struct isl_basic_set *bset;
2332 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 1, 0);
2333 bset = isl_basic_set_set_to_empty(bset);
2337 struct isl_basic_map *isl_basic_map_universe(struct isl_ctx *ctx,
2338 unsigned nparam, unsigned in, unsigned out)
2340 struct isl_basic_map *bmap;
2341 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 0, 0);
2345 struct isl_basic_set *isl_basic_set_universe(struct isl_ctx *ctx,
2346 unsigned nparam, unsigned dim)
2348 struct isl_basic_set *bset;
2349 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 0, 0);
2353 struct isl_map *isl_map_empty(struct isl_ctx *ctx,
2354 unsigned nparam, unsigned in, unsigned out)
2356 return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
2359 struct isl_set *isl_set_empty(struct isl_ctx *ctx,
2360 unsigned nparam, unsigned dim)
2362 return isl_set_alloc(ctx, nparam, dim, 0, ISL_MAP_DISJOINT);
2365 struct isl_map *isl_map_dup(struct isl_map *map)
2368 struct isl_map *dup;
2372 dup = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out, map->n,
2374 for (i = 0; i < map->n; ++i)
2375 dup = isl_map_add(dup,
2376 isl_basic_map_dup(map->p[i]));
2380 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2384 isl_assert(map->ctx, map->nparam == bmap->nparam, goto error);
2385 isl_assert(map->ctx, map->n_in == bmap->n_in, goto error);
2386 isl_assert(map->ctx, map->n_out == bmap->n_out, goto error);
2387 isl_assert(map->ctx, map->n < map->size, goto error);
2388 map->p[map->n] = bmap;
2395 isl_basic_map_free(bmap);
2399 void isl_map_free(struct isl_map *map)
2409 isl_ctx_deref(map->ctx);
2410 for (i = 0; i < map->n; ++i)
2411 isl_basic_map_free(map->p[i]);
2415 struct isl_map *isl_map_extend(struct isl_map *base,
2416 unsigned nparam, unsigned n_in, unsigned n_out)
2420 base = isl_map_cow(base);
2424 isl_assert(base->ctx, base->nparam <= nparam, goto error);
2425 isl_assert(base->ctx, base->n_in <= n_in, goto error);
2426 isl_assert(base->ctx, base->n_out <= n_out, goto error);
2427 base->nparam = nparam;
2429 base->n_out = n_out;
2430 for (i = 0; i < base->n; ++i) {
2431 base->p[i] = isl_basic_map_extend(base->p[i],
2432 nparam, n_in, n_out, 0, 0, 0);
2442 struct isl_set *isl_set_extend(struct isl_set *base,
2443 unsigned nparam, unsigned dim)
2445 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2449 static struct isl_basic_map *isl_basic_map_fix_var(struct isl_basic_map *bmap,
2450 unsigned var, int value)
2454 bmap = isl_basic_map_cow(bmap);
2455 bmap = isl_basic_map_extend(bmap,
2456 bmap->nparam, bmap->n_in, bmap->n_out, 0, 1, 0);
2457 j = isl_basic_map_alloc_equality(bmap);
2460 isl_seq_clr(bmap->eq[j],
2461 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2462 isl_int_set_si(bmap->eq[j][1 + var], -1);
2463 isl_int_set_si(bmap->eq[j][0], value);
2464 bmap = isl_basic_map_simplify(bmap);
2465 return isl_basic_map_finalize(bmap);
2467 isl_basic_map_free(bmap);
2471 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2472 unsigned input, int value)
2476 isl_assert(bmap->ctx, input < bmap->n_in, goto error);
2477 return isl_basic_map_fix_var(bmap, bmap->nparam + input, value);
2479 isl_basic_map_free(bmap);
2483 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2484 unsigned dim, int value)
2488 isl_assert(bset->ctx, dim < bset->dim, goto error);
2489 return (struct isl_basic_set *)
2490 isl_basic_map_fix_var((struct isl_basic_map *)bset,
2491 bset->nparam + dim, value);
2493 isl_basic_set_free(bset);
2497 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2498 unsigned input, int value)
2502 map = isl_map_cow(map);
2506 isl_assert(ctx, input < map->n_in, goto error);
2507 for (i = 0; i < map->n; ++i) {
2508 map->p[i] = isl_basic_map_fix_input_si(map->p[i], input, value);
2518 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2522 set = isl_set_cow(set);
2526 isl_assert(set->ctx, dim < set->dim, goto error);
2527 for (i = 0; i < set->n; ++i) {
2528 set->p[i] = isl_basic_set_fix_dim_si(set->p[i], dim, value);
2538 struct isl_map *isl_map_reverse(struct isl_map *map)
2543 map = isl_map_cow(map);
2548 map->n_in = map->n_out;
2550 for (i = 0; i < map->n; ++i) {
2551 map->p[i] = isl_basic_map_reverse(map->p[i]);
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;
2583 bmap = isl_basic_map_from_basic_set(bset, 0, bset->dim);
2586 dom = isl_basic_set_alloc(bmap->ctx, bmap->nparam, 0, 0, 0, 0);
2589 min = isl_basic_map_lexmin(bmap, dom, NULL);
2590 return isl_map_range(min);
2592 isl_basic_map_free(bmap);
2596 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2600 if (bmap->n_div == 0)
2601 return isl_map_from_basic_map(bmap);
2602 return isl_pip_basic_map_compute_divs(bmap);
2605 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2608 struct isl_map *res;
2614 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2615 for (i = 1 ; i < map->n; ++i) {
2617 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2618 if (F_ISSET(map, ISL_MAP_DISJOINT))
2619 res = isl_map_union_disjoint(res, r2);
2621 res = isl_map_union(res, r2);
2628 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2630 return (struct isl_set *)
2631 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2634 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2636 return (struct isl_set *)
2637 isl_map_compute_divs((struct isl_map *)set);
2640 struct isl_set *isl_map_domain(struct isl_map *map)
2643 struct isl_set *set;
2648 map = isl_map_cow(map);
2652 set = (struct isl_set *)map;
2653 set->dim = map->n_in;
2655 for (i = 0; i < map->n; ++i) {
2656 set->p[i] = isl_basic_map_domain(map->p[i]);
2660 F_CLR(set, ISL_MAP_DISJOINT);
2667 struct isl_map *isl_map_union_disjoint(
2668 struct isl_map *map1, struct isl_map *map2)
2672 struct isl_map *map = NULL;
2686 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
2687 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
2688 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
2690 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2691 F_ISSET(map2, ISL_MAP_DISJOINT))
2692 FL_SET(flags, ISL_MAP_DISJOINT);
2694 map = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
2695 map1->n + map2->n, flags);
2698 for (i = 0; i < map1->n; ++i) {
2699 map = isl_map_add(map,
2700 isl_basic_map_copy(map1->p[i]));
2704 for (i = 0; i < map2->n; ++i) {
2705 map = isl_map_add(map,
2706 isl_basic_map_copy(map2->p[i]));
2720 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
2722 map1 = isl_map_union_disjoint(map1, map2);
2726 F_CLR(map1, ISL_MAP_DISJOINT);
2730 struct isl_set *isl_set_union_disjoint(
2731 struct isl_set *set1, struct isl_set *set2)
2733 return (struct isl_set *)
2734 isl_map_union_disjoint(
2735 (struct isl_map *)set1, (struct isl_map *)set2);
2738 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
2740 return (struct isl_set *)
2741 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
2744 struct isl_map *isl_map_intersect_range(
2745 struct isl_map *map, struct isl_set *set)
2748 struct isl_map *result;
2754 if (F_ISSET(map, ISL_MAP_DISJOINT) &&
2755 F_ISSET(set, ISL_MAP_DISJOINT))
2756 FL_SET(flags, ISL_MAP_DISJOINT);
2758 result = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
2759 map->n * set->n, flags);
2762 for (i = 0; i < map->n; ++i)
2763 for (j = 0; j < set->n; ++j) {
2764 result = isl_map_add(result,
2765 isl_basic_map_intersect_range(
2766 isl_basic_map_copy(map->p[i]),
2767 isl_basic_set_copy(set->p[j])));
2780 struct isl_map *isl_map_intersect_domain(
2781 struct isl_map *map, struct isl_set *set)
2783 return isl_map_reverse(
2784 isl_map_intersect_range(isl_map_reverse(map), set));
2787 struct isl_map *isl_map_apply_domain(
2788 struct isl_map *map1, struct isl_map *map2)
2792 map1 = isl_map_reverse(map1);
2793 map1 = isl_map_apply_range(map1, map2);
2794 return isl_map_reverse(map1);
2801 struct isl_map *isl_map_apply_range(
2802 struct isl_map *map1, struct isl_map *map2)
2804 struct isl_map *result;
2810 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
2811 isl_assert(map1->ctx, map1->n_out == map2->n_in, goto error);
2813 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map2->n_out,
2814 map1->n * map2->n, 0);
2817 for (i = 0; i < map1->n; ++i)
2818 for (j = 0; j < map2->n; ++j) {
2819 result = isl_map_add(result,
2820 isl_basic_map_apply_range(
2821 isl_basic_map_copy(map1->p[i]),
2822 isl_basic_map_copy(map2->p[j])));
2828 if (result && result->n <= 1)
2829 F_SET(result, ISL_MAP_DISJOINT);
2838 * returns range - domain
2840 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
2842 struct isl_basic_set *bset;
2848 isl_assert(bmap->ctx, bmap->n_in == bmap->n_out, goto error);
2850 bset = isl_basic_set_from_basic_map(bmap);
2851 bset = isl_basic_set_extend(bset, bset->nparam, 3*dim, 0,
2853 bset = isl_basic_set_swap_vars(bset, 2*dim);
2854 for (i = 0; i < dim; ++i) {
2855 int j = isl_basic_map_alloc_equality(
2856 (struct isl_basic_map *)bset);
2859 isl_seq_clr(bset->eq[j],
2860 1 + bset->nparam + bset->dim + bset->extra);
2861 isl_int_set_si(bset->eq[j][1+bset->nparam+i], 1);
2862 isl_int_set_si(bset->eq[j][1+bset->nparam+dim+i], 1);
2863 isl_int_set_si(bset->eq[j][1+bset->nparam+2*dim+i], -1);
2865 return isl_basic_set_project_out(bset, 2*dim, 0);
2867 isl_basic_map_free(bmap);
2871 /* If the only constraints a div d=floor(f/m)
2872 * appears in are its two defining constraints
2875 * -(f - (m - 1)) + m d >= 0
2877 * then it can safely be removed.
2879 static int div_is_redundant(struct isl_basic_map *bmap, int div)
2882 unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
2884 for (i = 0; i < bmap->n_eq; ++i)
2885 if (!isl_int_is_zero(bmap->eq[i][pos]))
2888 for (i = 0; i < bmap->n_ineq; ++i) {
2889 if (isl_int_is_zero(bmap->ineq[i][pos]))
2891 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
2893 isl_int_sub(bmap->div[div][1],
2894 bmap->div[div][1], bmap->div[div][0]);
2895 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
2896 neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
2897 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
2898 isl_int_add(bmap->div[div][1],
2899 bmap->div[div][1], bmap->div[div][0]);
2902 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
2903 bmap->n_div-div-1) != -1)
2905 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
2906 if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
2908 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
2909 bmap->n_div-div-1) != -1)
2915 for (i = 0; i < bmap->n_div; ++i)
2916 if (!isl_int_is_zero(bmap->div[i][1+pos]))
2923 * Remove divs that don't occur in any of the constraints or other divs.
2924 * These can arise when dropping some of the variables in a quast
2925 * returned by piplib.
2927 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
2934 for (i = bmap->n_div-1; i >= 0; --i) {
2935 if (!div_is_redundant(bmap, i))
2937 bmap = isl_basic_map_drop_div(bmap, i);
2942 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
2944 bmap = remove_redundant_divs(bmap);
2947 F_SET(bmap, ISL_BASIC_SET_FINAL);
2951 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
2953 return (struct isl_basic_set *)
2954 isl_basic_map_finalize((struct isl_basic_map *)bset);
2957 struct isl_set *isl_set_finalize(struct isl_set *set)
2963 for (i = 0; i < set->n; ++i) {
2964 set->p[i] = isl_basic_set_finalize(set->p[i]);
2974 struct isl_map *isl_map_finalize(struct isl_map *map)
2980 for (i = 0; i < map->n; ++i) {
2981 map->p[i] = isl_basic_map_finalize(map->p[i]);
2991 struct isl_basic_map *isl_basic_map_identity(struct isl_ctx *ctx,
2992 unsigned nparam, unsigned dim)
2994 struct isl_basic_map *bmap;
2997 bmap = isl_basic_map_alloc(ctx, nparam, dim, dim, 0, dim, 0);
3001 for (i = 0; i < dim; ++i) {
3002 int j = isl_basic_map_alloc_equality(bmap);
3005 isl_seq_clr(bmap->eq[j],
3006 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
3007 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3008 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3010 return isl_basic_map_finalize(bmap);
3012 isl_basic_map_free(bmap);
3016 struct isl_map *isl_map_identity(struct isl_ctx *ctx,
3017 unsigned nparam, unsigned dim)
3019 struct isl_map *map = isl_map_alloc(ctx, nparam, dim, dim, 1,
3023 map = isl_map_add(map,
3024 isl_basic_map_identity(ctx, nparam, dim));
3031 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3033 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3036 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3038 return isl_map_is_subset(
3039 (struct isl_map *)set1, (struct isl_map *)set2);
3042 int isl_basic_map_is_subset(
3043 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3046 struct isl_map *map1;
3047 struct isl_map *map2;
3049 if (!bmap1 || !bmap2)
3052 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3053 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3055 is_subset = isl_map_is_subset(map1, map2);
3063 int isl_basic_map_is_equal(
3064 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3068 if (!bmap1 || !bmap2)
3070 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3073 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3077 int isl_basic_set_is_equal(
3078 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3080 return isl_basic_map_is_equal(
3081 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3084 int isl_map_is_empty(struct isl_map *map)
3091 for (i = 0; i < map->n; ++i) {
3092 is_empty = isl_basic_map_is_empty(map->p[i]);
3101 int isl_set_is_empty(struct isl_set *set)
3103 return isl_map_is_empty((struct isl_map *)set);
3106 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3110 struct isl_map *diff;
3115 if (isl_map_is_empty(map1))
3118 if (isl_map_is_empty(map2))
3121 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3125 is_subset = isl_map_is_empty(diff);
3131 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3137 is_subset = isl_map_is_subset(map1, map2);
3140 is_subset = isl_map_is_subset(map2, map1);
3144 int isl_basic_map_is_strict_subset(
3145 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3149 if (!bmap1 || !bmap2)
3151 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3154 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3155 if (is_subset == -1)
3160 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3166 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3167 if (total != vec->size)
3172 for (i = 0; i < bmap->n_eq; ++i) {
3173 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3174 if (!isl_int_is_zero(s)) {
3180 for (i = 0; i < bmap->n_ineq; ++i) {
3181 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3182 if (isl_int_is_neg(s)) {
3193 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3197 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3200 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3202 struct isl_basic_set *bset = NULL;
3203 struct isl_vec *sample = NULL;
3210 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3213 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3214 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3215 copy = isl_basic_map_convex_hull(copy);
3216 empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3217 isl_basic_map_free(copy);
3221 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3222 if (bmap->sample && bmap->sample->size == total) {
3223 int contains = basic_map_contains(bmap, bmap->sample);
3229 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3232 sample = isl_basic_set_sample(bset);
3235 empty = sample->size == 0;
3237 isl_vec_free(bmap->ctx, bmap->sample);
3238 bmap->sample = sample;
3243 struct isl_map *isl_basic_map_union(
3244 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3246 struct isl_map *map;
3247 if (!bmap1 || !bmap2)
3250 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
3251 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
3252 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
3254 map = isl_map_alloc(bmap1->ctx, bmap1->nparam,
3255 bmap1->n_in, bmap1->n_out, 2, 0);
3258 map = isl_map_add(map, bmap1);
3259 map = isl_map_add(map, bmap2);
3262 isl_basic_map_free(bmap1);
3263 isl_basic_map_free(bmap2);
3267 struct isl_set *isl_basic_set_union(
3268 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3270 return (struct isl_set *)isl_basic_map_union(
3271 (struct isl_basic_map *)bset1,
3272 (struct isl_basic_map *)bset2);
3275 /* Order divs such that any div only depends on previous divs */
3276 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3279 unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
3281 for (i = 0; i < bmap->n_div; ++i) {
3283 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3287 swap_div(bmap, i, pos);
3293 static int find_div(struct isl_basic_map *dst,
3294 struct isl_basic_map *src, unsigned div)
3298 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3300 for (i = 0; i < dst->n_div; ++i)
3301 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total) &&
3302 isl_seq_first_non_zero(dst->div[i]+1+1+total,
3303 dst->n_div - src->n_div) == -1)
3308 struct isl_basic_map *isl_basic_map_align_divs(
3309 struct isl_basic_map *dst, struct isl_basic_map *src)
3312 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3314 if (src->n_div == 0)
3317 src = order_divs(src);
3318 dst = isl_basic_map_extend(dst, dst->nparam, dst->n_in,
3319 dst->n_out, src->n_div, 0, 2 * src->n_div);
3322 for (i = 0; i < src->n_div; ++i) {
3323 int j = find_div(dst, src, i);
3325 j = isl_basic_map_alloc_div(dst);
3328 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
3329 isl_seq_clr(dst->div[j]+1+1+total,
3330 dst->extra - src->n_div);
3331 if (add_div_constraints(dst, j) < 0)
3335 swap_div(dst, i, j);
3339 isl_basic_map_free(dst);
3343 struct isl_map *isl_map_align_divs(struct isl_map *map)
3347 map = isl_map_compute_divs(map);
3348 map = isl_map_cow(map);
3352 for (i = 1; i < map->n; ++i)
3353 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3354 for (i = 1; i < map->n; ++i)
3355 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3360 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3361 struct isl_basic_map *src, isl_int *c,
3362 unsigned len, int oppose)
3364 struct isl_basic_map *copy = NULL;
3369 copy = isl_basic_map_copy(src);
3370 copy = isl_basic_map_cow(copy);
3373 copy = isl_basic_map_extend(copy,
3374 copy->nparam, copy->n_in, copy->n_out, 0, 0, 1);
3375 k = isl_basic_map_alloc_inequality(copy);
3379 isl_seq_neg(copy->ineq[k], c, len);
3381 isl_seq_cpy(copy->ineq[k], c, len);
3382 total = 1 + copy->nparam + copy->n_in + copy->n_out + copy->extra;
3383 isl_seq_clr(copy->ineq[k]+len, total - len);
3384 isl_inequality_negate(copy, k);
3385 copy = isl_basic_map_simplify(copy);
3386 copy = isl_basic_map_finalize(copy);
3387 is_empty = isl_basic_map_is_empty(copy);
3391 dst = isl_map_add(dst, copy);
3393 isl_basic_map_free(copy);
3396 isl_basic_map_free(copy);
3401 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3405 struct isl_map *rest = NULL;
3407 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3414 if (F_ISSET(map, ISL_MAP_DISJOINT))
3415 FL_SET(flags, ISL_MAP_DISJOINT);
3417 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3418 rest = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
3423 for (i = 0; i < map->n; ++i) {
3424 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3429 for (j = 0; j < map->n; ++j)
3430 map->p[j] = isl_basic_map_cow(map->p[j]);
3432 for (i = 0; i < bmap->n_eq; ++i) {
3433 for (j = 0; j < map->n; ++j) {
3434 rest = add_cut_constraint(rest,
3435 map->p[j], bmap->eq[i], 1+total, 0);
3439 rest = add_cut_constraint(rest,
3440 map->p[j], bmap->eq[i], 1+total, 1);
3444 map->p[j] = isl_basic_map_extend(map->p[j],
3445 map->p[j]->nparam, map->p[j]->n_in,
3446 map->p[j]->n_out, 0, 1, 0);
3449 k = isl_basic_map_alloc_equality(map->p[j]);
3452 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3453 isl_seq_clr(map->p[j]->eq[k]+1+total,
3454 map->p[j]->extra - bmap->n_div);
3458 for (i = 0; i < bmap->n_ineq; ++i) {
3459 for (j = 0; j < map->n; ++j) {
3460 rest = add_cut_constraint(rest,
3461 map->p[j], bmap->ineq[i], 1+total, 0);
3465 map->p[j] = isl_basic_map_extend(map->p[j],
3466 map->p[j]->nparam, map->p[j]->n_in,
3467 map->p[j]->n_out, 0, 0, 1);
3470 k = isl_basic_map_alloc_inequality(map->p[j]);
3473 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3474 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3475 map->p[j]->extra - bmap->n_div);
3487 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3493 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3494 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
3495 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
3497 if (isl_map_is_empty(map2)) {
3502 map1 = isl_map_compute_divs(map1);
3503 map2 = isl_map_compute_divs(map2);
3507 for (i = 0; map1 && i < map2->n; ++i)
3508 map1 = subtract(map1, map2->p[i]);
3518 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3520 return (struct isl_set *)
3522 (struct isl_map *)set1, (struct isl_map *)set2);
3525 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3529 isl_assert(set->ctx, set->dim == map->n_in, goto error);
3530 map = isl_map_intersect_domain(map, set);
3531 set = isl_map_range(map);
3539 /* There is no need to cow as removing empty parts doesn't change
3540 * the meaning of the set.
3542 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3549 for (i = map->n-1; i >= 0; --i) {
3550 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3552 isl_basic_map_free(map->p[i]);
3554 map->p[i] = map->p[map->n-1];
3561 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3563 return (struct isl_set *)
3564 isl_map_remove_empty_parts((struct isl_map *)set);
3567 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3569 struct isl_basic_set *bset;
3570 if (!set || set->n == 0)
3572 bset = set->p[set->n-1];
3573 isl_assert(set->ctx, F_ISSET(bset, ISL_BASIC_SET_FINAL), return NULL);
3574 return isl_basic_set_copy(bset);
3577 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3578 struct isl_basic_set *bset)
3584 for (i = set->n-1; i >= 0; --i) {
3585 if (set->p[i] != bset)
3587 set = isl_set_cow(set);
3590 isl_basic_set_free(set->p[i]);
3592 set->p[i] = set->p[set->n-1];
3596 isl_basic_set_free(bset);
3600 isl_basic_set_free(bset);