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 /* Is this basic set a parameter domain?
614 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
618 return isl_space_is_params(bset->dim);
621 /* Is this set a parameter domain?
623 int isl_set_is_params(__isl_keep isl_set *set)
627 return isl_space_is_params(set->dim);
630 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
631 struct isl_basic_map *bmap, unsigned extra,
632 unsigned n_eq, unsigned n_ineq)
635 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
640 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
641 if (isl_blk_is_error(bmap->block))
644 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
649 bmap->block2 = isl_blk_empty();
652 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
653 if (isl_blk_is_error(bmap->block2))
656 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
661 for (i = 0; i < n_ineq + n_eq; ++i)
662 bmap->ineq[i] = bmap->block.data + i * row_size;
664 for (i = 0; i < extra; ++i)
665 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
669 bmap->c_size = n_eq + n_ineq;
670 bmap->eq = bmap->ineq + n_ineq;
679 isl_basic_map_free(bmap);
683 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
684 unsigned nparam, unsigned dim, unsigned extra,
685 unsigned n_eq, unsigned n_ineq)
687 struct isl_basic_map *bmap;
690 space = isl_space_set_alloc(ctx, nparam, dim);
694 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
695 return (struct isl_basic_set *)bmap;
698 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
699 unsigned extra, unsigned n_eq, unsigned n_ineq)
701 struct isl_basic_map *bmap;
704 isl_assert(dim->ctx, dim->n_in == 0, goto error);
705 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
706 return (struct isl_basic_set *)bmap;
712 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
713 unsigned extra, unsigned n_eq, unsigned n_ineq)
715 struct isl_basic_map *bmap;
719 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
724 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
730 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
731 unsigned nparam, unsigned in, unsigned out, unsigned extra,
732 unsigned n_eq, unsigned n_ineq)
734 struct isl_basic_map *bmap;
737 dim = isl_space_alloc(ctx, nparam, in, out);
741 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
745 static void dup_constraints(
746 struct isl_basic_map *dst, struct isl_basic_map *src)
749 unsigned total = isl_basic_map_total_dim(src);
751 for (i = 0; i < src->n_eq; ++i) {
752 int j = isl_basic_map_alloc_equality(dst);
753 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
756 for (i = 0; i < src->n_ineq; ++i) {
757 int j = isl_basic_map_alloc_inequality(dst);
758 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
761 for (i = 0; i < src->n_div; ++i) {
762 int j = isl_basic_map_alloc_div(dst);
763 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
765 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
768 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
770 struct isl_basic_map *dup;
774 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
775 bmap->n_div, bmap->n_eq, bmap->n_ineq);
778 dup_constraints(dup, bmap);
779 dup->flags = bmap->flags;
780 dup->sample = isl_vec_copy(bmap->sample);
784 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
786 struct isl_basic_map *dup;
788 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
789 return (struct isl_basic_set *)dup;
792 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
797 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
801 return isl_basic_set_dup(bset);
804 struct isl_set *isl_set_copy(struct isl_set *set)
813 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
818 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
822 bmap = isl_basic_map_dup(bmap);
824 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
828 struct isl_map *isl_map_copy(struct isl_map *map)
837 void isl_basic_map_free(struct isl_basic_map *bmap)
845 isl_ctx_deref(bmap->ctx);
847 isl_blk_free(bmap->ctx, bmap->block2);
849 isl_blk_free(bmap->ctx, bmap->block);
850 isl_vec_free(bmap->sample);
851 isl_space_free(bmap->dim);
855 void isl_basic_set_free(struct isl_basic_set *bset)
857 isl_basic_map_free((struct isl_basic_map *)bset);
860 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
862 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
865 __isl_give isl_map *isl_map_align_params_map_map_and(
866 __isl_take isl_map *map1, __isl_take isl_map *map2,
867 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
868 __isl_take isl_map *map2))
872 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
873 return fn(map1, map2);
874 if (!isl_space_has_named_params(map1->dim) ||
875 !isl_space_has_named_params(map2->dim))
876 isl_die(map1->ctx, isl_error_invalid,
877 "unaligned unnamed parameters", goto error);
878 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
879 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
880 return fn(map1, map2);
887 static int align_params_map_map_and_test(__isl_keep isl_map *map1,
888 __isl_keep isl_map *map2,
889 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
895 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
896 return fn(map1, map2);
897 if (!isl_space_has_named_params(map1->dim) ||
898 !isl_space_has_named_params(map2->dim))
899 isl_die(map1->ctx, isl_error_invalid,
900 "unaligned unnamed parameters", return -1);
901 map1 = isl_map_copy(map1);
902 map2 = isl_map_copy(map2);
903 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
904 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
911 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
917 isl_assert(ctx, room_for_con(bmap, 1), return -1);
918 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
920 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
921 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
922 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
923 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
924 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
925 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
927 int j = isl_basic_map_alloc_inequality(bmap);
931 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
932 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
939 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
940 bmap->extra - bmap->n_div);
944 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
946 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
949 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
953 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
958 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
960 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
963 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
968 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
970 if (pos != bmap->n_eq - 1) {
972 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
973 bmap->eq[bmap->n_eq - 1] = t;
979 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
981 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
984 void isl_basic_map_inequality_to_equality(
985 struct isl_basic_map *bmap, unsigned pos)
990 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
991 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
996 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
997 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
998 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
999 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1002 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1004 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1007 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1009 struct isl_ctx *ctx;
1013 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1014 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1015 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1016 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1017 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1018 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1019 1 + isl_basic_map_total_dim(bmap),
1020 bmap->extra - bmap->n_div);
1021 return bmap->n_ineq++;
1024 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1026 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1029 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1033 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1038 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1040 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1043 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1048 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1050 if (pos != bmap->n_ineq - 1) {
1051 t = bmap->ineq[pos];
1052 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1053 bmap->ineq[bmap->n_ineq - 1] = t;
1054 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1060 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1062 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1065 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1070 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1073 k = isl_basic_map_alloc_equality(bmap);
1076 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1079 isl_basic_map_free(bmap);
1083 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1086 return (isl_basic_set *)
1087 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1090 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1095 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1098 k = isl_basic_map_alloc_inequality(bmap);
1101 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1104 isl_basic_map_free(bmap);
1108 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1111 return (isl_basic_set *)
1112 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1115 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1119 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1120 isl_seq_clr(bmap->div[bmap->n_div] +
1121 1 + 1 + isl_basic_map_total_dim(bmap),
1122 bmap->extra - bmap->n_div);
1123 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1124 return bmap->n_div++;
1127 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1129 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1132 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1136 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1141 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1143 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1146 /* Copy constraint from src to dst, putting the vars of src at offset
1147 * dim_off in dst and the divs of src at offset div_off in dst.
1148 * If both sets are actually map, then dim_off applies to the input
1151 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1152 struct isl_basic_map *src_map, isl_int *src,
1153 unsigned in_off, unsigned out_off, unsigned div_off)
1155 unsigned src_nparam = isl_basic_map_n_param(src_map);
1156 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1157 unsigned src_in = isl_basic_map_n_in(src_map);
1158 unsigned dst_in = isl_basic_map_n_in(dst_map);
1159 unsigned src_out = isl_basic_map_n_out(src_map);
1160 unsigned dst_out = isl_basic_map_n_out(dst_map);
1161 isl_int_set(dst[0], src[0]);
1162 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1163 if (dst_nparam > src_nparam)
1164 isl_seq_clr(dst+1+src_nparam,
1165 dst_nparam - src_nparam);
1166 isl_seq_clr(dst+1+dst_nparam, in_off);
1167 isl_seq_cpy(dst+1+dst_nparam+in_off,
1169 isl_min(dst_in-in_off, src_in));
1170 if (dst_in-in_off > src_in)
1171 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1172 dst_in - in_off - src_in);
1173 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1174 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1175 src+1+src_nparam+src_in,
1176 isl_min(dst_out-out_off, src_out));
1177 if (dst_out-out_off > src_out)
1178 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1179 dst_out - out_off - src_out);
1180 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1181 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1182 src+1+src_nparam+src_in+src_out,
1183 isl_min(dst_map->extra-div_off, src_map->n_div));
1184 if (dst_map->n_div-div_off > src_map->n_div)
1185 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1186 div_off+src_map->n_div,
1187 dst_map->n_div - div_off - src_map->n_div);
1190 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1191 struct isl_basic_map *src_map, isl_int *src,
1192 unsigned in_off, unsigned out_off, unsigned div_off)
1194 isl_int_set(dst[0], src[0]);
1195 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1198 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1199 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1204 if (!bmap1 || !bmap2)
1207 div_off = bmap1->n_div;
1209 for (i = 0; i < bmap2->n_eq; ++i) {
1210 int i1 = isl_basic_map_alloc_equality(bmap1);
1213 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1214 i_pos, o_pos, div_off);
1217 for (i = 0; i < bmap2->n_ineq; ++i) {
1218 int i1 = isl_basic_map_alloc_inequality(bmap1);
1221 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1222 i_pos, o_pos, div_off);
1225 for (i = 0; i < bmap2->n_div; ++i) {
1226 int i1 = isl_basic_map_alloc_div(bmap1);
1229 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1230 i_pos, o_pos, div_off);
1233 isl_basic_map_free(bmap2);
1238 isl_basic_map_free(bmap1);
1239 isl_basic_map_free(bmap2);
1243 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1244 struct isl_basic_set *bset2, unsigned pos)
1246 return (struct isl_basic_set *)
1247 add_constraints((struct isl_basic_map *)bset1,
1248 (struct isl_basic_map *)bset2, 0, pos);
1251 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1252 __isl_take isl_space *dim, unsigned extra,
1253 unsigned n_eq, unsigned n_ineq)
1255 struct isl_basic_map *ext;
1265 dims_ok = isl_space_is_equal(base->dim, dim) &&
1266 base->extra >= base->n_div + extra;
1268 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1269 room_for_ineq(base, n_ineq)) {
1270 isl_space_free(dim);
1274 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1275 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1276 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1277 extra += base->extra;
1279 n_ineq += base->n_ineq;
1281 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1287 ext->sample = isl_vec_copy(base->sample);
1288 flags = base->flags;
1289 ext = add_constraints(ext, base, 0, 0);
1292 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1298 isl_space_free(dim);
1299 isl_basic_map_free(base);
1303 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1304 __isl_take isl_space *dim, unsigned extra,
1305 unsigned n_eq, unsigned n_ineq)
1307 return (struct isl_basic_set *)
1308 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1309 extra, n_eq, n_ineq);
1312 struct isl_basic_map *isl_basic_map_extend_constraints(
1313 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1317 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1321 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1322 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1323 unsigned n_eq, unsigned n_ineq)
1325 struct isl_basic_map *bmap;
1330 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1334 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1337 isl_basic_map_free(base);
1341 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1342 unsigned nparam, unsigned dim, unsigned extra,
1343 unsigned n_eq, unsigned n_ineq)
1345 return (struct isl_basic_set *)
1346 isl_basic_map_extend((struct isl_basic_map *)base,
1347 nparam, 0, dim, extra, n_eq, n_ineq);
1350 struct isl_basic_set *isl_basic_set_extend_constraints(
1351 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1353 return (struct isl_basic_set *)
1354 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1358 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1360 return (struct isl_basic_set *)
1361 isl_basic_map_cow((struct isl_basic_map *)bset);
1364 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1369 if (bmap->ref > 1) {
1371 bmap = isl_basic_map_dup(bmap);
1374 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1378 struct isl_set *isl_set_cow(struct isl_set *set)
1386 return isl_set_dup(set);
1389 struct isl_map *isl_map_cow(struct isl_map *map)
1397 return isl_map_dup(map);
1400 static void swap_vars(struct isl_blk blk, isl_int *a,
1401 unsigned a_len, unsigned b_len)
1403 isl_seq_cpy(blk.data, a+a_len, b_len);
1404 isl_seq_cpy(blk.data+b_len, a, a_len);
1405 isl_seq_cpy(a, blk.data, b_len+a_len);
1408 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1409 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1417 isl_assert(bmap->ctx,
1418 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1420 if (n1 == 0 || n2 == 0)
1423 bmap = isl_basic_map_cow(bmap);
1427 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1428 if (isl_blk_is_error(blk))
1431 for (i = 0; i < bmap->n_eq; ++i)
1433 bmap->eq[i] + pos, n1, n2);
1435 for (i = 0; i < bmap->n_ineq; ++i)
1437 bmap->ineq[i] + pos, n1, n2);
1439 for (i = 0; i < bmap->n_div; ++i)
1441 bmap->div[i]+1 + pos, n1, n2);
1443 isl_blk_free(bmap->ctx, blk);
1445 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1446 bmap = isl_basic_map_gauss(bmap, NULL);
1447 return isl_basic_map_finalize(bmap);
1449 isl_basic_map_free(bmap);
1453 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1454 __isl_take isl_basic_set *bset, unsigned n)
1459 nparam = isl_basic_set_n_param(bset);
1460 dim = isl_basic_set_n_dim(bset);
1461 isl_assert(bset->ctx, n <= dim, goto error);
1463 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1465 isl_basic_set_free(bset);
1469 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1475 total = isl_basic_map_total_dim(bmap);
1476 isl_basic_map_free_div(bmap, bmap->n_div);
1477 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1479 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1481 i = isl_basic_map_alloc_equality(bmap);
1485 isl_int_set_si(bmap->eq[i][0], 1);
1486 isl_seq_clr(bmap->eq[i]+1, total);
1487 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1488 isl_vec_free(bmap->sample);
1489 bmap->sample = NULL;
1490 return isl_basic_map_finalize(bmap);
1492 isl_basic_map_free(bmap);
1496 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1498 return (struct isl_basic_set *)
1499 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1502 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1505 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1506 isl_int *t = bmap->div[a];
1507 bmap->div[a] = bmap->div[b];
1510 for (i = 0; i < bmap->n_eq; ++i)
1511 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1513 for (i = 0; i < bmap->n_ineq; ++i)
1514 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1516 for (i = 0; i < bmap->n_div; ++i)
1517 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1518 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1521 /* Eliminate the specified n dimensions starting at first from the
1522 * constraints using Fourier-Motzkin. The dimensions themselves
1525 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1526 enum isl_dim_type type, unsigned first, unsigned n)
1535 map = isl_map_cow(map);
1538 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1539 first += pos(map->dim, type) - 1;
1541 for (i = 0; i < map->n; ++i) {
1542 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1552 /* Eliminate the specified n dimensions starting at first from the
1553 * constraints using Fourier-Motzkin. The dimensions themselves
1556 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1557 enum isl_dim_type type, unsigned first, unsigned n)
1559 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1562 /* Eliminate the specified n dimensions starting at first from the
1563 * constraints using Fourier-Motzkin. The dimensions themselves
1566 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1567 unsigned first, unsigned n)
1569 return isl_set_eliminate(set, isl_dim_set, first, n);
1572 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1573 __isl_take isl_basic_map *bmap)
1577 bmap = isl_basic_map_eliminate_vars(bmap,
1578 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1582 return isl_basic_map_finalize(bmap);
1585 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1586 __isl_take isl_basic_set *bset)
1588 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1589 (struct isl_basic_map *)bset);
1592 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1601 map = isl_map_cow(map);
1605 for (i = 0; i < map->n; ++i) {
1606 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1616 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1618 return isl_map_remove_divs(set);
1621 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1622 enum isl_dim_type type, unsigned first, unsigned n)
1626 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1628 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1630 bmap = isl_basic_map_eliminate_vars(bmap,
1631 isl_basic_map_offset(bmap, type) - 1 + first, n);
1634 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1636 bmap = isl_basic_map_drop(bmap, type, first, n);
1639 isl_basic_map_free(bmap);
1643 /* Return true if the definition of the given div (recursively) involves
1644 * any of the given variables.
1646 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1647 unsigned first, unsigned n)
1650 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1652 if (isl_int_is_zero(bmap->div[div][0]))
1654 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1657 for (i = bmap->n_div - 1; i >= 0; --i) {
1658 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1660 if (div_involves_vars(bmap, i, first, n))
1667 /* Remove all divs (recursively) involving any of the given dimensions
1668 * in their definitions.
1670 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1671 __isl_take isl_basic_map *bmap,
1672 enum isl_dim_type type, unsigned first, unsigned n)
1678 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1680 first += isl_basic_map_offset(bmap, type);
1682 for (i = bmap->n_div - 1; i >= 0; --i) {
1683 if (!div_involves_vars(bmap, i, first, n))
1685 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1693 isl_basic_map_free(bmap);
1697 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1698 enum isl_dim_type type, unsigned first, unsigned n)
1707 map = isl_map_cow(map);
1711 for (i = 0; i < map->n; ++i) {
1712 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1723 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1724 enum isl_dim_type type, unsigned first, unsigned n)
1726 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1730 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1731 enum isl_dim_type type, unsigned first, unsigned n)
1738 if (first + n > isl_basic_map_dim(bmap, type))
1739 isl_die(bmap->ctx, isl_error_invalid,
1740 "index out of bounds", return -1);
1742 first += isl_basic_map_offset(bmap, type);
1743 for (i = 0; i < bmap->n_eq; ++i)
1744 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1746 for (i = 0; i < bmap->n_ineq; ++i)
1747 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1753 int isl_map_involves_dims(__isl_keep isl_map *map,
1754 enum isl_dim_type type, unsigned first, unsigned n)
1761 if (first + n > isl_map_dim(map, type))
1762 isl_die(map->ctx, isl_error_invalid,
1763 "index out of bounds", return -1);
1765 for (i = 0; i < map->n; ++i) {
1766 int involves = isl_basic_map_involves_dims(map->p[i],
1768 if (involves < 0 || !involves)
1775 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1776 enum isl_dim_type type, unsigned first, unsigned n)
1778 return isl_basic_map_involves_dims(bset, type, first, n);
1781 int isl_set_involves_dims(__isl_keep isl_set *set,
1782 enum isl_dim_type type, unsigned first, unsigned n)
1784 return isl_map_involves_dims(set, type, first, n);
1787 /* Return true if the definition of the given div is unknown or depends
1790 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1793 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1795 if (isl_int_is_zero(bmap->div[div][0]))
1798 for (i = bmap->n_div - 1; i >= 0; --i) {
1799 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1801 if (div_is_unknown(bmap, i))
1808 /* Remove all divs that are unknown or defined in terms of unknown divs.
1810 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1811 __isl_take isl_basic_map *bmap)
1818 for (i = bmap->n_div - 1; i >= 0; --i) {
1819 if (!div_is_unknown(bmap, i))
1821 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1827 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1836 map = isl_map_cow(map);
1840 for (i = 0; i < map->n; ++i) {
1841 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1851 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1853 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1856 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1857 __isl_take isl_basic_set *bset,
1858 enum isl_dim_type type, unsigned first, unsigned n)
1860 return (isl_basic_set *)
1861 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1864 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1865 enum isl_dim_type type, unsigned first, unsigned n)
1872 map = isl_map_cow(map);
1875 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1877 for (i = 0; i < map->n; ++i) {
1878 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1879 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1883 map = isl_map_drop(map, type, first, n);
1890 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1891 enum isl_dim_type type, unsigned first, unsigned n)
1893 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1896 /* Project out n inputs starting at first using Fourier-Motzkin */
1897 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1898 unsigned first, unsigned n)
1900 return isl_map_remove_dims(map, isl_dim_in, first, n);
1903 static void dump_term(struct isl_basic_map *bmap,
1904 isl_int c, int pos, FILE *out)
1907 unsigned in = isl_basic_map_n_in(bmap);
1908 unsigned dim = in + isl_basic_map_n_out(bmap);
1909 unsigned nparam = isl_basic_map_n_param(bmap);
1911 isl_int_print(out, c, 0);
1913 if (!isl_int_is_one(c))
1914 isl_int_print(out, c, 0);
1915 if (pos < 1 + nparam) {
1916 name = isl_space_get_dim_name(bmap->dim,
1917 isl_dim_param, pos - 1);
1919 fprintf(out, "%s", name);
1921 fprintf(out, "p%d", pos - 1);
1922 } else if (pos < 1 + nparam + in)
1923 fprintf(out, "i%d", pos - 1 - nparam);
1924 else if (pos < 1 + nparam + dim)
1925 fprintf(out, "o%d", pos - 1 - nparam - in);
1927 fprintf(out, "e%d", pos - 1 - nparam - dim);
1931 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1932 int sign, FILE *out)
1936 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1940 for (i = 0, first = 1; i < len; ++i) {
1941 if (isl_int_sgn(c[i]) * sign <= 0)
1944 fprintf(out, " + ");
1946 isl_int_abs(v, c[i]);
1947 dump_term(bmap, v, i, out);
1954 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1955 const char *op, FILE *out, int indent)
1959 fprintf(out, "%*s", indent, "");
1961 dump_constraint_sign(bmap, c, 1, out);
1962 fprintf(out, " %s ", op);
1963 dump_constraint_sign(bmap, c, -1, out);
1967 for (i = bmap->n_div; i < bmap->extra; ++i) {
1968 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
1970 fprintf(out, "%*s", indent, "");
1971 fprintf(out, "ERROR: unused div coefficient not zero\n");
1976 static void dump_constraints(struct isl_basic_map *bmap,
1977 isl_int **c, unsigned n,
1978 const char *op, FILE *out, int indent)
1982 for (i = 0; i < n; ++i)
1983 dump_constraint(bmap, c[i], op, out, indent);
1986 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1990 unsigned total = isl_basic_map_total_dim(bmap);
1992 for (j = 0; j < 1 + total; ++j) {
1993 if (isl_int_is_zero(exp[j]))
1995 if (!first && isl_int_is_pos(exp[j]))
1997 dump_term(bmap, exp[j], j, out);
2002 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2006 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2007 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2009 for (i = 0; i < bmap->n_div; ++i) {
2010 fprintf(out, "%*s", indent, "");
2011 fprintf(out, "e%d = [(", i);
2012 dump_affine(bmap, bmap->div[i]+1, out);
2014 isl_int_print(out, bmap->div[i][0], 0);
2015 fprintf(out, "]\n");
2019 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2020 FILE *out, int indent)
2023 fprintf(out, "null basic set\n");
2027 fprintf(out, "%*s", indent, "");
2028 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2029 bset->ref, bset->dim->nparam, bset->dim->n_out,
2030 bset->extra, bset->flags);
2031 dump((struct isl_basic_map *)bset, out, indent);
2034 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2035 FILE *out, int indent)
2038 fprintf(out, "null basic map\n");
2042 fprintf(out, "%*s", indent, "");
2043 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2044 "flags: %x, n_name: %d\n",
2046 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2047 bmap->extra, bmap->flags, bmap->dim->n_id);
2048 dump(bmap, out, indent);
2051 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2056 total = isl_basic_map_total_dim(bmap);
2057 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2058 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2059 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2060 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2064 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2067 struct isl_set *set;
2071 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2072 isl_assert(dim->ctx, n >= 0, goto error);
2073 set = isl_alloc(dim->ctx, struct isl_set,
2074 sizeof(struct isl_set) +
2075 (n - 1) * sizeof(struct isl_basic_set *));
2079 set->ctx = dim->ctx;
2080 isl_ctx_ref(set->ctx);
2088 isl_space_free(dim);
2092 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2093 unsigned nparam, unsigned dim, int n, unsigned flags)
2095 struct isl_set *set;
2098 dims = isl_space_alloc(ctx, nparam, 0, dim);
2102 set = isl_set_alloc_space(dims, n, flags);
2106 /* Make sure "map" has room for at least "n" more basic maps.
2108 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2111 struct isl_map *grown = NULL;
2115 isl_assert(map->ctx, n >= 0, goto error);
2116 if (map->n + n <= map->size)
2118 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2121 for (i = 0; i < map->n; ++i) {
2122 grown->p[i] = isl_basic_map_copy(map->p[i]);
2130 isl_map_free(grown);
2135 /* Make sure "set" has room for at least "n" more basic sets.
2137 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2139 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2142 struct isl_set *isl_set_dup(struct isl_set *set)
2145 struct isl_set *dup;
2150 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2153 for (i = 0; i < set->n; ++i)
2154 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2158 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2160 return isl_map_from_basic_map(bset);
2163 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2165 struct isl_map *map;
2170 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2171 return isl_map_add_basic_map(map, bmap);
2174 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2175 __isl_take isl_basic_set *bset)
2177 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2178 (struct isl_basic_map *)bset);
2181 void isl_set_free(struct isl_set *set)
2191 isl_ctx_deref(set->ctx);
2192 for (i = 0; i < set->n; ++i)
2193 isl_basic_set_free(set->p[i]);
2194 isl_space_free(set->dim);
2198 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2203 fprintf(out, "null set\n");
2207 fprintf(out, "%*s", indent, "");
2208 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2209 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2211 for (i = 0; i < set->n; ++i) {
2212 fprintf(out, "%*s", indent, "");
2213 fprintf(out, "basic set %d:\n", i);
2214 isl_basic_set_print_internal(set->p[i], out, indent+4);
2218 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2223 fprintf(out, "null map\n");
2227 fprintf(out, "%*s", indent, "");
2228 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2229 "flags: %x, n_name: %d\n",
2230 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2231 map->dim->n_out, map->flags, map->dim->n_id);
2232 for (i = 0; i < map->n; ++i) {
2233 fprintf(out, "%*s", indent, "");
2234 fprintf(out, "basic map %d:\n", i);
2235 isl_basic_map_print_internal(map->p[i], out, indent+4);
2239 struct isl_basic_map *isl_basic_map_intersect_domain(
2240 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2242 struct isl_basic_map *bmap_domain;
2247 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2248 bset->dim, isl_dim_param), goto error);
2250 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2251 isl_assert(bset->ctx,
2252 isl_basic_map_compatible_domain(bmap, bset), goto error);
2254 bmap = isl_basic_map_cow(bmap);
2257 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2258 bset->n_div, bset->n_eq, bset->n_ineq);
2259 bmap_domain = isl_basic_map_from_domain(bset);
2260 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2262 bmap = isl_basic_map_simplify(bmap);
2263 return isl_basic_map_finalize(bmap);
2265 isl_basic_map_free(bmap);
2266 isl_basic_set_free(bset);
2270 struct isl_basic_map *isl_basic_map_intersect_range(
2271 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2273 struct isl_basic_map *bmap_range;
2278 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2279 bset->dim, isl_dim_param), goto error);
2281 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2282 isl_assert(bset->ctx,
2283 isl_basic_map_compatible_range(bmap, bset), goto error);
2285 if (isl_basic_set_is_universe(bset)) {
2286 isl_basic_set_free(bset);
2290 bmap = isl_basic_map_cow(bmap);
2293 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2294 bset->n_div, bset->n_eq, bset->n_ineq);
2295 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2296 bmap = add_constraints(bmap, bmap_range, 0, 0);
2298 bmap = isl_basic_map_simplify(bmap);
2299 return isl_basic_map_finalize(bmap);
2301 isl_basic_map_free(bmap);
2302 isl_basic_set_free(bset);
2306 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2312 total = 1 + isl_basic_map_total_dim(bmap);
2313 if (total != vec->size)
2318 for (i = 0; i < bmap->n_eq; ++i) {
2319 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2320 if (!isl_int_is_zero(s)) {
2326 for (i = 0; i < bmap->n_ineq; ++i) {
2327 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2328 if (isl_int_is_neg(s)) {
2339 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2341 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2344 struct isl_basic_map *isl_basic_map_intersect(
2345 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2347 struct isl_vec *sample = NULL;
2349 if (!bmap1 || !bmap2)
2352 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2353 bmap2->dim, isl_dim_param), goto error);
2354 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2355 isl_space_dim(bmap1->dim, isl_dim_param) &&
2356 isl_space_dim(bmap2->dim, isl_dim_all) !=
2357 isl_space_dim(bmap2->dim, isl_dim_param))
2358 return isl_basic_map_intersect(bmap2, bmap1);
2360 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2361 isl_space_dim(bmap2->dim, isl_dim_param))
2362 isl_assert(bmap1->ctx,
2363 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2365 if (bmap1->sample &&
2366 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2367 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2368 sample = isl_vec_copy(bmap1->sample);
2369 else if (bmap2->sample &&
2370 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2371 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2372 sample = isl_vec_copy(bmap2->sample);
2374 bmap1 = isl_basic_map_cow(bmap1);
2377 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2378 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2379 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2382 isl_vec_free(sample);
2384 isl_vec_free(bmap1->sample);
2385 bmap1->sample = sample;
2388 bmap1 = isl_basic_map_simplify(bmap1);
2389 return isl_basic_map_finalize(bmap1);
2392 isl_vec_free(sample);
2393 isl_basic_map_free(bmap1);
2394 isl_basic_map_free(bmap2);
2398 struct isl_basic_set *isl_basic_set_intersect(
2399 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2401 return (struct isl_basic_set *)
2402 isl_basic_map_intersect(
2403 (struct isl_basic_map *)bset1,
2404 (struct isl_basic_map *)bset2);
2407 /* Special case of isl_map_intersect, where both map1 and map2
2408 * are convex, without any divs and such that either map1 or map2
2409 * contains a single constraint. This constraint is then simply
2410 * added to the other map.
2412 static __isl_give isl_map *map_intersect_add_constraint(
2413 __isl_take isl_map *map1, __isl_take isl_map *map2)
2415 isl_assert(map1->ctx, map1->n == 1, goto error);
2416 isl_assert(map2->ctx, map1->n == 1, goto error);
2417 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2418 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2420 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2421 return isl_map_intersect(map2, map1);
2423 isl_assert(map2->ctx,
2424 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2426 map1 = isl_map_cow(map1);
2429 if (isl_map_plain_is_empty(map1)) {
2433 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2434 if (map2->p[0]->n_eq == 1)
2435 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2437 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2438 map2->p[0]->ineq[0]);
2440 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2441 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2445 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2446 isl_basic_map_free(map1->p[0]);
2459 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2460 __isl_take isl_map *map2)
2463 struct isl_map *result;
2469 if (isl_map_plain_is_empty(map1) &&
2470 isl_space_is_equal(map1->dim, map2->dim)) {
2474 if (isl_map_plain_is_empty(map2) &&
2475 isl_space_is_equal(map1->dim, map2->dim)) {
2480 if (map1->n == 1 && map2->n == 1 &&
2481 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2482 isl_space_is_equal(map1->dim, map2->dim) &&
2483 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2484 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2485 return map_intersect_add_constraint(map1, map2);
2486 if (isl_space_dim(map1->dim, isl_dim_all) ==
2487 isl_space_dim(map1->dim, isl_dim_param) &&
2488 isl_space_dim(map2->dim, isl_dim_all) !=
2489 isl_space_dim(map2->dim, isl_dim_param))
2490 return isl_map_intersect(map2, map1);
2492 if (isl_space_dim(map2->dim, isl_dim_all) !=
2493 isl_space_dim(map2->dim, isl_dim_param))
2494 isl_assert(map1->ctx,
2495 isl_space_is_equal(map1->dim, map2->dim), goto error);
2497 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2498 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2499 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2501 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2502 map1->n * map2->n, flags);
2505 for (i = 0; i < map1->n; ++i)
2506 for (j = 0; j < map2->n; ++j) {
2507 struct isl_basic_map *part;
2508 part = isl_basic_map_intersect(
2509 isl_basic_map_copy(map1->p[i]),
2510 isl_basic_map_copy(map2->p[j]));
2511 if (isl_basic_map_is_empty(part))
2512 isl_basic_map_free(part);
2514 result = isl_map_add_basic_map(result, part);
2527 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2528 __isl_take isl_map *map2)
2530 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2533 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2535 return (struct isl_set *)
2536 isl_map_intersect((struct isl_map *)set1,
2537 (struct isl_map *)set2);
2540 /* The current implementation of isl_map_intersect accepts intersections
2541 * with parameter domains, so we can just call that for now.
2543 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2544 __isl_take isl_set *params)
2546 return isl_map_intersect(map, params);
2549 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2550 __isl_take isl_map *map2)
2552 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2555 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2556 __isl_take isl_set *params)
2558 return isl_map_intersect_params(set, params);
2561 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2564 struct isl_basic_set *bset;
2569 bmap = isl_basic_map_cow(bmap);
2572 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2573 in = isl_basic_map_n_in(bmap);
2574 bset = isl_basic_set_from_basic_map(bmap);
2575 bset = isl_basic_set_swap_vars(bset, in);
2576 return isl_basic_map_from_basic_set(bset, dim);
2579 static __isl_give isl_basic_map *basic_map_space_reset(
2580 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2584 if (!isl_space_is_named_or_nested(bmap->dim, type))
2587 space = isl_basic_map_get_space(bmap);
2588 space = isl_space_reset(space, type);
2589 bmap = isl_basic_map_reset_space(bmap, space);
2593 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2594 enum isl_dim_type type, unsigned pos, unsigned n)
2597 struct isl_basic_map *res;
2598 struct isl_dim_map *dim_map;
2599 unsigned total, off;
2600 enum isl_dim_type t;
2603 return basic_map_space_reset(bmap, type);
2608 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2610 total = isl_basic_map_total_dim(bmap) + n;
2611 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2613 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2615 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2617 unsigned size = isl_basic_map_dim(bmap, t);
2618 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2620 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2621 pos, size - pos, off + pos + n);
2623 off += isl_space_dim(res_dim, t);
2625 isl_dim_map_div(dim_map, bmap, off);
2627 res = isl_basic_map_alloc_space(res_dim,
2628 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2629 if (isl_basic_map_is_rational(bmap))
2630 res = isl_basic_map_set_rational(res);
2631 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2632 return isl_basic_map_finalize(res);
2635 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2636 enum isl_dim_type type, unsigned n)
2640 return isl_basic_map_insert(bmap, type,
2641 isl_basic_map_dim(bmap, type), n);
2644 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2645 enum isl_dim_type type, unsigned n)
2649 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2650 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2652 isl_basic_set_free(bset);
2656 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2657 enum isl_dim_type type)
2661 if (!isl_space_is_named_or_nested(map->dim, type))
2664 space = isl_map_get_space(map);
2665 space = isl_space_reset(space, type);
2666 map = isl_map_reset_space(map, space);
2670 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2671 enum isl_dim_type type, unsigned pos, unsigned n)
2676 return map_space_reset(map, type);
2678 map = isl_map_cow(map);
2682 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2686 for (i = 0; i < map->n; ++i) {
2687 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2698 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2699 enum isl_dim_type type, unsigned pos, unsigned n)
2701 return isl_map_insert_dims(set, type, pos, n);
2704 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2705 enum isl_dim_type type, unsigned n)
2709 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2712 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2713 enum isl_dim_type type, unsigned n)
2717 isl_assert(set->ctx, type != isl_dim_in, goto error);
2718 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2724 __isl_give isl_basic_map *isl_basic_map_move_dims(
2725 __isl_take isl_basic_map *bmap,
2726 enum isl_dim_type dst_type, unsigned dst_pos,
2727 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2729 struct isl_dim_map *dim_map;
2730 struct isl_basic_map *res;
2731 enum isl_dim_type t;
2732 unsigned total, off;
2739 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2742 if (dst_type == src_type && dst_pos == src_pos)
2745 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2747 if (pos(bmap->dim, dst_type) + dst_pos ==
2748 pos(bmap->dim, src_type) + src_pos +
2749 ((src_type < dst_type) ? n : 0)) {
2750 bmap = isl_basic_map_cow(bmap);
2754 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2755 src_type, src_pos, n);
2759 bmap = isl_basic_map_finalize(bmap);
2764 total = isl_basic_map_total_dim(bmap);
2765 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2768 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2769 unsigned size = isl_space_dim(bmap->dim, t);
2770 if (t == dst_type) {
2771 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2774 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2777 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2778 dst_pos, size - dst_pos, off);
2779 off += size - dst_pos;
2780 } else if (t == src_type) {
2781 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2784 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2785 src_pos + n, size - src_pos - n, off);
2786 off += size - src_pos - n;
2788 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2792 isl_dim_map_div(dim_map, bmap, off);
2794 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2795 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2796 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2798 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2799 src_type, src_pos, n);
2803 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2804 bmap = isl_basic_map_gauss(bmap, NULL);
2805 bmap = isl_basic_map_finalize(bmap);
2809 isl_basic_map_free(bmap);
2813 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2814 enum isl_dim_type dst_type, unsigned dst_pos,
2815 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2817 return (isl_basic_set *)isl_basic_map_move_dims(
2818 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2821 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2822 enum isl_dim_type dst_type, unsigned dst_pos,
2823 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2827 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2828 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2829 src_type, src_pos, n);
2835 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2836 enum isl_dim_type dst_type, unsigned dst_pos,
2837 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2846 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2849 if (dst_type == src_type && dst_pos == src_pos)
2852 isl_assert(map->ctx, dst_type != src_type, goto error);
2854 map = isl_map_cow(map);
2858 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2862 for (i = 0; i < map->n; ++i) {
2863 map->p[i] = isl_basic_map_move_dims(map->p[i],
2865 src_type, src_pos, n);
2876 /* Move the specified dimensions to the last columns right before
2877 * the divs. Don't change the dimension specification of bmap.
2878 * That's the responsibility of the caller.
2880 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2881 enum isl_dim_type type, unsigned first, unsigned n)
2883 struct isl_dim_map *dim_map;
2884 struct isl_basic_map *res;
2885 enum isl_dim_type t;
2886 unsigned total, off;
2890 if (pos(bmap->dim, type) + first + n ==
2891 1 + isl_space_dim(bmap->dim, isl_dim_all))
2894 total = isl_basic_map_total_dim(bmap);
2895 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2898 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2899 unsigned size = isl_space_dim(bmap->dim, t);
2901 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2904 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2905 first, n, total - bmap->n_div - n);
2906 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2907 first + n, size - (first + n), off);
2908 off += size - (first + n);
2910 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2914 isl_dim_map_div(dim_map, bmap, off + n);
2916 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2917 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2918 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2922 /* Turn the n dimensions of type type, starting at first
2923 * into existentially quantified variables.
2925 __isl_give isl_basic_map *isl_basic_map_project_out(
2926 __isl_take isl_basic_map *bmap,
2927 enum isl_dim_type type, unsigned first, unsigned n)
2940 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2941 return isl_basic_map_remove_dims(bmap, type, first, n);
2943 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2946 bmap = move_last(bmap, type, first, n);
2947 bmap = isl_basic_map_cow(bmap);
2951 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
2952 old = bmap->block2.data;
2953 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2954 (bmap->extra + n) * (1 + row_size));
2955 if (!bmap->block2.data)
2957 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2960 for (i = 0; i < n; ++i) {
2961 new_div[i] = bmap->block2.data +
2962 (bmap->extra + i) * (1 + row_size);
2963 isl_seq_clr(new_div[i], 1 + row_size);
2965 for (i = 0; i < bmap->extra; ++i)
2966 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2968 bmap->div = new_div;
2972 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2975 bmap = isl_basic_map_simplify(bmap);
2976 bmap = isl_basic_map_drop_redundant_divs(bmap);
2977 return isl_basic_map_finalize(bmap);
2979 isl_basic_map_free(bmap);
2983 /* Turn the n dimensions of type type, starting at first
2984 * into existentially quantified variables.
2986 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2987 enum isl_dim_type type, unsigned first, unsigned n)
2989 return (isl_basic_set *)isl_basic_map_project_out(
2990 (isl_basic_map *)bset, type, first, n);
2993 /* Turn the n dimensions of type type, starting at first
2994 * into existentially quantified variables.
2996 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2997 enum isl_dim_type type, unsigned first, unsigned n)
3007 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3009 map = isl_map_cow(map);
3013 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3017 for (i = 0; i < map->n; ++i) {
3018 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3029 /* Turn the n dimensions of type type, starting at first
3030 * into existentially quantified variables.
3032 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3033 enum isl_dim_type type, unsigned first, unsigned n)
3035 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3038 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3042 for (i = 0; i < n; ++i) {
3043 j = isl_basic_map_alloc_div(bmap);
3046 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3050 isl_basic_map_free(bmap);
3054 struct isl_basic_map *isl_basic_map_apply_range(
3055 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3057 isl_space *dim_result = NULL;
3058 struct isl_basic_map *bmap;
3059 unsigned n_in, n_out, n, nparam, total, pos;
3060 struct isl_dim_map *dim_map1, *dim_map2;
3062 if (!bmap1 || !bmap2)
3065 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3066 isl_space_copy(bmap2->dim));
3068 n_in = isl_basic_map_n_in(bmap1);
3069 n_out = isl_basic_map_n_out(bmap2);
3070 n = isl_basic_map_n_out(bmap1);
3071 nparam = isl_basic_map_n_param(bmap1);
3073 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3074 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3075 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3076 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3077 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3078 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3079 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3080 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3081 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3082 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3083 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3085 bmap = isl_basic_map_alloc_space(dim_result,
3086 bmap1->n_div + bmap2->n_div + n,
3087 bmap1->n_eq + bmap2->n_eq,
3088 bmap1->n_ineq + bmap2->n_ineq);
3089 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3090 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3091 bmap = add_divs(bmap, n);
3092 bmap = isl_basic_map_simplify(bmap);
3093 bmap = isl_basic_map_drop_redundant_divs(bmap);
3094 return isl_basic_map_finalize(bmap);
3096 isl_basic_map_free(bmap1);
3097 isl_basic_map_free(bmap2);
3101 struct isl_basic_set *isl_basic_set_apply(
3102 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3107 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3110 return (struct isl_basic_set *)
3111 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3113 isl_basic_set_free(bset);
3114 isl_basic_map_free(bmap);
3118 struct isl_basic_map *isl_basic_map_apply_domain(
3119 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3121 if (!bmap1 || !bmap2)
3124 isl_assert(bmap1->ctx,
3125 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3126 isl_assert(bmap1->ctx,
3127 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3130 bmap1 = isl_basic_map_reverse(bmap1);
3131 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3132 return isl_basic_map_reverse(bmap1);
3134 isl_basic_map_free(bmap1);
3135 isl_basic_map_free(bmap2);
3139 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3140 * A \cap B -> f(A) + f(B)
3142 struct isl_basic_map *isl_basic_map_sum(
3143 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3145 unsigned n_in, n_out, nparam, total, pos;
3146 struct isl_basic_map *bmap = NULL;
3147 struct isl_dim_map *dim_map1, *dim_map2;
3150 if (!bmap1 || !bmap2)
3153 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3156 nparam = isl_basic_map_n_param(bmap1);
3157 n_in = isl_basic_map_n_in(bmap1);
3158 n_out = isl_basic_map_n_out(bmap1);
3160 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3161 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3162 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3163 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3164 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3165 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3166 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3167 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3168 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3169 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3170 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3172 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3173 bmap1->n_div + bmap2->n_div + 2 * n_out,
3174 bmap1->n_eq + bmap2->n_eq + n_out,
3175 bmap1->n_ineq + bmap2->n_ineq);
3176 for (i = 0; i < n_out; ++i) {
3177 int j = isl_basic_map_alloc_equality(bmap);
3180 isl_seq_clr(bmap->eq[j], 1+total);
3181 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3182 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3183 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3185 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3186 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3187 bmap = add_divs(bmap, 2 * n_out);
3189 bmap = isl_basic_map_simplify(bmap);
3190 return isl_basic_map_finalize(bmap);
3192 isl_basic_map_free(bmap);
3193 isl_basic_map_free(bmap1);
3194 isl_basic_map_free(bmap2);
3198 /* Given two maps A -> f(A) and B -> g(B), construct a map
3199 * A \cap B -> f(A) + f(B)
3201 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3203 struct isl_map *result;
3209 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3211 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3212 map1->n * map2->n, 0);
3215 for (i = 0; i < map1->n; ++i)
3216 for (j = 0; j < map2->n; ++j) {
3217 struct isl_basic_map *part;
3218 part = isl_basic_map_sum(
3219 isl_basic_map_copy(map1->p[i]),
3220 isl_basic_map_copy(map2->p[j]));
3221 if (isl_basic_map_is_empty(part))
3222 isl_basic_map_free(part);
3224 result = isl_map_add_basic_map(result, part);
3237 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3238 __isl_take isl_set *set2)
3240 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3243 /* Given a basic map A -> f(A), construct A -> -f(A).
3245 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3250 bmap = isl_basic_map_cow(bmap);
3254 n = isl_basic_map_dim(bmap, isl_dim_out);
3255 off = isl_basic_map_offset(bmap, isl_dim_out);
3256 for (i = 0; i < bmap->n_eq; ++i)
3257 for (j = 0; j < n; ++j)
3258 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3259 for (i = 0; i < bmap->n_ineq; ++i)
3260 for (j = 0; j < n; ++j)
3261 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3262 for (i = 0; i < bmap->n_div; ++i)
3263 for (j = 0; j < n; ++j)
3264 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3265 bmap = isl_basic_map_gauss(bmap, NULL);
3266 return isl_basic_map_finalize(bmap);
3269 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3271 return isl_basic_map_neg(bset);
3274 /* Given a map A -> f(A), construct A -> -f(A).
3276 struct isl_map *isl_map_neg(struct isl_map *map)
3280 map = isl_map_cow(map);
3284 for (i = 0; i < map->n; ++i) {
3285 map->p[i] = isl_basic_map_neg(map->p[i]);
3296 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3298 return (isl_set *)isl_map_neg((isl_map *)set);
3301 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3302 * A -> floor(f(A)/d).
3304 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3307 unsigned n_in, n_out, nparam, total, pos;
3308 struct isl_basic_map *result = NULL;
3309 struct isl_dim_map *dim_map;
3315 nparam = isl_basic_map_n_param(bmap);
3316 n_in = isl_basic_map_n_in(bmap);
3317 n_out = isl_basic_map_n_out(bmap);
3319 total = nparam + n_in + n_out + bmap->n_div + n_out;
3320 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3321 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3322 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3323 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3324 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3326 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3327 bmap->n_div + n_out,
3328 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3329 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3330 result = add_divs(result, n_out);
3331 for (i = 0; i < n_out; ++i) {
3333 j = isl_basic_map_alloc_inequality(result);
3336 isl_seq_clr(result->ineq[j], 1+total);
3337 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3338 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3339 j = isl_basic_map_alloc_inequality(result);
3342 isl_seq_clr(result->ineq[j], 1+total);
3343 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3344 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3345 isl_int_sub_ui(result->ineq[j][0], d, 1);
3348 result = isl_basic_map_simplify(result);
3349 return isl_basic_map_finalize(result);
3351 isl_basic_map_free(result);
3355 /* Given a map A -> f(A) and an integer d, construct a map
3356 * A -> floor(f(A)/d).
3358 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3362 map = isl_map_cow(map);
3366 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3367 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3368 for (i = 0; i < map->n; ++i) {
3369 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3380 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3386 i = isl_basic_map_alloc_equality(bmap);
3389 nparam = isl_basic_map_n_param(bmap);
3390 n_in = isl_basic_map_n_in(bmap);
3391 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3392 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3393 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3394 return isl_basic_map_finalize(bmap);
3396 isl_basic_map_free(bmap);
3400 /* Add a constraints to "bmap" expressing i_pos < o_pos
3402 static struct isl_basic_map *var_less(struct 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][0], -1);
3415 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3416 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3417 return isl_basic_map_finalize(bmap);
3419 isl_basic_map_free(bmap);
3423 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3425 static __isl_give isl_basic_map *var_less_or_equal(
3426 __isl_take isl_basic_map *bmap, unsigned pos)
3432 i = isl_basic_map_alloc_inequality(bmap);
3435 nparam = isl_basic_map_n_param(bmap);
3436 n_in = isl_basic_map_n_in(bmap);
3437 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3438 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3439 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3440 return isl_basic_map_finalize(bmap);
3442 isl_basic_map_free(bmap);
3446 /* Add a constraints to "bmap" expressing i_pos > o_pos
3448 static struct isl_basic_map *var_more(struct 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][0], -1);
3461 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3462 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3463 return isl_basic_map_finalize(bmap);
3465 isl_basic_map_free(bmap);
3469 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3471 static __isl_give isl_basic_map *var_more_or_equal(
3472 __isl_take isl_basic_map *bmap, unsigned pos)
3478 i = isl_basic_map_alloc_inequality(bmap);
3481 nparam = isl_basic_map_n_param(bmap);
3482 n_in = isl_basic_map_n_in(bmap);
3483 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3484 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3485 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3486 return isl_basic_map_finalize(bmap);
3488 isl_basic_map_free(bmap);
3492 __isl_give isl_basic_map *isl_basic_map_equal(
3493 __isl_take isl_space *dim, unsigned n_equal)
3496 struct isl_basic_map *bmap;
3497 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3500 for (i = 0; i < n_equal && bmap; ++i)
3501 bmap = var_equal(bmap, i);
3502 return isl_basic_map_finalize(bmap);
3505 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3507 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3511 struct isl_basic_map *bmap;
3512 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3515 for (i = 0; i < pos && bmap; ++i)
3516 bmap = var_equal(bmap, i);
3518 bmap = var_less(bmap, pos);
3519 return isl_basic_map_finalize(bmap);
3522 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3524 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3525 __isl_take isl_space *dim, unsigned pos)
3528 isl_basic_map *bmap;
3530 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3531 for (i = 0; i < pos; ++i)
3532 bmap = var_equal(bmap, i);
3533 bmap = var_less_or_equal(bmap, pos);
3534 return isl_basic_map_finalize(bmap);
3537 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3539 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3543 struct isl_basic_map *bmap;
3544 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3547 for (i = 0; i < pos && bmap; ++i)
3548 bmap = var_equal(bmap, i);
3550 bmap = var_more(bmap, pos);
3551 return isl_basic_map_finalize(bmap);
3554 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3556 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3557 __isl_take isl_space *dim, unsigned pos)
3560 isl_basic_map *bmap;
3562 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3563 for (i = 0; i < pos; ++i)
3564 bmap = var_equal(bmap, i);
3565 bmap = var_more_or_equal(bmap, pos);
3566 return isl_basic_map_finalize(bmap);
3569 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3570 unsigned n, int equal)
3572 struct isl_map *map;
3575 if (n == 0 && equal)
3576 return isl_map_universe(dims);
3578 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3580 for (i = 0; i + 1 < n; ++i)
3581 map = isl_map_add_basic_map(map,
3582 isl_basic_map_less_at(isl_space_copy(dims), i));
3585 map = isl_map_add_basic_map(map,
3586 isl_basic_map_less_or_equal_at(dims, n - 1));
3588 map = isl_map_add_basic_map(map,
3589 isl_basic_map_less_at(dims, n - 1));
3591 isl_space_free(dims);
3596 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3600 return map_lex_lte_first(dims, dims->n_out, equal);
3603 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3605 return map_lex_lte_first(dim, n, 0);
3608 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3610 return map_lex_lte_first(dim, n, 1);
3613 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3615 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3618 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3620 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3623 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3624 unsigned n, int equal)
3626 struct isl_map *map;
3629 if (n == 0 && equal)
3630 return isl_map_universe(dims);
3632 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3634 for (i = 0; i + 1 < n; ++i)
3635 map = isl_map_add_basic_map(map,
3636 isl_basic_map_more_at(isl_space_copy(dims), i));
3639 map = isl_map_add_basic_map(map,
3640 isl_basic_map_more_or_equal_at(dims, n - 1));
3642 map = isl_map_add_basic_map(map,
3643 isl_basic_map_more_at(dims, n - 1));
3645 isl_space_free(dims);
3650 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3654 return map_lex_gte_first(dims, dims->n_out, equal);
3657 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3659 return map_lex_gte_first(dim, n, 0);
3662 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3664 return map_lex_gte_first(dim, n, 1);
3667 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3669 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3672 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3674 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3677 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3678 __isl_take isl_set *set2)
3681 map = isl_map_lex_le(isl_set_get_space(set1));
3682 map = isl_map_intersect_domain(map, set1);
3683 map = isl_map_intersect_range(map, set2);
3687 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3688 __isl_take isl_set *set2)
3691 map = isl_map_lex_lt(isl_set_get_space(set1));
3692 map = isl_map_intersect_domain(map, set1);
3693 map = isl_map_intersect_range(map, set2);
3697 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3698 __isl_take isl_set *set2)
3701 map = isl_map_lex_ge(isl_set_get_space(set1));
3702 map = isl_map_intersect_domain(map, set1);
3703 map = isl_map_intersect_range(map, set2);
3707 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3708 __isl_take isl_set *set2)
3711 map = isl_map_lex_gt(isl_set_get_space(set1));
3712 map = isl_map_intersect_domain(map, set1);
3713 map = isl_map_intersect_range(map, set2);
3717 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3718 __isl_take isl_map *map2)
3721 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3722 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3723 map = isl_map_apply_range(map, isl_map_reverse(map2));
3727 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3728 __isl_take isl_map *map2)
3731 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3732 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3733 map = isl_map_apply_range(map, isl_map_reverse(map2));
3737 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3738 __isl_take isl_map *map2)
3741 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3742 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3743 map = isl_map_apply_range(map, isl_map_reverse(map2));
3747 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3748 __isl_take isl_map *map2)
3751 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3752 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3753 map = isl_map_apply_range(map, isl_map_reverse(map2));
3757 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3758 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3760 struct isl_basic_map *bmap;
3762 bset = isl_basic_set_cow(bset);
3766 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3767 isl_space_free(bset->dim);
3768 bmap = (struct isl_basic_map *) bset;
3770 return isl_basic_map_finalize(bmap);
3772 isl_basic_set_free(bset);
3773 isl_space_free(dim);
3777 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3781 if (bmap->dim->n_in == 0)
3782 return (struct isl_basic_set *)bmap;
3783 bmap = isl_basic_map_cow(bmap);
3786 bmap->dim = isl_space_as_set_space(bmap->dim);
3789 bmap = isl_basic_map_finalize(bmap);
3790 return (struct isl_basic_set *)bmap;
3792 isl_basic_map_free(bmap);
3796 /* For a div d = floor(f/m), add the constraints
3799 * -(f-(n-1)) + m d >= 0
3801 * Note that the second constraint is the negation of
3805 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3806 unsigned pos, isl_int *div)
3809 unsigned total = isl_basic_map_total_dim(bmap);
3811 i = isl_basic_map_alloc_inequality(bmap);
3814 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3815 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3817 j = isl_basic_map_alloc_inequality(bmap);
3820 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3821 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3822 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3826 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3827 unsigned pos, isl_int *div)
3829 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3833 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3835 unsigned total = isl_basic_map_total_dim(bmap);
3836 unsigned div_pos = total - bmap->n_div + div;
3838 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3842 struct isl_basic_set *isl_basic_map_underlying_set(
3843 struct isl_basic_map *bmap)
3847 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3849 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3850 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3851 return (struct isl_basic_set *)bmap;
3852 bmap = isl_basic_map_cow(bmap);
3855 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3858 bmap->extra -= bmap->n_div;
3860 bmap = isl_basic_map_finalize(bmap);
3861 return (struct isl_basic_set *)bmap;
3866 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3867 __isl_take isl_basic_set *bset)
3869 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3872 struct isl_basic_map *isl_basic_map_overlying_set(
3873 struct isl_basic_set *bset, struct isl_basic_map *like)
3875 struct isl_basic_map *bmap;
3876 struct isl_ctx *ctx;
3883 isl_assert(ctx, bset->n_div == 0, goto error);
3884 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3885 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3887 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3888 isl_basic_map_free(like);
3889 return (struct isl_basic_map *)bset;
3891 bset = isl_basic_set_cow(bset);
3894 total = bset->dim->n_out + bset->extra;
3895 bmap = (struct isl_basic_map *)bset;
3896 isl_space_free(bmap->dim);
3897 bmap->dim = isl_space_copy(like->dim);
3900 bmap->n_div = like->n_div;
3901 bmap->extra += like->n_div;
3905 ltotal = total - bmap->extra + like->extra;
3908 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3909 bmap->extra * (1 + 1 + total));
3910 if (isl_blk_is_error(bmap->block2))
3912 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3916 for (i = 0; i < bmap->extra; ++i)
3917 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3918 for (i = 0; i < like->n_div; ++i) {
3919 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3920 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3922 bmap = isl_basic_map_extend_constraints(bmap,
3923 0, 2 * like->n_div);
3924 for (i = 0; i < like->n_div; ++i) {
3925 if (isl_int_is_zero(bmap->div[i][0]))
3927 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3931 isl_basic_map_free(like);
3932 bmap = isl_basic_map_simplify(bmap);
3933 bmap = isl_basic_map_finalize(bmap);
3936 isl_basic_map_free(like);
3937 isl_basic_set_free(bset);
3941 struct isl_basic_set *isl_basic_set_from_underlying_set(
3942 struct isl_basic_set *bset, struct isl_basic_set *like)
3944 return (struct isl_basic_set *)
3945 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3948 struct isl_set *isl_set_from_underlying_set(
3949 struct isl_set *set, struct isl_basic_set *like)
3955 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3957 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
3958 isl_basic_set_free(like);
3961 set = isl_set_cow(set);
3964 for (i = 0; i < set->n; ++i) {
3965 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3966 isl_basic_set_copy(like));
3970 isl_space_free(set->dim);
3971 set->dim = isl_space_copy(like->dim);
3974 isl_basic_set_free(like);
3977 isl_basic_set_free(like);
3982 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3986 map = isl_map_cow(map);
3989 map->dim = isl_space_cow(map->dim);
3993 for (i = 1; i < map->n; ++i)
3994 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3996 for (i = 0; i < map->n; ++i) {
3997 map->p[i] = (struct isl_basic_map *)
3998 isl_basic_map_underlying_set(map->p[i]);
4003 map->dim = isl_space_underlying(map->dim, 0);
4005 isl_space_free(map->dim);
4006 map->dim = isl_space_copy(map->p[0]->dim);
4010 return (struct isl_set *)map;
4016 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4018 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4021 __isl_give isl_basic_map *isl_basic_map_reset_space(
4022 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4024 bmap = isl_basic_map_cow(bmap);
4028 isl_space_free(bmap->dim);
4031 bmap = isl_basic_map_finalize(bmap);
4035 isl_basic_map_free(bmap);
4036 isl_space_free(dim);
4040 __isl_give isl_basic_set *isl_basic_set_reset_space(
4041 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4043 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4047 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4048 __isl_take isl_space *dim)
4052 map = isl_map_cow(map);
4056 for (i = 0; i < map->n; ++i) {
4057 map->p[i] = isl_basic_map_reset_space(map->p[i],
4058 isl_space_copy(dim));
4062 isl_space_free(map->dim);
4068 isl_space_free(dim);
4072 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4073 __isl_take isl_space *dim)
4075 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4078 /* Compute the parameter domain of the given basic set.
4080 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4085 if (isl_basic_set_is_params(bset))
4088 n = isl_basic_set_dim(bset, isl_dim_set);
4089 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4090 space = isl_basic_set_get_space(bset);
4091 space = isl_space_params(space);
4092 bset = isl_basic_set_reset_space(bset, space);
4096 /* Compute the parameter domain of the given set.
4098 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4103 if (isl_set_is_params(set))
4106 n = isl_set_dim(set, isl_dim_set);
4107 set = isl_set_project_out(set, isl_dim_set, 0, n);
4108 space = isl_set_get_space(set);
4109 space = isl_space_params(space);
4110 set = isl_set_reset_space(set, space);
4114 /* Compute the parameter domain of the given map.
4116 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4121 n = isl_map_dim(map, isl_dim_in);
4122 map = isl_map_project_out(map, isl_dim_in, 0, n);
4123 n = isl_map_dim(map, isl_dim_out);
4124 map = isl_map_project_out(map, isl_dim_out, 0, n);
4125 space = isl_map_get_space(map);
4126 space = isl_space_params(space);
4127 map = isl_map_reset_space(map, space);
4131 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4134 struct isl_basic_set *domain;
4140 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4142 n_in = isl_basic_map_n_in(bmap);
4143 n_out = isl_basic_map_n_out(bmap);
4144 domain = isl_basic_set_from_basic_map(bmap);
4145 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4147 domain = isl_basic_set_reset_space(domain, dim);
4152 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4156 return isl_space_may_be_set(bmap->dim);
4159 /* Is this basic map actually a set?
4160 * Users should never call this function. Outside of isl,
4161 * the type should indicate whether something is a set or a map.
4163 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4167 return isl_space_is_set(bmap->dim);
4170 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4174 if (isl_basic_map_is_set(bmap))
4176 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4179 __isl_give isl_basic_map *isl_basic_map_domain_map(
4180 __isl_take isl_basic_map *bmap)
4184 isl_basic_map *domain;
4185 int nparam, n_in, n_out;
4188 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4189 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4190 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4192 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4193 domain = isl_basic_map_universe(dim);
4195 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4196 bmap = isl_basic_map_apply_range(bmap, domain);
4197 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4199 total = isl_basic_map_total_dim(bmap);
4201 for (i = 0; i < n_in; ++i) {
4202 k = isl_basic_map_alloc_equality(bmap);
4205 isl_seq_clr(bmap->eq[k], 1 + total);
4206 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4207 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4210 bmap = isl_basic_map_gauss(bmap, NULL);
4211 return isl_basic_map_finalize(bmap);
4213 isl_basic_map_free(bmap);
4217 __isl_give isl_basic_map *isl_basic_map_range_map(
4218 __isl_take isl_basic_map *bmap)
4222 isl_basic_map *range;
4223 int nparam, n_in, n_out;
4226 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4227 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4228 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4230 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4231 range = isl_basic_map_universe(dim);
4233 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4234 bmap = isl_basic_map_apply_range(bmap, range);
4235 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4237 total = isl_basic_map_total_dim(bmap);
4239 for (i = 0; i < n_out; ++i) {
4240 k = isl_basic_map_alloc_equality(bmap);
4243 isl_seq_clr(bmap->eq[k], 1 + total);
4244 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4245 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4248 bmap = isl_basic_map_gauss(bmap, NULL);
4249 return isl_basic_map_finalize(bmap);
4251 isl_basic_map_free(bmap);
4255 int isl_map_may_be_set(__isl_keep isl_map *map)
4259 return isl_space_may_be_set(map->dim);
4262 /* Is this map actually a set?
4263 * Users should never call this function. Outside of isl,
4264 * the type should indicate whether something is a set or a map.
4266 int isl_map_is_set(__isl_keep isl_map *map)
4270 return isl_space_is_set(map->dim);
4273 struct isl_set *isl_map_range(struct isl_map *map)
4276 struct isl_set *set;
4280 if (isl_map_is_set(map))
4281 return (isl_set *)map;
4283 map = isl_map_cow(map);
4287 set = (struct isl_set *) map;
4288 set->dim = isl_space_range(set->dim);
4291 for (i = 0; i < map->n; ++i) {
4292 set->p[i] = isl_basic_map_range(map->p[i]);
4296 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4297 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4304 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4307 isl_space *domain_dim;
4309 map = isl_map_cow(map);
4313 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4314 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4315 map->dim = isl_space_join(map->dim, domain_dim);
4318 for (i = 0; i < map->n; ++i) {
4319 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4323 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4324 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4331 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4334 isl_space *range_dim;
4336 map = isl_map_cow(map);
4340 range_dim = isl_space_range(isl_map_get_space(map));
4341 range_dim = isl_space_from_range(range_dim);
4342 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4343 map->dim = isl_space_join(map->dim, range_dim);
4346 for (i = 0; i < map->n; ++i) {
4347 map->p[i] = isl_basic_map_range_map(map->p[i]);
4351 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4352 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4359 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4360 __isl_take isl_space *dim)
4363 struct isl_map *map = NULL;
4365 set = isl_set_cow(set);
4368 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4369 map = (struct isl_map *)set;
4370 for (i = 0; i < set->n; ++i) {
4371 map->p[i] = isl_basic_map_from_basic_set(
4372 set->p[i], isl_space_copy(dim));
4376 isl_space_free(map->dim);
4380 isl_space_free(dim);
4385 __isl_give isl_basic_map *isl_basic_map_from_domain(
4386 __isl_take isl_basic_set *bset)
4388 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4391 __isl_give isl_basic_map *isl_basic_map_from_range(
4392 __isl_take isl_basic_set *bset)
4395 space = isl_basic_set_get_space(bset);
4396 space = isl_space_from_range(space);
4397 bset = isl_basic_set_reset_space(bset, space);
4398 return (isl_basic_map *)bset;
4401 struct isl_map *isl_map_from_range(struct isl_set *set)
4404 space = isl_set_get_space(set);
4405 space = isl_space_from_range(space);
4406 set = isl_set_reset_space(set, space);
4407 return (struct isl_map *)set;
4410 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4412 return isl_map_reverse(isl_map_from_range(set));
4415 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4416 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4418 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4421 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4422 __isl_take isl_set *range)
4424 return isl_map_apply_range(isl_map_reverse(domain), range);
4427 struct isl_set *isl_set_from_map(struct isl_map *map)
4430 struct isl_set *set = NULL;
4434 map = isl_map_cow(map);
4437 map->dim = isl_space_as_set_space(map->dim);
4440 set = (struct isl_set *)map;
4441 for (i = 0; i < map->n; ++i) {
4442 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4452 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4455 struct isl_map *map;
4459 isl_assert(dim->ctx, n >= 0, return NULL);
4460 map = isl_alloc(dim->ctx, struct isl_map,
4461 sizeof(struct isl_map) +
4462 (n - 1) * sizeof(struct isl_basic_map *));
4466 map->ctx = dim->ctx;
4467 isl_ctx_ref(map->ctx);
4475 isl_space_free(dim);
4479 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4480 unsigned nparam, unsigned in, unsigned out, int n,
4483 struct isl_map *map;
4486 dims = isl_space_alloc(ctx, nparam, in, out);
4490 map = isl_map_alloc_space(dims, n, flags);
4494 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4496 struct isl_basic_map *bmap;
4497 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4498 bmap = isl_basic_map_set_to_empty(bmap);
4502 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4504 struct isl_basic_set *bset;
4505 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4506 bset = isl_basic_set_set_to_empty(bset);
4510 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4512 struct isl_basic_map *bmap;
4515 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4516 bmap = isl_basic_map_set_to_empty(bmap);
4520 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4522 struct isl_basic_map *bmap;
4525 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4526 bmap = isl_basic_map_set_to_empty(bmap);
4530 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4532 struct isl_basic_set *bset;
4535 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4536 bset = isl_basic_set_set_to_empty(bset);
4540 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4542 struct isl_basic_map *bmap;
4543 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4544 bmap = isl_basic_map_finalize(bmap);
4548 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4550 struct isl_basic_set *bset;
4551 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4552 bset = isl_basic_set_finalize(bset);
4556 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4559 unsigned total = isl_space_dim(dim, isl_dim_all);
4560 isl_basic_map *bmap;
4562 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4563 for (i = 0; i < total; ++i) {
4564 int k = isl_basic_map_alloc_inequality(bmap);
4567 isl_seq_clr(bmap->ineq[k], 1 + total);
4568 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4572 isl_basic_map_free(bmap);
4576 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4578 return isl_basic_map_nat_universe(dim);
4581 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4583 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4586 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4588 return isl_map_nat_universe(dim);
4591 __isl_give isl_basic_map *isl_basic_map_universe_like(
4592 __isl_keep isl_basic_map *model)
4596 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4599 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4603 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4606 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4607 __isl_keep isl_set *model)
4611 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4614 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4616 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4619 struct isl_map *isl_map_empty_like(struct isl_map *model)
4623 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4626 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4630 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4633 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4635 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4638 struct isl_set *isl_set_empty_like(struct isl_set *model)
4642 return isl_set_empty(isl_space_copy(model->dim));
4645 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4647 struct isl_map *map;
4650 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4651 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4655 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4657 struct isl_set *set;
4660 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4661 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4665 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4669 return isl_set_universe(isl_space_copy(model->dim));
4672 struct isl_map *isl_map_dup(struct isl_map *map)
4675 struct isl_map *dup;
4679 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4680 for (i = 0; i < map->n; ++i)
4681 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4685 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4686 __isl_take isl_basic_map *bmap)
4690 if (isl_basic_map_plain_is_empty(bmap)) {
4691 isl_basic_map_free(bmap);
4694 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4695 isl_assert(map->ctx, map->n < map->size, goto error);
4696 map->p[map->n] = bmap;
4698 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4704 isl_basic_map_free(bmap);
4708 void isl_map_free(struct isl_map *map)
4718 isl_ctx_deref(map->ctx);
4719 for (i = 0; i < map->n; ++i)
4720 isl_basic_map_free(map->p[i]);
4721 isl_space_free(map->dim);
4725 struct isl_map *isl_map_extend(struct isl_map *base,
4726 unsigned nparam, unsigned n_in, unsigned n_out)
4730 base = isl_map_cow(base);
4734 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4737 for (i = 0; i < base->n; ++i) {
4738 base->p[i] = isl_basic_map_extend_space(base->p[i],
4739 isl_space_copy(base->dim), 0, 0, 0);
4749 struct isl_set *isl_set_extend(struct isl_set *base,
4750 unsigned nparam, unsigned dim)
4752 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4756 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4757 struct isl_basic_map *bmap, unsigned pos, int value)
4761 bmap = isl_basic_map_cow(bmap);
4762 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4763 j = isl_basic_map_alloc_equality(bmap);
4766 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4767 isl_int_set_si(bmap->eq[j][pos], -1);
4768 isl_int_set_si(bmap->eq[j][0], value);
4769 bmap = isl_basic_map_simplify(bmap);
4770 return isl_basic_map_finalize(bmap);
4772 isl_basic_map_free(bmap);
4776 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4777 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4781 bmap = isl_basic_map_cow(bmap);
4782 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4783 j = isl_basic_map_alloc_equality(bmap);
4786 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4787 isl_int_set_si(bmap->eq[j][pos], -1);
4788 isl_int_set(bmap->eq[j][0], value);
4789 bmap = isl_basic_map_simplify(bmap);
4790 return isl_basic_map_finalize(bmap);
4792 isl_basic_map_free(bmap);
4796 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4797 enum isl_dim_type type, unsigned pos, int value)
4801 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4802 return isl_basic_map_fix_pos_si(bmap,
4803 isl_basic_map_offset(bmap, type) + pos, value);
4805 isl_basic_map_free(bmap);
4809 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4810 enum isl_dim_type type, unsigned pos, isl_int value)
4814 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4815 return isl_basic_map_fix_pos(bmap,
4816 isl_basic_map_offset(bmap, type) + pos, value);
4818 isl_basic_map_free(bmap);
4822 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4823 enum isl_dim_type type, unsigned pos, int value)
4825 return (struct isl_basic_set *)
4826 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4830 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4831 enum isl_dim_type type, unsigned pos, isl_int value)
4833 return (struct isl_basic_set *)
4834 isl_basic_map_fix((struct isl_basic_map *)bset,
4838 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4839 unsigned input, int value)
4841 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4844 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4845 unsigned dim, int value)
4847 return (struct isl_basic_set *)
4848 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4849 isl_dim_set, dim, value);
4852 struct isl_map *isl_map_fix_si(struct isl_map *map,
4853 enum isl_dim_type type, unsigned pos, int value)
4857 map = isl_map_cow(map);
4861 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4862 for (i = 0; i < map->n; ++i) {
4863 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4867 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4874 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4875 enum isl_dim_type type, unsigned pos, int value)
4877 return (struct isl_set *)
4878 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4881 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4882 enum isl_dim_type type, unsigned pos, isl_int value)
4886 map = isl_map_cow(map);
4890 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4891 for (i = 0; i < map->n; ++i) {
4892 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4896 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4903 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4904 enum isl_dim_type type, unsigned pos, isl_int value)
4906 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4909 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4910 unsigned input, int value)
4912 return isl_map_fix_si(map, isl_dim_in, input, value);
4915 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4917 return (struct isl_set *)
4918 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4921 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4922 __isl_take isl_basic_map *bmap,
4923 enum isl_dim_type type, unsigned pos, int value)
4929 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4930 pos += isl_basic_map_offset(bmap, type);
4931 bmap = isl_basic_map_cow(bmap);
4932 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4933 j = isl_basic_map_alloc_inequality(bmap);
4936 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4937 isl_int_set_si(bmap->ineq[j][pos], 1);
4938 isl_int_set_si(bmap->ineq[j][0], -value);
4939 bmap = isl_basic_map_simplify(bmap);
4940 return isl_basic_map_finalize(bmap);
4942 isl_basic_map_free(bmap);
4946 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4947 unsigned dim, isl_int value)
4951 bset = isl_basic_set_cow(bset);
4952 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4953 j = isl_basic_set_alloc_inequality(bset);
4956 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4957 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4958 isl_int_neg(bset->ineq[j][0], value);
4959 bset = isl_basic_set_simplify(bset);
4960 return isl_basic_set_finalize(bset);
4962 isl_basic_set_free(bset);
4966 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4967 enum isl_dim_type type, unsigned pos, int value)
4971 map = isl_map_cow(map);
4975 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4976 for (i = 0; i < map->n; ++i) {
4977 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4982 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4989 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4990 enum isl_dim_type type, unsigned pos, int value)
4992 return (struct isl_set *)
4993 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4996 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5001 set = isl_set_cow(set);
5005 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5006 for (i = 0; i < set->n; ++i) {
5007 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5017 struct isl_map *isl_map_reverse(struct isl_map *map)
5021 map = isl_map_cow(map);
5025 map->dim = isl_space_reverse(map->dim);
5028 for (i = 0; i < map->n; ++i) {
5029 map->p[i] = isl_basic_map_reverse(map->p[i]);
5033 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5040 static struct isl_map *isl_basic_map_partial_lexopt(
5041 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5042 struct isl_set **empty, int max)
5046 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5047 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5049 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5051 isl_basic_map_free(bmap);
5052 isl_basic_set_free(dom);
5058 struct isl_map *isl_basic_map_partial_lexmax(
5059 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5060 struct isl_set **empty)
5062 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5065 struct isl_map *isl_basic_map_partial_lexmin(
5066 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5067 struct isl_set **empty)
5069 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5072 struct isl_set *isl_basic_set_partial_lexmin(
5073 struct isl_basic_set *bset, struct isl_basic_set *dom,
5074 struct isl_set **empty)
5076 return (struct isl_set *)
5077 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5081 struct isl_set *isl_basic_set_partial_lexmax(
5082 struct isl_basic_set *bset, struct isl_basic_set *dom,
5083 struct isl_set **empty)
5085 return (struct isl_set *)
5086 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5090 /* Given a basic map "bmap", compute the lexicographically minimal
5091 * (or maximal) image element for each domain element in dom.
5092 * Set *empty to those elements in dom that do not have an image element.
5094 * We first make sure the basic sets in dom are disjoint and then
5095 * simply collect the results over each of the basic sets separately.
5096 * We could probably improve the efficiency a bit by moving the union
5097 * domain down into the parametric integer programming.
5099 static __isl_give isl_map *basic_map_partial_lexopt(
5100 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5101 __isl_give isl_set **empty, int max)
5104 struct isl_map *res;
5106 dom = isl_set_make_disjoint(dom);
5110 if (isl_set_plain_is_empty(dom)) {
5111 res = isl_map_empty_like_basic_map(bmap);
5112 *empty = isl_set_empty_like(dom);
5114 isl_basic_map_free(bmap);
5118 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5119 isl_basic_set_copy(dom->p[0]), empty, max);
5121 for (i = 1; i < dom->n; ++i) {
5122 struct isl_map *res_i;
5123 struct isl_set *empty_i;
5125 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5126 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5128 res = isl_map_union_disjoint(res, res_i);
5129 *empty = isl_set_union_disjoint(*empty, empty_i);
5133 isl_basic_map_free(bmap);
5138 isl_basic_map_free(bmap);
5142 /* Given a map "map", compute the lexicographically minimal
5143 * (or maximal) image element for each domain element in dom.
5144 * Set *empty to those elements in dom that do not have an image element.
5146 * We first compute the lexicographically minimal or maximal element
5147 * in the first basic map. This results in a partial solution "res"
5148 * and a subset "todo" of dom that still need to be handled.
5149 * We then consider each of the remaining maps in "map" and successively
5150 * improve both "res" and "todo".
5152 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5153 * Assume we are computing the lexicographical maximum.
5154 * We first compute the lexicographically maximal element in basic map i.
5155 * This results in a partial solution res_i and a subset todo_i.
5156 * Then we combine these results with those obtain for the first k basic maps
5157 * to obtain a result that is valid for the first k+1 basic maps.
5158 * In particular, the set where there is no solution is the set where
5159 * there is no solution for the first k basic maps and also no solution
5160 * for the ith basic map, i.e.,
5162 * todo^i = todo^k * todo_i
5164 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5165 * solutions, arbitrarily breaking ties in favor of res^k.
5166 * That is, when res^k(a) >= res_i(a), we pick res^k and
5167 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5168 * the lexicographic order.)
5169 * In practice, we compute
5171 * res^k * (res_i . "<=")
5175 * res_i * (res^k . "<")
5177 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5178 * where only one of res^k and res_i provides a solution and we simply pick
5185 * Note that we only compute these intersections when dom(res^k) intersects
5186 * dom(res_i). Otherwise, the only effect of these intersections is to
5187 * potentially break up res^k and res_i into smaller pieces.
5188 * We want to avoid such splintering as much as possible.
5189 * In fact, an earlier implementation of this function would look for
5190 * better results in the domain of res^k and for extra results in todo^k,
5191 * but this would always result in a splintering according to todo^k,
5192 * even when the domain of basic map i is disjoint from the domains of
5193 * the previous basic maps.
5195 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5196 __isl_take isl_map *map, __isl_take isl_set *dom,
5197 __isl_give isl_set **empty, int max)
5200 struct isl_map *res;
5201 struct isl_set *todo;
5206 if (isl_map_plain_is_empty(map)) {
5214 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5215 isl_set_copy(dom), &todo, max);
5217 for (i = 1; i < map->n; ++i) {
5221 isl_space *dim = isl_space_range(isl_map_get_space(res));
5223 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5224 isl_set_copy(dom), &todo_i, max);
5227 lt = isl_map_lex_lt(isl_space_copy(dim));
5228 le = isl_map_lex_le(dim);
5230 lt = isl_map_lex_gt(isl_space_copy(dim));
5231 le = isl_map_lex_ge(dim);
5233 lt = isl_map_apply_range(isl_map_copy(res), lt);
5234 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5235 le = isl_map_apply_range(isl_map_copy(res_i), le);
5236 le = isl_map_intersect(le, isl_map_copy(res));
5238 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5239 res = isl_map_intersect_domain(res,
5240 isl_set_copy(todo_i));
5241 res_i = isl_map_intersect_domain(res_i,
5242 isl_set_copy(todo));
5245 res = isl_map_union_disjoint(res, res_i);
5246 res = isl_map_union_disjoint(res, lt);
5247 res = isl_map_union_disjoint(res, le);
5249 todo = isl_set_intersect(todo, todo_i);
5269 /* Given a map "map", compute the lexicographically minimal
5270 * (or maximal) image element for each domain element in dom.
5271 * Set *empty to those elements in dom that do not have an image element.
5273 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5275 static __isl_give isl_map *isl_map_partial_lexopt(
5276 __isl_take isl_map *map, __isl_take isl_set *dom,
5277 __isl_give isl_set **empty, int max)
5281 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5282 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5283 if (!isl_space_has_named_params(map->dim) ||
5284 !isl_space_has_named_params(dom->dim))
5285 isl_die(map->ctx, isl_error_invalid,
5286 "unaligned unnamed parameters", goto error);
5287 map = isl_map_align_params(map, isl_map_get_space(dom));
5288 dom = isl_map_align_params(dom, isl_map_get_space(map));
5289 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5298 __isl_give isl_map *isl_map_partial_lexmax(
5299 __isl_take isl_map *map, __isl_take isl_set *dom,
5300 __isl_give isl_set **empty)
5302 return isl_map_partial_lexopt(map, dom, empty, 1);
5305 __isl_give isl_map *isl_map_partial_lexmin(
5306 __isl_take isl_map *map, __isl_take isl_set *dom,
5307 __isl_give isl_set **empty)
5309 return isl_map_partial_lexopt(map, dom, empty, 0);
5312 __isl_give isl_set *isl_set_partial_lexmin(
5313 __isl_take isl_set *set, __isl_take isl_set *dom,
5314 __isl_give isl_set **empty)
5316 return (struct isl_set *)
5317 isl_map_partial_lexmin((struct isl_map *)set,
5321 __isl_give isl_set *isl_set_partial_lexmax(
5322 __isl_take isl_set *set, __isl_take isl_set *dom,
5323 __isl_give isl_set **empty)
5325 return (struct isl_set *)
5326 isl_map_partial_lexmax((struct isl_map *)set,
5330 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5332 struct isl_basic_set *dom = NULL;
5337 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5338 dom = isl_basic_set_universe(dom_dim);
5339 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5341 isl_basic_map_free(bmap);
5345 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5347 return isl_basic_map_lexopt(bmap, 0);
5350 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5352 return isl_basic_map_lexopt(bmap, 1);
5355 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5357 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5360 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5362 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5365 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5367 struct isl_set *dom = NULL;
5372 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5373 dom = isl_set_universe(dom_dim);
5374 return isl_map_partial_lexopt(map, dom, NULL, max);
5380 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5382 return isl_map_lexopt(map, 0);
5385 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5387 return isl_map_lexopt(map, 1);
5390 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5392 return (isl_set *)isl_map_lexmin((isl_map *)set);
5395 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5397 return (isl_set *)isl_map_lexmax((isl_map *)set);
5400 /* Construct a map that equates the two given dimensions in the given space.
5402 static __isl_give isl_map *equate(__isl_take isl_space *dim,
5403 enum isl_dim_type src_type, int src_pos,
5404 enum isl_dim_type dst_type, int dst_pos)
5406 isl_basic_map *bmap;
5409 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5410 k = isl_basic_map_alloc_equality(bmap);
5413 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
5414 src_pos += isl_basic_map_offset(bmap, src_type);
5415 dst_pos += isl_basic_map_offset(bmap, dst_type);
5416 isl_int_set_si(bmap->eq[k][src_pos], 1);
5417 isl_int_set_si(bmap->eq[k][dst_pos], -1);
5419 return isl_map_from_basic_map(bmap);
5421 isl_basic_map_free(bmap);
5425 /* Extract the first and only affine expression from list
5426 * and then add it to *pwaff with the given dom.
5427 * This domain is known to be disjoint from other domains
5428 * because of the way isl_basic_set_foreach_lexmax works.
5430 static int update_dim_opt(__isl_take isl_basic_set *dom,
5431 __isl_take isl_aff_list *list, void *user)
5433 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5435 isl_pw_aff **pwaff = user;
5436 isl_pw_aff *pwaff_i;
5438 if (isl_aff_list_n_aff(list) != 1)
5439 isl_die(ctx, isl_error_internal,
5440 "expecting single element list", goto error);
5442 aff = isl_aff_list_get_aff(list, 0);
5443 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5445 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5447 isl_aff_list_free(list);
5451 isl_basic_set_free(dom);
5452 isl_aff_list_free(list);
5456 /* Given a one-dimensional basic set, compute the minimum or maximum of that
5457 * dimension as an isl_pw_aff.
5459 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexopt
5460 * call update_dim_opt on each leaf of the result.
5462 static __isl_give isl_pw_aff *basic_set_dim_opt(__isl_keep isl_basic_set *bset,
5465 isl_space *dim = isl_basic_set_get_space(bset);
5469 dim = isl_space_params(dim);
5470 dim = isl_space_add_dims(dim, isl_dim_set, 1);
5471 pwaff = isl_pw_aff_empty(dim);
5473 r = isl_basic_set_foreach_lexopt(bset, max, &update_dim_opt, &pwaff);
5475 return isl_pw_aff_free(pwaff);
5480 /* Compute the minimum or maximum of the given set dimension
5481 * as a function of the parameters, but independently of
5482 * the other set dimensions.
5484 * We first project the set onto the given dimension and then compute
5485 * the "lexicographic" maximum in each basic set, combining the results
5486 * using isl_pw_aff_union_max.
5488 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5495 map = isl_map_from_domain(set);
5496 map = isl_map_add_dims(map, isl_dim_out, 1);
5497 map = isl_map_intersect(map,
5498 equate(isl_map_get_space(map), isl_dim_in, pos,
5500 set = isl_map_range(map);
5505 isl_space *dim = isl_set_get_space(set);
5506 dim = isl_space_domain(isl_space_from_range(dim));
5508 return isl_pw_aff_empty(dim);
5511 pwaff = basic_set_dim_opt(set->p[0], max);
5512 for (i = 1; i < set->n; ++i) {
5513 isl_pw_aff *pwaff_i;
5515 pwaff_i = basic_set_dim_opt(set->p[i], max);
5516 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5524 /* Compute the maximum of the given set dimension as a function of the
5525 * parameters, but independently of the other set dimensions.
5527 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5529 return set_dim_opt(set, pos, 1);
5532 /* Compute the minimum of the given set dimension as a function of the
5533 * parameters, but independently of the other set dimensions.
5535 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5537 return set_dim_opt(set, pos, 0);
5540 /* Apply a preimage specified by "mat" on the parameters of "bset".
5541 * bset is assumed to have only parameters and divs.
5543 static struct isl_basic_set *basic_set_parameter_preimage(
5544 struct isl_basic_set *bset, struct isl_mat *mat)
5551 bset->dim = isl_space_cow(bset->dim);
5555 nparam = isl_basic_set_dim(bset, isl_dim_param);
5557 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5559 bset->dim->nparam = 0;
5560 bset->dim->n_out = nparam;
5561 bset = isl_basic_set_preimage(bset, mat);
5563 bset->dim->nparam = bset->dim->n_out;
5564 bset->dim->n_out = 0;
5569 isl_basic_set_free(bset);
5573 /* Apply a preimage specified by "mat" on the parameters of "set".
5574 * set is assumed to have only parameters and divs.
5576 static struct isl_set *set_parameter_preimage(
5577 struct isl_set *set, struct isl_mat *mat)
5579 isl_space *dim = NULL;
5585 dim = isl_space_copy(set->dim);
5586 dim = isl_space_cow(dim);
5590 nparam = isl_set_dim(set, isl_dim_param);
5592 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5595 dim->n_out = nparam;
5596 isl_set_reset_space(set, dim);
5597 set = isl_set_preimage(set, mat);
5600 dim = isl_space_copy(set->dim);
5601 dim = isl_space_cow(dim);
5604 dim->nparam = dim->n_out;
5606 isl_set_reset_space(set, dim);
5609 isl_space_free(dim);
5616 /* Intersect the basic set "bset" with the affine space specified by the
5617 * equalities in "eq".
5619 static struct isl_basic_set *basic_set_append_equalities(
5620 struct isl_basic_set *bset, struct isl_mat *eq)
5628 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5633 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5634 for (i = 0; i < eq->n_row; ++i) {
5635 k = isl_basic_set_alloc_equality(bset);
5638 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5639 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5643 bset = isl_basic_set_gauss(bset, NULL);
5644 bset = isl_basic_set_finalize(bset);
5649 isl_basic_set_free(bset);
5653 /* Intersect the set "set" with the affine space specified by the
5654 * equalities in "eq".
5656 static struct isl_set *set_append_equalities(struct isl_set *set,
5664 for (i = 0; i < set->n; ++i) {
5665 set->p[i] = basic_set_append_equalities(set->p[i],
5678 /* Project the given basic set onto its parameter domain, possibly introducing
5679 * new, explicit, existential variables in the constraints.
5680 * The input has parameters and (possibly implicit) existential variables.
5681 * The output has the same parameters, but only
5682 * explicit existentially quantified variables.
5684 * The actual projection is performed by pip, but pip doesn't seem
5685 * to like equalities very much, so we first remove the equalities
5686 * among the parameters by performing a variable compression on
5687 * the parameters. Afterward, an inverse transformation is performed
5688 * and the equalities among the parameters are inserted back in.
5690 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5694 struct isl_mat *T, *T2;
5695 struct isl_set *set;
5696 unsigned nparam, n_div;
5698 bset = isl_basic_set_cow(bset);
5702 if (bset->n_eq == 0)
5703 return isl_basic_set_lexmin(bset);
5705 isl_basic_set_gauss(bset, NULL);
5707 nparam = isl_basic_set_dim(bset, isl_dim_param);
5708 n_div = isl_basic_set_dim(bset, isl_dim_div);
5710 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5711 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5714 if (i == bset->n_eq)
5715 return isl_basic_set_lexmin(bset);
5717 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5719 eq = isl_mat_cow(eq);
5720 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5721 if (T && T->n_col == 0) {
5725 bset = isl_basic_set_set_to_empty(bset);
5726 return isl_set_from_basic_set(bset);
5728 bset = basic_set_parameter_preimage(bset, T);
5730 set = isl_basic_set_lexmin(bset);
5731 set = set_parameter_preimage(set, T2);
5732 set = set_append_equalities(set, eq);
5736 /* Compute an explicit representation for all the existentially
5737 * quantified variables.
5738 * The input and output dimensions are first turned into parameters.
5739 * compute_divs then returns a map with the same parameters and
5740 * no input or output dimensions and the dimension specification
5741 * is reset to that of the input.
5743 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5745 struct isl_basic_set *bset;
5746 struct isl_set *set;
5747 struct isl_map *map;
5748 isl_space *dim, *orig_dim = NULL;
5753 bmap = isl_basic_map_cow(bmap);
5757 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5758 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5759 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5760 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5764 orig_dim = bmap->dim;
5766 bset = (struct isl_basic_set *)bmap;
5768 set = parameter_compute_divs(bset);
5769 map = (struct isl_map *)set;
5770 map = isl_map_reset_space(map, orig_dim);
5774 isl_basic_map_free(bmap);
5778 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5786 off = isl_space_dim(bmap->dim, isl_dim_all);
5787 for (i = 0; i < bmap->n_div; ++i) {
5788 if (isl_int_is_zero(bmap->div[i][0]))
5790 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5796 static int map_divs_known(__isl_keep isl_map *map)
5803 for (i = 0; i < map->n; ++i) {
5804 int known = isl_basic_map_divs_known(map->p[i]);
5812 /* If bmap contains any unknown divs, then compute explicit
5813 * expressions for them. However, this computation may be
5814 * quite expensive, so first try to remove divs that aren't
5817 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5820 struct isl_map *map;
5822 known = isl_basic_map_divs_known(bmap);
5826 return isl_map_from_basic_map(bmap);
5828 bmap = isl_basic_map_drop_redundant_divs(bmap);
5830 known = isl_basic_map_divs_known(bmap);
5834 return isl_map_from_basic_map(bmap);
5836 map = compute_divs(bmap);
5839 isl_basic_map_free(bmap);
5843 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5847 struct isl_map *res;
5854 known = map_divs_known(map);
5862 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5863 for (i = 1 ; i < map->n; ++i) {
5865 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5866 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5867 res = isl_map_union_disjoint(res, r2);
5869 res = isl_map_union(res, r2);
5876 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5878 return (struct isl_set *)
5879 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5882 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5884 return (struct isl_set *)
5885 isl_map_compute_divs((struct isl_map *)set);
5888 struct isl_set *isl_map_domain(struct isl_map *map)
5891 struct isl_set *set;
5896 map = isl_map_cow(map);
5900 set = (struct isl_set *)map;
5901 set->dim = isl_space_domain(set->dim);
5904 for (i = 0; i < map->n; ++i) {
5905 set->p[i] = isl_basic_map_domain(map->p[i]);
5909 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5910 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5917 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
5918 __isl_take isl_map *map2)
5922 struct isl_map *map = NULL;
5936 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
5938 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5939 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5940 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5942 map = isl_map_alloc_space(isl_space_copy(map1->dim),
5943 map1->n + map2->n, flags);
5946 for (i = 0; i < map1->n; ++i) {
5947 map = isl_map_add_basic_map(map,
5948 isl_basic_map_copy(map1->p[i]));
5952 for (i = 0; i < map2->n; ++i) {
5953 map = isl_map_add_basic_map(map,
5954 isl_basic_map_copy(map2->p[i]));
5968 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
5969 __isl_take isl_map *map2)
5971 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
5974 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5976 map1 = isl_map_union_disjoint(map1, map2);
5980 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5984 struct isl_set *isl_set_union_disjoint(
5985 struct isl_set *set1, struct isl_set *set2)
5987 return (struct isl_set *)
5988 isl_map_union_disjoint(
5989 (struct isl_map *)set1, (struct isl_map *)set2);
5992 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5994 return (struct isl_set *)
5995 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5998 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
5999 __isl_take isl_set *set)
6002 struct isl_map *result;
6008 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6009 isl_die(set->ctx, isl_error_invalid,
6010 "parameters don't match", goto error);
6012 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6013 !isl_map_compatible_range(map, set))
6014 isl_die(set->ctx, isl_error_invalid,
6015 "incompatible spaces", goto error);
6017 if (isl_set_plain_is_universe(set)) {
6022 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6023 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6024 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6026 result = isl_map_alloc_space(isl_space_copy(map->dim),
6027 map->n * set->n, flags);
6030 for (i = 0; i < map->n; ++i)
6031 for (j = 0; j < set->n; ++j) {
6032 result = isl_map_add_basic_map(result,
6033 isl_basic_map_intersect_range(
6034 isl_basic_map_copy(map->p[i]),
6035 isl_basic_set_copy(set->p[j])));
6048 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6049 __isl_take isl_set *set)
6051 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6054 struct isl_map *isl_map_intersect_domain(
6055 struct isl_map *map, struct isl_set *set)
6057 return isl_map_reverse(
6058 isl_map_intersect_range(isl_map_reverse(map), set));
6061 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6062 __isl_take isl_map *map2)
6066 map1 = isl_map_reverse(map1);
6067 map1 = isl_map_apply_range(map1, map2);
6068 return isl_map_reverse(map1);
6075 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6076 __isl_take isl_map *map2)
6078 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6081 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6082 __isl_take isl_map *map2)
6084 isl_space *dim_result;
6085 struct isl_map *result;
6091 dim_result = isl_space_join(isl_space_copy(map1->dim),
6092 isl_space_copy(map2->dim));
6094 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6097 for (i = 0; i < map1->n; ++i)
6098 for (j = 0; j < map2->n; ++j) {
6099 result = isl_map_add_basic_map(result,
6100 isl_basic_map_apply_range(
6101 isl_basic_map_copy(map1->p[i]),
6102 isl_basic_map_copy(map2->p[j])));
6108 if (result && result->n <= 1)
6109 ISL_F_SET(result, ISL_MAP_DISJOINT);
6117 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6118 __isl_take isl_map *map2)
6120 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6124 * returns range - domain
6126 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6128 isl_space *dims, *target_dim;
6129 struct isl_basic_set *bset;
6136 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6137 bmap->dim, isl_dim_out),
6139 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6140 dim = isl_basic_map_n_in(bmap);
6141 nparam = isl_basic_map_n_param(bmap);
6142 bset = isl_basic_set_from_basic_map(bmap);
6143 bset = isl_basic_set_cow(bset);
6144 dims = isl_basic_set_get_space(bset);
6145 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6146 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6147 bset = isl_basic_set_swap_vars(bset, 2*dim);
6148 for (i = 0; i < dim; ++i) {
6149 int j = isl_basic_map_alloc_equality(
6150 (struct isl_basic_map *)bset);
6153 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6154 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6155 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6156 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6158 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6159 bset = isl_basic_set_reset_space(bset, target_dim);
6162 isl_basic_map_free(bmap);
6167 * returns range - domain
6169 struct isl_set *isl_map_deltas(struct isl_map *map)
6173 struct isl_set *result;
6178 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6179 map->dim, isl_dim_out),
6181 dim = isl_map_get_space(map);
6182 dim = isl_space_domain(dim);
6183 result = isl_set_alloc_space(dim, map->n, 0);
6186 for (i = 0; i < map->n; ++i)
6187 result = isl_set_add_basic_set(result,
6188 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6197 * returns [domain -> range] -> range - domain
6199 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6200 __isl_take isl_basic_map *bmap)
6204 isl_basic_map *domain;
6208 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6209 isl_die(bmap->ctx, isl_error_invalid,
6210 "domain and range don't match", goto error);
6212 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6213 n = isl_basic_map_dim(bmap, isl_dim_in);
6215 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6216 domain = isl_basic_map_universe(dim);
6218 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6219 bmap = isl_basic_map_apply_range(bmap, domain);
6220 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6222 total = isl_basic_map_total_dim(bmap);
6224 for (i = 0; i < n; ++i) {
6225 k = isl_basic_map_alloc_equality(bmap);
6228 isl_seq_clr(bmap->eq[k], 1 + total);
6229 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6230 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6231 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6234 bmap = isl_basic_map_gauss(bmap, NULL);
6235 return isl_basic_map_finalize(bmap);
6237 isl_basic_map_free(bmap);
6242 * returns [domain -> range] -> range - domain
6244 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6247 isl_space *domain_dim;
6252 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6253 isl_die(map->ctx, isl_error_invalid,
6254 "domain and range don't match", goto error);
6256 map = isl_map_cow(map);
6260 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6261 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6262 map->dim = isl_space_join(map->dim, domain_dim);
6265 for (i = 0; i < map->n; ++i) {
6266 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6270 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6277 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6279 struct isl_basic_map *bmap;
6287 nparam = dims->nparam;
6289 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6293 for (i = 0; i < dim; ++i) {
6294 int j = isl_basic_map_alloc_equality(bmap);
6297 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6298 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6299 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6301 return isl_basic_map_finalize(bmap);
6303 isl_basic_map_free(bmap);
6307 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6311 if (dim->n_in != dim->n_out)
6312 isl_die(dim->ctx, isl_error_invalid,
6313 "number of input and output dimensions needs to be "
6314 "the same", goto error);
6315 return basic_map_identity(dim);
6317 isl_space_free(dim);
6321 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6323 if (!model || !model->dim)
6325 return isl_basic_map_identity(isl_space_copy(model->dim));
6328 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6330 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6333 struct isl_map *isl_map_identity_like(struct isl_map *model)
6335 if (!model || !model->dim)
6337 return isl_map_identity(isl_space_copy(model->dim));
6340 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6342 if (!model || !model->dim)
6344 return isl_map_identity(isl_space_copy(model->dim));
6347 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6349 isl_space *dim = isl_set_get_space(set);
6351 id = isl_map_identity(isl_space_map_from_set(dim));
6352 return isl_map_intersect_range(id, set);
6355 /* Construct a basic set with all set dimensions having only non-negative
6358 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6359 __isl_take isl_space *space)
6364 struct isl_basic_set *bset;
6368 nparam = space->nparam;
6370 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6373 for (i = 0; i < dim; ++i) {
6374 int k = isl_basic_set_alloc_inequality(bset);
6377 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6378 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6382 isl_basic_set_free(bset);
6386 /* Construct the half-space x_pos >= 0.
6388 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6392 isl_basic_set *nonneg;
6394 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6395 k = isl_basic_set_alloc_inequality(nonneg);
6398 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6399 isl_int_set_si(nonneg->ineq[k][pos], 1);
6401 return isl_basic_set_finalize(nonneg);
6403 isl_basic_set_free(nonneg);
6407 /* Construct the half-space x_pos <= -1.
6409 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6414 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6415 k = isl_basic_set_alloc_inequality(neg);
6418 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6419 isl_int_set_si(neg->ineq[k][0], -1);
6420 isl_int_set_si(neg->ineq[k][pos], -1);
6422 return isl_basic_set_finalize(neg);
6424 isl_basic_set_free(neg);
6428 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6429 enum isl_dim_type type, unsigned first, unsigned n)
6432 isl_basic_set *nonneg;
6440 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6442 for (i = 0; i < n; ++i) {
6443 nonneg = nonneg_halfspace(isl_set_get_space(set),
6444 pos(set->dim, type) + first + i);
6445 neg = neg_halfspace(isl_set_get_space(set),
6446 pos(set->dim, type) + first + i);
6448 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6457 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6458 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6465 if (isl_set_plain_is_empty(set)) {
6470 return fn(set, signs, user);
6473 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6475 half = isl_set_intersect(half, isl_set_copy(set));
6476 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6480 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6482 half = isl_set_intersect(half, set);
6483 return foreach_orthant(half, signs, first + 1, len, fn, user);
6489 /* Call "fn" on the intersections of "set" with each of the orthants
6490 * (except for obviously empty intersections). The orthant is identified
6491 * by the signs array, with each entry having value 1 or -1 according
6492 * to the sign of the corresponding variable.
6494 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6495 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6505 if (isl_set_plain_is_empty(set))
6508 nparam = isl_set_dim(set, isl_dim_param);
6509 nvar = isl_set_dim(set, isl_dim_set);
6511 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6513 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6521 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6523 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6526 int isl_basic_map_is_subset(
6527 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6530 struct isl_map *map1;
6531 struct isl_map *map2;
6533 if (!bmap1 || !bmap2)
6536 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6537 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6539 is_subset = isl_map_is_subset(map1, map2);
6547 int isl_basic_map_is_equal(
6548 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6552 if (!bmap1 || !bmap2)
6554 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6557 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6561 int isl_basic_set_is_equal(
6562 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6564 return isl_basic_map_is_equal(
6565 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6568 int isl_map_is_empty(struct isl_map *map)
6575 for (i = 0; i < map->n; ++i) {
6576 is_empty = isl_basic_map_is_empty(map->p[i]);
6585 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6587 return map ? map->n == 0 : -1;
6590 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6592 return isl_map_plain_is_empty(map);
6595 int isl_set_plain_is_empty(struct isl_set *set)
6597 return set ? set->n == 0 : -1;
6600 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6602 return isl_set_plain_is_empty(set);
6605 int isl_set_is_empty(struct isl_set *set)
6607 return isl_map_is_empty((struct isl_map *)set);
6610 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6615 return isl_space_is_equal(map1->dim, map2->dim);
6618 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6623 return isl_space_is_equal(set1->dim, set2->dim);
6626 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6632 is_subset = isl_map_is_subset(map1, map2);
6635 is_subset = isl_map_is_subset(map2, map1);
6639 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6641 return align_params_map_map_and_test(map1, map2, &map_is_equal);
6644 int isl_basic_map_is_strict_subset(
6645 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6649 if (!bmap1 || !bmap2)
6651 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6654 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6655 if (is_subset == -1)
6660 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6666 is_subset = isl_map_is_subset(map1, map2);
6669 is_subset = isl_map_is_subset(map2, map1);
6670 if (is_subset == -1)
6675 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6677 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6680 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6684 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6687 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6691 return bset->n_eq == 0 && bset->n_ineq == 0;
6694 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6701 for (i = 0; i < map->n; ++i) {
6702 int r = isl_basic_map_is_universe(map->p[i]);
6710 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6712 return isl_map_plain_is_universe((isl_map *) set);
6715 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6717 return isl_set_plain_is_universe(set);
6720 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6722 struct isl_basic_set *bset = NULL;
6723 struct isl_vec *sample = NULL;
6730 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6733 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6734 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6735 copy = isl_basic_map_remove_redundancies(copy);
6736 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6737 isl_basic_map_free(copy);
6741 total = 1 + isl_basic_map_total_dim(bmap);
6742 if (bmap->sample && bmap->sample->size == total) {
6743 int contains = isl_basic_map_contains(bmap, bmap->sample);
6749 isl_vec_free(bmap->sample);
6750 bmap->sample = NULL;
6751 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6754 sample = isl_basic_set_sample_vec(bset);
6757 empty = sample->size == 0;
6758 isl_vec_free(bmap->sample);
6759 bmap->sample = sample;
6761 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6766 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6770 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6773 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6775 return isl_basic_map_plain_is_empty(bmap);
6778 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6782 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6785 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6787 return isl_basic_set_plain_is_empty(bset);
6790 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6792 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6795 struct isl_map *isl_basic_map_union(
6796 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6798 struct isl_map *map;
6799 if (!bmap1 || !bmap2)
6802 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
6804 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
6807 map = isl_map_add_basic_map(map, bmap1);
6808 map = isl_map_add_basic_map(map, bmap2);
6811 isl_basic_map_free(bmap1);
6812 isl_basic_map_free(bmap2);
6816 struct isl_set *isl_basic_set_union(
6817 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6819 return (struct isl_set *)isl_basic_map_union(
6820 (struct isl_basic_map *)bset1,
6821 (struct isl_basic_map *)bset2);
6824 /* Order divs such that any div only depends on previous divs */
6825 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6833 off = isl_space_dim(bmap->dim, isl_dim_all);
6835 for (i = 0; i < bmap->n_div; ++i) {
6837 if (isl_int_is_zero(bmap->div[i][0]))
6839 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6843 isl_basic_map_swap_div(bmap, i, i + pos);
6849 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6851 return (struct isl_basic_set *)
6852 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6855 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6862 for (i = 0; i < map->n; ++i) {
6863 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6874 /* Apply the expansion computed by isl_merge_divs.
6875 * The expansion itself is given by "exp" while the resulting
6876 * list of divs is given by "div".
6878 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6879 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6884 bset = isl_basic_set_cow(bset);
6888 if (div->n_row < bset->n_div)
6889 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6890 "not an expansion", goto error);
6892 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
6893 div->n_row - bset->n_div, 0,
6894 2 * (div->n_row - bset->n_div));
6896 n_div = bset->n_div;
6897 for (i = n_div; i < div->n_row; ++i)
6898 if (isl_basic_set_alloc_div(bset) < 0)
6902 for (i = div->n_row - 1; i >= 0; --i) {
6903 if (j >= 0 && exp[j] == i) {
6905 isl_basic_map_swap_div(bset, i, j);
6908 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6909 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6917 isl_basic_set_free(bset);
6922 /* Look for a div in dst that corresponds to the div "div" in src.
6923 * The divs before "div" in src and dst are assumed to be the same.
6925 * Returns -1 if no corresponding div was found and the position
6926 * of the corresponding div in dst otherwise.
6928 static int find_div(struct isl_basic_map *dst,
6929 struct isl_basic_map *src, unsigned div)
6933 unsigned total = isl_space_dim(src->dim, isl_dim_all);
6935 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6936 for (i = div; i < dst->n_div; ++i)
6937 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6938 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6939 dst->n_div - div) == -1)
6944 struct isl_basic_map *isl_basic_map_align_divs(
6945 struct isl_basic_map *dst, struct isl_basic_map *src)
6948 unsigned total = isl_space_dim(src->dim, isl_dim_all);
6953 if (src->n_div == 0)
6956 for (i = 0; i < src->n_div; ++i)
6957 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6959 src = isl_basic_map_order_divs(src);
6960 dst = isl_basic_map_cow(dst);
6961 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
6962 src->n_div, 0, 2 * src->n_div);
6965 for (i = 0; i < src->n_div; ++i) {
6966 int j = find_div(dst, src, i);
6968 j = isl_basic_map_alloc_div(dst);
6971 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6972 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6973 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6977 isl_basic_map_swap_div(dst, i, j);
6981 isl_basic_map_free(dst);
6985 struct isl_basic_set *isl_basic_set_align_divs(
6986 struct isl_basic_set *dst, struct isl_basic_set *src)
6988 return (struct isl_basic_set *)isl_basic_map_align_divs(
6989 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6992 struct isl_map *isl_map_align_divs(struct isl_map *map)
7000 map = isl_map_compute_divs(map);
7001 map = isl_map_cow(map);
7005 for (i = 1; i < map->n; ++i)
7006 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7007 for (i = 1; i < map->n; ++i)
7008 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7010 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7014 struct isl_set *isl_set_align_divs(struct isl_set *set)
7016 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7019 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7020 __isl_take isl_map *map)
7024 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7025 map = isl_map_intersect_domain(map, set);
7026 set = isl_map_range(map);
7034 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7035 __isl_take isl_map *map)
7037 return isl_map_align_params_map_map_and(set, map, &set_apply);
7040 /* There is no need to cow as removing empty parts doesn't change
7041 * the meaning of the set.
7043 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7050 for (i = map->n-1; i >= 0; --i) {
7051 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
7053 isl_basic_map_free(map->p[i]);
7054 if (i != map->n-1) {
7055 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7056 map->p[i] = map->p[map->n-1];
7064 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7066 return (struct isl_set *)
7067 isl_map_remove_empty_parts((struct isl_map *)set);
7070 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7072 struct isl_basic_map *bmap;
7073 if (!map || map->n == 0)
7075 bmap = map->p[map->n-1];
7076 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7077 return isl_basic_map_copy(bmap);
7080 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7082 return (struct isl_basic_set *)
7083 isl_map_copy_basic_map((struct isl_map *)set);
7086 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7087 __isl_keep isl_basic_map *bmap)
7093 for (i = map->n-1; i >= 0; --i) {
7094 if (map->p[i] != bmap)
7096 map = isl_map_cow(map);
7099 isl_basic_map_free(map->p[i]);
7100 if (i != map->n-1) {
7101 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7102 map->p[i] = map->p[map->n-1];
7113 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7114 struct isl_basic_set *bset)
7116 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7117 (struct isl_basic_map *)bset);
7120 /* Given two basic sets bset1 and bset2, compute the maximal difference
7121 * between the values of dimension pos in bset1 and those in bset2
7122 * for any common value of the parameters and dimensions preceding pos.
7124 static enum isl_lp_result basic_set_maximal_difference_at(
7125 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7126 int pos, isl_int *opt)
7129 struct isl_basic_map *bmap1 = NULL;
7130 struct isl_basic_map *bmap2 = NULL;
7131 struct isl_ctx *ctx;
7132 struct isl_vec *obj;
7135 unsigned dim1, dim2;
7136 enum isl_lp_result res;
7138 if (!bset1 || !bset2)
7139 return isl_lp_error;
7141 nparam = isl_basic_set_n_param(bset1);
7142 dim1 = isl_basic_set_n_dim(bset1);
7143 dim2 = isl_basic_set_n_dim(bset2);
7144 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7145 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7146 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7147 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7148 if (!bmap1 || !bmap2)
7150 bmap1 = isl_basic_map_cow(bmap1);
7151 bmap1 = isl_basic_map_extend(bmap1, nparam,
7152 pos, (dim1 - pos) + (dim2 - pos),
7153 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7154 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7157 total = isl_basic_map_total_dim(bmap1);
7159 obj = isl_vec_alloc(ctx, 1 + total);
7160 isl_seq_clr(obj->block.data, 1 + total);
7161 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7162 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7165 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7167 isl_basic_map_free(bmap1);
7171 isl_basic_map_free(bmap1);
7172 isl_basic_map_free(bmap2);
7173 return isl_lp_error;
7176 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7177 * for any common value of the parameters and dimensions preceding pos
7178 * in both basic sets, the values of dimension pos in bset1 are
7179 * smaller or larger than those in bset2.
7182 * 1 if bset1 follows bset2
7183 * -1 if bset1 precedes bset2
7184 * 0 if bset1 and bset2 are incomparable
7185 * -2 if some error occurred.
7187 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7188 struct isl_basic_set *bset2, int pos)
7191 enum isl_lp_result res;
7196 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7198 if (res == isl_lp_empty)
7200 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7201 res == isl_lp_unbounded)
7203 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7212 /* Given two basic sets bset1 and bset2, check whether
7213 * for any common value of the parameters and dimensions preceding pos
7214 * there is a value of dimension pos in bset1 that is larger
7215 * than a value of the same dimension in bset2.
7218 * 1 if there exists such a pair
7219 * 0 if there is no such pair, but there is a pair of equal values
7221 * -2 if some error occurred.
7223 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7224 __isl_keep isl_basic_set *bset2, int pos)
7227 enum isl_lp_result res;
7232 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7234 if (res == isl_lp_empty)
7236 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7237 res == isl_lp_unbounded)
7239 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7241 else if (res == isl_lp_ok)
7250 /* Given two sets set1 and set2, check whether
7251 * for any common value of the parameters and dimensions preceding pos
7252 * there is a value of dimension pos in set1 that is larger
7253 * than a value of the same dimension in set2.
7256 * 1 if there exists such a pair
7257 * 0 if there is no such pair, but there is a pair of equal values
7259 * -2 if some error occurred.
7261 int isl_set_follows_at(__isl_keep isl_set *set1,
7262 __isl_keep isl_set *set2, int pos)
7270 for (i = 0; i < set1->n; ++i)
7271 for (j = 0; j < set2->n; ++j) {
7273 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7274 if (f == 1 || f == -2)
7283 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7284 unsigned pos, isl_int *val)
7292 total = isl_basic_map_total_dim(bmap);
7293 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7294 for (; d+1 > pos; --d)
7295 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7299 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7301 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7303 if (!isl_int_is_one(bmap->eq[i][1+d]))
7306 isl_int_neg(*val, bmap->eq[i][0]);
7312 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7313 unsigned pos, isl_int *val)
7325 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7328 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7329 for (i = 1; fixed == 1 && i < map->n; ++i) {
7330 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7331 if (fixed == 1 && isl_int_ne(tmp, v))
7335 isl_int_set(*val, v);
7341 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7342 unsigned pos, isl_int *val)
7344 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7348 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7351 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7354 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7355 enum isl_dim_type type, unsigned pos, isl_int *val)
7357 if (pos >= isl_basic_map_dim(bmap, type))
7359 return isl_basic_map_plain_has_fixed_var(bmap,
7360 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7363 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7364 enum isl_dim_type type, unsigned pos, isl_int *val)
7366 if (pos >= isl_map_dim(map, type))
7368 return isl_map_plain_has_fixed_var(map,
7369 map_offset(map, type) - 1 + pos, val);
7372 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7373 enum isl_dim_type type, unsigned pos, isl_int *val)
7375 return isl_map_plain_is_fixed(map, type, pos, val);
7378 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7379 * then return this fixed value in *val.
7381 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7382 unsigned dim, isl_int *val)
7384 return isl_basic_set_plain_has_fixed_var(bset,
7385 isl_basic_set_n_param(bset) + dim, val);
7388 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7389 * then return this fixed value in *val.
7391 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7392 unsigned dim, isl_int *val)
7394 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7397 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7398 unsigned dim, isl_int *val)
7400 return isl_set_plain_dim_is_fixed(set, dim, val);
7403 /* Check if input variable in has fixed value and if so and if val is not NULL,
7404 * then return this fixed value in *val.
7406 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7407 unsigned in, isl_int *val)
7409 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7412 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7413 * and if val is not NULL, then return this lower bound in *val.
7415 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7416 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7418 int i, i_eq = -1, i_ineq = -1;
7425 total = isl_basic_set_total_dim(bset);
7426 nparam = isl_basic_set_n_param(bset);
7427 for (i = 0; i < bset->n_eq; ++i) {
7428 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7434 for (i = 0; i < bset->n_ineq; ++i) {
7435 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7437 if (i_eq != -1 || i_ineq != -1)
7441 if (i_eq == -1 && i_ineq == -1)
7443 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7444 /* The coefficient should always be one due to normalization. */
7445 if (!isl_int_is_one(c[1+nparam+dim]))
7447 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7449 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7450 total - nparam - dim - 1) != -1)
7453 isl_int_neg(*val, c[0]);
7457 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7458 unsigned dim, isl_int *val)
7470 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7474 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7476 for (i = 1; fixed == 1 && i < set->n; ++i) {
7477 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7479 if (fixed == 1 && isl_int_ne(tmp, v))
7483 isl_int_set(*val, v);
7494 /* uset_gist depends on constraints without existentially quantified
7495 * variables sorting first.
7497 static int qsort_constraint_cmp(const void *p1, const void *p2)
7499 const struct constraint *c1 = (const struct constraint *)p1;
7500 const struct constraint *c2 = (const struct constraint *)p2;
7502 unsigned size = isl_min(c1->size, c2->size);
7504 l1 = isl_seq_last_non_zero(c1->c, size);
7505 l2 = isl_seq_last_non_zero(c2->c, size);
7510 return isl_seq_cmp(c1->c, c2->c, size);
7513 static struct isl_basic_map *isl_basic_map_sort_constraints(
7514 struct isl_basic_map *bmap)
7517 struct constraint *c;
7522 total = isl_basic_map_total_dim(bmap);
7523 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7526 for (i = 0; i < bmap->n_ineq; ++i) {
7528 c[i].c = bmap->ineq[i];
7530 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7531 for (i = 0; i < bmap->n_ineq; ++i)
7532 bmap->ineq[i] = c[i].c;
7536 isl_basic_map_free(bmap);
7540 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7541 __isl_take isl_basic_set *bset)
7543 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7544 (struct isl_basic_map *)bset);
7547 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7551 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7553 bmap = isl_basic_map_remove_redundancies(bmap);
7554 bmap = isl_basic_map_sort_constraints(bmap);
7555 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7559 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7561 return (struct isl_basic_set *)isl_basic_map_normalize(
7562 (struct isl_basic_map *)bset);
7565 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7566 const __isl_keep isl_basic_map *bmap2)
7573 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7574 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7575 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7576 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7577 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7578 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7579 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7580 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7581 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7582 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7583 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7585 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7587 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7589 if (bmap1->n_eq != bmap2->n_eq)
7590 return bmap1->n_eq - bmap2->n_eq;
7591 if (bmap1->n_ineq != bmap2->n_ineq)
7592 return bmap1->n_ineq - bmap2->n_ineq;
7593 if (bmap1->n_div != bmap2->n_div)
7594 return bmap1->n_div - bmap2->n_div;
7595 total = isl_basic_map_total_dim(bmap1);
7596 for (i = 0; i < bmap1->n_eq; ++i) {
7597 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7601 for (i = 0; i < bmap1->n_ineq; ++i) {
7602 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7606 for (i = 0; i < bmap1->n_div; ++i) {
7607 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7614 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
7615 const __isl_keep isl_basic_set *bset2)
7617 return isl_basic_map_plain_cmp(bset1, bset2);
7620 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
7621 const __isl_keep isl_set *set2)
7627 if (set1->n != set2->n)
7628 return set1->n - set2->n;
7630 for (i = 0; i < set1->n; ++i) {
7631 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
7639 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7640 __isl_keep isl_basic_map *bmap2)
7642 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7645 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7646 __isl_keep isl_basic_set *bset2)
7648 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7649 (isl_basic_map *)bset2);
7652 static int qsort_bmap_cmp(const void *p1, const void *p2)
7654 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7655 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7657 return isl_basic_map_plain_cmp(bmap1, bmap2);
7660 /* We normalize in place, but if anything goes wrong we need
7661 * to return NULL, so we need to make sure we don't change the
7662 * meaning of any possible other copies of map.
7664 struct isl_map *isl_map_normalize(struct isl_map *map)
7667 struct isl_basic_map *bmap;
7671 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7673 for (i = 0; i < map->n; ++i) {
7674 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7677 isl_basic_map_free(map->p[i]);
7680 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7681 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7682 map = isl_map_remove_empty_parts(map);
7685 for (i = map->n - 1; i >= 1; --i) {
7686 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7688 isl_basic_map_free(map->p[i-1]);
7689 for (j = i; j < map->n; ++j)
7690 map->p[j-1] = map->p[j];
7700 struct isl_set *isl_set_normalize(struct isl_set *set)
7702 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7705 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7715 if (!isl_space_is_equal(map1->dim, map2->dim))
7718 map1 = isl_map_copy(map1);
7719 map2 = isl_map_copy(map2);
7720 map1 = isl_map_normalize(map1);
7721 map2 = isl_map_normalize(map2);
7724 equal = map1->n == map2->n;
7725 for (i = 0; equal && i < map1->n; ++i) {
7726 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7739 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7741 return isl_map_plain_is_equal(map1, map2);
7744 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7746 return isl_map_plain_is_equal((struct isl_map *)set1,
7747 (struct isl_map *)set2);
7750 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7752 return isl_set_plain_is_equal(set1, set2);
7755 /* Return an interval that ranges from min to max (inclusive)
7757 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7758 isl_int min, isl_int max)
7761 struct isl_basic_set *bset = NULL;
7763 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7767 k = isl_basic_set_alloc_inequality(bset);
7770 isl_int_set_si(bset->ineq[k][1], 1);
7771 isl_int_neg(bset->ineq[k][0], min);
7773 k = isl_basic_set_alloc_inequality(bset);
7776 isl_int_set_si(bset->ineq[k][1], -1);
7777 isl_int_set(bset->ineq[k][0], max);
7781 isl_basic_set_free(bset);
7785 /* Return the Cartesian product of the basic sets in list (in the given order).
7787 __isl_give isl_basic_set *isl_basic_set_list_product(
7788 __isl_take struct isl_basic_set_list *list)
7796 struct isl_basic_set *product = NULL;
7800 isl_assert(list->ctx, list->n > 0, goto error);
7801 isl_assert(list->ctx, list->p[0], goto error);
7802 nparam = isl_basic_set_n_param(list->p[0]);
7803 dim = isl_basic_set_n_dim(list->p[0]);
7804 extra = list->p[0]->n_div;
7805 n_eq = list->p[0]->n_eq;
7806 n_ineq = list->p[0]->n_ineq;
7807 for (i = 1; i < list->n; ++i) {
7808 isl_assert(list->ctx, list->p[i], goto error);
7809 isl_assert(list->ctx,
7810 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7811 dim += isl_basic_set_n_dim(list->p[i]);
7812 extra += list->p[i]->n_div;
7813 n_eq += list->p[i]->n_eq;
7814 n_ineq += list->p[i]->n_ineq;
7816 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7821 for (i = 0; i < list->n; ++i) {
7822 isl_basic_set_add_constraints(product,
7823 isl_basic_set_copy(list->p[i]), dim);
7824 dim += isl_basic_set_n_dim(list->p[i]);
7826 isl_basic_set_list_free(list);
7829 isl_basic_set_free(product);
7830 isl_basic_set_list_free(list);
7834 struct isl_basic_map *isl_basic_map_product(
7835 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7837 isl_space *dim_result = NULL;
7838 struct isl_basic_map *bmap;
7839 unsigned in1, in2, out1, out2, nparam, total, pos;
7840 struct isl_dim_map *dim_map1, *dim_map2;
7842 if (!bmap1 || !bmap2)
7845 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
7846 bmap2->dim, isl_dim_param), goto error);
7847 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
7848 isl_space_copy(bmap2->dim));
7850 in1 = isl_basic_map_n_in(bmap1);
7851 in2 = isl_basic_map_n_in(bmap2);
7852 out1 = isl_basic_map_n_out(bmap1);
7853 out2 = isl_basic_map_n_out(bmap2);
7854 nparam = isl_basic_map_n_param(bmap1);
7856 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7857 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7858 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7859 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7860 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7861 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7862 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7863 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7864 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7865 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7866 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7868 bmap = isl_basic_map_alloc_space(dim_result,
7869 bmap1->n_div + bmap2->n_div,
7870 bmap1->n_eq + bmap2->n_eq,
7871 bmap1->n_ineq + bmap2->n_ineq);
7872 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7873 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7874 bmap = isl_basic_map_simplify(bmap);
7875 return isl_basic_map_finalize(bmap);
7877 isl_basic_map_free(bmap1);
7878 isl_basic_map_free(bmap2);
7882 __isl_give isl_basic_map *isl_basic_map_flat_product(
7883 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7885 isl_basic_map *prod;
7887 prod = isl_basic_map_product(bmap1, bmap2);
7888 prod = isl_basic_map_flatten(prod);
7892 __isl_give isl_basic_set *isl_basic_set_flat_product(
7893 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7895 return isl_basic_map_flat_range_product(bset1, bset2);
7898 __isl_give isl_basic_map *isl_basic_map_domain_product(
7899 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7901 isl_space *space_result = NULL;
7902 isl_basic_map *bmap;
7903 unsigned in1, in2, out, nparam, total, pos;
7904 struct isl_dim_map *dim_map1, *dim_map2;
7906 if (!bmap1 || !bmap2)
7909 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
7910 isl_space_copy(bmap2->dim));
7912 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
7913 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
7914 out = isl_basic_map_dim(bmap1, isl_dim_out);
7915 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
7917 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
7918 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7919 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7920 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7921 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7922 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7923 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7924 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7925 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
7926 isl_dim_map_div(dim_map1, bmap1, pos += out);
7927 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7929 bmap = isl_basic_map_alloc_space(space_result,
7930 bmap1->n_div + bmap2->n_div,
7931 bmap1->n_eq + bmap2->n_eq,
7932 bmap1->n_ineq + bmap2->n_ineq);
7933 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7934 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7935 bmap = isl_basic_map_simplify(bmap);
7936 return isl_basic_map_finalize(bmap);
7938 isl_basic_map_free(bmap1);
7939 isl_basic_map_free(bmap2);
7943 __isl_give isl_basic_map *isl_basic_map_range_product(
7944 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7946 isl_space *dim_result = NULL;
7947 isl_basic_map *bmap;
7948 unsigned in, out1, out2, nparam, total, pos;
7949 struct isl_dim_map *dim_map1, *dim_map2;
7951 if (!bmap1 || !bmap2)
7954 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
7955 isl_space_copy(bmap2->dim));
7957 in = isl_basic_map_dim(bmap1, isl_dim_in);
7958 out1 = isl_basic_map_n_out(bmap1);
7959 out2 = isl_basic_map_n_out(bmap2);
7960 nparam = isl_basic_map_n_param(bmap1);
7962 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7963 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7964 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7965 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7966 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7967 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7968 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7969 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7970 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7971 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7972 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7974 bmap = isl_basic_map_alloc_space(dim_result,
7975 bmap1->n_div + bmap2->n_div,
7976 bmap1->n_eq + bmap2->n_eq,
7977 bmap1->n_ineq + bmap2->n_ineq);
7978 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7979 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7980 bmap = isl_basic_map_simplify(bmap);
7981 return isl_basic_map_finalize(bmap);
7983 isl_basic_map_free(bmap1);
7984 isl_basic_map_free(bmap2);
7988 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
7989 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7991 isl_basic_map *prod;
7993 prod = isl_basic_map_range_product(bmap1, bmap2);
7994 prod = isl_basic_map_flatten_range(prod);
7998 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7999 __isl_take isl_map *map2,
8000 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8001 __isl_take isl_space *right),
8002 __isl_give isl_basic_map *(*basic_map_product)(
8003 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8006 struct isl_map *result;
8012 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8013 map2->dim, isl_dim_param), goto error);
8015 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8016 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8017 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8019 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8020 isl_space_copy(map2->dim)),
8021 map1->n * map2->n, flags);
8024 for (i = 0; i < map1->n; ++i)
8025 for (j = 0; j < map2->n; ++j) {
8026 struct isl_basic_map *part;
8027 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8028 isl_basic_map_copy(map2->p[j]));
8029 if (isl_basic_map_is_empty(part))
8030 isl_basic_map_free(part);
8032 result = isl_map_add_basic_map(result, part);
8045 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8047 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8048 __isl_take isl_map *map2)
8050 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8053 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8054 __isl_take isl_map *map2)
8056 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8059 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8061 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8062 __isl_take isl_map *map2)
8066 prod = isl_map_product(map1, map2);
8067 prod = isl_map_flatten(prod);
8071 /* Given two set A and B, construct its Cartesian product A x B.
8073 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8075 return isl_map_range_product(set1, set2);
8078 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8079 __isl_take isl_set *set2)
8081 return isl_map_flat_range_product(set1, set2);
8084 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8086 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8087 __isl_take isl_map *map2)
8089 return map_product(map1, map2, &isl_space_domain_product,
8090 &isl_basic_map_domain_product);
8093 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8095 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8096 __isl_take isl_map *map2)
8098 return map_product(map1, map2, &isl_space_range_product,
8099 &isl_basic_map_range_product);
8102 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8103 __isl_take isl_map *map2)
8105 return isl_map_align_params_map_map_and(map1, map2,
8106 &map_domain_product_aligned);
8109 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8110 __isl_take isl_map *map2)
8112 return isl_map_align_params_map_map_and(map1, map2,
8113 &map_range_product_aligned);
8116 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8118 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8119 __isl_take isl_map *map2)
8123 prod = isl_map_domain_product(map1, map2);
8124 prod = isl_map_flatten_domain(prod);
8128 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8130 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8131 __isl_take isl_map *map2)
8135 prod = isl_map_range_product(map1, map2);
8136 prod = isl_map_flatten_range(prod);
8140 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8143 uint32_t hash = isl_hash_init();
8148 bmap = isl_basic_map_copy(bmap);
8149 bmap = isl_basic_map_normalize(bmap);
8152 total = isl_basic_map_total_dim(bmap);
8153 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8154 for (i = 0; i < bmap->n_eq; ++i) {
8156 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8157 isl_hash_hash(hash, c_hash);
8159 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8160 for (i = 0; i < bmap->n_ineq; ++i) {
8162 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8163 isl_hash_hash(hash, c_hash);
8165 isl_hash_byte(hash, bmap->n_div & 0xFF);
8166 for (i = 0; i < bmap->n_div; ++i) {
8168 if (isl_int_is_zero(bmap->div[i][0]))
8170 isl_hash_byte(hash, i & 0xFF);
8171 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8172 isl_hash_hash(hash, c_hash);
8174 isl_basic_map_free(bmap);
8178 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8180 return isl_basic_map_get_hash((isl_basic_map *)bset);
8183 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8190 map = isl_map_copy(map);
8191 map = isl_map_normalize(map);
8195 hash = isl_hash_init();
8196 for (i = 0; i < map->n; ++i) {
8198 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8199 isl_hash_hash(hash, bmap_hash);
8207 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8209 return isl_map_get_hash((isl_map *)set);
8212 /* Check if the value for dimension dim is completely determined
8213 * by the values of the other parameters and variables.
8214 * That is, check if dimension dim is involved in an equality.
8216 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8223 nparam = isl_basic_set_n_param(bset);
8224 for (i = 0; i < bset->n_eq; ++i)
8225 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8230 /* Check if the value for dimension dim is completely determined
8231 * by the values of the other parameters and variables.
8232 * That is, check if dimension dim is involved in an equality
8233 * for each of the subsets.
8235 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8241 for (i = 0; i < set->n; ++i) {
8243 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8250 int isl_set_n_basic_set(__isl_keep isl_set *set)
8252 return set ? set->n : 0;
8255 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8256 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8263 for (i = 0; i < map->n; ++i)
8264 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8270 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8271 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8278 for (i = 0; i < set->n; ++i)
8279 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8285 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8292 bset = isl_basic_set_cow(bset);
8296 dim = isl_basic_set_get_space(bset);
8297 dim = isl_space_lift(dim, bset->n_div);
8300 isl_space_free(bset->dim);
8302 bset->extra -= bset->n_div;
8305 bset = isl_basic_set_finalize(bset);
8309 isl_basic_set_free(bset);
8313 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8319 set = isl_set_align_divs(set);
8324 set = isl_set_cow(set);
8328 n_div = set->p[0]->n_div;
8329 dim = isl_set_get_space(set);
8330 dim = isl_space_lift(dim, n_div);
8333 isl_space_free(set->dim);
8336 for (i = 0; i < set->n; ++i) {
8337 set->p[i] = isl_basic_set_lift(set->p[i]);
8348 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8351 struct isl_basic_map *bmap;
8358 set = isl_set_align_divs(set);
8363 dim = isl_set_get_space(set);
8364 if (set->n == 0 || set->p[0]->n_div == 0) {
8366 return isl_map_identity(isl_space_map_from_set(dim));
8369 n_div = set->p[0]->n_div;
8370 dim = isl_space_map_from_set(dim);
8371 n_param = isl_space_dim(dim, isl_dim_param);
8372 n_set = isl_space_dim(dim, isl_dim_in);
8373 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8374 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8375 for (i = 0; i < n_set; ++i)
8376 bmap = var_equal(bmap, i);
8378 total = n_param + n_set + n_set + n_div;
8379 for (i = 0; i < n_div; ++i) {
8380 k = isl_basic_map_alloc_inequality(bmap);
8383 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8384 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8385 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8386 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8387 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8388 set->p[0]->div[i][0]);
8390 l = isl_basic_map_alloc_inequality(bmap);
8393 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8394 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8395 set->p[0]->div[i][0]);
8396 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8400 bmap = isl_basic_map_simplify(bmap);
8401 bmap = isl_basic_map_finalize(bmap);
8402 return isl_map_from_basic_map(bmap);
8405 isl_basic_map_free(bmap);
8409 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8417 dim = isl_basic_set_total_dim(bset);
8418 size += bset->n_eq * (1 + dim);
8419 size += bset->n_ineq * (1 + dim);
8420 size += bset->n_div * (2 + dim);
8425 int isl_set_size(__isl_keep isl_set *set)
8433 for (i = 0; i < set->n; ++i)
8434 size += isl_basic_set_size(set->p[i]);
8439 /* Check if there is any lower bound (if lower == 0) and/or upper
8440 * bound (if upper == 0) on the specified dim.
8442 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8443 enum isl_dim_type type, unsigned pos, int lower, int upper)
8450 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8452 pos += isl_basic_map_offset(bmap, type);
8454 for (i = 0; i < bmap->n_div; ++i) {
8455 if (isl_int_is_zero(bmap->div[i][0]))
8457 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8461 for (i = 0; i < bmap->n_eq; ++i)
8462 if (!isl_int_is_zero(bmap->eq[i][pos]))
8465 for (i = 0; i < bmap->n_ineq; ++i) {
8466 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8473 return lower && upper;
8476 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8477 enum isl_dim_type type, unsigned pos)
8479 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8482 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8483 enum isl_dim_type type, unsigned pos)
8485 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8488 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8489 enum isl_dim_type type, unsigned pos)
8491 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8494 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8495 enum isl_dim_type type, unsigned pos)
8502 for (i = 0; i < map->n; ++i) {
8504 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8505 if (bounded < 0 || !bounded)
8512 /* Return 1 if the specified dim is involved in both an upper bound
8513 * and a lower bound.
8515 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8516 enum isl_dim_type type, unsigned pos)
8518 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8521 static int has_bound(__isl_keep isl_map *map,
8522 enum isl_dim_type type, unsigned pos,
8523 int (*fn)(__isl_keep isl_basic_map *bmap,
8524 enum isl_dim_type type, unsigned pos))
8531 for (i = 0; i < map->n; ++i) {
8533 bounded = fn(map->p[i], type, pos);
8534 if (bounded < 0 || bounded)
8541 /* Return 1 if the specified dim is involved in any lower bound.
8543 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8544 enum isl_dim_type type, unsigned pos)
8546 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8549 /* Return 1 if the specified dim is involved in any upper bound.
8551 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8552 enum isl_dim_type type, unsigned pos)
8554 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8557 /* For each of the "n" variables starting at "first", determine
8558 * the sign of the variable and put the results in the first "n"
8559 * elements of the array "signs".
8561 * 1 means that the variable is non-negative
8562 * -1 means that the variable is non-positive
8563 * 0 means the variable attains both positive and negative values.
8565 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8566 unsigned first, unsigned n, int *signs)
8568 isl_vec *bound = NULL;
8569 struct isl_tab *tab = NULL;
8570 struct isl_tab_undo *snap;
8573 if (!bset || !signs)
8576 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8577 tab = isl_tab_from_basic_set(bset);
8581 isl_seq_clr(bound->el, bound->size);
8582 isl_int_set_si(bound->el[0], -1);
8584 snap = isl_tab_snap(tab);
8585 for (i = 0; i < n; ++i) {
8588 isl_int_set_si(bound->el[1 + first + i], -1);
8589 if (isl_tab_add_ineq(tab, bound->el) < 0)
8592 isl_int_set_si(bound->el[1 + first + i], 0);
8593 if (isl_tab_rollback(tab, snap) < 0)
8601 isl_int_set_si(bound->el[1 + first + i], 1);
8602 if (isl_tab_add_ineq(tab, bound->el) < 0)
8605 isl_int_set_si(bound->el[1 + first + i], 0);
8606 if (isl_tab_rollback(tab, snap) < 0)
8609 signs[i] = empty ? -1 : 0;
8613 isl_vec_free(bound);
8617 isl_vec_free(bound);
8621 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8622 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8624 if (!bset || !signs)
8626 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8629 first += pos(bset->dim, type) - 1;
8630 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8633 /* Check if the given basic map is obviously single-valued.
8634 * In particular, for each output dimension, check that there is
8635 * an equality that defines the output dimension in terms of
8636 * earlier dimensions.
8638 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8648 total = 1 + isl_basic_map_total_dim(bmap);
8649 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8650 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8652 for (i = 0; i < n_out; ++i) {
8653 for (j = 0; j < bmap->n_eq; ++j) {
8654 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8656 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8657 total - (o_out + i + 1)) == -1)
8660 if (j >= bmap->n_eq)
8667 /* Check if the given map is obviously single-valued.
8669 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8678 return isl_basic_map_plain_is_single_valued(map->p[0]);
8681 /* Check if the given map is single-valued.
8686 * and check if the result is a subset of the identity mapping.
8688 int isl_map_is_single_valued(__isl_keep isl_map *map)
8695 sv = isl_map_plain_is_single_valued(map);
8699 test = isl_map_reverse(isl_map_copy(map));
8700 test = isl_map_apply_range(test, isl_map_copy(map));
8702 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8703 id = isl_map_identity(dim);
8705 sv = isl_map_is_subset(test, id);
8713 int isl_map_is_injective(__isl_keep isl_map *map)
8717 map = isl_map_copy(map);
8718 map = isl_map_reverse(map);
8719 in = isl_map_is_single_valued(map);
8725 /* Check if the given map is obviously injective.
8727 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8731 map = isl_map_copy(map);
8732 map = isl_map_reverse(map);
8733 in = isl_map_plain_is_single_valued(map);
8739 int isl_map_is_bijective(__isl_keep isl_map *map)
8743 sv = isl_map_is_single_valued(map);
8747 return isl_map_is_injective(map);
8750 int isl_set_is_singleton(__isl_keep isl_set *set)
8752 return isl_map_is_single_valued((isl_map *)set);
8755 int isl_map_is_translation(__isl_keep isl_map *map)
8760 delta = isl_map_deltas(isl_map_copy(map));
8761 ok = isl_set_is_singleton(delta);
8762 isl_set_free(delta);
8767 static int unique(isl_int *p, unsigned pos, unsigned len)
8769 if (isl_seq_first_non_zero(p, pos) != -1)
8771 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8776 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8785 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8788 nvar = isl_basic_set_dim(bset, isl_dim_set);
8789 ovar = isl_space_offset(bset->dim, isl_dim_set);
8790 for (j = 0; j < nvar; ++j) {
8791 int lower = 0, upper = 0;
8792 for (i = 0; i < bset->n_eq; ++i) {
8793 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8795 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8801 for (i = 0; i < bset->n_ineq; ++i) {
8802 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8804 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8806 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8811 if (!lower || !upper)
8818 int isl_set_is_box(__isl_keep isl_set *set)
8825 return isl_basic_set_is_box(set->p[0]);
8828 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8833 return isl_space_is_wrapping(bset->dim);
8836 int isl_set_is_wrapping(__isl_keep isl_set *set)
8841 return isl_space_is_wrapping(set->dim);
8844 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8846 bmap = isl_basic_map_cow(bmap);
8850 bmap->dim = isl_space_wrap(bmap->dim);
8854 bmap = isl_basic_map_finalize(bmap);
8856 return (isl_basic_set *)bmap;
8858 isl_basic_map_free(bmap);
8862 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8866 map = isl_map_cow(map);
8870 for (i = 0; i < map->n; ++i) {
8871 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8875 map->dim = isl_space_wrap(map->dim);
8879 return (isl_set *)map;
8885 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8887 bset = isl_basic_set_cow(bset);
8891 bset->dim = isl_space_unwrap(bset->dim);
8895 bset = isl_basic_set_finalize(bset);
8897 return (isl_basic_map *)bset;
8899 isl_basic_set_free(bset);
8903 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8910 if (!isl_set_is_wrapping(set))
8911 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8914 set = isl_set_cow(set);
8918 for (i = 0; i < set->n; ++i) {
8919 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8924 set->dim = isl_space_unwrap(set->dim);
8928 return (isl_map *)set;
8934 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8935 enum isl_dim_type type)
8940 if (!isl_space_is_named_or_nested(bmap->dim, type))
8943 bmap = isl_basic_map_cow(bmap);
8947 bmap->dim = isl_space_reset(bmap->dim, type);
8951 bmap = isl_basic_map_finalize(bmap);
8955 isl_basic_map_free(bmap);
8959 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8960 enum isl_dim_type type)
8967 if (!isl_space_is_named_or_nested(map->dim, type))
8970 map = isl_map_cow(map);
8974 for (i = 0; i < map->n; ++i) {
8975 map->p[i] = isl_basic_map_reset(map->p[i], type);
8979 map->dim = isl_space_reset(map->dim, type);
8989 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8994 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8997 bmap = isl_basic_map_cow(bmap);
9001 bmap->dim = isl_space_flatten(bmap->dim);
9005 bmap = isl_basic_map_finalize(bmap);
9009 isl_basic_map_free(bmap);
9013 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9015 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9018 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9019 __isl_take isl_basic_map *bmap)
9024 if (!bmap->dim->nested[0])
9027 bmap = isl_basic_map_cow(bmap);
9031 bmap->dim = isl_space_flatten_domain(bmap->dim);
9035 bmap = isl_basic_map_finalize(bmap);
9039 isl_basic_map_free(bmap);
9043 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9044 __isl_take isl_basic_map *bmap)
9049 if (!bmap->dim->nested[1])
9052 bmap = isl_basic_map_cow(bmap);
9056 bmap->dim = isl_space_flatten_range(bmap->dim);
9060 bmap = isl_basic_map_finalize(bmap);
9064 isl_basic_map_free(bmap);
9068 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9075 if (!map->dim->nested[0] && !map->dim->nested[1])
9078 map = isl_map_cow(map);
9082 for (i = 0; i < map->n; ++i) {
9083 map->p[i] = isl_basic_map_flatten(map->p[i]);
9087 map->dim = isl_space_flatten(map->dim);
9097 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9099 return (isl_set *)isl_map_flatten((isl_map *)set);
9102 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9104 isl_space *dim, *flat_dim;
9107 dim = isl_set_get_space(set);
9108 flat_dim = isl_space_flatten(isl_space_copy(dim));
9109 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9110 map = isl_map_intersect_domain(map, set);
9115 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9122 if (!map->dim->nested[0])
9125 map = isl_map_cow(map);
9129 for (i = 0; i < map->n; ++i) {
9130 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9134 map->dim = isl_space_flatten_domain(map->dim);
9144 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9151 if (!map->dim->nested[1])
9154 map = isl_map_cow(map);
9158 for (i = 0; i < map->n; ++i) {
9159 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9163 map->dim = isl_space_flatten_range(map->dim);
9173 /* Reorder the dimensions of "bmap" according to the given dim_map
9174 * and set the dimension specification to "dim".
9176 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9177 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9181 bmap = isl_basic_map_cow(bmap);
9182 if (!bmap || !dim || !dim_map)
9185 res = isl_basic_map_alloc_space(dim,
9186 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9187 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9188 res = isl_basic_map_finalize(res);
9192 isl_basic_map_free(bmap);
9193 isl_space_free(dim);
9197 /* Reorder the dimensions of "map" according to given reordering.
9199 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9200 __isl_take isl_reordering *r)
9203 struct isl_dim_map *dim_map;
9205 map = isl_map_cow(map);
9206 dim_map = isl_dim_map_from_reordering(r);
9207 if (!map || !r || !dim_map)
9210 for (i = 0; i < map->n; ++i) {
9211 struct isl_dim_map *dim_map_i;
9213 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9215 map->p[i] = isl_basic_map_realign(map->p[i],
9216 isl_space_copy(r->dim), dim_map_i);
9222 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9224 isl_reordering_free(r);
9230 isl_reordering_free(r);
9234 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9235 __isl_take isl_reordering *r)
9237 return (isl_set *)isl_map_realign((isl_map *)set, r);
9240 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9241 __isl_take isl_space *model)
9248 ctx = isl_space_get_ctx(model);
9249 if (!isl_space_has_named_params(model))
9250 isl_die(ctx, isl_error_invalid,
9251 "model has unnamed parameters", goto error);
9252 if (!isl_space_has_named_params(map->dim))
9253 isl_die(ctx, isl_error_invalid,
9254 "relation has unnamed parameters", goto error);
9255 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9256 isl_reordering *exp;
9258 model = isl_space_drop_dims(model, isl_dim_in,
9259 0, isl_space_dim(model, isl_dim_in));
9260 model = isl_space_drop_dims(model, isl_dim_out,
9261 0, isl_space_dim(model, isl_dim_out));
9262 exp = isl_parameter_alignment_reordering(map->dim, model);
9263 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9264 map = isl_map_realign(map, exp);
9267 isl_space_free(model);
9270 isl_space_free(model);
9275 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9276 __isl_take isl_space *model)
9278 return isl_map_align_params(set, model);
9281 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9282 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9283 enum isl_dim_type c2, enum isl_dim_type c3,
9284 enum isl_dim_type c4, enum isl_dim_type c5)
9286 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9287 struct isl_mat *mat;
9293 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9294 isl_basic_map_total_dim(bmap) + 1);
9297 for (i = 0; i < bmap->n_eq; ++i)
9298 for (j = 0, pos = 0; j < 5; ++j) {
9299 int off = isl_basic_map_offset(bmap, c[j]);
9300 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9301 isl_int_set(mat->row[i][pos],
9302 bmap->eq[i][off + k]);
9310 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9311 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9312 enum isl_dim_type c2, enum isl_dim_type c3,
9313 enum isl_dim_type c4, enum isl_dim_type c5)
9315 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9316 struct isl_mat *mat;
9322 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9323 isl_basic_map_total_dim(bmap) + 1);
9326 for (i = 0; i < bmap->n_ineq; ++i)
9327 for (j = 0, pos = 0; j < 5; ++j) {
9328 int off = isl_basic_map_offset(bmap, c[j]);
9329 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9330 isl_int_set(mat->row[i][pos],
9331 bmap->ineq[i][off + k]);
9339 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9340 __isl_take isl_space *dim,
9341 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9342 enum isl_dim_type c2, enum isl_dim_type c3,
9343 enum isl_dim_type c4, enum isl_dim_type c5)
9345 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9346 isl_basic_map *bmap;
9352 if (!dim || !eq || !ineq)
9355 if (eq->n_col != ineq->n_col)
9356 isl_die(dim->ctx, isl_error_invalid,
9357 "equalities and inequalities matrices should have "
9358 "same number of columns", goto error);
9360 total = 1 + isl_space_dim(dim, isl_dim_all);
9362 if (eq->n_col < total)
9363 isl_die(dim->ctx, isl_error_invalid,
9364 "number of columns too small", goto error);
9366 extra = eq->n_col - total;
9368 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9369 eq->n_row, ineq->n_row);
9372 for (i = 0; i < extra; ++i) {
9373 k = isl_basic_map_alloc_div(bmap);
9376 isl_int_set_si(bmap->div[k][0], 0);
9378 for (i = 0; i < eq->n_row; ++i) {
9379 l = isl_basic_map_alloc_equality(bmap);
9382 for (j = 0, pos = 0; j < 5; ++j) {
9383 int off = isl_basic_map_offset(bmap, c[j]);
9384 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9385 isl_int_set(bmap->eq[l][off + k],
9391 for (i = 0; i < ineq->n_row; ++i) {
9392 l = isl_basic_map_alloc_inequality(bmap);
9395 for (j = 0, pos = 0; j < 5; ++j) {
9396 int off = isl_basic_map_offset(bmap, c[j]);
9397 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9398 isl_int_set(bmap->ineq[l][off + k],
9405 isl_space_free(dim);
9411 isl_space_free(dim);
9417 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9418 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9419 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9421 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9422 c1, c2, c3, c4, isl_dim_in);
9425 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9426 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9427 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9429 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9430 c1, c2, c3, c4, isl_dim_in);
9433 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9434 __isl_take isl_space *dim,
9435 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9436 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9438 return (isl_basic_set*)
9439 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9440 c1, c2, c3, c4, isl_dim_in);
9443 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9448 return isl_space_can_zip(bmap->dim);
9451 int isl_map_can_zip(__isl_keep isl_map *map)
9456 return isl_space_can_zip(map->dim);
9459 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9460 * (A -> C) -> (B -> D).
9462 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9471 if (!isl_basic_map_can_zip(bmap))
9472 isl_die(bmap->ctx, isl_error_invalid,
9473 "basic map cannot be zipped", goto error);
9474 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9475 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9476 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9477 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9478 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9481 bmap->dim = isl_space_zip(bmap->dim);
9486 isl_basic_map_free(bmap);
9490 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9491 * (A -> C) -> (B -> D).
9493 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9500 if (!isl_map_can_zip(map))
9501 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9504 map = isl_map_cow(map);
9508 for (i = 0; i < map->n; ++i) {
9509 map->p[i] = isl_basic_map_zip(map->p[i]);
9514 map->dim = isl_space_zip(map->dim);
9524 /* Construct a basic map mapping the domain of the affine expression
9525 * to a one-dimensional range prescribed by the affine expression.
9527 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9531 isl_local_space *ls;
9532 isl_basic_map *bmap;
9537 ls = isl_aff_get_local_space(aff);
9538 bmap = isl_basic_map_from_local_space(ls);
9539 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9540 k = isl_basic_map_alloc_equality(bmap);
9544 pos = isl_basic_map_offset(bmap, isl_dim_out);
9545 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9546 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9547 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9548 aff->v->size - (pos + 1));
9551 bmap = isl_basic_map_finalize(bmap);
9555 isl_basic_map_free(bmap);
9559 /* Construct a basic map mapping a domain in the given space to
9560 * to an n-dimensional range, with n the number of elements in the list,
9561 * where each coordinate in the range is prescribed by the
9562 * corresponding affine expression.
9563 * The domains of all affine expressions in the list are assumed to match
9566 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9567 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9571 isl_basic_map *bmap;
9576 dim = isl_space_from_domain(domain_dim);
9577 bmap = isl_basic_map_universe(dim);
9579 for (i = 0; i < list->n; ++i) {
9581 isl_basic_map *bmap_i;
9583 aff = isl_aff_copy(list->p[i]);
9584 bmap_i = isl_basic_map_from_aff(aff);
9586 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9589 isl_aff_list_free(list);
9593 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9594 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9596 return isl_map_equate(set, type1, pos1, type2, pos2);
9599 /* Add a constraint imposing that the given two dimensions are equal.
9601 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9602 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9604 isl_basic_map *bmap = NULL;
9610 if (pos1 >= isl_map_dim(map, type1))
9611 isl_die(map->ctx, isl_error_invalid,
9612 "index out of bounds", goto error);
9613 if (pos2 >= isl_map_dim(map, type2))
9614 isl_die(map->ctx, isl_error_invalid,
9615 "index out of bounds", goto error);
9617 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9618 i = isl_basic_map_alloc_equality(bmap);
9621 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9622 pos1 += isl_basic_map_offset(bmap, type1);
9623 pos2 += isl_basic_map_offset(bmap, type2);
9624 isl_int_set_si(bmap->eq[i][pos1], -1);
9625 isl_int_set_si(bmap->eq[i][pos2], 1);
9626 bmap = isl_basic_map_finalize(bmap);
9628 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9632 isl_basic_map_free(bmap);
9637 /* Add a constraint imposing that the given two dimensions have opposite values.
9639 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9640 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9642 isl_basic_map *bmap = NULL;
9648 if (pos1 >= isl_map_dim(map, type1))
9649 isl_die(map->ctx, isl_error_invalid,
9650 "index out of bounds", goto error);
9651 if (pos2 >= isl_map_dim(map, type2))
9652 isl_die(map->ctx, isl_error_invalid,
9653 "index out of bounds", goto error);
9655 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9656 i = isl_basic_map_alloc_equality(bmap);
9659 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9660 pos1 += isl_basic_map_offset(bmap, type1);
9661 pos2 += isl_basic_map_offset(bmap, type2);
9662 isl_int_set_si(bmap->eq[i][pos1], 1);
9663 isl_int_set_si(bmap->eq[i][pos2], 1);
9664 bmap = isl_basic_map_finalize(bmap);
9666 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9670 isl_basic_map_free(bmap);
9675 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
9679 isl_local_space *ls;
9684 if (!isl_basic_map_divs_known(bmap))
9685 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
9686 "some divs are unknown", return NULL);
9688 ls = isl_basic_map_get_local_space(bmap);
9689 div = isl_local_space_get_div(ls, pos);
9690 isl_local_space_free(ls);
9695 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
9698 return isl_basic_map_get_div(bset, pos);