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
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
17 #include "isl_space_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
34 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
37 case isl_dim_param: return dim->nparam;
38 case isl_dim_in: return dim->n_in;
39 case isl_dim_out: return dim->n_out;
40 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
45 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
48 case isl_dim_param: return 1;
49 case isl_dim_in: return 1 + dim->nparam;
50 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
55 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
56 enum isl_dim_type type)
61 case isl_dim_cst: return 1;
64 case isl_dim_out: return isl_space_dim(bmap->dim, type);
65 case isl_dim_div: return bmap->n_div;
66 case isl_dim_all: return isl_basic_map_total_dim(bmap);
71 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
73 return map ? n(map->dim, type) : 0;
76 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
78 return set ? n(set->dim, type) : 0;
81 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
82 enum isl_dim_type type)
84 isl_space *dim = bmap->dim;
86 case isl_dim_cst: return 0;
87 case isl_dim_param: return 1;
88 case isl_dim_in: return 1 + dim->nparam;
89 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
90 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
95 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
96 enum isl_dim_type type)
98 return isl_basic_map_offset(bset, type);
101 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
103 return pos(map->dim, type);
106 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_dim(bset, type);
112 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
114 return isl_basic_set_dim(bset, isl_dim_set);
117 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
119 return isl_basic_set_dim(bset, isl_dim_param);
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
124 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
127 unsigned isl_set_n_dim(__isl_keep isl_set *set)
129 return isl_set_dim(set, isl_dim_set);
132 unsigned isl_set_n_param(__isl_keep isl_set *set)
134 return isl_set_dim(set, isl_dim_param);
137 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
139 return bmap ? bmap->dim->n_in : 0;
142 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
144 return bmap ? bmap->dim->n_out : 0;
147 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
149 return bmap ? bmap->dim->nparam : 0;
152 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
154 return bmap ? bmap->n_div : 0;
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
159 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
162 unsigned isl_map_n_in(const struct isl_map *map)
164 return map ? map->dim->n_in : 0;
167 unsigned isl_map_n_out(const struct isl_map *map)
169 return map ? map->dim->n_out : 0;
172 unsigned isl_map_n_param(const struct isl_map *map)
174 return map ? map->dim->nparam : 0;
177 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
182 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
185 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
188 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
189 struct isl_basic_set *bset)
194 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
197 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
200 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
205 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
208 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
211 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
212 struct isl_basic_set *bset)
217 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
220 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
223 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
225 return bmap ? bmap->ctx : NULL;
228 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
230 return bset ? bset->ctx : NULL;
233 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
235 return map ? map->ctx : NULL;
238 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
240 return set ? set->ctx : NULL;
243 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
247 return isl_space_copy(bmap->dim);
250 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
254 return isl_space_copy(bset->dim);
257 __isl_give isl_local_space *isl_basic_map_get_local_space(
258 __isl_keep isl_basic_map *bmap)
267 total = isl_basic_map_total_dim(bmap);
268 ls = isl_local_space_alloc(isl_space_copy(bmap->dim), bmap->n_div);
272 for (i = 0; i < bmap->n_div; ++i)
273 isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total);
278 __isl_give isl_local_space *isl_basic_set_get_local_space(
279 __isl_keep isl_basic_set *bset)
281 return isl_basic_map_get_local_space(bset);
284 __isl_give isl_basic_map *isl_basic_map_from_local_space(
285 __isl_take isl_local_space *ls)
294 n_div = isl_local_space_dim(ls, isl_dim_div);
295 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
296 n_div, 0, 2 * n_div);
298 for (i = 0; i < n_div; ++i)
299 if (isl_basic_map_alloc_div(bmap) < 0)
302 for (i = 0; i < n_div; ++i) {
303 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
304 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
308 isl_local_space_free(ls);
311 isl_local_space_free(ls);
312 isl_basic_map_free(bmap);
316 __isl_give isl_basic_set *isl_basic_set_from_local_space(
317 __isl_take isl_local_space *ls)
319 return isl_basic_map_from_local_space(ls);
322 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
326 return isl_space_copy(map->dim);
329 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
333 return isl_space_copy(set->dim);
336 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
337 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
339 bmap = isl_basic_map_cow(bmap);
342 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
345 bmap = isl_basic_map_finalize(bmap);
348 isl_basic_map_free(bmap);
352 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
353 __isl_take isl_basic_set *bset, const char *s)
355 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
358 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
359 enum isl_dim_type type)
361 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
364 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
365 enum isl_dim_type type, const char *s)
369 map = isl_map_cow(map);
373 map->dim = isl_space_set_tuple_name(map->dim, type, s);
377 for (i = 0; i < map->n; ++i) {
378 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
389 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
390 enum isl_dim_type type)
392 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
395 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
398 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
401 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
402 enum isl_dim_type type, __isl_take isl_id *id)
404 map = isl_map_cow(map);
406 return isl_id_free(id);
408 map->dim = isl_space_set_tuple_id(map->dim, type, id);
410 return isl_map_reset_space(map, isl_space_copy(map->dim));
413 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
414 __isl_take isl_id *id)
416 return isl_map_set_tuple_id(set, isl_dim_set, id);
419 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
420 enum isl_dim_type type)
422 map = isl_map_cow(map);
426 map->dim = isl_space_reset_tuple_id(map->dim, type);
428 return isl_map_reset_space(map, isl_space_copy(map->dim));
431 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
433 return isl_map_reset_tuple_id(set, isl_dim_set);
436 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
438 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
441 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
442 enum isl_dim_type type)
444 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
447 int isl_set_has_tuple_id(__isl_keep isl_set *set)
449 return isl_map_has_tuple_id(set, isl_dim_set);
452 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
454 return isl_map_get_tuple_id(set, isl_dim_set);
457 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
459 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
462 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
464 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
467 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
468 enum isl_dim_type type, unsigned pos)
470 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
473 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
474 enum isl_dim_type type, unsigned pos)
476 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
479 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
480 enum isl_dim_type type, unsigned pos)
482 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
485 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
486 enum isl_dim_type type, unsigned pos)
488 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
491 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
492 __isl_take isl_basic_map *bmap,
493 enum isl_dim_type type, unsigned pos, const char *s)
497 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
502 isl_basic_map_free(bmap);
506 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
507 enum isl_dim_type type, unsigned pos, const char *s)
514 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
518 for (i = 0; i < map->n; ++i) {
519 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
530 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
531 __isl_take isl_basic_set *bset,
532 enum isl_dim_type type, unsigned pos, const char *s)
534 return (isl_basic_set *)isl_basic_map_set_dim_name(
535 (isl_basic_map *)bset, type, pos, s);
538 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
539 enum isl_dim_type type, unsigned pos, const char *s)
541 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
544 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
545 enum isl_dim_type type, unsigned pos)
547 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
550 int isl_map_has_dim_id(__isl_keep isl_map *map,
551 enum isl_dim_type type, unsigned pos)
553 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
556 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
557 enum isl_dim_type type, unsigned pos)
559 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
562 int isl_set_has_dim_id(__isl_keep isl_set *set,
563 enum isl_dim_type type, unsigned pos)
565 return isl_map_has_dim_id(set, type, pos);
568 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
569 enum isl_dim_type type, unsigned pos)
571 return isl_map_get_dim_id(set, type, pos);
574 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
575 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
577 map = isl_map_cow(map);
579 return isl_id_free(id);
581 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
583 return isl_map_reset_space(map, isl_space_copy(map->dim));
586 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
587 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
589 return isl_map_set_dim_id(set, type, pos, id);
592 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
593 __isl_keep isl_id *id)
597 return isl_space_find_dim_by_id(map->dim, type, id);
600 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
601 __isl_keep isl_id *id)
603 return isl_map_find_dim_by_id(set, type, id);
606 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
611 return isl_space_find_dim_by_name(map->dim, type, name);
614 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
618 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
621 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
623 return isl_basic_map_is_rational(bset);
626 /* Is this basic set a parameter domain?
628 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
632 return isl_space_is_params(bset->dim);
635 /* Is this set a parameter domain?
637 int isl_set_is_params(__isl_keep isl_set *set)
641 return isl_space_is_params(set->dim);
644 /* Is this map actually a parameter domain?
645 * Users should never call this function. Outside of isl,
646 * a map can never be a parameter domain.
648 int isl_map_is_params(__isl_keep isl_map *map)
652 return isl_space_is_params(map->dim);
655 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
656 struct isl_basic_map *bmap, unsigned extra,
657 unsigned n_eq, unsigned n_ineq)
660 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
665 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
666 if (isl_blk_is_error(bmap->block))
669 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
674 bmap->block2 = isl_blk_empty();
677 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
678 if (isl_blk_is_error(bmap->block2))
681 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
686 for (i = 0; i < n_ineq + n_eq; ++i)
687 bmap->ineq[i] = bmap->block.data + i * row_size;
689 for (i = 0; i < extra; ++i)
690 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
694 bmap->c_size = n_eq + n_ineq;
695 bmap->eq = bmap->ineq + n_ineq;
704 isl_basic_map_free(bmap);
708 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
709 unsigned nparam, unsigned dim, unsigned extra,
710 unsigned n_eq, unsigned n_ineq)
712 struct isl_basic_map *bmap;
715 space = isl_space_set_alloc(ctx, nparam, dim);
719 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
720 return (struct isl_basic_set *)bmap;
723 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
724 unsigned extra, unsigned n_eq, unsigned n_ineq)
726 struct isl_basic_map *bmap;
729 isl_assert(dim->ctx, dim->n_in == 0, goto error);
730 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
731 return (struct isl_basic_set *)bmap;
737 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
738 unsigned extra, unsigned n_eq, unsigned n_ineq)
740 struct isl_basic_map *bmap;
744 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
749 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
755 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
756 unsigned nparam, unsigned in, unsigned out, unsigned extra,
757 unsigned n_eq, unsigned n_ineq)
759 struct isl_basic_map *bmap;
762 dim = isl_space_alloc(ctx, nparam, in, out);
766 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
770 static void dup_constraints(
771 struct isl_basic_map *dst, struct isl_basic_map *src)
774 unsigned total = isl_basic_map_total_dim(src);
776 for (i = 0; i < src->n_eq; ++i) {
777 int j = isl_basic_map_alloc_equality(dst);
778 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
781 for (i = 0; i < src->n_ineq; ++i) {
782 int j = isl_basic_map_alloc_inequality(dst);
783 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
786 for (i = 0; i < src->n_div; ++i) {
787 int j = isl_basic_map_alloc_div(dst);
788 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
790 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
793 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
795 struct isl_basic_map *dup;
799 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
800 bmap->n_div, bmap->n_eq, bmap->n_ineq);
803 dup_constraints(dup, bmap);
804 dup->flags = bmap->flags;
805 dup->sample = isl_vec_copy(bmap->sample);
809 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
811 struct isl_basic_map *dup;
813 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
814 return (struct isl_basic_set *)dup;
817 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
822 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
826 return isl_basic_set_dup(bset);
829 struct isl_set *isl_set_copy(struct isl_set *set)
838 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
843 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
847 bmap = isl_basic_map_dup(bmap);
849 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
853 struct isl_map *isl_map_copy(struct isl_map *map)
862 void isl_basic_map_free(struct isl_basic_map *bmap)
870 isl_ctx_deref(bmap->ctx);
872 isl_blk_free(bmap->ctx, bmap->block2);
874 isl_blk_free(bmap->ctx, bmap->block);
875 isl_vec_free(bmap->sample);
876 isl_space_free(bmap->dim);
880 void isl_basic_set_free(struct isl_basic_set *bset)
882 isl_basic_map_free((struct isl_basic_map *)bset);
885 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
887 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
890 __isl_give isl_map *isl_map_align_params_map_map_and(
891 __isl_take isl_map *map1, __isl_take isl_map *map2,
892 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
893 __isl_take isl_map *map2))
897 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
898 return fn(map1, map2);
899 if (!isl_space_has_named_params(map1->dim) ||
900 !isl_space_has_named_params(map2->dim))
901 isl_die(map1->ctx, isl_error_invalid,
902 "unaligned unnamed parameters", goto error);
903 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
904 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
905 return fn(map1, map2);
912 static int align_params_map_map_and_test(__isl_keep isl_map *map1,
913 __isl_keep isl_map *map2,
914 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
920 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
921 return fn(map1, map2);
922 if (!isl_space_has_named_params(map1->dim) ||
923 !isl_space_has_named_params(map2->dim))
924 isl_die(map1->ctx, isl_error_invalid,
925 "unaligned unnamed parameters", return -1);
926 map1 = isl_map_copy(map1);
927 map2 = isl_map_copy(map2);
928 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
929 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
936 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
942 isl_assert(ctx, room_for_con(bmap, 1), return -1);
943 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
945 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
946 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
947 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
948 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
949 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
950 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
952 int j = isl_basic_map_alloc_inequality(bmap);
956 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
957 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
964 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
965 bmap->extra - bmap->n_div);
969 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
971 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
974 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
978 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
983 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
985 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
988 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
993 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
995 if (pos != bmap->n_eq - 1) {
997 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
998 bmap->eq[bmap->n_eq - 1] = t;
1004 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1006 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1009 void isl_basic_map_inequality_to_equality(
1010 struct isl_basic_map *bmap, unsigned pos)
1014 t = bmap->ineq[pos];
1015 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1016 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1021 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1022 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1023 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1024 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1027 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1029 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1032 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1034 struct isl_ctx *ctx;
1038 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1039 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1040 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1041 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1042 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1043 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1044 1 + isl_basic_map_total_dim(bmap),
1045 bmap->extra - bmap->n_div);
1046 return bmap->n_ineq++;
1049 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1051 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1054 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1058 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1063 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1065 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1068 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1073 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1075 if (pos != bmap->n_ineq - 1) {
1076 t = bmap->ineq[pos];
1077 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1078 bmap->ineq[bmap->n_ineq - 1] = t;
1079 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1085 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1087 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1090 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1095 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1098 k = isl_basic_map_alloc_equality(bmap);
1101 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1104 isl_basic_map_free(bmap);
1108 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1111 return (isl_basic_set *)
1112 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1115 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1120 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1123 k = isl_basic_map_alloc_inequality(bmap);
1126 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1129 isl_basic_map_free(bmap);
1133 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1136 return (isl_basic_set *)
1137 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1140 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1144 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1145 isl_seq_clr(bmap->div[bmap->n_div] +
1146 1 + 1 + isl_basic_map_total_dim(bmap),
1147 bmap->extra - bmap->n_div);
1148 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1149 return bmap->n_div++;
1152 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1154 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1157 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1161 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1166 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1168 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1171 /* Copy constraint from src to dst, putting the vars of src at offset
1172 * dim_off in dst and the divs of src at offset div_off in dst.
1173 * If both sets are actually map, then dim_off applies to the input
1176 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1177 struct isl_basic_map *src_map, isl_int *src,
1178 unsigned in_off, unsigned out_off, unsigned div_off)
1180 unsigned src_nparam = isl_basic_map_n_param(src_map);
1181 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1182 unsigned src_in = isl_basic_map_n_in(src_map);
1183 unsigned dst_in = isl_basic_map_n_in(dst_map);
1184 unsigned src_out = isl_basic_map_n_out(src_map);
1185 unsigned dst_out = isl_basic_map_n_out(dst_map);
1186 isl_int_set(dst[0], src[0]);
1187 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1188 if (dst_nparam > src_nparam)
1189 isl_seq_clr(dst+1+src_nparam,
1190 dst_nparam - src_nparam);
1191 isl_seq_clr(dst+1+dst_nparam, in_off);
1192 isl_seq_cpy(dst+1+dst_nparam+in_off,
1194 isl_min(dst_in-in_off, src_in));
1195 if (dst_in-in_off > src_in)
1196 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1197 dst_in - in_off - src_in);
1198 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1199 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1200 src+1+src_nparam+src_in,
1201 isl_min(dst_out-out_off, src_out));
1202 if (dst_out-out_off > src_out)
1203 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1204 dst_out - out_off - src_out);
1205 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1206 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1207 src+1+src_nparam+src_in+src_out,
1208 isl_min(dst_map->extra-div_off, src_map->n_div));
1209 if (dst_map->n_div-div_off > src_map->n_div)
1210 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1211 div_off+src_map->n_div,
1212 dst_map->n_div - div_off - src_map->n_div);
1215 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1216 struct isl_basic_map *src_map, isl_int *src,
1217 unsigned in_off, unsigned out_off, unsigned div_off)
1219 isl_int_set(dst[0], src[0]);
1220 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1223 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1224 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1229 if (!bmap1 || !bmap2)
1232 div_off = bmap1->n_div;
1234 for (i = 0; i < bmap2->n_eq; ++i) {
1235 int i1 = isl_basic_map_alloc_equality(bmap1);
1238 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1239 i_pos, o_pos, div_off);
1242 for (i = 0; i < bmap2->n_ineq; ++i) {
1243 int i1 = isl_basic_map_alloc_inequality(bmap1);
1246 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1247 i_pos, o_pos, div_off);
1250 for (i = 0; i < bmap2->n_div; ++i) {
1251 int i1 = isl_basic_map_alloc_div(bmap1);
1254 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1255 i_pos, o_pos, div_off);
1258 isl_basic_map_free(bmap2);
1263 isl_basic_map_free(bmap1);
1264 isl_basic_map_free(bmap2);
1268 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1269 struct isl_basic_set *bset2, unsigned pos)
1271 return (struct isl_basic_set *)
1272 add_constraints((struct isl_basic_map *)bset1,
1273 (struct isl_basic_map *)bset2, 0, pos);
1276 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1277 __isl_take isl_space *dim, unsigned extra,
1278 unsigned n_eq, unsigned n_ineq)
1280 struct isl_basic_map *ext;
1290 dims_ok = isl_space_is_equal(base->dim, dim) &&
1291 base->extra >= base->n_div + extra;
1293 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1294 room_for_ineq(base, n_ineq)) {
1295 isl_space_free(dim);
1299 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1300 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1301 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1302 extra += base->extra;
1304 n_ineq += base->n_ineq;
1306 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1312 ext->sample = isl_vec_copy(base->sample);
1313 flags = base->flags;
1314 ext = add_constraints(ext, base, 0, 0);
1317 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1323 isl_space_free(dim);
1324 isl_basic_map_free(base);
1328 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1329 __isl_take isl_space *dim, unsigned extra,
1330 unsigned n_eq, unsigned n_ineq)
1332 return (struct isl_basic_set *)
1333 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1334 extra, n_eq, n_ineq);
1337 struct isl_basic_map *isl_basic_map_extend_constraints(
1338 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1342 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1346 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1347 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1348 unsigned n_eq, unsigned n_ineq)
1350 struct isl_basic_map *bmap;
1355 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1359 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1362 isl_basic_map_free(base);
1366 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1367 unsigned nparam, unsigned dim, unsigned extra,
1368 unsigned n_eq, unsigned n_ineq)
1370 return (struct isl_basic_set *)
1371 isl_basic_map_extend((struct isl_basic_map *)base,
1372 nparam, 0, dim, extra, n_eq, n_ineq);
1375 struct isl_basic_set *isl_basic_set_extend_constraints(
1376 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1378 return (struct isl_basic_set *)
1379 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1383 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1385 return (struct isl_basic_set *)
1386 isl_basic_map_cow((struct isl_basic_map *)bset);
1389 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1394 if (bmap->ref > 1) {
1396 bmap = isl_basic_map_dup(bmap);
1399 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1403 struct isl_set *isl_set_cow(struct isl_set *set)
1411 return isl_set_dup(set);
1414 struct isl_map *isl_map_cow(struct isl_map *map)
1422 return isl_map_dup(map);
1425 static void swap_vars(struct isl_blk blk, isl_int *a,
1426 unsigned a_len, unsigned b_len)
1428 isl_seq_cpy(blk.data, a+a_len, b_len);
1429 isl_seq_cpy(blk.data+b_len, a, a_len);
1430 isl_seq_cpy(a, blk.data, b_len+a_len);
1433 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1434 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1442 isl_assert(bmap->ctx,
1443 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1445 if (n1 == 0 || n2 == 0)
1448 bmap = isl_basic_map_cow(bmap);
1452 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1453 if (isl_blk_is_error(blk))
1456 for (i = 0; i < bmap->n_eq; ++i)
1458 bmap->eq[i] + pos, n1, n2);
1460 for (i = 0; i < bmap->n_ineq; ++i)
1462 bmap->ineq[i] + pos, n1, n2);
1464 for (i = 0; i < bmap->n_div; ++i)
1466 bmap->div[i]+1 + pos, n1, n2);
1468 isl_blk_free(bmap->ctx, blk);
1470 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1471 bmap = isl_basic_map_gauss(bmap, NULL);
1472 return isl_basic_map_finalize(bmap);
1474 isl_basic_map_free(bmap);
1478 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1479 __isl_take isl_basic_set *bset, unsigned n)
1484 nparam = isl_basic_set_n_param(bset);
1485 dim = isl_basic_set_n_dim(bset);
1486 isl_assert(bset->ctx, n <= dim, goto error);
1488 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1490 isl_basic_set_free(bset);
1494 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1500 total = isl_basic_map_total_dim(bmap);
1501 isl_basic_map_free_div(bmap, bmap->n_div);
1502 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1504 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1506 i = isl_basic_map_alloc_equality(bmap);
1510 isl_int_set_si(bmap->eq[i][0], 1);
1511 isl_seq_clr(bmap->eq[i]+1, total);
1512 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1513 isl_vec_free(bmap->sample);
1514 bmap->sample = NULL;
1515 return isl_basic_map_finalize(bmap);
1517 isl_basic_map_free(bmap);
1521 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1523 return (struct isl_basic_set *)
1524 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1527 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1530 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1531 isl_int *t = bmap->div[a];
1532 bmap->div[a] = bmap->div[b];
1535 for (i = 0; i < bmap->n_eq; ++i)
1536 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1538 for (i = 0; i < bmap->n_ineq; ++i)
1539 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1541 for (i = 0; i < bmap->n_div; ++i)
1542 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1543 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1546 /* Eliminate the specified n dimensions starting at first from the
1547 * constraints using Fourier-Motzkin. The dimensions themselves
1550 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1551 enum isl_dim_type type, unsigned first, unsigned n)
1560 map = isl_map_cow(map);
1564 for (i = 0; i < map->n; ++i) {
1565 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1575 /* Eliminate the specified n dimensions starting at first from the
1576 * constraints using Fourier-Motzkin. The dimensions themselves
1579 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1580 enum isl_dim_type type, unsigned first, unsigned n)
1582 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1585 /* Eliminate the specified n dimensions starting at first from the
1586 * constraints using Fourier-Motzkin. The dimensions themselves
1589 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1590 unsigned first, unsigned n)
1592 return isl_set_eliminate(set, isl_dim_set, first, n);
1595 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1596 __isl_take isl_basic_map *bmap)
1600 bmap = isl_basic_map_eliminate_vars(bmap,
1601 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1605 return isl_basic_map_finalize(bmap);
1608 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1609 __isl_take isl_basic_set *bset)
1611 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1612 (struct isl_basic_map *)bset);
1615 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1624 map = isl_map_cow(map);
1628 for (i = 0; i < map->n; ++i) {
1629 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1639 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1641 return isl_map_remove_divs(set);
1644 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1645 enum isl_dim_type type, unsigned first, unsigned n)
1649 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1651 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1653 bmap = isl_basic_map_eliminate_vars(bmap,
1654 isl_basic_map_offset(bmap, type) - 1 + first, n);
1657 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1659 bmap = isl_basic_map_drop(bmap, type, first, n);
1662 isl_basic_map_free(bmap);
1666 /* Return true if the definition of the given div (recursively) involves
1667 * any of the given variables.
1669 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1670 unsigned first, unsigned n)
1673 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1675 if (isl_int_is_zero(bmap->div[div][0]))
1677 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1680 for (i = bmap->n_div - 1; i >= 0; --i) {
1681 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1683 if (div_involves_vars(bmap, i, first, n))
1690 /* Remove all divs (recursively) involving any of the given dimensions
1691 * in their definitions.
1693 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1694 __isl_take isl_basic_map *bmap,
1695 enum isl_dim_type type, unsigned first, unsigned n)
1701 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1703 first += isl_basic_map_offset(bmap, type);
1705 for (i = bmap->n_div - 1; i >= 0; --i) {
1706 if (!div_involves_vars(bmap, i, first, n))
1708 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1716 isl_basic_map_free(bmap);
1720 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1721 enum isl_dim_type type, unsigned first, unsigned n)
1730 map = isl_map_cow(map);
1734 for (i = 0; i < map->n; ++i) {
1735 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1746 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1747 enum isl_dim_type type, unsigned first, unsigned n)
1749 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1753 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1754 enum isl_dim_type type, unsigned first, unsigned n)
1761 if (first + n > isl_basic_map_dim(bmap, type))
1762 isl_die(bmap->ctx, isl_error_invalid,
1763 "index out of bounds", return -1);
1765 first += isl_basic_map_offset(bmap, type);
1766 for (i = 0; i < bmap->n_eq; ++i)
1767 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1769 for (i = 0; i < bmap->n_ineq; ++i)
1770 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1776 int isl_map_involves_dims(__isl_keep isl_map *map,
1777 enum isl_dim_type type, unsigned first, unsigned n)
1784 if (first + n > isl_map_dim(map, type))
1785 isl_die(map->ctx, isl_error_invalid,
1786 "index out of bounds", return -1);
1788 for (i = 0; i < map->n; ++i) {
1789 int involves = isl_basic_map_involves_dims(map->p[i],
1791 if (involves < 0 || involves)
1798 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1799 enum isl_dim_type type, unsigned first, unsigned n)
1801 return isl_basic_map_involves_dims(bset, type, first, n);
1804 int isl_set_involves_dims(__isl_keep isl_set *set,
1805 enum isl_dim_type type, unsigned first, unsigned n)
1807 return isl_map_involves_dims(set, type, first, n);
1810 /* Return true if the definition of the given div is unknown or depends
1813 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1816 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1818 if (isl_int_is_zero(bmap->div[div][0]))
1821 for (i = bmap->n_div - 1; i >= 0; --i) {
1822 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1824 if (div_is_unknown(bmap, i))
1831 /* Remove all divs that are unknown or defined in terms of unknown divs.
1833 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1834 __isl_take isl_basic_map *bmap)
1841 for (i = bmap->n_div - 1; i >= 0; --i) {
1842 if (!div_is_unknown(bmap, i))
1844 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1850 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1859 map = isl_map_cow(map);
1863 for (i = 0; i < map->n; ++i) {
1864 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1874 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1876 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1879 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1880 __isl_take isl_basic_set *bset,
1881 enum isl_dim_type type, unsigned first, unsigned n)
1883 return (isl_basic_set *)
1884 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1887 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1888 enum isl_dim_type type, unsigned first, unsigned n)
1895 map = isl_map_cow(map);
1898 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1900 for (i = 0; i < map->n; ++i) {
1901 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1902 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1906 map = isl_map_drop(map, type, first, n);
1913 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1914 enum isl_dim_type type, unsigned first, unsigned n)
1916 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1919 /* Project out n inputs starting at first using Fourier-Motzkin */
1920 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1921 unsigned first, unsigned n)
1923 return isl_map_remove_dims(map, isl_dim_in, first, n);
1926 static void dump_term(struct isl_basic_map *bmap,
1927 isl_int c, int pos, FILE *out)
1930 unsigned in = isl_basic_map_n_in(bmap);
1931 unsigned dim = in + isl_basic_map_n_out(bmap);
1932 unsigned nparam = isl_basic_map_n_param(bmap);
1934 isl_int_print(out, c, 0);
1936 if (!isl_int_is_one(c))
1937 isl_int_print(out, c, 0);
1938 if (pos < 1 + nparam) {
1939 name = isl_space_get_dim_name(bmap->dim,
1940 isl_dim_param, pos - 1);
1942 fprintf(out, "%s", name);
1944 fprintf(out, "p%d", pos - 1);
1945 } else if (pos < 1 + nparam + in)
1946 fprintf(out, "i%d", pos - 1 - nparam);
1947 else if (pos < 1 + nparam + dim)
1948 fprintf(out, "o%d", pos - 1 - nparam - in);
1950 fprintf(out, "e%d", pos - 1 - nparam - dim);
1954 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1955 int sign, FILE *out)
1959 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1963 for (i = 0, first = 1; i < len; ++i) {
1964 if (isl_int_sgn(c[i]) * sign <= 0)
1967 fprintf(out, " + ");
1969 isl_int_abs(v, c[i]);
1970 dump_term(bmap, v, i, out);
1977 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1978 const char *op, FILE *out, int indent)
1982 fprintf(out, "%*s", indent, "");
1984 dump_constraint_sign(bmap, c, 1, out);
1985 fprintf(out, " %s ", op);
1986 dump_constraint_sign(bmap, c, -1, out);
1990 for (i = bmap->n_div; i < bmap->extra; ++i) {
1991 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
1993 fprintf(out, "%*s", indent, "");
1994 fprintf(out, "ERROR: unused div coefficient not zero\n");
1999 static void dump_constraints(struct isl_basic_map *bmap,
2000 isl_int **c, unsigned n,
2001 const char *op, FILE *out, int indent)
2005 for (i = 0; i < n; ++i)
2006 dump_constraint(bmap, c[i], op, out, indent);
2009 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2013 unsigned total = isl_basic_map_total_dim(bmap);
2015 for (j = 0; j < 1 + total; ++j) {
2016 if (isl_int_is_zero(exp[j]))
2018 if (!first && isl_int_is_pos(exp[j]))
2020 dump_term(bmap, exp[j], j, out);
2025 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2029 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2030 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2032 for (i = 0; i < bmap->n_div; ++i) {
2033 fprintf(out, "%*s", indent, "");
2034 fprintf(out, "e%d = [(", i);
2035 dump_affine(bmap, bmap->div[i]+1, out);
2037 isl_int_print(out, bmap->div[i][0], 0);
2038 fprintf(out, "]\n");
2042 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2043 FILE *out, int indent)
2046 fprintf(out, "null basic set\n");
2050 fprintf(out, "%*s", indent, "");
2051 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2052 bset->ref, bset->dim->nparam, bset->dim->n_out,
2053 bset->extra, bset->flags);
2054 dump((struct isl_basic_map *)bset, out, indent);
2057 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2058 FILE *out, int indent)
2061 fprintf(out, "null basic map\n");
2065 fprintf(out, "%*s", indent, "");
2066 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2067 "flags: %x, n_name: %d\n",
2069 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2070 bmap->extra, bmap->flags, bmap->dim->n_id);
2071 dump(bmap, out, indent);
2074 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2079 total = isl_basic_map_total_dim(bmap);
2080 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2081 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2082 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2083 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2087 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2090 struct isl_set *set;
2094 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2095 isl_assert(dim->ctx, n >= 0, goto error);
2096 set = isl_alloc(dim->ctx, struct isl_set,
2097 sizeof(struct isl_set) +
2098 (n - 1) * sizeof(struct isl_basic_set *));
2102 set->ctx = dim->ctx;
2103 isl_ctx_ref(set->ctx);
2111 isl_space_free(dim);
2115 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2116 unsigned nparam, unsigned dim, int n, unsigned flags)
2118 struct isl_set *set;
2121 dims = isl_space_alloc(ctx, nparam, 0, dim);
2125 set = isl_set_alloc_space(dims, n, flags);
2129 /* Make sure "map" has room for at least "n" more basic maps.
2131 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2134 struct isl_map *grown = NULL;
2138 isl_assert(map->ctx, n >= 0, goto error);
2139 if (map->n + n <= map->size)
2141 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2144 for (i = 0; i < map->n; ++i) {
2145 grown->p[i] = isl_basic_map_copy(map->p[i]);
2153 isl_map_free(grown);
2158 /* Make sure "set" has room for at least "n" more basic sets.
2160 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2162 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2165 struct isl_set *isl_set_dup(struct isl_set *set)
2168 struct isl_set *dup;
2173 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2176 for (i = 0; i < set->n; ++i)
2177 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2181 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2183 return isl_map_from_basic_map(bset);
2186 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2188 struct isl_map *map;
2193 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2194 return isl_map_add_basic_map(map, bmap);
2197 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2198 __isl_take isl_basic_set *bset)
2200 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2201 (struct isl_basic_map *)bset);
2204 void isl_set_free(struct isl_set *set)
2214 isl_ctx_deref(set->ctx);
2215 for (i = 0; i < set->n; ++i)
2216 isl_basic_set_free(set->p[i]);
2217 isl_space_free(set->dim);
2221 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2226 fprintf(out, "null set\n");
2230 fprintf(out, "%*s", indent, "");
2231 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2232 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2234 for (i = 0; i < set->n; ++i) {
2235 fprintf(out, "%*s", indent, "");
2236 fprintf(out, "basic set %d:\n", i);
2237 isl_basic_set_print_internal(set->p[i], out, indent+4);
2241 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2246 fprintf(out, "null map\n");
2250 fprintf(out, "%*s", indent, "");
2251 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2252 "flags: %x, n_name: %d\n",
2253 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2254 map->dim->n_out, map->flags, map->dim->n_id);
2255 for (i = 0; i < map->n; ++i) {
2256 fprintf(out, "%*s", indent, "");
2257 fprintf(out, "basic map %d:\n", i);
2258 isl_basic_map_print_internal(map->p[i], out, indent+4);
2262 struct isl_basic_map *isl_basic_map_intersect_domain(
2263 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2265 struct isl_basic_map *bmap_domain;
2270 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2271 bset->dim, isl_dim_param), goto error);
2273 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2274 isl_assert(bset->ctx,
2275 isl_basic_map_compatible_domain(bmap, bset), goto error);
2277 bmap = isl_basic_map_cow(bmap);
2280 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2281 bset->n_div, bset->n_eq, bset->n_ineq);
2282 bmap_domain = isl_basic_map_from_domain(bset);
2283 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2285 bmap = isl_basic_map_simplify(bmap);
2286 return isl_basic_map_finalize(bmap);
2288 isl_basic_map_free(bmap);
2289 isl_basic_set_free(bset);
2293 struct isl_basic_map *isl_basic_map_intersect_range(
2294 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2296 struct isl_basic_map *bmap_range;
2301 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2302 bset->dim, isl_dim_param), goto error);
2304 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2305 isl_assert(bset->ctx,
2306 isl_basic_map_compatible_range(bmap, bset), goto error);
2308 if (isl_basic_set_is_universe(bset)) {
2309 isl_basic_set_free(bset);
2313 bmap = isl_basic_map_cow(bmap);
2316 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2317 bset->n_div, bset->n_eq, bset->n_ineq);
2318 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2319 bmap = add_constraints(bmap, bmap_range, 0, 0);
2321 bmap = isl_basic_map_simplify(bmap);
2322 return isl_basic_map_finalize(bmap);
2324 isl_basic_map_free(bmap);
2325 isl_basic_set_free(bset);
2329 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2335 total = 1 + isl_basic_map_total_dim(bmap);
2336 if (total != vec->size)
2341 for (i = 0; i < bmap->n_eq; ++i) {
2342 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2343 if (!isl_int_is_zero(s)) {
2349 for (i = 0; i < bmap->n_ineq; ++i) {
2350 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2351 if (isl_int_is_neg(s)) {
2362 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2364 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2367 struct isl_basic_map *isl_basic_map_intersect(
2368 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2370 struct isl_vec *sample = NULL;
2372 if (!bmap1 || !bmap2)
2375 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2376 bmap2->dim, isl_dim_param), goto error);
2377 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2378 isl_space_dim(bmap1->dim, isl_dim_param) &&
2379 isl_space_dim(bmap2->dim, isl_dim_all) !=
2380 isl_space_dim(bmap2->dim, isl_dim_param))
2381 return isl_basic_map_intersect(bmap2, bmap1);
2383 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2384 isl_space_dim(bmap2->dim, isl_dim_param))
2385 isl_assert(bmap1->ctx,
2386 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2388 if (bmap1->sample &&
2389 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2390 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2391 sample = isl_vec_copy(bmap1->sample);
2392 else if (bmap2->sample &&
2393 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2394 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2395 sample = isl_vec_copy(bmap2->sample);
2397 bmap1 = isl_basic_map_cow(bmap1);
2400 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2401 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2402 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2405 isl_vec_free(sample);
2407 isl_vec_free(bmap1->sample);
2408 bmap1->sample = sample;
2411 bmap1 = isl_basic_map_simplify(bmap1);
2412 return isl_basic_map_finalize(bmap1);
2415 isl_vec_free(sample);
2416 isl_basic_map_free(bmap1);
2417 isl_basic_map_free(bmap2);
2421 struct isl_basic_set *isl_basic_set_intersect(
2422 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2424 return (struct isl_basic_set *)
2425 isl_basic_map_intersect(
2426 (struct isl_basic_map *)bset1,
2427 (struct isl_basic_map *)bset2);
2430 /* Special case of isl_map_intersect, where both map1 and map2
2431 * are convex, without any divs and such that either map1 or map2
2432 * contains a single constraint. This constraint is then simply
2433 * added to the other map.
2435 static __isl_give isl_map *map_intersect_add_constraint(
2436 __isl_take isl_map *map1, __isl_take isl_map *map2)
2438 isl_assert(map1->ctx, map1->n == 1, goto error);
2439 isl_assert(map2->ctx, map1->n == 1, goto error);
2440 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2441 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2443 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2444 return isl_map_intersect(map2, map1);
2446 isl_assert(map2->ctx,
2447 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2449 map1 = isl_map_cow(map1);
2452 if (isl_map_plain_is_empty(map1)) {
2456 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2457 if (map2->p[0]->n_eq == 1)
2458 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2460 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2461 map2->p[0]->ineq[0]);
2463 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2464 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2468 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2469 isl_basic_map_free(map1->p[0]);
2482 /* map2 may be either a parameter domain or a map living in the same
2485 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2486 __isl_take isl_map *map2)
2489 struct isl_map *result;
2495 if (isl_map_plain_is_empty(map1) &&
2496 isl_space_is_equal(map1->dim, map2->dim)) {
2500 if (isl_map_plain_is_empty(map2) &&
2501 isl_space_is_equal(map1->dim, map2->dim)) {
2506 if (map1->n == 1 && map2->n == 1 &&
2507 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2508 isl_space_is_equal(map1->dim, map2->dim) &&
2509 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2510 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2511 return map_intersect_add_constraint(map1, map2);
2513 if (isl_space_dim(map2->dim, isl_dim_all) !=
2514 isl_space_dim(map2->dim, isl_dim_param))
2515 isl_assert(map1->ctx,
2516 isl_space_is_equal(map1->dim, map2->dim), goto error);
2518 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2519 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2520 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2522 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2523 map1->n * map2->n, flags);
2526 for (i = 0; i < map1->n; ++i)
2527 for (j = 0; j < map2->n; ++j) {
2528 struct isl_basic_map *part;
2529 part = isl_basic_map_intersect(
2530 isl_basic_map_copy(map1->p[i]),
2531 isl_basic_map_copy(map2->p[j]));
2532 if (isl_basic_map_is_empty(part))
2533 isl_basic_map_free(part);
2535 result = isl_map_add_basic_map(result, part);
2548 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2549 __isl_take isl_map *map2)
2553 if (!isl_space_is_equal(map1->dim, map2->dim))
2554 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2555 "spaces don't match", goto error);
2556 return map_intersect_internal(map1, map2);
2563 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2564 __isl_take isl_map *map2)
2566 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2569 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2571 return (struct isl_set *)
2572 isl_map_intersect((struct isl_map *)set1,
2573 (struct isl_map *)set2);
2576 /* map_intersect_internal accepts intersections
2577 * with parameter domains, so we can just call that function.
2579 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2580 __isl_take isl_set *params)
2582 return map_intersect_internal(map, params);
2585 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2586 __isl_take isl_map *map2)
2588 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2591 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2592 __isl_take isl_set *params)
2594 return isl_map_intersect_params(set, params);
2597 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2600 struct isl_basic_set *bset;
2605 bmap = isl_basic_map_cow(bmap);
2608 dim = isl_space_reverse(isl_space_copy(bmap->dim));
2609 in = isl_basic_map_n_in(bmap);
2610 bset = isl_basic_set_from_basic_map(bmap);
2611 bset = isl_basic_set_swap_vars(bset, in);
2612 return isl_basic_map_from_basic_set(bset, dim);
2615 static __isl_give isl_basic_map *basic_map_space_reset(
2616 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2620 if (!isl_space_is_named_or_nested(bmap->dim, type))
2623 space = isl_basic_map_get_space(bmap);
2624 space = isl_space_reset(space, type);
2625 bmap = isl_basic_map_reset_space(bmap, space);
2629 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2630 enum isl_dim_type type, unsigned pos, unsigned n)
2633 struct isl_basic_map *res;
2634 struct isl_dim_map *dim_map;
2635 unsigned total, off;
2636 enum isl_dim_type t;
2639 return basic_map_space_reset(bmap, type);
2644 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
2646 total = isl_basic_map_total_dim(bmap) + n;
2647 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2649 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2651 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2653 unsigned size = isl_basic_map_dim(bmap, t);
2654 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2656 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2657 pos, size - pos, off + pos + n);
2659 off += isl_space_dim(res_dim, t);
2661 isl_dim_map_div(dim_map, bmap, off);
2663 res = isl_basic_map_alloc_space(res_dim,
2664 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2665 if (isl_basic_map_is_rational(bmap))
2666 res = isl_basic_map_set_rational(res);
2667 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2668 return isl_basic_map_finalize(res);
2671 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2672 enum isl_dim_type type, unsigned n)
2676 return isl_basic_map_insert(bmap, type,
2677 isl_basic_map_dim(bmap, type), n);
2680 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2681 enum isl_dim_type type, unsigned n)
2685 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2686 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2688 isl_basic_set_free(bset);
2692 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
2693 enum isl_dim_type type)
2697 if (!isl_space_is_named_or_nested(map->dim, type))
2700 space = isl_map_get_space(map);
2701 space = isl_space_reset(space, type);
2702 map = isl_map_reset_space(map, space);
2706 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
2707 enum isl_dim_type type, unsigned pos, unsigned n)
2712 return map_space_reset(map, type);
2714 map = isl_map_cow(map);
2718 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
2722 for (i = 0; i < map->n; ++i) {
2723 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2734 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
2735 enum isl_dim_type type, unsigned pos, unsigned n)
2737 return isl_map_insert_dims(set, type, pos, n);
2740 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2741 enum isl_dim_type type, unsigned n)
2745 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
2748 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2749 enum isl_dim_type type, unsigned n)
2753 isl_assert(set->ctx, type != isl_dim_in, goto error);
2754 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2760 __isl_give isl_basic_map *isl_basic_map_move_dims(
2761 __isl_take isl_basic_map *bmap,
2762 enum isl_dim_type dst_type, unsigned dst_pos,
2763 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2765 struct isl_dim_map *dim_map;
2766 struct isl_basic_map *res;
2767 enum isl_dim_type t;
2768 unsigned total, off;
2775 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2778 if (dst_type == src_type && dst_pos == src_pos)
2781 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2783 if (pos(bmap->dim, dst_type) + dst_pos ==
2784 pos(bmap->dim, src_type) + src_pos +
2785 ((src_type < dst_type) ? n : 0)) {
2786 bmap = isl_basic_map_cow(bmap);
2790 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2791 src_type, src_pos, n);
2795 bmap = isl_basic_map_finalize(bmap);
2800 total = isl_basic_map_total_dim(bmap);
2801 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2804 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2805 unsigned size = isl_space_dim(bmap->dim, t);
2806 if (t == dst_type) {
2807 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2810 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2813 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2814 dst_pos, size - dst_pos, off);
2815 off += size - dst_pos;
2816 } else if (t == src_type) {
2817 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2820 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2821 src_pos + n, size - src_pos - n, off);
2822 off += size - src_pos - n;
2824 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2828 isl_dim_map_div(dim_map, bmap, off);
2830 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2831 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2832 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2834 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
2835 src_type, src_pos, n);
2839 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2840 bmap = isl_basic_map_gauss(bmap, NULL);
2841 bmap = isl_basic_map_finalize(bmap);
2845 isl_basic_map_free(bmap);
2849 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2850 enum isl_dim_type dst_type, unsigned dst_pos,
2851 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2853 return (isl_basic_set *)isl_basic_map_move_dims(
2854 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2857 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2858 enum isl_dim_type dst_type, unsigned dst_pos,
2859 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2863 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2864 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2865 src_type, src_pos, n);
2871 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2872 enum isl_dim_type dst_type, unsigned dst_pos,
2873 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2882 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2885 if (dst_type == src_type && dst_pos == src_pos)
2888 isl_assert(map->ctx, dst_type != src_type, goto error);
2890 map = isl_map_cow(map);
2894 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2898 for (i = 0; i < map->n; ++i) {
2899 map->p[i] = isl_basic_map_move_dims(map->p[i],
2901 src_type, src_pos, n);
2912 /* Move the specified dimensions to the last columns right before
2913 * the divs. Don't change the dimension specification of bmap.
2914 * That's the responsibility of the caller.
2916 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2917 enum isl_dim_type type, unsigned first, unsigned n)
2919 struct isl_dim_map *dim_map;
2920 struct isl_basic_map *res;
2921 enum isl_dim_type t;
2922 unsigned total, off;
2926 if (pos(bmap->dim, type) + first + n ==
2927 1 + isl_space_dim(bmap->dim, isl_dim_all))
2930 total = isl_basic_map_total_dim(bmap);
2931 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2934 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2935 unsigned size = isl_space_dim(bmap->dim, t);
2937 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2940 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2941 first, n, total - bmap->n_div - n);
2942 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2943 first + n, size - (first + n), off);
2944 off += size - (first + n);
2946 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2950 isl_dim_map_div(dim_map, bmap, off + n);
2952 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
2953 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2954 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2958 /* Turn the n dimensions of type type, starting at first
2959 * into existentially quantified variables.
2961 __isl_give isl_basic_map *isl_basic_map_project_out(
2962 __isl_take isl_basic_map *bmap,
2963 enum isl_dim_type type, unsigned first, unsigned n)
2976 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2977 return isl_basic_map_remove_dims(bmap, type, first, n);
2979 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2982 bmap = move_last(bmap, type, first, n);
2983 bmap = isl_basic_map_cow(bmap);
2987 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
2988 old = bmap->block2.data;
2989 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2990 (bmap->extra + n) * (1 + row_size));
2991 if (!bmap->block2.data)
2993 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2996 for (i = 0; i < n; ++i) {
2997 new_div[i] = bmap->block2.data +
2998 (bmap->extra + i) * (1 + row_size);
2999 isl_seq_clr(new_div[i], 1 + row_size);
3001 for (i = 0; i < bmap->extra; ++i)
3002 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3004 bmap->div = new_div;
3008 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3011 bmap = isl_basic_map_simplify(bmap);
3012 bmap = isl_basic_map_drop_redundant_divs(bmap);
3013 return isl_basic_map_finalize(bmap);
3015 isl_basic_map_free(bmap);
3019 /* Turn the n dimensions of type type, starting at first
3020 * into existentially quantified variables.
3022 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3023 enum isl_dim_type type, unsigned first, unsigned n)
3025 return (isl_basic_set *)isl_basic_map_project_out(
3026 (isl_basic_map *)bset, type, first, n);
3029 /* Turn the n dimensions of type type, starting at first
3030 * into existentially quantified variables.
3032 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3033 enum isl_dim_type type, unsigned first, unsigned n)
3043 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3045 map = isl_map_cow(map);
3049 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3053 for (i = 0; i < map->n; ++i) {
3054 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3065 /* Turn the n dimensions of type type, starting at first
3066 * into existentially quantified variables.
3068 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3069 enum isl_dim_type type, unsigned first, unsigned n)
3071 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3074 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3078 for (i = 0; i < n; ++i) {
3079 j = isl_basic_map_alloc_div(bmap);
3082 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3086 isl_basic_map_free(bmap);
3090 struct isl_basic_map *isl_basic_map_apply_range(
3091 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3093 isl_space *dim_result = NULL;
3094 struct isl_basic_map *bmap;
3095 unsigned n_in, n_out, n, nparam, total, pos;
3096 struct isl_dim_map *dim_map1, *dim_map2;
3098 if (!bmap1 || !bmap2)
3101 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3102 isl_space_copy(bmap2->dim));
3104 n_in = isl_basic_map_n_in(bmap1);
3105 n_out = isl_basic_map_n_out(bmap2);
3106 n = isl_basic_map_n_out(bmap1);
3107 nparam = isl_basic_map_n_param(bmap1);
3109 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3110 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3111 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3112 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3113 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3114 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3115 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3116 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3117 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3118 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3119 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3121 bmap = isl_basic_map_alloc_space(dim_result,
3122 bmap1->n_div + bmap2->n_div + n,
3123 bmap1->n_eq + bmap2->n_eq,
3124 bmap1->n_ineq + bmap2->n_ineq);
3125 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3126 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3127 bmap = add_divs(bmap, n);
3128 bmap = isl_basic_map_simplify(bmap);
3129 bmap = isl_basic_map_drop_redundant_divs(bmap);
3130 return isl_basic_map_finalize(bmap);
3132 isl_basic_map_free(bmap1);
3133 isl_basic_map_free(bmap2);
3137 struct isl_basic_set *isl_basic_set_apply(
3138 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3143 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3146 return (struct isl_basic_set *)
3147 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3149 isl_basic_set_free(bset);
3150 isl_basic_map_free(bmap);
3154 struct isl_basic_map *isl_basic_map_apply_domain(
3155 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3157 if (!bmap1 || !bmap2)
3160 isl_assert(bmap1->ctx,
3161 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3162 isl_assert(bmap1->ctx,
3163 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3166 bmap1 = isl_basic_map_reverse(bmap1);
3167 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3168 return isl_basic_map_reverse(bmap1);
3170 isl_basic_map_free(bmap1);
3171 isl_basic_map_free(bmap2);
3175 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3176 * A \cap B -> f(A) + f(B)
3178 struct isl_basic_map *isl_basic_map_sum(
3179 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3181 unsigned n_in, n_out, nparam, total, pos;
3182 struct isl_basic_map *bmap = NULL;
3183 struct isl_dim_map *dim_map1, *dim_map2;
3186 if (!bmap1 || !bmap2)
3189 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3192 nparam = isl_basic_map_n_param(bmap1);
3193 n_in = isl_basic_map_n_in(bmap1);
3194 n_out = isl_basic_map_n_out(bmap1);
3196 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3197 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3198 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3199 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3200 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3201 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3202 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3203 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3204 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3205 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3206 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3208 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3209 bmap1->n_div + bmap2->n_div + 2 * n_out,
3210 bmap1->n_eq + bmap2->n_eq + n_out,
3211 bmap1->n_ineq + bmap2->n_ineq);
3212 for (i = 0; i < n_out; ++i) {
3213 int j = isl_basic_map_alloc_equality(bmap);
3216 isl_seq_clr(bmap->eq[j], 1+total);
3217 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3218 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3219 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3221 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3222 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3223 bmap = add_divs(bmap, 2 * n_out);
3225 bmap = isl_basic_map_simplify(bmap);
3226 return isl_basic_map_finalize(bmap);
3228 isl_basic_map_free(bmap);
3229 isl_basic_map_free(bmap1);
3230 isl_basic_map_free(bmap2);
3234 /* Given two maps A -> f(A) and B -> g(B), construct a map
3235 * A \cap B -> f(A) + f(B)
3237 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3239 struct isl_map *result;
3245 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3247 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3248 map1->n * map2->n, 0);
3251 for (i = 0; i < map1->n; ++i)
3252 for (j = 0; j < map2->n; ++j) {
3253 struct isl_basic_map *part;
3254 part = isl_basic_map_sum(
3255 isl_basic_map_copy(map1->p[i]),
3256 isl_basic_map_copy(map2->p[j]));
3257 if (isl_basic_map_is_empty(part))
3258 isl_basic_map_free(part);
3260 result = isl_map_add_basic_map(result, part);
3273 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3274 __isl_take isl_set *set2)
3276 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3279 /* Given a basic map A -> f(A), construct A -> -f(A).
3281 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3286 bmap = isl_basic_map_cow(bmap);
3290 n = isl_basic_map_dim(bmap, isl_dim_out);
3291 off = isl_basic_map_offset(bmap, isl_dim_out);
3292 for (i = 0; i < bmap->n_eq; ++i)
3293 for (j = 0; j < n; ++j)
3294 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3295 for (i = 0; i < bmap->n_ineq; ++i)
3296 for (j = 0; j < n; ++j)
3297 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3298 for (i = 0; i < bmap->n_div; ++i)
3299 for (j = 0; j < n; ++j)
3300 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3301 bmap = isl_basic_map_gauss(bmap, NULL);
3302 return isl_basic_map_finalize(bmap);
3305 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3307 return isl_basic_map_neg(bset);
3310 /* Given a map A -> f(A), construct A -> -f(A).
3312 struct isl_map *isl_map_neg(struct isl_map *map)
3316 map = isl_map_cow(map);
3320 for (i = 0; i < map->n; ++i) {
3321 map->p[i] = isl_basic_map_neg(map->p[i]);
3332 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3334 return (isl_set *)isl_map_neg((isl_map *)set);
3337 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3338 * A -> floor(f(A)/d).
3340 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3343 unsigned n_in, n_out, nparam, total, pos;
3344 struct isl_basic_map *result = NULL;
3345 struct isl_dim_map *dim_map;
3351 nparam = isl_basic_map_n_param(bmap);
3352 n_in = isl_basic_map_n_in(bmap);
3353 n_out = isl_basic_map_n_out(bmap);
3355 total = nparam + n_in + n_out + bmap->n_div + n_out;
3356 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3357 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3358 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3359 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3360 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3362 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3363 bmap->n_div + n_out,
3364 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3365 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3366 result = add_divs(result, n_out);
3367 for (i = 0; i < n_out; ++i) {
3369 j = isl_basic_map_alloc_inequality(result);
3372 isl_seq_clr(result->ineq[j], 1+total);
3373 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3374 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3375 j = isl_basic_map_alloc_inequality(result);
3378 isl_seq_clr(result->ineq[j], 1+total);
3379 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3380 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3381 isl_int_sub_ui(result->ineq[j][0], d, 1);
3384 result = isl_basic_map_simplify(result);
3385 return isl_basic_map_finalize(result);
3387 isl_basic_map_free(result);
3391 /* Given a map A -> f(A) and an integer d, construct a map
3392 * A -> floor(f(A)/d).
3394 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3398 map = isl_map_cow(map);
3402 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3403 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3404 for (i = 0; i < map->n; ++i) {
3405 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3416 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3422 i = isl_basic_map_alloc_equality(bmap);
3425 nparam = isl_basic_map_n_param(bmap);
3426 n_in = isl_basic_map_n_in(bmap);
3427 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3428 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3429 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3430 return isl_basic_map_finalize(bmap);
3432 isl_basic_map_free(bmap);
3436 /* Add a constraints to "bmap" expressing i_pos < o_pos
3438 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3444 i = isl_basic_map_alloc_inequality(bmap);
3447 nparam = isl_basic_map_n_param(bmap);
3448 n_in = isl_basic_map_n_in(bmap);
3449 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3450 isl_int_set_si(bmap->ineq[i][0], -1);
3451 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3452 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3453 return isl_basic_map_finalize(bmap);
3455 isl_basic_map_free(bmap);
3459 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3461 static __isl_give isl_basic_map *var_less_or_equal(
3462 __isl_take isl_basic_map *bmap, unsigned pos)
3468 i = isl_basic_map_alloc_inequality(bmap);
3471 nparam = isl_basic_map_n_param(bmap);
3472 n_in = isl_basic_map_n_in(bmap);
3473 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3474 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3475 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3476 return isl_basic_map_finalize(bmap);
3478 isl_basic_map_free(bmap);
3482 /* Add a constraints to "bmap" expressing i_pos > o_pos
3484 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3490 i = isl_basic_map_alloc_inequality(bmap);
3493 nparam = isl_basic_map_n_param(bmap);
3494 n_in = isl_basic_map_n_in(bmap);
3495 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3496 isl_int_set_si(bmap->ineq[i][0], -1);
3497 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3498 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3499 return isl_basic_map_finalize(bmap);
3501 isl_basic_map_free(bmap);
3505 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3507 static __isl_give isl_basic_map *var_more_or_equal(
3508 __isl_take isl_basic_map *bmap, unsigned pos)
3514 i = isl_basic_map_alloc_inequality(bmap);
3517 nparam = isl_basic_map_n_param(bmap);
3518 n_in = isl_basic_map_n_in(bmap);
3519 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3520 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3521 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3522 return isl_basic_map_finalize(bmap);
3524 isl_basic_map_free(bmap);
3528 __isl_give isl_basic_map *isl_basic_map_equal(
3529 __isl_take isl_space *dim, unsigned n_equal)
3532 struct isl_basic_map *bmap;
3533 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3536 for (i = 0; i < n_equal && bmap; ++i)
3537 bmap = var_equal(bmap, i);
3538 return isl_basic_map_finalize(bmap);
3541 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3543 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3547 struct isl_basic_map *bmap;
3548 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3551 for (i = 0; i < pos && bmap; ++i)
3552 bmap = var_equal(bmap, i);
3554 bmap = var_less(bmap, pos);
3555 return isl_basic_map_finalize(bmap);
3558 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3560 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3561 __isl_take isl_space *dim, unsigned pos)
3564 isl_basic_map *bmap;
3566 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3567 for (i = 0; i < pos; ++i)
3568 bmap = var_equal(bmap, i);
3569 bmap = var_less_or_equal(bmap, pos);
3570 return isl_basic_map_finalize(bmap);
3573 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3575 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3579 struct isl_basic_map *bmap;
3580 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3583 for (i = 0; i < pos && bmap; ++i)
3584 bmap = var_equal(bmap, i);
3586 bmap = var_more(bmap, pos);
3587 return isl_basic_map_finalize(bmap);
3590 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3592 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3593 __isl_take isl_space *dim, unsigned pos)
3596 isl_basic_map *bmap;
3598 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3599 for (i = 0; i < pos; ++i)
3600 bmap = var_equal(bmap, i);
3601 bmap = var_more_or_equal(bmap, pos);
3602 return isl_basic_map_finalize(bmap);
3605 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3606 unsigned n, int equal)
3608 struct isl_map *map;
3611 if (n == 0 && equal)
3612 return isl_map_universe(dims);
3614 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3616 for (i = 0; i + 1 < n; ++i)
3617 map = isl_map_add_basic_map(map,
3618 isl_basic_map_less_at(isl_space_copy(dims), i));
3621 map = isl_map_add_basic_map(map,
3622 isl_basic_map_less_or_equal_at(dims, n - 1));
3624 map = isl_map_add_basic_map(map,
3625 isl_basic_map_less_at(dims, n - 1));
3627 isl_space_free(dims);
3632 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
3636 return map_lex_lte_first(dims, dims->n_out, equal);
3639 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
3641 return map_lex_lte_first(dim, n, 0);
3644 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
3646 return map_lex_lte_first(dim, n, 1);
3649 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
3651 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
3654 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
3656 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
3659 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
3660 unsigned n, int equal)
3662 struct isl_map *map;
3665 if (n == 0 && equal)
3666 return isl_map_universe(dims);
3668 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
3670 for (i = 0; i + 1 < n; ++i)
3671 map = isl_map_add_basic_map(map,
3672 isl_basic_map_more_at(isl_space_copy(dims), i));
3675 map = isl_map_add_basic_map(map,
3676 isl_basic_map_more_or_equal_at(dims, n - 1));
3678 map = isl_map_add_basic_map(map,
3679 isl_basic_map_more_at(dims, n - 1));
3681 isl_space_free(dims);
3686 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
3690 return map_lex_gte_first(dims, dims->n_out, equal);
3693 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
3695 return map_lex_gte_first(dim, n, 0);
3698 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
3700 return map_lex_gte_first(dim, n, 1);
3703 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
3705 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
3708 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
3710 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
3713 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3714 __isl_take isl_set *set2)
3717 map = isl_map_lex_le(isl_set_get_space(set1));
3718 map = isl_map_intersect_domain(map, set1);
3719 map = isl_map_intersect_range(map, set2);
3723 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3724 __isl_take isl_set *set2)
3727 map = isl_map_lex_lt(isl_set_get_space(set1));
3728 map = isl_map_intersect_domain(map, set1);
3729 map = isl_map_intersect_range(map, set2);
3733 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3734 __isl_take isl_set *set2)
3737 map = isl_map_lex_ge(isl_set_get_space(set1));
3738 map = isl_map_intersect_domain(map, set1);
3739 map = isl_map_intersect_range(map, set2);
3743 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3744 __isl_take isl_set *set2)
3747 map = isl_map_lex_gt(isl_set_get_space(set1));
3748 map = isl_map_intersect_domain(map, set1);
3749 map = isl_map_intersect_range(map, set2);
3753 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3754 __isl_take isl_map *map2)
3757 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
3758 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3759 map = isl_map_apply_range(map, isl_map_reverse(map2));
3763 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3764 __isl_take isl_map *map2)
3767 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
3768 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3769 map = isl_map_apply_range(map, isl_map_reverse(map2));
3773 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3774 __isl_take isl_map *map2)
3777 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
3778 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3779 map = isl_map_apply_range(map, isl_map_reverse(map2));
3783 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3784 __isl_take isl_map *map2)
3787 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
3788 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3789 map = isl_map_apply_range(map, isl_map_reverse(map2));
3793 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
3794 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
3796 struct isl_basic_map *bmap;
3798 bset = isl_basic_set_cow(bset);
3802 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
3803 isl_space_free(bset->dim);
3804 bmap = (struct isl_basic_map *) bset;
3806 return isl_basic_map_finalize(bmap);
3808 isl_basic_set_free(bset);
3809 isl_space_free(dim);
3813 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3817 if (bmap->dim->n_in == 0)
3818 return (struct isl_basic_set *)bmap;
3819 bmap = isl_basic_map_cow(bmap);
3822 bmap->dim = isl_space_as_set_space(bmap->dim);
3825 bmap = isl_basic_map_finalize(bmap);
3826 return (struct isl_basic_set *)bmap;
3828 isl_basic_map_free(bmap);
3832 /* For a div d = floor(f/m), add the constraints
3835 * -(f-(n-1)) + m d >= 0
3837 * Note that the second constraint is the negation of
3841 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3842 unsigned pos, isl_int *div)
3845 unsigned total = isl_basic_map_total_dim(bmap);
3847 i = isl_basic_map_alloc_inequality(bmap);
3850 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3851 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3853 j = isl_basic_map_alloc_inequality(bmap);
3856 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3857 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3858 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3862 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3863 unsigned pos, isl_int *div)
3865 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3869 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3871 unsigned total = isl_basic_map_total_dim(bmap);
3872 unsigned div_pos = total - bmap->n_div + div;
3874 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3878 struct isl_basic_set *isl_basic_map_underlying_set(
3879 struct isl_basic_map *bmap)
3883 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3885 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
3886 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
3887 return (struct isl_basic_set *)bmap;
3888 bmap = isl_basic_map_cow(bmap);
3891 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
3894 bmap->extra -= bmap->n_div;
3896 bmap = isl_basic_map_finalize(bmap);
3897 return (struct isl_basic_set *)bmap;
3902 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3903 __isl_take isl_basic_set *bset)
3905 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3908 struct isl_basic_map *isl_basic_map_overlying_set(
3909 struct isl_basic_set *bset, struct isl_basic_map *like)
3911 struct isl_basic_map *bmap;
3912 struct isl_ctx *ctx;
3919 isl_assert(ctx, bset->n_div == 0, goto error);
3920 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3921 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3923 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
3924 isl_basic_map_free(like);
3925 return (struct isl_basic_map *)bset;
3927 bset = isl_basic_set_cow(bset);
3930 total = bset->dim->n_out + bset->extra;
3931 bmap = (struct isl_basic_map *)bset;
3932 isl_space_free(bmap->dim);
3933 bmap->dim = isl_space_copy(like->dim);
3936 bmap->n_div = like->n_div;
3937 bmap->extra += like->n_div;
3941 ltotal = total - bmap->extra + like->extra;
3944 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3945 bmap->extra * (1 + 1 + total));
3946 if (isl_blk_is_error(bmap->block2))
3948 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3952 for (i = 0; i < bmap->extra; ++i)
3953 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3954 for (i = 0; i < like->n_div; ++i) {
3955 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3956 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3958 bmap = isl_basic_map_extend_constraints(bmap,
3959 0, 2 * like->n_div);
3960 for (i = 0; i < like->n_div; ++i) {
3961 if (isl_int_is_zero(bmap->div[i][0]))
3963 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3967 isl_basic_map_free(like);
3968 bmap = isl_basic_map_simplify(bmap);
3969 bmap = isl_basic_map_finalize(bmap);
3972 isl_basic_map_free(like);
3973 isl_basic_set_free(bset);
3977 struct isl_basic_set *isl_basic_set_from_underlying_set(
3978 struct isl_basic_set *bset, struct isl_basic_set *like)
3980 return (struct isl_basic_set *)
3981 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3984 struct isl_set *isl_set_from_underlying_set(
3985 struct isl_set *set, struct isl_basic_set *like)
3991 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3993 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
3994 isl_basic_set_free(like);
3997 set = isl_set_cow(set);
4000 for (i = 0; i < set->n; ++i) {
4001 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4002 isl_basic_set_copy(like));
4006 isl_space_free(set->dim);
4007 set->dim = isl_space_copy(like->dim);
4010 isl_basic_set_free(like);
4013 isl_basic_set_free(like);
4018 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4022 map = isl_map_cow(map);
4025 map->dim = isl_space_cow(map->dim);
4029 for (i = 1; i < map->n; ++i)
4030 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4032 for (i = 0; i < map->n; ++i) {
4033 map->p[i] = (struct isl_basic_map *)
4034 isl_basic_map_underlying_set(map->p[i]);
4039 map->dim = isl_space_underlying(map->dim, 0);
4041 isl_space_free(map->dim);
4042 map->dim = isl_space_copy(map->p[0]->dim);
4046 return (struct isl_set *)map;
4052 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4054 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4057 __isl_give isl_basic_map *isl_basic_map_reset_space(
4058 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4060 bmap = isl_basic_map_cow(bmap);
4064 isl_space_free(bmap->dim);
4067 bmap = isl_basic_map_finalize(bmap);
4071 isl_basic_map_free(bmap);
4072 isl_space_free(dim);
4076 __isl_give isl_basic_set *isl_basic_set_reset_space(
4077 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4079 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4083 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4084 __isl_take isl_space *dim)
4088 map = isl_map_cow(map);
4092 for (i = 0; i < map->n; ++i) {
4093 map->p[i] = isl_basic_map_reset_space(map->p[i],
4094 isl_space_copy(dim));
4098 isl_space_free(map->dim);
4104 isl_space_free(dim);
4108 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4109 __isl_take isl_space *dim)
4111 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4114 /* Compute the parameter domain of the given basic set.
4116 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4121 if (isl_basic_set_is_params(bset))
4124 n = isl_basic_set_dim(bset, isl_dim_set);
4125 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4126 space = isl_basic_set_get_space(bset);
4127 space = isl_space_params(space);
4128 bset = isl_basic_set_reset_space(bset, space);
4132 /* Compute the parameter domain of the given set.
4134 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4139 if (isl_set_is_params(set))
4142 n = isl_set_dim(set, isl_dim_set);
4143 set = isl_set_project_out(set, isl_dim_set, 0, n);
4144 space = isl_set_get_space(set);
4145 space = isl_space_params(space);
4146 set = isl_set_reset_space(set, space);
4150 /* Construct a zero-dimensional set with the given parameter domain.
4152 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4155 space = isl_set_get_space(set);
4156 space = isl_space_set_from_params(space);
4157 set = isl_set_reset_space(set, space);
4161 /* Compute the parameter domain of the given map.
4163 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4168 n = isl_map_dim(map, isl_dim_in);
4169 map = isl_map_project_out(map, isl_dim_in, 0, n);
4170 n = isl_map_dim(map, isl_dim_out);
4171 map = isl_map_project_out(map, isl_dim_out, 0, n);
4172 space = isl_map_get_space(map);
4173 space = isl_space_params(space);
4174 map = isl_map_reset_space(map, space);
4178 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4181 struct isl_basic_set *domain;
4187 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4189 n_in = isl_basic_map_n_in(bmap);
4190 n_out = isl_basic_map_n_out(bmap);
4191 domain = isl_basic_set_from_basic_map(bmap);
4192 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4194 domain = isl_basic_set_reset_space(domain, dim);
4199 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4203 return isl_space_may_be_set(bmap->dim);
4206 /* Is this basic map actually a set?
4207 * Users should never call this function. Outside of isl,
4208 * the type should indicate whether something is a set or a map.
4210 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4214 return isl_space_is_set(bmap->dim);
4217 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4221 if (isl_basic_map_is_set(bmap))
4223 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4226 __isl_give isl_basic_map *isl_basic_map_domain_map(
4227 __isl_take isl_basic_map *bmap)
4231 isl_basic_map *domain;
4232 int nparam, n_in, n_out;
4235 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4236 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4237 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4239 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4240 domain = isl_basic_map_universe(dim);
4242 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4243 bmap = isl_basic_map_apply_range(bmap, domain);
4244 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4246 total = isl_basic_map_total_dim(bmap);
4248 for (i = 0; i < n_in; ++i) {
4249 k = isl_basic_map_alloc_equality(bmap);
4252 isl_seq_clr(bmap->eq[k], 1 + total);
4253 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4254 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4257 bmap = isl_basic_map_gauss(bmap, NULL);
4258 return isl_basic_map_finalize(bmap);
4260 isl_basic_map_free(bmap);
4264 __isl_give isl_basic_map *isl_basic_map_range_map(
4265 __isl_take isl_basic_map *bmap)
4269 isl_basic_map *range;
4270 int nparam, n_in, n_out;
4273 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4274 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4275 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4277 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4278 range = isl_basic_map_universe(dim);
4280 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4281 bmap = isl_basic_map_apply_range(bmap, range);
4282 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4284 total = isl_basic_map_total_dim(bmap);
4286 for (i = 0; i < n_out; ++i) {
4287 k = isl_basic_map_alloc_equality(bmap);
4290 isl_seq_clr(bmap->eq[k], 1 + total);
4291 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4292 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4295 bmap = isl_basic_map_gauss(bmap, NULL);
4296 return isl_basic_map_finalize(bmap);
4298 isl_basic_map_free(bmap);
4302 int isl_map_may_be_set(__isl_keep isl_map *map)
4306 return isl_space_may_be_set(map->dim);
4309 /* Is this map actually a set?
4310 * Users should never call this function. Outside of isl,
4311 * the type should indicate whether something is a set or a map.
4313 int isl_map_is_set(__isl_keep isl_map *map)
4317 return isl_space_is_set(map->dim);
4320 struct isl_set *isl_map_range(struct isl_map *map)
4323 struct isl_set *set;
4327 if (isl_map_is_set(map))
4328 return (isl_set *)map;
4330 map = isl_map_cow(map);
4334 set = (struct isl_set *) map;
4335 set->dim = isl_space_range(set->dim);
4338 for (i = 0; i < map->n; ++i) {
4339 set->p[i] = isl_basic_map_range(map->p[i]);
4343 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4344 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4351 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4354 isl_space *domain_dim;
4356 map = isl_map_cow(map);
4360 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4361 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4362 map->dim = isl_space_join(map->dim, domain_dim);
4365 for (i = 0; i < map->n; ++i) {
4366 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4370 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4371 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4378 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4381 isl_space *range_dim;
4383 map = isl_map_cow(map);
4387 range_dim = isl_space_range(isl_map_get_space(map));
4388 range_dim = isl_space_from_range(range_dim);
4389 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4390 map->dim = isl_space_join(map->dim, range_dim);
4393 for (i = 0; i < map->n; ++i) {
4394 map->p[i] = isl_basic_map_range_map(map->p[i]);
4398 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4399 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4406 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4407 __isl_take isl_space *dim)
4410 struct isl_map *map = NULL;
4412 set = isl_set_cow(set);
4415 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4416 map = (struct isl_map *)set;
4417 for (i = 0; i < set->n; ++i) {
4418 map->p[i] = isl_basic_map_from_basic_set(
4419 set->p[i], isl_space_copy(dim));
4423 isl_space_free(map->dim);
4427 isl_space_free(dim);
4432 __isl_give isl_basic_map *isl_basic_map_from_domain(
4433 __isl_take isl_basic_set *bset)
4435 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4438 __isl_give isl_basic_map *isl_basic_map_from_range(
4439 __isl_take isl_basic_set *bset)
4442 space = isl_basic_set_get_space(bset);
4443 space = isl_space_from_range(space);
4444 bset = isl_basic_set_reset_space(bset, space);
4445 return (isl_basic_map *)bset;
4448 struct isl_map *isl_map_from_range(struct isl_set *set)
4451 space = isl_set_get_space(set);
4452 space = isl_space_from_range(space);
4453 set = isl_set_reset_space(set, space);
4454 return (struct isl_map *)set;
4457 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4459 return isl_map_reverse(isl_map_from_range(set));
4462 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4463 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4465 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4468 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4469 __isl_take isl_set *range)
4471 return isl_map_apply_range(isl_map_reverse(domain), range);
4474 struct isl_set *isl_set_from_map(struct isl_map *map)
4477 struct isl_set *set = NULL;
4481 map = isl_map_cow(map);
4484 map->dim = isl_space_as_set_space(map->dim);
4487 set = (struct isl_set *)map;
4488 for (i = 0; i < map->n; ++i) {
4489 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4499 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4502 struct isl_map *map;
4507 isl_die(dim->ctx, isl_error_internal,
4508 "negative number of basic maps", goto error);
4509 map = isl_alloc(dim->ctx, struct isl_map,
4510 sizeof(struct isl_map) +
4511 (n - 1) * sizeof(struct isl_basic_map *));
4515 map->ctx = dim->ctx;
4516 isl_ctx_ref(map->ctx);
4524 isl_space_free(dim);
4528 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4529 unsigned nparam, unsigned in, unsigned out, int n,
4532 struct isl_map *map;
4535 dims = isl_space_alloc(ctx, nparam, in, out);
4539 map = isl_map_alloc_space(dims, n, flags);
4543 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4545 struct isl_basic_map *bmap;
4546 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4547 bmap = isl_basic_map_set_to_empty(bmap);
4551 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4553 struct isl_basic_set *bset;
4554 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4555 bset = isl_basic_set_set_to_empty(bset);
4559 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4561 struct isl_basic_map *bmap;
4564 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4565 bmap = isl_basic_map_set_to_empty(bmap);
4569 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4571 struct isl_basic_map *bmap;
4574 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4575 bmap = isl_basic_map_set_to_empty(bmap);
4579 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4581 struct isl_basic_set *bset;
4584 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4585 bset = isl_basic_set_set_to_empty(bset);
4589 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
4591 struct isl_basic_map *bmap;
4592 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
4593 bmap = isl_basic_map_finalize(bmap);
4597 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
4599 struct isl_basic_set *bset;
4600 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
4601 bset = isl_basic_set_finalize(bset);
4605 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
4608 unsigned total = isl_space_dim(dim, isl_dim_all);
4609 isl_basic_map *bmap;
4611 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
4612 for (i = 0; i < total; ++i) {
4613 int k = isl_basic_map_alloc_inequality(bmap);
4616 isl_seq_clr(bmap->ineq[k], 1 + total);
4617 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4621 isl_basic_map_free(bmap);
4625 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
4627 return isl_basic_map_nat_universe(dim);
4630 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
4632 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4635 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
4637 return isl_map_nat_universe(dim);
4640 __isl_give isl_basic_map *isl_basic_map_universe_like(
4641 __isl_keep isl_basic_map *model)
4645 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4648 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4652 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4655 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4656 __isl_keep isl_set *model)
4660 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
4663 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
4665 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4668 struct isl_map *isl_map_empty_like(struct isl_map *model)
4672 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4675 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4679 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
4682 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
4684 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
4687 struct isl_set *isl_set_empty_like(struct isl_set *model)
4691 return isl_set_empty(isl_space_copy(model->dim));
4694 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
4696 struct isl_map *map;
4699 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4700 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4704 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
4706 struct isl_set *set;
4709 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
4710 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4714 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4718 return isl_set_universe(isl_space_copy(model->dim));
4721 struct isl_map *isl_map_dup(struct isl_map *map)
4724 struct isl_map *dup;
4728 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
4729 for (i = 0; i < map->n; ++i)
4730 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4734 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4735 __isl_take isl_basic_map *bmap)
4739 if (isl_basic_map_plain_is_empty(bmap)) {
4740 isl_basic_map_free(bmap);
4743 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
4744 isl_assert(map->ctx, map->n < map->size, goto error);
4745 map->p[map->n] = bmap;
4747 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4753 isl_basic_map_free(bmap);
4757 void isl_map_free(struct isl_map *map)
4767 isl_ctx_deref(map->ctx);
4768 for (i = 0; i < map->n; ++i)
4769 isl_basic_map_free(map->p[i]);
4770 isl_space_free(map->dim);
4774 struct isl_map *isl_map_extend(struct isl_map *base,
4775 unsigned nparam, unsigned n_in, unsigned n_out)
4779 base = isl_map_cow(base);
4783 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
4786 for (i = 0; i < base->n; ++i) {
4787 base->p[i] = isl_basic_map_extend_space(base->p[i],
4788 isl_space_copy(base->dim), 0, 0, 0);
4798 struct isl_set *isl_set_extend(struct isl_set *base,
4799 unsigned nparam, unsigned dim)
4801 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4805 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4806 struct isl_basic_map *bmap, unsigned pos, int value)
4810 bmap = isl_basic_map_cow(bmap);
4811 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4812 j = isl_basic_map_alloc_equality(bmap);
4815 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4816 isl_int_set_si(bmap->eq[j][pos], -1);
4817 isl_int_set_si(bmap->eq[j][0], value);
4818 bmap = isl_basic_map_simplify(bmap);
4819 return isl_basic_map_finalize(bmap);
4821 isl_basic_map_free(bmap);
4825 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4826 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4830 bmap = isl_basic_map_cow(bmap);
4831 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4832 j = isl_basic_map_alloc_equality(bmap);
4835 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4836 isl_int_set_si(bmap->eq[j][pos], -1);
4837 isl_int_set(bmap->eq[j][0], value);
4838 bmap = isl_basic_map_simplify(bmap);
4839 return isl_basic_map_finalize(bmap);
4841 isl_basic_map_free(bmap);
4845 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4846 enum isl_dim_type type, unsigned pos, int value)
4850 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4851 return isl_basic_map_fix_pos_si(bmap,
4852 isl_basic_map_offset(bmap, type) + pos, value);
4854 isl_basic_map_free(bmap);
4858 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4859 enum isl_dim_type type, unsigned pos, isl_int value)
4863 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4864 return isl_basic_map_fix_pos(bmap,
4865 isl_basic_map_offset(bmap, type) + pos, value);
4867 isl_basic_map_free(bmap);
4871 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4872 enum isl_dim_type type, unsigned pos, int value)
4874 return (struct isl_basic_set *)
4875 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4879 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4880 enum isl_dim_type type, unsigned pos, isl_int value)
4882 return (struct isl_basic_set *)
4883 isl_basic_map_fix((struct isl_basic_map *)bset,
4887 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4888 unsigned input, int value)
4890 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4893 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4894 unsigned dim, int value)
4896 return (struct isl_basic_set *)
4897 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4898 isl_dim_set, dim, value);
4901 struct isl_map *isl_map_fix_si(struct isl_map *map,
4902 enum isl_dim_type type, unsigned pos, int value)
4906 map = isl_map_cow(map);
4910 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4911 for (i = 0; i < map->n; ++i) {
4912 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4916 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4923 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4924 enum isl_dim_type type, unsigned pos, int value)
4926 return (struct isl_set *)
4927 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4930 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4931 enum isl_dim_type type, unsigned pos, isl_int value)
4935 map = isl_map_cow(map);
4939 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4940 for (i = 0; i < map->n; ++i) {
4941 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4945 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4952 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4953 enum isl_dim_type type, unsigned pos, isl_int value)
4955 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4958 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4959 unsigned input, int value)
4961 return isl_map_fix_si(map, isl_dim_in, input, value);
4964 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4966 return (struct isl_set *)
4967 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4970 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4971 __isl_take isl_basic_map *bmap,
4972 enum isl_dim_type type, unsigned pos, int value)
4978 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4979 pos += isl_basic_map_offset(bmap, type);
4980 bmap = isl_basic_map_cow(bmap);
4981 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4982 j = isl_basic_map_alloc_inequality(bmap);
4985 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4986 isl_int_set_si(bmap->ineq[j][pos], 1);
4987 isl_int_set_si(bmap->ineq[j][0], -value);
4988 bmap = isl_basic_map_simplify(bmap);
4989 return isl_basic_map_finalize(bmap);
4991 isl_basic_map_free(bmap);
4995 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4996 unsigned dim, isl_int value)
5000 bset = isl_basic_set_cow(bset);
5001 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5002 j = isl_basic_set_alloc_inequality(bset);
5005 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5006 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5007 isl_int_neg(bset->ineq[j][0], value);
5008 bset = isl_basic_set_simplify(bset);
5009 return isl_basic_set_finalize(bset);
5011 isl_basic_set_free(bset);
5015 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5016 enum isl_dim_type type, unsigned pos, int value)
5020 map = isl_map_cow(map);
5024 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5025 for (i = 0; i < map->n; ++i) {
5026 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
5031 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5038 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5039 enum isl_dim_type type, unsigned pos, int value)
5041 return (struct isl_set *)
5042 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5045 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5050 set = isl_set_cow(set);
5054 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5055 for (i = 0; i < set->n; ++i) {
5056 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5066 struct isl_map *isl_map_reverse(struct isl_map *map)
5070 map = isl_map_cow(map);
5074 map->dim = isl_space_reverse(map->dim);
5077 for (i = 0; i < map->n; ++i) {
5078 map->p[i] = isl_basic_map_reverse(map->p[i]);
5082 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5089 static struct isl_map *isl_basic_map_partial_lexopt(
5090 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5091 struct isl_set **empty, int max)
5095 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5096 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5098 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5100 isl_basic_map_free(bmap);
5101 isl_basic_set_free(dom);
5107 struct isl_map *isl_basic_map_partial_lexmax(
5108 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5109 struct isl_set **empty)
5111 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5114 struct isl_map *isl_basic_map_partial_lexmin(
5115 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5116 struct isl_set **empty)
5118 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5121 struct isl_set *isl_basic_set_partial_lexmin(
5122 struct isl_basic_set *bset, struct isl_basic_set *dom,
5123 struct isl_set **empty)
5125 return (struct isl_set *)
5126 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5130 struct isl_set *isl_basic_set_partial_lexmax(
5131 struct isl_basic_set *bset, struct isl_basic_set *dom,
5132 struct isl_set **empty)
5134 return (struct isl_set *)
5135 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5139 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5140 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5141 __isl_give isl_set **empty)
5143 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5146 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5147 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5148 __isl_give isl_set **empty)
5150 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5153 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5154 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5155 __isl_give isl_set **empty)
5157 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5160 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5161 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5162 __isl_give isl_set **empty)
5164 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5167 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5168 __isl_take isl_basic_map *bmap, int max)
5170 isl_basic_set *dom = NULL;
5171 isl_space *dom_space;
5175 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5176 dom = isl_basic_set_universe(dom_space);
5177 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5179 isl_basic_map_free(bmap);
5183 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5184 __isl_take isl_basic_map *bmap)
5186 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5189 /* Given a basic map "bmap", compute the lexicographically minimal
5190 * (or maximal) image element for each domain element in dom.
5191 * Set *empty to those elements in dom that do not have an image element.
5193 * We first make sure the basic sets in dom are disjoint and then
5194 * simply collect the results over each of the basic sets separately.
5195 * We could probably improve the efficiency a bit by moving the union
5196 * domain down into the parametric integer programming.
5198 static __isl_give isl_map *basic_map_partial_lexopt(
5199 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
5200 __isl_give isl_set **empty, int max)
5203 struct isl_map *res;
5205 dom = isl_set_make_disjoint(dom);
5209 if (isl_set_plain_is_empty(dom)) {
5210 res = isl_map_empty_like_basic_map(bmap);
5211 *empty = isl_set_empty_like(dom);
5213 isl_basic_map_free(bmap);
5217 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5218 isl_basic_set_copy(dom->p[0]), empty, max);
5220 for (i = 1; i < dom->n; ++i) {
5221 struct isl_map *res_i;
5222 struct isl_set *empty_i;
5224 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
5225 isl_basic_set_copy(dom->p[i]), &empty_i, max);
5227 res = isl_map_union_disjoint(res, res_i);
5228 *empty = isl_set_union_disjoint(*empty, empty_i);
5232 isl_basic_map_free(bmap);
5237 isl_basic_map_free(bmap);
5241 /* Given a map "map", compute the lexicographically minimal
5242 * (or maximal) image element for each domain element in dom.
5243 * Set *empty to those elements in dom that do not have an image element.
5245 * We first compute the lexicographically minimal or maximal element
5246 * in the first basic map. This results in a partial solution "res"
5247 * and a subset "todo" of dom that still need to be handled.
5248 * We then consider each of the remaining maps in "map" and successively
5249 * improve both "res" and "todo".
5251 * Let res^k and todo^k be the results after k steps and let i = k + 1.
5252 * Assume we are computing the lexicographical maximum.
5253 * We first compute the lexicographically maximal element in basic map i.
5254 * This results in a partial solution res_i and a subset todo_i.
5255 * Then we combine these results with those obtain for the first k basic maps
5256 * to obtain a result that is valid for the first k+1 basic maps.
5257 * In particular, the set where there is no solution is the set where
5258 * there is no solution for the first k basic maps and also no solution
5259 * for the ith basic map, i.e.,
5261 * todo^i = todo^k * todo_i
5263 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5264 * solutions, arbitrarily breaking ties in favor of res^k.
5265 * That is, when res^k(a) >= res_i(a), we pick res^k and
5266 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
5267 * the lexicographic order.)
5268 * In practice, we compute
5270 * res^k * (res_i . "<=")
5274 * res_i * (res^k . "<")
5276 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5277 * where only one of res^k and res_i provides a solution and we simply pick
5284 * Note that we only compute these intersections when dom(res^k) intersects
5285 * dom(res_i). Otherwise, the only effect of these intersections is to
5286 * potentially break up res^k and res_i into smaller pieces.
5287 * We want to avoid such splintering as much as possible.
5288 * In fact, an earlier implementation of this function would look for
5289 * better results in the domain of res^k and for extra results in todo^k,
5290 * but this would always result in a splintering according to todo^k,
5291 * even when the domain of basic map i is disjoint from the domains of
5292 * the previous basic maps.
5294 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5295 __isl_take isl_map *map, __isl_take isl_set *dom,
5296 __isl_give isl_set **empty, int max)
5299 struct isl_map *res;
5300 struct isl_set *todo;
5305 if (isl_map_plain_is_empty(map)) {
5313 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5314 isl_set_copy(dom), &todo, max);
5316 for (i = 1; i < map->n; ++i) {
5320 isl_space *dim = isl_space_range(isl_map_get_space(res));
5322 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5323 isl_set_copy(dom), &todo_i, max);
5326 lt = isl_map_lex_lt(isl_space_copy(dim));
5327 le = isl_map_lex_le(dim);
5329 lt = isl_map_lex_gt(isl_space_copy(dim));
5330 le = isl_map_lex_ge(dim);
5332 lt = isl_map_apply_range(isl_map_copy(res), lt);
5333 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5334 le = isl_map_apply_range(isl_map_copy(res_i), le);
5335 le = isl_map_intersect(le, isl_map_copy(res));
5337 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5338 res = isl_map_intersect_domain(res,
5339 isl_set_copy(todo_i));
5340 res_i = isl_map_intersect_domain(res_i,
5341 isl_set_copy(todo));
5344 res = isl_map_union_disjoint(res, res_i);
5345 res = isl_map_union_disjoint(res, lt);
5346 res = isl_map_union_disjoint(res, le);
5348 todo = isl_set_intersect(todo, todo_i);
5368 /* Given a map "map", compute the lexicographically minimal
5369 * (or maximal) image element for each domain element in dom.
5370 * Set *empty to those elements in dom that do not have an image element.
5372 * Align parameters if needed and then call isl_map_partial_lexopt_aligned.
5374 static __isl_give isl_map *isl_map_partial_lexopt(
5375 __isl_take isl_map *map, __isl_take isl_set *dom,
5376 __isl_give isl_set **empty, int max)
5380 if (isl_space_match(map->dim, isl_dim_param, dom->dim, isl_dim_param))
5381 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5382 if (!isl_space_has_named_params(map->dim) ||
5383 !isl_space_has_named_params(dom->dim))
5384 isl_die(map->ctx, isl_error_invalid,
5385 "unaligned unnamed parameters", goto error);
5386 map = isl_map_align_params(map, isl_map_get_space(dom));
5387 dom = isl_map_align_params(dom, isl_map_get_space(map));
5388 return isl_map_partial_lexopt_aligned(map, dom, empty, max);
5397 __isl_give isl_map *isl_map_partial_lexmax(
5398 __isl_take isl_map *map, __isl_take isl_set *dom,
5399 __isl_give isl_set **empty)
5401 return isl_map_partial_lexopt(map, dom, empty, 1);
5404 __isl_give isl_map *isl_map_partial_lexmin(
5405 __isl_take isl_map *map, __isl_take isl_set *dom,
5406 __isl_give isl_set **empty)
5408 return isl_map_partial_lexopt(map, dom, empty, 0);
5411 __isl_give isl_set *isl_set_partial_lexmin(
5412 __isl_take isl_set *set, __isl_take isl_set *dom,
5413 __isl_give isl_set **empty)
5415 return (struct isl_set *)
5416 isl_map_partial_lexmin((struct isl_map *)set,
5420 __isl_give isl_set *isl_set_partial_lexmax(
5421 __isl_take isl_set *set, __isl_take isl_set *dom,
5422 __isl_give isl_set **empty)
5424 return (struct isl_set *)
5425 isl_map_partial_lexmax((struct isl_map *)set,
5429 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5431 struct isl_basic_set *dom = NULL;
5436 dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5437 dom = isl_basic_set_universe(dom_dim);
5438 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5440 isl_basic_map_free(bmap);
5444 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5446 return isl_basic_map_lexopt(bmap, 0);
5449 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5451 return isl_basic_map_lexopt(bmap, 1);
5454 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5456 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5459 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5461 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5464 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5466 struct isl_set *dom = NULL;
5471 dom_dim = isl_space_domain(isl_space_copy(map->dim));
5472 dom = isl_set_universe(dom_dim);
5473 return isl_map_partial_lexopt(map, dom, NULL, max);
5479 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5481 return isl_map_lexopt(map, 0);
5484 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5486 return isl_map_lexopt(map, 1);
5489 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5491 return (isl_set *)isl_map_lexmin((isl_map *)set);
5494 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5496 return (isl_set *)isl_map_lexmax((isl_map *)set);
5499 /* Extract the first and only affine expression from list
5500 * and then add it to *pwaff with the given dom.
5501 * This domain is known to be disjoint from other domains
5502 * because of the way isl_basic_map_foreach_lexmax works.
5504 static int update_dim_opt(__isl_take isl_basic_set *dom,
5505 __isl_take isl_aff_list *list, void *user)
5507 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5509 isl_pw_aff **pwaff = user;
5510 isl_pw_aff *pwaff_i;
5512 if (isl_aff_list_n_aff(list) != 1)
5513 isl_die(ctx, isl_error_internal,
5514 "expecting single element list", goto error);
5516 aff = isl_aff_list_get_aff(list, 0);
5517 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5519 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5521 isl_aff_list_free(list);
5525 isl_basic_set_free(dom);
5526 isl_aff_list_free(list);
5530 /* Given a basic map with one output dimension, compute the minimum or
5531 * maximum of that dimension as an isl_pw_aff.
5533 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
5534 * call update_dim_opt on each leaf of the result.
5536 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
5539 isl_space *dim = isl_basic_map_get_space(bmap);
5543 dim = isl_space_from_domain(isl_space_domain(dim));
5544 dim = isl_space_add_dims(dim, isl_dim_out, 1);
5545 pwaff = isl_pw_aff_empty(dim);
5547 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
5549 return isl_pw_aff_free(pwaff);
5554 /* Compute the minimum or maximum of the given output dimension
5555 * as a function of the parameters and the input dimensions,
5556 * but independently of the other output dimensions.
5558 * We first project out the other output dimension and then compute
5559 * the "lexicographic" maximum in each basic map, combining the results
5560 * using isl_pw_aff_union_max.
5562 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
5569 n_out = isl_map_dim(map, isl_dim_out);
5570 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
5571 map = isl_map_project_out(map, isl_dim_out, 0, pos);
5576 isl_space *dim = isl_map_get_space(map);
5577 dim = isl_space_domain(isl_space_from_range(dim));
5579 return isl_pw_aff_empty(dim);
5582 pwaff = basic_map_dim_opt(map->p[0], max);
5583 for (i = 1; i < map->n; ++i) {
5584 isl_pw_aff *pwaff_i;
5586 pwaff_i = basic_map_dim_opt(map->p[i], max);
5587 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
5595 /* Compute the maximum of the given output dimension as a function of the
5596 * parameters and input dimensions, but independently of
5597 * the other output dimensions.
5599 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
5601 return map_dim_opt(map, pos, 1);
5604 /* Compute the minimum or maximum of the given set dimension
5605 * as a function of the parameters,
5606 * but independently of the other set dimensions.
5608 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
5611 return map_dim_opt(set, pos, max);
5614 /* Compute the maximum of the given set dimension as a function of the
5615 * parameters, but independently of the other set dimensions.
5617 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5619 return set_dim_opt(set, pos, 1);
5622 /* Compute the minimum of the given set dimension as a function of the
5623 * parameters, but independently of the other set dimensions.
5625 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
5627 return set_dim_opt(set, pos, 0);
5630 /* Apply a preimage specified by "mat" on the parameters of "bset".
5631 * bset is assumed to have only parameters and divs.
5633 static struct isl_basic_set *basic_set_parameter_preimage(
5634 struct isl_basic_set *bset, struct isl_mat *mat)
5641 bset->dim = isl_space_cow(bset->dim);
5645 nparam = isl_basic_set_dim(bset, isl_dim_param);
5647 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5649 bset->dim->nparam = 0;
5650 bset->dim->n_out = nparam;
5651 bset = isl_basic_set_preimage(bset, mat);
5653 bset->dim->nparam = bset->dim->n_out;
5654 bset->dim->n_out = 0;
5659 isl_basic_set_free(bset);
5663 /* Apply a preimage specified by "mat" on the parameters of "set".
5664 * set is assumed to have only parameters and divs.
5666 static struct isl_set *set_parameter_preimage(
5667 struct isl_set *set, struct isl_mat *mat)
5669 isl_space *dim = NULL;
5675 dim = isl_space_copy(set->dim);
5676 dim = isl_space_cow(dim);
5680 nparam = isl_set_dim(set, isl_dim_param);
5682 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5685 dim->n_out = nparam;
5686 isl_set_reset_space(set, dim);
5687 set = isl_set_preimage(set, mat);
5690 dim = isl_space_copy(set->dim);
5691 dim = isl_space_cow(dim);
5694 dim->nparam = dim->n_out;
5696 isl_set_reset_space(set, dim);
5699 isl_space_free(dim);
5706 /* Intersect the basic set "bset" with the affine space specified by the
5707 * equalities in "eq".
5709 static struct isl_basic_set *basic_set_append_equalities(
5710 struct isl_basic_set *bset, struct isl_mat *eq)
5718 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
5723 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
5724 for (i = 0; i < eq->n_row; ++i) {
5725 k = isl_basic_set_alloc_equality(bset);
5728 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5729 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5733 bset = isl_basic_set_gauss(bset, NULL);
5734 bset = isl_basic_set_finalize(bset);
5739 isl_basic_set_free(bset);
5743 /* Intersect the set "set" with the affine space specified by the
5744 * equalities in "eq".
5746 static struct isl_set *set_append_equalities(struct isl_set *set,
5754 for (i = 0; i < set->n; ++i) {
5755 set->p[i] = basic_set_append_equalities(set->p[i],
5768 /* Project the given basic set onto its parameter domain, possibly introducing
5769 * new, explicit, existential variables in the constraints.
5770 * The input has parameters and (possibly implicit) existential variables.
5771 * The output has the same parameters, but only
5772 * explicit existentially quantified variables.
5774 * The actual projection is performed by pip, but pip doesn't seem
5775 * to like equalities very much, so we first remove the equalities
5776 * among the parameters by performing a variable compression on
5777 * the parameters. Afterward, an inverse transformation is performed
5778 * and the equalities among the parameters are inserted back in.
5780 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5784 struct isl_mat *T, *T2;
5785 struct isl_set *set;
5786 unsigned nparam, n_div;
5788 bset = isl_basic_set_cow(bset);
5792 if (bset->n_eq == 0)
5793 return isl_basic_set_lexmin(bset);
5795 isl_basic_set_gauss(bset, NULL);
5797 nparam = isl_basic_set_dim(bset, isl_dim_param);
5798 n_div = isl_basic_set_dim(bset, isl_dim_div);
5800 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5801 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5804 if (i == bset->n_eq)
5805 return isl_basic_set_lexmin(bset);
5807 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5809 eq = isl_mat_cow(eq);
5810 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5811 if (T && T->n_col == 0) {
5815 bset = isl_basic_set_set_to_empty(bset);
5816 return isl_set_from_basic_set(bset);
5818 bset = basic_set_parameter_preimage(bset, T);
5820 set = isl_basic_set_lexmin(bset);
5821 set = set_parameter_preimage(set, T2);
5822 set = set_append_equalities(set, eq);
5826 /* Compute an explicit representation for all the existentially
5827 * quantified variables.
5828 * The input and output dimensions are first turned into parameters.
5829 * compute_divs then returns a map with the same parameters and
5830 * no input or output dimensions and the dimension specification
5831 * is reset to that of the input.
5833 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5835 struct isl_basic_set *bset;
5836 struct isl_set *set;
5837 struct isl_map *map;
5838 isl_space *dim, *orig_dim = NULL;
5843 bmap = isl_basic_map_cow(bmap);
5847 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5848 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5849 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5850 dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5854 orig_dim = bmap->dim;
5856 bset = (struct isl_basic_set *)bmap;
5858 set = parameter_compute_divs(bset);
5859 map = (struct isl_map *)set;
5860 map = isl_map_reset_space(map, orig_dim);
5864 isl_basic_map_free(bmap);
5868 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5876 off = isl_space_dim(bmap->dim, isl_dim_all);
5877 for (i = 0; i < bmap->n_div; ++i) {
5878 if (isl_int_is_zero(bmap->div[i][0]))
5880 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5886 static int map_divs_known(__isl_keep isl_map *map)
5893 for (i = 0; i < map->n; ++i) {
5894 int known = isl_basic_map_divs_known(map->p[i]);
5902 /* If bmap contains any unknown divs, then compute explicit
5903 * expressions for them. However, this computation may be
5904 * quite expensive, so first try to remove divs that aren't
5907 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5910 struct isl_map *map;
5912 known = isl_basic_map_divs_known(bmap);
5916 return isl_map_from_basic_map(bmap);
5918 bmap = isl_basic_map_drop_redundant_divs(bmap);
5920 known = isl_basic_map_divs_known(bmap);
5924 return isl_map_from_basic_map(bmap);
5926 map = compute_divs(bmap);
5929 isl_basic_map_free(bmap);
5933 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5937 struct isl_map *res;
5944 known = map_divs_known(map);
5952 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5953 for (i = 1 ; i < map->n; ++i) {
5955 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5956 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5957 res = isl_map_union_disjoint(res, r2);
5959 res = isl_map_union(res, r2);
5966 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5968 return (struct isl_set *)
5969 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5972 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5974 return (struct isl_set *)
5975 isl_map_compute_divs((struct isl_map *)set);
5978 struct isl_set *isl_map_domain(struct isl_map *map)
5981 struct isl_set *set;
5986 map = isl_map_cow(map);
5990 set = (struct isl_set *)map;
5991 set->dim = isl_space_domain(set->dim);
5994 for (i = 0; i < map->n; ++i) {
5995 set->p[i] = isl_basic_map_domain(map->p[i]);
5999 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6000 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6007 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6008 __isl_take isl_map *map2)
6012 struct isl_map *map = NULL;
6026 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6028 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6029 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6030 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6032 map = isl_map_alloc_space(isl_space_copy(map1->dim),
6033 map1->n + map2->n, flags);
6036 for (i = 0; i < map1->n; ++i) {
6037 map = isl_map_add_basic_map(map,
6038 isl_basic_map_copy(map1->p[i]));
6042 for (i = 0; i < map2->n; ++i) {
6043 map = isl_map_add_basic_map(map,
6044 isl_basic_map_copy(map2->p[i]));
6058 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6059 __isl_take isl_map *map2)
6061 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6064 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6066 map1 = isl_map_union_disjoint(map1, map2);
6070 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6074 struct isl_set *isl_set_union_disjoint(
6075 struct isl_set *set1, struct isl_set *set2)
6077 return (struct isl_set *)
6078 isl_map_union_disjoint(
6079 (struct isl_map *)set1, (struct isl_map *)set2);
6082 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6084 return (struct isl_set *)
6085 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6088 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6089 __isl_take isl_set *set)
6092 struct isl_map *result;
6098 if (!isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
6099 isl_die(set->ctx, isl_error_invalid,
6100 "parameters don't match", goto error);
6102 if (isl_space_dim(set->dim, isl_dim_set) != 0 &&
6103 !isl_map_compatible_range(map, set))
6104 isl_die(set->ctx, isl_error_invalid,
6105 "incompatible spaces", goto error);
6107 if (isl_set_plain_is_universe(set)) {
6112 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6113 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6114 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6116 result = isl_map_alloc_space(isl_space_copy(map->dim),
6117 map->n * set->n, flags);
6120 for (i = 0; i < map->n; ++i)
6121 for (j = 0; j < set->n; ++j) {
6122 result = isl_map_add_basic_map(result,
6123 isl_basic_map_intersect_range(
6124 isl_basic_map_copy(map->p[i]),
6125 isl_basic_set_copy(set->p[j])));
6138 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6139 __isl_take isl_set *set)
6141 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6144 struct isl_map *isl_map_intersect_domain(
6145 struct isl_map *map, struct isl_set *set)
6147 return isl_map_reverse(
6148 isl_map_intersect_range(isl_map_reverse(map), set));
6151 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6152 __isl_take isl_map *map2)
6156 map1 = isl_map_reverse(map1);
6157 map1 = isl_map_apply_range(map1, map2);
6158 return isl_map_reverse(map1);
6165 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6166 __isl_take isl_map *map2)
6168 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6171 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6172 __isl_take isl_map *map2)
6174 isl_space *dim_result;
6175 struct isl_map *result;
6181 dim_result = isl_space_join(isl_space_copy(map1->dim),
6182 isl_space_copy(map2->dim));
6184 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6187 for (i = 0; i < map1->n; ++i)
6188 for (j = 0; j < map2->n; ++j) {
6189 result = isl_map_add_basic_map(result,
6190 isl_basic_map_apply_range(
6191 isl_basic_map_copy(map1->p[i]),
6192 isl_basic_map_copy(map2->p[j])));
6198 if (result && result->n <= 1)
6199 ISL_F_SET(result, ISL_MAP_DISJOINT);
6207 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6208 __isl_take isl_map *map2)
6210 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6214 * returns range - domain
6216 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6218 isl_space *dims, *target_dim;
6219 struct isl_basic_set *bset;
6226 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6227 bmap->dim, isl_dim_out),
6229 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6230 dim = isl_basic_map_n_in(bmap);
6231 nparam = isl_basic_map_n_param(bmap);
6232 bset = isl_basic_set_from_basic_map(bmap);
6233 bset = isl_basic_set_cow(bset);
6234 dims = isl_basic_set_get_space(bset);
6235 dims = isl_space_add_dims(dims, isl_dim_set, dim);
6236 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6237 bset = isl_basic_set_swap_vars(bset, 2*dim);
6238 for (i = 0; i < dim; ++i) {
6239 int j = isl_basic_map_alloc_equality(
6240 (struct isl_basic_map *)bset);
6243 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6244 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6245 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6246 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6248 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6249 bset = isl_basic_set_reset_space(bset, target_dim);
6252 isl_basic_map_free(bmap);
6257 * returns range - domain
6259 struct isl_set *isl_map_deltas(struct isl_map *map)
6263 struct isl_set *result;
6268 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6269 map->dim, isl_dim_out),
6271 dim = isl_map_get_space(map);
6272 dim = isl_space_domain(dim);
6273 result = isl_set_alloc_space(dim, map->n, 0);
6276 for (i = 0; i < map->n; ++i)
6277 result = isl_set_add_basic_set(result,
6278 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6287 * returns [domain -> range] -> range - domain
6289 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6290 __isl_take isl_basic_map *bmap)
6294 isl_basic_map *domain;
6298 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6299 isl_die(bmap->ctx, isl_error_invalid,
6300 "domain and range don't match", goto error);
6302 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6303 n = isl_basic_map_dim(bmap, isl_dim_in);
6305 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6306 domain = isl_basic_map_universe(dim);
6308 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6309 bmap = isl_basic_map_apply_range(bmap, domain);
6310 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6312 total = isl_basic_map_total_dim(bmap);
6314 for (i = 0; i < n; ++i) {
6315 k = isl_basic_map_alloc_equality(bmap);
6318 isl_seq_clr(bmap->eq[k], 1 + total);
6319 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6320 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6321 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6324 bmap = isl_basic_map_gauss(bmap, NULL);
6325 return isl_basic_map_finalize(bmap);
6327 isl_basic_map_free(bmap);
6332 * returns [domain -> range] -> range - domain
6334 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6337 isl_space *domain_dim;
6342 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6343 isl_die(map->ctx, isl_error_invalid,
6344 "domain and range don't match", goto error);
6346 map = isl_map_cow(map);
6350 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6351 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6352 map->dim = isl_space_join(map->dim, domain_dim);
6355 for (i = 0; i < map->n; ++i) {
6356 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6360 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6367 __isl_give struct isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6369 struct isl_basic_map *bmap;
6377 nparam = dims->nparam;
6379 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6383 for (i = 0; i < dim; ++i) {
6384 int j = isl_basic_map_alloc_equality(bmap);
6387 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6388 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6389 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6391 return isl_basic_map_finalize(bmap);
6393 isl_basic_map_free(bmap);
6397 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6401 if (dim->n_in != dim->n_out)
6402 isl_die(dim->ctx, isl_error_invalid,
6403 "number of input and output dimensions needs to be "
6404 "the same", goto error);
6405 return basic_map_identity(dim);
6407 isl_space_free(dim);
6411 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6413 if (!model || !model->dim)
6415 return isl_basic_map_identity(isl_space_copy(model->dim));
6418 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
6420 return isl_map_from_basic_map(isl_basic_map_identity(dim));
6423 struct isl_map *isl_map_identity_like(struct isl_map *model)
6425 if (!model || !model->dim)
6427 return isl_map_identity(isl_space_copy(model->dim));
6430 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6432 if (!model || !model->dim)
6434 return isl_map_identity(isl_space_copy(model->dim));
6437 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6439 isl_space *dim = isl_set_get_space(set);
6441 id = isl_map_identity(isl_space_map_from_set(dim));
6442 return isl_map_intersect_range(id, set);
6445 /* Construct a basic set with all set dimensions having only non-negative
6448 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
6449 __isl_take isl_space *space)
6454 struct isl_basic_set *bset;
6458 nparam = space->nparam;
6460 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
6463 for (i = 0; i < dim; ++i) {
6464 int k = isl_basic_set_alloc_inequality(bset);
6467 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6468 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6472 isl_basic_set_free(bset);
6476 /* Construct the half-space x_pos >= 0.
6478 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
6482 isl_basic_set *nonneg;
6484 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6485 k = isl_basic_set_alloc_inequality(nonneg);
6488 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6489 isl_int_set_si(nonneg->ineq[k][pos], 1);
6491 return isl_basic_set_finalize(nonneg);
6493 isl_basic_set_free(nonneg);
6497 /* Construct the half-space x_pos <= -1.
6499 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
6504 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
6505 k = isl_basic_set_alloc_inequality(neg);
6508 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6509 isl_int_set_si(neg->ineq[k][0], -1);
6510 isl_int_set_si(neg->ineq[k][pos], -1);
6512 return isl_basic_set_finalize(neg);
6514 isl_basic_set_free(neg);
6518 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6519 enum isl_dim_type type, unsigned first, unsigned n)
6522 isl_basic_set *nonneg;
6530 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6532 for (i = 0; i < n; ++i) {
6533 nonneg = nonneg_halfspace(isl_set_get_space(set),
6534 pos(set->dim, type) + first + i);
6535 neg = neg_halfspace(isl_set_get_space(set),
6536 pos(set->dim, type) + first + i);
6538 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6547 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6548 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6555 if (isl_set_plain_is_empty(set)) {
6560 return fn(set, signs, user);
6563 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
6565 half = isl_set_intersect(half, isl_set_copy(set));
6566 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6570 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
6572 half = isl_set_intersect(half, set);
6573 return foreach_orthant(half, signs, first + 1, len, fn, user);
6579 /* Call "fn" on the intersections of "set" with each of the orthants
6580 * (except for obviously empty intersections). The orthant is identified
6581 * by the signs array, with each entry having value 1 or -1 according
6582 * to the sign of the corresponding variable.
6584 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6585 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6595 if (isl_set_plain_is_empty(set))
6598 nparam = isl_set_dim(set, isl_dim_param);
6599 nvar = isl_set_dim(set, isl_dim_set);
6601 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6603 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6611 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6613 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6616 int isl_basic_map_is_subset(
6617 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6620 struct isl_map *map1;
6621 struct isl_map *map2;
6623 if (!bmap1 || !bmap2)
6626 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6627 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6629 is_subset = isl_map_is_subset(map1, map2);
6637 int isl_basic_map_is_equal(
6638 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6642 if (!bmap1 || !bmap2)
6644 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6647 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6651 int isl_basic_set_is_equal(
6652 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6654 return isl_basic_map_is_equal(
6655 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6658 int isl_map_is_empty(struct isl_map *map)
6665 for (i = 0; i < map->n; ++i) {
6666 is_empty = isl_basic_map_is_empty(map->p[i]);
6675 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6677 return map ? map->n == 0 : -1;
6680 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6682 return isl_map_plain_is_empty(map);
6685 int isl_set_plain_is_empty(struct isl_set *set)
6687 return set ? set->n == 0 : -1;
6690 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6692 return isl_set_plain_is_empty(set);
6695 int isl_set_is_empty(struct isl_set *set)
6697 return isl_map_is_empty((struct isl_map *)set);
6700 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6705 return isl_space_is_equal(map1->dim, map2->dim);
6708 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6713 return isl_space_is_equal(set1->dim, set2->dim);
6716 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6722 is_subset = isl_map_is_subset(map1, map2);
6725 is_subset = isl_map_is_subset(map2, map1);
6729 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6731 return align_params_map_map_and_test(map1, map2, &map_is_equal);
6734 int isl_basic_map_is_strict_subset(
6735 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6739 if (!bmap1 || !bmap2)
6741 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6744 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6745 if (is_subset == -1)
6750 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6756 is_subset = isl_map_is_subset(map1, map2);
6759 is_subset = isl_map_is_subset(map2, map1);
6760 if (is_subset == -1)
6765 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6767 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6770 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6774 return bmap->n_eq == 0 && bmap->n_ineq == 0;
6777 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6781 return bset->n_eq == 0 && bset->n_ineq == 0;
6784 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6791 for (i = 0; i < map->n; ++i) {
6792 int r = isl_basic_map_is_universe(map->p[i]);
6800 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6802 return isl_map_plain_is_universe((isl_map *) set);
6805 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6807 return isl_set_plain_is_universe(set);
6810 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6812 struct isl_basic_set *bset = NULL;
6813 struct isl_vec *sample = NULL;
6820 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6823 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6824 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6825 copy = isl_basic_map_remove_redundancies(copy);
6826 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6827 isl_basic_map_free(copy);
6831 total = 1 + isl_basic_map_total_dim(bmap);
6832 if (bmap->sample && bmap->sample->size == total) {
6833 int contains = isl_basic_map_contains(bmap, bmap->sample);
6839 isl_vec_free(bmap->sample);
6840 bmap->sample = NULL;
6841 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6844 sample = isl_basic_set_sample_vec(bset);
6847 empty = sample->size == 0;
6848 isl_vec_free(bmap->sample);
6849 bmap->sample = sample;
6851 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6856 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6860 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6863 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6865 return isl_basic_map_plain_is_empty(bmap);
6868 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6872 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6875 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6877 return isl_basic_set_plain_is_empty(bset);
6880 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6882 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6885 struct isl_map *isl_basic_map_union(
6886 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6888 struct isl_map *map;
6889 if (!bmap1 || !bmap2)
6892 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
6894 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
6897 map = isl_map_add_basic_map(map, bmap1);
6898 map = isl_map_add_basic_map(map, bmap2);
6901 isl_basic_map_free(bmap1);
6902 isl_basic_map_free(bmap2);
6906 struct isl_set *isl_basic_set_union(
6907 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6909 return (struct isl_set *)isl_basic_map_union(
6910 (struct isl_basic_map *)bset1,
6911 (struct isl_basic_map *)bset2);
6914 /* Order divs such that any div only depends on previous divs */
6915 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6923 off = isl_space_dim(bmap->dim, isl_dim_all);
6925 for (i = 0; i < bmap->n_div; ++i) {
6927 if (isl_int_is_zero(bmap->div[i][0]))
6929 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6933 isl_basic_map_swap_div(bmap, i, i + pos);
6939 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6941 return (struct isl_basic_set *)
6942 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6945 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6952 for (i = 0; i < map->n; ++i) {
6953 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6964 /* Apply the expansion computed by isl_merge_divs.
6965 * The expansion itself is given by "exp" while the resulting
6966 * list of divs is given by "div".
6968 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6969 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6974 bset = isl_basic_set_cow(bset);
6978 if (div->n_row < bset->n_div)
6979 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6980 "not an expansion", goto error);
6982 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
6983 div->n_row - bset->n_div, 0,
6984 2 * (div->n_row - bset->n_div));
6986 n_div = bset->n_div;
6987 for (i = n_div; i < div->n_row; ++i)
6988 if (isl_basic_set_alloc_div(bset) < 0)
6992 for (i = div->n_row - 1; i >= 0; --i) {
6993 if (j >= 0 && exp[j] == i) {
6995 isl_basic_map_swap_div(bset, i, j);
6998 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6999 if (isl_basic_map_add_div_constraints(bset, i) < 0)
7007 isl_basic_set_free(bset);
7012 /* Look for a div in dst that corresponds to the div "div" in src.
7013 * The divs before "div" in src and dst are assumed to be the same.
7015 * Returns -1 if no corresponding div was found and the position
7016 * of the corresponding div in dst otherwise.
7018 static int find_div(struct isl_basic_map *dst,
7019 struct isl_basic_map *src, unsigned div)
7023 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7025 isl_assert(dst->ctx, div <= dst->n_div, return -1);
7026 for (i = div; i < dst->n_div; ++i)
7027 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7028 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7029 dst->n_div - div) == -1)
7034 struct isl_basic_map *isl_basic_map_align_divs(
7035 struct isl_basic_map *dst, struct isl_basic_map *src)
7038 unsigned total = isl_space_dim(src->dim, isl_dim_all);
7043 if (src->n_div == 0)
7046 for (i = 0; i < src->n_div; ++i)
7047 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7049 src = isl_basic_map_order_divs(src);
7050 dst = isl_basic_map_cow(dst);
7051 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7052 src->n_div, 0, 2 * src->n_div);
7055 for (i = 0; i < src->n_div; ++i) {
7056 int j = find_div(dst, src, i);
7058 j = isl_basic_map_alloc_div(dst);
7061 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7062 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7063 if (isl_basic_map_add_div_constraints(dst, j) < 0)
7067 isl_basic_map_swap_div(dst, i, j);
7071 isl_basic_map_free(dst);
7075 struct isl_basic_set *isl_basic_set_align_divs(
7076 struct isl_basic_set *dst, struct isl_basic_set *src)
7078 return (struct isl_basic_set *)isl_basic_map_align_divs(
7079 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7082 struct isl_map *isl_map_align_divs(struct isl_map *map)
7090 map = isl_map_compute_divs(map);
7091 map = isl_map_cow(map);
7095 for (i = 1; i < map->n; ++i)
7096 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7097 for (i = 1; i < map->n; ++i)
7098 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7100 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7104 struct isl_set *isl_set_align_divs(struct isl_set *set)
7106 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7109 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7110 __isl_take isl_map *map)
7114 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7115 map = isl_map_intersect_domain(map, set);
7116 set = isl_map_range(map);
7124 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7125 __isl_take isl_map *map)
7127 return isl_map_align_params_map_map_and(set, map, &set_apply);
7130 /* There is no need to cow as removing empty parts doesn't change
7131 * the meaning of the set.
7133 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7140 for (i = map->n-1; i >= 0; --i) {
7141 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
7143 isl_basic_map_free(map->p[i]);
7144 if (i != map->n-1) {
7145 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7146 map->p[i] = map->p[map->n-1];
7154 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7156 return (struct isl_set *)
7157 isl_map_remove_empty_parts((struct isl_map *)set);
7160 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7162 struct isl_basic_map *bmap;
7163 if (!map || map->n == 0)
7165 bmap = map->p[map->n-1];
7166 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7167 return isl_basic_map_copy(bmap);
7170 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7172 return (struct isl_basic_set *)
7173 isl_map_copy_basic_map((struct isl_map *)set);
7176 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7177 __isl_keep isl_basic_map *bmap)
7183 for (i = map->n-1; i >= 0; --i) {
7184 if (map->p[i] != bmap)
7186 map = isl_map_cow(map);
7189 isl_basic_map_free(map->p[i]);
7190 if (i != map->n-1) {
7191 ISL_F_CLR(map, ISL_SET_NORMALIZED);
7192 map->p[i] = map->p[map->n-1];
7203 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7204 struct isl_basic_set *bset)
7206 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7207 (struct isl_basic_map *)bset);
7210 /* Given two basic sets bset1 and bset2, compute the maximal difference
7211 * between the values of dimension pos in bset1 and those in bset2
7212 * for any common value of the parameters and dimensions preceding pos.
7214 static enum isl_lp_result basic_set_maximal_difference_at(
7215 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7216 int pos, isl_int *opt)
7219 struct isl_basic_map *bmap1 = NULL;
7220 struct isl_basic_map *bmap2 = NULL;
7221 struct isl_ctx *ctx;
7222 struct isl_vec *obj;
7225 unsigned dim1, dim2;
7226 enum isl_lp_result res;
7228 if (!bset1 || !bset2)
7229 return isl_lp_error;
7231 nparam = isl_basic_set_n_param(bset1);
7232 dim1 = isl_basic_set_n_dim(bset1);
7233 dim2 = isl_basic_set_n_dim(bset2);
7234 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7235 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7236 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7237 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7238 if (!bmap1 || !bmap2)
7240 bmap1 = isl_basic_map_cow(bmap1);
7241 bmap1 = isl_basic_map_extend(bmap1, nparam,
7242 pos, (dim1 - pos) + (dim2 - pos),
7243 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7244 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7247 total = isl_basic_map_total_dim(bmap1);
7249 obj = isl_vec_alloc(ctx, 1 + total);
7250 isl_seq_clr(obj->block.data, 1 + total);
7251 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7252 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7255 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7257 isl_basic_map_free(bmap1);
7261 isl_basic_map_free(bmap1);
7262 isl_basic_map_free(bmap2);
7263 return isl_lp_error;
7266 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7267 * for any common value of the parameters and dimensions preceding pos
7268 * in both basic sets, the values of dimension pos in bset1 are
7269 * smaller or larger than those in bset2.
7272 * 1 if bset1 follows bset2
7273 * -1 if bset1 precedes bset2
7274 * 0 if bset1 and bset2 are incomparable
7275 * -2 if some error occurred.
7277 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7278 struct isl_basic_set *bset2, int pos)
7281 enum isl_lp_result res;
7286 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7288 if (res == isl_lp_empty)
7290 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7291 res == isl_lp_unbounded)
7293 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7302 /* Given two basic sets bset1 and bset2, check whether
7303 * for any common value of the parameters and dimensions preceding pos
7304 * there is a value of dimension pos in bset1 that is larger
7305 * than a value of the same dimension in bset2.
7308 * 1 if there exists such a pair
7309 * 0 if there is no such pair, but there is a pair of equal values
7311 * -2 if some error occurred.
7313 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7314 __isl_keep isl_basic_set *bset2, int pos)
7317 enum isl_lp_result res;
7322 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7324 if (res == isl_lp_empty)
7326 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7327 res == isl_lp_unbounded)
7329 else if (res == isl_lp_ok && isl_int_is_neg(opt))
7331 else if (res == isl_lp_ok)
7340 /* Given two sets set1 and set2, check whether
7341 * for any common value of the parameters and dimensions preceding pos
7342 * there is a value of dimension pos in set1 that is larger
7343 * than a value of the same dimension in set2.
7346 * 1 if there exists such a pair
7347 * 0 if there is no such pair, but there is a pair of equal values
7349 * -2 if some error occurred.
7351 int isl_set_follows_at(__isl_keep isl_set *set1,
7352 __isl_keep isl_set *set2, int pos)
7360 for (i = 0; i < set1->n; ++i)
7361 for (j = 0; j < set2->n; ++j) {
7363 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7364 if (f == 1 || f == -2)
7373 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7374 unsigned pos, isl_int *val)
7382 total = isl_basic_map_total_dim(bmap);
7383 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7384 for (; d+1 > pos; --d)
7385 if (!isl_int_is_zero(bmap->eq[i][1+d]))
7389 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7391 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7393 if (!isl_int_is_one(bmap->eq[i][1+d]))
7396 isl_int_neg(*val, bmap->eq[i][0]);
7402 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7403 unsigned pos, isl_int *val)
7415 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
7418 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
7419 for (i = 1; fixed == 1 && i < map->n; ++i) {
7420 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
7421 if (fixed == 1 && isl_int_ne(tmp, v))
7425 isl_int_set(*val, v);
7431 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7432 unsigned pos, isl_int *val)
7434 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7438 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7441 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7444 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7445 enum isl_dim_type type, unsigned pos, isl_int *val)
7447 if (pos >= isl_basic_map_dim(bmap, type))
7449 return isl_basic_map_plain_has_fixed_var(bmap,
7450 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7453 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7454 enum isl_dim_type type, unsigned pos, isl_int *val)
7456 if (pos >= isl_map_dim(map, type))
7458 return isl_map_plain_has_fixed_var(map,
7459 map_offset(map, type) - 1 + pos, val);
7462 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
7463 enum isl_dim_type type, unsigned pos, isl_int *val)
7465 return isl_map_plain_is_fixed(set, type, pos, val);
7468 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7469 enum isl_dim_type type, unsigned pos, isl_int *val)
7471 return isl_map_plain_is_fixed(map, type, pos, val);
7474 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7475 * then return this fixed value in *val.
7477 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7478 unsigned dim, isl_int *val)
7480 return isl_basic_set_plain_has_fixed_var(bset,
7481 isl_basic_set_n_param(bset) + dim, val);
7484 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7485 * then return this fixed value in *val.
7487 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7488 unsigned dim, isl_int *val)
7490 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7493 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7494 unsigned dim, isl_int *val)
7496 return isl_set_plain_dim_is_fixed(set, dim, val);
7499 /* Check if input variable in has fixed value and if so and if val is not NULL,
7500 * then return this fixed value in *val.
7502 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7503 unsigned in, isl_int *val)
7505 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7508 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7509 * and if val is not NULL, then return this lower bound in *val.
7511 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7512 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7514 int i, i_eq = -1, i_ineq = -1;
7521 total = isl_basic_set_total_dim(bset);
7522 nparam = isl_basic_set_n_param(bset);
7523 for (i = 0; i < bset->n_eq; ++i) {
7524 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7530 for (i = 0; i < bset->n_ineq; ++i) {
7531 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7533 if (i_eq != -1 || i_ineq != -1)
7537 if (i_eq == -1 && i_ineq == -1)
7539 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7540 /* The coefficient should always be one due to normalization. */
7541 if (!isl_int_is_one(c[1+nparam+dim]))
7543 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7545 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7546 total - nparam - dim - 1) != -1)
7549 isl_int_neg(*val, c[0]);
7553 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7554 unsigned dim, isl_int *val)
7566 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7570 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7572 for (i = 1; fixed == 1 && i < set->n; ++i) {
7573 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7575 if (fixed == 1 && isl_int_ne(tmp, v))
7579 isl_int_set(*val, v);
7590 /* uset_gist depends on constraints without existentially quantified
7591 * variables sorting first.
7593 static int qsort_constraint_cmp(const void *p1, const void *p2)
7595 const struct constraint *c1 = (const struct constraint *)p1;
7596 const struct constraint *c2 = (const struct constraint *)p2;
7598 unsigned size = isl_min(c1->size, c2->size);
7600 l1 = isl_seq_last_non_zero(c1->c, size);
7601 l2 = isl_seq_last_non_zero(c2->c, size);
7606 return isl_seq_cmp(c1->c, c2->c, size);
7609 static struct isl_basic_map *isl_basic_map_sort_constraints(
7610 struct isl_basic_map *bmap)
7613 struct constraint *c;
7618 total = isl_basic_map_total_dim(bmap);
7619 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7622 for (i = 0; i < bmap->n_ineq; ++i) {
7624 c[i].c = bmap->ineq[i];
7626 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7627 for (i = 0; i < bmap->n_ineq; ++i)
7628 bmap->ineq[i] = c[i].c;
7632 isl_basic_map_free(bmap);
7636 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7637 __isl_take isl_basic_set *bset)
7639 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7640 (struct isl_basic_map *)bset);
7643 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7647 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7649 bmap = isl_basic_map_remove_redundancies(bmap);
7650 bmap = isl_basic_map_sort_constraints(bmap);
7651 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7655 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7657 return (struct isl_basic_set *)isl_basic_map_normalize(
7658 (struct isl_basic_map *)bset);
7661 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7662 const __isl_keep isl_basic_map *bmap2)
7669 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7670 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7671 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7672 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7673 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7674 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7675 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7676 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7677 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7678 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7679 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7681 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7683 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7685 if (bmap1->n_eq != bmap2->n_eq)
7686 return bmap1->n_eq - bmap2->n_eq;
7687 if (bmap1->n_ineq != bmap2->n_ineq)
7688 return bmap1->n_ineq - bmap2->n_ineq;
7689 if (bmap1->n_div != bmap2->n_div)
7690 return bmap1->n_div - bmap2->n_div;
7691 total = isl_basic_map_total_dim(bmap1);
7692 for (i = 0; i < bmap1->n_eq; ++i) {
7693 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7697 for (i = 0; i < bmap1->n_ineq; ++i) {
7698 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7702 for (i = 0; i < bmap1->n_div; ++i) {
7703 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7710 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
7711 const __isl_keep isl_basic_set *bset2)
7713 return isl_basic_map_plain_cmp(bset1, bset2);
7716 int isl_set_plain_cmp(const __isl_keep isl_set *set1,
7717 const __isl_keep isl_set *set2)
7723 if (set1->n != set2->n)
7724 return set1->n - set2->n;
7726 for (i = 0; i < set1->n; ++i) {
7727 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
7735 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7736 __isl_keep isl_basic_map *bmap2)
7738 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7741 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7742 __isl_keep isl_basic_set *bset2)
7744 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7745 (isl_basic_map *)bset2);
7748 static int qsort_bmap_cmp(const void *p1, const void *p2)
7750 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7751 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7753 return isl_basic_map_plain_cmp(bmap1, bmap2);
7756 /* We normalize in place, but if anything goes wrong we need
7757 * to return NULL, so we need to make sure we don't change the
7758 * meaning of any possible other copies of map.
7760 struct isl_map *isl_map_normalize(struct isl_map *map)
7763 struct isl_basic_map *bmap;
7767 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7769 for (i = 0; i < map->n; ++i) {
7770 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7773 isl_basic_map_free(map->p[i]);
7776 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7777 ISL_F_SET(map, ISL_MAP_NORMALIZED);
7778 map = isl_map_remove_empty_parts(map);
7781 for (i = map->n - 1; i >= 1; --i) {
7782 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7784 isl_basic_map_free(map->p[i-1]);
7785 for (j = i; j < map->n; ++j)
7786 map->p[j-1] = map->p[j];
7796 struct isl_set *isl_set_normalize(struct isl_set *set)
7798 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7801 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7811 if (!isl_space_is_equal(map1->dim, map2->dim))
7814 map1 = isl_map_copy(map1);
7815 map2 = isl_map_copy(map2);
7816 map1 = isl_map_normalize(map1);
7817 map2 = isl_map_normalize(map2);
7820 equal = map1->n == map2->n;
7821 for (i = 0; equal && i < map1->n; ++i) {
7822 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7835 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7837 return isl_map_plain_is_equal(map1, map2);
7840 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7842 return isl_map_plain_is_equal((struct isl_map *)set1,
7843 (struct isl_map *)set2);
7846 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7848 return isl_set_plain_is_equal(set1, set2);
7851 /* Return an interval that ranges from min to max (inclusive)
7853 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7854 isl_int min, isl_int max)
7857 struct isl_basic_set *bset = NULL;
7859 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7863 k = isl_basic_set_alloc_inequality(bset);
7866 isl_int_set_si(bset->ineq[k][1], 1);
7867 isl_int_neg(bset->ineq[k][0], min);
7869 k = isl_basic_set_alloc_inequality(bset);
7872 isl_int_set_si(bset->ineq[k][1], -1);
7873 isl_int_set(bset->ineq[k][0], max);
7877 isl_basic_set_free(bset);
7881 /* Return the Cartesian product of the basic sets in list (in the given order).
7883 __isl_give isl_basic_set *isl_basic_set_list_product(
7884 __isl_take struct isl_basic_set_list *list)
7892 struct isl_basic_set *product = NULL;
7896 isl_assert(list->ctx, list->n > 0, goto error);
7897 isl_assert(list->ctx, list->p[0], goto error);
7898 nparam = isl_basic_set_n_param(list->p[0]);
7899 dim = isl_basic_set_n_dim(list->p[0]);
7900 extra = list->p[0]->n_div;
7901 n_eq = list->p[0]->n_eq;
7902 n_ineq = list->p[0]->n_ineq;
7903 for (i = 1; i < list->n; ++i) {
7904 isl_assert(list->ctx, list->p[i], goto error);
7905 isl_assert(list->ctx,
7906 nparam == isl_basic_set_n_param(list->p[i]), goto error);
7907 dim += isl_basic_set_n_dim(list->p[i]);
7908 extra += list->p[i]->n_div;
7909 n_eq += list->p[i]->n_eq;
7910 n_ineq += list->p[i]->n_ineq;
7912 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7917 for (i = 0; i < list->n; ++i) {
7918 isl_basic_set_add_constraints(product,
7919 isl_basic_set_copy(list->p[i]), dim);
7920 dim += isl_basic_set_n_dim(list->p[i]);
7922 isl_basic_set_list_free(list);
7925 isl_basic_set_free(product);
7926 isl_basic_set_list_free(list);
7930 struct isl_basic_map *isl_basic_map_product(
7931 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7933 isl_space *dim_result = NULL;
7934 struct isl_basic_map *bmap;
7935 unsigned in1, in2, out1, out2, nparam, total, pos;
7936 struct isl_dim_map *dim_map1, *dim_map2;
7938 if (!bmap1 || !bmap2)
7941 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
7942 bmap2->dim, isl_dim_param), goto error);
7943 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
7944 isl_space_copy(bmap2->dim));
7946 in1 = isl_basic_map_n_in(bmap1);
7947 in2 = isl_basic_map_n_in(bmap2);
7948 out1 = isl_basic_map_n_out(bmap1);
7949 out2 = isl_basic_map_n_out(bmap2);
7950 nparam = isl_basic_map_n_param(bmap1);
7952 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7953 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7954 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7955 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7956 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7957 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7958 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7959 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7960 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7961 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7962 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7964 bmap = isl_basic_map_alloc_space(dim_result,
7965 bmap1->n_div + bmap2->n_div,
7966 bmap1->n_eq + bmap2->n_eq,
7967 bmap1->n_ineq + bmap2->n_ineq);
7968 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7969 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7970 bmap = isl_basic_map_simplify(bmap);
7971 return isl_basic_map_finalize(bmap);
7973 isl_basic_map_free(bmap1);
7974 isl_basic_map_free(bmap2);
7978 __isl_give isl_basic_map *isl_basic_map_flat_product(
7979 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7981 isl_basic_map *prod;
7983 prod = isl_basic_map_product(bmap1, bmap2);
7984 prod = isl_basic_map_flatten(prod);
7988 __isl_give isl_basic_set *isl_basic_set_flat_product(
7989 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7991 return isl_basic_map_flat_range_product(bset1, bset2);
7994 __isl_give isl_basic_map *isl_basic_map_domain_product(
7995 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7997 isl_space *space_result = NULL;
7998 isl_basic_map *bmap;
7999 unsigned in1, in2, out, nparam, total, pos;
8000 struct isl_dim_map *dim_map1, *dim_map2;
8002 if (!bmap1 || !bmap2)
8005 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8006 isl_space_copy(bmap2->dim));
8008 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8009 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8010 out = isl_basic_map_dim(bmap1, isl_dim_out);
8011 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8013 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8014 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8015 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8016 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8017 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8018 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8019 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8020 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8021 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8022 isl_dim_map_div(dim_map1, bmap1, pos += out);
8023 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8025 bmap = isl_basic_map_alloc_space(space_result,
8026 bmap1->n_div + bmap2->n_div,
8027 bmap1->n_eq + bmap2->n_eq,
8028 bmap1->n_ineq + bmap2->n_ineq);
8029 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8030 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8031 bmap = isl_basic_map_simplify(bmap);
8032 return isl_basic_map_finalize(bmap);
8034 isl_basic_map_free(bmap1);
8035 isl_basic_map_free(bmap2);
8039 __isl_give isl_basic_map *isl_basic_map_range_product(
8040 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8042 isl_space *dim_result = NULL;
8043 isl_basic_map *bmap;
8044 unsigned in, out1, out2, nparam, total, pos;
8045 struct isl_dim_map *dim_map1, *dim_map2;
8047 if (!bmap1 || !bmap2)
8050 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8051 isl_space_copy(bmap2->dim));
8053 in = isl_basic_map_dim(bmap1, isl_dim_in);
8054 out1 = isl_basic_map_n_out(bmap1);
8055 out2 = isl_basic_map_n_out(bmap2);
8056 nparam = isl_basic_map_n_param(bmap1);
8058 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8059 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8060 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8061 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8062 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8063 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8064 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8065 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8066 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8067 isl_dim_map_div(dim_map1, bmap1, pos += out2);
8068 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8070 bmap = isl_basic_map_alloc_space(dim_result,
8071 bmap1->n_div + bmap2->n_div,
8072 bmap1->n_eq + bmap2->n_eq,
8073 bmap1->n_ineq + bmap2->n_ineq);
8074 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8075 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8076 bmap = isl_basic_map_simplify(bmap);
8077 return isl_basic_map_finalize(bmap);
8079 isl_basic_map_free(bmap1);
8080 isl_basic_map_free(bmap2);
8084 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8085 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8087 isl_basic_map *prod;
8089 prod = isl_basic_map_range_product(bmap1, bmap2);
8090 prod = isl_basic_map_flatten_range(prod);
8094 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8095 __isl_take isl_map *map2,
8096 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8097 __isl_take isl_space *right),
8098 __isl_give isl_basic_map *(*basic_map_product)(
8099 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8102 struct isl_map *result;
8108 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8109 map2->dim, isl_dim_param), goto error);
8111 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8112 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8113 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8115 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8116 isl_space_copy(map2->dim)),
8117 map1->n * map2->n, flags);
8120 for (i = 0; i < map1->n; ++i)
8121 for (j = 0; j < map2->n; ++j) {
8122 struct isl_basic_map *part;
8123 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8124 isl_basic_map_copy(map2->p[j]));
8125 if (isl_basic_map_is_empty(part))
8126 isl_basic_map_free(part);
8128 result = isl_map_add_basic_map(result, part);
8141 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8143 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8144 __isl_take isl_map *map2)
8146 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8149 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8150 __isl_take isl_map *map2)
8152 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8155 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8157 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8158 __isl_take isl_map *map2)
8162 prod = isl_map_product(map1, map2);
8163 prod = isl_map_flatten(prod);
8167 /* Given two set A and B, construct its Cartesian product A x B.
8169 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8171 return isl_map_range_product(set1, set2);
8174 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8175 __isl_take isl_set *set2)
8177 return isl_map_flat_range_product(set1, set2);
8180 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8182 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8183 __isl_take isl_map *map2)
8185 return map_product(map1, map2, &isl_space_domain_product,
8186 &isl_basic_map_domain_product);
8189 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8191 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8192 __isl_take isl_map *map2)
8194 return map_product(map1, map2, &isl_space_range_product,
8195 &isl_basic_map_range_product);
8198 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8199 __isl_take isl_map *map2)
8201 return isl_map_align_params_map_map_and(map1, map2,
8202 &map_domain_product_aligned);
8205 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8206 __isl_take isl_map *map2)
8208 return isl_map_align_params_map_map_and(map1, map2,
8209 &map_range_product_aligned);
8212 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8214 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8215 __isl_take isl_map *map2)
8219 prod = isl_map_domain_product(map1, map2);
8220 prod = isl_map_flatten_domain(prod);
8224 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8226 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8227 __isl_take isl_map *map2)
8231 prod = isl_map_range_product(map1, map2);
8232 prod = isl_map_flatten_range(prod);
8236 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8239 uint32_t hash = isl_hash_init();
8244 bmap = isl_basic_map_copy(bmap);
8245 bmap = isl_basic_map_normalize(bmap);
8248 total = isl_basic_map_total_dim(bmap);
8249 isl_hash_byte(hash, bmap->n_eq & 0xFF);
8250 for (i = 0; i < bmap->n_eq; ++i) {
8252 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8253 isl_hash_hash(hash, c_hash);
8255 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8256 for (i = 0; i < bmap->n_ineq; ++i) {
8258 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8259 isl_hash_hash(hash, c_hash);
8261 isl_hash_byte(hash, bmap->n_div & 0xFF);
8262 for (i = 0; i < bmap->n_div; ++i) {
8264 if (isl_int_is_zero(bmap->div[i][0]))
8266 isl_hash_byte(hash, i & 0xFF);
8267 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8268 isl_hash_hash(hash, c_hash);
8270 isl_basic_map_free(bmap);
8274 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8276 return isl_basic_map_get_hash((isl_basic_map *)bset);
8279 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8286 map = isl_map_copy(map);
8287 map = isl_map_normalize(map);
8291 hash = isl_hash_init();
8292 for (i = 0; i < map->n; ++i) {
8294 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8295 isl_hash_hash(hash, bmap_hash);
8303 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8305 return isl_map_get_hash((isl_map *)set);
8308 /* Check if the value for dimension dim is completely determined
8309 * by the values of the other parameters and variables.
8310 * That is, check if dimension dim is involved in an equality.
8312 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8319 nparam = isl_basic_set_n_param(bset);
8320 for (i = 0; i < bset->n_eq; ++i)
8321 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8326 /* Check if the value for dimension dim is completely determined
8327 * by the values of the other parameters and variables.
8328 * That is, check if dimension dim is involved in an equality
8329 * for each of the subsets.
8331 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8337 for (i = 0; i < set->n; ++i) {
8339 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8346 int isl_set_n_basic_set(__isl_keep isl_set *set)
8348 return set ? set->n : 0;
8351 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8352 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8359 for (i = 0; i < map->n; ++i)
8360 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8366 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8367 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8374 for (i = 0; i < set->n; ++i)
8375 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8381 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8388 bset = isl_basic_set_cow(bset);
8392 dim = isl_basic_set_get_space(bset);
8393 dim = isl_space_lift(dim, bset->n_div);
8396 isl_space_free(bset->dim);
8398 bset->extra -= bset->n_div;
8401 bset = isl_basic_set_finalize(bset);
8405 isl_basic_set_free(bset);
8409 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
8415 set = isl_set_align_divs(set);
8420 set = isl_set_cow(set);
8424 n_div = set->p[0]->n_div;
8425 dim = isl_set_get_space(set);
8426 dim = isl_space_lift(dim, n_div);
8429 isl_space_free(set->dim);
8432 for (i = 0; i < set->n; ++i) {
8433 set->p[i] = isl_basic_set_lift(set->p[i]);
8444 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8447 struct isl_basic_map *bmap;
8454 set = isl_set_align_divs(set);
8459 dim = isl_set_get_space(set);
8460 if (set->n == 0 || set->p[0]->n_div == 0) {
8462 return isl_map_identity(isl_space_map_from_set(dim));
8465 n_div = set->p[0]->n_div;
8466 dim = isl_space_map_from_set(dim);
8467 n_param = isl_space_dim(dim, isl_dim_param);
8468 n_set = isl_space_dim(dim, isl_dim_in);
8469 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
8470 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
8471 for (i = 0; i < n_set; ++i)
8472 bmap = var_equal(bmap, i);
8474 total = n_param + n_set + n_set + n_div;
8475 for (i = 0; i < n_div; ++i) {
8476 k = isl_basic_map_alloc_inequality(bmap);
8479 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8480 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8481 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8482 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8483 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8484 set->p[0]->div[i][0]);
8486 l = isl_basic_map_alloc_inequality(bmap);
8489 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8490 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8491 set->p[0]->div[i][0]);
8492 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8496 bmap = isl_basic_map_simplify(bmap);
8497 bmap = isl_basic_map_finalize(bmap);
8498 return isl_map_from_basic_map(bmap);
8501 isl_basic_map_free(bmap);
8505 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8513 dim = isl_basic_set_total_dim(bset);
8514 size += bset->n_eq * (1 + dim);
8515 size += bset->n_ineq * (1 + dim);
8516 size += bset->n_div * (2 + dim);
8521 int isl_set_size(__isl_keep isl_set *set)
8529 for (i = 0; i < set->n; ++i)
8530 size += isl_basic_set_size(set->p[i]);
8535 /* Check if there is any lower bound (if lower == 0) and/or upper
8536 * bound (if upper == 0) on the specified dim.
8538 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8539 enum isl_dim_type type, unsigned pos, int lower, int upper)
8546 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8548 pos += isl_basic_map_offset(bmap, type);
8550 for (i = 0; i < bmap->n_div; ++i) {
8551 if (isl_int_is_zero(bmap->div[i][0]))
8553 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
8557 for (i = 0; i < bmap->n_eq; ++i)
8558 if (!isl_int_is_zero(bmap->eq[i][pos]))
8561 for (i = 0; i < bmap->n_ineq; ++i) {
8562 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8569 return lower && upper;
8572 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8573 enum isl_dim_type type, unsigned pos)
8575 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
8578 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
8579 enum isl_dim_type type, unsigned pos)
8581 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
8584 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
8585 enum isl_dim_type type, unsigned pos)
8587 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
8590 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8591 enum isl_dim_type type, unsigned pos)
8598 for (i = 0; i < map->n; ++i) {
8600 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8601 if (bounded < 0 || !bounded)
8608 /* Return 1 if the specified dim is involved in both an upper bound
8609 * and a lower bound.
8611 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8612 enum isl_dim_type type, unsigned pos)
8614 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8617 static int has_bound(__isl_keep isl_map *map,
8618 enum isl_dim_type type, unsigned pos,
8619 int (*fn)(__isl_keep isl_basic_map *bmap,
8620 enum isl_dim_type type, unsigned pos))
8627 for (i = 0; i < map->n; ++i) {
8629 bounded = fn(map->p[i], type, pos);
8630 if (bounded < 0 || bounded)
8637 /* Return 1 if the specified dim is involved in any lower bound.
8639 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
8640 enum isl_dim_type type, unsigned pos)
8642 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
8645 /* Return 1 if the specified dim is involved in any upper bound.
8647 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
8648 enum isl_dim_type type, unsigned pos)
8650 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
8653 /* For each of the "n" variables starting at "first", determine
8654 * the sign of the variable and put the results in the first "n"
8655 * elements of the array "signs".
8657 * 1 means that the variable is non-negative
8658 * -1 means that the variable is non-positive
8659 * 0 means the variable attains both positive and negative values.
8661 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8662 unsigned first, unsigned n, int *signs)
8664 isl_vec *bound = NULL;
8665 struct isl_tab *tab = NULL;
8666 struct isl_tab_undo *snap;
8669 if (!bset || !signs)
8672 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8673 tab = isl_tab_from_basic_set(bset);
8677 isl_seq_clr(bound->el, bound->size);
8678 isl_int_set_si(bound->el[0], -1);
8680 snap = isl_tab_snap(tab);
8681 for (i = 0; i < n; ++i) {
8684 isl_int_set_si(bound->el[1 + first + i], -1);
8685 if (isl_tab_add_ineq(tab, bound->el) < 0)
8688 isl_int_set_si(bound->el[1 + first + i], 0);
8689 if (isl_tab_rollback(tab, snap) < 0)
8697 isl_int_set_si(bound->el[1 + first + i], 1);
8698 if (isl_tab_add_ineq(tab, bound->el) < 0)
8701 isl_int_set_si(bound->el[1 + first + i], 0);
8702 if (isl_tab_rollback(tab, snap) < 0)
8705 signs[i] = empty ? -1 : 0;
8709 isl_vec_free(bound);
8713 isl_vec_free(bound);
8717 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8718 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8720 if (!bset || !signs)
8722 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8725 first += pos(bset->dim, type) - 1;
8726 return isl_basic_set_vars_get_sign(bset, first, n, signs);
8729 /* Check if the given basic map is obviously single-valued.
8730 * In particular, for each output dimension, check that there is
8731 * an equality that defines the output dimension in terms of
8732 * earlier dimensions.
8734 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8744 total = 1 + isl_basic_map_total_dim(bmap);
8745 n_out = isl_basic_map_dim(bmap, isl_dim_out);
8746 o_out = isl_basic_map_offset(bmap, isl_dim_out);
8748 for (i = 0; i < n_out; ++i) {
8749 for (j = 0; j < bmap->n_eq; ++j) {
8750 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8752 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8753 total - (o_out + i + 1)) == -1)
8756 if (j >= bmap->n_eq)
8763 /* Check if the given map is obviously single-valued.
8765 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8774 return isl_basic_map_plain_is_single_valued(map->p[0]);
8777 /* Check if the given map is single-valued.
8782 * and check if the result is a subset of the identity mapping.
8784 int isl_map_is_single_valued(__isl_keep isl_map *map)
8791 sv = isl_map_plain_is_single_valued(map);
8795 test = isl_map_reverse(isl_map_copy(map));
8796 test = isl_map_apply_range(test, isl_map_copy(map));
8798 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
8799 id = isl_map_identity(dim);
8801 sv = isl_map_is_subset(test, id);
8809 int isl_map_is_injective(__isl_keep isl_map *map)
8813 map = isl_map_copy(map);
8814 map = isl_map_reverse(map);
8815 in = isl_map_is_single_valued(map);
8821 /* Check if the given map is obviously injective.
8823 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8827 map = isl_map_copy(map);
8828 map = isl_map_reverse(map);
8829 in = isl_map_plain_is_single_valued(map);
8835 int isl_map_is_bijective(__isl_keep isl_map *map)
8839 sv = isl_map_is_single_valued(map);
8843 return isl_map_is_injective(map);
8846 int isl_set_is_singleton(__isl_keep isl_set *set)
8848 return isl_map_is_single_valued((isl_map *)set);
8851 int isl_map_is_translation(__isl_keep isl_map *map)
8856 delta = isl_map_deltas(isl_map_copy(map));
8857 ok = isl_set_is_singleton(delta);
8858 isl_set_free(delta);
8863 static int unique(isl_int *p, unsigned pos, unsigned len)
8865 if (isl_seq_first_non_zero(p, pos) != -1)
8867 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8872 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8881 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8884 nvar = isl_basic_set_dim(bset, isl_dim_set);
8885 ovar = isl_space_offset(bset->dim, isl_dim_set);
8886 for (j = 0; j < nvar; ++j) {
8887 int lower = 0, upper = 0;
8888 for (i = 0; i < bset->n_eq; ++i) {
8889 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8891 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8897 for (i = 0; i < bset->n_ineq; ++i) {
8898 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8900 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8902 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8907 if (!lower || !upper)
8914 int isl_set_is_box(__isl_keep isl_set *set)
8921 return isl_basic_set_is_box(set->p[0]);
8924 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8929 return isl_space_is_wrapping(bset->dim);
8932 int isl_set_is_wrapping(__isl_keep isl_set *set)
8937 return isl_space_is_wrapping(set->dim);
8940 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8942 bmap = isl_basic_map_cow(bmap);
8946 bmap->dim = isl_space_wrap(bmap->dim);
8950 bmap = isl_basic_map_finalize(bmap);
8952 return (isl_basic_set *)bmap;
8954 isl_basic_map_free(bmap);
8958 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8962 map = isl_map_cow(map);
8966 for (i = 0; i < map->n; ++i) {
8967 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8971 map->dim = isl_space_wrap(map->dim);
8975 return (isl_set *)map;
8981 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8983 bset = isl_basic_set_cow(bset);
8987 bset->dim = isl_space_unwrap(bset->dim);
8991 bset = isl_basic_set_finalize(bset);
8993 return (isl_basic_map *)bset;
8995 isl_basic_set_free(bset);
8999 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9006 if (!isl_set_is_wrapping(set))
9007 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9010 set = isl_set_cow(set);
9014 for (i = 0; i < set->n; ++i) {
9015 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9020 set->dim = isl_space_unwrap(set->dim);
9024 return (isl_map *)set;
9030 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9031 enum isl_dim_type type)
9036 if (!isl_space_is_named_or_nested(bmap->dim, type))
9039 bmap = isl_basic_map_cow(bmap);
9043 bmap->dim = isl_space_reset(bmap->dim, type);
9047 bmap = isl_basic_map_finalize(bmap);
9051 isl_basic_map_free(bmap);
9055 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9056 enum isl_dim_type type)
9063 if (!isl_space_is_named_or_nested(map->dim, type))
9066 map = isl_map_cow(map);
9070 for (i = 0; i < map->n; ++i) {
9071 map->p[i] = isl_basic_map_reset(map->p[i], type);
9075 map->dim = isl_space_reset(map->dim, type);
9085 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9090 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9093 bmap = isl_basic_map_cow(bmap);
9097 bmap->dim = isl_space_flatten(bmap->dim);
9101 bmap = isl_basic_map_finalize(bmap);
9105 isl_basic_map_free(bmap);
9109 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9111 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9114 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9115 __isl_take isl_basic_map *bmap)
9120 if (!bmap->dim->nested[0])
9123 bmap = isl_basic_map_cow(bmap);
9127 bmap->dim = isl_space_flatten_domain(bmap->dim);
9131 bmap = isl_basic_map_finalize(bmap);
9135 isl_basic_map_free(bmap);
9139 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9140 __isl_take isl_basic_map *bmap)
9145 if (!bmap->dim->nested[1])
9148 bmap = isl_basic_map_cow(bmap);
9152 bmap->dim = isl_space_flatten_range(bmap->dim);
9156 bmap = isl_basic_map_finalize(bmap);
9160 isl_basic_map_free(bmap);
9164 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9171 if (!map->dim->nested[0] && !map->dim->nested[1])
9174 map = isl_map_cow(map);
9178 for (i = 0; i < map->n; ++i) {
9179 map->p[i] = isl_basic_map_flatten(map->p[i]);
9183 map->dim = isl_space_flatten(map->dim);
9193 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9195 return (isl_set *)isl_map_flatten((isl_map *)set);
9198 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9200 isl_space *dim, *flat_dim;
9203 dim = isl_set_get_space(set);
9204 flat_dim = isl_space_flatten(isl_space_copy(dim));
9205 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9206 map = isl_map_intersect_domain(map, set);
9211 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9218 if (!map->dim->nested[0])
9221 map = isl_map_cow(map);
9225 for (i = 0; i < map->n; ++i) {
9226 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9230 map->dim = isl_space_flatten_domain(map->dim);
9240 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9247 if (!map->dim->nested[1])
9250 map = isl_map_cow(map);
9254 for (i = 0; i < map->n; ++i) {
9255 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9259 map->dim = isl_space_flatten_range(map->dim);
9269 /* Reorder the dimensions of "bmap" according to the given dim_map
9270 * and set the dimension specification to "dim".
9272 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9273 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9277 bmap = isl_basic_map_cow(bmap);
9278 if (!bmap || !dim || !dim_map)
9281 res = isl_basic_map_alloc_space(dim,
9282 bmap->n_div, bmap->n_eq, bmap->n_ineq);
9283 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9284 res = isl_basic_map_finalize(res);
9288 isl_basic_map_free(bmap);
9289 isl_space_free(dim);
9293 /* Reorder the dimensions of "map" according to given reordering.
9295 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9296 __isl_take isl_reordering *r)
9299 struct isl_dim_map *dim_map;
9301 map = isl_map_cow(map);
9302 dim_map = isl_dim_map_from_reordering(r);
9303 if (!map || !r || !dim_map)
9306 for (i = 0; i < map->n; ++i) {
9307 struct isl_dim_map *dim_map_i;
9309 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9311 map->p[i] = isl_basic_map_realign(map->p[i],
9312 isl_space_copy(r->dim), dim_map_i);
9318 map = isl_map_reset_space(map, isl_space_copy(r->dim));
9320 isl_reordering_free(r);
9326 isl_reordering_free(r);
9330 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
9331 __isl_take isl_reordering *r)
9333 return (isl_set *)isl_map_realign((isl_map *)set, r);
9336 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
9337 __isl_take isl_space *model)
9344 ctx = isl_space_get_ctx(model);
9345 if (!isl_space_has_named_params(model))
9346 isl_die(ctx, isl_error_invalid,
9347 "model has unnamed parameters", goto error);
9348 if (!isl_space_has_named_params(map->dim))
9349 isl_die(ctx, isl_error_invalid,
9350 "relation has unnamed parameters", goto error);
9351 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
9352 isl_reordering *exp;
9354 model = isl_space_drop_dims(model, isl_dim_in,
9355 0, isl_space_dim(model, isl_dim_in));
9356 model = isl_space_drop_dims(model, isl_dim_out,
9357 0, isl_space_dim(model, isl_dim_out));
9358 exp = isl_parameter_alignment_reordering(map->dim, model);
9359 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
9360 map = isl_map_realign(map, exp);
9363 isl_space_free(model);
9366 isl_space_free(model);
9371 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
9372 __isl_take isl_space *model)
9374 return isl_map_align_params(set, model);
9377 __isl_give isl_mat *isl_basic_map_equalities_matrix(
9378 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9379 enum isl_dim_type c2, enum isl_dim_type c3,
9380 enum isl_dim_type c4, enum isl_dim_type c5)
9382 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9383 struct isl_mat *mat;
9389 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
9390 isl_basic_map_total_dim(bmap) + 1);
9393 for (i = 0; i < bmap->n_eq; ++i)
9394 for (j = 0, pos = 0; j < 5; ++j) {
9395 int off = isl_basic_map_offset(bmap, c[j]);
9396 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9397 isl_int_set(mat->row[i][pos],
9398 bmap->eq[i][off + k]);
9406 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
9407 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
9408 enum isl_dim_type c2, enum isl_dim_type c3,
9409 enum isl_dim_type c4, enum isl_dim_type c5)
9411 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9412 struct isl_mat *mat;
9418 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
9419 isl_basic_map_total_dim(bmap) + 1);
9422 for (i = 0; i < bmap->n_ineq; ++i)
9423 for (j = 0, pos = 0; j < 5; ++j) {
9424 int off = isl_basic_map_offset(bmap, c[j]);
9425 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9426 isl_int_set(mat->row[i][pos],
9427 bmap->ineq[i][off + k]);
9435 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
9436 __isl_take isl_space *dim,
9437 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9438 enum isl_dim_type c2, enum isl_dim_type c3,
9439 enum isl_dim_type c4, enum isl_dim_type c5)
9441 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
9442 isl_basic_map *bmap;
9448 if (!dim || !eq || !ineq)
9451 if (eq->n_col != ineq->n_col)
9452 isl_die(dim->ctx, isl_error_invalid,
9453 "equalities and inequalities matrices should have "
9454 "same number of columns", goto error);
9456 total = 1 + isl_space_dim(dim, isl_dim_all);
9458 if (eq->n_col < total)
9459 isl_die(dim->ctx, isl_error_invalid,
9460 "number of columns too small", goto error);
9462 extra = eq->n_col - total;
9464 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
9465 eq->n_row, ineq->n_row);
9468 for (i = 0; i < extra; ++i) {
9469 k = isl_basic_map_alloc_div(bmap);
9472 isl_int_set_si(bmap->div[k][0], 0);
9474 for (i = 0; i < eq->n_row; ++i) {
9475 l = isl_basic_map_alloc_equality(bmap);
9478 for (j = 0, pos = 0; j < 5; ++j) {
9479 int off = isl_basic_map_offset(bmap, c[j]);
9480 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9481 isl_int_set(bmap->eq[l][off + k],
9487 for (i = 0; i < ineq->n_row; ++i) {
9488 l = isl_basic_map_alloc_inequality(bmap);
9491 for (j = 0, pos = 0; j < 5; ++j) {
9492 int off = isl_basic_map_offset(bmap, c[j]);
9493 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
9494 isl_int_set(bmap->ineq[l][off + k],
9501 isl_space_free(dim);
9507 isl_space_free(dim);
9513 __isl_give isl_mat *isl_basic_set_equalities_matrix(
9514 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9515 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9517 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
9518 c1, c2, c3, c4, isl_dim_in);
9521 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
9522 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
9523 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9525 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
9526 c1, c2, c3, c4, isl_dim_in);
9529 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
9530 __isl_take isl_space *dim,
9531 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9532 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9534 return (isl_basic_set*)
9535 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9536 c1, c2, c3, c4, isl_dim_in);
9539 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9544 return isl_space_can_zip(bmap->dim);
9547 int isl_map_can_zip(__isl_keep isl_map *map)
9552 return isl_space_can_zip(map->dim);
9555 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9556 * (A -> C) -> (B -> D).
9558 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9567 if (!isl_basic_map_can_zip(bmap))
9568 isl_die(bmap->ctx, isl_error_invalid,
9569 "basic map cannot be zipped", goto error);
9570 pos = isl_basic_map_offset(bmap, isl_dim_in) +
9571 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
9572 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
9573 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
9574 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9577 bmap->dim = isl_space_zip(bmap->dim);
9582 isl_basic_map_free(bmap);
9586 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9587 * (A -> C) -> (B -> D).
9589 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9596 if (!isl_map_can_zip(map))
9597 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9600 map = isl_map_cow(map);
9604 for (i = 0; i < map->n; ++i) {
9605 map->p[i] = isl_basic_map_zip(map->p[i]);
9610 map->dim = isl_space_zip(map->dim);
9620 /* Construct a basic map mapping the domain of the affine expression
9621 * to a one-dimensional range prescribed by the affine expression.
9623 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9627 isl_local_space *ls;
9628 isl_basic_map *bmap;
9633 ls = isl_aff_get_local_space(aff);
9634 bmap = isl_basic_map_from_local_space(ls);
9635 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9636 k = isl_basic_map_alloc_equality(bmap);
9640 pos = isl_basic_map_offset(bmap, isl_dim_out);
9641 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9642 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9643 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9644 aff->v->size - (pos + 1));
9647 bmap = isl_basic_map_finalize(bmap);
9651 isl_basic_map_free(bmap);
9655 /* Construct a basic map mapping the domain the multi-affine expression
9656 * to its range, with each dimension in the range equated to the
9657 * corresponding affine expression.
9659 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
9660 __isl_take isl_multi_aff *maff)
9664 isl_basic_map *bmap;
9669 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
9670 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
9671 "invalid space", return isl_multi_aff_free(maff));
9673 space = isl_space_domain(isl_multi_aff_get_space(maff));
9674 bmap = isl_basic_map_universe(isl_space_from_domain(space));
9676 for (i = 0; i < maff->n; ++i) {
9678 isl_basic_map *bmap_i;
9680 aff = isl_aff_copy(maff->p[i]);
9681 bmap_i = isl_basic_map_from_aff(aff);
9683 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9686 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
9688 isl_multi_aff_free(maff);
9692 /* Construct a basic map mapping a domain in the given space to
9693 * to an n-dimensional range, with n the number of elements in the list,
9694 * where each coordinate in the range is prescribed by the
9695 * corresponding affine expression.
9696 * The domains of all affine expressions in the list are assumed to match
9699 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9700 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
9704 isl_basic_map *bmap;
9709 dim = isl_space_from_domain(domain_dim);
9710 bmap = isl_basic_map_universe(dim);
9712 for (i = 0; i < list->n; ++i) {
9714 isl_basic_map *bmap_i;
9716 aff = isl_aff_copy(list->p[i]);
9717 bmap_i = isl_basic_map_from_aff(aff);
9719 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9722 isl_aff_list_free(list);
9726 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9727 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9729 return isl_map_equate(set, type1, pos1, type2, pos2);
9732 /* Add a constraint imposing that the given two dimensions are equal.
9734 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9735 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9737 isl_basic_map *bmap = NULL;
9743 if (pos1 >= isl_map_dim(map, type1))
9744 isl_die(map->ctx, isl_error_invalid,
9745 "index out of bounds", goto error);
9746 if (pos2 >= isl_map_dim(map, type2))
9747 isl_die(map->ctx, isl_error_invalid,
9748 "index out of bounds", goto error);
9750 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9751 i = isl_basic_map_alloc_equality(bmap);
9754 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9755 pos1 += isl_basic_map_offset(bmap, type1);
9756 pos2 += isl_basic_map_offset(bmap, type2);
9757 isl_int_set_si(bmap->eq[i][pos1], -1);
9758 isl_int_set_si(bmap->eq[i][pos2], 1);
9759 bmap = isl_basic_map_finalize(bmap);
9761 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9765 isl_basic_map_free(bmap);
9770 /* Add a constraint imposing that the given two dimensions have opposite values.
9772 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9773 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9775 isl_basic_map *bmap = NULL;
9781 if (pos1 >= isl_map_dim(map, type1))
9782 isl_die(map->ctx, isl_error_invalid,
9783 "index out of bounds", goto error);
9784 if (pos2 >= isl_map_dim(map, type2))
9785 isl_die(map->ctx, isl_error_invalid,
9786 "index out of bounds", goto error);
9788 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
9789 i = isl_basic_map_alloc_equality(bmap);
9792 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9793 pos1 += isl_basic_map_offset(bmap, type1);
9794 pos2 += isl_basic_map_offset(bmap, type2);
9795 isl_int_set_si(bmap->eq[i][pos1], 1);
9796 isl_int_set_si(bmap->eq[i][pos2], 1);
9797 bmap = isl_basic_map_finalize(bmap);
9799 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9803 isl_basic_map_free(bmap);
9808 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
9812 isl_local_space *ls;
9817 if (!isl_basic_map_divs_known(bmap))
9818 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
9819 "some divs are unknown", return NULL);
9821 ls = isl_basic_map_get_local_space(bmap);
9822 div = isl_local_space_get_div(ls, pos);
9823 isl_local_space_free(ls);
9828 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
9831 return isl_basic_map_get_div(bset, pos);
9834 /* Plug in "subs" for dimension "type", "pos" of "bset".
9836 * Let i be the dimension to replace and let "subs" be of the form
9840 * Any integer division with a non-zero coefficient for i,
9842 * floor((a i + g)/m)
9846 * floor((a f + d g)/(m d))
9848 * Constraints of the form
9856 __isl_give isl_basic_set *isl_basic_set_substitute(
9857 __isl_take isl_basic_set *bset,
9858 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9864 if (bset && isl_basic_set_plain_is_empty(bset))
9867 bset = isl_basic_set_cow(bset);
9871 ctx = isl_basic_set_get_ctx(bset);
9872 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
9873 isl_die(ctx, isl_error_invalid,
9874 "spaces don't match", goto error);
9875 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
9876 isl_die(ctx, isl_error_unsupported,
9877 "cannot handle divs yet", goto error);
9879 pos += isl_basic_set_offset(bset, type);
9883 for (i = 0; i < bset->n_eq; ++i) {
9884 if (isl_int_is_zero(bset->eq[i][pos]))
9886 isl_int_set(v, bset->eq[i][pos]);
9887 isl_int_set_si(bset->eq[i][pos], 0);
9888 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
9889 v, subs->v->el + 1, subs->v->size - 1);
9892 for (i = 0; i < bset->n_ineq; ++i) {
9893 if (isl_int_is_zero(bset->ineq[i][pos]))
9895 isl_int_set(v, bset->ineq[i][pos]);
9896 isl_int_set_si(bset->ineq[i][pos], 0);
9897 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
9898 v, subs->v->el + 1, subs->v->size - 1);
9901 for (i = 0; i < bset->n_div; ++i) {
9902 if (isl_int_is_zero(bset->div[i][1 + pos]))
9904 isl_int_set(v, bset->div[i][1 + pos]);
9905 isl_int_set_si(bset->div[i][1 + pos], 0);
9906 isl_seq_combine(bset->div[i] + 1,
9907 subs->v->el[0], bset->div[i] + 1,
9908 v, subs->v->el + 1, subs->v->size - 1);
9909 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
9914 bset = isl_basic_set_simplify(bset);
9915 return isl_basic_set_finalize(bset);
9917 isl_basic_set_free(bset);
9921 /* Plug in "subs" for dimension "type", "pos" of "set".
9923 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
9924 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
9928 if (set && isl_set_plain_is_empty(set))
9931 set = isl_set_cow(set);
9935 for (i = set->n - 1; i >= 0; --i) {
9936 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
9939 if (isl_basic_set_plain_is_empty(set->p[i])) {
9940 isl_basic_set_free(set->p[i]);
9941 if (i != set->n - 1)
9942 set->p[i] = set->p[set->n - 1];