2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
24 #include "isl_map_private.h"
25 #include "isl_map_piplib.h"
26 #include "isl_sample.h"
30 /* Maps dst positions to src positions */
36 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
39 struct isl_dim_map *dim_map;
40 dim_map = isl_alloc(ctx, struct isl_dim_map,
41 sizeof(struct isl_dim_map) + len * sizeof(int));
44 dim_map->len = 1 + len;
46 for (i = 0; i < len; ++i)
47 dim_map->pos[1 + i] = -1;
51 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
54 case isl_dim_param: return dim->nparam;
55 case isl_dim_in: return dim->n_in;
56 case isl_dim_out: return dim->n_out;
57 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
62 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
65 case isl_dim_param: return 1;
66 case isl_dim_in: return 1 + dim->nparam;
67 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
72 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
73 struct isl_dim *dim, enum isl_dim_type type,
74 unsigned first, unsigned n, unsigned dst_pos)
82 src_pos = pos(dim, type);
83 for (i = 0; i < n; ++i)
84 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
87 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
88 enum isl_dim_type type, unsigned dst_pos)
90 isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
93 static void isl_dim_map_div(struct isl_dim_map *dim_map,
94 struct isl_basic_map *bmap, unsigned dst_pos)
99 if (!dim_map || !bmap)
102 src_pos = 1 + isl_dim_total(bmap->dim);
103 for (i = 0; i < bmap->n_div; ++i)
104 dim_map->pos[1 + dst_pos + i] = src_pos + i;
107 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
111 for (i = 0; i < dim_map->len; ++i)
112 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
113 fprintf(stderr, "\n");
116 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
117 enum isl_dim_type type)
124 case isl_dim_out: return isl_dim_size(bmap->dim, type);
125 case isl_dim_div: return bmap->n_div;
126 case isl_dim_all: return isl_basic_map_total_dim(bmap);
131 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
133 return map ? n(map->dim, type) : 0;
136 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
138 return set ? n(set->dim, type) : 0;
141 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
142 enum isl_dim_type type)
144 struct isl_dim *dim = bmap->dim;
146 case isl_dim_param: return 1;
147 case isl_dim_in: return 1 + dim->nparam;
148 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
149 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
154 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
156 return pos(map->dim, type);
159 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
160 enum isl_dim_type type)
162 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
165 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
167 return isl_basic_set_dim(bset, isl_dim_set);
170 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
172 return isl_basic_set_dim(bset, isl_dim_param);
175 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
177 return isl_dim_total(bset->dim) + bset->n_div;
180 unsigned isl_set_n_dim(const struct isl_set *set)
182 return isl_set_dim(set, isl_dim_set);
185 unsigned isl_set_n_param(const struct isl_set *set)
187 return isl_set_dim(set, isl_dim_param);
190 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
192 return bmap->dim->n_in;
195 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
197 return bmap->dim->n_out;
200 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
202 return bmap->dim->nparam;
205 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
210 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
212 return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
215 unsigned isl_map_n_in(const struct isl_map *map)
217 return map->dim->n_in;
220 unsigned isl_map_n_out(const struct isl_map *map)
222 return map->dim->n_out;
225 unsigned isl_map_n_param(const struct isl_map *map)
227 return map->dim->nparam;
230 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
232 return map->dim->n_in == set->dim->n_out &&
233 map->dim->nparam == set->dim->nparam;
236 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
237 struct isl_basic_set *bset)
239 return bmap->dim->n_in == bset->dim->n_out &&
240 bmap->dim->nparam == bset->dim->nparam;
243 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
244 struct isl_basic_set *bset)
246 return bmap->dim->n_out == bset->dim->n_out &&
247 bmap->dim->nparam == bset->dim->nparam;
250 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
254 return isl_dim_copy(bmap->dim);
257 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
261 return isl_dim_copy(bset->dim);
264 struct isl_dim *isl_map_get_dim(struct isl_map *map)
268 return isl_dim_copy(map->dim);
271 struct isl_dim *isl_set_get_dim(struct isl_set *set)
275 return isl_dim_copy(set->dim);
278 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
279 __isl_take isl_basic_map *bmap,
280 enum isl_dim_type type, unsigned pos, const char *s)
284 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
289 isl_basic_map_free(bmap);
293 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
294 enum isl_dim_type type, unsigned pos, const char *s)
301 map->dim = isl_dim_set_name(map->dim, type, pos, s);
305 for (i = 0; i < map->n; ++i) {
306 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
317 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
318 __isl_take isl_basic_set *bset,
319 enum isl_dim_type type, unsigned pos, const char *s)
321 return (isl_basic_set *)isl_basic_map_set_dim_name(
322 (isl_basic_map *)bset, type, pos, s);
325 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
326 enum isl_dim_type type, unsigned pos, const char *s)
328 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
331 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
335 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
338 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
339 struct isl_basic_map *bmap, unsigned extra,
340 unsigned n_eq, unsigned n_ineq)
343 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
348 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
349 if (isl_blk_is_error(bmap->block))
352 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
357 bmap->block2 = isl_blk_empty();
360 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
361 if (isl_blk_is_error(bmap->block2))
364 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
369 for (i = 0; i < n_ineq + n_eq; ++i)
370 bmap->ineq[i] = bmap->block.data + i * row_size;
372 for (i = 0; i < extra; ++i)
373 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
377 bmap->c_size = n_eq + n_ineq;
378 bmap->eq = bmap->ineq + n_ineq;
387 isl_basic_map_free(bmap);
391 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
392 unsigned nparam, unsigned dim, unsigned extra,
393 unsigned n_eq, unsigned n_ineq)
395 struct isl_basic_map *bmap;
396 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
397 return (struct isl_basic_set *)bmap;
400 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
401 unsigned extra, unsigned n_eq, unsigned n_ineq)
403 struct isl_basic_map *bmap;
406 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
407 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
408 return (struct isl_basic_set *)bmap;
411 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
412 unsigned extra, unsigned n_eq, unsigned n_ineq)
414 struct isl_basic_map *bmap;
418 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
423 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
429 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
430 unsigned nparam, unsigned in, unsigned out, unsigned extra,
431 unsigned n_eq, unsigned n_ineq)
433 struct isl_basic_map *bmap;
436 dim = isl_dim_alloc(ctx, nparam, in, out);
440 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
444 static void dup_constraints(
445 struct isl_basic_map *dst, struct isl_basic_map *src)
448 unsigned total = isl_basic_map_total_dim(src);
450 for (i = 0; i < src->n_eq; ++i) {
451 int j = isl_basic_map_alloc_equality(dst);
452 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
455 for (i = 0; i < src->n_ineq; ++i) {
456 int j = isl_basic_map_alloc_inequality(dst);
457 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
460 for (i = 0; i < src->n_div; ++i) {
461 int j = isl_basic_map_alloc_div(dst);
462 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
464 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
467 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
469 struct isl_basic_map *dup;
473 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
474 bmap->n_div, bmap->n_eq, bmap->n_ineq);
477 dup_constraints(dup, bmap);
478 dup->flags = bmap->flags;
479 dup->sample = isl_vec_copy(bmap->sample);
483 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
485 struct isl_basic_map *dup;
487 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
488 return (struct isl_basic_set *)dup;
491 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
496 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
500 return isl_basic_set_dup(bset);
503 struct isl_set *isl_set_copy(struct isl_set *set)
512 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
517 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
521 bmap = isl_basic_map_dup(bmap);
523 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
527 struct isl_map *isl_map_copy(struct isl_map *map)
536 void isl_basic_map_free(struct isl_basic_map *bmap)
544 isl_ctx_deref(bmap->ctx);
546 isl_blk_free(bmap->ctx, bmap->block2);
548 isl_blk_free(bmap->ctx, bmap->block);
549 isl_vec_free(bmap->sample);
550 isl_dim_free(bmap->dim);
554 void isl_basic_set_free(struct isl_basic_set *bset)
556 isl_basic_map_free((struct isl_basic_map *)bset);
559 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
561 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
564 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
570 isl_assert(ctx, room_for_con(bmap, 1), return -1);
571 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
573 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
574 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
575 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
576 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
577 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
578 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
580 int j = isl_basic_map_alloc_inequality(bmap);
584 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
585 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
592 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
593 bmap->extra - bmap->n_div);
597 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
599 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
602 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
606 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
611 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
613 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
616 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
621 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
623 if (pos != bmap->n_eq - 1) {
625 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
626 bmap->eq[bmap->n_eq - 1] = t;
632 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
634 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
637 void isl_basic_map_inequality_to_equality(
638 struct isl_basic_map *bmap, unsigned pos)
643 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
644 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
649 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
650 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
651 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
652 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
655 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
657 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
660 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
666 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
667 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
668 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
669 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
670 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
671 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
672 1 + isl_basic_map_total_dim(bmap),
673 bmap->extra - bmap->n_div);
674 return bmap->n_ineq++;
677 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
679 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
682 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
686 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
691 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
693 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
696 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
701 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
703 if (pos != bmap->n_ineq - 1) {
705 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
706 bmap->ineq[bmap->n_ineq - 1] = t;
707 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
713 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
715 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
718 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
723 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
726 k = isl_basic_map_alloc_equality(bmap);
729 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
732 isl_basic_map_free(bmap);
736 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
739 return (isl_basic_set *)
740 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
743 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
748 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
751 k = isl_basic_map_alloc_inequality(bmap);
754 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
757 isl_basic_map_free(bmap);
761 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
764 return (isl_basic_set *)
765 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
768 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
772 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
773 isl_seq_clr(bmap->div[bmap->n_div] +
774 1 + 1 + isl_basic_map_total_dim(bmap),
775 bmap->extra - bmap->n_div);
776 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
777 return bmap->n_div++;
780 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
782 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
785 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
789 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
794 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
796 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
799 /* Copy constraint from src to dst, putting the vars of src at offset
800 * dim_off in dst and the divs of src at offset div_off in dst.
801 * If both sets are actually map, then dim_off applies to the input
804 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
805 struct isl_basic_map *src_map, isl_int *src,
806 unsigned in_off, unsigned out_off, unsigned div_off)
808 unsigned src_nparam = isl_basic_map_n_param(src_map);
809 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
810 unsigned src_in = isl_basic_map_n_in(src_map);
811 unsigned dst_in = isl_basic_map_n_in(dst_map);
812 unsigned src_out = isl_basic_map_n_out(src_map);
813 unsigned dst_out = isl_basic_map_n_out(dst_map);
814 isl_int_set(dst[0], src[0]);
815 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
816 if (dst_nparam > src_nparam)
817 isl_seq_clr(dst+1+src_nparam,
818 dst_nparam - src_nparam);
819 isl_seq_clr(dst+1+dst_nparam, in_off);
820 isl_seq_cpy(dst+1+dst_nparam+in_off,
822 isl_min(dst_in-in_off, src_in));
823 if (dst_in-in_off > src_in)
824 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
825 dst_in - in_off - src_in);
826 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
827 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
828 src+1+src_nparam+src_in,
829 isl_min(dst_out-out_off, src_out));
830 if (dst_out-out_off > src_out)
831 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
832 dst_out - out_off - src_out);
833 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
834 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
835 src+1+src_nparam+src_in+src_out,
836 isl_min(dst_map->extra-div_off, src_map->n_div));
837 if (dst_map->n_div-div_off > src_map->n_div)
838 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
839 div_off+src_map->n_div,
840 dst_map->n_div - div_off - src_map->n_div);
843 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
844 struct isl_basic_map *src_map, isl_int *src,
845 unsigned in_off, unsigned out_off, unsigned div_off)
847 isl_int_set(dst[0], src[0]);
848 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
851 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
852 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
857 if (!bmap1 || !bmap2)
860 div_off = bmap1->n_div;
862 for (i = 0; i < bmap2->n_eq; ++i) {
863 int i1 = isl_basic_map_alloc_equality(bmap1);
866 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
867 i_pos, o_pos, div_off);
870 for (i = 0; i < bmap2->n_ineq; ++i) {
871 int i1 = isl_basic_map_alloc_inequality(bmap1);
874 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
875 i_pos, o_pos, div_off);
878 for (i = 0; i < bmap2->n_div; ++i) {
879 int i1 = isl_basic_map_alloc_div(bmap1);
882 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
883 i_pos, o_pos, div_off);
886 isl_basic_map_free(bmap2);
891 isl_basic_map_free(bmap1);
892 isl_basic_map_free(bmap2);
896 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
897 struct isl_dim_map *dim_map)
901 for (i = 0; i < dim_map->len; ++i) {
902 if (dim_map->pos[i] < 0)
903 isl_int_set_si(dst[i], 0);
905 isl_int_set(dst[i], src[dim_map->pos[i]]);
909 static void copy_div_dim_map(isl_int *dst, isl_int *src,
910 struct isl_dim_map *dim_map)
912 isl_int_set(dst[0], src[0]);
913 copy_constraint_dim_map(dst+1, src+1, dim_map);
916 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
917 struct isl_basic_map *src, struct isl_dim_map *dim_map)
921 if (!src || !dst || !dim_map)
924 for (i = 0; i < src->n_eq; ++i) {
925 int i1 = isl_basic_map_alloc_equality(dst);
928 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
931 for (i = 0; i < src->n_ineq; ++i) {
932 int i1 = isl_basic_map_alloc_inequality(dst);
935 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
938 for (i = 0; i < src->n_div; ++i) {
939 int i1 = isl_basic_map_alloc_div(dst);
942 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
946 isl_basic_map_free(src);
951 isl_basic_map_free(src);
952 isl_basic_map_free(dst);
956 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
957 struct isl_basic_set *bset2, unsigned pos)
959 return (struct isl_basic_set *)
960 add_constraints((struct isl_basic_map *)bset1,
961 (struct isl_basic_map *)bset2, 0, pos);
964 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
965 struct isl_dim *dim, unsigned extra,
966 unsigned n_eq, unsigned n_ineq)
968 struct isl_basic_map *ext;
978 dims_ok = isl_dim_equal(base->dim, dim) &&
979 base->extra >= base->n_div + extra;
981 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
982 room_for_ineq(base, n_ineq)) {
987 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
988 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
989 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
990 extra += base->extra;
992 n_ineq += base->n_ineq;
994 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1000 ext->sample = isl_vec_copy(base->sample);
1001 flags = base->flags;
1002 ext = add_constraints(ext, base, 0, 0);
1005 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1012 isl_basic_map_free(base);
1016 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1017 struct isl_dim *dim, unsigned extra,
1018 unsigned n_eq, unsigned n_ineq)
1020 return (struct isl_basic_set *)
1021 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1022 extra, n_eq, n_ineq);
1025 struct isl_basic_map *isl_basic_map_extend_constraints(
1026 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1030 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1034 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1035 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1036 unsigned n_eq, unsigned n_ineq)
1038 struct isl_basic_map *bmap;
1039 struct isl_dim *dim;
1043 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1047 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1050 isl_basic_map_free(base);
1054 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1055 unsigned nparam, unsigned dim, unsigned extra,
1056 unsigned n_eq, unsigned n_ineq)
1058 return (struct isl_basic_set *)
1059 isl_basic_map_extend((struct isl_basic_map *)base,
1060 nparam, 0, dim, extra, n_eq, n_ineq);
1063 struct isl_basic_set *isl_basic_set_extend_constraints(
1064 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1066 return (struct isl_basic_set *)
1067 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1071 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1073 return (struct isl_basic_set *)
1074 isl_basic_map_cow((struct isl_basic_map *)bset);
1077 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1082 if (bmap->ref > 1) {
1084 bmap = isl_basic_map_dup(bmap);
1087 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1091 struct isl_set *isl_set_cow(struct isl_set *set)
1099 return isl_set_dup(set);
1102 struct isl_map *isl_map_cow(struct isl_map *map)
1110 return isl_map_dup(map);
1113 static void swap_vars(struct isl_blk blk, isl_int *a,
1114 unsigned a_len, unsigned b_len)
1116 isl_seq_cpy(blk.data, a+a_len, b_len);
1117 isl_seq_cpy(blk.data+b_len, a, a_len);
1118 isl_seq_cpy(a, blk.data, b_len+a_len);
1121 struct isl_basic_set *isl_basic_set_swap_vars(
1122 struct isl_basic_set *bset, unsigned n)
1132 nparam = isl_basic_set_n_param(bset);
1133 dim = isl_basic_set_n_dim(bset);
1134 isl_assert(bset->ctx, n <= dim, goto error);
1139 bset = isl_basic_set_cow(bset);
1143 blk = isl_blk_alloc(bset->ctx, dim);
1144 if (isl_blk_is_error(blk))
1147 for (i = 0; i < bset->n_eq; ++i)
1149 bset->eq[i]+1+nparam, n, dim - n);
1151 for (i = 0; i < bset->n_ineq; ++i)
1153 bset->ineq[i]+1+nparam, n, dim - n);
1155 for (i = 0; i < bset->n_div; ++i)
1157 bset->div[i]+1+1+nparam, n, dim - n);
1159 isl_blk_free(bset->ctx, blk);
1161 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1162 return isl_basic_set_gauss(bset, NULL);
1164 isl_basic_set_free(bset);
1168 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1171 set = isl_set_cow(set);
1175 for (i = 0; i < set->n; ++i) {
1176 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1182 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1186 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1192 total = isl_basic_map_total_dim(bmap);
1193 isl_basic_map_free_div(bmap, bmap->n_div);
1194 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1196 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1198 i = isl_basic_map_alloc_equality(bmap);
1202 isl_int_set_si(bmap->eq[i][0], 1);
1203 isl_seq_clr(bmap->eq[i]+1, total);
1204 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1205 isl_vec_free(bmap->sample);
1206 bmap->sample = NULL;
1207 return isl_basic_map_finalize(bmap);
1209 isl_basic_map_free(bmap);
1213 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1215 return (struct isl_basic_set *)
1216 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1219 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1222 unsigned off = isl_dim_total(bmap->dim);
1223 isl_int *t = bmap->div[a];
1224 bmap->div[a] = bmap->div[b];
1227 for (i = 0; i < bmap->n_eq; ++i)
1228 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1230 for (i = 0; i < bmap->n_ineq; ++i)
1231 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1233 for (i = 0; i < bmap->n_div; ++i)
1234 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1235 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1238 /* Eliminate the specified n dimensions starting at first from the
1239 * constraints using Fourier-Motzkin. The dimensions themselves
1242 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1243 enum isl_dim_type type, unsigned first, unsigned n)
1252 map = isl_map_cow(map);
1255 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1256 first += pos(map->dim, type) - 1;
1258 for (i = 0; i < map->n; ++i) {
1259 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1269 /* Eliminate the specified n dimensions starting at first from the
1270 * constraints using Fourier-Motzkin. The dimensions themselves
1273 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1274 enum isl_dim_type type, unsigned first, unsigned n)
1276 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1279 /* Eliminate the specified n dimensions starting at first from the
1280 * constraints using Fourier-Motzkin. The dimensions themselves
1283 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1284 unsigned first, unsigned n)
1286 return isl_set_eliminate(set, isl_dim_set, first, n);
1289 /* Project out n dimensions starting at first using Fourier-Motzkin */
1290 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1291 unsigned first, unsigned n)
1293 set = isl_set_eliminate_dims(set, first, n);
1294 set = isl_set_drop_dims(set, first, n);
1298 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1300 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1308 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1317 set = isl_set_cow(set);
1321 for (i = 0; i < set->n; ++i) {
1322 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1332 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1333 enum isl_dim_type type, unsigned first, unsigned n)
1337 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1341 bmap = isl_basic_map_eliminate_vars(bmap,
1342 isl_basic_map_offset(bmap, type) - 1 + first, n);
1345 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1347 bmap = isl_basic_map_drop(bmap, type, first, n);
1350 isl_basic_map_free(bmap);
1354 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1355 enum isl_dim_type type, unsigned first, unsigned n)
1357 return (isl_basic_set *)
1358 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1361 struct isl_map *isl_map_remove(struct isl_map *map,
1362 enum isl_dim_type type, unsigned first, unsigned n)
1369 map = isl_map_cow(map);
1372 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1374 for (i = 0; i < map->n; ++i) {
1375 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1376 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1380 map = isl_map_drop(map, type, first, n);
1387 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1388 enum isl_dim_type type, unsigned first, unsigned n)
1390 return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1393 /* Project out n inputs starting at first using Fourier-Motzkin */
1394 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1395 unsigned first, unsigned n)
1397 return isl_map_remove(map, isl_dim_in, first, n);
1400 /* Project out n dimensions starting at first using Fourier-Motzkin */
1401 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1402 unsigned first, unsigned n)
1404 unsigned nparam = isl_basic_set_n_param(bset);
1405 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1406 bset = isl_basic_set_drop_dims(bset, first, n);
1410 static void dump_term(struct isl_basic_map *bmap,
1411 isl_int c, int pos, FILE *out)
1414 unsigned in = isl_basic_map_n_in(bmap);
1415 unsigned dim = in + isl_basic_map_n_out(bmap);
1416 unsigned nparam = isl_basic_map_n_param(bmap);
1418 isl_int_print(out, c, 0);
1420 if (!isl_int_is_one(c))
1421 isl_int_print(out, c, 0);
1422 if (pos < 1 + nparam) {
1423 name = isl_dim_get_name(bmap->dim,
1424 isl_dim_param, pos - 1);
1426 fprintf(out, "%s", name);
1428 fprintf(out, "p%d", pos - 1);
1429 } else if (pos < 1 + nparam + in)
1430 fprintf(out, "i%d", pos - 1 - nparam);
1431 else if (pos < 1 + nparam + dim)
1432 fprintf(out, "o%d", pos - 1 - nparam - in);
1434 fprintf(out, "e%d", pos - 1 - nparam - dim);
1438 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1439 int sign, FILE *out)
1443 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1447 for (i = 0, first = 1; i < len; ++i) {
1448 if (isl_int_sgn(c[i]) * sign <= 0)
1451 fprintf(out, " + ");
1453 isl_int_abs(v, c[i]);
1454 dump_term(bmap, v, i, out);
1461 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1462 const char *op, FILE *out, int indent)
1466 fprintf(out, "%*s", indent, "");
1468 dump_constraint_sign(bmap, c, 1, out);
1469 fprintf(out, " %s ", op);
1470 dump_constraint_sign(bmap, c, -1, out);
1474 for (i = bmap->n_div; i < bmap->extra; ++i) {
1475 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1477 fprintf(out, "%*s", indent, "");
1478 fprintf(out, "ERROR: unused div coefficient not zero\n");
1483 static void dump_constraints(struct isl_basic_map *bmap,
1484 isl_int **c, unsigned n,
1485 const char *op, FILE *out, int indent)
1489 for (i = 0; i < n; ++i)
1490 dump_constraint(bmap, c[i], op, out, indent);
1493 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1497 unsigned total = isl_basic_map_total_dim(bmap);
1499 for (j = 0; j < 1 + total; ++j) {
1500 if (isl_int_is_zero(exp[j]))
1502 if (!first && isl_int_is_pos(exp[j]))
1504 dump_term(bmap, exp[j], j, out);
1509 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1513 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1514 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1516 for (i = 0; i < bmap->n_div; ++i) {
1517 fprintf(out, "%*s", indent, "");
1518 fprintf(out, "e%d = [(", i);
1519 dump_affine(bmap, bmap->div[i]+1, out);
1521 isl_int_print(out, bmap->div[i][0], 0);
1522 fprintf(out, "]\n");
1526 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1529 fprintf(out, "null basic set\n");
1533 fprintf(out, "%*s", indent, "");
1534 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1535 bset->ref, bset->dim->nparam, bset->dim->n_out,
1536 bset->extra, bset->flags);
1537 dump((struct isl_basic_map *)bset, out, indent);
1540 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1543 fprintf(out, "null basic map\n");
1547 fprintf(out, "%*s", indent, "");
1548 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1549 "flags: %x, n_name: %d\n",
1551 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1552 bmap->extra, bmap->flags, bmap->dim->n_name);
1553 dump(bmap, out, indent);
1556 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1561 total = isl_basic_map_total_dim(bmap);
1562 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1563 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1564 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1565 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1569 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1571 struct isl_set *set;
1575 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1576 isl_assert(dim->ctx, n >= 0, return NULL);
1577 set = isl_alloc(dim->ctx, struct isl_set,
1578 sizeof(struct isl_set) +
1579 (n - 1) * sizeof(struct isl_basic_set *));
1583 set->ctx = dim->ctx;
1584 isl_ctx_ref(set->ctx);
1596 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1597 unsigned nparam, unsigned dim, int n, unsigned flags)
1599 struct isl_set *set;
1600 struct isl_dim *dims;
1602 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1606 set = isl_set_alloc_dim(dims, n, flags);
1610 /* Make sure "map" has room for at least "n" more basic maps.
1612 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1615 struct isl_map *grown = NULL;
1619 isl_assert(map->ctx, n >= 0, goto error);
1620 if (map->n + n <= map->size)
1622 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1625 for (i = 0; i < map->n; ++i) {
1626 grown->p[i] = isl_basic_map_copy(map->p[i]);
1634 isl_map_free(grown);
1639 /* Make sure "set" has room for at least "n" more basic sets.
1641 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1643 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1646 struct isl_set *isl_set_dup(struct isl_set *set)
1649 struct isl_set *dup;
1654 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1657 for (i = 0; i < set->n; ++i)
1658 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1662 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1664 struct isl_set *set;
1669 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1671 isl_basic_set_free(bset);
1674 return isl_set_add_basic_set(set, bset);
1677 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1679 struct isl_map *map;
1684 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1686 isl_basic_map_free(bmap);
1689 return isl_map_add_basic_map(map, bmap);
1692 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1693 __isl_take isl_basic_set *bset)
1695 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1696 (struct isl_basic_map *)bset);
1699 void isl_set_free(struct isl_set *set)
1709 isl_ctx_deref(set->ctx);
1710 for (i = 0; i < set->n; ++i)
1711 isl_basic_set_free(set->p[i]);
1712 isl_dim_free(set->dim);
1716 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1721 fprintf(out, "null set\n");
1725 fprintf(out, "%*s", indent, "");
1726 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1727 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1729 for (i = 0; i < set->n; ++i) {
1730 fprintf(out, "%*s", indent, "");
1731 fprintf(out, "basic set %d:\n", i);
1732 isl_basic_set_dump(set->p[i], out, indent+4);
1736 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1741 fprintf(out, "null map\n");
1745 fprintf(out, "%*s", indent, "");
1746 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1747 "flags: %x, n_name: %d\n",
1748 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1749 map->dim->n_out, map->flags, map->dim->n_name);
1750 for (i = 0; i < map->n; ++i) {
1751 fprintf(out, "%*s", indent, "");
1752 fprintf(out, "basic map %d:\n", i);
1753 isl_basic_map_dump(map->p[i], out, indent+4);
1757 struct isl_basic_map *isl_basic_map_intersect_domain(
1758 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1760 struct isl_basic_map *bmap_domain;
1761 struct isl_dim *dim;
1766 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1767 bset->dim, isl_dim_param), goto error);
1769 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1770 isl_assert(bset->ctx,
1771 isl_basic_map_compatible_domain(bmap, bset), goto error);
1773 bmap = isl_basic_map_cow(bmap);
1776 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1777 bset->n_div, bset->n_eq, bset->n_ineq);
1778 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1779 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1780 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1782 bmap = isl_basic_map_simplify(bmap);
1783 return isl_basic_map_finalize(bmap);
1785 isl_basic_map_free(bmap);
1786 isl_basic_set_free(bset);
1790 struct isl_basic_map *isl_basic_map_intersect_range(
1791 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1793 struct isl_basic_map *bmap_range;
1798 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1799 bset->dim, isl_dim_param), goto error);
1801 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1802 isl_assert(bset->ctx,
1803 isl_basic_map_compatible_range(bmap, bset), goto error);
1805 bmap = isl_basic_map_cow(bmap);
1808 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1809 bset->n_div, bset->n_eq, bset->n_ineq);
1810 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1811 bmap = add_constraints(bmap, bmap_range, 0, 0);
1813 bmap = isl_basic_map_simplify(bmap);
1814 return isl_basic_map_finalize(bmap);
1816 isl_basic_map_free(bmap);
1817 isl_basic_set_free(bset);
1821 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1827 total = 1 + isl_basic_map_total_dim(bmap);
1828 if (total != vec->size)
1833 for (i = 0; i < bmap->n_eq; ++i) {
1834 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1835 if (!isl_int_is_zero(s)) {
1841 for (i = 0; i < bmap->n_ineq; ++i) {
1842 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1843 if (isl_int_is_neg(s)) {
1854 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1856 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1859 struct isl_basic_map *isl_basic_map_intersect(
1860 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1862 struct isl_vec *sample = NULL;
1864 if (!bmap1 || !bmap2)
1867 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1868 bmap2->dim, isl_dim_param), goto error);
1869 if (isl_dim_total(bmap1->dim) ==
1870 isl_dim_size(bmap1->dim, isl_dim_param) &&
1871 isl_dim_total(bmap2->dim) !=
1872 isl_dim_size(bmap2->dim, isl_dim_param))
1873 return isl_basic_map_intersect(bmap2, bmap1);
1875 if (isl_dim_total(bmap2->dim) !=
1876 isl_dim_size(bmap2->dim, isl_dim_param))
1877 isl_assert(bmap1->ctx,
1878 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1880 if (bmap1->sample &&
1881 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1882 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1883 sample = isl_vec_copy(bmap1->sample);
1884 else if (bmap2->sample &&
1885 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1886 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1887 sample = isl_vec_copy(bmap2->sample);
1889 bmap1 = isl_basic_map_cow(bmap1);
1892 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1893 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1894 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1897 isl_vec_free(sample);
1899 isl_vec_free(bmap1->sample);
1900 bmap1->sample = sample;
1903 bmap1 = isl_basic_map_simplify(bmap1);
1904 return isl_basic_map_finalize(bmap1);
1907 isl_vec_free(sample);
1908 isl_basic_map_free(bmap1);
1909 isl_basic_map_free(bmap2);
1913 struct isl_basic_set *isl_basic_set_intersect(
1914 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1916 return (struct isl_basic_set *)
1917 isl_basic_map_intersect(
1918 (struct isl_basic_map *)bset1,
1919 (struct isl_basic_map *)bset2);
1922 /* Special case of isl_map_intersect, where both map1 and map2
1923 * are convex, without any divs and such that either map1 or map2
1924 * contains a single constraint. This constraint is then simply
1925 * added to the other map.
1927 static __isl_give isl_map *map_intersect_add_constraint(
1928 __isl_take isl_map *map1, __isl_take isl_map *map2)
1930 struct isl_basic_map *bmap1;
1931 struct isl_basic_map *bmap2;
1933 isl_assert(map1->ctx, map1->n == 1, goto error);
1934 isl_assert(map2->ctx, map1->n == 1, goto error);
1935 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
1936 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
1938 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
1939 return isl_map_intersect(map2, map1);
1941 isl_assert(map2->ctx,
1942 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
1944 map1 = isl_map_cow(map1);
1947 if (isl_map_fast_is_empty(map1)) {
1951 map1->p[0] = isl_basic_map_cow(map1->p[0]);
1952 if (map2->p[0]->n_eq == 1)
1953 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
1955 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
1956 map2->p[0]->ineq[0]);
1958 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
1959 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
1963 if (isl_basic_map_fast_is_empty(map1->p[0])) {
1964 isl_basic_map_free(map1->p[0]);
1977 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1980 struct isl_map *result;
1986 if (isl_map_fast_is_empty(map1)) {
1990 if (isl_map_fast_is_empty(map2)) {
1995 if (map1->n == 1 && map2->n == 1 &&
1996 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
1997 isl_dim_equal(map1->dim, map2->dim) &&
1998 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
1999 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2000 return map_intersect_add_constraint(map1, map2);
2001 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2002 map2->dim, isl_dim_param), goto error);
2003 if (isl_dim_total(map1->dim) ==
2004 isl_dim_size(map1->dim, isl_dim_param) &&
2005 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2006 return isl_map_intersect(map2, map1);
2008 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2009 isl_assert(map1->ctx,
2010 isl_dim_equal(map1->dim, map2->dim), goto error);
2012 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2013 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2014 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2016 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2017 map1->n * map2->n, flags);
2020 for (i = 0; i < map1->n; ++i)
2021 for (j = 0; j < map2->n; ++j) {
2022 struct isl_basic_map *part;
2023 part = isl_basic_map_intersect(
2024 isl_basic_map_copy(map1->p[i]),
2025 isl_basic_map_copy(map2->p[j]));
2026 if (isl_basic_map_is_empty(part))
2027 isl_basic_map_free(part);
2029 result = isl_map_add_basic_map(result, part);
2042 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2044 return (struct isl_set *)
2045 isl_map_intersect((struct isl_map *)set1,
2046 (struct isl_map *)set2);
2049 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2051 struct isl_dim *dim;
2052 struct isl_basic_set *bset;
2057 bmap = isl_basic_map_cow(bmap);
2060 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2061 in = isl_basic_map_n_in(bmap);
2062 bset = isl_basic_set_from_basic_map(bmap);
2063 bset = isl_basic_set_swap_vars(bset, in);
2064 return isl_basic_map_from_basic_set(bset, dim);
2067 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2068 enum isl_dim_type type, unsigned pos, unsigned n)
2070 struct isl_dim *res_dim;
2071 struct isl_basic_map *res;
2072 struct isl_dim_map *dim_map;
2073 unsigned total, off;
2074 enum isl_dim_type t;
2082 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2084 total = isl_basic_map_total_dim(bmap) + n;
2085 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2087 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2089 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2091 unsigned size = isl_basic_map_dim(bmap, t);
2092 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2094 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2095 pos, size - pos, off + pos + n);
2097 off += isl_dim_size(res_dim, t);
2099 isl_dim_map_div(dim_map, bmap, off);
2101 res = isl_basic_map_alloc_dim(res_dim,
2102 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2103 res = add_constraints_dim_map(res, bmap, dim_map);
2104 res = isl_basic_map_simplify(res);
2105 return isl_basic_map_finalize(res);
2108 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2109 enum isl_dim_type type, unsigned n)
2113 return isl_basic_map_insert(bmap, type,
2114 isl_basic_map_dim(bmap, type), n);
2117 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2118 enum isl_dim_type type, unsigned n)
2122 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2123 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2125 isl_basic_set_free(bset);
2129 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2130 enum isl_dim_type type, unsigned pos, unsigned n)
2137 map = isl_map_cow(map);
2141 map->dim = isl_dim_insert(map->dim, type, pos, n);
2145 for (i = 0; i < map->n; ++i) {
2146 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2157 __isl_give isl_map *isl_map_add(__isl_take isl_map *map,
2158 enum isl_dim_type type, unsigned n)
2162 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2165 __isl_give isl_set *isl_set_add(__isl_take isl_set *set,
2166 enum isl_dim_type type, unsigned n)
2170 isl_assert(set->ctx, type != isl_dim_in, goto error);
2171 return (isl_set *)isl_map_add((isl_map *)set, type, n);
2177 __isl_give isl_basic_map *isl_basic_map_move_dims(
2178 __isl_take isl_basic_map *bmap,
2179 enum isl_dim_type dst_type, unsigned dst_pos,
2180 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2183 struct isl_dim_map *dim_map;
2184 struct isl_basic_map *res;
2185 enum isl_dim_type t;
2186 unsigned total, off;
2193 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2196 if (dst_type == src_type && dst_pos == src_pos)
2199 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2201 if (pos(bmap->dim, dst_type) + dst_pos ==
2202 pos(bmap->dim, src_type) + src_pos +
2203 ((src_type < dst_type) ? n : 0)) {
2204 bmap = isl_basic_map_cow(bmap);
2208 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2209 src_type, src_pos, n);
2213 bmap = isl_basic_map_finalize(bmap);
2218 total = isl_basic_map_total_dim(bmap);
2219 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2222 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2223 unsigned size = isl_dim_size(bmap->dim, t);
2224 if (t == dst_type) {
2225 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2228 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2231 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2232 dst_pos, size - dst_pos, off);
2233 off += size - dst_pos;
2234 } else if (t == src_type) {
2235 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2238 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2239 src_pos + n, size - src_pos - n, off);
2240 off += size - src_pos - n;
2242 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2246 isl_dim_map_div(dim_map, bmap, off + n);
2248 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2249 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2250 bmap = add_constraints_dim_map(res, bmap, dim_map);
2252 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2253 src_type, src_pos, n);
2257 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2258 bmap = isl_basic_map_gauss(bmap, NULL);
2259 bmap = isl_basic_map_finalize(bmap);
2263 isl_basic_map_free(bmap);
2267 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2268 enum isl_dim_type dst_type, unsigned dst_pos,
2269 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2271 return (isl_basic_set *)isl_basic_map_move_dims(
2272 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2275 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2276 enum isl_dim_type dst_type, unsigned dst_pos,
2277 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2281 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2282 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2283 src_type, src_pos, n);
2289 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2290 enum isl_dim_type dst_type, unsigned dst_pos,
2291 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2300 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2303 if (dst_type == src_type && dst_pos == src_pos)
2306 isl_assert(map->ctx, dst_type != src_type, goto error);
2308 map = isl_map_cow(map);
2312 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2316 for (i = 0; i < map->n; ++i) {
2317 map->p[i] = isl_basic_map_move_dims(map->p[i],
2319 src_type, src_pos, n);
2330 /* Move the specified dimensions to the last columns right before
2331 * the divs. Don't change the dimension specification of bmap.
2332 * That's the responsibility of the caller.
2334 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2335 enum isl_dim_type type, unsigned first, unsigned n)
2337 struct isl_dim_map *dim_map;
2338 struct isl_basic_map *res;
2339 enum isl_dim_type t;
2340 unsigned total, off;
2344 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2347 total = isl_basic_map_total_dim(bmap);
2348 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2351 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2352 unsigned size = isl_dim_size(bmap->dim, t);
2354 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2357 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2358 first, n, total - bmap->n_div - n);
2359 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2360 first + n, size - (first + n), off);
2361 off += size - (first + n);
2363 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2367 isl_dim_map_div(dim_map, bmap, off + n);
2369 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2370 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2371 res = add_constraints_dim_map(res, bmap, dim_map);
2375 /* Turn the n dimensions of type type, starting at first
2376 * into existentially quantified variables.
2378 __isl_give isl_basic_map *isl_basic_map_project_out(
2379 __isl_take isl_basic_map *bmap,
2380 enum isl_dim_type type, unsigned first, unsigned n)
2393 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2394 return isl_basic_map_remove(bmap, type, first, n);
2396 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2399 bmap = move_last(bmap, type, first, n);
2400 bmap = isl_basic_map_cow(bmap);
2404 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2405 old = bmap->block2.data;
2406 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2407 (bmap->extra + n) * (1 + row_size));
2408 if (!bmap->block2.data)
2410 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2413 for (i = 0; i < n; ++i) {
2414 new_div[i] = bmap->block2.data +
2415 (bmap->extra + i) * (1 + row_size);
2416 isl_seq_clr(new_div[i], 1 + row_size);
2418 for (i = 0; i < bmap->extra; ++i)
2419 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2421 bmap->div = new_div;
2425 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2428 bmap = isl_basic_map_simplify(bmap);
2429 bmap = isl_basic_map_drop_redundant_divs(bmap);
2430 return isl_basic_map_finalize(bmap);
2432 isl_basic_map_free(bmap);
2436 /* Turn the n dimensions of type type, starting at first
2437 * into existentially quantified variables.
2439 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2440 enum isl_dim_type type, unsigned first, unsigned n)
2442 return (isl_basic_set *)isl_basic_map_project_out(
2443 (isl_basic_map *)bset, type, first, n);
2446 /* Turn the n dimensions of type type, starting at first
2447 * into existentially quantified variables.
2449 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2450 enum isl_dim_type type, unsigned first, unsigned n)
2460 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2462 map = isl_map_cow(map);
2466 map->dim = isl_dim_drop(map->dim, type, first, n);
2470 for (i = 0; i < map->n; ++i) {
2471 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2482 /* Turn the n dimensions of type type, starting at first
2483 * into existentially quantified variables.
2485 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2486 enum isl_dim_type type, unsigned first, unsigned n)
2488 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2491 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2495 for (i = 0; i < n; ++i) {
2496 j = isl_basic_map_alloc_div(bmap);
2499 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2503 isl_basic_map_free(bmap);
2507 struct isl_basic_map *isl_basic_map_apply_range(
2508 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2510 struct isl_dim *dim_result = NULL;
2511 struct isl_basic_map *bmap;
2512 unsigned n_in, n_out, n, nparam, total, pos;
2513 struct isl_dim_map *dim_map1, *dim_map2;
2515 if (!bmap1 || !bmap2)
2518 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2519 isl_dim_copy(bmap2->dim));
2521 n_in = isl_basic_map_n_in(bmap1);
2522 n_out = isl_basic_map_n_out(bmap2);
2523 n = isl_basic_map_n_out(bmap1);
2524 nparam = isl_basic_map_n_param(bmap1);
2526 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2527 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2528 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2529 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2530 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2531 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2532 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2533 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2534 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2535 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2536 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2538 bmap = isl_basic_map_alloc_dim(dim_result,
2539 bmap1->n_div + bmap2->n_div + n,
2540 bmap1->n_eq + bmap2->n_eq,
2541 bmap1->n_ineq + bmap2->n_ineq);
2542 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2543 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2544 bmap = add_divs(bmap, n);
2545 bmap = isl_basic_map_simplify(bmap);
2546 bmap = isl_basic_map_drop_redundant_divs(bmap);
2547 return isl_basic_map_finalize(bmap);
2549 isl_basic_map_free(bmap1);
2550 isl_basic_map_free(bmap2);
2554 struct isl_basic_set *isl_basic_set_apply(
2555 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2560 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2563 return (struct isl_basic_set *)
2564 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2566 isl_basic_set_free(bset);
2567 isl_basic_map_free(bmap);
2571 struct isl_basic_map *isl_basic_map_apply_domain(
2572 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2574 if (!bmap1 || !bmap2)
2577 isl_assert(bmap1->ctx,
2578 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2579 isl_assert(bmap1->ctx,
2580 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2583 bmap1 = isl_basic_map_reverse(bmap1);
2584 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2585 return isl_basic_map_reverse(bmap1);
2587 isl_basic_map_free(bmap1);
2588 isl_basic_map_free(bmap2);
2592 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2593 * A \cap B -> f(A) + f(B)
2595 struct isl_basic_map *isl_basic_map_sum(
2596 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2598 unsigned n_in, n_out, nparam, total, pos;
2599 struct isl_basic_map *bmap = NULL;
2600 struct isl_dim_map *dim_map1, *dim_map2;
2603 if (!bmap1 || !bmap2)
2606 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2609 nparam = isl_basic_map_n_param(bmap1);
2610 n_in = isl_basic_map_n_in(bmap1);
2611 n_out = isl_basic_map_n_out(bmap1);
2613 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2614 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2615 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2616 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2617 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2618 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2619 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2620 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2621 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2622 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2623 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2625 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2626 bmap1->n_div + bmap2->n_div + 2 * n_out,
2627 bmap1->n_eq + bmap2->n_eq + n_out,
2628 bmap1->n_ineq + bmap2->n_ineq);
2629 for (i = 0; i < n_out; ++i) {
2630 int j = isl_basic_map_alloc_equality(bmap);
2633 isl_seq_clr(bmap->eq[j], 1+total);
2634 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2635 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2636 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2638 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2639 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2640 bmap = add_divs(bmap, 2 * n_out);
2642 bmap = isl_basic_map_simplify(bmap);
2643 return isl_basic_map_finalize(bmap);
2645 isl_basic_map_free(bmap);
2646 isl_basic_map_free(bmap1);
2647 isl_basic_map_free(bmap2);
2651 /* Given two maps A -> f(A) and B -> g(B), construct a map
2652 * A \cap B -> f(A) + f(B)
2654 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2656 struct isl_map *result;
2662 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2664 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2665 map1->n * map2->n, 0);
2668 for (i = 0; i < map1->n; ++i)
2669 for (j = 0; j < map2->n; ++j) {
2670 struct isl_basic_map *part;
2671 part = isl_basic_map_sum(
2672 isl_basic_map_copy(map1->p[i]),
2673 isl_basic_map_copy(map2->p[j]));
2674 if (isl_basic_map_is_empty(part))
2675 isl_basic_map_free(part);
2677 result = isl_map_add_basic_map(result, part);
2690 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2691 __isl_take isl_set *set2)
2693 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2696 /* Given a basic map A -> f(A), construct A -> -f(A).
2698 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2703 bmap = isl_basic_map_cow(bmap);
2707 n = isl_basic_map_dim(bmap, isl_dim_out);
2708 off = isl_basic_map_offset(bmap, isl_dim_out);
2709 for (i = 0; i < bmap->n_eq; ++i)
2710 for (j = 0; j < n; ++j)
2711 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2712 for (i = 0; i < bmap->n_ineq; ++i)
2713 for (j = 0; j < n; ++j)
2714 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2715 for (i = 0; i < bmap->n_div; ++i)
2716 for (j = 0; j < n; ++j)
2717 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2718 return isl_basic_map_finalize(bmap);
2721 /* Given a map A -> f(A), construct A -> -f(A).
2723 struct isl_map *isl_map_neg(struct isl_map *map)
2727 map = isl_map_cow(map);
2731 for (i = 0; i < map->n; ++i) {
2732 map->p[i] = isl_basic_map_neg(map->p[i]);
2743 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
2745 return (isl_set *)isl_map_neg((isl_map *)set);
2748 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2749 * A -> floor(f(A)/d).
2751 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2754 unsigned n_in, n_out, nparam, total, pos;
2755 struct isl_basic_map *result = NULL;
2756 struct isl_dim_map *dim_map;
2762 nparam = isl_basic_map_n_param(bmap);
2763 n_in = isl_basic_map_n_in(bmap);
2764 n_out = isl_basic_map_n_out(bmap);
2766 total = nparam + n_in + n_out + bmap->n_div + n_out;
2767 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2768 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2769 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2770 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2771 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2773 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2774 bmap->n_div + n_out,
2775 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2776 result = add_constraints_dim_map(result, bmap, dim_map);
2777 result = add_divs(result, n_out);
2778 for (i = 0; i < n_out; ++i) {
2780 j = isl_basic_map_alloc_inequality(result);
2783 isl_seq_clr(result->ineq[j], 1+total);
2784 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2785 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2786 j = isl_basic_map_alloc_inequality(result);
2789 isl_seq_clr(result->ineq[j], 1+total);
2790 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2791 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2792 isl_int_sub_ui(result->ineq[j][0], d, 1);
2795 result = isl_basic_map_simplify(result);
2796 return isl_basic_map_finalize(result);
2798 isl_basic_map_free(result);
2802 /* Given a map A -> f(A) and an integer d, construct a map
2803 * A -> floor(f(A)/d).
2805 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2809 map = isl_map_cow(map);
2813 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2814 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2815 for (i = 0; i < map->n; ++i) {
2816 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2827 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2833 i = isl_basic_map_alloc_equality(bmap);
2836 nparam = isl_basic_map_n_param(bmap);
2837 n_in = isl_basic_map_n_in(bmap);
2838 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2839 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2840 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2841 return isl_basic_map_finalize(bmap);
2843 isl_basic_map_free(bmap);
2847 /* Add a constraints to "bmap" expressing i_pos < o_pos
2849 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2855 i = isl_basic_map_alloc_inequality(bmap);
2858 nparam = isl_basic_map_n_param(bmap);
2859 n_in = isl_basic_map_n_in(bmap);
2860 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2861 isl_int_set_si(bmap->ineq[i][0], -1);
2862 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2863 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2864 return isl_basic_map_finalize(bmap);
2866 isl_basic_map_free(bmap);
2870 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2872 static __isl_give isl_basic_map *var_less_or_equal(
2873 __isl_take isl_basic_map *bmap, unsigned pos)
2879 i = isl_basic_map_alloc_inequality(bmap);
2882 nparam = isl_basic_map_n_param(bmap);
2883 n_in = isl_basic_map_n_in(bmap);
2884 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2885 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2886 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2887 return isl_basic_map_finalize(bmap);
2889 isl_basic_map_free(bmap);
2893 /* Add a constraints to "bmap" expressing i_pos > o_pos
2895 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2901 i = isl_basic_map_alloc_inequality(bmap);
2904 nparam = isl_basic_map_n_param(bmap);
2905 n_in = isl_basic_map_n_in(bmap);
2906 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2907 isl_int_set_si(bmap->ineq[i][0], -1);
2908 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2909 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2910 return isl_basic_map_finalize(bmap);
2912 isl_basic_map_free(bmap);
2916 /* Add a constraint to "bmap" expressing i_pos >= o_pos
2918 static __isl_give isl_basic_map *var_more_or_equal(
2919 __isl_take isl_basic_map *bmap, unsigned pos)
2925 i = isl_basic_map_alloc_inequality(bmap);
2928 nparam = isl_basic_map_n_param(bmap);
2929 n_in = isl_basic_map_n_in(bmap);
2930 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2931 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2932 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2933 return isl_basic_map_finalize(bmap);
2935 isl_basic_map_free(bmap);
2939 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2942 struct isl_basic_map *bmap;
2943 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2946 for (i = 0; i < n_equal && bmap; ++i)
2947 bmap = var_equal(bmap, i);
2948 return isl_basic_map_finalize(bmap);
2951 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
2953 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2956 struct isl_basic_map *bmap;
2957 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2960 for (i = 0; i < pos && bmap; ++i)
2961 bmap = var_equal(bmap, i);
2963 bmap = var_less(bmap, pos);
2964 return isl_basic_map_finalize(bmap);
2967 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
2969 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
2970 __isl_take isl_dim *dim, unsigned pos)
2973 isl_basic_map *bmap;
2975 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2976 for (i = 0; i < pos; ++i)
2977 bmap = var_equal(bmap, i);
2978 bmap = var_less_or_equal(bmap, pos);
2979 return isl_basic_map_finalize(bmap);
2982 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2984 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2987 struct isl_basic_map *bmap;
2988 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2991 for (i = 0; i < pos && bmap; ++i)
2992 bmap = var_equal(bmap, i);
2994 bmap = var_more(bmap, pos);
2995 return isl_basic_map_finalize(bmap);
2998 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3000 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3001 __isl_take isl_dim *dim, unsigned pos)
3004 isl_basic_map *bmap;
3006 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3007 for (i = 0; i < pos; ++i)
3008 bmap = var_equal(bmap, i);
3009 bmap = var_more_or_equal(bmap, pos);
3010 return isl_basic_map_finalize(bmap);
3013 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3014 unsigned n, int equal)
3016 struct isl_map *map;
3019 if (n == 0 && equal)
3020 return isl_map_universe(dims);
3022 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3024 for (i = 0; i + 1 < n; ++i)
3025 map = isl_map_add_basic_map(map,
3026 isl_basic_map_less_at(isl_dim_copy(dims), i));
3029 map = isl_map_add_basic_map(map,
3030 isl_basic_map_less_or_equal_at(dims, n - 1));
3032 map = isl_map_add_basic_map(map,
3033 isl_basic_map_less_at(dims, n - 1));
3040 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3044 return map_lex_lte_first(dims, dims->n_out, equal);
3047 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3049 return map_lex_lte_first(dim, n, 0);
3052 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3054 return map_lex_lte_first(dim, n, 1);
3057 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3059 return map_lex_lte(isl_dim_map(set_dim), 0);
3062 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3064 return map_lex_lte(isl_dim_map(set_dim), 1);
3067 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3068 unsigned n, int equal)
3070 struct isl_map *map;
3073 if (n == 0 && equal)
3074 return isl_map_universe(dims);
3076 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3078 for (i = 0; i + 1 < n; ++i)
3079 map = isl_map_add_basic_map(map,
3080 isl_basic_map_more_at(isl_dim_copy(dims), i));
3083 map = isl_map_add_basic_map(map,
3084 isl_basic_map_more_or_equal_at(dims, n - 1));
3086 map = isl_map_add_basic_map(map,
3087 isl_basic_map_more_at(dims, n - 1));
3094 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3098 return map_lex_gte_first(dims, dims->n_out, equal);
3101 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3103 return map_lex_gte_first(dim, n, 0);
3106 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3108 return map_lex_gte_first(dim, n, 1);
3111 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3113 return map_lex_gte(isl_dim_map(set_dim), 0);
3116 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3118 return map_lex_gte(isl_dim_map(set_dim), 1);
3121 struct isl_basic_map *isl_basic_map_from_basic_set(
3122 struct isl_basic_set *bset, struct isl_dim *dim)
3124 struct isl_basic_map *bmap;
3126 bset = isl_basic_set_cow(bset);
3130 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3131 isl_dim_free(bset->dim);
3132 bmap = (struct isl_basic_map *) bset;
3134 return isl_basic_map_finalize(bmap);
3136 isl_basic_set_free(bset);
3141 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3145 if (bmap->dim->n_in == 0)
3146 return (struct isl_basic_set *)bmap;
3147 bmap = isl_basic_map_cow(bmap);
3150 bmap->dim = isl_dim_cow(bmap->dim);
3153 bmap->dim->n_out += bmap->dim->n_in;
3154 bmap->dim->n_in = 0;
3155 bmap = isl_basic_map_finalize(bmap);
3156 return (struct isl_basic_set *)bmap;
3158 isl_basic_map_free(bmap);
3162 /* For a div d = floor(f/m), add the constraints
3165 * -(f-(n-1)) + m d >= 0
3167 * Note that the second constraint is the negation of
3171 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3172 unsigned pos, isl_int *div)
3175 unsigned total = isl_basic_map_total_dim(bmap);
3177 i = isl_basic_map_alloc_inequality(bmap);
3180 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3181 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3183 j = isl_basic_map_alloc_inequality(bmap);
3186 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3187 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3188 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3192 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3193 unsigned pos, isl_int *div)
3195 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3199 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3201 unsigned total = isl_basic_map_total_dim(bmap);
3202 unsigned div_pos = total - bmap->n_div + div;
3204 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3208 struct isl_basic_set *isl_basic_map_underlying_set(
3209 struct isl_basic_map *bmap)
3213 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
3214 return (struct isl_basic_set *)bmap;
3215 bmap = isl_basic_map_cow(bmap);
3218 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3221 bmap->extra -= bmap->n_div;
3223 bmap = isl_basic_map_finalize(bmap);
3224 return (struct isl_basic_set *)bmap;
3229 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3230 __isl_take isl_basic_set *bset)
3232 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3235 struct isl_basic_map *isl_basic_map_overlying_set(
3236 struct isl_basic_set *bset, struct isl_basic_map *like)
3238 struct isl_basic_map *bmap;
3239 struct isl_ctx *ctx;
3246 isl_assert(ctx, bset->n_div == 0, goto error);
3247 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3248 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3250 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3251 isl_basic_map_free(like);
3252 return (struct isl_basic_map *)bset;
3254 bset = isl_basic_set_cow(bset);
3257 total = bset->dim->n_out + bset->extra;
3258 bmap = (struct isl_basic_map *)bset;
3259 isl_dim_free(bmap->dim);
3260 bmap->dim = isl_dim_copy(like->dim);
3263 bmap->n_div = like->n_div;
3264 bmap->extra += like->n_div;
3267 ltotal = total - bmap->extra + like->extra;
3270 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3271 bmap->extra * (1 + 1 + total));
3272 if (isl_blk_is_error(bmap->block2))
3274 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3278 for (i = 0; i < bmap->extra; ++i)
3279 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3280 for (i = 0; i < like->n_div; ++i) {
3281 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3282 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3284 bmap = isl_basic_map_extend_constraints(bmap,
3285 0, 2 * like->n_div);
3286 for (i = 0; i < like->n_div; ++i) {
3287 if (isl_int_is_zero(bmap->div[i][0]))
3289 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3293 isl_basic_map_free(like);
3294 bmap = isl_basic_map_simplify(bmap);
3295 bmap = isl_basic_map_finalize(bmap);
3298 isl_basic_map_free(like);
3299 isl_basic_set_free(bset);
3303 struct isl_basic_set *isl_basic_set_from_underlying_set(
3304 struct isl_basic_set *bset, struct isl_basic_set *like)
3306 return (struct isl_basic_set *)
3307 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3310 struct isl_set *isl_set_from_underlying_set(
3311 struct isl_set *set, struct isl_basic_set *like)
3317 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3319 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3320 isl_basic_set_free(like);
3323 set = isl_set_cow(set);
3326 for (i = 0; i < set->n; ++i) {
3327 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3328 isl_basic_set_copy(like));
3332 isl_dim_free(set->dim);
3333 set->dim = isl_dim_copy(like->dim);
3336 isl_basic_set_free(like);
3339 isl_basic_set_free(like);
3344 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3348 map = isl_map_cow(map);
3351 map->dim = isl_dim_cow(map->dim);
3355 for (i = 1; i < map->n; ++i)
3356 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3358 for (i = 0; i < map->n; ++i) {
3359 map->p[i] = (struct isl_basic_map *)
3360 isl_basic_map_underlying_set(map->p[i]);
3365 map->dim = isl_dim_underlying(map->dim, 0);
3367 isl_dim_free(map->dim);
3368 map->dim = isl_dim_copy(map->p[0]->dim);
3372 return (struct isl_set *)map;
3378 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3380 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3383 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3385 struct isl_basic_set *domain;
3390 n_in = isl_basic_map_n_in(bmap);
3391 n_out = isl_basic_map_n_out(bmap);
3392 domain = isl_basic_set_from_basic_map(bmap);
3393 return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3396 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3398 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3401 struct isl_set *isl_map_range(struct isl_map *map)
3404 struct isl_set *set;
3408 if (isl_map_dim(map, isl_dim_in) == 0)
3409 return (isl_set *)map;
3411 map = isl_map_cow(map);
3415 set = (struct isl_set *) map;
3416 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3419 for (i = 0; i < map->n; ++i) {
3420 set->p[i] = isl_basic_map_range(map->p[i]);
3424 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3425 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3432 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3435 struct isl_map *map = NULL;
3437 set = isl_set_cow(set);
3440 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3441 map = (struct isl_map *)set;
3442 for (i = 0; i < set->n; ++i) {
3443 map->p[i] = isl_basic_map_from_basic_set(
3444 set->p[i], isl_dim_copy(dim));
3448 isl_dim_free(map->dim);
3457 struct isl_map *isl_map_from_range(struct isl_set *set)
3459 return (struct isl_map *)set;
3462 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3464 return isl_map_reverse(isl_map_from_range(set));;
3467 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3468 __isl_take isl_set *range)
3470 return isl_map_product(isl_map_from_domain(domain),
3471 isl_map_from_range(range));
3474 struct isl_set *isl_set_from_map(struct isl_map *map)
3477 struct isl_set *set = NULL;
3481 map = isl_map_cow(map);
3484 map->dim = isl_dim_cow(map->dim);
3487 map->dim->n_out += map->dim->n_in;
3489 set = (struct isl_set *)map;
3490 for (i = 0; i < map->n; ++i) {
3491 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3501 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3503 struct isl_map *map;
3507 isl_assert(dim->ctx, n >= 0, return NULL);
3508 map = isl_alloc(dim->ctx, struct isl_map,
3509 sizeof(struct isl_map) +
3510 (n - 1) * sizeof(struct isl_basic_map *));
3514 map->ctx = dim->ctx;
3515 isl_ctx_ref(map->ctx);
3527 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3528 unsigned nparam, unsigned in, unsigned out, int n,
3531 struct isl_map *map;
3532 struct isl_dim *dims;
3534 dims = isl_dim_alloc(ctx, nparam, in, out);
3538 map = isl_map_alloc_dim(dims, n, flags);
3542 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3544 struct isl_basic_map *bmap;
3545 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3546 bmap = isl_basic_map_set_to_empty(bmap);
3550 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3552 struct isl_basic_set *bset;
3553 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3554 bset = isl_basic_set_set_to_empty(bset);
3558 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3560 struct isl_basic_map *bmap;
3563 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3564 bmap = isl_basic_map_set_to_empty(bmap);
3568 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3570 struct isl_basic_map *bmap;
3573 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3574 bmap = isl_basic_map_set_to_empty(bmap);
3578 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3580 struct isl_basic_set *bset;
3583 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3584 bset = isl_basic_set_set_to_empty(bset);
3588 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3590 struct isl_basic_map *bmap;
3591 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
3595 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
3597 struct isl_basic_set *bset;
3598 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
3602 __isl_give isl_basic_map *isl_basic_map_universe_like(
3603 __isl_keep isl_basic_map *model)
3607 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3610 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3614 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3617 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3618 __isl_keep isl_set *model)
3622 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3625 struct isl_map *isl_map_empty(struct isl_dim *dim)
3627 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3630 struct isl_map *isl_map_empty_like(struct isl_map *model)
3634 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3637 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
3641 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3644 struct isl_set *isl_set_empty(struct isl_dim *dim)
3646 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3649 struct isl_set *isl_set_empty_like(struct isl_set *model)
3653 return isl_set_empty(isl_dim_copy(model->dim));
3656 struct isl_map *isl_map_universe(struct isl_dim *dim)
3658 struct isl_map *map;
3661 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3662 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
3666 struct isl_set *isl_set_universe(struct isl_dim *dim)
3668 struct isl_set *set;
3671 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3672 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
3676 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3680 return isl_set_universe(isl_dim_copy(model->dim));
3683 struct isl_map *isl_map_dup(struct isl_map *map)
3686 struct isl_map *dup;
3690 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3691 for (i = 0; i < map->n; ++i)
3692 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
3696 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
3697 __isl_take isl_basic_map *bmap)
3701 if (isl_basic_map_fast_is_empty(bmap)) {
3702 isl_basic_map_free(bmap);
3705 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3706 isl_assert(map->ctx, map->n < map->size, goto error);
3707 map->p[map->n] = bmap;
3709 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3715 isl_basic_map_free(bmap);
3719 void isl_map_free(struct isl_map *map)
3729 isl_ctx_deref(map->ctx);
3730 for (i = 0; i < map->n; ++i)
3731 isl_basic_map_free(map->p[i]);
3732 isl_dim_free(map->dim);
3736 struct isl_map *isl_map_extend(struct isl_map *base,
3737 unsigned nparam, unsigned n_in, unsigned n_out)
3741 base = isl_map_cow(base);
3745 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3748 for (i = 0; i < base->n; ++i) {
3749 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3750 isl_dim_copy(base->dim), 0, 0, 0);
3760 struct isl_set *isl_set_extend(struct isl_set *base,
3761 unsigned nparam, unsigned dim)
3763 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3767 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3768 struct isl_basic_map *bmap, unsigned pos, int value)
3772 bmap = isl_basic_map_cow(bmap);
3773 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3774 j = isl_basic_map_alloc_equality(bmap);
3777 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3778 isl_int_set_si(bmap->eq[j][pos], -1);
3779 isl_int_set_si(bmap->eq[j][0], value);
3780 bmap = isl_basic_map_simplify(bmap);
3781 return isl_basic_map_finalize(bmap);
3783 isl_basic_map_free(bmap);
3787 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3788 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3792 bmap = isl_basic_map_cow(bmap);
3793 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3794 j = isl_basic_map_alloc_equality(bmap);
3797 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3798 isl_int_set_si(bmap->eq[j][pos], -1);
3799 isl_int_set(bmap->eq[j][0], value);
3800 bmap = isl_basic_map_simplify(bmap);
3801 return isl_basic_map_finalize(bmap);
3803 isl_basic_map_free(bmap);
3807 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3808 enum isl_dim_type type, unsigned pos, int value)
3812 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3813 return isl_basic_map_fix_pos_si(bmap,
3814 isl_basic_map_offset(bmap, type) + pos, value);
3816 isl_basic_map_free(bmap);
3820 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3821 enum isl_dim_type type, unsigned pos, isl_int value)
3825 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3826 return isl_basic_map_fix_pos(bmap,
3827 isl_basic_map_offset(bmap, type) + pos, value);
3829 isl_basic_map_free(bmap);
3833 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3834 enum isl_dim_type type, unsigned pos, int value)
3836 return (struct isl_basic_set *)
3837 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3841 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3842 enum isl_dim_type type, unsigned pos, isl_int value)
3844 return (struct isl_basic_set *)
3845 isl_basic_map_fix((struct isl_basic_map *)bset,
3849 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3850 unsigned input, int value)
3852 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3855 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3856 unsigned dim, int value)
3858 return (struct isl_basic_set *)
3859 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3860 isl_dim_set, dim, value);
3863 struct isl_map *isl_map_fix_si(struct isl_map *map,
3864 enum isl_dim_type type, unsigned pos, int value)
3868 map = isl_map_cow(map);
3872 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3873 for (i = 0; i < map->n; ++i) {
3874 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3878 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3885 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
3886 enum isl_dim_type type, unsigned pos, int value)
3888 return (struct isl_set *)
3889 isl_map_fix_si((struct isl_map *)set, type, pos, value);
3892 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
3893 enum isl_dim_type type, unsigned pos, isl_int value)
3897 map = isl_map_cow(map);
3901 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3902 for (i = 0; i < map->n; ++i) {
3903 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
3907 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3914 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
3915 enum isl_dim_type type, unsigned pos, isl_int value)
3917 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
3920 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3921 unsigned input, int value)
3923 return isl_map_fix_si(map, isl_dim_in, input, value);
3926 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3928 return (struct isl_set *)
3929 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3932 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
3933 __isl_take isl_basic_map *bmap,
3934 enum isl_dim_type type, unsigned pos, int value)
3940 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3941 pos += isl_basic_map_offset(bmap, type);
3942 bmap = isl_basic_map_cow(bmap);
3943 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
3944 j = isl_basic_map_alloc_inequality(bmap);
3947 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
3948 isl_int_set_si(bmap->ineq[j][pos], 1);
3949 isl_int_set_si(bmap->ineq[j][0], -value);
3950 bmap = isl_basic_map_simplify(bmap);
3951 return isl_basic_map_finalize(bmap);
3953 isl_basic_map_free(bmap);
3957 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3958 unsigned dim, isl_int value)
3962 bset = isl_basic_set_cow(bset);
3963 bset = isl_basic_set_extend_constraints(bset, 0, 1);
3964 j = isl_basic_set_alloc_inequality(bset);
3967 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3968 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3969 isl_int_neg(bset->ineq[j][0], value);
3970 bset = isl_basic_set_simplify(bset);
3971 return isl_basic_set_finalize(bset);
3973 isl_basic_set_free(bset);
3977 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
3978 enum isl_dim_type type, unsigned pos, int value)
3982 map = isl_map_cow(map);
3986 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3987 for (i = 0; i < map->n; ++i) {
3988 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
3993 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4000 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4001 enum isl_dim_type type, unsigned pos, int value)
4003 return (struct isl_set *)
4004 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4007 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4012 set = isl_set_cow(set);
4016 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4017 for (i = 0; i < set->n; ++i) {
4018 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4028 struct isl_map *isl_map_reverse(struct isl_map *map)
4032 map = isl_map_cow(map);
4036 map->dim = isl_dim_reverse(map->dim);
4039 for (i = 0; i < map->n; ++i) {
4040 map->p[i] = isl_basic_map_reverse(map->p[i]);
4044 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4051 static struct isl_map *isl_basic_map_partial_lexopt(
4052 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4053 struct isl_set **empty, int max)
4057 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4058 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4060 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4062 isl_basic_map_free(bmap);
4063 isl_basic_set_free(dom);
4069 struct isl_map *isl_basic_map_partial_lexmax(
4070 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4071 struct isl_set **empty)
4073 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4076 struct isl_map *isl_basic_map_partial_lexmin(
4077 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4078 struct isl_set **empty)
4080 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4083 struct isl_set *isl_basic_set_partial_lexmin(
4084 struct isl_basic_set *bset, struct isl_basic_set *dom,
4085 struct isl_set **empty)
4087 return (struct isl_set *)
4088 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4092 struct isl_set *isl_basic_set_partial_lexmax(
4093 struct isl_basic_set *bset, struct isl_basic_set *dom,
4094 struct isl_set **empty)
4096 return (struct isl_set *)
4097 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4101 /* Given a basic map "bmap", compute the lexicograhically minimal
4102 * (or maximal) image element for each domain element in dom.
4103 * Set *empty to those elements in dom that do not have an image element.
4105 * We first make sure the basic sets in dom are disjoint and then
4106 * simply collect the results over each of the basic sets separately.
4107 * We could probably improve the efficiency a bit by moving the union
4108 * domain down into the parametric integer programming.
4110 static __isl_give isl_map *basic_map_partial_lexopt(
4111 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4112 __isl_give isl_set **empty, int max)
4115 struct isl_map *res;
4117 dom = isl_set_make_disjoint(dom);
4121 if (isl_set_fast_is_empty(dom)) {
4122 res = isl_map_empty_like_basic_map(bmap);
4123 *empty = isl_set_empty_like(dom);
4125 isl_basic_map_free(bmap);
4129 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4130 isl_basic_set_copy(dom->p[0]), empty, max);
4132 for (i = 1; i < dom->n; ++i) {
4133 struct isl_map *res_i;
4134 struct isl_set *empty_i;
4136 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4137 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4139 res = isl_map_union_disjoint(res, res_i);
4140 *empty = isl_set_union_disjoint(*empty, empty_i);
4144 isl_basic_map_free(bmap);
4149 isl_basic_map_free(bmap);
4153 /* Given a map "map", compute the lexicograhically minimal
4154 * (or maximal) image element for each domain element in dom.
4155 * Set *empty to those elements in dom that do not have an image element.
4157 * We first compute the lexicographically minimal or maximal element
4158 * in the first basic map. This results in a partial solution "res"
4159 * and a subset "todo" of dom that still need to be handled.
4160 * We then consider each of the remaining maps in "map" and successively
4161 * improve both "res" and "todo".
4163 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4164 * Assume we are computing the lexicographical maximum.
4165 * We first intersect basic map i with a relation that maps elements
4166 * to elements that are lexicographically larger than the image elements
4167 * in res^k and the compute the maximum image element of this intersection.
4168 * The result ("better") corresponds to those image elements in basic map i
4169 * that are better than what we had before. The remainder ("keep") are the
4170 * domain elements for which the image element in res_k was better.
4171 * We also compute the lexicographical maximum of basic map i in todo^k.
4172 * res^i is the result of the operation + better + those elements in
4173 * res^k that we should keep
4174 * todo^i is the remainder of the maximum operation on todo^k.
4176 static __isl_give isl_map *isl_map_partial_lexopt(
4177 __isl_take isl_map *map, __isl_take isl_set *dom,
4178 __isl_give isl_set **empty, int max)
4181 struct isl_map *res;
4182 struct isl_set *todo;
4187 if (isl_map_fast_is_empty(map)) {
4195 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4196 isl_set_copy(dom), &todo, max);
4198 for (i = 1; i < map->n; ++i) {
4200 struct isl_map *better;
4201 struct isl_set *keep;
4202 struct isl_map *res_i;
4203 struct isl_set *todo_i;
4204 struct isl_dim *dim = isl_map_get_dim(res);
4206 dim = isl_dim_range(dim);
4208 lt = isl_map_lex_lt(dim);
4210 lt = isl_map_lex_gt(dim);
4211 lt = isl_map_apply_range(isl_map_copy(res), lt);
4212 lt = isl_map_intersect(lt,
4213 isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
4214 better = isl_map_partial_lexopt(lt,
4215 isl_map_domain(isl_map_copy(res)),
4218 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4219 todo, &todo_i, max);
4221 res = isl_map_intersect_domain(res, keep);
4222 res = isl_map_union_disjoint(res, res_i);
4223 res = isl_map_union_disjoint(res, better);
4244 __isl_give isl_map *isl_map_partial_lexmax(
4245 __isl_take isl_map *map, __isl_take isl_set *dom,
4246 __isl_give isl_set **empty)
4248 return isl_map_partial_lexopt(map, dom, empty, 1);
4251 __isl_give isl_map *isl_map_partial_lexmin(
4252 __isl_take isl_map *map, __isl_take isl_set *dom,
4253 __isl_give isl_set **empty)
4255 return isl_map_partial_lexopt(map, dom, empty, 0);
4258 __isl_give isl_set *isl_set_partial_lexmin(
4259 __isl_take isl_set *set, __isl_take isl_set *dom,
4260 __isl_give isl_set **empty)
4262 return (struct isl_set *)
4263 isl_map_partial_lexmin((struct isl_map *)set,
4267 __isl_give isl_set *isl_set_partial_lexmax(
4268 __isl_take isl_set *set, __isl_take isl_set *dom,
4269 __isl_give isl_set **empty)
4271 return (struct isl_set *)
4272 isl_map_partial_lexmax((struct isl_map *)set,
4276 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4278 struct isl_basic_set *dom = NULL;
4279 struct isl_dim *dom_dim;
4283 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4284 dom = isl_basic_set_universe(dom_dim);
4285 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4287 isl_basic_map_free(bmap);
4291 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4293 return isl_basic_map_lexopt(bmap, 0);
4296 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4298 return isl_basic_map_lexopt(bmap, 1);
4301 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4303 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4306 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4308 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4311 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4313 struct isl_set *dom = NULL;
4314 struct isl_dim *dom_dim;
4318 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4319 dom = isl_set_universe(dom_dim);
4320 return isl_map_partial_lexopt(map, dom, NULL, max);
4326 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4328 return isl_map_lexopt(map, 0);
4331 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4333 return isl_map_lexopt(map, 1);
4336 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4338 return (isl_set *)isl_map_lexmin((isl_map *)set);
4341 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4343 return (isl_set *)isl_map_lexmax((isl_map *)set);
4346 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
4347 struct isl_dim *dim)
4354 for (i = 0; i < map->n; ++i) {
4355 isl_dim_free(map->p[i]->dim);
4356 map->p[i]->dim = isl_dim_copy(dim);
4358 isl_dim_free(map->dim);
4368 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
4369 struct isl_dim *dim)
4371 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
4374 /* Apply a preimage specified by "mat" on the parameters of "bset".
4375 * bset is assumed to have only parameters and divs.
4377 static struct isl_basic_set *basic_set_parameter_preimage(
4378 struct isl_basic_set *bset, struct isl_mat *mat)
4385 bset->dim = isl_dim_cow(bset->dim);
4389 nparam = isl_basic_set_dim(bset, isl_dim_param);
4391 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4393 bset->dim->nparam = 0;
4394 bset->dim->n_out = nparam;
4395 bset = isl_basic_set_preimage(bset, mat);
4397 bset->dim->nparam = bset->dim->n_out;
4398 bset->dim->n_out = 0;
4403 isl_basic_set_free(bset);
4407 /* Apply a preimage specified by "mat" on the parameters of "set".
4408 * set is assumed to have only parameters and divs.
4410 static struct isl_set *set_parameter_preimage(
4411 struct isl_set *set, struct isl_mat *mat)
4413 struct isl_dim *dim = NULL;
4419 dim = isl_dim_copy(set->dim);
4420 dim = isl_dim_cow(dim);
4424 nparam = isl_set_dim(set, isl_dim_param);
4426 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4429 dim->n_out = nparam;
4430 isl_set_reset_dim(set, dim);
4431 set = isl_set_preimage(set, mat);
4434 dim = isl_dim_copy(set->dim);
4435 dim = isl_dim_cow(dim);
4438 dim->nparam = dim->n_out;
4440 isl_set_reset_dim(set, dim);
4450 /* Intersect the basic set "bset" with the affine space specified by the
4451 * equalities in "eq".
4453 static struct isl_basic_set *basic_set_append_equalities(
4454 struct isl_basic_set *bset, struct isl_mat *eq)
4462 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4467 len = 1 + isl_dim_total(bset->dim) + bset->extra;
4468 for (i = 0; i < eq->n_row; ++i) {
4469 k = isl_basic_set_alloc_equality(bset);
4472 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4473 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4477 bset = isl_basic_set_gauss(bset, NULL);
4478 bset = isl_basic_set_finalize(bset);
4483 isl_basic_set_free(bset);
4487 /* Intersect the set "set" with the affine space specified by the
4488 * equalities in "eq".
4490 static struct isl_set *set_append_equalities(struct isl_set *set,
4498 for (i = 0; i < set->n; ++i) {
4499 set->p[i] = basic_set_append_equalities(set->p[i],
4512 /* Project the given basic set onto its parameter domain, possibly introducing
4513 * new, explicit, existential variables in the constraints.
4514 * The input has parameters and (possibly implicit) existential variables.
4515 * The output has the same parameters, but only
4516 * explicit existentially quantified variables.
4518 * The actual projection is performed by pip, but pip doesn't seem
4519 * to like equalities very much, so we first remove the equalities
4520 * among the parameters by performing a variable compression on
4521 * the parameters. Afterward, an inverse transformation is performed
4522 * and the equalities among the parameters are inserted back in.
4524 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4528 struct isl_mat *T, *T2;
4529 struct isl_set *set;
4530 unsigned nparam, n_div;
4532 bset = isl_basic_set_cow(bset);
4536 if (bset->n_eq == 0)
4537 return isl_basic_set_lexmin(bset);
4539 isl_basic_set_gauss(bset, NULL);
4541 nparam = isl_basic_set_dim(bset, isl_dim_param);
4542 n_div = isl_basic_set_dim(bset, isl_dim_div);
4544 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4545 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4548 if (i == bset->n_eq)
4549 return isl_basic_set_lexmin(bset);
4551 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4553 eq = isl_mat_cow(eq);
4554 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4555 if (T && T->n_col == 0) {
4559 bset = isl_basic_set_set_to_empty(bset);
4560 return isl_set_from_basic_set(bset);
4562 bset = basic_set_parameter_preimage(bset, T);
4564 set = isl_basic_set_lexmin(bset);
4565 set = set_parameter_preimage(set, T2);
4566 set = set_append_equalities(set, eq);
4570 /* Compute an explicit representation for all the existentially
4571 * quantified variables.
4572 * The input and output dimensions are first turned into parameters.
4573 * compute_divs then returns a map with the same parameters and
4574 * no input or output dimensions and the dimension specification
4575 * is reset to that of the input.
4577 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4579 struct isl_basic_set *bset;
4580 struct isl_set *set;
4581 struct isl_map *map;
4582 struct isl_dim *dim, *orig_dim = NULL;
4587 bmap = isl_basic_map_cow(bmap);
4591 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4592 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4593 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4594 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4598 orig_dim = bmap->dim;
4600 bset = (struct isl_basic_set *)bmap;
4602 set = parameter_compute_divs(bset);
4603 map = (struct isl_map *)set;
4604 map = isl_map_reset_dim(map, orig_dim);
4608 isl_basic_map_free(bmap);
4612 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4620 off = isl_dim_total(bmap->dim);
4621 for (i = 0; i < bmap->n_div; ++i) {
4622 if (isl_int_is_zero(bmap->div[i][0]))
4624 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
4630 static int map_divs_known(__isl_keep isl_map *map)
4637 for (i = 0; i < map->n; ++i) {
4638 int known = basic_map_divs_known(map->p[i]);
4646 /* If bmap contains any unknown divs, then compute explicit
4647 * expressions for them. However, this computation may be
4648 * quite expensive, so first try to remove divs that aren't
4651 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
4655 struct isl_map *map;
4657 known = basic_map_divs_known(bmap);
4661 return isl_map_from_basic_map(bmap);
4663 bmap = isl_basic_map_drop_redundant_divs(bmap);
4665 known = basic_map_divs_known(bmap);
4669 return isl_map_from_basic_map(bmap);
4671 map = compute_divs(bmap);
4674 isl_basic_map_free(bmap);
4678 struct isl_map *isl_map_compute_divs(struct isl_map *map)
4682 struct isl_map *res;
4689 known = map_divs_known(map);
4697 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
4698 for (i = 1 ; i < map->n; ++i) {
4700 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
4701 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4702 res = isl_map_union_disjoint(res, r2);
4704 res = isl_map_union(res, r2);
4711 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
4713 return (struct isl_set *)
4714 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
4717 struct isl_set *isl_set_compute_divs(struct isl_set *set)
4719 return (struct isl_set *)
4720 isl_map_compute_divs((struct isl_map *)set);
4723 struct isl_set *isl_map_domain(struct isl_map *map)
4726 struct isl_set *set;
4731 map = isl_map_cow(map);
4735 set = (struct isl_set *)map;
4736 set->dim = isl_dim_domain(set->dim);
4739 for (i = 0; i < map->n; ++i) {
4740 set->p[i] = isl_basic_map_domain(map->p[i]);
4744 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4745 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4752 struct isl_map *isl_map_union_disjoint(
4753 struct isl_map *map1, struct isl_map *map2)
4757 struct isl_map *map = NULL;
4771 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4773 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4774 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4775 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4777 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
4778 map1->n + map2->n, flags);
4781 for (i = 0; i < map1->n; ++i) {
4782 map = isl_map_add_basic_map(map,
4783 isl_basic_map_copy(map1->p[i]));
4787 for (i = 0; i < map2->n; ++i) {
4788 map = isl_map_add_basic_map(map,
4789 isl_basic_map_copy(map2->p[i]));
4803 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
4805 map1 = isl_map_union_disjoint(map1, map2);
4809 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
4813 struct isl_set *isl_set_union_disjoint(
4814 struct isl_set *set1, struct isl_set *set2)
4816 return (struct isl_set *)
4817 isl_map_union_disjoint(
4818 (struct isl_map *)set1, (struct isl_map *)set2);
4821 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
4823 return (struct isl_set *)
4824 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
4827 struct isl_map *isl_map_intersect_range(
4828 struct isl_map *map, struct isl_set *set)
4831 struct isl_map *result;
4837 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
4838 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
4839 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4841 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
4842 map->n * set->n, flags);
4845 for (i = 0; i < map->n; ++i)
4846 for (j = 0; j < set->n; ++j) {
4847 result = isl_map_add_basic_map(result,
4848 isl_basic_map_intersect_range(
4849 isl_basic_map_copy(map->p[i]),
4850 isl_basic_set_copy(set->p[j])));
4863 struct isl_map *isl_map_intersect_domain(
4864 struct isl_map *map, struct isl_set *set)
4866 return isl_map_reverse(
4867 isl_map_intersect_range(isl_map_reverse(map), set));
4870 struct isl_map *isl_map_apply_domain(
4871 struct isl_map *map1, struct isl_map *map2)
4875 map1 = isl_map_reverse(map1);
4876 map1 = isl_map_apply_range(map1, map2);
4877 return isl_map_reverse(map1);
4884 struct isl_map *isl_map_apply_range(
4885 struct isl_map *map1, struct isl_map *map2)
4887 struct isl_dim *dim_result;
4888 struct isl_map *result;
4894 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
4895 isl_dim_copy(map2->dim));
4897 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
4900 for (i = 0; i < map1->n; ++i)
4901 for (j = 0; j < map2->n; ++j) {
4902 result = isl_map_add_basic_map(result,
4903 isl_basic_map_apply_range(
4904 isl_basic_map_copy(map1->p[i]),
4905 isl_basic_map_copy(map2->p[j])));
4911 if (result && result->n <= 1)
4912 ISL_F_SET(result, ISL_MAP_DISJOINT);
4921 * returns range - domain
4923 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
4926 struct isl_basic_set *bset;
4933 dim = isl_basic_map_n_in(bmap);
4934 nparam = isl_basic_map_n_param(bmap);
4935 isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
4936 bset = isl_basic_set_from_basic_map(bmap);
4937 bset = isl_basic_set_cow(bset);
4938 dims = isl_basic_set_get_dim(bset);
4939 dims = isl_dim_add(dims, isl_dim_set, dim);
4940 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
4941 bset = isl_basic_set_swap_vars(bset, 2*dim);
4942 for (i = 0; i < dim; ++i) {
4943 int j = isl_basic_map_alloc_equality(
4944 (struct isl_basic_map *)bset);
4947 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
4948 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
4949 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
4950 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
4952 return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
4954 isl_basic_map_free(bmap);
4959 * returns range - domain
4961 struct isl_set *isl_map_deltas(struct isl_map *map)
4965 struct isl_set *result;
4970 isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
4971 dim = isl_map_get_dim(map);
4972 dim = isl_dim_domain(dim);
4973 result = isl_set_alloc_dim(dim, map->n, map->flags);
4976 for (i = 0; i < map->n; ++i)
4977 result = isl_set_add_basic_set(result,
4978 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
4986 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
4988 struct isl_basic_map *bmap;
4996 nparam = dims->nparam;
4998 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5002 for (i = 0; i < dim; ++i) {
5003 int j = isl_basic_map_alloc_equality(bmap);
5006 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5007 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5008 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5010 return isl_basic_map_finalize(bmap);
5012 isl_basic_map_free(bmap);
5016 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
5018 struct isl_dim *dim = isl_dim_map(set_dim);
5021 return basic_map_identity(dim);
5024 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5026 if (!model || !model->dim)
5028 isl_assert(model->ctx,
5029 model->dim->n_in == model->dim->n_out, return NULL);
5030 return basic_map_identity(isl_dim_copy(model->dim));
5033 static struct isl_map *map_identity(struct isl_dim *dim)
5035 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
5036 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
5039 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
5041 struct isl_dim *dim = isl_dim_map(set_dim);
5044 return map_identity(dim);
5047 struct isl_map *isl_map_identity_like(struct isl_map *model)
5049 if (!model || !model->dim)
5051 isl_assert(model->ctx,
5052 model->dim->n_in == model->dim->n_out, return NULL);
5053 return map_identity(isl_dim_copy(model->dim));
5056 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5058 if (!model || !model->dim)
5060 isl_assert(model->ctx,
5061 model->dim->n_in == model->dim->n_out, return NULL);
5062 return map_identity(isl_dim_copy(model->dim));
5065 /* Construct a basic set with all set dimensions having only non-negative
5068 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5073 struct isl_basic_set *bset;
5077 nparam = dims->nparam;
5079 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5082 for (i = 0; i < dim; ++i) {
5083 int k = isl_basic_set_alloc_inequality(bset);
5086 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5087 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5091 isl_basic_set_free(bset);
5095 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5096 enum isl_dim_type type, unsigned first, unsigned n)
5099 isl_basic_set *nonneg = NULL;
5100 isl_basic_set *neg = NULL;
5107 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5109 for (i = 0; i < n; ++i) {
5113 nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5114 k = isl_basic_set_alloc_inequality(nonneg);
5117 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5118 isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
5120 neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5121 k = isl_basic_set_alloc_inequality(neg);
5124 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5125 isl_int_set_si(neg->ineq[k][0], -1);
5126 isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
5128 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5133 isl_basic_set_free(nonneg);
5134 isl_basic_set_free(neg);
5139 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5141 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5144 int isl_basic_map_is_subset(
5145 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5148 struct isl_map *map1;
5149 struct isl_map *map2;
5151 if (!bmap1 || !bmap2)
5154 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5155 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5157 is_subset = isl_map_is_subset(map1, map2);
5165 int isl_basic_map_is_equal(
5166 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5170 if (!bmap1 || !bmap2)
5172 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5175 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5179 int isl_basic_set_is_equal(
5180 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5182 return isl_basic_map_is_equal(
5183 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5186 int isl_map_is_empty(struct isl_map *map)
5193 for (i = 0; i < map->n; ++i) {
5194 is_empty = isl_basic_map_is_empty(map->p[i]);
5203 int isl_map_fast_is_empty(struct isl_map *map)
5205 return map ? map->n == 0 : -1;
5208 int isl_set_fast_is_empty(struct isl_set *set)
5210 return set ? set->n == 0 : -1;
5213 int isl_set_is_empty(struct isl_set *set)
5215 return isl_map_is_empty((struct isl_map *)set);
5218 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
5224 is_subset = isl_map_is_subset(map1, map2);
5227 is_subset = isl_map_is_subset(map2, map1);
5231 int isl_basic_map_is_strict_subset(
5232 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5236 if (!bmap1 || !bmap2)
5238 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5241 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5242 if (is_subset == -1)
5247 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
5253 is_subset = isl_map_is_subset(map1, map2);
5256 is_subset = isl_map_is_subset(map2, map1);
5257 if (is_subset == -1)
5262 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5264 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5267 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5271 return bmap->n_eq == 0 && bmap->n_ineq == 0;
5274 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5278 return bset->n_eq == 0 && bset->n_ineq == 0;
5281 int isl_map_fast_is_universe(__isl_keep isl_map *map)
5286 return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
5289 int isl_set_fast_is_universe(__isl_keep isl_set *set)
5291 return isl_map_fast_is_universe((isl_map *) set);
5294 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
5296 struct isl_basic_set *bset = NULL;
5297 struct isl_vec *sample = NULL;
5304 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
5307 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
5308 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
5309 copy = isl_basic_map_remove_redundancies(copy);
5310 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
5311 isl_basic_map_free(copy);
5315 total = 1 + isl_basic_map_total_dim(bmap);
5316 if (bmap->sample && bmap->sample->size == total) {
5317 int contains = isl_basic_map_contains(bmap, bmap->sample);
5323 isl_vec_free(bmap->sample);
5324 bmap->sample = NULL;
5325 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
5328 sample = isl_basic_set_sample_vec(bset);
5331 empty = sample->size == 0;
5332 isl_vec_free(bmap->sample);
5333 bmap->sample = sample;
5335 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
5340 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
5344 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
5347 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
5351 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
5354 int isl_basic_set_is_empty(struct isl_basic_set *bset)
5356 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
5359 struct isl_map *isl_basic_map_union(
5360 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5362 struct isl_map *map;
5363 if (!bmap1 || !bmap2)
5366 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
5368 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
5371 map = isl_map_add_basic_map(map, bmap1);
5372 map = isl_map_add_basic_map(map, bmap2);
5375 isl_basic_map_free(bmap1);
5376 isl_basic_map_free(bmap2);
5380 struct isl_set *isl_basic_set_union(
5381 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5383 return (struct isl_set *)isl_basic_map_union(
5384 (struct isl_basic_map *)bset1,
5385 (struct isl_basic_map *)bset2);
5388 /* Order divs such that any div only depends on previous divs */
5389 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
5397 off = isl_dim_total(bmap->dim);
5399 for (i = 0; i < bmap->n_div; ++i) {
5401 if (isl_int_is_zero(bmap->div[i][0]))
5403 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
5407 isl_basic_map_swap_div(bmap, i, i + pos);
5413 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
5415 return (struct isl_basic_set *)
5416 isl_basic_map_order_divs((struct isl_basic_map *)bset);
5419 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
5426 for (i = 0; i < map->n; ++i) {
5427 map->p[i] = isl_basic_map_order_divs(map->p[i]);
5438 /* Look for a div in dst that corresponds to the div "div" in src.
5439 * The divs before "div" in src and dst are assumed to be the same.
5441 * Returns -1 if no corresponding div was found and the position
5442 * of the corresponding div in dst otherwise.
5444 static int find_div(struct isl_basic_map *dst,
5445 struct isl_basic_map *src, unsigned div)
5449 unsigned total = isl_dim_total(src->dim);
5451 isl_assert(dst->ctx, div <= dst->n_div, return -1);
5452 for (i = div; i < dst->n_div; ++i)
5453 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
5454 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
5455 dst->n_div - div) == -1)
5460 struct isl_basic_map *isl_basic_map_align_divs(
5461 struct isl_basic_map *dst, struct isl_basic_map *src)
5464 unsigned total = isl_dim_total(src->dim);
5469 if (src->n_div == 0)
5472 for (i = 0; i < src->n_div; ++i)
5473 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
5475 src = isl_basic_map_order_divs(src);
5476 dst = isl_basic_map_cow(dst);
5477 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
5478 src->n_div, 0, 2 * src->n_div);
5481 for (i = 0; i < src->n_div; ++i) {
5482 int j = find_div(dst, src, i);
5484 j = isl_basic_map_alloc_div(dst);
5487 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
5488 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
5489 if (isl_basic_map_add_div_constraints(dst, j) < 0)
5493 isl_basic_map_swap_div(dst, i, j);
5497 isl_basic_map_free(dst);
5501 struct isl_basic_set *isl_basic_set_align_divs(
5502 struct isl_basic_set *dst, struct isl_basic_set *src)
5504 return (struct isl_basic_set *)isl_basic_map_align_divs(
5505 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
5508 struct isl_map *isl_map_align_divs(struct isl_map *map)
5516 map = isl_map_compute_divs(map);
5517 map = isl_map_cow(map);
5521 for (i = 1; i < map->n; ++i)
5522 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5523 for (i = 1; i < map->n; ++i)
5524 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5526 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5530 struct isl_set *isl_set_align_divs(struct isl_set *set)
5532 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5535 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5539 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5540 map = isl_map_intersect_domain(map, set);
5541 set = isl_map_range(map);
5549 /* There is no need to cow as removing empty parts doesn't change
5550 * the meaning of the set.
5552 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5559 for (i = map->n-1; i >= 0; --i) {
5560 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5562 isl_basic_map_free(map->p[i]);
5563 if (i != map->n-1) {
5564 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5565 map->p[i] = map->p[map->n-1];
5573 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
5575 return (struct isl_set *)
5576 isl_map_remove_empty_parts((struct isl_map *)set);
5579 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
5581 struct isl_basic_map *bmap;
5582 if (!map || map->n == 0)
5584 bmap = map->p[map->n-1];
5585 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
5586 return isl_basic_map_copy(bmap);
5589 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
5591 return (struct isl_basic_set *)
5592 isl_map_copy_basic_map((struct isl_map *)set);
5595 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
5596 __isl_keep isl_basic_map *bmap)
5602 for (i = map->n-1; i >= 0; --i) {
5603 if (map->p[i] != bmap)
5605 map = isl_map_cow(map);
5608 isl_basic_map_free(map->p[i]);
5609 if (i != map->n-1) {
5610 ISL_F_CLR(map, ISL_SET_NORMALIZED);
5611 map->p[i] = map->p[map->n-1];
5622 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
5623 struct isl_basic_set *bset)
5625 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
5626 (struct isl_basic_map *)bset);
5629 /* Given two basic sets bset1 and bset2, compute the maximal difference
5630 * between the values of dimension pos in bset1 and those in bset2
5631 * for any common value of the parameters and dimensions preceding pos.
5633 static enum isl_lp_result basic_set_maximal_difference_at(
5634 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
5635 int pos, isl_int *opt)
5637 struct isl_dim *dims;
5638 struct isl_basic_map *bmap1 = NULL;
5639 struct isl_basic_map *bmap2 = NULL;
5640 struct isl_ctx *ctx;
5641 struct isl_vec *obj;
5644 unsigned dim1, dim2;
5645 enum isl_lp_result res;
5647 if (!bset1 || !bset2)
5648 return isl_lp_error;
5650 nparam = isl_basic_set_n_param(bset1);
5651 dim1 = isl_basic_set_n_dim(bset1);
5652 dim2 = isl_basic_set_n_dim(bset2);
5653 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
5654 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
5655 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
5656 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
5657 if (!bmap1 || !bmap2)
5659 bmap1 = isl_basic_map_cow(bmap1);
5660 bmap1 = isl_basic_map_extend(bmap1, nparam,
5661 pos, (dim1 - pos) + (dim2 - pos),
5662 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
5663 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
5666 total = isl_basic_map_total_dim(bmap1);
5668 obj = isl_vec_alloc(ctx, 1 + total);
5669 isl_seq_clr(obj->block.data, 1 + total);
5670 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
5671 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
5674 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
5676 isl_basic_map_free(bmap1);
5680 isl_basic_map_free(bmap1);
5681 isl_basic_map_free(bmap2);
5682 return isl_lp_error;
5685 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5686 * for any common value of the parameters and dimensions preceding pos
5687 * in both basic sets, the values of dimension pos in bset1 are
5688 * smaller or larger than those in bset2.
5691 * 1 if bset1 follows bset2
5692 * -1 if bset1 precedes bset2
5693 * 0 if bset1 and bset2 are incomparable
5694 * -2 if some error occurred.
5696 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
5697 struct isl_basic_set *bset2, int pos)
5700 enum isl_lp_result res;
5705 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5707 if (res == isl_lp_empty)
5709 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5710 res == isl_lp_unbounded)
5712 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5721 /* Given two basic sets bset1 and bset2, check whether
5722 * for any common value of the parameters and dimensions preceding pos
5723 * there is a value of dimension pos in bset1 that is larger
5724 * than a value of the same dimension in bset2.
5727 * 1 if there exists such a pair
5728 * 0 if there is no such pair, but there is a pair of equal values
5730 * -2 if some error occurred.
5732 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
5733 __isl_keep isl_basic_set *bset2, int pos)
5736 enum isl_lp_result res;
5741 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5743 if (res == isl_lp_empty)
5745 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5746 res == isl_lp_unbounded)
5748 else if (res == isl_lp_ok && isl_int_is_neg(opt))
5750 else if (res == isl_lp_ok)
5759 /* Given two sets set1 and set2, check whether
5760 * for any common value of the parameters and dimensions preceding pos
5761 * there is a value of dimension pos in set1 that is larger
5762 * than a value of the same dimension in set2.
5765 * 1 if there exists such a pair
5766 * 0 if there is no such pair, but there is a pair of equal values
5768 * -2 if some error occurred.
5770 int isl_set_follows_at(__isl_keep isl_set *set1,
5771 __isl_keep isl_set *set2, int pos)
5779 for (i = 0; i < set1->n; ++i)
5780 for (j = 0; j < set2->n; ++j) {
5782 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
5783 if (f == 1 || f == -2)
5792 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
5793 unsigned pos, isl_int *val)
5801 total = isl_basic_map_total_dim(bmap);
5802 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5803 for (; d+1 > pos; --d)
5804 if (!isl_int_is_zero(bmap->eq[i][1+d]))
5808 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5810 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5812 if (!isl_int_is_one(bmap->eq[i][1+d]))
5815 isl_int_neg(*val, bmap->eq[i][0]);
5821 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5822 unsigned pos, isl_int *val)
5834 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
5837 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
5838 for (i = 1; fixed == 1 && i < map->n; ++i) {
5839 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
5840 if (fixed == 1 && isl_int_ne(tmp, v))
5844 isl_int_set(*val, v);
5850 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5851 unsigned pos, isl_int *val)
5853 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
5857 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
5860 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
5863 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
5864 enum isl_dim_type type, unsigned pos, isl_int *val)
5866 if (pos >= isl_basic_map_dim(bmap, type))
5868 return isl_basic_map_fast_has_fixed_var(bmap,
5869 isl_basic_map_offset(bmap, type) - 1 + pos, val);
5872 int isl_map_fast_is_fixed(struct isl_map *map,
5873 enum isl_dim_type type, unsigned pos, isl_int *val)
5875 if (pos >= isl_map_dim(map, type))
5877 return isl_map_fast_has_fixed_var(map,
5878 map_offset(map, type) - 1 + pos, val);
5881 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5882 * then return this fixed value in *val.
5884 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
5887 return isl_basic_set_fast_has_fixed_var(bset,
5888 isl_basic_set_n_param(bset) + dim, val);
5891 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5892 * then return this fixed value in *val.
5894 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
5896 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
5899 /* Check if input variable in has fixed value and if so and if val is not NULL,
5900 * then return this fixed value in *val.
5902 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
5904 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
5907 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5908 * and if val is not NULL, then return this lower bound in *val.
5910 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
5911 unsigned dim, isl_int *val)
5913 int i, i_eq = -1, i_ineq = -1;
5920 total = isl_basic_set_total_dim(bset);
5921 nparam = isl_basic_set_n_param(bset);
5922 for (i = 0; i < bset->n_eq; ++i) {
5923 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
5929 for (i = 0; i < bset->n_ineq; ++i) {
5930 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
5932 if (i_eq != -1 || i_ineq != -1)
5936 if (i_eq == -1 && i_ineq == -1)
5938 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
5939 /* The coefficient should always be one due to normalization. */
5940 if (!isl_int_is_one(c[1+nparam+dim]))
5942 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
5944 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
5945 total - nparam - dim - 1) != -1)
5948 isl_int_neg(*val, c[0]);
5952 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
5953 unsigned dim, isl_int *val)
5965 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5969 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5971 for (i = 1; fixed == 1 && i < set->n; ++i) {
5972 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
5974 if (fixed == 1 && isl_int_ne(tmp, v))
5978 isl_int_set(*val, v);
5989 static int qsort_constraint_cmp(const void *p1, const void *p2)
5991 const struct constraint *c1 = (const struct constraint *)p1;
5992 const struct constraint *c2 = (const struct constraint *)p2;
5993 unsigned size = isl_min(c1->size, c2->size);
5994 return isl_seq_cmp(c1->c, c2->c, size);
5997 static struct isl_basic_map *isl_basic_map_sort_constraints(
5998 struct isl_basic_map *bmap)
6001 struct constraint *c;
6006 total = isl_basic_map_total_dim(bmap);
6007 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6010 for (i = 0; i < bmap->n_ineq; ++i) {
6012 c[i].c = bmap->ineq[i];
6014 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6015 for (i = 0; i < bmap->n_ineq; ++i)
6016 bmap->ineq[i] = c[i].c;
6020 isl_basic_map_free(bmap);
6024 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6025 __isl_take isl_basic_set *bset)
6027 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6028 (struct isl_basic_map *)bset);
6031 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6035 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6037 bmap = isl_basic_map_remove_redundancies(bmap);
6038 bmap = isl_basic_map_sort_constraints(bmap);
6039 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6043 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6045 return (struct isl_basic_set *)isl_basic_map_normalize(
6046 (struct isl_basic_map *)bset);
6049 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6050 const __isl_keep isl_basic_map *bmap2)
6057 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6058 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6059 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6060 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6061 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6062 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6063 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6064 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6066 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6068 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6070 if (bmap1->n_eq != bmap2->n_eq)
6071 return bmap1->n_eq - bmap2->n_eq;
6072 if (bmap1->n_ineq != bmap2->n_ineq)
6073 return bmap1->n_ineq - bmap2->n_ineq;
6074 if (bmap1->n_div != bmap2->n_div)
6075 return bmap1->n_div - bmap2->n_div;
6076 total = isl_basic_map_total_dim(bmap1);
6077 for (i = 0; i < bmap1->n_eq; ++i) {
6078 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6082 for (i = 0; i < bmap1->n_ineq; ++i) {
6083 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6087 for (i = 0; i < bmap1->n_div; ++i) {
6088 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6095 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6096 __isl_keep isl_basic_map *bmap2)
6098 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6101 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6102 __isl_keep isl_basic_set *bset2)
6104 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6105 (isl_basic_map *)bset2);
6108 static int qsort_bmap_cmp(const void *p1, const void *p2)
6110 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6111 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6113 return isl_basic_map_fast_cmp(bmap1, bmap2);
6116 /* We normalize in place, but if anything goes wrong we need
6117 * to return NULL, so we need to make sure we don't change the
6118 * meaning of any possible other copies of map.
6120 struct isl_map *isl_map_normalize(struct isl_map *map)
6123 struct isl_basic_map *bmap;
6127 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6129 for (i = 0; i < map->n; ++i) {
6130 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6133 isl_basic_map_free(map->p[i]);
6136 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6137 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6138 map = isl_map_remove_empty_parts(map);
6141 for (i = map->n - 1; i >= 1; --i) {
6142 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6144 isl_basic_map_free(map->p[i-1]);
6145 for (j = i; j < map->n; ++j)
6146 map->p[j-1] = map->p[j];
6156 struct isl_set *isl_set_normalize(struct isl_set *set)
6158 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
6161 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
6171 if (!isl_dim_equal(map1->dim, map2->dim))
6174 map1 = isl_map_copy(map1);
6175 map2 = isl_map_copy(map2);
6176 map1 = isl_map_normalize(map1);
6177 map2 = isl_map_normalize(map2);
6180 equal = map1->n == map2->n;
6181 for (i = 0; equal && i < map1->n; ++i) {
6182 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
6195 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
6197 return isl_map_fast_is_equal((struct isl_map *)set1,
6198 (struct isl_map *)set2);
6201 /* Return an interval that ranges from min to max (inclusive)
6203 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
6204 isl_int min, isl_int max)
6207 struct isl_basic_set *bset = NULL;
6209 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
6213 k = isl_basic_set_alloc_inequality(bset);
6216 isl_int_set_si(bset->ineq[k][1], 1);
6217 isl_int_neg(bset->ineq[k][0], min);
6219 k = isl_basic_set_alloc_inequality(bset);
6222 isl_int_set_si(bset->ineq[k][1], -1);
6223 isl_int_set(bset->ineq[k][0], max);
6227 isl_basic_set_free(bset);
6231 /* Return the Cartesian product of the basic sets in list (in the given order).
6233 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
6241 struct isl_basic_set *product = NULL;
6245 isl_assert(list->ctx, list->n > 0, goto error);
6246 isl_assert(list->ctx, list->p[0], goto error);
6247 nparam = isl_basic_set_n_param(list->p[0]);
6248 dim = isl_basic_set_n_dim(list->p[0]);
6249 extra = list->p[0]->n_div;
6250 n_eq = list->p[0]->n_eq;
6251 n_ineq = list->p[0]->n_ineq;
6252 for (i = 1; i < list->n; ++i) {
6253 isl_assert(list->ctx, list->p[i], goto error);
6254 isl_assert(list->ctx,
6255 nparam == isl_basic_set_n_param(list->p[i]), goto error);
6256 dim += isl_basic_set_n_dim(list->p[i]);
6257 extra += list->p[i]->n_div;
6258 n_eq += list->p[i]->n_eq;
6259 n_ineq += list->p[i]->n_ineq;
6261 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
6266 for (i = 0; i < list->n; ++i) {
6267 isl_basic_set_add_constraints(product,
6268 isl_basic_set_copy(list->p[i]), dim);
6269 dim += isl_basic_set_n_dim(list->p[i]);
6271 isl_basic_set_list_free(list);
6274 isl_basic_set_free(product);
6275 isl_basic_set_list_free(list);
6279 struct isl_basic_map *isl_basic_map_product(
6280 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6282 struct isl_dim *dim_result = NULL;
6283 struct isl_basic_map *bmap;
6284 unsigned in1, in2, out1, out2, nparam, total, pos;
6285 struct isl_dim_map *dim_map1, *dim_map2;
6287 if (!bmap1 || !bmap2)
6290 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
6291 bmap2->dim, isl_dim_param), goto error);
6292 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
6293 isl_dim_copy(bmap2->dim));
6295 in1 = isl_basic_map_n_in(bmap1);
6296 in2 = isl_basic_map_n_in(bmap2);
6297 out1 = isl_basic_map_n_out(bmap1);
6298 out2 = isl_basic_map_n_out(bmap2);
6299 nparam = isl_basic_map_n_param(bmap1);
6301 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
6302 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
6303 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
6304 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
6305 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
6306 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
6307 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
6308 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
6309 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
6310 isl_dim_map_div(dim_map1, bmap1, pos += out2);
6311 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
6313 bmap = isl_basic_map_alloc_dim(dim_result,
6314 bmap1->n_div + bmap2->n_div,
6315 bmap1->n_eq + bmap2->n_eq,
6316 bmap1->n_ineq + bmap2->n_ineq);
6317 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
6318 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
6319 bmap = isl_basic_map_simplify(bmap);
6320 return isl_basic_map_finalize(bmap);
6322 isl_basic_map_free(bmap1);
6323 isl_basic_map_free(bmap2);
6327 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6329 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
6332 struct isl_map *result;
6338 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
6339 map2->dim, isl_dim_param), goto error);
6341 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6342 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6343 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6345 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
6346 isl_dim_copy(map2->dim)),
6347 map1->n * map2->n, flags);
6350 for (i = 0; i < map1->n; ++i)
6351 for (j = 0; j < map2->n; ++j) {
6352 struct isl_basic_map *part;
6353 part = isl_basic_map_product(
6354 isl_basic_map_copy(map1->p[i]),
6355 isl_basic_map_copy(map2->p[j]));
6356 if (isl_basic_map_is_empty(part))
6357 isl_basic_map_free(part);
6359 result = isl_map_add_basic_map(result, part);
6372 /* Given two set A and B, construct its Cartesian product A x B.
6374 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
6376 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
6377 (struct isl_map *)set2);
6380 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
6383 uint32_t hash = isl_hash_init();
6388 bset = isl_basic_set_copy(bset);
6389 bset = isl_basic_set_normalize(bset);
6392 total = isl_basic_set_total_dim(bset);
6393 isl_hash_byte(hash, bset->n_eq & 0xFF);
6394 for (i = 0; i < bset->n_eq; ++i) {
6396 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
6397 isl_hash_hash(hash, c_hash);
6399 isl_hash_byte(hash, bset->n_ineq & 0xFF);
6400 for (i = 0; i < bset->n_ineq; ++i) {
6402 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
6403 isl_hash_hash(hash, c_hash);
6405 isl_hash_byte(hash, bset->n_div & 0xFF);
6406 for (i = 0; i < bset->n_div; ++i) {
6408 if (isl_int_is_zero(bset->div[i][0]))
6410 isl_hash_byte(hash, i & 0xFF);
6411 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
6412 isl_hash_hash(hash, c_hash);
6414 isl_basic_set_free(bset);
6418 uint32_t isl_set_get_hash(struct isl_set *set)
6425 set = isl_set_copy(set);
6426 set = isl_set_normalize(set);
6430 hash = isl_hash_init();
6431 for (i = 0; i < set->n; ++i) {
6433 bset_hash = isl_basic_set_get_hash(set->p[i]);
6434 isl_hash_hash(hash, bset_hash);
6442 /* Check if the value for dimension dim is completely determined
6443 * by the values of the other parameters and variables.
6444 * That is, check if dimension dim is involved in an equality.
6446 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
6453 nparam = isl_basic_set_n_param(bset);
6454 for (i = 0; i < bset->n_eq; ++i)
6455 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
6460 /* Check if the value for dimension dim is completely determined
6461 * by the values of the other parameters and variables.
6462 * That is, check if dimension dim is involved in an equality
6463 * for each of the subsets.
6465 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
6471 for (i = 0; i < set->n; ++i) {
6473 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
6480 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
6481 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
6488 for (i = 0; i < map->n; ++i)
6489 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
6495 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
6496 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
6503 for (i = 0; i < set->n; ++i)
6504 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
6510 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
6512 struct isl_dim *dim;
6517 if (bset->n_div == 0)
6520 bset = isl_basic_set_cow(bset);
6524 dim = isl_basic_set_get_dim(bset);
6525 dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
6528 isl_dim_free(bset->dim);
6532 bset = isl_basic_set_finalize(bset);
6536 isl_basic_set_free(bset);
6540 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
6543 struct isl_dim *dim;
6546 set = isl_set_align_divs(set);
6550 if (set->n == 0 || set->p[0]->n_div == 0)
6553 set = isl_set_cow(set);
6557 n_div = set->p[0]->n_div;
6558 dim = isl_set_get_dim(set);
6559 dim = isl_dim_add(dim, isl_dim_set, n_div);
6562 isl_dim_free(set->dim);
6565 for (i = 0; i < set->n; ++i) {
6566 set->p[i] = isl_basic_set_lift(set->p[i]);
6577 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
6579 struct isl_dim *dim;
6580 struct isl_basic_map *bmap;
6587 set = isl_set_align_divs(set);
6592 dim = isl_set_get_dim(set);
6593 if (set->n == 0 || set->p[0]->n_div == 0) {
6595 return isl_map_identity(dim);
6598 n_div = set->p[0]->n_div;
6599 dim = isl_dim_map(dim);
6600 n_param = isl_dim_size(dim, isl_dim_param);
6601 n_set = isl_dim_size(dim, isl_dim_in);
6602 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
6603 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
6604 for (i = 0; i < n_set; ++i)
6605 bmap = var_equal(bmap, i);
6607 total = n_param + n_set + n_set + n_div;
6608 for (i = 0; i < n_div; ++i) {
6609 k = isl_basic_map_alloc_inequality(bmap);
6612 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
6613 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
6614 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
6615 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
6616 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
6617 set->p[0]->div[i][0]);
6619 l = isl_basic_map_alloc_inequality(bmap);
6622 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
6623 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
6624 set->p[0]->div[i][0]);
6625 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
6629 return isl_map_from_basic_map(bmap);
6632 isl_basic_map_free(bmap);
6636 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
6644 dim = isl_basic_set_total_dim(bset);
6645 size += bset->n_eq * (1 + dim);
6646 size += bset->n_ineq * (1 + dim);
6647 size += bset->n_div * (2 + dim);
6652 int isl_set_size(__isl_keep isl_set *set)
6660 for (i = 0; i < set->n; ++i)
6661 size += isl_basic_set_size(set->p[i]);
6666 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
6667 enum isl_dim_type type, unsigned pos)
6675 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
6677 pos += isl_basic_map_offset(bmap, type);
6679 for (i = 0; i < bmap->n_eq; ++i)
6680 if (!isl_int_is_zero(bmap->eq[i][pos]))
6684 for (i = 0; i < bmap->n_ineq; ++i) {
6685 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
6692 return lower && upper;
6695 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
6696 enum isl_dim_type type, unsigned pos)
6703 for (i = 0; i < map->n; ++i) {
6705 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
6706 if (bounded < 0 || !bounded)
6713 /* Return 1 if the specified dim is involved in both an upper bound
6714 * and a lower bound.
6716 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
6717 enum isl_dim_type type, unsigned pos)
6719 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
6722 /* For each of the "n" variables starting at "first", determine
6723 * the sign of the variable and put the results in the first "n"
6724 * elements of the array "signs".
6726 * 1 means that the variable is non-negative
6727 * -1 means that the variable is non-positive
6728 * 0 means the variable attains both positive and negative values.
6730 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
6731 unsigned first, unsigned n, int *signs)
6733 isl_vec *bound = NULL;
6734 struct isl_tab *tab = NULL;
6735 struct isl_tab_undo *snap;
6738 if (!bset || !signs)
6741 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
6742 tab = isl_tab_from_basic_set(bset);
6746 isl_seq_clr(bound->el, bound->size);
6747 isl_int_set_si(bound->el[0], -1);
6749 snap = isl_tab_snap(tab);
6750 for (i = 0; i < n; ++i) {
6753 isl_int_set_si(bound->el[1 + first + i], -1);
6754 if (isl_tab_add_ineq(tab, bound->el) < 0)
6757 isl_int_set_si(bound->el[1 + first + i], 0);
6758 if (isl_tab_rollback(tab, snap) < 0)
6766 isl_int_set_si(bound->el[1 + first + i], 1);
6767 if (isl_tab_add_ineq(tab, bound->el) < 0)
6770 isl_int_set_si(bound->el[1 + first + i], 0);
6771 if (isl_tab_rollback(tab, snap) < 0)
6774 signs[i] = empty ? -1 : 0;
6778 isl_vec_free(bound);
6782 isl_vec_free(bound);
6786 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
6787 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
6789 if (!bset || !signs)
6791 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
6794 first += pos(bset->dim, type) - 1;
6795 return isl_basic_set_vars_get_sign(bset, first, n, signs);
6798 /* Check if the given map is single-valued.
6803 * and check if the result is a subset of the identity mapping.
6805 int isl_map_is_single_valued(__isl_keep isl_map *map)
6811 test = isl_map_reverse(isl_map_copy(map));
6812 test = isl_map_apply_range(test, isl_map_copy(map));
6814 id = isl_map_identity(isl_dim_range(isl_map_get_dim(map)));
6816 sv = isl_map_is_subset(test, id);
6824 int isl_map_is_bijective(__isl_keep isl_map *map)
6828 sv = isl_map_is_single_valued(map);
6832 map = isl_map_copy(map);
6833 map = isl_map_reverse(map);
6834 sv = isl_map_is_single_valued(map);
6840 int isl_set_is_singleton(__isl_keep isl_set *set)
6842 return isl_map_is_single_valued((isl_map *)set);
6845 int isl_map_is_translation(__isl_keep isl_map *map)
6850 delta = isl_map_deltas(isl_map_copy(map));
6851 ok = isl_set_is_singleton(delta);
6852 isl_set_free(delta);
6857 static int unique(isl_int *p, unsigned pos, unsigned len)
6859 if (isl_seq_first_non_zero(p, pos) != -1)
6861 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
6866 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
6875 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
6878 nvar = isl_basic_set_dim(bset, isl_dim_set);
6879 ovar = isl_dim_offset(bset->dim, isl_dim_set);
6880 for (j = 0; j < nvar; ++j) {
6881 int lower = 0, upper = 0;
6882 for (i = 0; i < bset->n_eq; ++i) {
6883 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
6885 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
6891 for (i = 0; i < bset->n_ineq; ++i) {
6892 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
6894 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
6896 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
6901 if (!lower || !upper)
6908 int isl_set_is_box(__isl_keep isl_set *set)
6915 return isl_basic_set_is_box(set->p[0]);