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_dim_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(struct isl_dim *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(struct isl_dim *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(const struct isl_basic_map *bmap,
56 enum isl_dim_type type)
61 case isl_dim_cst: return 1;
64 case isl_dim_out: return isl_dim_size(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(const struct isl_map *map, enum isl_dim_type type)
73 return map ? n(map->dim, type) : 0;
76 unsigned isl_set_dim(const struct 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 struct isl_dim *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(const struct isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
112 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
114 return isl_basic_set_dim(bset, isl_dim_set);
117 unsigned isl_basic_set_n_param(const struct 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_dim_total(bset->dim) + bset->n_div;
127 unsigned isl_set_n_dim(const struct isl_set *set)
129 return isl_set_dim(set, isl_dim_set);
132 unsigned isl_set_n_param(const struct 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_dim_total(bmap->dim) + 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_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
185 return isl_dim_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_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
197 return isl_dim_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_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
208 return isl_dim_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_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
220 return isl_dim_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 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
247 return isl_dim_copy(bmap->dim);
250 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
254 return isl_dim_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_dim_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_dim(isl_local_space_get_dim(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 struct isl_dim *isl_map_get_dim(struct isl_map *map)
326 return isl_dim_copy(map->dim);
329 struct isl_dim *isl_set_get_dim(struct isl_set *set)
333 return isl_dim_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_dim_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_dim_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_dim_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_dim_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 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
403 return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
406 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
408 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
411 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
412 enum isl_dim_type type, unsigned pos)
414 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
417 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
418 enum isl_dim_type type, unsigned pos)
420 return bset ? isl_dim_get_name(bset->dim, type, pos) : NULL;
423 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
424 enum isl_dim_type type, unsigned pos)
426 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
429 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
430 enum isl_dim_type type, unsigned pos)
432 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
435 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
436 __isl_take isl_basic_map *bmap,
437 enum isl_dim_type type, unsigned pos, const char *s)
441 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
446 isl_basic_map_free(bmap);
450 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
451 enum isl_dim_type type, unsigned pos, const char *s)
458 map->dim = isl_dim_set_name(map->dim, type, pos, s);
462 for (i = 0; i < map->n; ++i) {
463 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
474 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
475 __isl_take isl_basic_set *bset,
476 enum isl_dim_type type, unsigned pos, const char *s)
478 return (isl_basic_set *)isl_basic_map_set_dim_name(
479 (isl_basic_map *)bset, type, pos, s);
482 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
483 enum isl_dim_type type, unsigned pos, const char *s)
485 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
488 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
492 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
495 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
497 return isl_basic_map_is_rational(bset);
500 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
501 struct isl_basic_map *bmap, unsigned extra,
502 unsigned n_eq, unsigned n_ineq)
505 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
510 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
511 if (isl_blk_is_error(bmap->block))
514 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
519 bmap->block2 = isl_blk_empty();
522 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
523 if (isl_blk_is_error(bmap->block2))
526 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
531 for (i = 0; i < n_ineq + n_eq; ++i)
532 bmap->ineq[i] = bmap->block.data + i * row_size;
534 for (i = 0; i < extra; ++i)
535 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
539 bmap->c_size = n_eq + n_ineq;
540 bmap->eq = bmap->ineq + n_ineq;
549 isl_basic_map_free(bmap);
553 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
554 unsigned nparam, unsigned dim, unsigned extra,
555 unsigned n_eq, unsigned n_ineq)
557 struct isl_basic_map *bmap;
558 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
559 return (struct isl_basic_set *)bmap;
562 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
563 unsigned extra, unsigned n_eq, unsigned n_ineq)
565 struct isl_basic_map *bmap;
568 isl_assert(dim->ctx, dim->n_in == 0, goto error);
569 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
570 return (struct isl_basic_set *)bmap;
576 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
577 unsigned extra, unsigned n_eq, unsigned n_ineq)
579 struct isl_basic_map *bmap;
583 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
588 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
594 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
595 unsigned nparam, unsigned in, unsigned out, unsigned extra,
596 unsigned n_eq, unsigned n_ineq)
598 struct isl_basic_map *bmap;
601 dim = isl_dim_alloc(ctx, nparam, in, out);
605 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
609 static void dup_constraints(
610 struct isl_basic_map *dst, struct isl_basic_map *src)
613 unsigned total = isl_basic_map_total_dim(src);
615 for (i = 0; i < src->n_eq; ++i) {
616 int j = isl_basic_map_alloc_equality(dst);
617 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
620 for (i = 0; i < src->n_ineq; ++i) {
621 int j = isl_basic_map_alloc_inequality(dst);
622 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
625 for (i = 0; i < src->n_div; ++i) {
626 int j = isl_basic_map_alloc_div(dst);
627 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
629 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
632 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
634 struct isl_basic_map *dup;
638 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
639 bmap->n_div, bmap->n_eq, bmap->n_ineq);
642 dup_constraints(dup, bmap);
643 dup->flags = bmap->flags;
644 dup->sample = isl_vec_copy(bmap->sample);
648 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
650 struct isl_basic_map *dup;
652 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
653 return (struct isl_basic_set *)dup;
656 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
661 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
665 return isl_basic_set_dup(bset);
668 struct isl_set *isl_set_copy(struct isl_set *set)
677 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
682 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
686 bmap = isl_basic_map_dup(bmap);
688 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
692 struct isl_map *isl_map_copy(struct isl_map *map)
701 void isl_basic_map_free(struct isl_basic_map *bmap)
709 isl_ctx_deref(bmap->ctx);
711 isl_blk_free(bmap->ctx, bmap->block2);
713 isl_blk_free(bmap->ctx, bmap->block);
714 isl_vec_free(bmap->sample);
715 isl_dim_free(bmap->dim);
719 void isl_basic_set_free(struct isl_basic_set *bset)
721 isl_basic_map_free((struct isl_basic_map *)bset);
724 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
726 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
729 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
735 isl_assert(ctx, room_for_con(bmap, 1), return -1);
736 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
738 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
739 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
740 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
741 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
742 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
743 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
745 int j = isl_basic_map_alloc_inequality(bmap);
749 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
750 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
757 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
758 bmap->extra - bmap->n_div);
762 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
764 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
767 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
771 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
776 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
778 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
781 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
786 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
788 if (pos != bmap->n_eq - 1) {
790 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
791 bmap->eq[bmap->n_eq - 1] = t;
797 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
799 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
802 void isl_basic_map_inequality_to_equality(
803 struct isl_basic_map *bmap, unsigned pos)
808 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
809 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
814 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
815 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
816 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
817 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
820 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
822 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
825 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
831 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
832 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
833 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
834 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
835 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
836 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
837 1 + isl_basic_map_total_dim(bmap),
838 bmap->extra - bmap->n_div);
839 return bmap->n_ineq++;
842 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
844 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
847 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
851 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
856 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
858 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
861 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
866 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
868 if (pos != bmap->n_ineq - 1) {
870 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
871 bmap->ineq[bmap->n_ineq - 1] = t;
872 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
878 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
880 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
883 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
888 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
891 k = isl_basic_map_alloc_equality(bmap);
894 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
897 isl_basic_map_free(bmap);
901 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
904 return (isl_basic_set *)
905 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
908 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
913 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
916 k = isl_basic_map_alloc_inequality(bmap);
919 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
922 isl_basic_map_free(bmap);
926 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
929 return (isl_basic_set *)
930 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
933 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
937 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
938 isl_seq_clr(bmap->div[bmap->n_div] +
939 1 + 1 + isl_basic_map_total_dim(bmap),
940 bmap->extra - bmap->n_div);
941 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
942 return bmap->n_div++;
945 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
947 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
950 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
954 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
959 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
961 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
964 /* Copy constraint from src to dst, putting the vars of src at offset
965 * dim_off in dst and the divs of src at offset div_off in dst.
966 * If both sets are actually map, then dim_off applies to the input
969 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
970 struct isl_basic_map *src_map, isl_int *src,
971 unsigned in_off, unsigned out_off, unsigned div_off)
973 unsigned src_nparam = isl_basic_map_n_param(src_map);
974 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
975 unsigned src_in = isl_basic_map_n_in(src_map);
976 unsigned dst_in = isl_basic_map_n_in(dst_map);
977 unsigned src_out = isl_basic_map_n_out(src_map);
978 unsigned dst_out = isl_basic_map_n_out(dst_map);
979 isl_int_set(dst[0], src[0]);
980 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
981 if (dst_nparam > src_nparam)
982 isl_seq_clr(dst+1+src_nparam,
983 dst_nparam - src_nparam);
984 isl_seq_clr(dst+1+dst_nparam, in_off);
985 isl_seq_cpy(dst+1+dst_nparam+in_off,
987 isl_min(dst_in-in_off, src_in));
988 if (dst_in-in_off > src_in)
989 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
990 dst_in - in_off - src_in);
991 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
992 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
993 src+1+src_nparam+src_in,
994 isl_min(dst_out-out_off, src_out));
995 if (dst_out-out_off > src_out)
996 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
997 dst_out - out_off - src_out);
998 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
999 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1000 src+1+src_nparam+src_in+src_out,
1001 isl_min(dst_map->extra-div_off, src_map->n_div));
1002 if (dst_map->n_div-div_off > src_map->n_div)
1003 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1004 div_off+src_map->n_div,
1005 dst_map->n_div - div_off - src_map->n_div);
1008 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1009 struct isl_basic_map *src_map, isl_int *src,
1010 unsigned in_off, unsigned out_off, unsigned div_off)
1012 isl_int_set(dst[0], src[0]);
1013 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1016 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1017 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1022 if (!bmap1 || !bmap2)
1025 div_off = bmap1->n_div;
1027 for (i = 0; i < bmap2->n_eq; ++i) {
1028 int i1 = isl_basic_map_alloc_equality(bmap1);
1031 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1032 i_pos, o_pos, div_off);
1035 for (i = 0; i < bmap2->n_ineq; ++i) {
1036 int i1 = isl_basic_map_alloc_inequality(bmap1);
1039 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1040 i_pos, o_pos, div_off);
1043 for (i = 0; i < bmap2->n_div; ++i) {
1044 int i1 = isl_basic_map_alloc_div(bmap1);
1047 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1048 i_pos, o_pos, div_off);
1051 isl_basic_map_free(bmap2);
1056 isl_basic_map_free(bmap1);
1057 isl_basic_map_free(bmap2);
1061 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1062 struct isl_basic_set *bset2, unsigned pos)
1064 return (struct isl_basic_set *)
1065 add_constraints((struct isl_basic_map *)bset1,
1066 (struct isl_basic_map *)bset2, 0, pos);
1069 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1070 struct isl_dim *dim, unsigned extra,
1071 unsigned n_eq, unsigned n_ineq)
1073 struct isl_basic_map *ext;
1083 dims_ok = isl_dim_equal(base->dim, dim) &&
1084 base->extra >= base->n_div + extra;
1086 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1087 room_for_ineq(base, n_ineq)) {
1092 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1093 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1094 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1095 extra += base->extra;
1097 n_ineq += base->n_ineq;
1099 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1105 ext->sample = isl_vec_copy(base->sample);
1106 flags = base->flags;
1107 ext = add_constraints(ext, base, 0, 0);
1110 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1117 isl_basic_map_free(base);
1121 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1122 struct isl_dim *dim, unsigned extra,
1123 unsigned n_eq, unsigned n_ineq)
1125 return (struct isl_basic_set *)
1126 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1127 extra, n_eq, n_ineq);
1130 struct isl_basic_map *isl_basic_map_extend_constraints(
1131 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1135 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1139 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1140 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1141 unsigned n_eq, unsigned n_ineq)
1143 struct isl_basic_map *bmap;
1144 struct isl_dim *dim;
1148 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1152 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1155 isl_basic_map_free(base);
1159 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1160 unsigned nparam, unsigned dim, unsigned extra,
1161 unsigned n_eq, unsigned n_ineq)
1163 return (struct isl_basic_set *)
1164 isl_basic_map_extend((struct isl_basic_map *)base,
1165 nparam, 0, dim, extra, n_eq, n_ineq);
1168 struct isl_basic_set *isl_basic_set_extend_constraints(
1169 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1171 return (struct isl_basic_set *)
1172 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1176 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1178 return (struct isl_basic_set *)
1179 isl_basic_map_cow((struct isl_basic_map *)bset);
1182 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1187 if (bmap->ref > 1) {
1189 bmap = isl_basic_map_dup(bmap);
1192 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1196 struct isl_set *isl_set_cow(struct isl_set *set)
1204 return isl_set_dup(set);
1207 struct isl_map *isl_map_cow(struct isl_map *map)
1215 return isl_map_dup(map);
1218 static void swap_vars(struct isl_blk blk, isl_int *a,
1219 unsigned a_len, unsigned b_len)
1221 isl_seq_cpy(blk.data, a+a_len, b_len);
1222 isl_seq_cpy(blk.data+b_len, a, a_len);
1223 isl_seq_cpy(a, blk.data, b_len+a_len);
1226 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1227 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1235 isl_assert(bmap->ctx,
1236 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1238 if (n1 == 0 || n2 == 0)
1241 bmap = isl_basic_map_cow(bmap);
1245 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1246 if (isl_blk_is_error(blk))
1249 for (i = 0; i < bmap->n_eq; ++i)
1251 bmap->eq[i] + pos, n1, n2);
1253 for (i = 0; i < bmap->n_ineq; ++i)
1255 bmap->ineq[i] + pos, n1, n2);
1257 for (i = 0; i < bmap->n_div; ++i)
1259 bmap->div[i]+1 + pos, n1, n2);
1261 isl_blk_free(bmap->ctx, blk);
1263 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1264 bmap = isl_basic_map_gauss(bmap, NULL);
1265 return isl_basic_map_finalize(bmap);
1267 isl_basic_map_free(bmap);
1271 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1272 __isl_take isl_basic_set *bset, unsigned n)
1277 nparam = isl_basic_set_n_param(bset);
1278 dim = isl_basic_set_n_dim(bset);
1279 isl_assert(bset->ctx, n <= dim, goto error);
1281 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1283 isl_basic_set_free(bset);
1287 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1293 total = isl_basic_map_total_dim(bmap);
1294 isl_basic_map_free_div(bmap, bmap->n_div);
1295 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1297 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1299 i = isl_basic_map_alloc_equality(bmap);
1303 isl_int_set_si(bmap->eq[i][0], 1);
1304 isl_seq_clr(bmap->eq[i]+1, total);
1305 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1306 isl_vec_free(bmap->sample);
1307 bmap->sample = NULL;
1308 return isl_basic_map_finalize(bmap);
1310 isl_basic_map_free(bmap);
1314 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1316 return (struct isl_basic_set *)
1317 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1320 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1323 unsigned off = isl_dim_total(bmap->dim);
1324 isl_int *t = bmap->div[a];
1325 bmap->div[a] = bmap->div[b];
1328 for (i = 0; i < bmap->n_eq; ++i)
1329 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1331 for (i = 0; i < bmap->n_ineq; ++i)
1332 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1334 for (i = 0; i < bmap->n_div; ++i)
1335 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1336 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1339 /* Eliminate the specified n dimensions starting at first from the
1340 * constraints using Fourier-Motzkin. The dimensions themselves
1343 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1344 enum isl_dim_type type, unsigned first, unsigned n)
1353 map = isl_map_cow(map);
1356 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1357 first += pos(map->dim, type) - 1;
1359 for (i = 0; i < map->n; ++i) {
1360 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1370 /* Eliminate the specified n dimensions starting at first from the
1371 * constraints using Fourier-Motzkin. The dimensions themselves
1374 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1375 enum isl_dim_type type, unsigned first, unsigned n)
1377 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1380 /* Eliminate the specified n dimensions starting at first from the
1381 * constraints using Fourier-Motzkin. The dimensions themselves
1384 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1385 unsigned first, unsigned n)
1387 return isl_set_eliminate(set, isl_dim_set, first, n);
1390 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1391 __isl_take isl_basic_map *bmap)
1393 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1398 return isl_basic_map_finalize(bmap);
1401 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1402 __isl_take isl_basic_set *bset)
1404 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1405 (struct isl_basic_map *)bset);
1408 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1417 map = isl_map_cow(map);
1421 for (i = 0; i < map->n; ++i) {
1422 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1432 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1434 return isl_map_remove_divs(set);
1437 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1438 enum isl_dim_type type, unsigned first, unsigned n)
1442 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1446 bmap = isl_basic_map_eliminate_vars(bmap,
1447 isl_basic_map_offset(bmap, type) - 1 + first, n);
1450 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1452 bmap = isl_basic_map_drop(bmap, type, first, n);
1455 isl_basic_map_free(bmap);
1459 /* Return true if the definition of the given div (recursively) involves
1460 * any of the given variables.
1462 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1463 unsigned first, unsigned n)
1466 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1468 if (isl_int_is_zero(bmap->div[div][0]))
1470 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1473 for (i = bmap->n_div - 1; i >= 0; --i) {
1474 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1476 if (div_involves_vars(bmap, i, first, n))
1483 /* Remove all divs (recursively) involving any of the given dimensions
1484 * in their definitions.
1486 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1487 __isl_take isl_basic_map *bmap,
1488 enum isl_dim_type type, unsigned first, unsigned n)
1494 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1496 first += isl_basic_map_offset(bmap, type);
1498 for (i = bmap->n_div - 1; i >= 0; --i) {
1499 if (!div_involves_vars(bmap, i, first, n))
1501 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1509 isl_basic_map_free(bmap);
1513 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1514 enum isl_dim_type type, unsigned first, unsigned n)
1523 map = isl_map_cow(map);
1527 for (i = 0; i < map->n; ++i) {
1528 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1539 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1540 enum isl_dim_type type, unsigned first, unsigned n)
1542 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1546 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1547 enum isl_dim_type type, unsigned first, unsigned n)
1554 if (first + n > isl_basic_map_dim(bmap, type))
1555 isl_die(bmap->ctx, isl_error_invalid,
1556 "index out of bounds", return -1);
1558 first += isl_basic_map_offset(bmap, type);
1559 for (i = 0; i < bmap->n_eq; ++i)
1560 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1562 for (i = 0; i < bmap->n_ineq; ++i)
1563 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1569 int isl_map_involves_dims(__isl_keep isl_map *map,
1570 enum isl_dim_type type, unsigned first, unsigned n)
1577 if (first + n > isl_map_dim(map, type))
1578 isl_die(map->ctx, isl_error_invalid,
1579 "index out of bounds", return -1);
1581 for (i = 0; i < map->n; ++i) {
1582 int involves = isl_basic_map_involves_dims(map->p[i],
1584 if (involves < 0 || !involves)
1591 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1592 enum isl_dim_type type, unsigned first, unsigned n)
1594 return isl_basic_map_involves_dims(bset, type, first, n);
1597 int isl_set_involves_dims(__isl_keep isl_set *set,
1598 enum isl_dim_type type, unsigned first, unsigned n)
1600 return isl_map_involves_dims(set, type, first, n);
1603 /* Return true if the definition of the given div is unknown or depends
1606 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1609 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1611 if (isl_int_is_zero(bmap->div[div][0]))
1614 for (i = bmap->n_div - 1; i >= 0; --i) {
1615 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1617 if (div_is_unknown(bmap, i))
1624 /* Remove all divs that are unknown or defined in terms of unknown divs.
1626 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1627 __isl_take isl_basic_map *bmap)
1634 for (i = bmap->n_div - 1; i >= 0; --i) {
1635 if (!div_is_unknown(bmap, i))
1637 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1643 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1652 map = isl_map_cow(map);
1656 for (i = 0; i < map->n; ++i) {
1657 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1667 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1669 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1672 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1673 __isl_take isl_basic_set *bset,
1674 enum isl_dim_type type, unsigned first, unsigned n)
1676 return (isl_basic_set *)
1677 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1680 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1681 enum isl_dim_type type, unsigned first, unsigned n)
1688 map = isl_map_cow(map);
1691 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1693 for (i = 0; i < map->n; ++i) {
1694 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1695 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1699 map = isl_map_drop(map, type, first, n);
1706 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1707 enum isl_dim_type type, unsigned first, unsigned n)
1709 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1712 /* Project out n inputs starting at first using Fourier-Motzkin */
1713 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1714 unsigned first, unsigned n)
1716 return isl_map_remove_dims(map, isl_dim_in, first, n);
1719 static void dump_term(struct isl_basic_map *bmap,
1720 isl_int c, int pos, FILE *out)
1723 unsigned in = isl_basic_map_n_in(bmap);
1724 unsigned dim = in + isl_basic_map_n_out(bmap);
1725 unsigned nparam = isl_basic_map_n_param(bmap);
1727 isl_int_print(out, c, 0);
1729 if (!isl_int_is_one(c))
1730 isl_int_print(out, c, 0);
1731 if (pos < 1 + nparam) {
1732 name = isl_dim_get_name(bmap->dim,
1733 isl_dim_param, pos - 1);
1735 fprintf(out, "%s", name);
1737 fprintf(out, "p%d", pos - 1);
1738 } else if (pos < 1 + nparam + in)
1739 fprintf(out, "i%d", pos - 1 - nparam);
1740 else if (pos < 1 + nparam + dim)
1741 fprintf(out, "o%d", pos - 1 - nparam - in);
1743 fprintf(out, "e%d", pos - 1 - nparam - dim);
1747 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1748 int sign, FILE *out)
1752 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1756 for (i = 0, first = 1; i < len; ++i) {
1757 if (isl_int_sgn(c[i]) * sign <= 0)
1760 fprintf(out, " + ");
1762 isl_int_abs(v, c[i]);
1763 dump_term(bmap, v, i, out);
1770 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1771 const char *op, FILE *out, int indent)
1775 fprintf(out, "%*s", indent, "");
1777 dump_constraint_sign(bmap, c, 1, out);
1778 fprintf(out, " %s ", op);
1779 dump_constraint_sign(bmap, c, -1, out);
1783 for (i = bmap->n_div; i < bmap->extra; ++i) {
1784 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1786 fprintf(out, "%*s", indent, "");
1787 fprintf(out, "ERROR: unused div coefficient not zero\n");
1792 static void dump_constraints(struct isl_basic_map *bmap,
1793 isl_int **c, unsigned n,
1794 const char *op, FILE *out, int indent)
1798 for (i = 0; i < n; ++i)
1799 dump_constraint(bmap, c[i], op, out, indent);
1802 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1806 unsigned total = isl_basic_map_total_dim(bmap);
1808 for (j = 0; j < 1 + total; ++j) {
1809 if (isl_int_is_zero(exp[j]))
1811 if (!first && isl_int_is_pos(exp[j]))
1813 dump_term(bmap, exp[j], j, out);
1818 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1822 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1823 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1825 for (i = 0; i < bmap->n_div; ++i) {
1826 fprintf(out, "%*s", indent, "");
1827 fprintf(out, "e%d = [(", i);
1828 dump_affine(bmap, bmap->div[i]+1, out);
1830 isl_int_print(out, bmap->div[i][0], 0);
1831 fprintf(out, "]\n");
1835 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1836 FILE *out, int indent)
1839 fprintf(out, "null basic set\n");
1843 fprintf(out, "%*s", indent, "");
1844 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1845 bset->ref, bset->dim->nparam, bset->dim->n_out,
1846 bset->extra, bset->flags);
1847 dump((struct isl_basic_map *)bset, out, indent);
1850 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1851 FILE *out, int indent)
1854 fprintf(out, "null basic map\n");
1858 fprintf(out, "%*s", indent, "");
1859 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1860 "flags: %x, n_name: %d\n",
1862 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1863 bmap->extra, bmap->flags, bmap->dim->n_name);
1864 dump(bmap, out, indent);
1867 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1872 total = isl_basic_map_total_dim(bmap);
1873 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1874 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1875 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1876 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1880 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1882 struct isl_set *set;
1886 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1887 isl_assert(dim->ctx, n >= 0, goto error);
1888 set = isl_alloc(dim->ctx, struct isl_set,
1889 sizeof(struct isl_set) +
1890 (n - 1) * sizeof(struct isl_basic_set *));
1894 set->ctx = dim->ctx;
1895 isl_ctx_ref(set->ctx);
1907 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1908 unsigned nparam, unsigned dim, int n, unsigned flags)
1910 struct isl_set *set;
1911 struct isl_dim *dims;
1913 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1917 set = isl_set_alloc_dim(dims, n, flags);
1921 /* Make sure "map" has room for at least "n" more basic maps.
1923 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1926 struct isl_map *grown = NULL;
1930 isl_assert(map->ctx, n >= 0, goto error);
1931 if (map->n + n <= map->size)
1933 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1936 for (i = 0; i < map->n; ++i) {
1937 grown->p[i] = isl_basic_map_copy(map->p[i]);
1945 isl_map_free(grown);
1950 /* Make sure "set" has room for at least "n" more basic sets.
1952 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1954 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1957 struct isl_set *isl_set_dup(struct isl_set *set)
1960 struct isl_set *dup;
1965 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1968 for (i = 0; i < set->n; ++i)
1969 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1973 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1975 return isl_map_from_basic_map(bset);
1978 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1980 struct isl_map *map;
1985 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1986 return isl_map_add_basic_map(map, bmap);
1989 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1990 __isl_take isl_basic_set *bset)
1992 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1993 (struct isl_basic_map *)bset);
1996 void isl_set_free(struct isl_set *set)
2006 isl_ctx_deref(set->ctx);
2007 for (i = 0; i < set->n; ++i)
2008 isl_basic_set_free(set->p[i]);
2009 isl_dim_free(set->dim);
2013 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2018 fprintf(out, "null set\n");
2022 fprintf(out, "%*s", indent, "");
2023 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2024 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2026 for (i = 0; i < set->n; ++i) {
2027 fprintf(out, "%*s", indent, "");
2028 fprintf(out, "basic set %d:\n", i);
2029 isl_basic_set_print_internal(set->p[i], out, indent+4);
2033 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2038 fprintf(out, "null map\n");
2042 fprintf(out, "%*s", indent, "");
2043 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2044 "flags: %x, n_name: %d\n",
2045 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2046 map->dim->n_out, map->flags, map->dim->n_name);
2047 for (i = 0; i < map->n; ++i) {
2048 fprintf(out, "%*s", indent, "");
2049 fprintf(out, "basic map %d:\n", i);
2050 isl_basic_map_print_internal(map->p[i], out, indent+4);
2054 struct isl_basic_map *isl_basic_map_intersect_domain(
2055 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2057 struct isl_basic_map *bmap_domain;
2062 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2063 bset->dim, isl_dim_param), goto error);
2065 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2066 isl_assert(bset->ctx,
2067 isl_basic_map_compatible_domain(bmap, bset), goto error);
2069 bmap = isl_basic_map_cow(bmap);
2072 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2073 bset->n_div, bset->n_eq, bset->n_ineq);
2074 bmap_domain = isl_basic_map_from_domain(bset);
2075 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2077 bmap = isl_basic_map_simplify(bmap);
2078 return isl_basic_map_finalize(bmap);
2080 isl_basic_map_free(bmap);
2081 isl_basic_set_free(bset);
2085 struct isl_basic_map *isl_basic_map_intersect_range(
2086 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2088 struct isl_basic_map *bmap_range;
2093 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2094 bset->dim, isl_dim_param), goto error);
2096 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2097 isl_assert(bset->ctx,
2098 isl_basic_map_compatible_range(bmap, bset), goto error);
2100 if (isl_basic_set_is_universe(bset)) {
2101 isl_basic_set_free(bset);
2105 bmap = isl_basic_map_cow(bmap);
2108 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2109 bset->n_div, bset->n_eq, bset->n_ineq);
2110 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2111 bmap = add_constraints(bmap, bmap_range, 0, 0);
2113 bmap = isl_basic_map_simplify(bmap);
2114 return isl_basic_map_finalize(bmap);
2116 isl_basic_map_free(bmap);
2117 isl_basic_set_free(bset);
2121 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2127 total = 1 + isl_basic_map_total_dim(bmap);
2128 if (total != vec->size)
2133 for (i = 0; i < bmap->n_eq; ++i) {
2134 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2135 if (!isl_int_is_zero(s)) {
2141 for (i = 0; i < bmap->n_ineq; ++i) {
2142 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2143 if (isl_int_is_neg(s)) {
2154 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2156 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2159 struct isl_basic_map *isl_basic_map_intersect(
2160 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2162 struct isl_vec *sample = NULL;
2164 if (!bmap1 || !bmap2)
2167 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2168 bmap2->dim, isl_dim_param), goto error);
2169 if (isl_dim_total(bmap1->dim) ==
2170 isl_dim_size(bmap1->dim, isl_dim_param) &&
2171 isl_dim_total(bmap2->dim) !=
2172 isl_dim_size(bmap2->dim, isl_dim_param))
2173 return isl_basic_map_intersect(bmap2, bmap1);
2175 if (isl_dim_total(bmap2->dim) !=
2176 isl_dim_size(bmap2->dim, isl_dim_param))
2177 isl_assert(bmap1->ctx,
2178 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2180 if (bmap1->sample &&
2181 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2182 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2183 sample = isl_vec_copy(bmap1->sample);
2184 else if (bmap2->sample &&
2185 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2186 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2187 sample = isl_vec_copy(bmap2->sample);
2189 bmap1 = isl_basic_map_cow(bmap1);
2192 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2193 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2194 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2197 isl_vec_free(sample);
2199 isl_vec_free(bmap1->sample);
2200 bmap1->sample = sample;
2203 bmap1 = isl_basic_map_simplify(bmap1);
2204 return isl_basic_map_finalize(bmap1);
2207 isl_vec_free(sample);
2208 isl_basic_map_free(bmap1);
2209 isl_basic_map_free(bmap2);
2213 struct isl_basic_set *isl_basic_set_intersect(
2214 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2216 return (struct isl_basic_set *)
2217 isl_basic_map_intersect(
2218 (struct isl_basic_map *)bset1,
2219 (struct isl_basic_map *)bset2);
2222 /* Special case of isl_map_intersect, where both map1 and map2
2223 * are convex, without any divs and such that either map1 or map2
2224 * contains a single constraint. This constraint is then simply
2225 * added to the other map.
2227 static __isl_give isl_map *map_intersect_add_constraint(
2228 __isl_take isl_map *map1, __isl_take isl_map *map2)
2230 isl_assert(map1->ctx, map1->n == 1, goto error);
2231 isl_assert(map2->ctx, map1->n == 1, goto error);
2232 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2233 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2235 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2236 return isl_map_intersect(map2, map1);
2238 isl_assert(map2->ctx,
2239 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2241 map1 = isl_map_cow(map1);
2244 if (isl_map_plain_is_empty(map1)) {
2248 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2249 if (map2->p[0]->n_eq == 1)
2250 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2252 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2253 map2->p[0]->ineq[0]);
2255 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2256 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2260 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2261 isl_basic_map_free(map1->p[0]);
2274 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2277 struct isl_map *result;
2283 if (isl_map_plain_is_empty(map1)) {
2287 if (isl_map_plain_is_empty(map2)) {
2292 if (map1->n == 1 && map2->n == 1 &&
2293 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2294 isl_dim_equal(map1->dim, map2->dim) &&
2295 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2296 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2297 return map_intersect_add_constraint(map1, map2);
2298 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2299 map2->dim, isl_dim_param), goto error);
2300 if (isl_dim_total(map1->dim) ==
2301 isl_dim_size(map1->dim, isl_dim_param) &&
2302 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2303 return isl_map_intersect(map2, map1);
2305 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2306 isl_assert(map1->ctx,
2307 isl_dim_equal(map1->dim, map2->dim), goto error);
2309 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2310 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2311 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2313 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2314 map1->n * map2->n, flags);
2317 for (i = 0; i < map1->n; ++i)
2318 for (j = 0; j < map2->n; ++j) {
2319 struct isl_basic_map *part;
2320 part = isl_basic_map_intersect(
2321 isl_basic_map_copy(map1->p[i]),
2322 isl_basic_map_copy(map2->p[j]));
2323 if (isl_basic_map_is_empty(part))
2324 isl_basic_map_free(part);
2326 result = isl_map_add_basic_map(result, part);
2339 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2341 return (struct isl_set *)
2342 isl_map_intersect((struct isl_map *)set1,
2343 (struct isl_map *)set2);
2346 /* The current implementation of isl_map_intersect accepts intersections
2347 * with parameter domains, so we can just call that for now.
2349 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
2350 __isl_take isl_set *params)
2352 return isl_map_intersect(map, params);
2355 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2356 __isl_take isl_set *params)
2358 return isl_map_intersect_params(set, params);
2361 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2363 struct isl_dim *dim;
2364 struct isl_basic_set *bset;
2369 bmap = isl_basic_map_cow(bmap);
2372 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2373 in = isl_basic_map_n_in(bmap);
2374 bset = isl_basic_set_from_basic_map(bmap);
2375 bset = isl_basic_set_swap_vars(bset, in);
2376 return isl_basic_map_from_basic_set(bset, dim);
2379 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2380 enum isl_dim_type type, unsigned pos, unsigned n)
2382 struct isl_dim *res_dim;
2383 struct isl_basic_map *res;
2384 struct isl_dim_map *dim_map;
2385 unsigned total, off;
2386 enum isl_dim_type t;
2394 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2396 total = isl_basic_map_total_dim(bmap) + n;
2397 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2399 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2401 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2403 unsigned size = isl_basic_map_dim(bmap, t);
2404 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2406 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2407 pos, size - pos, off + pos + n);
2409 off += isl_dim_size(res_dim, t);
2411 isl_dim_map_div(dim_map, bmap, off);
2413 res = isl_basic_map_alloc_dim(res_dim,
2414 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2415 if (isl_basic_map_is_rational(bmap))
2416 res = isl_basic_map_set_rational(res);
2417 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2418 return isl_basic_map_finalize(res);
2421 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2422 enum isl_dim_type type, unsigned n)
2426 return isl_basic_map_insert(bmap, type,
2427 isl_basic_map_dim(bmap, type), n);
2430 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2431 enum isl_dim_type type, unsigned n)
2435 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2436 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2438 isl_basic_set_free(bset);
2442 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2443 enum isl_dim_type type, unsigned pos, unsigned n)
2450 map = isl_map_cow(map);
2454 map->dim = isl_dim_insert(map->dim, type, pos, n);
2458 for (i = 0; i < map->n; ++i) {
2459 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2470 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2471 enum isl_dim_type type, unsigned pos, unsigned n)
2473 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2476 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2477 enum isl_dim_type type, unsigned n)
2481 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2484 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2485 enum isl_dim_type type, unsigned n)
2489 isl_assert(set->ctx, type != isl_dim_in, goto error);
2490 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2496 __isl_give isl_basic_map *isl_basic_map_move_dims(
2497 __isl_take isl_basic_map *bmap,
2498 enum isl_dim_type dst_type, unsigned dst_pos,
2499 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2501 struct isl_dim_map *dim_map;
2502 struct isl_basic_map *res;
2503 enum isl_dim_type t;
2504 unsigned total, off;
2511 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2514 if (dst_type == src_type && dst_pos == src_pos)
2517 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2519 if (pos(bmap->dim, dst_type) + dst_pos ==
2520 pos(bmap->dim, src_type) + src_pos +
2521 ((src_type < dst_type) ? n : 0)) {
2522 bmap = isl_basic_map_cow(bmap);
2526 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2527 src_type, src_pos, n);
2531 bmap = isl_basic_map_finalize(bmap);
2536 total = isl_basic_map_total_dim(bmap);
2537 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2540 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2541 unsigned size = isl_dim_size(bmap->dim, t);
2542 if (t == dst_type) {
2543 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2546 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2549 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2550 dst_pos, size - dst_pos, off);
2551 off += size - dst_pos;
2552 } else if (t == src_type) {
2553 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2556 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2557 src_pos + n, size - src_pos - n, off);
2558 off += size - src_pos - n;
2560 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2564 isl_dim_map_div(dim_map, bmap, off);
2566 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2567 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2568 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2570 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2571 src_type, src_pos, n);
2575 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2576 bmap = isl_basic_map_gauss(bmap, NULL);
2577 bmap = isl_basic_map_finalize(bmap);
2581 isl_basic_map_free(bmap);
2585 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2586 enum isl_dim_type dst_type, unsigned dst_pos,
2587 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2589 return (isl_basic_set *)isl_basic_map_move_dims(
2590 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2593 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2594 enum isl_dim_type dst_type, unsigned dst_pos,
2595 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2599 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2600 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2601 src_type, src_pos, n);
2607 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2608 enum isl_dim_type dst_type, unsigned dst_pos,
2609 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2618 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2621 if (dst_type == src_type && dst_pos == src_pos)
2624 isl_assert(map->ctx, dst_type != src_type, goto error);
2626 map = isl_map_cow(map);
2630 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2634 for (i = 0; i < map->n; ++i) {
2635 map->p[i] = isl_basic_map_move_dims(map->p[i],
2637 src_type, src_pos, n);
2648 /* Move the specified dimensions to the last columns right before
2649 * the divs. Don't change the dimension specification of bmap.
2650 * That's the responsibility of the caller.
2652 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2653 enum isl_dim_type type, unsigned first, unsigned n)
2655 struct isl_dim_map *dim_map;
2656 struct isl_basic_map *res;
2657 enum isl_dim_type t;
2658 unsigned total, off;
2662 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2665 total = isl_basic_map_total_dim(bmap);
2666 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2669 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2670 unsigned size = isl_dim_size(bmap->dim, t);
2672 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2675 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2676 first, n, total - bmap->n_div - n);
2677 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2678 first + n, size - (first + n), off);
2679 off += size - (first + n);
2681 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2685 isl_dim_map_div(dim_map, bmap, off + n);
2687 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2688 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2689 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2693 /* Turn the n dimensions of type type, starting at first
2694 * into existentially quantified variables.
2696 __isl_give isl_basic_map *isl_basic_map_project_out(
2697 __isl_take isl_basic_map *bmap,
2698 enum isl_dim_type type, unsigned first, unsigned n)
2711 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2712 return isl_basic_map_remove_dims(bmap, type, first, n);
2714 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2717 bmap = move_last(bmap, type, first, n);
2718 bmap = isl_basic_map_cow(bmap);
2722 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2723 old = bmap->block2.data;
2724 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2725 (bmap->extra + n) * (1 + row_size));
2726 if (!bmap->block2.data)
2728 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2731 for (i = 0; i < n; ++i) {
2732 new_div[i] = bmap->block2.data +
2733 (bmap->extra + i) * (1 + row_size);
2734 isl_seq_clr(new_div[i], 1 + row_size);
2736 for (i = 0; i < bmap->extra; ++i)
2737 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2739 bmap->div = new_div;
2743 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2746 bmap = isl_basic_map_simplify(bmap);
2747 bmap = isl_basic_map_drop_redundant_divs(bmap);
2748 return isl_basic_map_finalize(bmap);
2750 isl_basic_map_free(bmap);
2754 /* Turn the n dimensions of type type, starting at first
2755 * into existentially quantified variables.
2757 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2758 enum isl_dim_type type, unsigned first, unsigned n)
2760 return (isl_basic_set *)isl_basic_map_project_out(
2761 (isl_basic_map *)bset, type, first, n);
2764 /* Turn the n dimensions of type type, starting at first
2765 * into existentially quantified variables.
2767 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2768 enum isl_dim_type type, unsigned first, unsigned n)
2778 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2780 map = isl_map_cow(map);
2784 map->dim = isl_dim_drop(map->dim, type, first, n);
2788 for (i = 0; i < map->n; ++i) {
2789 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2800 /* Turn the n dimensions of type type, starting at first
2801 * into existentially quantified variables.
2803 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2804 enum isl_dim_type type, unsigned first, unsigned n)
2806 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2809 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2813 for (i = 0; i < n; ++i) {
2814 j = isl_basic_map_alloc_div(bmap);
2817 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2821 isl_basic_map_free(bmap);
2825 struct isl_basic_map *isl_basic_map_apply_range(
2826 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2828 struct isl_dim *dim_result = NULL;
2829 struct isl_basic_map *bmap;
2830 unsigned n_in, n_out, n, nparam, total, pos;
2831 struct isl_dim_map *dim_map1, *dim_map2;
2833 if (!bmap1 || !bmap2)
2836 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2837 isl_dim_copy(bmap2->dim));
2839 n_in = isl_basic_map_n_in(bmap1);
2840 n_out = isl_basic_map_n_out(bmap2);
2841 n = isl_basic_map_n_out(bmap1);
2842 nparam = isl_basic_map_n_param(bmap1);
2844 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2845 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2846 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2847 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2848 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2849 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2850 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2851 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2852 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2853 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2854 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2856 bmap = isl_basic_map_alloc_dim(dim_result,
2857 bmap1->n_div + bmap2->n_div + n,
2858 bmap1->n_eq + bmap2->n_eq,
2859 bmap1->n_ineq + bmap2->n_ineq);
2860 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2861 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2862 bmap = add_divs(bmap, n);
2863 bmap = isl_basic_map_simplify(bmap);
2864 bmap = isl_basic_map_drop_redundant_divs(bmap);
2865 return isl_basic_map_finalize(bmap);
2867 isl_basic_map_free(bmap1);
2868 isl_basic_map_free(bmap2);
2872 struct isl_basic_set *isl_basic_set_apply(
2873 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2878 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2881 return (struct isl_basic_set *)
2882 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2884 isl_basic_set_free(bset);
2885 isl_basic_map_free(bmap);
2889 struct isl_basic_map *isl_basic_map_apply_domain(
2890 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2892 if (!bmap1 || !bmap2)
2895 isl_assert(bmap1->ctx,
2896 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2897 isl_assert(bmap1->ctx,
2898 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2901 bmap1 = isl_basic_map_reverse(bmap1);
2902 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2903 return isl_basic_map_reverse(bmap1);
2905 isl_basic_map_free(bmap1);
2906 isl_basic_map_free(bmap2);
2910 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2911 * A \cap B -> f(A) + f(B)
2913 struct isl_basic_map *isl_basic_map_sum(
2914 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2916 unsigned n_in, n_out, nparam, total, pos;
2917 struct isl_basic_map *bmap = NULL;
2918 struct isl_dim_map *dim_map1, *dim_map2;
2921 if (!bmap1 || !bmap2)
2924 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2927 nparam = isl_basic_map_n_param(bmap1);
2928 n_in = isl_basic_map_n_in(bmap1);
2929 n_out = isl_basic_map_n_out(bmap1);
2931 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2932 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2933 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2934 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2935 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2936 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2937 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2938 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2939 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2940 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2941 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2943 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2944 bmap1->n_div + bmap2->n_div + 2 * n_out,
2945 bmap1->n_eq + bmap2->n_eq + n_out,
2946 bmap1->n_ineq + bmap2->n_ineq);
2947 for (i = 0; i < n_out; ++i) {
2948 int j = isl_basic_map_alloc_equality(bmap);
2951 isl_seq_clr(bmap->eq[j], 1+total);
2952 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2953 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2954 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2956 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2957 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2958 bmap = add_divs(bmap, 2 * n_out);
2960 bmap = isl_basic_map_simplify(bmap);
2961 return isl_basic_map_finalize(bmap);
2963 isl_basic_map_free(bmap);
2964 isl_basic_map_free(bmap1);
2965 isl_basic_map_free(bmap2);
2969 /* Given two maps A -> f(A) and B -> g(B), construct a map
2970 * A \cap B -> f(A) + f(B)
2972 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2974 struct isl_map *result;
2980 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2982 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2983 map1->n * map2->n, 0);
2986 for (i = 0; i < map1->n; ++i)
2987 for (j = 0; j < map2->n; ++j) {
2988 struct isl_basic_map *part;
2989 part = isl_basic_map_sum(
2990 isl_basic_map_copy(map1->p[i]),
2991 isl_basic_map_copy(map2->p[j]));
2992 if (isl_basic_map_is_empty(part))
2993 isl_basic_map_free(part);
2995 result = isl_map_add_basic_map(result, part);
3008 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3009 __isl_take isl_set *set2)
3011 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3014 /* Given a basic map A -> f(A), construct A -> -f(A).
3016 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3021 bmap = isl_basic_map_cow(bmap);
3025 n = isl_basic_map_dim(bmap, isl_dim_out);
3026 off = isl_basic_map_offset(bmap, isl_dim_out);
3027 for (i = 0; i < bmap->n_eq; ++i)
3028 for (j = 0; j < n; ++j)
3029 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3030 for (i = 0; i < bmap->n_ineq; ++i)
3031 for (j = 0; j < n; ++j)
3032 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3033 for (i = 0; i < bmap->n_div; ++i)
3034 for (j = 0; j < n; ++j)
3035 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3036 return isl_basic_map_finalize(bmap);
3039 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3041 return isl_basic_map_neg(bset);
3044 /* Given a map A -> f(A), construct A -> -f(A).
3046 struct isl_map *isl_map_neg(struct isl_map *map)
3050 map = isl_map_cow(map);
3054 for (i = 0; i < map->n; ++i) {
3055 map->p[i] = isl_basic_map_neg(map->p[i]);
3066 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3068 return (isl_set *)isl_map_neg((isl_map *)set);
3071 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3072 * A -> floor(f(A)/d).
3074 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3077 unsigned n_in, n_out, nparam, total, pos;
3078 struct isl_basic_map *result = NULL;
3079 struct isl_dim_map *dim_map;
3085 nparam = isl_basic_map_n_param(bmap);
3086 n_in = isl_basic_map_n_in(bmap);
3087 n_out = isl_basic_map_n_out(bmap);
3089 total = nparam + n_in + n_out + bmap->n_div + n_out;
3090 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3091 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3092 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3093 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3094 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3096 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3097 bmap->n_div + n_out,
3098 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3099 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3100 result = add_divs(result, n_out);
3101 for (i = 0; i < n_out; ++i) {
3103 j = isl_basic_map_alloc_inequality(result);
3106 isl_seq_clr(result->ineq[j], 1+total);
3107 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3108 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3109 j = isl_basic_map_alloc_inequality(result);
3112 isl_seq_clr(result->ineq[j], 1+total);
3113 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3114 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3115 isl_int_sub_ui(result->ineq[j][0], d, 1);
3118 result = isl_basic_map_simplify(result);
3119 return isl_basic_map_finalize(result);
3121 isl_basic_map_free(result);
3125 /* Given a map A -> f(A) and an integer d, construct a map
3126 * A -> floor(f(A)/d).
3128 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3132 map = isl_map_cow(map);
3136 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3137 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3138 for (i = 0; i < map->n; ++i) {
3139 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3150 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3156 i = isl_basic_map_alloc_equality(bmap);
3159 nparam = isl_basic_map_n_param(bmap);
3160 n_in = isl_basic_map_n_in(bmap);
3161 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3162 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3163 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3164 return isl_basic_map_finalize(bmap);
3166 isl_basic_map_free(bmap);
3170 /* Add a constraints to "bmap" expressing i_pos < o_pos
3172 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3178 i = isl_basic_map_alloc_inequality(bmap);
3181 nparam = isl_basic_map_n_param(bmap);
3182 n_in = isl_basic_map_n_in(bmap);
3183 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3184 isl_int_set_si(bmap->ineq[i][0], -1);
3185 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3186 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3187 return isl_basic_map_finalize(bmap);
3189 isl_basic_map_free(bmap);
3193 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3195 static __isl_give isl_basic_map *var_less_or_equal(
3196 __isl_take isl_basic_map *bmap, unsigned pos)
3202 i = isl_basic_map_alloc_inequality(bmap);
3205 nparam = isl_basic_map_n_param(bmap);
3206 n_in = isl_basic_map_n_in(bmap);
3207 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3208 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3209 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3210 return isl_basic_map_finalize(bmap);
3212 isl_basic_map_free(bmap);
3216 /* Add a constraints to "bmap" expressing i_pos > o_pos
3218 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3224 i = isl_basic_map_alloc_inequality(bmap);
3227 nparam = isl_basic_map_n_param(bmap);
3228 n_in = isl_basic_map_n_in(bmap);
3229 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3230 isl_int_set_si(bmap->ineq[i][0], -1);
3231 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3232 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3233 return isl_basic_map_finalize(bmap);
3235 isl_basic_map_free(bmap);
3239 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3241 static __isl_give isl_basic_map *var_more_or_equal(
3242 __isl_take isl_basic_map *bmap, unsigned pos)
3248 i = isl_basic_map_alloc_inequality(bmap);
3251 nparam = isl_basic_map_n_param(bmap);
3252 n_in = isl_basic_map_n_in(bmap);
3253 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3254 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3255 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3256 return isl_basic_map_finalize(bmap);
3258 isl_basic_map_free(bmap);
3262 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3265 struct isl_basic_map *bmap;
3266 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3269 for (i = 0; i < n_equal && bmap; ++i)
3270 bmap = var_equal(bmap, i);
3271 return isl_basic_map_finalize(bmap);
3274 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3276 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3279 struct isl_basic_map *bmap;
3280 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3283 for (i = 0; i < pos && bmap; ++i)
3284 bmap = var_equal(bmap, i);
3286 bmap = var_less(bmap, pos);
3287 return isl_basic_map_finalize(bmap);
3290 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3292 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3293 __isl_take isl_dim *dim, unsigned pos)
3296 isl_basic_map *bmap;
3298 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3299 for (i = 0; i < pos; ++i)
3300 bmap = var_equal(bmap, i);
3301 bmap = var_less_or_equal(bmap, pos);
3302 return isl_basic_map_finalize(bmap);
3305 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3307 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3310 struct isl_basic_map *bmap;
3311 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3314 for (i = 0; i < pos && bmap; ++i)
3315 bmap = var_equal(bmap, i);
3317 bmap = var_more(bmap, pos);
3318 return isl_basic_map_finalize(bmap);
3321 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3323 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3324 __isl_take isl_dim *dim, unsigned pos)
3327 isl_basic_map *bmap;
3329 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3330 for (i = 0; i < pos; ++i)
3331 bmap = var_equal(bmap, i);
3332 bmap = var_more_or_equal(bmap, pos);
3333 return isl_basic_map_finalize(bmap);
3336 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3337 unsigned n, int equal)
3339 struct isl_map *map;
3342 if (n == 0 && equal)
3343 return isl_map_universe(dims);
3345 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3347 for (i = 0; i + 1 < n; ++i)
3348 map = isl_map_add_basic_map(map,
3349 isl_basic_map_less_at(isl_dim_copy(dims), i));
3352 map = isl_map_add_basic_map(map,
3353 isl_basic_map_less_or_equal_at(dims, n - 1));
3355 map = isl_map_add_basic_map(map,
3356 isl_basic_map_less_at(dims, n - 1));
3363 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3367 return map_lex_lte_first(dims, dims->n_out, equal);
3370 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3372 return map_lex_lte_first(dim, n, 0);
3375 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3377 return map_lex_lte_first(dim, n, 1);
3380 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3382 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3385 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3387 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3390 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3391 unsigned n, int equal)
3393 struct isl_map *map;
3396 if (n == 0 && equal)
3397 return isl_map_universe(dims);
3399 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3401 for (i = 0; i + 1 < n; ++i)
3402 map = isl_map_add_basic_map(map,
3403 isl_basic_map_more_at(isl_dim_copy(dims), i));
3406 map = isl_map_add_basic_map(map,
3407 isl_basic_map_more_or_equal_at(dims, n - 1));
3409 map = isl_map_add_basic_map(map,
3410 isl_basic_map_more_at(dims, n - 1));
3417 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3421 return map_lex_gte_first(dims, dims->n_out, equal);
3424 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3426 return map_lex_gte_first(dim, n, 0);
3429 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3431 return map_lex_gte_first(dim, n, 1);
3434 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3436 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3439 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3441 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3444 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3445 __isl_take isl_set *set2)
3448 map = isl_map_lex_le(isl_set_get_dim(set1));
3449 map = isl_map_intersect_domain(map, set1);
3450 map = isl_map_intersect_range(map, set2);
3454 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3455 __isl_take isl_set *set2)
3458 map = isl_map_lex_lt(isl_set_get_dim(set1));
3459 map = isl_map_intersect_domain(map, set1);
3460 map = isl_map_intersect_range(map, set2);
3464 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3465 __isl_take isl_set *set2)
3468 map = isl_map_lex_ge(isl_set_get_dim(set1));
3469 map = isl_map_intersect_domain(map, set1);
3470 map = isl_map_intersect_range(map, set2);
3474 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3475 __isl_take isl_set *set2)
3478 map = isl_map_lex_gt(isl_set_get_dim(set1));
3479 map = isl_map_intersect_domain(map, set1);
3480 map = isl_map_intersect_range(map, set2);
3484 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3485 __isl_take isl_map *map2)
3488 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3489 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3490 map = isl_map_apply_range(map, isl_map_reverse(map2));
3494 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3495 __isl_take isl_map *map2)
3498 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3499 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3500 map = isl_map_apply_range(map, isl_map_reverse(map2));
3504 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3505 __isl_take isl_map *map2)
3508 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3509 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3510 map = isl_map_apply_range(map, isl_map_reverse(map2));
3514 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3515 __isl_take isl_map *map2)
3518 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3519 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3520 map = isl_map_apply_range(map, isl_map_reverse(map2));
3524 struct isl_basic_map *isl_basic_map_from_basic_set(
3525 struct isl_basic_set *bset, struct isl_dim *dim)
3527 struct isl_basic_map *bmap;
3529 bset = isl_basic_set_cow(bset);
3533 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3534 isl_dim_free(bset->dim);
3535 bmap = (struct isl_basic_map *) bset;
3537 return isl_basic_map_finalize(bmap);
3539 isl_basic_set_free(bset);
3544 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3548 if (bmap->dim->n_in == 0)
3549 return (struct isl_basic_set *)bmap;
3550 bmap = isl_basic_map_cow(bmap);
3553 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3556 bmap = isl_basic_map_finalize(bmap);
3557 return (struct isl_basic_set *)bmap;
3559 isl_basic_map_free(bmap);
3563 /* For a div d = floor(f/m), add the constraints
3566 * -(f-(n-1)) + m d >= 0
3568 * Note that the second constraint is the negation of
3572 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3573 unsigned pos, isl_int *div)
3576 unsigned total = isl_basic_map_total_dim(bmap);
3578 i = isl_basic_map_alloc_inequality(bmap);
3581 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3582 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3584 j = isl_basic_map_alloc_inequality(bmap);
3587 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3588 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3589 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3593 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3594 unsigned pos, isl_int *div)
3596 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3600 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3602 unsigned total = isl_basic_map_total_dim(bmap);
3603 unsigned div_pos = total - bmap->n_div + div;
3605 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3609 struct isl_basic_set *isl_basic_map_underlying_set(
3610 struct isl_basic_map *bmap)
3614 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3616 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3617 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3618 return (struct isl_basic_set *)bmap;
3619 bmap = isl_basic_map_cow(bmap);
3622 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3625 bmap->extra -= bmap->n_div;
3627 bmap = isl_basic_map_finalize(bmap);
3628 return (struct isl_basic_set *)bmap;
3633 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3634 __isl_take isl_basic_set *bset)
3636 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3639 struct isl_basic_map *isl_basic_map_overlying_set(
3640 struct isl_basic_set *bset, struct isl_basic_map *like)
3642 struct isl_basic_map *bmap;
3643 struct isl_ctx *ctx;
3650 isl_assert(ctx, bset->n_div == 0, goto error);
3651 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3652 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3654 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3655 isl_basic_map_free(like);
3656 return (struct isl_basic_map *)bset;
3658 bset = isl_basic_set_cow(bset);
3661 total = bset->dim->n_out + bset->extra;
3662 bmap = (struct isl_basic_map *)bset;
3663 isl_dim_free(bmap->dim);
3664 bmap->dim = isl_dim_copy(like->dim);
3667 bmap->n_div = like->n_div;
3668 bmap->extra += like->n_div;
3672 ltotal = total - bmap->extra + like->extra;
3675 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3676 bmap->extra * (1 + 1 + total));
3677 if (isl_blk_is_error(bmap->block2))
3679 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3683 for (i = 0; i < bmap->extra; ++i)
3684 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3685 for (i = 0; i < like->n_div; ++i) {
3686 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3687 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3689 bmap = isl_basic_map_extend_constraints(bmap,
3690 0, 2 * like->n_div);
3691 for (i = 0; i < like->n_div; ++i) {
3692 if (isl_int_is_zero(bmap->div[i][0]))
3694 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3698 isl_basic_map_free(like);
3699 bmap = isl_basic_map_simplify(bmap);
3700 bmap = isl_basic_map_finalize(bmap);
3703 isl_basic_map_free(like);
3704 isl_basic_set_free(bset);
3708 struct isl_basic_set *isl_basic_set_from_underlying_set(
3709 struct isl_basic_set *bset, struct isl_basic_set *like)
3711 return (struct isl_basic_set *)
3712 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3715 struct isl_set *isl_set_from_underlying_set(
3716 struct isl_set *set, struct isl_basic_set *like)
3722 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3724 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3725 isl_basic_set_free(like);
3728 set = isl_set_cow(set);
3731 for (i = 0; i < set->n; ++i) {
3732 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3733 isl_basic_set_copy(like));
3737 isl_dim_free(set->dim);
3738 set->dim = isl_dim_copy(like->dim);
3741 isl_basic_set_free(like);
3744 isl_basic_set_free(like);
3749 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3753 map = isl_map_cow(map);
3756 map->dim = isl_dim_cow(map->dim);
3760 for (i = 1; i < map->n; ++i)
3761 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3763 for (i = 0; i < map->n; ++i) {
3764 map->p[i] = (struct isl_basic_map *)
3765 isl_basic_map_underlying_set(map->p[i]);
3770 map->dim = isl_dim_underlying(map->dim, 0);
3772 isl_dim_free(map->dim);
3773 map->dim = isl_dim_copy(map->p[0]->dim);
3777 return (struct isl_set *)map;
3783 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3785 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3788 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3789 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3791 bmap = isl_basic_map_cow(bmap);
3795 isl_dim_free(bmap->dim);
3798 bmap = isl_basic_map_finalize(bmap);
3802 isl_basic_map_free(bmap);
3807 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3808 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3810 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3814 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3815 __isl_take isl_dim *dim)
3819 map = isl_map_cow(map);
3823 for (i = 0; i < map->n; ++i) {
3824 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3829 isl_dim_free(map->dim);
3839 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3840 __isl_take isl_dim *dim)
3842 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3845 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3848 struct isl_basic_set *domain;
3854 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3856 n_in = isl_basic_map_n_in(bmap);
3857 n_out = isl_basic_map_n_out(bmap);
3858 domain = isl_basic_set_from_basic_map(bmap);
3859 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3861 domain = isl_basic_set_reset_dim(domain, dim);
3866 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
3870 return isl_dim_may_be_set(bmap->dim);
3873 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3877 if (isl_basic_map_may_be_set(bmap))
3879 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3882 __isl_give isl_basic_map *isl_basic_map_domain_map(
3883 __isl_take isl_basic_map *bmap)
3887 isl_basic_map *domain;
3888 int nparam, n_in, n_out;
3891 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3892 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3893 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3895 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3896 domain = isl_basic_map_universe(dim);
3898 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3899 bmap = isl_basic_map_apply_range(bmap, domain);
3900 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3902 total = isl_basic_map_total_dim(bmap);
3904 for (i = 0; i < n_in; ++i) {
3905 k = isl_basic_map_alloc_equality(bmap);
3908 isl_seq_clr(bmap->eq[k], 1 + total);
3909 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3910 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3913 bmap = isl_basic_map_gauss(bmap, NULL);
3914 return isl_basic_map_finalize(bmap);
3916 isl_basic_map_free(bmap);
3920 __isl_give isl_basic_map *isl_basic_map_range_map(
3921 __isl_take isl_basic_map *bmap)
3925 isl_basic_map *range;
3926 int nparam, n_in, n_out;
3929 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3930 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3931 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3933 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3934 range = isl_basic_map_universe(dim);
3936 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3937 bmap = isl_basic_map_apply_range(bmap, range);
3938 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3940 total = isl_basic_map_total_dim(bmap);
3942 for (i = 0; i < n_out; ++i) {
3943 k = isl_basic_map_alloc_equality(bmap);
3946 isl_seq_clr(bmap->eq[k], 1 + total);
3947 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3948 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3951 bmap = isl_basic_map_gauss(bmap, NULL);
3952 return isl_basic_map_finalize(bmap);
3954 isl_basic_map_free(bmap);
3958 int isl_map_may_be_set(__isl_keep isl_map *map)
3962 return isl_dim_may_be_set(map->dim);
3965 struct isl_set *isl_map_range(struct isl_map *map)
3968 struct isl_set *set;
3972 if (isl_map_may_be_set(map))
3973 return (isl_set *)map;
3975 map = isl_map_cow(map);
3979 set = (struct isl_set *) map;
3980 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3983 for (i = 0; i < map->n; ++i) {
3984 set->p[i] = isl_basic_map_range(map->p[i]);
3988 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3989 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3996 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3999 isl_dim *domain_dim;
4001 map = isl_map_cow(map);
4005 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
4006 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
4007 map->dim = isl_dim_join(map->dim, domain_dim);
4010 for (i = 0; i < map->n; ++i) {
4011 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4015 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4016 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4023 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4028 map = isl_map_cow(map);
4032 range_dim = isl_dim_range(isl_map_get_dim(map));
4033 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
4034 map->dim = isl_dim_join(map->dim, range_dim);
4037 for (i = 0; i < map->n; ++i) {
4038 map->p[i] = isl_basic_map_range_map(map->p[i]);
4042 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4043 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4050 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4053 struct isl_map *map = NULL;
4055 set = isl_set_cow(set);
4058 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4059 map = (struct isl_map *)set;
4060 for (i = 0; i < set->n; ++i) {
4061 map->p[i] = isl_basic_map_from_basic_set(
4062 set->p[i], isl_dim_copy(dim));
4066 isl_dim_free(map->dim);
4075 __isl_give isl_basic_map *isl_basic_map_from_domain(
4076 __isl_take isl_basic_set *bset)
4078 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4081 __isl_give isl_basic_map *isl_basic_map_from_range(
4082 __isl_take isl_basic_set *bset)
4084 return (isl_basic_map *)bset;
4087 struct isl_map *isl_map_from_range(struct isl_set *set)
4089 return (struct isl_map *)set;
4092 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4094 return isl_map_reverse(isl_map_from_range(set));
4097 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4098 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4100 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4101 isl_basic_map_from_range(range));
4104 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4105 __isl_take isl_set *range)
4107 return isl_map_apply_range(isl_map_from_domain(domain),
4108 isl_map_from_range(range));
4111 struct isl_set *isl_set_from_map(struct isl_map *map)
4114 struct isl_set *set = NULL;
4118 map = isl_map_cow(map);
4121 map->dim = isl_dim_as_set_dim(map->dim);
4124 set = (struct isl_set *)map;
4125 for (i = 0; i < map->n; ++i) {
4126 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4136 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4138 struct isl_map *map;
4142 isl_assert(dim->ctx, n >= 0, return NULL);
4143 map = isl_alloc(dim->ctx, struct isl_map,
4144 sizeof(struct isl_map) +
4145 (n - 1) * sizeof(struct isl_basic_map *));
4149 map->ctx = dim->ctx;
4150 isl_ctx_ref(map->ctx);
4162 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4163 unsigned nparam, unsigned in, unsigned out, int n,
4166 struct isl_map *map;
4167 struct isl_dim *dims;
4169 dims = isl_dim_alloc(ctx, nparam, in, out);
4173 map = isl_map_alloc_dim(dims, n, flags);
4177 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4179 struct isl_basic_map *bmap;
4180 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4181 bmap = isl_basic_map_set_to_empty(bmap);
4185 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4187 struct isl_basic_set *bset;
4188 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4189 bset = isl_basic_set_set_to_empty(bset);
4193 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4195 struct isl_basic_map *bmap;
4198 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4199 bmap = isl_basic_map_set_to_empty(bmap);
4203 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4205 struct isl_basic_map *bmap;
4208 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4209 bmap = isl_basic_map_set_to_empty(bmap);
4213 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4215 struct isl_basic_set *bset;
4218 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4219 bset = isl_basic_set_set_to_empty(bset);
4223 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4225 struct isl_basic_map *bmap;
4226 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4227 bmap = isl_basic_map_finalize(bmap);
4231 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4233 struct isl_basic_set *bset;
4234 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4235 bset = isl_basic_set_finalize(bset);
4239 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4242 unsigned total = isl_dim_total(dim);
4243 isl_basic_map *bmap;
4245 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4246 for (i = 0; i < total; ++i) {
4247 int k = isl_basic_map_alloc_inequality(bmap);
4250 isl_seq_clr(bmap->ineq[k], 1 + total);
4251 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4255 isl_basic_map_free(bmap);
4259 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4261 return isl_basic_map_nat_universe(dim);
4264 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4266 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4269 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4271 return isl_map_nat_universe(dim);
4274 __isl_give isl_basic_map *isl_basic_map_universe_like(
4275 __isl_keep isl_basic_map *model)
4279 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4282 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4286 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4289 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4290 __isl_keep isl_set *model)
4294 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4297 struct isl_map *isl_map_empty(struct isl_dim *dim)
4299 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4302 struct isl_map *isl_map_empty_like(struct isl_map *model)
4306 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4309 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4313 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4316 struct isl_set *isl_set_empty(struct isl_dim *dim)
4318 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4321 struct isl_set *isl_set_empty_like(struct isl_set *model)
4325 return isl_set_empty(isl_dim_copy(model->dim));
4328 struct isl_map *isl_map_universe(struct isl_dim *dim)
4330 struct isl_map *map;
4333 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4334 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4338 struct isl_set *isl_set_universe(struct isl_dim *dim)
4340 struct isl_set *set;
4343 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4344 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4348 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4352 return isl_set_universe(isl_dim_copy(model->dim));
4355 struct isl_map *isl_map_dup(struct isl_map *map)
4358 struct isl_map *dup;
4362 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4363 for (i = 0; i < map->n; ++i)
4364 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4368 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4369 __isl_take isl_basic_map *bmap)
4373 if (isl_basic_map_plain_is_empty(bmap)) {
4374 isl_basic_map_free(bmap);
4377 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4378 isl_assert(map->ctx, map->n < map->size, goto error);
4379 map->p[map->n] = bmap;
4381 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4387 isl_basic_map_free(bmap);
4391 void isl_map_free(struct isl_map *map)
4401 isl_ctx_deref(map->ctx);
4402 for (i = 0; i < map->n; ++i)
4403 isl_basic_map_free(map->p[i]);
4404 isl_dim_free(map->dim);
4408 struct isl_map *isl_map_extend(struct isl_map *base,
4409 unsigned nparam, unsigned n_in, unsigned n_out)
4413 base = isl_map_cow(base);
4417 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4420 for (i = 0; i < base->n; ++i) {
4421 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4422 isl_dim_copy(base->dim), 0, 0, 0);
4432 struct isl_set *isl_set_extend(struct isl_set *base,
4433 unsigned nparam, unsigned dim)
4435 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4439 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4440 struct isl_basic_map *bmap, unsigned pos, int value)
4444 bmap = isl_basic_map_cow(bmap);
4445 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4446 j = isl_basic_map_alloc_equality(bmap);
4449 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4450 isl_int_set_si(bmap->eq[j][pos], -1);
4451 isl_int_set_si(bmap->eq[j][0], value);
4452 bmap = isl_basic_map_simplify(bmap);
4453 return isl_basic_map_finalize(bmap);
4455 isl_basic_map_free(bmap);
4459 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4460 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4464 bmap = isl_basic_map_cow(bmap);
4465 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4466 j = isl_basic_map_alloc_equality(bmap);
4469 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4470 isl_int_set_si(bmap->eq[j][pos], -1);
4471 isl_int_set(bmap->eq[j][0], value);
4472 bmap = isl_basic_map_simplify(bmap);
4473 return isl_basic_map_finalize(bmap);
4475 isl_basic_map_free(bmap);
4479 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4480 enum isl_dim_type type, unsigned pos, int value)
4484 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4485 return isl_basic_map_fix_pos_si(bmap,
4486 isl_basic_map_offset(bmap, type) + pos, value);
4488 isl_basic_map_free(bmap);
4492 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4493 enum isl_dim_type type, unsigned pos, isl_int value)
4497 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4498 return isl_basic_map_fix_pos(bmap,
4499 isl_basic_map_offset(bmap, type) + pos, value);
4501 isl_basic_map_free(bmap);
4505 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4506 enum isl_dim_type type, unsigned pos, int value)
4508 return (struct isl_basic_set *)
4509 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4513 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4514 enum isl_dim_type type, unsigned pos, isl_int value)
4516 return (struct isl_basic_set *)
4517 isl_basic_map_fix((struct isl_basic_map *)bset,
4521 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4522 unsigned input, int value)
4524 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4527 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4528 unsigned dim, int value)
4530 return (struct isl_basic_set *)
4531 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4532 isl_dim_set, dim, value);
4535 struct isl_map *isl_map_fix_si(struct isl_map *map,
4536 enum isl_dim_type type, unsigned pos, int value)
4540 map = isl_map_cow(map);
4544 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4545 for (i = 0; i < map->n; ++i) {
4546 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4550 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4557 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4558 enum isl_dim_type type, unsigned pos, int value)
4560 return (struct isl_set *)
4561 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4564 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4565 enum isl_dim_type type, unsigned pos, isl_int value)
4569 map = isl_map_cow(map);
4573 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4574 for (i = 0; i < map->n; ++i) {
4575 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4579 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4586 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4587 enum isl_dim_type type, unsigned pos, isl_int value)
4589 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4592 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4593 unsigned input, int value)
4595 return isl_map_fix_si(map, isl_dim_in, input, value);
4598 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4600 return (struct isl_set *)
4601 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4604 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4605 __isl_take isl_basic_map *bmap,
4606 enum isl_dim_type type, unsigned pos, int value)
4612 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4613 pos += isl_basic_map_offset(bmap, type);
4614 bmap = isl_basic_map_cow(bmap);
4615 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4616 j = isl_basic_map_alloc_inequality(bmap);
4619 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4620 isl_int_set_si(bmap->ineq[j][pos], 1);
4621 isl_int_set_si(bmap->ineq[j][0], -value);
4622 bmap = isl_basic_map_simplify(bmap);
4623 return isl_basic_map_finalize(bmap);
4625 isl_basic_map_free(bmap);
4629 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4630 unsigned dim, isl_int value)
4634 bset = isl_basic_set_cow(bset);
4635 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4636 j = isl_basic_set_alloc_inequality(bset);
4639 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4640 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4641 isl_int_neg(bset->ineq[j][0], value);
4642 bset = isl_basic_set_simplify(bset);
4643 return isl_basic_set_finalize(bset);
4645 isl_basic_set_free(bset);
4649 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4650 enum isl_dim_type type, unsigned pos, int value)
4654 map = isl_map_cow(map);
4658 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4659 for (i = 0; i < map->n; ++i) {
4660 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4665 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4672 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4673 enum isl_dim_type type, unsigned pos, int value)
4675 return (struct isl_set *)
4676 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4679 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4684 set = isl_set_cow(set);
4688 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4689 for (i = 0; i < set->n; ++i) {
4690 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4700 struct isl_map *isl_map_reverse(struct isl_map *map)
4704 map = isl_map_cow(map);
4708 map->dim = isl_dim_reverse(map->dim);
4711 for (i = 0; i < map->n; ++i) {
4712 map->p[i] = isl_basic_map_reverse(map->p[i]);
4716 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4723 static struct isl_map *isl_basic_map_partial_lexopt(
4724 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4725 struct isl_set **empty, int max)
4729 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4730 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4732 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4734 isl_basic_map_free(bmap);
4735 isl_basic_set_free(dom);
4741 struct isl_map *isl_basic_map_partial_lexmax(
4742 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4743 struct isl_set **empty)
4745 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4748 struct isl_map *isl_basic_map_partial_lexmin(
4749 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4750 struct isl_set **empty)
4752 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4755 struct isl_set *isl_basic_set_partial_lexmin(
4756 struct isl_basic_set *bset, struct isl_basic_set *dom,
4757 struct isl_set **empty)
4759 return (struct isl_set *)
4760 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4764 struct isl_set *isl_basic_set_partial_lexmax(
4765 struct isl_basic_set *bset, struct isl_basic_set *dom,
4766 struct isl_set **empty)
4768 return (struct isl_set *)
4769 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4773 /* Given a basic map "bmap", compute the lexicographically minimal
4774 * (or maximal) image element for each domain element in dom.
4775 * Set *empty to those elements in dom that do not have an image element.
4777 * We first make sure the basic sets in dom are disjoint and then
4778 * simply collect the results over each of the basic sets separately.
4779 * We could probably improve the efficiency a bit by moving the union
4780 * domain down into the parametric integer programming.
4782 static __isl_give isl_map *basic_map_partial_lexopt(
4783 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4784 __isl_give isl_set **empty, int max)
4787 struct isl_map *res;
4789 dom = isl_set_make_disjoint(dom);
4793 if (isl_set_plain_is_empty(dom)) {
4794 res = isl_map_empty_like_basic_map(bmap);
4795 *empty = isl_set_empty_like(dom);
4797 isl_basic_map_free(bmap);
4801 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4802 isl_basic_set_copy(dom->p[0]), empty, max);
4804 for (i = 1; i < dom->n; ++i) {
4805 struct isl_map *res_i;
4806 struct isl_set *empty_i;
4808 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4809 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4811 res = isl_map_union_disjoint(res, res_i);
4812 *empty = isl_set_union_disjoint(*empty, empty_i);
4816 isl_basic_map_free(bmap);
4821 isl_basic_map_free(bmap);
4825 /* Given a map "map", compute the lexicographically minimal
4826 * (or maximal) image element for each domain element in dom.
4827 * Set *empty to those elements in dom that do not have an image element.
4829 * We first compute the lexicographically minimal or maximal element
4830 * in the first basic map. This results in a partial solution "res"
4831 * and a subset "todo" of dom that still need to be handled.
4832 * We then consider each of the remaining maps in "map" and successively
4833 * improve both "res" and "todo".
4835 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4836 * Assume we are computing the lexicographical maximum.
4837 * We first compute the lexicographically maximal element in basic map i.
4838 * This results in a partial solution res_i and a subset todo_i.
4839 * Then we combine these results with those obtain for the first k basic maps
4840 * to obtain a result that is valid for the first k+1 basic maps.
4841 * In particular, the set where there is no solution is the set where
4842 * there is no solution for the first k basic maps and also no solution
4843 * for the ith basic map, i.e.,
4845 * todo^i = todo^k * todo_i
4847 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4848 * solutions, arbitrarily breaking ties in favor of res^k.
4849 * That is, when res^k(a) >= res_i(a), we pick res^k and
4850 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4851 * the lexicographic order.)
4852 * In practice, we compute
4854 * res^k * (res_i . "<=")
4858 * res_i * (res^k . "<")
4860 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4861 * where only one of res^k and res_i provides a solution and we simply pick
4868 * Note that we only compute these intersections when dom(res^k) intersects
4869 * dom(res_i). Otherwise, the only effect of these intersections is to
4870 * potentially break up res^k and res_i into smaller pieces.
4871 * We want to avoid such splintering as much as possible.
4872 * In fact, an earlier implementation of this function would look for
4873 * better results in the domain of res^k and for extra results in todo^k,
4874 * but this would always result in a splintering according to todo^k,
4875 * even when the domain of basic map i is disjoint from the domains of
4876 * the previous basic maps.
4878 static __isl_give isl_map *isl_map_partial_lexopt(
4879 __isl_take isl_map *map, __isl_take isl_set *dom,
4880 __isl_give isl_set **empty, int max)
4883 struct isl_map *res;
4884 struct isl_set *todo;
4889 if (isl_map_plain_is_empty(map)) {
4897 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4898 isl_set_copy(dom), &todo, max);
4900 for (i = 1; i < map->n; ++i) {
4904 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4906 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4907 isl_set_copy(dom), &todo_i, max);
4910 lt = isl_map_lex_lt(isl_dim_copy(dim));
4911 le = isl_map_lex_le(dim);
4913 lt = isl_map_lex_gt(isl_dim_copy(dim));
4914 le = isl_map_lex_ge(dim);
4916 lt = isl_map_apply_range(isl_map_copy(res), lt);
4917 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4918 le = isl_map_apply_range(isl_map_copy(res_i), le);
4919 le = isl_map_intersect(le, isl_map_copy(res));
4921 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4922 res = isl_map_intersect_domain(res,
4923 isl_set_copy(todo_i));
4924 res_i = isl_map_intersect_domain(res_i,
4925 isl_set_copy(todo));
4928 res = isl_map_union_disjoint(res, res_i);
4929 res = isl_map_union_disjoint(res, lt);
4930 res = isl_map_union_disjoint(res, le);
4932 todo = isl_set_intersect(todo, todo_i);
4952 __isl_give isl_map *isl_map_partial_lexmax(
4953 __isl_take isl_map *map, __isl_take isl_set *dom,
4954 __isl_give isl_set **empty)
4956 return isl_map_partial_lexopt(map, dom, empty, 1);
4959 __isl_give isl_map *isl_map_partial_lexmin(
4960 __isl_take isl_map *map, __isl_take isl_set *dom,
4961 __isl_give isl_set **empty)
4963 return isl_map_partial_lexopt(map, dom, empty, 0);
4966 __isl_give isl_set *isl_set_partial_lexmin(
4967 __isl_take isl_set *set, __isl_take isl_set *dom,
4968 __isl_give isl_set **empty)
4970 return (struct isl_set *)
4971 isl_map_partial_lexmin((struct isl_map *)set,
4975 __isl_give isl_set *isl_set_partial_lexmax(
4976 __isl_take isl_set *set, __isl_take isl_set *dom,
4977 __isl_give isl_set **empty)
4979 return (struct isl_set *)
4980 isl_map_partial_lexmax((struct isl_map *)set,
4984 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4986 struct isl_basic_set *dom = NULL;
4987 struct isl_dim *dom_dim;
4991 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4992 dom = isl_basic_set_universe(dom_dim);
4993 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4995 isl_basic_map_free(bmap);
4999 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5001 return isl_basic_map_lexopt(bmap, 0);
5004 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5006 return isl_basic_map_lexopt(bmap, 1);
5009 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5011 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5014 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5016 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5019 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5021 struct isl_set *dom = NULL;
5022 struct isl_dim *dom_dim;
5026 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
5027 dom = isl_set_universe(dom_dim);
5028 return isl_map_partial_lexopt(map, dom, NULL, max);
5034 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5036 return isl_map_lexopt(map, 0);
5039 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5041 return isl_map_lexopt(map, 1);
5044 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5046 return (isl_set *)isl_map_lexmin((isl_map *)set);
5049 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5051 return (isl_set *)isl_map_lexmax((isl_map *)set);
5054 /* Construct a map that equates the two given dimensions in the given space.
5056 static __isl_give isl_map *equate(__isl_take isl_dim *dim,
5057 enum isl_dim_type src_type, int src_pos,
5058 enum isl_dim_type dst_type, int dst_pos)
5060 isl_basic_map *bmap;
5063 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
5064 k = isl_basic_map_alloc_equality(bmap);
5067 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
5068 src_pos += isl_basic_map_offset(bmap, src_type);
5069 dst_pos += isl_basic_map_offset(bmap, dst_type);
5070 isl_int_set_si(bmap->eq[k][src_pos], 1);
5071 isl_int_set_si(bmap->eq[k][dst_pos], -1);
5073 return isl_map_from_basic_map(bmap);
5075 isl_basic_map_free(bmap);
5079 /* Extract the first and only affine expression from list
5080 * and then add it to *pwaff with the given dom.
5081 * This domain is known to be disjoint from other domains
5082 * because of the way isl_basic_set_foreach_lexmax works.
5084 static int update_dim_max(__isl_take isl_basic_set *dom,
5085 __isl_take isl_aff_list *list, void *user)
5087 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5089 isl_pw_aff **pwaff = user;
5090 isl_pw_aff *pwaff_i;
5092 if (isl_aff_list_n_aff(list) != 1)
5093 isl_die(ctx, isl_error_internal,
5094 "expecting single element list", goto error);
5096 aff = isl_aff_list_get_aff(list, 0);
5097 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5099 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5101 isl_aff_list_free(list);
5105 isl_basic_set_free(dom);
5106 isl_aff_list_free(list);
5110 /* Given a one-dimensional basic set, compute the maximum of that
5111 * dimension as an isl_pw_aff.
5113 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexmax
5114 * call update_dim_max on each leaf of the result.
5116 static __isl_give isl_pw_aff *basic_set_dim_max(__isl_keep isl_basic_set *bset)
5118 isl_dim *dim = isl_basic_set_get_dim(bset);
5122 dim = isl_dim_domain(isl_dim_from_range(dim));
5123 pwaff = isl_pw_aff_empty(dim);
5125 r = isl_basic_set_foreach_lexmax(bset, &update_dim_max, &pwaff);
5127 return isl_pw_aff_free(pwaff);
5132 /* Compute the maximum of the given set dimension as a function of the
5133 * parameters, but independently of the other set dimensions.
5135 * We first project the set onto the given dimension and then compute
5136 * the "lexicographic" maximum in each basic set, combining the results
5137 * using isl_pw_aff_max.
5139 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5145 map = isl_map_from_domain(set);
5146 map = isl_map_add_dims(map, isl_dim_out, 1);
5147 map = isl_map_intersect(map,
5148 equate(isl_map_get_dim(map), isl_dim_in, pos,
5150 set = isl_map_range(map);
5155 isl_dim *dim = isl_set_get_dim(set);
5156 dim = isl_dim_domain(isl_dim_from_range(dim));
5158 return isl_pw_aff_empty(dim);
5161 pwaff = basic_set_dim_max(set->p[0]);
5162 for (i = 1; i < set->n; ++i) {
5163 isl_pw_aff *pwaff_i;
5165 pwaff_i = basic_set_dim_max(set->p[i]);
5166 pwaff = isl_pw_aff_max(pwaff, pwaff_i);
5174 /* Apply a preimage specified by "mat" on the parameters of "bset".
5175 * bset is assumed to have only parameters and divs.
5177 static struct isl_basic_set *basic_set_parameter_preimage(
5178 struct isl_basic_set *bset, struct isl_mat *mat)
5185 bset->dim = isl_dim_cow(bset->dim);
5189 nparam = isl_basic_set_dim(bset, isl_dim_param);
5191 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5193 bset->dim->nparam = 0;
5194 bset->dim->n_out = nparam;
5195 bset = isl_basic_set_preimage(bset, mat);
5197 bset->dim->nparam = bset->dim->n_out;
5198 bset->dim->n_out = 0;
5203 isl_basic_set_free(bset);
5207 /* Apply a preimage specified by "mat" on the parameters of "set".
5208 * set is assumed to have only parameters and divs.
5210 static struct isl_set *set_parameter_preimage(
5211 struct isl_set *set, struct isl_mat *mat)
5213 struct isl_dim *dim = NULL;
5219 dim = isl_dim_copy(set->dim);
5220 dim = isl_dim_cow(dim);
5224 nparam = isl_set_dim(set, isl_dim_param);
5226 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5229 dim->n_out = nparam;
5230 isl_set_reset_dim(set, dim);
5231 set = isl_set_preimage(set, mat);
5234 dim = isl_dim_copy(set->dim);
5235 dim = isl_dim_cow(dim);
5238 dim->nparam = dim->n_out;
5240 isl_set_reset_dim(set, dim);
5250 /* Intersect the basic set "bset" with the affine space specified by the
5251 * equalities in "eq".
5253 static struct isl_basic_set *basic_set_append_equalities(
5254 struct isl_basic_set *bset, struct isl_mat *eq)
5262 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5267 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5268 for (i = 0; i < eq->n_row; ++i) {
5269 k = isl_basic_set_alloc_equality(bset);
5272 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5273 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5277 bset = isl_basic_set_gauss(bset, NULL);
5278 bset = isl_basic_set_finalize(bset);
5283 isl_basic_set_free(bset);
5287 /* Intersect the set "set" with the affine space specified by the
5288 * equalities in "eq".
5290 static struct isl_set *set_append_equalities(struct isl_set *set,
5298 for (i = 0; i < set->n; ++i) {
5299 set->p[i] = basic_set_append_equalities(set->p[i],
5312 /* Project the given basic set onto its parameter domain, possibly introducing
5313 * new, explicit, existential variables in the constraints.
5314 * The input has parameters and (possibly implicit) existential variables.
5315 * The output has the same parameters, but only
5316 * explicit existentially quantified variables.
5318 * The actual projection is performed by pip, but pip doesn't seem
5319 * to like equalities very much, so we first remove the equalities
5320 * among the parameters by performing a variable compression on
5321 * the parameters. Afterward, an inverse transformation is performed
5322 * and the equalities among the parameters are inserted back in.
5324 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5328 struct isl_mat *T, *T2;
5329 struct isl_set *set;
5330 unsigned nparam, n_div;
5332 bset = isl_basic_set_cow(bset);
5336 if (bset->n_eq == 0)
5337 return isl_basic_set_lexmin(bset);
5339 isl_basic_set_gauss(bset, NULL);
5341 nparam = isl_basic_set_dim(bset, isl_dim_param);
5342 n_div = isl_basic_set_dim(bset, isl_dim_div);
5344 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5345 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5348 if (i == bset->n_eq)
5349 return isl_basic_set_lexmin(bset);
5351 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5353 eq = isl_mat_cow(eq);
5354 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5355 if (T && T->n_col == 0) {
5359 bset = isl_basic_set_set_to_empty(bset);
5360 return isl_set_from_basic_set(bset);
5362 bset = basic_set_parameter_preimage(bset, T);
5364 set = isl_basic_set_lexmin(bset);
5365 set = set_parameter_preimage(set, T2);
5366 set = set_append_equalities(set, eq);
5370 /* Compute an explicit representation for all the existentially
5371 * quantified variables.
5372 * The input and output dimensions are first turned into parameters.
5373 * compute_divs then returns a map with the same parameters and
5374 * no input or output dimensions and the dimension specification
5375 * is reset to that of the input.
5377 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5379 struct isl_basic_set *bset;
5380 struct isl_set *set;
5381 struct isl_map *map;
5382 struct isl_dim *dim, *orig_dim = NULL;
5387 bmap = isl_basic_map_cow(bmap);
5391 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5392 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5393 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5394 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5398 orig_dim = bmap->dim;
5400 bset = (struct isl_basic_set *)bmap;
5402 set = parameter_compute_divs(bset);
5403 map = (struct isl_map *)set;
5404 map = isl_map_reset_dim(map, orig_dim);
5408 isl_basic_map_free(bmap);
5412 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5420 off = isl_dim_total(bmap->dim);
5421 for (i = 0; i < bmap->n_div; ++i) {
5422 if (isl_int_is_zero(bmap->div[i][0]))
5424 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5430 static int map_divs_known(__isl_keep isl_map *map)
5437 for (i = 0; i < map->n; ++i) {
5438 int known = isl_basic_map_divs_known(map->p[i]);
5446 /* If bmap contains any unknown divs, then compute explicit
5447 * expressions for them. However, this computation may be
5448 * quite expensive, so first try to remove divs that aren't
5451 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5454 struct isl_map *map;
5456 known = isl_basic_map_divs_known(bmap);
5460 return isl_map_from_basic_map(bmap);
5462 bmap = isl_basic_map_drop_redundant_divs(bmap);
5464 known = isl_basic_map_divs_known(bmap);
5468 return isl_map_from_basic_map(bmap);
5470 map = compute_divs(bmap);
5473 isl_basic_map_free(bmap);
5477 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5481 struct isl_map *res;
5488 known = map_divs_known(map);
5496 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5497 for (i = 1 ; i < map->n; ++i) {
5499 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5500 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5501 res = isl_map_union_disjoint(res, r2);
5503 res = isl_map_union(res, r2);
5510 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5512 return (struct isl_set *)
5513 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5516 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5518 return (struct isl_set *)
5519 isl_map_compute_divs((struct isl_map *)set);
5522 struct isl_set *isl_map_domain(struct isl_map *map)
5525 struct isl_set *set;
5530 map = isl_map_cow(map);
5534 set = (struct isl_set *)map;
5535 set->dim = isl_dim_domain(set->dim);
5538 for (i = 0; i < map->n; ++i) {
5539 set->p[i] = isl_basic_map_domain(map->p[i]);
5543 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5544 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5551 struct isl_map *isl_map_union_disjoint(
5552 struct isl_map *map1, struct isl_map *map2)
5556 struct isl_map *map = NULL;
5570 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5572 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5573 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5574 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5576 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5577 map1->n + map2->n, flags);
5580 for (i = 0; i < map1->n; ++i) {
5581 map = isl_map_add_basic_map(map,
5582 isl_basic_map_copy(map1->p[i]));
5586 for (i = 0; i < map2->n; ++i) {
5587 map = isl_map_add_basic_map(map,
5588 isl_basic_map_copy(map2->p[i]));
5602 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5604 map1 = isl_map_union_disjoint(map1, map2);
5608 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5612 struct isl_set *isl_set_union_disjoint(
5613 struct isl_set *set1, struct isl_set *set2)
5615 return (struct isl_set *)
5616 isl_map_union_disjoint(
5617 (struct isl_map *)set1, (struct isl_map *)set2);
5620 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5622 return (struct isl_set *)
5623 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5626 struct isl_map *isl_map_intersect_range(
5627 struct isl_map *map, struct isl_set *set)
5630 struct isl_map *result;
5636 if (!isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5637 isl_die(set->ctx, isl_error_invalid,
5638 "parameters don't match", goto error);
5640 if (isl_dim_size(set->dim, isl_dim_set) != 0 &&
5641 !isl_map_compatible_range(map, set))
5642 isl_die(set->ctx, isl_error_invalid,
5643 "incompatible spaces", goto error);
5645 if (isl_set_plain_is_universe(set)) {
5650 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5651 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5652 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5654 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5655 map->n * set->n, flags);
5658 for (i = 0; i < map->n; ++i)
5659 for (j = 0; j < set->n; ++j) {
5660 result = isl_map_add_basic_map(result,
5661 isl_basic_map_intersect_range(
5662 isl_basic_map_copy(map->p[i]),
5663 isl_basic_set_copy(set->p[j])));
5676 struct isl_map *isl_map_intersect_domain(
5677 struct isl_map *map, struct isl_set *set)
5679 return isl_map_reverse(
5680 isl_map_intersect_range(isl_map_reverse(map), set));
5683 struct isl_map *isl_map_apply_domain(
5684 struct isl_map *map1, struct isl_map *map2)
5688 map1 = isl_map_reverse(map1);
5689 map1 = isl_map_apply_range(map1, map2);
5690 return isl_map_reverse(map1);
5697 struct isl_map *isl_map_apply_range(
5698 struct isl_map *map1, struct isl_map *map2)
5700 struct isl_dim *dim_result;
5701 struct isl_map *result;
5707 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5708 isl_dim_copy(map2->dim));
5710 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5713 for (i = 0; i < map1->n; ++i)
5714 for (j = 0; j < map2->n; ++j) {
5715 result = isl_map_add_basic_map(result,
5716 isl_basic_map_apply_range(
5717 isl_basic_map_copy(map1->p[i]),
5718 isl_basic_map_copy(map2->p[j])));
5724 if (result && result->n <= 1)
5725 ISL_F_SET(result, ISL_MAP_DISJOINT);
5734 * returns range - domain
5736 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5738 isl_dim *dims, *target_dim;
5739 struct isl_basic_set *bset;
5746 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5747 bmap->dim, isl_dim_out),
5749 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5750 dim = isl_basic_map_n_in(bmap);
5751 nparam = isl_basic_map_n_param(bmap);
5752 bset = isl_basic_set_from_basic_map(bmap);
5753 bset = isl_basic_set_cow(bset);
5754 dims = isl_basic_set_get_dim(bset);
5755 dims = isl_dim_add(dims, isl_dim_set, dim);
5756 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5757 bset = isl_basic_set_swap_vars(bset, 2*dim);
5758 for (i = 0; i < dim; ++i) {
5759 int j = isl_basic_map_alloc_equality(
5760 (struct isl_basic_map *)bset);
5763 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5764 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5765 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5766 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5768 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5769 bset = isl_basic_set_reset_dim(bset, target_dim);
5772 isl_basic_map_free(bmap);
5777 * returns range - domain
5779 struct isl_set *isl_map_deltas(struct isl_map *map)
5783 struct isl_set *result;
5788 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5789 map->dim, isl_dim_out),
5791 dim = isl_map_get_dim(map);
5792 dim = isl_dim_domain(dim);
5793 result = isl_set_alloc_dim(dim, map->n, 0);
5796 for (i = 0; i < map->n; ++i)
5797 result = isl_set_add_basic_set(result,
5798 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5807 * returns [domain -> range] -> range - domain
5809 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5810 __isl_take isl_basic_map *bmap)
5814 isl_basic_map *domain;
5818 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5819 isl_die(bmap->ctx, isl_error_invalid,
5820 "domain and range don't match", goto error);
5822 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5823 n = isl_basic_map_dim(bmap, isl_dim_in);
5825 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5826 domain = isl_basic_map_universe(dim);
5828 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5829 bmap = isl_basic_map_apply_range(bmap, domain);
5830 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5832 total = isl_basic_map_total_dim(bmap);
5834 for (i = 0; i < n; ++i) {
5835 k = isl_basic_map_alloc_equality(bmap);
5838 isl_seq_clr(bmap->eq[k], 1 + total);
5839 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5840 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5841 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5844 bmap = isl_basic_map_gauss(bmap, NULL);
5845 return isl_basic_map_finalize(bmap);
5847 isl_basic_map_free(bmap);
5852 * returns [domain -> range] -> range - domain
5854 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5857 isl_dim *domain_dim;
5862 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5863 isl_die(map->ctx, isl_error_invalid,
5864 "domain and range don't match", goto error);
5866 map = isl_map_cow(map);
5870 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5871 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5872 map->dim = isl_dim_join(map->dim, domain_dim);
5875 for (i = 0; i < map->n; ++i) {
5876 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5880 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5887 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5889 struct isl_basic_map *bmap;
5897 nparam = dims->nparam;
5899 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5903 for (i = 0; i < dim; ++i) {
5904 int j = isl_basic_map_alloc_equality(bmap);
5907 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5908 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5909 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5911 return isl_basic_map_finalize(bmap);
5913 isl_basic_map_free(bmap);
5917 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5921 if (dim->n_in != dim->n_out)
5922 isl_die(dim->ctx, isl_error_invalid,
5923 "number of input and output dimensions needs to be "
5924 "the same", goto error);
5925 return basic_map_identity(dim);
5931 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5933 if (!model || !model->dim)
5935 return isl_basic_map_identity(isl_dim_copy(model->dim));
5938 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5940 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5943 struct isl_map *isl_map_identity_like(struct isl_map *model)
5945 if (!model || !model->dim)
5947 return isl_map_identity(isl_dim_copy(model->dim));
5950 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5952 if (!model || !model->dim)
5954 return isl_map_identity(isl_dim_copy(model->dim));
5957 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5959 isl_dim *dim = isl_set_get_dim(set);
5961 id = isl_map_identity(isl_dim_map_from_set(dim));
5962 return isl_map_intersect_range(id, set);
5965 /* Construct a basic set with all set dimensions having only non-negative
5968 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5973 struct isl_basic_set *bset;
5977 nparam = dims->nparam;
5979 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5982 for (i = 0; i < dim; ++i) {
5983 int k = isl_basic_set_alloc_inequality(bset);
5986 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5987 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5991 isl_basic_set_free(bset);
5995 /* Construct the half-space x_pos >= 0.
5997 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
6001 isl_basic_set *nonneg;
6003 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
6004 k = isl_basic_set_alloc_inequality(nonneg);
6007 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6008 isl_int_set_si(nonneg->ineq[k][pos], 1);
6010 return isl_basic_set_finalize(nonneg);
6012 isl_basic_set_free(nonneg);
6016 /* Construct the half-space x_pos <= -1.
6018 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
6023 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
6024 k = isl_basic_set_alloc_inequality(neg);
6027 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6028 isl_int_set_si(neg->ineq[k][0], -1);
6029 isl_int_set_si(neg->ineq[k][pos], -1);
6031 return isl_basic_set_finalize(neg);
6033 isl_basic_set_free(neg);
6037 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6038 enum isl_dim_type type, unsigned first, unsigned n)
6041 isl_basic_set *nonneg;
6049 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6051 for (i = 0; i < n; ++i) {
6052 nonneg = nonneg_halfspace(isl_set_get_dim(set),
6053 pos(set->dim, type) + first + i);
6054 neg = neg_halfspace(isl_set_get_dim(set),
6055 pos(set->dim, type) + first + i);
6057 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6066 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6067 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6074 if (isl_set_plain_is_empty(set)) {
6079 return fn(set, signs, user);
6082 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
6084 half = isl_set_intersect(half, isl_set_copy(set));
6085 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6089 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
6091 half = isl_set_intersect(half, set);
6092 return foreach_orthant(half, signs, first + 1, len, fn, user);
6098 /* Call "fn" on the intersections of "set" with each of the orthants
6099 * (except for obviously empty intersections). The orthant is identified
6100 * by the signs array, with each entry having value 1 or -1 according
6101 * to the sign of the corresponding variable.
6103 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6104 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6114 if (isl_set_plain_is_empty(set))
6117 nparam = isl_set_dim(set, isl_dim_param);
6118 nvar = isl_set_dim(set, isl_dim_set);
6120 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6122 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6130 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6132 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6135 int isl_basic_map_is_subset(
6136 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6139 struct isl_map *map1;
6140 struct isl_map *map2;
6142 if (!bmap1 || !bmap2)
6145 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6146 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6148 is_subset = isl_map_is_subset(map1, map2);
6156 int isl_basic_map_is_equal(
6157 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6161 if (!bmap1 || !bmap2)
6163 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6166 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6170 int isl_basic_set_is_equal(
6171 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6173 return isl_basic_map_is_equal(
6174 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6177 int isl_map_is_empty(struct isl_map *map)
6184 for (i = 0; i < map->n; ++i) {
6185 is_empty = isl_basic_map_is_empty(map->p[i]);
6194 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6196 return map ? map->n == 0 : -1;
6199 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6201 return isl_map_plain_is_empty(map);
6204 int isl_set_plain_is_empty(struct isl_set *set)
6206 return set ? set->n == 0 : -1;
6209 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6211 return isl_set_plain_is_empty(set);
6214 int isl_set_is_empty(struct isl_set *set)
6216 return isl_map_is_empty((struct isl_map *)set);
6219 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6224 return isl_dim_equal(map1->dim, map2->dim);
6227 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6232 return isl_dim_equal(set1->dim, set2->dim);
6235 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6241 is_subset = isl_map_is_subset(map1, map2);
6244 is_subset = isl_map_is_subset(map2, map1);
6248 int isl_basic_map_is_strict_subset(
6249 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6253 if (!bmap1 || !bmap2)
6255 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6258 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6259 if (is_subset == -1)
6264 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6270 is_subset = isl_map_is_subset(map1, map2);
6273 is_subset = isl_map_is_subset(map2, map1);
6274 if (is_subset == -1)
6279 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6281 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6284 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6288 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6291 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6295 return bset->n_eq == 0 && bset->n_ineq == 0;
6298 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6305 for (i = 0; i < map->n; ++i) {
6306 int r = isl_basic_map_is_universe(map->p[i]);
6314 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6316 return isl_map_plain_is_universe((isl_map *) set);
6319 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6321 return isl_set_plain_is_universe(set);
6324 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6326 struct isl_basic_set *bset = NULL;
6327 struct isl_vec *sample = NULL;
6334 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6337 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6338 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6339 copy = isl_basic_map_remove_redundancies(copy);
6340 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6341 isl_basic_map_free(copy);
6345 total = 1 + isl_basic_map_total_dim(bmap);
6346 if (bmap->sample && bmap->sample->size == total) {
6347 int contains = isl_basic_map_contains(bmap, bmap->sample);
6353 isl_vec_free(bmap->sample);
6354 bmap->sample = NULL;
6355 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6358 sample = isl_basic_set_sample_vec(bset);
6361 empty = sample->size == 0;
6362 isl_vec_free(bmap->sample);
6363 bmap->sample = sample;
6365 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6370 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6374 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6377 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6379 return isl_basic_map_plain_is_empty(bmap);
6382 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6386 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6389 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6391 return isl_basic_set_plain_is_empty(bset);
6394 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6396 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6399 struct isl_map *isl_basic_map_union(
6400 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6402 struct isl_map *map;
6403 if (!bmap1 || !bmap2)
6406 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6408 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6411 map = isl_map_add_basic_map(map, bmap1);
6412 map = isl_map_add_basic_map(map, bmap2);
6415 isl_basic_map_free(bmap1);
6416 isl_basic_map_free(bmap2);
6420 struct isl_set *isl_basic_set_union(
6421 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6423 return (struct isl_set *)isl_basic_map_union(
6424 (struct isl_basic_map *)bset1,
6425 (struct isl_basic_map *)bset2);
6428 /* Order divs such that any div only depends on previous divs */
6429 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6437 off = isl_dim_total(bmap->dim);
6439 for (i = 0; i < bmap->n_div; ++i) {
6441 if (isl_int_is_zero(bmap->div[i][0]))
6443 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6447 isl_basic_map_swap_div(bmap, i, i + pos);
6453 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6455 return (struct isl_basic_set *)
6456 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6459 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6466 for (i = 0; i < map->n; ++i) {
6467 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6478 /* Apply the expansion computed by isl_merge_divs.
6479 * The expansion itself is given by "exp" while the resulting
6480 * list of divs is given by "div".
6482 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6483 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6488 bset = isl_basic_set_cow(bset);
6492 if (div->n_row < bset->n_div)
6493 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6494 "not an expansion", goto error);
6496 bset = isl_basic_map_extend_dim(bset, isl_dim_copy(bset->dim),
6497 div->n_row - bset->n_div, 0,
6498 2 * (div->n_row - bset->n_div));
6500 n_div = bset->n_div;
6501 for (i = n_div; i < div->n_row; ++i)
6502 if (isl_basic_set_alloc_div(bset) < 0)
6506 for (i = div->n_row - 1; i >= 0; --i) {
6507 if (j >= 0 && exp[j] == i) {
6509 isl_basic_map_swap_div(bset, i, j);
6512 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6513 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6521 isl_basic_set_free(bset);
6526 /* Look for a div in dst that corresponds to the div "div" in src.
6527 * The divs before "div" in src and dst are assumed to be the same.
6529 * Returns -1 if no corresponding div was found and the position
6530 * of the corresponding div in dst otherwise.
6532 static int find_div(struct isl_basic_map *dst,
6533 struct isl_basic_map *src, unsigned div)
6537 unsigned total = isl_dim_total(src->dim);
6539 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6540 for (i = div; i < dst->n_div; ++i)
6541 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6542 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6543 dst->n_div - div) == -1)
6548 struct isl_basic_map *isl_basic_map_align_divs(
6549 struct isl_basic_map *dst, struct isl_basic_map *src)
6552 unsigned total = isl_dim_total(src->dim);
6557 if (src->n_div == 0)
6560 for (i = 0; i < src->n_div; ++i)
6561 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6563 src = isl_basic_map_order_divs(src);
6564 dst = isl_basic_map_cow(dst);
6565 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6566 src->n_div, 0, 2 * src->n_div);
6569 for (i = 0; i < src->n_div; ++i) {
6570 int j = find_div(dst, src, i);
6572 j = isl_basic_map_alloc_div(dst);
6575 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6576 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6577 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6581 isl_basic_map_swap_div(dst, i, j);
6585 isl_basic_map_free(dst);
6589 struct isl_basic_set *isl_basic_set_align_divs(
6590 struct isl_basic_set *dst, struct isl_basic_set *src)
6592 return (struct isl_basic_set *)isl_basic_map_align_divs(
6593 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6596 struct isl_map *isl_map_align_divs(struct isl_map *map)
6604 map = isl_map_compute_divs(map);
6605 map = isl_map_cow(map);
6609 for (i = 1; i < map->n; ++i)
6610 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6611 for (i = 1; i < map->n; ++i)
6612 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6614 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6618 struct isl_set *isl_set_align_divs(struct isl_set *set)
6620 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6623 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6627 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6628 map = isl_map_intersect_domain(map, set);
6629 set = isl_map_range(map);
6637 /* There is no need to cow as removing empty parts doesn't change
6638 * the meaning of the set.
6640 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6647 for (i = map->n-1; i >= 0; --i) {
6648 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6650 isl_basic_map_free(map->p[i]);
6651 if (i != map->n-1) {
6652 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6653 map->p[i] = map->p[map->n-1];
6661 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6663 return (struct isl_set *)
6664 isl_map_remove_empty_parts((struct isl_map *)set);
6667 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6669 struct isl_basic_map *bmap;
6670 if (!map || map->n == 0)
6672 bmap = map->p[map->n-1];
6673 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6674 return isl_basic_map_copy(bmap);
6677 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6679 return (struct isl_basic_set *)
6680 isl_map_copy_basic_map((struct isl_map *)set);
6683 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6684 __isl_keep isl_basic_map *bmap)
6690 for (i = map->n-1; i >= 0; --i) {
6691 if (map->p[i] != bmap)
6693 map = isl_map_cow(map);
6696 isl_basic_map_free(map->p[i]);
6697 if (i != map->n-1) {
6698 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6699 map->p[i] = map->p[map->n-1];
6710 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6711 struct isl_basic_set *bset)
6713 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6714 (struct isl_basic_map *)bset);
6717 /* Given two basic sets bset1 and bset2, compute the maximal difference
6718 * between the values of dimension pos in bset1 and those in bset2
6719 * for any common value of the parameters and dimensions preceding pos.
6721 static enum isl_lp_result basic_set_maximal_difference_at(
6722 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6723 int pos, isl_int *opt)
6725 struct isl_dim *dims;
6726 struct isl_basic_map *bmap1 = NULL;
6727 struct isl_basic_map *bmap2 = NULL;
6728 struct isl_ctx *ctx;
6729 struct isl_vec *obj;
6732 unsigned dim1, dim2;
6733 enum isl_lp_result res;
6735 if (!bset1 || !bset2)
6736 return isl_lp_error;
6738 nparam = isl_basic_set_n_param(bset1);
6739 dim1 = isl_basic_set_n_dim(bset1);
6740 dim2 = isl_basic_set_n_dim(bset2);
6741 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6742 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6743 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6744 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6745 if (!bmap1 || !bmap2)
6747 bmap1 = isl_basic_map_cow(bmap1);
6748 bmap1 = isl_basic_map_extend(bmap1, nparam,
6749 pos, (dim1 - pos) + (dim2 - pos),
6750 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6751 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6754 total = isl_basic_map_total_dim(bmap1);
6756 obj = isl_vec_alloc(ctx, 1 + total);
6757 isl_seq_clr(obj->block.data, 1 + total);
6758 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6759 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6762 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6764 isl_basic_map_free(bmap1);
6768 isl_basic_map_free(bmap1);
6769 isl_basic_map_free(bmap2);
6770 return isl_lp_error;
6773 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6774 * for any common value of the parameters and dimensions preceding pos
6775 * in both basic sets, the values of dimension pos in bset1 are
6776 * smaller or larger than those in bset2.
6779 * 1 if bset1 follows bset2
6780 * -1 if bset1 precedes bset2
6781 * 0 if bset1 and bset2 are incomparable
6782 * -2 if some error occurred.
6784 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6785 struct isl_basic_set *bset2, int pos)
6788 enum isl_lp_result res;
6793 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6795 if (res == isl_lp_empty)
6797 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6798 res == isl_lp_unbounded)
6800 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6809 /* Given two basic sets bset1 and bset2, check whether
6810 * for any common value of the parameters and dimensions preceding pos
6811 * there is a value of dimension pos in bset1 that is larger
6812 * than a value of the same dimension in bset2.
6815 * 1 if there exists such a pair
6816 * 0 if there is no such pair, but there is a pair of equal values
6818 * -2 if some error occurred.
6820 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6821 __isl_keep isl_basic_set *bset2, int pos)
6824 enum isl_lp_result res;
6829 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6831 if (res == isl_lp_empty)
6833 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6834 res == isl_lp_unbounded)
6836 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6838 else if (res == isl_lp_ok)
6847 /* Given two sets set1 and set2, check whether
6848 * for any common value of the parameters and dimensions preceding pos
6849 * there is a value of dimension pos in set1 that is larger
6850 * than a value of the same dimension in set2.
6853 * 1 if there exists such a pair
6854 * 0 if there is no such pair, but there is a pair of equal values
6856 * -2 if some error occurred.
6858 int isl_set_follows_at(__isl_keep isl_set *set1,
6859 __isl_keep isl_set *set2, int pos)
6867 for (i = 0; i < set1->n; ++i)
6868 for (j = 0; j < set2->n; ++j) {
6870 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6871 if (f == 1 || f == -2)
6880 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
6881 unsigned pos, isl_int *val)
6889 total = isl_basic_map_total_dim(bmap);
6890 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6891 for (; d+1 > pos; --d)
6892 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6896 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6898 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6900 if (!isl_int_is_one(bmap->eq[i][1+d]))
6903 isl_int_neg(*val, bmap->eq[i][0]);
6909 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
6910 unsigned pos, isl_int *val)
6922 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
6925 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
6926 for (i = 1; fixed == 1 && i < map->n; ++i) {
6927 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
6928 if (fixed == 1 && isl_int_ne(tmp, v))
6932 isl_int_set(*val, v);
6938 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
6939 unsigned pos, isl_int *val)
6941 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
6945 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
6948 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
6951 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
6952 enum isl_dim_type type, unsigned pos, isl_int *val)
6954 if (pos >= isl_basic_map_dim(bmap, type))
6956 return isl_basic_map_plain_has_fixed_var(bmap,
6957 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6960 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
6961 enum isl_dim_type type, unsigned pos, isl_int *val)
6963 if (pos >= isl_map_dim(map, type))
6965 return isl_map_plain_has_fixed_var(map,
6966 map_offset(map, type) - 1 + pos, val);
6969 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
6970 enum isl_dim_type type, unsigned pos, isl_int *val)
6972 return isl_map_plain_is_fixed(map, type, pos, val);
6975 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6976 * then return this fixed value in *val.
6978 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
6979 unsigned dim, isl_int *val)
6981 return isl_basic_set_plain_has_fixed_var(bset,
6982 isl_basic_set_n_param(bset) + dim, val);
6985 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6986 * then return this fixed value in *val.
6988 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
6989 unsigned dim, isl_int *val)
6991 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6994 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
6995 unsigned dim, isl_int *val)
6997 return isl_set_plain_dim_is_fixed(set, dim, val);
7000 /* Check if input variable in has fixed value and if so and if val is not NULL,
7001 * then return this fixed value in *val.
7003 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7004 unsigned in, isl_int *val)
7006 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7009 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7010 * and if val is not NULL, then return this lower bound in *val.
7012 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7013 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7015 int i, i_eq = -1, i_ineq = -1;
7022 total = isl_basic_set_total_dim(bset);
7023 nparam = isl_basic_set_n_param(bset);
7024 for (i = 0; i < bset->n_eq; ++i) {
7025 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7031 for (i = 0; i < bset->n_ineq; ++i) {
7032 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7034 if (i_eq != -1 || i_ineq != -1)
7038 if (i_eq == -1 && i_ineq == -1)
7040 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7041 /* The coefficient should always be one due to normalization. */
7042 if (!isl_int_is_one(c[1+nparam+dim]))
7044 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7046 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7047 total - nparam - dim - 1) != -1)
7050 isl_int_neg(*val, c[0]);
7054 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7055 unsigned dim, isl_int *val)
7067 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7071 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7073 for (i = 1; fixed == 1 && i < set->n; ++i) {
7074 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7076 if (fixed == 1 && isl_int_ne(tmp, v))
7080 isl_int_set(*val, v);
7091 /* uset_gist depends on constraints without existentially quantified
7092 * variables sorting first.
7094 static int qsort_constraint_cmp(const void *p1, const void *p2)
7096 const struct constraint *c1 = (const struct constraint *)p1;
7097 const struct constraint *c2 = (const struct constraint *)p2;
7099 unsigned size = isl_min(c1->size, c2->size);
7101 l1 = isl_seq_last_non_zero(c1->c, size);
7102 l2 = isl_seq_last_non_zero(c2->c, size);
7107 return isl_seq_cmp(c1->c, c2->c, size);
7110 static struct isl_basic_map *isl_basic_map_sort_constraints(
7111 struct isl_basic_map *bmap)
7114 struct constraint *c;
7119 total = isl_basic_map_total_dim(bmap);
7120 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7123 for (i = 0; i < bmap->n_ineq; ++i) {
7125 c[i].c = bmap->ineq[i];
7127 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7128 for (i = 0; i < bmap->n_ineq; ++i)
7129 bmap->ineq[i] = c[i].c;
7133 isl_basic_map_free(bmap);
7137 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7138 __isl_take isl_basic_set *bset)
7140 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7141 (struct isl_basic_map *)bset);
7144 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7148 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7150 bmap = isl_basic_map_remove_redundancies(bmap);
7151 bmap = isl_basic_map_sort_constraints(bmap);
7152 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7156 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7158 return (struct isl_basic_set *)isl_basic_map_normalize(
7159 (struct isl_basic_map *)bset);
7162 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7163 const __isl_keep isl_basic_map *bmap2)
7170 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7171 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7172 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7173 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7174 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7175 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7176 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7177 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7178 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7179 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7180 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7182 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7184 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7186 if (bmap1->n_eq != bmap2->n_eq)
7187 return bmap1->n_eq - bmap2->n_eq;
7188 if (bmap1->n_ineq != bmap2->n_ineq)
7189 return bmap1->n_ineq - bmap2->n_ineq;
7190 if (bmap1->n_div != bmap2->n_div)
7191 return bmap1->n_div - bmap2->n_div;
7192 total = isl_basic_map_total_dim(bmap1);
7193 for (i = 0; i < bmap1->n_eq; ++i) {
7194 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7198 for (i = 0; i < bmap1->n_ineq; ++i) {
7199 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7203 for (i = 0; i < bmap1->n_div; ++i) {
7204 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7211 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7212 __isl_keep isl_basic_map *bmap2)
7214 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7217 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7218 __isl_keep isl_basic_set *bset2)
7220 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7221 (isl_basic_map *)bset2);
7224 static int qsort_bmap_cmp(const void *p1, const void *p2)
7226 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7227 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7229 return isl_basic_map_plain_cmp(bmap1, bmap2);
7232 /* We normalize in place, but if anything goes wrong we need
7233 * to return NULL, so we need to make sure we don't change the
7234 * meaning of any possible other copies of map.
7236 struct isl_map *isl_map_normalize(struct isl_map *map)
7239 struct isl_basic_map *bmap;
7243 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7245 for (i = 0; i < map->n; ++i) {
7246 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7249 isl_basic_map_free(map->p[i]);
7252 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7253 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7254 map = isl_map_remove_empty_parts(map);
7257 for (i = map->n - 1; i >= 1; --i) {
7258 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7260 isl_basic_map_free(map->p[i-1]);
7261 for (j = i; j < map->n; ++j)
7262 map->p[j-1] = map->p[j];
7272 struct isl_set *isl_set_normalize(struct isl_set *set)
7274 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7277 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7287 if (!isl_dim_equal(map1->dim, map2->dim))
7290 map1 = isl_map_copy(map1);
7291 map2 = isl_map_copy(map2);
7292 map1 = isl_map_normalize(map1);
7293 map2 = isl_map_normalize(map2);
7296 equal = map1->n == map2->n;
7297 for (i = 0; equal && i < map1->n; ++i) {
7298 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7311 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7313 return isl_map_plain_is_equal(map1, map2);
7316 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7318 return isl_map_plain_is_equal((struct isl_map *)set1,
7319 (struct isl_map *)set2);
7322 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7324 return isl_set_plain_is_equal(set1, set2);
7327 /* Return an interval that ranges from min to max (inclusive)
7329 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7330 isl_int min, isl_int max)
7333 struct isl_basic_set *bset = NULL;
7335 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7339 k = isl_basic_set_alloc_inequality(bset);
7342 isl_int_set_si(bset->ineq[k][1], 1);
7343 isl_int_neg(bset->ineq[k][0], min);
7345 k = isl_basic_set_alloc_inequality(bset);
7348 isl_int_set_si(bset->ineq[k][1], -1);
7349 isl_int_set(bset->ineq[k][0], max);
7353 isl_basic_set_free(bset);
7357 /* Return the Cartesian product of the basic sets in list (in the given order).
7359 __isl_give isl_basic_set *isl_basic_set_list_product(
7360 __isl_take struct isl_basic_set_list *list)
7368 struct isl_basic_set *product = NULL;
7372 isl_assert(list->ctx, list->n > 0, goto error);
7373 isl_assert(list->ctx, list->p[0], goto error);
7374 nparam = isl_basic_set_n_param(list->p[0]);
7375 dim = isl_basic_set_n_dim(list->p[0]);
7376 extra = list->p[0]->n_div;
7377 n_eq = list->p[0]->n_eq;
7378 n_ineq = list->p[0]->n_ineq;
7379 for (i = 1; i < list->n; ++i) {
7380 isl_assert(list->ctx, list->p[i], goto error);
7381 isl_assert(list->ctx,
7382 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7383 dim += isl_basic_set_n_dim(list->p[i]);
7384 extra += list->p[i]->n_div;
7385 n_eq += list->p[i]->n_eq;
7386 n_ineq += list->p[i]->n_ineq;
7388 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7393 for (i = 0; i < list->n; ++i) {
7394 isl_basic_set_add_constraints(product,
7395 isl_basic_set_copy(list->p[i]), dim);
7396 dim += isl_basic_set_n_dim(list->p[i]);
7398 isl_basic_set_list_free(list);
7401 isl_basic_set_free(product);
7402 isl_basic_set_list_free(list);
7406 struct isl_basic_map *isl_basic_map_product(
7407 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7409 struct isl_dim *dim_result = NULL;
7410 struct isl_basic_map *bmap;
7411 unsigned in1, in2, out1, out2, nparam, total, pos;
7412 struct isl_dim_map *dim_map1, *dim_map2;
7414 if (!bmap1 || !bmap2)
7417 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7418 bmap2->dim, isl_dim_param), goto error);
7419 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7420 isl_dim_copy(bmap2->dim));
7422 in1 = isl_basic_map_n_in(bmap1);
7423 in2 = isl_basic_map_n_in(bmap2);
7424 out1 = isl_basic_map_n_out(bmap1);
7425 out2 = isl_basic_map_n_out(bmap2);
7426 nparam = isl_basic_map_n_param(bmap1);
7428 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7429 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7430 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7431 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7432 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7433 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7434 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7435 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7436 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7437 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7438 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7440 bmap = isl_basic_map_alloc_dim(dim_result,
7441 bmap1->n_div + bmap2->n_div,
7442 bmap1->n_eq + bmap2->n_eq,
7443 bmap1->n_ineq + bmap2->n_ineq);
7444 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7445 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7446 bmap = isl_basic_map_simplify(bmap);
7447 return isl_basic_map_finalize(bmap);
7449 isl_basic_map_free(bmap1);
7450 isl_basic_map_free(bmap2);
7454 __isl_give isl_basic_map *isl_basic_map_flat_product(
7455 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7457 isl_basic_map *prod;
7459 prod = isl_basic_map_product(bmap1, bmap2);
7460 prod = isl_basic_map_flatten(prod);
7464 __isl_give isl_basic_set *isl_basic_set_flat_product(
7465 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7467 return isl_basic_map_flat_product(bset1, bset2);
7470 __isl_give isl_basic_map *isl_basic_map_range_product(
7471 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7473 isl_dim *dim_result = NULL;
7474 isl_basic_map *bmap;
7475 unsigned in, out1, out2, nparam, total, pos;
7476 struct isl_dim_map *dim_map1, *dim_map2;
7478 if (!bmap1 || !bmap2)
7481 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7482 isl_dim_copy(bmap2->dim));
7484 in = isl_basic_map_dim(bmap1, isl_dim_in);
7485 out1 = isl_basic_map_n_out(bmap1);
7486 out2 = isl_basic_map_n_out(bmap2);
7487 nparam = isl_basic_map_n_param(bmap1);
7489 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7490 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7491 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7492 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7493 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7494 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7495 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7496 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7497 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7498 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7499 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7501 bmap = isl_basic_map_alloc_dim(dim_result,
7502 bmap1->n_div + bmap2->n_div,
7503 bmap1->n_eq + bmap2->n_eq,
7504 bmap1->n_ineq + bmap2->n_ineq);
7505 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7506 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7507 bmap = isl_basic_map_simplify(bmap);
7508 return isl_basic_map_finalize(bmap);
7510 isl_basic_map_free(bmap1);
7511 isl_basic_map_free(bmap2);
7515 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
7516 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7518 isl_basic_map *prod;
7520 prod = isl_basic_map_range_product(bmap1, bmap2);
7521 prod = isl_basic_map_flatten_range(prod);
7525 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7526 __isl_take isl_map *map2,
7527 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7528 __isl_take isl_dim *right),
7529 __isl_give isl_basic_map *(*basic_map_product)(
7530 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7533 struct isl_map *result;
7539 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7540 map2->dim, isl_dim_param), goto error);
7542 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7543 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7544 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7546 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7547 isl_dim_copy(map2->dim)),
7548 map1->n * map2->n, flags);
7551 for (i = 0; i < map1->n; ++i)
7552 for (j = 0; j < map2->n; ++j) {
7553 struct isl_basic_map *part;
7554 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7555 isl_basic_map_copy(map2->p[j]));
7556 if (isl_basic_map_is_empty(part))
7557 isl_basic_map_free(part);
7559 result = isl_map_add_basic_map(result, part);
7572 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7574 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7576 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7579 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7581 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7582 __isl_take isl_map *map2)
7586 prod = isl_map_product(map1, map2);
7587 prod = isl_map_flatten(prod);
7591 /* Given two set A and B, construct its Cartesian product A x B.
7593 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7595 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7596 (struct isl_map *)set2);
7599 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7600 __isl_take isl_set *set2)
7602 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7605 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7607 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7608 __isl_take isl_map *map2)
7610 return map_product(map1, map2, &isl_dim_range_product,
7611 &isl_basic_map_range_product);
7614 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7616 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
7617 __isl_take isl_map *map2)
7621 prod = isl_map_range_product(map1, map2);
7622 prod = isl_map_flatten_range(prod);
7626 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7629 uint32_t hash = isl_hash_init();
7634 bmap = isl_basic_map_copy(bmap);
7635 bmap = isl_basic_map_normalize(bmap);
7638 total = isl_basic_map_total_dim(bmap);
7639 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7640 for (i = 0; i < bmap->n_eq; ++i) {
7642 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7643 isl_hash_hash(hash, c_hash);
7645 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7646 for (i = 0; i < bmap->n_ineq; ++i) {
7648 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7649 isl_hash_hash(hash, c_hash);
7651 isl_hash_byte(hash, bmap->n_div & 0xFF);
7652 for (i = 0; i < bmap->n_div; ++i) {
7654 if (isl_int_is_zero(bmap->div[i][0]))
7656 isl_hash_byte(hash, i & 0xFF);
7657 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7658 isl_hash_hash(hash, c_hash);
7660 isl_basic_map_free(bmap);
7664 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7666 return isl_basic_map_get_hash((isl_basic_map *)bset);
7669 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7676 map = isl_map_copy(map);
7677 map = isl_map_normalize(map);
7681 hash = isl_hash_init();
7682 for (i = 0; i < map->n; ++i) {
7684 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7685 isl_hash_hash(hash, bmap_hash);
7693 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7695 return isl_map_get_hash((isl_map *)set);
7698 /* Check if the value for dimension dim is completely determined
7699 * by the values of the other parameters and variables.
7700 * That is, check if dimension dim is involved in an equality.
7702 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7709 nparam = isl_basic_set_n_param(bset);
7710 for (i = 0; i < bset->n_eq; ++i)
7711 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7716 /* Check if the value for dimension dim is completely determined
7717 * by the values of the other parameters and variables.
7718 * That is, check if dimension dim is involved in an equality
7719 * for each of the subsets.
7721 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7727 for (i = 0; i < set->n; ++i) {
7729 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7736 int isl_set_n_basic_set(__isl_keep isl_set *set)
7738 return set ? set->n : 0;
7741 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7742 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7749 for (i = 0; i < map->n; ++i)
7750 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7756 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7757 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7764 for (i = 0; i < set->n; ++i)
7765 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7771 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7773 struct isl_dim *dim;
7778 bset = isl_basic_set_cow(bset);
7782 dim = isl_basic_set_get_dim(bset);
7783 dim = isl_dim_lift(dim, bset->n_div);
7786 isl_dim_free(bset->dim);
7788 bset->extra -= bset->n_div;
7791 bset = isl_basic_set_finalize(bset);
7795 isl_basic_set_free(bset);
7799 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7802 struct isl_dim *dim;
7805 set = isl_set_align_divs(set);
7810 set = isl_set_cow(set);
7814 n_div = set->p[0]->n_div;
7815 dim = isl_set_get_dim(set);
7816 dim = isl_dim_lift(dim, n_div);
7819 isl_dim_free(set->dim);
7822 for (i = 0; i < set->n; ++i) {
7823 set->p[i] = isl_basic_set_lift(set->p[i]);
7834 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7836 struct isl_dim *dim;
7837 struct isl_basic_map *bmap;
7844 set = isl_set_align_divs(set);
7849 dim = isl_set_get_dim(set);
7850 if (set->n == 0 || set->p[0]->n_div == 0) {
7852 return isl_map_identity(isl_dim_map_from_set(dim));
7855 n_div = set->p[0]->n_div;
7856 dim = isl_dim_map_from_set(dim);
7857 n_param = isl_dim_size(dim, isl_dim_param);
7858 n_set = isl_dim_size(dim, isl_dim_in);
7859 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7860 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7861 for (i = 0; i < n_set; ++i)
7862 bmap = var_equal(bmap, i);
7864 total = n_param + n_set + n_set + n_div;
7865 for (i = 0; i < n_div; ++i) {
7866 k = isl_basic_map_alloc_inequality(bmap);
7869 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7870 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7871 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7872 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7873 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7874 set->p[0]->div[i][0]);
7876 l = isl_basic_map_alloc_inequality(bmap);
7879 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7880 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7881 set->p[0]->div[i][0]);
7882 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7886 bmap = isl_basic_map_simplify(bmap);
7887 bmap = isl_basic_map_finalize(bmap);
7888 return isl_map_from_basic_map(bmap);
7891 isl_basic_map_free(bmap);
7895 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7903 dim = isl_basic_set_total_dim(bset);
7904 size += bset->n_eq * (1 + dim);
7905 size += bset->n_ineq * (1 + dim);
7906 size += bset->n_div * (2 + dim);
7911 int isl_set_size(__isl_keep isl_set *set)
7919 for (i = 0; i < set->n; ++i)
7920 size += isl_basic_set_size(set->p[i]);
7925 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7926 enum isl_dim_type type, unsigned pos)
7934 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7936 pos += isl_basic_map_offset(bmap, type);
7938 for (i = 0; i < bmap->n_eq; ++i)
7939 if (!isl_int_is_zero(bmap->eq[i][pos]))
7943 for (i = 0; i < bmap->n_ineq; ++i) {
7944 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7951 return lower && upper;
7954 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7955 enum isl_dim_type type, unsigned pos)
7962 for (i = 0; i < map->n; ++i) {
7964 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7965 if (bounded < 0 || !bounded)
7972 /* Return 1 if the specified dim is involved in both an upper bound
7973 * and a lower bound.
7975 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7976 enum isl_dim_type type, unsigned pos)
7978 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7981 /* For each of the "n" variables starting at "first", determine
7982 * the sign of the variable and put the results in the first "n"
7983 * elements of the array "signs".
7985 * 1 means that the variable is non-negative
7986 * -1 means that the variable is non-positive
7987 * 0 means the variable attains both positive and negative values.
7989 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7990 unsigned first, unsigned n, int *signs)
7992 isl_vec *bound = NULL;
7993 struct isl_tab *tab = NULL;
7994 struct isl_tab_undo *snap;
7997 if (!bset || !signs)
8000 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8001 tab = isl_tab_from_basic_set(bset);
8005 isl_seq_clr(bound->el, bound->size);
8006 isl_int_set_si(bound->el[0], -1);
8008 snap = isl_tab_snap(tab);
8009 for (i = 0; i < n; ++i) {
8012 isl_int_set_si(bound->el[1 + first + i], -1);
8013 if (isl_tab_add_ineq(tab, bound->el) < 0)
8016 isl_int_set_si(bound->el[1 + first + i], 0);
8017 if (isl_tab_rollback(tab, snap) < 0)
8025 isl_int_set_si(bound->el[1 + first + i], 1);
8026 if (isl_tab_add_ineq(tab, bound->el) < 0)
8029 isl_int_set_si(bound->el[1 + first + i], 0);
8030 if (isl_tab_rollback(tab, snap) < 0)
8033 signs[i] = empty ? -1 : 0;
8037 isl_vec_free(bound);
8041 isl_vec_free(bound);
8045 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8046 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8048 if (!bset || !signs)
8050 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8053 first += pos(bset->dim, type) - 1;
8054 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8057 /* Check if the given basic map is obviously single-valued.
8058 * In particular, for each output dimension, check that there is
8059 * an equality that defines the output dimension in terms of
8060 * earlier dimensions.
8062 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8072 total = 1 + isl_basic_map_total_dim(bmap);
8073 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8074 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8076 for (i = 0; i < n_out; ++i) {
8077 for (j = 0; j < bmap->n_eq; ++j) {
8078 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8080 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8081 total - (o_out + i + 1)) == -1)
8084 if (j >= bmap->n_eq)
8091 /* Check if the given map is obviously single-valued.
8093 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8102 return isl_basic_map_plain_is_single_valued(map->p[0]);
8105 /* Check if the given map is single-valued.
8110 * and check if the result is a subset of the identity mapping.
8112 int isl_map_is_single_valued(__isl_keep isl_map *map)
8119 sv = isl_map_plain_is_single_valued(map);
8123 test = isl_map_reverse(isl_map_copy(map));
8124 test = isl_map_apply_range(test, isl_map_copy(map));
8126 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
8127 id = isl_map_identity(dim);
8129 sv = isl_map_is_subset(test, id);
8137 int isl_map_is_injective(__isl_keep isl_map *map)
8141 map = isl_map_copy(map);
8142 map = isl_map_reverse(map);
8143 in = isl_map_is_single_valued(map);
8149 /* Check if the given map is obviously injective.
8151 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8155 map = isl_map_copy(map);
8156 map = isl_map_reverse(map);
8157 in = isl_map_plain_is_single_valued(map);
8163 int isl_map_is_bijective(__isl_keep isl_map *map)
8167 sv = isl_map_is_single_valued(map);
8171 return isl_map_is_injective(map);
8174 int isl_set_is_singleton(__isl_keep isl_set *set)
8176 return isl_map_is_single_valued((isl_map *)set);
8179 int isl_map_is_translation(__isl_keep isl_map *map)
8184 delta = isl_map_deltas(isl_map_copy(map));
8185 ok = isl_set_is_singleton(delta);
8186 isl_set_free(delta);
8191 static int unique(isl_int *p, unsigned pos, unsigned len)
8193 if (isl_seq_first_non_zero(p, pos) != -1)
8195 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8200 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8209 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8212 nvar = isl_basic_set_dim(bset, isl_dim_set);
8213 ovar = isl_dim_offset(bset->dim, isl_dim_set);
8214 for (j = 0; j < nvar; ++j) {
8215 int lower = 0, upper = 0;
8216 for (i = 0; i < bset->n_eq; ++i) {
8217 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8219 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8225 for (i = 0; i < bset->n_ineq; ++i) {
8226 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8228 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8230 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8235 if (!lower || !upper)
8242 int isl_set_is_box(__isl_keep isl_set *set)
8249 return isl_basic_set_is_box(set->p[0]);
8252 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8257 return isl_dim_is_wrapping(bset->dim);
8260 int isl_set_is_wrapping(__isl_keep isl_set *set)
8265 return isl_dim_is_wrapping(set->dim);
8268 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8270 bmap = isl_basic_map_cow(bmap);
8274 bmap->dim = isl_dim_wrap(bmap->dim);
8278 bmap = isl_basic_map_finalize(bmap);
8280 return (isl_basic_set *)bmap;
8282 isl_basic_map_free(bmap);
8286 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8290 map = isl_map_cow(map);
8294 for (i = 0; i < map->n; ++i) {
8295 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8299 map->dim = isl_dim_wrap(map->dim);
8303 return (isl_set *)map;
8309 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8311 bset = isl_basic_set_cow(bset);
8315 bset->dim = isl_dim_unwrap(bset->dim);
8319 bset = isl_basic_set_finalize(bset);
8321 return (isl_basic_map *)bset;
8323 isl_basic_set_free(bset);
8327 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8334 if (!isl_set_is_wrapping(set))
8335 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8338 set = isl_set_cow(set);
8342 for (i = 0; i < set->n; ++i) {
8343 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8348 set->dim = isl_dim_unwrap(set->dim);
8352 return (isl_map *)set;
8358 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8359 enum isl_dim_type type)
8364 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8367 bmap = isl_basic_map_cow(bmap);
8371 bmap->dim = isl_dim_reset(bmap->dim, type);
8375 bmap = isl_basic_map_finalize(bmap);
8379 isl_basic_map_free(bmap);
8383 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8384 enum isl_dim_type type)
8391 if (!isl_dim_is_named_or_nested(map->dim, type))
8394 map = isl_map_cow(map);
8398 for (i = 0; i < map->n; ++i) {
8399 map->p[i] = isl_basic_map_reset(map->p[i], type);
8403 map->dim = isl_dim_reset(map->dim, type);
8413 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8418 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8421 bmap = isl_basic_map_cow(bmap);
8425 bmap->dim = isl_dim_flatten(bmap->dim);
8429 bmap = isl_basic_map_finalize(bmap);
8433 isl_basic_map_free(bmap);
8437 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8439 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8442 __isl_give isl_basic_map *isl_basic_map_flatten_range(
8443 __isl_take isl_basic_map *bmap)
8448 if (!bmap->dim->nested[1])
8451 bmap = isl_basic_map_cow(bmap);
8455 bmap->dim = isl_dim_flatten_range(bmap->dim);
8459 bmap = isl_basic_map_finalize(bmap);
8463 isl_basic_map_free(bmap);
8467 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8474 if (!map->dim->nested[0] && !map->dim->nested[1])
8477 map = isl_map_cow(map);
8481 for (i = 0; i < map->n; ++i) {
8482 map->p[i] = isl_basic_map_flatten(map->p[i]);
8486 map->dim = isl_dim_flatten(map->dim);
8496 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8498 return (isl_set *)isl_map_flatten((isl_map *)set);
8501 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8503 isl_dim *dim, *flat_dim;
8506 dim = isl_set_get_dim(set);
8507 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8508 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8509 map = isl_map_intersect_domain(map, set);
8514 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
8521 if (!map->dim->nested[1])
8524 map = isl_map_cow(map);
8528 for (i = 0; i < map->n; ++i) {
8529 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
8533 map->dim = isl_dim_flatten_range(map->dim);
8543 /* Reorder the dimensions of "bmap" according to the given dim_map
8544 * and set the dimension specification to "dim".
8546 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8547 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8551 bmap = isl_basic_map_cow(bmap);
8552 if (!bmap || !dim || !dim_map)
8555 res = isl_basic_map_alloc_dim(dim,
8556 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8557 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8558 res = isl_basic_map_finalize(res);
8562 isl_basic_map_free(bmap);
8567 /* Reorder the dimensions of "map" according to given reordering.
8569 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8570 __isl_take isl_reordering *r)
8573 struct isl_dim_map *dim_map;
8575 map = isl_map_cow(map);
8576 dim_map = isl_dim_map_from_reordering(r);
8577 if (!map || !r || !dim_map)
8580 for (i = 0; i < map->n; ++i) {
8581 struct isl_dim_map *dim_map_i;
8583 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8585 map->p[i] = isl_basic_map_realign(map->p[i],
8586 isl_dim_copy(r->dim), dim_map_i);
8592 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8594 isl_reordering_free(r);
8600 isl_reordering_free(r);
8604 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8605 __isl_take isl_reordering *r)
8607 return (isl_set *)isl_map_realign((isl_map *)set, r);
8610 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
8611 __isl_take isl_dim *model)
8618 ctx = isl_dim_get_ctx(model);
8619 if (!isl_dim_has_named_params(model))
8620 isl_die(ctx, isl_error_invalid,
8621 "model has unnamed parameters", goto error);
8622 if (!isl_dim_has_named_params(map->dim))
8623 isl_die(ctx, isl_error_invalid,
8624 "relation has unnamed parameters", goto error);
8625 if (!isl_dim_match(map->dim, isl_dim_param, model, isl_dim_param)) {
8626 isl_reordering *exp;
8628 model = isl_dim_drop(model, isl_dim_in,
8629 0, isl_dim_size(model, isl_dim_in));
8630 model = isl_dim_drop(model, isl_dim_out,
8631 0, isl_dim_size(model, isl_dim_out));
8632 exp = isl_parameter_alignment_reordering(map->dim, model);
8633 exp = isl_reordering_extend_dim(exp, isl_map_get_dim(map));
8634 map = isl_map_realign(map, exp);
8637 isl_dim_free(model);
8640 isl_dim_free(model);
8645 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
8646 __isl_take isl_dim *model)
8648 return isl_map_align_params(set, model);
8651 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8652 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8653 enum isl_dim_type c2, enum isl_dim_type c3,
8654 enum isl_dim_type c4, enum isl_dim_type c5)
8656 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8657 struct isl_mat *mat;
8663 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8664 isl_basic_map_total_dim(bmap) + 1);
8667 for (i = 0; i < bmap->n_eq; ++i)
8668 for (j = 0, pos = 0; j < 5; ++j) {
8669 int off = isl_basic_map_offset(bmap, c[j]);
8670 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8671 isl_int_set(mat->row[i][pos],
8672 bmap->eq[i][off + k]);
8680 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8681 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8682 enum isl_dim_type c2, enum isl_dim_type c3,
8683 enum isl_dim_type c4, enum isl_dim_type c5)
8685 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8686 struct isl_mat *mat;
8692 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8693 isl_basic_map_total_dim(bmap) + 1);
8696 for (i = 0; i < bmap->n_ineq; ++i)
8697 for (j = 0, pos = 0; j < 5; ++j) {
8698 int off = isl_basic_map_offset(bmap, c[j]);
8699 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8700 isl_int_set(mat->row[i][pos],
8701 bmap->ineq[i][off + k]);
8709 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8710 __isl_take isl_dim *dim,
8711 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8712 enum isl_dim_type c2, enum isl_dim_type c3,
8713 enum isl_dim_type c4, enum isl_dim_type c5)
8715 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8716 isl_basic_map *bmap;
8722 if (!dim || !eq || !ineq)
8725 if (eq->n_col != ineq->n_col)
8726 isl_die(dim->ctx, isl_error_invalid,
8727 "equalities and inequalities matrices should have "
8728 "same number of columns", goto error);
8730 total = 1 + isl_dim_total(dim);
8732 if (eq->n_col < total)
8733 isl_die(dim->ctx, isl_error_invalid,
8734 "number of columns too small", goto error);
8736 extra = eq->n_col - total;
8738 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8739 eq->n_row, ineq->n_row);
8742 for (i = 0; i < extra; ++i) {
8743 k = isl_basic_map_alloc_div(bmap);
8746 isl_int_set_si(bmap->div[k][0], 0);
8748 for (i = 0; i < eq->n_row; ++i) {
8749 l = isl_basic_map_alloc_equality(bmap);
8752 for (j = 0, pos = 0; j < 5; ++j) {
8753 int off = isl_basic_map_offset(bmap, c[j]);
8754 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8755 isl_int_set(bmap->eq[l][off + k],
8761 for (i = 0; i < ineq->n_row; ++i) {
8762 l = isl_basic_map_alloc_inequality(bmap);
8765 for (j = 0, pos = 0; j < 5; ++j) {
8766 int off = isl_basic_map_offset(bmap, c[j]);
8767 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8768 isl_int_set(bmap->ineq[l][off + k],
8787 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8788 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8789 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8791 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8792 c1, c2, c3, c4, isl_dim_in);
8795 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8796 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8797 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8799 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8800 c1, c2, c3, c4, isl_dim_in);
8803 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8804 __isl_take isl_dim *dim,
8805 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8806 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8808 return (isl_basic_set*)
8809 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8810 c1, c2, c3, c4, isl_dim_in);
8813 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8818 return isl_dim_can_zip(bmap->dim);
8821 int isl_map_can_zip(__isl_keep isl_map *map)
8826 return isl_dim_can_zip(map->dim);
8829 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8830 * (A -> C) -> (B -> D).
8832 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8841 if (!isl_basic_map_can_zip(bmap))
8842 isl_die(bmap->ctx, isl_error_invalid,
8843 "basic map cannot be zipped", goto error);
8844 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8845 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8846 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8847 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8848 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8851 bmap->dim = isl_dim_zip(bmap->dim);
8856 isl_basic_map_free(bmap);
8860 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8861 * (A -> C) -> (B -> D).
8863 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8870 if (!isl_map_can_zip(map))
8871 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8874 map = isl_map_cow(map);
8878 for (i = 0; i < map->n; ++i) {
8879 map->p[i] = isl_basic_map_zip(map->p[i]);
8884 map->dim = isl_dim_zip(map->dim);
8894 /* Construct a basic map mapping the domain of the affine expression
8895 * to a one-dimensional range prescribed by the affine expression.
8897 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
8901 isl_local_space *ls;
8902 isl_basic_map *bmap;
8907 ls = isl_aff_get_local_space(aff);
8908 ls = isl_local_space_from_domain(ls);
8909 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
8910 bmap = isl_basic_map_from_local_space(ls);
8911 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
8912 k = isl_basic_map_alloc_equality(bmap);
8916 pos = isl_basic_map_offset(bmap, isl_dim_out);
8917 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
8918 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
8919 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
8920 aff->v->size - (pos + 1));
8923 bmap = isl_basic_map_finalize(bmap);
8927 isl_basic_map_free(bmap);
8931 /* Construct a basic map mapping a domain in the given space to
8932 * to an n-dimensional range, with n the number of elements in the list,
8933 * where each coordinate in the range is prescribed by the
8934 * corresponding affine expression.
8935 * The domains of all affine expressions in the list are assumed to match
8938 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
8939 __isl_take isl_dim *domain_dim, __isl_take isl_aff_list *list)
8943 isl_basic_map *bmap;
8948 dim = isl_dim_from_domain(domain_dim);
8949 bmap = isl_basic_map_universe(dim);
8951 for (i = 0; i < list->n; ++i) {
8953 isl_basic_map *bmap_i;
8955 aff = isl_aff_copy(list->p[i]);
8956 bmap_i = isl_basic_map_from_aff(aff);
8958 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
8961 isl_aff_list_free(list);
8965 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
8966 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
8968 return isl_map_equate(set, type1, pos1, type2, pos2);
8971 /* Add a constraint imposing that the given two dimensions are equal.
8973 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
8974 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
8976 isl_basic_map *bmap = NULL;
8982 if (pos1 >= isl_map_dim(map, type1))
8983 isl_die(map->ctx, isl_error_invalid,
8984 "index out of bounds", goto error);
8985 if (pos2 >= isl_map_dim(map, type2))
8986 isl_die(map->ctx, isl_error_invalid,
8987 "index out of bounds", goto error);
8989 bmap = isl_basic_map_alloc_dim(isl_map_get_dim(map), 0, 1, 0);
8990 i = isl_basic_map_alloc_equality(bmap);
8993 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
8994 pos1 += isl_basic_map_offset(bmap, type1);
8995 pos2 += isl_basic_map_offset(bmap, type2);
8996 isl_int_set_si(bmap->eq[i][pos1], -1);
8997 isl_int_set_si(bmap->eq[i][pos2], 1);
8998 bmap = isl_basic_map_finalize(bmap);
9000 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9004 isl_basic_map_free(bmap);