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 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
333 enum isl_dim_type type, const char *s)
337 map = isl_map_cow(map);
341 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
345 for (i = 0; i < map->n; ++i) {
346 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
357 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
360 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
363 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
364 __isl_take isl_basic_map *bmap,
365 enum isl_dim_type type, unsigned pos, const char *s)
369 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
374 isl_basic_map_free(bmap);
378 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
379 enum isl_dim_type type, unsigned pos, const char *s)
386 map->dim = isl_dim_set_name(map->dim, type, pos, s);
390 for (i = 0; i < map->n; ++i) {
391 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
402 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
403 __isl_take isl_basic_set *bset,
404 enum isl_dim_type type, unsigned pos, const char *s)
406 return (isl_basic_set *)isl_basic_map_set_dim_name(
407 (isl_basic_map *)bset, type, pos, s);
410 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
411 enum isl_dim_type type, unsigned pos, const char *s)
413 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
416 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
420 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
423 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
424 struct isl_basic_map *bmap, unsigned extra,
425 unsigned n_eq, unsigned n_ineq)
428 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
433 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
434 if (isl_blk_is_error(bmap->block))
437 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
442 bmap->block2 = isl_blk_empty();
445 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
446 if (isl_blk_is_error(bmap->block2))
449 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
454 for (i = 0; i < n_ineq + n_eq; ++i)
455 bmap->ineq[i] = bmap->block.data + i * row_size;
457 for (i = 0; i < extra; ++i)
458 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
462 bmap->c_size = n_eq + n_ineq;
463 bmap->eq = bmap->ineq + n_ineq;
472 isl_basic_map_free(bmap);
476 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
477 unsigned nparam, unsigned dim, unsigned extra,
478 unsigned n_eq, unsigned n_ineq)
480 struct isl_basic_map *bmap;
481 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
482 return (struct isl_basic_set *)bmap;
485 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
486 unsigned extra, unsigned n_eq, unsigned n_ineq)
488 struct isl_basic_map *bmap;
491 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
492 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
493 return (struct isl_basic_set *)bmap;
496 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
497 unsigned extra, unsigned n_eq, unsigned n_ineq)
499 struct isl_basic_map *bmap;
503 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
508 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
514 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
515 unsigned nparam, unsigned in, unsigned out, unsigned extra,
516 unsigned n_eq, unsigned n_ineq)
518 struct isl_basic_map *bmap;
521 dim = isl_dim_alloc(ctx, nparam, in, out);
525 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
529 static void dup_constraints(
530 struct isl_basic_map *dst, struct isl_basic_map *src)
533 unsigned total = isl_basic_map_total_dim(src);
535 for (i = 0; i < src->n_eq; ++i) {
536 int j = isl_basic_map_alloc_equality(dst);
537 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
540 for (i = 0; i < src->n_ineq; ++i) {
541 int j = isl_basic_map_alloc_inequality(dst);
542 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
545 for (i = 0; i < src->n_div; ++i) {
546 int j = isl_basic_map_alloc_div(dst);
547 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
549 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
552 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
554 struct isl_basic_map *dup;
558 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
559 bmap->n_div, bmap->n_eq, bmap->n_ineq);
562 dup_constraints(dup, bmap);
563 dup->flags = bmap->flags;
564 dup->sample = isl_vec_copy(bmap->sample);
568 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
570 struct isl_basic_map *dup;
572 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
573 return (struct isl_basic_set *)dup;
576 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
581 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
585 return isl_basic_set_dup(bset);
588 struct isl_set *isl_set_copy(struct isl_set *set)
597 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
602 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
606 bmap = isl_basic_map_dup(bmap);
608 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
612 struct isl_map *isl_map_copy(struct isl_map *map)
621 void isl_basic_map_free(struct isl_basic_map *bmap)
629 isl_ctx_deref(bmap->ctx);
631 isl_blk_free(bmap->ctx, bmap->block2);
633 isl_blk_free(bmap->ctx, bmap->block);
634 isl_vec_free(bmap->sample);
635 isl_dim_free(bmap->dim);
639 void isl_basic_set_free(struct isl_basic_set *bset)
641 isl_basic_map_free((struct isl_basic_map *)bset);
644 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
646 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
649 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
655 isl_assert(ctx, room_for_con(bmap, 1), return -1);
656 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
658 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
659 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
660 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
661 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
662 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
663 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
665 int j = isl_basic_map_alloc_inequality(bmap);
669 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
670 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
677 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
678 bmap->extra - bmap->n_div);
682 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
684 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
687 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
691 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
696 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
698 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
701 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
706 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
708 if (pos != bmap->n_eq - 1) {
710 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
711 bmap->eq[bmap->n_eq - 1] = t;
717 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
719 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
722 void isl_basic_map_inequality_to_equality(
723 struct isl_basic_map *bmap, unsigned pos)
728 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
729 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
734 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
735 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
736 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
737 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
740 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
742 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
745 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
751 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
752 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
753 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
754 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
755 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
756 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
757 1 + isl_basic_map_total_dim(bmap),
758 bmap->extra - bmap->n_div);
759 return bmap->n_ineq++;
762 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
764 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
767 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
771 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
776 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
778 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
781 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
786 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
788 if (pos != bmap->n_ineq - 1) {
790 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
791 bmap->ineq[bmap->n_ineq - 1] = t;
792 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
798 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
800 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
803 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
808 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
811 k = isl_basic_map_alloc_equality(bmap);
814 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
817 isl_basic_map_free(bmap);
821 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
824 return (isl_basic_set *)
825 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
828 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
833 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
836 k = isl_basic_map_alloc_inequality(bmap);
839 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
842 isl_basic_map_free(bmap);
846 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
849 return (isl_basic_set *)
850 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
853 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
857 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
858 isl_seq_clr(bmap->div[bmap->n_div] +
859 1 + 1 + isl_basic_map_total_dim(bmap),
860 bmap->extra - bmap->n_div);
861 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
862 return bmap->n_div++;
865 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
867 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
870 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
874 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
879 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
881 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
884 /* Copy constraint from src to dst, putting the vars of src at offset
885 * dim_off in dst and the divs of src at offset div_off in dst.
886 * If both sets are actually map, then dim_off applies to the input
889 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
890 struct isl_basic_map *src_map, isl_int *src,
891 unsigned in_off, unsigned out_off, unsigned div_off)
893 unsigned src_nparam = isl_basic_map_n_param(src_map);
894 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
895 unsigned src_in = isl_basic_map_n_in(src_map);
896 unsigned dst_in = isl_basic_map_n_in(dst_map);
897 unsigned src_out = isl_basic_map_n_out(src_map);
898 unsigned dst_out = isl_basic_map_n_out(dst_map);
899 isl_int_set(dst[0], src[0]);
900 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
901 if (dst_nparam > src_nparam)
902 isl_seq_clr(dst+1+src_nparam,
903 dst_nparam - src_nparam);
904 isl_seq_clr(dst+1+dst_nparam, in_off);
905 isl_seq_cpy(dst+1+dst_nparam+in_off,
907 isl_min(dst_in-in_off, src_in));
908 if (dst_in-in_off > src_in)
909 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
910 dst_in - in_off - src_in);
911 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
912 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
913 src+1+src_nparam+src_in,
914 isl_min(dst_out-out_off, src_out));
915 if (dst_out-out_off > src_out)
916 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
917 dst_out - out_off - src_out);
918 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
919 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
920 src+1+src_nparam+src_in+src_out,
921 isl_min(dst_map->extra-div_off, src_map->n_div));
922 if (dst_map->n_div-div_off > src_map->n_div)
923 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
924 div_off+src_map->n_div,
925 dst_map->n_div - div_off - src_map->n_div);
928 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
929 struct isl_basic_map *src_map, isl_int *src,
930 unsigned in_off, unsigned out_off, unsigned div_off)
932 isl_int_set(dst[0], src[0]);
933 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
936 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
937 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
942 if (!bmap1 || !bmap2)
945 div_off = bmap1->n_div;
947 for (i = 0; i < bmap2->n_eq; ++i) {
948 int i1 = isl_basic_map_alloc_equality(bmap1);
951 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
952 i_pos, o_pos, div_off);
955 for (i = 0; i < bmap2->n_ineq; ++i) {
956 int i1 = isl_basic_map_alloc_inequality(bmap1);
959 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
960 i_pos, o_pos, div_off);
963 for (i = 0; i < bmap2->n_div; ++i) {
964 int i1 = isl_basic_map_alloc_div(bmap1);
967 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
968 i_pos, o_pos, div_off);
971 isl_basic_map_free(bmap2);
976 isl_basic_map_free(bmap1);
977 isl_basic_map_free(bmap2);
981 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
982 struct isl_dim_map *dim_map)
986 for (i = 0; i < dim_map->len; ++i) {
987 if (dim_map->pos[i] < 0)
988 isl_int_set_si(dst[i], 0);
990 isl_int_set(dst[i], src[dim_map->pos[i]]);
994 static void copy_div_dim_map(isl_int *dst, isl_int *src,
995 struct isl_dim_map *dim_map)
997 isl_int_set(dst[0], src[0]);
998 copy_constraint_dim_map(dst+1, src+1, dim_map);
1001 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
1002 struct isl_basic_map *src, struct isl_dim_map *dim_map)
1006 if (!src || !dst || !dim_map)
1009 for (i = 0; i < src->n_eq; ++i) {
1010 int i1 = isl_basic_map_alloc_equality(dst);
1013 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
1016 for (i = 0; i < src->n_ineq; ++i) {
1017 int i1 = isl_basic_map_alloc_inequality(dst);
1020 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
1023 for (i = 0; i < src->n_div; ++i) {
1024 int i1 = isl_basic_map_alloc_div(dst);
1027 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
1031 isl_basic_map_free(src);
1036 isl_basic_map_free(src);
1037 isl_basic_map_free(dst);
1041 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1042 struct isl_basic_set *bset2, unsigned pos)
1044 return (struct isl_basic_set *)
1045 add_constraints((struct isl_basic_map *)bset1,
1046 (struct isl_basic_map *)bset2, 0, pos);
1049 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1050 struct isl_dim *dim, unsigned extra,
1051 unsigned n_eq, unsigned n_ineq)
1053 struct isl_basic_map *ext;
1063 dims_ok = isl_dim_equal(base->dim, dim) &&
1064 base->extra >= base->n_div + extra;
1066 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1067 room_for_ineq(base, n_ineq)) {
1072 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1073 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1074 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1075 extra += base->extra;
1077 n_ineq += base->n_ineq;
1079 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1085 ext->sample = isl_vec_copy(base->sample);
1086 flags = base->flags;
1087 ext = add_constraints(ext, base, 0, 0);
1090 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1097 isl_basic_map_free(base);
1101 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1102 struct isl_dim *dim, unsigned extra,
1103 unsigned n_eq, unsigned n_ineq)
1105 return (struct isl_basic_set *)
1106 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1107 extra, n_eq, n_ineq);
1110 struct isl_basic_map *isl_basic_map_extend_constraints(
1111 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1115 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1119 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1120 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1121 unsigned n_eq, unsigned n_ineq)
1123 struct isl_basic_map *bmap;
1124 struct isl_dim *dim;
1128 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1132 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1135 isl_basic_map_free(base);
1139 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1140 unsigned nparam, unsigned dim, unsigned extra,
1141 unsigned n_eq, unsigned n_ineq)
1143 return (struct isl_basic_set *)
1144 isl_basic_map_extend((struct isl_basic_map *)base,
1145 nparam, 0, dim, extra, n_eq, n_ineq);
1148 struct isl_basic_set *isl_basic_set_extend_constraints(
1149 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1151 return (struct isl_basic_set *)
1152 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1156 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1158 return (struct isl_basic_set *)
1159 isl_basic_map_cow((struct isl_basic_map *)bset);
1162 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1167 if (bmap->ref > 1) {
1169 bmap = isl_basic_map_dup(bmap);
1172 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1176 struct isl_set *isl_set_cow(struct isl_set *set)
1184 return isl_set_dup(set);
1187 struct isl_map *isl_map_cow(struct isl_map *map)
1195 return isl_map_dup(map);
1198 static void swap_vars(struct isl_blk blk, isl_int *a,
1199 unsigned a_len, unsigned b_len)
1201 isl_seq_cpy(blk.data, a+a_len, b_len);
1202 isl_seq_cpy(blk.data+b_len, a, a_len);
1203 isl_seq_cpy(a, blk.data, b_len+a_len);
1206 struct isl_basic_set *isl_basic_set_swap_vars(
1207 struct isl_basic_set *bset, unsigned n)
1217 nparam = isl_basic_set_n_param(bset);
1218 dim = isl_basic_set_n_dim(bset);
1219 isl_assert(bset->ctx, n <= dim, goto error);
1224 bset = isl_basic_set_cow(bset);
1228 blk = isl_blk_alloc(bset->ctx, dim);
1229 if (isl_blk_is_error(blk))
1232 for (i = 0; i < bset->n_eq; ++i)
1234 bset->eq[i]+1+nparam, n, dim - n);
1236 for (i = 0; i < bset->n_ineq; ++i)
1238 bset->ineq[i]+1+nparam, n, dim - n);
1240 for (i = 0; i < bset->n_div; ++i)
1242 bset->div[i]+1+1+nparam, n, dim - n);
1244 isl_blk_free(bset->ctx, blk);
1246 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1247 bset = isl_basic_set_gauss(bset, NULL);
1248 return isl_basic_set_finalize(bset);
1250 isl_basic_set_free(bset);
1254 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1257 set = isl_set_cow(set);
1261 for (i = 0; i < set->n; ++i) {
1262 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1268 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1272 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1278 total = isl_basic_map_total_dim(bmap);
1279 isl_basic_map_free_div(bmap, bmap->n_div);
1280 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1282 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1284 i = isl_basic_map_alloc_equality(bmap);
1288 isl_int_set_si(bmap->eq[i][0], 1);
1289 isl_seq_clr(bmap->eq[i]+1, total);
1290 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1291 isl_vec_free(bmap->sample);
1292 bmap->sample = NULL;
1293 return isl_basic_map_finalize(bmap);
1295 isl_basic_map_free(bmap);
1299 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1301 return (struct isl_basic_set *)
1302 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1305 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1308 unsigned off = isl_dim_total(bmap->dim);
1309 isl_int *t = bmap->div[a];
1310 bmap->div[a] = bmap->div[b];
1313 for (i = 0; i < bmap->n_eq; ++i)
1314 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1316 for (i = 0; i < bmap->n_ineq; ++i)
1317 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1319 for (i = 0; i < bmap->n_div; ++i)
1320 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1321 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1324 /* Eliminate the specified n dimensions starting at first from the
1325 * constraints using Fourier-Motzkin. The dimensions themselves
1328 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1329 enum isl_dim_type type, unsigned first, unsigned n)
1338 map = isl_map_cow(map);
1341 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1342 first += pos(map->dim, type) - 1;
1344 for (i = 0; i < map->n; ++i) {
1345 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1355 /* Eliminate the specified n dimensions starting at first from the
1356 * constraints using Fourier-Motzkin. The dimensions themselves
1359 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1360 enum isl_dim_type type, unsigned first, unsigned n)
1362 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1365 /* Eliminate the specified n dimensions starting at first from the
1366 * constraints using Fourier-Motzkin. The dimensions themselves
1369 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1370 unsigned first, unsigned n)
1372 return isl_set_eliminate(set, isl_dim_set, first, n);
1375 /* Project out n dimensions starting at first using Fourier-Motzkin */
1376 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1377 unsigned first, unsigned n)
1379 set = isl_set_eliminate_dims(set, first, n);
1380 set = isl_set_drop_dims(set, first, n);
1384 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1386 bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1394 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1403 set = isl_set_cow(set);
1407 for (i = 0; i < set->n; ++i) {
1408 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1418 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1419 enum isl_dim_type type, unsigned first, unsigned n)
1423 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1427 bmap = isl_basic_map_eliminate_vars(bmap,
1428 isl_basic_map_offset(bmap, type) - 1 + first, n);
1431 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1433 bmap = isl_basic_map_drop(bmap, type, first, n);
1436 isl_basic_map_free(bmap);
1440 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1441 enum isl_dim_type type, unsigned first, unsigned n)
1443 return (isl_basic_set *)
1444 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1447 struct isl_map *isl_map_remove(struct isl_map *map,
1448 enum isl_dim_type type, unsigned first, unsigned n)
1455 map = isl_map_cow(map);
1458 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1460 for (i = 0; i < map->n; ++i) {
1461 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1462 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1466 map = isl_map_drop(map, type, first, n);
1473 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1474 enum isl_dim_type type, unsigned first, unsigned n)
1476 return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1479 /* Project out n inputs starting at first using Fourier-Motzkin */
1480 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1481 unsigned first, unsigned n)
1483 return isl_map_remove(map, isl_dim_in, first, n);
1486 /* Project out n dimensions starting at first using Fourier-Motzkin */
1487 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1488 unsigned first, unsigned n)
1490 unsigned nparam = isl_basic_set_n_param(bset);
1491 bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1492 bset = isl_basic_set_drop_dims(bset, first, n);
1496 static void dump_term(struct isl_basic_map *bmap,
1497 isl_int c, int pos, FILE *out)
1500 unsigned in = isl_basic_map_n_in(bmap);
1501 unsigned dim = in + isl_basic_map_n_out(bmap);
1502 unsigned nparam = isl_basic_map_n_param(bmap);
1504 isl_int_print(out, c, 0);
1506 if (!isl_int_is_one(c))
1507 isl_int_print(out, c, 0);
1508 if (pos < 1 + nparam) {
1509 name = isl_dim_get_name(bmap->dim,
1510 isl_dim_param, pos - 1);
1512 fprintf(out, "%s", name);
1514 fprintf(out, "p%d", pos - 1);
1515 } else if (pos < 1 + nparam + in)
1516 fprintf(out, "i%d", pos - 1 - nparam);
1517 else if (pos < 1 + nparam + dim)
1518 fprintf(out, "o%d", pos - 1 - nparam - in);
1520 fprintf(out, "e%d", pos - 1 - nparam - dim);
1524 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1525 int sign, FILE *out)
1529 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1533 for (i = 0, first = 1; i < len; ++i) {
1534 if (isl_int_sgn(c[i]) * sign <= 0)
1537 fprintf(out, " + ");
1539 isl_int_abs(v, c[i]);
1540 dump_term(bmap, v, i, out);
1547 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1548 const char *op, FILE *out, int indent)
1552 fprintf(out, "%*s", indent, "");
1554 dump_constraint_sign(bmap, c, 1, out);
1555 fprintf(out, " %s ", op);
1556 dump_constraint_sign(bmap, c, -1, out);
1560 for (i = bmap->n_div; i < bmap->extra; ++i) {
1561 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1563 fprintf(out, "%*s", indent, "");
1564 fprintf(out, "ERROR: unused div coefficient not zero\n");
1569 static void dump_constraints(struct isl_basic_map *bmap,
1570 isl_int **c, unsigned n,
1571 const char *op, FILE *out, int indent)
1575 for (i = 0; i < n; ++i)
1576 dump_constraint(bmap, c[i], op, out, indent);
1579 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1583 unsigned total = isl_basic_map_total_dim(bmap);
1585 for (j = 0; j < 1 + total; ++j) {
1586 if (isl_int_is_zero(exp[j]))
1588 if (!first && isl_int_is_pos(exp[j]))
1590 dump_term(bmap, exp[j], j, out);
1595 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1599 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1600 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1602 for (i = 0; i < bmap->n_div; ++i) {
1603 fprintf(out, "%*s", indent, "");
1604 fprintf(out, "e%d = [(", i);
1605 dump_affine(bmap, bmap->div[i]+1, out);
1607 isl_int_print(out, bmap->div[i][0], 0);
1608 fprintf(out, "]\n");
1612 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1615 fprintf(out, "null basic set\n");
1619 fprintf(out, "%*s", indent, "");
1620 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1621 bset->ref, bset->dim->nparam, bset->dim->n_out,
1622 bset->extra, bset->flags);
1623 dump((struct isl_basic_map *)bset, out, indent);
1626 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1629 fprintf(out, "null basic map\n");
1633 fprintf(out, "%*s", indent, "");
1634 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1635 "flags: %x, n_name: %d\n",
1637 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1638 bmap->extra, bmap->flags, bmap->dim->n_name);
1639 dump(bmap, out, indent);
1642 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1647 total = isl_basic_map_total_dim(bmap);
1648 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1649 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1650 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1651 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1655 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1657 struct isl_set *set;
1661 isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1662 isl_assert(dim->ctx, n >= 0, return NULL);
1663 set = isl_alloc(dim->ctx, struct isl_set,
1664 sizeof(struct isl_set) +
1665 (n - 1) * sizeof(struct isl_basic_set *));
1669 set->ctx = dim->ctx;
1670 isl_ctx_ref(set->ctx);
1682 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1683 unsigned nparam, unsigned dim, int n, unsigned flags)
1685 struct isl_set *set;
1686 struct isl_dim *dims;
1688 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1692 set = isl_set_alloc_dim(dims, n, flags);
1696 /* Make sure "map" has room for at least "n" more basic maps.
1698 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1701 struct isl_map *grown = NULL;
1705 isl_assert(map->ctx, n >= 0, goto error);
1706 if (map->n + n <= map->size)
1708 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1711 for (i = 0; i < map->n; ++i) {
1712 grown->p[i] = isl_basic_map_copy(map->p[i]);
1720 isl_map_free(grown);
1725 /* Make sure "set" has room for at least "n" more basic sets.
1727 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1729 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1732 struct isl_set *isl_set_dup(struct isl_set *set)
1735 struct isl_set *dup;
1740 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1743 for (i = 0; i < set->n; ++i)
1744 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1748 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1750 struct isl_set *set;
1755 set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1757 isl_basic_set_free(bset);
1760 return isl_set_add_basic_set(set, bset);
1763 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1765 struct isl_map *map;
1770 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1772 isl_basic_map_free(bmap);
1775 return isl_map_add_basic_map(map, bmap);
1778 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1779 __isl_take isl_basic_set *bset)
1781 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1782 (struct isl_basic_map *)bset);
1785 void isl_set_free(struct isl_set *set)
1795 isl_ctx_deref(set->ctx);
1796 for (i = 0; i < set->n; ++i)
1797 isl_basic_set_free(set->p[i]);
1798 isl_dim_free(set->dim);
1802 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1807 fprintf(out, "null set\n");
1811 fprintf(out, "%*s", indent, "");
1812 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1813 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1815 for (i = 0; i < set->n; ++i) {
1816 fprintf(out, "%*s", indent, "");
1817 fprintf(out, "basic set %d:\n", i);
1818 isl_basic_set_dump(set->p[i], out, indent+4);
1822 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1827 fprintf(out, "null map\n");
1831 fprintf(out, "%*s", indent, "");
1832 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1833 "flags: %x, n_name: %d\n",
1834 map->ref, map->n, map->dim->nparam, map->dim->n_in,
1835 map->dim->n_out, map->flags, map->dim->n_name);
1836 for (i = 0; i < map->n; ++i) {
1837 fprintf(out, "%*s", indent, "");
1838 fprintf(out, "basic map %d:\n", i);
1839 isl_basic_map_dump(map->p[i], out, indent+4);
1843 struct isl_basic_map *isl_basic_map_intersect_domain(
1844 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1846 struct isl_basic_map *bmap_domain;
1847 struct isl_dim *dim;
1852 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1853 bset->dim, isl_dim_param), goto error);
1855 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1856 isl_assert(bset->ctx,
1857 isl_basic_map_compatible_domain(bmap, bset), goto error);
1859 bmap = isl_basic_map_cow(bmap);
1862 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1863 bset->n_div, bset->n_eq, bset->n_ineq);
1864 dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1865 bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1866 bmap = add_constraints(bmap, bmap_domain, 0, 0);
1868 bmap = isl_basic_map_simplify(bmap);
1869 return isl_basic_map_finalize(bmap);
1871 isl_basic_map_free(bmap);
1872 isl_basic_set_free(bset);
1876 struct isl_basic_map *isl_basic_map_intersect_range(
1877 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1879 struct isl_basic_map *bmap_range;
1884 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1885 bset->dim, isl_dim_param), goto error);
1887 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1888 isl_assert(bset->ctx,
1889 isl_basic_map_compatible_range(bmap, bset), goto error);
1891 bmap = isl_basic_map_cow(bmap);
1894 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1895 bset->n_div, bset->n_eq, bset->n_ineq);
1896 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1897 bmap = add_constraints(bmap, bmap_range, 0, 0);
1899 bmap = isl_basic_map_simplify(bmap);
1900 return isl_basic_map_finalize(bmap);
1902 isl_basic_map_free(bmap);
1903 isl_basic_set_free(bset);
1907 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1913 total = 1 + isl_basic_map_total_dim(bmap);
1914 if (total != vec->size)
1919 for (i = 0; i < bmap->n_eq; ++i) {
1920 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1921 if (!isl_int_is_zero(s)) {
1927 for (i = 0; i < bmap->n_ineq; ++i) {
1928 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1929 if (isl_int_is_neg(s)) {
1940 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1942 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1945 struct isl_basic_map *isl_basic_map_intersect(
1946 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1948 struct isl_vec *sample = NULL;
1950 if (!bmap1 || !bmap2)
1953 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1954 bmap2->dim, isl_dim_param), goto error);
1955 if (isl_dim_total(bmap1->dim) ==
1956 isl_dim_size(bmap1->dim, isl_dim_param) &&
1957 isl_dim_total(bmap2->dim) !=
1958 isl_dim_size(bmap2->dim, isl_dim_param))
1959 return isl_basic_map_intersect(bmap2, bmap1);
1961 if (isl_dim_total(bmap2->dim) !=
1962 isl_dim_size(bmap2->dim, isl_dim_param))
1963 isl_assert(bmap1->ctx,
1964 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1966 if (bmap1->sample &&
1967 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1968 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1969 sample = isl_vec_copy(bmap1->sample);
1970 else if (bmap2->sample &&
1971 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1972 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1973 sample = isl_vec_copy(bmap2->sample);
1975 bmap1 = isl_basic_map_cow(bmap1);
1978 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1979 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1980 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1983 isl_vec_free(sample);
1985 isl_vec_free(bmap1->sample);
1986 bmap1->sample = sample;
1989 bmap1 = isl_basic_map_simplify(bmap1);
1990 return isl_basic_map_finalize(bmap1);
1993 isl_vec_free(sample);
1994 isl_basic_map_free(bmap1);
1995 isl_basic_map_free(bmap2);
1999 struct isl_basic_set *isl_basic_set_intersect(
2000 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2002 return (struct isl_basic_set *)
2003 isl_basic_map_intersect(
2004 (struct isl_basic_map *)bset1,
2005 (struct isl_basic_map *)bset2);
2008 /* Special case of isl_map_intersect, where both map1 and map2
2009 * are convex, without any divs and such that either map1 or map2
2010 * contains a single constraint. This constraint is then simply
2011 * added to the other map.
2013 static __isl_give isl_map *map_intersect_add_constraint(
2014 __isl_take isl_map *map1, __isl_take isl_map *map2)
2016 struct isl_basic_map *bmap1;
2017 struct isl_basic_map *bmap2;
2019 isl_assert(map1->ctx, map1->n == 1, goto error);
2020 isl_assert(map2->ctx, map1->n == 1, goto error);
2021 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2022 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2024 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2025 return isl_map_intersect(map2, map1);
2027 isl_assert(map2->ctx,
2028 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2030 map1 = isl_map_cow(map1);
2033 if (isl_map_fast_is_empty(map1)) {
2037 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2038 if (map2->p[0]->n_eq == 1)
2039 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2041 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2042 map2->p[0]->ineq[0]);
2044 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2045 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2049 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2050 isl_basic_map_free(map1->p[0]);
2063 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2066 struct isl_map *result;
2072 if (isl_map_fast_is_empty(map1)) {
2076 if (isl_map_fast_is_empty(map2)) {
2081 if (map1->n == 1 && map2->n == 1 &&
2082 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2083 isl_dim_equal(map1->dim, map2->dim) &&
2084 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2085 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2086 return map_intersect_add_constraint(map1, map2);
2087 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2088 map2->dim, isl_dim_param), goto error);
2089 if (isl_dim_total(map1->dim) ==
2090 isl_dim_size(map1->dim, isl_dim_param) &&
2091 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2092 return isl_map_intersect(map2, map1);
2094 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2095 isl_assert(map1->ctx,
2096 isl_dim_equal(map1->dim, map2->dim), goto error);
2098 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2099 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2100 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2102 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2103 map1->n * map2->n, flags);
2106 for (i = 0; i < map1->n; ++i)
2107 for (j = 0; j < map2->n; ++j) {
2108 struct isl_basic_map *part;
2109 part = isl_basic_map_intersect(
2110 isl_basic_map_copy(map1->p[i]),
2111 isl_basic_map_copy(map2->p[j]));
2112 if (isl_basic_map_is_empty(part))
2113 isl_basic_map_free(part);
2115 result = isl_map_add_basic_map(result, part);
2128 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2130 return (struct isl_set *)
2131 isl_map_intersect((struct isl_map *)set1,
2132 (struct isl_map *)set2);
2135 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2137 struct isl_dim *dim;
2138 struct isl_basic_set *bset;
2143 bmap = isl_basic_map_cow(bmap);
2146 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2147 in = isl_basic_map_n_in(bmap);
2148 bset = isl_basic_set_from_basic_map(bmap);
2149 bset = isl_basic_set_swap_vars(bset, in);
2150 return isl_basic_map_from_basic_set(bset, dim);
2153 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2154 enum isl_dim_type type, unsigned pos, unsigned n)
2156 struct isl_dim *res_dim;
2157 struct isl_basic_map *res;
2158 struct isl_dim_map *dim_map;
2159 unsigned total, off;
2160 enum isl_dim_type t;
2168 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2170 total = isl_basic_map_total_dim(bmap) + n;
2171 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2173 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2175 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2177 unsigned size = isl_basic_map_dim(bmap, t);
2178 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2180 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2181 pos, size - pos, off + pos + n);
2183 off += isl_dim_size(res_dim, t);
2185 isl_dim_map_div(dim_map, bmap, off);
2187 res = isl_basic_map_alloc_dim(res_dim,
2188 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2189 res = add_constraints_dim_map(res, bmap, dim_map);
2190 res = isl_basic_map_simplify(res);
2191 return isl_basic_map_finalize(res);
2194 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2195 enum isl_dim_type type, unsigned n)
2199 return isl_basic_map_insert(bmap, type,
2200 isl_basic_map_dim(bmap, type), n);
2203 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2204 enum isl_dim_type type, unsigned n)
2208 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2209 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2211 isl_basic_set_free(bset);
2215 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2216 enum isl_dim_type type, unsigned pos, unsigned n)
2223 map = isl_map_cow(map);
2227 map->dim = isl_dim_insert(map->dim, type, pos, n);
2231 for (i = 0; i < map->n; ++i) {
2232 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2243 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2244 enum isl_dim_type type, unsigned pos, unsigned n)
2246 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2249 __isl_give isl_map *isl_map_add(__isl_take isl_map *map,
2250 enum isl_dim_type type, unsigned n)
2254 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2257 __isl_give isl_set *isl_set_add(__isl_take isl_set *set,
2258 enum isl_dim_type type, unsigned n)
2262 isl_assert(set->ctx, type != isl_dim_in, goto error);
2263 return (isl_set *)isl_map_add((isl_map *)set, type, n);
2269 __isl_give isl_basic_map *isl_basic_map_move_dims(
2270 __isl_take isl_basic_map *bmap,
2271 enum isl_dim_type dst_type, unsigned dst_pos,
2272 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2275 struct isl_dim_map *dim_map;
2276 struct isl_basic_map *res;
2277 enum isl_dim_type t;
2278 unsigned total, off;
2285 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2288 if (dst_type == src_type && dst_pos == src_pos)
2291 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2293 if (pos(bmap->dim, dst_type) + dst_pos ==
2294 pos(bmap->dim, src_type) + src_pos +
2295 ((src_type < dst_type) ? n : 0)) {
2296 bmap = isl_basic_map_cow(bmap);
2300 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2301 src_type, src_pos, n);
2305 bmap = isl_basic_map_finalize(bmap);
2310 total = isl_basic_map_total_dim(bmap);
2311 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2314 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2315 unsigned size = isl_dim_size(bmap->dim, t);
2316 if (t == dst_type) {
2317 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2320 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2323 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2324 dst_pos, size - dst_pos, off);
2325 off += size - dst_pos;
2326 } else if (t == src_type) {
2327 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2330 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2331 src_pos + n, size - src_pos - n, off);
2332 off += size - src_pos - n;
2334 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2338 isl_dim_map_div(dim_map, bmap, off);
2340 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2341 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2342 bmap = add_constraints_dim_map(res, bmap, dim_map);
2344 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2345 src_type, src_pos, n);
2349 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2350 bmap = isl_basic_map_gauss(bmap, NULL);
2351 bmap = isl_basic_map_finalize(bmap);
2355 isl_basic_map_free(bmap);
2359 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2360 enum isl_dim_type dst_type, unsigned dst_pos,
2361 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2363 return (isl_basic_set *)isl_basic_map_move_dims(
2364 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2367 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2368 enum isl_dim_type dst_type, unsigned dst_pos,
2369 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2373 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2374 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2375 src_type, src_pos, n);
2381 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2382 enum isl_dim_type dst_type, unsigned dst_pos,
2383 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2392 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2395 if (dst_type == src_type && dst_pos == src_pos)
2398 isl_assert(map->ctx, dst_type != src_type, goto error);
2400 map = isl_map_cow(map);
2404 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2408 for (i = 0; i < map->n; ++i) {
2409 map->p[i] = isl_basic_map_move_dims(map->p[i],
2411 src_type, src_pos, n);
2422 /* Move the specified dimensions to the last columns right before
2423 * the divs. Don't change the dimension specification of bmap.
2424 * That's the responsibility of the caller.
2426 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2427 enum isl_dim_type type, unsigned first, unsigned n)
2429 struct isl_dim_map *dim_map;
2430 struct isl_basic_map *res;
2431 enum isl_dim_type t;
2432 unsigned total, off;
2436 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2439 total = isl_basic_map_total_dim(bmap);
2440 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2443 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2444 unsigned size = isl_dim_size(bmap->dim, t);
2446 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2449 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2450 first, n, total - bmap->n_div - n);
2451 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2452 first + n, size - (first + n), off);
2453 off += size - (first + n);
2455 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2459 isl_dim_map_div(dim_map, bmap, off + n);
2461 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2462 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2463 res = add_constraints_dim_map(res, bmap, dim_map);
2467 /* Turn the n dimensions of type type, starting at first
2468 * into existentially quantified variables.
2470 __isl_give isl_basic_map *isl_basic_map_project_out(
2471 __isl_take isl_basic_map *bmap,
2472 enum isl_dim_type type, unsigned first, unsigned n)
2485 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2486 return isl_basic_map_remove(bmap, type, first, n);
2488 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2491 bmap = move_last(bmap, type, first, n);
2492 bmap = isl_basic_map_cow(bmap);
2496 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2497 old = bmap->block2.data;
2498 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2499 (bmap->extra + n) * (1 + row_size));
2500 if (!bmap->block2.data)
2502 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2505 for (i = 0; i < n; ++i) {
2506 new_div[i] = bmap->block2.data +
2507 (bmap->extra + i) * (1 + row_size);
2508 isl_seq_clr(new_div[i], 1 + row_size);
2510 for (i = 0; i < bmap->extra; ++i)
2511 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2513 bmap->div = new_div;
2517 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2520 bmap = isl_basic_map_simplify(bmap);
2521 bmap = isl_basic_map_drop_redundant_divs(bmap);
2522 return isl_basic_map_finalize(bmap);
2524 isl_basic_map_free(bmap);
2528 /* Turn the n dimensions of type type, starting at first
2529 * into existentially quantified variables.
2531 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2532 enum isl_dim_type type, unsigned first, unsigned n)
2534 return (isl_basic_set *)isl_basic_map_project_out(
2535 (isl_basic_map *)bset, type, first, n);
2538 /* Turn the n dimensions of type type, starting at first
2539 * into existentially quantified variables.
2541 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2542 enum isl_dim_type type, unsigned first, unsigned n)
2552 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2554 map = isl_map_cow(map);
2558 map->dim = isl_dim_drop(map->dim, type, first, n);
2562 for (i = 0; i < map->n; ++i) {
2563 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2574 /* Turn the n dimensions of type type, starting at first
2575 * into existentially quantified variables.
2577 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2578 enum isl_dim_type type, unsigned first, unsigned n)
2580 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2583 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2587 for (i = 0; i < n; ++i) {
2588 j = isl_basic_map_alloc_div(bmap);
2591 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2595 isl_basic_map_free(bmap);
2599 struct isl_basic_map *isl_basic_map_apply_range(
2600 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2602 struct isl_dim *dim_result = NULL;
2603 struct isl_basic_map *bmap;
2604 unsigned n_in, n_out, n, nparam, total, pos;
2605 struct isl_dim_map *dim_map1, *dim_map2;
2607 if (!bmap1 || !bmap2)
2610 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2611 isl_dim_copy(bmap2->dim));
2613 n_in = isl_basic_map_n_in(bmap1);
2614 n_out = isl_basic_map_n_out(bmap2);
2615 n = isl_basic_map_n_out(bmap1);
2616 nparam = isl_basic_map_n_param(bmap1);
2618 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2619 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2620 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2621 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2622 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2623 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2624 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2625 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2626 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2627 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2628 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2630 bmap = isl_basic_map_alloc_dim(dim_result,
2631 bmap1->n_div + bmap2->n_div + n,
2632 bmap1->n_eq + bmap2->n_eq,
2633 bmap1->n_ineq + bmap2->n_ineq);
2634 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2635 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2636 bmap = add_divs(bmap, n);
2637 bmap = isl_basic_map_simplify(bmap);
2638 bmap = isl_basic_map_drop_redundant_divs(bmap);
2639 return isl_basic_map_finalize(bmap);
2641 isl_basic_map_free(bmap1);
2642 isl_basic_map_free(bmap2);
2646 struct isl_basic_set *isl_basic_set_apply(
2647 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2652 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2655 return (struct isl_basic_set *)
2656 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2658 isl_basic_set_free(bset);
2659 isl_basic_map_free(bmap);
2663 struct isl_basic_map *isl_basic_map_apply_domain(
2664 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2666 if (!bmap1 || !bmap2)
2669 isl_assert(bmap1->ctx,
2670 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2671 isl_assert(bmap1->ctx,
2672 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2675 bmap1 = isl_basic_map_reverse(bmap1);
2676 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2677 return isl_basic_map_reverse(bmap1);
2679 isl_basic_map_free(bmap1);
2680 isl_basic_map_free(bmap2);
2684 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2685 * A \cap B -> f(A) + f(B)
2687 struct isl_basic_map *isl_basic_map_sum(
2688 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2690 unsigned n_in, n_out, nparam, total, pos;
2691 struct isl_basic_map *bmap = NULL;
2692 struct isl_dim_map *dim_map1, *dim_map2;
2695 if (!bmap1 || !bmap2)
2698 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2701 nparam = isl_basic_map_n_param(bmap1);
2702 n_in = isl_basic_map_n_in(bmap1);
2703 n_out = isl_basic_map_n_out(bmap1);
2705 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2706 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2707 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2708 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2709 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2710 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2711 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2712 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2713 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2714 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2715 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2717 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2718 bmap1->n_div + bmap2->n_div + 2 * n_out,
2719 bmap1->n_eq + bmap2->n_eq + n_out,
2720 bmap1->n_ineq + bmap2->n_ineq);
2721 for (i = 0; i < n_out; ++i) {
2722 int j = isl_basic_map_alloc_equality(bmap);
2725 isl_seq_clr(bmap->eq[j], 1+total);
2726 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2727 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2728 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2730 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2731 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2732 bmap = add_divs(bmap, 2 * n_out);
2734 bmap = isl_basic_map_simplify(bmap);
2735 return isl_basic_map_finalize(bmap);
2737 isl_basic_map_free(bmap);
2738 isl_basic_map_free(bmap1);
2739 isl_basic_map_free(bmap2);
2743 /* Given two maps A -> f(A) and B -> g(B), construct a map
2744 * A \cap B -> f(A) + f(B)
2746 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2748 struct isl_map *result;
2754 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2756 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2757 map1->n * map2->n, 0);
2760 for (i = 0; i < map1->n; ++i)
2761 for (j = 0; j < map2->n; ++j) {
2762 struct isl_basic_map *part;
2763 part = isl_basic_map_sum(
2764 isl_basic_map_copy(map1->p[i]),
2765 isl_basic_map_copy(map2->p[j]));
2766 if (isl_basic_map_is_empty(part))
2767 isl_basic_map_free(part);
2769 result = isl_map_add_basic_map(result, part);
2782 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2783 __isl_take isl_set *set2)
2785 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2788 /* Given a basic map A -> f(A), construct A -> -f(A).
2790 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2795 bmap = isl_basic_map_cow(bmap);
2799 n = isl_basic_map_dim(bmap, isl_dim_out);
2800 off = isl_basic_map_offset(bmap, isl_dim_out);
2801 for (i = 0; i < bmap->n_eq; ++i)
2802 for (j = 0; j < n; ++j)
2803 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2804 for (i = 0; i < bmap->n_ineq; ++i)
2805 for (j = 0; j < n; ++j)
2806 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2807 for (i = 0; i < bmap->n_div; ++i)
2808 for (j = 0; j < n; ++j)
2809 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2810 return isl_basic_map_finalize(bmap);
2813 /* Given a map A -> f(A), construct A -> -f(A).
2815 struct isl_map *isl_map_neg(struct isl_map *map)
2819 map = isl_map_cow(map);
2823 for (i = 0; i < map->n; ++i) {
2824 map->p[i] = isl_basic_map_neg(map->p[i]);
2835 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
2837 return (isl_set *)isl_map_neg((isl_map *)set);
2840 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2841 * A -> floor(f(A)/d).
2843 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2846 unsigned n_in, n_out, nparam, total, pos;
2847 struct isl_basic_map *result = NULL;
2848 struct isl_dim_map *dim_map;
2854 nparam = isl_basic_map_n_param(bmap);
2855 n_in = isl_basic_map_n_in(bmap);
2856 n_out = isl_basic_map_n_out(bmap);
2858 total = nparam + n_in + n_out + bmap->n_div + n_out;
2859 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2860 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2861 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2862 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2863 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2865 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2866 bmap->n_div + n_out,
2867 bmap->n_eq, bmap->n_ineq + 2 * n_out);
2868 result = add_constraints_dim_map(result, bmap, dim_map);
2869 result = add_divs(result, n_out);
2870 for (i = 0; i < n_out; ++i) {
2872 j = isl_basic_map_alloc_inequality(result);
2875 isl_seq_clr(result->ineq[j], 1+total);
2876 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2877 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2878 j = isl_basic_map_alloc_inequality(result);
2881 isl_seq_clr(result->ineq[j], 1+total);
2882 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2883 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2884 isl_int_sub_ui(result->ineq[j][0], d, 1);
2887 result = isl_basic_map_simplify(result);
2888 return isl_basic_map_finalize(result);
2890 isl_basic_map_free(result);
2894 /* Given a map A -> f(A) and an integer d, construct a map
2895 * A -> floor(f(A)/d).
2897 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2901 map = isl_map_cow(map);
2905 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2906 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2907 for (i = 0; i < map->n; ++i) {
2908 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2919 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2925 i = isl_basic_map_alloc_equality(bmap);
2928 nparam = isl_basic_map_n_param(bmap);
2929 n_in = isl_basic_map_n_in(bmap);
2930 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2931 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2932 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2933 return isl_basic_map_finalize(bmap);
2935 isl_basic_map_free(bmap);
2939 /* Add a constraints to "bmap" expressing i_pos < o_pos
2941 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2947 i = isl_basic_map_alloc_inequality(bmap);
2950 nparam = isl_basic_map_n_param(bmap);
2951 n_in = isl_basic_map_n_in(bmap);
2952 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2953 isl_int_set_si(bmap->ineq[i][0], -1);
2954 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2955 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2956 return isl_basic_map_finalize(bmap);
2958 isl_basic_map_free(bmap);
2962 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2964 static __isl_give isl_basic_map *var_less_or_equal(
2965 __isl_take isl_basic_map *bmap, unsigned pos)
2971 i = isl_basic_map_alloc_inequality(bmap);
2974 nparam = isl_basic_map_n_param(bmap);
2975 n_in = isl_basic_map_n_in(bmap);
2976 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2977 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2978 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2979 return isl_basic_map_finalize(bmap);
2981 isl_basic_map_free(bmap);
2985 /* Add a constraints to "bmap" expressing i_pos > o_pos
2987 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2993 i = isl_basic_map_alloc_inequality(bmap);
2996 nparam = isl_basic_map_n_param(bmap);
2997 n_in = isl_basic_map_n_in(bmap);
2998 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2999 isl_int_set_si(bmap->ineq[i][0], -1);
3000 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3001 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3002 return isl_basic_map_finalize(bmap);
3004 isl_basic_map_free(bmap);
3008 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3010 static __isl_give isl_basic_map *var_more_or_equal(
3011 __isl_take isl_basic_map *bmap, unsigned pos)
3017 i = isl_basic_map_alloc_inequality(bmap);
3020 nparam = isl_basic_map_n_param(bmap);
3021 n_in = isl_basic_map_n_in(bmap);
3022 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3023 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3024 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3025 return isl_basic_map_finalize(bmap);
3027 isl_basic_map_free(bmap);
3031 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3034 struct isl_basic_map *bmap;
3035 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3038 for (i = 0; i < n_equal && bmap; ++i)
3039 bmap = var_equal(bmap, i);
3040 return isl_basic_map_finalize(bmap);
3043 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3045 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3048 struct isl_basic_map *bmap;
3049 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3052 for (i = 0; i < pos && bmap; ++i)
3053 bmap = var_equal(bmap, i);
3055 bmap = var_less(bmap, pos);
3056 return isl_basic_map_finalize(bmap);
3059 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3061 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3062 __isl_take isl_dim *dim, unsigned pos)
3065 isl_basic_map *bmap;
3067 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3068 for (i = 0; i < pos; ++i)
3069 bmap = var_equal(bmap, i);
3070 bmap = var_less_or_equal(bmap, pos);
3071 return isl_basic_map_finalize(bmap);
3074 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3076 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3079 struct isl_basic_map *bmap;
3080 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3083 for (i = 0; i < pos && bmap; ++i)
3084 bmap = var_equal(bmap, i);
3086 bmap = var_more(bmap, pos);
3087 return isl_basic_map_finalize(bmap);
3090 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3092 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3093 __isl_take isl_dim *dim, unsigned pos)
3096 isl_basic_map *bmap;
3098 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3099 for (i = 0; i < pos; ++i)
3100 bmap = var_equal(bmap, i);
3101 bmap = var_more_or_equal(bmap, pos);
3102 return isl_basic_map_finalize(bmap);
3105 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3106 unsigned n, int equal)
3108 struct isl_map *map;
3111 if (n == 0 && equal)
3112 return isl_map_universe(dims);
3114 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3116 for (i = 0; i + 1 < n; ++i)
3117 map = isl_map_add_basic_map(map,
3118 isl_basic_map_less_at(isl_dim_copy(dims), i));
3121 map = isl_map_add_basic_map(map,
3122 isl_basic_map_less_or_equal_at(dims, n - 1));
3124 map = isl_map_add_basic_map(map,
3125 isl_basic_map_less_at(dims, n - 1));
3132 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3136 return map_lex_lte_first(dims, dims->n_out, equal);
3139 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3141 return map_lex_lte_first(dim, n, 0);
3144 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3146 return map_lex_lte_first(dim, n, 1);
3149 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3151 return map_lex_lte(isl_dim_map(set_dim), 0);
3154 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3156 return map_lex_lte(isl_dim_map(set_dim), 1);
3159 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3160 unsigned n, int equal)
3162 struct isl_map *map;
3165 if (n == 0 && equal)
3166 return isl_map_universe(dims);
3168 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3170 for (i = 0; i + 1 < n; ++i)
3171 map = isl_map_add_basic_map(map,
3172 isl_basic_map_more_at(isl_dim_copy(dims), i));
3175 map = isl_map_add_basic_map(map,
3176 isl_basic_map_more_or_equal_at(dims, n - 1));
3178 map = isl_map_add_basic_map(map,
3179 isl_basic_map_more_at(dims, n - 1));
3186 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3190 return map_lex_gte_first(dims, dims->n_out, equal);
3193 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3195 return map_lex_gte_first(dim, n, 0);
3198 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3200 return map_lex_gte_first(dim, n, 1);
3203 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3205 return map_lex_gte(isl_dim_map(set_dim), 0);
3208 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3210 return map_lex_gte(isl_dim_map(set_dim), 1);
3213 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3214 __isl_take isl_set *set2)
3217 map = isl_map_lex_le(isl_set_get_dim(set1));
3218 map = isl_map_intersect_domain(map, set1);
3219 map = isl_map_intersect_range(map, set2);
3223 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3224 __isl_take isl_set *set2)
3227 map = isl_map_lex_lt(isl_set_get_dim(set1));
3228 map = isl_map_intersect_domain(map, set1);
3229 map = isl_map_intersect_range(map, set2);
3233 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3234 __isl_take isl_set *set2)
3237 map = isl_map_lex_ge(isl_set_get_dim(set1));
3238 map = isl_map_intersect_domain(map, set1);
3239 map = isl_map_intersect_range(map, set2);
3243 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3244 __isl_take isl_set *set2)
3247 map = isl_map_lex_gt(isl_set_get_dim(set1));
3248 map = isl_map_intersect_domain(map, set1);
3249 map = isl_map_intersect_range(map, set2);
3253 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3254 __isl_take isl_map *map2)
3257 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3258 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3259 map = isl_map_apply_range(map, isl_map_reverse(map2));
3263 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3264 __isl_take isl_map *map2)
3267 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3268 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3269 map = isl_map_apply_range(map, isl_map_reverse(map2));
3273 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3274 __isl_take isl_map *map2)
3277 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3278 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3279 map = isl_map_apply_range(map, isl_map_reverse(map2));
3283 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3284 __isl_take isl_map *map2)
3287 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3288 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3289 map = isl_map_apply_range(map, isl_map_reverse(map2));
3293 struct isl_basic_map *isl_basic_map_from_basic_set(
3294 struct isl_basic_set *bset, struct isl_dim *dim)
3296 struct isl_basic_map *bmap;
3298 bset = isl_basic_set_cow(bset);
3302 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3303 isl_dim_free(bset->dim);
3304 bmap = (struct isl_basic_map *) bset;
3306 return isl_basic_map_finalize(bmap);
3308 isl_basic_set_free(bset);
3313 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3317 if (bmap->dim->n_in == 0)
3318 return (struct isl_basic_set *)bmap;
3319 bmap = isl_basic_map_cow(bmap);
3322 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3325 bmap = isl_basic_map_finalize(bmap);
3326 return (struct isl_basic_set *)bmap;
3328 isl_basic_map_free(bmap);
3332 /* For a div d = floor(f/m), add the constraints
3335 * -(f-(n-1)) + m d >= 0
3337 * Note that the second constraint is the negation of
3341 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3342 unsigned pos, isl_int *div)
3345 unsigned total = isl_basic_map_total_dim(bmap);
3347 i = isl_basic_map_alloc_inequality(bmap);
3350 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3351 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3353 j = isl_basic_map_alloc_inequality(bmap);
3356 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3357 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3358 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3362 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3363 unsigned pos, isl_int *div)
3365 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3369 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3371 unsigned total = isl_basic_map_total_dim(bmap);
3372 unsigned div_pos = total - bmap->n_div + div;
3374 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3378 struct isl_basic_set *isl_basic_map_underlying_set(
3379 struct isl_basic_map *bmap)
3383 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3384 bmap->n_div == 0 && !isl_dim_get_tuple_name(bmap->dim, isl_dim_out))
3385 return (struct isl_basic_set *)bmap;
3386 bmap = isl_basic_map_cow(bmap);
3389 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3392 bmap->extra -= bmap->n_div;
3394 bmap = isl_basic_map_finalize(bmap);
3395 return (struct isl_basic_set *)bmap;
3400 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3401 __isl_take isl_basic_set *bset)
3403 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3406 struct isl_basic_map *isl_basic_map_overlying_set(
3407 struct isl_basic_set *bset, struct isl_basic_map *like)
3409 struct isl_basic_map *bmap;
3410 struct isl_ctx *ctx;
3417 isl_assert(ctx, bset->n_div == 0, goto error);
3418 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3419 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3421 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3422 isl_basic_map_free(like);
3423 return (struct isl_basic_map *)bset;
3425 bset = isl_basic_set_cow(bset);
3428 total = bset->dim->n_out + bset->extra;
3429 bmap = (struct isl_basic_map *)bset;
3430 isl_dim_free(bmap->dim);
3431 bmap->dim = isl_dim_copy(like->dim);
3434 bmap->n_div = like->n_div;
3435 bmap->extra += like->n_div;
3438 ltotal = total - bmap->extra + like->extra;
3441 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3442 bmap->extra * (1 + 1 + total));
3443 if (isl_blk_is_error(bmap->block2))
3445 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3449 for (i = 0; i < bmap->extra; ++i)
3450 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3451 for (i = 0; i < like->n_div; ++i) {
3452 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3453 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3455 bmap = isl_basic_map_extend_constraints(bmap,
3456 0, 2 * like->n_div);
3457 for (i = 0; i < like->n_div; ++i) {
3458 if (isl_int_is_zero(bmap->div[i][0]))
3460 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3464 isl_basic_map_free(like);
3465 bmap = isl_basic_map_simplify(bmap);
3466 bmap = isl_basic_map_finalize(bmap);
3469 isl_basic_map_free(like);
3470 isl_basic_set_free(bset);
3474 struct isl_basic_set *isl_basic_set_from_underlying_set(
3475 struct isl_basic_set *bset, struct isl_basic_set *like)
3477 return (struct isl_basic_set *)
3478 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3481 struct isl_set *isl_set_from_underlying_set(
3482 struct isl_set *set, struct isl_basic_set *like)
3488 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3490 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3491 isl_basic_set_free(like);
3494 set = isl_set_cow(set);
3497 for (i = 0; i < set->n; ++i) {
3498 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3499 isl_basic_set_copy(like));
3503 isl_dim_free(set->dim);
3504 set->dim = isl_dim_copy(like->dim);
3507 isl_basic_set_free(like);
3510 isl_basic_set_free(like);
3515 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3519 map = isl_map_cow(map);
3522 map->dim = isl_dim_cow(map->dim);
3526 for (i = 1; i < map->n; ++i)
3527 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3529 for (i = 0; i < map->n; ++i) {
3530 map->p[i] = (struct isl_basic_map *)
3531 isl_basic_map_underlying_set(map->p[i]);
3536 map->dim = isl_dim_underlying(map->dim, 0);
3538 isl_dim_free(map->dim);
3539 map->dim = isl_dim_copy(map->p[0]->dim);
3543 return (struct isl_set *)map;
3549 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3551 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3554 static __isl_give isl_basic_map *isl_basic_map_reset_dim(
3555 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3557 bmap = isl_basic_map_cow(bmap);
3561 isl_dim_free(bmap->dim);
3564 bmap = isl_basic_map_finalize(bmap);
3568 isl_basic_map_free(bmap);
3573 static __isl_give isl_basic_set *isl_basic_set_reset_dim(
3574 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3576 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3580 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3581 __isl_take isl_dim *dim)
3585 map = isl_map_cow(map);
3589 for (i = 0; i < map->n; ++i) {
3590 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3595 isl_dim_free(map->dim);
3605 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3606 __isl_take isl_dim *dim)
3608 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3611 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3614 struct isl_basic_set *domain;
3620 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3622 n_in = isl_basic_map_n_in(bmap);
3623 n_out = isl_basic_map_n_out(bmap);
3624 domain = isl_basic_set_from_basic_map(bmap);
3625 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3627 domain = isl_basic_set_reset_dim(domain, dim);
3632 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3634 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3637 __isl_give isl_basic_map *isl_basic_map_domain_map(
3638 __isl_take isl_basic_map *bmap)
3642 isl_basic_map *domain;
3643 isl_basic_set *bset;
3644 int nparam, n_in, n_out;
3647 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3648 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3649 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3651 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3652 domain = isl_basic_map_universe(dim);
3654 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3655 bmap = isl_basic_map_apply_range(bmap, domain);
3656 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3658 total = isl_basic_map_total_dim(bmap);
3660 for (i = 0; i < n_in; ++i) {
3661 k = isl_basic_map_alloc_equality(bmap);
3664 isl_seq_clr(bmap->eq[k], 1 + total);
3665 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3666 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3669 bmap = isl_basic_map_gauss(bmap, NULL);
3670 return isl_basic_map_finalize(bmap);
3672 isl_basic_map_free(bmap);
3676 __isl_give isl_basic_map *isl_basic_map_range_map(
3677 __isl_take isl_basic_map *bmap)
3681 isl_basic_map *range;
3682 isl_basic_set *bset;
3683 int nparam, n_in, n_out;
3686 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3687 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3688 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3690 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3691 range = isl_basic_map_universe(dim);
3693 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3694 bmap = isl_basic_map_apply_range(bmap, range);
3695 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3697 total = isl_basic_map_total_dim(bmap);
3699 for (i = 0; i < n_out; ++i) {
3700 k = isl_basic_map_alloc_equality(bmap);
3703 isl_seq_clr(bmap->eq[k], 1 + total);
3704 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3705 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3708 bmap = isl_basic_map_gauss(bmap, NULL);
3709 return isl_basic_map_finalize(bmap);
3711 isl_basic_map_free(bmap);
3715 struct isl_set *isl_map_range(struct isl_map *map)
3718 struct isl_set *set;
3722 if (isl_map_dim(map, isl_dim_in) == 0)
3723 return (isl_set *)map;
3725 map = isl_map_cow(map);
3729 set = (struct isl_set *) map;
3730 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3733 for (i = 0; i < map->n; ++i) {
3734 set->p[i] = isl_basic_map_range(map->p[i]);
3738 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3739 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3746 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3749 isl_dim *domain_dim;
3751 map = isl_map_cow(map);
3755 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3756 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3757 map->dim = isl_dim_join(map->dim, domain_dim);
3760 for (i = 0; i < map->n; ++i) {
3761 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3765 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3766 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3773 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3778 map = isl_map_cow(map);
3782 range_dim = isl_dim_range(isl_map_get_dim(map));
3783 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3784 map->dim = isl_dim_join(map->dim, range_dim);
3787 for (i = 0; i < map->n; ++i) {
3788 map->p[i] = isl_basic_map_range_map(map->p[i]);
3792 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3793 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3800 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3803 struct isl_map *map = NULL;
3805 set = isl_set_cow(set);
3808 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3809 map = (struct isl_map *)set;
3810 for (i = 0; i < set->n; ++i) {
3811 map->p[i] = isl_basic_map_from_basic_set(
3812 set->p[i], isl_dim_copy(dim));
3816 isl_dim_free(map->dim);
3825 __isl_give isl_basic_map *isl_basic_map_from_domain(
3826 __isl_take isl_basic_set *bset)
3828 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
3831 __isl_give isl_basic_map *isl_basic_map_from_range(
3832 __isl_take isl_basic_set *bset)
3834 return (isl_basic_map *)bset;
3837 struct isl_map *isl_map_from_range(struct isl_set *set)
3839 return (struct isl_map *)set;
3842 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3844 return isl_map_reverse(isl_map_from_range(set));
3847 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3848 __isl_take isl_set *range)
3850 return isl_map_apply_range(isl_map_from_domain(domain),
3851 isl_map_from_range(range));
3854 struct isl_set *isl_set_from_map(struct isl_map *map)
3857 struct isl_set *set = NULL;
3861 map = isl_map_cow(map);
3864 map->dim = isl_dim_as_set_dim(map->dim);
3867 set = (struct isl_set *)map;
3868 for (i = 0; i < map->n; ++i) {
3869 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3879 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3881 struct isl_map *map;
3885 isl_assert(dim->ctx, n >= 0, return NULL);
3886 map = isl_alloc(dim->ctx, struct isl_map,
3887 sizeof(struct isl_map) +
3888 (n - 1) * sizeof(struct isl_basic_map *));
3892 map->ctx = dim->ctx;
3893 isl_ctx_ref(map->ctx);
3905 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3906 unsigned nparam, unsigned in, unsigned out, int n,
3909 struct isl_map *map;
3910 struct isl_dim *dims;
3912 dims = isl_dim_alloc(ctx, nparam, in, out);
3916 map = isl_map_alloc_dim(dims, n, flags);
3920 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3922 struct isl_basic_map *bmap;
3923 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3924 bmap = isl_basic_map_set_to_empty(bmap);
3928 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3930 struct isl_basic_set *bset;
3931 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3932 bset = isl_basic_set_set_to_empty(bset);
3936 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3938 struct isl_basic_map *bmap;
3941 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3942 bmap = isl_basic_map_set_to_empty(bmap);
3946 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3948 struct isl_basic_map *bmap;
3951 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3952 bmap = isl_basic_map_set_to_empty(bmap);
3956 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3958 struct isl_basic_set *bset;
3961 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3962 bset = isl_basic_set_set_to_empty(bset);
3966 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3968 struct isl_basic_map *bmap;
3969 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
3973 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
3975 struct isl_basic_set *bset;
3976 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
3980 __isl_give isl_basic_map *isl_basic_map_universe_like(
3981 __isl_keep isl_basic_map *model)
3985 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3988 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3992 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3995 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3996 __isl_keep isl_set *model)
4000 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4003 struct isl_map *isl_map_empty(struct isl_dim *dim)
4005 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4008 struct isl_map *isl_map_empty_like(struct isl_map *model)
4012 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4015 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4019 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4022 struct isl_set *isl_set_empty(struct isl_dim *dim)
4024 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4027 struct isl_set *isl_set_empty_like(struct isl_set *model)
4031 return isl_set_empty(isl_dim_copy(model->dim));
4034 struct isl_map *isl_map_universe(struct isl_dim *dim)
4036 struct isl_map *map;
4039 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4040 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4044 struct isl_set *isl_set_universe(struct isl_dim *dim)
4046 struct isl_set *set;
4049 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4050 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4054 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4058 return isl_set_universe(isl_dim_copy(model->dim));
4061 struct isl_map *isl_map_dup(struct isl_map *map)
4064 struct isl_map *dup;
4068 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4069 for (i = 0; i < map->n; ++i)
4070 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4074 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4075 __isl_take isl_basic_map *bmap)
4079 if (isl_basic_map_fast_is_empty(bmap)) {
4080 isl_basic_map_free(bmap);
4083 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4084 isl_assert(map->ctx, map->n < map->size, goto error);
4085 map->p[map->n] = bmap;
4087 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4093 isl_basic_map_free(bmap);
4097 void isl_map_free(struct isl_map *map)
4107 isl_ctx_deref(map->ctx);
4108 for (i = 0; i < map->n; ++i)
4109 isl_basic_map_free(map->p[i]);
4110 isl_dim_free(map->dim);
4114 struct isl_map *isl_map_extend(struct isl_map *base,
4115 unsigned nparam, unsigned n_in, unsigned n_out)
4119 base = isl_map_cow(base);
4123 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4126 for (i = 0; i < base->n; ++i) {
4127 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4128 isl_dim_copy(base->dim), 0, 0, 0);
4138 struct isl_set *isl_set_extend(struct isl_set *base,
4139 unsigned nparam, unsigned dim)
4141 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4145 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4146 struct isl_basic_map *bmap, unsigned pos, int value)
4150 bmap = isl_basic_map_cow(bmap);
4151 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4152 j = isl_basic_map_alloc_equality(bmap);
4155 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4156 isl_int_set_si(bmap->eq[j][pos], -1);
4157 isl_int_set_si(bmap->eq[j][0], value);
4158 bmap = isl_basic_map_simplify(bmap);
4159 return isl_basic_map_finalize(bmap);
4161 isl_basic_map_free(bmap);
4165 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4166 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4170 bmap = isl_basic_map_cow(bmap);
4171 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4172 j = isl_basic_map_alloc_equality(bmap);
4175 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4176 isl_int_set_si(bmap->eq[j][pos], -1);
4177 isl_int_set(bmap->eq[j][0], value);
4178 bmap = isl_basic_map_simplify(bmap);
4179 return isl_basic_map_finalize(bmap);
4181 isl_basic_map_free(bmap);
4185 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4186 enum isl_dim_type type, unsigned pos, int value)
4190 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4191 return isl_basic_map_fix_pos_si(bmap,
4192 isl_basic_map_offset(bmap, type) + pos, value);
4194 isl_basic_map_free(bmap);
4198 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4199 enum isl_dim_type type, unsigned pos, isl_int value)
4203 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4204 return isl_basic_map_fix_pos(bmap,
4205 isl_basic_map_offset(bmap, type) + pos, value);
4207 isl_basic_map_free(bmap);
4211 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4212 enum isl_dim_type type, unsigned pos, int value)
4214 return (struct isl_basic_set *)
4215 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4219 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4220 enum isl_dim_type type, unsigned pos, isl_int value)
4222 return (struct isl_basic_set *)
4223 isl_basic_map_fix((struct isl_basic_map *)bset,
4227 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4228 unsigned input, int value)
4230 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4233 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4234 unsigned dim, int value)
4236 return (struct isl_basic_set *)
4237 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4238 isl_dim_set, dim, value);
4241 struct isl_map *isl_map_fix_si(struct isl_map *map,
4242 enum isl_dim_type type, unsigned pos, int value)
4246 map = isl_map_cow(map);
4250 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4251 for (i = 0; i < map->n; ++i) {
4252 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4256 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4263 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4264 enum isl_dim_type type, unsigned pos, int value)
4266 return (struct isl_set *)
4267 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4270 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4271 enum isl_dim_type type, unsigned pos, isl_int value)
4275 map = isl_map_cow(map);
4279 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4280 for (i = 0; i < map->n; ++i) {
4281 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4285 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4292 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4293 enum isl_dim_type type, unsigned pos, isl_int value)
4295 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4298 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4299 unsigned input, int value)
4301 return isl_map_fix_si(map, isl_dim_in, input, value);
4304 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4306 return (struct isl_set *)
4307 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4310 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4311 __isl_take isl_basic_map *bmap,
4312 enum isl_dim_type type, unsigned pos, int value)
4318 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4319 pos += isl_basic_map_offset(bmap, type);
4320 bmap = isl_basic_map_cow(bmap);
4321 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4322 j = isl_basic_map_alloc_inequality(bmap);
4325 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4326 isl_int_set_si(bmap->ineq[j][pos], 1);
4327 isl_int_set_si(bmap->ineq[j][0], -value);
4328 bmap = isl_basic_map_simplify(bmap);
4329 return isl_basic_map_finalize(bmap);
4331 isl_basic_map_free(bmap);
4335 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4336 unsigned dim, isl_int value)
4340 bset = isl_basic_set_cow(bset);
4341 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4342 j = isl_basic_set_alloc_inequality(bset);
4345 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4346 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4347 isl_int_neg(bset->ineq[j][0], value);
4348 bset = isl_basic_set_simplify(bset);
4349 return isl_basic_set_finalize(bset);
4351 isl_basic_set_free(bset);
4355 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4356 enum isl_dim_type type, unsigned pos, int value)
4360 map = isl_map_cow(map);
4364 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4365 for (i = 0; i < map->n; ++i) {
4366 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4371 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4378 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4379 enum isl_dim_type type, unsigned pos, int value)
4381 return (struct isl_set *)
4382 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4385 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4390 set = isl_set_cow(set);
4394 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4395 for (i = 0; i < set->n; ++i) {
4396 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4406 struct isl_map *isl_map_reverse(struct isl_map *map)
4410 map = isl_map_cow(map);
4414 map->dim = isl_dim_reverse(map->dim);
4417 for (i = 0; i < map->n; ++i) {
4418 map->p[i] = isl_basic_map_reverse(map->p[i]);
4422 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4429 static struct isl_map *isl_basic_map_partial_lexopt(
4430 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4431 struct isl_set **empty, int max)
4435 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4436 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4438 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4440 isl_basic_map_free(bmap);
4441 isl_basic_set_free(dom);
4447 struct isl_map *isl_basic_map_partial_lexmax(
4448 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4449 struct isl_set **empty)
4451 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4454 struct isl_map *isl_basic_map_partial_lexmin(
4455 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4456 struct isl_set **empty)
4458 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4461 struct isl_set *isl_basic_set_partial_lexmin(
4462 struct isl_basic_set *bset, struct isl_basic_set *dom,
4463 struct isl_set **empty)
4465 return (struct isl_set *)
4466 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4470 struct isl_set *isl_basic_set_partial_lexmax(
4471 struct isl_basic_set *bset, struct isl_basic_set *dom,
4472 struct isl_set **empty)
4474 return (struct isl_set *)
4475 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4479 /* Given a basic map "bmap", compute the lexicograhically minimal
4480 * (or maximal) image element for each domain element in dom.
4481 * Set *empty to those elements in dom that do not have an image element.
4483 * We first make sure the basic sets in dom are disjoint and then
4484 * simply collect the results over each of the basic sets separately.
4485 * We could probably improve the efficiency a bit by moving the union
4486 * domain down into the parametric integer programming.
4488 static __isl_give isl_map *basic_map_partial_lexopt(
4489 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4490 __isl_give isl_set **empty, int max)
4493 struct isl_map *res;
4495 dom = isl_set_make_disjoint(dom);
4499 if (isl_set_fast_is_empty(dom)) {
4500 res = isl_map_empty_like_basic_map(bmap);
4501 *empty = isl_set_empty_like(dom);
4503 isl_basic_map_free(bmap);
4507 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4508 isl_basic_set_copy(dom->p[0]), empty, max);
4510 for (i = 1; i < dom->n; ++i) {
4511 struct isl_map *res_i;
4512 struct isl_set *empty_i;
4514 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4515 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4517 res = isl_map_union_disjoint(res, res_i);
4518 *empty = isl_set_union_disjoint(*empty, empty_i);
4522 isl_basic_map_free(bmap);
4527 isl_basic_map_free(bmap);
4531 /* Given a map "map", compute the lexicograhically minimal
4532 * (or maximal) image element for each domain element in dom.
4533 * Set *empty to those elements in dom that do not have an image element.
4535 * We first compute the lexicographically minimal or maximal element
4536 * in the first basic map. This results in a partial solution "res"
4537 * and a subset "todo" of dom that still need to be handled.
4538 * We then consider each of the remaining maps in "map" and successively
4539 * improve both "res" and "todo".
4541 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4542 * Assume we are computing the lexicographical maximum.
4543 * We first intersect basic map i with a relation that maps elements
4544 * to elements that are lexicographically larger than the image elements
4545 * in res^k and the compute the maximum image element of this intersection.
4546 * The result ("better") corresponds to those image elements in basic map i
4547 * that are better than what we had before. The remainder ("keep") are the
4548 * domain elements for which the image element in res_k was better.
4549 * We also compute the lexicographical maximum of basic map i in todo^k.
4550 * res^i is the result of the operation + better + those elements in
4551 * res^k that we should keep
4552 * todo^i is the remainder of the maximum operation on todo^k.
4554 static __isl_give isl_map *isl_map_partial_lexopt(
4555 __isl_take isl_map *map, __isl_take isl_set *dom,
4556 __isl_give isl_set **empty, int max)
4559 struct isl_map *res;
4560 struct isl_set *todo;
4565 if (isl_map_fast_is_empty(map)) {
4573 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4574 isl_set_copy(dom), &todo, max);
4576 for (i = 1; i < map->n; ++i) {
4578 struct isl_map *better;
4579 struct isl_set *keep;
4580 struct isl_map *res_i;
4581 struct isl_set *todo_i;
4582 struct isl_dim *dim = isl_map_get_dim(res);
4584 dim = isl_dim_range(dim);
4586 lt = isl_map_lex_lt(dim);
4588 lt = isl_map_lex_gt(dim);
4589 lt = isl_map_apply_range(isl_map_copy(res), lt);
4590 lt = isl_map_intersect(lt,
4591 isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
4592 better = isl_map_partial_lexopt(lt,
4593 isl_map_domain(isl_map_copy(res)),
4596 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4597 todo, &todo_i, max);
4599 res = isl_map_intersect_domain(res, keep);
4600 res = isl_map_union_disjoint(res, res_i);
4601 res = isl_map_union_disjoint(res, better);
4622 __isl_give isl_map *isl_map_partial_lexmax(
4623 __isl_take isl_map *map, __isl_take isl_set *dom,
4624 __isl_give isl_set **empty)
4626 return isl_map_partial_lexopt(map, dom, empty, 1);
4629 __isl_give isl_map *isl_map_partial_lexmin(
4630 __isl_take isl_map *map, __isl_take isl_set *dom,
4631 __isl_give isl_set **empty)
4633 return isl_map_partial_lexopt(map, dom, empty, 0);
4636 __isl_give isl_set *isl_set_partial_lexmin(
4637 __isl_take isl_set *set, __isl_take isl_set *dom,
4638 __isl_give isl_set **empty)
4640 return (struct isl_set *)
4641 isl_map_partial_lexmin((struct isl_map *)set,
4645 __isl_give isl_set *isl_set_partial_lexmax(
4646 __isl_take isl_set *set, __isl_take isl_set *dom,
4647 __isl_give isl_set **empty)
4649 return (struct isl_set *)
4650 isl_map_partial_lexmax((struct isl_map *)set,
4654 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4656 struct isl_basic_set *dom = NULL;
4657 struct isl_dim *dom_dim;
4661 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4662 dom = isl_basic_set_universe(dom_dim);
4663 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4665 isl_basic_map_free(bmap);
4669 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4671 return isl_basic_map_lexopt(bmap, 0);
4674 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4676 return isl_basic_map_lexopt(bmap, 1);
4679 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4681 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4684 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4686 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4689 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4691 struct isl_set *dom = NULL;
4692 struct isl_dim *dom_dim;
4696 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4697 dom = isl_set_universe(dom_dim);
4698 return isl_map_partial_lexopt(map, dom, NULL, max);
4704 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4706 return isl_map_lexopt(map, 0);
4709 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4711 return isl_map_lexopt(map, 1);
4714 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4716 return (isl_set *)isl_map_lexmin((isl_map *)set);
4719 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4721 return (isl_set *)isl_map_lexmax((isl_map *)set);
4724 /* Apply a preimage specified by "mat" on the parameters of "bset".
4725 * bset is assumed to have only parameters and divs.
4727 static struct isl_basic_set *basic_set_parameter_preimage(
4728 struct isl_basic_set *bset, struct isl_mat *mat)
4735 bset->dim = isl_dim_cow(bset->dim);
4739 nparam = isl_basic_set_dim(bset, isl_dim_param);
4741 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4743 bset->dim->nparam = 0;
4744 bset->dim->n_out = nparam;
4745 bset = isl_basic_set_preimage(bset, mat);
4747 bset->dim->nparam = bset->dim->n_out;
4748 bset->dim->n_out = 0;
4753 isl_basic_set_free(bset);
4757 /* Apply a preimage specified by "mat" on the parameters of "set".
4758 * set is assumed to have only parameters and divs.
4760 static struct isl_set *set_parameter_preimage(
4761 struct isl_set *set, struct isl_mat *mat)
4763 struct isl_dim *dim = NULL;
4769 dim = isl_dim_copy(set->dim);
4770 dim = isl_dim_cow(dim);
4774 nparam = isl_set_dim(set, isl_dim_param);
4776 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4779 dim->n_out = nparam;
4780 isl_set_reset_dim(set, dim);
4781 set = isl_set_preimage(set, mat);
4784 dim = isl_dim_copy(set->dim);
4785 dim = isl_dim_cow(dim);
4788 dim->nparam = dim->n_out;
4790 isl_set_reset_dim(set, dim);
4800 /* Intersect the basic set "bset" with the affine space specified by the
4801 * equalities in "eq".
4803 static struct isl_basic_set *basic_set_append_equalities(
4804 struct isl_basic_set *bset, struct isl_mat *eq)
4812 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4817 len = 1 + isl_dim_total(bset->dim) + bset->extra;
4818 for (i = 0; i < eq->n_row; ++i) {
4819 k = isl_basic_set_alloc_equality(bset);
4822 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4823 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4827 bset = isl_basic_set_gauss(bset, NULL);
4828 bset = isl_basic_set_finalize(bset);
4833 isl_basic_set_free(bset);
4837 /* Intersect the set "set" with the affine space specified by the
4838 * equalities in "eq".
4840 static struct isl_set *set_append_equalities(struct isl_set *set,
4848 for (i = 0; i < set->n; ++i) {
4849 set->p[i] = basic_set_append_equalities(set->p[i],
4862 /* Project the given basic set onto its parameter domain, possibly introducing
4863 * new, explicit, existential variables in the constraints.
4864 * The input has parameters and (possibly implicit) existential variables.
4865 * The output has the same parameters, but only
4866 * explicit existentially quantified variables.
4868 * The actual projection is performed by pip, but pip doesn't seem
4869 * to like equalities very much, so we first remove the equalities
4870 * among the parameters by performing a variable compression on
4871 * the parameters. Afterward, an inverse transformation is performed
4872 * and the equalities among the parameters are inserted back in.
4874 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4878 struct isl_mat *T, *T2;
4879 struct isl_set *set;
4880 unsigned nparam, n_div;
4882 bset = isl_basic_set_cow(bset);
4886 if (bset->n_eq == 0)
4887 return isl_basic_set_lexmin(bset);
4889 isl_basic_set_gauss(bset, NULL);
4891 nparam = isl_basic_set_dim(bset, isl_dim_param);
4892 n_div = isl_basic_set_dim(bset, isl_dim_div);
4894 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4895 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4898 if (i == bset->n_eq)
4899 return isl_basic_set_lexmin(bset);
4901 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4903 eq = isl_mat_cow(eq);
4904 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4905 if (T && T->n_col == 0) {
4909 bset = isl_basic_set_set_to_empty(bset);
4910 return isl_set_from_basic_set(bset);
4912 bset = basic_set_parameter_preimage(bset, T);
4914 set = isl_basic_set_lexmin(bset);
4915 set = set_parameter_preimage(set, T2);
4916 set = set_append_equalities(set, eq);
4920 /* Compute an explicit representation for all the existentially
4921 * quantified variables.
4922 * The input and output dimensions are first turned into parameters.
4923 * compute_divs then returns a map with the same parameters and
4924 * no input or output dimensions and the dimension specification
4925 * is reset to that of the input.
4927 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4929 struct isl_basic_set *bset;
4930 struct isl_set *set;
4931 struct isl_map *map;
4932 struct isl_dim *dim, *orig_dim = NULL;
4937 bmap = isl_basic_map_cow(bmap);
4941 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4942 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4943 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4944 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4948 orig_dim = bmap->dim;
4950 bset = (struct isl_basic_set *)bmap;
4952 set = parameter_compute_divs(bset);
4953 map = (struct isl_map *)set;
4954 map = isl_map_reset_dim(map, orig_dim);
4958 isl_basic_map_free(bmap);
4962 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4970 off = isl_dim_total(bmap->dim);
4971 for (i = 0; i < bmap->n_div; ++i) {
4972 if (isl_int_is_zero(bmap->div[i][0]))
4974 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
4980 static int map_divs_known(__isl_keep isl_map *map)
4987 for (i = 0; i < map->n; ++i) {
4988 int known = basic_map_divs_known(map->p[i]);
4996 /* If bmap contains any unknown divs, then compute explicit
4997 * expressions for them. However, this computation may be
4998 * quite expensive, so first try to remove divs that aren't
5001 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5005 struct isl_map *map;
5007 known = basic_map_divs_known(bmap);
5011 return isl_map_from_basic_map(bmap);
5013 bmap = isl_basic_map_drop_redundant_divs(bmap);
5015 known = basic_map_divs_known(bmap);
5019 return isl_map_from_basic_map(bmap);
5021 map = compute_divs(bmap);
5024 isl_basic_map_free(bmap);
5028 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5032 struct isl_map *res;
5039 known = map_divs_known(map);
5047 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5048 for (i = 1 ; i < map->n; ++i) {
5050 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5051 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5052 res = isl_map_union_disjoint(res, r2);
5054 res = isl_map_union(res, r2);
5061 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5063 return (struct isl_set *)
5064 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5067 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5069 return (struct isl_set *)
5070 isl_map_compute_divs((struct isl_map *)set);
5073 struct isl_set *isl_map_domain(struct isl_map *map)
5076 struct isl_set *set;
5081 map = isl_map_cow(map);
5085 set = (struct isl_set *)map;
5086 set->dim = isl_dim_domain(set->dim);
5089 for (i = 0; i < map->n; ++i) {
5090 set->p[i] = isl_basic_map_domain(map->p[i]);
5094 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5095 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5102 struct isl_map *isl_map_union_disjoint(
5103 struct isl_map *map1, struct isl_map *map2)
5107 struct isl_map *map = NULL;
5121 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5123 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5124 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5125 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5127 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5128 map1->n + map2->n, flags);
5131 for (i = 0; i < map1->n; ++i) {
5132 map = isl_map_add_basic_map(map,
5133 isl_basic_map_copy(map1->p[i]));
5137 for (i = 0; i < map2->n; ++i) {
5138 map = isl_map_add_basic_map(map,
5139 isl_basic_map_copy(map2->p[i]));
5153 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5155 map1 = isl_map_union_disjoint(map1, map2);
5159 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5163 struct isl_set *isl_set_union_disjoint(
5164 struct isl_set *set1, struct isl_set *set2)
5166 return (struct isl_set *)
5167 isl_map_union_disjoint(
5168 (struct isl_map *)set1, (struct isl_map *)set2);
5171 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5173 return (struct isl_set *)
5174 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5177 struct isl_map *isl_map_intersect_range(
5178 struct isl_map *map, struct isl_set *set)
5181 struct isl_map *result;
5187 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5188 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5189 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5191 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5192 map->n * set->n, flags);
5195 for (i = 0; i < map->n; ++i)
5196 for (j = 0; j < set->n; ++j) {
5197 result = isl_map_add_basic_map(result,
5198 isl_basic_map_intersect_range(
5199 isl_basic_map_copy(map->p[i]),
5200 isl_basic_set_copy(set->p[j])));
5213 struct isl_map *isl_map_intersect_domain(
5214 struct isl_map *map, struct isl_set *set)
5216 return isl_map_reverse(
5217 isl_map_intersect_range(isl_map_reverse(map), set));
5220 struct isl_map *isl_map_apply_domain(
5221 struct isl_map *map1, struct isl_map *map2)
5225 map1 = isl_map_reverse(map1);
5226 map1 = isl_map_apply_range(map1, map2);
5227 return isl_map_reverse(map1);
5234 struct isl_map *isl_map_apply_range(
5235 struct isl_map *map1, struct isl_map *map2)
5237 struct isl_dim *dim_result;
5238 struct isl_map *result;
5244 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5245 isl_dim_copy(map2->dim));
5247 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5250 for (i = 0; i < map1->n; ++i)
5251 for (j = 0; j < map2->n; ++j) {
5252 result = isl_map_add_basic_map(result,
5253 isl_basic_map_apply_range(
5254 isl_basic_map_copy(map1->p[i]),
5255 isl_basic_map_copy(map2->p[j])));
5261 if (result && result->n <= 1)
5262 ISL_F_SET(result, ISL_MAP_DISJOINT);
5271 * returns range - domain
5273 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5275 isl_dim *dims, *target_dim;
5276 struct isl_basic_set *bset;
5283 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5284 bmap->dim, isl_dim_out),
5286 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5287 dim = isl_basic_map_n_in(bmap);
5288 nparam = isl_basic_map_n_param(bmap);
5289 bset = isl_basic_set_from_basic_map(bmap);
5290 bset = isl_basic_set_cow(bset);
5291 dims = isl_basic_set_get_dim(bset);
5292 dims = isl_dim_add(dims, isl_dim_set, dim);
5293 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5294 bset = isl_basic_set_swap_vars(bset, 2*dim);
5295 for (i = 0; i < dim; ++i) {
5296 int j = isl_basic_map_alloc_equality(
5297 (struct isl_basic_map *)bset);
5300 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5301 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5302 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5303 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5305 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5306 bset = isl_basic_set_reset_dim(bset, target_dim);
5309 isl_basic_map_free(bmap);
5314 * returns range - domain
5316 struct isl_set *isl_map_deltas(struct isl_map *map)
5320 struct isl_set *result;
5325 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5326 map->dim, isl_dim_out),
5328 dim = isl_map_get_dim(map);
5329 dim = isl_dim_domain(dim);
5330 result = isl_set_alloc_dim(dim, map->n, map->flags);
5333 for (i = 0; i < map->n; ++i)
5334 result = isl_set_add_basic_set(result,
5335 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5343 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5345 struct isl_basic_map *bmap;
5353 nparam = dims->nparam;
5355 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5359 for (i = 0; i < dim; ++i) {
5360 int j = isl_basic_map_alloc_equality(bmap);
5363 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5364 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5365 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5367 return isl_basic_map_finalize(bmap);
5369 isl_basic_map_free(bmap);
5373 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
5375 struct isl_dim *dim = isl_dim_map(set_dim);
5378 return basic_map_identity(dim);
5381 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5383 if (!model || !model->dim)
5385 isl_assert(model->ctx,
5386 model->dim->n_in == model->dim->n_out, return NULL);
5387 return basic_map_identity(isl_dim_copy(model->dim));
5390 static struct isl_map *map_identity(struct isl_dim *dim)
5392 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
5393 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
5396 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
5398 struct isl_dim *dim = isl_dim_map(set_dim);
5401 return map_identity(dim);
5404 struct isl_map *isl_map_identity_like(struct isl_map *model)
5406 if (!model || !model->dim)
5408 isl_assert(model->ctx,
5409 model->dim->n_in == model->dim->n_out, return NULL);
5410 return map_identity(isl_dim_copy(model->dim));
5413 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5415 if (!model || !model->dim)
5417 isl_assert(model->ctx,
5418 model->dim->n_in == model->dim->n_out, return NULL);
5419 return map_identity(isl_dim_copy(model->dim));
5422 /* Construct a basic set with all set dimensions having only non-negative
5425 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5430 struct isl_basic_set *bset;
5434 nparam = dims->nparam;
5436 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5439 for (i = 0; i < dim; ++i) {
5440 int k = isl_basic_set_alloc_inequality(bset);
5443 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5444 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5448 isl_basic_set_free(bset);
5452 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5453 enum isl_dim_type type, unsigned first, unsigned n)
5456 isl_basic_set *nonneg = NULL;
5457 isl_basic_set *neg = NULL;
5464 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5466 for (i = 0; i < n; ++i) {
5470 nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5471 k = isl_basic_set_alloc_inequality(nonneg);
5474 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5475 isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
5477 neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5478 k = isl_basic_set_alloc_inequality(neg);
5481 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5482 isl_int_set_si(neg->ineq[k][0], -1);
5483 isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
5485 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5490 isl_basic_set_free(nonneg);
5491 isl_basic_set_free(neg);
5496 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5498 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5501 int isl_basic_map_is_subset(
5502 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5505 struct isl_map *map1;
5506 struct isl_map *map2;
5508 if (!bmap1 || !bmap2)
5511 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5512 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5514 is_subset = isl_map_is_subset(map1, map2);
5522 int isl_basic_map_is_equal(
5523 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5527 if (!bmap1 || !bmap2)
5529 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5532 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5536 int isl_basic_set_is_equal(
5537 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5539 return isl_basic_map_is_equal(
5540 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5543 int isl_map_is_empty(struct isl_map *map)
5550 for (i = 0; i < map->n; ++i) {
5551 is_empty = isl_basic_map_is_empty(map->p[i]);
5560 int isl_map_fast_is_empty(struct isl_map *map)
5562 return map ? map->n == 0 : -1;
5565 int isl_set_fast_is_empty(struct isl_set *set)
5567 return set ? set->n == 0 : -1;
5570 int isl_set_is_empty(struct isl_set *set)
5572 return isl_map_is_empty((struct isl_map *)set);
5575 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
5580 return isl_dim_equal(map1->dim, map2->dim);
5583 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5588 return isl_dim_equal(set1->dim, set2->dim);
5591 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
5597 is_subset = isl_map_is_subset(map1, map2);
5600 is_subset = isl_map_is_subset(map2, map1);
5604 int isl_basic_map_is_strict_subset(
5605 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5609 if (!bmap1 || !bmap2)
5611 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5614 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5615 if (is_subset == -1)
5620 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
5626 is_subset = isl_map_is_subset(map1, map2);
5629 is_subset = isl_map_is_subset(map2, map1);
5630 if (is_subset == -1)
5635 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5637 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5640 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5644 return bmap->n_eq == 0 && bmap->n_ineq == 0;
5647 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5651 return bset->n_eq == 0 && bset->n_ineq == 0;
5654 int isl_map_fast_is_universe(__isl_keep isl_map *map)
5661 for (i = 0; i < map->n; ++i) {
5662 int r = isl_basic_map_is_universe(map->p[i]);
5670 int isl_set_fast_is_universe(__isl_keep isl_set *set)
5672 return isl_map_fast_is_universe((isl_map *) set);
5675 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
5677 struct isl_basic_set *bset = NULL;
5678 struct isl_vec *sample = NULL;
5685 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
5688 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
5689 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
5690 copy = isl_basic_map_remove_redundancies(copy);
5691 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
5692 isl_basic_map_free(copy);
5696 total = 1 + isl_basic_map_total_dim(bmap);
5697 if (bmap->sample && bmap->sample->size == total) {
5698 int contains = isl_basic_map_contains(bmap, bmap->sample);
5704 isl_vec_free(bmap->sample);
5705 bmap->sample = NULL;
5706 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
5709 sample = isl_basic_set_sample_vec(bset);
5712 empty = sample->size == 0;
5713 isl_vec_free(bmap->sample);
5714 bmap->sample = sample;
5716 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
5721 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
5725 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
5728 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
5732 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
5735 int isl_basic_set_is_empty(struct isl_basic_set *bset)
5737 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
5740 struct isl_map *isl_basic_map_union(
5741 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5743 struct isl_map *map;
5744 if (!bmap1 || !bmap2)
5747 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
5749 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
5752 map = isl_map_add_basic_map(map, bmap1);
5753 map = isl_map_add_basic_map(map, bmap2);
5756 isl_basic_map_free(bmap1);
5757 isl_basic_map_free(bmap2);
5761 struct isl_set *isl_basic_set_union(
5762 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5764 return (struct isl_set *)isl_basic_map_union(
5765 (struct isl_basic_map *)bset1,
5766 (struct isl_basic_map *)bset2);
5769 /* Order divs such that any div only depends on previous divs */
5770 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
5778 off = isl_dim_total(bmap->dim);
5780 for (i = 0; i < bmap->n_div; ++i) {
5782 if (isl_int_is_zero(bmap->div[i][0]))
5784 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
5788 isl_basic_map_swap_div(bmap, i, i + pos);
5794 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
5796 return (struct isl_basic_set *)
5797 isl_basic_map_order_divs((struct isl_basic_map *)bset);
5800 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
5807 for (i = 0; i < map->n; ++i) {
5808 map->p[i] = isl_basic_map_order_divs(map->p[i]);
5819 /* Look for a div in dst that corresponds to the div "div" in src.
5820 * The divs before "div" in src and dst are assumed to be the same.
5822 * Returns -1 if no corresponding div was found and the position
5823 * of the corresponding div in dst otherwise.
5825 static int find_div(struct isl_basic_map *dst,
5826 struct isl_basic_map *src, unsigned div)
5830 unsigned total = isl_dim_total(src->dim);
5832 isl_assert(dst->ctx, div <= dst->n_div, return -1);
5833 for (i = div; i < dst->n_div; ++i)
5834 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
5835 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
5836 dst->n_div - div) == -1)
5841 struct isl_basic_map *isl_basic_map_align_divs(
5842 struct isl_basic_map *dst, struct isl_basic_map *src)
5845 unsigned total = isl_dim_total(src->dim);
5850 if (src->n_div == 0)
5853 for (i = 0; i < src->n_div; ++i)
5854 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
5856 src = isl_basic_map_order_divs(src);
5857 dst = isl_basic_map_cow(dst);
5858 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
5859 src->n_div, 0, 2 * src->n_div);
5862 for (i = 0; i < src->n_div; ++i) {
5863 int j = find_div(dst, src, i);
5865 j = isl_basic_map_alloc_div(dst);
5868 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
5869 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
5870 if (isl_basic_map_add_div_constraints(dst, j) < 0)
5874 isl_basic_map_swap_div(dst, i, j);
5878 isl_basic_map_free(dst);
5882 struct isl_basic_set *isl_basic_set_align_divs(
5883 struct isl_basic_set *dst, struct isl_basic_set *src)
5885 return (struct isl_basic_set *)isl_basic_map_align_divs(
5886 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
5889 struct isl_map *isl_map_align_divs(struct isl_map *map)
5897 map = isl_map_compute_divs(map);
5898 map = isl_map_cow(map);
5902 for (i = 1; i < map->n; ++i)
5903 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5904 for (i = 1; i < map->n; ++i)
5905 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5907 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5911 struct isl_set *isl_set_align_divs(struct isl_set *set)
5913 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5916 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5920 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5921 map = isl_map_intersect_domain(map, set);
5922 set = isl_map_range(map);
5930 /* There is no need to cow as removing empty parts doesn't change
5931 * the meaning of the set.
5933 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5940 for (i = map->n-1; i >= 0; --i) {
5941 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5943 isl_basic_map_free(map->p[i]);
5944 if (i != map->n-1) {
5945 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5946 map->p[i] = map->p[map->n-1];
5954 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
5956 return (struct isl_set *)
5957 isl_map_remove_empty_parts((struct isl_map *)set);
5960 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
5962 struct isl_basic_map *bmap;
5963 if (!map || map->n == 0)
5965 bmap = map->p[map->n-1];
5966 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
5967 return isl_basic_map_copy(bmap);
5970 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
5972 return (struct isl_basic_set *)
5973 isl_map_copy_basic_map((struct isl_map *)set);
5976 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
5977 __isl_keep isl_basic_map *bmap)
5983 for (i = map->n-1; i >= 0; --i) {
5984 if (map->p[i] != bmap)
5986 map = isl_map_cow(map);
5989 isl_basic_map_free(map->p[i]);
5990 if (i != map->n-1) {
5991 ISL_F_CLR(map, ISL_SET_NORMALIZED);
5992 map->p[i] = map->p[map->n-1];
6003 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6004 struct isl_basic_set *bset)
6006 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6007 (struct isl_basic_map *)bset);
6010 /* Given two basic sets bset1 and bset2, compute the maximal difference
6011 * between the values of dimension pos in bset1 and those in bset2
6012 * for any common value of the parameters and dimensions preceding pos.
6014 static enum isl_lp_result basic_set_maximal_difference_at(
6015 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6016 int pos, isl_int *opt)
6018 struct isl_dim *dims;
6019 struct isl_basic_map *bmap1 = NULL;
6020 struct isl_basic_map *bmap2 = NULL;
6021 struct isl_ctx *ctx;
6022 struct isl_vec *obj;
6025 unsigned dim1, dim2;
6026 enum isl_lp_result res;
6028 if (!bset1 || !bset2)
6029 return isl_lp_error;
6031 nparam = isl_basic_set_n_param(bset1);
6032 dim1 = isl_basic_set_n_dim(bset1);
6033 dim2 = isl_basic_set_n_dim(bset2);
6034 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6035 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6036 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6037 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6038 if (!bmap1 || !bmap2)
6040 bmap1 = isl_basic_map_cow(bmap1);
6041 bmap1 = isl_basic_map_extend(bmap1, nparam,
6042 pos, (dim1 - pos) + (dim2 - pos),
6043 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6044 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6047 total = isl_basic_map_total_dim(bmap1);
6049 obj = isl_vec_alloc(ctx, 1 + total);
6050 isl_seq_clr(obj->block.data, 1 + total);
6051 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6052 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6055 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6057 isl_basic_map_free(bmap1);
6061 isl_basic_map_free(bmap1);
6062 isl_basic_map_free(bmap2);
6063 return isl_lp_error;
6066 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6067 * for any common value of the parameters and dimensions preceding pos
6068 * in both basic sets, the values of dimension pos in bset1 are
6069 * smaller or larger than those in bset2.
6072 * 1 if bset1 follows bset2
6073 * -1 if bset1 precedes bset2
6074 * 0 if bset1 and bset2 are incomparable
6075 * -2 if some error occurred.
6077 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6078 struct isl_basic_set *bset2, int pos)
6081 enum isl_lp_result res;
6086 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6088 if (res == isl_lp_empty)
6090 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6091 res == isl_lp_unbounded)
6093 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6102 /* Given two basic sets bset1 and bset2, check whether
6103 * for any common value of the parameters and dimensions preceding pos
6104 * there is a value of dimension pos in bset1 that is larger
6105 * than a value of the same dimension in bset2.
6108 * 1 if there exists such a pair
6109 * 0 if there is no such pair, but there is a pair of equal values
6111 * -2 if some error occurred.
6113 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6114 __isl_keep 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))
6131 else if (res == isl_lp_ok)
6140 /* Given two sets set1 and set2, check whether
6141 * for any common value of the parameters and dimensions preceding pos
6142 * there is a value of dimension pos in set1 that is larger
6143 * than a value of the same dimension in set2.
6146 * 1 if there exists such a pair
6147 * 0 if there is no such pair, but there is a pair of equal values
6149 * -2 if some error occurred.
6151 int isl_set_follows_at(__isl_keep isl_set *set1,
6152 __isl_keep isl_set *set2, int pos)
6160 for (i = 0; i < set1->n; ++i)
6161 for (j = 0; j < set2->n; ++j) {
6163 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6164 if (f == 1 || f == -2)
6173 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
6174 unsigned pos, isl_int *val)
6182 total = isl_basic_map_total_dim(bmap);
6183 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6184 for (; d+1 > pos; --d)
6185 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6189 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6191 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6193 if (!isl_int_is_one(bmap->eq[i][1+d]))
6196 isl_int_neg(*val, bmap->eq[i][0]);
6202 static int isl_map_fast_has_fixed_var(struct isl_map *map,
6203 unsigned pos, isl_int *val)
6215 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
6218 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
6219 for (i = 1; fixed == 1 && i < map->n; ++i) {
6220 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
6221 if (fixed == 1 && isl_int_ne(tmp, v))
6225 isl_int_set(*val, v);
6231 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
6232 unsigned pos, isl_int *val)
6234 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
6238 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
6241 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
6244 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
6245 enum isl_dim_type type, unsigned pos, isl_int *val)
6247 if (pos >= isl_basic_map_dim(bmap, type))
6249 return isl_basic_map_fast_has_fixed_var(bmap,
6250 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6253 int isl_map_fast_is_fixed(struct isl_map *map,
6254 enum isl_dim_type type, unsigned pos, isl_int *val)
6256 if (pos >= isl_map_dim(map, type))
6258 return isl_map_fast_has_fixed_var(map,
6259 map_offset(map, type) - 1 + pos, val);
6262 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6263 * then return this fixed value in *val.
6265 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6268 return isl_basic_set_fast_has_fixed_var(bset,
6269 isl_basic_set_n_param(bset) + dim, val);
6272 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6273 * then return this fixed value in *val.
6275 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6277 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6280 /* Check if input variable in has fixed value and if so and if val is not NULL,
6281 * then return this fixed value in *val.
6283 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6285 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6288 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6289 * and if val is not NULL, then return this lower bound in *val.
6291 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6292 unsigned dim, isl_int *val)
6294 int i, i_eq = -1, i_ineq = -1;
6301 total = isl_basic_set_total_dim(bset);
6302 nparam = isl_basic_set_n_param(bset);
6303 for (i = 0; i < bset->n_eq; ++i) {
6304 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6310 for (i = 0; i < bset->n_ineq; ++i) {
6311 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6313 if (i_eq != -1 || i_ineq != -1)
6317 if (i_eq == -1 && i_ineq == -1)
6319 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6320 /* The coefficient should always be one due to normalization. */
6321 if (!isl_int_is_one(c[1+nparam+dim]))
6323 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6325 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6326 total - nparam - dim - 1) != -1)
6329 isl_int_neg(*val, c[0]);
6333 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6334 unsigned dim, isl_int *val)
6346 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6350 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6352 for (i = 1; fixed == 1 && i < set->n; ++i) {
6353 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6355 if (fixed == 1 && isl_int_ne(tmp, v))
6359 isl_int_set(*val, v);
6370 static int qsort_constraint_cmp(const void *p1, const void *p2)
6372 const struct constraint *c1 = (const struct constraint *)p1;
6373 const struct constraint *c2 = (const struct constraint *)p2;
6374 unsigned size = isl_min(c1->size, c2->size);
6375 return isl_seq_cmp(c1->c, c2->c, size);
6378 static struct isl_basic_map *isl_basic_map_sort_constraints(
6379 struct isl_basic_map *bmap)
6382 struct constraint *c;
6387 total = isl_basic_map_total_dim(bmap);
6388 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6391 for (i = 0; i < bmap->n_ineq; ++i) {
6393 c[i].c = bmap->ineq[i];
6395 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6396 for (i = 0; i < bmap->n_ineq; ++i)
6397 bmap->ineq[i] = c[i].c;
6401 isl_basic_map_free(bmap);
6405 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6406 __isl_take isl_basic_set *bset)
6408 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6409 (struct isl_basic_map *)bset);
6412 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6416 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6418 bmap = isl_basic_map_remove_redundancies(bmap);
6419 bmap = isl_basic_map_sort_constraints(bmap);
6420 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6424 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6426 return (struct isl_basic_set *)isl_basic_map_normalize(
6427 (struct isl_basic_map *)bset);
6430 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6431 const __isl_keep isl_basic_map *bmap2)
6438 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6439 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6440 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6441 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6442 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6443 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6444 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6445 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6447 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6449 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6451 if (bmap1->n_eq != bmap2->n_eq)
6452 return bmap1->n_eq - bmap2->n_eq;
6453 if (bmap1->n_ineq != bmap2->n_ineq)
6454 return bmap1->n_ineq - bmap2->n_ineq;
6455 if (bmap1->n_div != bmap2->n_div)
6456 return bmap1->n_div - bmap2->n_div;
6457 total = isl_basic_map_total_dim(bmap1);
6458 for (i = 0; i < bmap1->n_eq; ++i) {
6459 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6463 for (i = 0; i < bmap1->n_ineq; ++i) {
6464 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6468 for (i = 0; i < bmap1->n_div; ++i) {
6469 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6476 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6477 __isl_keep isl_basic_map *bmap2)
6479 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6482 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6483 __isl_keep isl_basic_set *bset2)
6485 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6486 (isl_basic_map *)bset2);
6489 static int qsort_bmap_cmp(const void *p1, const void *p2)
6491 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6492 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6494 return isl_basic_map_fast_cmp(bmap1, bmap2);
6497 /* We normalize in place, but if anything goes wrong we need
6498 * to return NULL, so we need to make sure we don't change the
6499 * meaning of any possible other copies of map.
6501 struct isl_map *isl_map_normalize(struct isl_map *map)
6504 struct isl_basic_map *bmap;
6508 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6510 for (i = 0; i < map->n; ++i) {
6511 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6514 isl_basic_map_free(map->p[i]);
6517 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6518 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6519 map = isl_map_remove_empty_parts(map);
6522 for (i = map->n - 1; i >= 1; --i) {
6523 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6525 isl_basic_map_free(map->p[i-1]);
6526 for (j = i; j < map->n; ++j)
6527 map->p[j-1] = map->p[j];
6537 struct isl_set *isl_set_normalize(struct isl_set *set)
6539 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
6542 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
6552 if (!isl_dim_equal(map1->dim, map2->dim))
6555 map1 = isl_map_copy(map1);
6556 map2 = isl_map_copy(map2);
6557 map1 = isl_map_normalize(map1);
6558 map2 = isl_map_normalize(map2);
6561 equal = map1->n == map2->n;
6562 for (i = 0; equal && i < map1->n; ++i) {
6563 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
6576 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
6578 return isl_map_fast_is_equal((struct isl_map *)set1,
6579 (struct isl_map *)set2);
6582 /* Return an interval that ranges from min to max (inclusive)
6584 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
6585 isl_int min, isl_int max)
6588 struct isl_basic_set *bset = NULL;
6590 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
6594 k = isl_basic_set_alloc_inequality(bset);
6597 isl_int_set_si(bset->ineq[k][1], 1);
6598 isl_int_neg(bset->ineq[k][0], min);
6600 k = isl_basic_set_alloc_inequality(bset);
6603 isl_int_set_si(bset->ineq[k][1], -1);
6604 isl_int_set(bset->ineq[k][0], max);
6608 isl_basic_set_free(bset);
6612 /* Return the Cartesian product of the basic sets in list (in the given order).
6614 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
6622 struct isl_basic_set *product = NULL;
6626 isl_assert(list->ctx, list->n > 0, goto error);
6627 isl_assert(list->ctx, list->p[0], goto error);
6628 nparam = isl_basic_set_n_param(list->p[0]);
6629 dim = isl_basic_set_n_dim(list->p[0]);
6630 extra = list->p[0]->n_div;
6631 n_eq = list->p[0]->n_eq;
6632 n_ineq = list->p[0]->n_ineq;
6633 for (i = 1; i < list->n; ++i) {
6634 isl_assert(list->ctx, list->p[i], goto error);
6635 isl_assert(list->ctx,
6636 nparam == isl_basic_set_n_param(list->p[i]), goto error);
6637 dim += isl_basic_set_n_dim(list->p[i]);
6638 extra += list->p[i]->n_div;
6639 n_eq += list->p[i]->n_eq;
6640 n_ineq += list->p[i]->n_ineq;
6642 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
6647 for (i = 0; i < list->n; ++i) {
6648 isl_basic_set_add_constraints(product,
6649 isl_basic_set_copy(list->p[i]), dim);
6650 dim += isl_basic_set_n_dim(list->p[i]);
6652 isl_basic_set_list_free(list);
6655 isl_basic_set_free(product);
6656 isl_basic_set_list_free(list);
6660 struct isl_basic_map *isl_basic_map_product(
6661 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6663 struct isl_dim *dim_result = NULL;
6664 struct isl_basic_map *bmap;
6665 unsigned in1, in2, out1, out2, nparam, total, pos;
6666 struct isl_dim_map *dim_map1, *dim_map2;
6668 if (!bmap1 || !bmap2)
6671 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
6672 bmap2->dim, isl_dim_param), goto error);
6673 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
6674 isl_dim_copy(bmap2->dim));
6676 in1 = isl_basic_map_n_in(bmap1);
6677 in2 = isl_basic_map_n_in(bmap2);
6678 out1 = isl_basic_map_n_out(bmap1);
6679 out2 = isl_basic_map_n_out(bmap2);
6680 nparam = isl_basic_map_n_param(bmap1);
6682 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
6683 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
6684 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
6685 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
6686 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
6687 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
6688 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
6689 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
6690 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
6691 isl_dim_map_div(dim_map1, bmap1, pos += out2);
6692 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
6694 bmap = isl_basic_map_alloc_dim(dim_result,
6695 bmap1->n_div + bmap2->n_div,
6696 bmap1->n_eq + bmap2->n_eq,
6697 bmap1->n_ineq + bmap2->n_ineq);
6698 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
6699 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
6700 bmap = isl_basic_map_simplify(bmap);
6701 return isl_basic_map_finalize(bmap);
6703 isl_basic_map_free(bmap1);
6704 isl_basic_map_free(bmap2);
6708 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
6710 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
6713 struct isl_map *result;
6719 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
6720 map2->dim, isl_dim_param), goto error);
6722 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6723 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6724 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6726 result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
6727 isl_dim_copy(map2->dim)),
6728 map1->n * map2->n, flags);
6731 for (i = 0; i < map1->n; ++i)
6732 for (j = 0; j < map2->n; ++j) {
6733 struct isl_basic_map *part;
6734 part = isl_basic_map_product(
6735 isl_basic_map_copy(map1->p[i]),
6736 isl_basic_map_copy(map2->p[j]));
6737 if (isl_basic_map_is_empty(part))
6738 isl_basic_map_free(part);
6740 result = isl_map_add_basic_map(result, part);
6753 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6755 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
6756 __isl_take isl_map *map2)
6760 prod = isl_map_product(map1, map2);
6761 prod = isl_map_flatten(prod);
6765 /* Given two set A and B, construct its Cartesian product A x B.
6767 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
6769 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
6770 (struct isl_map *)set2);
6773 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
6774 __isl_take isl_set *set2)
6776 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
6779 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
6782 uint32_t hash = isl_hash_init();
6787 bset = isl_basic_set_copy(bset);
6788 bset = isl_basic_set_normalize(bset);
6791 total = isl_basic_set_total_dim(bset);
6792 isl_hash_byte(hash, bset->n_eq & 0xFF);
6793 for (i = 0; i < bset->n_eq; ++i) {
6795 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
6796 isl_hash_hash(hash, c_hash);
6798 isl_hash_byte(hash, bset->n_ineq & 0xFF);
6799 for (i = 0; i < bset->n_ineq; ++i) {
6801 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
6802 isl_hash_hash(hash, c_hash);
6804 isl_hash_byte(hash, bset->n_div & 0xFF);
6805 for (i = 0; i < bset->n_div; ++i) {
6807 if (isl_int_is_zero(bset->div[i][0]))
6809 isl_hash_byte(hash, i & 0xFF);
6810 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
6811 isl_hash_hash(hash, c_hash);
6813 isl_basic_set_free(bset);
6817 uint32_t isl_set_get_hash(struct isl_set *set)
6824 set = isl_set_copy(set);
6825 set = isl_set_normalize(set);
6829 hash = isl_hash_init();
6830 for (i = 0; i < set->n; ++i) {
6832 bset_hash = isl_basic_set_get_hash(set->p[i]);
6833 isl_hash_hash(hash, bset_hash);
6841 /* Check if the value for dimension dim is completely determined
6842 * by the values of the other parameters and variables.
6843 * That is, check if dimension dim is involved in an equality.
6845 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
6852 nparam = isl_basic_set_n_param(bset);
6853 for (i = 0; i < bset->n_eq; ++i)
6854 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
6859 /* Check if the value for dimension dim is completely determined
6860 * by the values of the other parameters and variables.
6861 * That is, check if dimension dim is involved in an equality
6862 * for each of the subsets.
6864 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
6870 for (i = 0; i < set->n; ++i) {
6872 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
6879 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
6880 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
6887 for (i = 0; i < map->n; ++i)
6888 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
6894 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
6895 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
6902 for (i = 0; i < set->n; ++i)
6903 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
6909 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
6911 struct isl_dim *dim;
6916 if (bset->n_div == 0)
6919 bset = isl_basic_set_cow(bset);
6923 dim = isl_basic_set_get_dim(bset);
6924 dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
6927 isl_dim_free(bset->dim);
6931 bset = isl_basic_set_finalize(bset);
6935 isl_basic_set_free(bset);
6939 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
6942 struct isl_dim *dim;
6945 set = isl_set_align_divs(set);
6949 if (set->n == 0 || set->p[0]->n_div == 0)
6952 set = isl_set_cow(set);
6956 n_div = set->p[0]->n_div;
6957 dim = isl_set_get_dim(set);
6958 dim = isl_dim_add(dim, isl_dim_set, n_div);
6961 isl_dim_free(set->dim);
6964 for (i = 0; i < set->n; ++i) {
6965 set->p[i] = isl_basic_set_lift(set->p[i]);
6976 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
6978 struct isl_dim *dim;
6979 struct isl_basic_map *bmap;
6986 set = isl_set_align_divs(set);
6991 dim = isl_set_get_dim(set);
6992 if (set->n == 0 || set->p[0]->n_div == 0) {
6994 return isl_map_identity(dim);
6997 n_div = set->p[0]->n_div;
6998 dim = isl_dim_map(dim);
6999 n_param = isl_dim_size(dim, isl_dim_param);
7000 n_set = isl_dim_size(dim, isl_dim_in);
7001 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7002 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7003 for (i = 0; i < n_set; ++i)
7004 bmap = var_equal(bmap, i);
7006 total = n_param + n_set + n_set + n_div;
7007 for (i = 0; i < n_div; ++i) {
7008 k = isl_basic_map_alloc_inequality(bmap);
7011 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7012 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7013 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7014 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7015 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7016 set->p[0]->div[i][0]);
7018 l = isl_basic_map_alloc_inequality(bmap);
7021 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7022 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7023 set->p[0]->div[i][0]);
7024 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7028 return isl_map_from_basic_map(bmap);
7031 isl_basic_map_free(bmap);
7035 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7043 dim = isl_basic_set_total_dim(bset);
7044 size += bset->n_eq * (1 + dim);
7045 size += bset->n_ineq * (1 + dim);
7046 size += bset->n_div * (2 + dim);
7051 int isl_set_size(__isl_keep isl_set *set)
7059 for (i = 0; i < set->n; ++i)
7060 size += isl_basic_set_size(set->p[i]);
7065 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7066 enum isl_dim_type type, unsigned pos)
7074 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7076 pos += isl_basic_map_offset(bmap, type);
7078 for (i = 0; i < bmap->n_eq; ++i)
7079 if (!isl_int_is_zero(bmap->eq[i][pos]))
7083 for (i = 0; i < bmap->n_ineq; ++i) {
7084 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7091 return lower && upper;
7094 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7095 enum isl_dim_type type, unsigned pos)
7102 for (i = 0; i < map->n; ++i) {
7104 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7105 if (bounded < 0 || !bounded)
7112 /* Return 1 if the specified dim is involved in both an upper bound
7113 * and a lower bound.
7115 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7116 enum isl_dim_type type, unsigned pos)
7118 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7121 /* For each of the "n" variables starting at "first", determine
7122 * the sign of the variable and put the results in the first "n"
7123 * elements of the array "signs".
7125 * 1 means that the variable is non-negative
7126 * -1 means that the variable is non-positive
7127 * 0 means the variable attains both positive and negative values.
7129 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7130 unsigned first, unsigned n, int *signs)
7132 isl_vec *bound = NULL;
7133 struct isl_tab *tab = NULL;
7134 struct isl_tab_undo *snap;
7137 if (!bset || !signs)
7140 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7141 tab = isl_tab_from_basic_set(bset);
7145 isl_seq_clr(bound->el, bound->size);
7146 isl_int_set_si(bound->el[0], -1);
7148 snap = isl_tab_snap(tab);
7149 for (i = 0; i < n; ++i) {
7152 isl_int_set_si(bound->el[1 + first + i], -1);
7153 if (isl_tab_add_ineq(tab, bound->el) < 0)
7156 isl_int_set_si(bound->el[1 + first + i], 0);
7157 if (isl_tab_rollback(tab, snap) < 0)
7165 isl_int_set_si(bound->el[1 + first + i], 1);
7166 if (isl_tab_add_ineq(tab, bound->el) < 0)
7169 isl_int_set_si(bound->el[1 + first + i], 0);
7170 if (isl_tab_rollback(tab, snap) < 0)
7173 signs[i] = empty ? -1 : 0;
7177 isl_vec_free(bound);
7181 isl_vec_free(bound);
7185 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7186 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7188 if (!bset || !signs)
7190 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7193 first += pos(bset->dim, type) - 1;
7194 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7197 /* Check if the given map is single-valued.
7202 * and check if the result is a subset of the identity mapping.
7204 int isl_map_is_single_valued(__isl_keep isl_map *map)
7210 test = isl_map_reverse(isl_map_copy(map));
7211 test = isl_map_apply_range(test, isl_map_copy(map));
7213 id = isl_map_identity(isl_dim_range(isl_map_get_dim(map)));
7215 sv = isl_map_is_subset(test, id);
7223 int isl_map_is_bijective(__isl_keep isl_map *map)
7227 sv = isl_map_is_single_valued(map);
7231 map = isl_map_copy(map);
7232 map = isl_map_reverse(map);
7233 sv = isl_map_is_single_valued(map);
7239 int isl_set_is_singleton(__isl_keep isl_set *set)
7241 return isl_map_is_single_valued((isl_map *)set);
7244 int isl_map_is_translation(__isl_keep isl_map *map)
7249 delta = isl_map_deltas(isl_map_copy(map));
7250 ok = isl_set_is_singleton(delta);
7251 isl_set_free(delta);
7256 static int unique(isl_int *p, unsigned pos, unsigned len)
7258 if (isl_seq_first_non_zero(p, pos) != -1)
7260 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7265 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7274 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7277 nvar = isl_basic_set_dim(bset, isl_dim_set);
7278 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7279 for (j = 0; j < nvar; ++j) {
7280 int lower = 0, upper = 0;
7281 for (i = 0; i < bset->n_eq; ++i) {
7282 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7284 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7290 for (i = 0; i < bset->n_ineq; ++i) {
7291 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7293 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7295 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7300 if (!lower || !upper)
7307 int isl_set_is_box(__isl_keep isl_set *set)
7314 return isl_basic_set_is_box(set->p[0]);
7317 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7322 return isl_dim_is_wrapping(bset->dim);
7325 int isl_set_is_wrapping(__isl_keep isl_set *set)
7330 return isl_dim_is_wrapping(set->dim);
7333 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7335 bmap = isl_basic_map_cow(bmap);
7339 bmap->dim = isl_dim_wrap(bmap->dim);
7343 bmap = isl_basic_map_finalize(bmap);
7345 return (isl_basic_set *)bmap;
7347 isl_basic_map_free(bmap);
7351 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7355 map = isl_map_cow(map);
7359 for (i = 0; i < map->n; ++i) {
7360 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7364 map->dim = isl_dim_wrap(map->dim);
7368 return (isl_set *)map;
7374 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
7376 bset = isl_basic_set_cow(bset);
7380 bset->dim = isl_dim_unwrap(bset->dim);
7384 bset = isl_basic_set_finalize(bset);
7386 return (isl_basic_map *)bset;
7388 isl_basic_set_free(bset);
7392 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
7399 if (!isl_set_is_wrapping(set))
7400 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
7403 set = isl_set_cow(set);
7407 for (i = 0; i < set->n; ++i) {
7408 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
7413 set->dim = isl_dim_unwrap(set->dim);
7417 return (isl_map *)set;
7423 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
7424 enum isl_dim_type type)
7429 if (!isl_dim_is_named_or_nested(bmap->dim, type))
7432 bmap = isl_basic_map_cow(bmap);
7436 bmap->dim = isl_dim_reset(bmap->dim, type);
7440 bmap = isl_basic_map_finalize(bmap);
7444 isl_basic_map_free(bmap);
7448 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
7449 enum isl_dim_type type)
7456 if (!isl_dim_is_named_or_nested(map->dim, type))
7459 map = isl_map_cow(map);
7463 for (i = 0; i < map->n; ++i) {
7464 map->p[i] = isl_basic_map_reset(map->p[i], type);
7468 map->dim = isl_dim_reset(map->dim, type);
7478 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
7483 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
7486 bmap = isl_basic_map_cow(bmap);
7490 bmap->dim = isl_dim_flatten(bmap->dim);
7494 bmap = isl_basic_map_finalize(bmap);
7498 isl_basic_map_free(bmap);
7502 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
7509 if (!map->dim->nested[0] && !map->dim->nested[1])
7512 map = isl_map_cow(map);
7516 for (i = 0; i < map->n; ++i) {
7517 map->p[i] = isl_basic_map_flatten(map->p[i]);
7521 map->dim = isl_dim_flatten(map->dim);
7531 /* Extend the given dim_map with mappings for the divs in bmap.
7533 static __isl_give struct isl_dim_map *extend_dim_map(
7534 __isl_keep struct isl_dim_map *dim_map,
7535 __isl_keep isl_basic_map *bmap)
7538 struct isl_dim_map *res;
7541 offset = isl_basic_map_offset(bmap, isl_dim_div);
7543 res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
7547 for (i = 0; i < dim_map->len; ++i)
7548 res->pos[i] = dim_map->pos[i];
7549 for (i = 0; i < bmap->n_div; ++i)
7550 res->pos[dim_map->len + i] = offset + i;
7555 /* Extract a dim_map from a reordering.
7556 * We essentially need to reverse the mapping, and add an offset
7557 * of 1 for the constant term.
7559 __isl_give struct isl_dim_map *isl_dim_map_from_reordering(
7560 __isl_keep isl_reordering *exp)
7563 struct isl_dim_map *dim_map;
7568 dim_map = isl_dim_map_alloc(exp->dim->ctx, isl_dim_total(exp->dim));
7572 for (i = 0; i < exp->len; ++i)
7573 dim_map->pos[1 + exp->pos[i]] = 1 + i;
7578 /* Reorder the dimensions of "bmap" according to the given dim_map
7579 * and set the dimension specification to "dim".
7581 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
7582 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
7586 bmap = isl_basic_map_cow(bmap);
7587 if (!bmap || !dim || !dim_map)
7590 res = isl_basic_map_alloc_dim(dim,
7591 bmap->n_div, bmap->n_eq, bmap->n_ineq);
7592 res = add_constraints_dim_map(res, bmap, dim_map);
7593 res = isl_basic_map_finalize(res);
7597 isl_basic_map_free(bmap);
7602 /* Reorder the dimensions of "map" according to given reordering.
7604 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
7605 __isl_take isl_reordering *r)
7608 struct isl_dim_map *dim_map;
7610 map = isl_map_cow(map);
7611 dim_map = isl_dim_map_from_reordering(r);
7612 if (!map || !r || !dim_map)
7615 for (i = 0; i < map->n; ++i) {
7616 struct isl_dim_map *dim_map_i;
7618 dim_map_i = extend_dim_map(dim_map, map->p[i]);
7620 map->p[i] = isl_basic_map_realign(map->p[i],
7621 isl_dim_copy(r->dim), dim_map_i);
7627 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
7629 isl_reordering_free(r);
7635 isl_reordering_free(r);
7639 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
7640 __isl_take isl_reordering *r)
7642 return (isl_set *)isl_map_realign((isl_map *)set, r);
7645 __isl_give isl_mat *isl_basic_map_equalities_matrix(
7646 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
7647 enum isl_dim_type c2, enum isl_dim_type c3,
7648 enum isl_dim_type c4, enum isl_dim_type c5)
7650 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
7651 struct isl_mat *mat;
7657 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
7658 isl_basic_map_total_dim(bmap) + 1);
7661 for (i = 0; i < bmap->n_eq; ++i)
7662 for (j = 0, pos = 0; j < 5; ++j) {
7663 int off = isl_basic_map_offset(bmap, c[j]);
7664 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7665 isl_int_set(mat->row[i][pos],
7666 bmap->eq[i][off + k]);
7674 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
7675 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
7676 enum isl_dim_type c2, enum isl_dim_type c3,
7677 enum isl_dim_type c4, enum isl_dim_type c5)
7679 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
7680 struct isl_mat *mat;
7686 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
7687 isl_basic_map_total_dim(bmap) + 1);
7690 for (i = 0; i < bmap->n_ineq; ++i)
7691 for (j = 0, pos = 0; j < 5; ++j) {
7692 int off = isl_basic_map_offset(bmap, c[j]);
7693 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7694 isl_int_set(mat->row[i][pos],
7695 bmap->ineq[i][off + k]);
7703 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
7704 __isl_take isl_dim *dim,
7705 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, 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 isl_basic_map *bmap;
7716 if (!dim || !eq || !ineq)
7719 if (eq->n_col != ineq->n_col)
7720 isl_die(dim->ctx, isl_error_invalid,
7721 "equalities and inequalities matrices should have "
7722 "same number of columns", goto error);
7724 total = 1 + isl_dim_total(dim);
7726 if (eq->n_col < total)
7727 isl_die(dim->ctx, isl_error_invalid,
7728 "number of columns too small", goto error);
7730 extra = eq->n_col - total;
7732 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
7733 eq->n_row, ineq->n_row);
7736 for (i = 0; i < extra; ++i)
7737 if (isl_basic_map_alloc_div(bmap) < 0)
7739 for (i = 0; i < eq->n_row; ++i) {
7740 l = isl_basic_map_alloc_equality(bmap);
7743 for (j = 0, pos = 0; j < 5; ++j) {
7744 int off = isl_basic_map_offset(bmap, c[j]);
7745 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7746 isl_int_set(bmap->eq[l][off + k],
7752 for (i = 0; i < ineq->n_row; ++i) {
7753 l = isl_basic_map_alloc_inequality(bmap);
7756 for (j = 0, pos = 0; j < 5; ++j) {
7757 int off = isl_basic_map_offset(bmap, c[j]);
7758 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
7759 isl_int_set(bmap->ineq[l][off + k],
7778 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
7779 __isl_take isl_dim *dim,
7780 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
7781 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
7783 return (isl_basic_set*)
7784 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
7785 c1, c2, c3, c4, isl_dim_in);