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
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
24 #include "isl_map_private.h"
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
30 #include <isl_mat_private.h>
32 /* Maps dst positions to src positions */
38 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
41 struct isl_dim_map *dim_map;
42 dim_map = isl_alloc(ctx, struct isl_dim_map,
43 sizeof(struct isl_dim_map) + len * sizeof(int));
46 dim_map->len = 1 + len;
48 for (i = 0; i < len; ++i)
49 dim_map->pos[1 + i] = -1;
53 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
56 case isl_dim_param: return dim->nparam;
57 case isl_dim_in: return dim->n_in;
58 case isl_dim_out: return dim->n_out;
59 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
64 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
67 case isl_dim_param: return 1;
68 case isl_dim_in: return 1 + dim->nparam;
69 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
74 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
75 struct isl_dim *dim, enum isl_dim_type type,
76 unsigned first, unsigned n, unsigned dst_pos)
84 src_pos = pos(dim, type);
85 for (i = 0; i < n; ++i)
86 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
89 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
90 enum isl_dim_type type, unsigned dst_pos)
92 isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
95 static void isl_dim_map_div(struct isl_dim_map *dim_map,
96 struct isl_basic_map *bmap, unsigned dst_pos)
101 if (!dim_map || !bmap)
104 src_pos = 1 + isl_dim_total(bmap->dim);
105 for (i = 0; i < bmap->n_div; ++i)
106 dim_map->pos[1 + dst_pos + i] = src_pos + i;
109 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
113 for (i = 0; i < dim_map->len; ++i)
114 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
115 fprintf(stderr, "\n");
118 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
119 enum isl_dim_type type)
124 case isl_dim_cst: return 1;
127 case isl_dim_out: return isl_dim_size(bmap->dim, type);
128 case isl_dim_div: return bmap->n_div;
129 case isl_dim_all: return isl_basic_map_total_dim(bmap);
134 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
136 return map ? n(map->dim, type) : 0;
139 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
141 return set ? n(set->dim, type) : 0;
144 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
145 enum isl_dim_type type)
147 struct isl_dim *dim = bmap->dim;
149 case isl_dim_cst: return 0;
150 case isl_dim_param: return 1;
151 case isl_dim_in: return 1 + dim->nparam;
152 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
153 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
158 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
160 return pos(map->dim, type);
163 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
164 enum isl_dim_type type)
166 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
169 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
171 return isl_basic_set_dim(bset, isl_dim_set);
174 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
176 return isl_basic_set_dim(bset, isl_dim_param);
179 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
181 return isl_dim_total(bset->dim) + bset->n_div;
184 unsigned isl_set_n_dim(const struct isl_set *set)
186 return isl_set_dim(set, isl_dim_set);
189 unsigned isl_set_n_param(const struct isl_set *set)
191 return isl_set_dim(set, isl_dim_param);
194 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
196 return bmap ? bmap->dim->n_in : 0;
199 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
201 return bmap ? bmap->dim->n_out : 0;
204 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
206 return bmap ? bmap->dim->nparam : 0;
209 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
211 return bmap ? bmap->n_div : 0;
214 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
216 return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
219 unsigned isl_map_n_in(const struct isl_map *map)
221 return map->dim->n_in;
224 unsigned isl_map_n_out(const struct isl_map *map)
226 return map->dim->n_out;
229 unsigned isl_map_n_param(const struct isl_map *map)
231 return map->dim->nparam;
234 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
239 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
242 return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
245 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
246 struct isl_basic_set *bset)
251 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
254 return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
257 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
258 struct isl_basic_set *bset)
263 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
266 return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
269 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
271 return bmap ? bmap->ctx : NULL;
274 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
276 return bset ? bset->ctx : NULL;
279 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
281 return map ? map->ctx : NULL;
284 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
286 return set ? set->ctx : NULL;
289 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
293 return isl_dim_copy(bmap->dim);
296 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
300 return isl_dim_copy(bset->dim);
303 struct isl_dim *isl_map_get_dim(struct isl_map *map)
307 return isl_dim_copy(map->dim);
310 struct isl_dim *isl_set_get_dim(struct isl_set *set)
314 return isl_dim_copy(set->dim);
317 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
318 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
320 bmap = isl_basic_map_cow(bmap);
323 bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
328 isl_basic_map_free(bmap);
332 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
333 enum isl_dim_type type)
335 return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
338 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
339 enum isl_dim_type type, const char *s)
343 map = isl_map_cow(map);
347 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
351 for (i = 0; i < map->n; ++i) {
352 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
363 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
364 enum isl_dim_type type)
366 return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
369 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
372 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
375 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
377 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
380 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
381 enum isl_dim_type type, unsigned pos)
383 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
386 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
387 enum isl_dim_type type, unsigned pos)
389 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
392 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
393 enum isl_dim_type type, unsigned pos)
395 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
398 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
399 __isl_take isl_basic_map *bmap,
400 enum isl_dim_type type, unsigned pos, const char *s)
404 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
409 isl_basic_map_free(bmap);
413 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
414 enum isl_dim_type type, unsigned pos, const char *s)
421 map->dim = isl_dim_set_name(map->dim, type, pos, s);
425 for (i = 0; i < map->n; ++i) {
426 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
437 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
438 __isl_take isl_basic_set *bset,
439 enum isl_dim_type type, unsigned pos, const char *s)
441 return (isl_basic_set *)isl_basic_map_set_dim_name(
442 (isl_basic_map *)bset, type, pos, s);
445 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
446 enum isl_dim_type type, unsigned pos, const char *s)
448 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
451 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
455 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
458 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
459 struct isl_basic_map *bmap, unsigned extra,
460 unsigned n_eq, unsigned n_ineq)
463 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
468 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
469 if (isl_blk_is_error(bmap->block))
472 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
477 bmap->block2 = isl_blk_empty();
480 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
481 if (isl_blk_is_error(bmap->block2))
484 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
489 for (i = 0; i < n_ineq + n_eq; ++i)
490 bmap->ineq[i] = bmap->block.data + i * row_size;
492 for (i = 0; i < extra; ++i)
493 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
497 bmap->c_size = n_eq + n_ineq;
498 bmap->eq = bmap->ineq + n_ineq;
507 isl_basic_map_free(bmap);
511 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
512 unsigned nparam, unsigned dim, unsigned extra,
513 unsigned n_eq, unsigned n_ineq)
515 struct isl_basic_map *bmap;
516 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
517 return (struct isl_basic_set *)bmap;
520 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
521 unsigned extra, unsigned n_eq, unsigned n_ineq)
523 struct isl_basic_map *bmap;
526 isl_assert(dim->ctx, dim->n_in == 0, goto error);
527 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
528 return (struct isl_basic_set *)bmap;
534 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
535 unsigned extra, unsigned n_eq, unsigned n_ineq)
537 struct isl_basic_map *bmap;
541 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
546 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
552 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
553 unsigned nparam, unsigned in, unsigned out, unsigned extra,
554 unsigned n_eq, unsigned n_ineq)
556 struct isl_basic_map *bmap;
559 dim = isl_dim_alloc(ctx, nparam, in, out);
563 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
567 static void dup_constraints(
568 struct isl_basic_map *dst, struct isl_basic_map *src)
571 unsigned total = isl_basic_map_total_dim(src);
573 for (i = 0; i < src->n_eq; ++i) {
574 int j = isl_basic_map_alloc_equality(dst);
575 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
578 for (i = 0; i < src->n_ineq; ++i) {
579 int j = isl_basic_map_alloc_inequality(dst);
580 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
583 for (i = 0; i < src->n_div; ++i) {
584 int j = isl_basic_map_alloc_div(dst);
585 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
587 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
590 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
592 struct isl_basic_map *dup;
596 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
597 bmap->n_div, bmap->n_eq, bmap->n_ineq);
600 dup_constraints(dup, bmap);
601 dup->flags = bmap->flags;
602 dup->sample = isl_vec_copy(bmap->sample);
606 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
608 struct isl_basic_map *dup;
610 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
611 return (struct isl_basic_set *)dup;
614 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
619 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
623 return isl_basic_set_dup(bset);
626 struct isl_set *isl_set_copy(struct isl_set *set)
635 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
640 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
644 bmap = isl_basic_map_dup(bmap);
646 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
650 struct isl_map *isl_map_copy(struct isl_map *map)
659 void isl_basic_map_free(struct isl_basic_map *bmap)
667 isl_ctx_deref(bmap->ctx);
669 isl_blk_free(bmap->ctx, bmap->block2);
671 isl_blk_free(bmap->ctx, bmap->block);
672 isl_vec_free(bmap->sample);
673 isl_dim_free(bmap->dim);
677 void isl_basic_set_free(struct isl_basic_set *bset)
679 isl_basic_map_free((struct isl_basic_map *)bset);
682 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
684 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
687 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
693 isl_assert(ctx, room_for_con(bmap, 1), return -1);
694 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
696 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
697 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
698 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
699 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
700 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
701 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
703 int j = isl_basic_map_alloc_inequality(bmap);
707 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
708 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
715 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
716 bmap->extra - bmap->n_div);
720 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
722 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
725 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
729 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
734 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
736 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
739 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
744 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
746 if (pos != bmap->n_eq - 1) {
748 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
749 bmap->eq[bmap->n_eq - 1] = t;
755 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
757 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
760 void isl_basic_map_inequality_to_equality(
761 struct isl_basic_map *bmap, unsigned pos)
766 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
767 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
772 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
773 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
774 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
775 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
778 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
780 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
783 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
789 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
790 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
791 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
792 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
793 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
794 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
795 1 + isl_basic_map_total_dim(bmap),
796 bmap->extra - bmap->n_div);
797 return bmap->n_ineq++;
800 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
802 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
805 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
809 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
814 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
816 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
819 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
824 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
826 if (pos != bmap->n_ineq - 1) {
828 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
829 bmap->ineq[bmap->n_ineq - 1] = t;
830 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
836 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
838 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
841 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
846 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
849 k = isl_basic_map_alloc_equality(bmap);
852 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
855 isl_basic_map_free(bmap);
859 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
862 return (isl_basic_set *)
863 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
866 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
871 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
874 k = isl_basic_map_alloc_inequality(bmap);
877 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
880 isl_basic_map_free(bmap);
884 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
887 return (isl_basic_set *)
888 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
891 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
895 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
896 isl_seq_clr(bmap->div[bmap->n_div] +
897 1 + 1 + isl_basic_map_total_dim(bmap),
898 bmap->extra - bmap->n_div);
899 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
900 return bmap->n_div++;
903 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
905 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
908 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
912 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
917 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
919 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
922 /* Copy constraint from src to dst, putting the vars of src at offset
923 * dim_off in dst and the divs of src at offset div_off in dst.
924 * If both sets are actually map, then dim_off applies to the input
927 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
928 struct isl_basic_map *src_map, isl_int *src,
929 unsigned in_off, unsigned out_off, unsigned div_off)
931 unsigned src_nparam = isl_basic_map_n_param(src_map);
932 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
933 unsigned src_in = isl_basic_map_n_in(src_map);
934 unsigned dst_in = isl_basic_map_n_in(dst_map);
935 unsigned src_out = isl_basic_map_n_out(src_map);
936 unsigned dst_out = isl_basic_map_n_out(dst_map);
937 isl_int_set(dst[0], src[0]);
938 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
939 if (dst_nparam > src_nparam)
940 isl_seq_clr(dst+1+src_nparam,
941 dst_nparam - src_nparam);
942 isl_seq_clr(dst+1+dst_nparam, in_off);
943 isl_seq_cpy(dst+1+dst_nparam+in_off,
945 isl_min(dst_in-in_off, src_in));
946 if (dst_in-in_off > src_in)
947 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
948 dst_in - in_off - src_in);
949 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
950 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
951 src+1+src_nparam+src_in,
952 isl_min(dst_out-out_off, src_out));
953 if (dst_out-out_off > src_out)
954 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
955 dst_out - out_off - src_out);
956 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
957 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
958 src+1+src_nparam+src_in+src_out,
959 isl_min(dst_map->extra-div_off, src_map->n_div));
960 if (dst_map->n_div-div_off > src_map->n_div)
961 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
962 div_off+src_map->n_div,
963 dst_map->n_div - div_off - src_map->n_div);
966 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
967 struct isl_basic_map *src_map, isl_int *src,
968 unsigned in_off, unsigned out_off, unsigned div_off)
970 isl_int_set(dst[0], src[0]);
971 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
974 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
975 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
980 if (!bmap1 || !bmap2)
983 div_off = bmap1->n_div;
985 for (i = 0; i < bmap2->n_eq; ++i) {
986 int i1 = isl_basic_map_alloc_equality(bmap1);
989 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
990 i_pos, o_pos, div_off);
993 for (i = 0; i < bmap2->n_ineq; ++i) {
994 int i1 = isl_basic_map_alloc_inequality(bmap1);
997 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
998 i_pos, o_pos, div_off);
1001 for (i = 0; i < bmap2->n_div; ++i) {
1002 int i1 = isl_basic_map_alloc_div(bmap1);
1005 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1006 i_pos, o_pos, div_off);
1009 isl_basic_map_free(bmap2);
1014 isl_basic_map_free(bmap1);
1015 isl_basic_map_free(bmap2);
1019 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
1020 struct isl_dim_map *dim_map)
1024 for (i = 0; i < dim_map->len; ++i) {
1025 if (dim_map->pos[i] < 0)
1026 isl_int_set_si(dst[i], 0);
1028 isl_int_set(dst[i], src[dim_map->pos[i]]);
1032 static void copy_div_dim_map(isl_int *dst, isl_int *src,
1033 struct isl_dim_map *dim_map)
1035 isl_int_set(dst[0], src[0]);
1036 copy_constraint_dim_map(dst+1, src+1, dim_map);
1039 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
1040 struct isl_basic_map *src, struct isl_dim_map *dim_map)
1044 if (!src || !dst || !dim_map)
1047 for (i = 0; i < src->n_eq; ++i) {
1048 int i1 = isl_basic_map_alloc_equality(dst);
1051 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
1054 for (i = 0; i < src->n_ineq; ++i) {
1055 int i1 = isl_basic_map_alloc_inequality(dst);
1058 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
1061 for (i = 0; i < src->n_div; ++i) {
1062 int i1 = isl_basic_map_alloc_div(dst);
1065 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
1069 isl_basic_map_free(src);
1074 isl_basic_map_free(src);
1075 isl_basic_map_free(dst);
1079 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1080 struct isl_basic_set *bset2, unsigned pos)
1082 return (struct isl_basic_set *)
1083 add_constraints((struct isl_basic_map *)bset1,
1084 (struct isl_basic_map *)bset2, 0, pos);
1087 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1088 struct isl_dim *dim, unsigned extra,
1089 unsigned n_eq, unsigned n_ineq)
1091 struct isl_basic_map *ext;
1101 dims_ok = isl_dim_equal(base->dim, dim) &&
1102 base->extra >= base->n_div + extra;
1104 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1105 room_for_ineq(base, n_ineq)) {
1110 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1111 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1112 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1113 extra += base->extra;
1115 n_ineq += base->n_ineq;
1117 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1123 ext->sample = isl_vec_copy(base->sample);
1124 flags = base->flags;
1125 ext = add_constraints(ext, base, 0, 0);
1128 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1135 isl_basic_map_free(base);
1139 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1140 struct isl_dim *dim, unsigned extra,
1141 unsigned n_eq, unsigned n_ineq)
1143 return (struct isl_basic_set *)
1144 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1145 extra, n_eq, n_ineq);
1148 struct isl_basic_map *isl_basic_map_extend_constraints(
1149 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1153 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1157 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1158 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1159 unsigned n_eq, unsigned n_ineq)
1161 struct isl_basic_map *bmap;
1162 struct isl_dim *dim;
1166 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1170 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1173 isl_basic_map_free(base);
1177 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1178 unsigned nparam, unsigned dim, unsigned extra,
1179 unsigned n_eq, unsigned n_ineq)
1181 return (struct isl_basic_set *)
1182 isl_basic_map_extend((struct isl_basic_map *)base,
1183 nparam, 0, dim, extra, n_eq, n_ineq);
1186 struct isl_basic_set *isl_basic_set_extend_constraints(
1187 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1189 return (struct isl_basic_set *)
1190 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1194 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1196 return (struct isl_basic_set *)
1197 isl_basic_map_cow((struct isl_basic_map *)bset);
1200 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1205 if (bmap->ref > 1) {
1207 bmap = isl_basic_map_dup(bmap);
1210 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1214 struct isl_set *isl_set_cow(struct isl_set *set)
1222 return isl_set_dup(set);
1225 struct isl_map *isl_map_cow(struct isl_map *map)
1233 return isl_map_dup(map);
1236 static void swap_vars(struct isl_blk blk, isl_int *a,
1237 unsigned a_len, unsigned b_len)
1239 isl_seq_cpy(blk.data, a+a_len, b_len);
1240 isl_seq_cpy(blk.data+b_len, a, a_len);
1241 isl_seq_cpy(a, blk.data, b_len+a_len);
1244 struct isl_basic_set *isl_basic_set_swap_vars(
1245 struct isl_basic_set *bset, unsigned n)
1255 nparam = isl_basic_set_n_param(bset);
1256 dim = isl_basic_set_n_dim(bset);
1257 isl_assert(bset->ctx, n <= dim, goto error);
1262 bset = isl_basic_set_cow(bset);
1266 blk = isl_blk_alloc(bset->ctx, dim);
1267 if (isl_blk_is_error(blk))
1270 for (i = 0; i < bset->n_eq; ++i)
1272 bset->eq[i]+1+nparam, n, dim - n);
1274 for (i = 0; i < bset->n_ineq; ++i)
1276 bset->ineq[i]+1+nparam, n, dim - n);
1278 for (i = 0; i < bset->n_div; ++i)
1280 bset->div[i]+1+1+nparam, n, dim - n);
1282 isl_blk_free(bset->ctx, blk);
1284 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1285 bset = isl_basic_set_gauss(bset, NULL);
1286 return isl_basic_set_finalize(bset);
1288 isl_basic_set_free(bset);
1292 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1295 set = isl_set_cow(set);
1299 for (i = 0; i < set->n; ++i) {
1300 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1306 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1310 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1316 total = isl_basic_map_total_dim(bmap);
1317 isl_basic_map_free_div(bmap, bmap->n_div);
1318 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1320 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1322 i = isl_basic_map_alloc_equality(bmap);
1326 isl_int_set_si(bmap->eq[i][0], 1);
1327 isl_seq_clr(bmap->eq[i]+1, total);
1328 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1329 isl_vec_free(bmap->sample);
1330 bmap->sample = NULL;
1331 return isl_basic_map_finalize(bmap);
1333 isl_basic_map_free(bmap);
1337 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1339 return (struct isl_basic_set *)
1340 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1343 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1346 unsigned off = isl_dim_total(bmap->dim);
1347 isl_int *t = bmap->div[a];
1348 bmap->div[a] = bmap->div[b];
1351 for (i = 0; i < bmap->n_eq; ++i)
1352 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1354 for (i = 0; i < bmap->n_ineq; ++i)
1355 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1357 for (i = 0; i < bmap->n_div; ++i)
1358 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1359 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1362 /* Eliminate the specified n dimensions starting at first from the
1363 * constraints using Fourier-Motzkin. The dimensions themselves
1366 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1367 enum isl_dim_type type, unsigned first, unsigned n)
1376 map = isl_map_cow(map);
1379 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1380 first += pos(map->dim, type) - 1;
1382 for (i = 0; i < map->n; ++i) {
1383 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1393 /* Eliminate the specified n dimensions starting at first from the
1394 * constraints using Fourier-Motzkin. The dimensions themselves
1397 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1398 enum isl_dim_type type, unsigned first, unsigned n)
1400 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1403 /* Eliminate the specified n dimensions starting at first from the
1404 * constraints using Fourier-Motzkin. The dimensions themselves
1407 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1408 unsigned first, unsigned n)
1410 return isl_set_eliminate(set, isl_dim_set, first, n);
1413 /* Project out n dimensions starting at first using Fourier-Motzkin */
1414 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1415 unsigned first, unsigned n)
1417 set = isl_set_eliminate_dims(set, first, n);
1418 set = isl_set_drop_dims(set, first, n);
1422 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1423 __isl_take isl_basic_map *bmap)
1425 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1433 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1434 __isl_take isl_basic_set *bset)
1436 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1437 (struct isl_basic_map *)bset);
1440 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1449 set = isl_set_cow(set);
1453 for (i = 0; i < set->n; ++i) {
1454 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1464 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1465 enum isl_dim_type type, unsigned first, unsigned n)
1469 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1473 bmap = isl_basic_map_eliminate_vars(bmap,
1474 isl_basic_map_offset(bmap, type) - 1 + first, n);
1477 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1479 bmap = isl_basic_map_drop(bmap, type, first, n);
1482 isl_basic_map_free(bmap);
1486 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1487 enum isl_dim_type type, unsigned first, unsigned n)
1489 return (isl_basic_set *)
1490 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1493 struct isl_map *isl_map_remove(struct isl_map *map,
1494 enum isl_dim_type type, unsigned first, unsigned n)
1501 map = isl_map_cow(map);
1504 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1506 for (i = 0; i < map->n; ++i) {
1507 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1508 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1512 map = isl_map_drop(map, type, first, n);
1519 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1520 enum isl_dim_type type, unsigned first, unsigned n)
1522 return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1525 /* Project out n inputs starting at first using Fourier-Motzkin */
1526 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1527 unsigned first, unsigned n)
1529 return isl_map_remove(map, isl_dim_in, first, n);
1532 /* Project out n dimensions starting at first using Fourier-Motzkin */
1533 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1534 unsigned first, unsigned n)
1536 unsigned nparam = isl_basic_set_n_param(bset);
1537 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1538 bset = isl_basic_set_drop_dims(bset, first, n);
1542 static void dump_term(struct isl_basic_map *bmap,
1543 isl_int c, int pos, FILE *out)
1546 unsigned in = isl_basic_map_n_in(bmap);
1547 unsigned dim = in + isl_basic_map_n_out(bmap);
1548 unsigned nparam = isl_basic_map_n_param(bmap);
1550 isl_int_print(out, c, 0);
1552 if (!isl_int_is_one(c))
1553 isl_int_print(out, c, 0);
1554 if (pos < 1 + nparam) {
1555 name = isl_dim_get_name(bmap->dim,
1556 isl_dim_param, pos - 1);
1558 fprintf(out, "%s", name);
1560 fprintf(out, "p%d", pos - 1);
1561 } else if (pos < 1 + nparam + in)
1562 fprintf(out, "i%d", pos - 1 - nparam);
1563 else if (pos < 1 + nparam + dim)
1564 fprintf(out, "o%d", pos - 1 - nparam - in);
1566 fprintf(out, "e%d", pos - 1 - nparam - dim);
1570 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1571 int sign, FILE *out)
1575 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1579 for (i = 0, first = 1; i < len; ++i) {
1580 if (isl_int_sgn(c[i]) * sign <= 0)
1583 fprintf(out, " + ");
1585 isl_int_abs(v, c[i]);
1586 dump_term(bmap, v, i, out);
1593 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1594 const char *op, FILE *out, int indent)
1598 fprintf(out, "%*s", indent, "");
1600 dump_constraint_sign(bmap, c, 1, out);
1601 fprintf(out, " %s ", op);
1602 dump_constraint_sign(bmap, c, -1, out);
1606 for (i = bmap->n_div; i < bmap->extra; ++i) {
1607 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1609 fprintf(out, "%*s", indent, "");
1610 fprintf(out, "ERROR: unused div coefficient not zero\n");
1615 static void dump_constraints(struct isl_basic_map *bmap,
1616 isl_int **c, unsigned n,
1617 const char *op, FILE *out, int indent)
1621 for (i = 0; i < n; ++i)
1622 dump_constraint(bmap, c[i], op, out, indent);
1625 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1629 unsigned total = isl_basic_map_total_dim(bmap);
1631 for (j = 0; j < 1 + total; ++j) {
1632 if (isl_int_is_zero(exp[j]))
1634 if (!first && isl_int_is_pos(exp[j]))
1636 dump_term(bmap, exp[j], j, out);
1641 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1645 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1646 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1648 for (i = 0; i < bmap->n_div; ++i) {
1649 fprintf(out, "%*s", indent, "");
1650 fprintf(out, "e%d = [(", i);
1651 dump_affine(bmap, bmap->div[i]+1, out);
1653 isl_int_print(out, bmap->div[i][0], 0);
1654 fprintf(out, "]\n");
1658 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1661 fprintf(out, "null basic set\n");
1665 fprintf(out, "%*s", indent, "");
1666 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1667 bset->ref, bset->dim->nparam, bset->dim->n_out,
1668 bset->extra, bset->flags);
1669 dump((struct isl_basic_map *)bset, out, indent);
1672 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1675 fprintf(out, "null basic map\n");
1679 fprintf(out, "%*s", indent, "");
1680 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1681 "flags: %x, n_name: %d\n",
1683 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1684 bmap->extra, bmap->flags, bmap->dim->n_name);
1685 dump(bmap, out, indent);
1688 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1693 total = isl_basic_map_total_dim(bmap);
1694 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1695 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1696 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1697 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1701 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1703 struct isl_set *set;
1707 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1708 isl_assert(dim->ctx, n >= 0, goto error);
1709 set = isl_alloc(dim->ctx, struct isl_set,
1710 sizeof(struct isl_set) +
1711 (n - 1) * sizeof(struct isl_basic_set *));
1715 set->ctx = dim->ctx;
1716 isl_ctx_ref(set->ctx);
1728 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1729 unsigned nparam, unsigned dim, int n, unsigned flags)
1731 struct isl_set *set;
1732 struct isl_dim *dims;
1734 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1738 set = isl_set_alloc_dim(dims, n, flags);
1742 /* Make sure "map" has room for at least "n" more basic maps.
1744 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1747 struct isl_map *grown = NULL;
1751 isl_assert(map->ctx, n >= 0, goto error);
1752 if (map->n + n <= map->size)
1754 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1757 for (i = 0; i < map->n; ++i) {
1758 grown->p[i] = isl_basic_map_copy(map->p[i]);
1766 isl_map_free(grown);
1771 /* Make sure "set" has room for at least "n" more basic sets.
1773 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1775 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1778 struct isl_set *isl_set_dup(struct isl_set *set)
1781 struct isl_set *dup;
1786 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1789 for (i = 0; i < set->n; ++i)
1790 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1794 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1796 struct isl_set *set;
1801 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1803 isl_basic_set_free(bset);
1806 return isl_set_add_basic_set(set, bset);
1809 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1811 struct isl_map *map;
1816 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1818 isl_basic_map_free(bmap);
1821 return isl_map_add_basic_map(map, bmap);
1824 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1825 __isl_take isl_basic_set *bset)
1827 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1828 (struct isl_basic_map *)bset);
1831 void isl_set_free(struct isl_set *set)
1841 isl_ctx_deref(set->ctx);
1842 for (i = 0; i < set->n; ++i)
1843 isl_basic_set_free(set->p[i]);
1844 isl_dim_free(set->dim);
1848 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1853 fprintf(out, "null set\n");
1857 fprintf(out, "%*s", indent, "");
1858 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1859 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1861 for (i = 0; i < set->n; ++i) {
1862 fprintf(out, "%*s", indent, "");
1863 fprintf(out, "basic set %d:\n", i);
1864 isl_basic_set_dump(set->p[i], out, indent+4);
1868 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1873 fprintf(out, "null map\n");
1877 fprintf(out, "%*s", indent, "");
1878 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1879 "flags: %x, n_name: %d\n",
1880 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1881 map->dim->n_out, map->flags, map->dim->n_name);
1882 for (i = 0; i < map->n; ++i) {
1883 fprintf(out, "%*s", indent, "");
1884 fprintf(out, "basic map %d:\n", i);
1885 isl_basic_map_dump(map->p[i], out, indent+4);
1889 struct isl_basic_map *isl_basic_map_intersect_domain(
1890 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1892 struct isl_basic_map *bmap_domain;
1893 struct isl_dim *dim;
1898 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1899 bset->dim, isl_dim_param), goto error);
1901 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1902 isl_assert(bset->ctx,
1903 isl_basic_map_compatible_domain(bmap, bset), goto error);
1905 bmap = isl_basic_map_cow(bmap);
1908 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1909 bset->n_div, bset->n_eq, bset->n_ineq);
1910 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1911 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1912 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1914 bmap = isl_basic_map_simplify(bmap);
1915 return isl_basic_map_finalize(bmap);
1917 isl_basic_map_free(bmap);
1918 isl_basic_set_free(bset);
1922 struct isl_basic_map *isl_basic_map_intersect_range(
1923 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1925 struct isl_basic_map *bmap_range;
1930 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1931 bset->dim, isl_dim_param), goto error);
1933 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1934 isl_assert(bset->ctx,
1935 isl_basic_map_compatible_range(bmap, bset), goto error);
1937 bmap = isl_basic_map_cow(bmap);
1940 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1941 bset->n_div, bset->n_eq, bset->n_ineq);
1942 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1943 bmap = add_constraints(bmap, bmap_range, 0, 0);
1945 bmap = isl_basic_map_simplify(bmap);
1946 return isl_basic_map_finalize(bmap);
1948 isl_basic_map_free(bmap);
1949 isl_basic_set_free(bset);
1953 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1959 total = 1 + isl_basic_map_total_dim(bmap);
1960 if (total != vec->size)
1965 for (i = 0; i < bmap->n_eq; ++i) {
1966 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1967 if (!isl_int_is_zero(s)) {
1973 for (i = 0; i < bmap->n_ineq; ++i) {
1974 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1975 if (isl_int_is_neg(s)) {
1986 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1988 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1991 struct isl_basic_map *isl_basic_map_intersect(
1992 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1994 struct isl_vec *sample = NULL;
1996 if (!bmap1 || !bmap2)
1999 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2000 bmap2->dim, isl_dim_param), goto error);
2001 if (isl_dim_total(bmap1->dim) ==
2002 isl_dim_size(bmap1->dim, isl_dim_param) &&
2003 isl_dim_total(bmap2->dim) !=
2004 isl_dim_size(bmap2->dim, isl_dim_param))
2005 return isl_basic_map_intersect(bmap2, bmap1);
2007 if (isl_dim_total(bmap2->dim) !=
2008 isl_dim_size(bmap2->dim, isl_dim_param))
2009 isl_assert(bmap1->ctx,
2010 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2012 if (bmap1->sample &&
2013 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2014 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2015 sample = isl_vec_copy(bmap1->sample);
2016 else if (bmap2->sample &&
2017 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2018 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2019 sample = isl_vec_copy(bmap2->sample);
2021 bmap1 = isl_basic_map_cow(bmap1);
2024 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2025 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2026 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2029 isl_vec_free(sample);
2031 isl_vec_free(bmap1->sample);
2032 bmap1->sample = sample;
2035 bmap1 = isl_basic_map_simplify(bmap1);
2036 return isl_basic_map_finalize(bmap1);
2039 isl_vec_free(sample);
2040 isl_basic_map_free(bmap1);
2041 isl_basic_map_free(bmap2);
2045 struct isl_basic_set *isl_basic_set_intersect(
2046 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2048 return (struct isl_basic_set *)
2049 isl_basic_map_intersect(
2050 (struct isl_basic_map *)bset1,
2051 (struct isl_basic_map *)bset2);
2054 /* Special case of isl_map_intersect, where both map1 and map2
2055 * are convex, without any divs and such that either map1 or map2
2056 * contains a single constraint. This constraint is then simply
2057 * added to the other map.
2059 static __isl_give isl_map *map_intersect_add_constraint(
2060 __isl_take isl_map *map1, __isl_take isl_map *map2)
2062 struct isl_basic_map *bmap1;
2063 struct isl_basic_map *bmap2;
2065 isl_assert(map1->ctx, map1->n == 1, goto error);
2066 isl_assert(map2->ctx, map1->n == 1, goto error);
2067 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2068 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2070 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2071 return isl_map_intersect(map2, map1);
2073 isl_assert(map2->ctx,
2074 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2076 map1 = isl_map_cow(map1);
2079 if (isl_map_fast_is_empty(map1)) {
2083 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2084 if (map2->p[0]->n_eq == 1)
2085 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2087 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2088 map2->p[0]->ineq[0]);
2090 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2091 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2095 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2096 isl_basic_map_free(map1->p[0]);
2109 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2112 struct isl_map *result;
2118 if (isl_map_fast_is_empty(map1)) {
2122 if (isl_map_fast_is_empty(map2)) {
2127 if (map1->n == 1 && map2->n == 1 &&
2128 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2129 isl_dim_equal(map1->dim, map2->dim) &&
2130 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2131 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2132 return map_intersect_add_constraint(map1, map2);
2133 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2134 map2->dim, isl_dim_param), goto error);
2135 if (isl_dim_total(map1->dim) ==
2136 isl_dim_size(map1->dim, isl_dim_param) &&
2137 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2138 return isl_map_intersect(map2, map1);
2140 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2141 isl_assert(map1->ctx,
2142 isl_dim_equal(map1->dim, map2->dim), goto error);
2144 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2145 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2146 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2148 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2149 map1->n * map2->n, flags);
2152 for (i = 0; i < map1->n; ++i)
2153 for (j = 0; j < map2->n; ++j) {
2154 struct isl_basic_map *part;
2155 part = isl_basic_map_intersect(
2156 isl_basic_map_copy(map1->p[i]),
2157 isl_basic_map_copy(map2->p[j]));
2158 if (isl_basic_map_is_empty(part))
2159 isl_basic_map_free(part);
2161 result = isl_map_add_basic_map(result, part);
2174 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2176 return (struct isl_set *)
2177 isl_map_intersect((struct isl_map *)set1,
2178 (struct isl_map *)set2);
2181 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2183 struct isl_dim *dim;
2184 struct isl_basic_set *bset;
2189 bmap = isl_basic_map_cow(bmap);
2192 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2193 in = isl_basic_map_n_in(bmap);
2194 bset = isl_basic_set_from_basic_map(bmap);
2195 bset = isl_basic_set_swap_vars(bset, in);
2196 return isl_basic_map_from_basic_set(bset, dim);
2199 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2200 enum isl_dim_type type, unsigned pos, unsigned n)
2202 struct isl_dim *res_dim;
2203 struct isl_basic_map *res;
2204 struct isl_dim_map *dim_map;
2205 unsigned total, off;
2206 enum isl_dim_type t;
2214 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2216 total = isl_basic_map_total_dim(bmap) + n;
2217 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2219 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2221 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2223 unsigned size = isl_basic_map_dim(bmap, t);
2224 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2226 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2227 pos, size - pos, off + pos + n);
2229 off += isl_dim_size(res_dim, t);
2231 isl_dim_map_div(dim_map, bmap, off);
2233 res = isl_basic_map_alloc_dim(res_dim,
2234 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2235 res = add_constraints_dim_map(res, bmap, dim_map);
2236 res = isl_basic_map_simplify(res);
2237 return isl_basic_map_finalize(res);
2240 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2241 enum isl_dim_type type, unsigned n)
2245 return isl_basic_map_insert(bmap, type,
2246 isl_basic_map_dim(bmap, type), n);
2249 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2250 enum isl_dim_type type, unsigned n)
2254 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2255 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2257 isl_basic_set_free(bset);
2261 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2262 enum isl_dim_type type, unsigned pos, unsigned n)
2269 map = isl_map_cow(map);
2273 map->dim = isl_dim_insert(map->dim, type, pos, n);
2277 for (i = 0; i < map->n; ++i) {
2278 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2289 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2290 enum isl_dim_type type, unsigned pos, unsigned n)
2292 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2295 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2296 enum isl_dim_type type, unsigned n)
2300 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2303 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2304 enum isl_dim_type type, unsigned n)
2308 isl_assert(set->ctx, type != isl_dim_in, goto error);
2309 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2315 __isl_give isl_basic_map *isl_basic_map_move_dims(
2316 __isl_take isl_basic_map *bmap,
2317 enum isl_dim_type dst_type, unsigned dst_pos,
2318 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2321 struct isl_dim_map *dim_map;
2322 struct isl_basic_map *res;
2323 enum isl_dim_type t;
2324 unsigned total, off;
2331 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2334 if (dst_type == src_type && dst_pos == src_pos)
2337 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2339 if (pos(bmap->dim, dst_type) + dst_pos ==
2340 pos(bmap->dim, src_type) + src_pos +
2341 ((src_type < dst_type) ? n : 0)) {
2342 bmap = isl_basic_map_cow(bmap);
2346 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2347 src_type, src_pos, n);
2351 bmap = isl_basic_map_finalize(bmap);
2356 total = isl_basic_map_total_dim(bmap);
2357 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2360 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2361 unsigned size = isl_dim_size(bmap->dim, t);
2362 if (t == dst_type) {
2363 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2366 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2369 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2370 dst_pos, size - dst_pos, off);
2371 off += size - dst_pos;
2372 } else if (t == src_type) {
2373 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2376 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2377 src_pos + n, size - src_pos - n, off);
2378 off += size - src_pos - n;
2380 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2384 isl_dim_map_div(dim_map, bmap, off);
2386 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2387 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2388 bmap = add_constraints_dim_map(res, bmap, dim_map);
2390 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2391 src_type, src_pos, n);
2395 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2396 bmap = isl_basic_map_gauss(bmap, NULL);
2397 bmap = isl_basic_map_finalize(bmap);
2401 isl_basic_map_free(bmap);
2405 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2406 enum isl_dim_type dst_type, unsigned dst_pos,
2407 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2409 return (isl_basic_set *)isl_basic_map_move_dims(
2410 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2413 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2414 enum isl_dim_type dst_type, unsigned dst_pos,
2415 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2419 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2420 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2421 src_type, src_pos, n);
2427 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2428 enum isl_dim_type dst_type, unsigned dst_pos,
2429 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2438 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2441 if (dst_type == src_type && dst_pos == src_pos)
2444 isl_assert(map->ctx, dst_type != src_type, goto error);
2446 map = isl_map_cow(map);
2450 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2454 for (i = 0; i < map->n; ++i) {
2455 map->p[i] = isl_basic_map_move_dims(map->p[i],
2457 src_type, src_pos, n);
2468 /* Move the specified dimensions to the last columns right before
2469 * the divs. Don't change the dimension specification of bmap.
2470 * That's the responsibility of the caller.
2472 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2473 enum isl_dim_type type, unsigned first, unsigned n)
2475 struct isl_dim_map *dim_map;
2476 struct isl_basic_map *res;
2477 enum isl_dim_type t;
2478 unsigned total, off;
2482 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2485 total = isl_basic_map_total_dim(bmap);
2486 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2489 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2490 unsigned size = isl_dim_size(bmap->dim, t);
2492 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2495 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2496 first, n, total - bmap->n_div - n);
2497 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2498 first + n, size - (first + n), off);
2499 off += size - (first + n);
2501 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2505 isl_dim_map_div(dim_map, bmap, off + n);
2507 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2508 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2509 res = add_constraints_dim_map(res, bmap, dim_map);
2513 /* Turn the n dimensions of type type, starting at first
2514 * into existentially quantified variables.
2516 __isl_give isl_basic_map *isl_basic_map_project_out(
2517 __isl_take isl_basic_map *bmap,
2518 enum isl_dim_type type, unsigned first, unsigned n)
2531 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2532 return isl_basic_map_remove(bmap, type, first, n);
2534 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2537 bmap = move_last(bmap, type, first, n);
2538 bmap = isl_basic_map_cow(bmap);
2542 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2543 old = bmap->block2.data;
2544 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2545 (bmap->extra + n) * (1 + row_size));
2546 if (!bmap->block2.data)
2548 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2551 for (i = 0; i < n; ++i) {
2552 new_div[i] = bmap->block2.data +
2553 (bmap->extra + i) * (1 + row_size);
2554 isl_seq_clr(new_div[i], 1 + row_size);
2556 for (i = 0; i < bmap->extra; ++i)
2557 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2559 bmap->div = new_div;
2563 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2566 bmap = isl_basic_map_simplify(bmap);
2567 bmap = isl_basic_map_drop_redundant_divs(bmap);
2568 return isl_basic_map_finalize(bmap);
2570 isl_basic_map_free(bmap);
2574 /* Turn the n dimensions of type type, starting at first
2575 * into existentially quantified variables.
2577 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2578 enum isl_dim_type type, unsigned first, unsigned n)
2580 return (isl_basic_set *)isl_basic_map_project_out(
2581 (isl_basic_map *)bset, type, first, n);
2584 /* Turn the n dimensions of type type, starting at first
2585 * into existentially quantified variables.
2587 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2588 enum isl_dim_type type, unsigned first, unsigned n)
2598 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2600 map = isl_map_cow(map);
2604 map->dim = isl_dim_drop(map->dim, type, first, n);
2608 for (i = 0; i < map->n; ++i) {
2609 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2620 /* Turn the n dimensions of type type, starting at first
2621 * into existentially quantified variables.
2623 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2624 enum isl_dim_type type, unsigned first, unsigned n)
2626 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2629 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2633 for (i = 0; i < n; ++i) {
2634 j = isl_basic_map_alloc_div(bmap);
2637 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2641 isl_basic_map_free(bmap);
2645 struct isl_basic_map *isl_basic_map_apply_range(
2646 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2648 struct isl_dim *dim_result = NULL;
2649 struct isl_basic_map *bmap;
2650 unsigned n_in, n_out, n, nparam, total, pos;
2651 struct isl_dim_map *dim_map1, *dim_map2;
2653 if (!bmap1 || !bmap2)
2656 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2657 isl_dim_copy(bmap2->dim));
2659 n_in = isl_basic_map_n_in(bmap1);
2660 n_out = isl_basic_map_n_out(bmap2);
2661 n = isl_basic_map_n_out(bmap1);
2662 nparam = isl_basic_map_n_param(bmap1);
2664 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2665 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2666 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2667 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2668 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2669 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2670 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2671 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2672 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2673 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2674 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2676 bmap = isl_basic_map_alloc_dim(dim_result,
2677 bmap1->n_div + bmap2->n_div + n,
2678 bmap1->n_eq + bmap2->n_eq,
2679 bmap1->n_ineq + bmap2->n_ineq);
2680 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2681 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2682 bmap = add_divs(bmap, n);
2683 bmap = isl_basic_map_simplify(bmap);
2684 bmap = isl_basic_map_drop_redundant_divs(bmap);
2685 return isl_basic_map_finalize(bmap);
2687 isl_basic_map_free(bmap1);
2688 isl_basic_map_free(bmap2);
2692 struct isl_basic_set *isl_basic_set_apply(
2693 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2698 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2701 return (struct isl_basic_set *)
2702 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2704 isl_basic_set_free(bset);
2705 isl_basic_map_free(bmap);
2709 struct isl_basic_map *isl_basic_map_apply_domain(
2710 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2712 if (!bmap1 || !bmap2)
2715 isl_assert(bmap1->ctx,
2716 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2717 isl_assert(bmap1->ctx,
2718 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2721 bmap1 = isl_basic_map_reverse(bmap1);
2722 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2723 return isl_basic_map_reverse(bmap1);
2725 isl_basic_map_free(bmap1);
2726 isl_basic_map_free(bmap2);
2730 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2731 * A \cap B -> f(A) + f(B)
2733 struct isl_basic_map *isl_basic_map_sum(
2734 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2736 unsigned n_in, n_out, nparam, total, pos;
2737 struct isl_basic_map *bmap = NULL;
2738 struct isl_dim_map *dim_map1, *dim_map2;
2741 if (!bmap1 || !bmap2)
2744 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2747 nparam = isl_basic_map_n_param(bmap1);
2748 n_in = isl_basic_map_n_in(bmap1);
2749 n_out = isl_basic_map_n_out(bmap1);
2751 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2752 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2753 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2754 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2755 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2756 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2757 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2758 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2759 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2760 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2761 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2763 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2764 bmap1->n_div + bmap2->n_div + 2 * n_out,
2765 bmap1->n_eq + bmap2->n_eq + n_out,
2766 bmap1->n_ineq + bmap2->n_ineq);
2767 for (i = 0; i < n_out; ++i) {
2768 int j = isl_basic_map_alloc_equality(bmap);
2771 isl_seq_clr(bmap->eq[j], 1+total);
2772 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2773 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2774 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2776 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2777 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2778 bmap = add_divs(bmap, 2 * n_out);
2780 bmap = isl_basic_map_simplify(bmap);
2781 return isl_basic_map_finalize(bmap);
2783 isl_basic_map_free(bmap);
2784 isl_basic_map_free(bmap1);
2785 isl_basic_map_free(bmap2);
2789 /* Given two maps A -> f(A) and B -> g(B), construct a map
2790 * A \cap B -> f(A) + f(B)
2792 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2794 struct isl_map *result;
2800 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2802 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2803 map1->n * map2->n, 0);
2806 for (i = 0; i < map1->n; ++i)
2807 for (j = 0; j < map2->n; ++j) {
2808 struct isl_basic_map *part;
2809 part = isl_basic_map_sum(
2810 isl_basic_map_copy(map1->p[i]),
2811 isl_basic_map_copy(map2->p[j]));
2812 if (isl_basic_map_is_empty(part))
2813 isl_basic_map_free(part);
2815 result = isl_map_add_basic_map(result, part);
2828 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2829 __isl_take isl_set *set2)
2831 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2834 /* Given a basic map A -> f(A), construct A -> -f(A).
2836 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2841 bmap = isl_basic_map_cow(bmap);
2845 n = isl_basic_map_dim(bmap, isl_dim_out);
2846 off = isl_basic_map_offset(bmap, isl_dim_out);
2847 for (i = 0; i < bmap->n_eq; ++i)
2848 for (j = 0; j < n; ++j)
2849 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2850 for (i = 0; i < bmap->n_ineq; ++i)
2851 for (j = 0; j < n; ++j)
2852 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2853 for (i = 0; i < bmap->n_div; ++i)
2854 for (j = 0; j < n; ++j)
2855 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2856 return isl_basic_map_finalize(bmap);
2859 /* Given a map A -> f(A), construct A -> -f(A).
2861 struct isl_map *isl_map_neg(struct isl_map *map)
2865 map = isl_map_cow(map);
2869 for (i = 0; i < map->n; ++i) {
2870 map->p[i] = isl_basic_map_neg(map->p[i]);
2881 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
2883 return (isl_set *)isl_map_neg((isl_map *)set);
2886 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2887 * A -> floor(f(A)/d).
2889 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2892 unsigned n_in, n_out, nparam, total, pos;
2893 struct isl_basic_map *result = NULL;
2894 struct isl_dim_map *dim_map;
2900 nparam = isl_basic_map_n_param(bmap);
2901 n_in = isl_basic_map_n_in(bmap);
2902 n_out = isl_basic_map_n_out(bmap);
2904 total = nparam + n_in + n_out + bmap->n_div + n_out;
2905 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2906 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2907 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2908 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2909 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2911 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2912 bmap->n_div + n_out,
2913 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2914 result = add_constraints_dim_map(result, bmap, dim_map);
2915 result = add_divs(result, n_out);
2916 for (i = 0; i < n_out; ++i) {
2918 j = isl_basic_map_alloc_inequality(result);
2921 isl_seq_clr(result->ineq[j], 1+total);
2922 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2923 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2924 j = isl_basic_map_alloc_inequality(result);
2927 isl_seq_clr(result->ineq[j], 1+total);
2928 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2929 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2930 isl_int_sub_ui(result->ineq[j][0], d, 1);
2933 result = isl_basic_map_simplify(result);
2934 return isl_basic_map_finalize(result);
2936 isl_basic_map_free(result);
2940 /* Given a map A -> f(A) and an integer d, construct a map
2941 * A -> floor(f(A)/d).
2943 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2947 map = isl_map_cow(map);
2951 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2952 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2953 for (i = 0; i < map->n; ++i) {
2954 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2965 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2971 i = isl_basic_map_alloc_equality(bmap);
2974 nparam = isl_basic_map_n_param(bmap);
2975 n_in = isl_basic_map_n_in(bmap);
2976 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2977 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2978 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2979 return isl_basic_map_finalize(bmap);
2981 isl_basic_map_free(bmap);
2985 /* Add a constraints to "bmap" expressing i_pos < o_pos
2987 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2993 i = isl_basic_map_alloc_inequality(bmap);
2996 nparam = isl_basic_map_n_param(bmap);
2997 n_in = isl_basic_map_n_in(bmap);
2998 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2999 isl_int_set_si(bmap->ineq[i][0], -1);
3000 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3001 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3002 return isl_basic_map_finalize(bmap);
3004 isl_basic_map_free(bmap);
3008 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3010 static __isl_give isl_basic_map *var_less_or_equal(
3011 __isl_take isl_basic_map *bmap, unsigned pos)
3017 i = isl_basic_map_alloc_inequality(bmap);
3020 nparam = isl_basic_map_n_param(bmap);
3021 n_in = isl_basic_map_n_in(bmap);
3022 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3023 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3024 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3025 return isl_basic_map_finalize(bmap);
3027 isl_basic_map_free(bmap);
3031 /* Add a constraints to "bmap" expressing i_pos > o_pos
3033 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3039 i = isl_basic_map_alloc_inequality(bmap);
3042 nparam = isl_basic_map_n_param(bmap);
3043 n_in = isl_basic_map_n_in(bmap);
3044 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3045 isl_int_set_si(bmap->ineq[i][0], -1);
3046 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3047 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3048 return isl_basic_map_finalize(bmap);
3050 isl_basic_map_free(bmap);
3054 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3056 static __isl_give isl_basic_map *var_more_or_equal(
3057 __isl_take isl_basic_map *bmap, unsigned pos)
3063 i = isl_basic_map_alloc_inequality(bmap);
3066 nparam = isl_basic_map_n_param(bmap);
3067 n_in = isl_basic_map_n_in(bmap);
3068 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3069 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3070 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3071 return isl_basic_map_finalize(bmap);
3073 isl_basic_map_free(bmap);
3077 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3080 struct isl_basic_map *bmap;
3081 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3084 for (i = 0; i < n_equal && bmap; ++i)
3085 bmap = var_equal(bmap, i);
3086 return isl_basic_map_finalize(bmap);
3089 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3091 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3094 struct isl_basic_map *bmap;
3095 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3098 for (i = 0; i < pos && bmap; ++i)
3099 bmap = var_equal(bmap, i);
3101 bmap = var_less(bmap, pos);
3102 return isl_basic_map_finalize(bmap);
3105 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3107 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3108 __isl_take isl_dim *dim, unsigned pos)
3111 isl_basic_map *bmap;
3113 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3114 for (i = 0; i < pos; ++i)
3115 bmap = var_equal(bmap, i);
3116 bmap = var_less_or_equal(bmap, pos);
3117 return isl_basic_map_finalize(bmap);
3120 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3122 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3125 struct isl_basic_map *bmap;
3126 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3129 for (i = 0; i < pos && bmap; ++i)
3130 bmap = var_equal(bmap, i);
3132 bmap = var_more(bmap, pos);
3133 return isl_basic_map_finalize(bmap);
3136 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3138 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3139 __isl_take isl_dim *dim, unsigned pos)
3142 isl_basic_map *bmap;
3144 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3145 for (i = 0; i < pos; ++i)
3146 bmap = var_equal(bmap, i);
3147 bmap = var_more_or_equal(bmap, pos);
3148 return isl_basic_map_finalize(bmap);
3151 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3152 unsigned n, int equal)
3154 struct isl_map *map;
3157 if (n == 0 && equal)
3158 return isl_map_universe(dims);
3160 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3162 for (i = 0; i + 1 < n; ++i)
3163 map = isl_map_add_basic_map(map,
3164 isl_basic_map_less_at(isl_dim_copy(dims), i));
3167 map = isl_map_add_basic_map(map,
3168 isl_basic_map_less_or_equal_at(dims, n - 1));
3170 map = isl_map_add_basic_map(map,
3171 isl_basic_map_less_at(dims, n - 1));
3178 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3182 return map_lex_lte_first(dims, dims->n_out, equal);
3185 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3187 return map_lex_lte_first(dim, n, 0);
3190 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3192 return map_lex_lte_first(dim, n, 1);
3195 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3197 return map_lex_lte(isl_dim_map(set_dim), 0);
3200 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3202 return map_lex_lte(isl_dim_map(set_dim), 1);
3205 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3206 unsigned n, int equal)
3208 struct isl_map *map;
3211 if (n == 0 && equal)
3212 return isl_map_universe(dims);
3214 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3216 for (i = 0; i + 1 < n; ++i)
3217 map = isl_map_add_basic_map(map,
3218 isl_basic_map_more_at(isl_dim_copy(dims), i));
3221 map = isl_map_add_basic_map(map,
3222 isl_basic_map_more_or_equal_at(dims, n - 1));
3224 map = isl_map_add_basic_map(map,
3225 isl_basic_map_more_at(dims, n - 1));
3232 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3236 return map_lex_gte_first(dims, dims->n_out, equal);
3239 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3241 return map_lex_gte_first(dim, n, 0);
3244 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3246 return map_lex_gte_first(dim, n, 1);
3249 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3251 return map_lex_gte(isl_dim_map(set_dim), 0);
3254 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3256 return map_lex_gte(isl_dim_map(set_dim), 1);
3259 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3260 __isl_take isl_set *set2)
3263 map = isl_map_lex_le(isl_set_get_dim(set1));
3264 map = isl_map_intersect_domain(map, set1);
3265 map = isl_map_intersect_range(map, set2);
3269 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3270 __isl_take isl_set *set2)
3273 map = isl_map_lex_lt(isl_set_get_dim(set1));
3274 map = isl_map_intersect_domain(map, set1);
3275 map = isl_map_intersect_range(map, set2);
3279 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3280 __isl_take isl_set *set2)
3283 map = isl_map_lex_ge(isl_set_get_dim(set1));
3284 map = isl_map_intersect_domain(map, set1);
3285 map = isl_map_intersect_range(map, set2);
3289 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3290 __isl_take isl_set *set2)
3293 map = isl_map_lex_gt(isl_set_get_dim(set1));
3294 map = isl_map_intersect_domain(map, set1);
3295 map = isl_map_intersect_range(map, set2);
3299 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3300 __isl_take isl_map *map2)
3303 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3304 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3305 map = isl_map_apply_range(map, isl_map_reverse(map2));
3309 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3310 __isl_take isl_map *map2)
3313 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3314 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3315 map = isl_map_apply_range(map, isl_map_reverse(map2));
3319 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3320 __isl_take isl_map *map2)
3323 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3324 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3325 map = isl_map_apply_range(map, isl_map_reverse(map2));
3329 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3330 __isl_take isl_map *map2)
3333 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3334 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3335 map = isl_map_apply_range(map, isl_map_reverse(map2));
3339 struct isl_basic_map *isl_basic_map_from_basic_set(
3340 struct isl_basic_set *bset, struct isl_dim *dim)
3342 struct isl_basic_map *bmap;
3344 bset = isl_basic_set_cow(bset);
3348 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3349 isl_dim_free(bset->dim);
3350 bmap = (struct isl_basic_map *) bset;
3352 return isl_basic_map_finalize(bmap);
3354 isl_basic_set_free(bset);
3359 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3363 if (bmap->dim->n_in == 0)
3364 return (struct isl_basic_set *)bmap;
3365 bmap = isl_basic_map_cow(bmap);
3368 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3371 bmap = isl_basic_map_finalize(bmap);
3372 return (struct isl_basic_set *)bmap;
3374 isl_basic_map_free(bmap);
3378 /* For a div d = floor(f/m), add the constraints
3381 * -(f-(n-1)) + m d >= 0
3383 * Note that the second constraint is the negation of
3387 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3388 unsigned pos, isl_int *div)
3391 unsigned total = isl_basic_map_total_dim(bmap);
3393 i = isl_basic_map_alloc_inequality(bmap);
3396 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3397 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3399 j = isl_basic_map_alloc_inequality(bmap);
3402 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3403 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3404 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3408 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3409 unsigned pos, isl_int *div)
3411 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3415 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3417 unsigned total = isl_basic_map_total_dim(bmap);
3418 unsigned div_pos = total - bmap->n_div + div;
3420 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3424 struct isl_basic_set *isl_basic_map_underlying_set(
3425 struct isl_basic_map *bmap)
3429 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3430 bmap->n_div == 0 && !isl_dim_get_tuple_name(bmap->dim, isl_dim_out))
3431 return (struct isl_basic_set *)bmap;
3432 bmap = isl_basic_map_cow(bmap);
3435 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3438 bmap->extra -= bmap->n_div;
3440 bmap = isl_basic_map_finalize(bmap);
3441 return (struct isl_basic_set *)bmap;
3446 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3447 __isl_take isl_basic_set *bset)
3449 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3452 struct isl_basic_map *isl_basic_map_overlying_set(
3453 struct isl_basic_set *bset, struct isl_basic_map *like)
3455 struct isl_basic_map *bmap;
3456 struct isl_ctx *ctx;
3463 isl_assert(ctx, bset->n_div == 0, goto error);
3464 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3465 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3467 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3468 isl_basic_map_free(like);
3469 return (struct isl_basic_map *)bset;
3471 bset = isl_basic_set_cow(bset);
3474 total = bset->dim->n_out + bset->extra;
3475 bmap = (struct isl_basic_map *)bset;
3476 isl_dim_free(bmap->dim);
3477 bmap->dim = isl_dim_copy(like->dim);
3480 bmap->n_div = like->n_div;
3481 bmap->extra += like->n_div;
3484 ltotal = total - bmap->extra + like->extra;
3487 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3488 bmap->extra * (1 + 1 + total));
3489 if (isl_blk_is_error(bmap->block2))
3491 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3495 for (i = 0; i < bmap->extra; ++i)
3496 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3497 for (i = 0; i < like->n_div; ++i) {
3498 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3499 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3501 bmap = isl_basic_map_extend_constraints(bmap,
3502 0, 2 * like->n_div);
3503 for (i = 0; i < like->n_div; ++i) {
3504 if (isl_int_is_zero(bmap->div[i][0]))
3506 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3510 isl_basic_map_free(like);
3511 bmap = isl_basic_map_simplify(bmap);
3512 bmap = isl_basic_map_finalize(bmap);
3515 isl_basic_map_free(like);
3516 isl_basic_set_free(bset);
3520 struct isl_basic_set *isl_basic_set_from_underlying_set(
3521 struct isl_basic_set *bset, struct isl_basic_set *like)
3523 return (struct isl_basic_set *)
3524 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3527 struct isl_set *isl_set_from_underlying_set(
3528 struct isl_set *set, struct isl_basic_set *like)
3534 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3536 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3537 isl_basic_set_free(like);
3540 set = isl_set_cow(set);
3543 for (i = 0; i < set->n; ++i) {
3544 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3545 isl_basic_set_copy(like));
3549 isl_dim_free(set->dim);
3550 set->dim = isl_dim_copy(like->dim);
3553 isl_basic_set_free(like);
3556 isl_basic_set_free(like);
3561 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3565 map = isl_map_cow(map);
3568 map->dim = isl_dim_cow(map->dim);
3572 for (i = 1; i < map->n; ++i)
3573 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3575 for (i = 0; i < map->n; ++i) {
3576 map->p[i] = (struct isl_basic_map *)
3577 isl_basic_map_underlying_set(map->p[i]);
3582 map->dim = isl_dim_underlying(map->dim, 0);
3584 isl_dim_free(map->dim);
3585 map->dim = isl_dim_copy(map->p[0]->dim);
3589 return (struct isl_set *)map;
3595 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3597 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3600 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3601 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3603 bmap = isl_basic_map_cow(bmap);
3607 isl_dim_free(bmap->dim);
3610 bmap = isl_basic_map_finalize(bmap);
3614 isl_basic_map_free(bmap);
3619 static __isl_give isl_basic_set *isl_basic_set_reset_dim(
3620 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3622 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3626 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3627 __isl_take isl_dim *dim)
3631 map = isl_map_cow(map);
3635 for (i = 0; i < map->n; ++i) {
3636 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3641 isl_dim_free(map->dim);
3651 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3652 __isl_take isl_dim *dim)
3654 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3657 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3660 struct isl_basic_set *domain;
3666 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3668 n_in = isl_basic_map_n_in(bmap);
3669 n_out = isl_basic_map_n_out(bmap);
3670 domain = isl_basic_set_from_basic_map(bmap);
3671 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3673 domain = isl_basic_set_reset_dim(domain, dim);
3678 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3680 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3683 __isl_give isl_basic_map *isl_basic_map_domain_map(
3684 __isl_take isl_basic_map *bmap)
3688 isl_basic_map *domain;
3689 isl_basic_set *bset;
3690 int nparam, n_in, n_out;
3693 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3694 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3695 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3697 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3698 domain = isl_basic_map_universe(dim);
3700 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3701 bmap = isl_basic_map_apply_range(bmap, domain);
3702 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3704 total = isl_basic_map_total_dim(bmap);
3706 for (i = 0; i < n_in; ++i) {
3707 k = isl_basic_map_alloc_equality(bmap);
3710 isl_seq_clr(bmap->eq[k], 1 + total);
3711 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3712 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3715 bmap = isl_basic_map_gauss(bmap, NULL);
3716 return isl_basic_map_finalize(bmap);
3718 isl_basic_map_free(bmap);
3722 __isl_give isl_basic_map *isl_basic_map_range_map(
3723 __isl_take isl_basic_map *bmap)
3727 isl_basic_map *range;
3728 isl_basic_set *bset;
3729 int nparam, n_in, n_out;
3732 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3733 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3734 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3736 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3737 range = isl_basic_map_universe(dim);
3739 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3740 bmap = isl_basic_map_apply_range(bmap, range);
3741 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3743 total = isl_basic_map_total_dim(bmap);
3745 for (i = 0; i < n_out; ++i) {
3746 k = isl_basic_map_alloc_equality(bmap);
3749 isl_seq_clr(bmap->eq[k], 1 + total);
3750 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3751 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3754 bmap = isl_basic_map_gauss(bmap, NULL);
3755 return isl_basic_map_finalize(bmap);
3757 isl_basic_map_free(bmap);
3761 struct isl_set *isl_map_range(struct isl_map *map)
3764 struct isl_set *set;
3768 if (isl_map_dim(map, isl_dim_in) == 0)
3769 return (isl_set *)map;
3771 map = isl_map_cow(map);
3775 set = (struct isl_set *) map;
3776 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3779 for (i = 0; i < map->n; ++i) {
3780 set->p[i] = isl_basic_map_range(map->p[i]);
3784 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3785 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3792 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3795 isl_dim *domain_dim;
3797 map = isl_map_cow(map);
3801 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3802 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3803 map->dim = isl_dim_join(map->dim, domain_dim);
3806 for (i = 0; i < map->n; ++i) {
3807 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3811 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3812 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3819 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3824 map = isl_map_cow(map);
3828 range_dim = isl_dim_range(isl_map_get_dim(map));
3829 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3830 map->dim = isl_dim_join(map->dim, range_dim);
3833 for (i = 0; i < map->n; ++i) {
3834 map->p[i] = isl_basic_map_range_map(map->p[i]);
3838 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3839 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3846 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3849 struct isl_map *map = NULL;
3851 set = isl_set_cow(set);
3854 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3855 map = (struct isl_map *)set;
3856 for (i = 0; i < set->n; ++i) {
3857 map->p[i] = isl_basic_map_from_basic_set(
3858 set->p[i], isl_dim_copy(dim));
3862 isl_dim_free(map->dim);
3871 __isl_give isl_basic_map *isl_basic_map_from_domain(
3872 __isl_take isl_basic_set *bset)
3874 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
3877 __isl_give isl_basic_map *isl_basic_map_from_range(
3878 __isl_take isl_basic_set *bset)
3880 return (isl_basic_map *)bset;
3883 struct isl_map *isl_map_from_range(struct isl_set *set)
3885 return (struct isl_map *)set;
3888 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3890 return isl_map_reverse(isl_map_from_range(set));
3893 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3894 __isl_take isl_set *range)
3896 return isl_map_apply_range(isl_map_from_domain(domain),
3897 isl_map_from_range(range));
3900 struct isl_set *isl_set_from_map(struct isl_map *map)
3903 struct isl_set *set = NULL;
3907 map = isl_map_cow(map);
3910 map->dim = isl_dim_as_set_dim(map->dim);
3913 set = (struct isl_set *)map;
3914 for (i = 0; i < map->n; ++i) {
3915 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3925 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3927 struct isl_map *map;
3931 isl_assert(dim->ctx, n >= 0, return NULL);
3932 map = isl_alloc(dim->ctx, struct isl_map,
3933 sizeof(struct isl_map) +
3934 (n - 1) * sizeof(struct isl_basic_map *));
3938 map->ctx = dim->ctx;
3939 isl_ctx_ref(map->ctx);
3951 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3952 unsigned nparam, unsigned in, unsigned out, int n,
3955 struct isl_map *map;
3956 struct isl_dim *dims;
3958 dims = isl_dim_alloc(ctx, nparam, in, out);
3962 map = isl_map_alloc_dim(dims, n, flags);
3966 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3968 struct isl_basic_map *bmap;
3969 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3970 bmap = isl_basic_map_set_to_empty(bmap);
3974 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3976 struct isl_basic_set *bset;
3977 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3978 bset = isl_basic_set_set_to_empty(bset);
3982 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3984 struct isl_basic_map *bmap;
3987 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3988 bmap = isl_basic_map_set_to_empty(bmap);
3992 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3994 struct isl_basic_map *bmap;
3997 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3998 bmap = isl_basic_map_set_to_empty(bmap);
4002 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4004 struct isl_basic_set *bset;
4007 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4008 bset = isl_basic_set_set_to_empty(bset);
4012 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4014 struct isl_basic_map *bmap;
4015 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4019 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4021 struct isl_basic_set *bset;
4022 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4026 __isl_give isl_basic_map *isl_basic_map_universe_like(
4027 __isl_keep isl_basic_map *model)
4031 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4034 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4038 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4041 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4042 __isl_keep isl_set *model)
4046 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4049 struct isl_map *isl_map_empty(struct isl_dim *dim)
4051 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4054 struct isl_map *isl_map_empty_like(struct isl_map *model)
4058 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4061 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4065 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4068 struct isl_set *isl_set_empty(struct isl_dim *dim)
4070 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4073 struct isl_set *isl_set_empty_like(struct isl_set *model)
4077 return isl_set_empty(isl_dim_copy(model->dim));
4080 struct isl_map *isl_map_universe(struct isl_dim *dim)
4082 struct isl_map *map;
4085 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4086 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4090 struct isl_set *isl_set_universe(struct isl_dim *dim)
4092 struct isl_set *set;
4095 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4096 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4100 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4104 return isl_set_universe(isl_dim_copy(model->dim));
4107 struct isl_map *isl_map_dup(struct isl_map *map)
4110 struct isl_map *dup;
4114 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4115 for (i = 0; i < map->n; ++i)
4116 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4120 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4121 __isl_take isl_basic_map *bmap)
4125 if (isl_basic_map_fast_is_empty(bmap)) {
4126 isl_basic_map_free(bmap);
4129 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4130 isl_assert(map->ctx, map->n < map->size, goto error);
4131 map->p[map->n] = bmap;
4133 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4139 isl_basic_map_free(bmap);
4143 void isl_map_free(struct isl_map *map)
4153 isl_ctx_deref(map->ctx);
4154 for (i = 0; i < map->n; ++i)
4155 isl_basic_map_free(map->p[i]);
4156 isl_dim_free(map->dim);
4160 struct isl_map *isl_map_extend(struct isl_map *base,
4161 unsigned nparam, unsigned n_in, unsigned n_out)
4165 base = isl_map_cow(base);
4169 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4172 for (i = 0; i < base->n; ++i) {
4173 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4174 isl_dim_copy(base->dim), 0, 0, 0);
4184 struct isl_set *isl_set_extend(struct isl_set *base,
4185 unsigned nparam, unsigned dim)
4187 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4191 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4192 struct isl_basic_map *bmap, unsigned pos, int value)
4196 bmap = isl_basic_map_cow(bmap);
4197 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4198 j = isl_basic_map_alloc_equality(bmap);
4201 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4202 isl_int_set_si(bmap->eq[j][pos], -1);
4203 isl_int_set_si(bmap->eq[j][0], value);
4204 bmap = isl_basic_map_simplify(bmap);
4205 return isl_basic_map_finalize(bmap);
4207 isl_basic_map_free(bmap);
4211 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4212 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4216 bmap = isl_basic_map_cow(bmap);
4217 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4218 j = isl_basic_map_alloc_equality(bmap);
4221 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4222 isl_int_set_si(bmap->eq[j][pos], -1);
4223 isl_int_set(bmap->eq[j][0], value);
4224 bmap = isl_basic_map_simplify(bmap);
4225 return isl_basic_map_finalize(bmap);
4227 isl_basic_map_free(bmap);
4231 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4232 enum isl_dim_type type, unsigned pos, int value)
4236 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4237 return isl_basic_map_fix_pos_si(bmap,
4238 isl_basic_map_offset(bmap, type) + pos, value);
4240 isl_basic_map_free(bmap);
4244 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4245 enum isl_dim_type type, unsigned pos, isl_int value)
4249 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4250 return isl_basic_map_fix_pos(bmap,
4251 isl_basic_map_offset(bmap, type) + pos, value);
4253 isl_basic_map_free(bmap);
4257 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4258 enum isl_dim_type type, unsigned pos, int value)
4260 return (struct isl_basic_set *)
4261 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4265 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4266 enum isl_dim_type type, unsigned pos, isl_int value)
4268 return (struct isl_basic_set *)
4269 isl_basic_map_fix((struct isl_basic_map *)bset,
4273 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4274 unsigned input, int value)
4276 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4279 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4280 unsigned dim, int value)
4282 return (struct isl_basic_set *)
4283 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4284 isl_dim_set, dim, value);
4287 struct isl_map *isl_map_fix_si(struct isl_map *map,
4288 enum isl_dim_type type, unsigned pos, int value)
4292 map = isl_map_cow(map);
4296 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4297 for (i = 0; i < map->n; ++i) {
4298 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4302 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4309 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4310 enum isl_dim_type type, unsigned pos, int value)
4312 return (struct isl_set *)
4313 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4316 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4317 enum isl_dim_type type, unsigned pos, isl_int value)
4321 map = isl_map_cow(map);
4325 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4326 for (i = 0; i < map->n; ++i) {
4327 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4331 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4338 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4339 enum isl_dim_type type, unsigned pos, isl_int value)
4341 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4344 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4345 unsigned input, int value)
4347 return isl_map_fix_si(map, isl_dim_in, input, value);
4350 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4352 return (struct isl_set *)
4353 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4356 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4357 __isl_take isl_basic_map *bmap,
4358 enum isl_dim_type type, unsigned pos, int value)
4364 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4365 pos += isl_basic_map_offset(bmap, type);
4366 bmap = isl_basic_map_cow(bmap);
4367 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4368 j = isl_basic_map_alloc_inequality(bmap);
4371 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4372 isl_int_set_si(bmap->ineq[j][pos], 1);
4373 isl_int_set_si(bmap->ineq[j][0], -value);
4374 bmap = isl_basic_map_simplify(bmap);
4375 return isl_basic_map_finalize(bmap);
4377 isl_basic_map_free(bmap);
4381 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4382 unsigned dim, isl_int value)
4386 bset = isl_basic_set_cow(bset);
4387 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4388 j = isl_basic_set_alloc_inequality(bset);
4391 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4392 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4393 isl_int_neg(bset->ineq[j][0], value);
4394 bset = isl_basic_set_simplify(bset);
4395 return isl_basic_set_finalize(bset);
4397 isl_basic_set_free(bset);
4401 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4402 enum isl_dim_type type, unsigned pos, int value)
4406 map = isl_map_cow(map);
4410 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4411 for (i = 0; i < map->n; ++i) {
4412 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4417 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4424 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4425 enum isl_dim_type type, unsigned pos, int value)
4427 return (struct isl_set *)
4428 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4431 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4436 set = isl_set_cow(set);
4440 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4441 for (i = 0; i < set->n; ++i) {
4442 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4452 struct isl_map *isl_map_reverse(struct isl_map *map)
4456 map = isl_map_cow(map);
4460 map->dim = isl_dim_reverse(map->dim);
4463 for (i = 0; i < map->n; ++i) {
4464 map->p[i] = isl_basic_map_reverse(map->p[i]);
4468 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4475 static struct isl_map *isl_basic_map_partial_lexopt(
4476 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4477 struct isl_set **empty, int max)
4481 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4482 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4484 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4486 isl_basic_map_free(bmap);
4487 isl_basic_set_free(dom);
4493 struct isl_map *isl_basic_map_partial_lexmax(
4494 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4495 struct isl_set **empty)
4497 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4500 struct isl_map *isl_basic_map_partial_lexmin(
4501 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4502 struct isl_set **empty)
4504 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4507 struct isl_set *isl_basic_set_partial_lexmin(
4508 struct isl_basic_set *bset, struct isl_basic_set *dom,
4509 struct isl_set **empty)
4511 return (struct isl_set *)
4512 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4516 struct isl_set *isl_basic_set_partial_lexmax(
4517 struct isl_basic_set *bset, struct isl_basic_set *dom,
4518 struct isl_set **empty)
4520 return (struct isl_set *)
4521 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4525 /* Given a basic map "bmap", compute the lexicograhically minimal
4526 * (or maximal) image element for each domain element in dom.
4527 * Set *empty to those elements in dom that do not have an image element.
4529 * We first make sure the basic sets in dom are disjoint and then
4530 * simply collect the results over each of the basic sets separately.
4531 * We could probably improve the efficiency a bit by moving the union
4532 * domain down into the parametric integer programming.
4534 static __isl_give isl_map *basic_map_partial_lexopt(
4535 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4536 __isl_give isl_set **empty, int max)
4539 struct isl_map *res;
4541 dom = isl_set_make_disjoint(dom);
4545 if (isl_set_fast_is_empty(dom)) {
4546 res = isl_map_empty_like_basic_map(bmap);
4547 *empty = isl_set_empty_like(dom);
4549 isl_basic_map_free(bmap);
4553 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4554 isl_basic_set_copy(dom->p[0]), empty, max);
4556 for (i = 1; i < dom->n; ++i) {
4557 struct isl_map *res_i;
4558 struct isl_set *empty_i;
4560 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4561 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4563 res = isl_map_union_disjoint(res, res_i);
4564 *empty = isl_set_union_disjoint(*empty, empty_i);
4568 isl_basic_map_free(bmap);
4573 isl_basic_map_free(bmap);
4577 /* Given a map "map", compute the lexicograhically minimal
4578 * (or maximal) image element for each domain element in dom.
4579 * Set *empty to those elements in dom that do not have an image element.
4581 * We first compute the lexicographically minimal or maximal element
4582 * in the first basic map. This results in a partial solution "res"
4583 * and a subset "todo" of dom that still need to be handled.
4584 * We then consider each of the remaining maps in "map" and successively
4585 * improve both "res" and "todo".
4587 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4588 * Assume we are computing the lexicographical maximum.
4589 * We first intersect basic map i with a relation that maps elements
4590 * to elements that are lexicographically larger than the image elements
4591 * in res^k and the compute the maximum image element of this intersection.
4592 * The result ("better") corresponds to those image elements in basic map i
4593 * that are better than what we had before. The remainder ("keep") are the
4594 * domain elements for which the image element in res_k was better.
4595 * We also compute the lexicographical maximum of basic map i in todo^k.
4596 * res^i is the result of the operation + better + those elements in
4597 * res^k that we should keep
4598 * todo^i is the remainder of the maximum operation on todo^k.
4600 static __isl_give isl_map *isl_map_partial_lexopt(
4601 __isl_take isl_map *map, __isl_take isl_set *dom,
4602 __isl_give isl_set **empty, int max)
4605 struct isl_map *res;
4606 struct isl_set *todo;
4611 if (isl_map_fast_is_empty(map)) {
4619 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4620 isl_set_copy(dom), &todo, max);
4622 for (i = 1; i < map->n; ++i) {
4624 struct isl_map *better;
4625 struct isl_set *keep;
4626 struct isl_map *res_i;
4627 struct isl_set *todo_i;
4628 struct isl_dim *dim = isl_map_get_dim(res);
4630 dim = isl_dim_range(dim);
4632 lt = isl_map_lex_lt(dim);
4634 lt = isl_map_lex_gt(dim);
4635 lt = isl_map_apply_range(isl_map_copy(res), lt);
4636 lt = isl_map_intersect(lt,
4637 isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
4638 better = isl_map_partial_lexopt(lt,
4639 isl_map_domain(isl_map_copy(res)),
4642 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4643 todo, &todo_i, max);
4645 res = isl_map_intersect_domain(res, keep);
4646 res = isl_map_union_disjoint(res, res_i);
4647 res = isl_map_union_disjoint(res, better);
4668 __isl_give isl_map *isl_map_partial_lexmax(
4669 __isl_take isl_map *map, __isl_take isl_set *dom,
4670 __isl_give isl_set **empty)
4672 return isl_map_partial_lexopt(map, dom, empty, 1);
4675 __isl_give isl_map *isl_map_partial_lexmin(
4676 __isl_take isl_map *map, __isl_take isl_set *dom,
4677 __isl_give isl_set **empty)
4679 return isl_map_partial_lexopt(map, dom, empty, 0);
4682 __isl_give isl_set *isl_set_partial_lexmin(
4683 __isl_take isl_set *set, __isl_take isl_set *dom,
4684 __isl_give isl_set **empty)
4686 return (struct isl_set *)
4687 isl_map_partial_lexmin((struct isl_map *)set,
4691 __isl_give isl_set *isl_set_partial_lexmax(
4692 __isl_take isl_set *set, __isl_take isl_set *dom,
4693 __isl_give isl_set **empty)
4695 return (struct isl_set *)
4696 isl_map_partial_lexmax((struct isl_map *)set,
4700 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4702 struct isl_basic_set *dom = NULL;
4703 struct isl_dim *dom_dim;
4707 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4708 dom = isl_basic_set_universe(dom_dim);
4709 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4711 isl_basic_map_free(bmap);
4715 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4717 return isl_basic_map_lexopt(bmap, 0);
4720 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4722 return isl_basic_map_lexopt(bmap, 1);
4725 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4727 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4730 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4732 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4735 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4737 struct isl_set *dom = NULL;
4738 struct isl_dim *dom_dim;
4742 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4743 dom = isl_set_universe(dom_dim);
4744 return isl_map_partial_lexopt(map, dom, NULL, max);
4750 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4752 return isl_map_lexopt(map, 0);
4755 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4757 return isl_map_lexopt(map, 1);
4760 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4762 return (isl_set *)isl_map_lexmin((isl_map *)set);
4765 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4767 return (isl_set *)isl_map_lexmax((isl_map *)set);
4770 /* Apply a preimage specified by "mat" on the parameters of "bset".
4771 * bset is assumed to have only parameters and divs.
4773 static struct isl_basic_set *basic_set_parameter_preimage(
4774 struct isl_basic_set *bset, struct isl_mat *mat)
4781 bset->dim = isl_dim_cow(bset->dim);
4785 nparam = isl_basic_set_dim(bset, isl_dim_param);
4787 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4789 bset->dim->nparam = 0;
4790 bset->dim->n_out = nparam;
4791 bset = isl_basic_set_preimage(bset, mat);
4793 bset->dim->nparam = bset->dim->n_out;
4794 bset->dim->n_out = 0;
4799 isl_basic_set_free(bset);
4803 /* Apply a preimage specified by "mat" on the parameters of "set".
4804 * set is assumed to have only parameters and divs.
4806 static struct isl_set *set_parameter_preimage(
4807 struct isl_set *set, struct isl_mat *mat)
4809 struct isl_dim *dim = NULL;
4815 dim = isl_dim_copy(set->dim);
4816 dim = isl_dim_cow(dim);
4820 nparam = isl_set_dim(set, isl_dim_param);
4822 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4825 dim->n_out = nparam;
4826 isl_set_reset_dim(set, dim);
4827 set = isl_set_preimage(set, mat);
4830 dim = isl_dim_copy(set->dim);
4831 dim = isl_dim_cow(dim);
4834 dim->nparam = dim->n_out;
4836 isl_set_reset_dim(set, dim);
4846 /* Intersect the basic set "bset" with the affine space specified by the
4847 * equalities in "eq".
4849 static struct isl_basic_set *basic_set_append_equalities(
4850 struct isl_basic_set *bset, struct isl_mat *eq)
4858 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4863 len = 1 + isl_dim_total(bset->dim) + bset->extra;
4864 for (i = 0; i < eq->n_row; ++i) {
4865 k = isl_basic_set_alloc_equality(bset);
4868 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4869 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4873 bset = isl_basic_set_gauss(bset, NULL);
4874 bset = isl_basic_set_finalize(bset);
4879 isl_basic_set_free(bset);
4883 /* Intersect the set "set" with the affine space specified by the
4884 * equalities in "eq".
4886 static struct isl_set *set_append_equalities(struct isl_set *set,
4894 for (i = 0; i < set->n; ++i) {
4895 set->p[i] = basic_set_append_equalities(set->p[i],
4908 /* Project the given basic set onto its parameter domain, possibly introducing
4909 * new, explicit, existential variables in the constraints.
4910 * The input has parameters and (possibly implicit) existential variables.
4911 * The output has the same parameters, but only
4912 * explicit existentially quantified variables.
4914 * The actual projection is performed by pip, but pip doesn't seem
4915 * to like equalities very much, so we first remove the equalities
4916 * among the parameters by performing a variable compression on
4917 * the parameters. Afterward, an inverse transformation is performed
4918 * and the equalities among the parameters are inserted back in.
4920 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4924 struct isl_mat *T, *T2;
4925 struct isl_set *set;
4926 unsigned nparam, n_div;
4928 bset = isl_basic_set_cow(bset);
4932 if (bset->n_eq == 0)
4933 return isl_basic_set_lexmin(bset);
4935 isl_basic_set_gauss(bset, NULL);
4937 nparam = isl_basic_set_dim(bset, isl_dim_param);
4938 n_div = isl_basic_set_dim(bset, isl_dim_div);
4940 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4941 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4944 if (i == bset->n_eq)
4945 return isl_basic_set_lexmin(bset);
4947 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4949 eq = isl_mat_cow(eq);
4950 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4951 if (T && T->n_col == 0) {
4955 bset = isl_basic_set_set_to_empty(bset);
4956 return isl_set_from_basic_set(bset);
4958 bset = basic_set_parameter_preimage(bset, T);
4960 set = isl_basic_set_lexmin(bset);
4961 set = set_parameter_preimage(set, T2);
4962 set = set_append_equalities(set, eq);
4966 /* Compute an explicit representation for all the existentially
4967 * quantified variables.
4968 * The input and output dimensions are first turned into parameters.
4969 * compute_divs then returns a map with the same parameters and
4970 * no input or output dimensions and the dimension specification
4971 * is reset to that of the input.
4973 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4975 struct isl_basic_set *bset;
4976 struct isl_set *set;
4977 struct isl_map *map;
4978 struct isl_dim *dim, *orig_dim = NULL;
4983 bmap = isl_basic_map_cow(bmap);
4987 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4988 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4989 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4990 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4994 orig_dim = bmap->dim;
4996 bset = (struct isl_basic_set *)bmap;
4998 set = parameter_compute_divs(bset);
4999 map = (struct isl_map *)set;
5000 map = isl_map_reset_dim(map, orig_dim);
5004 isl_basic_map_free(bmap);
5008 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5016 off = isl_dim_total(bmap->dim);
5017 for (i = 0; i < bmap->n_div; ++i) {
5018 if (isl_int_is_zero(bmap->div[i][0]))
5020 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5026 static int map_divs_known(__isl_keep isl_map *map)
5033 for (i = 0; i < map->n; ++i) {
5034 int known = basic_map_divs_known(map->p[i]);
5042 /* If bmap contains any unknown divs, then compute explicit
5043 * expressions for them. However, this computation may be
5044 * quite expensive, so first try to remove divs that aren't
5047 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5051 struct isl_map *map;
5053 known = basic_map_divs_known(bmap);
5057 return isl_map_from_basic_map(bmap);
5059 bmap = isl_basic_map_drop_redundant_divs(bmap);
5061 known = basic_map_divs_known(bmap);
5065 return isl_map_from_basic_map(bmap);
5067 map = compute_divs(bmap);
5070 isl_basic_map_free(bmap);
5074 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5078 struct isl_map *res;
5085 known = map_divs_known(map);
5093 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5094 for (i = 1 ; i < map->n; ++i) {
5096 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5097 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5098 res = isl_map_union_disjoint(res, r2);
5100 res = isl_map_union(res, r2);
5107 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5109 return (struct isl_set *)
5110 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5113 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5115 return (struct isl_set *)
5116 isl_map_compute_divs((struct isl_map *)set);
5119 struct isl_set *isl_map_domain(struct isl_map *map)
5122 struct isl_set *set;
5127 map = isl_map_cow(map);
5131 set = (struct isl_set *)map;
5132 set->dim = isl_dim_domain(set->dim);
5135 for (i = 0; i < map->n; ++i) {
5136 set->p[i] = isl_basic_map_domain(map->p[i]);
5140 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5141 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5148 struct isl_map *isl_map_union_disjoint(
5149 struct isl_map *map1, struct isl_map *map2)
5153 struct isl_map *map = NULL;
5167 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5169 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5170 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5171 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5173 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5174 map1->n + map2->n, flags);
5177 for (i = 0; i < map1->n; ++i) {
5178 map = isl_map_add_basic_map(map,
5179 isl_basic_map_copy(map1->p[i]));
5183 for (i = 0; i < map2->n; ++i) {
5184 map = isl_map_add_basic_map(map,
5185 isl_basic_map_copy(map2->p[i]));
5199 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5201 map1 = isl_map_union_disjoint(map1, map2);
5205 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5209 struct isl_set *isl_set_union_disjoint(
5210 struct isl_set *set1, struct isl_set *set2)
5212 return (struct isl_set *)
5213 isl_map_union_disjoint(
5214 (struct isl_map *)set1, (struct isl_map *)set2);
5217 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5219 return (struct isl_set *)
5220 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5223 struct isl_map *isl_map_intersect_range(
5224 struct isl_map *map, struct isl_set *set)
5227 struct isl_map *result;
5233 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5234 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5235 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5237 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5238 map->n * set->n, flags);
5241 for (i = 0; i < map->n; ++i)
5242 for (j = 0; j < set->n; ++j) {
5243 result = isl_map_add_basic_map(result,
5244 isl_basic_map_intersect_range(
5245 isl_basic_map_copy(map->p[i]),
5246 isl_basic_set_copy(set->p[j])));
5259 struct isl_map *isl_map_intersect_domain(
5260 struct isl_map *map, struct isl_set *set)
5262 return isl_map_reverse(
5263 isl_map_intersect_range(isl_map_reverse(map), set));
5266 struct isl_map *isl_map_apply_domain(
5267 struct isl_map *map1, struct isl_map *map2)
5271 map1 = isl_map_reverse(map1);
5272 map1 = isl_map_apply_range(map1, map2);
5273 return isl_map_reverse(map1);
5280 struct isl_map *isl_map_apply_range(
5281 struct isl_map *map1, struct isl_map *map2)
5283 struct isl_dim *dim_result;
5284 struct isl_map *result;
5290 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5291 isl_dim_copy(map2->dim));
5293 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5296 for (i = 0; i < map1->n; ++i)
5297 for (j = 0; j < map2->n; ++j) {
5298 result = isl_map_add_basic_map(result,
5299 isl_basic_map_apply_range(
5300 isl_basic_map_copy(map1->p[i]),
5301 isl_basic_map_copy(map2->p[j])));
5307 if (result && result->n <= 1)
5308 ISL_F_SET(result, ISL_MAP_DISJOINT);
5317 * returns range - domain
5319 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5321 isl_dim *dims, *target_dim;
5322 struct isl_basic_set *bset;
5329 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5330 bmap->dim, isl_dim_out),
5332 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5333 dim = isl_basic_map_n_in(bmap);
5334 nparam = isl_basic_map_n_param(bmap);
5335 bset = isl_basic_set_from_basic_map(bmap);
5336 bset = isl_basic_set_cow(bset);
5337 dims = isl_basic_set_get_dim(bset);
5338 dims = isl_dim_add(dims, isl_dim_set, dim);
5339 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5340 bset = isl_basic_set_swap_vars(bset, 2*dim);
5341 for (i = 0; i < dim; ++i) {
5342 int j = isl_basic_map_alloc_equality(
5343 (struct isl_basic_map *)bset);
5346 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5347 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5348 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5349 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5351 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5352 bset = isl_basic_set_reset_dim(bset, target_dim);
5355 isl_basic_map_free(bmap);
5360 * returns range - domain
5362 struct isl_set *isl_map_deltas(struct isl_map *map)
5366 struct isl_set *result;
5371 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5372 map->dim, isl_dim_out),
5374 dim = isl_map_get_dim(map);
5375 dim = isl_dim_domain(dim);
5376 result = isl_set_alloc_dim(dim, map->n, map->flags);
5379 for (i = 0; i < map->n; ++i)
5380 result = isl_set_add_basic_set(result,
5381 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5389 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5391 struct isl_basic_map *bmap;
5399 nparam = dims->nparam;
5401 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5405 for (i = 0; i < dim; ++i) {
5406 int j = isl_basic_map_alloc_equality(bmap);
5409 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5410 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5411 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5413 return isl_basic_map_finalize(bmap);
5415 isl_basic_map_free(bmap);
5419 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
5421 struct isl_dim *dim = isl_dim_map(set_dim);
5424 return basic_map_identity(dim);
5427 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5429 if (!model || !model->dim)
5431 isl_assert(model->ctx,
5432 model->dim->n_in == model->dim->n_out, return NULL);
5433 return basic_map_identity(isl_dim_copy(model->dim));
5436 static struct isl_map *map_identity(struct isl_dim *dim)
5438 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
5439 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
5442 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
5444 struct isl_dim *dim = isl_dim_map(set_dim);
5447 return map_identity(dim);
5450 struct isl_map *isl_map_identity_like(struct isl_map *model)
5452 if (!model || !model->dim)
5454 isl_assert(model->ctx,
5455 model->dim->n_in == model->dim->n_out, return NULL);
5456 return map_identity(isl_dim_copy(model->dim));
5459 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5461 if (!model || !model->dim)
5463 isl_assert(model->ctx,
5464 model->dim->n_in == model->dim->n_out, return NULL);
5465 return map_identity(isl_dim_copy(model->dim));
5468 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5470 isl_dim *dim = isl_set_get_dim(set);
5472 id = isl_map_identity(dim);
5473 return isl_map_intersect_range(id, set);
5476 /* Construct a basic set with all set dimensions having only non-negative
5479 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5484 struct isl_basic_set *bset;
5488 nparam = dims->nparam;
5490 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5493 for (i = 0; i < dim; ++i) {
5494 int k = isl_basic_set_alloc_inequality(bset);
5497 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5498 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5502 isl_basic_set_free(bset);
5506 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5507 enum isl_dim_type type, unsigned first, unsigned n)
5510 isl_basic_set *nonneg = NULL;
5511 isl_basic_set *neg = NULL;
5518 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5520 for (i = 0; i < n; ++i) {
5524 nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5525 k = isl_basic_set_alloc_inequality(nonneg);
5528 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5529 isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
5531 neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5532 k = isl_basic_set_alloc_inequality(neg);
5535 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5536 isl_int_set_si(neg->ineq[k][0], -1);
5537 isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
5539 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5544 isl_basic_set_free(nonneg);
5545 isl_basic_set_free(neg);
5550 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5552 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5555 int isl_basic_map_is_subset(
5556 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5559 struct isl_map *map1;
5560 struct isl_map *map2;
5562 if (!bmap1 || !bmap2)
5565 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5566 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5568 is_subset = isl_map_is_subset(map1, map2);
5576 int isl_basic_map_is_equal(
5577 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5581 if (!bmap1 || !bmap2)
5583 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5586 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5590 int isl_basic_set_is_equal(
5591 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5593 return isl_basic_map_is_equal(
5594 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5597 int isl_map_is_empty(struct isl_map *map)
5604 for (i = 0; i < map->n; ++i) {
5605 is_empty = isl_basic_map_is_empty(map->p[i]);
5614 int isl_map_fast_is_empty(struct isl_map *map)
5616 return map ? map->n == 0 : -1;
5619 int isl_set_fast_is_empty(struct isl_set *set)
5621 return set ? set->n == 0 : -1;
5624 int isl_set_is_empty(struct isl_set *set)
5626 return isl_map_is_empty((struct isl_map *)set);
5629 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
5634 return isl_dim_equal(map1->dim, map2->dim);
5637 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5642 return isl_dim_equal(set1->dim, set2->dim);
5645 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
5651 is_subset = isl_map_is_subset(map1, map2);
5654 is_subset = isl_map_is_subset(map2, map1);
5658 int isl_basic_map_is_strict_subset(
5659 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5663 if (!bmap1 || !bmap2)
5665 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5668 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5669 if (is_subset == -1)
5674 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
5680 is_subset = isl_map_is_subset(map1, map2);
5683 is_subset = isl_map_is_subset(map2, map1);
5684 if (is_subset == -1)
5689 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5691 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5694 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5698 return bmap->n_eq == 0 && bmap->n_ineq == 0;
5701 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5705 return bset->n_eq == 0 && bset->n_ineq == 0;
5708 int isl_map_fast_is_universe(__isl_keep isl_map *map)
5715 for (i = 0; i < map->n; ++i) {
5716 int r = isl_basic_map_is_universe(map->p[i]);
5724 int isl_set_fast_is_universe(__isl_keep isl_set *set)
5726 return isl_map_fast_is_universe((isl_map *) set);
5729 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
5731 struct isl_basic_set *bset = NULL;
5732 struct isl_vec *sample = NULL;
5739 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
5742 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
5743 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
5744 copy = isl_basic_map_remove_redundancies(copy);
5745 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
5746 isl_basic_map_free(copy);
5750 total = 1 + isl_basic_map_total_dim(bmap);
5751 if (bmap->sample && bmap->sample->size == total) {
5752 int contains = isl_basic_map_contains(bmap, bmap->sample);
5758 isl_vec_free(bmap->sample);
5759 bmap->sample = NULL;
5760 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
5763 sample = isl_basic_set_sample_vec(bset);
5766 empty = sample->size == 0;
5767 isl_vec_free(bmap->sample);
5768 bmap->sample = sample;
5770 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
5775 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
5779 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
5782 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
5786 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
5789 int isl_basic_set_is_empty(struct isl_basic_set *bset)
5791 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
5794 struct isl_map *isl_basic_map_union(
5795 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5797 struct isl_map *map;
5798 if (!bmap1 || !bmap2)
5801 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
5803 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
5806 map = isl_map_add_basic_map(map, bmap1);
5807 map = isl_map_add_basic_map(map, bmap2);
5810 isl_basic_map_free(bmap1);
5811 isl_basic_map_free(bmap2);
5815 struct isl_set *isl_basic_set_union(
5816 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5818 return (struct isl_set *)isl_basic_map_union(
5819 (struct isl_basic_map *)bset1,
5820 (struct isl_basic_map *)bset2);
5823 /* Order divs such that any div only depends on previous divs */
5824 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
5832 off = isl_dim_total(bmap->dim);
5834 for (i = 0; i < bmap->n_div; ++i) {
5836 if (isl_int_is_zero(bmap->div[i][0]))
5838 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
5842 isl_basic_map_swap_div(bmap, i, i + pos);
5848 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
5850 return (struct isl_basic_set *)
5851 isl_basic_map_order_divs((struct isl_basic_map *)bset);
5854 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
5861 for (i = 0; i < map->n; ++i) {
5862 map->p[i] = isl_basic_map_order_divs(map->p[i]);
5873 /* Look for a div in dst that corresponds to the div "div" in src.
5874 * The divs before "div" in src and dst are assumed to be the same.
5876 * Returns -1 if no corresponding div was found and the position
5877 * of the corresponding div in dst otherwise.
5879 static int find_div(struct isl_basic_map *dst,
5880 struct isl_basic_map *src, unsigned div)
5884 unsigned total = isl_dim_total(src->dim);
5886 isl_assert(dst->ctx, div <= dst->n_div, return -1);
5887 for (i = div; i < dst->n_div; ++i)
5888 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
5889 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
5890 dst->n_div - div) == -1)
5895 struct isl_basic_map *isl_basic_map_align_divs(
5896 struct isl_basic_map *dst, struct isl_basic_map *src)
5899 unsigned total = isl_dim_total(src->dim);
5904 if (src->n_div == 0)
5907 for (i = 0; i < src->n_div; ++i)
5908 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
5910 src = isl_basic_map_order_divs(src);
5911 dst = isl_basic_map_cow(dst);
5912 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
5913 src->n_div, 0, 2 * src->n_div);
5916 for (i = 0; i < src->n_div; ++i) {
5917 int j = find_div(dst, src, i);
5919 j = isl_basic_map_alloc_div(dst);
5922 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
5923 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
5924 if (isl_basic_map_add_div_constraints(dst, j) < 0)
5928 isl_basic_map_swap_div(dst, i, j);
5932 isl_basic_map_free(dst);
5936 struct isl_basic_set *isl_basic_set_align_divs(
5937 struct isl_basic_set *dst, struct isl_basic_set *src)
5939 return (struct isl_basic_set *)isl_basic_map_align_divs(
5940 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
5943 struct isl_map *isl_map_align_divs(struct isl_map *map)
5951 map = isl_map_compute_divs(map);
5952 map = isl_map_cow(map);
5956 for (i = 1; i < map->n; ++i)
5957 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5958 for (i = 1; i < map->n; ++i)
5959 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5961 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5965 struct isl_set *isl_set_align_divs(struct isl_set *set)
5967 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5970 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5974 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5975 map = isl_map_intersect_domain(map, set);
5976 set = isl_map_range(map);
5984 /* There is no need to cow as removing empty parts doesn't change
5985 * the meaning of the set.
5987 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5994 for (i = map->n-1; i >= 0; --i) {
5995 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5997 isl_basic_map_free(map->p[i]);
5998 if (i != map->n-1) {
5999 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6000 map->p[i] = map->p[map->n-1];
6008 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6010 return (struct isl_set *)
6011 isl_map_remove_empty_parts((struct isl_map *)set);
6014 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6016 struct isl_basic_map *bmap;
6017 if (!map || map->n == 0)
6019 bmap = map->p[map->n-1];
6020 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6021 return isl_basic_map_copy(bmap);
6024 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6026 return (struct isl_basic_set *)
6027 isl_map_copy_basic_map((struct isl_map *)set);
6030 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6031 __isl_keep isl_basic_map *bmap)
6037 for (i = map->n-1; i >= 0; --i) {
6038 if (map->p[i] != bmap)
6040 map = isl_map_cow(map);
6043 isl_basic_map_free(map->p[i]);
6044 if (i != map->n-1) {
6045 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6046 map->p[i] = map->p[map->n-1];
6057 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6058 struct isl_basic_set *bset)
6060 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6061 (struct isl_basic_map *)bset);
6064 /* Given two basic sets bset1 and bset2, compute the maximal difference
6065 * between the values of dimension pos in bset1 and those in bset2
6066 * for any common value of the parameters and dimensions preceding pos.
6068 static enum isl_lp_result basic_set_maximal_difference_at(
6069 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6070 int pos, isl_int *opt)
6072 struct isl_dim *dims;
6073 struct isl_basic_map *bmap1 = NULL;
6074 struct isl_basic_map *bmap2 = NULL;
6075 struct isl_ctx *ctx;
6076 struct isl_vec *obj;
6079 unsigned dim1, dim2;
6080 enum isl_lp_result res;
6082 if (!bset1 || !bset2)
6083 return isl_lp_error;
6085 nparam = isl_basic_set_n_param(bset1);
6086 dim1 = isl_basic_set_n_dim(bset1);
6087 dim2 = isl_basic_set_n_dim(bset2);
6088 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6089 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6090 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6091 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6092 if (!bmap1 || !bmap2)
6094 bmap1 = isl_basic_map_cow(bmap1);
6095 bmap1 = isl_basic_map_extend(bmap1, nparam,
6096 pos, (dim1 - pos) + (dim2 - pos),
6097 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6098 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6101 total = isl_basic_map_total_dim(bmap1);
6103 obj = isl_vec_alloc(ctx, 1 + total);
6104 isl_seq_clr(obj->block.data, 1 + total);
6105 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6106 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6109 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6111 isl_basic_map_free(bmap1);
6115 isl_basic_map_free(bmap1);
6116 isl_basic_map_free(bmap2);
6117 return isl_lp_error;
6120 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6121 * for any common value of the parameters and dimensions preceding pos
6122 * in both basic sets, the values of dimension pos in bset1 are
6123 * smaller or larger than those in bset2.
6126 * 1 if bset1 follows bset2
6127 * -1 if bset1 precedes bset2
6128 * 0 if bset1 and bset2 are incomparable
6129 * -2 if some error occurred.
6131 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6132 struct isl_basic_set *bset2, int pos)
6135 enum isl_lp_result res;
6140 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6142 if (res == isl_lp_empty)
6144 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6145 res == isl_lp_unbounded)
6147 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6156 /* Given two basic sets bset1 and bset2, check whether
6157 * for any common value of the parameters and dimensions preceding pos
6158 * there is a value of dimension pos in bset1 that is larger
6159 * than a value of the same dimension in bset2.
6162 * 1 if there exists such a pair
6163 * 0 if there is no such pair, but there is a pair of equal values
6165 * -2 if some error occurred.
6167 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6168 __isl_keep isl_basic_set *bset2, int pos)
6171 enum isl_lp_result res;
6176 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6178 if (res == isl_lp_empty)
6180 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6181 res == isl_lp_unbounded)
6183 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6185 else if (res == isl_lp_ok)
6194 /* Given two sets set1 and set2, check whether
6195 * for any common value of the parameters and dimensions preceding pos
6196 * there is a value of dimension pos in set1 that is larger
6197 * than a value of the same dimension in set2.
6200 * 1 if there exists such a pair
6201 * 0 if there is no such pair, but there is a pair of equal values
6203 * -2 if some error occurred.
6205 int isl_set_follows_at(__isl_keep isl_set *set1,
6206 __isl_keep isl_set *set2, int pos)
6214 for (i = 0; i < set1->n; ++i)
6215 for (j = 0; j < set2->n; ++j) {
6217 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6218 if (f == 1 || f == -2)
6227 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
6228 unsigned pos, isl_int *val)
6236 total = isl_basic_map_total_dim(bmap);
6237 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6238 for (; d+1 > pos; --d)
6239 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6243 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6245 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6247 if (!isl_int_is_one(bmap->eq[i][1+d]))
6250 isl_int_neg(*val, bmap->eq[i][0]);
6256 static int isl_map_fast_has_fixed_var(struct isl_map *map,
6257 unsigned pos, isl_int *val)
6269 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
6272 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
6273 for (i = 1; fixed == 1 && i < map->n; ++i) {
6274 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
6275 if (fixed == 1 && isl_int_ne(tmp, v))
6279 isl_int_set(*val, v);
6285 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
6286 unsigned pos, isl_int *val)
6288 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
6292 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
6295 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
6298 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
6299 enum isl_dim_type type, unsigned pos, isl_int *val)
6301 if (pos >= isl_basic_map_dim(bmap, type))
6303 return isl_basic_map_fast_has_fixed_var(bmap,
6304 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6307 int isl_map_fast_is_fixed(struct isl_map *map,
6308 enum isl_dim_type type, unsigned pos, isl_int *val)
6310 if (pos >= isl_map_dim(map, type))
6312 return isl_map_fast_has_fixed_var(map,
6313 map_offset(map, type) - 1 + pos, val);
6316 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6317 * then return this fixed value in *val.
6319 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6322 return isl_basic_set_fast_has_fixed_var(bset,
6323 isl_basic_set_n_param(bset) + dim, val);
6326 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6327 * then return this fixed value in *val.
6329 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6331 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6334 /* Check if input variable in has fixed value and if so and if val is not NULL,
6335 * then return this fixed value in *val.
6337 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6339 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6342 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6343 * and if val is not NULL, then return this lower bound in *val.
6345 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6346 unsigned dim, isl_int *val)
6348 int i, i_eq = -1, i_ineq = -1;
6355 total = isl_basic_set_total_dim(bset);
6356 nparam = isl_basic_set_n_param(bset);
6357 for (i = 0; i < bset->n_eq; ++i) {
6358 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6364 for (i = 0; i < bset->n_ineq; ++i) {
6365 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6367 if (i_eq != -1 || i_ineq != -1)
6371 if (i_eq == -1 && i_ineq == -1)
6373 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6374 /* The coefficient should always be one due to normalization. */
6375 if (!isl_int_is_one(c[1+nparam+dim]))
6377 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6379 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6380 total - nparam - dim - 1) != -1)
6383 isl_int_neg(*val, c[0]);
6387 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6388 unsigned dim, isl_int *val)
6400 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6404 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6406 for (i = 1; fixed == 1 && i < set->n; ++i) {
6407 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6409 if (fixed == 1 && isl_int_ne(tmp, v))
6413 isl_int_set(*val, v);
6424 static int qsort_constraint_cmp(const void *p1, const void *p2)
6426 const struct constraint *c1 = (const struct constraint *)p1;
6427 const struct constraint *c2 = (const struct constraint *)p2;
6428 unsigned size = isl_min(c1->size, c2->size);
6429 return isl_seq_cmp(c1->c, c2->c, size);
6432 static struct isl_basic_map *isl_basic_map_sort_constraints(
6433 struct isl_basic_map *bmap)
6436 struct constraint *c;
6441 total = isl_basic_map_total_dim(bmap);
6442 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6445 for (i = 0; i < bmap->n_ineq; ++i) {
6447 c[i].c = bmap->ineq[i];
6449 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6450 for (i = 0; i < bmap->n_ineq; ++i)
6451 bmap->ineq[i] = c[i].c;
6455 isl_basic_map_free(bmap);
6459 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6460 __isl_take isl_basic_set *bset)
6462 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6463 (struct isl_basic_map *)bset);
6466 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6470 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6472 bmap = isl_basic_map_remove_redundancies(bmap);
6473 bmap = isl_basic_map_sort_constraints(bmap);
6474 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6478 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6480 return (struct isl_basic_set *)isl_basic_map_normalize(
6481 (struct isl_basic_map *)bset);
6484 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6485 const __isl_keep isl_basic_map *bmap2)
6492 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6493 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6494 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6495 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6496 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6497 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6498 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6499 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6501 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6503 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6505 if (bmap1->n_eq != bmap2->n_eq)
6506 return bmap1->n_eq - bmap2->n_eq;
6507 if (bmap1->n_ineq != bmap2->n_ineq)
6508 return bmap1->n_ineq - bmap2->n_ineq;
6509 if (bmap1->n_div != bmap2->n_div)
6510 return bmap1->n_div - bmap2->n_div;
6511 total = isl_basic_map_total_dim(bmap1);
6512 for (i = 0; i < bmap1->n_eq; ++i) {
6513 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6517 for (i = 0; i < bmap1->n_ineq; ++i) {
6518 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6522 for (i = 0; i < bmap1->n_div; ++i) {
6523 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6530 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6531 __isl_keep isl_basic_map *bmap2)
6533 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6536 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6537 __isl_keep isl_basic_set *bset2)
6539 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6540 (isl_basic_map *)bset2);
6543 static int qsort_bmap_cmp(const void *p1, const void *p2)
6545 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6546 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6548 return isl_basic_map_fast_cmp(bmap1, bmap2);
6551 /* We normalize in place, but if anything goes wrong we need
6552 * to return NULL, so we need to make sure we don't change the
6553 * meaning of any possible other copies of map.
6555 struct isl_map *isl_map_normalize(struct isl_map *map)
6558 struct isl_basic_map *bmap;
6562 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6564 for (i = 0; i < map->n; ++i) {
6565 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6568 isl_basic_map_free(map->p[i]);
6571 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6572 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6573 map = isl_map_remove_empty_parts(map);
6576 for (i = map->n - 1; i >= 1; --i) {
6577 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6579 isl_basic_map_free(map->p[i-1]);
6580 for (j = i; j < map->n; ++j)
6581 map->p[j-1] = map->p[j];
6591 struct isl_set *isl_set_normalize(struct isl_set *set)
6593 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
6596 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
6606 if (!isl_dim_equal(map1->dim, map2->dim))
6609 map1 = isl_map_copy(map1);
6610 map2 = isl_map_copy(map2);
6611 map1 = isl_map_normalize(map1);
6612 map2 = isl_map_normalize(map2);
6615 equal = map1->n == map2->n;
6616 for (i = 0; equal && i < map1->n; ++i) {
6617 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
6630 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
6632 return isl_map_fast_is_equal((struct isl_map *)set1,
6633 (struct isl_map *)set2);
6636 /* Return an interval that ranges from min to max (inclusive)
6638 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
6639 isl_int min, isl_int max)
6642 struct isl_basic_set *bset = NULL;
6644 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
6648 k = isl_basic_set_alloc_inequality(bset);
6651 isl_int_set_si(bset->ineq[k][1], 1);
6652 isl_int_neg(bset->ineq[k][0], min);
6654 k = isl_basic_set_alloc_inequality(bset);
6657 isl_int_set_si(bset->ineq[k][1], -1);
6658 isl_int_set(bset->ineq[k][0], max);
6662 isl_basic_set_free(bset);
6666 /* Return the Cartesian product of the basic sets in list (in the given order).
6668 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
6676 struct isl_basic_set *product = NULL;
6680 isl_assert(list->ctx, list->n > 0, goto error);
6681 isl_assert(list->ctx, list->p[0], goto error);
6682 nparam = isl_basic_set_n_param(list->p[0]);
6683 dim = isl_basic_set_n_dim(list->p[0]);
6684 extra = list->p[0]->n_div;
6685 n_eq = list->p[0]->n_eq;
6686 n_ineq = list->p[0]->n_ineq;
6687 for (i = 1; i < list->n; ++i) {
6688 isl_assert(list->ctx, list->p[i], goto error);
6689 isl_assert(list->ctx,
6690 nparam == isl_basic_set_n_param(list->p[i]), goto error);
6691 dim += isl_basic_set_n_dim(list->p[i]);
6692 extra += list->p[i]->n_div;
6693 n_eq += list->p[i]->n_eq;
6694 n_ineq += list->p[i]->n_ineq;
6696 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
6701 for (i = 0; i < list->n; ++i) {
6702 isl_basic_set_add_constraints(product,
6703 isl_basic_set_copy(list->p[i]), dim);
6704 dim += isl_basic_set_n_dim(list->p[i]);
6706 isl_basic_set_list_free(list);
6709 isl_basic_set_free(product);
6710 isl_basic_set_list_free(list);
6714 struct isl_basic_map *isl_basic_map_product(
6715 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6717 struct isl_dim *dim_result = NULL;
6718 struct isl_basic_map *bmap;
6719 unsigned in1, in2, out1, out2, nparam, total, pos;
6720 struct isl_dim_map *dim_map1, *dim_map2;
6722 if (!bmap1 || !bmap2)
6725 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
6726 bmap2->dim, isl_dim_param), goto error);
6727 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
6728 isl_dim_copy(bmap2->dim));
6730 in1 = isl_basic_map_n_in(bmap1);
6731 in2 = isl_basic_map_n_in(bmap2);
6732 out1 = isl_basic_map_n_out(bmap1);
6733 out2 = isl_basic_map_n_out(bmap2);
6734 nparam = isl_basic_map_n_param(bmap1);
6736 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
6737 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
6738 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
6739 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
6740 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
6741 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
6742 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
6743 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
6744 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
6745 isl_dim_map_div(dim_map1, bmap1, pos += out2);
6746 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
6748 bmap = isl_basic_map_alloc_dim(dim_result,
6749 bmap1->n_div + bmap2->n_div,
6750 bmap1->n_eq + bmap2->n_eq,
6751 bmap1->n_ineq + bmap2->n_ineq);
6752 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
6753 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
6754 bmap = isl_basic_map_simplify(bmap);
6755 return isl_basic_map_finalize(bmap);
6757 isl_basic_map_free(bmap1);
6758 isl_basic_map_free(bmap2);
6762 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
6764 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
6767 struct isl_map *result;
6773 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
6774 map2->dim, isl_dim_param), goto error);
6776 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6777 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6778 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6780 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
6781 isl_dim_copy(map2->dim)),
6782 map1->n * map2->n, flags);
6785 for (i = 0; i < map1->n; ++i)
6786 for (j = 0; j < map2->n; ++j) {
6787 struct isl_basic_map *part;
6788 part = isl_basic_map_product(
6789 isl_basic_map_copy(map1->p[i]),
6790 isl_basic_map_copy(map2->p[j]));
6791 if (isl_basic_map_is_empty(part))
6792 isl_basic_map_free(part);
6794 result = isl_map_add_basic_map(result, part);
6807 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6809 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
6810 __isl_take isl_map *map2)
6814 prod = isl_map_product(map1, map2);
6815 prod = isl_map_flatten(prod);
6819 /* Given two set A and B, construct its Cartesian product A x B.
6821 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
6823 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
6824 (struct isl_map *)set2);
6827 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
6828 __isl_take isl_set *set2)
6830 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
6833 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
6836 uint32_t hash = isl_hash_init();
6841 bset = isl_basic_set_copy(bset);
6842 bset = isl_basic_set_normalize(bset);
6845 total = isl_basic_set_total_dim(bset);
6846 isl_hash_byte(hash, bset->n_eq & 0xFF);
6847 for (i = 0; i < bset->n_eq; ++i) {
6849 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
6850 isl_hash_hash(hash, c_hash);
6852 isl_hash_byte(hash, bset->n_ineq & 0xFF);
6853 for (i = 0; i < bset->n_ineq; ++i) {
6855 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
6856 isl_hash_hash(hash, c_hash);
6858 isl_hash_byte(hash, bset->n_div & 0xFF);
6859 for (i = 0; i < bset->n_div; ++i) {
6861 if (isl_int_is_zero(bset->div[i][0]))
6863 isl_hash_byte(hash, i & 0xFF);
6864 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
6865 isl_hash_hash(hash, c_hash);
6867 isl_basic_set_free(bset);
6871 uint32_t isl_set_get_hash(struct isl_set *set)
6878 set = isl_set_copy(set);
6879 set = isl_set_normalize(set);
6883 hash = isl_hash_init();
6884 for (i = 0; i < set->n; ++i) {
6886 bset_hash = isl_basic_set_get_hash(set->p[i]);
6887 isl_hash_hash(hash, bset_hash);
6895 /* Check if the value for dimension dim is completely determined
6896 * by the values of the other parameters and variables.
6897 * That is, check if dimension dim is involved in an equality.
6899 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
6906 nparam = isl_basic_set_n_param(bset);
6907 for (i = 0; i < bset->n_eq; ++i)
6908 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
6913 /* Check if the value for dimension dim is completely determined
6914 * by the values of the other parameters and variables.
6915 * That is, check if dimension dim is involved in an equality
6916 * for each of the subsets.
6918 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
6924 for (i = 0; i < set->n; ++i) {
6926 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
6933 int isl_set_n_basic_set(__isl_keep isl_set *set)
6935 return set ? set->n : 0;
6938 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
6939 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
6946 for (i = 0; i < map->n; ++i)
6947 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
6953 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
6954 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
6961 for (i = 0; i < set->n; ++i)
6962 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
6968 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
6970 struct isl_dim *dim;
6975 if (bset->n_div == 0)
6978 bset = isl_basic_set_cow(bset);
6982 dim = isl_basic_set_get_dim(bset);
6983 dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
6986 isl_dim_free(bset->dim);
6990 bset = isl_basic_set_finalize(bset);
6994 isl_basic_set_free(bset);
6998 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7001 struct isl_dim *dim;
7004 set = isl_set_align_divs(set);
7008 if (set->n == 0 || set->p[0]->n_div == 0)
7011 set = isl_set_cow(set);
7015 n_div = set->p[0]->n_div;
7016 dim = isl_set_get_dim(set);
7017 dim = isl_dim_add(dim, isl_dim_set, n_div);
7020 isl_dim_free(set->dim);
7023 for (i = 0; i < set->n; ++i) {
7024 set->p[i] = isl_basic_set_lift(set->p[i]);
7035 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7037 struct isl_dim *dim;
7038 struct isl_basic_map *bmap;
7045 set = isl_set_align_divs(set);
7050 dim = isl_set_get_dim(set);
7051 if (set->n == 0 || set->p[0]->n_div == 0) {
7053 return isl_map_identity(dim);
7056 n_div = set->p[0]->n_div;
7057 dim = isl_dim_map(dim);
7058 n_param = isl_dim_size(dim, isl_dim_param);
7059 n_set = isl_dim_size(dim, isl_dim_in);
7060 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7061 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7062 for (i = 0; i < n_set; ++i)
7063 bmap = var_equal(bmap, i);
7065 total = n_param + n_set + n_set + n_div;
7066 for (i = 0; i < n_div; ++i) {
7067 k = isl_basic_map_alloc_inequality(bmap);
7070 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7071 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7072 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7073 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7074 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7075 set->p[0]->div[i][0]);
7077 l = isl_basic_map_alloc_inequality(bmap);
7080 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7081 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7082 set->p[0]->div[i][0]);
7083 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7087 return isl_map_from_basic_map(bmap);
7090 isl_basic_map_free(bmap);
7094 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7102 dim = isl_basic_set_total_dim(bset);
7103 size += bset->n_eq * (1 + dim);
7104 size += bset->n_ineq * (1 + dim);
7105 size += bset->n_div * (2 + dim);
7110 int isl_set_size(__isl_keep isl_set *set)
7118 for (i = 0; i < set->n; ++i)
7119 size += isl_basic_set_size(set->p[i]);
7124 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7125 enum isl_dim_type type, unsigned pos)
7133 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7135 pos += isl_basic_map_offset(bmap, type);
7137 for (i = 0; i < bmap->n_eq; ++i)
7138 if (!isl_int_is_zero(bmap->eq[i][pos]))
7142 for (i = 0; i < bmap->n_ineq; ++i) {
7143 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7150 return lower && upper;
7153 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7154 enum isl_dim_type type, unsigned pos)
7161 for (i = 0; i < map->n; ++i) {
7163 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7164 if (bounded < 0 || !bounded)
7171 /* Return 1 if the specified dim is involved in both an upper bound
7172 * and a lower bound.
7174 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7175 enum isl_dim_type type, unsigned pos)
7177 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7180 /* For each of the "n" variables starting at "first", determine
7181 * the sign of the variable and put the results in the first "n"
7182 * elements of the array "signs".
7184 * 1 means that the variable is non-negative
7185 * -1 means that the variable is non-positive
7186 * 0 means the variable attains both positive and negative values.
7188 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7189 unsigned first, unsigned n, int *signs)
7191 isl_vec *bound = NULL;
7192 struct isl_tab *tab = NULL;
7193 struct isl_tab_undo *snap;
7196 if (!bset || !signs)
7199 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7200 tab = isl_tab_from_basic_set(bset);
7204 isl_seq_clr(bound->el, bound->size);
7205 isl_int_set_si(bound->el[0], -1);
7207 snap = isl_tab_snap(tab);
7208 for (i = 0; i < n; ++i) {
7211 isl_int_set_si(bound->el[1 + first + i], -1);
7212 if (isl_tab_add_ineq(tab, bound->el) < 0)
7215 isl_int_set_si(bound->el[1 + first + i], 0);
7216 if (isl_tab_rollback(tab, snap) < 0)
7224 isl_int_set_si(bound->el[1 + first + i], 1);
7225 if (isl_tab_add_ineq(tab, bound->el) < 0)
7228 isl_int_set_si(bound->el[1 + first + i], 0);
7229 if (isl_tab_rollback(tab, snap) < 0)
7232 signs[i] = empty ? -1 : 0;
7236 isl_vec_free(bound);
7240 isl_vec_free(bound);
7244 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7245 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7247 if (!bset || !signs)
7249 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7252 first += pos(bset->dim, type) - 1;
7253 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7256 /* Check if the given map is single-valued.
7261 * and check if the result is a subset of the identity mapping.
7263 int isl_map_is_single_valued(__isl_keep isl_map *map)
7269 test = isl_map_reverse(isl_map_copy(map));
7270 test = isl_map_apply_range(test, isl_map_copy(map));
7272 id = isl_map_identity(isl_dim_range(isl_map_get_dim(map)));
7274 sv = isl_map_is_subset(test, id);
7282 int isl_map_is_bijective(__isl_keep isl_map *map)
7286 sv = isl_map_is_single_valued(map);
7290 map = isl_map_copy(map);
7291 map = isl_map_reverse(map);
7292 sv = isl_map_is_single_valued(map);
7298 int isl_set_is_singleton(__isl_keep isl_set *set)
7300 return isl_map_is_single_valued((isl_map *)set);
7303 int isl_map_is_translation(__isl_keep isl_map *map)
7308 delta = isl_map_deltas(isl_map_copy(map));
7309 ok = isl_set_is_singleton(delta);
7310 isl_set_free(delta);
7315 static int unique(isl_int *p, unsigned pos, unsigned len)
7317 if (isl_seq_first_non_zero(p, pos) != -1)
7319 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7324 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7333 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7336 nvar = isl_basic_set_dim(bset, isl_dim_set);
7337 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7338 for (j = 0; j < nvar; ++j) {
7339 int lower = 0, upper = 0;
7340 for (i = 0; i < bset->n_eq; ++i) {
7341 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7343 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7349 for (i = 0; i < bset->n_ineq; ++i) {
7350 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7352 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7354 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7359 if (!lower || !upper)
7366 int isl_set_is_box(__isl_keep isl_set *set)
7373 return isl_basic_set_is_box(set->p[0]);
7376 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7381 return isl_dim_is_wrapping(bset->dim);
7384 int isl_set_is_wrapping(__isl_keep isl_set *set)
7389 return isl_dim_is_wrapping(set->dim);
7392 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7394 bmap = isl_basic_map_cow(bmap);
7398 bmap->dim = isl_dim_wrap(bmap->dim);
7402 bmap = isl_basic_map_finalize(bmap);
7404 return (isl_basic_set *)bmap;
7406 isl_basic_map_free(bmap);
7410 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7414 map = isl_map_cow(map);
7418 for (i = 0; i < map->n; ++i) {
7419 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7423 map->dim = isl_dim_wrap(map->dim);
7427 return (isl_set *)map;
7433 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
7435 bset = isl_basic_set_cow(bset);
7439 bset->dim = isl_dim_unwrap(bset->dim);
7443 bset = isl_basic_set_finalize(bset);
7445 return (isl_basic_map *)bset;
7447 isl_basic_set_free(bset);
7451 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
7458 if (!isl_set_is_wrapping(set))
7459 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
7462 set = isl_set_cow(set);
7466 for (i = 0; i < set->n; ++i) {
7467 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
7472 set->dim = isl_dim_unwrap(set->dim);
7476 return (isl_map *)set;
7482 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
7483 enum isl_dim_type type)
7488 if (!isl_dim_is_named_or_nested(bmap->dim, type))
7491 bmap = isl_basic_map_cow(bmap);
7495 bmap->dim = isl_dim_reset(bmap->dim, type);
7499 bmap = isl_basic_map_finalize(bmap);
7503 isl_basic_map_free(bmap);
7507 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
7508 enum isl_dim_type type)
7515 if (!isl_dim_is_named_or_nested(map->dim, type))
7518 map = isl_map_cow(map);
7522 for (i = 0; i < map->n; ++i) {
7523 map->p[i] = isl_basic_map_reset(map->p[i], type);
7527 map->dim = isl_dim_reset(map->dim, type);
7537 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
7542 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
7545 bmap = isl_basic_map_cow(bmap);
7549 bmap->dim = isl_dim_flatten(bmap->dim);
7553 bmap = isl_basic_map_finalize(bmap);
7557 isl_basic_map_free(bmap);
7561 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
7568 if (!map->dim->nested[0] && !map->dim->nested[1])
7571 map = isl_map_cow(map);
7575 for (i = 0; i < map->n; ++i) {
7576 map->p[i] = isl_basic_map_flatten(map->p[i]);
7580 map->dim = isl_dim_flatten(map->dim);
7590 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
7592 return (isl_set *)isl_map_flatten((isl_map *)set);
7595 /* Extend the given dim_map with mappings for the divs in bmap.
7597 static __isl_give struct isl_dim_map *extend_dim_map(
7598 __isl_keep struct isl_dim_map *dim_map,
7599 __isl_keep isl_basic_map *bmap)
7602 struct isl_dim_map *res;
7605 offset = isl_basic_map_offset(bmap, isl_dim_div);
7607 res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
7611 for (i = 0; i < dim_map->len; ++i)
7612 res->pos[i] = dim_map->pos[i];
7613 for (i = 0; i < bmap->n_div; ++i)
7614 res->pos[dim_map->len + i] = offset + i;
7619 /* Extract a dim_map from a reordering.
7620 * We essentially need to reverse the mapping, and add an offset
7621 * of 1 for the constant term.
7623 __isl_give struct isl_dim_map *isl_dim_map_from_reordering(
7624 __isl_keep isl_reordering *exp)
7627 struct isl_dim_map *dim_map;
7632 dim_map = isl_dim_map_alloc(exp->dim->ctx, isl_dim_total(exp->dim));
7636 for (i = 0; i < exp->len; ++i)
7637 dim_map->pos[1 + exp->pos[i]] = 1 + i;
7642 /* Reorder the dimensions of "bmap" according to the given dim_map
7643 * and set the dimension specification to "dim".
7645 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
7646 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
7650 bmap = isl_basic_map_cow(bmap);
7651 if (!bmap || !dim || !dim_map)
7654 res = isl_basic_map_alloc_dim(dim,
7655 bmap->n_div, bmap->n_eq, bmap->n_ineq);
7656 res = add_constraints_dim_map(res, bmap, dim_map);
7657 res = isl_basic_map_finalize(res);
7661 isl_basic_map_free(bmap);
7666 /* Reorder the dimensions of "map" according to given reordering.
7668 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
7669 __isl_take isl_reordering *r)
7672 struct isl_dim_map *dim_map;
7674 map = isl_map_cow(map);
7675 dim_map = isl_dim_map_from_reordering(r);
7676 if (!map || !r || !dim_map)
7679 for (i = 0; i < map->n; ++i) {
7680 struct isl_dim_map *dim_map_i;
7682 dim_map_i = extend_dim_map(dim_map, map->p[i]);
7684 map->p[i] = isl_basic_map_realign(map->p[i],
7685 isl_dim_copy(r->dim), dim_map_i);
7691 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
7693 isl_reordering_free(r);
7699 isl_reordering_free(r);
7703 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
7704 __isl_take isl_reordering *r)
7706 return (isl_set *)isl_map_realign((isl_map *)set, r);
7709 __isl_give isl_mat *isl_basic_map_equalities_matrix(
7710 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
7711 enum isl_dim_type c2, enum isl_dim_type c3,
7712 enum isl_dim_type c4, enum isl_dim_type c5)
7714 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
7715 struct isl_mat *mat;
7721 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
7722 isl_basic_map_total_dim(bmap) + 1);
7725 for (i = 0; i < bmap->n_eq; ++i)
7726 for (j = 0, pos = 0; j < 5; ++j) {
7727 int off = isl_basic_map_offset(bmap, c[j]);
7728 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7729 isl_int_set(mat->row[i][pos],
7730 bmap->eq[i][off + k]);
7738 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
7739 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
7740 enum isl_dim_type c2, enum isl_dim_type c3,
7741 enum isl_dim_type c4, enum isl_dim_type c5)
7743 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
7744 struct isl_mat *mat;
7750 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
7751 isl_basic_map_total_dim(bmap) + 1);
7754 for (i = 0; i < bmap->n_ineq; ++i)
7755 for (j = 0, pos = 0; j < 5; ++j) {
7756 int off = isl_basic_map_offset(bmap, c[j]);
7757 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7758 isl_int_set(mat->row[i][pos],
7759 bmap->ineq[i][off + k]);
7767 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
7768 __isl_take isl_dim *dim,
7769 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
7770 enum isl_dim_type c2, enum isl_dim_type c3,
7771 enum isl_dim_type c4, enum isl_dim_type c5)
7773 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
7774 isl_basic_map *bmap;
7780 if (!dim || !eq || !ineq)
7783 if (eq->n_col != ineq->n_col)
7784 isl_die(dim->ctx, isl_error_invalid,
7785 "equalities and inequalities matrices should have "
7786 "same number of columns", goto error);
7788 total = 1 + isl_dim_total(dim);
7790 if (eq->n_col < total)
7791 isl_die(dim->ctx, isl_error_invalid,
7792 "number of columns too small", goto error);
7794 extra = eq->n_col - total;
7796 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
7797 eq->n_row, ineq->n_row);
7800 for (i = 0; i < extra; ++i)
7801 if (isl_basic_map_alloc_div(bmap) < 0)
7803 for (i = 0; i < eq->n_row; ++i) {
7804 l = isl_basic_map_alloc_equality(bmap);
7807 for (j = 0, pos = 0; j < 5; ++j) {
7808 int off = isl_basic_map_offset(bmap, c[j]);
7809 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7810 isl_int_set(bmap->eq[l][off + k],
7816 for (i = 0; i < ineq->n_row; ++i) {
7817 l = isl_basic_map_alloc_inequality(bmap);
7820 for (j = 0, pos = 0; j < 5; ++j) {
7821 int off = isl_basic_map_offset(bmap, c[j]);
7822 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7823 isl_int_set(bmap->ineq[l][off + k],
7842 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
7843 __isl_take isl_dim *dim,
7844 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
7845 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
7847 return (isl_basic_set*)
7848 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
7849 c1, c2, c3, c4, isl_dim_in);