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 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1471 map = isl_map_cow(map);
1475 for (i = 0; i < map->n; ++i) {
1476 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1486 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1488 return isl_map_remove_divs(set);
1491 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1492 enum isl_dim_type type, unsigned first, unsigned n)
1496 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1500 bmap = isl_basic_map_eliminate_vars(bmap,
1501 isl_basic_map_offset(bmap, type) - 1 + first, n);
1504 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1506 bmap = isl_basic_map_drop(bmap, type, first, n);
1509 isl_basic_map_free(bmap);
1513 /* Return true if the definition of the given div (recursively) involves
1514 * any of the given variables.
1516 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1517 unsigned first, unsigned n)
1520 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1522 if (isl_int_is_zero(bmap->div[div][0]))
1524 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1527 for (i = bmap->n_div - 1; i >= 0; --i) {
1528 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1530 if (div_involves_vars(bmap, i, first, n))
1537 /* Remove all divs (recursively) involving any of the given dimensions
1538 * in their definitions.
1540 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1541 __isl_take isl_basic_map *bmap,
1542 enum isl_dim_type type, unsigned first, unsigned n)
1548 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1550 first += isl_basic_map_offset(bmap, type);
1552 for (i = bmap->n_div - 1; i >= 0; --i) {
1553 if (!div_involves_vars(bmap, i, first, n))
1555 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1560 isl_basic_map_free(bmap);
1564 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1565 enum isl_dim_type type, unsigned first, unsigned n)
1574 map = isl_map_cow(map);
1578 for (i = 0; i < map->n; ++i) {
1579 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1590 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1591 enum isl_dim_type type, unsigned first, unsigned n)
1593 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1597 /* Return true if the definition of the given div is unknown or depends
1600 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1603 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1605 if (isl_int_is_zero(bmap->div[div][0]))
1608 for (i = bmap->n_div - 1; i >= 0; --i) {
1609 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1611 if (div_is_unknown(bmap, i))
1618 /* Remove all divs that are unknown or defined in terms of unknown divs.
1620 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1621 __isl_take isl_basic_map *bmap)
1628 for (i = bmap->n_div - 1; i >= 0; --i) {
1629 if (!div_is_unknown(bmap, i))
1631 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1637 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1646 map = isl_map_cow(map);
1650 for (i = 0; i < map->n; ++i) {
1651 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1661 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1663 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1666 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1667 __isl_take isl_basic_set *bset,
1668 enum isl_dim_type type, unsigned first, unsigned n)
1670 return (isl_basic_set *)
1671 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1674 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1675 enum isl_dim_type type, unsigned first, unsigned n)
1682 map = isl_map_cow(map);
1685 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1687 for (i = 0; i < map->n; ++i) {
1688 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1689 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1693 map = isl_map_drop(map, type, first, n);
1700 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1701 enum isl_dim_type type, unsigned first, unsigned n)
1703 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1706 /* Project out n inputs starting at first using Fourier-Motzkin */
1707 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1708 unsigned first, unsigned n)
1710 return isl_map_remove_dims(map, isl_dim_in, first, n);
1713 static void dump_term(struct isl_basic_map *bmap,
1714 isl_int c, int pos, FILE *out)
1717 unsigned in = isl_basic_map_n_in(bmap);
1718 unsigned dim = in + isl_basic_map_n_out(bmap);
1719 unsigned nparam = isl_basic_map_n_param(bmap);
1721 isl_int_print(out, c, 0);
1723 if (!isl_int_is_one(c))
1724 isl_int_print(out, c, 0);
1725 if (pos < 1 + nparam) {
1726 name = isl_dim_get_name(bmap->dim,
1727 isl_dim_param, pos - 1);
1729 fprintf(out, "%s", name);
1731 fprintf(out, "p%d", pos - 1);
1732 } else if (pos < 1 + nparam + in)
1733 fprintf(out, "i%d", pos - 1 - nparam);
1734 else if (pos < 1 + nparam + dim)
1735 fprintf(out, "o%d", pos - 1 - nparam - in);
1737 fprintf(out, "e%d", pos - 1 - nparam - dim);
1741 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1742 int sign, FILE *out)
1746 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1750 for (i = 0, first = 1; i < len; ++i) {
1751 if (isl_int_sgn(c[i]) * sign <= 0)
1754 fprintf(out, " + ");
1756 isl_int_abs(v, c[i]);
1757 dump_term(bmap, v, i, out);
1764 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1765 const char *op, FILE *out, int indent)
1769 fprintf(out, "%*s", indent, "");
1771 dump_constraint_sign(bmap, c, 1, out);
1772 fprintf(out, " %s ", op);
1773 dump_constraint_sign(bmap, c, -1, out);
1777 for (i = bmap->n_div; i < bmap->extra; ++i) {
1778 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1780 fprintf(out, "%*s", indent, "");
1781 fprintf(out, "ERROR: unused div coefficient not zero\n");
1786 static void dump_constraints(struct isl_basic_map *bmap,
1787 isl_int **c, unsigned n,
1788 const char *op, FILE *out, int indent)
1792 for (i = 0; i < n; ++i)
1793 dump_constraint(bmap, c[i], op, out, indent);
1796 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1800 unsigned total = isl_basic_map_total_dim(bmap);
1802 for (j = 0; j < 1 + total; ++j) {
1803 if (isl_int_is_zero(exp[j]))
1805 if (!first && isl_int_is_pos(exp[j]))
1807 dump_term(bmap, exp[j], j, out);
1812 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1816 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1817 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1819 for (i = 0; i < bmap->n_div; ++i) {
1820 fprintf(out, "%*s", indent, "");
1821 fprintf(out, "e%d = [(", i);
1822 dump_affine(bmap, bmap->div[i]+1, out);
1824 isl_int_print(out, bmap->div[i][0], 0);
1825 fprintf(out, "]\n");
1829 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1830 FILE *out, int indent)
1833 fprintf(out, "null basic set\n");
1837 fprintf(out, "%*s", indent, "");
1838 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1839 bset->ref, bset->dim->nparam, bset->dim->n_out,
1840 bset->extra, bset->flags);
1841 dump((struct isl_basic_map *)bset, out, indent);
1844 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1845 FILE *out, int indent)
1848 fprintf(out, "null basic map\n");
1852 fprintf(out, "%*s", indent, "");
1853 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1854 "flags: %x, n_name: %d\n",
1856 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1857 bmap->extra, bmap->flags, bmap->dim->n_name);
1858 dump(bmap, out, indent);
1861 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1866 total = isl_basic_map_total_dim(bmap);
1867 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1868 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1869 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1870 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1874 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1876 struct isl_set *set;
1880 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1881 isl_assert(dim->ctx, n >= 0, goto error);
1882 set = isl_alloc(dim->ctx, struct isl_set,
1883 sizeof(struct isl_set) +
1884 (n - 1) * sizeof(struct isl_basic_set *));
1888 set->ctx = dim->ctx;
1889 isl_ctx_ref(set->ctx);
1901 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1902 unsigned nparam, unsigned dim, int n, unsigned flags)
1904 struct isl_set *set;
1905 struct isl_dim *dims;
1907 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1911 set = isl_set_alloc_dim(dims, n, flags);
1915 /* Make sure "map" has room for at least "n" more basic maps.
1917 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1920 struct isl_map *grown = NULL;
1924 isl_assert(map->ctx, n >= 0, goto error);
1925 if (map->n + n <= map->size)
1927 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1930 for (i = 0; i < map->n; ++i) {
1931 grown->p[i] = isl_basic_map_copy(map->p[i]);
1939 isl_map_free(grown);
1944 /* Make sure "set" has room for at least "n" more basic sets.
1946 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1948 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1951 struct isl_set *isl_set_dup(struct isl_set *set)
1954 struct isl_set *dup;
1959 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1962 for (i = 0; i < set->n; ++i)
1963 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1967 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1969 return isl_map_from_basic_map(bset);
1972 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1974 struct isl_map *map;
1979 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1980 return isl_map_add_basic_map(map, bmap);
1983 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1984 __isl_take isl_basic_set *bset)
1986 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1987 (struct isl_basic_map *)bset);
1990 void isl_set_free(struct isl_set *set)
2000 isl_ctx_deref(set->ctx);
2001 for (i = 0; i < set->n; ++i)
2002 isl_basic_set_free(set->p[i]);
2003 isl_dim_free(set->dim);
2007 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2012 fprintf(out, "null set\n");
2016 fprintf(out, "%*s", indent, "");
2017 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2018 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2020 for (i = 0; i < set->n; ++i) {
2021 fprintf(out, "%*s", indent, "");
2022 fprintf(out, "basic set %d:\n", i);
2023 isl_basic_set_print_internal(set->p[i], out, indent+4);
2027 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2032 fprintf(out, "null map\n");
2036 fprintf(out, "%*s", indent, "");
2037 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2038 "flags: %x, n_name: %d\n",
2039 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2040 map->dim->n_out, map->flags, map->dim->n_name);
2041 for (i = 0; i < map->n; ++i) {
2042 fprintf(out, "%*s", indent, "");
2043 fprintf(out, "basic map %d:\n", i);
2044 isl_basic_map_print_internal(map->p[i], out, indent+4);
2048 struct isl_basic_map *isl_basic_map_intersect_domain(
2049 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2051 struct isl_basic_map *bmap_domain;
2056 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2057 bset->dim, isl_dim_param), goto error);
2059 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2060 isl_assert(bset->ctx,
2061 isl_basic_map_compatible_domain(bmap, bset), goto error);
2063 bmap = isl_basic_map_cow(bmap);
2066 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2067 bset->n_div, bset->n_eq, bset->n_ineq);
2068 bmap_domain = isl_basic_map_from_domain(bset);
2069 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2071 bmap = isl_basic_map_simplify(bmap);
2072 return isl_basic_map_finalize(bmap);
2074 isl_basic_map_free(bmap);
2075 isl_basic_set_free(bset);
2079 struct isl_basic_map *isl_basic_map_intersect_range(
2080 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2082 struct isl_basic_map *bmap_range;
2087 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2088 bset->dim, isl_dim_param), goto error);
2090 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2091 isl_assert(bset->ctx,
2092 isl_basic_map_compatible_range(bmap, bset), goto error);
2094 bmap = isl_basic_map_cow(bmap);
2097 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2098 bset->n_div, bset->n_eq, bset->n_ineq);
2099 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2100 bmap = add_constraints(bmap, bmap_range, 0, 0);
2102 bmap = isl_basic_map_simplify(bmap);
2103 return isl_basic_map_finalize(bmap);
2105 isl_basic_map_free(bmap);
2106 isl_basic_set_free(bset);
2110 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2116 total = 1 + isl_basic_map_total_dim(bmap);
2117 if (total != vec->size)
2122 for (i = 0; i < bmap->n_eq; ++i) {
2123 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2124 if (!isl_int_is_zero(s)) {
2130 for (i = 0; i < bmap->n_ineq; ++i) {
2131 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2132 if (isl_int_is_neg(s)) {
2143 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2145 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2148 struct isl_basic_map *isl_basic_map_intersect(
2149 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2151 struct isl_vec *sample = NULL;
2153 if (!bmap1 || !bmap2)
2156 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2157 bmap2->dim, isl_dim_param), goto error);
2158 if (isl_dim_total(bmap1->dim) ==
2159 isl_dim_size(bmap1->dim, isl_dim_param) &&
2160 isl_dim_total(bmap2->dim) !=
2161 isl_dim_size(bmap2->dim, isl_dim_param))
2162 return isl_basic_map_intersect(bmap2, bmap1);
2164 if (isl_dim_total(bmap2->dim) !=
2165 isl_dim_size(bmap2->dim, isl_dim_param))
2166 isl_assert(bmap1->ctx,
2167 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2169 if (bmap1->sample &&
2170 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2171 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2172 sample = isl_vec_copy(bmap1->sample);
2173 else if (bmap2->sample &&
2174 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2175 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2176 sample = isl_vec_copy(bmap2->sample);
2178 bmap1 = isl_basic_map_cow(bmap1);
2181 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2182 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2183 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2186 isl_vec_free(sample);
2188 isl_vec_free(bmap1->sample);
2189 bmap1->sample = sample;
2192 bmap1 = isl_basic_map_simplify(bmap1);
2193 return isl_basic_map_finalize(bmap1);
2196 isl_vec_free(sample);
2197 isl_basic_map_free(bmap1);
2198 isl_basic_map_free(bmap2);
2202 struct isl_basic_set *isl_basic_set_intersect(
2203 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2205 return (struct isl_basic_set *)
2206 isl_basic_map_intersect(
2207 (struct isl_basic_map *)bset1,
2208 (struct isl_basic_map *)bset2);
2211 /* Special case of isl_map_intersect, where both map1 and map2
2212 * are convex, without any divs and such that either map1 or map2
2213 * contains a single constraint. This constraint is then simply
2214 * added to the other map.
2216 static __isl_give isl_map *map_intersect_add_constraint(
2217 __isl_take isl_map *map1, __isl_take isl_map *map2)
2219 struct isl_basic_map *bmap1;
2220 struct isl_basic_map *bmap2;
2222 isl_assert(map1->ctx, map1->n == 1, goto error);
2223 isl_assert(map2->ctx, map1->n == 1, goto error);
2224 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2225 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2227 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2228 return isl_map_intersect(map2, map1);
2230 isl_assert(map2->ctx,
2231 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2233 map1 = isl_map_cow(map1);
2236 if (isl_map_fast_is_empty(map1)) {
2240 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2241 if (map2->p[0]->n_eq == 1)
2242 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2244 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2245 map2->p[0]->ineq[0]);
2247 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2248 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2252 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2253 isl_basic_map_free(map1->p[0]);
2266 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2269 struct isl_map *result;
2275 if (isl_map_fast_is_empty(map1)) {
2279 if (isl_map_fast_is_empty(map2)) {
2284 if (map1->n == 1 && map2->n == 1 &&
2285 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2286 isl_dim_equal(map1->dim, map2->dim) &&
2287 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2288 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2289 return map_intersect_add_constraint(map1, map2);
2290 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2291 map2->dim, isl_dim_param), goto error);
2292 if (isl_dim_total(map1->dim) ==
2293 isl_dim_size(map1->dim, isl_dim_param) &&
2294 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2295 return isl_map_intersect(map2, map1);
2297 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2298 isl_assert(map1->ctx,
2299 isl_dim_equal(map1->dim, map2->dim), goto error);
2301 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2302 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2303 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2305 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2306 map1->n * map2->n, flags);
2309 for (i = 0; i < map1->n; ++i)
2310 for (j = 0; j < map2->n; ++j) {
2311 struct isl_basic_map *part;
2312 part = isl_basic_map_intersect(
2313 isl_basic_map_copy(map1->p[i]),
2314 isl_basic_map_copy(map2->p[j]));
2315 if (isl_basic_map_is_empty(part))
2316 isl_basic_map_free(part);
2318 result = isl_map_add_basic_map(result, part);
2331 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2333 return (struct isl_set *)
2334 isl_map_intersect((struct isl_map *)set1,
2335 (struct isl_map *)set2);
2338 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2340 struct isl_dim *dim;
2341 struct isl_basic_set *bset;
2346 bmap = isl_basic_map_cow(bmap);
2349 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2350 in = isl_basic_map_n_in(bmap);
2351 bset = isl_basic_set_from_basic_map(bmap);
2352 bset = isl_basic_set_swap_vars(bset, in);
2353 return isl_basic_map_from_basic_set(bset, dim);
2356 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2357 enum isl_dim_type type, unsigned pos, unsigned n)
2359 struct isl_dim *res_dim;
2360 struct isl_basic_map *res;
2361 struct isl_dim_map *dim_map;
2362 unsigned total, off;
2363 enum isl_dim_type t;
2371 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2373 total = isl_basic_map_total_dim(bmap) + n;
2374 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2376 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2378 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2380 unsigned size = isl_basic_map_dim(bmap, t);
2381 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2383 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2384 pos, size - pos, off + pos + n);
2386 off += isl_dim_size(res_dim, t);
2388 isl_dim_map_div(dim_map, bmap, off);
2390 res = isl_basic_map_alloc_dim(res_dim,
2391 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2392 if (isl_basic_map_is_rational(bmap))
2393 res = isl_basic_map_set_rational(res);
2394 res = add_constraints_dim_map(res, bmap, dim_map);
2395 return isl_basic_map_finalize(res);
2398 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2399 enum isl_dim_type type, unsigned n)
2403 return isl_basic_map_insert(bmap, type,
2404 isl_basic_map_dim(bmap, type), n);
2407 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2408 enum isl_dim_type type, unsigned n)
2412 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2413 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2415 isl_basic_set_free(bset);
2419 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2420 enum isl_dim_type type, unsigned pos, unsigned n)
2427 map = isl_map_cow(map);
2431 map->dim = isl_dim_insert(map->dim, type, pos, n);
2435 for (i = 0; i < map->n; ++i) {
2436 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2447 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2448 enum isl_dim_type type, unsigned pos, unsigned n)
2450 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2453 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2454 enum isl_dim_type type, unsigned n)
2458 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2461 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2462 enum isl_dim_type type, unsigned n)
2466 isl_assert(set->ctx, type != isl_dim_in, goto error);
2467 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2473 __isl_give isl_basic_map *isl_basic_map_move_dims(
2474 __isl_take isl_basic_map *bmap,
2475 enum isl_dim_type dst_type, unsigned dst_pos,
2476 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2479 struct isl_dim_map *dim_map;
2480 struct isl_basic_map *res;
2481 enum isl_dim_type t;
2482 unsigned total, off;
2489 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2492 if (dst_type == src_type && dst_pos == src_pos)
2495 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2497 if (pos(bmap->dim, dst_type) + dst_pos ==
2498 pos(bmap->dim, src_type) + src_pos +
2499 ((src_type < dst_type) ? n : 0)) {
2500 bmap = isl_basic_map_cow(bmap);
2504 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2505 src_type, src_pos, n);
2509 bmap = isl_basic_map_finalize(bmap);
2514 total = isl_basic_map_total_dim(bmap);
2515 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2518 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2519 unsigned size = isl_dim_size(bmap->dim, t);
2520 if (t == dst_type) {
2521 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2524 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2527 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2528 dst_pos, size - dst_pos, off);
2529 off += size - dst_pos;
2530 } else if (t == src_type) {
2531 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2534 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2535 src_pos + n, size - src_pos - n, off);
2536 off += size - src_pos - n;
2538 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2542 isl_dim_map_div(dim_map, bmap, off);
2544 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2545 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2546 bmap = add_constraints_dim_map(res, bmap, dim_map);
2548 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2549 src_type, src_pos, n);
2553 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2554 bmap = isl_basic_map_gauss(bmap, NULL);
2555 bmap = isl_basic_map_finalize(bmap);
2559 isl_basic_map_free(bmap);
2563 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2564 enum isl_dim_type dst_type, unsigned dst_pos,
2565 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2567 return (isl_basic_set *)isl_basic_map_move_dims(
2568 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2571 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2572 enum isl_dim_type dst_type, unsigned dst_pos,
2573 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2577 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2578 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2579 src_type, src_pos, n);
2585 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2586 enum isl_dim_type dst_type, unsigned dst_pos,
2587 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2596 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2599 if (dst_type == src_type && dst_pos == src_pos)
2602 isl_assert(map->ctx, dst_type != src_type, goto error);
2604 map = isl_map_cow(map);
2608 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2612 for (i = 0; i < map->n; ++i) {
2613 map->p[i] = isl_basic_map_move_dims(map->p[i],
2615 src_type, src_pos, n);
2626 /* Move the specified dimensions to the last columns right before
2627 * the divs. Don't change the dimension specification of bmap.
2628 * That's the responsibility of the caller.
2630 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2631 enum isl_dim_type type, unsigned first, unsigned n)
2633 struct isl_dim_map *dim_map;
2634 struct isl_basic_map *res;
2635 enum isl_dim_type t;
2636 unsigned total, off;
2640 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2643 total = isl_basic_map_total_dim(bmap);
2644 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2647 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2648 unsigned size = isl_dim_size(bmap->dim, t);
2650 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2653 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2654 first, n, total - bmap->n_div - n);
2655 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2656 first + n, size - (first + n), off);
2657 off += size - (first + n);
2659 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2663 isl_dim_map_div(dim_map, bmap, off + n);
2665 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2666 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2667 res = add_constraints_dim_map(res, bmap, dim_map);
2671 /* Turn the n dimensions of type type, starting at first
2672 * into existentially quantified variables.
2674 __isl_give isl_basic_map *isl_basic_map_project_out(
2675 __isl_take isl_basic_map *bmap,
2676 enum isl_dim_type type, unsigned first, unsigned n)
2689 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2690 return isl_basic_map_remove_dims(bmap, type, first, n);
2692 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2695 bmap = move_last(bmap, type, first, n);
2696 bmap = isl_basic_map_cow(bmap);
2700 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2701 old = bmap->block2.data;
2702 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2703 (bmap->extra + n) * (1 + row_size));
2704 if (!bmap->block2.data)
2706 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2709 for (i = 0; i < n; ++i) {
2710 new_div[i] = bmap->block2.data +
2711 (bmap->extra + i) * (1 + row_size);
2712 isl_seq_clr(new_div[i], 1 + row_size);
2714 for (i = 0; i < bmap->extra; ++i)
2715 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2717 bmap->div = new_div;
2721 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2724 bmap = isl_basic_map_simplify(bmap);
2725 bmap = isl_basic_map_drop_redundant_divs(bmap);
2726 return isl_basic_map_finalize(bmap);
2728 isl_basic_map_free(bmap);
2732 /* Turn the n dimensions of type type, starting at first
2733 * into existentially quantified variables.
2735 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2736 enum isl_dim_type type, unsigned first, unsigned n)
2738 return (isl_basic_set *)isl_basic_map_project_out(
2739 (isl_basic_map *)bset, type, first, n);
2742 /* Turn the n dimensions of type type, starting at first
2743 * into existentially quantified variables.
2745 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2746 enum isl_dim_type type, unsigned first, unsigned n)
2756 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2758 map = isl_map_cow(map);
2762 map->dim = isl_dim_drop(map->dim, type, first, n);
2766 for (i = 0; i < map->n; ++i) {
2767 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2778 /* Turn the n dimensions of type type, starting at first
2779 * into existentially quantified variables.
2781 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2782 enum isl_dim_type type, unsigned first, unsigned n)
2784 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2787 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2791 for (i = 0; i < n; ++i) {
2792 j = isl_basic_map_alloc_div(bmap);
2795 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2799 isl_basic_map_free(bmap);
2803 struct isl_basic_map *isl_basic_map_apply_range(
2804 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2806 struct isl_dim *dim_result = NULL;
2807 struct isl_basic_map *bmap;
2808 unsigned n_in, n_out, n, nparam, total, pos;
2809 struct isl_dim_map *dim_map1, *dim_map2;
2811 if (!bmap1 || !bmap2)
2814 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2815 isl_dim_copy(bmap2->dim));
2817 n_in = isl_basic_map_n_in(bmap1);
2818 n_out = isl_basic_map_n_out(bmap2);
2819 n = isl_basic_map_n_out(bmap1);
2820 nparam = isl_basic_map_n_param(bmap1);
2822 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2823 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2824 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2825 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2826 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2827 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2828 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2829 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2830 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2831 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2832 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2834 bmap = isl_basic_map_alloc_dim(dim_result,
2835 bmap1->n_div + bmap2->n_div + n,
2836 bmap1->n_eq + bmap2->n_eq,
2837 bmap1->n_ineq + bmap2->n_ineq);
2838 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2839 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2840 bmap = add_divs(bmap, n);
2841 bmap = isl_basic_map_simplify(bmap);
2842 bmap = isl_basic_map_drop_redundant_divs(bmap);
2843 return isl_basic_map_finalize(bmap);
2845 isl_basic_map_free(bmap1);
2846 isl_basic_map_free(bmap2);
2850 struct isl_basic_set *isl_basic_set_apply(
2851 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2856 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2859 return (struct isl_basic_set *)
2860 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2862 isl_basic_set_free(bset);
2863 isl_basic_map_free(bmap);
2867 struct isl_basic_map *isl_basic_map_apply_domain(
2868 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2870 if (!bmap1 || !bmap2)
2873 isl_assert(bmap1->ctx,
2874 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2875 isl_assert(bmap1->ctx,
2876 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2879 bmap1 = isl_basic_map_reverse(bmap1);
2880 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2881 return isl_basic_map_reverse(bmap1);
2883 isl_basic_map_free(bmap1);
2884 isl_basic_map_free(bmap2);
2888 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2889 * A \cap B -> f(A) + f(B)
2891 struct isl_basic_map *isl_basic_map_sum(
2892 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2894 unsigned n_in, n_out, nparam, total, pos;
2895 struct isl_basic_map *bmap = NULL;
2896 struct isl_dim_map *dim_map1, *dim_map2;
2899 if (!bmap1 || !bmap2)
2902 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2905 nparam = isl_basic_map_n_param(bmap1);
2906 n_in = isl_basic_map_n_in(bmap1);
2907 n_out = isl_basic_map_n_out(bmap1);
2909 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2910 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2911 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2912 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2913 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2914 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2915 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2916 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2917 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2918 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2919 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2921 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2922 bmap1->n_div + bmap2->n_div + 2 * n_out,
2923 bmap1->n_eq + bmap2->n_eq + n_out,
2924 bmap1->n_ineq + bmap2->n_ineq);
2925 for (i = 0; i < n_out; ++i) {
2926 int j = isl_basic_map_alloc_equality(bmap);
2929 isl_seq_clr(bmap->eq[j], 1+total);
2930 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2931 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2932 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2934 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2935 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2936 bmap = add_divs(bmap, 2 * n_out);
2938 bmap = isl_basic_map_simplify(bmap);
2939 return isl_basic_map_finalize(bmap);
2941 isl_basic_map_free(bmap);
2942 isl_basic_map_free(bmap1);
2943 isl_basic_map_free(bmap2);
2947 /* Given two maps A -> f(A) and B -> g(B), construct a map
2948 * A \cap B -> f(A) + f(B)
2950 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2952 struct isl_map *result;
2958 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2960 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2961 map1->n * map2->n, 0);
2964 for (i = 0; i < map1->n; ++i)
2965 for (j = 0; j < map2->n; ++j) {
2966 struct isl_basic_map *part;
2967 part = isl_basic_map_sum(
2968 isl_basic_map_copy(map1->p[i]),
2969 isl_basic_map_copy(map2->p[j]));
2970 if (isl_basic_map_is_empty(part))
2971 isl_basic_map_free(part);
2973 result = isl_map_add_basic_map(result, part);
2986 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2987 __isl_take isl_set *set2)
2989 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2992 /* Given a basic map A -> f(A), construct A -> -f(A).
2994 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2999 bmap = isl_basic_map_cow(bmap);
3003 n = isl_basic_map_dim(bmap, isl_dim_out);
3004 off = isl_basic_map_offset(bmap, isl_dim_out);
3005 for (i = 0; i < bmap->n_eq; ++i)
3006 for (j = 0; j < n; ++j)
3007 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3008 for (i = 0; i < bmap->n_ineq; ++i)
3009 for (j = 0; j < n; ++j)
3010 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3011 for (i = 0; i < bmap->n_div; ++i)
3012 for (j = 0; j < n; ++j)
3013 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3014 return isl_basic_map_finalize(bmap);
3017 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3019 return isl_basic_map_neg(bset);
3022 /* Given a map A -> f(A), construct A -> -f(A).
3024 struct isl_map *isl_map_neg(struct isl_map *map)
3028 map = isl_map_cow(map);
3032 for (i = 0; i < map->n; ++i) {
3033 map->p[i] = isl_basic_map_neg(map->p[i]);
3044 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3046 return (isl_set *)isl_map_neg((isl_map *)set);
3049 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3050 * A -> floor(f(A)/d).
3052 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3055 unsigned n_in, n_out, nparam, total, pos;
3056 struct isl_basic_map *result = NULL;
3057 struct isl_dim_map *dim_map;
3063 nparam = isl_basic_map_n_param(bmap);
3064 n_in = isl_basic_map_n_in(bmap);
3065 n_out = isl_basic_map_n_out(bmap);
3067 total = nparam + n_in + n_out + bmap->n_div + n_out;
3068 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3069 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3070 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3071 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3072 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3074 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3075 bmap->n_div + n_out,
3076 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3077 result = add_constraints_dim_map(result, bmap, dim_map);
3078 result = add_divs(result, n_out);
3079 for (i = 0; i < n_out; ++i) {
3081 j = isl_basic_map_alloc_inequality(result);
3084 isl_seq_clr(result->ineq[j], 1+total);
3085 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3086 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3087 j = isl_basic_map_alloc_inequality(result);
3090 isl_seq_clr(result->ineq[j], 1+total);
3091 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3092 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3093 isl_int_sub_ui(result->ineq[j][0], d, 1);
3096 result = isl_basic_map_simplify(result);
3097 return isl_basic_map_finalize(result);
3099 isl_basic_map_free(result);
3103 /* Given a map A -> f(A) and an integer d, construct a map
3104 * A -> floor(f(A)/d).
3106 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3110 map = isl_map_cow(map);
3114 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3115 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3116 for (i = 0; i < map->n; ++i) {
3117 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3128 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3134 i = isl_basic_map_alloc_equality(bmap);
3137 nparam = isl_basic_map_n_param(bmap);
3138 n_in = isl_basic_map_n_in(bmap);
3139 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3140 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3141 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3142 return isl_basic_map_finalize(bmap);
3144 isl_basic_map_free(bmap);
3148 /* Add a constraints to "bmap" expressing i_pos < o_pos
3150 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3156 i = isl_basic_map_alloc_inequality(bmap);
3159 nparam = isl_basic_map_n_param(bmap);
3160 n_in = isl_basic_map_n_in(bmap);
3161 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3162 isl_int_set_si(bmap->ineq[i][0], -1);
3163 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3164 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3165 return isl_basic_map_finalize(bmap);
3167 isl_basic_map_free(bmap);
3171 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3173 static __isl_give isl_basic_map *var_less_or_equal(
3174 __isl_take isl_basic_map *bmap, unsigned pos)
3180 i = isl_basic_map_alloc_inequality(bmap);
3183 nparam = isl_basic_map_n_param(bmap);
3184 n_in = isl_basic_map_n_in(bmap);
3185 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3186 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3187 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3188 return isl_basic_map_finalize(bmap);
3190 isl_basic_map_free(bmap);
3194 /* Add a constraints to "bmap" expressing i_pos > o_pos
3196 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3202 i = isl_basic_map_alloc_inequality(bmap);
3205 nparam = isl_basic_map_n_param(bmap);
3206 n_in = isl_basic_map_n_in(bmap);
3207 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3208 isl_int_set_si(bmap->ineq[i][0], -1);
3209 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3210 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3211 return isl_basic_map_finalize(bmap);
3213 isl_basic_map_free(bmap);
3217 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3219 static __isl_give isl_basic_map *var_more_or_equal(
3220 __isl_take isl_basic_map *bmap, unsigned pos)
3226 i = isl_basic_map_alloc_inequality(bmap);
3229 nparam = isl_basic_map_n_param(bmap);
3230 n_in = isl_basic_map_n_in(bmap);
3231 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3232 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3233 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3234 return isl_basic_map_finalize(bmap);
3236 isl_basic_map_free(bmap);
3240 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3243 struct isl_basic_map *bmap;
3244 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3247 for (i = 0; i < n_equal && bmap; ++i)
3248 bmap = var_equal(bmap, i);
3249 return isl_basic_map_finalize(bmap);
3252 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3254 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3257 struct isl_basic_map *bmap;
3258 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3261 for (i = 0; i < pos && bmap; ++i)
3262 bmap = var_equal(bmap, i);
3264 bmap = var_less(bmap, pos);
3265 return isl_basic_map_finalize(bmap);
3268 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3270 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3271 __isl_take isl_dim *dim, unsigned pos)
3274 isl_basic_map *bmap;
3276 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3277 for (i = 0; i < pos; ++i)
3278 bmap = var_equal(bmap, i);
3279 bmap = var_less_or_equal(bmap, pos);
3280 return isl_basic_map_finalize(bmap);
3283 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3285 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3288 struct isl_basic_map *bmap;
3289 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3292 for (i = 0; i < pos && bmap; ++i)
3293 bmap = var_equal(bmap, i);
3295 bmap = var_more(bmap, pos);
3296 return isl_basic_map_finalize(bmap);
3299 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3301 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3302 __isl_take isl_dim *dim, unsigned pos)
3305 isl_basic_map *bmap;
3307 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3308 for (i = 0; i < pos; ++i)
3309 bmap = var_equal(bmap, i);
3310 bmap = var_more_or_equal(bmap, pos);
3311 return isl_basic_map_finalize(bmap);
3314 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3315 unsigned n, int equal)
3317 struct isl_map *map;
3320 if (n == 0 && equal)
3321 return isl_map_universe(dims);
3323 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3325 for (i = 0; i + 1 < n; ++i)
3326 map = isl_map_add_basic_map(map,
3327 isl_basic_map_less_at(isl_dim_copy(dims), i));
3330 map = isl_map_add_basic_map(map,
3331 isl_basic_map_less_or_equal_at(dims, n - 1));
3333 map = isl_map_add_basic_map(map,
3334 isl_basic_map_less_at(dims, n - 1));
3341 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3345 return map_lex_lte_first(dims, dims->n_out, equal);
3348 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3350 return map_lex_lte_first(dim, n, 0);
3353 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3355 return map_lex_lte_first(dim, n, 1);
3358 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3360 return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3363 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3365 return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3368 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3369 unsigned n, int equal)
3371 struct isl_map *map;
3374 if (n == 0 && equal)
3375 return isl_map_universe(dims);
3377 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3379 for (i = 0; i + 1 < n; ++i)
3380 map = isl_map_add_basic_map(map,
3381 isl_basic_map_more_at(isl_dim_copy(dims), i));
3384 map = isl_map_add_basic_map(map,
3385 isl_basic_map_more_or_equal_at(dims, n - 1));
3387 map = isl_map_add_basic_map(map,
3388 isl_basic_map_more_at(dims, n - 1));
3395 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3399 return map_lex_gte_first(dims, dims->n_out, equal);
3402 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3404 return map_lex_gte_first(dim, n, 0);
3407 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3409 return map_lex_gte_first(dim, n, 1);
3412 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3414 return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3417 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3419 return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3422 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3423 __isl_take isl_set *set2)
3426 map = isl_map_lex_le(isl_set_get_dim(set1));
3427 map = isl_map_intersect_domain(map, set1);
3428 map = isl_map_intersect_range(map, set2);
3432 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3433 __isl_take isl_set *set2)
3436 map = isl_map_lex_lt(isl_set_get_dim(set1));
3437 map = isl_map_intersect_domain(map, set1);
3438 map = isl_map_intersect_range(map, set2);
3442 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3443 __isl_take isl_set *set2)
3446 map = isl_map_lex_ge(isl_set_get_dim(set1));
3447 map = isl_map_intersect_domain(map, set1);
3448 map = isl_map_intersect_range(map, set2);
3452 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3453 __isl_take isl_set *set2)
3456 map = isl_map_lex_gt(isl_set_get_dim(set1));
3457 map = isl_map_intersect_domain(map, set1);
3458 map = isl_map_intersect_range(map, set2);
3462 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3463 __isl_take isl_map *map2)
3466 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3467 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3468 map = isl_map_apply_range(map, isl_map_reverse(map2));
3472 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3473 __isl_take isl_map *map2)
3476 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3477 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3478 map = isl_map_apply_range(map, isl_map_reverse(map2));
3482 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3483 __isl_take isl_map *map2)
3486 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3487 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3488 map = isl_map_apply_range(map, isl_map_reverse(map2));
3492 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3493 __isl_take isl_map *map2)
3496 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3497 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3498 map = isl_map_apply_range(map, isl_map_reverse(map2));
3502 struct isl_basic_map *isl_basic_map_from_basic_set(
3503 struct isl_basic_set *bset, struct isl_dim *dim)
3505 struct isl_basic_map *bmap;
3507 bset = isl_basic_set_cow(bset);
3511 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3512 isl_dim_free(bset->dim);
3513 bmap = (struct isl_basic_map *) bset;
3515 return isl_basic_map_finalize(bmap);
3517 isl_basic_set_free(bset);
3522 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3526 if (bmap->dim->n_in == 0)
3527 return (struct isl_basic_set *)bmap;
3528 bmap = isl_basic_map_cow(bmap);
3531 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3534 bmap = isl_basic_map_finalize(bmap);
3535 return (struct isl_basic_set *)bmap;
3537 isl_basic_map_free(bmap);
3541 /* For a div d = floor(f/m), add the constraints
3544 * -(f-(n-1)) + m d >= 0
3546 * Note that the second constraint is the negation of
3550 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3551 unsigned pos, isl_int *div)
3554 unsigned total = isl_basic_map_total_dim(bmap);
3556 i = isl_basic_map_alloc_inequality(bmap);
3559 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3560 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3562 j = isl_basic_map_alloc_inequality(bmap);
3565 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3566 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3567 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3571 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3572 unsigned pos, isl_int *div)
3574 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3578 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3580 unsigned total = isl_basic_map_total_dim(bmap);
3581 unsigned div_pos = total - bmap->n_div + div;
3583 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3587 struct isl_basic_set *isl_basic_map_underlying_set(
3588 struct isl_basic_map *bmap)
3592 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3594 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3595 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3596 return (struct isl_basic_set *)bmap;
3597 bmap = isl_basic_map_cow(bmap);
3600 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3603 bmap->extra -= bmap->n_div;
3605 bmap = isl_basic_map_finalize(bmap);
3606 return (struct isl_basic_set *)bmap;
3611 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3612 __isl_take isl_basic_set *bset)
3614 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3617 struct isl_basic_map *isl_basic_map_overlying_set(
3618 struct isl_basic_set *bset, struct isl_basic_map *like)
3620 struct isl_basic_map *bmap;
3621 struct isl_ctx *ctx;
3628 isl_assert(ctx, bset->n_div == 0, goto error);
3629 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3630 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3632 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3633 isl_basic_map_free(like);
3634 return (struct isl_basic_map *)bset;
3636 bset = isl_basic_set_cow(bset);
3639 total = bset->dim->n_out + bset->extra;
3640 bmap = (struct isl_basic_map *)bset;
3641 isl_dim_free(bmap->dim);
3642 bmap->dim = isl_dim_copy(like->dim);
3645 bmap->n_div = like->n_div;
3646 bmap->extra += like->n_div;
3650 ltotal = total - bmap->extra + like->extra;
3653 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3654 bmap->extra * (1 + 1 + total));
3655 if (isl_blk_is_error(bmap->block2))
3657 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3661 for (i = 0; i < bmap->extra; ++i)
3662 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3663 for (i = 0; i < like->n_div; ++i) {
3664 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3665 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3667 bmap = isl_basic_map_extend_constraints(bmap,
3668 0, 2 * like->n_div);
3669 for (i = 0; i < like->n_div; ++i) {
3670 if (isl_int_is_zero(bmap->div[i][0]))
3672 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3676 isl_basic_map_free(like);
3677 bmap = isl_basic_map_simplify(bmap);
3678 bmap = isl_basic_map_finalize(bmap);
3681 isl_basic_map_free(like);
3682 isl_basic_set_free(bset);
3686 struct isl_basic_set *isl_basic_set_from_underlying_set(
3687 struct isl_basic_set *bset, struct isl_basic_set *like)
3689 return (struct isl_basic_set *)
3690 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3693 struct isl_set *isl_set_from_underlying_set(
3694 struct isl_set *set, struct isl_basic_set *like)
3700 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3702 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3703 isl_basic_set_free(like);
3706 set = isl_set_cow(set);
3709 for (i = 0; i < set->n; ++i) {
3710 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3711 isl_basic_set_copy(like));
3715 isl_dim_free(set->dim);
3716 set->dim = isl_dim_copy(like->dim);
3719 isl_basic_set_free(like);
3722 isl_basic_set_free(like);
3727 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3731 map = isl_map_cow(map);
3734 map->dim = isl_dim_cow(map->dim);
3738 for (i = 1; i < map->n; ++i)
3739 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3741 for (i = 0; i < map->n; ++i) {
3742 map->p[i] = (struct isl_basic_map *)
3743 isl_basic_map_underlying_set(map->p[i]);
3748 map->dim = isl_dim_underlying(map->dim, 0);
3750 isl_dim_free(map->dim);
3751 map->dim = isl_dim_copy(map->p[0]->dim);
3755 return (struct isl_set *)map;
3761 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3763 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3766 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3767 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3769 bmap = isl_basic_map_cow(bmap);
3773 isl_dim_free(bmap->dim);
3776 bmap = isl_basic_map_finalize(bmap);
3780 isl_basic_map_free(bmap);
3785 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3786 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3788 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3792 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3793 __isl_take isl_dim *dim)
3797 map = isl_map_cow(map);
3801 for (i = 0; i < map->n; ++i) {
3802 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3807 isl_dim_free(map->dim);
3817 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3818 __isl_take isl_dim *dim)
3820 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3823 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3826 struct isl_basic_set *domain;
3832 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3834 n_in = isl_basic_map_n_in(bmap);
3835 n_out = isl_basic_map_n_out(bmap);
3836 domain = isl_basic_set_from_basic_map(bmap);
3837 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3839 domain = isl_basic_set_reset_dim(domain, dim);
3844 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3846 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3849 __isl_give isl_basic_map *isl_basic_map_domain_map(
3850 __isl_take isl_basic_map *bmap)
3854 isl_basic_map *domain;
3855 isl_basic_set *bset;
3856 int nparam, n_in, n_out;
3859 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3860 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3861 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3863 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3864 domain = isl_basic_map_universe(dim);
3866 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3867 bmap = isl_basic_map_apply_range(bmap, domain);
3868 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3870 total = isl_basic_map_total_dim(bmap);
3872 for (i = 0; i < n_in; ++i) {
3873 k = isl_basic_map_alloc_equality(bmap);
3876 isl_seq_clr(bmap->eq[k], 1 + total);
3877 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3878 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3881 bmap = isl_basic_map_gauss(bmap, NULL);
3882 return isl_basic_map_finalize(bmap);
3884 isl_basic_map_free(bmap);
3888 __isl_give isl_basic_map *isl_basic_map_range_map(
3889 __isl_take isl_basic_map *bmap)
3893 isl_basic_map *range;
3894 isl_basic_set *bset;
3895 int nparam, n_in, n_out;
3898 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3899 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3900 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3902 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3903 range = isl_basic_map_universe(dim);
3905 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3906 bmap = isl_basic_map_apply_range(bmap, range);
3907 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3909 total = isl_basic_map_total_dim(bmap);
3911 for (i = 0; i < n_out; ++i) {
3912 k = isl_basic_map_alloc_equality(bmap);
3915 isl_seq_clr(bmap->eq[k], 1 + total);
3916 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3917 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3920 bmap = isl_basic_map_gauss(bmap, NULL);
3921 return isl_basic_map_finalize(bmap);
3923 isl_basic_map_free(bmap);
3927 struct isl_set *isl_map_range(struct isl_map *map)
3930 struct isl_set *set;
3934 if (isl_map_dim(map, isl_dim_in) == 0)
3935 return (isl_set *)map;
3937 map = isl_map_cow(map);
3941 set = (struct isl_set *) map;
3942 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3945 for (i = 0; i < map->n; ++i) {
3946 set->p[i] = isl_basic_map_range(map->p[i]);
3950 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3951 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3958 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3961 isl_dim *domain_dim;
3963 map = isl_map_cow(map);
3967 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3968 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3969 map->dim = isl_dim_join(map->dim, domain_dim);
3972 for (i = 0; i < map->n; ++i) {
3973 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3977 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3978 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3985 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3990 map = isl_map_cow(map);
3994 range_dim = isl_dim_range(isl_map_get_dim(map));
3995 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3996 map->dim = isl_dim_join(map->dim, range_dim);
3999 for (i = 0; i < map->n; ++i) {
4000 map->p[i] = isl_basic_map_range_map(map->p[i]);
4004 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4005 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4012 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4015 struct isl_map *map = NULL;
4017 set = isl_set_cow(set);
4020 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4021 map = (struct isl_map *)set;
4022 for (i = 0; i < set->n; ++i) {
4023 map->p[i] = isl_basic_map_from_basic_set(
4024 set->p[i], isl_dim_copy(dim));
4028 isl_dim_free(map->dim);
4037 __isl_give isl_basic_map *isl_basic_map_from_domain(
4038 __isl_take isl_basic_set *bset)
4040 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4043 __isl_give isl_basic_map *isl_basic_map_from_range(
4044 __isl_take isl_basic_set *bset)
4046 return (isl_basic_map *)bset;
4049 struct isl_map *isl_map_from_range(struct isl_set *set)
4051 return (struct isl_map *)set;
4054 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4056 return isl_map_reverse(isl_map_from_range(set));
4059 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4060 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4062 return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4063 isl_basic_map_from_range(range));
4066 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4067 __isl_take isl_set *range)
4069 return isl_map_apply_range(isl_map_from_domain(domain),
4070 isl_map_from_range(range));
4073 struct isl_set *isl_set_from_map(struct isl_map *map)
4076 struct isl_set *set = NULL;
4080 map = isl_map_cow(map);
4083 map->dim = isl_dim_as_set_dim(map->dim);
4086 set = (struct isl_set *)map;
4087 for (i = 0; i < map->n; ++i) {
4088 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4098 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4100 struct isl_map *map;
4104 isl_assert(dim->ctx, n >= 0, return NULL);
4105 map = isl_alloc(dim->ctx, struct isl_map,
4106 sizeof(struct isl_map) +
4107 (n - 1) * sizeof(struct isl_basic_map *));
4111 map->ctx = dim->ctx;
4112 isl_ctx_ref(map->ctx);
4124 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4125 unsigned nparam, unsigned in, unsigned out, int n,
4128 struct isl_map *map;
4129 struct isl_dim *dims;
4131 dims = isl_dim_alloc(ctx, nparam, in, out);
4135 map = isl_map_alloc_dim(dims, n, flags);
4139 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4141 struct isl_basic_map *bmap;
4142 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4143 bmap = isl_basic_map_set_to_empty(bmap);
4147 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4149 struct isl_basic_set *bset;
4150 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4151 bset = isl_basic_set_set_to_empty(bset);
4155 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4157 struct isl_basic_map *bmap;
4160 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4161 bmap = isl_basic_map_set_to_empty(bmap);
4165 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4167 struct isl_basic_map *bmap;
4170 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4171 bmap = isl_basic_map_set_to_empty(bmap);
4175 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4177 struct isl_basic_set *bset;
4180 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4181 bset = isl_basic_set_set_to_empty(bset);
4185 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4187 struct isl_basic_map *bmap;
4188 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4189 bmap = isl_basic_map_finalize(bmap);
4193 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4195 struct isl_basic_set *bset;
4196 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4197 bset = isl_basic_set_finalize(bset);
4201 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4204 unsigned total = isl_dim_total(dim);
4205 isl_basic_map *bmap;
4207 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4208 for (i = 0; i < total; ++i) {
4209 int k = isl_basic_map_alloc_inequality(bmap);
4212 isl_seq_clr(bmap->ineq[k], 1 + total);
4213 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4217 isl_basic_map_free(bmap);
4221 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4223 return isl_basic_map_nat_universe(dim);
4226 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4228 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4231 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4233 return isl_map_nat_universe(dim);
4236 __isl_give isl_basic_map *isl_basic_map_universe_like(
4237 __isl_keep isl_basic_map *model)
4241 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4244 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4248 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4251 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4252 __isl_keep isl_set *model)
4256 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4259 struct isl_map *isl_map_empty(struct isl_dim *dim)
4261 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4264 struct isl_map *isl_map_empty_like(struct isl_map *model)
4268 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4271 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4275 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4278 struct isl_set *isl_set_empty(struct isl_dim *dim)
4280 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4283 struct isl_set *isl_set_empty_like(struct isl_set *model)
4287 return isl_set_empty(isl_dim_copy(model->dim));
4290 struct isl_map *isl_map_universe(struct isl_dim *dim)
4292 struct isl_map *map;
4295 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4296 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4300 struct isl_set *isl_set_universe(struct isl_dim *dim)
4302 struct isl_set *set;
4305 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4306 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4310 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4314 return isl_set_universe(isl_dim_copy(model->dim));
4317 struct isl_map *isl_map_dup(struct isl_map *map)
4320 struct isl_map *dup;
4324 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4325 for (i = 0; i < map->n; ++i)
4326 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4330 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4331 __isl_take isl_basic_map *bmap)
4335 if (isl_basic_map_fast_is_empty(bmap)) {
4336 isl_basic_map_free(bmap);
4339 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4340 isl_assert(map->ctx, map->n < map->size, goto error);
4341 map->p[map->n] = bmap;
4343 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4349 isl_basic_map_free(bmap);
4353 void isl_map_free(struct isl_map *map)
4363 isl_ctx_deref(map->ctx);
4364 for (i = 0; i < map->n; ++i)
4365 isl_basic_map_free(map->p[i]);
4366 isl_dim_free(map->dim);
4370 struct isl_map *isl_map_extend(struct isl_map *base,
4371 unsigned nparam, unsigned n_in, unsigned n_out)
4375 base = isl_map_cow(base);
4379 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4382 for (i = 0; i < base->n; ++i) {
4383 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4384 isl_dim_copy(base->dim), 0, 0, 0);
4394 struct isl_set *isl_set_extend(struct isl_set *base,
4395 unsigned nparam, unsigned dim)
4397 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4401 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4402 struct isl_basic_map *bmap, unsigned pos, int value)
4406 bmap = isl_basic_map_cow(bmap);
4407 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4408 j = isl_basic_map_alloc_equality(bmap);
4411 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4412 isl_int_set_si(bmap->eq[j][pos], -1);
4413 isl_int_set_si(bmap->eq[j][0], value);
4414 bmap = isl_basic_map_simplify(bmap);
4415 return isl_basic_map_finalize(bmap);
4417 isl_basic_map_free(bmap);
4421 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4422 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4426 bmap = isl_basic_map_cow(bmap);
4427 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4428 j = isl_basic_map_alloc_equality(bmap);
4431 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4432 isl_int_set_si(bmap->eq[j][pos], -1);
4433 isl_int_set(bmap->eq[j][0], value);
4434 bmap = isl_basic_map_simplify(bmap);
4435 return isl_basic_map_finalize(bmap);
4437 isl_basic_map_free(bmap);
4441 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4442 enum isl_dim_type type, unsigned pos, int value)
4446 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4447 return isl_basic_map_fix_pos_si(bmap,
4448 isl_basic_map_offset(bmap, type) + pos, value);
4450 isl_basic_map_free(bmap);
4454 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4455 enum isl_dim_type type, unsigned pos, isl_int value)
4459 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4460 return isl_basic_map_fix_pos(bmap,
4461 isl_basic_map_offset(bmap, type) + pos, value);
4463 isl_basic_map_free(bmap);
4467 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4468 enum isl_dim_type type, unsigned pos, int value)
4470 return (struct isl_basic_set *)
4471 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4475 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4476 enum isl_dim_type type, unsigned pos, isl_int value)
4478 return (struct isl_basic_set *)
4479 isl_basic_map_fix((struct isl_basic_map *)bset,
4483 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4484 unsigned input, int value)
4486 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4489 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4490 unsigned dim, int value)
4492 return (struct isl_basic_set *)
4493 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4494 isl_dim_set, dim, value);
4497 struct isl_map *isl_map_fix_si(struct isl_map *map,
4498 enum isl_dim_type type, unsigned pos, int value)
4502 map = isl_map_cow(map);
4506 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4507 for (i = 0; i < map->n; ++i) {
4508 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4512 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4519 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4520 enum isl_dim_type type, unsigned pos, int value)
4522 return (struct isl_set *)
4523 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4526 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4527 enum isl_dim_type type, unsigned pos, isl_int value)
4531 map = isl_map_cow(map);
4535 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4536 for (i = 0; i < map->n; ++i) {
4537 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4541 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4548 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4549 enum isl_dim_type type, unsigned pos, isl_int value)
4551 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4554 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4555 unsigned input, int value)
4557 return isl_map_fix_si(map, isl_dim_in, input, value);
4560 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4562 return (struct isl_set *)
4563 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4566 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4567 __isl_take isl_basic_map *bmap,
4568 enum isl_dim_type type, unsigned pos, int value)
4574 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4575 pos += isl_basic_map_offset(bmap, type);
4576 bmap = isl_basic_map_cow(bmap);
4577 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4578 j = isl_basic_map_alloc_inequality(bmap);
4581 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4582 isl_int_set_si(bmap->ineq[j][pos], 1);
4583 isl_int_set_si(bmap->ineq[j][0], -value);
4584 bmap = isl_basic_map_simplify(bmap);
4585 return isl_basic_map_finalize(bmap);
4587 isl_basic_map_free(bmap);
4591 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4592 unsigned dim, isl_int value)
4596 bset = isl_basic_set_cow(bset);
4597 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4598 j = isl_basic_set_alloc_inequality(bset);
4601 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4602 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4603 isl_int_neg(bset->ineq[j][0], value);
4604 bset = isl_basic_set_simplify(bset);
4605 return isl_basic_set_finalize(bset);
4607 isl_basic_set_free(bset);
4611 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4612 enum isl_dim_type type, unsigned pos, int value)
4616 map = isl_map_cow(map);
4620 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4621 for (i = 0; i < map->n; ++i) {
4622 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4627 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4634 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4635 enum isl_dim_type type, unsigned pos, int value)
4637 return (struct isl_set *)
4638 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4641 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4646 set = isl_set_cow(set);
4650 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4651 for (i = 0; i < set->n; ++i) {
4652 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4662 struct isl_map *isl_map_reverse(struct isl_map *map)
4666 map = isl_map_cow(map);
4670 map->dim = isl_dim_reverse(map->dim);
4673 for (i = 0; i < map->n; ++i) {
4674 map->p[i] = isl_basic_map_reverse(map->p[i]);
4678 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4685 static struct isl_map *isl_basic_map_partial_lexopt(
4686 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4687 struct isl_set **empty, int max)
4691 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4692 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4694 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4696 isl_basic_map_free(bmap);
4697 isl_basic_set_free(dom);
4703 struct isl_map *isl_basic_map_partial_lexmax(
4704 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4705 struct isl_set **empty)
4707 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4710 struct isl_map *isl_basic_map_partial_lexmin(
4711 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4712 struct isl_set **empty)
4714 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4717 struct isl_set *isl_basic_set_partial_lexmin(
4718 struct isl_basic_set *bset, struct isl_basic_set *dom,
4719 struct isl_set **empty)
4721 return (struct isl_set *)
4722 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4726 struct isl_set *isl_basic_set_partial_lexmax(
4727 struct isl_basic_set *bset, struct isl_basic_set *dom,
4728 struct isl_set **empty)
4730 return (struct isl_set *)
4731 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4735 /* Given a basic map "bmap", compute the lexicographically minimal
4736 * (or maximal) image element for each domain element in dom.
4737 * Set *empty to those elements in dom that do not have an image element.
4739 * We first make sure the basic sets in dom are disjoint and then
4740 * simply collect the results over each of the basic sets separately.
4741 * We could probably improve the efficiency a bit by moving the union
4742 * domain down into the parametric integer programming.
4744 static __isl_give isl_map *basic_map_partial_lexopt(
4745 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4746 __isl_give isl_set **empty, int max)
4749 struct isl_map *res;
4751 dom = isl_set_make_disjoint(dom);
4755 if (isl_set_fast_is_empty(dom)) {
4756 res = isl_map_empty_like_basic_map(bmap);
4757 *empty = isl_set_empty_like(dom);
4759 isl_basic_map_free(bmap);
4763 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4764 isl_basic_set_copy(dom->p[0]), empty, max);
4766 for (i = 1; i < dom->n; ++i) {
4767 struct isl_map *res_i;
4768 struct isl_set *empty_i;
4770 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4771 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4773 res = isl_map_union_disjoint(res, res_i);
4774 *empty = isl_set_union_disjoint(*empty, empty_i);
4778 isl_basic_map_free(bmap);
4783 isl_basic_map_free(bmap);
4787 /* Given a map "map", compute the lexicographically minimal
4788 * (or maximal) image element for each domain element in dom.
4789 * Set *empty to those elements in dom that do not have an image element.
4791 * We first compute the lexicographically minimal or maximal element
4792 * in the first basic map. This results in a partial solution "res"
4793 * and a subset "todo" of dom that still need to be handled.
4794 * We then consider each of the remaining maps in "map" and successively
4795 * improve both "res" and "todo".
4797 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4798 * Assume we are computing the lexicographical maximum.
4799 * We first compute the lexicographically maximal element in basic map i.
4800 * This results in a partial solution res_i and a subset todo_i.
4801 * Then we combine these results with those obtain for the first k basic maps
4802 * to obtain a result that is valid for the first k+1 basic maps.
4803 * In particular, the set where there is no solution is the set where
4804 * there is no solution for the first k basic maps and also no solution
4805 * for the ith basic map, i.e.,
4807 * todo^i = todo^k * todo_i
4809 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4810 * solutions, arbitrarily breaking ties in favor of res^k.
4811 * That is, when res^k(a) >= res_i(a), we pick res^k and
4812 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4813 * the lexicographic order.)
4814 * In practice, we compute
4816 * res^k * (res_i . "<=")
4820 * res_i * (res^k . "<")
4822 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4823 * where only one of res^k and res_i provides a solution and we simply pick
4830 * Note that we only compute these intersections when dom(res^k) intersects
4831 * dom(res_i). Otherwise, the only effect of these intersections is to
4832 * potentially break up res^k and res_i into smaller pieces.
4833 * We want to avoid such splintering as much as possible.
4834 * In fact, an earlier implementation of this function would look for
4835 * better results in the domain of res^k and for extra results in todo^k,
4836 * but this would always result in a splintering according to todo^k,
4837 * even when the domain of basic map i is disjoint from the domains of
4838 * the previous basic maps.
4840 static __isl_give isl_map *isl_map_partial_lexopt(
4841 __isl_take isl_map *map, __isl_take isl_set *dom,
4842 __isl_give isl_set **empty, int max)
4845 struct isl_map *res;
4846 struct isl_set *todo;
4851 if (isl_map_fast_is_empty(map)) {
4859 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4860 isl_set_copy(dom), &todo, max);
4862 for (i = 1; i < map->n; ++i) {
4866 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4868 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4869 isl_set_copy(dom), &todo_i, max);
4872 lt = isl_map_lex_lt(isl_dim_copy(dim));
4873 le = isl_map_lex_le(dim);
4875 lt = isl_map_lex_gt(isl_dim_copy(dim));
4876 le = isl_map_lex_ge(dim);
4878 lt = isl_map_apply_range(isl_map_copy(res), lt);
4879 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4880 le = isl_map_apply_range(isl_map_copy(res_i), le);
4881 le = isl_map_intersect(le, isl_map_copy(res));
4883 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4884 res = isl_map_intersect_domain(res,
4885 isl_set_copy(todo_i));
4886 res_i = isl_map_intersect_domain(res_i,
4887 isl_set_copy(todo));
4890 res = isl_map_union_disjoint(res, res_i);
4891 res = isl_map_union_disjoint(res, lt);
4892 res = isl_map_union_disjoint(res, le);
4894 todo = isl_set_intersect(todo, todo_i);
4914 __isl_give isl_map *isl_map_partial_lexmax(
4915 __isl_take isl_map *map, __isl_take isl_set *dom,
4916 __isl_give isl_set **empty)
4918 return isl_map_partial_lexopt(map, dom, empty, 1);
4921 __isl_give isl_map *isl_map_partial_lexmin(
4922 __isl_take isl_map *map, __isl_take isl_set *dom,
4923 __isl_give isl_set **empty)
4925 return isl_map_partial_lexopt(map, dom, empty, 0);
4928 __isl_give isl_set *isl_set_partial_lexmin(
4929 __isl_take isl_set *set, __isl_take isl_set *dom,
4930 __isl_give isl_set **empty)
4932 return (struct isl_set *)
4933 isl_map_partial_lexmin((struct isl_map *)set,
4937 __isl_give isl_set *isl_set_partial_lexmax(
4938 __isl_take isl_set *set, __isl_take isl_set *dom,
4939 __isl_give isl_set **empty)
4941 return (struct isl_set *)
4942 isl_map_partial_lexmax((struct isl_map *)set,
4946 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4948 struct isl_basic_set *dom = NULL;
4949 struct isl_dim *dom_dim;
4953 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4954 dom = isl_basic_set_universe(dom_dim);
4955 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4957 isl_basic_map_free(bmap);
4961 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4963 return isl_basic_map_lexopt(bmap, 0);
4966 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4968 return isl_basic_map_lexopt(bmap, 1);
4971 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4973 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4976 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4978 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4981 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4983 struct isl_set *dom = NULL;
4984 struct isl_dim *dom_dim;
4988 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4989 dom = isl_set_universe(dom_dim);
4990 return isl_map_partial_lexopt(map, dom, NULL, max);
4996 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4998 return isl_map_lexopt(map, 0);
5001 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5003 return isl_map_lexopt(map, 1);
5006 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5008 return (isl_set *)isl_map_lexmin((isl_map *)set);
5011 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5013 return (isl_set *)isl_map_lexmax((isl_map *)set);
5016 /* Apply a preimage specified by "mat" on the parameters of "bset".
5017 * bset is assumed to have only parameters and divs.
5019 static struct isl_basic_set *basic_set_parameter_preimage(
5020 struct isl_basic_set *bset, struct isl_mat *mat)
5027 bset->dim = isl_dim_cow(bset->dim);
5031 nparam = isl_basic_set_dim(bset, isl_dim_param);
5033 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5035 bset->dim->nparam = 0;
5036 bset->dim->n_out = nparam;
5037 bset = isl_basic_set_preimage(bset, mat);
5039 bset->dim->nparam = bset->dim->n_out;
5040 bset->dim->n_out = 0;
5045 isl_basic_set_free(bset);
5049 /* Apply a preimage specified by "mat" on the parameters of "set".
5050 * set is assumed to have only parameters and divs.
5052 static struct isl_set *set_parameter_preimage(
5053 struct isl_set *set, struct isl_mat *mat)
5055 struct isl_dim *dim = NULL;
5061 dim = isl_dim_copy(set->dim);
5062 dim = isl_dim_cow(dim);
5066 nparam = isl_set_dim(set, isl_dim_param);
5068 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5071 dim->n_out = nparam;
5072 isl_set_reset_dim(set, dim);
5073 set = isl_set_preimage(set, mat);
5076 dim = isl_dim_copy(set->dim);
5077 dim = isl_dim_cow(dim);
5080 dim->nparam = dim->n_out;
5082 isl_set_reset_dim(set, dim);
5092 /* Intersect the basic set "bset" with the affine space specified by the
5093 * equalities in "eq".
5095 static struct isl_basic_set *basic_set_append_equalities(
5096 struct isl_basic_set *bset, struct isl_mat *eq)
5104 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5109 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5110 for (i = 0; i < eq->n_row; ++i) {
5111 k = isl_basic_set_alloc_equality(bset);
5114 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5115 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5119 bset = isl_basic_set_gauss(bset, NULL);
5120 bset = isl_basic_set_finalize(bset);
5125 isl_basic_set_free(bset);
5129 /* Intersect the set "set" with the affine space specified by the
5130 * equalities in "eq".
5132 static struct isl_set *set_append_equalities(struct isl_set *set,
5140 for (i = 0; i < set->n; ++i) {
5141 set->p[i] = basic_set_append_equalities(set->p[i],
5154 /* Project the given basic set onto its parameter domain, possibly introducing
5155 * new, explicit, existential variables in the constraints.
5156 * The input has parameters and (possibly implicit) existential variables.
5157 * The output has the same parameters, but only
5158 * explicit existentially quantified variables.
5160 * The actual projection is performed by pip, but pip doesn't seem
5161 * to like equalities very much, so we first remove the equalities
5162 * among the parameters by performing a variable compression on
5163 * the parameters. Afterward, an inverse transformation is performed
5164 * and the equalities among the parameters are inserted back in.
5166 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5170 struct isl_mat *T, *T2;
5171 struct isl_set *set;
5172 unsigned nparam, n_div;
5174 bset = isl_basic_set_cow(bset);
5178 if (bset->n_eq == 0)
5179 return isl_basic_set_lexmin(bset);
5181 isl_basic_set_gauss(bset, NULL);
5183 nparam = isl_basic_set_dim(bset, isl_dim_param);
5184 n_div = isl_basic_set_dim(bset, isl_dim_div);
5186 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5187 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5190 if (i == bset->n_eq)
5191 return isl_basic_set_lexmin(bset);
5193 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
5195 eq = isl_mat_cow(eq);
5196 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5197 if (T && T->n_col == 0) {
5201 bset = isl_basic_set_set_to_empty(bset);
5202 return isl_set_from_basic_set(bset);
5204 bset = basic_set_parameter_preimage(bset, T);
5206 set = isl_basic_set_lexmin(bset);
5207 set = set_parameter_preimage(set, T2);
5208 set = set_append_equalities(set, eq);
5212 /* Compute an explicit representation for all the existentially
5213 * quantified variables.
5214 * The input and output dimensions are first turned into parameters.
5215 * compute_divs then returns a map with the same parameters and
5216 * no input or output dimensions and the dimension specification
5217 * is reset to that of the input.
5219 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5221 struct isl_basic_set *bset;
5222 struct isl_set *set;
5223 struct isl_map *map;
5224 struct isl_dim *dim, *orig_dim = NULL;
5229 bmap = isl_basic_map_cow(bmap);
5233 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5234 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5235 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5236 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5240 orig_dim = bmap->dim;
5242 bset = (struct isl_basic_set *)bmap;
5244 set = parameter_compute_divs(bset);
5245 map = (struct isl_map *)set;
5246 map = isl_map_reset_dim(map, orig_dim);
5250 isl_basic_map_free(bmap);
5254 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5262 off = isl_dim_total(bmap->dim);
5263 for (i = 0; i < bmap->n_div; ++i) {
5264 if (isl_int_is_zero(bmap->div[i][0]))
5266 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5272 static int map_divs_known(__isl_keep isl_map *map)
5279 for (i = 0; i < map->n; ++i) {
5280 int known = basic_map_divs_known(map->p[i]);
5288 /* If bmap contains any unknown divs, then compute explicit
5289 * expressions for them. However, this computation may be
5290 * quite expensive, so first try to remove divs that aren't
5293 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5297 struct isl_map *map;
5299 known = basic_map_divs_known(bmap);
5303 return isl_map_from_basic_map(bmap);
5305 bmap = isl_basic_map_drop_redundant_divs(bmap);
5307 known = basic_map_divs_known(bmap);
5311 return isl_map_from_basic_map(bmap);
5313 map = compute_divs(bmap);
5316 isl_basic_map_free(bmap);
5320 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5324 struct isl_map *res;
5331 known = map_divs_known(map);
5339 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5340 for (i = 1 ; i < map->n; ++i) {
5342 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5343 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5344 res = isl_map_union_disjoint(res, r2);
5346 res = isl_map_union(res, r2);
5353 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5355 return (struct isl_set *)
5356 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5359 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5361 return (struct isl_set *)
5362 isl_map_compute_divs((struct isl_map *)set);
5365 struct isl_set *isl_map_domain(struct isl_map *map)
5368 struct isl_set *set;
5373 map = isl_map_cow(map);
5377 set = (struct isl_set *)map;
5378 set->dim = isl_dim_domain(set->dim);
5381 for (i = 0; i < map->n; ++i) {
5382 set->p[i] = isl_basic_map_domain(map->p[i]);
5386 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5387 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5394 struct isl_map *isl_map_union_disjoint(
5395 struct isl_map *map1, struct isl_map *map2)
5399 struct isl_map *map = NULL;
5413 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5415 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5416 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5417 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5419 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5420 map1->n + map2->n, flags);
5423 for (i = 0; i < map1->n; ++i) {
5424 map = isl_map_add_basic_map(map,
5425 isl_basic_map_copy(map1->p[i]));
5429 for (i = 0; i < map2->n; ++i) {
5430 map = isl_map_add_basic_map(map,
5431 isl_basic_map_copy(map2->p[i]));
5445 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5447 map1 = isl_map_union_disjoint(map1, map2);
5451 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5455 struct isl_set *isl_set_union_disjoint(
5456 struct isl_set *set1, struct isl_set *set2)
5458 return (struct isl_set *)
5459 isl_map_union_disjoint(
5460 (struct isl_map *)set1, (struct isl_map *)set2);
5463 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5465 return (struct isl_set *)
5466 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5469 struct isl_map *isl_map_intersect_range(
5470 struct isl_map *map, struct isl_set *set)
5473 struct isl_map *result;
5479 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5480 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5481 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5483 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5484 map->n * set->n, flags);
5487 for (i = 0; i < map->n; ++i)
5488 for (j = 0; j < set->n; ++j) {
5489 result = isl_map_add_basic_map(result,
5490 isl_basic_map_intersect_range(
5491 isl_basic_map_copy(map->p[i]),
5492 isl_basic_set_copy(set->p[j])));
5505 struct isl_map *isl_map_intersect_domain(
5506 struct isl_map *map, struct isl_set *set)
5508 return isl_map_reverse(
5509 isl_map_intersect_range(isl_map_reverse(map), set));
5512 struct isl_map *isl_map_apply_domain(
5513 struct isl_map *map1, struct isl_map *map2)
5517 map1 = isl_map_reverse(map1);
5518 map1 = isl_map_apply_range(map1, map2);
5519 return isl_map_reverse(map1);
5526 struct isl_map *isl_map_apply_range(
5527 struct isl_map *map1, struct isl_map *map2)
5529 struct isl_dim *dim_result;
5530 struct isl_map *result;
5536 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5537 isl_dim_copy(map2->dim));
5539 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5542 for (i = 0; i < map1->n; ++i)
5543 for (j = 0; j < map2->n; ++j) {
5544 result = isl_map_add_basic_map(result,
5545 isl_basic_map_apply_range(
5546 isl_basic_map_copy(map1->p[i]),
5547 isl_basic_map_copy(map2->p[j])));
5553 if (result && result->n <= 1)
5554 ISL_F_SET(result, ISL_MAP_DISJOINT);
5563 * returns range - domain
5565 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5567 isl_dim *dims, *target_dim;
5568 struct isl_basic_set *bset;
5575 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5576 bmap->dim, isl_dim_out),
5578 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5579 dim = isl_basic_map_n_in(bmap);
5580 nparam = isl_basic_map_n_param(bmap);
5581 bset = isl_basic_set_from_basic_map(bmap);
5582 bset = isl_basic_set_cow(bset);
5583 dims = isl_basic_set_get_dim(bset);
5584 dims = isl_dim_add(dims, isl_dim_set, dim);
5585 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5586 bset = isl_basic_set_swap_vars(bset, 2*dim);
5587 for (i = 0; i < dim; ++i) {
5588 int j = isl_basic_map_alloc_equality(
5589 (struct isl_basic_map *)bset);
5592 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5593 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5594 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5595 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5597 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5598 bset = isl_basic_set_reset_dim(bset, target_dim);
5601 isl_basic_map_free(bmap);
5606 * returns range - domain
5608 struct isl_set *isl_map_deltas(struct isl_map *map)
5612 struct isl_set *result;
5617 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5618 map->dim, isl_dim_out),
5620 dim = isl_map_get_dim(map);
5621 dim = isl_dim_domain(dim);
5622 result = isl_set_alloc_dim(dim, map->n, 0);
5625 for (i = 0; i < map->n; ++i)
5626 result = isl_set_add_basic_set(result,
5627 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5636 * returns [domain -> range] -> range - domain
5638 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5639 __isl_take isl_basic_map *bmap)
5643 isl_basic_map *domain;
5644 isl_basic_set *bset;
5648 if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5649 isl_die(bmap->ctx, isl_error_invalid,
5650 "domain and range don't match", goto error);
5652 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5653 n = isl_basic_map_dim(bmap, isl_dim_in);
5655 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5656 domain = isl_basic_map_universe(dim);
5658 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5659 bmap = isl_basic_map_apply_range(bmap, domain);
5660 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5662 total = isl_basic_map_total_dim(bmap);
5664 for (i = 0; i < n; ++i) {
5665 k = isl_basic_map_alloc_equality(bmap);
5668 isl_seq_clr(bmap->eq[k], 1 + total);
5669 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
5670 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
5671 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
5674 bmap = isl_basic_map_gauss(bmap, NULL);
5675 return isl_basic_map_finalize(bmap);
5677 isl_basic_map_free(bmap);
5682 * returns [domain -> range] -> range - domain
5684 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
5687 isl_dim *domain_dim;
5692 if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
5693 isl_die(map->ctx, isl_error_invalid,
5694 "domain and range don't match", goto error);
5696 map = isl_map_cow(map);
5700 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
5701 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
5702 map->dim = isl_dim_join(map->dim, domain_dim);
5705 for (i = 0; i < map->n; ++i) {
5706 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
5710 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5717 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5719 struct isl_basic_map *bmap;
5727 nparam = dims->nparam;
5729 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5733 for (i = 0; i < dim; ++i) {
5734 int j = isl_basic_map_alloc_equality(bmap);
5737 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5738 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5739 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5741 return isl_basic_map_finalize(bmap);
5743 isl_basic_map_free(bmap);
5747 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
5751 if (dim->n_in != dim->n_out)
5752 isl_die(dim->ctx, isl_error_invalid,
5753 "number of input and output dimensions needs to be "
5754 "the same", goto error);
5755 return basic_map_identity(dim);
5761 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5763 if (!model || !model->dim)
5765 return isl_basic_map_identity(isl_dim_copy(model->dim));
5768 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
5770 return isl_map_from_basic_map(isl_basic_map_identity(dim));
5773 struct isl_map *isl_map_identity_like(struct isl_map *model)
5775 if (!model || !model->dim)
5777 return isl_map_identity(isl_dim_copy(model->dim));
5780 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5782 if (!model || !model->dim)
5784 return isl_map_identity(isl_dim_copy(model->dim));
5787 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5789 isl_dim *dim = isl_set_get_dim(set);
5791 id = isl_map_identity(isl_dim_map_from_set(dim));
5792 return isl_map_intersect_range(id, set);
5795 /* Construct a basic set with all set dimensions having only non-negative
5798 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5803 struct isl_basic_set *bset;
5807 nparam = dims->nparam;
5809 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5812 for (i = 0; i < dim; ++i) {
5813 int k = isl_basic_set_alloc_inequality(bset);
5816 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5817 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5821 isl_basic_set_free(bset);
5825 /* Construct the half-space x_pos >= 0.
5827 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5831 isl_basic_set *nonneg;
5833 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5834 k = isl_basic_set_alloc_inequality(nonneg);
5837 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5838 isl_int_set_si(nonneg->ineq[k][pos], 1);
5840 return isl_basic_set_finalize(nonneg);
5842 isl_basic_set_free(nonneg);
5846 /* Construct the half-space x_pos <= -1.
5848 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5853 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5854 k = isl_basic_set_alloc_inequality(neg);
5857 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5858 isl_int_set_si(neg->ineq[k][0], -1);
5859 isl_int_set_si(neg->ineq[k][pos], -1);
5861 return isl_basic_set_finalize(neg);
5863 isl_basic_set_free(neg);
5867 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5868 enum isl_dim_type type, unsigned first, unsigned n)
5871 isl_basic_set *nonneg;
5879 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5881 for (i = 0; i < n; ++i) {
5882 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5883 pos(set->dim, type) + first + i);
5884 neg = neg_halfspace(isl_set_get_dim(set),
5885 pos(set->dim, type) + first + i);
5887 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5896 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5897 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5904 if (isl_set_fast_is_empty(set)) {
5909 return fn(set, signs, user);
5912 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5914 half = isl_set_intersect(half, isl_set_copy(set));
5915 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5919 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5921 half = isl_set_intersect(half, set);
5922 return foreach_orthant(half, signs, first + 1, len, fn, user);
5928 /* Call "fn" on the intersections of "set" with each of the orthants
5929 * (except for obviously empty intersections). The orthant is identified
5930 * by the signs array, with each entry having value 1 or -1 according
5931 * to the sign of the corresponding variable.
5933 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5934 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5944 if (isl_set_fast_is_empty(set))
5947 nparam = isl_set_dim(set, isl_dim_param);
5948 nvar = isl_set_dim(set, isl_dim_set);
5950 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5952 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5960 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5962 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5965 int isl_basic_map_is_subset(
5966 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5969 struct isl_map *map1;
5970 struct isl_map *map2;
5972 if (!bmap1 || !bmap2)
5975 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5976 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5978 is_subset = isl_map_is_subset(map1, map2);
5986 int isl_basic_map_is_equal(
5987 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5991 if (!bmap1 || !bmap2)
5993 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5996 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6000 int isl_basic_set_is_equal(
6001 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6003 return isl_basic_map_is_equal(
6004 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6007 int isl_map_is_empty(struct isl_map *map)
6014 for (i = 0; i < map->n; ++i) {
6015 is_empty = isl_basic_map_is_empty(map->p[i]);
6024 int isl_map_fast_is_empty(struct isl_map *map)
6026 return map ? map->n == 0 : -1;
6029 int isl_set_fast_is_empty(struct isl_set *set)
6031 return set ? set->n == 0 : -1;
6034 int isl_set_is_empty(struct isl_set *set)
6036 return isl_map_is_empty((struct isl_map *)set);
6039 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6044 return isl_dim_equal(map1->dim, map2->dim);
6047 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6052 return isl_dim_equal(set1->dim, set2->dim);
6055 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
6061 is_subset = isl_map_is_subset(map1, map2);
6064 is_subset = isl_map_is_subset(map2, map1);
6068 int isl_basic_map_is_strict_subset(
6069 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6073 if (!bmap1 || !bmap2)
6075 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6078 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6079 if (is_subset == -1)
6084 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6090 is_subset = isl_map_is_subset(map1, map2);
6093 is_subset = isl_map_is_subset(map2, map1);
6094 if (is_subset == -1)
6099 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6101 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6104 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6108 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6111 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6115 return bset->n_eq == 0 && bset->n_ineq == 0;
6118 int isl_map_fast_is_universe(__isl_keep isl_map *map)
6125 for (i = 0; i < map->n; ++i) {
6126 int r = isl_basic_map_is_universe(map->p[i]);
6134 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6136 return isl_map_fast_is_universe((isl_map *) set);
6139 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6141 struct isl_basic_set *bset = NULL;
6142 struct isl_vec *sample = NULL;
6149 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6152 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6153 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6154 copy = isl_basic_map_remove_redundancies(copy);
6155 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6156 isl_basic_map_free(copy);
6160 total = 1 + isl_basic_map_total_dim(bmap);
6161 if (bmap->sample && bmap->sample->size == total) {
6162 int contains = isl_basic_map_contains(bmap, bmap->sample);
6168 isl_vec_free(bmap->sample);
6169 bmap->sample = NULL;
6170 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6173 sample = isl_basic_set_sample_vec(bset);
6176 empty = sample->size == 0;
6177 isl_vec_free(bmap->sample);
6178 bmap->sample = sample;
6180 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6185 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
6189 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6192 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
6196 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6199 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6201 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6204 struct isl_map *isl_basic_map_union(
6205 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6207 struct isl_map *map;
6208 if (!bmap1 || !bmap2)
6211 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6213 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6216 map = isl_map_add_basic_map(map, bmap1);
6217 map = isl_map_add_basic_map(map, bmap2);
6220 isl_basic_map_free(bmap1);
6221 isl_basic_map_free(bmap2);
6225 struct isl_set *isl_basic_set_union(
6226 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6228 return (struct isl_set *)isl_basic_map_union(
6229 (struct isl_basic_map *)bset1,
6230 (struct isl_basic_map *)bset2);
6233 /* Order divs such that any div only depends on previous divs */
6234 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6242 off = isl_dim_total(bmap->dim);
6244 for (i = 0; i < bmap->n_div; ++i) {
6246 if (isl_int_is_zero(bmap->div[i][0]))
6248 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6252 isl_basic_map_swap_div(bmap, i, i + pos);
6258 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6260 return (struct isl_basic_set *)
6261 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6264 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6271 for (i = 0; i < map->n; ++i) {
6272 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6283 /* Look for a div in dst that corresponds to the div "div" in src.
6284 * The divs before "div" in src and dst are assumed to be the same.
6286 * Returns -1 if no corresponding div was found and the position
6287 * of the corresponding div in dst otherwise.
6289 static int find_div(struct isl_basic_map *dst,
6290 struct isl_basic_map *src, unsigned div)
6294 unsigned total = isl_dim_total(src->dim);
6296 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6297 for (i = div; i < dst->n_div; ++i)
6298 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6299 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6300 dst->n_div - div) == -1)
6305 struct isl_basic_map *isl_basic_map_align_divs(
6306 struct isl_basic_map *dst, struct isl_basic_map *src)
6309 unsigned total = isl_dim_total(src->dim);
6314 if (src->n_div == 0)
6317 for (i = 0; i < src->n_div; ++i)
6318 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6320 src = isl_basic_map_order_divs(src);
6321 dst = isl_basic_map_cow(dst);
6322 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6323 src->n_div, 0, 2 * src->n_div);
6326 for (i = 0; i < src->n_div; ++i) {
6327 int j = find_div(dst, src, i);
6329 j = isl_basic_map_alloc_div(dst);
6332 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6333 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6334 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6338 isl_basic_map_swap_div(dst, i, j);
6342 isl_basic_map_free(dst);
6346 struct isl_basic_set *isl_basic_set_align_divs(
6347 struct isl_basic_set *dst, struct isl_basic_set *src)
6349 return (struct isl_basic_set *)isl_basic_map_align_divs(
6350 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6353 struct isl_map *isl_map_align_divs(struct isl_map *map)
6361 map = isl_map_compute_divs(map);
6362 map = isl_map_cow(map);
6366 for (i = 1; i < map->n; ++i)
6367 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6368 for (i = 1; i < map->n; ++i)
6369 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6371 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6375 struct isl_set *isl_set_align_divs(struct isl_set *set)
6377 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6380 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6384 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6385 map = isl_map_intersect_domain(map, set);
6386 set = isl_map_range(map);
6394 /* There is no need to cow as removing empty parts doesn't change
6395 * the meaning of the set.
6397 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6404 for (i = map->n-1; i >= 0; --i) {
6405 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6407 isl_basic_map_free(map->p[i]);
6408 if (i != map->n-1) {
6409 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6410 map->p[i] = map->p[map->n-1];
6418 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6420 return (struct isl_set *)
6421 isl_map_remove_empty_parts((struct isl_map *)set);
6424 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6426 struct isl_basic_map *bmap;
6427 if (!map || map->n == 0)
6429 bmap = map->p[map->n-1];
6430 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6431 return isl_basic_map_copy(bmap);
6434 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6436 return (struct isl_basic_set *)
6437 isl_map_copy_basic_map((struct isl_map *)set);
6440 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6441 __isl_keep isl_basic_map *bmap)
6447 for (i = map->n-1; i >= 0; --i) {
6448 if (map->p[i] != bmap)
6450 map = isl_map_cow(map);
6453 isl_basic_map_free(map->p[i]);
6454 if (i != map->n-1) {
6455 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6456 map->p[i] = map->p[map->n-1];
6467 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6468 struct isl_basic_set *bset)
6470 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6471 (struct isl_basic_map *)bset);
6474 /* Given two basic sets bset1 and bset2, compute the maximal difference
6475 * between the values of dimension pos in bset1 and those in bset2
6476 * for any common value of the parameters and dimensions preceding pos.
6478 static enum isl_lp_result basic_set_maximal_difference_at(
6479 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6480 int pos, isl_int *opt)
6482 struct isl_dim *dims;
6483 struct isl_basic_map *bmap1 = NULL;
6484 struct isl_basic_map *bmap2 = NULL;
6485 struct isl_ctx *ctx;
6486 struct isl_vec *obj;
6489 unsigned dim1, dim2;
6490 enum isl_lp_result res;
6492 if (!bset1 || !bset2)
6493 return isl_lp_error;
6495 nparam = isl_basic_set_n_param(bset1);
6496 dim1 = isl_basic_set_n_dim(bset1);
6497 dim2 = isl_basic_set_n_dim(bset2);
6498 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6499 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6500 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6501 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6502 if (!bmap1 || !bmap2)
6504 bmap1 = isl_basic_map_cow(bmap1);
6505 bmap1 = isl_basic_map_extend(bmap1, nparam,
6506 pos, (dim1 - pos) + (dim2 - pos),
6507 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6508 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6511 total = isl_basic_map_total_dim(bmap1);
6513 obj = isl_vec_alloc(ctx, 1 + total);
6514 isl_seq_clr(obj->block.data, 1 + total);
6515 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6516 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6519 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6521 isl_basic_map_free(bmap1);
6525 isl_basic_map_free(bmap1);
6526 isl_basic_map_free(bmap2);
6527 return isl_lp_error;
6530 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6531 * for any common value of the parameters and dimensions preceding pos
6532 * in both basic sets, the values of dimension pos in bset1 are
6533 * smaller or larger than those in bset2.
6536 * 1 if bset1 follows bset2
6537 * -1 if bset1 precedes bset2
6538 * 0 if bset1 and bset2 are incomparable
6539 * -2 if some error occurred.
6541 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6542 struct isl_basic_set *bset2, int pos)
6545 enum isl_lp_result res;
6550 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6552 if (res == isl_lp_empty)
6554 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6555 res == isl_lp_unbounded)
6557 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6566 /* Given two basic sets bset1 and bset2, check whether
6567 * for any common value of the parameters and dimensions preceding pos
6568 * there is a value of dimension pos in bset1 that is larger
6569 * than a value of the same dimension in bset2.
6572 * 1 if there exists such a pair
6573 * 0 if there is no such pair, but there is a pair of equal values
6575 * -2 if some error occurred.
6577 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6578 __isl_keep isl_basic_set *bset2, int pos)
6581 enum isl_lp_result res;
6586 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6588 if (res == isl_lp_empty)
6590 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6591 res == isl_lp_unbounded)
6593 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6595 else if (res == isl_lp_ok)
6604 /* Given two sets set1 and set2, check whether
6605 * for any common value of the parameters and dimensions preceding pos
6606 * there is a value of dimension pos in set1 that is larger
6607 * than a value of the same dimension in set2.
6610 * 1 if there exists such a pair
6611 * 0 if there is no such pair, but there is a pair of equal values
6613 * -2 if some error occurred.
6615 int isl_set_follows_at(__isl_keep isl_set *set1,
6616 __isl_keep isl_set *set2, int pos)
6624 for (i = 0; i < set1->n; ++i)
6625 for (j = 0; j < set2->n; ++j) {
6627 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6628 if (f == 1 || f == -2)
6637 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
6638 unsigned pos, isl_int *val)
6646 total = isl_basic_map_total_dim(bmap);
6647 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6648 for (; d+1 > pos; --d)
6649 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6653 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6655 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6657 if (!isl_int_is_one(bmap->eq[i][1+d]))
6660 isl_int_neg(*val, bmap->eq[i][0]);
6666 static int isl_map_fast_has_fixed_var(struct isl_map *map,
6667 unsigned pos, isl_int *val)
6679 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
6682 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
6683 for (i = 1; fixed == 1 && i < map->n; ++i) {
6684 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
6685 if (fixed == 1 && isl_int_ne(tmp, v))
6689 isl_int_set(*val, v);
6695 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
6696 unsigned pos, isl_int *val)
6698 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
6702 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
6705 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
6708 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
6709 enum isl_dim_type type, unsigned pos, isl_int *val)
6711 if (pos >= isl_basic_map_dim(bmap, type))
6713 return isl_basic_map_fast_has_fixed_var(bmap,
6714 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6717 int isl_map_fast_is_fixed(struct isl_map *map,
6718 enum isl_dim_type type, unsigned pos, isl_int *val)
6720 if (pos >= isl_map_dim(map, type))
6722 return isl_map_fast_has_fixed_var(map,
6723 map_offset(map, type) - 1 + pos, val);
6726 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6727 * then return this fixed value in *val.
6729 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6732 return isl_basic_set_fast_has_fixed_var(bset,
6733 isl_basic_set_n_param(bset) + dim, val);
6736 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6737 * then return this fixed value in *val.
6739 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6741 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6744 /* Check if input variable in has fixed value and if so and if val is not NULL,
6745 * then return this fixed value in *val.
6747 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6749 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6752 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6753 * and if val is not NULL, then return this lower bound in *val.
6755 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6756 unsigned dim, isl_int *val)
6758 int i, i_eq = -1, i_ineq = -1;
6765 total = isl_basic_set_total_dim(bset);
6766 nparam = isl_basic_set_n_param(bset);
6767 for (i = 0; i < bset->n_eq; ++i) {
6768 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6774 for (i = 0; i < bset->n_ineq; ++i) {
6775 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6777 if (i_eq != -1 || i_ineq != -1)
6781 if (i_eq == -1 && i_ineq == -1)
6783 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6784 /* The coefficient should always be one due to normalization. */
6785 if (!isl_int_is_one(c[1+nparam+dim]))
6787 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6789 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6790 total - nparam - dim - 1) != -1)
6793 isl_int_neg(*val, c[0]);
6797 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6798 unsigned dim, isl_int *val)
6810 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6814 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6816 for (i = 1; fixed == 1 && i < set->n; ++i) {
6817 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6819 if (fixed == 1 && isl_int_ne(tmp, v))
6823 isl_int_set(*val, v);
6834 /* uset_gist depends on constraints without existentially quantified
6835 * variables sorting first.
6837 static int qsort_constraint_cmp(const void *p1, const void *p2)
6839 const struct constraint *c1 = (const struct constraint *)p1;
6840 const struct constraint *c2 = (const struct constraint *)p2;
6842 unsigned size = isl_min(c1->size, c2->size);
6844 l1 = isl_seq_last_non_zero(c1->c, size);
6845 l2 = isl_seq_last_non_zero(c2->c, size);
6850 return isl_seq_cmp(c1->c, c2->c, size);
6853 static struct isl_basic_map *isl_basic_map_sort_constraints(
6854 struct isl_basic_map *bmap)
6857 struct constraint *c;
6862 total = isl_basic_map_total_dim(bmap);
6863 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6866 for (i = 0; i < bmap->n_ineq; ++i) {
6868 c[i].c = bmap->ineq[i];
6870 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6871 for (i = 0; i < bmap->n_ineq; ++i)
6872 bmap->ineq[i] = c[i].c;
6876 isl_basic_map_free(bmap);
6880 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6881 __isl_take isl_basic_set *bset)
6883 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6884 (struct isl_basic_map *)bset);
6887 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6891 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6893 bmap = isl_basic_map_remove_redundancies(bmap);
6894 bmap = isl_basic_map_sort_constraints(bmap);
6895 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6899 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6901 return (struct isl_basic_set *)isl_basic_map_normalize(
6902 (struct isl_basic_map *)bset);
6905 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6906 const __isl_keep isl_basic_map *bmap2)
6913 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
6914 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
6915 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
6916 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6917 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6918 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6919 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6920 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6921 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6922 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6923 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6925 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6927 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6929 if (bmap1->n_eq != bmap2->n_eq)
6930 return bmap1->n_eq - bmap2->n_eq;
6931 if (bmap1->n_ineq != bmap2->n_ineq)
6932 return bmap1->n_ineq - bmap2->n_ineq;
6933 if (bmap1->n_div != bmap2->n_div)
6934 return bmap1->n_div - bmap2->n_div;
6935 total = isl_basic_map_total_dim(bmap1);
6936 for (i = 0; i < bmap1->n_eq; ++i) {
6937 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6941 for (i = 0; i < bmap1->n_ineq; ++i) {
6942 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6946 for (i = 0; i < bmap1->n_div; ++i) {
6947 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6954 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6955 __isl_keep isl_basic_map *bmap2)
6957 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6960 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6961 __isl_keep isl_basic_set *bset2)
6963 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6964 (isl_basic_map *)bset2);
6967 static int qsort_bmap_cmp(const void *p1, const void *p2)
6969 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6970 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6972 return isl_basic_map_fast_cmp(bmap1, bmap2);
6975 /* We normalize in place, but if anything goes wrong we need
6976 * to return NULL, so we need to make sure we don't change the
6977 * meaning of any possible other copies of map.
6979 struct isl_map *isl_map_normalize(struct isl_map *map)
6982 struct isl_basic_map *bmap;
6986 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6988 for (i = 0; i < map->n; ++i) {
6989 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6992 isl_basic_map_free(map->p[i]);
6995 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6996 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6997 map = isl_map_remove_empty_parts(map);
7000 for (i = map->n - 1; i >= 1; --i) {
7001 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
7003 isl_basic_map_free(map->p[i-1]);
7004 for (j = i; j < map->n; ++j)
7005 map->p[j-1] = map->p[j];
7015 struct isl_set *isl_set_normalize(struct isl_set *set)
7017 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7020 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
7030 if (!isl_dim_equal(map1->dim, map2->dim))
7033 map1 = isl_map_copy(map1);
7034 map2 = isl_map_copy(map2);
7035 map1 = isl_map_normalize(map1);
7036 map2 = isl_map_normalize(map2);
7039 equal = map1->n == map2->n;
7040 for (i = 0; equal && i < map1->n; ++i) {
7041 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
7054 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
7056 return isl_map_fast_is_equal((struct isl_map *)set1,
7057 (struct isl_map *)set2);
7060 /* Return an interval that ranges from min to max (inclusive)
7062 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7063 isl_int min, isl_int max)
7066 struct isl_basic_set *bset = NULL;
7068 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7072 k = isl_basic_set_alloc_inequality(bset);
7075 isl_int_set_si(bset->ineq[k][1], 1);
7076 isl_int_neg(bset->ineq[k][0], min);
7078 k = isl_basic_set_alloc_inequality(bset);
7081 isl_int_set_si(bset->ineq[k][1], -1);
7082 isl_int_set(bset->ineq[k][0], max);
7086 isl_basic_set_free(bset);
7090 /* Return the Cartesian product of the basic sets in list (in the given order).
7092 __isl_give isl_basic_set *isl_basic_set_list_product(
7093 __isl_take struct isl_basic_set_list *list)
7101 struct isl_basic_set *product = NULL;
7105 isl_assert(list->ctx, list->n > 0, goto error);
7106 isl_assert(list->ctx, list->p[0], goto error);
7107 nparam = isl_basic_set_n_param(list->p[0]);
7108 dim = isl_basic_set_n_dim(list->p[0]);
7109 extra = list->p[0]->n_div;
7110 n_eq = list->p[0]->n_eq;
7111 n_ineq = list->p[0]->n_ineq;
7112 for (i = 1; i < list->n; ++i) {
7113 isl_assert(list->ctx, list->p[i], goto error);
7114 isl_assert(list->ctx,
7115 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7116 dim += isl_basic_set_n_dim(list->p[i]);
7117 extra += list->p[i]->n_div;
7118 n_eq += list->p[i]->n_eq;
7119 n_ineq += list->p[i]->n_ineq;
7121 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7126 for (i = 0; i < list->n; ++i) {
7127 isl_basic_set_add_constraints(product,
7128 isl_basic_set_copy(list->p[i]), dim);
7129 dim += isl_basic_set_n_dim(list->p[i]);
7131 isl_basic_set_list_free(list);
7134 isl_basic_set_free(product);
7135 isl_basic_set_list_free(list);
7139 struct isl_basic_map *isl_basic_map_product(
7140 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7142 struct isl_dim *dim_result = NULL;
7143 struct isl_basic_map *bmap;
7144 unsigned in1, in2, out1, out2, nparam, total, pos;
7145 struct isl_dim_map *dim_map1, *dim_map2;
7147 if (!bmap1 || !bmap2)
7150 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7151 bmap2->dim, isl_dim_param), goto error);
7152 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7153 isl_dim_copy(bmap2->dim));
7155 in1 = isl_basic_map_n_in(bmap1);
7156 in2 = isl_basic_map_n_in(bmap2);
7157 out1 = isl_basic_map_n_out(bmap1);
7158 out2 = isl_basic_map_n_out(bmap2);
7159 nparam = isl_basic_map_n_param(bmap1);
7161 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7162 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7163 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7164 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7165 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7166 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7167 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7168 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7169 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7170 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7171 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7173 bmap = isl_basic_map_alloc_dim(dim_result,
7174 bmap1->n_div + bmap2->n_div,
7175 bmap1->n_eq + bmap2->n_eq,
7176 bmap1->n_ineq + bmap2->n_ineq);
7177 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
7178 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
7179 bmap = isl_basic_map_simplify(bmap);
7180 return isl_basic_map_finalize(bmap);
7182 isl_basic_map_free(bmap1);
7183 isl_basic_map_free(bmap2);
7187 __isl_give isl_basic_map *isl_basic_map_flat_product(
7188 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7190 isl_basic_map *prod;
7192 prod = isl_basic_map_product(bmap1, bmap2);
7193 prod = isl_basic_map_flatten(prod);
7197 __isl_give isl_basic_set *isl_basic_set_flat_product(
7198 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7200 return isl_basic_map_flat_product(bset1, bset2);
7203 __isl_give isl_basic_map *isl_basic_map_range_product(
7204 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7206 isl_dim *dim_result = NULL;
7207 isl_basic_map *bmap;
7208 unsigned in, out1, out2, nparam, total, pos;
7209 struct isl_dim_map *dim_map1, *dim_map2;
7211 if (!bmap1 || !bmap2)
7214 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7215 isl_dim_copy(bmap2->dim));
7217 in = isl_basic_map_dim(bmap1, isl_dim_in);
7218 out1 = isl_basic_map_n_out(bmap1);
7219 out2 = isl_basic_map_n_out(bmap2);
7220 nparam = isl_basic_map_n_param(bmap1);
7222 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7223 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7224 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7225 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7226 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7227 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7228 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7229 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7230 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7231 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7232 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7234 bmap = isl_basic_map_alloc_dim(dim_result,
7235 bmap1->n_div + bmap2->n_div,
7236 bmap1->n_eq + bmap2->n_eq,
7237 bmap1->n_ineq + bmap2->n_ineq);
7238 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
7239 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
7240 bmap = isl_basic_map_simplify(bmap);
7241 return isl_basic_map_finalize(bmap);
7243 isl_basic_map_free(bmap1);
7244 isl_basic_map_free(bmap2);
7248 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7249 __isl_take isl_map *map2,
7250 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7251 __isl_take isl_dim *right),
7252 __isl_give isl_basic_map *(*basic_map_product)(
7253 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7256 struct isl_map *result;
7262 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7263 map2->dim, isl_dim_param), goto error);
7265 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7266 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7267 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7269 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7270 isl_dim_copy(map2->dim)),
7271 map1->n * map2->n, flags);
7274 for (i = 0; i < map1->n; ++i)
7275 for (j = 0; j < map2->n; ++j) {
7276 struct isl_basic_map *part;
7277 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7278 isl_basic_map_copy(map2->p[j]));
7279 if (isl_basic_map_is_empty(part))
7280 isl_basic_map_free(part);
7282 result = isl_map_add_basic_map(result, part);
7295 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7297 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7299 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7302 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7304 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7305 __isl_take isl_map *map2)
7309 prod = isl_map_product(map1, map2);
7310 prod = isl_map_flatten(prod);
7314 /* Given two set A and B, construct its Cartesian product A x B.
7316 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7318 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7319 (struct isl_map *)set2);
7322 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7323 __isl_take isl_set *set2)
7325 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7328 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7330 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7331 __isl_take isl_map *map2)
7333 return map_product(map1, map2, &isl_dim_range_product,
7334 &isl_basic_map_range_product);
7337 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7340 uint32_t hash = isl_hash_init();
7345 bmap = isl_basic_map_copy(bmap);
7346 bmap = isl_basic_map_normalize(bmap);
7349 total = isl_basic_map_total_dim(bmap);
7350 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7351 for (i = 0; i < bmap->n_eq; ++i) {
7353 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7354 isl_hash_hash(hash, c_hash);
7356 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7357 for (i = 0; i < bmap->n_ineq; ++i) {
7359 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7360 isl_hash_hash(hash, c_hash);
7362 isl_hash_byte(hash, bmap->n_div & 0xFF);
7363 for (i = 0; i < bmap->n_div; ++i) {
7365 if (isl_int_is_zero(bmap->div[i][0]))
7367 isl_hash_byte(hash, i & 0xFF);
7368 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7369 isl_hash_hash(hash, c_hash);
7371 isl_basic_map_free(bmap);
7375 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7377 return isl_basic_map_get_hash((isl_basic_map *)bset);
7380 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7387 map = isl_map_copy(map);
7388 map = isl_map_normalize(map);
7392 hash = isl_hash_init();
7393 for (i = 0; i < map->n; ++i) {
7395 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7396 isl_hash_hash(hash, bmap_hash);
7404 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7406 return isl_map_get_hash((isl_map *)set);
7409 /* Check if the value for dimension dim is completely determined
7410 * by the values of the other parameters and variables.
7411 * That is, check if dimension dim is involved in an equality.
7413 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7420 nparam = isl_basic_set_n_param(bset);
7421 for (i = 0; i < bset->n_eq; ++i)
7422 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7427 /* Check if the value for dimension dim is completely determined
7428 * by the values of the other parameters and variables.
7429 * That is, check if dimension dim is involved in an equality
7430 * for each of the subsets.
7432 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7438 for (i = 0; i < set->n; ++i) {
7440 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7447 int isl_set_n_basic_set(__isl_keep isl_set *set)
7449 return set ? set->n : 0;
7452 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7453 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7460 for (i = 0; i < map->n; ++i)
7461 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7467 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7468 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7475 for (i = 0; i < set->n; ++i)
7476 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7482 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7484 struct isl_dim *dim;
7489 bset = isl_basic_set_cow(bset);
7493 dim = isl_basic_set_get_dim(bset);
7494 dim = isl_dim_lift(dim, bset->n_div);
7497 isl_dim_free(bset->dim);
7499 bset->extra -= bset->n_div;
7502 bset = isl_basic_set_finalize(bset);
7506 isl_basic_set_free(bset);
7510 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7513 struct isl_dim *dim;
7516 set = isl_set_align_divs(set);
7521 set = isl_set_cow(set);
7525 n_div = set->p[0]->n_div;
7526 dim = isl_set_get_dim(set);
7527 dim = isl_dim_lift(dim, n_div);
7530 isl_dim_free(set->dim);
7533 for (i = 0; i < set->n; ++i) {
7534 set->p[i] = isl_basic_set_lift(set->p[i]);
7545 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7547 struct isl_dim *dim;
7548 struct isl_basic_map *bmap;
7555 set = isl_set_align_divs(set);
7560 dim = isl_set_get_dim(set);
7561 if (set->n == 0 || set->p[0]->n_div == 0) {
7563 return isl_map_identity(isl_dim_map_from_set(dim));
7566 n_div = set->p[0]->n_div;
7567 dim = isl_dim_map_from_set(dim);
7568 n_param = isl_dim_size(dim, isl_dim_param);
7569 n_set = isl_dim_size(dim, isl_dim_in);
7570 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7571 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7572 for (i = 0; i < n_set; ++i)
7573 bmap = var_equal(bmap, i);
7575 total = n_param + n_set + n_set + n_div;
7576 for (i = 0; i < n_div; ++i) {
7577 k = isl_basic_map_alloc_inequality(bmap);
7580 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7581 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7582 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7583 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7584 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7585 set->p[0]->div[i][0]);
7587 l = isl_basic_map_alloc_inequality(bmap);
7590 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7591 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7592 set->p[0]->div[i][0]);
7593 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7597 bmap = isl_basic_map_simplify(bmap);
7598 bmap = isl_basic_map_finalize(bmap);
7599 return isl_map_from_basic_map(bmap);
7602 isl_basic_map_free(bmap);
7606 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7614 dim = isl_basic_set_total_dim(bset);
7615 size += bset->n_eq * (1 + dim);
7616 size += bset->n_ineq * (1 + dim);
7617 size += bset->n_div * (2 + dim);
7622 int isl_set_size(__isl_keep isl_set *set)
7630 for (i = 0; i < set->n; ++i)
7631 size += isl_basic_set_size(set->p[i]);
7636 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7637 enum isl_dim_type type, unsigned pos)
7645 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7647 pos += isl_basic_map_offset(bmap, type);
7649 for (i = 0; i < bmap->n_eq; ++i)
7650 if (!isl_int_is_zero(bmap->eq[i][pos]))
7654 for (i = 0; i < bmap->n_ineq; ++i) {
7655 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7662 return lower && upper;
7665 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7666 enum isl_dim_type type, unsigned pos)
7673 for (i = 0; i < map->n; ++i) {
7675 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7676 if (bounded < 0 || !bounded)
7683 /* Return 1 if the specified dim is involved in both an upper bound
7684 * and a lower bound.
7686 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7687 enum isl_dim_type type, unsigned pos)
7689 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7692 /* For each of the "n" variables starting at "first", determine
7693 * the sign of the variable and put the results in the first "n"
7694 * elements of the array "signs".
7696 * 1 means that the variable is non-negative
7697 * -1 means that the variable is non-positive
7698 * 0 means the variable attains both positive and negative values.
7700 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7701 unsigned first, unsigned n, int *signs)
7703 isl_vec *bound = NULL;
7704 struct isl_tab *tab = NULL;
7705 struct isl_tab_undo *snap;
7708 if (!bset || !signs)
7711 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7712 tab = isl_tab_from_basic_set(bset);
7716 isl_seq_clr(bound->el, bound->size);
7717 isl_int_set_si(bound->el[0], -1);
7719 snap = isl_tab_snap(tab);
7720 for (i = 0; i < n; ++i) {
7723 isl_int_set_si(bound->el[1 + first + i], -1);
7724 if (isl_tab_add_ineq(tab, bound->el) < 0)
7727 isl_int_set_si(bound->el[1 + first + i], 0);
7728 if (isl_tab_rollback(tab, snap) < 0)
7736 isl_int_set_si(bound->el[1 + first + i], 1);
7737 if (isl_tab_add_ineq(tab, bound->el) < 0)
7740 isl_int_set_si(bound->el[1 + first + i], 0);
7741 if (isl_tab_rollback(tab, snap) < 0)
7744 signs[i] = empty ? -1 : 0;
7748 isl_vec_free(bound);
7752 isl_vec_free(bound);
7756 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7757 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7759 if (!bset || !signs)
7761 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7764 first += pos(bset->dim, type) - 1;
7765 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7768 /* Check if the given basic map is obviously single-valued.
7769 * In particular, for each output dimension, check that there is
7770 * an equality that defines the output dimension in terms of
7771 * earlier dimensions.
7773 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map *bmap)
7783 total = 1 + isl_basic_map_total_dim(bmap);
7784 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7785 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7787 for (i = 0; i < n_out; ++i) {
7788 for (j = 0; j < bmap->n_eq; ++j) {
7789 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7791 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7792 total - (o_out + i + 1)) == -1)
7795 if (j >= bmap->n_eq)
7802 /* Check if the given map is obviously single-valued.
7804 int isl_map_fast_is_single_valued(__isl_keep isl_map *map)
7815 return isl_basic_map_fast_is_single_valued(map->p[0]);
7818 /* Check if the given map is single-valued.
7823 * and check if the result is a subset of the identity mapping.
7825 int isl_map_is_single_valued(__isl_keep isl_map *map)
7832 sv = isl_map_fast_is_single_valued(map);
7836 test = isl_map_reverse(isl_map_copy(map));
7837 test = isl_map_apply_range(test, isl_map_copy(map));
7839 dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
7840 id = isl_map_identity(dim);
7842 sv = isl_map_is_subset(test, id);
7850 int isl_map_is_bijective(__isl_keep isl_map *map)
7854 sv = isl_map_is_single_valued(map);
7858 map = isl_map_copy(map);
7859 map = isl_map_reverse(map);
7860 sv = isl_map_is_single_valued(map);
7866 int isl_set_is_singleton(__isl_keep isl_set *set)
7868 return isl_map_is_single_valued((isl_map *)set);
7871 int isl_map_is_translation(__isl_keep isl_map *map)
7876 delta = isl_map_deltas(isl_map_copy(map));
7877 ok = isl_set_is_singleton(delta);
7878 isl_set_free(delta);
7883 static int unique(isl_int *p, unsigned pos, unsigned len)
7885 if (isl_seq_first_non_zero(p, pos) != -1)
7887 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7892 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7901 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7904 nvar = isl_basic_set_dim(bset, isl_dim_set);
7905 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7906 for (j = 0; j < nvar; ++j) {
7907 int lower = 0, upper = 0;
7908 for (i = 0; i < bset->n_eq; ++i) {
7909 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7911 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7917 for (i = 0; i < bset->n_ineq; ++i) {
7918 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7920 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7922 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7927 if (!lower || !upper)
7934 int isl_set_is_box(__isl_keep isl_set *set)
7941 return isl_basic_set_is_box(set->p[0]);
7944 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7949 return isl_dim_is_wrapping(bset->dim);
7952 int isl_set_is_wrapping(__isl_keep isl_set *set)
7957 return isl_dim_is_wrapping(set->dim);
7960 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7962 bmap = isl_basic_map_cow(bmap);
7966 bmap->dim = isl_dim_wrap(bmap->dim);
7970 bmap = isl_basic_map_finalize(bmap);
7972 return (isl_basic_set *)bmap;
7974 isl_basic_map_free(bmap);
7978 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7982 map = isl_map_cow(map);
7986 for (i = 0; i < map->n; ++i) {
7987 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7991 map->dim = isl_dim_wrap(map->dim);
7995 return (isl_set *)map;
8001 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8003 bset = isl_basic_set_cow(bset);
8007 bset->dim = isl_dim_unwrap(bset->dim);
8011 bset = isl_basic_set_finalize(bset);
8013 return (isl_basic_map *)bset;
8015 isl_basic_set_free(bset);
8019 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8026 if (!isl_set_is_wrapping(set))
8027 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8030 set = isl_set_cow(set);
8034 for (i = 0; i < set->n; ++i) {
8035 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8040 set->dim = isl_dim_unwrap(set->dim);
8044 return (isl_map *)set;
8050 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8051 enum isl_dim_type type)
8056 if (!isl_dim_is_named_or_nested(bmap->dim, type))
8059 bmap = isl_basic_map_cow(bmap);
8063 bmap->dim = isl_dim_reset(bmap->dim, type);
8067 bmap = isl_basic_map_finalize(bmap);
8071 isl_basic_map_free(bmap);
8075 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8076 enum isl_dim_type type)
8083 if (!isl_dim_is_named_or_nested(map->dim, type))
8086 map = isl_map_cow(map);
8090 for (i = 0; i < map->n; ++i) {
8091 map->p[i] = isl_basic_map_reset(map->p[i], type);
8095 map->dim = isl_dim_reset(map->dim, type);
8105 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8110 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8113 bmap = isl_basic_map_cow(bmap);
8117 bmap->dim = isl_dim_flatten(bmap->dim);
8121 bmap = isl_basic_map_finalize(bmap);
8125 isl_basic_map_free(bmap);
8129 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8131 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8134 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8141 if (!map->dim->nested[0] && !map->dim->nested[1])
8144 map = isl_map_cow(map);
8148 for (i = 0; i < map->n; ++i) {
8149 map->p[i] = isl_basic_map_flatten(map->p[i]);
8153 map->dim = isl_dim_flatten(map->dim);
8163 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8165 return (isl_set *)isl_map_flatten((isl_map *)set);
8168 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8170 isl_dim *dim, *flat_dim;
8173 dim = isl_set_get_dim(set);
8174 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8175 map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8176 map = isl_map_intersect_domain(map, set);
8181 /* Extend the given dim_map with mappings for the divs in bmap.
8183 static __isl_give struct isl_dim_map *extend_dim_map(
8184 __isl_keep struct isl_dim_map *dim_map,
8185 __isl_keep isl_basic_map *bmap)
8188 struct isl_dim_map *res;
8191 offset = isl_basic_map_offset(bmap, isl_dim_div);
8193 res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
8197 for (i = 0; i < dim_map->len; ++i)
8198 res->pos[i] = dim_map->pos[i];
8199 for (i = 0; i < bmap->n_div; ++i)
8200 res->pos[dim_map->len + i] = offset + i;
8205 /* Extract a dim_map from a reordering.
8206 * We essentially need to reverse the mapping, and add an offset
8207 * of 1 for the constant term.
8209 __isl_give struct isl_dim_map *isl_dim_map_from_reordering(
8210 __isl_keep isl_reordering *exp)
8213 struct isl_dim_map *dim_map;
8218 dim_map = isl_dim_map_alloc(exp->dim->ctx, isl_dim_total(exp->dim));
8222 for (i = 0; i < exp->len; ++i)
8223 dim_map->pos[1 + exp->pos[i]] = 1 + i;
8228 /* Reorder the dimensions of "bmap" according to the given dim_map
8229 * and set the dimension specification to "dim".
8231 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8232 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8236 bmap = isl_basic_map_cow(bmap);
8237 if (!bmap || !dim || !dim_map)
8240 res = isl_basic_map_alloc_dim(dim,
8241 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8242 res = add_constraints_dim_map(res, bmap, dim_map);
8243 res = isl_basic_map_finalize(res);
8247 isl_basic_map_free(bmap);
8252 /* Reorder the dimensions of "map" according to given reordering.
8254 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8255 __isl_take isl_reordering *r)
8258 struct isl_dim_map *dim_map;
8260 map = isl_map_cow(map);
8261 dim_map = isl_dim_map_from_reordering(r);
8262 if (!map || !r || !dim_map)
8265 for (i = 0; i < map->n; ++i) {
8266 struct isl_dim_map *dim_map_i;
8268 dim_map_i = extend_dim_map(dim_map, map->p[i]);
8270 map->p[i] = isl_basic_map_realign(map->p[i],
8271 isl_dim_copy(r->dim), dim_map_i);
8277 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8279 isl_reordering_free(r);
8285 isl_reordering_free(r);
8289 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8290 __isl_take isl_reordering *r)
8292 return (isl_set *)isl_map_realign((isl_map *)set, r);
8295 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8296 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8297 enum isl_dim_type c2, enum isl_dim_type c3,
8298 enum isl_dim_type c4, enum isl_dim_type c5)
8300 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8301 struct isl_mat *mat;
8307 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8308 isl_basic_map_total_dim(bmap) + 1);
8311 for (i = 0; i < bmap->n_eq; ++i)
8312 for (j = 0, pos = 0; j < 5; ++j) {
8313 int off = isl_basic_map_offset(bmap, c[j]);
8314 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8315 isl_int_set(mat->row[i][pos],
8316 bmap->eq[i][off + k]);
8324 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8325 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8326 enum isl_dim_type c2, enum isl_dim_type c3,
8327 enum isl_dim_type c4, enum isl_dim_type c5)
8329 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8330 struct isl_mat *mat;
8336 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8337 isl_basic_map_total_dim(bmap) + 1);
8340 for (i = 0; i < bmap->n_ineq; ++i)
8341 for (j = 0, pos = 0; j < 5; ++j) {
8342 int off = isl_basic_map_offset(bmap, c[j]);
8343 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8344 isl_int_set(mat->row[i][pos],
8345 bmap->ineq[i][off + k]);
8353 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8354 __isl_take isl_dim *dim,
8355 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8356 enum isl_dim_type c2, enum isl_dim_type c3,
8357 enum isl_dim_type c4, enum isl_dim_type c5)
8359 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8360 isl_basic_map *bmap;
8366 if (!dim || !eq || !ineq)
8369 if (eq->n_col != ineq->n_col)
8370 isl_die(dim->ctx, isl_error_invalid,
8371 "equalities and inequalities matrices should have "
8372 "same number of columns", goto error);
8374 total = 1 + isl_dim_total(dim);
8376 if (eq->n_col < total)
8377 isl_die(dim->ctx, isl_error_invalid,
8378 "number of columns too small", goto error);
8380 extra = eq->n_col - total;
8382 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8383 eq->n_row, ineq->n_row);
8386 for (i = 0; i < extra; ++i) {
8387 k = isl_basic_map_alloc_div(bmap);
8390 isl_int_set_si(bmap->div[k][0], 0);
8392 for (i = 0; i < eq->n_row; ++i) {
8393 l = isl_basic_map_alloc_equality(bmap);
8396 for (j = 0, pos = 0; j < 5; ++j) {
8397 int off = isl_basic_map_offset(bmap, c[j]);
8398 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8399 isl_int_set(bmap->eq[l][off + k],
8405 for (i = 0; i < ineq->n_row; ++i) {
8406 l = isl_basic_map_alloc_inequality(bmap);
8409 for (j = 0, pos = 0; j < 5; ++j) {
8410 int off = isl_basic_map_offset(bmap, c[j]);
8411 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8412 isl_int_set(bmap->ineq[l][off + k],
8431 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8432 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8433 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8435 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8436 c1, c2, c3, c4, isl_dim_in);
8439 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8440 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8441 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8443 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8444 c1, c2, c3, c4, isl_dim_in);
8447 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8448 __isl_take isl_dim *dim,
8449 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8450 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8452 return (isl_basic_set*)
8453 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8454 c1, c2, c3, c4, isl_dim_in);
8457 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
8462 return isl_dim_can_zip(bmap->dim);
8465 int isl_map_can_zip(__isl_keep isl_map *map)
8470 return isl_dim_can_zip(map->dim);
8473 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
8474 * (A -> C) -> (B -> D).
8476 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
8485 if (!isl_basic_map_can_zip(bmap))
8486 isl_die(bmap->ctx, isl_error_invalid,
8487 "basic map cannot be zipped", goto error);
8488 pos = isl_basic_map_offset(bmap, isl_dim_in) +
8489 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
8490 n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
8491 n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
8492 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
8495 bmap->dim = isl_dim_zip(bmap->dim);
8500 isl_basic_map_free(bmap);
8504 /* Given a map (A -> B) -> (C -> D), return the corresponding map
8505 * (A -> C) -> (B -> D).
8507 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
8514 if (!isl_map_can_zip(map))
8515 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
8518 map = isl_map_cow(map);
8522 for (i = 0; i < map->n; ++i) {
8523 map->p[i] = isl_basic_map_zip(map->p[i]);
8528 map->dim = isl_dim_zip(map->dim);