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);
1555 isl_basic_map_free(bmap);
1559 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1560 enum isl_dim_type type, unsigned first, unsigned n)
1569 map = isl_map_cow(map);
1573 for (i = 0; i < map->n; ++i) {
1574 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1585 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1586 enum isl_dim_type type, unsigned first, unsigned n)
1588 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1592 /* Return true if the definition of the given div is unknown or depends
1595 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1598 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1600 if (isl_int_is_zero(bmap->div[div][0]))
1603 for (i = bmap->n_div - 1; i >= 0; --i) {
1604 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1606 if (div_is_unknown(bmap, i))
1613 /* Remove all divs that are unknown or defined in terms of unknown divs.
1615 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1616 __isl_take isl_basic_map *bmap)
1623 for (i = bmap->n_div - 1; i >= 0; --i) {
1624 if (!div_is_unknown(bmap, i))
1626 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1632 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1641 map = isl_map_cow(map);
1645 for (i = 0; i < map->n; ++i) {
1646 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1656 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1658 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1661 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1662 __isl_take isl_basic_set *bset,
1663 enum isl_dim_type type, unsigned first, unsigned n)
1665 return (isl_basic_set *)
1666 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1669 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1670 enum isl_dim_type type, unsigned first, unsigned n)
1677 map = isl_map_cow(map);
1680 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1682 for (i = 0; i < map->n; ++i) {
1683 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1684 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1688 map = isl_map_drop(map, type, first, n);
1695 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1696 enum isl_dim_type type, unsigned first, unsigned n)
1698 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1701 /* Project out n inputs starting at first using Fourier-Motzkin */
1702 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1703 unsigned first, unsigned n)
1705 return isl_map_remove_dims(map, isl_dim_in, first, n);
1708 static void dump_term(struct isl_basic_map *bmap,
1709 isl_int c, int pos, FILE *out)
1712 unsigned in = isl_basic_map_n_in(bmap);
1713 unsigned dim = in + isl_basic_map_n_out(bmap);
1714 unsigned nparam = isl_basic_map_n_param(bmap);
1716 isl_int_print(out, c, 0);
1718 if (!isl_int_is_one(c))
1719 isl_int_print(out, c, 0);
1720 if (pos < 1 + nparam) {
1721 name = isl_dim_get_name(bmap->dim,
1722 isl_dim_param, pos - 1);
1724 fprintf(out, "%s", name);
1726 fprintf(out, "p%d", pos - 1);
1727 } else if (pos < 1 + nparam + in)
1728 fprintf(out, "i%d", pos - 1 - nparam);
1729 else if (pos < 1 + nparam + dim)
1730 fprintf(out, "o%d", pos - 1 - nparam - in);
1732 fprintf(out, "e%d", pos - 1 - nparam - dim);
1736 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1737 int sign, FILE *out)
1741 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1745 for (i = 0, first = 1; i < len; ++i) {
1746 if (isl_int_sgn(c[i]) * sign <= 0)
1749 fprintf(out, " + ");
1751 isl_int_abs(v, c[i]);
1752 dump_term(bmap, v, i, out);
1759 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1760 const char *op, FILE *out, int indent)
1764 fprintf(out, "%*s", indent, "");
1766 dump_constraint_sign(bmap, c, 1, out);
1767 fprintf(out, " %s ", op);
1768 dump_constraint_sign(bmap, c, -1, out);
1772 for (i = bmap->n_div; i < bmap->extra; ++i) {
1773 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1775 fprintf(out, "%*s", indent, "");
1776 fprintf(out, "ERROR: unused div coefficient not zero\n");
1781 static void dump_constraints(struct isl_basic_map *bmap,
1782 isl_int **c, unsigned n,
1783 const char *op, FILE *out, int indent)
1787 for (i = 0; i < n; ++i)
1788 dump_constraint(bmap, c[i], op, out, indent);
1791 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1795 unsigned total = isl_basic_map_total_dim(bmap);
1797 for (j = 0; j < 1 + total; ++j) {
1798 if (isl_int_is_zero(exp[j]))
1800 if (!first && isl_int_is_pos(exp[j]))
1802 dump_term(bmap, exp[j], j, out);
1807 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1811 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1812 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1814 for (i = 0; i < bmap->n_div; ++i) {
1815 fprintf(out, "%*s", indent, "");
1816 fprintf(out, "e%d = [(", i);
1817 dump_affine(bmap, bmap->div[i]+1, out);
1819 isl_int_print(out, bmap->div[i][0], 0);
1820 fprintf(out, "]\n");
1824 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1825 FILE *out, int indent)
1828 fprintf(out, "null basic set\n");
1832 fprintf(out, "%*s", indent, "");
1833 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1834 bset->ref, bset->dim->nparam, bset->dim->n_out,
1835 bset->extra, bset->flags);
1836 dump((struct isl_basic_map *)bset, out, indent);
1839 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1840 FILE *out, int indent)
1843 fprintf(out, "null basic map\n");
1847 fprintf(out, "%*s", indent, "");
1848 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1849 "flags: %x, n_name: %d\n",
1851 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1852 bmap->extra, bmap->flags, bmap->dim->n_name);
1853 dump(bmap, out, indent);
1856 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1861 total = isl_basic_map_total_dim(bmap);
1862 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1863 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1864 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1865 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1869 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1871 struct isl_set *set;
1875 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1876 isl_assert(dim->ctx, n >= 0, goto error);
1877 set = isl_alloc(dim->ctx, struct isl_set,
1878 sizeof(struct isl_set) +
1879 (n - 1) * sizeof(struct isl_basic_set *));
1883 set->ctx = dim->ctx;
1884 isl_ctx_ref(set->ctx);
1896 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1897 unsigned nparam, unsigned dim, int n, unsigned flags)
1899 struct isl_set *set;
1900 struct isl_dim *dims;
1902 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1906 set = isl_set_alloc_dim(dims, n, flags);
1910 /* Make sure "map" has room for at least "n" more basic maps.
1912 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1915 struct isl_map *grown = NULL;
1919 isl_assert(map->ctx, n >= 0, goto error);
1920 if (map->n + n <= map->size)
1922 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1925 for (i = 0; i < map->n; ++i) {
1926 grown->p[i] = isl_basic_map_copy(map->p[i]);
1934 isl_map_free(grown);
1939 /* Make sure "set" has room for at least "n" more basic sets.
1941 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1943 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1946 struct isl_set *isl_set_dup(struct isl_set *set)
1949 struct isl_set *dup;
1954 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1957 for (i = 0; i < set->n; ++i)
1958 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1962 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1964 return isl_map_from_basic_map(bset);
1967 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1969 struct isl_map *map;
1974 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1975 return isl_map_add_basic_map(map, bmap);
1978 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1979 __isl_take isl_basic_set *bset)
1981 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1982 (struct isl_basic_map *)bset);
1985 void isl_set_free(struct isl_set *set)
1995 isl_ctx_deref(set->ctx);
1996 for (i = 0; i < set->n; ++i)
1997 isl_basic_set_free(set->p[i]);
1998 isl_dim_free(set->dim);
2002 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2007 fprintf(out, "null set\n");
2011 fprintf(out, "%*s", indent, "");
2012 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2013 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2015 for (i = 0; i < set->n; ++i) {
2016 fprintf(out, "%*s", indent, "");
2017 fprintf(out, "basic set %d:\n", i);
2018 isl_basic_set_print_internal(set->p[i], out, indent+4);
2022 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2027 fprintf(out, "null map\n");
2031 fprintf(out, "%*s", indent, "");
2032 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2033 "flags: %x, n_name: %d\n",
2034 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2035 map->dim->n_out, map->flags, map->dim->n_name);
2036 for (i = 0; i < map->n; ++i) {
2037 fprintf(out, "%*s", indent, "");
2038 fprintf(out, "basic map %d:\n", i);
2039 isl_basic_map_print_internal(map->p[i], out, indent+4);
2043 struct isl_basic_map *isl_basic_map_intersect_domain(
2044 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2046 struct isl_basic_map *bmap_domain;
2051 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2052 bset->dim, isl_dim_param), goto error);
2054 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2055 isl_assert(bset->ctx,
2056 isl_basic_map_compatible_domain(bmap, bset), goto error);
2058 bmap = isl_basic_map_cow(bmap);
2061 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2062 bset->n_div, bset->n_eq, bset->n_ineq);
2063 bmap_domain = isl_basic_map_from_domain(bset);
2064 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2066 bmap = isl_basic_map_simplify(bmap);
2067 return isl_basic_map_finalize(bmap);
2069 isl_basic_map_free(bmap);
2070 isl_basic_set_free(bset);
2074 struct isl_basic_map *isl_basic_map_intersect_range(
2075 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2077 struct isl_basic_map *bmap_range;
2082 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2083 bset->dim, isl_dim_param), goto error);
2085 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2086 isl_assert(bset->ctx,
2087 isl_basic_map_compatible_range(bmap, bset), goto error);
2089 bmap = isl_basic_map_cow(bmap);
2092 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2093 bset->n_div, bset->n_eq, bset->n_ineq);
2094 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2095 bmap = add_constraints(bmap, bmap_range, 0, 0);
2097 bmap = isl_basic_map_simplify(bmap);
2098 return isl_basic_map_finalize(bmap);
2100 isl_basic_map_free(bmap);
2101 isl_basic_set_free(bset);
2105 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2111 total = 1 + isl_basic_map_total_dim(bmap);
2112 if (total != vec->size)
2117 for (i = 0; i < bmap->n_eq; ++i) {
2118 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2119 if (!isl_int_is_zero(s)) {
2125 for (i = 0; i < bmap->n_ineq; ++i) {
2126 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2127 if (isl_int_is_neg(s)) {
2138 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2140 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2143 struct isl_basic_map *isl_basic_map_intersect(
2144 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2146 struct isl_vec *sample = NULL;
2148 if (!bmap1 || !bmap2)
2151 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2152 bmap2->dim, isl_dim_param), goto error);
2153 if (isl_dim_total(bmap1->dim) ==
2154 isl_dim_size(bmap1->dim, isl_dim_param) &&
2155 isl_dim_total(bmap2->dim) !=
2156 isl_dim_size(bmap2->dim, isl_dim_param))
2157 return isl_basic_map_intersect(bmap2, bmap1);
2159 if (isl_dim_total(bmap2->dim) !=
2160 isl_dim_size(bmap2->dim, isl_dim_param))
2161 isl_assert(bmap1->ctx,
2162 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2164 if (bmap1->sample &&
2165 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2166 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2167 sample = isl_vec_copy(bmap1->sample);
2168 else if (bmap2->sample &&
2169 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2170 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2171 sample = isl_vec_copy(bmap2->sample);
2173 bmap1 = isl_basic_map_cow(bmap1);
2176 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2177 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2178 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2181 isl_vec_free(sample);
2183 isl_vec_free(bmap1->sample);
2184 bmap1->sample = sample;
2187 bmap1 = isl_basic_map_simplify(bmap1);
2188 return isl_basic_map_finalize(bmap1);
2191 isl_vec_free(sample);
2192 isl_basic_map_free(bmap1);
2193 isl_basic_map_free(bmap2);
2197 struct isl_basic_set *isl_basic_set_intersect(
2198 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2200 return (struct isl_basic_set *)
2201 isl_basic_map_intersect(
2202 (struct isl_basic_map *)bset1,
2203 (struct isl_basic_map *)bset2);
2206 /* Special case of isl_map_intersect, where both map1 and map2
2207 * are convex, without any divs and such that either map1 or map2
2208 * contains a single constraint. This constraint is then simply
2209 * added to the other map.
2211 static __isl_give isl_map *map_intersect_add_constraint(
2212 __isl_take isl_map *map1, __isl_take isl_map *map2)
2214 struct isl_basic_map *bmap1;
2215 struct isl_basic_map *bmap2;
2217 isl_assert(map1->ctx, map1->n == 1, goto error);
2218 isl_assert(map2->ctx, map1->n == 1, goto error);
2219 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2220 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2222 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2223 return isl_map_intersect(map2, map1);
2225 isl_assert(map2->ctx,
2226 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2228 map1 = isl_map_cow(map1);
2231 if (isl_map_fast_is_empty(map1)) {
2235 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2236 if (map2->p[0]->n_eq == 1)
2237 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2239 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2240 map2->p[0]->ineq[0]);
2242 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2243 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2247 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2248 isl_basic_map_free(map1->p[0]);
2261 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2264 struct isl_map *result;
2270 if (isl_map_fast_is_empty(map1)) {
2274 if (isl_map_fast_is_empty(map2)) {
2279 if (map1->n == 1 && map2->n == 1 &&
2280 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2281 isl_dim_equal(map1->dim, map2->dim) &&
2282 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2283 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2284 return map_intersect_add_constraint(map1, map2);
2285 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2286 map2->dim, isl_dim_param), goto error);
2287 if (isl_dim_total(map1->dim) ==
2288 isl_dim_size(map1->dim, isl_dim_param) &&
2289 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2290 return isl_map_intersect(map2, map1);
2292 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2293 isl_assert(map1->ctx,
2294 isl_dim_equal(map1->dim, map2->dim), goto error);
2296 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2297 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2298 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2300 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2301 map1->n * map2->n, flags);
2304 for (i = 0; i < map1->n; ++i)
2305 for (j = 0; j < map2->n; ++j) {
2306 struct isl_basic_map *part;
2307 part = isl_basic_map_intersect(
2308 isl_basic_map_copy(map1->p[i]),
2309 isl_basic_map_copy(map2->p[j]));
2310 if (isl_basic_map_is_empty(part))
2311 isl_basic_map_free(part);
2313 result = isl_map_add_basic_map(result, part);
2326 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2328 return (struct isl_set *)
2329 isl_map_intersect((struct isl_map *)set1,
2330 (struct isl_map *)set2);
2333 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2335 struct isl_dim *dim;
2336 struct isl_basic_set *bset;
2341 bmap = isl_basic_map_cow(bmap);
2344 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2345 in = isl_basic_map_n_in(bmap);
2346 bset = isl_basic_set_from_basic_map(bmap);
2347 bset = isl_basic_set_swap_vars(bset, in);
2348 return isl_basic_map_from_basic_set(bset, dim);
2351 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2352 enum isl_dim_type type, unsigned pos, unsigned n)
2354 struct isl_dim *res_dim;
2355 struct isl_basic_map *res;
2356 struct isl_dim_map *dim_map;
2357 unsigned total, off;
2358 enum isl_dim_type t;
2366 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2368 total = isl_basic_map_total_dim(bmap) + n;
2369 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2371 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2373 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2375 unsigned size = isl_basic_map_dim(bmap, t);
2376 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2378 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2379 pos, size - pos, off + pos + n);
2381 off += isl_dim_size(res_dim, t);
2383 isl_dim_map_div(dim_map, bmap, off);
2385 res = isl_basic_map_alloc_dim(res_dim,
2386 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2387 if (isl_basic_map_is_rational(bmap))
2388 res = isl_basic_map_set_rational(res);
2389 res = add_constraints_dim_map(res, bmap, dim_map);
2390 return isl_basic_map_finalize(res);
2393 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2394 enum isl_dim_type type, unsigned n)
2398 return isl_basic_map_insert(bmap, type,
2399 isl_basic_map_dim(bmap, type), n);
2402 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2403 enum isl_dim_type type, unsigned n)
2407 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2408 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2410 isl_basic_set_free(bset);
2414 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2415 enum isl_dim_type type, unsigned pos, unsigned n)
2422 map = isl_map_cow(map);
2426 map->dim = isl_dim_insert(map->dim, type, pos, n);
2430 for (i = 0; i < map->n; ++i) {
2431 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2442 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2443 enum isl_dim_type type, unsigned pos, unsigned n)
2445 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2448 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2449 enum isl_dim_type type, unsigned n)
2453 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2456 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2457 enum isl_dim_type type, unsigned n)
2461 isl_assert(set->ctx, type != isl_dim_in, goto error);
2462 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2468 __isl_give isl_basic_map *isl_basic_map_move_dims(
2469 __isl_take isl_basic_map *bmap,
2470 enum isl_dim_type dst_type, unsigned dst_pos,
2471 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2474 struct isl_dim_map *dim_map;
2475 struct isl_basic_map *res;
2476 enum isl_dim_type t;
2477 unsigned total, off;
2484 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2487 if (dst_type == src_type && dst_pos == src_pos)
2490 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2492 if (pos(bmap->dim, dst_type) + dst_pos ==
2493 pos(bmap->dim, src_type) + src_pos +
2494 ((src_type < dst_type) ? n : 0)) {
2495 bmap = isl_basic_map_cow(bmap);
2499 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2500 src_type, src_pos, n);
2504 bmap = isl_basic_map_finalize(bmap);
2509 total = isl_basic_map_total_dim(bmap);
2510 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2513 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2514 unsigned size = isl_dim_size(bmap->dim, t);
2515 if (t == dst_type) {
2516 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2519 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2522 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2523 dst_pos, size - dst_pos, off);
2524 off += size - dst_pos;
2525 } else if (t == src_type) {
2526 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2529 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2530 src_pos + n, size - src_pos - n, off);
2531 off += size - src_pos - n;
2533 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2537 isl_dim_map_div(dim_map, bmap, off);
2539 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2540 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2541 bmap = add_constraints_dim_map(res, bmap, dim_map);
2543 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2544 src_type, src_pos, n);
2548 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2549 bmap = isl_basic_map_gauss(bmap, NULL);
2550 bmap = isl_basic_map_finalize(bmap);
2554 isl_basic_map_free(bmap);
2558 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2559 enum isl_dim_type dst_type, unsigned dst_pos,
2560 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2562 return (isl_basic_set *)isl_basic_map_move_dims(
2563 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2566 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2567 enum isl_dim_type dst_type, unsigned dst_pos,
2568 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2572 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2573 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2574 src_type, src_pos, n);
2580 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2581 enum isl_dim_type dst_type, unsigned dst_pos,
2582 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2591 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2594 if (dst_type == src_type && dst_pos == src_pos)
2597 isl_assert(map->ctx, dst_type != src_type, goto error);
2599 map = isl_map_cow(map);
2603 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2607 for (i = 0; i < map->n; ++i) {
2608 map->p[i] = isl_basic_map_move_dims(map->p[i],
2610 src_type, src_pos, n);
2621 /* Move the specified dimensions to the last columns right before
2622 * the divs. Don't change the dimension specification of bmap.
2623 * That's the responsibility of the caller.
2625 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2626 enum isl_dim_type type, unsigned first, unsigned n)
2628 struct isl_dim_map *dim_map;
2629 struct isl_basic_map *res;
2630 enum isl_dim_type t;
2631 unsigned total, off;
2635 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2638 total = isl_basic_map_total_dim(bmap);
2639 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2642 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2643 unsigned size = isl_dim_size(bmap->dim, t);
2645 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2648 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2649 first, n, total - bmap->n_div - n);
2650 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2651 first + n, size - (first + n), off);
2652 off += size - (first + n);
2654 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2658 isl_dim_map_div(dim_map, bmap, off + n);
2660 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2661 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2662 res = add_constraints_dim_map(res, bmap, dim_map);
2666 /* Turn the n dimensions of type type, starting at first
2667 * into existentially quantified variables.
2669 __isl_give isl_basic_map *isl_basic_map_project_out(
2670 __isl_take isl_basic_map *bmap,
2671 enum isl_dim_type type, unsigned first, unsigned n)
2684 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2685 return isl_basic_map_remove_dims(bmap, type, first, n);
2687 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2690 bmap = move_last(bmap, type, first, n);
2691 bmap = isl_basic_map_cow(bmap);
2695 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2696 old = bmap->block2.data;
2697 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2698 (bmap->extra + n) * (1 + row_size));
2699 if (!bmap->block2.data)
2701 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2704 for (i = 0; i < n; ++i) {
2705 new_div[i] = bmap->block2.data +
2706 (bmap->extra + i) * (1 + row_size);
2707 isl_seq_clr(new_div[i], 1 + row_size);
2709 for (i = 0; i < bmap->extra; ++i)
2710 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2712 bmap->div = new_div;
2716 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2719 bmap = isl_basic_map_simplify(bmap);
2720 bmap = isl_basic_map_drop_redundant_divs(bmap);
2721 return isl_basic_map_finalize(bmap);
2723 isl_basic_map_free(bmap);
2727 /* Turn the n dimensions of type type, starting at first
2728 * into existentially quantified variables.
2730 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2731 enum isl_dim_type type, unsigned first, unsigned n)
2733 return (isl_basic_set *)isl_basic_map_project_out(
2734 (isl_basic_map *)bset, type, first, n);
2737 /* Turn the n dimensions of type type, starting at first
2738 * into existentially quantified variables.
2740 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2741 enum isl_dim_type type, unsigned first, unsigned n)
2751 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2753 map = isl_map_cow(map);
2757 map->dim = isl_dim_drop(map->dim, type, first, n);
2761 for (i = 0; i < map->n; ++i) {
2762 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2773 /* Turn the n dimensions of type type, starting at first
2774 * into existentially quantified variables.
2776 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2777 enum isl_dim_type type, unsigned first, unsigned n)
2779 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2782 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2786 for (i = 0; i < n; ++i) {
2787 j = isl_basic_map_alloc_div(bmap);
2790 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2794 isl_basic_map_free(bmap);
2798 struct isl_basic_map *isl_basic_map_apply_range(
2799 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2801 struct isl_dim *dim_result = NULL;
2802 struct isl_basic_map *bmap;
2803 unsigned n_in, n_out, n, nparam, total, pos;
2804 struct isl_dim_map *dim_map1, *dim_map2;
2806 if (!bmap1 || !bmap2)
2809 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2810 isl_dim_copy(bmap2->dim));
2812 n_in = isl_basic_map_n_in(bmap1);
2813 n_out = isl_basic_map_n_out(bmap2);
2814 n = isl_basic_map_n_out(bmap1);
2815 nparam = isl_basic_map_n_param(bmap1);
2817 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2818 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2819 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2820 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2821 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2822 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2823 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2824 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2825 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2826 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2827 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2829 bmap = isl_basic_map_alloc_dim(dim_result,
2830 bmap1->n_div + bmap2->n_div + n,
2831 bmap1->n_eq + bmap2->n_eq,
2832 bmap1->n_ineq + bmap2->n_ineq);
2833 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2834 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2835 bmap = add_divs(bmap, n);
2836 bmap = isl_basic_map_simplify(bmap);
2837 bmap = isl_basic_map_drop_redundant_divs(bmap);
2838 return isl_basic_map_finalize(bmap);
2840 isl_basic_map_free(bmap1);
2841 isl_basic_map_free(bmap2);
2845 struct isl_basic_set *isl_basic_set_apply(
2846 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2851 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2854 return (struct isl_basic_set *)
2855 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2857 isl_basic_set_free(bset);
2858 isl_basic_map_free(bmap);
2862 struct isl_basic_map *isl_basic_map_apply_domain(
2863 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2865 if (!bmap1 || !bmap2)
2868 isl_assert(bmap1->ctx,
2869 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2870 isl_assert(bmap1->ctx,
2871 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2874 bmap1 = isl_basic_map_reverse(bmap1);
2875 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2876 return isl_basic_map_reverse(bmap1);
2878 isl_basic_map_free(bmap1);
2879 isl_basic_map_free(bmap2);
2883 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2884 * A \cap B -> f(A) + f(B)
2886 struct isl_basic_map *isl_basic_map_sum(
2887 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2889 unsigned n_in, n_out, nparam, total, pos;
2890 struct isl_basic_map *bmap = NULL;
2891 struct isl_dim_map *dim_map1, *dim_map2;
2894 if (!bmap1 || !bmap2)
2897 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2900 nparam = isl_basic_map_n_param(bmap1);
2901 n_in = isl_basic_map_n_in(bmap1);
2902 n_out = isl_basic_map_n_out(bmap1);
2904 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2905 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2906 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2907 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2908 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2909 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2910 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2911 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2912 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2913 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2914 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2916 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2917 bmap1->n_div + bmap2->n_div + 2 * n_out,
2918 bmap1->n_eq + bmap2->n_eq + n_out,
2919 bmap1->n_ineq + bmap2->n_ineq);
2920 for (i = 0; i < n_out; ++i) {
2921 int j = isl_basic_map_alloc_equality(bmap);
2924 isl_seq_clr(bmap->eq[j], 1+total);
2925 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2926 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2927 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2929 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2930 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2931 bmap = add_divs(bmap, 2 * n_out);
2933 bmap = isl_basic_map_simplify(bmap);
2934 return isl_basic_map_finalize(bmap);
2936 isl_basic_map_free(bmap);
2937 isl_basic_map_free(bmap1);
2938 isl_basic_map_free(bmap2);
2942 /* Given two maps A -> f(A) and B -> g(B), construct a map
2943 * A \cap B -> f(A) + f(B)
2945 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2947 struct isl_map *result;
2953 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2955 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2956 map1->n * map2->n, 0);
2959 for (i = 0; i < map1->n; ++i)
2960 for (j = 0; j < map2->n; ++j) {
2961 struct isl_basic_map *part;
2962 part = isl_basic_map_sum(
2963 isl_basic_map_copy(map1->p[i]),
2964 isl_basic_map_copy(map2->p[j]));
2965 if (isl_basic_map_is_empty(part))
2966 isl_basic_map_free(part);
2968 result = isl_map_add_basic_map(result, part);
2981 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2982 __isl_take isl_set *set2)
2984 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2987 /* Given a basic map A -> f(A), construct A -> -f(A).
2989 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2994 bmap = isl_basic_map_cow(bmap);
2998 n = isl_basic_map_dim(bmap, isl_dim_out);
2999 off = isl_basic_map_offset(bmap, isl_dim_out);
3000 for (i = 0; i < bmap->n_eq; ++i)
3001 for (j = 0; j < n; ++j)
3002 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3003 for (i = 0; i < bmap->n_ineq; ++i)
3004 for (j = 0; j < n; ++j)
3005 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3006 for (i = 0; i < bmap->n_div; ++i)
3007 for (j = 0; j < n; ++j)
3008 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3009 return isl_basic_map_finalize(bmap);
3012 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3014 return isl_basic_map_neg(bset);
3017 /* Given a map A -> f(A), construct A -> -f(A).
3019 struct isl_map *isl_map_neg(struct isl_map *map)
3023 map = isl_map_cow(map);
3027 for (i = 0; i < map->n; ++i) {
3028 map->p[i] = isl_basic_map_neg(map->p[i]);
3039 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3041 return (isl_set *)isl_map_neg((isl_map *)set);
3044 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3045 * A -> floor(f(A)/d).
3047 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3050 unsigned n_in, n_out, nparam, total, pos;
3051 struct isl_basic_map *result = NULL;
3052 struct isl_dim_map *dim_map;
3058 nparam = isl_basic_map_n_param(bmap);
3059 n_in = isl_basic_map_n_in(bmap);
3060 n_out = isl_basic_map_n_out(bmap);
3062 total = nparam + n_in + n_out + bmap->n_div + n_out;
3063 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3064 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3065 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3066 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3067 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3069 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3070 bmap->n_div + n_out,
3071 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3072 result = add_constraints_dim_map(result, bmap, dim_map);
3073 result = add_divs(result, n_out);
3074 for (i = 0; i < n_out; ++i) {
3076 j = isl_basic_map_alloc_inequality(result);
3079 isl_seq_clr(result->ineq[j], 1+total);
3080 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3081 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3082 j = isl_basic_map_alloc_inequality(result);
3085 isl_seq_clr(result->ineq[j], 1+total);
3086 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3087 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3088 isl_int_sub_ui(result->ineq[j][0], d, 1);
3091 result = isl_basic_map_simplify(result);
3092 return isl_basic_map_finalize(result);
3094 isl_basic_map_free(result);
3098 /* Given a map A -> f(A) and an integer d, construct a map
3099 * A -> floor(f(A)/d).
3101 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3105 map = isl_map_cow(map);
3109 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3110 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3111 for (i = 0; i < map->n; ++i) {
3112 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3123 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3129 i = isl_basic_map_alloc_equality(bmap);
3132 nparam = isl_basic_map_n_param(bmap);
3133 n_in = isl_basic_map_n_in(bmap);
3134 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3135 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3136 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3137 return isl_basic_map_finalize(bmap);
3139 isl_basic_map_free(bmap);
3143 /* Add a constraints to "bmap" expressing i_pos < o_pos
3145 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3151 i = isl_basic_map_alloc_inequality(bmap);
3154 nparam = isl_basic_map_n_param(bmap);
3155 n_in = isl_basic_map_n_in(bmap);
3156 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3157 isl_int_set_si(bmap->ineq[i][0], -1);
3158 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3159 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3160 return isl_basic_map_finalize(bmap);
3162 isl_basic_map_free(bmap);
3166 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3168 static __isl_give isl_basic_map *var_less_or_equal(
3169 __isl_take isl_basic_map *bmap, unsigned pos)
3175 i = isl_basic_map_alloc_inequality(bmap);
3178 nparam = isl_basic_map_n_param(bmap);
3179 n_in = isl_basic_map_n_in(bmap);
3180 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3181 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3182 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3183 return isl_basic_map_finalize(bmap);
3185 isl_basic_map_free(bmap);
3189 /* Add a constraints to "bmap" expressing i_pos > o_pos
3191 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3197 i = isl_basic_map_alloc_inequality(bmap);
3200 nparam = isl_basic_map_n_param(bmap);
3201 n_in = isl_basic_map_n_in(bmap);
3202 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3203 isl_int_set_si(bmap->ineq[i][0], -1);
3204 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3205 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3206 return isl_basic_map_finalize(bmap);
3208 isl_basic_map_free(bmap);
3212 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3214 static __isl_give isl_basic_map *var_more_or_equal(
3215 __isl_take isl_basic_map *bmap, unsigned pos)
3221 i = isl_basic_map_alloc_inequality(bmap);
3224 nparam = isl_basic_map_n_param(bmap);
3225 n_in = isl_basic_map_n_in(bmap);
3226 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3227 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3228 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3229 return isl_basic_map_finalize(bmap);
3231 isl_basic_map_free(bmap);
3235 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3238 struct isl_basic_map *bmap;
3239 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3242 for (i = 0; i < n_equal && bmap; ++i)
3243 bmap = var_equal(bmap, i);
3244 return isl_basic_map_finalize(bmap);
3247 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3249 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3252 struct isl_basic_map *bmap;
3253 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3256 for (i = 0; i < pos && bmap; ++i)
3257 bmap = var_equal(bmap, i);
3259 bmap = var_less(bmap, pos);
3260 return isl_basic_map_finalize(bmap);
3263 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3265 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3266 __isl_take isl_dim *dim, unsigned pos)
3269 isl_basic_map *bmap;
3271 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3272 for (i = 0; i < pos; ++i)
3273 bmap = var_equal(bmap, i);
3274 bmap = var_less_or_equal(bmap, pos);
3275 return isl_basic_map_finalize(bmap);
3278 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3280 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3283 struct isl_basic_map *bmap;
3284 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3287 for (i = 0; i < pos && bmap; ++i)
3288 bmap = var_equal(bmap, i);
3290 bmap = var_more(bmap, pos);
3291 return isl_basic_map_finalize(bmap);
3294 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3296 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3297 __isl_take isl_dim *dim, unsigned pos)
3300 isl_basic_map *bmap;
3302 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3303 for (i = 0; i < pos; ++i)
3304 bmap = var_equal(bmap, i);
3305 bmap = var_more_or_equal(bmap, pos);
3306 return isl_basic_map_finalize(bmap);
3309 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3310 unsigned n, int equal)
3312 struct isl_map *map;
3315 if (n == 0 && equal)
3316 return isl_map_universe(dims);
3318 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3320 for (i = 0; i + 1 < n; ++i)
3321 map = isl_map_add_basic_map(map,
3322 isl_basic_map_less_at(isl_dim_copy(dims), i));
3325 map = isl_map_add_basic_map(map,
3326 isl_basic_map_less_or_equal_at(dims, n - 1));
3328 map = isl_map_add_basic_map(map,
3329 isl_basic_map_less_at(dims, n - 1));
3336 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3340 return map_lex_lte_first(dims, dims->n_out, equal);
3343 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3345 return map_lex_lte_first(dim, n, 0);
3348 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3350 return map_lex_lte_first(dim, n, 1);
3353 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3355 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3358 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3360 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3363 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3364 unsigned n, int equal)
3366 struct isl_map *map;
3369 if (n == 0 && equal)
3370 return isl_map_universe(dims);
3372 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3374 for (i = 0; i + 1 < n; ++i)
3375 map = isl_map_add_basic_map(map,
3376 isl_basic_map_more_at(isl_dim_copy(dims), i));
3379 map = isl_map_add_basic_map(map,
3380 isl_basic_map_more_or_equal_at(dims, n - 1));
3382 map = isl_map_add_basic_map(map,
3383 isl_basic_map_more_at(dims, n - 1));
3390 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3394 return map_lex_gte_first(dims, dims->n_out, equal);
3397 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3399 return map_lex_gte_first(dim, n, 0);
3402 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3404 return map_lex_gte_first(dim, n, 1);
3407 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3409 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3412 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3414 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3417 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3418 __isl_take isl_set *set2)
3421 map = isl_map_lex_le(isl_set_get_dim(set1));
3422 map = isl_map_intersect_domain(map, set1);
3423 map = isl_map_intersect_range(map, set2);
3427 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3428 __isl_take isl_set *set2)
3431 map = isl_map_lex_lt(isl_set_get_dim(set1));
3432 map = isl_map_intersect_domain(map, set1);
3433 map = isl_map_intersect_range(map, set2);
3437 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3438 __isl_take isl_set *set2)
3441 map = isl_map_lex_ge(isl_set_get_dim(set1));
3442 map = isl_map_intersect_domain(map, set1);
3443 map = isl_map_intersect_range(map, set2);
3447 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3448 __isl_take isl_set *set2)
3451 map = isl_map_lex_gt(isl_set_get_dim(set1));
3452 map = isl_map_intersect_domain(map, set1);
3453 map = isl_map_intersect_range(map, set2);
3457 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3458 __isl_take isl_map *map2)
3461 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3462 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3463 map = isl_map_apply_range(map, isl_map_reverse(map2));
3467 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3468 __isl_take isl_map *map2)
3471 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3472 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3473 map = isl_map_apply_range(map, isl_map_reverse(map2));
3477 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3478 __isl_take isl_map *map2)
3481 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3482 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3483 map = isl_map_apply_range(map, isl_map_reverse(map2));
3487 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3488 __isl_take isl_map *map2)
3491 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3492 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3493 map = isl_map_apply_range(map, isl_map_reverse(map2));
3497 struct isl_basic_map *isl_basic_map_from_basic_set(
3498 struct isl_basic_set *bset, struct isl_dim *dim)
3500 struct isl_basic_map *bmap;
3502 bset = isl_basic_set_cow(bset);
3506 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3507 isl_dim_free(bset->dim);
3508 bmap = (struct isl_basic_map *) bset;
3510 return isl_basic_map_finalize(bmap);
3512 isl_basic_set_free(bset);
3517 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3521 if (bmap->dim->n_in == 0)
3522 return (struct isl_basic_set *)bmap;
3523 bmap = isl_basic_map_cow(bmap);
3526 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3529 bmap = isl_basic_map_finalize(bmap);
3530 return (struct isl_basic_set *)bmap;
3532 isl_basic_map_free(bmap);
3536 /* For a div d = floor(f/m), add the constraints
3539 * -(f-(n-1)) + m d >= 0
3541 * Note that the second constraint is the negation of
3545 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3546 unsigned pos, isl_int *div)
3549 unsigned total = isl_basic_map_total_dim(bmap);
3551 i = isl_basic_map_alloc_inequality(bmap);
3554 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3555 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3557 j = isl_basic_map_alloc_inequality(bmap);
3560 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3561 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3562 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3566 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3567 unsigned pos, isl_int *div)
3569 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3573 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3575 unsigned total = isl_basic_map_total_dim(bmap);
3576 unsigned div_pos = total - bmap->n_div + div;
3578 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3582 struct isl_basic_set *isl_basic_map_underlying_set(
3583 struct isl_basic_map *bmap)
3587 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3589 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3590 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3591 return (struct isl_basic_set *)bmap;
3592 bmap = isl_basic_map_cow(bmap);
3595 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3598 bmap->extra -= bmap->n_div;
3600 bmap = isl_basic_map_finalize(bmap);
3601 return (struct isl_basic_set *)bmap;
3606 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3607 __isl_take isl_basic_set *bset)
3609 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3612 struct isl_basic_map *isl_basic_map_overlying_set(
3613 struct isl_basic_set *bset, struct isl_basic_map *like)
3615 struct isl_basic_map *bmap;
3616 struct isl_ctx *ctx;
3623 isl_assert(ctx, bset->n_div == 0, goto error);
3624 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3625 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3627 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3628 isl_basic_map_free(like);
3629 return (struct isl_basic_map *)bset;
3631 bset = isl_basic_set_cow(bset);
3634 total = bset->dim->n_out + bset->extra;
3635 bmap = (struct isl_basic_map *)bset;
3636 isl_dim_free(bmap->dim);
3637 bmap->dim = isl_dim_copy(like->dim);
3640 bmap->n_div = like->n_div;
3641 bmap->extra += like->n_div;
3645 ltotal = total - bmap->extra + like->extra;
3648 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3649 bmap->extra * (1 + 1 + total));
3650 if (isl_blk_is_error(bmap->block2))
3652 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3656 for (i = 0; i < bmap->extra; ++i)
3657 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3658 for (i = 0; i < like->n_div; ++i) {
3659 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3660 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3662 bmap = isl_basic_map_extend_constraints(bmap,
3663 0, 2 * like->n_div);
3664 for (i = 0; i < like->n_div; ++i) {
3665 if (isl_int_is_zero(bmap->div[i][0]))
3667 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3671 isl_basic_map_free(like);
3672 bmap = isl_basic_map_simplify(bmap);
3673 bmap = isl_basic_map_finalize(bmap);
3676 isl_basic_map_free(like);
3677 isl_basic_set_free(bset);
3681 struct isl_basic_set *isl_basic_set_from_underlying_set(
3682 struct isl_basic_set *bset, struct isl_basic_set *like)
3684 return (struct isl_basic_set *)
3685 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3688 struct isl_set *isl_set_from_underlying_set(
3689 struct isl_set *set, struct isl_basic_set *like)
3695 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3697 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3698 isl_basic_set_free(like);
3701 set = isl_set_cow(set);
3704 for (i = 0; i < set->n; ++i) {
3705 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3706 isl_basic_set_copy(like));
3710 isl_dim_free(set->dim);
3711 set->dim = isl_dim_copy(like->dim);
3714 isl_basic_set_free(like);
3717 isl_basic_set_free(like);
3722 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3726 map = isl_map_cow(map);
3729 map->dim = isl_dim_cow(map->dim);
3733 for (i = 1; i < map->n; ++i)
3734 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3736 for (i = 0; i < map->n; ++i) {
3737 map->p[i] = (struct isl_basic_map *)
3738 isl_basic_map_underlying_set(map->p[i]);
3743 map->dim = isl_dim_underlying(map->dim, 0);
3745 isl_dim_free(map->dim);
3746 map->dim = isl_dim_copy(map->p[0]->dim);
3750 return (struct isl_set *)map;
3756 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3758 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3761 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3762 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3764 bmap = isl_basic_map_cow(bmap);
3768 isl_dim_free(bmap->dim);
3771 bmap = isl_basic_map_finalize(bmap);
3775 isl_basic_map_free(bmap);
3780 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3781 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3783 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3787 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3788 __isl_take isl_dim *dim)
3792 map = isl_map_cow(map);
3796 for (i = 0; i < map->n; ++i) {
3797 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3802 isl_dim_free(map->dim);
3812 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3813 __isl_take isl_dim *dim)
3815 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3818 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3821 struct isl_basic_set *domain;
3827 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3829 n_in = isl_basic_map_n_in(bmap);
3830 n_out = isl_basic_map_n_out(bmap);
3831 domain = isl_basic_set_from_basic_map(bmap);
3832 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3834 domain = isl_basic_set_reset_dim(domain, dim);
3839 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3841 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3844 __isl_give isl_basic_map *isl_basic_map_domain_map(
3845 __isl_take isl_basic_map *bmap)
3849 isl_basic_map *domain;
3850 isl_basic_set *bset;
3851 int nparam, n_in, n_out;
3854 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3855 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3856 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3858 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3859 domain = isl_basic_map_universe(dim);
3861 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3862 bmap = isl_basic_map_apply_range(bmap, domain);
3863 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3865 total = isl_basic_map_total_dim(bmap);
3867 for (i = 0; i < n_in; ++i) {
3868 k = isl_basic_map_alloc_equality(bmap);
3871 isl_seq_clr(bmap->eq[k], 1 + total);
3872 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3873 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3876 bmap = isl_basic_map_gauss(bmap, NULL);
3877 return isl_basic_map_finalize(bmap);
3879 isl_basic_map_free(bmap);
3883 __isl_give isl_basic_map *isl_basic_map_range_map(
3884 __isl_take isl_basic_map *bmap)
3888 isl_basic_map *range;
3889 isl_basic_set *bset;
3890 int nparam, n_in, n_out;
3893 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3894 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3895 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3897 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3898 range = isl_basic_map_universe(dim);
3900 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3901 bmap = isl_basic_map_apply_range(bmap, range);
3902 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3904 total = isl_basic_map_total_dim(bmap);
3906 for (i = 0; i < n_out; ++i) {
3907 k = isl_basic_map_alloc_equality(bmap);
3910 isl_seq_clr(bmap->eq[k], 1 + total);
3911 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3912 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3915 bmap = isl_basic_map_gauss(bmap, NULL);
3916 return isl_basic_map_finalize(bmap);
3918 isl_basic_map_free(bmap);
3922 struct isl_set *isl_map_range(struct isl_map *map)
3925 struct isl_set *set;
3929 if (isl_map_dim(map, isl_dim_in) == 0)
3930 return (isl_set *)map;
3932 map = isl_map_cow(map);
3936 set = (struct isl_set *) map;
3937 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3940 for (i = 0; i < map->n; ++i) {
3941 set->p[i] = isl_basic_map_range(map->p[i]);
3945 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3946 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3953 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3956 isl_dim *domain_dim;
3958 map = isl_map_cow(map);
3962 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3963 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3964 map->dim = isl_dim_join(map->dim, domain_dim);
3967 for (i = 0; i < map->n; ++i) {
3968 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3972 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3973 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3980 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3985 map = isl_map_cow(map);
3989 range_dim = isl_dim_range(isl_map_get_dim(map));
3990 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3991 map->dim = isl_dim_join(map->dim, range_dim);
3994 for (i = 0; i < map->n; ++i) {
3995 map->p[i] = isl_basic_map_range_map(map->p[i]);
3999 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4000 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4007 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4010 struct isl_map *map = NULL;
4012 set = isl_set_cow(set);
4015 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4016 map = (struct isl_map *)set;
4017 for (i = 0; i < set->n; ++i) {
4018 map->p[i] = isl_basic_map_from_basic_set(
4019 set->p[i], isl_dim_copy(dim));
4023 isl_dim_free(map->dim);
4032 __isl_give isl_basic_map *isl_basic_map_from_domain(
4033 __isl_take isl_basic_set *bset)
4035 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4038 __isl_give isl_basic_map *isl_basic_map_from_range(
4039 __isl_take isl_basic_set *bset)
4041 return (isl_basic_map *)bset;
4044 struct isl_map *isl_map_from_range(struct isl_set *set)
4046 return (struct isl_map *)set;
4049 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4051 return isl_map_reverse(isl_map_from_range(set));
4054 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4055 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4057 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4058 isl_basic_map_from_range(range));
4061 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4062 __isl_take isl_set *range)
4064 return isl_map_apply_range(isl_map_from_domain(domain),
4065 isl_map_from_range(range));
4068 struct isl_set *isl_set_from_map(struct isl_map *map)
4071 struct isl_set *set = NULL;
4075 map = isl_map_cow(map);
4078 map->dim = isl_dim_as_set_dim(map->dim);
4081 set = (struct isl_set *)map;
4082 for (i = 0; i < map->n; ++i) {
4083 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4093 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4095 struct isl_map *map;
4099 isl_assert(dim->ctx, n >= 0, return NULL);
4100 map = isl_alloc(dim->ctx, struct isl_map,
4101 sizeof(struct isl_map) +
4102 (n - 1) * sizeof(struct isl_basic_map *));
4106 map->ctx = dim->ctx;
4107 isl_ctx_ref(map->ctx);
4119 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4120 unsigned nparam, unsigned in, unsigned out, int n,
4123 struct isl_map *map;
4124 struct isl_dim *dims;
4126 dims = isl_dim_alloc(ctx, nparam, in, out);
4130 map = isl_map_alloc_dim(dims, n, flags);
4134 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4136 struct isl_basic_map *bmap;
4137 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4138 bmap = isl_basic_map_set_to_empty(bmap);
4142 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4144 struct isl_basic_set *bset;
4145 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4146 bset = isl_basic_set_set_to_empty(bset);
4150 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4152 struct isl_basic_map *bmap;
4155 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4156 bmap = isl_basic_map_set_to_empty(bmap);
4160 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4162 struct isl_basic_map *bmap;
4165 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4166 bmap = isl_basic_map_set_to_empty(bmap);
4170 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4172 struct isl_basic_set *bset;
4175 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4176 bset = isl_basic_set_set_to_empty(bset);
4180 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4182 struct isl_basic_map *bmap;
4183 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4184 bmap = isl_basic_map_finalize(bmap);
4188 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4190 struct isl_basic_set *bset;
4191 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4192 bset = isl_basic_set_finalize(bset);
4196 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4199 unsigned total = isl_dim_total(dim);
4200 isl_basic_map *bmap;
4202 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4203 for (i = 0; i < total; ++i) {
4204 int k = isl_basic_map_alloc_inequality(bmap);
4207 isl_seq_clr(bmap->ineq[k], 1 + total);
4208 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4212 isl_basic_map_free(bmap);
4216 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4218 return isl_basic_map_nat_universe(dim);
4221 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4223 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4226 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4228 return isl_map_nat_universe(dim);
4231 __isl_give isl_basic_map *isl_basic_map_universe_like(
4232 __isl_keep isl_basic_map *model)
4236 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4239 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4243 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4246 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4247 __isl_keep isl_set *model)
4251 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4254 struct isl_map *isl_map_empty(struct isl_dim *dim)
4256 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4259 struct isl_map *isl_map_empty_like(struct isl_map *model)
4263 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4266 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4270 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4273 struct isl_set *isl_set_empty(struct isl_dim *dim)
4275 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4278 struct isl_set *isl_set_empty_like(struct isl_set *model)
4282 return isl_set_empty(isl_dim_copy(model->dim));
4285 struct isl_map *isl_map_universe(struct isl_dim *dim)
4287 struct isl_map *map;
4290 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4291 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4295 struct isl_set *isl_set_universe(struct isl_dim *dim)
4297 struct isl_set *set;
4300 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4301 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4305 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4309 return isl_set_universe(isl_dim_copy(model->dim));
4312 struct isl_map *isl_map_dup(struct isl_map *map)
4315 struct isl_map *dup;
4319 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4320 for (i = 0; i < map->n; ++i)
4321 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4325 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4326 __isl_take isl_basic_map *bmap)
4330 if (isl_basic_map_fast_is_empty(bmap)) {
4331 isl_basic_map_free(bmap);
4334 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4335 isl_assert(map->ctx, map->n < map->size, goto error);
4336 map->p[map->n] = bmap;
4338 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4344 isl_basic_map_free(bmap);
4348 void isl_map_free(struct isl_map *map)
4358 isl_ctx_deref(map->ctx);
4359 for (i = 0; i < map->n; ++i)
4360 isl_basic_map_free(map->p[i]);
4361 isl_dim_free(map->dim);
4365 struct isl_map *isl_map_extend(struct isl_map *base,
4366 unsigned nparam, unsigned n_in, unsigned n_out)
4370 base = isl_map_cow(base);
4374 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4377 for (i = 0; i < base->n; ++i) {
4378 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4379 isl_dim_copy(base->dim), 0, 0, 0);
4389 struct isl_set *isl_set_extend(struct isl_set *base,
4390 unsigned nparam, unsigned dim)
4392 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4396 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4397 struct isl_basic_map *bmap, unsigned pos, int value)
4401 bmap = isl_basic_map_cow(bmap);
4402 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4403 j = isl_basic_map_alloc_equality(bmap);
4406 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4407 isl_int_set_si(bmap->eq[j][pos], -1);
4408 isl_int_set_si(bmap->eq[j][0], value);
4409 bmap = isl_basic_map_simplify(bmap);
4410 return isl_basic_map_finalize(bmap);
4412 isl_basic_map_free(bmap);
4416 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4417 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4421 bmap = isl_basic_map_cow(bmap);
4422 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4423 j = isl_basic_map_alloc_equality(bmap);
4426 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4427 isl_int_set_si(bmap->eq[j][pos], -1);
4428 isl_int_set(bmap->eq[j][0], value);
4429 bmap = isl_basic_map_simplify(bmap);
4430 return isl_basic_map_finalize(bmap);
4432 isl_basic_map_free(bmap);
4436 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4437 enum isl_dim_type type, unsigned pos, int value)
4441 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4442 return isl_basic_map_fix_pos_si(bmap,
4443 isl_basic_map_offset(bmap, type) + pos, value);
4445 isl_basic_map_free(bmap);
4449 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4450 enum isl_dim_type type, unsigned pos, isl_int value)
4454 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4455 return isl_basic_map_fix_pos(bmap,
4456 isl_basic_map_offset(bmap, type) + pos, value);
4458 isl_basic_map_free(bmap);
4462 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4463 enum isl_dim_type type, unsigned pos, int value)
4465 return (struct isl_basic_set *)
4466 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4470 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4471 enum isl_dim_type type, unsigned pos, isl_int value)
4473 return (struct isl_basic_set *)
4474 isl_basic_map_fix((struct isl_basic_map *)bset,
4478 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4479 unsigned input, int value)
4481 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4484 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4485 unsigned dim, int value)
4487 return (struct isl_basic_set *)
4488 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4489 isl_dim_set, dim, value);
4492 struct isl_map *isl_map_fix_si(struct isl_map *map,
4493 enum isl_dim_type type, unsigned pos, int value)
4497 map = isl_map_cow(map);
4501 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4502 for (i = 0; i < map->n; ++i) {
4503 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4507 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4514 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4515 enum isl_dim_type type, unsigned pos, int value)
4517 return (struct isl_set *)
4518 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4521 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4522 enum isl_dim_type type, unsigned pos, isl_int value)
4526 map = isl_map_cow(map);
4530 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4531 for (i = 0; i < map->n; ++i) {
4532 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4536 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4543 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4544 enum isl_dim_type type, unsigned pos, isl_int value)
4546 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4549 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4550 unsigned input, int value)
4552 return isl_map_fix_si(map, isl_dim_in, input, value);
4555 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4557 return (struct isl_set *)
4558 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4561 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4562 __isl_take isl_basic_map *bmap,
4563 enum isl_dim_type type, unsigned pos, int value)
4569 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4570 pos += isl_basic_map_offset(bmap, type);
4571 bmap = isl_basic_map_cow(bmap);
4572 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4573 j = isl_basic_map_alloc_inequality(bmap);
4576 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4577 isl_int_set_si(bmap->ineq[j][pos], 1);
4578 isl_int_set_si(bmap->ineq[j][0], -value);
4579 bmap = isl_basic_map_simplify(bmap);
4580 return isl_basic_map_finalize(bmap);
4582 isl_basic_map_free(bmap);
4586 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4587 unsigned dim, isl_int value)
4591 bset = isl_basic_set_cow(bset);
4592 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4593 j = isl_basic_set_alloc_inequality(bset);
4596 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4597 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4598 isl_int_neg(bset->ineq[j][0], value);
4599 bset = isl_basic_set_simplify(bset);
4600 return isl_basic_set_finalize(bset);
4602 isl_basic_set_free(bset);
4606 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4607 enum isl_dim_type type, unsigned pos, int value)
4611 map = isl_map_cow(map);
4615 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4616 for (i = 0; i < map->n; ++i) {
4617 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4622 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4629 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4630 enum isl_dim_type type, unsigned pos, int value)
4632 return (struct isl_set *)
4633 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4636 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4641 set = isl_set_cow(set);
4645 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4646 for (i = 0; i < set->n; ++i) {
4647 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4657 struct isl_map *isl_map_reverse(struct isl_map *map)
4661 map = isl_map_cow(map);
4665 map->dim = isl_dim_reverse(map->dim);
4668 for (i = 0; i < map->n; ++i) {
4669 map->p[i] = isl_basic_map_reverse(map->p[i]);
4673 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4680 static struct isl_map *isl_basic_map_partial_lexopt(
4681 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4682 struct isl_set **empty, int max)
4686 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4687 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4689 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4691 isl_basic_map_free(bmap);
4692 isl_basic_set_free(dom);
4698 struct isl_map *isl_basic_map_partial_lexmax(
4699 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4700 struct isl_set **empty)
4702 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4705 struct isl_map *isl_basic_map_partial_lexmin(
4706 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4707 struct isl_set **empty)
4709 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4712 struct isl_set *isl_basic_set_partial_lexmin(
4713 struct isl_basic_set *bset, struct isl_basic_set *dom,
4714 struct isl_set **empty)
4716 return (struct isl_set *)
4717 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4721 struct isl_set *isl_basic_set_partial_lexmax(
4722 struct isl_basic_set *bset, struct isl_basic_set *dom,
4723 struct isl_set **empty)
4725 return (struct isl_set *)
4726 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4730 /* Given a basic map "bmap", compute the lexicographically minimal
4731 * (or maximal) image element for each domain element in dom.
4732 * Set *empty to those elements in dom that do not have an image element.
4734 * We first make sure the basic sets in dom are disjoint and then
4735 * simply collect the results over each of the basic sets separately.
4736 * We could probably improve the efficiency a bit by moving the union
4737 * domain down into the parametric integer programming.
4739 static __isl_give isl_map *basic_map_partial_lexopt(
4740 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4741 __isl_give isl_set **empty, int max)
4744 struct isl_map *res;
4746 dom = isl_set_make_disjoint(dom);
4750 if (isl_set_fast_is_empty(dom)) {
4751 res = isl_map_empty_like_basic_map(bmap);
4752 *empty = isl_set_empty_like(dom);
4754 isl_basic_map_free(bmap);
4758 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4759 isl_basic_set_copy(dom->p[0]), empty, max);
4761 for (i = 1; i < dom->n; ++i) {
4762 struct isl_map *res_i;
4763 struct isl_set *empty_i;
4765 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4766 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4768 res = isl_map_union_disjoint(res, res_i);
4769 *empty = isl_set_union_disjoint(*empty, empty_i);
4773 isl_basic_map_free(bmap);
4778 isl_basic_map_free(bmap);
4782 /* Given a map "map", compute the lexicographically minimal
4783 * (or maximal) image element for each domain element in dom.
4784 * Set *empty to those elements in dom that do not have an image element.
4786 * We first compute the lexicographically minimal or maximal element
4787 * in the first basic map. This results in a partial solution "res"
4788 * and a subset "todo" of dom that still need to be handled.
4789 * We then consider each of the remaining maps in "map" and successively
4790 * improve both "res" and "todo".
4792 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4793 * Assume we are computing the lexicographical maximum.
4794 * We first compute the lexicographically maximal element in basic map i.
4795 * This results in a partial solution res_i and a subset todo_i.
4796 * Then we combine these results with those obtain for the first k basic maps
4797 * to obtain a result that is valid for the first k+1 basic maps.
4798 * In particular, the set where there is no solution is the set where
4799 * there is no solution for the first k basic maps and also no solution
4800 * for the ith basic map, i.e.,
4802 * todo^i = todo^k * todo_i
4804 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4805 * solutions, arbitrarily breaking ties in favor of res^k.
4806 * That is, when res^k(a) >= res_i(a), we pick res^k and
4807 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4808 * the lexicographic order.)
4809 * In practice, we compute
4811 * res^k * (res_i . "<=")
4815 * res_i * (res^k . "<")
4817 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4818 * where only one of res^k and res_i provides a solution and we simply pick
4825 * Note that we only compute these intersections when dom(res^k) intersects
4826 * dom(res_i). Otherwise, the only effect of these intersections is to
4827 * potentially break up res^k and res_i into smaller pieces.
4828 * We want to avoid such splintering as much as possible.
4829 * In fact, an earlier implementation of this function would look for
4830 * better results in the domain of res^k and for extra results in todo^k,
4831 * but this would always result in a splintering according to todo^k,
4832 * even when the domain of basic map i is disjoint from the domains of
4833 * the previous basic maps.
4835 static __isl_give isl_map *isl_map_partial_lexopt(
4836 __isl_take isl_map *map, __isl_take isl_set *dom,
4837 __isl_give isl_set **empty, int max)
4840 struct isl_map *res;
4841 struct isl_set *todo;
4846 if (isl_map_fast_is_empty(map)) {
4854 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4855 isl_set_copy(dom), &todo, max);
4857 for (i = 1; i < map->n; ++i) {
4861 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4863 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4864 isl_set_copy(dom), &todo_i, max);
4867 lt = isl_map_lex_lt(isl_dim_copy(dim));
4868 le = isl_map_lex_le(dim);
4870 lt = isl_map_lex_gt(isl_dim_copy(dim));
4871 le = isl_map_lex_ge(dim);
4873 lt = isl_map_apply_range(isl_map_copy(res), lt);
4874 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4875 le = isl_map_apply_range(isl_map_copy(res_i), le);
4876 le = isl_map_intersect(le, isl_map_copy(res));
4878 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4879 res = isl_map_intersect_domain(res,
4880 isl_set_copy(todo_i));
4881 res_i = isl_map_intersect_domain(res_i,
4882 isl_set_copy(todo));
4885 res = isl_map_union_disjoint(res, res_i);
4886 res = isl_map_union_disjoint(res, lt);
4887 res = isl_map_union_disjoint(res, le);
4889 todo = isl_set_intersect(todo, todo_i);
4909 __isl_give isl_map *isl_map_partial_lexmax(
4910 __isl_take isl_map *map, __isl_take isl_set *dom,
4911 __isl_give isl_set **empty)
4913 return isl_map_partial_lexopt(map, dom, empty, 1);
4916 __isl_give isl_map *isl_map_partial_lexmin(
4917 __isl_take isl_map *map, __isl_take isl_set *dom,
4918 __isl_give isl_set **empty)
4920 return isl_map_partial_lexopt(map, dom, empty, 0);
4923 __isl_give isl_set *isl_set_partial_lexmin(
4924 __isl_take isl_set *set, __isl_take isl_set *dom,
4925 __isl_give isl_set **empty)
4927 return (struct isl_set *)
4928 isl_map_partial_lexmin((struct isl_map *)set,
4932 __isl_give isl_set *isl_set_partial_lexmax(
4933 __isl_take isl_set *set, __isl_take isl_set *dom,
4934 __isl_give isl_set **empty)
4936 return (struct isl_set *)
4937 isl_map_partial_lexmax((struct isl_map *)set,
4941 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4943 struct isl_basic_set *dom = NULL;
4944 struct isl_dim *dom_dim;
4948 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4949 dom = isl_basic_set_universe(dom_dim);
4950 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4952 isl_basic_map_free(bmap);
4956 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4958 return isl_basic_map_lexopt(bmap, 0);
4961 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4963 return isl_basic_map_lexopt(bmap, 1);
4966 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4968 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4971 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4973 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4976 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4978 struct isl_set *dom = NULL;
4979 struct isl_dim *dom_dim;
4983 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4984 dom = isl_set_universe(dom_dim);
4985 return isl_map_partial_lexopt(map, dom, NULL, max);
4991 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4993 return isl_map_lexopt(map, 0);
4996 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4998 return isl_map_lexopt(map, 1);
5001 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5003 return (isl_set *)isl_map_lexmin((isl_map *)set);
5006 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5008 return (isl_set *)isl_map_lexmax((isl_map *)set);
5011 /* Apply a preimage specified by "mat" on the parameters of "bset".
5012 * bset is assumed to have only parameters and divs.
5014 static struct isl_basic_set *basic_set_parameter_preimage(
5015 struct isl_basic_set *bset, struct isl_mat *mat)
5022 bset->dim = isl_dim_cow(bset->dim);
5026 nparam = isl_basic_set_dim(bset, isl_dim_param);
5028 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5030 bset->dim->nparam = 0;
5031 bset->dim->n_out = nparam;
5032 bset = isl_basic_set_preimage(bset, mat);
5034 bset->dim->nparam = bset->dim->n_out;
5035 bset->dim->n_out = 0;
5040 isl_basic_set_free(bset);
5044 /* Apply a preimage specified by "mat" on the parameters of "set".
5045 * set is assumed to have only parameters and divs.
5047 static struct isl_set *set_parameter_preimage(
5048 struct isl_set *set, struct isl_mat *mat)
5050 struct isl_dim *dim = NULL;
5056 dim = isl_dim_copy(set->dim);
5057 dim = isl_dim_cow(dim);
5061 nparam = isl_set_dim(set, isl_dim_param);
5063 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5066 dim->n_out = nparam;
5067 isl_set_reset_dim(set, dim);
5068 set = isl_set_preimage(set, mat);
5071 dim = isl_dim_copy(set->dim);
5072 dim = isl_dim_cow(dim);
5075 dim->nparam = dim->n_out;
5077 isl_set_reset_dim(set, dim);
5087 /* Intersect the basic set "bset" with the affine space specified by the
5088 * equalities in "eq".
5090 static struct isl_basic_set *basic_set_append_equalities(
5091 struct isl_basic_set *bset, struct isl_mat *eq)
5099 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5104 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5105 for (i = 0; i < eq->n_row; ++i) {
5106 k = isl_basic_set_alloc_equality(bset);
5109 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5110 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5114 bset = isl_basic_set_gauss(bset, NULL);
5115 bset = isl_basic_set_finalize(bset);
5120 isl_basic_set_free(bset);
5124 /* Intersect the set "set" with the affine space specified by the
5125 * equalities in "eq".
5127 static struct isl_set *set_append_equalities(struct isl_set *set,
5135 for (i = 0; i < set->n; ++i) {
5136 set->p[i] = basic_set_append_equalities(set->p[i],
5149 /* Project the given basic set onto its parameter domain, possibly introducing
5150 * new, explicit, existential variables in the constraints.
5151 * The input has parameters and (possibly implicit) existential variables.
5152 * The output has the same parameters, but only
5153 * explicit existentially quantified variables.
5155 * The actual projection is performed by pip, but pip doesn't seem
5156 * to like equalities very much, so we first remove the equalities
5157 * among the parameters by performing a variable compression on
5158 * the parameters. Afterward, an inverse transformation is performed
5159 * and the equalities among the parameters are inserted back in.
5161 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5165 struct isl_mat *T, *T2;
5166 struct isl_set *set;
5167 unsigned nparam, n_div;
5169 bset = isl_basic_set_cow(bset);
5173 if (bset->n_eq == 0)
5174 return isl_basic_set_lexmin(bset);
5176 isl_basic_set_gauss(bset, NULL);
5178 nparam = isl_basic_set_dim(bset, isl_dim_param);
5179 n_div = isl_basic_set_dim(bset, isl_dim_div);
5181 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5182 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5185 if (i == bset->n_eq)
5186 return isl_basic_set_lexmin(bset);
5188 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
5190 eq = isl_mat_cow(eq);
5191 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5192 if (T && T->n_col == 0) {
5196 bset = isl_basic_set_set_to_empty(bset);
5197 return isl_set_from_basic_set(bset);
5199 bset = basic_set_parameter_preimage(bset, T);
5201 set = isl_basic_set_lexmin(bset);
5202 set = set_parameter_preimage(set, T2);
5203 set = set_append_equalities(set, eq);
5207 /* Compute an explicit representation for all the existentially
5208 * quantified variables.
5209 * The input and output dimensions are first turned into parameters.
5210 * compute_divs then returns a map with the same parameters and
5211 * no input or output dimensions and the dimension specification
5212 * is reset to that of the input.
5214 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5216 struct isl_basic_set *bset;
5217 struct isl_set *set;
5218 struct isl_map *map;
5219 struct isl_dim *dim, *orig_dim = NULL;
5224 bmap = isl_basic_map_cow(bmap);
5228 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5229 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5230 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5231 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5235 orig_dim = bmap->dim;
5237 bset = (struct isl_basic_set *)bmap;
5239 set = parameter_compute_divs(bset);
5240 map = (struct isl_map *)set;
5241 map = isl_map_reset_dim(map, orig_dim);
5245 isl_basic_map_free(bmap);
5249 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5257 off = isl_dim_total(bmap->dim);
5258 for (i = 0; i < bmap->n_div; ++i) {
5259 if (isl_int_is_zero(bmap->div[i][0]))
5261 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5267 static int map_divs_known(__isl_keep isl_map *map)
5274 for (i = 0; i < map->n; ++i) {
5275 int known = basic_map_divs_known(map->p[i]);
5283 /* If bmap contains any unknown divs, then compute explicit
5284 * expressions for them. However, this computation may be
5285 * quite expensive, so first try to remove divs that aren't
5288 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5292 struct isl_map *map;
5294 known = basic_map_divs_known(bmap);
5298 return isl_map_from_basic_map(bmap);
5300 bmap = isl_basic_map_drop_redundant_divs(bmap);
5302 known = basic_map_divs_known(bmap);
5306 return isl_map_from_basic_map(bmap);
5308 map = compute_divs(bmap);
5311 isl_basic_map_free(bmap);
5315 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5319 struct isl_map *res;
5326 known = map_divs_known(map);
5334 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5335 for (i = 1 ; i < map->n; ++i) {
5337 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5338 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5339 res = isl_map_union_disjoint(res, r2);
5341 res = isl_map_union(res, r2);
5348 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5350 return (struct isl_set *)
5351 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5354 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5356 return (struct isl_set *)
5357 isl_map_compute_divs((struct isl_map *)set);
5360 struct isl_set *isl_map_domain(struct isl_map *map)
5363 struct isl_set *set;
5368 map = isl_map_cow(map);
5372 set = (struct isl_set *)map;
5373 set->dim = isl_dim_domain(set->dim);
5376 for (i = 0; i < map->n; ++i) {
5377 set->p[i] = isl_basic_map_domain(map->p[i]);
5381 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5382 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5389 struct isl_map *isl_map_union_disjoint(
5390 struct isl_map *map1, struct isl_map *map2)
5394 struct isl_map *map = NULL;
5408 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5410 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5411 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5412 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5414 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5415 map1->n + map2->n, flags);
5418 for (i = 0; i < map1->n; ++i) {
5419 map = isl_map_add_basic_map(map,
5420 isl_basic_map_copy(map1->p[i]));
5424 for (i = 0; i < map2->n; ++i) {
5425 map = isl_map_add_basic_map(map,
5426 isl_basic_map_copy(map2->p[i]));
5440 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5442 map1 = isl_map_union_disjoint(map1, map2);
5446 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5450 struct isl_set *isl_set_union_disjoint(
5451 struct isl_set *set1, struct isl_set *set2)
5453 return (struct isl_set *)
5454 isl_map_union_disjoint(
5455 (struct isl_map *)set1, (struct isl_map *)set2);
5458 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5460 return (struct isl_set *)
5461 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5464 struct isl_map *isl_map_intersect_range(
5465 struct isl_map *map, struct isl_set *set)
5468 struct isl_map *result;
5474 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5475 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5476 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5478 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5479 map->n * set->n, flags);
5482 for (i = 0; i < map->n; ++i)
5483 for (j = 0; j < set->n; ++j) {
5484 result = isl_map_add_basic_map(result,
5485 isl_basic_map_intersect_range(
5486 isl_basic_map_copy(map->p[i]),
5487 isl_basic_set_copy(set->p[j])));
5500 struct isl_map *isl_map_intersect_domain(
5501 struct isl_map *map, struct isl_set *set)
5503 return isl_map_reverse(
5504 isl_map_intersect_range(isl_map_reverse(map), set));
5507 struct isl_map *isl_map_apply_domain(
5508 struct isl_map *map1, struct isl_map *map2)
5512 map1 = isl_map_reverse(map1);
5513 map1 = isl_map_apply_range(map1, map2);
5514 return isl_map_reverse(map1);
5521 struct isl_map *isl_map_apply_range(
5522 struct isl_map *map1, struct isl_map *map2)
5524 struct isl_dim *dim_result;
5525 struct isl_map *result;
5531 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5532 isl_dim_copy(map2->dim));
5534 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5537 for (i = 0; i < map1->n; ++i)
5538 for (j = 0; j < map2->n; ++j) {
5539 result = isl_map_add_basic_map(result,
5540 isl_basic_map_apply_range(
5541 isl_basic_map_copy(map1->p[i]),
5542 isl_basic_map_copy(map2->p[j])));
5548 if (result && result->n <= 1)
5549 ISL_F_SET(result, ISL_MAP_DISJOINT);
5558 * returns range - domain
5560 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5562 isl_dim *dims, *target_dim;
5563 struct isl_basic_set *bset;
5570 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5571 bmap->dim, isl_dim_out),
5573 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5574 dim = isl_basic_map_n_in(bmap);
5575 nparam = isl_basic_map_n_param(bmap);
5576 bset = isl_basic_set_from_basic_map(bmap);
5577 bset = isl_basic_set_cow(bset);
5578 dims = isl_basic_set_get_dim(bset);
5579 dims = isl_dim_add(dims, isl_dim_set, dim);
5580 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5581 bset = isl_basic_set_swap_vars(bset, 2*dim);
5582 for (i = 0; i < dim; ++i) {
5583 int j = isl_basic_map_alloc_equality(
5584 (struct isl_basic_map *)bset);
5587 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5588 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5589 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5590 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5592 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5593 bset = isl_basic_set_reset_dim(bset, target_dim);
5596 isl_basic_map_free(bmap);
5601 * returns range - domain
5603 struct isl_set *isl_map_deltas(struct isl_map *map)
5607 struct isl_set *result;
5612 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5613 map->dim, isl_dim_out),
5615 dim = isl_map_get_dim(map);
5616 dim = isl_dim_domain(dim);
5617 result = isl_set_alloc_dim(dim, map->n, 0);
5620 for (i = 0; i < map->n; ++i)
5621 result = isl_set_add_basic_set(result,
5622 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5631 * returns [domain -> range] -> range - domain
5633 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5634 __isl_take isl_basic_map *bmap)
5638 isl_basic_map *domain;
5639 isl_basic_set *bset;
5643 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5644 isl_die(bmap->ctx, isl_error_invalid,
5645 "domain and range don't match", goto error);
5647 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5648 n = isl_basic_map_dim(bmap, isl_dim_in);
5650 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5651 domain = isl_basic_map_universe(dim);
5653 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5654 bmap = isl_basic_map_apply_range(bmap, domain);
5655 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5657 total = isl_basic_map_total_dim(bmap);
5659 for (i = 0; i < n; ++i) {
5660 k = isl_basic_map_alloc_equality(bmap);
5663 isl_seq_clr(bmap->eq[k], 1 + total);
5664 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5665 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5666 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5669 bmap = isl_basic_map_gauss(bmap, NULL);
5670 return isl_basic_map_finalize(bmap);
5672 isl_basic_map_free(bmap);
5677 * returns [domain -> range] -> range - domain
5679 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5682 isl_dim *domain_dim;
5687 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5688 isl_die(map->ctx, isl_error_invalid,
5689 "domain and range don't match", goto error);
5691 map = isl_map_cow(map);
5695 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5696 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5697 map->dim = isl_dim_join(map->dim, domain_dim);
5700 for (i = 0; i < map->n; ++i) {
5701 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5705 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5712 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5714 struct isl_basic_map *bmap;
5722 nparam = dims->nparam;
5724 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5728 for (i = 0; i < dim; ++i) {
5729 int j = isl_basic_map_alloc_equality(bmap);
5732 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5733 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5734 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5736 return isl_basic_map_finalize(bmap);
5738 isl_basic_map_free(bmap);
5742 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5746 if (dim->n_in != dim->n_out)
5747 isl_die(dim->ctx, isl_error_invalid,
5748 "number of input and output dimensions needs to be "
5749 "the same", goto error);
5750 return basic_map_identity(dim);
5756 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5758 if (!model || !model->dim)
5760 return isl_basic_map_identity(isl_dim_copy(model->dim));
5763 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5765 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5768 struct isl_map *isl_map_identity_like(struct isl_map *model)
5770 if (!model || !model->dim)
5772 return isl_map_identity(isl_dim_copy(model->dim));
5775 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5777 if (!model || !model->dim)
5779 return isl_map_identity(isl_dim_copy(model->dim));
5782 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5784 isl_dim *dim = isl_set_get_dim(set);
5786 id = isl_map_identity(isl_dim_map_from_set(dim));
5787 return isl_map_intersect_range(id, set);
5790 /* Construct a basic set with all set dimensions having only non-negative
5793 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5798 struct isl_basic_set *bset;
5802 nparam = dims->nparam;
5804 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5807 for (i = 0; i < dim; ++i) {
5808 int k = isl_basic_set_alloc_inequality(bset);
5811 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5812 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5816 isl_basic_set_free(bset);
5820 /* Construct the half-space x_pos >= 0.
5822 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5826 isl_basic_set *nonneg;
5828 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5829 k = isl_basic_set_alloc_inequality(nonneg);
5832 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5833 isl_int_set_si(nonneg->ineq[k][pos], 1);
5835 return isl_basic_set_finalize(nonneg);
5837 isl_basic_set_free(nonneg);
5841 /* Construct the half-space x_pos <= -1.
5843 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5848 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5849 k = isl_basic_set_alloc_inequality(neg);
5852 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5853 isl_int_set_si(neg->ineq[k][0], -1);
5854 isl_int_set_si(neg->ineq[k][pos], -1);
5856 return isl_basic_set_finalize(neg);
5858 isl_basic_set_free(neg);
5862 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5863 enum isl_dim_type type, unsigned first, unsigned n)
5866 isl_basic_set *nonneg;
5874 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5876 for (i = 0; i < n; ++i) {
5877 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5878 pos(set->dim, type) + first + i);
5879 neg = neg_halfspace(isl_set_get_dim(set),
5880 pos(set->dim, type) + first + i);
5882 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5891 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5892 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5899 if (isl_set_fast_is_empty(set)) {
5904 return fn(set, signs, user);
5907 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5909 half = isl_set_intersect(half, isl_set_copy(set));
5910 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5914 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5916 half = isl_set_intersect(half, set);
5917 return foreach_orthant(half, signs, first + 1, len, fn, user);
5923 /* Call "fn" on the intersections of "set" with each of the orthants
5924 * (except for obviously empty intersections). The orthant is identified
5925 * by the signs array, with each entry having value 1 or -1 according
5926 * to the sign of the corresponding variable.
5928 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5929 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5939 if (isl_set_fast_is_empty(set))
5942 nparam = isl_set_dim(set, isl_dim_param);
5943 nvar = isl_set_dim(set, isl_dim_set);
5945 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5947 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5955 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5957 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5960 int isl_basic_map_is_subset(
5961 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5964 struct isl_map *map1;
5965 struct isl_map *map2;
5967 if (!bmap1 || !bmap2)
5970 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5971 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5973 is_subset = isl_map_is_subset(map1, map2);
5981 int isl_basic_map_is_equal(
5982 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5986 if (!bmap1 || !bmap2)
5988 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5991 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5995 int isl_basic_set_is_equal(
5996 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5998 return isl_basic_map_is_equal(
5999 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6002 int isl_map_is_empty(struct isl_map *map)
6009 for (i = 0; i < map->n; ++i) {
6010 is_empty = isl_basic_map_is_empty(map->p[i]);
6019 int isl_map_fast_is_empty(struct isl_map *map)
6021 return map ? map->n == 0 : -1;
6024 int isl_set_fast_is_empty(struct isl_set *set)
6026 return set ? set->n == 0 : -1;
6029 int isl_set_is_empty(struct isl_set *set)
6031 return isl_map_is_empty((struct isl_map *)set);
6034 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6039 return isl_dim_equal(map1->dim, map2->dim);
6042 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6047 return isl_dim_equal(set1->dim, set2->dim);
6050 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6056 is_subset = isl_map_is_subset(map1, map2);
6059 is_subset = isl_map_is_subset(map2, map1);
6063 int isl_basic_map_is_strict_subset(
6064 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6068 if (!bmap1 || !bmap2)
6070 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6073 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6074 if (is_subset == -1)
6079 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6085 is_subset = isl_map_is_subset(map1, map2);
6088 is_subset = isl_map_is_subset(map2, map1);
6089 if (is_subset == -1)
6094 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6096 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6099 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6103 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6106 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6110 return bset->n_eq == 0 && bset->n_ineq == 0;
6113 int isl_map_fast_is_universe(__isl_keep isl_map *map)
6120 for (i = 0; i < map->n; ++i) {
6121 int r = isl_basic_map_is_universe(map->p[i]);
6129 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6131 return isl_map_fast_is_universe((isl_map *) set);
6134 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6136 struct isl_basic_set *bset = NULL;
6137 struct isl_vec *sample = NULL;
6144 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6147 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6148 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6149 copy = isl_basic_map_remove_redundancies(copy);
6150 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6151 isl_basic_map_free(copy);
6155 total = 1 + isl_basic_map_total_dim(bmap);
6156 if (bmap->sample && bmap->sample->size == total) {
6157 int contains = isl_basic_map_contains(bmap, bmap->sample);
6163 isl_vec_free(bmap->sample);
6164 bmap->sample = NULL;
6165 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6168 sample = isl_basic_set_sample_vec(bset);
6171 empty = sample->size == 0;
6172 isl_vec_free(bmap->sample);
6173 bmap->sample = sample;
6175 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6180 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
6184 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6187 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
6191 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6194 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6196 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6199 struct isl_map *isl_basic_map_union(
6200 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6202 struct isl_map *map;
6203 if (!bmap1 || !bmap2)
6206 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6208 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6211 map = isl_map_add_basic_map(map, bmap1);
6212 map = isl_map_add_basic_map(map, bmap2);
6215 isl_basic_map_free(bmap1);
6216 isl_basic_map_free(bmap2);
6220 struct isl_set *isl_basic_set_union(
6221 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6223 return (struct isl_set *)isl_basic_map_union(
6224 (struct isl_basic_map *)bset1,
6225 (struct isl_basic_map *)bset2);
6228 /* Order divs such that any div only depends on previous divs */
6229 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6237 off = isl_dim_total(bmap->dim);
6239 for (i = 0; i < bmap->n_div; ++i) {
6241 if (isl_int_is_zero(bmap->div[i][0]))
6243 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6247 isl_basic_map_swap_div(bmap, i, i + pos);
6253 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6255 return (struct isl_basic_set *)
6256 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6259 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6266 for (i = 0; i < map->n; ++i) {
6267 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6278 /* Look for a div in dst that corresponds to the div "div" in src.
6279 * The divs before "div" in src and dst are assumed to be the same.
6281 * Returns -1 if no corresponding div was found and the position
6282 * of the corresponding div in dst otherwise.
6284 static int find_div(struct isl_basic_map *dst,
6285 struct isl_basic_map *src, unsigned div)
6289 unsigned total = isl_dim_total(src->dim);
6291 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6292 for (i = div; i < dst->n_div; ++i)
6293 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6294 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6295 dst->n_div - div) == -1)
6300 struct isl_basic_map *isl_basic_map_align_divs(
6301 struct isl_basic_map *dst, struct isl_basic_map *src)
6304 unsigned total = isl_dim_total(src->dim);
6309 if (src->n_div == 0)
6312 for (i = 0; i < src->n_div; ++i)
6313 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6315 src = isl_basic_map_order_divs(src);
6316 dst = isl_basic_map_cow(dst);
6317 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6318 src->n_div, 0, 2 * src->n_div);
6321 for (i = 0; i < src->n_div; ++i) {
6322 int j = find_div(dst, src, i);
6324 j = isl_basic_map_alloc_div(dst);
6327 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6328 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6329 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6333 isl_basic_map_swap_div(dst, i, j);
6337 isl_basic_map_free(dst);
6341 struct isl_basic_set *isl_basic_set_align_divs(
6342 struct isl_basic_set *dst, struct isl_basic_set *src)
6344 return (struct isl_basic_set *)isl_basic_map_align_divs(
6345 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6348 struct isl_map *isl_map_align_divs(struct isl_map *map)
6356 map = isl_map_compute_divs(map);
6357 map = isl_map_cow(map);
6361 for (i = 1; i < map->n; ++i)
6362 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6363 for (i = 1; i < map->n; ++i)
6364 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6366 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6370 struct isl_set *isl_set_align_divs(struct isl_set *set)
6372 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6375 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6379 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6380 map = isl_map_intersect_domain(map, set);
6381 set = isl_map_range(map);
6389 /* There is no need to cow as removing empty parts doesn't change
6390 * the meaning of the set.
6392 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6399 for (i = map->n-1; i >= 0; --i) {
6400 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6402 isl_basic_map_free(map->p[i]);
6403 if (i != map->n-1) {
6404 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6405 map->p[i] = map->p[map->n-1];
6413 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6415 return (struct isl_set *)
6416 isl_map_remove_empty_parts((struct isl_map *)set);
6419 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6421 struct isl_basic_map *bmap;
6422 if (!map || map->n == 0)
6424 bmap = map->p[map->n-1];
6425 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6426 return isl_basic_map_copy(bmap);
6429 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6431 return (struct isl_basic_set *)
6432 isl_map_copy_basic_map((struct isl_map *)set);
6435 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6436 __isl_keep isl_basic_map *bmap)
6442 for (i = map->n-1; i >= 0; --i) {
6443 if (map->p[i] != bmap)
6445 map = isl_map_cow(map);
6448 isl_basic_map_free(map->p[i]);
6449 if (i != map->n-1) {
6450 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6451 map->p[i] = map->p[map->n-1];
6462 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6463 struct isl_basic_set *bset)
6465 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6466 (struct isl_basic_map *)bset);
6469 /* Given two basic sets bset1 and bset2, compute the maximal difference
6470 * between the values of dimension pos in bset1 and those in bset2
6471 * for any common value of the parameters and dimensions preceding pos.
6473 static enum isl_lp_result basic_set_maximal_difference_at(
6474 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6475 int pos, isl_int *opt)
6477 struct isl_dim *dims;
6478 struct isl_basic_map *bmap1 = NULL;
6479 struct isl_basic_map *bmap2 = NULL;
6480 struct isl_ctx *ctx;
6481 struct isl_vec *obj;
6484 unsigned dim1, dim2;
6485 enum isl_lp_result res;
6487 if (!bset1 || !bset2)
6488 return isl_lp_error;
6490 nparam = isl_basic_set_n_param(bset1);
6491 dim1 = isl_basic_set_n_dim(bset1);
6492 dim2 = isl_basic_set_n_dim(bset2);
6493 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6494 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6495 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6496 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6497 if (!bmap1 || !bmap2)
6499 bmap1 = isl_basic_map_cow(bmap1);
6500 bmap1 = isl_basic_map_extend(bmap1, nparam,
6501 pos, (dim1 - pos) + (dim2 - pos),
6502 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6503 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6506 total = isl_basic_map_total_dim(bmap1);
6508 obj = isl_vec_alloc(ctx, 1 + total);
6509 isl_seq_clr(obj->block.data, 1 + total);
6510 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6511 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6514 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6516 isl_basic_map_free(bmap1);
6520 isl_basic_map_free(bmap1);
6521 isl_basic_map_free(bmap2);
6522 return isl_lp_error;
6525 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6526 * for any common value of the parameters and dimensions preceding pos
6527 * in both basic sets, the values of dimension pos in bset1 are
6528 * smaller or larger than those in bset2.
6531 * 1 if bset1 follows bset2
6532 * -1 if bset1 precedes bset2
6533 * 0 if bset1 and bset2 are incomparable
6534 * -2 if some error occurred.
6536 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6537 struct isl_basic_set *bset2, int pos)
6540 enum isl_lp_result res;
6545 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6547 if (res == isl_lp_empty)
6549 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6550 res == isl_lp_unbounded)
6552 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6561 /* Given two basic sets bset1 and bset2, check whether
6562 * for any common value of the parameters and dimensions preceding pos
6563 * there is a value of dimension pos in bset1 that is larger
6564 * than a value of the same dimension in bset2.
6567 * 1 if there exists such a pair
6568 * 0 if there is no such pair, but there is a pair of equal values
6570 * -2 if some error occurred.
6572 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6573 __isl_keep isl_basic_set *bset2, int pos)
6576 enum isl_lp_result res;
6581 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6583 if (res == isl_lp_empty)
6585 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6586 res == isl_lp_unbounded)
6588 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6590 else if (res == isl_lp_ok)
6599 /* Given two sets set1 and set2, check whether
6600 * for any common value of the parameters and dimensions preceding pos
6601 * there is a value of dimension pos in set1 that is larger
6602 * than a value of the same dimension in set2.
6605 * 1 if there exists such a pair
6606 * 0 if there is no such pair, but there is a pair of equal values
6608 * -2 if some error occurred.
6610 int isl_set_follows_at(__isl_keep isl_set *set1,
6611 __isl_keep isl_set *set2, int pos)
6619 for (i = 0; i < set1->n; ++i)
6620 for (j = 0; j < set2->n; ++j) {
6622 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6623 if (f == 1 || f == -2)
6632 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
6633 unsigned pos, isl_int *val)
6641 total = isl_basic_map_total_dim(bmap);
6642 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6643 for (; d+1 > pos; --d)
6644 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6648 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6650 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6652 if (!isl_int_is_one(bmap->eq[i][1+d]))
6655 isl_int_neg(*val, bmap->eq[i][0]);
6661 static int isl_map_fast_has_fixed_var(struct isl_map *map,
6662 unsigned pos, isl_int *val)
6674 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
6677 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
6678 for (i = 1; fixed == 1 && i < map->n; ++i) {
6679 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
6680 if (fixed == 1 && isl_int_ne(tmp, v))
6684 isl_int_set(*val, v);
6690 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
6691 unsigned pos, isl_int *val)
6693 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
6697 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
6700 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
6703 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
6704 enum isl_dim_type type, unsigned pos, isl_int *val)
6706 if (pos >= isl_basic_map_dim(bmap, type))
6708 return isl_basic_map_fast_has_fixed_var(bmap,
6709 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6712 int isl_map_fast_is_fixed(struct isl_map *map,
6713 enum isl_dim_type type, unsigned pos, isl_int *val)
6715 if (pos >= isl_map_dim(map, type))
6717 return isl_map_fast_has_fixed_var(map,
6718 map_offset(map, type) - 1 + pos, val);
6721 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6722 * then return this fixed value in *val.
6724 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6727 return isl_basic_set_fast_has_fixed_var(bset,
6728 isl_basic_set_n_param(bset) + dim, val);
6731 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6732 * then return this fixed value in *val.
6734 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6736 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6739 /* Check if input variable in has fixed value and if so and if val is not NULL,
6740 * then return this fixed value in *val.
6742 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6744 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6747 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6748 * and if val is not NULL, then return this lower bound in *val.
6750 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6751 unsigned dim, isl_int *val)
6753 int i, i_eq = -1, i_ineq = -1;
6760 total = isl_basic_set_total_dim(bset);
6761 nparam = isl_basic_set_n_param(bset);
6762 for (i = 0; i < bset->n_eq; ++i) {
6763 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6769 for (i = 0; i < bset->n_ineq; ++i) {
6770 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6772 if (i_eq != -1 || i_ineq != -1)
6776 if (i_eq == -1 && i_ineq == -1)
6778 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6779 /* The coefficient should always be one due to normalization. */
6780 if (!isl_int_is_one(c[1+nparam+dim]))
6782 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6784 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6785 total - nparam - dim - 1) != -1)
6788 isl_int_neg(*val, c[0]);
6792 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6793 unsigned dim, isl_int *val)
6805 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6809 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6811 for (i = 1; fixed == 1 && i < set->n; ++i) {
6812 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6814 if (fixed == 1 && isl_int_ne(tmp, v))
6818 isl_int_set(*val, v);
6829 /* uset_gist depends on constraints without existentially quantified
6830 * variables sorting first.
6832 static int qsort_constraint_cmp(const void *p1, const void *p2)
6834 const struct constraint *c1 = (const struct constraint *)p1;
6835 const struct constraint *c2 = (const struct constraint *)p2;
6837 unsigned size = isl_min(c1->size, c2->size);
6839 l1 = isl_seq_last_non_zero(c1->c, size);
6840 l2 = isl_seq_last_non_zero(c2->c, size);
6845 return isl_seq_cmp(c1->c, c2->c, size);
6848 static struct isl_basic_map *isl_basic_map_sort_constraints(
6849 struct isl_basic_map *bmap)
6852 struct constraint *c;
6857 total = isl_basic_map_total_dim(bmap);
6858 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6861 for (i = 0; i < bmap->n_ineq; ++i) {
6863 c[i].c = bmap->ineq[i];
6865 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6866 for (i = 0; i < bmap->n_ineq; ++i)
6867 bmap->ineq[i] = c[i].c;
6871 isl_basic_map_free(bmap);
6875 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6876 __isl_take isl_basic_set *bset)
6878 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6879 (struct isl_basic_map *)bset);
6882 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6886 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6888 bmap = isl_basic_map_remove_redundancies(bmap);
6889 bmap = isl_basic_map_sort_constraints(bmap);
6890 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6894 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6896 return (struct isl_basic_set *)isl_basic_map_normalize(
6897 (struct isl_basic_map *)bset);
6900 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6901 const __isl_keep isl_basic_map *bmap2)
6908 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
6909 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
6910 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
6911 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6912 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6913 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6914 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6915 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6916 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6917 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6918 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6920 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6922 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6924 if (bmap1->n_eq != bmap2->n_eq)
6925 return bmap1->n_eq - bmap2->n_eq;
6926 if (bmap1->n_ineq != bmap2->n_ineq)
6927 return bmap1->n_ineq - bmap2->n_ineq;
6928 if (bmap1->n_div != bmap2->n_div)
6929 return bmap1->n_div - bmap2->n_div;
6930 total = isl_basic_map_total_dim(bmap1);
6931 for (i = 0; i < bmap1->n_eq; ++i) {
6932 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6936 for (i = 0; i < bmap1->n_ineq; ++i) {
6937 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6941 for (i = 0; i < bmap1->n_div; ++i) {
6942 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6949 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6950 __isl_keep isl_basic_map *bmap2)
6952 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6955 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6956 __isl_keep isl_basic_set *bset2)
6958 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6959 (isl_basic_map *)bset2);
6962 static int qsort_bmap_cmp(const void *p1, const void *p2)
6964 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6965 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6967 return isl_basic_map_fast_cmp(bmap1, bmap2);
6970 /* We normalize in place, but if anything goes wrong we need
6971 * to return NULL, so we need to make sure we don't change the
6972 * meaning of any possible other copies of map.
6974 struct isl_map *isl_map_normalize(struct isl_map *map)
6977 struct isl_basic_map *bmap;
6981 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6983 for (i = 0; i < map->n; ++i) {
6984 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6987 isl_basic_map_free(map->p[i]);
6990 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6991 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6992 map = isl_map_remove_empty_parts(map);
6995 for (i = map->n - 1; i >= 1; --i) {
6996 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6998 isl_basic_map_free(map->p[i-1]);
6999 for (j = i; j < map->n; ++j)
7000 map->p[j-1] = map->p[j];
7010 struct isl_set *isl_set_normalize(struct isl_set *set)
7012 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7015 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
7025 if (!isl_dim_equal(map1->dim, map2->dim))
7028 map1 = isl_map_copy(map1);
7029 map2 = isl_map_copy(map2);
7030 map1 = isl_map_normalize(map1);
7031 map2 = isl_map_normalize(map2);
7034 equal = map1->n == map2->n;
7035 for (i = 0; equal && i < map1->n; ++i) {
7036 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
7049 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
7051 return isl_map_fast_is_equal((struct isl_map *)set1,
7052 (struct isl_map *)set2);
7055 /* Return an interval that ranges from min to max (inclusive)
7057 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7058 isl_int min, isl_int max)
7061 struct isl_basic_set *bset = NULL;
7063 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7067 k = isl_basic_set_alloc_inequality(bset);
7070 isl_int_set_si(bset->ineq[k][1], 1);
7071 isl_int_neg(bset->ineq[k][0], min);
7073 k = isl_basic_set_alloc_inequality(bset);
7076 isl_int_set_si(bset->ineq[k][1], -1);
7077 isl_int_set(bset->ineq[k][0], max);
7081 isl_basic_set_free(bset);
7085 /* Return the Cartesian product of the basic sets in list (in the given order).
7087 __isl_give isl_basic_set *isl_basic_set_list_product(
7088 __isl_take struct isl_basic_set_list *list)
7096 struct isl_basic_set *product = NULL;
7100 isl_assert(list->ctx, list->n > 0, goto error);
7101 isl_assert(list->ctx, list->p[0], goto error);
7102 nparam = isl_basic_set_n_param(list->p[0]);
7103 dim = isl_basic_set_n_dim(list->p[0]);
7104 extra = list->p[0]->n_div;
7105 n_eq = list->p[0]->n_eq;
7106 n_ineq = list->p[0]->n_ineq;
7107 for (i = 1; i < list->n; ++i) {
7108 isl_assert(list->ctx, list->p[i], goto error);
7109 isl_assert(list->ctx,
7110 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7111 dim += isl_basic_set_n_dim(list->p[i]);
7112 extra += list->p[i]->n_div;
7113 n_eq += list->p[i]->n_eq;
7114 n_ineq += list->p[i]->n_ineq;
7116 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7121 for (i = 0; i < list->n; ++i) {
7122 isl_basic_set_add_constraints(product,
7123 isl_basic_set_copy(list->p[i]), dim);
7124 dim += isl_basic_set_n_dim(list->p[i]);
7126 isl_basic_set_list_free(list);
7129 isl_basic_set_free(product);
7130 isl_basic_set_list_free(list);
7134 struct isl_basic_map *isl_basic_map_product(
7135 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7137 struct isl_dim *dim_result = NULL;
7138 struct isl_basic_map *bmap;
7139 unsigned in1, in2, out1, out2, nparam, total, pos;
7140 struct isl_dim_map *dim_map1, *dim_map2;
7142 if (!bmap1 || !bmap2)
7145 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7146 bmap2->dim, isl_dim_param), goto error);
7147 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7148 isl_dim_copy(bmap2->dim));
7150 in1 = isl_basic_map_n_in(bmap1);
7151 in2 = isl_basic_map_n_in(bmap2);
7152 out1 = isl_basic_map_n_out(bmap1);
7153 out2 = isl_basic_map_n_out(bmap2);
7154 nparam = isl_basic_map_n_param(bmap1);
7156 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7157 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7158 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7159 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7160 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7161 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7162 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7163 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7164 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7165 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7166 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7168 bmap = isl_basic_map_alloc_dim(dim_result,
7169 bmap1->n_div + bmap2->n_div,
7170 bmap1->n_eq + bmap2->n_eq,
7171 bmap1->n_ineq + bmap2->n_ineq);
7172 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
7173 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
7174 bmap = isl_basic_map_simplify(bmap);
7175 return isl_basic_map_finalize(bmap);
7177 isl_basic_map_free(bmap1);
7178 isl_basic_map_free(bmap2);
7182 __isl_give isl_basic_map *isl_basic_map_flat_product(
7183 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7185 isl_basic_map *prod;
7187 prod = isl_basic_map_product(bmap1, bmap2);
7188 prod = isl_basic_map_flatten(prod);
7192 __isl_give isl_basic_set *isl_basic_set_flat_product(
7193 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7195 return isl_basic_map_flat_product(bset1, bset2);
7198 __isl_give isl_basic_map *isl_basic_map_range_product(
7199 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7201 isl_dim *dim_result = NULL;
7202 isl_basic_map *bmap;
7203 unsigned in, out1, out2, nparam, total, pos;
7204 struct isl_dim_map *dim_map1, *dim_map2;
7206 if (!bmap1 || !bmap2)
7209 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7210 isl_dim_copy(bmap2->dim));
7212 in = isl_basic_map_dim(bmap1, isl_dim_in);
7213 out1 = isl_basic_map_n_out(bmap1);
7214 out2 = isl_basic_map_n_out(bmap2);
7215 nparam = isl_basic_map_n_param(bmap1);
7217 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7218 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7219 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7220 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7221 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7222 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7223 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7224 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7225 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7226 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7227 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7229 bmap = isl_basic_map_alloc_dim(dim_result,
7230 bmap1->n_div + bmap2->n_div,
7231 bmap1->n_eq + bmap2->n_eq,
7232 bmap1->n_ineq + bmap2->n_ineq);
7233 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
7234 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
7235 bmap = isl_basic_map_simplify(bmap);
7236 return isl_basic_map_finalize(bmap);
7238 isl_basic_map_free(bmap1);
7239 isl_basic_map_free(bmap2);
7243 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7244 __isl_take isl_map *map2,
7245 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7246 __isl_take isl_dim *right),
7247 __isl_give isl_basic_map *(*basic_map_product)(
7248 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7251 struct isl_map *result;
7257 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7258 map2->dim, isl_dim_param), goto error);
7260 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7261 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7262 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7264 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7265 isl_dim_copy(map2->dim)),
7266 map1->n * map2->n, flags);
7269 for (i = 0; i < map1->n; ++i)
7270 for (j = 0; j < map2->n; ++j) {
7271 struct isl_basic_map *part;
7272 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7273 isl_basic_map_copy(map2->p[j]));
7274 if (isl_basic_map_is_empty(part))
7275 isl_basic_map_free(part);
7277 result = isl_map_add_basic_map(result, part);
7290 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7292 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7294 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7297 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7299 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7300 __isl_take isl_map *map2)
7304 prod = isl_map_product(map1, map2);
7305 prod = isl_map_flatten(prod);
7309 /* Given two set A and B, construct its Cartesian product A x B.
7311 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7313 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7314 (struct isl_map *)set2);
7317 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7318 __isl_take isl_set *set2)
7320 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7323 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7325 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7326 __isl_take isl_map *map2)
7328 return map_product(map1, map2, &isl_dim_range_product,
7329 &isl_basic_map_range_product);
7332 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7335 uint32_t hash = isl_hash_init();
7340 bmap = isl_basic_map_copy(bmap);
7341 bmap = isl_basic_map_normalize(bmap);
7344 total = isl_basic_map_total_dim(bmap);
7345 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7346 for (i = 0; i < bmap->n_eq; ++i) {
7348 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7349 isl_hash_hash(hash, c_hash);
7351 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7352 for (i = 0; i < bmap->n_ineq; ++i) {
7354 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7355 isl_hash_hash(hash, c_hash);
7357 isl_hash_byte(hash, bmap->n_div & 0xFF);
7358 for (i = 0; i < bmap->n_div; ++i) {
7360 if (isl_int_is_zero(bmap->div[i][0]))
7362 isl_hash_byte(hash, i & 0xFF);
7363 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7364 isl_hash_hash(hash, c_hash);
7366 isl_basic_map_free(bmap);
7370 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7372 return isl_basic_map_get_hash((isl_basic_map *)bset);
7375 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7382 map = isl_map_copy(map);
7383 map = isl_map_normalize(map);
7387 hash = isl_hash_init();
7388 for (i = 0; i < map->n; ++i) {
7390 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7391 isl_hash_hash(hash, bmap_hash);
7399 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7401 return isl_map_get_hash((isl_map *)set);
7404 /* Check if the value for dimension dim is completely determined
7405 * by the values of the other parameters and variables.
7406 * That is, check if dimension dim is involved in an equality.
7408 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7415 nparam = isl_basic_set_n_param(bset);
7416 for (i = 0; i < bset->n_eq; ++i)
7417 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7422 /* Check if the value for dimension dim is completely determined
7423 * by the values of the other parameters and variables.
7424 * That is, check if dimension dim is involved in an equality
7425 * for each of the subsets.
7427 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7433 for (i = 0; i < set->n; ++i) {
7435 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7442 int isl_set_n_basic_set(__isl_keep isl_set *set)
7444 return set ? set->n : 0;
7447 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7448 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7455 for (i = 0; i < map->n; ++i)
7456 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7462 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7463 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7470 for (i = 0; i < set->n; ++i)
7471 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7477 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7479 struct isl_dim *dim;
7484 bset = isl_basic_set_cow(bset);
7488 dim = isl_basic_set_get_dim(bset);
7489 dim = isl_dim_lift(dim, bset->n_div);
7492 isl_dim_free(bset->dim);
7494 bset->extra -= bset->n_div;
7497 bset = isl_basic_set_finalize(bset);
7501 isl_basic_set_free(bset);
7505 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7508 struct isl_dim *dim;
7511 set = isl_set_align_divs(set);
7516 set = isl_set_cow(set);
7520 n_div = set->p[0]->n_div;
7521 dim = isl_set_get_dim(set);
7522 dim = isl_dim_lift(dim, n_div);
7525 isl_dim_free(set->dim);
7528 for (i = 0; i < set->n; ++i) {
7529 set->p[i] = isl_basic_set_lift(set->p[i]);
7540 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7542 struct isl_dim *dim;
7543 struct isl_basic_map *bmap;
7550 set = isl_set_align_divs(set);
7555 dim = isl_set_get_dim(set);
7556 if (set->n == 0 || set->p[0]->n_div == 0) {
7558 return isl_map_identity(isl_dim_map_from_set(dim));
7561 n_div = set->p[0]->n_div;
7562 dim = isl_dim_map_from_set(dim);
7563 n_param = isl_dim_size(dim, isl_dim_param);
7564 n_set = isl_dim_size(dim, isl_dim_in);
7565 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7566 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7567 for (i = 0; i < n_set; ++i)
7568 bmap = var_equal(bmap, i);
7570 total = n_param + n_set + n_set + n_div;
7571 for (i = 0; i < n_div; ++i) {
7572 k = isl_basic_map_alloc_inequality(bmap);
7575 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7576 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7577 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7578 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7579 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7580 set->p[0]->div[i][0]);
7582 l = isl_basic_map_alloc_inequality(bmap);
7585 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7586 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7587 set->p[0]->div[i][0]);
7588 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7592 bmap = isl_basic_map_simplify(bmap);
7593 bmap = isl_basic_map_finalize(bmap);
7594 return isl_map_from_basic_map(bmap);
7597 isl_basic_map_free(bmap);
7601 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7609 dim = isl_basic_set_total_dim(bset);
7610 size += bset->n_eq * (1 + dim);
7611 size += bset->n_ineq * (1 + dim);
7612 size += bset->n_div * (2 + dim);
7617 int isl_set_size(__isl_keep isl_set *set)
7625 for (i = 0; i < set->n; ++i)
7626 size += isl_basic_set_size(set->p[i]);
7631 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7632 enum isl_dim_type type, unsigned pos)
7640 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7642 pos += isl_basic_map_offset(bmap, type);
7644 for (i = 0; i < bmap->n_eq; ++i)
7645 if (!isl_int_is_zero(bmap->eq[i][pos]))
7649 for (i = 0; i < bmap->n_ineq; ++i) {
7650 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7657 return lower && upper;
7660 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7661 enum isl_dim_type type, unsigned pos)
7668 for (i = 0; i < map->n; ++i) {
7670 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7671 if (bounded < 0 || !bounded)
7678 /* Return 1 if the specified dim is involved in both an upper bound
7679 * and a lower bound.
7681 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7682 enum isl_dim_type type, unsigned pos)
7684 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7687 /* For each of the "n" variables starting at "first", determine
7688 * the sign of the variable and put the results in the first "n"
7689 * elements of the array "signs".
7691 * 1 means that the variable is non-negative
7692 * -1 means that the variable is non-positive
7693 * 0 means the variable attains both positive and negative values.
7695 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7696 unsigned first, unsigned n, int *signs)
7698 isl_vec *bound = NULL;
7699 struct isl_tab *tab = NULL;
7700 struct isl_tab_undo *snap;
7703 if (!bset || !signs)
7706 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7707 tab = isl_tab_from_basic_set(bset);
7711 isl_seq_clr(bound->el, bound->size);
7712 isl_int_set_si(bound->el[0], -1);
7714 snap = isl_tab_snap(tab);
7715 for (i = 0; i < n; ++i) {
7718 isl_int_set_si(bound->el[1 + first + i], -1);
7719 if (isl_tab_add_ineq(tab, bound->el) < 0)
7722 isl_int_set_si(bound->el[1 + first + i], 0);
7723 if (isl_tab_rollback(tab, snap) < 0)
7731 isl_int_set_si(bound->el[1 + first + i], 1);
7732 if (isl_tab_add_ineq(tab, bound->el) < 0)
7735 isl_int_set_si(bound->el[1 + first + i], 0);
7736 if (isl_tab_rollback(tab, snap) < 0)
7739 signs[i] = empty ? -1 : 0;
7743 isl_vec_free(bound);
7747 isl_vec_free(bound);
7751 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7752 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7754 if (!bset || !signs)
7756 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7759 first += pos(bset->dim, type) - 1;
7760 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7763 /* Check if the given basic map is obviously single-valued.
7764 * In particular, for each output dimension, check that there is
7765 * an equality that defines the output dimension in terms of
7766 * earlier dimensions.
7768 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map *bmap)
7778 total = 1 + isl_basic_map_total_dim(bmap);
7779 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7780 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7782 for (i = 0; i < n_out; ++i) {
7783 for (j = 0; j < bmap->n_eq; ++j) {
7784 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7786 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7787 total - (o_out + i + 1)) == -1)
7790 if (j >= bmap->n_eq)
7797 /* Check if the given map is obviously single-valued.
7799 int isl_map_fast_is_single_valued(__isl_keep isl_map *map)
7810 return isl_basic_map_fast_is_single_valued(map->p[0]);
7813 /* Check if the given map is single-valued.
7818 * and check if the result is a subset of the identity mapping.
7820 int isl_map_is_single_valued(__isl_keep isl_map *map)
7827 sv = isl_map_fast_is_single_valued(map);
7831 test = isl_map_reverse(isl_map_copy(map));
7832 test = isl_map_apply_range(test, isl_map_copy(map));
7834 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
7835 id = isl_map_identity(dim);
7837 sv = isl_map_is_subset(test, id);
7845 int isl_map_is_bijective(__isl_keep isl_map *map)
7849 sv = isl_map_is_single_valued(map);
7853 map = isl_map_copy(map);
7854 map = isl_map_reverse(map);
7855 sv = isl_map_is_single_valued(map);
7861 int isl_set_is_singleton(__isl_keep isl_set *set)
7863 return isl_map_is_single_valued((isl_map *)set);
7866 int isl_map_is_translation(__isl_keep isl_map *map)
7871 delta = isl_map_deltas(isl_map_copy(map));
7872 ok = isl_set_is_singleton(delta);
7873 isl_set_free(delta);
7878 static int unique(isl_int *p, unsigned pos, unsigned len)
7880 if (isl_seq_first_non_zero(p, pos) != -1)
7882 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7887 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7896 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7899 nvar = isl_basic_set_dim(bset, isl_dim_set);
7900 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7901 for (j = 0; j < nvar; ++j) {
7902 int lower = 0, upper = 0;
7903 for (i = 0; i < bset->n_eq; ++i) {
7904 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7906 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7912 for (i = 0; i < bset->n_ineq; ++i) {
7913 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7915 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7917 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7922 if (!lower || !upper)
7929 int isl_set_is_box(__isl_keep isl_set *set)
7936 return isl_basic_set_is_box(set->p[0]);
7939 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7944 return isl_dim_is_wrapping(bset->dim);
7947 int isl_set_is_wrapping(__isl_keep isl_set *set)
7952 return isl_dim_is_wrapping(set->dim);
7955 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7957 bmap = isl_basic_map_cow(bmap);
7961 bmap->dim = isl_dim_wrap(bmap->dim);
7965 bmap = isl_basic_map_finalize(bmap);
7967 return (isl_basic_set *)bmap;
7969 isl_basic_map_free(bmap);
7973 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7977 map = isl_map_cow(map);
7981 for (i = 0; i < map->n; ++i) {
7982 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7986 map->dim = isl_dim_wrap(map->dim);
7990 return (isl_set *)map;
7996 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
7998 bset = isl_basic_set_cow(bset);
8002 bset->dim = isl_dim_unwrap(bset->dim);
8006 bset = isl_basic_set_finalize(bset);
8008 return (isl_basic_map *)bset;
8010 isl_basic_set_free(bset);
8014 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8021 if (!isl_set_is_wrapping(set))
8022 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8025 set = isl_set_cow(set);
8029 for (i = 0; i < set->n; ++i) {
8030 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8035 set->dim = isl_dim_unwrap(set->dim);
8039 return (isl_map *)set;
8045 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8046 enum isl_dim_type type)
8051 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8054 bmap = isl_basic_map_cow(bmap);
8058 bmap->dim = isl_dim_reset(bmap->dim, type);
8062 bmap = isl_basic_map_finalize(bmap);
8066 isl_basic_map_free(bmap);
8070 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8071 enum isl_dim_type type)
8078 if (!isl_dim_is_named_or_nested(map->dim, type))
8081 map = isl_map_cow(map);
8085 for (i = 0; i < map->n; ++i) {
8086 map->p[i] = isl_basic_map_reset(map->p[i], type);
8090 map->dim = isl_dim_reset(map->dim, type);
8100 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8105 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8108 bmap = isl_basic_map_cow(bmap);
8112 bmap->dim = isl_dim_flatten(bmap->dim);
8116 bmap = isl_basic_map_finalize(bmap);
8120 isl_basic_map_free(bmap);
8124 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8126 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8129 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8136 if (!map->dim->nested[0] && !map->dim->nested[1])
8139 map = isl_map_cow(map);
8143 for (i = 0; i < map->n; ++i) {
8144 map->p[i] = isl_basic_map_flatten(map->p[i]);
8148 map->dim = isl_dim_flatten(map->dim);
8158 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8160 return (isl_set *)isl_map_flatten((isl_map *)set);
8163 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8165 isl_dim *dim, *flat_dim;
8168 dim = isl_set_get_dim(set);
8169 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8170 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8171 map = isl_map_intersect_domain(map, set);
8176 /* Extend the given dim_map with mappings for the divs in bmap.
8178 static __isl_give struct isl_dim_map *extend_dim_map(
8179 __isl_keep struct isl_dim_map *dim_map,
8180 __isl_keep isl_basic_map *bmap)
8183 struct isl_dim_map *res;
8186 offset = isl_basic_map_offset(bmap, isl_dim_div);
8188 res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
8192 for (i = 0; i < dim_map->len; ++i)
8193 res->pos[i] = dim_map->pos[i];
8194 for (i = 0; i < bmap->n_div; ++i)
8195 res->pos[dim_map->len + i] = offset + i;
8200 /* Extract a dim_map from a reordering.
8201 * We essentially need to reverse the mapping, and add an offset
8202 * of 1 for the constant term.
8204 __isl_give struct isl_dim_map *isl_dim_map_from_reordering(
8205 __isl_keep isl_reordering *exp)
8208 struct isl_dim_map *dim_map;
8213 dim_map = isl_dim_map_alloc(exp->dim->ctx, isl_dim_total(exp->dim));
8217 for (i = 0; i < exp->len; ++i)
8218 dim_map->pos[1 + exp->pos[i]] = 1 + i;
8223 /* Reorder the dimensions of "bmap" according to the given dim_map
8224 * and set the dimension specification to "dim".
8226 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8227 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8231 bmap = isl_basic_map_cow(bmap);
8232 if (!bmap || !dim || !dim_map)
8235 res = isl_basic_map_alloc_dim(dim,
8236 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8237 res = add_constraints_dim_map(res, bmap, dim_map);
8238 res = isl_basic_map_finalize(res);
8242 isl_basic_map_free(bmap);
8247 /* Reorder the dimensions of "map" according to given reordering.
8249 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8250 __isl_take isl_reordering *r)
8253 struct isl_dim_map *dim_map;
8255 map = isl_map_cow(map);
8256 dim_map = isl_dim_map_from_reordering(r);
8257 if (!map || !r || !dim_map)
8260 for (i = 0; i < map->n; ++i) {
8261 struct isl_dim_map *dim_map_i;
8263 dim_map_i = extend_dim_map(dim_map, map->p[i]);
8265 map->p[i] = isl_basic_map_realign(map->p[i],
8266 isl_dim_copy(r->dim), dim_map_i);
8272 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8274 isl_reordering_free(r);
8280 isl_reordering_free(r);
8284 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8285 __isl_take isl_reordering *r)
8287 return (isl_set *)isl_map_realign((isl_map *)set, r);
8290 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8291 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8292 enum isl_dim_type c2, enum isl_dim_type c3,
8293 enum isl_dim_type c4, enum isl_dim_type c5)
8295 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8296 struct isl_mat *mat;
8302 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8303 isl_basic_map_total_dim(bmap) + 1);
8306 for (i = 0; i < bmap->n_eq; ++i)
8307 for (j = 0, pos = 0; j < 5; ++j) {
8308 int off = isl_basic_map_offset(bmap, c[j]);
8309 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8310 isl_int_set(mat->row[i][pos],
8311 bmap->eq[i][off + k]);
8319 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8320 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8321 enum isl_dim_type c2, enum isl_dim_type c3,
8322 enum isl_dim_type c4, enum isl_dim_type c5)
8324 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8325 struct isl_mat *mat;
8331 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8332 isl_basic_map_total_dim(bmap) + 1);
8335 for (i = 0; i < bmap->n_ineq; ++i)
8336 for (j = 0, pos = 0; j < 5; ++j) {
8337 int off = isl_basic_map_offset(bmap, c[j]);
8338 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8339 isl_int_set(mat->row[i][pos],
8340 bmap->ineq[i][off + k]);
8348 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8349 __isl_take isl_dim *dim,
8350 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8351 enum isl_dim_type c2, enum isl_dim_type c3,
8352 enum isl_dim_type c4, enum isl_dim_type c5)
8354 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8355 isl_basic_map *bmap;
8361 if (!dim || !eq || !ineq)
8364 if (eq->n_col != ineq->n_col)
8365 isl_die(dim->ctx, isl_error_invalid,
8366 "equalities and inequalities matrices should have "
8367 "same number of columns", goto error);
8369 total = 1 + isl_dim_total(dim);
8371 if (eq->n_col < total)
8372 isl_die(dim->ctx, isl_error_invalid,
8373 "number of columns too small", goto error);
8375 extra = eq->n_col - total;
8377 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8378 eq->n_row, ineq->n_row);
8381 for (i = 0; i < extra; ++i) {
8382 k = isl_basic_map_alloc_div(bmap);
8385 isl_int_set_si(bmap->div[k][0], 0);
8387 for (i = 0; i < eq->n_row; ++i) {
8388 l = isl_basic_map_alloc_equality(bmap);
8391 for (j = 0, pos = 0; j < 5; ++j) {
8392 int off = isl_basic_map_offset(bmap, c[j]);
8393 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8394 isl_int_set(bmap->eq[l][off + k],
8400 for (i = 0; i < ineq->n_row; ++i) {
8401 l = isl_basic_map_alloc_inequality(bmap);
8404 for (j = 0, pos = 0; j < 5; ++j) {
8405 int off = isl_basic_map_offset(bmap, c[j]);
8406 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8407 isl_int_set(bmap->ineq[l][off + k],
8426 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8427 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8428 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8430 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8431 c1, c2, c3, c4, isl_dim_in);
8434 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8435 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8436 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8438 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8439 c1, c2, c3, c4, isl_dim_in);
8442 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8443 __isl_take isl_dim *dim,
8444 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8445 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8447 return (isl_basic_set*)
8448 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8449 c1, c2, c3, c4, isl_dim_in);
8452 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8457 return isl_dim_can_zip(bmap->dim);
8460 int isl_map_can_zip(__isl_keep isl_map *map)
8465 return isl_dim_can_zip(map->dim);
8468 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8469 * (A -> C) -> (B -> D).
8471 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8480 if (!isl_basic_map_can_zip(bmap))
8481 isl_die(bmap->ctx, isl_error_invalid,
8482 "basic map cannot be zipped", goto error);
8483 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8484 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8485 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8486 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8487 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8490 bmap->dim = isl_dim_zip(bmap->dim);
8495 isl_basic_map_free(bmap);
8499 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8500 * (A -> C) -> (B -> D).
8502 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8509 if (!isl_map_can_zip(map))
8510 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8513 map = isl_map_cow(map);
8517 for (i = 0; i < map->n; ++i) {
8518 map->p[i] = isl_basic_map_zip(map->p[i]);
8523 map->dim = isl_dim_zip(map->dim);