2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 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"
21 #include <isl_list_private.h>
26 #include "isl_map_piplib.h"
27 #include <isl_reordering.h>
28 #include "isl_sample.h"
31 #include <isl_mat_private.h>
32 #include <isl_dim_map.h>
33 #include <isl_local_space_private.h>
34 #include <isl_aff_private.h>
35 #include <isl_options_private.h>
37 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
40 case isl_dim_param: return dim->nparam;
41 case isl_dim_in: return dim->n_in;
42 case isl_dim_out: return dim->n_out;
43 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
48 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
51 case isl_dim_param: return 1;
52 case isl_dim_in: return 1 + dim->nparam;
53 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
58 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
59 enum isl_dim_type type)
64 case isl_dim_cst: return 1;
67 case isl_dim_out: return isl_space_dim(bmap->dim, type);
68 case isl_dim_div: return bmap->n_div;
69 case isl_dim_all: return isl_basic_map_total_dim(bmap);
74 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
76 return map ? n(map->dim, type) : 0;
79 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
81 return set ? n(set->dim, type) : 0;
84 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
85 enum isl_dim_type type)
87 isl_space *dim = bmap->dim;
89 case isl_dim_cst: return 0;
90 case isl_dim_param: return 1;
91 case isl_dim_in: return 1 + dim->nparam;
92 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
93 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
98 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
99 enum isl_dim_type type)
101 return isl_basic_map_offset(bset, type);
104 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
106 return pos(map->dim, type);
109 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
110 enum isl_dim_type type)
112 return isl_basic_map_dim(bset, type);
115 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
117 return isl_basic_set_dim(bset, isl_dim_set);
120 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
122 return isl_basic_set_dim(bset, isl_dim_param);
125 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
129 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
132 unsigned isl_set_n_dim(__isl_keep isl_set *set)
134 return isl_set_dim(set, isl_dim_set);
137 unsigned isl_set_n_param(__isl_keep isl_set *set)
139 return isl_set_dim(set, isl_dim_param);
142 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
144 return bmap ? bmap->dim->n_in : 0;
147 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
149 return bmap ? bmap->dim->n_out : 0;
152 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
154 return bmap ? bmap->dim->nparam : 0;
157 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
159 return bmap ? bmap->n_div : 0;
162 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
164 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
167 unsigned isl_map_n_in(const struct isl_map *map)
169 return map ? map->dim->n_in : 0;
172 unsigned isl_map_n_out(const struct isl_map *map)
174 return map ? map->dim->n_out : 0;
177 unsigned isl_map_n_param(const struct isl_map *map)
179 return map ? map->dim->nparam : 0;
182 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
187 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
190 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
193 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
194 struct isl_basic_set *bset)
199 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
202 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
205 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
210 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
213 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
216 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
217 struct isl_basic_set *bset)
222 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
225 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
228 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
230 return bmap ? bmap->ctx : NULL;
233 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
235 return bset ? bset->ctx : NULL;
238 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
240 return map ? map->ctx : NULL;
243 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
245 return set ? set->ctx : NULL;
248 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
252 return isl_space_copy(bmap->dim);
255 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
259 return isl_space_copy(bset->dim);
262 /* Extract the divs in "bmap" as a matrix.
264 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
275 ctx = isl_basic_map_get_ctx(bmap);
276 total = isl_space_dim(bmap->dim, isl_dim_all);
277 cols = 1 + 1 + total + bmap->n_div;
278 div = isl_mat_alloc(ctx, bmap->n_div, cols);
282 for (i = 0; i < bmap->n_div; ++i)
283 isl_seq_cpy(div->row[i], bmap->div[i], cols);
288 __isl_give isl_local_space *isl_basic_map_get_local_space(
289 __isl_keep isl_basic_map *bmap)
296 div = isl_basic_map_get_divs(bmap);
297 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
300 __isl_give isl_local_space *isl_basic_set_get_local_space(
301 __isl_keep isl_basic_set *bset)
303 return isl_basic_map_get_local_space(bset);
306 __isl_give isl_basic_map *isl_basic_map_from_local_space(
307 __isl_take isl_local_space *ls)
316 n_div = isl_local_space_dim(ls, isl_dim_div);
317 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
318 n_div, 0, 2 * n_div);
320 for (i = 0; i < n_div; ++i)
321 if (isl_basic_map_alloc_div(bmap) < 0)
324 for (i = 0; i < n_div; ++i) {
325 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
326 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
330 isl_local_space_free(ls);
333 isl_local_space_free(ls);
334 isl_basic_map_free(bmap);
338 __isl_give isl_basic_set *isl_basic_set_from_local_space(
339 __isl_take isl_local_space *ls)
341 return isl_basic_map_from_local_space(ls);
344 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
348 return isl_space_copy(map->dim);
351 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
355 return isl_space_copy(set->dim);
358 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
359 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
361 bmap = isl_basic_map_cow(bmap);
364 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
367 bmap = isl_basic_map_finalize(bmap);
370 isl_basic_map_free(bmap);
374 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
375 __isl_take isl_basic_set *bset, const char *s)
377 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
380 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
381 enum isl_dim_type type)
383 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
386 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
387 enum isl_dim_type type, const char *s)
391 map = isl_map_cow(map);
395 map->dim = isl_space_set_tuple_name(map->dim, type, s);
399 for (i = 0; i < map->n; ++i) {
400 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
411 /* Does the input or output tuple have a name?
413 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
415 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
418 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
419 enum isl_dim_type type)
421 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
424 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
427 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
430 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
431 enum isl_dim_type type, __isl_take isl_id *id)
433 map = isl_map_cow(map);
435 return isl_id_free(id);
437 map->dim = isl_space_set_tuple_id(map->dim, type, id);
439 return isl_map_reset_space(map, isl_space_copy(map->dim));
442 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
443 __isl_take isl_id *id)
445 return isl_map_set_tuple_id(set, isl_dim_set, id);
448 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
449 enum isl_dim_type type)
451 map = isl_map_cow(map);
455 map->dim = isl_space_reset_tuple_id(map->dim, type);
457 return isl_map_reset_space(map, isl_space_copy(map->dim));
460 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
462 return isl_map_reset_tuple_id(set, isl_dim_set);
465 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
467 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
470 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
471 enum isl_dim_type type)
473 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
476 int isl_set_has_tuple_id(__isl_keep isl_set *set)
478 return isl_map_has_tuple_id(set, isl_dim_set);
481 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
483 return isl_map_get_tuple_id(set, isl_dim_set);
486 /* Does the set tuple have a name?
488 int isl_set_has_tuple_name(__isl_keep isl_set *set)
490 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
494 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
496 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
499 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
501 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
504 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
505 enum isl_dim_type type, unsigned pos)
507 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
510 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
511 enum isl_dim_type type, unsigned pos)
513 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
516 /* Does the given dimension have a name?
518 int isl_map_has_dim_name(__isl_keep isl_map *map,
519 enum isl_dim_type type, unsigned pos)
521 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
524 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
525 enum isl_dim_type type, unsigned pos)
527 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
530 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
531 enum isl_dim_type type, unsigned pos)
533 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
536 /* Does the given dimension have a name?
538 int isl_set_has_dim_name(__isl_keep isl_set *set,
539 enum isl_dim_type type, unsigned pos)
541 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
544 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
545 __isl_take isl_basic_map *bmap,
546 enum isl_dim_type type, unsigned pos, const char *s)
548 bmap = isl_basic_map_cow(bmap);
551 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
554 return isl_basic_map_finalize(bmap);
556 isl_basic_map_free(bmap);
560 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
561 enum isl_dim_type type, unsigned pos, const char *s)
565 map = isl_map_cow(map);
569 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
573 for (i = 0; i < map->n; ++i) {
574 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
585 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
586 __isl_take isl_basic_set *bset,
587 enum isl_dim_type type, unsigned pos, const char *s)
589 return (isl_basic_set *)isl_basic_map_set_dim_name(
590 (isl_basic_map *)bset, type, pos, s);
593 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
594 enum isl_dim_type type, unsigned pos, const char *s)
596 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
599 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
600 enum isl_dim_type type, unsigned pos)
602 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
605 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
606 enum isl_dim_type type, unsigned pos)
608 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
611 int isl_map_has_dim_id(__isl_keep isl_map *map,
612 enum isl_dim_type type, unsigned pos)
614 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
617 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
618 enum isl_dim_type type, unsigned pos)
620 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
623 int isl_set_has_dim_id(__isl_keep isl_set *set,
624 enum isl_dim_type type, unsigned pos)
626 return isl_map_has_dim_id(set, type, pos);
629 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
630 enum isl_dim_type type, unsigned pos)
632 return isl_map_get_dim_id(set, type, pos);
635 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
636 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
638 map = isl_map_cow(map);
640 return isl_id_free(id);
642 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
644 return isl_map_reset_space(map, isl_space_copy(map->dim));
647 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
648 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
650 return isl_map_set_dim_id(set, type, pos, id);
653 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
654 __isl_keep isl_id *id)
658 return isl_space_find_dim_by_id(map->dim, type, id);
661 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
662 __isl_keep isl_id *id)
664 return isl_map_find_dim_by_id(set, type, id);
667 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
672 return isl_space_find_dim_by_name(map->dim, type, name);
675 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
678 return isl_map_find_dim_by_name(set, type, name);
681 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
685 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
688 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
690 return isl_basic_map_is_rational(bset);
693 /* Does "bmap" contain any rational points?
695 * If "bmap" has an equality for each dimension, equating the dimension
696 * to an integer constant, then it has no rational points, even if it
697 * is marked as rational.
699 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
701 int has_rational = 1;
706 if (isl_basic_map_plain_is_empty(bmap))
708 if (!isl_basic_map_is_rational(bmap))
710 bmap = isl_basic_map_copy(bmap);
711 bmap = isl_basic_map_implicit_equalities(bmap);
714 total = isl_basic_map_total_dim(bmap);
715 if (bmap->n_eq == total) {
717 for (i = 0; i < bmap->n_eq; ++i) {
718 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
721 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
722 !isl_int_is_negone(bmap->eq[i][1 + j]))
724 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
732 isl_basic_map_free(bmap);
737 /* Does "map" contain any rational points?
739 int isl_map_has_rational(__isl_keep isl_map *map)
746 for (i = 0; i < map->n; ++i) {
747 has_rational = isl_basic_map_has_rational(map->p[i]);
748 if (has_rational < 0)
756 /* Does "set" contain any rational points?
758 int isl_set_has_rational(__isl_keep isl_set *set)
760 return isl_map_has_rational(set);
763 /* Is this basic set a parameter domain?
765 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
769 return isl_space_is_params(bset->dim);
772 /* Is this set a parameter domain?
774 int isl_set_is_params(__isl_keep isl_set *set)
778 return isl_space_is_params(set->dim);
781 /* Is this map actually a parameter domain?
782 * Users should never call this function. Outside of isl,
783 * a map can never be a parameter domain.
785 int isl_map_is_params(__isl_keep isl_map *map)
789 return isl_space_is_params(map->dim);
792 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
793 struct isl_basic_map *bmap, unsigned extra,
794 unsigned n_eq, unsigned n_ineq)
797 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
802 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
803 if (isl_blk_is_error(bmap->block))
806 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
811 bmap->block2 = isl_blk_empty();
814 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
815 if (isl_blk_is_error(bmap->block2))
818 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
823 for (i = 0; i < n_ineq + n_eq; ++i)
824 bmap->ineq[i] = bmap->block.data + i * row_size;
826 for (i = 0; i < extra; ++i)
827 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
831 bmap->c_size = n_eq + n_ineq;
832 bmap->eq = bmap->ineq + n_ineq;
841 isl_basic_map_free(bmap);
845 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
846 unsigned nparam, unsigned dim, unsigned extra,
847 unsigned n_eq, unsigned n_ineq)
849 struct isl_basic_map *bmap;
852 space = isl_space_set_alloc(ctx, nparam, dim);
856 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
857 return (struct isl_basic_set *)bmap;
860 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
861 unsigned extra, unsigned n_eq, unsigned n_ineq)
863 struct isl_basic_map *bmap;
866 isl_assert(dim->ctx, dim->n_in == 0, goto error);
867 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
868 return (struct isl_basic_set *)bmap;
874 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
875 unsigned extra, unsigned n_eq, unsigned n_ineq)
877 struct isl_basic_map *bmap;
881 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
886 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
892 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
893 unsigned nparam, unsigned in, unsigned out, unsigned extra,
894 unsigned n_eq, unsigned n_ineq)
896 struct isl_basic_map *bmap;
899 dim = isl_space_alloc(ctx, nparam, in, out);
903 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
907 static void dup_constraints(
908 struct isl_basic_map *dst, struct isl_basic_map *src)
911 unsigned total = isl_basic_map_total_dim(src);
913 for (i = 0; i < src->n_eq; ++i) {
914 int j = isl_basic_map_alloc_equality(dst);
915 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
918 for (i = 0; i < src->n_ineq; ++i) {
919 int j = isl_basic_map_alloc_inequality(dst);
920 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
923 for (i = 0; i < src->n_div; ++i) {
924 int j = isl_basic_map_alloc_div(dst);
925 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
927 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
930 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
932 struct isl_basic_map *dup;
936 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
937 bmap->n_div, bmap->n_eq, bmap->n_ineq);
940 dup_constraints(dup, bmap);
941 dup->flags = bmap->flags;
942 dup->sample = isl_vec_copy(bmap->sample);
946 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
948 struct isl_basic_map *dup;
950 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
951 return (struct isl_basic_set *)dup;
954 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
959 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
963 return isl_basic_set_dup(bset);
966 struct isl_set *isl_set_copy(struct isl_set *set)
975 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
980 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
984 bmap = isl_basic_map_dup(bmap);
986 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
990 struct isl_map *isl_map_copy(struct isl_map *map)
999 void *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1004 if (--bmap->ref > 0)
1007 isl_ctx_deref(bmap->ctx);
1009 isl_blk_free(bmap->ctx, bmap->block2);
1011 isl_blk_free(bmap->ctx, bmap->block);
1012 isl_vec_free(bmap->sample);
1013 isl_space_free(bmap->dim);
1019 void *isl_basic_set_free(struct isl_basic_set *bset)
1021 return isl_basic_map_free((struct isl_basic_map *)bset);
1024 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1026 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1029 __isl_give isl_map *isl_map_align_params_map_map_and(
1030 __isl_take isl_map *map1, __isl_take isl_map *map2,
1031 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1032 __isl_take isl_map *map2))
1036 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1037 return fn(map1, map2);
1038 if (!isl_space_has_named_params(map1->dim) ||
1039 !isl_space_has_named_params(map2->dim))
1040 isl_die(map1->ctx, isl_error_invalid,
1041 "unaligned unnamed parameters", goto error);
1042 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1043 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1044 return fn(map1, map2);
1051 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1052 __isl_keep isl_map *map2,
1053 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1059 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1060 return fn(map1, map2);
1061 if (!isl_space_has_named_params(map1->dim) ||
1062 !isl_space_has_named_params(map2->dim))
1063 isl_die(map1->ctx, isl_error_invalid,
1064 "unaligned unnamed parameters", return -1);
1065 map1 = isl_map_copy(map1);
1066 map2 = isl_map_copy(map2);
1067 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1068 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1075 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1077 struct isl_ctx *ctx;
1081 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1082 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1084 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1085 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1086 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1087 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1088 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1089 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1091 int j = isl_basic_map_alloc_inequality(bmap);
1095 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1096 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1103 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1104 bmap->extra - bmap->n_div);
1105 return bmap->n_eq++;
1108 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1110 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1113 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1117 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1122 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1124 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1127 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1132 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1134 if (pos != bmap->n_eq - 1) {
1136 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1137 bmap->eq[bmap->n_eq - 1] = t;
1143 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1145 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1148 /* Turn inequality "pos" of "bmap" into an equality.
1150 * In particular, we move the inequality in front of the equalities
1151 * and move the last inequality in the position of the moved inequality.
1152 * Note that isl_tab_make_equalities_explicit depends on this particular
1153 * change in the ordering of the constraints.
1155 void isl_basic_map_inequality_to_equality(
1156 struct isl_basic_map *bmap, unsigned pos)
1160 t = bmap->ineq[pos];
1161 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1162 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1167 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1168 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1169 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1170 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1173 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1175 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1178 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1180 struct isl_ctx *ctx;
1184 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1185 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1186 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1187 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1188 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1189 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1190 1 + isl_basic_map_total_dim(bmap),
1191 bmap->extra - bmap->n_div);
1192 return bmap->n_ineq++;
1195 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1197 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1200 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1204 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1209 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1211 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1214 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1219 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1221 if (pos != bmap->n_ineq - 1) {
1222 t = bmap->ineq[pos];
1223 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1224 bmap->ineq[bmap->n_ineq - 1] = t;
1225 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1231 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1233 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1236 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1241 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1244 k = isl_basic_map_alloc_equality(bmap);
1247 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1250 isl_basic_map_free(bmap);
1254 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1257 return (isl_basic_set *)
1258 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1261 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1266 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1269 k = isl_basic_map_alloc_inequality(bmap);
1272 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1275 isl_basic_map_free(bmap);
1279 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1282 return (isl_basic_set *)
1283 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1286 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1290 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1291 isl_seq_clr(bmap->div[bmap->n_div] +
1292 1 + 1 + isl_basic_map_total_dim(bmap),
1293 bmap->extra - bmap->n_div);
1294 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1295 return bmap->n_div++;
1298 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1300 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1303 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1307 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1312 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1314 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1317 /* Copy constraint from src to dst, putting the vars of src at offset
1318 * dim_off in dst and the divs of src at offset div_off in dst.
1319 * If both sets are actually map, then dim_off applies to the input
1322 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1323 struct isl_basic_map *src_map, isl_int *src,
1324 unsigned in_off, unsigned out_off, unsigned div_off)
1326 unsigned src_nparam = isl_basic_map_n_param(src_map);
1327 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1328 unsigned src_in = isl_basic_map_n_in(src_map);
1329 unsigned dst_in = isl_basic_map_n_in(dst_map);
1330 unsigned src_out = isl_basic_map_n_out(src_map);
1331 unsigned dst_out = isl_basic_map_n_out(dst_map);
1332 isl_int_set(dst[0], src[0]);
1333 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1334 if (dst_nparam > src_nparam)
1335 isl_seq_clr(dst+1+src_nparam,
1336 dst_nparam - src_nparam);
1337 isl_seq_clr(dst+1+dst_nparam, in_off);
1338 isl_seq_cpy(dst+1+dst_nparam+in_off,
1340 isl_min(dst_in-in_off, src_in));
1341 if (dst_in-in_off > src_in)
1342 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1343 dst_in - in_off - src_in);
1344 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1345 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1346 src+1+src_nparam+src_in,
1347 isl_min(dst_out-out_off, src_out));
1348 if (dst_out-out_off > src_out)
1349 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1350 dst_out - out_off - src_out);
1351 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1352 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1353 src+1+src_nparam+src_in+src_out,
1354 isl_min(dst_map->extra-div_off, src_map->n_div));
1355 if (dst_map->n_div-div_off > src_map->n_div)
1356 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1357 div_off+src_map->n_div,
1358 dst_map->n_div - div_off - src_map->n_div);
1361 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1362 struct isl_basic_map *src_map, isl_int *src,
1363 unsigned in_off, unsigned out_off, unsigned div_off)
1365 isl_int_set(dst[0], src[0]);
1366 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1369 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1370 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1375 if (!bmap1 || !bmap2)
1378 div_off = bmap1->n_div;
1380 for (i = 0; i < bmap2->n_eq; ++i) {
1381 int i1 = isl_basic_map_alloc_equality(bmap1);
1384 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1385 i_pos, o_pos, div_off);
1388 for (i = 0; i < bmap2->n_ineq; ++i) {
1389 int i1 = isl_basic_map_alloc_inequality(bmap1);
1392 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1393 i_pos, o_pos, div_off);
1396 for (i = 0; i < bmap2->n_div; ++i) {
1397 int i1 = isl_basic_map_alloc_div(bmap1);
1400 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1401 i_pos, o_pos, div_off);
1404 isl_basic_map_free(bmap2);
1409 isl_basic_map_free(bmap1);
1410 isl_basic_map_free(bmap2);
1414 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1415 struct isl_basic_set *bset2, unsigned pos)
1417 return (struct isl_basic_set *)
1418 add_constraints((struct isl_basic_map *)bset1,
1419 (struct isl_basic_map *)bset2, 0, pos);
1422 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1423 __isl_take isl_space *dim, unsigned extra,
1424 unsigned n_eq, unsigned n_ineq)
1426 struct isl_basic_map *ext;
1436 dims_ok = isl_space_is_equal(base->dim, dim) &&
1437 base->extra >= base->n_div + extra;
1439 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1440 room_for_ineq(base, n_ineq)) {
1441 isl_space_free(dim);
1445 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1446 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1447 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1448 extra += base->extra;
1450 n_ineq += base->n_ineq;
1452 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1458 ext->sample = isl_vec_copy(base->sample);
1459 flags = base->flags;
1460 ext = add_constraints(ext, base, 0, 0);
1463 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1469 isl_space_free(dim);
1470 isl_basic_map_free(base);
1474 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1475 __isl_take isl_space *dim, unsigned extra,
1476 unsigned n_eq, unsigned n_ineq)
1478 return (struct isl_basic_set *)
1479 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1480 extra, n_eq, n_ineq);
1483 struct isl_basic_map *isl_basic_map_extend_constraints(
1484 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1488 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1492 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1493 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1494 unsigned n_eq, unsigned n_ineq)
1496 struct isl_basic_map *bmap;
1501 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1505 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1508 isl_basic_map_free(base);
1512 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1513 unsigned nparam, unsigned dim, unsigned extra,
1514 unsigned n_eq, unsigned n_ineq)
1516 return (struct isl_basic_set *)
1517 isl_basic_map_extend((struct isl_basic_map *)base,
1518 nparam, 0, dim, extra, n_eq, n_ineq);
1521 struct isl_basic_set *isl_basic_set_extend_constraints(
1522 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1524 return (struct isl_basic_set *)
1525 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1529 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1531 return (struct isl_basic_set *)
1532 isl_basic_map_cow((struct isl_basic_map *)bset);
1535 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1540 if (bmap->ref > 1) {
1542 bmap = isl_basic_map_dup(bmap);
1545 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1549 struct isl_set *isl_set_cow(struct isl_set *set)
1557 return isl_set_dup(set);
1560 struct isl_map *isl_map_cow(struct isl_map *map)
1568 return isl_map_dup(map);
1571 static void swap_vars(struct isl_blk blk, isl_int *a,
1572 unsigned a_len, unsigned b_len)
1574 isl_seq_cpy(blk.data, a+a_len, b_len);
1575 isl_seq_cpy(blk.data+b_len, a, a_len);
1576 isl_seq_cpy(a, blk.data, b_len+a_len);
1579 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1580 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1588 isl_assert(bmap->ctx,
1589 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1591 if (n1 == 0 || n2 == 0)
1594 bmap = isl_basic_map_cow(bmap);
1598 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1599 if (isl_blk_is_error(blk))
1602 for (i = 0; i < bmap->n_eq; ++i)
1604 bmap->eq[i] + pos, n1, n2);
1606 for (i = 0; i < bmap->n_ineq; ++i)
1608 bmap->ineq[i] + pos, n1, n2);
1610 for (i = 0; i < bmap->n_div; ++i)
1612 bmap->div[i]+1 + pos, n1, n2);
1614 isl_blk_free(bmap->ctx, blk);
1616 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1617 bmap = isl_basic_map_gauss(bmap, NULL);
1618 return isl_basic_map_finalize(bmap);
1620 isl_basic_map_free(bmap);
1624 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1625 __isl_take isl_basic_set *bset, unsigned n)
1633 nparam = isl_basic_set_n_param(bset);
1634 dim = isl_basic_set_n_dim(bset);
1635 isl_assert(bset->ctx, n <= dim, goto error);
1637 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1639 isl_basic_set_free(bset);
1643 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1649 total = isl_basic_map_total_dim(bmap);
1650 isl_basic_map_free_div(bmap, bmap->n_div);
1651 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1653 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1655 i = isl_basic_map_alloc_equality(bmap);
1659 isl_int_set_si(bmap->eq[i][0], 1);
1660 isl_seq_clr(bmap->eq[i]+1, total);
1661 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1662 isl_vec_free(bmap->sample);
1663 bmap->sample = NULL;
1664 return isl_basic_map_finalize(bmap);
1666 isl_basic_map_free(bmap);
1670 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1672 return (struct isl_basic_set *)
1673 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1676 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1679 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1680 isl_int *t = bmap->div[a];
1681 bmap->div[a] = bmap->div[b];
1684 for (i = 0; i < bmap->n_eq; ++i)
1685 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1687 for (i = 0; i < bmap->n_ineq; ++i)
1688 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1690 for (i = 0; i < bmap->n_div; ++i)
1691 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1692 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1695 /* Eliminate the specified n dimensions starting at first from the
1696 * constraints, without removing the dimensions from the space.
1697 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1699 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1700 enum isl_dim_type type, unsigned first, unsigned n)
1709 if (first + n > isl_map_dim(map, type) || first + n < first)
1710 isl_die(map->ctx, isl_error_invalid,
1711 "index out of bounds", goto error);
1713 map = isl_map_cow(map);
1717 for (i = 0; i < map->n; ++i) {
1718 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1728 /* Eliminate the specified n dimensions starting at first from the
1729 * constraints, without removing the dimensions from the space.
1730 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1732 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1733 enum isl_dim_type type, unsigned first, unsigned n)
1735 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1738 /* Eliminate the specified n dimensions starting at first from the
1739 * constraints, without removing the dimensions from the space.
1740 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1742 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1743 unsigned first, unsigned n)
1745 return isl_set_eliminate(set, isl_dim_set, first, n);
1748 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1749 __isl_take isl_basic_map *bmap)
1753 bmap = isl_basic_map_eliminate_vars(bmap,
1754 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1758 return isl_basic_map_finalize(bmap);
1761 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1762 __isl_take isl_basic_set *bset)
1764 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1765 (struct isl_basic_map *)bset);
1768 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1777 map = isl_map_cow(map);
1781 for (i = 0; i < map->n; ++i) {
1782 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1792 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1794 return isl_map_remove_divs(set);
1797 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1798 enum isl_dim_type type, unsigned first, unsigned n)
1802 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1804 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1806 bmap = isl_basic_map_eliminate_vars(bmap,
1807 isl_basic_map_offset(bmap, type) - 1 + first, n);
1810 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1812 bmap = isl_basic_map_drop(bmap, type, first, n);
1815 isl_basic_map_free(bmap);
1819 /* Return true if the definition of the given div (recursively) involves
1820 * any of the given variables.
1822 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1823 unsigned first, unsigned n)
1826 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1828 if (isl_int_is_zero(bmap->div[div][0]))
1830 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1833 for (i = bmap->n_div - 1; i >= 0; --i) {
1834 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1836 if (div_involves_vars(bmap, i, first, n))
1843 /* Try and add a lower and/or upper bound on "div" to "bmap"
1844 * based on inequality "i".
1845 * "total" is the total number of variables (excluding the divs).
1846 * "v" is a temporary object that can be used during the calculations.
1847 * If "lb" is set, then a lower bound should be constructed.
1848 * If "ub" is set, then an upper bound should be constructed.
1850 * The calling function has already checked that the inequality does not
1851 * reference "div", but we still need to check that the inequality is
1852 * of the right form. We'll consider the case where we want to construct
1853 * a lower bound. The construction of upper bounds is similar.
1855 * Let "div" be of the form
1857 * q = floor((a + f(x))/d)
1859 * We essentially check if constraint "i" is of the form
1863 * so that we can use it to derive a lower bound on "div".
1864 * However, we allow a slightly more general form
1868 * with the condition that the coefficients of g(x) - f(x) are all
1870 * Rewriting this constraint as
1874 * adding a + f(x) to both sides and dividing by d, we obtain
1876 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1878 * Taking the floor on both sides, we obtain
1880 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1884 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1886 * In the case of an upper bound, we construct the constraint
1888 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1891 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1892 __isl_take isl_basic_map *bmap, int div, int i,
1893 unsigned total, isl_int v, int lb, int ub)
1897 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1899 isl_int_sub(v, bmap->ineq[i][1 + j],
1900 bmap->div[div][1 + 1 + j]);
1901 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1904 isl_int_add(v, bmap->ineq[i][1 + j],
1905 bmap->div[div][1 + 1 + j]);
1906 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1912 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1914 int k = isl_basic_map_alloc_inequality(bmap);
1917 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1918 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1919 bmap->div[div][1 + j]);
1920 isl_int_cdiv_q(bmap->ineq[k][j],
1921 bmap->ineq[k][j], bmap->div[div][0]);
1923 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1926 int k = isl_basic_map_alloc_inequality(bmap);
1929 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1930 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1931 bmap->div[div][1 + j]);
1932 isl_int_fdiv_q(bmap->ineq[k][j],
1933 bmap->ineq[k][j], bmap->div[div][0]);
1935 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1940 isl_basic_map_free(bmap);
1944 /* This function is called right before "div" is eliminated from "bmap"
1945 * using Fourier-Motzkin.
1946 * Look through the constraints of "bmap" for constraints on the argument
1947 * of the integer division and use them to construct constraints on the
1948 * integer division itself. These constraints can then be combined
1949 * during the Fourier-Motzkin elimination.
1950 * Note that it is only useful to introduce lower bounds on "div"
1951 * if "bmap" already contains upper bounds on "div" as the newly
1952 * introduce lower bounds can then be combined with the pre-existing
1953 * upper bounds. Similarly for upper bounds.
1954 * We therefore first check if "bmap" contains any lower and/or upper bounds
1957 * It is interesting to note that the introduction of these constraints
1958 * can indeed lead to more accurate results, even when compared to
1959 * deriving constraints on the argument of "div" from constraints on "div".
1960 * Consider, for example, the set
1962 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1964 * The second constraint can be rewritten as
1966 * 2 * [(-i-2j+3)/4] + k >= 0
1968 * from which we can derive
1970 * -i - 2j + 3 >= -2k
1976 * Combined with the first constraint, we obtain
1978 * -3 <= 3 + 2k or k >= -3
1980 * If, on the other hand we derive a constraint on [(i+2j)/4] from
1981 * the first constraint, we obtain
1983 * [(i + 2j)/4] >= [-3/4] = -1
1985 * Combining this constraint with the second constraint, we obtain
1989 static __isl_give isl_basic_map *insert_bounds_on_div(
1990 __isl_take isl_basic_map *bmap, int div)
1993 int check_lb, check_ub;
2000 if (isl_int_is_zero(bmap->div[div][0]))
2003 total = isl_space_dim(bmap->dim, isl_dim_all);
2007 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2008 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2015 if (!check_lb && !check_ub)
2020 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2021 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2024 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2025 check_lb, check_ub);
2033 /* Remove all divs (recursively) involving any of the given dimensions
2034 * in their definitions.
2036 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2037 __isl_take isl_basic_map *bmap,
2038 enum isl_dim_type type, unsigned first, unsigned n)
2044 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2046 first += isl_basic_map_offset(bmap, type);
2048 for (i = bmap->n_div - 1; i >= 0; --i) {
2049 if (!div_involves_vars(bmap, i, first, n))
2051 bmap = insert_bounds_on_div(bmap, i);
2052 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2060 isl_basic_map_free(bmap);
2064 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2065 __isl_take isl_basic_set *bset,
2066 enum isl_dim_type type, unsigned first, unsigned n)
2068 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2071 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2072 enum isl_dim_type type, unsigned first, unsigned n)
2081 map = isl_map_cow(map);
2085 for (i = 0; i < map->n; ++i) {
2086 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2097 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2098 enum isl_dim_type type, unsigned first, unsigned n)
2100 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2104 /* Does the desciption of "bmap" depend on the specified dimensions?
2105 * We also check whether the dimensions appear in any of the div definitions.
2106 * In principle there is no need for this check. If the dimensions appear
2107 * in a div definition, they also appear in the defining constraints of that
2110 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2111 enum isl_dim_type type, unsigned first, unsigned n)
2118 if (first + n > isl_basic_map_dim(bmap, type))
2119 isl_die(bmap->ctx, isl_error_invalid,
2120 "index out of bounds", return -1);
2122 first += isl_basic_map_offset(bmap, type);
2123 for (i = 0; i < bmap->n_eq; ++i)
2124 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2126 for (i = 0; i < bmap->n_ineq; ++i)
2127 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2129 for (i = 0; i < bmap->n_div; ++i) {
2130 if (isl_int_is_zero(bmap->div[i][0]))
2132 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2139 int isl_map_involves_dims(__isl_keep isl_map *map,
2140 enum isl_dim_type type, unsigned first, unsigned n)
2147 if (first + n > isl_map_dim(map, type))
2148 isl_die(map->ctx, isl_error_invalid,
2149 "index out of bounds", return -1);
2151 for (i = 0; i < map->n; ++i) {
2152 int involves = isl_basic_map_involves_dims(map->p[i],
2154 if (involves < 0 || involves)
2161 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2162 enum isl_dim_type type, unsigned first, unsigned n)
2164 return isl_basic_map_involves_dims(bset, type, first, n);
2167 int isl_set_involves_dims(__isl_keep isl_set *set,
2168 enum isl_dim_type type, unsigned first, unsigned n)
2170 return isl_map_involves_dims(set, type, first, n);
2173 /* Return true if the definition of the given div is unknown or depends
2176 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2179 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2181 if (isl_int_is_zero(bmap->div[div][0]))
2184 for (i = bmap->n_div - 1; i >= 0; --i) {
2185 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2187 if (div_is_unknown(bmap, i))
2194 /* Remove all divs that are unknown or defined in terms of unknown divs.
2196 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2197 __isl_take isl_basic_map *bmap)
2204 for (i = bmap->n_div - 1; i >= 0; --i) {
2205 if (!div_is_unknown(bmap, i))
2207 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2216 /* Remove all divs that are unknown or defined in terms of unknown divs.
2218 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2219 __isl_take isl_basic_set *bset)
2221 return isl_basic_map_remove_unknown_divs(bset);
2224 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2233 map = isl_map_cow(map);
2237 for (i = 0; i < map->n; ++i) {
2238 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2248 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2250 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2253 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2254 __isl_take isl_basic_set *bset,
2255 enum isl_dim_type type, unsigned first, unsigned n)
2257 return (isl_basic_set *)
2258 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2261 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2262 enum isl_dim_type type, unsigned first, unsigned n)
2269 map = isl_map_cow(map);
2272 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2274 for (i = 0; i < map->n; ++i) {
2275 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2276 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2280 map = isl_map_drop(map, type, first, n);
2287 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2288 enum isl_dim_type type, unsigned first, unsigned n)
2290 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2293 /* Project out n inputs starting at first using Fourier-Motzkin */
2294 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2295 unsigned first, unsigned n)
2297 return isl_map_remove_dims(map, isl_dim_in, first, n);
2300 static void dump_term(struct isl_basic_map *bmap,
2301 isl_int c, int pos, FILE *out)
2304 unsigned in = isl_basic_map_n_in(bmap);
2305 unsigned dim = in + isl_basic_map_n_out(bmap);
2306 unsigned nparam = isl_basic_map_n_param(bmap);
2308 isl_int_print(out, c, 0);
2310 if (!isl_int_is_one(c))
2311 isl_int_print(out, c, 0);
2312 if (pos < 1 + nparam) {
2313 name = isl_space_get_dim_name(bmap->dim,
2314 isl_dim_param, pos - 1);
2316 fprintf(out, "%s", name);
2318 fprintf(out, "p%d", pos - 1);
2319 } else if (pos < 1 + nparam + in)
2320 fprintf(out, "i%d", pos - 1 - nparam);
2321 else if (pos < 1 + nparam + dim)
2322 fprintf(out, "o%d", pos - 1 - nparam - in);
2324 fprintf(out, "e%d", pos - 1 - nparam - dim);
2328 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2329 int sign, FILE *out)
2333 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2337 for (i = 0, first = 1; i < len; ++i) {
2338 if (isl_int_sgn(c[i]) * sign <= 0)
2341 fprintf(out, " + ");
2343 isl_int_abs(v, c[i]);
2344 dump_term(bmap, v, i, out);
2351 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2352 const char *op, FILE *out, int indent)
2356 fprintf(out, "%*s", indent, "");
2358 dump_constraint_sign(bmap, c, 1, out);
2359 fprintf(out, " %s ", op);
2360 dump_constraint_sign(bmap, c, -1, out);
2364 for (i = bmap->n_div; i < bmap->extra; ++i) {
2365 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2367 fprintf(out, "%*s", indent, "");
2368 fprintf(out, "ERROR: unused div coefficient not zero\n");
2373 static void dump_constraints(struct isl_basic_map *bmap,
2374 isl_int **c, unsigned n,
2375 const char *op, FILE *out, int indent)
2379 for (i = 0; i < n; ++i)
2380 dump_constraint(bmap, c[i], op, out, indent);
2383 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2387 unsigned total = isl_basic_map_total_dim(bmap);
2389 for (j = 0; j < 1 + total; ++j) {
2390 if (isl_int_is_zero(exp[j]))
2392 if (!first && isl_int_is_pos(exp[j]))
2394 dump_term(bmap, exp[j], j, out);
2399 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2403 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2404 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2406 for (i = 0; i < bmap->n_div; ++i) {
2407 fprintf(out, "%*s", indent, "");
2408 fprintf(out, "e%d = [(", i);
2409 dump_affine(bmap, bmap->div[i]+1, out);
2411 isl_int_print(out, bmap->div[i][0], 0);
2412 fprintf(out, "]\n");
2416 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2417 FILE *out, int indent)
2420 fprintf(out, "null basic set\n");
2424 fprintf(out, "%*s", indent, "");
2425 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2426 bset->ref, bset->dim->nparam, bset->dim->n_out,
2427 bset->extra, bset->flags);
2428 dump((struct isl_basic_map *)bset, out, indent);
2431 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2432 FILE *out, int indent)
2435 fprintf(out, "null basic map\n");
2439 fprintf(out, "%*s", indent, "");
2440 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2441 "flags: %x, n_name: %d\n",
2443 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2444 bmap->extra, bmap->flags, bmap->dim->n_id);
2445 dump(bmap, out, indent);
2448 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2453 total = isl_basic_map_total_dim(bmap);
2454 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2455 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2456 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2457 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2461 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2464 struct isl_set *set;
2468 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2469 isl_assert(dim->ctx, n >= 0, goto error);
2470 set = isl_alloc(dim->ctx, struct isl_set,
2471 sizeof(struct isl_set) +
2472 (n - 1) * sizeof(struct isl_basic_set *));
2476 set->ctx = dim->ctx;
2477 isl_ctx_ref(set->ctx);
2485 isl_space_free(dim);
2489 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2490 unsigned nparam, unsigned dim, int n, unsigned flags)
2492 struct isl_set *set;
2495 dims = isl_space_alloc(ctx, nparam, 0, dim);
2499 set = isl_set_alloc_space(dims, n, flags);
2503 /* Make sure "map" has room for at least "n" more basic maps.
2505 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2508 struct isl_map *grown = NULL;
2512 isl_assert(map->ctx, n >= 0, goto error);
2513 if (map->n + n <= map->size)
2515 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2518 for (i = 0; i < map->n; ++i) {
2519 grown->p[i] = isl_basic_map_copy(map->p[i]);
2527 isl_map_free(grown);
2532 /* Make sure "set" has room for at least "n" more basic sets.
2534 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2536 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2539 struct isl_set *isl_set_dup(struct isl_set *set)
2542 struct isl_set *dup;
2547 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2550 for (i = 0; i < set->n; ++i)
2551 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2555 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2557 return isl_map_from_basic_map(bset);
2560 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2562 struct isl_map *map;
2567 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2568 return isl_map_add_basic_map(map, bmap);
2571 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2572 __isl_take isl_basic_set *bset)
2574 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2575 (struct isl_basic_map *)bset);
2578 void *isl_set_free(__isl_take isl_set *set)
2588 isl_ctx_deref(set->ctx);
2589 for (i = 0; i < set->n; ++i)
2590 isl_basic_set_free(set->p[i]);
2591 isl_space_free(set->dim);
2597 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2602 fprintf(out, "null set\n");
2606 fprintf(out, "%*s", indent, "");
2607 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2608 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2610 for (i = 0; i < set->n; ++i) {
2611 fprintf(out, "%*s", indent, "");
2612 fprintf(out, "basic set %d:\n", i);
2613 isl_basic_set_print_internal(set->p[i], out, indent+4);
2617 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2622 fprintf(out, "null map\n");
2626 fprintf(out, "%*s", indent, "");
2627 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2628 "flags: %x, n_name: %d\n",
2629 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2630 map->dim->n_out, map->flags, map->dim->n_id);
2631 for (i = 0; i < map->n; ++i) {
2632 fprintf(out, "%*s", indent, "");
2633 fprintf(out, "basic map %d:\n", i);
2634 isl_basic_map_print_internal(map->p[i], out, indent+4);
2638 struct isl_basic_map *isl_basic_map_intersect_domain(
2639 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2641 struct isl_basic_map *bmap_domain;
2646 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2647 bset->dim, isl_dim_param), goto error);
2649 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2650 isl_assert(bset->ctx,
2651 isl_basic_map_compatible_domain(bmap, bset), goto error);
2653 bmap = isl_basic_map_cow(bmap);
2656 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2657 bset->n_div, bset->n_eq, bset->n_ineq);
2658 bmap_domain = isl_basic_map_from_domain(bset);
2659 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2661 bmap = isl_basic_map_simplify(bmap);
2662 return isl_basic_map_finalize(bmap);
2664 isl_basic_map_free(bmap);
2665 isl_basic_set_free(bset);
2669 struct isl_basic_map *isl_basic_map_intersect_range(
2670 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2672 struct isl_basic_map *bmap_range;
2677 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2678 bset->dim, isl_dim_param), goto error);
2680 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2681 isl_assert(bset->ctx,
2682 isl_basic_map_compatible_range(bmap, bset), goto error);
2684 if (isl_basic_set_is_universe(bset)) {
2685 isl_basic_set_free(bset);
2689 bmap = isl_basic_map_cow(bmap);
2692 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2693 bset->n_div, bset->n_eq, bset->n_ineq);
2694 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2695 bmap = add_constraints(bmap, bmap_range, 0, 0);
2697 bmap = isl_basic_map_simplify(bmap);
2698 return isl_basic_map_finalize(bmap);
2700 isl_basic_map_free(bmap);
2701 isl_basic_set_free(bset);
2705 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2714 total = 1 + isl_basic_map_total_dim(bmap);
2715 if (total != vec->size)
2720 for (i = 0; i < bmap->n_eq; ++i) {
2721 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2722 if (!isl_int_is_zero(s)) {
2728 for (i = 0; i < bmap->n_ineq; ++i) {
2729 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2730 if (isl_int_is_neg(s)) {
2741 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2743 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2746 struct isl_basic_map *isl_basic_map_intersect(
2747 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2749 struct isl_vec *sample = NULL;
2751 if (!bmap1 || !bmap2)
2754 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2755 bmap2->dim, isl_dim_param), goto error);
2756 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2757 isl_space_dim(bmap1->dim, isl_dim_param) &&
2758 isl_space_dim(bmap2->dim, isl_dim_all) !=
2759 isl_space_dim(bmap2->dim, isl_dim_param))
2760 return isl_basic_map_intersect(bmap2, bmap1);
2762 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2763 isl_space_dim(bmap2->dim, isl_dim_param))
2764 isl_assert(bmap1->ctx,
2765 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2767 if (bmap1->sample &&
2768 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2769 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2770 sample = isl_vec_copy(bmap1->sample);
2771 else if (bmap2->sample &&
2772 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2773 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2774 sample = isl_vec_copy(bmap2->sample);
2776 bmap1 = isl_basic_map_cow(bmap1);
2779 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2780 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2781 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2784 isl_vec_free(sample);
2786 isl_vec_free(bmap1->sample);
2787 bmap1->sample = sample;
2790 bmap1 = isl_basic_map_simplify(bmap1);
2791 return isl_basic_map_finalize(bmap1);
2794 isl_vec_free(sample);
2795 isl_basic_map_free(bmap1);
2796 isl_basic_map_free(bmap2);
2800 struct isl_basic_set *isl_basic_set_intersect(
2801 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2803 return (struct isl_basic_set *)
2804 isl_basic_map_intersect(
2805 (struct isl_basic_map *)bset1,
2806 (struct isl_basic_map *)bset2);
2809 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2810 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2812 return isl_basic_set_intersect(bset1, bset2);
2815 /* Special case of isl_map_intersect, where both map1 and map2
2816 * are convex, without any divs and such that either map1 or map2
2817 * contains a single constraint. This constraint is then simply
2818 * added to the other map.
2820 static __isl_give isl_map *map_intersect_add_constraint(
2821 __isl_take isl_map *map1, __isl_take isl_map *map2)
2823 isl_assert(map1->ctx, map1->n == 1, goto error);
2824 isl_assert(map2->ctx, map1->n == 1, goto error);
2825 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2826 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2828 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2829 return isl_map_intersect(map2, map1);
2831 isl_assert(map2->ctx,
2832 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2834 map1 = isl_map_cow(map1);
2837 if (isl_map_plain_is_empty(map1)) {
2841 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2842 if (map2->p[0]->n_eq == 1)
2843 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2845 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2846 map2->p[0]->ineq[0]);
2848 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2849 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2853 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2854 isl_basic_map_free(map1->p[0]);
2867 /* map2 may be either a parameter domain or a map living in the same
2870 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2871 __isl_take isl_map *map2)
2880 if ((isl_map_plain_is_empty(map1) ||
2881 isl_map_plain_is_universe(map2)) &&
2882 isl_space_is_equal(map1->dim, map2->dim)) {
2886 if ((isl_map_plain_is_empty(map2) ||
2887 isl_map_plain_is_universe(map1)) &&
2888 isl_space_is_equal(map1->dim, map2->dim)) {
2893 if (map1->n == 1 && map2->n == 1 &&
2894 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2895 isl_space_is_equal(map1->dim, map2->dim) &&
2896 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2897 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2898 return map_intersect_add_constraint(map1, map2);
2900 if (isl_space_dim(map2->dim, isl_dim_all) !=
2901 isl_space_dim(map2->dim, isl_dim_param))
2902 isl_assert(map1->ctx,
2903 isl_space_is_equal(map1->dim, map2->dim), goto error);
2905 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2906 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2907 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2909 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2910 map1->n * map2->n, flags);
2913 for (i = 0; i < map1->n; ++i)
2914 for (j = 0; j < map2->n; ++j) {
2915 struct isl_basic_map *part;
2916 part = isl_basic_map_intersect(
2917 isl_basic_map_copy(map1->p[i]),
2918 isl_basic_map_copy(map2->p[j]));
2919 if (isl_basic_map_is_empty(part) < 0)
2920 part = isl_basic_map_free(part);
2921 result = isl_map_add_basic_map(result, part);
2934 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2935 __isl_take isl_map *map2)
2939 if (!isl_space_is_equal(map1->dim, map2->dim))
2940 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2941 "spaces don't match", goto error);
2942 return map_intersect_internal(map1, map2);
2949 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2950 __isl_take isl_map *map2)
2952 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2955 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2957 return (struct isl_set *)
2958 isl_map_intersect((struct isl_map *)set1,
2959 (struct isl_map *)set2);
2962 /* map_intersect_internal accepts intersections
2963 * with parameter domains, so we can just call that function.
2965 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2966 __isl_take isl_set *params)
2968 return map_intersect_internal(map, params);
2971 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2972 __isl_take isl_map *map2)
2974 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2977 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2978 __isl_take isl_set *params)
2980 return isl_map_intersect_params(set, params);
2983 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2986 struct isl_basic_set *bset;
2991 bmap = isl_basic_map_cow(bmap);
2994 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2995 in = isl_basic_map_n_in(bmap);
2996 bset = isl_basic_set_from_basic_map(bmap);
2997 bset = isl_basic_set_swap_vars(bset, in);
2998 return isl_basic_map_from_basic_set(bset, dim);
3001 static __isl_give isl_basic_map *basic_map_space_reset(
3002 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3008 if (!isl_space_is_named_or_nested(bmap->dim, type))
3011 space = isl_basic_map_get_space(bmap);
3012 space = isl_space_reset(space, type);
3013 bmap = isl_basic_map_reset_space(bmap, space);
3017 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3018 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3019 unsigned pos, unsigned n)
3022 struct isl_basic_map *res;
3023 struct isl_dim_map *dim_map;
3024 unsigned total, off;
3025 enum isl_dim_type t;
3028 return basic_map_space_reset(bmap, type);
3033 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3035 total = isl_basic_map_total_dim(bmap) + n;
3036 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3038 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3040 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3042 unsigned size = isl_basic_map_dim(bmap, t);
3043 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3045 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3046 pos, size - pos, off + pos + n);
3048 off += isl_space_dim(res_dim, t);
3050 isl_dim_map_div(dim_map, bmap, off);
3052 res = isl_basic_map_alloc_space(res_dim,
3053 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3054 if (isl_basic_map_is_rational(bmap))
3055 res = isl_basic_map_set_rational(res);
3056 if (isl_basic_map_plain_is_empty(bmap)) {
3057 isl_basic_map_free(bmap);
3059 return isl_basic_map_set_to_empty(res);
3061 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3062 return isl_basic_map_finalize(res);
3065 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3066 __isl_take isl_basic_set *bset,
3067 enum isl_dim_type type, unsigned pos, unsigned n)
3069 return isl_basic_map_insert_dims(bset, type, pos, n);
3072 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3073 enum isl_dim_type type, unsigned n)
3077 return isl_basic_map_insert_dims(bmap, type,
3078 isl_basic_map_dim(bmap, type), n);
3081 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3082 enum isl_dim_type type, unsigned n)
3086 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3087 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3089 isl_basic_set_free(bset);
3093 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3094 enum isl_dim_type type)
3098 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3101 space = isl_map_get_space(map);
3102 space = isl_space_reset(space, type);
3103 map = isl_map_reset_space(map, space);
3107 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3108 enum isl_dim_type type, unsigned pos, unsigned n)
3113 return map_space_reset(map, type);
3115 map = isl_map_cow(map);
3119 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3123 for (i = 0; i < map->n; ++i) {
3124 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3135 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3136 enum isl_dim_type type, unsigned pos, unsigned n)
3138 return isl_map_insert_dims(set, type, pos, n);
3141 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3142 enum isl_dim_type type, unsigned n)
3146 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3149 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3150 enum isl_dim_type type, unsigned n)
3154 isl_assert(set->ctx, type != isl_dim_in, goto error);
3155 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3161 __isl_give isl_basic_map *isl_basic_map_move_dims(
3162 __isl_take isl_basic_map *bmap,
3163 enum isl_dim_type dst_type, unsigned dst_pos,
3164 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3166 struct isl_dim_map *dim_map;
3167 struct isl_basic_map *res;
3168 enum isl_dim_type t;
3169 unsigned total, off;
3176 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3179 if (dst_type == src_type && dst_pos == src_pos)
3182 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3184 if (pos(bmap->dim, dst_type) + dst_pos ==
3185 pos(bmap->dim, src_type) + src_pos +
3186 ((src_type < dst_type) ? n : 0)) {
3187 bmap = isl_basic_map_cow(bmap);
3191 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3192 src_type, src_pos, n);
3196 bmap = isl_basic_map_finalize(bmap);
3201 total = isl_basic_map_total_dim(bmap);
3202 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3205 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3206 unsigned size = isl_space_dim(bmap->dim, t);
3207 if (t == dst_type) {
3208 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3211 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3214 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3215 dst_pos, size - dst_pos, off);
3216 off += size - dst_pos;
3217 } else if (t == src_type) {
3218 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3221 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3222 src_pos + n, size - src_pos - n, off);
3223 off += size - src_pos - n;
3225 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3229 isl_dim_map_div(dim_map, bmap, off);
3231 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3232 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3233 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3237 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3238 src_type, src_pos, n);
3242 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3243 bmap = isl_basic_map_gauss(bmap, NULL);
3244 bmap = isl_basic_map_finalize(bmap);
3248 isl_basic_map_free(bmap);
3252 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3253 enum isl_dim_type dst_type, unsigned dst_pos,
3254 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3256 return (isl_basic_set *)isl_basic_map_move_dims(
3257 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3260 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3261 enum isl_dim_type dst_type, unsigned dst_pos,
3262 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3266 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3267 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3268 src_type, src_pos, n);
3274 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3275 enum isl_dim_type dst_type, unsigned dst_pos,
3276 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3285 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3288 if (dst_type == src_type && dst_pos == src_pos)
3291 isl_assert(map->ctx, dst_type != src_type, goto error);
3293 map = isl_map_cow(map);
3297 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3301 for (i = 0; i < map->n; ++i) {
3302 map->p[i] = isl_basic_map_move_dims(map->p[i],
3304 src_type, src_pos, n);
3315 /* Move the specified dimensions to the last columns right before
3316 * the divs. Don't change the dimension specification of bmap.
3317 * That's the responsibility of the caller.
3319 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3320 enum isl_dim_type type, unsigned first, unsigned n)
3322 struct isl_dim_map *dim_map;
3323 struct isl_basic_map *res;
3324 enum isl_dim_type t;
3325 unsigned total, off;
3329 if (pos(bmap->dim, type) + first + n ==
3330 1 + isl_space_dim(bmap->dim, isl_dim_all))
3333 total = isl_basic_map_total_dim(bmap);
3334 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3337 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3338 unsigned size = isl_space_dim(bmap->dim, t);
3340 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3343 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3344 first, n, total - bmap->n_div - n);
3345 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3346 first + n, size - (first + n), off);
3347 off += size - (first + n);
3349 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3353 isl_dim_map_div(dim_map, bmap, off + n);
3355 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3356 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3357 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3361 /* Turn the n dimensions of type type, starting at first
3362 * into existentially quantified variables.
3364 __isl_give isl_basic_map *isl_basic_map_project_out(
3365 __isl_take isl_basic_map *bmap,
3366 enum isl_dim_type type, unsigned first, unsigned n)
3374 return basic_map_space_reset(bmap, type);
3379 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3380 return isl_basic_map_remove_dims(bmap, type, first, n);
3382 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3385 bmap = move_last(bmap, type, first, n);
3386 bmap = isl_basic_map_cow(bmap);
3390 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3391 old = bmap->block2.data;
3392 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3393 (bmap->extra + n) * (1 + row_size));
3394 if (!bmap->block2.data)
3396 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3399 for (i = 0; i < n; ++i) {
3400 new_div[i] = bmap->block2.data +
3401 (bmap->extra + i) * (1 + row_size);
3402 isl_seq_clr(new_div[i], 1 + row_size);
3404 for (i = 0; i < bmap->extra; ++i)
3405 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3407 bmap->div = new_div;
3411 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3414 bmap = isl_basic_map_simplify(bmap);
3415 bmap = isl_basic_map_drop_redundant_divs(bmap);
3416 return isl_basic_map_finalize(bmap);
3418 isl_basic_map_free(bmap);
3422 /* Turn the n dimensions of type type, starting at first
3423 * into existentially quantified variables.
3425 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3426 enum isl_dim_type type, unsigned first, unsigned n)
3428 return (isl_basic_set *)isl_basic_map_project_out(
3429 (isl_basic_map *)bset, type, first, n);
3432 /* Turn the n dimensions of type type, starting at first
3433 * into existentially quantified variables.
3435 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3436 enum isl_dim_type type, unsigned first, unsigned n)
3444 return map_space_reset(map, type);
3446 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3448 map = isl_map_cow(map);
3452 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3456 for (i = 0; i < map->n; ++i) {
3457 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3468 /* Turn the n dimensions of type type, starting at first
3469 * into existentially quantified variables.
3471 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3472 enum isl_dim_type type, unsigned first, unsigned n)
3474 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3477 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3481 for (i = 0; i < n; ++i) {
3482 j = isl_basic_map_alloc_div(bmap);
3485 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3489 isl_basic_map_free(bmap);
3493 struct isl_basic_map *isl_basic_map_apply_range(
3494 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3496 isl_space *dim_result = NULL;
3497 struct isl_basic_map *bmap;
3498 unsigned n_in, n_out, n, nparam, total, pos;
3499 struct isl_dim_map *dim_map1, *dim_map2;
3501 if (!bmap1 || !bmap2)
3504 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3505 isl_space_copy(bmap2->dim));
3507 n_in = isl_basic_map_n_in(bmap1);
3508 n_out = isl_basic_map_n_out(bmap2);
3509 n = isl_basic_map_n_out(bmap1);
3510 nparam = isl_basic_map_n_param(bmap1);
3512 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3513 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3514 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3515 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3516 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3517 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3518 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3519 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3520 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3521 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3522 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3524 bmap = isl_basic_map_alloc_space(dim_result,
3525 bmap1->n_div + bmap2->n_div + n,
3526 bmap1->n_eq + bmap2->n_eq,
3527 bmap1->n_ineq + bmap2->n_ineq);
3528 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3529 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3530 bmap = add_divs(bmap, n);
3531 bmap = isl_basic_map_simplify(bmap);
3532 bmap = isl_basic_map_drop_redundant_divs(bmap);
3533 return isl_basic_map_finalize(bmap);
3535 isl_basic_map_free(bmap1);
3536 isl_basic_map_free(bmap2);
3540 struct isl_basic_set *isl_basic_set_apply(
3541 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3546 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3549 return (struct isl_basic_set *)
3550 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3552 isl_basic_set_free(bset);
3553 isl_basic_map_free(bmap);
3557 struct isl_basic_map *isl_basic_map_apply_domain(
3558 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3560 if (!bmap1 || !bmap2)
3563 isl_assert(bmap1->ctx,
3564 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3565 isl_assert(bmap1->ctx,
3566 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3569 bmap1 = isl_basic_map_reverse(bmap1);
3570 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3571 return isl_basic_map_reverse(bmap1);
3573 isl_basic_map_free(bmap1);
3574 isl_basic_map_free(bmap2);
3578 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3579 * A \cap B -> f(A) + f(B)
3581 struct isl_basic_map *isl_basic_map_sum(
3582 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3584 unsigned n_in, n_out, nparam, total, pos;
3585 struct isl_basic_map *bmap = NULL;
3586 struct isl_dim_map *dim_map1, *dim_map2;
3589 if (!bmap1 || !bmap2)
3592 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3595 nparam = isl_basic_map_n_param(bmap1);
3596 n_in = isl_basic_map_n_in(bmap1);
3597 n_out = isl_basic_map_n_out(bmap1);
3599 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3600 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3601 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3602 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3603 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3604 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3605 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3606 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3607 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3608 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3609 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3611 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3612 bmap1->n_div + bmap2->n_div + 2 * n_out,
3613 bmap1->n_eq + bmap2->n_eq + n_out,
3614 bmap1->n_ineq + bmap2->n_ineq);
3615 for (i = 0; i < n_out; ++i) {
3616 int j = isl_basic_map_alloc_equality(bmap);
3619 isl_seq_clr(bmap->eq[j], 1+total);
3620 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3621 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3622 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3624 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3625 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3626 bmap = add_divs(bmap, 2 * n_out);
3628 bmap = isl_basic_map_simplify(bmap);
3629 return isl_basic_map_finalize(bmap);
3631 isl_basic_map_free(bmap);
3632 isl_basic_map_free(bmap1);
3633 isl_basic_map_free(bmap2);
3637 /* Given two maps A -> f(A) and B -> g(B), construct a map
3638 * A \cap B -> f(A) + f(B)
3640 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3642 struct isl_map *result;
3648 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3650 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3651 map1->n * map2->n, 0);
3654 for (i = 0; i < map1->n; ++i)
3655 for (j = 0; j < map2->n; ++j) {
3656 struct isl_basic_map *part;
3657 part = isl_basic_map_sum(
3658 isl_basic_map_copy(map1->p[i]),
3659 isl_basic_map_copy(map2->p[j]));
3660 if (isl_basic_map_is_empty(part))
3661 isl_basic_map_free(part);
3663 result = isl_map_add_basic_map(result, part);
3676 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3677 __isl_take isl_set *set2)
3679 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3682 /* Given a basic map A -> f(A), construct A -> -f(A).
3684 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3689 bmap = isl_basic_map_cow(bmap);
3693 n = isl_basic_map_dim(bmap, isl_dim_out);
3694 off = isl_basic_map_offset(bmap, isl_dim_out);
3695 for (i = 0; i < bmap->n_eq; ++i)
3696 for (j = 0; j < n; ++j)
3697 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3698 for (i = 0; i < bmap->n_ineq; ++i)
3699 for (j = 0; j < n; ++j)
3700 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3701 for (i = 0; i < bmap->n_div; ++i)
3702 for (j = 0; j < n; ++j)
3703 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3704 bmap = isl_basic_map_gauss(bmap, NULL);
3705 return isl_basic_map_finalize(bmap);
3708 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3710 return isl_basic_map_neg(bset);
3713 /* Given a map A -> f(A), construct A -> -f(A).
3715 struct isl_map *isl_map_neg(struct isl_map *map)
3719 map = isl_map_cow(map);
3723 for (i = 0; i < map->n; ++i) {
3724 map->p[i] = isl_basic_map_neg(map->p[i]);
3735 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3737 return (isl_set *)isl_map_neg((isl_map *)set);
3740 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3741 * A -> floor(f(A)/d).
3743 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3746 unsigned n_in, n_out, nparam, total, pos;
3747 struct isl_basic_map *result = NULL;
3748 struct isl_dim_map *dim_map;
3754 nparam = isl_basic_map_n_param(bmap);
3755 n_in = isl_basic_map_n_in(bmap);
3756 n_out = isl_basic_map_n_out(bmap);
3758 total = nparam + n_in + n_out + bmap->n_div + n_out;
3759 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3760 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3761 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3762 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3763 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3765 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3766 bmap->n_div + n_out,
3767 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3768 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3769 result = add_divs(result, n_out);
3770 for (i = 0; i < n_out; ++i) {
3772 j = isl_basic_map_alloc_inequality(result);
3775 isl_seq_clr(result->ineq[j], 1+total);
3776 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3777 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3778 j = isl_basic_map_alloc_inequality(result);
3781 isl_seq_clr(result->ineq[j], 1+total);
3782 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3783 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3784 isl_int_sub_ui(result->ineq[j][0], d, 1);
3787 result = isl_basic_map_simplify(result);
3788 return isl_basic_map_finalize(result);
3790 isl_basic_map_free(result);
3794 /* Given a map A -> f(A) and an integer d, construct a map
3795 * A -> floor(f(A)/d).
3797 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3801 map = isl_map_cow(map);
3805 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3806 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3807 for (i = 0; i < map->n; ++i) {
3808 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3819 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3825 i = isl_basic_map_alloc_equality(bmap);
3828 nparam = isl_basic_map_n_param(bmap);
3829 n_in = isl_basic_map_n_in(bmap);
3830 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3831 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3832 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3833 return isl_basic_map_finalize(bmap);
3835 isl_basic_map_free(bmap);
3839 /* Add a constraints to "bmap" expressing i_pos < o_pos
3841 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3847 i = isl_basic_map_alloc_inequality(bmap);
3850 nparam = isl_basic_map_n_param(bmap);
3851 n_in = isl_basic_map_n_in(bmap);
3852 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3853 isl_int_set_si(bmap->ineq[i][0], -1);
3854 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3855 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3856 return isl_basic_map_finalize(bmap);
3858 isl_basic_map_free(bmap);
3862 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3864 static __isl_give isl_basic_map *var_less_or_equal(
3865 __isl_take isl_basic_map *bmap, unsigned pos)
3871 i = isl_basic_map_alloc_inequality(bmap);
3874 nparam = isl_basic_map_n_param(bmap);
3875 n_in = isl_basic_map_n_in(bmap);
3876 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3877 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3878 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3879 return isl_basic_map_finalize(bmap);
3881 isl_basic_map_free(bmap);
3885 /* Add a constraints to "bmap" expressing i_pos > o_pos
3887 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3893 i = isl_basic_map_alloc_inequality(bmap);
3896 nparam = isl_basic_map_n_param(bmap);
3897 n_in = isl_basic_map_n_in(bmap);
3898 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3899 isl_int_set_si(bmap->ineq[i][0], -1);
3900 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3901 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3902 return isl_basic_map_finalize(bmap);
3904 isl_basic_map_free(bmap);
3908 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3910 static __isl_give isl_basic_map *var_more_or_equal(
3911 __isl_take isl_basic_map *bmap, unsigned pos)
3917 i = isl_basic_map_alloc_inequality(bmap);
3920 nparam = isl_basic_map_n_param(bmap);
3921 n_in = isl_basic_map_n_in(bmap);
3922 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3923 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3924 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3925 return isl_basic_map_finalize(bmap);
3927 isl_basic_map_free(bmap);
3931 __isl_give isl_basic_map *isl_basic_map_equal(
3932 __isl_take isl_space *dim, unsigned n_equal)
3935 struct isl_basic_map *bmap;
3936 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3939 for (i = 0; i < n_equal && bmap; ++i)
3940 bmap = var_equal(bmap, i);
3941 return isl_basic_map_finalize(bmap);
3944 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3946 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3950 struct isl_basic_map *bmap;
3951 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3954 for (i = 0; i < pos && bmap; ++i)
3955 bmap = var_equal(bmap, i);
3957 bmap = var_less(bmap, pos);
3958 return isl_basic_map_finalize(bmap);
3961 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3963 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3964 __isl_take isl_space *dim, unsigned pos)
3967 isl_basic_map *bmap;
3969 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3970 for (i = 0; i < pos; ++i)
3971 bmap = var_equal(bmap, i);
3972 bmap = var_less_or_equal(bmap, pos);
3973 return isl_basic_map_finalize(bmap);
3976 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3978 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3982 struct isl_basic_map *bmap;
3983 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3986 for (i = 0; i < pos && bmap; ++i)
3987 bmap = var_equal(bmap, i);
3989 bmap = var_more(bmap, pos);
3990 return isl_basic_map_finalize(bmap);
3993 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3995 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3996 __isl_take isl_space *dim, unsigned pos)
3999 isl_basic_map *bmap;
4001 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4002 for (i = 0; i < pos; ++i)
4003 bmap = var_equal(bmap, i);
4004 bmap = var_more_or_equal(bmap, pos);
4005 return isl_basic_map_finalize(bmap);
4008 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4009 unsigned n, int equal)
4011 struct isl_map *map;
4014 if (n == 0 && equal)
4015 return isl_map_universe(dims);
4017 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4019 for (i = 0; i + 1 < n; ++i)
4020 map = isl_map_add_basic_map(map,
4021 isl_basic_map_less_at(isl_space_copy(dims), i));
4024 map = isl_map_add_basic_map(map,
4025 isl_basic_map_less_or_equal_at(dims, n - 1));
4027 map = isl_map_add_basic_map(map,
4028 isl_basic_map_less_at(dims, n - 1));
4030 isl_space_free(dims);
4035 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4039 return map_lex_lte_first(dims, dims->n_out, equal);
4042 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4044 return map_lex_lte_first(dim, n, 0);
4047 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4049 return map_lex_lte_first(dim, n, 1);
4052 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4054 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4057 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4059 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4062 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4063 unsigned n, int equal)
4065 struct isl_map *map;
4068 if (n == 0 && equal)
4069 return isl_map_universe(dims);
4071 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4073 for (i = 0; i + 1 < n; ++i)
4074 map = isl_map_add_basic_map(map,
4075 isl_basic_map_more_at(isl_space_copy(dims), i));
4078 map = isl_map_add_basic_map(map,
4079 isl_basic_map_more_or_equal_at(dims, n - 1));
4081 map = isl_map_add_basic_map(map,
4082 isl_basic_map_more_at(dims, n - 1));
4084 isl_space_free(dims);
4089 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4093 return map_lex_gte_first(dims, dims->n_out, equal);
4096 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4098 return map_lex_gte_first(dim, n, 0);
4101 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4103 return map_lex_gte_first(dim, n, 1);
4106 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4108 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4111 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4113 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4116 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4117 __isl_take isl_set *set2)
4120 map = isl_map_lex_le(isl_set_get_space(set1));
4121 map = isl_map_intersect_domain(map, set1);
4122 map = isl_map_intersect_range(map, set2);
4126 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4127 __isl_take isl_set *set2)
4130 map = isl_map_lex_lt(isl_set_get_space(set1));
4131 map = isl_map_intersect_domain(map, set1);
4132 map = isl_map_intersect_range(map, set2);
4136 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4137 __isl_take isl_set *set2)
4140 map = isl_map_lex_ge(isl_set_get_space(set1));
4141 map = isl_map_intersect_domain(map, set1);
4142 map = isl_map_intersect_range(map, set2);
4146 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4147 __isl_take isl_set *set2)
4150 map = isl_map_lex_gt(isl_set_get_space(set1));
4151 map = isl_map_intersect_domain(map, set1);
4152 map = isl_map_intersect_range(map, set2);
4156 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4157 __isl_take isl_map *map2)
4160 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4161 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4162 map = isl_map_apply_range(map, isl_map_reverse(map2));
4166 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4167 __isl_take isl_map *map2)
4170 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4171 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4172 map = isl_map_apply_range(map, isl_map_reverse(map2));
4176 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4177 __isl_take isl_map *map2)
4180 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4181 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4182 map = isl_map_apply_range(map, isl_map_reverse(map2));
4186 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4187 __isl_take isl_map *map2)
4190 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4191 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4192 map = isl_map_apply_range(map, isl_map_reverse(map2));
4196 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4197 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4199 struct isl_basic_map *bmap;
4201 bset = isl_basic_set_cow(bset);
4205 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4206 isl_space_free(bset->dim);
4207 bmap = (struct isl_basic_map *) bset;
4209 return isl_basic_map_finalize(bmap);
4211 isl_basic_set_free(bset);
4212 isl_space_free(dim);
4216 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4220 if (bmap->dim->n_in == 0)
4221 return (struct isl_basic_set *)bmap;
4222 bmap = isl_basic_map_cow(bmap);
4225 bmap->dim = isl_space_as_set_space(bmap->dim);
4228 bmap = isl_basic_map_finalize(bmap);
4229 return (struct isl_basic_set *)bmap;
4231 isl_basic_map_free(bmap);
4235 /* For a div d = floor(f/m), add the constraints
4238 * -(f-(n-1)) + m d >= 0
4240 * Note that the second constraint is the negation of
4244 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4245 unsigned pos, isl_int *div)
4248 unsigned total = isl_basic_map_total_dim(bmap);
4250 i = isl_basic_map_alloc_inequality(bmap);
4253 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4254 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4256 j = isl_basic_map_alloc_inequality(bmap);
4259 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4260 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4261 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4265 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4266 unsigned pos, isl_int *div)
4268 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4272 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4274 unsigned total = isl_basic_map_total_dim(bmap);
4275 unsigned div_pos = total - bmap->n_div + div;
4277 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4281 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4283 return isl_basic_map_add_div_constraints(bset, div);
4286 struct isl_basic_set *isl_basic_map_underlying_set(
4287 struct isl_basic_map *bmap)
4291 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4293 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4294 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4295 return (struct isl_basic_set *)bmap;
4296 bmap = isl_basic_map_cow(bmap);
4299 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4302 bmap->extra -= bmap->n_div;
4304 bmap = isl_basic_map_finalize(bmap);
4305 return (struct isl_basic_set *)bmap;
4307 isl_basic_map_free(bmap);
4311 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4312 __isl_take isl_basic_set *bset)
4314 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4317 struct isl_basic_map *isl_basic_map_overlying_set(
4318 struct isl_basic_set *bset, struct isl_basic_map *like)
4320 struct isl_basic_map *bmap;
4321 struct isl_ctx *ctx;
4328 isl_assert(ctx, bset->n_div == 0, goto error);
4329 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4330 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4332 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4333 isl_basic_map_free(like);
4334 return (struct isl_basic_map *)bset;
4336 bset = isl_basic_set_cow(bset);
4339 total = bset->dim->n_out + bset->extra;
4340 bmap = (struct isl_basic_map *)bset;
4341 isl_space_free(bmap->dim);
4342 bmap->dim = isl_space_copy(like->dim);
4345 bmap->n_div = like->n_div;
4346 bmap->extra += like->n_div;
4350 ltotal = total - bmap->extra + like->extra;
4353 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4354 bmap->extra * (1 + 1 + total));
4355 if (isl_blk_is_error(bmap->block2))
4357 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4361 for (i = 0; i < bmap->extra; ++i)
4362 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4363 for (i = 0; i < like->n_div; ++i) {
4364 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4365 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4367 bmap = isl_basic_map_extend_constraints(bmap,
4368 0, 2 * like->n_div);
4369 for (i = 0; i < like->n_div; ++i) {
4372 if (isl_int_is_zero(bmap->div[i][0]))
4374 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4375 bmap = isl_basic_map_free(bmap);
4378 isl_basic_map_free(like);
4379 bmap = isl_basic_map_simplify(bmap);
4380 bmap = isl_basic_map_finalize(bmap);
4383 isl_basic_map_free(like);
4384 isl_basic_set_free(bset);
4388 struct isl_basic_set *isl_basic_set_from_underlying_set(
4389 struct isl_basic_set *bset, struct isl_basic_set *like)
4391 return (struct isl_basic_set *)
4392 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4395 struct isl_set *isl_set_from_underlying_set(
4396 struct isl_set *set, struct isl_basic_set *like)
4402 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4404 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4405 isl_basic_set_free(like);
4408 set = isl_set_cow(set);
4411 for (i = 0; i < set->n; ++i) {
4412 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4413 isl_basic_set_copy(like));
4417 isl_space_free(set->dim);
4418 set->dim = isl_space_copy(like->dim);
4421 isl_basic_set_free(like);
4424 isl_basic_set_free(like);
4429 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4433 map = isl_map_cow(map);
4436 map->dim = isl_space_cow(map->dim);
4440 for (i = 1; i < map->n; ++i)
4441 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4443 for (i = 0; i < map->n; ++i) {
4444 map->p[i] = (struct isl_basic_map *)
4445 isl_basic_map_underlying_set(map->p[i]);
4450 map->dim = isl_space_underlying(map->dim, 0);
4452 isl_space_free(map->dim);
4453 map->dim = isl_space_copy(map->p[0]->dim);
4457 return (struct isl_set *)map;
4463 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4465 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4468 __isl_give isl_basic_map *isl_basic_map_reset_space(
4469 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4471 bmap = isl_basic_map_cow(bmap);
4475 isl_space_free(bmap->dim);
4478 bmap = isl_basic_map_finalize(bmap);
4482 isl_basic_map_free(bmap);
4483 isl_space_free(dim);
4487 __isl_give isl_basic_set *isl_basic_set_reset_space(
4488 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4490 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4494 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4495 __isl_take isl_space *dim)
4499 map = isl_map_cow(map);
4503 for (i = 0; i < map->n; ++i) {
4504 map->p[i] = isl_basic_map_reset_space(map->p[i],
4505 isl_space_copy(dim));
4509 isl_space_free(map->dim);
4515 isl_space_free(dim);
4519 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4520 __isl_take isl_space *dim)
4522 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4525 /* Compute the parameter domain of the given basic set.
4527 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4532 if (isl_basic_set_is_params(bset))
4535 n = isl_basic_set_dim(bset, isl_dim_set);
4536 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4537 space = isl_basic_set_get_space(bset);
4538 space = isl_space_params(space);
4539 bset = isl_basic_set_reset_space(bset, space);
4543 /* Construct a zero-dimensional basic set with the given parameter domain.
4545 __isl_give isl_basic_set *isl_basic_set_from_params(
4546 __isl_take isl_basic_set *bset)
4549 space = isl_basic_set_get_space(bset);
4550 space = isl_space_set_from_params(space);
4551 bset = isl_basic_set_reset_space(bset, space);
4555 /* Compute the parameter domain of the given set.
4557 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4562 if (isl_set_is_params(set))
4565 n = isl_set_dim(set, isl_dim_set);
4566 set = isl_set_project_out(set, isl_dim_set, 0, n);
4567 space = isl_set_get_space(set);
4568 space = isl_space_params(space);
4569 set = isl_set_reset_space(set, space);
4573 /* Construct a zero-dimensional set with the given parameter domain.
4575 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4578 space = isl_set_get_space(set);
4579 space = isl_space_set_from_params(space);
4580 set = isl_set_reset_space(set, space);
4584 /* Compute the parameter domain of the given map.
4586 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4591 n = isl_map_dim(map, isl_dim_in);
4592 map = isl_map_project_out(map, isl_dim_in, 0, n);
4593 n = isl_map_dim(map, isl_dim_out);
4594 map = isl_map_project_out(map, isl_dim_out, 0, n);
4595 space = isl_map_get_space(map);
4596 space = isl_space_params(space);
4597 map = isl_map_reset_space(map, space);
4601 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4604 struct isl_basic_set *domain;
4610 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4612 n_in = isl_basic_map_n_in(bmap);
4613 n_out = isl_basic_map_n_out(bmap);
4614 domain = isl_basic_set_from_basic_map(bmap);
4615 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4617 domain = isl_basic_set_reset_space(domain, dim);
4622 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4626 return isl_space_may_be_set(bmap->dim);
4629 /* Is this basic map actually a set?
4630 * Users should never call this function. Outside of isl,
4631 * the type should indicate whether something is a set or a map.
4633 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4637 return isl_space_is_set(bmap->dim);
4640 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4644 if (isl_basic_map_is_set(bmap))
4646 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4649 __isl_give isl_basic_map *isl_basic_map_domain_map(
4650 __isl_take isl_basic_map *bmap)
4654 isl_basic_map *domain;
4655 int nparam, n_in, n_out;
4658 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4659 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4660 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4662 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4663 domain = isl_basic_map_universe(dim);
4665 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4666 bmap = isl_basic_map_apply_range(bmap, domain);
4667 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4669 total = isl_basic_map_total_dim(bmap);
4671 for (i = 0; i < n_in; ++i) {
4672 k = isl_basic_map_alloc_equality(bmap);
4675 isl_seq_clr(bmap->eq[k], 1 + total);
4676 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4677 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4680 bmap = isl_basic_map_gauss(bmap, NULL);
4681 return isl_basic_map_finalize(bmap);
4683 isl_basic_map_free(bmap);
4687 __isl_give isl_basic_map *isl_basic_map_range_map(
4688 __isl_take isl_basic_map *bmap)
4692 isl_basic_map *range;
4693 int nparam, n_in, n_out;
4696 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4697 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4698 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4700 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4701 range = isl_basic_map_universe(dim);
4703 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4704 bmap = isl_basic_map_apply_range(bmap, range);
4705 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4707 total = isl_basic_map_total_dim(bmap);
4709 for (i = 0; i < n_out; ++i) {
4710 k = isl_basic_map_alloc_equality(bmap);
4713 isl_seq_clr(bmap->eq[k], 1 + total);
4714 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4715 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4718 bmap = isl_basic_map_gauss(bmap, NULL);
4719 return isl_basic_map_finalize(bmap);
4721 isl_basic_map_free(bmap);
4725 int isl_map_may_be_set(__isl_keep isl_map *map)
4729 return isl_space_may_be_set(map->dim);
4732 /* Is this map actually a set?
4733 * Users should never call this function. Outside of isl,
4734 * the type should indicate whether something is a set or a map.
4736 int isl_map_is_set(__isl_keep isl_map *map)
4740 return isl_space_is_set(map->dim);
4743 struct isl_set *isl_map_range(struct isl_map *map)
4746 struct isl_set *set;
4750 if (isl_map_is_set(map))
4751 return (isl_set *)map;
4753 map = isl_map_cow(map);
4757 set = (struct isl_set *) map;
4758 set->dim = isl_space_range(set->dim);
4761 for (i = 0; i < map->n; ++i) {
4762 set->p[i] = isl_basic_map_range(map->p[i]);
4766 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4767 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4774 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4777 isl_space *domain_dim;
4779 map = isl_map_cow(map);
4783 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4784 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4785 map->dim = isl_space_join(map->dim, domain_dim);
4788 for (i = 0; i < map->n; ++i) {
4789 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4793 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4794 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4801 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4804 isl_space *range_dim;
4806 map = isl_map_cow(map);
4810 range_dim = isl_space_range(isl_map_get_space(map));
4811 range_dim = isl_space_from_range(range_dim);
4812 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4813 map->dim = isl_space_join(map->dim, range_dim);
4816 for (i = 0; i < map->n; ++i) {
4817 map->p[i] = isl_basic_map_range_map(map->p[i]);
4821 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4822 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4829 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4830 __isl_take isl_space *dim)
4833 struct isl_map *map = NULL;
4835 set = isl_set_cow(set);
4838 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4839 map = (struct isl_map *)set;
4840 for (i = 0; i < set->n; ++i) {
4841 map->p[i] = isl_basic_map_from_basic_set(
4842 set->p[i], isl_space_copy(dim));
4846 isl_space_free(map->dim);
4850 isl_space_free(dim);
4855 __isl_give isl_basic_map *isl_basic_map_from_domain(
4856 __isl_take isl_basic_set *bset)
4858 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4861 __isl_give isl_basic_map *isl_basic_map_from_range(
4862 __isl_take isl_basic_set *bset)
4865 space = isl_basic_set_get_space(bset);
4866 space = isl_space_from_range(space);
4867 bset = isl_basic_set_reset_space(bset, space);
4868 return (isl_basic_map *)bset;
4871 struct isl_map *isl_map_from_range(struct isl_set *set)
4874 space = isl_set_get_space(set);
4875 space = isl_space_from_range(space);
4876 set = isl_set_reset_space(set, space);
4877 return (struct isl_map *)set;
4880 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4882 return isl_map_reverse(isl_map_from_range(set));
4885 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4886 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4888 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4891 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4892 __isl_take isl_set *range)
4894 return isl_map_apply_range(isl_map_reverse(domain), range);
4897 struct isl_set *isl_set_from_map(struct isl_map *map)
4900 struct isl_set *set = NULL;
4904 map = isl_map_cow(map);
4907 map->dim = isl_space_as_set_space(map->dim);
4910 set = (struct isl_set *)map;
4911 for (i = 0; i < map->n; ++i) {
4912 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4922 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4925 struct isl_map *map;
4930 isl_die(dim->ctx, isl_error_internal,
4931 "negative number of basic maps", goto error);
4932 map = isl_alloc(dim->ctx, struct isl_map,
4933 sizeof(struct isl_map) +
4934 (n - 1) * sizeof(struct isl_basic_map *));
4938 map->ctx = dim->ctx;
4939 isl_ctx_ref(map->ctx);
4947 isl_space_free(dim);
4951 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4952 unsigned nparam, unsigned in, unsigned out, int n,
4955 struct isl_map *map;
4958 dims = isl_space_alloc(ctx, nparam, in, out);
4962 map = isl_map_alloc_space(dims, n, flags);
4966 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4968 struct isl_basic_map *bmap;
4969 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4970 bmap = isl_basic_map_set_to_empty(bmap);
4974 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4976 struct isl_basic_set *bset;
4977 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4978 bset = isl_basic_set_set_to_empty(bset);
4982 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4984 struct isl_basic_map *bmap;
4987 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4988 bmap = isl_basic_map_set_to_empty(bmap);
4992 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4994 struct isl_basic_map *bmap;
4997 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4998 bmap = isl_basic_map_set_to_empty(bmap);
5002 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5004 struct isl_basic_set *bset;
5007 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5008 bset = isl_basic_set_set_to_empty(bset);
5012 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5014 struct isl_basic_map *bmap;
5015 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5016 bmap = isl_basic_map_finalize(bmap);
5020 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5022 struct isl_basic_set *bset;
5023 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5024 bset = isl_basic_set_finalize(bset);
5028 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5031 unsigned total = isl_space_dim(dim, isl_dim_all);
5032 isl_basic_map *bmap;
5034 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5035 for (i = 0; i < total; ++i) {
5036 int k = isl_basic_map_alloc_inequality(bmap);
5039 isl_seq_clr(bmap->ineq[k], 1 + total);
5040 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5044 isl_basic_map_free(bmap);
5048 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5050 return isl_basic_map_nat_universe(dim);
5053 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5055 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5058 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5060 return isl_map_nat_universe(dim);
5063 __isl_give isl_basic_map *isl_basic_map_universe_like(
5064 __isl_keep isl_basic_map *model)
5068 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5071 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5075 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5078 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5079 __isl_keep isl_set *model)
5083 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5086 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5088 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5091 struct isl_map *isl_map_empty_like(struct isl_map *model)
5095 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5098 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5102 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5105 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5107 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5110 struct isl_set *isl_set_empty_like(struct isl_set *model)
5114 return isl_set_empty(isl_space_copy(model->dim));
5117 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5119 struct isl_map *map;
5122 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5123 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5127 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5129 struct isl_set *set;
5132 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5133 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5137 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5141 return isl_set_universe(isl_space_copy(model->dim));
5144 struct isl_map *isl_map_dup(struct isl_map *map)
5147 struct isl_map *dup;
5151 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5152 for (i = 0; i < map->n; ++i)
5153 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5157 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5158 __isl_take isl_basic_map *bmap)
5162 if (isl_basic_map_plain_is_empty(bmap)) {
5163 isl_basic_map_free(bmap);
5166 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5167 isl_assert(map->ctx, map->n < map->size, goto error);
5168 map->p[map->n] = bmap;
5170 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5176 isl_basic_map_free(bmap);
5180 void *isl_map_free(struct isl_map *map)
5190 isl_ctx_deref(map->ctx);
5191 for (i = 0; i < map->n; ++i)
5192 isl_basic_map_free(map->p[i]);
5193 isl_space_free(map->dim);
5199 struct isl_map *isl_map_extend(struct isl_map *base,
5200 unsigned nparam, unsigned n_in, unsigned n_out)
5204 base = isl_map_cow(base);
5208 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5211 for (i = 0; i < base->n; ++i) {
5212 base->p[i] = isl_basic_map_extend_space(base->p[i],
5213 isl_space_copy(base->dim), 0, 0, 0);
5223 struct isl_set *isl_set_extend(struct isl_set *base,
5224 unsigned nparam, unsigned dim)
5226 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5230 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5231 struct isl_basic_map *bmap, unsigned pos, int value)
5235 bmap = isl_basic_map_cow(bmap);
5236 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5237 j = isl_basic_map_alloc_equality(bmap);
5240 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5241 isl_int_set_si(bmap->eq[j][pos], -1);
5242 isl_int_set_si(bmap->eq[j][0], value);
5243 bmap = isl_basic_map_simplify(bmap);
5244 return isl_basic_map_finalize(bmap);
5246 isl_basic_map_free(bmap);
5250 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5251 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5255 bmap = isl_basic_map_cow(bmap);
5256 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5257 j = isl_basic_map_alloc_equality(bmap);
5260 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5261 isl_int_set_si(bmap->eq[j][pos], -1);
5262 isl_int_set(bmap->eq[j][0], value);
5263 bmap = isl_basic_map_simplify(bmap);
5264 return isl_basic_map_finalize(bmap);
5266 isl_basic_map_free(bmap);
5270 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5271 enum isl_dim_type type, unsigned pos, int value)
5275 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5276 return isl_basic_map_fix_pos_si(bmap,
5277 isl_basic_map_offset(bmap, type) + pos, value);
5279 isl_basic_map_free(bmap);
5283 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5284 enum isl_dim_type type, unsigned pos, isl_int value)
5288 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5289 return isl_basic_map_fix_pos(bmap,
5290 isl_basic_map_offset(bmap, type) + pos, value);
5292 isl_basic_map_free(bmap);
5296 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5297 enum isl_dim_type type, unsigned pos, int value)
5299 return (struct isl_basic_set *)
5300 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5304 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5305 enum isl_dim_type type, unsigned pos, isl_int value)
5307 return (struct isl_basic_set *)
5308 isl_basic_map_fix((struct isl_basic_map *)bset,
5312 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5313 unsigned input, int value)
5315 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5318 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5319 unsigned dim, int value)
5321 return (struct isl_basic_set *)
5322 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5323 isl_dim_set, dim, value);
5326 static int remove_if_empty(__isl_keep isl_map *map, int i)
5328 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5335 isl_basic_map_free(map->p[i]);
5336 if (i != map->n - 1) {
5337 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5338 map->p[i] = map->p[map->n - 1];
5345 struct isl_map *isl_map_fix_si(struct isl_map *map,
5346 enum isl_dim_type type, unsigned pos, int value)
5350 map = isl_map_cow(map);
5354 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5355 for (i = map->n - 1; i >= 0; --i) {
5356 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5357 if (remove_if_empty(map, i) < 0)
5360 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5367 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5368 enum isl_dim_type type, unsigned pos, int value)
5370 return (struct isl_set *)
5371 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5374 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5375 enum isl_dim_type type, unsigned pos, isl_int value)
5379 map = isl_map_cow(map);
5383 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5384 for (i = 0; i < map->n; ++i) {
5385 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5389 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5396 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5397 enum isl_dim_type type, unsigned pos, isl_int value)
5399 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5402 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5403 unsigned input, int value)
5405 return isl_map_fix_si(map, isl_dim_in, input, value);
5408 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5410 return (struct isl_set *)
5411 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5414 static __isl_give isl_basic_map *basic_map_bound_si(
5415 __isl_take isl_basic_map *bmap,
5416 enum isl_dim_type type, unsigned pos, int value, int upper)
5422 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5423 pos += isl_basic_map_offset(bmap, type);
5424 bmap = isl_basic_map_cow(bmap);
5425 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5426 j = isl_basic_map_alloc_inequality(bmap);
5429 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5431 isl_int_set_si(bmap->ineq[j][pos], -1);
5432 isl_int_set_si(bmap->ineq[j][0], value);
5434 isl_int_set_si(bmap->ineq[j][pos], 1);
5435 isl_int_set_si(bmap->ineq[j][0], -value);
5437 bmap = isl_basic_map_simplify(bmap);
5438 return isl_basic_map_finalize(bmap);
5440 isl_basic_map_free(bmap);
5444 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5445 __isl_take isl_basic_map *bmap,
5446 enum isl_dim_type type, unsigned pos, int value)
5448 return basic_map_bound_si(bmap, type, pos, value, 0);
5451 /* Constrain the values of the given dimension to be no greater than "value".
5453 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5454 __isl_take isl_basic_map *bmap,
5455 enum isl_dim_type type, unsigned pos, int value)
5457 return basic_map_bound_si(bmap, type, pos, value, 1);
5460 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5461 unsigned dim, isl_int value)
5465 bset = isl_basic_set_cow(bset);
5466 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5467 j = isl_basic_set_alloc_inequality(bset);
5470 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5471 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5472 isl_int_neg(bset->ineq[j][0], value);
5473 bset = isl_basic_set_simplify(bset);
5474 return isl_basic_set_finalize(bset);
5476 isl_basic_set_free(bset);
5480 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5481 enum isl_dim_type type, unsigned pos, int value, int upper)
5485 map = isl_map_cow(map);
5489 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5490 for (i = 0; i < map->n; ++i) {
5491 map->p[i] = basic_map_bound_si(map->p[i],
5492 type, pos, value, upper);
5496 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5503 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5504 enum isl_dim_type type, unsigned pos, int value)
5506 return map_bound_si(map, type, pos, value, 0);
5509 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5510 enum isl_dim_type type, unsigned pos, int value)
5512 return map_bound_si(map, type, pos, value, 1);
5515 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5516 enum isl_dim_type type, unsigned pos, int value)
5518 return (struct isl_set *)
5519 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5522 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5523 enum isl_dim_type type, unsigned pos, int value)
5525 return isl_map_upper_bound_si(set, type, pos, value);
5528 /* Bound the given variable of "bmap" from below (or above is "upper"
5529 * is set) to "value".
5531 static __isl_give isl_basic_map *basic_map_bound(
5532 __isl_take isl_basic_map *bmap,
5533 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5539 if (pos >= isl_basic_map_dim(bmap, type))
5540 isl_die(bmap->ctx, isl_error_invalid,
5541 "index out of bounds", goto error);
5542 pos += isl_basic_map_offset(bmap, type);
5543 bmap = isl_basic_map_cow(bmap);
5544 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5545 j = isl_basic_map_alloc_inequality(bmap);
5548 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5550 isl_int_set_si(bmap->ineq[j][pos], -1);
5551 isl_int_set(bmap->ineq[j][0], value);
5553 isl_int_set_si(bmap->ineq[j][pos], 1);
5554 isl_int_neg(bmap->ineq[j][0], value);
5556 bmap = isl_basic_map_simplify(bmap);
5557 return isl_basic_map_finalize(bmap);
5559 isl_basic_map_free(bmap);
5563 /* Bound the given variable of "map" from below (or above is "upper"
5564 * is set) to "value".
5566 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5567 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5571 map = isl_map_cow(map);
5575 if (pos >= isl_map_dim(map, type))
5576 isl_die(map->ctx, isl_error_invalid,
5577 "index out of bounds", goto error);
5578 for (i = map->n - 1; i >= 0; --i) {
5579 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5580 if (remove_if_empty(map, i) < 0)
5583 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5590 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5591 enum isl_dim_type type, unsigned pos, isl_int value)
5593 return map_bound(map, type, pos, value, 0);
5596 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5597 enum isl_dim_type type, unsigned pos, isl_int value)
5599 return map_bound(map, type, pos, value, 1);
5602 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5603 enum isl_dim_type type, unsigned pos, isl_int value)
5605 return isl_map_lower_bound(set, type, pos, value);
5608 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5609 enum isl_dim_type type, unsigned pos, isl_int value)
5611 return isl_map_upper_bound(set, type, pos, value);
5614 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5619 set = isl_set_cow(set);
5623 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5624 for (i = 0; i < set->n; ++i) {
5625 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5635 struct isl_map *isl_map_reverse(struct isl_map *map)
5639 map = isl_map_cow(map);
5643 map->dim = isl_space_reverse(map->dim);
5646 for (i = 0; i < map->n; ++i) {
5647 map->p[i] = isl_basic_map_reverse(map->p[i]);
5651 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5658 static struct isl_map *isl_basic_map_partial_lexopt(
5659 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5660 struct isl_set **empty, int max)
5664 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5665 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5667 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5669 isl_basic_map_free(bmap);
5670 isl_basic_set_free(dom);
5676 struct isl_map *isl_basic_map_partial_lexmax(
5677 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5678 struct isl_set **empty)
5680 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5683 struct isl_map *isl_basic_map_partial_lexmin(
5684 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5685 struct isl_set **empty)
5687 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5690 struct isl_set *isl_basic_set_partial_lexmin(
5691 struct isl_basic_set *bset, struct isl_basic_set *dom,
5692 struct isl_set **empty)
5694 return (struct isl_set *)
5695 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5699 struct isl_set *isl_basic_set_partial_lexmax(
5700 struct isl_basic_set *bset, struct isl_basic_set *dom,
5701 struct isl_set **empty)
5703 return (struct isl_set *)
5704 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5708 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5709 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5710 __isl_give isl_set **empty)
5712 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5715 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5716 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5717 __isl_give isl_set **empty)
5719 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5722 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5723 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5724 __isl_give isl_set **empty)
5726 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5729 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5730 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5731 __isl_give isl_set **empty)
5733 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5736 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5737 __isl_take isl_basic_map *bmap, int max)
5739 isl_basic_set *dom = NULL;
5740 isl_space *dom_space;
5744 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5745 dom = isl_basic_set_universe(dom_space);
5746 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5748 isl_basic_map_free(bmap);
5752 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5753 __isl_take isl_basic_map *bmap)
5755 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5759 #define TYPE isl_pw_multi_aff
5761 #define SUFFIX _pw_multi_aff
5763 #define EMPTY isl_pw_multi_aff_empty
5765 #define ADD isl_pw_multi_aff_union_add
5766 #include "isl_map_lexopt_templ.c"
5768 /* Given a map "map", compute the lexicographically minimal
5769 * (or maximal) image element for each domain element in dom,
5770 * in the form of an isl_pw_multi_aff.
5771 * Set *empty to those elements in dom that do not have an image element.
5773 * We first compute the lexicographically minimal or maximal element
5774 * in the first basic map. This results in a partial solution "res"
5775 * and a subset "todo" of dom that still need to be handled.
5776 * We then consider each of the remaining maps in "map" and successively
5777 * update both "res" and "todo".
5779 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
5780 __isl_take isl_map *map, __isl_take isl_set *dom,
5781 __isl_give isl_set **empty, int max)
5784 isl_pw_multi_aff *res;
5790 if (isl_map_plain_is_empty(map)) {
5795 return isl_pw_multi_aff_from_map(map);
5798 res = basic_map_partial_lexopt_pw_multi_aff(
5799 isl_basic_map_copy(map->p[0]),
5800 isl_set_copy(dom), &todo, max);
5802 for (i = 1; i < map->n; ++i) {
5803 isl_pw_multi_aff *res_i;
5806 res_i = basic_map_partial_lexopt_pw_multi_aff(
5807 isl_basic_map_copy(map->p[i]),
5808 isl_set_copy(dom), &todo_i, max);
5811 res = isl_pw_multi_aff_union_lexmax(res, res_i);
5813 res = isl_pw_multi_aff_union_lexmin(res, res_i);
5815 todo = isl_set_intersect(todo, todo_i);
5836 #define TYPE isl_map
5840 #define EMPTY isl_map_empty
5842 #define ADD isl_map_union_disjoint
5843 #include "isl_map_lexopt_templ.c"
5845 /* Given a map "map", compute the lexicographically minimal
5846 * (or maximal) image element for each domain element in dom.
5847 * Set *empty to those elements in dom that do not have an image element.
5849 * We first compute the lexicographically minimal or maximal element
5850 * in the first basic map. This results in a partial solution "res"
5851 * and a subset "todo" of dom that still need to be handled.
5852 * We then consider each of the remaining maps in "map" and successively
5853 * update both "res" and "todo".
5855 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5856 * Assume we are computing the lexicographical maximum.
5857 * We first compute the lexicographically maximal element in basic map i.
5858 * This results in a partial solution res_i and a subset todo_i.
5859 * Then we combine these results with those obtain for the first k basic maps
5860 * to obtain a result that is valid for the first k+1 basic maps.
5861 * In particular, the set where there is no solution is the set where
5862 * there is no solution for the first k basic maps and also no solution
5863 * for the ith basic map, i.e.,
5865 * todo^i = todo^k * todo_i
5867 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5868 * solutions, arbitrarily breaking ties in favor of res^k.
5869 * That is, when res^k(a) >= res_i(a), we pick res^k and
5870 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5871 * the lexicographic order.)
5872 * In practice, we compute
5874 * res^k * (res_i . "<=")
5878 * res_i * (res^k . "<")
5880 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5881 * where only one of res^k and res_i provides a solution and we simply pick
5888 * Note that we only compute these intersections when dom(res^k) intersects
5889 * dom(res_i). Otherwise, the only effect of these intersections is to
5890 * potentially break up res^k and res_i into smaller pieces.
5891 * We want to avoid such splintering as much as possible.
5892 * In fact, an earlier implementation of this function would look for
5893 * better results in the domain of res^k and for extra results in todo^k,
5894 * but this would always result in a splintering according to todo^k,
5895 * even when the domain of basic map i is disjoint from the domains of
5896 * the previous basic maps.
5898 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5899 __isl_take isl_map *map, __isl_take isl_set *dom,
5900 __isl_give isl_set **empty, int max)
5903 struct isl_map *res;
5904 struct isl_set *todo;
5909 if (isl_map_plain_is_empty(map)) {
5917 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5918 isl_set_copy(dom), &todo, max);
5920 for (i = 1; i < map->n; ++i) {
5924 isl_space *dim = isl_space_range(isl_map_get_space(res));
5926 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5927 isl_set_copy(dom), &todo_i, max);
5930 lt = isl_map_lex_lt(isl_space_copy(dim));
5931 le = isl_map_lex_le(dim);
5933 lt = isl_map_lex_gt(isl_space_copy(dim));
5934 le = isl_map_lex_ge(dim);
5936 lt = isl_map_apply_range(isl_map_copy(res), lt);
5937 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5938 le = isl_map_apply_range(isl_map_copy(res_i), le);
5939 le = isl_map_intersect(le, isl_map_copy(res));
5941 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5942 res = isl_map_intersect_domain(res,
5943 isl_set_copy(todo_i));
5944 res_i = isl_map_intersect_domain(res_i,
5945 isl_set_copy(todo));
5948 res = isl_map_union_disjoint(res, res_i);
5949 res = isl_map_union_disjoint(res, lt);
5950 res = isl_map_union_disjoint(res, le);
5952 todo = isl_set_intersect(todo, todo_i);
5972 __isl_give isl_map *isl_map_partial_lexmax(
5973 __isl_take isl_map *map, __isl_take isl_set *dom,
5974 __isl_give isl_set **empty)
5976 return isl_map_partial_lexopt(map, dom, empty, 1);
5979 __isl_give isl_map *isl_map_partial_lexmin(
5980 __isl_take isl_map *map, __isl_take isl_set *dom,
5981 __isl_give isl_set **empty)
5983 return isl_map_partial_lexopt(map, dom, empty, 0);
5986 __isl_give isl_set *isl_set_partial_lexmin(
5987 __isl_take isl_set *set, __isl_take isl_set *dom,
5988 __isl_give isl_set **empty)
5990 return (struct isl_set *)
5991 isl_map_partial_lexmin((struct isl_map *)set,
5995 __isl_give isl_set *isl_set_partial_lexmax(
5996 __isl_take isl_set *set, __isl_take isl_set *dom,
5997 __isl_give isl_set **empty)
5999 return (struct isl_set *)
6000 isl_map_partial_lexmax((struct isl_map *)set,
6004 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6006 struct isl_basic_set *dom = NULL;
6011 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
6012 dom = isl_basic_set_universe(dom_dim);
6013 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6015 isl_basic_map_free(bmap);
6019 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6021 return isl_basic_map_lexopt(bmap, 0);
6024 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6026 return isl_basic_map_lexopt(bmap, 1);
6029 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6031 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6034 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6036 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6039 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
6041 return (isl_set *)isl_map_lexmin((isl_map *)set);
6044 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
6046 return (isl_set *)isl_map_lexmax((isl_map *)set);
6049 /* Extract the first and only affine expression from list
6050 * and then add it to *pwaff with the given dom.
6051 * This domain is known to be disjoint from other domains
6052 * because of the way isl_basic_map_foreach_lexmax works.
6054 static int update_dim_opt(__isl_take isl_basic_set *dom,
6055 __isl_take isl_aff_list *list, void *user)
6057 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6059 isl_pw_aff **pwaff = user;
6060 isl_pw_aff *pwaff_i;
6064 if (isl_aff_list_n_aff(list) != 1)
6065 isl_die(ctx, isl_error_internal,
6066 "expecting single element list", goto error);
6068 aff = isl_aff_list_get_aff(list, 0);
6069 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6071 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6073 isl_aff_list_free(list);
6077 isl_basic_set_free(dom);
6078 isl_aff_list_free(list);
6082 /* Given a basic map with one output dimension, compute the minimum or
6083 * maximum of that dimension as an isl_pw_aff.
6085 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6086 * call update_dim_opt on each leaf of the result.
6088 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6091 isl_space *dim = isl_basic_map_get_space(bmap);
6095 dim = isl_space_from_domain(isl_space_domain(dim));
6096 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6097 pwaff = isl_pw_aff_empty(dim);
6099 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6101 return isl_pw_aff_free(pwaff);
6106 /* Compute the minimum or maximum of the given output dimension
6107 * as a function of the parameters and the input dimensions,
6108 * but independently of the other output dimensions.
6110 * We first project out the other output dimension and then compute
6111 * the "lexicographic" maximum in each basic map, combining the results
6112 * using isl_pw_aff_union_max.
6114 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6121 n_out = isl_map_dim(map, isl_dim_out);
6122 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6123 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6128 isl_space *dim = isl_map_get_space(map);
6129 dim = isl_space_domain(isl_space_from_range(dim));
6131 return isl_pw_aff_empty(dim);
6134 pwaff = basic_map_dim_opt(map->p[0], max);
6135 for (i = 1; i < map->n; ++i) {
6136 isl_pw_aff *pwaff_i;
6138 pwaff_i = basic_map_dim_opt(map->p[i], max);
6139 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6147 /* Compute the maximum of the given output dimension as a function of the
6148 * parameters and input dimensions, but independently of
6149 * the other output dimensions.
6151 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6153 return map_dim_opt(map, pos, 1);
6156 /* Compute the minimum or maximum of the given set dimension
6157 * as a function of the parameters,
6158 * but independently of the other set dimensions.
6160 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6163 return map_dim_opt(set, pos, max);
6166 /* Compute the maximum of the given set dimension as a function of the
6167 * parameters, but independently of the other set dimensions.
6169 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6171 return set_dim_opt(set, pos, 1);
6174 /* Compute the minimum of the given set dimension as a function of the
6175 * parameters, but independently of the other set dimensions.
6177 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6179 return set_dim_opt(set, pos, 0);
6182 /* Apply a preimage specified by "mat" on the parameters of "bset".
6183 * bset is assumed to have only parameters and divs.
6185 static struct isl_basic_set *basic_set_parameter_preimage(
6186 struct isl_basic_set *bset, struct isl_mat *mat)
6193 bset->dim = isl_space_cow(bset->dim);
6197 nparam = isl_basic_set_dim(bset, isl_dim_param);
6199 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6201 bset->dim->nparam = 0;
6202 bset->dim->n_out = nparam;
6203 bset = isl_basic_set_preimage(bset, mat);
6205 bset->dim->nparam = bset->dim->n_out;
6206 bset->dim->n_out = 0;
6211 isl_basic_set_free(bset);
6215 /* Apply a preimage specified by "mat" on the parameters of "set".
6216 * set is assumed to have only parameters and divs.
6218 static struct isl_set *set_parameter_preimage(
6219 struct isl_set *set, struct isl_mat *mat)
6221 isl_space *dim = NULL;
6227 dim = isl_space_copy(set->dim);
6228 dim = isl_space_cow(dim);
6232 nparam = isl_set_dim(set, isl_dim_param);
6234 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6237 dim->n_out = nparam;
6238 isl_set_reset_space(set, dim);
6239 set = isl_set_preimage(set, mat);
6242 dim = isl_space_copy(set->dim);
6243 dim = isl_space_cow(dim);
6246 dim->nparam = dim->n_out;
6248 isl_set_reset_space(set, dim);
6251 isl_space_free(dim);
6258 /* Intersect the basic set "bset" with the affine space specified by the
6259 * equalities in "eq".
6261 static struct isl_basic_set *basic_set_append_equalities(
6262 struct isl_basic_set *bset, struct isl_mat *eq)
6270 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6275 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6276 for (i = 0; i < eq->n_row; ++i) {
6277 k = isl_basic_set_alloc_equality(bset);
6280 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6281 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6285 bset = isl_basic_set_gauss(bset, NULL);
6286 bset = isl_basic_set_finalize(bset);
6291 isl_basic_set_free(bset);
6295 /* Intersect the set "set" with the affine space specified by the
6296 * equalities in "eq".
6298 static struct isl_set *set_append_equalities(struct isl_set *set,
6306 for (i = 0; i < set->n; ++i) {
6307 set->p[i] = basic_set_append_equalities(set->p[i],
6320 /* Project the given basic set onto its parameter domain, possibly introducing
6321 * new, explicit, existential variables in the constraints.
6322 * The input has parameters and (possibly implicit) existential variables.
6323 * The output has the same parameters, but only
6324 * explicit existentially quantified variables.
6326 * The actual projection is performed by pip, but pip doesn't seem
6327 * to like equalities very much, so we first remove the equalities
6328 * among the parameters by performing a variable compression on
6329 * the parameters. Afterward, an inverse transformation is performed
6330 * and the equalities among the parameters are inserted back in.
6332 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6336 struct isl_mat *T, *T2;
6337 struct isl_set *set;
6338 unsigned nparam, n_div;
6340 bset = isl_basic_set_cow(bset);
6344 if (bset->n_eq == 0)
6345 return isl_basic_set_lexmin(bset);
6347 bset = isl_basic_set_gauss(bset, NULL);
6350 if (isl_basic_set_plain_is_empty(bset))
6351 return isl_set_from_basic_set(bset);
6353 nparam = isl_basic_set_dim(bset, isl_dim_param);
6354 n_div = isl_basic_set_dim(bset, isl_dim_div);
6356 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6357 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6360 if (i == bset->n_eq)
6361 return isl_basic_set_lexmin(bset);
6363 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6365 eq = isl_mat_cow(eq);
6366 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6367 if (T && T->n_col == 0) {
6371 bset = isl_basic_set_set_to_empty(bset);
6372 return isl_set_from_basic_set(bset);
6374 bset = basic_set_parameter_preimage(bset, T);
6376 set = isl_basic_set_lexmin(bset);
6377 set = set_parameter_preimage(set, T2);
6378 set = set_append_equalities(set, eq);
6382 /* Compute an explicit representation for all the existentially
6383 * quantified variables.
6384 * The input and output dimensions are first turned into parameters.
6385 * compute_divs then returns a map with the same parameters and
6386 * no input or output dimensions and the dimension specification
6387 * is reset to that of the input.
6389 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6391 struct isl_basic_set *bset;
6392 struct isl_set *set;
6393 struct isl_map *map;
6394 isl_space *dim, *orig_dim = NULL;
6399 bmap = isl_basic_map_cow(bmap);
6403 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6404 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6405 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6406 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
6410 orig_dim = bmap->dim;
6412 bset = (struct isl_basic_set *)bmap;
6414 set = parameter_compute_divs(bset);
6415 map = (struct isl_map *)set;
6416 map = isl_map_reset_space(map, orig_dim);
6420 isl_basic_map_free(bmap);
6424 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6432 off = isl_space_dim(bmap->dim, isl_dim_all);
6433 for (i = 0; i < bmap->n_div; ++i) {
6434 if (isl_int_is_zero(bmap->div[i][0]))
6436 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6442 static int map_divs_known(__isl_keep isl_map *map)
6449 for (i = 0; i < map->n; ++i) {
6450 int known = isl_basic_map_divs_known(map->p[i]);
6458 /* If bmap contains any unknown divs, then compute explicit
6459 * expressions for them. However, this computation may be
6460 * quite expensive, so first try to remove divs that aren't
6463 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6466 struct isl_map *map;
6468 known = isl_basic_map_divs_known(bmap);
6472 return isl_map_from_basic_map(bmap);
6474 bmap = isl_basic_map_drop_redundant_divs(bmap);
6476 known = isl_basic_map_divs_known(bmap);
6480 return isl_map_from_basic_map(bmap);
6482 map = compute_divs(bmap);
6485 isl_basic_map_free(bmap);
6489 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6493 struct isl_map *res;
6500 known = map_divs_known(map);
6508 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6509 for (i = 1 ; i < map->n; ++i) {
6511 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6512 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6513 res = isl_map_union_disjoint(res, r2);
6515 res = isl_map_union(res, r2);
6522 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6524 return (struct isl_set *)
6525 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6528 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6530 return (struct isl_set *)
6531 isl_map_compute_divs((struct isl_map *)set);
6534 struct isl_set *isl_map_domain(struct isl_map *map)
6537 struct isl_set *set;
6542 map = isl_map_cow(map);
6546 set = (struct isl_set *)map;
6547 set->dim = isl_space_domain(set->dim);
6550 for (i = 0; i < map->n; ++i) {
6551 set->p[i] = isl_basic_map_domain(map->p[i]);
6555 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6556 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6563 /* Return the union of "map1" and "map2", where we assume for now that
6564 * "map1" and "map2" are disjoint. Note that the basic maps inside
6565 * "map1" or "map2" may not be disjoint from each other.
6566 * Also note that this function is also called from isl_map_union,
6567 * which takes care of handling the situation where "map1" and "map2"
6568 * may not be disjoint.
6570 * If one of the inputs is empty, we can simply return the other input.
6571 * Similarly, if one of the inputs is universal, then it is equal to the union.
6573 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6574 __isl_take isl_map *map2)
6578 struct isl_map *map = NULL;
6593 is_universe = isl_map_plain_is_universe(map1);
6594 if (is_universe < 0)
6601 is_universe = isl_map_plain_is_universe(map2);
6602 if (is_universe < 0)
6609 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6611 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6612 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6613 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6615 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6616 map1->n + map2->n, flags);
6619 for (i = 0; i < map1->n; ++i) {
6620 map = isl_map_add_basic_map(map,
6621 isl_basic_map_copy(map1->p[i]));
6625 for (i = 0; i < map2->n; ++i) {
6626 map = isl_map_add_basic_map(map,
6627 isl_basic_map_copy(map2->p[i]));
6641 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6642 __isl_take isl_map *map2)
6644 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6647 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6649 map1 = isl_map_union_disjoint(map1, map2);
6653 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6657 struct isl_set *isl_set_union_disjoint(
6658 struct isl_set *set1, struct isl_set *set2)
6660 return (struct isl_set *)
6661 isl_map_union_disjoint(
6662 (struct isl_map *)set1, (struct isl_map *)set2);
6665 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6667 return (struct isl_set *)
6668 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6671 /* Apply "fn" to pairs of elements from "map" and "set" and collect
6674 * "map" and "set" are assumed to be compatible and non-NULL.
6676 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
6677 __isl_take isl_set *set,
6678 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
6679 __isl_take isl_basic_set *bset))
6682 struct isl_map *result;
6685 if (isl_set_plain_is_universe(set)) {
6690 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6691 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6692 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6694 result = isl_map_alloc_space(isl_space_copy(map->dim),
6695 map->n * set->n, flags);
6696 for (i = 0; result && i < map->n; ++i)
6697 for (j = 0; j < set->n; ++j) {
6698 result = isl_map_add_basic_map(result,
6699 fn(isl_basic_map_copy(map->p[i]),
6700 isl_basic_set_copy(set->p[j])));
6710 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6711 __isl_take isl_set *set)
6716 if (!isl_map_compatible_range(map, set))
6717 isl_die(set->ctx, isl_error_invalid,
6718 "incompatible spaces", goto error);
6720 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
6727 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6728 __isl_take isl_set *set)
6730 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6733 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
6734 __isl_take isl_set *set)
6739 if (!isl_map_compatible_domain(map, set))
6740 isl_die(set->ctx, isl_error_invalid,
6741 "incompatible spaces", goto error);
6743 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
6750 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
6751 __isl_take isl_set *set)
6753 return isl_map_align_params_map_map_and(map, set,
6754 &map_intersect_domain);
6757 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6758 __isl_take isl_map *map2)
6762 map1 = isl_map_reverse(map1);
6763 map1 = isl_map_apply_range(map1, map2);
6764 return isl_map_reverse(map1);
6771 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6772 __isl_take isl_map *map2)
6774 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6777 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6778 __isl_take isl_map *map2)
6780 isl_space *dim_result;
6781 struct isl_map *result;
6787 dim_result = isl_space_join(isl_space_copy(map1->dim),
6788 isl_space_copy(map2->dim));
6790 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6793 for (i = 0; i < map1->n; ++i)
6794 for (j = 0; j < map2->n; ++j) {
6795 result = isl_map_add_basic_map(result,
6796 isl_basic_map_apply_range(
6797 isl_basic_map_copy(map1->p[i]),
6798 isl_basic_map_copy(map2->p[j])));
6804 if (result && result->n <= 1)
6805 ISL_F_SET(result, ISL_MAP_DISJOINT);
6813 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6814 __isl_take isl_map *map2)
6816 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6820 * returns range - domain
6822 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6824 isl_space *dims, *target_dim;
6825 struct isl_basic_set *bset;
6832 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6833 bmap->dim, isl_dim_out),
6835 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6836 dim = isl_basic_map_n_in(bmap);
6837 nparam = isl_basic_map_n_param(bmap);
6838 bset = isl_basic_set_from_basic_map(bmap);
6839 bset = isl_basic_set_cow(bset);
6840 dims = isl_basic_set_get_space(bset);
6841 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6842 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6843 bset = isl_basic_set_swap_vars(bset, 2*dim);
6844 for (i = 0; i < dim; ++i) {
6845 int j = isl_basic_map_alloc_equality(
6846 (struct isl_basic_map *)bset);
6848 bset = isl_basic_set_free(bset);
6851 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6852 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6853 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6854 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6856 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6857 bset = isl_basic_set_reset_space(bset, target_dim);
6860 isl_basic_map_free(bmap);
6865 * returns range - domain
6867 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
6871 struct isl_set *result;
6876 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6877 map->dim, isl_dim_out),
6879 dim = isl_map_get_space(map);
6880 dim = isl_space_domain(dim);
6881 result = isl_set_alloc_space(dim, map->n, 0);
6884 for (i = 0; i < map->n; ++i)
6885 result = isl_set_add_basic_set(result,
6886 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6895 * returns [domain -> range] -> range - domain
6897 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6898 __isl_take isl_basic_map *bmap)
6902 isl_basic_map *domain;
6906 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6907 isl_die(bmap->ctx, isl_error_invalid,
6908 "domain and range don't match", goto error);
6910 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6911 n = isl_basic_map_dim(bmap, isl_dim_in);
6913 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6914 domain = isl_basic_map_universe(dim);
6916 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6917 bmap = isl_basic_map_apply_range(bmap, domain);
6918 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6920 total = isl_basic_map_total_dim(bmap);
6922 for (i = 0; i < n; ++i) {
6923 k = isl_basic_map_alloc_equality(bmap);
6926 isl_seq_clr(bmap->eq[k], 1 + total);
6927 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6928 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6929 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6932 bmap = isl_basic_map_gauss(bmap, NULL);
6933 return isl_basic_map_finalize(bmap);
6935 isl_basic_map_free(bmap);
6940 * returns [domain -> range] -> range - domain
6942 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6945 isl_space *domain_dim;
6950 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6951 isl_die(map->ctx, isl_error_invalid,
6952 "domain and range don't match", goto error);
6954 map = isl_map_cow(map);
6958 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6959 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6960 map->dim = isl_space_join(map->dim, domain_dim);
6963 for (i = 0; i < map->n; ++i) {
6964 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6968 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6975 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6977 struct isl_basic_map *bmap;
6985 nparam = dims->nparam;
6987 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6991 for (i = 0; i < dim; ++i) {
6992 int j = isl_basic_map_alloc_equality(bmap);
6995 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6996 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6997 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6999 return isl_basic_map_finalize(bmap);
7001 isl_basic_map_free(bmap);
7005 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7009 if (dim->n_in != dim->n_out)
7010 isl_die(dim->ctx, isl_error_invalid,
7011 "number of input and output dimensions needs to be "
7012 "the same", goto error);
7013 return basic_map_identity(dim);
7015 isl_space_free(dim);
7019 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7021 if (!model || !model->dim)
7023 return isl_basic_map_identity(isl_space_copy(model->dim));
7026 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7028 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7031 struct isl_map *isl_map_identity_like(struct isl_map *model)
7033 if (!model || !model->dim)
7035 return isl_map_identity(isl_space_copy(model->dim));
7038 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7040 if (!model || !model->dim)
7042 return isl_map_identity(isl_space_copy(model->dim));
7045 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7047 isl_space *dim = isl_set_get_space(set);
7049 id = isl_map_identity(isl_space_map_from_set(dim));
7050 return isl_map_intersect_range(id, set);
7053 /* Construct a basic set with all set dimensions having only non-negative
7056 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7057 __isl_take isl_space *space)
7062 struct isl_basic_set *bset;
7066 nparam = space->nparam;
7068 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7071 for (i = 0; i < dim; ++i) {
7072 int k = isl_basic_set_alloc_inequality(bset);
7075 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7076 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7080 isl_basic_set_free(bset);
7084 /* Construct the half-space x_pos >= 0.
7086 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7090 isl_basic_set *nonneg;
7092 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7093 k = isl_basic_set_alloc_inequality(nonneg);
7096 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7097 isl_int_set_si(nonneg->ineq[k][pos], 1);
7099 return isl_basic_set_finalize(nonneg);
7101 isl_basic_set_free(nonneg);
7105 /* Construct the half-space x_pos <= -1.
7107 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7112 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7113 k = isl_basic_set_alloc_inequality(neg);
7116 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7117 isl_int_set_si(neg->ineq[k][0], -1);
7118 isl_int_set_si(neg->ineq[k][pos], -1);
7120 return isl_basic_set_finalize(neg);
7122 isl_basic_set_free(neg);
7126 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7127 enum isl_dim_type type, unsigned first, unsigned n)
7130 isl_basic_set *nonneg;
7138 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7140 for (i = 0; i < n; ++i) {
7141 nonneg = nonneg_halfspace(isl_set_get_space(set),
7142 pos(set->dim, type) + first + i);
7143 neg = neg_halfspace(isl_set_get_space(set),
7144 pos(set->dim, type) + first + i);
7146 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7155 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7156 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7163 if (isl_set_plain_is_empty(set)) {
7168 return fn(set, signs, user);
7171 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7173 half = isl_set_intersect(half, isl_set_copy(set));
7174 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7178 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7180 half = isl_set_intersect(half, set);
7181 return foreach_orthant(half, signs, first + 1, len, fn, user);
7187 /* Call "fn" on the intersections of "set" with each of the orthants
7188 * (except for obviously empty intersections). The orthant is identified
7189 * by the signs array, with each entry having value 1 or -1 according
7190 * to the sign of the corresponding variable.
7192 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7193 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7203 if (isl_set_plain_is_empty(set))
7206 nparam = isl_set_dim(set, isl_dim_param);
7207 nvar = isl_set_dim(set, isl_dim_set);
7209 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7211 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7219 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7221 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7224 int isl_basic_map_is_subset(
7225 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7228 struct isl_map *map1;
7229 struct isl_map *map2;
7231 if (!bmap1 || !bmap2)
7234 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7235 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7237 is_subset = isl_map_is_subset(map1, map2);
7245 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7246 __isl_keep isl_basic_set *bset2)
7248 return isl_basic_map_is_subset(bset1, bset2);
7251 int isl_basic_map_is_equal(
7252 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7256 if (!bmap1 || !bmap2)
7258 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7261 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7265 int isl_basic_set_is_equal(
7266 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7268 return isl_basic_map_is_equal(
7269 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7272 int isl_map_is_empty(struct isl_map *map)
7279 for (i = 0; i < map->n; ++i) {
7280 is_empty = isl_basic_map_is_empty(map->p[i]);
7289 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7291 return map ? map->n == 0 : -1;
7294 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7296 return isl_map_plain_is_empty(map);
7299 int isl_set_plain_is_empty(struct isl_set *set)
7301 return set ? set->n == 0 : -1;
7304 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7306 return isl_set_plain_is_empty(set);
7309 int isl_set_is_empty(struct isl_set *set)
7311 return isl_map_is_empty((struct isl_map *)set);
7314 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7319 return isl_space_is_equal(map1->dim, map2->dim);
7322 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7327 return isl_space_is_equal(set1->dim, set2->dim);
7330 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7336 is_subset = isl_map_is_subset(map1, map2);
7339 is_subset = isl_map_is_subset(map2, map1);
7343 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7345 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7348 int isl_basic_map_is_strict_subset(
7349 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7353 if (!bmap1 || !bmap2)
7355 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7358 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7359 if (is_subset == -1)
7364 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7370 is_subset = isl_map_is_subset(map1, map2);
7373 is_subset = isl_map_is_subset(map2, map1);
7374 if (is_subset == -1)
7379 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7381 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7384 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7388 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7391 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7395 return bset->n_eq == 0 && bset->n_ineq == 0;
7398 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7405 for (i = 0; i < map->n; ++i) {
7406 int r = isl_basic_map_is_universe(map->p[i]);
7414 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7416 return isl_map_plain_is_universe((isl_map *) set);
7419 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7421 return isl_set_plain_is_universe(set);
7424 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7426 struct isl_basic_set *bset = NULL;
7427 struct isl_vec *sample = NULL;
7434 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7437 if (isl_basic_map_is_universe(bmap))
7440 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7441 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7442 copy = isl_basic_map_remove_redundancies(copy);
7443 empty = isl_basic_map_plain_is_empty(copy);
7444 isl_basic_map_free(copy);
7448 total = 1 + isl_basic_map_total_dim(bmap);
7449 if (bmap->sample && bmap->sample->size == total) {
7450 int contains = isl_basic_map_contains(bmap, bmap->sample);
7456 isl_vec_free(bmap->sample);
7457 bmap->sample = NULL;
7458 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7461 sample = isl_basic_set_sample_vec(bset);
7464 empty = sample->size == 0;
7465 isl_vec_free(bmap->sample);
7466 bmap->sample = sample;
7468 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7473 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7477 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7480 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7482 return isl_basic_map_plain_is_empty(bmap);
7485 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7489 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7492 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7494 return isl_basic_set_plain_is_empty(bset);
7497 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7499 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7502 struct isl_map *isl_basic_map_union(
7503 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7505 struct isl_map *map;
7506 if (!bmap1 || !bmap2)
7509 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7511 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7514 map = isl_map_add_basic_map(map, bmap1);
7515 map = isl_map_add_basic_map(map, bmap2);
7518 isl_basic_map_free(bmap1);
7519 isl_basic_map_free(bmap2);
7523 struct isl_set *isl_basic_set_union(
7524 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7526 return (struct isl_set *)isl_basic_map_union(
7527 (struct isl_basic_map *)bset1,
7528 (struct isl_basic_map *)bset2);
7531 /* Order divs such that any div only depends on previous divs */
7532 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7540 off = isl_space_dim(bmap->dim, isl_dim_all);
7542 for (i = 0; i < bmap->n_div; ++i) {
7544 if (isl_int_is_zero(bmap->div[i][0]))
7546 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7550 isl_basic_map_swap_div(bmap, i, i + pos);
7556 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7558 return (struct isl_basic_set *)
7559 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7562 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7569 for (i = 0; i < map->n; ++i) {
7570 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7581 /* Apply the expansion computed by isl_merge_divs.
7582 * The expansion itself is given by "exp" while the resulting
7583 * list of divs is given by "div".
7585 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7586 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7591 bset = isl_basic_set_cow(bset);
7595 if (div->n_row < bset->n_div)
7596 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7597 "not an expansion", goto error);
7599 n_div = bset->n_div;
7600 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7601 div->n_row - n_div, 0,
7602 2 * (div->n_row - n_div));
7604 for (i = n_div; i < div->n_row; ++i)
7605 if (isl_basic_set_alloc_div(bset) < 0)
7609 for (i = div->n_row - 1; i >= 0; --i) {
7610 if (j >= 0 && exp[j] == i) {
7612 isl_basic_map_swap_div(bset, i, j);
7615 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7616 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7624 isl_basic_set_free(bset);
7629 /* Look for a div in dst that corresponds to the div "div" in src.
7630 * The divs before "div" in src and dst are assumed to be the same.
7632 * Returns -1 if no corresponding div was found and the position
7633 * of the corresponding div in dst otherwise.
7635 static int find_div(struct isl_basic_map *dst,
7636 struct isl_basic_map *src, unsigned div)
7640 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7642 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7643 for (i = div; i < dst->n_div; ++i)
7644 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7645 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7646 dst->n_div - div) == -1)
7651 struct isl_basic_map *isl_basic_map_align_divs(
7652 struct isl_basic_map *dst, struct isl_basic_map *src)
7660 if (src->n_div == 0)
7663 for (i = 0; i < src->n_div; ++i)
7664 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7666 src = isl_basic_map_order_divs(src);
7667 dst = isl_basic_map_cow(dst);
7670 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7671 src->n_div, 0, 2 * src->n_div);
7674 total = isl_space_dim(src->dim, isl_dim_all);
7675 for (i = 0; i < src->n_div; ++i) {
7676 int j = find_div(dst, src, i);
7678 j = isl_basic_map_alloc_div(dst);
7681 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7682 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7683 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7687 isl_basic_map_swap_div(dst, i, j);
7691 isl_basic_map_free(dst);
7695 struct isl_basic_set *isl_basic_set_align_divs(
7696 struct isl_basic_set *dst, struct isl_basic_set *src)
7698 return (struct isl_basic_set *)isl_basic_map_align_divs(
7699 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7702 struct isl_map *isl_map_align_divs(struct isl_map *map)
7710 map = isl_map_compute_divs(map);
7711 map = isl_map_cow(map);
7715 for (i = 1; i < map->n; ++i)
7716 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7717 for (i = 1; i < map->n; ++i) {
7718 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7720 return isl_map_free(map);
7723 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7727 struct isl_set *isl_set_align_divs(struct isl_set *set)
7729 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7732 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7733 __isl_take isl_map *map)
7737 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7738 map = isl_map_intersect_domain(map, set);
7739 set = isl_map_range(map);
7747 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7748 __isl_take isl_map *map)
7750 return isl_map_align_params_map_map_and(set, map, &set_apply);
7753 /* There is no need to cow as removing empty parts doesn't change
7754 * the meaning of the set.
7756 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7763 for (i = map->n - 1; i >= 0; --i)
7764 remove_if_empty(map, i);
7769 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7771 return (struct isl_set *)
7772 isl_map_remove_empty_parts((struct isl_map *)set);
7775 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7777 struct isl_basic_map *bmap;
7778 if (!map || map->n == 0)
7780 bmap = map->p[map->n-1];
7781 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7782 return isl_basic_map_copy(bmap);
7785 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7787 return (struct isl_basic_set *)
7788 isl_map_copy_basic_map((struct isl_map *)set);
7791 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7792 __isl_keep isl_basic_map *bmap)
7798 for (i = map->n-1; i >= 0; --i) {
7799 if (map->p[i] != bmap)
7801 map = isl_map_cow(map);
7804 isl_basic_map_free(map->p[i]);
7805 if (i != map->n-1) {
7806 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7807 map->p[i] = map->p[map->n-1];
7818 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7819 struct isl_basic_set *bset)
7821 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7822 (struct isl_basic_map *)bset);
7825 /* Given two basic sets bset1 and bset2, compute the maximal difference
7826 * between the values of dimension pos in bset1 and those in bset2
7827 * for any common value of the parameters and dimensions preceding pos.
7829 static enum isl_lp_result basic_set_maximal_difference_at(
7830 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7831 int pos, isl_int *opt)
7834 struct isl_basic_map *bmap1 = NULL;
7835 struct isl_basic_map *bmap2 = NULL;
7836 struct isl_ctx *ctx;
7837 struct isl_vec *obj;
7840 unsigned dim1, dim2;
7841 enum isl_lp_result res;
7843 if (!bset1 || !bset2)
7844 return isl_lp_error;
7846 nparam = isl_basic_set_n_param(bset1);
7847 dim1 = isl_basic_set_n_dim(bset1);
7848 dim2 = isl_basic_set_n_dim(bset2);
7849 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7850 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7851 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7852 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7853 if (!bmap1 || !bmap2)
7855 bmap1 = isl_basic_map_cow(bmap1);
7856 bmap1 = isl_basic_map_extend(bmap1, nparam,
7857 pos, (dim1 - pos) + (dim2 - pos),
7858 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7859 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7862 total = isl_basic_map_total_dim(bmap1);
7864 obj = isl_vec_alloc(ctx, 1 + total);
7867 isl_seq_clr(obj->block.data, 1 + total);
7868 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7869 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7870 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7872 isl_basic_map_free(bmap1);
7876 isl_basic_map_free(bmap2);
7878 isl_basic_map_free(bmap1);
7879 return isl_lp_error;
7882 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7883 * for any common value of the parameters and dimensions preceding pos
7884 * in both basic sets, the values of dimension pos in bset1 are
7885 * smaller or larger than those in bset2.
7888 * 1 if bset1 follows bset2
7889 * -1 if bset1 precedes bset2
7890 * 0 if bset1 and bset2 are incomparable
7891 * -2 if some error occurred.
7893 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7894 struct isl_basic_set *bset2, int pos)
7897 enum isl_lp_result res;
7902 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7904 if (res == isl_lp_empty)
7906 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7907 res == isl_lp_unbounded)
7909 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7918 /* Given two basic sets bset1 and bset2, check whether
7919 * for any common value of the parameters and dimensions preceding pos
7920 * there is a value of dimension pos in bset1 that is larger
7921 * than a value of the same dimension in bset2.
7924 * 1 if there exists such a pair
7925 * 0 if there is no such pair, but there is a pair of equal values
7927 * -2 if some error occurred.
7929 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7930 __isl_keep isl_basic_set *bset2, int pos)
7933 enum isl_lp_result res;
7938 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7940 if (res == isl_lp_empty)
7942 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7943 res == isl_lp_unbounded)
7945 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7947 else if (res == isl_lp_ok)
7956 /* Given two sets set1 and set2, check whether
7957 * for any common value of the parameters and dimensions preceding pos
7958 * there is a value of dimension pos in set1 that is larger
7959 * than a value of the same dimension in set2.
7962 * 1 if there exists such a pair
7963 * 0 if there is no such pair, but there is a pair of equal values
7965 * -2 if some error occurred.
7967 int isl_set_follows_at(__isl_keep isl_set *set1,
7968 __isl_keep isl_set *set2, int pos)
7976 for (i = 0; i < set1->n; ++i)
7977 for (j = 0; j < set2->n; ++j) {
7979 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7980 if (f == 1 || f == -2)
7989 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7990 unsigned pos, isl_int *val)
7998 total = isl_basic_map_total_dim(bmap);
7999 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8000 for (; d+1 > pos; --d)
8001 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8005 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8007 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8009 if (!isl_int_is_one(bmap->eq[i][1+d]))
8012 isl_int_neg(*val, bmap->eq[i][0]);
8018 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8019 unsigned pos, isl_int *val)
8031 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8034 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8035 for (i = 1; fixed == 1 && i < map->n; ++i) {
8036 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8037 if (fixed == 1 && isl_int_ne(tmp, v))
8041 isl_int_set(*val, v);
8047 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8048 unsigned pos, isl_int *val)
8050 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8054 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8057 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8060 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8061 enum isl_dim_type type, unsigned pos, isl_int *val)
8063 if (pos >= isl_basic_map_dim(bmap, type))
8065 return isl_basic_map_plain_has_fixed_var(bmap,
8066 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8069 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8070 enum isl_dim_type type, unsigned pos, isl_int *val)
8072 if (pos >= isl_map_dim(map, type))
8074 return isl_map_plain_has_fixed_var(map,
8075 map_offset(map, type) - 1 + pos, val);
8078 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8079 enum isl_dim_type type, unsigned pos, isl_int *val)
8081 return isl_map_plain_is_fixed(set, type, pos, val);
8084 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8085 enum isl_dim_type type, unsigned pos, isl_int *val)
8087 return isl_map_plain_is_fixed(map, type, pos, val);
8090 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8091 * then return this fixed value in *val.
8093 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8094 unsigned dim, isl_int *val)
8096 return isl_basic_set_plain_has_fixed_var(bset,
8097 isl_basic_set_n_param(bset) + dim, val);
8100 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8101 * then return this fixed value in *val.
8103 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8104 unsigned dim, isl_int *val)
8106 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8109 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8110 unsigned dim, isl_int *val)
8112 return isl_set_plain_dim_is_fixed(set, dim, val);
8115 /* Check if input variable in has fixed value and if so and if val is not NULL,
8116 * then return this fixed value in *val.
8118 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8119 unsigned in, isl_int *val)
8121 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8124 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8125 * and if val is not NULL, then return this lower bound in *val.
8127 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8128 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8130 int i, i_eq = -1, i_ineq = -1;
8137 total = isl_basic_set_total_dim(bset);
8138 nparam = isl_basic_set_n_param(bset);
8139 for (i = 0; i < bset->n_eq; ++i) {
8140 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8146 for (i = 0; i < bset->n_ineq; ++i) {
8147 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8149 if (i_eq != -1 || i_ineq != -1)
8153 if (i_eq == -1 && i_ineq == -1)
8155 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8156 /* The coefficient should always be one due to normalization. */
8157 if (!isl_int_is_one(c[1+nparam+dim]))
8159 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8161 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8162 total - nparam - dim - 1) != -1)
8165 isl_int_neg(*val, c[0]);
8169 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8170 unsigned dim, isl_int *val)
8182 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8186 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8188 for (i = 1; fixed == 1 && i < set->n; ++i) {
8189 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8191 if (fixed == 1 && isl_int_ne(tmp, v))
8195 isl_int_set(*val, v);
8206 /* uset_gist depends on constraints without existentially quantified
8207 * variables sorting first.
8209 static int qsort_constraint_cmp(const void *p1, const void *p2)
8211 const struct constraint *c1 = (const struct constraint *)p1;
8212 const struct constraint *c2 = (const struct constraint *)p2;
8214 unsigned size = isl_min(c1->size, c2->size);
8216 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8217 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8222 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8225 static struct isl_basic_map *isl_basic_map_sort_constraints(
8226 struct isl_basic_map *bmap)
8229 struct constraint *c;
8234 total = isl_basic_map_total_dim(bmap);
8235 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8238 for (i = 0; i < bmap->n_ineq; ++i) {
8240 c[i].c = bmap->ineq[i];
8242 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8243 for (i = 0; i < bmap->n_ineq; ++i)
8244 bmap->ineq[i] = c[i].c;
8248 isl_basic_map_free(bmap);
8252 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8253 __isl_take isl_basic_set *bset)
8255 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8256 (struct isl_basic_map *)bset);
8259 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8263 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8265 bmap = isl_basic_map_remove_redundancies(bmap);
8266 bmap = isl_basic_map_sort_constraints(bmap);
8268 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8272 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8274 return (struct isl_basic_set *)isl_basic_map_normalize(
8275 (struct isl_basic_map *)bset);
8278 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8279 const __isl_keep isl_basic_map *bmap2)
8286 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8287 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8288 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8289 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8290 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8291 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8292 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8293 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8294 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8295 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8296 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8298 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8300 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8302 if (bmap1->n_eq != bmap2->n_eq)
8303 return bmap1->n_eq - bmap2->n_eq;
8304 if (bmap1->n_ineq != bmap2->n_ineq)
8305 return bmap1->n_ineq - bmap2->n_ineq;
8306 if (bmap1->n_div != bmap2->n_div)
8307 return bmap1->n_div - bmap2->n_div;
8308 total = isl_basic_map_total_dim(bmap1);
8309 for (i = 0; i < bmap1->n_eq; ++i) {
8310 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8314 for (i = 0; i < bmap1->n_ineq; ++i) {
8315 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8319 for (i = 0; i < bmap1->n_div; ++i) {
8320 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8327 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8328 const __isl_keep isl_basic_set *bset2)
8330 return isl_basic_map_plain_cmp(bset1, bset2);
8333 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8339 if (set1->n != set2->n)
8340 return set1->n - set2->n;
8342 for (i = 0; i < set1->n; ++i) {
8343 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8351 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8352 __isl_keep isl_basic_map *bmap2)
8354 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8357 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8358 __isl_keep isl_basic_set *bset2)
8360 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8361 (isl_basic_map *)bset2);
8364 static int qsort_bmap_cmp(const void *p1, const void *p2)
8366 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8367 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8369 return isl_basic_map_plain_cmp(bmap1, bmap2);
8372 /* We normalize in place, but if anything goes wrong we need
8373 * to return NULL, so we need to make sure we don't change the
8374 * meaning of any possible other copies of map.
8376 struct isl_map *isl_map_normalize(struct isl_map *map)
8379 struct isl_basic_map *bmap;
8383 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8385 for (i = 0; i < map->n; ++i) {
8386 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8389 isl_basic_map_free(map->p[i]);
8392 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8393 ISL_F_SET(map, ISL_MAP_NORMALIZED);
8394 map = isl_map_remove_empty_parts(map);
8397 for (i = map->n - 1; i >= 1; --i) {
8398 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8400 isl_basic_map_free(map->p[i-1]);
8401 for (j = i; j < map->n; ++j)
8402 map->p[j-1] = map->p[j];
8412 struct isl_set *isl_set_normalize(struct isl_set *set)
8414 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8417 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8427 if (!isl_space_is_equal(map1->dim, map2->dim))
8430 map1 = isl_map_copy(map1);
8431 map2 = isl_map_copy(map2);
8432 map1 = isl_map_normalize(map1);
8433 map2 = isl_map_normalize(map2);
8436 equal = map1->n == map2->n;
8437 for (i = 0; equal && i < map1->n; ++i) {
8438 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8451 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8453 return isl_map_plain_is_equal(map1, map2);
8456 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8458 return isl_map_plain_is_equal((struct isl_map *)set1,
8459 (struct isl_map *)set2);
8462 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8464 return isl_set_plain_is_equal(set1, set2);
8467 /* Return an interval that ranges from min to max (inclusive)
8469 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8470 isl_int min, isl_int max)
8473 struct isl_basic_set *bset = NULL;
8475 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8479 k = isl_basic_set_alloc_inequality(bset);
8482 isl_int_set_si(bset->ineq[k][1], 1);
8483 isl_int_neg(bset->ineq[k][0], min);
8485 k = isl_basic_set_alloc_inequality(bset);
8488 isl_int_set_si(bset->ineq[k][1], -1);
8489 isl_int_set(bset->ineq[k][0], max);
8493 isl_basic_set_free(bset);
8497 /* Return the Cartesian product of the basic sets in list (in the given order).
8499 __isl_give isl_basic_set *isl_basic_set_list_product(
8500 __isl_take struct isl_basic_set_list *list)
8508 struct isl_basic_set *product = NULL;
8512 isl_assert(list->ctx, list->n > 0, goto error);
8513 isl_assert(list->ctx, list->p[0], goto error);
8514 nparam = isl_basic_set_n_param(list->p[0]);
8515 dim = isl_basic_set_n_dim(list->p[0]);
8516 extra = list->p[0]->n_div;
8517 n_eq = list->p[0]->n_eq;
8518 n_ineq = list->p[0]->n_ineq;
8519 for (i = 1; i < list->n; ++i) {
8520 isl_assert(list->ctx, list->p[i], goto error);
8521 isl_assert(list->ctx,
8522 nparam == isl_basic_set_n_param(list->p[i]), goto error);
8523 dim += isl_basic_set_n_dim(list->p[i]);
8524 extra += list->p[i]->n_div;
8525 n_eq += list->p[i]->n_eq;
8526 n_ineq += list->p[i]->n_ineq;
8528 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8533 for (i = 0; i < list->n; ++i) {
8534 isl_basic_set_add_constraints(product,
8535 isl_basic_set_copy(list->p[i]), dim);
8536 dim += isl_basic_set_n_dim(list->p[i]);
8538 isl_basic_set_list_free(list);
8541 isl_basic_set_free(product);
8542 isl_basic_set_list_free(list);
8546 struct isl_basic_map *isl_basic_map_product(
8547 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8549 isl_space *dim_result = NULL;
8550 struct isl_basic_map *bmap;
8551 unsigned in1, in2, out1, out2, nparam, total, pos;
8552 struct isl_dim_map *dim_map1, *dim_map2;
8554 if (!bmap1 || !bmap2)
8557 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8558 bmap2->dim, isl_dim_param), goto error);
8559 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8560 isl_space_copy(bmap2->dim));
8562 in1 = isl_basic_map_n_in(bmap1);
8563 in2 = isl_basic_map_n_in(bmap2);
8564 out1 = isl_basic_map_n_out(bmap1);
8565 out2 = isl_basic_map_n_out(bmap2);
8566 nparam = isl_basic_map_n_param(bmap1);
8568 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8569 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8570 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8571 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8572 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8573 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8574 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8575 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8576 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8577 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8578 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8580 bmap = isl_basic_map_alloc_space(dim_result,
8581 bmap1->n_div + bmap2->n_div,
8582 bmap1->n_eq + bmap2->n_eq,
8583 bmap1->n_ineq + bmap2->n_ineq);
8584 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8585 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8586 bmap = isl_basic_map_simplify(bmap);
8587 return isl_basic_map_finalize(bmap);
8589 isl_basic_map_free(bmap1);
8590 isl_basic_map_free(bmap2);
8594 __isl_give isl_basic_map *isl_basic_map_flat_product(
8595 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8597 isl_basic_map *prod;
8599 prod = isl_basic_map_product(bmap1, bmap2);
8600 prod = isl_basic_map_flatten(prod);
8604 __isl_give isl_basic_set *isl_basic_set_flat_product(
8605 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8607 return isl_basic_map_flat_range_product(bset1, bset2);
8610 __isl_give isl_basic_map *isl_basic_map_domain_product(
8611 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8613 isl_space *space_result = NULL;
8614 isl_basic_map *bmap;
8615 unsigned in1, in2, out, nparam, total, pos;
8616 struct isl_dim_map *dim_map1, *dim_map2;
8618 if (!bmap1 || !bmap2)
8621 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8622 isl_space_copy(bmap2->dim));
8624 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8625 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8626 out = isl_basic_map_dim(bmap1, isl_dim_out);
8627 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8629 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8630 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8631 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8632 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8633 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8634 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8635 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8636 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8637 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8638 isl_dim_map_div(dim_map1, bmap1, pos += out);
8639 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8641 bmap = isl_basic_map_alloc_space(space_result,
8642 bmap1->n_div + bmap2->n_div,
8643 bmap1->n_eq + bmap2->n_eq,
8644 bmap1->n_ineq + bmap2->n_ineq);
8645 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8646 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8647 bmap = isl_basic_map_simplify(bmap);
8648 return isl_basic_map_finalize(bmap);
8650 isl_basic_map_free(bmap1);
8651 isl_basic_map_free(bmap2);
8655 __isl_give isl_basic_map *isl_basic_map_range_product(
8656 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8658 isl_space *dim_result = NULL;
8659 isl_basic_map *bmap;
8660 unsigned in, out1, out2, nparam, total, pos;
8661 struct isl_dim_map *dim_map1, *dim_map2;
8663 if (!bmap1 || !bmap2)
8666 if (!isl_space_match(bmap1->dim, isl_dim_param,
8667 bmap2->dim, isl_dim_param))
8668 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
8669 "parameters don't match", goto error);
8671 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8672 isl_space_copy(bmap2->dim));
8674 in = isl_basic_map_dim(bmap1, isl_dim_in);
8675 out1 = isl_basic_map_n_out(bmap1);
8676 out2 = isl_basic_map_n_out(bmap2);
8677 nparam = isl_basic_map_n_param(bmap1);
8679 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8680 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8681 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8682 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8683 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8684 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8685 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8686 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8687 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8688 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8689 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8691 bmap = isl_basic_map_alloc_space(dim_result,
8692 bmap1->n_div + bmap2->n_div,
8693 bmap1->n_eq + bmap2->n_eq,
8694 bmap1->n_ineq + bmap2->n_ineq);
8695 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8696 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8697 bmap = isl_basic_map_simplify(bmap);
8698 return isl_basic_map_finalize(bmap);
8700 isl_basic_map_free(bmap1);
8701 isl_basic_map_free(bmap2);
8705 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8706 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8708 isl_basic_map *prod;
8710 prod = isl_basic_map_range_product(bmap1, bmap2);
8711 prod = isl_basic_map_flatten_range(prod);
8715 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8716 __isl_take isl_map *map2,
8717 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8718 __isl_take isl_space *right),
8719 __isl_give isl_basic_map *(*basic_map_product)(
8720 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8723 struct isl_map *result;
8729 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8730 map2->dim, isl_dim_param), goto error);
8732 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8733 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8734 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8736 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8737 isl_space_copy(map2->dim)),
8738 map1->n * map2->n, flags);
8741 for (i = 0; i < map1->n; ++i)
8742 for (j = 0; j < map2->n; ++j) {
8743 struct isl_basic_map *part;
8744 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8745 isl_basic_map_copy(map2->p[j]));
8746 if (isl_basic_map_is_empty(part))
8747 isl_basic_map_free(part);
8749 result = isl_map_add_basic_map(result, part);
8762 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8764 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8765 __isl_take isl_map *map2)
8767 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8770 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8771 __isl_take isl_map *map2)
8773 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8776 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8778 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8779 __isl_take isl_map *map2)
8783 prod = isl_map_product(map1, map2);
8784 prod = isl_map_flatten(prod);
8788 /* Given two set A and B, construct its Cartesian product A x B.
8790 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8792 return isl_map_range_product(set1, set2);
8795 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8796 __isl_take isl_set *set2)
8798 return isl_map_flat_range_product(set1, set2);
8801 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8803 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8804 __isl_take isl_map *map2)
8806 return map_product(map1, map2, &isl_space_domain_product,
8807 &isl_basic_map_domain_product);
8810 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8812 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8813 __isl_take isl_map *map2)
8815 return map_product(map1, map2, &isl_space_range_product,
8816 &isl_basic_map_range_product);
8819 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8820 __isl_take isl_map *map2)
8822 return isl_map_align_params_map_map_and(map1, map2,
8823 &map_domain_product_aligned);
8826 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8827 __isl_take isl_map *map2)
8829 return isl_map_align_params_map_map_and(map1, map2,
8830 &map_range_product_aligned);
8833 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8835 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8836 __isl_take isl_map *map2)
8840 prod = isl_map_domain_product(map1, map2);
8841 prod = isl_map_flatten_domain(prod);
8845 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8847 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8848 __isl_take isl_map *map2)
8852 prod = isl_map_range_product(map1, map2);
8853 prod = isl_map_flatten_range(prod);
8857 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8860 uint32_t hash = isl_hash_init();
8865 bmap = isl_basic_map_copy(bmap);
8866 bmap = isl_basic_map_normalize(bmap);
8869 total = isl_basic_map_total_dim(bmap);
8870 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8871 for (i = 0; i < bmap->n_eq; ++i) {
8873 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8874 isl_hash_hash(hash, c_hash);
8876 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8877 for (i = 0; i < bmap->n_ineq; ++i) {
8879 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8880 isl_hash_hash(hash, c_hash);
8882 isl_hash_byte(hash, bmap->n_div & 0xFF);
8883 for (i = 0; i < bmap->n_div; ++i) {
8885 if (isl_int_is_zero(bmap->div[i][0]))
8887 isl_hash_byte(hash, i & 0xFF);
8888 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8889 isl_hash_hash(hash, c_hash);
8891 isl_basic_map_free(bmap);
8895 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8897 return isl_basic_map_get_hash((isl_basic_map *)bset);
8900 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8907 map = isl_map_copy(map);
8908 map = isl_map_normalize(map);
8912 hash = isl_hash_init();
8913 for (i = 0; i < map->n; ++i) {
8915 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8916 isl_hash_hash(hash, bmap_hash);
8924 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8926 return isl_map_get_hash((isl_map *)set);
8929 /* Check if the value for dimension dim is completely determined
8930 * by the values of the other parameters and variables.
8931 * That is, check if dimension dim is involved in an equality.
8933 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8940 nparam = isl_basic_set_n_param(bset);
8941 for (i = 0; i < bset->n_eq; ++i)
8942 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8947 /* Check if the value for dimension dim is completely determined
8948 * by the values of the other parameters and variables.
8949 * That is, check if dimension dim is involved in an equality
8950 * for each of the subsets.
8952 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8958 for (i = 0; i < set->n; ++i) {
8960 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8967 int isl_set_n_basic_set(__isl_keep isl_set *set)
8969 return set ? set->n : 0;
8972 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8973 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8980 for (i = 0; i < map->n; ++i)
8981 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8987 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8988 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8995 for (i = 0; i < set->n; ++i)
8996 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9002 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9009 bset = isl_basic_set_cow(bset);
9013 dim = isl_basic_set_get_space(bset);
9014 dim = isl_space_lift(dim, bset->n_div);
9017 isl_space_free(bset->dim);
9019 bset->extra -= bset->n_div;
9022 bset = isl_basic_set_finalize(bset);
9026 isl_basic_set_free(bset);
9030 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9036 set = isl_set_align_divs(set);
9041 set = isl_set_cow(set);
9045 n_div = set->p[0]->n_div;
9046 dim = isl_set_get_space(set);
9047 dim = isl_space_lift(dim, n_div);
9050 isl_space_free(set->dim);
9053 for (i = 0; i < set->n; ++i) {
9054 set->p[i] = isl_basic_set_lift(set->p[i]);
9065 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9068 struct isl_basic_map *bmap;
9075 set = isl_set_align_divs(set);
9080 dim = isl_set_get_space(set);
9081 if (set->n == 0 || set->p[0]->n_div == 0) {
9083 return isl_map_identity(isl_space_map_from_set(dim));
9086 n_div = set->p[0]->n_div;
9087 dim = isl_space_map_from_set(dim);
9088 n_param = isl_space_dim(dim, isl_dim_param);
9089 n_set = isl_space_dim(dim, isl_dim_in);
9090 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9091 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9092 for (i = 0; i < n_set; ++i)
9093 bmap = var_equal(bmap, i);
9095 total = n_param + n_set + n_set + n_div;
9096 for (i = 0; i < n_div; ++i) {
9097 k = isl_basic_map_alloc_inequality(bmap);
9100 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9101 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9102 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9103 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9104 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9105 set->p[0]->div[i][0]);
9107 l = isl_basic_map_alloc_inequality(bmap);
9110 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9111 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9112 set->p[0]->div[i][0]);
9113 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9117 bmap = isl_basic_map_simplify(bmap);
9118 bmap = isl_basic_map_finalize(bmap);
9119 return isl_map_from_basic_map(bmap);
9122 isl_basic_map_free(bmap);
9126 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9134 dim = isl_basic_set_total_dim(bset);
9135 size += bset->n_eq * (1 + dim);
9136 size += bset->n_ineq * (1 + dim);
9137 size += bset->n_div * (2 + dim);
9142 int isl_set_size(__isl_keep isl_set *set)
9150 for (i = 0; i < set->n; ++i)
9151 size += isl_basic_set_size(set->p[i]);
9156 /* Check if there is any lower bound (if lower == 0) and/or upper
9157 * bound (if upper == 0) on the specified dim.
9159 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9160 enum isl_dim_type type, unsigned pos, int lower, int upper)
9167 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9169 pos += isl_basic_map_offset(bmap, type);
9171 for (i = 0; i < bmap->n_div; ++i) {
9172 if (isl_int_is_zero(bmap->div[i][0]))
9174 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9178 for (i = 0; i < bmap->n_eq; ++i)
9179 if (!isl_int_is_zero(bmap->eq[i][pos]))
9182 for (i = 0; i < bmap->n_ineq; ++i) {
9183 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9190 return lower && upper;
9193 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9194 enum isl_dim_type type, unsigned pos)
9196 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9199 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9200 enum isl_dim_type type, unsigned pos)
9202 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9205 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9206 enum isl_dim_type type, unsigned pos)
9208 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9211 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9212 enum isl_dim_type type, unsigned pos)
9219 for (i = 0; i < map->n; ++i) {
9221 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9222 if (bounded < 0 || !bounded)
9229 /* Return 1 if the specified dim is involved in both an upper bound
9230 * and a lower bound.
9232 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9233 enum isl_dim_type type, unsigned pos)
9235 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9238 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9240 static int has_any_bound(__isl_keep isl_map *map,
9241 enum isl_dim_type type, unsigned pos,
9242 int (*fn)(__isl_keep isl_basic_map *bmap,
9243 enum isl_dim_type type, unsigned pos))
9250 for (i = 0; i < map->n; ++i) {
9252 bounded = fn(map->p[i], type, pos);
9253 if (bounded < 0 || bounded)
9260 /* Return 1 if the specified dim is involved in any lower bound.
9262 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9263 enum isl_dim_type type, unsigned pos)
9265 return has_any_bound(set, type, pos,
9266 &isl_basic_map_dim_has_lower_bound);
9269 /* Return 1 if the specified dim is involved in any upper bound.
9271 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9272 enum isl_dim_type type, unsigned pos)
9274 return has_any_bound(set, type, pos,
9275 &isl_basic_map_dim_has_upper_bound);
9278 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9280 static int has_bound(__isl_keep isl_map *map,
9281 enum isl_dim_type type, unsigned pos,
9282 int (*fn)(__isl_keep isl_basic_map *bmap,
9283 enum isl_dim_type type, unsigned pos))
9290 for (i = 0; i < map->n; ++i) {
9292 bounded = fn(map->p[i], type, pos);
9293 if (bounded < 0 || !bounded)
9300 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9302 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9303 enum isl_dim_type type, unsigned pos)
9305 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9308 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9310 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9311 enum isl_dim_type type, unsigned pos)
9313 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9316 /* For each of the "n" variables starting at "first", determine
9317 * the sign of the variable and put the results in the first "n"
9318 * elements of the array "signs".
9320 * 1 means that the variable is non-negative
9321 * -1 means that the variable is non-positive
9322 * 0 means the variable attains both positive and negative values.
9324 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9325 unsigned first, unsigned n, int *signs)
9327 isl_vec *bound = NULL;
9328 struct isl_tab *tab = NULL;
9329 struct isl_tab_undo *snap;
9332 if (!bset || !signs)
9335 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9336 tab = isl_tab_from_basic_set(bset, 0);
9340 isl_seq_clr(bound->el, bound->size);
9341 isl_int_set_si(bound->el[0], -1);
9343 snap = isl_tab_snap(tab);
9344 for (i = 0; i < n; ++i) {
9347 isl_int_set_si(bound->el[1 + first + i], -1);
9348 if (isl_tab_add_ineq(tab, bound->el) < 0)
9351 isl_int_set_si(bound->el[1 + first + i], 0);
9352 if (isl_tab_rollback(tab, snap) < 0)
9360 isl_int_set_si(bound->el[1 + first + i], 1);
9361 if (isl_tab_add_ineq(tab, bound->el) < 0)
9364 isl_int_set_si(bound->el[1 + first + i], 0);
9365 if (isl_tab_rollback(tab, snap) < 0)
9368 signs[i] = empty ? -1 : 0;
9372 isl_vec_free(bound);
9376 isl_vec_free(bound);
9380 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9381 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9383 if (!bset || !signs)
9385 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9388 first += pos(bset->dim, type) - 1;
9389 return isl_basic_set_vars_get_sign(bset, first, n, signs);
9392 /* Check if the given basic map is obviously single-valued.
9393 * In particular, for each output dimension, check that there is
9394 * an equality that defines the output dimension in terms of
9395 * earlier dimensions.
9397 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9407 total = 1 + isl_basic_map_total_dim(bmap);
9408 n_out = isl_basic_map_dim(bmap, isl_dim_out);
9409 o_out = isl_basic_map_offset(bmap, isl_dim_out);
9411 for (i = 0; i < n_out; ++i) {
9412 for (j = 0; j < bmap->n_eq; ++j) {
9413 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9415 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9416 total - (o_out + i + 1)) == -1)
9419 if (j >= bmap->n_eq)
9426 /* Check if the given basic map is single-valued.
9431 * and check if the result is a subset of the identity mapping.
9433 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9436 isl_basic_map *test;
9440 sv = isl_basic_map_plain_is_single_valued(bmap);
9444 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9445 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9447 space = isl_basic_map_get_space(bmap);
9448 space = isl_space_map_from_set(isl_space_range(space));
9449 id = isl_basic_map_identity(space);
9451 sv = isl_basic_map_is_subset(test, id);
9453 isl_basic_map_free(test);
9454 isl_basic_map_free(id);
9459 /* Check if the given map is obviously single-valued.
9461 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9470 return isl_basic_map_plain_is_single_valued(map->p[0]);
9473 /* Check if the given map is single-valued.
9478 * and check if the result is a subset of the identity mapping.
9480 int isl_map_is_single_valued(__isl_keep isl_map *map)
9487 sv = isl_map_plain_is_single_valued(map);
9491 test = isl_map_reverse(isl_map_copy(map));
9492 test = isl_map_apply_range(test, isl_map_copy(map));
9494 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9495 id = isl_map_identity(dim);
9497 sv = isl_map_is_subset(test, id);
9505 int isl_map_is_injective(__isl_keep isl_map *map)
9509 map = isl_map_copy(map);
9510 map = isl_map_reverse(map);
9511 in = isl_map_is_single_valued(map);
9517 /* Check if the given map is obviously injective.
9519 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9523 map = isl_map_copy(map);
9524 map = isl_map_reverse(map);
9525 in = isl_map_plain_is_single_valued(map);
9531 int isl_map_is_bijective(__isl_keep isl_map *map)
9535 sv = isl_map_is_single_valued(map);
9539 return isl_map_is_injective(map);
9542 int isl_set_is_singleton(__isl_keep isl_set *set)
9544 return isl_map_is_single_valued((isl_map *)set);
9547 int isl_map_is_translation(__isl_keep isl_map *map)
9552 delta = isl_map_deltas(isl_map_copy(map));
9553 ok = isl_set_is_singleton(delta);
9554 isl_set_free(delta);
9559 static int unique(isl_int *p, unsigned pos, unsigned len)
9561 if (isl_seq_first_non_zero(p, pos) != -1)
9563 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9568 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9577 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9580 nvar = isl_basic_set_dim(bset, isl_dim_set);
9581 ovar = isl_space_offset(bset->dim, isl_dim_set);
9582 for (j = 0; j < nvar; ++j) {
9583 int lower = 0, upper = 0;
9584 for (i = 0; i < bset->n_eq; ++i) {
9585 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9587 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9593 for (i = 0; i < bset->n_ineq; ++i) {
9594 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
9596 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
9598 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
9603 if (!lower || !upper)
9610 int isl_set_is_box(__isl_keep isl_set *set)
9617 return isl_basic_set_is_box(set->p[0]);
9620 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
9625 return isl_space_is_wrapping(bset->dim);
9628 int isl_set_is_wrapping(__isl_keep isl_set *set)
9633 return isl_space_is_wrapping(set->dim);
9636 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
9638 bmap = isl_basic_map_cow(bmap);
9642 bmap->dim = isl_space_wrap(bmap->dim);
9646 bmap = isl_basic_map_finalize(bmap);
9648 return (isl_basic_set *)bmap;
9650 isl_basic_map_free(bmap);
9654 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9658 map = isl_map_cow(map);
9662 for (i = 0; i < map->n; ++i) {
9663 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9667 map->dim = isl_space_wrap(map->dim);
9671 return (isl_set *)map;
9677 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9679 bset = isl_basic_set_cow(bset);
9683 bset->dim = isl_space_unwrap(bset->dim);
9687 bset = isl_basic_set_finalize(bset);
9689 return (isl_basic_map *)bset;
9691 isl_basic_set_free(bset);
9695 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9702 if (!isl_set_is_wrapping(set))
9703 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9706 set = isl_set_cow(set);
9710 for (i = 0; i < set->n; ++i) {
9711 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9716 set->dim = isl_space_unwrap(set->dim);
9720 return (isl_map *)set;
9726 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9727 enum isl_dim_type type)
9732 if (!isl_space_is_named_or_nested(bmap->dim, type))
9735 bmap = isl_basic_map_cow(bmap);
9739 bmap->dim = isl_space_reset(bmap->dim, type);
9743 bmap = isl_basic_map_finalize(bmap);
9747 isl_basic_map_free(bmap);
9751 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9752 enum isl_dim_type type)
9759 if (!isl_space_is_named_or_nested(map->dim, type))
9762 map = isl_map_cow(map);
9766 for (i = 0; i < map->n; ++i) {
9767 map->p[i] = isl_basic_map_reset(map->p[i], type);
9771 map->dim = isl_space_reset(map->dim, type);
9781 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9786 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9789 bmap = isl_basic_map_cow(bmap);
9793 bmap->dim = isl_space_flatten(bmap->dim);
9797 bmap = isl_basic_map_finalize(bmap);
9801 isl_basic_map_free(bmap);
9805 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9807 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9810 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9811 __isl_take isl_basic_map *bmap)
9816 if (!bmap->dim->nested[0])
9819 bmap = isl_basic_map_cow(bmap);
9823 bmap->dim = isl_space_flatten_domain(bmap->dim);
9827 bmap = isl_basic_map_finalize(bmap);
9831 isl_basic_map_free(bmap);
9835 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9836 __isl_take isl_basic_map *bmap)
9841 if (!bmap->dim->nested[1])
9844 bmap = isl_basic_map_cow(bmap);
9848 bmap->dim = isl_space_flatten_range(bmap->dim);
9852 bmap = isl_basic_map_finalize(bmap);
9856 isl_basic_map_free(bmap);
9860 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9867 if (!map->dim->nested[0] && !map->dim->nested[1])
9870 map = isl_map_cow(map);
9874 for (i = 0; i < map->n; ++i) {
9875 map->p[i] = isl_basic_map_flatten(map->p[i]);
9879 map->dim = isl_space_flatten(map->dim);
9889 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9891 return (isl_set *)isl_map_flatten((isl_map *)set);
9894 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9896 isl_space *dim, *flat_dim;
9899 dim = isl_set_get_space(set);
9900 flat_dim = isl_space_flatten(isl_space_copy(dim));
9901 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9902 map = isl_map_intersect_domain(map, set);
9907 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9914 if (!map->dim->nested[0])
9917 map = isl_map_cow(map);
9921 for (i = 0; i < map->n; ++i) {
9922 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9926 map->dim = isl_space_flatten_domain(map->dim);
9936 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9943 if (!map->dim->nested[1])
9946 map = isl_map_cow(map);
9950 for (i = 0; i < map->n; ++i) {
9951 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9955 map->dim = isl_space_flatten_range(map->dim);
9965 /* Reorder the dimensions of "bmap" according to the given dim_map
9966 * and set the dimension specification to "dim".
9968 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9969 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9974 bmap = isl_basic_map_cow(bmap);
9975 if (!bmap || !dim || !dim_map)
9978 flags = bmap->flags;
9979 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
9980 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
9981 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
9982 res = isl_basic_map_alloc_space(dim,
9983 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9984 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9987 res = isl_basic_map_finalize(res);
9991 isl_basic_map_free(bmap);
9992 isl_space_free(dim);
9996 /* Reorder the dimensions of "map" according to given reordering.
9998 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9999 __isl_take isl_reordering *r)
10002 struct isl_dim_map *dim_map;
10004 map = isl_map_cow(map);
10005 dim_map = isl_dim_map_from_reordering(r);
10006 if (!map || !r || !dim_map)
10009 for (i = 0; i < map->n; ++i) {
10010 struct isl_dim_map *dim_map_i;
10012 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10014 map->p[i] = isl_basic_map_realign(map->p[i],
10015 isl_space_copy(r->dim), dim_map_i);
10021 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10023 isl_reordering_free(r);
10029 isl_reordering_free(r);
10033 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10034 __isl_take isl_reordering *r)
10036 return (isl_set *)isl_map_realign((isl_map *)set, r);
10039 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10040 __isl_take isl_space *model)
10044 if (!map || !model)
10047 ctx = isl_space_get_ctx(model);
10048 if (!isl_space_has_named_params(model))
10049 isl_die(ctx, isl_error_invalid,
10050 "model has unnamed parameters", goto error);
10051 if (!isl_space_has_named_params(map->dim))
10052 isl_die(ctx, isl_error_invalid,
10053 "relation has unnamed parameters", goto error);
10054 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10055 isl_reordering *exp;
10057 model = isl_space_drop_dims(model, isl_dim_in,
10058 0, isl_space_dim(model, isl_dim_in));
10059 model = isl_space_drop_dims(model, isl_dim_out,
10060 0, isl_space_dim(model, isl_dim_out));
10061 exp = isl_parameter_alignment_reordering(map->dim, model);
10062 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10063 map = isl_map_realign(map, exp);
10066 isl_space_free(model);
10069 isl_space_free(model);
10074 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10075 __isl_take isl_space *model)
10077 return isl_map_align_params(set, model);
10080 /* Align the parameters of "bmap" to those of "model", introducing
10081 * additional parameters if needed.
10083 __isl_give isl_basic_map *isl_basic_map_align_params(
10084 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10088 if (!bmap || !model)
10091 ctx = isl_space_get_ctx(model);
10092 if (!isl_space_has_named_params(model))
10093 isl_die(ctx, isl_error_invalid,
10094 "model has unnamed parameters", goto error);
10095 if (!isl_space_has_named_params(bmap->dim))
10096 isl_die(ctx, isl_error_invalid,
10097 "relation has unnamed parameters", goto error);
10098 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10099 isl_reordering *exp;
10100 struct isl_dim_map *dim_map;
10102 model = isl_space_drop_dims(model, isl_dim_in,
10103 0, isl_space_dim(model, isl_dim_in));
10104 model = isl_space_drop_dims(model, isl_dim_out,
10105 0, isl_space_dim(model, isl_dim_out));
10106 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10107 exp = isl_reordering_extend_space(exp,
10108 isl_basic_map_get_space(bmap));
10109 dim_map = isl_dim_map_from_reordering(exp);
10110 bmap = isl_basic_map_realign(bmap,
10111 exp ? isl_space_copy(exp->dim) : NULL,
10112 isl_dim_map_extend(dim_map, bmap));
10113 isl_reordering_free(exp);
10117 isl_space_free(model);
10120 isl_space_free(model);
10121 isl_basic_map_free(bmap);
10125 /* Align the parameters of "bset" to those of "model", introducing
10126 * additional parameters if needed.
10128 __isl_give isl_basic_set *isl_basic_set_align_params(
10129 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10131 return isl_basic_map_align_params(bset, model);
10134 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10135 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10136 enum isl_dim_type c2, enum isl_dim_type c3,
10137 enum isl_dim_type c4, enum isl_dim_type c5)
10139 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10140 struct isl_mat *mat;
10146 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10147 isl_basic_map_total_dim(bmap) + 1);
10150 for (i = 0; i < bmap->n_eq; ++i)
10151 for (j = 0, pos = 0; j < 5; ++j) {
10152 int off = isl_basic_map_offset(bmap, c[j]);
10153 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10154 isl_int_set(mat->row[i][pos],
10155 bmap->eq[i][off + k]);
10163 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10164 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10165 enum isl_dim_type c2, enum isl_dim_type c3,
10166 enum isl_dim_type c4, enum isl_dim_type c5)
10168 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10169 struct isl_mat *mat;
10175 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10176 isl_basic_map_total_dim(bmap) + 1);
10179 for (i = 0; i < bmap->n_ineq; ++i)
10180 for (j = 0, pos = 0; j < 5; ++j) {
10181 int off = isl_basic_map_offset(bmap, c[j]);
10182 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10183 isl_int_set(mat->row[i][pos],
10184 bmap->ineq[i][off + k]);
10192 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10193 __isl_take isl_space *dim,
10194 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10195 enum isl_dim_type c2, enum isl_dim_type c3,
10196 enum isl_dim_type c4, enum isl_dim_type c5)
10198 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10199 isl_basic_map *bmap;
10205 if (!dim || !eq || !ineq)
10208 if (eq->n_col != ineq->n_col)
10209 isl_die(dim->ctx, isl_error_invalid,
10210 "equalities and inequalities matrices should have "
10211 "same number of columns", goto error);
10213 total = 1 + isl_space_dim(dim, isl_dim_all);
10215 if (eq->n_col < total)
10216 isl_die(dim->ctx, isl_error_invalid,
10217 "number of columns too small", goto error);
10219 extra = eq->n_col - total;
10221 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10222 eq->n_row, ineq->n_row);
10225 for (i = 0; i < extra; ++i) {
10226 k = isl_basic_map_alloc_div(bmap);
10229 isl_int_set_si(bmap->div[k][0], 0);
10231 for (i = 0; i < eq->n_row; ++i) {
10232 l = isl_basic_map_alloc_equality(bmap);
10235 for (j = 0, pos = 0; j < 5; ++j) {
10236 int off = isl_basic_map_offset(bmap, c[j]);
10237 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10238 isl_int_set(bmap->eq[l][off + k],
10244 for (i = 0; i < ineq->n_row; ++i) {
10245 l = isl_basic_map_alloc_inequality(bmap);
10248 for (j = 0, pos = 0; j < 5; ++j) {
10249 int off = isl_basic_map_offset(bmap, c[j]);
10250 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10251 isl_int_set(bmap->ineq[l][off + k],
10252 ineq->row[i][pos]);
10258 isl_space_free(dim);
10260 isl_mat_free(ineq);
10264 isl_space_free(dim);
10266 isl_mat_free(ineq);
10270 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10271 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10272 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10274 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10275 c1, c2, c3, c4, isl_dim_in);
10278 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10279 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10280 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10282 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10283 c1, c2, c3, c4, isl_dim_in);
10286 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10287 __isl_take isl_space *dim,
10288 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10289 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10291 return (isl_basic_set*)
10292 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10293 c1, c2, c3, c4, isl_dim_in);
10296 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10301 return isl_space_can_zip(bmap->dim);
10304 int isl_map_can_zip(__isl_keep isl_map *map)
10309 return isl_space_can_zip(map->dim);
10312 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10313 * (A -> C) -> (B -> D).
10315 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10324 if (!isl_basic_map_can_zip(bmap))
10325 isl_die(bmap->ctx, isl_error_invalid,
10326 "basic map cannot be zipped", goto error);
10327 pos = isl_basic_map_offset(bmap, isl_dim_in) +
10328 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10329 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10330 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10331 bmap = isl_basic_map_cow(bmap);
10332 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10335 bmap->dim = isl_space_zip(bmap->dim);
10340 isl_basic_map_free(bmap);
10344 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10345 * (A -> C) -> (B -> D).
10347 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10354 if (!isl_map_can_zip(map))
10355 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10358 map = isl_map_cow(map);
10362 for (i = 0; i < map->n; ++i) {
10363 map->p[i] = isl_basic_map_zip(map->p[i]);
10368 map->dim = isl_space_zip(map->dim);
10378 /* Can we apply isl_basic_map_curry to "bmap"?
10379 * That is, does it have a nested relation in its domain?
10381 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10386 return isl_space_can_curry(bmap->dim);
10389 /* Can we apply isl_map_curry to "map"?
10390 * That is, does it have a nested relation in its domain?
10392 int isl_map_can_curry(__isl_keep isl_map *map)
10397 return isl_space_can_curry(map->dim);
10400 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10403 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10409 if (!isl_basic_map_can_curry(bmap))
10410 isl_die(bmap->ctx, isl_error_invalid,
10411 "basic map cannot be curried", goto error);
10412 bmap = isl_basic_map_cow(bmap);
10415 bmap->dim = isl_space_curry(bmap->dim);
10420 isl_basic_map_free(bmap);
10424 /* Given a map (A -> B) -> C, return the corresponding map
10427 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10434 if (!isl_map_can_curry(map))
10435 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10438 map = isl_map_cow(map);
10442 for (i = 0; i < map->n; ++i) {
10443 map->p[i] = isl_basic_map_curry(map->p[i]);
10448 map->dim = isl_space_curry(map->dim);
10458 /* Can we apply isl_basic_map_uncurry to "bmap"?
10459 * That is, does it have a nested relation in its domain?
10461 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
10466 return isl_space_can_uncurry(bmap->dim);
10469 /* Can we apply isl_map_uncurry to "map"?
10470 * That is, does it have a nested relation in its domain?
10472 int isl_map_can_uncurry(__isl_keep isl_map *map)
10477 return isl_space_can_uncurry(map->dim);
10480 /* Given a basic map A -> (B -> C), return the corresponding basic map
10483 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
10489 if (!isl_basic_map_can_uncurry(bmap))
10490 isl_die(bmap->ctx, isl_error_invalid,
10491 "basic map cannot be uncurried",
10492 return isl_basic_map_free(bmap));
10493 bmap = isl_basic_map_cow(bmap);
10496 bmap->dim = isl_space_uncurry(bmap->dim);
10498 return isl_basic_map_free(bmap);
10502 /* Given a map A -> (B -> C), return the corresponding map
10505 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
10512 if (!isl_map_can_uncurry(map))
10513 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
10514 return isl_map_free(map));
10516 map = isl_map_cow(map);
10520 for (i = 0; i < map->n; ++i) {
10521 map->p[i] = isl_basic_map_uncurry(map->p[i]);
10523 return isl_map_free(map);
10526 map->dim = isl_space_uncurry(map->dim);
10528 return isl_map_free(map);
10533 /* Construct a basic map mapping the domain of the affine expression
10534 * to a one-dimensional range prescribed by the affine expression.
10536 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
10540 isl_local_space *ls;
10541 isl_basic_map *bmap;
10546 ls = isl_aff_get_local_space(aff);
10547 bmap = isl_basic_map_from_local_space(ls);
10548 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
10549 k = isl_basic_map_alloc_equality(bmap);
10553 pos = isl_basic_map_offset(bmap, isl_dim_out);
10554 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
10555 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
10556 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
10557 aff->v->size - (pos + 1));
10560 bmap = isl_basic_map_finalize(bmap);
10564 isl_basic_map_free(bmap);
10568 /* Construct a map mapping the domain of the affine expression
10569 * to a one-dimensional range prescribed by the affine expression.
10571 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
10573 isl_basic_map *bmap;
10575 bmap = isl_basic_map_from_aff(aff);
10576 return isl_map_from_basic_map(bmap);
10579 /* Construct a basic map mapping the domain the multi-affine expression
10580 * to its range, with each dimension in the range equated to the
10581 * corresponding affine expression.
10583 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
10584 __isl_take isl_multi_aff *maff)
10588 isl_basic_map *bmap;
10593 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
10594 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
10595 "invalid space", return isl_multi_aff_free(maff));
10597 space = isl_space_domain(isl_multi_aff_get_space(maff));
10598 bmap = isl_basic_map_universe(isl_space_from_domain(space));
10600 for (i = 0; i < maff->n; ++i) {
10602 isl_basic_map *bmap_i;
10604 aff = isl_aff_copy(maff->p[i]);
10605 bmap_i = isl_basic_map_from_aff(aff);
10607 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10610 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
10612 isl_multi_aff_free(maff);
10616 /* Construct a map mapping the domain the multi-affine expression
10617 * to its range, with each dimension in the range equated to the
10618 * corresponding affine expression.
10620 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
10622 isl_basic_map *bmap;
10624 bmap = isl_basic_map_from_multi_aff(maff);
10625 return isl_map_from_basic_map(bmap);
10628 /* Construct a basic map mapping a domain in the given space to
10629 * to an n-dimensional range, with n the number of elements in the list,
10630 * where each coordinate in the range is prescribed by the
10631 * corresponding affine expression.
10632 * The domains of all affine expressions in the list are assumed to match
10635 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
10636 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
10640 isl_basic_map *bmap;
10645 dim = isl_space_from_domain(domain_dim);
10646 bmap = isl_basic_map_universe(dim);
10648 for (i = 0; i < list->n; ++i) {
10650 isl_basic_map *bmap_i;
10652 aff = isl_aff_copy(list->p[i]);
10653 bmap_i = isl_basic_map_from_aff(aff);
10655 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10658 isl_aff_list_free(list);
10662 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
10663 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10665 return isl_map_equate(set, type1, pos1, type2, pos2);
10668 /* Construct a basic map where the given dimensions are equal to each other.
10670 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
10671 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10673 isl_basic_map *bmap = NULL;
10679 if (pos1 >= isl_space_dim(space, type1))
10680 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10681 "index out of bounds", goto error);
10682 if (pos2 >= isl_space_dim(space, type2))
10683 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10684 "index out of bounds", goto error);
10686 if (type1 == type2 && pos1 == pos2)
10687 return isl_basic_map_universe(space);
10689 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
10690 i = isl_basic_map_alloc_equality(bmap);
10693 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10694 pos1 += isl_basic_map_offset(bmap, type1);
10695 pos2 += isl_basic_map_offset(bmap, type2);
10696 isl_int_set_si(bmap->eq[i][pos1], -1);
10697 isl_int_set_si(bmap->eq[i][pos2], 1);
10698 bmap = isl_basic_map_finalize(bmap);
10699 isl_space_free(space);
10702 isl_space_free(space);
10703 isl_basic_map_free(bmap);
10707 /* Add a constraint imposing that the given two dimensions are equal.
10709 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
10710 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10714 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
10716 bmap = isl_basic_map_intersect(bmap, eq);
10721 /* Add a constraint imposing that the given two dimensions are equal.
10723 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
10724 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10726 isl_basic_map *bmap;
10728 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
10730 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10735 /* Add a constraint imposing that the given two dimensions have opposite values.
10737 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
10738 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10740 isl_basic_map *bmap = NULL;
10746 if (pos1 >= isl_map_dim(map, type1))
10747 isl_die(map->ctx, isl_error_invalid,
10748 "index out of bounds", goto error);
10749 if (pos2 >= isl_map_dim(map, type2))
10750 isl_die(map->ctx, isl_error_invalid,
10751 "index out of bounds", goto error);
10753 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
10754 i = isl_basic_map_alloc_equality(bmap);
10757 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10758 pos1 += isl_basic_map_offset(bmap, type1);
10759 pos2 += isl_basic_map_offset(bmap, type2);
10760 isl_int_set_si(bmap->eq[i][pos1], 1);
10761 isl_int_set_si(bmap->eq[i][pos2], 1);
10762 bmap = isl_basic_map_finalize(bmap);
10764 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10768 isl_basic_map_free(bmap);
10773 /* Add a constraint imposing that the value of the first dimension is
10774 * greater than or equal to that of the second.
10776 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
10777 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10780 isl_local_space *ls;
10785 if (pos1 >= isl_basic_map_dim(bmap, type1))
10786 isl_die(bmap->ctx, isl_error_invalid,
10787 "index out of bounds", return isl_basic_map_free(bmap));
10788 if (pos2 >= isl_basic_map_dim(bmap, type2))
10789 isl_die(bmap->ctx, isl_error_invalid,
10790 "index out of bounds", return isl_basic_map_free(bmap));
10792 if (type1 == type2 && pos1 == pos2)
10795 ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
10796 c = isl_inequality_alloc(ls);
10797 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
10798 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
10799 bmap = isl_basic_map_add_constraint(bmap, c);
10804 /* Add a constraint imposing that the value of the first dimension is
10805 * greater than that of the second.
10807 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
10808 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10810 isl_basic_map *bmap = NULL;
10816 if (pos1 >= isl_map_dim(map, type1))
10817 isl_die(map->ctx, isl_error_invalid,
10818 "index out of bounds", goto error);
10819 if (pos2 >= isl_map_dim(map, type2))
10820 isl_die(map->ctx, isl_error_invalid,
10821 "index out of bounds", goto error);
10823 if (type1 == type2 && pos1 == pos2) {
10824 isl_space *space = isl_map_get_space(map);
10826 return isl_map_empty(space);
10829 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 0, 1);
10830 i = isl_basic_map_alloc_inequality(bmap);
10833 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
10834 pos1 += isl_basic_map_offset(bmap, type1);
10835 pos2 += isl_basic_map_offset(bmap, type2);
10836 isl_int_set_si(bmap->ineq[i][pos1], 1);
10837 isl_int_set_si(bmap->ineq[i][pos2], -1);
10838 isl_int_set_si(bmap->ineq[i][0], -1);
10839 bmap = isl_basic_map_finalize(bmap);
10841 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10845 isl_basic_map_free(bmap);
10850 /* Add a constraint imposing that the value of the first dimension is
10851 * smaller than that of the second.
10853 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
10854 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10856 return isl_map_order_gt(map, type2, pos2, type1, pos1);
10859 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
10863 isl_local_space *ls;
10868 if (!isl_basic_map_divs_known(bmap))
10869 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
10870 "some divs are unknown", return NULL);
10872 ls = isl_basic_map_get_local_space(bmap);
10873 div = isl_local_space_get_div(ls, pos);
10874 isl_local_space_free(ls);
10879 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
10882 return isl_basic_map_get_div(bset, pos);
10885 /* Plug in "subs" for dimension "type", "pos" of "bset".
10887 * Let i be the dimension to replace and let "subs" be of the form
10891 * Any integer division with a non-zero coefficient for i,
10893 * floor((a i + g)/m)
10897 * floor((a f + d g)/(m d))
10899 * Constraints of the form
10907 * We currently require that "subs" is an integral expression.
10908 * Handling rational expressions may require us to add stride constraints
10909 * as we do in isl_basic_set_preimage_multi_aff.
10911 __isl_give isl_basic_set *isl_basic_set_substitute(
10912 __isl_take isl_basic_set *bset,
10913 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10919 if (bset && isl_basic_set_plain_is_empty(bset))
10922 bset = isl_basic_set_cow(bset);
10923 if (!bset || !subs)
10926 ctx = isl_basic_set_get_ctx(bset);
10927 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
10928 isl_die(ctx, isl_error_invalid,
10929 "spaces don't match", goto error);
10930 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
10931 isl_die(ctx, isl_error_unsupported,
10932 "cannot handle divs yet", goto error);
10933 if (!isl_int_is_one(subs->v->el[0]))
10934 isl_die(ctx, isl_error_invalid,
10935 "can only substitute integer expressions", goto error);
10937 pos += isl_basic_set_offset(bset, type);
10941 for (i = 0; i < bset->n_eq; ++i) {
10942 if (isl_int_is_zero(bset->eq[i][pos]))
10944 isl_int_set(v, bset->eq[i][pos]);
10945 isl_int_set_si(bset->eq[i][pos], 0);
10946 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
10947 v, subs->v->el + 1, subs->v->size - 1);
10950 for (i = 0; i < bset->n_ineq; ++i) {
10951 if (isl_int_is_zero(bset->ineq[i][pos]))
10953 isl_int_set(v, bset->ineq[i][pos]);
10954 isl_int_set_si(bset->ineq[i][pos], 0);
10955 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
10956 v, subs->v->el + 1, subs->v->size - 1);
10959 for (i = 0; i < bset->n_div; ++i) {
10960 if (isl_int_is_zero(bset->div[i][1 + pos]))
10962 isl_int_set(v, bset->div[i][1 + pos]);
10963 isl_int_set_si(bset->div[i][1 + pos], 0);
10964 isl_seq_combine(bset->div[i] + 1,
10965 subs->v->el[0], bset->div[i] + 1,
10966 v, subs->v->el + 1, subs->v->size - 1);
10967 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
10972 bset = isl_basic_set_simplify(bset);
10973 return isl_basic_set_finalize(bset);
10975 isl_basic_set_free(bset);
10979 /* Plug in "subs" for dimension "type", "pos" of "set".
10981 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
10982 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10986 if (set && isl_set_plain_is_empty(set))
10989 set = isl_set_cow(set);
10993 for (i = set->n - 1; i >= 0; --i) {
10994 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
10995 if (remove_if_empty(set, i) < 0)
11005 /* Check if the range of "ma" is compatible with "space".
11006 * Return -1 if anything is wrong.
11008 static int check_space_compatible_range_multi_aff(
11009 __isl_keep isl_space *space, __isl_keep isl_multi_aff *ma)
11012 isl_space *ma_space;
11014 ma_space = isl_multi_aff_get_space(ma);
11015 m = isl_space_is_range_internal(space, ma_space);
11016 isl_space_free(ma_space);
11018 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11019 "spaces don't match", return -1);
11023 /* Check if the range of "ma" is compatible with "bset".
11024 * Return -1 if anything is wrong.
11026 static int check_basic_set_compatible_range_multi_aff(
11027 __isl_keep isl_basic_set *bset, __isl_keep isl_multi_aff *ma)
11029 return check_space_compatible_range_multi_aff(bset->dim, ma);
11032 /* Copy the divs from "ma" to "bset", adding zeros for the coefficients
11033 * of the other divs in "bset".
11035 static int set_ma_divs(__isl_keep isl_basic_set *bset,
11036 __isl_keep isl_multi_aff *ma, int n_div)
11039 isl_local_space *ls;
11044 ls = isl_aff_get_domain_local_space(ma->p[0]);
11048 for (i = 0; i < n_div; ++i) {
11049 isl_seq_cpy(bset->div[i], ls->div->row[i], ls->div->n_col);
11050 isl_seq_clr(bset->div[i] + ls->div->n_col, bset->n_div - n_div);
11051 if (isl_basic_set_add_div_constraints(bset, i) < 0)
11055 isl_local_space_free(ls);
11058 isl_local_space_free(ls);
11062 /* How many stride constraints does "ma" enforce?
11063 * That is, how many of the affine expressions have a denominator
11064 * different from one?
11066 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11071 for (i = 0; i < ma->n; ++i)
11072 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11078 /* For each affine expression in ma of the form
11080 * x_i = (f_i y + h_i)/m_i
11082 * with m_i different from one, add a constraint to "bset"
11085 * f_i y + h_i = m_i alpha_i
11087 * with alpha_i an additional existentially quantified variable.
11089 static __isl_give isl_basic_set *add_ma_strides(
11090 __isl_take isl_basic_set *bset, __isl_keep isl_multi_aff *ma)
11096 total = isl_basic_set_total_dim(bset);
11097 for (i = 0; i < ma->n; ++i) {
11100 if (isl_int_is_one(ma->p[i]->v->el[0]))
11102 div = isl_basic_set_alloc_div(bset);
11103 k = isl_basic_set_alloc_equality(bset);
11104 if (div < 0 || k < 0)
11106 isl_int_set_si(bset->div[div][0], 0);
11107 len = ma->p[i]->v->size;
11108 isl_seq_cpy(bset->eq[k], ma->p[i]->v->el + 1, len - 1);
11109 isl_seq_clr(bset->eq[k] + len - 1, 1 + total - (len - 1));
11110 isl_int_neg(bset->eq[k][1 + total], ma->p[i]->v->el[0]);
11116 isl_basic_set_free(bset);
11120 /* Compute the preimage of "bset" under the function represented by "ma".
11121 * In other words, plug in "ma" in "bset". The result is a basic set
11122 * that lives in the domain space of "ma".
11124 * If bset is represented by
11126 * A(p) + B x + C(divs) >= 0
11128 * and ma is represented by
11130 * x = D(p) + F(y) + G(divs')
11132 * then the result is
11134 * A(p) + B D(p) + B F(y) + B G(divs') + C(divs) >= 0
11136 * The divs in the input set are similarly adjusted.
11139 * floor((a_i(p) + b_i x + c_i(divs))/n_i)
11143 * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
11145 * If bset is not a rational set and if F(y) involves any denominators
11147 * x_i = (f_i y + h_i)/m_i
11149 * the additional constraints are added to ensure that we only
11150 * map back integer points. That is we enforce
11152 * f_i y + h_i = m_i alpha_i
11154 * with alpha_i an additional existentially quantified variable.
11156 * We first copy over the divs from "ma".
11157 * Then we add the modified constraints and divs from "bset".
11158 * Finally, we add the stride constraints, if needed.
11160 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11161 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11165 isl_basic_set *res = NULL;
11166 int n_div_bset, n_div_ma;
11167 isl_int f, c1, c2, g;
11168 int rational, strides;
11175 ma = isl_multi_aff_align_divs(ma);
11178 if (check_basic_set_compatible_range_multi_aff(bset, ma) < 0)
11181 n_div_bset = isl_basic_set_dim(bset, isl_dim_div);
11182 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11184 space = isl_space_domain(isl_multi_aff_get_space(ma));
11185 rational = isl_basic_set_is_rational(bset);
11186 strides = rational ? 0 : multi_aff_strides(ma);
11187 res = isl_basic_set_alloc_space(space, n_div_ma + n_div_bset + strides,
11188 bset->n_eq + strides, bset->n_ineq + 2 * n_div_ma);
11190 res = isl_basic_set_set_rational(res);
11192 for (i = 0; i < n_div_ma + n_div_bset; ++i)
11193 if (isl_basic_set_alloc_div(res) < 0)
11196 if (set_ma_divs(res, ma, n_div_ma) < 0)
11199 for (i = 0; i < bset->n_eq; ++i) {
11200 k = isl_basic_set_alloc_equality(res);
11203 isl_seq_preimage(res->eq[k], bset->eq[i], ma, n_div_ma,
11204 n_div_bset, f, c1, c2, g, 0);
11207 for (i = 0; i < bset->n_ineq; ++i) {
11208 k = isl_basic_set_alloc_inequality(res);
11211 isl_seq_preimage(res->ineq[k], bset->ineq[i], ma, n_div_ma,
11212 n_div_bset, f, c1, c2, g, 0);
11215 for (i = 0; i < bset->n_div; ++i) {
11216 if (isl_int_is_zero(bset->div[i][0])) {
11217 isl_int_set_si(res->div[n_div_ma + i][0], 0);
11220 isl_seq_preimage(res->div[n_div_ma + i], bset->div[i],
11221 ma, n_div_ma, n_div_bset, f, c1, c2, g, 1);
11225 res = add_ma_strides(res, ma);
11231 isl_basic_set_free(bset);
11232 isl_multi_aff_free(ma);
11233 res = isl_basic_set_simplify(res);
11234 return isl_basic_set_finalize(res);
11240 isl_basic_set_free(bset);
11241 isl_multi_aff_free(ma);
11242 isl_basic_set_free(res);
11246 /* Check if the range of "ma" is compatible with "set".
11247 * Return -1 if anything is wrong.
11249 static int check_set_compatible_range_multi_aff(
11250 __isl_keep isl_set *set, __isl_keep isl_multi_aff *ma)
11252 return check_space_compatible_range_multi_aff(set->dim, ma);
11255 /* Compute the preimage of "set" under the function represented by "ma".
11256 * In other words, plug in "ma" in "set. The result is a set
11257 * that lives in the domain space of "ma".
11259 static __isl_give isl_set *set_preimage_multi_aff(__isl_take isl_set *set,
11260 __isl_take isl_multi_aff *ma)
11264 set = isl_set_cow(set);
11265 ma = isl_multi_aff_align_divs(ma);
11268 if (check_set_compatible_range_multi_aff(set, ma) < 0)
11271 for (i = 0; i < set->n; ++i) {
11272 set->p[i] = isl_basic_set_preimage_multi_aff(set->p[i],
11273 isl_multi_aff_copy(ma));
11278 isl_space_free(set->dim);
11279 set->dim = isl_multi_aff_get_domain_space(ma);
11283 isl_multi_aff_free(ma);
11285 ISL_F_CLR(set, ISL_MAP_DISJOINT);
11286 ISL_F_CLR(set, ISL_SET_NORMALIZED);
11289 isl_multi_aff_free(ma);
11294 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
11295 __isl_take isl_multi_aff *ma)
11300 if (isl_space_match(set->dim, isl_dim_param, ma->space, isl_dim_param))
11301 return set_preimage_multi_aff(set, ma);
11303 if (!isl_space_has_named_params(set->dim) ||
11304 !isl_space_has_named_params(ma->space))
11305 isl_die(set->ctx, isl_error_invalid,
11306 "unaligned unnamed parameters", goto error);
11307 set = isl_set_align_params(set, isl_multi_aff_get_space(ma));
11308 ma = isl_multi_aff_align_params(ma, isl_set_get_space(set));
11310 return set_preimage_multi_aff(set, ma);
11312 isl_multi_aff_free(ma);
11313 return isl_set_free(set);
11316 /* Compute the preimage of "set" under the function represented by "pma".
11317 * In other words, plug in "pma" in "set. The result is a set
11318 * that lives in the domain space of "pma".
11320 static __isl_give isl_set *set_preimage_pw_multi_aff(__isl_take isl_set *set,
11321 __isl_take isl_pw_multi_aff *pma)
11330 isl_pw_multi_aff_free(pma);
11331 res = isl_set_empty(isl_set_get_space(set));
11336 res = isl_set_preimage_multi_aff(isl_set_copy(set),
11337 isl_multi_aff_copy(pma->p[0].maff));
11338 res = isl_set_intersect(res, isl_set_copy(pma->p[0].set));
11340 for (i = 1; i < pma->n; ++i) {
11343 res_i = isl_set_preimage_multi_aff(isl_set_copy(set),
11344 isl_multi_aff_copy(pma->p[i].maff));
11345 res_i = isl_set_intersect(res_i, isl_set_copy(pma->p[i].set));
11346 res = isl_set_union(res, res_i);
11349 isl_pw_multi_aff_free(pma);
11353 isl_pw_multi_aff_free(pma);
11358 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
11359 __isl_take isl_pw_multi_aff *pma)
11364 if (isl_space_match(set->dim, isl_dim_param, pma->dim, isl_dim_param))
11365 return set_preimage_pw_multi_aff(set, pma);
11367 if (!isl_space_has_named_params(set->dim) ||
11368 !isl_space_has_named_params(pma->dim))
11369 isl_die(set->ctx, isl_error_invalid,
11370 "unaligned unnamed parameters", goto error);
11371 set = isl_set_align_params(set, isl_pw_multi_aff_get_space(pma));
11372 pma = isl_pw_multi_aff_align_params(pma, isl_set_get_space(set));
11374 return set_preimage_pw_multi_aff(set, pma);
11376 isl_pw_multi_aff_free(pma);
11377 return isl_set_free(set);