5 #include "isl_equalities.h"
10 #include "isl_map_private.h"
11 #include "isl_map_piplib.h"
12 #include "isl_sample.h"
15 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
16 struct isl_basic_map *bmap,
17 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
18 unsigned n_eq, unsigned n_ineq)
21 size_t row_size = 1 + nparam + n_in + n_out + extra;
23 bmap->block = isl_blk_alloc(ctx, (n_eq + n_ineq) * row_size);
24 if (isl_blk_is_error(bmap->block)) {
29 bmap->eq = isl_alloc_array(ctx, isl_int *, n_eq + n_ineq);
31 isl_blk_free(ctx, bmap->block);
37 bmap->block2 = isl_blk_empty();
40 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
41 if (isl_blk_is_error(bmap->block2)) {
43 isl_blk_free(ctx, bmap->block);
48 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
50 isl_blk_free(ctx, bmap->block2);
52 isl_blk_free(ctx, bmap->block);
58 for (i = 0; i < n_eq + n_ineq; ++i)
59 bmap->eq[i] = bmap->block.data + i * row_size;
61 for (i = 0; i < extra; ++i)
62 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
68 bmap->c_size = n_eq + n_ineq;
69 bmap->ineq = bmap->eq + n_eq;
70 bmap->nparam = nparam;
82 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
83 unsigned nparam, unsigned dim, unsigned extra,
84 unsigned n_eq, unsigned n_ineq)
86 struct isl_basic_map *bmap;
87 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
88 return (struct isl_basic_set *)bmap;
91 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
92 unsigned nparam, unsigned in, unsigned out, unsigned extra,
93 unsigned n_eq, unsigned n_ineq)
95 struct isl_basic_map *bmap;
97 bmap = isl_alloc_type(ctx, struct isl_basic_map);
101 return basic_map_init(ctx, bmap,
102 nparam, in, out, extra, n_eq, n_ineq);
105 static void dup_constraints(
106 struct isl_basic_map *dst, struct isl_basic_map *src)
109 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
111 for (i = 0; i < src->n_eq; ++i) {
112 int j = isl_basic_map_alloc_equality(dst);
113 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
116 for (i = 0; i < src->n_ineq; ++i) {
117 int j = isl_basic_map_alloc_inequality(dst);
118 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
121 for (i = 0; i < src->n_div; ++i) {
122 int j = isl_basic_map_alloc_div(dst);
123 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
125 F_SET(dst, ISL_BASIC_SET_FINAL);
128 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
130 struct isl_basic_map *dup;
134 dup = isl_basic_map_alloc(bmap->ctx, bmap->nparam,
135 bmap->n_in, bmap->n_out,
136 bmap->n_div, bmap->n_eq, bmap->n_ineq);
139 dup->flags = bmap->flags;
140 dup_constraints(dup, bmap);
141 dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
145 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
147 struct isl_basic_map *dup;
149 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
150 return (struct isl_basic_set *)dup;
153 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
158 if (F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
162 return isl_basic_set_dup(bset);
165 struct isl_set *isl_set_copy(struct isl_set *set)
174 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
179 if (F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
183 return isl_basic_map_dup(bmap);
186 struct isl_map *isl_map_copy(struct isl_map *map)
195 void isl_basic_map_free(struct isl_basic_map *bmap)
203 isl_ctx_deref(bmap->ctx);
205 isl_blk_free(bmap->ctx, bmap->block2);
207 isl_blk_free(bmap->ctx, bmap->block);
208 isl_vec_free(bmap->ctx, bmap->sample);
212 void isl_basic_set_free(struct isl_basic_set *bset)
214 isl_basic_map_free((struct isl_basic_map *)bset);
217 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
223 isl_assert(ctx, bmap->n_eq + bmap->n_ineq < bmap->c_size, return -1);
224 isl_assert(ctx, bmap->eq + bmap->n_eq <= bmap->ineq, return -1);
225 if (bmap->eq + bmap->n_eq == bmap->ineq) {
227 int j = isl_basic_map_alloc_inequality(bmap);
231 bmap->ineq[0] = bmap->ineq[j];
239 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
241 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
244 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
248 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
253 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
258 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
260 if (pos != bmap->n_eq - 1) {
262 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
263 bmap->eq[bmap->n_eq - 1] = t;
269 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
271 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
274 void isl_basic_map_inequality_to_equality(
275 struct isl_basic_map *bmap, unsigned pos)
280 bmap->ineq[pos] = bmap->ineq[0];
281 bmap->ineq[0] = bmap->eq[bmap->n_eq];
282 bmap->eq[bmap->n_eq] = t;
286 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
289 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
295 isl_assert(ctx, (bmap->ineq - bmap->eq) + bmap->n_ineq < bmap->c_size,
297 F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
298 F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
299 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
300 return bmap->n_ineq++;
303 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
305 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
308 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
312 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
317 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
319 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
322 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
327 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
329 if (pos != bmap->n_ineq - 1) {
331 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
332 bmap->ineq[bmap->n_ineq - 1] = t;
333 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
339 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
341 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
344 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
348 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
349 return bmap->n_div++;
352 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
356 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
361 /* Copy constraint from src to dst, putting the vars of src at offset
362 * dim_off in dst and the divs of src at offset div_off in dst.
363 * If both sets are actually map, then dim_off applies to the input
366 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
367 struct isl_basic_map *src_map, isl_int *src,
368 unsigned in_off, unsigned out_off, unsigned div_off)
370 isl_int_set(dst[0], src[0]);
371 isl_seq_cpy(dst+1, src+1, isl_min(dst_map->nparam, src_map->nparam));
372 if (dst_map->nparam > src_map->nparam)
373 isl_seq_clr(dst+1+src_map->nparam,
374 dst_map->nparam - src_map->nparam);
375 isl_seq_clr(dst+1+dst_map->nparam, in_off);
376 isl_seq_cpy(dst+1+dst_map->nparam+in_off,
377 src+1+src_map->nparam,
378 isl_min(dst_map->n_in-in_off, src_map->n_in));
379 if (dst_map->n_in-in_off > src_map->n_in)
380 isl_seq_clr(dst+1+dst_map->nparam+in_off+src_map->n_in,
381 dst_map->n_in - in_off - src_map->n_in);
382 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in, out_off);
383 isl_seq_cpy(dst+1+dst_map->nparam+dst_map->n_in+out_off,
384 src+1+src_map->nparam+src_map->n_in,
385 isl_min(dst_map->n_out-out_off, src_map->n_out));
386 if (dst_map->n_out-out_off > src_map->n_out)
387 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+out_off+src_map->n_out,
388 dst_map->n_out - out_off - src_map->n_out);
389 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out, div_off);
390 isl_seq_cpy(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out+div_off,
391 src+1+src_map->nparam+src_map->n_in+src_map->n_out,
392 isl_min(dst_map->extra-div_off, src_map->extra));
393 if (dst_map->extra-div_off > src_map->extra)
394 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out+
395 div_off+src_map->extra,
396 dst_map->extra - div_off - src_map->extra);
399 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
400 struct isl_basic_map *src_map, isl_int *src,
401 unsigned in_off, unsigned out_off, unsigned div_off)
403 isl_int_set(dst[0], src[0]);
404 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
407 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
408 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
413 if (!bmap1 || !bmap2)
416 div_off = bmap1->n_div;
418 for (i = 0; i < bmap2->n_eq; ++i) {
419 int i1 = isl_basic_map_alloc_equality(bmap1);
422 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
423 i_pos, o_pos, div_off);
426 for (i = 0; i < bmap2->n_ineq; ++i) {
427 int i1 = isl_basic_map_alloc_inequality(bmap1);
430 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
431 i_pos, o_pos, div_off);
434 for (i = 0; i < bmap2->n_div; ++i) {
435 int i1 = isl_basic_map_alloc_div(bmap1);
438 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
439 i_pos, o_pos, div_off);
442 isl_basic_map_free(bmap2);
447 isl_basic_map_free(bmap1);
448 isl_basic_map_free(bmap2);
452 static struct isl_basic_set *set_add_constraints(struct isl_basic_set *bset1,
453 struct isl_basic_set *bset2, unsigned pos)
455 return (struct isl_basic_set *)
456 add_constraints((struct isl_basic_map *)bset1,
457 (struct isl_basic_map *)bset2, 0, pos);
460 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
461 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
462 unsigned n_eq, unsigned n_ineq)
464 struct isl_basic_map *ext;
468 base = isl_basic_map_cow(base);
472 dims_ok = base && base->nparam == nparam &&
473 base->n_in == n_in && base->n_out == n_out &&
474 base->extra >= base->n_div + extra;
476 if (dims_ok && n_eq == 0 && n_ineq == 0)
479 isl_assert(base->ctx, base->nparam <= nparam, goto error);
480 isl_assert(base->ctx, base->n_in <= n_in, goto error);
481 isl_assert(base->ctx, base->n_out <= n_out, goto error);
482 extra += base->extra;
484 n_ineq += base->n_ineq;
486 ext = isl_basic_map_alloc(base->ctx, nparam, n_in, n_out,
487 extra, n_eq, n_ineq);
494 ext = add_constraints(ext, base, 0, 0);
497 F_CLR(ext, ISL_BASIC_SET_FINAL);
503 isl_basic_map_free(base);
507 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
508 unsigned nparam, unsigned dim, unsigned extra,
509 unsigned n_eq, unsigned n_ineq)
511 return (struct isl_basic_set *)
512 isl_basic_map_extend((struct isl_basic_map *)base,
513 nparam, 0, dim, extra, n_eq, n_ineq);
516 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
518 return (struct isl_basic_set *)
519 isl_basic_map_cow((struct isl_basic_map *)bset);
522 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
529 bmap = isl_basic_map_dup(bmap);
531 F_CLR(bmap, ISL_BASIC_SET_FINAL);
535 struct isl_set *isl_set_cow(struct isl_set *set)
543 return isl_set_dup(set);
546 struct isl_map *isl_map_cow(struct isl_map *map)
554 return isl_map_dup(map);
557 static void swap_vars(struct isl_blk blk, isl_int *a,
558 unsigned a_len, unsigned b_len)
560 isl_seq_cpy(blk.data, a+a_len, b_len);
561 isl_seq_cpy(blk.data+b_len, a, a_len);
562 isl_seq_cpy(a, blk.data, b_len+a_len);
565 struct isl_basic_set *isl_basic_set_swap_vars(
566 struct isl_basic_set *bset, unsigned n)
574 isl_assert(bset->ctx, n <= bset->dim, goto error);
579 bset = isl_basic_set_cow(bset);
583 blk = isl_blk_alloc(bset->ctx, bset->dim);
584 if (isl_blk_is_error(blk))
587 for (i = 0; i < bset->n_eq; ++i)
589 bset->eq[i]+1+bset->nparam, n, bset->dim - n);
591 for (i = 0; i < bset->n_ineq; ++i)
593 bset->ineq[i]+1+bset->nparam, n, bset->dim - n);
595 for (i = 0; i < bset->n_div; ++i)
597 bset->div[i]+1+1+bset->nparam, n, bset->dim - n);
599 isl_blk_free(bset->ctx, blk);
601 F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
605 isl_basic_set_free(bset);
609 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
612 set = isl_set_cow(set);
616 for (i = 0; i < set->n; ++i) {
617 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
623 F_CLR(set, ISL_SET_NORMALIZED);
627 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
629 isl_seq_cpy(c, c + n, rem);
630 isl_seq_clr(c + rem, n);
633 struct isl_basic_set *isl_basic_set_drop_dims(
634 struct isl_basic_set *bset, unsigned first, unsigned n)
641 isl_assert(bset->ctx, first + n <= bset->dim, goto error);
646 bset = isl_basic_set_cow(bset);
650 for (i = 0; i < bset->n_eq; ++i)
651 constraint_drop_vars(bset->eq[i]+1+bset->nparam+first, n,
652 (bset->dim-first-n)+bset->extra);
654 for (i = 0; i < bset->n_ineq; ++i)
655 constraint_drop_vars(bset->ineq[i]+1+bset->nparam+first, n,
656 (bset->dim-first-n)+bset->extra);
658 for (i = 0; i < bset->n_div; ++i)
659 constraint_drop_vars(bset->div[i]+1+1+bset->nparam+first, n,
660 (bset->dim-first-n)+bset->extra);
665 F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
666 bset = isl_basic_set_simplify(bset);
667 return isl_basic_set_finalize(bset);
669 isl_basic_set_free(bset);
673 static struct isl_set *isl_set_drop_dims(
674 struct isl_set *set, unsigned first, unsigned n)
681 isl_assert(set->ctx, first + n <= set->dim, goto error);
685 set = isl_set_cow(set);
689 for (i = 0; i < set->n; ++i) {
690 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
696 F_CLR(set, ISL_SET_NORMALIZED);
703 struct isl_basic_map *isl_basic_map_drop_inputs(
704 struct isl_basic_map *bmap, unsigned first, unsigned n)
711 isl_assert(bmap->ctx, first + n <= bmap->n_in, goto error);
716 bmap = isl_basic_map_cow(bmap);
720 for (i = 0; i < bmap->n_eq; ++i)
721 constraint_drop_vars(bmap->eq[i]+1+bmap->nparam+first, n,
722 (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
724 for (i = 0; i < bmap->n_ineq; ++i)
725 constraint_drop_vars(bmap->ineq[i]+1+bmap->nparam+first, n,
726 (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
728 for (i = 0; i < bmap->n_div; ++i)
729 constraint_drop_vars(bmap->div[i]+1+1+bmap->nparam+first, n,
730 (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
735 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
736 bmap = isl_basic_map_simplify(bmap);
737 return isl_basic_map_finalize(bmap);
739 isl_basic_map_free(bmap);
743 static struct isl_map *isl_map_drop_inputs(
744 struct isl_map *map, unsigned first, unsigned n)
751 isl_assert(map->ctx, first + n <= map->n_in, goto error);
755 map = isl_map_cow(map);
759 for (i = 0; i < map->n; ++i) {
760 map->p[i] = isl_basic_map_drop_inputs(map->p[i], first, n);
765 F_CLR(map, ISL_MAP_NORMALIZED);
774 * We don't cow, as the div is assumed to be redundant.
776 static struct isl_basic_map *isl_basic_map_drop_div(
777 struct isl_basic_map *bmap, unsigned div)
785 pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
787 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
789 for (i = 0; i < bmap->n_eq; ++i)
790 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
792 for (i = 0; i < bmap->n_ineq; ++i) {
793 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
794 isl_basic_map_drop_inequality(bmap, i);
798 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
801 for (i = 0; i < bmap->n_div; ++i)
802 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
804 if (div != bmap->n_div - 1) {
806 isl_int *t = bmap->div[div];
808 for (j = div; j < bmap->n_div - 1; ++j)
809 bmap->div[j] = bmap->div[j+1];
811 bmap->div[bmap->n_div - 1] = t;
813 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
814 isl_basic_map_free_div(bmap, 1);
818 isl_basic_map_free(bmap);
822 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
825 unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
827 len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
829 for (i = 0; i < bmap->n_eq; ++i)
830 if (bmap->eq[i] != eq)
831 isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
833 for (i = 0; i < bmap->n_ineq; ++i)
834 isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
836 /* We need to be careful about circular definitions,
837 * so for now we just remove the definitions of other divs that
838 * depend on this div and (possibly) recompute them later.
840 for (i = 0; i < bmap->n_div; ++i)
841 if (!isl_int_is_zero(bmap->div[i][0]) &&
842 !isl_int_is_zero(bmap->div[i][1 + pos]))
843 isl_seq_clr(bmap->div[i], 1 + len);
845 isl_basic_map_drop_div(bmap, div);
848 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
854 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
855 isl_basic_map_free_div(bmap, bmap->n_div);
856 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
858 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
860 isl_basic_map_alloc_equality(bmap);
864 isl_int_set_si(bmap->eq[i][0], 1);
865 isl_seq_clr(bmap->eq[i]+1, total);
866 F_SET(bmap, ISL_BASIC_MAP_EMPTY);
867 return isl_basic_map_finalize(bmap);
869 isl_basic_map_free(bmap);
873 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
875 return (struct isl_basic_set *)
876 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
879 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
881 isl_int *t = bmap->eq[a];
882 bmap->eq[a] = bmap->eq[b];
886 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
889 isl_int *t = bmap->ineq[a];
890 bmap->ineq[a] = bmap->ineq[b];
895 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
897 swap_inequality((struct isl_basic_map *)bset, a, b);
900 static void swap_div(struct isl_basic_map *bmap, int a, int b)
903 unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
904 isl_int *t = bmap->div[a];
905 bmap->div[a] = bmap->div[b];
908 for (i = 0; i < bmap->n_eq; ++i)
909 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
911 for (i = 0; i < bmap->n_ineq; ++i)
912 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
914 for (i = 0; i < bmap->n_div; ++i)
915 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
916 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
919 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
920 unsigned pos, isl_int *eq, int *progress)
925 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
926 for (k = 0; k < bmap->n_eq; ++k) {
927 if (bmap->eq[k] == eq)
929 if (isl_int_is_zero(bmap->eq[k][1+pos]))
933 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
936 for (k = 0; k < bmap->n_ineq; ++k) {
937 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
941 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
942 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
945 for (k = 0; k < bmap->n_div; ++k) {
946 if (isl_int_is_zero(bmap->div[k][0]))
948 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
952 isl_seq_elim(bmap->div[k]+1, eq,
953 1+pos, 1+total, &bmap->div[k][0]);
954 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
959 struct isl_basic_map *isl_basic_map_gauss(
960 struct isl_basic_map *bmap, int *progress)
971 total_var = bmap->nparam + bmap->n_in + bmap->n_out;
972 total = total_var + bmap->n_div;
974 last_var = total - 1;
975 for (done = 0; done < bmap->n_eq; ++done) {
976 for (; last_var >= 0; --last_var) {
977 for (k = done; k < bmap->n_eq; ++k)
978 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
986 swap_equality(bmap, k, done);
987 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
988 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
990 eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
993 if (last_var >= total_var &&
994 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
995 unsigned div = last_var - total_var;
996 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
997 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
998 isl_int_set(bmap->div[div][0],
999 bmap->eq[done][1+last_var]);
1000 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1003 if (done == bmap->n_eq)
1005 for (k = done; k < bmap->n_eq; ++k) {
1006 if (isl_int_is_zero(bmap->eq[k][0]))
1008 return isl_basic_map_set_to_empty(bmap);
1010 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
1014 struct isl_basic_set *isl_basic_set_gauss(
1015 struct isl_basic_set *bset, int *progress)
1017 return (struct isl_basic_set*)isl_basic_map_gauss(
1018 (struct isl_basic_map *)bset, progress);
1021 static unsigned int round_up(unsigned int v)
1032 static int hash_index(int *index, unsigned int size, int bits,
1033 struct isl_basic_map *bmap, int k)
1036 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1037 u_int32_t hash = isl_seq_hash(bmap->ineq[k]+1, total, bits);
1038 for (h = hash; index[h]; h = (h+1) % size)
1039 if (k != index[h]-1 &&
1040 isl_seq_eq(bmap->ineq[k]+1,
1041 bmap->ineq[index[h]-1]+1, total))
1046 static struct isl_basic_map *remove_duplicate_constraints(
1047 struct isl_basic_map *bmap, int *progress)
1053 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1056 if (bmap->n_ineq <= 1)
1059 size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
1060 bits = ffs(size) - 1;
1061 index = isl_alloc_array(ctx, int, size);
1062 memset(index, 0, size * sizeof(int));
1066 index[isl_seq_hash(bmap->ineq[0]+1, total, bits)] = 1;
1067 for (k = 1; k < bmap->n_ineq; ++k) {
1068 h = hash_index(index, size, bits, bmap, k);
1076 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1077 swap_inequality(bmap, k, l);
1078 isl_basic_map_drop_inequality(bmap, k);
1082 for (k = 0; k < bmap->n_ineq-1; ++k) {
1083 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1084 h = hash_index(index, size, bits, bmap, k);
1085 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1089 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1090 if (isl_int_is_pos(sum))
1092 if (isl_int_is_zero(sum)) {
1093 /* We need to break out of the loop after these
1094 * changes since the contents of the hash
1095 * will no longer be valid.
1096 * Plus, we probably we want to regauss first.
1098 isl_basic_map_drop_inequality(bmap, l);
1099 isl_basic_map_inequality_to_equality(bmap, k);
1101 bmap = isl_basic_map_set_to_empty(bmap);
1110 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1115 total = bmap->nparam + bmap->n_in + bmap->n_out;
1116 for (d = 0; d < total; ++d)
1118 for (d = total - 1, i = 0; d >= 0 && i < bmap->n_eq; ++i) {
1119 for (; d >= 0; --d) {
1120 if (isl_int_is_zero(bmap->eq[i][1+d]))
1128 static int reduced_using_equalities(isl_int *c, struct isl_vec *v,
1129 struct isl_basic_map *bmap, int *elim)
1135 total = bmap->nparam + bmap->n_in + bmap->n_out;
1136 for (d = total - 1; d >= 0; --d) {
1137 if (isl_int_is_zero(c[1+d]))
1142 isl_seq_cpy(v->block.data, c, 1 + total);
1145 isl_seq_elim(v->block.data, bmap->eq[elim[d]],
1146 1 + d, 1 + total, NULL);
1151 /* Quick check to see if two basic maps are disjoint.
1152 * In particular, we reduce the equalities and inequalities of
1153 * one basic map in the context of the equalities of the other
1154 * basic map and check if we get a contradiction.
1156 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1157 struct isl_basic_map *bmap2)
1159 struct isl_vec *v = NULL;
1164 if (!bmap1 || !bmap2)
1166 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, return -1);
1167 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, return -1);
1168 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, return -1);
1169 if (bmap1->n_div || bmap2->n_div)
1171 if (!bmap1->n_eq && !bmap2->n_eq)
1174 total = bmap1->nparam + bmap1->n_in + bmap1->n_out;
1177 v = isl_vec_alloc(bmap1->ctx, 1 + total);
1180 elim = isl_alloc_array(bmap1->ctx, int, total);
1183 compute_elimination_index(bmap1, elim);
1184 for (i = 0; i < bmap2->n_eq; ++i) {
1186 reduced = reduced_using_equalities(bmap2->eq[i], v, bmap1, elim);
1187 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1188 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1191 for (i = 0; i < bmap2->n_ineq; ++i) {
1193 reduced = reduced_using_equalities(bmap2->ineq[i], v, bmap1, elim);
1194 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1195 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1198 compute_elimination_index(bmap2, elim);
1199 for (i = 0; i < bmap1->n_ineq; ++i) {
1201 reduced = reduced_using_equalities(bmap1->ineq[i], v, bmap2, elim);
1202 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1203 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1206 isl_vec_free(bmap1->ctx, v);
1210 isl_vec_free(bmap1->ctx, v);
1214 isl_vec_free(bmap1->ctx, v);
1219 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1220 struct isl_basic_set *bset2)
1222 return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1223 (struct isl_basic_map *)bset2);
1226 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1233 if (isl_map_fast_is_equal(map1, map2))
1236 for (i = 0; i < map1->n; ++i) {
1237 for (j = 0; j < map2->n; ++j) {
1238 int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1247 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1249 return isl_map_fast_is_disjoint((struct isl_map *)set1,
1250 (struct isl_map *)set2);
1253 static struct isl_basic_map *remove_duplicate_divs(
1254 struct isl_basic_map *bmap, int *progress)
1261 unsigned total_var = bmap->nparam + bmap->n_in + bmap->n_out;
1262 unsigned total = total_var + bmap->n_div;
1263 struct isl_ctx *ctx;
1265 if (bmap->n_div <= 1)
1269 for (k = bmap->n_div - 1; k >= 0; --k)
1270 if (!isl_int_is_zero(bmap->div[k][0]))
1275 size = round_up(4 * bmap->n_div / 3 - 1);
1276 bits = ffs(size) - 1;
1277 index = isl_alloc_array(ctx, int, size);
1278 memset(index, 0, size * sizeof(int));
1281 eq = isl_blk_alloc(ctx, 1+total);
1282 if (isl_blk_is_error(eq))
1285 isl_seq_clr(eq.data, 1+total);
1286 index[isl_seq_hash(bmap->div[k], 2+total, bits)] = k + 1;
1287 for (--k; k >= 0; --k) {
1290 if (isl_int_is_zero(bmap->div[k][0]))
1293 hash = isl_seq_hash(bmap->div[k], 2+total, bits);
1294 for (h = hash; index[h]; h = (h+1) % size)
1295 if (isl_seq_eq(bmap->div[k],
1296 bmap->div[index[h]-1], 2+total))
1301 isl_int_set_si(eq.data[1+total_var+k], -1);
1302 isl_int_set_si(eq.data[1+total_var+l], 1);
1303 eliminate_div(bmap, eq.data, l);
1304 isl_int_set_si(eq.data[1+total_var+k], 0);
1305 isl_int_set_si(eq.data[1+total_var+l], 0);
1310 isl_blk_free(ctx, eq);
1316 /* Elimininate divs based on equalities
1318 static struct isl_basic_map *eliminate_divs_eq(
1319 struct isl_basic_map *bmap, int *progress)
1329 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1331 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1332 for (i = 0; i < bmap->n_eq; ++i) {
1333 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
1334 !isl_int_is_negone(bmap->eq[i][off + d]))
1338 eliminate_div(bmap, bmap->eq[i], d);
1339 isl_basic_map_drop_equality(bmap, i);
1344 return eliminate_divs_eq(bmap, progress);
1348 static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
1352 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1355 for (i = bmap->n_eq - 1; i >= 0; --i) {
1356 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
1357 if (isl_int_is_zero(gcd)) {
1358 if (!isl_int_is_zero(bmap->eq[i][0])) {
1359 bmap = isl_basic_map_set_to_empty(bmap);
1362 isl_basic_map_drop_equality(bmap, i);
1365 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1366 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
1367 if (isl_int_is_one(gcd))
1369 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
1370 bmap = isl_basic_map_set_to_empty(bmap);
1373 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
1376 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1377 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
1378 if (isl_int_is_zero(gcd)) {
1379 if (isl_int_is_neg(bmap->ineq[i][0])) {
1380 bmap = isl_basic_map_set_to_empty(bmap);
1383 isl_basic_map_drop_inequality(bmap, i);
1386 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1387 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
1388 if (isl_int_is_one(gcd))
1390 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
1391 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
1399 /* Eliminate the specified variables from the constraints using
1400 * Fourier-Motzkin. The variables themselves are not removed.
1402 struct isl_basic_map *isl_basic_map_eliminate_vars(
1403 struct isl_basic_map *bmap, int pos, unsigned n)
1412 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1413 extra = bmap->extra - bmap->n_div;
1415 bmap = isl_basic_map_cow(bmap);
1416 for (d = pos + n - 1; d >= pos; --d) {
1417 int n_lower, n_upper;
1420 for (i = 0; i < bmap->n_eq; ++i) {
1421 if (isl_int_is_zero(bmap->eq[i][1+d]))
1423 eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
1424 isl_basic_map_drop_equality(bmap, i);
1431 for (i = 0; i < bmap->n_ineq; ++i) {
1432 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1434 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1437 bmap = isl_basic_map_extend(bmap,
1438 bmap->nparam, bmap->n_in, bmap->n_out, 0,
1439 0, n_lower * n_upper);
1440 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1442 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1445 for (j = 0; j < i; ++j) {
1446 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1449 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1450 isl_int_sgn(bmap->ineq[j][1+d]))
1452 k = isl_basic_map_alloc_inequality(bmap);
1455 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1457 isl_seq_clr(bmap->ineq[k]+1+total, extra);
1458 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1459 1+d, 1+total, NULL);
1461 isl_basic_map_drop_inequality(bmap, i);
1464 if (n_lower > 0 && n_upper > 0) {
1465 bmap = normalize_constraints(bmap);
1466 bmap = remove_duplicate_constraints(bmap, NULL);
1467 bmap = isl_basic_map_gauss(bmap, NULL);
1468 bmap = isl_basic_map_convex_hull(bmap);
1471 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1475 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1478 isl_basic_map_free(bmap);
1482 struct isl_basic_set *isl_basic_set_eliminate_vars(
1483 struct isl_basic_set *bset, unsigned pos, unsigned n)
1485 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1486 (struct isl_basic_map *)bset, pos, n);
1489 /* Eliminate the specified n dimensions starting at first from the
1490 * constraints using Fourier-Motzkin, The dimensions themselves
1493 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1494 unsigned first, unsigned n)
1501 set = isl_set_cow(set);
1504 isl_assert(set->ctx, first+n <= set->dim, goto error);
1506 for (i = 0; i < set->n; ++i) {
1507 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1508 set->nparam + first, n);
1518 /* Project out n dimensions starting at first using Fourier-Motzkin */
1519 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1520 unsigned first, unsigned n)
1522 set = isl_set_eliminate_dims(set, first, n);
1523 set = isl_set_drop_dims(set, first, n);
1527 /* Project out n inputs starting at first using Fourier-Motzkin */
1528 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1529 unsigned first, unsigned n)
1536 map = isl_map_cow(map);
1539 isl_assert(map->ctx, first+n <= map->n_in, goto error);
1541 for (i = 0; i < map->n; ++i) {
1542 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1543 map->nparam + first, n);
1547 map = isl_map_drop_inputs(map, first, n);
1554 /* Project out n dimensions starting at first using Fourier-Motzkin */
1555 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1556 unsigned first, unsigned n)
1558 bset = isl_basic_set_eliminate_vars(bset, bset->nparam + first, n);
1559 bset = isl_basic_set_drop_dims(bset, first, n);
1563 /* Elimininate divs based on inequalities
1565 static struct isl_basic_map *eliminate_divs_ineq(
1566 struct isl_basic_map *bmap, int *progress)
1571 struct isl_ctx *ctx;
1577 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1579 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1580 for (i = 0; i < bmap->n_eq; ++i)
1581 if (!isl_int_is_zero(bmap->eq[i][off + d]))
1585 for (i = 0; i < bmap->n_ineq; ++i)
1586 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
1588 if (i < bmap->n_ineq)
1591 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
1592 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1594 bmap = isl_basic_map_drop_div(bmap, d);
1601 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1608 bmap = normalize_constraints(bmap);
1609 bmap = eliminate_divs_eq(bmap, &progress);
1610 bmap = eliminate_divs_ineq(bmap, &progress);
1611 bmap = isl_basic_map_gauss(bmap, &progress);
1612 bmap = remove_duplicate_divs(bmap, &progress);
1613 bmap = remove_duplicate_constraints(bmap, &progress);
1618 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1620 return (struct isl_basic_set *)
1621 isl_basic_map_simplify((struct isl_basic_map *)bset);
1624 static void dump_term(struct isl_basic_map *bmap,
1625 isl_int c, int pos, FILE *out)
1627 unsigned in = bmap->n_in;
1628 unsigned dim = bmap->n_in + bmap->n_out;
1630 isl_int_print(out, c, 0);
1632 if (!isl_int_is_one(c))
1633 isl_int_print(out, c, 0);
1634 if (pos < 1 + bmap->nparam)
1635 fprintf(out, "p%d", pos - 1);
1636 else if (pos < 1 + bmap->nparam + in)
1637 fprintf(out, "i%d", pos - 1 - bmap->nparam);
1638 else if (pos < 1 + bmap->nparam + dim)
1639 fprintf(out, "o%d", pos - 1 - bmap->nparam - in);
1641 fprintf(out, "e%d", pos - 1 - bmap->nparam - dim);
1645 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1646 int sign, FILE *out)
1650 unsigned len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1654 for (i = 0, first = 1; i < len; ++i) {
1655 if (isl_int_sgn(c[i]) * sign <= 0)
1658 fprintf(out, " + ");
1660 isl_int_abs(v, c[i]);
1661 dump_term(bmap, v, i, out);
1668 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1669 const char *op, FILE *out, int indent)
1673 fprintf(out, "%*s", indent, "");
1675 dump_constraint_sign(bmap, c, 1, out);
1676 fprintf(out, " %s ", op);
1677 dump_constraint_sign(bmap, c, -1, out);
1681 for (i = bmap->n_div; i < bmap->extra; ++i) {
1682 if (isl_int_is_zero(c[1+bmap->nparam+bmap->n_in+bmap->n_out+i]))
1684 fprintf(out, "%*s", indent, "");
1685 fprintf(out, "ERROR: unused div coefficient not zero\n");
1689 static void dump_constraints(struct isl_basic_map *bmap,
1690 isl_int **c, unsigned n,
1691 const char *op, FILE *out, int indent)
1695 for (i = 0; i < n; ++i)
1696 dump_constraint(bmap, c[i], op, out, indent);
1699 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1703 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1705 for (j = 0; j < 1 + total; ++j) {
1706 if (isl_int_is_zero(exp[j]))
1708 if (!first && isl_int_is_pos(exp[j]))
1710 dump_term(bmap, exp[j], j, out);
1715 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1719 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1720 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1722 for (i = 0; i < bmap->n_div; ++i) {
1723 fprintf(out, "%*s", indent, "");
1724 fprintf(out, "e%d = [(", i);
1725 dump_affine(bmap, bmap->div[i]+1, out);
1727 isl_int_print(out, bmap->div[i][0], 0);
1728 fprintf(out, "]\n");
1732 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1735 fprintf(out, "null basic set\n");
1739 fprintf(out, "%*s", indent, "");
1740 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1741 bset->ref, bset->nparam, bset->dim, bset->extra,
1743 dump((struct isl_basic_map *)bset, out, indent);
1746 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1749 fprintf(out, "null basic map\n");
1753 fprintf(out, "%*s", indent, "");
1754 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, flags: %x\n",
1756 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, bmap->flags);
1757 dump(bmap, out, indent);
1760 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1762 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1765 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1766 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1767 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1768 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1772 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1773 unsigned nparam, unsigned dim, int n, unsigned flags)
1775 struct isl_set *set;
1777 isl_assert(ctx, n >= 0, return NULL);
1778 set = isl_alloc(ctx, struct isl_set,
1779 sizeof(struct isl_set) +
1780 n * sizeof(struct isl_basic_set *));
1789 set->nparam = nparam;
1796 struct isl_set *isl_set_dup(struct isl_set *set)
1799 struct isl_set *dup;
1804 dup = isl_set_alloc(set->ctx, set->nparam, set->dim, set->n, set->flags);
1807 for (i = 0; i < set->n; ++i)
1808 dup = isl_set_add(dup,
1809 isl_basic_set_dup(set->p[i]));
1813 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1815 struct isl_set *set;
1820 set = isl_set_alloc(bset->ctx, bset->nparam, bset->dim, 1, ISL_MAP_DISJOINT);
1822 isl_basic_set_free(bset);
1825 return isl_set_add(set, bset);
1828 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1830 struct isl_map *map;
1835 map = isl_map_alloc(bmap->ctx, bmap->nparam, bmap->n_in, bmap->n_out, 1,
1838 isl_basic_map_free(bmap);
1841 return isl_map_add(map, bmap);
1844 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1848 isl_assert(set->ctx, set->nparam == bset->nparam, goto error);
1849 isl_assert(set->ctx, set->dim == bset->dim, goto error);
1850 isl_assert(set->ctx, set->n < set->size, goto error);
1851 set->p[set->n] = bset;
1858 isl_basic_set_free(bset);
1862 void isl_set_free(struct isl_set *set)
1872 isl_ctx_deref(set->ctx);
1873 for (i = 0; i < set->n; ++i)
1874 isl_basic_set_free(set->p[i]);
1878 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1883 fprintf(out, "null set\n");
1887 fprintf(out, "%*s", indent, "");
1888 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1889 set->ref, set->n, set->nparam, set->dim, set->flags);
1890 for (i = 0; i < set->n; ++i) {
1891 fprintf(out, "%*s", indent, "");
1892 fprintf(out, "basic set %d:\n", i);
1893 isl_basic_set_dump(set->p[i], out, indent+4);
1897 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1902 fprintf(out, "null map\n");
1906 fprintf(out, "%*s", indent, "");
1907 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, flags: %x\n",
1908 map->ref, map->n, map->nparam, map->n_in, map->n_out,
1910 for (i = 0; i < map->n; ++i) {
1911 fprintf(out, "%*s", indent, "");
1912 fprintf(out, "basic map %d:\n", i);
1913 isl_basic_map_dump(map->p[i], out, indent+4);
1917 struct isl_basic_map *isl_basic_map_intersect_domain(
1918 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1920 struct isl_basic_map *bmap_domain;
1925 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1926 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1928 bmap = isl_basic_map_extend(bmap,
1929 bset->nparam, bmap->n_in, bmap->n_out,
1930 bset->n_div, bset->n_eq, bset->n_ineq);
1933 bmap_domain = isl_basic_map_from_basic_set(bset,
1935 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1937 bmap = isl_basic_map_simplify(bmap);
1938 return isl_basic_map_finalize(bmap);
1940 isl_basic_map_free(bmap);
1941 isl_basic_set_free(bset);
1945 struct isl_basic_map *isl_basic_map_intersect_range(
1946 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1948 struct isl_basic_map *bmap_range;
1953 isl_assert(bset->ctx, bset->dim == bmap->n_out, goto error);
1954 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1956 bmap = isl_basic_map_extend(bmap,
1957 bset->nparam, bmap->n_in, bmap->n_out,
1958 bset->n_div, bset->n_eq, bset->n_ineq);
1961 bmap_range = isl_basic_map_from_basic_set(bset,
1963 bmap = add_constraints(bmap, bmap_range, 0, 0);
1965 bmap = isl_basic_map_simplify(bmap);
1966 return isl_basic_map_finalize(bmap);
1968 isl_basic_map_free(bmap);
1969 isl_basic_set_free(bset);
1973 struct isl_basic_map *isl_basic_map_intersect(
1974 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1976 if (!bmap1 || !bmap2)
1979 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
1980 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
1981 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
1983 bmap1 = isl_basic_map_extend(bmap1,
1984 bmap1->nparam, bmap1->n_in, bmap1->n_out,
1985 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1988 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1990 bmap1 = isl_basic_map_simplify(bmap1);
1991 return isl_basic_map_finalize(bmap1);
1993 isl_basic_map_free(bmap1);
1994 isl_basic_map_free(bmap2);
1998 struct isl_basic_set *isl_basic_set_intersect(
1999 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2001 return (struct isl_basic_set *)
2002 isl_basic_map_intersect(
2003 (struct isl_basic_map *)bset1,
2004 (struct isl_basic_map *)bset2);
2007 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2010 struct isl_map *result;
2016 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2017 F_ISSET(map2, ISL_MAP_DISJOINT))
2018 FL_SET(flags, ISL_MAP_DISJOINT);
2020 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
2021 map1->n * map2->n, flags);
2024 for (i = 0; i < map1->n; ++i)
2025 for (j = 0; j < map2->n; ++j) {
2026 struct isl_basic_map *part;
2027 part = isl_basic_map_intersect(
2028 isl_basic_map_copy(map1->p[i]),
2029 isl_basic_map_copy(map2->p[j]));
2030 if (isl_basic_map_is_empty(part))
2031 isl_basic_map_free(part);
2033 result = isl_map_add(result, part);
2046 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2048 return (struct isl_set *)
2049 isl_map_intersect((struct isl_map *)set1,
2050 (struct isl_map *)set2);
2053 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2055 struct isl_basic_set *bset;
2060 bmap = isl_basic_map_cow(bmap);
2064 bset = isl_basic_set_from_basic_map(bmap);
2065 bset = isl_basic_set_swap_vars(bset, in);
2066 return isl_basic_map_from_basic_set(bset, bset->dim-in, in);
2069 /* Turn final n dimensions into existentially quantified variables.
2071 struct isl_basic_set *isl_basic_set_project_out(
2072 struct isl_basic_set *bset, unsigned n, unsigned flags)
2082 isl_assert(bset->ctx, n <= bset->dim, goto error);
2087 bset = isl_basic_set_cow(bset);
2089 row_size = 1 + bset->nparam + bset->dim + bset->extra;
2090 old = bset->block2.data;
2091 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
2092 (bset->extra + n) * (1 + row_size));
2093 if (!bset->block2.data)
2095 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
2098 for (i = 0; i < n; ++i) {
2099 new_div[i] = bset->block2.data +
2100 (bset->extra + i) * (1 + row_size);
2101 isl_seq_clr(new_div[i], 1 + row_size);
2103 for (i = 0; i < bset->extra; ++i)
2104 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
2106 bset->div = new_div;
2110 bset = isl_basic_set_simplify(bset);
2111 return isl_basic_set_finalize(bset);
2113 isl_basic_set_free(bset);
2117 struct isl_basic_map *isl_basic_map_apply_range(
2118 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2120 struct isl_basic_set *bset;
2121 unsigned n_in, n_out;
2123 if (!bmap1 || !bmap2)
2126 isl_assert(bmap->ctx, bmap1->n_out == bmap2->n_in, goto error);
2127 isl_assert(bmap->ctx, bmap1->nparam == bmap2->nparam, goto error);
2130 n_out = bmap2->n_out;
2132 bmap2 = isl_basic_map_reverse(bmap2);
2135 bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
2136 bmap1->n_in + bmap2->n_in, bmap1->n_out,
2137 bmap2->extra, bmap2->n_eq, bmap2->n_ineq);
2140 bmap1 = add_constraints(bmap1, bmap2, bmap1->n_in - bmap2->n_in, 0);
2141 bmap1 = isl_basic_map_simplify(bmap1);
2142 bset = isl_basic_set_from_basic_map(bmap1);
2143 bset = isl_basic_set_project_out(bset,
2144 bset->dim - (n_in + n_out), 0);
2145 return isl_basic_map_from_basic_set(bset, n_in, n_out);
2147 isl_basic_map_free(bmap1);
2148 isl_basic_map_free(bmap2);
2152 struct isl_basic_set *isl_basic_set_apply(
2153 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2158 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
2160 return (struct isl_basic_set *)
2161 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2163 isl_basic_set_free(bset);
2164 isl_basic_map_free(bmap);
2168 struct isl_basic_map *isl_basic_map_apply_domain(
2169 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2171 if (!bmap1 || !bmap2)
2174 isl_assert(ctx, bmap1->n_in == bmap2->n_in, goto error);
2175 isl_assert(ctx, bmap1->nparam == bmap2->nparam, goto error);
2177 bmap1 = isl_basic_map_reverse(bmap1);
2178 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2179 return isl_basic_map_reverse(bmap1);
2181 isl_basic_map_free(bmap1);
2182 isl_basic_map_free(bmap2);
2186 static struct isl_basic_map *var_equal(struct isl_ctx *ctx,
2187 struct isl_basic_map *bmap, unsigned pos)
2190 i = isl_basic_map_alloc_equality(bmap);
2193 isl_seq_clr(bmap->eq[i],
2194 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2195 isl_int_set_si(bmap->eq[i][1+bmap->nparam+pos], -1);
2196 isl_int_set_si(bmap->eq[i][1+bmap->nparam+bmap->n_in+pos], 1);
2197 return isl_basic_map_finalize(bmap);
2199 isl_basic_map_free(bmap);
2203 static struct isl_basic_map *var_more(struct isl_ctx *ctx,
2204 struct isl_basic_map *bmap, unsigned pos)
2207 i = isl_basic_map_alloc_inequality(bmap);
2210 isl_seq_clr(bmap->ineq[i],
2211 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2212 isl_int_set_si(bmap->ineq[i][0], -1);
2213 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], -1);
2214 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], 1);
2215 return isl_basic_map_finalize(bmap);
2217 isl_basic_map_free(bmap);
2221 static struct isl_basic_map *var_less(struct isl_ctx *ctx,
2222 struct isl_basic_map *bmap, unsigned pos)
2225 i = isl_basic_map_alloc_inequality(bmap);
2228 isl_seq_clr(bmap->ineq[i],
2229 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2230 isl_int_set_si(bmap->ineq[i][0], -1);
2231 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], 1);
2232 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], -1);
2233 return isl_basic_map_finalize(bmap);
2235 isl_basic_map_free(bmap);
2239 struct isl_basic_map *isl_basic_map_equal(struct isl_ctx *ctx,
2240 unsigned nparam, unsigned in, unsigned out, unsigned n_equal)
2243 struct isl_basic_map *bmap;
2244 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, n_equal, 0);
2247 for (i = 0; i < n_equal && bmap; ++i)
2248 bmap = var_equal(ctx, bmap, i);
2249 return isl_basic_map_finalize(bmap);
2252 struct isl_basic_map *isl_basic_map_less_at(struct isl_ctx *ctx,
2253 unsigned nparam, unsigned in, unsigned out, unsigned pos)
2256 struct isl_basic_map *bmap;
2257 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
2260 for (i = 0; i < pos && bmap; ++i)
2261 bmap = var_equal(ctx, bmap, i);
2263 bmap = var_less(ctx, bmap, pos);
2264 return isl_basic_map_finalize(bmap);
2267 struct isl_basic_map *isl_basic_map_more_at(struct isl_ctx *ctx,
2268 unsigned nparam, unsigned in, unsigned out, unsigned pos)
2271 struct isl_basic_map *bmap;
2272 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
2275 for (i = 0; i < pos && bmap; ++i)
2276 bmap = var_equal(ctx, bmap, i);
2278 bmap = var_more(ctx, bmap, pos);
2279 return isl_basic_map_finalize(bmap);
2282 struct isl_basic_map *isl_basic_map_from_basic_set(
2283 struct isl_basic_set *bset, unsigned n_in, unsigned n_out)
2285 struct isl_basic_map *bmap;
2287 bset = isl_basic_set_cow(bset);
2291 isl_assert(ctx, bset->dim == n_in + n_out, goto error);
2292 bmap = (struct isl_basic_map *) bset;
2294 bmap->n_out = n_out;
2295 return isl_basic_map_finalize(bmap);
2297 isl_basic_set_free(bset);
2301 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2305 if (bmap->n_in == 0)
2306 return (struct isl_basic_set *)bmap;
2307 bmap = isl_basic_map_cow(bmap);
2310 bmap->n_out += bmap->n_in;
2312 bmap = isl_basic_map_finalize(bmap);
2313 return (struct isl_basic_set *)bmap;
2318 /* For a div d = floor(f/m), add the constraints
2321 * -(f-(n-1)) + m d >= 0
2323 * Note that the second constraint is the negation of
2327 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2330 unsigned div_pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
2331 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
2333 i = isl_basic_map_alloc_inequality(bmap);
2336 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2337 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2339 j = isl_basic_map_alloc_inequality(bmap);
2342 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2343 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2344 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2348 struct isl_basic_set *isl_basic_map_underlying_set(
2349 struct isl_basic_map *bmap)
2353 if (bmap->nparam == 0 && bmap->n_in == 0 && bmap->n_div == 0)
2354 return (struct isl_basic_set *)bmap;
2355 bmap = isl_basic_map_cow(bmap);
2358 bmap->n_out += bmap->nparam + bmap->n_in + bmap->n_div;
2361 bmap->extra -= bmap->n_div;
2363 bmap = isl_basic_map_finalize(bmap);
2364 return (struct isl_basic_set *)bmap;
2369 struct isl_basic_map *isl_basic_map_overlying_set(
2370 struct isl_basic_set *bset, struct isl_basic_map *like)
2372 struct isl_basic_map *bmap;
2373 struct isl_ctx *ctx;
2380 isl_assert(ctx, bset->dim ==
2381 like->nparam + like->n_in + like->n_out + like->n_div,
2383 if (like->nparam == 0 && like->n_in == 0 && like->n_div == 0) {
2384 isl_basic_map_free(like);
2385 return (struct isl_basic_map *)bset;
2387 bset = isl_basic_set_cow(bset);
2390 total = bset->dim + bset->extra;
2391 bmap = (struct isl_basic_map *)bset;
2392 bmap->nparam = like->nparam;
2393 bmap->n_in = like->n_in;
2394 bmap->n_out = like->n_out;
2395 bmap->extra += like->n_div;
2398 ltotal = total - bmap->extra + like->extra;
2401 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2402 bmap->extra * (1 + 1 + total));
2403 if (isl_blk_is_error(bmap->block2))
2405 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2409 bmap = isl_basic_map_extend(bmap, bmap->nparam,
2410 bmap->n_in, bmap->n_out, 0, 0, 2 * like->n_div);
2411 for (i = 0; i < like->n_div; ++i) {
2412 k = isl_basic_map_alloc_div(bmap);
2415 isl_seq_cpy(bmap->div[k], like->div[i], 1 + 1 + ltotal);
2416 isl_seq_clr(bmap->div[k]+1+1+ltotal, total - ltotal);
2417 if (add_div_constraints(bmap, k) < 0)
2421 isl_basic_map_free(like);
2422 bmap = isl_basic_map_finalize(bmap);
2425 isl_basic_map_free(like);
2426 isl_basic_set_free(bset);
2430 struct isl_basic_set *isl_basic_set_from_underlying_set(
2431 struct isl_basic_set *bset, struct isl_basic_set *like)
2433 return (struct isl_basic_set *)
2434 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2437 struct isl_set *isl_set_from_underlying_set(
2438 struct isl_set *set, struct isl_basic_set *like)
2444 isl_assert(set->ctx, set->dim == like->nparam + like->dim + like->n_div,
2446 if (like->nparam == 0 && like->n_div == 0) {
2447 isl_basic_set_free(like);
2450 set = isl_set_cow(set);
2453 for (i = 0; i < set->n; ++i) {
2454 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2455 isl_basic_set_copy(like));
2459 set->nparam = like->nparam;
2460 set->dim = like->dim;
2461 isl_basic_set_free(like);
2464 isl_basic_set_free(like);
2469 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2473 map = isl_map_align_divs(map);
2474 map = isl_map_cow(map);
2478 for (i = 0; i < map->n; ++i) {
2479 map->p[i] = (struct isl_basic_map *)
2480 isl_basic_map_underlying_set(map->p[i]);
2485 map->n_out += map->nparam + map->n_in;
2487 map->n_out = map->p[0]->n_out;
2490 return (struct isl_set *)map;
2496 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2498 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2501 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2503 struct isl_basic_set *domain;
2507 n_out = bmap->n_out;
2508 domain = isl_basic_set_from_basic_map(bmap);
2509 return isl_basic_set_project_out(domain, n_out, 0);
2512 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2514 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2517 struct isl_set *isl_map_range(struct isl_map *map)
2520 struct isl_set *set;
2524 map = isl_map_cow(map);
2528 set = (struct isl_set *) map;
2530 for (i = 0; i < map->n; ++i) {
2531 set->p[i] = isl_basic_map_range(map->p[i]);
2535 F_CLR(set, ISL_MAP_DISJOINT);
2536 F_CLR(set, ISL_SET_NORMALIZED);
2543 struct isl_map *isl_map_from_set(struct isl_set *set,
2544 unsigned n_in, unsigned n_out)
2547 struct isl_map *map = NULL;
2551 isl_assert(set->ctx, set->dim == n_in + n_out, goto error);
2552 set = isl_set_cow(set);
2555 map = (struct isl_map *)set;
2556 for (i = 0; i < set->n; ++i) {
2557 map->p[i] = isl_basic_map_from_basic_set(
2558 set->p[i], n_in, n_out);
2570 struct isl_set *isl_set_from_map(struct isl_map *map)
2573 struct isl_set *set = NULL;
2577 map = isl_map_cow(map);
2580 map->n_out += map->n_in;
2582 set = (struct isl_set *)map;
2583 for (i = 0; i < map->n; ++i) {
2584 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2594 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2595 unsigned nparam, unsigned in, unsigned out, int n,
2598 struct isl_map *map;
2600 map = isl_alloc(ctx, struct isl_map,
2601 sizeof(struct isl_map) +
2602 n * sizeof(struct isl_basic_map *));
2611 map->nparam = nparam;
2618 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2619 unsigned nparam, unsigned in, unsigned out)
2621 struct isl_basic_map *bmap;
2622 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2623 bmap = isl_basic_map_set_to_empty(bmap);
2627 struct isl_basic_set *isl_basic_set_empty(struct isl_ctx *ctx,
2628 unsigned nparam, unsigned dim)
2630 struct isl_basic_set *bset;
2631 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 1, 0);
2632 bset = isl_basic_set_set_to_empty(bset);
2636 struct isl_basic_map *isl_basic_map_universe(struct isl_ctx *ctx,
2637 unsigned nparam, unsigned in, unsigned out)
2639 struct isl_basic_map *bmap;
2640 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 0, 0);
2644 struct isl_basic_set *isl_basic_set_universe(struct isl_ctx *ctx,
2645 unsigned nparam, unsigned dim)
2647 struct isl_basic_set *bset;
2648 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 0, 0);
2652 struct isl_map *isl_map_empty(struct isl_ctx *ctx,
2653 unsigned nparam, unsigned in, unsigned out)
2655 return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
2658 struct isl_set *isl_set_empty(struct isl_ctx *ctx,
2659 unsigned nparam, unsigned dim)
2661 return isl_set_alloc(ctx, nparam, dim, 0, ISL_MAP_DISJOINT);
2664 struct isl_map *isl_map_dup(struct isl_map *map)
2667 struct isl_map *dup;
2671 dup = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out, map->n,
2673 for (i = 0; i < map->n; ++i)
2674 dup = isl_map_add(dup,
2675 isl_basic_map_dup(map->p[i]));
2679 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2683 isl_assert(map->ctx, map->nparam == bmap->nparam, goto error);
2684 isl_assert(map->ctx, map->n_in == bmap->n_in, goto error);
2685 isl_assert(map->ctx, map->n_out == bmap->n_out, goto error);
2686 isl_assert(map->ctx, map->n < map->size, goto error);
2687 map->p[map->n] = bmap;
2689 F_CLR(map, ISL_MAP_NORMALIZED);
2695 isl_basic_map_free(bmap);
2699 void isl_map_free(struct isl_map *map)
2709 isl_ctx_deref(map->ctx);
2710 for (i = 0; i < map->n; ++i)
2711 isl_basic_map_free(map->p[i]);
2715 struct isl_map *isl_map_extend(struct isl_map *base,
2716 unsigned nparam, unsigned n_in, unsigned n_out)
2720 base = isl_map_cow(base);
2724 isl_assert(base->ctx, base->nparam <= nparam, goto error);
2725 isl_assert(base->ctx, base->n_in <= n_in, goto error);
2726 isl_assert(base->ctx, base->n_out <= n_out, goto error);
2727 base->nparam = nparam;
2729 base->n_out = n_out;
2730 for (i = 0; i < base->n; ++i) {
2731 base->p[i] = isl_basic_map_extend(base->p[i],
2732 nparam, n_in, n_out, 0, 0, 0);
2742 struct isl_set *isl_set_extend(struct isl_set *base,
2743 unsigned nparam, unsigned dim)
2745 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2749 static struct isl_basic_map *isl_basic_map_fix_var(struct isl_basic_map *bmap,
2750 unsigned var, int value)
2754 bmap = isl_basic_map_cow(bmap);
2755 bmap = isl_basic_map_extend(bmap,
2756 bmap->nparam, bmap->n_in, bmap->n_out, 0, 1, 0);
2757 j = isl_basic_map_alloc_equality(bmap);
2760 isl_seq_clr(bmap->eq[j],
2761 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2762 isl_int_set_si(bmap->eq[j][1 + var], -1);
2763 isl_int_set_si(bmap->eq[j][0], value);
2764 bmap = isl_basic_map_simplify(bmap);
2765 return isl_basic_map_finalize(bmap);
2767 isl_basic_map_free(bmap);
2771 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2772 unsigned input, int value)
2776 isl_assert(bmap->ctx, input < bmap->n_in, goto error);
2777 return isl_basic_map_fix_var(bmap, bmap->nparam + input, value);
2779 isl_basic_map_free(bmap);
2783 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2784 unsigned dim, int value)
2788 isl_assert(bset->ctx, dim < bset->dim, goto error);
2789 return (struct isl_basic_set *)
2790 isl_basic_map_fix_var((struct isl_basic_map *)bset,
2791 bset->nparam + dim, value);
2793 isl_basic_set_free(bset);
2797 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2798 unsigned input, int value)
2802 map = isl_map_cow(map);
2806 isl_assert(ctx, input < map->n_in, goto error);
2807 for (i = 0; i < map->n; ++i) {
2808 map->p[i] = isl_basic_map_fix_input_si(map->p[i], input, value);
2812 F_CLR(map, ISL_MAP_NORMALIZED);
2819 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2823 set = isl_set_cow(set);
2827 isl_assert(set->ctx, dim < set->dim, goto error);
2828 for (i = 0; i < set->n; ++i) {
2829 set->p[i] = isl_basic_set_fix_dim_si(set->p[i], dim, value);
2839 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2840 unsigned dim, isl_int value)
2844 bset = isl_basic_set_cow(bset);
2845 bset = isl_basic_set_extend(bset, bset->nparam, bset->dim, 0, 0, 1);
2846 j = isl_basic_set_alloc_inequality(bset);
2849 isl_seq_clr(bset->ineq[j], 1 + bset->nparam + bset->dim + bset->extra);
2850 isl_int_set_si(bset->ineq[j][1 + bset->nparam + dim], 1);
2851 isl_int_neg(bset->ineq[j][0], value);
2852 bset = isl_basic_set_simplify(bset);
2853 return isl_basic_set_finalize(bset);
2855 isl_basic_set_free(bset);
2859 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2864 set = isl_set_cow(set);
2868 isl_assert(set->ctx, dim < set->dim, goto error);
2869 for (i = 0; i < set->n; ++i) {
2870 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2880 struct isl_map *isl_map_reverse(struct isl_map *map)
2885 map = isl_map_cow(map);
2890 map->n_in = map->n_out;
2892 for (i = 0; i < map->n; ++i) {
2893 map->p[i] = isl_basic_map_reverse(map->p[i]);
2897 F_CLR(map, ISL_MAP_NORMALIZED);
2904 struct isl_map *isl_basic_map_lexmax(
2905 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2906 struct isl_set **empty)
2908 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2911 struct isl_map *isl_basic_map_lexmin(
2912 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2913 struct isl_set **empty)
2915 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2918 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2920 struct isl_basic_map *bmap = NULL;
2921 struct isl_basic_set *dom = NULL;
2922 struct isl_map *min;
2926 bmap = isl_basic_map_from_basic_set(bset, 0, bset->dim);
2929 dom = isl_basic_set_alloc(bmap->ctx, bmap->nparam, 0, 0, 0, 0);
2932 min = isl_basic_map_lexmin(bmap, dom, NULL);
2933 return isl_map_range(min);
2935 isl_basic_map_free(bmap);
2939 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2943 if (bmap->n_div == 0)
2944 return isl_map_from_basic_map(bmap);
2945 return isl_pip_basic_map_compute_divs(bmap);
2948 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2951 struct isl_map *res;
2957 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2958 for (i = 1 ; i < map->n; ++i) {
2960 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2961 if (F_ISSET(map, ISL_MAP_DISJOINT))
2962 res = isl_map_union_disjoint(res, r2);
2964 res = isl_map_union(res, r2);
2971 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2973 return (struct isl_set *)
2974 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2977 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2979 return (struct isl_set *)
2980 isl_map_compute_divs((struct isl_map *)set);
2983 struct isl_set *isl_map_domain(struct isl_map *map)
2986 struct isl_set *set;
2991 map = isl_map_cow(map);
2995 set = (struct isl_set *)map;
2996 set->dim = map->n_in;
2998 for (i = 0; i < map->n; ++i) {
2999 set->p[i] = isl_basic_map_domain(map->p[i]);
3003 F_CLR(set, ISL_MAP_DISJOINT);
3004 F_CLR(set, ISL_SET_NORMALIZED);
3011 struct isl_map *isl_map_union_disjoint(
3012 struct isl_map *map1, struct isl_map *map2)
3016 struct isl_map *map = NULL;
3030 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3031 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
3032 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
3034 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
3035 F_ISSET(map2, ISL_MAP_DISJOINT))
3036 FL_SET(flags, ISL_MAP_DISJOINT);
3038 map = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
3039 map1->n + map2->n, flags);
3042 for (i = 0; i < map1->n; ++i) {
3043 map = isl_map_add(map,
3044 isl_basic_map_copy(map1->p[i]));
3048 for (i = 0; i < map2->n; ++i) {
3049 map = isl_map_add(map,
3050 isl_basic_map_copy(map2->p[i]));
3064 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3066 map1 = isl_map_union_disjoint(map1, map2);
3070 F_CLR(map1, ISL_MAP_DISJOINT);
3074 struct isl_set *isl_set_union_disjoint(
3075 struct isl_set *set1, struct isl_set *set2)
3077 return (struct isl_set *)
3078 isl_map_union_disjoint(
3079 (struct isl_map *)set1, (struct isl_map *)set2);
3082 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3084 return (struct isl_set *)
3085 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3088 struct isl_map *isl_map_intersect_range(
3089 struct isl_map *map, struct isl_set *set)
3092 struct isl_map *result;
3098 if (F_ISSET(map, ISL_MAP_DISJOINT) &&
3099 F_ISSET(set, ISL_MAP_DISJOINT))
3100 FL_SET(flags, ISL_MAP_DISJOINT);
3102 result = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
3103 map->n * set->n, flags);
3106 for (i = 0; i < map->n; ++i)
3107 for (j = 0; j < set->n; ++j) {
3108 result = isl_map_add(result,
3109 isl_basic_map_intersect_range(
3110 isl_basic_map_copy(map->p[i]),
3111 isl_basic_set_copy(set->p[j])));
3124 struct isl_map *isl_map_intersect_domain(
3125 struct isl_map *map, struct isl_set *set)
3127 return isl_map_reverse(
3128 isl_map_intersect_range(isl_map_reverse(map), set));
3131 struct isl_map *isl_map_apply_domain(
3132 struct isl_map *map1, struct isl_map *map2)
3136 map1 = isl_map_reverse(map1);
3137 map1 = isl_map_apply_range(map1, map2);
3138 return isl_map_reverse(map1);
3145 struct isl_map *isl_map_apply_range(
3146 struct isl_map *map1, struct isl_map *map2)
3148 struct isl_map *result;
3154 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3155 isl_assert(map1->ctx, map1->n_out == map2->n_in, goto error);
3157 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map2->n_out,
3158 map1->n * map2->n, 0);
3161 for (i = 0; i < map1->n; ++i)
3162 for (j = 0; j < map2->n; ++j) {
3163 result = isl_map_add(result,
3164 isl_basic_map_apply_range(
3165 isl_basic_map_copy(map1->p[i]),
3166 isl_basic_map_copy(map2->p[j])));
3172 if (result && result->n <= 1)
3173 F_SET(result, ISL_MAP_DISJOINT);
3182 * returns range - domain
3184 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3186 struct isl_basic_set *bset;
3192 isl_assert(bmap->ctx, bmap->n_in == bmap->n_out, goto error);
3194 bset = isl_basic_set_from_basic_map(bmap);
3195 bset = isl_basic_set_extend(bset, bset->nparam, 3*dim, 0,
3197 bset = isl_basic_set_swap_vars(bset, 2*dim);
3198 for (i = 0; i < dim; ++i) {
3199 int j = isl_basic_map_alloc_equality(
3200 (struct isl_basic_map *)bset);
3203 isl_seq_clr(bset->eq[j],
3204 1 + bset->nparam + bset->dim + bset->extra);
3205 isl_int_set_si(bset->eq[j][1+bset->nparam+i], 1);
3206 isl_int_set_si(bset->eq[j][1+bset->nparam+dim+i], 1);
3207 isl_int_set_si(bset->eq[j][1+bset->nparam+2*dim+i], -1);
3209 return isl_basic_set_project_out(bset, 2*dim, 0);
3211 isl_basic_map_free(bmap);
3216 * returns range - domain
3218 struct isl_set *isl_map_deltas(struct isl_map *map)
3221 struct isl_set *result;
3226 isl_assert(map->ctx, map->n_in == map->n_out, goto error);
3227 result = isl_set_alloc(map->ctx, map->nparam, map->n_in, map->n, map->flags);
3230 for (i = 0; i < map->n; ++i)
3231 result = isl_set_add(result,
3232 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3240 /* If the only constraints a div d=floor(f/m)
3241 * appears in are its two defining constraints
3244 * -(f - (m - 1)) + m d >= 0
3246 * then it can safely be removed.
3248 static int div_is_redundant(struct isl_basic_map *bmap, int div)
3251 unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
3253 for (i = 0; i < bmap->n_eq; ++i)
3254 if (!isl_int_is_zero(bmap->eq[i][pos]))
3257 for (i = 0; i < bmap->n_ineq; ++i) {
3258 if (isl_int_is_zero(bmap->ineq[i][pos]))
3260 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
3262 isl_int_sub(bmap->div[div][1],
3263 bmap->div[div][1], bmap->div[div][0]);
3264 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
3265 neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
3266 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
3267 isl_int_add(bmap->div[div][1],
3268 bmap->div[div][1], bmap->div[div][0]);
3271 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
3272 bmap->n_div-div-1) != -1)
3274 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
3275 if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
3277 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
3278 bmap->n_div-div-1) != -1)
3284 for (i = 0; i < bmap->n_div; ++i)
3285 if (!isl_int_is_zero(bmap->div[i][1+pos]))
3292 * Remove divs that don't occur in any of the constraints or other divs.
3293 * These can arise when dropping some of the variables in a quast
3294 * returned by piplib.
3296 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
3303 for (i = bmap->n_div-1; i >= 0; --i) {
3304 if (!div_is_redundant(bmap, i))
3306 bmap = isl_basic_map_drop_div(bmap, i);
3311 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
3313 bmap = remove_redundant_divs(bmap);
3316 F_SET(bmap, ISL_BASIC_SET_FINAL);
3320 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
3322 return (struct isl_basic_set *)
3323 isl_basic_map_finalize((struct isl_basic_map *)bset);
3326 struct isl_set *isl_set_finalize(struct isl_set *set)
3332 for (i = 0; i < set->n; ++i) {
3333 set->p[i] = isl_basic_set_finalize(set->p[i]);
3343 struct isl_map *isl_map_finalize(struct isl_map *map)
3349 for (i = 0; i < map->n; ++i) {
3350 map->p[i] = isl_basic_map_finalize(map->p[i]);
3354 F_CLR(map, ISL_MAP_NORMALIZED);
3361 struct isl_basic_map *isl_basic_map_identity(struct isl_ctx *ctx,
3362 unsigned nparam, unsigned dim)
3364 struct isl_basic_map *bmap;
3367 bmap = isl_basic_map_alloc(ctx, nparam, dim, dim, 0, dim, 0);
3371 for (i = 0; i < dim; ++i) {
3372 int j = isl_basic_map_alloc_equality(bmap);
3375 isl_seq_clr(bmap->eq[j],
3376 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
3377 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3378 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3380 return isl_basic_map_finalize(bmap);
3382 isl_basic_map_free(bmap);
3386 struct isl_map *isl_map_identity(struct isl_ctx *ctx,
3387 unsigned nparam, unsigned dim)
3389 struct isl_map *map = isl_map_alloc(ctx, nparam, dim, dim, 1,
3393 map = isl_map_add(map,
3394 isl_basic_map_identity(ctx, nparam, dim));
3401 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3403 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3406 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3408 return isl_map_is_subset(
3409 (struct isl_map *)set1, (struct isl_map *)set2);
3412 int isl_basic_map_is_subset(
3413 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3416 struct isl_map *map1;
3417 struct isl_map *map2;
3419 if (!bmap1 || !bmap2)
3422 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3423 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3425 is_subset = isl_map_is_subset(map1, map2);
3433 int isl_basic_map_is_equal(
3434 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3438 if (!bmap1 || !bmap2)
3440 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3443 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3447 int isl_basic_set_is_equal(
3448 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3450 return isl_basic_map_is_equal(
3451 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3454 int isl_map_is_empty(struct isl_map *map)
3461 for (i = 0; i < map->n; ++i) {
3462 is_empty = isl_basic_map_is_empty(map->p[i]);
3471 int isl_set_is_empty(struct isl_set *set)
3473 return isl_map_is_empty((struct isl_map *)set);
3476 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3480 struct isl_map *diff;
3485 if (isl_map_is_empty(map1))
3488 if (isl_map_is_empty(map2))
3491 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3495 is_subset = isl_map_is_empty(diff);
3501 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3507 is_subset = isl_map_is_subset(map1, map2);
3510 is_subset = isl_map_is_subset(map2, map1);
3514 int isl_basic_map_is_strict_subset(
3515 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3519 if (!bmap1 || !bmap2)
3521 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3524 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3525 if (is_subset == -1)
3530 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3536 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3537 if (total != vec->size)
3542 for (i = 0; i < bmap->n_eq; ++i) {
3543 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3544 if (!isl_int_is_zero(s)) {
3550 for (i = 0; i < bmap->n_ineq; ++i) {
3551 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3552 if (isl_int_is_neg(s)) {
3563 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3567 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3570 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3572 struct isl_basic_set *bset = NULL;
3573 struct isl_vec *sample = NULL;
3580 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3583 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3584 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3585 copy = isl_basic_map_convex_hull(copy);
3586 empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3587 isl_basic_map_free(copy);
3591 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3592 if (bmap->sample && bmap->sample->size == total) {
3593 int contains = basic_map_contains(bmap, bmap->sample);
3599 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3602 sample = isl_basic_set_sample(bset);
3605 empty = sample->size == 0;
3607 isl_vec_free(bmap->ctx, bmap->sample);
3608 bmap->sample = sample;
3613 struct isl_map *isl_basic_map_union(
3614 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3616 struct isl_map *map;
3617 if (!bmap1 || !bmap2)
3620 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
3621 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
3622 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
3624 map = isl_map_alloc(bmap1->ctx, bmap1->nparam,
3625 bmap1->n_in, bmap1->n_out, 2, 0);
3628 map = isl_map_add(map, bmap1);
3629 map = isl_map_add(map, bmap2);
3632 isl_basic_map_free(bmap1);
3633 isl_basic_map_free(bmap2);
3637 struct isl_set *isl_basic_set_union(
3638 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3640 return (struct isl_set *)isl_basic_map_union(
3641 (struct isl_basic_map *)bset1,
3642 (struct isl_basic_map *)bset2);
3645 /* Order divs such that any div only depends on previous divs */
3646 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3649 unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
3651 for (i = 0; i < bmap->n_div; ++i) {
3653 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3657 swap_div(bmap, i, pos);
3663 static int find_div(struct isl_basic_map *dst,
3664 struct isl_basic_map *src, unsigned div)
3668 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3670 for (i = 0; i < dst->n_div; ++i)
3671 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total) &&
3672 isl_seq_first_non_zero(dst->div[i]+1+1+total,
3673 dst->n_div - src->n_div) == -1)
3678 struct isl_basic_map *isl_basic_map_align_divs(
3679 struct isl_basic_map *dst, struct isl_basic_map *src)
3682 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3684 if (src->n_div == 0)
3687 src = order_divs(src);
3688 dst = isl_basic_map_extend(dst, dst->nparam, dst->n_in,
3689 dst->n_out, src->n_div, 0, 2 * src->n_div);
3692 for (i = 0; i < src->n_div; ++i) {
3693 int j = find_div(dst, src, i);
3695 j = isl_basic_map_alloc_div(dst);
3698 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
3699 isl_seq_clr(dst->div[j]+1+1+total,
3700 dst->extra - src->n_div);
3701 if (add_div_constraints(dst, j) < 0)
3705 swap_div(dst, i, j);
3709 isl_basic_map_free(dst);
3713 struct isl_map *isl_map_align_divs(struct isl_map *map)
3717 map = isl_map_compute_divs(map);
3718 map = isl_map_cow(map);
3722 for (i = 1; i < map->n; ++i)
3723 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3724 for (i = 1; i < map->n; ++i)
3725 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3727 F_CLR(map, ISL_MAP_NORMALIZED);
3731 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3732 struct isl_basic_map *src, isl_int *c,
3733 unsigned len, int oppose)
3735 struct isl_basic_map *copy = NULL;
3740 copy = isl_basic_map_copy(src);
3741 copy = isl_basic_map_cow(copy);
3744 copy = isl_basic_map_extend(copy,
3745 copy->nparam, copy->n_in, copy->n_out, 0, 0, 1);
3746 k = isl_basic_map_alloc_inequality(copy);
3750 isl_seq_neg(copy->ineq[k], c, len);
3752 isl_seq_cpy(copy->ineq[k], c, len);
3753 total = 1 + copy->nparam + copy->n_in + copy->n_out + copy->extra;
3754 isl_seq_clr(copy->ineq[k]+len, total - len);
3755 isl_inequality_negate(copy, k);
3756 copy = isl_basic_map_simplify(copy);
3757 copy = isl_basic_map_finalize(copy);
3758 is_empty = isl_basic_map_is_empty(copy);
3762 dst = isl_map_add(dst, copy);
3764 isl_basic_map_free(copy);
3767 isl_basic_map_free(copy);
3772 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3776 struct isl_map *rest = NULL;
3778 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3785 if (F_ISSET(map, ISL_MAP_DISJOINT))
3786 FL_SET(flags, ISL_MAP_DISJOINT);
3788 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3789 rest = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
3794 for (i = 0; i < map->n; ++i) {
3795 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3800 for (j = 0; j < map->n; ++j)
3801 map->p[j] = isl_basic_map_cow(map->p[j]);
3803 for (i = 0; i < bmap->n_eq; ++i) {
3804 for (j = 0; j < map->n; ++j) {
3805 rest = add_cut_constraint(rest,
3806 map->p[j], bmap->eq[i], 1+total, 0);
3810 rest = add_cut_constraint(rest,
3811 map->p[j], bmap->eq[i], 1+total, 1);
3815 map->p[j] = isl_basic_map_extend(map->p[j],
3816 map->p[j]->nparam, map->p[j]->n_in,
3817 map->p[j]->n_out, 0, 1, 0);
3820 k = isl_basic_map_alloc_equality(map->p[j]);
3823 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3824 isl_seq_clr(map->p[j]->eq[k]+1+total,
3825 map->p[j]->extra - bmap->n_div);
3829 for (i = 0; i < bmap->n_ineq; ++i) {
3830 for (j = 0; j < map->n; ++j) {
3831 rest = add_cut_constraint(rest,
3832 map->p[j], bmap->ineq[i], 1+total, 0);
3836 map->p[j] = isl_basic_map_extend(map->p[j],
3837 map->p[j]->nparam, map->p[j]->n_in,
3838 map->p[j]->n_out, 0, 0, 1);
3841 k = isl_basic_map_alloc_inequality(map->p[j]);
3844 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3845 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3846 map->p[j]->extra - bmap->n_div);
3858 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3864 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3865 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
3866 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
3868 if (isl_map_is_empty(map2)) {
3873 map1 = isl_map_compute_divs(map1);
3874 map2 = isl_map_compute_divs(map2);
3878 for (i = 0; map1 && i < map2->n; ++i)
3879 map1 = subtract(map1, map2->p[i]);
3889 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3891 return (struct isl_set *)
3893 (struct isl_map *)set1, (struct isl_map *)set2);
3896 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3900 isl_assert(set->ctx, set->dim == map->n_in, goto error);
3901 map = isl_map_intersect_domain(map, set);
3902 set = isl_map_range(map);
3910 /* There is no need to cow as removing empty parts doesn't change
3911 * the meaning of the set.
3913 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3920 for (i = map->n-1; i >= 0; --i) {
3921 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3923 isl_basic_map_free(map->p[i]);
3924 if (i != map->n-1) {
3925 F_CLR(map, ISL_MAP_NORMALIZED);
3926 map->p[i] = map->p[map->n-1];
3934 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3936 return (struct isl_set *)
3937 isl_map_remove_empty_parts((struct isl_map *)set);
3940 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3942 struct isl_basic_set *bset;
3943 if (!set || set->n == 0)
3945 bset = set->p[set->n-1];
3946 isl_assert(set->ctx, F_ISSET(bset, ISL_BASIC_SET_FINAL), return NULL);
3947 return isl_basic_set_copy(bset);
3950 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3951 struct isl_basic_set *bset)
3957 for (i = set->n-1; i >= 0; --i) {
3958 if (set->p[i] != bset)
3960 set = isl_set_cow(set);
3963 isl_basic_set_free(set->p[i]);
3964 if (i != set->n-1) {
3965 F_CLR(set, ISL_SET_NORMALIZED);
3966 set->p[i] = set->p[set->n-1];
3971 isl_basic_set_free(bset);
3975 isl_basic_set_free(bset);
3979 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3980 * for any common value of the parameters and dimensions preceding dim
3981 * in both basic sets, the values of dimension pos in bset1 are
3982 * smaller or larger then those in bset2.
3985 * 1 if bset1 follows bset2
3986 * -1 if bset1 precedes bset2
3987 * 0 if bset1 and bset2 are incomparable
3988 * -2 if some error occurred.
3990 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3991 struct isl_basic_set *bset2, int pos)
3993 struct isl_basic_map *bmap1 = NULL;
3994 struct isl_basic_map *bmap2 = NULL;
3995 struct isl_ctx *ctx;
3996 struct isl_vec *obj;
3999 enum isl_lp_result res;
4002 if (!bset1 || !bset2)
4005 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1),
4006 pos, bset1->dim - pos);
4007 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2),
4008 pos, bset2->dim - pos);
4009 if (!bmap1 || !bmap2)
4011 bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
4012 bmap1->n_in, bmap1->n_out + bmap2->n_out,
4013 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4016 total = bmap1->nparam + bmap1->n_in + bmap1->n_out + bmap1->n_div;
4018 obj = isl_vec_alloc(ctx, total);
4019 isl_seq_clr(obj->block.data, total);
4020 isl_int_set_si(obj->block.data[bmap1->nparam+bmap1->n_in], 1);
4021 isl_int_set_si(obj->block.data[bmap1->nparam+bmap1->n_in+
4022 bmap1->n_out-bmap2->n_out], -1);
4025 bmap1 = add_constraints(bmap1, bmap2, 0, bmap1->n_out - bmap2->n_out);
4028 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
4029 if (res == isl_lp_empty)
4031 else if (res == isl_lp_ok && isl_int_is_pos(num))
4033 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4034 res == isl_lp_unbounded)
4040 isl_basic_map_free(bmap1);
4041 isl_vec_free(ctx, obj);
4044 isl_basic_map_free(bmap1);
4045 isl_basic_map_free(bmap2);
4049 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4050 unsigned pos, isl_int *val)
4058 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
4059 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4060 for (; d+1 > pos; --d)
4061 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4065 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4067 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4069 if (!isl_int_is_one(bmap->eq[i][1+d]))
4072 isl_int_neg(*val, bmap->eq[i][0]);
4078 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4079 unsigned pos, isl_int *val)
4091 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4094 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4095 for (i = 1; fixed == 1 && i < map->n; ++i) {
4096 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4097 if (fixed == 1 && isl_int_ne(tmp, v))
4101 isl_int_set(*val, v);
4107 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4110 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4113 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4114 * then return this fixed value in *val.
4116 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4118 return isl_set_fast_has_fixed_var(set, set->nparam + dim, val);
4121 /* Check if input variable in has fixed value and if so and if val is not NULL,
4122 * then return this fixed value in *val.
4124 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4126 return isl_map_fast_has_fixed_var(map, map->nparam + in, val);
4129 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4130 * and if val is not NULL, then return this lower bound in *val.
4132 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4133 unsigned dim, isl_int *val)
4135 int i, i_eq = -1, i_ineq = -1;
4141 total = bset->nparam + bset->dim + bset->n_div;
4142 for (i = 0; i < bset->n_eq; ++i) {
4143 if (isl_int_is_zero(bset->eq[i][1+bset->nparam+dim]))
4149 for (i = 0; i < bset->n_ineq; ++i) {
4150 if (!isl_int_is_pos(bset->ineq[i][1+bset->nparam+dim]))
4152 if (i_eq != -1 || i_ineq != -1)
4156 if (i_eq == -1 && i_ineq == -1)
4158 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4159 /* The coefficient should always be one due to normalization. */
4160 if (!isl_int_is_one(c[1+bset->nparam+dim]))
4162 if (isl_seq_first_non_zero(c+1, bset->nparam+dim) != -1)
4164 if (isl_seq_first_non_zero(c+1+bset->nparam+dim+1,
4165 total - bset->nparam - dim - 1) != -1)
4168 isl_int_neg(*val, c[0]);
4172 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4173 unsigned dim, isl_int *val)
4185 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4189 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4191 for (i = 1; fixed == 1 && i < set->n; ++i) {
4192 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4194 if (fixed == 1 && isl_int_ne(tmp, v))
4198 isl_int_set(*val, v);
4204 /* Remove all information from bset that is redundant in the context
4205 * of context. In particular, equalities that are linear combinations
4206 * of those in context are remobed. Then the inequalities that are
4207 * redundant in the context of the equalities and inequalities of
4208 * context are removed.
4210 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
4211 struct isl_basic_set *context)
4215 struct isl_basic_set *combined;
4216 struct isl_ctx *ctx;
4217 enum isl_lp_result res = isl_lp_ok;
4219 if (!bset || !context)
4222 if (context->n_eq > 0) {
4225 struct isl_ctx *ctx = context->ctx;
4226 context = isl_basic_set_remove_equalities(context, &T, &T2);
4229 bset = isl_basic_set_preimage(ctx, bset, T);
4230 bset = uset_gist(bset, context);
4231 bset = isl_basic_set_preimage(ctx, bset, T2);
4234 combined = isl_basic_set_extend(isl_basic_set_copy(bset),
4235 0, bset->dim, 0, context->n_eq, context->n_ineq);
4236 context = set_add_constraints(combined, context, 0);
4241 for (i = bset->n_ineq-1; i >= 0; --i) {
4242 set_swap_inequality(context, i, context->n_ineq-1);
4244 res = isl_solve_lp((struct isl_basic_map *)context, 0,
4245 context->ineq[context->n_ineq]+1, ctx->one, &opt, NULL);
4247 set_swap_inequality(context, i, context->n_ineq-1);
4248 if (res == isl_lp_unbounded)
4250 if (res == isl_lp_error)
4252 if (res == isl_lp_empty) {
4253 bset = isl_basic_set_set_to_empty(bset);
4256 isl_int_add(opt, opt, context->ineq[i][0]);
4257 if (!isl_int_is_neg(opt)) {
4258 isl_basic_set_drop_inequality(context, i);
4259 isl_basic_set_drop_inequality(bset, i);
4263 if (res == isl_lp_error)
4265 isl_basic_set_free(context);
4268 isl_basic_set_free(context);
4269 isl_basic_set_free(bset);
4273 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
4274 struct isl_basic_map *context)
4276 struct isl_basic_set *bset;
4278 if (!bmap || !context)
4281 context = isl_basic_map_align_divs(context, bmap);
4282 bmap = isl_basic_map_align_divs(bmap, context);
4284 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
4285 isl_basic_map_underlying_set(context));
4287 return isl_basic_map_overlying_set(bset, bmap);
4289 isl_basic_map_free(bmap);
4290 isl_basic_map_free(context);
4294 struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
4298 map = isl_map_cow(map);
4299 if (!map || !context)
4301 isl_assert(map->ctx, map->nparam == context->nparam, goto error);
4302 isl_assert(map->ctx, map->n_in == context->n_in, goto error);
4303 isl_assert(map->ctx, map->n_out == context->n_out, goto error);
4304 for (i = 0; i < map->n; ++i)
4305 context = isl_basic_map_align_divs(context, map->p[i]);
4306 for (i = 0; i < map->n; ++i) {
4307 map->p[i] = isl_basic_map_gist(map->p[i],
4308 isl_basic_map_copy(context));
4312 isl_basic_map_free(context);
4313 F_CLR(map, ISL_MAP_NORMALIZED);
4317 isl_basic_map_free(context);
4321 struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
4323 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
4324 (struct isl_basic_map *)context);
4332 static int qsort_constraint_cmp(const void *p1, const void *p2)
4334 const struct constraint *c1 = (const struct constraint *)p1;
4335 const struct constraint *c2 = (const struct constraint *)p2;
4336 unsigned size = isl_min(c1->size, c2->size);
4337 return isl_seq_cmp(c1->c, c2->c, size);
4340 static struct isl_basic_map *isl_basic_map_sort_constraints(
4341 struct isl_basic_map *bmap)
4344 struct constraint *c;
4349 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
4350 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4353 for (i = 0; i < bmap->n_ineq; ++i) {
4355 c[i].c = bmap->ineq[i];
4357 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4358 for (i = 0; i < bmap->n_ineq; ++i)
4359 bmap->ineq[i] = c[i].c;
4363 isl_basic_map_free(bmap);
4367 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4371 if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4373 bmap = isl_basic_map_convex_hull(bmap);
4374 bmap = isl_basic_map_sort_constraints(bmap);
4375 F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4379 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4380 const struct isl_basic_map *bmap2)
4387 if (bmap1->nparam != bmap2->nparam)
4388 return bmap1->nparam - bmap2->nparam;
4389 if (bmap1->n_in != bmap2->n_in)
4390 return bmap1->n_in - bmap2->n_in;
4391 if (bmap1->n_out != bmap2->n_out)
4392 return bmap1->n_out - bmap2->n_out;
4393 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4394 F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4396 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4398 if (F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4400 if (bmap1->n_eq != bmap2->n_eq)
4401 return bmap1->n_eq - bmap2->n_eq;
4402 if (bmap1->n_ineq != bmap2->n_ineq)
4403 return bmap1->n_ineq - bmap2->n_ineq;
4404 if (bmap1->n_div != bmap2->n_div)
4405 return bmap1->n_div - bmap2->n_div;
4406 total = bmap1->nparam + bmap1->n_in + bmap1->n_out + bmap1->n_div;
4407 for (i = 0; i < bmap1->n_eq; ++i) {
4408 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4412 for (i = 0; i < bmap1->n_ineq; ++i) {
4413 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4417 for (i = 0; i < bmap1->n_div; ++i) {
4418 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4425 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4426 struct isl_basic_map *bmap2)
4428 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4431 static int qsort_bmap_cmp(const void *p1, const void *p2)
4433 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4434 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4436 return isl_basic_map_fast_cmp(bmap1, bmap2);
4439 /* We normalize in place, but if anything goes wrong we need
4440 * to return NULL, so we need to make sure we don't change the
4441 * meaning of any possible other copies of map.
4443 struct isl_map *isl_map_normalize(struct isl_map *map)
4446 struct isl_basic_map *bmap;
4450 if (F_ISSET(map, ISL_MAP_NORMALIZED))
4452 for (i = 0; i < map->n; ++i) {
4453 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4456 isl_basic_map_free(map->p[i]);
4459 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4460 F_SET(map, ISL_MAP_NORMALIZED);
4461 map = isl_map_remove_empty_parts(map);
4464 for (i = map->n - 1; i >= 1; --i) {
4465 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4467 isl_basic_map_free(map->p[i-1]);
4468 for (j = i; j < map->n; ++j)
4469 map->p[j-1] = map->p[j];
4479 struct isl_set *isl_set_normalize(struct isl_set *set)
4481 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4484 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4494 if (map1->nparam != map2->nparam)
4496 if (map1->n_in != map2->n_in)
4498 if (map1->n_out != map2->n_out)
4501 map1 = isl_map_copy(map1);
4502 map2 = isl_map_copy(map2);
4503 map1 = isl_map_normalize(map1);
4504 map2 = isl_map_normalize(map2);
4507 equal = map1->n == map2->n;
4508 for (i = 0; equal && i < map1->n; ++i) {
4509 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4522 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4524 return isl_map_fast_is_equal((struct isl_map *)set1,
4525 (struct isl_map *)set2);