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
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
18 #include "isl_dim_private.h"
19 #include "isl_equalities.h"
20 #include <isl_list_private.h>
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
30 #include <isl_mat_private.h>
31 #include <isl_dim_map.h>
32 #include <isl_local_space_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->dim->n_in;
167 unsigned isl_map_n_out(const struct isl_map *map)
169 return map->dim->n_out;
172 unsigned isl_map_n_param(const struct isl_map *map)
174 return map->dim->nparam;
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 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
353 enum isl_dim_type type)
355 return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
358 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
359 enum isl_dim_type type, const char *s)
363 map = isl_map_cow(map);
367 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
371 for (i = 0; i < map->n; ++i) {
372 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
383 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
384 enum isl_dim_type type)
386 return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
389 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
392 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
395 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
397 return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
400 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
402 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
405 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
406 enum isl_dim_type type, unsigned pos)
408 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
411 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
412 enum isl_dim_type type, unsigned pos)
414 return bset ? isl_dim_get_name(bset->dim, type, pos) : NULL;
417 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
418 enum isl_dim_type type, unsigned pos)
420 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
423 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
424 enum isl_dim_type type, unsigned pos)
426 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
429 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
430 __isl_take isl_basic_map *bmap,
431 enum isl_dim_type type, unsigned pos, const char *s)
435 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
440 isl_basic_map_free(bmap);
444 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
445 enum isl_dim_type type, unsigned pos, const char *s)
452 map->dim = isl_dim_set_name(map->dim, type, pos, s);
456 for (i = 0; i < map->n; ++i) {
457 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
468 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
469 __isl_take isl_basic_set *bset,
470 enum isl_dim_type type, unsigned pos, const char *s)
472 return (isl_basic_set *)isl_basic_map_set_dim_name(
473 (isl_basic_map *)bset, type, pos, s);
476 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
477 enum isl_dim_type type, unsigned pos, const char *s)
479 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
482 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
486 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
489 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
491 return isl_basic_map_is_rational(bset);
494 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
495 struct isl_basic_map *bmap, unsigned extra,
496 unsigned n_eq, unsigned n_ineq)
499 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
504 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
505 if (isl_blk_is_error(bmap->block))
508 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
513 bmap->block2 = isl_blk_empty();
516 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
517 if (isl_blk_is_error(bmap->block2))
520 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
525 for (i = 0; i < n_ineq + n_eq; ++i)
526 bmap->ineq[i] = bmap->block.data + i * row_size;
528 for (i = 0; i < extra; ++i)
529 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
533 bmap->c_size = n_eq + n_ineq;
534 bmap->eq = bmap->ineq + n_ineq;
543 isl_basic_map_free(bmap);
547 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
548 unsigned nparam, unsigned dim, unsigned extra,
549 unsigned n_eq, unsigned n_ineq)
551 struct isl_basic_map *bmap;
552 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
553 return (struct isl_basic_set *)bmap;
556 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
557 unsigned extra, unsigned n_eq, unsigned n_ineq)
559 struct isl_basic_map *bmap;
562 isl_assert(dim->ctx, dim->n_in == 0, goto error);
563 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
564 return (struct isl_basic_set *)bmap;
570 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
571 unsigned extra, unsigned n_eq, unsigned n_ineq)
573 struct isl_basic_map *bmap;
577 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
582 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
588 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
589 unsigned nparam, unsigned in, unsigned out, unsigned extra,
590 unsigned n_eq, unsigned n_ineq)
592 struct isl_basic_map *bmap;
595 dim = isl_dim_alloc(ctx, nparam, in, out);
599 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
603 static void dup_constraints(
604 struct isl_basic_map *dst, struct isl_basic_map *src)
607 unsigned total = isl_basic_map_total_dim(src);
609 for (i = 0; i < src->n_eq; ++i) {
610 int j = isl_basic_map_alloc_equality(dst);
611 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
614 for (i = 0; i < src->n_ineq; ++i) {
615 int j = isl_basic_map_alloc_inequality(dst);
616 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
619 for (i = 0; i < src->n_div; ++i) {
620 int j = isl_basic_map_alloc_div(dst);
621 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
623 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
626 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
628 struct isl_basic_map *dup;
632 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
633 bmap->n_div, bmap->n_eq, bmap->n_ineq);
636 dup_constraints(dup, bmap);
637 dup->flags = bmap->flags;
638 dup->sample = isl_vec_copy(bmap->sample);
642 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
644 struct isl_basic_map *dup;
646 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
647 return (struct isl_basic_set *)dup;
650 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
655 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
659 return isl_basic_set_dup(bset);
662 struct isl_set *isl_set_copy(struct isl_set *set)
671 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
676 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
680 bmap = isl_basic_map_dup(bmap);
682 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
686 struct isl_map *isl_map_copy(struct isl_map *map)
695 void isl_basic_map_free(struct isl_basic_map *bmap)
703 isl_ctx_deref(bmap->ctx);
705 isl_blk_free(bmap->ctx, bmap->block2);
707 isl_blk_free(bmap->ctx, bmap->block);
708 isl_vec_free(bmap->sample);
709 isl_dim_free(bmap->dim);
713 void isl_basic_set_free(struct isl_basic_set *bset)
715 isl_basic_map_free((struct isl_basic_map *)bset);
718 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
720 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
723 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
729 isl_assert(ctx, room_for_con(bmap, 1), return -1);
730 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
732 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
733 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
734 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
735 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
736 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
737 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
739 int j = isl_basic_map_alloc_inequality(bmap);
743 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
744 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
751 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
752 bmap->extra - bmap->n_div);
756 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
758 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
761 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
765 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
770 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
772 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
775 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
780 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
782 if (pos != bmap->n_eq - 1) {
784 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
785 bmap->eq[bmap->n_eq - 1] = t;
791 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
793 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
796 void isl_basic_map_inequality_to_equality(
797 struct isl_basic_map *bmap, unsigned pos)
802 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
803 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
808 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
809 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
810 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
811 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
814 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
816 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
819 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
825 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
826 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
827 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
828 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
829 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
830 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
831 1 + isl_basic_map_total_dim(bmap),
832 bmap->extra - bmap->n_div);
833 return bmap->n_ineq++;
836 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
838 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
841 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
845 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
850 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
852 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
855 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
860 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
862 if (pos != bmap->n_ineq - 1) {
864 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
865 bmap->ineq[bmap->n_ineq - 1] = t;
866 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
872 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
874 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
877 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
882 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
885 k = isl_basic_map_alloc_equality(bmap);
888 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
891 isl_basic_map_free(bmap);
895 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
898 return (isl_basic_set *)
899 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
902 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
907 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
910 k = isl_basic_map_alloc_inequality(bmap);
913 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
916 isl_basic_map_free(bmap);
920 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
923 return (isl_basic_set *)
924 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
927 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
931 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
932 isl_seq_clr(bmap->div[bmap->n_div] +
933 1 + 1 + isl_basic_map_total_dim(bmap),
934 bmap->extra - bmap->n_div);
935 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
936 return bmap->n_div++;
939 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
941 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
944 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
948 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
953 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
955 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
958 /* Copy constraint from src to dst, putting the vars of src at offset
959 * dim_off in dst and the divs of src at offset div_off in dst.
960 * If both sets are actually map, then dim_off applies to the input
963 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
964 struct isl_basic_map *src_map, isl_int *src,
965 unsigned in_off, unsigned out_off, unsigned div_off)
967 unsigned src_nparam = isl_basic_map_n_param(src_map);
968 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
969 unsigned src_in = isl_basic_map_n_in(src_map);
970 unsigned dst_in = isl_basic_map_n_in(dst_map);
971 unsigned src_out = isl_basic_map_n_out(src_map);
972 unsigned dst_out = isl_basic_map_n_out(dst_map);
973 isl_int_set(dst[0], src[0]);
974 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
975 if (dst_nparam > src_nparam)
976 isl_seq_clr(dst+1+src_nparam,
977 dst_nparam - src_nparam);
978 isl_seq_clr(dst+1+dst_nparam, in_off);
979 isl_seq_cpy(dst+1+dst_nparam+in_off,
981 isl_min(dst_in-in_off, src_in));
982 if (dst_in-in_off > src_in)
983 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
984 dst_in - in_off - src_in);
985 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
986 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
987 src+1+src_nparam+src_in,
988 isl_min(dst_out-out_off, src_out));
989 if (dst_out-out_off > src_out)
990 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
991 dst_out - out_off - src_out);
992 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
993 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
994 src+1+src_nparam+src_in+src_out,
995 isl_min(dst_map->extra-div_off, src_map->n_div));
996 if (dst_map->n_div-div_off > src_map->n_div)
997 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
998 div_off+src_map->n_div,
999 dst_map->n_div - div_off - src_map->n_div);
1002 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1003 struct isl_basic_map *src_map, isl_int *src,
1004 unsigned in_off, unsigned out_off, unsigned div_off)
1006 isl_int_set(dst[0], src[0]);
1007 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1010 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1011 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1016 if (!bmap1 || !bmap2)
1019 div_off = bmap1->n_div;
1021 for (i = 0; i < bmap2->n_eq; ++i) {
1022 int i1 = isl_basic_map_alloc_equality(bmap1);
1025 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1026 i_pos, o_pos, div_off);
1029 for (i = 0; i < bmap2->n_ineq; ++i) {
1030 int i1 = isl_basic_map_alloc_inequality(bmap1);
1033 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1034 i_pos, o_pos, div_off);
1037 for (i = 0; i < bmap2->n_div; ++i) {
1038 int i1 = isl_basic_map_alloc_div(bmap1);
1041 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1042 i_pos, o_pos, div_off);
1045 isl_basic_map_free(bmap2);
1050 isl_basic_map_free(bmap1);
1051 isl_basic_map_free(bmap2);
1055 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1056 struct isl_basic_set *bset2, unsigned pos)
1058 return (struct isl_basic_set *)
1059 add_constraints((struct isl_basic_map *)bset1,
1060 (struct isl_basic_map *)bset2, 0, pos);
1063 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1064 struct isl_dim *dim, unsigned extra,
1065 unsigned n_eq, unsigned n_ineq)
1067 struct isl_basic_map *ext;
1077 dims_ok = isl_dim_equal(base->dim, dim) &&
1078 base->extra >= base->n_div + extra;
1080 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1081 room_for_ineq(base, n_ineq)) {
1086 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1087 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1088 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1089 extra += base->extra;
1091 n_ineq += base->n_ineq;
1093 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1099 ext->sample = isl_vec_copy(base->sample);
1100 flags = base->flags;
1101 ext = add_constraints(ext, base, 0, 0);
1104 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1111 isl_basic_map_free(base);
1115 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1116 struct isl_dim *dim, unsigned extra,
1117 unsigned n_eq, unsigned n_ineq)
1119 return (struct isl_basic_set *)
1120 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1121 extra, n_eq, n_ineq);
1124 struct isl_basic_map *isl_basic_map_extend_constraints(
1125 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1129 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1133 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1134 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1135 unsigned n_eq, unsigned n_ineq)
1137 struct isl_basic_map *bmap;
1138 struct isl_dim *dim;
1142 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1146 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1149 isl_basic_map_free(base);
1153 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1154 unsigned nparam, unsigned dim, unsigned extra,
1155 unsigned n_eq, unsigned n_ineq)
1157 return (struct isl_basic_set *)
1158 isl_basic_map_extend((struct isl_basic_map *)base,
1159 nparam, 0, dim, extra, n_eq, n_ineq);
1162 struct isl_basic_set *isl_basic_set_extend_constraints(
1163 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1165 return (struct isl_basic_set *)
1166 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1170 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1172 return (struct isl_basic_set *)
1173 isl_basic_map_cow((struct isl_basic_map *)bset);
1176 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1181 if (bmap->ref > 1) {
1183 bmap = isl_basic_map_dup(bmap);
1186 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1190 struct isl_set *isl_set_cow(struct isl_set *set)
1198 return isl_set_dup(set);
1201 struct isl_map *isl_map_cow(struct isl_map *map)
1209 return isl_map_dup(map);
1212 static void swap_vars(struct isl_blk blk, isl_int *a,
1213 unsigned a_len, unsigned b_len)
1215 isl_seq_cpy(blk.data, a+a_len, b_len);
1216 isl_seq_cpy(blk.data+b_len, a, a_len);
1217 isl_seq_cpy(a, blk.data, b_len+a_len);
1220 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1221 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1229 isl_assert(bmap->ctx,
1230 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1232 if (n1 == 0 || n2 == 0)
1235 bmap = isl_basic_map_cow(bmap);
1239 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1240 if (isl_blk_is_error(blk))
1243 for (i = 0; i < bmap->n_eq; ++i)
1245 bmap->eq[i] + pos, n1, n2);
1247 for (i = 0; i < bmap->n_ineq; ++i)
1249 bmap->ineq[i] + pos, n1, n2);
1251 for (i = 0; i < bmap->n_div; ++i)
1253 bmap->div[i]+1 + pos, n1, n2);
1255 isl_blk_free(bmap->ctx, blk);
1257 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1258 bmap = isl_basic_map_gauss(bmap, NULL);
1259 return isl_basic_map_finalize(bmap);
1261 isl_basic_map_free(bmap);
1265 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1266 __isl_take isl_basic_set *bset, unsigned n)
1271 nparam = isl_basic_set_n_param(bset);
1272 dim = isl_basic_set_n_dim(bset);
1273 isl_assert(bset->ctx, n <= dim, goto error);
1275 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1277 isl_basic_set_free(bset);
1281 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1287 total = isl_basic_map_total_dim(bmap);
1288 isl_basic_map_free_div(bmap, bmap->n_div);
1289 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1291 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1293 i = isl_basic_map_alloc_equality(bmap);
1297 isl_int_set_si(bmap->eq[i][0], 1);
1298 isl_seq_clr(bmap->eq[i]+1, total);
1299 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1300 isl_vec_free(bmap->sample);
1301 bmap->sample = NULL;
1302 return isl_basic_map_finalize(bmap);
1304 isl_basic_map_free(bmap);
1308 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1310 return (struct isl_basic_set *)
1311 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1314 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1317 unsigned off = isl_dim_total(bmap->dim);
1318 isl_int *t = bmap->div[a];
1319 bmap->div[a] = bmap->div[b];
1322 for (i = 0; i < bmap->n_eq; ++i)
1323 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1325 for (i = 0; i < bmap->n_ineq; ++i)
1326 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1328 for (i = 0; i < bmap->n_div; ++i)
1329 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1330 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1333 /* Eliminate the specified n dimensions starting at first from the
1334 * constraints using Fourier-Motzkin. The dimensions themselves
1337 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1338 enum isl_dim_type type, unsigned first, unsigned n)
1347 map = isl_map_cow(map);
1350 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1351 first += pos(map->dim, type) - 1;
1353 for (i = 0; i < map->n; ++i) {
1354 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1364 /* Eliminate the specified n dimensions starting at first from the
1365 * constraints using Fourier-Motzkin. The dimensions themselves
1368 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1369 enum isl_dim_type type, unsigned first, unsigned n)
1371 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1374 /* Eliminate the specified n dimensions starting at first from the
1375 * constraints using Fourier-Motzkin. The dimensions themselves
1378 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1379 unsigned first, unsigned n)
1381 return isl_set_eliminate(set, isl_dim_set, first, n);
1384 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1385 __isl_take isl_basic_map *bmap)
1387 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1392 return isl_basic_map_finalize(bmap);
1395 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1396 __isl_take isl_basic_set *bset)
1398 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1399 (struct isl_basic_map *)bset);
1402 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1411 map = isl_map_cow(map);
1415 for (i = 0; i < map->n; ++i) {
1416 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1426 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1428 return isl_map_remove_divs(set);
1431 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1432 enum isl_dim_type type, unsigned first, unsigned n)
1436 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1440 bmap = isl_basic_map_eliminate_vars(bmap,
1441 isl_basic_map_offset(bmap, type) - 1 + first, n);
1444 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1446 bmap = isl_basic_map_drop(bmap, type, first, n);
1449 isl_basic_map_free(bmap);
1453 /* Return true if the definition of the given div (recursively) involves
1454 * any of the given variables.
1456 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1457 unsigned first, unsigned n)
1460 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1462 if (isl_int_is_zero(bmap->div[div][0]))
1464 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1467 for (i = bmap->n_div - 1; i >= 0; --i) {
1468 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1470 if (div_involves_vars(bmap, i, first, n))
1477 /* Remove all divs (recursively) involving any of the given dimensions
1478 * in their definitions.
1480 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1481 __isl_take isl_basic_map *bmap,
1482 enum isl_dim_type type, unsigned first, unsigned n)
1488 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1490 first += isl_basic_map_offset(bmap, type);
1492 for (i = bmap->n_div - 1; i >= 0; --i) {
1493 if (!div_involves_vars(bmap, i, first, n))
1495 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1500 isl_basic_map_free(bmap);
1504 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1505 enum isl_dim_type type, unsigned first, unsigned n)
1514 map = isl_map_cow(map);
1518 for (i = 0; i < map->n; ++i) {
1519 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1530 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1531 enum isl_dim_type type, unsigned first, unsigned n)
1533 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1537 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1538 enum isl_dim_type type, unsigned first, unsigned n)
1545 if (first + n > isl_basic_set_dim(bset, type))
1546 isl_die(bset->ctx, isl_error_invalid,
1547 "index out of bounds", return -1);
1549 first += isl_basic_set_offset(bset, type);
1550 for (i = 0; i < bset->n_eq; ++i)
1551 if (isl_seq_first_non_zero(bset->eq[i] + first, n) >= 0)
1553 for (i = 0; i < bset->n_ineq; ++i)
1554 if (isl_seq_first_non_zero(bset->ineq[i] + first, n) >= 0)
1560 int isl_set_involves_dims(__isl_keep isl_set *set,
1561 enum isl_dim_type type, unsigned first, unsigned n)
1568 if (first + n > isl_set_dim(set, type))
1569 isl_die(set->ctx, isl_error_invalid,
1570 "index out of bounds", return -1);
1572 for (i = 0; i < set->n; ++i) {
1573 int involves = isl_basic_set_involves_dims(set->p[i],
1575 if (involves < 0 || !involves)
1582 /* Return true if the definition of the given div is unknown or depends
1585 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1588 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1590 if (isl_int_is_zero(bmap->div[div][0]))
1593 for (i = bmap->n_div - 1; i >= 0; --i) {
1594 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1596 if (div_is_unknown(bmap, i))
1603 /* Remove all divs that are unknown or defined in terms of unknown divs.
1605 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1606 __isl_take isl_basic_map *bmap)
1613 for (i = bmap->n_div - 1; i >= 0; --i) {
1614 if (!div_is_unknown(bmap, i))
1616 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1622 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1631 map = isl_map_cow(map);
1635 for (i = 0; i < map->n; ++i) {
1636 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1646 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1648 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1651 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1652 __isl_take isl_basic_set *bset,
1653 enum isl_dim_type type, unsigned first, unsigned n)
1655 return (isl_basic_set *)
1656 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1659 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1660 enum isl_dim_type type, unsigned first, unsigned n)
1667 map = isl_map_cow(map);
1670 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1672 for (i = 0; i < map->n; ++i) {
1673 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1674 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1678 map = isl_map_drop(map, type, first, n);
1685 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1686 enum isl_dim_type type, unsigned first, unsigned n)
1688 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1691 /* Project out n inputs starting at first using Fourier-Motzkin */
1692 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1693 unsigned first, unsigned n)
1695 return isl_map_remove_dims(map, isl_dim_in, first, n);
1698 static void dump_term(struct isl_basic_map *bmap,
1699 isl_int c, int pos, FILE *out)
1702 unsigned in = isl_basic_map_n_in(bmap);
1703 unsigned dim = in + isl_basic_map_n_out(bmap);
1704 unsigned nparam = isl_basic_map_n_param(bmap);
1706 isl_int_print(out, c, 0);
1708 if (!isl_int_is_one(c))
1709 isl_int_print(out, c, 0);
1710 if (pos < 1 + nparam) {
1711 name = isl_dim_get_name(bmap->dim,
1712 isl_dim_param, pos - 1);
1714 fprintf(out, "%s", name);
1716 fprintf(out, "p%d", pos - 1);
1717 } else if (pos < 1 + nparam + in)
1718 fprintf(out, "i%d", pos - 1 - nparam);
1719 else if (pos < 1 + nparam + dim)
1720 fprintf(out, "o%d", pos - 1 - nparam - in);
1722 fprintf(out, "e%d", pos - 1 - nparam - dim);
1726 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1727 int sign, FILE *out)
1731 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1735 for (i = 0, first = 1; i < len; ++i) {
1736 if (isl_int_sgn(c[i]) * sign <= 0)
1739 fprintf(out, " + ");
1741 isl_int_abs(v, c[i]);
1742 dump_term(bmap, v, i, out);
1749 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1750 const char *op, FILE *out, int indent)
1754 fprintf(out, "%*s", indent, "");
1756 dump_constraint_sign(bmap, c, 1, out);
1757 fprintf(out, " %s ", op);
1758 dump_constraint_sign(bmap, c, -1, out);
1762 for (i = bmap->n_div; i < bmap->extra; ++i) {
1763 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1765 fprintf(out, "%*s", indent, "");
1766 fprintf(out, "ERROR: unused div coefficient not zero\n");
1771 static void dump_constraints(struct isl_basic_map *bmap,
1772 isl_int **c, unsigned n,
1773 const char *op, FILE *out, int indent)
1777 for (i = 0; i < n; ++i)
1778 dump_constraint(bmap, c[i], op, out, indent);
1781 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1785 unsigned total = isl_basic_map_total_dim(bmap);
1787 for (j = 0; j < 1 + total; ++j) {
1788 if (isl_int_is_zero(exp[j]))
1790 if (!first && isl_int_is_pos(exp[j]))
1792 dump_term(bmap, exp[j], j, out);
1797 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1801 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1802 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1804 for (i = 0; i < bmap->n_div; ++i) {
1805 fprintf(out, "%*s", indent, "");
1806 fprintf(out, "e%d = [(", i);
1807 dump_affine(bmap, bmap->div[i]+1, out);
1809 isl_int_print(out, bmap->div[i][0], 0);
1810 fprintf(out, "]\n");
1814 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1815 FILE *out, int indent)
1818 fprintf(out, "null basic set\n");
1822 fprintf(out, "%*s", indent, "");
1823 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1824 bset->ref, bset->dim->nparam, bset->dim->n_out,
1825 bset->extra, bset->flags);
1826 dump((struct isl_basic_map *)bset, out, indent);
1829 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1830 FILE *out, int indent)
1833 fprintf(out, "null basic map\n");
1837 fprintf(out, "%*s", indent, "");
1838 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1839 "flags: %x, n_name: %d\n",
1841 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1842 bmap->extra, bmap->flags, bmap->dim->n_name);
1843 dump(bmap, out, indent);
1846 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1851 total = isl_basic_map_total_dim(bmap);
1852 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1853 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1854 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1855 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1859 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1861 struct isl_set *set;
1865 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1866 isl_assert(dim->ctx, n >= 0, goto error);
1867 set = isl_alloc(dim->ctx, struct isl_set,
1868 sizeof(struct isl_set) +
1869 (n - 1) * sizeof(struct isl_basic_set *));
1873 set->ctx = dim->ctx;
1874 isl_ctx_ref(set->ctx);
1886 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1887 unsigned nparam, unsigned dim, int n, unsigned flags)
1889 struct isl_set *set;
1890 struct isl_dim *dims;
1892 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1896 set = isl_set_alloc_dim(dims, n, flags);
1900 /* Make sure "map" has room for at least "n" more basic maps.
1902 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1905 struct isl_map *grown = NULL;
1909 isl_assert(map->ctx, n >= 0, goto error);
1910 if (map->n + n <= map->size)
1912 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1915 for (i = 0; i < map->n; ++i) {
1916 grown->p[i] = isl_basic_map_copy(map->p[i]);
1924 isl_map_free(grown);
1929 /* Make sure "set" has room for at least "n" more basic sets.
1931 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1933 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1936 struct isl_set *isl_set_dup(struct isl_set *set)
1939 struct isl_set *dup;
1944 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1947 for (i = 0; i < set->n; ++i)
1948 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1952 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1954 return isl_map_from_basic_map(bset);
1957 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1959 struct isl_map *map;
1964 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1965 return isl_map_add_basic_map(map, bmap);
1968 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1969 __isl_take isl_basic_set *bset)
1971 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1972 (struct isl_basic_map *)bset);
1975 void isl_set_free(struct isl_set *set)
1985 isl_ctx_deref(set->ctx);
1986 for (i = 0; i < set->n; ++i)
1987 isl_basic_set_free(set->p[i]);
1988 isl_dim_free(set->dim);
1992 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
1997 fprintf(out, "null set\n");
2001 fprintf(out, "%*s", indent, "");
2002 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2003 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2005 for (i = 0; i < set->n; ++i) {
2006 fprintf(out, "%*s", indent, "");
2007 fprintf(out, "basic set %d:\n", i);
2008 isl_basic_set_print_internal(set->p[i], out, indent+4);
2012 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2017 fprintf(out, "null map\n");
2021 fprintf(out, "%*s", indent, "");
2022 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2023 "flags: %x, n_name: %d\n",
2024 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2025 map->dim->n_out, map->flags, map->dim->n_name);
2026 for (i = 0; i < map->n; ++i) {
2027 fprintf(out, "%*s", indent, "");
2028 fprintf(out, "basic map %d:\n", i);
2029 isl_basic_map_print_internal(map->p[i], out, indent+4);
2033 struct isl_basic_map *isl_basic_map_intersect_domain(
2034 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2036 struct isl_basic_map *bmap_domain;
2041 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2042 bset->dim, isl_dim_param), goto error);
2044 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2045 isl_assert(bset->ctx,
2046 isl_basic_map_compatible_domain(bmap, bset), goto error);
2048 bmap = isl_basic_map_cow(bmap);
2051 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2052 bset->n_div, bset->n_eq, bset->n_ineq);
2053 bmap_domain = isl_basic_map_from_domain(bset);
2054 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2056 bmap = isl_basic_map_simplify(bmap);
2057 return isl_basic_map_finalize(bmap);
2059 isl_basic_map_free(bmap);
2060 isl_basic_set_free(bset);
2064 struct isl_basic_map *isl_basic_map_intersect_range(
2065 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2067 struct isl_basic_map *bmap_range;
2072 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2073 bset->dim, isl_dim_param), goto error);
2075 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2076 isl_assert(bset->ctx,
2077 isl_basic_map_compatible_range(bmap, bset), goto error);
2079 if (isl_basic_set_is_universe(bset)) {
2080 isl_basic_set_free(bset);
2084 bmap = isl_basic_map_cow(bmap);
2087 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2088 bset->n_div, bset->n_eq, bset->n_ineq);
2089 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2090 bmap = add_constraints(bmap, bmap_range, 0, 0);
2092 bmap = isl_basic_map_simplify(bmap);
2093 return isl_basic_map_finalize(bmap);
2095 isl_basic_map_free(bmap);
2096 isl_basic_set_free(bset);
2100 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2106 total = 1 + isl_basic_map_total_dim(bmap);
2107 if (total != vec->size)
2112 for (i = 0; i < bmap->n_eq; ++i) {
2113 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2114 if (!isl_int_is_zero(s)) {
2120 for (i = 0; i < bmap->n_ineq; ++i) {
2121 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2122 if (isl_int_is_neg(s)) {
2133 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2135 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2138 struct isl_basic_map *isl_basic_map_intersect(
2139 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2141 struct isl_vec *sample = NULL;
2143 if (!bmap1 || !bmap2)
2146 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2147 bmap2->dim, isl_dim_param), goto error);
2148 if (isl_dim_total(bmap1->dim) ==
2149 isl_dim_size(bmap1->dim, isl_dim_param) &&
2150 isl_dim_total(bmap2->dim) !=
2151 isl_dim_size(bmap2->dim, isl_dim_param))
2152 return isl_basic_map_intersect(bmap2, bmap1);
2154 if (isl_dim_total(bmap2->dim) !=
2155 isl_dim_size(bmap2->dim, isl_dim_param))
2156 isl_assert(bmap1->ctx,
2157 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2159 if (bmap1->sample &&
2160 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2161 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2162 sample = isl_vec_copy(bmap1->sample);
2163 else if (bmap2->sample &&
2164 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2165 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2166 sample = isl_vec_copy(bmap2->sample);
2168 bmap1 = isl_basic_map_cow(bmap1);
2171 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2172 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2173 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2176 isl_vec_free(sample);
2178 isl_vec_free(bmap1->sample);
2179 bmap1->sample = sample;
2182 bmap1 = isl_basic_map_simplify(bmap1);
2183 return isl_basic_map_finalize(bmap1);
2186 isl_vec_free(sample);
2187 isl_basic_map_free(bmap1);
2188 isl_basic_map_free(bmap2);
2192 struct isl_basic_set *isl_basic_set_intersect(
2193 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2195 return (struct isl_basic_set *)
2196 isl_basic_map_intersect(
2197 (struct isl_basic_map *)bset1,
2198 (struct isl_basic_map *)bset2);
2201 /* Special case of isl_map_intersect, where both map1 and map2
2202 * are convex, without any divs and such that either map1 or map2
2203 * contains a single constraint. This constraint is then simply
2204 * added to the other map.
2206 static __isl_give isl_map *map_intersect_add_constraint(
2207 __isl_take isl_map *map1, __isl_take isl_map *map2)
2209 isl_assert(map1->ctx, map1->n == 1, goto error);
2210 isl_assert(map2->ctx, map1->n == 1, goto error);
2211 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2212 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2214 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2215 return isl_map_intersect(map2, map1);
2217 isl_assert(map2->ctx,
2218 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2220 map1 = isl_map_cow(map1);
2223 if (isl_map_plain_is_empty(map1)) {
2227 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2228 if (map2->p[0]->n_eq == 1)
2229 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2231 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2232 map2->p[0]->ineq[0]);
2234 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2235 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2239 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2240 isl_basic_map_free(map1->p[0]);
2253 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2256 struct isl_map *result;
2262 if (isl_map_plain_is_empty(map1)) {
2266 if (isl_map_plain_is_empty(map2)) {
2271 if (map1->n == 1 && map2->n == 1 &&
2272 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2273 isl_dim_equal(map1->dim, map2->dim) &&
2274 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2275 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2276 return map_intersect_add_constraint(map1, map2);
2277 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2278 map2->dim, isl_dim_param), goto error);
2279 if (isl_dim_total(map1->dim) ==
2280 isl_dim_size(map1->dim, isl_dim_param) &&
2281 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2282 return isl_map_intersect(map2, map1);
2284 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2285 isl_assert(map1->ctx,
2286 isl_dim_equal(map1->dim, map2->dim), goto error);
2288 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2289 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2290 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2292 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2293 map1->n * map2->n, flags);
2296 for (i = 0; i < map1->n; ++i)
2297 for (j = 0; j < map2->n; ++j) {
2298 struct isl_basic_map *part;
2299 part = isl_basic_map_intersect(
2300 isl_basic_map_copy(map1->p[i]),
2301 isl_basic_map_copy(map2->p[j]));
2302 if (isl_basic_map_is_empty(part))
2303 isl_basic_map_free(part);
2305 result = isl_map_add_basic_map(result, part);
2318 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2320 return (struct isl_set *)
2321 isl_map_intersect((struct isl_map *)set1,
2322 (struct isl_map *)set2);
2325 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2327 struct isl_dim *dim;
2328 struct isl_basic_set *bset;
2333 bmap = isl_basic_map_cow(bmap);
2336 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2337 in = isl_basic_map_n_in(bmap);
2338 bset = isl_basic_set_from_basic_map(bmap);
2339 bset = isl_basic_set_swap_vars(bset, in);
2340 return isl_basic_map_from_basic_set(bset, dim);
2343 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2344 enum isl_dim_type type, unsigned pos, unsigned n)
2346 struct isl_dim *res_dim;
2347 struct isl_basic_map *res;
2348 struct isl_dim_map *dim_map;
2349 unsigned total, off;
2350 enum isl_dim_type t;
2358 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2360 total = isl_basic_map_total_dim(bmap) + n;
2361 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2363 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2365 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2367 unsigned size = isl_basic_map_dim(bmap, t);
2368 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2370 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2371 pos, size - pos, off + pos + n);
2373 off += isl_dim_size(res_dim, t);
2375 isl_dim_map_div(dim_map, bmap, off);
2377 res = isl_basic_map_alloc_dim(res_dim,
2378 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2379 if (isl_basic_map_is_rational(bmap))
2380 res = isl_basic_map_set_rational(res);
2381 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2382 return isl_basic_map_finalize(res);
2385 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2386 enum isl_dim_type type, unsigned n)
2390 return isl_basic_map_insert(bmap, type,
2391 isl_basic_map_dim(bmap, type), n);
2394 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2395 enum isl_dim_type type, unsigned n)
2399 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2400 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2402 isl_basic_set_free(bset);
2406 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2407 enum isl_dim_type type, unsigned pos, unsigned n)
2414 map = isl_map_cow(map);
2418 map->dim = isl_dim_insert(map->dim, type, pos, n);
2422 for (i = 0; i < map->n; ++i) {
2423 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2434 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2435 enum isl_dim_type type, unsigned pos, unsigned n)
2437 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2440 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2441 enum isl_dim_type type, unsigned n)
2445 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2448 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2449 enum isl_dim_type type, unsigned n)
2453 isl_assert(set->ctx, type != isl_dim_in, goto error);
2454 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2460 __isl_give isl_basic_map *isl_basic_map_move_dims(
2461 __isl_take isl_basic_map *bmap,
2462 enum isl_dim_type dst_type, unsigned dst_pos,
2463 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2465 struct isl_dim_map *dim_map;
2466 struct isl_basic_map *res;
2467 enum isl_dim_type t;
2468 unsigned total, off;
2475 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2478 if (dst_type == src_type && dst_pos == src_pos)
2481 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2483 if (pos(bmap->dim, dst_type) + dst_pos ==
2484 pos(bmap->dim, src_type) + src_pos +
2485 ((src_type < dst_type) ? n : 0)) {
2486 bmap = isl_basic_map_cow(bmap);
2490 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2491 src_type, src_pos, n);
2495 bmap = isl_basic_map_finalize(bmap);
2500 total = isl_basic_map_total_dim(bmap);
2501 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2504 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2505 unsigned size = isl_dim_size(bmap->dim, t);
2506 if (t == dst_type) {
2507 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2510 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2513 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2514 dst_pos, size - dst_pos, off);
2515 off += size - dst_pos;
2516 } else if (t == src_type) {
2517 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2520 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2521 src_pos + n, size - src_pos - n, off);
2522 off += size - src_pos - n;
2524 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2528 isl_dim_map_div(dim_map, bmap, off);
2530 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2531 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2532 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2534 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2535 src_type, src_pos, n);
2539 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2540 bmap = isl_basic_map_gauss(bmap, NULL);
2541 bmap = isl_basic_map_finalize(bmap);
2545 isl_basic_map_free(bmap);
2549 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2550 enum isl_dim_type dst_type, unsigned dst_pos,
2551 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2553 return (isl_basic_set *)isl_basic_map_move_dims(
2554 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2557 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2558 enum isl_dim_type dst_type, unsigned dst_pos,
2559 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2563 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2564 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2565 src_type, src_pos, n);
2571 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2572 enum isl_dim_type dst_type, unsigned dst_pos,
2573 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2582 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2585 if (dst_type == src_type && dst_pos == src_pos)
2588 isl_assert(map->ctx, dst_type != src_type, goto error);
2590 map = isl_map_cow(map);
2594 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2598 for (i = 0; i < map->n; ++i) {
2599 map->p[i] = isl_basic_map_move_dims(map->p[i],
2601 src_type, src_pos, n);
2612 /* Move the specified dimensions to the last columns right before
2613 * the divs. Don't change the dimension specification of bmap.
2614 * That's the responsibility of the caller.
2616 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2617 enum isl_dim_type type, unsigned first, unsigned n)
2619 struct isl_dim_map *dim_map;
2620 struct isl_basic_map *res;
2621 enum isl_dim_type t;
2622 unsigned total, off;
2626 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2629 total = isl_basic_map_total_dim(bmap);
2630 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2633 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2634 unsigned size = isl_dim_size(bmap->dim, t);
2636 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2639 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2640 first, n, total - bmap->n_div - n);
2641 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2642 first + n, size - (first + n), off);
2643 off += size - (first + n);
2645 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2649 isl_dim_map_div(dim_map, bmap, off + n);
2651 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2652 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2653 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2657 /* Turn the n dimensions of type type, starting at first
2658 * into existentially quantified variables.
2660 __isl_give isl_basic_map *isl_basic_map_project_out(
2661 __isl_take isl_basic_map *bmap,
2662 enum isl_dim_type type, unsigned first, unsigned n)
2675 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2676 return isl_basic_map_remove_dims(bmap, type, first, n);
2678 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2681 bmap = move_last(bmap, type, first, n);
2682 bmap = isl_basic_map_cow(bmap);
2686 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2687 old = bmap->block2.data;
2688 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2689 (bmap->extra + n) * (1 + row_size));
2690 if (!bmap->block2.data)
2692 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2695 for (i = 0; i < n; ++i) {
2696 new_div[i] = bmap->block2.data +
2697 (bmap->extra + i) * (1 + row_size);
2698 isl_seq_clr(new_div[i], 1 + row_size);
2700 for (i = 0; i < bmap->extra; ++i)
2701 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2703 bmap->div = new_div;
2707 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2710 bmap = isl_basic_map_simplify(bmap);
2711 bmap = isl_basic_map_drop_redundant_divs(bmap);
2712 return isl_basic_map_finalize(bmap);
2714 isl_basic_map_free(bmap);
2718 /* Turn the n dimensions of type type, starting at first
2719 * into existentially quantified variables.
2721 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2722 enum isl_dim_type type, unsigned first, unsigned n)
2724 return (isl_basic_set *)isl_basic_map_project_out(
2725 (isl_basic_map *)bset, type, first, n);
2728 /* Turn the n dimensions of type type, starting at first
2729 * into existentially quantified variables.
2731 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2732 enum isl_dim_type type, unsigned first, unsigned n)
2742 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2744 map = isl_map_cow(map);
2748 map->dim = isl_dim_drop(map->dim, type, first, n);
2752 for (i = 0; i < map->n; ++i) {
2753 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2764 /* Turn the n dimensions of type type, starting at first
2765 * into existentially quantified variables.
2767 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2768 enum isl_dim_type type, unsigned first, unsigned n)
2770 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2773 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2777 for (i = 0; i < n; ++i) {
2778 j = isl_basic_map_alloc_div(bmap);
2781 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2785 isl_basic_map_free(bmap);
2789 struct isl_basic_map *isl_basic_map_apply_range(
2790 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2792 struct isl_dim *dim_result = NULL;
2793 struct isl_basic_map *bmap;
2794 unsigned n_in, n_out, n, nparam, total, pos;
2795 struct isl_dim_map *dim_map1, *dim_map2;
2797 if (!bmap1 || !bmap2)
2800 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2801 isl_dim_copy(bmap2->dim));
2803 n_in = isl_basic_map_n_in(bmap1);
2804 n_out = isl_basic_map_n_out(bmap2);
2805 n = isl_basic_map_n_out(bmap1);
2806 nparam = isl_basic_map_n_param(bmap1);
2808 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2809 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2810 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2811 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2812 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2813 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2814 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2815 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2816 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2817 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2818 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2820 bmap = isl_basic_map_alloc_dim(dim_result,
2821 bmap1->n_div + bmap2->n_div + n,
2822 bmap1->n_eq + bmap2->n_eq,
2823 bmap1->n_ineq + bmap2->n_ineq);
2824 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2825 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2826 bmap = add_divs(bmap, n);
2827 bmap = isl_basic_map_simplify(bmap);
2828 bmap = isl_basic_map_drop_redundant_divs(bmap);
2829 return isl_basic_map_finalize(bmap);
2831 isl_basic_map_free(bmap1);
2832 isl_basic_map_free(bmap2);
2836 struct isl_basic_set *isl_basic_set_apply(
2837 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2842 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2845 return (struct isl_basic_set *)
2846 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2848 isl_basic_set_free(bset);
2849 isl_basic_map_free(bmap);
2853 struct isl_basic_map *isl_basic_map_apply_domain(
2854 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2856 if (!bmap1 || !bmap2)
2859 isl_assert(bmap1->ctx,
2860 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2861 isl_assert(bmap1->ctx,
2862 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2865 bmap1 = isl_basic_map_reverse(bmap1);
2866 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2867 return isl_basic_map_reverse(bmap1);
2869 isl_basic_map_free(bmap1);
2870 isl_basic_map_free(bmap2);
2874 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2875 * A \cap B -> f(A) + f(B)
2877 struct isl_basic_map *isl_basic_map_sum(
2878 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2880 unsigned n_in, n_out, nparam, total, pos;
2881 struct isl_basic_map *bmap = NULL;
2882 struct isl_dim_map *dim_map1, *dim_map2;
2885 if (!bmap1 || !bmap2)
2888 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2891 nparam = isl_basic_map_n_param(bmap1);
2892 n_in = isl_basic_map_n_in(bmap1);
2893 n_out = isl_basic_map_n_out(bmap1);
2895 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2896 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2897 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2898 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2899 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2900 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2901 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2902 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2903 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2904 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2905 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2907 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2908 bmap1->n_div + bmap2->n_div + 2 * n_out,
2909 bmap1->n_eq + bmap2->n_eq + n_out,
2910 bmap1->n_ineq + bmap2->n_ineq);
2911 for (i = 0; i < n_out; ++i) {
2912 int j = isl_basic_map_alloc_equality(bmap);
2915 isl_seq_clr(bmap->eq[j], 1+total);
2916 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2917 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2918 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2920 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
2921 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
2922 bmap = add_divs(bmap, 2 * n_out);
2924 bmap = isl_basic_map_simplify(bmap);
2925 return isl_basic_map_finalize(bmap);
2927 isl_basic_map_free(bmap);
2928 isl_basic_map_free(bmap1);
2929 isl_basic_map_free(bmap2);
2933 /* Given two maps A -> f(A) and B -> g(B), construct a map
2934 * A \cap B -> f(A) + f(B)
2936 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2938 struct isl_map *result;
2944 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2946 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2947 map1->n * map2->n, 0);
2950 for (i = 0; i < map1->n; ++i)
2951 for (j = 0; j < map2->n; ++j) {
2952 struct isl_basic_map *part;
2953 part = isl_basic_map_sum(
2954 isl_basic_map_copy(map1->p[i]),
2955 isl_basic_map_copy(map2->p[j]));
2956 if (isl_basic_map_is_empty(part))
2957 isl_basic_map_free(part);
2959 result = isl_map_add_basic_map(result, part);
2972 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2973 __isl_take isl_set *set2)
2975 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2978 /* Given a basic map A -> f(A), construct A -> -f(A).
2980 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2985 bmap = isl_basic_map_cow(bmap);
2989 n = isl_basic_map_dim(bmap, isl_dim_out);
2990 off = isl_basic_map_offset(bmap, isl_dim_out);
2991 for (i = 0; i < bmap->n_eq; ++i)
2992 for (j = 0; j < n; ++j)
2993 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2994 for (i = 0; i < bmap->n_ineq; ++i)
2995 for (j = 0; j < n; ++j)
2996 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2997 for (i = 0; i < bmap->n_div; ++i)
2998 for (j = 0; j < n; ++j)
2999 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3000 return isl_basic_map_finalize(bmap);
3003 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3005 return isl_basic_map_neg(bset);
3008 /* Given a map A -> f(A), construct A -> -f(A).
3010 struct isl_map *isl_map_neg(struct isl_map *map)
3014 map = isl_map_cow(map);
3018 for (i = 0; i < map->n; ++i) {
3019 map->p[i] = isl_basic_map_neg(map->p[i]);
3030 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3032 return (isl_set *)isl_map_neg((isl_map *)set);
3035 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3036 * A -> floor(f(A)/d).
3038 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3041 unsigned n_in, n_out, nparam, total, pos;
3042 struct isl_basic_map *result = NULL;
3043 struct isl_dim_map *dim_map;
3049 nparam = isl_basic_map_n_param(bmap);
3050 n_in = isl_basic_map_n_in(bmap);
3051 n_out = isl_basic_map_n_out(bmap);
3053 total = nparam + n_in + n_out + bmap->n_div + n_out;
3054 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3055 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3056 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3057 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3058 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3060 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3061 bmap->n_div + n_out,
3062 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3063 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3064 result = add_divs(result, n_out);
3065 for (i = 0; i < n_out; ++i) {
3067 j = isl_basic_map_alloc_inequality(result);
3070 isl_seq_clr(result->ineq[j], 1+total);
3071 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3072 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3073 j = isl_basic_map_alloc_inequality(result);
3076 isl_seq_clr(result->ineq[j], 1+total);
3077 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3078 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3079 isl_int_sub_ui(result->ineq[j][0], d, 1);
3082 result = isl_basic_map_simplify(result);
3083 return isl_basic_map_finalize(result);
3085 isl_basic_map_free(result);
3089 /* Given a map A -> f(A) and an integer d, construct a map
3090 * A -> floor(f(A)/d).
3092 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3096 map = isl_map_cow(map);
3100 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3101 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3102 for (i = 0; i < map->n; ++i) {
3103 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3114 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3120 i = isl_basic_map_alloc_equality(bmap);
3123 nparam = isl_basic_map_n_param(bmap);
3124 n_in = isl_basic_map_n_in(bmap);
3125 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3126 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3127 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3128 return isl_basic_map_finalize(bmap);
3130 isl_basic_map_free(bmap);
3134 /* Add a constraints to "bmap" expressing i_pos < o_pos
3136 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3142 i = isl_basic_map_alloc_inequality(bmap);
3145 nparam = isl_basic_map_n_param(bmap);
3146 n_in = isl_basic_map_n_in(bmap);
3147 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3148 isl_int_set_si(bmap->ineq[i][0], -1);
3149 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3150 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3151 return isl_basic_map_finalize(bmap);
3153 isl_basic_map_free(bmap);
3157 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3159 static __isl_give isl_basic_map *var_less_or_equal(
3160 __isl_take isl_basic_map *bmap, unsigned pos)
3166 i = isl_basic_map_alloc_inequality(bmap);
3169 nparam = isl_basic_map_n_param(bmap);
3170 n_in = isl_basic_map_n_in(bmap);
3171 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3172 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3173 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3174 return isl_basic_map_finalize(bmap);
3176 isl_basic_map_free(bmap);
3180 /* Add a constraints to "bmap" expressing i_pos > o_pos
3182 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3188 i = isl_basic_map_alloc_inequality(bmap);
3191 nparam = isl_basic_map_n_param(bmap);
3192 n_in = isl_basic_map_n_in(bmap);
3193 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3194 isl_int_set_si(bmap->ineq[i][0], -1);
3195 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3196 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3197 return isl_basic_map_finalize(bmap);
3199 isl_basic_map_free(bmap);
3203 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3205 static __isl_give isl_basic_map *var_more_or_equal(
3206 __isl_take isl_basic_map *bmap, unsigned pos)
3212 i = isl_basic_map_alloc_inequality(bmap);
3215 nparam = isl_basic_map_n_param(bmap);
3216 n_in = isl_basic_map_n_in(bmap);
3217 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3218 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3219 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3220 return isl_basic_map_finalize(bmap);
3222 isl_basic_map_free(bmap);
3226 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3229 struct isl_basic_map *bmap;
3230 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3233 for (i = 0; i < n_equal && bmap; ++i)
3234 bmap = var_equal(bmap, i);
3235 return isl_basic_map_finalize(bmap);
3238 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3240 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3243 struct isl_basic_map *bmap;
3244 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3247 for (i = 0; i < pos && bmap; ++i)
3248 bmap = var_equal(bmap, i);
3250 bmap = var_less(bmap, pos);
3251 return isl_basic_map_finalize(bmap);
3254 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3256 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3257 __isl_take isl_dim *dim, unsigned pos)
3260 isl_basic_map *bmap;
3262 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3263 for (i = 0; i < pos; ++i)
3264 bmap = var_equal(bmap, i);
3265 bmap = var_less_or_equal(bmap, pos);
3266 return isl_basic_map_finalize(bmap);
3269 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3271 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3274 struct isl_basic_map *bmap;
3275 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3278 for (i = 0; i < pos && bmap; ++i)
3279 bmap = var_equal(bmap, i);
3281 bmap = var_more(bmap, pos);
3282 return isl_basic_map_finalize(bmap);
3285 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3287 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3288 __isl_take isl_dim *dim, unsigned pos)
3291 isl_basic_map *bmap;
3293 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3294 for (i = 0; i < pos; ++i)
3295 bmap = var_equal(bmap, i);
3296 bmap = var_more_or_equal(bmap, pos);
3297 return isl_basic_map_finalize(bmap);
3300 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3301 unsigned n, int equal)
3303 struct isl_map *map;
3306 if (n == 0 && equal)
3307 return isl_map_universe(dims);
3309 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3311 for (i = 0; i + 1 < n; ++i)
3312 map = isl_map_add_basic_map(map,
3313 isl_basic_map_less_at(isl_dim_copy(dims), i));
3316 map = isl_map_add_basic_map(map,
3317 isl_basic_map_less_or_equal_at(dims, n - 1));
3319 map = isl_map_add_basic_map(map,
3320 isl_basic_map_less_at(dims, n - 1));
3327 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3331 return map_lex_lte_first(dims, dims->n_out, equal);
3334 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3336 return map_lex_lte_first(dim, n, 0);
3339 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3341 return map_lex_lte_first(dim, n, 1);
3344 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3346 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3349 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3351 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3354 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3355 unsigned n, int equal)
3357 struct isl_map *map;
3360 if (n == 0 && equal)
3361 return isl_map_universe(dims);
3363 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3365 for (i = 0; i + 1 < n; ++i)
3366 map = isl_map_add_basic_map(map,
3367 isl_basic_map_more_at(isl_dim_copy(dims), i));
3370 map = isl_map_add_basic_map(map,
3371 isl_basic_map_more_or_equal_at(dims, n - 1));
3373 map = isl_map_add_basic_map(map,
3374 isl_basic_map_more_at(dims, n - 1));
3381 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3385 return map_lex_gte_first(dims, dims->n_out, equal);
3388 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3390 return map_lex_gte_first(dim, n, 0);
3393 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3395 return map_lex_gte_first(dim, n, 1);
3398 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3400 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3403 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3405 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3408 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3409 __isl_take isl_set *set2)
3412 map = isl_map_lex_le(isl_set_get_dim(set1));
3413 map = isl_map_intersect_domain(map, set1);
3414 map = isl_map_intersect_range(map, set2);
3418 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3419 __isl_take isl_set *set2)
3422 map = isl_map_lex_lt(isl_set_get_dim(set1));
3423 map = isl_map_intersect_domain(map, set1);
3424 map = isl_map_intersect_range(map, set2);
3428 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3429 __isl_take isl_set *set2)
3432 map = isl_map_lex_ge(isl_set_get_dim(set1));
3433 map = isl_map_intersect_domain(map, set1);
3434 map = isl_map_intersect_range(map, set2);
3438 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3439 __isl_take isl_set *set2)
3442 map = isl_map_lex_gt(isl_set_get_dim(set1));
3443 map = isl_map_intersect_domain(map, set1);
3444 map = isl_map_intersect_range(map, set2);
3448 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3449 __isl_take isl_map *map2)
3452 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3453 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3454 map = isl_map_apply_range(map, isl_map_reverse(map2));
3458 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3459 __isl_take isl_map *map2)
3462 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3463 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3464 map = isl_map_apply_range(map, isl_map_reverse(map2));
3468 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3469 __isl_take isl_map *map2)
3472 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3473 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3474 map = isl_map_apply_range(map, isl_map_reverse(map2));
3478 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3479 __isl_take isl_map *map2)
3482 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3483 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3484 map = isl_map_apply_range(map, isl_map_reverse(map2));
3488 struct isl_basic_map *isl_basic_map_from_basic_set(
3489 struct isl_basic_set *bset, struct isl_dim *dim)
3491 struct isl_basic_map *bmap;
3493 bset = isl_basic_set_cow(bset);
3497 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3498 isl_dim_free(bset->dim);
3499 bmap = (struct isl_basic_map *) bset;
3501 return isl_basic_map_finalize(bmap);
3503 isl_basic_set_free(bset);
3508 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3512 if (bmap->dim->n_in == 0)
3513 return (struct isl_basic_set *)bmap;
3514 bmap = isl_basic_map_cow(bmap);
3517 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3520 bmap = isl_basic_map_finalize(bmap);
3521 return (struct isl_basic_set *)bmap;
3523 isl_basic_map_free(bmap);
3527 /* For a div d = floor(f/m), add the constraints
3530 * -(f-(n-1)) + m d >= 0
3532 * Note that the second constraint is the negation of
3536 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3537 unsigned pos, isl_int *div)
3540 unsigned total = isl_basic_map_total_dim(bmap);
3542 i = isl_basic_map_alloc_inequality(bmap);
3545 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3546 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3548 j = isl_basic_map_alloc_inequality(bmap);
3551 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3552 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3553 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3557 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3558 unsigned pos, isl_int *div)
3560 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3564 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3566 unsigned total = isl_basic_map_total_dim(bmap);
3567 unsigned div_pos = total - bmap->n_div + div;
3569 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3573 struct isl_basic_set *isl_basic_map_underlying_set(
3574 struct isl_basic_map *bmap)
3578 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3580 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3581 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3582 return (struct isl_basic_set *)bmap;
3583 bmap = isl_basic_map_cow(bmap);
3586 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3589 bmap->extra -= bmap->n_div;
3591 bmap = isl_basic_map_finalize(bmap);
3592 return (struct isl_basic_set *)bmap;
3597 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3598 __isl_take isl_basic_set *bset)
3600 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3603 struct isl_basic_map *isl_basic_map_overlying_set(
3604 struct isl_basic_set *bset, struct isl_basic_map *like)
3606 struct isl_basic_map *bmap;
3607 struct isl_ctx *ctx;
3614 isl_assert(ctx, bset->n_div == 0, goto error);
3615 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3616 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3618 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3619 isl_basic_map_free(like);
3620 return (struct isl_basic_map *)bset;
3622 bset = isl_basic_set_cow(bset);
3625 total = bset->dim->n_out + bset->extra;
3626 bmap = (struct isl_basic_map *)bset;
3627 isl_dim_free(bmap->dim);
3628 bmap->dim = isl_dim_copy(like->dim);
3631 bmap->n_div = like->n_div;
3632 bmap->extra += like->n_div;
3636 ltotal = total - bmap->extra + like->extra;
3639 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3640 bmap->extra * (1 + 1 + total));
3641 if (isl_blk_is_error(bmap->block2))
3643 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3647 for (i = 0; i < bmap->extra; ++i)
3648 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3649 for (i = 0; i < like->n_div; ++i) {
3650 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3651 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3653 bmap = isl_basic_map_extend_constraints(bmap,
3654 0, 2 * like->n_div);
3655 for (i = 0; i < like->n_div; ++i) {
3656 if (isl_int_is_zero(bmap->div[i][0]))
3658 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3662 isl_basic_map_free(like);
3663 bmap = isl_basic_map_simplify(bmap);
3664 bmap = isl_basic_map_finalize(bmap);
3667 isl_basic_map_free(like);
3668 isl_basic_set_free(bset);
3672 struct isl_basic_set *isl_basic_set_from_underlying_set(
3673 struct isl_basic_set *bset, struct isl_basic_set *like)
3675 return (struct isl_basic_set *)
3676 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3679 struct isl_set *isl_set_from_underlying_set(
3680 struct isl_set *set, struct isl_basic_set *like)
3686 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3688 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3689 isl_basic_set_free(like);
3692 set = isl_set_cow(set);
3695 for (i = 0; i < set->n; ++i) {
3696 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3697 isl_basic_set_copy(like));
3701 isl_dim_free(set->dim);
3702 set->dim = isl_dim_copy(like->dim);
3705 isl_basic_set_free(like);
3708 isl_basic_set_free(like);
3713 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3717 map = isl_map_cow(map);
3720 map->dim = isl_dim_cow(map->dim);
3724 for (i = 1; i < map->n; ++i)
3725 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3727 for (i = 0; i < map->n; ++i) {
3728 map->p[i] = (struct isl_basic_map *)
3729 isl_basic_map_underlying_set(map->p[i]);
3734 map->dim = isl_dim_underlying(map->dim, 0);
3736 isl_dim_free(map->dim);
3737 map->dim = isl_dim_copy(map->p[0]->dim);
3741 return (struct isl_set *)map;
3747 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3749 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3752 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3753 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3755 bmap = isl_basic_map_cow(bmap);
3759 isl_dim_free(bmap->dim);
3762 bmap = isl_basic_map_finalize(bmap);
3766 isl_basic_map_free(bmap);
3771 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3772 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3774 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3778 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3779 __isl_take isl_dim *dim)
3783 map = isl_map_cow(map);
3787 for (i = 0; i < map->n; ++i) {
3788 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3793 isl_dim_free(map->dim);
3803 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3804 __isl_take isl_dim *dim)
3806 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3809 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3812 struct isl_basic_set *domain;
3818 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3820 n_in = isl_basic_map_n_in(bmap);
3821 n_out = isl_basic_map_n_out(bmap);
3822 domain = isl_basic_set_from_basic_map(bmap);
3823 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3825 domain = isl_basic_set_reset_dim(domain, dim);
3830 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
3834 return isl_dim_may_be_set(bmap->dim);
3837 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3841 if (isl_basic_map_may_be_set(bmap))
3843 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3846 __isl_give isl_basic_map *isl_basic_map_domain_map(
3847 __isl_take isl_basic_map *bmap)
3851 isl_basic_map *domain;
3852 int nparam, n_in, n_out;
3855 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3856 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3857 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3859 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3860 domain = isl_basic_map_universe(dim);
3862 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3863 bmap = isl_basic_map_apply_range(bmap, domain);
3864 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3866 total = isl_basic_map_total_dim(bmap);
3868 for (i = 0; i < n_in; ++i) {
3869 k = isl_basic_map_alloc_equality(bmap);
3872 isl_seq_clr(bmap->eq[k], 1 + total);
3873 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3874 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3877 bmap = isl_basic_map_gauss(bmap, NULL);
3878 return isl_basic_map_finalize(bmap);
3880 isl_basic_map_free(bmap);
3884 __isl_give isl_basic_map *isl_basic_map_range_map(
3885 __isl_take isl_basic_map *bmap)
3889 isl_basic_map *range;
3890 int nparam, n_in, n_out;
3893 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3894 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3895 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3897 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3898 range = isl_basic_map_universe(dim);
3900 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3901 bmap = isl_basic_map_apply_range(bmap, range);
3902 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3904 total = isl_basic_map_total_dim(bmap);
3906 for (i = 0; i < n_out; ++i) {
3907 k = isl_basic_map_alloc_equality(bmap);
3910 isl_seq_clr(bmap->eq[k], 1 + total);
3911 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3912 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3915 bmap = isl_basic_map_gauss(bmap, NULL);
3916 return isl_basic_map_finalize(bmap);
3918 isl_basic_map_free(bmap);
3922 int isl_map_may_be_set(__isl_keep isl_map *map)
3926 return isl_dim_may_be_set(map->dim);
3929 struct isl_set *isl_map_range(struct isl_map *map)
3932 struct isl_set *set;
3936 if (isl_map_may_be_set(map))
3937 return (isl_set *)map;
3939 map = isl_map_cow(map);
3943 set = (struct isl_set *) map;
3944 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3947 for (i = 0; i < map->n; ++i) {
3948 set->p[i] = isl_basic_map_range(map->p[i]);
3952 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3953 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3960 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3963 isl_dim *domain_dim;
3965 map = isl_map_cow(map);
3969 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3970 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3971 map->dim = isl_dim_join(map->dim, domain_dim);
3974 for (i = 0; i < map->n; ++i) {
3975 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3979 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3980 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3987 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3992 map = isl_map_cow(map);
3996 range_dim = isl_dim_range(isl_map_get_dim(map));
3997 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3998 map->dim = isl_dim_join(map->dim, range_dim);
4001 for (i = 0; i < map->n; ++i) {
4002 map->p[i] = isl_basic_map_range_map(map->p[i]);
4006 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4007 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4014 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4017 struct isl_map *map = NULL;
4019 set = isl_set_cow(set);
4022 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4023 map = (struct isl_map *)set;
4024 for (i = 0; i < set->n; ++i) {
4025 map->p[i] = isl_basic_map_from_basic_set(
4026 set->p[i], isl_dim_copy(dim));
4030 isl_dim_free(map->dim);
4039 __isl_give isl_basic_map *isl_basic_map_from_domain(
4040 __isl_take isl_basic_set *bset)
4042 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4045 __isl_give isl_basic_map *isl_basic_map_from_range(
4046 __isl_take isl_basic_set *bset)
4048 return (isl_basic_map *)bset;
4051 struct isl_map *isl_map_from_range(struct isl_set *set)
4053 return (struct isl_map *)set;
4056 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4058 return isl_map_reverse(isl_map_from_range(set));
4061 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4062 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4064 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4065 isl_basic_map_from_range(range));
4068 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4069 __isl_take isl_set *range)
4071 return isl_map_apply_range(isl_map_from_domain(domain),
4072 isl_map_from_range(range));
4075 struct isl_set *isl_set_from_map(struct isl_map *map)
4078 struct isl_set *set = NULL;
4082 map = isl_map_cow(map);
4085 map->dim = isl_dim_as_set_dim(map->dim);
4088 set = (struct isl_set *)map;
4089 for (i = 0; i < map->n; ++i) {
4090 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4100 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4102 struct isl_map *map;
4106 isl_assert(dim->ctx, n >= 0, return NULL);
4107 map = isl_alloc(dim->ctx, struct isl_map,
4108 sizeof(struct isl_map) +
4109 (n - 1) * sizeof(struct isl_basic_map *));
4113 map->ctx = dim->ctx;
4114 isl_ctx_ref(map->ctx);
4126 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4127 unsigned nparam, unsigned in, unsigned out, int n,
4130 struct isl_map *map;
4131 struct isl_dim *dims;
4133 dims = isl_dim_alloc(ctx, nparam, in, out);
4137 map = isl_map_alloc_dim(dims, n, flags);
4141 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4143 struct isl_basic_map *bmap;
4144 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4145 bmap = isl_basic_map_set_to_empty(bmap);
4149 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4151 struct isl_basic_set *bset;
4152 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4153 bset = isl_basic_set_set_to_empty(bset);
4157 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4159 struct isl_basic_map *bmap;
4162 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4163 bmap = isl_basic_map_set_to_empty(bmap);
4167 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4169 struct isl_basic_map *bmap;
4172 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4173 bmap = isl_basic_map_set_to_empty(bmap);
4177 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4179 struct isl_basic_set *bset;
4182 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4183 bset = isl_basic_set_set_to_empty(bset);
4187 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4189 struct isl_basic_map *bmap;
4190 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4191 bmap = isl_basic_map_finalize(bmap);
4195 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4197 struct isl_basic_set *bset;
4198 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4199 bset = isl_basic_set_finalize(bset);
4203 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4206 unsigned total = isl_dim_total(dim);
4207 isl_basic_map *bmap;
4209 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4210 for (i = 0; i < total; ++i) {
4211 int k = isl_basic_map_alloc_inequality(bmap);
4214 isl_seq_clr(bmap->ineq[k], 1 + total);
4215 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4219 isl_basic_map_free(bmap);
4223 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4225 return isl_basic_map_nat_universe(dim);
4228 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4230 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4233 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4235 return isl_map_nat_universe(dim);
4238 __isl_give isl_basic_map *isl_basic_map_universe_like(
4239 __isl_keep isl_basic_map *model)
4243 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4246 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4250 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4253 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4254 __isl_keep isl_set *model)
4258 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4261 struct isl_map *isl_map_empty(struct isl_dim *dim)
4263 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4266 struct isl_map *isl_map_empty_like(struct isl_map *model)
4270 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4273 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4277 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4280 struct isl_set *isl_set_empty(struct isl_dim *dim)
4282 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4285 struct isl_set *isl_set_empty_like(struct isl_set *model)
4289 return isl_set_empty(isl_dim_copy(model->dim));
4292 struct isl_map *isl_map_universe(struct isl_dim *dim)
4294 struct isl_map *map;
4297 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4298 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4302 struct isl_set *isl_set_universe(struct isl_dim *dim)
4304 struct isl_set *set;
4307 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4308 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4312 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4316 return isl_set_universe(isl_dim_copy(model->dim));
4319 struct isl_map *isl_map_dup(struct isl_map *map)
4322 struct isl_map *dup;
4326 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4327 for (i = 0; i < map->n; ++i)
4328 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4332 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4333 __isl_take isl_basic_map *bmap)
4337 if (isl_basic_map_plain_is_empty(bmap)) {
4338 isl_basic_map_free(bmap);
4341 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4342 isl_assert(map->ctx, map->n < map->size, goto error);
4343 map->p[map->n] = bmap;
4345 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4351 isl_basic_map_free(bmap);
4355 void isl_map_free(struct isl_map *map)
4365 isl_ctx_deref(map->ctx);
4366 for (i = 0; i < map->n; ++i)
4367 isl_basic_map_free(map->p[i]);
4368 isl_dim_free(map->dim);
4372 struct isl_map *isl_map_extend(struct isl_map *base,
4373 unsigned nparam, unsigned n_in, unsigned n_out)
4377 base = isl_map_cow(base);
4381 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4384 for (i = 0; i < base->n; ++i) {
4385 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4386 isl_dim_copy(base->dim), 0, 0, 0);
4396 struct isl_set *isl_set_extend(struct isl_set *base,
4397 unsigned nparam, unsigned dim)
4399 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4403 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4404 struct isl_basic_map *bmap, unsigned pos, int value)
4408 bmap = isl_basic_map_cow(bmap);
4409 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4410 j = isl_basic_map_alloc_equality(bmap);
4413 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4414 isl_int_set_si(bmap->eq[j][pos], -1);
4415 isl_int_set_si(bmap->eq[j][0], value);
4416 bmap = isl_basic_map_simplify(bmap);
4417 return isl_basic_map_finalize(bmap);
4419 isl_basic_map_free(bmap);
4423 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4424 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4428 bmap = isl_basic_map_cow(bmap);
4429 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4430 j = isl_basic_map_alloc_equality(bmap);
4433 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4434 isl_int_set_si(bmap->eq[j][pos], -1);
4435 isl_int_set(bmap->eq[j][0], value);
4436 bmap = isl_basic_map_simplify(bmap);
4437 return isl_basic_map_finalize(bmap);
4439 isl_basic_map_free(bmap);
4443 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4444 enum isl_dim_type type, unsigned pos, int value)
4448 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4449 return isl_basic_map_fix_pos_si(bmap,
4450 isl_basic_map_offset(bmap, type) + pos, value);
4452 isl_basic_map_free(bmap);
4456 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4457 enum isl_dim_type type, unsigned pos, isl_int value)
4461 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4462 return isl_basic_map_fix_pos(bmap,
4463 isl_basic_map_offset(bmap, type) + pos, value);
4465 isl_basic_map_free(bmap);
4469 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4470 enum isl_dim_type type, unsigned pos, int value)
4472 return (struct isl_basic_set *)
4473 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4477 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4478 enum isl_dim_type type, unsigned pos, isl_int value)
4480 return (struct isl_basic_set *)
4481 isl_basic_map_fix((struct isl_basic_map *)bset,
4485 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4486 unsigned input, int value)
4488 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4491 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4492 unsigned dim, int value)
4494 return (struct isl_basic_set *)
4495 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4496 isl_dim_set, dim, value);
4499 struct isl_map *isl_map_fix_si(struct isl_map *map,
4500 enum isl_dim_type type, unsigned pos, int value)
4504 map = isl_map_cow(map);
4508 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4509 for (i = 0; i < map->n; ++i) {
4510 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4514 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4521 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4522 enum isl_dim_type type, unsigned pos, int value)
4524 return (struct isl_set *)
4525 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4528 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4529 enum isl_dim_type type, unsigned pos, isl_int value)
4533 map = isl_map_cow(map);
4537 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4538 for (i = 0; i < map->n; ++i) {
4539 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4543 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4550 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4551 enum isl_dim_type type, unsigned pos, isl_int value)
4553 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4556 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4557 unsigned input, int value)
4559 return isl_map_fix_si(map, isl_dim_in, input, value);
4562 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4564 return (struct isl_set *)
4565 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4568 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4569 __isl_take isl_basic_map *bmap,
4570 enum isl_dim_type type, unsigned pos, int value)
4576 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4577 pos += isl_basic_map_offset(bmap, type);
4578 bmap = isl_basic_map_cow(bmap);
4579 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4580 j = isl_basic_map_alloc_inequality(bmap);
4583 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4584 isl_int_set_si(bmap->ineq[j][pos], 1);
4585 isl_int_set_si(bmap->ineq[j][0], -value);
4586 bmap = isl_basic_map_simplify(bmap);
4587 return isl_basic_map_finalize(bmap);
4589 isl_basic_map_free(bmap);
4593 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4594 unsigned dim, isl_int value)
4598 bset = isl_basic_set_cow(bset);
4599 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4600 j = isl_basic_set_alloc_inequality(bset);
4603 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4604 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4605 isl_int_neg(bset->ineq[j][0], value);
4606 bset = isl_basic_set_simplify(bset);
4607 return isl_basic_set_finalize(bset);
4609 isl_basic_set_free(bset);
4613 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4614 enum isl_dim_type type, unsigned pos, int value)
4618 map = isl_map_cow(map);
4622 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4623 for (i = 0; i < map->n; ++i) {
4624 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4629 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4636 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4637 enum isl_dim_type type, unsigned pos, int value)
4639 return (struct isl_set *)
4640 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4643 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4648 set = isl_set_cow(set);
4652 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4653 for (i = 0; i < set->n; ++i) {
4654 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4664 struct isl_map *isl_map_reverse(struct isl_map *map)
4668 map = isl_map_cow(map);
4672 map->dim = isl_dim_reverse(map->dim);
4675 for (i = 0; i < map->n; ++i) {
4676 map->p[i] = isl_basic_map_reverse(map->p[i]);
4680 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4687 static struct isl_map *isl_basic_map_partial_lexopt(
4688 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4689 struct isl_set **empty, int max)
4693 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4694 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4696 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4698 isl_basic_map_free(bmap);
4699 isl_basic_set_free(dom);
4705 struct isl_map *isl_basic_map_partial_lexmax(
4706 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4707 struct isl_set **empty)
4709 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4712 struct isl_map *isl_basic_map_partial_lexmin(
4713 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4714 struct isl_set **empty)
4716 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4719 struct isl_set *isl_basic_set_partial_lexmin(
4720 struct isl_basic_set *bset, struct isl_basic_set *dom,
4721 struct isl_set **empty)
4723 return (struct isl_set *)
4724 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4728 struct isl_set *isl_basic_set_partial_lexmax(
4729 struct isl_basic_set *bset, struct isl_basic_set *dom,
4730 struct isl_set **empty)
4732 return (struct isl_set *)
4733 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4737 /* Given a basic map "bmap", compute the lexicographically minimal
4738 * (or maximal) image element for each domain element in dom.
4739 * Set *empty to those elements in dom that do not have an image element.
4741 * We first make sure the basic sets in dom are disjoint and then
4742 * simply collect the results over each of the basic sets separately.
4743 * We could probably improve the efficiency a bit by moving the union
4744 * domain down into the parametric integer programming.
4746 static __isl_give isl_map *basic_map_partial_lexopt(
4747 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4748 __isl_give isl_set **empty, int max)
4751 struct isl_map *res;
4753 dom = isl_set_make_disjoint(dom);
4757 if (isl_set_plain_is_empty(dom)) {
4758 res = isl_map_empty_like_basic_map(bmap);
4759 *empty = isl_set_empty_like(dom);
4761 isl_basic_map_free(bmap);
4765 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4766 isl_basic_set_copy(dom->p[0]), empty, max);
4768 for (i = 1; i < dom->n; ++i) {
4769 struct isl_map *res_i;
4770 struct isl_set *empty_i;
4772 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4773 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4775 res = isl_map_union_disjoint(res, res_i);
4776 *empty = isl_set_union_disjoint(*empty, empty_i);
4780 isl_basic_map_free(bmap);
4785 isl_basic_map_free(bmap);
4789 /* Given a map "map", compute the lexicographically minimal
4790 * (or maximal) image element for each domain element in dom.
4791 * Set *empty to those elements in dom that do not have an image element.
4793 * We first compute the lexicographically minimal or maximal element
4794 * in the first basic map. This results in a partial solution "res"
4795 * and a subset "todo" of dom that still need to be handled.
4796 * We then consider each of the remaining maps in "map" and successively
4797 * improve both "res" and "todo".
4799 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4800 * Assume we are computing the lexicographical maximum.
4801 * We first compute the lexicographically maximal element in basic map i.
4802 * This results in a partial solution res_i and a subset todo_i.
4803 * Then we combine these results with those obtain for the first k basic maps
4804 * to obtain a result that is valid for the first k+1 basic maps.
4805 * In particular, the set where there is no solution is the set where
4806 * there is no solution for the first k basic maps and also no solution
4807 * for the ith basic map, i.e.,
4809 * todo^i = todo^k * todo_i
4811 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4812 * solutions, arbitrarily breaking ties in favor of res^k.
4813 * That is, when res^k(a) >= res_i(a), we pick res^k and
4814 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4815 * the lexicographic order.)
4816 * In practice, we compute
4818 * res^k * (res_i . "<=")
4822 * res_i * (res^k . "<")
4824 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4825 * where only one of res^k and res_i provides a solution and we simply pick
4832 * Note that we only compute these intersections when dom(res^k) intersects
4833 * dom(res_i). Otherwise, the only effect of these intersections is to
4834 * potentially break up res^k and res_i into smaller pieces.
4835 * We want to avoid such splintering as much as possible.
4836 * In fact, an earlier implementation of this function would look for
4837 * better results in the domain of res^k and for extra results in todo^k,
4838 * but this would always result in a splintering according to todo^k,
4839 * even when the domain of basic map i is disjoint from the domains of
4840 * the previous basic maps.
4842 static __isl_give isl_map *isl_map_partial_lexopt(
4843 __isl_take isl_map *map, __isl_take isl_set *dom,
4844 __isl_give isl_set **empty, int max)
4847 struct isl_map *res;
4848 struct isl_set *todo;
4853 if (isl_map_plain_is_empty(map)) {
4861 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4862 isl_set_copy(dom), &todo, max);
4864 for (i = 1; i < map->n; ++i) {
4868 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4870 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4871 isl_set_copy(dom), &todo_i, max);
4874 lt = isl_map_lex_lt(isl_dim_copy(dim));
4875 le = isl_map_lex_le(dim);
4877 lt = isl_map_lex_gt(isl_dim_copy(dim));
4878 le = isl_map_lex_ge(dim);
4880 lt = isl_map_apply_range(isl_map_copy(res), lt);
4881 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4882 le = isl_map_apply_range(isl_map_copy(res_i), le);
4883 le = isl_map_intersect(le, isl_map_copy(res));
4885 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4886 res = isl_map_intersect_domain(res,
4887 isl_set_copy(todo_i));
4888 res_i = isl_map_intersect_domain(res_i,
4889 isl_set_copy(todo));
4892 res = isl_map_union_disjoint(res, res_i);
4893 res = isl_map_union_disjoint(res, lt);
4894 res = isl_map_union_disjoint(res, le);
4896 todo = isl_set_intersect(todo, todo_i);
4916 __isl_give isl_map *isl_map_partial_lexmax(
4917 __isl_take isl_map *map, __isl_take isl_set *dom,
4918 __isl_give isl_set **empty)
4920 return isl_map_partial_lexopt(map, dom, empty, 1);
4923 __isl_give isl_map *isl_map_partial_lexmin(
4924 __isl_take isl_map *map, __isl_take isl_set *dom,
4925 __isl_give isl_set **empty)
4927 return isl_map_partial_lexopt(map, dom, empty, 0);
4930 __isl_give isl_set *isl_set_partial_lexmin(
4931 __isl_take isl_set *set, __isl_take isl_set *dom,
4932 __isl_give isl_set **empty)
4934 return (struct isl_set *)
4935 isl_map_partial_lexmin((struct isl_map *)set,
4939 __isl_give isl_set *isl_set_partial_lexmax(
4940 __isl_take isl_set *set, __isl_take isl_set *dom,
4941 __isl_give isl_set **empty)
4943 return (struct isl_set *)
4944 isl_map_partial_lexmax((struct isl_map *)set,
4948 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4950 struct isl_basic_set *dom = NULL;
4951 struct isl_dim *dom_dim;
4955 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4956 dom = isl_basic_set_universe(dom_dim);
4957 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4959 isl_basic_map_free(bmap);
4963 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4965 return isl_basic_map_lexopt(bmap, 0);
4968 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4970 return isl_basic_map_lexopt(bmap, 1);
4973 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4975 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4978 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4980 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4983 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4985 struct isl_set *dom = NULL;
4986 struct isl_dim *dom_dim;
4990 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4991 dom = isl_set_universe(dom_dim);
4992 return isl_map_partial_lexopt(map, dom, NULL, max);
4998 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5000 return isl_map_lexopt(map, 0);
5003 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5005 return isl_map_lexopt(map, 1);
5008 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5010 return (isl_set *)isl_map_lexmin((isl_map *)set);
5013 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5015 return (isl_set *)isl_map_lexmax((isl_map *)set);
5018 /* Apply a preimage specified by "mat" on the parameters of "bset".
5019 * bset is assumed to have only parameters and divs.
5021 static struct isl_basic_set *basic_set_parameter_preimage(
5022 struct isl_basic_set *bset, struct isl_mat *mat)
5029 bset->dim = isl_dim_cow(bset->dim);
5033 nparam = isl_basic_set_dim(bset, isl_dim_param);
5035 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5037 bset->dim->nparam = 0;
5038 bset->dim->n_out = nparam;
5039 bset = isl_basic_set_preimage(bset, mat);
5041 bset->dim->nparam = bset->dim->n_out;
5042 bset->dim->n_out = 0;
5047 isl_basic_set_free(bset);
5051 /* Apply a preimage specified by "mat" on the parameters of "set".
5052 * set is assumed to have only parameters and divs.
5054 static struct isl_set *set_parameter_preimage(
5055 struct isl_set *set, struct isl_mat *mat)
5057 struct isl_dim *dim = NULL;
5063 dim = isl_dim_copy(set->dim);
5064 dim = isl_dim_cow(dim);
5068 nparam = isl_set_dim(set, isl_dim_param);
5070 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5073 dim->n_out = nparam;
5074 isl_set_reset_dim(set, dim);
5075 set = isl_set_preimage(set, mat);
5078 dim = isl_dim_copy(set->dim);
5079 dim = isl_dim_cow(dim);
5082 dim->nparam = dim->n_out;
5084 isl_set_reset_dim(set, dim);
5094 /* Intersect the basic set "bset" with the affine space specified by the
5095 * equalities in "eq".
5097 static struct isl_basic_set *basic_set_append_equalities(
5098 struct isl_basic_set *bset, struct isl_mat *eq)
5106 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5111 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5112 for (i = 0; i < eq->n_row; ++i) {
5113 k = isl_basic_set_alloc_equality(bset);
5116 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5117 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5121 bset = isl_basic_set_gauss(bset, NULL);
5122 bset = isl_basic_set_finalize(bset);
5127 isl_basic_set_free(bset);
5131 /* Intersect the set "set" with the affine space specified by the
5132 * equalities in "eq".
5134 static struct isl_set *set_append_equalities(struct isl_set *set,
5142 for (i = 0; i < set->n; ++i) {
5143 set->p[i] = basic_set_append_equalities(set->p[i],
5156 /* Project the given basic set onto its parameter domain, possibly introducing
5157 * new, explicit, existential variables in the constraints.
5158 * The input has parameters and (possibly implicit) existential variables.
5159 * The output has the same parameters, but only
5160 * explicit existentially quantified variables.
5162 * The actual projection is performed by pip, but pip doesn't seem
5163 * to like equalities very much, so we first remove the equalities
5164 * among the parameters by performing a variable compression on
5165 * the parameters. Afterward, an inverse transformation is performed
5166 * and the equalities among the parameters are inserted back in.
5168 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5172 struct isl_mat *T, *T2;
5173 struct isl_set *set;
5174 unsigned nparam, n_div;
5176 bset = isl_basic_set_cow(bset);
5180 if (bset->n_eq == 0)
5181 return isl_basic_set_lexmin(bset);
5183 isl_basic_set_gauss(bset, NULL);
5185 nparam = isl_basic_set_dim(bset, isl_dim_param);
5186 n_div = isl_basic_set_dim(bset, isl_dim_div);
5188 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5189 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5192 if (i == bset->n_eq)
5193 return isl_basic_set_lexmin(bset);
5195 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5197 eq = isl_mat_cow(eq);
5198 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5199 if (T && T->n_col == 0) {
5203 bset = isl_basic_set_set_to_empty(bset);
5204 return isl_set_from_basic_set(bset);
5206 bset = basic_set_parameter_preimage(bset, T);
5208 set = isl_basic_set_lexmin(bset);
5209 set = set_parameter_preimage(set, T2);
5210 set = set_append_equalities(set, eq);
5214 /* Compute an explicit representation for all the existentially
5215 * quantified variables.
5216 * The input and output dimensions are first turned into parameters.
5217 * compute_divs then returns a map with the same parameters and
5218 * no input or output dimensions and the dimension specification
5219 * is reset to that of the input.
5221 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5223 struct isl_basic_set *bset;
5224 struct isl_set *set;
5225 struct isl_map *map;
5226 struct isl_dim *dim, *orig_dim = NULL;
5231 bmap = isl_basic_map_cow(bmap);
5235 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5236 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5237 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5238 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5242 orig_dim = bmap->dim;
5244 bset = (struct isl_basic_set *)bmap;
5246 set = parameter_compute_divs(bset);
5247 map = (struct isl_map *)set;
5248 map = isl_map_reset_dim(map, orig_dim);
5252 isl_basic_map_free(bmap);
5256 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5264 off = isl_dim_total(bmap->dim);
5265 for (i = 0; i < bmap->n_div; ++i) {
5266 if (isl_int_is_zero(bmap->div[i][0]))
5268 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5274 static int map_divs_known(__isl_keep isl_map *map)
5281 for (i = 0; i < map->n; ++i) {
5282 int known = basic_map_divs_known(map->p[i]);
5290 /* If bmap contains any unknown divs, then compute explicit
5291 * expressions for them. However, this computation may be
5292 * quite expensive, so first try to remove divs that aren't
5295 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5298 struct isl_map *map;
5300 known = basic_map_divs_known(bmap);
5304 return isl_map_from_basic_map(bmap);
5306 bmap = isl_basic_map_drop_redundant_divs(bmap);
5308 known = basic_map_divs_known(bmap);
5312 return isl_map_from_basic_map(bmap);
5314 map = compute_divs(bmap);
5317 isl_basic_map_free(bmap);
5321 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5325 struct isl_map *res;
5332 known = map_divs_known(map);
5340 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5341 for (i = 1 ; i < map->n; ++i) {
5343 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5344 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5345 res = isl_map_union_disjoint(res, r2);
5347 res = isl_map_union(res, r2);
5354 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5356 return (struct isl_set *)
5357 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5360 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5362 return (struct isl_set *)
5363 isl_map_compute_divs((struct isl_map *)set);
5366 struct isl_set *isl_map_domain(struct isl_map *map)
5369 struct isl_set *set;
5374 map = isl_map_cow(map);
5378 set = (struct isl_set *)map;
5379 set->dim = isl_dim_domain(set->dim);
5382 for (i = 0; i < map->n; ++i) {
5383 set->p[i] = isl_basic_map_domain(map->p[i]);
5387 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5388 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5395 struct isl_map *isl_map_union_disjoint(
5396 struct isl_map *map1, struct isl_map *map2)
5400 struct isl_map *map = NULL;
5414 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5416 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5417 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5418 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5420 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5421 map1->n + map2->n, flags);
5424 for (i = 0; i < map1->n; ++i) {
5425 map = isl_map_add_basic_map(map,
5426 isl_basic_map_copy(map1->p[i]));
5430 for (i = 0; i < map2->n; ++i) {
5431 map = isl_map_add_basic_map(map,
5432 isl_basic_map_copy(map2->p[i]));
5446 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5448 map1 = isl_map_union_disjoint(map1, map2);
5452 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5456 struct isl_set *isl_set_union_disjoint(
5457 struct isl_set *set1, struct isl_set *set2)
5459 return (struct isl_set *)
5460 isl_map_union_disjoint(
5461 (struct isl_map *)set1, (struct isl_map *)set2);
5464 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5466 return (struct isl_set *)
5467 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5470 struct isl_map *isl_map_intersect_range(
5471 struct isl_map *map, struct isl_set *set)
5474 struct isl_map *result;
5480 if (!isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5481 isl_die(set->ctx, isl_error_invalid,
5482 "parameters don't match", goto error);
5484 if (isl_dim_size(set->dim, isl_dim_set) != 0 &&
5485 !isl_map_compatible_range(map, set))
5486 isl_die(set->ctx, isl_error_invalid,
5487 "incompatible spaces", goto error);
5489 if (isl_set_plain_is_universe(set)) {
5494 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5495 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5496 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5498 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5499 map->n * set->n, flags);
5502 for (i = 0; i < map->n; ++i)
5503 for (j = 0; j < set->n; ++j) {
5504 result = isl_map_add_basic_map(result,
5505 isl_basic_map_intersect_range(
5506 isl_basic_map_copy(map->p[i]),
5507 isl_basic_set_copy(set->p[j])));
5520 struct isl_map *isl_map_intersect_domain(
5521 struct isl_map *map, struct isl_set *set)
5523 return isl_map_reverse(
5524 isl_map_intersect_range(isl_map_reverse(map), set));
5527 struct isl_map *isl_map_apply_domain(
5528 struct isl_map *map1, struct isl_map *map2)
5532 map1 = isl_map_reverse(map1);
5533 map1 = isl_map_apply_range(map1, map2);
5534 return isl_map_reverse(map1);
5541 struct isl_map *isl_map_apply_range(
5542 struct isl_map *map1, struct isl_map *map2)
5544 struct isl_dim *dim_result;
5545 struct isl_map *result;
5551 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5552 isl_dim_copy(map2->dim));
5554 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5557 for (i = 0; i < map1->n; ++i)
5558 for (j = 0; j < map2->n; ++j) {
5559 result = isl_map_add_basic_map(result,
5560 isl_basic_map_apply_range(
5561 isl_basic_map_copy(map1->p[i]),
5562 isl_basic_map_copy(map2->p[j])));
5568 if (result && result->n <= 1)
5569 ISL_F_SET(result, ISL_MAP_DISJOINT);
5578 * returns range - domain
5580 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5582 isl_dim *dims, *target_dim;
5583 struct isl_basic_set *bset;
5590 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5591 bmap->dim, isl_dim_out),
5593 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5594 dim = isl_basic_map_n_in(bmap);
5595 nparam = isl_basic_map_n_param(bmap);
5596 bset = isl_basic_set_from_basic_map(bmap);
5597 bset = isl_basic_set_cow(bset);
5598 dims = isl_basic_set_get_dim(bset);
5599 dims = isl_dim_add(dims, isl_dim_set, dim);
5600 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5601 bset = isl_basic_set_swap_vars(bset, 2*dim);
5602 for (i = 0; i < dim; ++i) {
5603 int j = isl_basic_map_alloc_equality(
5604 (struct isl_basic_map *)bset);
5607 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5608 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5609 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5610 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5612 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5613 bset = isl_basic_set_reset_dim(bset, target_dim);
5616 isl_basic_map_free(bmap);
5621 * returns range - domain
5623 struct isl_set *isl_map_deltas(struct isl_map *map)
5627 struct isl_set *result;
5632 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5633 map->dim, isl_dim_out),
5635 dim = isl_map_get_dim(map);
5636 dim = isl_dim_domain(dim);
5637 result = isl_set_alloc_dim(dim, map->n, 0);
5640 for (i = 0; i < map->n; ++i)
5641 result = isl_set_add_basic_set(result,
5642 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5651 * returns [domain -> range] -> range - domain
5653 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5654 __isl_take isl_basic_map *bmap)
5658 isl_basic_map *domain;
5662 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5663 isl_die(bmap->ctx, isl_error_invalid,
5664 "domain and range don't match", goto error);
5666 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5667 n = isl_basic_map_dim(bmap, isl_dim_in);
5669 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5670 domain = isl_basic_map_universe(dim);
5672 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5673 bmap = isl_basic_map_apply_range(bmap, domain);
5674 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5676 total = isl_basic_map_total_dim(bmap);
5678 for (i = 0; i < n; ++i) {
5679 k = isl_basic_map_alloc_equality(bmap);
5682 isl_seq_clr(bmap->eq[k], 1 + total);
5683 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5684 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5685 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5688 bmap = isl_basic_map_gauss(bmap, NULL);
5689 return isl_basic_map_finalize(bmap);
5691 isl_basic_map_free(bmap);
5696 * returns [domain -> range] -> range - domain
5698 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5701 isl_dim *domain_dim;
5706 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5707 isl_die(map->ctx, isl_error_invalid,
5708 "domain and range don't match", goto error);
5710 map = isl_map_cow(map);
5714 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5715 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5716 map->dim = isl_dim_join(map->dim, domain_dim);
5719 for (i = 0; i < map->n; ++i) {
5720 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5724 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5731 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5733 struct isl_basic_map *bmap;
5741 nparam = dims->nparam;
5743 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5747 for (i = 0; i < dim; ++i) {
5748 int j = isl_basic_map_alloc_equality(bmap);
5751 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5752 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5753 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5755 return isl_basic_map_finalize(bmap);
5757 isl_basic_map_free(bmap);
5761 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5765 if (dim->n_in != dim->n_out)
5766 isl_die(dim->ctx, isl_error_invalid,
5767 "number of input and output dimensions needs to be "
5768 "the same", goto error);
5769 return basic_map_identity(dim);
5775 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5777 if (!model || !model->dim)
5779 return isl_basic_map_identity(isl_dim_copy(model->dim));
5782 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5784 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5787 struct isl_map *isl_map_identity_like(struct isl_map *model)
5789 if (!model || !model->dim)
5791 return isl_map_identity(isl_dim_copy(model->dim));
5794 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5796 if (!model || !model->dim)
5798 return isl_map_identity(isl_dim_copy(model->dim));
5801 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5803 isl_dim *dim = isl_set_get_dim(set);
5805 id = isl_map_identity(isl_dim_map_from_set(dim));
5806 return isl_map_intersect_range(id, set);
5809 /* Construct a basic set with all set dimensions having only non-negative
5812 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5817 struct isl_basic_set *bset;
5821 nparam = dims->nparam;
5823 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5826 for (i = 0; i < dim; ++i) {
5827 int k = isl_basic_set_alloc_inequality(bset);
5830 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5831 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5835 isl_basic_set_free(bset);
5839 /* Construct the half-space x_pos >= 0.
5841 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5845 isl_basic_set *nonneg;
5847 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5848 k = isl_basic_set_alloc_inequality(nonneg);
5851 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5852 isl_int_set_si(nonneg->ineq[k][pos], 1);
5854 return isl_basic_set_finalize(nonneg);
5856 isl_basic_set_free(nonneg);
5860 /* Construct the half-space x_pos <= -1.
5862 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5867 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5868 k = isl_basic_set_alloc_inequality(neg);
5871 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5872 isl_int_set_si(neg->ineq[k][0], -1);
5873 isl_int_set_si(neg->ineq[k][pos], -1);
5875 return isl_basic_set_finalize(neg);
5877 isl_basic_set_free(neg);
5881 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5882 enum isl_dim_type type, unsigned first, unsigned n)
5885 isl_basic_set *nonneg;
5893 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5895 for (i = 0; i < n; ++i) {
5896 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5897 pos(set->dim, type) + first + i);
5898 neg = neg_halfspace(isl_set_get_dim(set),
5899 pos(set->dim, type) + first + i);
5901 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5910 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5911 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5918 if (isl_set_plain_is_empty(set)) {
5923 return fn(set, signs, user);
5926 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5928 half = isl_set_intersect(half, isl_set_copy(set));
5929 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5933 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5935 half = isl_set_intersect(half, set);
5936 return foreach_orthant(half, signs, first + 1, len, fn, user);
5942 /* Call "fn" on the intersections of "set" with each of the orthants
5943 * (except for obviously empty intersections). The orthant is identified
5944 * by the signs array, with each entry having value 1 or -1 according
5945 * to the sign of the corresponding variable.
5947 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5948 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5958 if (isl_set_plain_is_empty(set))
5961 nparam = isl_set_dim(set, isl_dim_param);
5962 nvar = isl_set_dim(set, isl_dim_set);
5964 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5966 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5974 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5976 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5979 int isl_basic_map_is_subset(
5980 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5983 struct isl_map *map1;
5984 struct isl_map *map2;
5986 if (!bmap1 || !bmap2)
5989 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5990 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5992 is_subset = isl_map_is_subset(map1, map2);
6000 int isl_basic_map_is_equal(
6001 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6005 if (!bmap1 || !bmap2)
6007 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6010 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6014 int isl_basic_set_is_equal(
6015 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6017 return isl_basic_map_is_equal(
6018 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6021 int isl_map_is_empty(struct isl_map *map)
6028 for (i = 0; i < map->n; ++i) {
6029 is_empty = isl_basic_map_is_empty(map->p[i]);
6038 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6040 return map ? map->n == 0 : -1;
6043 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6045 return isl_map_plain_is_empty(map);
6048 int isl_set_plain_is_empty(struct isl_set *set)
6050 return set ? set->n == 0 : -1;
6053 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6055 return isl_set_plain_is_empty(set);
6058 int isl_set_is_empty(struct isl_set *set)
6060 return isl_map_is_empty((struct isl_map *)set);
6063 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6068 return isl_dim_equal(map1->dim, map2->dim);
6071 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6076 return isl_dim_equal(set1->dim, set2->dim);
6079 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6085 is_subset = isl_map_is_subset(map1, map2);
6088 is_subset = isl_map_is_subset(map2, map1);
6092 int isl_basic_map_is_strict_subset(
6093 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6097 if (!bmap1 || !bmap2)
6099 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6102 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6103 if (is_subset == -1)
6108 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6114 is_subset = isl_map_is_subset(map1, map2);
6117 is_subset = isl_map_is_subset(map2, map1);
6118 if (is_subset == -1)
6123 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6125 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6128 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6132 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6135 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6139 return bset->n_eq == 0 && bset->n_ineq == 0;
6142 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6149 for (i = 0; i < map->n; ++i) {
6150 int r = isl_basic_map_is_universe(map->p[i]);
6158 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6160 return isl_map_plain_is_universe((isl_map *) set);
6163 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6165 return isl_set_plain_is_universe(set);
6168 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6170 struct isl_basic_set *bset = NULL;
6171 struct isl_vec *sample = NULL;
6178 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6181 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6182 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6183 copy = isl_basic_map_remove_redundancies(copy);
6184 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6185 isl_basic_map_free(copy);
6189 total = 1 + isl_basic_map_total_dim(bmap);
6190 if (bmap->sample && bmap->sample->size == total) {
6191 int contains = isl_basic_map_contains(bmap, bmap->sample);
6197 isl_vec_free(bmap->sample);
6198 bmap->sample = NULL;
6199 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6202 sample = isl_basic_set_sample_vec(bset);
6205 empty = sample->size == 0;
6206 isl_vec_free(bmap->sample);
6207 bmap->sample = sample;
6209 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6214 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6218 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6221 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6223 return isl_basic_map_plain_is_empty(bmap);
6226 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6230 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6233 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6235 return isl_basic_set_plain_is_empty(bset);
6238 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6240 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6243 struct isl_map *isl_basic_map_union(
6244 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6246 struct isl_map *map;
6247 if (!bmap1 || !bmap2)
6250 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6252 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6255 map = isl_map_add_basic_map(map, bmap1);
6256 map = isl_map_add_basic_map(map, bmap2);
6259 isl_basic_map_free(bmap1);
6260 isl_basic_map_free(bmap2);
6264 struct isl_set *isl_basic_set_union(
6265 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6267 return (struct isl_set *)isl_basic_map_union(
6268 (struct isl_basic_map *)bset1,
6269 (struct isl_basic_map *)bset2);
6272 /* Order divs such that any div only depends on previous divs */
6273 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6281 off = isl_dim_total(bmap->dim);
6283 for (i = 0; i < bmap->n_div; ++i) {
6285 if (isl_int_is_zero(bmap->div[i][0]))
6287 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6291 isl_basic_map_swap_div(bmap, i, i + pos);
6297 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6299 return (struct isl_basic_set *)
6300 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6303 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6310 for (i = 0; i < map->n; ++i) {
6311 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6322 /* Apply the expansion computed by isl_merge_divs.
6323 * The expansion itself is given by "exp" while the resulting
6324 * list of divs is given by "div".
6326 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6327 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6332 bset = isl_basic_set_cow(bset);
6336 if (div->n_row < bset->n_div)
6337 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6338 "not an expansion", goto error);
6340 bset = isl_basic_map_extend_dim(bset, isl_dim_copy(bset->dim),
6341 div->n_row - bset->n_div, 0,
6342 2 * (div->n_row - bset->n_div));
6344 n_div = bset->n_div;
6345 for (i = n_div; i < div->n_row; ++i)
6346 if (isl_basic_set_alloc_div(bset) < 0)
6350 for (i = div->n_row - 1; i >= 0; --i) {
6351 if (j >= 0 && exp[j] == i) {
6353 isl_basic_map_swap_div(bset, i, j);
6356 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6357 if (isl_basic_map_add_div_constraints(bset, i) < 0)
6365 isl_basic_set_free(bset);
6370 /* Look for a div in dst that corresponds to the div "div" in src.
6371 * The divs before "div" in src and dst are assumed to be the same.
6373 * Returns -1 if no corresponding div was found and the position
6374 * of the corresponding div in dst otherwise.
6376 static int find_div(struct isl_basic_map *dst,
6377 struct isl_basic_map *src, unsigned div)
6381 unsigned total = isl_dim_total(src->dim);
6383 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6384 for (i = div; i < dst->n_div; ++i)
6385 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6386 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6387 dst->n_div - div) == -1)
6392 struct isl_basic_map *isl_basic_map_align_divs(
6393 struct isl_basic_map *dst, struct isl_basic_map *src)
6396 unsigned total = isl_dim_total(src->dim);
6401 if (src->n_div == 0)
6404 for (i = 0; i < src->n_div; ++i)
6405 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6407 src = isl_basic_map_order_divs(src);
6408 dst = isl_basic_map_cow(dst);
6409 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6410 src->n_div, 0, 2 * src->n_div);
6413 for (i = 0; i < src->n_div; ++i) {
6414 int j = find_div(dst, src, i);
6416 j = isl_basic_map_alloc_div(dst);
6419 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6420 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6421 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6425 isl_basic_map_swap_div(dst, i, j);
6429 isl_basic_map_free(dst);
6433 struct isl_basic_set *isl_basic_set_align_divs(
6434 struct isl_basic_set *dst, struct isl_basic_set *src)
6436 return (struct isl_basic_set *)isl_basic_map_align_divs(
6437 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6440 struct isl_map *isl_map_align_divs(struct isl_map *map)
6448 map = isl_map_compute_divs(map);
6449 map = isl_map_cow(map);
6453 for (i = 1; i < map->n; ++i)
6454 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6455 for (i = 1; i < map->n; ++i)
6456 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6458 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6462 struct isl_set *isl_set_align_divs(struct isl_set *set)
6464 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6467 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6471 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6472 map = isl_map_intersect_domain(map, set);
6473 set = isl_map_range(map);
6481 /* There is no need to cow as removing empty parts doesn't change
6482 * the meaning of the set.
6484 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6491 for (i = map->n-1; i >= 0; --i) {
6492 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6494 isl_basic_map_free(map->p[i]);
6495 if (i != map->n-1) {
6496 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6497 map->p[i] = map->p[map->n-1];
6505 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6507 return (struct isl_set *)
6508 isl_map_remove_empty_parts((struct isl_map *)set);
6511 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6513 struct isl_basic_map *bmap;
6514 if (!map || map->n == 0)
6516 bmap = map->p[map->n-1];
6517 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6518 return isl_basic_map_copy(bmap);
6521 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6523 return (struct isl_basic_set *)
6524 isl_map_copy_basic_map((struct isl_map *)set);
6527 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6528 __isl_keep isl_basic_map *bmap)
6534 for (i = map->n-1; i >= 0; --i) {
6535 if (map->p[i] != bmap)
6537 map = isl_map_cow(map);
6540 isl_basic_map_free(map->p[i]);
6541 if (i != map->n-1) {
6542 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6543 map->p[i] = map->p[map->n-1];
6554 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6555 struct isl_basic_set *bset)
6557 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6558 (struct isl_basic_map *)bset);
6561 /* Given two basic sets bset1 and bset2, compute the maximal difference
6562 * between the values of dimension pos in bset1 and those in bset2
6563 * for any common value of the parameters and dimensions preceding pos.
6565 static enum isl_lp_result basic_set_maximal_difference_at(
6566 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6567 int pos, isl_int *opt)
6569 struct isl_dim *dims;
6570 struct isl_basic_map *bmap1 = NULL;
6571 struct isl_basic_map *bmap2 = NULL;
6572 struct isl_ctx *ctx;
6573 struct isl_vec *obj;
6576 unsigned dim1, dim2;
6577 enum isl_lp_result res;
6579 if (!bset1 || !bset2)
6580 return isl_lp_error;
6582 nparam = isl_basic_set_n_param(bset1);
6583 dim1 = isl_basic_set_n_dim(bset1);
6584 dim2 = isl_basic_set_n_dim(bset2);
6585 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6586 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6587 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6588 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6589 if (!bmap1 || !bmap2)
6591 bmap1 = isl_basic_map_cow(bmap1);
6592 bmap1 = isl_basic_map_extend(bmap1, nparam,
6593 pos, (dim1 - pos) + (dim2 - pos),
6594 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6595 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6598 total = isl_basic_map_total_dim(bmap1);
6600 obj = isl_vec_alloc(ctx, 1 + total);
6601 isl_seq_clr(obj->block.data, 1 + total);
6602 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6603 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6606 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6608 isl_basic_map_free(bmap1);
6612 isl_basic_map_free(bmap1);
6613 isl_basic_map_free(bmap2);
6614 return isl_lp_error;
6617 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6618 * for any common value of the parameters and dimensions preceding pos
6619 * in both basic sets, the values of dimension pos in bset1 are
6620 * smaller or larger than those in bset2.
6623 * 1 if bset1 follows bset2
6624 * -1 if bset1 precedes bset2
6625 * 0 if bset1 and bset2 are incomparable
6626 * -2 if some error occurred.
6628 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6629 struct isl_basic_set *bset2, int pos)
6632 enum isl_lp_result res;
6637 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6639 if (res == isl_lp_empty)
6641 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6642 res == isl_lp_unbounded)
6644 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6653 /* Given two basic sets bset1 and bset2, check whether
6654 * for any common value of the parameters and dimensions preceding pos
6655 * there is a value of dimension pos in bset1 that is larger
6656 * than a value of the same dimension in bset2.
6659 * 1 if there exists such a pair
6660 * 0 if there is no such pair, but there is a pair of equal values
6662 * -2 if some error occurred.
6664 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6665 __isl_keep isl_basic_set *bset2, int pos)
6668 enum isl_lp_result res;
6673 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6675 if (res == isl_lp_empty)
6677 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6678 res == isl_lp_unbounded)
6680 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6682 else if (res == isl_lp_ok)
6691 /* Given two sets set1 and set2, check whether
6692 * for any common value of the parameters and dimensions preceding pos
6693 * there is a value of dimension pos in set1 that is larger
6694 * than a value of the same dimension in set2.
6697 * 1 if there exists such a pair
6698 * 0 if there is no such pair, but there is a pair of equal values
6700 * -2 if some error occurred.
6702 int isl_set_follows_at(__isl_keep isl_set *set1,
6703 __isl_keep isl_set *set2, int pos)
6711 for (i = 0; i < set1->n; ++i)
6712 for (j = 0; j < set2->n; ++j) {
6714 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6715 if (f == 1 || f == -2)
6724 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
6725 unsigned pos, isl_int *val)
6733 total = isl_basic_map_total_dim(bmap);
6734 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6735 for (; d+1 > pos; --d)
6736 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6740 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6742 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6744 if (!isl_int_is_one(bmap->eq[i][1+d]))
6747 isl_int_neg(*val, bmap->eq[i][0]);
6753 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
6754 unsigned pos, isl_int *val)
6766 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
6769 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
6770 for (i = 1; fixed == 1 && i < map->n; ++i) {
6771 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
6772 if (fixed == 1 && isl_int_ne(tmp, v))
6776 isl_int_set(*val, v);
6782 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
6783 unsigned pos, isl_int *val)
6785 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
6789 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
6792 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
6795 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
6796 enum isl_dim_type type, unsigned pos, isl_int *val)
6798 if (pos >= isl_basic_map_dim(bmap, type))
6800 return isl_basic_map_plain_has_fixed_var(bmap,
6801 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6804 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
6805 enum isl_dim_type type, unsigned pos, isl_int *val)
6807 if (pos >= isl_map_dim(map, type))
6809 return isl_map_plain_has_fixed_var(map,
6810 map_offset(map, type) - 1 + pos, val);
6813 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
6814 enum isl_dim_type type, unsigned pos, isl_int *val)
6816 return isl_map_plain_is_fixed(map, type, pos, val);
6819 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6820 * then return this fixed value in *val.
6822 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
6823 unsigned dim, isl_int *val)
6825 return isl_basic_set_plain_has_fixed_var(bset,
6826 isl_basic_set_n_param(bset) + dim, val);
6829 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6830 * then return this fixed value in *val.
6832 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
6833 unsigned dim, isl_int *val)
6835 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6838 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
6839 unsigned dim, isl_int *val)
6841 return isl_set_plain_dim_is_fixed(set, dim, val);
6844 /* Check if input variable in has fixed value and if so and if val is not NULL,
6845 * then return this fixed value in *val.
6847 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
6848 unsigned in, isl_int *val)
6850 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
6853 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6854 * and if val is not NULL, then return this lower bound in *val.
6856 int isl_basic_set_plain_dim_has_fixed_lower_bound(
6857 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
6859 int i, i_eq = -1, i_ineq = -1;
6866 total = isl_basic_set_total_dim(bset);
6867 nparam = isl_basic_set_n_param(bset);
6868 for (i = 0; i < bset->n_eq; ++i) {
6869 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6875 for (i = 0; i < bset->n_ineq; ++i) {
6876 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6878 if (i_eq != -1 || i_ineq != -1)
6882 if (i_eq == -1 && i_ineq == -1)
6884 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6885 /* The coefficient should always be one due to normalization. */
6886 if (!isl_int_is_one(c[1+nparam+dim]))
6888 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6890 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6891 total - nparam - dim - 1) != -1)
6894 isl_int_neg(*val, c[0]);
6898 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
6899 unsigned dim, isl_int *val)
6911 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
6915 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
6917 for (i = 1; fixed == 1 && i < set->n; ++i) {
6918 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
6920 if (fixed == 1 && isl_int_ne(tmp, v))
6924 isl_int_set(*val, v);
6935 /* uset_gist depends on constraints without existentially quantified
6936 * variables sorting first.
6938 static int qsort_constraint_cmp(const void *p1, const void *p2)
6940 const struct constraint *c1 = (const struct constraint *)p1;
6941 const struct constraint *c2 = (const struct constraint *)p2;
6943 unsigned size = isl_min(c1->size, c2->size);
6945 l1 = isl_seq_last_non_zero(c1->c, size);
6946 l2 = isl_seq_last_non_zero(c2->c, size);
6951 return isl_seq_cmp(c1->c, c2->c, size);
6954 static struct isl_basic_map *isl_basic_map_sort_constraints(
6955 struct isl_basic_map *bmap)
6958 struct constraint *c;
6963 total = isl_basic_map_total_dim(bmap);
6964 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6967 for (i = 0; i < bmap->n_ineq; ++i) {
6969 c[i].c = bmap->ineq[i];
6971 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6972 for (i = 0; i < bmap->n_ineq; ++i)
6973 bmap->ineq[i] = c[i].c;
6977 isl_basic_map_free(bmap);
6981 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6982 __isl_take isl_basic_set *bset)
6984 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6985 (struct isl_basic_map *)bset);
6988 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6992 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6994 bmap = isl_basic_map_remove_redundancies(bmap);
6995 bmap = isl_basic_map_sort_constraints(bmap);
6996 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7000 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7002 return (struct isl_basic_set *)isl_basic_map_normalize(
7003 (struct isl_basic_map *)bset);
7006 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7007 const __isl_keep isl_basic_map *bmap2)
7014 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7015 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7016 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7017 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7018 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7019 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7020 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7021 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7022 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7023 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7024 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7026 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7028 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7030 if (bmap1->n_eq != bmap2->n_eq)
7031 return bmap1->n_eq - bmap2->n_eq;
7032 if (bmap1->n_ineq != bmap2->n_ineq)
7033 return bmap1->n_ineq - bmap2->n_ineq;
7034 if (bmap1->n_div != bmap2->n_div)
7035 return bmap1->n_div - bmap2->n_div;
7036 total = isl_basic_map_total_dim(bmap1);
7037 for (i = 0; i < bmap1->n_eq; ++i) {
7038 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7042 for (i = 0; i < bmap1->n_ineq; ++i) {
7043 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7047 for (i = 0; i < bmap1->n_div; ++i) {
7048 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7055 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7056 __isl_keep isl_basic_map *bmap2)
7058 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7061 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7062 __isl_keep isl_basic_set *bset2)
7064 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7065 (isl_basic_map *)bset2);
7068 static int qsort_bmap_cmp(const void *p1, const void *p2)
7070 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7071 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7073 return isl_basic_map_plain_cmp(bmap1, bmap2);
7076 /* We normalize in place, but if anything goes wrong we need
7077 * to return NULL, so we need to make sure we don't change the
7078 * meaning of any possible other copies of map.
7080 struct isl_map *isl_map_normalize(struct isl_map *map)
7083 struct isl_basic_map *bmap;
7087 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7089 for (i = 0; i < map->n; ++i) {
7090 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7093 isl_basic_map_free(map->p[i]);
7096 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7097 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7098 map = isl_map_remove_empty_parts(map);
7101 for (i = map->n - 1; i >= 1; --i) {
7102 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7104 isl_basic_map_free(map->p[i-1]);
7105 for (j = i; j < map->n; ++j)
7106 map->p[j-1] = map->p[j];
7116 struct isl_set *isl_set_normalize(struct isl_set *set)
7118 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7121 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7131 if (!isl_dim_equal(map1->dim, map2->dim))
7134 map1 = isl_map_copy(map1);
7135 map2 = isl_map_copy(map2);
7136 map1 = isl_map_normalize(map1);
7137 map2 = isl_map_normalize(map2);
7140 equal = map1->n == map2->n;
7141 for (i = 0; equal && i < map1->n; ++i) {
7142 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7155 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7157 return isl_map_plain_is_equal(map1, map2);
7160 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7162 return isl_map_plain_is_equal((struct isl_map *)set1,
7163 (struct isl_map *)set2);
7166 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7168 return isl_set_plain_is_equal(set1, set2);
7171 /* Return an interval that ranges from min to max (inclusive)
7173 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7174 isl_int min, isl_int max)
7177 struct isl_basic_set *bset = NULL;
7179 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7183 k = isl_basic_set_alloc_inequality(bset);
7186 isl_int_set_si(bset->ineq[k][1], 1);
7187 isl_int_neg(bset->ineq[k][0], min);
7189 k = isl_basic_set_alloc_inequality(bset);
7192 isl_int_set_si(bset->ineq[k][1], -1);
7193 isl_int_set(bset->ineq[k][0], max);
7197 isl_basic_set_free(bset);
7201 /* Return the Cartesian product of the basic sets in list (in the given order).
7203 __isl_give isl_basic_set *isl_basic_set_list_product(
7204 __isl_take struct isl_basic_set_list *list)
7212 struct isl_basic_set *product = NULL;
7216 isl_assert(list->ctx, list->n > 0, goto error);
7217 isl_assert(list->ctx, list->p[0], goto error);
7218 nparam = isl_basic_set_n_param(list->p[0]);
7219 dim = isl_basic_set_n_dim(list->p[0]);
7220 extra = list->p[0]->n_div;
7221 n_eq = list->p[0]->n_eq;
7222 n_ineq = list->p[0]->n_ineq;
7223 for (i = 1; i < list->n; ++i) {
7224 isl_assert(list->ctx, list->p[i], goto error);
7225 isl_assert(list->ctx,
7226 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7227 dim += isl_basic_set_n_dim(list->p[i]);
7228 extra += list->p[i]->n_div;
7229 n_eq += list->p[i]->n_eq;
7230 n_ineq += list->p[i]->n_ineq;
7232 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7237 for (i = 0; i < list->n; ++i) {
7238 isl_basic_set_add_constraints(product,
7239 isl_basic_set_copy(list->p[i]), dim);
7240 dim += isl_basic_set_n_dim(list->p[i]);
7242 isl_basic_set_list_free(list);
7245 isl_basic_set_free(product);
7246 isl_basic_set_list_free(list);
7250 struct isl_basic_map *isl_basic_map_product(
7251 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7253 struct isl_dim *dim_result = NULL;
7254 struct isl_basic_map *bmap;
7255 unsigned in1, in2, out1, out2, nparam, total, pos;
7256 struct isl_dim_map *dim_map1, *dim_map2;
7258 if (!bmap1 || !bmap2)
7261 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7262 bmap2->dim, isl_dim_param), goto error);
7263 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7264 isl_dim_copy(bmap2->dim));
7266 in1 = isl_basic_map_n_in(bmap1);
7267 in2 = isl_basic_map_n_in(bmap2);
7268 out1 = isl_basic_map_n_out(bmap1);
7269 out2 = isl_basic_map_n_out(bmap2);
7270 nparam = isl_basic_map_n_param(bmap1);
7272 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7273 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7274 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7275 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7276 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7277 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7278 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7279 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7280 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7281 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7282 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7284 bmap = isl_basic_map_alloc_dim(dim_result,
7285 bmap1->n_div + bmap2->n_div,
7286 bmap1->n_eq + bmap2->n_eq,
7287 bmap1->n_ineq + bmap2->n_ineq);
7288 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7289 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7290 bmap = isl_basic_map_simplify(bmap);
7291 return isl_basic_map_finalize(bmap);
7293 isl_basic_map_free(bmap1);
7294 isl_basic_map_free(bmap2);
7298 __isl_give isl_basic_map *isl_basic_map_flat_product(
7299 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7301 isl_basic_map *prod;
7303 prod = isl_basic_map_product(bmap1, bmap2);
7304 prod = isl_basic_map_flatten(prod);
7308 __isl_give isl_basic_set *isl_basic_set_flat_product(
7309 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7311 return isl_basic_map_flat_product(bset1, bset2);
7314 __isl_give isl_basic_map *isl_basic_map_range_product(
7315 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7317 isl_dim *dim_result = NULL;
7318 isl_basic_map *bmap;
7319 unsigned in, out1, out2, nparam, total, pos;
7320 struct isl_dim_map *dim_map1, *dim_map2;
7322 if (!bmap1 || !bmap2)
7325 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7326 isl_dim_copy(bmap2->dim));
7328 in = isl_basic_map_dim(bmap1, isl_dim_in);
7329 out1 = isl_basic_map_n_out(bmap1);
7330 out2 = isl_basic_map_n_out(bmap2);
7331 nparam = isl_basic_map_n_param(bmap1);
7333 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7334 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7335 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7336 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7337 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7338 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7339 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7340 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7341 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7342 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7343 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7345 bmap = isl_basic_map_alloc_dim(dim_result,
7346 bmap1->n_div + bmap2->n_div,
7347 bmap1->n_eq + bmap2->n_eq,
7348 bmap1->n_ineq + bmap2->n_ineq);
7349 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7350 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7351 bmap = isl_basic_map_simplify(bmap);
7352 return isl_basic_map_finalize(bmap);
7354 isl_basic_map_free(bmap1);
7355 isl_basic_map_free(bmap2);
7359 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7360 __isl_take isl_map *map2,
7361 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7362 __isl_take isl_dim *right),
7363 __isl_give isl_basic_map *(*basic_map_product)(
7364 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7367 struct isl_map *result;
7373 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7374 map2->dim, isl_dim_param), goto error);
7376 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7377 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7378 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7380 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7381 isl_dim_copy(map2->dim)),
7382 map1->n * map2->n, flags);
7385 for (i = 0; i < map1->n; ++i)
7386 for (j = 0; j < map2->n; ++j) {
7387 struct isl_basic_map *part;
7388 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7389 isl_basic_map_copy(map2->p[j]));
7390 if (isl_basic_map_is_empty(part))
7391 isl_basic_map_free(part);
7393 result = isl_map_add_basic_map(result, part);
7406 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7408 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7410 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7413 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7415 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7416 __isl_take isl_map *map2)
7420 prod = isl_map_product(map1, map2);
7421 prod = isl_map_flatten(prod);
7425 /* Given two set A and B, construct its Cartesian product A x B.
7427 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7429 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7430 (struct isl_map *)set2);
7433 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7434 __isl_take isl_set *set2)
7436 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7439 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7441 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7442 __isl_take isl_map *map2)
7444 return map_product(map1, map2, &isl_dim_range_product,
7445 &isl_basic_map_range_product);
7448 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7450 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
7451 __isl_take isl_map *map2)
7455 prod = isl_map_range_product(map1, map2);
7456 prod = isl_map_flatten_range(prod);
7460 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7463 uint32_t hash = isl_hash_init();
7468 bmap = isl_basic_map_copy(bmap);
7469 bmap = isl_basic_map_normalize(bmap);
7472 total = isl_basic_map_total_dim(bmap);
7473 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7474 for (i = 0; i < bmap->n_eq; ++i) {
7476 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7477 isl_hash_hash(hash, c_hash);
7479 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7480 for (i = 0; i < bmap->n_ineq; ++i) {
7482 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7483 isl_hash_hash(hash, c_hash);
7485 isl_hash_byte(hash, bmap->n_div & 0xFF);
7486 for (i = 0; i < bmap->n_div; ++i) {
7488 if (isl_int_is_zero(bmap->div[i][0]))
7490 isl_hash_byte(hash, i & 0xFF);
7491 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7492 isl_hash_hash(hash, c_hash);
7494 isl_basic_map_free(bmap);
7498 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7500 return isl_basic_map_get_hash((isl_basic_map *)bset);
7503 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7510 map = isl_map_copy(map);
7511 map = isl_map_normalize(map);
7515 hash = isl_hash_init();
7516 for (i = 0; i < map->n; ++i) {
7518 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7519 isl_hash_hash(hash, bmap_hash);
7527 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7529 return isl_map_get_hash((isl_map *)set);
7532 /* Check if the value for dimension dim is completely determined
7533 * by the values of the other parameters and variables.
7534 * That is, check if dimension dim is involved in an equality.
7536 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7543 nparam = isl_basic_set_n_param(bset);
7544 for (i = 0; i < bset->n_eq; ++i)
7545 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7550 /* Check if the value for dimension dim is completely determined
7551 * by the values of the other parameters and variables.
7552 * That is, check if dimension dim is involved in an equality
7553 * for each of the subsets.
7555 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7561 for (i = 0; i < set->n; ++i) {
7563 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7570 int isl_set_n_basic_set(__isl_keep isl_set *set)
7572 return set ? set->n : 0;
7575 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7576 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7583 for (i = 0; i < map->n; ++i)
7584 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7590 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7591 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7598 for (i = 0; i < set->n; ++i)
7599 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7605 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7607 struct isl_dim *dim;
7612 bset = isl_basic_set_cow(bset);
7616 dim = isl_basic_set_get_dim(bset);
7617 dim = isl_dim_lift(dim, bset->n_div);
7620 isl_dim_free(bset->dim);
7622 bset->extra -= bset->n_div;
7625 bset = isl_basic_set_finalize(bset);
7629 isl_basic_set_free(bset);
7633 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7636 struct isl_dim *dim;
7639 set = isl_set_align_divs(set);
7644 set = isl_set_cow(set);
7648 n_div = set->p[0]->n_div;
7649 dim = isl_set_get_dim(set);
7650 dim = isl_dim_lift(dim, n_div);
7653 isl_dim_free(set->dim);
7656 for (i = 0; i < set->n; ++i) {
7657 set->p[i] = isl_basic_set_lift(set->p[i]);
7668 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7670 struct isl_dim *dim;
7671 struct isl_basic_map *bmap;
7678 set = isl_set_align_divs(set);
7683 dim = isl_set_get_dim(set);
7684 if (set->n == 0 || set->p[0]->n_div == 0) {
7686 return isl_map_identity(isl_dim_map_from_set(dim));
7689 n_div = set->p[0]->n_div;
7690 dim = isl_dim_map_from_set(dim);
7691 n_param = isl_dim_size(dim, isl_dim_param);
7692 n_set = isl_dim_size(dim, isl_dim_in);
7693 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7694 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7695 for (i = 0; i < n_set; ++i)
7696 bmap = var_equal(bmap, i);
7698 total = n_param + n_set + n_set + n_div;
7699 for (i = 0; i < n_div; ++i) {
7700 k = isl_basic_map_alloc_inequality(bmap);
7703 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7704 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7705 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7706 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7707 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7708 set->p[0]->div[i][0]);
7710 l = isl_basic_map_alloc_inequality(bmap);
7713 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7714 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7715 set->p[0]->div[i][0]);
7716 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7720 bmap = isl_basic_map_simplify(bmap);
7721 bmap = isl_basic_map_finalize(bmap);
7722 return isl_map_from_basic_map(bmap);
7725 isl_basic_map_free(bmap);
7729 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7737 dim = isl_basic_set_total_dim(bset);
7738 size += bset->n_eq * (1 + dim);
7739 size += bset->n_ineq * (1 + dim);
7740 size += bset->n_div * (2 + dim);
7745 int isl_set_size(__isl_keep isl_set *set)
7753 for (i = 0; i < set->n; ++i)
7754 size += isl_basic_set_size(set->p[i]);
7759 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7760 enum isl_dim_type type, unsigned pos)
7768 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7770 pos += isl_basic_map_offset(bmap, type);
7772 for (i = 0; i < bmap->n_eq; ++i)
7773 if (!isl_int_is_zero(bmap->eq[i][pos]))
7777 for (i = 0; i < bmap->n_ineq; ++i) {
7778 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7785 return lower && upper;
7788 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7789 enum isl_dim_type type, unsigned pos)
7796 for (i = 0; i < map->n; ++i) {
7798 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7799 if (bounded < 0 || !bounded)
7806 /* Return 1 if the specified dim is involved in both an upper bound
7807 * and a lower bound.
7809 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7810 enum isl_dim_type type, unsigned pos)
7812 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7815 /* For each of the "n" variables starting at "first", determine
7816 * the sign of the variable and put the results in the first "n"
7817 * elements of the array "signs".
7819 * 1 means that the variable is non-negative
7820 * -1 means that the variable is non-positive
7821 * 0 means the variable attains both positive and negative values.
7823 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7824 unsigned first, unsigned n, int *signs)
7826 isl_vec *bound = NULL;
7827 struct isl_tab *tab = NULL;
7828 struct isl_tab_undo *snap;
7831 if (!bset || !signs)
7834 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7835 tab = isl_tab_from_basic_set(bset);
7839 isl_seq_clr(bound->el, bound->size);
7840 isl_int_set_si(bound->el[0], -1);
7842 snap = isl_tab_snap(tab);
7843 for (i = 0; i < n; ++i) {
7846 isl_int_set_si(bound->el[1 + first + i], -1);
7847 if (isl_tab_add_ineq(tab, bound->el) < 0)
7850 isl_int_set_si(bound->el[1 + first + i], 0);
7851 if (isl_tab_rollback(tab, snap) < 0)
7859 isl_int_set_si(bound->el[1 + first + i], 1);
7860 if (isl_tab_add_ineq(tab, bound->el) < 0)
7863 isl_int_set_si(bound->el[1 + first + i], 0);
7864 if (isl_tab_rollback(tab, snap) < 0)
7867 signs[i] = empty ? -1 : 0;
7871 isl_vec_free(bound);
7875 isl_vec_free(bound);
7879 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7880 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7882 if (!bset || !signs)
7884 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7887 first += pos(bset->dim, type) - 1;
7888 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7891 /* Check if the given basic map is obviously single-valued.
7892 * In particular, for each output dimension, check that there is
7893 * an equality that defines the output dimension in terms of
7894 * earlier dimensions.
7896 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
7906 total = 1 + isl_basic_map_total_dim(bmap);
7907 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7908 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7910 for (i = 0; i < n_out; ++i) {
7911 for (j = 0; j < bmap->n_eq; ++j) {
7912 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7914 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7915 total - (o_out + i + 1)) == -1)
7918 if (j >= bmap->n_eq)
7925 /* Check if the given map is obviously single-valued.
7927 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
7936 return isl_basic_map_plain_is_single_valued(map->p[0]);
7939 /* Check if the given map is single-valued.
7944 * and check if the result is a subset of the identity mapping.
7946 int isl_map_is_single_valued(__isl_keep isl_map *map)
7953 sv = isl_map_plain_is_single_valued(map);
7957 test = isl_map_reverse(isl_map_copy(map));
7958 test = isl_map_apply_range(test, isl_map_copy(map));
7960 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
7961 id = isl_map_identity(dim);
7963 sv = isl_map_is_subset(test, id);
7971 int isl_map_is_injective(__isl_keep isl_map *map)
7975 map = isl_map_copy(map);
7976 map = isl_map_reverse(map);
7977 in = isl_map_is_single_valued(map);
7983 /* Check if the given map is obviously injective.
7985 int isl_map_plain_is_injective(__isl_keep isl_map *map)
7989 map = isl_map_copy(map);
7990 map = isl_map_reverse(map);
7991 in = isl_map_plain_is_single_valued(map);
7997 int isl_map_is_bijective(__isl_keep isl_map *map)
8001 sv = isl_map_is_single_valued(map);
8005 return isl_map_is_injective(map);
8008 int isl_set_is_singleton(__isl_keep isl_set *set)
8010 return isl_map_is_single_valued((isl_map *)set);
8013 int isl_map_is_translation(__isl_keep isl_map *map)
8018 delta = isl_map_deltas(isl_map_copy(map));
8019 ok = isl_set_is_singleton(delta);
8020 isl_set_free(delta);
8025 static int unique(isl_int *p, unsigned pos, unsigned len)
8027 if (isl_seq_first_non_zero(p, pos) != -1)
8029 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8034 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8043 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8046 nvar = isl_basic_set_dim(bset, isl_dim_set);
8047 ovar = isl_dim_offset(bset->dim, isl_dim_set);
8048 for (j = 0; j < nvar; ++j) {
8049 int lower = 0, upper = 0;
8050 for (i = 0; i < bset->n_eq; ++i) {
8051 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8053 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8059 for (i = 0; i < bset->n_ineq; ++i) {
8060 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8062 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8064 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8069 if (!lower || !upper)
8076 int isl_set_is_box(__isl_keep isl_set *set)
8083 return isl_basic_set_is_box(set->p[0]);
8086 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8091 return isl_dim_is_wrapping(bset->dim);
8094 int isl_set_is_wrapping(__isl_keep isl_set *set)
8099 return isl_dim_is_wrapping(set->dim);
8102 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8104 bmap = isl_basic_map_cow(bmap);
8108 bmap->dim = isl_dim_wrap(bmap->dim);
8112 bmap = isl_basic_map_finalize(bmap);
8114 return (isl_basic_set *)bmap;
8116 isl_basic_map_free(bmap);
8120 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8124 map = isl_map_cow(map);
8128 for (i = 0; i < map->n; ++i) {
8129 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8133 map->dim = isl_dim_wrap(map->dim);
8137 return (isl_set *)map;
8143 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8145 bset = isl_basic_set_cow(bset);
8149 bset->dim = isl_dim_unwrap(bset->dim);
8153 bset = isl_basic_set_finalize(bset);
8155 return (isl_basic_map *)bset;
8157 isl_basic_set_free(bset);
8161 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8168 if (!isl_set_is_wrapping(set))
8169 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8172 set = isl_set_cow(set);
8176 for (i = 0; i < set->n; ++i) {
8177 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8182 set->dim = isl_dim_unwrap(set->dim);
8186 return (isl_map *)set;
8192 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8193 enum isl_dim_type type)
8198 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8201 bmap = isl_basic_map_cow(bmap);
8205 bmap->dim = isl_dim_reset(bmap->dim, type);
8209 bmap = isl_basic_map_finalize(bmap);
8213 isl_basic_map_free(bmap);
8217 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8218 enum isl_dim_type type)
8225 if (!isl_dim_is_named_or_nested(map->dim, type))
8228 map = isl_map_cow(map);
8232 for (i = 0; i < map->n; ++i) {
8233 map->p[i] = isl_basic_map_reset(map->p[i], type);
8237 map->dim = isl_dim_reset(map->dim, type);
8247 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8252 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8255 bmap = isl_basic_map_cow(bmap);
8259 bmap->dim = isl_dim_flatten(bmap->dim);
8263 bmap = isl_basic_map_finalize(bmap);
8267 isl_basic_map_free(bmap);
8271 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8273 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8276 __isl_give isl_basic_map *isl_basic_map_flatten_range(
8277 __isl_take isl_basic_map *bmap)
8282 if (!bmap->dim->nested[1])
8285 bmap = isl_basic_map_cow(bmap);
8289 bmap->dim = isl_dim_flatten_range(bmap->dim);
8293 bmap = isl_basic_map_finalize(bmap);
8297 isl_basic_map_free(bmap);
8301 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8308 if (!map->dim->nested[0] && !map->dim->nested[1])
8311 map = isl_map_cow(map);
8315 for (i = 0; i < map->n; ++i) {
8316 map->p[i] = isl_basic_map_flatten(map->p[i]);
8320 map->dim = isl_dim_flatten(map->dim);
8330 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8332 return (isl_set *)isl_map_flatten((isl_map *)set);
8335 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8337 isl_dim *dim, *flat_dim;
8340 dim = isl_set_get_dim(set);
8341 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8342 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8343 map = isl_map_intersect_domain(map, set);
8348 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
8355 if (!map->dim->nested[1])
8358 map = isl_map_cow(map);
8362 for (i = 0; i < map->n; ++i) {
8363 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
8367 map->dim = isl_dim_flatten_range(map->dim);
8377 /* Reorder the dimensions of "bmap" according to the given dim_map
8378 * and set the dimension specification to "dim".
8380 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8381 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8385 bmap = isl_basic_map_cow(bmap);
8386 if (!bmap || !dim || !dim_map)
8389 res = isl_basic_map_alloc_dim(dim,
8390 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8391 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8392 res = isl_basic_map_finalize(res);
8396 isl_basic_map_free(bmap);
8401 /* Reorder the dimensions of "map" according to given reordering.
8403 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8404 __isl_take isl_reordering *r)
8407 struct isl_dim_map *dim_map;
8409 map = isl_map_cow(map);
8410 dim_map = isl_dim_map_from_reordering(r);
8411 if (!map || !r || !dim_map)
8414 for (i = 0; i < map->n; ++i) {
8415 struct isl_dim_map *dim_map_i;
8417 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8419 map->p[i] = isl_basic_map_realign(map->p[i],
8420 isl_dim_copy(r->dim), dim_map_i);
8426 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8428 isl_reordering_free(r);
8434 isl_reordering_free(r);
8438 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8439 __isl_take isl_reordering *r)
8441 return (isl_set *)isl_map_realign((isl_map *)set, r);
8444 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
8445 __isl_take isl_dim *model)
8452 ctx = isl_dim_get_ctx(model);
8453 if (!isl_dim_has_named_params(model))
8454 isl_die(ctx, isl_error_invalid,
8455 "model has unnamed parameters", goto error);
8456 if (!isl_dim_has_named_params(map->dim))
8457 isl_die(ctx, isl_error_invalid,
8458 "relation has unnamed parameters", goto error);
8459 if (!isl_dim_match(map->dim, isl_dim_param, model, isl_dim_param)) {
8460 isl_reordering *exp;
8462 model = isl_dim_drop(model, isl_dim_in,
8463 0, isl_dim_size(model, isl_dim_in));
8464 model = isl_dim_drop(model, isl_dim_out,
8465 0, isl_dim_size(model, isl_dim_out));
8466 exp = isl_parameter_alignment_reordering(map->dim, model);
8467 exp = isl_reordering_extend_dim(exp, isl_map_get_dim(map));
8468 map = isl_map_realign(map, exp);
8471 isl_dim_free(model);
8474 isl_dim_free(model);
8479 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
8480 __isl_take isl_dim *model)
8482 return isl_map_align_params(set, model);
8485 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8486 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8487 enum isl_dim_type c2, enum isl_dim_type c3,
8488 enum isl_dim_type c4, enum isl_dim_type c5)
8490 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8491 struct isl_mat *mat;
8497 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8498 isl_basic_map_total_dim(bmap) + 1);
8501 for (i = 0; i < bmap->n_eq; ++i)
8502 for (j = 0, pos = 0; j < 5; ++j) {
8503 int off = isl_basic_map_offset(bmap, c[j]);
8504 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8505 isl_int_set(mat->row[i][pos],
8506 bmap->eq[i][off + k]);
8514 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8515 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8516 enum isl_dim_type c2, enum isl_dim_type c3,
8517 enum isl_dim_type c4, enum isl_dim_type c5)
8519 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8520 struct isl_mat *mat;
8526 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8527 isl_basic_map_total_dim(bmap) + 1);
8530 for (i = 0; i < bmap->n_ineq; ++i)
8531 for (j = 0, pos = 0; j < 5; ++j) {
8532 int off = isl_basic_map_offset(bmap, c[j]);
8533 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8534 isl_int_set(mat->row[i][pos],
8535 bmap->ineq[i][off + k]);
8543 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8544 __isl_take isl_dim *dim,
8545 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8546 enum isl_dim_type c2, enum isl_dim_type c3,
8547 enum isl_dim_type c4, enum isl_dim_type c5)
8549 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8550 isl_basic_map *bmap;
8556 if (!dim || !eq || !ineq)
8559 if (eq->n_col != ineq->n_col)
8560 isl_die(dim->ctx, isl_error_invalid,
8561 "equalities and inequalities matrices should have "
8562 "same number of columns", goto error);
8564 total = 1 + isl_dim_total(dim);
8566 if (eq->n_col < total)
8567 isl_die(dim->ctx, isl_error_invalid,
8568 "number of columns too small", goto error);
8570 extra = eq->n_col - total;
8572 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8573 eq->n_row, ineq->n_row);
8576 for (i = 0; i < extra; ++i) {
8577 k = isl_basic_map_alloc_div(bmap);
8580 isl_int_set_si(bmap->div[k][0], 0);
8582 for (i = 0; i < eq->n_row; ++i) {
8583 l = isl_basic_map_alloc_equality(bmap);
8586 for (j = 0, pos = 0; j < 5; ++j) {
8587 int off = isl_basic_map_offset(bmap, c[j]);
8588 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8589 isl_int_set(bmap->eq[l][off + k],
8595 for (i = 0; i < ineq->n_row; ++i) {
8596 l = isl_basic_map_alloc_inequality(bmap);
8599 for (j = 0, pos = 0; j < 5; ++j) {
8600 int off = isl_basic_map_offset(bmap, c[j]);
8601 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8602 isl_int_set(bmap->ineq[l][off + k],
8621 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8622 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8623 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8625 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8626 c1, c2, c3, c4, isl_dim_in);
8629 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8630 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8631 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8633 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8634 c1, c2, c3, c4, isl_dim_in);
8637 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8638 __isl_take isl_dim *dim,
8639 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8640 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8642 return (isl_basic_set*)
8643 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8644 c1, c2, c3, c4, isl_dim_in);
8647 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8652 return isl_dim_can_zip(bmap->dim);
8655 int isl_map_can_zip(__isl_keep isl_map *map)
8660 return isl_dim_can_zip(map->dim);
8663 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8664 * (A -> C) -> (B -> D).
8666 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8675 if (!isl_basic_map_can_zip(bmap))
8676 isl_die(bmap->ctx, isl_error_invalid,
8677 "basic map cannot be zipped", goto error);
8678 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8679 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8680 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8681 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8682 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8685 bmap->dim = isl_dim_zip(bmap->dim);
8690 isl_basic_map_free(bmap);
8694 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8695 * (A -> C) -> (B -> D).
8697 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8704 if (!isl_map_can_zip(map))
8705 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8708 map = isl_map_cow(map);
8712 for (i = 0; i < map->n; ++i) {
8713 map->p[i] = isl_basic_map_zip(map->p[i]);
8718 map->dim = isl_dim_zip(map->dim);