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 struct isl_basic_map *remove_duplicate_divs(
1111 struct isl_basic_map *bmap, int *progress)
1118 unsigned total_var = bmap->nparam + bmap->n_in + bmap->n_out;
1119 unsigned total = total_var + bmap->n_div;
1120 struct isl_ctx *ctx;
1122 if (bmap->n_div <= 1)
1126 for (k = bmap->n_div - 1; k >= 0; --k)
1127 if (!isl_int_is_zero(bmap->div[k][0]))
1132 size = round_up(4 * bmap->n_div / 3 - 1);
1133 bits = ffs(size) - 1;
1134 index = isl_alloc_array(ctx, int, size);
1135 memset(index, 0, size * sizeof(int));
1138 eq = isl_blk_alloc(ctx, 1+total);
1139 if (isl_blk_is_error(eq))
1142 isl_seq_clr(eq.data, 1+total);
1143 index[isl_seq_hash(bmap->div[k], 2+total, bits)] = k + 1;
1144 for (--k; k >= 0; --k) {
1147 if (isl_int_is_zero(bmap->div[k][0]))
1150 hash = isl_seq_hash(bmap->div[k], 2+total, bits);
1151 for (h = hash; index[h]; h = (h+1) % size)
1152 if (isl_seq_eq(bmap->div[k],
1153 bmap->div[index[h]-1], 2+total))
1158 isl_int_set_si(eq.data[1+total_var+k], -1);
1159 isl_int_set_si(eq.data[1+total_var+l], 1);
1160 eliminate_div(bmap, eq.data, l);
1161 isl_int_set_si(eq.data[1+total_var+k], 0);
1162 isl_int_set_si(eq.data[1+total_var+l], 0);
1167 isl_blk_free(ctx, eq);
1173 /* Elimininate divs based on equalities
1175 static struct isl_basic_map *eliminate_divs_eq(
1176 struct isl_basic_map *bmap, int *progress)
1186 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1188 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1189 for (i = 0; i < bmap->n_eq; ++i) {
1190 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
1191 !isl_int_is_negone(bmap->eq[i][off + d]))
1195 eliminate_div(bmap, bmap->eq[i], d);
1196 isl_basic_map_drop_equality(bmap, i);
1201 return eliminate_divs_eq(bmap, progress);
1205 static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
1209 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1212 for (i = bmap->n_eq - 1; i >= 0; --i) {
1213 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
1214 if (isl_int_is_zero(gcd)) {
1215 if (!isl_int_is_zero(bmap->eq[i][0])) {
1216 bmap = isl_basic_map_set_to_empty(bmap);
1219 isl_basic_map_drop_equality(bmap, i);
1222 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1223 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
1224 if (isl_int_is_one(gcd))
1226 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
1227 bmap = isl_basic_map_set_to_empty(bmap);
1230 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
1233 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1234 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
1235 if (isl_int_is_zero(gcd)) {
1236 if (isl_int_is_neg(bmap->ineq[i][0])) {
1237 bmap = isl_basic_map_set_to_empty(bmap);
1240 isl_basic_map_drop_inequality(bmap, i);
1243 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1244 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
1245 if (isl_int_is_one(gcd))
1247 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
1248 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
1256 /* Eliminate the specified variables from the constraints using
1257 * Fourier-Motzkin. The variables themselves are not removed.
1259 struct isl_basic_map *isl_basic_map_eliminate_vars(
1260 struct isl_basic_map *bmap, int pos, unsigned n)
1269 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1270 extra = bmap->extra - bmap->n_div;
1272 bmap = isl_basic_map_cow(bmap);
1273 for (d = pos + n - 1; d >= pos; --d) {
1274 int n_lower, n_upper;
1277 for (i = 0; i < bmap->n_eq; ++i) {
1278 if (isl_int_is_zero(bmap->eq[i][1+d]))
1280 eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
1281 isl_basic_map_drop_equality(bmap, i);
1288 for (i = 0; i < bmap->n_ineq; ++i) {
1289 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1291 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1294 bmap = isl_basic_map_extend(bmap,
1295 bmap->nparam, bmap->n_in, bmap->n_out, 0,
1296 0, n_lower * n_upper);
1297 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1299 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1302 for (j = 0; j < i; ++j) {
1303 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1306 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1307 isl_int_sgn(bmap->ineq[j][1+d]))
1309 k = isl_basic_map_alloc_inequality(bmap);
1312 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1314 isl_seq_clr(bmap->ineq[k]+1+total, extra);
1315 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1316 1+d, 1+total, NULL);
1318 isl_basic_map_drop_inequality(bmap, i);
1321 if (n_lower > 0 && n_upper > 0) {
1322 bmap = normalize_constraints(bmap);
1323 bmap = remove_duplicate_constraints(bmap, NULL);
1324 bmap = isl_basic_map_gauss(bmap, NULL);
1325 bmap = isl_basic_map_convex_hull(bmap);
1328 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1332 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1335 isl_basic_map_free(bmap);
1339 struct isl_basic_set *isl_basic_set_eliminate_vars(
1340 struct isl_basic_set *bset, unsigned pos, unsigned n)
1342 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1343 (struct isl_basic_map *)bset, pos, n);
1346 /* Eliminate the specified n dimensions starting at first from the
1347 * constraints using Fourier-Motzkin, The dimensions themselves
1350 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1351 unsigned first, unsigned n)
1358 set = isl_set_cow(set);
1361 isl_assert(set->ctx, first+n <= set->dim, goto error);
1363 for (i = 0; i < set->n; ++i) {
1364 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1365 set->nparam + first, n);
1375 /* Project out n dimensions starting at first using Fourier-Motzkin */
1376 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1377 unsigned first, unsigned n)
1379 set = isl_set_eliminate_dims(set, first, n);
1380 set = isl_set_drop_dims(set, first, n);
1384 /* Project out n inputs starting at first using Fourier-Motzkin */
1385 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1386 unsigned first, unsigned n)
1393 map = isl_map_cow(map);
1396 isl_assert(map->ctx, first+n <= map->n_in, goto error);
1398 for (i = 0; i < map->n; ++i) {
1399 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1400 map->nparam + first, n);
1404 map = isl_map_drop_inputs(map, first, n);
1411 /* Project out n dimensions starting at first using Fourier-Motzkin */
1412 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1413 unsigned first, unsigned n)
1415 bset = isl_basic_set_eliminate_vars(bset, bset->nparam + first, n);
1416 bset = isl_basic_set_drop_dims(bset, first, n);
1420 /* Elimininate divs based on inequalities
1422 static struct isl_basic_map *eliminate_divs_ineq(
1423 struct isl_basic_map *bmap, int *progress)
1428 struct isl_ctx *ctx;
1434 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1436 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1437 for (i = 0; i < bmap->n_eq; ++i)
1438 if (!isl_int_is_zero(bmap->eq[i][off + d]))
1442 for (i = 0; i < bmap->n_ineq; ++i)
1443 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
1445 if (i < bmap->n_ineq)
1448 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
1449 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1451 bmap = isl_basic_map_drop_div(bmap, d);
1458 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1465 bmap = normalize_constraints(bmap);
1466 bmap = eliminate_divs_eq(bmap, &progress);
1467 bmap = eliminate_divs_ineq(bmap, &progress);
1468 bmap = isl_basic_map_gauss(bmap, &progress);
1469 bmap = remove_duplicate_divs(bmap, &progress);
1470 bmap = remove_duplicate_constraints(bmap, &progress);
1475 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1477 return (struct isl_basic_set *)
1478 isl_basic_map_simplify((struct isl_basic_map *)bset);
1481 static void dump_term(struct isl_basic_map *bmap,
1482 isl_int c, int pos, FILE *out)
1484 unsigned in = bmap->n_in;
1485 unsigned dim = bmap->n_in + bmap->n_out;
1487 isl_int_print(out, c, 0);
1489 if (!isl_int_is_one(c))
1490 isl_int_print(out, c, 0);
1491 if (pos < 1 + bmap->nparam)
1492 fprintf(out, "p%d", pos - 1);
1493 else if (pos < 1 + bmap->nparam + in)
1494 fprintf(out, "i%d", pos - 1 - bmap->nparam);
1495 else if (pos < 1 + bmap->nparam + dim)
1496 fprintf(out, "o%d", pos - 1 - bmap->nparam - in);
1498 fprintf(out, "e%d", pos - 1 - bmap->nparam - dim);
1502 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1503 int sign, FILE *out)
1507 unsigned len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1511 for (i = 0, first = 1; i < len; ++i) {
1512 if (isl_int_sgn(c[i]) * sign <= 0)
1515 fprintf(out, " + ");
1517 isl_int_abs(v, c[i]);
1518 dump_term(bmap, v, i, out);
1525 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1526 const char *op, FILE *out, int indent)
1530 fprintf(out, "%*s", indent, "");
1532 dump_constraint_sign(bmap, c, 1, out);
1533 fprintf(out, " %s ", op);
1534 dump_constraint_sign(bmap, c, -1, out);
1538 for (i = bmap->n_div; i < bmap->extra; ++i) {
1539 if (isl_int_is_zero(c[1+bmap->nparam+bmap->n_in+bmap->n_out+i]))
1541 fprintf(out, "%*s", indent, "");
1542 fprintf(out, "ERROR: unused div coefficient not zero\n");
1546 static void dump_constraints(struct isl_basic_map *bmap,
1547 isl_int **c, unsigned n,
1548 const char *op, FILE *out, int indent)
1552 for (i = 0; i < n; ++i)
1553 dump_constraint(bmap, c[i], op, out, indent);
1556 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1560 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1562 for (j = 0; j < 1 + total; ++j) {
1563 if (isl_int_is_zero(exp[j]))
1565 if (!first && isl_int_is_pos(exp[j]))
1567 dump_term(bmap, exp[j], j, out);
1572 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1576 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1577 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1579 for (i = 0; i < bmap->n_div; ++i) {
1580 fprintf(out, "%*s", indent, "");
1581 fprintf(out, "e%d = [(", i);
1582 dump_affine(bmap, bmap->div[i]+1, out);
1584 isl_int_print(out, bmap->div[i][0], 0);
1585 fprintf(out, "]\n");
1589 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1592 fprintf(out, "null basic set\n");
1596 fprintf(out, "%*s", indent, "");
1597 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1598 bset->ref, bset->nparam, bset->dim, bset->extra,
1600 dump((struct isl_basic_map *)bset, out, indent);
1603 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1606 fprintf(out, "null basic map\n");
1610 fprintf(out, "%*s", indent, "");
1611 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, flags: %x\n",
1613 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, bmap->flags);
1614 dump(bmap, out, indent);
1617 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1619 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1622 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1623 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1624 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1625 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1629 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1630 unsigned nparam, unsigned dim, int n, unsigned flags)
1632 struct isl_set *set;
1634 isl_assert(ctx, n >= 0, return NULL);
1635 set = isl_alloc(ctx, struct isl_set,
1636 sizeof(struct isl_set) +
1637 n * sizeof(struct isl_basic_set *));
1646 set->nparam = nparam;
1653 struct isl_set *isl_set_dup(struct isl_set *set)
1656 struct isl_set *dup;
1661 dup = isl_set_alloc(set->ctx, set->nparam, set->dim, set->n, set->flags);
1664 for (i = 0; i < set->n; ++i)
1665 dup = isl_set_add(dup,
1666 isl_basic_set_dup(set->p[i]));
1670 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1672 struct isl_set *set;
1677 set = isl_set_alloc(bset->ctx, bset->nparam, bset->dim, 1, ISL_MAP_DISJOINT);
1679 isl_basic_set_free(bset);
1682 return isl_set_add(set, bset);
1685 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1687 struct isl_map *map;
1692 map = isl_map_alloc(bmap->ctx, bmap->nparam, bmap->n_in, bmap->n_out, 1,
1695 isl_basic_map_free(bmap);
1698 return isl_map_add(map, bmap);
1701 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1705 isl_assert(set->ctx, set->nparam == bset->nparam, goto error);
1706 isl_assert(set->ctx, set->dim == bset->dim, goto error);
1707 isl_assert(set->ctx, set->n < set->size, goto error);
1708 set->p[set->n] = bset;
1715 isl_basic_set_free(bset);
1719 void isl_set_free(struct isl_set *set)
1729 isl_ctx_deref(set->ctx);
1730 for (i = 0; i < set->n; ++i)
1731 isl_basic_set_free(set->p[i]);
1735 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1740 fprintf(out, "null set\n");
1744 fprintf(out, "%*s", indent, "");
1745 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1746 set->ref, set->n, set->nparam, set->dim, set->flags);
1747 for (i = 0; i < set->n; ++i) {
1748 fprintf(out, "%*s", indent, "");
1749 fprintf(out, "basic set %d:\n", i);
1750 isl_basic_set_dump(set->p[i], out, indent+4);
1754 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1759 fprintf(out, "null map\n");
1763 fprintf(out, "%*s", indent, "");
1764 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, flags: %x\n",
1765 map->ref, map->n, map->nparam, map->n_in, map->n_out,
1767 for (i = 0; i < map->n; ++i) {
1768 fprintf(out, "%*s", indent, "");
1769 fprintf(out, "basic map %d:\n", i);
1770 isl_basic_map_dump(map->p[i], out, indent+4);
1774 struct isl_basic_map *isl_basic_map_intersect_domain(
1775 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1777 struct isl_basic_map *bmap_domain;
1782 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1783 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1785 bmap = isl_basic_map_extend(bmap,
1786 bset->nparam, bmap->n_in, bmap->n_out,
1787 bset->n_div, bset->n_eq, bset->n_ineq);
1790 bmap_domain = isl_basic_map_from_basic_set(bset,
1792 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1794 bmap = isl_basic_map_simplify(bmap);
1795 return isl_basic_map_finalize(bmap);
1797 isl_basic_map_free(bmap);
1798 isl_basic_set_free(bset);
1802 struct isl_basic_map *isl_basic_map_intersect_range(
1803 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1805 struct isl_basic_map *bmap_range;
1810 isl_assert(bset->ctx, bset->dim == bmap->n_out, goto error);
1811 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1813 bmap = isl_basic_map_extend(bmap,
1814 bset->nparam, bmap->n_in, bmap->n_out,
1815 bset->n_div, bset->n_eq, bset->n_ineq);
1818 bmap_range = isl_basic_map_from_basic_set(bset,
1820 bmap = add_constraints(bmap, bmap_range, 0, 0);
1822 bmap = isl_basic_map_simplify(bmap);
1823 return isl_basic_map_finalize(bmap);
1825 isl_basic_map_free(bmap);
1826 isl_basic_set_free(bset);
1830 struct isl_basic_map *isl_basic_map_intersect(
1831 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1833 if (!bmap1 || !bmap2)
1836 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
1837 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
1838 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
1840 bmap1 = isl_basic_map_extend(bmap1,
1841 bmap1->nparam, bmap1->n_in, bmap1->n_out,
1842 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1845 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1847 bmap1 = isl_basic_map_simplify(bmap1);
1848 return isl_basic_map_finalize(bmap1);
1850 isl_basic_map_free(bmap1);
1851 isl_basic_map_free(bmap2);
1855 struct isl_basic_set *isl_basic_set_intersect(
1856 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1858 return (struct isl_basic_set *)
1859 isl_basic_map_intersect(
1860 (struct isl_basic_map *)bset1,
1861 (struct isl_basic_map *)bset2);
1864 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1867 struct isl_map *result;
1873 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
1874 F_ISSET(map2, ISL_MAP_DISJOINT))
1875 FL_SET(flags, ISL_MAP_DISJOINT);
1877 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
1878 map1->n * map2->n, flags);
1881 for (i = 0; i < map1->n; ++i)
1882 for (j = 0; j < map2->n; ++j) {
1883 struct isl_basic_map *part;
1884 part = isl_basic_map_intersect(
1885 isl_basic_map_copy(map1->p[i]),
1886 isl_basic_map_copy(map2->p[j]));
1887 if (isl_basic_map_is_empty(part))
1888 isl_basic_map_free(part);
1890 result = isl_map_add(result, part);
1903 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1905 return (struct isl_set *)
1906 isl_map_intersect((struct isl_map *)set1,
1907 (struct isl_map *)set2);
1910 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1912 struct isl_basic_set *bset;
1917 bmap = isl_basic_map_cow(bmap);
1921 bset = isl_basic_set_from_basic_map(bmap);
1922 bset = isl_basic_set_swap_vars(bset, in);
1923 return isl_basic_map_from_basic_set(bset, bset->dim-in, in);
1926 /* Turn final n dimensions into existentially quantified variables.
1928 struct isl_basic_set *isl_basic_set_project_out(
1929 struct isl_basic_set *bset, unsigned n, unsigned flags)
1939 isl_assert(bset->ctx, n <= bset->dim, goto error);
1944 bset = isl_basic_set_cow(bset);
1946 row_size = 1 + bset->nparam + bset->dim + bset->extra;
1947 old = bset->block2.data;
1948 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1949 (bset->extra + n) * (1 + row_size));
1950 if (!bset->block2.data)
1952 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1955 for (i = 0; i < n; ++i) {
1956 new_div[i] = bset->block2.data +
1957 (bset->extra + i) * (1 + row_size);
1958 isl_seq_clr(new_div[i], 1 + row_size);
1960 for (i = 0; i < bset->extra; ++i)
1961 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1963 bset->div = new_div;
1967 bset = isl_basic_set_simplify(bset);
1968 return isl_basic_set_finalize(bset);
1970 isl_basic_set_free(bset);
1974 struct isl_basic_map *isl_basic_map_apply_range(
1975 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1977 struct isl_basic_set *bset;
1978 unsigned n_in, n_out;
1980 if (!bmap1 || !bmap2)
1983 isl_assert(bmap->ctx, bmap1->n_out == bmap2->n_in, goto error);
1984 isl_assert(bmap->ctx, bmap1->nparam == bmap2->nparam, goto error);
1987 n_out = bmap2->n_out;
1989 bmap2 = isl_basic_map_reverse(bmap2);
1992 bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
1993 bmap1->n_in + bmap2->n_in, bmap1->n_out,
1994 bmap2->extra, bmap2->n_eq, bmap2->n_ineq);
1997 bmap1 = add_constraints(bmap1, bmap2, bmap1->n_in - bmap2->n_in, 0);
1998 bmap1 = isl_basic_map_simplify(bmap1);
1999 bset = isl_basic_set_from_basic_map(bmap1);
2000 bset = isl_basic_set_project_out(bset,
2001 bset->dim - (n_in + n_out), 0);
2002 return isl_basic_map_from_basic_set(bset, n_in, n_out);
2004 isl_basic_map_free(bmap1);
2005 isl_basic_map_free(bmap2);
2009 struct isl_basic_set *isl_basic_set_apply(
2010 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2015 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
2017 return (struct isl_basic_set *)
2018 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2020 isl_basic_set_free(bset);
2021 isl_basic_map_free(bmap);
2025 struct isl_basic_map *isl_basic_map_apply_domain(
2026 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2028 if (!bmap1 || !bmap2)
2031 isl_assert(ctx, bmap1->n_in == bmap2->n_in, goto error);
2032 isl_assert(ctx, bmap1->nparam == bmap2->nparam, goto error);
2034 bmap1 = isl_basic_map_reverse(bmap1);
2035 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2036 return isl_basic_map_reverse(bmap1);
2038 isl_basic_map_free(bmap1);
2039 isl_basic_map_free(bmap2);
2043 static struct isl_basic_map *var_equal(struct isl_ctx *ctx,
2044 struct isl_basic_map *bmap, unsigned pos)
2047 i = isl_basic_map_alloc_equality(bmap);
2050 isl_seq_clr(bmap->eq[i],
2051 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2052 isl_int_set_si(bmap->eq[i][1+bmap->nparam+pos], -1);
2053 isl_int_set_si(bmap->eq[i][1+bmap->nparam+bmap->n_in+pos], 1);
2054 return isl_basic_map_finalize(bmap);
2056 isl_basic_map_free(bmap);
2060 static struct isl_basic_map *var_more(struct isl_ctx *ctx,
2061 struct isl_basic_map *bmap, unsigned pos)
2064 i = isl_basic_map_alloc_inequality(bmap);
2067 isl_seq_clr(bmap->ineq[i],
2068 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2069 isl_int_set_si(bmap->ineq[i][0], -1);
2070 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], -1);
2071 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], 1);
2072 return isl_basic_map_finalize(bmap);
2074 isl_basic_map_free(bmap);
2078 static struct isl_basic_map *var_less(struct isl_ctx *ctx,
2079 struct isl_basic_map *bmap, unsigned pos)
2082 i = isl_basic_map_alloc_inequality(bmap);
2085 isl_seq_clr(bmap->ineq[i],
2086 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2087 isl_int_set_si(bmap->ineq[i][0], -1);
2088 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], 1);
2089 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], -1);
2090 return isl_basic_map_finalize(bmap);
2092 isl_basic_map_free(bmap);
2096 struct isl_basic_map *isl_basic_map_equal(struct isl_ctx *ctx,
2097 unsigned nparam, unsigned in, unsigned out, unsigned n_equal)
2100 struct isl_basic_map *bmap;
2101 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, n_equal, 0);
2104 for (i = 0; i < n_equal && bmap; ++i)
2105 bmap = var_equal(ctx, bmap, i);
2106 return isl_basic_map_finalize(bmap);
2109 struct isl_basic_map *isl_basic_map_less_at(struct isl_ctx *ctx,
2110 unsigned nparam, unsigned in, unsigned out, unsigned pos)
2113 struct isl_basic_map *bmap;
2114 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
2117 for (i = 0; i < pos && bmap; ++i)
2118 bmap = var_equal(ctx, bmap, i);
2120 bmap = var_less(ctx, bmap, pos);
2121 return isl_basic_map_finalize(bmap);
2124 struct isl_basic_map *isl_basic_map_more_at(struct isl_ctx *ctx,
2125 unsigned nparam, unsigned in, unsigned out, unsigned pos)
2128 struct isl_basic_map *bmap;
2129 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
2132 for (i = 0; i < pos && bmap; ++i)
2133 bmap = var_equal(ctx, bmap, i);
2135 bmap = var_more(ctx, bmap, pos);
2136 return isl_basic_map_finalize(bmap);
2139 struct isl_basic_map *isl_basic_map_from_basic_set(
2140 struct isl_basic_set *bset, unsigned n_in, unsigned n_out)
2142 struct isl_basic_map *bmap;
2144 bset = isl_basic_set_cow(bset);
2148 isl_assert(ctx, bset->dim == n_in + n_out, goto error);
2149 bmap = (struct isl_basic_map *) bset;
2151 bmap->n_out = n_out;
2152 return isl_basic_map_finalize(bmap);
2154 isl_basic_set_free(bset);
2158 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2162 if (bmap->n_in == 0)
2163 return (struct isl_basic_set *)bmap;
2164 bmap = isl_basic_map_cow(bmap);
2167 bmap->n_out += bmap->n_in;
2169 bmap = isl_basic_map_finalize(bmap);
2170 return (struct isl_basic_set *)bmap;
2175 /* For a div d = floor(f/m), add the constraints
2178 * -(f-(n-1)) + m d >= 0
2180 * Note that the second constraint is the negation of
2184 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2187 unsigned div_pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
2188 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
2190 i = isl_basic_map_alloc_inequality(bmap);
2193 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2194 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2196 j = isl_basic_map_alloc_inequality(bmap);
2199 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2200 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2201 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2205 struct isl_basic_set *isl_basic_map_underlying_set(
2206 struct isl_basic_map *bmap)
2210 if (bmap->nparam == 0 && bmap->n_in == 0 && bmap->n_div == 0)
2211 return (struct isl_basic_set *)bmap;
2212 bmap = isl_basic_map_cow(bmap);
2215 bmap->n_out += bmap->nparam + bmap->n_in + bmap->n_div;
2218 bmap->extra -= bmap->n_div;
2220 bmap = isl_basic_map_finalize(bmap);
2221 return (struct isl_basic_set *)bmap;
2226 struct isl_basic_map *isl_basic_map_overlying_set(
2227 struct isl_basic_set *bset, struct isl_basic_map *like)
2229 struct isl_basic_map *bmap;
2230 struct isl_ctx *ctx;
2237 isl_assert(ctx, bset->dim ==
2238 like->nparam + like->n_in + like->n_out + like->n_div,
2240 if (like->nparam == 0 && like->n_in == 0 && like->n_div == 0) {
2241 isl_basic_map_free(like);
2242 return (struct isl_basic_map *)bset;
2244 bset = isl_basic_set_cow(bset);
2247 total = bset->dim + bset->extra;
2248 bmap = (struct isl_basic_map *)bset;
2249 bmap->nparam = like->nparam;
2250 bmap->n_in = like->n_in;
2251 bmap->n_out = like->n_out;
2252 bmap->extra += like->n_div;
2255 ltotal = total - bmap->extra + like->extra;
2258 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2259 bmap->extra * (1 + 1 + total));
2260 if (isl_blk_is_error(bmap->block2))
2262 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2266 bmap = isl_basic_map_extend(bmap, bmap->nparam,
2267 bmap->n_in, bmap->n_out, 0, 0, 2 * like->n_div);
2268 for (i = 0; i < like->n_div; ++i) {
2269 k = isl_basic_map_alloc_div(bmap);
2272 isl_seq_cpy(bmap->div[k], like->div[i], 1 + 1 + ltotal);
2273 isl_seq_clr(bmap->div[k]+1+1+ltotal, total - ltotal);
2274 if (add_div_constraints(bmap, k) < 0)
2278 isl_basic_map_free(like);
2279 bmap = isl_basic_map_finalize(bmap);
2282 isl_basic_map_free(like);
2283 isl_basic_set_free(bset);
2287 struct isl_basic_set *isl_basic_set_from_underlying_set(
2288 struct isl_basic_set *bset, struct isl_basic_set *like)
2290 return (struct isl_basic_set *)
2291 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2294 struct isl_set *isl_set_from_underlying_set(
2295 struct isl_set *set, struct isl_basic_set *like)
2301 isl_assert(set->ctx, set->dim == like->nparam + like->dim + like->n_div,
2303 if (like->nparam == 0 && like->n_div == 0) {
2304 isl_basic_set_free(like);
2307 set = isl_set_cow(set);
2310 for (i = 0; i < set->n; ++i) {
2311 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2312 isl_basic_set_copy(like));
2316 set->nparam = like->nparam;
2317 set->dim = like->dim;
2318 isl_basic_set_free(like);
2321 isl_basic_set_free(like);
2326 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2330 map = isl_map_align_divs(map);
2331 map = isl_map_cow(map);
2335 for (i = 0; i < map->n; ++i) {
2336 map->p[i] = (struct isl_basic_map *)
2337 isl_basic_map_underlying_set(map->p[i]);
2342 map->n_out += map->nparam + map->n_in;
2344 map->n_out = map->p[0]->n_out;
2347 return (struct isl_set *)map;
2353 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2355 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2358 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2360 struct isl_basic_set *domain;
2364 n_out = bmap->n_out;
2365 domain = isl_basic_set_from_basic_map(bmap);
2366 return isl_basic_set_project_out(domain, n_out, 0);
2369 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2371 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2374 struct isl_set *isl_map_range(struct isl_map *map)
2377 struct isl_set *set;
2381 map = isl_map_cow(map);
2385 set = (struct isl_set *) map;
2387 for (i = 0; i < map->n; ++i) {
2388 set->p[i] = isl_basic_map_range(map->p[i]);
2392 F_CLR(set, ISL_MAP_DISJOINT);
2393 F_CLR(set, ISL_SET_NORMALIZED);
2400 struct isl_map *isl_map_from_set(struct isl_set *set,
2401 unsigned n_in, unsigned n_out)
2404 struct isl_map *map = NULL;
2408 isl_assert(set->ctx, set->dim == n_in + n_out, goto error);
2409 set = isl_set_cow(set);
2412 map = (struct isl_map *)set;
2413 for (i = 0; i < set->n; ++i) {
2414 map->p[i] = isl_basic_map_from_basic_set(
2415 set->p[i], n_in, n_out);
2427 struct isl_set *isl_set_from_map(struct isl_map *map)
2430 struct isl_set *set = NULL;
2434 map = isl_map_cow(map);
2437 map->n_out += map->n_in;
2439 set = (struct isl_set *)map;
2440 for (i = 0; i < map->n; ++i) {
2441 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2451 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2452 unsigned nparam, unsigned in, unsigned out, int n,
2455 struct isl_map *map;
2457 map = isl_alloc(ctx, struct isl_map,
2458 sizeof(struct isl_map) +
2459 n * sizeof(struct isl_basic_map *));
2468 map->nparam = nparam;
2475 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2476 unsigned nparam, unsigned in, unsigned out)
2478 struct isl_basic_map *bmap;
2479 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2480 bmap = isl_basic_map_set_to_empty(bmap);
2484 struct isl_basic_set *isl_basic_set_empty(struct isl_ctx *ctx,
2485 unsigned nparam, unsigned dim)
2487 struct isl_basic_set *bset;
2488 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 1, 0);
2489 bset = isl_basic_set_set_to_empty(bset);
2493 struct isl_basic_map *isl_basic_map_universe(struct isl_ctx *ctx,
2494 unsigned nparam, unsigned in, unsigned out)
2496 struct isl_basic_map *bmap;
2497 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 0, 0);
2501 struct isl_basic_set *isl_basic_set_universe(struct isl_ctx *ctx,
2502 unsigned nparam, unsigned dim)
2504 struct isl_basic_set *bset;
2505 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 0, 0);
2509 struct isl_map *isl_map_empty(struct isl_ctx *ctx,
2510 unsigned nparam, unsigned in, unsigned out)
2512 return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
2515 struct isl_set *isl_set_empty(struct isl_ctx *ctx,
2516 unsigned nparam, unsigned dim)
2518 return isl_set_alloc(ctx, nparam, dim, 0, ISL_MAP_DISJOINT);
2521 struct isl_map *isl_map_dup(struct isl_map *map)
2524 struct isl_map *dup;
2528 dup = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out, map->n,
2530 for (i = 0; i < map->n; ++i)
2531 dup = isl_map_add(dup,
2532 isl_basic_map_dup(map->p[i]));
2536 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2540 isl_assert(map->ctx, map->nparam == bmap->nparam, goto error);
2541 isl_assert(map->ctx, map->n_in == bmap->n_in, goto error);
2542 isl_assert(map->ctx, map->n_out == bmap->n_out, goto error);
2543 isl_assert(map->ctx, map->n < map->size, goto error);
2544 map->p[map->n] = bmap;
2546 F_CLR(map, ISL_MAP_NORMALIZED);
2552 isl_basic_map_free(bmap);
2556 void isl_map_free(struct isl_map *map)
2566 isl_ctx_deref(map->ctx);
2567 for (i = 0; i < map->n; ++i)
2568 isl_basic_map_free(map->p[i]);
2572 struct isl_map *isl_map_extend(struct isl_map *base,
2573 unsigned nparam, unsigned n_in, unsigned n_out)
2577 base = isl_map_cow(base);
2581 isl_assert(base->ctx, base->nparam <= nparam, goto error);
2582 isl_assert(base->ctx, base->n_in <= n_in, goto error);
2583 isl_assert(base->ctx, base->n_out <= n_out, goto error);
2584 base->nparam = nparam;
2586 base->n_out = n_out;
2587 for (i = 0; i < base->n; ++i) {
2588 base->p[i] = isl_basic_map_extend(base->p[i],
2589 nparam, n_in, n_out, 0, 0, 0);
2599 struct isl_set *isl_set_extend(struct isl_set *base,
2600 unsigned nparam, unsigned dim)
2602 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2606 static struct isl_basic_map *isl_basic_map_fix_var(struct isl_basic_map *bmap,
2607 unsigned var, int value)
2611 bmap = isl_basic_map_cow(bmap);
2612 bmap = isl_basic_map_extend(bmap,
2613 bmap->nparam, bmap->n_in, bmap->n_out, 0, 1, 0);
2614 j = isl_basic_map_alloc_equality(bmap);
2617 isl_seq_clr(bmap->eq[j],
2618 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2619 isl_int_set_si(bmap->eq[j][1 + var], -1);
2620 isl_int_set_si(bmap->eq[j][0], value);
2621 bmap = isl_basic_map_simplify(bmap);
2622 return isl_basic_map_finalize(bmap);
2624 isl_basic_map_free(bmap);
2628 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2629 unsigned input, int value)
2633 isl_assert(bmap->ctx, input < bmap->n_in, goto error);
2634 return isl_basic_map_fix_var(bmap, bmap->nparam + input, value);
2636 isl_basic_map_free(bmap);
2640 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2641 unsigned dim, int value)
2645 isl_assert(bset->ctx, dim < bset->dim, goto error);
2646 return (struct isl_basic_set *)
2647 isl_basic_map_fix_var((struct isl_basic_map *)bset,
2648 bset->nparam + dim, value);
2650 isl_basic_set_free(bset);
2654 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2655 unsigned input, int value)
2659 map = isl_map_cow(map);
2663 isl_assert(ctx, input < map->n_in, goto error);
2664 for (i = 0; i < map->n; ++i) {
2665 map->p[i] = isl_basic_map_fix_input_si(map->p[i], input, value);
2669 F_CLR(map, ISL_MAP_NORMALIZED);
2676 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2680 set = isl_set_cow(set);
2684 isl_assert(set->ctx, dim < set->dim, goto error);
2685 for (i = 0; i < set->n; ++i) {
2686 set->p[i] = isl_basic_set_fix_dim_si(set->p[i], dim, value);
2696 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2697 unsigned dim, isl_int value)
2701 bset = isl_basic_set_cow(bset);
2702 bset = isl_basic_set_extend(bset, bset->nparam, bset->dim, 0, 0, 1);
2703 j = isl_basic_set_alloc_inequality(bset);
2706 isl_seq_clr(bset->ineq[j], 1 + bset->nparam + bset->dim + bset->extra);
2707 isl_int_set_si(bset->ineq[j][1 + bset->nparam + dim], 1);
2708 isl_int_neg(bset->ineq[j][0], value);
2709 bset = isl_basic_set_simplify(bset);
2710 return isl_basic_set_finalize(bset);
2712 isl_basic_set_free(bset);
2716 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2721 set = isl_set_cow(set);
2725 isl_assert(set->ctx, dim < set->dim, goto error);
2726 for (i = 0; i < set->n; ++i) {
2727 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2737 struct isl_map *isl_map_reverse(struct isl_map *map)
2742 map = isl_map_cow(map);
2747 map->n_in = map->n_out;
2749 for (i = 0; i < map->n; ++i) {
2750 map->p[i] = isl_basic_map_reverse(map->p[i]);
2754 F_CLR(map, ISL_MAP_NORMALIZED);
2761 struct isl_map *isl_basic_map_lexmax(
2762 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2763 struct isl_set **empty)
2765 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2768 struct isl_map *isl_basic_map_lexmin(
2769 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2770 struct isl_set **empty)
2772 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2775 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2777 struct isl_basic_map *bmap = NULL;
2778 struct isl_basic_set *dom = NULL;
2779 struct isl_map *min;
2783 bmap = isl_basic_map_from_basic_set(bset, 0, bset->dim);
2786 dom = isl_basic_set_alloc(bmap->ctx, bmap->nparam, 0, 0, 0, 0);
2789 min = isl_basic_map_lexmin(bmap, dom, NULL);
2790 return isl_map_range(min);
2792 isl_basic_map_free(bmap);
2796 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2800 if (bmap->n_div == 0)
2801 return isl_map_from_basic_map(bmap);
2802 return isl_pip_basic_map_compute_divs(bmap);
2805 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2808 struct isl_map *res;
2814 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2815 for (i = 1 ; i < map->n; ++i) {
2817 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2818 if (F_ISSET(map, ISL_MAP_DISJOINT))
2819 res = isl_map_union_disjoint(res, r2);
2821 res = isl_map_union(res, r2);
2828 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2830 return (struct isl_set *)
2831 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2834 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2836 return (struct isl_set *)
2837 isl_map_compute_divs((struct isl_map *)set);
2840 struct isl_set *isl_map_domain(struct isl_map *map)
2843 struct isl_set *set;
2848 map = isl_map_cow(map);
2852 set = (struct isl_set *)map;
2853 set->dim = map->n_in;
2855 for (i = 0; i < map->n; ++i) {
2856 set->p[i] = isl_basic_map_domain(map->p[i]);
2860 F_CLR(set, ISL_MAP_DISJOINT);
2861 F_CLR(set, ISL_SET_NORMALIZED);
2868 struct isl_map *isl_map_union_disjoint(
2869 struct isl_map *map1, struct isl_map *map2)
2873 struct isl_map *map = NULL;
2887 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
2888 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
2889 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
2891 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2892 F_ISSET(map2, ISL_MAP_DISJOINT))
2893 FL_SET(flags, ISL_MAP_DISJOINT);
2895 map = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
2896 map1->n + map2->n, flags);
2899 for (i = 0; i < map1->n; ++i) {
2900 map = isl_map_add(map,
2901 isl_basic_map_copy(map1->p[i]));
2905 for (i = 0; i < map2->n; ++i) {
2906 map = isl_map_add(map,
2907 isl_basic_map_copy(map2->p[i]));
2921 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
2923 map1 = isl_map_union_disjoint(map1, map2);
2927 F_CLR(map1, ISL_MAP_DISJOINT);
2931 struct isl_set *isl_set_union_disjoint(
2932 struct isl_set *set1, struct isl_set *set2)
2934 return (struct isl_set *)
2935 isl_map_union_disjoint(
2936 (struct isl_map *)set1, (struct isl_map *)set2);
2939 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
2941 return (struct isl_set *)
2942 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
2945 struct isl_map *isl_map_intersect_range(
2946 struct isl_map *map, struct isl_set *set)
2949 struct isl_map *result;
2955 if (F_ISSET(map, ISL_MAP_DISJOINT) &&
2956 F_ISSET(set, ISL_MAP_DISJOINT))
2957 FL_SET(flags, ISL_MAP_DISJOINT);
2959 result = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
2960 map->n * set->n, flags);
2963 for (i = 0; i < map->n; ++i)
2964 for (j = 0; j < set->n; ++j) {
2965 result = isl_map_add(result,
2966 isl_basic_map_intersect_range(
2967 isl_basic_map_copy(map->p[i]),
2968 isl_basic_set_copy(set->p[j])));
2981 struct isl_map *isl_map_intersect_domain(
2982 struct isl_map *map, struct isl_set *set)
2984 return isl_map_reverse(
2985 isl_map_intersect_range(isl_map_reverse(map), set));
2988 struct isl_map *isl_map_apply_domain(
2989 struct isl_map *map1, struct isl_map *map2)
2993 map1 = isl_map_reverse(map1);
2994 map1 = isl_map_apply_range(map1, map2);
2995 return isl_map_reverse(map1);
3002 struct isl_map *isl_map_apply_range(
3003 struct isl_map *map1, struct isl_map *map2)
3005 struct isl_map *result;
3011 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3012 isl_assert(map1->ctx, map1->n_out == map2->n_in, goto error);
3014 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map2->n_out,
3015 map1->n * map2->n, 0);
3018 for (i = 0; i < map1->n; ++i)
3019 for (j = 0; j < map2->n; ++j) {
3020 result = isl_map_add(result,
3021 isl_basic_map_apply_range(
3022 isl_basic_map_copy(map1->p[i]),
3023 isl_basic_map_copy(map2->p[j])));
3029 if (result && result->n <= 1)
3030 F_SET(result, ISL_MAP_DISJOINT);
3039 * returns range - domain
3041 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3043 struct isl_basic_set *bset;
3049 isl_assert(bmap->ctx, bmap->n_in == bmap->n_out, goto error);
3051 bset = isl_basic_set_from_basic_map(bmap);
3052 bset = isl_basic_set_extend(bset, bset->nparam, 3*dim, 0,
3054 bset = isl_basic_set_swap_vars(bset, 2*dim);
3055 for (i = 0; i < dim; ++i) {
3056 int j = isl_basic_map_alloc_equality(
3057 (struct isl_basic_map *)bset);
3060 isl_seq_clr(bset->eq[j],
3061 1 + bset->nparam + bset->dim + bset->extra);
3062 isl_int_set_si(bset->eq[j][1+bset->nparam+i], 1);
3063 isl_int_set_si(bset->eq[j][1+bset->nparam+dim+i], 1);
3064 isl_int_set_si(bset->eq[j][1+bset->nparam+2*dim+i], -1);
3066 return isl_basic_set_project_out(bset, 2*dim, 0);
3068 isl_basic_map_free(bmap);
3073 * returns range - domain
3075 struct isl_set *isl_map_deltas(struct isl_map *map)
3078 struct isl_set *result;
3083 isl_assert(map->ctx, map->n_in == map->n_out, goto error);
3084 result = isl_set_alloc(map->ctx, map->nparam, map->n_in, map->n, map->flags);
3087 for (i = 0; i < map->n; ++i)
3088 result = isl_set_add(result,
3089 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3097 /* If the only constraints a div d=floor(f/m)
3098 * appears in are its two defining constraints
3101 * -(f - (m - 1)) + m d >= 0
3103 * then it can safely be removed.
3105 static int div_is_redundant(struct isl_basic_map *bmap, int div)
3108 unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
3110 for (i = 0; i < bmap->n_eq; ++i)
3111 if (!isl_int_is_zero(bmap->eq[i][pos]))
3114 for (i = 0; i < bmap->n_ineq; ++i) {
3115 if (isl_int_is_zero(bmap->ineq[i][pos]))
3117 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
3119 isl_int_sub(bmap->div[div][1],
3120 bmap->div[div][1], bmap->div[div][0]);
3121 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
3122 neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
3123 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
3124 isl_int_add(bmap->div[div][1],
3125 bmap->div[div][1], bmap->div[div][0]);
3128 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
3129 bmap->n_div-div-1) != -1)
3131 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
3132 if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
3134 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
3135 bmap->n_div-div-1) != -1)
3141 for (i = 0; i < bmap->n_div; ++i)
3142 if (!isl_int_is_zero(bmap->div[i][1+pos]))
3149 * Remove divs that don't occur in any of the constraints or other divs.
3150 * These can arise when dropping some of the variables in a quast
3151 * returned by piplib.
3153 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
3160 for (i = bmap->n_div-1; i >= 0; --i) {
3161 if (!div_is_redundant(bmap, i))
3163 bmap = isl_basic_map_drop_div(bmap, i);
3168 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
3170 bmap = remove_redundant_divs(bmap);
3173 F_SET(bmap, ISL_BASIC_SET_FINAL);
3177 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
3179 return (struct isl_basic_set *)
3180 isl_basic_map_finalize((struct isl_basic_map *)bset);
3183 struct isl_set *isl_set_finalize(struct isl_set *set)
3189 for (i = 0; i < set->n; ++i) {
3190 set->p[i] = isl_basic_set_finalize(set->p[i]);
3200 struct isl_map *isl_map_finalize(struct isl_map *map)
3206 for (i = 0; i < map->n; ++i) {
3207 map->p[i] = isl_basic_map_finalize(map->p[i]);
3211 F_CLR(map, ISL_MAP_NORMALIZED);
3218 struct isl_basic_map *isl_basic_map_identity(struct isl_ctx *ctx,
3219 unsigned nparam, unsigned dim)
3221 struct isl_basic_map *bmap;
3224 bmap = isl_basic_map_alloc(ctx, nparam, dim, dim, 0, dim, 0);
3228 for (i = 0; i < dim; ++i) {
3229 int j = isl_basic_map_alloc_equality(bmap);
3232 isl_seq_clr(bmap->eq[j],
3233 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
3234 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3235 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3237 return isl_basic_map_finalize(bmap);
3239 isl_basic_map_free(bmap);
3243 struct isl_map *isl_map_identity(struct isl_ctx *ctx,
3244 unsigned nparam, unsigned dim)
3246 struct isl_map *map = isl_map_alloc(ctx, nparam, dim, dim, 1,
3250 map = isl_map_add(map,
3251 isl_basic_map_identity(ctx, nparam, dim));
3258 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3260 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3263 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3265 return isl_map_is_subset(
3266 (struct isl_map *)set1, (struct isl_map *)set2);
3269 int isl_basic_map_is_subset(
3270 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3273 struct isl_map *map1;
3274 struct isl_map *map2;
3276 if (!bmap1 || !bmap2)
3279 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3280 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3282 is_subset = isl_map_is_subset(map1, map2);
3290 int isl_basic_map_is_equal(
3291 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3295 if (!bmap1 || !bmap2)
3297 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3300 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3304 int isl_basic_set_is_equal(
3305 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3307 return isl_basic_map_is_equal(
3308 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3311 int isl_map_is_empty(struct isl_map *map)
3318 for (i = 0; i < map->n; ++i) {
3319 is_empty = isl_basic_map_is_empty(map->p[i]);
3328 int isl_set_is_empty(struct isl_set *set)
3330 return isl_map_is_empty((struct isl_map *)set);
3333 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3337 struct isl_map *diff;
3342 if (isl_map_is_empty(map1))
3345 if (isl_map_is_empty(map2))
3348 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3352 is_subset = isl_map_is_empty(diff);
3358 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3364 is_subset = isl_map_is_subset(map1, map2);
3367 is_subset = isl_map_is_subset(map2, map1);
3371 int isl_basic_map_is_strict_subset(
3372 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3376 if (!bmap1 || !bmap2)
3378 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3381 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3382 if (is_subset == -1)
3387 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3393 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3394 if (total != vec->size)
3399 for (i = 0; i < bmap->n_eq; ++i) {
3400 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3401 if (!isl_int_is_zero(s)) {
3407 for (i = 0; i < bmap->n_ineq; ++i) {
3408 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3409 if (isl_int_is_neg(s)) {
3420 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3424 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3427 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3429 struct isl_basic_set *bset = NULL;
3430 struct isl_vec *sample = NULL;
3437 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3440 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3441 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3442 copy = isl_basic_map_convex_hull(copy);
3443 empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3444 isl_basic_map_free(copy);
3448 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3449 if (bmap->sample && bmap->sample->size == total) {
3450 int contains = basic_map_contains(bmap, bmap->sample);
3456 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3459 sample = isl_basic_set_sample(bset);
3462 empty = sample->size == 0;
3464 isl_vec_free(bmap->ctx, bmap->sample);
3465 bmap->sample = sample;
3470 struct isl_map *isl_basic_map_union(
3471 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3473 struct isl_map *map;
3474 if (!bmap1 || !bmap2)
3477 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
3478 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
3479 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
3481 map = isl_map_alloc(bmap1->ctx, bmap1->nparam,
3482 bmap1->n_in, bmap1->n_out, 2, 0);
3485 map = isl_map_add(map, bmap1);
3486 map = isl_map_add(map, bmap2);
3489 isl_basic_map_free(bmap1);
3490 isl_basic_map_free(bmap2);
3494 struct isl_set *isl_basic_set_union(
3495 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3497 return (struct isl_set *)isl_basic_map_union(
3498 (struct isl_basic_map *)bset1,
3499 (struct isl_basic_map *)bset2);
3502 /* Order divs such that any div only depends on previous divs */
3503 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3506 unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
3508 for (i = 0; i < bmap->n_div; ++i) {
3510 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3514 swap_div(bmap, i, pos);
3520 static int find_div(struct isl_basic_map *dst,
3521 struct isl_basic_map *src, unsigned div)
3525 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3527 for (i = 0; i < dst->n_div; ++i)
3528 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total) &&
3529 isl_seq_first_non_zero(dst->div[i]+1+1+total,
3530 dst->n_div - src->n_div) == -1)
3535 struct isl_basic_map *isl_basic_map_align_divs(
3536 struct isl_basic_map *dst, struct isl_basic_map *src)
3539 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3541 if (src->n_div == 0)
3544 src = order_divs(src);
3545 dst = isl_basic_map_extend(dst, dst->nparam, dst->n_in,
3546 dst->n_out, src->n_div, 0, 2 * src->n_div);
3549 for (i = 0; i < src->n_div; ++i) {
3550 int j = find_div(dst, src, i);
3552 j = isl_basic_map_alloc_div(dst);
3555 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
3556 isl_seq_clr(dst->div[j]+1+1+total,
3557 dst->extra - src->n_div);
3558 if (add_div_constraints(dst, j) < 0)
3562 swap_div(dst, i, j);
3566 isl_basic_map_free(dst);
3570 struct isl_map *isl_map_align_divs(struct isl_map *map)
3574 map = isl_map_compute_divs(map);
3575 map = isl_map_cow(map);
3579 for (i = 1; i < map->n; ++i)
3580 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3581 for (i = 1; i < map->n; ++i)
3582 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3584 F_CLR(map, ISL_MAP_NORMALIZED);
3588 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3589 struct isl_basic_map *src, isl_int *c,
3590 unsigned len, int oppose)
3592 struct isl_basic_map *copy = NULL;
3597 copy = isl_basic_map_copy(src);
3598 copy = isl_basic_map_cow(copy);
3601 copy = isl_basic_map_extend(copy,
3602 copy->nparam, copy->n_in, copy->n_out, 0, 0, 1);
3603 k = isl_basic_map_alloc_inequality(copy);
3607 isl_seq_neg(copy->ineq[k], c, len);
3609 isl_seq_cpy(copy->ineq[k], c, len);
3610 total = 1 + copy->nparam + copy->n_in + copy->n_out + copy->extra;
3611 isl_seq_clr(copy->ineq[k]+len, total - len);
3612 isl_inequality_negate(copy, k);
3613 copy = isl_basic_map_simplify(copy);
3614 copy = isl_basic_map_finalize(copy);
3615 is_empty = isl_basic_map_is_empty(copy);
3619 dst = isl_map_add(dst, copy);
3621 isl_basic_map_free(copy);
3624 isl_basic_map_free(copy);
3629 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3633 struct isl_map *rest = NULL;
3635 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3642 if (F_ISSET(map, ISL_MAP_DISJOINT))
3643 FL_SET(flags, ISL_MAP_DISJOINT);
3645 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3646 rest = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
3651 for (i = 0; i < map->n; ++i) {
3652 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3657 for (j = 0; j < map->n; ++j)
3658 map->p[j] = isl_basic_map_cow(map->p[j]);
3660 for (i = 0; i < bmap->n_eq; ++i) {
3661 for (j = 0; j < map->n; ++j) {
3662 rest = add_cut_constraint(rest,
3663 map->p[j], bmap->eq[i], 1+total, 0);
3667 rest = add_cut_constraint(rest,
3668 map->p[j], bmap->eq[i], 1+total, 1);
3672 map->p[j] = isl_basic_map_extend(map->p[j],
3673 map->p[j]->nparam, map->p[j]->n_in,
3674 map->p[j]->n_out, 0, 1, 0);
3677 k = isl_basic_map_alloc_equality(map->p[j]);
3680 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3681 isl_seq_clr(map->p[j]->eq[k]+1+total,
3682 map->p[j]->extra - bmap->n_div);
3686 for (i = 0; i < bmap->n_ineq; ++i) {
3687 for (j = 0; j < map->n; ++j) {
3688 rest = add_cut_constraint(rest,
3689 map->p[j], bmap->ineq[i], 1+total, 0);
3693 map->p[j] = isl_basic_map_extend(map->p[j],
3694 map->p[j]->nparam, map->p[j]->n_in,
3695 map->p[j]->n_out, 0, 0, 1);
3698 k = isl_basic_map_alloc_inequality(map->p[j]);
3701 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3702 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3703 map->p[j]->extra - bmap->n_div);
3715 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3721 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3722 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
3723 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
3725 if (isl_map_is_empty(map2)) {
3730 map1 = isl_map_compute_divs(map1);
3731 map2 = isl_map_compute_divs(map2);
3735 for (i = 0; map1 && i < map2->n; ++i)
3736 map1 = subtract(map1, map2->p[i]);
3746 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3748 return (struct isl_set *)
3750 (struct isl_map *)set1, (struct isl_map *)set2);
3753 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3757 isl_assert(set->ctx, set->dim == map->n_in, goto error);
3758 map = isl_map_intersect_domain(map, set);
3759 set = isl_map_range(map);
3767 /* There is no need to cow as removing empty parts doesn't change
3768 * the meaning of the set.
3770 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3777 for (i = map->n-1; i >= 0; --i) {
3778 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3780 isl_basic_map_free(map->p[i]);
3781 if (i != map->n-1) {
3782 F_CLR(map, ISL_MAP_NORMALIZED);
3783 map->p[i] = map->p[map->n-1];
3791 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3793 return (struct isl_set *)
3794 isl_map_remove_empty_parts((struct isl_map *)set);
3797 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3799 struct isl_basic_set *bset;
3800 if (!set || set->n == 0)
3802 bset = set->p[set->n-1];
3803 isl_assert(set->ctx, F_ISSET(bset, ISL_BASIC_SET_FINAL), return NULL);
3804 return isl_basic_set_copy(bset);
3807 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3808 struct isl_basic_set *bset)
3814 for (i = set->n-1; i >= 0; --i) {
3815 if (set->p[i] != bset)
3817 set = isl_set_cow(set);
3820 isl_basic_set_free(set->p[i]);
3821 if (i != set->n-1) {
3822 F_CLR(set, ISL_SET_NORMALIZED);
3823 set->p[i] = set->p[set->n-1];
3828 isl_basic_set_free(bset);
3832 isl_basic_set_free(bset);
3836 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3837 * for any common value of the parameters and dimensions preceding dim
3838 * in both basic sets, the values of dimension pos in bset1 are
3839 * smaller or larger then those in bset2.
3842 * 1 if bset1 follows bset2
3843 * -1 if bset1 precedes bset2
3844 * 0 if bset1 and bset2 are incomparable
3845 * -2 if some error occurred.
3847 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3848 struct isl_basic_set *bset2, int pos)
3850 struct isl_basic_map *bmap1 = NULL;
3851 struct isl_basic_map *bmap2 = NULL;
3852 struct isl_ctx *ctx;
3853 struct isl_vec *obj;
3856 enum isl_lp_result res;
3859 if (!bset1 || !bset2)
3862 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1),
3863 pos, bset1->dim - pos);
3864 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2),
3865 pos, bset2->dim - pos);
3866 if (!bmap1 || !bmap2)
3868 bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
3869 bmap1->n_in, bmap1->n_out + bmap2->n_out,
3870 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3873 total = bmap1->nparam + bmap1->n_in + bmap1->n_out + bmap1->n_div;
3875 obj = isl_vec_alloc(ctx, total);
3876 isl_seq_clr(obj->block.data, total);
3877 isl_int_set_si(obj->block.data[bmap1->nparam+bmap1->n_in], 1);
3878 isl_int_set_si(obj->block.data[bmap1->nparam+bmap1->n_in+
3879 bmap1->n_out-bmap2->n_out], -1);
3882 bmap1 = add_constraints(bmap1, bmap2, 0, bmap1->n_out - bmap2->n_out);
3885 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3886 if (res == isl_lp_empty)
3888 else if (res == isl_lp_ok && isl_int_is_pos(num))
3890 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3891 res == isl_lp_unbounded)
3897 isl_basic_map_free(bmap1);
3898 isl_vec_free(ctx, obj);
3901 isl_basic_map_free(bmap1);
3902 isl_basic_map_free(bmap2);
3906 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
3907 unsigned pos, isl_int *val)
3915 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3916 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
3917 for (; d+1 > pos; --d)
3918 if (!isl_int_is_zero(bmap->eq[i][1+d]))
3922 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
3924 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
3926 if (!isl_int_is_one(bmap->eq[i][1+d]))
3929 isl_int_neg(*val, bmap->eq[i][0]);
3935 static int isl_map_fast_has_fixed_var(struct isl_map *map,
3936 unsigned pos, isl_int *val)
3948 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
3951 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
3952 for (i = 1; fixed == 1 && i < map->n; ++i) {
3953 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
3954 if (fixed == 1 && isl_int_ne(tmp, v))
3958 isl_int_set(*val, v);
3964 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
3967 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
3970 /* Check if dimension dim has fixed value and if so and if val is not NULL,
3971 * then return this fixed value in *val.
3973 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
3975 return isl_set_fast_has_fixed_var(set, set->nparam + dim, val);
3978 /* Check if input variable in has fixed value and if so and if val is not NULL,
3979 * then return this fixed value in *val.
3981 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
3983 return isl_map_fast_has_fixed_var(map, map->nparam + in, val);
3986 /* Check if dimension dim has an (obvious) fixed lower bound and if so
3987 * and if val is not NULL, then return this lower bound in *val.
3989 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
3990 unsigned dim, isl_int *val)
3992 int i, i_eq = -1, i_ineq = -1;
3998 total = bset->nparam + bset->dim + bset->n_div;
3999 for (i = 0; i < bset->n_eq; ++i) {
4000 if (isl_int_is_zero(bset->eq[i][1+bset->nparam+dim]))
4006 for (i = 0; i < bset->n_ineq; ++i) {
4007 if (!isl_int_is_pos(bset->ineq[i][1+bset->nparam+dim]))
4009 if (i_eq != -1 || i_ineq != -1)
4013 if (i_eq == -1 && i_ineq == -1)
4015 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4016 /* The coefficient should always be one due to normalization. */
4017 if (!isl_int_is_one(c[1+bset->nparam+dim]))
4019 if (isl_seq_first_non_zero(c+1, bset->nparam+dim) != -1)
4021 if (isl_seq_first_non_zero(c+1+bset->nparam+dim+1,
4022 total - bset->nparam - dim - 1) != -1)
4025 isl_int_neg(*val, c[0]);
4029 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4030 unsigned dim, isl_int *val)
4042 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4046 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4048 for (i = 1; fixed == 1 && i < set->n; ++i) {
4049 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4051 if (fixed == 1 && isl_int_ne(tmp, v))
4055 isl_int_set(*val, v);
4061 /* Remove all information from bset that is redundant in the context
4062 * of context. In particular, equalities that are linear combinations
4063 * of those in context are remobed. Then the inequalities that are
4064 * redundant in the context of the equalities and inequalities of
4065 * context are removed.
4067 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
4068 struct isl_basic_set *context)
4072 struct isl_basic_set *combined;
4073 struct isl_ctx *ctx;
4074 enum isl_lp_result res = isl_lp_ok;
4076 if (!bset || !context)
4079 if (context->n_eq > 0) {
4082 struct isl_ctx *ctx = context->ctx;
4083 context = isl_basic_set_remove_equalities(context, &T, &T2);
4086 bset = isl_basic_set_preimage(ctx, bset, T);
4087 bset = uset_gist(bset, context);
4088 bset = isl_basic_set_preimage(ctx, bset, T2);
4091 combined = isl_basic_set_extend(isl_basic_set_copy(bset),
4092 0, bset->dim, 0, context->n_eq, context->n_ineq);
4093 context = set_add_constraints(combined, context, 0);
4098 for (i = bset->n_ineq-1; i >= 0; --i) {
4099 set_swap_inequality(context, i, context->n_ineq-1);
4101 res = isl_solve_lp((struct isl_basic_map *)context, 0,
4102 context->ineq[context->n_ineq]+1, ctx->one, &opt, NULL);
4104 set_swap_inequality(context, i, context->n_ineq-1);
4105 if (res == isl_lp_unbounded)
4107 if (res == isl_lp_error)
4109 if (res == isl_lp_empty) {
4110 bset = isl_basic_set_set_to_empty(bset);
4113 isl_int_add(opt, opt, context->ineq[i][0]);
4114 if (!isl_int_is_neg(opt)) {
4115 isl_basic_set_drop_inequality(context, i);
4116 isl_basic_set_drop_inequality(bset, i);
4120 if (res == isl_lp_error)
4122 isl_basic_set_free(context);
4125 isl_basic_set_free(context);
4126 isl_basic_set_free(bset);
4130 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
4131 struct isl_basic_map *context)
4133 struct isl_basic_set *bset;
4135 if (!bmap || !context)
4138 context = isl_basic_map_align_divs(context, bmap);
4139 bmap = isl_basic_map_align_divs(bmap, context);
4141 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
4142 isl_basic_map_underlying_set(context));
4144 return isl_basic_map_overlying_set(bset, bmap);
4146 isl_basic_map_free(bmap);
4147 isl_basic_map_free(context);
4151 struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
4155 map = isl_map_cow(map);
4156 if (!map || !context)
4158 isl_assert(map->ctx, map->nparam == context->nparam, goto error);
4159 isl_assert(map->ctx, map->n_in == context->n_in, goto error);
4160 isl_assert(map->ctx, map->n_out == context->n_out, goto error);
4161 for (i = 0; i < map->n; ++i)
4162 context = isl_basic_map_align_divs(context, map->p[i]);
4163 for (i = 0; i < map->n; ++i) {
4164 map->p[i] = isl_basic_map_gist(map->p[i],
4165 isl_basic_map_copy(context));
4169 isl_basic_map_free(context);
4170 F_CLR(map, ISL_MAP_NORMALIZED);
4174 isl_basic_map_free(context);
4178 struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
4180 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
4181 (struct isl_basic_map *)context);
4189 static int qsort_constraint_cmp(const void *p1, const void *p2)
4191 const struct constraint *c1 = (const struct constraint *)p1;
4192 const struct constraint *c2 = (const struct constraint *)p2;
4193 unsigned size = isl_min(c1->size, c2->size);
4194 return isl_seq_cmp(c1->c, c2->c, size);
4197 static struct isl_basic_map *isl_basic_map_sort_constraints(
4198 struct isl_basic_map *bmap)
4201 struct constraint *c;
4206 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
4207 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4210 for (i = 0; i < bmap->n_ineq; ++i) {
4212 c[i].c = bmap->ineq[i];
4214 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4215 for (i = 0; i < bmap->n_ineq; ++i)
4216 bmap->ineq[i] = c[i].c;
4220 isl_basic_map_free(bmap);
4224 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4228 if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4230 bmap = isl_basic_map_convex_hull(bmap);
4231 bmap = isl_basic_map_sort_constraints(bmap);
4232 F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4236 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4237 const struct isl_basic_map *bmap2)
4244 if (bmap1->nparam != bmap2->nparam)
4245 return bmap1->nparam - bmap2->nparam;
4246 if (bmap1->n_in != bmap2->n_in)
4247 return bmap1->n_in - bmap2->n_in;
4248 if (bmap1->n_out != bmap2->n_out)
4249 return bmap1->n_out - bmap2->n_out;
4250 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4251 F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4253 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4255 if (F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4257 if (bmap1->n_eq != bmap2->n_eq)
4258 return bmap1->n_eq - bmap2->n_eq;
4259 if (bmap1->n_ineq != bmap2->n_ineq)
4260 return bmap1->n_ineq - bmap2->n_ineq;
4261 if (bmap1->n_div != bmap2->n_div)
4262 return bmap1->n_div - bmap2->n_div;
4263 total = bmap1->nparam + bmap1->n_in + bmap1->n_out + bmap1->n_div;
4264 for (i = 0; i < bmap1->n_eq; ++i) {
4265 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4269 for (i = 0; i < bmap1->n_ineq; ++i) {
4270 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4274 for (i = 0; i < bmap1->n_div; ++i) {
4275 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4282 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4283 struct isl_basic_map *bmap2)
4285 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4288 static int qsort_bmap_cmp(const void *p1, const void *p2)
4290 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4291 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4293 return isl_basic_map_fast_cmp(bmap1, bmap2);
4296 /* We normalize in place, but if anything goes wrong we need
4297 * to return NULL, so we need to make sure we don't change the
4298 * meaning of any possible other copies of map.
4300 struct isl_map *isl_map_normalize(struct isl_map *map)
4303 struct isl_basic_map *bmap;
4307 if (F_ISSET(map, ISL_MAP_NORMALIZED))
4309 for (i = 0; i < map->n; ++i) {
4310 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4313 isl_basic_map_free(map->p[i]);
4316 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4317 F_SET(map, ISL_MAP_NORMALIZED);
4318 map = isl_map_remove_empty_parts(map);
4321 for (i = map->n - 1; i >= 1; --i) {
4322 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4324 isl_basic_map_free(map->p[i-1]);
4325 for (j = i; j < map->n; ++j)
4326 map->p[j-1] = map->p[j];
4336 struct isl_set *isl_set_normalize(struct isl_set *set)
4338 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4341 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4351 if (map1->nparam != map2->nparam)
4353 if (map1->n_in != map2->n_in)
4355 if (map1->n_out != map2->n_out)
4358 map1 = isl_map_copy(map1);
4359 map2 = isl_map_copy(map2);
4360 map1 = isl_map_normalize(map1);
4361 map2 = isl_map_normalize(map2);
4364 equal = map1->n == map2->n;
4365 for (i = 0; equal && i < map1->n; ++i) {
4366 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4379 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4381 return isl_map_fast_is_equal((struct isl_map *)set1,
4382 (struct isl_map *)set2);