2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
17 #include "isl_space_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
33 #include <isl_options_private.h>
35 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
38 case isl_dim_param: return dim->nparam;
39 case isl_dim_in: return dim->n_in;
40 case isl_dim_out: return dim->n_out;
41 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
46 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
49 case isl_dim_param: return 1;
50 case isl_dim_in: return 1 + dim->nparam;
51 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
56 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
57 enum isl_dim_type type)
62 case isl_dim_cst: return 1;
65 case isl_dim_out: return isl_space_dim(bmap->dim, type);
66 case isl_dim_div: return bmap->n_div;
67 case isl_dim_all: return isl_basic_map_total_dim(bmap);
72 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
74 return map ? n(map->dim, type) : 0;
77 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
79 return set ? n(set->dim, type) : 0;
82 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
83 enum isl_dim_type type)
85 isl_space *dim = bmap->dim;
87 case isl_dim_cst: return 0;
88 case isl_dim_param: return 1;
89 case isl_dim_in: return 1 + dim->nparam;
90 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
91 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
96 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
97 enum isl_dim_type type)
99 return isl_basic_map_offset(bset, type);
102 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
104 return pos(map->dim, type);
107 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
108 enum isl_dim_type type)
110 return isl_basic_map_dim(bset, type);
113 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
115 return isl_basic_set_dim(bset, isl_dim_set);
118 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
120 return isl_basic_set_dim(bset, isl_dim_param);
123 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
125 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
128 unsigned isl_set_n_dim(__isl_keep isl_set *set)
130 return isl_set_dim(set, isl_dim_set);
133 unsigned isl_set_n_param(__isl_keep isl_set *set)
135 return isl_set_dim(set, isl_dim_param);
138 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
140 return bmap ? bmap->dim->n_in : 0;
143 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
145 return bmap ? bmap->dim->n_out : 0;
148 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
150 return bmap ? bmap->dim->nparam : 0;
153 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
155 return bmap ? bmap->n_div : 0;
158 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
160 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
163 unsigned isl_map_n_in(const struct isl_map *map)
165 return map ? map->dim->n_in : 0;
168 unsigned isl_map_n_out(const struct isl_map *map)
170 return map ? map->dim->n_out : 0;
173 unsigned isl_map_n_param(const struct isl_map *map)
175 return map ? map->dim->nparam : 0;
178 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
183 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
186 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
189 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
190 struct isl_basic_set *bset)
195 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
198 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
201 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
206 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
209 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
212 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
213 struct isl_basic_set *bset)
218 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
221 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
224 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
226 return bmap ? bmap->ctx : NULL;
229 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
231 return bset ? bset->ctx : NULL;
234 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
236 return map ? map->ctx : NULL;
239 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
241 return set ? set->ctx : NULL;
244 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
248 return isl_space_copy(bmap->dim);
251 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
255 return isl_space_copy(bset->dim);
258 __isl_give isl_local_space *isl_basic_map_get_local_space(
259 __isl_keep isl_basic_map *bmap)
268 total = isl_basic_map_total_dim(bmap);
269 ls = isl_local_space_alloc(isl_space_copy(bmap->dim), bmap->n_div);
273 for (i = 0; i < bmap->n_div; ++i)
274 isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total);
279 __isl_give isl_local_space *isl_basic_set_get_local_space(
280 __isl_keep isl_basic_set *bset)
282 return isl_basic_map_get_local_space(bset);
285 __isl_give isl_basic_map *isl_basic_map_from_local_space(
286 __isl_take isl_local_space *ls)
295 n_div = isl_local_space_dim(ls, isl_dim_div);
296 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
297 n_div, 0, 2 * n_div);
299 for (i = 0; i < n_div; ++i)
300 if (isl_basic_map_alloc_div(bmap) < 0)
303 for (i = 0; i < n_div; ++i) {
304 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
305 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
309 isl_local_space_free(ls);
312 isl_local_space_free(ls);
313 isl_basic_map_free(bmap);
317 __isl_give isl_basic_set *isl_basic_set_from_local_space(
318 __isl_take isl_local_space *ls)
320 return isl_basic_map_from_local_space(ls);
323 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
327 return isl_space_copy(map->dim);
330 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
334 return isl_space_copy(set->dim);
337 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
338 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
340 bmap = isl_basic_map_cow(bmap);
343 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
346 bmap = isl_basic_map_finalize(bmap);
349 isl_basic_map_free(bmap);
353 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
354 __isl_take isl_basic_set *bset, const char *s)
356 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
359 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
360 enum isl_dim_type type)
362 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
365 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
366 enum isl_dim_type type, const char *s)
370 map = isl_map_cow(map);
374 map->dim = isl_space_set_tuple_name(map->dim, type, s);
378 for (i = 0; i < map->n; ++i) {
379 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
390 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
391 enum isl_dim_type type)
393 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
396 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
399 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
402 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
403 enum isl_dim_type type, __isl_take isl_id *id)
405 map = isl_map_cow(map);
407 return isl_id_free(id);
409 map->dim = isl_space_set_tuple_id(map->dim, type, id);
411 return isl_map_reset_space(map, isl_space_copy(map->dim));
414 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
415 __isl_take isl_id *id)
417 return isl_map_set_tuple_id(set, isl_dim_set, id);
420 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
421 enum isl_dim_type type)
423 map = isl_map_cow(map);
427 map->dim = isl_space_reset_tuple_id(map->dim, type);
429 return isl_map_reset_space(map, isl_space_copy(map->dim));
432 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
434 return isl_map_reset_tuple_id(set, isl_dim_set);
437 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
439 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
442 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
443 enum isl_dim_type type)
445 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
448 int isl_set_has_tuple_id(__isl_keep isl_set *set)
450 return isl_map_has_tuple_id(set, isl_dim_set);
453 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
455 return isl_map_get_tuple_id(set, isl_dim_set);
458 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
460 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
463 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
465 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
468 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
469 enum isl_dim_type type, unsigned pos)
471 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
474 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
475 enum isl_dim_type type, unsigned pos)
477 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
480 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
481 enum isl_dim_type type, unsigned pos)
483 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
486 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
487 enum isl_dim_type type, unsigned pos)
489 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
492 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
493 __isl_take isl_basic_map *bmap,
494 enum isl_dim_type type, unsigned pos, const char *s)
496 bmap = isl_basic_map_cow(bmap);
499 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
504 isl_basic_map_free(bmap);
508 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
509 enum isl_dim_type type, unsigned pos, const char *s)
513 map = isl_map_cow(map);
517 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
521 for (i = 0; i < map->n; ++i) {
522 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
533 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
534 __isl_take isl_basic_set *bset,
535 enum isl_dim_type type, unsigned pos, const char *s)
537 return (isl_basic_set *)isl_basic_map_set_dim_name(
538 (isl_basic_map *)bset, type, pos, s);
541 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
542 enum isl_dim_type type, unsigned pos, const char *s)
544 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
547 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
548 enum isl_dim_type type, unsigned pos)
550 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
553 int isl_map_has_dim_id(__isl_keep isl_map *map,
554 enum isl_dim_type type, unsigned pos)
556 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
559 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
560 enum isl_dim_type type, unsigned pos)
562 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
565 int isl_set_has_dim_id(__isl_keep isl_set *set,
566 enum isl_dim_type type, unsigned pos)
568 return isl_map_has_dim_id(set, type, pos);
571 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
572 enum isl_dim_type type, unsigned pos)
574 return isl_map_get_dim_id(set, type, pos);
577 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
578 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
580 map = isl_map_cow(map);
582 return isl_id_free(id);
584 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
586 return isl_map_reset_space(map, isl_space_copy(map->dim));
589 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
590 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
592 return isl_map_set_dim_id(set, type, pos, id);
595 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
596 __isl_keep isl_id *id)
600 return isl_space_find_dim_by_id(map->dim, type, id);
603 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
604 __isl_keep isl_id *id)
606 return isl_map_find_dim_by_id(set, type, id);
609 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
614 return isl_space_find_dim_by_name(map->dim, type, name);
617 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
621 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
624 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
626 return isl_basic_map_is_rational(bset);
629 /* Is this basic set a parameter domain?
631 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
635 return isl_space_is_params(bset->dim);
638 /* Is this set a parameter domain?
640 int isl_set_is_params(__isl_keep isl_set *set)
644 return isl_space_is_params(set->dim);
647 /* Is this map actually a parameter domain?
648 * Users should never call this function. Outside of isl,
649 * a map can never be a parameter domain.
651 int isl_map_is_params(__isl_keep isl_map *map)
655 return isl_space_is_params(map->dim);
658 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
659 struct isl_basic_map *bmap, unsigned extra,
660 unsigned n_eq, unsigned n_ineq)
663 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
668 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
669 if (isl_blk_is_error(bmap->block))
672 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
677 bmap->block2 = isl_blk_empty();
680 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
681 if (isl_blk_is_error(bmap->block2))
684 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
689 for (i = 0; i < n_ineq + n_eq; ++i)
690 bmap->ineq[i] = bmap->block.data + i * row_size;
692 for (i = 0; i < extra; ++i)
693 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
697 bmap->c_size = n_eq + n_ineq;
698 bmap->eq = bmap->ineq + n_ineq;
707 isl_basic_map_free(bmap);
711 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
712 unsigned nparam, unsigned dim, unsigned extra,
713 unsigned n_eq, unsigned n_ineq)
715 struct isl_basic_map *bmap;
718 space = isl_space_set_alloc(ctx, nparam, dim);
722 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
723 return (struct isl_basic_set *)bmap;
726 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
727 unsigned extra, unsigned n_eq, unsigned n_ineq)
729 struct isl_basic_map *bmap;
732 isl_assert(dim->ctx, dim->n_in == 0, goto error);
733 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
734 return (struct isl_basic_set *)bmap;
740 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
741 unsigned extra, unsigned n_eq, unsigned n_ineq)
743 struct isl_basic_map *bmap;
747 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
752 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
758 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
759 unsigned nparam, unsigned in, unsigned out, unsigned extra,
760 unsigned n_eq, unsigned n_ineq)
762 struct isl_basic_map *bmap;
765 dim = isl_space_alloc(ctx, nparam, in, out);
769 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
773 static void dup_constraints(
774 struct isl_basic_map *dst, struct isl_basic_map *src)
777 unsigned total = isl_basic_map_total_dim(src);
779 for (i = 0; i < src->n_eq; ++i) {
780 int j = isl_basic_map_alloc_equality(dst);
781 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
784 for (i = 0; i < src->n_ineq; ++i) {
785 int j = isl_basic_map_alloc_inequality(dst);
786 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
789 for (i = 0; i < src->n_div; ++i) {
790 int j = isl_basic_map_alloc_div(dst);
791 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
793 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
796 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
798 struct isl_basic_map *dup;
802 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
803 bmap->n_div, bmap->n_eq, bmap->n_ineq);
806 dup_constraints(dup, bmap);
807 dup->flags = bmap->flags;
808 dup->sample = isl_vec_copy(bmap->sample);
812 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
814 struct isl_basic_map *dup;
816 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
817 return (struct isl_basic_set *)dup;
820 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
825 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
829 return isl_basic_set_dup(bset);
832 struct isl_set *isl_set_copy(struct isl_set *set)
841 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
846 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
850 bmap = isl_basic_map_dup(bmap);
852 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
856 struct isl_map *isl_map_copy(struct isl_map *map)
865 void isl_basic_map_free(struct isl_basic_map *bmap)
873 isl_ctx_deref(bmap->ctx);
875 isl_blk_free(bmap->ctx, bmap->block2);
877 isl_blk_free(bmap->ctx, bmap->block);
878 isl_vec_free(bmap->sample);
879 isl_space_free(bmap->dim);
883 void isl_basic_set_free(struct isl_basic_set *bset)
885 isl_basic_map_free((struct isl_basic_map *)bset);
888 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
890 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
893 __isl_give isl_map *isl_map_align_params_map_map_and(
894 __isl_take isl_map *map1, __isl_take isl_map *map2,
895 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
896 __isl_take isl_map *map2))
900 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
901 return fn(map1, map2);
902 if (!isl_space_has_named_params(map1->dim) ||
903 !isl_space_has_named_params(map2->dim))
904 isl_die(map1->ctx, isl_error_invalid,
905 "unaligned unnamed parameters", goto error);
906 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
907 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
908 return fn(map1, map2);
915 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
916 __isl_keep isl_map *map2,
917 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
923 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
924 return fn(map1, map2);
925 if (!isl_space_has_named_params(map1->dim) ||
926 !isl_space_has_named_params(map2->dim))
927 isl_die(map1->ctx, isl_error_invalid,
928 "unaligned unnamed parameters", return -1);
929 map1 = isl_map_copy(map1);
930 map2 = isl_map_copy(map2);
931 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
932 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
939 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
945 isl_assert(ctx, room_for_con(bmap, 1), return -1);
946 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
948 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
949 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
950 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
951 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
952 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
953 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
955 int j = isl_basic_map_alloc_inequality(bmap);
959 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
960 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
967 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
968 bmap->extra - bmap->n_div);
972 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
974 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
977 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
981 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
986 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
988 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
991 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
996 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
998 if (pos != bmap->n_eq - 1) {
1000 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1001 bmap->eq[bmap->n_eq - 1] = t;
1007 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1009 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1012 void isl_basic_map_inequality_to_equality(
1013 struct isl_basic_map *bmap, unsigned pos)
1017 t = bmap->ineq[pos];
1018 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1019 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1024 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1025 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1026 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1027 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1030 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1032 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1035 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1037 struct isl_ctx *ctx;
1041 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1042 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1043 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1044 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1045 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1046 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1047 1 + isl_basic_map_total_dim(bmap),
1048 bmap->extra - bmap->n_div);
1049 return bmap->n_ineq++;
1052 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1054 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1057 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1061 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1066 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1068 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1071 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1076 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1078 if (pos != bmap->n_ineq - 1) {
1079 t = bmap->ineq[pos];
1080 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1081 bmap->ineq[bmap->n_ineq - 1] = t;
1082 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1088 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1090 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1093 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1098 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1101 k = isl_basic_map_alloc_equality(bmap);
1104 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1107 isl_basic_map_free(bmap);
1111 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1114 return (isl_basic_set *)
1115 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1118 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1123 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1126 k = isl_basic_map_alloc_inequality(bmap);
1129 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1132 isl_basic_map_free(bmap);
1136 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1139 return (isl_basic_set *)
1140 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1143 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1147 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1148 isl_seq_clr(bmap->div[bmap->n_div] +
1149 1 + 1 + isl_basic_map_total_dim(bmap),
1150 bmap->extra - bmap->n_div);
1151 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1152 return bmap->n_div++;
1155 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1157 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1160 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1164 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1169 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1171 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1174 /* Copy constraint from src to dst, putting the vars of src at offset
1175 * dim_off in dst and the divs of src at offset div_off in dst.
1176 * If both sets are actually map, then dim_off applies to the input
1179 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1180 struct isl_basic_map *src_map, isl_int *src,
1181 unsigned in_off, unsigned out_off, unsigned div_off)
1183 unsigned src_nparam = isl_basic_map_n_param(src_map);
1184 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1185 unsigned src_in = isl_basic_map_n_in(src_map);
1186 unsigned dst_in = isl_basic_map_n_in(dst_map);
1187 unsigned src_out = isl_basic_map_n_out(src_map);
1188 unsigned dst_out = isl_basic_map_n_out(dst_map);
1189 isl_int_set(dst[0], src[0]);
1190 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1191 if (dst_nparam > src_nparam)
1192 isl_seq_clr(dst+1+src_nparam,
1193 dst_nparam - src_nparam);
1194 isl_seq_clr(dst+1+dst_nparam, in_off);
1195 isl_seq_cpy(dst+1+dst_nparam+in_off,
1197 isl_min(dst_in-in_off, src_in));
1198 if (dst_in-in_off > src_in)
1199 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1200 dst_in - in_off - src_in);
1201 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1202 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1203 src+1+src_nparam+src_in,
1204 isl_min(dst_out-out_off, src_out));
1205 if (dst_out-out_off > src_out)
1206 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1207 dst_out - out_off - src_out);
1208 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1209 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1210 src+1+src_nparam+src_in+src_out,
1211 isl_min(dst_map->extra-div_off, src_map->n_div));
1212 if (dst_map->n_div-div_off > src_map->n_div)
1213 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1214 div_off+src_map->n_div,
1215 dst_map->n_div - div_off - src_map->n_div);
1218 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1219 struct isl_basic_map *src_map, isl_int *src,
1220 unsigned in_off, unsigned out_off, unsigned div_off)
1222 isl_int_set(dst[0], src[0]);
1223 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1226 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1227 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1232 if (!bmap1 || !bmap2)
1235 div_off = bmap1->n_div;
1237 for (i = 0; i < bmap2->n_eq; ++i) {
1238 int i1 = isl_basic_map_alloc_equality(bmap1);
1241 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1242 i_pos, o_pos, div_off);
1245 for (i = 0; i < bmap2->n_ineq; ++i) {
1246 int i1 = isl_basic_map_alloc_inequality(bmap1);
1249 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1250 i_pos, o_pos, div_off);
1253 for (i = 0; i < bmap2->n_div; ++i) {
1254 int i1 = isl_basic_map_alloc_div(bmap1);
1257 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1258 i_pos, o_pos, div_off);
1261 isl_basic_map_free(bmap2);
1266 isl_basic_map_free(bmap1);
1267 isl_basic_map_free(bmap2);
1271 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1272 struct isl_basic_set *bset2, unsigned pos)
1274 return (struct isl_basic_set *)
1275 add_constraints((struct isl_basic_map *)bset1,
1276 (struct isl_basic_map *)bset2, 0, pos);
1279 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1280 __isl_take isl_space *dim, unsigned extra,
1281 unsigned n_eq, unsigned n_ineq)
1283 struct isl_basic_map *ext;
1293 dims_ok = isl_space_is_equal(base->dim, dim) &&
1294 base->extra >= base->n_div + extra;
1296 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1297 room_for_ineq(base, n_ineq)) {
1298 isl_space_free(dim);
1302 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1303 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1304 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1305 extra += base->extra;
1307 n_ineq += base->n_ineq;
1309 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1315 ext->sample = isl_vec_copy(base->sample);
1316 flags = base->flags;
1317 ext = add_constraints(ext, base, 0, 0);
1320 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1326 isl_space_free(dim);
1327 isl_basic_map_free(base);
1331 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1332 __isl_take isl_space *dim, unsigned extra,
1333 unsigned n_eq, unsigned n_ineq)
1335 return (struct isl_basic_set *)
1336 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1337 extra, n_eq, n_ineq);
1340 struct isl_basic_map *isl_basic_map_extend_constraints(
1341 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1345 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1349 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1350 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1351 unsigned n_eq, unsigned n_ineq)
1353 struct isl_basic_map *bmap;
1358 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1362 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1365 isl_basic_map_free(base);
1369 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1370 unsigned nparam, unsigned dim, unsigned extra,
1371 unsigned n_eq, unsigned n_ineq)
1373 return (struct isl_basic_set *)
1374 isl_basic_map_extend((struct isl_basic_map *)base,
1375 nparam, 0, dim, extra, n_eq, n_ineq);
1378 struct isl_basic_set *isl_basic_set_extend_constraints(
1379 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1381 return (struct isl_basic_set *)
1382 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1386 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1388 return (struct isl_basic_set *)
1389 isl_basic_map_cow((struct isl_basic_map *)bset);
1392 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1397 if (bmap->ref > 1) {
1399 bmap = isl_basic_map_dup(bmap);
1402 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1406 struct isl_set *isl_set_cow(struct isl_set *set)
1414 return isl_set_dup(set);
1417 struct isl_map *isl_map_cow(struct isl_map *map)
1425 return isl_map_dup(map);
1428 static void swap_vars(struct isl_blk blk, isl_int *a,
1429 unsigned a_len, unsigned b_len)
1431 isl_seq_cpy(blk.data, a+a_len, b_len);
1432 isl_seq_cpy(blk.data+b_len, a, a_len);
1433 isl_seq_cpy(a, blk.data, b_len+a_len);
1436 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1437 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1445 isl_assert(bmap->ctx,
1446 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1448 if (n1 == 0 || n2 == 0)
1451 bmap = isl_basic_map_cow(bmap);
1455 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1456 if (isl_blk_is_error(blk))
1459 for (i = 0; i < bmap->n_eq; ++i)
1461 bmap->eq[i] + pos, n1, n2);
1463 for (i = 0; i < bmap->n_ineq; ++i)
1465 bmap->ineq[i] + pos, n1, n2);
1467 for (i = 0; i < bmap->n_div; ++i)
1469 bmap->div[i]+1 + pos, n1, n2);
1471 isl_blk_free(bmap->ctx, blk);
1473 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1474 bmap = isl_basic_map_gauss(bmap, NULL);
1475 return isl_basic_map_finalize(bmap);
1477 isl_basic_map_free(bmap);
1481 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1482 __isl_take isl_basic_set *bset, unsigned n)
1487 nparam = isl_basic_set_n_param(bset);
1488 dim = isl_basic_set_n_dim(bset);
1489 isl_assert(bset->ctx, n <= dim, goto error);
1491 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1493 isl_basic_set_free(bset);
1497 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1503 total = isl_basic_map_total_dim(bmap);
1504 isl_basic_map_free_div(bmap, bmap->n_div);
1505 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1507 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1509 i = isl_basic_map_alloc_equality(bmap);
1513 isl_int_set_si(bmap->eq[i][0], 1);
1514 isl_seq_clr(bmap->eq[i]+1, total);
1515 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1516 isl_vec_free(bmap->sample);
1517 bmap->sample = NULL;
1518 return isl_basic_map_finalize(bmap);
1520 isl_basic_map_free(bmap);
1524 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1526 return (struct isl_basic_set *)
1527 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1530 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1533 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1534 isl_int *t = bmap->div[a];
1535 bmap->div[a] = bmap->div[b];
1538 for (i = 0; i < bmap->n_eq; ++i)
1539 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1541 for (i = 0; i < bmap->n_ineq; ++i)
1542 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1544 for (i = 0; i < bmap->n_div; ++i)
1545 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1546 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1549 /* Eliminate the specified n dimensions starting at first from the
1550 * constraints using Fourier-Motzkin. The dimensions themselves
1553 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1554 enum isl_dim_type type, unsigned first, unsigned n)
1563 map = isl_map_cow(map);
1567 for (i = 0; i < map->n; ++i) {
1568 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1578 /* Eliminate the specified n dimensions starting at first from the
1579 * constraints using Fourier-Motzkin. The dimensions themselves
1582 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1583 enum isl_dim_type type, unsigned first, unsigned n)
1585 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1588 /* Eliminate the specified n dimensions starting at first from the
1589 * constraints using Fourier-Motzkin. The dimensions themselves
1592 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1593 unsigned first, unsigned n)
1595 return isl_set_eliminate(set, isl_dim_set, first, n);
1598 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1599 __isl_take isl_basic_map *bmap)
1603 bmap = isl_basic_map_eliminate_vars(bmap,
1604 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1608 return isl_basic_map_finalize(bmap);
1611 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1612 __isl_take isl_basic_set *bset)
1614 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1615 (struct isl_basic_map *)bset);
1618 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1627 map = isl_map_cow(map);
1631 for (i = 0; i < map->n; ++i) {
1632 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1642 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1644 return isl_map_remove_divs(set);
1647 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1648 enum isl_dim_type type, unsigned first, unsigned n)
1652 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1654 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1656 bmap = isl_basic_map_eliminate_vars(bmap,
1657 isl_basic_map_offset(bmap, type) - 1 + first, n);
1660 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1662 bmap = isl_basic_map_drop(bmap, type, first, n);
1665 isl_basic_map_free(bmap);
1669 /* Return true if the definition of the given div (recursively) involves
1670 * any of the given variables.
1672 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1673 unsigned first, unsigned n)
1676 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1678 if (isl_int_is_zero(bmap->div[div][0]))
1680 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1683 for (i = bmap->n_div - 1; i >= 0; --i) {
1684 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1686 if (div_involves_vars(bmap, i, first, n))
1693 /* Remove all divs (recursively) involving any of the given dimensions
1694 * in their definitions.
1696 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1697 __isl_take isl_basic_map *bmap,
1698 enum isl_dim_type type, unsigned first, unsigned n)
1704 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1706 first += isl_basic_map_offset(bmap, type);
1708 for (i = bmap->n_div - 1; i >= 0; --i) {
1709 if (!div_involves_vars(bmap, i, first, n))
1711 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1719 isl_basic_map_free(bmap);
1723 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1724 enum isl_dim_type type, unsigned first, unsigned n)
1733 map = isl_map_cow(map);
1737 for (i = 0; i < map->n; ++i) {
1738 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1749 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1750 enum isl_dim_type type, unsigned first, unsigned n)
1752 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1756 /* Does the desciption of "bmap" depend on the specified dimensions?
1757 * We also check whether the dimensions appear in any of the div definitions.
1758 * In principle there is no need for this check. If the dimensions appear
1759 * in a div definition, they also appear in the defining constraints of that
1762 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1763 enum isl_dim_type type, unsigned first, unsigned n)
1770 if (first + n > isl_basic_map_dim(bmap, type))
1771 isl_die(bmap->ctx, isl_error_invalid,
1772 "index out of bounds", return -1);
1774 first += isl_basic_map_offset(bmap, type);
1775 for (i = 0; i < bmap->n_eq; ++i)
1776 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1778 for (i = 0; i < bmap->n_ineq; ++i)
1779 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1781 for (i = 0; i < bmap->n_div; ++i) {
1782 if (isl_int_is_zero(bmap->div[i][0]))
1784 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
1791 int isl_map_involves_dims(__isl_keep isl_map *map,
1792 enum isl_dim_type type, unsigned first, unsigned n)
1799 if (first + n > isl_map_dim(map, type))
1800 isl_die(map->ctx, isl_error_invalid,
1801 "index out of bounds", return -1);
1803 for (i = 0; i < map->n; ++i) {
1804 int involves = isl_basic_map_involves_dims(map->p[i],
1806 if (involves < 0 || involves)
1813 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1814 enum isl_dim_type type, unsigned first, unsigned n)
1816 return isl_basic_map_involves_dims(bset, type, first, n);
1819 int isl_set_involves_dims(__isl_keep isl_set *set,
1820 enum isl_dim_type type, unsigned first, unsigned n)
1822 return isl_map_involves_dims(set, type, first, n);
1825 /* Return true if the definition of the given div is unknown or depends
1828 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1831 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1833 if (isl_int_is_zero(bmap->div[div][0]))
1836 for (i = bmap->n_div - 1; i >= 0; --i) {
1837 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1839 if (div_is_unknown(bmap, i))
1846 /* Remove all divs that are unknown or defined in terms of unknown divs.
1848 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1849 __isl_take isl_basic_map *bmap)
1856 for (i = bmap->n_div - 1; i >= 0; --i) {
1857 if (!div_is_unknown(bmap, i))
1859 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1865 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1874 map = isl_map_cow(map);
1878 for (i = 0; i < map->n; ++i) {
1879 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1889 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1891 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1894 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1895 __isl_take isl_basic_set *bset,
1896 enum isl_dim_type type, unsigned first, unsigned n)
1898 return (isl_basic_set *)
1899 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1902 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1903 enum isl_dim_type type, unsigned first, unsigned n)
1910 map = isl_map_cow(map);
1913 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1915 for (i = 0; i < map->n; ++i) {
1916 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1917 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1921 map = isl_map_drop(map, type, first, n);
1928 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1929 enum isl_dim_type type, unsigned first, unsigned n)
1931 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1934 /* Project out n inputs starting at first using Fourier-Motzkin */
1935 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1936 unsigned first, unsigned n)
1938 return isl_map_remove_dims(map, isl_dim_in, first, n);
1941 static void dump_term(struct isl_basic_map *bmap,
1942 isl_int c, int pos, FILE *out)
1945 unsigned in = isl_basic_map_n_in(bmap);
1946 unsigned dim = in + isl_basic_map_n_out(bmap);
1947 unsigned nparam = isl_basic_map_n_param(bmap);
1949 isl_int_print(out, c, 0);
1951 if (!isl_int_is_one(c))
1952 isl_int_print(out, c, 0);
1953 if (pos < 1 + nparam) {
1954 name = isl_space_get_dim_name(bmap->dim,
1955 isl_dim_param, pos - 1);
1957 fprintf(out, "%s", name);
1959 fprintf(out, "p%d", pos - 1);
1960 } else if (pos < 1 + nparam + in)
1961 fprintf(out, "i%d", pos - 1 - nparam);
1962 else if (pos < 1 + nparam + dim)
1963 fprintf(out, "o%d", pos - 1 - nparam - in);
1965 fprintf(out, "e%d", pos - 1 - nparam - dim);
1969 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1970 int sign, FILE *out)
1974 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1978 for (i = 0, first = 1; i < len; ++i) {
1979 if (isl_int_sgn(c[i]) * sign <= 0)
1982 fprintf(out, " + ");
1984 isl_int_abs(v, c[i]);
1985 dump_term(bmap, v, i, out);
1992 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1993 const char *op, FILE *out, int indent)
1997 fprintf(out, "%*s", indent, "");
1999 dump_constraint_sign(bmap, c, 1, out);
2000 fprintf(out, " %s ", op);
2001 dump_constraint_sign(bmap, c, -1, out);
2005 for (i = bmap->n_div; i < bmap->extra; ++i) {
2006 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2008 fprintf(out, "%*s", indent, "");
2009 fprintf(out, "ERROR: unused div coefficient not zero\n");
2014 static void dump_constraints(struct isl_basic_map *bmap,
2015 isl_int **c, unsigned n,
2016 const char *op, FILE *out, int indent)
2020 for (i = 0; i < n; ++i)
2021 dump_constraint(bmap, c[i], op, out, indent);
2024 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2028 unsigned total = isl_basic_map_total_dim(bmap);
2030 for (j = 0; j < 1 + total; ++j) {
2031 if (isl_int_is_zero(exp[j]))
2033 if (!first && isl_int_is_pos(exp[j]))
2035 dump_term(bmap, exp[j], j, out);
2040 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2044 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2045 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2047 for (i = 0; i < bmap->n_div; ++i) {
2048 fprintf(out, "%*s", indent, "");
2049 fprintf(out, "e%d = [(", i);
2050 dump_affine(bmap, bmap->div[i]+1, out);
2052 isl_int_print(out, bmap->div[i][0], 0);
2053 fprintf(out, "]\n");
2057 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2058 FILE *out, int indent)
2061 fprintf(out, "null basic set\n");
2065 fprintf(out, "%*s", indent, "");
2066 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2067 bset->ref, bset->dim->nparam, bset->dim->n_out,
2068 bset->extra, bset->flags);
2069 dump((struct isl_basic_map *)bset, out, indent);
2072 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2073 FILE *out, int indent)
2076 fprintf(out, "null basic map\n");
2080 fprintf(out, "%*s", indent, "");
2081 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2082 "flags: %x, n_name: %d\n",
2084 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2085 bmap->extra, bmap->flags, bmap->dim->n_id);
2086 dump(bmap, out, indent);
2089 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2094 total = isl_basic_map_total_dim(bmap);
2095 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2096 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2097 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2098 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2102 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2105 struct isl_set *set;
2109 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2110 isl_assert(dim->ctx, n >= 0, goto error);
2111 set = isl_alloc(dim->ctx, struct isl_set,
2112 sizeof(struct isl_set) +
2113 (n - 1) * sizeof(struct isl_basic_set *));
2117 set->ctx = dim->ctx;
2118 isl_ctx_ref(set->ctx);
2126 isl_space_free(dim);
2130 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2131 unsigned nparam, unsigned dim, int n, unsigned flags)
2133 struct isl_set *set;
2136 dims = isl_space_alloc(ctx, nparam, 0, dim);
2140 set = isl_set_alloc_space(dims, n, flags);
2144 /* Make sure "map" has room for at least "n" more basic maps.
2146 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2149 struct isl_map *grown = NULL;
2153 isl_assert(map->ctx, n >= 0, goto error);
2154 if (map->n + n <= map->size)
2156 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2159 for (i = 0; i < map->n; ++i) {
2160 grown->p[i] = isl_basic_map_copy(map->p[i]);
2168 isl_map_free(grown);
2173 /* Make sure "set" has room for at least "n" more basic sets.
2175 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2177 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2180 struct isl_set *isl_set_dup(struct isl_set *set)
2183 struct isl_set *dup;
2188 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2191 for (i = 0; i < set->n; ++i)
2192 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2196 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2198 return isl_map_from_basic_map(bset);
2201 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2203 struct isl_map *map;
2208 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2209 return isl_map_add_basic_map(map, bmap);
2212 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2213 __isl_take isl_basic_set *bset)
2215 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2216 (struct isl_basic_map *)bset);
2219 void isl_set_free(struct isl_set *set)
2229 isl_ctx_deref(set->ctx);
2230 for (i = 0; i < set->n; ++i)
2231 isl_basic_set_free(set->p[i]);
2232 isl_space_free(set->dim);
2236 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2241 fprintf(out, "null set\n");
2245 fprintf(out, "%*s", indent, "");
2246 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2247 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2249 for (i = 0; i < set->n; ++i) {
2250 fprintf(out, "%*s", indent, "");
2251 fprintf(out, "basic set %d:\n", i);
2252 isl_basic_set_print_internal(set->p[i], out, indent+4);
2256 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2261 fprintf(out, "null map\n");
2265 fprintf(out, "%*s", indent, "");
2266 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2267 "flags: %x, n_name: %d\n",
2268 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2269 map->dim->n_out, map->flags, map->dim->n_id);
2270 for (i = 0; i < map->n; ++i) {
2271 fprintf(out, "%*s", indent, "");
2272 fprintf(out, "basic map %d:\n", i);
2273 isl_basic_map_print_internal(map->p[i], out, indent+4);
2277 struct isl_basic_map *isl_basic_map_intersect_domain(
2278 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2280 struct isl_basic_map *bmap_domain;
2285 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2286 bset->dim, isl_dim_param), goto error);
2288 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2289 isl_assert(bset->ctx,
2290 isl_basic_map_compatible_domain(bmap, bset), goto error);
2292 bmap = isl_basic_map_cow(bmap);
2295 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2296 bset->n_div, bset->n_eq, bset->n_ineq);
2297 bmap_domain = isl_basic_map_from_domain(bset);
2298 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2300 bmap = isl_basic_map_simplify(bmap);
2301 return isl_basic_map_finalize(bmap);
2303 isl_basic_map_free(bmap);
2304 isl_basic_set_free(bset);
2308 struct isl_basic_map *isl_basic_map_intersect_range(
2309 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2311 struct isl_basic_map *bmap_range;
2316 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2317 bset->dim, isl_dim_param), goto error);
2319 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2320 isl_assert(bset->ctx,
2321 isl_basic_map_compatible_range(bmap, bset), goto error);
2323 if (isl_basic_set_is_universe(bset)) {
2324 isl_basic_set_free(bset);
2328 bmap = isl_basic_map_cow(bmap);
2331 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2332 bset->n_div, bset->n_eq, bset->n_ineq);
2333 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2334 bmap = add_constraints(bmap, bmap_range, 0, 0);
2336 bmap = isl_basic_map_simplify(bmap);
2337 return isl_basic_map_finalize(bmap);
2339 isl_basic_map_free(bmap);
2340 isl_basic_set_free(bset);
2344 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2350 total = 1 + isl_basic_map_total_dim(bmap);
2351 if (total != vec->size)
2356 for (i = 0; i < bmap->n_eq; ++i) {
2357 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2358 if (!isl_int_is_zero(s)) {
2364 for (i = 0; i < bmap->n_ineq; ++i) {
2365 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2366 if (isl_int_is_neg(s)) {
2377 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2379 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2382 struct isl_basic_map *isl_basic_map_intersect(
2383 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2385 struct isl_vec *sample = NULL;
2387 if (!bmap1 || !bmap2)
2390 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2391 bmap2->dim, isl_dim_param), goto error);
2392 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2393 isl_space_dim(bmap1->dim, isl_dim_param) &&
2394 isl_space_dim(bmap2->dim, isl_dim_all) !=
2395 isl_space_dim(bmap2->dim, isl_dim_param))
2396 return isl_basic_map_intersect(bmap2, bmap1);
2398 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2399 isl_space_dim(bmap2->dim, isl_dim_param))
2400 isl_assert(bmap1->ctx,
2401 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2403 if (bmap1->sample &&
2404 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2405 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2406 sample = isl_vec_copy(bmap1->sample);
2407 else if (bmap2->sample &&
2408 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2409 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2410 sample = isl_vec_copy(bmap2->sample);
2412 bmap1 = isl_basic_map_cow(bmap1);
2415 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2416 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2417 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2420 isl_vec_free(sample);
2422 isl_vec_free(bmap1->sample);
2423 bmap1->sample = sample;
2426 bmap1 = isl_basic_map_simplify(bmap1);
2427 return isl_basic_map_finalize(bmap1);
2430 isl_vec_free(sample);
2431 isl_basic_map_free(bmap1);
2432 isl_basic_map_free(bmap2);
2436 struct isl_basic_set *isl_basic_set_intersect(
2437 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2439 return (struct isl_basic_set *)
2440 isl_basic_map_intersect(
2441 (struct isl_basic_map *)bset1,
2442 (struct isl_basic_map *)bset2);
2445 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2446 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2448 return isl_basic_set_intersect(bset1, bset2);
2451 /* Special case of isl_map_intersect, where both map1 and map2
2452 * are convex, without any divs and such that either map1 or map2
2453 * contains a single constraint. This constraint is then simply
2454 * added to the other map.
2456 static __isl_give isl_map *map_intersect_add_constraint(
2457 __isl_take isl_map *map1, __isl_take isl_map *map2)
2459 isl_assert(map1->ctx, map1->n == 1, goto error);
2460 isl_assert(map2->ctx, map1->n == 1, goto error);
2461 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2462 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2464 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2465 return isl_map_intersect(map2, map1);
2467 isl_assert(map2->ctx,
2468 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2470 map1 = isl_map_cow(map1);
2473 if (isl_map_plain_is_empty(map1)) {
2477 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2478 if (map2->p[0]->n_eq == 1)
2479 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2481 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2482 map2->p[0]->ineq[0]);
2484 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2485 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2489 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2490 isl_basic_map_free(map1->p[0]);
2503 /* map2 may be either a parameter domain or a map living in the same
2506 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2507 __isl_take isl_map *map2)
2510 struct isl_map *result;
2516 if (isl_map_plain_is_empty(map1) &&
2517 isl_space_is_equal(map1->dim, map2->dim)) {
2521 if (isl_map_plain_is_empty(map2) &&
2522 isl_space_is_equal(map1->dim, map2->dim)) {
2527 if (map1->n == 1 && map2->n == 1 &&
2528 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2529 isl_space_is_equal(map1->dim, map2->dim) &&
2530 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2531 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2532 return map_intersect_add_constraint(map1, map2);
2534 if (isl_space_dim(map2->dim, isl_dim_all) !=
2535 isl_space_dim(map2->dim, isl_dim_param))
2536 isl_assert(map1->ctx,
2537 isl_space_is_equal(map1->dim, map2->dim), goto error);
2539 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2540 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2541 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2543 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2544 map1->n * map2->n, flags);
2547 for (i = 0; i < map1->n; ++i)
2548 for (j = 0; j < map2->n; ++j) {
2549 struct isl_basic_map *part;
2550 part = isl_basic_map_intersect(
2551 isl_basic_map_copy(map1->p[i]),
2552 isl_basic_map_copy(map2->p[j]));
2553 if (isl_basic_map_is_empty(part))
2554 isl_basic_map_free(part);
2556 result = isl_map_add_basic_map(result, part);
2569 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2570 __isl_take isl_map *map2)
2574 if (!isl_space_is_equal(map1->dim, map2->dim))
2575 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2576 "spaces don't match", goto error);
2577 return map_intersect_internal(map1, map2);
2584 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2585 __isl_take isl_map *map2)
2587 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2590 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2592 return (struct isl_set *)
2593 isl_map_intersect((struct isl_map *)set1,
2594 (struct isl_map *)set2);
2597 /* map_intersect_internal accepts intersections
2598 * with parameter domains, so we can just call that function.
2600 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2601 __isl_take isl_set *params)
2603 return map_intersect_internal(map, params);
2606 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2607 __isl_take isl_map *map2)
2609 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2612 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2613 __isl_take isl_set *params)
2615 return isl_map_intersect_params(set, params);
2618 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2621 struct isl_basic_set *bset;
2626 bmap = isl_basic_map_cow(bmap);
2629 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2630 in = isl_basic_map_n_in(bmap);
2631 bset = isl_basic_set_from_basic_map(bmap);
2632 bset = isl_basic_set_swap_vars(bset, in);
2633 return isl_basic_map_from_basic_set(bset, dim);
2636 static __isl_give isl_basic_map *basic_map_space_reset(
2637 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2641 if (!isl_space_is_named_or_nested(bmap->dim, type))
2644 space = isl_basic_map_get_space(bmap);
2645 space = isl_space_reset(space, type);
2646 bmap = isl_basic_map_reset_space(bmap, space);
2650 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2651 enum isl_dim_type type, unsigned pos, unsigned n)
2654 struct isl_basic_map *res;
2655 struct isl_dim_map *dim_map;
2656 unsigned total, off;
2657 enum isl_dim_type t;
2660 return basic_map_space_reset(bmap, type);
2665 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2667 total = isl_basic_map_total_dim(bmap) + n;
2668 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2670 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2672 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2674 unsigned size = isl_basic_map_dim(bmap, t);
2675 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2677 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2678 pos, size - pos, off + pos + n);
2680 off += isl_space_dim(res_dim, t);
2682 isl_dim_map_div(dim_map, bmap, off);
2684 res = isl_basic_map_alloc_space(res_dim,
2685 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2686 if (isl_basic_map_is_rational(bmap))
2687 res = isl_basic_map_set_rational(res);
2688 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2689 return isl_basic_map_finalize(res);
2692 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2693 enum isl_dim_type type, unsigned n)
2697 return isl_basic_map_insert(bmap, type,
2698 isl_basic_map_dim(bmap, type), n);
2701 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2702 enum isl_dim_type type, unsigned n)
2706 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2707 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2709 isl_basic_set_free(bset);
2713 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2714 enum isl_dim_type type)
2718 if (!map || !isl_space_is_named_or_nested(map->dim, type))
2721 space = isl_map_get_space(map);
2722 space = isl_space_reset(space, type);
2723 map = isl_map_reset_space(map, space);
2727 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2728 enum isl_dim_type type, unsigned pos, unsigned n)
2733 return map_space_reset(map, type);
2735 map = isl_map_cow(map);
2739 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2743 for (i = 0; i < map->n; ++i) {
2744 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2755 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2756 enum isl_dim_type type, unsigned pos, unsigned n)
2758 return isl_map_insert_dims(set, type, pos, n);
2761 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2762 enum isl_dim_type type, unsigned n)
2766 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2769 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2770 enum isl_dim_type type, unsigned n)
2774 isl_assert(set->ctx, type != isl_dim_in, goto error);
2775 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2781 __isl_give isl_basic_map *isl_basic_map_move_dims(
2782 __isl_take isl_basic_map *bmap,
2783 enum isl_dim_type dst_type, unsigned dst_pos,
2784 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2786 struct isl_dim_map *dim_map;
2787 struct isl_basic_map *res;
2788 enum isl_dim_type t;
2789 unsigned total, off;
2796 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2799 if (dst_type == src_type && dst_pos == src_pos)
2802 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2804 if (pos(bmap->dim, dst_type) + dst_pos ==
2805 pos(bmap->dim, src_type) + src_pos +
2806 ((src_type < dst_type) ? n : 0)) {
2807 bmap = isl_basic_map_cow(bmap);
2811 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2812 src_type, src_pos, n);
2816 bmap = isl_basic_map_finalize(bmap);
2821 total = isl_basic_map_total_dim(bmap);
2822 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2825 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2826 unsigned size = isl_space_dim(bmap->dim, t);
2827 if (t == dst_type) {
2828 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2831 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2834 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2835 dst_pos, size - dst_pos, off);
2836 off += size - dst_pos;
2837 } else if (t == src_type) {
2838 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2841 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2842 src_pos + n, size - src_pos - n, off);
2843 off += size - src_pos - n;
2845 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2849 isl_dim_map_div(dim_map, bmap, off);
2851 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2852 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2853 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2855 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2856 src_type, src_pos, n);
2860 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2861 bmap = isl_basic_map_gauss(bmap, NULL);
2862 bmap = isl_basic_map_finalize(bmap);
2866 isl_basic_map_free(bmap);
2870 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2871 enum isl_dim_type dst_type, unsigned dst_pos,
2872 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2874 return (isl_basic_set *)isl_basic_map_move_dims(
2875 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2878 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2879 enum isl_dim_type dst_type, unsigned dst_pos,
2880 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2884 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2885 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2886 src_type, src_pos, n);
2892 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2893 enum isl_dim_type dst_type, unsigned dst_pos,
2894 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2903 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2906 if (dst_type == src_type && dst_pos == src_pos)
2909 isl_assert(map->ctx, dst_type != src_type, goto error);
2911 map = isl_map_cow(map);
2915 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2919 for (i = 0; i < map->n; ++i) {
2920 map->p[i] = isl_basic_map_move_dims(map->p[i],
2922 src_type, src_pos, n);
2933 /* Move the specified dimensions to the last columns right before
2934 * the divs. Don't change the dimension specification of bmap.
2935 * That's the responsibility of the caller.
2937 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2938 enum isl_dim_type type, unsigned first, unsigned n)
2940 struct isl_dim_map *dim_map;
2941 struct isl_basic_map *res;
2942 enum isl_dim_type t;
2943 unsigned total, off;
2947 if (pos(bmap->dim, type) + first + n ==
2948 1 + isl_space_dim(bmap->dim, isl_dim_all))
2951 total = isl_basic_map_total_dim(bmap);
2952 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2955 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2956 unsigned size = isl_space_dim(bmap->dim, t);
2958 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2961 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2962 first, n, total - bmap->n_div - n);
2963 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2964 first + n, size - (first + n), off);
2965 off += size - (first + n);
2967 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2971 isl_dim_map_div(dim_map, bmap, off + n);
2973 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2974 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2975 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2979 /* Turn the n dimensions of type type, starting at first
2980 * into existentially quantified variables.
2982 __isl_give isl_basic_map *isl_basic_map_project_out(
2983 __isl_take isl_basic_map *bmap,
2984 enum isl_dim_type type, unsigned first, unsigned n)
2997 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2998 return isl_basic_map_remove_dims(bmap, type, first, n);
3000 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3003 bmap = move_last(bmap, type, first, n);
3004 bmap = isl_basic_map_cow(bmap);
3008 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3009 old = bmap->block2.data;
3010 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3011 (bmap->extra + n) * (1 + row_size));
3012 if (!bmap->block2.data)
3014 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3017 for (i = 0; i < n; ++i) {
3018 new_div[i] = bmap->block2.data +
3019 (bmap->extra + i) * (1 + row_size);
3020 isl_seq_clr(new_div[i], 1 + row_size);
3022 for (i = 0; i < bmap->extra; ++i)
3023 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3025 bmap->div = new_div;
3029 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3032 bmap = isl_basic_map_simplify(bmap);
3033 bmap = isl_basic_map_drop_redundant_divs(bmap);
3034 return isl_basic_map_finalize(bmap);
3036 isl_basic_map_free(bmap);
3040 /* Turn the n dimensions of type type, starting at first
3041 * into existentially quantified variables.
3043 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3044 enum isl_dim_type type, unsigned first, unsigned n)
3046 return (isl_basic_set *)isl_basic_map_project_out(
3047 (isl_basic_map *)bset, type, first, n);
3050 /* Turn the n dimensions of type type, starting at first
3051 * into existentially quantified variables.
3053 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3054 enum isl_dim_type type, unsigned first, unsigned n)
3064 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3066 map = isl_map_cow(map);
3070 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3074 for (i = 0; i < map->n; ++i) {
3075 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3086 /* Turn the n dimensions of type type, starting at first
3087 * into existentially quantified variables.
3089 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3090 enum isl_dim_type type, unsigned first, unsigned n)
3092 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3095 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3099 for (i = 0; i < n; ++i) {
3100 j = isl_basic_map_alloc_div(bmap);
3103 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3107 isl_basic_map_free(bmap);
3111 struct isl_basic_map *isl_basic_map_apply_range(
3112 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3114 isl_space *dim_result = NULL;
3115 struct isl_basic_map *bmap;
3116 unsigned n_in, n_out, n, nparam, total, pos;
3117 struct isl_dim_map *dim_map1, *dim_map2;
3119 if (!bmap1 || !bmap2)
3122 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3123 isl_space_copy(bmap2->dim));
3125 n_in = isl_basic_map_n_in(bmap1);
3126 n_out = isl_basic_map_n_out(bmap2);
3127 n = isl_basic_map_n_out(bmap1);
3128 nparam = isl_basic_map_n_param(bmap1);
3130 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3131 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3132 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3133 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3134 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3135 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3136 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3137 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3138 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3139 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3140 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3142 bmap = isl_basic_map_alloc_space(dim_result,
3143 bmap1->n_div + bmap2->n_div + n,
3144 bmap1->n_eq + bmap2->n_eq,
3145 bmap1->n_ineq + bmap2->n_ineq);
3146 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3147 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3148 bmap = add_divs(bmap, n);
3149 bmap = isl_basic_map_simplify(bmap);
3150 bmap = isl_basic_map_drop_redundant_divs(bmap);
3151 return isl_basic_map_finalize(bmap);
3153 isl_basic_map_free(bmap1);
3154 isl_basic_map_free(bmap2);
3158 struct isl_basic_set *isl_basic_set_apply(
3159 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3164 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3167 return (struct isl_basic_set *)
3168 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3170 isl_basic_set_free(bset);
3171 isl_basic_map_free(bmap);
3175 struct isl_basic_map *isl_basic_map_apply_domain(
3176 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3178 if (!bmap1 || !bmap2)
3181 isl_assert(bmap1->ctx,
3182 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3183 isl_assert(bmap1->ctx,
3184 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3187 bmap1 = isl_basic_map_reverse(bmap1);
3188 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3189 return isl_basic_map_reverse(bmap1);
3191 isl_basic_map_free(bmap1);
3192 isl_basic_map_free(bmap2);
3196 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3197 * A \cap B -> f(A) + f(B)
3199 struct isl_basic_map *isl_basic_map_sum(
3200 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3202 unsigned n_in, n_out, nparam, total, pos;
3203 struct isl_basic_map *bmap = NULL;
3204 struct isl_dim_map *dim_map1, *dim_map2;
3207 if (!bmap1 || !bmap2)
3210 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3213 nparam = isl_basic_map_n_param(bmap1);
3214 n_in = isl_basic_map_n_in(bmap1);
3215 n_out = isl_basic_map_n_out(bmap1);
3217 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3218 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3219 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3220 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3221 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3222 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3223 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3224 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3225 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3226 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3227 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3229 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3230 bmap1->n_div + bmap2->n_div + 2 * n_out,
3231 bmap1->n_eq + bmap2->n_eq + n_out,
3232 bmap1->n_ineq + bmap2->n_ineq);
3233 for (i = 0; i < n_out; ++i) {
3234 int j = isl_basic_map_alloc_equality(bmap);
3237 isl_seq_clr(bmap->eq[j], 1+total);
3238 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3239 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3240 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3242 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3243 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3244 bmap = add_divs(bmap, 2 * n_out);
3246 bmap = isl_basic_map_simplify(bmap);
3247 return isl_basic_map_finalize(bmap);
3249 isl_basic_map_free(bmap);
3250 isl_basic_map_free(bmap1);
3251 isl_basic_map_free(bmap2);
3255 /* Given two maps A -> f(A) and B -> g(B), construct a map
3256 * A \cap B -> f(A) + f(B)
3258 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3260 struct isl_map *result;
3266 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3268 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3269 map1->n * map2->n, 0);
3272 for (i = 0; i < map1->n; ++i)
3273 for (j = 0; j < map2->n; ++j) {
3274 struct isl_basic_map *part;
3275 part = isl_basic_map_sum(
3276 isl_basic_map_copy(map1->p[i]),
3277 isl_basic_map_copy(map2->p[j]));
3278 if (isl_basic_map_is_empty(part))
3279 isl_basic_map_free(part);
3281 result = isl_map_add_basic_map(result, part);
3294 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3295 __isl_take isl_set *set2)
3297 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3300 /* Given a basic map A -> f(A), construct A -> -f(A).
3302 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3307 bmap = isl_basic_map_cow(bmap);
3311 n = isl_basic_map_dim(bmap, isl_dim_out);
3312 off = isl_basic_map_offset(bmap, isl_dim_out);
3313 for (i = 0; i < bmap->n_eq; ++i)
3314 for (j = 0; j < n; ++j)
3315 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3316 for (i = 0; i < bmap->n_ineq; ++i)
3317 for (j = 0; j < n; ++j)
3318 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3319 for (i = 0; i < bmap->n_div; ++i)
3320 for (j = 0; j < n; ++j)
3321 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3322 bmap = isl_basic_map_gauss(bmap, NULL);
3323 return isl_basic_map_finalize(bmap);
3326 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3328 return isl_basic_map_neg(bset);
3331 /* Given a map A -> f(A), construct A -> -f(A).
3333 struct isl_map *isl_map_neg(struct isl_map *map)
3337 map = isl_map_cow(map);
3341 for (i = 0; i < map->n; ++i) {
3342 map->p[i] = isl_basic_map_neg(map->p[i]);
3353 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3355 return (isl_set *)isl_map_neg((isl_map *)set);
3358 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3359 * A -> floor(f(A)/d).
3361 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3364 unsigned n_in, n_out, nparam, total, pos;
3365 struct isl_basic_map *result = NULL;
3366 struct isl_dim_map *dim_map;
3372 nparam = isl_basic_map_n_param(bmap);
3373 n_in = isl_basic_map_n_in(bmap);
3374 n_out = isl_basic_map_n_out(bmap);
3376 total = nparam + n_in + n_out + bmap->n_div + n_out;
3377 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3378 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3379 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3380 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3381 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3383 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3384 bmap->n_div + n_out,
3385 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3386 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3387 result = add_divs(result, n_out);
3388 for (i = 0; i < n_out; ++i) {
3390 j = isl_basic_map_alloc_inequality(result);
3393 isl_seq_clr(result->ineq[j], 1+total);
3394 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3395 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3396 j = isl_basic_map_alloc_inequality(result);
3399 isl_seq_clr(result->ineq[j], 1+total);
3400 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3401 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3402 isl_int_sub_ui(result->ineq[j][0], d, 1);
3405 result = isl_basic_map_simplify(result);
3406 return isl_basic_map_finalize(result);
3408 isl_basic_map_free(result);
3412 /* Given a map A -> f(A) and an integer d, construct a map
3413 * A -> floor(f(A)/d).
3415 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3419 map = isl_map_cow(map);
3423 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3424 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3425 for (i = 0; i < map->n; ++i) {
3426 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3437 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3443 i = isl_basic_map_alloc_equality(bmap);
3446 nparam = isl_basic_map_n_param(bmap);
3447 n_in = isl_basic_map_n_in(bmap);
3448 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3449 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3450 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3451 return isl_basic_map_finalize(bmap);
3453 isl_basic_map_free(bmap);
3457 /* Add a constraints to "bmap" expressing i_pos < o_pos
3459 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3465 i = isl_basic_map_alloc_inequality(bmap);
3468 nparam = isl_basic_map_n_param(bmap);
3469 n_in = isl_basic_map_n_in(bmap);
3470 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3471 isl_int_set_si(bmap->ineq[i][0], -1);
3472 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3473 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3474 return isl_basic_map_finalize(bmap);
3476 isl_basic_map_free(bmap);
3480 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3482 static __isl_give isl_basic_map *var_less_or_equal(
3483 __isl_take isl_basic_map *bmap, unsigned pos)
3489 i = isl_basic_map_alloc_inequality(bmap);
3492 nparam = isl_basic_map_n_param(bmap);
3493 n_in = isl_basic_map_n_in(bmap);
3494 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3495 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3496 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3497 return isl_basic_map_finalize(bmap);
3499 isl_basic_map_free(bmap);
3503 /* Add a constraints to "bmap" expressing i_pos > o_pos
3505 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3511 i = isl_basic_map_alloc_inequality(bmap);
3514 nparam = isl_basic_map_n_param(bmap);
3515 n_in = isl_basic_map_n_in(bmap);
3516 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3517 isl_int_set_si(bmap->ineq[i][0], -1);
3518 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3519 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3520 return isl_basic_map_finalize(bmap);
3522 isl_basic_map_free(bmap);
3526 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3528 static __isl_give isl_basic_map *var_more_or_equal(
3529 __isl_take isl_basic_map *bmap, unsigned pos)
3535 i = isl_basic_map_alloc_inequality(bmap);
3538 nparam = isl_basic_map_n_param(bmap);
3539 n_in = isl_basic_map_n_in(bmap);
3540 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3541 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3542 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3543 return isl_basic_map_finalize(bmap);
3545 isl_basic_map_free(bmap);
3549 __isl_give isl_basic_map *isl_basic_map_equal(
3550 __isl_take isl_space *dim, unsigned n_equal)
3553 struct isl_basic_map *bmap;
3554 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3557 for (i = 0; i < n_equal && bmap; ++i)
3558 bmap = var_equal(bmap, i);
3559 return isl_basic_map_finalize(bmap);
3562 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3564 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3568 struct isl_basic_map *bmap;
3569 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3572 for (i = 0; i < pos && bmap; ++i)
3573 bmap = var_equal(bmap, i);
3575 bmap = var_less(bmap, pos);
3576 return isl_basic_map_finalize(bmap);
3579 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3581 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3582 __isl_take isl_space *dim, unsigned pos)
3585 isl_basic_map *bmap;
3587 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3588 for (i = 0; i < pos; ++i)
3589 bmap = var_equal(bmap, i);
3590 bmap = var_less_or_equal(bmap, pos);
3591 return isl_basic_map_finalize(bmap);
3594 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3596 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3600 struct isl_basic_map *bmap;
3601 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3604 for (i = 0; i < pos && bmap; ++i)
3605 bmap = var_equal(bmap, i);
3607 bmap = var_more(bmap, pos);
3608 return isl_basic_map_finalize(bmap);
3611 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3613 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3614 __isl_take isl_space *dim, unsigned pos)
3617 isl_basic_map *bmap;
3619 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3620 for (i = 0; i < pos; ++i)
3621 bmap = var_equal(bmap, i);
3622 bmap = var_more_or_equal(bmap, pos);
3623 return isl_basic_map_finalize(bmap);
3626 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3627 unsigned n, int equal)
3629 struct isl_map *map;
3632 if (n == 0 && equal)
3633 return isl_map_universe(dims);
3635 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3637 for (i = 0; i + 1 < n; ++i)
3638 map = isl_map_add_basic_map(map,
3639 isl_basic_map_less_at(isl_space_copy(dims), i));
3642 map = isl_map_add_basic_map(map,
3643 isl_basic_map_less_or_equal_at(dims, n - 1));
3645 map = isl_map_add_basic_map(map,
3646 isl_basic_map_less_at(dims, n - 1));
3648 isl_space_free(dims);
3653 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3657 return map_lex_lte_first(dims, dims->n_out, equal);
3660 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3662 return map_lex_lte_first(dim, n, 0);
3665 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3667 return map_lex_lte_first(dim, n, 1);
3670 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3672 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3675 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3677 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3680 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3681 unsigned n, int equal)
3683 struct isl_map *map;
3686 if (n == 0 && equal)
3687 return isl_map_universe(dims);
3689 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3691 for (i = 0; i + 1 < n; ++i)
3692 map = isl_map_add_basic_map(map,
3693 isl_basic_map_more_at(isl_space_copy(dims), i));
3696 map = isl_map_add_basic_map(map,
3697 isl_basic_map_more_or_equal_at(dims, n - 1));
3699 map = isl_map_add_basic_map(map,
3700 isl_basic_map_more_at(dims, n - 1));
3702 isl_space_free(dims);
3707 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3711 return map_lex_gte_first(dims, dims->n_out, equal);
3714 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3716 return map_lex_gte_first(dim, n, 0);
3719 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3721 return map_lex_gte_first(dim, n, 1);
3724 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3726 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3729 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3731 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3734 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3735 __isl_take isl_set *set2)
3738 map = isl_map_lex_le(isl_set_get_space(set1));
3739 map = isl_map_intersect_domain(map, set1);
3740 map = isl_map_intersect_range(map, set2);
3744 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3745 __isl_take isl_set *set2)
3748 map = isl_map_lex_lt(isl_set_get_space(set1));
3749 map = isl_map_intersect_domain(map, set1);
3750 map = isl_map_intersect_range(map, set2);
3754 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3755 __isl_take isl_set *set2)
3758 map = isl_map_lex_ge(isl_set_get_space(set1));
3759 map = isl_map_intersect_domain(map, set1);
3760 map = isl_map_intersect_range(map, set2);
3764 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3765 __isl_take isl_set *set2)
3768 map = isl_map_lex_gt(isl_set_get_space(set1));
3769 map = isl_map_intersect_domain(map, set1);
3770 map = isl_map_intersect_range(map, set2);
3774 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3775 __isl_take isl_map *map2)
3778 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3779 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3780 map = isl_map_apply_range(map, isl_map_reverse(map2));
3784 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3785 __isl_take isl_map *map2)
3788 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3789 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3790 map = isl_map_apply_range(map, isl_map_reverse(map2));
3794 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3795 __isl_take isl_map *map2)
3798 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3799 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3800 map = isl_map_apply_range(map, isl_map_reverse(map2));
3804 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3805 __isl_take isl_map *map2)
3808 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3809 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3810 map = isl_map_apply_range(map, isl_map_reverse(map2));
3814 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3815 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3817 struct isl_basic_map *bmap;
3819 bset = isl_basic_set_cow(bset);
3823 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3824 isl_space_free(bset->dim);
3825 bmap = (struct isl_basic_map *) bset;
3827 return isl_basic_map_finalize(bmap);
3829 isl_basic_set_free(bset);
3830 isl_space_free(dim);
3834 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3838 if (bmap->dim->n_in == 0)
3839 return (struct isl_basic_set *)bmap;
3840 bmap = isl_basic_map_cow(bmap);
3843 bmap->dim = isl_space_as_set_space(bmap->dim);
3846 bmap = isl_basic_map_finalize(bmap);
3847 return (struct isl_basic_set *)bmap;
3849 isl_basic_map_free(bmap);
3853 /* For a div d = floor(f/m), add the constraints
3856 * -(f-(n-1)) + m d >= 0
3858 * Note that the second constraint is the negation of
3862 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3863 unsigned pos, isl_int *div)
3866 unsigned total = isl_basic_map_total_dim(bmap);
3868 i = isl_basic_map_alloc_inequality(bmap);
3871 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3872 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3874 j = isl_basic_map_alloc_inequality(bmap);
3877 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3878 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3879 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3883 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3884 unsigned pos, isl_int *div)
3886 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3890 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3892 unsigned total = isl_basic_map_total_dim(bmap);
3893 unsigned div_pos = total - bmap->n_div + div;
3895 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3899 struct isl_basic_set *isl_basic_map_underlying_set(
3900 struct isl_basic_map *bmap)
3904 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3906 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3907 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3908 return (struct isl_basic_set *)bmap;
3909 bmap = isl_basic_map_cow(bmap);
3912 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3915 bmap->extra -= bmap->n_div;
3917 bmap = isl_basic_map_finalize(bmap);
3918 return (struct isl_basic_set *)bmap;
3923 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3924 __isl_take isl_basic_set *bset)
3926 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3929 struct isl_basic_map *isl_basic_map_overlying_set(
3930 struct isl_basic_set *bset, struct isl_basic_map *like)
3932 struct isl_basic_map *bmap;
3933 struct isl_ctx *ctx;
3940 isl_assert(ctx, bset->n_div == 0, goto error);
3941 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3942 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3944 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3945 isl_basic_map_free(like);
3946 return (struct isl_basic_map *)bset;
3948 bset = isl_basic_set_cow(bset);
3951 total = bset->dim->n_out + bset->extra;
3952 bmap = (struct isl_basic_map *)bset;
3953 isl_space_free(bmap->dim);
3954 bmap->dim = isl_space_copy(like->dim);
3957 bmap->n_div = like->n_div;
3958 bmap->extra += like->n_div;
3962 ltotal = total - bmap->extra + like->extra;
3965 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3966 bmap->extra * (1 + 1 + total));
3967 if (isl_blk_is_error(bmap->block2))
3969 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3973 for (i = 0; i < bmap->extra; ++i)
3974 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3975 for (i = 0; i < like->n_div; ++i) {
3976 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3977 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3979 bmap = isl_basic_map_extend_constraints(bmap,
3980 0, 2 * like->n_div);
3981 for (i = 0; i < like->n_div; ++i) {
3982 if (isl_int_is_zero(bmap->div[i][0]))
3984 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3988 isl_basic_map_free(like);
3989 bmap = isl_basic_map_simplify(bmap);
3990 bmap = isl_basic_map_finalize(bmap);
3993 isl_basic_map_free(like);
3994 isl_basic_set_free(bset);
3998 struct isl_basic_set *isl_basic_set_from_underlying_set(
3999 struct isl_basic_set *bset, struct isl_basic_set *like)
4001 return (struct isl_basic_set *)
4002 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4005 struct isl_set *isl_set_from_underlying_set(
4006 struct isl_set *set, struct isl_basic_set *like)
4012 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4014 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4015 isl_basic_set_free(like);
4018 set = isl_set_cow(set);
4021 for (i = 0; i < set->n; ++i) {
4022 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4023 isl_basic_set_copy(like));
4027 isl_space_free(set->dim);
4028 set->dim = isl_space_copy(like->dim);
4031 isl_basic_set_free(like);
4034 isl_basic_set_free(like);
4039 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4043 map = isl_map_cow(map);
4046 map->dim = isl_space_cow(map->dim);
4050 for (i = 1; i < map->n; ++i)
4051 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4053 for (i = 0; i < map->n; ++i) {
4054 map->p[i] = (struct isl_basic_map *)
4055 isl_basic_map_underlying_set(map->p[i]);
4060 map->dim = isl_space_underlying(map->dim, 0);
4062 isl_space_free(map->dim);
4063 map->dim = isl_space_copy(map->p[0]->dim);
4067 return (struct isl_set *)map;
4073 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4075 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4078 __isl_give isl_basic_map *isl_basic_map_reset_space(
4079 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4081 bmap = isl_basic_map_cow(bmap);
4085 isl_space_free(bmap->dim);
4088 bmap = isl_basic_map_finalize(bmap);
4092 isl_basic_map_free(bmap);
4093 isl_space_free(dim);
4097 __isl_give isl_basic_set *isl_basic_set_reset_space(
4098 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4100 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4104 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4105 __isl_take isl_space *dim)
4109 map = isl_map_cow(map);
4113 for (i = 0; i < map->n; ++i) {
4114 map->p[i] = isl_basic_map_reset_space(map->p[i],
4115 isl_space_copy(dim));
4119 isl_space_free(map->dim);
4125 isl_space_free(dim);
4129 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4130 __isl_take isl_space *dim)
4132 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4135 /* Compute the parameter domain of the given basic set.
4137 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4142 if (isl_basic_set_is_params(bset))
4145 n = isl_basic_set_dim(bset, isl_dim_set);
4146 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4147 space = isl_basic_set_get_space(bset);
4148 space = isl_space_params(space);
4149 bset = isl_basic_set_reset_space(bset, space);
4153 /* Compute the parameter domain of the given set.
4155 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4160 if (isl_set_is_params(set))
4163 n = isl_set_dim(set, isl_dim_set);
4164 set = isl_set_project_out(set, isl_dim_set, 0, n);
4165 space = isl_set_get_space(set);
4166 space = isl_space_params(space);
4167 set = isl_set_reset_space(set, space);
4171 /* Construct a zero-dimensional set with the given parameter domain.
4173 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4176 space = isl_set_get_space(set);
4177 space = isl_space_set_from_params(space);
4178 set = isl_set_reset_space(set, space);
4182 /* Compute the parameter domain of the given map.
4184 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4189 n = isl_map_dim(map, isl_dim_in);
4190 map = isl_map_project_out(map, isl_dim_in, 0, n);
4191 n = isl_map_dim(map, isl_dim_out);
4192 map = isl_map_project_out(map, isl_dim_out, 0, n);
4193 space = isl_map_get_space(map);
4194 space = isl_space_params(space);
4195 map = isl_map_reset_space(map, space);
4199 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4202 struct isl_basic_set *domain;
4208 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4210 n_in = isl_basic_map_n_in(bmap);
4211 n_out = isl_basic_map_n_out(bmap);
4212 domain = isl_basic_set_from_basic_map(bmap);
4213 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4215 domain = isl_basic_set_reset_space(domain, dim);
4220 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4224 return isl_space_may_be_set(bmap->dim);
4227 /* Is this basic map actually a set?
4228 * Users should never call this function. Outside of isl,
4229 * the type should indicate whether something is a set or a map.
4231 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4235 return isl_space_is_set(bmap->dim);
4238 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4242 if (isl_basic_map_is_set(bmap))
4244 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4247 __isl_give isl_basic_map *isl_basic_map_domain_map(
4248 __isl_take isl_basic_map *bmap)
4252 isl_basic_map *domain;
4253 int nparam, n_in, n_out;
4256 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4257 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4258 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4260 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4261 domain = isl_basic_map_universe(dim);
4263 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4264 bmap = isl_basic_map_apply_range(bmap, domain);
4265 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4267 total = isl_basic_map_total_dim(bmap);
4269 for (i = 0; i < n_in; ++i) {
4270 k = isl_basic_map_alloc_equality(bmap);
4273 isl_seq_clr(bmap->eq[k], 1 + total);
4274 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4275 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4278 bmap = isl_basic_map_gauss(bmap, NULL);
4279 return isl_basic_map_finalize(bmap);
4281 isl_basic_map_free(bmap);
4285 __isl_give isl_basic_map *isl_basic_map_range_map(
4286 __isl_take isl_basic_map *bmap)
4290 isl_basic_map *range;
4291 int nparam, n_in, n_out;
4294 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4295 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4296 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4298 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4299 range = isl_basic_map_universe(dim);
4301 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4302 bmap = isl_basic_map_apply_range(bmap, range);
4303 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4305 total = isl_basic_map_total_dim(bmap);
4307 for (i = 0; i < n_out; ++i) {
4308 k = isl_basic_map_alloc_equality(bmap);
4311 isl_seq_clr(bmap->eq[k], 1 + total);
4312 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4313 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4316 bmap = isl_basic_map_gauss(bmap, NULL);
4317 return isl_basic_map_finalize(bmap);
4319 isl_basic_map_free(bmap);
4323 int isl_map_may_be_set(__isl_keep isl_map *map)
4327 return isl_space_may_be_set(map->dim);
4330 /* Is this map actually a set?
4331 * Users should never call this function. Outside of isl,
4332 * the type should indicate whether something is a set or a map.
4334 int isl_map_is_set(__isl_keep isl_map *map)
4338 return isl_space_is_set(map->dim);
4341 struct isl_set *isl_map_range(struct isl_map *map)
4344 struct isl_set *set;
4348 if (isl_map_is_set(map))
4349 return (isl_set *)map;
4351 map = isl_map_cow(map);
4355 set = (struct isl_set *) map;
4356 set->dim = isl_space_range(set->dim);
4359 for (i = 0; i < map->n; ++i) {
4360 set->p[i] = isl_basic_map_range(map->p[i]);
4364 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4365 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4372 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4375 isl_space *domain_dim;
4377 map = isl_map_cow(map);
4381 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4382 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4383 map->dim = isl_space_join(map->dim, domain_dim);
4386 for (i = 0; i < map->n; ++i) {
4387 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4391 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4392 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4399 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4402 isl_space *range_dim;
4404 map = isl_map_cow(map);
4408 range_dim = isl_space_range(isl_map_get_space(map));
4409 range_dim = isl_space_from_range(range_dim);
4410 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4411 map->dim = isl_space_join(map->dim, range_dim);
4414 for (i = 0; i < map->n; ++i) {
4415 map->p[i] = isl_basic_map_range_map(map->p[i]);
4419 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4420 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4427 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4428 __isl_take isl_space *dim)
4431 struct isl_map *map = NULL;
4433 set = isl_set_cow(set);
4436 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4437 map = (struct isl_map *)set;
4438 for (i = 0; i < set->n; ++i) {
4439 map->p[i] = isl_basic_map_from_basic_set(
4440 set->p[i], isl_space_copy(dim));
4444 isl_space_free(map->dim);
4448 isl_space_free(dim);
4453 __isl_give isl_basic_map *isl_basic_map_from_domain(
4454 __isl_take isl_basic_set *bset)
4456 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4459 __isl_give isl_basic_map *isl_basic_map_from_range(
4460 __isl_take isl_basic_set *bset)
4463 space = isl_basic_set_get_space(bset);
4464 space = isl_space_from_range(space);
4465 bset = isl_basic_set_reset_space(bset, space);
4466 return (isl_basic_map *)bset;
4469 struct isl_map *isl_map_from_range(struct isl_set *set)
4472 space = isl_set_get_space(set);
4473 space = isl_space_from_range(space);
4474 set = isl_set_reset_space(set, space);
4475 return (struct isl_map *)set;
4478 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4480 return isl_map_reverse(isl_map_from_range(set));
4483 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4484 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4486 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4489 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4490 __isl_take isl_set *range)
4492 return isl_map_apply_range(isl_map_reverse(domain), range);
4495 struct isl_set *isl_set_from_map(struct isl_map *map)
4498 struct isl_set *set = NULL;
4502 map = isl_map_cow(map);
4505 map->dim = isl_space_as_set_space(map->dim);
4508 set = (struct isl_set *)map;
4509 for (i = 0; i < map->n; ++i) {
4510 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4520 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4523 struct isl_map *map;
4528 isl_die(dim->ctx, isl_error_internal,
4529 "negative number of basic maps", goto error);
4530 map = isl_alloc(dim->ctx, struct isl_map,
4531 sizeof(struct isl_map) +
4532 (n - 1) * sizeof(struct isl_basic_map *));
4536 map->ctx = dim->ctx;
4537 isl_ctx_ref(map->ctx);
4545 isl_space_free(dim);
4549 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4550 unsigned nparam, unsigned in, unsigned out, int n,
4553 struct isl_map *map;
4556 dims = isl_space_alloc(ctx, nparam, in, out);
4560 map = isl_map_alloc_space(dims, n, flags);
4564 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4566 struct isl_basic_map *bmap;
4567 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4568 bmap = isl_basic_map_set_to_empty(bmap);
4572 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4574 struct isl_basic_set *bset;
4575 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4576 bset = isl_basic_set_set_to_empty(bset);
4580 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4582 struct isl_basic_map *bmap;
4585 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4586 bmap = isl_basic_map_set_to_empty(bmap);
4590 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4592 struct isl_basic_map *bmap;
4595 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4596 bmap = isl_basic_map_set_to_empty(bmap);
4600 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4602 struct isl_basic_set *bset;
4605 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4606 bset = isl_basic_set_set_to_empty(bset);
4610 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4612 struct isl_basic_map *bmap;
4613 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4614 bmap = isl_basic_map_finalize(bmap);
4618 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4620 struct isl_basic_set *bset;
4621 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4622 bset = isl_basic_set_finalize(bset);
4626 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4629 unsigned total = isl_space_dim(dim, isl_dim_all);
4630 isl_basic_map *bmap;
4632 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4633 for (i = 0; i < total; ++i) {
4634 int k = isl_basic_map_alloc_inequality(bmap);
4637 isl_seq_clr(bmap->ineq[k], 1 + total);
4638 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4642 isl_basic_map_free(bmap);
4646 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4648 return isl_basic_map_nat_universe(dim);
4651 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4653 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4656 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4658 return isl_map_nat_universe(dim);
4661 __isl_give isl_basic_map *isl_basic_map_universe_like(
4662 __isl_keep isl_basic_map *model)
4666 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4669 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4673 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4676 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4677 __isl_keep isl_set *model)
4681 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4684 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4686 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4689 struct isl_map *isl_map_empty_like(struct isl_map *model)
4693 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4696 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4700 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4703 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4705 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4708 struct isl_set *isl_set_empty_like(struct isl_set *model)
4712 return isl_set_empty(isl_space_copy(model->dim));
4715 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4717 struct isl_map *map;
4720 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4721 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4725 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4727 struct isl_set *set;
4730 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4731 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4735 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4739 return isl_set_universe(isl_space_copy(model->dim));
4742 struct isl_map *isl_map_dup(struct isl_map *map)
4745 struct isl_map *dup;
4749 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4750 for (i = 0; i < map->n; ++i)
4751 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4755 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4756 __isl_take isl_basic_map *bmap)
4760 if (isl_basic_map_plain_is_empty(bmap)) {
4761 isl_basic_map_free(bmap);
4764 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4765 isl_assert(map->ctx, map->n < map->size, goto error);
4766 map->p[map->n] = bmap;
4768 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4774 isl_basic_map_free(bmap);
4778 void isl_map_free(struct isl_map *map)
4788 isl_ctx_deref(map->ctx);
4789 for (i = 0; i < map->n; ++i)
4790 isl_basic_map_free(map->p[i]);
4791 isl_space_free(map->dim);
4795 struct isl_map *isl_map_extend(struct isl_map *base,
4796 unsigned nparam, unsigned n_in, unsigned n_out)
4800 base = isl_map_cow(base);
4804 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4807 for (i = 0; i < base->n; ++i) {
4808 base->p[i] = isl_basic_map_extend_space(base->p[i],
4809 isl_space_copy(base->dim), 0, 0, 0);
4819 struct isl_set *isl_set_extend(struct isl_set *base,
4820 unsigned nparam, unsigned dim)
4822 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4826 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4827 struct isl_basic_map *bmap, unsigned pos, int value)
4831 bmap = isl_basic_map_cow(bmap);
4832 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4833 j = isl_basic_map_alloc_equality(bmap);
4836 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4837 isl_int_set_si(bmap->eq[j][pos], -1);
4838 isl_int_set_si(bmap->eq[j][0], value);
4839 bmap = isl_basic_map_simplify(bmap);
4840 return isl_basic_map_finalize(bmap);
4842 isl_basic_map_free(bmap);
4846 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4847 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4851 bmap = isl_basic_map_cow(bmap);
4852 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4853 j = isl_basic_map_alloc_equality(bmap);
4856 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4857 isl_int_set_si(bmap->eq[j][pos], -1);
4858 isl_int_set(bmap->eq[j][0], value);
4859 bmap = isl_basic_map_simplify(bmap);
4860 return isl_basic_map_finalize(bmap);
4862 isl_basic_map_free(bmap);
4866 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4867 enum isl_dim_type type, unsigned pos, int value)
4871 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4872 return isl_basic_map_fix_pos_si(bmap,
4873 isl_basic_map_offset(bmap, type) + pos, value);
4875 isl_basic_map_free(bmap);
4879 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4880 enum isl_dim_type type, unsigned pos, isl_int value)
4884 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4885 return isl_basic_map_fix_pos(bmap,
4886 isl_basic_map_offset(bmap, type) + pos, value);
4888 isl_basic_map_free(bmap);
4892 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4893 enum isl_dim_type type, unsigned pos, int value)
4895 return (struct isl_basic_set *)
4896 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4900 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4901 enum isl_dim_type type, unsigned pos, isl_int value)
4903 return (struct isl_basic_set *)
4904 isl_basic_map_fix((struct isl_basic_map *)bset,
4908 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4909 unsigned input, int value)
4911 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4914 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4915 unsigned dim, int value)
4917 return (struct isl_basic_set *)
4918 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4919 isl_dim_set, dim, value);
4922 struct isl_map *isl_map_fix_si(struct isl_map *map,
4923 enum isl_dim_type type, unsigned pos, int value)
4927 map = isl_map_cow(map);
4931 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4932 for (i = 0; i < map->n; ++i) {
4933 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4937 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4944 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4945 enum isl_dim_type type, unsigned pos, int value)
4947 return (struct isl_set *)
4948 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4951 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4952 enum isl_dim_type type, unsigned pos, isl_int value)
4956 map = isl_map_cow(map);
4960 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4961 for (i = 0; i < map->n; ++i) {
4962 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4966 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4973 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4974 enum isl_dim_type type, unsigned pos, isl_int value)
4976 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4979 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4980 unsigned input, int value)
4982 return isl_map_fix_si(map, isl_dim_in, input, value);
4985 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4987 return (struct isl_set *)
4988 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4991 static __isl_give isl_basic_map *basic_map_bound_si(
4992 __isl_take isl_basic_map *bmap,
4993 enum isl_dim_type type, unsigned pos, int value, int upper)
4999 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5000 pos += isl_basic_map_offset(bmap, type);
5001 bmap = isl_basic_map_cow(bmap);
5002 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5003 j = isl_basic_map_alloc_inequality(bmap);
5006 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5008 isl_int_set_si(bmap->ineq[j][pos], -1);
5009 isl_int_set_si(bmap->ineq[j][0], value);
5011 isl_int_set_si(bmap->ineq[j][pos], 1);
5012 isl_int_set_si(bmap->ineq[j][0], -value);
5014 bmap = isl_basic_map_simplify(bmap);
5015 return isl_basic_map_finalize(bmap);
5017 isl_basic_map_free(bmap);
5021 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5022 __isl_take isl_basic_map *bmap,
5023 enum isl_dim_type type, unsigned pos, int value)
5025 return basic_map_bound_si(bmap, type, pos, value, 0);
5028 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5029 unsigned dim, isl_int value)
5033 bset = isl_basic_set_cow(bset);
5034 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5035 j = isl_basic_set_alloc_inequality(bset);
5038 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5039 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5040 isl_int_neg(bset->ineq[j][0], value);
5041 bset = isl_basic_set_simplify(bset);
5042 return isl_basic_set_finalize(bset);
5044 isl_basic_set_free(bset);
5048 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5049 enum isl_dim_type type, unsigned pos, int value, int upper)
5053 map = isl_map_cow(map);
5057 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5058 for (i = 0; i < map->n; ++i) {
5059 map->p[i] = basic_map_bound_si(map->p[i],
5060 type, pos, value, upper);
5064 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5071 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5072 enum isl_dim_type type, unsigned pos, int value)
5074 return map_bound_si(map, type, pos, value, 0);
5077 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5078 enum isl_dim_type type, unsigned pos, int value)
5080 return map_bound_si(map, type, pos, value, 1);
5083 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5084 enum isl_dim_type type, unsigned pos, int value)
5086 return (struct isl_set *)
5087 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5090 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5091 enum isl_dim_type type, unsigned pos, int value)
5093 return isl_map_upper_bound_si(set, type, pos, value);
5096 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5101 set = isl_set_cow(set);
5105 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5106 for (i = 0; i < set->n; ++i) {
5107 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5117 struct isl_map *isl_map_reverse(struct isl_map *map)
5121 map = isl_map_cow(map);
5125 map->dim = isl_space_reverse(map->dim);
5128 for (i = 0; i < map->n; ++i) {
5129 map->p[i] = isl_basic_map_reverse(map->p[i]);
5133 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5140 static struct isl_map *isl_basic_map_partial_lexopt(
5141 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5142 struct isl_set **empty, int max)
5146 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5147 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5149 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5151 isl_basic_map_free(bmap);
5152 isl_basic_set_free(dom);
5158 struct isl_map *isl_basic_map_partial_lexmax(
5159 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5160 struct isl_set **empty)
5162 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5165 struct isl_map *isl_basic_map_partial_lexmin(
5166 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5167 struct isl_set **empty)
5169 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5172 struct isl_set *isl_basic_set_partial_lexmin(
5173 struct isl_basic_set *bset, struct isl_basic_set *dom,
5174 struct isl_set **empty)
5176 return (struct isl_set *)
5177 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5181 struct isl_set *isl_basic_set_partial_lexmax(
5182 struct isl_basic_set *bset, struct isl_basic_set *dom,
5183 struct isl_set **empty)
5185 return (struct isl_set *)
5186 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5190 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5191 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5192 __isl_give isl_set **empty)
5194 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5197 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5198 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5199 __isl_give isl_set **empty)
5201 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5204 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5205 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5206 __isl_give isl_set **empty)
5208 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5211 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5212 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5213 __isl_give isl_set **empty)
5215 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5218 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5219 __isl_take isl_basic_map *bmap, int max)
5221 isl_basic_set *dom = NULL;
5222 isl_space *dom_space;
5226 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5227 dom = isl_basic_set_universe(dom_space);
5228 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5230 isl_basic_map_free(bmap);
5234 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5235 __isl_take isl_basic_map *bmap)
5237 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5240 /* Given a basic map "bmap", compute the lexicographically minimal
5241 * (or maximal) image element for each domain element in dom.
5242 * Set *empty to those elements in dom that do not have an image element.
5244 * We first make sure the basic sets in dom are disjoint and then
5245 * simply collect the results over each of the basic sets separately.
5246 * We could probably improve the efficiency a bit by moving the union
5247 * domain down into the parametric integer programming.
5249 static __isl_give isl_map *basic_map_partial_lexopt(
5250 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5251 __isl_give isl_set **empty, int max)
5254 struct isl_map *res;
5256 dom = isl_set_make_disjoint(dom);
5260 if (isl_set_plain_is_empty(dom)) {
5261 res = isl_map_empty_like_basic_map(bmap);
5262 *empty = isl_set_empty_like(dom);
5264 isl_basic_map_free(bmap);
5268 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5269 isl_basic_set_copy(dom->p[0]), empty, max);
5271 for (i = 1; i < dom->n; ++i) {
5272 struct isl_map *res_i;
5273 struct isl_set *empty_i;
5275 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5276 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5278 res = isl_map_union_disjoint(res, res_i);
5279 *empty = isl_set_union_disjoint(*empty, empty_i);
5283 isl_basic_map_free(bmap);
5288 isl_basic_map_free(bmap);
5292 /* Given a map "map", compute the lexicographically minimal
5293 * (or maximal) image element for each domain element in dom.
5294 * Set *empty to those elements in dom that do not have an image element.
5296 * We first compute the lexicographically minimal or maximal element
5297 * in the first basic map. This results in a partial solution "res"
5298 * and a subset "todo" of dom that still need to be handled.
5299 * We then consider each of the remaining maps in "map" and successively
5300 * improve both "res" and "todo".
5302 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5303 * Assume we are computing the lexicographical maximum.
5304 * We first compute the lexicographically maximal element in basic map i.
5305 * This results in a partial solution res_i and a subset todo_i.
5306 * Then we combine these results with those obtain for the first k basic maps
5307 * to obtain a result that is valid for the first k+1 basic maps.
5308 * In particular, the set where there is no solution is the set where
5309 * there is no solution for the first k basic maps and also no solution
5310 * for the ith basic map, i.e.,
5312 * todo^i = todo^k * todo_i
5314 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5315 * solutions, arbitrarily breaking ties in favor of res^k.
5316 * That is, when res^k(a) >= res_i(a), we pick res^k and
5317 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5318 * the lexicographic order.)
5319 * In practice, we compute
5321 * res^k * (res_i . "<=")
5325 * res_i * (res^k . "<")
5327 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5328 * where only one of res^k and res_i provides a solution and we simply pick
5335 * Note that we only compute these intersections when dom(res^k) intersects
5336 * dom(res_i). Otherwise, the only effect of these intersections is to
5337 * potentially break up res^k and res_i into smaller pieces.
5338 * We want to avoid such splintering as much as possible.
5339 * In fact, an earlier implementation of this function would look for
5340 * better results in the domain of res^k and for extra results in todo^k,
5341 * but this would always result in a splintering according to todo^k,
5342 * even when the domain of basic map i is disjoint from the domains of
5343 * the previous basic maps.
5345 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5346 __isl_take isl_map *map, __isl_take isl_set *dom,
5347 __isl_give isl_set **empty, int max)
5350 struct isl_map *res;
5351 struct isl_set *todo;
5356 if (isl_map_plain_is_empty(map)) {
5364 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5365 isl_set_copy(dom), &todo, max);
5367 for (i = 1; i < map->n; ++i) {
5371 isl_space *dim = isl_space_range(isl_map_get_space(res));
5373 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5374 isl_set_copy(dom), &todo_i, max);
5377 lt = isl_map_lex_lt(isl_space_copy(dim));
5378 le = isl_map_lex_le(dim);
5380 lt = isl_map_lex_gt(isl_space_copy(dim));
5381 le = isl_map_lex_ge(dim);
5383 lt = isl_map_apply_range(isl_map_copy(res), lt);
5384 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5385 le = isl_map_apply_range(isl_map_copy(res_i), le);
5386 le = isl_map_intersect(le, isl_map_copy(res));
5388 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5389 res = isl_map_intersect_domain(res,
5390 isl_set_copy(todo_i));
5391 res_i = isl_map_intersect_domain(res_i,
5392 isl_set_copy(todo));
5395 res = isl_map_union_disjoint(res, res_i);
5396 res = isl_map_union_disjoint(res, lt);
5397 res = isl_map_union_disjoint(res, le);
5399 todo = isl_set_intersect(todo, todo_i);
5419 /* Given a map "map", compute the lexicographically minimal
5420 * (or maximal) image element for each domain element in dom.
5421 * Set *empty to those elements in dom that do not have an image element.
5423 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5425 static __isl_give isl_map *isl_map_partial_lexopt(
5426 __isl_take isl_map *map, __isl_take isl_set *dom,
5427 __isl_give isl_set **empty, int max)
5431 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5432 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5433 if (!isl_space_has_named_params(map->dim) ||
5434 !isl_space_has_named_params(dom->dim))
5435 isl_die(map->ctx, isl_error_invalid,
5436 "unaligned unnamed parameters", goto error);
5437 map = isl_map_align_params(map, isl_map_get_space(dom));
5438 dom = isl_map_align_params(dom, isl_map_get_space(map));
5439 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5448 __isl_give isl_map *isl_map_partial_lexmax(
5449 __isl_take isl_map *map, __isl_take isl_set *dom,
5450 __isl_give isl_set **empty)
5452 return isl_map_partial_lexopt(map, dom, empty, 1);
5455 __isl_give isl_map *isl_map_partial_lexmin(
5456 __isl_take isl_map *map, __isl_take isl_set *dom,
5457 __isl_give isl_set **empty)
5459 return isl_map_partial_lexopt(map, dom, empty, 0);
5462 __isl_give isl_set *isl_set_partial_lexmin(
5463 __isl_take isl_set *set, __isl_take isl_set *dom,
5464 __isl_give isl_set **empty)
5466 return (struct isl_set *)
5467 isl_map_partial_lexmin((struct isl_map *)set,
5471 __isl_give isl_set *isl_set_partial_lexmax(
5472 __isl_take isl_set *set, __isl_take isl_set *dom,
5473 __isl_give isl_set **empty)
5475 return (struct isl_set *)
5476 isl_map_partial_lexmax((struct isl_map *)set,
5480 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5482 struct isl_basic_set *dom = NULL;
5487 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5488 dom = isl_basic_set_universe(dom_dim);
5489 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5491 isl_basic_map_free(bmap);
5495 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5497 return isl_basic_map_lexopt(bmap, 0);
5500 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5502 return isl_basic_map_lexopt(bmap, 1);
5505 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5507 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5510 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5512 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5515 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5517 struct isl_set *dom = NULL;
5522 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5523 dom = isl_set_universe(dom_dim);
5524 return isl_map_partial_lexopt(map, dom, NULL, max);
5530 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5532 return isl_map_lexopt(map, 0);
5535 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5537 return isl_map_lexopt(map, 1);
5540 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5542 return (isl_set *)isl_map_lexmin((isl_map *)set);
5545 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5547 return (isl_set *)isl_map_lexmax((isl_map *)set);
5550 /* Extract the first and only affine expression from list
5551 * and then add it to *pwaff with the given dom.
5552 * This domain is known to be disjoint from other domains
5553 * because of the way isl_basic_map_foreach_lexmax works.
5555 static int update_dim_opt(__isl_take isl_basic_set *dom,
5556 __isl_take isl_aff_list *list, void *user)
5558 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5560 isl_pw_aff **pwaff = user;
5561 isl_pw_aff *pwaff_i;
5563 if (isl_aff_list_n_aff(list) != 1)
5564 isl_die(ctx, isl_error_internal,
5565 "expecting single element list", goto error);
5567 aff = isl_aff_list_get_aff(list, 0);
5568 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5570 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5572 isl_aff_list_free(list);
5576 isl_basic_set_free(dom);
5577 isl_aff_list_free(list);
5581 /* Given a basic map with one output dimension, compute the minimum or
5582 * maximum of that dimension as an isl_pw_aff.
5584 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5585 * call update_dim_opt on each leaf of the result.
5587 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5590 isl_space *dim = isl_basic_map_get_space(bmap);
5594 dim = isl_space_from_domain(isl_space_domain(dim));
5595 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5596 pwaff = isl_pw_aff_empty(dim);
5598 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5600 return isl_pw_aff_free(pwaff);
5605 /* Compute the minimum or maximum of the given output dimension
5606 * as a function of the parameters and the input dimensions,
5607 * but independently of the other output dimensions.
5609 * We first project out the other output dimension and then compute
5610 * the "lexicographic" maximum in each basic map, combining the results
5611 * using isl_pw_aff_union_max.
5613 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5620 n_out = isl_map_dim(map, isl_dim_out);
5621 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5622 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5627 isl_space *dim = isl_map_get_space(map);
5628 dim = isl_space_domain(isl_space_from_range(dim));
5630 return isl_pw_aff_empty(dim);
5633 pwaff = basic_map_dim_opt(map->p[0], max);
5634 for (i = 1; i < map->n; ++i) {
5635 isl_pw_aff *pwaff_i;
5637 pwaff_i = basic_map_dim_opt(map->p[i], max);
5638 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5646 /* Compute the maximum of the given output dimension as a function of the
5647 * parameters and input dimensions, but independently of
5648 * the other output dimensions.
5650 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
5652 return map_dim_opt(map, pos, 1);
5655 /* Compute the minimum or maximum of the given set dimension
5656 * as a function of the parameters,
5657 * but independently of the other set dimensions.
5659 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5662 return map_dim_opt(set, pos, max);
5665 /* Compute the maximum of the given set dimension as a function of the
5666 * parameters, but independently of the other set dimensions.
5668 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5670 return set_dim_opt(set, pos, 1);
5673 /* Compute the minimum of the given set dimension as a function of the
5674 * parameters, but independently of the other set dimensions.
5676 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5678 return set_dim_opt(set, pos, 0);
5681 /* Apply a preimage specified by "mat" on the parameters of "bset".
5682 * bset is assumed to have only parameters and divs.
5684 static struct isl_basic_set *basic_set_parameter_preimage(
5685 struct isl_basic_set *bset, struct isl_mat *mat)
5692 bset->dim = isl_space_cow(bset->dim);
5696 nparam = isl_basic_set_dim(bset, isl_dim_param);
5698 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5700 bset->dim->nparam = 0;
5701 bset->dim->n_out = nparam;
5702 bset = isl_basic_set_preimage(bset, mat);
5704 bset->dim->nparam = bset->dim->n_out;
5705 bset->dim->n_out = 0;
5710 isl_basic_set_free(bset);
5714 /* Apply a preimage specified by "mat" on the parameters of "set".
5715 * set is assumed to have only parameters and divs.
5717 static struct isl_set *set_parameter_preimage(
5718 struct isl_set *set, struct isl_mat *mat)
5720 isl_space *dim = NULL;
5726 dim = isl_space_copy(set->dim);
5727 dim = isl_space_cow(dim);
5731 nparam = isl_set_dim(set, isl_dim_param);
5733 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5736 dim->n_out = nparam;
5737 isl_set_reset_space(set, dim);
5738 set = isl_set_preimage(set, mat);
5741 dim = isl_space_copy(set->dim);
5742 dim = isl_space_cow(dim);
5745 dim->nparam = dim->n_out;
5747 isl_set_reset_space(set, dim);
5750 isl_space_free(dim);
5757 /* Intersect the basic set "bset" with the affine space specified by the
5758 * equalities in "eq".
5760 static struct isl_basic_set *basic_set_append_equalities(
5761 struct isl_basic_set *bset, struct isl_mat *eq)
5769 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5774 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5775 for (i = 0; i < eq->n_row; ++i) {
5776 k = isl_basic_set_alloc_equality(bset);
5779 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5780 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5784 bset = isl_basic_set_gauss(bset, NULL);
5785 bset = isl_basic_set_finalize(bset);
5790 isl_basic_set_free(bset);
5794 /* Intersect the set "set" with the affine space specified by the
5795 * equalities in "eq".
5797 static struct isl_set *set_append_equalities(struct isl_set *set,
5805 for (i = 0; i < set->n; ++i) {
5806 set->p[i] = basic_set_append_equalities(set->p[i],
5819 /* Project the given basic set onto its parameter domain, possibly introducing
5820 * new, explicit, existential variables in the constraints.
5821 * The input has parameters and (possibly implicit) existential variables.
5822 * The output has the same parameters, but only
5823 * explicit existentially quantified variables.
5825 * The actual projection is performed by pip, but pip doesn't seem
5826 * to like equalities very much, so we first remove the equalities
5827 * among the parameters by performing a variable compression on
5828 * the parameters. Afterward, an inverse transformation is performed
5829 * and the equalities among the parameters are inserted back in.
5831 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5835 struct isl_mat *T, *T2;
5836 struct isl_set *set;
5837 unsigned nparam, n_div;
5839 bset = isl_basic_set_cow(bset);
5843 if (bset->n_eq == 0)
5844 return isl_basic_set_lexmin(bset);
5846 isl_basic_set_gauss(bset, NULL);
5848 nparam = isl_basic_set_dim(bset, isl_dim_param);
5849 n_div = isl_basic_set_dim(bset, isl_dim_div);
5851 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5852 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5855 if (i == bset->n_eq)
5856 return isl_basic_set_lexmin(bset);
5858 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5860 eq = isl_mat_cow(eq);
5861 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5862 if (T && T->n_col == 0) {
5866 bset = isl_basic_set_set_to_empty(bset);
5867 return isl_set_from_basic_set(bset);
5869 bset = basic_set_parameter_preimage(bset, T);
5871 set = isl_basic_set_lexmin(bset);
5872 set = set_parameter_preimage(set, T2);
5873 set = set_append_equalities(set, eq);
5877 /* Compute an explicit representation for all the existentially
5878 * quantified variables.
5879 * The input and output dimensions are first turned into parameters.
5880 * compute_divs then returns a map with the same parameters and
5881 * no input or output dimensions and the dimension specification
5882 * is reset to that of the input.
5884 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5886 struct isl_basic_set *bset;
5887 struct isl_set *set;
5888 struct isl_map *map;
5889 isl_space *dim, *orig_dim = NULL;
5894 bmap = isl_basic_map_cow(bmap);
5898 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5899 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5900 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5901 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5905 orig_dim = bmap->dim;
5907 bset = (struct isl_basic_set *)bmap;
5909 set = parameter_compute_divs(bset);
5910 map = (struct isl_map *)set;
5911 map = isl_map_reset_space(map, orig_dim);
5915 isl_basic_map_free(bmap);
5919 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5927 off = isl_space_dim(bmap->dim, isl_dim_all);
5928 for (i = 0; i < bmap->n_div; ++i) {
5929 if (isl_int_is_zero(bmap->div[i][0]))
5931 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5937 static int map_divs_known(__isl_keep isl_map *map)
5944 for (i = 0; i < map->n; ++i) {
5945 int known = isl_basic_map_divs_known(map->p[i]);
5953 /* If bmap contains any unknown divs, then compute explicit
5954 * expressions for them. However, this computation may be
5955 * quite expensive, so first try to remove divs that aren't
5958 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5961 struct isl_map *map;
5963 known = isl_basic_map_divs_known(bmap);
5967 return isl_map_from_basic_map(bmap);
5969 bmap = isl_basic_map_drop_redundant_divs(bmap);
5971 known = isl_basic_map_divs_known(bmap);
5975 return isl_map_from_basic_map(bmap);
5977 map = compute_divs(bmap);
5980 isl_basic_map_free(bmap);
5984 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5988 struct isl_map *res;
5995 known = map_divs_known(map);
6003 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6004 for (i = 1 ; i < map->n; ++i) {
6006 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6007 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6008 res = isl_map_union_disjoint(res, r2);
6010 res = isl_map_union(res, r2);
6017 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6019 return (struct isl_set *)
6020 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6023 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6025 return (struct isl_set *)
6026 isl_map_compute_divs((struct isl_map *)set);
6029 struct isl_set *isl_map_domain(struct isl_map *map)
6032 struct isl_set *set;
6037 map = isl_map_cow(map);
6041 set = (struct isl_set *)map;
6042 set->dim = isl_space_domain(set->dim);
6045 for (i = 0; i < map->n; ++i) {
6046 set->p[i] = isl_basic_map_domain(map->p[i]);
6050 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6051 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6058 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6059 __isl_take isl_map *map2)
6063 struct isl_map *map = NULL;
6077 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6079 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6080 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6081 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6083 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6084 map1->n + map2->n, flags);
6087 for (i = 0; i < map1->n; ++i) {
6088 map = isl_map_add_basic_map(map,
6089 isl_basic_map_copy(map1->p[i]));
6093 for (i = 0; i < map2->n; ++i) {
6094 map = isl_map_add_basic_map(map,
6095 isl_basic_map_copy(map2->p[i]));
6109 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6110 __isl_take isl_map *map2)
6112 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6115 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6117 map1 = isl_map_union_disjoint(map1, map2);
6121 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6125 struct isl_set *isl_set_union_disjoint(
6126 struct isl_set *set1, struct isl_set *set2)
6128 return (struct isl_set *)
6129 isl_map_union_disjoint(
6130 (struct isl_map *)set1, (struct isl_map *)set2);
6133 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6135 return (struct isl_set *)
6136 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6139 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6140 __isl_take isl_set *set)
6143 struct isl_map *result;
6149 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6150 isl_die(set->ctx, isl_error_invalid,
6151 "parameters don't match", goto error);
6153 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6154 !isl_map_compatible_range(map, set))
6155 isl_die(set->ctx, isl_error_invalid,
6156 "incompatible spaces", goto error);
6158 if (isl_set_plain_is_universe(set)) {
6163 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6164 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6165 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6167 result = isl_map_alloc_space(isl_space_copy(map->dim),
6168 map->n * set->n, flags);
6171 for (i = 0; i < map->n; ++i)
6172 for (j = 0; j < set->n; ++j) {
6173 result = isl_map_add_basic_map(result,
6174 isl_basic_map_intersect_range(
6175 isl_basic_map_copy(map->p[i]),
6176 isl_basic_set_copy(set->p[j])));
6189 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6190 __isl_take isl_set *set)
6192 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6195 struct isl_map *isl_map_intersect_domain(
6196 struct isl_map *map, struct isl_set *set)
6198 return isl_map_reverse(
6199 isl_map_intersect_range(isl_map_reverse(map), set));
6202 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6203 __isl_take isl_map *map2)
6207 map1 = isl_map_reverse(map1);
6208 map1 = isl_map_apply_range(map1, map2);
6209 return isl_map_reverse(map1);
6216 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6217 __isl_take isl_map *map2)
6219 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6222 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6223 __isl_take isl_map *map2)
6225 isl_space *dim_result;
6226 struct isl_map *result;
6232 dim_result = isl_space_join(isl_space_copy(map1->dim),
6233 isl_space_copy(map2->dim));
6235 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6238 for (i = 0; i < map1->n; ++i)
6239 for (j = 0; j < map2->n; ++j) {
6240 result = isl_map_add_basic_map(result,
6241 isl_basic_map_apply_range(
6242 isl_basic_map_copy(map1->p[i]),
6243 isl_basic_map_copy(map2->p[j])));
6249 if (result && result->n <= 1)
6250 ISL_F_SET(result, ISL_MAP_DISJOINT);
6258 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6259 __isl_take isl_map *map2)
6261 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6265 * returns range - domain
6267 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6269 isl_space *dims, *target_dim;
6270 struct isl_basic_set *bset;
6277 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6278 bmap->dim, isl_dim_out),
6280 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6281 dim = isl_basic_map_n_in(bmap);
6282 nparam = isl_basic_map_n_param(bmap);
6283 bset = isl_basic_set_from_basic_map(bmap);
6284 bset = isl_basic_set_cow(bset);
6285 dims = isl_basic_set_get_space(bset);
6286 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6287 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6288 bset = isl_basic_set_swap_vars(bset, 2*dim);
6289 for (i = 0; i < dim; ++i) {
6290 int j = isl_basic_map_alloc_equality(
6291 (struct isl_basic_map *)bset);
6294 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6295 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6296 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6297 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6299 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6300 bset = isl_basic_set_reset_space(bset, target_dim);
6303 isl_basic_map_free(bmap);
6308 * returns range - domain
6310 struct isl_set *isl_map_deltas(struct isl_map *map)
6314 struct isl_set *result;
6319 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6320 map->dim, isl_dim_out),
6322 dim = isl_map_get_space(map);
6323 dim = isl_space_domain(dim);
6324 result = isl_set_alloc_space(dim, map->n, 0);
6327 for (i = 0; i < map->n; ++i)
6328 result = isl_set_add_basic_set(result,
6329 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6338 * returns [domain -> range] -> range - domain
6340 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6341 __isl_take isl_basic_map *bmap)
6345 isl_basic_map *domain;
6349 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6350 isl_die(bmap->ctx, isl_error_invalid,
6351 "domain and range don't match", goto error);
6353 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6354 n = isl_basic_map_dim(bmap, isl_dim_in);
6356 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6357 domain = isl_basic_map_universe(dim);
6359 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6360 bmap = isl_basic_map_apply_range(bmap, domain);
6361 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6363 total = isl_basic_map_total_dim(bmap);
6365 for (i = 0; i < n; ++i) {
6366 k = isl_basic_map_alloc_equality(bmap);
6369 isl_seq_clr(bmap->eq[k], 1 + total);
6370 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6371 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6372 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6375 bmap = isl_basic_map_gauss(bmap, NULL);
6376 return isl_basic_map_finalize(bmap);
6378 isl_basic_map_free(bmap);
6383 * returns [domain -> range] -> range - domain
6385 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6388 isl_space *domain_dim;
6393 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6394 isl_die(map->ctx, isl_error_invalid,
6395 "domain and range don't match", goto error);
6397 map = isl_map_cow(map);
6401 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6402 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6403 map->dim = isl_space_join(map->dim, domain_dim);
6406 for (i = 0; i < map->n; ++i) {
6407 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6411 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6418 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6420 struct isl_basic_map *bmap;
6428 nparam = dims->nparam;
6430 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6434 for (i = 0; i < dim; ++i) {
6435 int j = isl_basic_map_alloc_equality(bmap);
6438 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6439 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6440 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6442 return isl_basic_map_finalize(bmap);
6444 isl_basic_map_free(bmap);
6448 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6452 if (dim->n_in != dim->n_out)
6453 isl_die(dim->ctx, isl_error_invalid,
6454 "number of input and output dimensions needs to be "
6455 "the same", goto error);
6456 return basic_map_identity(dim);
6458 isl_space_free(dim);
6462 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6464 if (!model || !model->dim)
6466 return isl_basic_map_identity(isl_space_copy(model->dim));
6469 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6471 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6474 struct isl_map *isl_map_identity_like(struct isl_map *model)
6476 if (!model || !model->dim)
6478 return isl_map_identity(isl_space_copy(model->dim));
6481 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6483 if (!model || !model->dim)
6485 return isl_map_identity(isl_space_copy(model->dim));
6488 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6490 isl_space *dim = isl_set_get_space(set);
6492 id = isl_map_identity(isl_space_map_from_set(dim));
6493 return isl_map_intersect_range(id, set);
6496 /* Construct a basic set with all set dimensions having only non-negative
6499 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6500 __isl_take isl_space *space)
6505 struct isl_basic_set *bset;
6509 nparam = space->nparam;
6511 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6514 for (i = 0; i < dim; ++i) {
6515 int k = isl_basic_set_alloc_inequality(bset);
6518 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6519 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6523 isl_basic_set_free(bset);
6527 /* Construct the half-space x_pos >= 0.
6529 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6533 isl_basic_set *nonneg;
6535 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6536 k = isl_basic_set_alloc_inequality(nonneg);
6539 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6540 isl_int_set_si(nonneg->ineq[k][pos], 1);
6542 return isl_basic_set_finalize(nonneg);
6544 isl_basic_set_free(nonneg);
6548 /* Construct the half-space x_pos <= -1.
6550 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6555 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6556 k = isl_basic_set_alloc_inequality(neg);
6559 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6560 isl_int_set_si(neg->ineq[k][0], -1);
6561 isl_int_set_si(neg->ineq[k][pos], -1);
6563 return isl_basic_set_finalize(neg);
6565 isl_basic_set_free(neg);
6569 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6570 enum isl_dim_type type, unsigned first, unsigned n)
6573 isl_basic_set *nonneg;
6581 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6583 for (i = 0; i < n; ++i) {
6584 nonneg = nonneg_halfspace(isl_set_get_space(set),
6585 pos(set->dim, type) + first + i);
6586 neg = neg_halfspace(isl_set_get_space(set),
6587 pos(set->dim, type) + first + i);
6589 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6598 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6599 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6606 if (isl_set_plain_is_empty(set)) {
6611 return fn(set, signs, user);
6614 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6616 half = isl_set_intersect(half, isl_set_copy(set));
6617 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6621 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6623 half = isl_set_intersect(half, set);
6624 return foreach_orthant(half, signs, first + 1, len, fn, user);
6630 /* Call "fn" on the intersections of "set" with each of the orthants
6631 * (except for obviously empty intersections). The orthant is identified
6632 * by the signs array, with each entry having value 1 or -1 according
6633 * to the sign of the corresponding variable.
6635 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6636 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6646 if (isl_set_plain_is_empty(set))
6649 nparam = isl_set_dim(set, isl_dim_param);
6650 nvar = isl_set_dim(set, isl_dim_set);
6652 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6654 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6662 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6664 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6667 int isl_basic_map_is_subset(
6668 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6671 struct isl_map *map1;
6672 struct isl_map *map2;
6674 if (!bmap1 || !bmap2)
6677 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6678 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6680 is_subset = isl_map_is_subset(map1, map2);
6688 int isl_basic_map_is_equal(
6689 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6693 if (!bmap1 || !bmap2)
6695 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6698 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6702 int isl_basic_set_is_equal(
6703 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6705 return isl_basic_map_is_equal(
6706 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6709 int isl_map_is_empty(struct isl_map *map)
6716 for (i = 0; i < map->n; ++i) {
6717 is_empty = isl_basic_map_is_empty(map->p[i]);
6726 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6728 return map ? map->n == 0 : -1;
6731 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6733 return isl_map_plain_is_empty(map);
6736 int isl_set_plain_is_empty(struct isl_set *set)
6738 return set ? set->n == 0 : -1;
6741 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6743 return isl_set_plain_is_empty(set);
6746 int isl_set_is_empty(struct isl_set *set)
6748 return isl_map_is_empty((struct isl_map *)set);
6751 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6756 return isl_space_is_equal(map1->dim, map2->dim);
6759 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6764 return isl_space_is_equal(set1->dim, set2->dim);
6767 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6773 is_subset = isl_map_is_subset(map1, map2);
6776 is_subset = isl_map_is_subset(map2, map1);
6780 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6782 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
6785 int isl_basic_map_is_strict_subset(
6786 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6790 if (!bmap1 || !bmap2)
6792 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6795 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6796 if (is_subset == -1)
6801 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6807 is_subset = isl_map_is_subset(map1, map2);
6810 is_subset = isl_map_is_subset(map2, map1);
6811 if (is_subset == -1)
6816 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6818 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6821 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6825 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6828 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6832 return bset->n_eq == 0 && bset->n_ineq == 0;
6835 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6842 for (i = 0; i < map->n; ++i) {
6843 int r = isl_basic_map_is_universe(map->p[i]);
6851 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6853 return isl_map_plain_is_universe((isl_map *) set);
6856 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6858 return isl_set_plain_is_universe(set);
6861 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6863 struct isl_basic_set *bset = NULL;
6864 struct isl_vec *sample = NULL;
6871 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6874 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6875 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6876 copy = isl_basic_map_remove_redundancies(copy);
6877 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6878 isl_basic_map_free(copy);
6882 total = 1 + isl_basic_map_total_dim(bmap);
6883 if (bmap->sample && bmap->sample->size == total) {
6884 int contains = isl_basic_map_contains(bmap, bmap->sample);
6890 isl_vec_free(bmap->sample);
6891 bmap->sample = NULL;
6892 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6895 sample = isl_basic_set_sample_vec(bset);
6898 empty = sample->size == 0;
6899 isl_vec_free(bmap->sample);
6900 bmap->sample = sample;
6902 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6907 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6911 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6914 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6916 return isl_basic_map_plain_is_empty(bmap);
6919 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6923 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6926 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6928 return isl_basic_set_plain_is_empty(bset);
6931 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6933 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6936 struct isl_map *isl_basic_map_union(
6937 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6939 struct isl_map *map;
6940 if (!bmap1 || !bmap2)
6943 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
6945 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
6948 map = isl_map_add_basic_map(map, bmap1);
6949 map = isl_map_add_basic_map(map, bmap2);
6952 isl_basic_map_free(bmap1);
6953 isl_basic_map_free(bmap2);
6957 struct isl_set *isl_basic_set_union(
6958 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6960 return (struct isl_set *)isl_basic_map_union(
6961 (struct isl_basic_map *)bset1,
6962 (struct isl_basic_map *)bset2);
6965 /* Order divs such that any div only depends on previous divs */
6966 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6974 off = isl_space_dim(bmap->dim, isl_dim_all);
6976 for (i = 0; i < bmap->n_div; ++i) {
6978 if (isl_int_is_zero(bmap->div[i][0]))
6980 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6984 isl_basic_map_swap_div(bmap, i, i + pos);
6990 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6992 return (struct isl_basic_set *)
6993 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6996 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7003 for (i = 0; i < map->n; ++i) {
7004 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7015 /* Apply the expansion computed by isl_merge_divs.
7016 * The expansion itself is given by "exp" while the resulting
7017 * list of divs is given by "div".
7019 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7020 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7025 bset = isl_basic_set_cow(bset);
7029 if (div->n_row < bset->n_div)
7030 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7031 "not an expansion", goto error);
7033 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7034 div->n_row - bset->n_div, 0,
7035 2 * (div->n_row - bset->n_div));
7037 n_div = bset->n_div;
7038 for (i = n_div; i < div->n_row; ++i)
7039 if (isl_basic_set_alloc_div(bset) < 0)
7043 for (i = div->n_row - 1; i >= 0; --i) {
7044 if (j >= 0 && exp[j] == i) {
7046 isl_basic_map_swap_div(bset, i, j);
7049 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7050 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7058 isl_basic_set_free(bset);
7063 /* Look for a div in dst that corresponds to the div "div" in src.
7064 * The divs before "div" in src and dst are assumed to be the same.
7066 * Returns -1 if no corresponding div was found and the position
7067 * of the corresponding div in dst otherwise.
7069 static int find_div(struct isl_basic_map *dst,
7070 struct isl_basic_map *src, unsigned div)
7074 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7076 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7077 for (i = div; i < dst->n_div; ++i)
7078 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7079 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7080 dst->n_div - div) == -1)
7085 struct isl_basic_map *isl_basic_map_align_divs(
7086 struct isl_basic_map *dst, struct isl_basic_map *src)
7089 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7094 if (src->n_div == 0)
7097 for (i = 0; i < src->n_div; ++i)
7098 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7100 src = isl_basic_map_order_divs(src);
7101 dst = isl_basic_map_cow(dst);
7102 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7103 src->n_div, 0, 2 * src->n_div);
7106 for (i = 0; i < src->n_div; ++i) {
7107 int j = find_div(dst, src, i);
7109 j = isl_basic_map_alloc_div(dst);
7112 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7113 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7114 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7118 isl_basic_map_swap_div(dst, i, j);
7122 isl_basic_map_free(dst);
7126 struct isl_basic_set *isl_basic_set_align_divs(
7127 struct isl_basic_set *dst, struct isl_basic_set *src)
7129 return (struct isl_basic_set *)isl_basic_map_align_divs(
7130 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7133 struct isl_map *isl_map_align_divs(struct isl_map *map)
7141 map = isl_map_compute_divs(map);
7142 map = isl_map_cow(map);
7146 for (i = 1; i < map->n; ++i)
7147 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7148 for (i = 1; i < map->n; ++i)
7149 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7151 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7155 struct isl_set *isl_set_align_divs(struct isl_set *set)
7157 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7160 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7161 __isl_take isl_map *map)
7165 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7166 map = isl_map_intersect_domain(map, set);
7167 set = isl_map_range(map);
7175 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7176 __isl_take isl_map *map)
7178 return isl_map_align_params_map_map_and(set, map, &set_apply);
7181 /* There is no need to cow as removing empty parts doesn't change
7182 * the meaning of the set.
7184 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7191 for (i = map->n-1; i >= 0; --i) {
7192 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
7194 isl_basic_map_free(map->p[i]);
7195 if (i != map->n-1) {
7196 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7197 map->p[i] = map->p[map->n-1];
7205 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7207 return (struct isl_set *)
7208 isl_map_remove_empty_parts((struct isl_map *)set);
7211 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7213 struct isl_basic_map *bmap;
7214 if (!map || map->n == 0)
7216 bmap = map->p[map->n-1];
7217 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7218 return isl_basic_map_copy(bmap);
7221 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7223 return (struct isl_basic_set *)
7224 isl_map_copy_basic_map((struct isl_map *)set);
7227 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7228 __isl_keep isl_basic_map *bmap)
7234 for (i = map->n-1; i >= 0; --i) {
7235 if (map->p[i] != bmap)
7237 map = isl_map_cow(map);
7240 isl_basic_map_free(map->p[i]);
7241 if (i != map->n-1) {
7242 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7243 map->p[i] = map->p[map->n-1];
7254 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7255 struct isl_basic_set *bset)
7257 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7258 (struct isl_basic_map *)bset);
7261 /* Given two basic sets bset1 and bset2, compute the maximal difference
7262 * between the values of dimension pos in bset1 and those in bset2
7263 * for any common value of the parameters and dimensions preceding pos.
7265 static enum isl_lp_result basic_set_maximal_difference_at(
7266 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7267 int pos, isl_int *opt)
7270 struct isl_basic_map *bmap1 = NULL;
7271 struct isl_basic_map *bmap2 = NULL;
7272 struct isl_ctx *ctx;
7273 struct isl_vec *obj;
7276 unsigned dim1, dim2;
7277 enum isl_lp_result res;
7279 if (!bset1 || !bset2)
7280 return isl_lp_error;
7282 nparam = isl_basic_set_n_param(bset1);
7283 dim1 = isl_basic_set_n_dim(bset1);
7284 dim2 = isl_basic_set_n_dim(bset2);
7285 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7286 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7287 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7288 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7289 if (!bmap1 || !bmap2)
7291 bmap1 = isl_basic_map_cow(bmap1);
7292 bmap1 = isl_basic_map_extend(bmap1, nparam,
7293 pos, (dim1 - pos) + (dim2 - pos),
7294 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7295 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7298 total = isl_basic_map_total_dim(bmap1);
7300 obj = isl_vec_alloc(ctx, 1 + total);
7301 isl_seq_clr(obj->block.data, 1 + total);
7302 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7303 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7306 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7308 isl_basic_map_free(bmap1);
7312 isl_basic_map_free(bmap1);
7313 isl_basic_map_free(bmap2);
7314 return isl_lp_error;
7317 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7318 * for any common value of the parameters and dimensions preceding pos
7319 * in both basic sets, the values of dimension pos in bset1 are
7320 * smaller or larger than those in bset2.
7323 * 1 if bset1 follows bset2
7324 * -1 if bset1 precedes bset2
7325 * 0 if bset1 and bset2 are incomparable
7326 * -2 if some error occurred.
7328 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7329 struct isl_basic_set *bset2, int pos)
7332 enum isl_lp_result res;
7337 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7339 if (res == isl_lp_empty)
7341 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7342 res == isl_lp_unbounded)
7344 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7353 /* Given two basic sets bset1 and bset2, check whether
7354 * for any common value of the parameters and dimensions preceding pos
7355 * there is a value of dimension pos in bset1 that is larger
7356 * than a value of the same dimension in bset2.
7359 * 1 if there exists such a pair
7360 * 0 if there is no such pair, but there is a pair of equal values
7362 * -2 if some error occurred.
7364 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7365 __isl_keep isl_basic_set *bset2, int pos)
7368 enum isl_lp_result res;
7373 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7375 if (res == isl_lp_empty)
7377 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7378 res == isl_lp_unbounded)
7380 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7382 else if (res == isl_lp_ok)
7391 /* Given two sets set1 and set2, check whether
7392 * for any common value of the parameters and dimensions preceding pos
7393 * there is a value of dimension pos in set1 that is larger
7394 * than a value of the same dimension in set2.
7397 * 1 if there exists such a pair
7398 * 0 if there is no such pair, but there is a pair of equal values
7400 * -2 if some error occurred.
7402 int isl_set_follows_at(__isl_keep isl_set *set1,
7403 __isl_keep isl_set *set2, int pos)
7411 for (i = 0; i < set1->n; ++i)
7412 for (j = 0; j < set2->n; ++j) {
7414 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7415 if (f == 1 || f == -2)
7424 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7425 unsigned pos, isl_int *val)
7433 total = isl_basic_map_total_dim(bmap);
7434 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7435 for (; d+1 > pos; --d)
7436 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7440 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7442 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7444 if (!isl_int_is_one(bmap->eq[i][1+d]))
7447 isl_int_neg(*val, bmap->eq[i][0]);
7453 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7454 unsigned pos, isl_int *val)
7466 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7469 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7470 for (i = 1; fixed == 1 && i < map->n; ++i) {
7471 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7472 if (fixed == 1 && isl_int_ne(tmp, v))
7476 isl_int_set(*val, v);
7482 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7483 unsigned pos, isl_int *val)
7485 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7489 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7492 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7495 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7496 enum isl_dim_type type, unsigned pos, isl_int *val)
7498 if (pos >= isl_basic_map_dim(bmap, type))
7500 return isl_basic_map_plain_has_fixed_var(bmap,
7501 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7504 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7505 enum isl_dim_type type, unsigned pos, isl_int *val)
7507 if (pos >= isl_map_dim(map, type))
7509 return isl_map_plain_has_fixed_var(map,
7510 map_offset(map, type) - 1 + pos, val);
7513 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7514 enum isl_dim_type type, unsigned pos, isl_int *val)
7516 return isl_map_plain_is_fixed(set, type, pos, val);
7519 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7520 enum isl_dim_type type, unsigned pos, isl_int *val)
7522 return isl_map_plain_is_fixed(map, type, pos, val);
7525 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7526 * then return this fixed value in *val.
7528 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7529 unsigned dim, isl_int *val)
7531 return isl_basic_set_plain_has_fixed_var(bset,
7532 isl_basic_set_n_param(bset) + dim, val);
7535 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7536 * then return this fixed value in *val.
7538 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7539 unsigned dim, isl_int *val)
7541 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7544 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7545 unsigned dim, isl_int *val)
7547 return isl_set_plain_dim_is_fixed(set, dim, val);
7550 /* Check if input variable in has fixed value and if so and if val is not NULL,
7551 * then return this fixed value in *val.
7553 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7554 unsigned in, isl_int *val)
7556 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7559 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7560 * and if val is not NULL, then return this lower bound in *val.
7562 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7563 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7565 int i, i_eq = -1, i_ineq = -1;
7572 total = isl_basic_set_total_dim(bset);
7573 nparam = isl_basic_set_n_param(bset);
7574 for (i = 0; i < bset->n_eq; ++i) {
7575 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7581 for (i = 0; i < bset->n_ineq; ++i) {
7582 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7584 if (i_eq != -1 || i_ineq != -1)
7588 if (i_eq == -1 && i_ineq == -1)
7590 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7591 /* The coefficient should always be one due to normalization. */
7592 if (!isl_int_is_one(c[1+nparam+dim]))
7594 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7596 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7597 total - nparam - dim - 1) != -1)
7600 isl_int_neg(*val, c[0]);
7604 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7605 unsigned dim, isl_int *val)
7617 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7621 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7623 for (i = 1; fixed == 1 && i < set->n; ++i) {
7624 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7626 if (fixed == 1 && isl_int_ne(tmp, v))
7630 isl_int_set(*val, v);
7641 /* uset_gist depends on constraints without existentially quantified
7642 * variables sorting first.
7644 static int qsort_constraint_cmp(const void *p1, const void *p2)
7646 const struct constraint *c1 = (const struct constraint *)p1;
7647 const struct constraint *c2 = (const struct constraint *)p2;
7649 unsigned size = isl_min(c1->size, c2->size);
7651 l1 = isl_seq_last_non_zero(c1->c, size);
7652 l2 = isl_seq_last_non_zero(c2->c, size);
7657 return isl_seq_cmp(c1->c, c2->c, size);
7660 static struct isl_basic_map *isl_basic_map_sort_constraints(
7661 struct isl_basic_map *bmap)
7664 struct constraint *c;
7669 total = isl_basic_map_total_dim(bmap);
7670 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7673 for (i = 0; i < bmap->n_ineq; ++i) {
7675 c[i].c = bmap->ineq[i];
7677 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7678 for (i = 0; i < bmap->n_ineq; ++i)
7679 bmap->ineq[i] = c[i].c;
7683 isl_basic_map_free(bmap);
7687 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7688 __isl_take isl_basic_set *bset)
7690 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7691 (struct isl_basic_map *)bset);
7694 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7698 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7700 bmap = isl_basic_map_remove_redundancies(bmap);
7701 bmap = isl_basic_map_sort_constraints(bmap);
7702 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7706 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7708 return (struct isl_basic_set *)isl_basic_map_normalize(
7709 (struct isl_basic_map *)bset);
7712 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7713 const __isl_keep isl_basic_map *bmap2)
7720 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7721 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7722 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7723 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7724 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7725 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7726 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7727 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7728 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7729 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7730 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7732 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7734 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7736 if (bmap1->n_eq != bmap2->n_eq)
7737 return bmap1->n_eq - bmap2->n_eq;
7738 if (bmap1->n_ineq != bmap2->n_ineq)
7739 return bmap1->n_ineq - bmap2->n_ineq;
7740 if (bmap1->n_div != bmap2->n_div)
7741 return bmap1->n_div - bmap2->n_div;
7742 total = isl_basic_map_total_dim(bmap1);
7743 for (i = 0; i < bmap1->n_eq; ++i) {
7744 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7748 for (i = 0; i < bmap1->n_ineq; ++i) {
7749 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7753 for (i = 0; i < bmap1->n_div; ++i) {
7754 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7761 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
7762 const __isl_keep isl_basic_set *bset2)
7764 return isl_basic_map_plain_cmp(bset1, bset2);
7767 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
7768 const __isl_keep isl_set *set2)
7774 if (set1->n != set2->n)
7775 return set1->n - set2->n;
7777 for (i = 0; i < set1->n; ++i) {
7778 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
7786 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7787 __isl_keep isl_basic_map *bmap2)
7789 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7792 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7793 __isl_keep isl_basic_set *bset2)
7795 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7796 (isl_basic_map *)bset2);
7799 static int qsort_bmap_cmp(const void *p1, const void *p2)
7801 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7802 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7804 return isl_basic_map_plain_cmp(bmap1, bmap2);
7807 /* We normalize in place, but if anything goes wrong we need
7808 * to return NULL, so we need to make sure we don't change the
7809 * meaning of any possible other copies of map.
7811 struct isl_map *isl_map_normalize(struct isl_map *map)
7814 struct isl_basic_map *bmap;
7818 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7820 for (i = 0; i < map->n; ++i) {
7821 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7824 isl_basic_map_free(map->p[i]);
7827 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7828 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7829 map = isl_map_remove_empty_parts(map);
7832 for (i = map->n - 1; i >= 1; --i) {
7833 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7835 isl_basic_map_free(map->p[i-1]);
7836 for (j = i; j < map->n; ++j)
7837 map->p[j-1] = map->p[j];
7847 struct isl_set *isl_set_normalize(struct isl_set *set)
7849 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7852 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7862 if (!isl_space_is_equal(map1->dim, map2->dim))
7865 map1 = isl_map_copy(map1);
7866 map2 = isl_map_copy(map2);
7867 map1 = isl_map_normalize(map1);
7868 map2 = isl_map_normalize(map2);
7871 equal = map1->n == map2->n;
7872 for (i = 0; equal && i < map1->n; ++i) {
7873 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7886 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7888 return isl_map_plain_is_equal(map1, map2);
7891 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7893 return isl_map_plain_is_equal((struct isl_map *)set1,
7894 (struct isl_map *)set2);
7897 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7899 return isl_set_plain_is_equal(set1, set2);
7902 /* Return an interval that ranges from min to max (inclusive)
7904 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7905 isl_int min, isl_int max)
7908 struct isl_basic_set *bset = NULL;
7910 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7914 k = isl_basic_set_alloc_inequality(bset);
7917 isl_int_set_si(bset->ineq[k][1], 1);
7918 isl_int_neg(bset->ineq[k][0], min);
7920 k = isl_basic_set_alloc_inequality(bset);
7923 isl_int_set_si(bset->ineq[k][1], -1);
7924 isl_int_set(bset->ineq[k][0], max);
7928 isl_basic_set_free(bset);
7932 /* Return the Cartesian product of the basic sets in list (in the given order).
7934 __isl_give isl_basic_set *isl_basic_set_list_product(
7935 __isl_take struct isl_basic_set_list *list)
7943 struct isl_basic_set *product = NULL;
7947 isl_assert(list->ctx, list->n > 0, goto error);
7948 isl_assert(list->ctx, list->p[0], goto error);
7949 nparam = isl_basic_set_n_param(list->p[0]);
7950 dim = isl_basic_set_n_dim(list->p[0]);
7951 extra = list->p[0]->n_div;
7952 n_eq = list->p[0]->n_eq;
7953 n_ineq = list->p[0]->n_ineq;
7954 for (i = 1; i < list->n; ++i) {
7955 isl_assert(list->ctx, list->p[i], goto error);
7956 isl_assert(list->ctx,
7957 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7958 dim += isl_basic_set_n_dim(list->p[i]);
7959 extra += list->p[i]->n_div;
7960 n_eq += list->p[i]->n_eq;
7961 n_ineq += list->p[i]->n_ineq;
7963 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7968 for (i = 0; i < list->n; ++i) {
7969 isl_basic_set_add_constraints(product,
7970 isl_basic_set_copy(list->p[i]), dim);
7971 dim += isl_basic_set_n_dim(list->p[i]);
7973 isl_basic_set_list_free(list);
7976 isl_basic_set_free(product);
7977 isl_basic_set_list_free(list);
7981 struct isl_basic_map *isl_basic_map_product(
7982 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7984 isl_space *dim_result = NULL;
7985 struct isl_basic_map *bmap;
7986 unsigned in1, in2, out1, out2, nparam, total, pos;
7987 struct isl_dim_map *dim_map1, *dim_map2;
7989 if (!bmap1 || !bmap2)
7992 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
7993 bmap2->dim, isl_dim_param), goto error);
7994 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
7995 isl_space_copy(bmap2->dim));
7997 in1 = isl_basic_map_n_in(bmap1);
7998 in2 = isl_basic_map_n_in(bmap2);
7999 out1 = isl_basic_map_n_out(bmap1);
8000 out2 = isl_basic_map_n_out(bmap2);
8001 nparam = isl_basic_map_n_param(bmap1);
8003 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8004 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8005 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8006 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8007 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8008 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8009 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8010 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8011 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8012 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8013 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8015 bmap = isl_basic_map_alloc_space(dim_result,
8016 bmap1->n_div + bmap2->n_div,
8017 bmap1->n_eq + bmap2->n_eq,
8018 bmap1->n_ineq + bmap2->n_ineq);
8019 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8020 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8021 bmap = isl_basic_map_simplify(bmap);
8022 return isl_basic_map_finalize(bmap);
8024 isl_basic_map_free(bmap1);
8025 isl_basic_map_free(bmap2);
8029 __isl_give isl_basic_map *isl_basic_map_flat_product(
8030 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8032 isl_basic_map *prod;
8034 prod = isl_basic_map_product(bmap1, bmap2);
8035 prod = isl_basic_map_flatten(prod);
8039 __isl_give isl_basic_set *isl_basic_set_flat_product(
8040 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8042 return isl_basic_map_flat_range_product(bset1, bset2);
8045 __isl_give isl_basic_map *isl_basic_map_domain_product(
8046 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8048 isl_space *space_result = NULL;
8049 isl_basic_map *bmap;
8050 unsigned in1, in2, out, nparam, total, pos;
8051 struct isl_dim_map *dim_map1, *dim_map2;
8053 if (!bmap1 || !bmap2)
8056 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8057 isl_space_copy(bmap2->dim));
8059 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8060 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8061 out = isl_basic_map_dim(bmap1, isl_dim_out);
8062 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8064 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8065 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8066 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8067 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8068 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8069 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8070 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8071 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8072 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8073 isl_dim_map_div(dim_map1, bmap1, pos += out);
8074 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8076 bmap = isl_basic_map_alloc_space(space_result,
8077 bmap1->n_div + bmap2->n_div,
8078 bmap1->n_eq + bmap2->n_eq,
8079 bmap1->n_ineq + bmap2->n_ineq);
8080 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8081 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8082 bmap = isl_basic_map_simplify(bmap);
8083 return isl_basic_map_finalize(bmap);
8085 isl_basic_map_free(bmap1);
8086 isl_basic_map_free(bmap2);
8090 __isl_give isl_basic_map *isl_basic_map_range_product(
8091 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8093 isl_space *dim_result = NULL;
8094 isl_basic_map *bmap;
8095 unsigned in, out1, out2, nparam, total, pos;
8096 struct isl_dim_map *dim_map1, *dim_map2;
8098 if (!bmap1 || !bmap2)
8101 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8102 isl_space_copy(bmap2->dim));
8104 in = isl_basic_map_dim(bmap1, isl_dim_in);
8105 out1 = isl_basic_map_n_out(bmap1);
8106 out2 = isl_basic_map_n_out(bmap2);
8107 nparam = isl_basic_map_n_param(bmap1);
8109 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8110 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8111 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8112 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8113 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8114 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8115 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8116 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8117 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8118 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8119 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8121 bmap = isl_basic_map_alloc_space(dim_result,
8122 bmap1->n_div + bmap2->n_div,
8123 bmap1->n_eq + bmap2->n_eq,
8124 bmap1->n_ineq + bmap2->n_ineq);
8125 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8126 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8127 bmap = isl_basic_map_simplify(bmap);
8128 return isl_basic_map_finalize(bmap);
8130 isl_basic_map_free(bmap1);
8131 isl_basic_map_free(bmap2);
8135 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8136 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8138 isl_basic_map *prod;
8140 prod = isl_basic_map_range_product(bmap1, bmap2);
8141 prod = isl_basic_map_flatten_range(prod);
8145 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8146 __isl_take isl_map *map2,
8147 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8148 __isl_take isl_space *right),
8149 __isl_give isl_basic_map *(*basic_map_product)(
8150 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8153 struct isl_map *result;
8159 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8160 map2->dim, isl_dim_param), goto error);
8162 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8163 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8164 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8166 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8167 isl_space_copy(map2->dim)),
8168 map1->n * map2->n, flags);
8171 for (i = 0; i < map1->n; ++i)
8172 for (j = 0; j < map2->n; ++j) {
8173 struct isl_basic_map *part;
8174 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8175 isl_basic_map_copy(map2->p[j]));
8176 if (isl_basic_map_is_empty(part))
8177 isl_basic_map_free(part);
8179 result = isl_map_add_basic_map(result, part);
8192 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8194 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8195 __isl_take isl_map *map2)
8197 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8200 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8201 __isl_take isl_map *map2)
8203 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8206 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8208 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8209 __isl_take isl_map *map2)
8213 prod = isl_map_product(map1, map2);
8214 prod = isl_map_flatten(prod);
8218 /* Given two set A and B, construct its Cartesian product A x B.
8220 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8222 return isl_map_range_product(set1, set2);
8225 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8226 __isl_take isl_set *set2)
8228 return isl_map_flat_range_product(set1, set2);
8231 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8233 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8234 __isl_take isl_map *map2)
8236 return map_product(map1, map2, &isl_space_domain_product,
8237 &isl_basic_map_domain_product);
8240 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8242 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8243 __isl_take isl_map *map2)
8245 return map_product(map1, map2, &isl_space_range_product,
8246 &isl_basic_map_range_product);
8249 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8250 __isl_take isl_map *map2)
8252 return isl_map_align_params_map_map_and(map1, map2,
8253 &map_domain_product_aligned);
8256 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8257 __isl_take isl_map *map2)
8259 return isl_map_align_params_map_map_and(map1, map2,
8260 &map_range_product_aligned);
8263 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8265 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8266 __isl_take isl_map *map2)
8270 prod = isl_map_domain_product(map1, map2);
8271 prod = isl_map_flatten_domain(prod);
8275 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8277 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8278 __isl_take isl_map *map2)
8282 prod = isl_map_range_product(map1, map2);
8283 prod = isl_map_flatten_range(prod);
8287 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8290 uint32_t hash = isl_hash_init();
8295 bmap = isl_basic_map_copy(bmap);
8296 bmap = isl_basic_map_normalize(bmap);
8299 total = isl_basic_map_total_dim(bmap);
8300 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8301 for (i = 0; i < bmap->n_eq; ++i) {
8303 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8304 isl_hash_hash(hash, c_hash);
8306 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8307 for (i = 0; i < bmap->n_ineq; ++i) {
8309 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8310 isl_hash_hash(hash, c_hash);
8312 isl_hash_byte(hash, bmap->n_div & 0xFF);
8313 for (i = 0; i < bmap->n_div; ++i) {
8315 if (isl_int_is_zero(bmap->div[i][0]))
8317 isl_hash_byte(hash, i & 0xFF);
8318 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8319 isl_hash_hash(hash, c_hash);
8321 isl_basic_map_free(bmap);
8325 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8327 return isl_basic_map_get_hash((isl_basic_map *)bset);
8330 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8337 map = isl_map_copy(map);
8338 map = isl_map_normalize(map);
8342 hash = isl_hash_init();
8343 for (i = 0; i < map->n; ++i) {
8345 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8346 isl_hash_hash(hash, bmap_hash);
8354 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8356 return isl_map_get_hash((isl_map *)set);
8359 /* Check if the value for dimension dim is completely determined
8360 * by the values of the other parameters and variables.
8361 * That is, check if dimension dim is involved in an equality.
8363 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8370 nparam = isl_basic_set_n_param(bset);
8371 for (i = 0; i < bset->n_eq; ++i)
8372 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8377 /* Check if the value for dimension dim is completely determined
8378 * by the values of the other parameters and variables.
8379 * That is, check if dimension dim is involved in an equality
8380 * for each of the subsets.
8382 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8388 for (i = 0; i < set->n; ++i) {
8390 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8397 int isl_set_n_basic_set(__isl_keep isl_set *set)
8399 return set ? set->n : 0;
8402 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8403 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8410 for (i = 0; i < map->n; ++i)
8411 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8417 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8418 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8425 for (i = 0; i < set->n; ++i)
8426 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8432 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8439 bset = isl_basic_set_cow(bset);
8443 dim = isl_basic_set_get_space(bset);
8444 dim = isl_space_lift(dim, bset->n_div);
8447 isl_space_free(bset->dim);
8449 bset->extra -= bset->n_div;
8452 bset = isl_basic_set_finalize(bset);
8456 isl_basic_set_free(bset);
8460 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8466 set = isl_set_align_divs(set);
8471 set = isl_set_cow(set);
8475 n_div = set->p[0]->n_div;
8476 dim = isl_set_get_space(set);
8477 dim = isl_space_lift(dim, n_div);
8480 isl_space_free(set->dim);
8483 for (i = 0; i < set->n; ++i) {
8484 set->p[i] = isl_basic_set_lift(set->p[i]);
8495 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8498 struct isl_basic_map *bmap;
8505 set = isl_set_align_divs(set);
8510 dim = isl_set_get_space(set);
8511 if (set->n == 0 || set->p[0]->n_div == 0) {
8513 return isl_map_identity(isl_space_map_from_set(dim));
8516 n_div = set->p[0]->n_div;
8517 dim = isl_space_map_from_set(dim);
8518 n_param = isl_space_dim(dim, isl_dim_param);
8519 n_set = isl_space_dim(dim, isl_dim_in);
8520 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8521 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8522 for (i = 0; i < n_set; ++i)
8523 bmap = var_equal(bmap, i);
8525 total = n_param + n_set + n_set + n_div;
8526 for (i = 0; i < n_div; ++i) {
8527 k = isl_basic_map_alloc_inequality(bmap);
8530 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8531 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8532 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8533 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8534 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8535 set->p[0]->div[i][0]);
8537 l = isl_basic_map_alloc_inequality(bmap);
8540 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8541 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8542 set->p[0]->div[i][0]);
8543 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8547 bmap = isl_basic_map_simplify(bmap);
8548 bmap = isl_basic_map_finalize(bmap);
8549 return isl_map_from_basic_map(bmap);
8552 isl_basic_map_free(bmap);
8556 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8564 dim = isl_basic_set_total_dim(bset);
8565 size += bset->n_eq * (1 + dim);
8566 size += bset->n_ineq * (1 + dim);
8567 size += bset->n_div * (2 + dim);
8572 int isl_set_size(__isl_keep isl_set *set)
8580 for (i = 0; i < set->n; ++i)
8581 size += isl_basic_set_size(set->p[i]);
8586 /* Check if there is any lower bound (if lower == 0) and/or upper
8587 * bound (if upper == 0) on the specified dim.
8589 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8590 enum isl_dim_type type, unsigned pos, int lower, int upper)
8597 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8599 pos += isl_basic_map_offset(bmap, type);
8601 for (i = 0; i < bmap->n_div; ++i) {
8602 if (isl_int_is_zero(bmap->div[i][0]))
8604 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8608 for (i = 0; i < bmap->n_eq; ++i)
8609 if (!isl_int_is_zero(bmap->eq[i][pos]))
8612 for (i = 0; i < bmap->n_ineq; ++i) {
8613 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8620 return lower && upper;
8623 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8624 enum isl_dim_type type, unsigned pos)
8626 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8629 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8630 enum isl_dim_type type, unsigned pos)
8632 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8635 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8636 enum isl_dim_type type, unsigned pos)
8638 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8641 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8642 enum isl_dim_type type, unsigned pos)
8649 for (i = 0; i < map->n; ++i) {
8651 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8652 if (bounded < 0 || !bounded)
8659 /* Return 1 if the specified dim is involved in both an upper bound
8660 * and a lower bound.
8662 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8663 enum isl_dim_type type, unsigned pos)
8665 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8668 static int has_bound(__isl_keep isl_map *map,
8669 enum isl_dim_type type, unsigned pos,
8670 int (*fn)(__isl_keep isl_basic_map *bmap,
8671 enum isl_dim_type type, unsigned pos))
8678 for (i = 0; i < map->n; ++i) {
8680 bounded = fn(map->p[i], type, pos);
8681 if (bounded < 0 || bounded)
8688 /* Return 1 if the specified dim is involved in any lower bound.
8690 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8691 enum isl_dim_type type, unsigned pos)
8693 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8696 /* Return 1 if the specified dim is involved in any upper bound.
8698 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8699 enum isl_dim_type type, unsigned pos)
8701 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8704 /* For each of the "n" variables starting at "first", determine
8705 * the sign of the variable and put the results in the first "n"
8706 * elements of the array "signs".
8708 * 1 means that the variable is non-negative
8709 * -1 means that the variable is non-positive
8710 * 0 means the variable attains both positive and negative values.
8712 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8713 unsigned first, unsigned n, int *signs)
8715 isl_vec *bound = NULL;
8716 struct isl_tab *tab = NULL;
8717 struct isl_tab_undo *snap;
8720 if (!bset || !signs)
8723 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8724 tab = isl_tab_from_basic_set(bset);
8728 isl_seq_clr(bound->el, bound->size);
8729 isl_int_set_si(bound->el[0], -1);
8731 snap = isl_tab_snap(tab);
8732 for (i = 0; i < n; ++i) {
8735 isl_int_set_si(bound->el[1 + first + i], -1);
8736 if (isl_tab_add_ineq(tab, bound->el) < 0)
8739 isl_int_set_si(bound->el[1 + first + i], 0);
8740 if (isl_tab_rollback(tab, snap) < 0)
8748 isl_int_set_si(bound->el[1 + first + i], 1);
8749 if (isl_tab_add_ineq(tab, bound->el) < 0)
8752 isl_int_set_si(bound->el[1 + first + i], 0);
8753 if (isl_tab_rollback(tab, snap) < 0)
8756 signs[i] = empty ? -1 : 0;
8760 isl_vec_free(bound);
8764 isl_vec_free(bound);
8768 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8769 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8771 if (!bset || !signs)
8773 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8776 first += pos(bset->dim, type) - 1;
8777 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8780 /* Check if the given basic map is obviously single-valued.
8781 * In particular, for each output dimension, check that there is
8782 * an equality that defines the output dimension in terms of
8783 * earlier dimensions.
8785 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8795 total = 1 + isl_basic_map_total_dim(bmap);
8796 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8797 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8799 for (i = 0; i < n_out; ++i) {
8800 for (j = 0; j < bmap->n_eq; ++j) {
8801 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8803 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8804 total - (o_out + i + 1)) == -1)
8807 if (j >= bmap->n_eq)
8814 /* Check if the given map is obviously single-valued.
8816 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8825 return isl_basic_map_plain_is_single_valued(map->p[0]);
8828 /* Check if the given map is single-valued.
8833 * and check if the result is a subset of the identity mapping.
8835 int isl_map_is_single_valued(__isl_keep isl_map *map)
8842 sv = isl_map_plain_is_single_valued(map);
8846 test = isl_map_reverse(isl_map_copy(map));
8847 test = isl_map_apply_range(test, isl_map_copy(map));
8849 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8850 id = isl_map_identity(dim);
8852 sv = isl_map_is_subset(test, id);
8860 int isl_map_is_injective(__isl_keep isl_map *map)
8864 map = isl_map_copy(map);
8865 map = isl_map_reverse(map);
8866 in = isl_map_is_single_valued(map);
8872 /* Check if the given map is obviously injective.
8874 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8878 map = isl_map_copy(map);
8879 map = isl_map_reverse(map);
8880 in = isl_map_plain_is_single_valued(map);
8886 int isl_map_is_bijective(__isl_keep isl_map *map)
8890 sv = isl_map_is_single_valued(map);
8894 return isl_map_is_injective(map);
8897 int isl_set_is_singleton(__isl_keep isl_set *set)
8899 return isl_map_is_single_valued((isl_map *)set);
8902 int isl_map_is_translation(__isl_keep isl_map *map)
8907 delta = isl_map_deltas(isl_map_copy(map));
8908 ok = isl_set_is_singleton(delta);
8909 isl_set_free(delta);
8914 static int unique(isl_int *p, unsigned pos, unsigned len)
8916 if (isl_seq_first_non_zero(p, pos) != -1)
8918 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8923 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8932 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8935 nvar = isl_basic_set_dim(bset, isl_dim_set);
8936 ovar = isl_space_offset(bset->dim, isl_dim_set);
8937 for (j = 0; j < nvar; ++j) {
8938 int lower = 0, upper = 0;
8939 for (i = 0; i < bset->n_eq; ++i) {
8940 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8942 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8948 for (i = 0; i < bset->n_ineq; ++i) {
8949 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8951 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8953 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8958 if (!lower || !upper)
8965 int isl_set_is_box(__isl_keep isl_set *set)
8972 return isl_basic_set_is_box(set->p[0]);
8975 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8980 return isl_space_is_wrapping(bset->dim);
8983 int isl_set_is_wrapping(__isl_keep isl_set *set)
8988 return isl_space_is_wrapping(set->dim);
8991 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8993 bmap = isl_basic_map_cow(bmap);
8997 bmap->dim = isl_space_wrap(bmap->dim);
9001 bmap = isl_basic_map_finalize(bmap);
9003 return (isl_basic_set *)bmap;
9005 isl_basic_map_free(bmap);
9009 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9013 map = isl_map_cow(map);
9017 for (i = 0; i < map->n; ++i) {
9018 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9022 map->dim = isl_space_wrap(map->dim);
9026 return (isl_set *)map;
9032 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9034 bset = isl_basic_set_cow(bset);
9038 bset->dim = isl_space_unwrap(bset->dim);
9042 bset = isl_basic_set_finalize(bset);
9044 return (isl_basic_map *)bset;
9046 isl_basic_set_free(bset);
9050 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9057 if (!isl_set_is_wrapping(set))
9058 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9061 set = isl_set_cow(set);
9065 for (i = 0; i < set->n; ++i) {
9066 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9071 set->dim = isl_space_unwrap(set->dim);
9075 return (isl_map *)set;
9081 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9082 enum isl_dim_type type)
9087 if (!isl_space_is_named_or_nested(bmap->dim, type))
9090 bmap = isl_basic_map_cow(bmap);
9094 bmap->dim = isl_space_reset(bmap->dim, type);
9098 bmap = isl_basic_map_finalize(bmap);
9102 isl_basic_map_free(bmap);
9106 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9107 enum isl_dim_type type)
9114 if (!isl_space_is_named_or_nested(map->dim, type))
9117 map = isl_map_cow(map);
9121 for (i = 0; i < map->n; ++i) {
9122 map->p[i] = isl_basic_map_reset(map->p[i], type);
9126 map->dim = isl_space_reset(map->dim, type);
9136 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9141 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9144 bmap = isl_basic_map_cow(bmap);
9148 bmap->dim = isl_space_flatten(bmap->dim);
9152 bmap = isl_basic_map_finalize(bmap);
9156 isl_basic_map_free(bmap);
9160 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9162 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9165 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9166 __isl_take isl_basic_map *bmap)
9171 if (!bmap->dim->nested[0])
9174 bmap = isl_basic_map_cow(bmap);
9178 bmap->dim = isl_space_flatten_domain(bmap->dim);
9182 bmap = isl_basic_map_finalize(bmap);
9186 isl_basic_map_free(bmap);
9190 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9191 __isl_take isl_basic_map *bmap)
9196 if (!bmap->dim->nested[1])
9199 bmap = isl_basic_map_cow(bmap);
9203 bmap->dim = isl_space_flatten_range(bmap->dim);
9207 bmap = isl_basic_map_finalize(bmap);
9211 isl_basic_map_free(bmap);
9215 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9222 if (!map->dim->nested[0] && !map->dim->nested[1])
9225 map = isl_map_cow(map);
9229 for (i = 0; i < map->n; ++i) {
9230 map->p[i] = isl_basic_map_flatten(map->p[i]);
9234 map->dim = isl_space_flatten(map->dim);
9244 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9246 return (isl_set *)isl_map_flatten((isl_map *)set);
9249 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9251 isl_space *dim, *flat_dim;
9254 dim = isl_set_get_space(set);
9255 flat_dim = isl_space_flatten(isl_space_copy(dim));
9256 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9257 map = isl_map_intersect_domain(map, set);
9262 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9269 if (!map->dim->nested[0])
9272 map = isl_map_cow(map);
9276 for (i = 0; i < map->n; ++i) {
9277 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9281 map->dim = isl_space_flatten_domain(map->dim);
9291 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9298 if (!map->dim->nested[1])
9301 map = isl_map_cow(map);
9305 for (i = 0; i < map->n; ++i) {
9306 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9310 map->dim = isl_space_flatten_range(map->dim);
9320 /* Reorder the dimensions of "bmap" according to the given dim_map
9321 * and set the dimension specification to "dim".
9323 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9324 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9328 bmap = isl_basic_map_cow(bmap);
9329 if (!bmap || !dim || !dim_map)
9332 res = isl_basic_map_alloc_space(dim,
9333 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9334 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9335 res = isl_basic_map_finalize(res);
9339 isl_basic_map_free(bmap);
9340 isl_space_free(dim);
9344 /* Reorder the dimensions of "map" according to given reordering.
9346 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9347 __isl_take isl_reordering *r)
9350 struct isl_dim_map *dim_map;
9352 map = isl_map_cow(map);
9353 dim_map = isl_dim_map_from_reordering(r);
9354 if (!map || !r || !dim_map)
9357 for (i = 0; i < map->n; ++i) {
9358 struct isl_dim_map *dim_map_i;
9360 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9362 map->p[i] = isl_basic_map_realign(map->p[i],
9363 isl_space_copy(r->dim), dim_map_i);
9369 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9371 isl_reordering_free(r);
9377 isl_reordering_free(r);
9381 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9382 __isl_take isl_reordering *r)
9384 return (isl_set *)isl_map_realign((isl_map *)set, r);
9387 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9388 __isl_take isl_space *model)
9395 ctx = isl_space_get_ctx(model);
9396 if (!isl_space_has_named_params(model))
9397 isl_die(ctx, isl_error_invalid,
9398 "model has unnamed parameters", goto error);
9399 if (!isl_space_has_named_params(map->dim))
9400 isl_die(ctx, isl_error_invalid,
9401 "relation has unnamed parameters", goto error);
9402 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9403 isl_reordering *exp;
9405 model = isl_space_drop_dims(model, isl_dim_in,
9406 0, isl_space_dim(model, isl_dim_in));
9407 model = isl_space_drop_dims(model, isl_dim_out,
9408 0, isl_space_dim(model, isl_dim_out));
9409 exp = isl_parameter_alignment_reordering(map->dim, model);
9410 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9411 map = isl_map_realign(map, exp);
9414 isl_space_free(model);
9417 isl_space_free(model);
9422 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9423 __isl_take isl_space *model)
9425 return isl_map_align_params(set, model);
9428 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9429 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9430 enum isl_dim_type c2, enum isl_dim_type c3,
9431 enum isl_dim_type c4, enum isl_dim_type c5)
9433 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9434 struct isl_mat *mat;
9440 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9441 isl_basic_map_total_dim(bmap) + 1);
9444 for (i = 0; i < bmap->n_eq; ++i)
9445 for (j = 0, pos = 0; j < 5; ++j) {
9446 int off = isl_basic_map_offset(bmap, c[j]);
9447 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9448 isl_int_set(mat->row[i][pos],
9449 bmap->eq[i][off + k]);
9457 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9458 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9459 enum isl_dim_type c2, enum isl_dim_type c3,
9460 enum isl_dim_type c4, enum isl_dim_type c5)
9462 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9463 struct isl_mat *mat;
9469 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9470 isl_basic_map_total_dim(bmap) + 1);
9473 for (i = 0; i < bmap->n_ineq; ++i)
9474 for (j = 0, pos = 0; j < 5; ++j) {
9475 int off = isl_basic_map_offset(bmap, c[j]);
9476 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9477 isl_int_set(mat->row[i][pos],
9478 bmap->ineq[i][off + k]);
9486 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9487 __isl_take isl_space *dim,
9488 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9489 enum isl_dim_type c2, enum isl_dim_type c3,
9490 enum isl_dim_type c4, enum isl_dim_type c5)
9492 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9493 isl_basic_map *bmap;
9499 if (!dim || !eq || !ineq)
9502 if (eq->n_col != ineq->n_col)
9503 isl_die(dim->ctx, isl_error_invalid,
9504 "equalities and inequalities matrices should have "
9505 "same number of columns", goto error);
9507 total = 1 + isl_space_dim(dim, isl_dim_all);
9509 if (eq->n_col < total)
9510 isl_die(dim->ctx, isl_error_invalid,
9511 "number of columns too small", goto error);
9513 extra = eq->n_col - total;
9515 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9516 eq->n_row, ineq->n_row);
9519 for (i = 0; i < extra; ++i) {
9520 k = isl_basic_map_alloc_div(bmap);
9523 isl_int_set_si(bmap->div[k][0], 0);
9525 for (i = 0; i < eq->n_row; ++i) {
9526 l = isl_basic_map_alloc_equality(bmap);
9529 for (j = 0, pos = 0; j < 5; ++j) {
9530 int off = isl_basic_map_offset(bmap, c[j]);
9531 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9532 isl_int_set(bmap->eq[l][off + k],
9538 for (i = 0; i < ineq->n_row; ++i) {
9539 l = isl_basic_map_alloc_inequality(bmap);
9542 for (j = 0, pos = 0; j < 5; ++j) {
9543 int off = isl_basic_map_offset(bmap, c[j]);
9544 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9545 isl_int_set(bmap->ineq[l][off + k],
9552 isl_space_free(dim);
9558 isl_space_free(dim);
9564 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9565 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9566 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9568 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9569 c1, c2, c3, c4, isl_dim_in);
9572 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9573 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9574 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9576 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9577 c1, c2, c3, c4, isl_dim_in);
9580 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9581 __isl_take isl_space *dim,
9582 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9583 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9585 return (isl_basic_set*)
9586 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9587 c1, c2, c3, c4, isl_dim_in);
9590 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9595 return isl_space_can_zip(bmap->dim);
9598 int isl_map_can_zip(__isl_keep isl_map *map)
9603 return isl_space_can_zip(map->dim);
9606 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9607 * (A -> C) -> (B -> D).
9609 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9618 if (!isl_basic_map_can_zip(bmap))
9619 isl_die(bmap->ctx, isl_error_invalid,
9620 "basic map cannot be zipped", goto error);
9621 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9622 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9623 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9624 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9625 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9628 bmap->dim = isl_space_zip(bmap->dim);
9633 isl_basic_map_free(bmap);
9637 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9638 * (A -> C) -> (B -> D).
9640 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9647 if (!isl_map_can_zip(map))
9648 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9651 map = isl_map_cow(map);
9655 for (i = 0; i < map->n; ++i) {
9656 map->p[i] = isl_basic_map_zip(map->p[i]);
9661 map->dim = isl_space_zip(map->dim);
9671 /* Construct a basic map mapping the domain of the affine expression
9672 * to a one-dimensional range prescribed by the affine expression.
9674 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9678 isl_local_space *ls;
9679 isl_basic_map *bmap;
9684 ls = isl_aff_get_local_space(aff);
9685 bmap = isl_basic_map_from_local_space(ls);
9686 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9687 k = isl_basic_map_alloc_equality(bmap);
9691 pos = isl_basic_map_offset(bmap, isl_dim_out);
9692 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9693 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9694 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9695 aff->v->size - (pos + 1));
9698 bmap = isl_basic_map_finalize(bmap);
9702 isl_basic_map_free(bmap);
9706 /* Construct a basic map mapping the domain the multi-affine expression
9707 * to its range, with each dimension in the range equated to the
9708 * corresponding affine expression.
9710 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
9711 __isl_take isl_multi_aff *maff)
9715 isl_basic_map *bmap;
9720 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
9721 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
9722 "invalid space", return isl_multi_aff_free(maff));
9724 space = isl_space_domain(isl_multi_aff_get_space(maff));
9725 bmap = isl_basic_map_universe(isl_space_from_domain(space));
9727 for (i = 0; i < maff->n; ++i) {
9729 isl_basic_map *bmap_i;
9731 aff = isl_aff_copy(maff->p[i]);
9732 bmap_i = isl_basic_map_from_aff(aff);
9734 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9737 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
9739 isl_multi_aff_free(maff);
9743 /* Construct a basic map mapping a domain in the given space to
9744 * to an n-dimensional range, with n the number of elements in the list,
9745 * where each coordinate in the range is prescribed by the
9746 * corresponding affine expression.
9747 * The domains of all affine expressions in the list are assumed to match
9750 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9751 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9755 isl_basic_map *bmap;
9760 dim = isl_space_from_domain(domain_dim);
9761 bmap = isl_basic_map_universe(dim);
9763 for (i = 0; i < list->n; ++i) {
9765 isl_basic_map *bmap_i;
9767 aff = isl_aff_copy(list->p[i]);
9768 bmap_i = isl_basic_map_from_aff(aff);
9770 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9773 isl_aff_list_free(list);
9777 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9778 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9780 return isl_map_equate(set, type1, pos1, type2, pos2);
9783 /* Add a constraint imposing that the given two dimensions are equal.
9785 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9786 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9788 isl_basic_map *bmap = NULL;
9794 if (pos1 >= isl_map_dim(map, type1))
9795 isl_die(map->ctx, isl_error_invalid,
9796 "index out of bounds", goto error);
9797 if (pos2 >= isl_map_dim(map, type2))
9798 isl_die(map->ctx, isl_error_invalid,
9799 "index out of bounds", goto error);
9801 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9802 i = isl_basic_map_alloc_equality(bmap);
9805 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9806 pos1 += isl_basic_map_offset(bmap, type1);
9807 pos2 += isl_basic_map_offset(bmap, type2);
9808 isl_int_set_si(bmap->eq[i][pos1], -1);
9809 isl_int_set_si(bmap->eq[i][pos2], 1);
9810 bmap = isl_basic_map_finalize(bmap);
9812 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9816 isl_basic_map_free(bmap);
9821 /* Add a constraint imposing that the given two dimensions have opposite values.
9823 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9824 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9826 isl_basic_map *bmap = NULL;
9832 if (pos1 >= isl_map_dim(map, type1))
9833 isl_die(map->ctx, isl_error_invalid,
9834 "index out of bounds", goto error);
9835 if (pos2 >= isl_map_dim(map, type2))
9836 isl_die(map->ctx, isl_error_invalid,
9837 "index out of bounds", goto error);
9839 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9840 i = isl_basic_map_alloc_equality(bmap);
9843 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9844 pos1 += isl_basic_map_offset(bmap, type1);
9845 pos2 += isl_basic_map_offset(bmap, type2);
9846 isl_int_set_si(bmap->eq[i][pos1], 1);
9847 isl_int_set_si(bmap->eq[i][pos2], 1);
9848 bmap = isl_basic_map_finalize(bmap);
9850 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9854 isl_basic_map_free(bmap);
9859 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
9863 isl_local_space *ls;
9868 if (!isl_basic_map_divs_known(bmap))
9869 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
9870 "some divs are unknown", return NULL);
9872 ls = isl_basic_map_get_local_space(bmap);
9873 div = isl_local_space_get_div(ls, pos);
9874 isl_local_space_free(ls);
9879 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
9882 return isl_basic_map_get_div(bset, pos);
9885 /* Plug in "subs" for dimension "type", "pos" of "bset".
9887 * Let i be the dimension to replace and let "subs" be of the form
9891 * Any integer division with a non-zero coefficient for i,
9893 * floor((a i + g)/m)
9897 * floor((a f + d g)/(m d))
9899 * Constraints of the form
9907 __isl_give isl_basic_set *isl_basic_set_substitute(
9908 __isl_take isl_basic_set *bset,
9909 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9915 if (bset && isl_basic_set_plain_is_empty(bset))
9918 bset = isl_basic_set_cow(bset);
9922 ctx = isl_basic_set_get_ctx(bset);
9923 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
9924 isl_die(ctx, isl_error_invalid,
9925 "spaces don't match", goto error);
9926 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
9927 isl_die(ctx, isl_error_unsupported,
9928 "cannot handle divs yet", goto error);
9930 pos += isl_basic_set_offset(bset, type);
9934 for (i = 0; i < bset->n_eq; ++i) {
9935 if (isl_int_is_zero(bset->eq[i][pos]))
9937 isl_int_set(v, bset->eq[i][pos]);
9938 isl_int_set_si(bset->eq[i][pos], 0);
9939 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
9940 v, subs->v->el + 1, subs->v->size - 1);
9943 for (i = 0; i < bset->n_ineq; ++i) {
9944 if (isl_int_is_zero(bset->ineq[i][pos]))
9946 isl_int_set(v, bset->ineq[i][pos]);
9947 isl_int_set_si(bset->ineq[i][pos], 0);
9948 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
9949 v, subs->v->el + 1, subs->v->size - 1);
9952 for (i = 0; i < bset->n_div; ++i) {
9953 if (isl_int_is_zero(bset->div[i][1 + pos]))
9955 isl_int_set(v, bset->div[i][1 + pos]);
9956 isl_int_set_si(bset->div[i][1 + pos], 0);
9957 isl_seq_combine(bset->div[i] + 1,
9958 subs->v->el[0], bset->div[i] + 1,
9959 v, subs->v->el + 1, subs->v->size - 1);
9960 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
9965 bset = isl_basic_set_simplify(bset);
9966 return isl_basic_set_finalize(bset);
9968 isl_basic_set_free(bset);
9972 /* Plug in "subs" for dimension "type", "pos" of "set".
9974 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
9975 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9979 if (set && isl_set_plain_is_empty(set))
9982 set = isl_set_cow(set);
9986 for (i = set->n - 1; i >= 0; --i) {
9987 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
9990 if (isl_basic_set_plain_is_empty(set->p[i])) {
9991 isl_basic_set_free(set->p[i]);
9992 if (i != set->n - 1)
9993 set->p[i] = set->p[set->n - 1];