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 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
402 enum isl_dim_type type, __isl_take isl_id *id)
404 map = isl_map_cow(map);
406 return isl_id_free(id);
408 map->dim = isl_dim_set_tuple_id(map->dim, type, id);
410 return isl_map_reset_dim(map, isl_dim_copy(map->dim));
413 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
414 __isl_take isl_id *id)
416 return isl_map_set_tuple_id(set, isl_dim_set, id);
419 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
420 enum isl_dim_type type)
422 map = isl_map_cow(map);
426 map->dim = isl_dim_reset_tuple_id(map->dim, type);
428 return isl_map_reset_dim(map, isl_dim_copy(map->dim));
431 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
432 enum isl_dim_type type)
434 return map ? isl_dim_get_tuple_id(map->dim, type) : NULL;
437 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
439 return isl_map_get_tuple_id(set, isl_dim_set);
442 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
444 return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
447 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
449 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
452 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
453 enum isl_dim_type type, unsigned pos)
455 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
458 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
459 enum isl_dim_type type, unsigned pos)
461 return bset ? isl_dim_get_name(bset->dim, type, pos) : NULL;
464 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
465 enum isl_dim_type type, unsigned pos)
467 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
470 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
471 enum isl_dim_type type, unsigned pos)
473 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
476 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
477 __isl_take isl_basic_map *bmap,
478 enum isl_dim_type type, unsigned pos, const char *s)
482 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
487 isl_basic_map_free(bmap);
491 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
492 enum isl_dim_type type, unsigned pos, const char *s)
499 map->dim = isl_dim_set_name(map->dim, type, pos, s);
503 for (i = 0; i < map->n; ++i) {
504 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
515 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
516 __isl_take isl_basic_set *bset,
517 enum isl_dim_type type, unsigned pos, const char *s)
519 return (isl_basic_set *)isl_basic_map_set_dim_name(
520 (isl_basic_map *)bset, type, pos, s);
523 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
524 enum isl_dim_type type, unsigned pos, const char *s)
526 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
529 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
530 enum isl_dim_type type, unsigned pos)
532 return map ? isl_dim_get_dim_id(map->dim, type, pos) : NULL;
535 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
536 enum isl_dim_type type, unsigned pos)
538 return isl_map_get_dim_id(set, type, pos);
541 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
542 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
544 map = isl_map_cow(map);
546 return isl_id_free(id);
548 map->dim = isl_dim_set_dim_id(map->dim, type, pos, id);
550 return isl_map_reset_dim(map, isl_dim_copy(map->dim));
553 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
554 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
556 return isl_map_set_dim_id(set, type, pos, id);
559 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
560 __isl_keep isl_id *id)
564 return isl_dim_find_dim_by_id(map->dim, type, id);
567 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
568 __isl_keep isl_id *id)
570 return isl_map_find_dim_by_id(set, type, id);
573 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
577 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
580 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
582 return isl_basic_map_is_rational(bset);
585 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
586 struct isl_basic_map *bmap, unsigned extra,
587 unsigned n_eq, unsigned n_ineq)
590 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
595 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
596 if (isl_blk_is_error(bmap->block))
599 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
604 bmap->block2 = isl_blk_empty();
607 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
608 if (isl_blk_is_error(bmap->block2))
611 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
616 for (i = 0; i < n_ineq + n_eq; ++i)
617 bmap->ineq[i] = bmap->block.data + i * row_size;
619 for (i = 0; i < extra; ++i)
620 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
624 bmap->c_size = n_eq + n_ineq;
625 bmap->eq = bmap->ineq + n_ineq;
634 isl_basic_map_free(bmap);
638 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
639 unsigned nparam, unsigned dim, unsigned extra,
640 unsigned n_eq, unsigned n_ineq)
642 struct isl_basic_map *bmap;
643 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
644 return (struct isl_basic_set *)bmap;
647 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
648 unsigned extra, unsigned n_eq, unsigned n_ineq)
650 struct isl_basic_map *bmap;
653 isl_assert(dim->ctx, dim->n_in == 0, goto error);
654 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
655 return (struct isl_basic_set *)bmap;
661 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
662 unsigned extra, unsigned n_eq, unsigned n_ineq)
664 struct isl_basic_map *bmap;
668 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
673 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
679 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
680 unsigned nparam, unsigned in, unsigned out, unsigned extra,
681 unsigned n_eq, unsigned n_ineq)
683 struct isl_basic_map *bmap;
686 dim = isl_dim_alloc(ctx, nparam, in, out);
690 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
694 static void dup_constraints(
695 struct isl_basic_map *dst, struct isl_basic_map *src)
698 unsigned total = isl_basic_map_total_dim(src);
700 for (i = 0; i < src->n_eq; ++i) {
701 int j = isl_basic_map_alloc_equality(dst);
702 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
705 for (i = 0; i < src->n_ineq; ++i) {
706 int j = isl_basic_map_alloc_inequality(dst);
707 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
710 for (i = 0; i < src->n_div; ++i) {
711 int j = isl_basic_map_alloc_div(dst);
712 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
714 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
717 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
719 struct isl_basic_map *dup;
723 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
724 bmap->n_div, bmap->n_eq, bmap->n_ineq);
727 dup_constraints(dup, bmap);
728 dup->flags = bmap->flags;
729 dup->sample = isl_vec_copy(bmap->sample);
733 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
735 struct isl_basic_map *dup;
737 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
738 return (struct isl_basic_set *)dup;
741 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
746 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
750 return isl_basic_set_dup(bset);
753 struct isl_set *isl_set_copy(struct isl_set *set)
762 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
767 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
771 bmap = isl_basic_map_dup(bmap);
773 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
777 struct isl_map *isl_map_copy(struct isl_map *map)
786 void isl_basic_map_free(struct isl_basic_map *bmap)
794 isl_ctx_deref(bmap->ctx);
796 isl_blk_free(bmap->ctx, bmap->block2);
798 isl_blk_free(bmap->ctx, bmap->block);
799 isl_vec_free(bmap->sample);
800 isl_dim_free(bmap->dim);
804 void isl_basic_set_free(struct isl_basic_set *bset)
806 isl_basic_map_free((struct isl_basic_map *)bset);
809 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
811 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
814 __isl_give isl_map *isl_map_align_params_map_map_and(
815 __isl_take isl_map *map1, __isl_take isl_map *map2,
816 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
817 __isl_take isl_map *map2))
821 if (isl_dim_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
822 return fn(map1, map2);
823 if (!isl_dim_has_named_params(map1->dim) ||
824 !isl_dim_has_named_params(map2->dim))
825 isl_die(map1->ctx, isl_error_invalid,
826 "unaligned unnamed parameters", goto error);
827 map1 = isl_map_align_params(map1, isl_map_get_dim(map2));
828 map2 = isl_map_align_params(map2, isl_map_get_dim(map1));
829 return fn(map1, map2);
836 static int align_params_map_map_and_test(__isl_keep isl_map *map1,
837 __isl_keep isl_map *map2,
838 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
844 if (isl_dim_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
845 return fn(map1, map2);
846 if (!isl_dim_has_named_params(map1->dim) ||
847 !isl_dim_has_named_params(map2->dim))
848 isl_die(map1->ctx, isl_error_invalid,
849 "unaligned unnamed parameters", return -1);
850 map1 = isl_map_copy(map1);
851 map2 = isl_map_copy(map2);
852 map1 = isl_map_align_params(map1, isl_map_get_dim(map2));
853 map2 = isl_map_align_params(map2, isl_map_get_dim(map1));
860 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
866 isl_assert(ctx, room_for_con(bmap, 1), return -1);
867 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
869 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
870 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
871 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
872 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
873 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
874 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
876 int j = isl_basic_map_alloc_inequality(bmap);
880 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
881 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
888 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
889 bmap->extra - bmap->n_div);
893 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
895 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
898 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
902 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
907 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
909 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
912 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
917 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
919 if (pos != bmap->n_eq - 1) {
921 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
922 bmap->eq[bmap->n_eq - 1] = t;
928 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
930 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
933 void isl_basic_map_inequality_to_equality(
934 struct isl_basic_map *bmap, unsigned pos)
939 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
940 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
945 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
946 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
947 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
948 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
951 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
953 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
956 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
962 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
963 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
964 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
965 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
966 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
967 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
968 1 + isl_basic_map_total_dim(bmap),
969 bmap->extra - bmap->n_div);
970 return bmap->n_ineq++;
973 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
975 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
978 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
982 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
987 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
989 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
992 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
997 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
999 if (pos != bmap->n_ineq - 1) {
1000 t = bmap->ineq[pos];
1001 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1002 bmap->ineq[bmap->n_ineq - 1] = t;
1003 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1009 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1011 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1014 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1019 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1022 k = isl_basic_map_alloc_equality(bmap);
1025 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1028 isl_basic_map_free(bmap);
1032 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1035 return (isl_basic_set *)
1036 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1039 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1044 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1047 k = isl_basic_map_alloc_inequality(bmap);
1050 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1053 isl_basic_map_free(bmap);
1057 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1060 return (isl_basic_set *)
1061 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1064 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1068 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1069 isl_seq_clr(bmap->div[bmap->n_div] +
1070 1 + 1 + isl_basic_map_total_dim(bmap),
1071 bmap->extra - bmap->n_div);
1072 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1073 return bmap->n_div++;
1076 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1078 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1081 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1085 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1090 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1092 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1095 /* Copy constraint from src to dst, putting the vars of src at offset
1096 * dim_off in dst and the divs of src at offset div_off in dst.
1097 * If both sets are actually map, then dim_off applies to the input
1100 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1101 struct isl_basic_map *src_map, isl_int *src,
1102 unsigned in_off, unsigned out_off, unsigned div_off)
1104 unsigned src_nparam = isl_basic_map_n_param(src_map);
1105 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1106 unsigned src_in = isl_basic_map_n_in(src_map);
1107 unsigned dst_in = isl_basic_map_n_in(dst_map);
1108 unsigned src_out = isl_basic_map_n_out(src_map);
1109 unsigned dst_out = isl_basic_map_n_out(dst_map);
1110 isl_int_set(dst[0], src[0]);
1111 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1112 if (dst_nparam > src_nparam)
1113 isl_seq_clr(dst+1+src_nparam,
1114 dst_nparam - src_nparam);
1115 isl_seq_clr(dst+1+dst_nparam, in_off);
1116 isl_seq_cpy(dst+1+dst_nparam+in_off,
1118 isl_min(dst_in-in_off, src_in));
1119 if (dst_in-in_off > src_in)
1120 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1121 dst_in - in_off - src_in);
1122 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1123 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1124 src+1+src_nparam+src_in,
1125 isl_min(dst_out-out_off, src_out));
1126 if (dst_out-out_off > src_out)
1127 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1128 dst_out - out_off - src_out);
1129 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1130 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1131 src+1+src_nparam+src_in+src_out,
1132 isl_min(dst_map->extra-div_off, src_map->n_div));
1133 if (dst_map->n_div-div_off > src_map->n_div)
1134 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1135 div_off+src_map->n_div,
1136 dst_map->n_div - div_off - src_map->n_div);
1139 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1140 struct isl_basic_map *src_map, isl_int *src,
1141 unsigned in_off, unsigned out_off, unsigned div_off)
1143 isl_int_set(dst[0], src[0]);
1144 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1147 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1148 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1153 if (!bmap1 || !bmap2)
1156 div_off = bmap1->n_div;
1158 for (i = 0; i < bmap2->n_eq; ++i) {
1159 int i1 = isl_basic_map_alloc_equality(bmap1);
1162 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1163 i_pos, o_pos, div_off);
1166 for (i = 0; i < bmap2->n_ineq; ++i) {
1167 int i1 = isl_basic_map_alloc_inequality(bmap1);
1170 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1171 i_pos, o_pos, div_off);
1174 for (i = 0; i < bmap2->n_div; ++i) {
1175 int i1 = isl_basic_map_alloc_div(bmap1);
1178 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1179 i_pos, o_pos, div_off);
1182 isl_basic_map_free(bmap2);
1187 isl_basic_map_free(bmap1);
1188 isl_basic_map_free(bmap2);
1192 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1193 struct isl_basic_set *bset2, unsigned pos)
1195 return (struct isl_basic_set *)
1196 add_constraints((struct isl_basic_map *)bset1,
1197 (struct isl_basic_map *)bset2, 0, pos);
1200 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1201 struct isl_dim *dim, unsigned extra,
1202 unsigned n_eq, unsigned n_ineq)
1204 struct isl_basic_map *ext;
1214 dims_ok = isl_dim_equal(base->dim, dim) &&
1215 base->extra >= base->n_div + extra;
1217 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1218 room_for_ineq(base, n_ineq)) {
1223 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1224 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1225 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1226 extra += base->extra;
1228 n_ineq += base->n_ineq;
1230 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1236 ext->sample = isl_vec_copy(base->sample);
1237 flags = base->flags;
1238 ext = add_constraints(ext, base, 0, 0);
1241 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1248 isl_basic_map_free(base);
1252 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1253 struct isl_dim *dim, unsigned extra,
1254 unsigned n_eq, unsigned n_ineq)
1256 return (struct isl_basic_set *)
1257 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1258 extra, n_eq, n_ineq);
1261 struct isl_basic_map *isl_basic_map_extend_constraints(
1262 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1266 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1270 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1271 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1272 unsigned n_eq, unsigned n_ineq)
1274 struct isl_basic_map *bmap;
1275 struct isl_dim *dim;
1279 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1283 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1286 isl_basic_map_free(base);
1290 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1291 unsigned nparam, unsigned dim, unsigned extra,
1292 unsigned n_eq, unsigned n_ineq)
1294 return (struct isl_basic_set *)
1295 isl_basic_map_extend((struct isl_basic_map *)base,
1296 nparam, 0, dim, extra, n_eq, n_ineq);
1299 struct isl_basic_set *isl_basic_set_extend_constraints(
1300 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1302 return (struct isl_basic_set *)
1303 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1307 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1309 return (struct isl_basic_set *)
1310 isl_basic_map_cow((struct isl_basic_map *)bset);
1313 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1318 if (bmap->ref > 1) {
1320 bmap = isl_basic_map_dup(bmap);
1323 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1327 struct isl_set *isl_set_cow(struct isl_set *set)
1335 return isl_set_dup(set);
1338 struct isl_map *isl_map_cow(struct isl_map *map)
1346 return isl_map_dup(map);
1349 static void swap_vars(struct isl_blk blk, isl_int *a,
1350 unsigned a_len, unsigned b_len)
1352 isl_seq_cpy(blk.data, a+a_len, b_len);
1353 isl_seq_cpy(blk.data+b_len, a, a_len);
1354 isl_seq_cpy(a, blk.data, b_len+a_len);
1357 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1358 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1366 isl_assert(bmap->ctx,
1367 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1369 if (n1 == 0 || n2 == 0)
1372 bmap = isl_basic_map_cow(bmap);
1376 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1377 if (isl_blk_is_error(blk))
1380 for (i = 0; i < bmap->n_eq; ++i)
1382 bmap->eq[i] + pos, n1, n2);
1384 for (i = 0; i < bmap->n_ineq; ++i)
1386 bmap->ineq[i] + pos, n1, n2);
1388 for (i = 0; i < bmap->n_div; ++i)
1390 bmap->div[i]+1 + pos, n1, n2);
1392 isl_blk_free(bmap->ctx, blk);
1394 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1395 bmap = isl_basic_map_gauss(bmap, NULL);
1396 return isl_basic_map_finalize(bmap);
1398 isl_basic_map_free(bmap);
1402 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1403 __isl_take isl_basic_set *bset, unsigned n)
1408 nparam = isl_basic_set_n_param(bset);
1409 dim = isl_basic_set_n_dim(bset);
1410 isl_assert(bset->ctx, n <= dim, goto error);
1412 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1414 isl_basic_set_free(bset);
1418 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1424 total = isl_basic_map_total_dim(bmap);
1425 isl_basic_map_free_div(bmap, bmap->n_div);
1426 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1428 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1430 i = isl_basic_map_alloc_equality(bmap);
1434 isl_int_set_si(bmap->eq[i][0], 1);
1435 isl_seq_clr(bmap->eq[i]+1, total);
1436 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1437 isl_vec_free(bmap->sample);
1438 bmap->sample = NULL;
1439 return isl_basic_map_finalize(bmap);
1441 isl_basic_map_free(bmap);
1445 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1447 return (struct isl_basic_set *)
1448 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1451 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1454 unsigned off = isl_dim_total(bmap->dim);
1455 isl_int *t = bmap->div[a];
1456 bmap->div[a] = bmap->div[b];
1459 for (i = 0; i < bmap->n_eq; ++i)
1460 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1462 for (i = 0; i < bmap->n_ineq; ++i)
1463 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1465 for (i = 0; i < bmap->n_div; ++i)
1466 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1467 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1470 /* Eliminate the specified n dimensions starting at first from the
1471 * constraints using Fourier-Motzkin. The dimensions themselves
1474 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1475 enum isl_dim_type type, unsigned first, unsigned n)
1484 map = isl_map_cow(map);
1487 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1488 first += pos(map->dim, type) - 1;
1490 for (i = 0; i < map->n; ++i) {
1491 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1501 /* Eliminate the specified n dimensions starting at first from the
1502 * constraints using Fourier-Motzkin. The dimensions themselves
1505 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1506 enum isl_dim_type type, unsigned first, unsigned n)
1508 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1511 /* Eliminate the specified n dimensions starting at first from the
1512 * constraints using Fourier-Motzkin. The dimensions themselves
1515 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1516 unsigned first, unsigned n)
1518 return isl_set_eliminate(set, isl_dim_set, first, n);
1521 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1522 __isl_take isl_basic_map *bmap)
1524 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1529 return isl_basic_map_finalize(bmap);
1532 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1533 __isl_take isl_basic_set *bset)
1535 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1536 (struct isl_basic_map *)bset);
1539 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1548 map = isl_map_cow(map);
1552 for (i = 0; i < map->n; ++i) {
1553 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1563 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1565 return isl_map_remove_divs(set);
1568 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1569 enum isl_dim_type type, unsigned first, unsigned n)
1573 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1577 bmap = isl_basic_map_eliminate_vars(bmap,
1578 isl_basic_map_offset(bmap, type) - 1 + first, n);
1581 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1583 bmap = isl_basic_map_drop(bmap, type, first, n);
1586 isl_basic_map_free(bmap);
1590 /* Return true if the definition of the given div (recursively) involves
1591 * any of the given variables.
1593 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1594 unsigned first, unsigned n)
1597 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1599 if (isl_int_is_zero(bmap->div[div][0]))
1601 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1604 for (i = bmap->n_div - 1; i >= 0; --i) {
1605 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1607 if (div_involves_vars(bmap, i, first, n))
1614 /* Remove all divs (recursively) involving any of the given dimensions
1615 * in their definitions.
1617 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1618 __isl_take isl_basic_map *bmap,
1619 enum isl_dim_type type, unsigned first, unsigned n)
1625 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1627 first += isl_basic_map_offset(bmap, type);
1629 for (i = bmap->n_div - 1; i >= 0; --i) {
1630 if (!div_involves_vars(bmap, i, first, n))
1632 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1640 isl_basic_map_free(bmap);
1644 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1645 enum isl_dim_type type, unsigned first, unsigned n)
1654 map = isl_map_cow(map);
1658 for (i = 0; i < map->n; ++i) {
1659 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1670 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1671 enum isl_dim_type type, unsigned first, unsigned n)
1673 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1677 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1678 enum isl_dim_type type, unsigned first, unsigned n)
1685 if (first + n > isl_basic_map_dim(bmap, type))
1686 isl_die(bmap->ctx, isl_error_invalid,
1687 "index out of bounds", return -1);
1689 first += isl_basic_map_offset(bmap, type);
1690 for (i = 0; i < bmap->n_eq; ++i)
1691 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1693 for (i = 0; i < bmap->n_ineq; ++i)
1694 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1700 int isl_map_involves_dims(__isl_keep isl_map *map,
1701 enum isl_dim_type type, unsigned first, unsigned n)
1708 if (first + n > isl_map_dim(map, type))
1709 isl_die(map->ctx, isl_error_invalid,
1710 "index out of bounds", return -1);
1712 for (i = 0; i < map->n; ++i) {
1713 int involves = isl_basic_map_involves_dims(map->p[i],
1715 if (involves < 0 || !involves)
1722 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1723 enum isl_dim_type type, unsigned first, unsigned n)
1725 return isl_basic_map_involves_dims(bset, type, first, n);
1728 int isl_set_involves_dims(__isl_keep isl_set *set,
1729 enum isl_dim_type type, unsigned first, unsigned n)
1731 return isl_map_involves_dims(set, type, first, n);
1734 /* Return true if the definition of the given div is unknown or depends
1737 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1740 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1742 if (isl_int_is_zero(bmap->div[div][0]))
1745 for (i = bmap->n_div - 1; i >= 0; --i) {
1746 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1748 if (div_is_unknown(bmap, i))
1755 /* Remove all divs that are unknown or defined in terms of unknown divs.
1757 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1758 __isl_take isl_basic_map *bmap)
1765 for (i = bmap->n_div - 1; i >= 0; --i) {
1766 if (!div_is_unknown(bmap, i))
1768 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1774 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1783 map = isl_map_cow(map);
1787 for (i = 0; i < map->n; ++i) {
1788 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1798 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1800 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1803 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1804 __isl_take isl_basic_set *bset,
1805 enum isl_dim_type type, unsigned first, unsigned n)
1807 return (isl_basic_set *)
1808 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1811 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1812 enum isl_dim_type type, unsigned first, unsigned n)
1819 map = isl_map_cow(map);
1822 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1824 for (i = 0; i < map->n; ++i) {
1825 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1826 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1830 map = isl_map_drop(map, type, first, n);
1837 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1838 enum isl_dim_type type, unsigned first, unsigned n)
1840 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1843 /* Project out n inputs starting at first using Fourier-Motzkin */
1844 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1845 unsigned first, unsigned n)
1847 return isl_map_remove_dims(map, isl_dim_in, first, n);
1850 static void dump_term(struct isl_basic_map *bmap,
1851 isl_int c, int pos, FILE *out)
1854 unsigned in = isl_basic_map_n_in(bmap);
1855 unsigned dim = in + isl_basic_map_n_out(bmap);
1856 unsigned nparam = isl_basic_map_n_param(bmap);
1858 isl_int_print(out, c, 0);
1860 if (!isl_int_is_one(c))
1861 isl_int_print(out, c, 0);
1862 if (pos < 1 + nparam) {
1863 name = isl_dim_get_name(bmap->dim,
1864 isl_dim_param, pos - 1);
1866 fprintf(out, "%s", name);
1868 fprintf(out, "p%d", pos - 1);
1869 } else if (pos < 1 + nparam + in)
1870 fprintf(out, "i%d", pos - 1 - nparam);
1871 else if (pos < 1 + nparam + dim)
1872 fprintf(out, "o%d", pos - 1 - nparam - in);
1874 fprintf(out, "e%d", pos - 1 - nparam - dim);
1878 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1879 int sign, FILE *out)
1883 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1887 for (i = 0, first = 1; i < len; ++i) {
1888 if (isl_int_sgn(c[i]) * sign <= 0)
1891 fprintf(out, " + ");
1893 isl_int_abs(v, c[i]);
1894 dump_term(bmap, v, i, out);
1901 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1902 const char *op, FILE *out, int indent)
1906 fprintf(out, "%*s", indent, "");
1908 dump_constraint_sign(bmap, c, 1, out);
1909 fprintf(out, " %s ", op);
1910 dump_constraint_sign(bmap, c, -1, out);
1914 for (i = bmap->n_div; i < bmap->extra; ++i) {
1915 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1917 fprintf(out, "%*s", indent, "");
1918 fprintf(out, "ERROR: unused div coefficient not zero\n");
1923 static void dump_constraints(struct isl_basic_map *bmap,
1924 isl_int **c, unsigned n,
1925 const char *op, FILE *out, int indent)
1929 for (i = 0; i < n; ++i)
1930 dump_constraint(bmap, c[i], op, out, indent);
1933 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1937 unsigned total = isl_basic_map_total_dim(bmap);
1939 for (j = 0; j < 1 + total; ++j) {
1940 if (isl_int_is_zero(exp[j]))
1942 if (!first && isl_int_is_pos(exp[j]))
1944 dump_term(bmap, exp[j], j, out);
1949 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1953 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1954 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1956 for (i = 0; i < bmap->n_div; ++i) {
1957 fprintf(out, "%*s", indent, "");
1958 fprintf(out, "e%d = [(", i);
1959 dump_affine(bmap, bmap->div[i]+1, out);
1961 isl_int_print(out, bmap->div[i][0], 0);
1962 fprintf(out, "]\n");
1966 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1967 FILE *out, int indent)
1970 fprintf(out, "null basic set\n");
1974 fprintf(out, "%*s", indent, "");
1975 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1976 bset->ref, bset->dim->nparam, bset->dim->n_out,
1977 bset->extra, bset->flags);
1978 dump((struct isl_basic_map *)bset, out, indent);
1981 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1982 FILE *out, int indent)
1985 fprintf(out, "null basic map\n");
1989 fprintf(out, "%*s", indent, "");
1990 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1991 "flags: %x, n_name: %d\n",
1993 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1994 bmap->extra, bmap->flags, bmap->dim->n_id);
1995 dump(bmap, out, indent);
1998 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2003 total = isl_basic_map_total_dim(bmap);
2004 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2005 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2006 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2007 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2011 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2013 struct isl_set *set;
2017 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2018 isl_assert(dim->ctx, n >= 0, goto error);
2019 set = isl_alloc(dim->ctx, struct isl_set,
2020 sizeof(struct isl_set) +
2021 (n - 1) * sizeof(struct isl_basic_set *));
2025 set->ctx = dim->ctx;
2026 isl_ctx_ref(set->ctx);
2038 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2039 unsigned nparam, unsigned dim, int n, unsigned flags)
2041 struct isl_set *set;
2042 struct isl_dim *dims;
2044 dims = isl_dim_alloc(ctx, nparam, 0, dim);
2048 set = isl_set_alloc_dim(dims, n, flags);
2052 /* Make sure "map" has room for at least "n" more basic maps.
2054 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2057 struct isl_map *grown = NULL;
2061 isl_assert(map->ctx, n >= 0, goto error);
2062 if (map->n + n <= map->size)
2064 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
2067 for (i = 0; i < map->n; ++i) {
2068 grown->p[i] = isl_basic_map_copy(map->p[i]);
2076 isl_map_free(grown);
2081 /* Make sure "set" has room for at least "n" more basic sets.
2083 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2085 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2088 struct isl_set *isl_set_dup(struct isl_set *set)
2091 struct isl_set *dup;
2096 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
2099 for (i = 0; i < set->n; ++i)
2100 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2104 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2106 return isl_map_from_basic_map(bset);
2109 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2111 struct isl_map *map;
2116 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2117 return isl_map_add_basic_map(map, bmap);
2120 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2121 __isl_take isl_basic_set *bset)
2123 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2124 (struct isl_basic_map *)bset);
2127 void isl_set_free(struct isl_set *set)
2137 isl_ctx_deref(set->ctx);
2138 for (i = 0; i < set->n; ++i)
2139 isl_basic_set_free(set->p[i]);
2140 isl_dim_free(set->dim);
2144 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2149 fprintf(out, "null set\n");
2153 fprintf(out, "%*s", indent, "");
2154 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2155 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2157 for (i = 0; i < set->n; ++i) {
2158 fprintf(out, "%*s", indent, "");
2159 fprintf(out, "basic set %d:\n", i);
2160 isl_basic_set_print_internal(set->p[i], out, indent+4);
2164 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2169 fprintf(out, "null map\n");
2173 fprintf(out, "%*s", indent, "");
2174 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2175 "flags: %x, n_name: %d\n",
2176 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2177 map->dim->n_out, map->flags, map->dim->n_id);
2178 for (i = 0; i < map->n; ++i) {
2179 fprintf(out, "%*s", indent, "");
2180 fprintf(out, "basic map %d:\n", i);
2181 isl_basic_map_print_internal(map->p[i], out, indent+4);
2185 struct isl_basic_map *isl_basic_map_intersect_domain(
2186 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2188 struct isl_basic_map *bmap_domain;
2193 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2194 bset->dim, isl_dim_param), goto error);
2196 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2197 isl_assert(bset->ctx,
2198 isl_basic_map_compatible_domain(bmap, bset), goto error);
2200 bmap = isl_basic_map_cow(bmap);
2203 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2204 bset->n_div, bset->n_eq, bset->n_ineq);
2205 bmap_domain = isl_basic_map_from_domain(bset);
2206 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2208 bmap = isl_basic_map_simplify(bmap);
2209 return isl_basic_map_finalize(bmap);
2211 isl_basic_map_free(bmap);
2212 isl_basic_set_free(bset);
2216 struct isl_basic_map *isl_basic_map_intersect_range(
2217 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2219 struct isl_basic_map *bmap_range;
2224 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2225 bset->dim, isl_dim_param), goto error);
2227 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2228 isl_assert(bset->ctx,
2229 isl_basic_map_compatible_range(bmap, bset), goto error);
2231 if (isl_basic_set_is_universe(bset)) {
2232 isl_basic_set_free(bset);
2236 bmap = isl_basic_map_cow(bmap);
2239 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2240 bset->n_div, bset->n_eq, bset->n_ineq);
2241 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2242 bmap = add_constraints(bmap, bmap_range, 0, 0);
2244 bmap = isl_basic_map_simplify(bmap);
2245 return isl_basic_map_finalize(bmap);
2247 isl_basic_map_free(bmap);
2248 isl_basic_set_free(bset);
2252 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2258 total = 1 + isl_basic_map_total_dim(bmap);
2259 if (total != vec->size)
2264 for (i = 0; i < bmap->n_eq; ++i) {
2265 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2266 if (!isl_int_is_zero(s)) {
2272 for (i = 0; i < bmap->n_ineq; ++i) {
2273 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2274 if (isl_int_is_neg(s)) {
2285 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2287 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2290 struct isl_basic_map *isl_basic_map_intersect(
2291 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2293 struct isl_vec *sample = NULL;
2295 if (!bmap1 || !bmap2)
2298 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2299 bmap2->dim, isl_dim_param), goto error);
2300 if (isl_dim_total(bmap1->dim) ==
2301 isl_dim_size(bmap1->dim, isl_dim_param) &&
2302 isl_dim_total(bmap2->dim) !=
2303 isl_dim_size(bmap2->dim, isl_dim_param))
2304 return isl_basic_map_intersect(bmap2, bmap1);
2306 if (isl_dim_total(bmap2->dim) !=
2307 isl_dim_size(bmap2->dim, isl_dim_param))
2308 isl_assert(bmap1->ctx,
2309 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2311 if (bmap1->sample &&
2312 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2313 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2314 sample = isl_vec_copy(bmap1->sample);
2315 else if (bmap2->sample &&
2316 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2317 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2318 sample = isl_vec_copy(bmap2->sample);
2320 bmap1 = isl_basic_map_cow(bmap1);
2323 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2324 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2325 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2328 isl_vec_free(sample);
2330 isl_vec_free(bmap1->sample);
2331 bmap1->sample = sample;
2334 bmap1 = isl_basic_map_simplify(bmap1);
2335 return isl_basic_map_finalize(bmap1);
2338 isl_vec_free(sample);
2339 isl_basic_map_free(bmap1);
2340 isl_basic_map_free(bmap2);
2344 struct isl_basic_set *isl_basic_set_intersect(
2345 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2347 return (struct isl_basic_set *)
2348 isl_basic_map_intersect(
2349 (struct isl_basic_map *)bset1,
2350 (struct isl_basic_map *)bset2);
2353 /* Special case of isl_map_intersect, where both map1 and map2
2354 * are convex, without any divs and such that either map1 or map2
2355 * contains a single constraint. This constraint is then simply
2356 * added to the other map.
2358 static __isl_give isl_map *map_intersect_add_constraint(
2359 __isl_take isl_map *map1, __isl_take isl_map *map2)
2361 isl_assert(map1->ctx, map1->n == 1, goto error);
2362 isl_assert(map2->ctx, map1->n == 1, goto error);
2363 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2364 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2366 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2367 return isl_map_intersect(map2, map1);
2369 isl_assert(map2->ctx,
2370 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2372 map1 = isl_map_cow(map1);
2375 if (isl_map_plain_is_empty(map1)) {
2379 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2380 if (map2->p[0]->n_eq == 1)
2381 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2383 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2384 map2->p[0]->ineq[0]);
2386 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2387 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2391 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2392 isl_basic_map_free(map1->p[0]);
2405 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2406 __isl_take isl_map *map2)
2409 struct isl_map *result;
2415 if (isl_map_plain_is_empty(map1) &&
2416 isl_dim_equal(map1->dim, map2->dim)) {
2420 if (isl_map_plain_is_empty(map2) &&
2421 isl_dim_equal(map1->dim, map2->dim)) {
2426 if (map1->n == 1 && map2->n == 1 &&
2427 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2428 isl_dim_equal(map1->dim, map2->dim) &&
2429 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2430 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2431 return map_intersect_add_constraint(map1, map2);
2432 if (isl_dim_total(map1->dim) ==
2433 isl_dim_size(map1->dim, isl_dim_param) &&
2434 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2435 return isl_map_intersect(map2, map1);
2437 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2438 isl_assert(map1->ctx,
2439 isl_dim_equal(map1->dim, map2->dim), goto error);
2441 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2442 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2443 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2445 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2446 map1->n * map2->n, flags);
2449 for (i = 0; i < map1->n; ++i)
2450 for (j = 0; j < map2->n; ++j) {
2451 struct isl_basic_map *part;
2452 part = isl_basic_map_intersect(
2453 isl_basic_map_copy(map1->p[i]),
2454 isl_basic_map_copy(map2->p[j]));
2455 if (isl_basic_map_is_empty(part))
2456 isl_basic_map_free(part);
2458 result = isl_map_add_basic_map(result, part);
2471 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2472 __isl_take isl_map *map2)
2474 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2477 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2479 return (struct isl_set *)
2480 isl_map_intersect((struct isl_map *)set1,
2481 (struct isl_map *)set2);
2484 /* The current implementation of isl_map_intersect accepts intersections
2485 * with parameter domains, so we can just call that for now.
2487 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2488 __isl_take isl_set *params)
2490 return isl_map_intersect(map, params);
2493 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2494 __isl_take isl_map *map2)
2496 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2499 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2500 __isl_take isl_set *params)
2502 return isl_map_intersect_params(set, params);
2505 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2507 struct isl_dim *dim;
2508 struct isl_basic_set *bset;
2513 bmap = isl_basic_map_cow(bmap);
2516 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2517 in = isl_basic_map_n_in(bmap);
2518 bset = isl_basic_set_from_basic_map(bmap);
2519 bset = isl_basic_set_swap_vars(bset, in);
2520 return isl_basic_map_from_basic_set(bset, dim);
2523 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2524 enum isl_dim_type type, unsigned pos, unsigned n)
2526 struct isl_dim *res_dim;
2527 struct isl_basic_map *res;
2528 struct isl_dim_map *dim_map;
2529 unsigned total, off;
2530 enum isl_dim_type t;
2538 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2540 total = isl_basic_map_total_dim(bmap) + n;
2541 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2543 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2545 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2547 unsigned size = isl_basic_map_dim(bmap, t);
2548 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2550 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2551 pos, size - pos, off + pos + n);
2553 off += isl_dim_size(res_dim, t);
2555 isl_dim_map_div(dim_map, bmap, off);
2557 res = isl_basic_map_alloc_dim(res_dim,
2558 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2559 if (isl_basic_map_is_rational(bmap))
2560 res = isl_basic_map_set_rational(res);
2561 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2562 return isl_basic_map_finalize(res);
2565 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2566 enum isl_dim_type type, unsigned n)
2570 return isl_basic_map_insert(bmap, type,
2571 isl_basic_map_dim(bmap, type), n);
2574 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2575 enum isl_dim_type type, unsigned n)
2579 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2580 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2582 isl_basic_set_free(bset);
2586 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2587 enum isl_dim_type type, unsigned pos, unsigned n)
2594 map = isl_map_cow(map);
2598 map->dim = isl_dim_insert(map->dim, type, pos, n);
2602 for (i = 0; i < map->n; ++i) {
2603 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2614 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2615 enum isl_dim_type type, unsigned pos, unsigned n)
2617 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2620 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2621 enum isl_dim_type type, unsigned n)
2625 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2628 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2629 enum isl_dim_type type, unsigned n)
2633 isl_assert(set->ctx, type != isl_dim_in, goto error);
2634 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2640 __isl_give isl_basic_map *isl_basic_map_move_dims(
2641 __isl_take isl_basic_map *bmap,
2642 enum isl_dim_type dst_type, unsigned dst_pos,
2643 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2645 struct isl_dim_map *dim_map;
2646 struct isl_basic_map *res;
2647 enum isl_dim_type t;
2648 unsigned total, off;
2655 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2658 if (dst_type == src_type && dst_pos == src_pos)
2661 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2663 if (pos(bmap->dim, dst_type) + dst_pos ==
2664 pos(bmap->dim, src_type) + src_pos +
2665 ((src_type < dst_type) ? n : 0)) {
2666 bmap = isl_basic_map_cow(bmap);
2670 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2671 src_type, src_pos, n);
2675 bmap = isl_basic_map_finalize(bmap);
2680 total = isl_basic_map_total_dim(bmap);
2681 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2684 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2685 unsigned size = isl_dim_size(bmap->dim, t);
2686 if (t == dst_type) {
2687 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2690 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2693 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2694 dst_pos, size - dst_pos, off);
2695 off += size - dst_pos;
2696 } else if (t == src_type) {
2697 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2700 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2701 src_pos + n, size - src_pos - n, off);
2702 off += size - src_pos - n;
2704 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2708 isl_dim_map_div(dim_map, bmap, off);
2710 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2711 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2712 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2714 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2715 src_type, src_pos, n);
2719 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2720 bmap = isl_basic_map_gauss(bmap, NULL);
2721 bmap = isl_basic_map_finalize(bmap);
2725 isl_basic_map_free(bmap);
2729 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2730 enum isl_dim_type dst_type, unsigned dst_pos,
2731 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2733 return (isl_basic_set *)isl_basic_map_move_dims(
2734 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2737 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2738 enum isl_dim_type dst_type, unsigned dst_pos,
2739 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2743 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2744 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2745 src_type, src_pos, n);
2751 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2752 enum isl_dim_type dst_type, unsigned dst_pos,
2753 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2762 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2765 if (dst_type == src_type && dst_pos == src_pos)
2768 isl_assert(map->ctx, dst_type != src_type, goto error);
2770 map = isl_map_cow(map);
2774 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2778 for (i = 0; i < map->n; ++i) {
2779 map->p[i] = isl_basic_map_move_dims(map->p[i],
2781 src_type, src_pos, n);
2792 /* Move the specified dimensions to the last columns right before
2793 * the divs. Don't change the dimension specification of bmap.
2794 * That's the responsibility of the caller.
2796 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2797 enum isl_dim_type type, unsigned first, unsigned n)
2799 struct isl_dim_map *dim_map;
2800 struct isl_basic_map *res;
2801 enum isl_dim_type t;
2802 unsigned total, off;
2806 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2809 total = isl_basic_map_total_dim(bmap);
2810 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2813 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2814 unsigned size = isl_dim_size(bmap->dim, t);
2816 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2819 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2820 first, n, total - bmap->n_div - n);
2821 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2822 first + n, size - (first + n), off);
2823 off += size - (first + n);
2825 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2829 isl_dim_map_div(dim_map, bmap, off + n);
2831 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2832 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2833 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2837 /* Turn the n dimensions of type type, starting at first
2838 * into existentially quantified variables.
2840 __isl_give isl_basic_map *isl_basic_map_project_out(
2841 __isl_take isl_basic_map *bmap,
2842 enum isl_dim_type type, unsigned first, unsigned n)
2855 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2856 return isl_basic_map_remove_dims(bmap, type, first, n);
2858 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2861 bmap = move_last(bmap, type, first, n);
2862 bmap = isl_basic_map_cow(bmap);
2866 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2867 old = bmap->block2.data;
2868 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2869 (bmap->extra + n) * (1 + row_size));
2870 if (!bmap->block2.data)
2872 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2875 for (i = 0; i < n; ++i) {
2876 new_div[i] = bmap->block2.data +
2877 (bmap->extra + i) * (1 + row_size);
2878 isl_seq_clr(new_div[i], 1 + row_size);
2880 for (i = 0; i < bmap->extra; ++i)
2881 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2883 bmap->div = new_div;
2887 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2890 bmap = isl_basic_map_simplify(bmap);
2891 bmap = isl_basic_map_drop_redundant_divs(bmap);
2892 return isl_basic_map_finalize(bmap);
2894 isl_basic_map_free(bmap);
2898 /* Turn the n dimensions of type type, starting at first
2899 * into existentially quantified variables.
2901 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2902 enum isl_dim_type type, unsigned first, unsigned n)
2904 return (isl_basic_set *)isl_basic_map_project_out(
2905 (isl_basic_map *)bset, type, first, n);
2908 /* Turn the n dimensions of type type, starting at first
2909 * into existentially quantified variables.
2911 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2912 enum isl_dim_type type, unsigned first, unsigned n)
2922 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2924 map = isl_map_cow(map);
2928 map->dim = isl_dim_drop(map->dim, type, first, n);
2932 for (i = 0; i < map->n; ++i) {
2933 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2944 /* Turn the n dimensions of type type, starting at first
2945 * into existentially quantified variables.
2947 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2948 enum isl_dim_type type, unsigned first, unsigned n)
2950 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2953 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2957 for (i = 0; i < n; ++i) {
2958 j = isl_basic_map_alloc_div(bmap);
2961 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2965 isl_basic_map_free(bmap);
2969 struct isl_basic_map *isl_basic_map_apply_range(
2970 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2972 struct isl_dim *dim_result = NULL;
2973 struct isl_basic_map *bmap;
2974 unsigned n_in, n_out, n, nparam, total, pos;
2975 struct isl_dim_map *dim_map1, *dim_map2;
2977 if (!bmap1 || !bmap2)
2980 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2981 isl_dim_copy(bmap2->dim));
2983 n_in = isl_basic_map_n_in(bmap1);
2984 n_out = isl_basic_map_n_out(bmap2);
2985 n = isl_basic_map_n_out(bmap1);
2986 nparam = isl_basic_map_n_param(bmap1);
2988 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2989 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2990 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2991 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2992 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2993 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2994 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2995 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2996 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2997 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2998 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3000 bmap = isl_basic_map_alloc_dim(dim_result,
3001 bmap1->n_div + bmap2->n_div + n,
3002 bmap1->n_eq + bmap2->n_eq,
3003 bmap1->n_ineq + bmap2->n_ineq);
3004 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3005 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3006 bmap = add_divs(bmap, n);
3007 bmap = isl_basic_map_simplify(bmap);
3008 bmap = isl_basic_map_drop_redundant_divs(bmap);
3009 return isl_basic_map_finalize(bmap);
3011 isl_basic_map_free(bmap1);
3012 isl_basic_map_free(bmap2);
3016 struct isl_basic_set *isl_basic_set_apply(
3017 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3022 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3025 return (struct isl_basic_set *)
3026 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3028 isl_basic_set_free(bset);
3029 isl_basic_map_free(bmap);
3033 struct isl_basic_map *isl_basic_map_apply_domain(
3034 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3036 if (!bmap1 || !bmap2)
3039 isl_assert(bmap1->ctx,
3040 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3041 isl_assert(bmap1->ctx,
3042 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3045 bmap1 = isl_basic_map_reverse(bmap1);
3046 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3047 return isl_basic_map_reverse(bmap1);
3049 isl_basic_map_free(bmap1);
3050 isl_basic_map_free(bmap2);
3054 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3055 * A \cap B -> f(A) + f(B)
3057 struct isl_basic_map *isl_basic_map_sum(
3058 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3060 unsigned n_in, n_out, nparam, total, pos;
3061 struct isl_basic_map *bmap = NULL;
3062 struct isl_dim_map *dim_map1, *dim_map2;
3065 if (!bmap1 || !bmap2)
3068 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
3071 nparam = isl_basic_map_n_param(bmap1);
3072 n_in = isl_basic_map_n_in(bmap1);
3073 n_out = isl_basic_map_n_out(bmap1);
3075 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3076 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3077 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3078 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3079 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3080 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3081 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3082 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3083 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3084 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3085 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3087 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
3088 bmap1->n_div + bmap2->n_div + 2 * n_out,
3089 bmap1->n_eq + bmap2->n_eq + n_out,
3090 bmap1->n_ineq + bmap2->n_ineq);
3091 for (i = 0; i < n_out; ++i) {
3092 int j = isl_basic_map_alloc_equality(bmap);
3095 isl_seq_clr(bmap->eq[j], 1+total);
3096 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3097 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3098 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3100 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3101 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3102 bmap = add_divs(bmap, 2 * n_out);
3104 bmap = isl_basic_map_simplify(bmap);
3105 return isl_basic_map_finalize(bmap);
3107 isl_basic_map_free(bmap);
3108 isl_basic_map_free(bmap1);
3109 isl_basic_map_free(bmap2);
3113 /* Given two maps A -> f(A) and B -> g(B), construct a map
3114 * A \cap B -> f(A) + f(B)
3116 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3118 struct isl_map *result;
3124 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3126 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3127 map1->n * map2->n, 0);
3130 for (i = 0; i < map1->n; ++i)
3131 for (j = 0; j < map2->n; ++j) {
3132 struct isl_basic_map *part;
3133 part = isl_basic_map_sum(
3134 isl_basic_map_copy(map1->p[i]),
3135 isl_basic_map_copy(map2->p[j]));
3136 if (isl_basic_map_is_empty(part))
3137 isl_basic_map_free(part);
3139 result = isl_map_add_basic_map(result, part);
3152 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3153 __isl_take isl_set *set2)
3155 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3158 /* Given a basic map A -> f(A), construct A -> -f(A).
3160 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3165 bmap = isl_basic_map_cow(bmap);
3169 n = isl_basic_map_dim(bmap, isl_dim_out);
3170 off = isl_basic_map_offset(bmap, isl_dim_out);
3171 for (i = 0; i < bmap->n_eq; ++i)
3172 for (j = 0; j < n; ++j)
3173 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3174 for (i = 0; i < bmap->n_ineq; ++i)
3175 for (j = 0; j < n; ++j)
3176 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3177 for (i = 0; i < bmap->n_div; ++i)
3178 for (j = 0; j < n; ++j)
3179 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3180 return isl_basic_map_finalize(bmap);
3183 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3185 return isl_basic_map_neg(bset);
3188 /* Given a map A -> f(A), construct A -> -f(A).
3190 struct isl_map *isl_map_neg(struct isl_map *map)
3194 map = isl_map_cow(map);
3198 for (i = 0; i < map->n; ++i) {
3199 map->p[i] = isl_basic_map_neg(map->p[i]);
3210 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3212 return (isl_set *)isl_map_neg((isl_map *)set);
3215 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3216 * A -> floor(f(A)/d).
3218 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3221 unsigned n_in, n_out, nparam, total, pos;
3222 struct isl_basic_map *result = NULL;
3223 struct isl_dim_map *dim_map;
3229 nparam = isl_basic_map_n_param(bmap);
3230 n_in = isl_basic_map_n_in(bmap);
3231 n_out = isl_basic_map_n_out(bmap);
3233 total = nparam + n_in + n_out + bmap->n_div + n_out;
3234 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3235 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3236 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3237 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3238 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3240 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3241 bmap->n_div + n_out,
3242 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3243 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3244 result = add_divs(result, n_out);
3245 for (i = 0; i < n_out; ++i) {
3247 j = isl_basic_map_alloc_inequality(result);
3250 isl_seq_clr(result->ineq[j], 1+total);
3251 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3252 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3253 j = isl_basic_map_alloc_inequality(result);
3256 isl_seq_clr(result->ineq[j], 1+total);
3257 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3258 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3259 isl_int_sub_ui(result->ineq[j][0], d, 1);
3262 result = isl_basic_map_simplify(result);
3263 return isl_basic_map_finalize(result);
3265 isl_basic_map_free(result);
3269 /* Given a map A -> f(A) and an integer d, construct a map
3270 * A -> floor(f(A)/d).
3272 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3276 map = isl_map_cow(map);
3280 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3281 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3282 for (i = 0; i < map->n; ++i) {
3283 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3294 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3300 i = isl_basic_map_alloc_equality(bmap);
3303 nparam = isl_basic_map_n_param(bmap);
3304 n_in = isl_basic_map_n_in(bmap);
3305 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3306 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3307 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3308 return isl_basic_map_finalize(bmap);
3310 isl_basic_map_free(bmap);
3314 /* Add a constraints to "bmap" expressing i_pos < o_pos
3316 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3322 i = isl_basic_map_alloc_inequality(bmap);
3325 nparam = isl_basic_map_n_param(bmap);
3326 n_in = isl_basic_map_n_in(bmap);
3327 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3328 isl_int_set_si(bmap->ineq[i][0], -1);
3329 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3330 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3331 return isl_basic_map_finalize(bmap);
3333 isl_basic_map_free(bmap);
3337 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3339 static __isl_give isl_basic_map *var_less_or_equal(
3340 __isl_take isl_basic_map *bmap, unsigned pos)
3346 i = isl_basic_map_alloc_inequality(bmap);
3349 nparam = isl_basic_map_n_param(bmap);
3350 n_in = isl_basic_map_n_in(bmap);
3351 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3352 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3353 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3354 return isl_basic_map_finalize(bmap);
3356 isl_basic_map_free(bmap);
3360 /* Add a constraints to "bmap" expressing i_pos > o_pos
3362 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3368 i = isl_basic_map_alloc_inequality(bmap);
3371 nparam = isl_basic_map_n_param(bmap);
3372 n_in = isl_basic_map_n_in(bmap);
3373 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3374 isl_int_set_si(bmap->ineq[i][0], -1);
3375 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3376 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3377 return isl_basic_map_finalize(bmap);
3379 isl_basic_map_free(bmap);
3383 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3385 static __isl_give isl_basic_map *var_more_or_equal(
3386 __isl_take isl_basic_map *bmap, unsigned pos)
3392 i = isl_basic_map_alloc_inequality(bmap);
3395 nparam = isl_basic_map_n_param(bmap);
3396 n_in = isl_basic_map_n_in(bmap);
3397 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3398 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3399 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3400 return isl_basic_map_finalize(bmap);
3402 isl_basic_map_free(bmap);
3406 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3409 struct isl_basic_map *bmap;
3410 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3413 for (i = 0; i < n_equal && bmap; ++i)
3414 bmap = var_equal(bmap, i);
3415 return isl_basic_map_finalize(bmap);
3418 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3420 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3423 struct isl_basic_map *bmap;
3424 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3427 for (i = 0; i < pos && bmap; ++i)
3428 bmap = var_equal(bmap, i);
3430 bmap = var_less(bmap, pos);
3431 return isl_basic_map_finalize(bmap);
3434 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3436 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3437 __isl_take isl_dim *dim, unsigned pos)
3440 isl_basic_map *bmap;
3442 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3443 for (i = 0; i < pos; ++i)
3444 bmap = var_equal(bmap, i);
3445 bmap = var_less_or_equal(bmap, pos);
3446 return isl_basic_map_finalize(bmap);
3449 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3451 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3454 struct isl_basic_map *bmap;
3455 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3458 for (i = 0; i < pos && bmap; ++i)
3459 bmap = var_equal(bmap, i);
3461 bmap = var_more(bmap, pos);
3462 return isl_basic_map_finalize(bmap);
3465 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3467 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3468 __isl_take isl_dim *dim, unsigned pos)
3471 isl_basic_map *bmap;
3473 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3474 for (i = 0; i < pos; ++i)
3475 bmap = var_equal(bmap, i);
3476 bmap = var_more_or_equal(bmap, pos);
3477 return isl_basic_map_finalize(bmap);
3480 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3481 unsigned n, int equal)
3483 struct isl_map *map;
3486 if (n == 0 && equal)
3487 return isl_map_universe(dims);
3489 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3491 for (i = 0; i + 1 < n; ++i)
3492 map = isl_map_add_basic_map(map,
3493 isl_basic_map_less_at(isl_dim_copy(dims), i));
3496 map = isl_map_add_basic_map(map,
3497 isl_basic_map_less_or_equal_at(dims, n - 1));
3499 map = isl_map_add_basic_map(map,
3500 isl_basic_map_less_at(dims, n - 1));
3507 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3511 return map_lex_lte_first(dims, dims->n_out, equal);
3514 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3516 return map_lex_lte_first(dim, n, 0);
3519 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3521 return map_lex_lte_first(dim, n, 1);
3524 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3526 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3529 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3531 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3534 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3535 unsigned n, int equal)
3537 struct isl_map *map;
3540 if (n == 0 && equal)
3541 return isl_map_universe(dims);
3543 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3545 for (i = 0; i + 1 < n; ++i)
3546 map = isl_map_add_basic_map(map,
3547 isl_basic_map_more_at(isl_dim_copy(dims), i));
3550 map = isl_map_add_basic_map(map,
3551 isl_basic_map_more_or_equal_at(dims, n - 1));
3553 map = isl_map_add_basic_map(map,
3554 isl_basic_map_more_at(dims, n - 1));
3561 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3565 return map_lex_gte_first(dims, dims->n_out, equal);
3568 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3570 return map_lex_gte_first(dim, n, 0);
3573 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3575 return map_lex_gte_first(dim, n, 1);
3578 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3580 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3583 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3585 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3588 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3589 __isl_take isl_set *set2)
3592 map = isl_map_lex_le(isl_set_get_dim(set1));
3593 map = isl_map_intersect_domain(map, set1);
3594 map = isl_map_intersect_range(map, set2);
3598 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3599 __isl_take isl_set *set2)
3602 map = isl_map_lex_lt(isl_set_get_dim(set1));
3603 map = isl_map_intersect_domain(map, set1);
3604 map = isl_map_intersect_range(map, set2);
3608 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3609 __isl_take isl_set *set2)
3612 map = isl_map_lex_ge(isl_set_get_dim(set1));
3613 map = isl_map_intersect_domain(map, set1);
3614 map = isl_map_intersect_range(map, set2);
3618 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3619 __isl_take isl_set *set2)
3622 map = isl_map_lex_gt(isl_set_get_dim(set1));
3623 map = isl_map_intersect_domain(map, set1);
3624 map = isl_map_intersect_range(map, set2);
3628 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3629 __isl_take isl_map *map2)
3632 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3633 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3634 map = isl_map_apply_range(map, isl_map_reverse(map2));
3638 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3639 __isl_take isl_map *map2)
3642 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3643 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3644 map = isl_map_apply_range(map, isl_map_reverse(map2));
3648 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3649 __isl_take isl_map *map2)
3652 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3653 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3654 map = isl_map_apply_range(map, isl_map_reverse(map2));
3658 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3659 __isl_take isl_map *map2)
3662 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3663 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3664 map = isl_map_apply_range(map, isl_map_reverse(map2));
3668 struct isl_basic_map *isl_basic_map_from_basic_set(
3669 struct isl_basic_set *bset, struct isl_dim *dim)
3671 struct isl_basic_map *bmap;
3673 bset = isl_basic_set_cow(bset);
3677 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3678 isl_dim_free(bset->dim);
3679 bmap = (struct isl_basic_map *) bset;
3681 return isl_basic_map_finalize(bmap);
3683 isl_basic_set_free(bset);
3688 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3692 if (bmap->dim->n_in == 0)
3693 return (struct isl_basic_set *)bmap;
3694 bmap = isl_basic_map_cow(bmap);
3697 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3700 bmap = isl_basic_map_finalize(bmap);
3701 return (struct isl_basic_set *)bmap;
3703 isl_basic_map_free(bmap);
3707 /* For a div d = floor(f/m), add the constraints
3710 * -(f-(n-1)) + m d >= 0
3712 * Note that the second constraint is the negation of
3716 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3717 unsigned pos, isl_int *div)
3720 unsigned total = isl_basic_map_total_dim(bmap);
3722 i = isl_basic_map_alloc_inequality(bmap);
3725 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3726 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3728 j = isl_basic_map_alloc_inequality(bmap);
3731 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3732 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3733 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3737 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3738 unsigned pos, isl_int *div)
3740 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3744 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3746 unsigned total = isl_basic_map_total_dim(bmap);
3747 unsigned div_pos = total - bmap->n_div + div;
3749 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3753 struct isl_basic_set *isl_basic_map_underlying_set(
3754 struct isl_basic_map *bmap)
3758 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3760 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3761 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3762 return (struct isl_basic_set *)bmap;
3763 bmap = isl_basic_map_cow(bmap);
3766 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3769 bmap->extra -= bmap->n_div;
3771 bmap = isl_basic_map_finalize(bmap);
3772 return (struct isl_basic_set *)bmap;
3777 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3778 __isl_take isl_basic_set *bset)
3780 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3783 struct isl_basic_map *isl_basic_map_overlying_set(
3784 struct isl_basic_set *bset, struct isl_basic_map *like)
3786 struct isl_basic_map *bmap;
3787 struct isl_ctx *ctx;
3794 isl_assert(ctx, bset->n_div == 0, goto error);
3795 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3796 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3798 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3799 isl_basic_map_free(like);
3800 return (struct isl_basic_map *)bset;
3802 bset = isl_basic_set_cow(bset);
3805 total = bset->dim->n_out + bset->extra;
3806 bmap = (struct isl_basic_map *)bset;
3807 isl_dim_free(bmap->dim);
3808 bmap->dim = isl_dim_copy(like->dim);
3811 bmap->n_div = like->n_div;
3812 bmap->extra += like->n_div;
3816 ltotal = total - bmap->extra + like->extra;
3819 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3820 bmap->extra * (1 + 1 + total));
3821 if (isl_blk_is_error(bmap->block2))
3823 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3827 for (i = 0; i < bmap->extra; ++i)
3828 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3829 for (i = 0; i < like->n_div; ++i) {
3830 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3831 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3833 bmap = isl_basic_map_extend_constraints(bmap,
3834 0, 2 * like->n_div);
3835 for (i = 0; i < like->n_div; ++i) {
3836 if (isl_int_is_zero(bmap->div[i][0]))
3838 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3842 isl_basic_map_free(like);
3843 bmap = isl_basic_map_simplify(bmap);
3844 bmap = isl_basic_map_finalize(bmap);
3847 isl_basic_map_free(like);
3848 isl_basic_set_free(bset);
3852 struct isl_basic_set *isl_basic_set_from_underlying_set(
3853 struct isl_basic_set *bset, struct isl_basic_set *like)
3855 return (struct isl_basic_set *)
3856 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3859 struct isl_set *isl_set_from_underlying_set(
3860 struct isl_set *set, struct isl_basic_set *like)
3866 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3868 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3869 isl_basic_set_free(like);
3872 set = isl_set_cow(set);
3875 for (i = 0; i < set->n; ++i) {
3876 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3877 isl_basic_set_copy(like));
3881 isl_dim_free(set->dim);
3882 set->dim = isl_dim_copy(like->dim);
3885 isl_basic_set_free(like);
3888 isl_basic_set_free(like);
3893 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3897 map = isl_map_cow(map);
3900 map->dim = isl_dim_cow(map->dim);
3904 for (i = 1; i < map->n; ++i)
3905 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3907 for (i = 0; i < map->n; ++i) {
3908 map->p[i] = (struct isl_basic_map *)
3909 isl_basic_map_underlying_set(map->p[i]);
3914 map->dim = isl_dim_underlying(map->dim, 0);
3916 isl_dim_free(map->dim);
3917 map->dim = isl_dim_copy(map->p[0]->dim);
3921 return (struct isl_set *)map;
3927 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3929 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3932 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3933 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3935 bmap = isl_basic_map_cow(bmap);
3939 isl_dim_free(bmap->dim);
3942 bmap = isl_basic_map_finalize(bmap);
3946 isl_basic_map_free(bmap);
3951 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3952 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3954 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3958 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3959 __isl_take isl_dim *dim)
3963 map = isl_map_cow(map);
3967 for (i = 0; i < map->n; ++i) {
3968 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3973 isl_dim_free(map->dim);
3983 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3984 __isl_take isl_dim *dim)
3986 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3989 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3992 struct isl_basic_set *domain;
3998 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
4000 n_in = isl_basic_map_n_in(bmap);
4001 n_out = isl_basic_map_n_out(bmap);
4002 domain = isl_basic_set_from_basic_map(bmap);
4003 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4005 domain = isl_basic_set_reset_dim(domain, dim);
4010 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4014 return isl_dim_may_be_set(bmap->dim);
4017 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4021 if (isl_basic_map_may_be_set(bmap))
4023 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4026 __isl_give isl_basic_map *isl_basic_map_domain_map(
4027 __isl_take isl_basic_map *bmap)
4031 isl_basic_map *domain;
4032 int nparam, n_in, n_out;
4035 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4036 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4037 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4039 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
4040 domain = isl_basic_map_universe(dim);
4042 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4043 bmap = isl_basic_map_apply_range(bmap, domain);
4044 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4046 total = isl_basic_map_total_dim(bmap);
4048 for (i = 0; i < n_in; ++i) {
4049 k = isl_basic_map_alloc_equality(bmap);
4052 isl_seq_clr(bmap->eq[k], 1 + total);
4053 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4054 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4057 bmap = isl_basic_map_gauss(bmap, NULL);
4058 return isl_basic_map_finalize(bmap);
4060 isl_basic_map_free(bmap);
4064 __isl_give isl_basic_map *isl_basic_map_range_map(
4065 __isl_take isl_basic_map *bmap)
4069 isl_basic_map *range;
4070 int nparam, n_in, n_out;
4073 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4074 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4075 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4077 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
4078 range = isl_basic_map_universe(dim);
4080 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4081 bmap = isl_basic_map_apply_range(bmap, range);
4082 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4084 total = isl_basic_map_total_dim(bmap);
4086 for (i = 0; i < n_out; ++i) {
4087 k = isl_basic_map_alloc_equality(bmap);
4090 isl_seq_clr(bmap->eq[k], 1 + total);
4091 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4092 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4095 bmap = isl_basic_map_gauss(bmap, NULL);
4096 return isl_basic_map_finalize(bmap);
4098 isl_basic_map_free(bmap);
4102 int isl_map_may_be_set(__isl_keep isl_map *map)
4106 return isl_dim_may_be_set(map->dim);
4109 struct isl_set *isl_map_range(struct isl_map *map)
4112 struct isl_set *set;
4116 if (isl_map_may_be_set(map))
4117 return (isl_set *)map;
4119 map = isl_map_cow(map);
4123 set = (struct isl_set *) map;
4124 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
4127 for (i = 0; i < map->n; ++i) {
4128 set->p[i] = isl_basic_map_range(map->p[i]);
4132 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4133 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4140 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4143 isl_dim *domain_dim;
4145 map = isl_map_cow(map);
4149 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
4150 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
4151 map->dim = isl_dim_join(map->dim, domain_dim);
4154 for (i = 0; i < map->n; ++i) {
4155 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4159 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4160 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4167 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4172 map = isl_map_cow(map);
4176 range_dim = isl_dim_range(isl_map_get_dim(map));
4177 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
4178 map->dim = isl_dim_join(map->dim, range_dim);
4181 for (i = 0; i < map->n; ++i) {
4182 map->p[i] = isl_basic_map_range_map(map->p[i]);
4186 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4187 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4194 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4197 struct isl_map *map = NULL;
4199 set = isl_set_cow(set);
4202 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4203 map = (struct isl_map *)set;
4204 for (i = 0; i < set->n; ++i) {
4205 map->p[i] = isl_basic_map_from_basic_set(
4206 set->p[i], isl_dim_copy(dim));
4210 isl_dim_free(map->dim);
4219 __isl_give isl_basic_map *isl_basic_map_from_domain(
4220 __isl_take isl_basic_set *bset)
4222 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4225 __isl_give isl_basic_map *isl_basic_map_from_range(
4226 __isl_take isl_basic_set *bset)
4228 return (isl_basic_map *)bset;
4231 struct isl_map *isl_map_from_range(struct isl_set *set)
4233 return (struct isl_map *)set;
4236 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4238 return isl_map_reverse(isl_map_from_range(set));
4241 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4242 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4244 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4245 isl_basic_map_from_range(range));
4248 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4249 __isl_take isl_set *range)
4251 return isl_map_apply_range(isl_map_from_domain(domain),
4252 isl_map_from_range(range));
4255 struct isl_set *isl_set_from_map(struct isl_map *map)
4258 struct isl_set *set = NULL;
4262 map = isl_map_cow(map);
4265 map->dim = isl_dim_as_set_dim(map->dim);
4268 set = (struct isl_set *)map;
4269 for (i = 0; i < map->n; ++i) {
4270 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4280 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4282 struct isl_map *map;
4286 isl_assert(dim->ctx, n >= 0, return NULL);
4287 map = isl_alloc(dim->ctx, struct isl_map,
4288 sizeof(struct isl_map) +
4289 (n - 1) * sizeof(struct isl_basic_map *));
4293 map->ctx = dim->ctx;
4294 isl_ctx_ref(map->ctx);
4306 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4307 unsigned nparam, unsigned in, unsigned out, int n,
4310 struct isl_map *map;
4311 struct isl_dim *dims;
4313 dims = isl_dim_alloc(ctx, nparam, in, out);
4317 map = isl_map_alloc_dim(dims, n, flags);
4321 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4323 struct isl_basic_map *bmap;
4324 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4325 bmap = isl_basic_map_set_to_empty(bmap);
4329 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4331 struct isl_basic_set *bset;
4332 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4333 bset = isl_basic_set_set_to_empty(bset);
4337 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4339 struct isl_basic_map *bmap;
4342 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4343 bmap = isl_basic_map_set_to_empty(bmap);
4347 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4349 struct isl_basic_map *bmap;
4352 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4353 bmap = isl_basic_map_set_to_empty(bmap);
4357 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4359 struct isl_basic_set *bset;
4362 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4363 bset = isl_basic_set_set_to_empty(bset);
4367 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4369 struct isl_basic_map *bmap;
4370 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4371 bmap = isl_basic_map_finalize(bmap);
4375 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4377 struct isl_basic_set *bset;
4378 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4379 bset = isl_basic_set_finalize(bset);
4383 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4386 unsigned total = isl_dim_total(dim);
4387 isl_basic_map *bmap;
4389 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4390 for (i = 0; i < total; ++i) {
4391 int k = isl_basic_map_alloc_inequality(bmap);
4394 isl_seq_clr(bmap->ineq[k], 1 + total);
4395 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4399 isl_basic_map_free(bmap);
4403 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4405 return isl_basic_map_nat_universe(dim);
4408 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4410 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4413 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4415 return isl_map_nat_universe(dim);
4418 __isl_give isl_basic_map *isl_basic_map_universe_like(
4419 __isl_keep isl_basic_map *model)
4423 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4426 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4430 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4433 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4434 __isl_keep isl_set *model)
4438 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4441 struct isl_map *isl_map_empty(struct isl_dim *dim)
4443 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4446 struct isl_map *isl_map_empty_like(struct isl_map *model)
4450 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4453 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4457 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4460 struct isl_set *isl_set_empty(struct isl_dim *dim)
4462 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4465 struct isl_set *isl_set_empty_like(struct isl_set *model)
4469 return isl_set_empty(isl_dim_copy(model->dim));
4472 struct isl_map *isl_map_universe(struct isl_dim *dim)
4474 struct isl_map *map;
4477 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4478 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4482 struct isl_set *isl_set_universe(struct isl_dim *dim)
4484 struct isl_set *set;
4487 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4488 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4492 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4496 return isl_set_universe(isl_dim_copy(model->dim));
4499 struct isl_map *isl_map_dup(struct isl_map *map)
4502 struct isl_map *dup;
4506 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4507 for (i = 0; i < map->n; ++i)
4508 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4512 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4513 __isl_take isl_basic_map *bmap)
4517 if (isl_basic_map_plain_is_empty(bmap)) {
4518 isl_basic_map_free(bmap);
4521 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4522 isl_assert(map->ctx, map->n < map->size, goto error);
4523 map->p[map->n] = bmap;
4525 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4531 isl_basic_map_free(bmap);
4535 void isl_map_free(struct isl_map *map)
4545 isl_ctx_deref(map->ctx);
4546 for (i = 0; i < map->n; ++i)
4547 isl_basic_map_free(map->p[i]);
4548 isl_dim_free(map->dim);
4552 struct isl_map *isl_map_extend(struct isl_map *base,
4553 unsigned nparam, unsigned n_in, unsigned n_out)
4557 base = isl_map_cow(base);
4561 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4564 for (i = 0; i < base->n; ++i) {
4565 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4566 isl_dim_copy(base->dim), 0, 0, 0);
4576 struct isl_set *isl_set_extend(struct isl_set *base,
4577 unsigned nparam, unsigned dim)
4579 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4583 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4584 struct isl_basic_map *bmap, unsigned pos, int value)
4588 bmap = isl_basic_map_cow(bmap);
4589 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4590 j = isl_basic_map_alloc_equality(bmap);
4593 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4594 isl_int_set_si(bmap->eq[j][pos], -1);
4595 isl_int_set_si(bmap->eq[j][0], value);
4596 bmap = isl_basic_map_simplify(bmap);
4597 return isl_basic_map_finalize(bmap);
4599 isl_basic_map_free(bmap);
4603 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4604 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4608 bmap = isl_basic_map_cow(bmap);
4609 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4610 j = isl_basic_map_alloc_equality(bmap);
4613 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4614 isl_int_set_si(bmap->eq[j][pos], -1);
4615 isl_int_set(bmap->eq[j][0], value);
4616 bmap = isl_basic_map_simplify(bmap);
4617 return isl_basic_map_finalize(bmap);
4619 isl_basic_map_free(bmap);
4623 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4624 enum isl_dim_type type, unsigned pos, int value)
4628 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4629 return isl_basic_map_fix_pos_si(bmap,
4630 isl_basic_map_offset(bmap, type) + pos, value);
4632 isl_basic_map_free(bmap);
4636 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4637 enum isl_dim_type type, unsigned pos, isl_int value)
4641 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4642 return isl_basic_map_fix_pos(bmap,
4643 isl_basic_map_offset(bmap, type) + pos, value);
4645 isl_basic_map_free(bmap);
4649 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4650 enum isl_dim_type type, unsigned pos, int value)
4652 return (struct isl_basic_set *)
4653 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4657 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4658 enum isl_dim_type type, unsigned pos, isl_int value)
4660 return (struct isl_basic_set *)
4661 isl_basic_map_fix((struct isl_basic_map *)bset,
4665 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4666 unsigned input, int value)
4668 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4671 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4672 unsigned dim, int value)
4674 return (struct isl_basic_set *)
4675 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4676 isl_dim_set, dim, value);
4679 struct isl_map *isl_map_fix_si(struct isl_map *map,
4680 enum isl_dim_type type, unsigned pos, int value)
4684 map = isl_map_cow(map);
4688 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4689 for (i = 0; i < map->n; ++i) {
4690 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4694 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4701 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4702 enum isl_dim_type type, unsigned pos, int value)
4704 return (struct isl_set *)
4705 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4708 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4709 enum isl_dim_type type, unsigned pos, isl_int value)
4713 map = isl_map_cow(map);
4717 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4718 for (i = 0; i < map->n; ++i) {
4719 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4723 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4730 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4731 enum isl_dim_type type, unsigned pos, isl_int value)
4733 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4736 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4737 unsigned input, int value)
4739 return isl_map_fix_si(map, isl_dim_in, input, value);
4742 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4744 return (struct isl_set *)
4745 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4748 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4749 __isl_take isl_basic_map *bmap,
4750 enum isl_dim_type type, unsigned pos, int value)
4756 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4757 pos += isl_basic_map_offset(bmap, type);
4758 bmap = isl_basic_map_cow(bmap);
4759 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4760 j = isl_basic_map_alloc_inequality(bmap);
4763 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4764 isl_int_set_si(bmap->ineq[j][pos], 1);
4765 isl_int_set_si(bmap->ineq[j][0], -value);
4766 bmap = isl_basic_map_simplify(bmap);
4767 return isl_basic_map_finalize(bmap);
4769 isl_basic_map_free(bmap);
4773 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4774 unsigned dim, isl_int value)
4778 bset = isl_basic_set_cow(bset);
4779 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4780 j = isl_basic_set_alloc_inequality(bset);
4783 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4784 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4785 isl_int_neg(bset->ineq[j][0], value);
4786 bset = isl_basic_set_simplify(bset);
4787 return isl_basic_set_finalize(bset);
4789 isl_basic_set_free(bset);
4793 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4794 enum isl_dim_type type, unsigned pos, int value)
4798 map = isl_map_cow(map);
4802 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4803 for (i = 0; i < map->n; ++i) {
4804 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4809 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4816 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4817 enum isl_dim_type type, unsigned pos, int value)
4819 return (struct isl_set *)
4820 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4823 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4828 set = isl_set_cow(set);
4832 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4833 for (i = 0; i < set->n; ++i) {
4834 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4844 struct isl_map *isl_map_reverse(struct isl_map *map)
4848 map = isl_map_cow(map);
4852 map->dim = isl_dim_reverse(map->dim);
4855 for (i = 0; i < map->n; ++i) {
4856 map->p[i] = isl_basic_map_reverse(map->p[i]);
4860 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4867 static struct isl_map *isl_basic_map_partial_lexopt(
4868 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4869 struct isl_set **empty, int max)
4873 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4874 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4876 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4878 isl_basic_map_free(bmap);
4879 isl_basic_set_free(dom);
4885 struct isl_map *isl_basic_map_partial_lexmax(
4886 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4887 struct isl_set **empty)
4889 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4892 struct isl_map *isl_basic_map_partial_lexmin(
4893 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4894 struct isl_set **empty)
4896 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4899 struct isl_set *isl_basic_set_partial_lexmin(
4900 struct isl_basic_set *bset, struct isl_basic_set *dom,
4901 struct isl_set **empty)
4903 return (struct isl_set *)
4904 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4908 struct isl_set *isl_basic_set_partial_lexmax(
4909 struct isl_basic_set *bset, struct isl_basic_set *dom,
4910 struct isl_set **empty)
4912 return (struct isl_set *)
4913 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4917 /* Given a basic map "bmap", compute the lexicographically minimal
4918 * (or maximal) image element for each domain element in dom.
4919 * Set *empty to those elements in dom that do not have an image element.
4921 * We first make sure the basic sets in dom are disjoint and then
4922 * simply collect the results over each of the basic sets separately.
4923 * We could probably improve the efficiency a bit by moving the union
4924 * domain down into the parametric integer programming.
4926 static __isl_give isl_map *basic_map_partial_lexopt(
4927 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4928 __isl_give isl_set **empty, int max)
4931 struct isl_map *res;
4933 dom = isl_set_make_disjoint(dom);
4937 if (isl_set_plain_is_empty(dom)) {
4938 res = isl_map_empty_like_basic_map(bmap);
4939 *empty = isl_set_empty_like(dom);
4941 isl_basic_map_free(bmap);
4945 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4946 isl_basic_set_copy(dom->p[0]), empty, max);
4948 for (i = 1; i < dom->n; ++i) {
4949 struct isl_map *res_i;
4950 struct isl_set *empty_i;
4952 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4953 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4955 res = isl_map_union_disjoint(res, res_i);
4956 *empty = isl_set_union_disjoint(*empty, empty_i);
4960 isl_basic_map_free(bmap);
4965 isl_basic_map_free(bmap);
4969 /* Given a map "map", compute the lexicographically minimal
4970 * (or maximal) image element for each domain element in dom.
4971 * Set *empty to those elements in dom that do not have an image element.
4973 * We first compute the lexicographically minimal or maximal element
4974 * in the first basic map. This results in a partial solution "res"
4975 * and a subset "todo" of dom that still need to be handled.
4976 * We then consider each of the remaining maps in "map" and successively
4977 * improve both "res" and "todo".
4979 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4980 * Assume we are computing the lexicographical maximum.
4981 * We first compute the lexicographically maximal element in basic map i.
4982 * This results in a partial solution res_i and a subset todo_i.
4983 * Then we combine these results with those obtain for the first k basic maps
4984 * to obtain a result that is valid for the first k+1 basic maps.
4985 * In particular, the set where there is no solution is the set where
4986 * there is no solution for the first k basic maps and also no solution
4987 * for the ith basic map, i.e.,
4989 * todo^i = todo^k * todo_i
4991 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4992 * solutions, arbitrarily breaking ties in favor of res^k.
4993 * That is, when res^k(a) >= res_i(a), we pick res^k and
4994 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4995 * the lexicographic order.)
4996 * In practice, we compute
4998 * res^k * (res_i . "<=")
5002 * res_i * (res^k . "<")
5004 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5005 * where only one of res^k and res_i provides a solution and we simply pick
5012 * Note that we only compute these intersections when dom(res^k) intersects
5013 * dom(res_i). Otherwise, the only effect of these intersections is to
5014 * potentially break up res^k and res_i into smaller pieces.
5015 * We want to avoid such splintering as much as possible.
5016 * In fact, an earlier implementation of this function would look for
5017 * better results in the domain of res^k and for extra results in todo^k,
5018 * but this would always result in a splintering according to todo^k,
5019 * even when the domain of basic map i is disjoint from the domains of
5020 * the previous basic maps.
5022 static __isl_give isl_map *isl_map_partial_lexopt(
5023 __isl_take isl_map *map, __isl_take isl_set *dom,
5024 __isl_give isl_set **empty, int max)
5027 struct isl_map *res;
5028 struct isl_set *todo;
5033 if (isl_map_plain_is_empty(map)) {
5041 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5042 isl_set_copy(dom), &todo, max);
5044 for (i = 1; i < map->n; ++i) {
5048 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
5050 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5051 isl_set_copy(dom), &todo_i, max);
5054 lt = isl_map_lex_lt(isl_dim_copy(dim));
5055 le = isl_map_lex_le(dim);
5057 lt = isl_map_lex_gt(isl_dim_copy(dim));
5058 le = isl_map_lex_ge(dim);
5060 lt = isl_map_apply_range(isl_map_copy(res), lt);
5061 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5062 le = isl_map_apply_range(isl_map_copy(res_i), le);
5063 le = isl_map_intersect(le, isl_map_copy(res));
5065 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5066 res = isl_map_intersect_domain(res,
5067 isl_set_copy(todo_i));
5068 res_i = isl_map_intersect_domain(res_i,
5069 isl_set_copy(todo));
5072 res = isl_map_union_disjoint(res, res_i);
5073 res = isl_map_union_disjoint(res, lt);
5074 res = isl_map_union_disjoint(res, le);
5076 todo = isl_set_intersect(todo, todo_i);
5096 __isl_give isl_map *isl_map_partial_lexmax(
5097 __isl_take isl_map *map, __isl_take isl_set *dom,
5098 __isl_give isl_set **empty)
5100 return isl_map_partial_lexopt(map, dom, empty, 1);
5103 __isl_give isl_map *isl_map_partial_lexmin(
5104 __isl_take isl_map *map, __isl_take isl_set *dom,
5105 __isl_give isl_set **empty)
5107 return isl_map_partial_lexopt(map, dom, empty, 0);
5110 __isl_give isl_set *isl_set_partial_lexmin(
5111 __isl_take isl_set *set, __isl_take isl_set *dom,
5112 __isl_give isl_set **empty)
5114 return (struct isl_set *)
5115 isl_map_partial_lexmin((struct isl_map *)set,
5119 __isl_give isl_set *isl_set_partial_lexmax(
5120 __isl_take isl_set *set, __isl_take isl_set *dom,
5121 __isl_give isl_set **empty)
5123 return (struct isl_set *)
5124 isl_map_partial_lexmax((struct isl_map *)set,
5128 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5130 struct isl_basic_set *dom = NULL;
5131 struct isl_dim *dom_dim;
5135 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
5136 dom = isl_basic_set_universe(dom_dim);
5137 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5139 isl_basic_map_free(bmap);
5143 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5145 return isl_basic_map_lexopt(bmap, 0);
5148 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5150 return isl_basic_map_lexopt(bmap, 1);
5153 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5155 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5158 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5160 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5163 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5165 struct isl_set *dom = NULL;
5166 struct isl_dim *dom_dim;
5170 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
5171 dom = isl_set_universe(dom_dim);
5172 return isl_map_partial_lexopt(map, dom, NULL, max);
5178 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5180 return isl_map_lexopt(map, 0);
5183 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5185 return isl_map_lexopt(map, 1);
5188 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5190 return (isl_set *)isl_map_lexmin((isl_map *)set);
5193 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5195 return (isl_set *)isl_map_lexmax((isl_map *)set);
5198 /* Construct a map that equates the two given dimensions in the given space.
5200 static __isl_give isl_map *equate(__isl_take isl_dim *dim,
5201 enum isl_dim_type src_type, int src_pos,
5202 enum isl_dim_type dst_type, int dst_pos)
5204 isl_basic_map *bmap;
5207 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
5208 k = isl_basic_map_alloc_equality(bmap);
5211 isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
5212 src_pos += isl_basic_map_offset(bmap, src_type);
5213 dst_pos += isl_basic_map_offset(bmap, dst_type);
5214 isl_int_set_si(bmap->eq[k][src_pos], 1);
5215 isl_int_set_si(bmap->eq[k][dst_pos], -1);
5217 return isl_map_from_basic_map(bmap);
5219 isl_basic_map_free(bmap);
5223 /* Extract the first and only affine expression from list
5224 * and then add it to *pwaff with the given dom.
5225 * This domain is known to be disjoint from other domains
5226 * because of the way isl_basic_set_foreach_lexmax works.
5228 static int update_dim_max(__isl_take isl_basic_set *dom,
5229 __isl_take isl_aff_list *list, void *user)
5231 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5233 isl_pw_aff **pwaff = user;
5234 isl_pw_aff *pwaff_i;
5236 if (isl_aff_list_n_aff(list) != 1)
5237 isl_die(ctx, isl_error_internal,
5238 "expecting single element list", goto error);
5240 aff = isl_aff_list_get_aff(list, 0);
5241 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5243 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5245 isl_aff_list_free(list);
5249 isl_basic_set_free(dom);
5250 isl_aff_list_free(list);
5254 /* Given a one-dimensional basic set, compute the maximum of that
5255 * dimension as an isl_pw_aff.
5257 * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexmax
5258 * call update_dim_max on each leaf of the result.
5260 static __isl_give isl_pw_aff *basic_set_dim_max(__isl_keep isl_basic_set *bset)
5262 isl_dim *dim = isl_basic_set_get_dim(bset);
5266 dim = isl_dim_domain(isl_dim_from_range(dim));
5267 pwaff = isl_pw_aff_empty(dim);
5269 r = isl_basic_set_foreach_lexmax(bset, &update_dim_max, &pwaff);
5271 return isl_pw_aff_free(pwaff);
5276 /* Compute the maximum of the given set dimension as a function of the
5277 * parameters, but independently of the other set dimensions.
5279 * We first project the set onto the given dimension and then compute
5280 * the "lexicographic" maximum in each basic set, combining the results
5281 * using isl_pw_aff_union_max.
5283 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5289 map = isl_map_from_domain(set);
5290 map = isl_map_add_dims(map, isl_dim_out, 1);
5291 map = isl_map_intersect(map,
5292 equate(isl_map_get_dim(map), isl_dim_in, pos,
5294 set = isl_map_range(map);
5299 isl_dim *dim = isl_set_get_dim(set);
5300 dim = isl_dim_domain(isl_dim_from_range(dim));
5302 return isl_pw_aff_empty(dim);
5305 pwaff = basic_set_dim_max(set->p[0]);
5306 for (i = 1; i < set->n; ++i) {
5307 isl_pw_aff *pwaff_i;
5309 pwaff_i = basic_set_dim_max(set->p[i]);
5310 pwaff = isl_pw_aff_union_max(pwaff, pwaff_i);
5318 /* Apply a preimage specified by "mat" on the parameters of "bset".
5319 * bset is assumed to have only parameters and divs.
5321 static struct isl_basic_set *basic_set_parameter_preimage(
5322 struct isl_basic_set *bset, struct isl_mat *mat)
5329 bset->dim = isl_dim_cow(bset->dim);
5333 nparam = isl_basic_set_dim(bset, isl_dim_param);
5335 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5337 bset->dim->nparam = 0;
5338 bset->dim->n_out = nparam;
5339 bset = isl_basic_set_preimage(bset, mat);
5341 bset->dim->nparam = bset->dim->n_out;
5342 bset->dim->n_out = 0;
5347 isl_basic_set_free(bset);
5351 /* Apply a preimage specified by "mat" on the parameters of "set".
5352 * set is assumed to have only parameters and divs.
5354 static struct isl_set *set_parameter_preimage(
5355 struct isl_set *set, struct isl_mat *mat)
5357 struct isl_dim *dim = NULL;
5363 dim = isl_dim_copy(set->dim);
5364 dim = isl_dim_cow(dim);
5368 nparam = isl_set_dim(set, isl_dim_param);
5370 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5373 dim->n_out = nparam;
5374 isl_set_reset_dim(set, dim);
5375 set = isl_set_preimage(set, mat);
5378 dim = isl_dim_copy(set->dim);
5379 dim = isl_dim_cow(dim);
5382 dim->nparam = dim->n_out;
5384 isl_set_reset_dim(set, dim);
5394 /* Intersect the basic set "bset" with the affine space specified by the
5395 * equalities in "eq".
5397 static struct isl_basic_set *basic_set_append_equalities(
5398 struct isl_basic_set *bset, struct isl_mat *eq)
5406 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5411 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5412 for (i = 0; i < eq->n_row; ++i) {
5413 k = isl_basic_set_alloc_equality(bset);
5416 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5417 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5421 bset = isl_basic_set_gauss(bset, NULL);
5422 bset = isl_basic_set_finalize(bset);
5427 isl_basic_set_free(bset);
5431 /* Intersect the set "set" with the affine space specified by the
5432 * equalities in "eq".
5434 static struct isl_set *set_append_equalities(struct isl_set *set,
5442 for (i = 0; i < set->n; ++i) {
5443 set->p[i] = basic_set_append_equalities(set->p[i],
5456 /* Project the given basic set onto its parameter domain, possibly introducing
5457 * new, explicit, existential variables in the constraints.
5458 * The input has parameters and (possibly implicit) existential variables.
5459 * The output has the same parameters, but only
5460 * explicit existentially quantified variables.
5462 * The actual projection is performed by pip, but pip doesn't seem
5463 * to like equalities very much, so we first remove the equalities
5464 * among the parameters by performing a variable compression on
5465 * the parameters. Afterward, an inverse transformation is performed
5466 * and the equalities among the parameters are inserted back in.
5468 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5472 struct isl_mat *T, *T2;
5473 struct isl_set *set;
5474 unsigned nparam, n_div;
5476 bset = isl_basic_set_cow(bset);
5480 if (bset->n_eq == 0)
5481 return isl_basic_set_lexmin(bset);
5483 isl_basic_set_gauss(bset, NULL);
5485 nparam = isl_basic_set_dim(bset, isl_dim_param);
5486 n_div = isl_basic_set_dim(bset, isl_dim_div);
5488 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5489 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5492 if (i == bset->n_eq)
5493 return isl_basic_set_lexmin(bset);
5495 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5497 eq = isl_mat_cow(eq);
5498 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5499 if (T && T->n_col == 0) {
5503 bset = isl_basic_set_set_to_empty(bset);
5504 return isl_set_from_basic_set(bset);
5506 bset = basic_set_parameter_preimage(bset, T);
5508 set = isl_basic_set_lexmin(bset);
5509 set = set_parameter_preimage(set, T2);
5510 set = set_append_equalities(set, eq);
5514 /* Compute an explicit representation for all the existentially
5515 * quantified variables.
5516 * The input and output dimensions are first turned into parameters.
5517 * compute_divs then returns a map with the same parameters and
5518 * no input or output dimensions and the dimension specification
5519 * is reset to that of the input.
5521 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5523 struct isl_basic_set *bset;
5524 struct isl_set *set;
5525 struct isl_map *map;
5526 struct isl_dim *dim, *orig_dim = NULL;
5531 bmap = isl_basic_map_cow(bmap);
5535 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5536 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5537 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5538 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5542 orig_dim = bmap->dim;
5544 bset = (struct isl_basic_set *)bmap;
5546 set = parameter_compute_divs(bset);
5547 map = (struct isl_map *)set;
5548 map = isl_map_reset_dim(map, orig_dim);
5552 isl_basic_map_free(bmap);
5556 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5564 off = isl_dim_total(bmap->dim);
5565 for (i = 0; i < bmap->n_div; ++i) {
5566 if (isl_int_is_zero(bmap->div[i][0]))
5568 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5574 static int map_divs_known(__isl_keep isl_map *map)
5581 for (i = 0; i < map->n; ++i) {
5582 int known = isl_basic_map_divs_known(map->p[i]);
5590 /* If bmap contains any unknown divs, then compute explicit
5591 * expressions for them. However, this computation may be
5592 * quite expensive, so first try to remove divs that aren't
5595 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5598 struct isl_map *map;
5600 known = isl_basic_map_divs_known(bmap);
5604 return isl_map_from_basic_map(bmap);
5606 bmap = isl_basic_map_drop_redundant_divs(bmap);
5608 known = isl_basic_map_divs_known(bmap);
5612 return isl_map_from_basic_map(bmap);
5614 map = compute_divs(bmap);
5617 isl_basic_map_free(bmap);
5621 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5625 struct isl_map *res;
5632 known = map_divs_known(map);
5640 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5641 for (i = 1 ; i < map->n; ++i) {
5643 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5644 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5645 res = isl_map_union_disjoint(res, r2);
5647 res = isl_map_union(res, r2);
5654 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5656 return (struct isl_set *)
5657 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5660 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5662 return (struct isl_set *)
5663 isl_map_compute_divs((struct isl_map *)set);
5666 struct isl_set *isl_map_domain(struct isl_map *map)
5669 struct isl_set *set;
5674 map = isl_map_cow(map);
5678 set = (struct isl_set *)map;
5679 set->dim = isl_dim_domain(set->dim);
5682 for (i = 0; i < map->n; ++i) {
5683 set->p[i] = isl_basic_map_domain(map->p[i]);
5687 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5688 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5695 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
5696 __isl_take isl_map *map2)
5700 struct isl_map *map = NULL;
5714 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5716 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5717 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5718 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5720 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5721 map1->n + map2->n, flags);
5724 for (i = 0; i < map1->n; ++i) {
5725 map = isl_map_add_basic_map(map,
5726 isl_basic_map_copy(map1->p[i]));
5730 for (i = 0; i < map2->n; ++i) {
5731 map = isl_map_add_basic_map(map,
5732 isl_basic_map_copy(map2->p[i]));
5746 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
5747 __isl_take isl_map *map2)
5749 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
5752 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5754 map1 = isl_map_union_disjoint(map1, map2);
5758 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5762 struct isl_set *isl_set_union_disjoint(
5763 struct isl_set *set1, struct isl_set *set2)
5765 return (struct isl_set *)
5766 isl_map_union_disjoint(
5767 (struct isl_map *)set1, (struct isl_map *)set2);
5770 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5772 return (struct isl_set *)
5773 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5776 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
5777 __isl_take isl_set *set)
5780 struct isl_map *result;
5786 if (!isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5787 isl_die(set->ctx, isl_error_invalid,
5788 "parameters don't match", goto error);
5790 if (isl_dim_size(set->dim, isl_dim_set) != 0 &&
5791 !isl_map_compatible_range(map, set))
5792 isl_die(set->ctx, isl_error_invalid,
5793 "incompatible spaces", goto error);
5795 if (isl_set_plain_is_universe(set)) {
5800 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5801 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5802 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5804 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5805 map->n * set->n, flags);
5808 for (i = 0; i < map->n; ++i)
5809 for (j = 0; j < set->n; ++j) {
5810 result = isl_map_add_basic_map(result,
5811 isl_basic_map_intersect_range(
5812 isl_basic_map_copy(map->p[i]),
5813 isl_basic_set_copy(set->p[j])));
5826 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
5827 __isl_take isl_set *set)
5829 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
5832 struct isl_map *isl_map_intersect_domain(
5833 struct isl_map *map, struct isl_set *set)
5835 return isl_map_reverse(
5836 isl_map_intersect_range(isl_map_reverse(map), set));
5839 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
5840 __isl_take isl_map *map2)
5844 map1 = isl_map_reverse(map1);
5845 map1 = isl_map_apply_range(map1, map2);
5846 return isl_map_reverse(map1);
5853 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
5854 __isl_take isl_map *map2)
5856 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
5859 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
5860 __isl_take isl_map *map2)
5862 struct isl_dim *dim_result;
5863 struct isl_map *result;
5869 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5870 isl_dim_copy(map2->dim));
5872 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5875 for (i = 0; i < map1->n; ++i)
5876 for (j = 0; j < map2->n; ++j) {
5877 result = isl_map_add_basic_map(result,
5878 isl_basic_map_apply_range(
5879 isl_basic_map_copy(map1->p[i]),
5880 isl_basic_map_copy(map2->p[j])));
5886 if (result && result->n <= 1)
5887 ISL_F_SET(result, ISL_MAP_DISJOINT);
5895 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
5896 __isl_take isl_map *map2)
5898 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
5902 * returns range - domain
5904 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5906 isl_dim *dims, *target_dim;
5907 struct isl_basic_set *bset;
5914 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5915 bmap->dim, isl_dim_out),
5917 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5918 dim = isl_basic_map_n_in(bmap);
5919 nparam = isl_basic_map_n_param(bmap);
5920 bset = isl_basic_set_from_basic_map(bmap);
5921 bset = isl_basic_set_cow(bset);
5922 dims = isl_basic_set_get_dim(bset);
5923 dims = isl_dim_add(dims, isl_dim_set, dim);
5924 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5925 bset = isl_basic_set_swap_vars(bset, 2*dim);
5926 for (i = 0; i < dim; ++i) {
5927 int j = isl_basic_map_alloc_equality(
5928 (struct isl_basic_map *)bset);
5931 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5932 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5933 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5934 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5936 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5937 bset = isl_basic_set_reset_dim(bset, target_dim);
5940 isl_basic_map_free(bmap);
5945 * returns range - domain
5947 struct isl_set *isl_map_deltas(struct isl_map *map)
5951 struct isl_set *result;
5956 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5957 map->dim, isl_dim_out),
5959 dim = isl_map_get_dim(map);
5960 dim = isl_dim_domain(dim);
5961 result = isl_set_alloc_dim(dim, map->n, 0);
5964 for (i = 0; i < map->n; ++i)
5965 result = isl_set_add_basic_set(result,
5966 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5975 * returns [domain -> range] -> range - domain
5977 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5978 __isl_take isl_basic_map *bmap)
5982 isl_basic_map *domain;
5986 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5987 isl_die(bmap->ctx, isl_error_invalid,
5988 "domain and range don't match", goto error);
5990 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5991 n = isl_basic_map_dim(bmap, isl_dim_in);
5993 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5994 domain = isl_basic_map_universe(dim);
5996 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5997 bmap = isl_basic_map_apply_range(bmap, domain);
5998 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6000 total = isl_basic_map_total_dim(bmap);
6002 for (i = 0; i < n; ++i) {
6003 k = isl_basic_map_alloc_equality(bmap);
6006 isl_seq_clr(bmap->eq[k], 1 + total);
6007 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6008 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6009 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6012 bmap = isl_basic_map_gauss(bmap, NULL);
6013 return isl_basic_map_finalize(bmap);
6015 isl_basic_map_free(bmap);
6020 * returns [domain -> range] -> range - domain
6022 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6025 isl_dim *domain_dim;
6030 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6031 isl_die(map->ctx, isl_error_invalid,
6032 "domain and range don't match", goto error);
6034 map = isl_map_cow(map);
6038 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
6039 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
6040 map->dim = isl_dim_join(map->dim, domain_dim);
6043 for (i = 0; i < map->n; ++i) {
6044 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6048 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6055 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
6057 struct isl_basic_map *bmap;
6065 nparam = dims->nparam;
6067 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
6071 for (i = 0; i < dim; ++i) {
6072 int j = isl_basic_map_alloc_equality(bmap);
6075 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6076 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6077 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6079 return isl_basic_map_finalize(bmap);
6081 isl_basic_map_free(bmap);
6085 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
6089 if (dim->n_in != dim->n_out)
6090 isl_die(dim->ctx, isl_error_invalid,
6091 "number of input and output dimensions needs to be "
6092 "the same", goto error);
6093 return basic_map_identity(dim);
6099 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6101 if (!model || !model->dim)
6103 return isl_basic_map_identity(isl_dim_copy(model->dim));
6106 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
6108 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6111 struct isl_map *isl_map_identity_like(struct isl_map *model)
6113 if (!model || !model->dim)
6115 return isl_map_identity(isl_dim_copy(model->dim));
6118 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6120 if (!model || !model->dim)
6122 return isl_map_identity(isl_dim_copy(model->dim));
6125 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6127 isl_dim *dim = isl_set_get_dim(set);
6129 id = isl_map_identity(isl_dim_map_from_set(dim));
6130 return isl_map_intersect_range(id, set);
6133 /* Construct a basic set with all set dimensions having only non-negative
6136 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
6141 struct isl_basic_set *bset;
6145 nparam = dims->nparam;
6147 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
6150 for (i = 0; i < dim; ++i) {
6151 int k = isl_basic_set_alloc_inequality(bset);
6154 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6155 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6159 isl_basic_set_free(bset);
6163 /* Construct the half-space x_pos >= 0.
6165 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
6169 isl_basic_set *nonneg;
6171 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
6172 k = isl_basic_set_alloc_inequality(nonneg);
6175 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6176 isl_int_set_si(nonneg->ineq[k][pos], 1);
6178 return isl_basic_set_finalize(nonneg);
6180 isl_basic_set_free(nonneg);
6184 /* Construct the half-space x_pos <= -1.
6186 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
6191 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
6192 k = isl_basic_set_alloc_inequality(neg);
6195 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6196 isl_int_set_si(neg->ineq[k][0], -1);
6197 isl_int_set_si(neg->ineq[k][pos], -1);
6199 return isl_basic_set_finalize(neg);
6201 isl_basic_set_free(neg);
6205 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6206 enum isl_dim_type type, unsigned first, unsigned n)
6209 isl_basic_set *nonneg;
6217 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6219 for (i = 0; i < n; ++i) {
6220 nonneg = nonneg_halfspace(isl_set_get_dim(set),
6221 pos(set->dim, type) + first + i);
6222 neg = neg_halfspace(isl_set_get_dim(set),
6223 pos(set->dim, type) + first + i);
6225 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6234 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6235 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6242 if (isl_set_plain_is_empty(set)) {
6247 return fn(set, signs, user);
6250 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
6252 half = isl_set_intersect(half, isl_set_copy(set));
6253 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6257 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
6259 half = isl_set_intersect(half, set);
6260 return foreach_orthant(half, signs, first + 1, len, fn, user);
6266 /* Call "fn" on the intersections of "set" with each of the orthants
6267 * (except for obviously empty intersections). The orthant is identified
6268 * by the signs array, with each entry having value 1 or -1 according
6269 * to the sign of the corresponding variable.
6271 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6272 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6282 if (isl_set_plain_is_empty(set))
6285 nparam = isl_set_dim(set, isl_dim_param);
6286 nvar = isl_set_dim(set, isl_dim_set);
6288 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6290 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6298 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6300 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6303 int isl_basic_map_is_subset(
6304 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6307 struct isl_map *map1;
6308 struct isl_map *map2;
6310 if (!bmap1 || !bmap2)
6313 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6314 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6316 is_subset = isl_map_is_subset(map1, map2);
6324 int isl_basic_map_is_equal(
6325 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6329 if (!bmap1 || !bmap2)
6331 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6334 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6338 int isl_basic_set_is_equal(
6339 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6341 return isl_basic_map_is_equal(
6342 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6345 int isl_map_is_empty(struct isl_map *map)
6352 for (i = 0; i < map->n; ++i) {
6353 is_empty = isl_basic_map_is_empty(map->p[i]);
6362 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6364 return map ? map->n == 0 : -1;
6367 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6369 return isl_map_plain_is_empty(map);
6372 int isl_set_plain_is_empty(struct isl_set *set)
6374 return set ? set->n == 0 : -1;
6377 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6379 return isl_set_plain_is_empty(set);
6382 int isl_set_is_empty(struct isl_set *set)
6384 return isl_map_is_empty((struct isl_map *)set);
6387 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6392 return isl_dim_equal(map1->dim, map2->dim);
6395 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6400 return isl_dim_equal(set1->dim, set2->dim);
6403 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6409 is_subset = isl_map_is_subset(map1, map2);
6412 is_subset = isl_map_is_subset(map2, map1);
6416 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6418 return align_params_map_map_and_test(map1, map2, &map_is_equal);
6421 int isl_basic_map_is_strict_subset(
6422 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6426 if (!bmap1 || !bmap2)
6428 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6431 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6432 if (is_subset == -1)
6437 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6443 is_subset = isl_map_is_subset(map1, map2);
6446 is_subset = isl_map_is_subset(map2, map1);
6447 if (is_subset == -1)
6452 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6454 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6457 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6461 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6464 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6468 return bset->n_eq == 0 && bset->n_ineq == 0;
6471 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6478 for (i = 0; i < map->n; ++i) {
6479 int r = isl_basic_map_is_universe(map->p[i]);
6487 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6489 return isl_map_plain_is_universe((isl_map *) set);
6492 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6494 return isl_set_plain_is_universe(set);
6497 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6499 struct isl_basic_set *bset = NULL;
6500 struct isl_vec *sample = NULL;
6507 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6510 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6511 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6512 copy = isl_basic_map_remove_redundancies(copy);
6513 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6514 isl_basic_map_free(copy);
6518 total = 1 + isl_basic_map_total_dim(bmap);
6519 if (bmap->sample && bmap->sample->size == total) {
6520 int contains = isl_basic_map_contains(bmap, bmap->sample);
6526 isl_vec_free(bmap->sample);
6527 bmap->sample = NULL;
6528 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6531 sample = isl_basic_set_sample_vec(bset);
6534 empty = sample->size == 0;
6535 isl_vec_free(bmap->sample);
6536 bmap->sample = sample;
6538 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6543 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6547 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6550 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6552 return isl_basic_map_plain_is_empty(bmap);
6555 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6559 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6562 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6564 return isl_basic_set_plain_is_empty(bset);
6567 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6569 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6572 struct isl_map *isl_basic_map_union(
6573 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6575 struct isl_map *map;
6576 if (!bmap1 || !bmap2)
6579 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6581 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6584 map = isl_map_add_basic_map(map, bmap1);
6585 map = isl_map_add_basic_map(map, bmap2);
6588 isl_basic_map_free(bmap1);
6589 isl_basic_map_free(bmap2);
6593 struct isl_set *isl_basic_set_union(
6594 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6596 return (struct isl_set *)isl_basic_map_union(
6597 (struct isl_basic_map *)bset1,
6598 (struct isl_basic_map *)bset2);
6601 /* Order divs such that any div only depends on previous divs */
6602 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6610 off = isl_dim_total(bmap->dim);
6612 for (i = 0; i < bmap->n_div; ++i) {
6614 if (isl_int_is_zero(bmap->div[i][0]))
6616 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6620 isl_basic_map_swap_div(bmap, i, i + pos);
6626 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6628 return (struct isl_basic_set *)
6629 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6632 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6639 for (i = 0; i < map->n; ++i) {
6640 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6651 /* Apply the expansion computed by isl_merge_divs.
6652 * The expansion itself is given by "exp" while the resulting
6653 * list of divs is given by "div".
6655 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6656 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6661 bset = isl_basic_set_cow(bset);
6665 if (div->n_row < bset->n_div)
6666 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6667 "not an expansion", goto error);
6669 bset = isl_basic_map_extend_dim(bset, isl_dim_copy(bset->dim),
6670 div->n_row - bset->n_div, 0,
6671 2 * (div->n_row - bset->n_div));
6673 n_div = bset->n_div;
6674 for (i = n_div; i < div->n_row; ++i)
6675 if (isl_basic_set_alloc_div(bset) < 0)
6679 for (i = div->n_row - 1; i >= 0; --i) {
6680 if (j >= 0 && exp[j] == i) {
6682 isl_basic_map_swap_div(bset, i, j);
6685 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6686 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6694 isl_basic_set_free(bset);
6699 /* Look for a div in dst that corresponds to the div "div" in src.
6700 * The divs before "div" in src and dst are assumed to be the same.
6702 * Returns -1 if no corresponding div was found and the position
6703 * of the corresponding div in dst otherwise.
6705 static int find_div(struct isl_basic_map *dst,
6706 struct isl_basic_map *src, unsigned div)
6710 unsigned total = isl_dim_total(src->dim);
6712 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6713 for (i = div; i < dst->n_div; ++i)
6714 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6715 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6716 dst->n_div - div) == -1)
6721 struct isl_basic_map *isl_basic_map_align_divs(
6722 struct isl_basic_map *dst, struct isl_basic_map *src)
6725 unsigned total = isl_dim_total(src->dim);
6730 if (src->n_div == 0)
6733 for (i = 0; i < src->n_div; ++i)
6734 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6736 src = isl_basic_map_order_divs(src);
6737 dst = isl_basic_map_cow(dst);
6738 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6739 src->n_div, 0, 2 * src->n_div);
6742 for (i = 0; i < src->n_div; ++i) {
6743 int j = find_div(dst, src, i);
6745 j = isl_basic_map_alloc_div(dst);
6748 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6749 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6750 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6754 isl_basic_map_swap_div(dst, i, j);
6758 isl_basic_map_free(dst);
6762 struct isl_basic_set *isl_basic_set_align_divs(
6763 struct isl_basic_set *dst, struct isl_basic_set *src)
6765 return (struct isl_basic_set *)isl_basic_map_align_divs(
6766 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6769 struct isl_map *isl_map_align_divs(struct isl_map *map)
6777 map = isl_map_compute_divs(map);
6778 map = isl_map_cow(map);
6782 for (i = 1; i < map->n; ++i)
6783 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6784 for (i = 1; i < map->n; ++i)
6785 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6787 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6791 struct isl_set *isl_set_align_divs(struct isl_set *set)
6793 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6796 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
6797 __isl_take isl_map *map)
6801 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6802 map = isl_map_intersect_domain(map, set);
6803 set = isl_map_range(map);
6811 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
6812 __isl_take isl_map *map)
6814 return isl_map_align_params_map_map_and(set, map, &set_apply);
6817 /* There is no need to cow as removing empty parts doesn't change
6818 * the meaning of the set.
6820 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6827 for (i = map->n-1; i >= 0; --i) {
6828 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6830 isl_basic_map_free(map->p[i]);
6831 if (i != map->n-1) {
6832 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6833 map->p[i] = map->p[map->n-1];
6841 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6843 return (struct isl_set *)
6844 isl_map_remove_empty_parts((struct isl_map *)set);
6847 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6849 struct isl_basic_map *bmap;
6850 if (!map || map->n == 0)
6852 bmap = map->p[map->n-1];
6853 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6854 return isl_basic_map_copy(bmap);
6857 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6859 return (struct isl_basic_set *)
6860 isl_map_copy_basic_map((struct isl_map *)set);
6863 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6864 __isl_keep isl_basic_map *bmap)
6870 for (i = map->n-1; i >= 0; --i) {
6871 if (map->p[i] != bmap)
6873 map = isl_map_cow(map);
6876 isl_basic_map_free(map->p[i]);
6877 if (i != map->n-1) {
6878 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6879 map->p[i] = map->p[map->n-1];
6890 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6891 struct isl_basic_set *bset)
6893 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6894 (struct isl_basic_map *)bset);
6897 /* Given two basic sets bset1 and bset2, compute the maximal difference
6898 * between the values of dimension pos in bset1 and those in bset2
6899 * for any common value of the parameters and dimensions preceding pos.
6901 static enum isl_lp_result basic_set_maximal_difference_at(
6902 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6903 int pos, isl_int *opt)
6905 struct isl_dim *dims;
6906 struct isl_basic_map *bmap1 = NULL;
6907 struct isl_basic_map *bmap2 = NULL;
6908 struct isl_ctx *ctx;
6909 struct isl_vec *obj;
6912 unsigned dim1, dim2;
6913 enum isl_lp_result res;
6915 if (!bset1 || !bset2)
6916 return isl_lp_error;
6918 nparam = isl_basic_set_n_param(bset1);
6919 dim1 = isl_basic_set_n_dim(bset1);
6920 dim2 = isl_basic_set_n_dim(bset2);
6921 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6922 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6923 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6924 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6925 if (!bmap1 || !bmap2)
6927 bmap1 = isl_basic_map_cow(bmap1);
6928 bmap1 = isl_basic_map_extend(bmap1, nparam,
6929 pos, (dim1 - pos) + (dim2 - pos),
6930 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6931 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6934 total = isl_basic_map_total_dim(bmap1);
6936 obj = isl_vec_alloc(ctx, 1 + total);
6937 isl_seq_clr(obj->block.data, 1 + total);
6938 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6939 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6942 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6944 isl_basic_map_free(bmap1);
6948 isl_basic_map_free(bmap1);
6949 isl_basic_map_free(bmap2);
6950 return isl_lp_error;
6953 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6954 * for any common value of the parameters and dimensions preceding pos
6955 * in both basic sets, the values of dimension pos in bset1 are
6956 * smaller or larger than those in bset2.
6959 * 1 if bset1 follows bset2
6960 * -1 if bset1 precedes bset2
6961 * 0 if bset1 and bset2 are incomparable
6962 * -2 if some error occurred.
6964 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6965 struct isl_basic_set *bset2, int pos)
6968 enum isl_lp_result res;
6973 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6975 if (res == isl_lp_empty)
6977 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6978 res == isl_lp_unbounded)
6980 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6989 /* Given two basic sets bset1 and bset2, check whether
6990 * for any common value of the parameters and dimensions preceding pos
6991 * there is a value of dimension pos in bset1 that is larger
6992 * than a value of the same dimension in bset2.
6995 * 1 if there exists such a pair
6996 * 0 if there is no such pair, but there is a pair of equal values
6998 * -2 if some error occurred.
7000 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7001 __isl_keep isl_basic_set *bset2, int pos)
7004 enum isl_lp_result res;
7009 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7011 if (res == isl_lp_empty)
7013 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7014 res == isl_lp_unbounded)
7016 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7018 else if (res == isl_lp_ok)
7027 /* Given two sets set1 and set2, check whether
7028 * for any common value of the parameters and dimensions preceding pos
7029 * there is a value of dimension pos in set1 that is larger
7030 * than a value of the same dimension in set2.
7033 * 1 if there exists such a pair
7034 * 0 if there is no such pair, but there is a pair of equal values
7036 * -2 if some error occurred.
7038 int isl_set_follows_at(__isl_keep isl_set *set1,
7039 __isl_keep isl_set *set2, int pos)
7047 for (i = 0; i < set1->n; ++i)
7048 for (j = 0; j < set2->n; ++j) {
7050 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7051 if (f == 1 || f == -2)
7060 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7061 unsigned pos, isl_int *val)
7069 total = isl_basic_map_total_dim(bmap);
7070 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7071 for (; d+1 > pos; --d)
7072 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7076 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7078 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7080 if (!isl_int_is_one(bmap->eq[i][1+d]))
7083 isl_int_neg(*val, bmap->eq[i][0]);
7089 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7090 unsigned pos, isl_int *val)
7102 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7105 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7106 for (i = 1; fixed == 1 && i < map->n; ++i) {
7107 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7108 if (fixed == 1 && isl_int_ne(tmp, v))
7112 isl_int_set(*val, v);
7118 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7119 unsigned pos, isl_int *val)
7121 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7125 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7128 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7131 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7132 enum isl_dim_type type, unsigned pos, isl_int *val)
7134 if (pos >= isl_basic_map_dim(bmap, type))
7136 return isl_basic_map_plain_has_fixed_var(bmap,
7137 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7140 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7141 enum isl_dim_type type, unsigned pos, isl_int *val)
7143 if (pos >= isl_map_dim(map, type))
7145 return isl_map_plain_has_fixed_var(map,
7146 map_offset(map, type) - 1 + pos, val);
7149 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7150 enum isl_dim_type type, unsigned pos, isl_int *val)
7152 return isl_map_plain_is_fixed(map, type, pos, val);
7155 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7156 * then return this fixed value in *val.
7158 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7159 unsigned dim, isl_int *val)
7161 return isl_basic_set_plain_has_fixed_var(bset,
7162 isl_basic_set_n_param(bset) + dim, val);
7165 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7166 * then return this fixed value in *val.
7168 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7169 unsigned dim, isl_int *val)
7171 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7174 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7175 unsigned dim, isl_int *val)
7177 return isl_set_plain_dim_is_fixed(set, dim, val);
7180 /* Check if input variable in has fixed value and if so and if val is not NULL,
7181 * then return this fixed value in *val.
7183 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7184 unsigned in, isl_int *val)
7186 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7189 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7190 * and if val is not NULL, then return this lower bound in *val.
7192 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7193 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7195 int i, i_eq = -1, i_ineq = -1;
7202 total = isl_basic_set_total_dim(bset);
7203 nparam = isl_basic_set_n_param(bset);
7204 for (i = 0; i < bset->n_eq; ++i) {
7205 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7211 for (i = 0; i < bset->n_ineq; ++i) {
7212 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7214 if (i_eq != -1 || i_ineq != -1)
7218 if (i_eq == -1 && i_ineq == -1)
7220 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7221 /* The coefficient should always be one due to normalization. */
7222 if (!isl_int_is_one(c[1+nparam+dim]))
7224 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7226 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7227 total - nparam - dim - 1) != -1)
7230 isl_int_neg(*val, c[0]);
7234 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7235 unsigned dim, isl_int *val)
7247 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7251 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7253 for (i = 1; fixed == 1 && i < set->n; ++i) {
7254 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7256 if (fixed == 1 && isl_int_ne(tmp, v))
7260 isl_int_set(*val, v);
7271 /* uset_gist depends on constraints without existentially quantified
7272 * variables sorting first.
7274 static int qsort_constraint_cmp(const void *p1, const void *p2)
7276 const struct constraint *c1 = (const struct constraint *)p1;
7277 const struct constraint *c2 = (const struct constraint *)p2;
7279 unsigned size = isl_min(c1->size, c2->size);
7281 l1 = isl_seq_last_non_zero(c1->c, size);
7282 l2 = isl_seq_last_non_zero(c2->c, size);
7287 return isl_seq_cmp(c1->c, c2->c, size);
7290 static struct isl_basic_map *isl_basic_map_sort_constraints(
7291 struct isl_basic_map *bmap)
7294 struct constraint *c;
7299 total = isl_basic_map_total_dim(bmap);
7300 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7303 for (i = 0; i < bmap->n_ineq; ++i) {
7305 c[i].c = bmap->ineq[i];
7307 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7308 for (i = 0; i < bmap->n_ineq; ++i)
7309 bmap->ineq[i] = c[i].c;
7313 isl_basic_map_free(bmap);
7317 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7318 __isl_take isl_basic_set *bset)
7320 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7321 (struct isl_basic_map *)bset);
7324 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7328 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7330 bmap = isl_basic_map_remove_redundancies(bmap);
7331 bmap = isl_basic_map_sort_constraints(bmap);
7332 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7336 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7338 return (struct isl_basic_set *)isl_basic_map_normalize(
7339 (struct isl_basic_map *)bset);
7342 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7343 const __isl_keep isl_basic_map *bmap2)
7350 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7351 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7352 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7353 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7354 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7355 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7356 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7357 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7358 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7359 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7360 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7362 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7364 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7366 if (bmap1->n_eq != bmap2->n_eq)
7367 return bmap1->n_eq - bmap2->n_eq;
7368 if (bmap1->n_ineq != bmap2->n_ineq)
7369 return bmap1->n_ineq - bmap2->n_ineq;
7370 if (bmap1->n_div != bmap2->n_div)
7371 return bmap1->n_div - bmap2->n_div;
7372 total = isl_basic_map_total_dim(bmap1);
7373 for (i = 0; i < bmap1->n_eq; ++i) {
7374 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7378 for (i = 0; i < bmap1->n_ineq; ++i) {
7379 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7383 for (i = 0; i < bmap1->n_div; ++i) {
7384 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7391 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7392 __isl_keep isl_basic_map *bmap2)
7394 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7397 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7398 __isl_keep isl_basic_set *bset2)
7400 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7401 (isl_basic_map *)bset2);
7404 static int qsort_bmap_cmp(const void *p1, const void *p2)
7406 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7407 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7409 return isl_basic_map_plain_cmp(bmap1, bmap2);
7412 /* We normalize in place, but if anything goes wrong we need
7413 * to return NULL, so we need to make sure we don't change the
7414 * meaning of any possible other copies of map.
7416 struct isl_map *isl_map_normalize(struct isl_map *map)
7419 struct isl_basic_map *bmap;
7423 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7425 for (i = 0; i < map->n; ++i) {
7426 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7429 isl_basic_map_free(map->p[i]);
7432 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7433 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7434 map = isl_map_remove_empty_parts(map);
7437 for (i = map->n - 1; i >= 1; --i) {
7438 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7440 isl_basic_map_free(map->p[i-1]);
7441 for (j = i; j < map->n; ++j)
7442 map->p[j-1] = map->p[j];
7452 struct isl_set *isl_set_normalize(struct isl_set *set)
7454 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7457 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7467 if (!isl_dim_equal(map1->dim, map2->dim))
7470 map1 = isl_map_copy(map1);
7471 map2 = isl_map_copy(map2);
7472 map1 = isl_map_normalize(map1);
7473 map2 = isl_map_normalize(map2);
7476 equal = map1->n == map2->n;
7477 for (i = 0; equal && i < map1->n; ++i) {
7478 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7491 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7493 return isl_map_plain_is_equal(map1, map2);
7496 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7498 return isl_map_plain_is_equal((struct isl_map *)set1,
7499 (struct isl_map *)set2);
7502 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7504 return isl_set_plain_is_equal(set1, set2);
7507 /* Return an interval that ranges from min to max (inclusive)
7509 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7510 isl_int min, isl_int max)
7513 struct isl_basic_set *bset = NULL;
7515 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7519 k = isl_basic_set_alloc_inequality(bset);
7522 isl_int_set_si(bset->ineq[k][1], 1);
7523 isl_int_neg(bset->ineq[k][0], min);
7525 k = isl_basic_set_alloc_inequality(bset);
7528 isl_int_set_si(bset->ineq[k][1], -1);
7529 isl_int_set(bset->ineq[k][0], max);
7533 isl_basic_set_free(bset);
7537 /* Return the Cartesian product of the basic sets in list (in the given order).
7539 __isl_give isl_basic_set *isl_basic_set_list_product(
7540 __isl_take struct isl_basic_set_list *list)
7548 struct isl_basic_set *product = NULL;
7552 isl_assert(list->ctx, list->n > 0, goto error);
7553 isl_assert(list->ctx, list->p[0], goto error);
7554 nparam = isl_basic_set_n_param(list->p[0]);
7555 dim = isl_basic_set_n_dim(list->p[0]);
7556 extra = list->p[0]->n_div;
7557 n_eq = list->p[0]->n_eq;
7558 n_ineq = list->p[0]->n_ineq;
7559 for (i = 1; i < list->n; ++i) {
7560 isl_assert(list->ctx, list->p[i], goto error);
7561 isl_assert(list->ctx,
7562 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7563 dim += isl_basic_set_n_dim(list->p[i]);
7564 extra += list->p[i]->n_div;
7565 n_eq += list->p[i]->n_eq;
7566 n_ineq += list->p[i]->n_ineq;
7568 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7573 for (i = 0; i < list->n; ++i) {
7574 isl_basic_set_add_constraints(product,
7575 isl_basic_set_copy(list->p[i]), dim);
7576 dim += isl_basic_set_n_dim(list->p[i]);
7578 isl_basic_set_list_free(list);
7581 isl_basic_set_free(product);
7582 isl_basic_set_list_free(list);
7586 struct isl_basic_map *isl_basic_map_product(
7587 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7589 struct isl_dim *dim_result = NULL;
7590 struct isl_basic_map *bmap;
7591 unsigned in1, in2, out1, out2, nparam, total, pos;
7592 struct isl_dim_map *dim_map1, *dim_map2;
7594 if (!bmap1 || !bmap2)
7597 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7598 bmap2->dim, isl_dim_param), goto error);
7599 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7600 isl_dim_copy(bmap2->dim));
7602 in1 = isl_basic_map_n_in(bmap1);
7603 in2 = isl_basic_map_n_in(bmap2);
7604 out1 = isl_basic_map_n_out(bmap1);
7605 out2 = isl_basic_map_n_out(bmap2);
7606 nparam = isl_basic_map_n_param(bmap1);
7608 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7609 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7610 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7611 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7612 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7613 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7614 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7615 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7616 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7617 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7618 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7620 bmap = isl_basic_map_alloc_dim(dim_result,
7621 bmap1->n_div + bmap2->n_div,
7622 bmap1->n_eq + bmap2->n_eq,
7623 bmap1->n_ineq + bmap2->n_ineq);
7624 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7625 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7626 bmap = isl_basic_map_simplify(bmap);
7627 return isl_basic_map_finalize(bmap);
7629 isl_basic_map_free(bmap1);
7630 isl_basic_map_free(bmap2);
7634 __isl_give isl_basic_map *isl_basic_map_flat_product(
7635 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7637 isl_basic_map *prod;
7639 prod = isl_basic_map_product(bmap1, bmap2);
7640 prod = isl_basic_map_flatten(prod);
7644 __isl_give isl_basic_set *isl_basic_set_flat_product(
7645 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7647 return isl_basic_map_flat_product(bset1, bset2);
7650 __isl_give isl_basic_map *isl_basic_map_range_product(
7651 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7653 isl_dim *dim_result = NULL;
7654 isl_basic_map *bmap;
7655 unsigned in, out1, out2, nparam, total, pos;
7656 struct isl_dim_map *dim_map1, *dim_map2;
7658 if (!bmap1 || !bmap2)
7661 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7662 isl_dim_copy(bmap2->dim));
7664 in = isl_basic_map_dim(bmap1, isl_dim_in);
7665 out1 = isl_basic_map_n_out(bmap1);
7666 out2 = isl_basic_map_n_out(bmap2);
7667 nparam = isl_basic_map_n_param(bmap1);
7669 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7670 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7671 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7672 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7673 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7674 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7675 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7676 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7677 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7678 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7679 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7681 bmap = isl_basic_map_alloc_dim(dim_result,
7682 bmap1->n_div + bmap2->n_div,
7683 bmap1->n_eq + bmap2->n_eq,
7684 bmap1->n_ineq + bmap2->n_ineq);
7685 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7686 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7687 bmap = isl_basic_map_simplify(bmap);
7688 return isl_basic_map_finalize(bmap);
7690 isl_basic_map_free(bmap1);
7691 isl_basic_map_free(bmap2);
7695 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
7696 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7698 isl_basic_map *prod;
7700 prod = isl_basic_map_range_product(bmap1, bmap2);
7701 prod = isl_basic_map_flatten_range(prod);
7705 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7706 __isl_take isl_map *map2,
7707 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7708 __isl_take isl_dim *right),
7709 __isl_give isl_basic_map *(*basic_map_product)(
7710 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7713 struct isl_map *result;
7719 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7720 map2->dim, isl_dim_param), goto error);
7722 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7723 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7724 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7726 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7727 isl_dim_copy(map2->dim)),
7728 map1->n * map2->n, flags);
7731 for (i = 0; i < map1->n; ++i)
7732 for (j = 0; j < map2->n; ++j) {
7733 struct isl_basic_map *part;
7734 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7735 isl_basic_map_copy(map2->p[j]));
7736 if (isl_basic_map_is_empty(part))
7737 isl_basic_map_free(part);
7739 result = isl_map_add_basic_map(result, part);
7752 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7754 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
7755 __isl_take isl_map *map2)
7757 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7760 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
7761 __isl_take isl_map *map2)
7763 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
7766 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7768 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7769 __isl_take isl_map *map2)
7773 prod = isl_map_product(map1, map2);
7774 prod = isl_map_flatten(prod);
7778 /* Given two set A and B, construct its Cartesian product A x B.
7780 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7782 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7783 (struct isl_map *)set2);
7786 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7787 __isl_take isl_set *set2)
7789 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7792 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7794 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
7795 __isl_take isl_map *map2)
7797 return map_product(map1, map2, &isl_dim_range_product,
7798 &isl_basic_map_range_product);
7801 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7802 __isl_take isl_map *map2)
7804 return isl_map_align_params_map_map_and(map1, map2,
7805 &map_range_product_aligned);
7808 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7810 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
7811 __isl_take isl_map *map2)
7815 prod = isl_map_range_product(map1, map2);
7816 prod = isl_map_flatten_range(prod);
7820 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7823 uint32_t hash = isl_hash_init();
7828 bmap = isl_basic_map_copy(bmap);
7829 bmap = isl_basic_map_normalize(bmap);
7832 total = isl_basic_map_total_dim(bmap);
7833 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7834 for (i = 0; i < bmap->n_eq; ++i) {
7836 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7837 isl_hash_hash(hash, c_hash);
7839 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7840 for (i = 0; i < bmap->n_ineq; ++i) {
7842 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7843 isl_hash_hash(hash, c_hash);
7845 isl_hash_byte(hash, bmap->n_div & 0xFF);
7846 for (i = 0; i < bmap->n_div; ++i) {
7848 if (isl_int_is_zero(bmap->div[i][0]))
7850 isl_hash_byte(hash, i & 0xFF);
7851 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7852 isl_hash_hash(hash, c_hash);
7854 isl_basic_map_free(bmap);
7858 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7860 return isl_basic_map_get_hash((isl_basic_map *)bset);
7863 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7870 map = isl_map_copy(map);
7871 map = isl_map_normalize(map);
7875 hash = isl_hash_init();
7876 for (i = 0; i < map->n; ++i) {
7878 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7879 isl_hash_hash(hash, bmap_hash);
7887 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7889 return isl_map_get_hash((isl_map *)set);
7892 /* Check if the value for dimension dim is completely determined
7893 * by the values of the other parameters and variables.
7894 * That is, check if dimension dim is involved in an equality.
7896 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7903 nparam = isl_basic_set_n_param(bset);
7904 for (i = 0; i < bset->n_eq; ++i)
7905 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7910 /* Check if the value for dimension dim is completely determined
7911 * by the values of the other parameters and variables.
7912 * That is, check if dimension dim is involved in an equality
7913 * for each of the subsets.
7915 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7921 for (i = 0; i < set->n; ++i) {
7923 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7930 int isl_set_n_basic_set(__isl_keep isl_set *set)
7932 return set ? set->n : 0;
7935 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7936 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7943 for (i = 0; i < map->n; ++i)
7944 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7950 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7951 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7958 for (i = 0; i < set->n; ++i)
7959 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7965 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7967 struct isl_dim *dim;
7972 bset = isl_basic_set_cow(bset);
7976 dim = isl_basic_set_get_dim(bset);
7977 dim = isl_dim_lift(dim, bset->n_div);
7980 isl_dim_free(bset->dim);
7982 bset->extra -= bset->n_div;
7985 bset = isl_basic_set_finalize(bset);
7989 isl_basic_set_free(bset);
7993 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7996 struct isl_dim *dim;
7999 set = isl_set_align_divs(set);
8004 set = isl_set_cow(set);
8008 n_div = set->p[0]->n_div;
8009 dim = isl_set_get_dim(set);
8010 dim = isl_dim_lift(dim, n_div);
8013 isl_dim_free(set->dim);
8016 for (i = 0; i < set->n; ++i) {
8017 set->p[i] = isl_basic_set_lift(set->p[i]);
8028 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8030 struct isl_dim *dim;
8031 struct isl_basic_map *bmap;
8038 set = isl_set_align_divs(set);
8043 dim = isl_set_get_dim(set);
8044 if (set->n == 0 || set->p[0]->n_div == 0) {
8046 return isl_map_identity(isl_dim_map_from_set(dim));
8049 n_div = set->p[0]->n_div;
8050 dim = isl_dim_map_from_set(dim);
8051 n_param = isl_dim_size(dim, isl_dim_param);
8052 n_set = isl_dim_size(dim, isl_dim_in);
8053 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
8054 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
8055 for (i = 0; i < n_set; ++i)
8056 bmap = var_equal(bmap, i);
8058 total = n_param + n_set + n_set + n_div;
8059 for (i = 0; i < n_div; ++i) {
8060 k = isl_basic_map_alloc_inequality(bmap);
8063 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8064 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8065 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8066 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8067 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8068 set->p[0]->div[i][0]);
8070 l = isl_basic_map_alloc_inequality(bmap);
8073 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8074 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8075 set->p[0]->div[i][0]);
8076 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8080 bmap = isl_basic_map_simplify(bmap);
8081 bmap = isl_basic_map_finalize(bmap);
8082 return isl_map_from_basic_map(bmap);
8085 isl_basic_map_free(bmap);
8089 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8097 dim = isl_basic_set_total_dim(bset);
8098 size += bset->n_eq * (1 + dim);
8099 size += bset->n_ineq * (1 + dim);
8100 size += bset->n_div * (2 + dim);
8105 int isl_set_size(__isl_keep isl_set *set)
8113 for (i = 0; i < set->n; ++i)
8114 size += isl_basic_set_size(set->p[i]);
8119 /* Check if there is any lower bound (if lower == 0) and/or upper
8120 * bound (if upper == 0) on the specified dim.
8122 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8123 enum isl_dim_type type, unsigned pos, int lower, int upper)
8130 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8132 pos += isl_basic_map_offset(bmap, type);
8134 for (i = 0; i < bmap->n_div; ++i) {
8135 if (isl_int_is_zero(bmap->div[i][0]))
8137 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8141 for (i = 0; i < bmap->n_eq; ++i)
8142 if (!isl_int_is_zero(bmap->eq[i][pos]))
8145 for (i = 0; i < bmap->n_ineq; ++i) {
8146 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8153 return lower && upper;
8156 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8157 enum isl_dim_type type, unsigned pos)
8159 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8162 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8163 enum isl_dim_type type, unsigned pos)
8165 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8168 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8169 enum isl_dim_type type, unsigned pos)
8171 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8174 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8175 enum isl_dim_type type, unsigned pos)
8182 for (i = 0; i < map->n; ++i) {
8184 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8185 if (bounded < 0 || !bounded)
8192 /* Return 1 if the specified dim is involved in both an upper bound
8193 * and a lower bound.
8195 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8196 enum isl_dim_type type, unsigned pos)
8198 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8201 static int has_bound(__isl_keep isl_map *map,
8202 enum isl_dim_type type, unsigned pos,
8203 int (*fn)(__isl_keep isl_basic_map *bmap,
8204 enum isl_dim_type type, unsigned pos))
8211 for (i = 0; i < map->n; ++i) {
8213 bounded = fn(map->p[i], type, pos);
8214 if (bounded < 0 || bounded)
8221 /* Return 1 if the specified dim is involved in any lower bound.
8223 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8224 enum isl_dim_type type, unsigned pos)
8226 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8229 /* Return 1 if the specified dim is involved in any upper bound.
8231 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8232 enum isl_dim_type type, unsigned pos)
8234 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8237 /* For each of the "n" variables starting at "first", determine
8238 * the sign of the variable and put the results in the first "n"
8239 * elements of the array "signs".
8241 * 1 means that the variable is non-negative
8242 * -1 means that the variable is non-positive
8243 * 0 means the variable attains both positive and negative values.
8245 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8246 unsigned first, unsigned n, int *signs)
8248 isl_vec *bound = NULL;
8249 struct isl_tab *tab = NULL;
8250 struct isl_tab_undo *snap;
8253 if (!bset || !signs)
8256 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8257 tab = isl_tab_from_basic_set(bset);
8261 isl_seq_clr(bound->el, bound->size);
8262 isl_int_set_si(bound->el[0], -1);
8264 snap = isl_tab_snap(tab);
8265 for (i = 0; i < n; ++i) {
8268 isl_int_set_si(bound->el[1 + first + i], -1);
8269 if (isl_tab_add_ineq(tab, bound->el) < 0)
8272 isl_int_set_si(bound->el[1 + first + i], 0);
8273 if (isl_tab_rollback(tab, snap) < 0)
8281 isl_int_set_si(bound->el[1 + first + i], 1);
8282 if (isl_tab_add_ineq(tab, bound->el) < 0)
8285 isl_int_set_si(bound->el[1 + first + i], 0);
8286 if (isl_tab_rollback(tab, snap) < 0)
8289 signs[i] = empty ? -1 : 0;
8293 isl_vec_free(bound);
8297 isl_vec_free(bound);
8301 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8302 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8304 if (!bset || !signs)
8306 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8309 first += pos(bset->dim, type) - 1;
8310 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8313 /* Check if the given basic map is obviously single-valued.
8314 * In particular, for each output dimension, check that there is
8315 * an equality that defines the output dimension in terms of
8316 * earlier dimensions.
8318 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8328 total = 1 + isl_basic_map_total_dim(bmap);
8329 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8330 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8332 for (i = 0; i < n_out; ++i) {
8333 for (j = 0; j < bmap->n_eq; ++j) {
8334 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8336 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8337 total - (o_out + i + 1)) == -1)
8340 if (j >= bmap->n_eq)
8347 /* Check if the given map is obviously single-valued.
8349 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8358 return isl_basic_map_plain_is_single_valued(map->p[0]);
8361 /* Check if the given map is single-valued.
8366 * and check if the result is a subset of the identity mapping.
8368 int isl_map_is_single_valued(__isl_keep isl_map *map)
8375 sv = isl_map_plain_is_single_valued(map);
8379 test = isl_map_reverse(isl_map_copy(map));
8380 test = isl_map_apply_range(test, isl_map_copy(map));
8382 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
8383 id = isl_map_identity(dim);
8385 sv = isl_map_is_subset(test, id);
8393 int isl_map_is_injective(__isl_keep isl_map *map)
8397 map = isl_map_copy(map);
8398 map = isl_map_reverse(map);
8399 in = isl_map_is_single_valued(map);
8405 /* Check if the given map is obviously injective.
8407 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8411 map = isl_map_copy(map);
8412 map = isl_map_reverse(map);
8413 in = isl_map_plain_is_single_valued(map);
8419 int isl_map_is_bijective(__isl_keep isl_map *map)
8423 sv = isl_map_is_single_valued(map);
8427 return isl_map_is_injective(map);
8430 int isl_set_is_singleton(__isl_keep isl_set *set)
8432 return isl_map_is_single_valued((isl_map *)set);
8435 int isl_map_is_translation(__isl_keep isl_map *map)
8440 delta = isl_map_deltas(isl_map_copy(map));
8441 ok = isl_set_is_singleton(delta);
8442 isl_set_free(delta);
8447 static int unique(isl_int *p, unsigned pos, unsigned len)
8449 if (isl_seq_first_non_zero(p, pos) != -1)
8451 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8456 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8465 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8468 nvar = isl_basic_set_dim(bset, isl_dim_set);
8469 ovar = isl_dim_offset(bset->dim, isl_dim_set);
8470 for (j = 0; j < nvar; ++j) {
8471 int lower = 0, upper = 0;
8472 for (i = 0; i < bset->n_eq; ++i) {
8473 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8475 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8481 for (i = 0; i < bset->n_ineq; ++i) {
8482 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8484 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8486 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8491 if (!lower || !upper)
8498 int isl_set_is_box(__isl_keep isl_set *set)
8505 return isl_basic_set_is_box(set->p[0]);
8508 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8513 return isl_dim_is_wrapping(bset->dim);
8516 int isl_set_is_wrapping(__isl_keep isl_set *set)
8521 return isl_dim_is_wrapping(set->dim);
8524 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8526 bmap = isl_basic_map_cow(bmap);
8530 bmap->dim = isl_dim_wrap(bmap->dim);
8534 bmap = isl_basic_map_finalize(bmap);
8536 return (isl_basic_set *)bmap;
8538 isl_basic_map_free(bmap);
8542 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8546 map = isl_map_cow(map);
8550 for (i = 0; i < map->n; ++i) {
8551 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8555 map->dim = isl_dim_wrap(map->dim);
8559 return (isl_set *)map;
8565 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8567 bset = isl_basic_set_cow(bset);
8571 bset->dim = isl_dim_unwrap(bset->dim);
8575 bset = isl_basic_set_finalize(bset);
8577 return (isl_basic_map *)bset;
8579 isl_basic_set_free(bset);
8583 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8590 if (!isl_set_is_wrapping(set))
8591 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8594 set = isl_set_cow(set);
8598 for (i = 0; i < set->n; ++i) {
8599 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8604 set->dim = isl_dim_unwrap(set->dim);
8608 return (isl_map *)set;
8614 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8615 enum isl_dim_type type)
8620 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8623 bmap = isl_basic_map_cow(bmap);
8627 bmap->dim = isl_dim_reset(bmap->dim, type);
8631 bmap = isl_basic_map_finalize(bmap);
8635 isl_basic_map_free(bmap);
8639 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8640 enum isl_dim_type type)
8647 if (!isl_dim_is_named_or_nested(map->dim, type))
8650 map = isl_map_cow(map);
8654 for (i = 0; i < map->n; ++i) {
8655 map->p[i] = isl_basic_map_reset(map->p[i], type);
8659 map->dim = isl_dim_reset(map->dim, type);
8669 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8674 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8677 bmap = isl_basic_map_cow(bmap);
8681 bmap->dim = isl_dim_flatten(bmap->dim);
8685 bmap = isl_basic_map_finalize(bmap);
8689 isl_basic_map_free(bmap);
8693 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8695 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8698 __isl_give isl_basic_map *isl_basic_map_flatten_range(
8699 __isl_take isl_basic_map *bmap)
8704 if (!bmap->dim->nested[1])
8707 bmap = isl_basic_map_cow(bmap);
8711 bmap->dim = isl_dim_flatten_range(bmap->dim);
8715 bmap = isl_basic_map_finalize(bmap);
8719 isl_basic_map_free(bmap);
8723 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8730 if (!map->dim->nested[0] && !map->dim->nested[1])
8733 map = isl_map_cow(map);
8737 for (i = 0; i < map->n; ++i) {
8738 map->p[i] = isl_basic_map_flatten(map->p[i]);
8742 map->dim = isl_dim_flatten(map->dim);
8752 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8754 return (isl_set *)isl_map_flatten((isl_map *)set);
8757 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8759 isl_dim *dim, *flat_dim;
8762 dim = isl_set_get_dim(set);
8763 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8764 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8765 map = isl_map_intersect_domain(map, set);
8770 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
8777 if (!map->dim->nested[1])
8780 map = isl_map_cow(map);
8784 for (i = 0; i < map->n; ++i) {
8785 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
8789 map->dim = isl_dim_flatten_range(map->dim);
8799 /* Reorder the dimensions of "bmap" according to the given dim_map
8800 * and set the dimension specification to "dim".
8802 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8803 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8807 bmap = isl_basic_map_cow(bmap);
8808 if (!bmap || !dim || !dim_map)
8811 res = isl_basic_map_alloc_dim(dim,
8812 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8813 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8814 res = isl_basic_map_finalize(res);
8818 isl_basic_map_free(bmap);
8823 /* Reorder the dimensions of "map" according to given reordering.
8825 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8826 __isl_take isl_reordering *r)
8829 struct isl_dim_map *dim_map;
8831 map = isl_map_cow(map);
8832 dim_map = isl_dim_map_from_reordering(r);
8833 if (!map || !r || !dim_map)
8836 for (i = 0; i < map->n; ++i) {
8837 struct isl_dim_map *dim_map_i;
8839 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8841 map->p[i] = isl_basic_map_realign(map->p[i],
8842 isl_dim_copy(r->dim), dim_map_i);
8848 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8850 isl_reordering_free(r);
8856 isl_reordering_free(r);
8860 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8861 __isl_take isl_reordering *r)
8863 return (isl_set *)isl_map_realign((isl_map *)set, r);
8866 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
8867 __isl_take isl_dim *model)
8874 ctx = isl_dim_get_ctx(model);
8875 if (!isl_dim_has_named_params(model))
8876 isl_die(ctx, isl_error_invalid,
8877 "model has unnamed parameters", goto error);
8878 if (!isl_dim_has_named_params(map->dim))
8879 isl_die(ctx, isl_error_invalid,
8880 "relation has unnamed parameters", goto error);
8881 if (!isl_dim_match(map->dim, isl_dim_param, model, isl_dim_param)) {
8882 isl_reordering *exp;
8884 model = isl_dim_drop(model, isl_dim_in,
8885 0, isl_dim_size(model, isl_dim_in));
8886 model = isl_dim_drop(model, isl_dim_out,
8887 0, isl_dim_size(model, isl_dim_out));
8888 exp = isl_parameter_alignment_reordering(map->dim, model);
8889 exp = isl_reordering_extend_dim(exp, isl_map_get_dim(map));
8890 map = isl_map_realign(map, exp);
8893 isl_dim_free(model);
8896 isl_dim_free(model);
8901 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
8902 __isl_take isl_dim *model)
8904 return isl_map_align_params(set, model);
8907 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8908 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8909 enum isl_dim_type c2, enum isl_dim_type c3,
8910 enum isl_dim_type c4, enum isl_dim_type c5)
8912 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8913 struct isl_mat *mat;
8919 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8920 isl_basic_map_total_dim(bmap) + 1);
8923 for (i = 0; i < bmap->n_eq; ++i)
8924 for (j = 0, pos = 0; j < 5; ++j) {
8925 int off = isl_basic_map_offset(bmap, c[j]);
8926 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8927 isl_int_set(mat->row[i][pos],
8928 bmap->eq[i][off + k]);
8936 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8937 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8938 enum isl_dim_type c2, enum isl_dim_type c3,
8939 enum isl_dim_type c4, enum isl_dim_type c5)
8941 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8942 struct isl_mat *mat;
8948 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8949 isl_basic_map_total_dim(bmap) + 1);
8952 for (i = 0; i < bmap->n_ineq; ++i)
8953 for (j = 0, pos = 0; j < 5; ++j) {
8954 int off = isl_basic_map_offset(bmap, c[j]);
8955 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8956 isl_int_set(mat->row[i][pos],
8957 bmap->ineq[i][off + k]);
8965 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8966 __isl_take isl_dim *dim,
8967 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8968 enum isl_dim_type c2, enum isl_dim_type c3,
8969 enum isl_dim_type c4, enum isl_dim_type c5)
8971 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8972 isl_basic_map *bmap;
8978 if (!dim || !eq || !ineq)
8981 if (eq->n_col != ineq->n_col)
8982 isl_die(dim->ctx, isl_error_invalid,
8983 "equalities and inequalities matrices should have "
8984 "same number of columns", goto error);
8986 total = 1 + isl_dim_total(dim);
8988 if (eq->n_col < total)
8989 isl_die(dim->ctx, isl_error_invalid,
8990 "number of columns too small", goto error);
8992 extra = eq->n_col - total;
8994 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8995 eq->n_row, ineq->n_row);
8998 for (i = 0; i < extra; ++i) {
8999 k = isl_basic_map_alloc_div(bmap);
9002 isl_int_set_si(bmap->div[k][0], 0);
9004 for (i = 0; i < eq->n_row; ++i) {
9005 l = isl_basic_map_alloc_equality(bmap);
9008 for (j = 0, pos = 0; j < 5; ++j) {
9009 int off = isl_basic_map_offset(bmap, c[j]);
9010 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9011 isl_int_set(bmap->eq[l][off + k],
9017 for (i = 0; i < ineq->n_row; ++i) {
9018 l = isl_basic_map_alloc_inequality(bmap);
9021 for (j = 0, pos = 0; j < 5; ++j) {
9022 int off = isl_basic_map_offset(bmap, c[j]);
9023 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9024 isl_int_set(bmap->ineq[l][off + k],
9043 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9044 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9045 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9047 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9048 c1, c2, c3, c4, isl_dim_in);
9051 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9052 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9053 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9055 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9056 c1, c2, c3, c4, isl_dim_in);
9059 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9060 __isl_take isl_dim *dim,
9061 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9062 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9064 return (isl_basic_set*)
9065 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9066 c1, c2, c3, c4, isl_dim_in);
9069 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9074 return isl_dim_can_zip(bmap->dim);
9077 int isl_map_can_zip(__isl_keep isl_map *map)
9082 return isl_dim_can_zip(map->dim);
9085 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9086 * (A -> C) -> (B -> D).
9088 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9097 if (!isl_basic_map_can_zip(bmap))
9098 isl_die(bmap->ctx, isl_error_invalid,
9099 "basic map cannot be zipped", goto error);
9100 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9101 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
9102 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
9103 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
9104 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9107 bmap->dim = isl_dim_zip(bmap->dim);
9112 isl_basic_map_free(bmap);
9116 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9117 * (A -> C) -> (B -> D).
9119 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9126 if (!isl_map_can_zip(map))
9127 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9130 map = isl_map_cow(map);
9134 for (i = 0; i < map->n; ++i) {
9135 map->p[i] = isl_basic_map_zip(map->p[i]);
9140 map->dim = isl_dim_zip(map->dim);
9150 /* Construct a basic map mapping the domain of the affine expression
9151 * to a one-dimensional range prescribed by the affine expression.
9153 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9157 isl_local_space *ls;
9158 isl_basic_map *bmap;
9163 ls = isl_aff_get_local_space(aff);
9164 ls = isl_local_space_from_domain(ls);
9165 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
9166 bmap = isl_basic_map_from_local_space(ls);
9167 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9168 k = isl_basic_map_alloc_equality(bmap);
9172 pos = isl_basic_map_offset(bmap, isl_dim_out);
9173 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9174 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9175 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9176 aff->v->size - (pos + 1));
9179 bmap = isl_basic_map_finalize(bmap);
9183 isl_basic_map_free(bmap);
9187 /* Construct a basic map mapping a domain in the given space to
9188 * to an n-dimensional range, with n the number of elements in the list,
9189 * where each coordinate in the range is prescribed by the
9190 * corresponding affine expression.
9191 * The domains of all affine expressions in the list are assumed to match
9194 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9195 __isl_take isl_dim *domain_dim, __isl_take isl_aff_list *list)
9199 isl_basic_map *bmap;
9204 dim = isl_dim_from_domain(domain_dim);
9205 bmap = isl_basic_map_universe(dim);
9207 for (i = 0; i < list->n; ++i) {
9209 isl_basic_map *bmap_i;
9211 aff = isl_aff_copy(list->p[i]);
9212 bmap_i = isl_basic_map_from_aff(aff);
9214 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9217 isl_aff_list_free(list);
9221 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9222 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9224 return isl_map_equate(set, type1, pos1, type2, pos2);
9227 /* Add a constraint imposing that the given two dimensions are equal.
9229 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9230 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9232 isl_basic_map *bmap = NULL;
9238 if (pos1 >= isl_map_dim(map, type1))
9239 isl_die(map->ctx, isl_error_invalid,
9240 "index out of bounds", goto error);
9241 if (pos2 >= isl_map_dim(map, type2))
9242 isl_die(map->ctx, isl_error_invalid,
9243 "index out of bounds", goto error);
9245 bmap = isl_basic_map_alloc_dim(isl_map_get_dim(map), 0, 1, 0);
9246 i = isl_basic_map_alloc_equality(bmap);
9249 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9250 pos1 += isl_basic_map_offset(bmap, type1);
9251 pos2 += isl_basic_map_offset(bmap, type2);
9252 isl_int_set_si(bmap->eq[i][pos1], -1);
9253 isl_int_set_si(bmap->eq[i][pos2], 1);
9254 bmap = isl_basic_map_finalize(bmap);
9256 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9260 isl_basic_map_free(bmap);
9265 /* Add a constraint imposing that the given two dimensions have opposite values.
9267 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9268 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9270 isl_basic_map *bmap = NULL;
9276 if (pos1 >= isl_map_dim(map, type1))
9277 isl_die(map->ctx, isl_error_invalid,
9278 "index out of bounds", goto error);
9279 if (pos2 >= isl_map_dim(map, type2))
9280 isl_die(map->ctx, isl_error_invalid,
9281 "index out of bounds", goto error);
9283 bmap = isl_basic_map_alloc_dim(isl_map_get_dim(map), 0, 1, 0);
9284 i = isl_basic_map_alloc_equality(bmap);
9287 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9288 pos1 += isl_basic_map_offset(bmap, type1);
9289 pos2 += isl_basic_map_offset(bmap, type2);
9290 isl_int_set_si(bmap->eq[i][pos1], 1);
9291 isl_int_set_si(bmap->eq[i][pos2], 1);
9292 bmap = isl_basic_map_finalize(bmap);
9294 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9298 isl_basic_map_free(bmap);