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 /* Compute the parameter domain of the given basic set.
4062 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4067 n = isl_basic_set_dim(bset, isl_dim_set);
4068 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4069 space = isl_basic_set_get_space(bset);
4070 space = isl_space_params(space);
4071 bset = isl_basic_set_reset_space(bset, space);
4075 /* Compute the parameter domain of the given set.
4077 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4082 n = isl_set_dim(set, isl_dim_set);
4083 set = isl_set_project_out(set, isl_dim_set, 0, n);
4084 space = isl_set_get_space(set);
4085 space = isl_space_params(space);
4086 set = isl_set_reset_space(set, space);
4090 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4093 struct isl_basic_set *domain;
4099 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4101 n_in = isl_basic_map_n_in(bmap);
4102 n_out = isl_basic_map_n_out(bmap);
4103 domain = isl_basic_set_from_basic_map(bmap);
4104 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4106 domain = isl_basic_set_reset_space(domain, dim);
4111 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4115 return isl_space_may_be_set(bmap->dim);
4118 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4122 if (isl_basic_map_may_be_set(bmap))
4124 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4127 __isl_give isl_basic_map *isl_basic_map_domain_map(
4128 __isl_take isl_basic_map *bmap)
4132 isl_basic_map *domain;
4133 int nparam, n_in, n_out;
4136 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4137 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4138 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4140 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4141 domain = isl_basic_map_universe(dim);
4143 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4144 bmap = isl_basic_map_apply_range(bmap, domain);
4145 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4147 total = isl_basic_map_total_dim(bmap);
4149 for (i = 0; i < n_in; ++i) {
4150 k = isl_basic_map_alloc_equality(bmap);
4153 isl_seq_clr(bmap->eq[k], 1 + total);
4154 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4155 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4158 bmap = isl_basic_map_gauss(bmap, NULL);
4159 return isl_basic_map_finalize(bmap);
4161 isl_basic_map_free(bmap);
4165 __isl_give isl_basic_map *isl_basic_map_range_map(
4166 __isl_take isl_basic_map *bmap)
4170 isl_basic_map *range;
4171 int nparam, n_in, n_out;
4174 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4175 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4176 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4178 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4179 range = isl_basic_map_universe(dim);
4181 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4182 bmap = isl_basic_map_apply_range(bmap, range);
4183 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4185 total = isl_basic_map_total_dim(bmap);
4187 for (i = 0; i < n_out; ++i) {
4188 k = isl_basic_map_alloc_equality(bmap);
4191 isl_seq_clr(bmap->eq[k], 1 + total);
4192 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4193 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4196 bmap = isl_basic_map_gauss(bmap, NULL);
4197 return isl_basic_map_finalize(bmap);
4199 isl_basic_map_free(bmap);
4203 int isl_map_may_be_set(__isl_keep isl_map *map)
4207 return isl_space_may_be_set(map->dim);
4210 struct isl_set *isl_map_range(struct isl_map *map)
4213 struct isl_set *set;
4217 if (isl_map_may_be_set(map))
4218 return (isl_set *)map;
4220 map = isl_map_cow(map);
4224 set = (struct isl_set *) map;
4225 set->dim = isl_space_range(set->dim);
4228 for (i = 0; i < map->n; ++i) {
4229 set->p[i] = isl_basic_map_range(map->p[i]);
4233 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4234 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4241 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4244 isl_space *domain_dim;
4246 map = isl_map_cow(map);
4250 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4251 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4252 map->dim = isl_space_join(map->dim, domain_dim);
4255 for (i = 0; i < map->n; ++i) {
4256 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4260 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4261 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4268 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4271 isl_space *range_dim;
4273 map = isl_map_cow(map);
4277 range_dim = isl_space_range(isl_map_get_space(map));
4278 range_dim = isl_space_from_range(range_dim);
4279 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4280 map->dim = isl_space_join(map->dim, range_dim);
4283 for (i = 0; i < map->n; ++i) {
4284 map->p[i] = isl_basic_map_range_map(map->p[i]);
4288 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4289 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4296 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4297 __isl_take isl_space *dim)
4300 struct isl_map *map = NULL;
4302 set = isl_set_cow(set);
4305 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4306 map = (struct isl_map *)set;
4307 for (i = 0; i < set->n; ++i) {
4308 map->p[i] = isl_basic_map_from_basic_set(
4309 set->p[i], isl_space_copy(dim));
4313 isl_space_free(map->dim);
4317 isl_space_free(dim);
4322 __isl_give isl_basic_map *isl_basic_map_from_domain(
4323 __isl_take isl_basic_set *bset)
4325 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4328 __isl_give isl_basic_map *isl_basic_map_from_range(
4329 __isl_take isl_basic_set *bset)
4331 return (isl_basic_map *)bset;
4334 struct isl_map *isl_map_from_range(struct isl_set *set)
4336 return (struct isl_map *)set;
4339 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4341 return isl_map_reverse(isl_map_from_range(set));
4344 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4345 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4347 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4348 isl_basic_map_from_range(range));
4351 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4352 __isl_take isl_set *range)
4354 return isl_map_apply_range(isl_map_from_domain(domain),
4355 isl_map_from_range(range));
4358 struct isl_set *isl_set_from_map(struct isl_map *map)
4361 struct isl_set *set = NULL;
4365 map = isl_map_cow(map);
4368 map->dim = isl_space_as_set_space(map->dim);
4371 set = (struct isl_set *)map;
4372 for (i = 0; i < map->n; ++i) {
4373 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4383 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4386 struct isl_map *map;
4390 isl_assert(dim->ctx, n >= 0, return NULL);
4391 map = isl_alloc(dim->ctx, struct isl_map,
4392 sizeof(struct isl_map) +
4393 (n - 1) * sizeof(struct isl_basic_map *));
4397 map->ctx = dim->ctx;
4398 isl_ctx_ref(map->ctx);
4406 isl_space_free(dim);
4410 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4411 unsigned nparam, unsigned in, unsigned out, int n,
4414 struct isl_map *map;
4417 dims = isl_space_alloc(ctx, nparam, in, out);
4421 map = isl_map_alloc_space(dims, n, flags);
4425 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4427 struct isl_basic_map *bmap;
4428 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4429 bmap = isl_basic_map_set_to_empty(bmap);
4433 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4435 struct isl_basic_set *bset;
4436 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4437 bset = isl_basic_set_set_to_empty(bset);
4441 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4443 struct isl_basic_map *bmap;
4446 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4447 bmap = isl_basic_map_set_to_empty(bmap);
4451 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4453 struct isl_basic_map *bmap;
4456 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4457 bmap = isl_basic_map_set_to_empty(bmap);
4461 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4463 struct isl_basic_set *bset;
4466 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4467 bset = isl_basic_set_set_to_empty(bset);
4471 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4473 struct isl_basic_map *bmap;
4474 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4475 bmap = isl_basic_map_finalize(bmap);
4479 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4481 struct isl_basic_set *bset;
4482 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4483 bset = isl_basic_set_finalize(bset);
4487 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4490 unsigned total = isl_space_dim(dim, isl_dim_all);
4491 isl_basic_map *bmap;
4493 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4494 for (i = 0; i < total; ++i) {
4495 int k = isl_basic_map_alloc_inequality(bmap);
4498 isl_seq_clr(bmap->ineq[k], 1 + total);
4499 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4503 isl_basic_map_free(bmap);
4507 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4509 return isl_basic_map_nat_universe(dim);
4512 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4514 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4517 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4519 return isl_map_nat_universe(dim);
4522 __isl_give isl_basic_map *isl_basic_map_universe_like(
4523 __isl_keep isl_basic_map *model)
4527 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4530 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4534 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4537 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4538 __isl_keep isl_set *model)
4542 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4545 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4547 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4550 struct isl_map *isl_map_empty_like(struct isl_map *model)
4554 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4557 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4561 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4564 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4566 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4569 struct isl_set *isl_set_empty_like(struct isl_set *model)
4573 return isl_set_empty(isl_space_copy(model->dim));
4576 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4578 struct isl_map *map;
4581 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4582 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4586 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4588 struct isl_set *set;
4591 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4592 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4596 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4600 return isl_set_universe(isl_space_copy(model->dim));
4603 struct isl_map *isl_map_dup(struct isl_map *map)
4606 struct isl_map *dup;
4610 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4611 for (i = 0; i < map->n; ++i)
4612 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4616 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4617 __isl_take isl_basic_map *bmap)
4621 if (isl_basic_map_plain_is_empty(bmap)) {
4622 isl_basic_map_free(bmap);
4625 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4626 isl_assert(map->ctx, map->n < map->size, goto error);
4627 map->p[map->n] = bmap;
4629 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4635 isl_basic_map_free(bmap);
4639 void isl_map_free(struct isl_map *map)
4649 isl_ctx_deref(map->ctx);
4650 for (i = 0; i < map->n; ++i)
4651 isl_basic_map_free(map->p[i]);
4652 isl_space_free(map->dim);
4656 struct isl_map *isl_map_extend(struct isl_map *base,
4657 unsigned nparam, unsigned n_in, unsigned n_out)
4661 base = isl_map_cow(base);
4665 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4668 for (i = 0; i < base->n; ++i) {
4669 base->p[i] = isl_basic_map_extend_space(base->p[i],
4670 isl_space_copy(base->dim), 0, 0, 0);
4680 struct isl_set *isl_set_extend(struct isl_set *base,
4681 unsigned nparam, unsigned dim)
4683 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4687 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4688 struct isl_basic_map *bmap, unsigned pos, int value)
4692 bmap = isl_basic_map_cow(bmap);
4693 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4694 j = isl_basic_map_alloc_equality(bmap);
4697 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4698 isl_int_set_si(bmap->eq[j][pos], -1);
4699 isl_int_set_si(bmap->eq[j][0], value);
4700 bmap = isl_basic_map_simplify(bmap);
4701 return isl_basic_map_finalize(bmap);
4703 isl_basic_map_free(bmap);
4707 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4708 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4712 bmap = isl_basic_map_cow(bmap);
4713 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4714 j = isl_basic_map_alloc_equality(bmap);
4717 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4718 isl_int_set_si(bmap->eq[j][pos], -1);
4719 isl_int_set(bmap->eq[j][0], value);
4720 bmap = isl_basic_map_simplify(bmap);
4721 return isl_basic_map_finalize(bmap);
4723 isl_basic_map_free(bmap);
4727 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4728 enum isl_dim_type type, unsigned pos, int value)
4732 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4733 return isl_basic_map_fix_pos_si(bmap,
4734 isl_basic_map_offset(bmap, type) + pos, value);
4736 isl_basic_map_free(bmap);
4740 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4741 enum isl_dim_type type, unsigned pos, isl_int value)
4745 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4746 return isl_basic_map_fix_pos(bmap,
4747 isl_basic_map_offset(bmap, type) + pos, value);
4749 isl_basic_map_free(bmap);
4753 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4754 enum isl_dim_type type, unsigned pos, int value)
4756 return (struct isl_basic_set *)
4757 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4761 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4762 enum isl_dim_type type, unsigned pos, isl_int value)
4764 return (struct isl_basic_set *)
4765 isl_basic_map_fix((struct isl_basic_map *)bset,
4769 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4770 unsigned input, int value)
4772 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4775 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4776 unsigned dim, int value)
4778 return (struct isl_basic_set *)
4779 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4780 isl_dim_set, dim, value);
4783 struct isl_map *isl_map_fix_si(struct isl_map *map,
4784 enum isl_dim_type type, unsigned pos, int value)
4788 map = isl_map_cow(map);
4792 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4793 for (i = 0; i < map->n; ++i) {
4794 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4798 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4805 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4806 enum isl_dim_type type, unsigned pos, int value)
4808 return (struct isl_set *)
4809 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4812 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4813 enum isl_dim_type type, unsigned pos, isl_int value)
4817 map = isl_map_cow(map);
4821 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4822 for (i = 0; i < map->n; ++i) {
4823 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4827 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4834 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4835 enum isl_dim_type type, unsigned pos, isl_int value)
4837 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4840 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4841 unsigned input, int value)
4843 return isl_map_fix_si(map, isl_dim_in, input, value);
4846 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4848 return (struct isl_set *)
4849 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4852 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4853 __isl_take isl_basic_map *bmap,
4854 enum isl_dim_type type, unsigned pos, int value)
4860 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4861 pos += isl_basic_map_offset(bmap, type);
4862 bmap = isl_basic_map_cow(bmap);
4863 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4864 j = isl_basic_map_alloc_inequality(bmap);
4867 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4868 isl_int_set_si(bmap->ineq[j][pos], 1);
4869 isl_int_set_si(bmap->ineq[j][0], -value);
4870 bmap = isl_basic_map_simplify(bmap);
4871 return isl_basic_map_finalize(bmap);
4873 isl_basic_map_free(bmap);
4877 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4878 unsigned dim, isl_int value)
4882 bset = isl_basic_set_cow(bset);
4883 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4884 j = isl_basic_set_alloc_inequality(bset);
4887 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4888 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4889 isl_int_neg(bset->ineq[j][0], value);
4890 bset = isl_basic_set_simplify(bset);
4891 return isl_basic_set_finalize(bset);
4893 isl_basic_set_free(bset);
4897 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4898 enum isl_dim_type type, unsigned pos, int value)
4902 map = isl_map_cow(map);
4906 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4907 for (i = 0; i < map->n; ++i) {
4908 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4913 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4920 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4921 enum isl_dim_type type, unsigned pos, int value)
4923 return (struct isl_set *)
4924 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4927 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4932 set = isl_set_cow(set);
4936 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4937 for (i = 0; i < set->n; ++i) {
4938 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4948 struct isl_map *isl_map_reverse(struct isl_map *map)
4952 map = isl_map_cow(map);
4956 map->dim = isl_space_reverse(map->dim);
4959 for (i = 0; i < map->n; ++i) {
4960 map->p[i] = isl_basic_map_reverse(map->p[i]);
4964 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4971 static struct isl_map *isl_basic_map_partial_lexopt(
4972 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4973 struct isl_set **empty, int max)
4977 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4978 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4980 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4982 isl_basic_map_free(bmap);
4983 isl_basic_set_free(dom);
4989 struct isl_map *isl_basic_map_partial_lexmax(
4990 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4991 struct isl_set **empty)
4993 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4996 struct isl_map *isl_basic_map_partial_lexmin(
4997 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4998 struct isl_set **empty)
5000 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5003 struct isl_set *isl_basic_set_partial_lexmin(
5004 struct isl_basic_set *bset, struct isl_basic_set *dom,
5005 struct isl_set **empty)
5007 return (struct isl_set *)
5008 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5012 struct isl_set *isl_basic_set_partial_lexmax(
5013 struct isl_basic_set *bset, struct isl_basic_set *dom,
5014 struct isl_set **empty)
5016 return (struct isl_set *)
5017 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5021 /* Given a basic map "bmap", compute the lexicographically minimal
5022 * (or maximal) image element for each domain element in dom.
5023 * Set *empty to those elements in dom that do not have an image element.
5025 * We first make sure the basic sets in dom are disjoint and then
5026 * simply collect the results over each of the basic sets separately.
5027 * We could probably improve the efficiency a bit by moving the union
5028 * domain down into the parametric integer programming.
5030 static __isl_give isl_map *basic_map_partial_lexopt(
5031 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5032 __isl_give isl_set **empty, int max)
5035 struct isl_map *res;
5037 dom = isl_set_make_disjoint(dom);
5041 if (isl_set_plain_is_empty(dom)) {
5042 res = isl_map_empty_like_basic_map(bmap);
5043 *empty = isl_set_empty_like(dom);
5045 isl_basic_map_free(bmap);
5049 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5050 isl_basic_set_copy(dom->p[0]), empty, max);
5052 for (i = 1; i < dom->n; ++i) {
5053 struct isl_map *res_i;
5054 struct isl_set *empty_i;
5056 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5057 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5059 res = isl_map_union_disjoint(res, res_i);
5060 *empty = isl_set_union_disjoint(*empty, empty_i);
5064 isl_basic_map_free(bmap);
5069 isl_basic_map_free(bmap);
5073 /* Given a map "map", compute the lexicographically minimal
5074 * (or maximal) image element for each domain element in dom.
5075 * Set *empty to those elements in dom that do not have an image element.
5077 * We first compute the lexicographically minimal or maximal element
5078 * in the first basic map. This results in a partial solution "res"
5079 * and a subset "todo" of dom that still need to be handled.
5080 * We then consider each of the remaining maps in "map" and successively
5081 * improve both "res" and "todo".
5083 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5084 * Assume we are computing the lexicographical maximum.
5085 * We first compute the lexicographically maximal element in basic map i.
5086 * This results in a partial solution res_i and a subset todo_i.
5087 * Then we combine these results with those obtain for the first k basic maps
5088 * to obtain a result that is valid for the first k+1 basic maps.
5089 * In particular, the set where there is no solution is the set where
5090 * there is no solution for the first k basic maps and also no solution
5091 * for the ith basic map, i.e.,
5093 * todo^i = todo^k * todo_i
5095 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5096 * solutions, arbitrarily breaking ties in favor of res^k.
5097 * That is, when res^k(a) >= res_i(a), we pick res^k and
5098 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5099 * the lexicographic order.)
5100 * In practice, we compute
5102 * res^k * (res_i . "<=")
5106 * res_i * (res^k . "<")
5108 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5109 * where only one of res^k and res_i provides a solution and we simply pick
5116 * Note that we only compute these intersections when dom(res^k) intersects
5117 * dom(res_i). Otherwise, the only effect of these intersections is to
5118 * potentially break up res^k and res_i into smaller pieces.
5119 * We want to avoid such splintering as much as possible.
5120 * In fact, an earlier implementation of this function would look for
5121 * better results in the domain of res^k and for extra results in todo^k,
5122 * but this would always result in a splintering according to todo^k,
5123 * even when the domain of basic map i is disjoint from the domains of
5124 * the previous basic maps.
5126 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5127 __isl_take isl_map *map, __isl_take isl_set *dom,
5128 __isl_give isl_set **empty, int max)
5131 struct isl_map *res;
5132 struct isl_set *todo;
5137 if (isl_map_plain_is_empty(map)) {
5145 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5146 isl_set_copy(dom), &todo, max);
5148 for (i = 1; i < map->n; ++i) {
5152 isl_space *dim = isl_space_range(isl_map_get_space(res));
5154 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5155 isl_set_copy(dom), &todo_i, max);
5158 lt = isl_map_lex_lt(isl_space_copy(dim));
5159 le = isl_map_lex_le(dim);
5161 lt = isl_map_lex_gt(isl_space_copy(dim));
5162 le = isl_map_lex_ge(dim);
5164 lt = isl_map_apply_range(isl_map_copy(res), lt);
5165 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5166 le = isl_map_apply_range(isl_map_copy(res_i), le);
5167 le = isl_map_intersect(le, isl_map_copy(res));
5169 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5170 res = isl_map_intersect_domain(res,
5171 isl_set_copy(todo_i));
5172 res_i = isl_map_intersect_domain(res_i,
5173 isl_set_copy(todo));
5176 res = isl_map_union_disjoint(res, res_i);
5177 res = isl_map_union_disjoint(res, lt);
5178 res = isl_map_union_disjoint(res, le);
5180 todo = isl_set_intersect(todo, todo_i);
5200 /* Given a map "map", compute the lexicographically minimal
5201 * (or maximal) image element for each domain element in dom.
5202 * Set *empty to those elements in dom that do not have an image element.
5204 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5206 static __isl_give isl_map *isl_map_partial_lexopt(
5207 __isl_take isl_map *map, __isl_take isl_set *dom,
5208 __isl_give isl_set **empty, int max)
5212 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5213 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5214 if (!isl_space_has_named_params(map->dim) ||
5215 !isl_space_has_named_params(dom->dim))
5216 isl_die(map->ctx, isl_error_invalid,
5217 "unaligned unnamed parameters", goto error);
5218 map = isl_map_align_params(map, isl_map_get_space(dom));
5219 dom = isl_map_align_params(dom, isl_map_get_space(map));
5220 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5229 __isl_give isl_map *isl_map_partial_lexmax(
5230 __isl_take isl_map *map, __isl_take isl_set *dom,
5231 __isl_give isl_set **empty)
5233 return isl_map_partial_lexopt(map, dom, empty, 1);
5236 __isl_give isl_map *isl_map_partial_lexmin(
5237 __isl_take isl_map *map, __isl_take isl_set *dom,
5238 __isl_give isl_set **empty)
5240 return isl_map_partial_lexopt(map, dom, empty, 0);
5243 __isl_give isl_set *isl_set_partial_lexmin(
5244 __isl_take isl_set *set, __isl_take isl_set *dom,
5245 __isl_give isl_set **empty)
5247 return (struct isl_set *)
5248 isl_map_partial_lexmin((struct isl_map *)set,
5252 __isl_give isl_set *isl_set_partial_lexmax(
5253 __isl_take isl_set *set, __isl_take isl_set *dom,
5254 __isl_give isl_set **empty)
5256 return (struct isl_set *)
5257 isl_map_partial_lexmax((struct isl_map *)set,
5261 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5263 struct isl_basic_set *dom = NULL;
5268 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5269 dom = isl_basic_set_universe(dom_dim);
5270 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5272 isl_basic_map_free(bmap);
5276 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5278 return isl_basic_map_lexopt(bmap, 0);
5281 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5283 return isl_basic_map_lexopt(bmap, 1);
5286 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5288 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5291 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5293 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5296 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5298 struct isl_set *dom = NULL;
5303 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5304 dom = isl_set_universe(dom_dim);
5305 return isl_map_partial_lexopt(map, dom, NULL, max);
5311 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5313 return isl_map_lexopt(map, 0);
5316 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5318 return isl_map_lexopt(map, 1);
5321 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5323 return (isl_set *)isl_map_lexmin((isl_map *)set);
5326 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5328 return (isl_set *)isl_map_lexmax((isl_map *)set);
5331 /* Construct a map that equates the two given dimensions in the given space.
5333 static __isl_give isl_map *equate(__isl_take isl_space *dim,
5334 enum isl_dim_type src_type, int src_pos,
5335 enum isl_dim_type dst_type, int dst_pos)
5337 isl_basic_map *bmap;
5340 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5341 k = isl_basic_map_alloc_equality(bmap);
5344 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
5345 src_pos += isl_basic_map_offset(bmap, src_type);
5346 dst_pos += isl_basic_map_offset(bmap, dst_type);
5347 isl_int_set_si(bmap->eq[k][src_pos], 1);
5348 isl_int_set_si(bmap->eq[k][dst_pos], -1);
5350 return isl_map_from_basic_map(bmap);
5352 isl_basic_map_free(bmap);
5356 /* Extract the first and only affine expression from list
5357 * and then add it to *pwaff with the given dom.
5358 * This domain is known to be disjoint from other domains
5359 * because of the way isl_basic_set_foreach_lexmax works.
5361 static int update_dim_opt(__isl_take isl_basic_set *dom,
5362 __isl_take isl_aff_list *list, void *user)
5364 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5366 isl_pw_aff **pwaff = user;
5367 isl_pw_aff *pwaff_i;
5369 if (isl_aff_list_n_aff(list) != 1)
5370 isl_die(ctx, isl_error_internal,
5371 "expecting single element list", goto error);
5373 aff = isl_aff_list_get_aff(list, 0);
5374 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5376 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5378 isl_aff_list_free(list);
5382 isl_basic_set_free(dom);
5383 isl_aff_list_free(list);
5387 /* Given a one-dimensional basic set, compute the minimum or maximum of that
5388 * dimension as an isl_pw_aff.
5390 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexopt
5391 * call update_dim_opt on each leaf of the result.
5393 static __isl_give isl_pw_aff *basic_set_dim_opt(__isl_keep isl_basic_set *bset,
5396 isl_space *dim = isl_basic_set_get_space(bset);
5400 dim = isl_space_params(dim);
5401 pwaff = isl_pw_aff_empty(dim);
5403 r = isl_basic_set_foreach_lexopt(bset, max, &update_dim_opt, &pwaff);
5405 return isl_pw_aff_free(pwaff);
5410 /* Compute the minimum or maximum of the given set dimension
5411 * as a function of the parameters, but independently of
5412 * the other set dimensions.
5414 * We first project the set onto the given dimension and then compute
5415 * the "lexicographic" maximum in each basic set, combining the results
5416 * using isl_pw_aff_union_max.
5418 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5425 map = isl_map_from_domain(set);
5426 map = isl_map_add_dims(map, isl_dim_out, 1);
5427 map = isl_map_intersect(map,
5428 equate(isl_map_get_space(map), isl_dim_in, pos,
5430 set = isl_map_range(map);
5435 isl_space *dim = isl_set_get_space(set);
5436 dim = isl_space_domain(isl_space_from_range(dim));
5438 return isl_pw_aff_empty(dim);
5441 pwaff = basic_set_dim_opt(set->p[0], max);
5442 for (i = 1; i < set->n; ++i) {
5443 isl_pw_aff *pwaff_i;
5445 pwaff_i = basic_set_dim_opt(set->p[i], max);
5446 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5454 /* Compute the maximum of the given set dimension as a function of the
5455 * parameters, but independently of the other set dimensions.
5457 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5459 return set_dim_opt(set, pos, 1);
5462 /* Compute the minimum of the given set dimension as a function of the
5463 * parameters, but independently of the other set dimensions.
5465 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5467 return set_dim_opt(set, pos, 0);
5470 /* Apply a preimage specified by "mat" on the parameters of "bset".
5471 * bset is assumed to have only parameters and divs.
5473 static struct isl_basic_set *basic_set_parameter_preimage(
5474 struct isl_basic_set *bset, struct isl_mat *mat)
5481 bset->dim = isl_space_cow(bset->dim);
5485 nparam = isl_basic_set_dim(bset, isl_dim_param);
5487 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5489 bset->dim->nparam = 0;
5490 bset->dim->n_out = nparam;
5491 bset = isl_basic_set_preimage(bset, mat);
5493 bset->dim->nparam = bset->dim->n_out;
5494 bset->dim->n_out = 0;
5499 isl_basic_set_free(bset);
5503 /* Apply a preimage specified by "mat" on the parameters of "set".
5504 * set is assumed to have only parameters and divs.
5506 static struct isl_set *set_parameter_preimage(
5507 struct isl_set *set, struct isl_mat *mat)
5509 isl_space *dim = NULL;
5515 dim = isl_space_copy(set->dim);
5516 dim = isl_space_cow(dim);
5520 nparam = isl_set_dim(set, isl_dim_param);
5522 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5525 dim->n_out = nparam;
5526 isl_set_reset_space(set, dim);
5527 set = isl_set_preimage(set, mat);
5530 dim = isl_space_copy(set->dim);
5531 dim = isl_space_cow(dim);
5534 dim->nparam = dim->n_out;
5536 isl_set_reset_space(set, dim);
5539 isl_space_free(dim);
5546 /* Intersect the basic set "bset" with the affine space specified by the
5547 * equalities in "eq".
5549 static struct isl_basic_set *basic_set_append_equalities(
5550 struct isl_basic_set *bset, struct isl_mat *eq)
5558 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5563 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5564 for (i = 0; i < eq->n_row; ++i) {
5565 k = isl_basic_set_alloc_equality(bset);
5568 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5569 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5573 bset = isl_basic_set_gauss(bset, NULL);
5574 bset = isl_basic_set_finalize(bset);
5579 isl_basic_set_free(bset);
5583 /* Intersect the set "set" with the affine space specified by the
5584 * equalities in "eq".
5586 static struct isl_set *set_append_equalities(struct isl_set *set,
5594 for (i = 0; i < set->n; ++i) {
5595 set->p[i] = basic_set_append_equalities(set->p[i],
5608 /* Project the given basic set onto its parameter domain, possibly introducing
5609 * new, explicit, existential variables in the constraints.
5610 * The input has parameters and (possibly implicit) existential variables.
5611 * The output has the same parameters, but only
5612 * explicit existentially quantified variables.
5614 * The actual projection is performed by pip, but pip doesn't seem
5615 * to like equalities very much, so we first remove the equalities
5616 * among the parameters by performing a variable compression on
5617 * the parameters. Afterward, an inverse transformation is performed
5618 * and the equalities among the parameters are inserted back in.
5620 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5624 struct isl_mat *T, *T2;
5625 struct isl_set *set;
5626 unsigned nparam, n_div;
5628 bset = isl_basic_set_cow(bset);
5632 if (bset->n_eq == 0)
5633 return isl_basic_set_lexmin(bset);
5635 isl_basic_set_gauss(bset, NULL);
5637 nparam = isl_basic_set_dim(bset, isl_dim_param);
5638 n_div = isl_basic_set_dim(bset, isl_dim_div);
5640 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5641 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5644 if (i == bset->n_eq)
5645 return isl_basic_set_lexmin(bset);
5647 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5649 eq = isl_mat_cow(eq);
5650 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5651 if (T && T->n_col == 0) {
5655 bset = isl_basic_set_set_to_empty(bset);
5656 return isl_set_from_basic_set(bset);
5658 bset = basic_set_parameter_preimage(bset, T);
5660 set = isl_basic_set_lexmin(bset);
5661 set = set_parameter_preimage(set, T2);
5662 set = set_append_equalities(set, eq);
5666 /* Compute an explicit representation for all the existentially
5667 * quantified variables.
5668 * The input and output dimensions are first turned into parameters.
5669 * compute_divs then returns a map with the same parameters and
5670 * no input or output dimensions and the dimension specification
5671 * is reset to that of the input.
5673 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5675 struct isl_basic_set *bset;
5676 struct isl_set *set;
5677 struct isl_map *map;
5678 isl_space *dim, *orig_dim = NULL;
5683 bmap = isl_basic_map_cow(bmap);
5687 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5688 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5689 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5690 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5694 orig_dim = bmap->dim;
5696 bset = (struct isl_basic_set *)bmap;
5698 set = parameter_compute_divs(bset);
5699 map = (struct isl_map *)set;
5700 map = isl_map_reset_space(map, orig_dim);
5704 isl_basic_map_free(bmap);
5708 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5716 off = isl_space_dim(bmap->dim, isl_dim_all);
5717 for (i = 0; i < bmap->n_div; ++i) {
5718 if (isl_int_is_zero(bmap->div[i][0]))
5720 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5726 static int map_divs_known(__isl_keep isl_map *map)
5733 for (i = 0; i < map->n; ++i) {
5734 int known = isl_basic_map_divs_known(map->p[i]);
5742 /* If bmap contains any unknown divs, then compute explicit
5743 * expressions for them. However, this computation may be
5744 * quite expensive, so first try to remove divs that aren't
5747 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5750 struct isl_map *map;
5752 known = isl_basic_map_divs_known(bmap);
5756 return isl_map_from_basic_map(bmap);
5758 bmap = isl_basic_map_drop_redundant_divs(bmap);
5760 known = isl_basic_map_divs_known(bmap);
5764 return isl_map_from_basic_map(bmap);
5766 map = compute_divs(bmap);
5769 isl_basic_map_free(bmap);
5773 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5777 struct isl_map *res;
5784 known = map_divs_known(map);
5792 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5793 for (i = 1 ; i < map->n; ++i) {
5795 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5796 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5797 res = isl_map_union_disjoint(res, r2);
5799 res = isl_map_union(res, r2);
5806 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5808 return (struct isl_set *)
5809 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5812 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5814 return (struct isl_set *)
5815 isl_map_compute_divs((struct isl_map *)set);
5818 struct isl_set *isl_map_domain(struct isl_map *map)
5821 struct isl_set *set;
5826 map = isl_map_cow(map);
5830 set = (struct isl_set *)map;
5831 set->dim = isl_space_domain(set->dim);
5834 for (i = 0; i < map->n; ++i) {
5835 set->p[i] = isl_basic_map_domain(map->p[i]);
5839 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5840 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5847 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
5848 __isl_take isl_map *map2)
5852 struct isl_map *map = NULL;
5866 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
5868 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5869 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5870 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5872 map = isl_map_alloc_space(isl_space_copy(map1->dim),
5873 map1->n + map2->n, flags);
5876 for (i = 0; i < map1->n; ++i) {
5877 map = isl_map_add_basic_map(map,
5878 isl_basic_map_copy(map1->p[i]));
5882 for (i = 0; i < map2->n; ++i) {
5883 map = isl_map_add_basic_map(map,
5884 isl_basic_map_copy(map2->p[i]));
5898 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
5899 __isl_take isl_map *map2)
5901 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
5904 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5906 map1 = isl_map_union_disjoint(map1, map2);
5910 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5914 struct isl_set *isl_set_union_disjoint(
5915 struct isl_set *set1, struct isl_set *set2)
5917 return (struct isl_set *)
5918 isl_map_union_disjoint(
5919 (struct isl_map *)set1, (struct isl_map *)set2);
5922 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5924 return (struct isl_set *)
5925 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5928 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
5929 __isl_take isl_set *set)
5932 struct isl_map *result;
5938 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5939 isl_die(set->ctx, isl_error_invalid,
5940 "parameters don't match", goto error);
5942 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
5943 !isl_map_compatible_range(map, set))
5944 isl_die(set->ctx, isl_error_invalid,
5945 "incompatible spaces", goto error);
5947 if (isl_set_plain_is_universe(set)) {
5952 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5953 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5954 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5956 result = isl_map_alloc_space(isl_space_copy(map->dim),
5957 map->n * set->n, flags);
5960 for (i = 0; i < map->n; ++i)
5961 for (j = 0; j < set->n; ++j) {
5962 result = isl_map_add_basic_map(result,
5963 isl_basic_map_intersect_range(
5964 isl_basic_map_copy(map->p[i]),
5965 isl_basic_set_copy(set->p[j])));
5978 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
5979 __isl_take isl_set *set)
5981 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
5984 struct isl_map *isl_map_intersect_domain(
5985 struct isl_map *map, struct isl_set *set)
5987 return isl_map_reverse(
5988 isl_map_intersect_range(isl_map_reverse(map), set));
5991 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
5992 __isl_take isl_map *map2)
5996 map1 = isl_map_reverse(map1);
5997 map1 = isl_map_apply_range(map1, map2);
5998 return isl_map_reverse(map1);
6005 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6006 __isl_take isl_map *map2)
6008 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6011 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6012 __isl_take isl_map *map2)
6014 isl_space *dim_result;
6015 struct isl_map *result;
6021 dim_result = isl_space_join(isl_space_copy(map1->dim),
6022 isl_space_copy(map2->dim));
6024 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6027 for (i = 0; i < map1->n; ++i)
6028 for (j = 0; j < map2->n; ++j) {
6029 result = isl_map_add_basic_map(result,
6030 isl_basic_map_apply_range(
6031 isl_basic_map_copy(map1->p[i]),
6032 isl_basic_map_copy(map2->p[j])));
6038 if (result && result->n <= 1)
6039 ISL_F_SET(result, ISL_MAP_DISJOINT);
6047 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6048 __isl_take isl_map *map2)
6050 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6054 * returns range - domain
6056 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6058 isl_space *dims, *target_dim;
6059 struct isl_basic_set *bset;
6066 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6067 bmap->dim, isl_dim_out),
6069 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6070 dim = isl_basic_map_n_in(bmap);
6071 nparam = isl_basic_map_n_param(bmap);
6072 bset = isl_basic_set_from_basic_map(bmap);
6073 bset = isl_basic_set_cow(bset);
6074 dims = isl_basic_set_get_space(bset);
6075 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6076 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6077 bset = isl_basic_set_swap_vars(bset, 2*dim);
6078 for (i = 0; i < dim; ++i) {
6079 int j = isl_basic_map_alloc_equality(
6080 (struct isl_basic_map *)bset);
6083 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6084 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6085 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6086 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6088 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6089 bset = isl_basic_set_reset_space(bset, target_dim);
6092 isl_basic_map_free(bmap);
6097 * returns range - domain
6099 struct isl_set *isl_map_deltas(struct isl_map *map)
6103 struct isl_set *result;
6108 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6109 map->dim, isl_dim_out),
6111 dim = isl_map_get_space(map);
6112 dim = isl_space_domain(dim);
6113 result = isl_set_alloc_space(dim, map->n, 0);
6116 for (i = 0; i < map->n; ++i)
6117 result = isl_set_add_basic_set(result,
6118 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6127 * returns [domain -> range] -> range - domain
6129 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6130 __isl_take isl_basic_map *bmap)
6134 isl_basic_map *domain;
6138 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6139 isl_die(bmap->ctx, isl_error_invalid,
6140 "domain and range don't match", goto error);
6142 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6143 n = isl_basic_map_dim(bmap, isl_dim_in);
6145 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6146 domain = isl_basic_map_universe(dim);
6148 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6149 bmap = isl_basic_map_apply_range(bmap, domain);
6150 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6152 total = isl_basic_map_total_dim(bmap);
6154 for (i = 0; i < n; ++i) {
6155 k = isl_basic_map_alloc_equality(bmap);
6158 isl_seq_clr(bmap->eq[k], 1 + total);
6159 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6160 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6161 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6164 bmap = isl_basic_map_gauss(bmap, NULL);
6165 return isl_basic_map_finalize(bmap);
6167 isl_basic_map_free(bmap);
6172 * returns [domain -> range] -> range - domain
6174 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6177 isl_space *domain_dim;
6182 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6183 isl_die(map->ctx, isl_error_invalid,
6184 "domain and range don't match", goto error);
6186 map = isl_map_cow(map);
6190 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6191 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6192 map->dim = isl_space_join(map->dim, domain_dim);
6195 for (i = 0; i < map->n; ++i) {
6196 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6200 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6207 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6209 struct isl_basic_map *bmap;
6217 nparam = dims->nparam;
6219 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6223 for (i = 0; i < dim; ++i) {
6224 int j = isl_basic_map_alloc_equality(bmap);
6227 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6228 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6229 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6231 return isl_basic_map_finalize(bmap);
6233 isl_basic_map_free(bmap);
6237 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6241 if (dim->n_in != dim->n_out)
6242 isl_die(dim->ctx, isl_error_invalid,
6243 "number of input and output dimensions needs to be "
6244 "the same", goto error);
6245 return basic_map_identity(dim);
6247 isl_space_free(dim);
6251 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6253 if (!model || !model->dim)
6255 return isl_basic_map_identity(isl_space_copy(model->dim));
6258 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6260 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6263 struct isl_map *isl_map_identity_like(struct isl_map *model)
6265 if (!model || !model->dim)
6267 return isl_map_identity(isl_space_copy(model->dim));
6270 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6272 if (!model || !model->dim)
6274 return isl_map_identity(isl_space_copy(model->dim));
6277 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6279 isl_space *dim = isl_set_get_space(set);
6281 id = isl_map_identity(isl_space_map_from_set(dim));
6282 return isl_map_intersect_range(id, set);
6285 /* Construct a basic set with all set dimensions having only non-negative
6288 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6289 __isl_take isl_space *space)
6294 struct isl_basic_set *bset;
6298 nparam = space->nparam;
6300 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6303 for (i = 0; i < dim; ++i) {
6304 int k = isl_basic_set_alloc_inequality(bset);
6307 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6308 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6312 isl_basic_set_free(bset);
6316 /* Construct the half-space x_pos >= 0.
6318 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6322 isl_basic_set *nonneg;
6324 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6325 k = isl_basic_set_alloc_inequality(nonneg);
6328 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6329 isl_int_set_si(nonneg->ineq[k][pos], 1);
6331 return isl_basic_set_finalize(nonneg);
6333 isl_basic_set_free(nonneg);
6337 /* Construct the half-space x_pos <= -1.
6339 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6344 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6345 k = isl_basic_set_alloc_inequality(neg);
6348 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6349 isl_int_set_si(neg->ineq[k][0], -1);
6350 isl_int_set_si(neg->ineq[k][pos], -1);
6352 return isl_basic_set_finalize(neg);
6354 isl_basic_set_free(neg);
6358 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6359 enum isl_dim_type type, unsigned first, unsigned n)
6362 isl_basic_set *nonneg;
6370 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6372 for (i = 0; i < n; ++i) {
6373 nonneg = nonneg_halfspace(isl_set_get_space(set),
6374 pos(set->dim, type) + first + i);
6375 neg = neg_halfspace(isl_set_get_space(set),
6376 pos(set->dim, type) + first + i);
6378 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6387 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6388 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6395 if (isl_set_plain_is_empty(set)) {
6400 return fn(set, signs, user);
6403 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6405 half = isl_set_intersect(half, isl_set_copy(set));
6406 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6410 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6412 half = isl_set_intersect(half, set);
6413 return foreach_orthant(half, signs, first + 1, len, fn, user);
6419 /* Call "fn" on the intersections of "set" with each of the orthants
6420 * (except for obviously empty intersections). The orthant is identified
6421 * by the signs array, with each entry having value 1 or -1 according
6422 * to the sign of the corresponding variable.
6424 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6425 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6435 if (isl_set_plain_is_empty(set))
6438 nparam = isl_set_dim(set, isl_dim_param);
6439 nvar = isl_set_dim(set, isl_dim_set);
6441 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6443 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6451 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6453 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6456 int isl_basic_map_is_subset(
6457 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6460 struct isl_map *map1;
6461 struct isl_map *map2;
6463 if (!bmap1 || !bmap2)
6466 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6467 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6469 is_subset = isl_map_is_subset(map1, map2);
6477 int isl_basic_map_is_equal(
6478 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6482 if (!bmap1 || !bmap2)
6484 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6487 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6491 int isl_basic_set_is_equal(
6492 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6494 return isl_basic_map_is_equal(
6495 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6498 int isl_map_is_empty(struct isl_map *map)
6505 for (i = 0; i < map->n; ++i) {
6506 is_empty = isl_basic_map_is_empty(map->p[i]);
6515 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6517 return map ? map->n == 0 : -1;
6520 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6522 return isl_map_plain_is_empty(map);
6525 int isl_set_plain_is_empty(struct isl_set *set)
6527 return set ? set->n == 0 : -1;
6530 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6532 return isl_set_plain_is_empty(set);
6535 int isl_set_is_empty(struct isl_set *set)
6537 return isl_map_is_empty((struct isl_map *)set);
6540 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6545 return isl_space_is_equal(map1->dim, map2->dim);
6548 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6553 return isl_space_is_equal(set1->dim, set2->dim);
6556 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6562 is_subset = isl_map_is_subset(map1, map2);
6565 is_subset = isl_map_is_subset(map2, map1);
6569 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6571 return align_params_map_map_and_test(map1, map2, &map_is_equal);
6574 int isl_basic_map_is_strict_subset(
6575 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6579 if (!bmap1 || !bmap2)
6581 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6584 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6585 if (is_subset == -1)
6590 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6596 is_subset = isl_map_is_subset(map1, map2);
6599 is_subset = isl_map_is_subset(map2, map1);
6600 if (is_subset == -1)
6605 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6607 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6610 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6614 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6617 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6621 return bset->n_eq == 0 && bset->n_ineq == 0;
6624 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6631 for (i = 0; i < map->n; ++i) {
6632 int r = isl_basic_map_is_universe(map->p[i]);
6640 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6642 return isl_map_plain_is_universe((isl_map *) set);
6645 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6647 return isl_set_plain_is_universe(set);
6650 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6652 struct isl_basic_set *bset = NULL;
6653 struct isl_vec *sample = NULL;
6660 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6663 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6664 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6665 copy = isl_basic_map_remove_redundancies(copy);
6666 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6667 isl_basic_map_free(copy);
6671 total = 1 + isl_basic_map_total_dim(bmap);
6672 if (bmap->sample && bmap->sample->size == total) {
6673 int contains = isl_basic_map_contains(bmap, bmap->sample);
6679 isl_vec_free(bmap->sample);
6680 bmap->sample = NULL;
6681 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6684 sample = isl_basic_set_sample_vec(bset);
6687 empty = sample->size == 0;
6688 isl_vec_free(bmap->sample);
6689 bmap->sample = sample;
6691 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6696 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6700 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6703 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6705 return isl_basic_map_plain_is_empty(bmap);
6708 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6712 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6715 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6717 return isl_basic_set_plain_is_empty(bset);
6720 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6722 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6725 struct isl_map *isl_basic_map_union(
6726 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6728 struct isl_map *map;
6729 if (!bmap1 || !bmap2)
6732 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
6734 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
6737 map = isl_map_add_basic_map(map, bmap1);
6738 map = isl_map_add_basic_map(map, bmap2);
6741 isl_basic_map_free(bmap1);
6742 isl_basic_map_free(bmap2);
6746 struct isl_set *isl_basic_set_union(
6747 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6749 return (struct isl_set *)isl_basic_map_union(
6750 (struct isl_basic_map *)bset1,
6751 (struct isl_basic_map *)bset2);
6754 /* Order divs such that any div only depends on previous divs */
6755 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6763 off = isl_space_dim(bmap->dim, isl_dim_all);
6765 for (i = 0; i < bmap->n_div; ++i) {
6767 if (isl_int_is_zero(bmap->div[i][0]))
6769 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6773 isl_basic_map_swap_div(bmap, i, i + pos);
6779 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6781 return (struct isl_basic_set *)
6782 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6785 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6792 for (i = 0; i < map->n; ++i) {
6793 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6804 /* Apply the expansion computed by isl_merge_divs.
6805 * The expansion itself is given by "exp" while the resulting
6806 * list of divs is given by "div".
6808 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6809 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6814 bset = isl_basic_set_cow(bset);
6818 if (div->n_row < bset->n_div)
6819 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6820 "not an expansion", goto error);
6822 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
6823 div->n_row - bset->n_div, 0,
6824 2 * (div->n_row - bset->n_div));
6826 n_div = bset->n_div;
6827 for (i = n_div; i < div->n_row; ++i)
6828 if (isl_basic_set_alloc_div(bset) < 0)
6832 for (i = div->n_row - 1; i >= 0; --i) {
6833 if (j >= 0 && exp[j] == i) {
6835 isl_basic_map_swap_div(bset, i, j);
6838 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6839 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6847 isl_basic_set_free(bset);
6852 /* Look for a div in dst that corresponds to the div "div" in src.
6853 * The divs before "div" in src and dst are assumed to be the same.
6855 * Returns -1 if no corresponding div was found and the position
6856 * of the corresponding div in dst otherwise.
6858 static int find_div(struct isl_basic_map *dst,
6859 struct isl_basic_map *src, unsigned div)
6863 unsigned total = isl_space_dim(src->dim, isl_dim_all);
6865 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6866 for (i = div; i < dst->n_div; ++i)
6867 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6868 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6869 dst->n_div - div) == -1)
6874 struct isl_basic_map *isl_basic_map_align_divs(
6875 struct isl_basic_map *dst, struct isl_basic_map *src)
6878 unsigned total = isl_space_dim(src->dim, isl_dim_all);
6883 if (src->n_div == 0)
6886 for (i = 0; i < src->n_div; ++i)
6887 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6889 src = isl_basic_map_order_divs(src);
6890 dst = isl_basic_map_cow(dst);
6891 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
6892 src->n_div, 0, 2 * src->n_div);
6895 for (i = 0; i < src->n_div; ++i) {
6896 int j = find_div(dst, src, i);
6898 j = isl_basic_map_alloc_div(dst);
6901 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6902 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6903 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6907 isl_basic_map_swap_div(dst, i, j);
6911 isl_basic_map_free(dst);
6915 struct isl_basic_set *isl_basic_set_align_divs(
6916 struct isl_basic_set *dst, struct isl_basic_set *src)
6918 return (struct isl_basic_set *)isl_basic_map_align_divs(
6919 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6922 struct isl_map *isl_map_align_divs(struct isl_map *map)
6930 map = isl_map_compute_divs(map);
6931 map = isl_map_cow(map);
6935 for (i = 1; i < map->n; ++i)
6936 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6937 for (i = 1; i < map->n; ++i)
6938 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6940 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6944 struct isl_set *isl_set_align_divs(struct isl_set *set)
6946 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6949 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
6950 __isl_take isl_map *map)
6954 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6955 map = isl_map_intersect_domain(map, set);
6956 set = isl_map_range(map);
6964 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
6965 __isl_take isl_map *map)
6967 return isl_map_align_params_map_map_and(set, map, &set_apply);
6970 /* There is no need to cow as removing empty parts doesn't change
6971 * the meaning of the set.
6973 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6980 for (i = map->n-1; i >= 0; --i) {
6981 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6983 isl_basic_map_free(map->p[i]);
6984 if (i != map->n-1) {
6985 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6986 map->p[i] = map->p[map->n-1];
6994 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6996 return (struct isl_set *)
6997 isl_map_remove_empty_parts((struct isl_map *)set);
7000 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7002 struct isl_basic_map *bmap;
7003 if (!map || map->n == 0)
7005 bmap = map->p[map->n-1];
7006 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7007 return isl_basic_map_copy(bmap);
7010 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7012 return (struct isl_basic_set *)
7013 isl_map_copy_basic_map((struct isl_map *)set);
7016 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7017 __isl_keep isl_basic_map *bmap)
7023 for (i = map->n-1; i >= 0; --i) {
7024 if (map->p[i] != bmap)
7026 map = isl_map_cow(map);
7029 isl_basic_map_free(map->p[i]);
7030 if (i != map->n-1) {
7031 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7032 map->p[i] = map->p[map->n-1];
7043 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7044 struct isl_basic_set *bset)
7046 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7047 (struct isl_basic_map *)bset);
7050 /* Given two basic sets bset1 and bset2, compute the maximal difference
7051 * between the values of dimension pos in bset1 and those in bset2
7052 * for any common value of the parameters and dimensions preceding pos.
7054 static enum isl_lp_result basic_set_maximal_difference_at(
7055 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7056 int pos, isl_int *opt)
7059 struct isl_basic_map *bmap1 = NULL;
7060 struct isl_basic_map *bmap2 = NULL;
7061 struct isl_ctx *ctx;
7062 struct isl_vec *obj;
7065 unsigned dim1, dim2;
7066 enum isl_lp_result res;
7068 if (!bset1 || !bset2)
7069 return isl_lp_error;
7071 nparam = isl_basic_set_n_param(bset1);
7072 dim1 = isl_basic_set_n_dim(bset1);
7073 dim2 = isl_basic_set_n_dim(bset2);
7074 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7075 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7076 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7077 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7078 if (!bmap1 || !bmap2)
7080 bmap1 = isl_basic_map_cow(bmap1);
7081 bmap1 = isl_basic_map_extend(bmap1, nparam,
7082 pos, (dim1 - pos) + (dim2 - pos),
7083 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7084 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7087 total = isl_basic_map_total_dim(bmap1);
7089 obj = isl_vec_alloc(ctx, 1 + total);
7090 isl_seq_clr(obj->block.data, 1 + total);
7091 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7092 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7095 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7097 isl_basic_map_free(bmap1);
7101 isl_basic_map_free(bmap1);
7102 isl_basic_map_free(bmap2);
7103 return isl_lp_error;
7106 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7107 * for any common value of the parameters and dimensions preceding pos
7108 * in both basic sets, the values of dimension pos in bset1 are
7109 * smaller or larger than those in bset2.
7112 * 1 if bset1 follows bset2
7113 * -1 if bset1 precedes bset2
7114 * 0 if bset1 and bset2 are incomparable
7115 * -2 if some error occurred.
7117 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7118 struct isl_basic_set *bset2, int pos)
7121 enum isl_lp_result res;
7126 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7128 if (res == isl_lp_empty)
7130 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7131 res == isl_lp_unbounded)
7133 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7142 /* Given two basic sets bset1 and bset2, check whether
7143 * for any common value of the parameters and dimensions preceding pos
7144 * there is a value of dimension pos in bset1 that is larger
7145 * than a value of the same dimension in bset2.
7148 * 1 if there exists such a pair
7149 * 0 if there is no such pair, but there is a pair of equal values
7151 * -2 if some error occurred.
7153 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7154 __isl_keep isl_basic_set *bset2, int pos)
7157 enum isl_lp_result res;
7162 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7164 if (res == isl_lp_empty)
7166 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7167 res == isl_lp_unbounded)
7169 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7171 else if (res == isl_lp_ok)
7180 /* Given two sets set1 and set2, check whether
7181 * for any common value of the parameters and dimensions preceding pos
7182 * there is a value of dimension pos in set1 that is larger
7183 * than a value of the same dimension in set2.
7186 * 1 if there exists such a pair
7187 * 0 if there is no such pair, but there is a pair of equal values
7189 * -2 if some error occurred.
7191 int isl_set_follows_at(__isl_keep isl_set *set1,
7192 __isl_keep isl_set *set2, int pos)
7200 for (i = 0; i < set1->n; ++i)
7201 for (j = 0; j < set2->n; ++j) {
7203 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7204 if (f == 1 || f == -2)
7213 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7214 unsigned pos, isl_int *val)
7222 total = isl_basic_map_total_dim(bmap);
7223 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7224 for (; d+1 > pos; --d)
7225 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7229 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7231 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7233 if (!isl_int_is_one(bmap->eq[i][1+d]))
7236 isl_int_neg(*val, bmap->eq[i][0]);
7242 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7243 unsigned pos, isl_int *val)
7255 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7258 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7259 for (i = 1; fixed == 1 && i < map->n; ++i) {
7260 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7261 if (fixed == 1 && isl_int_ne(tmp, v))
7265 isl_int_set(*val, v);
7271 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7272 unsigned pos, isl_int *val)
7274 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7278 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7281 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7284 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7285 enum isl_dim_type type, unsigned pos, isl_int *val)
7287 if (pos >= isl_basic_map_dim(bmap, type))
7289 return isl_basic_map_plain_has_fixed_var(bmap,
7290 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7293 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7294 enum isl_dim_type type, unsigned pos, isl_int *val)
7296 if (pos >= isl_map_dim(map, type))
7298 return isl_map_plain_has_fixed_var(map,
7299 map_offset(map, type) - 1 + pos, val);
7302 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7303 enum isl_dim_type type, unsigned pos, isl_int *val)
7305 return isl_map_plain_is_fixed(map, type, pos, val);
7308 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7309 * then return this fixed value in *val.
7311 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7312 unsigned dim, isl_int *val)
7314 return isl_basic_set_plain_has_fixed_var(bset,
7315 isl_basic_set_n_param(bset) + dim, val);
7318 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7319 * then return this fixed value in *val.
7321 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7322 unsigned dim, isl_int *val)
7324 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7327 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7328 unsigned dim, isl_int *val)
7330 return isl_set_plain_dim_is_fixed(set, dim, val);
7333 /* Check if input variable in has fixed value and if so and if val is not NULL,
7334 * then return this fixed value in *val.
7336 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7337 unsigned in, isl_int *val)
7339 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7342 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7343 * and if val is not NULL, then return this lower bound in *val.
7345 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7346 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7348 int i, i_eq = -1, i_ineq = -1;
7355 total = isl_basic_set_total_dim(bset);
7356 nparam = isl_basic_set_n_param(bset);
7357 for (i = 0; i < bset->n_eq; ++i) {
7358 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7364 for (i = 0; i < bset->n_ineq; ++i) {
7365 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7367 if (i_eq != -1 || i_ineq != -1)
7371 if (i_eq == -1 && i_ineq == -1)
7373 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7374 /* The coefficient should always be one due to normalization. */
7375 if (!isl_int_is_one(c[1+nparam+dim]))
7377 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7379 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7380 total - nparam - dim - 1) != -1)
7383 isl_int_neg(*val, c[0]);
7387 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7388 unsigned dim, isl_int *val)
7400 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7404 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7406 for (i = 1; fixed == 1 && i < set->n; ++i) {
7407 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7409 if (fixed == 1 && isl_int_ne(tmp, v))
7413 isl_int_set(*val, v);
7424 /* uset_gist depends on constraints without existentially quantified
7425 * variables sorting first.
7427 static int qsort_constraint_cmp(const void *p1, const void *p2)
7429 const struct constraint *c1 = (const struct constraint *)p1;
7430 const struct constraint *c2 = (const struct constraint *)p2;
7432 unsigned size = isl_min(c1->size, c2->size);
7434 l1 = isl_seq_last_non_zero(c1->c, size);
7435 l2 = isl_seq_last_non_zero(c2->c, size);
7440 return isl_seq_cmp(c1->c, c2->c, size);
7443 static struct isl_basic_map *isl_basic_map_sort_constraints(
7444 struct isl_basic_map *bmap)
7447 struct constraint *c;
7452 total = isl_basic_map_total_dim(bmap);
7453 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7456 for (i = 0; i < bmap->n_ineq; ++i) {
7458 c[i].c = bmap->ineq[i];
7460 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7461 for (i = 0; i < bmap->n_ineq; ++i)
7462 bmap->ineq[i] = c[i].c;
7466 isl_basic_map_free(bmap);
7470 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7471 __isl_take isl_basic_set *bset)
7473 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7474 (struct isl_basic_map *)bset);
7477 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7481 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7483 bmap = isl_basic_map_remove_redundancies(bmap);
7484 bmap = isl_basic_map_sort_constraints(bmap);
7485 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7489 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7491 return (struct isl_basic_set *)isl_basic_map_normalize(
7492 (struct isl_basic_map *)bset);
7495 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7496 const __isl_keep isl_basic_map *bmap2)
7503 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7504 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7505 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7506 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7507 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7508 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7509 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7510 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7511 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7512 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7513 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7515 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7517 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7519 if (bmap1->n_eq != bmap2->n_eq)
7520 return bmap1->n_eq - bmap2->n_eq;
7521 if (bmap1->n_ineq != bmap2->n_ineq)
7522 return bmap1->n_ineq - bmap2->n_ineq;
7523 if (bmap1->n_div != bmap2->n_div)
7524 return bmap1->n_div - bmap2->n_div;
7525 total = isl_basic_map_total_dim(bmap1);
7526 for (i = 0; i < bmap1->n_eq; ++i) {
7527 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7531 for (i = 0; i < bmap1->n_ineq; ++i) {
7532 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7536 for (i = 0; i < bmap1->n_div; ++i) {
7537 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7544 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7545 __isl_keep isl_basic_map *bmap2)
7547 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7550 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7551 __isl_keep isl_basic_set *bset2)
7553 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7554 (isl_basic_map *)bset2);
7557 static int qsort_bmap_cmp(const void *p1, const void *p2)
7559 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7560 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7562 return isl_basic_map_plain_cmp(bmap1, bmap2);
7565 /* We normalize in place, but if anything goes wrong we need
7566 * to return NULL, so we need to make sure we don't change the
7567 * meaning of any possible other copies of map.
7569 struct isl_map *isl_map_normalize(struct isl_map *map)
7572 struct isl_basic_map *bmap;
7576 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7578 for (i = 0; i < map->n; ++i) {
7579 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7582 isl_basic_map_free(map->p[i]);
7585 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7586 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7587 map = isl_map_remove_empty_parts(map);
7590 for (i = map->n - 1; i >= 1; --i) {
7591 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7593 isl_basic_map_free(map->p[i-1]);
7594 for (j = i; j < map->n; ++j)
7595 map->p[j-1] = map->p[j];
7605 struct isl_set *isl_set_normalize(struct isl_set *set)
7607 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7610 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7620 if (!isl_space_is_equal(map1->dim, map2->dim))
7623 map1 = isl_map_copy(map1);
7624 map2 = isl_map_copy(map2);
7625 map1 = isl_map_normalize(map1);
7626 map2 = isl_map_normalize(map2);
7629 equal = map1->n == map2->n;
7630 for (i = 0; equal && i < map1->n; ++i) {
7631 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7644 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7646 return isl_map_plain_is_equal(map1, map2);
7649 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7651 return isl_map_plain_is_equal((struct isl_map *)set1,
7652 (struct isl_map *)set2);
7655 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7657 return isl_set_plain_is_equal(set1, set2);
7660 /* Return an interval that ranges from min to max (inclusive)
7662 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7663 isl_int min, isl_int max)
7666 struct isl_basic_set *bset = NULL;
7668 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7672 k = isl_basic_set_alloc_inequality(bset);
7675 isl_int_set_si(bset->ineq[k][1], 1);
7676 isl_int_neg(bset->ineq[k][0], min);
7678 k = isl_basic_set_alloc_inequality(bset);
7681 isl_int_set_si(bset->ineq[k][1], -1);
7682 isl_int_set(bset->ineq[k][0], max);
7686 isl_basic_set_free(bset);
7690 /* Return the Cartesian product of the basic sets in list (in the given order).
7692 __isl_give isl_basic_set *isl_basic_set_list_product(
7693 __isl_take struct isl_basic_set_list *list)
7701 struct isl_basic_set *product = NULL;
7705 isl_assert(list->ctx, list->n > 0, goto error);
7706 isl_assert(list->ctx, list->p[0], goto error);
7707 nparam = isl_basic_set_n_param(list->p[0]);
7708 dim = isl_basic_set_n_dim(list->p[0]);
7709 extra = list->p[0]->n_div;
7710 n_eq = list->p[0]->n_eq;
7711 n_ineq = list->p[0]->n_ineq;
7712 for (i = 1; i < list->n; ++i) {
7713 isl_assert(list->ctx, list->p[i], goto error);
7714 isl_assert(list->ctx,
7715 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7716 dim += isl_basic_set_n_dim(list->p[i]);
7717 extra += list->p[i]->n_div;
7718 n_eq += list->p[i]->n_eq;
7719 n_ineq += list->p[i]->n_ineq;
7721 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7726 for (i = 0; i < list->n; ++i) {
7727 isl_basic_set_add_constraints(product,
7728 isl_basic_set_copy(list->p[i]), dim);
7729 dim += isl_basic_set_n_dim(list->p[i]);
7731 isl_basic_set_list_free(list);
7734 isl_basic_set_free(product);
7735 isl_basic_set_list_free(list);
7739 struct isl_basic_map *isl_basic_map_product(
7740 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7742 isl_space *dim_result = NULL;
7743 struct isl_basic_map *bmap;
7744 unsigned in1, in2, out1, out2, nparam, total, pos;
7745 struct isl_dim_map *dim_map1, *dim_map2;
7747 if (!bmap1 || !bmap2)
7750 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
7751 bmap2->dim, isl_dim_param), goto error);
7752 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
7753 isl_space_copy(bmap2->dim));
7755 in1 = isl_basic_map_n_in(bmap1);
7756 in2 = isl_basic_map_n_in(bmap2);
7757 out1 = isl_basic_map_n_out(bmap1);
7758 out2 = isl_basic_map_n_out(bmap2);
7759 nparam = isl_basic_map_n_param(bmap1);
7761 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7762 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7763 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7764 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7765 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7766 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7767 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7768 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7769 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7770 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7771 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7773 bmap = isl_basic_map_alloc_space(dim_result,
7774 bmap1->n_div + bmap2->n_div,
7775 bmap1->n_eq + bmap2->n_eq,
7776 bmap1->n_ineq + bmap2->n_ineq);
7777 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7778 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7779 bmap = isl_basic_map_simplify(bmap);
7780 return isl_basic_map_finalize(bmap);
7782 isl_basic_map_free(bmap1);
7783 isl_basic_map_free(bmap2);
7787 __isl_give isl_basic_map *isl_basic_map_flat_product(
7788 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7790 isl_basic_map *prod;
7792 prod = isl_basic_map_product(bmap1, bmap2);
7793 prod = isl_basic_map_flatten(prod);
7797 __isl_give isl_basic_set *isl_basic_set_flat_product(
7798 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7800 return isl_basic_map_flat_range_product(bset1, bset2);
7803 __isl_give isl_basic_map *isl_basic_map_domain_product(
7804 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7806 isl_space *space_result = NULL;
7807 isl_basic_map *bmap;
7808 unsigned in1, in2, out, nparam, total, pos;
7809 struct isl_dim_map *dim_map1, *dim_map2;
7811 if (!bmap1 || !bmap2)
7814 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
7815 isl_space_copy(bmap2->dim));
7817 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
7818 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
7819 out = isl_basic_map_dim(bmap1, isl_dim_out);
7820 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
7822 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
7823 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7824 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7825 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7826 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7827 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7828 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7829 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7830 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
7831 isl_dim_map_div(dim_map1, bmap1, pos += out);
7832 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7834 bmap = isl_basic_map_alloc_space(space_result,
7835 bmap1->n_div + bmap2->n_div,
7836 bmap1->n_eq + bmap2->n_eq,
7837 bmap1->n_ineq + bmap2->n_ineq);
7838 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7839 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7840 bmap = isl_basic_map_simplify(bmap);
7841 return isl_basic_map_finalize(bmap);
7843 isl_basic_map_free(bmap1);
7844 isl_basic_map_free(bmap2);
7848 __isl_give isl_basic_map *isl_basic_map_range_product(
7849 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7851 isl_space *dim_result = NULL;
7852 isl_basic_map *bmap;
7853 unsigned in, out1, out2, nparam, total, pos;
7854 struct isl_dim_map *dim_map1, *dim_map2;
7856 if (!bmap1 || !bmap2)
7859 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
7860 isl_space_copy(bmap2->dim));
7862 in = isl_basic_map_dim(bmap1, isl_dim_in);
7863 out1 = isl_basic_map_n_out(bmap1);
7864 out2 = isl_basic_map_n_out(bmap2);
7865 nparam = isl_basic_map_n_param(bmap1);
7867 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7868 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7869 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7870 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7871 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7872 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7873 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7874 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7875 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7876 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7877 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7879 bmap = isl_basic_map_alloc_space(dim_result,
7880 bmap1->n_div + bmap2->n_div,
7881 bmap1->n_eq + bmap2->n_eq,
7882 bmap1->n_ineq + bmap2->n_ineq);
7883 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7884 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7885 bmap = isl_basic_map_simplify(bmap);
7886 return isl_basic_map_finalize(bmap);
7888 isl_basic_map_free(bmap1);
7889 isl_basic_map_free(bmap2);
7893 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
7894 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7896 isl_basic_map *prod;
7898 prod = isl_basic_map_range_product(bmap1, bmap2);
7899 prod = isl_basic_map_flatten_range(prod);
7903 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7904 __isl_take isl_map *map2,
7905 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
7906 __isl_take isl_space *right),
7907 __isl_give isl_basic_map *(*basic_map_product)(
7908 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7911 struct isl_map *result;
7917 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
7918 map2->dim, isl_dim_param), goto error);
7920 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7921 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7922 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7924 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
7925 isl_space_copy(map2->dim)),
7926 map1->n * map2->n, flags);
7929 for (i = 0; i < map1->n; ++i)
7930 for (j = 0; j < map2->n; ++j) {
7931 struct isl_basic_map *part;
7932 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7933 isl_basic_map_copy(map2->p[j]));
7934 if (isl_basic_map_is_empty(part))
7935 isl_basic_map_free(part);
7937 result = isl_map_add_basic_map(result, part);
7950 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7952 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
7953 __isl_take isl_map *map2)
7955 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
7958 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
7959 __isl_take isl_map *map2)
7961 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
7964 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7966 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7967 __isl_take isl_map *map2)
7971 prod = isl_map_product(map1, map2);
7972 prod = isl_map_flatten(prod);
7976 /* Given two set A and B, construct its Cartesian product A x B.
7978 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7980 return isl_map_range_product(set1, set2);
7983 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7984 __isl_take isl_set *set2)
7986 return isl_map_flat_range_product(set1, set2);
7989 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
7991 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
7992 __isl_take isl_map *map2)
7994 return map_product(map1, map2, &isl_space_domain_product,
7995 &isl_basic_map_domain_product);
7998 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8000 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8001 __isl_take isl_map *map2)
8003 return map_product(map1, map2, &isl_space_range_product,
8004 &isl_basic_map_range_product);
8007 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8008 __isl_take isl_map *map2)
8010 return isl_map_align_params_map_map_and(map1, map2,
8011 &map_domain_product_aligned);
8014 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8015 __isl_take isl_map *map2)
8017 return isl_map_align_params_map_map_and(map1, map2,
8018 &map_range_product_aligned);
8021 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8023 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8024 __isl_take isl_map *map2)
8028 prod = isl_map_domain_product(map1, map2);
8029 prod = isl_map_flatten_domain(prod);
8033 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8035 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8036 __isl_take isl_map *map2)
8040 prod = isl_map_range_product(map1, map2);
8041 prod = isl_map_flatten_range(prod);
8045 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8048 uint32_t hash = isl_hash_init();
8053 bmap = isl_basic_map_copy(bmap);
8054 bmap = isl_basic_map_normalize(bmap);
8057 total = isl_basic_map_total_dim(bmap);
8058 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8059 for (i = 0; i < bmap->n_eq; ++i) {
8061 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8062 isl_hash_hash(hash, c_hash);
8064 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8065 for (i = 0; i < bmap->n_ineq; ++i) {
8067 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8068 isl_hash_hash(hash, c_hash);
8070 isl_hash_byte(hash, bmap->n_div & 0xFF);
8071 for (i = 0; i < bmap->n_div; ++i) {
8073 if (isl_int_is_zero(bmap->div[i][0]))
8075 isl_hash_byte(hash, i & 0xFF);
8076 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8077 isl_hash_hash(hash, c_hash);
8079 isl_basic_map_free(bmap);
8083 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8085 return isl_basic_map_get_hash((isl_basic_map *)bset);
8088 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8095 map = isl_map_copy(map);
8096 map = isl_map_normalize(map);
8100 hash = isl_hash_init();
8101 for (i = 0; i < map->n; ++i) {
8103 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8104 isl_hash_hash(hash, bmap_hash);
8112 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8114 return isl_map_get_hash((isl_map *)set);
8117 /* Check if the value for dimension dim is completely determined
8118 * by the values of the other parameters and variables.
8119 * That is, check if dimension dim is involved in an equality.
8121 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8128 nparam = isl_basic_set_n_param(bset);
8129 for (i = 0; i < bset->n_eq; ++i)
8130 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8135 /* Check if the value for dimension dim is completely determined
8136 * by the values of the other parameters and variables.
8137 * That is, check if dimension dim is involved in an equality
8138 * for each of the subsets.
8140 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8146 for (i = 0; i < set->n; ++i) {
8148 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8155 int isl_set_n_basic_set(__isl_keep isl_set *set)
8157 return set ? set->n : 0;
8160 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8161 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8168 for (i = 0; i < map->n; ++i)
8169 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8175 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8176 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8183 for (i = 0; i < set->n; ++i)
8184 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8190 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8197 bset = isl_basic_set_cow(bset);
8201 dim = isl_basic_set_get_space(bset);
8202 dim = isl_space_lift(dim, bset->n_div);
8205 isl_space_free(bset->dim);
8207 bset->extra -= bset->n_div;
8210 bset = isl_basic_set_finalize(bset);
8214 isl_basic_set_free(bset);
8218 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8224 set = isl_set_align_divs(set);
8229 set = isl_set_cow(set);
8233 n_div = set->p[0]->n_div;
8234 dim = isl_set_get_space(set);
8235 dim = isl_space_lift(dim, n_div);
8238 isl_space_free(set->dim);
8241 for (i = 0; i < set->n; ++i) {
8242 set->p[i] = isl_basic_set_lift(set->p[i]);
8253 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8256 struct isl_basic_map *bmap;
8263 set = isl_set_align_divs(set);
8268 dim = isl_set_get_space(set);
8269 if (set->n == 0 || set->p[0]->n_div == 0) {
8271 return isl_map_identity(isl_space_map_from_set(dim));
8274 n_div = set->p[0]->n_div;
8275 dim = isl_space_map_from_set(dim);
8276 n_param = isl_space_dim(dim, isl_dim_param);
8277 n_set = isl_space_dim(dim, isl_dim_in);
8278 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8279 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8280 for (i = 0; i < n_set; ++i)
8281 bmap = var_equal(bmap, i);
8283 total = n_param + n_set + n_set + n_div;
8284 for (i = 0; i < n_div; ++i) {
8285 k = isl_basic_map_alloc_inequality(bmap);
8288 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8289 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8290 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8291 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8292 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8293 set->p[0]->div[i][0]);
8295 l = isl_basic_map_alloc_inequality(bmap);
8298 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8299 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8300 set->p[0]->div[i][0]);
8301 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8305 bmap = isl_basic_map_simplify(bmap);
8306 bmap = isl_basic_map_finalize(bmap);
8307 return isl_map_from_basic_map(bmap);
8310 isl_basic_map_free(bmap);
8314 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8322 dim = isl_basic_set_total_dim(bset);
8323 size += bset->n_eq * (1 + dim);
8324 size += bset->n_ineq * (1 + dim);
8325 size += bset->n_div * (2 + dim);
8330 int isl_set_size(__isl_keep isl_set *set)
8338 for (i = 0; i < set->n; ++i)
8339 size += isl_basic_set_size(set->p[i]);
8344 /* Check if there is any lower bound (if lower == 0) and/or upper
8345 * bound (if upper == 0) on the specified dim.
8347 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8348 enum isl_dim_type type, unsigned pos, int lower, int upper)
8355 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8357 pos += isl_basic_map_offset(bmap, type);
8359 for (i = 0; i < bmap->n_div; ++i) {
8360 if (isl_int_is_zero(bmap->div[i][0]))
8362 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8366 for (i = 0; i < bmap->n_eq; ++i)
8367 if (!isl_int_is_zero(bmap->eq[i][pos]))
8370 for (i = 0; i < bmap->n_ineq; ++i) {
8371 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8378 return lower && upper;
8381 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8382 enum isl_dim_type type, unsigned pos)
8384 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8387 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8388 enum isl_dim_type type, unsigned pos)
8390 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8393 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8394 enum isl_dim_type type, unsigned pos)
8396 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8399 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8400 enum isl_dim_type type, unsigned pos)
8407 for (i = 0; i < map->n; ++i) {
8409 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8410 if (bounded < 0 || !bounded)
8417 /* Return 1 if the specified dim is involved in both an upper bound
8418 * and a lower bound.
8420 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8421 enum isl_dim_type type, unsigned pos)
8423 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8426 static int has_bound(__isl_keep isl_map *map,
8427 enum isl_dim_type type, unsigned pos,
8428 int (*fn)(__isl_keep isl_basic_map *bmap,
8429 enum isl_dim_type type, unsigned pos))
8436 for (i = 0; i < map->n; ++i) {
8438 bounded = fn(map->p[i], type, pos);
8439 if (bounded < 0 || bounded)
8446 /* Return 1 if the specified dim is involved in any lower bound.
8448 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8449 enum isl_dim_type type, unsigned pos)
8451 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8454 /* Return 1 if the specified dim is involved in any upper bound.
8456 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8457 enum isl_dim_type type, unsigned pos)
8459 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8462 /* For each of the "n" variables starting at "first", determine
8463 * the sign of the variable and put the results in the first "n"
8464 * elements of the array "signs".
8466 * 1 means that the variable is non-negative
8467 * -1 means that the variable is non-positive
8468 * 0 means the variable attains both positive and negative values.
8470 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8471 unsigned first, unsigned n, int *signs)
8473 isl_vec *bound = NULL;
8474 struct isl_tab *tab = NULL;
8475 struct isl_tab_undo *snap;
8478 if (!bset || !signs)
8481 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8482 tab = isl_tab_from_basic_set(bset);
8486 isl_seq_clr(bound->el, bound->size);
8487 isl_int_set_si(bound->el[0], -1);
8489 snap = isl_tab_snap(tab);
8490 for (i = 0; i < n; ++i) {
8493 isl_int_set_si(bound->el[1 + first + i], -1);
8494 if (isl_tab_add_ineq(tab, bound->el) < 0)
8497 isl_int_set_si(bound->el[1 + first + i], 0);
8498 if (isl_tab_rollback(tab, snap) < 0)
8506 isl_int_set_si(bound->el[1 + first + i], 1);
8507 if (isl_tab_add_ineq(tab, bound->el) < 0)
8510 isl_int_set_si(bound->el[1 + first + i], 0);
8511 if (isl_tab_rollback(tab, snap) < 0)
8514 signs[i] = empty ? -1 : 0;
8518 isl_vec_free(bound);
8522 isl_vec_free(bound);
8526 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8527 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8529 if (!bset || !signs)
8531 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8534 first += pos(bset->dim, type) - 1;
8535 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8538 /* Check if the given basic map is obviously single-valued.
8539 * In particular, for each output dimension, check that there is
8540 * an equality that defines the output dimension in terms of
8541 * earlier dimensions.
8543 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8553 total = 1 + isl_basic_map_total_dim(bmap);
8554 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8555 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8557 for (i = 0; i < n_out; ++i) {
8558 for (j = 0; j < bmap->n_eq; ++j) {
8559 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8561 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8562 total - (o_out + i + 1)) == -1)
8565 if (j >= bmap->n_eq)
8572 /* Check if the given map is obviously single-valued.
8574 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8583 return isl_basic_map_plain_is_single_valued(map->p[0]);
8586 /* Check if the given map is single-valued.
8591 * and check if the result is a subset of the identity mapping.
8593 int isl_map_is_single_valued(__isl_keep isl_map *map)
8600 sv = isl_map_plain_is_single_valued(map);
8604 test = isl_map_reverse(isl_map_copy(map));
8605 test = isl_map_apply_range(test, isl_map_copy(map));
8607 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8608 id = isl_map_identity(dim);
8610 sv = isl_map_is_subset(test, id);
8618 int isl_map_is_injective(__isl_keep isl_map *map)
8622 map = isl_map_copy(map);
8623 map = isl_map_reverse(map);
8624 in = isl_map_is_single_valued(map);
8630 /* Check if the given map is obviously injective.
8632 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8636 map = isl_map_copy(map);
8637 map = isl_map_reverse(map);
8638 in = isl_map_plain_is_single_valued(map);
8644 int isl_map_is_bijective(__isl_keep isl_map *map)
8648 sv = isl_map_is_single_valued(map);
8652 return isl_map_is_injective(map);
8655 int isl_set_is_singleton(__isl_keep isl_set *set)
8657 return isl_map_is_single_valued((isl_map *)set);
8660 int isl_map_is_translation(__isl_keep isl_map *map)
8665 delta = isl_map_deltas(isl_map_copy(map));
8666 ok = isl_set_is_singleton(delta);
8667 isl_set_free(delta);
8672 static int unique(isl_int *p, unsigned pos, unsigned len)
8674 if (isl_seq_first_non_zero(p, pos) != -1)
8676 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8681 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8690 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8693 nvar = isl_basic_set_dim(bset, isl_dim_set);
8694 ovar = isl_space_offset(bset->dim, isl_dim_set);
8695 for (j = 0; j < nvar; ++j) {
8696 int lower = 0, upper = 0;
8697 for (i = 0; i < bset->n_eq; ++i) {
8698 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8700 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8706 for (i = 0; i < bset->n_ineq; ++i) {
8707 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8709 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8711 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8716 if (!lower || !upper)
8723 int isl_set_is_box(__isl_keep isl_set *set)
8730 return isl_basic_set_is_box(set->p[0]);
8733 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8738 return isl_space_is_wrapping(bset->dim);
8741 int isl_set_is_wrapping(__isl_keep isl_set *set)
8746 return isl_space_is_wrapping(set->dim);
8749 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8751 bmap = isl_basic_map_cow(bmap);
8755 bmap->dim = isl_space_wrap(bmap->dim);
8759 bmap = isl_basic_map_finalize(bmap);
8761 return (isl_basic_set *)bmap;
8763 isl_basic_map_free(bmap);
8767 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8771 map = isl_map_cow(map);
8775 for (i = 0; i < map->n; ++i) {
8776 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8780 map->dim = isl_space_wrap(map->dim);
8784 return (isl_set *)map;
8790 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8792 bset = isl_basic_set_cow(bset);
8796 bset->dim = isl_space_unwrap(bset->dim);
8800 bset = isl_basic_set_finalize(bset);
8802 return (isl_basic_map *)bset;
8804 isl_basic_set_free(bset);
8808 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8815 if (!isl_set_is_wrapping(set))
8816 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8819 set = isl_set_cow(set);
8823 for (i = 0; i < set->n; ++i) {
8824 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8829 set->dim = isl_space_unwrap(set->dim);
8833 return (isl_map *)set;
8839 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8840 enum isl_dim_type type)
8845 if (!isl_space_is_named_or_nested(bmap->dim, type))
8848 bmap = isl_basic_map_cow(bmap);
8852 bmap->dim = isl_space_reset(bmap->dim, type);
8856 bmap = isl_basic_map_finalize(bmap);
8860 isl_basic_map_free(bmap);
8864 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8865 enum isl_dim_type type)
8872 if (!isl_space_is_named_or_nested(map->dim, type))
8875 map = isl_map_cow(map);
8879 for (i = 0; i < map->n; ++i) {
8880 map->p[i] = isl_basic_map_reset(map->p[i], type);
8884 map->dim = isl_space_reset(map->dim, type);
8894 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8899 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8902 bmap = isl_basic_map_cow(bmap);
8906 bmap->dim = isl_space_flatten(bmap->dim);
8910 bmap = isl_basic_map_finalize(bmap);
8914 isl_basic_map_free(bmap);
8918 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8920 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8923 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
8924 __isl_take isl_basic_map *bmap)
8929 if (!bmap->dim->nested[0])
8932 bmap = isl_basic_map_cow(bmap);
8936 bmap->dim = isl_space_flatten_domain(bmap->dim);
8940 bmap = isl_basic_map_finalize(bmap);
8944 isl_basic_map_free(bmap);
8948 __isl_give isl_basic_map *isl_basic_map_flatten_range(
8949 __isl_take isl_basic_map *bmap)
8954 if (!bmap->dim->nested[1])
8957 bmap = isl_basic_map_cow(bmap);
8961 bmap->dim = isl_space_flatten_range(bmap->dim);
8965 bmap = isl_basic_map_finalize(bmap);
8969 isl_basic_map_free(bmap);
8973 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8980 if (!map->dim->nested[0] && !map->dim->nested[1])
8983 map = isl_map_cow(map);
8987 for (i = 0; i < map->n; ++i) {
8988 map->p[i] = isl_basic_map_flatten(map->p[i]);
8992 map->dim = isl_space_flatten(map->dim);
9002 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9004 return (isl_set *)isl_map_flatten((isl_map *)set);
9007 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9009 isl_space *dim, *flat_dim;
9012 dim = isl_set_get_space(set);
9013 flat_dim = isl_space_flatten(isl_space_copy(dim));
9014 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9015 map = isl_map_intersect_domain(map, set);
9020 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9027 if (!map->dim->nested[0])
9030 map = isl_map_cow(map);
9034 for (i = 0; i < map->n; ++i) {
9035 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9039 map->dim = isl_space_flatten_domain(map->dim);
9049 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9056 if (!map->dim->nested[1])
9059 map = isl_map_cow(map);
9063 for (i = 0; i < map->n; ++i) {
9064 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9068 map->dim = isl_space_flatten_range(map->dim);
9078 /* Reorder the dimensions of "bmap" according to the given dim_map
9079 * and set the dimension specification to "dim".
9081 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9082 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9086 bmap = isl_basic_map_cow(bmap);
9087 if (!bmap || !dim || !dim_map)
9090 res = isl_basic_map_alloc_space(dim,
9091 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9092 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9093 res = isl_basic_map_finalize(res);
9097 isl_basic_map_free(bmap);
9098 isl_space_free(dim);
9102 /* Reorder the dimensions of "map" according to given reordering.
9104 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9105 __isl_take isl_reordering *r)
9108 struct isl_dim_map *dim_map;
9110 map = isl_map_cow(map);
9111 dim_map = isl_dim_map_from_reordering(r);
9112 if (!map || !r || !dim_map)
9115 for (i = 0; i < map->n; ++i) {
9116 struct isl_dim_map *dim_map_i;
9118 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9120 map->p[i] = isl_basic_map_realign(map->p[i],
9121 isl_space_copy(r->dim), dim_map_i);
9127 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9129 isl_reordering_free(r);
9135 isl_reordering_free(r);
9139 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9140 __isl_take isl_reordering *r)
9142 return (isl_set *)isl_map_realign((isl_map *)set, r);
9145 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9146 __isl_take isl_space *model)
9153 ctx = isl_space_get_ctx(model);
9154 if (!isl_space_has_named_params(model))
9155 isl_die(ctx, isl_error_invalid,
9156 "model has unnamed parameters", goto error);
9157 if (!isl_space_has_named_params(map->dim))
9158 isl_die(ctx, isl_error_invalid,
9159 "relation has unnamed parameters", goto error);
9160 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9161 isl_reordering *exp;
9163 model = isl_space_drop_dims(model, isl_dim_in,
9164 0, isl_space_dim(model, isl_dim_in));
9165 model = isl_space_drop_dims(model, isl_dim_out,
9166 0, isl_space_dim(model, isl_dim_out));
9167 exp = isl_parameter_alignment_reordering(map->dim, model);
9168 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9169 map = isl_map_realign(map, exp);
9172 isl_space_free(model);
9175 isl_space_free(model);
9180 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9181 __isl_take isl_space *model)
9183 return isl_map_align_params(set, model);
9186 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9187 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9188 enum isl_dim_type c2, enum isl_dim_type c3,
9189 enum isl_dim_type c4, enum isl_dim_type c5)
9191 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9192 struct isl_mat *mat;
9198 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9199 isl_basic_map_total_dim(bmap) + 1);
9202 for (i = 0; i < bmap->n_eq; ++i)
9203 for (j = 0, pos = 0; j < 5; ++j) {
9204 int off = isl_basic_map_offset(bmap, c[j]);
9205 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9206 isl_int_set(mat->row[i][pos],
9207 bmap->eq[i][off + k]);
9215 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9216 __isl_keep isl_basic_map *bmap, 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 struct isl_mat *mat;
9227 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9228 isl_basic_map_total_dim(bmap) + 1);
9231 for (i = 0; i < bmap->n_ineq; ++i)
9232 for (j = 0, pos = 0; j < 5; ++j) {
9233 int off = isl_basic_map_offset(bmap, c[j]);
9234 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9235 isl_int_set(mat->row[i][pos],
9236 bmap->ineq[i][off + k]);
9244 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9245 __isl_take isl_space *dim,
9246 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9247 enum isl_dim_type c2, enum isl_dim_type c3,
9248 enum isl_dim_type c4, enum isl_dim_type c5)
9250 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9251 isl_basic_map *bmap;
9257 if (!dim || !eq || !ineq)
9260 if (eq->n_col != ineq->n_col)
9261 isl_die(dim->ctx, isl_error_invalid,
9262 "equalities and inequalities matrices should have "
9263 "same number of columns", goto error);
9265 total = 1 + isl_space_dim(dim, isl_dim_all);
9267 if (eq->n_col < total)
9268 isl_die(dim->ctx, isl_error_invalid,
9269 "number of columns too small", goto error);
9271 extra = eq->n_col - total;
9273 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9274 eq->n_row, ineq->n_row);
9277 for (i = 0; i < extra; ++i) {
9278 k = isl_basic_map_alloc_div(bmap);
9281 isl_int_set_si(bmap->div[k][0], 0);
9283 for (i = 0; i < eq->n_row; ++i) {
9284 l = isl_basic_map_alloc_equality(bmap);
9287 for (j = 0, pos = 0; j < 5; ++j) {
9288 int off = isl_basic_map_offset(bmap, c[j]);
9289 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9290 isl_int_set(bmap->eq[l][off + k],
9296 for (i = 0; i < ineq->n_row; ++i) {
9297 l = isl_basic_map_alloc_inequality(bmap);
9300 for (j = 0, pos = 0; j < 5; ++j) {
9301 int off = isl_basic_map_offset(bmap, c[j]);
9302 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9303 isl_int_set(bmap->ineq[l][off + k],
9310 isl_space_free(dim);
9316 isl_space_free(dim);
9322 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9323 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9324 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9326 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9327 c1, c2, c3, c4, isl_dim_in);
9330 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9331 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9332 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9334 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9335 c1, c2, c3, c4, isl_dim_in);
9338 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9339 __isl_take isl_space *dim,
9340 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9341 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9343 return (isl_basic_set*)
9344 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9345 c1, c2, c3, c4, isl_dim_in);
9348 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9353 return isl_space_can_zip(bmap->dim);
9356 int isl_map_can_zip(__isl_keep isl_map *map)
9361 return isl_space_can_zip(map->dim);
9364 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9365 * (A -> C) -> (B -> D).
9367 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9376 if (!isl_basic_map_can_zip(bmap))
9377 isl_die(bmap->ctx, isl_error_invalid,
9378 "basic map cannot be zipped", goto error);
9379 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9380 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9381 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9382 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9383 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9386 bmap->dim = isl_space_zip(bmap->dim);
9391 isl_basic_map_free(bmap);
9395 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9396 * (A -> C) -> (B -> D).
9398 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9405 if (!isl_map_can_zip(map))
9406 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9409 map = isl_map_cow(map);
9413 for (i = 0; i < map->n; ++i) {
9414 map->p[i] = isl_basic_map_zip(map->p[i]);
9419 map->dim = isl_space_zip(map->dim);
9429 /* Construct a basic map mapping the domain of the affine expression
9430 * to a one-dimensional range prescribed by the affine expression.
9432 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9436 isl_local_space *ls;
9437 isl_basic_map *bmap;
9442 ls = isl_aff_get_local_space(aff);
9443 ls = isl_local_space_from_domain(ls);
9444 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
9445 bmap = isl_basic_map_from_local_space(ls);
9446 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9447 k = isl_basic_map_alloc_equality(bmap);
9451 pos = isl_basic_map_offset(bmap, isl_dim_out);
9452 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9453 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9454 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9455 aff->v->size - (pos + 1));
9458 bmap = isl_basic_map_finalize(bmap);
9462 isl_basic_map_free(bmap);
9466 /* Construct a basic map mapping a domain in the given space to
9467 * to an n-dimensional range, with n the number of elements in the list,
9468 * where each coordinate in the range is prescribed by the
9469 * corresponding affine expression.
9470 * The domains of all affine expressions in the list are assumed to match
9473 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9474 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9478 isl_basic_map *bmap;
9483 dim = isl_space_from_domain(domain_dim);
9484 bmap = isl_basic_map_universe(dim);
9486 for (i = 0; i < list->n; ++i) {
9488 isl_basic_map *bmap_i;
9490 aff = isl_aff_copy(list->p[i]);
9491 bmap_i = isl_basic_map_from_aff(aff);
9493 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9496 isl_aff_list_free(list);
9500 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9501 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9503 return isl_map_equate(set, type1, pos1, type2, pos2);
9506 /* Add a constraint imposing that the given two dimensions are equal.
9508 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9509 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9511 isl_basic_map *bmap = NULL;
9517 if (pos1 >= isl_map_dim(map, type1))
9518 isl_die(map->ctx, isl_error_invalid,
9519 "index out of bounds", goto error);
9520 if (pos2 >= isl_map_dim(map, type2))
9521 isl_die(map->ctx, isl_error_invalid,
9522 "index out of bounds", goto error);
9524 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9525 i = isl_basic_map_alloc_equality(bmap);
9528 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9529 pos1 += isl_basic_map_offset(bmap, type1);
9530 pos2 += isl_basic_map_offset(bmap, type2);
9531 isl_int_set_si(bmap->eq[i][pos1], -1);
9532 isl_int_set_si(bmap->eq[i][pos2], 1);
9533 bmap = isl_basic_map_finalize(bmap);
9535 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9539 isl_basic_map_free(bmap);
9544 /* Add a constraint imposing that the given two dimensions have opposite values.
9546 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9547 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9549 isl_basic_map *bmap = NULL;
9555 if (pos1 >= isl_map_dim(map, type1))
9556 isl_die(map->ctx, isl_error_invalid,
9557 "index out of bounds", goto error);
9558 if (pos2 >= isl_map_dim(map, type2))
9559 isl_die(map->ctx, isl_error_invalid,
9560 "index out of bounds", goto error);
9562 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9563 i = isl_basic_map_alloc_equality(bmap);
9566 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9567 pos1 += isl_basic_map_offset(bmap, type1);
9568 pos2 += isl_basic_map_offset(bmap, type2);
9569 isl_int_set_si(bmap->eq[i][pos1], 1);
9570 isl_int_set_si(bmap->eq[i][pos2], 1);
9571 bmap = isl_basic_map_finalize(bmap);
9573 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9577 isl_basic_map_free(bmap);