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_vars(
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 struct isl_set *isl_set_drop_vars(
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_vars(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);
960 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
961 swap_inequality(bmap, k, l);
962 isl_basic_map_drop_inequality(bmap, k);
966 for (k = 0; k < bmap->n_ineq-1; ++k) {
967 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
968 h = hash_index(index, size, bits, bmap, k);
969 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
973 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
974 if (isl_int_is_pos(sum))
976 if (isl_int_is_zero(sum)) {
977 /* We need to break out of the loop after these
978 * changes since the contents of the hash
979 * will no longer be valid.
980 * Plus, we probably we want to regauss first.
982 isl_basic_map_drop_inequality(bmap, l);
983 isl_basic_map_inequality_to_equality(bmap, k);
985 bmap = isl_basic_map_set_to_empty(bmap);
994 static struct isl_basic_map *remove_duplicate_divs(
995 struct isl_basic_map *bmap, int *progress)
1002 unsigned total_var = bmap->nparam + bmap->n_in + bmap->n_out;
1003 unsigned total = total_var + bmap->n_div;
1004 struct isl_ctx *ctx;
1006 if (bmap->n_div <= 1)
1010 for (k = bmap->n_div - 1; k >= 0; --k)
1011 if (!isl_int_is_zero(bmap->div[k][0]))
1016 size = round_up(4 * bmap->n_div / 3 - 1);
1017 bits = ffs(size) - 1;
1018 index = isl_alloc_array(ctx, int, size);
1019 memset(index, 0, size * sizeof(int));
1022 eq = isl_blk_alloc(ctx, 1+total);
1023 if (isl_blk_is_error(eq))
1026 isl_seq_clr(eq.data, 1+total);
1027 index[isl_seq_hash(bmap->div[k], 2+total, bits)] = k + 1;
1028 for (--k; k >= 0; --k) {
1031 if (isl_int_is_zero(bmap->div[k][0]))
1034 hash = isl_seq_hash(bmap->div[k], 2+total, bits);
1035 for (h = hash; index[h]; h = (h+1) % size)
1036 if (isl_seq_eq(bmap->div[k],
1037 bmap->div[index[h]-1], 2+total))
1042 isl_int_set_si(eq.data[1+total_var+k], -1);
1043 isl_int_set_si(eq.data[1+total_var+l], 1);
1044 eliminate_div(bmap, eq.data, l);
1045 isl_int_set_si(eq.data[1+total_var+k], 0);
1046 isl_int_set_si(eq.data[1+total_var+l], 0);
1051 isl_blk_free(ctx, eq);
1057 /* Elimininate divs based on equalities
1059 static struct isl_basic_map *eliminate_divs_eq(
1060 struct isl_basic_map *bmap, int *progress)
1070 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1072 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1073 for (i = 0; i < bmap->n_eq; ++i) {
1074 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
1075 !isl_int_is_negone(bmap->eq[i][off + d]))
1079 eliminate_div(bmap, bmap->eq[i], d);
1080 isl_basic_map_drop_equality(bmap, i);
1085 return eliminate_divs_eq(bmap, progress);
1089 static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
1093 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1096 for (i = bmap->n_eq - 1; i >= 0; --i) {
1097 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
1098 if (isl_int_is_zero(gcd)) {
1099 if (!isl_int_is_zero(bmap->eq[i][0])) {
1100 bmap = isl_basic_map_set_to_empty(bmap);
1103 isl_basic_map_drop_equality(bmap, i);
1106 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1107 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
1108 if (isl_int_is_one(gcd))
1110 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
1111 bmap = isl_basic_map_set_to_empty(bmap);
1114 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
1117 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1118 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
1119 if (isl_int_is_zero(gcd)) {
1120 if (isl_int_is_neg(bmap->ineq[i][0])) {
1121 bmap = isl_basic_map_set_to_empty(bmap);
1124 isl_basic_map_drop_inequality(bmap, i);
1127 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1128 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
1129 if (isl_int_is_one(gcd))
1131 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
1132 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
1140 /* Eliminate the specified variables from the constraints using
1141 * Fourier-Motzkin. The variables themselves are not removed.
1143 struct isl_basic_map *isl_basic_map_eliminate_vars(
1144 struct isl_basic_map *bmap, int pos, unsigned n)
1153 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1154 extra = bmap->extra - bmap->n_div;
1156 bmap = isl_basic_map_cow(bmap);
1157 bmap = isl_basic_map_gauss(bmap, NULL);
1158 for (d = pos + n - 1; d >= pos; --d) {
1159 int n_lower, n_upper;
1162 for (i = 0; i < bmap->n_eq; ++i) {
1163 if (isl_int_is_zero(bmap->eq[i][1+d]))
1165 isl_basic_map_drop_equality(bmap, i);
1172 for (i = 0; i < bmap->n_ineq; ++i) {
1173 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1175 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1178 bmap = isl_basic_map_extend(bmap,
1179 bmap->nparam, bmap->n_in, bmap->n_out, 0,
1180 0, n_lower * n_upper);
1181 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1183 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1186 for (j = 0; j < i; ++j) {
1187 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1190 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1191 isl_int_sgn(bmap->ineq[j][1+d]))
1193 k = isl_basic_map_alloc_inequality(bmap);
1196 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1198 isl_seq_clr(bmap->ineq[k]+1+total, extra);
1199 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1200 1+d, 1+total, NULL);
1202 isl_basic_map_drop_inequality(bmap, i);
1205 if (n_lower > 0 && n_upper > 0) {
1206 bmap = normalize_constraints(bmap);
1207 bmap = isl_basic_map_gauss(bmap, NULL);
1208 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1214 isl_basic_map_free(bmap);
1218 struct isl_basic_set *isl_basic_set_eliminate_vars(
1219 struct isl_basic_set *bset, unsigned pos, unsigned n)
1221 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1222 (struct isl_basic_map *)bset, pos, n);
1225 /* Elimininate divs based on inequalities
1227 static struct isl_basic_map *eliminate_divs_ineq(
1228 struct isl_basic_map *bmap, int *progress)
1233 struct isl_ctx *ctx;
1239 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1241 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1242 for (i = 0; i < bmap->n_eq; ++i)
1243 if (!isl_int_is_zero(bmap->eq[i][off + d]))
1247 for (i = 0; i < bmap->n_ineq; ++i)
1248 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
1250 if (i < bmap->n_ineq)
1253 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
1254 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1256 bmap = isl_basic_map_drop_div(bmap, d);
1263 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1270 bmap = normalize_constraints(bmap);
1271 bmap = eliminate_divs_eq(bmap, &progress);
1272 bmap = eliminate_divs_ineq(bmap, &progress);
1273 bmap = isl_basic_map_gauss(bmap, &progress);
1274 bmap = remove_duplicate_divs(bmap, &progress);
1275 bmap = remove_duplicate_constraints(bmap, &progress);
1280 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1282 return (struct isl_basic_set *)
1283 isl_basic_map_simplify((struct isl_basic_map *)bset);
1286 static void dump_term(struct isl_basic_map *bmap,
1287 isl_int c, int pos, FILE *out)
1289 unsigned in = bmap->n_in;
1290 unsigned dim = bmap->n_in + bmap->n_out;
1292 isl_int_print(out, c);
1294 if (!isl_int_is_one(c))
1295 isl_int_print(out, c);
1296 if (pos < 1 + bmap->nparam)
1297 fprintf(out, "p%d", pos - 1);
1298 else if (pos < 1 + bmap->nparam + in)
1299 fprintf(out, "i%d", pos - 1 - bmap->nparam);
1300 else if (pos < 1 + bmap->nparam + dim)
1301 fprintf(out, "o%d", pos - 1 - bmap->nparam - in);
1303 fprintf(out, "e%d", pos - 1 - bmap->nparam - dim);
1307 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1308 int sign, FILE *out)
1312 unsigned len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1316 for (i = 0, first = 1; i < len; ++i) {
1317 if (isl_int_sgn(c[i]) * sign <= 0)
1320 fprintf(out, " + ");
1322 isl_int_abs(v, c[i]);
1323 dump_term(bmap, v, i, out);
1330 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1331 const char *op, FILE *out, int indent)
1335 fprintf(out, "%*s", indent, "");
1337 dump_constraint_sign(bmap, c, 1, out);
1338 fprintf(out, " %s ", op);
1339 dump_constraint_sign(bmap, c, -1, out);
1343 for (i = bmap->n_div; i < bmap->extra; ++i) {
1344 if (isl_int_is_zero(c[1+bmap->nparam+bmap->n_in+bmap->n_out+i]))
1346 fprintf(out, "%*s", indent, "");
1347 fprintf(out, "ERROR: unused div coefficient not zero\n");
1351 static void dump_constraints(struct isl_basic_map *bmap,
1352 isl_int **c, unsigned n,
1353 const char *op, FILE *out, int indent)
1357 for (i = 0; i < n; ++i)
1358 dump_constraint(bmap, c[i], op, out, indent);
1361 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1365 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1367 for (j = 0; j < 1 + total; ++j) {
1368 if (isl_int_is_zero(exp[j]))
1370 if (!first && isl_int_is_pos(exp[j]))
1372 dump_term(bmap, exp[j], j, out);
1377 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1381 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1382 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1384 for (i = 0; i < bmap->n_div; ++i) {
1385 fprintf(out, "%*s", indent, "");
1386 fprintf(out, "e%d = [(", i);
1387 dump_affine(bmap, bmap->div[i]+1, out);
1389 isl_int_print(out, bmap->div[i][0]);
1390 fprintf(out, "]\n");
1394 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1397 fprintf(out, "null basic set\n");
1401 fprintf(out, "%*s", indent, "");
1402 fprintf(out, "nparam: %d, dim: %d, extra: %d\n",
1403 bset->nparam, bset->dim, bset->extra);
1404 dump((struct isl_basic_map *)bset, out, indent);
1407 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1410 fprintf(out, "null basic map\n");
1414 fprintf(out, "%*s", indent, "");
1415 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, flags: %x\n",
1417 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, bmap->flags);
1418 dump(bmap, out, indent);
1421 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1423 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1426 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1427 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1428 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1432 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1433 unsigned nparam, unsigned dim, int n, unsigned flags)
1435 struct isl_set *set;
1437 isl_assert(ctx, n >= 0, return NULL);
1438 set = isl_alloc(ctx, struct isl_set,
1439 sizeof(struct isl_set) +
1440 n * sizeof(struct isl_basic_set *));
1449 set->nparam = nparam;
1456 struct isl_set *isl_set_dup(struct isl_set *set)
1459 struct isl_set *dup;
1464 dup = isl_set_alloc(set->ctx, set->nparam, set->dim, set->n, set->flags);
1467 for (i = 0; i < set->n; ++i)
1468 dup = isl_set_add(dup,
1469 isl_basic_set_dup(set->p[i]));
1473 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1475 struct isl_set *set;
1480 set = isl_set_alloc(bset->ctx, bset->nparam, bset->dim, 1, ISL_MAP_DISJOINT);
1482 isl_basic_set_free(bset);
1485 return isl_set_add(set, bset);
1488 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1490 struct isl_map *map;
1495 map = isl_map_alloc(bmap->ctx, bmap->nparam, bmap->n_in, bmap->n_out, 1,
1498 isl_basic_map_free(bmap);
1501 return isl_map_add(map, bmap);
1504 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1508 isl_assert(set->ctx, set->nparam == bset->nparam, goto error);
1509 isl_assert(set->ctx, set->dim == bset->dim, goto error);
1510 isl_assert(set->ctx, set->n < set->size, goto error);
1511 set->p[set->n] = bset;
1518 isl_basic_set_free(bset);
1522 void isl_set_free(struct isl_set *set)
1532 isl_ctx_deref(set->ctx);
1533 for (i = 0; i < set->n; ++i)
1534 isl_basic_set_free(set->p[i]);
1538 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1543 fprintf(out, "null set\n");
1547 fprintf(out, "%*s", indent, "");
1548 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1549 set->ref, set->n, set->nparam, set->dim, set->flags);
1550 for (i = 0; i < set->n; ++i) {
1551 fprintf(out, "%*s", indent, "");
1552 fprintf(out, "basic set %d:\n", i);
1553 isl_basic_set_dump(set->p[i], out, indent+4);
1557 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1562 fprintf(out, "null map\n");
1566 fprintf(out, "%*s", indent, "");
1567 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, flags: %x\n",
1568 map->ref, map->n, map->nparam, map->n_in, map->n_out,
1570 for (i = 0; i < map->n; ++i) {
1571 fprintf(out, "%*s", indent, "");
1572 fprintf(out, "basic map %d:\n", i);
1573 isl_basic_map_dump(map->p[i], out, indent+4);
1577 struct isl_basic_map *isl_basic_map_intersect_domain(
1578 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1580 struct isl_basic_map *bmap_domain;
1585 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1586 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1588 bmap = isl_basic_map_extend(bmap,
1589 bset->nparam, bmap->n_in, bmap->n_out,
1590 bset->n_div, bset->n_eq, bset->n_ineq);
1593 bmap_domain = isl_basic_map_from_basic_set(bset,
1595 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1597 bmap = isl_basic_map_simplify(bmap);
1598 return isl_basic_map_finalize(bmap);
1600 isl_basic_map_free(bmap);
1601 isl_basic_set_free(bset);
1605 struct isl_basic_map *isl_basic_map_intersect_range(
1606 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1608 struct isl_basic_map *bmap_range;
1613 isl_assert(bset->ctx, bset->dim == bmap->n_out, goto error);
1614 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1616 bmap = isl_basic_map_extend(bmap,
1617 bset->nparam, bmap->n_in, bmap->n_out,
1618 bset->n_div, bset->n_eq, bset->n_ineq);
1621 bmap_range = isl_basic_map_from_basic_set(bset,
1623 bmap = add_constraints(bmap, bmap_range, 0, 0);
1625 bmap = isl_basic_map_simplify(bmap);
1626 return isl_basic_map_finalize(bmap);
1628 isl_basic_map_free(bmap);
1629 isl_basic_set_free(bset);
1633 struct isl_basic_map *isl_basic_map_intersect(
1634 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1636 if (!bmap1 || !bmap2)
1639 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
1640 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
1641 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
1643 bmap1 = isl_basic_map_extend(bmap1,
1644 bmap1->nparam, bmap1->n_in, bmap1->n_out,
1645 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1648 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1650 bmap1 = isl_basic_map_simplify(bmap1);
1651 return isl_basic_map_finalize(bmap1);
1653 isl_basic_map_free(bmap1);
1654 isl_basic_map_free(bmap2);
1658 struct isl_basic_set *isl_basic_set_intersect(
1659 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1661 return (struct isl_basic_set *)
1662 isl_basic_map_intersect(
1663 (struct isl_basic_map *)bset1,
1664 (struct isl_basic_map *)bset2);
1667 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1670 struct isl_map *result;
1676 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
1677 F_ISSET(map2, ISL_MAP_DISJOINT))
1678 FL_SET(flags, ISL_MAP_DISJOINT);
1680 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
1681 map1->n * map2->n, flags);
1684 for (i = 0; i < map1->n; ++i)
1685 for (j = 0; j < map2->n; ++j) {
1686 struct isl_basic_map *part;
1687 part = isl_basic_map_intersect(
1688 isl_basic_map_copy(map1->p[i]),
1689 isl_basic_map_copy(map2->p[j]));
1690 if (isl_basic_map_is_empty(part))
1691 isl_basic_map_free(part);
1693 result = isl_map_add(result, part);
1706 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1708 return (struct isl_set *)
1709 isl_map_intersect((struct isl_map *)set1,
1710 (struct isl_map *)set2);
1713 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1715 struct isl_basic_set *bset;
1720 bmap = isl_basic_map_cow(bmap);
1724 bset = isl_basic_set_from_basic_map(bmap);
1725 bset = isl_basic_set_swap_vars(bset, in);
1726 return isl_basic_map_from_basic_set(bset, bset->dim-in, in);
1729 /* Turn final n dimensions into existentially quantified variables.
1731 struct isl_basic_set *isl_basic_set_project_out(
1732 struct isl_basic_set *bset, unsigned n, unsigned flags)
1742 isl_assert(bset->ctx, n <= bset->dim, goto error);
1747 bset = isl_basic_set_cow(bset);
1749 row_size = 1 + bset->nparam + bset->dim + bset->extra;
1750 old = bset->block2.data;
1751 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1752 (bset->extra + n) * (1 + row_size));
1753 if (!bset->block2.data)
1755 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1758 for (i = 0; i < n; ++i) {
1759 new_div[i] = bset->block2.data +
1760 (bset->extra + i) * (1 + row_size);
1761 isl_seq_clr(new_div[i], 1 + row_size);
1763 for (i = 0; i < bset->extra; ++i)
1764 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1766 bset->div = new_div;
1770 bset = isl_basic_set_simplify(bset);
1771 return isl_basic_set_finalize(bset);
1773 isl_basic_set_free(bset);
1777 struct isl_basic_map *isl_basic_map_apply_range(
1778 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1780 struct isl_basic_set *bset;
1781 unsigned n_in, n_out;
1783 if (!bmap1 || !bmap2)
1786 isl_assert(bmap->ctx, bmap1->n_out == bmap2->n_in, goto error);
1787 isl_assert(bmap->ctx, bmap1->nparam == bmap2->nparam, goto error);
1790 n_out = bmap2->n_out;
1792 bmap2 = isl_basic_map_reverse(bmap2);
1795 bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
1796 bmap1->n_in + bmap2->n_in, bmap1->n_out,
1797 bmap2->extra, bmap2->n_eq, bmap2->n_ineq);
1800 bmap1 = add_constraints(bmap1, bmap2, bmap1->n_in - bmap2->n_in, 0);
1801 bmap1 = isl_basic_map_simplify(bmap1);
1802 bset = isl_basic_set_from_basic_map(bmap1);
1803 bset = isl_basic_set_project_out(bset,
1804 bset->dim - (n_in + n_out), 0);
1805 return isl_basic_map_from_basic_set(bset, n_in, n_out);
1807 isl_basic_map_free(bmap1);
1808 isl_basic_map_free(bmap2);
1812 struct isl_basic_set *isl_basic_set_apply(
1813 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1818 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1820 return (struct isl_basic_set *)
1821 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1823 isl_basic_set_free(bset);
1824 isl_basic_map_free(bmap);
1828 struct isl_basic_map *isl_basic_map_apply_domain(
1829 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1831 if (!bmap1 || !bmap2)
1834 isl_assert(ctx, bmap1->n_in == bmap2->n_in, goto error);
1835 isl_assert(ctx, bmap1->nparam == bmap2->nparam, goto error);
1837 bmap1 = isl_basic_map_reverse(bmap1);
1838 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1839 return isl_basic_map_reverse(bmap1);
1841 isl_basic_map_free(bmap1);
1842 isl_basic_map_free(bmap2);
1846 static struct isl_basic_map *var_equal(struct isl_ctx *ctx,
1847 struct isl_basic_map *bmap, unsigned pos)
1850 i = isl_basic_map_alloc_equality(bmap);
1853 isl_seq_clr(bmap->eq[i],
1854 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1855 isl_int_set_si(bmap->eq[i][1+bmap->nparam+pos], -1);
1856 isl_int_set_si(bmap->eq[i][1+bmap->nparam+bmap->n_in+pos], 1);
1857 return isl_basic_map_finalize(bmap);
1859 isl_basic_map_free(bmap);
1863 static struct isl_basic_map *var_more(struct isl_ctx *ctx,
1864 struct isl_basic_map *bmap, unsigned pos)
1867 i = isl_basic_map_alloc_inequality(bmap);
1870 isl_seq_clr(bmap->ineq[i],
1871 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1872 isl_int_set_si(bmap->ineq[i][0], -1);
1873 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], -1);
1874 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], 1);
1875 return isl_basic_map_finalize(bmap);
1877 isl_basic_map_free(bmap);
1881 static struct isl_basic_map *var_less(struct isl_ctx *ctx,
1882 struct isl_basic_map *bmap, unsigned pos)
1885 i = isl_basic_map_alloc_inequality(bmap);
1888 isl_seq_clr(bmap->ineq[i],
1889 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1890 isl_int_set_si(bmap->ineq[i][0], -1);
1891 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], 1);
1892 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], -1);
1893 return isl_basic_map_finalize(bmap);
1895 isl_basic_map_free(bmap);
1899 struct isl_basic_map *isl_basic_map_equal(struct isl_ctx *ctx,
1900 unsigned nparam, unsigned in, unsigned out, unsigned n_equal)
1903 struct isl_basic_map *bmap;
1904 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, n_equal, 0);
1907 for (i = 0; i < n_equal && bmap; ++i)
1908 bmap = var_equal(ctx, bmap, i);
1909 return isl_basic_map_finalize(bmap);
1912 struct isl_basic_map *isl_basic_map_less_at(struct isl_ctx *ctx,
1913 unsigned nparam, unsigned in, unsigned out, unsigned pos)
1916 struct isl_basic_map *bmap;
1917 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
1920 for (i = 0; i < pos && bmap; ++i)
1921 bmap = var_equal(ctx, bmap, i);
1923 bmap = var_less(ctx, bmap, pos);
1924 return isl_basic_map_finalize(bmap);
1927 struct isl_basic_map *isl_basic_map_more_at(struct isl_ctx *ctx,
1928 unsigned nparam, unsigned in, unsigned out, unsigned pos)
1931 struct isl_basic_map *bmap;
1932 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
1935 for (i = 0; i < pos && bmap; ++i)
1936 bmap = var_equal(ctx, bmap, i);
1938 bmap = var_more(ctx, bmap, pos);
1939 return isl_basic_map_finalize(bmap);
1942 struct isl_basic_map *isl_basic_map_from_basic_set(
1943 struct isl_basic_set *bset, unsigned n_in, unsigned n_out)
1945 struct isl_basic_map *bmap;
1947 bset = isl_basic_set_cow(bset);
1951 isl_assert(ctx, bset->dim == n_in + n_out, goto error);
1952 bmap = (struct isl_basic_map *) bset;
1954 bmap->n_out = n_out;
1955 return isl_basic_map_finalize(bmap);
1957 isl_basic_set_free(bset);
1961 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
1965 if (bmap->n_in == 0)
1966 return (struct isl_basic_set *)bmap;
1967 bmap = isl_basic_map_cow(bmap);
1970 bmap->n_out += bmap->n_in;
1972 bmap = isl_basic_map_finalize(bmap);
1973 return (struct isl_basic_set *)bmap;
1978 /* For a div d = floor(f/m), add the constraints
1981 * -(f-(n-1)) + m d >= 0
1983 * Note that the second constraint is the negation of
1987 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
1990 unsigned div_pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
1991 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
1993 i = isl_basic_map_alloc_inequality(bmap);
1996 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
1997 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
1999 j = isl_basic_map_alloc_inequality(bmap);
2002 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2003 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2004 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2008 struct isl_basic_set *isl_basic_map_underlying_set(
2009 struct isl_basic_map *bmap)
2013 if (bmap->nparam == 0 && bmap->n_in == 0 && bmap->n_div == 0)
2014 return (struct isl_basic_set *)bmap;
2015 bmap = isl_basic_map_cow(bmap);
2018 bmap->n_out += bmap->nparam + bmap->n_in + bmap->n_div;
2021 bmap->extra -= bmap->n_div;
2023 bmap = isl_basic_map_finalize(bmap);
2024 return (struct isl_basic_set *)bmap;
2029 struct isl_basic_map *isl_basic_map_overlying_set(
2030 struct isl_basic_set *bset, struct isl_basic_map *like)
2032 struct isl_basic_map *bmap;
2033 struct isl_ctx *ctx;
2040 isl_assert(ctx, bset->dim ==
2041 like->nparam + like->n_in + like->n_out + like->n_div,
2043 if (like->nparam == 0 && like->n_in == 0 && like->n_div == 0) {
2044 isl_basic_map_free(like);
2045 return (struct isl_basic_map *)bset;
2047 bset = isl_basic_set_cow(bset);
2050 total = bset->dim + bset->extra;
2051 bmap = (struct isl_basic_map *)bset;
2052 bmap->nparam = like->nparam;
2053 bmap->n_in = like->n_in;
2054 bmap->n_out = like->n_out;
2055 bmap->extra += like->n_div;
2058 ltotal = total - bmap->extra + like->extra;
2061 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2062 bmap->extra * (1 + 1 + total));
2063 if (isl_blk_is_error(bmap->block2))
2065 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2069 bmap = isl_basic_map_extend(bmap, bmap->nparam,
2070 bmap->n_in, bmap->n_out, 0, 0, 2 * like->n_div);
2071 for (i = 0; i < like->n_div; ++i) {
2072 k = isl_basic_map_alloc_div(bmap);
2075 isl_seq_cpy(bmap->div[k], like->div[i], 1 + 1 + ltotal);
2076 isl_seq_clr(bmap->div[k]+1+1+ltotal, total - ltotal);
2077 if (add_div_constraints(bmap, k) < 0)
2081 isl_basic_map_free(like);
2082 bmap = isl_basic_map_finalize(bmap);
2085 isl_basic_map_free(like);
2086 isl_basic_set_free(bset);
2090 struct isl_basic_set *isl_basic_set_from_underlying_set(
2091 struct isl_basic_set *bset, struct isl_basic_set *like)
2093 return (struct isl_basic_set *)
2094 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2097 struct isl_set *isl_set_from_underlying_set(
2098 struct isl_set *set, struct isl_basic_set *like)
2104 isl_assert(set->ctx, set->dim == like->nparam + like->dim + like->n_div,
2106 if (like->nparam == 0 && like->n_div == 0) {
2107 isl_basic_set_free(like);
2110 set = isl_set_cow(set);
2113 for (i = 0; i < set->n; ++i) {
2114 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2115 isl_basic_set_copy(like));
2119 set->nparam = like->nparam;
2120 set->dim = like->dim;
2121 isl_basic_set_free(like);
2124 isl_basic_set_free(like);
2129 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2133 map = isl_map_align_divs(map);
2134 map = isl_map_cow(map);
2138 for (i = 0; i < map->n; ++i) {
2139 map->p[i] = (struct isl_basic_map *)
2140 isl_basic_map_underlying_set(map->p[i]);
2145 map->n_out += map->nparam + map->n_in;
2147 map->n_out = map->p[0]->n_out;
2150 return (struct isl_set *)map;
2156 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2158 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2161 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2163 struct isl_basic_set *domain;
2167 n_out = bmap->n_out;
2168 domain = isl_basic_set_from_basic_map(bmap);
2169 return isl_basic_set_project_out(domain, n_out, 0);
2172 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2174 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2177 struct isl_set *isl_map_range(struct isl_map *map)
2180 struct isl_set *set;
2184 map = isl_map_cow(map);
2188 set = (struct isl_set *) map;
2190 for (i = 0; i < map->n; ++i) {
2191 set->p[i] = isl_basic_map_range(map->p[i]);
2195 F_CLR(set, ISL_MAP_DISJOINT);
2202 struct isl_map *isl_map_from_set(struct isl_set *set,
2203 unsigned n_in, unsigned n_out)
2206 struct isl_map *map = NULL;
2210 isl_assert(set->ctx, set->dim == n_in + n_out, goto error);
2211 set = isl_set_cow(set);
2214 map = (struct isl_map *)set;
2215 for (i = 0; i < set->n; ++i) {
2216 map->p[i] = isl_basic_map_from_basic_set(
2217 set->p[i], n_in, n_out);
2229 struct isl_set *isl_set_from_map(struct isl_map *map)
2232 struct isl_set *set = NULL;
2236 map = isl_map_cow(map);
2239 map->n_out += map->n_in;
2241 set = (struct isl_set *)map;
2242 for (i = 0; i < map->n; ++i) {
2243 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2253 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2254 unsigned nparam, unsigned in, unsigned out, int n,
2257 struct isl_map *map;
2259 map = isl_alloc(ctx, struct isl_map,
2260 sizeof(struct isl_map) +
2261 n * sizeof(struct isl_basic_map *));
2270 map->nparam = nparam;
2277 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2278 unsigned nparam, unsigned in, unsigned out)
2280 struct isl_basic_map *bmap;
2281 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2282 bmap = isl_basic_map_set_to_empty(bmap);
2286 struct isl_basic_set *isl_basic_set_empty(struct isl_ctx *ctx,
2287 unsigned nparam, unsigned dim)
2289 struct isl_basic_set *bset;
2290 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 1, 0);
2291 bset = isl_basic_set_set_to_empty(bset);
2295 struct isl_basic_map *isl_basic_map_universe(struct isl_ctx *ctx,
2296 unsigned nparam, unsigned in, unsigned out)
2298 struct isl_basic_map *bmap;
2299 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 0, 0);
2303 struct isl_basic_set *isl_basic_set_universe(struct isl_ctx *ctx,
2304 unsigned nparam, unsigned dim)
2306 struct isl_basic_set *bset;
2307 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 0, 0);
2311 struct isl_map *isl_map_empty(struct isl_ctx *ctx,
2312 unsigned nparam, unsigned in, unsigned out)
2314 return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
2317 struct isl_set *isl_set_empty(struct isl_ctx *ctx,
2318 unsigned nparam, unsigned dim)
2320 return isl_set_alloc(ctx, nparam, dim, 0, ISL_MAP_DISJOINT);
2323 struct isl_map *isl_map_dup(struct isl_map *map)
2326 struct isl_map *dup;
2330 dup = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out, map->n,
2332 for (i = 0; i < map->n; ++i)
2333 dup = isl_map_add(dup,
2334 isl_basic_map_dup(map->p[i]));
2338 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2342 isl_assert(map->ctx, map->nparam == bmap->nparam, goto error);
2343 isl_assert(map->ctx, map->n_in == bmap->n_in, goto error);
2344 isl_assert(map->ctx, map->n_out == bmap->n_out, goto error);
2345 isl_assert(map->ctx, map->n < map->size, goto error);
2346 map->p[map->n] = bmap;
2353 isl_basic_map_free(bmap);
2357 void isl_map_free(struct isl_map *map)
2367 isl_ctx_deref(map->ctx);
2368 for (i = 0; i < map->n; ++i)
2369 isl_basic_map_free(map->p[i]);
2373 struct isl_map *isl_map_extend(struct isl_map *base,
2374 unsigned nparam, unsigned n_in, unsigned n_out)
2378 base = isl_map_cow(base);
2382 isl_assert(base->ctx, base->nparam <= nparam, goto error);
2383 isl_assert(base->ctx, base->n_in <= n_in, goto error);
2384 isl_assert(base->ctx, base->n_out <= n_out, goto error);
2385 base->nparam = nparam;
2387 base->n_out = n_out;
2388 for (i = 0; i < base->n; ++i) {
2389 base->p[i] = isl_basic_map_extend(base->p[i],
2390 nparam, n_in, n_out, 0, 0, 0);
2400 struct isl_set *isl_set_extend(struct isl_set *base,
2401 unsigned nparam, unsigned dim)
2403 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2407 static struct isl_basic_map *isl_basic_map_fix_var(struct isl_basic_map *bmap,
2408 unsigned var, int value)
2412 bmap = isl_basic_map_cow(bmap);
2413 bmap = isl_basic_map_extend(bmap,
2414 bmap->nparam, bmap->n_in, bmap->n_out, 0, 1, 0);
2415 j = isl_basic_map_alloc_equality(bmap);
2418 isl_seq_clr(bmap->eq[j],
2419 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2420 isl_int_set_si(bmap->eq[j][1 + var], -1);
2421 isl_int_set_si(bmap->eq[j][0], value);
2422 bmap = isl_basic_map_simplify(bmap);
2423 return isl_basic_map_finalize(bmap);
2425 isl_basic_map_free(bmap);
2429 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2430 unsigned input, int value)
2434 isl_assert(bmap->ctx, input < bmap->n_in, goto error);
2435 return isl_basic_map_fix_var(bmap, bmap->nparam + input, value);
2437 isl_basic_map_free(bmap);
2441 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2442 unsigned dim, int value)
2446 isl_assert(bset->ctx, dim < bset->dim, goto error);
2447 return (struct isl_basic_set *)
2448 isl_basic_map_fix_var((struct isl_basic_map *)bset,
2449 bset->nparam + dim, value);
2451 isl_basic_set_free(bset);
2455 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2456 unsigned input, int value)
2460 map = isl_map_cow(map);
2464 isl_assert(ctx, input < map->n_in, goto error);
2465 for (i = 0; i < map->n; ++i) {
2466 map->p[i] = isl_basic_map_fix_input_si(map->p[i], input, value);
2476 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2480 set = isl_set_cow(set);
2484 isl_assert(set->ctx, dim < set->dim, goto error);
2485 for (i = 0; i < set->n; ++i) {
2486 set->p[i] = isl_basic_set_fix_dim_si(set->p[i], dim, value);
2496 struct isl_map *isl_map_reverse(struct isl_map *map)
2501 map = isl_map_cow(map);
2506 map->n_in = map->n_out;
2508 for (i = 0; i < map->n; ++i) {
2509 map->p[i] = isl_basic_map_reverse(map->p[i]);
2519 struct isl_map *isl_basic_map_lexmax(
2520 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2521 struct isl_set **empty)
2523 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2526 struct isl_map *isl_basic_map_lexmin(
2527 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2528 struct isl_set **empty)
2530 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2533 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2535 struct isl_basic_map *bmap = NULL;
2536 struct isl_basic_set *dom = NULL;
2537 struct isl_map *min;
2541 bmap = isl_basic_map_from_basic_set(bset, 0, bset->dim);
2544 dom = isl_basic_set_alloc(bmap->ctx, bmap->nparam, 0, 0, 0, 0);
2547 min = isl_basic_map_lexmin(bmap, dom, NULL);
2548 return isl_map_range(min);
2550 isl_basic_map_free(bmap);
2554 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2558 if (bmap->n_div == 0)
2559 return isl_map_from_basic_map(bmap);
2560 return isl_pip_basic_map_compute_divs(bmap);
2563 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2566 struct isl_map *res;
2572 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2573 for (i = 1 ; i < map->n; ++i) {
2575 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2576 if (F_ISSET(map, ISL_MAP_DISJOINT))
2577 res = isl_map_union_disjoint(res, r2);
2579 res = isl_map_union(res, r2);
2586 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2588 return (struct isl_set *)
2589 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2592 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2594 return (struct isl_set *)
2595 isl_map_compute_divs((struct isl_map *)set);
2598 struct isl_set *isl_map_domain(struct isl_map *map)
2601 struct isl_set *set;
2606 map = isl_map_cow(map);
2610 set = (struct isl_set *)map;
2611 set->dim = map->n_in;
2613 for (i = 0; i < map->n; ++i) {
2614 set->p[i] = isl_basic_map_domain(map->p[i]);
2618 F_CLR(set, ISL_MAP_DISJOINT);
2625 struct isl_map *isl_map_union_disjoint(
2626 struct isl_map *map1, struct isl_map *map2)
2630 struct isl_map *map = NULL;
2644 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
2645 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
2646 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
2648 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2649 F_ISSET(map2, ISL_MAP_DISJOINT))
2650 FL_SET(flags, ISL_MAP_DISJOINT);
2652 map = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
2653 map1->n + map2->n, flags);
2656 for (i = 0; i < map1->n; ++i) {
2657 map = isl_map_add(map,
2658 isl_basic_map_copy(map1->p[i]));
2662 for (i = 0; i < map2->n; ++i) {
2663 map = isl_map_add(map,
2664 isl_basic_map_copy(map2->p[i]));
2678 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
2680 map1 = isl_map_union_disjoint(map1, map2);
2684 F_CLR(map1, ISL_MAP_DISJOINT);
2688 struct isl_set *isl_set_union_disjoint(
2689 struct isl_set *set1, struct isl_set *set2)
2691 return (struct isl_set *)
2692 isl_map_union_disjoint(
2693 (struct isl_map *)set1, (struct isl_map *)set2);
2696 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
2698 return (struct isl_set *)
2699 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
2702 struct isl_map *isl_map_intersect_range(
2703 struct isl_map *map, struct isl_set *set)
2706 struct isl_map *result;
2712 if (F_ISSET(map, ISL_MAP_DISJOINT) &&
2713 F_ISSET(set, ISL_MAP_DISJOINT))
2714 FL_SET(flags, ISL_MAP_DISJOINT);
2716 result = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
2717 map->n * set->n, flags);
2720 for (i = 0; i < map->n; ++i)
2721 for (j = 0; j < set->n; ++j) {
2722 result = isl_map_add(result,
2723 isl_basic_map_intersect_range(
2724 isl_basic_map_copy(map->p[i]),
2725 isl_basic_set_copy(set->p[j])));
2738 struct isl_map *isl_map_intersect_domain(
2739 struct isl_map *map, struct isl_set *set)
2741 return isl_map_reverse(
2742 isl_map_intersect_range(isl_map_reverse(map), set));
2745 struct isl_map *isl_map_apply_domain(
2746 struct isl_map *map1, struct isl_map *map2)
2750 map1 = isl_map_reverse(map1);
2751 map1 = isl_map_apply_range(map1, map2);
2752 return isl_map_reverse(map1);
2759 struct isl_map *isl_map_apply_range(
2760 struct isl_map *map1, struct isl_map *map2)
2762 struct isl_map *result;
2768 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
2769 isl_assert(map1->ctx, map1->n_out == map2->n_in, goto error);
2771 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map2->n_out,
2772 map1->n * map2->n, 0);
2775 for (i = 0; i < map1->n; ++i)
2776 for (j = 0; j < map2->n; ++j) {
2777 result = isl_map_add(result,
2778 isl_basic_map_apply_range(
2779 isl_basic_map_copy(map1->p[i]),
2780 isl_basic_map_copy(map2->p[j])));
2786 if (result && result->n <= 1)
2787 F_SET(result, ISL_MAP_DISJOINT);
2796 * returns range - domain
2798 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
2800 struct isl_basic_set *bset;
2806 isl_assert(bmap->ctx, bmap->n_in == bmap->n_out, goto error);
2808 bset = isl_basic_set_from_basic_map(bmap);
2809 bset = isl_basic_set_extend(bset, bset->nparam, 3*dim, 0,
2811 bset = isl_basic_set_swap_vars(bset, 2*dim);
2812 for (i = 0; i < dim; ++i) {
2813 int j = isl_basic_map_alloc_equality(
2814 (struct isl_basic_map *)bset);
2817 isl_seq_clr(bset->eq[j],
2818 1 + bset->nparam + bset->dim + bset->extra);
2819 isl_int_set_si(bset->eq[j][1+bset->nparam+i], 1);
2820 isl_int_set_si(bset->eq[j][1+bset->nparam+dim+i], 1);
2821 isl_int_set_si(bset->eq[j][1+bset->nparam+2*dim+i], -1);
2823 return isl_basic_set_project_out(bset, 2*dim, 0);
2825 isl_basic_map_free(bmap);
2829 /* If the only constraints a div d=floor(f/m)
2830 * appears in are its two defining constraints
2833 * -(f - (m - 1)) + m d >= 0
2835 * then it can safely be removed.
2837 static int div_is_redundant(struct isl_basic_map *bmap, int div)
2840 unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
2842 for (i = 0; i < bmap->n_eq; ++i)
2843 if (!isl_int_is_zero(bmap->eq[i][pos]))
2846 for (i = 0; i < bmap->n_ineq; ++i) {
2847 if (isl_int_is_zero(bmap->ineq[i][pos]))
2849 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
2851 isl_int_sub(bmap->div[div][1],
2852 bmap->div[div][1], bmap->div[div][0]);
2853 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
2854 neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
2855 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
2856 isl_int_add(bmap->div[div][1],
2857 bmap->div[div][1], bmap->div[div][0]);
2860 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
2861 bmap->n_div-div-1) != -1)
2863 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
2864 if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
2866 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
2867 bmap->n_div-div-1) != -1)
2873 for (i = 0; i < bmap->n_div; ++i)
2874 if (!isl_int_is_zero(bmap->div[i][1+pos]))
2881 * Remove divs that don't occur in any of the constraints or other divs.
2882 * These can arise when dropping some of the variables in a quast
2883 * returned by piplib.
2885 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
2892 for (i = bmap->n_div-1; i >= 0; --i) {
2893 if (!div_is_redundant(bmap, i))
2895 bmap = isl_basic_map_drop_div(bmap, i);
2900 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
2902 bmap = remove_redundant_divs(bmap);
2905 F_SET(bmap, ISL_BASIC_SET_FINAL);
2909 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
2911 return (struct isl_basic_set *)
2912 isl_basic_map_finalize((struct isl_basic_map *)bset);
2915 struct isl_set *isl_set_finalize(struct isl_set *set)
2921 for (i = 0; i < set->n; ++i) {
2922 set->p[i] = isl_basic_set_finalize(set->p[i]);
2932 struct isl_map *isl_map_finalize(struct isl_map *map)
2938 for (i = 0; i < map->n; ++i) {
2939 map->p[i] = isl_basic_map_finalize(map->p[i]);
2949 struct isl_basic_map *isl_basic_map_identity(struct isl_ctx *ctx,
2950 unsigned nparam, unsigned dim)
2952 struct isl_basic_map *bmap;
2955 bmap = isl_basic_map_alloc(ctx, nparam, dim, dim, 0, dim, 0);
2959 for (i = 0; i < dim; ++i) {
2960 int j = isl_basic_map_alloc_equality(bmap);
2963 isl_seq_clr(bmap->eq[j],
2964 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2965 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
2966 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
2968 return isl_basic_map_finalize(bmap);
2970 isl_basic_map_free(bmap);
2974 struct isl_map *isl_map_identity(struct isl_ctx *ctx,
2975 unsigned nparam, unsigned dim)
2977 struct isl_map *map = isl_map_alloc(ctx, nparam, dim, dim, 1,
2981 map = isl_map_add(map,
2982 isl_basic_map_identity(ctx, nparam, dim));
2989 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
2991 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
2994 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
2996 return isl_map_is_subset(
2997 (struct isl_map *)set1, (struct isl_map *)set2);
3000 int isl_basic_map_is_subset(
3001 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3004 struct isl_map *map1;
3005 struct isl_map *map2;
3007 if (!bmap1 || !bmap2)
3010 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3011 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3013 is_subset = isl_map_is_subset(map1, map2);
3021 int isl_basic_map_is_equal(
3022 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3026 if (!bmap1 || !bmap2)
3028 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3031 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3035 int isl_basic_set_is_equal(
3036 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3038 return isl_basic_map_is_equal(
3039 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3042 int isl_map_is_empty(struct isl_map *map)
3049 for (i = 0; i < map->n; ++i) {
3050 is_empty = isl_basic_map_is_empty(map->p[i]);
3059 int isl_set_is_empty(struct isl_set *set)
3061 return isl_map_is_empty((struct isl_map *)set);
3064 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3068 struct isl_map *diff;
3073 if (isl_map_is_empty(map1))
3076 if (isl_map_is_empty(map2))
3079 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3083 is_subset = isl_map_is_empty(diff);
3089 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3095 is_subset = isl_map_is_subset(map1, map2);
3098 is_subset = isl_map_is_subset(map2, map1);
3102 int isl_basic_map_is_strict_subset(
3103 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3107 if (!bmap1 || !bmap2)
3109 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3112 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3113 if (is_subset == -1)
3118 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3124 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3125 if (total != vec->size)
3130 for (i = 0; i < bmap->n_eq; ++i) {
3131 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3132 if (!isl_int_is_zero(s)) {
3138 for (i = 0; i < bmap->n_ineq; ++i) {
3139 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3140 if (isl_int_is_neg(s)) {
3151 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3155 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3158 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3160 struct isl_basic_set *bset = NULL;
3161 struct isl_vec *sample = NULL;
3168 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3171 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3172 if (bmap->sample && bmap->sample->size == total) {
3173 int contains = basic_map_contains(bmap, bmap->sample);
3179 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3182 sample = isl_basic_set_sample(bset);
3185 empty = sample->size == 0;
3187 isl_vec_free(bmap->ctx, bmap->sample);
3188 bmap->sample = sample;
3193 struct isl_map *isl_basic_map_union(
3194 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3196 struct isl_map *map;
3197 if (!bmap1 || !bmap2)
3200 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
3201 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
3202 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
3204 map = isl_map_alloc(bmap1->ctx, bmap1->nparam,
3205 bmap1->n_in, bmap1->n_out, 2, 0);
3208 map = isl_map_add(map, bmap1);
3209 map = isl_map_add(map, bmap2);
3212 isl_basic_map_free(bmap1);
3213 isl_basic_map_free(bmap2);
3217 struct isl_set *isl_basic_set_union(
3218 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3220 return (struct isl_set *)isl_basic_map_union(
3221 (struct isl_basic_map *)bset1,
3222 (struct isl_basic_map *)bset2);
3225 /* Order divs such that any div only depends on previous divs */
3226 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3229 unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
3231 for (i = 0; i < bmap->n_div; ++i) {
3233 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3237 swap_div(bmap, i, pos);
3243 static int find_div(struct isl_basic_map *dst,
3244 struct isl_basic_map *src, unsigned div)
3248 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3250 for (i = 0; i < dst->n_div; ++i)
3251 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total) &&
3252 isl_seq_first_non_zero(dst->div[i]+1+1+total,
3253 dst->n_div - src->n_div) == -1)
3258 struct isl_basic_map *isl_basic_map_align_divs(
3259 struct isl_basic_map *dst, struct isl_basic_map *src)
3262 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3264 if (src->n_div == 0)
3267 src = order_divs(src);
3268 dst = isl_basic_map_extend(dst, dst->nparam, dst->n_in,
3269 dst->n_out, src->n_div, 0, 2 * src->n_div);
3272 for (i = 0; i < src->n_div; ++i) {
3273 int j = find_div(dst, src, i);
3275 j = isl_basic_map_alloc_div(dst);
3278 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
3279 isl_seq_clr(dst->div[j]+1+1+total,
3280 dst->extra - src->n_div);
3281 if (add_div_constraints(dst, j) < 0)
3285 swap_div(dst, i, j);
3289 isl_basic_map_free(dst);
3293 struct isl_map *isl_map_align_divs(struct isl_map *map)
3297 map = isl_map_compute_divs(map);
3298 map = isl_map_cow(map);
3302 for (i = 1; i < map->n; ++i)
3303 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3304 for (i = 1; i < map->n; ++i)
3305 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3310 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3311 struct isl_basic_map *src, isl_int *c,
3312 unsigned len, int oppose)
3314 struct isl_basic_map *copy = NULL;
3319 copy = isl_basic_map_copy(src);
3320 copy = isl_basic_map_cow(copy);
3323 copy = isl_basic_map_extend(copy,
3324 copy->nparam, copy->n_in, copy->n_out, 0, 0, 1);
3325 k = isl_basic_map_alloc_inequality(copy);
3329 isl_seq_neg(copy->ineq[k], c, len);
3331 isl_seq_cpy(copy->ineq[k], c, len);
3332 total = 1 + copy->nparam + copy->n_in + copy->n_out + copy->extra;
3333 isl_seq_clr(copy->ineq[k]+len, total - len);
3334 isl_inequality_negate(copy, k);
3335 copy = isl_basic_map_simplify(copy);
3336 copy = isl_basic_map_finalize(copy);
3337 is_empty = isl_basic_map_is_empty(copy);
3341 dst = isl_map_add(dst, copy);
3343 isl_basic_map_free(copy);
3346 isl_basic_map_free(copy);
3351 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3355 struct isl_map *rest = NULL;
3357 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3364 if (F_ISSET(map, ISL_MAP_DISJOINT))
3365 FL_SET(flags, ISL_MAP_DISJOINT);
3367 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3368 rest = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
3373 for (i = 0; i < map->n; ++i) {
3374 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3379 for (j = 0; j < map->n; ++j)
3380 map->p[j] = isl_basic_map_cow(map->p[j]);
3382 for (i = 0; i < bmap->n_eq; ++i) {
3383 for (j = 0; j < map->n; ++j) {
3384 rest = add_cut_constraint(rest,
3385 map->p[j], bmap->eq[i], 1+total, 0);
3389 rest = add_cut_constraint(rest,
3390 map->p[j], bmap->eq[i], 1+total, 1);
3394 map->p[j] = isl_basic_map_extend(map->p[j],
3395 map->p[j]->nparam, map->p[j]->n_in,
3396 map->p[j]->n_out, 0, 1, 0);
3399 k = isl_basic_map_alloc_equality(map->p[j]);
3402 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3403 isl_seq_clr(map->p[j]->eq[k]+1+total,
3404 map->p[j]->extra - bmap->n_div);
3408 for (i = 0; i < bmap->n_ineq; ++i) {
3409 for (j = 0; j < map->n; ++j) {
3410 rest = add_cut_constraint(rest,
3411 map->p[j], bmap->ineq[i], 1+total, 0);
3415 map->p[j] = isl_basic_map_extend(map->p[j],
3416 map->p[j]->nparam, map->p[j]->n_in,
3417 map->p[j]->n_out, 0, 0, 1);
3420 k = isl_basic_map_alloc_inequality(map->p[j]);
3423 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3424 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3425 map->p[j]->extra - bmap->n_div);
3437 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3443 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3444 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
3445 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
3447 if (isl_map_is_empty(map2)) {
3452 map1 = isl_map_compute_divs(map1);
3453 map2 = isl_map_compute_divs(map2);
3457 for (i = 0; map1 && i < map2->n; ++i)
3458 map1 = subtract(map1, map2->p[i]);
3468 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3470 return (struct isl_set *)
3472 (struct isl_map *)set1, (struct isl_map *)set2);
3475 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3479 isl_assert(set->ctx, set->dim == map->n_in, goto error);
3480 map = isl_map_intersect_domain(map, set);
3481 set = isl_map_range(map);
3489 /* There is no need to cow as removing empty parts doesn't change
3490 * the meaning of the set.
3492 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3499 for (i = map->n-1; i >= 0; --i) {
3500 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3502 isl_basic_map_free(map->p[i]);
3504 map->p[i] = map->p[map->n-1];
3511 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3513 return (struct isl_set *)
3514 isl_map_remove_empty_parts((struct isl_map *)set);