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>
34 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
37 case isl_dim_param: return dim->nparam;
38 case isl_dim_in: return dim->n_in;
39 case isl_dim_out: return dim->n_out;
40 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
45 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
48 case isl_dim_param: return 1;
49 case isl_dim_in: return 1 + dim->nparam;
50 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
55 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
56 enum isl_dim_type type)
61 case isl_dim_cst: return 1;
64 case isl_dim_out: return isl_space_dim(bmap->dim, type);
65 case isl_dim_div: return bmap->n_div;
66 case isl_dim_all: return isl_basic_map_total_dim(bmap);
71 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
73 return map ? n(map->dim, type) : 0;
76 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
78 return set ? n(set->dim, type) : 0;
81 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
82 enum isl_dim_type type)
84 isl_space *dim = bmap->dim;
86 case isl_dim_cst: return 0;
87 case isl_dim_param: return 1;
88 case isl_dim_in: return 1 + dim->nparam;
89 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
90 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
95 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
96 enum isl_dim_type type)
98 return isl_basic_map_offset(bset, type);
101 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
103 return pos(map->dim, type);
106 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_dim(bset, type);
112 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
114 return isl_basic_set_dim(bset, isl_dim_set);
117 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
119 return isl_basic_set_dim(bset, isl_dim_param);
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
124 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
127 unsigned isl_set_n_dim(__isl_keep isl_set *set)
129 return isl_set_dim(set, isl_dim_set);
132 unsigned isl_set_n_param(__isl_keep isl_set *set)
134 return isl_set_dim(set, isl_dim_param);
137 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
139 return bmap ? bmap->dim->n_in : 0;
142 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
144 return bmap ? bmap->dim->n_out : 0;
147 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
149 return bmap ? bmap->dim->nparam : 0;
152 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
154 return bmap ? bmap->n_div : 0;
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
159 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
162 unsigned isl_map_n_in(const struct isl_map *map)
164 return map ? map->dim->n_in : 0;
167 unsigned isl_map_n_out(const struct isl_map *map)
169 return map ? map->dim->n_out : 0;
172 unsigned isl_map_n_param(const struct isl_map *map)
174 return map ? map->dim->nparam : 0;
177 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
182 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
185 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
188 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
189 struct isl_basic_set *bset)
194 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
197 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
200 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
205 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
208 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
211 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
212 struct isl_basic_set *bset)
217 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
220 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
223 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
225 return bmap ? bmap->ctx : NULL;
228 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
230 return bset ? bset->ctx : NULL;
233 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
235 return map ? map->ctx : NULL;
238 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
240 return set ? set->ctx : NULL;
243 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
247 return isl_space_copy(bmap->dim);
250 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
254 return isl_space_copy(bset->dim);
257 __isl_give isl_local_space *isl_basic_map_get_local_space(
258 __isl_keep isl_basic_map *bmap)
267 total = isl_basic_map_total_dim(bmap);
268 ls = isl_local_space_alloc(isl_space_copy(bmap->dim), bmap->n_div);
272 for (i = 0; i < bmap->n_div; ++i)
273 isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total);
278 __isl_give isl_local_space *isl_basic_set_get_local_space(
279 __isl_keep isl_basic_set *bset)
281 return isl_basic_map_get_local_space(bset);
284 __isl_give isl_basic_map *isl_basic_map_from_local_space(
285 __isl_take isl_local_space *ls)
294 n_div = isl_local_space_dim(ls, isl_dim_div);
295 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
296 n_div, 0, 2 * n_div);
298 for (i = 0; i < n_div; ++i)
299 if (isl_basic_map_alloc_div(bmap) < 0)
302 for (i = 0; i < n_div; ++i) {
303 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
304 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
308 isl_local_space_free(ls);
311 isl_local_space_free(ls);
312 isl_basic_map_free(bmap);
316 __isl_give isl_basic_set *isl_basic_set_from_local_space(
317 __isl_take isl_local_space *ls)
319 return isl_basic_map_from_local_space(ls);
322 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
326 return isl_space_copy(map->dim);
329 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
333 return isl_space_copy(set->dim);
336 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
337 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
339 bmap = isl_basic_map_cow(bmap);
342 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
345 bmap = isl_basic_map_finalize(bmap);
348 isl_basic_map_free(bmap);
352 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
353 __isl_take isl_basic_set *bset, const char *s)
355 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
358 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
359 enum isl_dim_type type)
361 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
364 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
365 enum isl_dim_type type, const char *s)
369 map = isl_map_cow(map);
373 map->dim = isl_space_set_tuple_name(map->dim, type, s);
377 for (i = 0; i < map->n; ++i) {
378 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
389 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
390 enum isl_dim_type type)
392 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
395 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
398 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
401 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
402 enum isl_dim_type type, __isl_take isl_id *id)
404 map = isl_map_cow(map);
406 return isl_id_free(id);
408 map->dim = isl_space_set_tuple_id(map->dim, type, id);
410 return isl_map_reset_space(map, isl_space_copy(map->dim));
413 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
414 __isl_take isl_id *id)
416 return isl_map_set_tuple_id(set, isl_dim_set, id);
419 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
420 enum isl_dim_type type)
422 map = isl_map_cow(map);
426 map->dim = isl_space_reset_tuple_id(map->dim, type);
428 return isl_map_reset_space(map, isl_space_copy(map->dim));
431 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
433 return isl_map_reset_tuple_id(set, isl_dim_set);
436 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
438 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
441 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
442 enum isl_dim_type type)
444 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
447 int isl_set_has_tuple_id(__isl_keep isl_set *set)
449 return isl_map_has_tuple_id(set, isl_dim_set);
452 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
454 return isl_map_get_tuple_id(set, isl_dim_set);
457 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
459 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
462 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
464 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
467 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
468 enum isl_dim_type type, unsigned pos)
470 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
473 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
474 enum isl_dim_type type, unsigned pos)
476 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
479 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
480 enum isl_dim_type type, unsigned pos)
482 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
485 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
486 enum isl_dim_type type, unsigned pos)
488 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
491 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
492 __isl_take isl_basic_map *bmap,
493 enum isl_dim_type type, unsigned pos, const char *s)
497 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
502 isl_basic_map_free(bmap);
506 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
507 enum isl_dim_type type, unsigned pos, const char *s)
514 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
518 for (i = 0; i < map->n; ++i) {
519 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
530 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
531 __isl_take isl_basic_set *bset,
532 enum isl_dim_type type, unsigned pos, const char *s)
534 return (isl_basic_set *)isl_basic_map_set_dim_name(
535 (isl_basic_map *)bset, type, pos, s);
538 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
539 enum isl_dim_type type, unsigned pos, const char *s)
541 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
544 int isl_map_has_dim_id(__isl_keep isl_map *map,
545 enum isl_dim_type type, unsigned pos)
547 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
550 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
551 enum isl_dim_type type, unsigned pos)
553 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
556 int isl_set_has_dim_id(__isl_keep isl_set *set,
557 enum isl_dim_type type, unsigned pos)
559 return isl_map_has_dim_id(set, type, pos);
562 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
563 enum isl_dim_type type, unsigned pos)
565 return isl_map_get_dim_id(set, type, pos);
568 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
569 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
571 map = isl_map_cow(map);
573 return isl_id_free(id);
575 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
577 return isl_map_reset_space(map, isl_space_copy(map->dim));
580 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
581 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
583 return isl_map_set_dim_id(set, type, pos, id);
586 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
587 __isl_keep isl_id *id)
591 return isl_space_find_dim_by_id(map->dim, type, id);
594 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
595 __isl_keep isl_id *id)
597 return isl_map_find_dim_by_id(set, type, id);
600 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
604 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
607 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
609 return isl_basic_map_is_rational(bset);
612 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
613 struct isl_basic_map *bmap, unsigned extra,
614 unsigned n_eq, unsigned n_ineq)
617 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
622 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
623 if (isl_blk_is_error(bmap->block))
626 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
631 bmap->block2 = isl_blk_empty();
634 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
635 if (isl_blk_is_error(bmap->block2))
638 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
643 for (i = 0; i < n_ineq + n_eq; ++i)
644 bmap->ineq[i] = bmap->block.data + i * row_size;
646 for (i = 0; i < extra; ++i)
647 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
651 bmap->c_size = n_eq + n_ineq;
652 bmap->eq = bmap->ineq + n_ineq;
661 isl_basic_map_free(bmap);
665 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
666 unsigned nparam, unsigned dim, unsigned extra,
667 unsigned n_eq, unsigned n_ineq)
669 struct isl_basic_map *bmap;
672 space = isl_space_set_alloc(ctx, nparam, dim);
676 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
677 return (struct isl_basic_set *)bmap;
680 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
681 unsigned extra, unsigned n_eq, unsigned n_ineq)
683 struct isl_basic_map *bmap;
686 isl_assert(dim->ctx, dim->n_in == 0, goto error);
687 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
688 return (struct isl_basic_set *)bmap;
694 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
695 unsigned extra, unsigned n_eq, unsigned n_ineq)
697 struct isl_basic_map *bmap;
701 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
706 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
712 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
713 unsigned nparam, unsigned in, unsigned out, unsigned extra,
714 unsigned n_eq, unsigned n_ineq)
716 struct isl_basic_map *bmap;
719 dim = isl_space_alloc(ctx, nparam, in, out);
723 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
727 static void dup_constraints(
728 struct isl_basic_map *dst, struct isl_basic_map *src)
731 unsigned total = isl_basic_map_total_dim(src);
733 for (i = 0; i < src->n_eq; ++i) {
734 int j = isl_basic_map_alloc_equality(dst);
735 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
738 for (i = 0; i < src->n_ineq; ++i) {
739 int j = isl_basic_map_alloc_inequality(dst);
740 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
743 for (i = 0; i < src->n_div; ++i) {
744 int j = isl_basic_map_alloc_div(dst);
745 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
747 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
750 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
752 struct isl_basic_map *dup;
756 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
757 bmap->n_div, bmap->n_eq, bmap->n_ineq);
760 dup_constraints(dup, bmap);
761 dup->flags = bmap->flags;
762 dup->sample = isl_vec_copy(bmap->sample);
766 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
768 struct isl_basic_map *dup;
770 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
771 return (struct isl_basic_set *)dup;
774 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
779 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
783 return isl_basic_set_dup(bset);
786 struct isl_set *isl_set_copy(struct isl_set *set)
795 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
800 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
804 bmap = isl_basic_map_dup(bmap);
806 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
810 struct isl_map *isl_map_copy(struct isl_map *map)
819 void isl_basic_map_free(struct isl_basic_map *bmap)
827 isl_ctx_deref(bmap->ctx);
829 isl_blk_free(bmap->ctx, bmap->block2);
831 isl_blk_free(bmap->ctx, bmap->block);
832 isl_vec_free(bmap->sample);
833 isl_space_free(bmap->dim);
837 void isl_basic_set_free(struct isl_basic_set *bset)
839 isl_basic_map_free((struct isl_basic_map *)bset);
842 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
844 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
847 __isl_give isl_map *isl_map_align_params_map_map_and(
848 __isl_take isl_map *map1, __isl_take isl_map *map2,
849 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
850 __isl_take isl_map *map2))
854 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
855 return fn(map1, map2);
856 if (!isl_space_has_named_params(map1->dim) ||
857 !isl_space_has_named_params(map2->dim))
858 isl_die(map1->ctx, isl_error_invalid,
859 "unaligned unnamed parameters", goto error);
860 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
861 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
862 return fn(map1, map2);
869 static int align_params_map_map_and_test(__isl_keep isl_map *map1,
870 __isl_keep isl_map *map2,
871 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
877 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
878 return fn(map1, map2);
879 if (!isl_space_has_named_params(map1->dim) ||
880 !isl_space_has_named_params(map2->dim))
881 isl_die(map1->ctx, isl_error_invalid,
882 "unaligned unnamed parameters", return -1);
883 map1 = isl_map_copy(map1);
884 map2 = isl_map_copy(map2);
885 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
886 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
893 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
899 isl_assert(ctx, room_for_con(bmap, 1), return -1);
900 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
902 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
903 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
904 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
905 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
906 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
907 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
909 int j = isl_basic_map_alloc_inequality(bmap);
913 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
914 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
921 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
922 bmap->extra - bmap->n_div);
926 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
928 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
931 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
935 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
940 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
942 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
945 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
950 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
952 if (pos != bmap->n_eq - 1) {
954 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
955 bmap->eq[bmap->n_eq - 1] = t;
961 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
963 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
966 void isl_basic_map_inequality_to_equality(
967 struct isl_basic_map *bmap, unsigned pos)
972 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
973 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
978 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
979 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
980 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
981 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
984 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
986 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
989 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
995 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
996 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
997 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
998 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
999 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1000 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1001 1 + isl_basic_map_total_dim(bmap),
1002 bmap->extra - bmap->n_div);
1003 return bmap->n_ineq++;
1006 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1008 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1011 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1015 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1020 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1022 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1025 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1030 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1032 if (pos != bmap->n_ineq - 1) {
1033 t = bmap->ineq[pos];
1034 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1035 bmap->ineq[bmap->n_ineq - 1] = t;
1036 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1042 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1044 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1047 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1052 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1055 k = isl_basic_map_alloc_equality(bmap);
1058 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1061 isl_basic_map_free(bmap);
1065 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1068 return (isl_basic_set *)
1069 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1072 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1077 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1080 k = isl_basic_map_alloc_inequality(bmap);
1083 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1086 isl_basic_map_free(bmap);
1090 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1093 return (isl_basic_set *)
1094 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1097 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1101 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1102 isl_seq_clr(bmap->div[bmap->n_div] +
1103 1 + 1 + isl_basic_map_total_dim(bmap),
1104 bmap->extra - bmap->n_div);
1105 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1106 return bmap->n_div++;
1109 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1111 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1114 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1118 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1123 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1125 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1128 /* Copy constraint from src to dst, putting the vars of src at offset
1129 * dim_off in dst and the divs of src at offset div_off in dst.
1130 * If both sets are actually map, then dim_off applies to the input
1133 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1134 struct isl_basic_map *src_map, isl_int *src,
1135 unsigned in_off, unsigned out_off, unsigned div_off)
1137 unsigned src_nparam = isl_basic_map_n_param(src_map);
1138 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1139 unsigned src_in = isl_basic_map_n_in(src_map);
1140 unsigned dst_in = isl_basic_map_n_in(dst_map);
1141 unsigned src_out = isl_basic_map_n_out(src_map);
1142 unsigned dst_out = isl_basic_map_n_out(dst_map);
1143 isl_int_set(dst[0], src[0]);
1144 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1145 if (dst_nparam > src_nparam)
1146 isl_seq_clr(dst+1+src_nparam,
1147 dst_nparam - src_nparam);
1148 isl_seq_clr(dst+1+dst_nparam, in_off);
1149 isl_seq_cpy(dst+1+dst_nparam+in_off,
1151 isl_min(dst_in-in_off, src_in));
1152 if (dst_in-in_off > src_in)
1153 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1154 dst_in - in_off - src_in);
1155 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1156 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1157 src+1+src_nparam+src_in,
1158 isl_min(dst_out-out_off, src_out));
1159 if (dst_out-out_off > src_out)
1160 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1161 dst_out - out_off - src_out);
1162 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1163 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1164 src+1+src_nparam+src_in+src_out,
1165 isl_min(dst_map->extra-div_off, src_map->n_div));
1166 if (dst_map->n_div-div_off > src_map->n_div)
1167 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1168 div_off+src_map->n_div,
1169 dst_map->n_div - div_off - src_map->n_div);
1172 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1173 struct isl_basic_map *src_map, isl_int *src,
1174 unsigned in_off, unsigned out_off, unsigned div_off)
1176 isl_int_set(dst[0], src[0]);
1177 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1180 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1181 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1186 if (!bmap1 || !bmap2)
1189 div_off = bmap1->n_div;
1191 for (i = 0; i < bmap2->n_eq; ++i) {
1192 int i1 = isl_basic_map_alloc_equality(bmap1);
1195 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1196 i_pos, o_pos, div_off);
1199 for (i = 0; i < bmap2->n_ineq; ++i) {
1200 int i1 = isl_basic_map_alloc_inequality(bmap1);
1203 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1204 i_pos, o_pos, div_off);
1207 for (i = 0; i < bmap2->n_div; ++i) {
1208 int i1 = isl_basic_map_alloc_div(bmap1);
1211 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1212 i_pos, o_pos, div_off);
1215 isl_basic_map_free(bmap2);
1220 isl_basic_map_free(bmap1);
1221 isl_basic_map_free(bmap2);
1225 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1226 struct isl_basic_set *bset2, unsigned pos)
1228 return (struct isl_basic_set *)
1229 add_constraints((struct isl_basic_map *)bset1,
1230 (struct isl_basic_map *)bset2, 0, pos);
1233 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1234 __isl_take isl_space *dim, unsigned extra,
1235 unsigned n_eq, unsigned n_ineq)
1237 struct isl_basic_map *ext;
1247 dims_ok = isl_space_is_equal(base->dim, dim) &&
1248 base->extra >= base->n_div + extra;
1250 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1251 room_for_ineq(base, n_ineq)) {
1252 isl_space_free(dim);
1256 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1257 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1258 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1259 extra += base->extra;
1261 n_ineq += base->n_ineq;
1263 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1269 ext->sample = isl_vec_copy(base->sample);
1270 flags = base->flags;
1271 ext = add_constraints(ext, base, 0, 0);
1274 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1280 isl_space_free(dim);
1281 isl_basic_map_free(base);
1285 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1286 __isl_take isl_space *dim, unsigned extra,
1287 unsigned n_eq, unsigned n_ineq)
1289 return (struct isl_basic_set *)
1290 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1291 extra, n_eq, n_ineq);
1294 struct isl_basic_map *isl_basic_map_extend_constraints(
1295 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1299 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1303 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1304 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1305 unsigned n_eq, unsigned n_ineq)
1307 struct isl_basic_map *bmap;
1312 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1316 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1319 isl_basic_map_free(base);
1323 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1324 unsigned nparam, unsigned dim, unsigned extra,
1325 unsigned n_eq, unsigned n_ineq)
1327 return (struct isl_basic_set *)
1328 isl_basic_map_extend((struct isl_basic_map *)base,
1329 nparam, 0, dim, extra, n_eq, n_ineq);
1332 struct isl_basic_set *isl_basic_set_extend_constraints(
1333 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1335 return (struct isl_basic_set *)
1336 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1340 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1342 return (struct isl_basic_set *)
1343 isl_basic_map_cow((struct isl_basic_map *)bset);
1346 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1351 if (bmap->ref > 1) {
1353 bmap = isl_basic_map_dup(bmap);
1356 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1360 struct isl_set *isl_set_cow(struct isl_set *set)
1368 return isl_set_dup(set);
1371 struct isl_map *isl_map_cow(struct isl_map *map)
1379 return isl_map_dup(map);
1382 static void swap_vars(struct isl_blk blk, isl_int *a,
1383 unsigned a_len, unsigned b_len)
1385 isl_seq_cpy(blk.data, a+a_len, b_len);
1386 isl_seq_cpy(blk.data+b_len, a, a_len);
1387 isl_seq_cpy(a, blk.data, b_len+a_len);
1390 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1391 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1399 isl_assert(bmap->ctx,
1400 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1402 if (n1 == 0 || n2 == 0)
1405 bmap = isl_basic_map_cow(bmap);
1409 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1410 if (isl_blk_is_error(blk))
1413 for (i = 0; i < bmap->n_eq; ++i)
1415 bmap->eq[i] + pos, n1, n2);
1417 for (i = 0; i < bmap->n_ineq; ++i)
1419 bmap->ineq[i] + pos, n1, n2);
1421 for (i = 0; i < bmap->n_div; ++i)
1423 bmap->div[i]+1 + pos, n1, n2);
1425 isl_blk_free(bmap->ctx, blk);
1427 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1428 bmap = isl_basic_map_gauss(bmap, NULL);
1429 return isl_basic_map_finalize(bmap);
1431 isl_basic_map_free(bmap);
1435 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1436 __isl_take isl_basic_set *bset, unsigned n)
1441 nparam = isl_basic_set_n_param(bset);
1442 dim = isl_basic_set_n_dim(bset);
1443 isl_assert(bset->ctx, n <= dim, goto error);
1445 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1447 isl_basic_set_free(bset);
1451 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1457 total = isl_basic_map_total_dim(bmap);
1458 isl_basic_map_free_div(bmap, bmap->n_div);
1459 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1461 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1463 i = isl_basic_map_alloc_equality(bmap);
1467 isl_int_set_si(bmap->eq[i][0], 1);
1468 isl_seq_clr(bmap->eq[i]+1, total);
1469 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1470 isl_vec_free(bmap->sample);
1471 bmap->sample = NULL;
1472 return isl_basic_map_finalize(bmap);
1474 isl_basic_map_free(bmap);
1478 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1480 return (struct isl_basic_set *)
1481 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1484 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1487 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1488 isl_int *t = bmap->div[a];
1489 bmap->div[a] = bmap->div[b];
1492 for (i = 0; i < bmap->n_eq; ++i)
1493 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1495 for (i = 0; i < bmap->n_ineq; ++i)
1496 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1498 for (i = 0; i < bmap->n_div; ++i)
1499 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1500 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1503 /* Eliminate the specified n dimensions starting at first from the
1504 * constraints using Fourier-Motzkin. The dimensions themselves
1507 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1508 enum isl_dim_type type, unsigned first, unsigned n)
1517 map = isl_map_cow(map);
1520 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1521 first += pos(map->dim, type) - 1;
1523 for (i = 0; i < map->n; ++i) {
1524 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1534 /* Eliminate the specified n dimensions starting at first from the
1535 * constraints using Fourier-Motzkin. The dimensions themselves
1538 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1539 enum isl_dim_type type, unsigned first, unsigned n)
1541 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1544 /* Eliminate the specified n dimensions starting at first from the
1545 * constraints using Fourier-Motzkin. The dimensions themselves
1548 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1549 unsigned first, unsigned n)
1551 return isl_set_eliminate(set, isl_dim_set, first, n);
1554 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1555 __isl_take isl_basic_map *bmap)
1559 bmap = isl_basic_map_eliminate_vars(bmap,
1560 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1564 return isl_basic_map_finalize(bmap);
1567 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1568 __isl_take isl_basic_set *bset)
1570 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1571 (struct isl_basic_map *)bset);
1574 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1583 map = isl_map_cow(map);
1587 for (i = 0; i < map->n; ++i) {
1588 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1598 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1600 return isl_map_remove_divs(set);
1603 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1604 enum isl_dim_type type, unsigned first, unsigned n)
1608 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1610 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1612 bmap = isl_basic_map_eliminate_vars(bmap,
1613 isl_basic_map_offset(bmap, type) - 1 + first, n);
1616 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1618 bmap = isl_basic_map_drop(bmap, type, first, n);
1621 isl_basic_map_free(bmap);
1625 /* Return true if the definition of the given div (recursively) involves
1626 * any of the given variables.
1628 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1629 unsigned first, unsigned n)
1632 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1634 if (isl_int_is_zero(bmap->div[div][0]))
1636 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1639 for (i = bmap->n_div - 1; i >= 0; --i) {
1640 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1642 if (div_involves_vars(bmap, i, first, n))
1649 /* Remove all divs (recursively) involving any of the given dimensions
1650 * in their definitions.
1652 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1653 __isl_take isl_basic_map *bmap,
1654 enum isl_dim_type type, unsigned first, unsigned n)
1660 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1662 first += isl_basic_map_offset(bmap, type);
1664 for (i = bmap->n_div - 1; i >= 0; --i) {
1665 if (!div_involves_vars(bmap, i, first, n))
1667 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1675 isl_basic_map_free(bmap);
1679 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1680 enum isl_dim_type type, unsigned first, unsigned n)
1689 map = isl_map_cow(map);
1693 for (i = 0; i < map->n; ++i) {
1694 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1705 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1706 enum isl_dim_type type, unsigned first, unsigned n)
1708 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1712 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1713 enum isl_dim_type type, unsigned first, unsigned n)
1720 if (first + n > isl_basic_map_dim(bmap, type))
1721 isl_die(bmap->ctx, isl_error_invalid,
1722 "index out of bounds", return -1);
1724 first += isl_basic_map_offset(bmap, type);
1725 for (i = 0; i < bmap->n_eq; ++i)
1726 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1728 for (i = 0; i < bmap->n_ineq; ++i)
1729 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1735 int isl_map_involves_dims(__isl_keep isl_map *map,
1736 enum isl_dim_type type, unsigned first, unsigned n)
1743 if (first + n > isl_map_dim(map, type))
1744 isl_die(map->ctx, isl_error_invalid,
1745 "index out of bounds", return -1);
1747 for (i = 0; i < map->n; ++i) {
1748 int involves = isl_basic_map_involves_dims(map->p[i],
1750 if (involves < 0 || !involves)
1757 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1758 enum isl_dim_type type, unsigned first, unsigned n)
1760 return isl_basic_map_involves_dims(bset, type, first, n);
1763 int isl_set_involves_dims(__isl_keep isl_set *set,
1764 enum isl_dim_type type, unsigned first, unsigned n)
1766 return isl_map_involves_dims(set, type, first, n);
1769 /* Return true if the definition of the given div is unknown or depends
1772 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1775 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1777 if (isl_int_is_zero(bmap->div[div][0]))
1780 for (i = bmap->n_div - 1; i >= 0; --i) {
1781 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1783 if (div_is_unknown(bmap, i))
1790 /* Remove all divs that are unknown or defined in terms of unknown divs.
1792 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1793 __isl_take isl_basic_map *bmap)
1800 for (i = bmap->n_div - 1; i >= 0; --i) {
1801 if (!div_is_unknown(bmap, i))
1803 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1809 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1818 map = isl_map_cow(map);
1822 for (i = 0; i < map->n; ++i) {
1823 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1833 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1835 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1838 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1839 __isl_take isl_basic_set *bset,
1840 enum isl_dim_type type, unsigned first, unsigned n)
1842 return (isl_basic_set *)
1843 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1846 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1847 enum isl_dim_type type, unsigned first, unsigned n)
1854 map = isl_map_cow(map);
1857 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1859 for (i = 0; i < map->n; ++i) {
1860 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1861 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1865 map = isl_map_drop(map, type, first, n);
1872 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1873 enum isl_dim_type type, unsigned first, unsigned n)
1875 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1878 /* Project out n inputs starting at first using Fourier-Motzkin */
1879 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1880 unsigned first, unsigned n)
1882 return isl_map_remove_dims(map, isl_dim_in, first, n);
1885 static void dump_term(struct isl_basic_map *bmap,
1886 isl_int c, int pos, FILE *out)
1889 unsigned in = isl_basic_map_n_in(bmap);
1890 unsigned dim = in + isl_basic_map_n_out(bmap);
1891 unsigned nparam = isl_basic_map_n_param(bmap);
1893 isl_int_print(out, c, 0);
1895 if (!isl_int_is_one(c))
1896 isl_int_print(out, c, 0);
1897 if (pos < 1 + nparam) {
1898 name = isl_space_get_dim_name(bmap->dim,
1899 isl_dim_param, pos - 1);
1901 fprintf(out, "%s", name);
1903 fprintf(out, "p%d", pos - 1);
1904 } else if (pos < 1 + nparam + in)
1905 fprintf(out, "i%d", pos - 1 - nparam);
1906 else if (pos < 1 + nparam + dim)
1907 fprintf(out, "o%d", pos - 1 - nparam - in);
1909 fprintf(out, "e%d", pos - 1 - nparam - dim);
1913 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1914 int sign, FILE *out)
1918 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1922 for (i = 0, first = 1; i < len; ++i) {
1923 if (isl_int_sgn(c[i]) * sign <= 0)
1926 fprintf(out, " + ");
1928 isl_int_abs(v, c[i]);
1929 dump_term(bmap, v, i, out);
1936 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1937 const char *op, FILE *out, int indent)
1941 fprintf(out, "%*s", indent, "");
1943 dump_constraint_sign(bmap, c, 1, out);
1944 fprintf(out, " %s ", op);
1945 dump_constraint_sign(bmap, c, -1, out);
1949 for (i = bmap->n_div; i < bmap->extra; ++i) {
1950 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
1952 fprintf(out, "%*s", indent, "");
1953 fprintf(out, "ERROR: unused div coefficient not zero\n");
1958 static void dump_constraints(struct isl_basic_map *bmap,
1959 isl_int **c, unsigned n,
1960 const char *op, FILE *out, int indent)
1964 for (i = 0; i < n; ++i)
1965 dump_constraint(bmap, c[i], op, out, indent);
1968 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1972 unsigned total = isl_basic_map_total_dim(bmap);
1974 for (j = 0; j < 1 + total; ++j) {
1975 if (isl_int_is_zero(exp[j]))
1977 if (!first && isl_int_is_pos(exp[j]))
1979 dump_term(bmap, exp[j], j, out);
1984 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1988 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1989 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1991 for (i = 0; i < bmap->n_div; ++i) {
1992 fprintf(out, "%*s", indent, "");
1993 fprintf(out, "e%d = [(", i);
1994 dump_affine(bmap, bmap->div[i]+1, out);
1996 isl_int_print(out, bmap->div[i][0], 0);
1997 fprintf(out, "]\n");
2001 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2002 FILE *out, int indent)
2005 fprintf(out, "null basic set\n");
2009 fprintf(out, "%*s", indent, "");
2010 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2011 bset->ref, bset->dim->nparam, bset->dim->n_out,
2012 bset->extra, bset->flags);
2013 dump((struct isl_basic_map *)bset, out, indent);
2016 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2017 FILE *out, int indent)
2020 fprintf(out, "null basic map\n");
2024 fprintf(out, "%*s", indent, "");
2025 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2026 "flags: %x, n_name: %d\n",
2028 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2029 bmap->extra, bmap->flags, bmap->dim->n_id);
2030 dump(bmap, out, indent);
2033 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2038 total = isl_basic_map_total_dim(bmap);
2039 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2040 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2041 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2042 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2046 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2049 struct isl_set *set;
2053 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2054 isl_assert(dim->ctx, n >= 0, goto error);
2055 set = isl_alloc(dim->ctx, struct isl_set,
2056 sizeof(struct isl_set) +
2057 (n - 1) * sizeof(struct isl_basic_set *));
2061 set->ctx = dim->ctx;
2062 isl_ctx_ref(set->ctx);
2070 isl_space_free(dim);
2074 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2075 unsigned nparam, unsigned dim, int n, unsigned flags)
2077 struct isl_set *set;
2080 dims = isl_space_alloc(ctx, nparam, 0, dim);
2084 set = isl_set_alloc_space(dims, n, flags);
2088 /* Make sure "map" has room for at least "n" more basic maps.
2090 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2093 struct isl_map *grown = NULL;
2097 isl_assert(map->ctx, n >= 0, goto error);
2098 if (map->n + n <= map->size)
2100 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2103 for (i = 0; i < map->n; ++i) {
2104 grown->p[i] = isl_basic_map_copy(map->p[i]);
2112 isl_map_free(grown);
2117 /* Make sure "set" has room for at least "n" more basic sets.
2119 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2121 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2124 struct isl_set *isl_set_dup(struct isl_set *set)
2127 struct isl_set *dup;
2132 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2135 for (i = 0; i < set->n; ++i)
2136 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2140 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2142 return isl_map_from_basic_map(bset);
2145 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2147 struct isl_map *map;
2152 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2153 return isl_map_add_basic_map(map, bmap);
2156 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2157 __isl_take isl_basic_set *bset)
2159 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2160 (struct isl_basic_map *)bset);
2163 void isl_set_free(struct isl_set *set)
2173 isl_ctx_deref(set->ctx);
2174 for (i = 0; i < set->n; ++i)
2175 isl_basic_set_free(set->p[i]);
2176 isl_space_free(set->dim);
2180 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2185 fprintf(out, "null set\n");
2189 fprintf(out, "%*s", indent, "");
2190 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2191 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2193 for (i = 0; i < set->n; ++i) {
2194 fprintf(out, "%*s", indent, "");
2195 fprintf(out, "basic set %d:\n", i);
2196 isl_basic_set_print_internal(set->p[i], out, indent+4);
2200 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2205 fprintf(out, "null map\n");
2209 fprintf(out, "%*s", indent, "");
2210 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2211 "flags: %x, n_name: %d\n",
2212 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2213 map->dim->n_out, map->flags, map->dim->n_id);
2214 for (i = 0; i < map->n; ++i) {
2215 fprintf(out, "%*s", indent, "");
2216 fprintf(out, "basic map %d:\n", i);
2217 isl_basic_map_print_internal(map->p[i], out, indent+4);
2221 struct isl_basic_map *isl_basic_map_intersect_domain(
2222 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2224 struct isl_basic_map *bmap_domain;
2229 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2230 bset->dim, isl_dim_param), goto error);
2232 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2233 isl_assert(bset->ctx,
2234 isl_basic_map_compatible_domain(bmap, bset), goto error);
2236 bmap = isl_basic_map_cow(bmap);
2239 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2240 bset->n_div, bset->n_eq, bset->n_ineq);
2241 bmap_domain = isl_basic_map_from_domain(bset);
2242 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2244 bmap = isl_basic_map_simplify(bmap);
2245 return isl_basic_map_finalize(bmap);
2247 isl_basic_map_free(bmap);
2248 isl_basic_set_free(bset);
2252 struct isl_basic_map *isl_basic_map_intersect_range(
2253 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2255 struct isl_basic_map *bmap_range;
2260 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2261 bset->dim, isl_dim_param), goto error);
2263 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2264 isl_assert(bset->ctx,
2265 isl_basic_map_compatible_range(bmap, bset), goto error);
2267 if (isl_basic_set_is_universe(bset)) {
2268 isl_basic_set_free(bset);
2272 bmap = isl_basic_map_cow(bmap);
2275 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2276 bset->n_div, bset->n_eq, bset->n_ineq);
2277 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2278 bmap = add_constraints(bmap, bmap_range, 0, 0);
2280 bmap = isl_basic_map_simplify(bmap);
2281 return isl_basic_map_finalize(bmap);
2283 isl_basic_map_free(bmap);
2284 isl_basic_set_free(bset);
2288 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2294 total = 1 + isl_basic_map_total_dim(bmap);
2295 if (total != vec->size)
2300 for (i = 0; i < bmap->n_eq; ++i) {
2301 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2302 if (!isl_int_is_zero(s)) {
2308 for (i = 0; i < bmap->n_ineq; ++i) {
2309 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2310 if (isl_int_is_neg(s)) {
2321 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2323 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2326 struct isl_basic_map *isl_basic_map_intersect(
2327 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2329 struct isl_vec *sample = NULL;
2331 if (!bmap1 || !bmap2)
2334 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2335 bmap2->dim, isl_dim_param), goto error);
2336 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2337 isl_space_dim(bmap1->dim, isl_dim_param) &&
2338 isl_space_dim(bmap2->dim, isl_dim_all) !=
2339 isl_space_dim(bmap2->dim, isl_dim_param))
2340 return isl_basic_map_intersect(bmap2, bmap1);
2342 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2343 isl_space_dim(bmap2->dim, isl_dim_param))
2344 isl_assert(bmap1->ctx,
2345 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2347 if (bmap1->sample &&
2348 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2349 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2350 sample = isl_vec_copy(bmap1->sample);
2351 else if (bmap2->sample &&
2352 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2353 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2354 sample = isl_vec_copy(bmap2->sample);
2356 bmap1 = isl_basic_map_cow(bmap1);
2359 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2360 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2361 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2364 isl_vec_free(sample);
2366 isl_vec_free(bmap1->sample);
2367 bmap1->sample = sample;
2370 bmap1 = isl_basic_map_simplify(bmap1);
2371 return isl_basic_map_finalize(bmap1);
2374 isl_vec_free(sample);
2375 isl_basic_map_free(bmap1);
2376 isl_basic_map_free(bmap2);
2380 struct isl_basic_set *isl_basic_set_intersect(
2381 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2383 return (struct isl_basic_set *)
2384 isl_basic_map_intersect(
2385 (struct isl_basic_map *)bset1,
2386 (struct isl_basic_map *)bset2);
2389 /* Special case of isl_map_intersect, where both map1 and map2
2390 * are convex, without any divs and such that either map1 or map2
2391 * contains a single constraint. This constraint is then simply
2392 * added to the other map.
2394 static __isl_give isl_map *map_intersect_add_constraint(
2395 __isl_take isl_map *map1, __isl_take isl_map *map2)
2397 isl_assert(map1->ctx, map1->n == 1, goto error);
2398 isl_assert(map2->ctx, map1->n == 1, goto error);
2399 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2400 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2402 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2403 return isl_map_intersect(map2, map1);
2405 isl_assert(map2->ctx,
2406 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2408 map1 = isl_map_cow(map1);
2411 if (isl_map_plain_is_empty(map1)) {
2415 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2416 if (map2->p[0]->n_eq == 1)
2417 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2419 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2420 map2->p[0]->ineq[0]);
2422 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2423 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2427 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2428 isl_basic_map_free(map1->p[0]);
2441 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2442 __isl_take isl_map *map2)
2445 struct isl_map *result;
2451 if (isl_map_plain_is_empty(map1) &&
2452 isl_space_is_equal(map1->dim, map2->dim)) {
2456 if (isl_map_plain_is_empty(map2) &&
2457 isl_space_is_equal(map1->dim, map2->dim)) {
2462 if (map1->n == 1 && map2->n == 1 &&
2463 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2464 isl_space_is_equal(map1->dim, map2->dim) &&
2465 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2466 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2467 return map_intersect_add_constraint(map1, map2);
2468 if (isl_space_dim(map1->dim, isl_dim_all) ==
2469 isl_space_dim(map1->dim, isl_dim_param) &&
2470 isl_space_dim(map2->dim, isl_dim_all) !=
2471 isl_space_dim(map2->dim, isl_dim_param))
2472 return isl_map_intersect(map2, map1);
2474 if (isl_space_dim(map2->dim, isl_dim_all) !=
2475 isl_space_dim(map2->dim, isl_dim_param))
2476 isl_assert(map1->ctx,
2477 isl_space_is_equal(map1->dim, map2->dim), goto error);
2479 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2480 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2481 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2483 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2484 map1->n * map2->n, flags);
2487 for (i = 0; i < map1->n; ++i)
2488 for (j = 0; j < map2->n; ++j) {
2489 struct isl_basic_map *part;
2490 part = isl_basic_map_intersect(
2491 isl_basic_map_copy(map1->p[i]),
2492 isl_basic_map_copy(map2->p[j]));
2493 if (isl_basic_map_is_empty(part))
2494 isl_basic_map_free(part);
2496 result = isl_map_add_basic_map(result, part);
2509 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2510 __isl_take isl_map *map2)
2512 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2515 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2517 return (struct isl_set *)
2518 isl_map_intersect((struct isl_map *)set1,
2519 (struct isl_map *)set2);
2522 /* The current implementation of isl_map_intersect accepts intersections
2523 * with parameter domains, so we can just call that for now.
2525 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2526 __isl_take isl_set *params)
2528 return isl_map_intersect(map, params);
2531 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2532 __isl_take isl_map *map2)
2534 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2537 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2538 __isl_take isl_set *params)
2540 return isl_map_intersect_params(set, params);
2543 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2546 struct isl_basic_set *bset;
2551 bmap = isl_basic_map_cow(bmap);
2554 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2555 in = isl_basic_map_n_in(bmap);
2556 bset = isl_basic_set_from_basic_map(bmap);
2557 bset = isl_basic_set_swap_vars(bset, in);
2558 return isl_basic_map_from_basic_set(bset, dim);
2561 static __isl_give isl_basic_map *basic_map_space_reset(
2562 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2566 if (!isl_space_is_named_or_nested(bmap->dim, type))
2569 space = isl_basic_map_get_space(bmap);
2570 space = isl_space_reset(space, type);
2571 bmap = isl_basic_map_reset_space(bmap, space);
2575 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2576 enum isl_dim_type type, unsigned pos, unsigned n)
2579 struct isl_basic_map *res;
2580 struct isl_dim_map *dim_map;
2581 unsigned total, off;
2582 enum isl_dim_type t;
2585 return basic_map_space_reset(bmap, type);
2590 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2592 total = isl_basic_map_total_dim(bmap) + n;
2593 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2595 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2597 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2599 unsigned size = isl_basic_map_dim(bmap, t);
2600 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2602 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2603 pos, size - pos, off + pos + n);
2605 off += isl_space_dim(res_dim, t);
2607 isl_dim_map_div(dim_map, bmap, off);
2609 res = isl_basic_map_alloc_space(res_dim,
2610 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2611 if (isl_basic_map_is_rational(bmap))
2612 res = isl_basic_map_set_rational(res);
2613 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2614 return isl_basic_map_finalize(res);
2617 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2618 enum isl_dim_type type, unsigned n)
2622 return isl_basic_map_insert(bmap, type,
2623 isl_basic_map_dim(bmap, type), n);
2626 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2627 enum isl_dim_type type, unsigned n)
2631 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2632 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2634 isl_basic_set_free(bset);
2638 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2639 enum isl_dim_type type)
2643 if (!isl_space_is_named_or_nested(map->dim, type))
2646 space = isl_map_get_space(map);
2647 space = isl_space_reset(space, type);
2648 map = isl_map_reset_space(map, space);
2652 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2653 enum isl_dim_type type, unsigned pos, unsigned n)
2658 return map_space_reset(map, type);
2660 map = isl_map_cow(map);
2664 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2668 for (i = 0; i < map->n; ++i) {
2669 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2680 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2681 enum isl_dim_type type, unsigned pos, unsigned n)
2683 return isl_map_insert_dims(set, type, pos, n);
2686 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2687 enum isl_dim_type type, unsigned n)
2691 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2694 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2695 enum isl_dim_type type, unsigned n)
2699 isl_assert(set->ctx, type != isl_dim_in, goto error);
2700 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2706 __isl_give isl_basic_map *isl_basic_map_move_dims(
2707 __isl_take isl_basic_map *bmap,
2708 enum isl_dim_type dst_type, unsigned dst_pos,
2709 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2711 struct isl_dim_map *dim_map;
2712 struct isl_basic_map *res;
2713 enum isl_dim_type t;
2714 unsigned total, off;
2721 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2724 if (dst_type == src_type && dst_pos == src_pos)
2727 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2729 if (pos(bmap->dim, dst_type) + dst_pos ==
2730 pos(bmap->dim, src_type) + src_pos +
2731 ((src_type < dst_type) ? n : 0)) {
2732 bmap = isl_basic_map_cow(bmap);
2736 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2737 src_type, src_pos, n);
2741 bmap = isl_basic_map_finalize(bmap);
2746 total = isl_basic_map_total_dim(bmap);
2747 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2750 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2751 unsigned size = isl_space_dim(bmap->dim, t);
2752 if (t == dst_type) {
2753 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2756 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2759 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2760 dst_pos, size - dst_pos, off);
2761 off += size - dst_pos;
2762 } else if (t == src_type) {
2763 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2766 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2767 src_pos + n, size - src_pos - n, off);
2768 off += size - src_pos - n;
2770 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2774 isl_dim_map_div(dim_map, bmap, off);
2776 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2777 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2778 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2780 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2781 src_type, src_pos, n);
2785 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2786 bmap = isl_basic_map_gauss(bmap, NULL);
2787 bmap = isl_basic_map_finalize(bmap);
2791 isl_basic_map_free(bmap);
2795 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2796 enum isl_dim_type dst_type, unsigned dst_pos,
2797 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2799 return (isl_basic_set *)isl_basic_map_move_dims(
2800 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2803 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2804 enum isl_dim_type dst_type, unsigned dst_pos,
2805 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2809 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2810 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2811 src_type, src_pos, n);
2817 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2818 enum isl_dim_type dst_type, unsigned dst_pos,
2819 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2828 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2831 if (dst_type == src_type && dst_pos == src_pos)
2834 isl_assert(map->ctx, dst_type != src_type, goto error);
2836 map = isl_map_cow(map);
2840 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2844 for (i = 0; i < map->n; ++i) {
2845 map->p[i] = isl_basic_map_move_dims(map->p[i],
2847 src_type, src_pos, n);
2858 /* Move the specified dimensions to the last columns right before
2859 * the divs. Don't change the dimension specification of bmap.
2860 * That's the responsibility of the caller.
2862 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2863 enum isl_dim_type type, unsigned first, unsigned n)
2865 struct isl_dim_map *dim_map;
2866 struct isl_basic_map *res;
2867 enum isl_dim_type t;
2868 unsigned total, off;
2872 if (pos(bmap->dim, type) + first + n ==
2873 1 + isl_space_dim(bmap->dim, isl_dim_all))
2876 total = isl_basic_map_total_dim(bmap);
2877 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2880 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2881 unsigned size = isl_space_dim(bmap->dim, t);
2883 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2886 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2887 first, n, total - bmap->n_div - n);
2888 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2889 first + n, size - (first + n), off);
2890 off += size - (first + n);
2892 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2896 isl_dim_map_div(dim_map, bmap, off + n);
2898 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2899 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2900 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2904 /* Turn the n dimensions of type type, starting at first
2905 * into existentially quantified variables.
2907 __isl_give isl_basic_map *isl_basic_map_project_out(
2908 __isl_take isl_basic_map *bmap,
2909 enum isl_dim_type type, unsigned first, unsigned n)
2922 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2923 return isl_basic_map_remove_dims(bmap, type, first, n);
2925 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2928 bmap = move_last(bmap, type, first, n);
2929 bmap = isl_basic_map_cow(bmap);
2933 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
2934 old = bmap->block2.data;
2935 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2936 (bmap->extra + n) * (1 + row_size));
2937 if (!bmap->block2.data)
2939 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2942 for (i = 0; i < n; ++i) {
2943 new_div[i] = bmap->block2.data +
2944 (bmap->extra + i) * (1 + row_size);
2945 isl_seq_clr(new_div[i], 1 + row_size);
2947 for (i = 0; i < bmap->extra; ++i)
2948 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2950 bmap->div = new_div;
2954 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2957 bmap = isl_basic_map_simplify(bmap);
2958 bmap = isl_basic_map_drop_redundant_divs(bmap);
2959 return isl_basic_map_finalize(bmap);
2961 isl_basic_map_free(bmap);
2965 /* Turn the n dimensions of type type, starting at first
2966 * into existentially quantified variables.
2968 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2969 enum isl_dim_type type, unsigned first, unsigned n)
2971 return (isl_basic_set *)isl_basic_map_project_out(
2972 (isl_basic_map *)bset, type, first, n);
2975 /* Turn the n dimensions of type type, starting at first
2976 * into existentially quantified variables.
2978 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2979 enum isl_dim_type type, unsigned first, unsigned n)
2989 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2991 map = isl_map_cow(map);
2995 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2999 for (i = 0; i < map->n; ++i) {
3000 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3011 /* Turn the n dimensions of type type, starting at first
3012 * into existentially quantified variables.
3014 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3015 enum isl_dim_type type, unsigned first, unsigned n)
3017 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3020 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3024 for (i = 0; i < n; ++i) {
3025 j = isl_basic_map_alloc_div(bmap);
3028 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3032 isl_basic_map_free(bmap);
3036 struct isl_basic_map *isl_basic_map_apply_range(
3037 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3039 isl_space *dim_result = NULL;
3040 struct isl_basic_map *bmap;
3041 unsigned n_in, n_out, n, nparam, total, pos;
3042 struct isl_dim_map *dim_map1, *dim_map2;
3044 if (!bmap1 || !bmap2)
3047 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3048 isl_space_copy(bmap2->dim));
3050 n_in = isl_basic_map_n_in(bmap1);
3051 n_out = isl_basic_map_n_out(bmap2);
3052 n = isl_basic_map_n_out(bmap1);
3053 nparam = isl_basic_map_n_param(bmap1);
3055 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3056 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3057 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3058 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3059 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3060 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3061 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3062 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3063 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3064 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3065 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3067 bmap = isl_basic_map_alloc_space(dim_result,
3068 bmap1->n_div + bmap2->n_div + n,
3069 bmap1->n_eq + bmap2->n_eq,
3070 bmap1->n_ineq + bmap2->n_ineq);
3071 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3072 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3073 bmap = add_divs(bmap, n);
3074 bmap = isl_basic_map_simplify(bmap);
3075 bmap = isl_basic_map_drop_redundant_divs(bmap);
3076 return isl_basic_map_finalize(bmap);
3078 isl_basic_map_free(bmap1);
3079 isl_basic_map_free(bmap2);
3083 struct isl_basic_set *isl_basic_set_apply(
3084 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3089 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3092 return (struct isl_basic_set *)
3093 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3095 isl_basic_set_free(bset);
3096 isl_basic_map_free(bmap);
3100 struct isl_basic_map *isl_basic_map_apply_domain(
3101 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3103 if (!bmap1 || !bmap2)
3106 isl_assert(bmap1->ctx,
3107 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3108 isl_assert(bmap1->ctx,
3109 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3112 bmap1 = isl_basic_map_reverse(bmap1);
3113 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3114 return isl_basic_map_reverse(bmap1);
3116 isl_basic_map_free(bmap1);
3117 isl_basic_map_free(bmap2);
3121 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3122 * A \cap B -> f(A) + f(B)
3124 struct isl_basic_map *isl_basic_map_sum(
3125 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3127 unsigned n_in, n_out, nparam, total, pos;
3128 struct isl_basic_map *bmap = NULL;
3129 struct isl_dim_map *dim_map1, *dim_map2;
3132 if (!bmap1 || !bmap2)
3135 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3138 nparam = isl_basic_map_n_param(bmap1);
3139 n_in = isl_basic_map_n_in(bmap1);
3140 n_out = isl_basic_map_n_out(bmap1);
3142 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3143 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3144 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3145 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3146 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3147 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3148 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3149 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3150 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3151 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3152 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3154 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3155 bmap1->n_div + bmap2->n_div + 2 * n_out,
3156 bmap1->n_eq + bmap2->n_eq + n_out,
3157 bmap1->n_ineq + bmap2->n_ineq);
3158 for (i = 0; i < n_out; ++i) {
3159 int j = isl_basic_map_alloc_equality(bmap);
3162 isl_seq_clr(bmap->eq[j], 1+total);
3163 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3164 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3165 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3167 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3168 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3169 bmap = add_divs(bmap, 2 * n_out);
3171 bmap = isl_basic_map_simplify(bmap);
3172 return isl_basic_map_finalize(bmap);
3174 isl_basic_map_free(bmap);
3175 isl_basic_map_free(bmap1);
3176 isl_basic_map_free(bmap2);
3180 /* Given two maps A -> f(A) and B -> g(B), construct a map
3181 * A \cap B -> f(A) + f(B)
3183 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3185 struct isl_map *result;
3191 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3193 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3194 map1->n * map2->n, 0);
3197 for (i = 0; i < map1->n; ++i)
3198 for (j = 0; j < map2->n; ++j) {
3199 struct isl_basic_map *part;
3200 part = isl_basic_map_sum(
3201 isl_basic_map_copy(map1->p[i]),
3202 isl_basic_map_copy(map2->p[j]));
3203 if (isl_basic_map_is_empty(part))
3204 isl_basic_map_free(part);
3206 result = isl_map_add_basic_map(result, part);
3219 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3220 __isl_take isl_set *set2)
3222 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3225 /* Given a basic map A -> f(A), construct A -> -f(A).
3227 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3232 bmap = isl_basic_map_cow(bmap);
3236 n = isl_basic_map_dim(bmap, isl_dim_out);
3237 off = isl_basic_map_offset(bmap, isl_dim_out);
3238 for (i = 0; i < bmap->n_eq; ++i)
3239 for (j = 0; j < n; ++j)
3240 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3241 for (i = 0; i < bmap->n_ineq; ++i)
3242 for (j = 0; j < n; ++j)
3243 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3244 for (i = 0; i < bmap->n_div; ++i)
3245 for (j = 0; j < n; ++j)
3246 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3247 bmap = isl_basic_map_gauss(bmap, NULL);
3248 return isl_basic_map_finalize(bmap);
3251 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3253 return isl_basic_map_neg(bset);
3256 /* Given a map A -> f(A), construct A -> -f(A).
3258 struct isl_map *isl_map_neg(struct isl_map *map)
3262 map = isl_map_cow(map);
3266 for (i = 0; i < map->n; ++i) {
3267 map->p[i] = isl_basic_map_neg(map->p[i]);
3278 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3280 return (isl_set *)isl_map_neg((isl_map *)set);
3283 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3284 * A -> floor(f(A)/d).
3286 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3289 unsigned n_in, n_out, nparam, total, pos;
3290 struct isl_basic_map *result = NULL;
3291 struct isl_dim_map *dim_map;
3297 nparam = isl_basic_map_n_param(bmap);
3298 n_in = isl_basic_map_n_in(bmap);
3299 n_out = isl_basic_map_n_out(bmap);
3301 total = nparam + n_in + n_out + bmap->n_div + n_out;
3302 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3303 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3304 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3305 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3306 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3308 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3309 bmap->n_div + n_out,
3310 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3311 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3312 result = add_divs(result, n_out);
3313 for (i = 0; i < n_out; ++i) {
3315 j = isl_basic_map_alloc_inequality(result);
3318 isl_seq_clr(result->ineq[j], 1+total);
3319 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3320 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3321 j = isl_basic_map_alloc_inequality(result);
3324 isl_seq_clr(result->ineq[j], 1+total);
3325 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3326 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3327 isl_int_sub_ui(result->ineq[j][0], d, 1);
3330 result = isl_basic_map_simplify(result);
3331 return isl_basic_map_finalize(result);
3333 isl_basic_map_free(result);
3337 /* Given a map A -> f(A) and an integer d, construct a map
3338 * A -> floor(f(A)/d).
3340 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3344 map = isl_map_cow(map);
3348 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3349 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3350 for (i = 0; i < map->n; ++i) {
3351 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3362 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3368 i = isl_basic_map_alloc_equality(bmap);
3371 nparam = isl_basic_map_n_param(bmap);
3372 n_in = isl_basic_map_n_in(bmap);
3373 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3374 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3375 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3376 return isl_basic_map_finalize(bmap);
3378 isl_basic_map_free(bmap);
3382 /* Add a constraints to "bmap" expressing i_pos < o_pos
3384 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3390 i = isl_basic_map_alloc_inequality(bmap);
3393 nparam = isl_basic_map_n_param(bmap);
3394 n_in = isl_basic_map_n_in(bmap);
3395 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3396 isl_int_set_si(bmap->ineq[i][0], -1);
3397 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3398 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3399 return isl_basic_map_finalize(bmap);
3401 isl_basic_map_free(bmap);
3405 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3407 static __isl_give isl_basic_map *var_less_or_equal(
3408 __isl_take isl_basic_map *bmap, unsigned pos)
3414 i = isl_basic_map_alloc_inequality(bmap);
3417 nparam = isl_basic_map_n_param(bmap);
3418 n_in = isl_basic_map_n_in(bmap);
3419 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3420 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3421 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3422 return isl_basic_map_finalize(bmap);
3424 isl_basic_map_free(bmap);
3428 /* Add a constraints to "bmap" expressing i_pos > o_pos
3430 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3436 i = isl_basic_map_alloc_inequality(bmap);
3439 nparam = isl_basic_map_n_param(bmap);
3440 n_in = isl_basic_map_n_in(bmap);
3441 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3442 isl_int_set_si(bmap->ineq[i][0], -1);
3443 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3444 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3445 return isl_basic_map_finalize(bmap);
3447 isl_basic_map_free(bmap);
3451 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3453 static __isl_give isl_basic_map *var_more_or_equal(
3454 __isl_take isl_basic_map *bmap, unsigned pos)
3460 i = isl_basic_map_alloc_inequality(bmap);
3463 nparam = isl_basic_map_n_param(bmap);
3464 n_in = isl_basic_map_n_in(bmap);
3465 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3466 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3467 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3468 return isl_basic_map_finalize(bmap);
3470 isl_basic_map_free(bmap);
3474 __isl_give isl_basic_map *isl_basic_map_equal(
3475 __isl_take isl_space *dim, unsigned n_equal)
3478 struct isl_basic_map *bmap;
3479 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3482 for (i = 0; i < n_equal && bmap; ++i)
3483 bmap = var_equal(bmap, i);
3484 return isl_basic_map_finalize(bmap);
3487 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3489 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3493 struct isl_basic_map *bmap;
3494 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3497 for (i = 0; i < pos && bmap; ++i)
3498 bmap = var_equal(bmap, i);
3500 bmap = var_less(bmap, pos);
3501 return isl_basic_map_finalize(bmap);
3504 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3506 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3507 __isl_take isl_space *dim, unsigned pos)
3510 isl_basic_map *bmap;
3512 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3513 for (i = 0; i < pos; ++i)
3514 bmap = var_equal(bmap, i);
3515 bmap = var_less_or_equal(bmap, pos);
3516 return isl_basic_map_finalize(bmap);
3519 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3521 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3525 struct isl_basic_map *bmap;
3526 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3529 for (i = 0; i < pos && bmap; ++i)
3530 bmap = var_equal(bmap, i);
3532 bmap = var_more(bmap, pos);
3533 return isl_basic_map_finalize(bmap);
3536 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3538 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3539 __isl_take isl_space *dim, unsigned pos)
3542 isl_basic_map *bmap;
3544 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3545 for (i = 0; i < pos; ++i)
3546 bmap = var_equal(bmap, i);
3547 bmap = var_more_or_equal(bmap, pos);
3548 return isl_basic_map_finalize(bmap);
3551 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3552 unsigned n, int equal)
3554 struct isl_map *map;
3557 if (n == 0 && equal)
3558 return isl_map_universe(dims);
3560 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3562 for (i = 0; i + 1 < n; ++i)
3563 map = isl_map_add_basic_map(map,
3564 isl_basic_map_less_at(isl_space_copy(dims), i));
3567 map = isl_map_add_basic_map(map,
3568 isl_basic_map_less_or_equal_at(dims, n - 1));
3570 map = isl_map_add_basic_map(map,
3571 isl_basic_map_less_at(dims, n - 1));
3573 isl_space_free(dims);
3578 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3582 return map_lex_lte_first(dims, dims->n_out, equal);
3585 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3587 return map_lex_lte_first(dim, n, 0);
3590 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3592 return map_lex_lte_first(dim, n, 1);
3595 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3597 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3600 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3602 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3605 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3606 unsigned n, int equal)
3608 struct isl_map *map;
3611 if (n == 0 && equal)
3612 return isl_map_universe(dims);
3614 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3616 for (i = 0; i + 1 < n; ++i)
3617 map = isl_map_add_basic_map(map,
3618 isl_basic_map_more_at(isl_space_copy(dims), i));
3621 map = isl_map_add_basic_map(map,
3622 isl_basic_map_more_or_equal_at(dims, n - 1));
3624 map = isl_map_add_basic_map(map,
3625 isl_basic_map_more_at(dims, n - 1));
3627 isl_space_free(dims);
3632 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3636 return map_lex_gte_first(dims, dims->n_out, equal);
3639 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3641 return map_lex_gte_first(dim, n, 0);
3644 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3646 return map_lex_gte_first(dim, n, 1);
3649 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3651 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3654 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3656 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3659 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3660 __isl_take isl_set *set2)
3663 map = isl_map_lex_le(isl_set_get_space(set1));
3664 map = isl_map_intersect_domain(map, set1);
3665 map = isl_map_intersect_range(map, set2);
3669 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3670 __isl_take isl_set *set2)
3673 map = isl_map_lex_lt(isl_set_get_space(set1));
3674 map = isl_map_intersect_domain(map, set1);
3675 map = isl_map_intersect_range(map, set2);
3679 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3680 __isl_take isl_set *set2)
3683 map = isl_map_lex_ge(isl_set_get_space(set1));
3684 map = isl_map_intersect_domain(map, set1);
3685 map = isl_map_intersect_range(map, set2);
3689 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3690 __isl_take isl_set *set2)
3693 map = isl_map_lex_gt(isl_set_get_space(set1));
3694 map = isl_map_intersect_domain(map, set1);
3695 map = isl_map_intersect_range(map, set2);
3699 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3700 __isl_take isl_map *map2)
3703 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3704 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3705 map = isl_map_apply_range(map, isl_map_reverse(map2));
3709 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3710 __isl_take isl_map *map2)
3713 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3714 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3715 map = isl_map_apply_range(map, isl_map_reverse(map2));
3719 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3720 __isl_take isl_map *map2)
3723 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3724 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3725 map = isl_map_apply_range(map, isl_map_reverse(map2));
3729 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3730 __isl_take isl_map *map2)
3733 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3734 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3735 map = isl_map_apply_range(map, isl_map_reverse(map2));
3739 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3740 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3742 struct isl_basic_map *bmap;
3744 bset = isl_basic_set_cow(bset);
3748 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3749 isl_space_free(bset->dim);
3750 bmap = (struct isl_basic_map *) bset;
3752 return isl_basic_map_finalize(bmap);
3754 isl_basic_set_free(bset);
3755 isl_space_free(dim);
3759 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3763 if (bmap->dim->n_in == 0)
3764 return (struct isl_basic_set *)bmap;
3765 bmap = isl_basic_map_cow(bmap);
3768 bmap->dim = isl_space_as_set_space(bmap->dim);
3771 bmap = isl_basic_map_finalize(bmap);
3772 return (struct isl_basic_set *)bmap;
3774 isl_basic_map_free(bmap);
3778 /* For a div d = floor(f/m), add the constraints
3781 * -(f-(n-1)) + m d >= 0
3783 * Note that the second constraint is the negation of
3787 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3788 unsigned pos, isl_int *div)
3791 unsigned total = isl_basic_map_total_dim(bmap);
3793 i = isl_basic_map_alloc_inequality(bmap);
3796 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3797 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3799 j = isl_basic_map_alloc_inequality(bmap);
3802 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3803 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3804 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3808 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3809 unsigned pos, isl_int *div)
3811 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3815 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3817 unsigned total = isl_basic_map_total_dim(bmap);
3818 unsigned div_pos = total - bmap->n_div + div;
3820 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3824 struct isl_basic_set *isl_basic_map_underlying_set(
3825 struct isl_basic_map *bmap)
3829 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3831 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3832 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3833 return (struct isl_basic_set *)bmap;
3834 bmap = isl_basic_map_cow(bmap);
3837 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3840 bmap->extra -= bmap->n_div;
3842 bmap = isl_basic_map_finalize(bmap);
3843 return (struct isl_basic_set *)bmap;
3848 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3849 __isl_take isl_basic_set *bset)
3851 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3854 struct isl_basic_map *isl_basic_map_overlying_set(
3855 struct isl_basic_set *bset, struct isl_basic_map *like)
3857 struct isl_basic_map *bmap;
3858 struct isl_ctx *ctx;
3865 isl_assert(ctx, bset->n_div == 0, goto error);
3866 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3867 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3869 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3870 isl_basic_map_free(like);
3871 return (struct isl_basic_map *)bset;
3873 bset = isl_basic_set_cow(bset);
3876 total = bset->dim->n_out + bset->extra;
3877 bmap = (struct isl_basic_map *)bset;
3878 isl_space_free(bmap->dim);
3879 bmap->dim = isl_space_copy(like->dim);
3882 bmap->n_div = like->n_div;
3883 bmap->extra += like->n_div;
3887 ltotal = total - bmap->extra + like->extra;
3890 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3891 bmap->extra * (1 + 1 + total));
3892 if (isl_blk_is_error(bmap->block2))
3894 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3898 for (i = 0; i < bmap->extra; ++i)
3899 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3900 for (i = 0; i < like->n_div; ++i) {
3901 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3902 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3904 bmap = isl_basic_map_extend_constraints(bmap,
3905 0, 2 * like->n_div);
3906 for (i = 0; i < like->n_div; ++i) {
3907 if (isl_int_is_zero(bmap->div[i][0]))
3909 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3913 isl_basic_map_free(like);
3914 bmap = isl_basic_map_simplify(bmap);
3915 bmap = isl_basic_map_finalize(bmap);
3918 isl_basic_map_free(like);
3919 isl_basic_set_free(bset);
3923 struct isl_basic_set *isl_basic_set_from_underlying_set(
3924 struct isl_basic_set *bset, struct isl_basic_set *like)
3926 return (struct isl_basic_set *)
3927 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3930 struct isl_set *isl_set_from_underlying_set(
3931 struct isl_set *set, struct isl_basic_set *like)
3937 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3939 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
3940 isl_basic_set_free(like);
3943 set = isl_set_cow(set);
3946 for (i = 0; i < set->n; ++i) {
3947 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3948 isl_basic_set_copy(like));
3952 isl_space_free(set->dim);
3953 set->dim = isl_space_copy(like->dim);
3956 isl_basic_set_free(like);
3959 isl_basic_set_free(like);
3964 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3968 map = isl_map_cow(map);
3971 map->dim = isl_space_cow(map->dim);
3975 for (i = 1; i < map->n; ++i)
3976 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3978 for (i = 0; i < map->n; ++i) {
3979 map->p[i] = (struct isl_basic_map *)
3980 isl_basic_map_underlying_set(map->p[i]);
3985 map->dim = isl_space_underlying(map->dim, 0);
3987 isl_space_free(map->dim);
3988 map->dim = isl_space_copy(map->p[0]->dim);
3992 return (struct isl_set *)map;
3998 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4000 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4003 __isl_give isl_basic_map *isl_basic_map_reset_space(
4004 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4006 bmap = isl_basic_map_cow(bmap);
4010 isl_space_free(bmap->dim);
4013 bmap = isl_basic_map_finalize(bmap);
4017 isl_basic_map_free(bmap);
4018 isl_space_free(dim);
4022 __isl_give isl_basic_set *isl_basic_set_reset_space(
4023 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4025 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4029 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4030 __isl_take isl_space *dim)
4034 map = isl_map_cow(map);
4038 for (i = 0; i < map->n; ++i) {
4039 map->p[i] = isl_basic_map_reset_space(map->p[i],
4040 isl_space_copy(dim));
4044 isl_space_free(map->dim);
4050 isl_space_free(dim);
4054 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4055 __isl_take isl_space *dim)
4057 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4060 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4063 struct isl_basic_set *domain;
4069 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4071 n_in = isl_basic_map_n_in(bmap);
4072 n_out = isl_basic_map_n_out(bmap);
4073 domain = isl_basic_set_from_basic_map(bmap);
4074 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4076 domain = isl_basic_set_reset_space(domain, dim);
4081 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4085 return isl_space_may_be_set(bmap->dim);
4088 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4092 if (isl_basic_map_may_be_set(bmap))
4094 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4097 __isl_give isl_basic_map *isl_basic_map_domain_map(
4098 __isl_take isl_basic_map *bmap)
4102 isl_basic_map *domain;
4103 int nparam, n_in, n_out;
4106 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4107 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4108 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4110 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4111 domain = isl_basic_map_universe(dim);
4113 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4114 bmap = isl_basic_map_apply_range(bmap, domain);
4115 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4117 total = isl_basic_map_total_dim(bmap);
4119 for (i = 0; i < n_in; ++i) {
4120 k = isl_basic_map_alloc_equality(bmap);
4123 isl_seq_clr(bmap->eq[k], 1 + total);
4124 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4125 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4128 bmap = isl_basic_map_gauss(bmap, NULL);
4129 return isl_basic_map_finalize(bmap);
4131 isl_basic_map_free(bmap);
4135 __isl_give isl_basic_map *isl_basic_map_range_map(
4136 __isl_take isl_basic_map *bmap)
4140 isl_basic_map *range;
4141 int nparam, n_in, n_out;
4144 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4145 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4146 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4148 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4149 range = isl_basic_map_universe(dim);
4151 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4152 bmap = isl_basic_map_apply_range(bmap, range);
4153 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4155 total = isl_basic_map_total_dim(bmap);
4157 for (i = 0; i < n_out; ++i) {
4158 k = isl_basic_map_alloc_equality(bmap);
4161 isl_seq_clr(bmap->eq[k], 1 + total);
4162 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4163 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4166 bmap = isl_basic_map_gauss(bmap, NULL);
4167 return isl_basic_map_finalize(bmap);
4169 isl_basic_map_free(bmap);
4173 int isl_map_may_be_set(__isl_keep isl_map *map)
4177 return isl_space_may_be_set(map->dim);
4180 struct isl_set *isl_map_range(struct isl_map *map)
4183 struct isl_set *set;
4187 if (isl_map_may_be_set(map))
4188 return (isl_set *)map;
4190 map = isl_map_cow(map);
4194 set = (struct isl_set *) map;
4195 set->dim = isl_space_range(set->dim);
4198 for (i = 0; i < map->n; ++i) {
4199 set->p[i] = isl_basic_map_range(map->p[i]);
4203 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4204 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4211 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4214 isl_space *domain_dim;
4216 map = isl_map_cow(map);
4220 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4221 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4222 map->dim = isl_space_join(map->dim, domain_dim);
4225 for (i = 0; i < map->n; ++i) {
4226 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4230 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4231 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4238 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4241 isl_space *range_dim;
4243 map = isl_map_cow(map);
4247 range_dim = isl_space_range(isl_map_get_space(map));
4248 range_dim = isl_space_from_range(range_dim);
4249 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4250 map->dim = isl_space_join(map->dim, range_dim);
4253 for (i = 0; i < map->n; ++i) {
4254 map->p[i] = isl_basic_map_range_map(map->p[i]);
4258 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4259 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4266 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4267 __isl_take isl_space *dim)
4270 struct isl_map *map = NULL;
4272 set = isl_set_cow(set);
4275 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4276 map = (struct isl_map *)set;
4277 for (i = 0; i < set->n; ++i) {
4278 map->p[i] = isl_basic_map_from_basic_set(
4279 set->p[i], isl_space_copy(dim));
4283 isl_space_free(map->dim);
4287 isl_space_free(dim);
4292 __isl_give isl_basic_map *isl_basic_map_from_domain(
4293 __isl_take isl_basic_set *bset)
4295 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4298 __isl_give isl_basic_map *isl_basic_map_from_range(
4299 __isl_take isl_basic_set *bset)
4301 return (isl_basic_map *)bset;
4304 struct isl_map *isl_map_from_range(struct isl_set *set)
4306 return (struct isl_map *)set;
4309 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4311 return isl_map_reverse(isl_map_from_range(set));
4314 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4315 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4317 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4318 isl_basic_map_from_range(range));
4321 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4322 __isl_take isl_set *range)
4324 return isl_map_apply_range(isl_map_from_domain(domain),
4325 isl_map_from_range(range));
4328 struct isl_set *isl_set_from_map(struct isl_map *map)
4331 struct isl_set *set = NULL;
4335 map = isl_map_cow(map);
4338 map->dim = isl_space_as_set_space(map->dim);
4341 set = (struct isl_set *)map;
4342 for (i = 0; i < map->n; ++i) {
4343 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4353 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4356 struct isl_map *map;
4360 isl_assert(dim->ctx, n >= 0, return NULL);
4361 map = isl_alloc(dim->ctx, struct isl_map,
4362 sizeof(struct isl_map) +
4363 (n - 1) * sizeof(struct isl_basic_map *));
4367 map->ctx = dim->ctx;
4368 isl_ctx_ref(map->ctx);
4376 isl_space_free(dim);
4380 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4381 unsigned nparam, unsigned in, unsigned out, int n,
4384 struct isl_map *map;
4387 dims = isl_space_alloc(ctx, nparam, in, out);
4391 map = isl_map_alloc_space(dims, n, flags);
4395 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4397 struct isl_basic_map *bmap;
4398 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4399 bmap = isl_basic_map_set_to_empty(bmap);
4403 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4405 struct isl_basic_set *bset;
4406 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4407 bset = isl_basic_set_set_to_empty(bset);
4411 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4413 struct isl_basic_map *bmap;
4416 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4417 bmap = isl_basic_map_set_to_empty(bmap);
4421 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4423 struct isl_basic_map *bmap;
4426 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4427 bmap = isl_basic_map_set_to_empty(bmap);
4431 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4433 struct isl_basic_set *bset;
4436 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4437 bset = isl_basic_set_set_to_empty(bset);
4441 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4443 struct isl_basic_map *bmap;
4444 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4445 bmap = isl_basic_map_finalize(bmap);
4449 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4451 struct isl_basic_set *bset;
4452 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4453 bset = isl_basic_set_finalize(bset);
4457 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4460 unsigned total = isl_space_dim(dim, isl_dim_all);
4461 isl_basic_map *bmap;
4463 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4464 for (i = 0; i < total; ++i) {
4465 int k = isl_basic_map_alloc_inequality(bmap);
4468 isl_seq_clr(bmap->ineq[k], 1 + total);
4469 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4473 isl_basic_map_free(bmap);
4477 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4479 return isl_basic_map_nat_universe(dim);
4482 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4484 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4487 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4489 return isl_map_nat_universe(dim);
4492 __isl_give isl_basic_map *isl_basic_map_universe_like(
4493 __isl_keep isl_basic_map *model)
4497 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4500 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4504 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4507 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4508 __isl_keep isl_set *model)
4512 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4515 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4517 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4520 struct isl_map *isl_map_empty_like(struct isl_map *model)
4524 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4527 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4531 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4534 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4536 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4539 struct isl_set *isl_set_empty_like(struct isl_set *model)
4543 return isl_set_empty(isl_space_copy(model->dim));
4546 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4548 struct isl_map *map;
4551 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4552 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4556 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4558 struct isl_set *set;
4561 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4562 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4566 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4570 return isl_set_universe(isl_space_copy(model->dim));
4573 struct isl_map *isl_map_dup(struct isl_map *map)
4576 struct isl_map *dup;
4580 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4581 for (i = 0; i < map->n; ++i)
4582 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4586 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4587 __isl_take isl_basic_map *bmap)
4591 if (isl_basic_map_plain_is_empty(bmap)) {
4592 isl_basic_map_free(bmap);
4595 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4596 isl_assert(map->ctx, map->n < map->size, goto error);
4597 map->p[map->n] = bmap;
4599 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4605 isl_basic_map_free(bmap);
4609 void isl_map_free(struct isl_map *map)
4619 isl_ctx_deref(map->ctx);
4620 for (i = 0; i < map->n; ++i)
4621 isl_basic_map_free(map->p[i]);
4622 isl_space_free(map->dim);
4626 struct isl_map *isl_map_extend(struct isl_map *base,
4627 unsigned nparam, unsigned n_in, unsigned n_out)
4631 base = isl_map_cow(base);
4635 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4638 for (i = 0; i < base->n; ++i) {
4639 base->p[i] = isl_basic_map_extend_space(base->p[i],
4640 isl_space_copy(base->dim), 0, 0, 0);
4650 struct isl_set *isl_set_extend(struct isl_set *base,
4651 unsigned nparam, unsigned dim)
4653 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4657 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4658 struct isl_basic_map *bmap, unsigned pos, int value)
4662 bmap = isl_basic_map_cow(bmap);
4663 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4664 j = isl_basic_map_alloc_equality(bmap);
4667 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4668 isl_int_set_si(bmap->eq[j][pos], -1);
4669 isl_int_set_si(bmap->eq[j][0], value);
4670 bmap = isl_basic_map_simplify(bmap);
4671 return isl_basic_map_finalize(bmap);
4673 isl_basic_map_free(bmap);
4677 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4678 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4682 bmap = isl_basic_map_cow(bmap);
4683 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4684 j = isl_basic_map_alloc_equality(bmap);
4687 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4688 isl_int_set_si(bmap->eq[j][pos], -1);
4689 isl_int_set(bmap->eq[j][0], value);
4690 bmap = isl_basic_map_simplify(bmap);
4691 return isl_basic_map_finalize(bmap);
4693 isl_basic_map_free(bmap);
4697 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4698 enum isl_dim_type type, unsigned pos, int value)
4702 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4703 return isl_basic_map_fix_pos_si(bmap,
4704 isl_basic_map_offset(bmap, type) + pos, value);
4706 isl_basic_map_free(bmap);
4710 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4711 enum isl_dim_type type, unsigned pos, isl_int value)
4715 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4716 return isl_basic_map_fix_pos(bmap,
4717 isl_basic_map_offset(bmap, type) + pos, value);
4719 isl_basic_map_free(bmap);
4723 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4724 enum isl_dim_type type, unsigned pos, int value)
4726 return (struct isl_basic_set *)
4727 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4731 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4732 enum isl_dim_type type, unsigned pos, isl_int value)
4734 return (struct isl_basic_set *)
4735 isl_basic_map_fix((struct isl_basic_map *)bset,
4739 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4740 unsigned input, int value)
4742 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4745 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4746 unsigned dim, int value)
4748 return (struct isl_basic_set *)
4749 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4750 isl_dim_set, dim, value);
4753 struct isl_map *isl_map_fix_si(struct isl_map *map,
4754 enum isl_dim_type type, unsigned pos, int value)
4758 map = isl_map_cow(map);
4762 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4763 for (i = 0; i < map->n; ++i) {
4764 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4768 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4775 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4776 enum isl_dim_type type, unsigned pos, int value)
4778 return (struct isl_set *)
4779 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4782 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4783 enum isl_dim_type type, unsigned pos, isl_int value)
4787 map = isl_map_cow(map);
4791 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4792 for (i = 0; i < map->n; ++i) {
4793 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4797 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4804 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4805 enum isl_dim_type type, unsigned pos, isl_int value)
4807 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4810 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4811 unsigned input, int value)
4813 return isl_map_fix_si(map, isl_dim_in, input, value);
4816 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4818 return (struct isl_set *)
4819 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4822 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4823 __isl_take isl_basic_map *bmap,
4824 enum isl_dim_type type, unsigned pos, int value)
4830 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4831 pos += isl_basic_map_offset(bmap, type);
4832 bmap = isl_basic_map_cow(bmap);
4833 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4834 j = isl_basic_map_alloc_inequality(bmap);
4837 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4838 isl_int_set_si(bmap->ineq[j][pos], 1);
4839 isl_int_set_si(bmap->ineq[j][0], -value);
4840 bmap = isl_basic_map_simplify(bmap);
4841 return isl_basic_map_finalize(bmap);
4843 isl_basic_map_free(bmap);
4847 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4848 unsigned dim, isl_int value)
4852 bset = isl_basic_set_cow(bset);
4853 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4854 j = isl_basic_set_alloc_inequality(bset);
4857 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4858 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4859 isl_int_neg(bset->ineq[j][0], value);
4860 bset = isl_basic_set_simplify(bset);
4861 return isl_basic_set_finalize(bset);
4863 isl_basic_set_free(bset);
4867 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4868 enum isl_dim_type type, unsigned pos, int value)
4872 map = isl_map_cow(map);
4876 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4877 for (i = 0; i < map->n; ++i) {
4878 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4883 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4890 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4891 enum isl_dim_type type, unsigned pos, int value)
4893 return (struct isl_set *)
4894 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4897 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4902 set = isl_set_cow(set);
4906 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4907 for (i = 0; i < set->n; ++i) {
4908 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4918 struct isl_map *isl_map_reverse(struct isl_map *map)
4922 map = isl_map_cow(map);
4926 map->dim = isl_space_reverse(map->dim);
4929 for (i = 0; i < map->n; ++i) {
4930 map->p[i] = isl_basic_map_reverse(map->p[i]);
4934 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4941 static struct isl_map *isl_basic_map_partial_lexopt(
4942 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4943 struct isl_set **empty, int max)
4947 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4948 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4950 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4952 isl_basic_map_free(bmap);
4953 isl_basic_set_free(dom);
4959 struct isl_map *isl_basic_map_partial_lexmax(
4960 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4961 struct isl_set **empty)
4963 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4966 struct isl_map *isl_basic_map_partial_lexmin(
4967 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4968 struct isl_set **empty)
4970 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4973 struct isl_set *isl_basic_set_partial_lexmin(
4974 struct isl_basic_set *bset, struct isl_basic_set *dom,
4975 struct isl_set **empty)
4977 return (struct isl_set *)
4978 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4982 struct isl_set *isl_basic_set_partial_lexmax(
4983 struct isl_basic_set *bset, struct isl_basic_set *dom,
4984 struct isl_set **empty)
4986 return (struct isl_set *)
4987 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4991 /* Given a basic map "bmap", compute the lexicographically minimal
4992 * (or maximal) image element for each domain element in dom.
4993 * Set *empty to those elements in dom that do not have an image element.
4995 * We first make sure the basic sets in dom are disjoint and then
4996 * simply collect the results over each of the basic sets separately.
4997 * We could probably improve the efficiency a bit by moving the union
4998 * domain down into the parametric integer programming.
5000 static __isl_give isl_map *basic_map_partial_lexopt(
5001 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5002 __isl_give isl_set **empty, int max)
5005 struct isl_map *res;
5007 dom = isl_set_make_disjoint(dom);
5011 if (isl_set_plain_is_empty(dom)) {
5012 res = isl_map_empty_like_basic_map(bmap);
5013 *empty = isl_set_empty_like(dom);
5015 isl_basic_map_free(bmap);
5019 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5020 isl_basic_set_copy(dom->p[0]), empty, max);
5022 for (i = 1; i < dom->n; ++i) {
5023 struct isl_map *res_i;
5024 struct isl_set *empty_i;
5026 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5027 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5029 res = isl_map_union_disjoint(res, res_i);
5030 *empty = isl_set_union_disjoint(*empty, empty_i);
5034 isl_basic_map_free(bmap);
5039 isl_basic_map_free(bmap);
5043 /* Given a map "map", compute the lexicographically minimal
5044 * (or maximal) image element for each domain element in dom.
5045 * Set *empty to those elements in dom that do not have an image element.
5047 * We first compute the lexicographically minimal or maximal element
5048 * in the first basic map. This results in a partial solution "res"
5049 * and a subset "todo" of dom that still need to be handled.
5050 * We then consider each of the remaining maps in "map" and successively
5051 * improve both "res" and "todo".
5053 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5054 * Assume we are computing the lexicographical maximum.
5055 * We first compute the lexicographically maximal element in basic map i.
5056 * This results in a partial solution res_i and a subset todo_i.
5057 * Then we combine these results with those obtain for the first k basic maps
5058 * to obtain a result that is valid for the first k+1 basic maps.
5059 * In particular, the set where there is no solution is the set where
5060 * there is no solution for the first k basic maps and also no solution
5061 * for the ith basic map, i.e.,
5063 * todo^i = todo^k * todo_i
5065 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5066 * solutions, arbitrarily breaking ties in favor of res^k.
5067 * That is, when res^k(a) >= res_i(a), we pick res^k and
5068 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5069 * the lexicographic order.)
5070 * In practice, we compute
5072 * res^k * (res_i . "<=")
5076 * res_i * (res^k . "<")
5078 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5079 * where only one of res^k and res_i provides a solution and we simply pick
5086 * Note that we only compute these intersections when dom(res^k) intersects
5087 * dom(res_i). Otherwise, the only effect of these intersections is to
5088 * potentially break up res^k and res_i into smaller pieces.
5089 * We want to avoid such splintering as much as possible.
5090 * In fact, an earlier implementation of this function would look for
5091 * better results in the domain of res^k and for extra results in todo^k,
5092 * but this would always result in a splintering according to todo^k,
5093 * even when the domain of basic map i is disjoint from the domains of
5094 * the previous basic maps.
5096 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5097 __isl_take isl_map *map, __isl_take isl_set *dom,
5098 __isl_give isl_set **empty, int max)
5101 struct isl_map *res;
5102 struct isl_set *todo;
5107 if (isl_map_plain_is_empty(map)) {
5115 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5116 isl_set_copy(dom), &todo, max);
5118 for (i = 1; i < map->n; ++i) {
5122 isl_space *dim = isl_space_range(isl_map_get_space(res));
5124 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5125 isl_set_copy(dom), &todo_i, max);
5128 lt = isl_map_lex_lt(isl_space_copy(dim));
5129 le = isl_map_lex_le(dim);
5131 lt = isl_map_lex_gt(isl_space_copy(dim));
5132 le = isl_map_lex_ge(dim);
5134 lt = isl_map_apply_range(isl_map_copy(res), lt);
5135 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5136 le = isl_map_apply_range(isl_map_copy(res_i), le);
5137 le = isl_map_intersect(le, isl_map_copy(res));
5139 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5140 res = isl_map_intersect_domain(res,
5141 isl_set_copy(todo_i));
5142 res_i = isl_map_intersect_domain(res_i,
5143 isl_set_copy(todo));
5146 res = isl_map_union_disjoint(res, res_i);
5147 res = isl_map_union_disjoint(res, lt);
5148 res = isl_map_union_disjoint(res, le);
5150 todo = isl_set_intersect(todo, todo_i);
5170 /* Given a map "map", compute the lexicographically minimal
5171 * (or maximal) image element for each domain element in dom.
5172 * Set *empty to those elements in dom that do not have an image element.
5174 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5176 static __isl_give isl_map *isl_map_partial_lexopt(
5177 __isl_take isl_map *map, __isl_take isl_set *dom,
5178 __isl_give isl_set **empty, int max)
5182 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5183 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5184 if (!isl_space_has_named_params(map->dim) ||
5185 !isl_space_has_named_params(dom->dim))
5186 isl_die(map->ctx, isl_error_invalid,
5187 "unaligned unnamed parameters", goto error);
5188 map = isl_map_align_params(map, isl_map_get_space(dom));
5189 dom = isl_map_align_params(dom, isl_map_get_space(map));
5190 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5199 __isl_give isl_map *isl_map_partial_lexmax(
5200 __isl_take isl_map *map, __isl_take isl_set *dom,
5201 __isl_give isl_set **empty)
5203 return isl_map_partial_lexopt(map, dom, empty, 1);
5206 __isl_give isl_map *isl_map_partial_lexmin(
5207 __isl_take isl_map *map, __isl_take isl_set *dom,
5208 __isl_give isl_set **empty)
5210 return isl_map_partial_lexopt(map, dom, empty, 0);
5213 __isl_give isl_set *isl_set_partial_lexmin(
5214 __isl_take isl_set *set, __isl_take isl_set *dom,
5215 __isl_give isl_set **empty)
5217 return (struct isl_set *)
5218 isl_map_partial_lexmin((struct isl_map *)set,
5222 __isl_give isl_set *isl_set_partial_lexmax(
5223 __isl_take isl_set *set, __isl_take isl_set *dom,
5224 __isl_give isl_set **empty)
5226 return (struct isl_set *)
5227 isl_map_partial_lexmax((struct isl_map *)set,
5231 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5233 struct isl_basic_set *dom = NULL;
5238 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5239 dom = isl_basic_set_universe(dom_dim);
5240 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5242 isl_basic_map_free(bmap);
5246 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5248 return isl_basic_map_lexopt(bmap, 0);
5251 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5253 return isl_basic_map_lexopt(bmap, 1);
5256 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5258 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5261 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5263 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5266 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5268 struct isl_set *dom = NULL;
5273 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5274 dom = isl_set_universe(dom_dim);
5275 return isl_map_partial_lexopt(map, dom, NULL, max);
5281 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5283 return isl_map_lexopt(map, 0);
5286 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5288 return isl_map_lexopt(map, 1);
5291 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5293 return (isl_set *)isl_map_lexmin((isl_map *)set);
5296 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5298 return (isl_set *)isl_map_lexmax((isl_map *)set);
5301 /* Construct a map that equates the two given dimensions in the given space.
5303 static __isl_give isl_map *equate(__isl_take isl_space *dim,
5304 enum isl_dim_type src_type, int src_pos,
5305 enum isl_dim_type dst_type, int dst_pos)
5307 isl_basic_map *bmap;
5310 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5311 k = isl_basic_map_alloc_equality(bmap);
5314 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
5315 src_pos += isl_basic_map_offset(bmap, src_type);
5316 dst_pos += isl_basic_map_offset(bmap, dst_type);
5317 isl_int_set_si(bmap->eq[k][src_pos], 1);
5318 isl_int_set_si(bmap->eq[k][dst_pos], -1);
5320 return isl_map_from_basic_map(bmap);
5322 isl_basic_map_free(bmap);
5326 /* Extract the first and only affine expression from list
5327 * and then add it to *pwaff with the given dom.
5328 * This domain is known to be disjoint from other domains
5329 * because of the way isl_basic_set_foreach_lexmax works.
5331 static int update_dim_opt(__isl_take isl_basic_set *dom,
5332 __isl_take isl_aff_list *list, void *user)
5334 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5336 isl_pw_aff **pwaff = user;
5337 isl_pw_aff *pwaff_i;
5339 if (isl_aff_list_n_aff(list) != 1)
5340 isl_die(ctx, isl_error_internal,
5341 "expecting single element list", goto error);
5343 aff = isl_aff_list_get_aff(list, 0);
5344 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5346 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5348 isl_aff_list_free(list);
5352 isl_basic_set_free(dom);
5353 isl_aff_list_free(list);
5357 /* Given a one-dimensional basic set, compute the minimum or maximum of that
5358 * dimension as an isl_pw_aff.
5360 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexopt
5361 * call update_dim_opt on each leaf of the result.
5363 static __isl_give isl_pw_aff *basic_set_dim_opt(__isl_keep isl_basic_set *bset,
5366 isl_space *dim = isl_basic_set_get_space(bset);
5370 dim = isl_space_domain(isl_space_from_range(dim));
5371 pwaff = isl_pw_aff_empty(dim);
5373 r = isl_basic_set_foreach_lexopt(bset, max, &update_dim_opt, &pwaff);
5375 return isl_pw_aff_free(pwaff);
5380 /* Compute the minimum or maximum of the given set dimension
5381 * as a function of the parameters, but independently of
5382 * the other set dimensions.
5384 * We first project the set onto the given dimension and then compute
5385 * the "lexicographic" maximum in each basic set, combining the results
5386 * using isl_pw_aff_union_max.
5388 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5395 map = isl_map_from_domain(set);
5396 map = isl_map_add_dims(map, isl_dim_out, 1);
5397 map = isl_map_intersect(map,
5398 equate(isl_map_get_space(map), isl_dim_in, pos,
5400 set = isl_map_range(map);
5405 isl_space *dim = isl_set_get_space(set);
5406 dim = isl_space_domain(isl_space_from_range(dim));
5408 return isl_pw_aff_empty(dim);
5411 pwaff = basic_set_dim_opt(set->p[0], max);
5412 for (i = 1; i < set->n; ++i) {
5413 isl_pw_aff *pwaff_i;
5415 pwaff_i = basic_set_dim_opt(set->p[i], max);
5416 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5424 /* Compute the maximum of the given set dimension as a function of the
5425 * parameters, but independently of the other set dimensions.
5427 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5429 return set_dim_opt(set, pos, 1);
5432 /* Compute the minimum of the given set dimension as a function of the
5433 * parameters, but independently of the other set dimensions.
5435 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5437 return set_dim_opt(set, pos, 0);
5440 /* Apply a preimage specified by "mat" on the parameters of "bset".
5441 * bset is assumed to have only parameters and divs.
5443 static struct isl_basic_set *basic_set_parameter_preimage(
5444 struct isl_basic_set *bset, struct isl_mat *mat)
5451 bset->dim = isl_space_cow(bset->dim);
5455 nparam = isl_basic_set_dim(bset, isl_dim_param);
5457 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5459 bset->dim->nparam = 0;
5460 bset->dim->n_out = nparam;
5461 bset = isl_basic_set_preimage(bset, mat);
5463 bset->dim->nparam = bset->dim->n_out;
5464 bset->dim->n_out = 0;
5469 isl_basic_set_free(bset);
5473 /* Apply a preimage specified by "mat" on the parameters of "set".
5474 * set is assumed to have only parameters and divs.
5476 static struct isl_set *set_parameter_preimage(
5477 struct isl_set *set, struct isl_mat *mat)
5479 isl_space *dim = NULL;
5485 dim = isl_space_copy(set->dim);
5486 dim = isl_space_cow(dim);
5490 nparam = isl_set_dim(set, isl_dim_param);
5492 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5495 dim->n_out = nparam;
5496 isl_set_reset_space(set, dim);
5497 set = isl_set_preimage(set, mat);
5500 dim = isl_space_copy(set->dim);
5501 dim = isl_space_cow(dim);
5504 dim->nparam = dim->n_out;
5506 isl_set_reset_space(set, dim);
5509 isl_space_free(dim);
5516 /* Intersect the basic set "bset" with the affine space specified by the
5517 * equalities in "eq".
5519 static struct isl_basic_set *basic_set_append_equalities(
5520 struct isl_basic_set *bset, struct isl_mat *eq)
5528 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5533 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5534 for (i = 0; i < eq->n_row; ++i) {
5535 k = isl_basic_set_alloc_equality(bset);
5538 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5539 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5543 bset = isl_basic_set_gauss(bset, NULL);
5544 bset = isl_basic_set_finalize(bset);
5549 isl_basic_set_free(bset);
5553 /* Intersect the set "set" with the affine space specified by the
5554 * equalities in "eq".
5556 static struct isl_set *set_append_equalities(struct isl_set *set,
5564 for (i = 0; i < set->n; ++i) {
5565 set->p[i] = basic_set_append_equalities(set->p[i],
5578 /* Project the given basic set onto its parameter domain, possibly introducing
5579 * new, explicit, existential variables in the constraints.
5580 * The input has parameters and (possibly implicit) existential variables.
5581 * The output has the same parameters, but only
5582 * explicit existentially quantified variables.
5584 * The actual projection is performed by pip, but pip doesn't seem
5585 * to like equalities very much, so we first remove the equalities
5586 * among the parameters by performing a variable compression on
5587 * the parameters. Afterward, an inverse transformation is performed
5588 * and the equalities among the parameters are inserted back in.
5590 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5594 struct isl_mat *T, *T2;
5595 struct isl_set *set;
5596 unsigned nparam, n_div;
5598 bset = isl_basic_set_cow(bset);
5602 if (bset->n_eq == 0)
5603 return isl_basic_set_lexmin(bset);
5605 isl_basic_set_gauss(bset, NULL);
5607 nparam = isl_basic_set_dim(bset, isl_dim_param);
5608 n_div = isl_basic_set_dim(bset, isl_dim_div);
5610 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5611 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5614 if (i == bset->n_eq)
5615 return isl_basic_set_lexmin(bset);
5617 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5619 eq = isl_mat_cow(eq);
5620 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5621 if (T && T->n_col == 0) {
5625 bset = isl_basic_set_set_to_empty(bset);
5626 return isl_set_from_basic_set(bset);
5628 bset = basic_set_parameter_preimage(bset, T);
5630 set = isl_basic_set_lexmin(bset);
5631 set = set_parameter_preimage(set, T2);
5632 set = set_append_equalities(set, eq);
5636 /* Compute an explicit representation for all the existentially
5637 * quantified variables.
5638 * The input and output dimensions are first turned into parameters.
5639 * compute_divs then returns a map with the same parameters and
5640 * no input or output dimensions and the dimension specification
5641 * is reset to that of the input.
5643 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5645 struct isl_basic_set *bset;
5646 struct isl_set *set;
5647 struct isl_map *map;
5648 isl_space *dim, *orig_dim = NULL;
5653 bmap = isl_basic_map_cow(bmap);
5657 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5658 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5659 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5660 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5664 orig_dim = bmap->dim;
5666 bset = (struct isl_basic_set *)bmap;
5668 set = parameter_compute_divs(bset);
5669 map = (struct isl_map *)set;
5670 map = isl_map_reset_space(map, orig_dim);
5674 isl_basic_map_free(bmap);
5678 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5686 off = isl_space_dim(bmap->dim, isl_dim_all);
5687 for (i = 0; i < bmap->n_div; ++i) {
5688 if (isl_int_is_zero(bmap->div[i][0]))
5690 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5696 static int map_divs_known(__isl_keep isl_map *map)
5703 for (i = 0; i < map->n; ++i) {
5704 int known = isl_basic_map_divs_known(map->p[i]);
5712 /* If bmap contains any unknown divs, then compute explicit
5713 * expressions for them. However, this computation may be
5714 * quite expensive, so first try to remove divs that aren't
5717 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5720 struct isl_map *map;
5722 known = isl_basic_map_divs_known(bmap);
5726 return isl_map_from_basic_map(bmap);
5728 bmap = isl_basic_map_drop_redundant_divs(bmap);
5730 known = isl_basic_map_divs_known(bmap);
5734 return isl_map_from_basic_map(bmap);
5736 map = compute_divs(bmap);
5739 isl_basic_map_free(bmap);
5743 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5747 struct isl_map *res;
5754 known = map_divs_known(map);
5762 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5763 for (i = 1 ; i < map->n; ++i) {
5765 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5766 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5767 res = isl_map_union_disjoint(res, r2);
5769 res = isl_map_union(res, r2);
5776 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5778 return (struct isl_set *)
5779 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5782 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5784 return (struct isl_set *)
5785 isl_map_compute_divs((struct isl_map *)set);
5788 struct isl_set *isl_map_domain(struct isl_map *map)
5791 struct isl_set *set;
5796 map = isl_map_cow(map);
5800 set = (struct isl_set *)map;
5801 set->dim = isl_space_domain(set->dim);
5804 for (i = 0; i < map->n; ++i) {
5805 set->p[i] = isl_basic_map_domain(map->p[i]);
5809 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5810 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5817 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
5818 __isl_take isl_map *map2)
5822 struct isl_map *map = NULL;
5836 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
5838 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5839 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5840 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5842 map = isl_map_alloc_space(isl_space_copy(map1->dim),
5843 map1->n + map2->n, flags);
5846 for (i = 0; i < map1->n; ++i) {
5847 map = isl_map_add_basic_map(map,
5848 isl_basic_map_copy(map1->p[i]));
5852 for (i = 0; i < map2->n; ++i) {
5853 map = isl_map_add_basic_map(map,
5854 isl_basic_map_copy(map2->p[i]));
5868 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
5869 __isl_take isl_map *map2)
5871 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
5874 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5876 map1 = isl_map_union_disjoint(map1, map2);
5880 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5884 struct isl_set *isl_set_union_disjoint(
5885 struct isl_set *set1, struct isl_set *set2)
5887 return (struct isl_set *)
5888 isl_map_union_disjoint(
5889 (struct isl_map *)set1, (struct isl_map *)set2);
5892 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5894 return (struct isl_set *)
5895 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5898 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
5899 __isl_take isl_set *set)
5902 struct isl_map *result;
5908 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5909 isl_die(set->ctx, isl_error_invalid,
5910 "parameters don't match", goto error);
5912 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
5913 !isl_map_compatible_range(map, set))
5914 isl_die(set->ctx, isl_error_invalid,
5915 "incompatible spaces", goto error);
5917 if (isl_set_plain_is_universe(set)) {
5922 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5923 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5924 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5926 result = isl_map_alloc_space(isl_space_copy(map->dim),
5927 map->n * set->n, flags);
5930 for (i = 0; i < map->n; ++i)
5931 for (j = 0; j < set->n; ++j) {
5932 result = isl_map_add_basic_map(result,
5933 isl_basic_map_intersect_range(
5934 isl_basic_map_copy(map->p[i]),
5935 isl_basic_set_copy(set->p[j])));
5948 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
5949 __isl_take isl_set *set)
5951 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
5954 struct isl_map *isl_map_intersect_domain(
5955 struct isl_map *map, struct isl_set *set)
5957 return isl_map_reverse(
5958 isl_map_intersect_range(isl_map_reverse(map), set));
5961 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
5962 __isl_take isl_map *map2)
5966 map1 = isl_map_reverse(map1);
5967 map1 = isl_map_apply_range(map1, map2);
5968 return isl_map_reverse(map1);
5975 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
5976 __isl_take isl_map *map2)
5978 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
5981 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
5982 __isl_take isl_map *map2)
5984 isl_space *dim_result;
5985 struct isl_map *result;
5991 dim_result = isl_space_join(isl_space_copy(map1->dim),
5992 isl_space_copy(map2->dim));
5994 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
5997 for (i = 0; i < map1->n; ++i)
5998 for (j = 0; j < map2->n; ++j) {
5999 result = isl_map_add_basic_map(result,
6000 isl_basic_map_apply_range(
6001 isl_basic_map_copy(map1->p[i]),
6002 isl_basic_map_copy(map2->p[j])));
6008 if (result && result->n <= 1)
6009 ISL_F_SET(result, ISL_MAP_DISJOINT);
6017 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6018 __isl_take isl_map *map2)
6020 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6024 * returns range - domain
6026 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6028 isl_space *dims, *target_dim;
6029 struct isl_basic_set *bset;
6036 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6037 bmap->dim, isl_dim_out),
6039 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6040 dim = isl_basic_map_n_in(bmap);
6041 nparam = isl_basic_map_n_param(bmap);
6042 bset = isl_basic_set_from_basic_map(bmap);
6043 bset = isl_basic_set_cow(bset);
6044 dims = isl_basic_set_get_space(bset);
6045 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6046 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6047 bset = isl_basic_set_swap_vars(bset, 2*dim);
6048 for (i = 0; i < dim; ++i) {
6049 int j = isl_basic_map_alloc_equality(
6050 (struct isl_basic_map *)bset);
6053 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6054 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6055 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6056 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6058 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6059 bset = isl_basic_set_reset_space(bset, target_dim);
6062 isl_basic_map_free(bmap);
6067 * returns range - domain
6069 struct isl_set *isl_map_deltas(struct isl_map *map)
6073 struct isl_set *result;
6078 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6079 map->dim, isl_dim_out),
6081 dim = isl_map_get_space(map);
6082 dim = isl_space_domain(dim);
6083 result = isl_set_alloc_space(dim, map->n, 0);
6086 for (i = 0; i < map->n; ++i)
6087 result = isl_set_add_basic_set(result,
6088 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6097 * returns [domain -> range] -> range - domain
6099 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6100 __isl_take isl_basic_map *bmap)
6104 isl_basic_map *domain;
6108 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6109 isl_die(bmap->ctx, isl_error_invalid,
6110 "domain and range don't match", goto error);
6112 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6113 n = isl_basic_map_dim(bmap, isl_dim_in);
6115 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6116 domain = isl_basic_map_universe(dim);
6118 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6119 bmap = isl_basic_map_apply_range(bmap, domain);
6120 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6122 total = isl_basic_map_total_dim(bmap);
6124 for (i = 0; i < n; ++i) {
6125 k = isl_basic_map_alloc_equality(bmap);
6128 isl_seq_clr(bmap->eq[k], 1 + total);
6129 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6130 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6131 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6134 bmap = isl_basic_map_gauss(bmap, NULL);
6135 return isl_basic_map_finalize(bmap);
6137 isl_basic_map_free(bmap);
6142 * returns [domain -> range] -> range - domain
6144 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6147 isl_space *domain_dim;
6152 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6153 isl_die(map->ctx, isl_error_invalid,
6154 "domain and range don't match", goto error);
6156 map = isl_map_cow(map);
6160 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6161 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6162 map->dim = isl_space_join(map->dim, domain_dim);
6165 for (i = 0; i < map->n; ++i) {
6166 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6170 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6177 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6179 struct isl_basic_map *bmap;
6187 nparam = dims->nparam;
6189 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6193 for (i = 0; i < dim; ++i) {
6194 int j = isl_basic_map_alloc_equality(bmap);
6197 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6198 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6199 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6201 return isl_basic_map_finalize(bmap);
6203 isl_basic_map_free(bmap);
6207 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6211 if (dim->n_in != dim->n_out)
6212 isl_die(dim->ctx, isl_error_invalid,
6213 "number of input and output dimensions needs to be "
6214 "the same", goto error);
6215 return basic_map_identity(dim);
6217 isl_space_free(dim);
6221 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6223 if (!model || !model->dim)
6225 return isl_basic_map_identity(isl_space_copy(model->dim));
6228 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6230 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6233 struct isl_map *isl_map_identity_like(struct isl_map *model)
6235 if (!model || !model->dim)
6237 return isl_map_identity(isl_space_copy(model->dim));
6240 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6242 if (!model || !model->dim)
6244 return isl_map_identity(isl_space_copy(model->dim));
6247 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6249 isl_space *dim = isl_set_get_space(set);
6251 id = isl_map_identity(isl_space_map_from_set(dim));
6252 return isl_map_intersect_range(id, set);
6255 /* Construct a basic set with all set dimensions having only non-negative
6258 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6259 __isl_take isl_space *space)
6264 struct isl_basic_set *bset;
6268 nparam = space->nparam;
6270 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6273 for (i = 0; i < dim; ++i) {
6274 int k = isl_basic_set_alloc_inequality(bset);
6277 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6278 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6282 isl_basic_set_free(bset);
6286 /* Construct the half-space x_pos >= 0.
6288 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6292 isl_basic_set *nonneg;
6294 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6295 k = isl_basic_set_alloc_inequality(nonneg);
6298 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6299 isl_int_set_si(nonneg->ineq[k][pos], 1);
6301 return isl_basic_set_finalize(nonneg);
6303 isl_basic_set_free(nonneg);
6307 /* Construct the half-space x_pos <= -1.
6309 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6314 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6315 k = isl_basic_set_alloc_inequality(neg);
6318 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6319 isl_int_set_si(neg->ineq[k][0], -1);
6320 isl_int_set_si(neg->ineq[k][pos], -1);
6322 return isl_basic_set_finalize(neg);
6324 isl_basic_set_free(neg);
6328 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6329 enum isl_dim_type type, unsigned first, unsigned n)
6332 isl_basic_set *nonneg;
6340 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6342 for (i = 0; i < n; ++i) {
6343 nonneg = nonneg_halfspace(isl_set_get_space(set),
6344 pos(set->dim, type) + first + i);
6345 neg = neg_halfspace(isl_set_get_space(set),
6346 pos(set->dim, type) + first + i);
6348 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6357 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6358 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6365 if (isl_set_plain_is_empty(set)) {
6370 return fn(set, signs, user);
6373 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6375 half = isl_set_intersect(half, isl_set_copy(set));
6376 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6380 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6382 half = isl_set_intersect(half, set);
6383 return foreach_orthant(half, signs, first + 1, len, fn, user);
6389 /* Call "fn" on the intersections of "set" with each of the orthants
6390 * (except for obviously empty intersections). The orthant is identified
6391 * by the signs array, with each entry having value 1 or -1 according
6392 * to the sign of the corresponding variable.
6394 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6395 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6405 if (isl_set_plain_is_empty(set))
6408 nparam = isl_set_dim(set, isl_dim_param);
6409 nvar = isl_set_dim(set, isl_dim_set);
6411 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6413 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6421 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6423 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6426 int isl_basic_map_is_subset(
6427 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6430 struct isl_map *map1;
6431 struct isl_map *map2;
6433 if (!bmap1 || !bmap2)
6436 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6437 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6439 is_subset = isl_map_is_subset(map1, map2);
6447 int isl_basic_map_is_equal(
6448 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6452 if (!bmap1 || !bmap2)
6454 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6457 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6461 int isl_basic_set_is_equal(
6462 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6464 return isl_basic_map_is_equal(
6465 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6468 int isl_map_is_empty(struct isl_map *map)
6475 for (i = 0; i < map->n; ++i) {
6476 is_empty = isl_basic_map_is_empty(map->p[i]);
6485 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6487 return map ? map->n == 0 : -1;
6490 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6492 return isl_map_plain_is_empty(map);
6495 int isl_set_plain_is_empty(struct isl_set *set)
6497 return set ? set->n == 0 : -1;
6500 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6502 return isl_set_plain_is_empty(set);
6505 int isl_set_is_empty(struct isl_set *set)
6507 return isl_map_is_empty((struct isl_map *)set);
6510 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6515 return isl_space_is_equal(map1->dim, map2->dim);
6518 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6523 return isl_space_is_equal(set1->dim, set2->dim);
6526 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6532 is_subset = isl_map_is_subset(map1, map2);
6535 is_subset = isl_map_is_subset(map2, map1);
6539 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6541 return align_params_map_map_and_test(map1, map2, &map_is_equal);
6544 int isl_basic_map_is_strict_subset(
6545 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6549 if (!bmap1 || !bmap2)
6551 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6554 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6555 if (is_subset == -1)
6560 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6566 is_subset = isl_map_is_subset(map1, map2);
6569 is_subset = isl_map_is_subset(map2, map1);
6570 if (is_subset == -1)
6575 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6577 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6580 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6584 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6587 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6591 return bset->n_eq == 0 && bset->n_ineq == 0;
6594 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6601 for (i = 0; i < map->n; ++i) {
6602 int r = isl_basic_map_is_universe(map->p[i]);
6610 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6612 return isl_map_plain_is_universe((isl_map *) set);
6615 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6617 return isl_set_plain_is_universe(set);
6620 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6622 struct isl_basic_set *bset = NULL;
6623 struct isl_vec *sample = NULL;
6630 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6633 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6634 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6635 copy = isl_basic_map_remove_redundancies(copy);
6636 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6637 isl_basic_map_free(copy);
6641 total = 1 + isl_basic_map_total_dim(bmap);
6642 if (bmap->sample && bmap->sample->size == total) {
6643 int contains = isl_basic_map_contains(bmap, bmap->sample);
6649 isl_vec_free(bmap->sample);
6650 bmap->sample = NULL;
6651 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6654 sample = isl_basic_set_sample_vec(bset);
6657 empty = sample->size == 0;
6658 isl_vec_free(bmap->sample);
6659 bmap->sample = sample;
6661 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6666 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6670 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6673 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6675 return isl_basic_map_plain_is_empty(bmap);
6678 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6682 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6685 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6687 return isl_basic_set_plain_is_empty(bset);
6690 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6692 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6695 struct isl_map *isl_basic_map_union(
6696 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6698 struct isl_map *map;
6699 if (!bmap1 || !bmap2)
6702 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
6704 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
6707 map = isl_map_add_basic_map(map, bmap1);
6708 map = isl_map_add_basic_map(map, bmap2);
6711 isl_basic_map_free(bmap1);
6712 isl_basic_map_free(bmap2);
6716 struct isl_set *isl_basic_set_union(
6717 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6719 return (struct isl_set *)isl_basic_map_union(
6720 (struct isl_basic_map *)bset1,
6721 (struct isl_basic_map *)bset2);
6724 /* Order divs such that any div only depends on previous divs */
6725 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6733 off = isl_space_dim(bmap->dim, isl_dim_all);
6735 for (i = 0; i < bmap->n_div; ++i) {
6737 if (isl_int_is_zero(bmap->div[i][0]))
6739 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6743 isl_basic_map_swap_div(bmap, i, i + pos);
6749 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6751 return (struct isl_basic_set *)
6752 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6755 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6762 for (i = 0; i < map->n; ++i) {
6763 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6774 /* Apply the expansion computed by isl_merge_divs.
6775 * The expansion itself is given by "exp" while the resulting
6776 * list of divs is given by "div".
6778 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6779 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6784 bset = isl_basic_set_cow(bset);
6788 if (div->n_row < bset->n_div)
6789 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6790 "not an expansion", goto error);
6792 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
6793 div->n_row - bset->n_div, 0,
6794 2 * (div->n_row - bset->n_div));
6796 n_div = bset->n_div;
6797 for (i = n_div; i < div->n_row; ++i)
6798 if (isl_basic_set_alloc_div(bset) < 0)
6802 for (i = div->n_row - 1; i >= 0; --i) {
6803 if (j >= 0 && exp[j] == i) {
6805 isl_basic_map_swap_div(bset, i, j);
6808 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6809 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6817 isl_basic_set_free(bset);
6822 /* Look for a div in dst that corresponds to the div "div" in src.
6823 * The divs before "div" in src and dst are assumed to be the same.
6825 * Returns -1 if no corresponding div was found and the position
6826 * of the corresponding div in dst otherwise.
6828 static int find_div(struct isl_basic_map *dst,
6829 struct isl_basic_map *src, unsigned div)
6833 unsigned total = isl_space_dim(src->dim, isl_dim_all);
6835 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6836 for (i = div; i < dst->n_div; ++i)
6837 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6838 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6839 dst->n_div - div) == -1)
6844 struct isl_basic_map *isl_basic_map_align_divs(
6845 struct isl_basic_map *dst, struct isl_basic_map *src)
6848 unsigned total = isl_space_dim(src->dim, isl_dim_all);
6853 if (src->n_div == 0)
6856 for (i = 0; i < src->n_div; ++i)
6857 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6859 src = isl_basic_map_order_divs(src);
6860 dst = isl_basic_map_cow(dst);
6861 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
6862 src->n_div, 0, 2 * src->n_div);
6865 for (i = 0; i < src->n_div; ++i) {
6866 int j = find_div(dst, src, i);
6868 j = isl_basic_map_alloc_div(dst);
6871 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6872 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6873 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6877 isl_basic_map_swap_div(dst, i, j);
6881 isl_basic_map_free(dst);
6885 struct isl_basic_set *isl_basic_set_align_divs(
6886 struct isl_basic_set *dst, struct isl_basic_set *src)
6888 return (struct isl_basic_set *)isl_basic_map_align_divs(
6889 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6892 struct isl_map *isl_map_align_divs(struct isl_map *map)
6900 map = isl_map_compute_divs(map);
6901 map = isl_map_cow(map);
6905 for (i = 1; i < map->n; ++i)
6906 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6907 for (i = 1; i < map->n; ++i)
6908 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6910 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6914 struct isl_set *isl_set_align_divs(struct isl_set *set)
6916 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6919 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
6920 __isl_take isl_map *map)
6924 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6925 map = isl_map_intersect_domain(map, set);
6926 set = isl_map_range(map);
6934 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
6935 __isl_take isl_map *map)
6937 return isl_map_align_params_map_map_and(set, map, &set_apply);
6940 /* There is no need to cow as removing empty parts doesn't change
6941 * the meaning of the set.
6943 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6950 for (i = map->n-1; i >= 0; --i) {
6951 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6953 isl_basic_map_free(map->p[i]);
6954 if (i != map->n-1) {
6955 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6956 map->p[i] = map->p[map->n-1];
6964 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6966 return (struct isl_set *)
6967 isl_map_remove_empty_parts((struct isl_map *)set);
6970 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6972 struct isl_basic_map *bmap;
6973 if (!map || map->n == 0)
6975 bmap = map->p[map->n-1];
6976 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6977 return isl_basic_map_copy(bmap);
6980 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6982 return (struct isl_basic_set *)
6983 isl_map_copy_basic_map((struct isl_map *)set);
6986 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6987 __isl_keep isl_basic_map *bmap)
6993 for (i = map->n-1; i >= 0; --i) {
6994 if (map->p[i] != bmap)
6996 map = isl_map_cow(map);
6999 isl_basic_map_free(map->p[i]);
7000 if (i != map->n-1) {
7001 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7002 map->p[i] = map->p[map->n-1];
7013 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7014 struct isl_basic_set *bset)
7016 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7017 (struct isl_basic_map *)bset);
7020 /* Given two basic sets bset1 and bset2, compute the maximal difference
7021 * between the values of dimension pos in bset1 and those in bset2
7022 * for any common value of the parameters and dimensions preceding pos.
7024 static enum isl_lp_result basic_set_maximal_difference_at(
7025 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7026 int pos, isl_int *opt)
7029 struct isl_basic_map *bmap1 = NULL;
7030 struct isl_basic_map *bmap2 = NULL;
7031 struct isl_ctx *ctx;
7032 struct isl_vec *obj;
7035 unsigned dim1, dim2;
7036 enum isl_lp_result res;
7038 if (!bset1 || !bset2)
7039 return isl_lp_error;
7041 nparam = isl_basic_set_n_param(bset1);
7042 dim1 = isl_basic_set_n_dim(bset1);
7043 dim2 = isl_basic_set_n_dim(bset2);
7044 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7045 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7046 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7047 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7048 if (!bmap1 || !bmap2)
7050 bmap1 = isl_basic_map_cow(bmap1);
7051 bmap1 = isl_basic_map_extend(bmap1, nparam,
7052 pos, (dim1 - pos) + (dim2 - pos),
7053 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7054 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7057 total = isl_basic_map_total_dim(bmap1);
7059 obj = isl_vec_alloc(ctx, 1 + total);
7060 isl_seq_clr(obj->block.data, 1 + total);
7061 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7062 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7065 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7067 isl_basic_map_free(bmap1);
7071 isl_basic_map_free(bmap1);
7072 isl_basic_map_free(bmap2);
7073 return isl_lp_error;
7076 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7077 * for any common value of the parameters and dimensions preceding pos
7078 * in both basic sets, the values of dimension pos in bset1 are
7079 * smaller or larger than those in bset2.
7082 * 1 if bset1 follows bset2
7083 * -1 if bset1 precedes bset2
7084 * 0 if bset1 and bset2 are incomparable
7085 * -2 if some error occurred.
7087 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7088 struct isl_basic_set *bset2, int pos)
7091 enum isl_lp_result res;
7096 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7098 if (res == isl_lp_empty)
7100 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7101 res == isl_lp_unbounded)
7103 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7112 /* Given two basic sets bset1 and bset2, check whether
7113 * for any common value of the parameters and dimensions preceding pos
7114 * there is a value of dimension pos in bset1 that is larger
7115 * than a value of the same dimension in bset2.
7118 * 1 if there exists such a pair
7119 * 0 if there is no such pair, but there is a pair of equal values
7121 * -2 if some error occurred.
7123 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7124 __isl_keep isl_basic_set *bset2, int pos)
7127 enum isl_lp_result res;
7132 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7134 if (res == isl_lp_empty)
7136 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7137 res == isl_lp_unbounded)
7139 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7141 else if (res == isl_lp_ok)
7150 /* Given two sets set1 and set2, check whether
7151 * for any common value of the parameters and dimensions preceding pos
7152 * there is a value of dimension pos in set1 that is larger
7153 * than a value of the same dimension in set2.
7156 * 1 if there exists such a pair
7157 * 0 if there is no such pair, but there is a pair of equal values
7159 * -2 if some error occurred.
7161 int isl_set_follows_at(__isl_keep isl_set *set1,
7162 __isl_keep isl_set *set2, int pos)
7170 for (i = 0; i < set1->n; ++i)
7171 for (j = 0; j < set2->n; ++j) {
7173 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7174 if (f == 1 || f == -2)
7183 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7184 unsigned pos, isl_int *val)
7192 total = isl_basic_map_total_dim(bmap);
7193 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7194 for (; d+1 > pos; --d)
7195 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7199 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7201 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7203 if (!isl_int_is_one(bmap->eq[i][1+d]))
7206 isl_int_neg(*val, bmap->eq[i][0]);
7212 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7213 unsigned pos, isl_int *val)
7225 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7228 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7229 for (i = 1; fixed == 1 && i < map->n; ++i) {
7230 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7231 if (fixed == 1 && isl_int_ne(tmp, v))
7235 isl_int_set(*val, v);
7241 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7242 unsigned pos, isl_int *val)
7244 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7248 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7251 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7254 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7255 enum isl_dim_type type, unsigned pos, isl_int *val)
7257 if (pos >= isl_basic_map_dim(bmap, type))
7259 return isl_basic_map_plain_has_fixed_var(bmap,
7260 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7263 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7264 enum isl_dim_type type, unsigned pos, isl_int *val)
7266 if (pos >= isl_map_dim(map, type))
7268 return isl_map_plain_has_fixed_var(map,
7269 map_offset(map, type) - 1 + pos, val);
7272 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7273 enum isl_dim_type type, unsigned pos, isl_int *val)
7275 return isl_map_plain_is_fixed(map, type, pos, val);
7278 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7279 * then return this fixed value in *val.
7281 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7282 unsigned dim, isl_int *val)
7284 return isl_basic_set_plain_has_fixed_var(bset,
7285 isl_basic_set_n_param(bset) + dim, val);
7288 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7289 * then return this fixed value in *val.
7291 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7292 unsigned dim, isl_int *val)
7294 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7297 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7298 unsigned dim, isl_int *val)
7300 return isl_set_plain_dim_is_fixed(set, dim, val);
7303 /* Check if input variable in has fixed value and if so and if val is not NULL,
7304 * then return this fixed value in *val.
7306 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7307 unsigned in, isl_int *val)
7309 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7312 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7313 * and if val is not NULL, then return this lower bound in *val.
7315 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7316 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7318 int i, i_eq = -1, i_ineq = -1;
7325 total = isl_basic_set_total_dim(bset);
7326 nparam = isl_basic_set_n_param(bset);
7327 for (i = 0; i < bset->n_eq; ++i) {
7328 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7334 for (i = 0; i < bset->n_ineq; ++i) {
7335 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7337 if (i_eq != -1 || i_ineq != -1)
7341 if (i_eq == -1 && i_ineq == -1)
7343 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7344 /* The coefficient should always be one due to normalization. */
7345 if (!isl_int_is_one(c[1+nparam+dim]))
7347 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7349 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7350 total - nparam - dim - 1) != -1)
7353 isl_int_neg(*val, c[0]);
7357 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7358 unsigned dim, isl_int *val)
7370 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7374 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7376 for (i = 1; fixed == 1 && i < set->n; ++i) {
7377 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7379 if (fixed == 1 && isl_int_ne(tmp, v))
7383 isl_int_set(*val, v);
7394 /* uset_gist depends on constraints without existentially quantified
7395 * variables sorting first.
7397 static int qsort_constraint_cmp(const void *p1, const void *p2)
7399 const struct constraint *c1 = (const struct constraint *)p1;
7400 const struct constraint *c2 = (const struct constraint *)p2;
7402 unsigned size = isl_min(c1->size, c2->size);
7404 l1 = isl_seq_last_non_zero(c1->c, size);
7405 l2 = isl_seq_last_non_zero(c2->c, size);
7410 return isl_seq_cmp(c1->c, c2->c, size);
7413 static struct isl_basic_map *isl_basic_map_sort_constraints(
7414 struct isl_basic_map *bmap)
7417 struct constraint *c;
7422 total = isl_basic_map_total_dim(bmap);
7423 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7426 for (i = 0; i < bmap->n_ineq; ++i) {
7428 c[i].c = bmap->ineq[i];
7430 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7431 for (i = 0; i < bmap->n_ineq; ++i)
7432 bmap->ineq[i] = c[i].c;
7436 isl_basic_map_free(bmap);
7440 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7441 __isl_take isl_basic_set *bset)
7443 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7444 (struct isl_basic_map *)bset);
7447 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7451 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7453 bmap = isl_basic_map_remove_redundancies(bmap);
7454 bmap = isl_basic_map_sort_constraints(bmap);
7455 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7459 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7461 return (struct isl_basic_set *)isl_basic_map_normalize(
7462 (struct isl_basic_map *)bset);
7465 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7466 const __isl_keep isl_basic_map *bmap2)
7473 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7474 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7475 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7476 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7477 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7478 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7479 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7480 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7481 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7482 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7483 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7485 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7487 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7489 if (bmap1->n_eq != bmap2->n_eq)
7490 return bmap1->n_eq - bmap2->n_eq;
7491 if (bmap1->n_ineq != bmap2->n_ineq)
7492 return bmap1->n_ineq - bmap2->n_ineq;
7493 if (bmap1->n_div != bmap2->n_div)
7494 return bmap1->n_div - bmap2->n_div;
7495 total = isl_basic_map_total_dim(bmap1);
7496 for (i = 0; i < bmap1->n_eq; ++i) {
7497 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7501 for (i = 0; i < bmap1->n_ineq; ++i) {
7502 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7506 for (i = 0; i < bmap1->n_div; ++i) {
7507 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7514 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7515 __isl_keep isl_basic_map *bmap2)
7517 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7520 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7521 __isl_keep isl_basic_set *bset2)
7523 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7524 (isl_basic_map *)bset2);
7527 static int qsort_bmap_cmp(const void *p1, const void *p2)
7529 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7530 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7532 return isl_basic_map_plain_cmp(bmap1, bmap2);
7535 /* We normalize in place, but if anything goes wrong we need
7536 * to return NULL, so we need to make sure we don't change the
7537 * meaning of any possible other copies of map.
7539 struct isl_map *isl_map_normalize(struct isl_map *map)
7542 struct isl_basic_map *bmap;
7546 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7548 for (i = 0; i < map->n; ++i) {
7549 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7552 isl_basic_map_free(map->p[i]);
7555 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7556 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7557 map = isl_map_remove_empty_parts(map);
7560 for (i = map->n - 1; i >= 1; --i) {
7561 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7563 isl_basic_map_free(map->p[i-1]);
7564 for (j = i; j < map->n; ++j)
7565 map->p[j-1] = map->p[j];
7575 struct isl_set *isl_set_normalize(struct isl_set *set)
7577 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7580 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7590 if (!isl_space_is_equal(map1->dim, map2->dim))
7593 map1 = isl_map_copy(map1);
7594 map2 = isl_map_copy(map2);
7595 map1 = isl_map_normalize(map1);
7596 map2 = isl_map_normalize(map2);
7599 equal = map1->n == map2->n;
7600 for (i = 0; equal && i < map1->n; ++i) {
7601 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7614 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7616 return isl_map_plain_is_equal(map1, map2);
7619 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7621 return isl_map_plain_is_equal((struct isl_map *)set1,
7622 (struct isl_map *)set2);
7625 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7627 return isl_set_plain_is_equal(set1, set2);
7630 /* Return an interval that ranges from min to max (inclusive)
7632 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7633 isl_int min, isl_int max)
7636 struct isl_basic_set *bset = NULL;
7638 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7642 k = isl_basic_set_alloc_inequality(bset);
7645 isl_int_set_si(bset->ineq[k][1], 1);
7646 isl_int_neg(bset->ineq[k][0], min);
7648 k = isl_basic_set_alloc_inequality(bset);
7651 isl_int_set_si(bset->ineq[k][1], -1);
7652 isl_int_set(bset->ineq[k][0], max);
7656 isl_basic_set_free(bset);
7660 /* Return the Cartesian product of the basic sets in list (in the given order).
7662 __isl_give isl_basic_set *isl_basic_set_list_product(
7663 __isl_take struct isl_basic_set_list *list)
7671 struct isl_basic_set *product = NULL;
7675 isl_assert(list->ctx, list->n > 0, goto error);
7676 isl_assert(list->ctx, list->p[0], goto error);
7677 nparam = isl_basic_set_n_param(list->p[0]);
7678 dim = isl_basic_set_n_dim(list->p[0]);
7679 extra = list->p[0]->n_div;
7680 n_eq = list->p[0]->n_eq;
7681 n_ineq = list->p[0]->n_ineq;
7682 for (i = 1; i < list->n; ++i) {
7683 isl_assert(list->ctx, list->p[i], goto error);
7684 isl_assert(list->ctx,
7685 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7686 dim += isl_basic_set_n_dim(list->p[i]);
7687 extra += list->p[i]->n_div;
7688 n_eq += list->p[i]->n_eq;
7689 n_ineq += list->p[i]->n_ineq;
7691 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7696 for (i = 0; i < list->n; ++i) {
7697 isl_basic_set_add_constraints(product,
7698 isl_basic_set_copy(list->p[i]), dim);
7699 dim += isl_basic_set_n_dim(list->p[i]);
7701 isl_basic_set_list_free(list);
7704 isl_basic_set_free(product);
7705 isl_basic_set_list_free(list);
7709 struct isl_basic_map *isl_basic_map_product(
7710 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7712 isl_space *dim_result = NULL;
7713 struct isl_basic_map *bmap;
7714 unsigned in1, in2, out1, out2, nparam, total, pos;
7715 struct isl_dim_map *dim_map1, *dim_map2;
7717 if (!bmap1 || !bmap2)
7720 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
7721 bmap2->dim, isl_dim_param), goto error);
7722 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
7723 isl_space_copy(bmap2->dim));
7725 in1 = isl_basic_map_n_in(bmap1);
7726 in2 = isl_basic_map_n_in(bmap2);
7727 out1 = isl_basic_map_n_out(bmap1);
7728 out2 = isl_basic_map_n_out(bmap2);
7729 nparam = isl_basic_map_n_param(bmap1);
7731 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7732 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7733 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7734 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7735 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7736 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7737 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7738 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7739 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7740 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7741 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7743 bmap = isl_basic_map_alloc_space(dim_result,
7744 bmap1->n_div + bmap2->n_div,
7745 bmap1->n_eq + bmap2->n_eq,
7746 bmap1->n_ineq + bmap2->n_ineq);
7747 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7748 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7749 bmap = isl_basic_map_simplify(bmap);
7750 return isl_basic_map_finalize(bmap);
7752 isl_basic_map_free(bmap1);
7753 isl_basic_map_free(bmap2);
7757 __isl_give isl_basic_map *isl_basic_map_flat_product(
7758 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7760 isl_basic_map *prod;
7762 prod = isl_basic_map_product(bmap1, bmap2);
7763 prod = isl_basic_map_flatten(prod);
7767 __isl_give isl_basic_set *isl_basic_set_flat_product(
7768 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7770 return isl_basic_map_flat_range_product(bset1, bset2);
7773 __isl_give isl_basic_map *isl_basic_map_domain_product(
7774 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7776 isl_space *space_result = NULL;
7777 isl_basic_map *bmap;
7778 unsigned in1, in2, out, nparam, total, pos;
7779 struct isl_dim_map *dim_map1, *dim_map2;
7781 if (!bmap1 || !bmap2)
7784 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
7785 isl_space_copy(bmap2->dim));
7787 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
7788 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
7789 out = isl_basic_map_dim(bmap1, isl_dim_out);
7790 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
7792 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
7793 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7794 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7795 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7796 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7797 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7798 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7799 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7800 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
7801 isl_dim_map_div(dim_map1, bmap1, pos += out);
7802 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7804 bmap = isl_basic_map_alloc_space(space_result,
7805 bmap1->n_div + bmap2->n_div,
7806 bmap1->n_eq + bmap2->n_eq,
7807 bmap1->n_ineq + bmap2->n_ineq);
7808 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7809 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7810 bmap = isl_basic_map_simplify(bmap);
7811 return isl_basic_map_finalize(bmap);
7813 isl_basic_map_free(bmap1);
7814 isl_basic_map_free(bmap2);
7818 __isl_give isl_basic_map *isl_basic_map_range_product(
7819 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7821 isl_space *dim_result = NULL;
7822 isl_basic_map *bmap;
7823 unsigned in, out1, out2, nparam, total, pos;
7824 struct isl_dim_map *dim_map1, *dim_map2;
7826 if (!bmap1 || !bmap2)
7829 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
7830 isl_space_copy(bmap2->dim));
7832 in = isl_basic_map_dim(bmap1, isl_dim_in);
7833 out1 = isl_basic_map_n_out(bmap1);
7834 out2 = isl_basic_map_n_out(bmap2);
7835 nparam = isl_basic_map_n_param(bmap1);
7837 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7838 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7839 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7840 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7841 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7842 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7843 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7844 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7845 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7846 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7847 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7849 bmap = isl_basic_map_alloc_space(dim_result,
7850 bmap1->n_div + bmap2->n_div,
7851 bmap1->n_eq + bmap2->n_eq,
7852 bmap1->n_ineq + bmap2->n_ineq);
7853 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7854 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7855 bmap = isl_basic_map_simplify(bmap);
7856 return isl_basic_map_finalize(bmap);
7858 isl_basic_map_free(bmap1);
7859 isl_basic_map_free(bmap2);
7863 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
7864 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7866 isl_basic_map *prod;
7868 prod = isl_basic_map_range_product(bmap1, bmap2);
7869 prod = isl_basic_map_flatten_range(prod);
7873 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7874 __isl_take isl_map *map2,
7875 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
7876 __isl_take isl_space *right),
7877 __isl_give isl_basic_map *(*basic_map_product)(
7878 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7881 struct isl_map *result;
7887 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
7888 map2->dim, isl_dim_param), goto error);
7890 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7891 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7892 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7894 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
7895 isl_space_copy(map2->dim)),
7896 map1->n * map2->n, flags);
7899 for (i = 0; i < map1->n; ++i)
7900 for (j = 0; j < map2->n; ++j) {
7901 struct isl_basic_map *part;
7902 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7903 isl_basic_map_copy(map2->p[j]));
7904 if (isl_basic_map_is_empty(part))
7905 isl_basic_map_free(part);
7907 result = isl_map_add_basic_map(result, part);
7920 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7922 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
7923 __isl_take isl_map *map2)
7925 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
7928 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
7929 __isl_take isl_map *map2)
7931 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
7934 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7936 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7937 __isl_take isl_map *map2)
7941 prod = isl_map_product(map1, map2);
7942 prod = isl_map_flatten(prod);
7946 /* Given two set A and B, construct its Cartesian product A x B.
7948 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7950 return isl_map_range_product(set1, set2);
7953 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7954 __isl_take isl_set *set2)
7956 return isl_map_flat_range_product(set1, set2);
7959 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
7961 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
7962 __isl_take isl_map *map2)
7964 return map_product(map1, map2, &isl_space_domain_product,
7965 &isl_basic_map_domain_product);
7968 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7970 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
7971 __isl_take isl_map *map2)
7973 return map_product(map1, map2, &isl_space_range_product,
7974 &isl_basic_map_range_product);
7977 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
7978 __isl_take isl_map *map2)
7980 return isl_map_align_params_map_map_and(map1, map2,
7981 &map_domain_product_aligned);
7984 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7985 __isl_take isl_map *map2)
7987 return isl_map_align_params_map_map_and(map1, map2,
7988 &map_range_product_aligned);
7991 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
7993 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
7994 __isl_take isl_map *map2)
7998 prod = isl_map_domain_product(map1, map2);
7999 prod = isl_map_flatten_domain(prod);
8003 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8005 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8006 __isl_take isl_map *map2)
8010 prod = isl_map_range_product(map1, map2);
8011 prod = isl_map_flatten_range(prod);
8015 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8018 uint32_t hash = isl_hash_init();
8023 bmap = isl_basic_map_copy(bmap);
8024 bmap = isl_basic_map_normalize(bmap);
8027 total = isl_basic_map_total_dim(bmap);
8028 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8029 for (i = 0; i < bmap->n_eq; ++i) {
8031 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8032 isl_hash_hash(hash, c_hash);
8034 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8035 for (i = 0; i < bmap->n_ineq; ++i) {
8037 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8038 isl_hash_hash(hash, c_hash);
8040 isl_hash_byte(hash, bmap->n_div & 0xFF);
8041 for (i = 0; i < bmap->n_div; ++i) {
8043 if (isl_int_is_zero(bmap->div[i][0]))
8045 isl_hash_byte(hash, i & 0xFF);
8046 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8047 isl_hash_hash(hash, c_hash);
8049 isl_basic_map_free(bmap);
8053 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8055 return isl_basic_map_get_hash((isl_basic_map *)bset);
8058 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8065 map = isl_map_copy(map);
8066 map = isl_map_normalize(map);
8070 hash = isl_hash_init();
8071 for (i = 0; i < map->n; ++i) {
8073 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8074 isl_hash_hash(hash, bmap_hash);
8082 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8084 return isl_map_get_hash((isl_map *)set);
8087 /* Check if the value for dimension dim is completely determined
8088 * by the values of the other parameters and variables.
8089 * That is, check if dimension dim is involved in an equality.
8091 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8098 nparam = isl_basic_set_n_param(bset);
8099 for (i = 0; i < bset->n_eq; ++i)
8100 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8105 /* Check if the value for dimension dim is completely determined
8106 * by the values of the other parameters and variables.
8107 * That is, check if dimension dim is involved in an equality
8108 * for each of the subsets.
8110 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8116 for (i = 0; i < set->n; ++i) {
8118 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8125 int isl_set_n_basic_set(__isl_keep isl_set *set)
8127 return set ? set->n : 0;
8130 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8131 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8138 for (i = 0; i < map->n; ++i)
8139 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8145 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8146 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8153 for (i = 0; i < set->n; ++i)
8154 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8160 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8167 bset = isl_basic_set_cow(bset);
8171 dim = isl_basic_set_get_space(bset);
8172 dim = isl_space_lift(dim, bset->n_div);
8175 isl_space_free(bset->dim);
8177 bset->extra -= bset->n_div;
8180 bset = isl_basic_set_finalize(bset);
8184 isl_basic_set_free(bset);
8188 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8194 set = isl_set_align_divs(set);
8199 set = isl_set_cow(set);
8203 n_div = set->p[0]->n_div;
8204 dim = isl_set_get_space(set);
8205 dim = isl_space_lift(dim, n_div);
8208 isl_space_free(set->dim);
8211 for (i = 0; i < set->n; ++i) {
8212 set->p[i] = isl_basic_set_lift(set->p[i]);
8223 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8226 struct isl_basic_map *bmap;
8233 set = isl_set_align_divs(set);
8238 dim = isl_set_get_space(set);
8239 if (set->n == 0 || set->p[0]->n_div == 0) {
8241 return isl_map_identity(isl_space_map_from_set(dim));
8244 n_div = set->p[0]->n_div;
8245 dim = isl_space_map_from_set(dim);
8246 n_param = isl_space_dim(dim, isl_dim_param);
8247 n_set = isl_space_dim(dim, isl_dim_in);
8248 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8249 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8250 for (i = 0; i < n_set; ++i)
8251 bmap = var_equal(bmap, i);
8253 total = n_param + n_set + n_set + n_div;
8254 for (i = 0; i < n_div; ++i) {
8255 k = isl_basic_map_alloc_inequality(bmap);
8258 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8259 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8260 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8261 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8262 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8263 set->p[0]->div[i][0]);
8265 l = isl_basic_map_alloc_inequality(bmap);
8268 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8269 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8270 set->p[0]->div[i][0]);
8271 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8275 bmap = isl_basic_map_simplify(bmap);
8276 bmap = isl_basic_map_finalize(bmap);
8277 return isl_map_from_basic_map(bmap);
8280 isl_basic_map_free(bmap);
8284 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8292 dim = isl_basic_set_total_dim(bset);
8293 size += bset->n_eq * (1 + dim);
8294 size += bset->n_ineq * (1 + dim);
8295 size += bset->n_div * (2 + dim);
8300 int isl_set_size(__isl_keep isl_set *set)
8308 for (i = 0; i < set->n; ++i)
8309 size += isl_basic_set_size(set->p[i]);
8314 /* Check if there is any lower bound (if lower == 0) and/or upper
8315 * bound (if upper == 0) on the specified dim.
8317 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8318 enum isl_dim_type type, unsigned pos, int lower, int upper)
8325 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8327 pos += isl_basic_map_offset(bmap, type);
8329 for (i = 0; i < bmap->n_div; ++i) {
8330 if (isl_int_is_zero(bmap->div[i][0]))
8332 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8336 for (i = 0; i < bmap->n_eq; ++i)
8337 if (!isl_int_is_zero(bmap->eq[i][pos]))
8340 for (i = 0; i < bmap->n_ineq; ++i) {
8341 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8348 return lower && upper;
8351 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8352 enum isl_dim_type type, unsigned pos)
8354 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8357 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8358 enum isl_dim_type type, unsigned pos)
8360 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8363 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8364 enum isl_dim_type type, unsigned pos)
8366 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8369 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8370 enum isl_dim_type type, unsigned pos)
8377 for (i = 0; i < map->n; ++i) {
8379 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8380 if (bounded < 0 || !bounded)
8387 /* Return 1 if the specified dim is involved in both an upper bound
8388 * and a lower bound.
8390 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8391 enum isl_dim_type type, unsigned pos)
8393 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8396 static int has_bound(__isl_keep isl_map *map,
8397 enum isl_dim_type type, unsigned pos,
8398 int (*fn)(__isl_keep isl_basic_map *bmap,
8399 enum isl_dim_type type, unsigned pos))
8406 for (i = 0; i < map->n; ++i) {
8408 bounded = fn(map->p[i], type, pos);
8409 if (bounded < 0 || bounded)
8416 /* Return 1 if the specified dim is involved in any lower bound.
8418 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8419 enum isl_dim_type type, unsigned pos)
8421 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8424 /* Return 1 if the specified dim is involved in any upper bound.
8426 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8427 enum isl_dim_type type, unsigned pos)
8429 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8432 /* For each of the "n" variables starting at "first", determine
8433 * the sign of the variable and put the results in the first "n"
8434 * elements of the array "signs".
8436 * 1 means that the variable is non-negative
8437 * -1 means that the variable is non-positive
8438 * 0 means the variable attains both positive and negative values.
8440 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8441 unsigned first, unsigned n, int *signs)
8443 isl_vec *bound = NULL;
8444 struct isl_tab *tab = NULL;
8445 struct isl_tab_undo *snap;
8448 if (!bset || !signs)
8451 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8452 tab = isl_tab_from_basic_set(bset);
8456 isl_seq_clr(bound->el, bound->size);
8457 isl_int_set_si(bound->el[0], -1);
8459 snap = isl_tab_snap(tab);
8460 for (i = 0; i < n; ++i) {
8463 isl_int_set_si(bound->el[1 + first + i], -1);
8464 if (isl_tab_add_ineq(tab, bound->el) < 0)
8467 isl_int_set_si(bound->el[1 + first + i], 0);
8468 if (isl_tab_rollback(tab, snap) < 0)
8476 isl_int_set_si(bound->el[1 + first + i], 1);
8477 if (isl_tab_add_ineq(tab, bound->el) < 0)
8480 isl_int_set_si(bound->el[1 + first + i], 0);
8481 if (isl_tab_rollback(tab, snap) < 0)
8484 signs[i] = empty ? -1 : 0;
8488 isl_vec_free(bound);
8492 isl_vec_free(bound);
8496 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8497 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8499 if (!bset || !signs)
8501 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8504 first += pos(bset->dim, type) - 1;
8505 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8508 /* Check if the given basic map is obviously single-valued.
8509 * In particular, for each output dimension, check that there is
8510 * an equality that defines the output dimension in terms of
8511 * earlier dimensions.
8513 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8523 total = 1 + isl_basic_map_total_dim(bmap);
8524 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8525 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8527 for (i = 0; i < n_out; ++i) {
8528 for (j = 0; j < bmap->n_eq; ++j) {
8529 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8531 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8532 total - (o_out + i + 1)) == -1)
8535 if (j >= bmap->n_eq)
8542 /* Check if the given map is obviously single-valued.
8544 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8553 return isl_basic_map_plain_is_single_valued(map->p[0]);
8556 /* Check if the given map is single-valued.
8561 * and check if the result is a subset of the identity mapping.
8563 int isl_map_is_single_valued(__isl_keep isl_map *map)
8570 sv = isl_map_plain_is_single_valued(map);
8574 test = isl_map_reverse(isl_map_copy(map));
8575 test = isl_map_apply_range(test, isl_map_copy(map));
8577 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8578 id = isl_map_identity(dim);
8580 sv = isl_map_is_subset(test, id);
8588 int isl_map_is_injective(__isl_keep isl_map *map)
8592 map = isl_map_copy(map);
8593 map = isl_map_reverse(map);
8594 in = isl_map_is_single_valued(map);
8600 /* Check if the given map is obviously injective.
8602 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8606 map = isl_map_copy(map);
8607 map = isl_map_reverse(map);
8608 in = isl_map_plain_is_single_valued(map);
8614 int isl_map_is_bijective(__isl_keep isl_map *map)
8618 sv = isl_map_is_single_valued(map);
8622 return isl_map_is_injective(map);
8625 int isl_set_is_singleton(__isl_keep isl_set *set)
8627 return isl_map_is_single_valued((isl_map *)set);
8630 int isl_map_is_translation(__isl_keep isl_map *map)
8635 delta = isl_map_deltas(isl_map_copy(map));
8636 ok = isl_set_is_singleton(delta);
8637 isl_set_free(delta);
8642 static int unique(isl_int *p, unsigned pos, unsigned len)
8644 if (isl_seq_first_non_zero(p, pos) != -1)
8646 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8651 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8660 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8663 nvar = isl_basic_set_dim(bset, isl_dim_set);
8664 ovar = isl_space_offset(bset->dim, isl_dim_set);
8665 for (j = 0; j < nvar; ++j) {
8666 int lower = 0, upper = 0;
8667 for (i = 0; i < bset->n_eq; ++i) {
8668 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8670 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8676 for (i = 0; i < bset->n_ineq; ++i) {
8677 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8679 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8681 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8686 if (!lower || !upper)
8693 int isl_set_is_box(__isl_keep isl_set *set)
8700 return isl_basic_set_is_box(set->p[0]);
8703 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8708 return isl_space_is_wrapping(bset->dim);
8711 int isl_set_is_wrapping(__isl_keep isl_set *set)
8716 return isl_space_is_wrapping(set->dim);
8719 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8721 bmap = isl_basic_map_cow(bmap);
8725 bmap->dim = isl_space_wrap(bmap->dim);
8729 bmap = isl_basic_map_finalize(bmap);
8731 return (isl_basic_set *)bmap;
8733 isl_basic_map_free(bmap);
8737 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8741 map = isl_map_cow(map);
8745 for (i = 0; i < map->n; ++i) {
8746 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8750 map->dim = isl_space_wrap(map->dim);
8754 return (isl_set *)map;
8760 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8762 bset = isl_basic_set_cow(bset);
8766 bset->dim = isl_space_unwrap(bset->dim);
8770 bset = isl_basic_set_finalize(bset);
8772 return (isl_basic_map *)bset;
8774 isl_basic_set_free(bset);
8778 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8785 if (!isl_set_is_wrapping(set))
8786 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8789 set = isl_set_cow(set);
8793 for (i = 0; i < set->n; ++i) {
8794 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8799 set->dim = isl_space_unwrap(set->dim);
8803 return (isl_map *)set;
8809 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8810 enum isl_dim_type type)
8815 if (!isl_space_is_named_or_nested(bmap->dim, type))
8818 bmap = isl_basic_map_cow(bmap);
8822 bmap->dim = isl_space_reset(bmap->dim, type);
8826 bmap = isl_basic_map_finalize(bmap);
8830 isl_basic_map_free(bmap);
8834 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8835 enum isl_dim_type type)
8842 if (!isl_space_is_named_or_nested(map->dim, type))
8845 map = isl_map_cow(map);
8849 for (i = 0; i < map->n; ++i) {
8850 map->p[i] = isl_basic_map_reset(map->p[i], type);
8854 map->dim = isl_space_reset(map->dim, type);
8864 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8869 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8872 bmap = isl_basic_map_cow(bmap);
8876 bmap->dim = isl_space_flatten(bmap->dim);
8880 bmap = isl_basic_map_finalize(bmap);
8884 isl_basic_map_free(bmap);
8888 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8890 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8893 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
8894 __isl_take isl_basic_map *bmap)
8899 if (!bmap->dim->nested[0])
8902 bmap = isl_basic_map_cow(bmap);
8906 bmap->dim = isl_space_flatten_domain(bmap->dim);
8910 bmap = isl_basic_map_finalize(bmap);
8914 isl_basic_map_free(bmap);
8918 __isl_give isl_basic_map *isl_basic_map_flatten_range(
8919 __isl_take isl_basic_map *bmap)
8924 if (!bmap->dim->nested[1])
8927 bmap = isl_basic_map_cow(bmap);
8931 bmap->dim = isl_space_flatten_range(bmap->dim);
8935 bmap = isl_basic_map_finalize(bmap);
8939 isl_basic_map_free(bmap);
8943 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8950 if (!map->dim->nested[0] && !map->dim->nested[1])
8953 map = isl_map_cow(map);
8957 for (i = 0; i < map->n; ++i) {
8958 map->p[i] = isl_basic_map_flatten(map->p[i]);
8962 map->dim = isl_space_flatten(map->dim);
8972 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8974 return (isl_set *)isl_map_flatten((isl_map *)set);
8977 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8979 isl_space *dim, *flat_dim;
8982 dim = isl_set_get_space(set);
8983 flat_dim = isl_space_flatten(isl_space_copy(dim));
8984 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
8985 map = isl_map_intersect_domain(map, set);
8990 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
8997 if (!map->dim->nested[0])
9000 map = isl_map_cow(map);
9004 for (i = 0; i < map->n; ++i) {
9005 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9009 map->dim = isl_space_flatten_domain(map->dim);
9019 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9026 if (!map->dim->nested[1])
9029 map = isl_map_cow(map);
9033 for (i = 0; i < map->n; ++i) {
9034 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9038 map->dim = isl_space_flatten_range(map->dim);
9048 /* Reorder the dimensions of "bmap" according to the given dim_map
9049 * and set the dimension specification to "dim".
9051 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9052 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9056 bmap = isl_basic_map_cow(bmap);
9057 if (!bmap || !dim || !dim_map)
9060 res = isl_basic_map_alloc_space(dim,
9061 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9062 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9063 res = isl_basic_map_finalize(res);
9067 isl_basic_map_free(bmap);
9068 isl_space_free(dim);
9072 /* Reorder the dimensions of "map" according to given reordering.
9074 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9075 __isl_take isl_reordering *r)
9078 struct isl_dim_map *dim_map;
9080 map = isl_map_cow(map);
9081 dim_map = isl_dim_map_from_reordering(r);
9082 if (!map || !r || !dim_map)
9085 for (i = 0; i < map->n; ++i) {
9086 struct isl_dim_map *dim_map_i;
9088 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9090 map->p[i] = isl_basic_map_realign(map->p[i],
9091 isl_space_copy(r->dim), dim_map_i);
9097 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9099 isl_reordering_free(r);
9105 isl_reordering_free(r);
9109 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9110 __isl_take isl_reordering *r)
9112 return (isl_set *)isl_map_realign((isl_map *)set, r);
9115 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9116 __isl_take isl_space *model)
9123 ctx = isl_space_get_ctx(model);
9124 if (!isl_space_has_named_params(model))
9125 isl_die(ctx, isl_error_invalid,
9126 "model has unnamed parameters", goto error);
9127 if (!isl_space_has_named_params(map->dim))
9128 isl_die(ctx, isl_error_invalid,
9129 "relation has unnamed parameters", goto error);
9130 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9131 isl_reordering *exp;
9133 model = isl_space_drop_dims(model, isl_dim_in,
9134 0, isl_space_dim(model, isl_dim_in));
9135 model = isl_space_drop_dims(model, isl_dim_out,
9136 0, isl_space_dim(model, isl_dim_out));
9137 exp = isl_parameter_alignment_reordering(map->dim, model);
9138 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9139 map = isl_map_realign(map, exp);
9142 isl_space_free(model);
9145 isl_space_free(model);
9150 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9151 __isl_take isl_space *model)
9153 return isl_map_align_params(set, model);
9156 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9157 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9158 enum isl_dim_type c2, enum isl_dim_type c3,
9159 enum isl_dim_type c4, enum isl_dim_type c5)
9161 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9162 struct isl_mat *mat;
9168 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9169 isl_basic_map_total_dim(bmap) + 1);
9172 for (i = 0; i < bmap->n_eq; ++i)
9173 for (j = 0, pos = 0; j < 5; ++j) {
9174 int off = isl_basic_map_offset(bmap, c[j]);
9175 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9176 isl_int_set(mat->row[i][pos],
9177 bmap->eq[i][off + k]);
9185 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9186 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9187 enum isl_dim_type c2, enum isl_dim_type c3,
9188 enum isl_dim_type c4, enum isl_dim_type c5)
9190 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9191 struct isl_mat *mat;
9197 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9198 isl_basic_map_total_dim(bmap) + 1);
9201 for (i = 0; i < bmap->n_ineq; ++i)
9202 for (j = 0, pos = 0; j < 5; ++j) {
9203 int off = isl_basic_map_offset(bmap, c[j]);
9204 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9205 isl_int_set(mat->row[i][pos],
9206 bmap->ineq[i][off + k]);
9214 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9215 __isl_take isl_space *dim,
9216 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9217 enum isl_dim_type c2, enum isl_dim_type c3,
9218 enum isl_dim_type c4, enum isl_dim_type c5)
9220 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9221 isl_basic_map *bmap;
9227 if (!dim || !eq || !ineq)
9230 if (eq->n_col != ineq->n_col)
9231 isl_die(dim->ctx, isl_error_invalid,
9232 "equalities and inequalities matrices should have "
9233 "same number of columns", goto error);
9235 total = 1 + isl_space_dim(dim, isl_dim_all);
9237 if (eq->n_col < total)
9238 isl_die(dim->ctx, isl_error_invalid,
9239 "number of columns too small", goto error);
9241 extra = eq->n_col - total;
9243 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9244 eq->n_row, ineq->n_row);
9247 for (i = 0; i < extra; ++i) {
9248 k = isl_basic_map_alloc_div(bmap);
9251 isl_int_set_si(bmap->div[k][0], 0);
9253 for (i = 0; i < eq->n_row; ++i) {
9254 l = isl_basic_map_alloc_equality(bmap);
9257 for (j = 0, pos = 0; j < 5; ++j) {
9258 int off = isl_basic_map_offset(bmap, c[j]);
9259 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9260 isl_int_set(bmap->eq[l][off + k],
9266 for (i = 0; i < ineq->n_row; ++i) {
9267 l = isl_basic_map_alloc_inequality(bmap);
9270 for (j = 0, pos = 0; j < 5; ++j) {
9271 int off = isl_basic_map_offset(bmap, c[j]);
9272 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9273 isl_int_set(bmap->ineq[l][off + k],
9280 isl_space_free(dim);
9286 isl_space_free(dim);
9292 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9293 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9294 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9296 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9297 c1, c2, c3, c4, isl_dim_in);
9300 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9301 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9302 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9304 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9305 c1, c2, c3, c4, isl_dim_in);
9308 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9309 __isl_take isl_space *dim,
9310 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9311 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9313 return (isl_basic_set*)
9314 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9315 c1, c2, c3, c4, isl_dim_in);
9318 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9323 return isl_space_can_zip(bmap->dim);
9326 int isl_map_can_zip(__isl_keep isl_map *map)
9331 return isl_space_can_zip(map->dim);
9334 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9335 * (A -> C) -> (B -> D).
9337 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9346 if (!isl_basic_map_can_zip(bmap))
9347 isl_die(bmap->ctx, isl_error_invalid,
9348 "basic map cannot be zipped", goto error);
9349 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9350 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9351 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9352 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9353 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9356 bmap->dim = isl_space_zip(bmap->dim);
9361 isl_basic_map_free(bmap);
9365 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9366 * (A -> C) -> (B -> D).
9368 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9375 if (!isl_map_can_zip(map))
9376 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9379 map = isl_map_cow(map);
9383 for (i = 0; i < map->n; ++i) {
9384 map->p[i] = isl_basic_map_zip(map->p[i]);
9389 map->dim = isl_space_zip(map->dim);
9399 /* Construct a basic map mapping the domain of the affine expression
9400 * to a one-dimensional range prescribed by the affine expression.
9402 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9406 isl_local_space *ls;
9407 isl_basic_map *bmap;
9412 ls = isl_aff_get_local_space(aff);
9413 ls = isl_local_space_from_domain(ls);
9414 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
9415 bmap = isl_basic_map_from_local_space(ls);
9416 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9417 k = isl_basic_map_alloc_equality(bmap);
9421 pos = isl_basic_map_offset(bmap, isl_dim_out);
9422 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9423 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9424 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9425 aff->v->size - (pos + 1));
9428 bmap = isl_basic_map_finalize(bmap);
9432 isl_basic_map_free(bmap);
9436 /* Construct a basic map mapping a domain in the given space to
9437 * to an n-dimensional range, with n the number of elements in the list,
9438 * where each coordinate in the range is prescribed by the
9439 * corresponding affine expression.
9440 * The domains of all affine expressions in the list are assumed to match
9443 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9444 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9448 isl_basic_map *bmap;
9453 dim = isl_space_from_domain(domain_dim);
9454 bmap = isl_basic_map_universe(dim);
9456 for (i = 0; i < list->n; ++i) {
9458 isl_basic_map *bmap_i;
9460 aff = isl_aff_copy(list->p[i]);
9461 bmap_i = isl_basic_map_from_aff(aff);
9463 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9466 isl_aff_list_free(list);
9470 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9471 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9473 return isl_map_equate(set, type1, pos1, type2, pos2);
9476 /* Add a constraint imposing that the given two dimensions are equal.
9478 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9479 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9481 isl_basic_map *bmap = NULL;
9487 if (pos1 >= isl_map_dim(map, type1))
9488 isl_die(map->ctx, isl_error_invalid,
9489 "index out of bounds", goto error);
9490 if (pos2 >= isl_map_dim(map, type2))
9491 isl_die(map->ctx, isl_error_invalid,
9492 "index out of bounds", goto error);
9494 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9495 i = isl_basic_map_alloc_equality(bmap);
9498 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9499 pos1 += isl_basic_map_offset(bmap, type1);
9500 pos2 += isl_basic_map_offset(bmap, type2);
9501 isl_int_set_si(bmap->eq[i][pos1], -1);
9502 isl_int_set_si(bmap->eq[i][pos2], 1);
9503 bmap = isl_basic_map_finalize(bmap);
9505 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9509 isl_basic_map_free(bmap);
9514 /* Add a constraint imposing that the given two dimensions have opposite values.
9516 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9517 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9519 isl_basic_map *bmap = NULL;
9525 if (pos1 >= isl_map_dim(map, type1))
9526 isl_die(map->ctx, isl_error_invalid,
9527 "index out of bounds", goto error);
9528 if (pos2 >= isl_map_dim(map, type2))
9529 isl_die(map->ctx, isl_error_invalid,
9530 "index out of bounds", goto error);
9532 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9533 i = isl_basic_map_alloc_equality(bmap);
9536 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9537 pos1 += isl_basic_map_offset(bmap, type1);
9538 pos2 += isl_basic_map_offset(bmap, type2);
9539 isl_int_set_si(bmap->eq[i][pos1], 1);
9540 isl_int_set_si(bmap->eq[i][pos2], 1);
9541 bmap = isl_basic_map_finalize(bmap);
9543 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9547 isl_basic_map_free(bmap);