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;
670 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
671 return (struct isl_basic_set *)bmap;
674 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
675 unsigned extra, unsigned n_eq, unsigned n_ineq)
677 struct isl_basic_map *bmap;
680 isl_assert(dim->ctx, dim->n_in == 0, goto error);
681 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
682 return (struct isl_basic_set *)bmap;
688 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
689 unsigned extra, unsigned n_eq, unsigned n_ineq)
691 struct isl_basic_map *bmap;
695 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
700 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
706 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
707 unsigned nparam, unsigned in, unsigned out, unsigned extra,
708 unsigned n_eq, unsigned n_ineq)
710 struct isl_basic_map *bmap;
713 dim = isl_space_alloc(ctx, nparam, in, out);
717 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
721 static void dup_constraints(
722 struct isl_basic_map *dst, struct isl_basic_map *src)
725 unsigned total = isl_basic_map_total_dim(src);
727 for (i = 0; i < src->n_eq; ++i) {
728 int j = isl_basic_map_alloc_equality(dst);
729 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
732 for (i = 0; i < src->n_ineq; ++i) {
733 int j = isl_basic_map_alloc_inequality(dst);
734 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
737 for (i = 0; i < src->n_div; ++i) {
738 int j = isl_basic_map_alloc_div(dst);
739 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
741 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
744 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
746 struct isl_basic_map *dup;
750 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
751 bmap->n_div, bmap->n_eq, bmap->n_ineq);
754 dup_constraints(dup, bmap);
755 dup->flags = bmap->flags;
756 dup->sample = isl_vec_copy(bmap->sample);
760 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
762 struct isl_basic_map *dup;
764 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
765 return (struct isl_basic_set *)dup;
768 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
773 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
777 return isl_basic_set_dup(bset);
780 struct isl_set *isl_set_copy(struct isl_set *set)
789 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
794 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
798 bmap = isl_basic_map_dup(bmap);
800 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
804 struct isl_map *isl_map_copy(struct isl_map *map)
813 void isl_basic_map_free(struct isl_basic_map *bmap)
821 isl_ctx_deref(bmap->ctx);
823 isl_blk_free(bmap->ctx, bmap->block2);
825 isl_blk_free(bmap->ctx, bmap->block);
826 isl_vec_free(bmap->sample);
827 isl_space_free(bmap->dim);
831 void isl_basic_set_free(struct isl_basic_set *bset)
833 isl_basic_map_free((struct isl_basic_map *)bset);
836 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
838 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
841 __isl_give isl_map *isl_map_align_params_map_map_and(
842 __isl_take isl_map *map1, __isl_take isl_map *map2,
843 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
844 __isl_take isl_map *map2))
848 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
849 return fn(map1, map2);
850 if (!isl_space_has_named_params(map1->dim) ||
851 !isl_space_has_named_params(map2->dim))
852 isl_die(map1->ctx, isl_error_invalid,
853 "unaligned unnamed parameters", goto error);
854 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
855 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
856 return fn(map1, map2);
863 static int align_params_map_map_and_test(__isl_keep isl_map *map1,
864 __isl_keep isl_map *map2,
865 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
871 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
872 return fn(map1, map2);
873 if (!isl_space_has_named_params(map1->dim) ||
874 !isl_space_has_named_params(map2->dim))
875 isl_die(map1->ctx, isl_error_invalid,
876 "unaligned unnamed parameters", return -1);
877 map1 = isl_map_copy(map1);
878 map2 = isl_map_copy(map2);
879 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
880 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
887 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
893 isl_assert(ctx, room_for_con(bmap, 1), return -1);
894 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
896 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
897 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
898 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
899 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
900 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
901 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
903 int j = isl_basic_map_alloc_inequality(bmap);
907 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
908 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
915 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
916 bmap->extra - bmap->n_div);
920 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
922 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
925 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
929 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
934 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
936 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
939 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
944 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
946 if (pos != bmap->n_eq - 1) {
948 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
949 bmap->eq[bmap->n_eq - 1] = t;
955 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
957 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
960 void isl_basic_map_inequality_to_equality(
961 struct isl_basic_map *bmap, unsigned pos)
966 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
967 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
972 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
973 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
974 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
975 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
978 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
980 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
983 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
989 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
990 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
991 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
992 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
993 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
994 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
995 1 + isl_basic_map_total_dim(bmap),
996 bmap->extra - bmap->n_div);
997 return bmap->n_ineq++;
1000 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1002 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1005 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1009 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1014 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1016 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1019 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1024 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1026 if (pos != bmap->n_ineq - 1) {
1027 t = bmap->ineq[pos];
1028 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1029 bmap->ineq[bmap->n_ineq - 1] = t;
1030 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1036 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1038 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1041 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1046 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1049 k = isl_basic_map_alloc_equality(bmap);
1052 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1055 isl_basic_map_free(bmap);
1059 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1062 return (isl_basic_set *)
1063 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1066 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1071 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1074 k = isl_basic_map_alloc_inequality(bmap);
1077 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1080 isl_basic_map_free(bmap);
1084 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1087 return (isl_basic_set *)
1088 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1091 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1095 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1096 isl_seq_clr(bmap->div[bmap->n_div] +
1097 1 + 1 + isl_basic_map_total_dim(bmap),
1098 bmap->extra - bmap->n_div);
1099 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1100 return bmap->n_div++;
1103 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1105 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1108 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1112 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1117 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1119 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1122 /* Copy constraint from src to dst, putting the vars of src at offset
1123 * dim_off in dst and the divs of src at offset div_off in dst.
1124 * If both sets are actually map, then dim_off applies to the input
1127 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1128 struct isl_basic_map *src_map, isl_int *src,
1129 unsigned in_off, unsigned out_off, unsigned div_off)
1131 unsigned src_nparam = isl_basic_map_n_param(src_map);
1132 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1133 unsigned src_in = isl_basic_map_n_in(src_map);
1134 unsigned dst_in = isl_basic_map_n_in(dst_map);
1135 unsigned src_out = isl_basic_map_n_out(src_map);
1136 unsigned dst_out = isl_basic_map_n_out(dst_map);
1137 isl_int_set(dst[0], src[0]);
1138 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1139 if (dst_nparam > src_nparam)
1140 isl_seq_clr(dst+1+src_nparam,
1141 dst_nparam - src_nparam);
1142 isl_seq_clr(dst+1+dst_nparam, in_off);
1143 isl_seq_cpy(dst+1+dst_nparam+in_off,
1145 isl_min(dst_in-in_off, src_in));
1146 if (dst_in-in_off > src_in)
1147 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1148 dst_in - in_off - src_in);
1149 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1150 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1151 src+1+src_nparam+src_in,
1152 isl_min(dst_out-out_off, src_out));
1153 if (dst_out-out_off > src_out)
1154 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1155 dst_out - out_off - src_out);
1156 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1157 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1158 src+1+src_nparam+src_in+src_out,
1159 isl_min(dst_map->extra-div_off, src_map->n_div));
1160 if (dst_map->n_div-div_off > src_map->n_div)
1161 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1162 div_off+src_map->n_div,
1163 dst_map->n_div - div_off - src_map->n_div);
1166 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1167 struct isl_basic_map *src_map, isl_int *src,
1168 unsigned in_off, unsigned out_off, unsigned div_off)
1170 isl_int_set(dst[0], src[0]);
1171 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1174 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1175 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1180 if (!bmap1 || !bmap2)
1183 div_off = bmap1->n_div;
1185 for (i = 0; i < bmap2->n_eq; ++i) {
1186 int i1 = isl_basic_map_alloc_equality(bmap1);
1189 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1190 i_pos, o_pos, div_off);
1193 for (i = 0; i < bmap2->n_ineq; ++i) {
1194 int i1 = isl_basic_map_alloc_inequality(bmap1);
1197 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1198 i_pos, o_pos, div_off);
1201 for (i = 0; i < bmap2->n_div; ++i) {
1202 int i1 = isl_basic_map_alloc_div(bmap1);
1205 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1206 i_pos, o_pos, div_off);
1209 isl_basic_map_free(bmap2);
1214 isl_basic_map_free(bmap1);
1215 isl_basic_map_free(bmap2);
1219 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1220 struct isl_basic_set *bset2, unsigned pos)
1222 return (struct isl_basic_set *)
1223 add_constraints((struct isl_basic_map *)bset1,
1224 (struct isl_basic_map *)bset2, 0, pos);
1227 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1228 __isl_take isl_space *dim, unsigned extra,
1229 unsigned n_eq, unsigned n_ineq)
1231 struct isl_basic_map *ext;
1241 dims_ok = isl_space_is_equal(base->dim, dim) &&
1242 base->extra >= base->n_div + extra;
1244 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1245 room_for_ineq(base, n_ineq)) {
1246 isl_space_free(dim);
1250 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1251 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1252 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1253 extra += base->extra;
1255 n_ineq += base->n_ineq;
1257 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1263 ext->sample = isl_vec_copy(base->sample);
1264 flags = base->flags;
1265 ext = add_constraints(ext, base, 0, 0);
1268 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1274 isl_space_free(dim);
1275 isl_basic_map_free(base);
1279 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1280 __isl_take isl_space *dim, unsigned extra,
1281 unsigned n_eq, unsigned n_ineq)
1283 return (struct isl_basic_set *)
1284 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1285 extra, n_eq, n_ineq);
1288 struct isl_basic_map *isl_basic_map_extend_constraints(
1289 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1293 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1297 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1298 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1299 unsigned n_eq, unsigned n_ineq)
1301 struct isl_basic_map *bmap;
1306 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1310 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1313 isl_basic_map_free(base);
1317 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1318 unsigned nparam, unsigned dim, unsigned extra,
1319 unsigned n_eq, unsigned n_ineq)
1321 return (struct isl_basic_set *)
1322 isl_basic_map_extend((struct isl_basic_map *)base,
1323 nparam, 0, dim, extra, n_eq, n_ineq);
1326 struct isl_basic_set *isl_basic_set_extend_constraints(
1327 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1329 return (struct isl_basic_set *)
1330 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1334 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1336 return (struct isl_basic_set *)
1337 isl_basic_map_cow((struct isl_basic_map *)bset);
1340 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1345 if (bmap->ref > 1) {
1347 bmap = isl_basic_map_dup(bmap);
1350 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1354 struct isl_set *isl_set_cow(struct isl_set *set)
1362 return isl_set_dup(set);
1365 struct isl_map *isl_map_cow(struct isl_map *map)
1373 return isl_map_dup(map);
1376 static void swap_vars(struct isl_blk blk, isl_int *a,
1377 unsigned a_len, unsigned b_len)
1379 isl_seq_cpy(blk.data, a+a_len, b_len);
1380 isl_seq_cpy(blk.data+b_len, a, a_len);
1381 isl_seq_cpy(a, blk.data, b_len+a_len);
1384 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1385 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1393 isl_assert(bmap->ctx,
1394 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1396 if (n1 == 0 || n2 == 0)
1399 bmap = isl_basic_map_cow(bmap);
1403 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1404 if (isl_blk_is_error(blk))
1407 for (i = 0; i < bmap->n_eq; ++i)
1409 bmap->eq[i] + pos, n1, n2);
1411 for (i = 0; i < bmap->n_ineq; ++i)
1413 bmap->ineq[i] + pos, n1, n2);
1415 for (i = 0; i < bmap->n_div; ++i)
1417 bmap->div[i]+1 + pos, n1, n2);
1419 isl_blk_free(bmap->ctx, blk);
1421 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1422 bmap = isl_basic_map_gauss(bmap, NULL);
1423 return isl_basic_map_finalize(bmap);
1425 isl_basic_map_free(bmap);
1429 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1430 __isl_take isl_basic_set *bset, unsigned n)
1435 nparam = isl_basic_set_n_param(bset);
1436 dim = isl_basic_set_n_dim(bset);
1437 isl_assert(bset->ctx, n <= dim, goto error);
1439 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1441 isl_basic_set_free(bset);
1445 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1451 total = isl_basic_map_total_dim(bmap);
1452 isl_basic_map_free_div(bmap, bmap->n_div);
1453 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1455 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1457 i = isl_basic_map_alloc_equality(bmap);
1461 isl_int_set_si(bmap->eq[i][0], 1);
1462 isl_seq_clr(bmap->eq[i]+1, total);
1463 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1464 isl_vec_free(bmap->sample);
1465 bmap->sample = NULL;
1466 return isl_basic_map_finalize(bmap);
1468 isl_basic_map_free(bmap);
1472 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1474 return (struct isl_basic_set *)
1475 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1478 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1481 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1482 isl_int *t = bmap->div[a];
1483 bmap->div[a] = bmap->div[b];
1486 for (i = 0; i < bmap->n_eq; ++i)
1487 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1489 for (i = 0; i < bmap->n_ineq; ++i)
1490 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1492 for (i = 0; i < bmap->n_div; ++i)
1493 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1494 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1497 /* Eliminate the specified n dimensions starting at first from the
1498 * constraints using Fourier-Motzkin. The dimensions themselves
1501 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1502 enum isl_dim_type type, unsigned first, unsigned n)
1511 map = isl_map_cow(map);
1514 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1515 first += pos(map->dim, type) - 1;
1517 for (i = 0; i < map->n; ++i) {
1518 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1528 /* Eliminate the specified n dimensions starting at first from the
1529 * constraints using Fourier-Motzkin. The dimensions themselves
1532 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1533 enum isl_dim_type type, unsigned first, unsigned n)
1535 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1538 /* Eliminate the specified n dimensions starting at first from the
1539 * constraints using Fourier-Motzkin. The dimensions themselves
1542 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1543 unsigned first, unsigned n)
1545 return isl_set_eliminate(set, isl_dim_set, first, n);
1548 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1549 __isl_take isl_basic_map *bmap)
1553 bmap = isl_basic_map_eliminate_vars(bmap,
1554 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1558 return isl_basic_map_finalize(bmap);
1561 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1562 __isl_take isl_basic_set *bset)
1564 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1565 (struct isl_basic_map *)bset);
1568 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1577 map = isl_map_cow(map);
1581 for (i = 0; i < map->n; ++i) {
1582 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1592 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1594 return isl_map_remove_divs(set);
1597 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1598 enum isl_dim_type type, unsigned first, unsigned n)
1602 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1604 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1606 bmap = isl_basic_map_eliminate_vars(bmap,
1607 isl_basic_map_offset(bmap, type) - 1 + first, n);
1610 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1612 bmap = isl_basic_map_drop(bmap, type, first, n);
1615 isl_basic_map_free(bmap);
1619 /* Return true if the definition of the given div (recursively) involves
1620 * any of the given variables.
1622 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1623 unsigned first, unsigned n)
1626 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1628 if (isl_int_is_zero(bmap->div[div][0]))
1630 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1633 for (i = bmap->n_div - 1; i >= 0; --i) {
1634 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1636 if (div_involves_vars(bmap, i, first, n))
1643 /* Remove all divs (recursively) involving any of the given dimensions
1644 * in their definitions.
1646 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1647 __isl_take isl_basic_map *bmap,
1648 enum isl_dim_type type, unsigned first, unsigned n)
1654 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1656 first += isl_basic_map_offset(bmap, type);
1658 for (i = bmap->n_div - 1; i >= 0; --i) {
1659 if (!div_involves_vars(bmap, i, first, n))
1661 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1669 isl_basic_map_free(bmap);
1673 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1674 enum isl_dim_type type, unsigned first, unsigned n)
1683 map = isl_map_cow(map);
1687 for (i = 0; i < map->n; ++i) {
1688 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1699 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1700 enum isl_dim_type type, unsigned first, unsigned n)
1702 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1706 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1707 enum isl_dim_type type, unsigned first, unsigned n)
1714 if (first + n > isl_basic_map_dim(bmap, type))
1715 isl_die(bmap->ctx, isl_error_invalid,
1716 "index out of bounds", return -1);
1718 first += isl_basic_map_offset(bmap, type);
1719 for (i = 0; i < bmap->n_eq; ++i)
1720 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1722 for (i = 0; i < bmap->n_ineq; ++i)
1723 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1729 int isl_map_involves_dims(__isl_keep isl_map *map,
1730 enum isl_dim_type type, unsigned first, unsigned n)
1737 if (first + n > isl_map_dim(map, type))
1738 isl_die(map->ctx, isl_error_invalid,
1739 "index out of bounds", return -1);
1741 for (i = 0; i < map->n; ++i) {
1742 int involves = isl_basic_map_involves_dims(map->p[i],
1744 if (involves < 0 || !involves)
1751 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1752 enum isl_dim_type type, unsigned first, unsigned n)
1754 return isl_basic_map_involves_dims(bset, type, first, n);
1757 int isl_set_involves_dims(__isl_keep isl_set *set,
1758 enum isl_dim_type type, unsigned first, unsigned n)
1760 return isl_map_involves_dims(set, type, first, n);
1763 /* Return true if the definition of the given div is unknown or depends
1766 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1769 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1771 if (isl_int_is_zero(bmap->div[div][0]))
1774 for (i = bmap->n_div - 1; i >= 0; --i) {
1775 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1777 if (div_is_unknown(bmap, i))
1784 /* Remove all divs that are unknown or defined in terms of unknown divs.
1786 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1787 __isl_take isl_basic_map *bmap)
1794 for (i = bmap->n_div - 1; i >= 0; --i) {
1795 if (!div_is_unknown(bmap, i))
1797 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1803 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1812 map = isl_map_cow(map);
1816 for (i = 0; i < map->n; ++i) {
1817 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1827 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1829 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1832 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1833 __isl_take isl_basic_set *bset,
1834 enum isl_dim_type type, unsigned first, unsigned n)
1836 return (isl_basic_set *)
1837 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1840 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1841 enum isl_dim_type type, unsigned first, unsigned n)
1848 map = isl_map_cow(map);
1851 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1853 for (i = 0; i < map->n; ++i) {
1854 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1855 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1859 map = isl_map_drop(map, type, first, n);
1866 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1867 enum isl_dim_type type, unsigned first, unsigned n)
1869 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1872 /* Project out n inputs starting at first using Fourier-Motzkin */
1873 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1874 unsigned first, unsigned n)
1876 return isl_map_remove_dims(map, isl_dim_in, first, n);
1879 static void dump_term(struct isl_basic_map *bmap,
1880 isl_int c, int pos, FILE *out)
1883 unsigned in = isl_basic_map_n_in(bmap);
1884 unsigned dim = in + isl_basic_map_n_out(bmap);
1885 unsigned nparam = isl_basic_map_n_param(bmap);
1887 isl_int_print(out, c, 0);
1889 if (!isl_int_is_one(c))
1890 isl_int_print(out, c, 0);
1891 if (pos < 1 + nparam) {
1892 name = isl_space_get_dim_name(bmap->dim,
1893 isl_dim_param, pos - 1);
1895 fprintf(out, "%s", name);
1897 fprintf(out, "p%d", pos - 1);
1898 } else if (pos < 1 + nparam + in)
1899 fprintf(out, "i%d", pos - 1 - nparam);
1900 else if (pos < 1 + nparam + dim)
1901 fprintf(out, "o%d", pos - 1 - nparam - in);
1903 fprintf(out, "e%d", pos - 1 - nparam - dim);
1907 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1908 int sign, FILE *out)
1912 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1916 for (i = 0, first = 1; i < len; ++i) {
1917 if (isl_int_sgn(c[i]) * sign <= 0)
1920 fprintf(out, " + ");
1922 isl_int_abs(v, c[i]);
1923 dump_term(bmap, v, i, out);
1930 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1931 const char *op, FILE *out, int indent)
1935 fprintf(out, "%*s", indent, "");
1937 dump_constraint_sign(bmap, c, 1, out);
1938 fprintf(out, " %s ", op);
1939 dump_constraint_sign(bmap, c, -1, out);
1943 for (i = bmap->n_div; i < bmap->extra; ++i) {
1944 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
1946 fprintf(out, "%*s", indent, "");
1947 fprintf(out, "ERROR: unused div coefficient not zero\n");
1952 static void dump_constraints(struct isl_basic_map *bmap,
1953 isl_int **c, unsigned n,
1954 const char *op, FILE *out, int indent)
1958 for (i = 0; i < n; ++i)
1959 dump_constraint(bmap, c[i], op, out, indent);
1962 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1966 unsigned total = isl_basic_map_total_dim(bmap);
1968 for (j = 0; j < 1 + total; ++j) {
1969 if (isl_int_is_zero(exp[j]))
1971 if (!first && isl_int_is_pos(exp[j]))
1973 dump_term(bmap, exp[j], j, out);
1978 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1982 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1983 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1985 for (i = 0; i < bmap->n_div; ++i) {
1986 fprintf(out, "%*s", indent, "");
1987 fprintf(out, "e%d = [(", i);
1988 dump_affine(bmap, bmap->div[i]+1, out);
1990 isl_int_print(out, bmap->div[i][0], 0);
1991 fprintf(out, "]\n");
1995 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1996 FILE *out, int indent)
1999 fprintf(out, "null basic set\n");
2003 fprintf(out, "%*s", indent, "");
2004 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2005 bset->ref, bset->dim->nparam, bset->dim->n_out,
2006 bset->extra, bset->flags);
2007 dump((struct isl_basic_map *)bset, out, indent);
2010 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2011 FILE *out, int indent)
2014 fprintf(out, "null basic map\n");
2018 fprintf(out, "%*s", indent, "");
2019 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2020 "flags: %x, n_name: %d\n",
2022 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2023 bmap->extra, bmap->flags, bmap->dim->n_id);
2024 dump(bmap, out, indent);
2027 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2032 total = isl_basic_map_total_dim(bmap);
2033 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2034 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2035 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2036 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2040 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2043 struct isl_set *set;
2047 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2048 isl_assert(dim->ctx, n >= 0, goto error);
2049 set = isl_alloc(dim->ctx, struct isl_set,
2050 sizeof(struct isl_set) +
2051 (n - 1) * sizeof(struct isl_basic_set *));
2055 set->ctx = dim->ctx;
2056 isl_ctx_ref(set->ctx);
2064 isl_space_free(dim);
2068 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2069 unsigned nparam, unsigned dim, int n, unsigned flags)
2071 struct isl_set *set;
2074 dims = isl_space_alloc(ctx, nparam, 0, dim);
2078 set = isl_set_alloc_space(dims, n, flags);
2082 /* Make sure "map" has room for at least "n" more basic maps.
2084 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2087 struct isl_map *grown = NULL;
2091 isl_assert(map->ctx, n >= 0, goto error);
2092 if (map->n + n <= map->size)
2094 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2097 for (i = 0; i < map->n; ++i) {
2098 grown->p[i] = isl_basic_map_copy(map->p[i]);
2106 isl_map_free(grown);
2111 /* Make sure "set" has room for at least "n" more basic sets.
2113 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2115 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2118 struct isl_set *isl_set_dup(struct isl_set *set)
2121 struct isl_set *dup;
2126 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2129 for (i = 0; i < set->n; ++i)
2130 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2134 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2136 return isl_map_from_basic_map(bset);
2139 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2141 struct isl_map *map;
2146 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2147 return isl_map_add_basic_map(map, bmap);
2150 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2151 __isl_take isl_basic_set *bset)
2153 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2154 (struct isl_basic_map *)bset);
2157 void isl_set_free(struct isl_set *set)
2167 isl_ctx_deref(set->ctx);
2168 for (i = 0; i < set->n; ++i)
2169 isl_basic_set_free(set->p[i]);
2170 isl_space_free(set->dim);
2174 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2179 fprintf(out, "null set\n");
2183 fprintf(out, "%*s", indent, "");
2184 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2185 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2187 for (i = 0; i < set->n; ++i) {
2188 fprintf(out, "%*s", indent, "");
2189 fprintf(out, "basic set %d:\n", i);
2190 isl_basic_set_print_internal(set->p[i], out, indent+4);
2194 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2199 fprintf(out, "null map\n");
2203 fprintf(out, "%*s", indent, "");
2204 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2205 "flags: %x, n_name: %d\n",
2206 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2207 map->dim->n_out, map->flags, map->dim->n_id);
2208 for (i = 0; i < map->n; ++i) {
2209 fprintf(out, "%*s", indent, "");
2210 fprintf(out, "basic map %d:\n", i);
2211 isl_basic_map_print_internal(map->p[i], out, indent+4);
2215 struct isl_basic_map *isl_basic_map_intersect_domain(
2216 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2218 struct isl_basic_map *bmap_domain;
2223 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2224 bset->dim, isl_dim_param), goto error);
2226 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2227 isl_assert(bset->ctx,
2228 isl_basic_map_compatible_domain(bmap, bset), goto error);
2230 bmap = isl_basic_map_cow(bmap);
2233 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2234 bset->n_div, bset->n_eq, bset->n_ineq);
2235 bmap_domain = isl_basic_map_from_domain(bset);
2236 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2238 bmap = isl_basic_map_simplify(bmap);
2239 return isl_basic_map_finalize(bmap);
2241 isl_basic_map_free(bmap);
2242 isl_basic_set_free(bset);
2246 struct isl_basic_map *isl_basic_map_intersect_range(
2247 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2249 struct isl_basic_map *bmap_range;
2254 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2255 bset->dim, isl_dim_param), goto error);
2257 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2258 isl_assert(bset->ctx,
2259 isl_basic_map_compatible_range(bmap, bset), goto error);
2261 if (isl_basic_set_is_universe(bset)) {
2262 isl_basic_set_free(bset);
2266 bmap = isl_basic_map_cow(bmap);
2269 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2270 bset->n_div, bset->n_eq, bset->n_ineq);
2271 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2272 bmap = add_constraints(bmap, bmap_range, 0, 0);
2274 bmap = isl_basic_map_simplify(bmap);
2275 return isl_basic_map_finalize(bmap);
2277 isl_basic_map_free(bmap);
2278 isl_basic_set_free(bset);
2282 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2288 total = 1 + isl_basic_map_total_dim(bmap);
2289 if (total != vec->size)
2294 for (i = 0; i < bmap->n_eq; ++i) {
2295 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2296 if (!isl_int_is_zero(s)) {
2302 for (i = 0; i < bmap->n_ineq; ++i) {
2303 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2304 if (isl_int_is_neg(s)) {
2315 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2317 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2320 struct isl_basic_map *isl_basic_map_intersect(
2321 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2323 struct isl_vec *sample = NULL;
2325 if (!bmap1 || !bmap2)
2328 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2329 bmap2->dim, isl_dim_param), goto error);
2330 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2331 isl_space_dim(bmap1->dim, isl_dim_param) &&
2332 isl_space_dim(bmap2->dim, isl_dim_all) !=
2333 isl_space_dim(bmap2->dim, isl_dim_param))
2334 return isl_basic_map_intersect(bmap2, bmap1);
2336 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2337 isl_space_dim(bmap2->dim, isl_dim_param))
2338 isl_assert(bmap1->ctx,
2339 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2341 if (bmap1->sample &&
2342 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2343 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2344 sample = isl_vec_copy(bmap1->sample);
2345 else if (bmap2->sample &&
2346 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2347 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2348 sample = isl_vec_copy(bmap2->sample);
2350 bmap1 = isl_basic_map_cow(bmap1);
2353 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2354 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2355 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2358 isl_vec_free(sample);
2360 isl_vec_free(bmap1->sample);
2361 bmap1->sample = sample;
2364 bmap1 = isl_basic_map_simplify(bmap1);
2365 return isl_basic_map_finalize(bmap1);
2368 isl_vec_free(sample);
2369 isl_basic_map_free(bmap1);
2370 isl_basic_map_free(bmap2);
2374 struct isl_basic_set *isl_basic_set_intersect(
2375 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2377 return (struct isl_basic_set *)
2378 isl_basic_map_intersect(
2379 (struct isl_basic_map *)bset1,
2380 (struct isl_basic_map *)bset2);
2383 /* Special case of isl_map_intersect, where both map1 and map2
2384 * are convex, without any divs and such that either map1 or map2
2385 * contains a single constraint. This constraint is then simply
2386 * added to the other map.
2388 static __isl_give isl_map *map_intersect_add_constraint(
2389 __isl_take isl_map *map1, __isl_take isl_map *map2)
2391 isl_assert(map1->ctx, map1->n == 1, goto error);
2392 isl_assert(map2->ctx, map1->n == 1, goto error);
2393 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2394 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2396 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2397 return isl_map_intersect(map2, map1);
2399 isl_assert(map2->ctx,
2400 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2402 map1 = isl_map_cow(map1);
2405 if (isl_map_plain_is_empty(map1)) {
2409 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2410 if (map2->p[0]->n_eq == 1)
2411 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2413 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2414 map2->p[0]->ineq[0]);
2416 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2417 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2421 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2422 isl_basic_map_free(map1->p[0]);
2435 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2436 __isl_take isl_map *map2)
2439 struct isl_map *result;
2445 if (isl_map_plain_is_empty(map1) &&
2446 isl_space_is_equal(map1->dim, map2->dim)) {
2450 if (isl_map_plain_is_empty(map2) &&
2451 isl_space_is_equal(map1->dim, map2->dim)) {
2456 if (map1->n == 1 && map2->n == 1 &&
2457 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2458 isl_space_is_equal(map1->dim, map2->dim) &&
2459 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2460 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2461 return map_intersect_add_constraint(map1, map2);
2462 if (isl_space_dim(map1->dim, isl_dim_all) ==
2463 isl_space_dim(map1->dim, isl_dim_param) &&
2464 isl_space_dim(map2->dim, isl_dim_all) !=
2465 isl_space_dim(map2->dim, isl_dim_param))
2466 return isl_map_intersect(map2, map1);
2468 if (isl_space_dim(map2->dim, isl_dim_all) !=
2469 isl_space_dim(map2->dim, isl_dim_param))
2470 isl_assert(map1->ctx,
2471 isl_space_is_equal(map1->dim, map2->dim), goto error);
2473 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2474 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2475 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2477 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2478 map1->n * map2->n, flags);
2481 for (i = 0; i < map1->n; ++i)
2482 for (j = 0; j < map2->n; ++j) {
2483 struct isl_basic_map *part;
2484 part = isl_basic_map_intersect(
2485 isl_basic_map_copy(map1->p[i]),
2486 isl_basic_map_copy(map2->p[j]));
2487 if (isl_basic_map_is_empty(part))
2488 isl_basic_map_free(part);
2490 result = isl_map_add_basic_map(result, part);
2503 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2504 __isl_take isl_map *map2)
2506 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2509 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2511 return (struct isl_set *)
2512 isl_map_intersect((struct isl_map *)set1,
2513 (struct isl_map *)set2);
2516 /* The current implementation of isl_map_intersect accepts intersections
2517 * with parameter domains, so we can just call that for now.
2519 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2520 __isl_take isl_set *params)
2522 return isl_map_intersect(map, params);
2525 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2526 __isl_take isl_map *map2)
2528 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2531 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2532 __isl_take isl_set *params)
2534 return isl_map_intersect_params(set, params);
2537 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2540 struct isl_basic_set *bset;
2545 bmap = isl_basic_map_cow(bmap);
2548 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2549 in = isl_basic_map_n_in(bmap);
2550 bset = isl_basic_set_from_basic_map(bmap);
2551 bset = isl_basic_set_swap_vars(bset, in);
2552 return isl_basic_map_from_basic_set(bset, dim);
2555 static __isl_give isl_basic_map *basic_map_space_reset(
2556 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2560 if (!isl_space_is_named_or_nested(bmap->dim, type))
2563 space = isl_basic_map_get_space(bmap);
2564 space = isl_space_reset(space, type);
2565 bmap = isl_basic_map_reset_space(bmap, space);
2569 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2570 enum isl_dim_type type, unsigned pos, unsigned n)
2573 struct isl_basic_map *res;
2574 struct isl_dim_map *dim_map;
2575 unsigned total, off;
2576 enum isl_dim_type t;
2579 return basic_map_space_reset(bmap, type);
2584 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2586 total = isl_basic_map_total_dim(bmap) + n;
2587 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2589 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2591 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2593 unsigned size = isl_basic_map_dim(bmap, t);
2594 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2596 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2597 pos, size - pos, off + pos + n);
2599 off += isl_space_dim(res_dim, t);
2601 isl_dim_map_div(dim_map, bmap, off);
2603 res = isl_basic_map_alloc_space(res_dim,
2604 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2605 if (isl_basic_map_is_rational(bmap))
2606 res = isl_basic_map_set_rational(res);
2607 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2608 return isl_basic_map_finalize(res);
2611 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2612 enum isl_dim_type type, unsigned n)
2616 return isl_basic_map_insert(bmap, type,
2617 isl_basic_map_dim(bmap, type), n);
2620 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2621 enum isl_dim_type type, unsigned n)
2625 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2626 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2628 isl_basic_set_free(bset);
2632 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2633 enum isl_dim_type type)
2637 if (!isl_space_is_named_or_nested(map->dim, type))
2640 space = isl_map_get_space(map);
2641 space = isl_space_reset(space, type);
2642 map = isl_map_reset_space(map, space);
2646 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2647 enum isl_dim_type type, unsigned pos, unsigned n)
2652 return map_space_reset(map, type);
2654 map = isl_map_cow(map);
2658 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2662 for (i = 0; i < map->n; ++i) {
2663 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2674 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2675 enum isl_dim_type type, unsigned pos, unsigned n)
2677 return isl_map_insert_dims(set, type, pos, n);
2680 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2681 enum isl_dim_type type, unsigned n)
2685 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2688 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2689 enum isl_dim_type type, unsigned n)
2693 isl_assert(set->ctx, type != isl_dim_in, goto error);
2694 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2700 __isl_give isl_basic_map *isl_basic_map_move_dims(
2701 __isl_take isl_basic_map *bmap,
2702 enum isl_dim_type dst_type, unsigned dst_pos,
2703 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2705 struct isl_dim_map *dim_map;
2706 struct isl_basic_map *res;
2707 enum isl_dim_type t;
2708 unsigned total, off;
2715 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2718 if (dst_type == src_type && dst_pos == src_pos)
2721 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2723 if (pos(bmap->dim, dst_type) + dst_pos ==
2724 pos(bmap->dim, src_type) + src_pos +
2725 ((src_type < dst_type) ? n : 0)) {
2726 bmap = isl_basic_map_cow(bmap);
2730 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2731 src_type, src_pos, n);
2735 bmap = isl_basic_map_finalize(bmap);
2740 total = isl_basic_map_total_dim(bmap);
2741 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2744 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2745 unsigned size = isl_space_dim(bmap->dim, t);
2746 if (t == dst_type) {
2747 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2750 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2753 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2754 dst_pos, size - dst_pos, off);
2755 off += size - dst_pos;
2756 } else if (t == src_type) {
2757 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2760 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2761 src_pos + n, size - src_pos - n, off);
2762 off += size - src_pos - n;
2764 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2768 isl_dim_map_div(dim_map, bmap, off);
2770 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2771 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2772 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2774 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2775 src_type, src_pos, n);
2779 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2780 bmap = isl_basic_map_gauss(bmap, NULL);
2781 bmap = isl_basic_map_finalize(bmap);
2785 isl_basic_map_free(bmap);
2789 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2790 enum isl_dim_type dst_type, unsigned dst_pos,
2791 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2793 return (isl_basic_set *)isl_basic_map_move_dims(
2794 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2797 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2798 enum isl_dim_type dst_type, unsigned dst_pos,
2799 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2803 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2804 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2805 src_type, src_pos, n);
2811 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2812 enum isl_dim_type dst_type, unsigned dst_pos,
2813 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2822 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2825 if (dst_type == src_type && dst_pos == src_pos)
2828 isl_assert(map->ctx, dst_type != src_type, goto error);
2830 map = isl_map_cow(map);
2834 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2838 for (i = 0; i < map->n; ++i) {
2839 map->p[i] = isl_basic_map_move_dims(map->p[i],
2841 src_type, src_pos, n);
2852 /* Move the specified dimensions to the last columns right before
2853 * the divs. Don't change the dimension specification of bmap.
2854 * That's the responsibility of the caller.
2856 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2857 enum isl_dim_type type, unsigned first, unsigned n)
2859 struct isl_dim_map *dim_map;
2860 struct isl_basic_map *res;
2861 enum isl_dim_type t;
2862 unsigned total, off;
2866 if (pos(bmap->dim, type) + first + n ==
2867 1 + isl_space_dim(bmap->dim, isl_dim_all))
2870 total = isl_basic_map_total_dim(bmap);
2871 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2874 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2875 unsigned size = isl_space_dim(bmap->dim, t);
2877 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2880 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2881 first, n, total - bmap->n_div - n);
2882 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2883 first + n, size - (first + n), off);
2884 off += size - (first + n);
2886 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2890 isl_dim_map_div(dim_map, bmap, off + n);
2892 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2893 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2894 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2898 /* Turn the n dimensions of type type, starting at first
2899 * into existentially quantified variables.
2901 __isl_give isl_basic_map *isl_basic_map_project_out(
2902 __isl_take isl_basic_map *bmap,
2903 enum isl_dim_type type, unsigned first, unsigned n)
2916 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2917 return isl_basic_map_remove_dims(bmap, type, first, n);
2919 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2922 bmap = move_last(bmap, type, first, n);
2923 bmap = isl_basic_map_cow(bmap);
2927 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
2928 old = bmap->block2.data;
2929 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2930 (bmap->extra + n) * (1 + row_size));
2931 if (!bmap->block2.data)
2933 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2936 for (i = 0; i < n; ++i) {
2937 new_div[i] = bmap->block2.data +
2938 (bmap->extra + i) * (1 + row_size);
2939 isl_seq_clr(new_div[i], 1 + row_size);
2941 for (i = 0; i < bmap->extra; ++i)
2942 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2944 bmap->div = new_div;
2948 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2951 bmap = isl_basic_map_simplify(bmap);
2952 bmap = isl_basic_map_drop_redundant_divs(bmap);
2953 return isl_basic_map_finalize(bmap);
2955 isl_basic_map_free(bmap);
2959 /* Turn the n dimensions of type type, starting at first
2960 * into existentially quantified variables.
2962 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2963 enum isl_dim_type type, unsigned first, unsigned n)
2965 return (isl_basic_set *)isl_basic_map_project_out(
2966 (isl_basic_map *)bset, type, first, n);
2969 /* Turn the n dimensions of type type, starting at first
2970 * into existentially quantified variables.
2972 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2973 enum isl_dim_type type, unsigned first, unsigned n)
2983 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2985 map = isl_map_cow(map);
2989 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2993 for (i = 0; i < map->n; ++i) {
2994 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3005 /* Turn the n dimensions of type type, starting at first
3006 * into existentially quantified variables.
3008 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3009 enum isl_dim_type type, unsigned first, unsigned n)
3011 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3014 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3018 for (i = 0; i < n; ++i) {
3019 j = isl_basic_map_alloc_div(bmap);
3022 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3026 isl_basic_map_free(bmap);
3030 struct isl_basic_map *isl_basic_map_apply_range(
3031 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3033 isl_space *dim_result = NULL;
3034 struct isl_basic_map *bmap;
3035 unsigned n_in, n_out, n, nparam, total, pos;
3036 struct isl_dim_map *dim_map1, *dim_map2;
3038 if (!bmap1 || !bmap2)
3041 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3042 isl_space_copy(bmap2->dim));
3044 n_in = isl_basic_map_n_in(bmap1);
3045 n_out = isl_basic_map_n_out(bmap2);
3046 n = isl_basic_map_n_out(bmap1);
3047 nparam = isl_basic_map_n_param(bmap1);
3049 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3050 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3051 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3052 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3053 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3054 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3055 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3056 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3057 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3058 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3059 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3061 bmap = isl_basic_map_alloc_space(dim_result,
3062 bmap1->n_div + bmap2->n_div + n,
3063 bmap1->n_eq + bmap2->n_eq,
3064 bmap1->n_ineq + bmap2->n_ineq);
3065 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3066 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3067 bmap = add_divs(bmap, n);
3068 bmap = isl_basic_map_simplify(bmap);
3069 bmap = isl_basic_map_drop_redundant_divs(bmap);
3070 return isl_basic_map_finalize(bmap);
3072 isl_basic_map_free(bmap1);
3073 isl_basic_map_free(bmap2);
3077 struct isl_basic_set *isl_basic_set_apply(
3078 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3083 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3086 return (struct isl_basic_set *)
3087 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3089 isl_basic_set_free(bset);
3090 isl_basic_map_free(bmap);
3094 struct isl_basic_map *isl_basic_map_apply_domain(
3095 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3097 if (!bmap1 || !bmap2)
3100 isl_assert(bmap1->ctx,
3101 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3102 isl_assert(bmap1->ctx,
3103 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3106 bmap1 = isl_basic_map_reverse(bmap1);
3107 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3108 return isl_basic_map_reverse(bmap1);
3110 isl_basic_map_free(bmap1);
3111 isl_basic_map_free(bmap2);
3115 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3116 * A \cap B -> f(A) + f(B)
3118 struct isl_basic_map *isl_basic_map_sum(
3119 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3121 unsigned n_in, n_out, nparam, total, pos;
3122 struct isl_basic_map *bmap = NULL;
3123 struct isl_dim_map *dim_map1, *dim_map2;
3126 if (!bmap1 || !bmap2)
3129 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3132 nparam = isl_basic_map_n_param(bmap1);
3133 n_in = isl_basic_map_n_in(bmap1);
3134 n_out = isl_basic_map_n_out(bmap1);
3136 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3137 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3138 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3139 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3140 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3141 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3142 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3143 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3144 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3145 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3146 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3148 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3149 bmap1->n_div + bmap2->n_div + 2 * n_out,
3150 bmap1->n_eq + bmap2->n_eq + n_out,
3151 bmap1->n_ineq + bmap2->n_ineq);
3152 for (i = 0; i < n_out; ++i) {
3153 int j = isl_basic_map_alloc_equality(bmap);
3156 isl_seq_clr(bmap->eq[j], 1+total);
3157 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3158 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3159 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3161 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3162 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3163 bmap = add_divs(bmap, 2 * n_out);
3165 bmap = isl_basic_map_simplify(bmap);
3166 return isl_basic_map_finalize(bmap);
3168 isl_basic_map_free(bmap);
3169 isl_basic_map_free(bmap1);
3170 isl_basic_map_free(bmap2);
3174 /* Given two maps A -> f(A) and B -> g(B), construct a map
3175 * A \cap B -> f(A) + f(B)
3177 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3179 struct isl_map *result;
3185 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3187 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3188 map1->n * map2->n, 0);
3191 for (i = 0; i < map1->n; ++i)
3192 for (j = 0; j < map2->n; ++j) {
3193 struct isl_basic_map *part;
3194 part = isl_basic_map_sum(
3195 isl_basic_map_copy(map1->p[i]),
3196 isl_basic_map_copy(map2->p[j]));
3197 if (isl_basic_map_is_empty(part))
3198 isl_basic_map_free(part);
3200 result = isl_map_add_basic_map(result, part);
3213 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3214 __isl_take isl_set *set2)
3216 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3219 /* Given a basic map A -> f(A), construct A -> -f(A).
3221 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3226 bmap = isl_basic_map_cow(bmap);
3230 n = isl_basic_map_dim(bmap, isl_dim_out);
3231 off = isl_basic_map_offset(bmap, isl_dim_out);
3232 for (i = 0; i < bmap->n_eq; ++i)
3233 for (j = 0; j < n; ++j)
3234 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3235 for (i = 0; i < bmap->n_ineq; ++i)
3236 for (j = 0; j < n; ++j)
3237 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3238 for (i = 0; i < bmap->n_div; ++i)
3239 for (j = 0; j < n; ++j)
3240 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3241 bmap = isl_basic_map_gauss(bmap, NULL);
3242 return isl_basic_map_finalize(bmap);
3245 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3247 return isl_basic_map_neg(bset);
3250 /* Given a map A -> f(A), construct A -> -f(A).
3252 struct isl_map *isl_map_neg(struct isl_map *map)
3256 map = isl_map_cow(map);
3260 for (i = 0; i < map->n; ++i) {
3261 map->p[i] = isl_basic_map_neg(map->p[i]);
3272 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3274 return (isl_set *)isl_map_neg((isl_map *)set);
3277 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3278 * A -> floor(f(A)/d).
3280 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3283 unsigned n_in, n_out, nparam, total, pos;
3284 struct isl_basic_map *result = NULL;
3285 struct isl_dim_map *dim_map;
3291 nparam = isl_basic_map_n_param(bmap);
3292 n_in = isl_basic_map_n_in(bmap);
3293 n_out = isl_basic_map_n_out(bmap);
3295 total = nparam + n_in + n_out + bmap->n_div + n_out;
3296 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3297 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3298 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3299 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3300 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3302 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3303 bmap->n_div + n_out,
3304 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3305 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3306 result = add_divs(result, n_out);
3307 for (i = 0; i < n_out; ++i) {
3309 j = isl_basic_map_alloc_inequality(result);
3312 isl_seq_clr(result->ineq[j], 1+total);
3313 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3314 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3315 j = isl_basic_map_alloc_inequality(result);
3318 isl_seq_clr(result->ineq[j], 1+total);
3319 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3320 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3321 isl_int_sub_ui(result->ineq[j][0], d, 1);
3324 result = isl_basic_map_simplify(result);
3325 return isl_basic_map_finalize(result);
3327 isl_basic_map_free(result);
3331 /* Given a map A -> f(A) and an integer d, construct a map
3332 * A -> floor(f(A)/d).
3334 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3338 map = isl_map_cow(map);
3342 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3343 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3344 for (i = 0; i < map->n; ++i) {
3345 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3356 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3362 i = isl_basic_map_alloc_equality(bmap);
3365 nparam = isl_basic_map_n_param(bmap);
3366 n_in = isl_basic_map_n_in(bmap);
3367 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3368 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3369 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3370 return isl_basic_map_finalize(bmap);
3372 isl_basic_map_free(bmap);
3376 /* Add a constraints to "bmap" expressing i_pos < o_pos
3378 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3384 i = isl_basic_map_alloc_inequality(bmap);
3387 nparam = isl_basic_map_n_param(bmap);
3388 n_in = isl_basic_map_n_in(bmap);
3389 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3390 isl_int_set_si(bmap->ineq[i][0], -1);
3391 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3392 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3393 return isl_basic_map_finalize(bmap);
3395 isl_basic_map_free(bmap);
3399 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3401 static __isl_give isl_basic_map *var_less_or_equal(
3402 __isl_take isl_basic_map *bmap, unsigned pos)
3408 i = isl_basic_map_alloc_inequality(bmap);
3411 nparam = isl_basic_map_n_param(bmap);
3412 n_in = isl_basic_map_n_in(bmap);
3413 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3414 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3415 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3416 return isl_basic_map_finalize(bmap);
3418 isl_basic_map_free(bmap);
3422 /* Add a constraints to "bmap" expressing i_pos > o_pos
3424 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3430 i = isl_basic_map_alloc_inequality(bmap);
3433 nparam = isl_basic_map_n_param(bmap);
3434 n_in = isl_basic_map_n_in(bmap);
3435 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3436 isl_int_set_si(bmap->ineq[i][0], -1);
3437 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3438 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3439 return isl_basic_map_finalize(bmap);
3441 isl_basic_map_free(bmap);
3445 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3447 static __isl_give isl_basic_map *var_more_or_equal(
3448 __isl_take isl_basic_map *bmap, unsigned pos)
3454 i = isl_basic_map_alloc_inequality(bmap);
3457 nparam = isl_basic_map_n_param(bmap);
3458 n_in = isl_basic_map_n_in(bmap);
3459 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3460 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3461 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3462 return isl_basic_map_finalize(bmap);
3464 isl_basic_map_free(bmap);
3468 __isl_give isl_basic_map *isl_basic_map_equal(
3469 __isl_take isl_space *dim, unsigned n_equal)
3472 struct isl_basic_map *bmap;
3473 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3476 for (i = 0; i < n_equal && bmap; ++i)
3477 bmap = var_equal(bmap, i);
3478 return isl_basic_map_finalize(bmap);
3481 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3483 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3487 struct isl_basic_map *bmap;
3488 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3491 for (i = 0; i < pos && bmap; ++i)
3492 bmap = var_equal(bmap, i);
3494 bmap = var_less(bmap, pos);
3495 return isl_basic_map_finalize(bmap);
3498 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3500 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3501 __isl_take isl_space *dim, unsigned pos)
3504 isl_basic_map *bmap;
3506 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3507 for (i = 0; i < pos; ++i)
3508 bmap = var_equal(bmap, i);
3509 bmap = var_less_or_equal(bmap, pos);
3510 return isl_basic_map_finalize(bmap);
3513 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3515 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3519 struct isl_basic_map *bmap;
3520 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3523 for (i = 0; i < pos && bmap; ++i)
3524 bmap = var_equal(bmap, i);
3526 bmap = var_more(bmap, pos);
3527 return isl_basic_map_finalize(bmap);
3530 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3532 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3533 __isl_take isl_space *dim, unsigned pos)
3536 isl_basic_map *bmap;
3538 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3539 for (i = 0; i < pos; ++i)
3540 bmap = var_equal(bmap, i);
3541 bmap = var_more_or_equal(bmap, pos);
3542 return isl_basic_map_finalize(bmap);
3545 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3546 unsigned n, int equal)
3548 struct isl_map *map;
3551 if (n == 0 && equal)
3552 return isl_map_universe(dims);
3554 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3556 for (i = 0; i + 1 < n; ++i)
3557 map = isl_map_add_basic_map(map,
3558 isl_basic_map_less_at(isl_space_copy(dims), i));
3561 map = isl_map_add_basic_map(map,
3562 isl_basic_map_less_or_equal_at(dims, n - 1));
3564 map = isl_map_add_basic_map(map,
3565 isl_basic_map_less_at(dims, n - 1));
3567 isl_space_free(dims);
3572 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3576 return map_lex_lte_first(dims, dims->n_out, equal);
3579 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3581 return map_lex_lte_first(dim, n, 0);
3584 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3586 return map_lex_lte_first(dim, n, 1);
3589 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3591 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3594 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3596 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3599 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3600 unsigned n, int equal)
3602 struct isl_map *map;
3605 if (n == 0 && equal)
3606 return isl_map_universe(dims);
3608 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3610 for (i = 0; i + 1 < n; ++i)
3611 map = isl_map_add_basic_map(map,
3612 isl_basic_map_more_at(isl_space_copy(dims), i));
3615 map = isl_map_add_basic_map(map,
3616 isl_basic_map_more_or_equal_at(dims, n - 1));
3618 map = isl_map_add_basic_map(map,
3619 isl_basic_map_more_at(dims, n - 1));
3621 isl_space_free(dims);
3626 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3630 return map_lex_gte_first(dims, dims->n_out, equal);
3633 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3635 return map_lex_gte_first(dim, n, 0);
3638 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3640 return map_lex_gte_first(dim, n, 1);
3643 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3645 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3648 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3650 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3653 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3654 __isl_take isl_set *set2)
3657 map = isl_map_lex_le(isl_set_get_space(set1));
3658 map = isl_map_intersect_domain(map, set1);
3659 map = isl_map_intersect_range(map, set2);
3663 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3664 __isl_take isl_set *set2)
3667 map = isl_map_lex_lt(isl_set_get_space(set1));
3668 map = isl_map_intersect_domain(map, set1);
3669 map = isl_map_intersect_range(map, set2);
3673 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3674 __isl_take isl_set *set2)
3677 map = isl_map_lex_ge(isl_set_get_space(set1));
3678 map = isl_map_intersect_domain(map, set1);
3679 map = isl_map_intersect_range(map, set2);
3683 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3684 __isl_take isl_set *set2)
3687 map = isl_map_lex_gt(isl_set_get_space(set1));
3688 map = isl_map_intersect_domain(map, set1);
3689 map = isl_map_intersect_range(map, set2);
3693 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3694 __isl_take isl_map *map2)
3697 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3698 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3699 map = isl_map_apply_range(map, isl_map_reverse(map2));
3703 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3704 __isl_take isl_map *map2)
3707 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3708 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3709 map = isl_map_apply_range(map, isl_map_reverse(map2));
3713 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3714 __isl_take isl_map *map2)
3717 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3718 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3719 map = isl_map_apply_range(map, isl_map_reverse(map2));
3723 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3724 __isl_take isl_map *map2)
3727 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3728 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3729 map = isl_map_apply_range(map, isl_map_reverse(map2));
3733 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3734 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3736 struct isl_basic_map *bmap;
3738 bset = isl_basic_set_cow(bset);
3742 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3743 isl_space_free(bset->dim);
3744 bmap = (struct isl_basic_map *) bset;
3746 return isl_basic_map_finalize(bmap);
3748 isl_basic_set_free(bset);
3749 isl_space_free(dim);
3753 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3757 if (bmap->dim->n_in == 0)
3758 return (struct isl_basic_set *)bmap;
3759 bmap = isl_basic_map_cow(bmap);
3762 bmap->dim = isl_space_as_set_space(bmap->dim);
3765 bmap = isl_basic_map_finalize(bmap);
3766 return (struct isl_basic_set *)bmap;
3768 isl_basic_map_free(bmap);
3772 /* For a div d = floor(f/m), add the constraints
3775 * -(f-(n-1)) + m d >= 0
3777 * Note that the second constraint is the negation of
3781 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3782 unsigned pos, isl_int *div)
3785 unsigned total = isl_basic_map_total_dim(bmap);
3787 i = isl_basic_map_alloc_inequality(bmap);
3790 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3791 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3793 j = isl_basic_map_alloc_inequality(bmap);
3796 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3797 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3798 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3802 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3803 unsigned pos, isl_int *div)
3805 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3809 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3811 unsigned total = isl_basic_map_total_dim(bmap);
3812 unsigned div_pos = total - bmap->n_div + div;
3814 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3818 struct isl_basic_set *isl_basic_map_underlying_set(
3819 struct isl_basic_map *bmap)
3823 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3825 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3826 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3827 return (struct isl_basic_set *)bmap;
3828 bmap = isl_basic_map_cow(bmap);
3831 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3834 bmap->extra -= bmap->n_div;
3836 bmap = isl_basic_map_finalize(bmap);
3837 return (struct isl_basic_set *)bmap;
3842 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3843 __isl_take isl_basic_set *bset)
3845 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3848 struct isl_basic_map *isl_basic_map_overlying_set(
3849 struct isl_basic_set *bset, struct isl_basic_map *like)
3851 struct isl_basic_map *bmap;
3852 struct isl_ctx *ctx;
3859 isl_assert(ctx, bset->n_div == 0, goto error);
3860 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3861 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3863 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3864 isl_basic_map_free(like);
3865 return (struct isl_basic_map *)bset;
3867 bset = isl_basic_set_cow(bset);
3870 total = bset->dim->n_out + bset->extra;
3871 bmap = (struct isl_basic_map *)bset;
3872 isl_space_free(bmap->dim);
3873 bmap->dim = isl_space_copy(like->dim);
3876 bmap->n_div = like->n_div;
3877 bmap->extra += like->n_div;
3881 ltotal = total - bmap->extra + like->extra;
3884 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3885 bmap->extra * (1 + 1 + total));
3886 if (isl_blk_is_error(bmap->block2))
3888 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3892 for (i = 0; i < bmap->extra; ++i)
3893 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3894 for (i = 0; i < like->n_div; ++i) {
3895 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3896 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3898 bmap = isl_basic_map_extend_constraints(bmap,
3899 0, 2 * like->n_div);
3900 for (i = 0; i < like->n_div; ++i) {
3901 if (isl_int_is_zero(bmap->div[i][0]))
3903 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3907 isl_basic_map_free(like);
3908 bmap = isl_basic_map_simplify(bmap);
3909 bmap = isl_basic_map_finalize(bmap);
3912 isl_basic_map_free(like);
3913 isl_basic_set_free(bset);
3917 struct isl_basic_set *isl_basic_set_from_underlying_set(
3918 struct isl_basic_set *bset, struct isl_basic_set *like)
3920 return (struct isl_basic_set *)
3921 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3924 struct isl_set *isl_set_from_underlying_set(
3925 struct isl_set *set, struct isl_basic_set *like)
3931 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3933 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
3934 isl_basic_set_free(like);
3937 set = isl_set_cow(set);
3940 for (i = 0; i < set->n; ++i) {
3941 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3942 isl_basic_set_copy(like));
3946 isl_space_free(set->dim);
3947 set->dim = isl_space_copy(like->dim);
3950 isl_basic_set_free(like);
3953 isl_basic_set_free(like);
3958 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3962 map = isl_map_cow(map);
3965 map->dim = isl_space_cow(map->dim);
3969 for (i = 1; i < map->n; ++i)
3970 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3972 for (i = 0; i < map->n; ++i) {
3973 map->p[i] = (struct isl_basic_map *)
3974 isl_basic_map_underlying_set(map->p[i]);
3979 map->dim = isl_space_underlying(map->dim, 0);
3981 isl_space_free(map->dim);
3982 map->dim = isl_space_copy(map->p[0]->dim);
3986 return (struct isl_set *)map;
3992 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3994 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3997 __isl_give isl_basic_map *isl_basic_map_reset_space(
3998 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4000 bmap = isl_basic_map_cow(bmap);
4004 isl_space_free(bmap->dim);
4007 bmap = isl_basic_map_finalize(bmap);
4011 isl_basic_map_free(bmap);
4012 isl_space_free(dim);
4016 __isl_give isl_basic_set *isl_basic_set_reset_space(
4017 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4019 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4023 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4024 __isl_take isl_space *dim)
4028 map = isl_map_cow(map);
4032 for (i = 0; i < map->n; ++i) {
4033 map->p[i] = isl_basic_map_reset_space(map->p[i],
4034 isl_space_copy(dim));
4038 isl_space_free(map->dim);
4044 isl_space_free(dim);
4048 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4049 __isl_take isl_space *dim)
4051 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4054 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4057 struct isl_basic_set *domain;
4063 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4065 n_in = isl_basic_map_n_in(bmap);
4066 n_out = isl_basic_map_n_out(bmap);
4067 domain = isl_basic_set_from_basic_map(bmap);
4068 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4070 domain = isl_basic_set_reset_space(domain, dim);
4075 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4079 return isl_space_may_be_set(bmap->dim);
4082 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4086 if (isl_basic_map_may_be_set(bmap))
4088 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4091 __isl_give isl_basic_map *isl_basic_map_domain_map(
4092 __isl_take isl_basic_map *bmap)
4096 isl_basic_map *domain;
4097 int nparam, n_in, n_out;
4100 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4101 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4102 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4104 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4105 domain = isl_basic_map_universe(dim);
4107 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4108 bmap = isl_basic_map_apply_range(bmap, domain);
4109 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4111 total = isl_basic_map_total_dim(bmap);
4113 for (i = 0; i < n_in; ++i) {
4114 k = isl_basic_map_alloc_equality(bmap);
4117 isl_seq_clr(bmap->eq[k], 1 + total);
4118 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4119 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4122 bmap = isl_basic_map_gauss(bmap, NULL);
4123 return isl_basic_map_finalize(bmap);
4125 isl_basic_map_free(bmap);
4129 __isl_give isl_basic_map *isl_basic_map_range_map(
4130 __isl_take isl_basic_map *bmap)
4134 isl_basic_map *range;
4135 int nparam, n_in, n_out;
4138 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4139 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4140 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4142 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4143 range = isl_basic_map_universe(dim);
4145 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4146 bmap = isl_basic_map_apply_range(bmap, range);
4147 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4149 total = isl_basic_map_total_dim(bmap);
4151 for (i = 0; i < n_out; ++i) {
4152 k = isl_basic_map_alloc_equality(bmap);
4155 isl_seq_clr(bmap->eq[k], 1 + total);
4156 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4157 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4160 bmap = isl_basic_map_gauss(bmap, NULL);
4161 return isl_basic_map_finalize(bmap);
4163 isl_basic_map_free(bmap);
4167 int isl_map_may_be_set(__isl_keep isl_map *map)
4171 return isl_space_may_be_set(map->dim);
4174 struct isl_set *isl_map_range(struct isl_map *map)
4177 struct isl_set *set;
4181 if (isl_map_may_be_set(map))
4182 return (isl_set *)map;
4184 map = isl_map_cow(map);
4188 set = (struct isl_set *) map;
4189 set->dim = isl_space_drop_inputs(set->dim, 0, set->dim->n_in);
4192 for (i = 0; i < map->n; ++i) {
4193 set->p[i] = isl_basic_map_range(map->p[i]);
4197 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4198 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4205 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4208 isl_space *domain_dim;
4210 map = isl_map_cow(map);
4214 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4215 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4216 map->dim = isl_space_join(map->dim, domain_dim);
4219 for (i = 0; i < map->n; ++i) {
4220 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4224 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4225 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4232 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4235 isl_space *range_dim;
4237 map = isl_map_cow(map);
4241 range_dim = isl_space_range(isl_map_get_space(map));
4242 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4243 map->dim = isl_space_join(map->dim, range_dim);
4246 for (i = 0; i < map->n; ++i) {
4247 map->p[i] = isl_basic_map_range_map(map->p[i]);
4251 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4252 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4259 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4260 __isl_take isl_space *dim)
4263 struct isl_map *map = NULL;
4265 set = isl_set_cow(set);
4268 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4269 map = (struct isl_map *)set;
4270 for (i = 0; i < set->n; ++i) {
4271 map->p[i] = isl_basic_map_from_basic_set(
4272 set->p[i], isl_space_copy(dim));
4276 isl_space_free(map->dim);
4280 isl_space_free(dim);
4285 __isl_give isl_basic_map *isl_basic_map_from_domain(
4286 __isl_take isl_basic_set *bset)
4288 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4291 __isl_give isl_basic_map *isl_basic_map_from_range(
4292 __isl_take isl_basic_set *bset)
4294 return (isl_basic_map *)bset;
4297 struct isl_map *isl_map_from_range(struct isl_set *set)
4299 return (struct isl_map *)set;
4302 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4304 return isl_map_reverse(isl_map_from_range(set));
4307 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4308 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4310 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4311 isl_basic_map_from_range(range));
4314 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4315 __isl_take isl_set *range)
4317 return isl_map_apply_range(isl_map_from_domain(domain),
4318 isl_map_from_range(range));
4321 struct isl_set *isl_set_from_map(struct isl_map *map)
4324 struct isl_set *set = NULL;
4328 map = isl_map_cow(map);
4331 map->dim = isl_space_as_set_space(map->dim);
4334 set = (struct isl_set *)map;
4335 for (i = 0; i < map->n; ++i) {
4336 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4346 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4349 struct isl_map *map;
4353 isl_assert(dim->ctx, n >= 0, return NULL);
4354 map = isl_alloc(dim->ctx, struct isl_map,
4355 sizeof(struct isl_map) +
4356 (n - 1) * sizeof(struct isl_basic_map *));
4360 map->ctx = dim->ctx;
4361 isl_ctx_ref(map->ctx);
4369 isl_space_free(dim);
4373 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4374 unsigned nparam, unsigned in, unsigned out, int n,
4377 struct isl_map *map;
4380 dims = isl_space_alloc(ctx, nparam, in, out);
4384 map = isl_map_alloc_space(dims, n, flags);
4388 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4390 struct isl_basic_map *bmap;
4391 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4392 bmap = isl_basic_map_set_to_empty(bmap);
4396 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4398 struct isl_basic_set *bset;
4399 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4400 bset = isl_basic_set_set_to_empty(bset);
4404 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4406 struct isl_basic_map *bmap;
4409 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4410 bmap = isl_basic_map_set_to_empty(bmap);
4414 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4416 struct isl_basic_map *bmap;
4419 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4420 bmap = isl_basic_map_set_to_empty(bmap);
4424 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4426 struct isl_basic_set *bset;
4429 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4430 bset = isl_basic_set_set_to_empty(bset);
4434 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4436 struct isl_basic_map *bmap;
4437 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4438 bmap = isl_basic_map_finalize(bmap);
4442 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4444 struct isl_basic_set *bset;
4445 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4446 bset = isl_basic_set_finalize(bset);
4450 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4453 unsigned total = isl_space_dim(dim, isl_dim_all);
4454 isl_basic_map *bmap;
4456 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4457 for (i = 0; i < total; ++i) {
4458 int k = isl_basic_map_alloc_inequality(bmap);
4461 isl_seq_clr(bmap->ineq[k], 1 + total);
4462 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4466 isl_basic_map_free(bmap);
4470 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4472 return isl_basic_map_nat_universe(dim);
4475 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4477 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4480 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4482 return isl_map_nat_universe(dim);
4485 __isl_give isl_basic_map *isl_basic_map_universe_like(
4486 __isl_keep isl_basic_map *model)
4490 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4493 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4497 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4500 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4501 __isl_keep isl_set *model)
4505 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4508 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4510 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4513 struct isl_map *isl_map_empty_like(struct isl_map *model)
4517 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4520 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4524 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4527 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4529 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4532 struct isl_set *isl_set_empty_like(struct isl_set *model)
4536 return isl_set_empty(isl_space_copy(model->dim));
4539 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4541 struct isl_map *map;
4544 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4545 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4549 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4551 struct isl_set *set;
4554 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4555 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4559 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4563 return isl_set_universe(isl_space_copy(model->dim));
4566 struct isl_map *isl_map_dup(struct isl_map *map)
4569 struct isl_map *dup;
4573 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4574 for (i = 0; i < map->n; ++i)
4575 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4579 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4580 __isl_take isl_basic_map *bmap)
4584 if (isl_basic_map_plain_is_empty(bmap)) {
4585 isl_basic_map_free(bmap);
4588 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4589 isl_assert(map->ctx, map->n < map->size, goto error);
4590 map->p[map->n] = bmap;
4592 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4598 isl_basic_map_free(bmap);
4602 void isl_map_free(struct isl_map *map)
4612 isl_ctx_deref(map->ctx);
4613 for (i = 0; i < map->n; ++i)
4614 isl_basic_map_free(map->p[i]);
4615 isl_space_free(map->dim);
4619 struct isl_map *isl_map_extend(struct isl_map *base,
4620 unsigned nparam, unsigned n_in, unsigned n_out)
4624 base = isl_map_cow(base);
4628 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4631 for (i = 0; i < base->n; ++i) {
4632 base->p[i] = isl_basic_map_extend_space(base->p[i],
4633 isl_space_copy(base->dim), 0, 0, 0);
4643 struct isl_set *isl_set_extend(struct isl_set *base,
4644 unsigned nparam, unsigned dim)
4646 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4650 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4651 struct isl_basic_map *bmap, unsigned pos, int value)
4655 bmap = isl_basic_map_cow(bmap);
4656 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4657 j = isl_basic_map_alloc_equality(bmap);
4660 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4661 isl_int_set_si(bmap->eq[j][pos], -1);
4662 isl_int_set_si(bmap->eq[j][0], value);
4663 bmap = isl_basic_map_simplify(bmap);
4664 return isl_basic_map_finalize(bmap);
4666 isl_basic_map_free(bmap);
4670 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4671 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4675 bmap = isl_basic_map_cow(bmap);
4676 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4677 j = isl_basic_map_alloc_equality(bmap);
4680 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4681 isl_int_set_si(bmap->eq[j][pos], -1);
4682 isl_int_set(bmap->eq[j][0], value);
4683 bmap = isl_basic_map_simplify(bmap);
4684 return isl_basic_map_finalize(bmap);
4686 isl_basic_map_free(bmap);
4690 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4691 enum isl_dim_type type, unsigned pos, int value)
4695 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4696 return isl_basic_map_fix_pos_si(bmap,
4697 isl_basic_map_offset(bmap, type) + pos, value);
4699 isl_basic_map_free(bmap);
4703 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4704 enum isl_dim_type type, unsigned pos, isl_int value)
4708 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4709 return isl_basic_map_fix_pos(bmap,
4710 isl_basic_map_offset(bmap, type) + pos, value);
4712 isl_basic_map_free(bmap);
4716 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4717 enum isl_dim_type type, unsigned pos, int value)
4719 return (struct isl_basic_set *)
4720 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4724 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4725 enum isl_dim_type type, unsigned pos, isl_int value)
4727 return (struct isl_basic_set *)
4728 isl_basic_map_fix((struct isl_basic_map *)bset,
4732 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4733 unsigned input, int value)
4735 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4738 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4739 unsigned dim, int value)
4741 return (struct isl_basic_set *)
4742 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4743 isl_dim_set, dim, value);
4746 struct isl_map *isl_map_fix_si(struct isl_map *map,
4747 enum isl_dim_type type, unsigned pos, int value)
4751 map = isl_map_cow(map);
4755 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4756 for (i = 0; i < map->n; ++i) {
4757 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4761 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4768 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4769 enum isl_dim_type type, unsigned pos, int value)
4771 return (struct isl_set *)
4772 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4775 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4776 enum isl_dim_type type, unsigned pos, isl_int value)
4780 map = isl_map_cow(map);
4784 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4785 for (i = 0; i < map->n; ++i) {
4786 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4790 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4797 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4798 enum isl_dim_type type, unsigned pos, isl_int value)
4800 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4803 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4804 unsigned input, int value)
4806 return isl_map_fix_si(map, isl_dim_in, input, value);
4809 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4811 return (struct isl_set *)
4812 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4815 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4816 __isl_take isl_basic_map *bmap,
4817 enum isl_dim_type type, unsigned pos, int value)
4823 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4824 pos += isl_basic_map_offset(bmap, type);
4825 bmap = isl_basic_map_cow(bmap);
4826 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4827 j = isl_basic_map_alloc_inequality(bmap);
4830 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4831 isl_int_set_si(bmap->ineq[j][pos], 1);
4832 isl_int_set_si(bmap->ineq[j][0], -value);
4833 bmap = isl_basic_map_simplify(bmap);
4834 return isl_basic_map_finalize(bmap);
4836 isl_basic_map_free(bmap);
4840 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4841 unsigned dim, isl_int value)
4845 bset = isl_basic_set_cow(bset);
4846 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4847 j = isl_basic_set_alloc_inequality(bset);
4850 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4851 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4852 isl_int_neg(bset->ineq[j][0], value);
4853 bset = isl_basic_set_simplify(bset);
4854 return isl_basic_set_finalize(bset);
4856 isl_basic_set_free(bset);
4860 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4861 enum isl_dim_type type, unsigned pos, int value)
4865 map = isl_map_cow(map);
4869 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4870 for (i = 0; i < map->n; ++i) {
4871 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4876 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4883 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4884 enum isl_dim_type type, unsigned pos, int value)
4886 return (struct isl_set *)
4887 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4890 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4895 set = isl_set_cow(set);
4899 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4900 for (i = 0; i < set->n; ++i) {
4901 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4911 struct isl_map *isl_map_reverse(struct isl_map *map)
4915 map = isl_map_cow(map);
4919 map->dim = isl_space_reverse(map->dim);
4922 for (i = 0; i < map->n; ++i) {
4923 map->p[i] = isl_basic_map_reverse(map->p[i]);
4927 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4934 static struct isl_map *isl_basic_map_partial_lexopt(
4935 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4936 struct isl_set **empty, int max)
4940 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4941 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4943 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4945 isl_basic_map_free(bmap);
4946 isl_basic_set_free(dom);
4952 struct isl_map *isl_basic_map_partial_lexmax(
4953 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4954 struct isl_set **empty)
4956 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4959 struct isl_map *isl_basic_map_partial_lexmin(
4960 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4961 struct isl_set **empty)
4963 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4966 struct isl_set *isl_basic_set_partial_lexmin(
4967 struct isl_basic_set *bset, struct isl_basic_set *dom,
4968 struct isl_set **empty)
4970 return (struct isl_set *)
4971 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4975 struct isl_set *isl_basic_set_partial_lexmax(
4976 struct isl_basic_set *bset, struct isl_basic_set *dom,
4977 struct isl_set **empty)
4979 return (struct isl_set *)
4980 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4984 /* Given a basic map "bmap", compute the lexicographically minimal
4985 * (or maximal) image element for each domain element in dom.
4986 * Set *empty to those elements in dom that do not have an image element.
4988 * We first make sure the basic sets in dom are disjoint and then
4989 * simply collect the results over each of the basic sets separately.
4990 * We could probably improve the efficiency a bit by moving the union
4991 * domain down into the parametric integer programming.
4993 static __isl_give isl_map *basic_map_partial_lexopt(
4994 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4995 __isl_give isl_set **empty, int max)
4998 struct isl_map *res;
5000 dom = isl_set_make_disjoint(dom);
5004 if (isl_set_plain_is_empty(dom)) {
5005 res = isl_map_empty_like_basic_map(bmap);
5006 *empty = isl_set_empty_like(dom);
5008 isl_basic_map_free(bmap);
5012 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5013 isl_basic_set_copy(dom->p[0]), empty, max);
5015 for (i = 1; i < dom->n; ++i) {
5016 struct isl_map *res_i;
5017 struct isl_set *empty_i;
5019 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5020 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5022 res = isl_map_union_disjoint(res, res_i);
5023 *empty = isl_set_union_disjoint(*empty, empty_i);
5027 isl_basic_map_free(bmap);
5032 isl_basic_map_free(bmap);
5036 /* Given a map "map", compute the lexicographically minimal
5037 * (or maximal) image element for each domain element in dom.
5038 * Set *empty to those elements in dom that do not have an image element.
5040 * We first compute the lexicographically minimal or maximal element
5041 * in the first basic map. This results in a partial solution "res"
5042 * and a subset "todo" of dom that still need to be handled.
5043 * We then consider each of the remaining maps in "map" and successively
5044 * improve both "res" and "todo".
5046 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5047 * Assume we are computing the lexicographical maximum.
5048 * We first compute the lexicographically maximal element in basic map i.
5049 * This results in a partial solution res_i and a subset todo_i.
5050 * Then we combine these results with those obtain for the first k basic maps
5051 * to obtain a result that is valid for the first k+1 basic maps.
5052 * In particular, the set where there is no solution is the set where
5053 * there is no solution for the first k basic maps and also no solution
5054 * for the ith basic map, i.e.,
5056 * todo^i = todo^k * todo_i
5058 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5059 * solutions, arbitrarily breaking ties in favor of res^k.
5060 * That is, when res^k(a) >= res_i(a), we pick res^k and
5061 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5062 * the lexicographic order.)
5063 * In practice, we compute
5065 * res^k * (res_i . "<=")
5069 * res_i * (res^k . "<")
5071 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5072 * where only one of res^k and res_i provides a solution and we simply pick
5079 * Note that we only compute these intersections when dom(res^k) intersects
5080 * dom(res_i). Otherwise, the only effect of these intersections is to
5081 * potentially break up res^k and res_i into smaller pieces.
5082 * We want to avoid such splintering as much as possible.
5083 * In fact, an earlier implementation of this function would look for
5084 * better results in the domain of res^k and for extra results in todo^k,
5085 * but this would always result in a splintering according to todo^k,
5086 * even when the domain of basic map i is disjoint from the domains of
5087 * the previous basic maps.
5089 static __isl_give isl_map *isl_map_partial_lexopt(
5090 __isl_take isl_map *map, __isl_take isl_set *dom,
5091 __isl_give isl_set **empty, int max)
5094 struct isl_map *res;
5095 struct isl_set *todo;
5100 if (isl_map_plain_is_empty(map)) {
5108 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5109 isl_set_copy(dom), &todo, max);
5111 for (i = 1; i < map->n; ++i) {
5115 isl_space *dim = isl_space_range(isl_map_get_space(res));
5117 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5118 isl_set_copy(dom), &todo_i, max);
5121 lt = isl_map_lex_lt(isl_space_copy(dim));
5122 le = isl_map_lex_le(dim);
5124 lt = isl_map_lex_gt(isl_space_copy(dim));
5125 le = isl_map_lex_ge(dim);
5127 lt = isl_map_apply_range(isl_map_copy(res), lt);
5128 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5129 le = isl_map_apply_range(isl_map_copy(res_i), le);
5130 le = isl_map_intersect(le, isl_map_copy(res));
5132 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5133 res = isl_map_intersect_domain(res,
5134 isl_set_copy(todo_i));
5135 res_i = isl_map_intersect_domain(res_i,
5136 isl_set_copy(todo));
5139 res = isl_map_union_disjoint(res, res_i);
5140 res = isl_map_union_disjoint(res, lt);
5141 res = isl_map_union_disjoint(res, le);
5143 todo = isl_set_intersect(todo, todo_i);
5163 __isl_give isl_map *isl_map_partial_lexmax(
5164 __isl_take isl_map *map, __isl_take isl_set *dom,
5165 __isl_give isl_set **empty)
5167 return isl_map_partial_lexopt(map, dom, empty, 1);
5170 __isl_give isl_map *isl_map_partial_lexmin(
5171 __isl_take isl_map *map, __isl_take isl_set *dom,
5172 __isl_give isl_set **empty)
5174 return isl_map_partial_lexopt(map, dom, empty, 0);
5177 __isl_give isl_set *isl_set_partial_lexmin(
5178 __isl_take isl_set *set, __isl_take isl_set *dom,
5179 __isl_give isl_set **empty)
5181 return (struct isl_set *)
5182 isl_map_partial_lexmin((struct isl_map *)set,
5186 __isl_give isl_set *isl_set_partial_lexmax(
5187 __isl_take isl_set *set, __isl_take isl_set *dom,
5188 __isl_give isl_set **empty)
5190 return (struct isl_set *)
5191 isl_map_partial_lexmax((struct isl_map *)set,
5195 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5197 struct isl_basic_set *dom = NULL;
5202 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5203 dom = isl_basic_set_universe(dom_dim);
5204 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5206 isl_basic_map_free(bmap);
5210 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5212 return isl_basic_map_lexopt(bmap, 0);
5215 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5217 return isl_basic_map_lexopt(bmap, 1);
5220 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5222 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5225 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5227 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5230 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5232 struct isl_set *dom = NULL;
5237 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5238 dom = isl_set_universe(dom_dim);
5239 return isl_map_partial_lexopt(map, dom, NULL, max);
5245 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5247 return isl_map_lexopt(map, 0);
5250 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5252 return isl_map_lexopt(map, 1);
5255 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5257 return (isl_set *)isl_map_lexmin((isl_map *)set);
5260 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5262 return (isl_set *)isl_map_lexmax((isl_map *)set);
5265 /* Construct a map that equates the two given dimensions in the given space.
5267 static __isl_give isl_map *equate(__isl_take isl_space *dim,
5268 enum isl_dim_type src_type, int src_pos,
5269 enum isl_dim_type dst_type, int dst_pos)
5271 isl_basic_map *bmap;
5274 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5275 k = isl_basic_map_alloc_equality(bmap);
5278 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
5279 src_pos += isl_basic_map_offset(bmap, src_type);
5280 dst_pos += isl_basic_map_offset(bmap, dst_type);
5281 isl_int_set_si(bmap->eq[k][src_pos], 1);
5282 isl_int_set_si(bmap->eq[k][dst_pos], -1);
5284 return isl_map_from_basic_map(bmap);
5286 isl_basic_map_free(bmap);
5290 /* Extract the first and only affine expression from list
5291 * and then add it to *pwaff with the given dom.
5292 * This domain is known to be disjoint from other domains
5293 * because of the way isl_basic_set_foreach_lexmax works.
5295 static int update_dim_opt(__isl_take isl_basic_set *dom,
5296 __isl_take isl_aff_list *list, void *user)
5298 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5300 isl_pw_aff **pwaff = user;
5301 isl_pw_aff *pwaff_i;
5303 if (isl_aff_list_n_aff(list) != 1)
5304 isl_die(ctx, isl_error_internal,
5305 "expecting single element list", goto error);
5307 aff = isl_aff_list_get_aff(list, 0);
5308 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5310 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5312 isl_aff_list_free(list);
5316 isl_basic_set_free(dom);
5317 isl_aff_list_free(list);
5321 /* Given a one-dimensional basic set, compute the minimum or maximum of that
5322 * dimension as an isl_pw_aff.
5324 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexopt
5325 * call update_dim_opt on each leaf of the result.
5327 static __isl_give isl_pw_aff *basic_set_dim_opt(__isl_keep isl_basic_set *bset,
5330 isl_space *dim = isl_basic_set_get_space(bset);
5334 dim = isl_space_domain(isl_space_from_range(dim));
5335 pwaff = isl_pw_aff_empty(dim);
5337 r = isl_basic_set_foreach_lexopt(bset, max, &update_dim_opt, &pwaff);
5339 return isl_pw_aff_free(pwaff);
5344 /* Compute the minimum or maximum of the given set dimension
5345 * as a function of the parameters, but independently of
5346 * the other set dimensions.
5348 * We first project the set onto the given dimension and then compute
5349 * the "lexicographic" maximum in each basic set, combining the results
5350 * using isl_pw_aff_union_max.
5352 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5359 map = isl_map_from_domain(set);
5360 map = isl_map_add_dims(map, isl_dim_out, 1);
5361 map = isl_map_intersect(map,
5362 equate(isl_map_get_space(map), isl_dim_in, pos,
5364 set = isl_map_range(map);
5369 isl_space *dim = isl_set_get_space(set);
5370 dim = isl_space_domain(isl_space_from_range(dim));
5372 return isl_pw_aff_empty(dim);
5375 pwaff = basic_set_dim_opt(set->p[0], max);
5376 for (i = 1; i < set->n; ++i) {
5377 isl_pw_aff *pwaff_i;
5379 pwaff_i = basic_set_dim_opt(set->p[i], max);
5380 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5388 /* Compute the maximum of the given set dimension as a function of the
5389 * parameters, but independently of the other set dimensions.
5391 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5393 return set_dim_opt(set, pos, 1);
5396 /* Compute the minimum of the given set dimension as a function of the
5397 * parameters, but independently of the other set dimensions.
5399 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5401 return set_dim_opt(set, pos, 0);
5404 /* Apply a preimage specified by "mat" on the parameters of "bset".
5405 * bset is assumed to have only parameters and divs.
5407 static struct isl_basic_set *basic_set_parameter_preimage(
5408 struct isl_basic_set *bset, struct isl_mat *mat)
5415 bset->dim = isl_space_cow(bset->dim);
5419 nparam = isl_basic_set_dim(bset, isl_dim_param);
5421 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5423 bset->dim->nparam = 0;
5424 bset->dim->n_out = nparam;
5425 bset = isl_basic_set_preimage(bset, mat);
5427 bset->dim->nparam = bset->dim->n_out;
5428 bset->dim->n_out = 0;
5433 isl_basic_set_free(bset);
5437 /* Apply a preimage specified by "mat" on the parameters of "set".
5438 * set is assumed to have only parameters and divs.
5440 static struct isl_set *set_parameter_preimage(
5441 struct isl_set *set, struct isl_mat *mat)
5443 isl_space *dim = NULL;
5449 dim = isl_space_copy(set->dim);
5450 dim = isl_space_cow(dim);
5454 nparam = isl_set_dim(set, isl_dim_param);
5456 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5459 dim->n_out = nparam;
5460 isl_set_reset_space(set, dim);
5461 set = isl_set_preimage(set, mat);
5464 dim = isl_space_copy(set->dim);
5465 dim = isl_space_cow(dim);
5468 dim->nparam = dim->n_out;
5470 isl_set_reset_space(set, dim);
5473 isl_space_free(dim);
5480 /* Intersect the basic set "bset" with the affine space specified by the
5481 * equalities in "eq".
5483 static struct isl_basic_set *basic_set_append_equalities(
5484 struct isl_basic_set *bset, struct isl_mat *eq)
5492 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5497 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5498 for (i = 0; i < eq->n_row; ++i) {
5499 k = isl_basic_set_alloc_equality(bset);
5502 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5503 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5507 bset = isl_basic_set_gauss(bset, NULL);
5508 bset = isl_basic_set_finalize(bset);
5513 isl_basic_set_free(bset);
5517 /* Intersect the set "set" with the affine space specified by the
5518 * equalities in "eq".
5520 static struct isl_set *set_append_equalities(struct isl_set *set,
5528 for (i = 0; i < set->n; ++i) {
5529 set->p[i] = basic_set_append_equalities(set->p[i],
5542 /* Project the given basic set onto its parameter domain, possibly introducing
5543 * new, explicit, existential variables in the constraints.
5544 * The input has parameters and (possibly implicit) existential variables.
5545 * The output has the same parameters, but only
5546 * explicit existentially quantified variables.
5548 * The actual projection is performed by pip, but pip doesn't seem
5549 * to like equalities very much, so we first remove the equalities
5550 * among the parameters by performing a variable compression on
5551 * the parameters. Afterward, an inverse transformation is performed
5552 * and the equalities among the parameters are inserted back in.
5554 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5558 struct isl_mat *T, *T2;
5559 struct isl_set *set;
5560 unsigned nparam, n_div;
5562 bset = isl_basic_set_cow(bset);
5566 if (bset->n_eq == 0)
5567 return isl_basic_set_lexmin(bset);
5569 isl_basic_set_gauss(bset, NULL);
5571 nparam = isl_basic_set_dim(bset, isl_dim_param);
5572 n_div = isl_basic_set_dim(bset, isl_dim_div);
5574 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5575 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5578 if (i == bset->n_eq)
5579 return isl_basic_set_lexmin(bset);
5581 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5583 eq = isl_mat_cow(eq);
5584 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5585 if (T && T->n_col == 0) {
5589 bset = isl_basic_set_set_to_empty(bset);
5590 return isl_set_from_basic_set(bset);
5592 bset = basic_set_parameter_preimage(bset, T);
5594 set = isl_basic_set_lexmin(bset);
5595 set = set_parameter_preimage(set, T2);
5596 set = set_append_equalities(set, eq);
5600 /* Compute an explicit representation for all the existentially
5601 * quantified variables.
5602 * The input and output dimensions are first turned into parameters.
5603 * compute_divs then returns a map with the same parameters and
5604 * no input or output dimensions and the dimension specification
5605 * is reset to that of the input.
5607 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5609 struct isl_basic_set *bset;
5610 struct isl_set *set;
5611 struct isl_map *map;
5612 isl_space *dim, *orig_dim = NULL;
5617 bmap = isl_basic_map_cow(bmap);
5621 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5622 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5623 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5624 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5628 orig_dim = bmap->dim;
5630 bset = (struct isl_basic_set *)bmap;
5632 set = parameter_compute_divs(bset);
5633 map = (struct isl_map *)set;
5634 map = isl_map_reset_space(map, orig_dim);
5638 isl_basic_map_free(bmap);
5642 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5650 off = isl_space_dim(bmap->dim, isl_dim_all);
5651 for (i = 0; i < bmap->n_div; ++i) {
5652 if (isl_int_is_zero(bmap->div[i][0]))
5654 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5660 static int map_divs_known(__isl_keep isl_map *map)
5667 for (i = 0; i < map->n; ++i) {
5668 int known = isl_basic_map_divs_known(map->p[i]);
5676 /* If bmap contains any unknown divs, then compute explicit
5677 * expressions for them. However, this computation may be
5678 * quite expensive, so first try to remove divs that aren't
5681 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5684 struct isl_map *map;
5686 known = isl_basic_map_divs_known(bmap);
5690 return isl_map_from_basic_map(bmap);
5692 bmap = isl_basic_map_drop_redundant_divs(bmap);
5694 known = isl_basic_map_divs_known(bmap);
5698 return isl_map_from_basic_map(bmap);
5700 map = compute_divs(bmap);
5703 isl_basic_map_free(bmap);
5707 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5711 struct isl_map *res;
5718 known = map_divs_known(map);
5726 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5727 for (i = 1 ; i < map->n; ++i) {
5729 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5730 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5731 res = isl_map_union_disjoint(res, r2);
5733 res = isl_map_union(res, r2);
5740 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5742 return (struct isl_set *)
5743 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5746 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5748 return (struct isl_set *)
5749 isl_map_compute_divs((struct isl_map *)set);
5752 struct isl_set *isl_map_domain(struct isl_map *map)
5755 struct isl_set *set;
5760 map = isl_map_cow(map);
5764 set = (struct isl_set *)map;
5765 set->dim = isl_space_domain(set->dim);
5768 for (i = 0; i < map->n; ++i) {
5769 set->p[i] = isl_basic_map_domain(map->p[i]);
5773 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5774 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5781 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
5782 __isl_take isl_map *map2)
5786 struct isl_map *map = NULL;
5800 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
5802 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5803 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5804 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5806 map = isl_map_alloc_space(isl_space_copy(map1->dim),
5807 map1->n + map2->n, flags);
5810 for (i = 0; i < map1->n; ++i) {
5811 map = isl_map_add_basic_map(map,
5812 isl_basic_map_copy(map1->p[i]));
5816 for (i = 0; i < map2->n; ++i) {
5817 map = isl_map_add_basic_map(map,
5818 isl_basic_map_copy(map2->p[i]));
5832 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
5833 __isl_take isl_map *map2)
5835 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
5838 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5840 map1 = isl_map_union_disjoint(map1, map2);
5844 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5848 struct isl_set *isl_set_union_disjoint(
5849 struct isl_set *set1, struct isl_set *set2)
5851 return (struct isl_set *)
5852 isl_map_union_disjoint(
5853 (struct isl_map *)set1, (struct isl_map *)set2);
5856 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5858 return (struct isl_set *)
5859 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5862 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
5863 __isl_take isl_set *set)
5866 struct isl_map *result;
5872 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5873 isl_die(set->ctx, isl_error_invalid,
5874 "parameters don't match", goto error);
5876 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
5877 !isl_map_compatible_range(map, set))
5878 isl_die(set->ctx, isl_error_invalid,
5879 "incompatible spaces", goto error);
5881 if (isl_set_plain_is_universe(set)) {
5886 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5887 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5888 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5890 result = isl_map_alloc_space(isl_space_copy(map->dim),
5891 map->n * set->n, flags);
5894 for (i = 0; i < map->n; ++i)
5895 for (j = 0; j < set->n; ++j) {
5896 result = isl_map_add_basic_map(result,
5897 isl_basic_map_intersect_range(
5898 isl_basic_map_copy(map->p[i]),
5899 isl_basic_set_copy(set->p[j])));
5912 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
5913 __isl_take isl_set *set)
5915 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
5918 struct isl_map *isl_map_intersect_domain(
5919 struct isl_map *map, struct isl_set *set)
5921 return isl_map_reverse(
5922 isl_map_intersect_range(isl_map_reverse(map), set));
5925 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
5926 __isl_take isl_map *map2)
5930 map1 = isl_map_reverse(map1);
5931 map1 = isl_map_apply_range(map1, map2);
5932 return isl_map_reverse(map1);
5939 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
5940 __isl_take isl_map *map2)
5942 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
5945 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
5946 __isl_take isl_map *map2)
5948 isl_space *dim_result;
5949 struct isl_map *result;
5955 dim_result = isl_space_join(isl_space_copy(map1->dim),
5956 isl_space_copy(map2->dim));
5958 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
5961 for (i = 0; i < map1->n; ++i)
5962 for (j = 0; j < map2->n; ++j) {
5963 result = isl_map_add_basic_map(result,
5964 isl_basic_map_apply_range(
5965 isl_basic_map_copy(map1->p[i]),
5966 isl_basic_map_copy(map2->p[j])));
5972 if (result && result->n <= 1)
5973 ISL_F_SET(result, ISL_MAP_DISJOINT);
5981 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
5982 __isl_take isl_map *map2)
5984 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
5988 * returns range - domain
5990 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5992 isl_space *dims, *target_dim;
5993 struct isl_basic_set *bset;
6000 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6001 bmap->dim, isl_dim_out),
6003 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6004 dim = isl_basic_map_n_in(bmap);
6005 nparam = isl_basic_map_n_param(bmap);
6006 bset = isl_basic_set_from_basic_map(bmap);
6007 bset = isl_basic_set_cow(bset);
6008 dims = isl_basic_set_get_space(bset);
6009 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6010 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6011 bset = isl_basic_set_swap_vars(bset, 2*dim);
6012 for (i = 0; i < dim; ++i) {
6013 int j = isl_basic_map_alloc_equality(
6014 (struct isl_basic_map *)bset);
6017 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6018 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6019 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6020 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6022 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6023 bset = isl_basic_set_reset_space(bset, target_dim);
6026 isl_basic_map_free(bmap);
6031 * returns range - domain
6033 struct isl_set *isl_map_deltas(struct isl_map *map)
6037 struct isl_set *result;
6042 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6043 map->dim, isl_dim_out),
6045 dim = isl_map_get_space(map);
6046 dim = isl_space_domain(dim);
6047 result = isl_set_alloc_space(dim, map->n, 0);
6050 for (i = 0; i < map->n; ++i)
6051 result = isl_set_add_basic_set(result,
6052 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6061 * returns [domain -> range] -> range - domain
6063 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6064 __isl_take isl_basic_map *bmap)
6068 isl_basic_map *domain;
6072 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6073 isl_die(bmap->ctx, isl_error_invalid,
6074 "domain and range don't match", goto error);
6076 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6077 n = isl_basic_map_dim(bmap, isl_dim_in);
6079 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6080 domain = isl_basic_map_universe(dim);
6082 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6083 bmap = isl_basic_map_apply_range(bmap, domain);
6084 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6086 total = isl_basic_map_total_dim(bmap);
6088 for (i = 0; i < n; ++i) {
6089 k = isl_basic_map_alloc_equality(bmap);
6092 isl_seq_clr(bmap->eq[k], 1 + total);
6093 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6094 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6095 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6098 bmap = isl_basic_map_gauss(bmap, NULL);
6099 return isl_basic_map_finalize(bmap);
6101 isl_basic_map_free(bmap);
6106 * returns [domain -> range] -> range - domain
6108 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6111 isl_space *domain_dim;
6116 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6117 isl_die(map->ctx, isl_error_invalid,
6118 "domain and range don't match", goto error);
6120 map = isl_map_cow(map);
6124 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6125 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6126 map->dim = isl_space_join(map->dim, domain_dim);
6129 for (i = 0; i < map->n; ++i) {
6130 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6134 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6141 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6143 struct isl_basic_map *bmap;
6151 nparam = dims->nparam;
6153 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6157 for (i = 0; i < dim; ++i) {
6158 int j = isl_basic_map_alloc_equality(bmap);
6161 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6162 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6163 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6165 return isl_basic_map_finalize(bmap);
6167 isl_basic_map_free(bmap);
6171 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6175 if (dim->n_in != dim->n_out)
6176 isl_die(dim->ctx, isl_error_invalid,
6177 "number of input and output dimensions needs to be "
6178 "the same", goto error);
6179 return basic_map_identity(dim);
6181 isl_space_free(dim);
6185 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6187 if (!model || !model->dim)
6189 return isl_basic_map_identity(isl_space_copy(model->dim));
6192 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6194 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6197 struct isl_map *isl_map_identity_like(struct isl_map *model)
6199 if (!model || !model->dim)
6201 return isl_map_identity(isl_space_copy(model->dim));
6204 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6206 if (!model || !model->dim)
6208 return isl_map_identity(isl_space_copy(model->dim));
6211 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6213 isl_space *dim = isl_set_get_space(set);
6215 id = isl_map_identity(isl_space_map_from_set(dim));
6216 return isl_map_intersect_range(id, set);
6219 /* Construct a basic set with all set dimensions having only non-negative
6222 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6223 __isl_take isl_space *space)
6228 struct isl_basic_set *bset;
6232 nparam = space->nparam;
6234 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6237 for (i = 0; i < dim; ++i) {
6238 int k = isl_basic_set_alloc_inequality(bset);
6241 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6242 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6246 isl_basic_set_free(bset);
6250 /* Construct the half-space x_pos >= 0.
6252 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6256 isl_basic_set *nonneg;
6258 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6259 k = isl_basic_set_alloc_inequality(nonneg);
6262 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6263 isl_int_set_si(nonneg->ineq[k][pos], 1);
6265 return isl_basic_set_finalize(nonneg);
6267 isl_basic_set_free(nonneg);
6271 /* Construct the half-space x_pos <= -1.
6273 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6278 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6279 k = isl_basic_set_alloc_inequality(neg);
6282 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6283 isl_int_set_si(neg->ineq[k][0], -1);
6284 isl_int_set_si(neg->ineq[k][pos], -1);
6286 return isl_basic_set_finalize(neg);
6288 isl_basic_set_free(neg);
6292 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6293 enum isl_dim_type type, unsigned first, unsigned n)
6296 isl_basic_set *nonneg;
6304 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6306 for (i = 0; i < n; ++i) {
6307 nonneg = nonneg_halfspace(isl_set_get_space(set),
6308 pos(set->dim, type) + first + i);
6309 neg = neg_halfspace(isl_set_get_space(set),
6310 pos(set->dim, type) + first + i);
6312 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6321 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6322 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6329 if (isl_set_plain_is_empty(set)) {
6334 return fn(set, signs, user);
6337 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6339 half = isl_set_intersect(half, isl_set_copy(set));
6340 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6344 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6346 half = isl_set_intersect(half, set);
6347 return foreach_orthant(half, signs, first + 1, len, fn, user);
6353 /* Call "fn" on the intersections of "set" with each of the orthants
6354 * (except for obviously empty intersections). The orthant is identified
6355 * by the signs array, with each entry having value 1 or -1 according
6356 * to the sign of the corresponding variable.
6358 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6359 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6369 if (isl_set_plain_is_empty(set))
6372 nparam = isl_set_dim(set, isl_dim_param);
6373 nvar = isl_set_dim(set, isl_dim_set);
6375 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6377 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6385 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6387 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6390 int isl_basic_map_is_subset(
6391 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6394 struct isl_map *map1;
6395 struct isl_map *map2;
6397 if (!bmap1 || !bmap2)
6400 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6401 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6403 is_subset = isl_map_is_subset(map1, map2);
6411 int isl_basic_map_is_equal(
6412 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6416 if (!bmap1 || !bmap2)
6418 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6421 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6425 int isl_basic_set_is_equal(
6426 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6428 return isl_basic_map_is_equal(
6429 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6432 int isl_map_is_empty(struct isl_map *map)
6439 for (i = 0; i < map->n; ++i) {
6440 is_empty = isl_basic_map_is_empty(map->p[i]);
6449 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6451 return map ? map->n == 0 : -1;
6454 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6456 return isl_map_plain_is_empty(map);
6459 int isl_set_plain_is_empty(struct isl_set *set)
6461 return set ? set->n == 0 : -1;
6464 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6466 return isl_set_plain_is_empty(set);
6469 int isl_set_is_empty(struct isl_set *set)
6471 return isl_map_is_empty((struct isl_map *)set);
6474 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6479 return isl_space_is_equal(map1->dim, map2->dim);
6482 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6487 return isl_space_is_equal(set1->dim, set2->dim);
6490 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6496 is_subset = isl_map_is_subset(map1, map2);
6499 is_subset = isl_map_is_subset(map2, map1);
6503 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6505 return align_params_map_map_and_test(map1, map2, &map_is_equal);
6508 int isl_basic_map_is_strict_subset(
6509 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6513 if (!bmap1 || !bmap2)
6515 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6518 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6519 if (is_subset == -1)
6524 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6530 is_subset = isl_map_is_subset(map1, map2);
6533 is_subset = isl_map_is_subset(map2, map1);
6534 if (is_subset == -1)
6539 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6541 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6544 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6548 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6551 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6555 return bset->n_eq == 0 && bset->n_ineq == 0;
6558 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6565 for (i = 0; i < map->n; ++i) {
6566 int r = isl_basic_map_is_universe(map->p[i]);
6574 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6576 return isl_map_plain_is_universe((isl_map *) set);
6579 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6581 return isl_set_plain_is_universe(set);
6584 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6586 struct isl_basic_set *bset = NULL;
6587 struct isl_vec *sample = NULL;
6594 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6597 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6598 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6599 copy = isl_basic_map_remove_redundancies(copy);
6600 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6601 isl_basic_map_free(copy);
6605 total = 1 + isl_basic_map_total_dim(bmap);
6606 if (bmap->sample && bmap->sample->size == total) {
6607 int contains = isl_basic_map_contains(bmap, bmap->sample);
6613 isl_vec_free(bmap->sample);
6614 bmap->sample = NULL;
6615 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6618 sample = isl_basic_set_sample_vec(bset);
6621 empty = sample->size == 0;
6622 isl_vec_free(bmap->sample);
6623 bmap->sample = sample;
6625 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6630 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6634 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6637 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6639 return isl_basic_map_plain_is_empty(bmap);
6642 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6646 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6649 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6651 return isl_basic_set_plain_is_empty(bset);
6654 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6656 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6659 struct isl_map *isl_basic_map_union(
6660 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6662 struct isl_map *map;
6663 if (!bmap1 || !bmap2)
6666 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
6668 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
6671 map = isl_map_add_basic_map(map, bmap1);
6672 map = isl_map_add_basic_map(map, bmap2);
6675 isl_basic_map_free(bmap1);
6676 isl_basic_map_free(bmap2);
6680 struct isl_set *isl_basic_set_union(
6681 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6683 return (struct isl_set *)isl_basic_map_union(
6684 (struct isl_basic_map *)bset1,
6685 (struct isl_basic_map *)bset2);
6688 /* Order divs such that any div only depends on previous divs */
6689 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6697 off = isl_space_dim(bmap->dim, isl_dim_all);
6699 for (i = 0; i < bmap->n_div; ++i) {
6701 if (isl_int_is_zero(bmap->div[i][0]))
6703 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6707 isl_basic_map_swap_div(bmap, i, i + pos);
6713 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6715 return (struct isl_basic_set *)
6716 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6719 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6726 for (i = 0; i < map->n; ++i) {
6727 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6738 /* Apply the expansion computed by isl_merge_divs.
6739 * The expansion itself is given by "exp" while the resulting
6740 * list of divs is given by "div".
6742 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6743 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6748 bset = isl_basic_set_cow(bset);
6752 if (div->n_row < bset->n_div)
6753 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6754 "not an expansion", goto error);
6756 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
6757 div->n_row - bset->n_div, 0,
6758 2 * (div->n_row - bset->n_div));
6760 n_div = bset->n_div;
6761 for (i = n_div; i < div->n_row; ++i)
6762 if (isl_basic_set_alloc_div(bset) < 0)
6766 for (i = div->n_row - 1; i >= 0; --i) {
6767 if (j >= 0 && exp[j] == i) {
6769 isl_basic_map_swap_div(bset, i, j);
6772 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6773 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6781 isl_basic_set_free(bset);
6786 /* Look for a div in dst that corresponds to the div "div" in src.
6787 * The divs before "div" in src and dst are assumed to be the same.
6789 * Returns -1 if no corresponding div was found and the position
6790 * of the corresponding div in dst otherwise.
6792 static int find_div(struct isl_basic_map *dst,
6793 struct isl_basic_map *src, unsigned div)
6797 unsigned total = isl_space_dim(src->dim, isl_dim_all);
6799 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6800 for (i = div; i < dst->n_div; ++i)
6801 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6802 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6803 dst->n_div - div) == -1)
6808 struct isl_basic_map *isl_basic_map_align_divs(
6809 struct isl_basic_map *dst, struct isl_basic_map *src)
6812 unsigned total = isl_space_dim(src->dim, isl_dim_all);
6817 if (src->n_div == 0)
6820 for (i = 0; i < src->n_div; ++i)
6821 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6823 src = isl_basic_map_order_divs(src);
6824 dst = isl_basic_map_cow(dst);
6825 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
6826 src->n_div, 0, 2 * src->n_div);
6829 for (i = 0; i < src->n_div; ++i) {
6830 int j = find_div(dst, src, i);
6832 j = isl_basic_map_alloc_div(dst);
6835 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6836 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6837 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6841 isl_basic_map_swap_div(dst, i, j);
6845 isl_basic_map_free(dst);
6849 struct isl_basic_set *isl_basic_set_align_divs(
6850 struct isl_basic_set *dst, struct isl_basic_set *src)
6852 return (struct isl_basic_set *)isl_basic_map_align_divs(
6853 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6856 struct isl_map *isl_map_align_divs(struct isl_map *map)
6864 map = isl_map_compute_divs(map);
6865 map = isl_map_cow(map);
6869 for (i = 1; i < map->n; ++i)
6870 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6871 for (i = 1; i < map->n; ++i)
6872 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6874 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6878 struct isl_set *isl_set_align_divs(struct isl_set *set)
6880 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6883 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
6884 __isl_take isl_map *map)
6888 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6889 map = isl_map_intersect_domain(map, set);
6890 set = isl_map_range(map);
6898 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
6899 __isl_take isl_map *map)
6901 return isl_map_align_params_map_map_and(set, map, &set_apply);
6904 /* There is no need to cow as removing empty parts doesn't change
6905 * the meaning of the set.
6907 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6914 for (i = map->n-1; i >= 0; --i) {
6915 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6917 isl_basic_map_free(map->p[i]);
6918 if (i != map->n-1) {
6919 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6920 map->p[i] = map->p[map->n-1];
6928 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6930 return (struct isl_set *)
6931 isl_map_remove_empty_parts((struct isl_map *)set);
6934 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6936 struct isl_basic_map *bmap;
6937 if (!map || map->n == 0)
6939 bmap = map->p[map->n-1];
6940 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6941 return isl_basic_map_copy(bmap);
6944 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6946 return (struct isl_basic_set *)
6947 isl_map_copy_basic_map((struct isl_map *)set);
6950 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6951 __isl_keep isl_basic_map *bmap)
6957 for (i = map->n-1; i >= 0; --i) {
6958 if (map->p[i] != bmap)
6960 map = isl_map_cow(map);
6963 isl_basic_map_free(map->p[i]);
6964 if (i != map->n-1) {
6965 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6966 map->p[i] = map->p[map->n-1];
6977 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6978 struct isl_basic_set *bset)
6980 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6981 (struct isl_basic_map *)bset);
6984 /* Given two basic sets bset1 and bset2, compute the maximal difference
6985 * between the values of dimension pos in bset1 and those in bset2
6986 * for any common value of the parameters and dimensions preceding pos.
6988 static enum isl_lp_result basic_set_maximal_difference_at(
6989 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6990 int pos, isl_int *opt)
6993 struct isl_basic_map *bmap1 = NULL;
6994 struct isl_basic_map *bmap2 = NULL;
6995 struct isl_ctx *ctx;
6996 struct isl_vec *obj;
6999 unsigned dim1, dim2;
7000 enum isl_lp_result res;
7002 if (!bset1 || !bset2)
7003 return isl_lp_error;
7005 nparam = isl_basic_set_n_param(bset1);
7006 dim1 = isl_basic_set_n_dim(bset1);
7007 dim2 = isl_basic_set_n_dim(bset2);
7008 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7009 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7010 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7011 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7012 if (!bmap1 || !bmap2)
7014 bmap1 = isl_basic_map_cow(bmap1);
7015 bmap1 = isl_basic_map_extend(bmap1, nparam,
7016 pos, (dim1 - pos) + (dim2 - pos),
7017 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7018 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7021 total = isl_basic_map_total_dim(bmap1);
7023 obj = isl_vec_alloc(ctx, 1 + total);
7024 isl_seq_clr(obj->block.data, 1 + total);
7025 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7026 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7029 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7031 isl_basic_map_free(bmap1);
7035 isl_basic_map_free(bmap1);
7036 isl_basic_map_free(bmap2);
7037 return isl_lp_error;
7040 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7041 * for any common value of the parameters and dimensions preceding pos
7042 * in both basic sets, the values of dimension pos in bset1 are
7043 * smaller or larger than those in bset2.
7046 * 1 if bset1 follows bset2
7047 * -1 if bset1 precedes bset2
7048 * 0 if bset1 and bset2 are incomparable
7049 * -2 if some error occurred.
7051 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7052 struct isl_basic_set *bset2, int pos)
7055 enum isl_lp_result res;
7060 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7062 if (res == isl_lp_empty)
7064 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7065 res == isl_lp_unbounded)
7067 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7076 /* Given two basic sets bset1 and bset2, check whether
7077 * for any common value of the parameters and dimensions preceding pos
7078 * there is a value of dimension pos in bset1 that is larger
7079 * than a value of the same dimension in bset2.
7082 * 1 if there exists such a pair
7083 * 0 if there is no such pair, but there is a pair of equal values
7085 * -2 if some error occurred.
7087 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7088 __isl_keep isl_basic_set *bset2, int pos)
7091 enum isl_lp_result res;
7096 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7098 if (res == isl_lp_empty)
7100 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7101 res == isl_lp_unbounded)
7103 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7105 else if (res == isl_lp_ok)
7114 /* Given two sets set1 and set2, check whether
7115 * for any common value of the parameters and dimensions preceding pos
7116 * there is a value of dimension pos in set1 that is larger
7117 * than a value of the same dimension in set2.
7120 * 1 if there exists such a pair
7121 * 0 if there is no such pair, but there is a pair of equal values
7123 * -2 if some error occurred.
7125 int isl_set_follows_at(__isl_keep isl_set *set1,
7126 __isl_keep isl_set *set2, int pos)
7134 for (i = 0; i < set1->n; ++i)
7135 for (j = 0; j < set2->n; ++j) {
7137 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7138 if (f == 1 || f == -2)
7147 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7148 unsigned pos, isl_int *val)
7156 total = isl_basic_map_total_dim(bmap);
7157 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7158 for (; d+1 > pos; --d)
7159 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7163 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7165 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7167 if (!isl_int_is_one(bmap->eq[i][1+d]))
7170 isl_int_neg(*val, bmap->eq[i][0]);
7176 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7177 unsigned pos, isl_int *val)
7189 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7192 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7193 for (i = 1; fixed == 1 && i < map->n; ++i) {
7194 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7195 if (fixed == 1 && isl_int_ne(tmp, v))
7199 isl_int_set(*val, v);
7205 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7206 unsigned pos, isl_int *val)
7208 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7212 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7215 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7218 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7219 enum isl_dim_type type, unsigned pos, isl_int *val)
7221 if (pos >= isl_basic_map_dim(bmap, type))
7223 return isl_basic_map_plain_has_fixed_var(bmap,
7224 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7227 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7228 enum isl_dim_type type, unsigned pos, isl_int *val)
7230 if (pos >= isl_map_dim(map, type))
7232 return isl_map_plain_has_fixed_var(map,
7233 map_offset(map, type) - 1 + pos, val);
7236 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7237 enum isl_dim_type type, unsigned pos, isl_int *val)
7239 return isl_map_plain_is_fixed(map, type, pos, val);
7242 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7243 * then return this fixed value in *val.
7245 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7246 unsigned dim, isl_int *val)
7248 return isl_basic_set_plain_has_fixed_var(bset,
7249 isl_basic_set_n_param(bset) + dim, val);
7252 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7253 * then return this fixed value in *val.
7255 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7256 unsigned dim, isl_int *val)
7258 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7261 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7262 unsigned dim, isl_int *val)
7264 return isl_set_plain_dim_is_fixed(set, dim, val);
7267 /* Check if input variable in has fixed value and if so and if val is not NULL,
7268 * then return this fixed value in *val.
7270 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7271 unsigned in, isl_int *val)
7273 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7276 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7277 * and if val is not NULL, then return this lower bound in *val.
7279 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7280 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7282 int i, i_eq = -1, i_ineq = -1;
7289 total = isl_basic_set_total_dim(bset);
7290 nparam = isl_basic_set_n_param(bset);
7291 for (i = 0; i < bset->n_eq; ++i) {
7292 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7298 for (i = 0; i < bset->n_ineq; ++i) {
7299 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7301 if (i_eq != -1 || i_ineq != -1)
7305 if (i_eq == -1 && i_ineq == -1)
7307 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7308 /* The coefficient should always be one due to normalization. */
7309 if (!isl_int_is_one(c[1+nparam+dim]))
7311 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7313 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7314 total - nparam - dim - 1) != -1)
7317 isl_int_neg(*val, c[0]);
7321 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7322 unsigned dim, isl_int *val)
7334 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7338 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7340 for (i = 1; fixed == 1 && i < set->n; ++i) {
7341 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7343 if (fixed == 1 && isl_int_ne(tmp, v))
7347 isl_int_set(*val, v);
7358 /* uset_gist depends on constraints without existentially quantified
7359 * variables sorting first.
7361 static int qsort_constraint_cmp(const void *p1, const void *p2)
7363 const struct constraint *c1 = (const struct constraint *)p1;
7364 const struct constraint *c2 = (const struct constraint *)p2;
7366 unsigned size = isl_min(c1->size, c2->size);
7368 l1 = isl_seq_last_non_zero(c1->c, size);
7369 l2 = isl_seq_last_non_zero(c2->c, size);
7374 return isl_seq_cmp(c1->c, c2->c, size);
7377 static struct isl_basic_map *isl_basic_map_sort_constraints(
7378 struct isl_basic_map *bmap)
7381 struct constraint *c;
7386 total = isl_basic_map_total_dim(bmap);
7387 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7390 for (i = 0; i < bmap->n_ineq; ++i) {
7392 c[i].c = bmap->ineq[i];
7394 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7395 for (i = 0; i < bmap->n_ineq; ++i)
7396 bmap->ineq[i] = c[i].c;
7400 isl_basic_map_free(bmap);
7404 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7405 __isl_take isl_basic_set *bset)
7407 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7408 (struct isl_basic_map *)bset);
7411 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7415 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7417 bmap = isl_basic_map_remove_redundancies(bmap);
7418 bmap = isl_basic_map_sort_constraints(bmap);
7419 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7423 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7425 return (struct isl_basic_set *)isl_basic_map_normalize(
7426 (struct isl_basic_map *)bset);
7429 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7430 const __isl_keep isl_basic_map *bmap2)
7437 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7438 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7439 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7440 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7441 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7442 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7443 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7444 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7445 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7446 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7447 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7449 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7451 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7453 if (bmap1->n_eq != bmap2->n_eq)
7454 return bmap1->n_eq - bmap2->n_eq;
7455 if (bmap1->n_ineq != bmap2->n_ineq)
7456 return bmap1->n_ineq - bmap2->n_ineq;
7457 if (bmap1->n_div != bmap2->n_div)
7458 return bmap1->n_div - bmap2->n_div;
7459 total = isl_basic_map_total_dim(bmap1);
7460 for (i = 0; i < bmap1->n_eq; ++i) {
7461 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7465 for (i = 0; i < bmap1->n_ineq; ++i) {
7466 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7470 for (i = 0; i < bmap1->n_div; ++i) {
7471 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7478 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7479 __isl_keep isl_basic_map *bmap2)
7481 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7484 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7485 __isl_keep isl_basic_set *bset2)
7487 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7488 (isl_basic_map *)bset2);
7491 static int qsort_bmap_cmp(const void *p1, const void *p2)
7493 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7494 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7496 return isl_basic_map_plain_cmp(bmap1, bmap2);
7499 /* We normalize in place, but if anything goes wrong we need
7500 * to return NULL, so we need to make sure we don't change the
7501 * meaning of any possible other copies of map.
7503 struct isl_map *isl_map_normalize(struct isl_map *map)
7506 struct isl_basic_map *bmap;
7510 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7512 for (i = 0; i < map->n; ++i) {
7513 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7516 isl_basic_map_free(map->p[i]);
7519 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7520 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7521 map = isl_map_remove_empty_parts(map);
7524 for (i = map->n - 1; i >= 1; --i) {
7525 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7527 isl_basic_map_free(map->p[i-1]);
7528 for (j = i; j < map->n; ++j)
7529 map->p[j-1] = map->p[j];
7539 struct isl_set *isl_set_normalize(struct isl_set *set)
7541 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7544 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7554 if (!isl_space_is_equal(map1->dim, map2->dim))
7557 map1 = isl_map_copy(map1);
7558 map2 = isl_map_copy(map2);
7559 map1 = isl_map_normalize(map1);
7560 map2 = isl_map_normalize(map2);
7563 equal = map1->n == map2->n;
7564 for (i = 0; equal && i < map1->n; ++i) {
7565 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7578 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7580 return isl_map_plain_is_equal(map1, map2);
7583 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7585 return isl_map_plain_is_equal((struct isl_map *)set1,
7586 (struct isl_map *)set2);
7589 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7591 return isl_set_plain_is_equal(set1, set2);
7594 /* Return an interval that ranges from min to max (inclusive)
7596 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7597 isl_int min, isl_int max)
7600 struct isl_basic_set *bset = NULL;
7602 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7606 k = isl_basic_set_alloc_inequality(bset);
7609 isl_int_set_si(bset->ineq[k][1], 1);
7610 isl_int_neg(bset->ineq[k][0], min);
7612 k = isl_basic_set_alloc_inequality(bset);
7615 isl_int_set_si(bset->ineq[k][1], -1);
7616 isl_int_set(bset->ineq[k][0], max);
7620 isl_basic_set_free(bset);
7624 /* Return the Cartesian product of the basic sets in list (in the given order).
7626 __isl_give isl_basic_set *isl_basic_set_list_product(
7627 __isl_take struct isl_basic_set_list *list)
7635 struct isl_basic_set *product = NULL;
7639 isl_assert(list->ctx, list->n > 0, goto error);
7640 isl_assert(list->ctx, list->p[0], goto error);
7641 nparam = isl_basic_set_n_param(list->p[0]);
7642 dim = isl_basic_set_n_dim(list->p[0]);
7643 extra = list->p[0]->n_div;
7644 n_eq = list->p[0]->n_eq;
7645 n_ineq = list->p[0]->n_ineq;
7646 for (i = 1; i < list->n; ++i) {
7647 isl_assert(list->ctx, list->p[i], goto error);
7648 isl_assert(list->ctx,
7649 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7650 dim += isl_basic_set_n_dim(list->p[i]);
7651 extra += list->p[i]->n_div;
7652 n_eq += list->p[i]->n_eq;
7653 n_ineq += list->p[i]->n_ineq;
7655 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7660 for (i = 0; i < list->n; ++i) {
7661 isl_basic_set_add_constraints(product,
7662 isl_basic_set_copy(list->p[i]), dim);
7663 dim += isl_basic_set_n_dim(list->p[i]);
7665 isl_basic_set_list_free(list);
7668 isl_basic_set_free(product);
7669 isl_basic_set_list_free(list);
7673 struct isl_basic_map *isl_basic_map_product(
7674 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7676 isl_space *dim_result = NULL;
7677 struct isl_basic_map *bmap;
7678 unsigned in1, in2, out1, out2, nparam, total, pos;
7679 struct isl_dim_map *dim_map1, *dim_map2;
7681 if (!bmap1 || !bmap2)
7684 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
7685 bmap2->dim, isl_dim_param), goto error);
7686 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
7687 isl_space_copy(bmap2->dim));
7689 in1 = isl_basic_map_n_in(bmap1);
7690 in2 = isl_basic_map_n_in(bmap2);
7691 out1 = isl_basic_map_n_out(bmap1);
7692 out2 = isl_basic_map_n_out(bmap2);
7693 nparam = isl_basic_map_n_param(bmap1);
7695 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7696 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7697 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7698 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7699 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7700 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7701 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7702 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7703 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7704 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7705 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7707 bmap = isl_basic_map_alloc_space(dim_result,
7708 bmap1->n_div + bmap2->n_div,
7709 bmap1->n_eq + bmap2->n_eq,
7710 bmap1->n_ineq + bmap2->n_ineq);
7711 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7712 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7713 bmap = isl_basic_map_simplify(bmap);
7714 return isl_basic_map_finalize(bmap);
7716 isl_basic_map_free(bmap1);
7717 isl_basic_map_free(bmap2);
7721 __isl_give isl_basic_map *isl_basic_map_flat_product(
7722 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7724 isl_basic_map *prod;
7726 prod = isl_basic_map_product(bmap1, bmap2);
7727 prod = isl_basic_map_flatten(prod);
7731 __isl_give isl_basic_set *isl_basic_set_flat_product(
7732 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7734 return isl_basic_map_flat_range_product(bset1, bset2);
7737 __isl_give isl_basic_map *isl_basic_map_range_product(
7738 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7740 isl_space *dim_result = NULL;
7741 isl_basic_map *bmap;
7742 unsigned in, out1, out2, nparam, total, pos;
7743 struct isl_dim_map *dim_map1, *dim_map2;
7745 if (!bmap1 || !bmap2)
7748 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
7749 isl_space_copy(bmap2->dim));
7751 in = isl_basic_map_dim(bmap1, isl_dim_in);
7752 out1 = isl_basic_map_n_out(bmap1);
7753 out2 = isl_basic_map_n_out(bmap2);
7754 nparam = isl_basic_map_n_param(bmap1);
7756 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7757 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7758 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7759 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7760 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7761 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7762 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7763 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7764 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7765 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7766 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7768 bmap = isl_basic_map_alloc_space(dim_result,
7769 bmap1->n_div + bmap2->n_div,
7770 bmap1->n_eq + bmap2->n_eq,
7771 bmap1->n_ineq + bmap2->n_ineq);
7772 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7773 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7774 bmap = isl_basic_map_simplify(bmap);
7775 return isl_basic_map_finalize(bmap);
7777 isl_basic_map_free(bmap1);
7778 isl_basic_map_free(bmap2);
7782 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
7783 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7785 isl_basic_map *prod;
7787 prod = isl_basic_map_range_product(bmap1, bmap2);
7788 prod = isl_basic_map_flatten_range(prod);
7792 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7793 __isl_take isl_map *map2,
7794 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
7795 __isl_take isl_space *right),
7796 __isl_give isl_basic_map *(*basic_map_product)(
7797 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7800 struct isl_map *result;
7806 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
7807 map2->dim, isl_dim_param), goto error);
7809 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7810 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7811 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7813 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
7814 isl_space_copy(map2->dim)),
7815 map1->n * map2->n, flags);
7818 for (i = 0; i < map1->n; ++i)
7819 for (j = 0; j < map2->n; ++j) {
7820 struct isl_basic_map *part;
7821 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7822 isl_basic_map_copy(map2->p[j]));
7823 if (isl_basic_map_is_empty(part))
7824 isl_basic_map_free(part);
7826 result = isl_map_add_basic_map(result, part);
7839 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7841 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
7842 __isl_take isl_map *map2)
7844 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
7847 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
7848 __isl_take isl_map *map2)
7850 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
7853 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7855 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7856 __isl_take isl_map *map2)
7860 prod = isl_map_product(map1, map2);
7861 prod = isl_map_flatten(prod);
7865 /* Given two set A and B, construct its Cartesian product A x B.
7867 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7869 return isl_map_range_product(set1, set2);
7872 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7873 __isl_take isl_set *set2)
7875 return isl_map_flat_range_product(set1, set2);
7878 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7880 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
7881 __isl_take isl_map *map2)
7883 return map_product(map1, map2, &isl_space_range_product,
7884 &isl_basic_map_range_product);
7887 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7888 __isl_take isl_map *map2)
7890 return isl_map_align_params_map_map_and(map1, map2,
7891 &map_range_product_aligned);
7894 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7896 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
7897 __isl_take isl_map *map2)
7901 prod = isl_map_range_product(map1, map2);
7902 prod = isl_map_flatten_range(prod);
7906 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7909 uint32_t hash = isl_hash_init();
7914 bmap = isl_basic_map_copy(bmap);
7915 bmap = isl_basic_map_normalize(bmap);
7918 total = isl_basic_map_total_dim(bmap);
7919 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7920 for (i = 0; i < bmap->n_eq; ++i) {
7922 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7923 isl_hash_hash(hash, c_hash);
7925 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7926 for (i = 0; i < bmap->n_ineq; ++i) {
7928 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7929 isl_hash_hash(hash, c_hash);
7931 isl_hash_byte(hash, bmap->n_div & 0xFF);
7932 for (i = 0; i < bmap->n_div; ++i) {
7934 if (isl_int_is_zero(bmap->div[i][0]))
7936 isl_hash_byte(hash, i & 0xFF);
7937 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7938 isl_hash_hash(hash, c_hash);
7940 isl_basic_map_free(bmap);
7944 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7946 return isl_basic_map_get_hash((isl_basic_map *)bset);
7949 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7956 map = isl_map_copy(map);
7957 map = isl_map_normalize(map);
7961 hash = isl_hash_init();
7962 for (i = 0; i < map->n; ++i) {
7964 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7965 isl_hash_hash(hash, bmap_hash);
7973 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7975 return isl_map_get_hash((isl_map *)set);
7978 /* Check if the value for dimension dim is completely determined
7979 * by the values of the other parameters and variables.
7980 * That is, check if dimension dim is involved in an equality.
7982 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7989 nparam = isl_basic_set_n_param(bset);
7990 for (i = 0; i < bset->n_eq; ++i)
7991 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7996 /* Check if the value for dimension dim is completely determined
7997 * by the values of the other parameters and variables.
7998 * That is, check if dimension dim is involved in an equality
7999 * for each of the subsets.
8001 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8007 for (i = 0; i < set->n; ++i) {
8009 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8016 int isl_set_n_basic_set(__isl_keep isl_set *set)
8018 return set ? set->n : 0;
8021 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8022 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8029 for (i = 0; i < map->n; ++i)
8030 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8036 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8037 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8044 for (i = 0; i < set->n; ++i)
8045 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8051 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8058 bset = isl_basic_set_cow(bset);
8062 dim = isl_basic_set_get_space(bset);
8063 dim = isl_space_lift(dim, bset->n_div);
8066 isl_space_free(bset->dim);
8068 bset->extra -= bset->n_div;
8071 bset = isl_basic_set_finalize(bset);
8075 isl_basic_set_free(bset);
8079 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8085 set = isl_set_align_divs(set);
8090 set = isl_set_cow(set);
8094 n_div = set->p[0]->n_div;
8095 dim = isl_set_get_space(set);
8096 dim = isl_space_lift(dim, n_div);
8099 isl_space_free(set->dim);
8102 for (i = 0; i < set->n; ++i) {
8103 set->p[i] = isl_basic_set_lift(set->p[i]);
8114 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8117 struct isl_basic_map *bmap;
8124 set = isl_set_align_divs(set);
8129 dim = isl_set_get_space(set);
8130 if (set->n == 0 || set->p[0]->n_div == 0) {
8132 return isl_map_identity(isl_space_map_from_set(dim));
8135 n_div = set->p[0]->n_div;
8136 dim = isl_space_map_from_set(dim);
8137 n_param = isl_space_dim(dim, isl_dim_param);
8138 n_set = isl_space_dim(dim, isl_dim_in);
8139 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8140 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8141 for (i = 0; i < n_set; ++i)
8142 bmap = var_equal(bmap, i);
8144 total = n_param + n_set + n_set + n_div;
8145 for (i = 0; i < n_div; ++i) {
8146 k = isl_basic_map_alloc_inequality(bmap);
8149 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8150 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8151 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8152 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8153 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8154 set->p[0]->div[i][0]);
8156 l = isl_basic_map_alloc_inequality(bmap);
8159 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8160 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8161 set->p[0]->div[i][0]);
8162 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8166 bmap = isl_basic_map_simplify(bmap);
8167 bmap = isl_basic_map_finalize(bmap);
8168 return isl_map_from_basic_map(bmap);
8171 isl_basic_map_free(bmap);
8175 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8183 dim = isl_basic_set_total_dim(bset);
8184 size += bset->n_eq * (1 + dim);
8185 size += bset->n_ineq * (1 + dim);
8186 size += bset->n_div * (2 + dim);
8191 int isl_set_size(__isl_keep isl_set *set)
8199 for (i = 0; i < set->n; ++i)
8200 size += isl_basic_set_size(set->p[i]);
8205 /* Check if there is any lower bound (if lower == 0) and/or upper
8206 * bound (if upper == 0) on the specified dim.
8208 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8209 enum isl_dim_type type, unsigned pos, int lower, int upper)
8216 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8218 pos += isl_basic_map_offset(bmap, type);
8220 for (i = 0; i < bmap->n_div; ++i) {
8221 if (isl_int_is_zero(bmap->div[i][0]))
8223 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8227 for (i = 0; i < bmap->n_eq; ++i)
8228 if (!isl_int_is_zero(bmap->eq[i][pos]))
8231 for (i = 0; i < bmap->n_ineq; ++i) {
8232 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8239 return lower && upper;
8242 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8243 enum isl_dim_type type, unsigned pos)
8245 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8248 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8249 enum isl_dim_type type, unsigned pos)
8251 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8254 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8255 enum isl_dim_type type, unsigned pos)
8257 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8260 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8261 enum isl_dim_type type, unsigned pos)
8268 for (i = 0; i < map->n; ++i) {
8270 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8271 if (bounded < 0 || !bounded)
8278 /* Return 1 if the specified dim is involved in both an upper bound
8279 * and a lower bound.
8281 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8282 enum isl_dim_type type, unsigned pos)
8284 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8287 static int has_bound(__isl_keep isl_map *map,
8288 enum isl_dim_type type, unsigned pos,
8289 int (*fn)(__isl_keep isl_basic_map *bmap,
8290 enum isl_dim_type type, unsigned pos))
8297 for (i = 0; i < map->n; ++i) {
8299 bounded = fn(map->p[i], type, pos);
8300 if (bounded < 0 || bounded)
8307 /* Return 1 if the specified dim is involved in any lower bound.
8309 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8310 enum isl_dim_type type, unsigned pos)
8312 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8315 /* Return 1 if the specified dim is involved in any upper bound.
8317 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8318 enum isl_dim_type type, unsigned pos)
8320 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8323 /* For each of the "n" variables starting at "first", determine
8324 * the sign of the variable and put the results in the first "n"
8325 * elements of the array "signs".
8327 * 1 means that the variable is non-negative
8328 * -1 means that the variable is non-positive
8329 * 0 means the variable attains both positive and negative values.
8331 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8332 unsigned first, unsigned n, int *signs)
8334 isl_vec *bound = NULL;
8335 struct isl_tab *tab = NULL;
8336 struct isl_tab_undo *snap;
8339 if (!bset || !signs)
8342 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8343 tab = isl_tab_from_basic_set(bset);
8347 isl_seq_clr(bound->el, bound->size);
8348 isl_int_set_si(bound->el[0], -1);
8350 snap = isl_tab_snap(tab);
8351 for (i = 0; i < n; ++i) {
8354 isl_int_set_si(bound->el[1 + first + i], -1);
8355 if (isl_tab_add_ineq(tab, bound->el) < 0)
8358 isl_int_set_si(bound->el[1 + first + i], 0);
8359 if (isl_tab_rollback(tab, snap) < 0)
8367 isl_int_set_si(bound->el[1 + first + i], 1);
8368 if (isl_tab_add_ineq(tab, bound->el) < 0)
8371 isl_int_set_si(bound->el[1 + first + i], 0);
8372 if (isl_tab_rollback(tab, snap) < 0)
8375 signs[i] = empty ? -1 : 0;
8379 isl_vec_free(bound);
8383 isl_vec_free(bound);
8387 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8388 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8390 if (!bset || !signs)
8392 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8395 first += pos(bset->dim, type) - 1;
8396 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8399 /* Check if the given basic map is obviously single-valued.
8400 * In particular, for each output dimension, check that there is
8401 * an equality that defines the output dimension in terms of
8402 * earlier dimensions.
8404 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8414 total = 1 + isl_basic_map_total_dim(bmap);
8415 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8416 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8418 for (i = 0; i < n_out; ++i) {
8419 for (j = 0; j < bmap->n_eq; ++j) {
8420 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8422 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8423 total - (o_out + i + 1)) == -1)
8426 if (j >= bmap->n_eq)
8433 /* Check if the given map is obviously single-valued.
8435 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8444 return isl_basic_map_plain_is_single_valued(map->p[0]);
8447 /* Check if the given map is single-valued.
8452 * and check if the result is a subset of the identity mapping.
8454 int isl_map_is_single_valued(__isl_keep isl_map *map)
8461 sv = isl_map_plain_is_single_valued(map);
8465 test = isl_map_reverse(isl_map_copy(map));
8466 test = isl_map_apply_range(test, isl_map_copy(map));
8468 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8469 id = isl_map_identity(dim);
8471 sv = isl_map_is_subset(test, id);
8479 int isl_map_is_injective(__isl_keep isl_map *map)
8483 map = isl_map_copy(map);
8484 map = isl_map_reverse(map);
8485 in = isl_map_is_single_valued(map);
8491 /* Check if the given map is obviously injective.
8493 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8497 map = isl_map_copy(map);
8498 map = isl_map_reverse(map);
8499 in = isl_map_plain_is_single_valued(map);
8505 int isl_map_is_bijective(__isl_keep isl_map *map)
8509 sv = isl_map_is_single_valued(map);
8513 return isl_map_is_injective(map);
8516 int isl_set_is_singleton(__isl_keep isl_set *set)
8518 return isl_map_is_single_valued((isl_map *)set);
8521 int isl_map_is_translation(__isl_keep isl_map *map)
8526 delta = isl_map_deltas(isl_map_copy(map));
8527 ok = isl_set_is_singleton(delta);
8528 isl_set_free(delta);
8533 static int unique(isl_int *p, unsigned pos, unsigned len)
8535 if (isl_seq_first_non_zero(p, pos) != -1)
8537 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8542 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8551 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8554 nvar = isl_basic_set_dim(bset, isl_dim_set);
8555 ovar = isl_space_offset(bset->dim, isl_dim_set);
8556 for (j = 0; j < nvar; ++j) {
8557 int lower = 0, upper = 0;
8558 for (i = 0; i < bset->n_eq; ++i) {
8559 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8561 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8567 for (i = 0; i < bset->n_ineq; ++i) {
8568 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8570 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8572 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8577 if (!lower || !upper)
8584 int isl_set_is_box(__isl_keep isl_set *set)
8591 return isl_basic_set_is_box(set->p[0]);
8594 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8599 return isl_space_is_wrapping(bset->dim);
8602 int isl_set_is_wrapping(__isl_keep isl_set *set)
8607 return isl_space_is_wrapping(set->dim);
8610 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8612 bmap = isl_basic_map_cow(bmap);
8616 bmap->dim = isl_space_wrap(bmap->dim);
8620 bmap = isl_basic_map_finalize(bmap);
8622 return (isl_basic_set *)bmap;
8624 isl_basic_map_free(bmap);
8628 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8632 map = isl_map_cow(map);
8636 for (i = 0; i < map->n; ++i) {
8637 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8641 map->dim = isl_space_wrap(map->dim);
8645 return (isl_set *)map;
8651 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8653 bset = isl_basic_set_cow(bset);
8657 bset->dim = isl_space_unwrap(bset->dim);
8661 bset = isl_basic_set_finalize(bset);
8663 return (isl_basic_map *)bset;
8665 isl_basic_set_free(bset);
8669 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8676 if (!isl_set_is_wrapping(set))
8677 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8680 set = isl_set_cow(set);
8684 for (i = 0; i < set->n; ++i) {
8685 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8690 set->dim = isl_space_unwrap(set->dim);
8694 return (isl_map *)set;
8700 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8701 enum isl_dim_type type)
8706 if (!isl_space_is_named_or_nested(bmap->dim, type))
8709 bmap = isl_basic_map_cow(bmap);
8713 bmap->dim = isl_space_reset(bmap->dim, type);
8717 bmap = isl_basic_map_finalize(bmap);
8721 isl_basic_map_free(bmap);
8725 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8726 enum isl_dim_type type)
8733 if (!isl_space_is_named_or_nested(map->dim, type))
8736 map = isl_map_cow(map);
8740 for (i = 0; i < map->n; ++i) {
8741 map->p[i] = isl_basic_map_reset(map->p[i], type);
8745 map->dim = isl_space_reset(map->dim, type);
8755 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8760 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8763 bmap = isl_basic_map_cow(bmap);
8767 bmap->dim = isl_space_flatten(bmap->dim);
8771 bmap = isl_basic_map_finalize(bmap);
8775 isl_basic_map_free(bmap);
8779 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8781 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8784 __isl_give isl_basic_map *isl_basic_map_flatten_range(
8785 __isl_take isl_basic_map *bmap)
8790 if (!bmap->dim->nested[1])
8793 bmap = isl_basic_map_cow(bmap);
8797 bmap->dim = isl_space_flatten_range(bmap->dim);
8801 bmap = isl_basic_map_finalize(bmap);
8805 isl_basic_map_free(bmap);
8809 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8816 if (!map->dim->nested[0] && !map->dim->nested[1])
8819 map = isl_map_cow(map);
8823 for (i = 0; i < map->n; ++i) {
8824 map->p[i] = isl_basic_map_flatten(map->p[i]);
8828 map->dim = isl_space_flatten(map->dim);
8838 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8840 return (isl_set *)isl_map_flatten((isl_map *)set);
8843 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8845 isl_space *dim, *flat_dim;
8848 dim = isl_set_get_space(set);
8849 flat_dim = isl_space_flatten(isl_space_copy(dim));
8850 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
8851 map = isl_map_intersect_domain(map, set);
8856 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
8863 if (!map->dim->nested[1])
8866 map = isl_map_cow(map);
8870 for (i = 0; i < map->n; ++i) {
8871 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
8875 map->dim = isl_space_flatten_range(map->dim);
8885 /* Reorder the dimensions of "bmap" according to the given dim_map
8886 * and set the dimension specification to "dim".
8888 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8889 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
8893 bmap = isl_basic_map_cow(bmap);
8894 if (!bmap || !dim || !dim_map)
8897 res = isl_basic_map_alloc_space(dim,
8898 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8899 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8900 res = isl_basic_map_finalize(res);
8904 isl_basic_map_free(bmap);
8905 isl_space_free(dim);
8909 /* Reorder the dimensions of "map" according to given reordering.
8911 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8912 __isl_take isl_reordering *r)
8915 struct isl_dim_map *dim_map;
8917 map = isl_map_cow(map);
8918 dim_map = isl_dim_map_from_reordering(r);
8919 if (!map || !r || !dim_map)
8922 for (i = 0; i < map->n; ++i) {
8923 struct isl_dim_map *dim_map_i;
8925 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8927 map->p[i] = isl_basic_map_realign(map->p[i],
8928 isl_space_copy(r->dim), dim_map_i);
8934 map = isl_map_reset_space(map, isl_space_copy(r->dim));
8936 isl_reordering_free(r);
8942 isl_reordering_free(r);
8946 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8947 __isl_take isl_reordering *r)
8949 return (isl_set *)isl_map_realign((isl_map *)set, r);
8952 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
8953 __isl_take isl_space *model)
8960 ctx = isl_space_get_ctx(model);
8961 if (!isl_space_has_named_params(model))
8962 isl_die(ctx, isl_error_invalid,
8963 "model has unnamed parameters", goto error);
8964 if (!isl_space_has_named_params(map->dim))
8965 isl_die(ctx, isl_error_invalid,
8966 "relation has unnamed parameters", goto error);
8967 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
8968 isl_reordering *exp;
8970 model = isl_space_drop_dims(model, isl_dim_in,
8971 0, isl_space_dim(model, isl_dim_in));
8972 model = isl_space_drop_dims(model, isl_dim_out,
8973 0, isl_space_dim(model, isl_dim_out));
8974 exp = isl_parameter_alignment_reordering(map->dim, model);
8975 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
8976 map = isl_map_realign(map, exp);
8979 isl_space_free(model);
8982 isl_space_free(model);
8987 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
8988 __isl_take isl_space *model)
8990 return isl_map_align_params(set, model);
8993 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8994 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8995 enum isl_dim_type c2, enum isl_dim_type c3,
8996 enum isl_dim_type c4, enum isl_dim_type c5)
8998 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8999 struct isl_mat *mat;
9005 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9006 isl_basic_map_total_dim(bmap) + 1);
9009 for (i = 0; i < bmap->n_eq; ++i)
9010 for (j = 0, pos = 0; j < 5; ++j) {
9011 int off = isl_basic_map_offset(bmap, c[j]);
9012 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9013 isl_int_set(mat->row[i][pos],
9014 bmap->eq[i][off + k]);
9022 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9023 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9024 enum isl_dim_type c2, enum isl_dim_type c3,
9025 enum isl_dim_type c4, enum isl_dim_type c5)
9027 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9028 struct isl_mat *mat;
9034 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9035 isl_basic_map_total_dim(bmap) + 1);
9038 for (i = 0; i < bmap->n_ineq; ++i)
9039 for (j = 0, pos = 0; j < 5; ++j) {
9040 int off = isl_basic_map_offset(bmap, c[j]);
9041 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9042 isl_int_set(mat->row[i][pos],
9043 bmap->ineq[i][off + k]);
9051 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9052 __isl_take isl_space *dim,
9053 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9054 enum isl_dim_type c2, enum isl_dim_type c3,
9055 enum isl_dim_type c4, enum isl_dim_type c5)
9057 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9058 isl_basic_map *bmap;
9064 if (!dim || !eq || !ineq)
9067 if (eq->n_col != ineq->n_col)
9068 isl_die(dim->ctx, isl_error_invalid,
9069 "equalities and inequalities matrices should have "
9070 "same number of columns", goto error);
9072 total = 1 + isl_space_dim(dim, isl_dim_all);
9074 if (eq->n_col < total)
9075 isl_die(dim->ctx, isl_error_invalid,
9076 "number of columns too small", goto error);
9078 extra = eq->n_col - total;
9080 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9081 eq->n_row, ineq->n_row);
9084 for (i = 0; i < extra; ++i) {
9085 k = isl_basic_map_alloc_div(bmap);
9088 isl_int_set_si(bmap->div[k][0], 0);
9090 for (i = 0; i < eq->n_row; ++i) {
9091 l = isl_basic_map_alloc_equality(bmap);
9094 for (j = 0, pos = 0; j < 5; ++j) {
9095 int off = isl_basic_map_offset(bmap, c[j]);
9096 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9097 isl_int_set(bmap->eq[l][off + k],
9103 for (i = 0; i < ineq->n_row; ++i) {
9104 l = isl_basic_map_alloc_inequality(bmap);
9107 for (j = 0, pos = 0; j < 5; ++j) {
9108 int off = isl_basic_map_offset(bmap, c[j]);
9109 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9110 isl_int_set(bmap->ineq[l][off + k],
9117 isl_space_free(dim);
9123 isl_space_free(dim);
9129 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9130 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9131 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9133 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9134 c1, c2, c3, c4, isl_dim_in);
9137 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9138 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9139 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9141 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9142 c1, c2, c3, c4, isl_dim_in);
9145 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9146 __isl_take isl_space *dim,
9147 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9148 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9150 return (isl_basic_set*)
9151 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9152 c1, c2, c3, c4, isl_dim_in);
9155 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9160 return isl_space_can_zip(bmap->dim);
9163 int isl_map_can_zip(__isl_keep isl_map *map)
9168 return isl_space_can_zip(map->dim);
9171 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9172 * (A -> C) -> (B -> D).
9174 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9183 if (!isl_basic_map_can_zip(bmap))
9184 isl_die(bmap->ctx, isl_error_invalid,
9185 "basic map cannot be zipped", goto error);
9186 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9187 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9188 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9189 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9190 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9193 bmap->dim = isl_space_zip(bmap->dim);
9198 isl_basic_map_free(bmap);
9202 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9203 * (A -> C) -> (B -> D).
9205 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9212 if (!isl_map_can_zip(map))
9213 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9216 map = isl_map_cow(map);
9220 for (i = 0; i < map->n; ++i) {
9221 map->p[i] = isl_basic_map_zip(map->p[i]);
9226 map->dim = isl_space_zip(map->dim);
9236 /* Construct a basic map mapping the domain of the affine expression
9237 * to a one-dimensional range prescribed by the affine expression.
9239 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9243 isl_local_space *ls;
9244 isl_basic_map *bmap;
9249 ls = isl_aff_get_local_space(aff);
9250 ls = isl_local_space_from_domain(ls);
9251 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
9252 bmap = isl_basic_map_from_local_space(ls);
9253 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9254 k = isl_basic_map_alloc_equality(bmap);
9258 pos = isl_basic_map_offset(bmap, isl_dim_out);
9259 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9260 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9261 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9262 aff->v->size - (pos + 1));
9265 bmap = isl_basic_map_finalize(bmap);
9269 isl_basic_map_free(bmap);
9273 /* Construct a basic map mapping a domain in the given space to
9274 * to an n-dimensional range, with n the number of elements in the list,
9275 * where each coordinate in the range is prescribed by the
9276 * corresponding affine expression.
9277 * The domains of all affine expressions in the list are assumed to match
9280 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9281 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9285 isl_basic_map *bmap;
9290 dim = isl_space_from_domain(domain_dim);
9291 bmap = isl_basic_map_universe(dim);
9293 for (i = 0; i < list->n; ++i) {
9295 isl_basic_map *bmap_i;
9297 aff = isl_aff_copy(list->p[i]);
9298 bmap_i = isl_basic_map_from_aff(aff);
9300 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9303 isl_aff_list_free(list);
9307 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9308 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9310 return isl_map_equate(set, type1, pos1, type2, pos2);
9313 /* Add a constraint imposing that the given two dimensions are equal.
9315 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9316 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9318 isl_basic_map *bmap = NULL;
9324 if (pos1 >= isl_map_dim(map, type1))
9325 isl_die(map->ctx, isl_error_invalid,
9326 "index out of bounds", goto error);
9327 if (pos2 >= isl_map_dim(map, type2))
9328 isl_die(map->ctx, isl_error_invalid,
9329 "index out of bounds", goto error);
9331 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9332 i = isl_basic_map_alloc_equality(bmap);
9335 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9336 pos1 += isl_basic_map_offset(bmap, type1);
9337 pos2 += isl_basic_map_offset(bmap, type2);
9338 isl_int_set_si(bmap->eq[i][pos1], -1);
9339 isl_int_set_si(bmap->eq[i][pos2], 1);
9340 bmap = isl_basic_map_finalize(bmap);
9342 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9346 isl_basic_map_free(bmap);
9351 /* Add a constraint imposing that the given two dimensions have opposite values.
9353 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9354 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9356 isl_basic_map *bmap = NULL;
9362 if (pos1 >= isl_map_dim(map, type1))
9363 isl_die(map->ctx, isl_error_invalid,
9364 "index out of bounds", goto error);
9365 if (pos2 >= isl_map_dim(map, type2))
9366 isl_die(map->ctx, isl_error_invalid,
9367 "index out of bounds", goto error);
9369 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9370 i = isl_basic_map_alloc_equality(bmap);
9373 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9374 pos1 += isl_basic_map_offset(bmap, type1);
9375 pos2 += isl_basic_map_offset(bmap, type2);
9376 isl_int_set_si(bmap->eq[i][pos1], 1);
9377 isl_int_set_si(bmap->eq[i][pos2], 1);
9378 bmap = isl_basic_map_finalize(bmap);
9380 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9384 isl_basic_map_free(bmap);