2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
18 #include "isl_dim_private.h"
19 #include "isl_equalities.h"
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);
326 bmap = isl_basic_map_finalize(bmap);
329 isl_basic_map_free(bmap);
333 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
334 enum isl_dim_type type)
336 return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
339 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
340 enum isl_dim_type type, const char *s)
344 map = isl_map_cow(map);
348 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
352 for (i = 0; i < map->n; ++i) {
353 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
364 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
365 enum isl_dim_type type)
367 return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
370 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
373 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
376 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
378 return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
381 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
383 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
386 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
387 enum isl_dim_type type, unsigned pos)
389 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
392 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
393 enum isl_dim_type type, unsigned pos)
395 return bset ? isl_dim_get_name(bset->dim, type, pos) : NULL;
398 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
399 enum isl_dim_type type, unsigned pos)
401 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
404 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
405 enum isl_dim_type type, unsigned pos)
407 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
410 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
411 __isl_take isl_basic_map *bmap,
412 enum isl_dim_type type, unsigned pos, const char *s)
416 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
421 isl_basic_map_free(bmap);
425 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
426 enum isl_dim_type type, unsigned pos, const char *s)
433 map->dim = isl_dim_set_name(map->dim, type, pos, s);
437 for (i = 0; i < map->n; ++i) {
438 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
449 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
450 __isl_take isl_basic_set *bset,
451 enum isl_dim_type type, unsigned pos, const char *s)
453 return (isl_basic_set *)isl_basic_map_set_dim_name(
454 (isl_basic_map *)bset, type, pos, s);
457 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
458 enum isl_dim_type type, unsigned pos, const char *s)
460 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
463 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
467 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
470 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
472 return isl_basic_map_is_rational(bset);
475 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
476 struct isl_basic_map *bmap, unsigned extra,
477 unsigned n_eq, unsigned n_ineq)
480 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
485 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
486 if (isl_blk_is_error(bmap->block))
489 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
494 bmap->block2 = isl_blk_empty();
497 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
498 if (isl_blk_is_error(bmap->block2))
501 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
506 for (i = 0; i < n_ineq + n_eq; ++i)
507 bmap->ineq[i] = bmap->block.data + i * row_size;
509 for (i = 0; i < extra; ++i)
510 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
514 bmap->c_size = n_eq + n_ineq;
515 bmap->eq = bmap->ineq + n_ineq;
524 isl_basic_map_free(bmap);
528 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
529 unsigned nparam, unsigned dim, unsigned extra,
530 unsigned n_eq, unsigned n_ineq)
532 struct isl_basic_map *bmap;
533 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
534 return (struct isl_basic_set *)bmap;
537 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
538 unsigned extra, unsigned n_eq, unsigned n_ineq)
540 struct isl_basic_map *bmap;
543 isl_assert(dim->ctx, dim->n_in == 0, goto error);
544 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
545 return (struct isl_basic_set *)bmap;
551 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
552 unsigned extra, unsigned n_eq, unsigned n_ineq)
554 struct isl_basic_map *bmap;
558 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
563 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
569 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
570 unsigned nparam, unsigned in, unsigned out, unsigned extra,
571 unsigned n_eq, unsigned n_ineq)
573 struct isl_basic_map *bmap;
576 dim = isl_dim_alloc(ctx, nparam, in, out);
580 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
584 static void dup_constraints(
585 struct isl_basic_map *dst, struct isl_basic_map *src)
588 unsigned total = isl_basic_map_total_dim(src);
590 for (i = 0; i < src->n_eq; ++i) {
591 int j = isl_basic_map_alloc_equality(dst);
592 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
595 for (i = 0; i < src->n_ineq; ++i) {
596 int j = isl_basic_map_alloc_inequality(dst);
597 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
600 for (i = 0; i < src->n_div; ++i) {
601 int j = isl_basic_map_alloc_div(dst);
602 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
604 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
607 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
609 struct isl_basic_map *dup;
613 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
614 bmap->n_div, bmap->n_eq, bmap->n_ineq);
617 dup_constraints(dup, bmap);
618 dup->flags = bmap->flags;
619 dup->sample = isl_vec_copy(bmap->sample);
623 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
625 struct isl_basic_map *dup;
627 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
628 return (struct isl_basic_set *)dup;
631 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
636 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
640 return isl_basic_set_dup(bset);
643 struct isl_set *isl_set_copy(struct isl_set *set)
652 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
657 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
661 bmap = isl_basic_map_dup(bmap);
663 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
667 struct isl_map *isl_map_copy(struct isl_map *map)
676 void isl_basic_map_free(struct isl_basic_map *bmap)
684 isl_ctx_deref(bmap->ctx);
686 isl_blk_free(bmap->ctx, bmap->block2);
688 isl_blk_free(bmap->ctx, bmap->block);
689 isl_vec_free(bmap->sample);
690 isl_dim_free(bmap->dim);
694 void isl_basic_set_free(struct isl_basic_set *bset)
696 isl_basic_map_free((struct isl_basic_map *)bset);
699 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
701 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
704 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
710 isl_assert(ctx, room_for_con(bmap, 1), return -1);
711 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
713 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
714 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
715 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
716 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
717 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
718 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
720 int j = isl_basic_map_alloc_inequality(bmap);
724 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
725 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
732 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
733 bmap->extra - bmap->n_div);
737 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
739 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
742 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
746 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
751 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
753 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
756 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
761 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
763 if (pos != bmap->n_eq - 1) {
765 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
766 bmap->eq[bmap->n_eq - 1] = t;
772 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
774 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
777 void isl_basic_map_inequality_to_equality(
778 struct isl_basic_map *bmap, unsigned pos)
783 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
784 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
789 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
790 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
791 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
792 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
795 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
797 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
800 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
806 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
807 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
808 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
809 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
810 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
811 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
812 1 + isl_basic_map_total_dim(bmap),
813 bmap->extra - bmap->n_div);
814 return bmap->n_ineq++;
817 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
819 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
822 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
826 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
831 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
833 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
836 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
841 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
843 if (pos != bmap->n_ineq - 1) {
845 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
846 bmap->ineq[bmap->n_ineq - 1] = t;
847 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
853 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
855 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
858 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
863 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
866 k = isl_basic_map_alloc_equality(bmap);
869 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
872 isl_basic_map_free(bmap);
876 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
879 return (isl_basic_set *)
880 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
883 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
888 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
891 k = isl_basic_map_alloc_inequality(bmap);
894 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
897 isl_basic_map_free(bmap);
901 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
904 return (isl_basic_set *)
905 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
908 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
912 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
913 isl_seq_clr(bmap->div[bmap->n_div] +
914 1 + 1 + isl_basic_map_total_dim(bmap),
915 bmap->extra - bmap->n_div);
916 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
917 return bmap->n_div++;
920 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
922 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
925 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
929 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
934 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
936 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
939 /* Copy constraint from src to dst, putting the vars of src at offset
940 * dim_off in dst and the divs of src at offset div_off in dst.
941 * If both sets are actually map, then dim_off applies to the input
944 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
945 struct isl_basic_map *src_map, isl_int *src,
946 unsigned in_off, unsigned out_off, unsigned div_off)
948 unsigned src_nparam = isl_basic_map_n_param(src_map);
949 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
950 unsigned src_in = isl_basic_map_n_in(src_map);
951 unsigned dst_in = isl_basic_map_n_in(dst_map);
952 unsigned src_out = isl_basic_map_n_out(src_map);
953 unsigned dst_out = isl_basic_map_n_out(dst_map);
954 isl_int_set(dst[0], src[0]);
955 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
956 if (dst_nparam > src_nparam)
957 isl_seq_clr(dst+1+src_nparam,
958 dst_nparam - src_nparam);
959 isl_seq_clr(dst+1+dst_nparam, in_off);
960 isl_seq_cpy(dst+1+dst_nparam+in_off,
962 isl_min(dst_in-in_off, src_in));
963 if (dst_in-in_off > src_in)
964 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
965 dst_in - in_off - src_in);
966 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
967 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
968 src+1+src_nparam+src_in,
969 isl_min(dst_out-out_off, src_out));
970 if (dst_out-out_off > src_out)
971 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
972 dst_out - out_off - src_out);
973 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
974 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
975 src+1+src_nparam+src_in+src_out,
976 isl_min(dst_map->extra-div_off, src_map->n_div));
977 if (dst_map->n_div-div_off > src_map->n_div)
978 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
979 div_off+src_map->n_div,
980 dst_map->n_div - div_off - src_map->n_div);
983 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
984 struct isl_basic_map *src_map, isl_int *src,
985 unsigned in_off, unsigned out_off, unsigned div_off)
987 isl_int_set(dst[0], src[0]);
988 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
991 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
992 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
997 if (!bmap1 || !bmap2)
1000 div_off = bmap1->n_div;
1002 for (i = 0; i < bmap2->n_eq; ++i) {
1003 int i1 = isl_basic_map_alloc_equality(bmap1);
1006 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1007 i_pos, o_pos, div_off);
1010 for (i = 0; i < bmap2->n_ineq; ++i) {
1011 int i1 = isl_basic_map_alloc_inequality(bmap1);
1014 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1015 i_pos, o_pos, div_off);
1018 for (i = 0; i < bmap2->n_div; ++i) {
1019 int i1 = isl_basic_map_alloc_div(bmap1);
1022 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1023 i_pos, o_pos, div_off);
1026 isl_basic_map_free(bmap2);
1031 isl_basic_map_free(bmap1);
1032 isl_basic_map_free(bmap2);
1036 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
1037 struct isl_dim_map *dim_map)
1041 for (i = 0; i < dim_map->len; ++i) {
1042 if (dim_map->pos[i] < 0)
1043 isl_int_set_si(dst[i], 0);
1045 isl_int_set(dst[i], src[dim_map->pos[i]]);
1049 static void copy_div_dim_map(isl_int *dst, isl_int *src,
1050 struct isl_dim_map *dim_map)
1052 isl_int_set(dst[0], src[0]);
1053 copy_constraint_dim_map(dst+1, src+1, dim_map);
1056 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
1057 struct isl_basic_map *src, struct isl_dim_map *dim_map)
1061 if (!src || !dst || !dim_map)
1064 for (i = 0; i < src->n_eq; ++i) {
1065 int i1 = isl_basic_map_alloc_equality(dst);
1068 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
1071 for (i = 0; i < src->n_ineq; ++i) {
1072 int i1 = isl_basic_map_alloc_inequality(dst);
1075 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
1078 for (i = 0; i < src->n_div; ++i) {
1079 int i1 = isl_basic_map_alloc_div(dst);
1082 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
1086 isl_basic_map_free(src);
1091 isl_basic_map_free(src);
1092 isl_basic_map_free(dst);
1096 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1097 struct isl_basic_set *bset2, unsigned pos)
1099 return (struct isl_basic_set *)
1100 add_constraints((struct isl_basic_map *)bset1,
1101 (struct isl_basic_map *)bset2, 0, pos);
1104 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1105 struct isl_dim *dim, unsigned extra,
1106 unsigned n_eq, unsigned n_ineq)
1108 struct isl_basic_map *ext;
1118 dims_ok = isl_dim_equal(base->dim, dim) &&
1119 base->extra >= base->n_div + extra;
1121 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1122 room_for_ineq(base, n_ineq)) {
1127 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1128 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1129 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1130 extra += base->extra;
1132 n_ineq += base->n_ineq;
1134 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1140 ext->sample = isl_vec_copy(base->sample);
1141 flags = base->flags;
1142 ext = add_constraints(ext, base, 0, 0);
1145 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1152 isl_basic_map_free(base);
1156 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1157 struct isl_dim *dim, unsigned extra,
1158 unsigned n_eq, unsigned n_ineq)
1160 return (struct isl_basic_set *)
1161 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1162 extra, n_eq, n_ineq);
1165 struct isl_basic_map *isl_basic_map_extend_constraints(
1166 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1170 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1174 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1175 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1176 unsigned n_eq, unsigned n_ineq)
1178 struct isl_basic_map *bmap;
1179 struct isl_dim *dim;
1183 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1187 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1190 isl_basic_map_free(base);
1194 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1195 unsigned nparam, unsigned dim, unsigned extra,
1196 unsigned n_eq, unsigned n_ineq)
1198 return (struct isl_basic_set *)
1199 isl_basic_map_extend((struct isl_basic_map *)base,
1200 nparam, 0, dim, extra, n_eq, n_ineq);
1203 struct isl_basic_set *isl_basic_set_extend_constraints(
1204 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1206 return (struct isl_basic_set *)
1207 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1211 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1213 return (struct isl_basic_set *)
1214 isl_basic_map_cow((struct isl_basic_map *)bset);
1217 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1222 if (bmap->ref > 1) {
1224 bmap = isl_basic_map_dup(bmap);
1227 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1231 struct isl_set *isl_set_cow(struct isl_set *set)
1239 return isl_set_dup(set);
1242 struct isl_map *isl_map_cow(struct isl_map *map)
1250 return isl_map_dup(map);
1253 static void swap_vars(struct isl_blk blk, isl_int *a,
1254 unsigned a_len, unsigned b_len)
1256 isl_seq_cpy(blk.data, a+a_len, b_len);
1257 isl_seq_cpy(blk.data+b_len, a, a_len);
1258 isl_seq_cpy(a, blk.data, b_len+a_len);
1261 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1262 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1270 isl_assert(bmap->ctx,
1271 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1273 if (n1 == 0 || n2 == 0)
1276 bmap = isl_basic_map_cow(bmap);
1280 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1281 if (isl_blk_is_error(blk))
1284 for (i = 0; i < bmap->n_eq; ++i)
1286 bmap->eq[i] + pos, n1, n2);
1288 for (i = 0; i < bmap->n_ineq; ++i)
1290 bmap->ineq[i] + pos, n1, n2);
1292 for (i = 0; i < bmap->n_div; ++i)
1294 bmap->div[i]+1 + pos, n1, n2);
1296 isl_blk_free(bmap->ctx, blk);
1298 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1299 bmap = isl_basic_map_gauss(bmap, NULL);
1300 return isl_basic_map_finalize(bmap);
1302 isl_basic_map_free(bmap);
1306 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1307 __isl_take isl_basic_set *bset, unsigned n)
1312 nparam = isl_basic_set_n_param(bset);
1313 dim = isl_basic_set_n_dim(bset);
1314 isl_assert(bset->ctx, n <= dim, goto error);
1316 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1318 isl_basic_set_free(bset);
1322 static __isl_give isl_set *isl_set_swap_vars(__isl_take isl_set *set,
1326 set = isl_set_cow(set);
1330 for (i = 0; i < set->n; ++i) {
1331 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1337 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1341 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1347 total = isl_basic_map_total_dim(bmap);
1348 isl_basic_map_free_div(bmap, bmap->n_div);
1349 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1351 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1353 i = isl_basic_map_alloc_equality(bmap);
1357 isl_int_set_si(bmap->eq[i][0], 1);
1358 isl_seq_clr(bmap->eq[i]+1, total);
1359 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1360 isl_vec_free(bmap->sample);
1361 bmap->sample = NULL;
1362 return isl_basic_map_finalize(bmap);
1364 isl_basic_map_free(bmap);
1368 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1370 return (struct isl_basic_set *)
1371 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1374 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1377 unsigned off = isl_dim_total(bmap->dim);
1378 isl_int *t = bmap->div[a];
1379 bmap->div[a] = bmap->div[b];
1382 for (i = 0; i < bmap->n_eq; ++i)
1383 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1385 for (i = 0; i < bmap->n_ineq; ++i)
1386 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1388 for (i = 0; i < bmap->n_div; ++i)
1389 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1390 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1393 /* Eliminate the specified n dimensions starting at first from the
1394 * constraints using Fourier-Motzkin. The dimensions themselves
1397 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1398 enum isl_dim_type type, unsigned first, unsigned n)
1407 map = isl_map_cow(map);
1410 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1411 first += pos(map->dim, type) - 1;
1413 for (i = 0; i < map->n; ++i) {
1414 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1424 /* Eliminate the specified n dimensions starting at first from the
1425 * constraints using Fourier-Motzkin. The dimensions themselves
1428 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1429 enum isl_dim_type type, unsigned first, unsigned n)
1431 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1434 /* Eliminate the specified n dimensions starting at first from the
1435 * constraints using Fourier-Motzkin. The dimensions themselves
1438 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1439 unsigned first, unsigned n)
1441 return isl_set_eliminate(set, isl_dim_set, first, n);
1444 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1445 __isl_take isl_basic_map *bmap)
1447 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1452 return isl_basic_map_finalize(bmap);
1455 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1456 __isl_take isl_basic_set *bset)
1458 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1459 (struct isl_basic_map *)bset);
1462 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1471 set = isl_set_cow(set);
1475 for (i = 0; i < set->n; ++i) {
1476 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1486 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1487 enum isl_dim_type type, unsigned first, unsigned n)
1491 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1495 bmap = isl_basic_map_eliminate_vars(bmap,
1496 isl_basic_map_offset(bmap, type) - 1 + first, n);
1499 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1501 bmap = isl_basic_map_drop(bmap, type, first, n);
1504 isl_basic_map_free(bmap);
1508 /* Return true if the definition of the given div (recursively) involves
1509 * any of the given variables.
1511 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1512 unsigned first, unsigned n)
1515 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1517 if (isl_int_is_zero(bmap->div[div][0]))
1519 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1522 for (i = bmap->n_div - 1; i >= 0; --i) {
1523 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1525 if (div_involves_vars(bmap, i, first, n))
1532 /* Remove all divs (recursively) involving any of the given dimensions
1533 * in their definitions.
1535 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1536 __isl_take isl_basic_map *bmap,
1537 enum isl_dim_type type, unsigned first, unsigned n)
1543 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1545 first += isl_basic_map_offset(bmap, type);
1547 for (i = bmap->n_div - 1; i >= 0; --i) {
1548 if (!div_involves_vars(bmap, i, first, n))
1550 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1558 isl_basic_map_free(bmap);
1562 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1563 enum isl_dim_type type, unsigned first, unsigned n)
1572 map = isl_map_cow(map);
1576 for (i = 0; i < map->n; ++i) {
1577 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1588 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1589 enum isl_dim_type type, unsigned first, unsigned n)
1591 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1595 /* Return true if the definition of the given div is unknown or depends
1598 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1601 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1603 if (isl_int_is_zero(bmap->div[div][0]))
1606 for (i = bmap->n_div - 1; i >= 0; --i) {
1607 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1609 if (div_is_unknown(bmap, i))
1616 /* Remove all divs that are unknown or defined in terms of unknown divs.
1618 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1619 __isl_take isl_basic_map *bmap)
1626 for (i = bmap->n_div - 1; i >= 0; --i) {
1627 if (!div_is_unknown(bmap, i))
1629 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1635 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1644 map = isl_map_cow(map);
1648 for (i = 0; i < map->n; ++i) {
1649 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1659 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1661 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1664 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1665 __isl_take isl_basic_set *bset,
1666 enum isl_dim_type type, unsigned first, unsigned n)
1668 return (isl_basic_set *)
1669 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1672 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1673 enum isl_dim_type type, unsigned first, unsigned n)
1680 map = isl_map_cow(map);
1683 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1685 for (i = 0; i < map->n; ++i) {
1686 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1687 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1691 map = isl_map_drop(map, type, first, n);
1698 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1699 enum isl_dim_type type, unsigned first, unsigned n)
1701 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1704 /* Project out n inputs starting at first using Fourier-Motzkin */
1705 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1706 unsigned first, unsigned n)
1708 return isl_map_remove_dims(map, isl_dim_in, first, n);
1711 static void dump_term(struct isl_basic_map *bmap,
1712 isl_int c, int pos, FILE *out)
1715 unsigned in = isl_basic_map_n_in(bmap);
1716 unsigned dim = in + isl_basic_map_n_out(bmap);
1717 unsigned nparam = isl_basic_map_n_param(bmap);
1719 isl_int_print(out, c, 0);
1721 if (!isl_int_is_one(c))
1722 isl_int_print(out, c, 0);
1723 if (pos < 1 + nparam) {
1724 name = isl_dim_get_name(bmap->dim,
1725 isl_dim_param, pos - 1);
1727 fprintf(out, "%s", name);
1729 fprintf(out, "p%d", pos - 1);
1730 } else if (pos < 1 + nparam + in)
1731 fprintf(out, "i%d", pos - 1 - nparam);
1732 else if (pos < 1 + nparam + dim)
1733 fprintf(out, "o%d", pos - 1 - nparam - in);
1735 fprintf(out, "e%d", pos - 1 - nparam - dim);
1739 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1740 int sign, FILE *out)
1744 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1748 for (i = 0, first = 1; i < len; ++i) {
1749 if (isl_int_sgn(c[i]) * sign <= 0)
1752 fprintf(out, " + ");
1754 isl_int_abs(v, c[i]);
1755 dump_term(bmap, v, i, out);
1762 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1763 const char *op, FILE *out, int indent)
1767 fprintf(out, "%*s", indent, "");
1769 dump_constraint_sign(bmap, c, 1, out);
1770 fprintf(out, " %s ", op);
1771 dump_constraint_sign(bmap, c, -1, out);
1775 for (i = bmap->n_div; i < bmap->extra; ++i) {
1776 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1778 fprintf(out, "%*s", indent, "");
1779 fprintf(out, "ERROR: unused div coefficient not zero\n");
1784 static void dump_constraints(struct isl_basic_map *bmap,
1785 isl_int **c, unsigned n,
1786 const char *op, FILE *out, int indent)
1790 for (i = 0; i < n; ++i)
1791 dump_constraint(bmap, c[i], op, out, indent);
1794 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1798 unsigned total = isl_basic_map_total_dim(bmap);
1800 for (j = 0; j < 1 + total; ++j) {
1801 if (isl_int_is_zero(exp[j]))
1803 if (!first && isl_int_is_pos(exp[j]))
1805 dump_term(bmap, exp[j], j, out);
1810 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1814 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1815 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1817 for (i = 0; i < bmap->n_div; ++i) {
1818 fprintf(out, "%*s", indent, "");
1819 fprintf(out, "e%d = [(", i);
1820 dump_affine(bmap, bmap->div[i]+1, out);
1822 isl_int_print(out, bmap->div[i][0], 0);
1823 fprintf(out, "]\n");
1827 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1828 FILE *out, int indent)
1831 fprintf(out, "null basic set\n");
1835 fprintf(out, "%*s", indent, "");
1836 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1837 bset->ref, bset->dim->nparam, bset->dim->n_out,
1838 bset->extra, bset->flags);
1839 dump((struct isl_basic_map *)bset, out, indent);
1842 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1843 FILE *out, int indent)
1846 fprintf(out, "null basic map\n");
1850 fprintf(out, "%*s", indent, "");
1851 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1852 "flags: %x, n_name: %d\n",
1854 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1855 bmap->extra, bmap->flags, bmap->dim->n_name);
1856 dump(bmap, out, indent);
1859 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1864 total = isl_basic_map_total_dim(bmap);
1865 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1866 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1867 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1868 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1872 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1874 struct isl_set *set;
1878 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1879 isl_assert(dim->ctx, n >= 0, goto error);
1880 set = isl_alloc(dim->ctx, struct isl_set,
1881 sizeof(struct isl_set) +
1882 (n - 1) * sizeof(struct isl_basic_set *));
1886 set->ctx = dim->ctx;
1887 isl_ctx_ref(set->ctx);
1899 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1900 unsigned nparam, unsigned dim, int n, unsigned flags)
1902 struct isl_set *set;
1903 struct isl_dim *dims;
1905 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1909 set = isl_set_alloc_dim(dims, n, flags);
1913 /* Make sure "map" has room for at least "n" more basic maps.
1915 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1918 struct isl_map *grown = NULL;
1922 isl_assert(map->ctx, n >= 0, goto error);
1923 if (map->n + n <= map->size)
1925 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1928 for (i = 0; i < map->n; ++i) {
1929 grown->p[i] = isl_basic_map_copy(map->p[i]);
1937 isl_map_free(grown);
1942 /* Make sure "set" has room for at least "n" more basic sets.
1944 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1946 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1949 struct isl_set *isl_set_dup(struct isl_set *set)
1952 struct isl_set *dup;
1957 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1960 for (i = 0; i < set->n; ++i)
1961 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1965 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1967 return isl_map_from_basic_map(bset);
1970 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1972 struct isl_map *map;
1977 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1978 return isl_map_add_basic_map(map, bmap);
1981 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1982 __isl_take isl_basic_set *bset)
1984 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1985 (struct isl_basic_map *)bset);
1988 void isl_set_free(struct isl_set *set)
1998 isl_ctx_deref(set->ctx);
1999 for (i = 0; i < set->n; ++i)
2000 isl_basic_set_free(set->p[i]);
2001 isl_dim_free(set->dim);
2005 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2010 fprintf(out, "null set\n");
2014 fprintf(out, "%*s", indent, "");
2015 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2016 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2018 for (i = 0; i < set->n; ++i) {
2019 fprintf(out, "%*s", indent, "");
2020 fprintf(out, "basic set %d:\n", i);
2021 isl_basic_set_print_internal(set->p[i], out, indent+4);
2025 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2030 fprintf(out, "null map\n");
2034 fprintf(out, "%*s", indent, "");
2035 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2036 "flags: %x, n_name: %d\n",
2037 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2038 map->dim->n_out, map->flags, map->dim->n_name);
2039 for (i = 0; i < map->n; ++i) {
2040 fprintf(out, "%*s", indent, "");
2041 fprintf(out, "basic map %d:\n", i);
2042 isl_basic_map_print_internal(map->p[i], out, indent+4);
2046 struct isl_basic_map *isl_basic_map_intersect_domain(
2047 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2049 struct isl_basic_map *bmap_domain;
2054 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2055 bset->dim, isl_dim_param), goto error);
2057 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2058 isl_assert(bset->ctx,
2059 isl_basic_map_compatible_domain(bmap, bset), goto error);
2061 bmap = isl_basic_map_cow(bmap);
2064 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2065 bset->n_div, bset->n_eq, bset->n_ineq);
2066 bmap_domain = isl_basic_map_from_domain(bset);
2067 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2069 bmap = isl_basic_map_simplify(bmap);
2070 return isl_basic_map_finalize(bmap);
2072 isl_basic_map_free(bmap);
2073 isl_basic_set_free(bset);
2077 struct isl_basic_map *isl_basic_map_intersect_range(
2078 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2080 struct isl_basic_map *bmap_range;
2085 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2086 bset->dim, isl_dim_param), goto error);
2088 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2089 isl_assert(bset->ctx,
2090 isl_basic_map_compatible_range(bmap, bset), goto error);
2092 bmap = isl_basic_map_cow(bmap);
2095 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2096 bset->n_div, bset->n_eq, bset->n_ineq);
2097 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2098 bmap = add_constraints(bmap, bmap_range, 0, 0);
2100 bmap = isl_basic_map_simplify(bmap);
2101 return isl_basic_map_finalize(bmap);
2103 isl_basic_map_free(bmap);
2104 isl_basic_set_free(bset);
2108 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2114 total = 1 + isl_basic_map_total_dim(bmap);
2115 if (total != vec->size)
2120 for (i = 0; i < bmap->n_eq; ++i) {
2121 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2122 if (!isl_int_is_zero(s)) {
2128 for (i = 0; i < bmap->n_ineq; ++i) {
2129 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2130 if (isl_int_is_neg(s)) {
2141 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2143 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2146 struct isl_basic_map *isl_basic_map_intersect(
2147 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2149 struct isl_vec *sample = NULL;
2151 if (!bmap1 || !bmap2)
2154 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2155 bmap2->dim, isl_dim_param), goto error);
2156 if (isl_dim_total(bmap1->dim) ==
2157 isl_dim_size(bmap1->dim, isl_dim_param) &&
2158 isl_dim_total(bmap2->dim) !=
2159 isl_dim_size(bmap2->dim, isl_dim_param))
2160 return isl_basic_map_intersect(bmap2, bmap1);
2162 if (isl_dim_total(bmap2->dim) !=
2163 isl_dim_size(bmap2->dim, isl_dim_param))
2164 isl_assert(bmap1->ctx,
2165 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2167 if (bmap1->sample &&
2168 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2169 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2170 sample = isl_vec_copy(bmap1->sample);
2171 else if (bmap2->sample &&
2172 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2173 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2174 sample = isl_vec_copy(bmap2->sample);
2176 bmap1 = isl_basic_map_cow(bmap1);
2179 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2180 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2181 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2184 isl_vec_free(sample);
2186 isl_vec_free(bmap1->sample);
2187 bmap1->sample = sample;
2190 bmap1 = isl_basic_map_simplify(bmap1);
2191 return isl_basic_map_finalize(bmap1);
2194 isl_vec_free(sample);
2195 isl_basic_map_free(bmap1);
2196 isl_basic_map_free(bmap2);
2200 struct isl_basic_set *isl_basic_set_intersect(
2201 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2203 return (struct isl_basic_set *)
2204 isl_basic_map_intersect(
2205 (struct isl_basic_map *)bset1,
2206 (struct isl_basic_map *)bset2);
2209 /* Special case of isl_map_intersect, where both map1 and map2
2210 * are convex, without any divs and such that either map1 or map2
2211 * contains a single constraint. This constraint is then simply
2212 * added to the other map.
2214 static __isl_give isl_map *map_intersect_add_constraint(
2215 __isl_take isl_map *map1, __isl_take isl_map *map2)
2217 struct isl_basic_map *bmap1;
2218 struct isl_basic_map *bmap2;
2220 isl_assert(map1->ctx, map1->n == 1, goto error);
2221 isl_assert(map2->ctx, map1->n == 1, goto error);
2222 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2223 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2225 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2226 return isl_map_intersect(map2, map1);
2228 isl_assert(map2->ctx,
2229 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2231 map1 = isl_map_cow(map1);
2234 if (isl_map_fast_is_empty(map1)) {
2238 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2239 if (map2->p[0]->n_eq == 1)
2240 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2242 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2243 map2->p[0]->ineq[0]);
2245 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2246 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2250 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2251 isl_basic_map_free(map1->p[0]);
2264 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2267 struct isl_map *result;
2273 if (isl_map_fast_is_empty(map1)) {
2277 if (isl_map_fast_is_empty(map2)) {
2282 if (map1->n == 1 && map2->n == 1 &&
2283 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2284 isl_dim_equal(map1->dim, map2->dim) &&
2285 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2286 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2287 return map_intersect_add_constraint(map1, map2);
2288 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2289 map2->dim, isl_dim_param), goto error);
2290 if (isl_dim_total(map1->dim) ==
2291 isl_dim_size(map1->dim, isl_dim_param) &&
2292 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2293 return isl_map_intersect(map2, map1);
2295 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2296 isl_assert(map1->ctx,
2297 isl_dim_equal(map1->dim, map2->dim), goto error);
2299 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2300 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2301 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2303 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2304 map1->n * map2->n, flags);
2307 for (i = 0; i < map1->n; ++i)
2308 for (j = 0; j < map2->n; ++j) {
2309 struct isl_basic_map *part;
2310 part = isl_basic_map_intersect(
2311 isl_basic_map_copy(map1->p[i]),
2312 isl_basic_map_copy(map2->p[j]));
2313 if (isl_basic_map_is_empty(part))
2314 isl_basic_map_free(part);
2316 result = isl_map_add_basic_map(result, part);
2329 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2331 return (struct isl_set *)
2332 isl_map_intersect((struct isl_map *)set1,
2333 (struct isl_map *)set2);
2336 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2338 struct isl_dim *dim;
2339 struct isl_basic_set *bset;
2344 bmap = isl_basic_map_cow(bmap);
2347 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2348 in = isl_basic_map_n_in(bmap);
2349 bset = isl_basic_set_from_basic_map(bmap);
2350 bset = isl_basic_set_swap_vars(bset, in);
2351 return isl_basic_map_from_basic_set(bset, dim);
2354 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2355 enum isl_dim_type type, unsigned pos, unsigned n)
2357 struct isl_dim *res_dim;
2358 struct isl_basic_map *res;
2359 struct isl_dim_map *dim_map;
2360 unsigned total, off;
2361 enum isl_dim_type t;
2369 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2371 total = isl_basic_map_total_dim(bmap) + n;
2372 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2374 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2376 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2378 unsigned size = isl_basic_map_dim(bmap, t);
2379 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2381 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2382 pos, size - pos, off + pos + n);
2384 off += isl_dim_size(res_dim, t);
2386 isl_dim_map_div(dim_map, bmap, off);
2388 res = isl_basic_map_alloc_dim(res_dim,
2389 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2390 if (isl_basic_map_is_rational(bmap))
2391 res = isl_basic_map_set_rational(res);
2392 res = add_constraints_dim_map(res, bmap, dim_map);
2393 return isl_basic_map_finalize(res);
2396 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2397 enum isl_dim_type type, unsigned n)
2401 return isl_basic_map_insert(bmap, type,
2402 isl_basic_map_dim(bmap, type), n);
2405 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2406 enum isl_dim_type type, unsigned n)
2410 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2411 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2413 isl_basic_set_free(bset);
2417 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2418 enum isl_dim_type type, unsigned pos, unsigned n)
2425 map = isl_map_cow(map);
2429 map->dim = isl_dim_insert(map->dim, type, pos, n);
2433 for (i = 0; i < map->n; ++i) {
2434 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2445 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2446 enum isl_dim_type type, unsigned pos, unsigned n)
2448 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2451 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2452 enum isl_dim_type type, unsigned n)
2456 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2459 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2460 enum isl_dim_type type, unsigned n)
2464 isl_assert(set->ctx, type != isl_dim_in, goto error);
2465 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2471 __isl_give isl_basic_map *isl_basic_map_move_dims(
2472 __isl_take isl_basic_map *bmap,
2473 enum isl_dim_type dst_type, unsigned dst_pos,
2474 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2477 struct isl_dim_map *dim_map;
2478 struct isl_basic_map *res;
2479 enum isl_dim_type t;
2480 unsigned total, off;
2487 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2490 if (dst_type == src_type && dst_pos == src_pos)
2493 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2495 if (pos(bmap->dim, dst_type) + dst_pos ==
2496 pos(bmap->dim, src_type) + src_pos +
2497 ((src_type < dst_type) ? n : 0)) {
2498 bmap = isl_basic_map_cow(bmap);
2502 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2503 src_type, src_pos, n);
2507 bmap = isl_basic_map_finalize(bmap);
2512 total = isl_basic_map_total_dim(bmap);
2513 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2516 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2517 unsigned size = isl_dim_size(bmap->dim, t);
2518 if (t == dst_type) {
2519 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2522 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2525 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2526 dst_pos, size - dst_pos, off);
2527 off += size - dst_pos;
2528 } else if (t == src_type) {
2529 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2532 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2533 src_pos + n, size - src_pos - n, off);
2534 off += size - src_pos - n;
2536 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2540 isl_dim_map_div(dim_map, bmap, off);
2542 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2543 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2544 bmap = add_constraints_dim_map(res, bmap, dim_map);
2546 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2547 src_type, src_pos, n);
2551 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2552 bmap = isl_basic_map_gauss(bmap, NULL);
2553 bmap = isl_basic_map_finalize(bmap);
2557 isl_basic_map_free(bmap);
2561 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2562 enum isl_dim_type dst_type, unsigned dst_pos,
2563 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2565 return (isl_basic_set *)isl_basic_map_move_dims(
2566 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2569 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2570 enum isl_dim_type dst_type, unsigned dst_pos,
2571 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2575 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2576 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2577 src_type, src_pos, n);
2583 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2584 enum isl_dim_type dst_type, unsigned dst_pos,
2585 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2594 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2597 if (dst_type == src_type && dst_pos == src_pos)
2600 isl_assert(map->ctx, dst_type != src_type, goto error);
2602 map = isl_map_cow(map);
2606 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2610 for (i = 0; i < map->n; ++i) {
2611 map->p[i] = isl_basic_map_move_dims(map->p[i],
2613 src_type, src_pos, n);
2624 /* Move the specified dimensions to the last columns right before
2625 * the divs. Don't change the dimension specification of bmap.
2626 * That's the responsibility of the caller.
2628 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2629 enum isl_dim_type type, unsigned first, unsigned n)
2631 struct isl_dim_map *dim_map;
2632 struct isl_basic_map *res;
2633 enum isl_dim_type t;
2634 unsigned total, off;
2638 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2641 total = isl_basic_map_total_dim(bmap);
2642 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2645 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2646 unsigned size = isl_dim_size(bmap->dim, t);
2648 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2651 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2652 first, n, total - bmap->n_div - n);
2653 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2654 first + n, size - (first + n), off);
2655 off += size - (first + n);
2657 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2661 isl_dim_map_div(dim_map, bmap, off + n);
2663 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2664 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2665 res = add_constraints_dim_map(res, bmap, dim_map);
2669 /* Turn the n dimensions of type type, starting at first
2670 * into existentially quantified variables.
2672 __isl_give isl_basic_map *isl_basic_map_project_out(
2673 __isl_take isl_basic_map *bmap,
2674 enum isl_dim_type type, unsigned first, unsigned n)
2687 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2688 return isl_basic_map_remove_dims(bmap, type, first, n);
2690 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2693 bmap = move_last(bmap, type, first, n);
2694 bmap = isl_basic_map_cow(bmap);
2698 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2699 old = bmap->block2.data;
2700 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2701 (bmap->extra + n) * (1 + row_size));
2702 if (!bmap->block2.data)
2704 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2707 for (i = 0; i < n; ++i) {
2708 new_div[i] = bmap->block2.data +
2709 (bmap->extra + i) * (1 + row_size);
2710 isl_seq_clr(new_div[i], 1 + row_size);
2712 for (i = 0; i < bmap->extra; ++i)
2713 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2715 bmap->div = new_div;
2719 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2722 bmap = isl_basic_map_simplify(bmap);
2723 bmap = isl_basic_map_drop_redundant_divs(bmap);
2724 return isl_basic_map_finalize(bmap);
2726 isl_basic_map_free(bmap);
2730 /* Turn the n dimensions of type type, starting at first
2731 * into existentially quantified variables.
2733 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2734 enum isl_dim_type type, unsigned first, unsigned n)
2736 return (isl_basic_set *)isl_basic_map_project_out(
2737 (isl_basic_map *)bset, type, first, n);
2740 /* Turn the n dimensions of type type, starting at first
2741 * into existentially quantified variables.
2743 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2744 enum isl_dim_type type, unsigned first, unsigned n)
2754 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2756 map = isl_map_cow(map);
2760 map->dim = isl_dim_drop(map->dim, type, first, n);
2764 for (i = 0; i < map->n; ++i) {
2765 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2776 /* Turn the n dimensions of type type, starting at first
2777 * into existentially quantified variables.
2779 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2780 enum isl_dim_type type, unsigned first, unsigned n)
2782 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2785 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2789 for (i = 0; i < n; ++i) {
2790 j = isl_basic_map_alloc_div(bmap);
2793 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2797 isl_basic_map_free(bmap);
2801 struct isl_basic_map *isl_basic_map_apply_range(
2802 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2804 struct isl_dim *dim_result = NULL;
2805 struct isl_basic_map *bmap;
2806 unsigned n_in, n_out, n, nparam, total, pos;
2807 struct isl_dim_map *dim_map1, *dim_map2;
2809 if (!bmap1 || !bmap2)
2812 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2813 isl_dim_copy(bmap2->dim));
2815 n_in = isl_basic_map_n_in(bmap1);
2816 n_out = isl_basic_map_n_out(bmap2);
2817 n = isl_basic_map_n_out(bmap1);
2818 nparam = isl_basic_map_n_param(bmap1);
2820 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2821 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2822 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2823 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2824 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2825 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2826 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2827 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2828 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2829 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2830 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2832 bmap = isl_basic_map_alloc_dim(dim_result,
2833 bmap1->n_div + bmap2->n_div + n,
2834 bmap1->n_eq + bmap2->n_eq,
2835 bmap1->n_ineq + bmap2->n_ineq);
2836 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2837 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2838 bmap = add_divs(bmap, n);
2839 bmap = isl_basic_map_simplify(bmap);
2840 bmap = isl_basic_map_drop_redundant_divs(bmap);
2841 return isl_basic_map_finalize(bmap);
2843 isl_basic_map_free(bmap1);
2844 isl_basic_map_free(bmap2);
2848 struct isl_basic_set *isl_basic_set_apply(
2849 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2854 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2857 return (struct isl_basic_set *)
2858 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2860 isl_basic_set_free(bset);
2861 isl_basic_map_free(bmap);
2865 struct isl_basic_map *isl_basic_map_apply_domain(
2866 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2868 if (!bmap1 || !bmap2)
2871 isl_assert(bmap1->ctx,
2872 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2873 isl_assert(bmap1->ctx,
2874 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2877 bmap1 = isl_basic_map_reverse(bmap1);
2878 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2879 return isl_basic_map_reverse(bmap1);
2881 isl_basic_map_free(bmap1);
2882 isl_basic_map_free(bmap2);
2886 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2887 * A \cap B -> f(A) + f(B)
2889 struct isl_basic_map *isl_basic_map_sum(
2890 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2892 unsigned n_in, n_out, nparam, total, pos;
2893 struct isl_basic_map *bmap = NULL;
2894 struct isl_dim_map *dim_map1, *dim_map2;
2897 if (!bmap1 || !bmap2)
2900 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2903 nparam = isl_basic_map_n_param(bmap1);
2904 n_in = isl_basic_map_n_in(bmap1);
2905 n_out = isl_basic_map_n_out(bmap1);
2907 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2908 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2909 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2910 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2911 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2912 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2913 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2914 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2915 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2916 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2917 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2919 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2920 bmap1->n_div + bmap2->n_div + 2 * n_out,
2921 bmap1->n_eq + bmap2->n_eq + n_out,
2922 bmap1->n_ineq + bmap2->n_ineq);
2923 for (i = 0; i < n_out; ++i) {
2924 int j = isl_basic_map_alloc_equality(bmap);
2927 isl_seq_clr(bmap->eq[j], 1+total);
2928 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2929 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2930 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2932 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2933 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2934 bmap = add_divs(bmap, 2 * n_out);
2936 bmap = isl_basic_map_simplify(bmap);
2937 return isl_basic_map_finalize(bmap);
2939 isl_basic_map_free(bmap);
2940 isl_basic_map_free(bmap1);
2941 isl_basic_map_free(bmap2);
2945 /* Given two maps A -> f(A) and B -> g(B), construct a map
2946 * A \cap B -> f(A) + f(B)
2948 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2950 struct isl_map *result;
2956 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2958 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2959 map1->n * map2->n, 0);
2962 for (i = 0; i < map1->n; ++i)
2963 for (j = 0; j < map2->n; ++j) {
2964 struct isl_basic_map *part;
2965 part = isl_basic_map_sum(
2966 isl_basic_map_copy(map1->p[i]),
2967 isl_basic_map_copy(map2->p[j]));
2968 if (isl_basic_map_is_empty(part))
2969 isl_basic_map_free(part);
2971 result = isl_map_add_basic_map(result, part);
2984 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2985 __isl_take isl_set *set2)
2987 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2990 /* Given a basic map A -> f(A), construct A -> -f(A).
2992 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2997 bmap = isl_basic_map_cow(bmap);
3001 n = isl_basic_map_dim(bmap, isl_dim_out);
3002 off = isl_basic_map_offset(bmap, isl_dim_out);
3003 for (i = 0; i < bmap->n_eq; ++i)
3004 for (j = 0; j < n; ++j)
3005 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3006 for (i = 0; i < bmap->n_ineq; ++i)
3007 for (j = 0; j < n; ++j)
3008 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3009 for (i = 0; i < bmap->n_div; ++i)
3010 for (j = 0; j < n; ++j)
3011 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3012 return isl_basic_map_finalize(bmap);
3015 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3017 return isl_basic_map_neg(bset);
3020 /* Given a map A -> f(A), construct A -> -f(A).
3022 struct isl_map *isl_map_neg(struct isl_map *map)
3026 map = isl_map_cow(map);
3030 for (i = 0; i < map->n; ++i) {
3031 map->p[i] = isl_basic_map_neg(map->p[i]);
3042 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3044 return (isl_set *)isl_map_neg((isl_map *)set);
3047 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3048 * A -> floor(f(A)/d).
3050 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3053 unsigned n_in, n_out, nparam, total, pos;
3054 struct isl_basic_map *result = NULL;
3055 struct isl_dim_map *dim_map;
3061 nparam = isl_basic_map_n_param(bmap);
3062 n_in = isl_basic_map_n_in(bmap);
3063 n_out = isl_basic_map_n_out(bmap);
3065 total = nparam + n_in + n_out + bmap->n_div + n_out;
3066 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3067 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3068 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3069 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3070 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3072 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3073 bmap->n_div + n_out,
3074 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3075 result = add_constraints_dim_map(result, bmap, dim_map);
3076 result = add_divs(result, n_out);
3077 for (i = 0; i < n_out; ++i) {
3079 j = isl_basic_map_alloc_inequality(result);
3082 isl_seq_clr(result->ineq[j], 1+total);
3083 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3084 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3085 j = isl_basic_map_alloc_inequality(result);
3088 isl_seq_clr(result->ineq[j], 1+total);
3089 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3090 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3091 isl_int_sub_ui(result->ineq[j][0], d, 1);
3094 result = isl_basic_map_simplify(result);
3095 return isl_basic_map_finalize(result);
3097 isl_basic_map_free(result);
3101 /* Given a map A -> f(A) and an integer d, construct a map
3102 * A -> floor(f(A)/d).
3104 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3108 map = isl_map_cow(map);
3112 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3113 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3114 for (i = 0; i < map->n; ++i) {
3115 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3126 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3132 i = isl_basic_map_alloc_equality(bmap);
3135 nparam = isl_basic_map_n_param(bmap);
3136 n_in = isl_basic_map_n_in(bmap);
3137 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3138 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3139 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3140 return isl_basic_map_finalize(bmap);
3142 isl_basic_map_free(bmap);
3146 /* Add a constraints to "bmap" expressing i_pos < o_pos
3148 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3154 i = isl_basic_map_alloc_inequality(bmap);
3157 nparam = isl_basic_map_n_param(bmap);
3158 n_in = isl_basic_map_n_in(bmap);
3159 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3160 isl_int_set_si(bmap->ineq[i][0], -1);
3161 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3162 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3163 return isl_basic_map_finalize(bmap);
3165 isl_basic_map_free(bmap);
3169 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3171 static __isl_give isl_basic_map *var_less_or_equal(
3172 __isl_take isl_basic_map *bmap, unsigned pos)
3178 i = isl_basic_map_alloc_inequality(bmap);
3181 nparam = isl_basic_map_n_param(bmap);
3182 n_in = isl_basic_map_n_in(bmap);
3183 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3184 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3185 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3186 return isl_basic_map_finalize(bmap);
3188 isl_basic_map_free(bmap);
3192 /* Add a constraints to "bmap" expressing i_pos > o_pos
3194 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3200 i = isl_basic_map_alloc_inequality(bmap);
3203 nparam = isl_basic_map_n_param(bmap);
3204 n_in = isl_basic_map_n_in(bmap);
3205 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3206 isl_int_set_si(bmap->ineq[i][0], -1);
3207 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3208 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3209 return isl_basic_map_finalize(bmap);
3211 isl_basic_map_free(bmap);
3215 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3217 static __isl_give isl_basic_map *var_more_or_equal(
3218 __isl_take isl_basic_map *bmap, unsigned pos)
3224 i = isl_basic_map_alloc_inequality(bmap);
3227 nparam = isl_basic_map_n_param(bmap);
3228 n_in = isl_basic_map_n_in(bmap);
3229 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3230 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3231 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3232 return isl_basic_map_finalize(bmap);
3234 isl_basic_map_free(bmap);
3238 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3241 struct isl_basic_map *bmap;
3242 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3245 for (i = 0; i < n_equal && bmap; ++i)
3246 bmap = var_equal(bmap, i);
3247 return isl_basic_map_finalize(bmap);
3250 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3252 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3255 struct isl_basic_map *bmap;
3256 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3259 for (i = 0; i < pos && bmap; ++i)
3260 bmap = var_equal(bmap, i);
3262 bmap = var_less(bmap, pos);
3263 return isl_basic_map_finalize(bmap);
3266 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3268 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3269 __isl_take isl_dim *dim, unsigned pos)
3272 isl_basic_map *bmap;
3274 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3275 for (i = 0; i < pos; ++i)
3276 bmap = var_equal(bmap, i);
3277 bmap = var_less_or_equal(bmap, pos);
3278 return isl_basic_map_finalize(bmap);
3281 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3283 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3286 struct isl_basic_map *bmap;
3287 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3290 for (i = 0; i < pos && bmap; ++i)
3291 bmap = var_equal(bmap, i);
3293 bmap = var_more(bmap, pos);
3294 return isl_basic_map_finalize(bmap);
3297 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3299 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3300 __isl_take isl_dim *dim, unsigned pos)
3303 isl_basic_map *bmap;
3305 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3306 for (i = 0; i < pos; ++i)
3307 bmap = var_equal(bmap, i);
3308 bmap = var_more_or_equal(bmap, pos);
3309 return isl_basic_map_finalize(bmap);
3312 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3313 unsigned n, int equal)
3315 struct isl_map *map;
3318 if (n == 0 && equal)
3319 return isl_map_universe(dims);
3321 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3323 for (i = 0; i + 1 < n; ++i)
3324 map = isl_map_add_basic_map(map,
3325 isl_basic_map_less_at(isl_dim_copy(dims), i));
3328 map = isl_map_add_basic_map(map,
3329 isl_basic_map_less_or_equal_at(dims, n - 1));
3331 map = isl_map_add_basic_map(map,
3332 isl_basic_map_less_at(dims, n - 1));
3339 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3343 return map_lex_lte_first(dims, dims->n_out, equal);
3346 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3348 return map_lex_lte_first(dim, n, 0);
3351 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3353 return map_lex_lte_first(dim, n, 1);
3356 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3358 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3361 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3363 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3366 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3367 unsigned n, int equal)
3369 struct isl_map *map;
3372 if (n == 0 && equal)
3373 return isl_map_universe(dims);
3375 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3377 for (i = 0; i + 1 < n; ++i)
3378 map = isl_map_add_basic_map(map,
3379 isl_basic_map_more_at(isl_dim_copy(dims), i));
3382 map = isl_map_add_basic_map(map,
3383 isl_basic_map_more_or_equal_at(dims, n - 1));
3385 map = isl_map_add_basic_map(map,
3386 isl_basic_map_more_at(dims, n - 1));
3393 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3397 return map_lex_gte_first(dims, dims->n_out, equal);
3400 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3402 return map_lex_gte_first(dim, n, 0);
3405 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3407 return map_lex_gte_first(dim, n, 1);
3410 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3412 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3415 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3417 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3420 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3421 __isl_take isl_set *set2)
3424 map = isl_map_lex_le(isl_set_get_dim(set1));
3425 map = isl_map_intersect_domain(map, set1);
3426 map = isl_map_intersect_range(map, set2);
3430 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3431 __isl_take isl_set *set2)
3434 map = isl_map_lex_lt(isl_set_get_dim(set1));
3435 map = isl_map_intersect_domain(map, set1);
3436 map = isl_map_intersect_range(map, set2);
3440 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3441 __isl_take isl_set *set2)
3444 map = isl_map_lex_ge(isl_set_get_dim(set1));
3445 map = isl_map_intersect_domain(map, set1);
3446 map = isl_map_intersect_range(map, set2);
3450 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3451 __isl_take isl_set *set2)
3454 map = isl_map_lex_gt(isl_set_get_dim(set1));
3455 map = isl_map_intersect_domain(map, set1);
3456 map = isl_map_intersect_range(map, set2);
3460 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3461 __isl_take isl_map *map2)
3464 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3465 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3466 map = isl_map_apply_range(map, isl_map_reverse(map2));
3470 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3471 __isl_take isl_map *map2)
3474 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3475 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3476 map = isl_map_apply_range(map, isl_map_reverse(map2));
3480 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3481 __isl_take isl_map *map2)
3484 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3485 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3486 map = isl_map_apply_range(map, isl_map_reverse(map2));
3490 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3491 __isl_take isl_map *map2)
3494 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3495 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3496 map = isl_map_apply_range(map, isl_map_reverse(map2));
3500 struct isl_basic_map *isl_basic_map_from_basic_set(
3501 struct isl_basic_set *bset, struct isl_dim *dim)
3503 struct isl_basic_map *bmap;
3505 bset = isl_basic_set_cow(bset);
3509 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3510 isl_dim_free(bset->dim);
3511 bmap = (struct isl_basic_map *) bset;
3513 return isl_basic_map_finalize(bmap);
3515 isl_basic_set_free(bset);
3520 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3524 if (bmap->dim->n_in == 0)
3525 return (struct isl_basic_set *)bmap;
3526 bmap = isl_basic_map_cow(bmap);
3529 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3532 bmap = isl_basic_map_finalize(bmap);
3533 return (struct isl_basic_set *)bmap;
3535 isl_basic_map_free(bmap);
3539 /* For a div d = floor(f/m), add the constraints
3542 * -(f-(n-1)) + m d >= 0
3544 * Note that the second constraint is the negation of
3548 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3549 unsigned pos, isl_int *div)
3552 unsigned total = isl_basic_map_total_dim(bmap);
3554 i = isl_basic_map_alloc_inequality(bmap);
3557 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3558 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3560 j = isl_basic_map_alloc_inequality(bmap);
3563 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3564 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3565 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3569 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3570 unsigned pos, isl_int *div)
3572 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3576 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3578 unsigned total = isl_basic_map_total_dim(bmap);
3579 unsigned div_pos = total - bmap->n_div + div;
3581 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3585 struct isl_basic_set *isl_basic_map_underlying_set(
3586 struct isl_basic_map *bmap)
3590 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3592 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3593 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3594 return (struct isl_basic_set *)bmap;
3595 bmap = isl_basic_map_cow(bmap);
3598 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3601 bmap->extra -= bmap->n_div;
3603 bmap = isl_basic_map_finalize(bmap);
3604 return (struct isl_basic_set *)bmap;
3609 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3610 __isl_take isl_basic_set *bset)
3612 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3615 struct isl_basic_map *isl_basic_map_overlying_set(
3616 struct isl_basic_set *bset, struct isl_basic_map *like)
3618 struct isl_basic_map *bmap;
3619 struct isl_ctx *ctx;
3626 isl_assert(ctx, bset->n_div == 0, goto error);
3627 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3628 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3630 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3631 isl_basic_map_free(like);
3632 return (struct isl_basic_map *)bset;
3634 bset = isl_basic_set_cow(bset);
3637 total = bset->dim->n_out + bset->extra;
3638 bmap = (struct isl_basic_map *)bset;
3639 isl_dim_free(bmap->dim);
3640 bmap->dim = isl_dim_copy(like->dim);
3643 bmap->n_div = like->n_div;
3644 bmap->extra += like->n_div;
3648 ltotal = total - bmap->extra + like->extra;
3651 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3652 bmap->extra * (1 + 1 + total));
3653 if (isl_blk_is_error(bmap->block2))
3655 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3659 for (i = 0; i < bmap->extra; ++i)
3660 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3661 for (i = 0; i < like->n_div; ++i) {
3662 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3663 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3665 bmap = isl_basic_map_extend_constraints(bmap,
3666 0, 2 * like->n_div);
3667 for (i = 0; i < like->n_div; ++i) {
3668 if (isl_int_is_zero(bmap->div[i][0]))
3670 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3674 isl_basic_map_free(like);
3675 bmap = isl_basic_map_simplify(bmap);
3676 bmap = isl_basic_map_finalize(bmap);
3679 isl_basic_map_free(like);
3680 isl_basic_set_free(bset);
3684 struct isl_basic_set *isl_basic_set_from_underlying_set(
3685 struct isl_basic_set *bset, struct isl_basic_set *like)
3687 return (struct isl_basic_set *)
3688 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3691 struct isl_set *isl_set_from_underlying_set(
3692 struct isl_set *set, struct isl_basic_set *like)
3698 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3700 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3701 isl_basic_set_free(like);
3704 set = isl_set_cow(set);
3707 for (i = 0; i < set->n; ++i) {
3708 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3709 isl_basic_set_copy(like));
3713 isl_dim_free(set->dim);
3714 set->dim = isl_dim_copy(like->dim);
3717 isl_basic_set_free(like);
3720 isl_basic_set_free(like);
3725 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3729 map = isl_map_cow(map);
3732 map->dim = isl_dim_cow(map->dim);
3736 for (i = 1; i < map->n; ++i)
3737 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3739 for (i = 0; i < map->n; ++i) {
3740 map->p[i] = (struct isl_basic_map *)
3741 isl_basic_map_underlying_set(map->p[i]);
3746 map->dim = isl_dim_underlying(map->dim, 0);
3748 isl_dim_free(map->dim);
3749 map->dim = isl_dim_copy(map->p[0]->dim);
3753 return (struct isl_set *)map;
3759 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3761 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3764 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3765 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3767 bmap = isl_basic_map_cow(bmap);
3771 isl_dim_free(bmap->dim);
3774 bmap = isl_basic_map_finalize(bmap);
3778 isl_basic_map_free(bmap);
3783 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3784 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3786 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3790 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3791 __isl_take isl_dim *dim)
3795 map = isl_map_cow(map);
3799 for (i = 0; i < map->n; ++i) {
3800 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3805 isl_dim_free(map->dim);
3815 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3816 __isl_take isl_dim *dim)
3818 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3821 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3824 struct isl_basic_set *domain;
3830 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3832 n_in = isl_basic_map_n_in(bmap);
3833 n_out = isl_basic_map_n_out(bmap);
3834 domain = isl_basic_set_from_basic_map(bmap);
3835 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3837 domain = isl_basic_set_reset_dim(domain, dim);
3842 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3844 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3847 __isl_give isl_basic_map *isl_basic_map_domain_map(
3848 __isl_take isl_basic_map *bmap)
3852 isl_basic_map *domain;
3853 isl_basic_set *bset;
3854 int nparam, n_in, n_out;
3857 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3858 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3859 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3861 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3862 domain = isl_basic_map_universe(dim);
3864 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3865 bmap = isl_basic_map_apply_range(bmap, domain);
3866 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3868 total = isl_basic_map_total_dim(bmap);
3870 for (i = 0; i < n_in; ++i) {
3871 k = isl_basic_map_alloc_equality(bmap);
3874 isl_seq_clr(bmap->eq[k], 1 + total);
3875 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3876 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3879 bmap = isl_basic_map_gauss(bmap, NULL);
3880 return isl_basic_map_finalize(bmap);
3882 isl_basic_map_free(bmap);
3886 __isl_give isl_basic_map *isl_basic_map_range_map(
3887 __isl_take isl_basic_map *bmap)
3891 isl_basic_map *range;
3892 isl_basic_set *bset;
3893 int nparam, n_in, n_out;
3896 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3897 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3898 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3900 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3901 range = isl_basic_map_universe(dim);
3903 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3904 bmap = isl_basic_map_apply_range(bmap, range);
3905 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3907 total = isl_basic_map_total_dim(bmap);
3909 for (i = 0; i < n_out; ++i) {
3910 k = isl_basic_map_alloc_equality(bmap);
3913 isl_seq_clr(bmap->eq[k], 1 + total);
3914 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3915 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3918 bmap = isl_basic_map_gauss(bmap, NULL);
3919 return isl_basic_map_finalize(bmap);
3921 isl_basic_map_free(bmap);
3925 struct isl_set *isl_map_range(struct isl_map *map)
3928 struct isl_set *set;
3932 if (isl_map_dim(map, isl_dim_in) == 0 &&
3933 !isl_dim_is_named_or_nested(map->dim, isl_dim_in))
3934 return (isl_set *)map;
3936 map = isl_map_cow(map);
3940 set = (struct isl_set *) map;
3941 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3944 for (i = 0; i < map->n; ++i) {
3945 set->p[i] = isl_basic_map_range(map->p[i]);
3949 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3950 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3957 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3960 isl_dim *domain_dim;
3962 map = isl_map_cow(map);
3966 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3967 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3968 map->dim = isl_dim_join(map->dim, domain_dim);
3971 for (i = 0; i < map->n; ++i) {
3972 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3976 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3977 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3984 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3989 map = isl_map_cow(map);
3993 range_dim = isl_dim_range(isl_map_get_dim(map));
3994 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3995 map->dim = isl_dim_join(map->dim, range_dim);
3998 for (i = 0; i < map->n; ++i) {
3999 map->p[i] = isl_basic_map_range_map(map->p[i]);
4003 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4004 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4011 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4014 struct isl_map *map = NULL;
4016 set = isl_set_cow(set);
4019 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4020 map = (struct isl_map *)set;
4021 for (i = 0; i < set->n; ++i) {
4022 map->p[i] = isl_basic_map_from_basic_set(
4023 set->p[i], isl_dim_copy(dim));
4027 isl_dim_free(map->dim);
4036 __isl_give isl_basic_map *isl_basic_map_from_domain(
4037 __isl_take isl_basic_set *bset)
4039 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4042 __isl_give isl_basic_map *isl_basic_map_from_range(
4043 __isl_take isl_basic_set *bset)
4045 return (isl_basic_map *)bset;
4048 struct isl_map *isl_map_from_range(struct isl_set *set)
4050 return (struct isl_map *)set;
4053 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4055 return isl_map_reverse(isl_map_from_range(set));
4058 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4059 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4061 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4062 isl_basic_map_from_range(range));
4065 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4066 __isl_take isl_set *range)
4068 return isl_map_apply_range(isl_map_from_domain(domain),
4069 isl_map_from_range(range));
4072 struct isl_set *isl_set_from_map(struct isl_map *map)
4075 struct isl_set *set = NULL;
4079 map = isl_map_cow(map);
4082 map->dim = isl_dim_as_set_dim(map->dim);
4085 set = (struct isl_set *)map;
4086 for (i = 0; i < map->n; ++i) {
4087 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4097 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4099 struct isl_map *map;
4103 isl_assert(dim->ctx, n >= 0, return NULL);
4104 map = isl_alloc(dim->ctx, struct isl_map,
4105 sizeof(struct isl_map) +
4106 (n - 1) * sizeof(struct isl_basic_map *));
4110 map->ctx = dim->ctx;
4111 isl_ctx_ref(map->ctx);
4123 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4124 unsigned nparam, unsigned in, unsigned out, int n,
4127 struct isl_map *map;
4128 struct isl_dim *dims;
4130 dims = isl_dim_alloc(ctx, nparam, in, out);
4134 map = isl_map_alloc_dim(dims, n, flags);
4138 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4140 struct isl_basic_map *bmap;
4141 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4142 bmap = isl_basic_map_set_to_empty(bmap);
4146 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4148 struct isl_basic_set *bset;
4149 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4150 bset = isl_basic_set_set_to_empty(bset);
4154 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4156 struct isl_basic_map *bmap;
4159 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4160 bmap = isl_basic_map_set_to_empty(bmap);
4164 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4166 struct isl_basic_map *bmap;
4169 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4170 bmap = isl_basic_map_set_to_empty(bmap);
4174 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4176 struct isl_basic_set *bset;
4179 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4180 bset = isl_basic_set_set_to_empty(bset);
4184 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4186 struct isl_basic_map *bmap;
4187 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4188 bmap = isl_basic_map_finalize(bmap);
4192 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4194 struct isl_basic_set *bset;
4195 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4196 bset = isl_basic_set_finalize(bset);
4200 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4203 unsigned total = isl_dim_total(dim);
4204 isl_basic_map *bmap;
4206 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4207 for (i = 0; i < total; ++i) {
4208 int k = isl_basic_map_alloc_inequality(bmap);
4211 isl_seq_clr(bmap->ineq[k], 1 + total);
4212 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4216 isl_basic_map_free(bmap);
4220 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4222 return isl_basic_map_nat_universe(dim);
4225 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4227 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4230 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4232 return isl_map_nat_universe(dim);
4235 __isl_give isl_basic_map *isl_basic_map_universe_like(
4236 __isl_keep isl_basic_map *model)
4240 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4243 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4247 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4250 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4251 __isl_keep isl_set *model)
4255 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4258 struct isl_map *isl_map_empty(struct isl_dim *dim)
4260 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4263 struct isl_map *isl_map_empty_like(struct isl_map *model)
4267 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4270 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4274 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4277 struct isl_set *isl_set_empty(struct isl_dim *dim)
4279 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4282 struct isl_set *isl_set_empty_like(struct isl_set *model)
4286 return isl_set_empty(isl_dim_copy(model->dim));
4289 struct isl_map *isl_map_universe(struct isl_dim *dim)
4291 struct isl_map *map;
4294 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4295 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4299 struct isl_set *isl_set_universe(struct isl_dim *dim)
4301 struct isl_set *set;
4304 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4305 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4309 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4313 return isl_set_universe(isl_dim_copy(model->dim));
4316 struct isl_map *isl_map_dup(struct isl_map *map)
4319 struct isl_map *dup;
4323 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4324 for (i = 0; i < map->n; ++i)
4325 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4329 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4330 __isl_take isl_basic_map *bmap)
4334 if (isl_basic_map_fast_is_empty(bmap)) {
4335 isl_basic_map_free(bmap);
4338 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4339 isl_assert(map->ctx, map->n < map->size, goto error);
4340 map->p[map->n] = bmap;
4342 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4348 isl_basic_map_free(bmap);
4352 void isl_map_free(struct isl_map *map)
4362 isl_ctx_deref(map->ctx);
4363 for (i = 0; i < map->n; ++i)
4364 isl_basic_map_free(map->p[i]);
4365 isl_dim_free(map->dim);
4369 struct isl_map *isl_map_extend(struct isl_map *base,
4370 unsigned nparam, unsigned n_in, unsigned n_out)
4374 base = isl_map_cow(base);
4378 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4381 for (i = 0; i < base->n; ++i) {
4382 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4383 isl_dim_copy(base->dim), 0, 0, 0);
4393 struct isl_set *isl_set_extend(struct isl_set *base,
4394 unsigned nparam, unsigned dim)
4396 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4400 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4401 struct isl_basic_map *bmap, unsigned pos, int value)
4405 bmap = isl_basic_map_cow(bmap);
4406 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4407 j = isl_basic_map_alloc_equality(bmap);
4410 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4411 isl_int_set_si(bmap->eq[j][pos], -1);
4412 isl_int_set_si(bmap->eq[j][0], value);
4413 bmap = isl_basic_map_simplify(bmap);
4414 return isl_basic_map_finalize(bmap);
4416 isl_basic_map_free(bmap);
4420 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4421 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4425 bmap = isl_basic_map_cow(bmap);
4426 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4427 j = isl_basic_map_alloc_equality(bmap);
4430 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4431 isl_int_set_si(bmap->eq[j][pos], -1);
4432 isl_int_set(bmap->eq[j][0], value);
4433 bmap = isl_basic_map_simplify(bmap);
4434 return isl_basic_map_finalize(bmap);
4436 isl_basic_map_free(bmap);
4440 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4441 enum isl_dim_type type, unsigned pos, int value)
4445 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4446 return isl_basic_map_fix_pos_si(bmap,
4447 isl_basic_map_offset(bmap, type) + pos, value);
4449 isl_basic_map_free(bmap);
4453 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4454 enum isl_dim_type type, unsigned pos, isl_int value)
4458 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4459 return isl_basic_map_fix_pos(bmap,
4460 isl_basic_map_offset(bmap, type) + pos, value);
4462 isl_basic_map_free(bmap);
4466 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4467 enum isl_dim_type type, unsigned pos, int value)
4469 return (struct isl_basic_set *)
4470 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4474 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4475 enum isl_dim_type type, unsigned pos, isl_int value)
4477 return (struct isl_basic_set *)
4478 isl_basic_map_fix((struct isl_basic_map *)bset,
4482 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4483 unsigned input, int value)
4485 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4488 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4489 unsigned dim, int value)
4491 return (struct isl_basic_set *)
4492 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4493 isl_dim_set, dim, value);
4496 struct isl_map *isl_map_fix_si(struct isl_map *map,
4497 enum isl_dim_type type, unsigned pos, int value)
4501 map = isl_map_cow(map);
4505 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4506 for (i = 0; i < map->n; ++i) {
4507 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4511 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4518 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4519 enum isl_dim_type type, unsigned pos, int value)
4521 return (struct isl_set *)
4522 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4525 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4526 enum isl_dim_type type, unsigned pos, isl_int value)
4530 map = isl_map_cow(map);
4534 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4535 for (i = 0; i < map->n; ++i) {
4536 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4540 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4547 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4548 enum isl_dim_type type, unsigned pos, isl_int value)
4550 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4553 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4554 unsigned input, int value)
4556 return isl_map_fix_si(map, isl_dim_in, input, value);
4559 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4561 return (struct isl_set *)
4562 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4565 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4566 __isl_take isl_basic_map *bmap,
4567 enum isl_dim_type type, unsigned pos, int value)
4573 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4574 pos += isl_basic_map_offset(bmap, type);
4575 bmap = isl_basic_map_cow(bmap);
4576 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4577 j = isl_basic_map_alloc_inequality(bmap);
4580 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4581 isl_int_set_si(bmap->ineq[j][pos], 1);
4582 isl_int_set_si(bmap->ineq[j][0], -value);
4583 bmap = isl_basic_map_simplify(bmap);
4584 return isl_basic_map_finalize(bmap);
4586 isl_basic_map_free(bmap);
4590 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4591 unsigned dim, isl_int value)
4595 bset = isl_basic_set_cow(bset);
4596 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4597 j = isl_basic_set_alloc_inequality(bset);
4600 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4601 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4602 isl_int_neg(bset->ineq[j][0], value);
4603 bset = isl_basic_set_simplify(bset);
4604 return isl_basic_set_finalize(bset);
4606 isl_basic_set_free(bset);
4610 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4611 enum isl_dim_type type, unsigned pos, int value)
4615 map = isl_map_cow(map);
4619 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4620 for (i = 0; i < map->n; ++i) {
4621 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4626 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4633 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4634 enum isl_dim_type type, unsigned pos, int value)
4636 return (struct isl_set *)
4637 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4640 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4645 set = isl_set_cow(set);
4649 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4650 for (i = 0; i < set->n; ++i) {
4651 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4661 struct isl_map *isl_map_reverse(struct isl_map *map)
4665 map = isl_map_cow(map);
4669 map->dim = isl_dim_reverse(map->dim);
4672 for (i = 0; i < map->n; ++i) {
4673 map->p[i] = isl_basic_map_reverse(map->p[i]);
4677 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4684 static struct isl_map *isl_basic_map_partial_lexopt(
4685 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4686 struct isl_set **empty, int max)
4690 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4691 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4693 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4695 isl_basic_map_free(bmap);
4696 isl_basic_set_free(dom);
4702 struct isl_map *isl_basic_map_partial_lexmax(
4703 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4704 struct isl_set **empty)
4706 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4709 struct isl_map *isl_basic_map_partial_lexmin(
4710 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4711 struct isl_set **empty)
4713 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4716 struct isl_set *isl_basic_set_partial_lexmin(
4717 struct isl_basic_set *bset, struct isl_basic_set *dom,
4718 struct isl_set **empty)
4720 return (struct isl_set *)
4721 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4725 struct isl_set *isl_basic_set_partial_lexmax(
4726 struct isl_basic_set *bset, struct isl_basic_set *dom,
4727 struct isl_set **empty)
4729 return (struct isl_set *)
4730 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4734 /* Given a basic map "bmap", compute the lexicographically minimal
4735 * (or maximal) image element for each domain element in dom.
4736 * Set *empty to those elements in dom that do not have an image element.
4738 * We first make sure the basic sets in dom are disjoint and then
4739 * simply collect the results over each of the basic sets separately.
4740 * We could probably improve the efficiency a bit by moving the union
4741 * domain down into the parametric integer programming.
4743 static __isl_give isl_map *basic_map_partial_lexopt(
4744 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4745 __isl_give isl_set **empty, int max)
4748 struct isl_map *res;
4750 dom = isl_set_make_disjoint(dom);
4754 if (isl_set_fast_is_empty(dom)) {
4755 res = isl_map_empty_like_basic_map(bmap);
4756 *empty = isl_set_empty_like(dom);
4758 isl_basic_map_free(bmap);
4762 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4763 isl_basic_set_copy(dom->p[0]), empty, max);
4765 for (i = 1; i < dom->n; ++i) {
4766 struct isl_map *res_i;
4767 struct isl_set *empty_i;
4769 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4770 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4772 res = isl_map_union_disjoint(res, res_i);
4773 *empty = isl_set_union_disjoint(*empty, empty_i);
4777 isl_basic_map_free(bmap);
4782 isl_basic_map_free(bmap);
4786 /* Given a map "map", compute the lexicographically minimal
4787 * (or maximal) image element for each domain element in dom.
4788 * Set *empty to those elements in dom that do not have an image element.
4790 * We first compute the lexicographically minimal or maximal element
4791 * in the first basic map. This results in a partial solution "res"
4792 * and a subset "todo" of dom that still need to be handled.
4793 * We then consider each of the remaining maps in "map" and successively
4794 * improve both "res" and "todo".
4796 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4797 * Assume we are computing the lexicographical maximum.
4798 * We first compute the lexicographically maximal element in basic map i.
4799 * This results in a partial solution res_i and a subset todo_i.
4800 * Then we combine these results with those obtain for the first k basic maps
4801 * to obtain a result that is valid for the first k+1 basic maps.
4802 * In particular, the set where there is no solution is the set where
4803 * there is no solution for the first k basic maps and also no solution
4804 * for the ith basic map, i.e.,
4806 * todo^i = todo^k * todo_i
4808 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4809 * solutions, arbitrarily breaking ties in favor of res^k.
4810 * That is, when res^k(a) >= res_i(a), we pick res^k and
4811 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4812 * the lexicographic order.)
4813 * In practice, we compute
4815 * res^k * (res_i . "<=")
4819 * res_i * (res^k . "<")
4821 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4822 * where only one of res^k and res_i provides a solution and we simply pick
4829 * Note that we only compute these intersections when dom(res^k) intersects
4830 * dom(res_i). Otherwise, the only effect of these intersections is to
4831 * potentially break up res^k and res_i into smaller pieces.
4832 * We want to avoid such splintering as much as possible.
4833 * In fact, an earlier implementation of this function would look for
4834 * better results in the domain of res^k and for extra results in todo^k,
4835 * but this would always result in a splintering according to todo^k,
4836 * even when the domain of basic map i is disjoint from the domains of
4837 * the previous basic maps.
4839 static __isl_give isl_map *isl_map_partial_lexopt(
4840 __isl_take isl_map *map, __isl_take isl_set *dom,
4841 __isl_give isl_set **empty, int max)
4844 struct isl_map *res;
4845 struct isl_set *todo;
4850 if (isl_map_fast_is_empty(map)) {
4858 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4859 isl_set_copy(dom), &todo, max);
4861 for (i = 1; i < map->n; ++i) {
4865 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4867 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4868 isl_set_copy(dom), &todo_i, max);
4871 lt = isl_map_lex_lt(isl_dim_copy(dim));
4872 le = isl_map_lex_le(dim);
4874 lt = isl_map_lex_gt(isl_dim_copy(dim));
4875 le = isl_map_lex_ge(dim);
4877 lt = isl_map_apply_range(isl_map_copy(res), lt);
4878 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4879 le = isl_map_apply_range(isl_map_copy(res_i), le);
4880 le = isl_map_intersect(le, isl_map_copy(res));
4882 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4883 res = isl_map_intersect_domain(res,
4884 isl_set_copy(todo_i));
4885 res_i = isl_map_intersect_domain(res_i,
4886 isl_set_copy(todo));
4889 res = isl_map_union_disjoint(res, res_i);
4890 res = isl_map_union_disjoint(res, lt);
4891 res = isl_map_union_disjoint(res, le);
4893 todo = isl_set_intersect(todo, todo_i);
4913 __isl_give isl_map *isl_map_partial_lexmax(
4914 __isl_take isl_map *map, __isl_take isl_set *dom,
4915 __isl_give isl_set **empty)
4917 return isl_map_partial_lexopt(map, dom, empty, 1);
4920 __isl_give isl_map *isl_map_partial_lexmin(
4921 __isl_take isl_map *map, __isl_take isl_set *dom,
4922 __isl_give isl_set **empty)
4924 return isl_map_partial_lexopt(map, dom, empty, 0);
4927 __isl_give isl_set *isl_set_partial_lexmin(
4928 __isl_take isl_set *set, __isl_take isl_set *dom,
4929 __isl_give isl_set **empty)
4931 return (struct isl_set *)
4932 isl_map_partial_lexmin((struct isl_map *)set,
4936 __isl_give isl_set *isl_set_partial_lexmax(
4937 __isl_take isl_set *set, __isl_take isl_set *dom,
4938 __isl_give isl_set **empty)
4940 return (struct isl_set *)
4941 isl_map_partial_lexmax((struct isl_map *)set,
4945 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4947 struct isl_basic_set *dom = NULL;
4948 struct isl_dim *dom_dim;
4952 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4953 dom = isl_basic_set_universe(dom_dim);
4954 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4956 isl_basic_map_free(bmap);
4960 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4962 return isl_basic_map_lexopt(bmap, 0);
4965 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4967 return isl_basic_map_lexopt(bmap, 1);
4970 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4972 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4975 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4977 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4980 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4982 struct isl_set *dom = NULL;
4983 struct isl_dim *dom_dim;
4987 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4988 dom = isl_set_universe(dom_dim);
4989 return isl_map_partial_lexopt(map, dom, NULL, max);
4995 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4997 return isl_map_lexopt(map, 0);
5000 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5002 return isl_map_lexopt(map, 1);
5005 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5007 return (isl_set *)isl_map_lexmin((isl_map *)set);
5010 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5012 return (isl_set *)isl_map_lexmax((isl_map *)set);
5015 /* Apply a preimage specified by "mat" on the parameters of "bset".
5016 * bset is assumed to have only parameters and divs.
5018 static struct isl_basic_set *basic_set_parameter_preimage(
5019 struct isl_basic_set *bset, struct isl_mat *mat)
5026 bset->dim = isl_dim_cow(bset->dim);
5030 nparam = isl_basic_set_dim(bset, isl_dim_param);
5032 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5034 bset->dim->nparam = 0;
5035 bset->dim->n_out = nparam;
5036 bset = isl_basic_set_preimage(bset, mat);
5038 bset->dim->nparam = bset->dim->n_out;
5039 bset->dim->n_out = 0;
5044 isl_basic_set_free(bset);
5048 /* Apply a preimage specified by "mat" on the parameters of "set".
5049 * set is assumed to have only parameters and divs.
5051 static struct isl_set *set_parameter_preimage(
5052 struct isl_set *set, struct isl_mat *mat)
5054 struct isl_dim *dim = NULL;
5060 dim = isl_dim_copy(set->dim);
5061 dim = isl_dim_cow(dim);
5065 nparam = isl_set_dim(set, isl_dim_param);
5067 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5070 dim->n_out = nparam;
5071 isl_set_reset_dim(set, dim);
5072 set = isl_set_preimage(set, mat);
5075 dim = isl_dim_copy(set->dim);
5076 dim = isl_dim_cow(dim);
5079 dim->nparam = dim->n_out;
5081 isl_set_reset_dim(set, dim);
5091 /* Intersect the basic set "bset" with the affine space specified by the
5092 * equalities in "eq".
5094 static struct isl_basic_set *basic_set_append_equalities(
5095 struct isl_basic_set *bset, struct isl_mat *eq)
5103 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5108 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5109 for (i = 0; i < eq->n_row; ++i) {
5110 k = isl_basic_set_alloc_equality(bset);
5113 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5114 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5118 bset = isl_basic_set_gauss(bset, NULL);
5119 bset = isl_basic_set_finalize(bset);
5124 isl_basic_set_free(bset);
5128 /* Intersect the set "set" with the affine space specified by the
5129 * equalities in "eq".
5131 static struct isl_set *set_append_equalities(struct isl_set *set,
5139 for (i = 0; i < set->n; ++i) {
5140 set->p[i] = basic_set_append_equalities(set->p[i],
5153 /* Project the given basic set onto its parameter domain, possibly introducing
5154 * new, explicit, existential variables in the constraints.
5155 * The input has parameters and (possibly implicit) existential variables.
5156 * The output has the same parameters, but only
5157 * explicit existentially quantified variables.
5159 * The actual projection is performed by pip, but pip doesn't seem
5160 * to like equalities very much, so we first remove the equalities
5161 * among the parameters by performing a variable compression on
5162 * the parameters. Afterward, an inverse transformation is performed
5163 * and the equalities among the parameters are inserted back in.
5165 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5169 struct isl_mat *T, *T2;
5170 struct isl_set *set;
5171 unsigned nparam, n_div;
5173 bset = isl_basic_set_cow(bset);
5177 if (bset->n_eq == 0)
5178 return isl_basic_set_lexmin(bset);
5180 isl_basic_set_gauss(bset, NULL);
5182 nparam = isl_basic_set_dim(bset, isl_dim_param);
5183 n_div = isl_basic_set_dim(bset, isl_dim_div);
5185 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5186 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5189 if (i == bset->n_eq)
5190 return isl_basic_set_lexmin(bset);
5192 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
5194 eq = isl_mat_cow(eq);
5195 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5196 if (T && T->n_col == 0) {
5200 bset = isl_basic_set_set_to_empty(bset);
5201 return isl_set_from_basic_set(bset);
5203 bset = basic_set_parameter_preimage(bset, T);
5205 set = isl_basic_set_lexmin(bset);
5206 set = set_parameter_preimage(set, T2);
5207 set = set_append_equalities(set, eq);
5211 /* Compute an explicit representation for all the existentially
5212 * quantified variables.
5213 * The input and output dimensions are first turned into parameters.
5214 * compute_divs then returns a map with the same parameters and
5215 * no input or output dimensions and the dimension specification
5216 * is reset to that of the input.
5218 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5220 struct isl_basic_set *bset;
5221 struct isl_set *set;
5222 struct isl_map *map;
5223 struct isl_dim *dim, *orig_dim = NULL;
5228 bmap = isl_basic_map_cow(bmap);
5232 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5233 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5234 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5235 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5239 orig_dim = bmap->dim;
5241 bset = (struct isl_basic_set *)bmap;
5243 set = parameter_compute_divs(bset);
5244 map = (struct isl_map *)set;
5245 map = isl_map_reset_dim(map, orig_dim);
5249 isl_basic_map_free(bmap);
5253 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5261 off = isl_dim_total(bmap->dim);
5262 for (i = 0; i < bmap->n_div; ++i) {
5263 if (isl_int_is_zero(bmap->div[i][0]))
5265 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5271 static int map_divs_known(__isl_keep isl_map *map)
5278 for (i = 0; i < map->n; ++i) {
5279 int known = basic_map_divs_known(map->p[i]);
5287 /* If bmap contains any unknown divs, then compute explicit
5288 * expressions for them. However, this computation may be
5289 * quite expensive, so first try to remove divs that aren't
5292 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5296 struct isl_map *map;
5298 known = basic_map_divs_known(bmap);
5302 return isl_map_from_basic_map(bmap);
5304 bmap = isl_basic_map_drop_redundant_divs(bmap);
5306 known = basic_map_divs_known(bmap);
5310 return isl_map_from_basic_map(bmap);
5312 map = compute_divs(bmap);
5315 isl_basic_map_free(bmap);
5319 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5323 struct isl_map *res;
5330 known = map_divs_known(map);
5338 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5339 for (i = 1 ; i < map->n; ++i) {
5341 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5342 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5343 res = isl_map_union_disjoint(res, r2);
5345 res = isl_map_union(res, r2);
5352 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5354 return (struct isl_set *)
5355 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5358 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5360 return (struct isl_set *)
5361 isl_map_compute_divs((struct isl_map *)set);
5364 struct isl_set *isl_map_domain(struct isl_map *map)
5367 struct isl_set *set;
5372 map = isl_map_cow(map);
5376 set = (struct isl_set *)map;
5377 set->dim = isl_dim_domain(set->dim);
5380 for (i = 0; i < map->n; ++i) {
5381 set->p[i] = isl_basic_map_domain(map->p[i]);
5385 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5386 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5393 struct isl_map *isl_map_union_disjoint(
5394 struct isl_map *map1, struct isl_map *map2)
5398 struct isl_map *map = NULL;
5412 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5414 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5415 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5416 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5418 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5419 map1->n + map2->n, flags);
5422 for (i = 0; i < map1->n; ++i) {
5423 map = isl_map_add_basic_map(map,
5424 isl_basic_map_copy(map1->p[i]));
5428 for (i = 0; i < map2->n; ++i) {
5429 map = isl_map_add_basic_map(map,
5430 isl_basic_map_copy(map2->p[i]));
5444 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5446 map1 = isl_map_union_disjoint(map1, map2);
5450 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5454 struct isl_set *isl_set_union_disjoint(
5455 struct isl_set *set1, struct isl_set *set2)
5457 return (struct isl_set *)
5458 isl_map_union_disjoint(
5459 (struct isl_map *)set1, (struct isl_map *)set2);
5462 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5464 return (struct isl_set *)
5465 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5468 struct isl_map *isl_map_intersect_range(
5469 struct isl_map *map, struct isl_set *set)
5472 struct isl_map *result;
5478 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5479 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5480 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5482 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5483 map->n * set->n, flags);
5486 for (i = 0; i < map->n; ++i)
5487 for (j = 0; j < set->n; ++j) {
5488 result = isl_map_add_basic_map(result,
5489 isl_basic_map_intersect_range(
5490 isl_basic_map_copy(map->p[i]),
5491 isl_basic_set_copy(set->p[j])));
5504 struct isl_map *isl_map_intersect_domain(
5505 struct isl_map *map, struct isl_set *set)
5507 return isl_map_reverse(
5508 isl_map_intersect_range(isl_map_reverse(map), set));
5511 struct isl_map *isl_map_apply_domain(
5512 struct isl_map *map1, struct isl_map *map2)
5516 map1 = isl_map_reverse(map1);
5517 map1 = isl_map_apply_range(map1, map2);
5518 return isl_map_reverse(map1);
5525 struct isl_map *isl_map_apply_range(
5526 struct isl_map *map1, struct isl_map *map2)
5528 struct isl_dim *dim_result;
5529 struct isl_map *result;
5535 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5536 isl_dim_copy(map2->dim));
5538 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5541 for (i = 0; i < map1->n; ++i)
5542 for (j = 0; j < map2->n; ++j) {
5543 result = isl_map_add_basic_map(result,
5544 isl_basic_map_apply_range(
5545 isl_basic_map_copy(map1->p[i]),
5546 isl_basic_map_copy(map2->p[j])));
5552 if (result && result->n <= 1)
5553 ISL_F_SET(result, ISL_MAP_DISJOINT);
5562 * returns range - domain
5564 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5566 isl_dim *dims, *target_dim;
5567 struct isl_basic_set *bset;
5574 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5575 bmap->dim, isl_dim_out),
5577 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5578 dim = isl_basic_map_n_in(bmap);
5579 nparam = isl_basic_map_n_param(bmap);
5580 bset = isl_basic_set_from_basic_map(bmap);
5581 bset = isl_basic_set_cow(bset);
5582 dims = isl_basic_set_get_dim(bset);
5583 dims = isl_dim_add(dims, isl_dim_set, dim);
5584 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5585 bset = isl_basic_set_swap_vars(bset, 2*dim);
5586 for (i = 0; i < dim; ++i) {
5587 int j = isl_basic_map_alloc_equality(
5588 (struct isl_basic_map *)bset);
5591 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5592 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5593 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5594 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5596 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5597 bset = isl_basic_set_reset_dim(bset, target_dim);
5600 isl_basic_map_free(bmap);
5605 * returns range - domain
5607 struct isl_set *isl_map_deltas(struct isl_map *map)
5611 struct isl_set *result;
5616 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5617 map->dim, isl_dim_out),
5619 dim = isl_map_get_dim(map);
5620 dim = isl_dim_domain(dim);
5621 result = isl_set_alloc_dim(dim, map->n, 0);
5624 for (i = 0; i < map->n; ++i)
5625 result = isl_set_add_basic_set(result,
5626 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5635 * returns [domain -> range] -> range - domain
5637 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5638 __isl_take isl_basic_map *bmap)
5642 isl_basic_map *domain;
5643 isl_basic_set *bset;
5647 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5648 isl_die(bmap->ctx, isl_error_invalid,
5649 "domain and range don't match", goto error);
5651 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5652 n = isl_basic_map_dim(bmap, isl_dim_in);
5654 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5655 domain = isl_basic_map_universe(dim);
5657 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5658 bmap = isl_basic_map_apply_range(bmap, domain);
5659 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5661 total = isl_basic_map_total_dim(bmap);
5663 for (i = 0; i < n; ++i) {
5664 k = isl_basic_map_alloc_equality(bmap);
5667 isl_seq_clr(bmap->eq[k], 1 + total);
5668 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5669 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5670 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5673 bmap = isl_basic_map_gauss(bmap, NULL);
5674 return isl_basic_map_finalize(bmap);
5676 isl_basic_map_free(bmap);
5681 * returns [domain -> range] -> range - domain
5683 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5686 isl_dim *domain_dim;
5691 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5692 isl_die(map->ctx, isl_error_invalid,
5693 "domain and range don't match", goto error);
5695 map = isl_map_cow(map);
5699 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5700 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5701 map->dim = isl_dim_join(map->dim, domain_dim);
5704 for (i = 0; i < map->n; ++i) {
5705 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5709 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5716 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5718 struct isl_basic_map *bmap;
5726 nparam = dims->nparam;
5728 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5732 for (i = 0; i < dim; ++i) {
5733 int j = isl_basic_map_alloc_equality(bmap);
5736 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5737 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5738 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5740 return isl_basic_map_finalize(bmap);
5742 isl_basic_map_free(bmap);
5746 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5750 if (dim->n_in != dim->n_out)
5751 isl_die(dim->ctx, isl_error_invalid,
5752 "number of input and output dimensions needs to be "
5753 "the same", goto error);
5754 return basic_map_identity(dim);
5760 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5762 if (!model || !model->dim)
5764 return isl_basic_map_identity(isl_dim_copy(model->dim));
5767 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5769 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5772 struct isl_map *isl_map_identity_like(struct isl_map *model)
5774 if (!model || !model->dim)
5776 return isl_map_identity(isl_dim_copy(model->dim));
5779 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5781 if (!model || !model->dim)
5783 return isl_map_identity(isl_dim_copy(model->dim));
5786 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5788 isl_dim *dim = isl_set_get_dim(set);
5790 id = isl_map_identity(isl_dim_map_from_set(dim));
5791 return isl_map_intersect_range(id, set);
5794 /* Construct a basic set with all set dimensions having only non-negative
5797 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5802 struct isl_basic_set *bset;
5806 nparam = dims->nparam;
5808 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5811 for (i = 0; i < dim; ++i) {
5812 int k = isl_basic_set_alloc_inequality(bset);
5815 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5816 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5820 isl_basic_set_free(bset);
5824 /* Construct the half-space x_pos >= 0.
5826 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5830 isl_basic_set *nonneg;
5832 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5833 k = isl_basic_set_alloc_inequality(nonneg);
5836 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5837 isl_int_set_si(nonneg->ineq[k][pos], 1);
5839 return isl_basic_set_finalize(nonneg);
5841 isl_basic_set_free(nonneg);
5845 /* Construct the half-space x_pos <= -1.
5847 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5852 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5853 k = isl_basic_set_alloc_inequality(neg);
5856 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5857 isl_int_set_si(neg->ineq[k][0], -1);
5858 isl_int_set_si(neg->ineq[k][pos], -1);
5860 return isl_basic_set_finalize(neg);
5862 isl_basic_set_free(neg);
5866 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5867 enum isl_dim_type type, unsigned first, unsigned n)
5870 isl_basic_set *nonneg;
5878 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5880 for (i = 0; i < n; ++i) {
5881 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5882 pos(set->dim, type) + first + i);
5883 neg = neg_halfspace(isl_set_get_dim(set),
5884 pos(set->dim, type) + first + i);
5886 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5895 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5896 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5903 if (isl_set_fast_is_empty(set)) {
5908 return fn(set, signs, user);
5911 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5913 half = isl_set_intersect(half, isl_set_copy(set));
5914 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5918 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5920 half = isl_set_intersect(half, set);
5921 return foreach_orthant(half, signs, first + 1, len, fn, user);
5927 /* Call "fn" on the intersections of "set" with each of the orthants
5928 * (except for obviously empty intersections). The orthant is identified
5929 * by the signs array, with each entry having value 1 or -1 according
5930 * to the sign of the corresponding variable.
5932 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5933 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5943 if (isl_set_fast_is_empty(set))
5946 nparam = isl_set_dim(set, isl_dim_param);
5947 nvar = isl_set_dim(set, isl_dim_set);
5949 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5951 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5959 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5961 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5964 int isl_basic_map_is_subset(
5965 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5968 struct isl_map *map1;
5969 struct isl_map *map2;
5971 if (!bmap1 || !bmap2)
5974 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5975 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5977 is_subset = isl_map_is_subset(map1, map2);
5985 int isl_basic_map_is_equal(
5986 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5990 if (!bmap1 || !bmap2)
5992 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5995 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5999 int isl_basic_set_is_equal(
6000 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6002 return isl_basic_map_is_equal(
6003 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6006 int isl_map_is_empty(struct isl_map *map)
6013 for (i = 0; i < map->n; ++i) {
6014 is_empty = isl_basic_map_is_empty(map->p[i]);
6023 int isl_map_fast_is_empty(struct isl_map *map)
6025 return map ? map->n == 0 : -1;
6028 int isl_set_fast_is_empty(struct isl_set *set)
6030 return set ? set->n == 0 : -1;
6033 int isl_set_is_empty(struct isl_set *set)
6035 return isl_map_is_empty((struct isl_map *)set);
6038 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6043 return isl_dim_equal(map1->dim, map2->dim);
6046 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6051 return isl_dim_equal(set1->dim, set2->dim);
6054 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6060 is_subset = isl_map_is_subset(map1, map2);
6063 is_subset = isl_map_is_subset(map2, map1);
6067 int isl_basic_map_is_strict_subset(
6068 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6072 if (!bmap1 || !bmap2)
6074 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6077 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6078 if (is_subset == -1)
6083 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6089 is_subset = isl_map_is_subset(map1, map2);
6092 is_subset = isl_map_is_subset(map2, map1);
6093 if (is_subset == -1)
6098 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6100 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6103 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6107 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6110 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6114 return bset->n_eq == 0 && bset->n_ineq == 0;
6117 int isl_map_fast_is_universe(__isl_keep isl_map *map)
6124 for (i = 0; i < map->n; ++i) {
6125 int r = isl_basic_map_is_universe(map->p[i]);
6133 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6135 return isl_map_fast_is_universe((isl_map *) set);
6138 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6140 struct isl_basic_set *bset = NULL;
6141 struct isl_vec *sample = NULL;
6148 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6151 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6152 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6153 copy = isl_basic_map_remove_redundancies(copy);
6154 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6155 isl_basic_map_free(copy);
6159 total = 1 + isl_basic_map_total_dim(bmap);
6160 if (bmap->sample && bmap->sample->size == total) {
6161 int contains = isl_basic_map_contains(bmap, bmap->sample);
6167 isl_vec_free(bmap->sample);
6168 bmap->sample = NULL;
6169 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6172 sample = isl_basic_set_sample_vec(bset);
6175 empty = sample->size == 0;
6176 isl_vec_free(bmap->sample);
6177 bmap->sample = sample;
6179 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6184 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
6188 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6191 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
6195 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6198 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6200 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6203 struct isl_map *isl_basic_map_union(
6204 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6206 struct isl_map *map;
6207 if (!bmap1 || !bmap2)
6210 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6212 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6215 map = isl_map_add_basic_map(map, bmap1);
6216 map = isl_map_add_basic_map(map, bmap2);
6219 isl_basic_map_free(bmap1);
6220 isl_basic_map_free(bmap2);
6224 struct isl_set *isl_basic_set_union(
6225 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6227 return (struct isl_set *)isl_basic_map_union(
6228 (struct isl_basic_map *)bset1,
6229 (struct isl_basic_map *)bset2);
6232 /* Order divs such that any div only depends on previous divs */
6233 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6241 off = isl_dim_total(bmap->dim);
6243 for (i = 0; i < bmap->n_div; ++i) {
6245 if (isl_int_is_zero(bmap->div[i][0]))
6247 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6251 isl_basic_map_swap_div(bmap, i, i + pos);
6257 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6259 return (struct isl_basic_set *)
6260 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6263 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6270 for (i = 0; i < map->n; ++i) {
6271 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6282 /* Look for a div in dst that corresponds to the div "div" in src.
6283 * The divs before "div" in src and dst are assumed to be the same.
6285 * Returns -1 if no corresponding div was found and the position
6286 * of the corresponding div in dst otherwise.
6288 static int find_div(struct isl_basic_map *dst,
6289 struct isl_basic_map *src, unsigned div)
6293 unsigned total = isl_dim_total(src->dim);
6295 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6296 for (i = div; i < dst->n_div; ++i)
6297 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6298 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6299 dst->n_div - div) == -1)
6304 struct isl_basic_map *isl_basic_map_align_divs(
6305 struct isl_basic_map *dst, struct isl_basic_map *src)
6308 unsigned total = isl_dim_total(src->dim);
6313 if (src->n_div == 0)
6316 for (i = 0; i < src->n_div; ++i)
6317 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6319 src = isl_basic_map_order_divs(src);
6320 dst = isl_basic_map_cow(dst);
6321 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6322 src->n_div, 0, 2 * src->n_div);
6325 for (i = 0; i < src->n_div; ++i) {
6326 int j = find_div(dst, src, i);
6328 j = isl_basic_map_alloc_div(dst);
6331 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6332 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6333 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6337 isl_basic_map_swap_div(dst, i, j);
6341 isl_basic_map_free(dst);
6345 struct isl_basic_set *isl_basic_set_align_divs(
6346 struct isl_basic_set *dst, struct isl_basic_set *src)
6348 return (struct isl_basic_set *)isl_basic_map_align_divs(
6349 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6352 struct isl_map *isl_map_align_divs(struct isl_map *map)
6360 map = isl_map_compute_divs(map);
6361 map = isl_map_cow(map);
6365 for (i = 1; i < map->n; ++i)
6366 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6367 for (i = 1; i < map->n; ++i)
6368 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6370 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6374 struct isl_set *isl_set_align_divs(struct isl_set *set)
6376 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6379 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6383 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6384 map = isl_map_intersect_domain(map, set);
6385 set = isl_map_range(map);
6393 /* There is no need to cow as removing empty parts doesn't change
6394 * the meaning of the set.
6396 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6403 for (i = map->n-1; i >= 0; --i) {
6404 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6406 isl_basic_map_free(map->p[i]);
6407 if (i != map->n-1) {
6408 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6409 map->p[i] = map->p[map->n-1];
6417 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6419 return (struct isl_set *)
6420 isl_map_remove_empty_parts((struct isl_map *)set);
6423 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6425 struct isl_basic_map *bmap;
6426 if (!map || map->n == 0)
6428 bmap = map->p[map->n-1];
6429 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6430 return isl_basic_map_copy(bmap);
6433 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6435 return (struct isl_basic_set *)
6436 isl_map_copy_basic_map((struct isl_map *)set);
6439 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6440 __isl_keep isl_basic_map *bmap)
6446 for (i = map->n-1; i >= 0; --i) {
6447 if (map->p[i] != bmap)
6449 map = isl_map_cow(map);
6452 isl_basic_map_free(map->p[i]);
6453 if (i != map->n-1) {
6454 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6455 map->p[i] = map->p[map->n-1];
6466 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6467 struct isl_basic_set *bset)
6469 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6470 (struct isl_basic_map *)bset);
6473 /* Given two basic sets bset1 and bset2, compute the maximal difference
6474 * between the values of dimension pos in bset1 and those in bset2
6475 * for any common value of the parameters and dimensions preceding pos.
6477 static enum isl_lp_result basic_set_maximal_difference_at(
6478 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6479 int pos, isl_int *opt)
6481 struct isl_dim *dims;
6482 struct isl_basic_map *bmap1 = NULL;
6483 struct isl_basic_map *bmap2 = NULL;
6484 struct isl_ctx *ctx;
6485 struct isl_vec *obj;
6488 unsigned dim1, dim2;
6489 enum isl_lp_result res;
6491 if (!bset1 || !bset2)
6492 return isl_lp_error;
6494 nparam = isl_basic_set_n_param(bset1);
6495 dim1 = isl_basic_set_n_dim(bset1);
6496 dim2 = isl_basic_set_n_dim(bset2);
6497 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6498 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6499 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6500 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6501 if (!bmap1 || !bmap2)
6503 bmap1 = isl_basic_map_cow(bmap1);
6504 bmap1 = isl_basic_map_extend(bmap1, nparam,
6505 pos, (dim1 - pos) + (dim2 - pos),
6506 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6507 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6510 total = isl_basic_map_total_dim(bmap1);
6512 obj = isl_vec_alloc(ctx, 1 + total);
6513 isl_seq_clr(obj->block.data, 1 + total);
6514 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6515 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6518 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6520 isl_basic_map_free(bmap1);
6524 isl_basic_map_free(bmap1);
6525 isl_basic_map_free(bmap2);
6526 return isl_lp_error;
6529 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6530 * for any common value of the parameters and dimensions preceding pos
6531 * in both basic sets, the values of dimension pos in bset1 are
6532 * smaller or larger than those in bset2.
6535 * 1 if bset1 follows bset2
6536 * -1 if bset1 precedes bset2
6537 * 0 if bset1 and bset2 are incomparable
6538 * -2 if some error occurred.
6540 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6541 struct isl_basic_set *bset2, int pos)
6544 enum isl_lp_result res;
6549 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6551 if (res == isl_lp_empty)
6553 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6554 res == isl_lp_unbounded)
6556 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6565 /* Given two basic sets bset1 and bset2, check whether
6566 * for any common value of the parameters and dimensions preceding pos
6567 * there is a value of dimension pos in bset1 that is larger
6568 * than a value of the same dimension in bset2.
6571 * 1 if there exists such a pair
6572 * 0 if there is no such pair, but there is a pair of equal values
6574 * -2 if some error occurred.
6576 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6577 __isl_keep isl_basic_set *bset2, int pos)
6580 enum isl_lp_result res;
6585 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6587 if (res == isl_lp_empty)
6589 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6590 res == isl_lp_unbounded)
6592 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6594 else if (res == isl_lp_ok)
6603 /* Given two sets set1 and set2, check whether
6604 * for any common value of the parameters and dimensions preceding pos
6605 * there is a value of dimension pos in set1 that is larger
6606 * than a value of the same dimension in set2.
6609 * 1 if there exists such a pair
6610 * 0 if there is no such pair, but there is a pair of equal values
6612 * -2 if some error occurred.
6614 int isl_set_follows_at(__isl_keep isl_set *set1,
6615 __isl_keep isl_set *set2, int pos)
6623 for (i = 0; i < set1->n; ++i)
6624 for (j = 0; j < set2->n; ++j) {
6626 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6627 if (f == 1 || f == -2)
6636 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
6637 unsigned pos, isl_int *val)
6645 total = isl_basic_map_total_dim(bmap);
6646 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6647 for (; d+1 > pos; --d)
6648 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6652 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6654 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6656 if (!isl_int_is_one(bmap->eq[i][1+d]))
6659 isl_int_neg(*val, bmap->eq[i][0]);
6665 static int isl_map_fast_has_fixed_var(struct isl_map *map,
6666 unsigned pos, isl_int *val)
6678 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
6681 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
6682 for (i = 1; fixed == 1 && i < map->n; ++i) {
6683 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
6684 if (fixed == 1 && isl_int_ne(tmp, v))
6688 isl_int_set(*val, v);
6694 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
6695 unsigned pos, isl_int *val)
6697 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
6701 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
6704 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
6707 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
6708 enum isl_dim_type type, unsigned pos, isl_int *val)
6710 if (pos >= isl_basic_map_dim(bmap, type))
6712 return isl_basic_map_fast_has_fixed_var(bmap,
6713 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6716 int isl_map_fast_is_fixed(struct isl_map *map,
6717 enum isl_dim_type type, unsigned pos, isl_int *val)
6719 if (pos >= isl_map_dim(map, type))
6721 return isl_map_fast_has_fixed_var(map,
6722 map_offset(map, type) - 1 + pos, val);
6725 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6726 * then return this fixed value in *val.
6728 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6731 return isl_basic_set_fast_has_fixed_var(bset,
6732 isl_basic_set_n_param(bset) + dim, val);
6735 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6736 * then return this fixed value in *val.
6738 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6740 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6743 /* Check if input variable in has fixed value and if so and if val is not NULL,
6744 * then return this fixed value in *val.
6746 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6748 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6751 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6752 * and if val is not NULL, then return this lower bound in *val.
6754 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6755 unsigned dim, isl_int *val)
6757 int i, i_eq = -1, i_ineq = -1;
6764 total = isl_basic_set_total_dim(bset);
6765 nparam = isl_basic_set_n_param(bset);
6766 for (i = 0; i < bset->n_eq; ++i) {
6767 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6773 for (i = 0; i < bset->n_ineq; ++i) {
6774 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6776 if (i_eq != -1 || i_ineq != -1)
6780 if (i_eq == -1 && i_ineq == -1)
6782 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6783 /* The coefficient should always be one due to normalization. */
6784 if (!isl_int_is_one(c[1+nparam+dim]))
6786 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6788 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6789 total - nparam - dim - 1) != -1)
6792 isl_int_neg(*val, c[0]);
6796 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6797 unsigned dim, isl_int *val)
6809 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6813 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6815 for (i = 1; fixed == 1 && i < set->n; ++i) {
6816 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6818 if (fixed == 1 && isl_int_ne(tmp, v))
6822 isl_int_set(*val, v);
6833 /* uset_gist depends on constraints without existentially quantified
6834 * variables sorting first.
6836 static int qsort_constraint_cmp(const void *p1, const void *p2)
6838 const struct constraint *c1 = (const struct constraint *)p1;
6839 const struct constraint *c2 = (const struct constraint *)p2;
6841 unsigned size = isl_min(c1->size, c2->size);
6843 l1 = isl_seq_last_non_zero(c1->c, size);
6844 l2 = isl_seq_last_non_zero(c2->c, size);
6849 return isl_seq_cmp(c1->c, c2->c, size);
6852 static struct isl_basic_map *isl_basic_map_sort_constraints(
6853 struct isl_basic_map *bmap)
6856 struct constraint *c;
6861 total = isl_basic_map_total_dim(bmap);
6862 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6865 for (i = 0; i < bmap->n_ineq; ++i) {
6867 c[i].c = bmap->ineq[i];
6869 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6870 for (i = 0; i < bmap->n_ineq; ++i)
6871 bmap->ineq[i] = c[i].c;
6875 isl_basic_map_free(bmap);
6879 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6880 __isl_take isl_basic_set *bset)
6882 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6883 (struct isl_basic_map *)bset);
6886 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6890 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6892 bmap = isl_basic_map_remove_redundancies(bmap);
6893 bmap = isl_basic_map_sort_constraints(bmap);
6894 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6898 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6900 return (struct isl_basic_set *)isl_basic_map_normalize(
6901 (struct isl_basic_map *)bset);
6904 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6905 const __isl_keep isl_basic_map *bmap2)
6912 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
6913 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
6914 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
6915 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6916 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6917 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6918 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6919 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6920 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6921 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6922 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6924 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6926 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6928 if (bmap1->n_eq != bmap2->n_eq)
6929 return bmap1->n_eq - bmap2->n_eq;
6930 if (bmap1->n_ineq != bmap2->n_ineq)
6931 return bmap1->n_ineq - bmap2->n_ineq;
6932 if (bmap1->n_div != bmap2->n_div)
6933 return bmap1->n_div - bmap2->n_div;
6934 total = isl_basic_map_total_dim(bmap1);
6935 for (i = 0; i < bmap1->n_eq; ++i) {
6936 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6940 for (i = 0; i < bmap1->n_ineq; ++i) {
6941 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6945 for (i = 0; i < bmap1->n_div; ++i) {
6946 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6953 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6954 __isl_keep isl_basic_map *bmap2)
6956 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6959 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6960 __isl_keep isl_basic_set *bset2)
6962 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6963 (isl_basic_map *)bset2);
6966 static int qsort_bmap_cmp(const void *p1, const void *p2)
6968 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6969 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6971 return isl_basic_map_fast_cmp(bmap1, bmap2);
6974 /* We normalize in place, but if anything goes wrong we need
6975 * to return NULL, so we need to make sure we don't change the
6976 * meaning of any possible other copies of map.
6978 struct isl_map *isl_map_normalize(struct isl_map *map)
6981 struct isl_basic_map *bmap;
6985 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6987 for (i = 0; i < map->n; ++i) {
6988 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6991 isl_basic_map_free(map->p[i]);
6994 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6995 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6996 map = isl_map_remove_empty_parts(map);
6999 for (i = map->n - 1; i >= 1; --i) {
7000 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
7002 isl_basic_map_free(map->p[i-1]);
7003 for (j = i; j < map->n; ++j)
7004 map->p[j-1] = map->p[j];
7014 struct isl_set *isl_set_normalize(struct isl_set *set)
7016 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7019 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
7029 if (!isl_dim_equal(map1->dim, map2->dim))
7032 map1 = isl_map_copy(map1);
7033 map2 = isl_map_copy(map2);
7034 map1 = isl_map_normalize(map1);
7035 map2 = isl_map_normalize(map2);
7038 equal = map1->n == map2->n;
7039 for (i = 0; equal && i < map1->n; ++i) {
7040 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
7053 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
7055 return isl_map_fast_is_equal((struct isl_map *)set1,
7056 (struct isl_map *)set2);
7059 /* Return an interval that ranges from min to max (inclusive)
7061 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7062 isl_int min, isl_int max)
7065 struct isl_basic_set *bset = NULL;
7067 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7071 k = isl_basic_set_alloc_inequality(bset);
7074 isl_int_set_si(bset->ineq[k][1], 1);
7075 isl_int_neg(bset->ineq[k][0], min);
7077 k = isl_basic_set_alloc_inequality(bset);
7080 isl_int_set_si(bset->ineq[k][1], -1);
7081 isl_int_set(bset->ineq[k][0], max);
7085 isl_basic_set_free(bset);
7089 /* Return the Cartesian product of the basic sets in list (in the given order).
7091 __isl_give isl_basic_set *isl_basic_set_list_product(
7092 __isl_take struct isl_basic_set_list *list)
7100 struct isl_basic_set *product = NULL;
7104 isl_assert(list->ctx, list->n > 0, goto error);
7105 isl_assert(list->ctx, list->p[0], goto error);
7106 nparam = isl_basic_set_n_param(list->p[0]);
7107 dim = isl_basic_set_n_dim(list->p[0]);
7108 extra = list->p[0]->n_div;
7109 n_eq = list->p[0]->n_eq;
7110 n_ineq = list->p[0]->n_ineq;
7111 for (i = 1; i < list->n; ++i) {
7112 isl_assert(list->ctx, list->p[i], goto error);
7113 isl_assert(list->ctx,
7114 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7115 dim += isl_basic_set_n_dim(list->p[i]);
7116 extra += list->p[i]->n_div;
7117 n_eq += list->p[i]->n_eq;
7118 n_ineq += list->p[i]->n_ineq;
7120 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7125 for (i = 0; i < list->n; ++i) {
7126 isl_basic_set_add_constraints(product,
7127 isl_basic_set_copy(list->p[i]), dim);
7128 dim += isl_basic_set_n_dim(list->p[i]);
7130 isl_basic_set_list_free(list);
7133 isl_basic_set_free(product);
7134 isl_basic_set_list_free(list);
7138 struct isl_basic_map *isl_basic_map_product(
7139 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7141 struct isl_dim *dim_result = NULL;
7142 struct isl_basic_map *bmap;
7143 unsigned in1, in2, out1, out2, nparam, total, pos;
7144 struct isl_dim_map *dim_map1, *dim_map2;
7146 if (!bmap1 || !bmap2)
7149 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7150 bmap2->dim, isl_dim_param), goto error);
7151 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7152 isl_dim_copy(bmap2->dim));
7154 in1 = isl_basic_map_n_in(bmap1);
7155 in2 = isl_basic_map_n_in(bmap2);
7156 out1 = isl_basic_map_n_out(bmap1);
7157 out2 = isl_basic_map_n_out(bmap2);
7158 nparam = isl_basic_map_n_param(bmap1);
7160 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7161 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7162 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7163 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7164 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7165 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7166 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7167 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7168 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7169 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7170 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7172 bmap = isl_basic_map_alloc_dim(dim_result,
7173 bmap1->n_div + bmap2->n_div,
7174 bmap1->n_eq + bmap2->n_eq,
7175 bmap1->n_ineq + bmap2->n_ineq);
7176 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
7177 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
7178 bmap = isl_basic_map_simplify(bmap);
7179 return isl_basic_map_finalize(bmap);
7181 isl_basic_map_free(bmap1);
7182 isl_basic_map_free(bmap2);
7186 __isl_give isl_basic_map *isl_basic_map_flat_product(
7187 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7189 isl_basic_map *prod;
7191 prod = isl_basic_map_product(bmap1, bmap2);
7192 prod = isl_basic_map_flatten(prod);
7196 __isl_give isl_basic_set *isl_basic_set_flat_product(
7197 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7199 return isl_basic_map_flat_product(bset1, bset2);
7202 __isl_give isl_basic_map *isl_basic_map_range_product(
7203 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7205 isl_dim *dim_result = NULL;
7206 isl_basic_map *bmap;
7207 unsigned in, out1, out2, nparam, total, pos;
7208 struct isl_dim_map *dim_map1, *dim_map2;
7210 if (!bmap1 || !bmap2)
7213 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7214 isl_dim_copy(bmap2->dim));
7216 in = isl_basic_map_dim(bmap1, isl_dim_in);
7217 out1 = isl_basic_map_n_out(bmap1);
7218 out2 = isl_basic_map_n_out(bmap2);
7219 nparam = isl_basic_map_n_param(bmap1);
7221 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7222 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7223 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7224 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7225 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7226 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7227 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7228 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7229 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7230 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7231 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7233 bmap = isl_basic_map_alloc_dim(dim_result,
7234 bmap1->n_div + bmap2->n_div,
7235 bmap1->n_eq + bmap2->n_eq,
7236 bmap1->n_ineq + bmap2->n_ineq);
7237 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
7238 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
7239 bmap = isl_basic_map_simplify(bmap);
7240 return isl_basic_map_finalize(bmap);
7242 isl_basic_map_free(bmap1);
7243 isl_basic_map_free(bmap2);
7247 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7248 __isl_take isl_map *map2,
7249 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7250 __isl_take isl_dim *right),
7251 __isl_give isl_basic_map *(*basic_map_product)(
7252 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7255 struct isl_map *result;
7261 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7262 map2->dim, isl_dim_param), goto error);
7264 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7265 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7266 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7268 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7269 isl_dim_copy(map2->dim)),
7270 map1->n * map2->n, flags);
7273 for (i = 0; i < map1->n; ++i)
7274 for (j = 0; j < map2->n; ++j) {
7275 struct isl_basic_map *part;
7276 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7277 isl_basic_map_copy(map2->p[j]));
7278 if (isl_basic_map_is_empty(part))
7279 isl_basic_map_free(part);
7281 result = isl_map_add_basic_map(result, part);
7294 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7296 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7298 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7301 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7303 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7304 __isl_take isl_map *map2)
7308 prod = isl_map_product(map1, map2);
7309 prod = isl_map_flatten(prod);
7313 /* Given two set A and B, construct its Cartesian product A x B.
7315 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7317 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7318 (struct isl_map *)set2);
7321 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7322 __isl_take isl_set *set2)
7324 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7327 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7329 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7330 __isl_take isl_map *map2)
7332 return map_product(map1, map2, &isl_dim_range_product,
7333 &isl_basic_map_range_product);
7336 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7339 uint32_t hash = isl_hash_init();
7344 bmap = isl_basic_map_copy(bmap);
7345 bmap = isl_basic_map_normalize(bmap);
7348 total = isl_basic_map_total_dim(bmap);
7349 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7350 for (i = 0; i < bmap->n_eq; ++i) {
7352 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7353 isl_hash_hash(hash, c_hash);
7355 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7356 for (i = 0; i < bmap->n_ineq; ++i) {
7358 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7359 isl_hash_hash(hash, c_hash);
7361 isl_hash_byte(hash, bmap->n_div & 0xFF);
7362 for (i = 0; i < bmap->n_div; ++i) {
7364 if (isl_int_is_zero(bmap->div[i][0]))
7366 isl_hash_byte(hash, i & 0xFF);
7367 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7368 isl_hash_hash(hash, c_hash);
7370 isl_basic_map_free(bmap);
7374 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7376 return isl_basic_map_get_hash((isl_basic_map *)bset);
7379 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7386 map = isl_map_copy(map);
7387 map = isl_map_normalize(map);
7391 hash = isl_hash_init();
7392 for (i = 0; i < map->n; ++i) {
7394 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7395 isl_hash_hash(hash, bmap_hash);
7403 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7405 return isl_map_get_hash((isl_map *)set);
7408 /* Check if the value for dimension dim is completely determined
7409 * by the values of the other parameters and variables.
7410 * That is, check if dimension dim is involved in an equality.
7412 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7419 nparam = isl_basic_set_n_param(bset);
7420 for (i = 0; i < bset->n_eq; ++i)
7421 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7426 /* Check if the value for dimension dim is completely determined
7427 * by the values of the other parameters and variables.
7428 * That is, check if dimension dim is involved in an equality
7429 * for each of the subsets.
7431 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7437 for (i = 0; i < set->n; ++i) {
7439 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7446 int isl_set_n_basic_set(__isl_keep isl_set *set)
7448 return set ? set->n : 0;
7451 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7452 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7459 for (i = 0; i < map->n; ++i)
7460 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7466 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7467 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7474 for (i = 0; i < set->n; ++i)
7475 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7481 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7483 struct isl_dim *dim;
7488 bset = isl_basic_set_cow(bset);
7492 dim = isl_basic_set_get_dim(bset);
7493 dim = isl_dim_lift(dim, bset->n_div);
7496 isl_dim_free(bset->dim);
7498 bset->extra -= bset->n_div;
7501 bset = isl_basic_set_finalize(bset);
7505 isl_basic_set_free(bset);
7509 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7512 struct isl_dim *dim;
7515 set = isl_set_align_divs(set);
7520 set = isl_set_cow(set);
7524 n_div = set->p[0]->n_div;
7525 dim = isl_set_get_dim(set);
7526 dim = isl_dim_lift(dim, n_div);
7529 isl_dim_free(set->dim);
7532 for (i = 0; i < set->n; ++i) {
7533 set->p[i] = isl_basic_set_lift(set->p[i]);
7544 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7546 struct isl_dim *dim;
7547 struct isl_basic_map *bmap;
7554 set = isl_set_align_divs(set);
7559 dim = isl_set_get_dim(set);
7560 if (set->n == 0 || set->p[0]->n_div == 0) {
7562 return isl_map_identity(isl_dim_map_from_set(dim));
7565 n_div = set->p[0]->n_div;
7566 dim = isl_dim_map_from_set(dim);
7567 n_param = isl_dim_size(dim, isl_dim_param);
7568 n_set = isl_dim_size(dim, isl_dim_in);
7569 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7570 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7571 for (i = 0; i < n_set; ++i)
7572 bmap = var_equal(bmap, i);
7574 total = n_param + n_set + n_set + n_div;
7575 for (i = 0; i < n_div; ++i) {
7576 k = isl_basic_map_alloc_inequality(bmap);
7579 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7580 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7581 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7582 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7583 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7584 set->p[0]->div[i][0]);
7586 l = isl_basic_map_alloc_inequality(bmap);
7589 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7590 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7591 set->p[0]->div[i][0]);
7592 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7596 bmap = isl_basic_map_simplify(bmap);
7597 bmap = isl_basic_map_finalize(bmap);
7598 return isl_map_from_basic_map(bmap);
7601 isl_basic_map_free(bmap);
7605 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7613 dim = isl_basic_set_total_dim(bset);
7614 size += bset->n_eq * (1 + dim);
7615 size += bset->n_ineq * (1 + dim);
7616 size += bset->n_div * (2 + dim);
7621 int isl_set_size(__isl_keep isl_set *set)
7629 for (i = 0; i < set->n; ++i)
7630 size += isl_basic_set_size(set->p[i]);
7635 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7636 enum isl_dim_type type, unsigned pos)
7644 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7646 pos += isl_basic_map_offset(bmap, type);
7648 for (i = 0; i < bmap->n_eq; ++i)
7649 if (!isl_int_is_zero(bmap->eq[i][pos]))
7653 for (i = 0; i < bmap->n_ineq; ++i) {
7654 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7661 return lower && upper;
7664 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7665 enum isl_dim_type type, unsigned pos)
7672 for (i = 0; i < map->n; ++i) {
7674 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7675 if (bounded < 0 || !bounded)
7682 /* Return 1 if the specified dim is involved in both an upper bound
7683 * and a lower bound.
7685 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7686 enum isl_dim_type type, unsigned pos)
7688 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7691 /* For each of the "n" variables starting at "first", determine
7692 * the sign of the variable and put the results in the first "n"
7693 * elements of the array "signs".
7695 * 1 means that the variable is non-negative
7696 * -1 means that the variable is non-positive
7697 * 0 means the variable attains both positive and negative values.
7699 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7700 unsigned first, unsigned n, int *signs)
7702 isl_vec *bound = NULL;
7703 struct isl_tab *tab = NULL;
7704 struct isl_tab_undo *snap;
7707 if (!bset || !signs)
7710 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7711 tab = isl_tab_from_basic_set(bset);
7715 isl_seq_clr(bound->el, bound->size);
7716 isl_int_set_si(bound->el[0], -1);
7718 snap = isl_tab_snap(tab);
7719 for (i = 0; i < n; ++i) {
7722 isl_int_set_si(bound->el[1 + first + i], -1);
7723 if (isl_tab_add_ineq(tab, bound->el) < 0)
7726 isl_int_set_si(bound->el[1 + first + i], 0);
7727 if (isl_tab_rollback(tab, snap) < 0)
7735 isl_int_set_si(bound->el[1 + first + i], 1);
7736 if (isl_tab_add_ineq(tab, bound->el) < 0)
7739 isl_int_set_si(bound->el[1 + first + i], 0);
7740 if (isl_tab_rollback(tab, snap) < 0)
7743 signs[i] = empty ? -1 : 0;
7747 isl_vec_free(bound);
7751 isl_vec_free(bound);
7755 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7756 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7758 if (!bset || !signs)
7760 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7763 first += pos(bset->dim, type) - 1;
7764 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7767 /* Check if the given basic map is obviously single-valued.
7768 * In particular, for each output dimension, check that there is
7769 * an equality that defines the output dimension in terms of
7770 * earlier dimensions.
7772 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map *bmap)
7782 total = 1 + isl_basic_map_total_dim(bmap);
7783 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7784 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7786 for (i = 0; i < n_out; ++i) {
7787 for (j = 0; j < bmap->n_eq; ++j) {
7788 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7790 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7791 total - (o_out + i + 1)) == -1)
7794 if (j >= bmap->n_eq)
7801 /* Check if the given map is obviously single-valued.
7803 int isl_map_fast_is_single_valued(__isl_keep isl_map *map)
7814 return isl_basic_map_fast_is_single_valued(map->p[0]);
7817 /* Check if the given map is single-valued.
7822 * and check if the result is a subset of the identity mapping.
7824 int isl_map_is_single_valued(__isl_keep isl_map *map)
7831 sv = isl_map_fast_is_single_valued(map);
7835 test = isl_map_reverse(isl_map_copy(map));
7836 test = isl_map_apply_range(test, isl_map_copy(map));
7838 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
7839 id = isl_map_identity(dim);
7841 sv = isl_map_is_subset(test, id);
7849 int isl_map_is_bijective(__isl_keep isl_map *map)
7853 sv = isl_map_is_single_valued(map);
7857 map = isl_map_copy(map);
7858 map = isl_map_reverse(map);
7859 sv = isl_map_is_single_valued(map);
7865 int isl_set_is_singleton(__isl_keep isl_set *set)
7867 return isl_map_is_single_valued((isl_map *)set);
7870 int isl_map_is_translation(__isl_keep isl_map *map)
7875 delta = isl_map_deltas(isl_map_copy(map));
7876 ok = isl_set_is_singleton(delta);
7877 isl_set_free(delta);
7882 static int unique(isl_int *p, unsigned pos, unsigned len)
7884 if (isl_seq_first_non_zero(p, pos) != -1)
7886 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7891 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7900 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7903 nvar = isl_basic_set_dim(bset, isl_dim_set);
7904 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7905 for (j = 0; j < nvar; ++j) {
7906 int lower = 0, upper = 0;
7907 for (i = 0; i < bset->n_eq; ++i) {
7908 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7910 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7916 for (i = 0; i < bset->n_ineq; ++i) {
7917 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7919 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7921 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7926 if (!lower || !upper)
7933 int isl_set_is_box(__isl_keep isl_set *set)
7940 return isl_basic_set_is_box(set->p[0]);
7943 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7948 return isl_dim_is_wrapping(bset->dim);
7951 int isl_set_is_wrapping(__isl_keep isl_set *set)
7956 return isl_dim_is_wrapping(set->dim);
7959 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7961 bmap = isl_basic_map_cow(bmap);
7965 bmap->dim = isl_dim_wrap(bmap->dim);
7969 bmap = isl_basic_map_finalize(bmap);
7971 return (isl_basic_set *)bmap;
7973 isl_basic_map_free(bmap);
7977 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7981 map = isl_map_cow(map);
7985 for (i = 0; i < map->n; ++i) {
7986 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7990 map->dim = isl_dim_wrap(map->dim);
7994 return (isl_set *)map;
8000 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8002 bset = isl_basic_set_cow(bset);
8006 bset->dim = isl_dim_unwrap(bset->dim);
8010 bset = isl_basic_set_finalize(bset);
8012 return (isl_basic_map *)bset;
8014 isl_basic_set_free(bset);
8018 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8025 if (!isl_set_is_wrapping(set))
8026 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8029 set = isl_set_cow(set);
8033 for (i = 0; i < set->n; ++i) {
8034 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8039 set->dim = isl_dim_unwrap(set->dim);
8043 return (isl_map *)set;
8049 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8050 enum isl_dim_type type)
8055 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8058 bmap = isl_basic_map_cow(bmap);
8062 bmap->dim = isl_dim_reset(bmap->dim, type);
8066 bmap = isl_basic_map_finalize(bmap);
8070 isl_basic_map_free(bmap);
8074 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8075 enum isl_dim_type type)
8082 if (!isl_dim_is_named_or_nested(map->dim, type))
8085 map = isl_map_cow(map);
8089 for (i = 0; i < map->n; ++i) {
8090 map->p[i] = isl_basic_map_reset(map->p[i], type);
8094 map->dim = isl_dim_reset(map->dim, type);
8104 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8109 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8112 bmap = isl_basic_map_cow(bmap);
8116 bmap->dim = isl_dim_flatten(bmap->dim);
8120 bmap = isl_basic_map_finalize(bmap);
8124 isl_basic_map_free(bmap);
8128 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8130 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8133 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8140 if (!map->dim->nested[0] && !map->dim->nested[1])
8143 map = isl_map_cow(map);
8147 for (i = 0; i < map->n; ++i) {
8148 map->p[i] = isl_basic_map_flatten(map->p[i]);
8152 map->dim = isl_dim_flatten(map->dim);
8162 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8164 return (isl_set *)isl_map_flatten((isl_map *)set);
8167 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8169 isl_dim *dim, *flat_dim;
8172 dim = isl_set_get_dim(set);
8173 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8174 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8175 map = isl_map_intersect_domain(map, set);
8180 /* Extend the given dim_map with mappings for the divs in bmap.
8182 static __isl_give struct isl_dim_map *extend_dim_map(
8183 __isl_keep struct isl_dim_map *dim_map,
8184 __isl_keep isl_basic_map *bmap)
8187 struct isl_dim_map *res;
8190 offset = isl_basic_map_offset(bmap, isl_dim_div);
8192 res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
8196 for (i = 0; i < dim_map->len; ++i)
8197 res->pos[i] = dim_map->pos[i];
8198 for (i = 0; i < bmap->n_div; ++i)
8199 res->pos[dim_map->len + i] = offset + i;
8204 /* Extract a dim_map from a reordering.
8205 * We essentially need to reverse the mapping, and add an offset
8206 * of 1 for the constant term.
8208 __isl_give struct isl_dim_map *isl_dim_map_from_reordering(
8209 __isl_keep isl_reordering *exp)
8212 struct isl_dim_map *dim_map;
8217 dim_map = isl_dim_map_alloc(exp->dim->ctx, isl_dim_total(exp->dim));
8221 for (i = 0; i < exp->len; ++i)
8222 dim_map->pos[1 + exp->pos[i]] = 1 + i;
8227 /* Reorder the dimensions of "bmap" according to the given dim_map
8228 * and set the dimension specification to "dim".
8230 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8231 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8235 bmap = isl_basic_map_cow(bmap);
8236 if (!bmap || !dim || !dim_map)
8239 res = isl_basic_map_alloc_dim(dim,
8240 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8241 res = add_constraints_dim_map(res, bmap, dim_map);
8242 res = isl_basic_map_finalize(res);
8246 isl_basic_map_free(bmap);
8251 /* Reorder the dimensions of "map" according to given reordering.
8253 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8254 __isl_take isl_reordering *r)
8257 struct isl_dim_map *dim_map;
8259 map = isl_map_cow(map);
8260 dim_map = isl_dim_map_from_reordering(r);
8261 if (!map || !r || !dim_map)
8264 for (i = 0; i < map->n; ++i) {
8265 struct isl_dim_map *dim_map_i;
8267 dim_map_i = extend_dim_map(dim_map, map->p[i]);
8269 map->p[i] = isl_basic_map_realign(map->p[i],
8270 isl_dim_copy(r->dim), dim_map_i);
8276 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8278 isl_reordering_free(r);
8284 isl_reordering_free(r);
8288 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8289 __isl_take isl_reordering *r)
8291 return (isl_set *)isl_map_realign((isl_map *)set, r);
8294 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8295 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8296 enum isl_dim_type c2, enum isl_dim_type c3,
8297 enum isl_dim_type c4, enum isl_dim_type c5)
8299 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8300 struct isl_mat *mat;
8306 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8307 isl_basic_map_total_dim(bmap) + 1);
8310 for (i = 0; i < bmap->n_eq; ++i)
8311 for (j = 0, pos = 0; j < 5; ++j) {
8312 int off = isl_basic_map_offset(bmap, c[j]);
8313 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8314 isl_int_set(mat->row[i][pos],
8315 bmap->eq[i][off + k]);
8323 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8324 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8325 enum isl_dim_type c2, enum isl_dim_type c3,
8326 enum isl_dim_type c4, enum isl_dim_type c5)
8328 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8329 struct isl_mat *mat;
8335 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8336 isl_basic_map_total_dim(bmap) + 1);
8339 for (i = 0; i < bmap->n_ineq; ++i)
8340 for (j = 0, pos = 0; j < 5; ++j) {
8341 int off = isl_basic_map_offset(bmap, c[j]);
8342 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8343 isl_int_set(mat->row[i][pos],
8344 bmap->ineq[i][off + k]);
8352 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8353 __isl_take isl_dim *dim,
8354 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8355 enum isl_dim_type c2, enum isl_dim_type c3,
8356 enum isl_dim_type c4, enum isl_dim_type c5)
8358 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8359 isl_basic_map *bmap;
8365 if (!dim || !eq || !ineq)
8368 if (eq->n_col != ineq->n_col)
8369 isl_die(dim->ctx, isl_error_invalid,
8370 "equalities and inequalities matrices should have "
8371 "same number of columns", goto error);
8373 total = 1 + isl_dim_total(dim);
8375 if (eq->n_col < total)
8376 isl_die(dim->ctx, isl_error_invalid,
8377 "number of columns too small", goto error);
8379 extra = eq->n_col - total;
8381 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8382 eq->n_row, ineq->n_row);
8385 for (i = 0; i < extra; ++i) {
8386 k = isl_basic_map_alloc_div(bmap);
8389 isl_int_set_si(bmap->div[k][0], 0);
8391 for (i = 0; i < eq->n_row; ++i) {
8392 l = isl_basic_map_alloc_equality(bmap);
8395 for (j = 0, pos = 0; j < 5; ++j) {
8396 int off = isl_basic_map_offset(bmap, c[j]);
8397 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8398 isl_int_set(bmap->eq[l][off + k],
8404 for (i = 0; i < ineq->n_row; ++i) {
8405 l = isl_basic_map_alloc_inequality(bmap);
8408 for (j = 0, pos = 0; j < 5; ++j) {
8409 int off = isl_basic_map_offset(bmap, c[j]);
8410 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8411 isl_int_set(bmap->ineq[l][off + k],
8430 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8431 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8432 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8434 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8435 c1, c2, c3, c4, isl_dim_in);
8438 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8439 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8440 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8442 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8443 c1, c2, c3, c4, isl_dim_in);
8446 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8447 __isl_take isl_dim *dim,
8448 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8449 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8451 return (isl_basic_set*)
8452 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8453 c1, c2, c3, c4, isl_dim_in);
8456 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8461 return isl_dim_can_zip(bmap->dim);
8464 int isl_map_can_zip(__isl_keep isl_map *map)
8469 return isl_dim_can_zip(map->dim);
8472 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8473 * (A -> C) -> (B -> D).
8475 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8484 if (!isl_basic_map_can_zip(bmap))
8485 isl_die(bmap->ctx, isl_error_invalid,
8486 "basic map cannot be zipped", goto error);
8487 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8488 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8489 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8490 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8491 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8494 bmap->dim = isl_dim_zip(bmap->dim);
8499 isl_basic_map_free(bmap);
8503 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8504 * (A -> C) -> (B -> D).
8506 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8513 if (!isl_map_can_zip(map))
8514 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8517 map = isl_map_cow(map);
8521 for (i = 0; i < map->n; ++i) {
8522 map->p[i] = isl_basic_map_zip(map->p[i]);
8527 map->dim = isl_dim_zip(map->dim);