2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
19 #include "isl_space_private.h"
20 #include "isl_equalities.h"
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
30 #include <isl_mat_private.h>
31 #include <isl_dim_map.h>
32 #include <isl_local_space_private.h>
33 #include <isl_aff_private.h>
34 #include <isl_options_private.h>
35 #include <isl_morph.h>
36 #include <isl_val_private.h>
38 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
41 case isl_dim_param: return dim->nparam;
42 case isl_dim_in: return dim->n_in;
43 case isl_dim_out: return dim->n_out;
44 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
49 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
52 case isl_dim_param: return 1;
53 case isl_dim_in: return 1 + dim->nparam;
54 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
59 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
60 enum isl_dim_type type)
65 case isl_dim_cst: return 1;
68 case isl_dim_out: return isl_space_dim(bmap->dim, type);
69 case isl_dim_div: return bmap->n_div;
70 case isl_dim_all: return isl_basic_map_total_dim(bmap);
75 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
77 return map ? n(map->dim, type) : 0;
80 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
82 return set ? n(set->dim, type) : 0;
85 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
86 enum isl_dim_type type)
88 isl_space *dim = bmap->dim;
90 case isl_dim_cst: return 0;
91 case isl_dim_param: return 1;
92 case isl_dim_in: return 1 + dim->nparam;
93 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
94 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
99 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
100 enum isl_dim_type type)
102 return isl_basic_map_offset(bset, type);
105 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
107 return pos(map->dim, type);
110 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
111 enum isl_dim_type type)
113 return isl_basic_map_dim(bset, type);
116 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
118 return isl_basic_set_dim(bset, isl_dim_set);
121 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
123 return isl_basic_set_dim(bset, isl_dim_param);
126 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
130 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
133 unsigned isl_set_n_dim(__isl_keep isl_set *set)
135 return isl_set_dim(set, isl_dim_set);
138 unsigned isl_set_n_param(__isl_keep isl_set *set)
140 return isl_set_dim(set, isl_dim_param);
143 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
145 return bmap ? bmap->dim->n_in : 0;
148 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
150 return bmap ? bmap->dim->n_out : 0;
153 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
155 return bmap ? bmap->dim->nparam : 0;
158 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
160 return bmap ? bmap->n_div : 0;
163 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
165 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
168 unsigned isl_map_n_in(const struct isl_map *map)
170 return map ? map->dim->n_in : 0;
173 unsigned isl_map_n_out(const struct isl_map *map)
175 return map ? map->dim->n_out : 0;
178 unsigned isl_map_n_param(const struct isl_map *map)
180 return map ? map->dim->nparam : 0;
183 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
188 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
191 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
194 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
195 struct isl_basic_set *bset)
200 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
203 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
206 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
211 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
214 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
217 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
218 struct isl_basic_set *bset)
223 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
226 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
229 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
231 return bmap ? bmap->ctx : NULL;
234 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
236 return bset ? bset->ctx : NULL;
239 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
241 return map ? map->ctx : NULL;
244 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
246 return set ? set->ctx : NULL;
249 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
253 return isl_space_copy(bmap->dim);
256 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
260 return isl_space_copy(bset->dim);
263 /* Extract the divs in "bmap" as a matrix.
265 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
276 ctx = isl_basic_map_get_ctx(bmap);
277 total = isl_space_dim(bmap->dim, isl_dim_all);
278 cols = 1 + 1 + total + bmap->n_div;
279 div = isl_mat_alloc(ctx, bmap->n_div, cols);
283 for (i = 0; i < bmap->n_div; ++i)
284 isl_seq_cpy(div->row[i], bmap->div[i], cols);
289 /* Extract the divs in "bset" as a matrix.
291 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
293 return isl_basic_map_get_divs(bset);
296 __isl_give isl_local_space *isl_basic_map_get_local_space(
297 __isl_keep isl_basic_map *bmap)
304 div = isl_basic_map_get_divs(bmap);
305 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
308 __isl_give isl_local_space *isl_basic_set_get_local_space(
309 __isl_keep isl_basic_set *bset)
311 return isl_basic_map_get_local_space(bset);
314 __isl_give isl_basic_map *isl_basic_map_from_local_space(
315 __isl_take isl_local_space *ls)
324 n_div = isl_local_space_dim(ls, isl_dim_div);
325 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
326 n_div, 0, 2 * n_div);
328 for (i = 0; i < n_div; ++i)
329 if (isl_basic_map_alloc_div(bmap) < 0)
332 for (i = 0; i < n_div; ++i) {
333 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
334 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
338 isl_local_space_free(ls);
341 isl_local_space_free(ls);
342 isl_basic_map_free(bmap);
346 __isl_give isl_basic_set *isl_basic_set_from_local_space(
347 __isl_take isl_local_space *ls)
349 return isl_basic_map_from_local_space(ls);
352 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
356 return isl_space_copy(map->dim);
359 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
363 return isl_space_copy(set->dim);
366 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
367 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
369 bmap = isl_basic_map_cow(bmap);
372 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
375 bmap = isl_basic_map_finalize(bmap);
378 isl_basic_map_free(bmap);
382 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
383 __isl_take isl_basic_set *bset, const char *s)
385 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
388 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
389 enum isl_dim_type type)
391 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
394 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
395 enum isl_dim_type type, const char *s)
399 map = isl_map_cow(map);
403 map->dim = isl_space_set_tuple_name(map->dim, type, s);
407 for (i = 0; i < map->n; ++i) {
408 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
419 /* Does the input or output tuple have a name?
421 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
423 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
426 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
427 enum isl_dim_type type)
429 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
432 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
435 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
438 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
439 enum isl_dim_type type, __isl_take isl_id *id)
441 map = isl_map_cow(map);
443 return isl_id_free(id);
445 map->dim = isl_space_set_tuple_id(map->dim, type, id);
447 return isl_map_reset_space(map, isl_space_copy(map->dim));
450 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
451 __isl_take isl_id *id)
453 return isl_map_set_tuple_id(set, isl_dim_set, id);
456 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
457 enum isl_dim_type type)
459 map = isl_map_cow(map);
463 map->dim = isl_space_reset_tuple_id(map->dim, type);
465 return isl_map_reset_space(map, isl_space_copy(map->dim));
468 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
470 return isl_map_reset_tuple_id(set, isl_dim_set);
473 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
475 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
478 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
479 enum isl_dim_type type)
481 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
484 int isl_set_has_tuple_id(__isl_keep isl_set *set)
486 return isl_map_has_tuple_id(set, isl_dim_set);
489 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
491 return isl_map_get_tuple_id(set, isl_dim_set);
494 /* Does the set tuple have a name?
496 int isl_set_has_tuple_name(__isl_keep isl_set *set)
498 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
502 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
504 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
507 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
509 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
512 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
513 enum isl_dim_type type, unsigned pos)
515 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
518 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
519 enum isl_dim_type type, unsigned pos)
521 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
524 /* Does the given dimension have a name?
526 int isl_map_has_dim_name(__isl_keep isl_map *map,
527 enum isl_dim_type type, unsigned pos)
529 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
532 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
533 enum isl_dim_type type, unsigned pos)
535 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
538 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
539 enum isl_dim_type type, unsigned pos)
541 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
544 /* Does the given dimension have a name?
546 int isl_set_has_dim_name(__isl_keep isl_set *set,
547 enum isl_dim_type type, unsigned pos)
549 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
552 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
553 __isl_take isl_basic_map *bmap,
554 enum isl_dim_type type, unsigned pos, const char *s)
556 bmap = isl_basic_map_cow(bmap);
559 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
562 return isl_basic_map_finalize(bmap);
564 isl_basic_map_free(bmap);
568 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
569 enum isl_dim_type type, unsigned pos, const char *s)
573 map = isl_map_cow(map);
577 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
581 for (i = 0; i < map->n; ++i) {
582 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
593 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
594 __isl_take isl_basic_set *bset,
595 enum isl_dim_type type, unsigned pos, const char *s)
597 return (isl_basic_set *)isl_basic_map_set_dim_name(
598 (isl_basic_map *)bset, type, pos, s);
601 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
602 enum isl_dim_type type, unsigned pos, const char *s)
604 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
607 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
608 enum isl_dim_type type, unsigned pos)
610 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
613 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
614 enum isl_dim_type type, unsigned pos)
616 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
619 int isl_map_has_dim_id(__isl_keep isl_map *map,
620 enum isl_dim_type type, unsigned pos)
622 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
625 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
626 enum isl_dim_type type, unsigned pos)
628 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
631 int isl_set_has_dim_id(__isl_keep isl_set *set,
632 enum isl_dim_type type, unsigned pos)
634 return isl_map_has_dim_id(set, type, pos);
637 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
638 enum isl_dim_type type, unsigned pos)
640 return isl_map_get_dim_id(set, type, pos);
643 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
644 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
646 map = isl_map_cow(map);
648 return isl_id_free(id);
650 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
652 return isl_map_reset_space(map, isl_space_copy(map->dim));
655 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
656 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
658 return isl_map_set_dim_id(set, type, pos, id);
661 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
662 __isl_keep isl_id *id)
666 return isl_space_find_dim_by_id(map->dim, type, id);
669 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
670 __isl_keep isl_id *id)
672 return isl_map_find_dim_by_id(set, type, id);
675 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
680 return isl_space_find_dim_by_name(map->dim, type, name);
683 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
686 return isl_map_find_dim_by_name(set, type, name);
689 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
693 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
696 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
698 return isl_basic_map_is_rational(bset);
701 /* Does "bmap" contain any rational points?
703 * If "bmap" has an equality for each dimension, equating the dimension
704 * to an integer constant, then it has no rational points, even if it
705 * is marked as rational.
707 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
709 int has_rational = 1;
714 if (isl_basic_map_plain_is_empty(bmap))
716 if (!isl_basic_map_is_rational(bmap))
718 bmap = isl_basic_map_copy(bmap);
719 bmap = isl_basic_map_implicit_equalities(bmap);
722 total = isl_basic_map_total_dim(bmap);
723 if (bmap->n_eq == total) {
725 for (i = 0; i < bmap->n_eq; ++i) {
726 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
729 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
730 !isl_int_is_negone(bmap->eq[i][1 + j]))
732 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
740 isl_basic_map_free(bmap);
745 /* Does "map" contain any rational points?
747 int isl_map_has_rational(__isl_keep isl_map *map)
754 for (i = 0; i < map->n; ++i) {
755 has_rational = isl_basic_map_has_rational(map->p[i]);
756 if (has_rational < 0)
764 /* Does "set" contain any rational points?
766 int isl_set_has_rational(__isl_keep isl_set *set)
768 return isl_map_has_rational(set);
771 /* Is this basic set a parameter domain?
773 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
777 return isl_space_is_params(bset->dim);
780 /* Is this set a parameter domain?
782 int isl_set_is_params(__isl_keep isl_set *set)
786 return isl_space_is_params(set->dim);
789 /* Is this map actually a parameter domain?
790 * Users should never call this function. Outside of isl,
791 * a map can never be a parameter domain.
793 int isl_map_is_params(__isl_keep isl_map *map)
797 return isl_space_is_params(map->dim);
800 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
801 struct isl_basic_map *bmap, unsigned extra,
802 unsigned n_eq, unsigned n_ineq)
805 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
810 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
811 if (isl_blk_is_error(bmap->block))
814 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
819 bmap->block2 = isl_blk_empty();
822 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
823 if (isl_blk_is_error(bmap->block2))
826 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
831 for (i = 0; i < n_ineq + n_eq; ++i)
832 bmap->ineq[i] = bmap->block.data + i * row_size;
834 for (i = 0; i < extra; ++i)
835 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
839 bmap->c_size = n_eq + n_ineq;
840 bmap->eq = bmap->ineq + n_ineq;
849 isl_basic_map_free(bmap);
853 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
854 unsigned nparam, unsigned dim, unsigned extra,
855 unsigned n_eq, unsigned n_ineq)
857 struct isl_basic_map *bmap;
860 space = isl_space_set_alloc(ctx, nparam, dim);
864 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
865 return (struct isl_basic_set *)bmap;
868 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
869 unsigned extra, unsigned n_eq, unsigned n_ineq)
871 struct isl_basic_map *bmap;
874 isl_assert(dim->ctx, dim->n_in == 0, goto error);
875 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
876 return (struct isl_basic_set *)bmap;
882 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
883 unsigned extra, unsigned n_eq, unsigned n_ineq)
885 struct isl_basic_map *bmap;
889 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
894 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
900 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
901 unsigned nparam, unsigned in, unsigned out, unsigned extra,
902 unsigned n_eq, unsigned n_ineq)
904 struct isl_basic_map *bmap;
907 dim = isl_space_alloc(ctx, nparam, in, out);
911 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
915 static void dup_constraints(
916 struct isl_basic_map *dst, struct isl_basic_map *src)
919 unsigned total = isl_basic_map_total_dim(src);
921 for (i = 0; i < src->n_eq; ++i) {
922 int j = isl_basic_map_alloc_equality(dst);
923 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
926 for (i = 0; i < src->n_ineq; ++i) {
927 int j = isl_basic_map_alloc_inequality(dst);
928 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
931 for (i = 0; i < src->n_div; ++i) {
932 int j = isl_basic_map_alloc_div(dst);
933 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
935 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
938 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
940 struct isl_basic_map *dup;
944 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
945 bmap->n_div, bmap->n_eq, bmap->n_ineq);
948 dup_constraints(dup, bmap);
949 dup->flags = bmap->flags;
950 dup->sample = isl_vec_copy(bmap->sample);
954 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
956 struct isl_basic_map *dup;
958 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
959 return (struct isl_basic_set *)dup;
962 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
967 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
971 return isl_basic_set_dup(bset);
974 struct isl_set *isl_set_copy(struct isl_set *set)
983 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
988 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
992 bmap = isl_basic_map_dup(bmap);
994 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
998 struct isl_map *isl_map_copy(struct isl_map *map)
1007 void *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1012 if (--bmap->ref > 0)
1015 isl_ctx_deref(bmap->ctx);
1017 isl_blk_free(bmap->ctx, bmap->block2);
1019 isl_blk_free(bmap->ctx, bmap->block);
1020 isl_vec_free(bmap->sample);
1021 isl_space_free(bmap->dim);
1027 void *isl_basic_set_free(struct isl_basic_set *bset)
1029 return isl_basic_map_free((struct isl_basic_map *)bset);
1032 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1034 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1037 __isl_give isl_map *isl_map_align_params_map_map_and(
1038 __isl_take isl_map *map1, __isl_take isl_map *map2,
1039 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1040 __isl_take isl_map *map2))
1044 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1045 return fn(map1, map2);
1046 if (!isl_space_has_named_params(map1->dim) ||
1047 !isl_space_has_named_params(map2->dim))
1048 isl_die(map1->ctx, isl_error_invalid,
1049 "unaligned unnamed parameters", goto error);
1050 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1051 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1052 return fn(map1, map2);
1059 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1060 __isl_keep isl_map *map2,
1061 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1067 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1068 return fn(map1, map2);
1069 if (!isl_space_has_named_params(map1->dim) ||
1070 !isl_space_has_named_params(map2->dim))
1071 isl_die(map1->ctx, isl_error_invalid,
1072 "unaligned unnamed parameters", return -1);
1073 map1 = isl_map_copy(map1);
1074 map2 = isl_map_copy(map2);
1075 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1076 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1083 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1085 struct isl_ctx *ctx;
1089 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1090 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1092 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1093 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1094 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1095 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1096 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1097 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1099 int j = isl_basic_map_alloc_inequality(bmap);
1103 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1104 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1111 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1112 bmap->extra - bmap->n_div);
1113 return bmap->n_eq++;
1116 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1118 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1121 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1125 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1130 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1132 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1135 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1140 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1142 if (pos != bmap->n_eq - 1) {
1144 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1145 bmap->eq[bmap->n_eq - 1] = t;
1151 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1153 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1156 /* Turn inequality "pos" of "bmap" into an equality.
1158 * In particular, we move the inequality in front of the equalities
1159 * and move the last inequality in the position of the moved inequality.
1160 * Note that isl_tab_make_equalities_explicit depends on this particular
1161 * change in the ordering of the constraints.
1163 void isl_basic_map_inequality_to_equality(
1164 struct isl_basic_map *bmap, unsigned pos)
1168 t = bmap->ineq[pos];
1169 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1170 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1175 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1176 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1177 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1178 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1181 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1183 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1186 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1188 struct isl_ctx *ctx;
1192 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1193 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1194 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1195 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1196 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1197 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1198 1 + isl_basic_map_total_dim(bmap),
1199 bmap->extra - bmap->n_div);
1200 return bmap->n_ineq++;
1203 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1205 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1208 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1212 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1217 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1219 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1222 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1227 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1229 if (pos != bmap->n_ineq - 1) {
1230 t = bmap->ineq[pos];
1231 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1232 bmap->ineq[bmap->n_ineq - 1] = t;
1233 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1239 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1241 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1244 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1249 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1252 k = isl_basic_map_alloc_equality(bmap);
1255 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1258 isl_basic_map_free(bmap);
1262 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1265 return (isl_basic_set *)
1266 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1269 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1274 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1277 k = isl_basic_map_alloc_inequality(bmap);
1280 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1283 isl_basic_map_free(bmap);
1287 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1290 return (isl_basic_set *)
1291 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1294 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1298 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1299 isl_seq_clr(bmap->div[bmap->n_div] +
1300 1 + 1 + isl_basic_map_total_dim(bmap),
1301 bmap->extra - bmap->n_div);
1302 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1303 return bmap->n_div++;
1306 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1308 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1311 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1315 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1320 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1322 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1325 /* Copy constraint from src to dst, putting the vars of src at offset
1326 * dim_off in dst and the divs of src at offset div_off in dst.
1327 * If both sets are actually map, then dim_off applies to the input
1330 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1331 struct isl_basic_map *src_map, isl_int *src,
1332 unsigned in_off, unsigned out_off, unsigned div_off)
1334 unsigned src_nparam = isl_basic_map_n_param(src_map);
1335 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1336 unsigned src_in = isl_basic_map_n_in(src_map);
1337 unsigned dst_in = isl_basic_map_n_in(dst_map);
1338 unsigned src_out = isl_basic_map_n_out(src_map);
1339 unsigned dst_out = isl_basic_map_n_out(dst_map);
1340 isl_int_set(dst[0], src[0]);
1341 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1342 if (dst_nparam > src_nparam)
1343 isl_seq_clr(dst+1+src_nparam,
1344 dst_nparam - src_nparam);
1345 isl_seq_clr(dst+1+dst_nparam, in_off);
1346 isl_seq_cpy(dst+1+dst_nparam+in_off,
1348 isl_min(dst_in-in_off, src_in));
1349 if (dst_in-in_off > src_in)
1350 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1351 dst_in - in_off - src_in);
1352 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1353 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1354 src+1+src_nparam+src_in,
1355 isl_min(dst_out-out_off, src_out));
1356 if (dst_out-out_off > src_out)
1357 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1358 dst_out - out_off - src_out);
1359 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1360 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1361 src+1+src_nparam+src_in+src_out,
1362 isl_min(dst_map->extra-div_off, src_map->n_div));
1363 if (dst_map->n_div-div_off > src_map->n_div)
1364 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1365 div_off+src_map->n_div,
1366 dst_map->n_div - div_off - src_map->n_div);
1369 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1370 struct isl_basic_map *src_map, isl_int *src,
1371 unsigned in_off, unsigned out_off, unsigned div_off)
1373 isl_int_set(dst[0], src[0]);
1374 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1377 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1378 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1383 if (!bmap1 || !bmap2)
1386 div_off = bmap1->n_div;
1388 for (i = 0; i < bmap2->n_eq; ++i) {
1389 int i1 = isl_basic_map_alloc_equality(bmap1);
1392 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1393 i_pos, o_pos, div_off);
1396 for (i = 0; i < bmap2->n_ineq; ++i) {
1397 int i1 = isl_basic_map_alloc_inequality(bmap1);
1400 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1401 i_pos, o_pos, div_off);
1404 for (i = 0; i < bmap2->n_div; ++i) {
1405 int i1 = isl_basic_map_alloc_div(bmap1);
1408 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1409 i_pos, o_pos, div_off);
1412 isl_basic_map_free(bmap2);
1417 isl_basic_map_free(bmap1);
1418 isl_basic_map_free(bmap2);
1422 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1423 struct isl_basic_set *bset2, unsigned pos)
1425 return (struct isl_basic_set *)
1426 add_constraints((struct isl_basic_map *)bset1,
1427 (struct isl_basic_map *)bset2, 0, pos);
1430 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1431 __isl_take isl_space *dim, unsigned extra,
1432 unsigned n_eq, unsigned n_ineq)
1434 struct isl_basic_map *ext;
1444 dims_ok = isl_space_is_equal(base->dim, dim) &&
1445 base->extra >= base->n_div + extra;
1447 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1448 room_for_ineq(base, n_ineq)) {
1449 isl_space_free(dim);
1453 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1454 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1455 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1456 extra += base->extra;
1458 n_ineq += base->n_ineq;
1460 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1466 ext->sample = isl_vec_copy(base->sample);
1467 flags = base->flags;
1468 ext = add_constraints(ext, base, 0, 0);
1471 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1477 isl_space_free(dim);
1478 isl_basic_map_free(base);
1482 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1483 __isl_take isl_space *dim, unsigned extra,
1484 unsigned n_eq, unsigned n_ineq)
1486 return (struct isl_basic_set *)
1487 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1488 extra, n_eq, n_ineq);
1491 struct isl_basic_map *isl_basic_map_extend_constraints(
1492 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1496 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1500 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1501 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1502 unsigned n_eq, unsigned n_ineq)
1504 struct isl_basic_map *bmap;
1509 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1513 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1516 isl_basic_map_free(base);
1520 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1521 unsigned nparam, unsigned dim, unsigned extra,
1522 unsigned n_eq, unsigned n_ineq)
1524 return (struct isl_basic_set *)
1525 isl_basic_map_extend((struct isl_basic_map *)base,
1526 nparam, 0, dim, extra, n_eq, n_ineq);
1529 struct isl_basic_set *isl_basic_set_extend_constraints(
1530 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1532 return (struct isl_basic_set *)
1533 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1537 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1539 return (struct isl_basic_set *)
1540 isl_basic_map_cow((struct isl_basic_map *)bset);
1543 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1548 if (bmap->ref > 1) {
1550 bmap = isl_basic_map_dup(bmap);
1553 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1557 struct isl_set *isl_set_cow(struct isl_set *set)
1565 return isl_set_dup(set);
1568 struct isl_map *isl_map_cow(struct isl_map *map)
1576 return isl_map_dup(map);
1579 static void swap_vars(struct isl_blk blk, isl_int *a,
1580 unsigned a_len, unsigned b_len)
1582 isl_seq_cpy(blk.data, a+a_len, b_len);
1583 isl_seq_cpy(blk.data+b_len, a, a_len);
1584 isl_seq_cpy(a, blk.data, b_len+a_len);
1587 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1588 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1596 isl_assert(bmap->ctx,
1597 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1599 if (n1 == 0 || n2 == 0)
1602 bmap = isl_basic_map_cow(bmap);
1606 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1607 if (isl_blk_is_error(blk))
1610 for (i = 0; i < bmap->n_eq; ++i)
1612 bmap->eq[i] + pos, n1, n2);
1614 for (i = 0; i < bmap->n_ineq; ++i)
1616 bmap->ineq[i] + pos, n1, n2);
1618 for (i = 0; i < bmap->n_div; ++i)
1620 bmap->div[i]+1 + pos, n1, n2);
1622 isl_blk_free(bmap->ctx, blk);
1624 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1625 bmap = isl_basic_map_gauss(bmap, NULL);
1626 return isl_basic_map_finalize(bmap);
1628 isl_basic_map_free(bmap);
1632 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1633 __isl_take isl_basic_set *bset, unsigned n)
1641 nparam = isl_basic_set_n_param(bset);
1642 dim = isl_basic_set_n_dim(bset);
1643 isl_assert(bset->ctx, n <= dim, goto error);
1645 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1647 isl_basic_set_free(bset);
1651 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1657 total = isl_basic_map_total_dim(bmap);
1658 isl_basic_map_free_div(bmap, bmap->n_div);
1659 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1661 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1663 i = isl_basic_map_alloc_equality(bmap);
1667 isl_int_set_si(bmap->eq[i][0], 1);
1668 isl_seq_clr(bmap->eq[i]+1, total);
1669 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1670 isl_vec_free(bmap->sample);
1671 bmap->sample = NULL;
1672 return isl_basic_map_finalize(bmap);
1674 isl_basic_map_free(bmap);
1678 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1680 return (struct isl_basic_set *)
1681 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1684 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1687 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1689 isl_int *t = bmap->div[a];
1690 bmap->div[a] = bmap->div[b];
1694 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1695 * div definitions accordingly.
1697 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1700 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1702 swap_div(bmap, a, b);
1704 for (i = 0; i < bmap->n_eq; ++i)
1705 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1707 for (i = 0; i < bmap->n_ineq; ++i)
1708 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1710 for (i = 0; i < bmap->n_div; ++i)
1711 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1712 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1715 /* Eliminate the specified n dimensions starting at first from the
1716 * constraints, without removing the dimensions from the space.
1717 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1719 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1720 enum isl_dim_type type, unsigned first, unsigned n)
1729 if (first + n > isl_map_dim(map, type) || first + n < first)
1730 isl_die(map->ctx, isl_error_invalid,
1731 "index out of bounds", goto error);
1733 map = isl_map_cow(map);
1737 for (i = 0; i < map->n; ++i) {
1738 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1748 /* Eliminate the specified n dimensions starting at first from the
1749 * constraints, without removing the dimensions from the space.
1750 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1752 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1753 enum isl_dim_type type, unsigned first, unsigned n)
1755 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1758 /* Eliminate the specified n dimensions starting at first from the
1759 * constraints, without removing the dimensions from the space.
1760 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1762 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1763 unsigned first, unsigned n)
1765 return isl_set_eliminate(set, isl_dim_set, first, n);
1768 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1769 __isl_take isl_basic_map *bmap)
1773 bmap = isl_basic_map_eliminate_vars(bmap,
1774 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1778 return isl_basic_map_finalize(bmap);
1781 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1782 __isl_take isl_basic_set *bset)
1784 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1785 (struct isl_basic_map *)bset);
1788 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1797 map = isl_map_cow(map);
1801 for (i = 0; i < map->n; ++i) {
1802 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1812 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1814 return isl_map_remove_divs(set);
1817 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1818 enum isl_dim_type type, unsigned first, unsigned n)
1822 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1824 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1826 bmap = isl_basic_map_eliminate_vars(bmap,
1827 isl_basic_map_offset(bmap, type) - 1 + first, n);
1830 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1832 bmap = isl_basic_map_drop(bmap, type, first, n);
1835 isl_basic_map_free(bmap);
1839 /* Return true if the definition of the given div (recursively) involves
1840 * any of the given variables.
1842 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1843 unsigned first, unsigned n)
1846 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1848 if (isl_int_is_zero(bmap->div[div][0]))
1850 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1853 for (i = bmap->n_div - 1; i >= 0; --i) {
1854 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1856 if (div_involves_vars(bmap, i, first, n))
1863 /* Try and add a lower and/or upper bound on "div" to "bmap"
1864 * based on inequality "i".
1865 * "total" is the total number of variables (excluding the divs).
1866 * "v" is a temporary object that can be used during the calculations.
1867 * If "lb" is set, then a lower bound should be constructed.
1868 * If "ub" is set, then an upper bound should be constructed.
1870 * The calling function has already checked that the inequality does not
1871 * reference "div", but we still need to check that the inequality is
1872 * of the right form. We'll consider the case where we want to construct
1873 * a lower bound. The construction of upper bounds is similar.
1875 * Let "div" be of the form
1877 * q = floor((a + f(x))/d)
1879 * We essentially check if constraint "i" is of the form
1883 * so that we can use it to derive a lower bound on "div".
1884 * However, we allow a slightly more general form
1888 * with the condition that the coefficients of g(x) - f(x) are all
1890 * Rewriting this constraint as
1894 * adding a + f(x) to both sides and dividing by d, we obtain
1896 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1898 * Taking the floor on both sides, we obtain
1900 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1904 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1906 * In the case of an upper bound, we construct the constraint
1908 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1911 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1912 __isl_take isl_basic_map *bmap, int div, int i,
1913 unsigned total, isl_int v, int lb, int ub)
1917 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1919 isl_int_sub(v, bmap->ineq[i][1 + j],
1920 bmap->div[div][1 + 1 + j]);
1921 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1924 isl_int_add(v, bmap->ineq[i][1 + j],
1925 bmap->div[div][1 + 1 + j]);
1926 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1932 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1934 int k = isl_basic_map_alloc_inequality(bmap);
1937 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1938 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1939 bmap->div[div][1 + j]);
1940 isl_int_cdiv_q(bmap->ineq[k][j],
1941 bmap->ineq[k][j], bmap->div[div][0]);
1943 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1946 int k = isl_basic_map_alloc_inequality(bmap);
1949 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1950 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1951 bmap->div[div][1 + j]);
1952 isl_int_fdiv_q(bmap->ineq[k][j],
1953 bmap->ineq[k][j], bmap->div[div][0]);
1955 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1960 isl_basic_map_free(bmap);
1964 /* This function is called right before "div" is eliminated from "bmap"
1965 * using Fourier-Motzkin.
1966 * Look through the constraints of "bmap" for constraints on the argument
1967 * of the integer division and use them to construct constraints on the
1968 * integer division itself. These constraints can then be combined
1969 * during the Fourier-Motzkin elimination.
1970 * Note that it is only useful to introduce lower bounds on "div"
1971 * if "bmap" already contains upper bounds on "div" as the newly
1972 * introduce lower bounds can then be combined with the pre-existing
1973 * upper bounds. Similarly for upper bounds.
1974 * We therefore first check if "bmap" contains any lower and/or upper bounds
1977 * It is interesting to note that the introduction of these constraints
1978 * can indeed lead to more accurate results, even when compared to
1979 * deriving constraints on the argument of "div" from constraints on "div".
1980 * Consider, for example, the set
1982 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1984 * The second constraint can be rewritten as
1986 * 2 * [(-i-2j+3)/4] + k >= 0
1988 * from which we can derive
1990 * -i - 2j + 3 >= -2k
1996 * Combined with the first constraint, we obtain
1998 * -3 <= 3 + 2k or k >= -3
2000 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2001 * the first constraint, we obtain
2003 * [(i + 2j)/4] >= [-3/4] = -1
2005 * Combining this constraint with the second constraint, we obtain
2009 static __isl_give isl_basic_map *insert_bounds_on_div(
2010 __isl_take isl_basic_map *bmap, int div)
2013 int check_lb, check_ub;
2020 if (isl_int_is_zero(bmap->div[div][0]))
2023 total = isl_space_dim(bmap->dim, isl_dim_all);
2027 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2028 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2035 if (!check_lb && !check_ub)
2040 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2041 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2044 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2045 check_lb, check_ub);
2053 /* Remove all divs (recursively) involving any of the given dimensions
2054 * in their definitions.
2056 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2057 __isl_take isl_basic_map *bmap,
2058 enum isl_dim_type type, unsigned first, unsigned n)
2064 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2066 first += isl_basic_map_offset(bmap, type);
2068 for (i = bmap->n_div - 1; i >= 0; --i) {
2069 if (!div_involves_vars(bmap, i, first, n))
2071 bmap = insert_bounds_on_div(bmap, i);
2072 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2080 isl_basic_map_free(bmap);
2084 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2085 __isl_take isl_basic_set *bset,
2086 enum isl_dim_type type, unsigned first, unsigned n)
2088 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2091 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2092 enum isl_dim_type type, unsigned first, unsigned n)
2101 map = isl_map_cow(map);
2105 for (i = 0; i < map->n; ++i) {
2106 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2117 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2118 enum isl_dim_type type, unsigned first, unsigned n)
2120 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2124 /* Does the desciption of "bmap" depend on the specified dimensions?
2125 * We also check whether the dimensions appear in any of the div definitions.
2126 * In principle there is no need for this check. If the dimensions appear
2127 * in a div definition, they also appear in the defining constraints of that
2130 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2131 enum isl_dim_type type, unsigned first, unsigned n)
2138 if (first + n > isl_basic_map_dim(bmap, type))
2139 isl_die(bmap->ctx, isl_error_invalid,
2140 "index out of bounds", return -1);
2142 first += isl_basic_map_offset(bmap, type);
2143 for (i = 0; i < bmap->n_eq; ++i)
2144 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2146 for (i = 0; i < bmap->n_ineq; ++i)
2147 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2149 for (i = 0; i < bmap->n_div; ++i) {
2150 if (isl_int_is_zero(bmap->div[i][0]))
2152 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2159 int isl_map_involves_dims(__isl_keep isl_map *map,
2160 enum isl_dim_type type, unsigned first, unsigned n)
2167 if (first + n > isl_map_dim(map, type))
2168 isl_die(map->ctx, isl_error_invalid,
2169 "index out of bounds", return -1);
2171 for (i = 0; i < map->n; ++i) {
2172 int involves = isl_basic_map_involves_dims(map->p[i],
2174 if (involves < 0 || involves)
2181 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2182 enum isl_dim_type type, unsigned first, unsigned n)
2184 return isl_basic_map_involves_dims(bset, type, first, n);
2187 int isl_set_involves_dims(__isl_keep isl_set *set,
2188 enum isl_dim_type type, unsigned first, unsigned n)
2190 return isl_map_involves_dims(set, type, first, n);
2193 /* Return true if the definition of the given div is unknown or depends
2196 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2199 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2201 if (isl_int_is_zero(bmap->div[div][0]))
2204 for (i = bmap->n_div - 1; i >= 0; --i) {
2205 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2207 if (div_is_unknown(bmap, i))
2214 /* Remove all divs that are unknown or defined in terms of unknown divs.
2216 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2217 __isl_take isl_basic_map *bmap)
2224 for (i = bmap->n_div - 1; i >= 0; --i) {
2225 if (!div_is_unknown(bmap, i))
2227 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2236 /* Remove all divs that are unknown or defined in terms of unknown divs.
2238 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2239 __isl_take isl_basic_set *bset)
2241 return isl_basic_map_remove_unknown_divs(bset);
2244 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2253 map = isl_map_cow(map);
2257 for (i = 0; i < map->n; ++i) {
2258 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2268 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2270 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2273 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2274 __isl_take isl_basic_set *bset,
2275 enum isl_dim_type type, unsigned first, unsigned n)
2277 return (isl_basic_set *)
2278 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2281 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2282 enum isl_dim_type type, unsigned first, unsigned n)
2289 map = isl_map_cow(map);
2292 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2294 for (i = 0; i < map->n; ++i) {
2295 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2296 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2300 map = isl_map_drop(map, type, first, n);
2307 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2308 enum isl_dim_type type, unsigned first, unsigned n)
2310 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2313 /* Project out n inputs starting at first using Fourier-Motzkin */
2314 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2315 unsigned first, unsigned n)
2317 return isl_map_remove_dims(map, isl_dim_in, first, n);
2320 static void dump_term(struct isl_basic_map *bmap,
2321 isl_int c, int pos, FILE *out)
2324 unsigned in = isl_basic_map_n_in(bmap);
2325 unsigned dim = in + isl_basic_map_n_out(bmap);
2326 unsigned nparam = isl_basic_map_n_param(bmap);
2328 isl_int_print(out, c, 0);
2330 if (!isl_int_is_one(c))
2331 isl_int_print(out, c, 0);
2332 if (pos < 1 + nparam) {
2333 name = isl_space_get_dim_name(bmap->dim,
2334 isl_dim_param, pos - 1);
2336 fprintf(out, "%s", name);
2338 fprintf(out, "p%d", pos - 1);
2339 } else if (pos < 1 + nparam + in)
2340 fprintf(out, "i%d", pos - 1 - nparam);
2341 else if (pos < 1 + nparam + dim)
2342 fprintf(out, "o%d", pos - 1 - nparam - in);
2344 fprintf(out, "e%d", pos - 1 - nparam - dim);
2348 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2349 int sign, FILE *out)
2353 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2357 for (i = 0, first = 1; i < len; ++i) {
2358 if (isl_int_sgn(c[i]) * sign <= 0)
2361 fprintf(out, " + ");
2363 isl_int_abs(v, c[i]);
2364 dump_term(bmap, v, i, out);
2371 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2372 const char *op, FILE *out, int indent)
2376 fprintf(out, "%*s", indent, "");
2378 dump_constraint_sign(bmap, c, 1, out);
2379 fprintf(out, " %s ", op);
2380 dump_constraint_sign(bmap, c, -1, out);
2384 for (i = bmap->n_div; i < bmap->extra; ++i) {
2385 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2387 fprintf(out, "%*s", indent, "");
2388 fprintf(out, "ERROR: unused div coefficient not zero\n");
2393 static void dump_constraints(struct isl_basic_map *bmap,
2394 isl_int **c, unsigned n,
2395 const char *op, FILE *out, int indent)
2399 for (i = 0; i < n; ++i)
2400 dump_constraint(bmap, c[i], op, out, indent);
2403 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2407 unsigned total = isl_basic_map_total_dim(bmap);
2409 for (j = 0; j < 1 + total; ++j) {
2410 if (isl_int_is_zero(exp[j]))
2412 if (!first && isl_int_is_pos(exp[j]))
2414 dump_term(bmap, exp[j], j, out);
2419 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2423 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2424 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2426 for (i = 0; i < bmap->n_div; ++i) {
2427 fprintf(out, "%*s", indent, "");
2428 fprintf(out, "e%d = [(", i);
2429 dump_affine(bmap, bmap->div[i]+1, out);
2431 isl_int_print(out, bmap->div[i][0], 0);
2432 fprintf(out, "]\n");
2436 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2437 FILE *out, int indent)
2440 fprintf(out, "null basic set\n");
2444 fprintf(out, "%*s", indent, "");
2445 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2446 bset->ref, bset->dim->nparam, bset->dim->n_out,
2447 bset->extra, bset->flags);
2448 dump((struct isl_basic_map *)bset, out, indent);
2451 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2452 FILE *out, int indent)
2455 fprintf(out, "null basic map\n");
2459 fprintf(out, "%*s", indent, "");
2460 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2461 "flags: %x, n_name: %d\n",
2463 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2464 bmap->extra, bmap->flags, bmap->dim->n_id);
2465 dump(bmap, out, indent);
2468 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2473 total = isl_basic_map_total_dim(bmap);
2474 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2475 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2476 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2481 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2484 struct isl_set *set;
2488 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2489 isl_assert(dim->ctx, n >= 0, goto error);
2490 set = isl_alloc(dim->ctx, struct isl_set,
2491 sizeof(struct isl_set) +
2492 (n - 1) * sizeof(struct isl_basic_set *));
2496 set->ctx = dim->ctx;
2497 isl_ctx_ref(set->ctx);
2505 isl_space_free(dim);
2509 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2510 unsigned nparam, unsigned dim, int n, unsigned flags)
2512 struct isl_set *set;
2515 dims = isl_space_alloc(ctx, nparam, 0, dim);
2519 set = isl_set_alloc_space(dims, n, flags);
2523 /* Make sure "map" has room for at least "n" more basic maps.
2525 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2528 struct isl_map *grown = NULL;
2532 isl_assert(map->ctx, n >= 0, goto error);
2533 if (map->n + n <= map->size)
2535 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2538 for (i = 0; i < map->n; ++i) {
2539 grown->p[i] = isl_basic_map_copy(map->p[i]);
2547 isl_map_free(grown);
2552 /* Make sure "set" has room for at least "n" more basic sets.
2554 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2556 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2559 struct isl_set *isl_set_dup(struct isl_set *set)
2562 struct isl_set *dup;
2567 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2570 for (i = 0; i < set->n; ++i)
2571 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2575 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2577 return isl_map_from_basic_map(bset);
2580 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2582 struct isl_map *map;
2587 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2588 return isl_map_add_basic_map(map, bmap);
2591 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2592 __isl_take isl_basic_set *bset)
2594 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2595 (struct isl_basic_map *)bset);
2598 void *isl_set_free(__isl_take isl_set *set)
2608 isl_ctx_deref(set->ctx);
2609 for (i = 0; i < set->n; ++i)
2610 isl_basic_set_free(set->p[i]);
2611 isl_space_free(set->dim);
2617 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2622 fprintf(out, "null set\n");
2626 fprintf(out, "%*s", indent, "");
2627 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2628 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2630 for (i = 0; i < set->n; ++i) {
2631 fprintf(out, "%*s", indent, "");
2632 fprintf(out, "basic set %d:\n", i);
2633 isl_basic_set_print_internal(set->p[i], out, indent+4);
2637 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2642 fprintf(out, "null map\n");
2646 fprintf(out, "%*s", indent, "");
2647 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2648 "flags: %x, n_name: %d\n",
2649 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2650 map->dim->n_out, map->flags, map->dim->n_id);
2651 for (i = 0; i < map->n; ++i) {
2652 fprintf(out, "%*s", indent, "");
2653 fprintf(out, "basic map %d:\n", i);
2654 isl_basic_map_print_internal(map->p[i], out, indent+4);
2658 struct isl_basic_map *isl_basic_map_intersect_domain(
2659 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2661 struct isl_basic_map *bmap_domain;
2666 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2667 bset->dim, isl_dim_param), goto error);
2669 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2670 isl_assert(bset->ctx,
2671 isl_basic_map_compatible_domain(bmap, bset), goto error);
2673 bmap = isl_basic_map_cow(bmap);
2676 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2677 bset->n_div, bset->n_eq, bset->n_ineq);
2678 bmap_domain = isl_basic_map_from_domain(bset);
2679 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2681 bmap = isl_basic_map_simplify(bmap);
2682 return isl_basic_map_finalize(bmap);
2684 isl_basic_map_free(bmap);
2685 isl_basic_set_free(bset);
2689 struct isl_basic_map *isl_basic_map_intersect_range(
2690 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2692 struct isl_basic_map *bmap_range;
2697 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2698 bset->dim, isl_dim_param), goto error);
2700 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2701 isl_assert(bset->ctx,
2702 isl_basic_map_compatible_range(bmap, bset), goto error);
2704 if (isl_basic_set_is_universe(bset)) {
2705 isl_basic_set_free(bset);
2709 bmap = isl_basic_map_cow(bmap);
2712 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2713 bset->n_div, bset->n_eq, bset->n_ineq);
2714 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2715 bmap = add_constraints(bmap, bmap_range, 0, 0);
2717 bmap = isl_basic_map_simplify(bmap);
2718 return isl_basic_map_finalize(bmap);
2720 isl_basic_map_free(bmap);
2721 isl_basic_set_free(bset);
2725 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2731 total = 1 + isl_basic_map_total_dim(bmap);
2732 if (total != vec->size)
2737 for (i = 0; i < bmap->n_eq; ++i) {
2738 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2739 if (!isl_int_is_zero(s)) {
2745 for (i = 0; i < bmap->n_ineq; ++i) {
2746 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2747 if (isl_int_is_neg(s)) {
2758 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2760 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2763 struct isl_basic_map *isl_basic_map_intersect(
2764 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2766 struct isl_vec *sample = NULL;
2768 if (!bmap1 || !bmap2)
2771 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2772 bmap2->dim, isl_dim_param), goto error);
2773 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2774 isl_space_dim(bmap1->dim, isl_dim_param) &&
2775 isl_space_dim(bmap2->dim, isl_dim_all) !=
2776 isl_space_dim(bmap2->dim, isl_dim_param))
2777 return isl_basic_map_intersect(bmap2, bmap1);
2779 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2780 isl_space_dim(bmap2->dim, isl_dim_param))
2781 isl_assert(bmap1->ctx,
2782 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2784 if (bmap1->sample &&
2785 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2786 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2787 sample = isl_vec_copy(bmap1->sample);
2788 else if (bmap2->sample &&
2789 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2790 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2791 sample = isl_vec_copy(bmap2->sample);
2793 bmap1 = isl_basic_map_cow(bmap1);
2796 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2797 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2798 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2801 isl_vec_free(sample);
2803 isl_vec_free(bmap1->sample);
2804 bmap1->sample = sample;
2807 bmap1 = isl_basic_map_simplify(bmap1);
2808 return isl_basic_map_finalize(bmap1);
2811 isl_vec_free(sample);
2812 isl_basic_map_free(bmap1);
2813 isl_basic_map_free(bmap2);
2817 struct isl_basic_set *isl_basic_set_intersect(
2818 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2820 return (struct isl_basic_set *)
2821 isl_basic_map_intersect(
2822 (struct isl_basic_map *)bset1,
2823 (struct isl_basic_map *)bset2);
2826 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2827 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2829 return isl_basic_set_intersect(bset1, bset2);
2832 /* Special case of isl_map_intersect, where both map1 and map2
2833 * are convex, without any divs and such that either map1 or map2
2834 * contains a single constraint. This constraint is then simply
2835 * added to the other map.
2837 static __isl_give isl_map *map_intersect_add_constraint(
2838 __isl_take isl_map *map1, __isl_take isl_map *map2)
2840 isl_assert(map1->ctx, map1->n == 1, goto error);
2841 isl_assert(map2->ctx, map1->n == 1, goto error);
2842 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2843 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2845 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2846 return isl_map_intersect(map2, map1);
2848 isl_assert(map2->ctx,
2849 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2851 map1 = isl_map_cow(map1);
2854 if (isl_map_plain_is_empty(map1)) {
2858 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2859 if (map2->p[0]->n_eq == 1)
2860 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2862 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2863 map2->p[0]->ineq[0]);
2865 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2866 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2870 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2871 isl_basic_map_free(map1->p[0]);
2884 /* map2 may be either a parameter domain or a map living in the same
2887 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2888 __isl_take isl_map *map2)
2897 if ((isl_map_plain_is_empty(map1) ||
2898 isl_map_plain_is_universe(map2)) &&
2899 isl_space_is_equal(map1->dim, map2->dim)) {
2903 if ((isl_map_plain_is_empty(map2) ||
2904 isl_map_plain_is_universe(map1)) &&
2905 isl_space_is_equal(map1->dim, map2->dim)) {
2910 if (map1->n == 1 && map2->n == 1 &&
2911 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2912 isl_space_is_equal(map1->dim, map2->dim) &&
2913 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2914 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2915 return map_intersect_add_constraint(map1, map2);
2917 if (isl_space_dim(map2->dim, isl_dim_all) !=
2918 isl_space_dim(map2->dim, isl_dim_param))
2919 isl_assert(map1->ctx,
2920 isl_space_is_equal(map1->dim, map2->dim), goto error);
2922 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2923 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2924 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2926 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2927 map1->n * map2->n, flags);
2930 for (i = 0; i < map1->n; ++i)
2931 for (j = 0; j < map2->n; ++j) {
2932 struct isl_basic_map *part;
2933 part = isl_basic_map_intersect(
2934 isl_basic_map_copy(map1->p[i]),
2935 isl_basic_map_copy(map2->p[j]));
2936 if (isl_basic_map_is_empty(part) < 0)
2937 part = isl_basic_map_free(part);
2938 result = isl_map_add_basic_map(result, part);
2951 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2952 __isl_take isl_map *map2)
2956 if (!isl_space_is_equal(map1->dim, map2->dim))
2957 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2958 "spaces don't match", goto error);
2959 return map_intersect_internal(map1, map2);
2966 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2967 __isl_take isl_map *map2)
2969 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2972 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2974 return (struct isl_set *)
2975 isl_map_intersect((struct isl_map *)set1,
2976 (struct isl_map *)set2);
2979 /* map_intersect_internal accepts intersections
2980 * with parameter domains, so we can just call that function.
2982 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2983 __isl_take isl_set *params)
2985 return map_intersect_internal(map, params);
2988 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2989 __isl_take isl_map *map2)
2991 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2994 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2995 __isl_take isl_set *params)
2997 return isl_map_intersect_params(set, params);
3000 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3003 struct isl_basic_set *bset;
3008 bmap = isl_basic_map_cow(bmap);
3011 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3012 in = isl_basic_map_n_in(bmap);
3013 bset = isl_basic_set_from_basic_map(bmap);
3014 bset = isl_basic_set_swap_vars(bset, in);
3015 return isl_basic_map_from_basic_set(bset, dim);
3018 static __isl_give isl_basic_map *basic_map_space_reset(
3019 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3025 if (!isl_space_is_named_or_nested(bmap->dim, type))
3028 space = isl_basic_map_get_space(bmap);
3029 space = isl_space_reset(space, type);
3030 bmap = isl_basic_map_reset_space(bmap, space);
3034 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3035 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3036 unsigned pos, unsigned n)
3039 struct isl_basic_map *res;
3040 struct isl_dim_map *dim_map;
3041 unsigned total, off;
3042 enum isl_dim_type t;
3045 return basic_map_space_reset(bmap, type);
3050 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3052 total = isl_basic_map_total_dim(bmap) + n;
3053 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3055 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3057 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3059 unsigned size = isl_basic_map_dim(bmap, t);
3060 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3062 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3063 pos, size - pos, off + pos + n);
3065 off += isl_space_dim(res_dim, t);
3067 isl_dim_map_div(dim_map, bmap, off);
3069 res = isl_basic_map_alloc_space(res_dim,
3070 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3071 if (isl_basic_map_is_rational(bmap))
3072 res = isl_basic_map_set_rational(res);
3073 if (isl_basic_map_plain_is_empty(bmap)) {
3074 isl_basic_map_free(bmap);
3076 return isl_basic_map_set_to_empty(res);
3078 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3079 return isl_basic_map_finalize(res);
3082 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3083 __isl_take isl_basic_set *bset,
3084 enum isl_dim_type type, unsigned pos, unsigned n)
3086 return isl_basic_map_insert_dims(bset, type, pos, n);
3089 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3090 enum isl_dim_type type, unsigned n)
3094 return isl_basic_map_insert_dims(bmap, type,
3095 isl_basic_map_dim(bmap, type), n);
3098 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3099 enum isl_dim_type type, unsigned n)
3103 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3104 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3106 isl_basic_set_free(bset);
3110 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3111 enum isl_dim_type type)
3115 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3118 space = isl_map_get_space(map);
3119 space = isl_space_reset(space, type);
3120 map = isl_map_reset_space(map, space);
3124 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3125 enum isl_dim_type type, unsigned pos, unsigned n)
3130 return map_space_reset(map, type);
3132 map = isl_map_cow(map);
3136 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3140 for (i = 0; i < map->n; ++i) {
3141 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3152 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3153 enum isl_dim_type type, unsigned pos, unsigned n)
3155 return isl_map_insert_dims(set, type, pos, n);
3158 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3159 enum isl_dim_type type, unsigned n)
3163 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3166 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3167 enum isl_dim_type type, unsigned n)
3171 isl_assert(set->ctx, type != isl_dim_in, goto error);
3172 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3178 __isl_give isl_basic_map *isl_basic_map_move_dims(
3179 __isl_take isl_basic_map *bmap,
3180 enum isl_dim_type dst_type, unsigned dst_pos,
3181 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3183 struct isl_dim_map *dim_map;
3184 struct isl_basic_map *res;
3185 enum isl_dim_type t;
3186 unsigned total, off;
3193 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3196 if (dst_type == src_type && dst_pos == src_pos)
3199 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3201 if (pos(bmap->dim, dst_type) + dst_pos ==
3202 pos(bmap->dim, src_type) + src_pos +
3203 ((src_type < dst_type) ? n : 0)) {
3204 bmap = isl_basic_map_cow(bmap);
3208 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3209 src_type, src_pos, n);
3213 bmap = isl_basic_map_finalize(bmap);
3218 total = isl_basic_map_total_dim(bmap);
3219 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3222 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3223 unsigned size = isl_space_dim(bmap->dim, t);
3224 if (t == dst_type) {
3225 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3228 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3231 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3232 dst_pos, size - dst_pos, off);
3233 off += size - dst_pos;
3234 } else if (t == src_type) {
3235 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3238 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3239 src_pos + n, size - src_pos - n, off);
3240 off += size - src_pos - n;
3242 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3246 isl_dim_map_div(dim_map, bmap, off);
3248 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3249 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3250 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3254 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3255 src_type, src_pos, n);
3259 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3260 bmap = isl_basic_map_gauss(bmap, NULL);
3261 bmap = isl_basic_map_finalize(bmap);
3265 isl_basic_map_free(bmap);
3269 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3270 enum isl_dim_type dst_type, unsigned dst_pos,
3271 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3273 return (isl_basic_set *)isl_basic_map_move_dims(
3274 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3277 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3278 enum isl_dim_type dst_type, unsigned dst_pos,
3279 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3283 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3284 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3285 src_type, src_pos, n);
3291 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3292 enum isl_dim_type dst_type, unsigned dst_pos,
3293 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3302 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3305 if (dst_type == src_type && dst_pos == src_pos)
3308 isl_assert(map->ctx, dst_type != src_type, goto error);
3310 map = isl_map_cow(map);
3314 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3318 for (i = 0; i < map->n; ++i) {
3319 map->p[i] = isl_basic_map_move_dims(map->p[i],
3321 src_type, src_pos, n);
3332 /* Move the specified dimensions to the last columns right before
3333 * the divs. Don't change the dimension specification of bmap.
3334 * That's the responsibility of the caller.
3336 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3337 enum isl_dim_type type, unsigned first, unsigned n)
3339 struct isl_dim_map *dim_map;
3340 struct isl_basic_map *res;
3341 enum isl_dim_type t;
3342 unsigned total, off;
3346 if (pos(bmap->dim, type) + first + n ==
3347 1 + isl_space_dim(bmap->dim, isl_dim_all))
3350 total = isl_basic_map_total_dim(bmap);
3351 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3354 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3355 unsigned size = isl_space_dim(bmap->dim, t);
3357 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3360 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3361 first, n, total - bmap->n_div - n);
3362 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3363 first + n, size - (first + n), off);
3364 off += size - (first + n);
3366 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3370 isl_dim_map_div(dim_map, bmap, off + n);
3372 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3373 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3374 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3378 /* Insert "n" rows in the divs of "bmap".
3380 * The number of columns is not changed, which means that the last
3381 * dimensions of "bmap" are being reintepreted as the new divs.
3382 * The space of "bmap" is not adjusted, however, which means
3383 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3384 * from the space of "bmap" is the responsibility of the caller.
3386 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3394 bmap = isl_basic_map_cow(bmap);
3398 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3399 old = bmap->block2.data;
3400 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3401 (bmap->extra + n) * (1 + row_size));
3402 if (!bmap->block2.data)
3403 return isl_basic_map_free(bmap);
3404 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3406 return isl_basic_map_free(bmap);
3407 for (i = 0; i < n; ++i) {
3408 new_div[i] = bmap->block2.data +
3409 (bmap->extra + i) * (1 + row_size);
3410 isl_seq_clr(new_div[i], 1 + row_size);
3412 for (i = 0; i < bmap->extra; ++i)
3413 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3415 bmap->div = new_div;
3422 /* Turn the n dimensions of type type, starting at first
3423 * into existentially quantified variables.
3425 __isl_give isl_basic_map *isl_basic_map_project_out(
3426 __isl_take isl_basic_map *bmap,
3427 enum isl_dim_type type, unsigned first, unsigned n)
3430 return basic_map_space_reset(bmap, type);
3435 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3436 return isl_basic_map_remove_dims(bmap, type, first, n);
3438 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3441 bmap = move_last(bmap, type, first, n);
3442 bmap = isl_basic_map_cow(bmap);
3443 bmap = insert_div_rows(bmap, n);
3447 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3450 bmap = isl_basic_map_simplify(bmap);
3451 bmap = isl_basic_map_drop_redundant_divs(bmap);
3452 return isl_basic_map_finalize(bmap);
3454 isl_basic_map_free(bmap);
3458 /* Turn the n dimensions of type type, starting at first
3459 * into existentially quantified variables.
3461 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3462 enum isl_dim_type type, unsigned first, unsigned n)
3464 return (isl_basic_set *)isl_basic_map_project_out(
3465 (isl_basic_map *)bset, type, first, n);
3468 /* Turn the n dimensions of type type, starting at first
3469 * into existentially quantified variables.
3471 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3472 enum isl_dim_type type, unsigned first, unsigned n)
3480 return map_space_reset(map, type);
3482 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3484 map = isl_map_cow(map);
3488 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3492 for (i = 0; i < map->n; ++i) {
3493 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3504 /* Turn the n dimensions of type type, starting at first
3505 * into existentially quantified variables.
3507 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3508 enum isl_dim_type type, unsigned first, unsigned n)
3510 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3513 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3517 for (i = 0; i < n; ++i) {
3518 j = isl_basic_map_alloc_div(bmap);
3521 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3525 isl_basic_map_free(bmap);
3529 struct isl_basic_map *isl_basic_map_apply_range(
3530 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3532 isl_space *dim_result = NULL;
3533 struct isl_basic_map *bmap;
3534 unsigned n_in, n_out, n, nparam, total, pos;
3535 struct isl_dim_map *dim_map1, *dim_map2;
3537 if (!bmap1 || !bmap2)
3540 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3541 isl_space_copy(bmap2->dim));
3543 n_in = isl_basic_map_n_in(bmap1);
3544 n_out = isl_basic_map_n_out(bmap2);
3545 n = isl_basic_map_n_out(bmap1);
3546 nparam = isl_basic_map_n_param(bmap1);
3548 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3549 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3550 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3551 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3552 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3553 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3554 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3555 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3556 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3557 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3558 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3560 bmap = isl_basic_map_alloc_space(dim_result,
3561 bmap1->n_div + bmap2->n_div + n,
3562 bmap1->n_eq + bmap2->n_eq,
3563 bmap1->n_ineq + bmap2->n_ineq);
3564 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3565 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3566 bmap = add_divs(bmap, n);
3567 bmap = isl_basic_map_simplify(bmap);
3568 bmap = isl_basic_map_drop_redundant_divs(bmap);
3569 return isl_basic_map_finalize(bmap);
3571 isl_basic_map_free(bmap1);
3572 isl_basic_map_free(bmap2);
3576 struct isl_basic_set *isl_basic_set_apply(
3577 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3582 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3585 return (struct isl_basic_set *)
3586 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3588 isl_basic_set_free(bset);
3589 isl_basic_map_free(bmap);
3593 struct isl_basic_map *isl_basic_map_apply_domain(
3594 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3596 if (!bmap1 || !bmap2)
3599 isl_assert(bmap1->ctx,
3600 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3601 isl_assert(bmap1->ctx,
3602 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3605 bmap1 = isl_basic_map_reverse(bmap1);
3606 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3607 return isl_basic_map_reverse(bmap1);
3609 isl_basic_map_free(bmap1);
3610 isl_basic_map_free(bmap2);
3614 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3615 * A \cap B -> f(A) + f(B)
3617 struct isl_basic_map *isl_basic_map_sum(
3618 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3620 unsigned n_in, n_out, nparam, total, pos;
3621 struct isl_basic_map *bmap = NULL;
3622 struct isl_dim_map *dim_map1, *dim_map2;
3625 if (!bmap1 || !bmap2)
3628 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3631 nparam = isl_basic_map_n_param(bmap1);
3632 n_in = isl_basic_map_n_in(bmap1);
3633 n_out = isl_basic_map_n_out(bmap1);
3635 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3636 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3637 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3638 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3639 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3640 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3641 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3642 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3643 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3644 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3645 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3647 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3648 bmap1->n_div + bmap2->n_div + 2 * n_out,
3649 bmap1->n_eq + bmap2->n_eq + n_out,
3650 bmap1->n_ineq + bmap2->n_ineq);
3651 for (i = 0; i < n_out; ++i) {
3652 int j = isl_basic_map_alloc_equality(bmap);
3655 isl_seq_clr(bmap->eq[j], 1+total);
3656 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3657 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3658 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3660 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3661 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3662 bmap = add_divs(bmap, 2 * n_out);
3664 bmap = isl_basic_map_simplify(bmap);
3665 return isl_basic_map_finalize(bmap);
3667 isl_basic_map_free(bmap);
3668 isl_basic_map_free(bmap1);
3669 isl_basic_map_free(bmap2);
3673 /* Given two maps A -> f(A) and B -> g(B), construct a map
3674 * A \cap B -> f(A) + f(B)
3676 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3678 struct isl_map *result;
3684 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3686 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3687 map1->n * map2->n, 0);
3690 for (i = 0; i < map1->n; ++i)
3691 for (j = 0; j < map2->n; ++j) {
3692 struct isl_basic_map *part;
3693 part = isl_basic_map_sum(
3694 isl_basic_map_copy(map1->p[i]),
3695 isl_basic_map_copy(map2->p[j]));
3696 if (isl_basic_map_is_empty(part))
3697 isl_basic_map_free(part);
3699 result = isl_map_add_basic_map(result, part);
3712 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3713 __isl_take isl_set *set2)
3715 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3718 /* Given a basic map A -> f(A), construct A -> -f(A).
3720 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3725 bmap = isl_basic_map_cow(bmap);
3729 n = isl_basic_map_dim(bmap, isl_dim_out);
3730 off = isl_basic_map_offset(bmap, isl_dim_out);
3731 for (i = 0; i < bmap->n_eq; ++i)
3732 for (j = 0; j < n; ++j)
3733 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3734 for (i = 0; i < bmap->n_ineq; ++i)
3735 for (j = 0; j < n; ++j)
3736 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3737 for (i = 0; i < bmap->n_div; ++i)
3738 for (j = 0; j < n; ++j)
3739 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3740 bmap = isl_basic_map_gauss(bmap, NULL);
3741 return isl_basic_map_finalize(bmap);
3744 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3746 return isl_basic_map_neg(bset);
3749 /* Given a map A -> f(A), construct A -> -f(A).
3751 struct isl_map *isl_map_neg(struct isl_map *map)
3755 map = isl_map_cow(map);
3759 for (i = 0; i < map->n; ++i) {
3760 map->p[i] = isl_basic_map_neg(map->p[i]);
3771 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3773 return (isl_set *)isl_map_neg((isl_map *)set);
3776 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3777 * A -> floor(f(A)/d).
3779 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3782 unsigned n_in, n_out, nparam, total, pos;
3783 struct isl_basic_map *result = NULL;
3784 struct isl_dim_map *dim_map;
3790 nparam = isl_basic_map_n_param(bmap);
3791 n_in = isl_basic_map_n_in(bmap);
3792 n_out = isl_basic_map_n_out(bmap);
3794 total = nparam + n_in + n_out + bmap->n_div + n_out;
3795 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3796 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3797 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3798 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3799 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3801 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3802 bmap->n_div + n_out,
3803 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3804 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3805 result = add_divs(result, n_out);
3806 for (i = 0; i < n_out; ++i) {
3808 j = isl_basic_map_alloc_inequality(result);
3811 isl_seq_clr(result->ineq[j], 1+total);
3812 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3813 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3814 j = isl_basic_map_alloc_inequality(result);
3817 isl_seq_clr(result->ineq[j], 1+total);
3818 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3819 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3820 isl_int_sub_ui(result->ineq[j][0], d, 1);
3823 result = isl_basic_map_simplify(result);
3824 return isl_basic_map_finalize(result);
3826 isl_basic_map_free(result);
3830 /* Given a map A -> f(A) and an integer d, construct a map
3831 * A -> floor(f(A)/d).
3833 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3837 map = isl_map_cow(map);
3841 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3842 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3843 for (i = 0; i < map->n; ++i) {
3844 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3855 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3861 i = isl_basic_map_alloc_equality(bmap);
3864 nparam = isl_basic_map_n_param(bmap);
3865 n_in = isl_basic_map_n_in(bmap);
3866 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3867 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3868 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3869 return isl_basic_map_finalize(bmap);
3871 isl_basic_map_free(bmap);
3875 /* Add a constraints to "bmap" expressing i_pos < o_pos
3877 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3883 i = isl_basic_map_alloc_inequality(bmap);
3886 nparam = isl_basic_map_n_param(bmap);
3887 n_in = isl_basic_map_n_in(bmap);
3888 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3889 isl_int_set_si(bmap->ineq[i][0], -1);
3890 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3891 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3892 return isl_basic_map_finalize(bmap);
3894 isl_basic_map_free(bmap);
3898 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3900 static __isl_give isl_basic_map *var_less_or_equal(
3901 __isl_take isl_basic_map *bmap, unsigned pos)
3907 i = isl_basic_map_alloc_inequality(bmap);
3910 nparam = isl_basic_map_n_param(bmap);
3911 n_in = isl_basic_map_n_in(bmap);
3912 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3913 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3914 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3915 return isl_basic_map_finalize(bmap);
3917 isl_basic_map_free(bmap);
3921 /* Add a constraints to "bmap" expressing i_pos > o_pos
3923 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3929 i = isl_basic_map_alloc_inequality(bmap);
3932 nparam = isl_basic_map_n_param(bmap);
3933 n_in = isl_basic_map_n_in(bmap);
3934 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3935 isl_int_set_si(bmap->ineq[i][0], -1);
3936 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3937 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3938 return isl_basic_map_finalize(bmap);
3940 isl_basic_map_free(bmap);
3944 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3946 static __isl_give isl_basic_map *var_more_or_equal(
3947 __isl_take isl_basic_map *bmap, unsigned pos)
3953 i = isl_basic_map_alloc_inequality(bmap);
3956 nparam = isl_basic_map_n_param(bmap);
3957 n_in = isl_basic_map_n_in(bmap);
3958 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3959 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3960 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3961 return isl_basic_map_finalize(bmap);
3963 isl_basic_map_free(bmap);
3967 __isl_give isl_basic_map *isl_basic_map_equal(
3968 __isl_take isl_space *dim, unsigned n_equal)
3971 struct isl_basic_map *bmap;
3972 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3975 for (i = 0; i < n_equal && bmap; ++i)
3976 bmap = var_equal(bmap, i);
3977 return isl_basic_map_finalize(bmap);
3980 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3982 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3986 struct isl_basic_map *bmap;
3987 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3990 for (i = 0; i < pos && bmap; ++i)
3991 bmap = var_equal(bmap, i);
3993 bmap = var_less(bmap, pos);
3994 return isl_basic_map_finalize(bmap);
3997 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3999 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4000 __isl_take isl_space *dim, unsigned pos)
4003 isl_basic_map *bmap;
4005 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4006 for (i = 0; i < pos; ++i)
4007 bmap = var_equal(bmap, i);
4008 bmap = var_less_or_equal(bmap, pos);
4009 return isl_basic_map_finalize(bmap);
4012 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4014 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4018 struct isl_basic_map *bmap;
4019 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4022 for (i = 0; i < pos && bmap; ++i)
4023 bmap = var_equal(bmap, i);
4025 bmap = var_more(bmap, pos);
4026 return isl_basic_map_finalize(bmap);
4029 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4031 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4032 __isl_take isl_space *dim, unsigned pos)
4035 isl_basic_map *bmap;
4037 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4038 for (i = 0; i < pos; ++i)
4039 bmap = var_equal(bmap, i);
4040 bmap = var_more_or_equal(bmap, pos);
4041 return isl_basic_map_finalize(bmap);
4044 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4045 unsigned n, int equal)
4047 struct isl_map *map;
4050 if (n == 0 && equal)
4051 return isl_map_universe(dims);
4053 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4055 for (i = 0; i + 1 < n; ++i)
4056 map = isl_map_add_basic_map(map,
4057 isl_basic_map_less_at(isl_space_copy(dims), i));
4060 map = isl_map_add_basic_map(map,
4061 isl_basic_map_less_or_equal_at(dims, n - 1));
4063 map = isl_map_add_basic_map(map,
4064 isl_basic_map_less_at(dims, n - 1));
4066 isl_space_free(dims);
4071 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4075 return map_lex_lte_first(dims, dims->n_out, equal);
4078 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4080 return map_lex_lte_first(dim, n, 0);
4083 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4085 return map_lex_lte_first(dim, n, 1);
4088 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4090 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4093 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4095 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4098 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4099 unsigned n, int equal)
4101 struct isl_map *map;
4104 if (n == 0 && equal)
4105 return isl_map_universe(dims);
4107 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4109 for (i = 0; i + 1 < n; ++i)
4110 map = isl_map_add_basic_map(map,
4111 isl_basic_map_more_at(isl_space_copy(dims), i));
4114 map = isl_map_add_basic_map(map,
4115 isl_basic_map_more_or_equal_at(dims, n - 1));
4117 map = isl_map_add_basic_map(map,
4118 isl_basic_map_more_at(dims, n - 1));
4120 isl_space_free(dims);
4125 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4129 return map_lex_gte_first(dims, dims->n_out, equal);
4132 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4134 return map_lex_gte_first(dim, n, 0);
4137 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4139 return map_lex_gte_first(dim, n, 1);
4142 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4144 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4147 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4149 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4152 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4153 __isl_take isl_set *set2)
4156 map = isl_map_lex_le(isl_set_get_space(set1));
4157 map = isl_map_intersect_domain(map, set1);
4158 map = isl_map_intersect_range(map, set2);
4162 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4163 __isl_take isl_set *set2)
4166 map = isl_map_lex_lt(isl_set_get_space(set1));
4167 map = isl_map_intersect_domain(map, set1);
4168 map = isl_map_intersect_range(map, set2);
4172 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4173 __isl_take isl_set *set2)
4176 map = isl_map_lex_ge(isl_set_get_space(set1));
4177 map = isl_map_intersect_domain(map, set1);
4178 map = isl_map_intersect_range(map, set2);
4182 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4183 __isl_take isl_set *set2)
4186 map = isl_map_lex_gt(isl_set_get_space(set1));
4187 map = isl_map_intersect_domain(map, set1);
4188 map = isl_map_intersect_range(map, set2);
4192 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4193 __isl_take isl_map *map2)
4196 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4197 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4198 map = isl_map_apply_range(map, isl_map_reverse(map2));
4202 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4203 __isl_take isl_map *map2)
4206 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4207 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4208 map = isl_map_apply_range(map, isl_map_reverse(map2));
4212 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4213 __isl_take isl_map *map2)
4216 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4217 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4218 map = isl_map_apply_range(map, isl_map_reverse(map2));
4222 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4223 __isl_take isl_map *map2)
4226 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4227 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4228 map = isl_map_apply_range(map, isl_map_reverse(map2));
4232 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4233 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4235 struct isl_basic_map *bmap;
4237 bset = isl_basic_set_cow(bset);
4241 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4242 isl_space_free(bset->dim);
4243 bmap = (struct isl_basic_map *) bset;
4245 return isl_basic_map_finalize(bmap);
4247 isl_basic_set_free(bset);
4248 isl_space_free(dim);
4252 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4256 if (bmap->dim->n_in == 0)
4257 return (struct isl_basic_set *)bmap;
4258 bmap = isl_basic_map_cow(bmap);
4261 bmap->dim = isl_space_as_set_space(bmap->dim);
4264 bmap = isl_basic_map_finalize(bmap);
4265 return (struct isl_basic_set *)bmap;
4267 isl_basic_map_free(bmap);
4271 /* For a div d = floor(f/m), add the constraints
4274 * -(f-(n-1)) + m d >= 0
4276 * Note that the second constraint is the negation of
4280 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4281 unsigned pos, isl_int *div)
4284 unsigned total = isl_basic_map_total_dim(bmap);
4286 i = isl_basic_map_alloc_inequality(bmap);
4289 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4290 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4292 j = isl_basic_map_alloc_inequality(bmap);
4295 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4296 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4297 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4301 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4302 unsigned pos, isl_int *div)
4304 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4308 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4310 unsigned total = isl_basic_map_total_dim(bmap);
4311 unsigned div_pos = total - bmap->n_div + div;
4313 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4317 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4319 return isl_basic_map_add_div_constraints(bset, div);
4322 struct isl_basic_set *isl_basic_map_underlying_set(
4323 struct isl_basic_map *bmap)
4327 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4329 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4330 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4331 return (struct isl_basic_set *)bmap;
4332 bmap = isl_basic_map_cow(bmap);
4335 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4338 bmap->extra -= bmap->n_div;
4340 bmap = isl_basic_map_finalize(bmap);
4341 return (struct isl_basic_set *)bmap;
4343 isl_basic_map_free(bmap);
4347 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4348 __isl_take isl_basic_set *bset)
4350 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4353 struct isl_basic_map *isl_basic_map_overlying_set(
4354 struct isl_basic_set *bset, struct isl_basic_map *like)
4356 struct isl_basic_map *bmap;
4357 struct isl_ctx *ctx;
4364 isl_assert(ctx, bset->n_div == 0, goto error);
4365 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4366 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4368 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4369 isl_basic_map_free(like);
4370 return (struct isl_basic_map *)bset;
4372 bset = isl_basic_set_cow(bset);
4375 total = bset->dim->n_out + bset->extra;
4376 bmap = (struct isl_basic_map *)bset;
4377 isl_space_free(bmap->dim);
4378 bmap->dim = isl_space_copy(like->dim);
4381 bmap->n_div = like->n_div;
4382 bmap->extra += like->n_div;
4386 ltotal = total - bmap->extra + like->extra;
4389 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4390 bmap->extra * (1 + 1 + total));
4391 if (isl_blk_is_error(bmap->block2))
4393 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4397 for (i = 0; i < bmap->extra; ++i)
4398 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4399 for (i = 0; i < like->n_div; ++i) {
4400 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4401 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4403 bmap = isl_basic_map_extend_constraints(bmap,
4404 0, 2 * like->n_div);
4405 for (i = 0; i < like->n_div; ++i) {
4408 if (isl_int_is_zero(bmap->div[i][0]))
4410 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4411 bmap = isl_basic_map_free(bmap);
4414 isl_basic_map_free(like);
4415 bmap = isl_basic_map_simplify(bmap);
4416 bmap = isl_basic_map_finalize(bmap);
4419 isl_basic_map_free(like);
4420 isl_basic_set_free(bset);
4424 struct isl_basic_set *isl_basic_set_from_underlying_set(
4425 struct isl_basic_set *bset, struct isl_basic_set *like)
4427 return (struct isl_basic_set *)
4428 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4431 struct isl_set *isl_set_from_underlying_set(
4432 struct isl_set *set, struct isl_basic_set *like)
4438 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4440 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4441 isl_basic_set_free(like);
4444 set = isl_set_cow(set);
4447 for (i = 0; i < set->n; ++i) {
4448 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4449 isl_basic_set_copy(like));
4453 isl_space_free(set->dim);
4454 set->dim = isl_space_copy(like->dim);
4457 isl_basic_set_free(like);
4460 isl_basic_set_free(like);
4465 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4469 map = isl_map_cow(map);
4472 map->dim = isl_space_cow(map->dim);
4476 for (i = 1; i < map->n; ++i)
4477 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4479 for (i = 0; i < map->n; ++i) {
4480 map->p[i] = (struct isl_basic_map *)
4481 isl_basic_map_underlying_set(map->p[i]);
4486 map->dim = isl_space_underlying(map->dim, 0);
4488 isl_space_free(map->dim);
4489 map->dim = isl_space_copy(map->p[0]->dim);
4493 return (struct isl_set *)map;
4499 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4501 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4504 __isl_give isl_basic_map *isl_basic_map_reset_space(
4505 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4507 bmap = isl_basic_map_cow(bmap);
4511 isl_space_free(bmap->dim);
4514 bmap = isl_basic_map_finalize(bmap);
4518 isl_basic_map_free(bmap);
4519 isl_space_free(dim);
4523 __isl_give isl_basic_set *isl_basic_set_reset_space(
4524 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4526 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4530 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4531 __isl_take isl_space *dim)
4535 map = isl_map_cow(map);
4539 for (i = 0; i < map->n; ++i) {
4540 map->p[i] = isl_basic_map_reset_space(map->p[i],
4541 isl_space_copy(dim));
4545 isl_space_free(map->dim);
4551 isl_space_free(dim);
4555 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4556 __isl_take isl_space *dim)
4558 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4561 /* Compute the parameter domain of the given basic set.
4563 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4568 if (isl_basic_set_is_params(bset))
4571 n = isl_basic_set_dim(bset, isl_dim_set);
4572 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4573 space = isl_basic_set_get_space(bset);
4574 space = isl_space_params(space);
4575 bset = isl_basic_set_reset_space(bset, space);
4579 /* Construct a zero-dimensional basic set with the given parameter domain.
4581 __isl_give isl_basic_set *isl_basic_set_from_params(
4582 __isl_take isl_basic_set *bset)
4585 space = isl_basic_set_get_space(bset);
4586 space = isl_space_set_from_params(space);
4587 bset = isl_basic_set_reset_space(bset, space);
4591 /* Compute the parameter domain of the given set.
4593 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4598 if (isl_set_is_params(set))
4601 n = isl_set_dim(set, isl_dim_set);
4602 set = isl_set_project_out(set, isl_dim_set, 0, n);
4603 space = isl_set_get_space(set);
4604 space = isl_space_params(space);
4605 set = isl_set_reset_space(set, space);
4609 /* Construct a zero-dimensional set with the given parameter domain.
4611 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4614 space = isl_set_get_space(set);
4615 space = isl_space_set_from_params(space);
4616 set = isl_set_reset_space(set, space);
4620 /* Compute the parameter domain of the given map.
4622 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4627 n = isl_map_dim(map, isl_dim_in);
4628 map = isl_map_project_out(map, isl_dim_in, 0, n);
4629 n = isl_map_dim(map, isl_dim_out);
4630 map = isl_map_project_out(map, isl_dim_out, 0, n);
4631 space = isl_map_get_space(map);
4632 space = isl_space_params(space);
4633 map = isl_map_reset_space(map, space);
4637 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4640 struct isl_basic_set *domain;
4646 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4648 n_in = isl_basic_map_n_in(bmap);
4649 n_out = isl_basic_map_n_out(bmap);
4650 domain = isl_basic_set_from_basic_map(bmap);
4651 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4653 domain = isl_basic_set_reset_space(domain, dim);
4658 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4662 return isl_space_may_be_set(bmap->dim);
4665 /* Is this basic map actually a set?
4666 * Users should never call this function. Outside of isl,
4667 * the type should indicate whether something is a set or a map.
4669 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4673 return isl_space_is_set(bmap->dim);
4676 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4680 if (isl_basic_map_is_set(bmap))
4682 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4685 __isl_give isl_basic_map *isl_basic_map_domain_map(
4686 __isl_take isl_basic_map *bmap)
4690 isl_basic_map *domain;
4691 int nparam, n_in, n_out;
4694 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4695 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4696 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4698 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4699 domain = isl_basic_map_universe(dim);
4701 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4702 bmap = isl_basic_map_apply_range(bmap, domain);
4703 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4705 total = isl_basic_map_total_dim(bmap);
4707 for (i = 0; i < n_in; ++i) {
4708 k = isl_basic_map_alloc_equality(bmap);
4711 isl_seq_clr(bmap->eq[k], 1 + total);
4712 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4713 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4716 bmap = isl_basic_map_gauss(bmap, NULL);
4717 return isl_basic_map_finalize(bmap);
4719 isl_basic_map_free(bmap);
4723 __isl_give isl_basic_map *isl_basic_map_range_map(
4724 __isl_take isl_basic_map *bmap)
4728 isl_basic_map *range;
4729 int nparam, n_in, n_out;
4732 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4733 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4734 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4736 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4737 range = isl_basic_map_universe(dim);
4739 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4740 bmap = isl_basic_map_apply_range(bmap, range);
4741 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4743 total = isl_basic_map_total_dim(bmap);
4745 for (i = 0; i < n_out; ++i) {
4746 k = isl_basic_map_alloc_equality(bmap);
4749 isl_seq_clr(bmap->eq[k], 1 + total);
4750 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4751 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4754 bmap = isl_basic_map_gauss(bmap, NULL);
4755 return isl_basic_map_finalize(bmap);
4757 isl_basic_map_free(bmap);
4761 int isl_map_may_be_set(__isl_keep isl_map *map)
4765 return isl_space_may_be_set(map->dim);
4768 /* Is this map actually a set?
4769 * Users should never call this function. Outside of isl,
4770 * the type should indicate whether something is a set or a map.
4772 int isl_map_is_set(__isl_keep isl_map *map)
4776 return isl_space_is_set(map->dim);
4779 struct isl_set *isl_map_range(struct isl_map *map)
4782 struct isl_set *set;
4786 if (isl_map_is_set(map))
4787 return (isl_set *)map;
4789 map = isl_map_cow(map);
4793 set = (struct isl_set *) map;
4794 set->dim = isl_space_range(set->dim);
4797 for (i = 0; i < map->n; ++i) {
4798 set->p[i] = isl_basic_map_range(map->p[i]);
4802 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4803 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4810 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4813 isl_space *domain_dim;
4815 map = isl_map_cow(map);
4819 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4820 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4821 map->dim = isl_space_join(map->dim, domain_dim);
4824 for (i = 0; i < map->n; ++i) {
4825 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4829 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4830 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4837 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4840 isl_space *range_dim;
4842 map = isl_map_cow(map);
4846 range_dim = isl_space_range(isl_map_get_space(map));
4847 range_dim = isl_space_from_range(range_dim);
4848 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4849 map->dim = isl_space_join(map->dim, range_dim);
4852 for (i = 0; i < map->n; ++i) {
4853 map->p[i] = isl_basic_map_range_map(map->p[i]);
4857 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4858 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4865 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4866 __isl_take isl_space *dim)
4869 struct isl_map *map = NULL;
4871 set = isl_set_cow(set);
4874 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4875 map = (struct isl_map *)set;
4876 for (i = 0; i < set->n; ++i) {
4877 map->p[i] = isl_basic_map_from_basic_set(
4878 set->p[i], isl_space_copy(dim));
4882 isl_space_free(map->dim);
4886 isl_space_free(dim);
4891 __isl_give isl_basic_map *isl_basic_map_from_domain(
4892 __isl_take isl_basic_set *bset)
4894 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4897 __isl_give isl_basic_map *isl_basic_map_from_range(
4898 __isl_take isl_basic_set *bset)
4901 space = isl_basic_set_get_space(bset);
4902 space = isl_space_from_range(space);
4903 bset = isl_basic_set_reset_space(bset, space);
4904 return (isl_basic_map *)bset;
4907 struct isl_map *isl_map_from_range(struct isl_set *set)
4910 space = isl_set_get_space(set);
4911 space = isl_space_from_range(space);
4912 set = isl_set_reset_space(set, space);
4913 return (struct isl_map *)set;
4916 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4918 return isl_map_reverse(isl_map_from_range(set));
4921 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4922 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4924 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4927 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4928 __isl_take isl_set *range)
4930 return isl_map_apply_range(isl_map_reverse(domain), range);
4933 struct isl_set *isl_set_from_map(struct isl_map *map)
4936 struct isl_set *set = NULL;
4940 map = isl_map_cow(map);
4943 map->dim = isl_space_as_set_space(map->dim);
4946 set = (struct isl_set *)map;
4947 for (i = 0; i < map->n; ++i) {
4948 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4958 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4961 struct isl_map *map;
4966 isl_die(dim->ctx, isl_error_internal,
4967 "negative number of basic maps", goto error);
4968 map = isl_alloc(dim->ctx, struct isl_map,
4969 sizeof(struct isl_map) +
4970 (n - 1) * sizeof(struct isl_basic_map *));
4974 map->ctx = dim->ctx;
4975 isl_ctx_ref(map->ctx);
4983 isl_space_free(dim);
4987 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4988 unsigned nparam, unsigned in, unsigned out, int n,
4991 struct isl_map *map;
4994 dims = isl_space_alloc(ctx, nparam, in, out);
4998 map = isl_map_alloc_space(dims, n, flags);
5002 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5004 struct isl_basic_map *bmap;
5005 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5006 bmap = isl_basic_map_set_to_empty(bmap);
5010 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5012 struct isl_basic_set *bset;
5013 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5014 bset = isl_basic_set_set_to_empty(bset);
5018 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5020 struct isl_basic_map *bmap;
5023 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5024 bmap = isl_basic_map_set_to_empty(bmap);
5028 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5030 struct isl_basic_map *bmap;
5033 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5034 bmap = isl_basic_map_set_to_empty(bmap);
5038 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5040 struct isl_basic_set *bset;
5043 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5044 bset = isl_basic_set_set_to_empty(bset);
5048 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5050 struct isl_basic_map *bmap;
5051 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5052 bmap = isl_basic_map_finalize(bmap);
5056 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5058 struct isl_basic_set *bset;
5059 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5060 bset = isl_basic_set_finalize(bset);
5064 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5067 unsigned total = isl_space_dim(dim, isl_dim_all);
5068 isl_basic_map *bmap;
5070 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5071 for (i = 0; i < total; ++i) {
5072 int k = isl_basic_map_alloc_inequality(bmap);
5075 isl_seq_clr(bmap->ineq[k], 1 + total);
5076 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5080 isl_basic_map_free(bmap);
5084 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5086 return isl_basic_map_nat_universe(dim);
5089 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5091 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5094 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5096 return isl_map_nat_universe(dim);
5099 __isl_give isl_basic_map *isl_basic_map_universe_like(
5100 __isl_keep isl_basic_map *model)
5104 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5107 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5111 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5114 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5115 __isl_keep isl_set *model)
5119 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5122 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5124 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5127 struct isl_map *isl_map_empty_like(struct isl_map *model)
5131 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5134 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5138 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5141 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5143 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5146 struct isl_set *isl_set_empty_like(struct isl_set *model)
5150 return isl_set_empty(isl_space_copy(model->dim));
5153 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5155 struct isl_map *map;
5158 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5159 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5163 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5165 struct isl_set *set;
5168 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5169 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5173 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5177 return isl_set_universe(isl_space_copy(model->dim));
5180 struct isl_map *isl_map_dup(struct isl_map *map)
5183 struct isl_map *dup;
5187 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5188 for (i = 0; i < map->n; ++i)
5189 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5193 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5194 __isl_take isl_basic_map *bmap)
5198 if (isl_basic_map_plain_is_empty(bmap)) {
5199 isl_basic_map_free(bmap);
5202 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5203 isl_assert(map->ctx, map->n < map->size, goto error);
5204 map->p[map->n] = bmap;
5206 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5212 isl_basic_map_free(bmap);
5216 void *isl_map_free(struct isl_map *map)
5226 isl_ctx_deref(map->ctx);
5227 for (i = 0; i < map->n; ++i)
5228 isl_basic_map_free(map->p[i]);
5229 isl_space_free(map->dim);
5235 struct isl_map *isl_map_extend(struct isl_map *base,
5236 unsigned nparam, unsigned n_in, unsigned n_out)
5240 base = isl_map_cow(base);
5244 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5247 for (i = 0; i < base->n; ++i) {
5248 base->p[i] = isl_basic_map_extend_space(base->p[i],
5249 isl_space_copy(base->dim), 0, 0, 0);
5259 struct isl_set *isl_set_extend(struct isl_set *base,
5260 unsigned nparam, unsigned dim)
5262 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5266 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5267 struct isl_basic_map *bmap, unsigned pos, int value)
5271 bmap = isl_basic_map_cow(bmap);
5272 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5273 j = isl_basic_map_alloc_equality(bmap);
5276 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5277 isl_int_set_si(bmap->eq[j][pos], -1);
5278 isl_int_set_si(bmap->eq[j][0], value);
5279 bmap = isl_basic_map_simplify(bmap);
5280 return isl_basic_map_finalize(bmap);
5282 isl_basic_map_free(bmap);
5286 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5287 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5291 bmap = isl_basic_map_cow(bmap);
5292 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5293 j = isl_basic_map_alloc_equality(bmap);
5296 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5297 isl_int_set_si(bmap->eq[j][pos], -1);
5298 isl_int_set(bmap->eq[j][0], value);
5299 bmap = isl_basic_map_simplify(bmap);
5300 return isl_basic_map_finalize(bmap);
5302 isl_basic_map_free(bmap);
5306 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5307 enum isl_dim_type type, unsigned pos, int value)
5311 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5312 return isl_basic_map_fix_pos_si(bmap,
5313 isl_basic_map_offset(bmap, type) + pos, value);
5315 isl_basic_map_free(bmap);
5319 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5320 enum isl_dim_type type, unsigned pos, isl_int value)
5324 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5325 return isl_basic_map_fix_pos(bmap,
5326 isl_basic_map_offset(bmap, type) + pos, value);
5328 isl_basic_map_free(bmap);
5332 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5333 * to be equal to "v".
5335 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5336 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5340 if (!isl_val_is_int(v))
5341 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5342 "expecting integer value", goto error);
5343 if (pos >= isl_basic_map_dim(bmap, type))
5344 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5345 "index out of bounds", goto error);
5346 pos += isl_basic_map_offset(bmap, type);
5347 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5351 isl_basic_map_free(bmap);
5356 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5357 * to be equal to "v".
5359 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5360 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5362 return isl_basic_map_fix_val(bset, type, pos, v);
5365 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5366 enum isl_dim_type type, unsigned pos, int value)
5368 return (struct isl_basic_set *)
5369 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5373 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5374 enum isl_dim_type type, unsigned pos, isl_int value)
5376 return (struct isl_basic_set *)
5377 isl_basic_map_fix((struct isl_basic_map *)bset,
5381 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5382 unsigned input, int value)
5384 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5387 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5388 unsigned dim, int value)
5390 return (struct isl_basic_set *)
5391 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5392 isl_dim_set, dim, value);
5395 static int remove_if_empty(__isl_keep isl_map *map, int i)
5397 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5404 isl_basic_map_free(map->p[i]);
5405 if (i != map->n - 1) {
5406 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5407 map->p[i] = map->p[map->n - 1];
5414 /* Perform "fn" on each basic map of "map", where we may not be holding
5415 * the only reference to "map".
5416 * In particular, "fn" should be a semantics preserving operation
5417 * that we want to apply to all copies of "map". We therefore need
5418 * to be careful not to modify "map" in a way that breaks "map"
5419 * in case anything goes wrong.
5421 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5422 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5424 struct isl_basic_map *bmap;
5430 for (i = map->n - 1; i >= 0; --i) {
5431 bmap = isl_basic_map_copy(map->p[i]);
5435 isl_basic_map_free(map->p[i]);
5437 if (remove_if_empty(map, i) < 0)
5447 struct isl_map *isl_map_fix_si(struct isl_map *map,
5448 enum isl_dim_type type, unsigned pos, int value)
5452 map = isl_map_cow(map);
5456 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5457 for (i = map->n - 1; i >= 0; --i) {
5458 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5459 if (remove_if_empty(map, i) < 0)
5462 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5469 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5470 enum isl_dim_type type, unsigned pos, int value)
5472 return (struct isl_set *)
5473 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5476 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5477 enum isl_dim_type type, unsigned pos, isl_int value)
5481 map = isl_map_cow(map);
5485 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5486 for (i = 0; i < map->n; ++i) {
5487 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5491 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5498 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5499 enum isl_dim_type type, unsigned pos, isl_int value)
5501 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5504 /* Fix the value of the variable at position "pos" of type "type" of "map"
5505 * to be equal to "v".
5507 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5508 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5512 map = isl_map_cow(map);
5516 if (!isl_val_is_int(v))
5517 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5518 "expecting integer value", goto error);
5519 if (pos >= isl_map_dim(map, type))
5520 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5521 "index out of bounds", goto error);
5522 for (i = map->n - 1; i >= 0; --i) {
5523 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5525 if (remove_if_empty(map, i) < 0)
5528 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5537 /* Fix the value of the variable at position "pos" of type "type" of "set"
5538 * to be equal to "v".
5540 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5541 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5543 return isl_map_fix_val(set, type, pos, v);
5546 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5547 unsigned input, int value)
5549 return isl_map_fix_si(map, isl_dim_in, input, value);
5552 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5554 return (struct isl_set *)
5555 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5558 static __isl_give isl_basic_map *basic_map_bound_si(
5559 __isl_take isl_basic_map *bmap,
5560 enum isl_dim_type type, unsigned pos, int value, int upper)
5566 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5567 pos += isl_basic_map_offset(bmap, type);
5568 bmap = isl_basic_map_cow(bmap);
5569 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5570 j = isl_basic_map_alloc_inequality(bmap);
5573 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5575 isl_int_set_si(bmap->ineq[j][pos], -1);
5576 isl_int_set_si(bmap->ineq[j][0], value);
5578 isl_int_set_si(bmap->ineq[j][pos], 1);
5579 isl_int_set_si(bmap->ineq[j][0], -value);
5581 bmap = isl_basic_map_simplify(bmap);
5582 return isl_basic_map_finalize(bmap);
5584 isl_basic_map_free(bmap);
5588 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5589 __isl_take isl_basic_map *bmap,
5590 enum isl_dim_type type, unsigned pos, int value)
5592 return basic_map_bound_si(bmap, type, pos, value, 0);
5595 /* Constrain the values of the given dimension to be no greater than "value".
5597 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5598 __isl_take isl_basic_map *bmap,
5599 enum isl_dim_type type, unsigned pos, int value)
5601 return basic_map_bound_si(bmap, type, pos, value, 1);
5604 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5605 unsigned dim, isl_int value)
5609 bset = isl_basic_set_cow(bset);
5610 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5611 j = isl_basic_set_alloc_inequality(bset);
5614 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5615 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5616 isl_int_neg(bset->ineq[j][0], value);
5617 bset = isl_basic_set_simplify(bset);
5618 return isl_basic_set_finalize(bset);
5620 isl_basic_set_free(bset);
5624 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5625 enum isl_dim_type type, unsigned pos, int value, int upper)
5629 map = isl_map_cow(map);
5633 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5634 for (i = 0; i < map->n; ++i) {
5635 map->p[i] = basic_map_bound_si(map->p[i],
5636 type, pos, value, upper);
5640 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5647 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5648 enum isl_dim_type type, unsigned pos, int value)
5650 return map_bound_si(map, type, pos, value, 0);
5653 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5654 enum isl_dim_type type, unsigned pos, int value)
5656 return map_bound_si(map, type, pos, value, 1);
5659 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5660 enum isl_dim_type type, unsigned pos, int value)
5662 return (struct isl_set *)
5663 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5666 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5667 enum isl_dim_type type, unsigned pos, int value)
5669 return isl_map_upper_bound_si(set, type, pos, value);
5672 /* Bound the given variable of "bmap" from below (or above is "upper"
5673 * is set) to "value".
5675 static __isl_give isl_basic_map *basic_map_bound(
5676 __isl_take isl_basic_map *bmap,
5677 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5683 if (pos >= isl_basic_map_dim(bmap, type))
5684 isl_die(bmap->ctx, isl_error_invalid,
5685 "index out of bounds", goto error);
5686 pos += isl_basic_map_offset(bmap, type);
5687 bmap = isl_basic_map_cow(bmap);
5688 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5689 j = isl_basic_map_alloc_inequality(bmap);
5692 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5694 isl_int_set_si(bmap->ineq[j][pos], -1);
5695 isl_int_set(bmap->ineq[j][0], value);
5697 isl_int_set_si(bmap->ineq[j][pos], 1);
5698 isl_int_neg(bmap->ineq[j][0], value);
5700 bmap = isl_basic_map_simplify(bmap);
5701 return isl_basic_map_finalize(bmap);
5703 isl_basic_map_free(bmap);
5707 /* Bound the given variable of "map" from below (or above is "upper"
5708 * is set) to "value".
5710 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5711 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5715 map = isl_map_cow(map);
5719 if (pos >= isl_map_dim(map, type))
5720 isl_die(map->ctx, isl_error_invalid,
5721 "index out of bounds", goto error);
5722 for (i = map->n - 1; i >= 0; --i) {
5723 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5724 if (remove_if_empty(map, i) < 0)
5727 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5734 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5735 enum isl_dim_type type, unsigned pos, isl_int value)
5737 return map_bound(map, type, pos, value, 0);
5740 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5741 enum isl_dim_type type, unsigned pos, isl_int value)
5743 return map_bound(map, type, pos, value, 1);
5746 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5747 enum isl_dim_type type, unsigned pos, isl_int value)
5749 return isl_map_lower_bound(set, type, pos, value);
5752 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5753 enum isl_dim_type type, unsigned pos, isl_int value)
5755 return isl_map_upper_bound(set, type, pos, value);
5758 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5763 set = isl_set_cow(set);
5767 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5768 for (i = 0; i < set->n; ++i) {
5769 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5779 struct isl_map *isl_map_reverse(struct isl_map *map)
5783 map = isl_map_cow(map);
5787 map->dim = isl_space_reverse(map->dim);
5790 for (i = 0; i < map->n; ++i) {
5791 map->p[i] = isl_basic_map_reverse(map->p[i]);
5795 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5802 static struct isl_map *isl_basic_map_partial_lexopt(
5803 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5804 struct isl_set **empty, int max)
5808 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5809 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5811 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5813 isl_basic_map_free(bmap);
5814 isl_basic_set_free(dom);
5820 struct isl_map *isl_basic_map_partial_lexmax(
5821 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5822 struct isl_set **empty)
5824 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5827 struct isl_map *isl_basic_map_partial_lexmin(
5828 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5829 struct isl_set **empty)
5831 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5834 struct isl_set *isl_basic_set_partial_lexmin(
5835 struct isl_basic_set *bset, struct isl_basic_set *dom,
5836 struct isl_set **empty)
5838 return (struct isl_set *)
5839 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5843 struct isl_set *isl_basic_set_partial_lexmax(
5844 struct isl_basic_set *bset, struct isl_basic_set *dom,
5845 struct isl_set **empty)
5847 return (struct isl_set *)
5848 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5852 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5853 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5854 __isl_give isl_set **empty)
5856 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5859 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5860 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5861 __isl_give isl_set **empty)
5863 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5866 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5867 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5868 __isl_give isl_set **empty)
5870 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5873 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5874 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5875 __isl_give isl_set **empty)
5877 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5880 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5881 __isl_take isl_basic_map *bmap, int max)
5883 isl_basic_set *dom = NULL;
5884 isl_space *dom_space;
5888 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5889 dom = isl_basic_set_universe(dom_space);
5890 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5892 isl_basic_map_free(bmap);
5896 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5897 __isl_take isl_basic_map *bmap)
5899 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5903 #define TYPE isl_pw_multi_aff
5905 #define SUFFIX _pw_multi_aff
5907 #define EMPTY isl_pw_multi_aff_empty
5909 #define ADD isl_pw_multi_aff_union_add
5910 #include "isl_map_lexopt_templ.c"
5912 /* Given a map "map", compute the lexicographically minimal
5913 * (or maximal) image element for each domain element in dom,
5914 * in the form of an isl_pw_multi_aff.
5915 * Set *empty to those elements in dom that do not have an image element.
5917 * We first compute the lexicographically minimal or maximal element
5918 * in the first basic map. This results in a partial solution "res"
5919 * and a subset "todo" of dom that still need to be handled.
5920 * We then consider each of the remaining maps in "map" and successively
5921 * update both "res" and "todo".
5923 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
5924 __isl_take isl_map *map, __isl_take isl_set *dom,
5925 __isl_give isl_set **empty, int max)
5928 isl_pw_multi_aff *res;
5934 if (isl_map_plain_is_empty(map)) {
5939 return isl_pw_multi_aff_from_map(map);
5942 res = basic_map_partial_lexopt_pw_multi_aff(
5943 isl_basic_map_copy(map->p[0]),
5944 isl_set_copy(dom), &todo, max);
5946 for (i = 1; i < map->n; ++i) {
5947 isl_pw_multi_aff *res_i;
5950 res_i = basic_map_partial_lexopt_pw_multi_aff(
5951 isl_basic_map_copy(map->p[i]),
5952 isl_set_copy(dom), &todo_i, max);
5955 res = isl_pw_multi_aff_union_lexmax(res, res_i);
5957 res = isl_pw_multi_aff_union_lexmin(res, res_i);
5959 todo = isl_set_intersect(todo, todo_i);
5980 #define TYPE isl_map
5984 #define EMPTY isl_map_empty
5986 #define ADD isl_map_union_disjoint
5987 #include "isl_map_lexopt_templ.c"
5989 /* Given a map "map", compute the lexicographically minimal
5990 * (or maximal) image element for each domain element in dom.
5991 * Set *empty to those elements in dom that do not have an image element.
5993 * We first compute the lexicographically minimal or maximal element
5994 * in the first basic map. This results in a partial solution "res"
5995 * and a subset "todo" of dom that still need to be handled.
5996 * We then consider each of the remaining maps in "map" and successively
5997 * update both "res" and "todo".
5999 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6000 * Assume we are computing the lexicographical maximum.
6001 * We first compute the lexicographically maximal element in basic map i.
6002 * This results in a partial solution res_i and a subset todo_i.
6003 * Then we combine these results with those obtain for the first k basic maps
6004 * to obtain a result that is valid for the first k+1 basic maps.
6005 * In particular, the set where there is no solution is the set where
6006 * there is no solution for the first k basic maps and also no solution
6007 * for the ith basic map, i.e.,
6009 * todo^i = todo^k * todo_i
6011 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6012 * solutions, arbitrarily breaking ties in favor of res^k.
6013 * That is, when res^k(a) >= res_i(a), we pick res^k and
6014 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6015 * the lexicographic order.)
6016 * In practice, we compute
6018 * res^k * (res_i . "<=")
6022 * res_i * (res^k . "<")
6024 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6025 * where only one of res^k and res_i provides a solution and we simply pick
6032 * Note that we only compute these intersections when dom(res^k) intersects
6033 * dom(res_i). Otherwise, the only effect of these intersections is to
6034 * potentially break up res^k and res_i into smaller pieces.
6035 * We want to avoid such splintering as much as possible.
6036 * In fact, an earlier implementation of this function would look for
6037 * better results in the domain of res^k and for extra results in todo^k,
6038 * but this would always result in a splintering according to todo^k,
6039 * even when the domain of basic map i is disjoint from the domains of
6040 * the previous basic maps.
6042 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6043 __isl_take isl_map *map, __isl_take isl_set *dom,
6044 __isl_give isl_set **empty, int max)
6047 struct isl_map *res;
6048 struct isl_set *todo;
6053 if (isl_map_plain_is_empty(map)) {
6061 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6062 isl_set_copy(dom), &todo, max);
6064 for (i = 1; i < map->n; ++i) {
6068 isl_space *dim = isl_space_range(isl_map_get_space(res));
6070 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6071 isl_set_copy(dom), &todo_i, max);
6074 lt = isl_map_lex_lt(isl_space_copy(dim));
6075 le = isl_map_lex_le(dim);
6077 lt = isl_map_lex_gt(isl_space_copy(dim));
6078 le = isl_map_lex_ge(dim);
6080 lt = isl_map_apply_range(isl_map_copy(res), lt);
6081 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6082 le = isl_map_apply_range(isl_map_copy(res_i), le);
6083 le = isl_map_intersect(le, isl_map_copy(res));
6085 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6086 res = isl_map_intersect_domain(res,
6087 isl_set_copy(todo_i));
6088 res_i = isl_map_intersect_domain(res_i,
6089 isl_set_copy(todo));
6092 res = isl_map_union_disjoint(res, res_i);
6093 res = isl_map_union_disjoint(res, lt);
6094 res = isl_map_union_disjoint(res, le);
6096 todo = isl_set_intersect(todo, todo_i);
6116 __isl_give isl_map *isl_map_partial_lexmax(
6117 __isl_take isl_map *map, __isl_take isl_set *dom,
6118 __isl_give isl_set **empty)
6120 return isl_map_partial_lexopt(map, dom, empty, 1);
6123 __isl_give isl_map *isl_map_partial_lexmin(
6124 __isl_take isl_map *map, __isl_take isl_set *dom,
6125 __isl_give isl_set **empty)
6127 return isl_map_partial_lexopt(map, dom, empty, 0);
6130 __isl_give isl_set *isl_set_partial_lexmin(
6131 __isl_take isl_set *set, __isl_take isl_set *dom,
6132 __isl_give isl_set **empty)
6134 return (struct isl_set *)
6135 isl_map_partial_lexmin((struct isl_map *)set,
6139 __isl_give isl_set *isl_set_partial_lexmax(
6140 __isl_take isl_set *set, __isl_take isl_set *dom,
6141 __isl_give isl_set **empty)
6143 return (struct isl_set *)
6144 isl_map_partial_lexmax((struct isl_map *)set,
6148 /* Compute the lexicographic minimum (or maximum if "max" is set)
6149 * of "bmap" over its domain.
6151 * Since we are not interested in the part of the domain space where
6152 * there is no solution, we initialize the domain to those constraints
6153 * of "bmap" that only involve the parameters and the input dimensions.
6154 * This relieves the parametric programming engine from detecting those
6155 * inequalities and transferring them to the context. More importantly,
6156 * it ensures that those inequalities are transferred first and not
6157 * intermixed with inequalities that actually split the domain.
6159 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6163 isl_basic_map *copy;
6166 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6167 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6168 copy = isl_basic_map_copy(bmap);
6169 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6170 isl_dim_div, 0, n_div);
6171 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6172 isl_dim_out, 0, n_out);
6173 dom = isl_basic_map_domain(copy);
6174 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6177 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6179 return isl_basic_map_lexopt(bmap, 0);
6182 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6184 return isl_basic_map_lexopt(bmap, 1);
6187 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6189 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6192 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6194 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6197 /* Extract the first and only affine expression from list
6198 * and then add it to *pwaff with the given dom.
6199 * This domain is known to be disjoint from other domains
6200 * because of the way isl_basic_map_foreach_lexmax works.
6202 static int update_dim_opt(__isl_take isl_basic_set *dom,
6203 __isl_take isl_aff_list *list, void *user)
6205 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6207 isl_pw_aff **pwaff = user;
6208 isl_pw_aff *pwaff_i;
6212 if (isl_aff_list_n_aff(list) != 1)
6213 isl_die(ctx, isl_error_internal,
6214 "expecting single element list", goto error);
6216 aff = isl_aff_list_get_aff(list, 0);
6217 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6219 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6221 isl_aff_list_free(list);
6225 isl_basic_set_free(dom);
6226 isl_aff_list_free(list);
6230 /* Given a basic map with one output dimension, compute the minimum or
6231 * maximum of that dimension as an isl_pw_aff.
6233 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6234 * call update_dim_opt on each leaf of the result.
6236 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6239 isl_space *dim = isl_basic_map_get_space(bmap);
6243 dim = isl_space_from_domain(isl_space_domain(dim));
6244 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6245 pwaff = isl_pw_aff_empty(dim);
6247 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6249 return isl_pw_aff_free(pwaff);
6254 /* Compute the minimum or maximum of the given output dimension
6255 * as a function of the parameters and the input dimensions,
6256 * but independently of the other output dimensions.
6258 * We first project out the other output dimension and then compute
6259 * the "lexicographic" maximum in each basic map, combining the results
6260 * using isl_pw_aff_union_max.
6262 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6269 n_out = isl_map_dim(map, isl_dim_out);
6270 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6271 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6276 isl_space *dim = isl_map_get_space(map);
6277 dim = isl_space_domain(isl_space_from_range(dim));
6279 return isl_pw_aff_empty(dim);
6282 pwaff = basic_map_dim_opt(map->p[0], max);
6283 for (i = 1; i < map->n; ++i) {
6284 isl_pw_aff *pwaff_i;
6286 pwaff_i = basic_map_dim_opt(map->p[i], max);
6287 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6295 /* Compute the maximum of the given output dimension as a function of the
6296 * parameters and input dimensions, but independently of
6297 * the other output dimensions.
6299 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6301 return map_dim_opt(map, pos, 1);
6304 /* Compute the minimum or maximum of the given set dimension
6305 * as a function of the parameters,
6306 * but independently of the other set dimensions.
6308 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6311 return map_dim_opt(set, pos, max);
6314 /* Compute the maximum of the given set dimension as a function of the
6315 * parameters, but independently of the other set dimensions.
6317 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6319 return set_dim_opt(set, pos, 1);
6322 /* Compute the minimum of the given set dimension as a function of the
6323 * parameters, but independently of the other set dimensions.
6325 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6327 return set_dim_opt(set, pos, 0);
6330 /* Apply a preimage specified by "mat" on the parameters of "bset".
6331 * bset is assumed to have only parameters and divs.
6333 static struct isl_basic_set *basic_set_parameter_preimage(
6334 struct isl_basic_set *bset, struct isl_mat *mat)
6341 bset->dim = isl_space_cow(bset->dim);
6345 nparam = isl_basic_set_dim(bset, isl_dim_param);
6347 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6349 bset->dim->nparam = 0;
6350 bset->dim->n_out = nparam;
6351 bset = isl_basic_set_preimage(bset, mat);
6353 bset->dim->nparam = bset->dim->n_out;
6354 bset->dim->n_out = 0;
6359 isl_basic_set_free(bset);
6363 /* Apply a preimage specified by "mat" on the parameters of "set".
6364 * set is assumed to have only parameters and divs.
6366 static struct isl_set *set_parameter_preimage(
6367 struct isl_set *set, struct isl_mat *mat)
6369 isl_space *dim = NULL;
6375 dim = isl_space_copy(set->dim);
6376 dim = isl_space_cow(dim);
6380 nparam = isl_set_dim(set, isl_dim_param);
6382 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6385 dim->n_out = nparam;
6386 isl_set_reset_space(set, dim);
6387 set = isl_set_preimage(set, mat);
6390 dim = isl_space_copy(set->dim);
6391 dim = isl_space_cow(dim);
6394 dim->nparam = dim->n_out;
6396 isl_set_reset_space(set, dim);
6399 isl_space_free(dim);
6406 /* Intersect the basic set "bset" with the affine space specified by the
6407 * equalities in "eq".
6409 static struct isl_basic_set *basic_set_append_equalities(
6410 struct isl_basic_set *bset, struct isl_mat *eq)
6418 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6423 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6424 for (i = 0; i < eq->n_row; ++i) {
6425 k = isl_basic_set_alloc_equality(bset);
6428 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6429 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6433 bset = isl_basic_set_gauss(bset, NULL);
6434 bset = isl_basic_set_finalize(bset);
6439 isl_basic_set_free(bset);
6443 /* Intersect the set "set" with the affine space specified by the
6444 * equalities in "eq".
6446 static struct isl_set *set_append_equalities(struct isl_set *set,
6454 for (i = 0; i < set->n; ++i) {
6455 set->p[i] = basic_set_append_equalities(set->p[i],
6468 /* Given a basic set "bset" that only involves parameters and existentially
6469 * quantified variables, return the index of the first equality
6470 * that only involves parameters. If there is no such equality then
6471 * return bset->n_eq.
6473 * This function assumes that isl_basic_set_gauss has been called on "bset".
6475 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6478 unsigned nparam, n_div;
6483 nparam = isl_basic_set_dim(bset, isl_dim_param);
6484 n_div = isl_basic_set_dim(bset, isl_dim_div);
6486 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6487 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6494 /* Compute an explicit representation for the existentially quantified
6495 * variables in "bset" by computing the "minimal value" of the set
6496 * variables. Since there are no set variables, the computation of
6497 * the minimal value essentially computes an explicit representation
6498 * of the non-empty part(s) of "bset".
6500 * The input only involves parameters and existentially quantified variables.
6501 * All equalities among parameters have been removed.
6503 * Since the existentially quantified variables in the result are in general
6504 * going to be different from those in the input, we first replace
6505 * them by the minimal number of variables based on their equalities.
6506 * This should simplify the parametric integer programming.
6508 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6510 isl_morph *morph1, *morph2;
6516 if (bset->n_eq == 0)
6517 return isl_basic_set_lexmin(bset);
6519 morph1 = isl_basic_set_parameter_compression(bset);
6520 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6521 bset = isl_basic_set_lift(bset);
6522 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6523 bset = isl_morph_basic_set(morph2, bset);
6524 n = isl_basic_set_dim(bset, isl_dim_set);
6525 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6527 set = isl_basic_set_lexmin(bset);
6529 set = isl_morph_set(isl_morph_inverse(morph1), set);
6534 /* Project the given basic set onto its parameter domain, possibly introducing
6535 * new, explicit, existential variables in the constraints.
6536 * The input has parameters and (possibly implicit) existential variables.
6537 * The output has the same parameters, but only
6538 * explicit existentially quantified variables.
6540 * The actual projection is performed by pip, but pip doesn't seem
6541 * to like equalities very much, so we first remove the equalities
6542 * among the parameters by performing a variable compression on
6543 * the parameters. Afterward, an inverse transformation is performed
6544 * and the equalities among the parameters are inserted back in.
6546 * The variable compression on the parameters may uncover additional
6547 * equalities that were only implicit before. We therefore check
6548 * if there are any new parameter equalities in the result and
6549 * if so recurse. The removal of parameter equalities is required
6550 * for the parameter compression performed by base_compute_divs.
6552 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6556 struct isl_mat *T, *T2;
6557 struct isl_set *set;
6560 bset = isl_basic_set_cow(bset);
6564 if (bset->n_eq == 0)
6565 return base_compute_divs(bset);
6567 bset = isl_basic_set_gauss(bset, NULL);
6570 if (isl_basic_set_plain_is_empty(bset))
6571 return isl_set_from_basic_set(bset);
6573 i = first_parameter_equality(bset);
6574 if (i == bset->n_eq)
6575 return base_compute_divs(bset);
6577 nparam = isl_basic_set_dim(bset, isl_dim_param);
6578 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6580 eq = isl_mat_cow(eq);
6581 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6582 if (T && T->n_col == 0) {
6586 bset = isl_basic_set_set_to_empty(bset);
6587 return isl_set_from_basic_set(bset);
6589 bset = basic_set_parameter_preimage(bset, T);
6591 i = first_parameter_equality(bset);
6594 else if (i == bset->n_eq)
6595 set = base_compute_divs(bset);
6597 set = parameter_compute_divs(bset);
6598 set = set_parameter_preimage(set, T2);
6599 set = set_append_equalities(set, eq);
6603 /* Insert the divs from "ls" before those of "bmap".
6605 * The number of columns is not changed, which means that the last
6606 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6607 * The caller is responsible for removing the same number of dimensions
6608 * from the space of "bmap".
6610 static __isl_give isl_basic_map *insert_divs_from_local_space(
6611 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6617 n_div = isl_local_space_dim(ls, isl_dim_div);
6621 old_n_div = bmap->n_div;
6622 bmap = insert_div_rows(bmap, n_div);
6626 for (i = 0; i < n_div; ++i) {
6627 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6628 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6634 /* Replace the space of "bmap" by the space and divs of "ls".
6636 * If "ls" has any divs, then we simplify the result since we may
6637 * have discovered some additional equalities that could simplify
6638 * the div expressions.
6640 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6641 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6645 bmap = isl_basic_map_cow(bmap);
6649 n_div = isl_local_space_dim(ls, isl_dim_div);
6650 bmap = insert_divs_from_local_space(bmap, ls);
6654 isl_space_free(bmap->dim);
6655 bmap->dim = isl_local_space_get_space(ls);
6659 isl_local_space_free(ls);
6661 bmap = isl_basic_map_simplify(bmap);
6662 bmap = isl_basic_map_finalize(bmap);
6665 isl_basic_map_free(bmap);
6666 isl_local_space_free(ls);
6670 /* Replace the space of "map" by the space and divs of "ls".
6672 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6673 __isl_take isl_local_space *ls)
6677 map = isl_map_cow(map);
6681 for (i = 0; i < map->n; ++i) {
6682 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6683 isl_local_space_copy(ls));
6687 isl_space_free(map->dim);
6688 map->dim = isl_local_space_get_space(ls);
6692 isl_local_space_free(ls);
6695 isl_local_space_free(ls);
6700 /* Compute an explicit representation for the existentially
6701 * quantified variables for which do not know any explicit representation yet.
6703 * We first sort the existentially quantified variables so that the
6704 * existentially quantified variables for which we already have an explicit
6705 * representation are placed before those for which we do not.
6706 * The input dimensions, the output dimensions and the existentially
6707 * quantified variables for which we already have an explicit
6708 * representation are then turned into parameters.
6709 * compute_divs returns a map with the same parameters and
6710 * no input or output dimensions and the dimension specification
6711 * is reset to that of the input, including the existentially quantified
6712 * variables for which we already had an explicit representation.
6714 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6716 struct isl_basic_set *bset;
6717 struct isl_set *set;
6718 struct isl_map *map;
6720 isl_local_space *ls;
6727 bmap = isl_basic_map_sort_divs(bmap);
6728 bmap = isl_basic_map_cow(bmap);
6732 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6733 if (isl_int_is_zero(bmap->div[n_known][0]))
6736 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6737 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6738 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6739 dim = isl_space_set_alloc(bmap->ctx,
6740 nparam + n_in + n_out + n_known, 0);
6744 ls = isl_basic_map_get_local_space(bmap);
6745 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6746 n_known, bmap->n_div - n_known);
6748 for (i = n_known; i < bmap->n_div; ++i)
6749 swap_div(bmap, i - n_known, i);
6750 bmap->n_div -= n_known;
6751 bmap->extra -= n_known;
6753 bmap = isl_basic_map_reset_space(bmap, dim);
6754 bset = (struct isl_basic_set *)bmap;
6756 set = parameter_compute_divs(bset);
6757 map = (struct isl_map *)set;
6758 map = replace_space_by_local_space(map, ls);
6762 isl_basic_map_free(bmap);
6766 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6774 off = isl_space_dim(bmap->dim, isl_dim_all);
6775 for (i = 0; i < bmap->n_div; ++i) {
6776 if (isl_int_is_zero(bmap->div[i][0]))
6778 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6784 static int map_divs_known(__isl_keep isl_map *map)
6791 for (i = 0; i < map->n; ++i) {
6792 int known = isl_basic_map_divs_known(map->p[i]);
6800 /* If bmap contains any unknown divs, then compute explicit
6801 * expressions for them. However, this computation may be
6802 * quite expensive, so first try to remove divs that aren't
6805 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6808 struct isl_map *map;
6810 known = isl_basic_map_divs_known(bmap);
6814 return isl_map_from_basic_map(bmap);
6816 bmap = isl_basic_map_drop_redundant_divs(bmap);
6818 known = isl_basic_map_divs_known(bmap);
6822 return isl_map_from_basic_map(bmap);
6824 map = compute_divs(bmap);
6827 isl_basic_map_free(bmap);
6831 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6835 struct isl_map *res;
6842 known = map_divs_known(map);
6850 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6851 for (i = 1 ; i < map->n; ++i) {
6853 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6854 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6855 res = isl_map_union_disjoint(res, r2);
6857 res = isl_map_union(res, r2);
6864 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6866 return (struct isl_set *)
6867 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6870 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6872 return (struct isl_set *)
6873 isl_map_compute_divs((struct isl_map *)set);
6876 struct isl_set *isl_map_domain(struct isl_map *map)
6879 struct isl_set *set;
6884 map = isl_map_cow(map);
6888 set = (struct isl_set *)map;
6889 set->dim = isl_space_domain(set->dim);
6892 for (i = 0; i < map->n; ++i) {
6893 set->p[i] = isl_basic_map_domain(map->p[i]);
6897 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6898 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6905 /* Return the union of "map1" and "map2", where we assume for now that
6906 * "map1" and "map2" are disjoint. Note that the basic maps inside
6907 * "map1" or "map2" may not be disjoint from each other.
6908 * Also note that this function is also called from isl_map_union,
6909 * which takes care of handling the situation where "map1" and "map2"
6910 * may not be disjoint.
6912 * If one of the inputs is empty, we can simply return the other input.
6913 * Similarly, if one of the inputs is universal, then it is equal to the union.
6915 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6916 __isl_take isl_map *map2)
6920 struct isl_map *map = NULL;
6935 is_universe = isl_map_plain_is_universe(map1);
6936 if (is_universe < 0)
6943 is_universe = isl_map_plain_is_universe(map2);
6944 if (is_universe < 0)
6951 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6953 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6954 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6955 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6957 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6958 map1->n + map2->n, flags);
6961 for (i = 0; i < map1->n; ++i) {
6962 map = isl_map_add_basic_map(map,
6963 isl_basic_map_copy(map1->p[i]));
6967 for (i = 0; i < map2->n; ++i) {
6968 map = isl_map_add_basic_map(map,
6969 isl_basic_map_copy(map2->p[i]));
6983 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6984 __isl_take isl_map *map2)
6986 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6989 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6991 map1 = isl_map_union_disjoint(map1, map2);
6995 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6999 struct isl_set *isl_set_union_disjoint(
7000 struct isl_set *set1, struct isl_set *set2)
7002 return (struct isl_set *)
7003 isl_map_union_disjoint(
7004 (struct isl_map *)set1, (struct isl_map *)set2);
7007 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7009 return (struct isl_set *)
7010 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7013 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7016 * "map" and "set" are assumed to be compatible and non-NULL.
7018 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7019 __isl_take isl_set *set,
7020 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7021 __isl_take isl_basic_set *bset))
7024 struct isl_map *result;
7027 if (isl_set_plain_is_universe(set)) {
7032 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7033 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7034 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7036 result = isl_map_alloc_space(isl_space_copy(map->dim),
7037 map->n * set->n, flags);
7038 for (i = 0; result && i < map->n; ++i)
7039 for (j = 0; j < set->n; ++j) {
7040 result = isl_map_add_basic_map(result,
7041 fn(isl_basic_map_copy(map->p[i]),
7042 isl_basic_set_copy(set->p[j])));
7052 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7053 __isl_take isl_set *set)
7058 if (!isl_map_compatible_range(map, set))
7059 isl_die(set->ctx, isl_error_invalid,
7060 "incompatible spaces", goto error);
7062 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7069 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7070 __isl_take isl_set *set)
7072 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7075 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7076 __isl_take isl_set *set)
7081 if (!isl_map_compatible_domain(map, set))
7082 isl_die(set->ctx, isl_error_invalid,
7083 "incompatible spaces", goto error);
7085 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7092 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7093 __isl_take isl_set *set)
7095 return isl_map_align_params_map_map_and(map, set,
7096 &map_intersect_domain);
7099 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7100 __isl_take isl_map *map2)
7104 map1 = isl_map_reverse(map1);
7105 map1 = isl_map_apply_range(map1, map2);
7106 return isl_map_reverse(map1);
7113 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7114 __isl_take isl_map *map2)
7116 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7119 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7120 __isl_take isl_map *map2)
7122 isl_space *dim_result;
7123 struct isl_map *result;
7129 dim_result = isl_space_join(isl_space_copy(map1->dim),
7130 isl_space_copy(map2->dim));
7132 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7135 for (i = 0; i < map1->n; ++i)
7136 for (j = 0; j < map2->n; ++j) {
7137 result = isl_map_add_basic_map(result,
7138 isl_basic_map_apply_range(
7139 isl_basic_map_copy(map1->p[i]),
7140 isl_basic_map_copy(map2->p[j])));
7146 if (result && result->n <= 1)
7147 ISL_F_SET(result, ISL_MAP_DISJOINT);
7155 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7156 __isl_take isl_map *map2)
7158 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7162 * returns range - domain
7164 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7166 isl_space *dims, *target_dim;
7167 struct isl_basic_set *bset;
7174 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7175 bmap->dim, isl_dim_out),
7177 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7178 dim = isl_basic_map_n_in(bmap);
7179 nparam = isl_basic_map_n_param(bmap);
7180 bset = isl_basic_set_from_basic_map(bmap);
7181 bset = isl_basic_set_cow(bset);
7182 dims = isl_basic_set_get_space(bset);
7183 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7184 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7185 bset = isl_basic_set_swap_vars(bset, 2*dim);
7186 for (i = 0; i < dim; ++i) {
7187 int j = isl_basic_map_alloc_equality(
7188 (struct isl_basic_map *)bset);
7190 bset = isl_basic_set_free(bset);
7193 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7194 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7195 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7196 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7198 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7199 bset = isl_basic_set_reset_space(bset, target_dim);
7202 isl_basic_map_free(bmap);
7207 * returns range - domain
7209 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7213 struct isl_set *result;
7218 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7219 map->dim, isl_dim_out),
7221 dim = isl_map_get_space(map);
7222 dim = isl_space_domain(dim);
7223 result = isl_set_alloc_space(dim, map->n, 0);
7226 for (i = 0; i < map->n; ++i)
7227 result = isl_set_add_basic_set(result,
7228 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7237 * returns [domain -> range] -> range - domain
7239 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7240 __isl_take isl_basic_map *bmap)
7244 isl_basic_map *domain;
7248 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7249 isl_die(bmap->ctx, isl_error_invalid,
7250 "domain and range don't match", goto error);
7252 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7253 n = isl_basic_map_dim(bmap, isl_dim_in);
7255 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7256 domain = isl_basic_map_universe(dim);
7258 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7259 bmap = isl_basic_map_apply_range(bmap, domain);
7260 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7262 total = isl_basic_map_total_dim(bmap);
7264 for (i = 0; i < n; ++i) {
7265 k = isl_basic_map_alloc_equality(bmap);
7268 isl_seq_clr(bmap->eq[k], 1 + total);
7269 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7270 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7271 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7274 bmap = isl_basic_map_gauss(bmap, NULL);
7275 return isl_basic_map_finalize(bmap);
7277 isl_basic_map_free(bmap);
7282 * returns [domain -> range] -> range - domain
7284 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7287 isl_space *domain_dim;
7292 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7293 isl_die(map->ctx, isl_error_invalid,
7294 "domain and range don't match", goto error);
7296 map = isl_map_cow(map);
7300 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7301 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7302 map->dim = isl_space_join(map->dim, domain_dim);
7305 for (i = 0; i < map->n; ++i) {
7306 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7310 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7317 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7319 struct isl_basic_map *bmap;
7327 nparam = dims->nparam;
7329 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7333 for (i = 0; i < dim; ++i) {
7334 int j = isl_basic_map_alloc_equality(bmap);
7337 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7338 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7339 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7341 return isl_basic_map_finalize(bmap);
7343 isl_basic_map_free(bmap);
7347 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7351 if (dim->n_in != dim->n_out)
7352 isl_die(dim->ctx, isl_error_invalid,
7353 "number of input and output dimensions needs to be "
7354 "the same", goto error);
7355 return basic_map_identity(dim);
7357 isl_space_free(dim);
7361 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7363 if (!model || !model->dim)
7365 return isl_basic_map_identity(isl_space_copy(model->dim));
7368 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7370 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7373 struct isl_map *isl_map_identity_like(struct isl_map *model)
7375 if (!model || !model->dim)
7377 return isl_map_identity(isl_space_copy(model->dim));
7380 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7382 if (!model || !model->dim)
7384 return isl_map_identity(isl_space_copy(model->dim));
7387 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7389 isl_space *dim = isl_set_get_space(set);
7391 id = isl_map_identity(isl_space_map_from_set(dim));
7392 return isl_map_intersect_range(id, set);
7395 /* Construct a basic set with all set dimensions having only non-negative
7398 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7399 __isl_take isl_space *space)
7404 struct isl_basic_set *bset;
7408 nparam = space->nparam;
7410 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7413 for (i = 0; i < dim; ++i) {
7414 int k = isl_basic_set_alloc_inequality(bset);
7417 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7418 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7422 isl_basic_set_free(bset);
7426 /* Construct the half-space x_pos >= 0.
7428 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7432 isl_basic_set *nonneg;
7434 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7435 k = isl_basic_set_alloc_inequality(nonneg);
7438 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7439 isl_int_set_si(nonneg->ineq[k][pos], 1);
7441 return isl_basic_set_finalize(nonneg);
7443 isl_basic_set_free(nonneg);
7447 /* Construct the half-space x_pos <= -1.
7449 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7454 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7455 k = isl_basic_set_alloc_inequality(neg);
7458 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7459 isl_int_set_si(neg->ineq[k][0], -1);
7460 isl_int_set_si(neg->ineq[k][pos], -1);
7462 return isl_basic_set_finalize(neg);
7464 isl_basic_set_free(neg);
7468 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7469 enum isl_dim_type type, unsigned first, unsigned n)
7472 isl_basic_set *nonneg;
7480 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7482 for (i = 0; i < n; ++i) {
7483 nonneg = nonneg_halfspace(isl_set_get_space(set),
7484 pos(set->dim, type) + first + i);
7485 neg = neg_halfspace(isl_set_get_space(set),
7486 pos(set->dim, type) + first + i);
7488 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7497 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7498 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7505 if (isl_set_plain_is_empty(set)) {
7510 return fn(set, signs, user);
7513 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7515 half = isl_set_intersect(half, isl_set_copy(set));
7516 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7520 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7522 half = isl_set_intersect(half, set);
7523 return foreach_orthant(half, signs, first + 1, len, fn, user);
7529 /* Call "fn" on the intersections of "set" with each of the orthants
7530 * (except for obviously empty intersections). The orthant is identified
7531 * by the signs array, with each entry having value 1 or -1 according
7532 * to the sign of the corresponding variable.
7534 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7535 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7545 if (isl_set_plain_is_empty(set))
7548 nparam = isl_set_dim(set, isl_dim_param);
7549 nvar = isl_set_dim(set, isl_dim_set);
7551 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7553 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7561 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7563 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7566 int isl_basic_map_is_subset(
7567 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7570 struct isl_map *map1;
7571 struct isl_map *map2;
7573 if (!bmap1 || !bmap2)
7576 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7577 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7579 is_subset = isl_map_is_subset(map1, map2);
7587 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7588 __isl_keep isl_basic_set *bset2)
7590 return isl_basic_map_is_subset(bset1, bset2);
7593 int isl_basic_map_is_equal(
7594 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7598 if (!bmap1 || !bmap2)
7600 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7603 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7607 int isl_basic_set_is_equal(
7608 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7610 return isl_basic_map_is_equal(
7611 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7614 int isl_map_is_empty(struct isl_map *map)
7621 for (i = 0; i < map->n; ++i) {
7622 is_empty = isl_basic_map_is_empty(map->p[i]);
7631 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7633 return map ? map->n == 0 : -1;
7636 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7638 return isl_map_plain_is_empty(map);
7641 int isl_set_plain_is_empty(struct isl_set *set)
7643 return set ? set->n == 0 : -1;
7646 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7648 return isl_set_plain_is_empty(set);
7651 int isl_set_is_empty(struct isl_set *set)
7653 return isl_map_is_empty((struct isl_map *)set);
7656 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7661 return isl_space_is_equal(map1->dim, map2->dim);
7664 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7669 return isl_space_is_equal(set1->dim, set2->dim);
7672 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7678 is_subset = isl_map_is_subset(map1, map2);
7681 is_subset = isl_map_is_subset(map2, map1);
7685 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7687 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7690 int isl_basic_map_is_strict_subset(
7691 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7695 if (!bmap1 || !bmap2)
7697 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7700 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7701 if (is_subset == -1)
7706 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7712 is_subset = isl_map_is_subset(map1, map2);
7715 is_subset = isl_map_is_subset(map2, map1);
7716 if (is_subset == -1)
7721 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7723 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7726 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7730 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7733 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7737 return bset->n_eq == 0 && bset->n_ineq == 0;
7740 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7747 for (i = 0; i < map->n; ++i) {
7748 int r = isl_basic_map_is_universe(map->p[i]);
7756 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7758 return isl_map_plain_is_universe((isl_map *) set);
7761 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7763 return isl_set_plain_is_universe(set);
7766 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7768 struct isl_basic_set *bset = NULL;
7769 struct isl_vec *sample = NULL;
7776 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7779 if (isl_basic_map_is_universe(bmap))
7782 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7783 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7784 copy = isl_basic_map_remove_redundancies(copy);
7785 empty = isl_basic_map_plain_is_empty(copy);
7786 isl_basic_map_free(copy);
7790 total = 1 + isl_basic_map_total_dim(bmap);
7791 if (bmap->sample && bmap->sample->size == total) {
7792 int contains = isl_basic_map_contains(bmap, bmap->sample);
7798 isl_vec_free(bmap->sample);
7799 bmap->sample = NULL;
7800 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7803 sample = isl_basic_set_sample_vec(bset);
7806 empty = sample->size == 0;
7807 isl_vec_free(bmap->sample);
7808 bmap->sample = sample;
7810 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7815 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7819 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7822 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7824 return isl_basic_map_plain_is_empty(bmap);
7827 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7831 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7834 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7836 return isl_basic_set_plain_is_empty(bset);
7839 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7841 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7844 struct isl_map *isl_basic_map_union(
7845 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7847 struct isl_map *map;
7848 if (!bmap1 || !bmap2)
7851 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7853 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7856 map = isl_map_add_basic_map(map, bmap1);
7857 map = isl_map_add_basic_map(map, bmap2);
7860 isl_basic_map_free(bmap1);
7861 isl_basic_map_free(bmap2);
7865 struct isl_set *isl_basic_set_union(
7866 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7868 return (struct isl_set *)isl_basic_map_union(
7869 (struct isl_basic_map *)bset1,
7870 (struct isl_basic_map *)bset2);
7873 /* Order divs such that any div only depends on previous divs */
7874 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7882 off = isl_space_dim(bmap->dim, isl_dim_all);
7884 for (i = 0; i < bmap->n_div; ++i) {
7886 if (isl_int_is_zero(bmap->div[i][0]))
7888 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7892 isl_basic_map_swap_div(bmap, i, i + pos);
7898 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7900 return (struct isl_basic_set *)
7901 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7904 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7911 for (i = 0; i < map->n; ++i) {
7912 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7923 /* Apply the expansion computed by isl_merge_divs.
7924 * The expansion itself is given by "exp" while the resulting
7925 * list of divs is given by "div".
7927 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7928 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7933 bset = isl_basic_set_cow(bset);
7937 if (div->n_row < bset->n_div)
7938 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7939 "not an expansion", goto error);
7941 n_div = bset->n_div;
7942 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7943 div->n_row - n_div, 0,
7944 2 * (div->n_row - n_div));
7946 for (i = n_div; i < div->n_row; ++i)
7947 if (isl_basic_set_alloc_div(bset) < 0)
7951 for (i = div->n_row - 1; i >= 0; --i) {
7952 if (j >= 0 && exp[j] == i) {
7954 isl_basic_map_swap_div(bset, i, j);
7957 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7958 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7966 isl_basic_set_free(bset);
7971 /* Look for a div in dst that corresponds to the div "div" in src.
7972 * The divs before "div" in src and dst are assumed to be the same.
7974 * Returns -1 if no corresponding div was found and the position
7975 * of the corresponding div in dst otherwise.
7977 static int find_div(struct isl_basic_map *dst,
7978 struct isl_basic_map *src, unsigned div)
7982 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7984 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7985 for (i = div; i < dst->n_div; ++i)
7986 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7987 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7988 dst->n_div - div) == -1)
7993 struct isl_basic_map *isl_basic_map_align_divs(
7994 struct isl_basic_map *dst, struct isl_basic_map *src)
8002 if (src->n_div == 0)
8005 for (i = 0; i < src->n_div; ++i)
8006 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8008 src = isl_basic_map_order_divs(src);
8009 dst = isl_basic_map_cow(dst);
8010 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8011 src->n_div, 0, 2 * src->n_div);
8014 total = isl_space_dim(src->dim, isl_dim_all);
8015 for (i = 0; i < src->n_div; ++i) {
8016 int j = find_div(dst, src, i);
8018 j = isl_basic_map_alloc_div(dst);
8021 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8022 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8023 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8027 isl_basic_map_swap_div(dst, i, j);
8031 isl_basic_map_free(dst);
8035 struct isl_basic_set *isl_basic_set_align_divs(
8036 struct isl_basic_set *dst, struct isl_basic_set *src)
8038 return (struct isl_basic_set *)isl_basic_map_align_divs(
8039 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8042 struct isl_map *isl_map_align_divs(struct isl_map *map)
8050 map = isl_map_compute_divs(map);
8051 map = isl_map_cow(map);
8055 for (i = 1; i < map->n; ++i)
8056 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8057 for (i = 1; i < map->n; ++i)
8058 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8060 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8064 struct isl_set *isl_set_align_divs(struct isl_set *set)
8066 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8069 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8070 __isl_take isl_map *map)
8074 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8075 map = isl_map_intersect_domain(map, set);
8076 set = isl_map_range(map);
8084 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8085 __isl_take isl_map *map)
8087 return isl_map_align_params_map_map_and(set, map, &set_apply);
8090 /* There is no need to cow as removing empty parts doesn't change
8091 * the meaning of the set.
8093 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8100 for (i = map->n - 1; i >= 0; --i)
8101 remove_if_empty(map, i);
8106 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8108 return (struct isl_set *)
8109 isl_map_remove_empty_parts((struct isl_map *)set);
8112 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8114 struct isl_basic_map *bmap;
8115 if (!map || map->n == 0)
8117 bmap = map->p[map->n-1];
8118 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8119 return isl_basic_map_copy(bmap);
8122 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8124 return (struct isl_basic_set *)
8125 isl_map_copy_basic_map((struct isl_map *)set);
8128 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8129 __isl_keep isl_basic_map *bmap)
8135 for (i = map->n-1; i >= 0; --i) {
8136 if (map->p[i] != bmap)
8138 map = isl_map_cow(map);
8141 isl_basic_map_free(map->p[i]);
8142 if (i != map->n-1) {
8143 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8144 map->p[i] = map->p[map->n-1];
8155 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8156 struct isl_basic_set *bset)
8158 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8159 (struct isl_basic_map *)bset);
8162 /* Given two basic sets bset1 and bset2, compute the maximal difference
8163 * between the values of dimension pos in bset1 and those in bset2
8164 * for any common value of the parameters and dimensions preceding pos.
8166 static enum isl_lp_result basic_set_maximal_difference_at(
8167 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8168 int pos, isl_int *opt)
8171 struct isl_basic_map *bmap1 = NULL;
8172 struct isl_basic_map *bmap2 = NULL;
8173 struct isl_ctx *ctx;
8174 struct isl_vec *obj;
8177 unsigned dim1, dim2;
8178 enum isl_lp_result res;
8180 if (!bset1 || !bset2)
8181 return isl_lp_error;
8183 nparam = isl_basic_set_n_param(bset1);
8184 dim1 = isl_basic_set_n_dim(bset1);
8185 dim2 = isl_basic_set_n_dim(bset2);
8186 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8187 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8188 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8189 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8190 if (!bmap1 || !bmap2)
8192 bmap1 = isl_basic_map_cow(bmap1);
8193 bmap1 = isl_basic_map_extend(bmap1, nparam,
8194 pos, (dim1 - pos) + (dim2 - pos),
8195 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8196 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8199 total = isl_basic_map_total_dim(bmap1);
8201 obj = isl_vec_alloc(ctx, 1 + total);
8204 isl_seq_clr(obj->block.data, 1 + total);
8205 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8206 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8207 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8209 isl_basic_map_free(bmap1);
8213 isl_basic_map_free(bmap2);
8215 isl_basic_map_free(bmap1);
8216 return isl_lp_error;
8219 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8220 * for any common value of the parameters and dimensions preceding pos
8221 * in both basic sets, the values of dimension pos in bset1 are
8222 * smaller or larger than those in bset2.
8225 * 1 if bset1 follows bset2
8226 * -1 if bset1 precedes bset2
8227 * 0 if bset1 and bset2 are incomparable
8228 * -2 if some error occurred.
8230 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8231 struct isl_basic_set *bset2, int pos)
8234 enum isl_lp_result res;
8239 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8241 if (res == isl_lp_empty)
8243 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8244 res == isl_lp_unbounded)
8246 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8255 /* Given two basic sets bset1 and bset2, check whether
8256 * for any common value of the parameters and dimensions preceding pos
8257 * there is a value of dimension pos in bset1 that is larger
8258 * than a value of the same dimension in bset2.
8261 * 1 if there exists such a pair
8262 * 0 if there is no such pair, but there is a pair of equal values
8264 * -2 if some error occurred.
8266 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8267 __isl_keep isl_basic_set *bset2, int pos)
8270 enum isl_lp_result res;
8275 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8277 if (res == isl_lp_empty)
8279 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8280 res == isl_lp_unbounded)
8282 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8284 else if (res == isl_lp_ok)
8293 /* Given two sets set1 and set2, check whether
8294 * for any common value of the parameters and dimensions preceding pos
8295 * there is a value of dimension pos in set1 that is larger
8296 * than a value of the same dimension in set2.
8299 * 1 if there exists such a pair
8300 * 0 if there is no such pair, but there is a pair of equal values
8302 * -2 if some error occurred.
8304 int isl_set_follows_at(__isl_keep isl_set *set1,
8305 __isl_keep isl_set *set2, int pos)
8313 for (i = 0; i < set1->n; ++i)
8314 for (j = 0; j < set2->n; ++j) {
8316 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8317 if (f == 1 || f == -2)
8326 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8327 unsigned pos, isl_int *val)
8335 total = isl_basic_map_total_dim(bmap);
8336 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8337 for (; d+1 > pos; --d)
8338 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8342 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8344 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8346 if (!isl_int_is_one(bmap->eq[i][1+d]))
8349 isl_int_neg(*val, bmap->eq[i][0]);
8355 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8356 unsigned pos, isl_int *val)
8368 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8371 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8372 for (i = 1; fixed == 1 && i < map->n; ++i) {
8373 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8374 if (fixed == 1 && isl_int_ne(tmp, v))
8378 isl_int_set(*val, v);
8384 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8385 unsigned pos, isl_int *val)
8387 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8391 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8394 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8397 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8398 enum isl_dim_type type, unsigned pos, isl_int *val)
8400 if (pos >= isl_basic_map_dim(bmap, type))
8402 return isl_basic_map_plain_has_fixed_var(bmap,
8403 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8406 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8407 enum isl_dim_type type, unsigned pos, isl_int *val)
8409 if (pos >= isl_map_dim(map, type))
8411 return isl_map_plain_has_fixed_var(map,
8412 map_offset(map, type) - 1 + pos, val);
8415 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8416 enum isl_dim_type type, unsigned pos, isl_int *val)
8418 return isl_map_plain_is_fixed(set, type, pos, val);
8421 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8422 enum isl_dim_type type, unsigned pos, isl_int *val)
8424 return isl_map_plain_is_fixed(map, type, pos, val);
8427 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8428 * then return this fixed value in *val.
8430 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8431 unsigned dim, isl_int *val)
8433 return isl_basic_set_plain_has_fixed_var(bset,
8434 isl_basic_set_n_param(bset) + dim, val);
8437 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8438 * then return this fixed value in *val.
8440 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8441 unsigned dim, isl_int *val)
8443 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8446 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8447 unsigned dim, isl_int *val)
8449 return isl_set_plain_dim_is_fixed(set, dim, val);
8452 /* Check if input variable in has fixed value and if so and if val is not NULL,
8453 * then return this fixed value in *val.
8455 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8456 unsigned in, isl_int *val)
8458 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8461 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8462 * and if val is not NULL, then return this lower bound in *val.
8464 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8465 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8467 int i, i_eq = -1, i_ineq = -1;
8474 total = isl_basic_set_total_dim(bset);
8475 nparam = isl_basic_set_n_param(bset);
8476 for (i = 0; i < bset->n_eq; ++i) {
8477 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8483 for (i = 0; i < bset->n_ineq; ++i) {
8484 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8486 if (i_eq != -1 || i_ineq != -1)
8490 if (i_eq == -1 && i_ineq == -1)
8492 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8493 /* The coefficient should always be one due to normalization. */
8494 if (!isl_int_is_one(c[1+nparam+dim]))
8496 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8498 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8499 total - nparam - dim - 1) != -1)
8502 isl_int_neg(*val, c[0]);
8506 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8507 unsigned dim, isl_int *val)
8519 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8523 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8525 for (i = 1; fixed == 1 && i < set->n; ++i) {
8526 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8528 if (fixed == 1 && isl_int_ne(tmp, v))
8532 isl_int_set(*val, v);
8543 /* uset_gist depends on constraints without existentially quantified
8544 * variables sorting first.
8546 static int qsort_constraint_cmp(const void *p1, const void *p2)
8548 const struct constraint *c1 = (const struct constraint *)p1;
8549 const struct constraint *c2 = (const struct constraint *)p2;
8551 unsigned size = isl_min(c1->size, c2->size);
8553 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8554 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8559 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8562 static struct isl_basic_map *isl_basic_map_sort_constraints(
8563 struct isl_basic_map *bmap)
8566 struct constraint *c;
8571 total = isl_basic_map_total_dim(bmap);
8572 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8575 for (i = 0; i < bmap->n_ineq; ++i) {
8577 c[i].c = bmap->ineq[i];
8579 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8580 for (i = 0; i < bmap->n_ineq; ++i)
8581 bmap->ineq[i] = c[i].c;
8585 isl_basic_map_free(bmap);
8589 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8590 __isl_take isl_basic_set *bset)
8592 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8593 (struct isl_basic_map *)bset);
8596 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8600 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8602 bmap = isl_basic_map_remove_redundancies(bmap);
8603 bmap = isl_basic_map_sort_constraints(bmap);
8605 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8609 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8611 return (struct isl_basic_set *)isl_basic_map_normalize(
8612 (struct isl_basic_map *)bset);
8615 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8616 const __isl_keep isl_basic_map *bmap2)
8623 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8624 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8625 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8626 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8627 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8628 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8629 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8630 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8631 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8632 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8633 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8635 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8637 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8639 if (bmap1->n_eq != bmap2->n_eq)
8640 return bmap1->n_eq - bmap2->n_eq;
8641 if (bmap1->n_ineq != bmap2->n_ineq)
8642 return bmap1->n_ineq - bmap2->n_ineq;
8643 if (bmap1->n_div != bmap2->n_div)
8644 return bmap1->n_div - bmap2->n_div;
8645 total = isl_basic_map_total_dim(bmap1);
8646 for (i = 0; i < bmap1->n_eq; ++i) {
8647 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8651 for (i = 0; i < bmap1->n_ineq; ++i) {
8652 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8656 for (i = 0; i < bmap1->n_div; ++i) {
8657 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8664 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8665 const __isl_keep isl_basic_set *bset2)
8667 return isl_basic_map_plain_cmp(bset1, bset2);
8670 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8676 if (set1->n != set2->n)
8677 return set1->n - set2->n;
8679 for (i = 0; i < set1->n; ++i) {
8680 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8688 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8689 __isl_keep isl_basic_map *bmap2)
8691 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8694 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8695 __isl_keep isl_basic_set *bset2)
8697 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8698 (isl_basic_map *)bset2);
8701 static int qsort_bmap_cmp(const void *p1, const void *p2)
8703 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8704 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8706 return isl_basic_map_plain_cmp(bmap1, bmap2);
8709 /* We normalize in place, but if anything goes wrong we need
8710 * to return NULL, so we need to make sure we don't change the
8711 * meaning of any possible other copies of map.
8713 struct isl_map *isl_map_normalize(struct isl_map *map)
8716 struct isl_basic_map *bmap;
8720 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8722 for (i = 0; i < map->n; ++i) {
8723 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8726 isl_basic_map_free(map->p[i]);
8729 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8730 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8731 map = isl_map_remove_empty_parts(map);
8734 for (i = map->n - 1; i >= 1; --i) {
8735 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8737 isl_basic_map_free(map->p[i-1]);
8738 for (j = i; j < map->n; ++j)
8739 map->p[j-1] = map->p[j];
8749 struct isl_set *isl_set_normalize(struct isl_set *set)
8751 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8754 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8764 if (!isl_space_is_equal(map1->dim, map2->dim))
8767 map1 = isl_map_copy(map1);
8768 map2 = isl_map_copy(map2);
8769 map1 = isl_map_normalize(map1);
8770 map2 = isl_map_normalize(map2);
8773 equal = map1->n == map2->n;
8774 for (i = 0; equal && i < map1->n; ++i) {
8775 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8788 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8790 return isl_map_plain_is_equal(map1, map2);
8793 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8795 return isl_map_plain_is_equal((struct isl_map *)set1,
8796 (struct isl_map *)set2);
8799 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8801 return isl_set_plain_is_equal(set1, set2);
8804 /* Return an interval that ranges from min to max (inclusive)
8806 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8807 isl_int min, isl_int max)
8810 struct isl_basic_set *bset = NULL;
8812 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8816 k = isl_basic_set_alloc_inequality(bset);
8819 isl_int_set_si(bset->ineq[k][1], 1);
8820 isl_int_neg(bset->ineq[k][0], min);
8822 k = isl_basic_set_alloc_inequality(bset);
8825 isl_int_set_si(bset->ineq[k][1], -1);
8826 isl_int_set(bset->ineq[k][0], max);
8830 isl_basic_set_free(bset);
8834 /* Return the Cartesian product of the basic sets in list (in the given order).
8836 __isl_give isl_basic_set *isl_basic_set_list_product(
8837 __isl_take struct isl_basic_set_list *list)
8845 struct isl_basic_set *product = NULL;
8849 isl_assert(list->ctx, list->n > 0, goto error);
8850 isl_assert(list->ctx, list->p[0], goto error);
8851 nparam = isl_basic_set_n_param(list->p[0]);
8852 dim = isl_basic_set_n_dim(list->p[0]);
8853 extra = list->p[0]->n_div;
8854 n_eq = list->p[0]->n_eq;
8855 n_ineq = list->p[0]->n_ineq;
8856 for (i = 1; i < list->n; ++i) {
8857 isl_assert(list->ctx, list->p[i], goto error);
8858 isl_assert(list->ctx,
8859 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8860 dim += isl_basic_set_n_dim(list->p[i]);
8861 extra += list->p[i]->n_div;
8862 n_eq += list->p[i]->n_eq;
8863 n_ineq += list->p[i]->n_ineq;
8865 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8870 for (i = 0; i < list->n; ++i) {
8871 isl_basic_set_add_constraints(product,
8872 isl_basic_set_copy(list->p[i]), dim);
8873 dim += isl_basic_set_n_dim(list->p[i]);
8875 isl_basic_set_list_free(list);
8878 isl_basic_set_free(product);
8879 isl_basic_set_list_free(list);
8883 struct isl_basic_map *isl_basic_map_product(
8884 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8886 isl_space *dim_result = NULL;
8887 struct isl_basic_map *bmap;
8888 unsigned in1, in2, out1, out2, nparam, total, pos;
8889 struct isl_dim_map *dim_map1, *dim_map2;
8891 if (!bmap1 || !bmap2)
8894 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8895 bmap2->dim, isl_dim_param), goto error);
8896 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8897 isl_space_copy(bmap2->dim));
8899 in1 = isl_basic_map_n_in(bmap1);
8900 in2 = isl_basic_map_n_in(bmap2);
8901 out1 = isl_basic_map_n_out(bmap1);
8902 out2 = isl_basic_map_n_out(bmap2);
8903 nparam = isl_basic_map_n_param(bmap1);
8905 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8906 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8907 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8908 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8909 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8910 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8911 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8912 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8913 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8914 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8915 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8917 bmap = isl_basic_map_alloc_space(dim_result,
8918 bmap1->n_div + bmap2->n_div,
8919 bmap1->n_eq + bmap2->n_eq,
8920 bmap1->n_ineq + bmap2->n_ineq);
8921 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8922 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8923 bmap = isl_basic_map_simplify(bmap);
8924 return isl_basic_map_finalize(bmap);
8926 isl_basic_map_free(bmap1);
8927 isl_basic_map_free(bmap2);
8931 __isl_give isl_basic_map *isl_basic_map_flat_product(
8932 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8934 isl_basic_map *prod;
8936 prod = isl_basic_map_product(bmap1, bmap2);
8937 prod = isl_basic_map_flatten(prod);
8941 __isl_give isl_basic_set *isl_basic_set_flat_product(
8942 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8944 return isl_basic_map_flat_range_product(bset1, bset2);
8947 __isl_give isl_basic_map *isl_basic_map_domain_product(
8948 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8950 isl_space *space_result = NULL;
8951 isl_basic_map *bmap;
8952 unsigned in1, in2, out, nparam, total, pos;
8953 struct isl_dim_map *dim_map1, *dim_map2;
8955 if (!bmap1 || !bmap2)
8958 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8959 isl_space_copy(bmap2->dim));
8961 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8962 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8963 out = isl_basic_map_dim(bmap1, isl_dim_out);
8964 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8966 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8967 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8968 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8969 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8970 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8971 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8972 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8973 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8974 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8975 isl_dim_map_div(dim_map1, bmap1, pos += out);
8976 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8978 bmap = isl_basic_map_alloc_space(space_result,
8979 bmap1->n_div + bmap2->n_div,
8980 bmap1->n_eq + bmap2->n_eq,
8981 bmap1->n_ineq + bmap2->n_ineq);
8982 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8983 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8984 bmap = isl_basic_map_simplify(bmap);
8985 return isl_basic_map_finalize(bmap);
8987 isl_basic_map_free(bmap1);
8988 isl_basic_map_free(bmap2);
8992 __isl_give isl_basic_map *isl_basic_map_range_product(
8993 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8995 isl_space *dim_result = NULL;
8996 isl_basic_map *bmap;
8997 unsigned in, out1, out2, nparam, total, pos;
8998 struct isl_dim_map *dim_map1, *dim_map2;
9000 if (!bmap1 || !bmap2)
9003 if (!isl_space_match(bmap1->dim, isl_dim_param,
9004 bmap2->dim, isl_dim_param))
9005 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9006 "parameters don't match", goto error);
9008 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9009 isl_space_copy(bmap2->dim));
9011 in = isl_basic_map_dim(bmap1, isl_dim_in);
9012 out1 = isl_basic_map_n_out(bmap1);
9013 out2 = isl_basic_map_n_out(bmap2);
9014 nparam = isl_basic_map_n_param(bmap1);
9016 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9017 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9018 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9019 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9020 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9021 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9022 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9023 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9024 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9025 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9026 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9028 bmap = isl_basic_map_alloc_space(dim_result,
9029 bmap1->n_div + bmap2->n_div,
9030 bmap1->n_eq + bmap2->n_eq,
9031 bmap1->n_ineq + bmap2->n_ineq);
9032 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9033 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9034 bmap = isl_basic_map_simplify(bmap);
9035 return isl_basic_map_finalize(bmap);
9037 isl_basic_map_free(bmap1);
9038 isl_basic_map_free(bmap2);
9042 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9043 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9045 isl_basic_map *prod;
9047 prod = isl_basic_map_range_product(bmap1, bmap2);
9048 prod = isl_basic_map_flatten_range(prod);
9052 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9053 __isl_take isl_map *map2,
9054 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9055 __isl_take isl_space *right),
9056 __isl_give isl_basic_map *(*basic_map_product)(
9057 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9060 struct isl_map *result;
9066 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9067 map2->dim, isl_dim_param), goto error);
9069 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9070 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9071 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9073 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9074 isl_space_copy(map2->dim)),
9075 map1->n * map2->n, flags);
9078 for (i = 0; i < map1->n; ++i)
9079 for (j = 0; j < map2->n; ++j) {
9080 struct isl_basic_map *part;
9081 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9082 isl_basic_map_copy(map2->p[j]));
9083 if (isl_basic_map_is_empty(part))
9084 isl_basic_map_free(part);
9086 result = isl_map_add_basic_map(result, part);
9099 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9101 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9102 __isl_take isl_map *map2)
9104 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9107 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9108 __isl_take isl_map *map2)
9110 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9113 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9115 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9116 __isl_take isl_map *map2)
9120 prod = isl_map_product(map1, map2);
9121 prod = isl_map_flatten(prod);
9125 /* Given two set A and B, construct its Cartesian product A x B.
9127 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9129 return isl_map_range_product(set1, set2);
9132 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9133 __isl_take isl_set *set2)
9135 return isl_map_flat_range_product(set1, set2);
9138 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9140 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9141 __isl_take isl_map *map2)
9143 return map_product(map1, map2, &isl_space_domain_product,
9144 &isl_basic_map_domain_product);
9147 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9149 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9150 __isl_take isl_map *map2)
9152 return map_product(map1, map2, &isl_space_range_product,
9153 &isl_basic_map_range_product);
9156 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9157 __isl_take isl_map *map2)
9159 return isl_map_align_params_map_map_and(map1, map2,
9160 &map_domain_product_aligned);
9163 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9164 __isl_take isl_map *map2)
9166 return isl_map_align_params_map_map_and(map1, map2,
9167 &map_range_product_aligned);
9170 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9172 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9173 __isl_take isl_map *map2)
9177 prod = isl_map_domain_product(map1, map2);
9178 prod = isl_map_flatten_domain(prod);
9182 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9184 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9185 __isl_take isl_map *map2)
9189 prod = isl_map_range_product(map1, map2);
9190 prod = isl_map_flatten_range(prod);
9194 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9197 uint32_t hash = isl_hash_init();
9202 bmap = isl_basic_map_copy(bmap);
9203 bmap = isl_basic_map_normalize(bmap);
9206 total = isl_basic_map_total_dim(bmap);
9207 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9208 for (i = 0; i < bmap->n_eq; ++i) {
9210 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9211 isl_hash_hash(hash, c_hash);
9213 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9214 for (i = 0; i < bmap->n_ineq; ++i) {
9216 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9217 isl_hash_hash(hash, c_hash);
9219 isl_hash_byte(hash, bmap->n_div & 0xFF);
9220 for (i = 0; i < bmap->n_div; ++i) {
9222 if (isl_int_is_zero(bmap->div[i][0]))
9224 isl_hash_byte(hash, i & 0xFF);
9225 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9226 isl_hash_hash(hash, c_hash);
9228 isl_basic_map_free(bmap);
9232 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9234 return isl_basic_map_get_hash((isl_basic_map *)bset);
9237 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9244 map = isl_map_copy(map);
9245 map = isl_map_normalize(map);
9249 hash = isl_hash_init();
9250 for (i = 0; i < map->n; ++i) {
9252 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9253 isl_hash_hash(hash, bmap_hash);
9261 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9263 return isl_map_get_hash((isl_map *)set);
9266 /* Check if the value for dimension dim is completely determined
9267 * by the values of the other parameters and variables.
9268 * That is, check if dimension dim is involved in an equality.
9270 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9277 nparam = isl_basic_set_n_param(bset);
9278 for (i = 0; i < bset->n_eq; ++i)
9279 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9284 /* Check if the value for dimension dim is completely determined
9285 * by the values of the other parameters and variables.
9286 * That is, check if dimension dim is involved in an equality
9287 * for each of the subsets.
9289 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9295 for (i = 0; i < set->n; ++i) {
9297 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9304 int isl_set_n_basic_set(__isl_keep isl_set *set)
9306 return set ? set->n : 0;
9309 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9310 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9317 for (i = 0; i < map->n; ++i)
9318 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9324 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9325 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9332 for (i = 0; i < set->n; ++i)
9333 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9339 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9346 bset = isl_basic_set_cow(bset);
9350 dim = isl_basic_set_get_space(bset);
9351 dim = isl_space_lift(dim, bset->n_div);
9354 isl_space_free(bset->dim);
9356 bset->extra -= bset->n_div;
9359 bset = isl_basic_set_finalize(bset);
9363 isl_basic_set_free(bset);
9367 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9373 set = isl_set_align_divs(set);
9378 set = isl_set_cow(set);
9382 n_div = set->p[0]->n_div;
9383 dim = isl_set_get_space(set);
9384 dim = isl_space_lift(dim, n_div);
9387 isl_space_free(set->dim);
9390 for (i = 0; i < set->n; ++i) {
9391 set->p[i] = isl_basic_set_lift(set->p[i]);
9402 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9405 struct isl_basic_map *bmap;
9412 set = isl_set_align_divs(set);
9417 dim = isl_set_get_space(set);
9418 if (set->n == 0 || set->p[0]->n_div == 0) {
9420 return isl_map_identity(isl_space_map_from_set(dim));
9423 n_div = set->p[0]->n_div;
9424 dim = isl_space_map_from_set(dim);
9425 n_param = isl_space_dim(dim, isl_dim_param);
9426 n_set = isl_space_dim(dim, isl_dim_in);
9427 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9428 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9429 for (i = 0; i < n_set; ++i)
9430 bmap = var_equal(bmap, i);
9432 total = n_param + n_set + n_set + n_div;
9433 for (i = 0; i < n_div; ++i) {
9434 k = isl_basic_map_alloc_inequality(bmap);
9437 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9438 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9439 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9440 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9441 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9442 set->p[0]->div[i][0]);
9444 l = isl_basic_map_alloc_inequality(bmap);
9447 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9448 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9449 set->p[0]->div[i][0]);
9450 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9454 bmap = isl_basic_map_simplify(bmap);
9455 bmap = isl_basic_map_finalize(bmap);
9456 return isl_map_from_basic_map(bmap);
9459 isl_basic_map_free(bmap);
9463 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9471 dim = isl_basic_set_total_dim(bset);
9472 size += bset->n_eq * (1 + dim);
9473 size += bset->n_ineq * (1 + dim);
9474 size += bset->n_div * (2 + dim);
9479 int isl_set_size(__isl_keep isl_set *set)
9487 for (i = 0; i < set->n; ++i)
9488 size += isl_basic_set_size(set->p[i]);
9493 /* Check if there is any lower bound (if lower == 0) and/or upper
9494 * bound (if upper == 0) on the specified dim.
9496 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9497 enum isl_dim_type type, unsigned pos, int lower, int upper)
9504 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9506 pos += isl_basic_map_offset(bmap, type);
9508 for (i = 0; i < bmap->n_div; ++i) {
9509 if (isl_int_is_zero(bmap->div[i][0]))
9511 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9515 for (i = 0; i < bmap->n_eq; ++i)
9516 if (!isl_int_is_zero(bmap->eq[i][pos]))
9519 for (i = 0; i < bmap->n_ineq; ++i) {
9520 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9527 return lower && upper;
9530 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9531 enum isl_dim_type type, unsigned pos)
9533 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9536 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9537 enum isl_dim_type type, unsigned pos)
9539 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9542 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9543 enum isl_dim_type type, unsigned pos)
9545 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9548 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9549 enum isl_dim_type type, unsigned pos)
9556 for (i = 0; i < map->n; ++i) {
9558 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9559 if (bounded < 0 || !bounded)
9566 /* Return 1 if the specified dim is involved in both an upper bound
9567 * and a lower bound.
9569 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9570 enum isl_dim_type type, unsigned pos)
9572 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9575 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9577 static int has_any_bound(__isl_keep isl_map *map,
9578 enum isl_dim_type type, unsigned pos,
9579 int (*fn)(__isl_keep isl_basic_map *bmap,
9580 enum isl_dim_type type, unsigned pos))
9587 for (i = 0; i < map->n; ++i) {
9589 bounded = fn(map->p[i], type, pos);
9590 if (bounded < 0 || bounded)
9597 /* Return 1 if the specified dim is involved in any lower bound.
9599 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9600 enum isl_dim_type type, unsigned pos)
9602 return has_any_bound(set, type, pos,
9603 &isl_basic_map_dim_has_lower_bound);
9606 /* Return 1 if the specified dim is involved in any upper bound.
9608 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9609 enum isl_dim_type type, unsigned pos)
9611 return has_any_bound(set, type, pos,
9612 &isl_basic_map_dim_has_upper_bound);
9615 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9617 static int has_bound(__isl_keep isl_map *map,
9618 enum isl_dim_type type, unsigned pos,
9619 int (*fn)(__isl_keep isl_basic_map *bmap,
9620 enum isl_dim_type type, unsigned pos))
9627 for (i = 0; i < map->n; ++i) {
9629 bounded = fn(map->p[i], type, pos);
9630 if (bounded < 0 || !bounded)
9637 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9639 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9640 enum isl_dim_type type, unsigned pos)
9642 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9645 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9647 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9648 enum isl_dim_type type, unsigned pos)
9650 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9653 /* For each of the "n" variables starting at "first", determine
9654 * the sign of the variable and put the results in the first "n"
9655 * elements of the array "signs".
9657 * 1 means that the variable is non-negative
9658 * -1 means that the variable is non-positive
9659 * 0 means the variable attains both positive and negative values.
9661 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9662 unsigned first, unsigned n, int *signs)
9664 isl_vec *bound = NULL;
9665 struct isl_tab *tab = NULL;
9666 struct isl_tab_undo *snap;
9669 if (!bset || !signs)
9672 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9673 tab = isl_tab_from_basic_set(bset, 0);
9677 isl_seq_clr(bound->el, bound->size);
9678 isl_int_set_si(bound->el[0], -1);
9680 snap = isl_tab_snap(tab);
9681 for (i = 0; i < n; ++i) {
9684 isl_int_set_si(bound->el[1 + first + i], -1);
9685 if (isl_tab_add_ineq(tab, bound->el) < 0)
9688 isl_int_set_si(bound->el[1 + first + i], 0);
9689 if (isl_tab_rollback(tab, snap) < 0)
9697 isl_int_set_si(bound->el[1 + first + i], 1);
9698 if (isl_tab_add_ineq(tab, bound->el) < 0)
9701 isl_int_set_si(bound->el[1 + first + i], 0);
9702 if (isl_tab_rollback(tab, snap) < 0)
9705 signs[i] = empty ? -1 : 0;
9709 isl_vec_free(bound);
9713 isl_vec_free(bound);
9717 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9718 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9720 if (!bset || !signs)
9722 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9725 first += pos(bset->dim, type) - 1;
9726 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9729 /* Check if the given basic map is obviously single-valued.
9730 * In particular, for each output dimension, check that there is
9731 * an equality that defines the output dimension in terms of
9732 * earlier dimensions.
9734 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9744 total = 1 + isl_basic_map_total_dim(bmap);
9745 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9746 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9748 for (i = 0; i < n_out; ++i) {
9749 for (j = 0; j < bmap->n_eq; ++j) {
9750 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9752 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9753 total - (o_out + i + 1)) == -1)
9756 if (j >= bmap->n_eq)
9763 /* Check if the given basic map is single-valued.
9768 * and check if the result is a subset of the identity mapping.
9770 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9773 isl_basic_map *test;
9777 sv = isl_basic_map_plain_is_single_valued(bmap);
9781 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9782 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9784 space = isl_basic_map_get_space(bmap);
9785 space = isl_space_map_from_set(isl_space_range(space));
9786 id = isl_basic_map_identity(space);
9788 sv = isl_basic_map_is_subset(test, id);
9790 isl_basic_map_free(test);
9791 isl_basic_map_free(id);
9796 /* Check if the given map is obviously single-valued.
9798 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9807 return isl_basic_map_plain_is_single_valued(map->p[0]);
9810 /* Check if the given map is single-valued.
9815 * and check if the result is a subset of the identity mapping.
9817 int isl_map_is_single_valued(__isl_keep isl_map *map)
9824 sv = isl_map_plain_is_single_valued(map);
9828 test = isl_map_reverse(isl_map_copy(map));
9829 test = isl_map_apply_range(test, isl_map_copy(map));
9831 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9832 id = isl_map_identity(dim);
9834 sv = isl_map_is_subset(test, id);
9842 int isl_map_is_injective(__isl_keep isl_map *map)
9846 map = isl_map_copy(map);
9847 map = isl_map_reverse(map);
9848 in = isl_map_is_single_valued(map);
9854 /* Check if the given map is obviously injective.
9856 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9860 map = isl_map_copy(map);
9861 map = isl_map_reverse(map);
9862 in = isl_map_plain_is_single_valued(map);
9868 int isl_map_is_bijective(__isl_keep isl_map *map)
9872 sv = isl_map_is_single_valued(map);
9876 return isl_map_is_injective(map);
9879 int isl_set_is_singleton(__isl_keep isl_set *set)
9881 return isl_map_is_single_valued((isl_map *)set);
9884 int isl_map_is_translation(__isl_keep isl_map *map)
9889 delta = isl_map_deltas(isl_map_copy(map));
9890 ok = isl_set_is_singleton(delta);
9891 isl_set_free(delta);
9896 static int unique(isl_int *p, unsigned pos, unsigned len)
9898 if (isl_seq_first_non_zero(p, pos) != -1)
9900 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9905 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9914 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9917 nvar = isl_basic_set_dim(bset, isl_dim_set);
9918 ovar = isl_space_offset(bset->dim, isl_dim_set);
9919 for (j = 0; j < nvar; ++j) {
9920 int lower = 0, upper = 0;
9921 for (i = 0; i < bset->n_eq; ++i) {
9922 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9924 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9930 for (i = 0; i < bset->n_ineq; ++i) {
9931 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
9933 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
9935 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
9940 if (!lower || !upper)
9947 int isl_set_is_box(__isl_keep isl_set *set)
9954 return isl_basic_set_is_box(set->p[0]);
9957 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
9962 return isl_space_is_wrapping(bset->dim);
9965 int isl_set_is_wrapping(__isl_keep isl_set *set)
9970 return isl_space_is_wrapping(set->dim);
9973 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
9975 bmap = isl_basic_map_cow(bmap);
9979 bmap->dim = isl_space_wrap(bmap->dim);
9983 bmap = isl_basic_map_finalize(bmap);
9985 return (isl_basic_set *)bmap;
9987 isl_basic_map_free(bmap);
9991 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9995 map = isl_map_cow(map);
9999 for (i = 0; i < map->n; ++i) {
10000 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10004 map->dim = isl_space_wrap(map->dim);
10008 return (isl_set *)map;
10014 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10016 bset = isl_basic_set_cow(bset);
10020 bset->dim = isl_space_unwrap(bset->dim);
10024 bset = isl_basic_set_finalize(bset);
10026 return (isl_basic_map *)bset;
10028 isl_basic_set_free(bset);
10032 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10039 if (!isl_set_is_wrapping(set))
10040 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10043 set = isl_set_cow(set);
10047 for (i = 0; i < set->n; ++i) {
10048 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10053 set->dim = isl_space_unwrap(set->dim);
10057 return (isl_map *)set;
10063 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10064 enum isl_dim_type type)
10069 if (!isl_space_is_named_or_nested(bmap->dim, type))
10072 bmap = isl_basic_map_cow(bmap);
10076 bmap->dim = isl_space_reset(bmap->dim, type);
10080 bmap = isl_basic_map_finalize(bmap);
10084 isl_basic_map_free(bmap);
10088 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10089 enum isl_dim_type type)
10096 if (!isl_space_is_named_or_nested(map->dim, type))
10099 map = isl_map_cow(map);
10103 for (i = 0; i < map->n; ++i) {
10104 map->p[i] = isl_basic_map_reset(map->p[i], type);
10108 map->dim = isl_space_reset(map->dim, type);
10118 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10123 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10126 bmap = isl_basic_map_cow(bmap);
10130 bmap->dim = isl_space_flatten(bmap->dim);
10134 bmap = isl_basic_map_finalize(bmap);
10138 isl_basic_map_free(bmap);
10142 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10144 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10147 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10148 __isl_take isl_basic_map *bmap)
10153 if (!bmap->dim->nested[0])
10156 bmap = isl_basic_map_cow(bmap);
10160 bmap->dim = isl_space_flatten_domain(bmap->dim);
10164 bmap = isl_basic_map_finalize(bmap);
10168 isl_basic_map_free(bmap);
10172 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10173 __isl_take isl_basic_map *bmap)
10178 if (!bmap->dim->nested[1])
10181 bmap = isl_basic_map_cow(bmap);
10185 bmap->dim = isl_space_flatten_range(bmap->dim);
10189 bmap = isl_basic_map_finalize(bmap);
10193 isl_basic_map_free(bmap);
10197 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10204 if (!map->dim->nested[0] && !map->dim->nested[1])
10207 map = isl_map_cow(map);
10211 for (i = 0; i < map->n; ++i) {
10212 map->p[i] = isl_basic_map_flatten(map->p[i]);
10216 map->dim = isl_space_flatten(map->dim);
10226 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10228 return (isl_set *)isl_map_flatten((isl_map *)set);
10231 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10233 isl_space *dim, *flat_dim;
10236 dim = isl_set_get_space(set);
10237 flat_dim = isl_space_flatten(isl_space_copy(dim));
10238 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10239 map = isl_map_intersect_domain(map, set);
10244 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10251 if (!map->dim->nested[0])
10254 map = isl_map_cow(map);
10258 for (i = 0; i < map->n; ++i) {
10259 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10263 map->dim = isl_space_flatten_domain(map->dim);
10273 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10280 if (!map->dim->nested[1])
10283 map = isl_map_cow(map);
10287 for (i = 0; i < map->n; ++i) {
10288 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10292 map->dim = isl_space_flatten_range(map->dim);
10302 /* Reorder the dimensions of "bmap" according to the given dim_map
10303 * and set the dimension specification to "dim".
10305 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10306 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10308 isl_basic_map *res;
10311 bmap = isl_basic_map_cow(bmap);
10312 if (!bmap || !dim || !dim_map)
10315 flags = bmap->flags;
10316 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10317 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10318 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10319 res = isl_basic_map_alloc_space(dim,
10320 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10321 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10323 res->flags = flags;
10324 res = isl_basic_map_finalize(res);
10328 isl_basic_map_free(bmap);
10329 isl_space_free(dim);
10333 /* Reorder the dimensions of "map" according to given reordering.
10335 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10336 __isl_take isl_reordering *r)
10339 struct isl_dim_map *dim_map;
10341 map = isl_map_cow(map);
10342 dim_map = isl_dim_map_from_reordering(r);
10343 if (!map || !r || !dim_map)
10346 for (i = 0; i < map->n; ++i) {
10347 struct isl_dim_map *dim_map_i;
10349 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10351 map->p[i] = isl_basic_map_realign(map->p[i],
10352 isl_space_copy(r->dim), dim_map_i);
10358 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10360 isl_reordering_free(r);
10366 isl_reordering_free(r);
10370 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10371 __isl_take isl_reordering *r)
10373 return (isl_set *)isl_map_realign((isl_map *)set, r);
10376 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10377 __isl_take isl_space *model)
10381 if (!map || !model)
10384 ctx = isl_space_get_ctx(model);
10385 if (!isl_space_has_named_params(model))
10386 isl_die(ctx, isl_error_invalid,
10387 "model has unnamed parameters", goto error);
10388 if (!isl_space_has_named_params(map->dim))
10389 isl_die(ctx, isl_error_invalid,
10390 "relation has unnamed parameters", goto error);
10391 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10392 isl_reordering *exp;
10394 model = isl_space_drop_dims(model, isl_dim_in,
10395 0, isl_space_dim(model, isl_dim_in));
10396 model = isl_space_drop_dims(model, isl_dim_out,
10397 0, isl_space_dim(model, isl_dim_out));
10398 exp = isl_parameter_alignment_reordering(map->dim, model);
10399 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10400 map = isl_map_realign(map, exp);
10403 isl_space_free(model);
10406 isl_space_free(model);
10411 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10412 __isl_take isl_space *model)
10414 return isl_map_align_params(set, model);
10417 /* Align the parameters of "bmap" to those of "model", introducing
10418 * additional parameters if needed.
10420 __isl_give isl_basic_map *isl_basic_map_align_params(
10421 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10425 if (!bmap || !model)
10428 ctx = isl_space_get_ctx(model);
10429 if (!isl_space_has_named_params(model))
10430 isl_die(ctx, isl_error_invalid,
10431 "model has unnamed parameters", goto error);
10432 if (!isl_space_has_named_params(bmap->dim))
10433 isl_die(ctx, isl_error_invalid,
10434 "relation has unnamed parameters", goto error);
10435 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10436 isl_reordering *exp;
10437 struct isl_dim_map *dim_map;
10439 model = isl_space_drop_dims(model, isl_dim_in,
10440 0, isl_space_dim(model, isl_dim_in));
10441 model = isl_space_drop_dims(model, isl_dim_out,
10442 0, isl_space_dim(model, isl_dim_out));
10443 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10444 exp = isl_reordering_extend_space(exp,
10445 isl_basic_map_get_space(bmap));
10446 dim_map = isl_dim_map_from_reordering(exp);
10447 bmap = isl_basic_map_realign(bmap,
10448 exp ? isl_space_copy(exp->dim) : NULL,
10449 isl_dim_map_extend(dim_map, bmap));
10450 isl_reordering_free(exp);
10454 isl_space_free(model);
10457 isl_space_free(model);
10458 isl_basic_map_free(bmap);
10462 /* Align the parameters of "bset" to those of "model", introducing
10463 * additional parameters if needed.
10465 __isl_give isl_basic_set *isl_basic_set_align_params(
10466 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10468 return isl_basic_map_align_params(bset, model);
10471 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10472 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10473 enum isl_dim_type c2, enum isl_dim_type c3,
10474 enum isl_dim_type c4, enum isl_dim_type c5)
10476 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10477 struct isl_mat *mat;
10483 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10484 isl_basic_map_total_dim(bmap) + 1);
10487 for (i = 0; i < bmap->n_eq; ++i)
10488 for (j = 0, pos = 0; j < 5; ++j) {
10489 int off = isl_basic_map_offset(bmap, c[j]);
10490 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10491 isl_int_set(mat->row[i][pos],
10492 bmap->eq[i][off + k]);
10500 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10501 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10502 enum isl_dim_type c2, enum isl_dim_type c3,
10503 enum isl_dim_type c4, enum isl_dim_type c5)
10505 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10506 struct isl_mat *mat;
10512 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10513 isl_basic_map_total_dim(bmap) + 1);
10516 for (i = 0; i < bmap->n_ineq; ++i)
10517 for (j = 0, pos = 0; j < 5; ++j) {
10518 int off = isl_basic_map_offset(bmap, c[j]);
10519 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10520 isl_int_set(mat->row[i][pos],
10521 bmap->ineq[i][off + k]);
10529 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10530 __isl_take isl_space *dim,
10531 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10532 enum isl_dim_type c2, enum isl_dim_type c3,
10533 enum isl_dim_type c4, enum isl_dim_type c5)
10535 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10536 isl_basic_map *bmap;
10542 if (!dim || !eq || !ineq)
10545 if (eq->n_col != ineq->n_col)
10546 isl_die(dim->ctx, isl_error_invalid,
10547 "equalities and inequalities matrices should have "
10548 "same number of columns", goto error);
10550 total = 1 + isl_space_dim(dim, isl_dim_all);
10552 if (eq->n_col < total)
10553 isl_die(dim->ctx, isl_error_invalid,
10554 "number of columns too small", goto error);
10556 extra = eq->n_col - total;
10558 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10559 eq->n_row, ineq->n_row);
10562 for (i = 0; i < extra; ++i) {
10563 k = isl_basic_map_alloc_div(bmap);
10566 isl_int_set_si(bmap->div[k][0], 0);
10568 for (i = 0; i < eq->n_row; ++i) {
10569 l = isl_basic_map_alloc_equality(bmap);
10572 for (j = 0, pos = 0; j < 5; ++j) {
10573 int off = isl_basic_map_offset(bmap, c[j]);
10574 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10575 isl_int_set(bmap->eq[l][off + k],
10581 for (i = 0; i < ineq->n_row; ++i) {
10582 l = isl_basic_map_alloc_inequality(bmap);
10585 for (j = 0, pos = 0; j < 5; ++j) {
10586 int off = isl_basic_map_offset(bmap, c[j]);
10587 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10588 isl_int_set(bmap->ineq[l][off + k],
10589 ineq->row[i][pos]);
10595 isl_space_free(dim);
10597 isl_mat_free(ineq);
10599 bmap = isl_basic_map_simplify(bmap);
10600 return isl_basic_map_finalize(bmap);
10602 isl_space_free(dim);
10604 isl_mat_free(ineq);
10608 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10609 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10610 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10612 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10613 c1, c2, c3, c4, isl_dim_in);
10616 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10617 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10618 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10620 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10621 c1, c2, c3, c4, isl_dim_in);
10624 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10625 __isl_take isl_space *dim,
10626 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10627 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10629 return (isl_basic_set*)
10630 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10631 c1, c2, c3, c4, isl_dim_in);
10634 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10639 return isl_space_can_zip(bmap->dim);
10642 int isl_map_can_zip(__isl_keep isl_map *map)
10647 return isl_space_can_zip(map->dim);
10650 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10651 * (A -> C) -> (B -> D).
10653 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10662 if (!isl_basic_map_can_zip(bmap))
10663 isl_die(bmap->ctx, isl_error_invalid,
10664 "basic map cannot be zipped", goto error);
10665 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10666 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10667 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10668 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10669 bmap = isl_basic_map_cow(bmap);
10670 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10673 bmap->dim = isl_space_zip(bmap->dim);
10678 isl_basic_map_free(bmap);
10682 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10683 * (A -> C) -> (B -> D).
10685 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10692 if (!isl_map_can_zip(map))
10693 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10696 map = isl_map_cow(map);
10700 for (i = 0; i < map->n; ++i) {
10701 map->p[i] = isl_basic_map_zip(map->p[i]);
10706 map->dim = isl_space_zip(map->dim);
10716 /* Can we apply isl_basic_map_curry to "bmap"?
10717 * That is, does it have a nested relation in its domain?
10719 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10724 return isl_space_can_curry(bmap->dim);
10727 /* Can we apply isl_map_curry to "map"?
10728 * That is, does it have a nested relation in its domain?
10730 int isl_map_can_curry(__isl_keep isl_map *map)
10735 return isl_space_can_curry(map->dim);
10738 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10741 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10747 if (!isl_basic_map_can_curry(bmap))
10748 isl_die(bmap->ctx, isl_error_invalid,
10749 "basic map cannot be curried", goto error);
10750 bmap = isl_basic_map_cow(bmap);
10753 bmap->dim = isl_space_curry(bmap->dim);
10758 isl_basic_map_free(bmap);
10762 /* Given a map (A -> B) -> C, return the corresponding map
10765 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10772 if (!isl_map_can_curry(map))
10773 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10776 map = isl_map_cow(map);
10780 for (i = 0; i < map->n; ++i) {
10781 map->p[i] = isl_basic_map_curry(map->p[i]);
10786 map->dim = isl_space_curry(map->dim);
10796 /* Can we apply isl_basic_map_uncurry to "bmap"?
10797 * That is, does it have a nested relation in its domain?
10799 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
10804 return isl_space_can_uncurry(bmap->dim);
10807 /* Can we apply isl_map_uncurry to "map"?
10808 * That is, does it have a nested relation in its domain?
10810 int isl_map_can_uncurry(__isl_keep isl_map *map)
10815 return isl_space_can_uncurry(map->dim);
10818 /* Given a basic map A -> (B -> C), return the corresponding basic map
10821 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
10827 if (!isl_basic_map_can_uncurry(bmap))
10828 isl_die(bmap->ctx, isl_error_invalid,
10829 "basic map cannot be uncurried",
10830 return isl_basic_map_free(bmap));
10831 bmap = isl_basic_map_cow(bmap);
10834 bmap->dim = isl_space_uncurry(bmap->dim);
10836 return isl_basic_map_free(bmap);
10840 /* Given a map A -> (B -> C), return the corresponding map
10843 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
10850 if (!isl_map_can_uncurry(map))
10851 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
10852 return isl_map_free(map));
10854 map = isl_map_cow(map);
10858 for (i = 0; i < map->n; ++i) {
10859 map->p[i] = isl_basic_map_uncurry(map->p[i]);
10861 return isl_map_free(map);
10864 map->dim = isl_space_uncurry(map->dim);
10866 return isl_map_free(map);
10871 /* Construct a basic map mapping the domain of the affine expression
10872 * to a one-dimensional range prescribed by the affine expression.
10874 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
10878 isl_local_space *ls;
10879 isl_basic_map *bmap;
10884 ls = isl_aff_get_local_space(aff);
10885 bmap = isl_basic_map_from_local_space(ls);
10886 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
10887 k = isl_basic_map_alloc_equality(bmap);
10891 pos = isl_basic_map_offset(bmap, isl_dim_out);
10892 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
10893 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
10894 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
10895 aff->v->size - (pos + 1));
10898 bmap = isl_basic_map_finalize(bmap);
10902 isl_basic_map_free(bmap);
10906 /* Construct a map mapping the domain of the affine expression
10907 * to a one-dimensional range prescribed by the affine expression.
10909 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
10911 isl_basic_map *bmap;
10913 bmap = isl_basic_map_from_aff(aff);
10914 return isl_map_from_basic_map(bmap);
10917 /* Construct a basic map mapping the domain the multi-affine expression
10918 * to its range, with each dimension in the range equated to the
10919 * corresponding affine expression.
10921 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
10922 __isl_take isl_multi_aff *maff)
10926 isl_basic_map *bmap;
10931 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
10932 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
10933 "invalid space", return isl_multi_aff_free(maff));
10935 space = isl_space_domain(isl_multi_aff_get_space(maff));
10936 bmap = isl_basic_map_universe(isl_space_from_domain(space));
10938 for (i = 0; i < maff->n; ++i) {
10940 isl_basic_map *bmap_i;
10942 aff = isl_aff_copy(maff->p[i]);
10943 bmap_i = isl_basic_map_from_aff(aff);
10945 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10948 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
10950 isl_multi_aff_free(maff);
10954 /* Construct a map mapping the domain the multi-affine expression
10955 * to its range, with each dimension in the range equated to the
10956 * corresponding affine expression.
10958 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
10960 isl_basic_map *bmap;
10962 bmap = isl_basic_map_from_multi_aff(maff);
10963 return isl_map_from_basic_map(bmap);
10966 /* Construct a basic map mapping a domain in the given space to
10967 * to an n-dimensional range, with n the number of elements in the list,
10968 * where each coordinate in the range is prescribed by the
10969 * corresponding affine expression.
10970 * The domains of all affine expressions in the list are assumed to match
10973 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
10974 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
10978 isl_basic_map *bmap;
10983 dim = isl_space_from_domain(domain_dim);
10984 bmap = isl_basic_map_universe(dim);
10986 for (i = 0; i < list->n; ++i) {
10988 isl_basic_map *bmap_i;
10990 aff = isl_aff_copy(list->p[i]);
10991 bmap_i = isl_basic_map_from_aff(aff);
10993 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10996 isl_aff_list_free(list);
11000 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11001 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11003 return isl_map_equate(set, type1, pos1, type2, pos2);
11006 /* Construct a basic map where the given dimensions are equal to each other.
11008 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11009 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11011 isl_basic_map *bmap = NULL;
11017 if (pos1 >= isl_space_dim(space, type1))
11018 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11019 "index out of bounds", goto error);
11020 if (pos2 >= isl_space_dim(space, type2))
11021 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11022 "index out of bounds", goto error);
11024 if (type1 == type2 && pos1 == pos2)
11025 return isl_basic_map_universe(space);
11027 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11028 i = isl_basic_map_alloc_equality(bmap);
11031 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11032 pos1 += isl_basic_map_offset(bmap, type1);
11033 pos2 += isl_basic_map_offset(bmap, type2);
11034 isl_int_set_si(bmap->eq[i][pos1], -1);
11035 isl_int_set_si(bmap->eq[i][pos2], 1);
11036 bmap = isl_basic_map_finalize(bmap);
11037 isl_space_free(space);
11040 isl_space_free(space);
11041 isl_basic_map_free(bmap);
11045 /* Add a constraint imposing that the given two dimensions are equal.
11047 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11048 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11052 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11054 bmap = isl_basic_map_intersect(bmap, eq);
11059 /* Add a constraint imposing that the given two dimensions are equal.
11061 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11062 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11064 isl_basic_map *bmap;
11066 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11068 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11073 /* Add a constraint imposing that the given two dimensions have opposite values.
11075 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11076 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11078 isl_basic_map *bmap = NULL;
11084 if (pos1 >= isl_map_dim(map, type1))
11085 isl_die(map->ctx, isl_error_invalid,
11086 "index out of bounds", goto error);
11087 if (pos2 >= isl_map_dim(map, type2))
11088 isl_die(map->ctx, isl_error_invalid,
11089 "index out of bounds", goto error);
11091 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11092 i = isl_basic_map_alloc_equality(bmap);
11095 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11096 pos1 += isl_basic_map_offset(bmap, type1);
11097 pos2 += isl_basic_map_offset(bmap, type2);
11098 isl_int_set_si(bmap->eq[i][pos1], 1);
11099 isl_int_set_si(bmap->eq[i][pos2], 1);
11100 bmap = isl_basic_map_finalize(bmap);
11102 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11106 isl_basic_map_free(bmap);
11111 /* Add a constraint imposing that the value of the first dimension is
11112 * greater than or equal to that of the second.
11114 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11115 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11118 isl_local_space *ls;
11123 if (pos1 >= isl_basic_map_dim(bmap, type1))
11124 isl_die(bmap->ctx, isl_error_invalid,
11125 "index out of bounds", return isl_basic_map_free(bmap));
11126 if (pos2 >= isl_basic_map_dim(bmap, type2))
11127 isl_die(bmap->ctx, isl_error_invalid,
11128 "index out of bounds", return isl_basic_map_free(bmap));
11130 if (type1 == type2 && pos1 == pos2)
11133 ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
11134 c = isl_inequality_alloc(ls);
11135 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11136 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11137 bmap = isl_basic_map_add_constraint(bmap, c);
11142 /* Construct a basic map where the value of the first dimension is
11143 * greater than that of the second.
11145 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11146 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11148 isl_basic_map *bmap = NULL;
11154 if (pos1 >= isl_space_dim(space, type1))
11155 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11156 "index out of bounds", goto error);
11157 if (pos2 >= isl_space_dim(space, type2))
11158 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11159 "index out of bounds", goto error);
11161 if (type1 == type2 && pos1 == pos2)
11162 return isl_basic_map_empty(space);
11164 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11165 i = isl_basic_map_alloc_inequality(bmap);
11168 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11169 pos1 += isl_basic_map_offset(bmap, type1);
11170 pos2 += isl_basic_map_offset(bmap, type2);
11171 isl_int_set_si(bmap->ineq[i][pos1], 1);
11172 isl_int_set_si(bmap->ineq[i][pos2], -1);
11173 isl_int_set_si(bmap->ineq[i][0], -1);
11174 bmap = isl_basic_map_finalize(bmap);
11178 isl_space_free(space);
11179 isl_basic_map_free(bmap);
11183 /* Add a constraint imposing that the value of the first dimension is
11184 * greater than that of the second.
11186 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11187 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11191 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11193 bmap = isl_basic_map_intersect(bmap, gt);
11198 /* Add a constraint imposing that the value of the first dimension is
11199 * greater than that of the second.
11201 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11202 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11204 isl_basic_map *bmap;
11206 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11208 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11213 /* Add a constraint imposing that the value of the first dimension is
11214 * smaller than that of the second.
11216 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11217 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11219 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11222 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11226 isl_local_space *ls;
11231 if (!isl_basic_map_divs_known(bmap))
11232 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11233 "some divs are unknown", return NULL);
11235 ls = isl_basic_map_get_local_space(bmap);
11236 div = isl_local_space_get_div(ls, pos);
11237 isl_local_space_free(ls);
11242 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11245 return isl_basic_map_get_div(bset, pos);
11248 /* Plug in "subs" for dimension "type", "pos" of "bset".
11250 * Let i be the dimension to replace and let "subs" be of the form
11254 * Any integer division with a non-zero coefficient for i,
11256 * floor((a i + g)/m)
11260 * floor((a f + d g)/(m d))
11262 * Constraints of the form
11270 * We currently require that "subs" is an integral expression.
11271 * Handling rational expressions may require us to add stride constraints
11272 * as we do in isl_basic_set_preimage_multi_aff.
11274 __isl_give isl_basic_set *isl_basic_set_substitute(
11275 __isl_take isl_basic_set *bset,
11276 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11282 if (bset && isl_basic_set_plain_is_empty(bset))
11285 bset = isl_basic_set_cow(bset);
11286 if (!bset || !subs)
11289 ctx = isl_basic_set_get_ctx(bset);
11290 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11291 isl_die(ctx, isl_error_invalid,
11292 "spaces don't match", goto error);
11293 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11294 isl_die(ctx, isl_error_unsupported,
11295 "cannot handle divs yet", goto error);
11296 if (!isl_int_is_one(subs->v->el[0]))
11297 isl_die(ctx, isl_error_invalid,
11298 "can only substitute integer expressions", goto error);
11300 pos += isl_basic_set_offset(bset, type);
11304 for (i = 0; i < bset->n_eq; ++i) {
11305 if (isl_int_is_zero(bset->eq[i][pos]))
11307 isl_int_set(v, bset->eq[i][pos]);
11308 isl_int_set_si(bset->eq[i][pos], 0);
11309 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11310 v, subs->v->el + 1, subs->v->size - 1);
11313 for (i = 0; i < bset->n_ineq; ++i) {
11314 if (isl_int_is_zero(bset->ineq[i][pos]))
11316 isl_int_set(v, bset->ineq[i][pos]);
11317 isl_int_set_si(bset->ineq[i][pos], 0);
11318 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11319 v, subs->v->el + 1, subs->v->size - 1);
11322 for (i = 0; i < bset->n_div; ++i) {
11323 if (isl_int_is_zero(bset->div[i][1 + pos]))
11325 isl_int_set(v, bset->div[i][1 + pos]);
11326 isl_int_set_si(bset->div[i][1 + pos], 0);
11327 isl_seq_combine(bset->div[i] + 1,
11328 subs->v->el[0], bset->div[i] + 1,
11329 v, subs->v->el + 1, subs->v->size - 1);
11330 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11335 bset = isl_basic_set_simplify(bset);
11336 return isl_basic_set_finalize(bset);
11338 isl_basic_set_free(bset);
11342 /* Plug in "subs" for dimension "type", "pos" of "set".
11344 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11345 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11349 if (set && isl_set_plain_is_empty(set))
11352 set = isl_set_cow(set);
11356 for (i = set->n - 1; i >= 0; --i) {
11357 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11358 if (remove_if_empty(set, i) < 0)
11368 /* Check if the range of "ma" is compatible with the domain or range
11369 * (depending on "type") of "bmap".
11370 * Return -1 if anything is wrong.
11372 static int check_basic_map_compatible_range_multi_aff(
11373 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11374 __isl_keep isl_multi_aff *ma)
11377 isl_space *ma_space;
11379 ma_space = isl_multi_aff_get_space(ma);
11380 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11381 isl_space_free(ma_space);
11383 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11384 "spaces don't match", return -1);
11388 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11389 * coefficients before the transformed range of dimensions,
11390 * the "n_after" coefficients after the transformed range of dimensions
11391 * and the coefficients of the other divs in "bmap".
11393 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11394 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11399 isl_local_space *ls;
11404 ls = isl_aff_get_domain_local_space(ma->p[0]);
11408 n_param = isl_local_space_dim(ls, isl_dim_param);
11409 n_set = isl_local_space_dim(ls, isl_dim_set);
11410 for (i = 0; i < n_div; ++i) {
11411 int o_bmap = 0, o_ls = 0;
11413 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11414 o_bmap += 1 + 1 + n_param;
11415 o_ls += 1 + 1 + n_param;
11416 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11417 o_bmap += n_before;
11418 isl_seq_cpy(bmap->div[i] + o_bmap,
11419 ls->div->row[i] + o_ls, n_set);
11422 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11424 isl_seq_cpy(bmap->div[i] + o_bmap,
11425 ls->div->row[i] + o_ls, n_div);
11428 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11429 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11433 isl_local_space_free(ls);
11436 isl_local_space_free(ls);
11440 /* How many stride constraints does "ma" enforce?
11441 * That is, how many of the affine expressions have a denominator
11442 * different from one?
11444 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11449 for (i = 0; i < ma->n; ++i)
11450 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11456 /* For each affine expression in ma of the form
11458 * x_i = (f_i y + h_i)/m_i
11460 * with m_i different from one, add a constraint to "bmap"
11463 * f_i y + h_i = m_i alpha_i
11465 * with alpha_i an additional existentially quantified variable.
11467 static __isl_give isl_basic_map *add_ma_strides(
11468 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11469 int n_before, int n_after)
11478 total = isl_basic_map_total_dim(bmap);
11479 n_param = isl_multi_aff_dim(ma, isl_dim_param);
11480 n_in = isl_multi_aff_dim(ma, isl_dim_in);
11481 n_div = isl_multi_aff_dim(ma, isl_dim_div);
11482 for (i = 0; i < ma->n; ++i) {
11483 int o_bmap = 0, o_ma = 1;
11485 if (isl_int_is_one(ma->p[i]->v->el[0]))
11487 div = isl_basic_map_alloc_div(bmap);
11488 k = isl_basic_map_alloc_equality(bmap);
11489 if (div < 0 || k < 0)
11491 isl_int_set_si(bmap->div[div][0], 0);
11492 isl_seq_cpy(bmap->eq[k] + o_bmap,
11493 ma->p[i]->v->el + o_ma, 1 + n_param);
11494 o_bmap += 1 + n_param;
11495 o_ma += 1 + n_param;
11496 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11497 o_bmap += n_before;
11498 isl_seq_cpy(bmap->eq[k] + o_bmap,
11499 ma->p[i]->v->el + o_ma, n_in);
11502 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11504 isl_seq_cpy(bmap->eq[k] + o_bmap,
11505 ma->p[i]->v->el + o_ma, n_div);
11508 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11509 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11515 isl_basic_map_free(bmap);
11519 /* Replace the domain or range space (depending on "type) of "space" by "set".
11521 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11522 enum isl_dim_type type, __isl_take isl_space *set)
11524 if (type == isl_dim_in) {
11525 space = isl_space_range(space);
11526 space = isl_space_map_from_domain_and_range(set, space);
11528 space = isl_space_domain(space);
11529 space = isl_space_map_from_domain_and_range(space, set);
11535 /* Compute the preimage of the domain or range (depending on "type")
11536 * of "bmap" under the function represented by "ma".
11537 * In other words, plug in "ma" in the domain or range of "bmap".
11538 * The result is a basic map that lives in the same space as "bmap"
11539 * except that the domain or range has been replaced by
11540 * the domain space of "ma".
11542 * If bmap is represented by
11544 * A(p) + S u + B x + T v + C(divs) >= 0,
11546 * where u and x are input and output dimensions if type == isl_dim_out
11547 * while x and v are input and output dimensions if type == isl_dim_in,
11548 * and ma is represented by
11550 * x = D(p) + F(y) + G(divs')
11552 * then the result is
11554 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11556 * The divs in the input set are similarly adjusted.
11559 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11563 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11564 * B_i G(divs') + c_i(divs))/n_i)
11566 * If bmap is not a rational map and if F(y) involves any denominators
11568 * x_i = (f_i y + h_i)/m_i
11570 * then additional constraints are added to ensure that we only
11571 * map back integer points. That is we enforce
11573 * f_i y + h_i = m_i alpha_i
11575 * with alpha_i an additional existentially quantified variable.
11577 * We first copy over the divs from "ma".
11578 * Then we add the modified constraints and divs from "bmap".
11579 * Finally, we add the stride constraints, if needed.
11581 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
11582 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
11583 __isl_take isl_multi_aff *ma)
11587 isl_basic_map *res = NULL;
11588 int n_before, n_after, n_div_bmap, n_div_ma;
11589 isl_int f, c1, c2, g;
11590 int rational, strides;
11597 ma = isl_multi_aff_align_divs(ma);
11600 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
11603 if (type == isl_dim_in) {
11605 n_after = isl_basic_map_dim(bmap, isl_dim_out);
11607 n_before = isl_basic_map_dim(bmap, isl_dim_in);
11610 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
11611 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11613 space = isl_multi_aff_get_domain_space(ma);
11614 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
11615 rational = isl_basic_map_is_rational(bmap);
11616 strides = rational ? 0 : multi_aff_strides(ma);
11617 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
11618 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
11620 res = isl_basic_map_set_rational(res);
11622 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
11623 if (isl_basic_map_alloc_div(res) < 0)
11626 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
11629 for (i = 0; i < bmap->n_eq; ++i) {
11630 k = isl_basic_map_alloc_equality(res);
11633 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
11634 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11637 for (i = 0; i < bmap->n_ineq; ++i) {
11638 k = isl_basic_map_alloc_inequality(res);
11641 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
11642 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11645 for (i = 0; i < bmap->n_div; ++i) {
11646 if (isl_int_is_zero(bmap->div[i][0])) {
11647 isl_int_set_si(res->div[n_div_ma + i][0], 0);
11650 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
11651 n_before, n_after, n_div_ma, n_div_bmap,
11656 res = add_ma_strides(res, ma, n_before, n_after);
11662 isl_basic_map_free(bmap);
11663 isl_multi_aff_free(ma);
11664 res = isl_basic_set_simplify(res);
11665 return isl_basic_map_finalize(res);
11671 isl_basic_map_free(bmap);
11672 isl_multi_aff_free(ma);
11673 isl_basic_map_free(res);
11677 /* Compute the preimage of "bset" under the function represented by "ma".
11678 * In other words, plug in "ma" in "bset". The result is a basic set
11679 * that lives in the domain space of "ma".
11681 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11682 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11684 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
11687 /* Check if the range of "ma" is compatible with the domain or range
11688 * (depending on "type") of "map".
11689 * Return -1 if anything is wrong.
11691 static int check_map_compatible_range_multi_aff(
11692 __isl_keep isl_map *map, enum isl_dim_type type,
11693 __isl_keep isl_multi_aff *ma)
11696 isl_space *ma_space;
11698 ma_space = isl_multi_aff_get_space(ma);
11699 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
11700 isl_space_free(ma_space);
11702 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11703 "spaces don't match", return -1);
11707 /* Compute the preimage of the domain or range (depending on "type")
11708 * of "map" under the function represented by "ma".
11709 * In other words, plug in "ma" in the domain or range of "map".
11710 * The result is a map that lives in the same space as "map"
11711 * except that the domain or range has been replaced by
11712 * the domain space of "ma".
11714 * The parameters are assumed to have been aligned.
11716 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
11717 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11722 map = isl_map_cow(map);
11723 ma = isl_multi_aff_align_divs(ma);
11726 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
11729 for (i = 0; i < map->n; ++i) {
11730 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
11731 isl_multi_aff_copy(ma));
11736 space = isl_multi_aff_get_domain_space(ma);
11737 space = isl_space_set(isl_map_get_space(map), type, space);
11739 isl_space_free(map->dim);
11744 isl_multi_aff_free(ma);
11746 ISL_F_CLR(map, ISL_MAP_DISJOINT);
11747 ISL_F_CLR(map, ISL_SET_NORMALIZED);
11750 isl_multi_aff_free(ma);
11755 /* Compute the preimage of the domain or range (depending on "type")
11756 * of "map" under the function represented by "ma".
11757 * In other words, plug in "ma" in the domain or range of "map".
11758 * The result is a map that lives in the same space as "map"
11759 * except that the domain or range has been replaced by
11760 * the domain space of "ma".
11762 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
11763 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11768 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
11769 return map_preimage_multi_aff(map, type, ma);
11771 if (!isl_space_has_named_params(map->dim) ||
11772 !isl_space_has_named_params(ma->space))
11773 isl_die(map->ctx, isl_error_invalid,
11774 "unaligned unnamed parameters", goto error);
11775 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
11776 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
11778 return map_preimage_multi_aff(map, type, ma);
11780 isl_multi_aff_free(ma);
11781 return isl_map_free(map);
11784 /* Compute the preimage of "set" under the function represented by "ma".
11785 * In other words, plug in "ma" "set". The result is a set
11786 * that lives in the domain space of "ma".
11788 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
11789 __isl_take isl_multi_aff *ma)
11791 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
11794 /* Compute the preimage of the domain of "map" under the function
11795 * represented by "ma".
11796 * In other words, plug in "ma" in the domain of "map".
11797 * The result is a map that lives in the same space as "map"
11798 * except that the domain has been replaced by the domain space of "ma".
11800 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
11801 __isl_take isl_multi_aff *ma)
11803 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
11806 /* Compute the preimage of "set" under the function represented by "pma".
11807 * In other words, plug in "pma" in "set. The result is a set
11808 * that lives in the domain space of "pma".
11810 static __isl_give isl_set *set_preimage_pw_multi_aff(__isl_take isl_set *set,
11811 __isl_take isl_pw_multi_aff *pma)
11820 isl_pw_multi_aff_free(pma);
11821 res = isl_set_empty(isl_set_get_space(set));
11826 res = isl_set_preimage_multi_aff(isl_set_copy(set),
11827 isl_multi_aff_copy(pma->p[0].maff));
11828 res = isl_set_intersect(res, isl_set_copy(pma->p[0].set));
11830 for (i = 1; i < pma->n; ++i) {
11833 res_i = isl_set_preimage_multi_aff(isl_set_copy(set),
11834 isl_multi_aff_copy(pma->p[i].maff));
11835 res_i = isl_set_intersect(res_i, isl_set_copy(pma->p[i].set));
11836 res = isl_set_union(res, res_i);
11839 isl_pw_multi_aff_free(pma);
11843 isl_pw_multi_aff_free(pma);
11848 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
11849 __isl_take isl_pw_multi_aff *pma)
11854 if (isl_space_match(set->dim, isl_dim_param, pma->dim, isl_dim_param))
11855 return set_preimage_pw_multi_aff(set, pma);
11857 if (!isl_space_has_named_params(set->dim) ||
11858 !isl_space_has_named_params(pma->dim))
11859 isl_die(set->ctx, isl_error_invalid,
11860 "unaligned unnamed parameters", goto error);
11861 set = isl_set_align_params(set, isl_pw_multi_aff_get_space(pma));
11862 pma = isl_pw_multi_aff_align_params(pma, isl_set_get_space(set));
11864 return set_preimage_pw_multi_aff(set, pma);
11866 isl_pw_multi_aff_free(pma);
11867 return isl_set_free(set);