5 #include "isl_equalities.h"
11 #include "isl_map_private.h"
12 #include "isl_map_piplib.h"
13 #include "isl_sample.h"
16 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
17 struct isl_basic_map *bmap,
18 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
19 unsigned n_eq, unsigned n_ineq)
22 size_t row_size = 1 + nparam + n_in + n_out + extra;
24 bmap->block = isl_blk_alloc(ctx, (n_eq + n_ineq) * row_size);
25 if (isl_blk_is_error(bmap->block)) {
30 bmap->eq = isl_alloc_array(ctx, isl_int *, n_eq + n_ineq);
32 isl_blk_free(ctx, bmap->block);
38 bmap->block2 = isl_blk_empty();
41 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
42 if (isl_blk_is_error(bmap->block2)) {
44 isl_blk_free(ctx, bmap->block);
49 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
51 isl_blk_free(ctx, bmap->block2);
53 isl_blk_free(ctx, bmap->block);
59 for (i = 0; i < n_eq + n_ineq; ++i)
60 bmap->eq[i] = bmap->block.data + i * row_size;
62 for (i = 0; i < extra; ++i)
63 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
69 bmap->c_size = n_eq + n_ineq;
70 bmap->ineq = bmap->eq + n_eq;
71 bmap->nparam = nparam;
83 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
84 unsigned nparam, unsigned dim, unsigned extra,
85 unsigned n_eq, unsigned n_ineq)
87 struct isl_basic_map *bmap;
88 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
89 return (struct isl_basic_set *)bmap;
92 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
93 unsigned nparam, unsigned in, unsigned out, unsigned extra,
94 unsigned n_eq, unsigned n_ineq)
96 struct isl_basic_map *bmap;
98 bmap = isl_alloc_type(ctx, struct isl_basic_map);
102 return basic_map_init(ctx, bmap,
103 nparam, in, out, extra, n_eq, n_ineq);
106 static void dup_constraints(
107 struct isl_basic_map *dst, struct isl_basic_map *src)
110 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
112 for (i = 0; i < src->n_eq; ++i) {
113 int j = isl_basic_map_alloc_equality(dst);
114 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
117 for (i = 0; i < src->n_ineq; ++i) {
118 int j = isl_basic_map_alloc_inequality(dst);
119 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
122 for (i = 0; i < src->n_div; ++i) {
123 int j = isl_basic_map_alloc_div(dst);
124 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
126 F_SET(dst, ISL_BASIC_SET_FINAL);
129 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
131 struct isl_basic_map *dup;
135 dup = isl_basic_map_alloc(bmap->ctx, bmap->nparam,
136 bmap->n_in, bmap->n_out,
137 bmap->n_div, bmap->n_eq, bmap->n_ineq);
140 dup->flags = bmap->flags;
141 dup_constraints(dup, bmap);
142 dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
146 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
148 struct isl_basic_map *dup;
150 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
151 return (struct isl_basic_set *)dup;
154 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
159 if (F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
163 return isl_basic_set_dup(bset);
166 struct isl_set *isl_set_copy(struct isl_set *set)
175 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
180 if (F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
184 return isl_basic_map_dup(bmap);
187 struct isl_map *isl_map_copy(struct isl_map *map)
196 void isl_basic_map_free(struct isl_basic_map *bmap)
204 isl_ctx_deref(bmap->ctx);
206 isl_blk_free(bmap->ctx, bmap->block2);
208 isl_blk_free(bmap->ctx, bmap->block);
209 isl_vec_free(bmap->ctx, bmap->sample);
213 void isl_basic_set_free(struct isl_basic_set *bset)
215 isl_basic_map_free((struct isl_basic_map *)bset);
218 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
224 isl_assert(ctx, bmap->n_eq + bmap->n_ineq < bmap->c_size, return -1);
225 isl_assert(ctx, bmap->eq + bmap->n_eq <= bmap->ineq, return -1);
226 if (bmap->eq + bmap->n_eq == bmap->ineq) {
228 int j = isl_basic_map_alloc_inequality(bmap);
232 bmap->ineq[0] = bmap->ineq[j];
237 isl_seq_clr(bmap->eq[bmap->n_eq] +
238 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div,
239 bmap->extra - bmap->n_div);
243 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
245 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
248 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
252 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
257 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
262 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
264 if (pos != bmap->n_eq - 1) {
266 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
267 bmap->eq[bmap->n_eq - 1] = t;
273 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
275 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
278 void isl_basic_map_inequality_to_equality(
279 struct isl_basic_map *bmap, unsigned pos)
284 bmap->ineq[pos] = bmap->ineq[0];
285 bmap->ineq[0] = bmap->eq[bmap->n_eq];
286 bmap->eq[bmap->n_eq] = t;
290 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
293 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
299 isl_assert(ctx, (bmap->ineq - bmap->eq) + bmap->n_ineq < bmap->c_size,
301 F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
302 F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
303 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
304 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
305 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div,
306 bmap->extra - bmap->n_div);
307 return bmap->n_ineq++;
310 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
312 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
315 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
319 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
324 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
326 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
329 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
334 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
336 if (pos != bmap->n_ineq - 1) {
338 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
339 bmap->ineq[bmap->n_ineq - 1] = t;
340 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
346 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
348 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
351 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
355 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
356 isl_seq_clr(bmap->div[bmap->n_div] +
357 1 + 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div,
358 bmap->extra - bmap->n_div);
359 return bmap->n_div++;
362 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
366 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
371 /* Copy constraint from src to dst, putting the vars of src at offset
372 * dim_off in dst and the divs of src at offset div_off in dst.
373 * If both sets are actually map, then dim_off applies to the input
376 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
377 struct isl_basic_map *src_map, isl_int *src,
378 unsigned in_off, unsigned out_off, unsigned div_off)
380 isl_int_set(dst[0], src[0]);
381 isl_seq_cpy(dst+1, src+1, isl_min(dst_map->nparam, src_map->nparam));
382 if (dst_map->nparam > src_map->nparam)
383 isl_seq_clr(dst+1+src_map->nparam,
384 dst_map->nparam - src_map->nparam);
385 isl_seq_clr(dst+1+dst_map->nparam, in_off);
386 isl_seq_cpy(dst+1+dst_map->nparam+in_off,
387 src+1+src_map->nparam,
388 isl_min(dst_map->n_in-in_off, src_map->n_in));
389 if (dst_map->n_in-in_off > src_map->n_in)
390 isl_seq_clr(dst+1+dst_map->nparam+in_off+src_map->n_in,
391 dst_map->n_in - in_off - src_map->n_in);
392 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in, out_off);
393 isl_seq_cpy(dst+1+dst_map->nparam+dst_map->n_in+out_off,
394 src+1+src_map->nparam+src_map->n_in,
395 isl_min(dst_map->n_out-out_off, src_map->n_out));
396 if (dst_map->n_out-out_off > src_map->n_out)
397 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+out_off+src_map->n_out,
398 dst_map->n_out - out_off - src_map->n_out);
399 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out, div_off);
400 isl_seq_cpy(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out+div_off,
401 src+1+src_map->nparam+src_map->n_in+src_map->n_out,
402 isl_min(dst_map->extra-div_off, src_map->n_div));
403 if (dst_map->n_div-div_off > src_map->n_div)
404 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out+
405 div_off+src_map->n_div,
406 dst_map->n_div - div_off - src_map->n_div);
409 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
410 struct isl_basic_map *src_map, isl_int *src,
411 unsigned in_off, unsigned out_off, unsigned div_off)
413 isl_int_set(dst[0], src[0]);
414 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
417 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
418 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
423 if (!bmap1 || !bmap2)
426 div_off = bmap1->n_div;
428 for (i = 0; i < bmap2->n_eq; ++i) {
429 int i1 = isl_basic_map_alloc_equality(bmap1);
432 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
433 i_pos, o_pos, div_off);
436 for (i = 0; i < bmap2->n_ineq; ++i) {
437 int i1 = isl_basic_map_alloc_inequality(bmap1);
440 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
441 i_pos, o_pos, div_off);
444 for (i = 0; i < bmap2->n_div; ++i) {
445 int i1 = isl_basic_map_alloc_div(bmap1);
448 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
449 i_pos, o_pos, div_off);
452 isl_basic_map_free(bmap2);
457 isl_basic_map_free(bmap1);
458 isl_basic_map_free(bmap2);
462 static struct isl_basic_set *set_add_constraints(struct isl_basic_set *bset1,
463 struct isl_basic_set *bset2, unsigned pos)
465 return (struct isl_basic_set *)
466 add_constraints((struct isl_basic_map *)bset1,
467 (struct isl_basic_map *)bset2, 0, pos);
470 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
471 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
472 unsigned n_eq, unsigned n_ineq)
474 struct isl_basic_map *ext;
478 base = isl_basic_map_cow(base);
482 dims_ok = base && base->nparam == nparam &&
483 base->n_in == n_in && base->n_out == n_out &&
484 base->extra >= base->n_div + extra;
486 if (dims_ok && n_eq == 0 && n_ineq == 0)
489 isl_assert(base->ctx, base->nparam <= nparam, goto error);
490 isl_assert(base->ctx, base->n_in <= n_in, goto error);
491 isl_assert(base->ctx, base->n_out <= n_out, goto error);
492 extra += base->extra;
494 n_ineq += base->n_ineq;
496 ext = isl_basic_map_alloc(base->ctx, nparam, n_in, n_out,
497 extra, n_eq, n_ineq);
504 ext = add_constraints(ext, base, 0, 0);
507 F_CLR(ext, ISL_BASIC_SET_FINAL);
513 isl_basic_map_free(base);
517 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
518 unsigned nparam, unsigned dim, unsigned extra,
519 unsigned n_eq, unsigned n_ineq)
521 return (struct isl_basic_set *)
522 isl_basic_map_extend((struct isl_basic_map *)base,
523 nparam, 0, dim, extra, n_eq, n_ineq);
526 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
528 return (struct isl_basic_set *)
529 isl_basic_map_cow((struct isl_basic_map *)bset);
532 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
539 bmap = isl_basic_map_dup(bmap);
541 F_CLR(bmap, ISL_BASIC_SET_FINAL);
545 struct isl_set *isl_set_cow(struct isl_set *set)
553 return isl_set_dup(set);
556 struct isl_map *isl_map_cow(struct isl_map *map)
564 return isl_map_dup(map);
567 static void swap_vars(struct isl_blk blk, isl_int *a,
568 unsigned a_len, unsigned b_len)
570 isl_seq_cpy(blk.data, a+a_len, b_len);
571 isl_seq_cpy(blk.data+b_len, a, a_len);
572 isl_seq_cpy(a, blk.data, b_len+a_len);
575 struct isl_basic_set *isl_basic_set_swap_vars(
576 struct isl_basic_set *bset, unsigned n)
584 isl_assert(bset->ctx, n <= bset->dim, goto error);
589 bset = isl_basic_set_cow(bset);
593 blk = isl_blk_alloc(bset->ctx, bset->dim);
594 if (isl_blk_is_error(blk))
597 for (i = 0; i < bset->n_eq; ++i)
599 bset->eq[i]+1+bset->nparam, n, bset->dim - n);
601 for (i = 0; i < bset->n_ineq; ++i)
603 bset->ineq[i]+1+bset->nparam, n, bset->dim - n);
605 for (i = 0; i < bset->n_div; ++i)
607 bset->div[i]+1+1+bset->nparam, n, bset->dim - n);
609 isl_blk_free(bset->ctx, blk);
611 F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
615 isl_basic_set_free(bset);
619 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
622 set = isl_set_cow(set);
626 for (i = 0; i < set->n; ++i) {
627 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
633 F_CLR(set, ISL_SET_NORMALIZED);
637 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
639 isl_seq_cpy(c, c + n, rem);
640 isl_seq_clr(c + rem, n);
643 struct isl_basic_set *isl_basic_set_drop_dims(
644 struct isl_basic_set *bset, unsigned first, unsigned n)
651 isl_assert(bset->ctx, first + n <= bset->dim, goto error);
656 bset = isl_basic_set_cow(bset);
660 for (i = 0; i < bset->n_eq; ++i)
661 constraint_drop_vars(bset->eq[i]+1+bset->nparam+first, n,
662 (bset->dim-first-n)+bset->extra);
664 for (i = 0; i < bset->n_ineq; ++i)
665 constraint_drop_vars(bset->ineq[i]+1+bset->nparam+first, n,
666 (bset->dim-first-n)+bset->extra);
668 for (i = 0; i < bset->n_div; ++i)
669 constraint_drop_vars(bset->div[i]+1+1+bset->nparam+first, n,
670 (bset->dim-first-n)+bset->extra);
675 F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
676 bset = isl_basic_set_simplify(bset);
677 return isl_basic_set_finalize(bset);
679 isl_basic_set_free(bset);
683 static struct isl_set *isl_set_drop_dims(
684 struct isl_set *set, unsigned first, unsigned n)
691 isl_assert(set->ctx, first + n <= set->dim, goto error);
695 set = isl_set_cow(set);
699 for (i = 0; i < set->n; ++i) {
700 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
706 F_CLR(set, ISL_SET_NORMALIZED);
713 struct isl_basic_map *isl_basic_map_drop_inputs(
714 struct isl_basic_map *bmap, unsigned first, unsigned n)
721 isl_assert(bmap->ctx, first + n <= bmap->n_in, goto error);
726 bmap = isl_basic_map_cow(bmap);
730 for (i = 0; i < bmap->n_eq; ++i)
731 constraint_drop_vars(bmap->eq[i]+1+bmap->nparam+first, n,
732 (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
734 for (i = 0; i < bmap->n_ineq; ++i)
735 constraint_drop_vars(bmap->ineq[i]+1+bmap->nparam+first, n,
736 (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
738 for (i = 0; i < bmap->n_div; ++i)
739 constraint_drop_vars(bmap->div[i]+1+1+bmap->nparam+first, n,
740 (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
745 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
746 bmap = isl_basic_map_simplify(bmap);
747 return isl_basic_map_finalize(bmap);
749 isl_basic_map_free(bmap);
753 static struct isl_map *isl_map_drop_inputs(
754 struct isl_map *map, unsigned first, unsigned n)
761 isl_assert(map->ctx, first + n <= map->n_in, goto error);
765 map = isl_map_cow(map);
769 for (i = 0; i < map->n; ++i) {
770 map->p[i] = isl_basic_map_drop_inputs(map->p[i], first, n);
775 F_CLR(map, ISL_MAP_NORMALIZED);
784 * We don't cow, as the div is assumed to be redundant.
786 static struct isl_basic_map *isl_basic_map_drop_div(
787 struct isl_basic_map *bmap, unsigned div)
795 pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
797 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
799 for (i = 0; i < bmap->n_eq; ++i)
800 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
802 for (i = 0; i < bmap->n_ineq; ++i) {
803 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
804 isl_basic_map_drop_inequality(bmap, i);
808 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
811 for (i = 0; i < bmap->n_div; ++i)
812 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
814 if (div != bmap->n_div - 1) {
816 isl_int *t = bmap->div[div];
818 for (j = div; j < bmap->n_div - 1; ++j)
819 bmap->div[j] = bmap->div[j+1];
821 bmap->div[bmap->n_div - 1] = t;
823 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
824 isl_basic_map_free_div(bmap, 1);
828 isl_basic_map_free(bmap);
832 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
835 unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
837 len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
839 for (i = 0; i < bmap->n_eq; ++i)
840 if (bmap->eq[i] != eq)
841 isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
843 for (i = 0; i < bmap->n_ineq; ++i)
844 isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
846 /* We need to be careful about circular definitions,
847 * so for now we just remove the definitions of other divs that
848 * depend on this div and (possibly) recompute them later.
850 for (i = 0; i < bmap->n_div; ++i)
851 if (!isl_int_is_zero(bmap->div[i][0]) &&
852 !isl_int_is_zero(bmap->div[i][1 + pos]))
853 isl_seq_clr(bmap->div[i], 1 + len);
855 isl_basic_map_drop_div(bmap, div);
858 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
864 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
865 isl_basic_map_free_div(bmap, bmap->n_div);
866 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
868 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
870 isl_basic_map_alloc_equality(bmap);
874 isl_int_set_si(bmap->eq[i][0], 1);
875 isl_seq_clr(bmap->eq[i]+1, total);
876 F_SET(bmap, ISL_BASIC_MAP_EMPTY);
877 return isl_basic_map_finalize(bmap);
879 isl_basic_map_free(bmap);
883 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
885 return (struct isl_basic_set *)
886 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
889 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
891 isl_int *t = bmap->eq[a];
892 bmap->eq[a] = bmap->eq[b];
896 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
899 isl_int *t = bmap->ineq[a];
900 bmap->ineq[a] = bmap->ineq[b];
905 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
907 swap_inequality((struct isl_basic_map *)bset, a, b);
910 static void swap_div(struct isl_basic_map *bmap, int a, int b)
913 unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
914 isl_int *t = bmap->div[a];
915 bmap->div[a] = bmap->div[b];
918 for (i = 0; i < bmap->n_eq; ++i)
919 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
921 for (i = 0; i < bmap->n_ineq; ++i)
922 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
924 for (i = 0; i < bmap->n_div; ++i)
925 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
926 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
929 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
930 unsigned pos, isl_int *eq, int *progress)
935 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
936 for (k = 0; k < bmap->n_eq; ++k) {
937 if (bmap->eq[k] == eq)
939 if (isl_int_is_zero(bmap->eq[k][1+pos]))
943 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
946 for (k = 0; k < bmap->n_ineq; ++k) {
947 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
951 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
952 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
955 for (k = 0; k < bmap->n_div; ++k) {
956 if (isl_int_is_zero(bmap->div[k][0]))
958 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
962 isl_seq_elim(bmap->div[k]+1, eq,
963 1+pos, 1+total, &bmap->div[k][0]);
964 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
969 struct isl_basic_map *isl_basic_map_gauss(
970 struct isl_basic_map *bmap, int *progress)
981 total_var = bmap->nparam + bmap->n_in + bmap->n_out;
982 total = total_var + bmap->n_div;
984 last_var = total - 1;
985 for (done = 0; done < bmap->n_eq; ++done) {
986 for (; last_var >= 0; --last_var) {
987 for (k = done; k < bmap->n_eq; ++k)
988 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
996 swap_equality(bmap, k, done);
997 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
998 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
1000 eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
1003 if (last_var >= total_var &&
1004 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
1005 unsigned div = last_var - total_var;
1006 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
1007 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
1008 isl_int_set(bmap->div[div][0],
1009 bmap->eq[done][1+last_var]);
1010 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1013 if (done == bmap->n_eq)
1015 for (k = done; k < bmap->n_eq; ++k) {
1016 if (isl_int_is_zero(bmap->eq[k][0]))
1018 return isl_basic_map_set_to_empty(bmap);
1020 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
1024 struct isl_basic_set *isl_basic_set_gauss(
1025 struct isl_basic_set *bset, int *progress)
1027 return (struct isl_basic_set*)isl_basic_map_gauss(
1028 (struct isl_basic_map *)bset, progress);
1031 static unsigned int round_up(unsigned int v)
1042 static int hash_index(isl_int ***index, unsigned int size, int bits,
1043 struct isl_basic_map *bmap, int k)
1046 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1047 uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
1048 for (h = hash; index[h]; h = (h+1) % size)
1049 if (&bmap->ineq[k] != index[h] &&
1050 isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
1055 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
1056 struct isl_basic_set *bset, int k)
1058 return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
1061 static struct isl_basic_map *remove_duplicate_constraints(
1062 struct isl_basic_map *bmap, int *progress)
1068 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1071 if (bmap->n_ineq <= 1)
1074 size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
1075 bits = ffs(size) - 1;
1076 index = isl_alloc_array(ctx, isl_int **, size);
1077 memset(index, 0, size * sizeof(isl_int **));
1081 index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
1082 for (k = 1; k < bmap->n_ineq; ++k) {
1083 h = hash_index(index, size, bits, bmap, k);
1085 index[h] = &bmap->ineq[k];
1090 l = index[h] - &bmap->ineq[0];
1091 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1092 swap_inequality(bmap, k, l);
1093 isl_basic_map_drop_inequality(bmap, k);
1097 for (k = 0; k < bmap->n_ineq-1; ++k) {
1098 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1099 h = hash_index(index, size, bits, bmap, k);
1100 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1103 l = index[h] - &bmap->ineq[0];
1104 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1105 if (isl_int_is_pos(sum))
1107 if (isl_int_is_zero(sum)) {
1108 /* We need to break out of the loop after these
1109 * changes since the contents of the hash
1110 * will no longer be valid.
1111 * Plus, we probably we want to regauss first.
1113 isl_basic_map_drop_inequality(bmap, l);
1114 isl_basic_map_inequality_to_equality(bmap, k);
1116 bmap = isl_basic_map_set_to_empty(bmap);
1125 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1130 total = bmap->nparam + bmap->n_in + bmap->n_out;
1131 for (d = 0; d < total; ++d)
1133 for (d = total - 1, i = 0; d >= 0 && i < bmap->n_eq; ++i) {
1134 for (; d >= 0; --d) {
1135 if (isl_int_is_zero(bmap->eq[i][1+d]))
1143 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1145 return compute_elimination_index((struct isl_basic_map *)bset, elim);
1148 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1149 struct isl_basic_map *bmap, int *elim)
1155 total = bmap->nparam + bmap->n_in + bmap->n_out;
1156 for (d = total - 1; d >= 0; --d) {
1157 if (isl_int_is_zero(src[1+d]))
1162 isl_seq_cpy(dst, src, 1 + total);
1165 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1170 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1171 struct isl_basic_set *bset, int *elim)
1173 return reduced_using_equalities(dst, src,
1174 (struct isl_basic_map *)bset, elim);
1177 /* Quick check to see if two basic maps are disjoint.
1178 * In particular, we reduce the equalities and inequalities of
1179 * one basic map in the context of the equalities of the other
1180 * basic map and check if we get a contradiction.
1182 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1183 struct isl_basic_map *bmap2)
1185 struct isl_vec *v = NULL;
1190 if (!bmap1 || !bmap2)
1192 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, return -1);
1193 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, return -1);
1194 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, return -1);
1195 if (bmap1->n_div || bmap2->n_div)
1197 if (!bmap1->n_eq && !bmap2->n_eq)
1200 total = bmap1->nparam + bmap1->n_in + bmap1->n_out;
1203 v = isl_vec_alloc(bmap1->ctx, 1 + total);
1206 elim = isl_alloc_array(bmap1->ctx, int, total);
1209 compute_elimination_index(bmap1, elim);
1210 for (i = 0; i < bmap2->n_eq; ++i) {
1212 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1214 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1215 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1218 for (i = 0; i < bmap2->n_ineq; ++i) {
1220 reduced = reduced_using_equalities(v->block.data,
1221 bmap2->ineq[i], bmap1, elim);
1222 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1223 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1226 compute_elimination_index(bmap2, elim);
1227 for (i = 0; i < bmap1->n_ineq; ++i) {
1229 reduced = reduced_using_equalities(v->block.data,
1230 bmap1->ineq[i], bmap2, elim);
1231 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1232 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1235 isl_vec_free(bmap1->ctx, v);
1239 isl_vec_free(bmap1->ctx, v);
1243 isl_vec_free(bmap1->ctx, v);
1248 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1249 struct isl_basic_set *bset2)
1251 return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1252 (struct isl_basic_map *)bset2);
1255 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1262 if (isl_map_fast_is_equal(map1, map2))
1265 for (i = 0; i < map1->n; ++i) {
1266 for (j = 0; j < map2->n; ++j) {
1267 int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1276 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1278 return isl_map_fast_is_disjoint((struct isl_map *)set1,
1279 (struct isl_map *)set2);
1282 static struct isl_basic_map *remove_duplicate_divs(
1283 struct isl_basic_map *bmap, int *progress)
1290 unsigned total_var = bmap->nparam + bmap->n_in + bmap->n_out;
1291 unsigned total = total_var + bmap->n_div;
1292 struct isl_ctx *ctx;
1294 if (bmap->n_div <= 1)
1298 for (k = bmap->n_div - 1; k >= 0; --k)
1299 if (!isl_int_is_zero(bmap->div[k][0]))
1304 size = round_up(4 * bmap->n_div / 3 - 1);
1305 bits = ffs(size) - 1;
1306 index = isl_alloc_array(ctx, int, size);
1307 memset(index, 0, size * sizeof(int));
1310 eq = isl_blk_alloc(ctx, 1+total);
1311 if (isl_blk_is_error(eq))
1314 isl_seq_clr(eq.data, 1+total);
1315 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
1316 for (--k; k >= 0; --k) {
1319 if (isl_int_is_zero(bmap->div[k][0]))
1322 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
1323 for (h = hash; index[h]; h = (h+1) % size)
1324 if (isl_seq_eq(bmap->div[k],
1325 bmap->div[index[h]-1], 2+total))
1330 isl_int_set_si(eq.data[1+total_var+k], -1);
1331 isl_int_set_si(eq.data[1+total_var+l], 1);
1332 eliminate_div(bmap, eq.data, l);
1333 isl_int_set_si(eq.data[1+total_var+k], 0);
1334 isl_int_set_si(eq.data[1+total_var+l], 0);
1339 isl_blk_free(ctx, eq);
1345 /* Elimininate divs based on equalities
1347 static struct isl_basic_map *eliminate_divs_eq(
1348 struct isl_basic_map *bmap, int *progress)
1358 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1360 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1361 for (i = 0; i < bmap->n_eq; ++i) {
1362 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
1363 !isl_int_is_negone(bmap->eq[i][off + d]))
1367 eliminate_div(bmap, bmap->eq[i], d);
1368 isl_basic_map_drop_equality(bmap, i);
1373 return eliminate_divs_eq(bmap, progress);
1377 static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
1381 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1384 for (i = bmap->n_eq - 1; i >= 0; --i) {
1385 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
1386 if (isl_int_is_zero(gcd)) {
1387 if (!isl_int_is_zero(bmap->eq[i][0])) {
1388 bmap = isl_basic_map_set_to_empty(bmap);
1391 isl_basic_map_drop_equality(bmap, i);
1394 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1395 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
1396 if (isl_int_is_one(gcd))
1398 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
1399 bmap = isl_basic_map_set_to_empty(bmap);
1402 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
1405 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1406 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
1407 if (isl_int_is_zero(gcd)) {
1408 if (isl_int_is_neg(bmap->ineq[i][0])) {
1409 bmap = isl_basic_map_set_to_empty(bmap);
1412 isl_basic_map_drop_inequality(bmap, i);
1415 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1416 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
1417 if (isl_int_is_one(gcd))
1419 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
1420 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
1428 /* Eliminate the specified variables from the constraints using
1429 * Fourier-Motzkin. The variables themselves are not removed.
1431 struct isl_basic_map *isl_basic_map_eliminate_vars(
1432 struct isl_basic_map *bmap, int pos, unsigned n)
1442 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1444 bmap = isl_basic_map_cow(bmap);
1445 for (d = pos + n - 1; d >= pos; --d) {
1446 int n_lower, n_upper;
1449 for (i = 0; i < bmap->n_eq; ++i) {
1450 if (isl_int_is_zero(bmap->eq[i][1+d]))
1452 eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
1453 isl_basic_map_drop_equality(bmap, i);
1460 for (i = 0; i < bmap->n_ineq; ++i) {
1461 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1463 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1466 bmap = isl_basic_map_extend(bmap,
1467 bmap->nparam, bmap->n_in, bmap->n_out, 0,
1468 0, n_lower * n_upper);
1469 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1471 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1474 for (j = 0; j < i; ++j) {
1475 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1478 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1479 isl_int_sgn(bmap->ineq[j][1+d]))
1481 k = isl_basic_map_alloc_inequality(bmap);
1484 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1486 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1487 1+d, 1+total, NULL);
1489 isl_basic_map_drop_inequality(bmap, i);
1492 if (n_lower > 0 && n_upper > 0) {
1493 bmap = normalize_constraints(bmap);
1494 bmap = remove_duplicate_constraints(bmap, NULL);
1495 bmap = isl_basic_map_gauss(bmap, NULL);
1496 bmap = isl_basic_map_convex_hull(bmap);
1499 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1503 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1506 isl_basic_map_free(bmap);
1510 struct isl_basic_set *isl_basic_set_eliminate_vars(
1511 struct isl_basic_set *bset, unsigned pos, unsigned n)
1513 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1514 (struct isl_basic_map *)bset, pos, n);
1517 /* Eliminate the specified n dimensions starting at first from the
1518 * constraints using Fourier-Motzkin, The dimensions themselves
1521 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1522 unsigned first, unsigned n)
1531 set = isl_set_cow(set);
1534 isl_assert(set->ctx, first+n <= set->dim, goto error);
1536 for (i = 0; i < set->n; ++i) {
1537 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1538 set->nparam + first, n);
1548 /* Project out n dimensions starting at first using Fourier-Motzkin */
1549 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1550 unsigned first, unsigned n)
1552 set = isl_set_eliminate_dims(set, first, n);
1553 set = isl_set_drop_dims(set, first, n);
1557 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1559 bset = isl_basic_set_eliminate_vars(bset, bset->nparam + bset->dim,
1567 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1576 set = isl_set_cow(set);
1580 for (i = 0; i < set->n; ++i) {
1581 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1591 /* Project out n inputs starting at first using Fourier-Motzkin */
1592 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1593 unsigned first, unsigned n)
1600 map = isl_map_cow(map);
1603 isl_assert(map->ctx, first+n <= map->n_in, goto error);
1605 for (i = 0; i < map->n; ++i) {
1606 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1607 map->nparam + first, n);
1611 map = isl_map_drop_inputs(map, first, n);
1618 /* Project out n dimensions starting at first using Fourier-Motzkin */
1619 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1620 unsigned first, unsigned n)
1622 bset = isl_basic_set_eliminate_vars(bset, bset->nparam + first, n);
1623 bset = isl_basic_set_drop_dims(bset, first, n);
1627 /* Elimininate divs based on inequalities
1629 static struct isl_basic_map *eliminate_divs_ineq(
1630 struct isl_basic_map *bmap, int *progress)
1635 struct isl_ctx *ctx;
1641 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1643 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1644 for (i = 0; i < bmap->n_eq; ++i)
1645 if (!isl_int_is_zero(bmap->eq[i][off + d]))
1649 for (i = 0; i < bmap->n_ineq; ++i)
1650 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
1652 if (i < bmap->n_ineq)
1655 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
1656 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1658 bmap = isl_basic_map_drop_div(bmap, d);
1665 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1672 bmap = normalize_constraints(bmap);
1673 bmap = eliminate_divs_eq(bmap, &progress);
1674 bmap = eliminate_divs_ineq(bmap, &progress);
1675 bmap = isl_basic_map_gauss(bmap, &progress);
1676 bmap = remove_duplicate_divs(bmap, &progress);
1677 bmap = remove_duplicate_constraints(bmap, &progress);
1682 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1684 return (struct isl_basic_set *)
1685 isl_basic_map_simplify((struct isl_basic_map *)bset);
1688 static void dump_term(struct isl_basic_map *bmap,
1689 isl_int c, int pos, FILE *out)
1691 unsigned in = bmap->n_in;
1692 unsigned dim = bmap->n_in + bmap->n_out;
1694 isl_int_print(out, c, 0);
1696 if (!isl_int_is_one(c))
1697 isl_int_print(out, c, 0);
1698 if (pos < 1 + bmap->nparam)
1699 fprintf(out, "p%d", pos - 1);
1700 else if (pos < 1 + bmap->nparam + in)
1701 fprintf(out, "i%d", pos - 1 - bmap->nparam);
1702 else if (pos < 1 + bmap->nparam + dim)
1703 fprintf(out, "o%d", pos - 1 - bmap->nparam - in);
1705 fprintf(out, "e%d", pos - 1 - bmap->nparam - dim);
1709 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1710 int sign, FILE *out)
1714 unsigned len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1718 for (i = 0, first = 1; i < len; ++i) {
1719 if (isl_int_sgn(c[i]) * sign <= 0)
1722 fprintf(out, " + ");
1724 isl_int_abs(v, c[i]);
1725 dump_term(bmap, v, i, out);
1732 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1733 const char *op, FILE *out, int indent)
1737 fprintf(out, "%*s", indent, "");
1739 dump_constraint_sign(bmap, c, 1, out);
1740 fprintf(out, " %s ", op);
1741 dump_constraint_sign(bmap, c, -1, out);
1745 for (i = bmap->n_div; i < bmap->extra; ++i) {
1746 if (isl_int_is_zero(c[1+bmap->nparam+bmap->n_in+bmap->n_out+i]))
1748 fprintf(out, "%*s", indent, "");
1749 fprintf(out, "ERROR: unused div coefficient not zero\n");
1753 static void dump_constraints(struct isl_basic_map *bmap,
1754 isl_int **c, unsigned n,
1755 const char *op, FILE *out, int indent)
1759 for (i = 0; i < n; ++i)
1760 dump_constraint(bmap, c[i], op, out, indent);
1763 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1767 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1769 for (j = 0; j < 1 + total; ++j) {
1770 if (isl_int_is_zero(exp[j]))
1772 if (!first && isl_int_is_pos(exp[j]))
1774 dump_term(bmap, exp[j], j, out);
1779 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1783 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1784 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1786 for (i = 0; i < bmap->n_div; ++i) {
1787 fprintf(out, "%*s", indent, "");
1788 fprintf(out, "e%d = [(", i);
1789 dump_affine(bmap, bmap->div[i]+1, out);
1791 isl_int_print(out, bmap->div[i][0], 0);
1792 fprintf(out, "]\n");
1796 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1799 fprintf(out, "null basic set\n");
1803 fprintf(out, "%*s", indent, "");
1804 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1805 bset->ref, bset->nparam, bset->dim, bset->extra,
1807 dump((struct isl_basic_map *)bset, out, indent);
1810 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1813 fprintf(out, "null basic map\n");
1817 fprintf(out, "%*s", indent, "");
1818 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, flags: %x\n",
1820 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, bmap->flags);
1821 dump(bmap, out, indent);
1824 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1826 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1829 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1830 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1831 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1832 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1836 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1837 unsigned nparam, unsigned dim, int n, unsigned flags)
1839 struct isl_set *set;
1841 isl_assert(ctx, n >= 0, return NULL);
1842 set = isl_alloc(ctx, struct isl_set,
1843 sizeof(struct isl_set) +
1844 n * sizeof(struct isl_basic_set *));
1853 set->nparam = nparam;
1860 struct isl_set *isl_set_dup(struct isl_set *set)
1863 struct isl_set *dup;
1868 dup = isl_set_alloc(set->ctx, set->nparam, set->dim, set->n, set->flags);
1871 for (i = 0; i < set->n; ++i)
1872 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1876 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1878 struct isl_set *set;
1883 set = isl_set_alloc(bset->ctx, bset->nparam, bset->dim, 1, ISL_MAP_DISJOINT);
1885 isl_basic_set_free(bset);
1888 return isl_set_add(set, bset);
1891 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1893 struct isl_map *map;
1898 map = isl_map_alloc(bmap->ctx, bmap->nparam, bmap->n_in, bmap->n_out, 1,
1901 isl_basic_map_free(bmap);
1904 return isl_map_add(map, bmap);
1907 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1911 isl_assert(set->ctx, set->nparam == bset->nparam, goto error);
1912 isl_assert(set->ctx, set->dim == bset->dim, goto error);
1913 isl_assert(set->ctx, set->n < set->size, goto error);
1914 set->p[set->n] = bset;
1921 isl_basic_set_free(bset);
1925 void isl_set_free(struct isl_set *set)
1935 isl_ctx_deref(set->ctx);
1936 for (i = 0; i < set->n; ++i)
1937 isl_basic_set_free(set->p[i]);
1941 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1946 fprintf(out, "null set\n");
1950 fprintf(out, "%*s", indent, "");
1951 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1952 set->ref, set->n, set->nparam, set->dim, set->flags);
1953 for (i = 0; i < set->n; ++i) {
1954 fprintf(out, "%*s", indent, "");
1955 fprintf(out, "basic set %d:\n", i);
1956 isl_basic_set_dump(set->p[i], out, indent+4);
1960 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1965 fprintf(out, "null map\n");
1969 fprintf(out, "%*s", indent, "");
1970 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, flags: %x\n",
1971 map->ref, map->n, map->nparam, map->n_in, map->n_out,
1973 for (i = 0; i < map->n; ++i) {
1974 fprintf(out, "%*s", indent, "");
1975 fprintf(out, "basic map %d:\n", i);
1976 isl_basic_map_dump(map->p[i], out, indent+4);
1980 struct isl_basic_map *isl_basic_map_intersect_domain(
1981 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1983 struct isl_basic_map *bmap_domain;
1988 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1989 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1991 bmap = isl_basic_map_extend(bmap,
1992 bset->nparam, bmap->n_in, bmap->n_out,
1993 bset->n_div, bset->n_eq, bset->n_ineq);
1996 bmap_domain = isl_basic_map_from_basic_set(bset,
1998 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2000 bmap = isl_basic_map_simplify(bmap);
2001 return isl_basic_map_finalize(bmap);
2003 isl_basic_map_free(bmap);
2004 isl_basic_set_free(bset);
2008 struct isl_basic_map *isl_basic_map_intersect_range(
2009 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2011 struct isl_basic_map *bmap_range;
2016 isl_assert(bset->ctx, bset->dim == bmap->n_out, goto error);
2017 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
2019 bmap = isl_basic_map_extend(bmap,
2020 bset->nparam, bmap->n_in, bmap->n_out,
2021 bset->n_div, bset->n_eq, bset->n_ineq);
2024 bmap_range = isl_basic_map_from_basic_set(bset,
2026 bmap = add_constraints(bmap, bmap_range, 0, 0);
2028 bmap = isl_basic_map_simplify(bmap);
2029 return isl_basic_map_finalize(bmap);
2031 isl_basic_map_free(bmap);
2032 isl_basic_set_free(bset);
2036 struct isl_basic_map *isl_basic_map_intersect(
2037 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2039 if (!bmap1 || !bmap2)
2042 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
2043 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
2044 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
2046 bmap1 = isl_basic_map_extend(bmap1,
2047 bmap1->nparam, bmap1->n_in, bmap1->n_out,
2048 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2051 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2053 bmap1 = isl_basic_map_simplify(bmap1);
2054 return isl_basic_map_finalize(bmap1);
2056 isl_basic_map_free(bmap1);
2057 isl_basic_map_free(bmap2);
2061 struct isl_basic_set *isl_basic_set_intersect(
2062 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2064 return (struct isl_basic_set *)
2065 isl_basic_map_intersect(
2066 (struct isl_basic_map *)bset1,
2067 (struct isl_basic_map *)bset2);
2070 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2073 struct isl_map *result;
2079 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2080 F_ISSET(map2, ISL_MAP_DISJOINT))
2081 FL_SET(flags, ISL_MAP_DISJOINT);
2083 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
2084 map1->n * map2->n, flags);
2087 for (i = 0; i < map1->n; ++i)
2088 for (j = 0; j < map2->n; ++j) {
2089 struct isl_basic_map *part;
2090 part = isl_basic_map_intersect(
2091 isl_basic_map_copy(map1->p[i]),
2092 isl_basic_map_copy(map2->p[j]));
2093 if (isl_basic_map_is_empty(part))
2094 isl_basic_map_free(part);
2096 result = isl_map_add(result, part);
2109 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2111 return (struct isl_set *)
2112 isl_map_intersect((struct isl_map *)set1,
2113 (struct isl_map *)set2);
2116 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2118 struct isl_basic_set *bset;
2123 bmap = isl_basic_map_cow(bmap);
2127 bset = isl_basic_set_from_basic_map(bmap);
2128 bset = isl_basic_set_swap_vars(bset, in);
2129 return isl_basic_map_from_basic_set(bset, bset->dim-in, in);
2132 /* Turn final n dimensions into existentially quantified variables.
2134 struct isl_basic_set *isl_basic_set_project_out(
2135 struct isl_basic_set *bset, unsigned n, unsigned flags)
2145 isl_assert(bset->ctx, n <= bset->dim, goto error);
2150 bset = isl_basic_set_cow(bset);
2152 row_size = 1 + bset->nparam + bset->dim + bset->extra;
2153 old = bset->block2.data;
2154 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
2155 (bset->extra + n) * (1 + row_size));
2156 if (!bset->block2.data)
2158 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
2161 for (i = 0; i < n; ++i) {
2162 new_div[i] = bset->block2.data +
2163 (bset->extra + i) * (1 + row_size);
2164 isl_seq_clr(new_div[i], 1 + row_size);
2166 for (i = 0; i < bset->extra; ++i)
2167 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
2169 bset->div = new_div;
2173 bset = isl_basic_set_simplify(bset);
2174 return isl_basic_set_finalize(bset);
2176 isl_basic_set_free(bset);
2180 struct isl_basic_map *isl_basic_map_apply_range(
2181 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2183 struct isl_basic_set *bset;
2184 unsigned n_in, n_out;
2186 if (!bmap1 || !bmap2)
2189 isl_assert(bmap->ctx, bmap1->n_out == bmap2->n_in, goto error);
2190 isl_assert(bmap->ctx, bmap1->nparam == bmap2->nparam, goto error);
2193 n_out = bmap2->n_out;
2195 bmap2 = isl_basic_map_reverse(bmap2);
2198 bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
2199 bmap1->n_in + bmap2->n_in, bmap1->n_out,
2200 bmap2->extra, bmap2->n_eq, bmap2->n_ineq);
2203 bmap1 = add_constraints(bmap1, bmap2, bmap1->n_in - bmap2->n_in, 0);
2204 bmap1 = isl_basic_map_simplify(bmap1);
2205 bset = isl_basic_set_from_basic_map(bmap1);
2206 bset = isl_basic_set_project_out(bset,
2207 bset->dim - (n_in + n_out), 0);
2208 return isl_basic_map_from_basic_set(bset, n_in, n_out);
2210 isl_basic_map_free(bmap1);
2211 isl_basic_map_free(bmap2);
2215 struct isl_basic_set *isl_basic_set_apply(
2216 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2221 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
2223 return (struct isl_basic_set *)
2224 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2226 isl_basic_set_free(bset);
2227 isl_basic_map_free(bmap);
2231 struct isl_basic_map *isl_basic_map_apply_domain(
2232 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2234 if (!bmap1 || !bmap2)
2237 isl_assert(ctx, bmap1->n_in == bmap2->n_in, goto error);
2238 isl_assert(ctx, bmap1->nparam == bmap2->nparam, goto error);
2240 bmap1 = isl_basic_map_reverse(bmap1);
2241 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2242 return isl_basic_map_reverse(bmap1);
2244 isl_basic_map_free(bmap1);
2245 isl_basic_map_free(bmap2);
2249 static struct isl_basic_map *var_equal(struct isl_ctx *ctx,
2250 struct isl_basic_map *bmap, unsigned pos)
2253 i = isl_basic_map_alloc_equality(bmap);
2256 isl_seq_clr(bmap->eq[i],
2257 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div);
2258 isl_int_set_si(bmap->eq[i][1+bmap->nparam+pos], -1);
2259 isl_int_set_si(bmap->eq[i][1+bmap->nparam+bmap->n_in+pos], 1);
2260 return isl_basic_map_finalize(bmap);
2262 isl_basic_map_free(bmap);
2266 static struct isl_basic_map *var_more(struct isl_ctx *ctx,
2267 struct isl_basic_map *bmap, unsigned pos)
2270 i = isl_basic_map_alloc_inequality(bmap);
2273 isl_seq_clr(bmap->ineq[i],
2274 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div);
2275 isl_int_set_si(bmap->ineq[i][0], -1);
2276 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], -1);
2277 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], 1);
2278 return isl_basic_map_finalize(bmap);
2280 isl_basic_map_free(bmap);
2284 static struct isl_basic_map *var_less(struct isl_ctx *ctx,
2285 struct isl_basic_map *bmap, unsigned pos)
2288 i = isl_basic_map_alloc_inequality(bmap);
2291 isl_seq_clr(bmap->ineq[i],
2292 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div);
2293 isl_int_set_si(bmap->ineq[i][0], -1);
2294 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], 1);
2295 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], -1);
2296 return isl_basic_map_finalize(bmap);
2298 isl_basic_map_free(bmap);
2302 struct isl_basic_map *isl_basic_map_equal(struct isl_ctx *ctx,
2303 unsigned nparam, unsigned in, unsigned out, unsigned n_equal)
2306 struct isl_basic_map *bmap;
2307 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, n_equal, 0);
2310 for (i = 0; i < n_equal && bmap; ++i)
2311 bmap = var_equal(ctx, bmap, i);
2312 return isl_basic_map_finalize(bmap);
2315 struct isl_basic_map *isl_basic_map_less_at(struct isl_ctx *ctx,
2316 unsigned nparam, unsigned in, unsigned out, unsigned pos)
2319 struct isl_basic_map *bmap;
2320 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
2323 for (i = 0; i < pos && bmap; ++i)
2324 bmap = var_equal(ctx, bmap, i);
2326 bmap = var_less(ctx, bmap, pos);
2327 return isl_basic_map_finalize(bmap);
2330 struct isl_basic_map *isl_basic_map_more_at(struct isl_ctx *ctx,
2331 unsigned nparam, unsigned in, unsigned out, unsigned pos)
2334 struct isl_basic_map *bmap;
2335 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
2338 for (i = 0; i < pos && bmap; ++i)
2339 bmap = var_equal(ctx, bmap, i);
2341 bmap = var_more(ctx, bmap, pos);
2342 return isl_basic_map_finalize(bmap);
2345 struct isl_basic_map *isl_basic_map_from_basic_set(
2346 struct isl_basic_set *bset, unsigned n_in, unsigned n_out)
2348 struct isl_basic_map *bmap;
2350 bset = isl_basic_set_cow(bset);
2354 isl_assert(ctx, bset->dim == n_in + n_out, goto error);
2355 bmap = (struct isl_basic_map *) bset;
2357 bmap->n_out = n_out;
2358 return isl_basic_map_finalize(bmap);
2360 isl_basic_set_free(bset);
2364 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2368 if (bmap->n_in == 0)
2369 return (struct isl_basic_set *)bmap;
2370 bmap = isl_basic_map_cow(bmap);
2373 bmap->n_out += bmap->n_in;
2375 bmap = isl_basic_map_finalize(bmap);
2376 return (struct isl_basic_set *)bmap;
2381 /* For a div d = floor(f/m), add the constraints
2384 * -(f-(n-1)) + m d >= 0
2386 * Note that the second constraint is the negation of
2390 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2393 unsigned div_pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
2394 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
2396 i = isl_basic_map_alloc_inequality(bmap);
2399 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2400 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2402 j = isl_basic_map_alloc_inequality(bmap);
2405 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2406 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2407 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2411 struct isl_basic_set *isl_basic_map_underlying_set(
2412 struct isl_basic_map *bmap)
2416 if (bmap->nparam == 0 && bmap->n_in == 0 && bmap->n_div == 0)
2417 return (struct isl_basic_set *)bmap;
2418 bmap = isl_basic_map_cow(bmap);
2421 bmap->n_out += bmap->nparam + bmap->n_in + bmap->n_div;
2424 bmap->extra -= bmap->n_div;
2426 bmap = isl_basic_map_finalize(bmap);
2427 return (struct isl_basic_set *)bmap;
2432 struct isl_basic_map *isl_basic_map_overlying_set(
2433 struct isl_basic_set *bset, struct isl_basic_map *like)
2435 struct isl_basic_map *bmap;
2436 struct isl_ctx *ctx;
2443 isl_assert(ctx, bset->dim ==
2444 like->nparam + like->n_in + like->n_out + like->n_div,
2446 if (like->nparam == 0 && like->n_in == 0 && like->n_div == 0) {
2447 isl_basic_map_free(like);
2448 return (struct isl_basic_map *)bset;
2450 bset = isl_basic_set_cow(bset);
2453 total = bset->dim + bset->extra;
2454 bmap = (struct isl_basic_map *)bset;
2455 bmap->nparam = like->nparam;
2456 bmap->n_in = like->n_in;
2457 bmap->n_out = like->n_out;
2458 bmap->extra += like->n_div;
2461 ltotal = total - bmap->extra + like->extra;
2464 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2465 bmap->extra * (1 + 1 + total));
2466 if (isl_blk_is_error(bmap->block2))
2468 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2472 bmap = isl_basic_map_extend(bmap, bmap->nparam,
2473 bmap->n_in, bmap->n_out, 0, 0, 2 * like->n_div);
2474 for (i = 0; i < like->n_div; ++i) {
2475 k = isl_basic_map_alloc_div(bmap);
2478 isl_seq_cpy(bmap->div[k], like->div[i], 1 + 1 + ltotal);
2479 isl_seq_clr(bmap->div[k]+1+1+ltotal, total - ltotal);
2480 if (add_div_constraints(bmap, k) < 0)
2484 isl_basic_map_free(like);
2485 bmap = isl_basic_map_finalize(bmap);
2488 isl_basic_map_free(like);
2489 isl_basic_set_free(bset);
2493 struct isl_basic_set *isl_basic_set_from_underlying_set(
2494 struct isl_basic_set *bset, struct isl_basic_set *like)
2496 return (struct isl_basic_set *)
2497 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2500 struct isl_set *isl_set_from_underlying_set(
2501 struct isl_set *set, struct isl_basic_set *like)
2507 isl_assert(set->ctx, set->dim == like->nparam + like->dim + like->n_div,
2509 if (like->nparam == 0 && like->n_div == 0) {
2510 isl_basic_set_free(like);
2513 set = isl_set_cow(set);
2516 for (i = 0; i < set->n; ++i) {
2517 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2518 isl_basic_set_copy(like));
2522 set->nparam = like->nparam;
2523 set->dim = like->dim;
2524 isl_basic_set_free(like);
2527 isl_basic_set_free(like);
2532 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2536 map = isl_map_align_divs(map);
2537 map = isl_map_cow(map);
2541 for (i = 0; i < map->n; ++i) {
2542 map->p[i] = (struct isl_basic_map *)
2543 isl_basic_map_underlying_set(map->p[i]);
2548 map->n_out += map->nparam + map->n_in;
2550 map->n_out = map->p[0]->n_out;
2553 return (struct isl_set *)map;
2559 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2561 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2564 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2566 struct isl_basic_set *domain;
2570 n_out = bmap->n_out;
2571 domain = isl_basic_set_from_basic_map(bmap);
2572 return isl_basic_set_project_out(domain, n_out, 0);
2575 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2577 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2580 struct isl_set *isl_map_range(struct isl_map *map)
2583 struct isl_set *set;
2587 map = isl_map_cow(map);
2591 set = (struct isl_set *) map;
2593 for (i = 0; i < map->n; ++i) {
2594 set->p[i] = isl_basic_map_range(map->p[i]);
2598 F_CLR(set, ISL_MAP_DISJOINT);
2599 F_CLR(set, ISL_SET_NORMALIZED);
2606 struct isl_map *isl_map_from_set(struct isl_set *set,
2607 unsigned n_in, unsigned n_out)
2610 struct isl_map *map = NULL;
2614 isl_assert(set->ctx, set->dim == n_in + n_out, goto error);
2615 set = isl_set_cow(set);
2618 map = (struct isl_map *)set;
2619 for (i = 0; i < set->n; ++i) {
2620 map->p[i] = isl_basic_map_from_basic_set(
2621 set->p[i], n_in, n_out);
2633 struct isl_set *isl_set_from_map(struct isl_map *map)
2636 struct isl_set *set = NULL;
2640 map = isl_map_cow(map);
2643 map->n_out += map->n_in;
2645 set = (struct isl_set *)map;
2646 for (i = 0; i < map->n; ++i) {
2647 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2657 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2658 unsigned nparam, unsigned in, unsigned out, int n,
2661 struct isl_map *map;
2663 map = isl_alloc(ctx, struct isl_map,
2664 sizeof(struct isl_map) +
2665 n * sizeof(struct isl_basic_map *));
2674 map->nparam = nparam;
2681 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2682 unsigned nparam, unsigned in, unsigned out)
2684 struct isl_basic_map *bmap;
2685 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2686 bmap = isl_basic_map_set_to_empty(bmap);
2690 struct isl_basic_set *isl_basic_set_empty(struct isl_ctx *ctx,
2691 unsigned nparam, unsigned dim)
2693 struct isl_basic_set *bset;
2694 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 1, 0);
2695 bset = isl_basic_set_set_to_empty(bset);
2699 struct isl_basic_map *isl_basic_map_universe(struct isl_ctx *ctx,
2700 unsigned nparam, unsigned in, unsigned out)
2702 struct isl_basic_map *bmap;
2703 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 0, 0);
2707 struct isl_basic_set *isl_basic_set_universe(struct isl_ctx *ctx,
2708 unsigned nparam, unsigned dim)
2710 struct isl_basic_set *bset;
2711 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 0, 0);
2715 struct isl_map *isl_map_empty(struct isl_ctx *ctx,
2716 unsigned nparam, unsigned in, unsigned out)
2718 return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
2721 struct isl_set *isl_set_empty(struct isl_ctx *ctx,
2722 unsigned nparam, unsigned dim)
2724 return isl_set_alloc(ctx, nparam, dim, 0, ISL_MAP_DISJOINT);
2727 struct isl_map *isl_map_dup(struct isl_map *map)
2730 struct isl_map *dup;
2734 dup = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out, map->n,
2736 for (i = 0; i < map->n; ++i)
2737 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2741 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2745 isl_assert(map->ctx, map->nparam == bmap->nparam, goto error);
2746 isl_assert(map->ctx, map->n_in == bmap->n_in, goto error);
2747 isl_assert(map->ctx, map->n_out == bmap->n_out, goto error);
2748 isl_assert(map->ctx, map->n < map->size, goto error);
2749 map->p[map->n] = bmap;
2751 F_CLR(map, ISL_MAP_NORMALIZED);
2757 isl_basic_map_free(bmap);
2761 void isl_map_free(struct isl_map *map)
2771 isl_ctx_deref(map->ctx);
2772 for (i = 0; i < map->n; ++i)
2773 isl_basic_map_free(map->p[i]);
2777 struct isl_map *isl_map_extend(struct isl_map *base,
2778 unsigned nparam, unsigned n_in, unsigned n_out)
2782 base = isl_map_cow(base);
2786 isl_assert(base->ctx, base->nparam <= nparam, goto error);
2787 isl_assert(base->ctx, base->n_in <= n_in, goto error);
2788 isl_assert(base->ctx, base->n_out <= n_out, goto error);
2789 base->nparam = nparam;
2791 base->n_out = n_out;
2792 for (i = 0; i < base->n; ++i) {
2793 base->p[i] = isl_basic_map_extend(base->p[i],
2794 nparam, n_in, n_out, 0, 0, 0);
2804 struct isl_set *isl_set_extend(struct isl_set *base,
2805 unsigned nparam, unsigned dim)
2807 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2811 static struct isl_basic_map *isl_basic_map_fix_var(struct isl_basic_map *bmap,
2812 unsigned var, int value)
2816 bmap = isl_basic_map_cow(bmap);
2817 bmap = isl_basic_map_extend(bmap,
2818 bmap->nparam, bmap->n_in, bmap->n_out, 0, 1, 0);
2819 j = isl_basic_map_alloc_equality(bmap);
2822 isl_seq_clr(bmap->eq[j],
2823 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div);
2824 isl_int_set_si(bmap->eq[j][1 + var], -1);
2825 isl_int_set_si(bmap->eq[j][0], value);
2826 bmap = isl_basic_map_simplify(bmap);
2827 return isl_basic_map_finalize(bmap);
2829 isl_basic_map_free(bmap);
2833 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2834 unsigned input, int value)
2838 isl_assert(bmap->ctx, input < bmap->n_in, goto error);
2839 return isl_basic_map_fix_var(bmap, bmap->nparam + input, value);
2841 isl_basic_map_free(bmap);
2845 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2846 unsigned dim, int value)
2850 isl_assert(bset->ctx, dim < bset->dim, goto error);
2851 return (struct isl_basic_set *)
2852 isl_basic_map_fix_var((struct isl_basic_map *)bset,
2853 bset->nparam + dim, value);
2855 isl_basic_set_free(bset);
2859 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2860 unsigned input, int value)
2864 map = isl_map_cow(map);
2868 isl_assert(ctx, input < map->n_in, goto error);
2869 for (i = 0; i < map->n; ++i) {
2870 map->p[i] = isl_basic_map_fix_input_si(map->p[i], input, value);
2874 F_CLR(map, ISL_MAP_NORMALIZED);
2881 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2885 set = isl_set_cow(set);
2889 isl_assert(set->ctx, dim < set->dim, goto error);
2890 for (i = 0; i < set->n; ++i) {
2891 set->p[i] = isl_basic_set_fix_dim_si(set->p[i], dim, value);
2901 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2902 unsigned dim, isl_int value)
2906 bset = isl_basic_set_cow(bset);
2907 bset = isl_basic_set_extend(bset, bset->nparam, bset->dim, 0, 0, 1);
2908 j = isl_basic_set_alloc_inequality(bset);
2911 isl_seq_clr(bset->ineq[j], 1 + bset->nparam + bset->dim + bset->n_div);
2912 isl_int_set_si(bset->ineq[j][1 + bset->nparam + dim], 1);
2913 isl_int_neg(bset->ineq[j][0], value);
2914 bset = isl_basic_set_simplify(bset);
2915 return isl_basic_set_finalize(bset);
2917 isl_basic_set_free(bset);
2921 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2926 set = isl_set_cow(set);
2930 isl_assert(set->ctx, dim < set->dim, goto error);
2931 for (i = 0; i < set->n; ++i) {
2932 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2942 struct isl_map *isl_map_reverse(struct isl_map *map)
2947 map = isl_map_cow(map);
2952 map->n_in = map->n_out;
2954 for (i = 0; i < map->n; ++i) {
2955 map->p[i] = isl_basic_map_reverse(map->p[i]);
2959 F_CLR(map, ISL_MAP_NORMALIZED);
2966 struct isl_map *isl_basic_map_lexmax(
2967 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2968 struct isl_set **empty)
2970 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2973 struct isl_map *isl_basic_map_lexmin(
2974 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2975 struct isl_set **empty)
2977 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2980 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2982 struct isl_basic_map *bmap = NULL;
2983 struct isl_basic_set *dom = NULL;
2984 struct isl_map *min;
2988 bmap = isl_basic_map_from_basic_set(bset, 0, bset->dim);
2991 dom = isl_basic_set_alloc(bmap->ctx, bmap->nparam, 0, 0, 0, 0);
2994 min = isl_basic_map_lexmin(bmap, dom, NULL);
2995 return isl_map_range(min);
2997 isl_basic_map_free(bmap);
3001 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
3005 if (bmap->n_div == 0)
3006 return isl_map_from_basic_map(bmap);
3007 return isl_pip_basic_map_compute_divs(bmap);
3010 struct isl_map *isl_map_compute_divs(struct isl_map *map)
3013 struct isl_map *res;
3019 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
3020 for (i = 1 ; i < map->n; ++i) {
3022 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
3023 if (F_ISSET(map, ISL_MAP_DISJOINT))
3024 res = isl_map_union_disjoint(res, r2);
3026 res = isl_map_union(res, r2);
3033 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
3035 return (struct isl_set *)
3036 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
3039 struct isl_set *isl_set_compute_divs(struct isl_set *set)
3041 return (struct isl_set *)
3042 isl_map_compute_divs((struct isl_map *)set);
3045 struct isl_set *isl_map_domain(struct isl_map *map)
3048 struct isl_set *set;
3053 map = isl_map_cow(map);
3057 set = (struct isl_set *)map;
3058 set->dim = map->n_in;
3060 for (i = 0; i < map->n; ++i) {
3061 set->p[i] = isl_basic_map_domain(map->p[i]);
3065 F_CLR(set, ISL_MAP_DISJOINT);
3066 F_CLR(set, ISL_SET_NORMALIZED);
3073 struct isl_map *isl_map_union_disjoint(
3074 struct isl_map *map1, struct isl_map *map2)
3078 struct isl_map *map = NULL;
3092 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3093 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
3094 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
3096 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
3097 F_ISSET(map2, ISL_MAP_DISJOINT))
3098 FL_SET(flags, ISL_MAP_DISJOINT);
3100 map = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
3101 map1->n + map2->n, flags);
3104 for (i = 0; i < map1->n; ++i) {
3105 map = isl_map_add(map,
3106 isl_basic_map_copy(map1->p[i]));
3110 for (i = 0; i < map2->n; ++i) {
3111 map = isl_map_add(map,
3112 isl_basic_map_copy(map2->p[i]));
3126 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3128 map1 = isl_map_union_disjoint(map1, map2);
3132 F_CLR(map1, ISL_MAP_DISJOINT);
3136 struct isl_set *isl_set_union_disjoint(
3137 struct isl_set *set1, struct isl_set *set2)
3139 return (struct isl_set *)
3140 isl_map_union_disjoint(
3141 (struct isl_map *)set1, (struct isl_map *)set2);
3144 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3146 return (struct isl_set *)
3147 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3150 struct isl_map *isl_map_intersect_range(
3151 struct isl_map *map, struct isl_set *set)
3154 struct isl_map *result;
3160 if (F_ISSET(map, ISL_MAP_DISJOINT) &&
3161 F_ISSET(set, ISL_MAP_DISJOINT))
3162 FL_SET(flags, ISL_MAP_DISJOINT);
3164 result = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
3165 map->n * set->n, flags);
3168 for (i = 0; i < map->n; ++i)
3169 for (j = 0; j < set->n; ++j) {
3170 result = isl_map_add(result,
3171 isl_basic_map_intersect_range(
3172 isl_basic_map_copy(map->p[i]),
3173 isl_basic_set_copy(set->p[j])));
3186 struct isl_map *isl_map_intersect_domain(
3187 struct isl_map *map, struct isl_set *set)
3189 return isl_map_reverse(
3190 isl_map_intersect_range(isl_map_reverse(map), set));
3193 struct isl_map *isl_map_apply_domain(
3194 struct isl_map *map1, struct isl_map *map2)
3198 map1 = isl_map_reverse(map1);
3199 map1 = isl_map_apply_range(map1, map2);
3200 return isl_map_reverse(map1);
3207 struct isl_map *isl_map_apply_range(
3208 struct isl_map *map1, struct isl_map *map2)
3210 struct isl_map *result;
3216 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3217 isl_assert(map1->ctx, map1->n_out == map2->n_in, goto error);
3219 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map2->n_out,
3220 map1->n * map2->n, 0);
3223 for (i = 0; i < map1->n; ++i)
3224 for (j = 0; j < map2->n; ++j) {
3225 result = isl_map_add(result,
3226 isl_basic_map_apply_range(
3227 isl_basic_map_copy(map1->p[i]),
3228 isl_basic_map_copy(map2->p[j])));
3234 if (result && result->n <= 1)
3235 F_SET(result, ISL_MAP_DISJOINT);
3244 * returns range - domain
3246 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3248 struct isl_basic_set *bset;
3254 isl_assert(bmap->ctx, bmap->n_in == bmap->n_out, goto error);
3256 bset = isl_basic_set_from_basic_map(bmap);
3257 bset = isl_basic_set_extend(bset, bset->nparam, 3*dim, 0,
3259 bset = isl_basic_set_swap_vars(bset, 2*dim);
3260 for (i = 0; i < dim; ++i) {
3261 int j = isl_basic_map_alloc_equality(
3262 (struct isl_basic_map *)bset);
3265 isl_seq_clr(bset->eq[j],
3266 1 + bset->nparam + bset->dim + bset->n_div);
3267 isl_int_set_si(bset->eq[j][1+bset->nparam+i], 1);
3268 isl_int_set_si(bset->eq[j][1+bset->nparam+dim+i], 1);
3269 isl_int_set_si(bset->eq[j][1+bset->nparam+2*dim+i], -1);
3271 return isl_basic_set_project_out(bset, 2*dim, 0);
3273 isl_basic_map_free(bmap);
3278 * returns range - domain
3280 struct isl_set *isl_map_deltas(struct isl_map *map)
3283 struct isl_set *result;
3288 isl_assert(map->ctx, map->n_in == map->n_out, goto error);
3289 result = isl_set_alloc(map->ctx, map->nparam, map->n_in, map->n, map->flags);
3292 for (i = 0; i < map->n; ++i)
3293 result = isl_set_add(result,
3294 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3302 /* If the only constraints a div d=floor(f/m)
3303 * appears in are its two defining constraints
3306 * -(f - (m - 1)) + m d >= 0
3308 * then it can safely be removed.
3310 static int div_is_redundant(struct isl_basic_map *bmap, int div)
3313 unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
3315 for (i = 0; i < bmap->n_eq; ++i)
3316 if (!isl_int_is_zero(bmap->eq[i][pos]))
3319 for (i = 0; i < bmap->n_ineq; ++i) {
3320 if (isl_int_is_zero(bmap->ineq[i][pos]))
3322 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
3324 isl_int_sub(bmap->div[div][1],
3325 bmap->div[div][1], bmap->div[div][0]);
3326 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
3327 neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
3328 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
3329 isl_int_add(bmap->div[div][1],
3330 bmap->div[div][1], bmap->div[div][0]);
3333 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
3334 bmap->n_div-div-1) != -1)
3336 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
3337 if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
3339 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
3340 bmap->n_div-div-1) != -1)
3346 for (i = 0; i < bmap->n_div; ++i)
3347 if (!isl_int_is_zero(bmap->div[i][1+pos]))
3354 * Remove divs that don't occur in any of the constraints or other divs.
3355 * These can arise when dropping some of the variables in a quast
3356 * returned by piplib.
3358 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
3365 for (i = bmap->n_div-1; i >= 0; --i) {
3366 if (!div_is_redundant(bmap, i))
3368 bmap = isl_basic_map_drop_div(bmap, i);
3373 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
3375 bmap = remove_redundant_divs(bmap);
3378 F_SET(bmap, ISL_BASIC_SET_FINAL);
3382 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
3384 return (struct isl_basic_set *)
3385 isl_basic_map_finalize((struct isl_basic_map *)bset);
3388 struct isl_set *isl_set_finalize(struct isl_set *set)
3394 for (i = 0; i < set->n; ++i) {
3395 set->p[i] = isl_basic_set_finalize(set->p[i]);
3405 struct isl_map *isl_map_finalize(struct isl_map *map)
3411 for (i = 0; i < map->n; ++i) {
3412 map->p[i] = isl_basic_map_finalize(map->p[i]);
3416 F_CLR(map, ISL_MAP_NORMALIZED);
3423 struct isl_basic_map *isl_basic_map_identity(struct isl_ctx *ctx,
3424 unsigned nparam, unsigned dim)
3426 struct isl_basic_map *bmap;
3429 bmap = isl_basic_map_alloc(ctx, nparam, dim, dim, 0, dim, 0);
3433 for (i = 0; i < dim; ++i) {
3434 int j = isl_basic_map_alloc_equality(bmap);
3437 isl_seq_clr(bmap->eq[j],
3438 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div);
3439 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3440 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3442 return isl_basic_map_finalize(bmap);
3444 isl_basic_map_free(bmap);
3448 struct isl_map *isl_map_identity(struct isl_ctx *ctx,
3449 unsigned nparam, unsigned dim)
3451 struct isl_map *map = isl_map_alloc(ctx, nparam, dim, dim, 1,
3455 map = isl_map_add(map,
3456 isl_basic_map_identity(ctx, nparam, dim));
3463 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3465 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3468 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3470 return isl_map_is_subset(
3471 (struct isl_map *)set1, (struct isl_map *)set2);
3474 int isl_basic_map_is_subset(
3475 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3478 struct isl_map *map1;
3479 struct isl_map *map2;
3481 if (!bmap1 || !bmap2)
3484 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3485 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3487 is_subset = isl_map_is_subset(map1, map2);
3495 int isl_basic_map_is_equal(
3496 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3500 if (!bmap1 || !bmap2)
3502 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3505 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3509 int isl_basic_set_is_equal(
3510 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3512 return isl_basic_map_is_equal(
3513 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3516 int isl_map_is_empty(struct isl_map *map)
3523 for (i = 0; i < map->n; ++i) {
3524 is_empty = isl_basic_map_is_empty(map->p[i]);
3533 int isl_set_is_empty(struct isl_set *set)
3535 return isl_map_is_empty((struct isl_map *)set);
3538 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3542 struct isl_map *diff;
3547 if (isl_map_is_empty(map1))
3550 if (isl_map_is_empty(map2))
3553 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3557 is_subset = isl_map_is_empty(diff);
3563 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3569 is_subset = isl_map_is_subset(map1, map2);
3572 is_subset = isl_map_is_subset(map2, map1);
3576 int isl_basic_map_is_strict_subset(
3577 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3581 if (!bmap1 || !bmap2)
3583 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3586 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3587 if (is_subset == -1)
3592 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3598 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3599 if (total != vec->size)
3604 for (i = 0; i < bmap->n_eq; ++i) {
3605 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3606 if (!isl_int_is_zero(s)) {
3612 for (i = 0; i < bmap->n_ineq; ++i) {
3613 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3614 if (isl_int_is_neg(s)) {
3625 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3629 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3632 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3634 struct isl_basic_set *bset = NULL;
3635 struct isl_vec *sample = NULL;
3642 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3645 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3646 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3647 copy = isl_basic_map_convex_hull(copy);
3648 empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3649 isl_basic_map_free(copy);
3653 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3654 if (bmap->sample && bmap->sample->size == total) {
3655 int contains = basic_map_contains(bmap, bmap->sample);
3661 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3664 sample = isl_basic_set_sample(bset);
3667 empty = sample->size == 0;
3669 isl_vec_free(bmap->ctx, bmap->sample);
3670 bmap->sample = sample;
3675 struct isl_map *isl_basic_map_union(
3676 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3678 struct isl_map *map;
3679 if (!bmap1 || !bmap2)
3682 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
3683 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
3684 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
3686 map = isl_map_alloc(bmap1->ctx, bmap1->nparam,
3687 bmap1->n_in, bmap1->n_out, 2, 0);
3690 map = isl_map_add(map, bmap1);
3691 map = isl_map_add(map, bmap2);
3694 isl_basic_map_free(bmap1);
3695 isl_basic_map_free(bmap2);
3699 struct isl_set *isl_basic_set_union(
3700 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3702 return (struct isl_set *)isl_basic_map_union(
3703 (struct isl_basic_map *)bset1,
3704 (struct isl_basic_map *)bset2);
3707 /* Order divs such that any div only depends on previous divs */
3708 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3711 unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
3713 for (i = 0; i < bmap->n_div; ++i) {
3715 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3719 swap_div(bmap, i, pos);
3725 static int find_div(struct isl_basic_map *dst,
3726 struct isl_basic_map *src, unsigned div)
3730 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3732 for (i = 0; i < dst->n_div; ++i)
3733 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total) &&
3734 isl_seq_first_non_zero(dst->div[i]+1+1+total,
3735 dst->n_div - src->n_div) == -1)
3740 struct isl_basic_map *isl_basic_map_align_divs(
3741 struct isl_basic_map *dst, struct isl_basic_map *src)
3744 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3749 if (src->n_div == 0)
3752 src = order_divs(src);
3753 dst = isl_basic_map_extend(dst, dst->nparam, dst->n_in,
3754 dst->n_out, src->n_div, 0, 2 * src->n_div);
3757 for (i = 0; i < src->n_div; ++i) {
3758 int j = find_div(dst, src, i);
3760 j = isl_basic_map_alloc_div(dst);
3763 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
3764 isl_seq_clr(dst->div[j]+1+1+total,
3765 dst->n_div - src->n_div);
3766 if (add_div_constraints(dst, j) < 0)
3770 swap_div(dst, i, j);
3774 isl_basic_map_free(dst);
3778 struct isl_basic_set *isl_basic_set_align_divs(
3779 struct isl_basic_set *dst, struct isl_basic_set *src)
3781 return (struct isl_basic_set *)isl_basic_map_align_divs(
3782 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3785 struct isl_map *isl_map_align_divs(struct isl_map *map)
3789 map = isl_map_compute_divs(map);
3790 map = isl_map_cow(map);
3794 for (i = 1; i < map->n; ++i)
3795 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3796 for (i = 1; i < map->n; ++i)
3797 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3799 F_CLR(map, ISL_MAP_NORMALIZED);
3803 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3804 struct isl_basic_map *src, isl_int *c,
3805 unsigned len, int oppose)
3807 struct isl_basic_map *copy = NULL;
3812 copy = isl_basic_map_copy(src);
3813 copy = isl_basic_map_cow(copy);
3816 copy = isl_basic_map_extend(copy,
3817 copy->nparam, copy->n_in, copy->n_out, 0, 0, 1);
3818 k = isl_basic_map_alloc_inequality(copy);
3822 isl_seq_neg(copy->ineq[k], c, len);
3824 isl_seq_cpy(copy->ineq[k], c, len);
3825 total = 1 + copy->nparam + copy->n_in + copy->n_out + copy->n_div;
3826 isl_seq_clr(copy->ineq[k]+len, total - len);
3827 isl_inequality_negate(copy, k);
3828 copy = isl_basic_map_simplify(copy);
3829 copy = isl_basic_map_finalize(copy);
3830 is_empty = isl_basic_map_is_empty(copy);
3834 dst = isl_map_add(dst, copy);
3836 isl_basic_map_free(copy);
3839 isl_basic_map_free(copy);
3844 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3848 struct isl_map *rest = NULL;
3850 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3857 if (F_ISSET(map, ISL_MAP_DISJOINT))
3858 FL_SET(flags, ISL_MAP_DISJOINT);
3860 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3861 rest = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
3866 for (i = 0; i < map->n; ++i) {
3867 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3872 for (j = 0; j < map->n; ++j)
3873 map->p[j] = isl_basic_map_cow(map->p[j]);
3875 for (i = 0; i < bmap->n_eq; ++i) {
3876 for (j = 0; j < map->n; ++j) {
3877 rest = add_cut_constraint(rest,
3878 map->p[j], bmap->eq[i], 1+total, 0);
3882 rest = add_cut_constraint(rest,
3883 map->p[j], bmap->eq[i], 1+total, 1);
3887 map->p[j] = isl_basic_map_extend(map->p[j],
3888 map->p[j]->nparam, map->p[j]->n_in,
3889 map->p[j]->n_out, 0, 1, 0);
3892 k = isl_basic_map_alloc_equality(map->p[j]);
3895 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3896 isl_seq_clr(map->p[j]->eq[k]+1+total,
3897 map->p[j]->n_div - bmap->n_div);
3901 for (i = 0; i < bmap->n_ineq; ++i) {
3902 for (j = 0; j < map->n; ++j) {
3903 rest = add_cut_constraint(rest,
3904 map->p[j], bmap->ineq[i], 1+total, 0);
3908 map->p[j] = isl_basic_map_extend(map->p[j],
3909 map->p[j]->nparam, map->p[j]->n_in,
3910 map->p[j]->n_out, 0, 0, 1);
3913 k = isl_basic_map_alloc_inequality(map->p[j]);
3916 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3917 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3918 map->p[j]->n_div - bmap->n_div);
3930 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3936 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3937 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
3938 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
3940 if (isl_map_is_empty(map2)) {
3945 map1 = isl_map_compute_divs(map1);
3946 map2 = isl_map_compute_divs(map2);
3950 for (i = 0; map1 && i < map2->n; ++i)
3951 map1 = subtract(map1, map2->p[i]);
3961 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3963 return (struct isl_set *)
3965 (struct isl_map *)set1, (struct isl_map *)set2);
3968 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3972 isl_assert(set->ctx, set->dim == map->n_in, goto error);
3973 map = isl_map_intersect_domain(map, set);
3974 set = isl_map_range(map);
3982 /* There is no need to cow as removing empty parts doesn't change
3983 * the meaning of the set.
3985 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3992 for (i = map->n-1; i >= 0; --i) {
3993 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3995 isl_basic_map_free(map->p[i]);
3996 if (i != map->n-1) {
3997 F_CLR(map, ISL_MAP_NORMALIZED);
3998 map->p[i] = map->p[map->n-1];
4006 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
4008 return (struct isl_set *)
4009 isl_map_remove_empty_parts((struct isl_map *)set);
4012 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
4014 struct isl_basic_set *bset;
4015 if (!set || set->n == 0)
4017 bset = set->p[set->n-1];
4018 isl_assert(set->ctx, F_ISSET(bset, ISL_BASIC_SET_FINAL), return NULL);
4019 return isl_basic_set_copy(bset);
4022 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
4023 struct isl_basic_set *bset)
4029 for (i = set->n-1; i >= 0; --i) {
4030 if (set->p[i] != bset)
4032 set = isl_set_cow(set);
4035 isl_basic_set_free(set->p[i]);
4036 if (i != set->n-1) {
4037 F_CLR(set, ISL_SET_NORMALIZED);
4038 set->p[i] = set->p[set->n-1];
4043 isl_basic_set_free(bset);
4047 isl_basic_set_free(bset);
4051 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4052 * for any common value of the parameters and dimensions preceding dim
4053 * in both basic sets, the values of dimension pos in bset1 are
4054 * smaller or larger then those in bset2.
4057 * 1 if bset1 follows bset2
4058 * -1 if bset1 precedes bset2
4059 * 0 if bset1 and bset2 are incomparable
4060 * -2 if some error occurred.
4062 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4063 struct isl_basic_set *bset2, int pos)
4065 struct isl_basic_map *bmap1 = NULL;
4066 struct isl_basic_map *bmap2 = NULL;
4067 struct isl_ctx *ctx;
4068 struct isl_vec *obj;
4071 enum isl_lp_result res;
4074 if (!bset1 || !bset2)
4077 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1),
4078 pos, bset1->dim - pos);
4079 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2),
4080 pos, bset2->dim - pos);
4081 if (!bmap1 || !bmap2)
4083 bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
4084 bmap1->n_in, bmap1->n_out + bmap2->n_out,
4085 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4088 total = bmap1->nparam + bmap1->n_in + bmap1->n_out + bmap1->n_div;
4090 obj = isl_vec_alloc(ctx, total);
4091 isl_seq_clr(obj->block.data, total);
4092 isl_int_set_si(obj->block.data[bmap1->nparam+bmap1->n_in], 1);
4093 isl_int_set_si(obj->block.data[bmap1->nparam+bmap1->n_in+
4094 bmap1->n_out-bmap2->n_out], -1);
4097 bmap1 = add_constraints(bmap1, bmap2, 0, bmap1->n_out - bmap2->n_out);
4100 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
4101 if (res == isl_lp_empty)
4103 else if (res == isl_lp_ok && isl_int_is_pos(num))
4105 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4106 res == isl_lp_unbounded)
4112 isl_basic_map_free(bmap1);
4113 isl_vec_free(ctx, obj);
4116 isl_basic_map_free(bmap1);
4117 isl_basic_map_free(bmap2);
4121 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4122 unsigned pos, isl_int *val)
4130 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
4131 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4132 for (; d+1 > pos; --d)
4133 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4137 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4139 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4141 if (!isl_int_is_one(bmap->eq[i][1+d]))
4144 isl_int_neg(*val, bmap->eq[i][0]);
4150 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4151 unsigned pos, isl_int *val)
4163 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4166 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4167 for (i = 1; fixed == 1 && i < map->n; ++i) {
4168 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4169 if (fixed == 1 && isl_int_ne(tmp, v))
4173 isl_int_set(*val, v);
4179 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4182 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4185 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4186 * then return this fixed value in *val.
4188 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4190 return isl_set_fast_has_fixed_var(set, set->nparam + dim, val);
4193 /* Check if input variable in has fixed value and if so and if val is not NULL,
4194 * then return this fixed value in *val.
4196 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4198 return isl_map_fast_has_fixed_var(map, map->nparam + in, val);
4201 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4202 * and if val is not NULL, then return this lower bound in *val.
4204 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4205 unsigned dim, isl_int *val)
4207 int i, i_eq = -1, i_ineq = -1;
4213 total = bset->nparam + bset->dim + bset->n_div;
4214 for (i = 0; i < bset->n_eq; ++i) {
4215 if (isl_int_is_zero(bset->eq[i][1+bset->nparam+dim]))
4221 for (i = 0; i < bset->n_ineq; ++i) {
4222 if (!isl_int_is_pos(bset->ineq[i][1+bset->nparam+dim]))
4224 if (i_eq != -1 || i_ineq != -1)
4228 if (i_eq == -1 && i_ineq == -1)
4230 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4231 /* The coefficient should always be one due to normalization. */
4232 if (!isl_int_is_one(c[1+bset->nparam+dim]))
4234 if (isl_seq_first_non_zero(c+1, bset->nparam+dim) != -1)
4236 if (isl_seq_first_non_zero(c+1+bset->nparam+dim+1,
4237 total - bset->nparam - dim - 1) != -1)
4240 isl_int_neg(*val, c[0]);
4244 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4245 unsigned dim, isl_int *val)
4257 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4261 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4263 for (i = 1; fixed == 1 && i < set->n; ++i) {
4264 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4266 if (fixed == 1 && isl_int_ne(tmp, v))
4270 isl_int_set(*val, v);
4276 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
4277 struct isl_basic_set *bset, struct isl_basic_set *context)
4282 if (!bset || !context)
4285 bset = isl_basic_set_cow(bset);
4289 elim = isl_alloc_array(ctx, int, bset->dim);
4292 set_compute_elimination_index(context, elim);
4293 for (i = 0; i < bset->n_eq; ++i)
4294 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
4296 for (i = 0; i < bset->n_ineq; ++i)
4297 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
4299 isl_basic_set_free(context);
4301 bset = isl_basic_set_simplify(bset);
4302 bset = isl_basic_set_finalize(bset);
4305 isl_basic_set_free(bset);
4306 isl_basic_set_free(context);
4310 static struct isl_basic_set *remove_shifted_constraints(
4311 struct isl_basic_set *bset, struct isl_basic_set *context)
4321 size = round_up(4 * (context->n_ineq+1) / 3 - 1);
4322 bits = ffs(size) - 1;
4323 index = isl_alloc_array(ctx, isl_int **, size);
4324 memset(index, 0, size * sizeof(isl_int **));
4328 for (k = 0; k < context->n_ineq; ++k) {
4329 h = set_hash_index(index, size, bits, context, k);
4330 index[h] = &context->ineq[k];
4332 for (k = 0; k < bset->n_ineq; ++k) {
4333 h = set_hash_index(index, size, bits, bset, k);
4336 l = index[h] - &context->ineq[0];
4337 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
4339 bset = isl_basic_set_cow(bset);
4342 isl_basic_set_drop_inequality(bset, k);
4352 /* Remove all information from bset that is redundant in the context
4353 * of context. In particular, equalities that are linear combinations
4354 * of those in context are removed. Then the inequalities that are
4355 * redundant in the context of the equalities and inequalities of
4356 * context are removed.
4358 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
4359 struct isl_basic_set *context)
4363 struct isl_basic_set *combined;
4364 struct isl_ctx *ctx;
4366 if (!bset || !context)
4369 if (context->n_eq > 0) {
4372 struct isl_ctx *ctx = context->ctx;
4373 struct isl_basic_set *reduced_context;
4374 reduced_context = isl_basic_set_remove_equalities(
4375 isl_basic_set_copy(context), &T, &T2);
4376 if (!reduced_context)
4378 bset = isl_basic_set_preimage(ctx, bset, T);
4379 bset = uset_gist(bset, reduced_context);
4380 bset = isl_basic_set_preimage(ctx, bset, T2);
4381 bset = isl_basic_set_reduce_using_equalities(bset, context);
4384 if (!context->n_ineq)
4386 bset = remove_shifted_constraints(bset, context);
4387 combined = isl_basic_set_extend(isl_basic_set_copy(bset),
4388 0, bset->dim, 0, context->n_eq, context->n_ineq);
4389 context = set_add_constraints(combined, context, 0);
4394 for (i = bset->n_ineq-1; i >= 0; --i) {
4396 set_swap_inequality(context, i, context->n_ineq-1);
4398 redundant = isl_basic_set_constraint_is_redundant(&context,
4399 context->ineq[context->n_ineq], &opt, NULL);
4400 if (redundant == -1) {
4404 if (F_ISSET(context, ISL_BASIC_MAP_EMPTY)) {
4405 bset = isl_basic_set_set_to_empty(bset);
4409 set_swap_inequality(context, i, context->n_ineq-1);
4411 isl_basic_set_drop_inequality(context, i);
4412 isl_basic_set_drop_inequality(bset, i);
4417 isl_basic_set_free(context);
4420 isl_basic_set_free(context);
4421 isl_basic_set_free(bset);
4425 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
4426 struct isl_basic_map *context)
4428 struct isl_basic_set *bset;
4430 if (!bmap || !context)
4433 context = isl_basic_map_align_divs(context, bmap);
4434 bmap = isl_basic_map_align_divs(bmap, context);
4436 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
4437 isl_basic_map_underlying_set(context));
4439 return isl_basic_map_overlying_set(bset, bmap);
4441 isl_basic_map_free(bmap);
4442 isl_basic_map_free(context);
4446 struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
4450 map = isl_map_cow(map);
4451 if (!map || !context)
4453 isl_assert(map->ctx, map->nparam == context->nparam, goto error);
4454 isl_assert(map->ctx, map->n_in == context->n_in, goto error);
4455 isl_assert(map->ctx, map->n_out == context->n_out, goto error);
4456 for (i = 0; i < map->n; ++i)
4457 context = isl_basic_map_align_divs(context, map->p[i]);
4458 for (i = 0; i < map->n; ++i) {
4459 map->p[i] = isl_basic_map_gist(map->p[i],
4460 isl_basic_map_copy(context));
4464 isl_basic_map_free(context);
4465 F_CLR(map, ISL_MAP_NORMALIZED);
4469 isl_basic_map_free(context);
4473 struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
4475 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
4476 (struct isl_basic_map *)context);
4484 static int qsort_constraint_cmp(const void *p1, const void *p2)
4486 const struct constraint *c1 = (const struct constraint *)p1;
4487 const struct constraint *c2 = (const struct constraint *)p2;
4488 unsigned size = isl_min(c1->size, c2->size);
4489 return isl_seq_cmp(c1->c, c2->c, size);
4492 static struct isl_basic_map *isl_basic_map_sort_constraints(
4493 struct isl_basic_map *bmap)
4496 struct constraint *c;
4501 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
4502 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4505 for (i = 0; i < bmap->n_ineq; ++i) {
4507 c[i].c = bmap->ineq[i];
4509 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4510 for (i = 0; i < bmap->n_ineq; ++i)
4511 bmap->ineq[i] = c[i].c;
4515 isl_basic_map_free(bmap);
4519 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4523 if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4525 bmap = isl_basic_map_convex_hull(bmap);
4526 bmap = isl_basic_map_sort_constraints(bmap);
4527 F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4531 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4533 return (struct isl_basic_set *)isl_basic_map_normalize(
4534 (struct isl_basic_map *)bset);
4537 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4538 const struct isl_basic_map *bmap2)
4545 if (bmap1->nparam != bmap2->nparam)
4546 return bmap1->nparam - bmap2->nparam;
4547 if (bmap1->n_in != bmap2->n_in)
4548 return bmap1->n_in - bmap2->n_in;
4549 if (bmap1->n_out != bmap2->n_out)
4550 return bmap1->n_out - bmap2->n_out;
4551 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4552 F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4554 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4556 if (F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4558 if (bmap1->n_eq != bmap2->n_eq)
4559 return bmap1->n_eq - bmap2->n_eq;
4560 if (bmap1->n_ineq != bmap2->n_ineq)
4561 return bmap1->n_ineq - bmap2->n_ineq;
4562 if (bmap1->n_div != bmap2->n_div)
4563 return bmap1->n_div - bmap2->n_div;
4564 total = bmap1->nparam + bmap1->n_in + bmap1->n_out + bmap1->n_div;
4565 for (i = 0; i < bmap1->n_eq; ++i) {
4566 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4570 for (i = 0; i < bmap1->n_ineq; ++i) {
4571 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4575 for (i = 0; i < bmap1->n_div; ++i) {
4576 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4583 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4584 struct isl_basic_map *bmap2)
4586 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4589 static int qsort_bmap_cmp(const void *p1, const void *p2)
4591 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4592 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4594 return isl_basic_map_fast_cmp(bmap1, bmap2);
4597 /* We normalize in place, but if anything goes wrong we need
4598 * to return NULL, so we need to make sure we don't change the
4599 * meaning of any possible other copies of map.
4601 struct isl_map *isl_map_normalize(struct isl_map *map)
4604 struct isl_basic_map *bmap;
4608 if (F_ISSET(map, ISL_MAP_NORMALIZED))
4610 for (i = 0; i < map->n; ++i) {
4611 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4614 isl_basic_map_free(map->p[i]);
4617 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4618 F_SET(map, ISL_MAP_NORMALIZED);
4619 map = isl_map_remove_empty_parts(map);
4622 for (i = map->n - 1; i >= 1; --i) {
4623 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4625 isl_basic_map_free(map->p[i-1]);
4626 for (j = i; j < map->n; ++j)
4627 map->p[j-1] = map->p[j];
4637 struct isl_set *isl_set_normalize(struct isl_set *set)
4639 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4642 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4652 if (map1->nparam != map2->nparam)
4654 if (map1->n_in != map2->n_in)
4656 if (map1->n_out != map2->n_out)
4659 map1 = isl_map_copy(map1);
4660 map2 = isl_map_copy(map2);
4661 map1 = isl_map_normalize(map1);
4662 map2 = isl_map_normalize(map2);
4665 equal = map1->n == map2->n;
4666 for (i = 0; equal && i < map1->n; ++i) {
4667 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4680 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4682 return isl_map_fast_is_equal((struct isl_map *)set1,
4683 (struct isl_map *)set2);
4686 /* Return an interval that ranges from min to max (inclusive)
4688 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4689 isl_int min, isl_int max)
4692 struct isl_basic_set *bset = NULL;
4694 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4698 k = isl_basic_set_alloc_inequality(bset);
4701 isl_int_set_si(bset->ineq[k][1], 1);
4702 isl_int_neg(bset->ineq[k][0], min);
4704 k = isl_basic_set_alloc_inequality(bset);
4707 isl_int_set_si(bset->ineq[k][1], -1);
4708 isl_int_set(bset->ineq[k][0], max);
4712 isl_basic_set_free(bset);
4716 /* Return the Cartesian product of the basic sets in list (in the given order).
4718 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4726 struct isl_basic_set *product = NULL;
4730 isl_assert(list->ctx, list->n > 0, goto error);
4731 isl_assert(list->ctx, list->p[0], goto error);
4732 nparam = list->p[0]->nparam;
4733 dim = list->p[0]->dim;
4734 extra = list->p[0]->n_div;
4735 n_eq = list->p[0]->n_eq;
4736 n_ineq = list->p[0]->n_ineq;
4737 for (i = 1; i < list->n; ++i) {
4738 isl_assert(list->ctx, list->p[i], goto error);
4739 isl_assert(list->ctx, nparam == list->p[i]->nparam, goto error);
4740 dim += list->p[i]->dim;
4741 extra += list->p[i]->n_div;
4742 n_eq += list->p[i]->n_eq;
4743 n_ineq += list->p[i]->n_ineq;
4745 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4750 for (i = 0; i < list->n; ++i) {
4751 set_add_constraints(product,
4752 isl_basic_set_copy(list->p[i]), dim);
4753 dim += list->p[i]->dim;
4755 isl_basic_set_list_free(list);
4758 isl_basic_set_free(product);
4759 isl_basic_set_list_free(list);
4763 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4771 bset = isl_basic_set_copy(bset);
4772 bset = isl_basic_set_normalize(bset);
4775 total = bset->nparam + bset->dim + bset->n_div;
4776 isl_hash_byte(hash, bset->n_eq & 0xFF);
4777 for (i = 0; i < bset->n_eq; ++i) {
4779 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4780 isl_hash_hash(hash, c_hash);
4782 isl_hash_byte(hash, bset->n_ineq & 0xFF);
4783 for (i = 0; i < bset->n_ineq; ++i) {
4785 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4786 isl_hash_hash(hash, c_hash);
4788 isl_hash_byte(hash, bset->n_div & 0xFF);
4789 for (i = 0; i < bset->n_div; ++i) {
4791 if (isl_int_is_zero(bset->div[i][0]))
4793 isl_hash_byte(hash, i & 0xFF);
4794 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4795 isl_hash_hash(hash, c_hash);
4797 isl_basic_set_free(bset);
4801 uint32_t isl_set_get_hash(struct isl_set *set)
4808 set = isl_set_copy(set);
4809 set = isl_set_normalize(set);
4813 isl_hash_init(hash);
4814 for (i = 0; i < set->n; ++i) {
4816 bset_hash = isl_basic_set_get_hash(set->p[i]);
4817 isl_hash_hash(hash, bset_hash);