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 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1414 __isl_take isl_basic_map *bmap)
1416 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1424 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1425 __isl_take isl_basic_set *bset)
1427 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1428 (struct isl_basic_map *)bset);
1431 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1440 set = isl_set_cow(set);
1444 for (i = 0; i < set->n; ++i) {
1445 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1455 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1456 enum isl_dim_type type, unsigned first, unsigned n)
1460 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1464 bmap = isl_basic_map_eliminate_vars(bmap,
1465 isl_basic_map_offset(bmap, type) - 1 + first, n);
1468 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1470 bmap = isl_basic_map_drop(bmap, type, first, n);
1473 isl_basic_map_free(bmap);
1477 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1478 __isl_take isl_basic_set *bset,
1479 enum isl_dim_type type, unsigned first, unsigned n)
1481 return (isl_basic_set *)
1482 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1485 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1486 enum isl_dim_type type, unsigned first, unsigned n)
1493 map = isl_map_cow(map);
1496 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1498 for (i = 0; i < map->n; ++i) {
1499 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1500 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1504 map = isl_map_drop(map, type, first, n);
1511 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1512 enum isl_dim_type type, unsigned first, unsigned n)
1514 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1517 /* Project out n inputs starting at first using Fourier-Motzkin */
1518 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1519 unsigned first, unsigned n)
1521 return isl_map_remove_dims(map, isl_dim_in, first, n);
1524 static void dump_term(struct isl_basic_map *bmap,
1525 isl_int c, int pos, FILE *out)
1528 unsigned in = isl_basic_map_n_in(bmap);
1529 unsigned dim = in + isl_basic_map_n_out(bmap);
1530 unsigned nparam = isl_basic_map_n_param(bmap);
1532 isl_int_print(out, c, 0);
1534 if (!isl_int_is_one(c))
1535 isl_int_print(out, c, 0);
1536 if (pos < 1 + nparam) {
1537 name = isl_dim_get_name(bmap->dim,
1538 isl_dim_param, pos - 1);
1540 fprintf(out, "%s", name);
1542 fprintf(out, "p%d", pos - 1);
1543 } else if (pos < 1 + nparam + in)
1544 fprintf(out, "i%d", pos - 1 - nparam);
1545 else if (pos < 1 + nparam + dim)
1546 fprintf(out, "o%d", pos - 1 - nparam - in);
1548 fprintf(out, "e%d", pos - 1 - nparam - dim);
1552 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1553 int sign, FILE *out)
1557 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1561 for (i = 0, first = 1; i < len; ++i) {
1562 if (isl_int_sgn(c[i]) * sign <= 0)
1565 fprintf(out, " + ");
1567 isl_int_abs(v, c[i]);
1568 dump_term(bmap, v, i, out);
1575 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1576 const char *op, FILE *out, int indent)
1580 fprintf(out, "%*s", indent, "");
1582 dump_constraint_sign(bmap, c, 1, out);
1583 fprintf(out, " %s ", op);
1584 dump_constraint_sign(bmap, c, -1, out);
1588 for (i = bmap->n_div; i < bmap->extra; ++i) {
1589 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1591 fprintf(out, "%*s", indent, "");
1592 fprintf(out, "ERROR: unused div coefficient not zero\n");
1597 static void dump_constraints(struct isl_basic_map *bmap,
1598 isl_int **c, unsigned n,
1599 const char *op, FILE *out, int indent)
1603 for (i = 0; i < n; ++i)
1604 dump_constraint(bmap, c[i], op, out, indent);
1607 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1611 unsigned total = isl_basic_map_total_dim(bmap);
1613 for (j = 0; j < 1 + total; ++j) {
1614 if (isl_int_is_zero(exp[j]))
1616 if (!first && isl_int_is_pos(exp[j]))
1618 dump_term(bmap, exp[j], j, out);
1623 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1627 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1628 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1630 for (i = 0; i < bmap->n_div; ++i) {
1631 fprintf(out, "%*s", indent, "");
1632 fprintf(out, "e%d = [(", i);
1633 dump_affine(bmap, bmap->div[i]+1, out);
1635 isl_int_print(out, bmap->div[i][0], 0);
1636 fprintf(out, "]\n");
1640 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1643 fprintf(out, "null basic set\n");
1647 fprintf(out, "%*s", indent, "");
1648 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1649 bset->ref, bset->dim->nparam, bset->dim->n_out,
1650 bset->extra, bset->flags);
1651 dump((struct isl_basic_map *)bset, out, indent);
1654 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1657 fprintf(out, "null basic map\n");
1661 fprintf(out, "%*s", indent, "");
1662 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1663 "flags: %x, n_name: %d\n",
1665 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1666 bmap->extra, bmap->flags, bmap->dim->n_name);
1667 dump(bmap, out, indent);
1670 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1675 total = isl_basic_map_total_dim(bmap);
1676 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1677 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1678 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1679 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1683 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1685 struct isl_set *set;
1689 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1690 isl_assert(dim->ctx, n >= 0, goto error);
1691 set = isl_alloc(dim->ctx, struct isl_set,
1692 sizeof(struct isl_set) +
1693 (n - 1) * sizeof(struct isl_basic_set *));
1697 set->ctx = dim->ctx;
1698 isl_ctx_ref(set->ctx);
1710 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1711 unsigned nparam, unsigned dim, int n, unsigned flags)
1713 struct isl_set *set;
1714 struct isl_dim *dims;
1716 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1720 set = isl_set_alloc_dim(dims, n, flags);
1724 /* Make sure "map" has room for at least "n" more basic maps.
1726 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1729 struct isl_map *grown = NULL;
1733 isl_assert(map->ctx, n >= 0, goto error);
1734 if (map->n + n <= map->size)
1736 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1739 for (i = 0; i < map->n; ++i) {
1740 grown->p[i] = isl_basic_map_copy(map->p[i]);
1748 isl_map_free(grown);
1753 /* Make sure "set" has room for at least "n" more basic sets.
1755 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1757 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1760 struct isl_set *isl_set_dup(struct isl_set *set)
1763 struct isl_set *dup;
1768 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1771 for (i = 0; i < set->n; ++i)
1772 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1776 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1778 struct isl_set *set;
1783 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1785 isl_basic_set_free(bset);
1788 return isl_set_add_basic_set(set, bset);
1791 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1793 struct isl_map *map;
1798 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1800 isl_basic_map_free(bmap);
1803 return isl_map_add_basic_map(map, bmap);
1806 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1807 __isl_take isl_basic_set *bset)
1809 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1810 (struct isl_basic_map *)bset);
1813 void isl_set_free(struct isl_set *set)
1823 isl_ctx_deref(set->ctx);
1824 for (i = 0; i < set->n; ++i)
1825 isl_basic_set_free(set->p[i]);
1826 isl_dim_free(set->dim);
1830 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1835 fprintf(out, "null set\n");
1839 fprintf(out, "%*s", indent, "");
1840 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1841 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1843 for (i = 0; i < set->n; ++i) {
1844 fprintf(out, "%*s", indent, "");
1845 fprintf(out, "basic set %d:\n", i);
1846 isl_basic_set_dump(set->p[i], out, indent+4);
1850 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1855 fprintf(out, "null map\n");
1859 fprintf(out, "%*s", indent, "");
1860 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1861 "flags: %x, n_name: %d\n",
1862 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1863 map->dim->n_out, map->flags, map->dim->n_name);
1864 for (i = 0; i < map->n; ++i) {
1865 fprintf(out, "%*s", indent, "");
1866 fprintf(out, "basic map %d:\n", i);
1867 isl_basic_map_dump(map->p[i], out, indent+4);
1871 struct isl_basic_map *isl_basic_map_intersect_domain(
1872 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1874 struct isl_basic_map *bmap_domain;
1875 struct isl_dim *dim;
1880 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1881 bset->dim, isl_dim_param), goto error);
1883 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1884 isl_assert(bset->ctx,
1885 isl_basic_map_compatible_domain(bmap, bset), goto error);
1887 bmap = isl_basic_map_cow(bmap);
1890 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1891 bset->n_div, bset->n_eq, bset->n_ineq);
1892 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1893 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1894 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1896 bmap = isl_basic_map_simplify(bmap);
1897 return isl_basic_map_finalize(bmap);
1899 isl_basic_map_free(bmap);
1900 isl_basic_set_free(bset);
1904 struct isl_basic_map *isl_basic_map_intersect_range(
1905 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1907 struct isl_basic_map *bmap_range;
1912 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1913 bset->dim, isl_dim_param), goto error);
1915 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1916 isl_assert(bset->ctx,
1917 isl_basic_map_compatible_range(bmap, bset), goto error);
1919 bmap = isl_basic_map_cow(bmap);
1922 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1923 bset->n_div, bset->n_eq, bset->n_ineq);
1924 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1925 bmap = add_constraints(bmap, bmap_range, 0, 0);
1927 bmap = isl_basic_map_simplify(bmap);
1928 return isl_basic_map_finalize(bmap);
1930 isl_basic_map_free(bmap);
1931 isl_basic_set_free(bset);
1935 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1941 total = 1 + isl_basic_map_total_dim(bmap);
1942 if (total != vec->size)
1947 for (i = 0; i < bmap->n_eq; ++i) {
1948 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1949 if (!isl_int_is_zero(s)) {
1955 for (i = 0; i < bmap->n_ineq; ++i) {
1956 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1957 if (isl_int_is_neg(s)) {
1968 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1970 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1973 struct isl_basic_map *isl_basic_map_intersect(
1974 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1976 struct isl_vec *sample = NULL;
1978 if (!bmap1 || !bmap2)
1981 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1982 bmap2->dim, isl_dim_param), goto error);
1983 if (isl_dim_total(bmap1->dim) ==
1984 isl_dim_size(bmap1->dim, isl_dim_param) &&
1985 isl_dim_total(bmap2->dim) !=
1986 isl_dim_size(bmap2->dim, isl_dim_param))
1987 return isl_basic_map_intersect(bmap2, bmap1);
1989 if (isl_dim_total(bmap2->dim) !=
1990 isl_dim_size(bmap2->dim, isl_dim_param))
1991 isl_assert(bmap1->ctx,
1992 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1994 if (bmap1->sample &&
1995 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1996 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1997 sample = isl_vec_copy(bmap1->sample);
1998 else if (bmap2->sample &&
1999 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2000 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2001 sample = isl_vec_copy(bmap2->sample);
2003 bmap1 = isl_basic_map_cow(bmap1);
2006 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2007 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2008 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2011 isl_vec_free(sample);
2013 isl_vec_free(bmap1->sample);
2014 bmap1->sample = sample;
2017 bmap1 = isl_basic_map_simplify(bmap1);
2018 return isl_basic_map_finalize(bmap1);
2021 isl_vec_free(sample);
2022 isl_basic_map_free(bmap1);
2023 isl_basic_map_free(bmap2);
2027 struct isl_basic_set *isl_basic_set_intersect(
2028 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2030 return (struct isl_basic_set *)
2031 isl_basic_map_intersect(
2032 (struct isl_basic_map *)bset1,
2033 (struct isl_basic_map *)bset2);
2036 /* Special case of isl_map_intersect, where both map1 and map2
2037 * are convex, without any divs and such that either map1 or map2
2038 * contains a single constraint. This constraint is then simply
2039 * added to the other map.
2041 static __isl_give isl_map *map_intersect_add_constraint(
2042 __isl_take isl_map *map1, __isl_take isl_map *map2)
2044 struct isl_basic_map *bmap1;
2045 struct isl_basic_map *bmap2;
2047 isl_assert(map1->ctx, map1->n == 1, goto error);
2048 isl_assert(map2->ctx, map1->n == 1, goto error);
2049 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2050 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2052 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2053 return isl_map_intersect(map2, map1);
2055 isl_assert(map2->ctx,
2056 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2058 map1 = isl_map_cow(map1);
2061 if (isl_map_fast_is_empty(map1)) {
2065 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2066 if (map2->p[0]->n_eq == 1)
2067 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2069 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2070 map2->p[0]->ineq[0]);
2072 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2073 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2077 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2078 isl_basic_map_free(map1->p[0]);
2091 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2094 struct isl_map *result;
2100 if (isl_map_fast_is_empty(map1)) {
2104 if (isl_map_fast_is_empty(map2)) {
2109 if (map1->n == 1 && map2->n == 1 &&
2110 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2111 isl_dim_equal(map1->dim, map2->dim) &&
2112 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2113 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2114 return map_intersect_add_constraint(map1, map2);
2115 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2116 map2->dim, isl_dim_param), goto error);
2117 if (isl_dim_total(map1->dim) ==
2118 isl_dim_size(map1->dim, isl_dim_param) &&
2119 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2120 return isl_map_intersect(map2, map1);
2122 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2123 isl_assert(map1->ctx,
2124 isl_dim_equal(map1->dim, map2->dim), goto error);
2126 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2127 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2128 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2130 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2131 map1->n * map2->n, flags);
2134 for (i = 0; i < map1->n; ++i)
2135 for (j = 0; j < map2->n; ++j) {
2136 struct isl_basic_map *part;
2137 part = isl_basic_map_intersect(
2138 isl_basic_map_copy(map1->p[i]),
2139 isl_basic_map_copy(map2->p[j]));
2140 if (isl_basic_map_is_empty(part))
2141 isl_basic_map_free(part);
2143 result = isl_map_add_basic_map(result, part);
2156 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2158 return (struct isl_set *)
2159 isl_map_intersect((struct isl_map *)set1,
2160 (struct isl_map *)set2);
2163 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2165 struct isl_dim *dim;
2166 struct isl_basic_set *bset;
2171 bmap = isl_basic_map_cow(bmap);
2174 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2175 in = isl_basic_map_n_in(bmap);
2176 bset = isl_basic_set_from_basic_map(bmap);
2177 bset = isl_basic_set_swap_vars(bset, in);
2178 return isl_basic_map_from_basic_set(bset, dim);
2181 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2182 enum isl_dim_type type, unsigned pos, unsigned n)
2184 struct isl_dim *res_dim;
2185 struct isl_basic_map *res;
2186 struct isl_dim_map *dim_map;
2187 unsigned total, off;
2188 enum isl_dim_type t;
2196 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2198 total = isl_basic_map_total_dim(bmap) + n;
2199 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2201 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2203 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2205 unsigned size = isl_basic_map_dim(bmap, t);
2206 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2208 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2209 pos, size - pos, off + pos + n);
2211 off += isl_dim_size(res_dim, t);
2213 isl_dim_map_div(dim_map, bmap, off);
2215 res = isl_basic_map_alloc_dim(res_dim,
2216 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2217 res = add_constraints_dim_map(res, bmap, dim_map);
2218 res = isl_basic_map_simplify(res);
2219 return isl_basic_map_finalize(res);
2222 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2223 enum isl_dim_type type, unsigned n)
2227 return isl_basic_map_insert(bmap, type,
2228 isl_basic_map_dim(bmap, type), n);
2231 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2232 enum isl_dim_type type, unsigned n)
2236 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2237 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2239 isl_basic_set_free(bset);
2243 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2244 enum isl_dim_type type, unsigned pos, unsigned n)
2251 map = isl_map_cow(map);
2255 map->dim = isl_dim_insert(map->dim, type, pos, n);
2259 for (i = 0; i < map->n; ++i) {
2260 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2271 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2272 enum isl_dim_type type, unsigned pos, unsigned n)
2274 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2277 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2278 enum isl_dim_type type, unsigned n)
2282 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2285 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2286 enum isl_dim_type type, unsigned n)
2290 isl_assert(set->ctx, type != isl_dim_in, goto error);
2291 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2297 __isl_give isl_basic_map *isl_basic_map_move_dims(
2298 __isl_take isl_basic_map *bmap,
2299 enum isl_dim_type dst_type, unsigned dst_pos,
2300 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2303 struct isl_dim_map *dim_map;
2304 struct isl_basic_map *res;
2305 enum isl_dim_type t;
2306 unsigned total, off;
2313 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2316 if (dst_type == src_type && dst_pos == src_pos)
2319 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2321 if (pos(bmap->dim, dst_type) + dst_pos ==
2322 pos(bmap->dim, src_type) + src_pos +
2323 ((src_type < dst_type) ? n : 0)) {
2324 bmap = isl_basic_map_cow(bmap);
2328 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2329 src_type, src_pos, n);
2333 bmap = isl_basic_map_finalize(bmap);
2338 total = isl_basic_map_total_dim(bmap);
2339 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2342 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2343 unsigned size = isl_dim_size(bmap->dim, t);
2344 if (t == dst_type) {
2345 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2348 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2351 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2352 dst_pos, size - dst_pos, off);
2353 off += size - dst_pos;
2354 } else if (t == src_type) {
2355 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2358 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2359 src_pos + n, size - src_pos - n, off);
2360 off += size - src_pos - n;
2362 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2366 isl_dim_map_div(dim_map, bmap, off);
2368 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2369 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2370 bmap = add_constraints_dim_map(res, bmap, dim_map);
2372 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2373 src_type, src_pos, n);
2377 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2378 bmap = isl_basic_map_gauss(bmap, NULL);
2379 bmap = isl_basic_map_finalize(bmap);
2383 isl_basic_map_free(bmap);
2387 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2388 enum isl_dim_type dst_type, unsigned dst_pos,
2389 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2391 return (isl_basic_set *)isl_basic_map_move_dims(
2392 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2395 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2396 enum isl_dim_type dst_type, unsigned dst_pos,
2397 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2401 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2402 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2403 src_type, src_pos, n);
2409 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2410 enum isl_dim_type dst_type, unsigned dst_pos,
2411 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2420 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2423 if (dst_type == src_type && dst_pos == src_pos)
2426 isl_assert(map->ctx, dst_type != src_type, goto error);
2428 map = isl_map_cow(map);
2432 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2436 for (i = 0; i < map->n; ++i) {
2437 map->p[i] = isl_basic_map_move_dims(map->p[i],
2439 src_type, src_pos, n);
2450 /* Move the specified dimensions to the last columns right before
2451 * the divs. Don't change the dimension specification of bmap.
2452 * That's the responsibility of the caller.
2454 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2455 enum isl_dim_type type, unsigned first, unsigned n)
2457 struct isl_dim_map *dim_map;
2458 struct isl_basic_map *res;
2459 enum isl_dim_type t;
2460 unsigned total, off;
2464 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2467 total = isl_basic_map_total_dim(bmap);
2468 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2471 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2472 unsigned size = isl_dim_size(bmap->dim, t);
2474 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2477 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2478 first, n, total - bmap->n_div - n);
2479 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2480 first + n, size - (first + n), off);
2481 off += size - (first + n);
2483 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2487 isl_dim_map_div(dim_map, bmap, off + n);
2489 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2490 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2491 res = add_constraints_dim_map(res, bmap, dim_map);
2495 /* Turn the n dimensions of type type, starting at first
2496 * into existentially quantified variables.
2498 __isl_give isl_basic_map *isl_basic_map_project_out(
2499 __isl_take isl_basic_map *bmap,
2500 enum isl_dim_type type, unsigned first, unsigned n)
2513 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2514 return isl_basic_map_remove_dims(bmap, type, first, n);
2516 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2519 bmap = move_last(bmap, type, first, n);
2520 bmap = isl_basic_map_cow(bmap);
2524 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2525 old = bmap->block2.data;
2526 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2527 (bmap->extra + n) * (1 + row_size));
2528 if (!bmap->block2.data)
2530 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2533 for (i = 0; i < n; ++i) {
2534 new_div[i] = bmap->block2.data +
2535 (bmap->extra + i) * (1 + row_size);
2536 isl_seq_clr(new_div[i], 1 + row_size);
2538 for (i = 0; i < bmap->extra; ++i)
2539 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2541 bmap->div = new_div;
2545 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2548 bmap = isl_basic_map_simplify(bmap);
2549 bmap = isl_basic_map_drop_redundant_divs(bmap);
2550 return isl_basic_map_finalize(bmap);
2552 isl_basic_map_free(bmap);
2556 /* Turn the n dimensions of type type, starting at first
2557 * into existentially quantified variables.
2559 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2560 enum isl_dim_type type, unsigned first, unsigned n)
2562 return (isl_basic_set *)isl_basic_map_project_out(
2563 (isl_basic_map *)bset, type, first, n);
2566 /* Turn the n dimensions of type type, starting at first
2567 * into existentially quantified variables.
2569 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2570 enum isl_dim_type type, unsigned first, unsigned n)
2580 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2582 map = isl_map_cow(map);
2586 map->dim = isl_dim_drop(map->dim, type, first, n);
2590 for (i = 0; i < map->n; ++i) {
2591 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2602 /* Turn the n dimensions of type type, starting at first
2603 * into existentially quantified variables.
2605 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2606 enum isl_dim_type type, unsigned first, unsigned n)
2608 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2611 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2615 for (i = 0; i < n; ++i) {
2616 j = isl_basic_map_alloc_div(bmap);
2619 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2623 isl_basic_map_free(bmap);
2627 struct isl_basic_map *isl_basic_map_apply_range(
2628 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2630 struct isl_dim *dim_result = NULL;
2631 struct isl_basic_map *bmap;
2632 unsigned n_in, n_out, n, nparam, total, pos;
2633 struct isl_dim_map *dim_map1, *dim_map2;
2635 if (!bmap1 || !bmap2)
2638 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2639 isl_dim_copy(bmap2->dim));
2641 n_in = isl_basic_map_n_in(bmap1);
2642 n_out = isl_basic_map_n_out(bmap2);
2643 n = isl_basic_map_n_out(bmap1);
2644 nparam = isl_basic_map_n_param(bmap1);
2646 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2647 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2648 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2649 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2650 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2651 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2652 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2653 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2654 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2655 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2656 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2658 bmap = isl_basic_map_alloc_dim(dim_result,
2659 bmap1->n_div + bmap2->n_div + n,
2660 bmap1->n_eq + bmap2->n_eq,
2661 bmap1->n_ineq + bmap2->n_ineq);
2662 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2663 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2664 bmap = add_divs(bmap, n);
2665 bmap = isl_basic_map_simplify(bmap);
2666 bmap = isl_basic_map_drop_redundant_divs(bmap);
2667 return isl_basic_map_finalize(bmap);
2669 isl_basic_map_free(bmap1);
2670 isl_basic_map_free(bmap2);
2674 struct isl_basic_set *isl_basic_set_apply(
2675 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2680 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2683 return (struct isl_basic_set *)
2684 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2686 isl_basic_set_free(bset);
2687 isl_basic_map_free(bmap);
2691 struct isl_basic_map *isl_basic_map_apply_domain(
2692 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2694 if (!bmap1 || !bmap2)
2697 isl_assert(bmap1->ctx,
2698 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2699 isl_assert(bmap1->ctx,
2700 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2703 bmap1 = isl_basic_map_reverse(bmap1);
2704 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2705 return isl_basic_map_reverse(bmap1);
2707 isl_basic_map_free(bmap1);
2708 isl_basic_map_free(bmap2);
2712 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2713 * A \cap B -> f(A) + f(B)
2715 struct isl_basic_map *isl_basic_map_sum(
2716 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2718 unsigned n_in, n_out, nparam, total, pos;
2719 struct isl_basic_map *bmap = NULL;
2720 struct isl_dim_map *dim_map1, *dim_map2;
2723 if (!bmap1 || !bmap2)
2726 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2729 nparam = isl_basic_map_n_param(bmap1);
2730 n_in = isl_basic_map_n_in(bmap1);
2731 n_out = isl_basic_map_n_out(bmap1);
2733 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2734 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2735 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2736 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2737 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2738 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2739 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2740 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2741 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2742 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2743 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2745 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2746 bmap1->n_div + bmap2->n_div + 2 * n_out,
2747 bmap1->n_eq + bmap2->n_eq + n_out,
2748 bmap1->n_ineq + bmap2->n_ineq);
2749 for (i = 0; i < n_out; ++i) {
2750 int j = isl_basic_map_alloc_equality(bmap);
2753 isl_seq_clr(bmap->eq[j], 1+total);
2754 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2755 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2756 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2758 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2759 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2760 bmap = add_divs(bmap, 2 * n_out);
2762 bmap = isl_basic_map_simplify(bmap);
2763 return isl_basic_map_finalize(bmap);
2765 isl_basic_map_free(bmap);
2766 isl_basic_map_free(bmap1);
2767 isl_basic_map_free(bmap2);
2771 /* Given two maps A -> f(A) and B -> g(B), construct a map
2772 * A \cap B -> f(A) + f(B)
2774 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2776 struct isl_map *result;
2782 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2784 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2785 map1->n * map2->n, 0);
2788 for (i = 0; i < map1->n; ++i)
2789 for (j = 0; j < map2->n; ++j) {
2790 struct isl_basic_map *part;
2791 part = isl_basic_map_sum(
2792 isl_basic_map_copy(map1->p[i]),
2793 isl_basic_map_copy(map2->p[j]));
2794 if (isl_basic_map_is_empty(part))
2795 isl_basic_map_free(part);
2797 result = isl_map_add_basic_map(result, part);
2810 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2811 __isl_take isl_set *set2)
2813 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2816 /* Given a basic map A -> f(A), construct A -> -f(A).
2818 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2823 bmap = isl_basic_map_cow(bmap);
2827 n = isl_basic_map_dim(bmap, isl_dim_out);
2828 off = isl_basic_map_offset(bmap, isl_dim_out);
2829 for (i = 0; i < bmap->n_eq; ++i)
2830 for (j = 0; j < n; ++j)
2831 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2832 for (i = 0; i < bmap->n_ineq; ++i)
2833 for (j = 0; j < n; ++j)
2834 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2835 for (i = 0; i < bmap->n_div; ++i)
2836 for (j = 0; j < n; ++j)
2837 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2838 return isl_basic_map_finalize(bmap);
2841 /* Given a map A -> f(A), construct A -> -f(A).
2843 struct isl_map *isl_map_neg(struct isl_map *map)
2847 map = isl_map_cow(map);
2851 for (i = 0; i < map->n; ++i) {
2852 map->p[i] = isl_basic_map_neg(map->p[i]);
2863 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
2865 return (isl_set *)isl_map_neg((isl_map *)set);
2868 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2869 * A -> floor(f(A)/d).
2871 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2874 unsigned n_in, n_out, nparam, total, pos;
2875 struct isl_basic_map *result = NULL;
2876 struct isl_dim_map *dim_map;
2882 nparam = isl_basic_map_n_param(bmap);
2883 n_in = isl_basic_map_n_in(bmap);
2884 n_out = isl_basic_map_n_out(bmap);
2886 total = nparam + n_in + n_out + bmap->n_div + n_out;
2887 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2888 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2889 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2890 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2891 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2893 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2894 bmap->n_div + n_out,
2895 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2896 result = add_constraints_dim_map(result, bmap, dim_map);
2897 result = add_divs(result, n_out);
2898 for (i = 0; i < n_out; ++i) {
2900 j = isl_basic_map_alloc_inequality(result);
2903 isl_seq_clr(result->ineq[j], 1+total);
2904 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2905 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2906 j = isl_basic_map_alloc_inequality(result);
2909 isl_seq_clr(result->ineq[j], 1+total);
2910 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2911 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2912 isl_int_sub_ui(result->ineq[j][0], d, 1);
2915 result = isl_basic_map_simplify(result);
2916 return isl_basic_map_finalize(result);
2918 isl_basic_map_free(result);
2922 /* Given a map A -> f(A) and an integer d, construct a map
2923 * A -> floor(f(A)/d).
2925 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2929 map = isl_map_cow(map);
2933 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2934 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2935 for (i = 0; i < map->n; ++i) {
2936 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2947 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2953 i = isl_basic_map_alloc_equality(bmap);
2956 nparam = isl_basic_map_n_param(bmap);
2957 n_in = isl_basic_map_n_in(bmap);
2958 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2959 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2960 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2961 return isl_basic_map_finalize(bmap);
2963 isl_basic_map_free(bmap);
2967 /* Add a constraints to "bmap" expressing i_pos < o_pos
2969 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2975 i = isl_basic_map_alloc_inequality(bmap);
2978 nparam = isl_basic_map_n_param(bmap);
2979 n_in = isl_basic_map_n_in(bmap);
2980 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2981 isl_int_set_si(bmap->ineq[i][0], -1);
2982 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2983 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2984 return isl_basic_map_finalize(bmap);
2986 isl_basic_map_free(bmap);
2990 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2992 static __isl_give isl_basic_map *var_less_or_equal(
2993 __isl_take isl_basic_map *bmap, unsigned pos)
2999 i = isl_basic_map_alloc_inequality(bmap);
3002 nparam = isl_basic_map_n_param(bmap);
3003 n_in = isl_basic_map_n_in(bmap);
3004 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3005 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3006 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3007 return isl_basic_map_finalize(bmap);
3009 isl_basic_map_free(bmap);
3013 /* Add a constraints to "bmap" expressing i_pos > o_pos
3015 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3021 i = isl_basic_map_alloc_inequality(bmap);
3024 nparam = isl_basic_map_n_param(bmap);
3025 n_in = isl_basic_map_n_in(bmap);
3026 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3027 isl_int_set_si(bmap->ineq[i][0], -1);
3028 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3029 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3030 return isl_basic_map_finalize(bmap);
3032 isl_basic_map_free(bmap);
3036 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3038 static __isl_give isl_basic_map *var_more_or_equal(
3039 __isl_take isl_basic_map *bmap, unsigned pos)
3045 i = isl_basic_map_alloc_inequality(bmap);
3048 nparam = isl_basic_map_n_param(bmap);
3049 n_in = isl_basic_map_n_in(bmap);
3050 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3051 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3052 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3053 return isl_basic_map_finalize(bmap);
3055 isl_basic_map_free(bmap);
3059 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3062 struct isl_basic_map *bmap;
3063 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3066 for (i = 0; i < n_equal && bmap; ++i)
3067 bmap = var_equal(bmap, i);
3068 return isl_basic_map_finalize(bmap);
3071 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3073 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3076 struct isl_basic_map *bmap;
3077 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3080 for (i = 0; i < pos && bmap; ++i)
3081 bmap = var_equal(bmap, i);
3083 bmap = var_less(bmap, pos);
3084 return isl_basic_map_finalize(bmap);
3087 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3089 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3090 __isl_take isl_dim *dim, unsigned pos)
3093 isl_basic_map *bmap;
3095 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3096 for (i = 0; i < pos; ++i)
3097 bmap = var_equal(bmap, i);
3098 bmap = var_less_or_equal(bmap, pos);
3099 return isl_basic_map_finalize(bmap);
3102 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3104 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3107 struct isl_basic_map *bmap;
3108 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3111 for (i = 0; i < pos && bmap; ++i)
3112 bmap = var_equal(bmap, i);
3114 bmap = var_more(bmap, pos);
3115 return isl_basic_map_finalize(bmap);
3118 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3120 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3121 __isl_take isl_dim *dim, unsigned pos)
3124 isl_basic_map *bmap;
3126 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3127 for (i = 0; i < pos; ++i)
3128 bmap = var_equal(bmap, i);
3129 bmap = var_more_or_equal(bmap, pos);
3130 return isl_basic_map_finalize(bmap);
3133 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3134 unsigned n, int equal)
3136 struct isl_map *map;
3139 if (n == 0 && equal)
3140 return isl_map_universe(dims);
3142 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3144 for (i = 0; i + 1 < n; ++i)
3145 map = isl_map_add_basic_map(map,
3146 isl_basic_map_less_at(isl_dim_copy(dims), i));
3149 map = isl_map_add_basic_map(map,
3150 isl_basic_map_less_or_equal_at(dims, n - 1));
3152 map = isl_map_add_basic_map(map,
3153 isl_basic_map_less_at(dims, n - 1));
3160 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3164 return map_lex_lte_first(dims, dims->n_out, equal);
3167 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3169 return map_lex_lte_first(dim, n, 0);
3172 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3174 return map_lex_lte_first(dim, n, 1);
3177 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3179 return map_lex_lte(isl_dim_map(set_dim), 0);
3182 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3184 return map_lex_lte(isl_dim_map(set_dim), 1);
3187 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3188 unsigned n, int equal)
3190 struct isl_map *map;
3193 if (n == 0 && equal)
3194 return isl_map_universe(dims);
3196 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3198 for (i = 0; i + 1 < n; ++i)
3199 map = isl_map_add_basic_map(map,
3200 isl_basic_map_more_at(isl_dim_copy(dims), i));
3203 map = isl_map_add_basic_map(map,
3204 isl_basic_map_more_or_equal_at(dims, n - 1));
3206 map = isl_map_add_basic_map(map,
3207 isl_basic_map_more_at(dims, n - 1));
3214 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3218 return map_lex_gte_first(dims, dims->n_out, equal);
3221 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3223 return map_lex_gte_first(dim, n, 0);
3226 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3228 return map_lex_gte_first(dim, n, 1);
3231 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3233 return map_lex_gte(isl_dim_map(set_dim), 0);
3236 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3238 return map_lex_gte(isl_dim_map(set_dim), 1);
3241 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3242 __isl_take isl_set *set2)
3245 map = isl_map_lex_le(isl_set_get_dim(set1));
3246 map = isl_map_intersect_domain(map, set1);
3247 map = isl_map_intersect_range(map, set2);
3251 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3252 __isl_take isl_set *set2)
3255 map = isl_map_lex_lt(isl_set_get_dim(set1));
3256 map = isl_map_intersect_domain(map, set1);
3257 map = isl_map_intersect_range(map, set2);
3261 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3262 __isl_take isl_set *set2)
3265 map = isl_map_lex_ge(isl_set_get_dim(set1));
3266 map = isl_map_intersect_domain(map, set1);
3267 map = isl_map_intersect_range(map, set2);
3271 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3272 __isl_take isl_set *set2)
3275 map = isl_map_lex_gt(isl_set_get_dim(set1));
3276 map = isl_map_intersect_domain(map, set1);
3277 map = isl_map_intersect_range(map, set2);
3281 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3282 __isl_take isl_map *map2)
3285 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3286 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3287 map = isl_map_apply_range(map, isl_map_reverse(map2));
3291 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3292 __isl_take isl_map *map2)
3295 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3296 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3297 map = isl_map_apply_range(map, isl_map_reverse(map2));
3301 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3302 __isl_take isl_map *map2)
3305 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3306 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3307 map = isl_map_apply_range(map, isl_map_reverse(map2));
3311 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3312 __isl_take isl_map *map2)
3315 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3316 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3317 map = isl_map_apply_range(map, isl_map_reverse(map2));
3321 struct isl_basic_map *isl_basic_map_from_basic_set(
3322 struct isl_basic_set *bset, struct isl_dim *dim)
3324 struct isl_basic_map *bmap;
3326 bset = isl_basic_set_cow(bset);
3330 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3331 isl_dim_free(bset->dim);
3332 bmap = (struct isl_basic_map *) bset;
3334 return isl_basic_map_finalize(bmap);
3336 isl_basic_set_free(bset);
3341 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3345 if (bmap->dim->n_in == 0)
3346 return (struct isl_basic_set *)bmap;
3347 bmap = isl_basic_map_cow(bmap);
3350 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3353 bmap = isl_basic_map_finalize(bmap);
3354 return (struct isl_basic_set *)bmap;
3356 isl_basic_map_free(bmap);
3360 /* For a div d = floor(f/m), add the constraints
3363 * -(f-(n-1)) + m d >= 0
3365 * Note that the second constraint is the negation of
3369 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3370 unsigned pos, isl_int *div)
3373 unsigned total = isl_basic_map_total_dim(bmap);
3375 i = isl_basic_map_alloc_inequality(bmap);
3378 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3379 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3381 j = isl_basic_map_alloc_inequality(bmap);
3384 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3385 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3386 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3390 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3391 unsigned pos, isl_int *div)
3393 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3397 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3399 unsigned total = isl_basic_map_total_dim(bmap);
3400 unsigned div_pos = total - bmap->n_div + div;
3402 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3406 struct isl_basic_set *isl_basic_map_underlying_set(
3407 struct isl_basic_map *bmap)
3411 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3412 bmap->n_div == 0 && !isl_dim_get_tuple_name(bmap->dim, isl_dim_out))
3413 return (struct isl_basic_set *)bmap;
3414 bmap = isl_basic_map_cow(bmap);
3417 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3420 bmap->extra -= bmap->n_div;
3422 bmap = isl_basic_map_finalize(bmap);
3423 return (struct isl_basic_set *)bmap;
3428 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3429 __isl_take isl_basic_set *bset)
3431 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3434 struct isl_basic_map *isl_basic_map_overlying_set(
3435 struct isl_basic_set *bset, struct isl_basic_map *like)
3437 struct isl_basic_map *bmap;
3438 struct isl_ctx *ctx;
3445 isl_assert(ctx, bset->n_div == 0, goto error);
3446 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3447 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3449 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3450 isl_basic_map_free(like);
3451 return (struct isl_basic_map *)bset;
3453 bset = isl_basic_set_cow(bset);
3456 total = bset->dim->n_out + bset->extra;
3457 bmap = (struct isl_basic_map *)bset;
3458 isl_dim_free(bmap->dim);
3459 bmap->dim = isl_dim_copy(like->dim);
3462 bmap->n_div = like->n_div;
3463 bmap->extra += like->n_div;
3466 ltotal = total - bmap->extra + like->extra;
3469 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3470 bmap->extra * (1 + 1 + total));
3471 if (isl_blk_is_error(bmap->block2))
3473 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3477 for (i = 0; i < bmap->extra; ++i)
3478 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3479 for (i = 0; i < like->n_div; ++i) {
3480 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3481 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3483 bmap = isl_basic_map_extend_constraints(bmap,
3484 0, 2 * like->n_div);
3485 for (i = 0; i < like->n_div; ++i) {
3486 if (isl_int_is_zero(bmap->div[i][0]))
3488 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3492 isl_basic_map_free(like);
3493 bmap = isl_basic_map_simplify(bmap);
3494 bmap = isl_basic_map_finalize(bmap);
3497 isl_basic_map_free(like);
3498 isl_basic_set_free(bset);
3502 struct isl_basic_set *isl_basic_set_from_underlying_set(
3503 struct isl_basic_set *bset, struct isl_basic_set *like)
3505 return (struct isl_basic_set *)
3506 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3509 struct isl_set *isl_set_from_underlying_set(
3510 struct isl_set *set, struct isl_basic_set *like)
3516 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3518 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3519 isl_basic_set_free(like);
3522 set = isl_set_cow(set);
3525 for (i = 0; i < set->n; ++i) {
3526 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3527 isl_basic_set_copy(like));
3531 isl_dim_free(set->dim);
3532 set->dim = isl_dim_copy(like->dim);
3535 isl_basic_set_free(like);
3538 isl_basic_set_free(like);
3543 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3547 map = isl_map_cow(map);
3550 map->dim = isl_dim_cow(map->dim);
3554 for (i = 1; i < map->n; ++i)
3555 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3557 for (i = 0; i < map->n; ++i) {
3558 map->p[i] = (struct isl_basic_map *)
3559 isl_basic_map_underlying_set(map->p[i]);
3564 map->dim = isl_dim_underlying(map->dim, 0);
3566 isl_dim_free(map->dim);
3567 map->dim = isl_dim_copy(map->p[0]->dim);
3571 return (struct isl_set *)map;
3577 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3579 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3582 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3583 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3585 bmap = isl_basic_map_cow(bmap);
3589 isl_dim_free(bmap->dim);
3592 bmap = isl_basic_map_finalize(bmap);
3596 isl_basic_map_free(bmap);
3601 static __isl_give isl_basic_set *isl_basic_set_reset_dim(
3602 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3604 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3608 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3609 __isl_take isl_dim *dim)
3613 map = isl_map_cow(map);
3617 for (i = 0; i < map->n; ++i) {
3618 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3623 isl_dim_free(map->dim);
3633 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3634 __isl_take isl_dim *dim)
3636 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3639 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3642 struct isl_basic_set *domain;
3648 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3650 n_in = isl_basic_map_n_in(bmap);
3651 n_out = isl_basic_map_n_out(bmap);
3652 domain = isl_basic_set_from_basic_map(bmap);
3653 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3655 domain = isl_basic_set_reset_dim(domain, dim);
3660 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3662 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3665 __isl_give isl_basic_map *isl_basic_map_domain_map(
3666 __isl_take isl_basic_map *bmap)
3670 isl_basic_map *domain;
3671 isl_basic_set *bset;
3672 int nparam, n_in, n_out;
3675 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3676 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3677 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3679 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3680 domain = isl_basic_map_universe(dim);
3682 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3683 bmap = isl_basic_map_apply_range(bmap, domain);
3684 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3686 total = isl_basic_map_total_dim(bmap);
3688 for (i = 0; i < n_in; ++i) {
3689 k = isl_basic_map_alloc_equality(bmap);
3692 isl_seq_clr(bmap->eq[k], 1 + total);
3693 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3694 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3697 bmap = isl_basic_map_gauss(bmap, NULL);
3698 return isl_basic_map_finalize(bmap);
3700 isl_basic_map_free(bmap);
3704 __isl_give isl_basic_map *isl_basic_map_range_map(
3705 __isl_take isl_basic_map *bmap)
3709 isl_basic_map *range;
3710 isl_basic_set *bset;
3711 int nparam, n_in, n_out;
3714 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3715 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3716 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3718 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3719 range = isl_basic_map_universe(dim);
3721 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3722 bmap = isl_basic_map_apply_range(bmap, range);
3723 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3725 total = isl_basic_map_total_dim(bmap);
3727 for (i = 0; i < n_out; ++i) {
3728 k = isl_basic_map_alloc_equality(bmap);
3731 isl_seq_clr(bmap->eq[k], 1 + total);
3732 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3733 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3736 bmap = isl_basic_map_gauss(bmap, NULL);
3737 return isl_basic_map_finalize(bmap);
3739 isl_basic_map_free(bmap);
3743 struct isl_set *isl_map_range(struct isl_map *map)
3746 struct isl_set *set;
3750 if (isl_map_dim(map, isl_dim_in) == 0)
3751 return (isl_set *)map;
3753 map = isl_map_cow(map);
3757 set = (struct isl_set *) map;
3758 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3761 for (i = 0; i < map->n; ++i) {
3762 set->p[i] = isl_basic_map_range(map->p[i]);
3766 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3767 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3774 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3777 isl_dim *domain_dim;
3779 map = isl_map_cow(map);
3783 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3784 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3785 map->dim = isl_dim_join(map->dim, domain_dim);
3788 for (i = 0; i < map->n; ++i) {
3789 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3793 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3794 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3801 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3806 map = isl_map_cow(map);
3810 range_dim = isl_dim_range(isl_map_get_dim(map));
3811 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3812 map->dim = isl_dim_join(map->dim, range_dim);
3815 for (i = 0; i < map->n; ++i) {
3816 map->p[i] = isl_basic_map_range_map(map->p[i]);
3820 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3821 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3828 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3831 struct isl_map *map = NULL;
3833 set = isl_set_cow(set);
3836 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3837 map = (struct isl_map *)set;
3838 for (i = 0; i < set->n; ++i) {
3839 map->p[i] = isl_basic_map_from_basic_set(
3840 set->p[i], isl_dim_copy(dim));
3844 isl_dim_free(map->dim);
3853 __isl_give isl_basic_map *isl_basic_map_from_domain(
3854 __isl_take isl_basic_set *bset)
3856 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
3859 __isl_give isl_basic_map *isl_basic_map_from_range(
3860 __isl_take isl_basic_set *bset)
3862 return (isl_basic_map *)bset;
3865 struct isl_map *isl_map_from_range(struct isl_set *set)
3867 return (struct isl_map *)set;
3870 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3872 return isl_map_reverse(isl_map_from_range(set));
3875 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3876 __isl_take isl_set *range)
3878 return isl_map_apply_range(isl_map_from_domain(domain),
3879 isl_map_from_range(range));
3882 struct isl_set *isl_set_from_map(struct isl_map *map)
3885 struct isl_set *set = NULL;
3889 map = isl_map_cow(map);
3892 map->dim = isl_dim_as_set_dim(map->dim);
3895 set = (struct isl_set *)map;
3896 for (i = 0; i < map->n; ++i) {
3897 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3907 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3909 struct isl_map *map;
3913 isl_assert(dim->ctx, n >= 0, return NULL);
3914 map = isl_alloc(dim->ctx, struct isl_map,
3915 sizeof(struct isl_map) +
3916 (n - 1) * sizeof(struct isl_basic_map *));
3920 map->ctx = dim->ctx;
3921 isl_ctx_ref(map->ctx);
3933 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3934 unsigned nparam, unsigned in, unsigned out, int n,
3937 struct isl_map *map;
3938 struct isl_dim *dims;
3940 dims = isl_dim_alloc(ctx, nparam, in, out);
3944 map = isl_map_alloc_dim(dims, n, flags);
3948 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3950 struct isl_basic_map *bmap;
3951 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3952 bmap = isl_basic_map_set_to_empty(bmap);
3956 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3958 struct isl_basic_set *bset;
3959 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3960 bset = isl_basic_set_set_to_empty(bset);
3964 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3966 struct isl_basic_map *bmap;
3969 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3970 bmap = isl_basic_map_set_to_empty(bmap);
3974 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3976 struct isl_basic_map *bmap;
3979 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3980 bmap = isl_basic_map_set_to_empty(bmap);
3984 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3986 struct isl_basic_set *bset;
3989 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3990 bset = isl_basic_set_set_to_empty(bset);
3994 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3996 struct isl_basic_map *bmap;
3997 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4001 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4003 struct isl_basic_set *bset;
4004 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4008 __isl_give isl_basic_map *isl_basic_map_universe_like(
4009 __isl_keep isl_basic_map *model)
4013 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4016 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4020 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4023 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4024 __isl_keep isl_set *model)
4028 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4031 struct isl_map *isl_map_empty(struct isl_dim *dim)
4033 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4036 struct isl_map *isl_map_empty_like(struct isl_map *model)
4040 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4043 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4047 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4050 struct isl_set *isl_set_empty(struct isl_dim *dim)
4052 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4055 struct isl_set *isl_set_empty_like(struct isl_set *model)
4059 return isl_set_empty(isl_dim_copy(model->dim));
4062 struct isl_map *isl_map_universe(struct isl_dim *dim)
4064 struct isl_map *map;
4067 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4068 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4072 struct isl_set *isl_set_universe(struct isl_dim *dim)
4074 struct isl_set *set;
4077 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4078 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4082 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4086 return isl_set_universe(isl_dim_copy(model->dim));
4089 struct isl_map *isl_map_dup(struct isl_map *map)
4092 struct isl_map *dup;
4096 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4097 for (i = 0; i < map->n; ++i)
4098 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4102 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4103 __isl_take isl_basic_map *bmap)
4107 if (isl_basic_map_fast_is_empty(bmap)) {
4108 isl_basic_map_free(bmap);
4111 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4112 isl_assert(map->ctx, map->n < map->size, goto error);
4113 map->p[map->n] = bmap;
4115 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4121 isl_basic_map_free(bmap);
4125 void isl_map_free(struct isl_map *map)
4135 isl_ctx_deref(map->ctx);
4136 for (i = 0; i < map->n; ++i)
4137 isl_basic_map_free(map->p[i]);
4138 isl_dim_free(map->dim);
4142 struct isl_map *isl_map_extend(struct isl_map *base,
4143 unsigned nparam, unsigned n_in, unsigned n_out)
4147 base = isl_map_cow(base);
4151 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4154 for (i = 0; i < base->n; ++i) {
4155 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4156 isl_dim_copy(base->dim), 0, 0, 0);
4166 struct isl_set *isl_set_extend(struct isl_set *base,
4167 unsigned nparam, unsigned dim)
4169 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4173 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4174 struct isl_basic_map *bmap, unsigned pos, int value)
4178 bmap = isl_basic_map_cow(bmap);
4179 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4180 j = isl_basic_map_alloc_equality(bmap);
4183 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4184 isl_int_set_si(bmap->eq[j][pos], -1);
4185 isl_int_set_si(bmap->eq[j][0], value);
4186 bmap = isl_basic_map_simplify(bmap);
4187 return isl_basic_map_finalize(bmap);
4189 isl_basic_map_free(bmap);
4193 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4194 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4198 bmap = isl_basic_map_cow(bmap);
4199 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4200 j = isl_basic_map_alloc_equality(bmap);
4203 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4204 isl_int_set_si(bmap->eq[j][pos], -1);
4205 isl_int_set(bmap->eq[j][0], value);
4206 bmap = isl_basic_map_simplify(bmap);
4207 return isl_basic_map_finalize(bmap);
4209 isl_basic_map_free(bmap);
4213 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4214 enum isl_dim_type type, unsigned pos, int value)
4218 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4219 return isl_basic_map_fix_pos_si(bmap,
4220 isl_basic_map_offset(bmap, type) + pos, value);
4222 isl_basic_map_free(bmap);
4226 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4227 enum isl_dim_type type, unsigned pos, isl_int value)
4231 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4232 return isl_basic_map_fix_pos(bmap,
4233 isl_basic_map_offset(bmap, type) + pos, value);
4235 isl_basic_map_free(bmap);
4239 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4240 enum isl_dim_type type, unsigned pos, int value)
4242 return (struct isl_basic_set *)
4243 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4247 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4248 enum isl_dim_type type, unsigned pos, isl_int value)
4250 return (struct isl_basic_set *)
4251 isl_basic_map_fix((struct isl_basic_map *)bset,
4255 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4256 unsigned input, int value)
4258 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4261 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4262 unsigned dim, int value)
4264 return (struct isl_basic_set *)
4265 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4266 isl_dim_set, dim, value);
4269 struct isl_map *isl_map_fix_si(struct isl_map *map,
4270 enum isl_dim_type type, unsigned pos, int value)
4274 map = isl_map_cow(map);
4278 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4279 for (i = 0; i < map->n; ++i) {
4280 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4284 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4291 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4292 enum isl_dim_type type, unsigned pos, int value)
4294 return (struct isl_set *)
4295 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4298 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4299 enum isl_dim_type type, unsigned pos, isl_int value)
4303 map = isl_map_cow(map);
4307 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4308 for (i = 0; i < map->n; ++i) {
4309 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4313 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4320 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4321 enum isl_dim_type type, unsigned pos, isl_int value)
4323 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4326 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4327 unsigned input, int value)
4329 return isl_map_fix_si(map, isl_dim_in, input, value);
4332 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4334 return (struct isl_set *)
4335 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4338 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4339 __isl_take isl_basic_map *bmap,
4340 enum isl_dim_type type, unsigned pos, int value)
4346 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4347 pos += isl_basic_map_offset(bmap, type);
4348 bmap = isl_basic_map_cow(bmap);
4349 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4350 j = isl_basic_map_alloc_inequality(bmap);
4353 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4354 isl_int_set_si(bmap->ineq[j][pos], 1);
4355 isl_int_set_si(bmap->ineq[j][0], -value);
4356 bmap = isl_basic_map_simplify(bmap);
4357 return isl_basic_map_finalize(bmap);
4359 isl_basic_map_free(bmap);
4363 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4364 unsigned dim, isl_int value)
4368 bset = isl_basic_set_cow(bset);
4369 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4370 j = isl_basic_set_alloc_inequality(bset);
4373 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4374 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4375 isl_int_neg(bset->ineq[j][0], value);
4376 bset = isl_basic_set_simplify(bset);
4377 return isl_basic_set_finalize(bset);
4379 isl_basic_set_free(bset);
4383 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4384 enum isl_dim_type type, unsigned pos, int value)
4388 map = isl_map_cow(map);
4392 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4393 for (i = 0; i < map->n; ++i) {
4394 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4399 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4406 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4407 enum isl_dim_type type, unsigned pos, int value)
4409 return (struct isl_set *)
4410 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4413 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4418 set = isl_set_cow(set);
4422 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4423 for (i = 0; i < set->n; ++i) {
4424 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4434 struct isl_map *isl_map_reverse(struct isl_map *map)
4438 map = isl_map_cow(map);
4442 map->dim = isl_dim_reverse(map->dim);
4445 for (i = 0; i < map->n; ++i) {
4446 map->p[i] = isl_basic_map_reverse(map->p[i]);
4450 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4457 static struct isl_map *isl_basic_map_partial_lexopt(
4458 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4459 struct isl_set **empty, int max)
4463 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4464 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4466 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4468 isl_basic_map_free(bmap);
4469 isl_basic_set_free(dom);
4475 struct isl_map *isl_basic_map_partial_lexmax(
4476 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4477 struct isl_set **empty)
4479 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4482 struct isl_map *isl_basic_map_partial_lexmin(
4483 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4484 struct isl_set **empty)
4486 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4489 struct isl_set *isl_basic_set_partial_lexmin(
4490 struct isl_basic_set *bset, struct isl_basic_set *dom,
4491 struct isl_set **empty)
4493 return (struct isl_set *)
4494 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4498 struct isl_set *isl_basic_set_partial_lexmax(
4499 struct isl_basic_set *bset, struct isl_basic_set *dom,
4500 struct isl_set **empty)
4502 return (struct isl_set *)
4503 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4507 /* Given a basic map "bmap", compute the lexicograhically minimal
4508 * (or maximal) image element for each domain element in dom.
4509 * Set *empty to those elements in dom that do not have an image element.
4511 * We first make sure the basic sets in dom are disjoint and then
4512 * simply collect the results over each of the basic sets separately.
4513 * We could probably improve the efficiency a bit by moving the union
4514 * domain down into the parametric integer programming.
4516 static __isl_give isl_map *basic_map_partial_lexopt(
4517 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4518 __isl_give isl_set **empty, int max)
4521 struct isl_map *res;
4523 dom = isl_set_make_disjoint(dom);
4527 if (isl_set_fast_is_empty(dom)) {
4528 res = isl_map_empty_like_basic_map(bmap);
4529 *empty = isl_set_empty_like(dom);
4531 isl_basic_map_free(bmap);
4535 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4536 isl_basic_set_copy(dom->p[0]), empty, max);
4538 for (i = 1; i < dom->n; ++i) {
4539 struct isl_map *res_i;
4540 struct isl_set *empty_i;
4542 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4543 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4545 res = isl_map_union_disjoint(res, res_i);
4546 *empty = isl_set_union_disjoint(*empty, empty_i);
4550 isl_basic_map_free(bmap);
4555 isl_basic_map_free(bmap);
4559 /* Given a map "map", compute the lexicograhically minimal
4560 * (or maximal) image element for each domain element in dom.
4561 * Set *empty to those elements in dom that do not have an image element.
4563 * We first compute the lexicographically minimal or maximal element
4564 * in the first basic map. This results in a partial solution "res"
4565 * and a subset "todo" of dom that still need to be handled.
4566 * We then consider each of the remaining maps in "map" and successively
4567 * improve both "res" and "todo".
4569 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4570 * Assume we are computing the lexicographical maximum.
4571 * We first intersect basic map i with a relation that maps elements
4572 * to elements that are lexicographically larger than the image elements
4573 * in res^k and the compute the maximum image element of this intersection.
4574 * The result ("better") corresponds to those image elements in basic map i
4575 * that are better than what we had before. The remainder ("keep") are the
4576 * domain elements for which the image element in res_k was better.
4577 * We also compute the lexicographical maximum of basic map i in todo^k.
4578 * res^i is the result of the operation + better + those elements in
4579 * res^k that we should keep
4580 * todo^i is the remainder of the maximum operation on todo^k.
4582 static __isl_give isl_map *isl_map_partial_lexopt(
4583 __isl_take isl_map *map, __isl_take isl_set *dom,
4584 __isl_give isl_set **empty, int max)
4587 struct isl_map *res;
4588 struct isl_set *todo;
4593 if (isl_map_fast_is_empty(map)) {
4601 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4602 isl_set_copy(dom), &todo, max);
4604 for (i = 1; i < map->n; ++i) {
4606 struct isl_map *better;
4607 struct isl_set *keep;
4608 struct isl_map *res_i;
4609 struct isl_set *todo_i;
4610 struct isl_dim *dim = isl_map_get_dim(res);
4612 dim = isl_dim_range(dim);
4614 lt = isl_map_lex_lt(dim);
4616 lt = isl_map_lex_gt(dim);
4617 lt = isl_map_apply_range(isl_map_copy(res), lt);
4618 lt = isl_map_intersect(lt,
4619 isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
4620 better = isl_map_partial_lexopt(lt,
4621 isl_map_domain(isl_map_copy(res)),
4624 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4625 todo, &todo_i, max);
4627 res = isl_map_intersect_domain(res, keep);
4628 res = isl_map_union_disjoint(res, res_i);
4629 res = isl_map_union_disjoint(res, better);
4650 __isl_give isl_map *isl_map_partial_lexmax(
4651 __isl_take isl_map *map, __isl_take isl_set *dom,
4652 __isl_give isl_set **empty)
4654 return isl_map_partial_lexopt(map, dom, empty, 1);
4657 __isl_give isl_map *isl_map_partial_lexmin(
4658 __isl_take isl_map *map, __isl_take isl_set *dom,
4659 __isl_give isl_set **empty)
4661 return isl_map_partial_lexopt(map, dom, empty, 0);
4664 __isl_give isl_set *isl_set_partial_lexmin(
4665 __isl_take isl_set *set, __isl_take isl_set *dom,
4666 __isl_give isl_set **empty)
4668 return (struct isl_set *)
4669 isl_map_partial_lexmin((struct isl_map *)set,
4673 __isl_give isl_set *isl_set_partial_lexmax(
4674 __isl_take isl_set *set, __isl_take isl_set *dom,
4675 __isl_give isl_set **empty)
4677 return (struct isl_set *)
4678 isl_map_partial_lexmax((struct isl_map *)set,
4682 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4684 struct isl_basic_set *dom = NULL;
4685 struct isl_dim *dom_dim;
4689 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4690 dom = isl_basic_set_universe(dom_dim);
4691 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4693 isl_basic_map_free(bmap);
4697 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4699 return isl_basic_map_lexopt(bmap, 0);
4702 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4704 return isl_basic_map_lexopt(bmap, 1);
4707 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4709 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4712 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4714 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4717 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4719 struct isl_set *dom = NULL;
4720 struct isl_dim *dom_dim;
4724 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4725 dom = isl_set_universe(dom_dim);
4726 return isl_map_partial_lexopt(map, dom, NULL, max);
4732 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4734 return isl_map_lexopt(map, 0);
4737 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4739 return isl_map_lexopt(map, 1);
4742 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4744 return (isl_set *)isl_map_lexmin((isl_map *)set);
4747 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4749 return (isl_set *)isl_map_lexmax((isl_map *)set);
4752 /* Apply a preimage specified by "mat" on the parameters of "bset".
4753 * bset is assumed to have only parameters and divs.
4755 static struct isl_basic_set *basic_set_parameter_preimage(
4756 struct isl_basic_set *bset, struct isl_mat *mat)
4763 bset->dim = isl_dim_cow(bset->dim);
4767 nparam = isl_basic_set_dim(bset, isl_dim_param);
4769 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4771 bset->dim->nparam = 0;
4772 bset->dim->n_out = nparam;
4773 bset = isl_basic_set_preimage(bset, mat);
4775 bset->dim->nparam = bset->dim->n_out;
4776 bset->dim->n_out = 0;
4781 isl_basic_set_free(bset);
4785 /* Apply a preimage specified by "mat" on the parameters of "set".
4786 * set is assumed to have only parameters and divs.
4788 static struct isl_set *set_parameter_preimage(
4789 struct isl_set *set, struct isl_mat *mat)
4791 struct isl_dim *dim = NULL;
4797 dim = isl_dim_copy(set->dim);
4798 dim = isl_dim_cow(dim);
4802 nparam = isl_set_dim(set, isl_dim_param);
4804 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4807 dim->n_out = nparam;
4808 isl_set_reset_dim(set, dim);
4809 set = isl_set_preimage(set, mat);
4812 dim = isl_dim_copy(set->dim);
4813 dim = isl_dim_cow(dim);
4816 dim->nparam = dim->n_out;
4818 isl_set_reset_dim(set, dim);
4828 /* Intersect the basic set "bset" with the affine space specified by the
4829 * equalities in "eq".
4831 static struct isl_basic_set *basic_set_append_equalities(
4832 struct isl_basic_set *bset, struct isl_mat *eq)
4840 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4845 len = 1 + isl_dim_total(bset->dim) + bset->extra;
4846 for (i = 0; i < eq->n_row; ++i) {
4847 k = isl_basic_set_alloc_equality(bset);
4850 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4851 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4855 bset = isl_basic_set_gauss(bset, NULL);
4856 bset = isl_basic_set_finalize(bset);
4861 isl_basic_set_free(bset);
4865 /* Intersect the set "set" with the affine space specified by the
4866 * equalities in "eq".
4868 static struct isl_set *set_append_equalities(struct isl_set *set,
4876 for (i = 0; i < set->n; ++i) {
4877 set->p[i] = basic_set_append_equalities(set->p[i],
4890 /* Project the given basic set onto its parameter domain, possibly introducing
4891 * new, explicit, existential variables in the constraints.
4892 * The input has parameters and (possibly implicit) existential variables.
4893 * The output has the same parameters, but only
4894 * explicit existentially quantified variables.
4896 * The actual projection is performed by pip, but pip doesn't seem
4897 * to like equalities very much, so we first remove the equalities
4898 * among the parameters by performing a variable compression on
4899 * the parameters. Afterward, an inverse transformation is performed
4900 * and the equalities among the parameters are inserted back in.
4902 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4906 struct isl_mat *T, *T2;
4907 struct isl_set *set;
4908 unsigned nparam, n_div;
4910 bset = isl_basic_set_cow(bset);
4914 if (bset->n_eq == 0)
4915 return isl_basic_set_lexmin(bset);
4917 isl_basic_set_gauss(bset, NULL);
4919 nparam = isl_basic_set_dim(bset, isl_dim_param);
4920 n_div = isl_basic_set_dim(bset, isl_dim_div);
4922 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4923 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4926 if (i == bset->n_eq)
4927 return isl_basic_set_lexmin(bset);
4929 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4931 eq = isl_mat_cow(eq);
4932 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4933 if (T && T->n_col == 0) {
4937 bset = isl_basic_set_set_to_empty(bset);
4938 return isl_set_from_basic_set(bset);
4940 bset = basic_set_parameter_preimage(bset, T);
4942 set = isl_basic_set_lexmin(bset);
4943 set = set_parameter_preimage(set, T2);
4944 set = set_append_equalities(set, eq);
4948 /* Compute an explicit representation for all the existentially
4949 * quantified variables.
4950 * The input and output dimensions are first turned into parameters.
4951 * compute_divs then returns a map with the same parameters and
4952 * no input or output dimensions and the dimension specification
4953 * is reset to that of the input.
4955 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4957 struct isl_basic_set *bset;
4958 struct isl_set *set;
4959 struct isl_map *map;
4960 struct isl_dim *dim, *orig_dim = NULL;
4965 bmap = isl_basic_map_cow(bmap);
4969 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4970 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4971 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4972 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4976 orig_dim = bmap->dim;
4978 bset = (struct isl_basic_set *)bmap;
4980 set = parameter_compute_divs(bset);
4981 map = (struct isl_map *)set;
4982 map = isl_map_reset_dim(map, orig_dim);
4986 isl_basic_map_free(bmap);
4990 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4998 off = isl_dim_total(bmap->dim);
4999 for (i = 0; i < bmap->n_div; ++i) {
5000 if (isl_int_is_zero(bmap->div[i][0]))
5002 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5008 static int map_divs_known(__isl_keep isl_map *map)
5015 for (i = 0; i < map->n; ++i) {
5016 int known = basic_map_divs_known(map->p[i]);
5024 /* If bmap contains any unknown divs, then compute explicit
5025 * expressions for them. However, this computation may be
5026 * quite expensive, so first try to remove divs that aren't
5029 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5033 struct isl_map *map;
5035 known = basic_map_divs_known(bmap);
5039 return isl_map_from_basic_map(bmap);
5041 bmap = isl_basic_map_drop_redundant_divs(bmap);
5043 known = basic_map_divs_known(bmap);
5047 return isl_map_from_basic_map(bmap);
5049 map = compute_divs(bmap);
5052 isl_basic_map_free(bmap);
5056 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5060 struct isl_map *res;
5067 known = map_divs_known(map);
5075 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5076 for (i = 1 ; i < map->n; ++i) {
5078 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5079 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5080 res = isl_map_union_disjoint(res, r2);
5082 res = isl_map_union(res, r2);
5089 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5091 return (struct isl_set *)
5092 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5095 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5097 return (struct isl_set *)
5098 isl_map_compute_divs((struct isl_map *)set);
5101 struct isl_set *isl_map_domain(struct isl_map *map)
5104 struct isl_set *set;
5109 map = isl_map_cow(map);
5113 set = (struct isl_set *)map;
5114 set->dim = isl_dim_domain(set->dim);
5117 for (i = 0; i < map->n; ++i) {
5118 set->p[i] = isl_basic_map_domain(map->p[i]);
5122 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5123 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5130 struct isl_map *isl_map_union_disjoint(
5131 struct isl_map *map1, struct isl_map *map2)
5135 struct isl_map *map = NULL;
5149 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5151 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5152 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5153 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5155 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5156 map1->n + map2->n, flags);
5159 for (i = 0; i < map1->n; ++i) {
5160 map = isl_map_add_basic_map(map,
5161 isl_basic_map_copy(map1->p[i]));
5165 for (i = 0; i < map2->n; ++i) {
5166 map = isl_map_add_basic_map(map,
5167 isl_basic_map_copy(map2->p[i]));
5181 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5183 map1 = isl_map_union_disjoint(map1, map2);
5187 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5191 struct isl_set *isl_set_union_disjoint(
5192 struct isl_set *set1, struct isl_set *set2)
5194 return (struct isl_set *)
5195 isl_map_union_disjoint(
5196 (struct isl_map *)set1, (struct isl_map *)set2);
5199 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5201 return (struct isl_set *)
5202 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5205 struct isl_map *isl_map_intersect_range(
5206 struct isl_map *map, struct isl_set *set)
5209 struct isl_map *result;
5215 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5216 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5217 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5219 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5220 map->n * set->n, flags);
5223 for (i = 0; i < map->n; ++i)
5224 for (j = 0; j < set->n; ++j) {
5225 result = isl_map_add_basic_map(result,
5226 isl_basic_map_intersect_range(
5227 isl_basic_map_copy(map->p[i]),
5228 isl_basic_set_copy(set->p[j])));
5241 struct isl_map *isl_map_intersect_domain(
5242 struct isl_map *map, struct isl_set *set)
5244 return isl_map_reverse(
5245 isl_map_intersect_range(isl_map_reverse(map), set));
5248 struct isl_map *isl_map_apply_domain(
5249 struct isl_map *map1, struct isl_map *map2)
5253 map1 = isl_map_reverse(map1);
5254 map1 = isl_map_apply_range(map1, map2);
5255 return isl_map_reverse(map1);
5262 struct isl_map *isl_map_apply_range(
5263 struct isl_map *map1, struct isl_map *map2)
5265 struct isl_dim *dim_result;
5266 struct isl_map *result;
5272 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5273 isl_dim_copy(map2->dim));
5275 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5278 for (i = 0; i < map1->n; ++i)
5279 for (j = 0; j < map2->n; ++j) {
5280 result = isl_map_add_basic_map(result,
5281 isl_basic_map_apply_range(
5282 isl_basic_map_copy(map1->p[i]),
5283 isl_basic_map_copy(map2->p[j])));
5289 if (result && result->n <= 1)
5290 ISL_F_SET(result, ISL_MAP_DISJOINT);
5299 * returns range - domain
5301 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5303 isl_dim *dims, *target_dim;
5304 struct isl_basic_set *bset;
5311 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5312 bmap->dim, isl_dim_out),
5314 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5315 dim = isl_basic_map_n_in(bmap);
5316 nparam = isl_basic_map_n_param(bmap);
5317 bset = isl_basic_set_from_basic_map(bmap);
5318 bset = isl_basic_set_cow(bset);
5319 dims = isl_basic_set_get_dim(bset);
5320 dims = isl_dim_add(dims, isl_dim_set, dim);
5321 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5322 bset = isl_basic_set_swap_vars(bset, 2*dim);
5323 for (i = 0; i < dim; ++i) {
5324 int j = isl_basic_map_alloc_equality(
5325 (struct isl_basic_map *)bset);
5328 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5329 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5330 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5331 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5333 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5334 bset = isl_basic_set_reset_dim(bset, target_dim);
5337 isl_basic_map_free(bmap);
5342 * returns range - domain
5344 struct isl_set *isl_map_deltas(struct isl_map *map)
5348 struct isl_set *result;
5353 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5354 map->dim, isl_dim_out),
5356 dim = isl_map_get_dim(map);
5357 dim = isl_dim_domain(dim);
5358 result = isl_set_alloc_dim(dim, map->n, map->flags);
5361 for (i = 0; i < map->n; ++i)
5362 result = isl_set_add_basic_set(result,
5363 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5371 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5373 struct isl_basic_map *bmap;
5381 nparam = dims->nparam;
5383 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5387 for (i = 0; i < dim; ++i) {
5388 int j = isl_basic_map_alloc_equality(bmap);
5391 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5392 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5393 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5395 return isl_basic_map_finalize(bmap);
5397 isl_basic_map_free(bmap);
5401 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
5403 struct isl_dim *dim = isl_dim_map(set_dim);
5406 return basic_map_identity(dim);
5409 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5411 if (!model || !model->dim)
5413 isl_assert(model->ctx,
5414 model->dim->n_in == model->dim->n_out, return NULL);
5415 return basic_map_identity(isl_dim_copy(model->dim));
5418 static struct isl_map *map_identity(struct isl_dim *dim)
5420 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
5421 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
5424 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
5426 struct isl_dim *dim = isl_dim_map(set_dim);
5429 return map_identity(dim);
5432 struct isl_map *isl_map_identity_like(struct isl_map *model)
5434 if (!model || !model->dim)
5436 isl_assert(model->ctx,
5437 model->dim->n_in == model->dim->n_out, return NULL);
5438 return map_identity(isl_dim_copy(model->dim));
5441 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5443 if (!model || !model->dim)
5445 isl_assert(model->ctx,
5446 model->dim->n_in == model->dim->n_out, return NULL);
5447 return map_identity(isl_dim_copy(model->dim));
5450 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5452 isl_dim *dim = isl_set_get_dim(set);
5454 id = isl_map_identity(dim);
5455 return isl_map_intersect_range(id, set);
5458 /* Construct a basic set with all set dimensions having only non-negative
5461 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5466 struct isl_basic_set *bset;
5470 nparam = dims->nparam;
5472 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5475 for (i = 0; i < dim; ++i) {
5476 int k = isl_basic_set_alloc_inequality(bset);
5479 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5480 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5484 isl_basic_set_free(bset);
5488 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5489 enum isl_dim_type type, unsigned first, unsigned n)
5492 isl_basic_set *nonneg = NULL;
5493 isl_basic_set *neg = NULL;
5500 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5502 for (i = 0; i < n; ++i) {
5506 nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5507 k = isl_basic_set_alloc_inequality(nonneg);
5510 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5511 isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
5513 neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5514 k = isl_basic_set_alloc_inequality(neg);
5517 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5518 isl_int_set_si(neg->ineq[k][0], -1);
5519 isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
5521 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5526 isl_basic_set_free(nonneg);
5527 isl_basic_set_free(neg);
5532 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5534 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5537 int isl_basic_map_is_subset(
5538 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5541 struct isl_map *map1;
5542 struct isl_map *map2;
5544 if (!bmap1 || !bmap2)
5547 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5548 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5550 is_subset = isl_map_is_subset(map1, map2);
5558 int isl_basic_map_is_equal(
5559 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5563 if (!bmap1 || !bmap2)
5565 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5568 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5572 int isl_basic_set_is_equal(
5573 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5575 return isl_basic_map_is_equal(
5576 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5579 int isl_map_is_empty(struct isl_map *map)
5586 for (i = 0; i < map->n; ++i) {
5587 is_empty = isl_basic_map_is_empty(map->p[i]);
5596 int isl_map_fast_is_empty(struct isl_map *map)
5598 return map ? map->n == 0 : -1;
5601 int isl_set_fast_is_empty(struct isl_set *set)
5603 return set ? set->n == 0 : -1;
5606 int isl_set_is_empty(struct isl_set *set)
5608 return isl_map_is_empty((struct isl_map *)set);
5611 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
5616 return isl_dim_equal(map1->dim, map2->dim);
5619 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5624 return isl_dim_equal(set1->dim, set2->dim);
5627 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
5633 is_subset = isl_map_is_subset(map1, map2);
5636 is_subset = isl_map_is_subset(map2, map1);
5640 int isl_basic_map_is_strict_subset(
5641 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5645 if (!bmap1 || !bmap2)
5647 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5650 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5651 if (is_subset == -1)
5656 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
5662 is_subset = isl_map_is_subset(map1, map2);
5665 is_subset = isl_map_is_subset(map2, map1);
5666 if (is_subset == -1)
5671 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5673 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5676 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5680 return bmap->n_eq == 0 && bmap->n_ineq == 0;
5683 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5687 return bset->n_eq == 0 && bset->n_ineq == 0;
5690 int isl_map_fast_is_universe(__isl_keep isl_map *map)
5697 for (i = 0; i < map->n; ++i) {
5698 int r = isl_basic_map_is_universe(map->p[i]);
5706 int isl_set_fast_is_universe(__isl_keep isl_set *set)
5708 return isl_map_fast_is_universe((isl_map *) set);
5711 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
5713 struct isl_basic_set *bset = NULL;
5714 struct isl_vec *sample = NULL;
5721 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
5724 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
5725 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
5726 copy = isl_basic_map_remove_redundancies(copy);
5727 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
5728 isl_basic_map_free(copy);
5732 total = 1 + isl_basic_map_total_dim(bmap);
5733 if (bmap->sample && bmap->sample->size == total) {
5734 int contains = isl_basic_map_contains(bmap, bmap->sample);
5740 isl_vec_free(bmap->sample);
5741 bmap->sample = NULL;
5742 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
5745 sample = isl_basic_set_sample_vec(bset);
5748 empty = sample->size == 0;
5749 isl_vec_free(bmap->sample);
5750 bmap->sample = sample;
5752 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
5757 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
5761 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
5764 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
5768 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
5771 int isl_basic_set_is_empty(struct isl_basic_set *bset)
5773 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
5776 struct isl_map *isl_basic_map_union(
5777 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5779 struct isl_map *map;
5780 if (!bmap1 || !bmap2)
5783 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
5785 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
5788 map = isl_map_add_basic_map(map, bmap1);
5789 map = isl_map_add_basic_map(map, bmap2);
5792 isl_basic_map_free(bmap1);
5793 isl_basic_map_free(bmap2);
5797 struct isl_set *isl_basic_set_union(
5798 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5800 return (struct isl_set *)isl_basic_map_union(
5801 (struct isl_basic_map *)bset1,
5802 (struct isl_basic_map *)bset2);
5805 /* Order divs such that any div only depends on previous divs */
5806 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
5814 off = isl_dim_total(bmap->dim);
5816 for (i = 0; i < bmap->n_div; ++i) {
5818 if (isl_int_is_zero(bmap->div[i][0]))
5820 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
5824 isl_basic_map_swap_div(bmap, i, i + pos);
5830 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
5832 return (struct isl_basic_set *)
5833 isl_basic_map_order_divs((struct isl_basic_map *)bset);
5836 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
5843 for (i = 0; i < map->n; ++i) {
5844 map->p[i] = isl_basic_map_order_divs(map->p[i]);
5855 /* Look for a div in dst that corresponds to the div "div" in src.
5856 * The divs before "div" in src and dst are assumed to be the same.
5858 * Returns -1 if no corresponding div was found and the position
5859 * of the corresponding div in dst otherwise.
5861 static int find_div(struct isl_basic_map *dst,
5862 struct isl_basic_map *src, unsigned div)
5866 unsigned total = isl_dim_total(src->dim);
5868 isl_assert(dst->ctx, div <= dst->n_div, return -1);
5869 for (i = div; i < dst->n_div; ++i)
5870 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
5871 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
5872 dst->n_div - div) == -1)
5877 struct isl_basic_map *isl_basic_map_align_divs(
5878 struct isl_basic_map *dst, struct isl_basic_map *src)
5881 unsigned total = isl_dim_total(src->dim);
5886 if (src->n_div == 0)
5889 for (i = 0; i < src->n_div; ++i)
5890 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
5892 src = isl_basic_map_order_divs(src);
5893 dst = isl_basic_map_cow(dst);
5894 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
5895 src->n_div, 0, 2 * src->n_div);
5898 for (i = 0; i < src->n_div; ++i) {
5899 int j = find_div(dst, src, i);
5901 j = isl_basic_map_alloc_div(dst);
5904 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
5905 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
5906 if (isl_basic_map_add_div_constraints(dst, j) < 0)
5910 isl_basic_map_swap_div(dst, i, j);
5914 isl_basic_map_free(dst);
5918 struct isl_basic_set *isl_basic_set_align_divs(
5919 struct isl_basic_set *dst, struct isl_basic_set *src)
5921 return (struct isl_basic_set *)isl_basic_map_align_divs(
5922 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
5925 struct isl_map *isl_map_align_divs(struct isl_map *map)
5933 map = isl_map_compute_divs(map);
5934 map = isl_map_cow(map);
5938 for (i = 1; i < map->n; ++i)
5939 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5940 for (i = 1; i < map->n; ++i)
5941 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5943 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5947 struct isl_set *isl_set_align_divs(struct isl_set *set)
5949 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5952 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5956 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5957 map = isl_map_intersect_domain(map, set);
5958 set = isl_map_range(map);
5966 /* There is no need to cow as removing empty parts doesn't change
5967 * the meaning of the set.
5969 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5976 for (i = map->n-1; i >= 0; --i) {
5977 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5979 isl_basic_map_free(map->p[i]);
5980 if (i != map->n-1) {
5981 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5982 map->p[i] = map->p[map->n-1];
5990 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
5992 return (struct isl_set *)
5993 isl_map_remove_empty_parts((struct isl_map *)set);
5996 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
5998 struct isl_basic_map *bmap;
5999 if (!map || map->n == 0)
6001 bmap = map->p[map->n-1];
6002 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6003 return isl_basic_map_copy(bmap);
6006 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6008 return (struct isl_basic_set *)
6009 isl_map_copy_basic_map((struct isl_map *)set);
6012 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6013 __isl_keep isl_basic_map *bmap)
6019 for (i = map->n-1; i >= 0; --i) {
6020 if (map->p[i] != bmap)
6022 map = isl_map_cow(map);
6025 isl_basic_map_free(map->p[i]);
6026 if (i != map->n-1) {
6027 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6028 map->p[i] = map->p[map->n-1];
6039 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6040 struct isl_basic_set *bset)
6042 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6043 (struct isl_basic_map *)bset);
6046 /* Given two basic sets bset1 and bset2, compute the maximal difference
6047 * between the values of dimension pos in bset1 and those in bset2
6048 * for any common value of the parameters and dimensions preceding pos.
6050 static enum isl_lp_result basic_set_maximal_difference_at(
6051 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6052 int pos, isl_int *opt)
6054 struct isl_dim *dims;
6055 struct isl_basic_map *bmap1 = NULL;
6056 struct isl_basic_map *bmap2 = NULL;
6057 struct isl_ctx *ctx;
6058 struct isl_vec *obj;
6061 unsigned dim1, dim2;
6062 enum isl_lp_result res;
6064 if (!bset1 || !bset2)
6065 return isl_lp_error;
6067 nparam = isl_basic_set_n_param(bset1);
6068 dim1 = isl_basic_set_n_dim(bset1);
6069 dim2 = isl_basic_set_n_dim(bset2);
6070 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6071 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6072 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6073 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6074 if (!bmap1 || !bmap2)
6076 bmap1 = isl_basic_map_cow(bmap1);
6077 bmap1 = isl_basic_map_extend(bmap1, nparam,
6078 pos, (dim1 - pos) + (dim2 - pos),
6079 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6080 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6083 total = isl_basic_map_total_dim(bmap1);
6085 obj = isl_vec_alloc(ctx, 1 + total);
6086 isl_seq_clr(obj->block.data, 1 + total);
6087 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6088 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6091 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6093 isl_basic_map_free(bmap1);
6097 isl_basic_map_free(bmap1);
6098 isl_basic_map_free(bmap2);
6099 return isl_lp_error;
6102 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6103 * for any common value of the parameters and dimensions preceding pos
6104 * in both basic sets, the values of dimension pos in bset1 are
6105 * smaller or larger than those in bset2.
6108 * 1 if bset1 follows bset2
6109 * -1 if bset1 precedes bset2
6110 * 0 if bset1 and bset2 are incomparable
6111 * -2 if some error occurred.
6113 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6114 struct isl_basic_set *bset2, int pos)
6117 enum isl_lp_result res;
6122 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6124 if (res == isl_lp_empty)
6126 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6127 res == isl_lp_unbounded)
6129 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6138 /* Given two basic sets bset1 and bset2, check whether
6139 * for any common value of the parameters and dimensions preceding pos
6140 * there is a value of dimension pos in bset1 that is larger
6141 * than a value of the same dimension in bset2.
6144 * 1 if there exists such a pair
6145 * 0 if there is no such pair, but there is a pair of equal values
6147 * -2 if some error occurred.
6149 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6150 __isl_keep isl_basic_set *bset2, int pos)
6153 enum isl_lp_result res;
6158 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6160 if (res == isl_lp_empty)
6162 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6163 res == isl_lp_unbounded)
6165 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6167 else if (res == isl_lp_ok)
6176 /* Given two sets set1 and set2, check whether
6177 * for any common value of the parameters and dimensions preceding pos
6178 * there is a value of dimension pos in set1 that is larger
6179 * than a value of the same dimension in set2.
6182 * 1 if there exists such a pair
6183 * 0 if there is no such pair, but there is a pair of equal values
6185 * -2 if some error occurred.
6187 int isl_set_follows_at(__isl_keep isl_set *set1,
6188 __isl_keep isl_set *set2, int pos)
6196 for (i = 0; i < set1->n; ++i)
6197 for (j = 0; j < set2->n; ++j) {
6199 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6200 if (f == 1 || f == -2)
6209 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
6210 unsigned pos, isl_int *val)
6218 total = isl_basic_map_total_dim(bmap);
6219 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6220 for (; d+1 > pos; --d)
6221 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6225 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6227 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6229 if (!isl_int_is_one(bmap->eq[i][1+d]))
6232 isl_int_neg(*val, bmap->eq[i][0]);
6238 static int isl_map_fast_has_fixed_var(struct isl_map *map,
6239 unsigned pos, isl_int *val)
6251 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
6254 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
6255 for (i = 1; fixed == 1 && i < map->n; ++i) {
6256 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
6257 if (fixed == 1 && isl_int_ne(tmp, v))
6261 isl_int_set(*val, v);
6267 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
6268 unsigned pos, isl_int *val)
6270 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
6274 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
6277 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
6280 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
6281 enum isl_dim_type type, unsigned pos, isl_int *val)
6283 if (pos >= isl_basic_map_dim(bmap, type))
6285 return isl_basic_map_fast_has_fixed_var(bmap,
6286 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6289 int isl_map_fast_is_fixed(struct isl_map *map,
6290 enum isl_dim_type type, unsigned pos, isl_int *val)
6292 if (pos >= isl_map_dim(map, type))
6294 return isl_map_fast_has_fixed_var(map,
6295 map_offset(map, type) - 1 + pos, val);
6298 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6299 * then return this fixed value in *val.
6301 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6304 return isl_basic_set_fast_has_fixed_var(bset,
6305 isl_basic_set_n_param(bset) + dim, val);
6308 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6309 * then return this fixed value in *val.
6311 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6313 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6316 /* Check if input variable in has fixed value and if so and if val is not NULL,
6317 * then return this fixed value in *val.
6319 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6321 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6324 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6325 * and if val is not NULL, then return this lower bound in *val.
6327 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6328 unsigned dim, isl_int *val)
6330 int i, i_eq = -1, i_ineq = -1;
6337 total = isl_basic_set_total_dim(bset);
6338 nparam = isl_basic_set_n_param(bset);
6339 for (i = 0; i < bset->n_eq; ++i) {
6340 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6346 for (i = 0; i < bset->n_ineq; ++i) {
6347 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6349 if (i_eq != -1 || i_ineq != -1)
6353 if (i_eq == -1 && i_ineq == -1)
6355 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6356 /* The coefficient should always be one due to normalization. */
6357 if (!isl_int_is_one(c[1+nparam+dim]))
6359 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6361 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6362 total - nparam - dim - 1) != -1)
6365 isl_int_neg(*val, c[0]);
6369 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6370 unsigned dim, isl_int *val)
6382 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6386 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6388 for (i = 1; fixed == 1 && i < set->n; ++i) {
6389 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6391 if (fixed == 1 && isl_int_ne(tmp, v))
6395 isl_int_set(*val, v);
6406 static int qsort_constraint_cmp(const void *p1, const void *p2)
6408 const struct constraint *c1 = (const struct constraint *)p1;
6409 const struct constraint *c2 = (const struct constraint *)p2;
6410 unsigned size = isl_min(c1->size, c2->size);
6411 return isl_seq_cmp(c1->c, c2->c, size);
6414 static struct isl_basic_map *isl_basic_map_sort_constraints(
6415 struct isl_basic_map *bmap)
6418 struct constraint *c;
6423 total = isl_basic_map_total_dim(bmap);
6424 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6427 for (i = 0; i < bmap->n_ineq; ++i) {
6429 c[i].c = bmap->ineq[i];
6431 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6432 for (i = 0; i < bmap->n_ineq; ++i)
6433 bmap->ineq[i] = c[i].c;
6437 isl_basic_map_free(bmap);
6441 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6442 __isl_take isl_basic_set *bset)
6444 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6445 (struct isl_basic_map *)bset);
6448 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6452 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6454 bmap = isl_basic_map_remove_redundancies(bmap);
6455 bmap = isl_basic_map_sort_constraints(bmap);
6456 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6460 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6462 return (struct isl_basic_set *)isl_basic_map_normalize(
6463 (struct isl_basic_map *)bset);
6466 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6467 const __isl_keep isl_basic_map *bmap2)
6474 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6475 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6476 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6477 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6478 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6479 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6480 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6481 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6483 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6485 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6487 if (bmap1->n_eq != bmap2->n_eq)
6488 return bmap1->n_eq - bmap2->n_eq;
6489 if (bmap1->n_ineq != bmap2->n_ineq)
6490 return bmap1->n_ineq - bmap2->n_ineq;
6491 if (bmap1->n_div != bmap2->n_div)
6492 return bmap1->n_div - bmap2->n_div;
6493 total = isl_basic_map_total_dim(bmap1);
6494 for (i = 0; i < bmap1->n_eq; ++i) {
6495 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6499 for (i = 0; i < bmap1->n_ineq; ++i) {
6500 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6504 for (i = 0; i < bmap1->n_div; ++i) {
6505 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6512 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6513 __isl_keep isl_basic_map *bmap2)
6515 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6518 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6519 __isl_keep isl_basic_set *bset2)
6521 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6522 (isl_basic_map *)bset2);
6525 static int qsort_bmap_cmp(const void *p1, const void *p2)
6527 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6528 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6530 return isl_basic_map_fast_cmp(bmap1, bmap2);
6533 /* We normalize in place, but if anything goes wrong we need
6534 * to return NULL, so we need to make sure we don't change the
6535 * meaning of any possible other copies of map.
6537 struct isl_map *isl_map_normalize(struct isl_map *map)
6540 struct isl_basic_map *bmap;
6544 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6546 for (i = 0; i < map->n; ++i) {
6547 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6550 isl_basic_map_free(map->p[i]);
6553 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6554 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6555 map = isl_map_remove_empty_parts(map);
6558 for (i = map->n - 1; i >= 1; --i) {
6559 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6561 isl_basic_map_free(map->p[i-1]);
6562 for (j = i; j < map->n; ++j)
6563 map->p[j-1] = map->p[j];
6573 struct isl_set *isl_set_normalize(struct isl_set *set)
6575 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
6578 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
6588 if (!isl_dim_equal(map1->dim, map2->dim))
6591 map1 = isl_map_copy(map1);
6592 map2 = isl_map_copy(map2);
6593 map1 = isl_map_normalize(map1);
6594 map2 = isl_map_normalize(map2);
6597 equal = map1->n == map2->n;
6598 for (i = 0; equal && i < map1->n; ++i) {
6599 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
6612 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
6614 return isl_map_fast_is_equal((struct isl_map *)set1,
6615 (struct isl_map *)set2);
6618 /* Return an interval that ranges from min to max (inclusive)
6620 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
6621 isl_int min, isl_int max)
6624 struct isl_basic_set *bset = NULL;
6626 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
6630 k = isl_basic_set_alloc_inequality(bset);
6633 isl_int_set_si(bset->ineq[k][1], 1);
6634 isl_int_neg(bset->ineq[k][0], min);
6636 k = isl_basic_set_alloc_inequality(bset);
6639 isl_int_set_si(bset->ineq[k][1], -1);
6640 isl_int_set(bset->ineq[k][0], max);
6644 isl_basic_set_free(bset);
6648 /* Return the Cartesian product of the basic sets in list (in the given order).
6650 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
6658 struct isl_basic_set *product = NULL;
6662 isl_assert(list->ctx, list->n > 0, goto error);
6663 isl_assert(list->ctx, list->p[0], goto error);
6664 nparam = isl_basic_set_n_param(list->p[0]);
6665 dim = isl_basic_set_n_dim(list->p[0]);
6666 extra = list->p[0]->n_div;
6667 n_eq = list->p[0]->n_eq;
6668 n_ineq = list->p[0]->n_ineq;
6669 for (i = 1; i < list->n; ++i) {
6670 isl_assert(list->ctx, list->p[i], goto error);
6671 isl_assert(list->ctx,
6672 nparam == isl_basic_set_n_param(list->p[i]), goto error);
6673 dim += isl_basic_set_n_dim(list->p[i]);
6674 extra += list->p[i]->n_div;
6675 n_eq += list->p[i]->n_eq;
6676 n_ineq += list->p[i]->n_ineq;
6678 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
6683 for (i = 0; i < list->n; ++i) {
6684 isl_basic_set_add_constraints(product,
6685 isl_basic_set_copy(list->p[i]), dim);
6686 dim += isl_basic_set_n_dim(list->p[i]);
6688 isl_basic_set_list_free(list);
6691 isl_basic_set_free(product);
6692 isl_basic_set_list_free(list);
6696 struct isl_basic_map *isl_basic_map_product(
6697 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6699 struct isl_dim *dim_result = NULL;
6700 struct isl_basic_map *bmap;
6701 unsigned in1, in2, out1, out2, nparam, total, pos;
6702 struct isl_dim_map *dim_map1, *dim_map2;
6704 if (!bmap1 || !bmap2)
6707 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
6708 bmap2->dim, isl_dim_param), goto error);
6709 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
6710 isl_dim_copy(bmap2->dim));
6712 in1 = isl_basic_map_n_in(bmap1);
6713 in2 = isl_basic_map_n_in(bmap2);
6714 out1 = isl_basic_map_n_out(bmap1);
6715 out2 = isl_basic_map_n_out(bmap2);
6716 nparam = isl_basic_map_n_param(bmap1);
6718 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
6719 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
6720 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
6721 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
6722 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
6723 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
6724 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
6725 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
6726 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
6727 isl_dim_map_div(dim_map1, bmap1, pos += out2);
6728 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
6730 bmap = isl_basic_map_alloc_dim(dim_result,
6731 bmap1->n_div + bmap2->n_div,
6732 bmap1->n_eq + bmap2->n_eq,
6733 bmap1->n_ineq + bmap2->n_ineq);
6734 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
6735 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
6736 bmap = isl_basic_map_simplify(bmap);
6737 return isl_basic_map_finalize(bmap);
6739 isl_basic_map_free(bmap1);
6740 isl_basic_map_free(bmap2);
6744 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
6746 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
6749 struct isl_map *result;
6755 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
6756 map2->dim, isl_dim_param), goto error);
6758 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6759 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6760 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6762 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
6763 isl_dim_copy(map2->dim)),
6764 map1->n * map2->n, flags);
6767 for (i = 0; i < map1->n; ++i)
6768 for (j = 0; j < map2->n; ++j) {
6769 struct isl_basic_map *part;
6770 part = isl_basic_map_product(
6771 isl_basic_map_copy(map1->p[i]),
6772 isl_basic_map_copy(map2->p[j]));
6773 if (isl_basic_map_is_empty(part))
6774 isl_basic_map_free(part);
6776 result = isl_map_add_basic_map(result, part);
6789 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6791 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
6792 __isl_take isl_map *map2)
6796 prod = isl_map_product(map1, map2);
6797 prod = isl_map_flatten(prod);
6801 /* Given two set A and B, construct its Cartesian product A x B.
6803 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
6805 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
6806 (struct isl_map *)set2);
6809 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
6810 __isl_take isl_set *set2)
6812 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
6815 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
6818 uint32_t hash = isl_hash_init();
6823 bset = isl_basic_set_copy(bset);
6824 bset = isl_basic_set_normalize(bset);
6827 total = isl_basic_set_total_dim(bset);
6828 isl_hash_byte(hash, bset->n_eq & 0xFF);
6829 for (i = 0; i < bset->n_eq; ++i) {
6831 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
6832 isl_hash_hash(hash, c_hash);
6834 isl_hash_byte(hash, bset->n_ineq & 0xFF);
6835 for (i = 0; i < bset->n_ineq; ++i) {
6837 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
6838 isl_hash_hash(hash, c_hash);
6840 isl_hash_byte(hash, bset->n_div & 0xFF);
6841 for (i = 0; i < bset->n_div; ++i) {
6843 if (isl_int_is_zero(bset->div[i][0]))
6845 isl_hash_byte(hash, i & 0xFF);
6846 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
6847 isl_hash_hash(hash, c_hash);
6849 isl_basic_set_free(bset);
6853 uint32_t isl_set_get_hash(struct isl_set *set)
6860 set = isl_set_copy(set);
6861 set = isl_set_normalize(set);
6865 hash = isl_hash_init();
6866 for (i = 0; i < set->n; ++i) {
6868 bset_hash = isl_basic_set_get_hash(set->p[i]);
6869 isl_hash_hash(hash, bset_hash);
6877 /* Check if the value for dimension dim is completely determined
6878 * by the values of the other parameters and variables.
6879 * That is, check if dimension dim is involved in an equality.
6881 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
6888 nparam = isl_basic_set_n_param(bset);
6889 for (i = 0; i < bset->n_eq; ++i)
6890 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
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
6898 * for each of the subsets.
6900 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
6906 for (i = 0; i < set->n; ++i) {
6908 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
6915 int isl_set_n_basic_set(__isl_keep isl_set *set)
6917 return set ? set->n : 0;
6920 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
6921 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
6928 for (i = 0; i < map->n; ++i)
6929 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
6935 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
6936 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
6943 for (i = 0; i < set->n; ++i)
6944 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
6950 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
6952 struct isl_dim *dim;
6957 if (bset->n_div == 0)
6960 bset = isl_basic_set_cow(bset);
6964 dim = isl_basic_set_get_dim(bset);
6965 dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
6968 isl_dim_free(bset->dim);
6972 bset = isl_basic_set_finalize(bset);
6976 isl_basic_set_free(bset);
6980 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
6983 struct isl_dim *dim;
6986 set = isl_set_align_divs(set);
6990 if (set->n == 0 || set->p[0]->n_div == 0)
6993 set = isl_set_cow(set);
6997 n_div = set->p[0]->n_div;
6998 dim = isl_set_get_dim(set);
6999 dim = isl_dim_add(dim, isl_dim_set, n_div);
7002 isl_dim_free(set->dim);
7005 for (i = 0; i < set->n; ++i) {
7006 set->p[i] = isl_basic_set_lift(set->p[i]);
7017 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7019 struct isl_dim *dim;
7020 struct isl_basic_map *bmap;
7027 set = isl_set_align_divs(set);
7032 dim = isl_set_get_dim(set);
7033 if (set->n == 0 || set->p[0]->n_div == 0) {
7035 return isl_map_identity(dim);
7038 n_div = set->p[0]->n_div;
7039 dim = isl_dim_map(dim);
7040 n_param = isl_dim_size(dim, isl_dim_param);
7041 n_set = isl_dim_size(dim, isl_dim_in);
7042 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7043 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7044 for (i = 0; i < n_set; ++i)
7045 bmap = var_equal(bmap, i);
7047 total = n_param + n_set + n_set + n_div;
7048 for (i = 0; i < n_div; ++i) {
7049 k = isl_basic_map_alloc_inequality(bmap);
7052 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7053 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7054 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7055 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7056 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7057 set->p[0]->div[i][0]);
7059 l = isl_basic_map_alloc_inequality(bmap);
7062 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7063 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7064 set->p[0]->div[i][0]);
7065 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7069 return isl_map_from_basic_map(bmap);
7072 isl_basic_map_free(bmap);
7076 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7084 dim = isl_basic_set_total_dim(bset);
7085 size += bset->n_eq * (1 + dim);
7086 size += bset->n_ineq * (1 + dim);
7087 size += bset->n_div * (2 + dim);
7092 int isl_set_size(__isl_keep isl_set *set)
7100 for (i = 0; i < set->n; ++i)
7101 size += isl_basic_set_size(set->p[i]);
7106 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7107 enum isl_dim_type type, unsigned pos)
7115 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7117 pos += isl_basic_map_offset(bmap, type);
7119 for (i = 0; i < bmap->n_eq; ++i)
7120 if (!isl_int_is_zero(bmap->eq[i][pos]))
7124 for (i = 0; i < bmap->n_ineq; ++i) {
7125 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7132 return lower && upper;
7135 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7136 enum isl_dim_type type, unsigned pos)
7143 for (i = 0; i < map->n; ++i) {
7145 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7146 if (bounded < 0 || !bounded)
7153 /* Return 1 if the specified dim is involved in both an upper bound
7154 * and a lower bound.
7156 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7157 enum isl_dim_type type, unsigned pos)
7159 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7162 /* For each of the "n" variables starting at "first", determine
7163 * the sign of the variable and put the results in the first "n"
7164 * elements of the array "signs".
7166 * 1 means that the variable is non-negative
7167 * -1 means that the variable is non-positive
7168 * 0 means the variable attains both positive and negative values.
7170 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7171 unsigned first, unsigned n, int *signs)
7173 isl_vec *bound = NULL;
7174 struct isl_tab *tab = NULL;
7175 struct isl_tab_undo *snap;
7178 if (!bset || !signs)
7181 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7182 tab = isl_tab_from_basic_set(bset);
7186 isl_seq_clr(bound->el, bound->size);
7187 isl_int_set_si(bound->el[0], -1);
7189 snap = isl_tab_snap(tab);
7190 for (i = 0; i < n; ++i) {
7193 isl_int_set_si(bound->el[1 + first + i], -1);
7194 if (isl_tab_add_ineq(tab, bound->el) < 0)
7197 isl_int_set_si(bound->el[1 + first + i], 0);
7198 if (isl_tab_rollback(tab, snap) < 0)
7206 isl_int_set_si(bound->el[1 + first + i], 1);
7207 if (isl_tab_add_ineq(tab, bound->el) < 0)
7210 isl_int_set_si(bound->el[1 + first + i], 0);
7211 if (isl_tab_rollback(tab, snap) < 0)
7214 signs[i] = empty ? -1 : 0;
7218 isl_vec_free(bound);
7222 isl_vec_free(bound);
7226 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7227 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7229 if (!bset || !signs)
7231 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7234 first += pos(bset->dim, type) - 1;
7235 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7238 /* Check if the given map is single-valued.
7243 * and check if the result is a subset of the identity mapping.
7245 int isl_map_is_single_valued(__isl_keep isl_map *map)
7251 test = isl_map_reverse(isl_map_copy(map));
7252 test = isl_map_apply_range(test, isl_map_copy(map));
7254 id = isl_map_identity(isl_dim_range(isl_map_get_dim(map)));
7256 sv = isl_map_is_subset(test, id);
7264 int isl_map_is_bijective(__isl_keep isl_map *map)
7268 sv = isl_map_is_single_valued(map);
7272 map = isl_map_copy(map);
7273 map = isl_map_reverse(map);
7274 sv = isl_map_is_single_valued(map);
7280 int isl_set_is_singleton(__isl_keep isl_set *set)
7282 return isl_map_is_single_valued((isl_map *)set);
7285 int isl_map_is_translation(__isl_keep isl_map *map)
7290 delta = isl_map_deltas(isl_map_copy(map));
7291 ok = isl_set_is_singleton(delta);
7292 isl_set_free(delta);
7297 static int unique(isl_int *p, unsigned pos, unsigned len)
7299 if (isl_seq_first_non_zero(p, pos) != -1)
7301 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7306 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7315 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7318 nvar = isl_basic_set_dim(bset, isl_dim_set);
7319 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7320 for (j = 0; j < nvar; ++j) {
7321 int lower = 0, upper = 0;
7322 for (i = 0; i < bset->n_eq; ++i) {
7323 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7325 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7331 for (i = 0; i < bset->n_ineq; ++i) {
7332 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7334 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7336 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7341 if (!lower || !upper)
7348 int isl_set_is_box(__isl_keep isl_set *set)
7355 return isl_basic_set_is_box(set->p[0]);
7358 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7363 return isl_dim_is_wrapping(bset->dim);
7366 int isl_set_is_wrapping(__isl_keep isl_set *set)
7371 return isl_dim_is_wrapping(set->dim);
7374 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7376 bmap = isl_basic_map_cow(bmap);
7380 bmap->dim = isl_dim_wrap(bmap->dim);
7384 bmap = isl_basic_map_finalize(bmap);
7386 return (isl_basic_set *)bmap;
7388 isl_basic_map_free(bmap);
7392 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7396 map = isl_map_cow(map);
7400 for (i = 0; i < map->n; ++i) {
7401 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7405 map->dim = isl_dim_wrap(map->dim);
7409 return (isl_set *)map;
7415 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
7417 bset = isl_basic_set_cow(bset);
7421 bset->dim = isl_dim_unwrap(bset->dim);
7425 bset = isl_basic_set_finalize(bset);
7427 return (isl_basic_map *)bset;
7429 isl_basic_set_free(bset);
7433 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
7440 if (!isl_set_is_wrapping(set))
7441 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
7444 set = isl_set_cow(set);
7448 for (i = 0; i < set->n; ++i) {
7449 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
7454 set->dim = isl_dim_unwrap(set->dim);
7458 return (isl_map *)set;
7464 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
7465 enum isl_dim_type type)
7470 if (!isl_dim_is_named_or_nested(bmap->dim, type))
7473 bmap = isl_basic_map_cow(bmap);
7477 bmap->dim = isl_dim_reset(bmap->dim, type);
7481 bmap = isl_basic_map_finalize(bmap);
7485 isl_basic_map_free(bmap);
7489 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
7490 enum isl_dim_type type)
7497 if (!isl_dim_is_named_or_nested(map->dim, type))
7500 map = isl_map_cow(map);
7504 for (i = 0; i < map->n; ++i) {
7505 map->p[i] = isl_basic_map_reset(map->p[i], type);
7509 map->dim = isl_dim_reset(map->dim, type);
7519 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
7524 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
7527 bmap = isl_basic_map_cow(bmap);
7531 bmap->dim = isl_dim_flatten(bmap->dim);
7535 bmap = isl_basic_map_finalize(bmap);
7539 isl_basic_map_free(bmap);
7543 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
7550 if (!map->dim->nested[0] && !map->dim->nested[1])
7553 map = isl_map_cow(map);
7557 for (i = 0; i < map->n; ++i) {
7558 map->p[i] = isl_basic_map_flatten(map->p[i]);
7562 map->dim = isl_dim_flatten(map->dim);
7572 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
7574 return (isl_set *)isl_map_flatten((isl_map *)set);
7577 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
7579 isl_dim *dim, *flat_dim;
7582 dim = isl_set_get_dim(set);
7583 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
7584 map = map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
7585 map = isl_map_intersect_domain(map, set);
7590 /* Extend the given dim_map with mappings for the divs in bmap.
7592 static __isl_give struct isl_dim_map *extend_dim_map(
7593 __isl_keep struct isl_dim_map *dim_map,
7594 __isl_keep isl_basic_map *bmap)
7597 struct isl_dim_map *res;
7600 offset = isl_basic_map_offset(bmap, isl_dim_div);
7602 res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
7606 for (i = 0; i < dim_map->len; ++i)
7607 res->pos[i] = dim_map->pos[i];
7608 for (i = 0; i < bmap->n_div; ++i)
7609 res->pos[dim_map->len + i] = offset + i;
7614 /* Extract a dim_map from a reordering.
7615 * We essentially need to reverse the mapping, and add an offset
7616 * of 1 for the constant term.
7618 __isl_give struct isl_dim_map *isl_dim_map_from_reordering(
7619 __isl_keep isl_reordering *exp)
7622 struct isl_dim_map *dim_map;
7627 dim_map = isl_dim_map_alloc(exp->dim->ctx, isl_dim_total(exp->dim));
7631 for (i = 0; i < exp->len; ++i)
7632 dim_map->pos[1 + exp->pos[i]] = 1 + i;
7637 /* Reorder the dimensions of "bmap" according to the given dim_map
7638 * and set the dimension specification to "dim".
7640 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
7641 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
7645 bmap = isl_basic_map_cow(bmap);
7646 if (!bmap || !dim || !dim_map)
7649 res = isl_basic_map_alloc_dim(dim,
7650 bmap->n_div, bmap->n_eq, bmap->n_ineq);
7651 res = add_constraints_dim_map(res, bmap, dim_map);
7652 res = isl_basic_map_finalize(res);
7656 isl_basic_map_free(bmap);
7661 /* Reorder the dimensions of "map" according to given reordering.
7663 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
7664 __isl_take isl_reordering *r)
7667 struct isl_dim_map *dim_map;
7669 map = isl_map_cow(map);
7670 dim_map = isl_dim_map_from_reordering(r);
7671 if (!map || !r || !dim_map)
7674 for (i = 0; i < map->n; ++i) {
7675 struct isl_dim_map *dim_map_i;
7677 dim_map_i = extend_dim_map(dim_map, map->p[i]);
7679 map->p[i] = isl_basic_map_realign(map->p[i],
7680 isl_dim_copy(r->dim), dim_map_i);
7686 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
7688 isl_reordering_free(r);
7694 isl_reordering_free(r);
7698 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
7699 __isl_take isl_reordering *r)
7701 return (isl_set *)isl_map_realign((isl_map *)set, r);
7704 __isl_give isl_mat *isl_basic_map_equalities_matrix(
7705 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
7706 enum isl_dim_type c2, enum isl_dim_type c3,
7707 enum isl_dim_type c4, enum isl_dim_type c5)
7709 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
7710 struct isl_mat *mat;
7716 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
7717 isl_basic_map_total_dim(bmap) + 1);
7720 for (i = 0; i < bmap->n_eq; ++i)
7721 for (j = 0, pos = 0; j < 5; ++j) {
7722 int off = isl_basic_map_offset(bmap, c[j]);
7723 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7724 isl_int_set(mat->row[i][pos],
7725 bmap->eq[i][off + k]);
7733 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
7734 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
7735 enum isl_dim_type c2, enum isl_dim_type c3,
7736 enum isl_dim_type c4, enum isl_dim_type c5)
7738 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
7739 struct isl_mat *mat;
7745 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
7746 isl_basic_map_total_dim(bmap) + 1);
7749 for (i = 0; i < bmap->n_ineq; ++i)
7750 for (j = 0, pos = 0; j < 5; ++j) {
7751 int off = isl_basic_map_offset(bmap, c[j]);
7752 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7753 isl_int_set(mat->row[i][pos],
7754 bmap->ineq[i][off + k]);
7762 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
7763 __isl_take isl_dim *dim,
7764 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
7765 enum isl_dim_type c2, enum isl_dim_type c3,
7766 enum isl_dim_type c4, enum isl_dim_type c5)
7768 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
7769 isl_basic_map *bmap;
7775 if (!dim || !eq || !ineq)
7778 if (eq->n_col != ineq->n_col)
7779 isl_die(dim->ctx, isl_error_invalid,
7780 "equalities and inequalities matrices should have "
7781 "same number of columns", goto error);
7783 total = 1 + isl_dim_total(dim);
7785 if (eq->n_col < total)
7786 isl_die(dim->ctx, isl_error_invalid,
7787 "number of columns too small", goto error);
7789 extra = eq->n_col - total;
7791 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
7792 eq->n_row, ineq->n_row);
7795 for (i = 0; i < extra; ++i)
7796 if (isl_basic_map_alloc_div(bmap) < 0)
7798 for (i = 0; i < eq->n_row; ++i) {
7799 l = isl_basic_map_alloc_equality(bmap);
7802 for (j = 0, pos = 0; j < 5; ++j) {
7803 int off = isl_basic_map_offset(bmap, c[j]);
7804 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7805 isl_int_set(bmap->eq[l][off + k],
7811 for (i = 0; i < ineq->n_row; ++i) {
7812 l = isl_basic_map_alloc_inequality(bmap);
7815 for (j = 0, pos = 0; j < 5; ++j) {
7816 int off = isl_basic_map_offset(bmap, c[j]);
7817 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7818 isl_int_set(bmap->ineq[l][off + k],
7837 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
7838 __isl_take isl_dim *dim,
7839 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
7840 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
7842 return (isl_basic_set*)
7843 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
7844 c1, c2, c3, c4, isl_dim_in);