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
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
17 #include "isl_space_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
33 #include <isl_options_private.h>
35 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
38 case isl_dim_param: return dim->nparam;
39 case isl_dim_in: return dim->n_in;
40 case isl_dim_out: return dim->n_out;
41 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
46 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
49 case isl_dim_param: return 1;
50 case isl_dim_in: return 1 + dim->nparam;
51 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
56 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
57 enum isl_dim_type type)
62 case isl_dim_cst: return 1;
65 case isl_dim_out: return isl_space_dim(bmap->dim, type);
66 case isl_dim_div: return bmap->n_div;
67 case isl_dim_all: return isl_basic_map_total_dim(bmap);
72 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
74 return map ? n(map->dim, type) : 0;
77 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
79 return set ? n(set->dim, type) : 0;
82 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
83 enum isl_dim_type type)
85 isl_space *dim = bmap->dim;
87 case isl_dim_cst: return 0;
88 case isl_dim_param: return 1;
89 case isl_dim_in: return 1 + dim->nparam;
90 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
91 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
96 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
97 enum isl_dim_type type)
99 return isl_basic_map_offset(bset, type);
102 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
104 return pos(map->dim, type);
107 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
108 enum isl_dim_type type)
110 return isl_basic_map_dim(bset, type);
113 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
115 return isl_basic_set_dim(bset, isl_dim_set);
118 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
120 return isl_basic_set_dim(bset, isl_dim_param);
123 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
127 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
130 unsigned isl_set_n_dim(__isl_keep isl_set *set)
132 return isl_set_dim(set, isl_dim_set);
135 unsigned isl_set_n_param(__isl_keep isl_set *set)
137 return isl_set_dim(set, isl_dim_param);
140 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
142 return bmap ? bmap->dim->n_in : 0;
145 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
147 return bmap ? bmap->dim->n_out : 0;
150 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
152 return bmap ? bmap->dim->nparam : 0;
155 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
157 return bmap ? bmap->n_div : 0;
160 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
162 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
165 unsigned isl_map_n_in(const struct isl_map *map)
167 return map ? map->dim->n_in : 0;
170 unsigned isl_map_n_out(const struct isl_map *map)
172 return map ? map->dim->n_out : 0;
175 unsigned isl_map_n_param(const struct isl_map *map)
177 return map ? map->dim->nparam : 0;
180 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
185 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
188 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
191 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
192 struct isl_basic_set *bset)
197 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
200 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
203 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
208 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
211 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
214 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
215 struct isl_basic_set *bset)
220 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
223 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
226 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
228 return bmap ? bmap->ctx : NULL;
231 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
233 return bset ? bset->ctx : NULL;
236 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
238 return map ? map->ctx : NULL;
241 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
243 return set ? set->ctx : NULL;
246 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
250 return isl_space_copy(bmap->dim);
253 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
257 return isl_space_copy(bset->dim);
260 /* Extract the divs in "bmap" as a matrix.
262 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
273 ctx = isl_basic_map_get_ctx(bmap);
274 total = isl_space_dim(bmap->dim, isl_dim_all);
275 cols = 1 + 1 + total + bmap->n_div;
276 div = isl_mat_alloc(ctx, bmap->n_div, cols);
280 for (i = 0; i < bmap->n_div; ++i)
281 isl_seq_cpy(div->row[i], bmap->div[i], cols);
286 __isl_give isl_local_space *isl_basic_map_get_local_space(
287 __isl_keep isl_basic_map *bmap)
294 div = isl_basic_map_get_divs(bmap);
295 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
298 __isl_give isl_local_space *isl_basic_set_get_local_space(
299 __isl_keep isl_basic_set *bset)
301 return isl_basic_map_get_local_space(bset);
304 __isl_give isl_basic_map *isl_basic_map_from_local_space(
305 __isl_take isl_local_space *ls)
314 n_div = isl_local_space_dim(ls, isl_dim_div);
315 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
316 n_div, 0, 2 * n_div);
318 for (i = 0; i < n_div; ++i)
319 if (isl_basic_map_alloc_div(bmap) < 0)
322 for (i = 0; i < n_div; ++i) {
323 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
324 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
328 isl_local_space_free(ls);
331 isl_local_space_free(ls);
332 isl_basic_map_free(bmap);
336 __isl_give isl_basic_set *isl_basic_set_from_local_space(
337 __isl_take isl_local_space *ls)
339 return isl_basic_map_from_local_space(ls);
342 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
346 return isl_space_copy(map->dim);
349 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
353 return isl_space_copy(set->dim);
356 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
357 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
359 bmap = isl_basic_map_cow(bmap);
362 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
365 bmap = isl_basic_map_finalize(bmap);
368 isl_basic_map_free(bmap);
372 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
373 __isl_take isl_basic_set *bset, const char *s)
375 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
378 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
379 enum isl_dim_type type)
381 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
384 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
385 enum isl_dim_type type, const char *s)
389 map = isl_map_cow(map);
393 map->dim = isl_space_set_tuple_name(map->dim, type, s);
397 for (i = 0; i < map->n; ++i) {
398 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
409 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
410 enum isl_dim_type type)
412 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
415 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
418 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
421 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
422 enum isl_dim_type type, __isl_take isl_id *id)
424 map = isl_map_cow(map);
426 return isl_id_free(id);
428 map->dim = isl_space_set_tuple_id(map->dim, type, id);
430 return isl_map_reset_space(map, isl_space_copy(map->dim));
433 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
434 __isl_take isl_id *id)
436 return isl_map_set_tuple_id(set, isl_dim_set, id);
439 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
440 enum isl_dim_type type)
442 map = isl_map_cow(map);
446 map->dim = isl_space_reset_tuple_id(map->dim, type);
448 return isl_map_reset_space(map, isl_space_copy(map->dim));
451 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
453 return isl_map_reset_tuple_id(set, isl_dim_set);
456 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
458 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
461 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
462 enum isl_dim_type type)
464 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
467 int isl_set_has_tuple_id(__isl_keep isl_set *set)
469 return isl_map_has_tuple_id(set, isl_dim_set);
472 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
474 return isl_map_get_tuple_id(set, isl_dim_set);
477 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
479 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
482 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
484 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
487 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
488 enum isl_dim_type type, unsigned pos)
490 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
493 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
494 enum isl_dim_type type, unsigned pos)
496 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
499 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
500 enum isl_dim_type type, unsigned pos)
502 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
505 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
506 enum isl_dim_type type, unsigned pos)
508 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
511 /* Does the given dimension have a name?
513 int isl_set_has_dim_name(__isl_keep isl_set *set,
514 enum isl_dim_type type, unsigned pos)
516 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
519 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
520 __isl_take isl_basic_map *bmap,
521 enum isl_dim_type type, unsigned pos, const char *s)
523 bmap = isl_basic_map_cow(bmap);
526 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
531 isl_basic_map_free(bmap);
535 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
536 enum isl_dim_type type, unsigned pos, const char *s)
540 map = isl_map_cow(map);
544 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
548 for (i = 0; i < map->n; ++i) {
549 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
560 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
561 __isl_take isl_basic_set *bset,
562 enum isl_dim_type type, unsigned pos, const char *s)
564 return (isl_basic_set *)isl_basic_map_set_dim_name(
565 (isl_basic_map *)bset, type, pos, s);
568 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
569 enum isl_dim_type type, unsigned pos, const char *s)
571 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
574 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
575 enum isl_dim_type type, unsigned pos)
577 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
580 int isl_map_has_dim_id(__isl_keep isl_map *map,
581 enum isl_dim_type type, unsigned pos)
583 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
586 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
587 enum isl_dim_type type, unsigned pos)
589 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
592 int isl_set_has_dim_id(__isl_keep isl_set *set,
593 enum isl_dim_type type, unsigned pos)
595 return isl_map_has_dim_id(set, type, pos);
598 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
599 enum isl_dim_type type, unsigned pos)
601 return isl_map_get_dim_id(set, type, pos);
604 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
605 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
607 map = isl_map_cow(map);
609 return isl_id_free(id);
611 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
613 return isl_map_reset_space(map, isl_space_copy(map->dim));
616 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
617 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
619 return isl_map_set_dim_id(set, type, pos, id);
622 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
623 __isl_keep isl_id *id)
627 return isl_space_find_dim_by_id(map->dim, type, id);
630 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
631 __isl_keep isl_id *id)
633 return isl_map_find_dim_by_id(set, type, id);
636 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
641 return isl_space_find_dim_by_name(map->dim, type, name);
644 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
647 return isl_map_find_dim_by_name(set, type, name);
650 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
654 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
657 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
659 return isl_basic_map_is_rational(bset);
662 /* Is this basic set a parameter domain?
664 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
668 return isl_space_is_params(bset->dim);
671 /* Is this set a parameter domain?
673 int isl_set_is_params(__isl_keep isl_set *set)
677 return isl_space_is_params(set->dim);
680 /* Is this map actually a parameter domain?
681 * Users should never call this function. Outside of isl,
682 * a map can never be a parameter domain.
684 int isl_map_is_params(__isl_keep isl_map *map)
688 return isl_space_is_params(map->dim);
691 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
692 struct isl_basic_map *bmap, unsigned extra,
693 unsigned n_eq, unsigned n_ineq)
696 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
701 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
702 if (isl_blk_is_error(bmap->block))
705 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
710 bmap->block2 = isl_blk_empty();
713 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
714 if (isl_blk_is_error(bmap->block2))
717 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
722 for (i = 0; i < n_ineq + n_eq; ++i)
723 bmap->ineq[i] = bmap->block.data + i * row_size;
725 for (i = 0; i < extra; ++i)
726 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
730 bmap->c_size = n_eq + n_ineq;
731 bmap->eq = bmap->ineq + n_ineq;
740 isl_basic_map_free(bmap);
744 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
745 unsigned nparam, unsigned dim, unsigned extra,
746 unsigned n_eq, unsigned n_ineq)
748 struct isl_basic_map *bmap;
751 space = isl_space_set_alloc(ctx, nparam, dim);
755 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
756 return (struct isl_basic_set *)bmap;
759 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
760 unsigned extra, unsigned n_eq, unsigned n_ineq)
762 struct isl_basic_map *bmap;
765 isl_assert(dim->ctx, dim->n_in == 0, goto error);
766 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
767 return (struct isl_basic_set *)bmap;
773 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
774 unsigned extra, unsigned n_eq, unsigned n_ineq)
776 struct isl_basic_map *bmap;
780 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
785 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
791 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
792 unsigned nparam, unsigned in, unsigned out, unsigned extra,
793 unsigned n_eq, unsigned n_ineq)
795 struct isl_basic_map *bmap;
798 dim = isl_space_alloc(ctx, nparam, in, out);
802 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
806 static void dup_constraints(
807 struct isl_basic_map *dst, struct isl_basic_map *src)
810 unsigned total = isl_basic_map_total_dim(src);
812 for (i = 0; i < src->n_eq; ++i) {
813 int j = isl_basic_map_alloc_equality(dst);
814 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
817 for (i = 0; i < src->n_ineq; ++i) {
818 int j = isl_basic_map_alloc_inequality(dst);
819 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
822 for (i = 0; i < src->n_div; ++i) {
823 int j = isl_basic_map_alloc_div(dst);
824 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
826 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
829 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
831 struct isl_basic_map *dup;
835 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
836 bmap->n_div, bmap->n_eq, bmap->n_ineq);
839 dup_constraints(dup, bmap);
840 dup->flags = bmap->flags;
841 dup->sample = isl_vec_copy(bmap->sample);
845 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
847 struct isl_basic_map *dup;
849 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
850 return (struct isl_basic_set *)dup;
853 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
858 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
862 return isl_basic_set_dup(bset);
865 struct isl_set *isl_set_copy(struct isl_set *set)
874 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
879 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
883 bmap = isl_basic_map_dup(bmap);
885 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
889 struct isl_map *isl_map_copy(struct isl_map *map)
898 void isl_basic_map_free(struct isl_basic_map *bmap)
906 isl_ctx_deref(bmap->ctx);
908 isl_blk_free(bmap->ctx, bmap->block2);
910 isl_blk_free(bmap->ctx, bmap->block);
911 isl_vec_free(bmap->sample);
912 isl_space_free(bmap->dim);
916 void isl_basic_set_free(struct isl_basic_set *bset)
918 isl_basic_map_free((struct isl_basic_map *)bset);
921 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
923 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
926 __isl_give isl_map *isl_map_align_params_map_map_and(
927 __isl_take isl_map *map1, __isl_take isl_map *map2,
928 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
929 __isl_take isl_map *map2))
933 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
934 return fn(map1, map2);
935 if (!isl_space_has_named_params(map1->dim) ||
936 !isl_space_has_named_params(map2->dim))
937 isl_die(map1->ctx, isl_error_invalid,
938 "unaligned unnamed parameters", goto error);
939 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
940 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
941 return fn(map1, map2);
948 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
949 __isl_keep isl_map *map2,
950 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
956 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
957 return fn(map1, map2);
958 if (!isl_space_has_named_params(map1->dim) ||
959 !isl_space_has_named_params(map2->dim))
960 isl_die(map1->ctx, isl_error_invalid,
961 "unaligned unnamed parameters", return -1);
962 map1 = isl_map_copy(map1);
963 map2 = isl_map_copy(map2);
964 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
965 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
972 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
978 isl_assert(ctx, room_for_con(bmap, 1), return -1);
979 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
981 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
982 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
983 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
984 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
985 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
986 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
988 int j = isl_basic_map_alloc_inequality(bmap);
992 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
993 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1000 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1001 bmap->extra - bmap->n_div);
1002 return bmap->n_eq++;
1005 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1007 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1010 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1014 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1019 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1021 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1024 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1029 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1031 if (pos != bmap->n_eq - 1) {
1033 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1034 bmap->eq[bmap->n_eq - 1] = t;
1040 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1042 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1045 void isl_basic_map_inequality_to_equality(
1046 struct isl_basic_map *bmap, unsigned pos)
1050 t = bmap->ineq[pos];
1051 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1052 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1057 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1058 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1059 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1060 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1063 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1065 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1068 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1070 struct isl_ctx *ctx;
1074 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1075 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1076 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1077 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1078 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1079 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1080 1 + isl_basic_map_total_dim(bmap),
1081 bmap->extra - bmap->n_div);
1082 return bmap->n_ineq++;
1085 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1087 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1090 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1094 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1099 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1101 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1104 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1109 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1111 if (pos != bmap->n_ineq - 1) {
1112 t = bmap->ineq[pos];
1113 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1114 bmap->ineq[bmap->n_ineq - 1] = t;
1115 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1121 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1123 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1126 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1131 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1134 k = isl_basic_map_alloc_equality(bmap);
1137 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1140 isl_basic_map_free(bmap);
1144 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1147 return (isl_basic_set *)
1148 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1151 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1156 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1159 k = isl_basic_map_alloc_inequality(bmap);
1162 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1165 isl_basic_map_free(bmap);
1169 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1172 return (isl_basic_set *)
1173 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1176 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1180 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1181 isl_seq_clr(bmap->div[bmap->n_div] +
1182 1 + 1 + isl_basic_map_total_dim(bmap),
1183 bmap->extra - bmap->n_div);
1184 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1185 return bmap->n_div++;
1188 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1190 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1193 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1197 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1202 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1204 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1207 /* Copy constraint from src to dst, putting the vars of src at offset
1208 * dim_off in dst and the divs of src at offset div_off in dst.
1209 * If both sets are actually map, then dim_off applies to the input
1212 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1213 struct isl_basic_map *src_map, isl_int *src,
1214 unsigned in_off, unsigned out_off, unsigned div_off)
1216 unsigned src_nparam = isl_basic_map_n_param(src_map);
1217 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1218 unsigned src_in = isl_basic_map_n_in(src_map);
1219 unsigned dst_in = isl_basic_map_n_in(dst_map);
1220 unsigned src_out = isl_basic_map_n_out(src_map);
1221 unsigned dst_out = isl_basic_map_n_out(dst_map);
1222 isl_int_set(dst[0], src[0]);
1223 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1224 if (dst_nparam > src_nparam)
1225 isl_seq_clr(dst+1+src_nparam,
1226 dst_nparam - src_nparam);
1227 isl_seq_clr(dst+1+dst_nparam, in_off);
1228 isl_seq_cpy(dst+1+dst_nparam+in_off,
1230 isl_min(dst_in-in_off, src_in));
1231 if (dst_in-in_off > src_in)
1232 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1233 dst_in - in_off - src_in);
1234 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1235 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1236 src+1+src_nparam+src_in,
1237 isl_min(dst_out-out_off, src_out));
1238 if (dst_out-out_off > src_out)
1239 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1240 dst_out - out_off - src_out);
1241 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1242 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1243 src+1+src_nparam+src_in+src_out,
1244 isl_min(dst_map->extra-div_off, src_map->n_div));
1245 if (dst_map->n_div-div_off > src_map->n_div)
1246 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1247 div_off+src_map->n_div,
1248 dst_map->n_div - div_off - src_map->n_div);
1251 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1252 struct isl_basic_map *src_map, isl_int *src,
1253 unsigned in_off, unsigned out_off, unsigned div_off)
1255 isl_int_set(dst[0], src[0]);
1256 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1259 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1260 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1265 if (!bmap1 || !bmap2)
1268 div_off = bmap1->n_div;
1270 for (i = 0; i < bmap2->n_eq; ++i) {
1271 int i1 = isl_basic_map_alloc_equality(bmap1);
1274 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1275 i_pos, o_pos, div_off);
1278 for (i = 0; i < bmap2->n_ineq; ++i) {
1279 int i1 = isl_basic_map_alloc_inequality(bmap1);
1282 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1283 i_pos, o_pos, div_off);
1286 for (i = 0; i < bmap2->n_div; ++i) {
1287 int i1 = isl_basic_map_alloc_div(bmap1);
1290 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1291 i_pos, o_pos, div_off);
1294 isl_basic_map_free(bmap2);
1299 isl_basic_map_free(bmap1);
1300 isl_basic_map_free(bmap2);
1304 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1305 struct isl_basic_set *bset2, unsigned pos)
1307 return (struct isl_basic_set *)
1308 add_constraints((struct isl_basic_map *)bset1,
1309 (struct isl_basic_map *)bset2, 0, pos);
1312 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1313 __isl_take isl_space *dim, unsigned extra,
1314 unsigned n_eq, unsigned n_ineq)
1316 struct isl_basic_map *ext;
1326 dims_ok = isl_space_is_equal(base->dim, dim) &&
1327 base->extra >= base->n_div + extra;
1329 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1330 room_for_ineq(base, n_ineq)) {
1331 isl_space_free(dim);
1335 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1336 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1337 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1338 extra += base->extra;
1340 n_ineq += base->n_ineq;
1342 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1348 ext->sample = isl_vec_copy(base->sample);
1349 flags = base->flags;
1350 ext = add_constraints(ext, base, 0, 0);
1353 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1359 isl_space_free(dim);
1360 isl_basic_map_free(base);
1364 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1365 __isl_take isl_space *dim, unsigned extra,
1366 unsigned n_eq, unsigned n_ineq)
1368 return (struct isl_basic_set *)
1369 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1370 extra, n_eq, n_ineq);
1373 struct isl_basic_map *isl_basic_map_extend_constraints(
1374 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1378 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1382 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1383 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1384 unsigned n_eq, unsigned n_ineq)
1386 struct isl_basic_map *bmap;
1391 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1395 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1398 isl_basic_map_free(base);
1402 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1403 unsigned nparam, unsigned dim, unsigned extra,
1404 unsigned n_eq, unsigned n_ineq)
1406 return (struct isl_basic_set *)
1407 isl_basic_map_extend((struct isl_basic_map *)base,
1408 nparam, 0, dim, extra, n_eq, n_ineq);
1411 struct isl_basic_set *isl_basic_set_extend_constraints(
1412 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1414 return (struct isl_basic_set *)
1415 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1419 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1421 return (struct isl_basic_set *)
1422 isl_basic_map_cow((struct isl_basic_map *)bset);
1425 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1430 if (bmap->ref > 1) {
1432 bmap = isl_basic_map_dup(bmap);
1435 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1439 struct isl_set *isl_set_cow(struct isl_set *set)
1447 return isl_set_dup(set);
1450 struct isl_map *isl_map_cow(struct isl_map *map)
1458 return isl_map_dup(map);
1461 static void swap_vars(struct isl_blk blk, isl_int *a,
1462 unsigned a_len, unsigned b_len)
1464 isl_seq_cpy(blk.data, a+a_len, b_len);
1465 isl_seq_cpy(blk.data+b_len, a, a_len);
1466 isl_seq_cpy(a, blk.data, b_len+a_len);
1469 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1470 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1478 isl_assert(bmap->ctx,
1479 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1481 if (n1 == 0 || n2 == 0)
1484 bmap = isl_basic_map_cow(bmap);
1488 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1489 if (isl_blk_is_error(blk))
1492 for (i = 0; i < bmap->n_eq; ++i)
1494 bmap->eq[i] + pos, n1, n2);
1496 for (i = 0; i < bmap->n_ineq; ++i)
1498 bmap->ineq[i] + pos, n1, n2);
1500 for (i = 0; i < bmap->n_div; ++i)
1502 bmap->div[i]+1 + pos, n1, n2);
1504 isl_blk_free(bmap->ctx, blk);
1506 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1507 bmap = isl_basic_map_gauss(bmap, NULL);
1508 return isl_basic_map_finalize(bmap);
1510 isl_basic_map_free(bmap);
1514 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1515 __isl_take isl_basic_set *bset, unsigned n)
1520 nparam = isl_basic_set_n_param(bset);
1521 dim = isl_basic_set_n_dim(bset);
1522 isl_assert(bset->ctx, n <= dim, goto error);
1524 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1526 isl_basic_set_free(bset);
1530 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1536 total = isl_basic_map_total_dim(bmap);
1537 isl_basic_map_free_div(bmap, bmap->n_div);
1538 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1540 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1542 i = isl_basic_map_alloc_equality(bmap);
1546 isl_int_set_si(bmap->eq[i][0], 1);
1547 isl_seq_clr(bmap->eq[i]+1, total);
1548 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1549 isl_vec_free(bmap->sample);
1550 bmap->sample = NULL;
1551 return isl_basic_map_finalize(bmap);
1553 isl_basic_map_free(bmap);
1557 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1559 return (struct isl_basic_set *)
1560 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1563 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1566 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1567 isl_int *t = bmap->div[a];
1568 bmap->div[a] = bmap->div[b];
1571 for (i = 0; i < bmap->n_eq; ++i)
1572 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1574 for (i = 0; i < bmap->n_ineq; ++i)
1575 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1577 for (i = 0; i < bmap->n_div; ++i)
1578 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1579 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1582 /* Eliminate the specified n dimensions starting at first from the
1583 * constraints using Fourier-Motzkin. The dimensions themselves
1586 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1587 enum isl_dim_type type, unsigned first, unsigned n)
1596 map = isl_map_cow(map);
1600 for (i = 0; i < map->n; ++i) {
1601 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1611 /* Eliminate the specified n dimensions starting at first from the
1612 * constraints using Fourier-Motzkin. The dimensions themselves
1615 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1616 enum isl_dim_type type, unsigned first, unsigned n)
1618 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1621 /* Eliminate the specified n dimensions starting at first from the
1622 * constraints using Fourier-Motzkin. The dimensions themselves
1625 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1626 unsigned first, unsigned n)
1628 return isl_set_eliminate(set, isl_dim_set, first, n);
1631 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1632 __isl_take isl_basic_map *bmap)
1636 bmap = isl_basic_map_eliminate_vars(bmap,
1637 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1641 return isl_basic_map_finalize(bmap);
1644 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1645 __isl_take isl_basic_set *bset)
1647 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1648 (struct isl_basic_map *)bset);
1651 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1660 map = isl_map_cow(map);
1664 for (i = 0; i < map->n; ++i) {
1665 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1675 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1677 return isl_map_remove_divs(set);
1680 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1681 enum isl_dim_type type, unsigned first, unsigned n)
1685 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1687 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1689 bmap = isl_basic_map_eliminate_vars(bmap,
1690 isl_basic_map_offset(bmap, type) - 1 + first, n);
1693 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1695 bmap = isl_basic_map_drop(bmap, type, first, n);
1698 isl_basic_map_free(bmap);
1702 /* Return true if the definition of the given div (recursively) involves
1703 * any of the given variables.
1705 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1706 unsigned first, unsigned n)
1709 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1711 if (isl_int_is_zero(bmap->div[div][0]))
1713 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1716 for (i = bmap->n_div - 1; i >= 0; --i) {
1717 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1719 if (div_involves_vars(bmap, i, first, n))
1726 /* Remove all divs (recursively) involving any of the given dimensions
1727 * in their definitions.
1729 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1730 __isl_take isl_basic_map *bmap,
1731 enum isl_dim_type type, unsigned first, unsigned n)
1737 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1739 first += isl_basic_map_offset(bmap, type);
1741 for (i = bmap->n_div - 1; i >= 0; --i) {
1742 if (!div_involves_vars(bmap, i, first, n))
1744 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1752 isl_basic_map_free(bmap);
1756 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1757 enum isl_dim_type type, unsigned first, unsigned n)
1766 map = isl_map_cow(map);
1770 for (i = 0; i < map->n; ++i) {
1771 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1782 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1783 enum isl_dim_type type, unsigned first, unsigned n)
1785 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1789 /* Does the desciption of "bmap" depend on the specified dimensions?
1790 * We also check whether the dimensions appear in any of the div definitions.
1791 * In principle there is no need for this check. If the dimensions appear
1792 * in a div definition, they also appear in the defining constraints of that
1795 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1796 enum isl_dim_type type, unsigned first, unsigned n)
1803 if (first + n > isl_basic_map_dim(bmap, type))
1804 isl_die(bmap->ctx, isl_error_invalid,
1805 "index out of bounds", return -1);
1807 first += isl_basic_map_offset(bmap, type);
1808 for (i = 0; i < bmap->n_eq; ++i)
1809 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1811 for (i = 0; i < bmap->n_ineq; ++i)
1812 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1814 for (i = 0; i < bmap->n_div; ++i) {
1815 if (isl_int_is_zero(bmap->div[i][0]))
1817 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
1824 int isl_map_involves_dims(__isl_keep isl_map *map,
1825 enum isl_dim_type type, unsigned first, unsigned n)
1832 if (first + n > isl_map_dim(map, type))
1833 isl_die(map->ctx, isl_error_invalid,
1834 "index out of bounds", return -1);
1836 for (i = 0; i < map->n; ++i) {
1837 int involves = isl_basic_map_involves_dims(map->p[i],
1839 if (involves < 0 || involves)
1846 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1847 enum isl_dim_type type, unsigned first, unsigned n)
1849 return isl_basic_map_involves_dims(bset, type, first, n);
1852 int isl_set_involves_dims(__isl_keep isl_set *set,
1853 enum isl_dim_type type, unsigned first, unsigned n)
1855 return isl_map_involves_dims(set, type, first, n);
1858 /* Return true if the definition of the given div is unknown or depends
1861 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1864 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1866 if (isl_int_is_zero(bmap->div[div][0]))
1869 for (i = bmap->n_div - 1; i >= 0; --i) {
1870 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1872 if (div_is_unknown(bmap, i))
1879 /* Remove all divs that are unknown or defined in terms of unknown divs.
1881 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1882 __isl_take isl_basic_map *bmap)
1889 for (i = bmap->n_div - 1; i >= 0; --i) {
1890 if (!div_is_unknown(bmap, i))
1892 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1901 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1910 map = isl_map_cow(map);
1914 for (i = 0; i < map->n; ++i) {
1915 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1925 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1927 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1930 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1931 __isl_take isl_basic_set *bset,
1932 enum isl_dim_type type, unsigned first, unsigned n)
1934 return (isl_basic_set *)
1935 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1938 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1939 enum isl_dim_type type, unsigned first, unsigned n)
1946 map = isl_map_cow(map);
1949 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1951 for (i = 0; i < map->n; ++i) {
1952 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1953 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1957 map = isl_map_drop(map, type, first, n);
1964 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1965 enum isl_dim_type type, unsigned first, unsigned n)
1967 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1970 /* Project out n inputs starting at first using Fourier-Motzkin */
1971 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1972 unsigned first, unsigned n)
1974 return isl_map_remove_dims(map, isl_dim_in, first, n);
1977 static void dump_term(struct isl_basic_map *bmap,
1978 isl_int c, int pos, FILE *out)
1981 unsigned in = isl_basic_map_n_in(bmap);
1982 unsigned dim = in + isl_basic_map_n_out(bmap);
1983 unsigned nparam = isl_basic_map_n_param(bmap);
1985 isl_int_print(out, c, 0);
1987 if (!isl_int_is_one(c))
1988 isl_int_print(out, c, 0);
1989 if (pos < 1 + nparam) {
1990 name = isl_space_get_dim_name(bmap->dim,
1991 isl_dim_param, pos - 1);
1993 fprintf(out, "%s", name);
1995 fprintf(out, "p%d", pos - 1);
1996 } else if (pos < 1 + nparam + in)
1997 fprintf(out, "i%d", pos - 1 - nparam);
1998 else if (pos < 1 + nparam + dim)
1999 fprintf(out, "o%d", pos - 1 - nparam - in);
2001 fprintf(out, "e%d", pos - 1 - nparam - dim);
2005 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2006 int sign, FILE *out)
2010 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2014 for (i = 0, first = 1; i < len; ++i) {
2015 if (isl_int_sgn(c[i]) * sign <= 0)
2018 fprintf(out, " + ");
2020 isl_int_abs(v, c[i]);
2021 dump_term(bmap, v, i, out);
2028 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2029 const char *op, FILE *out, int indent)
2033 fprintf(out, "%*s", indent, "");
2035 dump_constraint_sign(bmap, c, 1, out);
2036 fprintf(out, " %s ", op);
2037 dump_constraint_sign(bmap, c, -1, out);
2041 for (i = bmap->n_div; i < bmap->extra; ++i) {
2042 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2044 fprintf(out, "%*s", indent, "");
2045 fprintf(out, "ERROR: unused div coefficient not zero\n");
2050 static void dump_constraints(struct isl_basic_map *bmap,
2051 isl_int **c, unsigned n,
2052 const char *op, FILE *out, int indent)
2056 for (i = 0; i < n; ++i)
2057 dump_constraint(bmap, c[i], op, out, indent);
2060 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2064 unsigned total = isl_basic_map_total_dim(bmap);
2066 for (j = 0; j < 1 + total; ++j) {
2067 if (isl_int_is_zero(exp[j]))
2069 if (!first && isl_int_is_pos(exp[j]))
2071 dump_term(bmap, exp[j], j, out);
2076 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2080 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2081 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2083 for (i = 0; i < bmap->n_div; ++i) {
2084 fprintf(out, "%*s", indent, "");
2085 fprintf(out, "e%d = [(", i);
2086 dump_affine(bmap, bmap->div[i]+1, out);
2088 isl_int_print(out, bmap->div[i][0], 0);
2089 fprintf(out, "]\n");
2093 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2094 FILE *out, int indent)
2097 fprintf(out, "null basic set\n");
2101 fprintf(out, "%*s", indent, "");
2102 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2103 bset->ref, bset->dim->nparam, bset->dim->n_out,
2104 bset->extra, bset->flags);
2105 dump((struct isl_basic_map *)bset, out, indent);
2108 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2109 FILE *out, int indent)
2112 fprintf(out, "null basic map\n");
2116 fprintf(out, "%*s", indent, "");
2117 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2118 "flags: %x, n_name: %d\n",
2120 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2121 bmap->extra, bmap->flags, bmap->dim->n_id);
2122 dump(bmap, out, indent);
2125 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2130 total = isl_basic_map_total_dim(bmap);
2131 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2132 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2133 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2134 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2138 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2141 struct isl_set *set;
2145 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2146 isl_assert(dim->ctx, n >= 0, goto error);
2147 set = isl_alloc(dim->ctx, struct isl_set,
2148 sizeof(struct isl_set) +
2149 (n - 1) * sizeof(struct isl_basic_set *));
2153 set->ctx = dim->ctx;
2154 isl_ctx_ref(set->ctx);
2162 isl_space_free(dim);
2166 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2167 unsigned nparam, unsigned dim, int n, unsigned flags)
2169 struct isl_set *set;
2172 dims = isl_space_alloc(ctx, nparam, 0, dim);
2176 set = isl_set_alloc_space(dims, n, flags);
2180 /* Make sure "map" has room for at least "n" more basic maps.
2182 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2185 struct isl_map *grown = NULL;
2189 isl_assert(map->ctx, n >= 0, goto error);
2190 if (map->n + n <= map->size)
2192 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2195 for (i = 0; i < map->n; ++i) {
2196 grown->p[i] = isl_basic_map_copy(map->p[i]);
2204 isl_map_free(grown);
2209 /* Make sure "set" has room for at least "n" more basic sets.
2211 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2213 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2216 struct isl_set *isl_set_dup(struct isl_set *set)
2219 struct isl_set *dup;
2224 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2227 for (i = 0; i < set->n; ++i)
2228 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2232 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2234 return isl_map_from_basic_map(bset);
2237 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2239 struct isl_map *map;
2244 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2245 return isl_map_add_basic_map(map, bmap);
2248 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2249 __isl_take isl_basic_set *bset)
2251 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2252 (struct isl_basic_map *)bset);
2255 void isl_set_free(struct isl_set *set)
2265 isl_ctx_deref(set->ctx);
2266 for (i = 0; i < set->n; ++i)
2267 isl_basic_set_free(set->p[i]);
2268 isl_space_free(set->dim);
2272 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2277 fprintf(out, "null set\n");
2281 fprintf(out, "%*s", indent, "");
2282 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2283 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2285 for (i = 0; i < set->n; ++i) {
2286 fprintf(out, "%*s", indent, "");
2287 fprintf(out, "basic set %d:\n", i);
2288 isl_basic_set_print_internal(set->p[i], out, indent+4);
2292 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2297 fprintf(out, "null map\n");
2301 fprintf(out, "%*s", indent, "");
2302 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2303 "flags: %x, n_name: %d\n",
2304 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2305 map->dim->n_out, map->flags, map->dim->n_id);
2306 for (i = 0; i < map->n; ++i) {
2307 fprintf(out, "%*s", indent, "");
2308 fprintf(out, "basic map %d:\n", i);
2309 isl_basic_map_print_internal(map->p[i], out, indent+4);
2313 struct isl_basic_map *isl_basic_map_intersect_domain(
2314 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2316 struct isl_basic_map *bmap_domain;
2321 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2322 bset->dim, isl_dim_param), goto error);
2324 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2325 isl_assert(bset->ctx,
2326 isl_basic_map_compatible_domain(bmap, bset), goto error);
2328 bmap = isl_basic_map_cow(bmap);
2331 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2332 bset->n_div, bset->n_eq, bset->n_ineq);
2333 bmap_domain = isl_basic_map_from_domain(bset);
2334 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2336 bmap = isl_basic_map_simplify(bmap);
2337 return isl_basic_map_finalize(bmap);
2339 isl_basic_map_free(bmap);
2340 isl_basic_set_free(bset);
2344 struct isl_basic_map *isl_basic_map_intersect_range(
2345 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2347 struct isl_basic_map *bmap_range;
2352 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2353 bset->dim, isl_dim_param), goto error);
2355 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2356 isl_assert(bset->ctx,
2357 isl_basic_map_compatible_range(bmap, bset), goto error);
2359 if (isl_basic_set_is_universe(bset)) {
2360 isl_basic_set_free(bset);
2364 bmap = isl_basic_map_cow(bmap);
2367 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2368 bset->n_div, bset->n_eq, bset->n_ineq);
2369 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2370 bmap = add_constraints(bmap, bmap_range, 0, 0);
2372 bmap = isl_basic_map_simplify(bmap);
2373 return isl_basic_map_finalize(bmap);
2375 isl_basic_map_free(bmap);
2376 isl_basic_set_free(bset);
2380 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2386 total = 1 + isl_basic_map_total_dim(bmap);
2387 if (total != vec->size)
2392 for (i = 0; i < bmap->n_eq; ++i) {
2393 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2394 if (!isl_int_is_zero(s)) {
2400 for (i = 0; i < bmap->n_ineq; ++i) {
2401 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2402 if (isl_int_is_neg(s)) {
2413 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2415 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2418 struct isl_basic_map *isl_basic_map_intersect(
2419 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2421 struct isl_vec *sample = NULL;
2423 if (!bmap1 || !bmap2)
2426 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2427 bmap2->dim, isl_dim_param), goto error);
2428 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2429 isl_space_dim(bmap1->dim, isl_dim_param) &&
2430 isl_space_dim(bmap2->dim, isl_dim_all) !=
2431 isl_space_dim(bmap2->dim, isl_dim_param))
2432 return isl_basic_map_intersect(bmap2, bmap1);
2434 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2435 isl_space_dim(bmap2->dim, isl_dim_param))
2436 isl_assert(bmap1->ctx,
2437 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2439 if (bmap1->sample &&
2440 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2441 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2442 sample = isl_vec_copy(bmap1->sample);
2443 else if (bmap2->sample &&
2444 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2445 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2446 sample = isl_vec_copy(bmap2->sample);
2448 bmap1 = isl_basic_map_cow(bmap1);
2451 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2452 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2453 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2456 isl_vec_free(sample);
2458 isl_vec_free(bmap1->sample);
2459 bmap1->sample = sample;
2462 bmap1 = isl_basic_map_simplify(bmap1);
2463 return isl_basic_map_finalize(bmap1);
2466 isl_vec_free(sample);
2467 isl_basic_map_free(bmap1);
2468 isl_basic_map_free(bmap2);
2472 struct isl_basic_set *isl_basic_set_intersect(
2473 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2475 return (struct isl_basic_set *)
2476 isl_basic_map_intersect(
2477 (struct isl_basic_map *)bset1,
2478 (struct isl_basic_map *)bset2);
2481 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2482 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2484 return isl_basic_set_intersect(bset1, bset2);
2487 /* Special case of isl_map_intersect, where both map1 and map2
2488 * are convex, without any divs and such that either map1 or map2
2489 * contains a single constraint. This constraint is then simply
2490 * added to the other map.
2492 static __isl_give isl_map *map_intersect_add_constraint(
2493 __isl_take isl_map *map1, __isl_take isl_map *map2)
2495 isl_assert(map1->ctx, map1->n == 1, goto error);
2496 isl_assert(map2->ctx, map1->n == 1, goto error);
2497 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2498 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2500 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2501 return isl_map_intersect(map2, map1);
2503 isl_assert(map2->ctx,
2504 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2506 map1 = isl_map_cow(map1);
2509 if (isl_map_plain_is_empty(map1)) {
2513 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2514 if (map2->p[0]->n_eq == 1)
2515 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2517 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2518 map2->p[0]->ineq[0]);
2520 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2521 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2525 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2526 isl_basic_map_free(map1->p[0]);
2539 /* map2 may be either a parameter domain or a map living in the same
2542 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2543 __isl_take isl_map *map2)
2546 struct isl_map *result;
2552 if (isl_map_plain_is_empty(map1) &&
2553 isl_space_is_equal(map1->dim, map2->dim)) {
2557 if (isl_map_plain_is_empty(map2) &&
2558 isl_space_is_equal(map1->dim, map2->dim)) {
2563 if (map1->n == 1 && map2->n == 1 &&
2564 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2565 isl_space_is_equal(map1->dim, map2->dim) &&
2566 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2567 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2568 return map_intersect_add_constraint(map1, map2);
2570 if (isl_space_dim(map2->dim, isl_dim_all) !=
2571 isl_space_dim(map2->dim, isl_dim_param))
2572 isl_assert(map1->ctx,
2573 isl_space_is_equal(map1->dim, map2->dim), goto error);
2575 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2576 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2577 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2579 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2580 map1->n * map2->n, flags);
2583 for (i = 0; i < map1->n; ++i)
2584 for (j = 0; j < map2->n; ++j) {
2585 struct isl_basic_map *part;
2586 part = isl_basic_map_intersect(
2587 isl_basic_map_copy(map1->p[i]),
2588 isl_basic_map_copy(map2->p[j]));
2589 if (isl_basic_map_is_empty(part))
2590 isl_basic_map_free(part);
2592 result = isl_map_add_basic_map(result, part);
2605 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2606 __isl_take isl_map *map2)
2610 if (!isl_space_is_equal(map1->dim, map2->dim))
2611 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2612 "spaces don't match", goto error);
2613 return map_intersect_internal(map1, map2);
2620 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2621 __isl_take isl_map *map2)
2623 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2626 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2628 return (struct isl_set *)
2629 isl_map_intersect((struct isl_map *)set1,
2630 (struct isl_map *)set2);
2633 /* map_intersect_internal accepts intersections
2634 * with parameter domains, so we can just call that function.
2636 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2637 __isl_take isl_set *params)
2639 return map_intersect_internal(map, params);
2642 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2643 __isl_take isl_map *map2)
2645 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2648 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2649 __isl_take isl_set *params)
2651 return isl_map_intersect_params(set, params);
2654 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2657 struct isl_basic_set *bset;
2662 bmap = isl_basic_map_cow(bmap);
2665 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2666 in = isl_basic_map_n_in(bmap);
2667 bset = isl_basic_set_from_basic_map(bmap);
2668 bset = isl_basic_set_swap_vars(bset, in);
2669 return isl_basic_map_from_basic_set(bset, dim);
2672 static __isl_give isl_basic_map *basic_map_space_reset(
2673 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2677 if (!isl_space_is_named_or_nested(bmap->dim, type))
2680 space = isl_basic_map_get_space(bmap);
2681 space = isl_space_reset(space, type);
2682 bmap = isl_basic_map_reset_space(bmap, space);
2686 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2687 enum isl_dim_type type, unsigned pos, unsigned n)
2690 struct isl_basic_map *res;
2691 struct isl_dim_map *dim_map;
2692 unsigned total, off;
2693 enum isl_dim_type t;
2696 return basic_map_space_reset(bmap, type);
2701 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2703 total = isl_basic_map_total_dim(bmap) + n;
2704 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2706 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2708 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2710 unsigned size = isl_basic_map_dim(bmap, t);
2711 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2713 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2714 pos, size - pos, off + pos + n);
2716 off += isl_space_dim(res_dim, t);
2718 isl_dim_map_div(dim_map, bmap, off);
2720 res = isl_basic_map_alloc_space(res_dim,
2721 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2722 if (isl_basic_map_is_rational(bmap))
2723 res = isl_basic_map_set_rational(res);
2724 if (isl_basic_map_plain_is_empty(bmap)) {
2725 isl_basic_map_free(bmap);
2726 return isl_basic_map_set_to_empty(res);
2728 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2729 return isl_basic_map_finalize(res);
2732 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2733 enum isl_dim_type type, unsigned n)
2737 return isl_basic_map_insert(bmap, type,
2738 isl_basic_map_dim(bmap, type), n);
2741 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2742 enum isl_dim_type type, unsigned n)
2746 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2747 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2749 isl_basic_set_free(bset);
2753 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2754 enum isl_dim_type type)
2758 if (!map || !isl_space_is_named_or_nested(map->dim, type))
2761 space = isl_map_get_space(map);
2762 space = isl_space_reset(space, type);
2763 map = isl_map_reset_space(map, space);
2767 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2768 enum isl_dim_type type, unsigned pos, unsigned n)
2773 return map_space_reset(map, type);
2775 map = isl_map_cow(map);
2779 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2783 for (i = 0; i < map->n; ++i) {
2784 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2795 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2796 enum isl_dim_type type, unsigned pos, unsigned n)
2798 return isl_map_insert_dims(set, type, pos, n);
2801 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2802 enum isl_dim_type type, unsigned n)
2806 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2809 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2810 enum isl_dim_type type, unsigned n)
2814 isl_assert(set->ctx, type != isl_dim_in, goto error);
2815 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2821 __isl_give isl_basic_map *isl_basic_map_move_dims(
2822 __isl_take isl_basic_map *bmap,
2823 enum isl_dim_type dst_type, unsigned dst_pos,
2824 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2826 struct isl_dim_map *dim_map;
2827 struct isl_basic_map *res;
2828 enum isl_dim_type t;
2829 unsigned total, off;
2836 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2839 if (dst_type == src_type && dst_pos == src_pos)
2842 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2844 if (pos(bmap->dim, dst_type) + dst_pos ==
2845 pos(bmap->dim, src_type) + src_pos +
2846 ((src_type < dst_type) ? n : 0)) {
2847 bmap = isl_basic_map_cow(bmap);
2851 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2852 src_type, src_pos, n);
2856 bmap = isl_basic_map_finalize(bmap);
2861 total = isl_basic_map_total_dim(bmap);
2862 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2865 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2866 unsigned size = isl_space_dim(bmap->dim, t);
2867 if (t == dst_type) {
2868 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2871 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2874 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2875 dst_pos, size - dst_pos, off);
2876 off += size - dst_pos;
2877 } else if (t == src_type) {
2878 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2881 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2882 src_pos + n, size - src_pos - n, off);
2883 off += size - src_pos - n;
2885 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2889 isl_dim_map_div(dim_map, bmap, off);
2891 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2892 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2893 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2895 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2896 src_type, src_pos, n);
2900 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2901 bmap = isl_basic_map_gauss(bmap, NULL);
2902 bmap = isl_basic_map_finalize(bmap);
2906 isl_basic_map_free(bmap);
2910 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2911 enum isl_dim_type dst_type, unsigned dst_pos,
2912 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2914 return (isl_basic_set *)isl_basic_map_move_dims(
2915 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2918 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2919 enum isl_dim_type dst_type, unsigned dst_pos,
2920 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2924 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2925 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2926 src_type, src_pos, n);
2932 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2933 enum isl_dim_type dst_type, unsigned dst_pos,
2934 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2943 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2946 if (dst_type == src_type && dst_pos == src_pos)
2949 isl_assert(map->ctx, dst_type != src_type, goto error);
2951 map = isl_map_cow(map);
2955 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2959 for (i = 0; i < map->n; ++i) {
2960 map->p[i] = isl_basic_map_move_dims(map->p[i],
2962 src_type, src_pos, n);
2973 /* Move the specified dimensions to the last columns right before
2974 * the divs. Don't change the dimension specification of bmap.
2975 * That's the responsibility of the caller.
2977 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2978 enum isl_dim_type type, unsigned first, unsigned n)
2980 struct isl_dim_map *dim_map;
2981 struct isl_basic_map *res;
2982 enum isl_dim_type t;
2983 unsigned total, off;
2987 if (pos(bmap->dim, type) + first + n ==
2988 1 + isl_space_dim(bmap->dim, isl_dim_all))
2991 total = isl_basic_map_total_dim(bmap);
2992 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2995 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2996 unsigned size = isl_space_dim(bmap->dim, t);
2998 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3001 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3002 first, n, total - bmap->n_div - n);
3003 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3004 first + n, size - (first + n), off);
3005 off += size - (first + n);
3007 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3011 isl_dim_map_div(dim_map, bmap, off + n);
3013 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3014 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3015 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3019 /* Turn the n dimensions of type type, starting at first
3020 * into existentially quantified variables.
3022 __isl_give isl_basic_map *isl_basic_map_project_out(
3023 __isl_take isl_basic_map *bmap,
3024 enum isl_dim_type type, unsigned first, unsigned n)
3032 return basic_map_space_reset(bmap, type);
3037 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3038 return isl_basic_map_remove_dims(bmap, type, first, n);
3040 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3043 bmap = move_last(bmap, type, first, n);
3044 bmap = isl_basic_map_cow(bmap);
3048 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3049 old = bmap->block2.data;
3050 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3051 (bmap->extra + n) * (1 + row_size));
3052 if (!bmap->block2.data)
3054 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3057 for (i = 0; i < n; ++i) {
3058 new_div[i] = bmap->block2.data +
3059 (bmap->extra + i) * (1 + row_size);
3060 isl_seq_clr(new_div[i], 1 + row_size);
3062 for (i = 0; i < bmap->extra; ++i)
3063 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3065 bmap->div = new_div;
3069 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3072 bmap = isl_basic_map_simplify(bmap);
3073 bmap = isl_basic_map_drop_redundant_divs(bmap);
3074 return isl_basic_map_finalize(bmap);
3076 isl_basic_map_free(bmap);
3080 /* Turn the n dimensions of type type, starting at first
3081 * into existentially quantified variables.
3083 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3084 enum isl_dim_type type, unsigned first, unsigned n)
3086 return (isl_basic_set *)isl_basic_map_project_out(
3087 (isl_basic_map *)bset, type, first, n);
3090 /* Turn the n dimensions of type type, starting at first
3091 * into existentially quantified variables.
3093 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3094 enum isl_dim_type type, unsigned first, unsigned n)
3102 return map_space_reset(map, type);
3104 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3106 map = isl_map_cow(map);
3110 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3114 for (i = 0; i < map->n; ++i) {
3115 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3126 /* Turn the n dimensions of type type, starting at first
3127 * into existentially quantified variables.
3129 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3130 enum isl_dim_type type, unsigned first, unsigned n)
3132 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3135 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3139 for (i = 0; i < n; ++i) {
3140 j = isl_basic_map_alloc_div(bmap);
3143 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3147 isl_basic_map_free(bmap);
3151 struct isl_basic_map *isl_basic_map_apply_range(
3152 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3154 isl_space *dim_result = NULL;
3155 struct isl_basic_map *bmap;
3156 unsigned n_in, n_out, n, nparam, total, pos;
3157 struct isl_dim_map *dim_map1, *dim_map2;
3159 if (!bmap1 || !bmap2)
3162 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3163 isl_space_copy(bmap2->dim));
3165 n_in = isl_basic_map_n_in(bmap1);
3166 n_out = isl_basic_map_n_out(bmap2);
3167 n = isl_basic_map_n_out(bmap1);
3168 nparam = isl_basic_map_n_param(bmap1);
3170 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3171 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3172 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3173 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3174 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3175 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3176 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3177 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3178 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3179 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3180 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3182 bmap = isl_basic_map_alloc_space(dim_result,
3183 bmap1->n_div + bmap2->n_div + n,
3184 bmap1->n_eq + bmap2->n_eq,
3185 bmap1->n_ineq + bmap2->n_ineq);
3186 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3187 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3188 bmap = add_divs(bmap, n);
3189 bmap = isl_basic_map_simplify(bmap);
3190 bmap = isl_basic_map_drop_redundant_divs(bmap);
3191 return isl_basic_map_finalize(bmap);
3193 isl_basic_map_free(bmap1);
3194 isl_basic_map_free(bmap2);
3198 struct isl_basic_set *isl_basic_set_apply(
3199 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3204 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3207 return (struct isl_basic_set *)
3208 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3210 isl_basic_set_free(bset);
3211 isl_basic_map_free(bmap);
3215 struct isl_basic_map *isl_basic_map_apply_domain(
3216 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3218 if (!bmap1 || !bmap2)
3221 isl_assert(bmap1->ctx,
3222 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3223 isl_assert(bmap1->ctx,
3224 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3227 bmap1 = isl_basic_map_reverse(bmap1);
3228 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3229 return isl_basic_map_reverse(bmap1);
3231 isl_basic_map_free(bmap1);
3232 isl_basic_map_free(bmap2);
3236 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3237 * A \cap B -> f(A) + f(B)
3239 struct isl_basic_map *isl_basic_map_sum(
3240 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3242 unsigned n_in, n_out, nparam, total, pos;
3243 struct isl_basic_map *bmap = NULL;
3244 struct isl_dim_map *dim_map1, *dim_map2;
3247 if (!bmap1 || !bmap2)
3250 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3253 nparam = isl_basic_map_n_param(bmap1);
3254 n_in = isl_basic_map_n_in(bmap1);
3255 n_out = isl_basic_map_n_out(bmap1);
3257 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3258 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3259 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3260 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3261 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3262 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3263 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3264 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3265 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3266 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3267 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3269 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3270 bmap1->n_div + bmap2->n_div + 2 * n_out,
3271 bmap1->n_eq + bmap2->n_eq + n_out,
3272 bmap1->n_ineq + bmap2->n_ineq);
3273 for (i = 0; i < n_out; ++i) {
3274 int j = isl_basic_map_alloc_equality(bmap);
3277 isl_seq_clr(bmap->eq[j], 1+total);
3278 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3279 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3280 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3282 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3283 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3284 bmap = add_divs(bmap, 2 * n_out);
3286 bmap = isl_basic_map_simplify(bmap);
3287 return isl_basic_map_finalize(bmap);
3289 isl_basic_map_free(bmap);
3290 isl_basic_map_free(bmap1);
3291 isl_basic_map_free(bmap2);
3295 /* Given two maps A -> f(A) and B -> g(B), construct a map
3296 * A \cap B -> f(A) + f(B)
3298 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3300 struct isl_map *result;
3306 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3308 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3309 map1->n * map2->n, 0);
3312 for (i = 0; i < map1->n; ++i)
3313 for (j = 0; j < map2->n; ++j) {
3314 struct isl_basic_map *part;
3315 part = isl_basic_map_sum(
3316 isl_basic_map_copy(map1->p[i]),
3317 isl_basic_map_copy(map2->p[j]));
3318 if (isl_basic_map_is_empty(part))
3319 isl_basic_map_free(part);
3321 result = isl_map_add_basic_map(result, part);
3334 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3335 __isl_take isl_set *set2)
3337 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3340 /* Given a basic map A -> f(A), construct A -> -f(A).
3342 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3347 bmap = isl_basic_map_cow(bmap);
3351 n = isl_basic_map_dim(bmap, isl_dim_out);
3352 off = isl_basic_map_offset(bmap, isl_dim_out);
3353 for (i = 0; i < bmap->n_eq; ++i)
3354 for (j = 0; j < n; ++j)
3355 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3356 for (i = 0; i < bmap->n_ineq; ++i)
3357 for (j = 0; j < n; ++j)
3358 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3359 for (i = 0; i < bmap->n_div; ++i)
3360 for (j = 0; j < n; ++j)
3361 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3362 bmap = isl_basic_map_gauss(bmap, NULL);
3363 return isl_basic_map_finalize(bmap);
3366 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3368 return isl_basic_map_neg(bset);
3371 /* Given a map A -> f(A), construct A -> -f(A).
3373 struct isl_map *isl_map_neg(struct isl_map *map)
3377 map = isl_map_cow(map);
3381 for (i = 0; i < map->n; ++i) {
3382 map->p[i] = isl_basic_map_neg(map->p[i]);
3393 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3395 return (isl_set *)isl_map_neg((isl_map *)set);
3398 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3399 * A -> floor(f(A)/d).
3401 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3404 unsigned n_in, n_out, nparam, total, pos;
3405 struct isl_basic_map *result = NULL;
3406 struct isl_dim_map *dim_map;
3412 nparam = isl_basic_map_n_param(bmap);
3413 n_in = isl_basic_map_n_in(bmap);
3414 n_out = isl_basic_map_n_out(bmap);
3416 total = nparam + n_in + n_out + bmap->n_div + n_out;
3417 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3418 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3419 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3420 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3421 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3423 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3424 bmap->n_div + n_out,
3425 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3426 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3427 result = add_divs(result, n_out);
3428 for (i = 0; i < n_out; ++i) {
3430 j = isl_basic_map_alloc_inequality(result);
3433 isl_seq_clr(result->ineq[j], 1+total);
3434 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3435 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3436 j = isl_basic_map_alloc_inequality(result);
3439 isl_seq_clr(result->ineq[j], 1+total);
3440 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3441 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3442 isl_int_sub_ui(result->ineq[j][0], d, 1);
3445 result = isl_basic_map_simplify(result);
3446 return isl_basic_map_finalize(result);
3448 isl_basic_map_free(result);
3452 /* Given a map A -> f(A) and an integer d, construct a map
3453 * A -> floor(f(A)/d).
3455 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3459 map = isl_map_cow(map);
3463 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3464 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3465 for (i = 0; i < map->n; ++i) {
3466 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3477 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3483 i = isl_basic_map_alloc_equality(bmap);
3486 nparam = isl_basic_map_n_param(bmap);
3487 n_in = isl_basic_map_n_in(bmap);
3488 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3489 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3490 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3491 return isl_basic_map_finalize(bmap);
3493 isl_basic_map_free(bmap);
3497 /* Add a constraints to "bmap" expressing i_pos < o_pos
3499 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3505 i = isl_basic_map_alloc_inequality(bmap);
3508 nparam = isl_basic_map_n_param(bmap);
3509 n_in = isl_basic_map_n_in(bmap);
3510 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3511 isl_int_set_si(bmap->ineq[i][0], -1);
3512 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3513 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3514 return isl_basic_map_finalize(bmap);
3516 isl_basic_map_free(bmap);
3520 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3522 static __isl_give isl_basic_map *var_less_or_equal(
3523 __isl_take isl_basic_map *bmap, unsigned pos)
3529 i = isl_basic_map_alloc_inequality(bmap);
3532 nparam = isl_basic_map_n_param(bmap);
3533 n_in = isl_basic_map_n_in(bmap);
3534 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3535 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3536 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3537 return isl_basic_map_finalize(bmap);
3539 isl_basic_map_free(bmap);
3543 /* Add a constraints to "bmap" expressing i_pos > o_pos
3545 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3551 i = isl_basic_map_alloc_inequality(bmap);
3554 nparam = isl_basic_map_n_param(bmap);
3555 n_in = isl_basic_map_n_in(bmap);
3556 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3557 isl_int_set_si(bmap->ineq[i][0], -1);
3558 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3559 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3560 return isl_basic_map_finalize(bmap);
3562 isl_basic_map_free(bmap);
3566 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3568 static __isl_give isl_basic_map *var_more_or_equal(
3569 __isl_take isl_basic_map *bmap, unsigned pos)
3575 i = isl_basic_map_alloc_inequality(bmap);
3578 nparam = isl_basic_map_n_param(bmap);
3579 n_in = isl_basic_map_n_in(bmap);
3580 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3581 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3582 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3583 return isl_basic_map_finalize(bmap);
3585 isl_basic_map_free(bmap);
3589 __isl_give isl_basic_map *isl_basic_map_equal(
3590 __isl_take isl_space *dim, unsigned n_equal)
3593 struct isl_basic_map *bmap;
3594 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3597 for (i = 0; i < n_equal && bmap; ++i)
3598 bmap = var_equal(bmap, i);
3599 return isl_basic_map_finalize(bmap);
3602 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3604 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3608 struct isl_basic_map *bmap;
3609 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3612 for (i = 0; i < pos && bmap; ++i)
3613 bmap = var_equal(bmap, i);
3615 bmap = var_less(bmap, pos);
3616 return isl_basic_map_finalize(bmap);
3619 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3621 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3622 __isl_take isl_space *dim, unsigned pos)
3625 isl_basic_map *bmap;
3627 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3628 for (i = 0; i < pos; ++i)
3629 bmap = var_equal(bmap, i);
3630 bmap = var_less_or_equal(bmap, pos);
3631 return isl_basic_map_finalize(bmap);
3634 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3636 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3640 struct isl_basic_map *bmap;
3641 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3644 for (i = 0; i < pos && bmap; ++i)
3645 bmap = var_equal(bmap, i);
3647 bmap = var_more(bmap, pos);
3648 return isl_basic_map_finalize(bmap);
3651 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3653 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3654 __isl_take isl_space *dim, unsigned pos)
3657 isl_basic_map *bmap;
3659 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3660 for (i = 0; i < pos; ++i)
3661 bmap = var_equal(bmap, i);
3662 bmap = var_more_or_equal(bmap, pos);
3663 return isl_basic_map_finalize(bmap);
3666 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3667 unsigned n, int equal)
3669 struct isl_map *map;
3672 if (n == 0 && equal)
3673 return isl_map_universe(dims);
3675 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3677 for (i = 0; i + 1 < n; ++i)
3678 map = isl_map_add_basic_map(map,
3679 isl_basic_map_less_at(isl_space_copy(dims), i));
3682 map = isl_map_add_basic_map(map,
3683 isl_basic_map_less_or_equal_at(dims, n - 1));
3685 map = isl_map_add_basic_map(map,
3686 isl_basic_map_less_at(dims, n - 1));
3688 isl_space_free(dims);
3693 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3697 return map_lex_lte_first(dims, dims->n_out, equal);
3700 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3702 return map_lex_lte_first(dim, n, 0);
3705 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3707 return map_lex_lte_first(dim, n, 1);
3710 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3712 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3715 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3717 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3720 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3721 unsigned n, int equal)
3723 struct isl_map *map;
3726 if (n == 0 && equal)
3727 return isl_map_universe(dims);
3729 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3731 for (i = 0; i + 1 < n; ++i)
3732 map = isl_map_add_basic_map(map,
3733 isl_basic_map_more_at(isl_space_copy(dims), i));
3736 map = isl_map_add_basic_map(map,
3737 isl_basic_map_more_or_equal_at(dims, n - 1));
3739 map = isl_map_add_basic_map(map,
3740 isl_basic_map_more_at(dims, n - 1));
3742 isl_space_free(dims);
3747 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3751 return map_lex_gte_first(dims, dims->n_out, equal);
3754 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3756 return map_lex_gte_first(dim, n, 0);
3759 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3761 return map_lex_gte_first(dim, n, 1);
3764 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3766 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3769 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3771 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3774 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3775 __isl_take isl_set *set2)
3778 map = isl_map_lex_le(isl_set_get_space(set1));
3779 map = isl_map_intersect_domain(map, set1);
3780 map = isl_map_intersect_range(map, set2);
3784 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3785 __isl_take isl_set *set2)
3788 map = isl_map_lex_lt(isl_set_get_space(set1));
3789 map = isl_map_intersect_domain(map, set1);
3790 map = isl_map_intersect_range(map, set2);
3794 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3795 __isl_take isl_set *set2)
3798 map = isl_map_lex_ge(isl_set_get_space(set1));
3799 map = isl_map_intersect_domain(map, set1);
3800 map = isl_map_intersect_range(map, set2);
3804 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3805 __isl_take isl_set *set2)
3808 map = isl_map_lex_gt(isl_set_get_space(set1));
3809 map = isl_map_intersect_domain(map, set1);
3810 map = isl_map_intersect_range(map, set2);
3814 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3815 __isl_take isl_map *map2)
3818 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3819 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3820 map = isl_map_apply_range(map, isl_map_reverse(map2));
3824 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3825 __isl_take isl_map *map2)
3828 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3829 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3830 map = isl_map_apply_range(map, isl_map_reverse(map2));
3834 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3835 __isl_take isl_map *map2)
3838 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3839 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3840 map = isl_map_apply_range(map, isl_map_reverse(map2));
3844 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3845 __isl_take isl_map *map2)
3848 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3849 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3850 map = isl_map_apply_range(map, isl_map_reverse(map2));
3854 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3855 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3857 struct isl_basic_map *bmap;
3859 bset = isl_basic_set_cow(bset);
3863 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3864 isl_space_free(bset->dim);
3865 bmap = (struct isl_basic_map *) bset;
3867 return isl_basic_map_finalize(bmap);
3869 isl_basic_set_free(bset);
3870 isl_space_free(dim);
3874 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3878 if (bmap->dim->n_in == 0)
3879 return (struct isl_basic_set *)bmap;
3880 bmap = isl_basic_map_cow(bmap);
3883 bmap->dim = isl_space_as_set_space(bmap->dim);
3886 bmap = isl_basic_map_finalize(bmap);
3887 return (struct isl_basic_set *)bmap;
3889 isl_basic_map_free(bmap);
3893 /* For a div d = floor(f/m), add the constraints
3896 * -(f-(n-1)) + m d >= 0
3898 * Note that the second constraint is the negation of
3902 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3903 unsigned pos, isl_int *div)
3906 unsigned total = isl_basic_map_total_dim(bmap);
3908 i = isl_basic_map_alloc_inequality(bmap);
3911 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3912 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3914 j = isl_basic_map_alloc_inequality(bmap);
3917 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3918 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3919 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3923 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3924 unsigned pos, isl_int *div)
3926 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3930 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3932 unsigned total = isl_basic_map_total_dim(bmap);
3933 unsigned div_pos = total - bmap->n_div + div;
3935 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3939 struct isl_basic_set *isl_basic_map_underlying_set(
3940 struct isl_basic_map *bmap)
3944 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3946 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3947 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3948 return (struct isl_basic_set *)bmap;
3949 bmap = isl_basic_map_cow(bmap);
3952 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3955 bmap->extra -= bmap->n_div;
3957 bmap = isl_basic_map_finalize(bmap);
3958 return (struct isl_basic_set *)bmap;
3963 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3964 __isl_take isl_basic_set *bset)
3966 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3969 struct isl_basic_map *isl_basic_map_overlying_set(
3970 struct isl_basic_set *bset, struct isl_basic_map *like)
3972 struct isl_basic_map *bmap;
3973 struct isl_ctx *ctx;
3980 isl_assert(ctx, bset->n_div == 0, goto error);
3981 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3982 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3984 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3985 isl_basic_map_free(like);
3986 return (struct isl_basic_map *)bset;
3988 bset = isl_basic_set_cow(bset);
3991 total = bset->dim->n_out + bset->extra;
3992 bmap = (struct isl_basic_map *)bset;
3993 isl_space_free(bmap->dim);
3994 bmap->dim = isl_space_copy(like->dim);
3997 bmap->n_div = like->n_div;
3998 bmap->extra += like->n_div;
4002 ltotal = total - bmap->extra + like->extra;
4005 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4006 bmap->extra * (1 + 1 + total));
4007 if (isl_blk_is_error(bmap->block2))
4009 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4013 for (i = 0; i < bmap->extra; ++i)
4014 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4015 for (i = 0; i < like->n_div; ++i) {
4016 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4017 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4019 bmap = isl_basic_map_extend_constraints(bmap,
4020 0, 2 * like->n_div);
4021 for (i = 0; i < like->n_div; ++i) {
4022 if (isl_int_is_zero(bmap->div[i][0]))
4024 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4028 isl_basic_map_free(like);
4029 bmap = isl_basic_map_simplify(bmap);
4030 bmap = isl_basic_map_finalize(bmap);
4033 isl_basic_map_free(like);
4034 isl_basic_set_free(bset);
4038 struct isl_basic_set *isl_basic_set_from_underlying_set(
4039 struct isl_basic_set *bset, struct isl_basic_set *like)
4041 return (struct isl_basic_set *)
4042 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4045 struct isl_set *isl_set_from_underlying_set(
4046 struct isl_set *set, struct isl_basic_set *like)
4052 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4054 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4055 isl_basic_set_free(like);
4058 set = isl_set_cow(set);
4061 for (i = 0; i < set->n; ++i) {
4062 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4063 isl_basic_set_copy(like));
4067 isl_space_free(set->dim);
4068 set->dim = isl_space_copy(like->dim);
4071 isl_basic_set_free(like);
4074 isl_basic_set_free(like);
4079 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4083 map = isl_map_cow(map);
4086 map->dim = isl_space_cow(map->dim);
4090 for (i = 1; i < map->n; ++i)
4091 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4093 for (i = 0; i < map->n; ++i) {
4094 map->p[i] = (struct isl_basic_map *)
4095 isl_basic_map_underlying_set(map->p[i]);
4100 map->dim = isl_space_underlying(map->dim, 0);
4102 isl_space_free(map->dim);
4103 map->dim = isl_space_copy(map->p[0]->dim);
4107 return (struct isl_set *)map;
4113 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4115 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4118 __isl_give isl_basic_map *isl_basic_map_reset_space(
4119 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4121 bmap = isl_basic_map_cow(bmap);
4125 isl_space_free(bmap->dim);
4128 bmap = isl_basic_map_finalize(bmap);
4132 isl_basic_map_free(bmap);
4133 isl_space_free(dim);
4137 __isl_give isl_basic_set *isl_basic_set_reset_space(
4138 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4140 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4144 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4145 __isl_take isl_space *dim)
4149 map = isl_map_cow(map);
4153 for (i = 0; i < map->n; ++i) {
4154 map->p[i] = isl_basic_map_reset_space(map->p[i],
4155 isl_space_copy(dim));
4159 isl_space_free(map->dim);
4165 isl_space_free(dim);
4169 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4170 __isl_take isl_space *dim)
4172 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4175 /* Compute the parameter domain of the given basic set.
4177 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4182 if (isl_basic_set_is_params(bset))
4185 n = isl_basic_set_dim(bset, isl_dim_set);
4186 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4187 space = isl_basic_set_get_space(bset);
4188 space = isl_space_params(space);
4189 bset = isl_basic_set_reset_space(bset, space);
4193 /* Compute the parameter domain of the given set.
4195 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4200 if (isl_set_is_params(set))
4203 n = isl_set_dim(set, isl_dim_set);
4204 set = isl_set_project_out(set, isl_dim_set, 0, n);
4205 space = isl_set_get_space(set);
4206 space = isl_space_params(space);
4207 set = isl_set_reset_space(set, space);
4211 /* Construct a zero-dimensional set with the given parameter domain.
4213 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4216 space = isl_set_get_space(set);
4217 space = isl_space_set_from_params(space);
4218 set = isl_set_reset_space(set, space);
4222 /* Compute the parameter domain of the given map.
4224 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4229 n = isl_map_dim(map, isl_dim_in);
4230 map = isl_map_project_out(map, isl_dim_in, 0, n);
4231 n = isl_map_dim(map, isl_dim_out);
4232 map = isl_map_project_out(map, isl_dim_out, 0, n);
4233 space = isl_map_get_space(map);
4234 space = isl_space_params(space);
4235 map = isl_map_reset_space(map, space);
4239 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4242 struct isl_basic_set *domain;
4248 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4250 n_in = isl_basic_map_n_in(bmap);
4251 n_out = isl_basic_map_n_out(bmap);
4252 domain = isl_basic_set_from_basic_map(bmap);
4253 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4255 domain = isl_basic_set_reset_space(domain, dim);
4260 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4264 return isl_space_may_be_set(bmap->dim);
4267 /* Is this basic map actually a set?
4268 * Users should never call this function. Outside of isl,
4269 * the type should indicate whether something is a set or a map.
4271 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4275 return isl_space_is_set(bmap->dim);
4278 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4282 if (isl_basic_map_is_set(bmap))
4284 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4287 __isl_give isl_basic_map *isl_basic_map_domain_map(
4288 __isl_take isl_basic_map *bmap)
4292 isl_basic_map *domain;
4293 int nparam, n_in, n_out;
4296 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4297 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4298 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4300 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4301 domain = isl_basic_map_universe(dim);
4303 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4304 bmap = isl_basic_map_apply_range(bmap, domain);
4305 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4307 total = isl_basic_map_total_dim(bmap);
4309 for (i = 0; i < n_in; ++i) {
4310 k = isl_basic_map_alloc_equality(bmap);
4313 isl_seq_clr(bmap->eq[k], 1 + total);
4314 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4315 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4318 bmap = isl_basic_map_gauss(bmap, NULL);
4319 return isl_basic_map_finalize(bmap);
4321 isl_basic_map_free(bmap);
4325 __isl_give isl_basic_map *isl_basic_map_range_map(
4326 __isl_take isl_basic_map *bmap)
4330 isl_basic_map *range;
4331 int nparam, n_in, n_out;
4334 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4335 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4336 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4338 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4339 range = isl_basic_map_universe(dim);
4341 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4342 bmap = isl_basic_map_apply_range(bmap, range);
4343 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4345 total = isl_basic_map_total_dim(bmap);
4347 for (i = 0; i < n_out; ++i) {
4348 k = isl_basic_map_alloc_equality(bmap);
4351 isl_seq_clr(bmap->eq[k], 1 + total);
4352 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4353 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4356 bmap = isl_basic_map_gauss(bmap, NULL);
4357 return isl_basic_map_finalize(bmap);
4359 isl_basic_map_free(bmap);
4363 int isl_map_may_be_set(__isl_keep isl_map *map)
4367 return isl_space_may_be_set(map->dim);
4370 /* Is this map actually a set?
4371 * Users should never call this function. Outside of isl,
4372 * the type should indicate whether something is a set or a map.
4374 int isl_map_is_set(__isl_keep isl_map *map)
4378 return isl_space_is_set(map->dim);
4381 struct isl_set *isl_map_range(struct isl_map *map)
4384 struct isl_set *set;
4388 if (isl_map_is_set(map))
4389 return (isl_set *)map;
4391 map = isl_map_cow(map);
4395 set = (struct isl_set *) map;
4396 set->dim = isl_space_range(set->dim);
4399 for (i = 0; i < map->n; ++i) {
4400 set->p[i] = isl_basic_map_range(map->p[i]);
4404 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4405 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4412 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4415 isl_space *domain_dim;
4417 map = isl_map_cow(map);
4421 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4422 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4423 map->dim = isl_space_join(map->dim, domain_dim);
4426 for (i = 0; i < map->n; ++i) {
4427 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4431 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4432 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4439 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4442 isl_space *range_dim;
4444 map = isl_map_cow(map);
4448 range_dim = isl_space_range(isl_map_get_space(map));
4449 range_dim = isl_space_from_range(range_dim);
4450 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4451 map->dim = isl_space_join(map->dim, range_dim);
4454 for (i = 0; i < map->n; ++i) {
4455 map->p[i] = isl_basic_map_range_map(map->p[i]);
4459 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4460 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4467 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4468 __isl_take isl_space *dim)
4471 struct isl_map *map = NULL;
4473 set = isl_set_cow(set);
4476 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4477 map = (struct isl_map *)set;
4478 for (i = 0; i < set->n; ++i) {
4479 map->p[i] = isl_basic_map_from_basic_set(
4480 set->p[i], isl_space_copy(dim));
4484 isl_space_free(map->dim);
4488 isl_space_free(dim);
4493 __isl_give isl_basic_map *isl_basic_map_from_domain(
4494 __isl_take isl_basic_set *bset)
4496 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4499 __isl_give isl_basic_map *isl_basic_map_from_range(
4500 __isl_take isl_basic_set *bset)
4503 space = isl_basic_set_get_space(bset);
4504 space = isl_space_from_range(space);
4505 bset = isl_basic_set_reset_space(bset, space);
4506 return (isl_basic_map *)bset;
4509 struct isl_map *isl_map_from_range(struct isl_set *set)
4512 space = isl_set_get_space(set);
4513 space = isl_space_from_range(space);
4514 set = isl_set_reset_space(set, space);
4515 return (struct isl_map *)set;
4518 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4520 return isl_map_reverse(isl_map_from_range(set));
4523 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4524 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4526 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4529 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4530 __isl_take isl_set *range)
4532 return isl_map_apply_range(isl_map_reverse(domain), range);
4535 struct isl_set *isl_set_from_map(struct isl_map *map)
4538 struct isl_set *set = NULL;
4542 map = isl_map_cow(map);
4545 map->dim = isl_space_as_set_space(map->dim);
4548 set = (struct isl_set *)map;
4549 for (i = 0; i < map->n; ++i) {
4550 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4560 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4563 struct isl_map *map;
4568 isl_die(dim->ctx, isl_error_internal,
4569 "negative number of basic maps", goto error);
4570 map = isl_alloc(dim->ctx, struct isl_map,
4571 sizeof(struct isl_map) +
4572 (n - 1) * sizeof(struct isl_basic_map *));
4576 map->ctx = dim->ctx;
4577 isl_ctx_ref(map->ctx);
4585 isl_space_free(dim);
4589 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4590 unsigned nparam, unsigned in, unsigned out, int n,
4593 struct isl_map *map;
4596 dims = isl_space_alloc(ctx, nparam, in, out);
4600 map = isl_map_alloc_space(dims, n, flags);
4604 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4606 struct isl_basic_map *bmap;
4607 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4608 bmap = isl_basic_map_set_to_empty(bmap);
4612 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4614 struct isl_basic_set *bset;
4615 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4616 bset = isl_basic_set_set_to_empty(bset);
4620 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4622 struct isl_basic_map *bmap;
4625 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4626 bmap = isl_basic_map_set_to_empty(bmap);
4630 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4632 struct isl_basic_map *bmap;
4635 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4636 bmap = isl_basic_map_set_to_empty(bmap);
4640 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4642 struct isl_basic_set *bset;
4645 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4646 bset = isl_basic_set_set_to_empty(bset);
4650 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4652 struct isl_basic_map *bmap;
4653 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4654 bmap = isl_basic_map_finalize(bmap);
4658 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4660 struct isl_basic_set *bset;
4661 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4662 bset = isl_basic_set_finalize(bset);
4666 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4669 unsigned total = isl_space_dim(dim, isl_dim_all);
4670 isl_basic_map *bmap;
4672 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4673 for (i = 0; i < total; ++i) {
4674 int k = isl_basic_map_alloc_inequality(bmap);
4677 isl_seq_clr(bmap->ineq[k], 1 + total);
4678 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4682 isl_basic_map_free(bmap);
4686 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4688 return isl_basic_map_nat_universe(dim);
4691 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4693 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4696 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4698 return isl_map_nat_universe(dim);
4701 __isl_give isl_basic_map *isl_basic_map_universe_like(
4702 __isl_keep isl_basic_map *model)
4706 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4709 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4713 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4716 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4717 __isl_keep isl_set *model)
4721 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4724 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4726 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4729 struct isl_map *isl_map_empty_like(struct isl_map *model)
4733 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4736 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4740 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4743 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4745 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4748 struct isl_set *isl_set_empty_like(struct isl_set *model)
4752 return isl_set_empty(isl_space_copy(model->dim));
4755 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4757 struct isl_map *map;
4760 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4761 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4765 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4767 struct isl_set *set;
4770 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4771 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4775 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4779 return isl_set_universe(isl_space_copy(model->dim));
4782 struct isl_map *isl_map_dup(struct isl_map *map)
4785 struct isl_map *dup;
4789 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4790 for (i = 0; i < map->n; ++i)
4791 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4795 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4796 __isl_take isl_basic_map *bmap)
4800 if (isl_basic_map_plain_is_empty(bmap)) {
4801 isl_basic_map_free(bmap);
4804 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4805 isl_assert(map->ctx, map->n < map->size, goto error);
4806 map->p[map->n] = bmap;
4808 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4814 isl_basic_map_free(bmap);
4818 void isl_map_free(struct isl_map *map)
4828 isl_ctx_deref(map->ctx);
4829 for (i = 0; i < map->n; ++i)
4830 isl_basic_map_free(map->p[i]);
4831 isl_space_free(map->dim);
4835 struct isl_map *isl_map_extend(struct isl_map *base,
4836 unsigned nparam, unsigned n_in, unsigned n_out)
4840 base = isl_map_cow(base);
4844 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4847 for (i = 0; i < base->n; ++i) {
4848 base->p[i] = isl_basic_map_extend_space(base->p[i],
4849 isl_space_copy(base->dim), 0, 0, 0);
4859 struct isl_set *isl_set_extend(struct isl_set *base,
4860 unsigned nparam, unsigned dim)
4862 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4866 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4867 struct isl_basic_map *bmap, unsigned pos, int value)
4871 bmap = isl_basic_map_cow(bmap);
4872 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4873 j = isl_basic_map_alloc_equality(bmap);
4876 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4877 isl_int_set_si(bmap->eq[j][pos], -1);
4878 isl_int_set_si(bmap->eq[j][0], value);
4879 bmap = isl_basic_map_simplify(bmap);
4880 return isl_basic_map_finalize(bmap);
4882 isl_basic_map_free(bmap);
4886 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4887 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4891 bmap = isl_basic_map_cow(bmap);
4892 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4893 j = isl_basic_map_alloc_equality(bmap);
4896 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4897 isl_int_set_si(bmap->eq[j][pos], -1);
4898 isl_int_set(bmap->eq[j][0], value);
4899 bmap = isl_basic_map_simplify(bmap);
4900 return isl_basic_map_finalize(bmap);
4902 isl_basic_map_free(bmap);
4906 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4907 enum isl_dim_type type, unsigned pos, int value)
4911 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4912 return isl_basic_map_fix_pos_si(bmap,
4913 isl_basic_map_offset(bmap, type) + pos, value);
4915 isl_basic_map_free(bmap);
4919 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4920 enum isl_dim_type type, unsigned pos, isl_int value)
4924 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4925 return isl_basic_map_fix_pos(bmap,
4926 isl_basic_map_offset(bmap, type) + pos, value);
4928 isl_basic_map_free(bmap);
4932 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4933 enum isl_dim_type type, unsigned pos, int value)
4935 return (struct isl_basic_set *)
4936 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4940 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4941 enum isl_dim_type type, unsigned pos, isl_int value)
4943 return (struct isl_basic_set *)
4944 isl_basic_map_fix((struct isl_basic_map *)bset,
4948 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4949 unsigned input, int value)
4951 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4954 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4955 unsigned dim, int value)
4957 return (struct isl_basic_set *)
4958 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4959 isl_dim_set, dim, value);
4962 static int remove_if_empty(__isl_keep isl_map *map, int i)
4964 int empty = isl_basic_map_plain_is_empty(map->p[i]);
4971 isl_basic_map_free(map->p[i]);
4972 if (i != map->n - 1) {
4973 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4974 map->p[i] = map->p[map->n - 1];
4981 struct isl_map *isl_map_fix_si(struct isl_map *map,
4982 enum isl_dim_type type, unsigned pos, int value)
4986 map = isl_map_cow(map);
4990 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4991 for (i = map->n - 1; i >= 0; --i) {
4992 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4993 if (remove_if_empty(map, i) < 0)
4996 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5003 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5004 enum isl_dim_type type, unsigned pos, int value)
5006 return (struct isl_set *)
5007 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5010 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5011 enum isl_dim_type type, unsigned pos, isl_int value)
5015 map = isl_map_cow(map);
5019 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5020 for (i = 0; i < map->n; ++i) {
5021 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5025 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5032 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5033 enum isl_dim_type type, unsigned pos, isl_int value)
5035 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5038 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5039 unsigned input, int value)
5041 return isl_map_fix_si(map, isl_dim_in, input, value);
5044 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5046 return (struct isl_set *)
5047 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5050 static __isl_give isl_basic_map *basic_map_bound_si(
5051 __isl_take isl_basic_map *bmap,
5052 enum isl_dim_type type, unsigned pos, int value, int upper)
5058 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5059 pos += isl_basic_map_offset(bmap, type);
5060 bmap = isl_basic_map_cow(bmap);
5061 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5062 j = isl_basic_map_alloc_inequality(bmap);
5065 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5067 isl_int_set_si(bmap->ineq[j][pos], -1);
5068 isl_int_set_si(bmap->ineq[j][0], value);
5070 isl_int_set_si(bmap->ineq[j][pos], 1);
5071 isl_int_set_si(bmap->ineq[j][0], -value);
5073 bmap = isl_basic_map_simplify(bmap);
5074 return isl_basic_map_finalize(bmap);
5076 isl_basic_map_free(bmap);
5080 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5081 __isl_take isl_basic_map *bmap,
5082 enum isl_dim_type type, unsigned pos, int value)
5084 return basic_map_bound_si(bmap, type, pos, value, 0);
5087 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5088 unsigned dim, isl_int value)
5092 bset = isl_basic_set_cow(bset);
5093 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5094 j = isl_basic_set_alloc_inequality(bset);
5097 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5098 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5099 isl_int_neg(bset->ineq[j][0], value);
5100 bset = isl_basic_set_simplify(bset);
5101 return isl_basic_set_finalize(bset);
5103 isl_basic_set_free(bset);
5107 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5108 enum isl_dim_type type, unsigned pos, int value, int upper)
5112 map = isl_map_cow(map);
5116 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5117 for (i = 0; i < map->n; ++i) {
5118 map->p[i] = basic_map_bound_si(map->p[i],
5119 type, pos, value, upper);
5123 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5130 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5131 enum isl_dim_type type, unsigned pos, int value)
5133 return map_bound_si(map, type, pos, value, 0);
5136 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5137 enum isl_dim_type type, unsigned pos, int value)
5139 return map_bound_si(map, type, pos, value, 1);
5142 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5143 enum isl_dim_type type, unsigned pos, int value)
5145 return (struct isl_set *)
5146 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5149 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5150 enum isl_dim_type type, unsigned pos, int value)
5152 return isl_map_upper_bound_si(set, type, pos, value);
5155 /* Bound the given variable of "bmap" from below (or above is "upper"
5156 * is set) to "value".
5158 static __isl_give isl_basic_map *basic_map_bound(
5159 __isl_take isl_basic_map *bmap,
5160 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5166 if (pos >= isl_basic_map_dim(bmap, type))
5167 isl_die(bmap->ctx, isl_error_invalid,
5168 "index out of bounds", goto error);
5169 pos += isl_basic_map_offset(bmap, type);
5170 bmap = isl_basic_map_cow(bmap);
5171 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5172 j = isl_basic_map_alloc_inequality(bmap);
5175 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5177 isl_int_set_si(bmap->ineq[j][pos], -1);
5178 isl_int_set(bmap->ineq[j][0], value);
5180 isl_int_set_si(bmap->ineq[j][pos], 1);
5181 isl_int_neg(bmap->ineq[j][0], value);
5183 bmap = isl_basic_map_simplify(bmap);
5184 return isl_basic_map_finalize(bmap);
5186 isl_basic_map_free(bmap);
5190 /* Bound the given variable of "map" from below (or above is "upper"
5191 * is set) to "value".
5193 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5194 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5198 map = isl_map_cow(map);
5202 if (pos >= isl_map_dim(map, type))
5203 isl_die(map->ctx, isl_error_invalid,
5204 "index out of bounds", goto error);
5205 for (i = map->n - 1; i >= 0; --i) {
5206 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5207 if (remove_if_empty(map, i) < 0)
5210 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5217 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5218 enum isl_dim_type type, unsigned pos, isl_int value)
5220 return map_bound(map, type, pos, value, 0);
5223 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5224 enum isl_dim_type type, unsigned pos, isl_int value)
5226 return map_bound(map, type, pos, value, 1);
5229 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5230 enum isl_dim_type type, unsigned pos, isl_int value)
5232 return isl_map_lower_bound(set, type, pos, value);
5235 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5236 enum isl_dim_type type, unsigned pos, isl_int value)
5238 return isl_map_upper_bound(set, type, pos, value);
5241 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5246 set = isl_set_cow(set);
5250 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5251 for (i = 0; i < set->n; ++i) {
5252 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5262 struct isl_map *isl_map_reverse(struct isl_map *map)
5266 map = isl_map_cow(map);
5270 map->dim = isl_space_reverse(map->dim);
5273 for (i = 0; i < map->n; ++i) {
5274 map->p[i] = isl_basic_map_reverse(map->p[i]);
5278 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5285 static struct isl_map *isl_basic_map_partial_lexopt(
5286 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5287 struct isl_set **empty, int max)
5291 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5292 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5294 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5296 isl_basic_map_free(bmap);
5297 isl_basic_set_free(dom);
5303 struct isl_map *isl_basic_map_partial_lexmax(
5304 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5305 struct isl_set **empty)
5307 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5310 struct isl_map *isl_basic_map_partial_lexmin(
5311 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5312 struct isl_set **empty)
5314 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5317 struct isl_set *isl_basic_set_partial_lexmin(
5318 struct isl_basic_set *bset, struct isl_basic_set *dom,
5319 struct isl_set **empty)
5321 return (struct isl_set *)
5322 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5326 struct isl_set *isl_basic_set_partial_lexmax(
5327 struct isl_basic_set *bset, struct isl_basic_set *dom,
5328 struct isl_set **empty)
5330 return (struct isl_set *)
5331 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5335 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5336 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5337 __isl_give isl_set **empty)
5339 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5342 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5343 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5344 __isl_give isl_set **empty)
5346 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5349 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5350 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5351 __isl_give isl_set **empty)
5353 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5356 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5357 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5358 __isl_give isl_set **empty)
5360 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5363 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5364 __isl_take isl_basic_map *bmap, int max)
5366 isl_basic_set *dom = NULL;
5367 isl_space *dom_space;
5371 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5372 dom = isl_basic_set_universe(dom_space);
5373 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5375 isl_basic_map_free(bmap);
5379 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5380 __isl_take isl_basic_map *bmap)
5382 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5385 /* Given a basic map "bmap", compute the lexicographically minimal
5386 * (or maximal) image element for each domain element in dom.
5387 * Set *empty to those elements in dom that do not have an image element.
5389 * We first make sure the basic sets in dom are disjoint and then
5390 * simply collect the results over each of the basic sets separately.
5391 * We could probably improve the efficiency a bit by moving the union
5392 * domain down into the parametric integer programming.
5394 static __isl_give isl_map *basic_map_partial_lexopt(
5395 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5396 __isl_give isl_set **empty, int max)
5399 struct isl_map *res;
5401 dom = isl_set_make_disjoint(dom);
5405 if (isl_set_plain_is_empty(dom)) {
5406 res = isl_map_empty_like_basic_map(bmap);
5407 *empty = isl_set_empty_like(dom);
5409 isl_basic_map_free(bmap);
5413 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5414 isl_basic_set_copy(dom->p[0]), empty, max);
5416 for (i = 1; i < dom->n; ++i) {
5417 struct isl_map *res_i;
5418 struct isl_set *empty_i;
5420 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5421 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5423 res = isl_map_union_disjoint(res, res_i);
5424 *empty = isl_set_union_disjoint(*empty, empty_i);
5428 isl_basic_map_free(bmap);
5433 isl_basic_map_free(bmap);
5437 /* Given a map "map", compute the lexicographically minimal
5438 * (or maximal) image element for each domain element in dom.
5439 * Set *empty to those elements in dom that do not have an image element.
5441 * We first compute the lexicographically minimal or maximal element
5442 * in the first basic map. This results in a partial solution "res"
5443 * and a subset "todo" of dom that still need to be handled.
5444 * We then consider each of the remaining maps in "map" and successively
5445 * improve both "res" and "todo".
5447 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5448 * Assume we are computing the lexicographical maximum.
5449 * We first compute the lexicographically maximal element in basic map i.
5450 * This results in a partial solution res_i and a subset todo_i.
5451 * Then we combine these results with those obtain for the first k basic maps
5452 * to obtain a result that is valid for the first k+1 basic maps.
5453 * In particular, the set where there is no solution is the set where
5454 * there is no solution for the first k basic maps and also no solution
5455 * for the ith basic map, i.e.,
5457 * todo^i = todo^k * todo_i
5459 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5460 * solutions, arbitrarily breaking ties in favor of res^k.
5461 * That is, when res^k(a) >= res_i(a), we pick res^k and
5462 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5463 * the lexicographic order.)
5464 * In practice, we compute
5466 * res^k * (res_i . "<=")
5470 * res_i * (res^k . "<")
5472 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5473 * where only one of res^k and res_i provides a solution and we simply pick
5480 * Note that we only compute these intersections when dom(res^k) intersects
5481 * dom(res_i). Otherwise, the only effect of these intersections is to
5482 * potentially break up res^k and res_i into smaller pieces.
5483 * We want to avoid such splintering as much as possible.
5484 * In fact, an earlier implementation of this function would look for
5485 * better results in the domain of res^k and for extra results in todo^k,
5486 * but this would always result in a splintering according to todo^k,
5487 * even when the domain of basic map i is disjoint from the domains of
5488 * the previous basic maps.
5490 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5491 __isl_take isl_map *map, __isl_take isl_set *dom,
5492 __isl_give isl_set **empty, int max)
5495 struct isl_map *res;
5496 struct isl_set *todo;
5501 if (isl_map_plain_is_empty(map)) {
5509 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5510 isl_set_copy(dom), &todo, max);
5512 for (i = 1; i < map->n; ++i) {
5516 isl_space *dim = isl_space_range(isl_map_get_space(res));
5518 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5519 isl_set_copy(dom), &todo_i, max);
5522 lt = isl_map_lex_lt(isl_space_copy(dim));
5523 le = isl_map_lex_le(dim);
5525 lt = isl_map_lex_gt(isl_space_copy(dim));
5526 le = isl_map_lex_ge(dim);
5528 lt = isl_map_apply_range(isl_map_copy(res), lt);
5529 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5530 le = isl_map_apply_range(isl_map_copy(res_i), le);
5531 le = isl_map_intersect(le, isl_map_copy(res));
5533 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5534 res = isl_map_intersect_domain(res,
5535 isl_set_copy(todo_i));
5536 res_i = isl_map_intersect_domain(res_i,
5537 isl_set_copy(todo));
5540 res = isl_map_union_disjoint(res, res_i);
5541 res = isl_map_union_disjoint(res, lt);
5542 res = isl_map_union_disjoint(res, le);
5544 todo = isl_set_intersect(todo, todo_i);
5564 /* Given a map "map", compute the lexicographically minimal
5565 * (or maximal) image element for each domain element in dom.
5566 * Set *empty to those elements in dom that do not have an image element.
5568 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5570 static __isl_give isl_map *isl_map_partial_lexopt(
5571 __isl_take isl_map *map, __isl_take isl_set *dom,
5572 __isl_give isl_set **empty, int max)
5576 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5577 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5578 if (!isl_space_has_named_params(map->dim) ||
5579 !isl_space_has_named_params(dom->dim))
5580 isl_die(map->ctx, isl_error_invalid,
5581 "unaligned unnamed parameters", goto error);
5582 map = isl_map_align_params(map, isl_map_get_space(dom));
5583 dom = isl_map_align_params(dom, isl_map_get_space(map));
5584 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5593 __isl_give isl_map *isl_map_partial_lexmax(
5594 __isl_take isl_map *map, __isl_take isl_set *dom,
5595 __isl_give isl_set **empty)
5597 return isl_map_partial_lexopt(map, dom, empty, 1);
5600 __isl_give isl_map *isl_map_partial_lexmin(
5601 __isl_take isl_map *map, __isl_take isl_set *dom,
5602 __isl_give isl_set **empty)
5604 return isl_map_partial_lexopt(map, dom, empty, 0);
5607 __isl_give isl_set *isl_set_partial_lexmin(
5608 __isl_take isl_set *set, __isl_take isl_set *dom,
5609 __isl_give isl_set **empty)
5611 return (struct isl_set *)
5612 isl_map_partial_lexmin((struct isl_map *)set,
5616 __isl_give isl_set *isl_set_partial_lexmax(
5617 __isl_take isl_set *set, __isl_take isl_set *dom,
5618 __isl_give isl_set **empty)
5620 return (struct isl_set *)
5621 isl_map_partial_lexmax((struct isl_map *)set,
5625 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5627 struct isl_basic_set *dom = NULL;
5632 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5633 dom = isl_basic_set_universe(dom_dim);
5634 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5636 isl_basic_map_free(bmap);
5640 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5642 return isl_basic_map_lexopt(bmap, 0);
5645 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5647 return isl_basic_map_lexopt(bmap, 1);
5650 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5652 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5655 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5657 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5660 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5662 struct isl_set *dom = NULL;
5667 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5668 dom = isl_set_universe(dom_dim);
5669 return isl_map_partial_lexopt(map, dom, NULL, max);
5675 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5677 return isl_map_lexopt(map, 0);
5680 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5682 return isl_map_lexopt(map, 1);
5685 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5687 return (isl_set *)isl_map_lexmin((isl_map *)set);
5690 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5692 return (isl_set *)isl_map_lexmax((isl_map *)set);
5695 /* Extract the first and only affine expression from list
5696 * and then add it to *pwaff with the given dom.
5697 * This domain is known to be disjoint from other domains
5698 * because of the way isl_basic_map_foreach_lexmax works.
5700 static int update_dim_opt(__isl_take isl_basic_set *dom,
5701 __isl_take isl_aff_list *list, void *user)
5703 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5705 isl_pw_aff **pwaff = user;
5706 isl_pw_aff *pwaff_i;
5708 if (isl_aff_list_n_aff(list) != 1)
5709 isl_die(ctx, isl_error_internal,
5710 "expecting single element list", goto error);
5712 aff = isl_aff_list_get_aff(list, 0);
5713 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5715 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5717 isl_aff_list_free(list);
5721 isl_basic_set_free(dom);
5722 isl_aff_list_free(list);
5726 /* Given a basic map with one output dimension, compute the minimum or
5727 * maximum of that dimension as an isl_pw_aff.
5729 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5730 * call update_dim_opt on each leaf of the result.
5732 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5735 isl_space *dim = isl_basic_map_get_space(bmap);
5739 dim = isl_space_from_domain(isl_space_domain(dim));
5740 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5741 pwaff = isl_pw_aff_empty(dim);
5743 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5745 return isl_pw_aff_free(pwaff);
5750 /* Compute the minimum or maximum of the given output dimension
5751 * as a function of the parameters and the input dimensions,
5752 * but independently of the other output dimensions.
5754 * We first project out the other output dimension and then compute
5755 * the "lexicographic" maximum in each basic map, combining the results
5756 * using isl_pw_aff_union_max.
5758 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5765 n_out = isl_map_dim(map, isl_dim_out);
5766 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5767 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5772 isl_space *dim = isl_map_get_space(map);
5773 dim = isl_space_domain(isl_space_from_range(dim));
5775 return isl_pw_aff_empty(dim);
5778 pwaff = basic_map_dim_opt(map->p[0], max);
5779 for (i = 1; i < map->n; ++i) {
5780 isl_pw_aff *pwaff_i;
5782 pwaff_i = basic_map_dim_opt(map->p[i], max);
5783 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5791 /* Compute the maximum of the given output dimension as a function of the
5792 * parameters and input dimensions, but independently of
5793 * the other output dimensions.
5795 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
5797 return map_dim_opt(map, pos, 1);
5800 /* Compute the minimum or maximum of the given set dimension
5801 * as a function of the parameters,
5802 * but independently of the other set dimensions.
5804 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5807 return map_dim_opt(set, pos, max);
5810 /* Compute the maximum of the given set dimension as a function of the
5811 * parameters, but independently of the other set dimensions.
5813 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5815 return set_dim_opt(set, pos, 1);
5818 /* Compute the minimum of the given set dimension as a function of the
5819 * parameters, but independently of the other set dimensions.
5821 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5823 return set_dim_opt(set, pos, 0);
5826 /* Apply a preimage specified by "mat" on the parameters of "bset".
5827 * bset is assumed to have only parameters and divs.
5829 static struct isl_basic_set *basic_set_parameter_preimage(
5830 struct isl_basic_set *bset, struct isl_mat *mat)
5837 bset->dim = isl_space_cow(bset->dim);
5841 nparam = isl_basic_set_dim(bset, isl_dim_param);
5843 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5845 bset->dim->nparam = 0;
5846 bset->dim->n_out = nparam;
5847 bset = isl_basic_set_preimage(bset, mat);
5849 bset->dim->nparam = bset->dim->n_out;
5850 bset->dim->n_out = 0;
5855 isl_basic_set_free(bset);
5859 /* Apply a preimage specified by "mat" on the parameters of "set".
5860 * set is assumed to have only parameters and divs.
5862 static struct isl_set *set_parameter_preimage(
5863 struct isl_set *set, struct isl_mat *mat)
5865 isl_space *dim = NULL;
5871 dim = isl_space_copy(set->dim);
5872 dim = isl_space_cow(dim);
5876 nparam = isl_set_dim(set, isl_dim_param);
5878 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5881 dim->n_out = nparam;
5882 isl_set_reset_space(set, dim);
5883 set = isl_set_preimage(set, mat);
5886 dim = isl_space_copy(set->dim);
5887 dim = isl_space_cow(dim);
5890 dim->nparam = dim->n_out;
5892 isl_set_reset_space(set, dim);
5895 isl_space_free(dim);
5902 /* Intersect the basic set "bset" with the affine space specified by the
5903 * equalities in "eq".
5905 static struct isl_basic_set *basic_set_append_equalities(
5906 struct isl_basic_set *bset, struct isl_mat *eq)
5914 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5919 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5920 for (i = 0; i < eq->n_row; ++i) {
5921 k = isl_basic_set_alloc_equality(bset);
5924 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5925 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5929 bset = isl_basic_set_gauss(bset, NULL);
5930 bset = isl_basic_set_finalize(bset);
5935 isl_basic_set_free(bset);
5939 /* Intersect the set "set" with the affine space specified by the
5940 * equalities in "eq".
5942 static struct isl_set *set_append_equalities(struct isl_set *set,
5950 for (i = 0; i < set->n; ++i) {
5951 set->p[i] = basic_set_append_equalities(set->p[i],
5964 /* Project the given basic set onto its parameter domain, possibly introducing
5965 * new, explicit, existential variables in the constraints.
5966 * The input has parameters and (possibly implicit) existential variables.
5967 * The output has the same parameters, but only
5968 * explicit existentially quantified variables.
5970 * The actual projection is performed by pip, but pip doesn't seem
5971 * to like equalities very much, so we first remove the equalities
5972 * among the parameters by performing a variable compression on
5973 * the parameters. Afterward, an inverse transformation is performed
5974 * and the equalities among the parameters are inserted back in.
5976 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5980 struct isl_mat *T, *T2;
5981 struct isl_set *set;
5982 unsigned nparam, n_div;
5984 bset = isl_basic_set_cow(bset);
5988 if (bset->n_eq == 0)
5989 return isl_basic_set_lexmin(bset);
5991 isl_basic_set_gauss(bset, NULL);
5993 nparam = isl_basic_set_dim(bset, isl_dim_param);
5994 n_div = isl_basic_set_dim(bset, isl_dim_div);
5996 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5997 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6000 if (i == bset->n_eq)
6001 return isl_basic_set_lexmin(bset);
6003 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6005 eq = isl_mat_cow(eq);
6006 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6007 if (T && T->n_col == 0) {
6011 bset = isl_basic_set_set_to_empty(bset);
6012 return isl_set_from_basic_set(bset);
6014 bset = basic_set_parameter_preimage(bset, T);
6016 set = isl_basic_set_lexmin(bset);
6017 set = set_parameter_preimage(set, T2);
6018 set = set_append_equalities(set, eq);
6022 /* Compute an explicit representation for all the existentially
6023 * quantified variables.
6024 * The input and output dimensions are first turned into parameters.
6025 * compute_divs then returns a map with the same parameters and
6026 * no input or output dimensions and the dimension specification
6027 * is reset to that of the input.
6029 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6031 struct isl_basic_set *bset;
6032 struct isl_set *set;
6033 struct isl_map *map;
6034 isl_space *dim, *orig_dim = NULL;
6039 bmap = isl_basic_map_cow(bmap);
6043 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6044 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6045 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6046 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
6050 orig_dim = bmap->dim;
6052 bset = (struct isl_basic_set *)bmap;
6054 set = parameter_compute_divs(bset);
6055 map = (struct isl_map *)set;
6056 map = isl_map_reset_space(map, orig_dim);
6060 isl_basic_map_free(bmap);
6064 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6072 off = isl_space_dim(bmap->dim, isl_dim_all);
6073 for (i = 0; i < bmap->n_div; ++i) {
6074 if (isl_int_is_zero(bmap->div[i][0]))
6076 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6082 static int map_divs_known(__isl_keep isl_map *map)
6089 for (i = 0; i < map->n; ++i) {
6090 int known = isl_basic_map_divs_known(map->p[i]);
6098 /* If bmap contains any unknown divs, then compute explicit
6099 * expressions for them. However, this computation may be
6100 * quite expensive, so first try to remove divs that aren't
6103 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6106 struct isl_map *map;
6108 known = isl_basic_map_divs_known(bmap);
6112 return isl_map_from_basic_map(bmap);
6114 bmap = isl_basic_map_drop_redundant_divs(bmap);
6116 known = isl_basic_map_divs_known(bmap);
6120 return isl_map_from_basic_map(bmap);
6122 map = compute_divs(bmap);
6125 isl_basic_map_free(bmap);
6129 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6133 struct isl_map *res;
6140 known = map_divs_known(map);
6148 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6149 for (i = 1 ; i < map->n; ++i) {
6151 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6152 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6153 res = isl_map_union_disjoint(res, r2);
6155 res = isl_map_union(res, r2);
6162 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6164 return (struct isl_set *)
6165 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6168 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6170 return (struct isl_set *)
6171 isl_map_compute_divs((struct isl_map *)set);
6174 struct isl_set *isl_map_domain(struct isl_map *map)
6177 struct isl_set *set;
6182 map = isl_map_cow(map);
6186 set = (struct isl_set *)map;
6187 set->dim = isl_space_domain(set->dim);
6190 for (i = 0; i < map->n; ++i) {
6191 set->p[i] = isl_basic_map_domain(map->p[i]);
6195 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6196 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6203 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6204 __isl_take isl_map *map2)
6208 struct isl_map *map = NULL;
6222 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6224 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6225 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6226 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6228 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6229 map1->n + map2->n, flags);
6232 for (i = 0; i < map1->n; ++i) {
6233 map = isl_map_add_basic_map(map,
6234 isl_basic_map_copy(map1->p[i]));
6238 for (i = 0; i < map2->n; ++i) {
6239 map = isl_map_add_basic_map(map,
6240 isl_basic_map_copy(map2->p[i]));
6254 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6255 __isl_take isl_map *map2)
6257 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6260 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6262 map1 = isl_map_union_disjoint(map1, map2);
6266 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6270 struct isl_set *isl_set_union_disjoint(
6271 struct isl_set *set1, struct isl_set *set2)
6273 return (struct isl_set *)
6274 isl_map_union_disjoint(
6275 (struct isl_map *)set1, (struct isl_map *)set2);
6278 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6280 return (struct isl_set *)
6281 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6284 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6285 __isl_take isl_set *set)
6288 struct isl_map *result;
6294 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6295 isl_die(set->ctx, isl_error_invalid,
6296 "parameters don't match", goto error);
6298 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6299 !isl_map_compatible_range(map, set))
6300 isl_die(set->ctx, isl_error_invalid,
6301 "incompatible spaces", goto error);
6303 if (isl_set_plain_is_universe(set)) {
6308 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6309 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6310 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6312 result = isl_map_alloc_space(isl_space_copy(map->dim),
6313 map->n * set->n, flags);
6316 for (i = 0; i < map->n; ++i)
6317 for (j = 0; j < set->n; ++j) {
6318 result = isl_map_add_basic_map(result,
6319 isl_basic_map_intersect_range(
6320 isl_basic_map_copy(map->p[i]),
6321 isl_basic_set_copy(set->p[j])));
6334 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6335 __isl_take isl_set *set)
6337 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6340 struct isl_map *isl_map_intersect_domain(
6341 struct isl_map *map, struct isl_set *set)
6343 return isl_map_reverse(
6344 isl_map_intersect_range(isl_map_reverse(map), set));
6347 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6348 __isl_take isl_map *map2)
6352 map1 = isl_map_reverse(map1);
6353 map1 = isl_map_apply_range(map1, map2);
6354 return isl_map_reverse(map1);
6361 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6362 __isl_take isl_map *map2)
6364 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6367 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6368 __isl_take isl_map *map2)
6370 isl_space *dim_result;
6371 struct isl_map *result;
6377 dim_result = isl_space_join(isl_space_copy(map1->dim),
6378 isl_space_copy(map2->dim));
6380 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6383 for (i = 0; i < map1->n; ++i)
6384 for (j = 0; j < map2->n; ++j) {
6385 result = isl_map_add_basic_map(result,
6386 isl_basic_map_apply_range(
6387 isl_basic_map_copy(map1->p[i]),
6388 isl_basic_map_copy(map2->p[j])));
6394 if (result && result->n <= 1)
6395 ISL_F_SET(result, ISL_MAP_DISJOINT);
6403 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6404 __isl_take isl_map *map2)
6406 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6410 * returns range - domain
6412 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6414 isl_space *dims, *target_dim;
6415 struct isl_basic_set *bset;
6422 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6423 bmap->dim, isl_dim_out),
6425 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6426 dim = isl_basic_map_n_in(bmap);
6427 nparam = isl_basic_map_n_param(bmap);
6428 bset = isl_basic_set_from_basic_map(bmap);
6429 bset = isl_basic_set_cow(bset);
6430 dims = isl_basic_set_get_space(bset);
6431 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6432 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6433 bset = isl_basic_set_swap_vars(bset, 2*dim);
6434 for (i = 0; i < dim; ++i) {
6435 int j = isl_basic_map_alloc_equality(
6436 (struct isl_basic_map *)bset);
6439 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6440 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6441 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6442 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6444 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6445 bset = isl_basic_set_reset_space(bset, target_dim);
6448 isl_basic_map_free(bmap);
6453 * returns range - domain
6455 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
6459 struct isl_set *result;
6464 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6465 map->dim, isl_dim_out),
6467 dim = isl_map_get_space(map);
6468 dim = isl_space_domain(dim);
6469 result = isl_set_alloc_space(dim, map->n, 0);
6472 for (i = 0; i < map->n; ++i)
6473 result = isl_set_add_basic_set(result,
6474 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6483 * returns [domain -> range] -> range - domain
6485 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6486 __isl_take isl_basic_map *bmap)
6490 isl_basic_map *domain;
6494 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6495 isl_die(bmap->ctx, isl_error_invalid,
6496 "domain and range don't match", goto error);
6498 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6499 n = isl_basic_map_dim(bmap, isl_dim_in);
6501 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6502 domain = isl_basic_map_universe(dim);
6504 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6505 bmap = isl_basic_map_apply_range(bmap, domain);
6506 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6508 total = isl_basic_map_total_dim(bmap);
6510 for (i = 0; i < n; ++i) {
6511 k = isl_basic_map_alloc_equality(bmap);
6514 isl_seq_clr(bmap->eq[k], 1 + total);
6515 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6516 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6517 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6520 bmap = isl_basic_map_gauss(bmap, NULL);
6521 return isl_basic_map_finalize(bmap);
6523 isl_basic_map_free(bmap);
6528 * returns [domain -> range] -> range - domain
6530 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6533 isl_space *domain_dim;
6538 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6539 isl_die(map->ctx, isl_error_invalid,
6540 "domain and range don't match", goto error);
6542 map = isl_map_cow(map);
6546 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6547 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6548 map->dim = isl_space_join(map->dim, domain_dim);
6551 for (i = 0; i < map->n; ++i) {
6552 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6556 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6563 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6565 struct isl_basic_map *bmap;
6573 nparam = dims->nparam;
6575 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6579 for (i = 0; i < dim; ++i) {
6580 int j = isl_basic_map_alloc_equality(bmap);
6583 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6584 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6585 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6587 return isl_basic_map_finalize(bmap);
6589 isl_basic_map_free(bmap);
6593 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6597 if (dim->n_in != dim->n_out)
6598 isl_die(dim->ctx, isl_error_invalid,
6599 "number of input and output dimensions needs to be "
6600 "the same", goto error);
6601 return basic_map_identity(dim);
6603 isl_space_free(dim);
6607 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6609 if (!model || !model->dim)
6611 return isl_basic_map_identity(isl_space_copy(model->dim));
6614 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6616 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6619 struct isl_map *isl_map_identity_like(struct isl_map *model)
6621 if (!model || !model->dim)
6623 return isl_map_identity(isl_space_copy(model->dim));
6626 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6628 if (!model || !model->dim)
6630 return isl_map_identity(isl_space_copy(model->dim));
6633 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6635 isl_space *dim = isl_set_get_space(set);
6637 id = isl_map_identity(isl_space_map_from_set(dim));
6638 return isl_map_intersect_range(id, set);
6641 /* Construct a basic set with all set dimensions having only non-negative
6644 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6645 __isl_take isl_space *space)
6650 struct isl_basic_set *bset;
6654 nparam = space->nparam;
6656 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6659 for (i = 0; i < dim; ++i) {
6660 int k = isl_basic_set_alloc_inequality(bset);
6663 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6664 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6668 isl_basic_set_free(bset);
6672 /* Construct the half-space x_pos >= 0.
6674 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6678 isl_basic_set *nonneg;
6680 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6681 k = isl_basic_set_alloc_inequality(nonneg);
6684 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6685 isl_int_set_si(nonneg->ineq[k][pos], 1);
6687 return isl_basic_set_finalize(nonneg);
6689 isl_basic_set_free(nonneg);
6693 /* Construct the half-space x_pos <= -1.
6695 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6700 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6701 k = isl_basic_set_alloc_inequality(neg);
6704 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6705 isl_int_set_si(neg->ineq[k][0], -1);
6706 isl_int_set_si(neg->ineq[k][pos], -1);
6708 return isl_basic_set_finalize(neg);
6710 isl_basic_set_free(neg);
6714 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6715 enum isl_dim_type type, unsigned first, unsigned n)
6718 isl_basic_set *nonneg;
6726 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6728 for (i = 0; i < n; ++i) {
6729 nonneg = nonneg_halfspace(isl_set_get_space(set),
6730 pos(set->dim, type) + first + i);
6731 neg = neg_halfspace(isl_set_get_space(set),
6732 pos(set->dim, type) + first + i);
6734 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6743 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6744 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6751 if (isl_set_plain_is_empty(set)) {
6756 return fn(set, signs, user);
6759 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6761 half = isl_set_intersect(half, isl_set_copy(set));
6762 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6766 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6768 half = isl_set_intersect(half, set);
6769 return foreach_orthant(half, signs, first + 1, len, fn, user);
6775 /* Call "fn" on the intersections of "set" with each of the orthants
6776 * (except for obviously empty intersections). The orthant is identified
6777 * by the signs array, with each entry having value 1 or -1 according
6778 * to the sign of the corresponding variable.
6780 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6781 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6791 if (isl_set_plain_is_empty(set))
6794 nparam = isl_set_dim(set, isl_dim_param);
6795 nvar = isl_set_dim(set, isl_dim_set);
6797 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6799 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6807 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6809 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6812 int isl_basic_map_is_subset(
6813 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6816 struct isl_map *map1;
6817 struct isl_map *map2;
6819 if (!bmap1 || !bmap2)
6822 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6823 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6825 is_subset = isl_map_is_subset(map1, map2);
6833 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
6834 __isl_keep isl_basic_set *bset2)
6836 return isl_basic_map_is_subset(bset1, bset2);
6839 int isl_basic_map_is_equal(
6840 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6844 if (!bmap1 || !bmap2)
6846 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6849 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6853 int isl_basic_set_is_equal(
6854 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6856 return isl_basic_map_is_equal(
6857 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6860 int isl_map_is_empty(struct isl_map *map)
6867 for (i = 0; i < map->n; ++i) {
6868 is_empty = isl_basic_map_is_empty(map->p[i]);
6877 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6879 return map ? map->n == 0 : -1;
6882 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6884 return isl_map_plain_is_empty(map);
6887 int isl_set_plain_is_empty(struct isl_set *set)
6889 return set ? set->n == 0 : -1;
6892 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6894 return isl_set_plain_is_empty(set);
6897 int isl_set_is_empty(struct isl_set *set)
6899 return isl_map_is_empty((struct isl_map *)set);
6902 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6907 return isl_space_is_equal(map1->dim, map2->dim);
6910 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6915 return isl_space_is_equal(set1->dim, set2->dim);
6918 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6924 is_subset = isl_map_is_subset(map1, map2);
6927 is_subset = isl_map_is_subset(map2, map1);
6931 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6933 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
6936 int isl_basic_map_is_strict_subset(
6937 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6941 if (!bmap1 || !bmap2)
6943 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6946 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6947 if (is_subset == -1)
6952 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6958 is_subset = isl_map_is_subset(map1, map2);
6961 is_subset = isl_map_is_subset(map2, map1);
6962 if (is_subset == -1)
6967 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6969 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6972 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6976 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6979 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6983 return bset->n_eq == 0 && bset->n_ineq == 0;
6986 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6993 for (i = 0; i < map->n; ++i) {
6994 int r = isl_basic_map_is_universe(map->p[i]);
7002 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7004 return isl_map_plain_is_universe((isl_map *) set);
7007 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7009 return isl_set_plain_is_universe(set);
7012 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7014 struct isl_basic_set *bset = NULL;
7015 struct isl_vec *sample = NULL;
7022 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7025 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7026 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7027 copy = isl_basic_map_remove_redundancies(copy);
7028 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
7029 isl_basic_map_free(copy);
7033 total = 1 + isl_basic_map_total_dim(bmap);
7034 if (bmap->sample && bmap->sample->size == total) {
7035 int contains = isl_basic_map_contains(bmap, bmap->sample);
7041 isl_vec_free(bmap->sample);
7042 bmap->sample = NULL;
7043 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7046 sample = isl_basic_set_sample_vec(bset);
7049 empty = sample->size == 0;
7050 isl_vec_free(bmap->sample);
7051 bmap->sample = sample;
7053 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7058 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7062 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7065 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7067 return isl_basic_map_plain_is_empty(bmap);
7070 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7074 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7077 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7079 return isl_basic_set_plain_is_empty(bset);
7082 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7084 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7087 struct isl_map *isl_basic_map_union(
7088 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7090 struct isl_map *map;
7091 if (!bmap1 || !bmap2)
7094 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7096 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7099 map = isl_map_add_basic_map(map, bmap1);
7100 map = isl_map_add_basic_map(map, bmap2);
7103 isl_basic_map_free(bmap1);
7104 isl_basic_map_free(bmap2);
7108 struct isl_set *isl_basic_set_union(
7109 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7111 return (struct isl_set *)isl_basic_map_union(
7112 (struct isl_basic_map *)bset1,
7113 (struct isl_basic_map *)bset2);
7116 /* Order divs such that any div only depends on previous divs */
7117 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7125 off = isl_space_dim(bmap->dim, isl_dim_all);
7127 for (i = 0; i < bmap->n_div; ++i) {
7129 if (isl_int_is_zero(bmap->div[i][0]))
7131 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7135 isl_basic_map_swap_div(bmap, i, i + pos);
7141 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7143 return (struct isl_basic_set *)
7144 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7147 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7154 for (i = 0; i < map->n; ++i) {
7155 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7166 /* Apply the expansion computed by isl_merge_divs.
7167 * The expansion itself is given by "exp" while the resulting
7168 * list of divs is given by "div".
7170 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7171 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7176 bset = isl_basic_set_cow(bset);
7180 if (div->n_row < bset->n_div)
7181 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7182 "not an expansion", goto error);
7184 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7185 div->n_row - bset->n_div, 0,
7186 2 * (div->n_row - bset->n_div));
7188 n_div = bset->n_div;
7189 for (i = n_div; i < div->n_row; ++i)
7190 if (isl_basic_set_alloc_div(bset) < 0)
7194 for (i = div->n_row - 1; i >= 0; --i) {
7195 if (j >= 0 && exp[j] == i) {
7197 isl_basic_map_swap_div(bset, i, j);
7200 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7201 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7209 isl_basic_set_free(bset);
7214 /* Look for a div in dst that corresponds to the div "div" in src.
7215 * The divs before "div" in src and dst are assumed to be the same.
7217 * Returns -1 if no corresponding div was found and the position
7218 * of the corresponding div in dst otherwise.
7220 static int find_div(struct isl_basic_map *dst,
7221 struct isl_basic_map *src, unsigned div)
7225 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7227 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7228 for (i = div; i < dst->n_div; ++i)
7229 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7230 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7231 dst->n_div - div) == -1)
7236 struct isl_basic_map *isl_basic_map_align_divs(
7237 struct isl_basic_map *dst, struct isl_basic_map *src)
7240 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7245 if (src->n_div == 0)
7248 for (i = 0; i < src->n_div; ++i)
7249 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7251 src = isl_basic_map_order_divs(src);
7252 dst = isl_basic_map_cow(dst);
7253 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7254 src->n_div, 0, 2 * src->n_div);
7257 for (i = 0; i < src->n_div; ++i) {
7258 int j = find_div(dst, src, i);
7260 j = isl_basic_map_alloc_div(dst);
7263 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7264 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7265 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7269 isl_basic_map_swap_div(dst, i, j);
7273 isl_basic_map_free(dst);
7277 struct isl_basic_set *isl_basic_set_align_divs(
7278 struct isl_basic_set *dst, struct isl_basic_set *src)
7280 return (struct isl_basic_set *)isl_basic_map_align_divs(
7281 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7284 struct isl_map *isl_map_align_divs(struct isl_map *map)
7292 map = isl_map_compute_divs(map);
7293 map = isl_map_cow(map);
7297 for (i = 1; i < map->n; ++i)
7298 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7299 for (i = 1; i < map->n; ++i)
7300 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7302 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7306 struct isl_set *isl_set_align_divs(struct isl_set *set)
7308 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7311 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7312 __isl_take isl_map *map)
7316 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7317 map = isl_map_intersect_domain(map, set);
7318 set = isl_map_range(map);
7326 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7327 __isl_take isl_map *map)
7329 return isl_map_align_params_map_map_and(set, map, &set_apply);
7332 /* There is no need to cow as removing empty parts doesn't change
7333 * the meaning of the set.
7335 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7342 for (i = map->n - 1; i >= 0; --i)
7343 remove_if_empty(map, i);
7348 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7350 return (struct isl_set *)
7351 isl_map_remove_empty_parts((struct isl_map *)set);
7354 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7356 struct isl_basic_map *bmap;
7357 if (!map || map->n == 0)
7359 bmap = map->p[map->n-1];
7360 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7361 return isl_basic_map_copy(bmap);
7364 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7366 return (struct isl_basic_set *)
7367 isl_map_copy_basic_map((struct isl_map *)set);
7370 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7371 __isl_keep isl_basic_map *bmap)
7377 for (i = map->n-1; i >= 0; --i) {
7378 if (map->p[i] != bmap)
7380 map = isl_map_cow(map);
7383 isl_basic_map_free(map->p[i]);
7384 if (i != map->n-1) {
7385 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7386 map->p[i] = map->p[map->n-1];
7397 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7398 struct isl_basic_set *bset)
7400 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7401 (struct isl_basic_map *)bset);
7404 /* Given two basic sets bset1 and bset2, compute the maximal difference
7405 * between the values of dimension pos in bset1 and those in bset2
7406 * for any common value of the parameters and dimensions preceding pos.
7408 static enum isl_lp_result basic_set_maximal_difference_at(
7409 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7410 int pos, isl_int *opt)
7413 struct isl_basic_map *bmap1 = NULL;
7414 struct isl_basic_map *bmap2 = NULL;
7415 struct isl_ctx *ctx;
7416 struct isl_vec *obj;
7419 unsigned dim1, dim2;
7420 enum isl_lp_result res;
7422 if (!bset1 || !bset2)
7423 return isl_lp_error;
7425 nparam = isl_basic_set_n_param(bset1);
7426 dim1 = isl_basic_set_n_dim(bset1);
7427 dim2 = isl_basic_set_n_dim(bset2);
7428 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7429 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7430 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7431 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7432 if (!bmap1 || !bmap2)
7434 bmap1 = isl_basic_map_cow(bmap1);
7435 bmap1 = isl_basic_map_extend(bmap1, nparam,
7436 pos, (dim1 - pos) + (dim2 - pos),
7437 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7438 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7441 total = isl_basic_map_total_dim(bmap1);
7443 obj = isl_vec_alloc(ctx, 1 + total);
7444 isl_seq_clr(obj->block.data, 1 + total);
7445 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7446 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7449 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7451 isl_basic_map_free(bmap1);
7455 isl_basic_map_free(bmap1);
7456 isl_basic_map_free(bmap2);
7457 return isl_lp_error;
7460 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7461 * for any common value of the parameters and dimensions preceding pos
7462 * in both basic sets, the values of dimension pos in bset1 are
7463 * smaller or larger than those in bset2.
7466 * 1 if bset1 follows bset2
7467 * -1 if bset1 precedes bset2
7468 * 0 if bset1 and bset2 are incomparable
7469 * -2 if some error occurred.
7471 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7472 struct isl_basic_set *bset2, int pos)
7475 enum isl_lp_result res;
7480 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7482 if (res == isl_lp_empty)
7484 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7485 res == isl_lp_unbounded)
7487 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7496 /* Given two basic sets bset1 and bset2, check whether
7497 * for any common value of the parameters and dimensions preceding pos
7498 * there is a value of dimension pos in bset1 that is larger
7499 * than a value of the same dimension in bset2.
7502 * 1 if there exists such a pair
7503 * 0 if there is no such pair, but there is a pair of equal values
7505 * -2 if some error occurred.
7507 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7508 __isl_keep isl_basic_set *bset2, int pos)
7511 enum isl_lp_result res;
7516 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7518 if (res == isl_lp_empty)
7520 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7521 res == isl_lp_unbounded)
7523 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7525 else if (res == isl_lp_ok)
7534 /* Given two sets set1 and set2, check whether
7535 * for any common value of the parameters and dimensions preceding pos
7536 * there is a value of dimension pos in set1 that is larger
7537 * than a value of the same dimension in set2.
7540 * 1 if there exists such a pair
7541 * 0 if there is no such pair, but there is a pair of equal values
7543 * -2 if some error occurred.
7545 int isl_set_follows_at(__isl_keep isl_set *set1,
7546 __isl_keep isl_set *set2, int pos)
7554 for (i = 0; i < set1->n; ++i)
7555 for (j = 0; j < set2->n; ++j) {
7557 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7558 if (f == 1 || f == -2)
7567 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7568 unsigned pos, isl_int *val)
7576 total = isl_basic_map_total_dim(bmap);
7577 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7578 for (; d+1 > pos; --d)
7579 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7583 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7585 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7587 if (!isl_int_is_one(bmap->eq[i][1+d]))
7590 isl_int_neg(*val, bmap->eq[i][0]);
7596 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7597 unsigned pos, isl_int *val)
7609 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7612 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7613 for (i = 1; fixed == 1 && i < map->n; ++i) {
7614 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7615 if (fixed == 1 && isl_int_ne(tmp, v))
7619 isl_int_set(*val, v);
7625 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7626 unsigned pos, isl_int *val)
7628 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7632 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7635 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7638 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7639 enum isl_dim_type type, unsigned pos, isl_int *val)
7641 if (pos >= isl_basic_map_dim(bmap, type))
7643 return isl_basic_map_plain_has_fixed_var(bmap,
7644 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7647 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7648 enum isl_dim_type type, unsigned pos, isl_int *val)
7650 if (pos >= isl_map_dim(map, type))
7652 return isl_map_plain_has_fixed_var(map,
7653 map_offset(map, type) - 1 + pos, val);
7656 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7657 enum isl_dim_type type, unsigned pos, isl_int *val)
7659 return isl_map_plain_is_fixed(set, type, pos, val);
7662 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7663 enum isl_dim_type type, unsigned pos, isl_int *val)
7665 return isl_map_plain_is_fixed(map, type, pos, val);
7668 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7669 * then return this fixed value in *val.
7671 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7672 unsigned dim, isl_int *val)
7674 return isl_basic_set_plain_has_fixed_var(bset,
7675 isl_basic_set_n_param(bset) + dim, val);
7678 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7679 * then return this fixed value in *val.
7681 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7682 unsigned dim, isl_int *val)
7684 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7687 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7688 unsigned dim, isl_int *val)
7690 return isl_set_plain_dim_is_fixed(set, dim, val);
7693 /* Check if input variable in has fixed value and if so and if val is not NULL,
7694 * then return this fixed value in *val.
7696 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7697 unsigned in, isl_int *val)
7699 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7702 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7703 * and if val is not NULL, then return this lower bound in *val.
7705 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7706 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7708 int i, i_eq = -1, i_ineq = -1;
7715 total = isl_basic_set_total_dim(bset);
7716 nparam = isl_basic_set_n_param(bset);
7717 for (i = 0; i < bset->n_eq; ++i) {
7718 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7724 for (i = 0; i < bset->n_ineq; ++i) {
7725 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7727 if (i_eq != -1 || i_ineq != -1)
7731 if (i_eq == -1 && i_ineq == -1)
7733 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7734 /* The coefficient should always be one due to normalization. */
7735 if (!isl_int_is_one(c[1+nparam+dim]))
7737 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7739 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7740 total - nparam - dim - 1) != -1)
7743 isl_int_neg(*val, c[0]);
7747 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7748 unsigned dim, isl_int *val)
7760 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7764 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7766 for (i = 1; fixed == 1 && i < set->n; ++i) {
7767 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7769 if (fixed == 1 && isl_int_ne(tmp, v))
7773 isl_int_set(*val, v);
7784 /* uset_gist depends on constraints without existentially quantified
7785 * variables sorting first.
7787 static int qsort_constraint_cmp(const void *p1, const void *p2)
7789 const struct constraint *c1 = (const struct constraint *)p1;
7790 const struct constraint *c2 = (const struct constraint *)p2;
7792 unsigned size = isl_min(c1->size, c2->size);
7794 l1 = isl_seq_last_non_zero(c1->c, size);
7795 l2 = isl_seq_last_non_zero(c2->c, size);
7800 return isl_seq_cmp(c1->c, c2->c, size);
7803 static struct isl_basic_map *isl_basic_map_sort_constraints(
7804 struct isl_basic_map *bmap)
7807 struct constraint *c;
7812 total = isl_basic_map_total_dim(bmap);
7813 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7816 for (i = 0; i < bmap->n_ineq; ++i) {
7818 c[i].c = bmap->ineq[i];
7820 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7821 for (i = 0; i < bmap->n_ineq; ++i)
7822 bmap->ineq[i] = c[i].c;
7826 isl_basic_map_free(bmap);
7830 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7831 __isl_take isl_basic_set *bset)
7833 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7834 (struct isl_basic_map *)bset);
7837 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7841 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7843 bmap = isl_basic_map_remove_redundancies(bmap);
7844 bmap = isl_basic_map_sort_constraints(bmap);
7845 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7849 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7851 return (struct isl_basic_set *)isl_basic_map_normalize(
7852 (struct isl_basic_map *)bset);
7855 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7856 const __isl_keep isl_basic_map *bmap2)
7863 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7864 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7865 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7866 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7867 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7868 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7869 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7870 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7871 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7872 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7873 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7875 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7877 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7879 if (bmap1->n_eq != bmap2->n_eq)
7880 return bmap1->n_eq - bmap2->n_eq;
7881 if (bmap1->n_ineq != bmap2->n_ineq)
7882 return bmap1->n_ineq - bmap2->n_ineq;
7883 if (bmap1->n_div != bmap2->n_div)
7884 return bmap1->n_div - bmap2->n_div;
7885 total = isl_basic_map_total_dim(bmap1);
7886 for (i = 0; i < bmap1->n_eq; ++i) {
7887 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7891 for (i = 0; i < bmap1->n_ineq; ++i) {
7892 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7896 for (i = 0; i < bmap1->n_div; ++i) {
7897 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7904 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
7905 const __isl_keep isl_basic_set *bset2)
7907 return isl_basic_map_plain_cmp(bset1, bset2);
7910 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
7911 const __isl_keep isl_set *set2)
7917 if (set1->n != set2->n)
7918 return set1->n - set2->n;
7920 for (i = 0; i < set1->n; ++i) {
7921 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
7929 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7930 __isl_keep isl_basic_map *bmap2)
7932 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7935 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7936 __isl_keep isl_basic_set *bset2)
7938 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7939 (isl_basic_map *)bset2);
7942 static int qsort_bmap_cmp(const void *p1, const void *p2)
7944 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7945 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7947 return isl_basic_map_plain_cmp(bmap1, bmap2);
7950 /* We normalize in place, but if anything goes wrong we need
7951 * to return NULL, so we need to make sure we don't change the
7952 * meaning of any possible other copies of map.
7954 struct isl_map *isl_map_normalize(struct isl_map *map)
7957 struct isl_basic_map *bmap;
7961 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7963 for (i = 0; i < map->n; ++i) {
7964 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7967 isl_basic_map_free(map->p[i]);
7970 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7971 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7972 map = isl_map_remove_empty_parts(map);
7975 for (i = map->n - 1; i >= 1; --i) {
7976 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7978 isl_basic_map_free(map->p[i-1]);
7979 for (j = i; j < map->n; ++j)
7980 map->p[j-1] = map->p[j];
7990 struct isl_set *isl_set_normalize(struct isl_set *set)
7992 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7995 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8005 if (!isl_space_is_equal(map1->dim, map2->dim))
8008 map1 = isl_map_copy(map1);
8009 map2 = isl_map_copy(map2);
8010 map1 = isl_map_normalize(map1);
8011 map2 = isl_map_normalize(map2);
8014 equal = map1->n == map2->n;
8015 for (i = 0; equal && i < map1->n; ++i) {
8016 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8029 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8031 return isl_map_plain_is_equal(map1, map2);
8034 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8036 return isl_map_plain_is_equal((struct isl_map *)set1,
8037 (struct isl_map *)set2);
8040 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8042 return isl_set_plain_is_equal(set1, set2);
8045 /* Return an interval that ranges from min to max (inclusive)
8047 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8048 isl_int min, isl_int max)
8051 struct isl_basic_set *bset = NULL;
8053 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8057 k = isl_basic_set_alloc_inequality(bset);
8060 isl_int_set_si(bset->ineq[k][1], 1);
8061 isl_int_neg(bset->ineq[k][0], min);
8063 k = isl_basic_set_alloc_inequality(bset);
8066 isl_int_set_si(bset->ineq[k][1], -1);
8067 isl_int_set(bset->ineq[k][0], max);
8071 isl_basic_set_free(bset);
8075 /* Return the Cartesian product of the basic sets in list (in the given order).
8077 __isl_give isl_basic_set *isl_basic_set_list_product(
8078 __isl_take struct isl_basic_set_list *list)
8086 struct isl_basic_set *product = NULL;
8090 isl_assert(list->ctx, list->n > 0, goto error);
8091 isl_assert(list->ctx, list->p[0], goto error);
8092 nparam = isl_basic_set_n_param(list->p[0]);
8093 dim = isl_basic_set_n_dim(list->p[0]);
8094 extra = list->p[0]->n_div;
8095 n_eq = list->p[0]->n_eq;
8096 n_ineq = list->p[0]->n_ineq;
8097 for (i = 1; i < list->n; ++i) {
8098 isl_assert(list->ctx, list->p[i], goto error);
8099 isl_assert(list->ctx,
8100 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8101 dim += isl_basic_set_n_dim(list->p[i]);
8102 extra += list->p[i]->n_div;
8103 n_eq += list->p[i]->n_eq;
8104 n_ineq += list->p[i]->n_ineq;
8106 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8111 for (i = 0; i < list->n; ++i) {
8112 isl_basic_set_add_constraints(product,
8113 isl_basic_set_copy(list->p[i]), dim);
8114 dim += isl_basic_set_n_dim(list->p[i]);
8116 isl_basic_set_list_free(list);
8119 isl_basic_set_free(product);
8120 isl_basic_set_list_free(list);
8124 struct isl_basic_map *isl_basic_map_product(
8125 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8127 isl_space *dim_result = NULL;
8128 struct isl_basic_map *bmap;
8129 unsigned in1, in2, out1, out2, nparam, total, pos;
8130 struct isl_dim_map *dim_map1, *dim_map2;
8132 if (!bmap1 || !bmap2)
8135 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8136 bmap2->dim, isl_dim_param), goto error);
8137 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8138 isl_space_copy(bmap2->dim));
8140 in1 = isl_basic_map_n_in(bmap1);
8141 in2 = isl_basic_map_n_in(bmap2);
8142 out1 = isl_basic_map_n_out(bmap1);
8143 out2 = isl_basic_map_n_out(bmap2);
8144 nparam = isl_basic_map_n_param(bmap1);
8146 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8147 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8148 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8149 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8150 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8151 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8152 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8153 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8154 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8155 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8156 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8158 bmap = isl_basic_map_alloc_space(dim_result,
8159 bmap1->n_div + bmap2->n_div,
8160 bmap1->n_eq + bmap2->n_eq,
8161 bmap1->n_ineq + bmap2->n_ineq);
8162 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8163 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8164 bmap = isl_basic_map_simplify(bmap);
8165 return isl_basic_map_finalize(bmap);
8167 isl_basic_map_free(bmap1);
8168 isl_basic_map_free(bmap2);
8172 __isl_give isl_basic_map *isl_basic_map_flat_product(
8173 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8175 isl_basic_map *prod;
8177 prod = isl_basic_map_product(bmap1, bmap2);
8178 prod = isl_basic_map_flatten(prod);
8182 __isl_give isl_basic_set *isl_basic_set_flat_product(
8183 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8185 return isl_basic_map_flat_range_product(bset1, bset2);
8188 __isl_give isl_basic_map *isl_basic_map_domain_product(
8189 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8191 isl_space *space_result = NULL;
8192 isl_basic_map *bmap;
8193 unsigned in1, in2, out, nparam, total, pos;
8194 struct isl_dim_map *dim_map1, *dim_map2;
8196 if (!bmap1 || !bmap2)
8199 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8200 isl_space_copy(bmap2->dim));
8202 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8203 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8204 out = isl_basic_map_dim(bmap1, isl_dim_out);
8205 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8207 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8208 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8209 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8210 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8211 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8212 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8213 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8214 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8215 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8216 isl_dim_map_div(dim_map1, bmap1, pos += out);
8217 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8219 bmap = isl_basic_map_alloc_space(space_result,
8220 bmap1->n_div + bmap2->n_div,
8221 bmap1->n_eq + bmap2->n_eq,
8222 bmap1->n_ineq + bmap2->n_ineq);
8223 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8224 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8225 bmap = isl_basic_map_simplify(bmap);
8226 return isl_basic_map_finalize(bmap);
8228 isl_basic_map_free(bmap1);
8229 isl_basic_map_free(bmap2);
8233 __isl_give isl_basic_map *isl_basic_map_range_product(
8234 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8236 isl_space *dim_result = NULL;
8237 isl_basic_map *bmap;
8238 unsigned in, out1, out2, nparam, total, pos;
8239 struct isl_dim_map *dim_map1, *dim_map2;
8241 if (!bmap1 || !bmap2)
8244 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8245 isl_space_copy(bmap2->dim));
8247 in = isl_basic_map_dim(bmap1, isl_dim_in);
8248 out1 = isl_basic_map_n_out(bmap1);
8249 out2 = isl_basic_map_n_out(bmap2);
8250 nparam = isl_basic_map_n_param(bmap1);
8252 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8253 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8254 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8255 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8256 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8257 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8258 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8259 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8260 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8261 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8262 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8264 bmap = isl_basic_map_alloc_space(dim_result,
8265 bmap1->n_div + bmap2->n_div,
8266 bmap1->n_eq + bmap2->n_eq,
8267 bmap1->n_ineq + bmap2->n_ineq);
8268 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8269 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8270 bmap = isl_basic_map_simplify(bmap);
8271 return isl_basic_map_finalize(bmap);
8273 isl_basic_map_free(bmap1);
8274 isl_basic_map_free(bmap2);
8278 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8279 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8281 isl_basic_map *prod;
8283 prod = isl_basic_map_range_product(bmap1, bmap2);
8284 prod = isl_basic_map_flatten_range(prod);
8288 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8289 __isl_take isl_map *map2,
8290 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8291 __isl_take isl_space *right),
8292 __isl_give isl_basic_map *(*basic_map_product)(
8293 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8296 struct isl_map *result;
8302 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8303 map2->dim, isl_dim_param), goto error);
8305 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8306 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8307 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8309 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8310 isl_space_copy(map2->dim)),
8311 map1->n * map2->n, flags);
8314 for (i = 0; i < map1->n; ++i)
8315 for (j = 0; j < map2->n; ++j) {
8316 struct isl_basic_map *part;
8317 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8318 isl_basic_map_copy(map2->p[j]));
8319 if (isl_basic_map_is_empty(part))
8320 isl_basic_map_free(part);
8322 result = isl_map_add_basic_map(result, part);
8335 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8337 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8338 __isl_take isl_map *map2)
8340 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8343 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8344 __isl_take isl_map *map2)
8346 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8349 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8351 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8352 __isl_take isl_map *map2)
8356 prod = isl_map_product(map1, map2);
8357 prod = isl_map_flatten(prod);
8361 /* Given two set A and B, construct its Cartesian product A x B.
8363 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8365 return isl_map_range_product(set1, set2);
8368 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8369 __isl_take isl_set *set2)
8371 return isl_map_flat_range_product(set1, set2);
8374 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8376 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8377 __isl_take isl_map *map2)
8379 return map_product(map1, map2, &isl_space_domain_product,
8380 &isl_basic_map_domain_product);
8383 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8385 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8386 __isl_take isl_map *map2)
8388 return map_product(map1, map2, &isl_space_range_product,
8389 &isl_basic_map_range_product);
8392 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8393 __isl_take isl_map *map2)
8395 return isl_map_align_params_map_map_and(map1, map2,
8396 &map_domain_product_aligned);
8399 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8400 __isl_take isl_map *map2)
8402 return isl_map_align_params_map_map_and(map1, map2,
8403 &map_range_product_aligned);
8406 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8408 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8409 __isl_take isl_map *map2)
8413 prod = isl_map_domain_product(map1, map2);
8414 prod = isl_map_flatten_domain(prod);
8418 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8420 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8421 __isl_take isl_map *map2)
8425 prod = isl_map_range_product(map1, map2);
8426 prod = isl_map_flatten_range(prod);
8430 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8433 uint32_t hash = isl_hash_init();
8438 bmap = isl_basic_map_copy(bmap);
8439 bmap = isl_basic_map_normalize(bmap);
8442 total = isl_basic_map_total_dim(bmap);
8443 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8444 for (i = 0; i < bmap->n_eq; ++i) {
8446 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8447 isl_hash_hash(hash, c_hash);
8449 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8450 for (i = 0; i < bmap->n_ineq; ++i) {
8452 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8453 isl_hash_hash(hash, c_hash);
8455 isl_hash_byte(hash, bmap->n_div & 0xFF);
8456 for (i = 0; i < bmap->n_div; ++i) {
8458 if (isl_int_is_zero(bmap->div[i][0]))
8460 isl_hash_byte(hash, i & 0xFF);
8461 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8462 isl_hash_hash(hash, c_hash);
8464 isl_basic_map_free(bmap);
8468 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8470 return isl_basic_map_get_hash((isl_basic_map *)bset);
8473 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8480 map = isl_map_copy(map);
8481 map = isl_map_normalize(map);
8485 hash = isl_hash_init();
8486 for (i = 0; i < map->n; ++i) {
8488 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8489 isl_hash_hash(hash, bmap_hash);
8497 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8499 return isl_map_get_hash((isl_map *)set);
8502 /* Check if the value for dimension dim is completely determined
8503 * by the values of the other parameters and variables.
8504 * That is, check if dimension dim is involved in an equality.
8506 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8513 nparam = isl_basic_set_n_param(bset);
8514 for (i = 0; i < bset->n_eq; ++i)
8515 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8520 /* Check if the value for dimension dim is completely determined
8521 * by the values of the other parameters and variables.
8522 * That is, check if dimension dim is involved in an equality
8523 * for each of the subsets.
8525 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8531 for (i = 0; i < set->n; ++i) {
8533 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8540 int isl_set_n_basic_set(__isl_keep isl_set *set)
8542 return set ? set->n : 0;
8545 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8546 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8553 for (i = 0; i < map->n; ++i)
8554 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8560 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8561 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8568 for (i = 0; i < set->n; ++i)
8569 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8575 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8582 bset = isl_basic_set_cow(bset);
8586 dim = isl_basic_set_get_space(bset);
8587 dim = isl_space_lift(dim, bset->n_div);
8590 isl_space_free(bset->dim);
8592 bset->extra -= bset->n_div;
8595 bset = isl_basic_set_finalize(bset);
8599 isl_basic_set_free(bset);
8603 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8609 set = isl_set_align_divs(set);
8614 set = isl_set_cow(set);
8618 n_div = set->p[0]->n_div;
8619 dim = isl_set_get_space(set);
8620 dim = isl_space_lift(dim, n_div);
8623 isl_space_free(set->dim);
8626 for (i = 0; i < set->n; ++i) {
8627 set->p[i] = isl_basic_set_lift(set->p[i]);
8638 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8641 struct isl_basic_map *bmap;
8648 set = isl_set_align_divs(set);
8653 dim = isl_set_get_space(set);
8654 if (set->n == 0 || set->p[0]->n_div == 0) {
8656 return isl_map_identity(isl_space_map_from_set(dim));
8659 n_div = set->p[0]->n_div;
8660 dim = isl_space_map_from_set(dim);
8661 n_param = isl_space_dim(dim, isl_dim_param);
8662 n_set = isl_space_dim(dim, isl_dim_in);
8663 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8664 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8665 for (i = 0; i < n_set; ++i)
8666 bmap = var_equal(bmap, i);
8668 total = n_param + n_set + n_set + n_div;
8669 for (i = 0; i < n_div; ++i) {
8670 k = isl_basic_map_alloc_inequality(bmap);
8673 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8674 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8675 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8676 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8677 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8678 set->p[0]->div[i][0]);
8680 l = isl_basic_map_alloc_inequality(bmap);
8683 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8684 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8685 set->p[0]->div[i][0]);
8686 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8690 bmap = isl_basic_map_simplify(bmap);
8691 bmap = isl_basic_map_finalize(bmap);
8692 return isl_map_from_basic_map(bmap);
8695 isl_basic_map_free(bmap);
8699 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8707 dim = isl_basic_set_total_dim(bset);
8708 size += bset->n_eq * (1 + dim);
8709 size += bset->n_ineq * (1 + dim);
8710 size += bset->n_div * (2 + dim);
8715 int isl_set_size(__isl_keep isl_set *set)
8723 for (i = 0; i < set->n; ++i)
8724 size += isl_basic_set_size(set->p[i]);
8729 /* Check if there is any lower bound (if lower == 0) and/or upper
8730 * bound (if upper == 0) on the specified dim.
8732 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8733 enum isl_dim_type type, unsigned pos, int lower, int upper)
8740 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8742 pos += isl_basic_map_offset(bmap, type);
8744 for (i = 0; i < bmap->n_div; ++i) {
8745 if (isl_int_is_zero(bmap->div[i][0]))
8747 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8751 for (i = 0; i < bmap->n_eq; ++i)
8752 if (!isl_int_is_zero(bmap->eq[i][pos]))
8755 for (i = 0; i < bmap->n_ineq; ++i) {
8756 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8763 return lower && upper;
8766 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8767 enum isl_dim_type type, unsigned pos)
8769 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8772 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8773 enum isl_dim_type type, unsigned pos)
8775 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8778 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8779 enum isl_dim_type type, unsigned pos)
8781 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8784 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8785 enum isl_dim_type type, unsigned pos)
8792 for (i = 0; i < map->n; ++i) {
8794 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8795 if (bounded < 0 || !bounded)
8802 /* Return 1 if the specified dim is involved in both an upper bound
8803 * and a lower bound.
8805 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8806 enum isl_dim_type type, unsigned pos)
8808 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8811 static int has_bound(__isl_keep isl_map *map,
8812 enum isl_dim_type type, unsigned pos,
8813 int (*fn)(__isl_keep isl_basic_map *bmap,
8814 enum isl_dim_type type, unsigned pos))
8821 for (i = 0; i < map->n; ++i) {
8823 bounded = fn(map->p[i], type, pos);
8824 if (bounded < 0 || bounded)
8831 /* Return 1 if the specified dim is involved in any lower bound.
8833 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8834 enum isl_dim_type type, unsigned pos)
8836 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8839 /* Return 1 if the specified dim is involved in any upper bound.
8841 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8842 enum isl_dim_type type, unsigned pos)
8844 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8847 /* For each of the "n" variables starting at "first", determine
8848 * the sign of the variable and put the results in the first "n"
8849 * elements of the array "signs".
8851 * 1 means that the variable is non-negative
8852 * -1 means that the variable is non-positive
8853 * 0 means the variable attains both positive and negative values.
8855 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8856 unsigned first, unsigned n, int *signs)
8858 isl_vec *bound = NULL;
8859 struct isl_tab *tab = NULL;
8860 struct isl_tab_undo *snap;
8863 if (!bset || !signs)
8866 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8867 tab = isl_tab_from_basic_set(bset, 0);
8871 isl_seq_clr(bound->el, bound->size);
8872 isl_int_set_si(bound->el[0], -1);
8874 snap = isl_tab_snap(tab);
8875 for (i = 0; i < n; ++i) {
8878 isl_int_set_si(bound->el[1 + first + i], -1);
8879 if (isl_tab_add_ineq(tab, bound->el) < 0)
8882 isl_int_set_si(bound->el[1 + first + i], 0);
8883 if (isl_tab_rollback(tab, snap) < 0)
8891 isl_int_set_si(bound->el[1 + first + i], 1);
8892 if (isl_tab_add_ineq(tab, bound->el) < 0)
8895 isl_int_set_si(bound->el[1 + first + i], 0);
8896 if (isl_tab_rollback(tab, snap) < 0)
8899 signs[i] = empty ? -1 : 0;
8903 isl_vec_free(bound);
8907 isl_vec_free(bound);
8911 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8912 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8914 if (!bset || !signs)
8916 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8919 first += pos(bset->dim, type) - 1;
8920 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8923 /* Check if the given basic map is obviously single-valued.
8924 * In particular, for each output dimension, check that there is
8925 * an equality that defines the output dimension in terms of
8926 * earlier dimensions.
8928 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8938 total = 1 + isl_basic_map_total_dim(bmap);
8939 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8940 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8942 for (i = 0; i < n_out; ++i) {
8943 for (j = 0; j < bmap->n_eq; ++j) {
8944 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8946 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8947 total - (o_out + i + 1)) == -1)
8950 if (j >= bmap->n_eq)
8957 /* Check if the given map is obviously single-valued.
8959 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8968 return isl_basic_map_plain_is_single_valued(map->p[0]);
8971 /* Check if the given map is single-valued.
8976 * and check if the result is a subset of the identity mapping.
8978 int isl_map_is_single_valued(__isl_keep isl_map *map)
8985 sv = isl_map_plain_is_single_valued(map);
8989 test = isl_map_reverse(isl_map_copy(map));
8990 test = isl_map_apply_range(test, isl_map_copy(map));
8992 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8993 id = isl_map_identity(dim);
8995 sv = isl_map_is_subset(test, id);
9003 int isl_map_is_injective(__isl_keep isl_map *map)
9007 map = isl_map_copy(map);
9008 map = isl_map_reverse(map);
9009 in = isl_map_is_single_valued(map);
9015 /* Check if the given map is obviously injective.
9017 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9021 map = isl_map_copy(map);
9022 map = isl_map_reverse(map);
9023 in = isl_map_plain_is_single_valued(map);
9029 int isl_map_is_bijective(__isl_keep isl_map *map)
9033 sv = isl_map_is_single_valued(map);
9037 return isl_map_is_injective(map);
9040 int isl_set_is_singleton(__isl_keep isl_set *set)
9042 return isl_map_is_single_valued((isl_map *)set);
9045 int isl_map_is_translation(__isl_keep isl_map *map)
9050 delta = isl_map_deltas(isl_map_copy(map));
9051 ok = isl_set_is_singleton(delta);
9052 isl_set_free(delta);
9057 static int unique(isl_int *p, unsigned pos, unsigned len)
9059 if (isl_seq_first_non_zero(p, pos) != -1)
9061 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9066 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9075 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9078 nvar = isl_basic_set_dim(bset, isl_dim_set);
9079 ovar = isl_space_offset(bset->dim, isl_dim_set);
9080 for (j = 0; j < nvar; ++j) {
9081 int lower = 0, upper = 0;
9082 for (i = 0; i < bset->n_eq; ++i) {
9083 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9085 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9091 for (i = 0; i < bset->n_ineq; ++i) {
9092 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
9094 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
9096 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
9101 if (!lower || !upper)
9108 int isl_set_is_box(__isl_keep isl_set *set)
9115 return isl_basic_set_is_box(set->p[0]);
9118 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
9123 return isl_space_is_wrapping(bset->dim);
9126 int isl_set_is_wrapping(__isl_keep isl_set *set)
9131 return isl_space_is_wrapping(set->dim);
9134 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
9136 bmap = isl_basic_map_cow(bmap);
9140 bmap->dim = isl_space_wrap(bmap->dim);
9144 bmap = isl_basic_map_finalize(bmap);
9146 return (isl_basic_set *)bmap;
9148 isl_basic_map_free(bmap);
9152 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9156 map = isl_map_cow(map);
9160 for (i = 0; i < map->n; ++i) {
9161 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9165 map->dim = isl_space_wrap(map->dim);
9169 return (isl_set *)map;
9175 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9177 bset = isl_basic_set_cow(bset);
9181 bset->dim = isl_space_unwrap(bset->dim);
9185 bset = isl_basic_set_finalize(bset);
9187 return (isl_basic_map *)bset;
9189 isl_basic_set_free(bset);
9193 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9200 if (!isl_set_is_wrapping(set))
9201 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9204 set = isl_set_cow(set);
9208 for (i = 0; i < set->n; ++i) {
9209 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9214 set->dim = isl_space_unwrap(set->dim);
9218 return (isl_map *)set;
9224 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9225 enum isl_dim_type type)
9230 if (!isl_space_is_named_or_nested(bmap->dim, type))
9233 bmap = isl_basic_map_cow(bmap);
9237 bmap->dim = isl_space_reset(bmap->dim, type);
9241 bmap = isl_basic_map_finalize(bmap);
9245 isl_basic_map_free(bmap);
9249 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9250 enum isl_dim_type type)
9257 if (!isl_space_is_named_or_nested(map->dim, type))
9260 map = isl_map_cow(map);
9264 for (i = 0; i < map->n; ++i) {
9265 map->p[i] = isl_basic_map_reset(map->p[i], type);
9269 map->dim = isl_space_reset(map->dim, type);
9279 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9284 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9287 bmap = isl_basic_map_cow(bmap);
9291 bmap->dim = isl_space_flatten(bmap->dim);
9295 bmap = isl_basic_map_finalize(bmap);
9299 isl_basic_map_free(bmap);
9303 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9305 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9308 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9309 __isl_take isl_basic_map *bmap)
9314 if (!bmap->dim->nested[0])
9317 bmap = isl_basic_map_cow(bmap);
9321 bmap->dim = isl_space_flatten_domain(bmap->dim);
9325 bmap = isl_basic_map_finalize(bmap);
9329 isl_basic_map_free(bmap);
9333 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9334 __isl_take isl_basic_map *bmap)
9339 if (!bmap->dim->nested[1])
9342 bmap = isl_basic_map_cow(bmap);
9346 bmap->dim = isl_space_flatten_range(bmap->dim);
9350 bmap = isl_basic_map_finalize(bmap);
9354 isl_basic_map_free(bmap);
9358 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9365 if (!map->dim->nested[0] && !map->dim->nested[1])
9368 map = isl_map_cow(map);
9372 for (i = 0; i < map->n; ++i) {
9373 map->p[i] = isl_basic_map_flatten(map->p[i]);
9377 map->dim = isl_space_flatten(map->dim);
9387 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9389 return (isl_set *)isl_map_flatten((isl_map *)set);
9392 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9394 isl_space *dim, *flat_dim;
9397 dim = isl_set_get_space(set);
9398 flat_dim = isl_space_flatten(isl_space_copy(dim));
9399 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9400 map = isl_map_intersect_domain(map, set);
9405 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9412 if (!map->dim->nested[0])
9415 map = isl_map_cow(map);
9419 for (i = 0; i < map->n; ++i) {
9420 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9424 map->dim = isl_space_flatten_domain(map->dim);
9434 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9441 if (!map->dim->nested[1])
9444 map = isl_map_cow(map);
9448 for (i = 0; i < map->n; ++i) {
9449 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9453 map->dim = isl_space_flatten_range(map->dim);
9463 /* Reorder the dimensions of "bmap" according to the given dim_map
9464 * and set the dimension specification to "dim".
9466 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9467 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9472 bmap = isl_basic_map_cow(bmap);
9473 if (!bmap || !dim || !dim_map)
9476 flags = bmap->flags;
9477 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
9478 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
9479 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
9480 res = isl_basic_map_alloc_space(dim,
9481 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9482 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9485 res = isl_basic_map_finalize(res);
9489 isl_basic_map_free(bmap);
9490 isl_space_free(dim);
9494 /* Reorder the dimensions of "map" according to given reordering.
9496 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9497 __isl_take isl_reordering *r)
9500 struct isl_dim_map *dim_map;
9502 map = isl_map_cow(map);
9503 dim_map = isl_dim_map_from_reordering(r);
9504 if (!map || !r || !dim_map)
9507 for (i = 0; i < map->n; ++i) {
9508 struct isl_dim_map *dim_map_i;
9510 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9512 map->p[i] = isl_basic_map_realign(map->p[i],
9513 isl_space_copy(r->dim), dim_map_i);
9519 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9521 isl_reordering_free(r);
9527 isl_reordering_free(r);
9531 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9532 __isl_take isl_reordering *r)
9534 return (isl_set *)isl_map_realign((isl_map *)set, r);
9537 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9538 __isl_take isl_space *model)
9545 ctx = isl_space_get_ctx(model);
9546 if (!isl_space_has_named_params(model))
9547 isl_die(ctx, isl_error_invalid,
9548 "model has unnamed parameters", goto error);
9549 if (!isl_space_has_named_params(map->dim))
9550 isl_die(ctx, isl_error_invalid,
9551 "relation has unnamed parameters", goto error);
9552 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9553 isl_reordering *exp;
9555 model = isl_space_drop_dims(model, isl_dim_in,
9556 0, isl_space_dim(model, isl_dim_in));
9557 model = isl_space_drop_dims(model, isl_dim_out,
9558 0, isl_space_dim(model, isl_dim_out));
9559 exp = isl_parameter_alignment_reordering(map->dim, model);
9560 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9561 map = isl_map_realign(map, exp);
9564 isl_space_free(model);
9567 isl_space_free(model);
9572 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9573 __isl_take isl_space *model)
9575 return isl_map_align_params(set, model);
9578 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9579 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9580 enum isl_dim_type c2, enum isl_dim_type c3,
9581 enum isl_dim_type c4, enum isl_dim_type c5)
9583 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9584 struct isl_mat *mat;
9590 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9591 isl_basic_map_total_dim(bmap) + 1);
9594 for (i = 0; i < bmap->n_eq; ++i)
9595 for (j = 0, pos = 0; j < 5; ++j) {
9596 int off = isl_basic_map_offset(bmap, c[j]);
9597 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9598 isl_int_set(mat->row[i][pos],
9599 bmap->eq[i][off + k]);
9607 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9608 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9609 enum isl_dim_type c2, enum isl_dim_type c3,
9610 enum isl_dim_type c4, enum isl_dim_type c5)
9612 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9613 struct isl_mat *mat;
9619 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9620 isl_basic_map_total_dim(bmap) + 1);
9623 for (i = 0; i < bmap->n_ineq; ++i)
9624 for (j = 0, pos = 0; j < 5; ++j) {
9625 int off = isl_basic_map_offset(bmap, c[j]);
9626 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9627 isl_int_set(mat->row[i][pos],
9628 bmap->ineq[i][off + k]);
9636 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9637 __isl_take isl_space *dim,
9638 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9639 enum isl_dim_type c2, enum isl_dim_type c3,
9640 enum isl_dim_type c4, enum isl_dim_type c5)
9642 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9643 isl_basic_map *bmap;
9649 if (!dim || !eq || !ineq)
9652 if (eq->n_col != ineq->n_col)
9653 isl_die(dim->ctx, isl_error_invalid,
9654 "equalities and inequalities matrices should have "
9655 "same number of columns", goto error);
9657 total = 1 + isl_space_dim(dim, isl_dim_all);
9659 if (eq->n_col < total)
9660 isl_die(dim->ctx, isl_error_invalid,
9661 "number of columns too small", goto error);
9663 extra = eq->n_col - total;
9665 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9666 eq->n_row, ineq->n_row);
9669 for (i = 0; i < extra; ++i) {
9670 k = isl_basic_map_alloc_div(bmap);
9673 isl_int_set_si(bmap->div[k][0], 0);
9675 for (i = 0; i < eq->n_row; ++i) {
9676 l = isl_basic_map_alloc_equality(bmap);
9679 for (j = 0, pos = 0; j < 5; ++j) {
9680 int off = isl_basic_map_offset(bmap, c[j]);
9681 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9682 isl_int_set(bmap->eq[l][off + k],
9688 for (i = 0; i < ineq->n_row; ++i) {
9689 l = isl_basic_map_alloc_inequality(bmap);
9692 for (j = 0, pos = 0; j < 5; ++j) {
9693 int off = isl_basic_map_offset(bmap, c[j]);
9694 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9695 isl_int_set(bmap->ineq[l][off + k],
9702 isl_space_free(dim);
9708 isl_space_free(dim);
9714 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9715 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9716 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9718 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9719 c1, c2, c3, c4, isl_dim_in);
9722 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9723 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9724 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9726 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9727 c1, c2, c3, c4, isl_dim_in);
9730 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9731 __isl_take isl_space *dim,
9732 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9733 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9735 return (isl_basic_set*)
9736 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9737 c1, c2, c3, c4, isl_dim_in);
9740 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9745 return isl_space_can_zip(bmap->dim);
9748 int isl_map_can_zip(__isl_keep isl_map *map)
9753 return isl_space_can_zip(map->dim);
9756 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9757 * (A -> C) -> (B -> D).
9759 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9768 if (!isl_basic_map_can_zip(bmap))
9769 isl_die(bmap->ctx, isl_error_invalid,
9770 "basic map cannot be zipped", goto error);
9771 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9772 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9773 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9774 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9775 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9778 bmap->dim = isl_space_zip(bmap->dim);
9783 isl_basic_map_free(bmap);
9787 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9788 * (A -> C) -> (B -> D).
9790 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9797 if (!isl_map_can_zip(map))
9798 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9801 map = isl_map_cow(map);
9805 for (i = 0; i < map->n; ++i) {
9806 map->p[i] = isl_basic_map_zip(map->p[i]);
9811 map->dim = isl_space_zip(map->dim);
9821 /* Construct a basic map mapping the domain of the affine expression
9822 * to a one-dimensional range prescribed by the affine expression.
9824 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9828 isl_local_space *ls;
9829 isl_basic_map *bmap;
9834 ls = isl_aff_get_local_space(aff);
9835 bmap = isl_basic_map_from_local_space(ls);
9836 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9837 k = isl_basic_map_alloc_equality(bmap);
9841 pos = isl_basic_map_offset(bmap, isl_dim_out);
9842 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9843 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9844 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9845 aff->v->size - (pos + 1));
9848 bmap = isl_basic_map_finalize(bmap);
9852 isl_basic_map_free(bmap);
9856 /* Construct a basic map mapping the domain the multi-affine expression
9857 * to its range, with each dimension in the range equated to the
9858 * corresponding affine expression.
9860 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
9861 __isl_take isl_multi_aff *maff)
9865 isl_basic_map *bmap;
9870 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
9871 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
9872 "invalid space", return isl_multi_aff_free(maff));
9874 space = isl_space_domain(isl_multi_aff_get_space(maff));
9875 bmap = isl_basic_map_universe(isl_space_from_domain(space));
9877 for (i = 0; i < maff->n; ++i) {
9879 isl_basic_map *bmap_i;
9881 aff = isl_aff_copy(maff->p[i]);
9882 bmap_i = isl_basic_map_from_aff(aff);
9884 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9887 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
9889 isl_multi_aff_free(maff);
9893 /* Construct a map mapping the domain the multi-affine expression
9894 * to its range, with each dimension in the range equated to the
9895 * corresponding affine expression.
9897 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
9899 isl_basic_map *bmap;
9901 bmap = isl_basic_map_from_multi_aff(maff);
9902 return isl_map_from_basic_map(bmap);
9905 /* Construct a basic map mapping a domain in the given space to
9906 * to an n-dimensional range, with n the number of elements in the list,
9907 * where each coordinate in the range is prescribed by the
9908 * corresponding affine expression.
9909 * The domains of all affine expressions in the list are assumed to match
9912 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9913 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9917 isl_basic_map *bmap;
9922 dim = isl_space_from_domain(domain_dim);
9923 bmap = isl_basic_map_universe(dim);
9925 for (i = 0; i < list->n; ++i) {
9927 isl_basic_map *bmap_i;
9929 aff = isl_aff_copy(list->p[i]);
9930 bmap_i = isl_basic_map_from_aff(aff);
9932 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9935 isl_aff_list_free(list);
9939 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9940 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9942 return isl_map_equate(set, type1, pos1, type2, pos2);
9945 /* Add a constraint imposing that the given two dimensions are equal.
9947 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9948 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9950 isl_basic_map *bmap = NULL;
9956 if (pos1 >= isl_map_dim(map, type1))
9957 isl_die(map->ctx, isl_error_invalid,
9958 "index out of bounds", goto error);
9959 if (pos2 >= isl_map_dim(map, type2))
9960 isl_die(map->ctx, isl_error_invalid,
9961 "index out of bounds", goto error);
9963 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9964 i = isl_basic_map_alloc_equality(bmap);
9967 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9968 pos1 += isl_basic_map_offset(bmap, type1);
9969 pos2 += isl_basic_map_offset(bmap, type2);
9970 isl_int_set_si(bmap->eq[i][pos1], -1);
9971 isl_int_set_si(bmap->eq[i][pos2], 1);
9972 bmap = isl_basic_map_finalize(bmap);
9974 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9978 isl_basic_map_free(bmap);
9983 /* Add a constraint imposing that the given two dimensions have opposite values.
9985 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9986 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9988 isl_basic_map *bmap = NULL;
9994 if (pos1 >= isl_map_dim(map, type1))
9995 isl_die(map->ctx, isl_error_invalid,
9996 "index out of bounds", goto error);
9997 if (pos2 >= isl_map_dim(map, type2))
9998 isl_die(map->ctx, isl_error_invalid,
9999 "index out of bounds", goto error);
10001 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
10002 i = isl_basic_map_alloc_equality(bmap);
10005 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10006 pos1 += isl_basic_map_offset(bmap, type1);
10007 pos2 += isl_basic_map_offset(bmap, type2);
10008 isl_int_set_si(bmap->eq[i][pos1], 1);
10009 isl_int_set_si(bmap->eq[i][pos2], 1);
10010 bmap = isl_basic_map_finalize(bmap);
10012 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10016 isl_basic_map_free(bmap);
10021 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
10025 isl_local_space *ls;
10030 if (!isl_basic_map_divs_known(bmap))
10031 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
10032 "some divs are unknown", return NULL);
10034 ls = isl_basic_map_get_local_space(bmap);
10035 div = isl_local_space_get_div(ls, pos);
10036 isl_local_space_free(ls);
10041 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
10044 return isl_basic_map_get_div(bset, pos);
10047 /* Plug in "subs" for dimension "type", "pos" of "bset".
10049 * Let i be the dimension to replace and let "subs" be of the form
10053 * Any integer division with a non-zero coefficient for i,
10055 * floor((a i + g)/m)
10059 * floor((a f + d g)/(m d))
10061 * Constraints of the form
10069 __isl_give isl_basic_set *isl_basic_set_substitute(
10070 __isl_take isl_basic_set *bset,
10071 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10077 if (bset && isl_basic_set_plain_is_empty(bset))
10080 bset = isl_basic_set_cow(bset);
10081 if (!bset || !subs)
10084 ctx = isl_basic_set_get_ctx(bset);
10085 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
10086 isl_die(ctx, isl_error_invalid,
10087 "spaces don't match", goto error);
10088 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
10089 isl_die(ctx, isl_error_unsupported,
10090 "cannot handle divs yet", goto error);
10092 pos += isl_basic_set_offset(bset, type);
10096 for (i = 0; i < bset->n_eq; ++i) {
10097 if (isl_int_is_zero(bset->eq[i][pos]))
10099 isl_int_set(v, bset->eq[i][pos]);
10100 isl_int_set_si(bset->eq[i][pos], 0);
10101 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
10102 v, subs->v->el + 1, subs->v->size - 1);
10105 for (i = 0; i < bset->n_ineq; ++i) {
10106 if (isl_int_is_zero(bset->ineq[i][pos]))
10108 isl_int_set(v, bset->ineq[i][pos]);
10109 isl_int_set_si(bset->ineq[i][pos], 0);
10110 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
10111 v, subs->v->el + 1, subs->v->size - 1);
10114 for (i = 0; i < bset->n_div; ++i) {
10115 if (isl_int_is_zero(bset->div[i][1 + pos]))
10117 isl_int_set(v, bset->div[i][1 + pos]);
10118 isl_int_set_si(bset->div[i][1 + pos], 0);
10119 isl_seq_combine(bset->div[i] + 1,
10120 subs->v->el[0], bset->div[i] + 1,
10121 v, subs->v->el + 1, subs->v->size - 1);
10122 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
10127 bset = isl_basic_set_simplify(bset);
10128 return isl_basic_set_finalize(bset);
10130 isl_basic_set_free(bset);
10134 /* Plug in "subs" for dimension "type", "pos" of "set".
10136 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
10137 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10141 if (set && isl_set_plain_is_empty(set))
10144 set = isl_set_cow(set);
10148 for (i = set->n - 1; i >= 0; --i) {
10149 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
10150 if (remove_if_empty(set, i) < 0)