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];
240 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
242 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
245 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
249 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
254 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
259 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
261 if (pos != bmap->n_eq - 1) {
263 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
264 bmap->eq[bmap->n_eq - 1] = t;
270 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
272 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
275 void isl_basic_map_inequality_to_equality(
276 struct isl_basic_map *bmap, unsigned pos)
281 bmap->ineq[pos] = bmap->ineq[0];
282 bmap->ineq[0] = bmap->eq[bmap->n_eq];
283 bmap->eq[bmap->n_eq] = t;
287 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
290 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
296 isl_assert(ctx, (bmap->ineq - bmap->eq) + bmap->n_ineq < bmap->c_size,
298 F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
299 F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
300 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
301 return bmap->n_ineq++;
304 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
306 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
309 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
313 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
318 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
320 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
323 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
328 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
330 if (pos != bmap->n_ineq - 1) {
332 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
333 bmap->ineq[bmap->n_ineq - 1] = t;
334 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
340 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
342 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
345 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
349 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
350 return bmap->n_div++;
353 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
357 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
362 /* Copy constraint from src to dst, putting the vars of src at offset
363 * dim_off in dst and the divs of src at offset div_off in dst.
364 * If both sets are actually map, then dim_off applies to the input
367 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
368 struct isl_basic_map *src_map, isl_int *src,
369 unsigned in_off, unsigned out_off, unsigned div_off)
371 isl_int_set(dst[0], src[0]);
372 isl_seq_cpy(dst+1, src+1, isl_min(dst_map->nparam, src_map->nparam));
373 if (dst_map->nparam > src_map->nparam)
374 isl_seq_clr(dst+1+src_map->nparam,
375 dst_map->nparam - src_map->nparam);
376 isl_seq_clr(dst+1+dst_map->nparam, in_off);
377 isl_seq_cpy(dst+1+dst_map->nparam+in_off,
378 src+1+src_map->nparam,
379 isl_min(dst_map->n_in-in_off, src_map->n_in));
380 if (dst_map->n_in-in_off > src_map->n_in)
381 isl_seq_clr(dst+1+dst_map->nparam+in_off+src_map->n_in,
382 dst_map->n_in - in_off - src_map->n_in);
383 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in, out_off);
384 isl_seq_cpy(dst+1+dst_map->nparam+dst_map->n_in+out_off,
385 src+1+src_map->nparam+src_map->n_in,
386 isl_min(dst_map->n_out-out_off, src_map->n_out));
387 if (dst_map->n_out-out_off > src_map->n_out)
388 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+out_off+src_map->n_out,
389 dst_map->n_out - out_off - src_map->n_out);
390 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out, div_off);
391 isl_seq_cpy(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out+div_off,
392 src+1+src_map->nparam+src_map->n_in+src_map->n_out,
393 isl_min(dst_map->extra-div_off, src_map->extra));
394 if (dst_map->extra-div_off > src_map->extra)
395 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out+
396 div_off+src_map->extra,
397 dst_map->extra - div_off - src_map->extra);
400 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
401 struct isl_basic_map *src_map, isl_int *src,
402 unsigned in_off, unsigned out_off, unsigned div_off)
404 isl_int_set(dst[0], src[0]);
405 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
408 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
409 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
414 if (!bmap1 || !bmap2)
417 div_off = bmap1->n_div;
419 for (i = 0; i < bmap2->n_eq; ++i) {
420 int i1 = isl_basic_map_alloc_equality(bmap1);
423 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
424 i_pos, o_pos, div_off);
427 for (i = 0; i < bmap2->n_ineq; ++i) {
428 int i1 = isl_basic_map_alloc_inequality(bmap1);
431 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
432 i_pos, o_pos, div_off);
435 for (i = 0; i < bmap2->n_div; ++i) {
436 int i1 = isl_basic_map_alloc_div(bmap1);
439 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
440 i_pos, o_pos, div_off);
443 isl_basic_map_free(bmap2);
448 isl_basic_map_free(bmap1);
449 isl_basic_map_free(bmap2);
453 static struct isl_basic_set *set_add_constraints(struct isl_basic_set *bset1,
454 struct isl_basic_set *bset2, unsigned pos)
456 return (struct isl_basic_set *)
457 add_constraints((struct isl_basic_map *)bset1,
458 (struct isl_basic_map *)bset2, 0, pos);
461 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
462 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
463 unsigned n_eq, unsigned n_ineq)
465 struct isl_basic_map *ext;
469 base = isl_basic_map_cow(base);
473 dims_ok = base && base->nparam == nparam &&
474 base->n_in == n_in && base->n_out == n_out &&
475 base->extra >= base->n_div + extra;
477 if (dims_ok && n_eq == 0 && n_ineq == 0)
480 isl_assert(base->ctx, base->nparam <= nparam, goto error);
481 isl_assert(base->ctx, base->n_in <= n_in, goto error);
482 isl_assert(base->ctx, base->n_out <= n_out, goto error);
483 extra += base->extra;
485 n_ineq += base->n_ineq;
487 ext = isl_basic_map_alloc(base->ctx, nparam, n_in, n_out,
488 extra, n_eq, n_ineq);
495 ext = add_constraints(ext, base, 0, 0);
498 F_CLR(ext, ISL_BASIC_SET_FINAL);
504 isl_basic_map_free(base);
508 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
509 unsigned nparam, unsigned dim, unsigned extra,
510 unsigned n_eq, unsigned n_ineq)
512 return (struct isl_basic_set *)
513 isl_basic_map_extend((struct isl_basic_map *)base,
514 nparam, 0, dim, extra, n_eq, n_ineq);
517 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
519 return (struct isl_basic_set *)
520 isl_basic_map_cow((struct isl_basic_map *)bset);
523 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
530 bmap = isl_basic_map_dup(bmap);
532 F_CLR(bmap, ISL_BASIC_SET_FINAL);
536 struct isl_set *isl_set_cow(struct isl_set *set)
544 return isl_set_dup(set);
547 struct isl_map *isl_map_cow(struct isl_map *map)
555 return isl_map_dup(map);
558 static void swap_vars(struct isl_blk blk, isl_int *a,
559 unsigned a_len, unsigned b_len)
561 isl_seq_cpy(blk.data, a+a_len, b_len);
562 isl_seq_cpy(blk.data+b_len, a, a_len);
563 isl_seq_cpy(a, blk.data, b_len+a_len);
566 struct isl_basic_set *isl_basic_set_swap_vars(
567 struct isl_basic_set *bset, unsigned n)
575 isl_assert(bset->ctx, n <= bset->dim, goto error);
580 bset = isl_basic_set_cow(bset);
584 blk = isl_blk_alloc(bset->ctx, bset->dim);
585 if (isl_blk_is_error(blk))
588 for (i = 0; i < bset->n_eq; ++i)
590 bset->eq[i]+1+bset->nparam, n, bset->dim - n);
592 for (i = 0; i < bset->n_ineq; ++i)
594 bset->ineq[i]+1+bset->nparam, n, bset->dim - n);
596 for (i = 0; i < bset->n_div; ++i)
598 bset->div[i]+1+1+bset->nparam, n, bset->dim - n);
600 isl_blk_free(bset->ctx, blk);
602 F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
606 isl_basic_set_free(bset);
610 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
613 set = isl_set_cow(set);
617 for (i = 0; i < set->n; ++i) {
618 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
624 F_CLR(set, ISL_SET_NORMALIZED);
628 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
630 isl_seq_cpy(c, c + n, rem);
631 isl_seq_clr(c + rem, n);
634 struct isl_basic_set *isl_basic_set_drop_dims(
635 struct isl_basic_set *bset, unsigned first, unsigned n)
642 isl_assert(bset->ctx, first + n <= bset->dim, goto error);
647 bset = isl_basic_set_cow(bset);
651 for (i = 0; i < bset->n_eq; ++i)
652 constraint_drop_vars(bset->eq[i]+1+bset->nparam+first, n,
653 (bset->dim-first-n)+bset->extra);
655 for (i = 0; i < bset->n_ineq; ++i)
656 constraint_drop_vars(bset->ineq[i]+1+bset->nparam+first, n,
657 (bset->dim-first-n)+bset->extra);
659 for (i = 0; i < bset->n_div; ++i)
660 constraint_drop_vars(bset->div[i]+1+1+bset->nparam+first, n,
661 (bset->dim-first-n)+bset->extra);
666 F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
667 bset = isl_basic_set_simplify(bset);
668 return isl_basic_set_finalize(bset);
670 isl_basic_set_free(bset);
674 static struct isl_set *isl_set_drop_dims(
675 struct isl_set *set, unsigned first, unsigned n)
682 isl_assert(set->ctx, first + n <= set->dim, goto error);
686 set = isl_set_cow(set);
690 for (i = 0; i < set->n; ++i) {
691 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
697 F_CLR(set, ISL_SET_NORMALIZED);
704 struct isl_basic_map *isl_basic_map_drop_inputs(
705 struct isl_basic_map *bmap, unsigned first, unsigned n)
712 isl_assert(bmap->ctx, first + n <= bmap->n_in, goto error);
717 bmap = isl_basic_map_cow(bmap);
721 for (i = 0; i < bmap->n_eq; ++i)
722 constraint_drop_vars(bmap->eq[i]+1+bmap->nparam+first, n,
723 (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
725 for (i = 0; i < bmap->n_ineq; ++i)
726 constraint_drop_vars(bmap->ineq[i]+1+bmap->nparam+first, n,
727 (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
729 for (i = 0; i < bmap->n_div; ++i)
730 constraint_drop_vars(bmap->div[i]+1+1+bmap->nparam+first, n,
731 (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
736 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
737 bmap = isl_basic_map_simplify(bmap);
738 return isl_basic_map_finalize(bmap);
740 isl_basic_map_free(bmap);
744 static struct isl_map *isl_map_drop_inputs(
745 struct isl_map *map, unsigned first, unsigned n)
752 isl_assert(map->ctx, first + n <= map->n_in, goto error);
756 map = isl_map_cow(map);
760 for (i = 0; i < map->n; ++i) {
761 map->p[i] = isl_basic_map_drop_inputs(map->p[i], first, n);
766 F_CLR(map, ISL_MAP_NORMALIZED);
775 * We don't cow, as the div is assumed to be redundant.
777 static struct isl_basic_map *isl_basic_map_drop_div(
778 struct isl_basic_map *bmap, unsigned div)
786 pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
788 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
790 for (i = 0; i < bmap->n_eq; ++i)
791 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
793 for (i = 0; i < bmap->n_ineq; ++i) {
794 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
795 isl_basic_map_drop_inequality(bmap, i);
799 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
802 for (i = 0; i < bmap->n_div; ++i)
803 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
805 if (div != bmap->n_div - 1) {
807 isl_int *t = bmap->div[div];
809 for (j = div; j < bmap->n_div - 1; ++j)
810 bmap->div[j] = bmap->div[j+1];
812 bmap->div[bmap->n_div - 1] = t;
814 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
815 isl_basic_map_free_div(bmap, 1);
819 isl_basic_map_free(bmap);
823 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
826 unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
828 len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
830 for (i = 0; i < bmap->n_eq; ++i)
831 if (bmap->eq[i] != eq)
832 isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
834 for (i = 0; i < bmap->n_ineq; ++i)
835 isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
837 /* We need to be careful about circular definitions,
838 * so for now we just remove the definitions of other divs that
839 * depend on this div and (possibly) recompute them later.
841 for (i = 0; i < bmap->n_div; ++i)
842 if (!isl_int_is_zero(bmap->div[i][0]) &&
843 !isl_int_is_zero(bmap->div[i][1 + pos]))
844 isl_seq_clr(bmap->div[i], 1 + len);
846 isl_basic_map_drop_div(bmap, div);
849 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
855 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
856 isl_basic_map_free_div(bmap, bmap->n_div);
857 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
859 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
861 isl_basic_map_alloc_equality(bmap);
865 isl_int_set_si(bmap->eq[i][0], 1);
866 isl_seq_clr(bmap->eq[i]+1, total);
867 F_SET(bmap, ISL_BASIC_MAP_EMPTY);
868 return isl_basic_map_finalize(bmap);
870 isl_basic_map_free(bmap);
874 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
876 return (struct isl_basic_set *)
877 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
880 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
882 isl_int *t = bmap->eq[a];
883 bmap->eq[a] = bmap->eq[b];
887 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
890 isl_int *t = bmap->ineq[a];
891 bmap->ineq[a] = bmap->ineq[b];
896 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
898 swap_inequality((struct isl_basic_map *)bset, a, b);
901 static void swap_div(struct isl_basic_map *bmap, int a, int b)
904 unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
905 isl_int *t = bmap->div[a];
906 bmap->div[a] = bmap->div[b];
909 for (i = 0; i < bmap->n_eq; ++i)
910 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
912 for (i = 0; i < bmap->n_ineq; ++i)
913 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
915 for (i = 0; i < bmap->n_div; ++i)
916 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
917 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
920 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
921 unsigned pos, isl_int *eq, int *progress)
926 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
927 for (k = 0; k < bmap->n_eq; ++k) {
928 if (bmap->eq[k] == eq)
930 if (isl_int_is_zero(bmap->eq[k][1+pos]))
934 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
937 for (k = 0; k < bmap->n_ineq; ++k) {
938 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
942 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
943 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
946 for (k = 0; k < bmap->n_div; ++k) {
947 if (isl_int_is_zero(bmap->div[k][0]))
949 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
953 isl_seq_elim(bmap->div[k]+1, eq,
954 1+pos, 1+total, &bmap->div[k][0]);
955 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
960 struct isl_basic_map *isl_basic_map_gauss(
961 struct isl_basic_map *bmap, int *progress)
972 total_var = bmap->nparam + bmap->n_in + bmap->n_out;
973 total = total_var + bmap->n_div;
975 last_var = total - 1;
976 for (done = 0; done < bmap->n_eq; ++done) {
977 for (; last_var >= 0; --last_var) {
978 for (k = done; k < bmap->n_eq; ++k)
979 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
987 swap_equality(bmap, k, done);
988 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
989 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
991 eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
994 if (last_var >= total_var &&
995 isl_int_is_zero(bmap->div[last_var - total_var][0])) {
996 unsigned div = last_var - total_var;
997 isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
998 isl_int_set_si(bmap->div[div][1+1+last_var], 0);
999 isl_int_set(bmap->div[div][0],
1000 bmap->eq[done][1+last_var]);
1001 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1004 if (done == bmap->n_eq)
1006 for (k = done; k < bmap->n_eq; ++k) {
1007 if (isl_int_is_zero(bmap->eq[k][0]))
1009 return isl_basic_map_set_to_empty(bmap);
1011 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
1015 struct isl_basic_set *isl_basic_set_gauss(
1016 struct isl_basic_set *bset, int *progress)
1018 return (struct isl_basic_set*)isl_basic_map_gauss(
1019 (struct isl_basic_map *)bset, progress);
1022 static unsigned int round_up(unsigned int v)
1033 static int hash_index(isl_int ***index, unsigned int size, int bits,
1034 struct isl_basic_map *bmap, int k)
1037 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1038 u_int32_t hash = isl_seq_hash(bmap->ineq[k]+1, total, bits);
1039 for (h = hash; index[h]; h = (h+1) % size)
1040 if (&bmap->ineq[k] != index[h] &&
1041 isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
1046 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
1047 struct isl_basic_set *bset, int k)
1049 return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
1052 static struct isl_basic_map *remove_duplicate_constraints(
1053 struct isl_basic_map *bmap, int *progress)
1059 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1062 if (bmap->n_ineq <= 1)
1065 size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
1066 bits = ffs(size) - 1;
1067 index = isl_alloc_array(ctx, isl_int **, size);
1068 memset(index, 0, size * sizeof(isl_int **));
1072 index[isl_seq_hash(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
1073 for (k = 1; k < bmap->n_ineq; ++k) {
1074 h = hash_index(index, size, bits, bmap, k);
1076 index[h] = &bmap->ineq[k];
1081 l = index[h] - &bmap->ineq[0];
1082 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1083 swap_inequality(bmap, k, l);
1084 isl_basic_map_drop_inequality(bmap, k);
1088 for (k = 0; k < bmap->n_ineq-1; ++k) {
1089 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1090 h = hash_index(index, size, bits, bmap, k);
1091 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1094 l = index[h] - &bmap->ineq[0];
1095 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1096 if (isl_int_is_pos(sum))
1098 if (isl_int_is_zero(sum)) {
1099 /* We need to break out of the loop after these
1100 * changes since the contents of the hash
1101 * will no longer be valid.
1102 * Plus, we probably we want to regauss first.
1104 isl_basic_map_drop_inequality(bmap, l);
1105 isl_basic_map_inequality_to_equality(bmap, k);
1107 bmap = isl_basic_map_set_to_empty(bmap);
1116 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1121 total = bmap->nparam + bmap->n_in + bmap->n_out;
1122 for (d = 0; d < total; ++d)
1124 for (d = total - 1, i = 0; d >= 0 && i < bmap->n_eq; ++i) {
1125 for (; d >= 0; --d) {
1126 if (isl_int_is_zero(bmap->eq[i][1+d]))
1134 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1136 return compute_elimination_index((struct isl_basic_map *)bset, elim);
1139 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1140 struct isl_basic_map *bmap, int *elim)
1146 total = bmap->nparam + bmap->n_in + bmap->n_out;
1147 for (d = total - 1; d >= 0; --d) {
1148 if (isl_int_is_zero(src[1+d]))
1153 isl_seq_cpy(dst, src, 1 + total);
1156 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1161 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1162 struct isl_basic_set *bset, int *elim)
1164 return reduced_using_equalities(dst, src,
1165 (struct isl_basic_map *)bset, elim);
1168 /* Quick check to see if two basic maps are disjoint.
1169 * In particular, we reduce the equalities and inequalities of
1170 * one basic map in the context of the equalities of the other
1171 * basic map and check if we get a contradiction.
1173 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1174 struct isl_basic_map *bmap2)
1176 struct isl_vec *v = NULL;
1181 if (!bmap1 || !bmap2)
1183 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, return -1);
1184 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, return -1);
1185 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, return -1);
1186 if (bmap1->n_div || bmap2->n_div)
1188 if (!bmap1->n_eq && !bmap2->n_eq)
1191 total = bmap1->nparam + bmap1->n_in + bmap1->n_out;
1194 v = isl_vec_alloc(bmap1->ctx, 1 + total);
1197 elim = isl_alloc_array(bmap1->ctx, int, total);
1200 compute_elimination_index(bmap1, elim);
1201 for (i = 0; i < bmap2->n_eq; ++i) {
1203 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1205 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1206 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1209 for (i = 0; i < bmap2->n_ineq; ++i) {
1211 reduced = reduced_using_equalities(v->block.data,
1212 bmap2->ineq[i], bmap1, elim);
1213 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1214 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1217 compute_elimination_index(bmap2, elim);
1218 for (i = 0; i < bmap1->n_ineq; ++i) {
1220 reduced = reduced_using_equalities(v->block.data,
1221 bmap1->ineq[i], bmap2, 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 isl_vec_free(bmap1->ctx, v);
1230 isl_vec_free(bmap1->ctx, v);
1234 isl_vec_free(bmap1->ctx, v);
1239 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1240 struct isl_basic_set *bset2)
1242 return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1243 (struct isl_basic_map *)bset2);
1246 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1253 if (isl_map_fast_is_equal(map1, map2))
1256 for (i = 0; i < map1->n; ++i) {
1257 for (j = 0; j < map2->n; ++j) {
1258 int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1267 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1269 return isl_map_fast_is_disjoint((struct isl_map *)set1,
1270 (struct isl_map *)set2);
1273 static struct isl_basic_map *remove_duplicate_divs(
1274 struct isl_basic_map *bmap, int *progress)
1281 unsigned total_var = bmap->nparam + bmap->n_in + bmap->n_out;
1282 unsigned total = total_var + bmap->n_div;
1283 struct isl_ctx *ctx;
1285 if (bmap->n_div <= 1)
1289 for (k = bmap->n_div - 1; k >= 0; --k)
1290 if (!isl_int_is_zero(bmap->div[k][0]))
1295 size = round_up(4 * bmap->n_div / 3 - 1);
1296 bits = ffs(size) - 1;
1297 index = isl_alloc_array(ctx, int, size);
1298 memset(index, 0, size * sizeof(int));
1301 eq = isl_blk_alloc(ctx, 1+total);
1302 if (isl_blk_is_error(eq))
1305 isl_seq_clr(eq.data, 1+total);
1306 index[isl_seq_hash(bmap->div[k], 2+total, bits)] = k + 1;
1307 for (--k; k >= 0; --k) {
1310 if (isl_int_is_zero(bmap->div[k][0]))
1313 hash = isl_seq_hash(bmap->div[k], 2+total, bits);
1314 for (h = hash; index[h]; h = (h+1) % size)
1315 if (isl_seq_eq(bmap->div[k],
1316 bmap->div[index[h]-1], 2+total))
1321 isl_int_set_si(eq.data[1+total_var+k], -1);
1322 isl_int_set_si(eq.data[1+total_var+l], 1);
1323 eliminate_div(bmap, eq.data, l);
1324 isl_int_set_si(eq.data[1+total_var+k], 0);
1325 isl_int_set_si(eq.data[1+total_var+l], 0);
1330 isl_blk_free(ctx, eq);
1336 /* Elimininate divs based on equalities
1338 static struct isl_basic_map *eliminate_divs_eq(
1339 struct isl_basic_map *bmap, int *progress)
1349 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1351 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1352 for (i = 0; i < bmap->n_eq; ++i) {
1353 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
1354 !isl_int_is_negone(bmap->eq[i][off + d]))
1358 eliminate_div(bmap, bmap->eq[i], d);
1359 isl_basic_map_drop_equality(bmap, i);
1364 return eliminate_divs_eq(bmap, progress);
1368 static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
1372 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1375 for (i = bmap->n_eq - 1; i >= 0; --i) {
1376 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
1377 if (isl_int_is_zero(gcd)) {
1378 if (!isl_int_is_zero(bmap->eq[i][0])) {
1379 bmap = isl_basic_map_set_to_empty(bmap);
1382 isl_basic_map_drop_equality(bmap, i);
1385 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1386 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
1387 if (isl_int_is_one(gcd))
1389 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
1390 bmap = isl_basic_map_set_to_empty(bmap);
1393 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
1396 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1397 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
1398 if (isl_int_is_zero(gcd)) {
1399 if (isl_int_is_neg(bmap->ineq[i][0])) {
1400 bmap = isl_basic_map_set_to_empty(bmap);
1403 isl_basic_map_drop_inequality(bmap, i);
1406 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1407 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
1408 if (isl_int_is_one(gcd))
1410 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
1411 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
1419 /* Eliminate the specified variables from the constraints using
1420 * Fourier-Motzkin. The variables themselves are not removed.
1422 struct isl_basic_map *isl_basic_map_eliminate_vars(
1423 struct isl_basic_map *bmap, int pos, unsigned n)
1432 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1434 bmap = isl_basic_map_cow(bmap);
1435 for (d = pos + n - 1; d >= pos; --d) {
1436 int n_lower, n_upper;
1439 for (i = 0; i < bmap->n_eq; ++i) {
1440 if (isl_int_is_zero(bmap->eq[i][1+d]))
1442 eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
1443 isl_basic_map_drop_equality(bmap, i);
1450 for (i = 0; i < bmap->n_ineq; ++i) {
1451 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1453 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1456 bmap = isl_basic_map_extend(bmap,
1457 bmap->nparam, bmap->n_in, bmap->n_out, 0,
1458 0, n_lower * n_upper);
1459 extra = bmap->extra - bmap->n_div;
1460 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1462 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1465 for (j = 0; j < i; ++j) {
1466 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1469 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1470 isl_int_sgn(bmap->ineq[j][1+d]))
1472 k = isl_basic_map_alloc_inequality(bmap);
1475 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1477 isl_seq_clr(bmap->ineq[k]+1+total, extra);
1478 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1479 1+d, 1+total, NULL);
1481 isl_basic_map_drop_inequality(bmap, i);
1484 if (n_lower > 0 && n_upper > 0) {
1485 bmap = normalize_constraints(bmap);
1486 bmap = remove_duplicate_constraints(bmap, NULL);
1487 bmap = isl_basic_map_gauss(bmap, NULL);
1488 bmap = isl_basic_map_convex_hull(bmap);
1491 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1495 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1498 isl_basic_map_free(bmap);
1502 struct isl_basic_set *isl_basic_set_eliminate_vars(
1503 struct isl_basic_set *bset, unsigned pos, unsigned n)
1505 return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1506 (struct isl_basic_map *)bset, pos, n);
1509 /* Eliminate the specified n dimensions starting at first from the
1510 * constraints using Fourier-Motzkin, The dimensions themselves
1513 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1514 unsigned first, unsigned n)
1521 set = isl_set_cow(set);
1524 isl_assert(set->ctx, first+n <= set->dim, goto error);
1526 for (i = 0; i < set->n; ++i) {
1527 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1528 set->nparam + first, n);
1538 /* Project out n dimensions starting at first using Fourier-Motzkin */
1539 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1540 unsigned first, unsigned n)
1542 set = isl_set_eliminate_dims(set, first, n);
1543 set = isl_set_drop_dims(set, first, n);
1547 /* Project out n inputs starting at first using Fourier-Motzkin */
1548 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1549 unsigned first, unsigned n)
1556 map = isl_map_cow(map);
1559 isl_assert(map->ctx, first+n <= map->n_in, goto error);
1561 for (i = 0; i < map->n; ++i) {
1562 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1563 map->nparam + first, n);
1567 map = isl_map_drop_inputs(map, first, n);
1574 /* Project out n dimensions starting at first using Fourier-Motzkin */
1575 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1576 unsigned first, unsigned n)
1578 bset = isl_basic_set_eliminate_vars(bset, bset->nparam + first, n);
1579 bset = isl_basic_set_drop_dims(bset, first, n);
1583 /* Elimininate divs based on inequalities
1585 static struct isl_basic_map *eliminate_divs_ineq(
1586 struct isl_basic_map *bmap, int *progress)
1591 struct isl_ctx *ctx;
1597 off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1599 for (d = bmap->n_div - 1; d >= 0 ; --d) {
1600 for (i = 0; i < bmap->n_eq; ++i)
1601 if (!isl_int_is_zero(bmap->eq[i][off + d]))
1605 for (i = 0; i < bmap->n_ineq; ++i)
1606 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
1608 if (i < bmap->n_ineq)
1611 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
1612 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1614 bmap = isl_basic_map_drop_div(bmap, d);
1621 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1628 bmap = normalize_constraints(bmap);
1629 bmap = eliminate_divs_eq(bmap, &progress);
1630 bmap = eliminate_divs_ineq(bmap, &progress);
1631 bmap = isl_basic_map_gauss(bmap, &progress);
1632 bmap = remove_duplicate_divs(bmap, &progress);
1633 bmap = remove_duplicate_constraints(bmap, &progress);
1638 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1640 return (struct isl_basic_set *)
1641 isl_basic_map_simplify((struct isl_basic_map *)bset);
1644 static void dump_term(struct isl_basic_map *bmap,
1645 isl_int c, int pos, FILE *out)
1647 unsigned in = bmap->n_in;
1648 unsigned dim = bmap->n_in + bmap->n_out;
1650 isl_int_print(out, c, 0);
1652 if (!isl_int_is_one(c))
1653 isl_int_print(out, c, 0);
1654 if (pos < 1 + bmap->nparam)
1655 fprintf(out, "p%d", pos - 1);
1656 else if (pos < 1 + bmap->nparam + in)
1657 fprintf(out, "i%d", pos - 1 - bmap->nparam);
1658 else if (pos < 1 + bmap->nparam + dim)
1659 fprintf(out, "o%d", pos - 1 - bmap->nparam - in);
1661 fprintf(out, "e%d", pos - 1 - bmap->nparam - dim);
1665 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1666 int sign, FILE *out)
1670 unsigned len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1674 for (i = 0, first = 1; i < len; ++i) {
1675 if (isl_int_sgn(c[i]) * sign <= 0)
1678 fprintf(out, " + ");
1680 isl_int_abs(v, c[i]);
1681 dump_term(bmap, v, i, out);
1688 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1689 const char *op, FILE *out, int indent)
1693 fprintf(out, "%*s", indent, "");
1695 dump_constraint_sign(bmap, c, 1, out);
1696 fprintf(out, " %s ", op);
1697 dump_constraint_sign(bmap, c, -1, out);
1701 for (i = bmap->n_div; i < bmap->extra; ++i) {
1702 if (isl_int_is_zero(c[1+bmap->nparam+bmap->n_in+bmap->n_out+i]))
1704 fprintf(out, "%*s", indent, "");
1705 fprintf(out, "ERROR: unused div coefficient not zero\n");
1709 static void dump_constraints(struct isl_basic_map *bmap,
1710 isl_int **c, unsigned n,
1711 const char *op, FILE *out, int indent)
1715 for (i = 0; i < n; ++i)
1716 dump_constraint(bmap, c[i], op, out, indent);
1719 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1723 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1725 for (j = 0; j < 1 + total; ++j) {
1726 if (isl_int_is_zero(exp[j]))
1728 if (!first && isl_int_is_pos(exp[j]))
1730 dump_term(bmap, exp[j], j, out);
1735 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1739 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1740 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1742 for (i = 0; i < bmap->n_div; ++i) {
1743 fprintf(out, "%*s", indent, "");
1744 fprintf(out, "e%d = [(", i);
1745 dump_affine(bmap, bmap->div[i]+1, out);
1747 isl_int_print(out, bmap->div[i][0], 0);
1748 fprintf(out, "]\n");
1752 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1755 fprintf(out, "null basic set\n");
1759 fprintf(out, "%*s", indent, "");
1760 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1761 bset->ref, bset->nparam, bset->dim, bset->extra,
1763 dump((struct isl_basic_map *)bset, out, indent);
1766 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1769 fprintf(out, "null basic map\n");
1773 fprintf(out, "%*s", indent, "");
1774 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, flags: %x\n",
1776 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, bmap->flags);
1777 dump(bmap, out, indent);
1780 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1782 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1785 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1786 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1787 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1788 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1792 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1793 unsigned nparam, unsigned dim, int n, unsigned flags)
1795 struct isl_set *set;
1797 isl_assert(ctx, n >= 0, return NULL);
1798 set = isl_alloc(ctx, struct isl_set,
1799 sizeof(struct isl_set) +
1800 n * sizeof(struct isl_basic_set *));
1809 set->nparam = nparam;
1816 struct isl_set *isl_set_dup(struct isl_set *set)
1819 struct isl_set *dup;
1824 dup = isl_set_alloc(set->ctx, set->nparam, set->dim, set->n, set->flags);
1827 for (i = 0; i < set->n; ++i)
1828 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1832 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1834 struct isl_set *set;
1839 set = isl_set_alloc(bset->ctx, bset->nparam, bset->dim, 1, ISL_MAP_DISJOINT);
1841 isl_basic_set_free(bset);
1844 return isl_set_add(set, bset);
1847 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1849 struct isl_map *map;
1854 map = isl_map_alloc(bmap->ctx, bmap->nparam, bmap->n_in, bmap->n_out, 1,
1857 isl_basic_map_free(bmap);
1860 return isl_map_add(map, bmap);
1863 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1867 isl_assert(set->ctx, set->nparam == bset->nparam, goto error);
1868 isl_assert(set->ctx, set->dim == bset->dim, goto error);
1869 isl_assert(set->ctx, set->n < set->size, goto error);
1870 set->p[set->n] = bset;
1877 isl_basic_set_free(bset);
1881 void isl_set_free(struct isl_set *set)
1891 isl_ctx_deref(set->ctx);
1892 for (i = 0; i < set->n; ++i)
1893 isl_basic_set_free(set->p[i]);
1897 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1902 fprintf(out, "null set\n");
1906 fprintf(out, "%*s", indent, "");
1907 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1908 set->ref, set->n, set->nparam, set->dim, set->flags);
1909 for (i = 0; i < set->n; ++i) {
1910 fprintf(out, "%*s", indent, "");
1911 fprintf(out, "basic set %d:\n", i);
1912 isl_basic_set_dump(set->p[i], out, indent+4);
1916 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1921 fprintf(out, "null map\n");
1925 fprintf(out, "%*s", indent, "");
1926 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, flags: %x\n",
1927 map->ref, map->n, map->nparam, map->n_in, map->n_out,
1929 for (i = 0; i < map->n; ++i) {
1930 fprintf(out, "%*s", indent, "");
1931 fprintf(out, "basic map %d:\n", i);
1932 isl_basic_map_dump(map->p[i], out, indent+4);
1936 struct isl_basic_map *isl_basic_map_intersect_domain(
1937 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1939 struct isl_basic_map *bmap_domain;
1944 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1945 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1947 bmap = isl_basic_map_extend(bmap,
1948 bset->nparam, bmap->n_in, bmap->n_out,
1949 bset->n_div, bset->n_eq, bset->n_ineq);
1952 bmap_domain = isl_basic_map_from_basic_set(bset,
1954 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1956 bmap = isl_basic_map_simplify(bmap);
1957 return isl_basic_map_finalize(bmap);
1959 isl_basic_map_free(bmap);
1960 isl_basic_set_free(bset);
1964 struct isl_basic_map *isl_basic_map_intersect_range(
1965 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1967 struct isl_basic_map *bmap_range;
1972 isl_assert(bset->ctx, bset->dim == bmap->n_out, goto error);
1973 isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1975 bmap = isl_basic_map_extend(bmap,
1976 bset->nparam, bmap->n_in, bmap->n_out,
1977 bset->n_div, bset->n_eq, bset->n_ineq);
1980 bmap_range = isl_basic_map_from_basic_set(bset,
1982 bmap = add_constraints(bmap, bmap_range, 0, 0);
1984 bmap = isl_basic_map_simplify(bmap);
1985 return isl_basic_map_finalize(bmap);
1987 isl_basic_map_free(bmap);
1988 isl_basic_set_free(bset);
1992 struct isl_basic_map *isl_basic_map_intersect(
1993 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1995 if (!bmap1 || !bmap2)
1998 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
1999 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
2000 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
2002 bmap1 = isl_basic_map_extend(bmap1,
2003 bmap1->nparam, bmap1->n_in, bmap1->n_out,
2004 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2007 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2009 bmap1 = isl_basic_map_simplify(bmap1);
2010 return isl_basic_map_finalize(bmap1);
2012 isl_basic_map_free(bmap1);
2013 isl_basic_map_free(bmap2);
2017 struct isl_basic_set *isl_basic_set_intersect(
2018 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2020 return (struct isl_basic_set *)
2021 isl_basic_map_intersect(
2022 (struct isl_basic_map *)bset1,
2023 (struct isl_basic_map *)bset2);
2026 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2029 struct isl_map *result;
2035 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2036 F_ISSET(map2, ISL_MAP_DISJOINT))
2037 FL_SET(flags, ISL_MAP_DISJOINT);
2039 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
2040 map1->n * map2->n, flags);
2043 for (i = 0; i < map1->n; ++i)
2044 for (j = 0; j < map2->n; ++j) {
2045 struct isl_basic_map *part;
2046 part = isl_basic_map_intersect(
2047 isl_basic_map_copy(map1->p[i]),
2048 isl_basic_map_copy(map2->p[j]));
2049 if (isl_basic_map_is_empty(part))
2050 isl_basic_map_free(part);
2052 result = isl_map_add(result, part);
2065 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2067 return (struct isl_set *)
2068 isl_map_intersect((struct isl_map *)set1,
2069 (struct isl_map *)set2);
2072 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2074 struct isl_basic_set *bset;
2079 bmap = isl_basic_map_cow(bmap);
2083 bset = isl_basic_set_from_basic_map(bmap);
2084 bset = isl_basic_set_swap_vars(bset, in);
2085 return isl_basic_map_from_basic_set(bset, bset->dim-in, in);
2088 /* Turn final n dimensions into existentially quantified variables.
2090 struct isl_basic_set *isl_basic_set_project_out(
2091 struct isl_basic_set *bset, unsigned n, unsigned flags)
2101 isl_assert(bset->ctx, n <= bset->dim, goto error);
2106 bset = isl_basic_set_cow(bset);
2108 row_size = 1 + bset->nparam + bset->dim + bset->extra;
2109 old = bset->block2.data;
2110 bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
2111 (bset->extra + n) * (1 + row_size));
2112 if (!bset->block2.data)
2114 new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
2117 for (i = 0; i < n; ++i) {
2118 new_div[i] = bset->block2.data +
2119 (bset->extra + i) * (1 + row_size);
2120 isl_seq_clr(new_div[i], 1 + row_size);
2122 for (i = 0; i < bset->extra; ++i)
2123 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
2125 bset->div = new_div;
2129 bset = isl_basic_set_simplify(bset);
2130 return isl_basic_set_finalize(bset);
2132 isl_basic_set_free(bset);
2136 struct isl_basic_map *isl_basic_map_apply_range(
2137 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2139 struct isl_basic_set *bset;
2140 unsigned n_in, n_out;
2142 if (!bmap1 || !bmap2)
2145 isl_assert(bmap->ctx, bmap1->n_out == bmap2->n_in, goto error);
2146 isl_assert(bmap->ctx, bmap1->nparam == bmap2->nparam, goto error);
2149 n_out = bmap2->n_out;
2151 bmap2 = isl_basic_map_reverse(bmap2);
2154 bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
2155 bmap1->n_in + bmap2->n_in, bmap1->n_out,
2156 bmap2->extra, bmap2->n_eq, bmap2->n_ineq);
2159 bmap1 = add_constraints(bmap1, bmap2, bmap1->n_in - bmap2->n_in, 0);
2160 bmap1 = isl_basic_map_simplify(bmap1);
2161 bset = isl_basic_set_from_basic_map(bmap1);
2162 bset = isl_basic_set_project_out(bset,
2163 bset->dim - (n_in + n_out), 0);
2164 return isl_basic_map_from_basic_set(bset, n_in, n_out);
2166 isl_basic_map_free(bmap1);
2167 isl_basic_map_free(bmap2);
2171 struct isl_basic_set *isl_basic_set_apply(
2172 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2177 isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
2179 return (struct isl_basic_set *)
2180 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2182 isl_basic_set_free(bset);
2183 isl_basic_map_free(bmap);
2187 struct isl_basic_map *isl_basic_map_apply_domain(
2188 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2190 if (!bmap1 || !bmap2)
2193 isl_assert(ctx, bmap1->n_in == bmap2->n_in, goto error);
2194 isl_assert(ctx, bmap1->nparam == bmap2->nparam, goto error);
2196 bmap1 = isl_basic_map_reverse(bmap1);
2197 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2198 return isl_basic_map_reverse(bmap1);
2200 isl_basic_map_free(bmap1);
2201 isl_basic_map_free(bmap2);
2205 static struct isl_basic_map *var_equal(struct isl_ctx *ctx,
2206 struct isl_basic_map *bmap, unsigned pos)
2209 i = isl_basic_map_alloc_equality(bmap);
2212 isl_seq_clr(bmap->eq[i],
2213 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2214 isl_int_set_si(bmap->eq[i][1+bmap->nparam+pos], -1);
2215 isl_int_set_si(bmap->eq[i][1+bmap->nparam+bmap->n_in+pos], 1);
2216 return isl_basic_map_finalize(bmap);
2218 isl_basic_map_free(bmap);
2222 static struct isl_basic_map *var_more(struct isl_ctx *ctx,
2223 struct isl_basic_map *bmap, unsigned pos)
2226 i = isl_basic_map_alloc_inequality(bmap);
2229 isl_seq_clr(bmap->ineq[i],
2230 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2231 isl_int_set_si(bmap->ineq[i][0], -1);
2232 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], -1);
2233 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], 1);
2234 return isl_basic_map_finalize(bmap);
2236 isl_basic_map_free(bmap);
2240 static struct isl_basic_map *var_less(struct isl_ctx *ctx,
2241 struct isl_basic_map *bmap, unsigned pos)
2244 i = isl_basic_map_alloc_inequality(bmap);
2247 isl_seq_clr(bmap->ineq[i],
2248 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2249 isl_int_set_si(bmap->ineq[i][0], -1);
2250 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], 1);
2251 isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], -1);
2252 return isl_basic_map_finalize(bmap);
2254 isl_basic_map_free(bmap);
2258 struct isl_basic_map *isl_basic_map_equal(struct isl_ctx *ctx,
2259 unsigned nparam, unsigned in, unsigned out, unsigned n_equal)
2262 struct isl_basic_map *bmap;
2263 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, n_equal, 0);
2266 for (i = 0; i < n_equal && bmap; ++i)
2267 bmap = var_equal(ctx, bmap, i);
2268 return isl_basic_map_finalize(bmap);
2271 struct isl_basic_map *isl_basic_map_less_at(struct isl_ctx *ctx,
2272 unsigned nparam, unsigned in, unsigned out, unsigned pos)
2275 struct isl_basic_map *bmap;
2276 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
2279 for (i = 0; i < pos && bmap; ++i)
2280 bmap = var_equal(ctx, bmap, i);
2282 bmap = var_less(ctx, bmap, pos);
2283 return isl_basic_map_finalize(bmap);
2286 struct isl_basic_map *isl_basic_map_more_at(struct isl_ctx *ctx,
2287 unsigned nparam, unsigned in, unsigned out, unsigned pos)
2290 struct isl_basic_map *bmap;
2291 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
2294 for (i = 0; i < pos && bmap; ++i)
2295 bmap = var_equal(ctx, bmap, i);
2297 bmap = var_more(ctx, bmap, pos);
2298 return isl_basic_map_finalize(bmap);
2301 struct isl_basic_map *isl_basic_map_from_basic_set(
2302 struct isl_basic_set *bset, unsigned n_in, unsigned n_out)
2304 struct isl_basic_map *bmap;
2306 bset = isl_basic_set_cow(bset);
2310 isl_assert(ctx, bset->dim == n_in + n_out, goto error);
2311 bmap = (struct isl_basic_map *) bset;
2313 bmap->n_out = n_out;
2314 return isl_basic_map_finalize(bmap);
2316 isl_basic_set_free(bset);
2320 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2324 if (bmap->n_in == 0)
2325 return (struct isl_basic_set *)bmap;
2326 bmap = isl_basic_map_cow(bmap);
2329 bmap->n_out += bmap->n_in;
2331 bmap = isl_basic_map_finalize(bmap);
2332 return (struct isl_basic_set *)bmap;
2337 /* For a div d = floor(f/m), add the constraints
2340 * -(f-(n-1)) + m d >= 0
2342 * Note that the second constraint is the negation of
2346 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2349 unsigned div_pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
2350 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
2352 i = isl_basic_map_alloc_inequality(bmap);
2355 isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2356 isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2358 j = isl_basic_map_alloc_inequality(bmap);
2361 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2362 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2363 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2367 struct isl_basic_set *isl_basic_map_underlying_set(
2368 struct isl_basic_map *bmap)
2372 if (bmap->nparam == 0 && bmap->n_in == 0 && bmap->n_div == 0)
2373 return (struct isl_basic_set *)bmap;
2374 bmap = isl_basic_map_cow(bmap);
2377 bmap->n_out += bmap->nparam + bmap->n_in + bmap->n_div;
2380 bmap->extra -= bmap->n_div;
2382 bmap = isl_basic_map_finalize(bmap);
2383 return (struct isl_basic_set *)bmap;
2388 struct isl_basic_map *isl_basic_map_overlying_set(
2389 struct isl_basic_set *bset, struct isl_basic_map *like)
2391 struct isl_basic_map *bmap;
2392 struct isl_ctx *ctx;
2399 isl_assert(ctx, bset->dim ==
2400 like->nparam + like->n_in + like->n_out + like->n_div,
2402 if (like->nparam == 0 && like->n_in == 0 && like->n_div == 0) {
2403 isl_basic_map_free(like);
2404 return (struct isl_basic_map *)bset;
2406 bset = isl_basic_set_cow(bset);
2409 total = bset->dim + bset->extra;
2410 bmap = (struct isl_basic_map *)bset;
2411 bmap->nparam = like->nparam;
2412 bmap->n_in = like->n_in;
2413 bmap->n_out = like->n_out;
2414 bmap->extra += like->n_div;
2417 ltotal = total - bmap->extra + like->extra;
2420 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2421 bmap->extra * (1 + 1 + total));
2422 if (isl_blk_is_error(bmap->block2))
2424 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2428 bmap = isl_basic_map_extend(bmap, bmap->nparam,
2429 bmap->n_in, bmap->n_out, 0, 0, 2 * like->n_div);
2430 for (i = 0; i < like->n_div; ++i) {
2431 k = isl_basic_map_alloc_div(bmap);
2434 isl_seq_cpy(bmap->div[k], like->div[i], 1 + 1 + ltotal);
2435 isl_seq_clr(bmap->div[k]+1+1+ltotal, total - ltotal);
2436 if (add_div_constraints(bmap, k) < 0)
2440 isl_basic_map_free(like);
2441 bmap = isl_basic_map_finalize(bmap);
2444 isl_basic_map_free(like);
2445 isl_basic_set_free(bset);
2449 struct isl_basic_set *isl_basic_set_from_underlying_set(
2450 struct isl_basic_set *bset, struct isl_basic_set *like)
2452 return (struct isl_basic_set *)
2453 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2456 struct isl_set *isl_set_from_underlying_set(
2457 struct isl_set *set, struct isl_basic_set *like)
2463 isl_assert(set->ctx, set->dim == like->nparam + like->dim + like->n_div,
2465 if (like->nparam == 0 && like->n_div == 0) {
2466 isl_basic_set_free(like);
2469 set = isl_set_cow(set);
2472 for (i = 0; i < set->n; ++i) {
2473 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2474 isl_basic_set_copy(like));
2478 set->nparam = like->nparam;
2479 set->dim = like->dim;
2480 isl_basic_set_free(like);
2483 isl_basic_set_free(like);
2488 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2492 map = isl_map_align_divs(map);
2493 map = isl_map_cow(map);
2497 for (i = 0; i < map->n; ++i) {
2498 map->p[i] = (struct isl_basic_map *)
2499 isl_basic_map_underlying_set(map->p[i]);
2504 map->n_out += map->nparam + map->n_in;
2506 map->n_out = map->p[0]->n_out;
2509 return (struct isl_set *)map;
2515 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2517 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2520 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2522 struct isl_basic_set *domain;
2526 n_out = bmap->n_out;
2527 domain = isl_basic_set_from_basic_map(bmap);
2528 return isl_basic_set_project_out(domain, n_out, 0);
2531 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2533 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2536 struct isl_set *isl_map_range(struct isl_map *map)
2539 struct isl_set *set;
2543 map = isl_map_cow(map);
2547 set = (struct isl_set *) map;
2549 for (i = 0; i < map->n; ++i) {
2550 set->p[i] = isl_basic_map_range(map->p[i]);
2554 F_CLR(set, ISL_MAP_DISJOINT);
2555 F_CLR(set, ISL_SET_NORMALIZED);
2562 struct isl_map *isl_map_from_set(struct isl_set *set,
2563 unsigned n_in, unsigned n_out)
2566 struct isl_map *map = NULL;
2570 isl_assert(set->ctx, set->dim == n_in + n_out, goto error);
2571 set = isl_set_cow(set);
2574 map = (struct isl_map *)set;
2575 for (i = 0; i < set->n; ++i) {
2576 map->p[i] = isl_basic_map_from_basic_set(
2577 set->p[i], n_in, n_out);
2589 struct isl_set *isl_set_from_map(struct isl_map *map)
2592 struct isl_set *set = NULL;
2596 map = isl_map_cow(map);
2599 map->n_out += map->n_in;
2601 set = (struct isl_set *)map;
2602 for (i = 0; i < map->n; ++i) {
2603 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2613 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2614 unsigned nparam, unsigned in, unsigned out, int n,
2617 struct isl_map *map;
2619 map = isl_alloc(ctx, struct isl_map,
2620 sizeof(struct isl_map) +
2621 n * sizeof(struct isl_basic_map *));
2630 map->nparam = nparam;
2637 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2638 unsigned nparam, unsigned in, unsigned out)
2640 struct isl_basic_map *bmap;
2641 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2642 bmap = isl_basic_map_set_to_empty(bmap);
2646 struct isl_basic_set *isl_basic_set_empty(struct isl_ctx *ctx,
2647 unsigned nparam, unsigned dim)
2649 struct isl_basic_set *bset;
2650 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 1, 0);
2651 bset = isl_basic_set_set_to_empty(bset);
2655 struct isl_basic_map *isl_basic_map_universe(struct isl_ctx *ctx,
2656 unsigned nparam, unsigned in, unsigned out)
2658 struct isl_basic_map *bmap;
2659 bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 0, 0);
2663 struct isl_basic_set *isl_basic_set_universe(struct isl_ctx *ctx,
2664 unsigned nparam, unsigned dim)
2666 struct isl_basic_set *bset;
2667 bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 0, 0);
2671 struct isl_map *isl_map_empty(struct isl_ctx *ctx,
2672 unsigned nparam, unsigned in, unsigned out)
2674 return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
2677 struct isl_set *isl_set_empty(struct isl_ctx *ctx,
2678 unsigned nparam, unsigned dim)
2680 return isl_set_alloc(ctx, nparam, dim, 0, ISL_MAP_DISJOINT);
2683 struct isl_map *isl_map_dup(struct isl_map *map)
2686 struct isl_map *dup;
2690 dup = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out, map->n,
2692 for (i = 0; i < map->n; ++i)
2693 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2697 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2701 isl_assert(map->ctx, map->nparam == bmap->nparam, goto error);
2702 isl_assert(map->ctx, map->n_in == bmap->n_in, goto error);
2703 isl_assert(map->ctx, map->n_out == bmap->n_out, goto error);
2704 isl_assert(map->ctx, map->n < map->size, goto error);
2705 map->p[map->n] = bmap;
2707 F_CLR(map, ISL_MAP_NORMALIZED);
2713 isl_basic_map_free(bmap);
2717 void isl_map_free(struct isl_map *map)
2727 isl_ctx_deref(map->ctx);
2728 for (i = 0; i < map->n; ++i)
2729 isl_basic_map_free(map->p[i]);
2733 struct isl_map *isl_map_extend(struct isl_map *base,
2734 unsigned nparam, unsigned n_in, unsigned n_out)
2738 base = isl_map_cow(base);
2742 isl_assert(base->ctx, base->nparam <= nparam, goto error);
2743 isl_assert(base->ctx, base->n_in <= n_in, goto error);
2744 isl_assert(base->ctx, base->n_out <= n_out, goto error);
2745 base->nparam = nparam;
2747 base->n_out = n_out;
2748 for (i = 0; i < base->n; ++i) {
2749 base->p[i] = isl_basic_map_extend(base->p[i],
2750 nparam, n_in, n_out, 0, 0, 0);
2760 struct isl_set *isl_set_extend(struct isl_set *base,
2761 unsigned nparam, unsigned dim)
2763 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2767 static struct isl_basic_map *isl_basic_map_fix_var(struct isl_basic_map *bmap,
2768 unsigned var, int value)
2772 bmap = isl_basic_map_cow(bmap);
2773 bmap = isl_basic_map_extend(bmap,
2774 bmap->nparam, bmap->n_in, bmap->n_out, 0, 1, 0);
2775 j = isl_basic_map_alloc_equality(bmap);
2778 isl_seq_clr(bmap->eq[j],
2779 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2780 isl_int_set_si(bmap->eq[j][1 + var], -1);
2781 isl_int_set_si(bmap->eq[j][0], value);
2782 bmap = isl_basic_map_simplify(bmap);
2783 return isl_basic_map_finalize(bmap);
2785 isl_basic_map_free(bmap);
2789 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2790 unsigned input, int value)
2794 isl_assert(bmap->ctx, input < bmap->n_in, goto error);
2795 return isl_basic_map_fix_var(bmap, bmap->nparam + input, value);
2797 isl_basic_map_free(bmap);
2801 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2802 unsigned dim, int value)
2806 isl_assert(bset->ctx, dim < bset->dim, goto error);
2807 return (struct isl_basic_set *)
2808 isl_basic_map_fix_var((struct isl_basic_map *)bset,
2809 bset->nparam + dim, value);
2811 isl_basic_set_free(bset);
2815 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2816 unsigned input, int value)
2820 map = isl_map_cow(map);
2824 isl_assert(ctx, input < map->n_in, goto error);
2825 for (i = 0; i < map->n; ++i) {
2826 map->p[i] = isl_basic_map_fix_input_si(map->p[i], input, value);
2830 F_CLR(map, ISL_MAP_NORMALIZED);
2837 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2841 set = isl_set_cow(set);
2845 isl_assert(set->ctx, dim < set->dim, goto error);
2846 for (i = 0; i < set->n; ++i) {
2847 set->p[i] = isl_basic_set_fix_dim_si(set->p[i], dim, value);
2857 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2858 unsigned dim, isl_int value)
2862 bset = isl_basic_set_cow(bset);
2863 bset = isl_basic_set_extend(bset, bset->nparam, bset->dim, 0, 0, 1);
2864 j = isl_basic_set_alloc_inequality(bset);
2867 isl_seq_clr(bset->ineq[j], 1 + bset->nparam + bset->dim + bset->extra);
2868 isl_int_set_si(bset->ineq[j][1 + bset->nparam + dim], 1);
2869 isl_int_neg(bset->ineq[j][0], value);
2870 bset = isl_basic_set_simplify(bset);
2871 return isl_basic_set_finalize(bset);
2873 isl_basic_set_free(bset);
2877 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2882 set = isl_set_cow(set);
2886 isl_assert(set->ctx, dim < set->dim, goto error);
2887 for (i = 0; i < set->n; ++i) {
2888 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2898 struct isl_map *isl_map_reverse(struct isl_map *map)
2903 map = isl_map_cow(map);
2908 map->n_in = map->n_out;
2910 for (i = 0; i < map->n; ++i) {
2911 map->p[i] = isl_basic_map_reverse(map->p[i]);
2915 F_CLR(map, ISL_MAP_NORMALIZED);
2922 struct isl_map *isl_basic_map_lexmax(
2923 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2924 struct isl_set **empty)
2926 return isl_pip_basic_map_lexmax(bmap, dom, empty);
2929 struct isl_map *isl_basic_map_lexmin(
2930 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2931 struct isl_set **empty)
2933 return isl_pip_basic_map_lexmin(bmap, dom, empty);
2936 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2938 struct isl_basic_map *bmap = NULL;
2939 struct isl_basic_set *dom = NULL;
2940 struct isl_map *min;
2944 bmap = isl_basic_map_from_basic_set(bset, 0, bset->dim);
2947 dom = isl_basic_set_alloc(bmap->ctx, bmap->nparam, 0, 0, 0, 0);
2950 min = isl_basic_map_lexmin(bmap, dom, NULL);
2951 return isl_map_range(min);
2953 isl_basic_map_free(bmap);
2957 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2961 if (bmap->n_div == 0)
2962 return isl_map_from_basic_map(bmap);
2963 return isl_pip_basic_map_compute_divs(bmap);
2966 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2969 struct isl_map *res;
2975 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2976 for (i = 1 ; i < map->n; ++i) {
2978 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2979 if (F_ISSET(map, ISL_MAP_DISJOINT))
2980 res = isl_map_union_disjoint(res, r2);
2982 res = isl_map_union(res, r2);
2989 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2991 return (struct isl_set *)
2992 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2995 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2997 return (struct isl_set *)
2998 isl_map_compute_divs((struct isl_map *)set);
3001 struct isl_set *isl_map_domain(struct isl_map *map)
3004 struct isl_set *set;
3009 map = isl_map_cow(map);
3013 set = (struct isl_set *)map;
3014 set->dim = map->n_in;
3016 for (i = 0; i < map->n; ++i) {
3017 set->p[i] = isl_basic_map_domain(map->p[i]);
3021 F_CLR(set, ISL_MAP_DISJOINT);
3022 F_CLR(set, ISL_SET_NORMALIZED);
3029 struct isl_map *isl_map_union_disjoint(
3030 struct isl_map *map1, struct isl_map *map2)
3034 struct isl_map *map = NULL;
3048 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3049 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
3050 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
3052 if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
3053 F_ISSET(map2, ISL_MAP_DISJOINT))
3054 FL_SET(flags, ISL_MAP_DISJOINT);
3056 map = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
3057 map1->n + map2->n, flags);
3060 for (i = 0; i < map1->n; ++i) {
3061 map = isl_map_add(map,
3062 isl_basic_map_copy(map1->p[i]));
3066 for (i = 0; i < map2->n; ++i) {
3067 map = isl_map_add(map,
3068 isl_basic_map_copy(map2->p[i]));
3082 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3084 map1 = isl_map_union_disjoint(map1, map2);
3088 F_CLR(map1, ISL_MAP_DISJOINT);
3092 struct isl_set *isl_set_union_disjoint(
3093 struct isl_set *set1, struct isl_set *set2)
3095 return (struct isl_set *)
3096 isl_map_union_disjoint(
3097 (struct isl_map *)set1, (struct isl_map *)set2);
3100 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3102 return (struct isl_set *)
3103 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3106 struct isl_map *isl_map_intersect_range(
3107 struct isl_map *map, struct isl_set *set)
3110 struct isl_map *result;
3116 if (F_ISSET(map, ISL_MAP_DISJOINT) &&
3117 F_ISSET(set, ISL_MAP_DISJOINT))
3118 FL_SET(flags, ISL_MAP_DISJOINT);
3120 result = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
3121 map->n * set->n, flags);
3124 for (i = 0; i < map->n; ++i)
3125 for (j = 0; j < set->n; ++j) {
3126 result = isl_map_add(result,
3127 isl_basic_map_intersect_range(
3128 isl_basic_map_copy(map->p[i]),
3129 isl_basic_set_copy(set->p[j])));
3142 struct isl_map *isl_map_intersect_domain(
3143 struct isl_map *map, struct isl_set *set)
3145 return isl_map_reverse(
3146 isl_map_intersect_range(isl_map_reverse(map), set));
3149 struct isl_map *isl_map_apply_domain(
3150 struct isl_map *map1, struct isl_map *map2)
3154 map1 = isl_map_reverse(map1);
3155 map1 = isl_map_apply_range(map1, map2);
3156 return isl_map_reverse(map1);
3163 struct isl_map *isl_map_apply_range(
3164 struct isl_map *map1, struct isl_map *map2)
3166 struct isl_map *result;
3172 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3173 isl_assert(map1->ctx, map1->n_out == map2->n_in, goto error);
3175 result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map2->n_out,
3176 map1->n * map2->n, 0);
3179 for (i = 0; i < map1->n; ++i)
3180 for (j = 0; j < map2->n; ++j) {
3181 result = isl_map_add(result,
3182 isl_basic_map_apply_range(
3183 isl_basic_map_copy(map1->p[i]),
3184 isl_basic_map_copy(map2->p[j])));
3190 if (result && result->n <= 1)
3191 F_SET(result, ISL_MAP_DISJOINT);
3200 * returns range - domain
3202 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3204 struct isl_basic_set *bset;
3210 isl_assert(bmap->ctx, bmap->n_in == bmap->n_out, goto error);
3212 bset = isl_basic_set_from_basic_map(bmap);
3213 bset = isl_basic_set_extend(bset, bset->nparam, 3*dim, 0,
3215 bset = isl_basic_set_swap_vars(bset, 2*dim);
3216 for (i = 0; i < dim; ++i) {
3217 int j = isl_basic_map_alloc_equality(
3218 (struct isl_basic_map *)bset);
3221 isl_seq_clr(bset->eq[j],
3222 1 + bset->nparam + bset->dim + bset->extra);
3223 isl_int_set_si(bset->eq[j][1+bset->nparam+i], 1);
3224 isl_int_set_si(bset->eq[j][1+bset->nparam+dim+i], 1);
3225 isl_int_set_si(bset->eq[j][1+bset->nparam+2*dim+i], -1);
3227 return isl_basic_set_project_out(bset, 2*dim, 0);
3229 isl_basic_map_free(bmap);
3234 * returns range - domain
3236 struct isl_set *isl_map_deltas(struct isl_map *map)
3239 struct isl_set *result;
3244 isl_assert(map->ctx, map->n_in == map->n_out, goto error);
3245 result = isl_set_alloc(map->ctx, map->nparam, map->n_in, map->n, map->flags);
3248 for (i = 0; i < map->n; ++i)
3249 result = isl_set_add(result,
3250 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3258 /* If the only constraints a div d=floor(f/m)
3259 * appears in are its two defining constraints
3262 * -(f - (m - 1)) + m d >= 0
3264 * then it can safely be removed.
3266 static int div_is_redundant(struct isl_basic_map *bmap, int div)
3269 unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
3271 for (i = 0; i < bmap->n_eq; ++i)
3272 if (!isl_int_is_zero(bmap->eq[i][pos]))
3275 for (i = 0; i < bmap->n_ineq; ++i) {
3276 if (isl_int_is_zero(bmap->ineq[i][pos]))
3278 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
3280 isl_int_sub(bmap->div[div][1],
3281 bmap->div[div][1], bmap->div[div][0]);
3282 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
3283 neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
3284 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
3285 isl_int_add(bmap->div[div][1],
3286 bmap->div[div][1], bmap->div[div][0]);
3289 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
3290 bmap->n_div-div-1) != -1)
3292 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
3293 if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
3295 if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
3296 bmap->n_div-div-1) != -1)
3302 for (i = 0; i < bmap->n_div; ++i)
3303 if (!isl_int_is_zero(bmap->div[i][1+pos]))
3310 * Remove divs that don't occur in any of the constraints or other divs.
3311 * These can arise when dropping some of the variables in a quast
3312 * returned by piplib.
3314 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
3321 for (i = bmap->n_div-1; i >= 0; --i) {
3322 if (!div_is_redundant(bmap, i))
3324 bmap = isl_basic_map_drop_div(bmap, i);
3329 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
3331 bmap = remove_redundant_divs(bmap);
3334 F_SET(bmap, ISL_BASIC_SET_FINAL);
3338 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
3340 return (struct isl_basic_set *)
3341 isl_basic_map_finalize((struct isl_basic_map *)bset);
3344 struct isl_set *isl_set_finalize(struct isl_set *set)
3350 for (i = 0; i < set->n; ++i) {
3351 set->p[i] = isl_basic_set_finalize(set->p[i]);
3361 struct isl_map *isl_map_finalize(struct isl_map *map)
3367 for (i = 0; i < map->n; ++i) {
3368 map->p[i] = isl_basic_map_finalize(map->p[i]);
3372 F_CLR(map, ISL_MAP_NORMALIZED);
3379 struct isl_basic_map *isl_basic_map_identity(struct isl_ctx *ctx,
3380 unsigned nparam, unsigned dim)
3382 struct isl_basic_map *bmap;
3385 bmap = isl_basic_map_alloc(ctx, nparam, dim, dim, 0, dim, 0);
3389 for (i = 0; i < dim; ++i) {
3390 int j = isl_basic_map_alloc_equality(bmap);
3393 isl_seq_clr(bmap->eq[j],
3394 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
3395 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3396 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3398 return isl_basic_map_finalize(bmap);
3400 isl_basic_map_free(bmap);
3404 struct isl_map *isl_map_identity(struct isl_ctx *ctx,
3405 unsigned nparam, unsigned dim)
3407 struct isl_map *map = isl_map_alloc(ctx, nparam, dim, dim, 1,
3411 map = isl_map_add(map,
3412 isl_basic_map_identity(ctx, nparam, dim));
3419 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3421 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3424 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3426 return isl_map_is_subset(
3427 (struct isl_map *)set1, (struct isl_map *)set2);
3430 int isl_basic_map_is_subset(
3431 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3434 struct isl_map *map1;
3435 struct isl_map *map2;
3437 if (!bmap1 || !bmap2)
3440 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3441 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3443 is_subset = isl_map_is_subset(map1, map2);
3451 int isl_basic_map_is_equal(
3452 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3456 if (!bmap1 || !bmap2)
3458 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3461 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3465 int isl_basic_set_is_equal(
3466 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3468 return isl_basic_map_is_equal(
3469 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3472 int isl_map_is_empty(struct isl_map *map)
3479 for (i = 0; i < map->n; ++i) {
3480 is_empty = isl_basic_map_is_empty(map->p[i]);
3489 int isl_set_is_empty(struct isl_set *set)
3491 return isl_map_is_empty((struct isl_map *)set);
3494 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3498 struct isl_map *diff;
3503 if (isl_map_is_empty(map1))
3506 if (isl_map_is_empty(map2))
3509 diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3513 is_subset = isl_map_is_empty(diff);
3519 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3525 is_subset = isl_map_is_subset(map1, map2);
3528 is_subset = isl_map_is_subset(map2, map1);
3532 int isl_basic_map_is_strict_subset(
3533 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3537 if (!bmap1 || !bmap2)
3539 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3542 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3543 if (is_subset == -1)
3548 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3554 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3555 if (total != vec->size)
3560 for (i = 0; i < bmap->n_eq; ++i) {
3561 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3562 if (!isl_int_is_zero(s)) {
3568 for (i = 0; i < bmap->n_ineq; ++i) {
3569 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3570 if (isl_int_is_neg(s)) {
3581 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3585 return bmap->n_eq == 0 && bmap->n_ineq == 0;
3588 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3590 struct isl_basic_set *bset = NULL;
3591 struct isl_vec *sample = NULL;
3598 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3601 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3602 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3603 copy = isl_basic_map_convex_hull(copy);
3604 empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3605 isl_basic_map_free(copy);
3609 total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3610 if (bmap->sample && bmap->sample->size == total) {
3611 int contains = basic_map_contains(bmap, bmap->sample);
3617 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3620 sample = isl_basic_set_sample(bset);
3623 empty = sample->size == 0;
3625 isl_vec_free(bmap->ctx, bmap->sample);
3626 bmap->sample = sample;
3631 struct isl_map *isl_basic_map_union(
3632 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3634 struct isl_map *map;
3635 if (!bmap1 || !bmap2)
3638 isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
3639 isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
3640 isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
3642 map = isl_map_alloc(bmap1->ctx, bmap1->nparam,
3643 bmap1->n_in, bmap1->n_out, 2, 0);
3646 map = isl_map_add(map, bmap1);
3647 map = isl_map_add(map, bmap2);
3650 isl_basic_map_free(bmap1);
3651 isl_basic_map_free(bmap2);
3655 struct isl_set *isl_basic_set_union(
3656 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3658 return (struct isl_set *)isl_basic_map_union(
3659 (struct isl_basic_map *)bset1,
3660 (struct isl_basic_map *)bset2);
3663 /* Order divs such that any div only depends on previous divs */
3664 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3667 unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
3669 for (i = 0; i < bmap->n_div; ++i) {
3671 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3675 swap_div(bmap, i, pos);
3681 static int find_div(struct isl_basic_map *dst,
3682 struct isl_basic_map *src, unsigned div)
3686 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3688 for (i = 0; i < dst->n_div; ++i)
3689 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total) &&
3690 isl_seq_first_non_zero(dst->div[i]+1+1+total,
3691 dst->n_div - src->n_div) == -1)
3696 struct isl_basic_map *isl_basic_map_align_divs(
3697 struct isl_basic_map *dst, struct isl_basic_map *src)
3700 unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3705 if (src->n_div == 0)
3708 src = order_divs(src);
3709 dst = isl_basic_map_extend(dst, dst->nparam, dst->n_in,
3710 dst->n_out, src->n_div, 0, 2 * src->n_div);
3713 for (i = 0; i < src->n_div; ++i) {
3714 int j = find_div(dst, src, i);
3716 j = isl_basic_map_alloc_div(dst);
3719 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
3720 isl_seq_clr(dst->div[j]+1+1+total,
3721 dst->extra - src->n_div);
3722 if (add_div_constraints(dst, j) < 0)
3726 swap_div(dst, i, j);
3730 isl_basic_map_free(dst);
3734 struct isl_basic_set *isl_basic_set_align_divs(
3735 struct isl_basic_set *dst, struct isl_basic_set *src)
3737 return (struct isl_basic_set *)isl_basic_map_align_divs(
3738 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3741 struct isl_map *isl_map_align_divs(struct isl_map *map)
3745 map = isl_map_compute_divs(map);
3746 map = isl_map_cow(map);
3750 for (i = 1; i < map->n; ++i)
3751 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3752 for (i = 1; i < map->n; ++i)
3753 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3755 F_CLR(map, ISL_MAP_NORMALIZED);
3759 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3760 struct isl_basic_map *src, isl_int *c,
3761 unsigned len, int oppose)
3763 struct isl_basic_map *copy = NULL;
3768 copy = isl_basic_map_copy(src);
3769 copy = isl_basic_map_cow(copy);
3772 copy = isl_basic_map_extend(copy,
3773 copy->nparam, copy->n_in, copy->n_out, 0, 0, 1);
3774 k = isl_basic_map_alloc_inequality(copy);
3778 isl_seq_neg(copy->ineq[k], c, len);
3780 isl_seq_cpy(copy->ineq[k], c, len);
3781 total = 1 + copy->nparam + copy->n_in + copy->n_out + copy->extra;
3782 isl_seq_clr(copy->ineq[k]+len, total - len);
3783 isl_inequality_negate(copy, k);
3784 copy = isl_basic_map_simplify(copy);
3785 copy = isl_basic_map_finalize(copy);
3786 is_empty = isl_basic_map_is_empty(copy);
3790 dst = isl_map_add(dst, copy);
3792 isl_basic_map_free(copy);
3795 isl_basic_map_free(copy);
3800 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3804 struct isl_map *rest = NULL;
3806 unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3813 if (F_ISSET(map, ISL_MAP_DISJOINT))
3814 FL_SET(flags, ISL_MAP_DISJOINT);
3816 max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3817 rest = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
3822 for (i = 0; i < map->n; ++i) {
3823 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3828 for (j = 0; j < map->n; ++j)
3829 map->p[j] = isl_basic_map_cow(map->p[j]);
3831 for (i = 0; i < bmap->n_eq; ++i) {
3832 for (j = 0; j < map->n; ++j) {
3833 rest = add_cut_constraint(rest,
3834 map->p[j], bmap->eq[i], 1+total, 0);
3838 rest = add_cut_constraint(rest,
3839 map->p[j], bmap->eq[i], 1+total, 1);
3843 map->p[j] = isl_basic_map_extend(map->p[j],
3844 map->p[j]->nparam, map->p[j]->n_in,
3845 map->p[j]->n_out, 0, 1, 0);
3848 k = isl_basic_map_alloc_equality(map->p[j]);
3851 isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3852 isl_seq_clr(map->p[j]->eq[k]+1+total,
3853 map->p[j]->extra - bmap->n_div);
3857 for (i = 0; i < bmap->n_ineq; ++i) {
3858 for (j = 0; j < map->n; ++j) {
3859 rest = add_cut_constraint(rest,
3860 map->p[j], bmap->ineq[i], 1+total, 0);
3864 map->p[j] = isl_basic_map_extend(map->p[j],
3865 map->p[j]->nparam, map->p[j]->n_in,
3866 map->p[j]->n_out, 0, 0, 1);
3869 k = isl_basic_map_alloc_inequality(map->p[j]);
3872 isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3873 isl_seq_clr(map->p[j]->ineq[k]+1+total,
3874 map->p[j]->extra - bmap->n_div);
3886 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3892 isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3893 isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
3894 isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
3896 if (isl_map_is_empty(map2)) {
3901 map1 = isl_map_compute_divs(map1);
3902 map2 = isl_map_compute_divs(map2);
3906 for (i = 0; map1 && i < map2->n; ++i)
3907 map1 = subtract(map1, map2->p[i]);
3917 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3919 return (struct isl_set *)
3921 (struct isl_map *)set1, (struct isl_map *)set2);
3924 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3928 isl_assert(set->ctx, set->dim == map->n_in, goto error);
3929 map = isl_map_intersect_domain(map, set);
3930 set = isl_map_range(map);
3938 /* There is no need to cow as removing empty parts doesn't change
3939 * the meaning of the set.
3941 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3948 for (i = map->n-1; i >= 0; --i) {
3949 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3951 isl_basic_map_free(map->p[i]);
3952 if (i != map->n-1) {
3953 F_CLR(map, ISL_MAP_NORMALIZED);
3954 map->p[i] = map->p[map->n-1];
3962 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3964 return (struct isl_set *)
3965 isl_map_remove_empty_parts((struct isl_map *)set);
3968 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3970 struct isl_basic_set *bset;
3971 if (!set || set->n == 0)
3973 bset = set->p[set->n-1];
3974 isl_assert(set->ctx, F_ISSET(bset, ISL_BASIC_SET_FINAL), return NULL);
3975 return isl_basic_set_copy(bset);
3978 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3979 struct isl_basic_set *bset)
3985 for (i = set->n-1; i >= 0; --i) {
3986 if (set->p[i] != bset)
3988 set = isl_set_cow(set);
3991 isl_basic_set_free(set->p[i]);
3992 if (i != set->n-1) {
3993 F_CLR(set, ISL_SET_NORMALIZED);
3994 set->p[i] = set->p[set->n-1];
3999 isl_basic_set_free(bset);
4003 isl_basic_set_free(bset);
4007 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
4008 * for any common value of the parameters and dimensions preceding dim
4009 * in both basic sets, the values of dimension pos in bset1 are
4010 * smaller or larger then those in bset2.
4013 * 1 if bset1 follows bset2
4014 * -1 if bset1 precedes bset2
4015 * 0 if bset1 and bset2 are incomparable
4016 * -2 if some error occurred.
4018 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
4019 struct isl_basic_set *bset2, int pos)
4021 struct isl_basic_map *bmap1 = NULL;
4022 struct isl_basic_map *bmap2 = NULL;
4023 struct isl_ctx *ctx;
4024 struct isl_vec *obj;
4027 enum isl_lp_result res;
4030 if (!bset1 || !bset2)
4033 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1),
4034 pos, bset1->dim - pos);
4035 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2),
4036 pos, bset2->dim - pos);
4037 if (!bmap1 || !bmap2)
4039 bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
4040 bmap1->n_in, bmap1->n_out + bmap2->n_out,
4041 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
4044 total = bmap1->nparam + bmap1->n_in + bmap1->n_out + bmap1->n_div;
4046 obj = isl_vec_alloc(ctx, total);
4047 isl_seq_clr(obj->block.data, total);
4048 isl_int_set_si(obj->block.data[bmap1->nparam+bmap1->n_in], 1);
4049 isl_int_set_si(obj->block.data[bmap1->nparam+bmap1->n_in+
4050 bmap1->n_out-bmap2->n_out], -1);
4053 bmap1 = add_constraints(bmap1, bmap2, 0, bmap1->n_out - bmap2->n_out);
4056 res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
4057 if (res == isl_lp_empty)
4059 else if (res == isl_lp_ok && isl_int_is_pos(num))
4061 else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
4062 res == isl_lp_unbounded)
4068 isl_basic_map_free(bmap1);
4069 isl_vec_free(ctx, obj);
4072 isl_basic_map_free(bmap1);
4073 isl_basic_map_free(bmap2);
4077 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
4078 unsigned pos, isl_int *val)
4086 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
4087 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
4088 for (; d+1 > pos; --d)
4089 if (!isl_int_is_zero(bmap->eq[i][1+d]))
4093 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
4095 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
4097 if (!isl_int_is_one(bmap->eq[i][1+d]))
4100 isl_int_neg(*val, bmap->eq[i][0]);
4106 static int isl_map_fast_has_fixed_var(struct isl_map *map,
4107 unsigned pos, isl_int *val)
4119 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
4122 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
4123 for (i = 1; fixed == 1 && i < map->n; ++i) {
4124 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
4125 if (fixed == 1 && isl_int_ne(tmp, v))
4129 isl_int_set(*val, v);
4135 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4138 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4141 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4142 * then return this fixed value in *val.
4144 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4146 return isl_set_fast_has_fixed_var(set, set->nparam + dim, val);
4149 /* Check if input variable in has fixed value and if so and if val is not NULL,
4150 * then return this fixed value in *val.
4152 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4154 return isl_map_fast_has_fixed_var(map, map->nparam + in, val);
4157 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4158 * and if val is not NULL, then return this lower bound in *val.
4160 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4161 unsigned dim, isl_int *val)
4163 int i, i_eq = -1, i_ineq = -1;
4169 total = bset->nparam + bset->dim + bset->n_div;
4170 for (i = 0; i < bset->n_eq; ++i) {
4171 if (isl_int_is_zero(bset->eq[i][1+bset->nparam+dim]))
4177 for (i = 0; i < bset->n_ineq; ++i) {
4178 if (!isl_int_is_pos(bset->ineq[i][1+bset->nparam+dim]))
4180 if (i_eq != -1 || i_ineq != -1)
4184 if (i_eq == -1 && i_ineq == -1)
4186 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4187 /* The coefficient should always be one due to normalization. */
4188 if (!isl_int_is_one(c[1+bset->nparam+dim]))
4190 if (isl_seq_first_non_zero(c+1, bset->nparam+dim) != -1)
4192 if (isl_seq_first_non_zero(c+1+bset->nparam+dim+1,
4193 total - bset->nparam - dim - 1) != -1)
4196 isl_int_neg(*val, c[0]);
4200 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4201 unsigned dim, isl_int *val)
4213 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4217 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4219 for (i = 1; fixed == 1 && i < set->n; ++i) {
4220 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4222 if (fixed == 1 && isl_int_ne(tmp, v))
4226 isl_int_set(*val, v);
4232 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
4233 struct isl_basic_set *bset, struct isl_basic_set *context)
4238 if (!bset || !context)
4241 bset = isl_basic_set_cow(bset);
4245 elim = isl_alloc_array(ctx, int, bset->dim);
4248 set_compute_elimination_index(context, elim);
4249 for (i = 0; i < bset->n_eq; ++i)
4250 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
4252 for (i = 0; i < bset->n_ineq; ++i)
4253 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
4255 isl_basic_set_free(context);
4257 bset = isl_basic_set_simplify(bset);
4258 bset = isl_basic_set_finalize(bset);
4261 isl_basic_set_free(bset);
4262 isl_basic_set_free(context);
4266 static struct isl_basic_set *remove_shifted_constraints(
4267 struct isl_basic_set *bset, struct isl_basic_set *context)
4277 size = round_up(4 * (context->n_ineq+1) / 3 - 1);
4278 bits = ffs(size) - 1;
4279 index = isl_alloc_array(ctx, isl_int **, size);
4280 memset(index, 0, size * sizeof(isl_int **));
4284 for (k = 0; k < context->n_ineq; ++k) {
4285 h = set_hash_index(index, size, bits, context, k);
4286 index[h] = &context->ineq[k];
4288 for (k = 0; k < bset->n_ineq; ++k) {
4289 h = set_hash_index(index, size, bits, bset, k);
4292 l = index[h] - &context->ineq[0];
4293 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
4295 bset = isl_basic_set_cow(bset);
4298 isl_basic_set_drop_inequality(bset, k);
4308 /* Remove all information from bset that is redundant in the context
4309 * of context. In particular, equalities that are linear combinations
4310 * of those in context are remobed. Then the inequalities that are
4311 * redundant in the context of the equalities and inequalities of
4312 * context are removed.
4314 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
4315 struct isl_basic_set *context)
4319 struct isl_basic_set *combined;
4320 struct isl_ctx *ctx;
4322 if (!bset || !context)
4325 if (context->n_eq > 0) {
4328 struct isl_ctx *ctx = context->ctx;
4329 struct isl_basic_set *reduced_context;
4330 reduced_context = isl_basic_set_remove_equalities(
4331 isl_basic_set_copy(context), &T, &T2);
4332 if (!reduced_context)
4334 bset = isl_basic_set_preimage(ctx, bset, T);
4335 bset = uset_gist(bset, reduced_context);
4336 bset = isl_basic_set_preimage(ctx, bset, T2);
4337 bset = isl_basic_set_reduce_using_equalities(bset, context);
4340 if (!context->n_ineq)
4342 bset = remove_shifted_constraints(bset, context);
4343 combined = isl_basic_set_extend(isl_basic_set_copy(bset),
4344 0, bset->dim, 0, context->n_eq, context->n_ineq);
4345 context = set_add_constraints(combined, context, 0);
4350 for (i = bset->n_ineq-1; i >= 0; --i) {
4352 set_swap_inequality(context, i, context->n_ineq-1);
4354 redundant = isl_basic_set_constraint_is_redundant(&context,
4355 context->ineq[context->n_ineq], &opt, NULL);
4356 if (redundant == -1) {
4360 if (F_ISSET(context, ISL_BASIC_MAP_EMPTY)) {
4361 bset = isl_basic_set_set_to_empty(bset);
4365 set_swap_inequality(context, i, context->n_ineq-1);
4367 isl_basic_set_drop_inequality(context, i);
4368 isl_basic_set_drop_inequality(bset, i);
4373 isl_basic_set_free(context);
4376 isl_basic_set_free(context);
4377 isl_basic_set_free(bset);
4381 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
4382 struct isl_basic_map *context)
4384 struct isl_basic_set *bset;
4386 if (!bmap || !context)
4389 context = isl_basic_map_align_divs(context, bmap);
4390 bmap = isl_basic_map_align_divs(bmap, context);
4392 bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
4393 isl_basic_map_underlying_set(context));
4395 return isl_basic_map_overlying_set(bset, bmap);
4397 isl_basic_map_free(bmap);
4398 isl_basic_map_free(context);
4402 struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
4406 map = isl_map_cow(map);
4407 if (!map || !context)
4409 isl_assert(map->ctx, map->nparam == context->nparam, goto error);
4410 isl_assert(map->ctx, map->n_in == context->n_in, goto error);
4411 isl_assert(map->ctx, map->n_out == context->n_out, goto error);
4412 for (i = 0; i < map->n; ++i)
4413 context = isl_basic_map_align_divs(context, map->p[i]);
4414 for (i = 0; i < map->n; ++i) {
4415 map->p[i] = isl_basic_map_gist(map->p[i],
4416 isl_basic_map_copy(context));
4420 isl_basic_map_free(context);
4421 F_CLR(map, ISL_MAP_NORMALIZED);
4425 isl_basic_map_free(context);
4429 struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
4431 return (struct isl_set *)isl_map_gist((struct isl_map *)set,
4432 (struct isl_basic_map *)context);
4440 static int qsort_constraint_cmp(const void *p1, const void *p2)
4442 const struct constraint *c1 = (const struct constraint *)p1;
4443 const struct constraint *c2 = (const struct constraint *)p2;
4444 unsigned size = isl_min(c1->size, c2->size);
4445 return isl_seq_cmp(c1->c, c2->c, size);
4448 static struct isl_basic_map *isl_basic_map_sort_constraints(
4449 struct isl_basic_map *bmap)
4452 struct constraint *c;
4457 total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
4458 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4461 for (i = 0; i < bmap->n_ineq; ++i) {
4463 c[i].c = bmap->ineq[i];
4465 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4466 for (i = 0; i < bmap->n_ineq; ++i)
4467 bmap->ineq[i] = c[i].c;
4471 isl_basic_map_free(bmap);
4475 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4479 if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4481 bmap = isl_basic_map_convex_hull(bmap);
4482 bmap = isl_basic_map_sort_constraints(bmap);
4483 F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4487 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4488 const struct isl_basic_map *bmap2)
4495 if (bmap1->nparam != bmap2->nparam)
4496 return bmap1->nparam - bmap2->nparam;
4497 if (bmap1->n_in != bmap2->n_in)
4498 return bmap1->n_in - bmap2->n_in;
4499 if (bmap1->n_out != bmap2->n_out)
4500 return bmap1->n_out - bmap2->n_out;
4501 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4502 F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4504 if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4506 if (F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4508 if (bmap1->n_eq != bmap2->n_eq)
4509 return bmap1->n_eq - bmap2->n_eq;
4510 if (bmap1->n_ineq != bmap2->n_ineq)
4511 return bmap1->n_ineq - bmap2->n_ineq;
4512 if (bmap1->n_div != bmap2->n_div)
4513 return bmap1->n_div - bmap2->n_div;
4514 total = bmap1->nparam + bmap1->n_in + bmap1->n_out + bmap1->n_div;
4515 for (i = 0; i < bmap1->n_eq; ++i) {
4516 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4520 for (i = 0; i < bmap1->n_ineq; ++i) {
4521 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4525 for (i = 0; i < bmap1->n_div; ++i) {
4526 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4533 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4534 struct isl_basic_map *bmap2)
4536 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4539 static int qsort_bmap_cmp(const void *p1, const void *p2)
4541 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4542 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4544 return isl_basic_map_fast_cmp(bmap1, bmap2);
4547 /* We normalize in place, but if anything goes wrong we need
4548 * to return NULL, so we need to make sure we don't change the
4549 * meaning of any possible other copies of map.
4551 struct isl_map *isl_map_normalize(struct isl_map *map)
4554 struct isl_basic_map *bmap;
4558 if (F_ISSET(map, ISL_MAP_NORMALIZED))
4560 for (i = 0; i < map->n; ++i) {
4561 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4564 isl_basic_map_free(map->p[i]);
4567 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4568 F_SET(map, ISL_MAP_NORMALIZED);
4569 map = isl_map_remove_empty_parts(map);
4572 for (i = map->n - 1; i >= 1; --i) {
4573 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4575 isl_basic_map_free(map->p[i-1]);
4576 for (j = i; j < map->n; ++j)
4577 map->p[j-1] = map->p[j];
4587 struct isl_set *isl_set_normalize(struct isl_set *set)
4589 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4592 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4602 if (map1->nparam != map2->nparam)
4604 if (map1->n_in != map2->n_in)
4606 if (map1->n_out != map2->n_out)
4609 map1 = isl_map_copy(map1);
4610 map2 = isl_map_copy(map2);
4611 map1 = isl_map_normalize(map1);
4612 map2 = isl_map_normalize(map2);
4615 equal = map1->n == map2->n;
4616 for (i = 0; equal && i < map1->n; ++i) {
4617 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4630 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4632 return isl_map_fast_is_equal((struct isl_map *)set1,
4633 (struct isl_map *)set2);
4636 /* Return an interval that ranges from min to max (inclusive)
4638 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4639 isl_int min, isl_int max)
4642 struct isl_basic_set *bset = NULL;
4644 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4648 k = isl_basic_set_alloc_inequality(bset);
4651 isl_int_set_si(bset->ineq[k][1], 1);
4652 isl_int_neg(bset->ineq[k][0], min);
4654 k = isl_basic_set_alloc_inequality(bset);
4657 isl_int_set_si(bset->ineq[k][1], -1);
4658 isl_int_set(bset->ineq[k][0], max);
4662 isl_basic_set_free(bset);
4666 /* Return the Cartesian product of the basic sets in list (in the given order).
4668 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4676 struct isl_basic_set *product = NULL;
4680 isl_assert(list->ctx, list->n > 0, goto error);
4681 isl_assert(list->ctx, list->p[0], goto error);
4682 nparam = list->p[0]->nparam;
4683 dim = list->p[0]->dim;
4684 extra = list->p[0]->n_div;
4685 n_eq = list->p[0]->n_eq;
4686 n_ineq = list->p[0]->n_ineq;
4687 for (i = 1; i < list->n; ++i) {
4688 isl_assert(list->ctx, list->p[i], goto error);
4689 isl_assert(list->ctx, nparam == list->p[i]->nparam, goto error);
4690 dim += list->p[i]->dim;
4691 extra += list->p[i]->n_div;
4692 n_eq += list->p[i]->n_eq;
4693 n_ineq += list->p[i]->n_ineq;
4695 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4700 for (i = 0; i < list->n; ++i) {
4701 set_add_constraints(product,
4702 isl_basic_set_copy(list->p[i]), dim);
4703 dim += list->p[i]->dim;
4705 isl_basic_set_list_free(list);
4708 isl_basic_set_free(product);
4709 isl_basic_set_list_free(list);